mfplat: Read queue subscriber within the critical section.
[wine/zf.git] / dlls / comctl32 / listview.c
blob799ec509f33a95cac6e5ca299dd870b889e2c767
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 * TODO:
29 * Default Message Processing
30 * -- WM_CREATE: create the icon and small icon image lists at this point only if
31 * the LVS_SHAREIMAGELISTS style is not specified.
32 * -- WM_WINDOWPOSCHANGED: arrange the list items if the current view is icon
33 * or small icon and the LVS_AUTOARRANGE style is specified.
34 * -- WM_TIMER
35 * -- WM_WININICHANGE
37 * Features
38 * -- Hot item handling, mouse hovering
39 * -- Workareas support
40 * -- Tilemode support
41 * -- Groups support
43 * Bugs
44 * -- Expand large item in ICON mode when the cursor is flying over the icon or text.
45 * -- Support CustomDraw options for _WIN32_IE >= 0x560 (see NMLVCUSTOMDRAW docs).
46 * -- LVA_SNAPTOGRID not implemented
47 * -- LISTVIEW_ApproximateViewRect partially implemented
48 * -- LISTVIEW_StyleChanged doesn't handle some changes too well
50 * Speedups
51 * -- LISTVIEW_GetNextItem needs to be rewritten. It is currently
52 * linear in the number of items in the list, and this is
53 * unacceptable for large lists.
54 * -- if list is sorted by item text LISTVIEW_InsertItemT could use
55 * binary search to calculate item index (e.g. DPA_Search()).
56 * This requires sorted state to be reliably tracked in item modifiers.
57 * -- we should keep an ordered array of coordinates in iconic mode.
58 * This would allow framing items (iterator_frameditems),
59 * and finding the nearest item (LVFI_NEARESTXY) a lot more efficiently.
61 * Flags
62 * -- LVIF_COLUMNS
63 * -- LVIF_GROUPID
65 * States
66 * -- LVIS_ACTIVATING (not currently supported by comctl32.dll version 6.0)
67 * -- LVIS_DROPHILITED
69 * Styles
70 * -- LVS_NOLABELWRAP
71 * -- LVS_NOSCROLL (see Q137520)
72 * -- LVS_ALIGNTOP
74 * Extended Styles
75 * -- LVS_EX_BORDERSELECT
76 * -- LVS_EX_FLATSB
77 * -- LVS_EX_INFOTIP
78 * -- LVS_EX_LABELTIP
79 * -- LVS_EX_MULTIWORKAREAS
80 * -- LVS_EX_REGIONAL
81 * -- LVS_EX_SIMPLESELECT
82 * -- LVS_EX_TWOCLICKACTIVATE
83 * -- LVS_EX_UNDERLINECOLD
84 * -- LVS_EX_UNDERLINEHOT
86 * Notifications:
87 * -- LVN_BEGINSCROLL, LVN_ENDSCROLL
88 * -- LVN_GETINFOTIP
89 * -- LVN_HOTTRACK
90 * -- LVN_SETDISPINFO
92 * Messages:
93 * -- LVM_ENABLEGROUPVIEW
94 * -- LVM_GETBKIMAGE, LVM_SETBKIMAGE
95 * -- LVM_GETGROUPINFO, LVM_SETGROUPINFO
96 * -- LVM_GETGROUPMETRICS, LVM_SETGROUPMETRICS
97 * -- LVM_GETINSERTMARK, LVM_SETINSERTMARK
98 * -- LVM_GETINSERTMARKCOLOR, LVM_SETINSERTMARKCOLOR
99 * -- LVM_GETINSERTMARKRECT
100 * -- LVM_GETNUMBEROFWORKAREAS
101 * -- LVM_GETOUTLINECOLOR, LVM_SETOUTLINECOLOR
102 * -- LVM_GETISEARCHSTRINGW, LVM_GETISEARCHSTRINGA
103 * -- LVM_GETTILEINFO, LVM_SETTILEINFO
104 * -- LVM_GETTILEVIEWINFO, LVM_SETTILEVIEWINFO
105 * -- LVM_GETWORKAREAS, LVM_SETWORKAREAS
106 * -- LVM_HASGROUP, LVM_INSERTGROUP, LVM_REMOVEGROUP, LVM_REMOVEALLGROUPS
107 * -- LVM_INSERTGROUPSORTED
108 * -- LVM_INSERTMARKHITTEST
109 * -- LVM_ISGROUPVIEWENABLED
110 * -- LVM_MOVEGROUP
111 * -- LVM_MOVEITEMTOGROUP
112 * -- LVM_SETINFOTIP
113 * -- LVM_SETTILEWIDTH
114 * -- LVM_SORTGROUPS
116 * Macros:
117 * -- ListView_GetHoverTime, ListView_SetHoverTime
118 * -- ListView_GetISearchString
119 * -- ListView_GetNumberOfWorkAreas
120 * -- ListView_GetWorkAreas, ListView_SetWorkAreas
122 * Functions:
123 * -- LVGroupComparE
126 #include <assert.h>
127 #include <ctype.h>
128 #include <string.h>
129 #include <stdlib.h>
130 #include <stdarg.h>
131 #include <stdio.h>
133 #include "windef.h"
134 #include "winbase.h"
135 #include "winnt.h"
136 #include "wingdi.h"
137 #include "winuser.h"
138 #include "winnls.h"
139 #include "commctrl.h"
140 #include "comctl32.h"
141 #include "uxtheme.h"
142 #include "shlwapi.h"
144 #include "wine/debug.h"
146 WINE_DEFAULT_DEBUG_CHANNEL(listview);
148 typedef struct tagCOLUMN_INFO
150 RECT rcHeader; /* tracks the header's rectangle */
151 INT fmt; /* same as LVCOLUMN.fmt */
152 INT cxMin;
153 } COLUMN_INFO;
155 typedef struct tagITEMHDR
157 LPWSTR pszText;
158 INT iImage;
159 } ITEMHDR, *LPITEMHDR;
161 typedef struct tagSUBITEM_INFO
163 ITEMHDR hdr;
164 INT iSubItem;
165 } SUBITEM_INFO;
167 typedef struct tagITEM_ID ITEM_ID;
169 typedef struct tagITEM_INFO
171 ITEMHDR hdr;
172 UINT state;
173 LPARAM lParam;
174 INT iIndent;
175 ITEM_ID *id;
176 } ITEM_INFO;
178 struct tagITEM_ID
180 UINT id; /* item id */
181 HDPA item; /* link to item data */
184 typedef struct tagRANGE
186 INT lower;
187 INT upper;
188 } RANGE;
190 typedef struct tagRANGES
192 HDPA hdpa;
193 } *RANGES;
195 typedef struct tagITERATOR
197 INT nItem;
198 INT nSpecial;
199 RANGE range;
200 RANGES ranges;
201 INT index;
202 } ITERATOR;
204 typedef struct tagDELAYED_ITEM_EDIT
206 BOOL fEnabled;
207 INT iItem;
208 } DELAYED_ITEM_EDIT;
210 enum notification_mask
212 NOTIFY_MASK_ITEM_CHANGE = 0x1,
213 NOTIFY_MASK_END_LABEL_EDIT = 0x2,
214 NOTIFY_MASK_UNMASK_ALL = 0xffffffff
217 typedef struct tagLISTVIEW_INFO
219 /* control window */
220 HWND hwndSelf;
221 RECT rcList; /* This rectangle is really the window
222 * client rectangle possibly reduced by the
223 * horizontal scroll bar and/or header - see
224 * LISTVIEW_UpdateSize. This rectangle offset
225 * by the LISTVIEW_GetOrigin value is in
226 * client coordinates */
228 /* notification window */
229 SHORT notifyFormat;
230 HWND hwndNotify;
231 DWORD notify_mask;
232 UINT uCallbackMask;
234 /* tooltips */
235 HWND hwndToolTip;
237 /* items */
238 INT nItemCount; /* the number of items in the list */
239 HDPA hdpaItems; /* array ITEM_INFO pointers */
240 HDPA hdpaItemIds; /* array of ITEM_ID pointers */
241 HDPA hdpaPosX; /* maintains the (X, Y) coordinates of the */
242 HDPA hdpaPosY; /* items in LVS_ICON, and LVS_SMALLICON modes */
243 RANGES selectionRanges;
244 INT nSelectionMark; /* item to start next multiselection from */
245 INT nHotItem;
247 /* columns */
248 HDPA hdpaColumns; /* array of COLUMN_INFO pointers */
249 BOOL colRectsDirty; /* trigger column rectangles requery from header */
250 INT selected_column; /* index for LVM_SETSELECTEDCOLUMN/LVM_GETSELECTEDCOLUMN */
252 /* item metrics */
253 BOOL bNoItemMetrics; /* flags if item metrics are not yet computed */
254 INT nItemHeight;
255 INT nItemWidth;
257 /* style */
258 DWORD dwStyle; /* the cached window GWL_STYLE */
259 DWORD dwLvExStyle; /* extended listview style */
260 DWORD uView; /* current view available through LVM_[G,S]ETVIEW */
262 /* edit item */
263 HWND hwndEdit;
264 WNDPROC EditWndProc;
265 INT nEditLabelItem;
266 DELAYED_ITEM_EDIT itemEdit; /* Pointer to this structure will be the timer ID */
268 /* icons */
269 HIMAGELIST himlNormal;
270 HIMAGELIST himlSmall;
271 HIMAGELIST himlState;
272 SIZE iconSize;
273 BOOL autoSpacing;
274 SIZE iconSpacing;
275 SIZE iconStateSize;
276 POINT currIconPos; /* this is the position next icon will be placed */
278 /* header */
279 HWND hwndHeader;
280 INT xTrackLine; /* The x coefficient of the track line or -1 if none */
282 /* marquee selection */
283 BOOL bMarqueeSelect; /* marquee selection/highlight underway */
284 BOOL bScrolling;
285 RECT marqueeRect; /* absolute coordinates of marquee selection */
286 RECT marqueeDrawRect; /* relative coordinates for drawing marquee */
287 POINT marqueeOrigin; /* absolute coordinates of marquee click origin */
289 /* focus drawing */
290 BOOL bFocus; /* control has focus */
291 INT nFocusedItem;
292 RECT rcFocus; /* focus bounds */
294 /* colors */
295 HBRUSH hBkBrush;
296 COLORREF clrBk;
297 COLORREF clrText;
298 COLORREF clrTextBk;
300 /* font */
301 HFONT hDefaultFont;
302 HFONT hFont;
303 INT ntmHeight; /* Some cached metrics of the font used */
304 INT ntmMaxCharWidth; /* by the listview to draw items */
305 INT nEllipsisWidth;
307 /* mouse operation */
308 BOOL bLButtonDown;
309 BOOL bDragging;
310 POINT ptClickPos; /* point where the user clicked */
311 INT nLButtonDownItem; /* tracks item to reset multiselection on WM_LBUTTONUP */
312 DWORD dwHoverTime;
313 HCURSOR hHotCursor;
314 INT cWheelRemainder;
316 /* keyboard operation */
317 DWORD lastKeyPressTimestamp;
318 WPARAM charCode;
319 INT nSearchParamLength;
320 WCHAR szSearchParam[ MAX_PATH ];
322 /* painting */
323 BOOL bIsDrawing; /* Drawing in progress */
324 INT nMeasureItemHeight; /* WM_MEASUREITEM result */
325 BOOL redraw; /* WM_SETREDRAW switch */
327 /* misc */
328 DWORD iVersion; /* CCM_[G,S]ETVERSION */
329 } LISTVIEW_INFO;
332 * constants
334 /* How many we debug buffer to allocate */
335 #define DEBUG_BUFFERS 20
336 /* The size of a single debug buffer */
337 #define DEBUG_BUFFER_SIZE 256
339 /* Internal interface to LISTVIEW_HScroll and LISTVIEW_VScroll */
340 #define SB_INTERNAL -1
342 /* maximum size of a label */
343 #define DISP_TEXT_SIZE 260
345 /* padding for items in list and small icon display modes */
346 #define WIDTH_PADDING 12
348 /* padding for items in list, report and small icon display modes */
349 #define HEIGHT_PADDING 1
351 /* offset of items in report display mode */
352 #define REPORT_MARGINX 2
354 /* padding for icon in large icon display mode
355 * ICON_TOP_PADDING_NOTHITABLE - space between top of box and area
356 * that HITTEST will see.
357 * ICON_TOP_PADDING_HITABLE - spacing between above and icon.
358 * ICON_TOP_PADDING - sum of the two above.
359 * ICON_BOTTOM_PADDING - between bottom of icon and top of text
360 * LABEL_HOR_PADDING - between text and sides of box
361 * LABEL_VERT_PADDING - between bottom of text and end of box
363 * ICON_LR_PADDING - additional width above icon size.
364 * ICON_LR_HALF - half of the above value
366 #define ICON_TOP_PADDING_NOTHITABLE 2
367 #define ICON_TOP_PADDING_HITABLE 2
368 #define ICON_TOP_PADDING (ICON_TOP_PADDING_NOTHITABLE + ICON_TOP_PADDING_HITABLE)
369 #define ICON_BOTTOM_PADDING 4
370 #define LABEL_HOR_PADDING 5
371 #define LABEL_VERT_PADDING 7
372 #define ICON_LR_PADDING 16
373 #define ICON_LR_HALF (ICON_LR_PADDING/2)
375 /* default label width for items in list and small icon display modes */
376 #define DEFAULT_LABEL_WIDTH 40
377 /* maximum select rectangle width for empty text item in LV_VIEW_DETAILS */
378 #define MAX_EMPTYTEXT_SELECT_WIDTH 80
380 /* default column width for items in list display mode */
381 #define DEFAULT_COLUMN_WIDTH 128
383 /* Size of "line" scroll for V & H scrolls */
384 #define LISTVIEW_SCROLL_ICON_LINE_SIZE 37
386 /* Padding between image and label */
387 #define IMAGE_PADDING 2
389 /* Padding behind the label */
390 #define TRAILING_LABEL_PADDING 12
391 #define TRAILING_HEADER_PADDING 11
393 /* Border for the icon caption */
394 #define CAPTION_BORDER 2
396 /* Standard DrawText flags */
397 #define LV_ML_DT_FLAGS (DT_TOP | DT_NOPREFIX | DT_EDITCONTROL | DT_CENTER | DT_WORDBREAK | DT_WORD_ELLIPSIS | DT_END_ELLIPSIS)
398 #define LV_FL_DT_FLAGS (DT_TOP | DT_NOPREFIX | DT_EDITCONTROL | DT_CENTER | DT_WORDBREAK | DT_NOCLIP)
399 #define LV_SL_DT_FLAGS (DT_VCENTER | DT_NOPREFIX | DT_EDITCONTROL | DT_SINGLELINE | DT_WORD_ELLIPSIS | DT_END_ELLIPSIS)
401 /* Image index from state */
402 #define STATEIMAGEINDEX(x) (((x) & LVIS_STATEIMAGEMASK) >> 12)
404 /* The time in milliseconds to reset the search in the list */
405 #define KEY_DELAY 450
407 /* Dump the LISTVIEW_INFO structure to the debug channel */
408 #define LISTVIEW_DUMP(iP) do { \
409 TRACE("hwndSelf=%p, clrBk=0x%06x, clrText=0x%06x, clrTextBk=0x%06x, ItemHeight=%d, ItemWidth=%d, Style=0x%08x\n", \
410 iP->hwndSelf, iP->clrBk, iP->clrText, iP->clrTextBk, \
411 iP->nItemHeight, iP->nItemWidth, iP->dwStyle); \
412 TRACE("hwndSelf=%p, himlNor=%p, himlSml=%p, himlState=%p, Focused=%d, Hot=%d, exStyle=0x%08x, Focus=%d\n", \
413 iP->hwndSelf, iP->himlNormal, iP->himlSmall, iP->himlState, \
414 iP->nFocusedItem, iP->nHotItem, iP->dwLvExStyle, iP->bFocus ); \
415 TRACE("hwndSelf=%p, ntmH=%d, icSz.cx=%d, icSz.cy=%d, icSp.cx=%d, icSp.cy=%d, notifyFmt=%d\n", \
416 iP->hwndSelf, iP->ntmHeight, iP->iconSize.cx, iP->iconSize.cy, \
417 iP->iconSpacing.cx, iP->iconSpacing.cy, iP->notifyFormat); \
418 TRACE("hwndSelf=%p, rcList=%s\n", iP->hwndSelf, wine_dbgstr_rect(&iP->rcList)); \
419 } while(0)
421 static const WCHAR themeClass[] = L"ListView";
424 * forward declarations
426 static BOOL LISTVIEW_GetItemT(const LISTVIEW_INFO *, LPLVITEMW, BOOL);
427 static void LISTVIEW_GetItemBox(const LISTVIEW_INFO *, INT, LPRECT);
428 static void LISTVIEW_GetItemOrigin(const LISTVIEW_INFO *, INT, LPPOINT);
429 static BOOL LISTVIEW_GetItemPosition(const LISTVIEW_INFO *, INT, LPPOINT);
430 static BOOL LISTVIEW_GetItemRect(const LISTVIEW_INFO *, INT, LPRECT);
431 static void LISTVIEW_GetOrigin(const LISTVIEW_INFO *, LPPOINT);
432 static BOOL LISTVIEW_GetViewRect(const LISTVIEW_INFO *, LPRECT);
433 static void LISTVIEW_UpdateSize(LISTVIEW_INFO *);
434 static LRESULT LISTVIEW_Command(LISTVIEW_INFO *, WPARAM, LPARAM);
435 static INT LISTVIEW_GetStringWidthT(const LISTVIEW_INFO *, LPCWSTR, BOOL);
436 static BOOL LISTVIEW_KeySelection(LISTVIEW_INFO *, INT, BOOL);
437 static UINT LISTVIEW_GetItemState(const LISTVIEW_INFO *, INT, UINT);
438 static BOOL LISTVIEW_SetItemState(LISTVIEW_INFO *, INT, const LVITEMW *);
439 static LRESULT LISTVIEW_VScroll(LISTVIEW_INFO *, INT, INT);
440 static LRESULT LISTVIEW_HScroll(LISTVIEW_INFO *, INT, INT);
441 static BOOL LISTVIEW_EnsureVisible(LISTVIEW_INFO *, INT, BOOL);
442 static HIMAGELIST LISTVIEW_SetImageList(LISTVIEW_INFO *, INT, HIMAGELIST);
443 static INT LISTVIEW_HitTest(const LISTVIEW_INFO *, LPLVHITTESTINFO, BOOL, BOOL);
444 static BOOL LISTVIEW_EndEditLabelT(LISTVIEW_INFO *, BOOL, BOOL);
445 static BOOL LISTVIEW_Scroll(LISTVIEW_INFO *, INT, INT);
447 /******** Text handling functions *************************************/
449 /* A text pointer is either NULL, LPSTR_TEXTCALLBACK, or points to a
450 * text string. The string may be ANSI or Unicode, in which case
451 * the boolean isW tells us the type of the string.
453 * The name of the function tell what type of strings it expects:
454 * W: Unicode, T: ANSI/Unicode - function of isW
457 static inline BOOL is_text(LPCWSTR text)
459 return text != NULL && text != LPSTR_TEXTCALLBACKW;
462 static inline int textlenT(LPCWSTR text, BOOL isW)
464 return !is_text(text) ? 0 :
465 isW ? lstrlenW(text) : lstrlenA((LPCSTR)text);
468 static inline void textcpynT(LPWSTR dest, BOOL isDestW, LPCWSTR src, BOOL isSrcW, INT max)
470 if (isDestW)
471 if (isSrcW) lstrcpynW(dest, src, max);
472 else MultiByteToWideChar(CP_ACP, 0, (LPCSTR)src, -1, dest, max);
473 else
474 if (isSrcW) WideCharToMultiByte(CP_ACP, 0, src, -1, (LPSTR)dest, max, NULL, NULL);
475 else lstrcpynA((LPSTR)dest, (LPCSTR)src, max);
478 static inline LPWSTR textdupTtoW(LPCWSTR text, BOOL isW)
480 LPWSTR wstr = (LPWSTR)text;
482 if (!isW && is_text(text))
484 INT len = MultiByteToWideChar(CP_ACP, 0, (LPCSTR)text, -1, NULL, 0);
485 wstr = Alloc(len * sizeof(WCHAR));
486 if (wstr) MultiByteToWideChar(CP_ACP, 0, (LPCSTR)text, -1, wstr, len);
488 TRACE(" wstr=%s\n", text == LPSTR_TEXTCALLBACKW ? "(callback)" : debugstr_w(wstr));
489 return wstr;
492 static inline void textfreeT(LPWSTR wstr, BOOL isW)
494 if (!isW && is_text(wstr)) Free (wstr);
498 * dest is a pointer to a Unicode string
499 * src is a pointer to a string (Unicode if isW, ANSI if !isW)
501 static BOOL textsetptrT(LPWSTR *dest, LPCWSTR src, BOOL isW)
503 BOOL bResult = TRUE;
505 if (src == LPSTR_TEXTCALLBACKW)
507 if (is_text(*dest)) Free(*dest);
508 *dest = LPSTR_TEXTCALLBACKW;
510 else
512 LPWSTR pszText = textdupTtoW(src, isW);
513 if (*dest == LPSTR_TEXTCALLBACKW) *dest = NULL;
514 bResult = Str_SetPtrW(dest, pszText);
515 textfreeT(pszText, isW);
517 return bResult;
521 * compares a Unicode to a Unicode/ANSI text string
523 static inline int textcmpWT(LPCWSTR aw, LPCWSTR bt, BOOL isW)
525 if (!aw) return bt ? -1 : 0;
526 if (!bt) return 1;
527 if (aw == LPSTR_TEXTCALLBACKW)
528 return bt == LPSTR_TEXTCALLBACKW ? 1 : -1;
529 if (bt != LPSTR_TEXTCALLBACKW)
531 LPWSTR bw = textdupTtoW(bt, isW);
532 int r = bw ? lstrcmpW(aw, bw) : 1;
533 textfreeT(bw, isW);
534 return r;
537 return 1;
540 /******** Debugging functions *****************************************/
542 static inline LPCSTR debugtext_t(LPCWSTR text, BOOL isW)
544 if (text == LPSTR_TEXTCALLBACKW) return "(callback)";
545 return isW ? debugstr_w(text) : debugstr_a((LPCSTR)text);
548 static inline LPCSTR debugtext_tn(LPCWSTR text, BOOL isW, INT n)
550 if (text == LPSTR_TEXTCALLBACKW) return "(callback)";
551 n = min(textlenT(text, isW), n);
552 return isW ? debugstr_wn(text, n) : debugstr_an((LPCSTR)text, n);
555 static char* debug_getbuf(void)
557 static int index = 0;
558 static char buffers[DEBUG_BUFFERS][DEBUG_BUFFER_SIZE];
559 return buffers[index++ % DEBUG_BUFFERS];
562 static inline const char* debugrange(const RANGE *lprng)
564 if (!lprng) return "(null)";
565 return wine_dbg_sprintf("[%d, %d]", lprng->lower, lprng->upper);
568 static const char* debugscrollinfo(const SCROLLINFO *pScrollInfo)
570 char* buf = debug_getbuf(), *text = buf;
571 int len, size = DEBUG_BUFFER_SIZE;
573 if (pScrollInfo == NULL) return "(null)";
574 len = snprintf(buf, size, "{cbSize=%u, ", pScrollInfo->cbSize);
575 if (len == -1) goto end;
576 buf += len; size -= len;
577 if (pScrollInfo->fMask & SIF_RANGE)
578 len = snprintf(buf, size, "nMin=%d, nMax=%d, ", pScrollInfo->nMin, pScrollInfo->nMax);
579 else len = 0;
580 if (len == -1) goto end;
581 buf += len; size -= len;
582 if (pScrollInfo->fMask & SIF_PAGE)
583 len = snprintf(buf, size, "nPage=%u, ", pScrollInfo->nPage);
584 else len = 0;
585 if (len == -1) goto end;
586 buf += len; size -= len;
587 if (pScrollInfo->fMask & SIF_POS)
588 len = snprintf(buf, size, "nPos=%d, ", pScrollInfo->nPos);
589 else len = 0;
590 if (len == -1) goto end;
591 buf += len; size -= len;
592 if (pScrollInfo->fMask & SIF_TRACKPOS)
593 len = snprintf(buf, size, "nTrackPos=%d, ", pScrollInfo->nTrackPos);
594 else len = 0;
595 if (len == -1) goto end;
596 buf += len;
597 goto undo;
598 end:
599 buf = text + strlen(text);
600 undo:
601 if (buf - text > 2) { buf[-2] = '}'; buf[-1] = 0; }
602 return text;
605 static const char* debugnmlistview(const NMLISTVIEW *plvnm)
607 if (!plvnm) return "(null)";
608 return wine_dbg_sprintf("iItem=%d, iSubItem=%d, uNewState=0x%x,"
609 " uOldState=0x%x, uChanged=0x%x, ptAction=%s, lParam=%ld",
610 plvnm->iItem, plvnm->iSubItem, plvnm->uNewState, plvnm->uOldState,
611 plvnm->uChanged, wine_dbgstr_point(&plvnm->ptAction), plvnm->lParam);
614 static const char* debuglvitem_t(const LVITEMW *lpLVItem, BOOL isW)
616 char* buf = debug_getbuf(), *text = buf;
617 int len, size = DEBUG_BUFFER_SIZE;
619 if (lpLVItem == NULL) return "(null)";
620 len = snprintf(buf, size, "{iItem=%d, iSubItem=%d, ", lpLVItem->iItem, lpLVItem->iSubItem);
621 if (len == -1) goto end;
622 buf += len; size -= len;
623 if (lpLVItem->mask & LVIF_STATE)
624 len = snprintf(buf, size, "state=%x, stateMask=%x, ", lpLVItem->state, lpLVItem->stateMask);
625 else len = 0;
626 if (len == -1) goto end;
627 buf += len; size -= len;
628 if (lpLVItem->mask & LVIF_TEXT)
629 len = snprintf(buf, size, "pszText=%s, cchTextMax=%d, ", debugtext_tn(lpLVItem->pszText, isW, 80), lpLVItem->cchTextMax);
630 else len = 0;
631 if (len == -1) goto end;
632 buf += len; size -= len;
633 if (lpLVItem->mask & LVIF_IMAGE)
634 len = snprintf(buf, size, "iImage=%d, ", lpLVItem->iImage);
635 else len = 0;
636 if (len == -1) goto end;
637 buf += len; size -= len;
638 if (lpLVItem->mask & LVIF_PARAM)
639 len = snprintf(buf, size, "lParam=%lx, ", lpLVItem->lParam);
640 else len = 0;
641 if (len == -1) goto end;
642 buf += len; size -= len;
643 if (lpLVItem->mask & LVIF_INDENT)
644 len = snprintf(buf, size, "iIndent=%d, ", lpLVItem->iIndent);
645 else len = 0;
646 if (len == -1) goto end;
647 buf += len;
648 goto undo;
649 end:
650 buf = text + strlen(text);
651 undo:
652 if (buf - text > 2) { buf[-2] = '}'; buf[-1] = 0; }
653 return text;
656 static const char* debuglvcolumn_t(const LVCOLUMNW *lpColumn, BOOL isW)
658 char* buf = debug_getbuf(), *text = buf;
659 int len, size = DEBUG_BUFFER_SIZE;
661 if (lpColumn == NULL) return "(null)";
662 len = snprintf(buf, size, "{");
663 if (len == -1) goto end;
664 buf += len; size -= len;
665 if (lpColumn->mask & LVCF_SUBITEM)
666 len = snprintf(buf, size, "iSubItem=%d, ", lpColumn->iSubItem);
667 else len = 0;
668 if (len == -1) goto end;
669 buf += len; size -= len;
670 if (lpColumn->mask & LVCF_FMT)
671 len = snprintf(buf, size, "fmt=%x, ", lpColumn->fmt);
672 else len = 0;
673 if (len == -1) goto end;
674 buf += len; size -= len;
675 if (lpColumn->mask & LVCF_WIDTH)
676 len = snprintf(buf, size, "cx=%d, ", lpColumn->cx);
677 else len = 0;
678 if (len == -1) goto end;
679 buf += len; size -= len;
680 if (lpColumn->mask & LVCF_TEXT)
681 len = snprintf(buf, size, "pszText=%s, cchTextMax=%d, ", debugtext_tn(lpColumn->pszText, isW, 80), lpColumn->cchTextMax);
682 else len = 0;
683 if (len == -1) goto end;
684 buf += len; size -= len;
685 if (lpColumn->mask & LVCF_IMAGE)
686 len = snprintf(buf, size, "iImage=%d, ", lpColumn->iImage);
687 else len = 0;
688 if (len == -1) goto end;
689 buf += len; size -= len;
690 if (lpColumn->mask & LVCF_ORDER)
691 len = snprintf(buf, size, "iOrder=%d, ", lpColumn->iOrder);
692 else len = 0;
693 if (len == -1) goto end;
694 buf += len;
695 goto undo;
696 end:
697 buf = text + strlen(text);
698 undo:
699 if (buf - text > 2) { buf[-2] = '}'; buf[-1] = 0; }
700 return text;
703 static const char* debuglvhittestinfo(const LVHITTESTINFO *lpht)
705 if (!lpht) return "(null)";
707 return wine_dbg_sprintf("{pt=%s, flags=0x%x, iItem=%d, iSubItem=%d}",
708 wine_dbgstr_point(&lpht->pt), lpht->flags, lpht->iItem, lpht->iSubItem);
711 /* Return the corresponding text for a given scroll value */
712 static inline LPCSTR debugscrollcode(int nScrollCode)
714 switch(nScrollCode)
716 case SB_LINELEFT: return "SB_LINELEFT";
717 case SB_LINERIGHT: return "SB_LINERIGHT";
718 case SB_PAGELEFT: return "SB_PAGELEFT";
719 case SB_PAGERIGHT: return "SB_PAGERIGHT";
720 case SB_THUMBPOSITION: return "SB_THUMBPOSITION";
721 case SB_THUMBTRACK: return "SB_THUMBTRACK";
722 case SB_ENDSCROLL: return "SB_ENDSCROLL";
723 case SB_INTERNAL: return "SB_INTERNAL";
724 default: return "unknown";
729 /******** Notification functions ************************************/
731 static int get_ansi_notification(UINT unicodeNotificationCode)
733 switch (unicodeNotificationCode)
735 case LVN_BEGINLABELEDITA:
736 case LVN_BEGINLABELEDITW: return LVN_BEGINLABELEDITA;
737 case LVN_ENDLABELEDITA:
738 case LVN_ENDLABELEDITW: return LVN_ENDLABELEDITA;
739 case LVN_GETDISPINFOA:
740 case LVN_GETDISPINFOW: return LVN_GETDISPINFOA;
741 case LVN_SETDISPINFOA:
742 case LVN_SETDISPINFOW: return LVN_SETDISPINFOA;
743 case LVN_ODFINDITEMA:
744 case LVN_ODFINDITEMW: return LVN_ODFINDITEMA;
745 case LVN_GETINFOTIPA:
746 case LVN_GETINFOTIPW: return LVN_GETINFOTIPA;
747 /* header forwards */
748 case HDN_TRACKA:
749 case HDN_TRACKW: return HDN_TRACKA;
750 case HDN_ENDTRACKA:
751 case HDN_ENDTRACKW: return HDN_ENDTRACKA;
752 case HDN_BEGINDRAG: return HDN_BEGINDRAG;
753 case HDN_ENDDRAG: return HDN_ENDDRAG;
754 case HDN_ITEMCHANGINGA:
755 case HDN_ITEMCHANGINGW: return HDN_ITEMCHANGINGA;
756 case HDN_ITEMCHANGEDA:
757 case HDN_ITEMCHANGEDW: return HDN_ITEMCHANGEDA;
758 case HDN_ITEMCLICKA:
759 case HDN_ITEMCLICKW: return HDN_ITEMCLICKA;
760 case HDN_DIVIDERDBLCLICKA:
761 case HDN_DIVIDERDBLCLICKW: return HDN_DIVIDERDBLCLICKA;
762 default: break;
764 FIXME("unknown notification %x\n", unicodeNotificationCode);
765 return unicodeNotificationCode;
768 /* forwards header notifications to listview parent */
769 static LRESULT notify_forward_header(const LISTVIEW_INFO *infoPtr, NMHEADERW *lpnmhW)
771 LPCWSTR text = NULL, filter = NULL;
772 LRESULT ret;
773 NMHEADERA *lpnmh = (NMHEADERA*) lpnmhW;
775 /* on unicode format exit earlier */
776 if (infoPtr->notifyFormat == NFR_UNICODE)
777 return SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, lpnmh->hdr.idFrom,
778 (LPARAM)lpnmh);
780 /* header always supplies unicode notifications,
781 all we have to do is to convert strings to ANSI */
782 if (lpnmh->pitem)
784 /* convert item text */
785 if (lpnmh->pitem->mask & HDI_TEXT)
787 text = (LPCWSTR)lpnmh->pitem->pszText;
788 lpnmh->pitem->pszText = NULL;
789 Str_SetPtrWtoA(&lpnmh->pitem->pszText, text);
791 /* convert filter text */
792 if ((lpnmh->pitem->mask & HDI_FILTER) && (lpnmh->pitem->type == HDFT_ISSTRING) &&
793 lpnmh->pitem->pvFilter)
795 filter = (LPCWSTR)((HD_TEXTFILTERA*)lpnmh->pitem->pvFilter)->pszText;
796 ((HD_TEXTFILTERA*)lpnmh->pitem->pvFilter)->pszText = NULL;
797 Str_SetPtrWtoA(&((HD_TEXTFILTERA*)lpnmh->pitem->pvFilter)->pszText, filter);
800 lpnmh->hdr.code = get_ansi_notification(lpnmh->hdr.code);
802 ret = SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, lpnmh->hdr.idFrom,
803 (LPARAM)lpnmh);
805 /* cleanup */
806 if(text)
808 Free(lpnmh->pitem->pszText);
809 lpnmh->pitem->pszText = (LPSTR)text;
811 if(filter)
813 Free(((HD_TEXTFILTERA*)lpnmh->pitem->pvFilter)->pszText);
814 ((HD_TEXTFILTERA*)lpnmh->pitem->pvFilter)->pszText = (LPSTR)filter;
817 return ret;
820 static LRESULT notify_hdr(const LISTVIEW_INFO *infoPtr, INT code, LPNMHDR pnmh)
822 LRESULT result;
824 TRACE("(code=%d)\n", code);
826 pnmh->hwndFrom = infoPtr->hwndSelf;
827 pnmh->idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
828 pnmh->code = code;
829 result = SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, pnmh->idFrom, (LPARAM)pnmh);
831 TRACE(" <= %ld\n", result);
833 return result;
836 static inline BOOL notify(const LISTVIEW_INFO *infoPtr, INT code)
838 NMHDR nmh;
839 HWND hwnd = infoPtr->hwndSelf;
840 notify_hdr(infoPtr, code, &nmh);
841 return IsWindow(hwnd);
844 static inline void notify_itemactivate(const LISTVIEW_INFO *infoPtr, const LVHITTESTINFO *htInfo)
846 NMITEMACTIVATE nmia;
847 LVITEMW item;
849 nmia.uNewState = 0;
850 nmia.uOldState = 0;
851 nmia.uChanged = 0;
852 nmia.uKeyFlags = 0;
854 item.mask = LVIF_PARAM|LVIF_STATE;
855 item.iItem = htInfo->iItem;
856 item.iSubItem = 0;
857 item.stateMask = (UINT)-1;
858 if (LISTVIEW_GetItemT(infoPtr, &item, TRUE)) {
859 nmia.lParam = item.lParam;
860 nmia.uOldState = item.state;
861 nmia.uNewState = item.state | LVIS_ACTIVATING;
862 nmia.uChanged = LVIF_STATE;
865 nmia.iItem = htInfo->iItem;
866 nmia.iSubItem = htInfo->iSubItem;
867 nmia.ptAction = htInfo->pt;
869 if (GetKeyState(VK_SHIFT) & 0x8000) nmia.uKeyFlags |= LVKF_SHIFT;
870 if (GetKeyState(VK_CONTROL) & 0x8000) nmia.uKeyFlags |= LVKF_CONTROL;
871 if (GetKeyState(VK_MENU) & 0x8000) nmia.uKeyFlags |= LVKF_ALT;
873 notify_hdr(infoPtr, LVN_ITEMACTIVATE, (LPNMHDR)&nmia);
876 static inline LRESULT notify_listview(const LISTVIEW_INFO *infoPtr, INT code, LPNMLISTVIEW plvnm)
878 TRACE("(code=%d, plvnm=%s)\n", code, debugnmlistview(plvnm));
879 return notify_hdr(infoPtr, code, (LPNMHDR)plvnm);
882 /* Handles NM_DBLCLK, NM_CLICK, NM_RDBLCLK, NM_RCLICK. Only NM_RCLICK return value is used. */
883 static BOOL notify_click(const LISTVIEW_INFO *infoPtr, INT code, const LVHITTESTINFO *lvht)
885 NMITEMACTIVATE nmia;
886 LVITEMW item;
887 HWND hwnd = infoPtr->hwndSelf;
888 LRESULT ret;
890 TRACE("code=%d, lvht=%s\n", code, debuglvhittestinfo(lvht));
891 ZeroMemory(&nmia, sizeof(nmia));
892 nmia.iItem = lvht->iItem;
893 nmia.iSubItem = lvht->iSubItem;
894 nmia.ptAction = lvht->pt;
895 item.mask = LVIF_PARAM;
896 item.iItem = lvht->iItem;
897 item.iSubItem = 0;
898 if (LISTVIEW_GetItemT(infoPtr, &item, TRUE)) nmia.lParam = item.lParam;
899 ret = notify_hdr(infoPtr, code, (NMHDR*)&nmia);
900 return IsWindow(hwnd) && (code == NM_RCLICK ? !ret : TRUE);
903 static BOOL notify_deleteitem(const LISTVIEW_INFO *infoPtr, INT nItem)
905 NMLISTVIEW nmlv;
906 LVITEMW item;
907 HWND hwnd = infoPtr->hwndSelf;
909 ZeroMemory(&nmlv, sizeof (NMLISTVIEW));
910 nmlv.iItem = nItem;
911 item.mask = LVIF_PARAM;
912 item.iItem = nItem;
913 item.iSubItem = 0;
914 if (LISTVIEW_GetItemT(infoPtr, &item, TRUE)) nmlv.lParam = item.lParam;
915 notify_listview(infoPtr, LVN_DELETEITEM, &nmlv);
916 return IsWindow(hwnd);
920 Send notification. depends on dispinfoW having same
921 structure as dispinfoA.
922 infoPtr : listview struct
923 code : *Unicode* notification code
924 pdi : dispinfo structure (can be unicode or ansi)
925 isW : TRUE if dispinfo is Unicode
927 static BOOL notify_dispinfoT(const LISTVIEW_INFO *infoPtr, UINT code, LPNMLVDISPINFOW pdi, BOOL isW)
929 INT length = 0, ret_length;
930 LPWSTR buffer = NULL, ret_text;
931 BOOL return_ansi = FALSE;
932 BOOL return_unicode = FALSE;
933 BOOL ret;
935 if ((pdi->item.mask & LVIF_TEXT) && is_text(pdi->item.pszText))
937 return_unicode = ( isW && infoPtr->notifyFormat == NFR_ANSI);
938 return_ansi = (!isW && infoPtr->notifyFormat == NFR_UNICODE);
941 ret_length = pdi->item.cchTextMax;
942 ret_text = pdi->item.pszText;
944 if (return_unicode || return_ansi)
946 if (code != LVN_GETDISPINFOW)
948 length = return_ansi ?
949 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pdi->item.pszText, -1, NULL, 0):
950 WideCharToMultiByte(CP_ACP, 0, pdi->item.pszText, -1, NULL, 0, NULL, NULL);
952 else
954 length = pdi->item.cchTextMax;
955 *pdi->item.pszText = 0; /* make sure we don't process garbage */
958 buffer = Alloc( (return_ansi ? sizeof(WCHAR) : sizeof(CHAR)) * length);
959 if (!buffer) return FALSE;
961 if (return_ansi)
962 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pdi->item.pszText, -1,
963 buffer, length);
964 else
965 WideCharToMultiByte(CP_ACP, 0, pdi->item.pszText, -1, (LPSTR) buffer,
966 length, NULL, NULL);
968 pdi->item.pszText = buffer;
969 pdi->item.cchTextMax = length;
972 if (infoPtr->notifyFormat == NFR_ANSI)
973 code = get_ansi_notification(code);
975 TRACE(" pdi->item=%s\n", debuglvitem_t(&pdi->item, infoPtr->notifyFormat != NFR_ANSI));
976 ret = notify_hdr(infoPtr, code, &pdi->hdr);
977 TRACE(" resulting code=%d\n", pdi->hdr.code);
979 if (return_ansi || return_unicode)
981 if (return_ansi && (pdi->hdr.code == LVN_GETDISPINFOA))
983 strcpy((char*)ret_text, (char*)pdi->item.pszText);
985 else if (return_unicode && (pdi->hdr.code == LVN_GETDISPINFOW))
987 lstrcpyW(ret_text, pdi->item.pszText);
989 else if (return_ansi) /* note : pointer can be changed by app ! */
991 WideCharToMultiByte(CP_ACP, 0, pdi->item.pszText, -1, (LPSTR) ret_text,
992 ret_length, NULL, NULL);
994 else
995 MultiByteToWideChar(CP_ACP, 0, (LPSTR) pdi->item.pszText, -1,
996 ret_text, ret_length);
998 pdi->item.pszText = ret_text; /* restores our buffer */
999 pdi->item.cchTextMax = ret_length;
1001 Free(buffer);
1002 return ret;
1005 /* if dispinfo holder changed notification code then convert */
1006 if (!isW && (pdi->hdr.code == LVN_GETDISPINFOW) && (pdi->item.mask & LVIF_TEXT))
1008 length = WideCharToMultiByte(CP_ACP, 0, pdi->item.pszText, -1, NULL, 0, NULL, NULL);
1010 buffer = Alloc(length * sizeof(CHAR));
1011 if (!buffer) return FALSE;
1013 WideCharToMultiByte(CP_ACP, 0, pdi->item.pszText, -1, (LPSTR) buffer,
1014 ret_length, NULL, NULL);
1016 strcpy((LPSTR)pdi->item.pszText, (LPSTR)buffer);
1017 Free(buffer);
1020 return ret;
1023 static void customdraw_fill(NMLVCUSTOMDRAW *lpnmlvcd, const LISTVIEW_INFO *infoPtr, HDC hdc,
1024 const RECT *rcBounds, const LVITEMW *lplvItem)
1026 ZeroMemory(lpnmlvcd, sizeof(NMLVCUSTOMDRAW));
1027 lpnmlvcd->nmcd.hdc = hdc;
1028 lpnmlvcd->nmcd.rc = *rcBounds;
1029 lpnmlvcd->clrTextBk = infoPtr->clrTextBk;
1030 lpnmlvcd->clrText = infoPtr->clrText;
1031 if (!lplvItem) return;
1032 lpnmlvcd->nmcd.dwItemSpec = lplvItem->iItem + 1;
1033 lpnmlvcd->iSubItem = lplvItem->iSubItem;
1034 if (lplvItem->state & LVIS_SELECTED) lpnmlvcd->nmcd.uItemState |= CDIS_SELECTED;
1035 if (lplvItem->state & LVIS_FOCUSED) lpnmlvcd->nmcd.uItemState |= CDIS_FOCUS;
1036 if (lplvItem->iItem == infoPtr->nHotItem) lpnmlvcd->nmcd.uItemState |= CDIS_HOT;
1037 lpnmlvcd->nmcd.lItemlParam = lplvItem->lParam;
1040 static inline DWORD notify_customdraw (const LISTVIEW_INFO *infoPtr, DWORD dwDrawStage, NMLVCUSTOMDRAW *lpnmlvcd)
1042 BOOL isForItem = (lpnmlvcd->nmcd.dwItemSpec != 0);
1043 DWORD result;
1045 lpnmlvcd->nmcd.dwDrawStage = dwDrawStage;
1046 if (isForItem) lpnmlvcd->nmcd.dwDrawStage |= CDDS_ITEM;
1047 if (lpnmlvcd->iSubItem) lpnmlvcd->nmcd.dwDrawStage |= CDDS_SUBITEM;
1048 if (isForItem) lpnmlvcd->nmcd.dwItemSpec--;
1049 result = notify_hdr(infoPtr, NM_CUSTOMDRAW, &lpnmlvcd->nmcd.hdr);
1050 if (isForItem) lpnmlvcd->nmcd.dwItemSpec++;
1051 return result;
1054 static void prepaint_setup (const LISTVIEW_INFO *infoPtr, HDC hdc, const NMLVCUSTOMDRAW *cd, BOOL SubItem)
1056 COLORREF backcolor, textcolor;
1058 backcolor = cd->clrTextBk;
1059 textcolor = cd->clrText;
1061 /* apparently, for selected items, we have to override the returned values */
1062 if (!SubItem)
1064 if (cd->nmcd.uItemState & CDIS_SELECTED)
1066 if (infoPtr->bFocus)
1068 backcolor = comctl32_color.clrHighlight;
1069 textcolor = comctl32_color.clrHighlightText;
1071 else if (infoPtr->dwStyle & LVS_SHOWSELALWAYS)
1073 backcolor = comctl32_color.clr3dFace;
1074 textcolor = comctl32_color.clrBtnText;
1079 if (backcolor == CLR_DEFAULT)
1080 backcolor = comctl32_color.clrWindow;
1081 if (textcolor == CLR_DEFAULT)
1082 textcolor = comctl32_color.clrWindowText;
1084 /* Set the text attributes */
1085 if (backcolor != CLR_NONE)
1087 SetBkMode(hdc, OPAQUE);
1088 SetBkColor(hdc, backcolor);
1090 else
1091 SetBkMode(hdc, TRANSPARENT);
1092 SetTextColor(hdc, textcolor);
1095 static inline DWORD notify_postpaint (const LISTVIEW_INFO *infoPtr, NMLVCUSTOMDRAW *lpnmlvcd)
1097 return notify_customdraw(infoPtr, CDDS_POSTPAINT, lpnmlvcd);
1100 /* returns TRUE when repaint needed, FALSE otherwise */
1101 static BOOL notify_measureitem(LISTVIEW_INFO *infoPtr)
1103 MEASUREITEMSTRUCT mis;
1104 mis.CtlType = ODT_LISTVIEW;
1105 mis.CtlID = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
1106 mis.itemID = -1;
1107 mis.itemWidth = 0;
1108 mis.itemData = 0;
1109 mis.itemHeight= infoPtr->nItemHeight;
1110 SendMessageW(infoPtr->hwndNotify, WM_MEASUREITEM, mis.CtlID, (LPARAM)&mis);
1111 if (infoPtr->nItemHeight != max(mis.itemHeight, 1))
1113 infoPtr->nMeasureItemHeight = infoPtr->nItemHeight = max(mis.itemHeight, 1);
1114 return TRUE;
1116 return FALSE;
1119 /******** Item iterator functions **********************************/
1121 static RANGES ranges_create(int count);
1122 static void ranges_destroy(RANGES ranges);
1123 static BOOL ranges_add(RANGES ranges, RANGE range);
1124 static BOOL ranges_del(RANGES ranges, RANGE range);
1125 static void ranges_dump(RANGES ranges);
1127 static inline BOOL ranges_additem(RANGES ranges, INT nItem)
1129 RANGE range = { nItem, nItem + 1 };
1131 return ranges_add(ranges, range);
1134 static inline BOOL ranges_delitem(RANGES ranges, INT nItem)
1136 RANGE range = { nItem, nItem + 1 };
1138 return ranges_del(ranges, range);
1141 /***
1142 * ITERATOR DOCUMENTATION
1144 * The iterator functions allow for easy, and convenient iteration
1145 * over items of interest in the list. Typically, you create an
1146 * iterator, use it, and destroy it, as such:
1147 * ITERATOR i;
1149 * iterator_xxxitems(&i, ...);
1150 * while (iterator_{prev,next}(&i)
1152 * //code which uses i.nItem
1154 * iterator_destroy(&i);
1156 * where xxx is either: framed, or visible.
1157 * Note that it is important that the code destroys the iterator
1158 * after it's done with it, as the creation of the iterator may
1159 * allocate memory, which thus needs to be freed.
1161 * You can iterate both forwards, and backwards through the list,
1162 * by using iterator_next or iterator_prev respectively.
1164 * Lower numbered items are draw on top of higher number items in
1165 * LVS_ICON, and LVS_SMALLICON (which are the only modes where
1166 * items may overlap). So, to test items, you should use
1167 * iterator_next
1168 * which lists the items top to bottom (in Z-order).
1169 * For drawing items, you should use
1170 * iterator_prev
1171 * which lists the items bottom to top (in Z-order).
1172 * If you keep iterating over the items after the end-of-items
1173 * marker (-1) is returned, the iterator will start from the
1174 * beginning. Typically, you don't need to test for -1,
1175 * because iterator_{next,prev} will return TRUE if more items
1176 * are to be iterated over, or FALSE otherwise.
1178 * Note: the iterator is defined to be bidirectional. That is,
1179 * any number of prev followed by any number of next, or
1180 * five versa, should leave the iterator at the same item:
1181 * prev * n, next * n = next * n, prev * n
1183 * The iterator has a notion of an out-of-order, special item,
1184 * which sits at the start of the list. This is used in
1185 * LVS_ICON, and LVS_SMALLICON mode to handle the focused item,
1186 * which needs to be first, as it may overlap other items.
1188 * The code is a bit messy because we have:
1189 * - a special item to deal with
1190 * - simple range, or composite range
1191 * - empty range.
1192 * If you find bugs, or want to add features, please make sure you
1193 * always check/modify *both* iterator_prev, and iterator_next.
1196 /****
1197 * This function iterates through the items in increasing order,
1198 * but prefixed by the special item, then -1. That is:
1199 * special, 1, 2, 3, ..., n, -1.
1200 * Each item is listed only once.
1202 static inline BOOL iterator_next(ITERATOR* i)
1204 if (i->nItem == -1)
1206 i->nItem = i->nSpecial;
1207 if (i->nItem != -1) return TRUE;
1209 if (i->nItem == i->nSpecial)
1211 if (i->ranges) i->index = 0;
1212 goto pickarange;
1215 i->nItem++;
1216 testitem:
1217 if (i->nItem == i->nSpecial) i->nItem++;
1218 if (i->nItem < i->range.upper) return TRUE;
1220 pickarange:
1221 if (i->ranges)
1223 if (i->index < DPA_GetPtrCount(i->ranges->hdpa))
1224 i->range = *(RANGE*)DPA_GetPtr(i->ranges->hdpa, i->index++);
1225 else goto end;
1227 else if (i->nItem >= i->range.upper) goto end;
1229 i->nItem = i->range.lower;
1230 if (i->nItem >= 0) goto testitem;
1231 end:
1232 i->nItem = -1;
1233 return FALSE;
1236 /****
1237 * This function iterates through the items in decreasing order,
1238 * followed by the special item, then -1. That is:
1239 * n, n-1, ..., 3, 2, 1, special, -1.
1240 * Each item is listed only once.
1242 static inline BOOL iterator_prev(ITERATOR* i)
1244 BOOL start = FALSE;
1246 if (i->nItem == -1)
1248 start = TRUE;
1249 if (i->ranges) i->index = DPA_GetPtrCount(i->ranges->hdpa);
1250 goto pickarange;
1252 if (i->nItem == i->nSpecial)
1254 i->nItem = -1;
1255 return FALSE;
1258 testitem:
1259 i->nItem--;
1260 if (i->nItem == i->nSpecial) i->nItem--;
1261 if (i->nItem >= i->range.lower) return TRUE;
1263 pickarange:
1264 if (i->ranges)
1266 if (i->index > 0)
1267 i->range = *(RANGE*)DPA_GetPtr(i->ranges->hdpa, --i->index);
1268 else goto end;
1270 else if (!start && i->nItem < i->range.lower) goto end;
1272 i->nItem = i->range.upper;
1273 if (i->nItem > 0) goto testitem;
1274 end:
1275 return (i->nItem = i->nSpecial) != -1;
1278 static RANGE iterator_range(const ITERATOR *i)
1280 RANGE range;
1282 if (!i->ranges) return i->range;
1284 if (DPA_GetPtrCount(i->ranges->hdpa) > 0)
1286 range.lower = (*(RANGE*)DPA_GetPtr(i->ranges->hdpa, 0)).lower;
1287 range.upper = (*(RANGE*)DPA_GetPtr(i->ranges->hdpa, DPA_GetPtrCount(i->ranges->hdpa) - 1)).upper;
1289 else range.lower = range.upper = 0;
1291 return range;
1294 /***
1295 * Releases resources associated with this iterator.
1297 static inline void iterator_destroy(const ITERATOR *i)
1299 ranges_destroy(i->ranges);
1302 /***
1303 * Create an empty iterator.
1305 static inline void iterator_empty(ITERATOR* i)
1307 ZeroMemory(i, sizeof(*i));
1308 i->nItem = i->nSpecial = i->range.lower = i->range.upper = -1;
1311 /***
1312 * Create an iterator over a range.
1314 static inline void iterator_rangeitems(ITERATOR* i, RANGE range)
1316 iterator_empty(i);
1317 i->range = range;
1320 /***
1321 * Create an iterator over a bunch of ranges.
1322 * Please note that the iterator will take ownership of the ranges,
1323 * and will free them upon destruction.
1325 static inline void iterator_rangesitems(ITERATOR* i, RANGES ranges)
1327 iterator_empty(i);
1328 i->ranges = ranges;
1331 /***
1332 * Creates an iterator over the items which intersect frame.
1333 * Uses absolute coordinates rather than compensating for the current offset.
1335 static BOOL iterator_frameditems_absolute(ITERATOR* i, const LISTVIEW_INFO* infoPtr, const RECT *frame)
1337 RECT rcItem, rcTemp;
1338 RANGES ranges;
1340 TRACE("(frame=%s)\n", wine_dbgstr_rect(frame));
1342 /* in case we fail, we want to return an empty iterator */
1343 iterator_empty(i);
1345 if (infoPtr->nItemCount == 0)
1346 return TRUE;
1348 if (infoPtr->uView == LV_VIEW_ICON || infoPtr->uView == LV_VIEW_SMALLICON)
1350 INT nItem;
1352 if (infoPtr->uView == LV_VIEW_ICON && infoPtr->nFocusedItem != -1)
1354 LISTVIEW_GetItemBox(infoPtr, infoPtr->nFocusedItem, &rcItem);
1355 if (IntersectRect(&rcTemp, &rcItem, frame))
1356 i->nSpecial = infoPtr->nFocusedItem;
1358 if (!(ranges = ranges_create(50))) return FALSE;
1359 iterator_rangesitems(i, ranges);
1360 /* to do better here, we need to have PosX, and PosY sorted */
1361 TRACE("building icon ranges:\n");
1362 for (nItem = 0; nItem < infoPtr->nItemCount; nItem++)
1364 rcItem.left = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosX, nItem);
1365 rcItem.top = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosY, nItem);
1366 rcItem.right = rcItem.left + infoPtr->nItemWidth;
1367 rcItem.bottom = rcItem.top + infoPtr->nItemHeight;
1368 if (IntersectRect(&rcTemp, &rcItem, frame))
1369 ranges_additem(i->ranges, nItem);
1371 return TRUE;
1373 else if (infoPtr->uView == LV_VIEW_DETAILS)
1375 RANGE range;
1377 if (frame->left >= infoPtr->nItemWidth) return TRUE;
1378 if (frame->top >= infoPtr->nItemHeight * infoPtr->nItemCount) return TRUE;
1380 range.lower = max(frame->top / infoPtr->nItemHeight, 0);
1381 range.upper = min((frame->bottom - 1) / infoPtr->nItemHeight, infoPtr->nItemCount - 1) + 1;
1382 if (range.upper <= range.lower) return TRUE;
1383 iterator_rangeitems(i, range);
1384 TRACE(" report=%s\n", debugrange(&i->range));
1386 else
1388 INT nPerCol = max((infoPtr->rcList.bottom - infoPtr->rcList.top) / infoPtr->nItemHeight, 1);
1389 INT nFirstRow = max(frame->top / infoPtr->nItemHeight, 0);
1390 INT nLastRow = min((frame->bottom - 1) / infoPtr->nItemHeight, nPerCol - 1);
1391 INT nFirstCol;
1392 INT nLastCol;
1393 INT lower;
1394 RANGE item_range;
1395 INT nCol;
1397 if (infoPtr->nItemWidth)
1399 nFirstCol = max(frame->left / infoPtr->nItemWidth, 0);
1400 nLastCol = min((frame->right - 1) / infoPtr->nItemWidth, (infoPtr->nItemCount + nPerCol - 1) / nPerCol);
1402 else
1404 nFirstCol = max(frame->left, 0);
1405 nLastCol = min(frame->right - 1, (infoPtr->nItemCount + nPerCol - 1) / nPerCol);
1408 lower = nFirstCol * nPerCol + nFirstRow;
1410 TRACE("nPerCol=%d, nFirstRow=%d, nLastRow=%d, nFirstCol=%d, nLastCol=%d, lower=%d\n",
1411 nPerCol, nFirstRow, nLastRow, nFirstCol, nLastCol, lower);
1413 if (nLastCol < nFirstCol || nLastRow < nFirstRow) return TRUE;
1415 if (!(ranges = ranges_create(nLastCol - nFirstCol + 1))) return FALSE;
1416 iterator_rangesitems(i, ranges);
1417 TRACE("building list ranges:\n");
1418 for (nCol = nFirstCol; nCol <= nLastCol; nCol++)
1420 item_range.lower = nCol * nPerCol + nFirstRow;
1421 if(item_range.lower >= infoPtr->nItemCount) break;
1422 item_range.upper = min(nCol * nPerCol + nLastRow + 1, infoPtr->nItemCount);
1423 TRACE(" list=%s\n", debugrange(&item_range));
1424 ranges_add(i->ranges, item_range);
1428 return TRUE;
1431 /***
1432 * Creates an iterator over the items which intersect lprc.
1434 static BOOL iterator_frameditems(ITERATOR* i, const LISTVIEW_INFO* infoPtr, const RECT *lprc)
1436 RECT frame = *lprc;
1437 POINT Origin;
1439 TRACE("(lprc=%s)\n", wine_dbgstr_rect(lprc));
1441 LISTVIEW_GetOrigin(infoPtr, &Origin);
1442 OffsetRect(&frame, -Origin.x, -Origin.y);
1444 return iterator_frameditems_absolute(i, infoPtr, &frame);
1447 /***
1448 * Creates an iterator over the items which intersect the visible region of hdc.
1450 static BOOL iterator_visibleitems(ITERATOR *i, const LISTVIEW_INFO *infoPtr, HDC hdc)
1452 POINT Origin, Position;
1453 RECT rcItem, rcClip;
1454 INT rgntype;
1456 rgntype = GetClipBox(hdc, &rcClip);
1457 if (rgntype == NULLREGION)
1459 iterator_empty(i);
1460 return TRUE;
1462 if (!iterator_frameditems(i, infoPtr, &rcClip)) return FALSE;
1463 if (rgntype == SIMPLEREGION) return TRUE;
1465 /* first deal with the special item */
1466 if (i->nSpecial != -1)
1468 LISTVIEW_GetItemBox(infoPtr, i->nSpecial, &rcItem);
1469 if (!RectVisible(hdc, &rcItem)) i->nSpecial = -1;
1472 /* if we can't deal with the region, we'll just go with the simple range */
1473 LISTVIEW_GetOrigin(infoPtr, &Origin);
1474 TRACE("building visible range:\n");
1475 if (!i->ranges && i->range.lower < i->range.upper)
1477 if (!(i->ranges = ranges_create(50))) return TRUE;
1478 if (!ranges_add(i->ranges, i->range))
1480 ranges_destroy(i->ranges);
1481 i->ranges = 0;
1482 return TRUE;
1486 /* now delete the invisible items from the list */
1487 while(iterator_next(i))
1489 LISTVIEW_GetItemOrigin(infoPtr, i->nItem, &Position);
1490 rcItem.left = (infoPtr->uView == LV_VIEW_DETAILS) ? Origin.x : Position.x + Origin.x;
1491 rcItem.top = Position.y + Origin.y;
1492 rcItem.right = rcItem.left + infoPtr->nItemWidth;
1493 rcItem.bottom = rcItem.top + infoPtr->nItemHeight;
1494 if (!RectVisible(hdc, &rcItem))
1495 ranges_delitem(i->ranges, i->nItem);
1497 /* the iterator should restart on the next iterator_next */
1498 TRACE("done\n");
1500 return TRUE;
1503 /* Remove common elements from two iterators */
1504 /* Passed iterators have to point on the first elements */
1505 static BOOL iterator_remove_common_items(ITERATOR *iter1, ITERATOR *iter2)
1507 if(!iter1->ranges || !iter2->ranges) {
1508 int lower, upper;
1510 if(iter1->ranges || iter2->ranges ||
1511 (iter1->range.lower<iter2->range.lower && iter1->range.upper>iter2->range.upper) ||
1512 (iter1->range.lower>iter2->range.lower && iter1->range.upper<iter2->range.upper)) {
1513 ERR("result is not a one range iterator\n");
1514 return FALSE;
1517 if(iter1->range.lower==-1 || iter2->range.lower==-1)
1518 return TRUE;
1520 lower = iter1->range.lower;
1521 upper = iter1->range.upper;
1523 if(lower < iter2->range.lower)
1524 iter1->range.upper = iter2->range.lower;
1525 else if(upper > iter2->range.upper)
1526 iter1->range.lower = iter2->range.upper;
1527 else
1528 iter1->range.lower = iter1->range.upper = -1;
1530 if(iter2->range.lower < lower)
1531 iter2->range.upper = lower;
1532 else if(iter2->range.upper > upper)
1533 iter2->range.lower = upper;
1534 else
1535 iter2->range.lower = iter2->range.upper = -1;
1537 return TRUE;
1540 iterator_next(iter1);
1541 iterator_next(iter2);
1543 while(1) {
1544 if(iter1->nItem==-1 || iter2->nItem==-1)
1545 break;
1547 if(iter1->nItem == iter2->nItem) {
1548 int delete = iter1->nItem;
1550 iterator_prev(iter1);
1551 iterator_prev(iter2);
1552 ranges_delitem(iter1->ranges, delete);
1553 ranges_delitem(iter2->ranges, delete);
1554 iterator_next(iter1);
1555 iterator_next(iter2);
1556 } else if(iter1->nItem > iter2->nItem)
1557 iterator_next(iter2);
1558 else
1559 iterator_next(iter1);
1562 iter1->nItem = iter1->range.lower = iter1->range.upper = -1;
1563 iter2->nItem = iter2->range.lower = iter2->range.upper = -1;
1564 return TRUE;
1567 /******** Misc helper functions ************************************/
1569 static inline LRESULT CallWindowProcT(WNDPROC proc, HWND hwnd, UINT uMsg,
1570 WPARAM wParam, LPARAM lParam, BOOL isW)
1572 if (isW) return CallWindowProcW(proc, hwnd, uMsg, wParam, lParam);
1573 else return CallWindowProcA(proc, hwnd, uMsg, wParam, lParam);
1576 static inline BOOL is_autoarrange(const LISTVIEW_INFO *infoPtr)
1578 return (infoPtr->dwStyle & LVS_AUTOARRANGE) &&
1579 (infoPtr->uView == LV_VIEW_ICON || infoPtr->uView == LV_VIEW_SMALLICON);
1582 static void toggle_checkbox_state(LISTVIEW_INFO *infoPtr, INT nItem)
1584 DWORD state = STATEIMAGEINDEX(LISTVIEW_GetItemState(infoPtr, nItem, LVIS_STATEIMAGEMASK));
1585 if(state == 1 || state == 2)
1587 LVITEMW lvitem;
1588 state ^= 3;
1589 lvitem.state = INDEXTOSTATEIMAGEMASK(state);
1590 lvitem.stateMask = LVIS_STATEIMAGEMASK;
1591 LISTVIEW_SetItemState(infoPtr, nItem, &lvitem);
1595 /* this should be called after window style got updated,
1596 it used to reset view state to match current window style */
1597 static inline void map_style_view(LISTVIEW_INFO *infoPtr)
1599 switch (infoPtr->dwStyle & LVS_TYPEMASK)
1601 case LVS_ICON:
1602 infoPtr->uView = LV_VIEW_ICON;
1603 break;
1604 case LVS_REPORT:
1605 infoPtr->uView = LV_VIEW_DETAILS;
1606 break;
1607 case LVS_SMALLICON:
1608 infoPtr->uView = LV_VIEW_SMALLICON;
1609 break;
1610 case LVS_LIST:
1611 infoPtr->uView = LV_VIEW_LIST;
1615 /* computes next item id value */
1616 static DWORD get_next_itemid(const LISTVIEW_INFO *infoPtr)
1618 INT count = DPA_GetPtrCount(infoPtr->hdpaItemIds);
1620 if (count > 0)
1622 ITEM_ID *lpID = DPA_GetPtr(infoPtr->hdpaItemIds, count - 1);
1623 return lpID->id + 1;
1625 return 0;
1628 /******** Internal API functions ************************************/
1630 static inline COLUMN_INFO * LISTVIEW_GetColumnInfo(const LISTVIEW_INFO *infoPtr, INT nSubItem)
1632 static COLUMN_INFO mainItem;
1634 if (nSubItem == 0 && DPA_GetPtrCount(infoPtr->hdpaColumns) == 0) return &mainItem;
1635 assert (nSubItem >= 0 && nSubItem < DPA_GetPtrCount(infoPtr->hdpaColumns));
1637 /* update cached column rectangles */
1638 if (infoPtr->colRectsDirty)
1640 COLUMN_INFO *info;
1641 LISTVIEW_INFO *Ptr = (LISTVIEW_INFO*)infoPtr;
1642 INT i;
1644 for (i = 0; i < DPA_GetPtrCount(infoPtr->hdpaColumns); i++) {
1645 info = DPA_GetPtr(infoPtr->hdpaColumns, i);
1646 SendMessageW(infoPtr->hwndHeader, HDM_GETITEMRECT, i, (LPARAM)&info->rcHeader);
1648 Ptr->colRectsDirty = FALSE;
1651 return DPA_GetPtr(infoPtr->hdpaColumns, nSubItem);
1654 static INT LISTVIEW_CreateHeader(LISTVIEW_INFO *infoPtr)
1656 DWORD dFlags = WS_CHILD | HDS_HORZ | HDS_FULLDRAG | HDS_DRAGDROP;
1657 HINSTANCE hInst;
1659 if (infoPtr->hwndHeader) return 0;
1661 TRACE("Creating header for list %p\n", infoPtr->hwndSelf);
1663 /* setup creation flags */
1664 dFlags |= (LVS_NOSORTHEADER & infoPtr->dwStyle) ? 0 : HDS_BUTTONS;
1665 dFlags |= (LVS_NOCOLUMNHEADER & infoPtr->dwStyle) ? HDS_HIDDEN : 0;
1667 hInst = (HINSTANCE)GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_HINSTANCE);
1669 /* create header */
1670 infoPtr->hwndHeader = CreateWindowW(WC_HEADERW, NULL, dFlags,
1671 0, 0, 0, 0, infoPtr->hwndSelf, NULL, hInst, NULL);
1672 if (!infoPtr->hwndHeader) return -1;
1674 /* set header unicode format */
1675 SendMessageW(infoPtr->hwndHeader, HDM_SETUNICODEFORMAT, TRUE, 0);
1677 /* set header font */
1678 SendMessageW(infoPtr->hwndHeader, WM_SETFONT, (WPARAM)infoPtr->hFont, TRUE);
1680 /* set header image list */
1681 if (infoPtr->himlSmall)
1682 SendMessageW(infoPtr->hwndHeader, HDM_SETIMAGELIST, 0, (LPARAM)infoPtr->himlSmall);
1684 LISTVIEW_UpdateSize(infoPtr);
1686 return 0;
1689 static inline void LISTVIEW_GetHeaderRect(const LISTVIEW_INFO *infoPtr, INT nSubItem, LPRECT lprc)
1691 *lprc = LISTVIEW_GetColumnInfo(infoPtr, nSubItem)->rcHeader;
1694 static inline BOOL LISTVIEW_IsHeaderEnabled(const LISTVIEW_INFO *infoPtr)
1696 return (infoPtr->uView == LV_VIEW_DETAILS ||
1697 infoPtr->dwLvExStyle & LVS_EX_HEADERINALLVIEWS) &&
1698 !(infoPtr->dwStyle & LVS_NOCOLUMNHEADER);
1701 static inline BOOL LISTVIEW_GetItemW(const LISTVIEW_INFO *infoPtr, LPLVITEMW lpLVItem)
1703 return LISTVIEW_GetItemT(infoPtr, lpLVItem, TRUE);
1706 /* used to handle collapse main item column case */
1707 static inline BOOL LISTVIEW_DrawFocusRect(const LISTVIEW_INFO *infoPtr, HDC hdc)
1709 return (infoPtr->rcFocus.left < infoPtr->rcFocus.right) ?
1710 DrawFocusRect(hdc, &infoPtr->rcFocus) : FALSE;
1713 /* Listview invalidation functions: use _only_ these functions to invalidate */
1715 static inline BOOL is_redrawing(const LISTVIEW_INFO *infoPtr)
1717 return infoPtr->redraw;
1720 static inline void LISTVIEW_InvalidateRect(const LISTVIEW_INFO *infoPtr, const RECT* rect)
1722 if(!is_redrawing(infoPtr)) return;
1723 TRACE(" invalidating rect=%s\n", wine_dbgstr_rect(rect));
1724 InvalidateRect(infoPtr->hwndSelf, rect, TRUE);
1727 static inline void LISTVIEW_InvalidateItem(const LISTVIEW_INFO *infoPtr, INT nItem)
1729 RECT rcBox;
1731 if (!is_redrawing(infoPtr) || nItem < 0 || nItem >= infoPtr->nItemCount)
1732 return;
1734 LISTVIEW_GetItemBox(infoPtr, nItem, &rcBox);
1735 LISTVIEW_InvalidateRect(infoPtr, &rcBox);
1738 static inline void LISTVIEW_InvalidateSubItem(const LISTVIEW_INFO *infoPtr, INT nItem, INT nSubItem)
1740 POINT Origin, Position;
1741 RECT rcBox;
1743 if(!is_redrawing(infoPtr)) return;
1744 assert (infoPtr->uView == LV_VIEW_DETAILS);
1745 LISTVIEW_GetOrigin(infoPtr, &Origin);
1746 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position);
1747 LISTVIEW_GetHeaderRect(infoPtr, nSubItem, &rcBox);
1748 rcBox.top = 0;
1749 rcBox.bottom = infoPtr->nItemHeight;
1750 OffsetRect(&rcBox, Origin.x, Origin.y + Position.y);
1751 LISTVIEW_InvalidateRect(infoPtr, &rcBox);
1754 static inline void LISTVIEW_InvalidateList(const LISTVIEW_INFO *infoPtr)
1756 LISTVIEW_InvalidateRect(infoPtr, NULL);
1759 static inline void LISTVIEW_InvalidateColumn(const LISTVIEW_INFO *infoPtr, INT nColumn)
1761 RECT rcCol;
1763 if(!is_redrawing(infoPtr)) return;
1764 LISTVIEW_GetHeaderRect(infoPtr, nColumn, &rcCol);
1765 rcCol.top = infoPtr->rcList.top;
1766 rcCol.bottom = infoPtr->rcList.bottom;
1767 LISTVIEW_InvalidateRect(infoPtr, &rcCol);
1770 /***
1771 * DESCRIPTION:
1772 * Retrieves the number of items that can fit vertically in the client area.
1774 * PARAMETER(S):
1775 * [I] infoPtr : valid pointer to the listview structure
1777 * RETURN:
1778 * Number of items per row.
1780 static inline INT LISTVIEW_GetCountPerRow(const LISTVIEW_INFO *infoPtr)
1782 INT nListWidth = infoPtr->rcList.right - infoPtr->rcList.left;
1784 return max(nListWidth/(infoPtr->nItemWidth ? infoPtr->nItemWidth : 1), 1);
1787 /***
1788 * DESCRIPTION:
1789 * Retrieves the number of items that can fit horizontally in the client
1790 * area.
1792 * PARAMETER(S):
1793 * [I] infoPtr : valid pointer to the listview structure
1795 * RETURN:
1796 * Number of items per column.
1798 static inline INT LISTVIEW_GetCountPerColumn(const LISTVIEW_INFO *infoPtr)
1800 INT nListHeight = infoPtr->rcList.bottom - infoPtr->rcList.top;
1802 return infoPtr->nItemHeight ? max(nListHeight / infoPtr->nItemHeight, 1) : 0;
1806 /*************************************************************************
1807 * LISTVIEW_ProcessLetterKeys
1809 * Processes keyboard messages generated by pressing the letter keys
1810 * on the keyboard.
1811 * What this does is perform a case insensitive search from the
1812 * current position with the following quirks:
1813 * - If two chars or more are pressed in quick succession we search
1814 * for the corresponding string (e.g. 'abc').
1815 * - If there is a delay we wipe away the current search string and
1816 * restart with just that char.
1817 * - If the user keeps pressing the same character, whether slowly or
1818 * fast, so that the search string is entirely composed of this
1819 * character ('aaaaa' for instance), then we search for first item
1820 * that starting with that character.
1821 * - If the user types the above character in quick succession, then
1822 * we must also search for the corresponding string ('aaaaa'), and
1823 * go to that string if there is a match.
1825 * PARAMETERS
1826 * [I] hwnd : handle to the window
1827 * [I] charCode : the character code, the actual character
1828 * [I] keyData : key data
1830 * RETURNS
1832 * Zero.
1834 * BUGS
1836 * - The current implementation has a list of characters it will
1837 * accept and it ignores everything else. In particular it will
1838 * ignore accentuated characters which seems to match what
1839 * Windows does. But I'm not sure it makes sense to follow
1840 * Windows there.
1841 * - We don't sound a beep when the search fails.
1843 * SEE ALSO
1845 * TREEVIEW_ProcessLetterKeys
1847 static INT LISTVIEW_ProcessLetterKeys(LISTVIEW_INFO *infoPtr, WPARAM charCode, LPARAM keyData)
1849 WCHAR buffer[MAX_PATH];
1850 DWORD prevTime;
1851 LVITEMW item;
1852 int startidx;
1853 INT nItem;
1854 INT diff;
1856 /* simple parameter checking */
1857 if (!charCode || !keyData || infoPtr->nItemCount == 0) return 0;
1859 /* only allow the valid WM_CHARs through */
1860 if (!iswalnum(charCode) &&
1861 charCode != '.' && charCode != '`' && charCode != '!' &&
1862 charCode != '@' && charCode != '#' && charCode != '$' &&
1863 charCode != '%' && charCode != '^' && charCode != '&' &&
1864 charCode != '*' && charCode != '(' && charCode != ')' &&
1865 charCode != '-' && charCode != '_' && charCode != '+' &&
1866 charCode != '=' && charCode != '\\'&& charCode != ']' &&
1867 charCode != '}' && charCode != '[' && charCode != '{' &&
1868 charCode != '/' && charCode != '?' && charCode != '>' &&
1869 charCode != '<' && charCode != ',' && charCode != '~')
1870 return 0;
1872 /* update the search parameters */
1873 prevTime = infoPtr->lastKeyPressTimestamp;
1874 infoPtr->lastKeyPressTimestamp = GetTickCount();
1875 diff = infoPtr->lastKeyPressTimestamp - prevTime;
1877 if (diff >= 0 && diff < KEY_DELAY)
1879 if (infoPtr->nSearchParamLength < MAX_PATH - 1)
1880 infoPtr->szSearchParam[infoPtr->nSearchParamLength++] = charCode;
1882 if (infoPtr->charCode != charCode)
1883 infoPtr->charCode = charCode = 0;
1885 else
1887 infoPtr->charCode = charCode;
1888 infoPtr->szSearchParam[0] = charCode;
1889 infoPtr->nSearchParamLength = 1;
1892 /* should start from next after focused item, so next item that matches
1893 will be selected, if there isn't any and focused matches it will be selected
1894 on second search stage from beginning of the list */
1895 if (infoPtr->nFocusedItem >= 0 && infoPtr->nItemCount > 1)
1897 /* with some accumulated search data available start with current focus, otherwise
1898 it's excluded from search */
1899 startidx = infoPtr->nSearchParamLength > 1 ? infoPtr->nFocusedItem : infoPtr->nFocusedItem + 1;
1900 if (startidx == infoPtr->nItemCount) startidx = 0;
1902 else
1903 startidx = 0;
1905 /* let application handle this for virtual listview */
1906 if (infoPtr->dwStyle & LVS_OWNERDATA)
1908 NMLVFINDITEMW nmlv;
1910 memset(&nmlv.lvfi, 0, sizeof(nmlv.lvfi));
1911 nmlv.lvfi.flags = (LVFI_WRAP | LVFI_PARTIAL);
1912 nmlv.lvfi.psz = infoPtr->szSearchParam;
1913 nmlv.iStart = startidx;
1915 infoPtr->szSearchParam[infoPtr->nSearchParamLength] = 0;
1917 nItem = notify_hdr(infoPtr, LVN_ODFINDITEMW, (LPNMHDR)&nmlv.hdr);
1919 else
1921 int i = startidx, endidx;
1923 /* and search from the current position */
1924 nItem = -1;
1925 endidx = infoPtr->nItemCount;
1927 /* first search in [startidx, endidx), on failure continue in [0, startidx) */
1928 while (1)
1930 /* start from first item if not found with >= startidx */
1931 if (i == infoPtr->nItemCount && startidx > 0)
1933 endidx = startidx;
1934 startidx = 0;
1937 for (i = startidx; i < endidx; i++)
1939 /* retrieve text */
1940 item.mask = LVIF_TEXT;
1941 item.iItem = i;
1942 item.iSubItem = 0;
1943 item.pszText = buffer;
1944 item.cchTextMax = MAX_PATH;
1945 if (!LISTVIEW_GetItemW(infoPtr, &item)) return 0;
1947 if (!wcsnicmp(item.pszText, infoPtr->szSearchParam, infoPtr->nSearchParamLength))
1949 nItem = i;
1950 break;
1952 /* this is used to find first char match when search string is not available yet,
1953 otherwise every WM_CHAR will search to next item by first char, ignoring that we're
1954 already waiting for user to complete a string */
1955 else if (nItem == -1 && infoPtr->nSearchParamLength == 1 && !wcsnicmp(item.pszText, infoPtr->szSearchParam, 1))
1957 /* this would work but we must keep looking for a longer match */
1958 nItem = i;
1962 if ( nItem != -1 || /* found something */
1963 endidx != infoPtr->nItemCount || /* second search done */
1964 (startidx == 0 && endidx == infoPtr->nItemCount) /* full range for first search */ )
1965 break;
1969 if (nItem != -1)
1970 LISTVIEW_KeySelection(infoPtr, nItem, FALSE);
1972 return 0;
1975 /*************************************************************************
1976 * LISTVIEW_UpdateHeaderSize [Internal]
1978 * Function to resize the header control
1980 * PARAMS
1981 * [I] hwnd : handle to a window
1982 * [I] nNewScrollPos : scroll pos to set
1984 * RETURNS
1985 * None.
1987 static void LISTVIEW_UpdateHeaderSize(const LISTVIEW_INFO *infoPtr, INT nNewScrollPos)
1989 RECT winRect;
1990 POINT point[2];
1992 TRACE("nNewScrollPos=%d\n", nNewScrollPos);
1994 if (!infoPtr->hwndHeader) return;
1996 GetWindowRect(infoPtr->hwndHeader, &winRect);
1997 point[0].x = winRect.left;
1998 point[0].y = winRect.top;
1999 point[1].x = winRect.right;
2000 point[1].y = winRect.bottom;
2002 MapWindowPoints(HWND_DESKTOP, infoPtr->hwndSelf, point, 2);
2003 point[0].x = -nNewScrollPos;
2004 point[1].x += nNewScrollPos;
2006 SetWindowPos(infoPtr->hwndHeader,0,
2007 point[0].x,point[0].y,point[1].x,point[1].y,
2008 (infoPtr->dwStyle & LVS_NOCOLUMNHEADER) ? SWP_HIDEWINDOW : SWP_SHOWWINDOW |
2009 SWP_NOZORDER | SWP_NOACTIVATE);
2012 static INT LISTVIEW_UpdateHScroll(LISTVIEW_INFO *infoPtr)
2014 SCROLLINFO horzInfo;
2015 INT dx;
2017 ZeroMemory(&horzInfo, sizeof(SCROLLINFO));
2018 horzInfo.cbSize = sizeof(SCROLLINFO);
2019 horzInfo.nPage = infoPtr->rcList.right - infoPtr->rcList.left;
2021 /* for now, we'll set info.nMax to the _count_, and adjust it later */
2022 if (infoPtr->uView == LV_VIEW_LIST)
2024 INT nPerCol = LISTVIEW_GetCountPerColumn(infoPtr);
2025 horzInfo.nMax = (infoPtr->nItemCount + nPerCol - 1) / nPerCol;
2027 /* scroll by at least one column per page */
2028 if(horzInfo.nPage < infoPtr->nItemWidth)
2029 horzInfo.nPage = infoPtr->nItemWidth;
2031 if (infoPtr->nItemWidth)
2032 horzInfo.nPage /= infoPtr->nItemWidth;
2034 else if (infoPtr->uView == LV_VIEW_DETAILS)
2036 horzInfo.nMax = infoPtr->nItemWidth;
2038 else /* LV_VIEW_ICON, or LV_VIEW_SMALLICON */
2040 RECT rcView;
2042 if (LISTVIEW_GetViewRect(infoPtr, &rcView)) horzInfo.nMax = rcView.right - rcView.left;
2045 if (LISTVIEW_IsHeaderEnabled(infoPtr))
2047 if (DPA_GetPtrCount(infoPtr->hdpaColumns))
2049 RECT rcHeader;
2050 INT index;
2052 index = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX,
2053 DPA_GetPtrCount(infoPtr->hdpaColumns) - 1, 0);
2055 LISTVIEW_GetHeaderRect(infoPtr, index, &rcHeader);
2056 horzInfo.nMax = rcHeader.right;
2057 TRACE("horzInfo.nMax=%d\n", horzInfo.nMax);
2061 horzInfo.fMask = SIF_RANGE | SIF_PAGE;
2062 horzInfo.nMax = max(horzInfo.nMax - 1, 0);
2063 dx = GetScrollPos(infoPtr->hwndSelf, SB_HORZ);
2064 dx -= SetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &horzInfo, TRUE);
2065 TRACE("horzInfo=%s\n", debugscrollinfo(&horzInfo));
2067 /* Update the Header Control */
2068 if (infoPtr->hwndHeader)
2070 horzInfo.fMask = SIF_POS;
2071 GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &horzInfo);
2072 LISTVIEW_UpdateHeaderSize(infoPtr, horzInfo.nPos);
2075 LISTVIEW_UpdateSize(infoPtr);
2076 return dx;
2079 static INT LISTVIEW_UpdateVScroll(LISTVIEW_INFO *infoPtr)
2081 SCROLLINFO vertInfo;
2082 INT dy;
2084 ZeroMemory(&vertInfo, sizeof(SCROLLINFO));
2085 vertInfo.cbSize = sizeof(SCROLLINFO);
2086 vertInfo.nPage = infoPtr->rcList.bottom - infoPtr->rcList.top;
2088 if (infoPtr->uView == LV_VIEW_DETAILS)
2090 vertInfo.nMax = infoPtr->nItemCount;
2092 /* scroll by at least one page */
2093 if(vertInfo.nPage < infoPtr->nItemHeight)
2094 vertInfo.nPage = infoPtr->nItemHeight;
2096 if (infoPtr->nItemHeight > 0)
2097 vertInfo.nPage /= infoPtr->nItemHeight;
2099 else if (infoPtr->uView != LV_VIEW_LIST) /* LV_VIEW_ICON, or LV_VIEW_SMALLICON */
2101 RECT rcView;
2103 if (LISTVIEW_GetViewRect(infoPtr, &rcView)) vertInfo.nMax = rcView.bottom - rcView.top;
2106 vertInfo.fMask = SIF_RANGE | SIF_PAGE;
2107 vertInfo.nMax = max(vertInfo.nMax - 1, 0);
2108 dy = GetScrollPos(infoPtr->hwndSelf, SB_VERT);
2109 dy -= SetScrollInfo(infoPtr->hwndSelf, SB_VERT, &vertInfo, TRUE);
2110 TRACE("vertInfo=%s\n", debugscrollinfo(&vertInfo));
2112 LISTVIEW_UpdateSize(infoPtr);
2113 return dy;
2116 /***
2117 * DESCRIPTION:
2118 * Update the scrollbars. This function should be called whenever
2119 * the content, size or view changes.
2121 * PARAMETER(S):
2122 * [I] infoPtr : valid pointer to the listview structure
2124 * RETURN:
2125 * None
2127 static void LISTVIEW_UpdateScroll(LISTVIEW_INFO *infoPtr)
2129 INT dx, dy, pass;
2131 if ((infoPtr->dwStyle & LVS_NOSCROLL) || !is_redrawing(infoPtr)) return;
2133 /* Setting the horizontal scroll can change the listview size
2134 * (and potentially everything else) so we need to recompute
2135 * everything again for the vertical scroll and vice-versa
2137 for (dx = 0, dy = 0, pass = 0; pass <= 1; pass++)
2139 dx += LISTVIEW_UpdateHScroll(infoPtr);
2140 dy += LISTVIEW_UpdateVScroll(infoPtr);
2143 /* Change of the range may have changed the scroll pos. If so move the content */
2144 if (dx != 0 || dy != 0)
2146 RECT listRect;
2147 listRect = infoPtr->rcList;
2148 ScrollWindowEx(infoPtr->hwndSelf, dx, dy, &listRect, &listRect, 0, 0,
2149 SW_ERASE | SW_INVALIDATE);
2154 /***
2155 * DESCRIPTION:
2156 * Shows/hides the focus rectangle.
2158 * PARAMETER(S):
2159 * [I] infoPtr : valid pointer to the listview structure
2160 * [I] fShow : TRUE to show the focus, FALSE to hide it.
2162 * RETURN:
2163 * None
2165 static void LISTVIEW_ShowFocusRect(const LISTVIEW_INFO *infoPtr, BOOL fShow)
2167 HDC hdc;
2169 TRACE("fShow=%d, nItem=%d\n", fShow, infoPtr->nFocusedItem);
2171 if (infoPtr->nFocusedItem < 0) return;
2173 /* we need some gymnastics in ICON mode to handle large items */
2174 if (infoPtr->uView == LV_VIEW_ICON)
2176 RECT rcBox;
2178 LISTVIEW_GetItemBox(infoPtr, infoPtr->nFocusedItem, &rcBox);
2179 if ((rcBox.bottom - rcBox.top) > infoPtr->nItemHeight)
2181 LISTVIEW_InvalidateRect(infoPtr, &rcBox);
2182 return;
2186 if (!(hdc = GetDC(infoPtr->hwndSelf))) return;
2188 /* for some reason, owner draw should work only in report mode */
2189 if ((infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && (infoPtr->uView == LV_VIEW_DETAILS))
2191 DRAWITEMSTRUCT dis;
2192 LVITEMW item;
2194 HFONT hFont = infoPtr->hFont ? infoPtr->hFont : infoPtr->hDefaultFont;
2195 HFONT hOldFont = SelectObject(hdc, hFont);
2197 item.iItem = infoPtr->nFocusedItem;
2198 item.iSubItem = 0;
2199 item.mask = LVIF_PARAM;
2200 if (!LISTVIEW_GetItemW(infoPtr, &item)) goto done;
2202 ZeroMemory(&dis, sizeof(dis));
2203 dis.CtlType = ODT_LISTVIEW;
2204 dis.CtlID = (UINT)GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
2205 dis.itemID = item.iItem;
2206 dis.itemAction = ODA_FOCUS;
2207 if (fShow) dis.itemState |= ODS_FOCUS;
2208 dis.hwndItem = infoPtr->hwndSelf;
2209 dis.hDC = hdc;
2210 LISTVIEW_GetItemBox(infoPtr, dis.itemID, &dis.rcItem);
2211 dis.itemData = item.lParam;
2213 SendMessageW(infoPtr->hwndNotify, WM_DRAWITEM, dis.CtlID, (LPARAM)&dis);
2215 SelectObject(hdc, hOldFont);
2217 else
2218 LISTVIEW_InvalidateItem(infoPtr, infoPtr->nFocusedItem);
2220 done:
2221 ReleaseDC(infoPtr->hwndSelf, hdc);
2224 /***
2225 * Invalidates all visible selected items.
2227 static void LISTVIEW_InvalidateSelectedItems(const LISTVIEW_INFO *infoPtr)
2229 ITERATOR i;
2231 iterator_frameditems(&i, infoPtr, &infoPtr->rcList);
2232 while(iterator_next(&i))
2234 if (LISTVIEW_GetItemState(infoPtr, i.nItem, LVIS_SELECTED))
2235 LISTVIEW_InvalidateItem(infoPtr, i.nItem);
2237 iterator_destroy(&i);
2241 /***
2242 * DESCRIPTION: [INTERNAL]
2243 * Computes an item's (left,top) corner, relative to rcView.
2244 * That is, the position has NOT been made relative to the Origin.
2245 * This is deliberate, to avoid computing the Origin over, and
2246 * over again, when this function is called in a loop. Instead,
2247 * one can factor the computation of the Origin before the loop,
2248 * and offset the value returned by this function, on every iteration.
2250 * PARAMETER(S):
2251 * [I] infoPtr : valid pointer to the listview structure
2252 * [I] nItem : item number
2253 * [O] lpptOrig : item top, left corner
2255 * RETURN:
2256 * None.
2258 static void LISTVIEW_GetItemOrigin(const LISTVIEW_INFO *infoPtr, INT nItem, LPPOINT lpptPosition)
2260 assert(nItem >= 0 && nItem < infoPtr->nItemCount);
2262 if ((infoPtr->uView == LV_VIEW_SMALLICON) || (infoPtr->uView == LV_VIEW_ICON))
2264 lpptPosition->x = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosX, nItem);
2265 lpptPosition->y = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosY, nItem);
2267 else if (infoPtr->uView == LV_VIEW_LIST)
2269 INT nCountPerColumn = LISTVIEW_GetCountPerColumn(infoPtr);
2270 lpptPosition->x = nItem / nCountPerColumn * infoPtr->nItemWidth;
2271 lpptPosition->y = nItem % nCountPerColumn * infoPtr->nItemHeight;
2273 else /* LV_VIEW_DETAILS */
2275 lpptPosition->x = REPORT_MARGINX;
2276 /* item is always at zero indexed column */
2277 if (DPA_GetPtrCount(infoPtr->hdpaColumns) > 0)
2278 lpptPosition->x += LISTVIEW_GetColumnInfo(infoPtr, 0)->rcHeader.left;
2279 lpptPosition->y = nItem * infoPtr->nItemHeight;
2283 /***
2284 * DESCRIPTION: [INTERNAL]
2285 * Compute the rectangles of an item. This is to localize all
2286 * the computations in one place. If you are not interested in some
2287 * of these values, simply pass in a NULL -- the function is smart
2288 * enough to compute only what's necessary. The function computes
2289 * the standard rectangles (BOUNDS, ICON, LABEL) plus a non-standard
2290 * one, the BOX rectangle. This rectangle is very cheap to compute,
2291 * and is guaranteed to contain all the other rectangles. Computing
2292 * the ICON rect is also cheap, but all the others are potentially
2293 * expensive. This gives an easy and effective optimization when
2294 * searching (like point inclusion, or rectangle intersection):
2295 * first test against the BOX, and if TRUE, test against the desired
2296 * rectangle.
2297 * If the function does not have all the necessary information
2298 * to computed the requested rectangles, will crash with a
2299 * failed assertion. This is done so we catch all programming
2300 * errors, given that the function is called only from our code.
2302 * We have the following 'special' meanings for a few fields:
2303 * * If LVIS_FOCUSED is set, we assume the item has the focus
2304 * This is important in ICON mode, where it might get a larger
2305 * then usual rectangle
2307 * Please note that subitem support works only in REPORT mode.
2309 * PARAMETER(S):
2310 * [I] infoPtr : valid pointer to the listview structure
2311 * [I] lpLVItem : item to compute the measures for
2312 * [O] lprcBox : ptr to Box rectangle
2313 * Same as LVM_GETITEMRECT with LVIR_BOUNDS
2314 * [0] lprcSelectBox : ptr to select box rectangle
2315 * Same as LVM_GETITEMRECT with LVIR_SELECTEDBOUNDS
2316 * [O] lprcIcon : ptr to Icon rectangle
2317 * Same as LVM_GETITEMRECT with LVIR_ICON
2318 * [O] lprcStateIcon: ptr to State Icon rectangle
2319 * [O] lprcLabel : ptr to Label rectangle
2320 * Same as LVM_GETITEMRECT with LVIR_LABEL
2322 * RETURN:
2323 * None.
2325 static void LISTVIEW_GetItemMetrics(const LISTVIEW_INFO *infoPtr, const LVITEMW *lpLVItem,
2326 LPRECT lprcBox, LPRECT lprcSelectBox,
2327 LPRECT lprcIcon, LPRECT lprcStateIcon, LPRECT lprcLabel)
2329 BOOL doSelectBox = FALSE, doIcon = FALSE, doLabel = FALSE, oversizedBox = FALSE;
2330 RECT Box, SelectBox, Icon, Label;
2331 COLUMN_INFO *lpColumnInfo = NULL;
2332 SIZE labelSize = { 0, 0 };
2334 TRACE("(lpLVItem=%s)\n", debuglvitem_t(lpLVItem, TRUE));
2336 /* Be smart and try to figure out the minimum we have to do */
2337 if (lpLVItem->iSubItem) assert(infoPtr->uView == LV_VIEW_DETAILS);
2338 if (infoPtr->uView == LV_VIEW_ICON && (lprcBox || lprcLabel))
2340 assert((lpLVItem->mask & LVIF_STATE) && (lpLVItem->stateMask & LVIS_FOCUSED));
2341 if (lpLVItem->state & LVIS_FOCUSED) oversizedBox = doLabel = TRUE;
2343 if (lprcSelectBox) doSelectBox = TRUE;
2344 if (lprcLabel) doLabel = TRUE;
2345 if (doLabel || lprcIcon || lprcStateIcon) doIcon = TRUE;
2346 if (doSelectBox)
2348 doIcon = TRUE;
2349 doLabel = TRUE;
2352 /************************************************************/
2353 /* compute the box rectangle (it should be cheap to do) */
2354 /************************************************************/
2355 if (lpLVItem->iSubItem || infoPtr->uView == LV_VIEW_DETAILS)
2356 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, lpLVItem->iSubItem);
2358 if (lpLVItem->iSubItem)
2360 Box = lpColumnInfo->rcHeader;
2362 else
2364 Box.left = 0;
2365 Box.right = infoPtr->nItemWidth;
2367 Box.top = 0;
2368 Box.bottom = infoPtr->nItemHeight;
2370 /******************************************************************/
2371 /* compute ICON bounding box (ala LVM_GETITEMRECT) and STATEICON */
2372 /******************************************************************/
2373 if (doIcon)
2375 LONG state_width = 0;
2377 if (infoPtr->himlState && lpLVItem->iSubItem == 0)
2378 state_width = infoPtr->iconStateSize.cx;
2380 if (infoPtr->uView == LV_VIEW_ICON)
2382 Icon.left = Box.left + state_width;
2383 if (infoPtr->himlNormal)
2384 Icon.left += (infoPtr->nItemWidth - infoPtr->iconSize.cx - state_width) / 2;
2385 Icon.top = Box.top + ICON_TOP_PADDING;
2386 Icon.right = Icon.left;
2387 Icon.bottom = Icon.top;
2388 if (infoPtr->himlNormal)
2390 Icon.right += infoPtr->iconSize.cx;
2391 Icon.bottom += infoPtr->iconSize.cy;
2394 else /* LV_VIEW_SMALLICON, LV_VIEW_LIST or LV_VIEW_DETAILS */
2396 Icon.left = Box.left + state_width;
2398 if (infoPtr->uView == LV_VIEW_DETAILS && lpLVItem->iSubItem == 0)
2400 /* we need the indent in report mode */
2401 assert(lpLVItem->mask & LVIF_INDENT);
2402 Icon.left += infoPtr->iconSize.cx * lpLVItem->iIndent + REPORT_MARGINX;
2405 Icon.top = Box.top;
2406 Icon.right = Icon.left;
2407 if (infoPtr->himlSmall &&
2408 (!lpColumnInfo || lpLVItem->iSubItem == 0 ||
2409 ((infoPtr->dwLvExStyle & LVS_EX_SUBITEMIMAGES) && lpLVItem->iImage != I_IMAGECALLBACK)))
2410 Icon.right += infoPtr->iconSize.cx;
2411 Icon.bottom = Icon.top + infoPtr->iconSize.cy;
2413 if(lprcIcon) *lprcIcon = Icon;
2414 TRACE(" - icon=%s\n", wine_dbgstr_rect(&Icon));
2416 /* TODO: is this correct? */
2417 if (lprcStateIcon)
2419 lprcStateIcon->left = Icon.left - state_width;
2420 lprcStateIcon->right = Icon.left;
2421 lprcStateIcon->top = Icon.top;
2422 lprcStateIcon->bottom = lprcStateIcon->top + infoPtr->iconSize.cy;
2423 TRACE(" - state icon=%s\n", wine_dbgstr_rect(lprcStateIcon));
2426 else Icon.right = 0;
2428 /************************************************************/
2429 /* compute LABEL bounding box (ala LVM_GETITEMRECT) */
2430 /************************************************************/
2431 if (doLabel)
2433 /* calculate how far to the right can the label stretch */
2434 Label.right = Box.right;
2435 if (infoPtr->uView == LV_VIEW_DETAILS)
2437 if (lpLVItem->iSubItem == 0)
2439 /* we need a zero based rect here */
2440 Label = lpColumnInfo->rcHeader;
2441 OffsetRect(&Label, -Label.left, 0);
2445 if (lpLVItem->iSubItem || ((infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && infoPtr->uView == LV_VIEW_DETAILS))
2447 labelSize.cx = infoPtr->nItemWidth;
2448 labelSize.cy = infoPtr->nItemHeight;
2449 goto calc_label;
2452 /* we need the text in non owner draw mode */
2453 assert(lpLVItem->mask & LVIF_TEXT);
2454 if (is_text(lpLVItem->pszText))
2456 HFONT hFont = infoPtr->hFont ? infoPtr->hFont : infoPtr->hDefaultFont;
2457 HDC hdc = GetDC(infoPtr->hwndSelf);
2458 HFONT hOldFont = SelectObject(hdc, hFont);
2459 UINT uFormat;
2460 RECT rcText;
2462 /* compute rough rectangle where the label will go */
2463 SetRectEmpty(&rcText);
2464 rcText.right = infoPtr->nItemWidth - TRAILING_LABEL_PADDING;
2465 rcText.bottom = infoPtr->nItemHeight;
2466 if (infoPtr->uView == LV_VIEW_ICON)
2467 rcText.bottom -= ICON_TOP_PADDING + infoPtr->iconSize.cy + ICON_BOTTOM_PADDING;
2469 /* now figure out the flags */
2470 if (infoPtr->uView == LV_VIEW_ICON)
2471 uFormat = oversizedBox ? LV_FL_DT_FLAGS : LV_ML_DT_FLAGS;
2472 else
2473 uFormat = LV_SL_DT_FLAGS;
2475 DrawTextW (hdc, lpLVItem->pszText, -1, &rcText, uFormat | DT_CALCRECT);
2477 if (rcText.right != rcText.left)
2478 labelSize.cx = min(rcText.right - rcText.left + TRAILING_LABEL_PADDING, infoPtr->nItemWidth);
2480 labelSize.cy = rcText.bottom - rcText.top;
2482 SelectObject(hdc, hOldFont);
2483 ReleaseDC(infoPtr->hwndSelf, hdc);
2486 calc_label:
2487 if (infoPtr->uView == LV_VIEW_ICON)
2489 Label.left = Box.left + (infoPtr->nItemWidth - labelSize.cx) / 2;
2490 Label.top = Box.top + ICON_TOP_PADDING_HITABLE +
2491 infoPtr->iconSize.cy + ICON_BOTTOM_PADDING;
2492 Label.right = Label.left + labelSize.cx;
2493 Label.bottom = Label.top + infoPtr->nItemHeight;
2494 if (!oversizedBox && labelSize.cy > infoPtr->ntmHeight)
2496 labelSize.cy = min(Box.bottom - Label.top, labelSize.cy);
2497 labelSize.cy /= infoPtr->ntmHeight;
2498 labelSize.cy = max(labelSize.cy, 1);
2499 labelSize.cy *= infoPtr->ntmHeight;
2501 Label.bottom = Label.top + labelSize.cy + HEIGHT_PADDING;
2503 else if (infoPtr->uView == LV_VIEW_DETAILS)
2505 Label.left = Icon.right;
2506 Label.top = Box.top;
2507 Label.right = lpLVItem->iSubItem ? lpColumnInfo->rcHeader.right :
2508 lpColumnInfo->rcHeader.right - lpColumnInfo->rcHeader.left;
2509 Label.bottom = Label.top + infoPtr->nItemHeight;
2511 else /* LV_VIEW_SMALLICON or LV_VIEW_LIST */
2513 Label.left = Icon.right;
2514 Label.top = Box.top;
2515 Label.right = min(Label.left + labelSize.cx, Label.right);
2516 Label.bottom = Label.top + infoPtr->nItemHeight;
2519 if (lprcLabel) *lprcLabel = Label;
2520 TRACE(" - label=%s\n", wine_dbgstr_rect(&Label));
2523 /************************************************************/
2524 /* compute SELECT bounding box */
2525 /************************************************************/
2526 if (doSelectBox)
2528 if (infoPtr->uView == LV_VIEW_DETAILS)
2530 SelectBox.left = Icon.left;
2531 SelectBox.top = Box.top;
2532 SelectBox.bottom = Box.bottom;
2534 if (labelSize.cx)
2535 SelectBox.right = min(Label.left + labelSize.cx, Label.right);
2536 else
2537 SelectBox.right = min(Label.left + MAX_EMPTYTEXT_SELECT_WIDTH, Label.right);
2539 else
2541 UnionRect(&SelectBox, &Icon, &Label);
2543 if (lprcSelectBox) *lprcSelectBox = SelectBox;
2544 TRACE(" - select box=%s\n", wine_dbgstr_rect(&SelectBox));
2547 /* Fix the Box if necessary */
2548 if (lprcBox)
2550 if (oversizedBox) UnionRect(lprcBox, &Box, &Label);
2551 else *lprcBox = Box;
2553 TRACE(" - box=%s\n", wine_dbgstr_rect(&Box));
2556 /***
2557 * DESCRIPTION: [INTERNAL]
2559 * PARAMETER(S):
2560 * [I] infoPtr : valid pointer to the listview structure
2561 * [I] nItem : item number
2562 * [O] lprcBox : ptr to Box rectangle
2564 * RETURN:
2565 * None.
2567 static void LISTVIEW_GetItemBox(const LISTVIEW_INFO *infoPtr, INT nItem, LPRECT lprcBox)
2569 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
2570 POINT Position, Origin;
2571 LVITEMW lvItem;
2573 LISTVIEW_GetOrigin(infoPtr, &Origin);
2574 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position);
2576 /* Be smart and try to figure out the minimum we have to do */
2577 lvItem.mask = 0;
2578 if (infoPtr->uView == LV_VIEW_ICON && infoPtr->bFocus && LISTVIEW_GetItemState(infoPtr, nItem, LVIS_FOCUSED))
2579 lvItem.mask |= LVIF_TEXT;
2580 lvItem.iItem = nItem;
2581 lvItem.iSubItem = 0;
2582 lvItem.pszText = szDispText;
2583 lvItem.cchTextMax = DISP_TEXT_SIZE;
2584 if (lvItem.mask) LISTVIEW_GetItemW(infoPtr, &lvItem);
2585 if (infoPtr->uView == LV_VIEW_ICON)
2587 lvItem.mask |= LVIF_STATE;
2588 lvItem.stateMask = LVIS_FOCUSED;
2589 lvItem.state = (lvItem.mask & LVIF_TEXT ? LVIS_FOCUSED : 0);
2591 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, lprcBox, 0, 0, 0, 0);
2593 if (infoPtr->uView == LV_VIEW_DETAILS && infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT &&
2594 SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX, 0, 0))
2596 OffsetRect(lprcBox, Origin.x, Position.y + Origin.y);
2598 else
2599 OffsetRect(lprcBox, Position.x + Origin.x, Position.y + Origin.y);
2602 /* LISTVIEW_MapIdToIndex helper */
2603 static INT CALLBACK MapIdSearchCompare(LPVOID p1, LPVOID p2, LPARAM lParam)
2605 ITEM_ID *id1 = (ITEM_ID*)p1;
2606 ITEM_ID *id2 = (ITEM_ID*)p2;
2608 if (id1->id == id2->id) return 0;
2610 return (id1->id < id2->id) ? -1 : 1;
2613 /***
2614 * DESCRIPTION:
2615 * Returns the item index for id specified.
2617 * PARAMETER(S):
2618 * [I] infoPtr : valid pointer to the listview structure
2619 * [I] iID : item id to get index for
2621 * RETURN:
2622 * Item index, or -1 on failure.
2624 static INT LISTVIEW_MapIdToIndex(const LISTVIEW_INFO *infoPtr, UINT iID)
2626 ITEM_ID ID;
2627 INT index;
2629 TRACE("iID=%d\n", iID);
2631 if (infoPtr->dwStyle & LVS_OWNERDATA) return -1;
2632 if (infoPtr->nItemCount == 0) return -1;
2634 ID.id = iID;
2635 index = DPA_Search(infoPtr->hdpaItemIds, &ID, -1, MapIdSearchCompare, 0, DPAS_SORTED);
2637 if (index != -1)
2639 ITEM_ID *lpID = DPA_GetPtr(infoPtr->hdpaItemIds, index);
2640 return DPA_GetPtrIndex(infoPtr->hdpaItems, lpID->item);
2643 return -1;
2646 /***
2647 * DESCRIPTION:
2648 * Returns the item id for index given.
2650 * PARAMETER(S):
2651 * [I] infoPtr : valid pointer to the listview structure
2652 * [I] iItem : item index to get id for
2654 * RETURN:
2655 * Item id.
2657 static DWORD LISTVIEW_MapIndexToId(const LISTVIEW_INFO *infoPtr, INT iItem)
2659 ITEM_INFO *lpItem;
2660 HDPA hdpaSubItems;
2662 TRACE("iItem=%d\n", iItem);
2664 if (infoPtr->dwStyle & LVS_OWNERDATA) return -1;
2665 if (iItem < 0 || iItem >= infoPtr->nItemCount) return -1;
2667 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, iItem);
2668 lpItem = DPA_GetPtr(hdpaSubItems, 0);
2670 return lpItem->id->id;
2673 /***
2674 * DESCRIPTION:
2675 * Returns the current icon position, and advances it along the top.
2676 * The returned position is not offset by Origin.
2678 * PARAMETER(S):
2679 * [I] infoPtr : valid pointer to the listview structure
2680 * [O] lpPos : will get the current icon position
2682 * RETURN:
2683 * None
2685 static void LISTVIEW_NextIconPosTop(LISTVIEW_INFO *infoPtr, LPPOINT lpPos)
2687 INT nListWidth = infoPtr->rcList.right - infoPtr->rcList.left;
2689 *lpPos = infoPtr->currIconPos;
2691 infoPtr->currIconPos.x += infoPtr->nItemWidth;
2692 if (infoPtr->currIconPos.x + infoPtr->nItemWidth <= nListWidth) return;
2694 infoPtr->currIconPos.x = 0;
2695 infoPtr->currIconPos.y += infoPtr->nItemHeight;
2699 /***
2700 * DESCRIPTION:
2701 * Returns the current icon position, and advances it down the left edge.
2702 * The returned position is not offset by Origin.
2704 * PARAMETER(S):
2705 * [I] infoPtr : valid pointer to the listview structure
2706 * [O] lpPos : will get the current icon position
2708 * RETURN:
2709 * None
2711 static void LISTVIEW_NextIconPosLeft(LISTVIEW_INFO *infoPtr, LPPOINT lpPos)
2713 INT nListHeight = infoPtr->rcList.bottom - infoPtr->rcList.top;
2715 *lpPos = infoPtr->currIconPos;
2717 infoPtr->currIconPos.y += infoPtr->nItemHeight;
2718 if (infoPtr->currIconPos.y + infoPtr->nItemHeight <= nListHeight) return;
2720 infoPtr->currIconPos.x += infoPtr->nItemWidth;
2721 infoPtr->currIconPos.y = 0;
2725 /***
2726 * DESCRIPTION:
2727 * Moves an icon to the specified position.
2728 * It takes care of invalidating the item, etc.
2730 * PARAMETER(S):
2731 * [I] infoPtr : valid pointer to the listview structure
2732 * [I] nItem : the item to move
2733 * [I] lpPos : the new icon position
2734 * [I] isNew : flags the item as being new
2736 * RETURN:
2737 * Success: TRUE
2738 * Failure: FALSE
2740 static BOOL LISTVIEW_MoveIconTo(const LISTVIEW_INFO *infoPtr, INT nItem, const POINT *lppt, BOOL isNew)
2742 POINT old;
2744 if (!isNew)
2746 old.x = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosX, nItem);
2747 old.y = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosY, nItem);
2749 if (lppt->x == old.x && lppt->y == old.y) return TRUE;
2750 LISTVIEW_InvalidateItem(infoPtr, nItem);
2753 /* Allocating a POINTER for every item is too resource intensive,
2754 * so we'll keep the (x,y) in different arrays */
2755 if (!DPA_SetPtr(infoPtr->hdpaPosX, nItem, (void *)(LONG_PTR)lppt->x)) return FALSE;
2756 if (!DPA_SetPtr(infoPtr->hdpaPosY, nItem, (void *)(LONG_PTR)lppt->y)) return FALSE;
2758 LISTVIEW_InvalidateItem(infoPtr, nItem);
2760 return TRUE;
2763 /***
2764 * DESCRIPTION:
2765 * Arranges listview items in icon display mode.
2767 * PARAMETER(S):
2768 * [I] infoPtr : valid pointer to the listview structure
2769 * [I] nAlignCode : alignment code
2771 * RETURN:
2772 * SUCCESS : TRUE
2773 * FAILURE : FALSE
2775 static BOOL LISTVIEW_Arrange(LISTVIEW_INFO *infoPtr, INT nAlignCode)
2777 void (*next_pos)(LISTVIEW_INFO *, LPPOINT);
2778 POINT pos;
2779 INT i;
2781 if (infoPtr->uView != LV_VIEW_ICON && infoPtr->uView != LV_VIEW_SMALLICON) return FALSE;
2783 TRACE("nAlignCode=%d\n", nAlignCode);
2785 if (nAlignCode == LVA_DEFAULT)
2787 if (infoPtr->dwStyle & LVS_ALIGNLEFT) nAlignCode = LVA_ALIGNLEFT;
2788 else nAlignCode = LVA_ALIGNTOP;
2791 switch (nAlignCode)
2793 case LVA_ALIGNLEFT: next_pos = LISTVIEW_NextIconPosLeft; break;
2794 case LVA_ALIGNTOP: next_pos = LISTVIEW_NextIconPosTop; break;
2795 case LVA_SNAPTOGRID: next_pos = LISTVIEW_NextIconPosTop; break; /* FIXME */
2796 default: return FALSE;
2799 infoPtr->currIconPos.x = infoPtr->currIconPos.y = 0;
2800 for (i = 0; i < infoPtr->nItemCount; i++)
2802 next_pos(infoPtr, &pos);
2803 LISTVIEW_MoveIconTo(infoPtr, i, &pos, FALSE);
2806 return TRUE;
2809 /***
2810 * DESCRIPTION:
2811 * Retrieves the bounding rectangle of all the items, not offset by Origin.
2812 * For LVS_REPORT always returns empty rectangle.
2814 * PARAMETER(S):
2815 * [I] infoPtr : valid pointer to the listview structure
2816 * [O] lprcView : bounding rectangle
2818 * RETURN:
2819 * SUCCESS : TRUE
2820 * FAILURE : FALSE
2822 static void LISTVIEW_GetAreaRect(const LISTVIEW_INFO *infoPtr, LPRECT lprcView)
2824 INT i, x, y;
2826 SetRectEmpty(lprcView);
2828 switch (infoPtr->uView)
2830 case LV_VIEW_ICON:
2831 case LV_VIEW_SMALLICON:
2832 for (i = 0; i < infoPtr->nItemCount; i++)
2834 x = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosX, i);
2835 y = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosY, i);
2836 lprcView->right = max(lprcView->right, x);
2837 lprcView->bottom = max(lprcView->bottom, y);
2839 if (infoPtr->nItemCount > 0)
2841 lprcView->right += infoPtr->nItemWidth;
2842 lprcView->bottom += infoPtr->nItemHeight;
2844 break;
2846 case LV_VIEW_LIST:
2847 y = LISTVIEW_GetCountPerColumn(infoPtr);
2848 x = infoPtr->nItemCount / y;
2849 if (infoPtr->nItemCount % y) x++;
2850 lprcView->right = x * infoPtr->nItemWidth;
2851 lprcView->bottom = y * infoPtr->nItemHeight;
2852 break;
2856 /***
2857 * DESCRIPTION:
2858 * Retrieves the bounding rectangle of all the items.
2860 * PARAMETER(S):
2861 * [I] infoPtr : valid pointer to the listview structure
2862 * [O] lprcView : bounding rectangle
2864 * RETURN:
2865 * SUCCESS : TRUE
2866 * FAILURE : FALSE
2868 static BOOL LISTVIEW_GetViewRect(const LISTVIEW_INFO *infoPtr, LPRECT lprcView)
2870 POINT ptOrigin;
2872 TRACE("(lprcView=%p)\n", lprcView);
2874 if (!lprcView) return FALSE;
2876 LISTVIEW_GetAreaRect(infoPtr, lprcView);
2878 if (infoPtr->uView != LV_VIEW_DETAILS)
2880 LISTVIEW_GetOrigin(infoPtr, &ptOrigin);
2881 OffsetRect(lprcView, ptOrigin.x, ptOrigin.y);
2884 TRACE("lprcView=%s\n", wine_dbgstr_rect(lprcView));
2886 return TRUE;
2889 /***
2890 * DESCRIPTION:
2891 * Retrieves the subitem pointer associated with the subitem index.
2893 * PARAMETER(S):
2894 * [I] hdpaSubItems : DPA handle for a specific item
2895 * [I] nSubItem : index of subitem
2897 * RETURN:
2898 * SUCCESS : subitem pointer
2899 * FAILURE : NULL
2901 static SUBITEM_INFO* LISTVIEW_GetSubItemPtr(HDPA hdpaSubItems, INT nSubItem)
2903 SUBITEM_INFO *lpSubItem;
2904 INT i;
2906 /* we should binary search here if need be */
2907 for (i = 1; i < DPA_GetPtrCount(hdpaSubItems); i++)
2909 lpSubItem = DPA_GetPtr(hdpaSubItems, i);
2910 if (lpSubItem->iSubItem == nSubItem)
2911 return lpSubItem;
2914 return NULL;
2918 /***
2919 * DESCRIPTION:
2920 * Calculates the desired item width.
2922 * PARAMETER(S):
2923 * [I] infoPtr : valid pointer to the listview structure
2925 * RETURN:
2926 * The desired item width.
2928 static INT LISTVIEW_CalculateItemWidth(const LISTVIEW_INFO *infoPtr)
2930 INT nItemWidth = 0;
2932 TRACE("uView=%d\n", infoPtr->uView);
2934 if (infoPtr->uView == LV_VIEW_ICON)
2935 nItemWidth = infoPtr->iconSpacing.cx;
2936 else if (infoPtr->uView == LV_VIEW_DETAILS)
2938 if (DPA_GetPtrCount(infoPtr->hdpaColumns) > 0)
2940 RECT rcHeader;
2941 INT index;
2943 index = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX,
2944 DPA_GetPtrCount(infoPtr->hdpaColumns) - 1, 0);
2946 LISTVIEW_GetHeaderRect(infoPtr, index, &rcHeader);
2947 nItemWidth = rcHeader.right;
2950 else /* LV_VIEW_SMALLICON, or LV_VIEW_LIST */
2952 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
2953 LVITEMW lvItem;
2954 INT i;
2956 lvItem.mask = LVIF_TEXT;
2957 lvItem.iSubItem = 0;
2959 for (i = 0; i < infoPtr->nItemCount; i++)
2961 lvItem.iItem = i;
2962 lvItem.pszText = szDispText;
2963 lvItem.cchTextMax = DISP_TEXT_SIZE;
2964 if (LISTVIEW_GetItemW(infoPtr, &lvItem))
2965 nItemWidth = max(LISTVIEW_GetStringWidthT(infoPtr, lvItem.pszText, TRUE),
2966 nItemWidth);
2969 if (infoPtr->himlSmall) nItemWidth += infoPtr->iconSize.cx;
2970 if (infoPtr->himlState) nItemWidth += infoPtr->iconStateSize.cx;
2972 nItemWidth = max(DEFAULT_COLUMN_WIDTH, nItemWidth + WIDTH_PADDING);
2975 return nItemWidth;
2978 /***
2979 * DESCRIPTION:
2980 * Calculates the desired item height.
2982 * PARAMETER(S):
2983 * [I] infoPtr : valid pointer to the listview structure
2985 * RETURN:
2986 * The desired item height.
2988 static INT LISTVIEW_CalculateItemHeight(const LISTVIEW_INFO *infoPtr)
2990 INT nItemHeight;
2992 TRACE("uView=%d\n", infoPtr->uView);
2994 if (infoPtr->uView == LV_VIEW_ICON)
2995 nItemHeight = infoPtr->iconSpacing.cy;
2996 else
2998 nItemHeight = infoPtr->ntmHeight;
2999 if (infoPtr->himlState)
3000 nItemHeight = max(nItemHeight, infoPtr->iconStateSize.cy);
3001 if (infoPtr->himlSmall)
3002 nItemHeight = max(nItemHeight, infoPtr->iconSize.cy);
3003 nItemHeight += HEIGHT_PADDING;
3004 if (infoPtr->nMeasureItemHeight > 0)
3005 nItemHeight = infoPtr->nMeasureItemHeight;
3008 return max(nItemHeight, 1);
3011 /***
3012 * DESCRIPTION:
3013 * Updates the width, and height of an item.
3015 * PARAMETER(S):
3016 * [I] infoPtr : valid pointer to the listview structure
3018 * RETURN:
3019 * None.
3021 static inline void LISTVIEW_UpdateItemSize(LISTVIEW_INFO *infoPtr)
3023 infoPtr->nItemWidth = LISTVIEW_CalculateItemWidth(infoPtr);
3024 infoPtr->nItemHeight = LISTVIEW_CalculateItemHeight(infoPtr);
3028 /***
3029 * DESCRIPTION:
3030 * Retrieves and saves important text metrics info for the current
3031 * Listview font.
3033 * PARAMETER(S):
3034 * [I] infoPtr : valid pointer to the listview structure
3037 static void LISTVIEW_SaveTextMetrics(LISTVIEW_INFO *infoPtr)
3039 HDC hdc = GetDC(infoPtr->hwndSelf);
3040 HFONT hFont = infoPtr->hFont ? infoPtr->hFont : infoPtr->hDefaultFont;
3041 HFONT hOldFont = SelectObject(hdc, hFont);
3042 TEXTMETRICW tm;
3043 SIZE sz;
3045 if (GetTextMetricsW(hdc, &tm))
3047 infoPtr->ntmHeight = tm.tmHeight;
3048 infoPtr->ntmMaxCharWidth = tm.tmMaxCharWidth;
3051 if (GetTextExtentPoint32A(hdc, "...", 3, &sz))
3052 infoPtr->nEllipsisWidth = sz.cx;
3054 SelectObject(hdc, hOldFont);
3055 ReleaseDC(infoPtr->hwndSelf, hdc);
3057 TRACE("tmHeight=%d\n", infoPtr->ntmHeight);
3060 /***
3061 * DESCRIPTION:
3062 * A compare function for ranges
3064 * PARAMETER(S)
3065 * [I] range1 : pointer to range 1;
3066 * [I] range2 : pointer to range 2;
3067 * [I] flags : flags
3069 * RETURNS:
3070 * > 0 : if range 1 > range 2
3071 * < 0 : if range 2 > range 1
3072 * = 0 : if range intersects range 2
3074 static INT CALLBACK ranges_cmp(LPVOID range1, LPVOID range2, LPARAM flags)
3076 INT cmp;
3078 if (((RANGE*)range1)->upper <= ((RANGE*)range2)->lower)
3079 cmp = -1;
3080 else if (((RANGE*)range2)->upper <= ((RANGE*)range1)->lower)
3081 cmp = 1;
3082 else
3083 cmp = 0;
3085 TRACE("range1=%s, range2=%s, cmp=%d\n", debugrange(range1), debugrange(range2), cmp);
3087 return cmp;
3090 #define ranges_check(ranges, desc) if (TRACE_ON(listview)) ranges_assert(ranges, desc, __FILE__, __LINE__)
3092 static void ranges_assert(RANGES ranges, LPCSTR desc, const char *file, int line)
3094 INT i;
3095 RANGE *prev, *curr;
3097 TRACE("*** Checking %s:%d:%s ***\n", file, line, desc);
3098 assert (ranges);
3099 assert (DPA_GetPtrCount(ranges->hdpa) >= 0);
3100 ranges_dump(ranges);
3101 if (DPA_GetPtrCount(ranges->hdpa) > 0)
3103 prev = DPA_GetPtr(ranges->hdpa, 0);
3104 assert (prev->lower >= 0 && prev->lower < prev->upper);
3105 for (i = 1; i < DPA_GetPtrCount(ranges->hdpa); i++)
3107 curr = DPA_GetPtr(ranges->hdpa, i);
3108 assert (prev->upper <= curr->lower);
3109 assert (curr->lower < curr->upper);
3110 prev = curr;
3113 TRACE("--- Done checking---\n");
3116 static RANGES ranges_create(int count)
3118 RANGES ranges = Alloc(sizeof(struct tagRANGES));
3119 if (!ranges) return NULL;
3120 ranges->hdpa = DPA_Create(count);
3121 if (ranges->hdpa) return ranges;
3122 Free(ranges);
3123 return NULL;
3126 static void ranges_clear(RANGES ranges)
3128 INT i;
3130 for(i = 0; i < DPA_GetPtrCount(ranges->hdpa); i++)
3131 Free(DPA_GetPtr(ranges->hdpa, i));
3132 DPA_DeleteAllPtrs(ranges->hdpa);
3136 static void ranges_destroy(RANGES ranges)
3138 if (!ranges) return;
3139 ranges_clear(ranges);
3140 DPA_Destroy(ranges->hdpa);
3141 Free(ranges);
3144 static RANGES ranges_clone(RANGES ranges)
3146 RANGES clone;
3147 INT i;
3149 if (!(clone = ranges_create(DPA_GetPtrCount(ranges->hdpa)))) goto fail;
3151 for (i = 0; i < DPA_GetPtrCount(ranges->hdpa); i++)
3153 RANGE *newrng = Alloc(sizeof(RANGE));
3154 if (!newrng) goto fail;
3155 *newrng = *((RANGE*)DPA_GetPtr(ranges->hdpa, i));
3156 if (!DPA_SetPtr(clone->hdpa, i, newrng))
3158 Free(newrng);
3159 goto fail;
3162 return clone;
3164 fail:
3165 TRACE ("clone failed\n");
3166 ranges_destroy(clone);
3167 return NULL;
3170 static RANGES ranges_diff(RANGES ranges, RANGES sub)
3172 INT i;
3174 for (i = 0; i < DPA_GetPtrCount(sub->hdpa); i++)
3175 ranges_del(ranges, *((RANGE *)DPA_GetPtr(sub->hdpa, i)));
3177 return ranges;
3180 static void ranges_dump(RANGES ranges)
3182 INT i;
3184 for (i = 0; i < DPA_GetPtrCount(ranges->hdpa); i++)
3185 TRACE(" %s\n", debugrange(DPA_GetPtr(ranges->hdpa, i)));
3188 static inline BOOL ranges_contain(RANGES ranges, INT nItem)
3190 RANGE srchrng = { nItem, nItem + 1 };
3192 TRACE("(nItem=%d)\n", nItem);
3193 ranges_check(ranges, "before contain");
3194 return DPA_Search(ranges->hdpa, &srchrng, 0, ranges_cmp, 0, DPAS_SORTED) != -1;
3197 static INT ranges_itemcount(RANGES ranges)
3199 INT i, count = 0;
3201 for (i = 0; i < DPA_GetPtrCount(ranges->hdpa); i++)
3203 RANGE *sel = DPA_GetPtr(ranges->hdpa, i);
3204 count += sel->upper - sel->lower;
3207 return count;
3210 static BOOL ranges_shift(RANGES ranges, INT nItem, INT delta, INT nUpper)
3212 RANGE srchrng = { nItem, nItem + 1 }, *chkrng;
3213 INT index;
3215 index = DPA_Search(ranges->hdpa, &srchrng, 0, ranges_cmp, 0, DPAS_SORTED | DPAS_INSERTAFTER);
3216 if (index == -1) return TRUE;
3218 for (; index < DPA_GetPtrCount(ranges->hdpa); index++)
3220 chkrng = DPA_GetPtr(ranges->hdpa, index);
3221 if (chkrng->lower >= nItem)
3222 chkrng->lower = max(min(chkrng->lower + delta, nUpper - 1), 0);
3223 if (chkrng->upper > nItem)
3224 chkrng->upper = max(min(chkrng->upper + delta, nUpper), 0);
3226 return TRUE;
3229 static BOOL ranges_add(RANGES ranges, RANGE range)
3231 RANGE srchrgn;
3232 INT index;
3234 TRACE("(%s)\n", debugrange(&range));
3235 ranges_check(ranges, "before add");
3237 /* try find overlapping regions first */
3238 srchrgn.lower = range.lower - 1;
3239 srchrgn.upper = range.upper + 1;
3240 index = DPA_Search(ranges->hdpa, &srchrgn, 0, ranges_cmp, 0, DPAS_SORTED);
3242 if (index == -1)
3244 RANGE *newrgn;
3246 TRACE("Adding new range\n");
3248 /* create the brand new range to insert */
3249 newrgn = Alloc(sizeof(RANGE));
3250 if(!newrgn) goto fail;
3251 *newrgn = range;
3253 /* figure out where to insert it */
3254 index = DPA_Search(ranges->hdpa, newrgn, 0, ranges_cmp, 0, DPAS_SORTED | DPAS_INSERTAFTER);
3255 TRACE("index=%d\n", index);
3256 if (index == -1) index = 0;
3258 /* and get it over with */
3259 if (DPA_InsertPtr(ranges->hdpa, index, newrgn) == -1)
3261 Free(newrgn);
3262 goto fail;
3265 else
3267 RANGE *chkrgn, *mrgrgn;
3268 INT fromindex, mergeindex;
3270 chkrgn = DPA_GetPtr(ranges->hdpa, index);
3271 TRACE("Merge with %s @%d\n", debugrange(chkrgn), index);
3273 chkrgn->lower = min(range.lower, chkrgn->lower);
3274 chkrgn->upper = max(range.upper, chkrgn->upper);
3276 TRACE("New range %s @%d\n", debugrange(chkrgn), index);
3278 /* merge now common ranges */
3279 fromindex = 0;
3280 srchrgn.lower = chkrgn->lower - 1;
3281 srchrgn.upper = chkrgn->upper + 1;
3285 mergeindex = DPA_Search(ranges->hdpa, &srchrgn, fromindex, ranges_cmp, 0, 0);
3286 if (mergeindex == -1) break;
3287 if (mergeindex == index)
3289 fromindex = index + 1;
3290 continue;
3293 TRACE("Merge with index %i\n", mergeindex);
3295 mrgrgn = DPA_GetPtr(ranges->hdpa, mergeindex);
3296 chkrgn->lower = min(chkrgn->lower, mrgrgn->lower);
3297 chkrgn->upper = max(chkrgn->upper, mrgrgn->upper);
3298 Free(mrgrgn);
3299 DPA_DeletePtr(ranges->hdpa, mergeindex);
3300 if (mergeindex < index) index --;
3301 } while(1);
3304 ranges_check(ranges, "after add");
3305 return TRUE;
3307 fail:
3308 ranges_check(ranges, "failed add");
3309 return FALSE;
3312 static BOOL ranges_del(RANGES ranges, RANGE range)
3314 RANGE *chkrgn;
3315 INT index;
3317 TRACE("(%s)\n", debugrange(&range));
3318 ranges_check(ranges, "before del");
3320 /* we don't use DPAS_SORTED here, since we need *
3321 * to find the first overlapping range */
3322 index = DPA_Search(ranges->hdpa, &range, 0, ranges_cmp, 0, 0);
3323 while(index != -1)
3325 chkrgn = DPA_GetPtr(ranges->hdpa, index);
3327 TRACE("Matches range %s @%d\n", debugrange(chkrgn), index);
3329 /* case 1: Same range */
3330 if ( (chkrgn->upper == range.upper) &&
3331 (chkrgn->lower == range.lower) )
3333 DPA_DeletePtr(ranges->hdpa, index);
3334 Free(chkrgn);
3335 break;
3337 /* case 2: engulf */
3338 else if ( (chkrgn->upper <= range.upper) &&
3339 (chkrgn->lower >= range.lower) )
3341 DPA_DeletePtr(ranges->hdpa, index);
3342 Free(chkrgn);
3344 /* case 3: overlap upper */
3345 else if ( (chkrgn->upper <= range.upper) &&
3346 (chkrgn->lower < range.lower) )
3348 chkrgn->upper = range.lower;
3350 /* case 4: overlap lower */
3351 else if ( (chkrgn->upper > range.upper) &&
3352 (chkrgn->lower >= range.lower) )
3354 chkrgn->lower = range.upper;
3355 break;
3357 /* case 5: fully internal */
3358 else
3360 RANGE *newrgn;
3362 if (!(newrgn = Alloc(sizeof(RANGE)))) goto fail;
3363 newrgn->lower = chkrgn->lower;
3364 newrgn->upper = range.lower;
3365 chkrgn->lower = range.upper;
3366 if (DPA_InsertPtr(ranges->hdpa, index, newrgn) == -1)
3368 Free(newrgn);
3369 goto fail;
3371 break;
3374 index = DPA_Search(ranges->hdpa, &range, index, ranges_cmp, 0, 0);
3377 ranges_check(ranges, "after del");
3378 return TRUE;
3380 fail:
3381 ranges_check(ranges, "failed del");
3382 return FALSE;
3385 /***
3386 * DESCRIPTION:
3387 * Removes all selection ranges
3389 * Parameters(s):
3390 * [I] infoPtr : valid pointer to the listview structure
3391 * [I] toSkip : item range to skip removing the selection
3393 * RETURNS:
3394 * SUCCESS : TRUE
3395 * FAILURE : FALSE
3397 static BOOL LISTVIEW_DeselectAllSkipItems(LISTVIEW_INFO *infoPtr, RANGES toSkip)
3399 LVITEMW lvItem;
3400 ITERATOR i;
3401 RANGES clone;
3403 TRACE("()\n");
3405 lvItem.state = 0;
3406 lvItem.stateMask = LVIS_SELECTED;
3408 /* need to clone the DPA because callbacks can change it */
3409 if (!(clone = ranges_clone(infoPtr->selectionRanges))) return FALSE;
3410 iterator_rangesitems(&i, ranges_diff(clone, toSkip));
3411 while(iterator_next(&i))
3412 LISTVIEW_SetItemState(infoPtr, i.nItem, &lvItem);
3413 /* note that the iterator destructor will free the cloned range */
3414 iterator_destroy(&i);
3416 return TRUE;
3419 static inline BOOL LISTVIEW_DeselectAllSkipItem(LISTVIEW_INFO *infoPtr, INT nItem)
3421 RANGES toSkip;
3423 if (!(toSkip = ranges_create(1))) return FALSE;
3424 if (nItem != -1) ranges_additem(toSkip, nItem);
3425 LISTVIEW_DeselectAllSkipItems(infoPtr, toSkip);
3426 ranges_destroy(toSkip);
3427 return TRUE;
3430 static inline BOOL LISTVIEW_DeselectAll(LISTVIEW_INFO *infoPtr)
3432 return LISTVIEW_DeselectAllSkipItem(infoPtr, -1);
3435 /***
3436 * DESCRIPTION:
3437 * Retrieves the number of items that are marked as selected.
3439 * PARAMETER(S):
3440 * [I] infoPtr : valid pointer to the listview structure
3442 * RETURN:
3443 * Number of items selected.
3445 static INT LISTVIEW_GetSelectedCount(const LISTVIEW_INFO *infoPtr)
3447 INT nSelectedCount = 0;
3449 if (infoPtr->uCallbackMask & LVIS_SELECTED)
3451 INT i;
3452 for (i = 0; i < infoPtr->nItemCount; i++)
3454 if (LISTVIEW_GetItemState(infoPtr, i, LVIS_SELECTED))
3455 nSelectedCount++;
3458 else
3459 nSelectedCount = ranges_itemcount(infoPtr->selectionRanges);
3461 TRACE("nSelectedCount=%d\n", nSelectedCount);
3462 return nSelectedCount;
3465 /***
3466 * DESCRIPTION:
3467 * Manages the item focus.
3469 * PARAMETER(S):
3470 * [I] infoPtr : valid pointer to the listview structure
3471 * [I] nItem : item index
3473 * RETURN:
3474 * TRUE : focused item changed
3475 * FALSE : focused item has NOT changed
3477 static inline BOOL LISTVIEW_SetItemFocus(LISTVIEW_INFO *infoPtr, INT nItem)
3479 INT oldFocus = infoPtr->nFocusedItem;
3480 LVITEMW lvItem;
3482 if (nItem == infoPtr->nFocusedItem) return FALSE;
3484 lvItem.state = nItem == -1 ? 0 : LVIS_FOCUSED;
3485 lvItem.stateMask = LVIS_FOCUSED;
3486 LISTVIEW_SetItemState(infoPtr, nItem == -1 ? infoPtr->nFocusedItem : nItem, &lvItem);
3488 return oldFocus != infoPtr->nFocusedItem;
3491 static INT shift_item(const LISTVIEW_INFO *infoPtr, INT nShiftItem, INT nItem, INT direction)
3493 if (nShiftItem < nItem) return nShiftItem;
3495 if (nShiftItem > nItem) return nShiftItem + direction;
3497 if (direction > 0) return nShiftItem + direction;
3499 return min(nShiftItem, infoPtr->nItemCount - 1);
3502 /* This function updates focus index.
3504 Parameters:
3505 focus : current focus index
3506 item : index of item to be added/removed
3507 direction : add/remove flag
3509 static void LISTVIEW_ShiftFocus(LISTVIEW_INFO *infoPtr, INT focus, INT item, INT direction)
3511 DWORD old_mask = infoPtr->notify_mask & NOTIFY_MASK_ITEM_CHANGE;
3513 infoPtr->notify_mask &= ~NOTIFY_MASK_ITEM_CHANGE;
3514 focus = shift_item(infoPtr, focus, item, direction);
3515 if (focus != infoPtr->nFocusedItem)
3516 LISTVIEW_SetItemFocus(infoPtr, focus);
3517 infoPtr->notify_mask |= old_mask;
3521 * DESCRIPTION:
3522 * Updates the various indices after an item has been inserted or deleted.
3524 * PARAMETER(S):
3525 * [I] infoPtr : valid pointer to the listview structure
3526 * [I] nItem : item index
3527 * [I] direction : Direction of shift, +1 or -1.
3529 * RETURN:
3530 * None
3532 static void LISTVIEW_ShiftIndices(LISTVIEW_INFO *infoPtr, INT nItem, INT direction)
3534 TRACE("Shifting %i, %i steps\n", nItem, direction);
3536 ranges_shift(infoPtr->selectionRanges, nItem, direction, infoPtr->nItemCount);
3537 assert(abs(direction) == 1);
3538 infoPtr->nSelectionMark = shift_item(infoPtr, infoPtr->nSelectionMark, nItem, direction);
3540 /* But we are not supposed to modify nHotItem! */
3544 * DESCRIPTION:
3545 * Adds a block of selections.
3547 * PARAMETER(S):
3548 * [I] infoPtr : valid pointer to the listview structure
3549 * [I] nItem : item index
3551 * RETURN:
3552 * Whether the window is still valid.
3554 static BOOL LISTVIEW_AddGroupSelection(LISTVIEW_INFO *infoPtr, INT nItem)
3556 INT nFirst = min(infoPtr->nSelectionMark, nItem);
3557 INT nLast = max(infoPtr->nSelectionMark, nItem);
3558 HWND hwndSelf = infoPtr->hwndSelf;
3559 NMLVODSTATECHANGE nmlv;
3560 DWORD old_mask;
3561 LVITEMW item;
3562 INT i;
3564 /* Temporarily disable change notification
3565 * If the control is LVS_OWNERDATA, we need to send
3566 * only one LVN_ODSTATECHANGED notification.
3567 * See MSDN documentation for LVN_ITEMCHANGED.
3569 old_mask = infoPtr->notify_mask & NOTIFY_MASK_ITEM_CHANGE;
3570 if (infoPtr->dwStyle & LVS_OWNERDATA)
3571 infoPtr->notify_mask &= ~NOTIFY_MASK_ITEM_CHANGE;
3573 if (nFirst == -1) nFirst = nItem;
3575 item.state = LVIS_SELECTED;
3576 item.stateMask = LVIS_SELECTED;
3578 for (i = nFirst; i <= nLast; i++)
3579 LISTVIEW_SetItemState(infoPtr,i,&item);
3581 ZeroMemory(&nmlv, sizeof(nmlv));
3582 nmlv.iFrom = nFirst;
3583 nmlv.iTo = nLast;
3584 nmlv.uOldState = 0;
3585 nmlv.uNewState = item.state;
3587 notify_hdr(infoPtr, LVN_ODSTATECHANGED, (LPNMHDR)&nmlv);
3588 if (!IsWindow(hwndSelf))
3589 return FALSE;
3590 infoPtr->notify_mask |= old_mask;
3591 return TRUE;
3595 /***
3596 * DESCRIPTION:
3597 * Sets a single group selection.
3599 * PARAMETER(S):
3600 * [I] infoPtr : valid pointer to the listview structure
3601 * [I] nItem : item index
3603 * RETURN:
3604 * None
3606 static void LISTVIEW_SetGroupSelection(LISTVIEW_INFO *infoPtr, INT nItem)
3608 RANGES selection;
3609 DWORD old_mask;
3610 LVITEMW item;
3611 ITERATOR i;
3613 if (!(selection = ranges_create(100))) return;
3615 item.state = LVIS_SELECTED;
3616 item.stateMask = LVIS_SELECTED;
3618 if ((infoPtr->uView == LV_VIEW_LIST) || (infoPtr->uView == LV_VIEW_DETAILS))
3620 if (infoPtr->nSelectionMark == -1)
3622 infoPtr->nSelectionMark = nItem;
3623 ranges_additem(selection, nItem);
3625 else
3627 RANGE sel;
3629 sel.lower = min(infoPtr->nSelectionMark, nItem);
3630 sel.upper = max(infoPtr->nSelectionMark, nItem) + 1;
3631 ranges_add(selection, sel);
3634 else
3636 RECT rcItem, rcSel, rcSelMark;
3637 POINT ptItem;
3639 rcItem.left = LVIR_BOUNDS;
3640 if (!LISTVIEW_GetItemRect(infoPtr, nItem, &rcItem)) {
3641 ranges_destroy (selection);
3642 return;
3644 rcSelMark.left = LVIR_BOUNDS;
3645 if (!LISTVIEW_GetItemRect(infoPtr, infoPtr->nSelectionMark, &rcSelMark)) {
3646 ranges_destroy (selection);
3647 return;
3649 UnionRect(&rcSel, &rcItem, &rcSelMark);
3650 iterator_frameditems(&i, infoPtr, &rcSel);
3651 while(iterator_next(&i))
3653 LISTVIEW_GetItemPosition(infoPtr, i.nItem, &ptItem);
3654 if (PtInRect(&rcSel, ptItem)) ranges_additem(selection, i.nItem);
3656 iterator_destroy(&i);
3659 /* disable per item notifications on LVS_OWNERDATA style
3660 FIXME: single LVN_ODSTATECHANGED should be used */
3661 old_mask = infoPtr->notify_mask & NOTIFY_MASK_ITEM_CHANGE;
3662 if (infoPtr->dwStyle & LVS_OWNERDATA)
3663 infoPtr->notify_mask &= ~NOTIFY_MASK_ITEM_CHANGE;
3665 LISTVIEW_DeselectAllSkipItems(infoPtr, selection);
3668 iterator_rangesitems(&i, selection);
3669 while(iterator_next(&i))
3670 LISTVIEW_SetItemState(infoPtr, i.nItem, &item);
3671 /* this will also destroy the selection */
3672 iterator_destroy(&i);
3674 infoPtr->notify_mask |= old_mask;
3675 LISTVIEW_SetItemFocus(infoPtr, nItem);
3678 /***
3679 * DESCRIPTION:
3680 * Sets a single selection.
3682 * PARAMETER(S):
3683 * [I] infoPtr : valid pointer to the listview structure
3684 * [I] nItem : item index
3686 * RETURN:
3687 * None
3689 static void LISTVIEW_SetSelection(LISTVIEW_INFO *infoPtr, INT nItem)
3691 LVITEMW lvItem;
3693 TRACE("nItem=%d\n", nItem);
3695 LISTVIEW_DeselectAllSkipItem(infoPtr, nItem);
3697 lvItem.state = LVIS_FOCUSED | LVIS_SELECTED;
3698 lvItem.stateMask = LVIS_FOCUSED | LVIS_SELECTED;
3699 LISTVIEW_SetItemState(infoPtr, nItem, &lvItem);
3701 infoPtr->nSelectionMark = nItem;
3704 /***
3705 * DESCRIPTION:
3706 * Set selection(s) with keyboard.
3708 * PARAMETER(S):
3709 * [I] infoPtr : valid pointer to the listview structure
3710 * [I] nItem : item index
3711 * [I] space : VK_SPACE code sent
3713 * RETURN:
3714 * SUCCESS : TRUE (needs to be repainted)
3715 * FAILURE : FALSE (nothing has changed)
3717 static BOOL LISTVIEW_KeySelection(LISTVIEW_INFO *infoPtr, INT nItem, BOOL space)
3719 /* FIXME: pass in the state */
3720 WORD wShift = GetKeyState(VK_SHIFT) & 0x8000;
3721 WORD wCtrl = GetKeyState(VK_CONTROL) & 0x8000;
3722 BOOL bResult = FALSE;
3724 TRACE("nItem=%d, wShift=%d, wCtrl=%d\n", nItem, wShift, wCtrl);
3725 if ((nItem >= 0) && (nItem < infoPtr->nItemCount))
3727 bResult = TRUE;
3729 if (infoPtr->dwStyle & LVS_SINGLESEL || (wShift == 0 && wCtrl == 0))
3730 LISTVIEW_SetSelection(infoPtr, nItem);
3731 else
3733 if (wShift)
3734 LISTVIEW_SetGroupSelection(infoPtr, nItem);
3735 else if (wCtrl)
3737 LVITEMW lvItem;
3738 lvItem.state = ~LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED);
3739 lvItem.stateMask = LVIS_SELECTED;
3740 if (space)
3742 LISTVIEW_SetItemState(infoPtr, nItem, &lvItem);
3743 if (lvItem.state & LVIS_SELECTED)
3744 infoPtr->nSelectionMark = nItem;
3746 bResult = LISTVIEW_SetItemFocus(infoPtr, nItem);
3749 LISTVIEW_EnsureVisible(infoPtr, nItem, FALSE);
3752 UpdateWindow(infoPtr->hwndSelf); /* update client area */
3753 return bResult;
3756 static BOOL LISTVIEW_GetItemAtPt(const LISTVIEW_INFO *infoPtr, LPLVITEMW lpLVItem, POINT pt)
3758 LVHITTESTINFO lvHitTestInfo;
3760 ZeroMemory(&lvHitTestInfo, sizeof(lvHitTestInfo));
3761 lvHitTestInfo.pt.x = pt.x;
3762 lvHitTestInfo.pt.y = pt.y;
3764 LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, FALSE);
3766 lpLVItem->mask = LVIF_PARAM;
3767 lpLVItem->iItem = lvHitTestInfo.iItem;
3768 lpLVItem->iSubItem = 0;
3770 return LISTVIEW_GetItemT(infoPtr, lpLVItem, TRUE);
3773 static inline BOOL LISTVIEW_IsHotTracking(const LISTVIEW_INFO *infoPtr)
3775 return ((infoPtr->dwLvExStyle & LVS_EX_TRACKSELECT) ||
3776 (infoPtr->dwLvExStyle & LVS_EX_ONECLICKACTIVATE) ||
3777 (infoPtr->dwLvExStyle & LVS_EX_TWOCLICKACTIVATE));
3780 /***
3781 * DESCRIPTION:
3782 * Called when the mouse is being actively tracked and has hovered for a specified
3783 * amount of time
3785 * PARAMETER(S):
3786 * [I] infoPtr : valid pointer to the listview structure
3787 * [I] fwKeys : key indicator
3788 * [I] x,y : mouse position
3790 * RETURN:
3791 * 0 if the message was processed, non-zero if there was an error
3793 * INFO:
3794 * LVS_EX_TRACKSELECT: An item is automatically selected when the cursor remains
3795 * over the item for a certain period of time.
3798 static LRESULT LISTVIEW_MouseHover(LISTVIEW_INFO *infoPtr, INT x, INT y)
3800 NMHDR hdr;
3802 if (notify_hdr(infoPtr, NM_HOVER, &hdr)) return 0;
3804 if (LISTVIEW_IsHotTracking(infoPtr))
3806 LVITEMW item;
3807 POINT pt;
3809 pt.x = x;
3810 pt.y = y;
3812 if (LISTVIEW_GetItemAtPt(infoPtr, &item, pt))
3813 LISTVIEW_SetSelection(infoPtr, item.iItem);
3815 SetFocus(infoPtr->hwndSelf);
3818 return 0;
3821 #define SCROLL_LEFT 0x1
3822 #define SCROLL_RIGHT 0x2
3823 #define SCROLL_UP 0x4
3824 #define SCROLL_DOWN 0x8
3826 /***
3827 * DESCRIPTION:
3828 * Utility routine to draw and highlight items within a marquee selection rectangle.
3830 * PARAMETER(S):
3831 * [I] infoPtr : valid pointer to the listview structure
3832 * [I] coords_orig : original co-ordinates of the cursor
3833 * [I] coords_offs : offsetted coordinates of the cursor
3834 * [I] offset : offset amount
3835 * [I] scroll : Bitmask of which directions we should scroll, if at all
3837 * RETURN:
3838 * None.
3840 static void LISTVIEW_MarqueeHighlight(LISTVIEW_INFO *infoPtr, const POINT *coords_orig,
3841 INT scroll)
3843 BOOL controlDown = FALSE;
3844 LVITEMW item;
3845 ITERATOR old_elems, new_elems;
3846 RECT rect;
3847 POINT coords_offs, offset;
3849 /* Ensure coordinates are within client bounds */
3850 coords_offs.x = max(min(coords_orig->x, infoPtr->rcList.right), 0);
3851 coords_offs.y = max(min(coords_orig->y, infoPtr->rcList.bottom), 0);
3853 /* Get offset */
3854 LISTVIEW_GetOrigin(infoPtr, &offset);
3856 /* Offset coordinates by the appropriate amount */
3857 coords_offs.x -= offset.x;
3858 coords_offs.y -= offset.y;
3860 if (coords_offs.x > infoPtr->marqueeOrigin.x)
3862 rect.left = infoPtr->marqueeOrigin.x;
3863 rect.right = coords_offs.x;
3865 else
3867 rect.left = coords_offs.x;
3868 rect.right = infoPtr->marqueeOrigin.x;
3871 if (coords_offs.y > infoPtr->marqueeOrigin.y)
3873 rect.top = infoPtr->marqueeOrigin.y;
3874 rect.bottom = coords_offs.y;
3876 else
3878 rect.top = coords_offs.y;
3879 rect.bottom = infoPtr->marqueeOrigin.y;
3882 /* Cancel out the old marquee rectangle and draw the new one */
3883 LISTVIEW_InvalidateRect(infoPtr, &infoPtr->marqueeDrawRect);
3885 /* Scroll by the appropriate distance if applicable - speed up scrolling as
3886 the cursor is further away */
3888 if ((scroll & SCROLL_LEFT) && (coords_orig->x <= 0))
3889 LISTVIEW_Scroll(infoPtr, coords_orig->x, 0);
3891 if ((scroll & SCROLL_RIGHT) && (coords_orig->x >= infoPtr->rcList.right))
3892 LISTVIEW_Scroll(infoPtr, (coords_orig->x - infoPtr->rcList.right), 0);
3894 if ((scroll & SCROLL_UP) && (coords_orig->y <= 0))
3895 LISTVIEW_Scroll(infoPtr, 0, coords_orig->y);
3897 if ((scroll & SCROLL_DOWN) && (coords_orig->y >= infoPtr->rcList.bottom))
3898 LISTVIEW_Scroll(infoPtr, 0, (coords_orig->y - infoPtr->rcList.bottom));
3900 iterator_frameditems_absolute(&old_elems, infoPtr, &infoPtr->marqueeRect);
3902 infoPtr->marqueeRect = rect;
3903 infoPtr->marqueeDrawRect = rect;
3904 OffsetRect(&infoPtr->marqueeDrawRect, offset.x, offset.y);
3906 iterator_frameditems_absolute(&new_elems, infoPtr, &infoPtr->marqueeRect);
3907 iterator_remove_common_items(&old_elems, &new_elems);
3909 /* Iterate over no longer selected items */
3910 while (iterator_next(&old_elems))
3912 if (old_elems.nItem > -1)
3914 if (LISTVIEW_GetItemState(infoPtr, old_elems.nItem, LVIS_SELECTED) == LVIS_SELECTED)
3915 item.state = 0;
3916 else
3917 item.state = LVIS_SELECTED;
3919 item.stateMask = LVIS_SELECTED;
3921 LISTVIEW_SetItemState(infoPtr, old_elems.nItem, &item);
3924 iterator_destroy(&old_elems);
3927 /* Iterate over newly selected items */
3928 if (GetKeyState(VK_CONTROL) & 0x8000)
3929 controlDown = TRUE;
3931 while (iterator_next(&new_elems))
3933 if (new_elems.nItem > -1)
3935 /* If CTRL is pressed, invert. If not, always select the item. */
3936 if ((controlDown) && (LISTVIEW_GetItemState(infoPtr, new_elems.nItem, LVIS_SELECTED)))
3937 item.state = 0;
3938 else
3939 item.state = LVIS_SELECTED;
3941 item.stateMask = LVIS_SELECTED;
3943 LISTVIEW_SetItemState(infoPtr, new_elems.nItem, &item);
3946 iterator_destroy(&new_elems);
3948 LISTVIEW_InvalidateRect(infoPtr, &infoPtr->marqueeDrawRect);
3951 /***
3952 * DESCRIPTION:
3953 * Called when we are in a marquee selection that involves scrolling the listview (ie,
3954 * the cursor is outside the bounds of the client area). This is a TIMERPROC.
3956 * PARAMETER(S):
3957 * [I] hwnd : Handle to the listview
3958 * [I] uMsg : WM_TIMER (ignored)
3959 * [I] idEvent : The timer ID interpreted as a pointer to a LISTVIEW_INFO struct
3960 * [I] dwTimer : The elapsed time (ignored)
3962 * RETURN:
3963 * None.
3965 static VOID CALLBACK LISTVIEW_ScrollTimer(HWND hWnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime)
3967 LISTVIEW_INFO *infoPtr;
3968 SCROLLINFO scrollInfo;
3969 POINT coords;
3970 INT scroll = 0;
3972 infoPtr = (LISTVIEW_INFO *) idEvent;
3974 if (!infoPtr)
3975 return;
3977 /* Get the current cursor position and convert to client coordinates */
3978 GetCursorPos(&coords);
3979 ScreenToClient(hWnd, &coords);
3981 scrollInfo.cbSize = sizeof(SCROLLINFO);
3982 scrollInfo.fMask = SIF_ALL;
3984 /* Work out in which directions we can scroll */
3985 if (GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo))
3987 if (scrollInfo.nPos != scrollInfo.nMin)
3988 scroll |= SCROLL_UP;
3990 if (((scrollInfo.nPage + scrollInfo.nPos) - 1) != scrollInfo.nMax)
3991 scroll |= SCROLL_DOWN;
3994 if (GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo))
3996 if (scrollInfo.nPos != scrollInfo.nMin)
3997 scroll |= SCROLL_LEFT;
3999 if (((scrollInfo.nPage + scrollInfo.nPos) - 1) != scrollInfo.nMax)
4000 scroll |= SCROLL_RIGHT;
4003 if (((coords.x <= 0) && (scroll & SCROLL_LEFT)) ||
4004 ((coords.y <= 0) && (scroll & SCROLL_UP)) ||
4005 ((coords.x >= infoPtr->rcList.right) && (scroll & SCROLL_RIGHT)) ||
4006 ((coords.y >= infoPtr->rcList.bottom) && (scroll & SCROLL_DOWN)))
4008 LISTVIEW_MarqueeHighlight(infoPtr, &coords, scroll);
4012 /***
4013 * DESCRIPTION:
4014 * Called whenever WM_MOUSEMOVE is received.
4016 * PARAMETER(S):
4017 * [I] infoPtr : valid pointer to the listview structure
4018 * [I] fwKeys : key indicator
4019 * [I] x,y : mouse position
4021 * RETURN:
4022 * 0 if the message is processed, non-zero if there was an error
4024 static LRESULT LISTVIEW_MouseMove(LISTVIEW_INFO *infoPtr, WORD fwKeys, INT x, INT y)
4026 LVHITTESTINFO ht;
4027 RECT rect;
4028 POINT pt;
4030 pt.x = x;
4031 pt.y = y;
4033 if (!(fwKeys & MK_LBUTTON))
4034 infoPtr->bLButtonDown = FALSE;
4036 if (infoPtr->bLButtonDown)
4038 rect.left = rect.right = infoPtr->ptClickPos.x;
4039 rect.top = rect.bottom = infoPtr->ptClickPos.y;
4041 InflateRect(&rect, GetSystemMetrics(SM_CXDRAG), GetSystemMetrics(SM_CYDRAG));
4043 if (infoPtr->bMarqueeSelect)
4045 /* Enable the timer if we're going outside our bounds, in case the user doesn't
4046 move the mouse again */
4048 if ((x <= 0) || (y <= 0) || (x >= infoPtr->rcList.right) ||
4049 (y >= infoPtr->rcList.bottom))
4051 if (!infoPtr->bScrolling)
4053 infoPtr->bScrolling = TRUE;
4054 SetTimer(infoPtr->hwndSelf, (UINT_PTR) infoPtr, 1, LISTVIEW_ScrollTimer);
4057 else
4059 infoPtr->bScrolling = FALSE;
4060 KillTimer(infoPtr->hwndSelf, (UINT_PTR) infoPtr);
4063 LISTVIEW_MarqueeHighlight(infoPtr, &pt, 0);
4064 return 0;
4067 ht.pt = pt;
4068 LISTVIEW_HitTest(infoPtr, &ht, TRUE, TRUE);
4070 /* reset item marker */
4071 if (infoPtr->nLButtonDownItem != ht.iItem)
4072 infoPtr->nLButtonDownItem = -1;
4074 if (!PtInRect(&rect, pt))
4076 /* this path covers the following:
4077 1. WM_LBUTTONDOWN over selected item (sets focus on it)
4078 2. change focus with keys
4079 3. move mouse over item from step 1 selects it and moves focus on it */
4080 if (infoPtr->nLButtonDownItem != -1 &&
4081 !LISTVIEW_GetItemState(infoPtr, infoPtr->nLButtonDownItem, LVIS_SELECTED))
4083 LVITEMW lvItem;
4085 lvItem.state = LVIS_FOCUSED | LVIS_SELECTED;
4086 lvItem.stateMask = LVIS_FOCUSED | LVIS_SELECTED;
4088 LISTVIEW_SetItemState(infoPtr, infoPtr->nLButtonDownItem, &lvItem);
4089 infoPtr->nLButtonDownItem = -1;
4092 if (!infoPtr->bDragging)
4094 ht.pt = infoPtr->ptClickPos;
4095 LISTVIEW_HitTest(infoPtr, &ht, TRUE, TRUE);
4097 /* If the click is outside the range of an item, begin a
4098 highlight. If not, begin an item drag. */
4099 if (ht.iItem == -1)
4101 NMHDR hdr;
4103 /* If we're allowing multiple selections, send notification.
4104 If return value is non-zero, cancel. */
4105 if (!(infoPtr->dwStyle & LVS_SINGLESEL) && (notify_hdr(infoPtr, LVN_MARQUEEBEGIN, &hdr) == 0))
4107 /* Store the absolute coordinates of the click */
4108 POINT offset;
4109 LISTVIEW_GetOrigin(infoPtr, &offset);
4111 infoPtr->marqueeOrigin.x = infoPtr->ptClickPos.x - offset.x;
4112 infoPtr->marqueeOrigin.y = infoPtr->ptClickPos.y - offset.y;
4114 /* Begin selection and capture mouse */
4115 infoPtr->bMarqueeSelect = TRUE;
4116 SetCapture(infoPtr->hwndSelf);
4119 else
4121 NMLISTVIEW nmlv;
4123 ZeroMemory(&nmlv, sizeof(nmlv));
4124 nmlv.iItem = ht.iItem;
4125 nmlv.ptAction = infoPtr->ptClickPos;
4127 notify_listview(infoPtr, LVN_BEGINDRAG, &nmlv);
4128 infoPtr->bDragging = TRUE;
4132 return 0;
4136 /* see if we are supposed to be tracking mouse hovering */
4137 if (LISTVIEW_IsHotTracking(infoPtr)) {
4138 TRACKMOUSEEVENT trackinfo;
4139 DWORD flags;
4141 trackinfo.cbSize = sizeof(TRACKMOUSEEVENT);
4142 trackinfo.dwFlags = TME_QUERY;
4144 /* see if we are already tracking this hwnd */
4145 _TrackMouseEvent(&trackinfo);
4147 flags = TME_LEAVE;
4148 if(infoPtr->dwLvExStyle & LVS_EX_TRACKSELECT)
4149 flags |= TME_HOVER;
4151 if((trackinfo.dwFlags & flags) != flags || trackinfo.hwndTrack != infoPtr->hwndSelf) {
4152 trackinfo.dwFlags = flags;
4153 trackinfo.dwHoverTime = infoPtr->dwHoverTime;
4154 trackinfo.hwndTrack = infoPtr->hwndSelf;
4156 /* call TRACKMOUSEEVENT so we receive WM_MOUSEHOVER messages */
4157 _TrackMouseEvent(&trackinfo);
4161 return 0;
4165 /***
4166 * Tests whether the item is assignable to a list with style lStyle
4168 static inline BOOL is_assignable_item(const LVITEMW *lpLVItem, LONG lStyle)
4170 if ( (lpLVItem->mask & LVIF_TEXT) &&
4171 (lpLVItem->pszText == LPSTR_TEXTCALLBACKW) &&
4172 (lStyle & (LVS_SORTASCENDING | LVS_SORTDESCENDING)) ) return FALSE;
4174 return TRUE;
4178 /***
4179 * DESCRIPTION:
4180 * Helper for LISTVIEW_SetItemT and LISTVIEW_InsertItemT: sets item attributes.
4182 * PARAMETER(S):
4183 * [I] infoPtr : valid pointer to the listview structure
4184 * [I] lpLVItem : valid pointer to new item attributes
4185 * [I] isNew : the item being set is being inserted
4186 * [I] isW : TRUE if lpLVItem is Unicode, FALSE if it's ANSI
4187 * [O] bChanged : will be set to TRUE if the item really changed
4189 * RETURN:
4190 * SUCCESS : TRUE
4191 * FAILURE : FALSE
4193 static BOOL set_main_item(LISTVIEW_INFO *infoPtr, const LVITEMW *lpLVItem, BOOL isNew, BOOL isW, BOOL *bChanged)
4195 ITEM_INFO *lpItem;
4196 NMLISTVIEW nmlv;
4197 UINT uChanged = 0;
4198 LVITEMW item;
4199 /* stateMask is ignored for LVM_INSERTITEM */
4200 UINT stateMask = isNew ? ~0 : lpLVItem->stateMask;
4202 TRACE("()\n");
4204 assert(lpLVItem->iItem >= 0 && lpLVItem->iItem < infoPtr->nItemCount);
4206 if (lpLVItem->mask == 0) return TRUE;
4208 if (infoPtr->dwStyle & LVS_OWNERDATA)
4210 /* a virtual listview only stores selection and focus */
4211 if (lpLVItem->mask & ~LVIF_STATE)
4212 return FALSE;
4213 lpItem = NULL;
4215 else
4217 HDPA hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, lpLVItem->iItem);
4218 lpItem = DPA_GetPtr(hdpaSubItems, 0);
4219 assert (lpItem);
4222 /* we need to get the lParam and state of the item */
4223 item.iItem = lpLVItem->iItem;
4224 item.iSubItem = lpLVItem->iSubItem;
4225 item.mask = LVIF_STATE | LVIF_PARAM;
4226 item.stateMask = (infoPtr->dwStyle & LVS_OWNERDATA) ? LVIS_FOCUSED | LVIS_SELECTED : ~0;
4228 item.state = 0;
4229 item.lParam = 0;
4230 if (!isNew && !LISTVIEW_GetItemW(infoPtr, &item)) return FALSE;
4232 TRACE("oldState=%x, newState=%x\n", item.state, lpLVItem->state);
4233 /* determine what fields will change */
4234 if ((lpLVItem->mask & LVIF_STATE) && ((item.state ^ lpLVItem->state) & stateMask & ~infoPtr->uCallbackMask))
4235 uChanged |= LVIF_STATE;
4237 if ((lpLVItem->mask & LVIF_IMAGE) && (lpItem->hdr.iImage != lpLVItem->iImage))
4238 uChanged |= LVIF_IMAGE;
4240 if ((lpLVItem->mask & LVIF_PARAM) && (lpItem->lParam != lpLVItem->lParam))
4241 uChanged |= LVIF_PARAM;
4243 if ((lpLVItem->mask & LVIF_INDENT) && (lpItem->iIndent != lpLVItem->iIndent))
4244 uChanged |= LVIF_INDENT;
4246 if ((lpLVItem->mask & LVIF_TEXT) && textcmpWT(lpItem->hdr.pszText, lpLVItem->pszText, isW))
4247 uChanged |= LVIF_TEXT;
4249 TRACE("change mask=0x%x\n", uChanged);
4251 memset(&nmlv, 0, sizeof(NMLISTVIEW));
4252 nmlv.iItem = lpLVItem->iItem;
4253 if (lpLVItem->mask & LVIF_STATE)
4255 nmlv.uNewState = (item.state & ~stateMask) | (lpLVItem->state & stateMask);
4256 nmlv.uOldState = item.state;
4258 nmlv.uChanged = uChanged ? uChanged : lpLVItem->mask;
4259 nmlv.lParam = item.lParam;
4261 /* Send LVN_ITEMCHANGING notification, if the item is not being inserted
4262 and we are _NOT_ virtual (LVS_OWNERDATA), and change notifications
4263 are enabled. Even nothing really changed we still need to send this,
4264 in this case uChanged mask is just set to passed item mask. */
4265 if (lpItem && !isNew && (infoPtr->notify_mask & NOTIFY_MASK_ITEM_CHANGE))
4267 HWND hwndSelf = infoPtr->hwndSelf;
4269 if (notify_listview(infoPtr, LVN_ITEMCHANGING, &nmlv))
4270 return FALSE;
4271 if (!IsWindow(hwndSelf))
4272 return FALSE;
4275 /* When item is inserted we need to shift existing focus index if new item has lower index. */
4276 if (isNew && (stateMask & ~infoPtr->uCallbackMask & LVIS_FOCUSED) &&
4277 /* this means we won't hit a focus change path later */
4278 ((uChanged & LVIF_STATE) == 0 || (!(lpLVItem->state & LVIS_FOCUSED) && (infoPtr->nFocusedItem != lpLVItem->iItem))))
4280 if (infoPtr->nFocusedItem != -1 && (lpLVItem->iItem <= infoPtr->nFocusedItem))
4281 infoPtr->nFocusedItem++;
4284 if (!uChanged) return TRUE;
4285 *bChanged = TRUE;
4287 /* copy information */
4288 if (lpLVItem->mask & LVIF_TEXT)
4289 textsetptrT(&lpItem->hdr.pszText, lpLVItem->pszText, isW);
4291 if (lpLVItem->mask & LVIF_IMAGE)
4292 lpItem->hdr.iImage = lpLVItem->iImage;
4294 if (lpLVItem->mask & LVIF_PARAM)
4295 lpItem->lParam = lpLVItem->lParam;
4297 if (lpLVItem->mask & LVIF_INDENT)
4298 lpItem->iIndent = lpLVItem->iIndent;
4300 if (uChanged & LVIF_STATE)
4302 if (lpItem && (stateMask & ~infoPtr->uCallbackMask))
4304 lpItem->state &= ~stateMask;
4305 lpItem->state |= (lpLVItem->state & stateMask);
4307 if (lpLVItem->state & stateMask & ~infoPtr->uCallbackMask & LVIS_SELECTED)
4309 if (infoPtr->dwStyle & LVS_SINGLESEL) LISTVIEW_DeselectAllSkipItem(infoPtr, lpLVItem->iItem);
4310 ranges_additem(infoPtr->selectionRanges, lpLVItem->iItem);
4312 else if (stateMask & LVIS_SELECTED)
4314 ranges_delitem(infoPtr->selectionRanges, lpLVItem->iItem);
4316 /* If we are asked to change focus, and we manage it, do it.
4317 It's important to have all new item data stored at this point,
4318 because changing existing focus could result in a redrawing operation,
4319 which in turn could ask for disp data, application should see all data
4320 for inserted item when processing LVN_GETDISPINFO.
4322 The way this works application will see nested item change notifications -
4323 changed item notifications interrupted by ones from item losing focus. */
4324 if (stateMask & ~infoPtr->uCallbackMask & LVIS_FOCUSED)
4326 if (lpLVItem->state & LVIS_FOCUSED)
4328 /* update selection mark */
4329 if (infoPtr->nFocusedItem == -1 && infoPtr->nSelectionMark == -1)
4330 infoPtr->nSelectionMark = lpLVItem->iItem;
4332 if (infoPtr->nFocusedItem != -1)
4334 /* remove current focus */
4335 item.mask = LVIF_STATE;
4336 item.state = 0;
4337 item.stateMask = LVIS_FOCUSED;
4339 /* recurse with redrawing an item */
4340 LISTVIEW_SetItemState(infoPtr, infoPtr->nFocusedItem, &item);
4343 infoPtr->nFocusedItem = lpLVItem->iItem;
4344 LISTVIEW_EnsureVisible(infoPtr, lpLVItem->iItem, infoPtr->uView == LV_VIEW_LIST);
4346 else if (infoPtr->nFocusedItem == lpLVItem->iItem)
4348 infoPtr->nFocusedItem = -1;
4353 /* if we're inserting the item, we're done */
4354 if (isNew) return TRUE;
4356 /* send LVN_ITEMCHANGED notification */
4357 if (lpLVItem->mask & LVIF_PARAM) nmlv.lParam = lpLVItem->lParam;
4358 if (infoPtr->notify_mask & NOTIFY_MASK_ITEM_CHANGE)
4359 notify_listview(infoPtr, LVN_ITEMCHANGED, &nmlv);
4361 return TRUE;
4364 /***
4365 * DESCRIPTION:
4366 * Helper for LISTVIEW_{Set,Insert}ItemT *only*: sets subitem attributes.
4368 * PARAMETER(S):
4369 * [I] infoPtr : valid pointer to the listview structure
4370 * [I] lpLVItem : valid pointer to new subitem attributes
4371 * [I] isW : TRUE if lpLVItem is Unicode, FALSE if it's ANSI
4372 * [O] bChanged : will be set to TRUE if the item really changed
4374 * RETURN:
4375 * SUCCESS : TRUE
4376 * FAILURE : FALSE
4378 static BOOL set_sub_item(const LISTVIEW_INFO *infoPtr, const LVITEMW *lpLVItem, BOOL isW, BOOL *bChanged)
4380 HDPA hdpaSubItems;
4381 SUBITEM_INFO *lpSubItem;
4383 /* we do not support subitems for virtual listviews */
4384 if (infoPtr->dwStyle & LVS_OWNERDATA) return FALSE;
4386 /* set subitem only if column is present */
4387 if (lpLVItem->iSubItem >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return FALSE;
4389 /* First do some sanity checks */
4390 /* The LVIF_STATE flag is valid for subitems, but does not appear to be
4391 particularly useful. We currently do not actually do anything with
4392 the flag on subitems.
4394 if (lpLVItem->mask & ~(LVIF_TEXT | LVIF_IMAGE | LVIF_STATE | LVIF_DI_SETITEM)) return FALSE;
4395 if (!(lpLVItem->mask & (LVIF_TEXT | LVIF_IMAGE | LVIF_STATE))) return TRUE;
4397 /* get the subitem structure, and create it if not there */
4398 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, lpLVItem->iItem);
4399 assert (hdpaSubItems);
4401 lpSubItem = LISTVIEW_GetSubItemPtr(hdpaSubItems, lpLVItem->iSubItem);
4402 if (!lpSubItem)
4404 SUBITEM_INFO *tmpSubItem;
4405 INT i;
4407 lpSubItem = Alloc(sizeof(SUBITEM_INFO));
4408 if (!lpSubItem) return FALSE;
4409 /* we could binary search here, if need be...*/
4410 for (i = 1; i < DPA_GetPtrCount(hdpaSubItems); i++)
4412 tmpSubItem = DPA_GetPtr(hdpaSubItems, i);
4413 if (tmpSubItem->iSubItem > lpLVItem->iSubItem) break;
4415 if (DPA_InsertPtr(hdpaSubItems, i, lpSubItem) == -1)
4417 Free(lpSubItem);
4418 return FALSE;
4420 lpSubItem->iSubItem = lpLVItem->iSubItem;
4421 lpSubItem->hdr.iImage = I_IMAGECALLBACK;
4422 *bChanged = TRUE;
4425 if ((lpLVItem->mask & LVIF_IMAGE) && (lpSubItem->hdr.iImage != lpLVItem->iImage))
4427 lpSubItem->hdr.iImage = lpLVItem->iImage;
4428 *bChanged = TRUE;
4431 if ((lpLVItem->mask & LVIF_TEXT) && textcmpWT(lpSubItem->hdr.pszText, lpLVItem->pszText, isW))
4433 textsetptrT(&lpSubItem->hdr.pszText, lpLVItem->pszText, isW);
4434 *bChanged = TRUE;
4437 return TRUE;
4440 /***
4441 * DESCRIPTION:
4442 * Sets item attributes.
4444 * PARAMETER(S):
4445 * [I] infoPtr : valid pointer to the listview structure
4446 * [I] lpLVItem : new item attributes
4447 * [I] isW : TRUE if lpLVItem is Unicode, FALSE if it's ANSI
4449 * RETURN:
4450 * SUCCESS : TRUE
4451 * FAILURE : FALSE
4453 static BOOL LISTVIEW_SetItemT(LISTVIEW_INFO *infoPtr, LVITEMW *lpLVItem, BOOL isW)
4455 HWND hwndSelf = infoPtr->hwndSelf;
4456 LPWSTR pszText = NULL;
4457 BOOL bResult, bChanged = FALSE;
4458 RECT oldItemArea;
4460 TRACE("(lpLVItem=%s, isW=%d)\n", debuglvitem_t(lpLVItem, isW), isW);
4462 if (!lpLVItem || lpLVItem->iItem < 0 || lpLVItem->iItem >= infoPtr->nItemCount)
4463 return FALSE;
4465 /* Store old item area */
4466 LISTVIEW_GetItemBox(infoPtr, lpLVItem->iItem, &oldItemArea);
4468 /* For efficiency, we transform the lpLVItem->pszText to Unicode here */
4469 if ((lpLVItem->mask & LVIF_TEXT) && is_text(lpLVItem->pszText))
4471 pszText = lpLVItem->pszText;
4472 lpLVItem->pszText = textdupTtoW(lpLVItem->pszText, isW);
4475 /* actually set the fields */
4476 if (!is_assignable_item(lpLVItem, infoPtr->dwStyle)) return FALSE;
4478 if (lpLVItem->iSubItem)
4479 bResult = set_sub_item(infoPtr, lpLVItem, TRUE, &bChanged);
4480 else
4481 bResult = set_main_item(infoPtr, lpLVItem, FALSE, TRUE, &bChanged);
4482 if (!IsWindow(hwndSelf))
4483 return FALSE;
4485 /* redraw item, if necessary */
4486 if (bChanged && !infoPtr->bIsDrawing)
4488 /* this little optimization eliminates some nasty flicker */
4489 if ( infoPtr->uView == LV_VIEW_DETAILS && !(infoPtr->dwStyle & LVS_OWNERDRAWFIXED) &&
4490 !(infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT) &&
4491 lpLVItem->iSubItem > 0 && lpLVItem->iSubItem <= DPA_GetPtrCount(infoPtr->hdpaColumns) )
4492 LISTVIEW_InvalidateSubItem(infoPtr, lpLVItem->iItem, lpLVItem->iSubItem);
4493 else
4495 LISTVIEW_InvalidateRect(infoPtr, &oldItemArea);
4496 LISTVIEW_InvalidateItem(infoPtr, lpLVItem->iItem);
4499 /* restore text */
4500 if (pszText)
4502 textfreeT(lpLVItem->pszText, isW);
4503 lpLVItem->pszText = pszText;
4506 return bResult;
4509 /***
4510 * DESCRIPTION:
4511 * Retrieves the index of the item at coordinate (0, 0) of the client area.
4513 * PARAMETER(S):
4514 * [I] infoPtr : valid pointer to the listview structure
4516 * RETURN:
4517 * item index
4519 static INT LISTVIEW_GetTopIndex(const LISTVIEW_INFO *infoPtr)
4521 INT nItem = 0;
4522 SCROLLINFO scrollInfo;
4524 scrollInfo.cbSize = sizeof(SCROLLINFO);
4525 scrollInfo.fMask = SIF_POS;
4527 if (infoPtr->uView == LV_VIEW_LIST)
4529 if (GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo))
4530 nItem = scrollInfo.nPos * LISTVIEW_GetCountPerColumn(infoPtr);
4532 else if (infoPtr->uView == LV_VIEW_DETAILS)
4534 if (GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo))
4535 nItem = scrollInfo.nPos;
4537 else
4539 if (GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo))
4540 nItem = LISTVIEW_GetCountPerRow(infoPtr) * (scrollInfo.nPos / infoPtr->nItemHeight);
4543 TRACE("nItem=%d\n", nItem);
4545 return nItem;
4549 /***
4550 * DESCRIPTION:
4551 * Erases the background of the given rectangle
4553 * PARAMETER(S):
4554 * [I] infoPtr : valid pointer to the listview structure
4555 * [I] hdc : device context handle
4556 * [I] lprcBox : clipping rectangle
4558 * RETURN:
4559 * Success: TRUE
4560 * Failure: FALSE
4562 static inline BOOL LISTVIEW_FillBkgnd(const LISTVIEW_INFO *infoPtr, HDC hdc, const RECT *lprcBox)
4564 if (!infoPtr->hBkBrush) return FALSE;
4566 TRACE("(hdc=%p, lprcBox=%s, hBkBrush=%p)\n", hdc, wine_dbgstr_rect(lprcBox), infoPtr->hBkBrush);
4568 return FillRect(hdc, lprcBox, infoPtr->hBkBrush);
4571 /* Draw main item or subitem */
4572 static void LISTVIEW_DrawItemPart(LISTVIEW_INFO *infoPtr, LVITEMW *item, const NMLVCUSTOMDRAW *nmlvcd, const POINT *pos)
4574 RECT rcSelect, rcLabel, rcBox, rcStateIcon, rcIcon;
4575 const RECT *background;
4576 HIMAGELIST himl;
4577 UINT format;
4578 RECT *focus;
4580 /* now check if we need to update the focus rectangle */
4581 focus = infoPtr->bFocus && (item->state & LVIS_FOCUSED) ? &infoPtr->rcFocus : 0;
4582 if (!focus) item->state &= ~LVIS_FOCUSED;
4584 LISTVIEW_GetItemMetrics(infoPtr, item, &rcBox, &rcSelect, &rcIcon, &rcStateIcon, &rcLabel);
4585 OffsetRect(&rcBox, pos->x, pos->y);
4586 OffsetRect(&rcSelect, pos->x, pos->y);
4587 OffsetRect(&rcIcon, pos->x, pos->y);
4588 OffsetRect(&rcStateIcon, pos->x, pos->y);
4589 OffsetRect(&rcLabel, pos->x, pos->y);
4590 TRACE("%d: box=%s, select=%s, icon=%s. label=%s\n", item->iSubItem,
4591 wine_dbgstr_rect(&rcBox), wine_dbgstr_rect(&rcSelect),
4592 wine_dbgstr_rect(&rcIcon), wine_dbgstr_rect(&rcLabel));
4594 /* FIXME: temporary hack */
4595 rcSelect.left = rcLabel.left;
4597 if (infoPtr->uView == LV_VIEW_DETAILS && item->iSubItem == 0)
4599 if (!(infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT))
4600 OffsetRect(&rcSelect, LISTVIEW_GetColumnInfo(infoPtr, 0)->rcHeader.left, 0);
4601 OffsetRect(&rcIcon, LISTVIEW_GetColumnInfo(infoPtr, 0)->rcHeader.left, 0);
4602 OffsetRect(&rcStateIcon, LISTVIEW_GetColumnInfo(infoPtr, 0)->rcHeader.left, 0);
4603 OffsetRect(&rcLabel, LISTVIEW_GetColumnInfo(infoPtr, 0)->rcHeader.left, 0);
4606 /* in icon mode, the label rect is really what we want to draw the
4607 * background for */
4608 /* in detail mode, we want to paint background for label rect when
4609 * item is not selected or listview has full row select; otherwise paint
4610 * background for text only */
4611 if ( infoPtr->uView == LV_VIEW_ICON ||
4612 (infoPtr->uView == LV_VIEW_DETAILS && (!(item->state & LVIS_SELECTED) ||
4613 (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT))))
4614 background = &rcLabel;
4615 else
4616 background = &rcSelect;
4618 if (nmlvcd->clrTextBk != CLR_NONE)
4619 ExtTextOutW(nmlvcd->nmcd.hdc, background->left, background->top, ETO_OPAQUE, background, NULL, 0, NULL);
4621 if (item->state & LVIS_FOCUSED)
4623 if (infoPtr->uView == LV_VIEW_DETAILS)
4625 if (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT)
4627 /* we have to update left focus bound too if item isn't in leftmost column
4628 and reduce right box bound */
4629 if (DPA_GetPtrCount(infoPtr->hdpaColumns) > 0)
4631 INT leftmost;
4633 if ((leftmost = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX, 0, 0)))
4635 INT Originx = pos->x - LISTVIEW_GetColumnInfo(infoPtr, leftmost)->rcHeader.left;
4636 INT rightmost = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX,
4637 DPA_GetPtrCount(infoPtr->hdpaColumns) - 1, 0);
4639 rcBox.right = LISTVIEW_GetColumnInfo(infoPtr, rightmost)->rcHeader.right + Originx;
4640 rcSelect.left = LISTVIEW_GetColumnInfo(infoPtr, leftmost)->rcHeader.left + Originx;
4643 rcSelect.right = rcBox.right;
4645 infoPtr->rcFocus = rcSelect;
4647 else
4648 infoPtr->rcFocus = rcLabel;
4651 /* state icons */
4652 if (infoPtr->himlState && STATEIMAGEINDEX(item->state) && (item->iSubItem == 0))
4654 UINT stateimage = STATEIMAGEINDEX(item->state);
4655 if (stateimage)
4657 TRACE("stateimage=%d\n", stateimage);
4658 ImageList_Draw(infoPtr->himlState, stateimage-1, nmlvcd->nmcd.hdc, rcStateIcon.left, rcStateIcon.top, ILD_NORMAL);
4662 /* item icons */
4663 himl = (infoPtr->uView == LV_VIEW_ICON ? infoPtr->himlNormal : infoPtr->himlSmall);
4664 if (himl && item->iImage >= 0 && !IsRectEmpty(&rcIcon))
4666 UINT style;
4668 TRACE("iImage=%d\n", item->iImage);
4670 if (item->state & (LVIS_SELECTED | LVIS_CUT) && infoPtr->bFocus)
4671 style = ILD_SELECTED;
4672 else
4673 style = ILD_NORMAL;
4675 ImageList_DrawEx(himl, item->iImage, nmlvcd->nmcd.hdc, rcIcon.left, rcIcon.top,
4676 rcIcon.right - rcIcon.left, rcIcon.bottom - rcIcon.top, infoPtr->clrBk,
4677 item->state & LVIS_CUT ? RGB(255, 255, 255) : CLR_DEFAULT,
4678 style | (item->state & LVIS_OVERLAYMASK));
4681 /* Don't bother painting item being edited */
4682 if (infoPtr->hwndEdit && item->iItem == infoPtr->nEditLabelItem && item->iSubItem == 0) return;
4684 /* figure out the text drawing flags */
4685 format = (infoPtr->uView == LV_VIEW_ICON ? (focus ? LV_FL_DT_FLAGS : LV_ML_DT_FLAGS) : LV_SL_DT_FLAGS);
4686 if (infoPtr->uView == LV_VIEW_ICON)
4687 format = (focus ? LV_FL_DT_FLAGS : LV_ML_DT_FLAGS);
4688 else if (item->iSubItem)
4690 switch (LISTVIEW_GetColumnInfo(infoPtr, item->iSubItem)->fmt & LVCFMT_JUSTIFYMASK)
4692 case LVCFMT_RIGHT: format |= DT_RIGHT; break;
4693 case LVCFMT_CENTER: format |= DT_CENTER; break;
4694 default: format |= DT_LEFT;
4697 if (!(format & (DT_RIGHT | DT_CENTER)))
4699 if (himl && item->iImage >= 0 && !IsRectEmpty(&rcIcon)) rcLabel.left += IMAGE_PADDING;
4700 else rcLabel.left += LABEL_HOR_PADDING;
4702 else if (format & DT_RIGHT) rcLabel.right -= LABEL_HOR_PADDING;
4704 /* for GRIDLINES reduce the bottom so the text formats correctly */
4705 if (infoPtr->uView == LV_VIEW_DETAILS && infoPtr->dwLvExStyle & LVS_EX_GRIDLINES)
4706 rcLabel.bottom--;
4708 DrawTextW(nmlvcd->nmcd.hdc, item->pszText, -1, &rcLabel, format);
4711 /***
4712 * DESCRIPTION:
4713 * Draws an item.
4715 * PARAMETER(S):
4716 * [I] infoPtr : valid pointer to the listview structure
4717 * [I] hdc : device context handle
4718 * [I] nItem : item index
4719 * [I] nSubItem : subitem index
4720 * [I] pos : item position in client coordinates
4721 * [I] cdmode : custom draw mode
4723 * RETURN:
4724 * Success: TRUE
4725 * Failure: FALSE
4727 static BOOL LISTVIEW_DrawItem(LISTVIEW_INFO *infoPtr, HDC hdc, INT nItem, ITERATOR *subitems, POINT pos, DWORD cdmode)
4729 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
4730 static WCHAR callbackW[] = L"(callback)";
4731 DWORD cdsubitemmode = CDRF_DODEFAULT;
4732 RECT *focus, rcBox;
4733 NMLVCUSTOMDRAW nmlvcd;
4734 LVITEMW lvItem;
4736 TRACE("(hdc=%p, nItem=%d, subitems=%p, pos=%s)\n", hdc, nItem, subitems, wine_dbgstr_point(&pos));
4738 /* get information needed for drawing the item */
4739 lvItem.mask = LVIF_TEXT | LVIF_IMAGE | LVIF_PARAM | LVIF_STATE;
4740 if (infoPtr->uView == LV_VIEW_DETAILS) lvItem.mask |= LVIF_INDENT;
4741 lvItem.stateMask = LVIS_SELECTED | LVIS_FOCUSED | LVIS_STATEIMAGEMASK | LVIS_CUT | LVIS_OVERLAYMASK;
4742 lvItem.iItem = nItem;
4743 lvItem.iSubItem = 0;
4744 lvItem.state = 0;
4745 lvItem.lParam = 0;
4746 lvItem.cchTextMax = DISP_TEXT_SIZE;
4747 lvItem.pszText = szDispText;
4748 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) return FALSE;
4749 if (lvItem.pszText == LPSTR_TEXTCALLBACKW) lvItem.pszText = callbackW;
4750 TRACE(" lvItem=%s\n", debuglvitem_t(&lvItem, TRUE));
4752 /* now check if we need to update the focus rectangle */
4753 focus = infoPtr->bFocus && (lvItem.state & LVIS_FOCUSED) ? &infoPtr->rcFocus : 0;
4754 if (!focus) lvItem.state &= ~LVIS_FOCUSED;
4756 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, &rcBox, NULL, NULL, NULL, NULL);
4757 OffsetRect(&rcBox, pos.x, pos.y);
4759 /* Full custom draw stage sequence looks like this:
4761 LV_VIEW_DETAILS:
4763 - CDDS_ITEMPREPAINT
4764 - CDDS_ITEMPREPAINT|CDDS_SUBITEM | => sent n times, where n is number of subitems,
4765 CDDS_ITEMPOSTPAINT|CDDS_SUBITEM | including item itself
4766 - CDDS_ITEMPOSTPAINT
4768 other styles:
4770 - CDDS_ITEMPREPAINT
4771 - CDDS_ITEMPOSTPAINT
4774 /* fill in the custom draw structure */
4775 customdraw_fill(&nmlvcd, infoPtr, hdc, &rcBox, &lvItem);
4776 if (cdmode & CDRF_NOTIFYITEMDRAW)
4777 cdsubitemmode = notify_customdraw(infoPtr, CDDS_ITEMPREPAINT, &nmlvcd);
4778 if (cdsubitemmode & CDRF_SKIPDEFAULT) goto postpaint;
4780 if (subitems)
4782 while (iterator_next(subitems))
4784 DWORD subitemstage = CDRF_DODEFAULT;
4786 /* We need to query for each subitem, item's data (subitem == 0) is already here at this point */
4787 if (subitems->nItem)
4789 lvItem.mask = LVIF_TEXT | LVIF_IMAGE | LVIF_PARAM | LVIF_INDENT;
4790 lvItem.stateMask = LVIS_SELECTED | LVIS_FOCUSED | LVIS_STATEIMAGEMASK | LVIS_CUT | LVIS_OVERLAYMASK;
4791 lvItem.iItem = nItem;
4792 lvItem.iSubItem = subitems->nItem;
4793 lvItem.state = 0;
4794 lvItem.lParam = 0;
4795 lvItem.cchTextMax = DISP_TEXT_SIZE;
4796 lvItem.pszText = szDispText;
4797 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) return FALSE;
4798 if (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT)
4799 lvItem.state = LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED);
4800 if (lvItem.pszText == LPSTR_TEXTCALLBACKW) lvItem.pszText = callbackW;
4801 TRACE(" lvItem=%s\n", debuglvitem_t(&lvItem, TRUE));
4803 /* update custom draw data */
4804 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, &nmlvcd.nmcd.rc, NULL, NULL, NULL, NULL);
4805 OffsetRect(&nmlvcd.nmcd.rc, pos.x, pos.y);
4806 nmlvcd.iSubItem = subitems->nItem;
4809 if (cdsubitemmode & CDRF_NOTIFYSUBITEMDRAW)
4810 subitemstage = notify_customdraw(infoPtr, CDDS_SUBITEM | CDDS_ITEMPREPAINT, &nmlvcd);
4812 if (subitems->nItem == 0 || (cdmode & CDRF_NOTIFYITEMDRAW))
4813 prepaint_setup(infoPtr, hdc, &nmlvcd, FALSE);
4814 else if (!(infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT))
4815 prepaint_setup(infoPtr, hdc, &nmlvcd, TRUE);
4817 if (!(subitemstage & CDRF_SKIPDEFAULT))
4818 LISTVIEW_DrawItemPart(infoPtr, &lvItem, &nmlvcd, &pos);
4820 if (subitemstage & CDRF_NOTIFYPOSTPAINT)
4821 subitemstage = notify_customdraw(infoPtr, CDDS_SUBITEM | CDDS_ITEMPOSTPAINT, &nmlvcd);
4824 else
4826 prepaint_setup(infoPtr, hdc, &nmlvcd, FALSE);
4827 LISTVIEW_DrawItemPart(infoPtr, &lvItem, &nmlvcd, &pos);
4830 postpaint:
4831 if (cdsubitemmode & CDRF_NOTIFYPOSTPAINT)
4833 nmlvcd.iSubItem = 0;
4834 notify_customdraw(infoPtr, CDDS_ITEMPOSTPAINT, &nmlvcd);
4837 return TRUE;
4840 /***
4841 * DESCRIPTION:
4842 * Draws listview items when in owner draw mode.
4844 * PARAMETER(S):
4845 * [I] infoPtr : valid pointer to the listview structure
4846 * [I] hdc : device context handle
4848 * RETURN:
4849 * None
4851 static void LISTVIEW_RefreshOwnerDraw(const LISTVIEW_INFO *infoPtr, ITERATOR *i, HDC hdc, DWORD cdmode)
4853 UINT uID = (UINT)GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
4854 DWORD cditemmode = CDRF_DODEFAULT;
4855 NMLVCUSTOMDRAW nmlvcd;
4856 POINT Origin, Position;
4857 DRAWITEMSTRUCT dis;
4858 LVITEMW item;
4860 TRACE("()\n");
4862 ZeroMemory(&dis, sizeof(dis));
4864 /* Get scroll info once before loop */
4865 LISTVIEW_GetOrigin(infoPtr, &Origin);
4867 /* iterate through the invalidated rows */
4868 while(iterator_next(i))
4870 item.iItem = i->nItem;
4871 item.iSubItem = 0;
4872 item.mask = LVIF_PARAM | LVIF_STATE;
4873 item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
4874 if (!LISTVIEW_GetItemW(infoPtr, &item)) continue;
4876 dis.CtlType = ODT_LISTVIEW;
4877 dis.CtlID = uID;
4878 dis.itemID = item.iItem;
4879 dis.itemAction = ODA_DRAWENTIRE;
4880 dis.itemState = 0;
4881 if (item.state & LVIS_SELECTED) dis.itemState |= ODS_SELECTED;
4882 if (infoPtr->bFocus && (item.state & LVIS_FOCUSED)) dis.itemState |= ODS_FOCUS;
4883 dis.hwndItem = infoPtr->hwndSelf;
4884 dis.hDC = hdc;
4885 LISTVIEW_GetItemOrigin(infoPtr, dis.itemID, &Position);
4886 dis.rcItem.left = Position.x + Origin.x;
4887 dis.rcItem.right = dis.rcItem.left + infoPtr->nItemWidth;
4888 dis.rcItem.top = Position.y + Origin.y;
4889 dis.rcItem.bottom = dis.rcItem.top + infoPtr->nItemHeight;
4890 dis.itemData = item.lParam;
4892 TRACE("item=%s, rcItem=%s\n", debuglvitem_t(&item, TRUE), wine_dbgstr_rect(&dis.rcItem));
4895 * Even if we do not send the CDRF_NOTIFYITEMDRAW we need to fill the nmlvcd
4896 * structure for the rest. of the paint cycle
4898 customdraw_fill(&nmlvcd, infoPtr, hdc, &dis.rcItem, &item);
4899 if (cdmode & CDRF_NOTIFYITEMDRAW)
4900 cditemmode = notify_customdraw(infoPtr, CDDS_PREPAINT, &nmlvcd);
4902 if (!(cditemmode & CDRF_SKIPDEFAULT))
4904 prepaint_setup (infoPtr, hdc, &nmlvcd, FALSE);
4905 SendMessageW(infoPtr->hwndNotify, WM_DRAWITEM, dis.CtlID, (LPARAM)&dis);
4908 if (cditemmode & CDRF_NOTIFYPOSTPAINT)
4909 notify_postpaint(infoPtr, &nmlvcd);
4913 /***
4914 * DESCRIPTION:
4915 * Draws listview items when in report display mode.
4917 * PARAMETER(S):
4918 * [I] infoPtr : valid pointer to the listview structure
4919 * [I] hdc : device context handle
4920 * [I] cdmode : custom draw mode
4922 * RETURN:
4923 * None
4925 static void LISTVIEW_RefreshReport(LISTVIEW_INFO *infoPtr, ITERATOR *i, HDC hdc, DWORD cdmode)
4927 INT rgntype;
4928 RECT rcClip, rcItem;
4929 POINT Origin;
4930 RANGES colRanges;
4931 INT col;
4932 ITERATOR j;
4934 TRACE("()\n");
4936 /* figure out what to draw */
4937 rgntype = GetClipBox(hdc, &rcClip);
4938 if (rgntype == NULLREGION) return;
4940 /* Get scroll info once before loop */
4941 LISTVIEW_GetOrigin(infoPtr, &Origin);
4943 colRanges = ranges_create(DPA_GetPtrCount(infoPtr->hdpaColumns));
4945 /* narrow down the columns we need to paint */
4946 for(col = 0; col < DPA_GetPtrCount(infoPtr->hdpaColumns); col++)
4948 INT index = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX, col, 0);
4950 LISTVIEW_GetHeaderRect(infoPtr, index, &rcItem);
4951 if ((rcItem.right + Origin.x >= rcClip.left) && (rcItem.left + Origin.x < rcClip.right))
4952 ranges_additem(colRanges, index);
4954 iterator_rangesitems(&j, colRanges);
4956 /* in full row select, we _have_ to draw the main item */
4957 if (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT)
4958 j.nSpecial = 0;
4960 /* iterate through the invalidated rows */
4961 while(iterator_next(i))
4963 RANGES subitems;
4964 POINT Position;
4965 ITERATOR k;
4967 SelectObject(hdc, infoPtr->hFont);
4968 LISTVIEW_GetItemOrigin(infoPtr, i->nItem, &Position);
4969 Position.x = Origin.x;
4970 Position.y += Origin.y;
4972 subitems = ranges_create(DPA_GetPtrCount(infoPtr->hdpaColumns));
4974 /* iterate through the invalidated columns */
4975 while(iterator_next(&j))
4977 LISTVIEW_GetHeaderRect(infoPtr, j.nItem, &rcItem);
4979 if (rgntype == COMPLEXREGION && !((infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT) && j.nItem == 0))
4981 rcItem.top = 0;
4982 rcItem.bottom = infoPtr->nItemHeight;
4983 OffsetRect(&rcItem, Origin.x, Position.y);
4984 if (!RectVisible(hdc, &rcItem)) continue;
4987 ranges_additem(subitems, j.nItem);
4990 iterator_rangesitems(&k, subitems);
4991 LISTVIEW_DrawItem(infoPtr, hdc, i->nItem, &k, Position, cdmode);
4992 iterator_destroy(&k);
4994 iterator_destroy(&j);
4997 /***
4998 * DESCRIPTION:
4999 * Draws the gridlines if necessary when in report display mode.
5001 * PARAMETER(S):
5002 * [I] infoPtr : valid pointer to the listview structure
5003 * [I] hdc : device context handle
5005 * RETURN:
5006 * None
5008 static void LISTVIEW_RefreshReportGrid(LISTVIEW_INFO *infoPtr, HDC hdc)
5010 INT rgntype;
5011 INT y, itemheight;
5012 INT col, index;
5013 HPEN hPen, hOldPen;
5014 RECT rcClip, rcItem = {0};
5015 POINT Origin;
5016 RANGES colRanges;
5017 ITERATOR j;
5018 BOOL rmost = FALSE;
5020 TRACE("()\n");
5022 /* figure out what to draw */
5023 rgntype = GetClipBox(hdc, &rcClip);
5024 if (rgntype == NULLREGION) return;
5026 /* Get scroll info once before loop */
5027 LISTVIEW_GetOrigin(infoPtr, &Origin);
5029 colRanges = ranges_create(DPA_GetPtrCount(infoPtr->hdpaColumns));
5031 /* narrow down the columns we need to paint */
5032 for(col = 0; col < DPA_GetPtrCount(infoPtr->hdpaColumns); col++)
5034 index = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX, col, 0);
5036 LISTVIEW_GetHeaderRect(infoPtr, index, &rcItem);
5037 if ((rcItem.right + Origin.x >= rcClip.left) && (rcItem.left + Origin.x < rcClip.right))
5038 ranges_additem(colRanges, index);
5041 /* is right most vertical line visible? */
5042 if (DPA_GetPtrCount(infoPtr->hdpaColumns) > 0)
5044 index = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX, DPA_GetPtrCount(infoPtr->hdpaColumns) - 1, 0);
5045 LISTVIEW_GetHeaderRect(infoPtr, index, &rcItem);
5046 rmost = (rcItem.right + Origin.x < rcClip.right);
5049 if ((hPen = CreatePen( PS_SOLID, 1, comctl32_color.clr3dFace )))
5051 hOldPen = SelectObject ( hdc, hPen );
5053 /* draw the vertical lines for the columns */
5054 iterator_rangesitems(&j, colRanges);
5055 while(iterator_next(&j))
5057 LISTVIEW_GetHeaderRect(infoPtr, j.nItem, &rcItem);
5058 if (rcItem.left == 0) continue; /* skip leftmost column */
5059 rcItem.left += Origin.x;
5060 rcItem.right += Origin.x;
5061 rcItem.top = infoPtr->rcList.top;
5062 rcItem.bottom = infoPtr->rcList.bottom;
5063 TRACE("vert col=%d, rcItem=%s\n", j.nItem, wine_dbgstr_rect(&rcItem));
5064 MoveToEx (hdc, rcItem.left, rcItem.top, NULL);
5065 LineTo (hdc, rcItem.left, rcItem.bottom);
5067 iterator_destroy(&j);
5068 /* draw rightmost grid line if visible */
5069 if (rmost)
5071 index = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX,
5072 DPA_GetPtrCount(infoPtr->hdpaColumns) - 1, 0);
5073 LISTVIEW_GetHeaderRect(infoPtr, index, &rcItem);
5075 rcItem.right += Origin.x;
5077 MoveToEx (hdc, rcItem.right, infoPtr->rcList.top, NULL);
5078 LineTo (hdc, rcItem.right, infoPtr->rcList.bottom);
5081 /* draw the horizontal lines for the rows */
5082 itemheight = LISTVIEW_CalculateItemHeight(infoPtr);
5083 rcItem.left = infoPtr->rcList.left;
5084 rcItem.right = infoPtr->rcList.right;
5085 for(y = Origin.y > 1 ? Origin.y - 1 : itemheight - 1 + Origin.y % itemheight; y<=infoPtr->rcList.bottom; y+=itemheight)
5087 rcItem.bottom = rcItem.top = y;
5088 TRACE("horz rcItem=%s\n", wine_dbgstr_rect(&rcItem));
5089 MoveToEx (hdc, rcItem.left, rcItem.top, NULL);
5090 LineTo (hdc, rcItem.right, rcItem.top);
5093 SelectObject( hdc, hOldPen );
5094 DeleteObject( hPen );
5096 else
5097 ranges_destroy(colRanges);
5100 /***
5101 * DESCRIPTION:
5102 * Draws listview items when in list display mode.
5104 * PARAMETER(S):
5105 * [I] infoPtr : valid pointer to the listview structure
5106 * [I] hdc : device context handle
5107 * [I] cdmode : custom draw mode
5109 * RETURN:
5110 * None
5112 static void LISTVIEW_RefreshList(LISTVIEW_INFO *infoPtr, ITERATOR *i, HDC hdc, DWORD cdmode)
5114 POINT Origin, Position;
5116 /* Get scroll info once before loop */
5117 LISTVIEW_GetOrigin(infoPtr, &Origin);
5119 while(iterator_prev(i))
5121 SelectObject(hdc, infoPtr->hFont);
5122 LISTVIEW_GetItemOrigin(infoPtr, i->nItem, &Position);
5123 Position.x += Origin.x;
5124 Position.y += Origin.y;
5126 LISTVIEW_DrawItem(infoPtr, hdc, i->nItem, NULL, Position, cdmode);
5131 /***
5132 * DESCRIPTION:
5133 * Draws listview items.
5135 * PARAMETER(S):
5136 * [I] infoPtr : valid pointer to the listview structure
5137 * [I] hdc : device context handle
5138 * [I] prcErase : rect to be erased before refresh (may be NULL)
5140 * RETURN:
5141 * NoneX
5143 static void LISTVIEW_Refresh(LISTVIEW_INFO *infoPtr, HDC hdc, const RECT *prcErase)
5145 COLORREF oldTextColor = 0, oldBkColor = 0;
5146 NMLVCUSTOMDRAW nmlvcd;
5147 HFONT hOldFont = 0;
5148 DWORD cdmode;
5149 INT oldBkMode = 0;
5150 RECT rcClient;
5151 ITERATOR i;
5152 HDC hdcOrig = hdc;
5153 HBITMAP hbmp = NULL;
5154 RANGE range;
5156 LISTVIEW_DUMP(infoPtr);
5158 if (infoPtr->dwLvExStyle & LVS_EX_DOUBLEBUFFER) {
5159 TRACE("double buffering\n");
5161 hdc = CreateCompatibleDC(hdcOrig);
5162 if (!hdc) {
5163 ERR("Failed to create DC for backbuffer\n");
5164 return;
5166 hbmp = CreateCompatibleBitmap(hdcOrig, infoPtr->rcList.right,
5167 infoPtr->rcList.bottom);
5168 if (!hbmp) {
5169 ERR("Failed to create bitmap for backbuffer\n");
5170 DeleteDC(hdc);
5171 return;
5174 SelectObject(hdc, hbmp);
5175 SelectObject(hdc, infoPtr->hFont);
5177 if(GetClipBox(hdcOrig, &rcClient))
5178 IntersectClipRect(hdc, rcClient.left, rcClient.top, rcClient.right, rcClient.bottom);
5179 } else {
5180 /* Save dc values we're gonna trash while drawing
5181 * FIXME: Should be done in LISTVIEW_DrawItem() */
5182 hOldFont = SelectObject(hdc, infoPtr->hFont);
5183 oldBkMode = GetBkMode(hdc);
5184 oldBkColor = GetBkColor(hdc);
5185 oldTextColor = GetTextColor(hdc);
5188 infoPtr->bIsDrawing = TRUE;
5190 if (prcErase) {
5191 LISTVIEW_FillBkgnd(infoPtr, hdc, prcErase);
5192 } else if (infoPtr->dwLvExStyle & LVS_EX_DOUBLEBUFFER) {
5193 /* If no erasing was done (usually because RedrawWindow was called
5194 * with RDW_INVALIDATE only) we need to copy the old contents into
5195 * the backbuffer before continuing. */
5196 BitBlt(hdc, infoPtr->rcList.left, infoPtr->rcList.top,
5197 infoPtr->rcList.right - infoPtr->rcList.left,
5198 infoPtr->rcList.bottom - infoPtr->rcList.top,
5199 hdcOrig, infoPtr->rcList.left, infoPtr->rcList.top, SRCCOPY);
5202 GetClientRect(infoPtr->hwndSelf, &rcClient);
5203 customdraw_fill(&nmlvcd, infoPtr, hdc, &rcClient, 0);
5204 cdmode = notify_customdraw(infoPtr, CDDS_PREPAINT, &nmlvcd);
5205 if (cdmode & CDRF_SKIPDEFAULT) goto enddraw;
5207 /* nothing to draw */
5208 if(infoPtr->nItemCount == 0) goto enddraw;
5210 /* figure out what we need to draw */
5211 iterator_visibleitems(&i, infoPtr, hdc);
5212 range = iterator_range(&i);
5214 /* send cache hint notification */
5215 if (infoPtr->dwStyle & LVS_OWNERDATA)
5217 NMLVCACHEHINT nmlv;
5219 ZeroMemory(&nmlv, sizeof(NMLVCACHEHINT));
5220 nmlv.iFrom = range.lower;
5221 nmlv.iTo = range.upper - 1;
5222 notify_hdr(infoPtr, LVN_ODCACHEHINT, &nmlv.hdr);
5225 if ((infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && (infoPtr->uView == LV_VIEW_DETAILS))
5226 LISTVIEW_RefreshOwnerDraw(infoPtr, &i, hdc, cdmode);
5227 else
5229 if (infoPtr->uView == LV_VIEW_DETAILS)
5230 LISTVIEW_RefreshReport(infoPtr, &i, hdc, cdmode);
5231 else /* LV_VIEW_LIST, LV_VIEW_ICON or LV_VIEW_SMALLICON */
5232 LISTVIEW_RefreshList(infoPtr, &i, hdc, cdmode);
5234 /* if we have a focus rect and it's visible, draw it */
5235 if (infoPtr->bFocus && range.lower <= infoPtr->nFocusedItem &&
5236 (range.upper - 1) >= infoPtr->nFocusedItem)
5237 LISTVIEW_DrawFocusRect(infoPtr, hdc);
5239 iterator_destroy(&i);
5241 enddraw:
5242 /* For LVS_EX_GRIDLINES go and draw lines */
5243 /* This includes the case where there were *no* items */
5244 if ((infoPtr->uView == LV_VIEW_DETAILS) && infoPtr->dwLvExStyle & LVS_EX_GRIDLINES)
5245 LISTVIEW_RefreshReportGrid(infoPtr, hdc);
5247 /* Draw marquee rectangle if appropriate */
5248 if (infoPtr->bMarqueeSelect)
5249 DrawFocusRect(hdc, &infoPtr->marqueeDrawRect);
5251 if (cdmode & CDRF_NOTIFYPOSTPAINT)
5252 notify_postpaint(infoPtr, &nmlvcd);
5254 if(hbmp) {
5255 BitBlt(hdcOrig, infoPtr->rcList.left, infoPtr->rcList.top,
5256 infoPtr->rcList.right - infoPtr->rcList.left,
5257 infoPtr->rcList.bottom - infoPtr->rcList.top,
5258 hdc, infoPtr->rcList.left, infoPtr->rcList.top, SRCCOPY);
5260 DeleteObject(hbmp);
5261 DeleteDC(hdc);
5262 } else {
5263 SelectObject(hdc, hOldFont);
5264 SetBkMode(hdc, oldBkMode);
5265 SetBkColor(hdc, oldBkColor);
5266 SetTextColor(hdc, oldTextColor);
5269 infoPtr->bIsDrawing = FALSE;
5273 /***
5274 * DESCRIPTION:
5275 * Calculates the approximate width and height of a given number of items.
5277 * PARAMETER(S):
5278 * [I] infoPtr : valid pointer to the listview structure
5279 * [I] nItemCount : number of items
5280 * [I] wWidth : width
5281 * [I] wHeight : height
5283 * RETURN:
5284 * Returns a DWORD. The width in the low word and the height in high word.
5286 static DWORD LISTVIEW_ApproximateViewRect(const LISTVIEW_INFO *infoPtr, INT nItemCount,
5287 WORD wWidth, WORD wHeight)
5289 DWORD dwViewRect = 0;
5291 if (nItemCount == -1)
5292 nItemCount = infoPtr->nItemCount;
5294 if (infoPtr->uView == LV_VIEW_LIST)
5296 INT nItemCountPerColumn = 1;
5297 INT nColumnCount = 0;
5299 if (wHeight == 0xFFFF)
5301 /* use current height */
5302 wHeight = infoPtr->rcList.bottom - infoPtr->rcList.top;
5305 if (wHeight < infoPtr->nItemHeight)
5306 wHeight = infoPtr->nItemHeight;
5308 if (nItemCount > 0)
5310 if (infoPtr->nItemHeight > 0)
5312 nItemCountPerColumn = wHeight / infoPtr->nItemHeight;
5313 if (nItemCountPerColumn == 0)
5314 nItemCountPerColumn = 1;
5316 if (nItemCount % nItemCountPerColumn != 0)
5317 nColumnCount = nItemCount / nItemCountPerColumn;
5318 else
5319 nColumnCount = nItemCount / nItemCountPerColumn + 1;
5323 /* Microsoft padding magic */
5324 wHeight = nItemCountPerColumn * infoPtr->nItemHeight + 2;
5325 wWidth = nColumnCount * infoPtr->nItemWidth + 2;
5327 dwViewRect = MAKELONG(wWidth, wHeight);
5329 else if (infoPtr->uView == LV_VIEW_DETAILS)
5331 RECT rcBox;
5333 if (infoPtr->nItemCount > 0)
5335 LISTVIEW_GetItemBox(infoPtr, 0, &rcBox);
5336 wWidth = rcBox.right - rcBox.left;
5337 wHeight = (rcBox.bottom - rcBox.top) * nItemCount;
5339 else
5341 /* use current height and width */
5342 if (wHeight == 0xffff)
5343 wHeight = infoPtr->rcList.bottom - infoPtr->rcList.top;
5344 if (wWidth == 0xffff)
5345 wWidth = infoPtr->rcList.right - infoPtr->rcList.left;
5348 dwViewRect = MAKELONG(wWidth, wHeight);
5350 else if (infoPtr->uView == LV_VIEW_ICON)
5352 UINT rows,cols;
5353 UINT nItemWidth;
5354 UINT nItemHeight;
5356 nItemWidth = infoPtr->iconSpacing.cx;
5357 nItemHeight = infoPtr->iconSpacing.cy;
5359 if (wWidth == 0xffff)
5360 wWidth = infoPtr->rcList.right - infoPtr->rcList.left;
5362 if (wWidth < nItemWidth)
5363 wWidth = nItemWidth;
5365 cols = wWidth / nItemWidth;
5366 if (cols > nItemCount)
5367 cols = nItemCount;
5368 if (cols < 1)
5369 cols = 1;
5371 if (nItemCount)
5373 rows = nItemCount / cols;
5374 if (nItemCount % cols)
5375 rows++;
5377 else
5378 rows = 0;
5380 wHeight = (nItemHeight * rows)+2;
5381 wWidth = (nItemWidth * cols)+2;
5383 dwViewRect = MAKELONG(wWidth, wHeight);
5385 else if (infoPtr->uView == LV_VIEW_SMALLICON)
5386 FIXME("uView == LV_VIEW_SMALLICON: not implemented\n");
5388 return dwViewRect;
5391 /***
5392 * DESCRIPTION:
5393 * Cancel edit label with saving item text.
5395 * PARAMETER(S):
5396 * [I] infoPtr : valid pointer to the listview structure
5398 * RETURN:
5399 * Always returns TRUE.
5401 static LRESULT LISTVIEW_CancelEditLabel(LISTVIEW_INFO *infoPtr)
5403 if (infoPtr->hwndEdit)
5405 /* handle value will be lost after LISTVIEW_EndEditLabelT */
5406 HWND edit = infoPtr->hwndEdit;
5408 LISTVIEW_EndEditLabelT(infoPtr, TRUE, IsWindowUnicode(infoPtr->hwndEdit));
5409 SendMessageW(edit, WM_CLOSE, 0, 0);
5412 return TRUE;
5415 /***
5416 * DESCRIPTION:
5417 * Create a drag image list for the specified item.
5419 * PARAMETER(S):
5420 * [I] infoPtr : valid pointer to the listview structure
5421 * [I] iItem : index of item
5422 * [O] lppt : Upper-left corner of the image
5424 * RETURN:
5425 * Returns a handle to the image list if successful, NULL otherwise.
5427 static HIMAGELIST LISTVIEW_CreateDragImage(LISTVIEW_INFO *infoPtr, INT iItem, LPPOINT lppt)
5429 RECT rcItem;
5430 SIZE size;
5431 POINT pos;
5432 HDC hdc, hdcOrig;
5433 HBITMAP hbmp, hOldbmp;
5434 HFONT hOldFont;
5435 HIMAGELIST dragList = 0;
5436 TRACE("iItem=%d Count=%d\n", iItem, infoPtr->nItemCount);
5438 if (iItem < 0 || iItem >= infoPtr->nItemCount || !lppt)
5439 return 0;
5441 rcItem.left = LVIR_BOUNDS;
5442 if (!LISTVIEW_GetItemRect(infoPtr, iItem, &rcItem))
5443 return 0;
5445 lppt->x = rcItem.left;
5446 lppt->y = rcItem.top;
5448 size.cx = rcItem.right - rcItem.left;
5449 size.cy = rcItem.bottom - rcItem.top;
5451 hdcOrig = GetDC(infoPtr->hwndSelf);
5452 hdc = CreateCompatibleDC(hdcOrig);
5453 hbmp = CreateCompatibleBitmap(hdcOrig, size.cx, size.cy);
5454 hOldbmp = SelectObject(hdc, hbmp);
5455 hOldFont = SelectObject(hdc, infoPtr->hFont);
5457 SetRect(&rcItem, 0, 0, size.cx, size.cy);
5458 FillRect(hdc, &rcItem, infoPtr->hBkBrush);
5460 pos.x = pos.y = 0;
5461 if (LISTVIEW_DrawItem(infoPtr, hdc, iItem, NULL, pos, CDRF_DODEFAULT))
5463 dragList = ImageList_Create(size.cx, size.cy, ILC_COLOR, 10, 10);
5464 SelectObject(hdc, hOldbmp);
5465 ImageList_Add(dragList, hbmp, 0);
5467 else
5468 SelectObject(hdc, hOldbmp);
5470 SelectObject(hdc, hOldFont);
5471 DeleteObject(hbmp);
5472 DeleteDC(hdc);
5473 ReleaseDC(infoPtr->hwndSelf, hdcOrig);
5475 TRACE("ret=%p\n", dragList);
5477 return dragList;
5481 /***
5482 * DESCRIPTION:
5483 * Removes all listview items and subitems.
5485 * PARAMETER(S):
5486 * [I] infoPtr : valid pointer to the listview structure
5488 * RETURN:
5489 * SUCCESS : TRUE
5490 * FAILURE : FALSE
5492 static BOOL LISTVIEW_DeleteAllItems(LISTVIEW_INFO *infoPtr, BOOL destroy)
5494 HDPA hdpaSubItems = NULL;
5495 BOOL suppress = FALSE;
5496 ITEMHDR *hdrItem;
5497 ITEM_INFO *lpItem;
5498 ITEM_ID *lpID;
5499 INT i, j;
5501 TRACE("()\n");
5503 /* we do it directly, to avoid notifications */
5504 ranges_clear(infoPtr->selectionRanges);
5505 infoPtr->nSelectionMark = -1;
5506 infoPtr->nFocusedItem = -1;
5507 SetRectEmpty(&infoPtr->rcFocus);
5508 /* But we are supposed to leave nHotItem as is! */
5510 /* send LVN_DELETEALLITEMS notification */
5511 if (!(infoPtr->dwStyle & LVS_OWNERDATA) || !destroy)
5513 NMLISTVIEW nmlv;
5515 memset(&nmlv, 0, sizeof(NMLISTVIEW));
5516 nmlv.iItem = -1;
5517 suppress = notify_listview(infoPtr, LVN_DELETEALLITEMS, &nmlv);
5520 for (i = infoPtr->nItemCount - 1; i >= 0; i--)
5522 if (!(infoPtr->dwStyle & LVS_OWNERDATA))
5524 /* send LVN_DELETEITEM notification, if not suppressed
5525 and if it is not a virtual listview */
5526 if (!suppress) notify_deleteitem(infoPtr, i);
5527 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, i);
5528 lpItem = DPA_GetPtr(hdpaSubItems, 0);
5529 /* free id struct */
5530 j = DPA_GetPtrIndex(infoPtr->hdpaItemIds, lpItem->id);
5531 lpID = DPA_GetPtr(infoPtr->hdpaItemIds, j);
5532 DPA_DeletePtr(infoPtr->hdpaItemIds, j);
5533 Free(lpID);
5534 /* both item and subitem start with ITEMHDR header */
5535 for (j = 0; j < DPA_GetPtrCount(hdpaSubItems); j++)
5537 hdrItem = DPA_GetPtr(hdpaSubItems, j);
5538 if (is_text(hdrItem->pszText)) Free(hdrItem->pszText);
5539 Free(hdrItem);
5541 DPA_Destroy(hdpaSubItems);
5542 DPA_DeletePtr(infoPtr->hdpaItems, i);
5544 DPA_DeletePtr(infoPtr->hdpaPosX, i);
5545 DPA_DeletePtr(infoPtr->hdpaPosY, i);
5546 infoPtr->nItemCount --;
5549 if (!destroy)
5551 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
5552 LISTVIEW_UpdateScroll(infoPtr);
5554 LISTVIEW_InvalidateList(infoPtr);
5556 return TRUE;
5559 /***
5560 * DESCRIPTION:
5561 * Scrolls, and updates the columns, when a column is changing width.
5563 * PARAMETER(S):
5564 * [I] infoPtr : valid pointer to the listview structure
5565 * [I] nColumn : column to scroll
5566 * [I] dx : amount of scroll, in pixels
5568 * RETURN:
5569 * None.
5571 static void LISTVIEW_ScrollColumns(LISTVIEW_INFO *infoPtr, INT nColumn, INT dx)
5573 COLUMN_INFO *lpColumnInfo;
5574 RECT rcOld, rcCol;
5575 POINT ptOrigin;
5576 INT nCol;
5577 HDITEMW hdi;
5579 if (nColumn < 0 || DPA_GetPtrCount(infoPtr->hdpaColumns) < 1) return;
5580 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, min(nColumn, DPA_GetPtrCount(infoPtr->hdpaColumns) - 1));
5581 rcCol = lpColumnInfo->rcHeader;
5582 if (nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns))
5583 rcCol.left = rcCol.right;
5585 /* adjust the other columns */
5586 hdi.mask = HDI_ORDER;
5587 if (SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, nColumn, (LPARAM)&hdi))
5589 INT nOrder = hdi.iOrder;
5590 for (nCol = 0; nCol < DPA_GetPtrCount(infoPtr->hdpaColumns); nCol++)
5592 hdi.mask = HDI_ORDER;
5593 SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, nCol, (LPARAM)&hdi);
5594 if (hdi.iOrder >= nOrder) {
5595 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, nCol);
5596 lpColumnInfo->rcHeader.left += dx;
5597 lpColumnInfo->rcHeader.right += dx;
5602 /* do not update screen if not in report mode */
5603 if (!is_redrawing(infoPtr) || infoPtr->uView != LV_VIEW_DETAILS) return;
5605 /* Need to reset the item width when inserting a new column */
5606 infoPtr->nItemWidth += dx;
5608 LISTVIEW_UpdateScroll(infoPtr);
5609 LISTVIEW_GetOrigin(infoPtr, &ptOrigin);
5611 /* scroll to cover the deleted column, and invalidate for redraw */
5612 rcOld = infoPtr->rcList;
5613 rcOld.left = ptOrigin.x + rcCol.left + dx;
5614 ScrollWindowEx(infoPtr->hwndSelf, dx, 0, &rcOld, &rcOld, 0, 0, SW_ERASE | SW_INVALIDATE);
5617 /***
5618 * DESCRIPTION:
5619 * Removes a column from the listview control.
5621 * PARAMETER(S):
5622 * [I] infoPtr : valid pointer to the listview structure
5623 * [I] nColumn : column index
5625 * RETURN:
5626 * SUCCESS : TRUE
5627 * FAILURE : FALSE
5629 static BOOL LISTVIEW_DeleteColumn(LISTVIEW_INFO *infoPtr, INT nColumn)
5631 RECT rcCol;
5633 TRACE("nColumn=%d\n", nColumn);
5635 if (nColumn < 0 || nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns))
5636 return FALSE;
5638 /* While the MSDN specifically says that column zero should not be deleted,
5639 what actually happens is that the column itself is deleted but no items or subitems
5640 are removed.
5643 LISTVIEW_GetHeaderRect(infoPtr, nColumn, &rcCol);
5645 if (!SendMessageW(infoPtr->hwndHeader, HDM_DELETEITEM, nColumn, 0))
5646 return FALSE;
5648 Free(DPA_GetPtr(infoPtr->hdpaColumns, nColumn));
5649 DPA_DeletePtr(infoPtr->hdpaColumns, nColumn);
5651 if (!(infoPtr->dwStyle & LVS_OWNERDATA) && nColumn)
5653 SUBITEM_INFO *lpSubItem, *lpDelItem;
5654 HDPA hdpaSubItems;
5655 INT nItem, nSubItem, i;
5657 for (nItem = 0; nItem < infoPtr->nItemCount; nItem++)
5659 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, nItem);
5660 nSubItem = 0;
5661 lpDelItem = 0;
5662 for (i = 1; i < DPA_GetPtrCount(hdpaSubItems); i++)
5664 lpSubItem = DPA_GetPtr(hdpaSubItems, i);
5665 if (lpSubItem->iSubItem == nColumn)
5667 nSubItem = i;
5668 lpDelItem = lpSubItem;
5670 else if (lpSubItem->iSubItem > nColumn)
5672 lpSubItem->iSubItem--;
5676 /* if we found our subitem, zap it */
5677 if (nSubItem > 0)
5679 /* free string */
5680 if (is_text(lpDelItem->hdr.pszText))
5681 Free(lpDelItem->hdr.pszText);
5683 /* free item */
5684 Free(lpDelItem);
5686 /* free dpa memory */
5687 DPA_DeletePtr(hdpaSubItems, nSubItem);
5692 /* update the other column info */
5693 if(DPA_GetPtrCount(infoPtr->hdpaColumns) == 0)
5694 LISTVIEW_InvalidateList(infoPtr);
5695 else
5696 LISTVIEW_ScrollColumns(infoPtr, nColumn, -(rcCol.right - rcCol.left));
5697 LISTVIEW_UpdateItemSize(infoPtr);
5699 return TRUE;
5702 /***
5703 * DESCRIPTION:
5704 * Invalidates the listview after an item's insertion or deletion.
5706 * PARAMETER(S):
5707 * [I] infoPtr : valid pointer to the listview structure
5708 * [I] nItem : item index
5709 * [I] dir : -1 if deleting, 1 if inserting
5711 * RETURN:
5712 * None
5714 static void LISTVIEW_ScrollOnInsert(LISTVIEW_INFO *infoPtr, INT nItem, INT dir)
5716 INT nPerCol, nItemCol, nItemRow;
5717 RECT rcScroll;
5718 POINT Origin;
5720 /* if we don't refresh, what's the point of scrolling? */
5721 if (!is_redrawing(infoPtr)) return;
5723 assert (abs(dir) == 1);
5725 /* arrange icons if autoarrange is on */
5726 if (is_autoarrange(infoPtr))
5728 BOOL arrange = TRUE;
5729 if (dir < 0 && nItem >= infoPtr->nItemCount) arrange = FALSE;
5730 if (dir > 0 && nItem == infoPtr->nItemCount - 1) arrange = FALSE;
5731 if (arrange) LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
5734 /* scrollbars need updating */
5735 LISTVIEW_UpdateScroll(infoPtr);
5737 /* figure out the item's position */
5738 if (infoPtr->uView == LV_VIEW_DETAILS)
5739 nPerCol = infoPtr->nItemCount + 1;
5740 else if (infoPtr->uView == LV_VIEW_LIST)
5741 nPerCol = LISTVIEW_GetCountPerColumn(infoPtr);
5742 else /* LV_VIEW_ICON, or LV_VIEW_SMALLICON */
5743 return;
5745 nItemCol = nItem / nPerCol;
5746 nItemRow = nItem % nPerCol;
5747 LISTVIEW_GetOrigin(infoPtr, &Origin);
5749 /* move the items below up a slot */
5750 rcScroll.left = nItemCol * infoPtr->nItemWidth;
5751 rcScroll.top = nItemRow * infoPtr->nItemHeight;
5752 rcScroll.right = rcScroll.left + infoPtr->nItemWidth;
5753 rcScroll.bottom = nPerCol * infoPtr->nItemHeight;
5754 OffsetRect(&rcScroll, Origin.x, Origin.y);
5755 TRACE("rcScroll=%s, dx=%d\n", wine_dbgstr_rect(&rcScroll), dir * infoPtr->nItemHeight);
5756 if (IntersectRect(&rcScroll, &rcScroll, &infoPtr->rcList))
5758 TRACE("Invalidating rcScroll=%s, rcList=%s\n", wine_dbgstr_rect(&rcScroll), wine_dbgstr_rect(&infoPtr->rcList));
5759 InvalidateRect(infoPtr->hwndSelf, &rcScroll, TRUE);
5762 /* report has only that column, so we're done */
5763 if (infoPtr->uView == LV_VIEW_DETAILS) return;
5765 /* now for LISTs, we have to deal with the columns to the right */
5766 SetRect(&rcScroll, (nItemCol + 1) * infoPtr->nItemWidth, 0,
5767 (infoPtr->nItemCount / nPerCol + 1) * infoPtr->nItemWidth,
5768 nPerCol * infoPtr->nItemHeight);
5769 OffsetRect(&rcScroll, Origin.x, Origin.y);
5770 if (IntersectRect(&rcScroll, &rcScroll, &infoPtr->rcList))
5771 InvalidateRect(infoPtr->hwndSelf, &rcScroll, TRUE);
5774 /***
5775 * DESCRIPTION:
5776 * Removes an item from the listview control.
5778 * PARAMETER(S):
5779 * [I] infoPtr : valid pointer to the listview structure
5780 * [I] nItem : item index
5782 * RETURN:
5783 * SUCCESS : TRUE
5784 * FAILURE : FALSE
5786 static BOOL LISTVIEW_DeleteItem(LISTVIEW_INFO *infoPtr, INT nItem)
5788 LVITEMW item;
5789 const BOOL is_icon = (infoPtr->uView == LV_VIEW_SMALLICON || infoPtr->uView == LV_VIEW_ICON);
5790 INT focus = infoPtr->nFocusedItem;
5792 TRACE("(nItem=%d)\n", nItem);
5794 if (nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
5796 /* remove selection, and focus */
5797 item.state = 0;
5798 item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
5799 LISTVIEW_SetItemState(infoPtr, nItem, &item);
5801 /* send LVN_DELETEITEM notification. */
5802 if (!notify_deleteitem(infoPtr, nItem)) return FALSE;
5804 /* we need to do this here, because we'll be deleting stuff */
5805 if (is_icon)
5806 LISTVIEW_InvalidateItem(infoPtr, nItem);
5808 if (!(infoPtr->dwStyle & LVS_OWNERDATA))
5810 HDPA hdpaSubItems;
5811 ITEMHDR *hdrItem;
5812 ITEM_INFO *lpItem;
5813 ITEM_ID *lpID;
5814 INT i;
5816 hdpaSubItems = DPA_DeletePtr(infoPtr->hdpaItems, nItem);
5817 lpItem = DPA_GetPtr(hdpaSubItems, 0);
5819 /* free id struct */
5820 i = DPA_GetPtrIndex(infoPtr->hdpaItemIds, lpItem->id);
5821 lpID = DPA_GetPtr(infoPtr->hdpaItemIds, i);
5822 DPA_DeletePtr(infoPtr->hdpaItemIds, i);
5823 Free(lpID);
5824 for (i = 0; i < DPA_GetPtrCount(hdpaSubItems); i++)
5826 hdrItem = DPA_GetPtr(hdpaSubItems, i);
5827 if (is_text(hdrItem->pszText)) Free(hdrItem->pszText);
5828 Free(hdrItem);
5830 DPA_Destroy(hdpaSubItems);
5833 if (is_icon)
5835 DPA_DeletePtr(infoPtr->hdpaPosX, nItem);
5836 DPA_DeletePtr(infoPtr->hdpaPosY, nItem);
5839 infoPtr->nItemCount--;
5840 LISTVIEW_ShiftIndices(infoPtr, nItem, -1);
5841 LISTVIEW_ShiftFocus(infoPtr, focus, nItem, -1);
5843 /* now is the invalidation fun */
5844 if (!is_icon)
5845 LISTVIEW_ScrollOnInsert(infoPtr, nItem, -1);
5846 return TRUE;
5850 /***
5851 * DESCRIPTION:
5852 * Callback implementation for editlabel control
5854 * PARAMETER(S):
5855 * [I] infoPtr : valid pointer to the listview structure
5856 * [I] storeText : store edit box text as item text
5857 * [I] isW : TRUE if psxText is Unicode, FALSE if it's ANSI
5859 * RETURN:
5860 * SUCCESS : TRUE
5861 * FAILURE : FALSE
5863 static BOOL LISTVIEW_EndEditLabelT(LISTVIEW_INFO *infoPtr, BOOL storeText, BOOL isW)
5865 HWND hwndSelf = infoPtr->hwndSelf;
5866 WCHAR szDispText[DISP_TEXT_SIZE] = { 0 };
5867 NMLVDISPINFOW dispInfo;
5868 INT editedItem = infoPtr->nEditLabelItem;
5869 BOOL same;
5870 WCHAR *pszText = NULL;
5871 BOOL res;
5873 if (storeText)
5875 DWORD len = isW ? GetWindowTextLengthW(infoPtr->hwndEdit) : GetWindowTextLengthA(infoPtr->hwndEdit);
5877 if (len++)
5879 if (!(pszText = Alloc(len * (isW ? sizeof(WCHAR) : sizeof(CHAR)))))
5880 return FALSE;
5882 if (isW)
5883 GetWindowTextW(infoPtr->hwndEdit, pszText, len);
5884 else
5885 GetWindowTextA(infoPtr->hwndEdit, (CHAR*)pszText, len);
5889 TRACE("(pszText=%s, isW=%d)\n", debugtext_t(pszText, isW), isW);
5891 ZeroMemory(&dispInfo, sizeof(dispInfo));
5892 dispInfo.item.mask = LVIF_PARAM | LVIF_STATE | LVIF_TEXT;
5893 dispInfo.item.iItem = editedItem;
5894 dispInfo.item.iSubItem = 0;
5895 dispInfo.item.stateMask = ~0;
5896 dispInfo.item.pszText = szDispText;
5897 dispInfo.item.cchTextMax = DISP_TEXT_SIZE;
5898 if (!LISTVIEW_GetItemT(infoPtr, &dispInfo.item, isW))
5900 res = FALSE;
5901 goto cleanup;
5904 if (isW)
5905 same = (lstrcmpW(dispInfo.item.pszText, pszText) == 0);
5906 else
5908 LPWSTR tmp = textdupTtoW(pszText, FALSE);
5909 same = (lstrcmpW(dispInfo.item.pszText, tmp) == 0);
5910 textfreeT(tmp, FALSE);
5913 /* add the text from the edit in */
5914 dispInfo.item.mask |= LVIF_TEXT;
5915 dispInfo.item.pszText = same ? NULL : pszText;
5916 dispInfo.item.cchTextMax = textlenT(dispInfo.item.pszText, isW);
5918 infoPtr->notify_mask &= ~NOTIFY_MASK_END_LABEL_EDIT;
5920 /* Do we need to update the Item Text */
5921 res = notify_dispinfoT(infoPtr, LVN_ENDLABELEDITW, &dispInfo, isW);
5923 infoPtr->notify_mask |= NOTIFY_MASK_END_LABEL_EDIT;
5925 infoPtr->nEditLabelItem = -1;
5926 infoPtr->hwndEdit = 0;
5928 if (!res) goto cleanup;
5930 if (!IsWindow(hwndSelf))
5932 res = FALSE;
5933 goto cleanup;
5935 if (!pszText) return TRUE;
5936 if (same)
5938 res = TRUE;
5939 goto cleanup;
5942 if (!(infoPtr->dwStyle & LVS_OWNERDATA))
5944 HDPA hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, editedItem);
5945 ITEM_INFO* lpItem = DPA_GetPtr(hdpaSubItems, 0);
5946 if (lpItem && lpItem->hdr.pszText == LPSTR_TEXTCALLBACKW)
5948 LISTVIEW_InvalidateItem(infoPtr, editedItem);
5949 res = TRUE;
5950 goto cleanup;
5954 ZeroMemory(&dispInfo, sizeof(dispInfo));
5955 dispInfo.item.mask = LVIF_TEXT;
5956 dispInfo.item.iItem = editedItem;
5957 dispInfo.item.iSubItem = 0;
5958 dispInfo.item.pszText = pszText;
5959 dispInfo.item.cchTextMax = textlenT(pszText, isW);
5960 res = LISTVIEW_SetItemT(infoPtr, &dispInfo.item, isW);
5962 cleanup:
5963 Free(pszText);
5965 return res;
5968 /***
5969 * DESCRIPTION:
5970 * Subclassed edit control windproc function
5972 * PARAMETER(S):
5973 * [I] hwnd : the edit window handle
5974 * [I] uMsg : the message that is to be processed
5975 * [I] wParam : first message parameter
5976 * [I] lParam : second message parameter
5977 * [I] isW : TRUE if input is Unicode
5979 * RETURN:
5980 * Zero.
5982 static LRESULT EditLblWndProcT(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL isW)
5984 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)GetWindowLongPtrW(GetParent(hwnd), 0);
5985 BOOL save = TRUE;
5987 TRACE("(hwnd=%p, uMsg=%x, wParam=%lx, lParam=%lx, isW=%d)\n",
5988 hwnd, uMsg, wParam, lParam, isW);
5990 switch (uMsg)
5992 case WM_GETDLGCODE:
5993 return DLGC_WANTARROWS | DLGC_WANTALLKEYS;
5995 case WM_DESTROY:
5997 WNDPROC editProc = infoPtr->EditWndProc;
5998 infoPtr->EditWndProc = 0;
5999 SetWindowLongPtrW(hwnd, GWLP_WNDPROC, (DWORD_PTR)editProc);
6000 return CallWindowProcT(editProc, hwnd, uMsg, wParam, lParam, isW);
6003 case WM_KEYDOWN:
6004 if (VK_ESCAPE == (INT)wParam)
6006 save = FALSE;
6007 break;
6009 else if (VK_RETURN == (INT)wParam)
6010 break;
6012 default:
6013 return CallWindowProcT(infoPtr->EditWndProc, hwnd, uMsg, wParam, lParam, isW);
6016 /* kill the edit */
6017 if (infoPtr->hwndEdit)
6018 LISTVIEW_EndEditLabelT(infoPtr, save, isW);
6020 SendMessageW(hwnd, WM_CLOSE, 0, 0);
6021 return 0;
6024 /***
6025 * DESCRIPTION:
6026 * Subclassed edit control Unicode windproc function
6028 * PARAMETER(S):
6029 * [I] hwnd : the edit window handle
6030 * [I] uMsg : the message that is to be processed
6031 * [I] wParam : first message parameter
6032 * [I] lParam : second message parameter
6034 * RETURN:
6036 static LRESULT CALLBACK EditLblWndProcW(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
6038 return EditLblWndProcT(hwnd, uMsg, wParam, lParam, TRUE);
6041 /***
6042 * DESCRIPTION:
6043 * Subclassed edit control ANSI windproc function
6045 * PARAMETER(S):
6046 * [I] hwnd : the edit window handle
6047 * [I] uMsg : the message that is to be processed
6048 * [I] wParam : first message parameter
6049 * [I] lParam : second message parameter
6051 * RETURN:
6053 static LRESULT CALLBACK EditLblWndProcA(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
6055 return EditLblWndProcT(hwnd, uMsg, wParam, lParam, FALSE);
6058 /***
6059 * DESCRIPTION:
6060 * Creates a subclassed edit control
6062 * PARAMETER(S):
6063 * [I] infoPtr : valid pointer to the listview structure
6064 * [I] text : initial text for the edit
6065 * [I] style : the window style
6066 * [I] isW : TRUE if input is Unicode
6068 * RETURN:
6070 static HWND CreateEditLabelT(LISTVIEW_INFO *infoPtr, LPCWSTR text, BOOL isW)
6072 static const DWORD style = WS_CHILDWINDOW|WS_CLIPSIBLINGS|ES_LEFT|ES_AUTOHSCROLL|WS_BORDER|WS_VISIBLE;
6073 HINSTANCE hinst = (HINSTANCE)GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_HINSTANCE);
6074 HWND hedit;
6076 TRACE("(%p, text=%s, isW=%d)\n", infoPtr, debugtext_t(text, isW), isW);
6078 /* window will be resized and positioned after LVN_BEGINLABELEDIT */
6079 if (isW)
6080 hedit = CreateWindowW(WC_EDITW, text, style, 0, 0, 0, 0, infoPtr->hwndSelf, 0, hinst, 0);
6081 else
6082 hedit = CreateWindowA(WC_EDITA, (LPCSTR)text, style, 0, 0, 0, 0, infoPtr->hwndSelf, 0, hinst, 0);
6084 if (!hedit) return 0;
6086 infoPtr->EditWndProc = (WNDPROC)
6087 (isW ? SetWindowLongPtrW(hedit, GWLP_WNDPROC, (DWORD_PTR)EditLblWndProcW) :
6088 SetWindowLongPtrA(hedit, GWLP_WNDPROC, (DWORD_PTR)EditLblWndProcA) );
6090 SendMessageW(hedit, WM_SETFONT, (WPARAM)infoPtr->hFont, FALSE);
6091 SendMessageW(hedit, EM_SETLIMITTEXT, DISP_TEXT_SIZE-1, 0);
6093 return hedit;
6096 /***
6097 * DESCRIPTION:
6098 * Begin in place editing of specified list view item
6100 * PARAMETER(S):
6101 * [I] infoPtr : valid pointer to the listview structure
6102 * [I] nItem : item index
6103 * [I] isW : TRUE if it's a Unicode req, FALSE if ASCII
6105 * RETURN:
6106 * SUCCESS : TRUE
6107 * FAILURE : FALSE
6109 static HWND LISTVIEW_EditLabelT(LISTVIEW_INFO *infoPtr, INT nItem, BOOL isW)
6111 WCHAR disptextW[DISP_TEXT_SIZE] = { 0 };
6112 HWND hwndSelf = infoPtr->hwndSelf;
6113 NMLVDISPINFOW dispInfo;
6114 HFONT hOldFont = NULL;
6115 TEXTMETRICW tm;
6116 RECT rect;
6117 SIZE sz;
6118 HDC hdc;
6120 TRACE("(nItem=%d, isW=%d)\n", nItem, isW);
6122 if (~infoPtr->dwStyle & LVS_EDITLABELS) return 0;
6124 /* remove existing edit box */
6125 if (infoPtr->hwndEdit)
6127 SetFocus(infoPtr->hwndSelf);
6128 infoPtr->hwndEdit = 0;
6131 if (nItem < 0 || nItem >= infoPtr->nItemCount) return 0;
6133 infoPtr->nEditLabelItem = nItem;
6135 LISTVIEW_SetSelection(infoPtr, nItem);
6136 LISTVIEW_SetItemFocus(infoPtr, nItem);
6137 LISTVIEW_InvalidateItem(infoPtr, nItem);
6139 rect.left = LVIR_LABEL;
6140 if (!LISTVIEW_GetItemRect(infoPtr, nItem, &rect)) return 0;
6142 ZeroMemory(&dispInfo, sizeof(dispInfo));
6143 dispInfo.item.mask = LVIF_PARAM | LVIF_STATE | LVIF_TEXT;
6144 dispInfo.item.iItem = nItem;
6145 dispInfo.item.iSubItem = 0;
6146 dispInfo.item.stateMask = ~0;
6147 dispInfo.item.pszText = disptextW;
6148 dispInfo.item.cchTextMax = DISP_TEXT_SIZE;
6149 if (!LISTVIEW_GetItemT(infoPtr, &dispInfo.item, isW)) return 0;
6151 infoPtr->hwndEdit = CreateEditLabelT(infoPtr, dispInfo.item.pszText, isW);
6152 if (!infoPtr->hwndEdit) return 0;
6154 if (notify_dispinfoT(infoPtr, LVN_BEGINLABELEDITW, &dispInfo, isW))
6156 if (!IsWindow(hwndSelf))
6157 return 0;
6158 SendMessageW(infoPtr->hwndEdit, WM_CLOSE, 0, 0);
6159 infoPtr->hwndEdit = 0;
6160 return 0;
6163 TRACE("disp text=%s\n", debugtext_t(dispInfo.item.pszText, isW));
6165 /* position and display edit box */
6166 hdc = GetDC(infoPtr->hwndSelf);
6168 /* select the font to get appropriate metric dimensions */
6169 if (infoPtr->hFont)
6170 hOldFont = SelectObject(hdc, infoPtr->hFont);
6172 /* use real edit box content, it could be altered during LVN_BEGINLABELEDIT notification */
6173 GetWindowTextW(infoPtr->hwndEdit, disptextW, DISP_TEXT_SIZE);
6174 TRACE("edit box text=%s\n", debugstr_w(disptextW));
6176 /* get string length in pixels */
6177 GetTextExtentPoint32W(hdc, disptextW, lstrlenW(disptextW), &sz);
6179 /* add extra spacing for the next character */
6180 GetTextMetricsW(hdc, &tm);
6181 sz.cx += tm.tmMaxCharWidth * 2;
6183 if (infoPtr->hFont)
6184 SelectObject(hdc, hOldFont);
6186 ReleaseDC(infoPtr->hwndSelf, hdc);
6188 sz.cy = rect.bottom - rect.top + 2;
6189 rect.left -= 2;
6190 rect.top -= 1;
6191 TRACE("moving edit=(%d,%d)-(%d,%d)\n", rect.left, rect.top, sz.cx, sz.cy);
6192 MoveWindow(infoPtr->hwndEdit, rect.left, rect.top, sz.cx, sz.cy, FALSE);
6193 ShowWindow(infoPtr->hwndEdit, SW_NORMAL);
6194 SetFocus(infoPtr->hwndEdit);
6195 SendMessageW(infoPtr->hwndEdit, EM_SETSEL, 0, -1);
6196 return infoPtr->hwndEdit;
6200 /***
6201 * DESCRIPTION:
6202 * Ensures the specified item is visible, scrolling into view if necessary.
6204 * PARAMETER(S):
6205 * [I] infoPtr : valid pointer to the listview structure
6206 * [I] nItem : item index
6207 * [I] bPartial : partially or entirely visible
6209 * RETURN:
6210 * SUCCESS : TRUE
6211 * FAILURE : FALSE
6213 static BOOL LISTVIEW_EnsureVisible(LISTVIEW_INFO *infoPtr, INT nItem, BOOL bPartial)
6215 INT nScrollPosHeight = 0;
6216 INT nScrollPosWidth = 0;
6217 INT nHorzAdjust = 0;
6218 INT nVertAdjust = 0;
6219 INT nHorzDiff = 0;
6220 INT nVertDiff = 0;
6221 RECT rcItem, rcTemp;
6223 rcItem.left = LVIR_BOUNDS;
6224 if (!LISTVIEW_GetItemRect(infoPtr, nItem, &rcItem)) return FALSE;
6226 if (bPartial && IntersectRect(&rcTemp, &infoPtr->rcList, &rcItem)) return TRUE;
6228 if (rcItem.left < infoPtr->rcList.left || rcItem.right > infoPtr->rcList.right)
6230 /* scroll left/right, but in LV_VIEW_DETAILS mode */
6231 if (infoPtr->uView == LV_VIEW_LIST)
6232 nScrollPosWidth = infoPtr->nItemWidth;
6233 else if ((infoPtr->uView == LV_VIEW_SMALLICON) || (infoPtr->uView == LV_VIEW_ICON))
6234 nScrollPosWidth = 1;
6236 if (rcItem.left < infoPtr->rcList.left)
6238 nHorzAdjust = -1;
6239 if (infoPtr->uView != LV_VIEW_DETAILS) nHorzDiff = rcItem.left - infoPtr->rcList.left;
6241 else
6243 nHorzAdjust = 1;
6244 if (infoPtr->uView != LV_VIEW_DETAILS) nHorzDiff = rcItem.right - infoPtr->rcList.right;
6248 if (rcItem.top < infoPtr->rcList.top || rcItem.bottom > infoPtr->rcList.bottom)
6250 /* scroll up/down, but not in LVS_LIST mode */
6251 if (infoPtr->uView == LV_VIEW_DETAILS)
6252 nScrollPosHeight = infoPtr->nItemHeight;
6253 else if ((infoPtr->uView == LV_VIEW_ICON) || (infoPtr->uView == LV_VIEW_SMALLICON))
6254 nScrollPosHeight = 1;
6256 if (rcItem.top < infoPtr->rcList.top)
6258 nVertAdjust = -1;
6259 if (infoPtr->uView != LV_VIEW_LIST) nVertDiff = rcItem.top - infoPtr->rcList.top;
6261 else
6263 nVertAdjust = 1;
6264 if (infoPtr->uView != LV_VIEW_LIST) nVertDiff = rcItem.bottom - infoPtr->rcList.bottom;
6268 if (!nScrollPosWidth && !nScrollPosHeight) return TRUE;
6270 if (nScrollPosWidth)
6272 INT diff = nHorzDiff / nScrollPosWidth;
6273 if (nHorzDiff % nScrollPosWidth) diff += nHorzAdjust;
6274 LISTVIEW_HScroll(infoPtr, SB_INTERNAL, diff);
6277 if (nScrollPosHeight)
6279 INT diff = nVertDiff / nScrollPosHeight;
6280 if (nVertDiff % nScrollPosHeight) diff += nVertAdjust;
6281 LISTVIEW_VScroll(infoPtr, SB_INTERNAL, diff);
6284 return TRUE;
6287 /***
6288 * DESCRIPTION:
6289 * Searches for an item with specific characteristics.
6291 * PARAMETER(S):
6292 * [I] hwnd : window handle
6293 * [I] nStart : base item index
6294 * [I] lpFindInfo : item information to look for
6296 * RETURN:
6297 * SUCCESS : index of item
6298 * FAILURE : -1
6300 static INT LISTVIEW_FindItemW(const LISTVIEW_INFO *infoPtr, INT nStart,
6301 const LVFINDINFOW *lpFindInfo)
6303 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
6304 BOOL bWrap = FALSE, bNearest = FALSE;
6305 INT nItem = nStart + 1, nLast = infoPtr->nItemCount, nNearestItem = -1;
6306 ULONG xdist, ydist, dist, mindist = 0x7fffffff;
6307 POINT Position, Destination;
6308 int search_len = 0;
6309 LVITEMW lvItem;
6311 /* Search in virtual listviews should be done by application, not by
6312 listview control, so we just send LVN_ODFINDITEMW and return the result */
6313 if (infoPtr->dwStyle & LVS_OWNERDATA)
6315 NMLVFINDITEMW nmlv;
6317 nmlv.iStart = nStart;
6318 nmlv.lvfi = *lpFindInfo;
6319 return notify_hdr(infoPtr, LVN_ODFINDITEMW, (LPNMHDR)&nmlv.hdr);
6322 if (!lpFindInfo || nItem < 0) return -1;
6324 lvItem.mask = 0;
6325 if (lpFindInfo->flags & (LVFI_STRING | LVFI_PARTIAL | LVFI_SUBSTRING))
6327 lvItem.mask |= LVIF_TEXT;
6328 lvItem.pszText = szDispText;
6329 lvItem.cchTextMax = DISP_TEXT_SIZE;
6332 if (lpFindInfo->flags & LVFI_WRAP)
6333 bWrap = TRUE;
6335 if ((lpFindInfo->flags & LVFI_NEARESTXY) &&
6336 (infoPtr->uView == LV_VIEW_ICON || infoPtr->uView == LV_VIEW_SMALLICON))
6338 POINT Origin;
6339 RECT rcArea;
6341 LISTVIEW_GetOrigin(infoPtr, &Origin);
6342 Destination.x = lpFindInfo->pt.x - Origin.x;
6343 Destination.y = lpFindInfo->pt.y - Origin.y;
6344 switch(lpFindInfo->vkDirection)
6346 case VK_DOWN: Destination.y += infoPtr->nItemHeight; break;
6347 case VK_UP: Destination.y -= infoPtr->nItemHeight; break;
6348 case VK_RIGHT: Destination.x += infoPtr->nItemWidth; break;
6349 case VK_LEFT: Destination.x -= infoPtr->nItemWidth; break;
6350 case VK_HOME: Destination.x = Destination.y = 0; break;
6351 case VK_NEXT: Destination.y += infoPtr->rcList.bottom - infoPtr->rcList.top; break;
6352 case VK_PRIOR: Destination.y -= infoPtr->rcList.bottom - infoPtr->rcList.top; break;
6353 case VK_END:
6354 LISTVIEW_GetAreaRect(infoPtr, &rcArea);
6355 Destination.x = rcArea.right;
6356 Destination.y = rcArea.bottom;
6357 break;
6358 default: ERR("Unknown vkDirection=%d\n", lpFindInfo->vkDirection);
6360 bNearest = TRUE;
6362 else Destination.x = Destination.y = 0;
6364 /* if LVFI_PARAM is specified, all other flags are ignored */
6365 if (lpFindInfo->flags & LVFI_PARAM)
6367 lvItem.mask |= LVIF_PARAM;
6368 bNearest = FALSE;
6369 lvItem.mask &= ~LVIF_TEXT;
6372 nItem = bNearest ? -1 : nStart + 1;
6374 if (lpFindInfo->flags & (LVFI_PARTIAL | LVFI_SUBSTRING))
6375 search_len = lstrlenW(lpFindInfo->psz);
6377 again:
6378 for (; nItem < nLast; nItem++)
6380 lvItem.iItem = nItem;
6381 lvItem.iSubItem = 0;
6382 lvItem.pszText = szDispText;
6383 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) continue;
6385 if (lvItem.mask & LVIF_PARAM)
6387 if (lpFindInfo->lParam == lvItem.lParam)
6388 return nItem;
6389 else
6390 continue;
6393 if (lvItem.mask & LVIF_TEXT)
6395 if (lpFindInfo->flags & (LVFI_PARTIAL | LVFI_SUBSTRING))
6397 if (StrCmpNIW(lvItem.pszText, lpFindInfo->psz, search_len)) continue;
6399 else
6401 if (StrCmpIW(lvItem.pszText, lpFindInfo->psz)) continue;
6405 if (!bNearest) return nItem;
6407 /* This is very inefficient. To do a good job here,
6408 * we need a sorted array of (x,y) item positions */
6409 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position);
6411 /* compute the distance^2 to the destination */
6412 xdist = Destination.x - Position.x;
6413 ydist = Destination.y - Position.y;
6414 dist = xdist * xdist + ydist * ydist;
6416 /* remember the distance, and item if it's closer */
6417 if (dist < mindist)
6419 mindist = dist;
6420 nNearestItem = nItem;
6424 if (bWrap)
6426 nItem = 0;
6427 nLast = min(nStart + 1, infoPtr->nItemCount);
6428 bWrap = FALSE;
6429 goto again;
6432 return nNearestItem;
6435 /***
6436 * DESCRIPTION:
6437 * Searches for an item with specific characteristics.
6439 * PARAMETER(S):
6440 * [I] hwnd : window handle
6441 * [I] nStart : base item index
6442 * [I] lpFindInfo : item information to look for
6444 * RETURN:
6445 * SUCCESS : index of item
6446 * FAILURE : -1
6448 static INT LISTVIEW_FindItemA(const LISTVIEW_INFO *infoPtr, INT nStart,
6449 const LVFINDINFOA *lpFindInfo)
6451 LVFINDINFOW fiw;
6452 INT res;
6453 LPWSTR strW = NULL;
6455 memcpy(&fiw, lpFindInfo, sizeof(fiw));
6456 if (lpFindInfo->flags & (LVFI_STRING | LVFI_PARTIAL | LVFI_SUBSTRING))
6457 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 BOOL is_subitem_invalid = FALSE;
6667 NMLVDISPINFOW dispInfo;
6668 ITEM_INFO *lpItem;
6669 ITEMHDR* pItemHdr;
6670 HDPA hdpaSubItems;
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 if (lpLVItem->iSubItem && (lpLVItem->mask & LVIF_STATE))
6681 lpLVItem->state = 0;
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 && !lpLVItem->iSubItem)
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 = lpLVItem->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) || lpLVItem->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 (lpLVItem->iSubItem)
6787 SUBITEM_INFO *lpSubItem = LISTVIEW_GetSubItemPtr(hdpaSubItems, lpLVItem->iSubItem);
6788 if (lpSubItem)
6789 pItemHdr = &lpSubItem->hdr;
6790 else
6792 pItemHdr = &callbackHdr;
6793 is_subitem_invalid = TRUE;
6796 else
6797 pItemHdr = &lpItem->hdr;
6799 /* Do we need to query the state from the app? */
6800 if ((lpLVItem->mask & LVIF_STATE) && infoPtr->uCallbackMask && (!lpLVItem->iSubItem || is_subitem_invalid))
6802 dispInfo.item.mask |= LVIF_STATE;
6803 dispInfo.item.stateMask = infoPtr->uCallbackMask;
6806 /* Do we need to enquire about the image? */
6807 if ((lpLVItem->mask & LVIF_IMAGE) && pItemHdr->iImage == I_IMAGECALLBACK &&
6808 (!lpLVItem->iSubItem || (infoPtr->dwLvExStyle & LVS_EX_SUBITEMIMAGES)))
6810 dispInfo.item.mask |= LVIF_IMAGE;
6811 dispInfo.item.iImage = I_IMAGECALLBACK;
6814 /* Only items support indentation */
6815 if ((lpLVItem->mask & LVIF_INDENT) && lpItem->iIndent == I_INDENTCALLBACK && !lpLVItem->iSubItem)
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;
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 (lpLVItem->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 (!lpLVItem->iSubItem || (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 (lpLVItem->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;
7829 WCHAR *textW;
7831 textW = textdupTtoW(lpLVItem->pszText, isW);
7833 while (i < infoPtr->nItemCount)
7835 hItem = DPA_GetPtr( infoPtr->hdpaItems, i);
7836 item_s = DPA_GetPtr(hItem, 0);
7838 cmpv = textcmpWT(item_s->hdr.pszText, textW, TRUE);
7839 if (infoPtr->dwStyle & LVS_SORTDESCENDING) cmpv *= -1;
7841 if (cmpv >= 0) break;
7842 i++;
7845 textfreeT(textW, isW);
7847 nItem = i;
7849 else
7850 nItem = min(lpLVItem->iItem, infoPtr->nItemCount);
7852 TRACE("inserting at %d, sorted=%d, count=%d, iItem=%d\n", nItem, is_sorted, infoPtr->nItemCount, lpLVItem->iItem);
7853 nItem = DPA_InsertPtr( infoPtr->hdpaItems, nItem, hdpaSubItems );
7854 if (nItem == -1) goto fail;
7855 infoPtr->nItemCount++;
7857 /* shift indices first so they don't get tangled */
7858 LISTVIEW_ShiftIndices(infoPtr, nItem, 1);
7860 /* set the item attributes */
7861 if (lpLVItem->mask & (LVIF_GROUPID|LVIF_COLUMNS))
7863 /* full size structure expected - _WIN32IE >= 0x560 */
7864 item = *lpLVItem;
7866 else if (lpLVItem->mask & LVIF_INDENT)
7868 /* indent member expected - _WIN32IE >= 0x300 */
7869 memcpy(&item, lpLVItem, offsetof( LVITEMW, iGroupId ));
7871 else
7873 /* minimal structure expected */
7874 memcpy(&item, lpLVItem, offsetof( LVITEMW, iIndent ));
7876 item.iItem = nItem;
7877 if (infoPtr->dwLvExStyle & LVS_EX_CHECKBOXES)
7879 if (item.mask & LVIF_STATE)
7881 item.stateMask |= LVIS_STATEIMAGEMASK;
7882 item.state &= ~LVIS_STATEIMAGEMASK;
7883 item.state |= INDEXTOSTATEIMAGEMASK(1);
7885 else
7887 item.mask |= LVIF_STATE;
7888 item.stateMask = LVIS_STATEIMAGEMASK;
7889 item.state = INDEXTOSTATEIMAGEMASK(1);
7893 if (!set_main_item(infoPtr, &item, TRUE, isW, &has_changed)) goto undo;
7895 /* make room for the position, if we are in the right mode */
7896 if ((infoPtr->uView == LV_VIEW_SMALLICON) || (infoPtr->uView == LV_VIEW_ICON))
7898 if (DPA_InsertPtr(infoPtr->hdpaPosX, nItem, 0) == -1)
7899 goto undo;
7900 if (DPA_InsertPtr(infoPtr->hdpaPosY, nItem, 0) == -1)
7902 DPA_DeletePtr(infoPtr->hdpaPosX, nItem);
7903 goto undo;
7907 /* send LVN_INSERTITEM notification */
7908 memset(&nmlv, 0, sizeof(NMLISTVIEW));
7909 nmlv.iItem = nItem;
7910 nmlv.lParam = lpItem->lParam;
7911 notify_listview(infoPtr, LVN_INSERTITEM, &nmlv);
7912 if (!IsWindow(hwndSelf))
7913 return -1;
7915 /* align items (set position of each item) */
7916 if (infoPtr->uView == LV_VIEW_SMALLICON || infoPtr->uView == LV_VIEW_ICON)
7918 POINT pt;
7920 if (infoPtr->dwStyle & LVS_ALIGNLEFT)
7921 LISTVIEW_NextIconPosLeft(infoPtr, &pt);
7922 else
7923 LISTVIEW_NextIconPosTop(infoPtr, &pt);
7925 LISTVIEW_MoveIconTo(infoPtr, nItem, &pt, TRUE);
7928 /* now is the invalidation fun */
7929 LISTVIEW_ScrollOnInsert(infoPtr, nItem, 1);
7930 return nItem;
7932 undo:
7933 LISTVIEW_ShiftIndices(infoPtr, nItem, -1);
7934 LISTVIEW_ShiftFocus(infoPtr, infoPtr->nFocusedItem, nItem, -1);
7935 DPA_DeletePtr(infoPtr->hdpaItems, nItem);
7936 infoPtr->nItemCount--;
7937 fail:
7938 DPA_DeletePtr(hdpaSubItems, 0);
7939 DPA_Destroy (hdpaSubItems);
7940 Free (lpItem);
7941 return -1;
7944 /***
7945 * DESCRIPTION:
7946 * Checks item visibility.
7948 * PARAMETER(S):
7949 * [I] infoPtr : valid pointer to the listview structure
7950 * [I] nFirst : item index to check for
7952 * RETURN:
7953 * Item visible : TRUE
7954 * Item invisible or failure : FALSE
7956 static BOOL LISTVIEW_IsItemVisible(const LISTVIEW_INFO *infoPtr, INT nItem)
7958 POINT Origin, Position;
7959 RECT rcItem;
7960 HDC hdc;
7961 BOOL ret;
7963 TRACE("nItem=%d\n", nItem);
7965 if (nItem < 0 || nItem >= DPA_GetPtrCount(infoPtr->hdpaItems)) return FALSE;
7967 LISTVIEW_GetOrigin(infoPtr, &Origin);
7968 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position);
7969 rcItem.left = Position.x + Origin.x;
7970 rcItem.top = Position.y + Origin.y;
7971 rcItem.right = rcItem.left + infoPtr->nItemWidth;
7972 rcItem.bottom = rcItem.top + infoPtr->nItemHeight;
7974 hdc = GetDC(infoPtr->hwndSelf);
7975 if (!hdc) return FALSE;
7976 ret = RectVisible(hdc, &rcItem);
7977 ReleaseDC(infoPtr->hwndSelf, hdc);
7979 return ret;
7982 /***
7983 * DESCRIPTION:
7984 * Redraws a range of items.
7986 * PARAMETER(S):
7987 * [I] infoPtr : valid pointer to the listview structure
7988 * [I] nFirst : first item
7989 * [I] nLast : last item
7991 * RETURN:
7992 * SUCCESS : TRUE
7993 * FAILURE : FALSE
7995 static BOOL LISTVIEW_RedrawItems(const LISTVIEW_INFO *infoPtr, INT nFirst, INT nLast)
7997 INT i;
7999 for (i = max(nFirst, 0); i <= min(nLast, infoPtr->nItemCount - 1); i++)
8000 LISTVIEW_InvalidateItem(infoPtr, i);
8002 return TRUE;
8005 /***
8006 * DESCRIPTION:
8007 * Scroll the content of a listview.
8009 * PARAMETER(S):
8010 * [I] infoPtr : valid pointer to the listview structure
8011 * [I] dx : horizontal scroll amount in pixels
8012 * [I] dy : vertical scroll amount in pixels
8014 * RETURN:
8015 * SUCCESS : TRUE
8016 * FAILURE : FALSE
8018 * COMMENTS:
8019 * If the control is in report view (LV_VIEW_DETAILS) the control can
8020 * be scrolled only in line increments. "dy" will be rounded to the
8021 * nearest number of pixels that are a whole line. Ex: if line height
8022 * is 16 and an 8 is passed, the list will be scrolled by 16. If a 7
8023 * is passed, then the scroll will be 0. (per MSDN 7/2002)
8025 static BOOL LISTVIEW_Scroll(LISTVIEW_INFO *infoPtr, INT dx, INT dy)
8027 switch(infoPtr->uView) {
8028 case LV_VIEW_DETAILS:
8029 dy += (dy < 0 ? -1 : 1) * infoPtr->nItemHeight/2;
8030 dy /= infoPtr->nItemHeight;
8031 break;
8032 case LV_VIEW_LIST:
8033 if (dy != 0) return FALSE;
8034 break;
8035 default: /* icon */
8036 break;
8039 if (dx != 0) LISTVIEW_HScroll(infoPtr, SB_INTERNAL, dx);
8040 if (dy != 0) LISTVIEW_VScroll(infoPtr, SB_INTERNAL, dy);
8042 return TRUE;
8045 /***
8046 * DESCRIPTION:
8047 * Sets the background color.
8049 * PARAMETER(S):
8050 * [I] infoPtr : valid pointer to the listview structure
8051 * [I] color : background color
8053 * RETURN:
8054 * SUCCESS : TRUE
8055 * FAILURE : FALSE
8057 static BOOL LISTVIEW_SetBkColor(LISTVIEW_INFO *infoPtr, COLORREF color)
8059 TRACE("(color=%x)\n", color);
8061 if(infoPtr->clrBk != color) {
8062 if (infoPtr->clrBk != CLR_NONE) DeleteObject(infoPtr->hBkBrush);
8063 infoPtr->clrBk = color;
8064 if (color == CLR_NONE)
8065 infoPtr->hBkBrush = (HBRUSH)GetClassLongPtrW(infoPtr->hwndSelf, GCLP_HBRBACKGROUND);
8066 else
8068 infoPtr->hBkBrush = CreateSolidBrush(color);
8069 infoPtr->dwLvExStyle &= ~LVS_EX_TRANSPARENTBKGND;
8073 return TRUE;
8076 /* LISTVIEW_SetBkImage */
8078 /*** Helper for {Insert,Set}ColumnT *only* */
8079 static void column_fill_hditem(const LISTVIEW_INFO *infoPtr, HDITEMW *lphdi, INT nColumn,
8080 const LVCOLUMNW *lpColumn, BOOL isW)
8082 if (lpColumn->mask & LVCF_FMT)
8084 /* format member is valid */
8085 lphdi->mask |= HDI_FORMAT;
8087 /* set text alignment (leftmost column must be left-aligned) */
8088 if (nColumn == 0 || (lpColumn->fmt & LVCFMT_JUSTIFYMASK) == LVCFMT_LEFT)
8089 lphdi->fmt |= HDF_LEFT;
8090 else if ((lpColumn->fmt & LVCFMT_JUSTIFYMASK) == LVCFMT_RIGHT)
8091 lphdi->fmt |= HDF_RIGHT;
8092 else if ((lpColumn->fmt & LVCFMT_JUSTIFYMASK) == LVCFMT_CENTER)
8093 lphdi->fmt |= HDF_CENTER;
8095 if (lpColumn->fmt & LVCFMT_BITMAP_ON_RIGHT)
8096 lphdi->fmt |= HDF_BITMAP_ON_RIGHT;
8098 if (lpColumn->fmt & LVCFMT_COL_HAS_IMAGES)
8100 lphdi->fmt |= HDF_IMAGE;
8101 lphdi->iImage = I_IMAGECALLBACK;
8104 if (lpColumn->fmt & LVCFMT_FIXED_WIDTH)
8105 lphdi->fmt |= HDF_FIXEDWIDTH;
8108 if (lpColumn->mask & LVCF_WIDTH)
8110 lphdi->mask |= HDI_WIDTH;
8111 if(lpColumn->cx == LVSCW_AUTOSIZE_USEHEADER)
8113 /* make it fill the remainder of the controls width */
8114 RECT rcHeader;
8115 INT item_index;
8117 for(item_index = 0; item_index < (nColumn - 1); item_index++)
8119 LISTVIEW_GetHeaderRect(infoPtr, item_index, &rcHeader);
8120 lphdi->cxy += rcHeader.right - rcHeader.left;
8123 /* retrieve the layout of the header */
8124 GetClientRect(infoPtr->hwndSelf, &rcHeader);
8125 TRACE("start cxy=%d rcHeader=%s\n", lphdi->cxy, wine_dbgstr_rect(&rcHeader));
8127 lphdi->cxy = (rcHeader.right - rcHeader.left) - lphdi->cxy;
8129 else
8130 lphdi->cxy = lpColumn->cx;
8133 if (lpColumn->mask & LVCF_TEXT)
8135 lphdi->mask |= HDI_TEXT | HDI_FORMAT;
8136 lphdi->fmt |= HDF_STRING;
8137 lphdi->pszText = lpColumn->pszText;
8138 lphdi->cchTextMax = textlenT(lpColumn->pszText, isW);
8141 if (lpColumn->mask & LVCF_IMAGE)
8143 lphdi->mask |= HDI_IMAGE;
8144 lphdi->iImage = lpColumn->iImage;
8147 if (lpColumn->mask & LVCF_ORDER)
8149 lphdi->mask |= HDI_ORDER;
8150 lphdi->iOrder = lpColumn->iOrder;
8155 /***
8156 * DESCRIPTION:
8157 * Inserts a new column.
8159 * PARAMETER(S):
8160 * [I] infoPtr : valid pointer to the listview structure
8161 * [I] nColumn : column index
8162 * [I] lpColumn : column information
8163 * [I] isW : TRUE if lpColumn is Unicode, FALSE otherwise
8165 * RETURN:
8166 * SUCCESS : new column index
8167 * FAILURE : -1
8169 static INT LISTVIEW_InsertColumnT(LISTVIEW_INFO *infoPtr, INT nColumn,
8170 const LVCOLUMNW *lpColumn, BOOL isW)
8172 COLUMN_INFO *lpColumnInfo;
8173 INT nNewColumn;
8174 HDITEMW hdi;
8176 TRACE("(nColumn=%d, lpColumn=%s, isW=%d)\n", nColumn, debuglvcolumn_t(lpColumn, isW), isW);
8178 if (!lpColumn || nColumn < 0) return -1;
8179 nColumn = min(nColumn, DPA_GetPtrCount(infoPtr->hdpaColumns));
8181 ZeroMemory(&hdi, sizeof(HDITEMW));
8182 column_fill_hditem(infoPtr, &hdi, nColumn, lpColumn, isW);
8185 * A mask not including LVCF_WIDTH turns into a mask of width, width 10
8186 * (can be seen in SPY) otherwise column never gets added.
8188 if (!(lpColumn->mask & LVCF_WIDTH)) {
8189 hdi.mask |= HDI_WIDTH;
8190 hdi.cxy = 10;
8194 * when the iSubItem is available Windows copies it to the header lParam. It seems
8195 * to happen only in LVM_INSERTCOLUMN - not in LVM_SETCOLUMN
8197 if (lpColumn->mask & LVCF_SUBITEM)
8199 hdi.mask |= HDI_LPARAM;
8200 hdi.lParam = lpColumn->iSubItem;
8203 /* create header if not present */
8204 LISTVIEW_CreateHeader(infoPtr);
8205 if (!(LVS_NOCOLUMNHEADER & infoPtr->dwStyle) &&
8206 (infoPtr->uView == LV_VIEW_DETAILS) && (WS_VISIBLE & infoPtr->dwStyle))
8208 ShowWindow(infoPtr->hwndHeader, SW_SHOWNORMAL);
8211 /* insert item in header control */
8212 nNewColumn = SendMessageW(infoPtr->hwndHeader,
8213 isW ? HDM_INSERTITEMW : HDM_INSERTITEMA,
8214 nColumn, (LPARAM)&hdi);
8215 if (nNewColumn == -1) return -1;
8216 if (nNewColumn != nColumn) ERR("nColumn=%d, nNewColumn=%d\n", nColumn, nNewColumn);
8218 /* create our own column info */
8219 if (!(lpColumnInfo = Alloc(sizeof(COLUMN_INFO)))) goto fail;
8220 if (DPA_InsertPtr(infoPtr->hdpaColumns, nNewColumn, lpColumnInfo) == -1) goto fail;
8222 if (lpColumn->mask & LVCF_FMT) lpColumnInfo->fmt = lpColumn->fmt;
8223 if (lpColumn->mask & LVCF_MINWIDTH) lpColumnInfo->cxMin = lpColumn->cxMin;
8224 if (!SendMessageW(infoPtr->hwndHeader, HDM_GETITEMRECT, nNewColumn, (LPARAM)&lpColumnInfo->rcHeader))
8225 goto fail;
8227 /* now we have to actually adjust the data */
8228 if (!(infoPtr->dwStyle & LVS_OWNERDATA) && infoPtr->nItemCount > 0)
8230 SUBITEM_INFO *lpSubItem;
8231 HDPA hdpaSubItems;
8232 INT nItem, i;
8233 LVITEMW item;
8234 BOOL changed;
8236 item.iSubItem = nNewColumn;
8237 item.mask = LVIF_TEXT | LVIF_IMAGE;
8238 item.iImage = I_IMAGECALLBACK;
8239 item.pszText = LPSTR_TEXTCALLBACKW;
8241 for (nItem = 0; nItem < infoPtr->nItemCount; nItem++)
8243 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, nItem);
8244 for (i = 1; i < DPA_GetPtrCount(hdpaSubItems); i++)
8246 lpSubItem = DPA_GetPtr(hdpaSubItems, i);
8247 if (lpSubItem->iSubItem >= nNewColumn)
8248 lpSubItem->iSubItem++;
8251 /* add new subitem for each item */
8252 item.iItem = nItem;
8253 set_sub_item(infoPtr, &item, isW, &changed);
8257 /* make space for the new column */
8258 LISTVIEW_ScrollColumns(infoPtr, nNewColumn + 1, lpColumnInfo->rcHeader.right - lpColumnInfo->rcHeader.left);
8259 LISTVIEW_UpdateItemSize(infoPtr);
8261 return nNewColumn;
8263 fail:
8264 if (nNewColumn != -1) SendMessageW(infoPtr->hwndHeader, HDM_DELETEITEM, nNewColumn, 0);
8265 if (lpColumnInfo)
8267 DPA_DeletePtr(infoPtr->hdpaColumns, nNewColumn);
8268 Free(lpColumnInfo);
8270 return -1;
8273 /***
8274 * DESCRIPTION:
8275 * Sets the attributes of a header item.
8277 * PARAMETER(S):
8278 * [I] infoPtr : valid pointer to the listview structure
8279 * [I] nColumn : column index
8280 * [I] lpColumn : column attributes
8281 * [I] isW: if TRUE, then lpColumn is a LPLVCOLUMNW, else it is a LPLVCOLUMNA
8283 * RETURN:
8284 * SUCCESS : TRUE
8285 * FAILURE : FALSE
8287 static BOOL LISTVIEW_SetColumnT(const LISTVIEW_INFO *infoPtr, INT nColumn,
8288 const LVCOLUMNW *lpColumn, BOOL isW)
8290 HDITEMW hdi, hdiget;
8291 BOOL bResult;
8293 TRACE("(nColumn=%d, lpColumn=%s, isW=%d)\n", nColumn, debuglvcolumn_t(lpColumn, isW), isW);
8295 if (!lpColumn || nColumn < 0 || nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return FALSE;
8297 ZeroMemory(&hdi, sizeof(HDITEMW));
8298 if (lpColumn->mask & LVCF_FMT)
8300 hdi.mask |= HDI_FORMAT;
8301 hdiget.mask = HDI_FORMAT;
8302 if (SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, nColumn, (LPARAM)&hdiget))
8303 hdi.fmt = hdiget.fmt & HDF_STRING;
8305 column_fill_hditem(infoPtr, &hdi, nColumn, lpColumn, isW);
8307 /* set header item attributes */
8308 bResult = SendMessageW(infoPtr->hwndHeader, isW ? HDM_SETITEMW : HDM_SETITEMA, nColumn, (LPARAM)&hdi);
8309 if (!bResult) return FALSE;
8311 if (lpColumn->mask & LVCF_FMT)
8313 COLUMN_INFO *lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, nColumn);
8314 INT oldFmt = lpColumnInfo->fmt;
8316 lpColumnInfo->fmt = lpColumn->fmt;
8317 if ((oldFmt ^ lpColumn->fmt) & (LVCFMT_JUSTIFYMASK | LVCFMT_IMAGE))
8319 if (infoPtr->uView == LV_VIEW_DETAILS) LISTVIEW_InvalidateColumn(infoPtr, nColumn);
8323 if (lpColumn->mask & LVCF_MINWIDTH)
8324 LISTVIEW_GetColumnInfo(infoPtr, nColumn)->cxMin = lpColumn->cxMin;
8326 return TRUE;
8329 /***
8330 * DESCRIPTION:
8331 * Sets the column order array
8333 * PARAMETERS:
8334 * [I] infoPtr : valid pointer to the listview structure
8335 * [I] iCount : number of elements in column order array
8336 * [I] lpiArray : pointer to column order array
8338 * RETURN:
8339 * SUCCESS : TRUE
8340 * FAILURE : FALSE
8342 static BOOL LISTVIEW_SetColumnOrderArray(LISTVIEW_INFO *infoPtr, INT iCount, const INT *lpiArray)
8344 if (!infoPtr->hwndHeader) return FALSE;
8345 infoPtr->colRectsDirty = TRUE;
8346 return SendMessageW(infoPtr->hwndHeader, HDM_SETORDERARRAY, iCount, (LPARAM)lpiArray);
8349 /***
8350 * DESCRIPTION:
8351 * Sets the width of a column
8353 * PARAMETERS:
8354 * [I] infoPtr : valid pointer to the listview structure
8355 * [I] nColumn : column index
8356 * [I] cx : column width
8358 * RETURN:
8359 * SUCCESS : TRUE
8360 * FAILURE : FALSE
8362 static BOOL LISTVIEW_SetColumnWidth(LISTVIEW_INFO *infoPtr, INT nColumn, INT cx)
8364 WCHAR szDispText[DISP_TEXT_SIZE] = { 0 };
8365 INT max_cx = 0;
8366 HDITEMW hdi;
8368 TRACE("(nColumn=%d, cx=%d)\n", nColumn, cx);
8370 /* set column width only if in report or list mode */
8371 if (infoPtr->uView != LV_VIEW_DETAILS && infoPtr->uView != LV_VIEW_LIST) return FALSE;
8373 /* take care of invalid cx values - LVSCW_AUTOSIZE_* values are negative,
8374 with _USEHEADER being the lowest */
8375 if (infoPtr->uView == LV_VIEW_DETAILS && cx < LVSCW_AUTOSIZE_USEHEADER) cx = LVSCW_AUTOSIZE;
8376 else if (infoPtr->uView == LV_VIEW_LIST && cx <= 0) return FALSE;
8378 /* resize all columns if in LV_VIEW_LIST mode */
8379 if(infoPtr->uView == LV_VIEW_LIST)
8381 infoPtr->nItemWidth = cx;
8382 LISTVIEW_InvalidateList(infoPtr);
8383 return TRUE;
8386 if (nColumn < 0 || nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return FALSE;
8388 if (cx == LVSCW_AUTOSIZE || (cx == LVSCW_AUTOSIZE_USEHEADER && nColumn < DPA_GetPtrCount(infoPtr->hdpaColumns) -1))
8390 INT nLabelWidth;
8391 LVITEMW lvItem;
8393 lvItem.mask = LVIF_TEXT;
8394 lvItem.iItem = 0;
8395 lvItem.iSubItem = nColumn;
8396 lvItem.cchTextMax = DISP_TEXT_SIZE;
8397 for (; lvItem.iItem < infoPtr->nItemCount; lvItem.iItem++)
8399 lvItem.pszText = szDispText;
8400 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) continue;
8401 nLabelWidth = LISTVIEW_GetStringWidthT(infoPtr, lvItem.pszText, TRUE);
8402 if (max_cx < nLabelWidth) max_cx = nLabelWidth;
8404 if (infoPtr->himlSmall && (nColumn == 0 || (LISTVIEW_GetColumnInfo(infoPtr, nColumn)->fmt & LVCFMT_IMAGE)))
8405 max_cx += infoPtr->iconSize.cx;
8406 max_cx += TRAILING_LABEL_PADDING;
8407 if (nColumn == 0 && (infoPtr->dwLvExStyle & LVS_EX_CHECKBOXES))
8408 max_cx += GetSystemMetrics(SM_CXSMICON);
8411 /* autosize based on listview items width */
8412 if(cx == LVSCW_AUTOSIZE)
8413 cx = max_cx;
8414 else if(cx == LVSCW_AUTOSIZE_USEHEADER)
8416 /* if iCol is the last column make it fill the remainder of the controls width */
8417 if(nColumn == DPA_GetPtrCount(infoPtr->hdpaColumns) - 1)
8419 RECT rcHeader;
8420 POINT Origin;
8422 LISTVIEW_GetOrigin(infoPtr, &Origin);
8423 LISTVIEW_GetHeaderRect(infoPtr, nColumn, &rcHeader);
8425 cx = infoPtr->rcList.right - Origin.x - rcHeader.left;
8427 else
8429 /* Despite what the MS docs say, if this is not the last
8430 column, then MS resizes the column to the width of the
8431 largest text string in the column, including headers
8432 and items. This is different from LVSCW_AUTOSIZE in that
8433 LVSCW_AUTOSIZE ignores the header string length. */
8434 cx = 0;
8436 /* retrieve header text */
8437 hdi.mask = HDI_TEXT|HDI_FORMAT|HDI_IMAGE|HDI_BITMAP;
8438 hdi.cchTextMax = DISP_TEXT_SIZE;
8439 hdi.pszText = szDispText;
8440 if (SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, nColumn, (LPARAM)&hdi))
8442 HDC hdc = GetDC(infoPtr->hwndSelf);
8443 HFONT old_font = SelectObject(hdc, (HFONT)SendMessageW(infoPtr->hwndHeader, WM_GETFONT, 0, 0));
8444 HIMAGELIST himl = (HIMAGELIST)SendMessageW(infoPtr->hwndHeader, HDM_GETIMAGELIST, 0, 0);
8445 INT bitmap_margin = 0;
8446 SIZE size;
8448 if (GetTextExtentPoint32W(hdc, hdi.pszText, lstrlenW(hdi.pszText), &size))
8449 cx = size.cx + TRAILING_HEADER_PADDING;
8451 if (hdi.fmt & (HDF_IMAGE|HDF_BITMAP))
8452 bitmap_margin = SendMessageW(infoPtr->hwndHeader, HDM_GETBITMAPMARGIN, 0, 0);
8454 if ((hdi.fmt & HDF_IMAGE) && himl)
8456 INT icon_cx, icon_cy;
8458 if (!ImageList_GetIconSize(himl, &icon_cx, &icon_cy))
8459 cx += icon_cx + 2*bitmap_margin;
8461 else if (hdi.fmt & HDF_BITMAP)
8463 BITMAP bmp;
8465 GetObjectW(hdi.hbm, sizeof(BITMAP), &bmp);
8466 cx += bmp.bmWidth + 2*bitmap_margin;
8469 SelectObject(hdc, old_font);
8470 ReleaseDC(infoPtr->hwndSelf, hdc);
8472 cx = max (cx, max_cx);
8476 if (cx < 0) return FALSE;
8478 /* call header to update the column change */
8479 hdi.mask = HDI_WIDTH;
8480 hdi.cxy = max(cx, LISTVIEW_GetColumnInfo(infoPtr, nColumn)->cxMin);
8481 TRACE("hdi.cxy=%d\n", hdi.cxy);
8482 return SendMessageW(infoPtr->hwndHeader, HDM_SETITEMW, nColumn, (LPARAM)&hdi);
8485 /***
8486 * Creates the checkbox imagelist. Helper for LISTVIEW_SetExtendedListViewStyle
8489 static HIMAGELIST LISTVIEW_CreateCheckBoxIL(const LISTVIEW_INFO *infoPtr)
8491 HDC hdc_wnd, hdc;
8492 HBITMAP hbm_im, hbm_mask, hbm_orig;
8493 RECT rc;
8494 HBRUSH hbr_white = GetStockObject(WHITE_BRUSH);
8495 HBRUSH hbr_black = GetStockObject(BLACK_BRUSH);
8496 HIMAGELIST himl;
8498 himl = ImageList_Create(GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON),
8499 ILC_COLOR | ILC_MASK, 2, 2);
8500 hdc_wnd = GetDC(infoPtr->hwndSelf);
8501 hdc = CreateCompatibleDC(hdc_wnd);
8502 hbm_im = CreateCompatibleBitmap(hdc_wnd, GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON));
8503 hbm_mask = CreateBitmap(GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), 1, 1, NULL);
8504 ReleaseDC(infoPtr->hwndSelf, hdc_wnd);
8506 SetRect(&rc, 0, 0, GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON));
8507 hbm_orig = SelectObject(hdc, hbm_mask);
8508 FillRect(hdc, &rc, hbr_white);
8509 InflateRect(&rc, -2, -2);
8510 FillRect(hdc, &rc, hbr_black);
8512 SelectObject(hdc, hbm_im);
8513 DrawFrameControl(hdc, &rc, DFC_BUTTON, DFCS_BUTTONCHECK | DFCS_MONO);
8514 SelectObject(hdc, hbm_orig);
8515 ImageList_Add(himl, hbm_im, hbm_mask);
8517 SelectObject(hdc, hbm_im);
8518 DrawFrameControl(hdc, &rc, DFC_BUTTON, DFCS_BUTTONCHECK | DFCS_MONO | DFCS_CHECKED);
8519 SelectObject(hdc, hbm_orig);
8520 ImageList_Add(himl, hbm_im, hbm_mask);
8522 DeleteObject(hbm_mask);
8523 DeleteObject(hbm_im);
8524 DeleteDC(hdc);
8526 return himl;
8529 /***
8530 * DESCRIPTION:
8531 * Sets the extended listview style.
8533 * PARAMETERS:
8534 * [I] infoPtr : valid pointer to the listview structure
8535 * [I] dwMask : mask
8536 * [I] dwStyle : style
8538 * RETURN:
8539 * SUCCESS : previous style
8540 * FAILURE : 0
8542 static DWORD LISTVIEW_SetExtendedListViewStyle(LISTVIEW_INFO *infoPtr, DWORD mask, DWORD ex_style)
8544 DWORD old_ex_style = infoPtr->dwLvExStyle;
8546 TRACE("mask=0x%08x, ex_style=0x%08x\n", mask, ex_style);
8548 /* set new style */
8549 if (mask)
8550 infoPtr->dwLvExStyle = (old_ex_style & ~mask) | (ex_style & mask);
8551 else
8552 infoPtr->dwLvExStyle = ex_style;
8554 if((infoPtr->dwLvExStyle ^ old_ex_style) & LVS_EX_CHECKBOXES)
8556 HIMAGELIST himl = 0;
8557 if(infoPtr->dwLvExStyle & LVS_EX_CHECKBOXES)
8559 LVITEMW item;
8560 item.mask = LVIF_STATE;
8561 item.stateMask = LVIS_STATEIMAGEMASK;
8562 item.state = INDEXTOSTATEIMAGEMASK(1);
8563 LISTVIEW_SetItemState(infoPtr, -1, &item);
8565 himl = LISTVIEW_CreateCheckBoxIL(infoPtr);
8566 if(!(infoPtr->dwStyle & LVS_SHAREIMAGELISTS))
8567 ImageList_Destroy(infoPtr->himlState);
8569 himl = LISTVIEW_SetImageList(infoPtr, LVSIL_STATE, himl);
8570 /* checkbox list replaces previous custom list or... */
8571 if(((infoPtr->dwLvExStyle & LVS_EX_CHECKBOXES) &&
8572 !(infoPtr->dwStyle & LVS_SHAREIMAGELISTS)) ||
8573 /* ...previous was checkbox list */
8574 (old_ex_style & LVS_EX_CHECKBOXES))
8575 ImageList_Destroy(himl);
8578 if((infoPtr->dwLvExStyle ^ old_ex_style) & LVS_EX_HEADERDRAGDROP)
8580 DWORD style;
8582 /* if not already created */
8583 LISTVIEW_CreateHeader(infoPtr);
8585 style = GetWindowLongW(infoPtr->hwndHeader, GWL_STYLE);
8586 if (infoPtr->dwLvExStyle & LVS_EX_HEADERDRAGDROP)
8587 style |= HDS_DRAGDROP;
8588 else
8589 style &= ~HDS_DRAGDROP;
8590 SetWindowLongW(infoPtr->hwndHeader, GWL_STYLE, style);
8593 /* GRIDLINES adds decoration at top so changes sizes */
8594 if((infoPtr->dwLvExStyle ^ old_ex_style) & LVS_EX_GRIDLINES)
8596 LISTVIEW_CreateHeader(infoPtr);
8597 LISTVIEW_UpdateSize(infoPtr);
8600 if((infoPtr->dwLvExStyle ^ old_ex_style) & LVS_EX_FULLROWSELECT)
8602 LISTVIEW_CreateHeader(infoPtr);
8605 if((infoPtr->dwLvExStyle ^ old_ex_style) & LVS_EX_TRANSPARENTBKGND)
8607 if (infoPtr->dwLvExStyle & LVS_EX_TRANSPARENTBKGND)
8608 LISTVIEW_SetBkColor(infoPtr, CLR_NONE);
8611 if((infoPtr->dwLvExStyle ^ old_ex_style) & LVS_EX_HEADERINALLVIEWS)
8613 if (infoPtr->dwLvExStyle & LVS_EX_HEADERINALLVIEWS)
8614 LISTVIEW_CreateHeader(infoPtr);
8615 else
8616 ShowWindow(infoPtr->hwndHeader, SW_HIDE);
8617 LISTVIEW_UpdateSize(infoPtr);
8618 LISTVIEW_UpdateScroll(infoPtr);
8621 LISTVIEW_InvalidateList(infoPtr);
8622 return old_ex_style;
8625 /***
8626 * DESCRIPTION:
8627 * Sets the new hot cursor used during hot tracking and hover selection.
8629 * PARAMETER(S):
8630 * [I] infoPtr : valid pointer to the listview structure
8631 * [I] hCursor : the new hot cursor handle
8633 * RETURN:
8634 * Returns the previous hot cursor
8636 static HCURSOR LISTVIEW_SetHotCursor(LISTVIEW_INFO *infoPtr, HCURSOR hCursor)
8638 HCURSOR oldCursor = infoPtr->hHotCursor;
8640 infoPtr->hHotCursor = hCursor;
8642 return oldCursor;
8646 /***
8647 * DESCRIPTION:
8648 * Sets the hot item index.
8650 * PARAMETERS:
8651 * [I] infoPtr : valid pointer to the listview structure
8652 * [I] iIndex : index
8654 * RETURN:
8655 * SUCCESS : previous hot item index
8656 * FAILURE : -1 (no hot item)
8658 static INT LISTVIEW_SetHotItem(LISTVIEW_INFO *infoPtr, INT iIndex)
8660 INT iOldIndex = infoPtr->nHotItem;
8662 infoPtr->nHotItem = iIndex;
8664 return iOldIndex;
8668 /***
8669 * DESCRIPTION:
8670 * Sets the amount of time the cursor must hover over an item before it is selected.
8672 * PARAMETER(S):
8673 * [I] infoPtr : valid pointer to the listview structure
8674 * [I] dwHoverTime : hover time, if -1 the hover time is set to the default
8676 * RETURN:
8677 * Returns the previous hover time
8679 static DWORD LISTVIEW_SetHoverTime(LISTVIEW_INFO *infoPtr, DWORD dwHoverTime)
8681 DWORD oldHoverTime = infoPtr->dwHoverTime;
8683 infoPtr->dwHoverTime = dwHoverTime;
8685 return oldHoverTime;
8688 /***
8689 * DESCRIPTION:
8690 * Sets spacing for icons of LVS_ICON style.
8692 * PARAMETER(S):
8693 * [I] infoPtr : valid pointer to the listview structure
8694 * [I] cx : horizontal spacing (-1 = system spacing, 0 = autosize)
8695 * [I] cy : vertical spacing (-1 = system spacing, 0 = autosize)
8697 * RETURN:
8698 * MAKELONG(oldcx, oldcy)
8700 static DWORD LISTVIEW_SetIconSpacing(LISTVIEW_INFO *infoPtr, INT cx, INT cy)
8702 INT iconWidth = 0, iconHeight = 0;
8703 DWORD oldspacing = MAKELONG(infoPtr->iconSpacing.cx, infoPtr->iconSpacing.cy);
8705 TRACE("requested=(%d,%d)\n", cx, cy);
8707 /* set to defaults, if instructed to */
8708 if (cx == -1 && cy == -1)
8710 infoPtr->autoSpacing = TRUE;
8711 if (infoPtr->himlNormal)
8712 ImageList_GetIconSize(infoPtr->himlNormal, &iconWidth, &iconHeight);
8713 cx = GetSystemMetrics(SM_CXICONSPACING) - GetSystemMetrics(SM_CXICON) + iconWidth;
8714 cy = GetSystemMetrics(SM_CYICONSPACING) - GetSystemMetrics(SM_CYICON) + iconHeight;
8716 else
8717 infoPtr->autoSpacing = FALSE;
8719 /* if 0 then keep width */
8720 if (cx != 0)
8721 infoPtr->iconSpacing.cx = cx;
8723 /* if 0 then keep height */
8724 if (cy != 0)
8725 infoPtr->iconSpacing.cy = cy;
8727 TRACE("old=(%d,%d), new=(%d,%d), iconSize=(%d,%d), ntmH=%d\n",
8728 LOWORD(oldspacing), HIWORD(oldspacing), infoPtr->iconSpacing.cx, infoPtr->iconSpacing.cy,
8729 infoPtr->iconSize.cx, infoPtr->iconSize.cy,
8730 infoPtr->ntmHeight);
8732 /* these depend on the iconSpacing */
8733 LISTVIEW_UpdateItemSize(infoPtr);
8735 return oldspacing;
8738 static inline void set_icon_size(SIZE *size, HIMAGELIST himl, BOOL is_small)
8740 INT cx, cy;
8742 if (himl && ImageList_GetIconSize(himl, &cx, &cy))
8744 size->cx = cx;
8745 size->cy = cy;
8747 else
8749 size->cx = GetSystemMetrics(is_small ? SM_CXSMICON : SM_CXICON);
8750 size->cy = GetSystemMetrics(is_small ? SM_CYSMICON : SM_CYICON);
8754 /***
8755 * DESCRIPTION:
8756 * Sets image lists.
8758 * PARAMETER(S):
8759 * [I] infoPtr : valid pointer to the listview structure
8760 * [I] nType : image list type
8761 * [I] himl : image list handle
8763 * RETURN:
8764 * SUCCESS : old image list
8765 * FAILURE : NULL
8767 static HIMAGELIST LISTVIEW_SetImageList(LISTVIEW_INFO *infoPtr, INT nType, HIMAGELIST himl)
8769 INT oldHeight = infoPtr->nItemHeight;
8770 HIMAGELIST himlOld = 0;
8772 TRACE("(nType=%d, himl=%p)\n", nType, himl);
8774 switch (nType)
8776 case LVSIL_NORMAL:
8777 himlOld = infoPtr->himlNormal;
8778 infoPtr->himlNormal = himl;
8779 if (infoPtr->uView == LV_VIEW_ICON) set_icon_size(&infoPtr->iconSize, himl, FALSE);
8780 if (infoPtr->autoSpacing)
8781 LISTVIEW_SetIconSpacing(infoPtr, -1, -1);
8782 break;
8784 case LVSIL_SMALL:
8785 himlOld = infoPtr->himlSmall;
8786 infoPtr->himlSmall = himl;
8787 if (infoPtr->uView != LV_VIEW_ICON) set_icon_size(&infoPtr->iconSize, himl, TRUE);
8788 if (infoPtr->hwndHeader)
8789 SendMessageW(infoPtr->hwndHeader, HDM_SETIMAGELIST, 0, (LPARAM)himl);
8790 break;
8792 case LVSIL_STATE:
8793 himlOld = infoPtr->himlState;
8794 infoPtr->himlState = himl;
8795 set_icon_size(&infoPtr->iconStateSize, himl, TRUE);
8796 ImageList_SetBkColor(infoPtr->himlState, CLR_NONE);
8797 break;
8799 default:
8800 ERR("Unknown icon type=%d\n", nType);
8801 return NULL;
8804 infoPtr->nItemHeight = LISTVIEW_CalculateItemHeight(infoPtr);
8805 if (infoPtr->nItemHeight != oldHeight)
8806 LISTVIEW_UpdateScroll(infoPtr);
8808 return himlOld;
8811 /***
8812 * DESCRIPTION:
8813 * Preallocates memory (does *not* set the actual count of items !)
8815 * PARAMETER(S):
8816 * [I] infoPtr : valid pointer to the listview structure
8817 * [I] nItems : item count (projected number of items to allocate)
8818 * [I] dwFlags : update flags
8820 * RETURN:
8821 * SUCCESS : TRUE
8822 * FAILURE : FALSE
8824 static BOOL LISTVIEW_SetItemCount(LISTVIEW_INFO *infoPtr, INT nItems, DWORD dwFlags)
8826 TRACE("(nItems=%d, dwFlags=%x)\n", nItems, dwFlags);
8828 if (infoPtr->dwStyle & LVS_OWNERDATA)
8830 INT nOldCount = infoPtr->nItemCount;
8831 infoPtr->nItemCount = nItems;
8833 if (nItems < nOldCount)
8835 RANGE range = { nItems, nOldCount };
8836 ranges_del(infoPtr->selectionRanges, range);
8837 if (infoPtr->nFocusedItem >= nItems)
8839 LISTVIEW_SetItemFocus(infoPtr, -1);
8840 infoPtr->nFocusedItem = -1;
8841 SetRectEmpty(&infoPtr->rcFocus);
8845 LISTVIEW_UpdateScroll(infoPtr);
8847 /* the flags are valid only in ownerdata report and list modes */
8848 if (infoPtr->uView == LV_VIEW_ICON || infoPtr->uView == LV_VIEW_SMALLICON) dwFlags = 0;
8850 if (!(dwFlags & LVSICF_NOSCROLL) && infoPtr->nFocusedItem != -1)
8851 LISTVIEW_EnsureVisible(infoPtr, infoPtr->nFocusedItem, FALSE);
8853 if (!(dwFlags & LVSICF_NOINVALIDATEALL))
8854 LISTVIEW_InvalidateList(infoPtr);
8855 else
8857 INT nFrom, nTo;
8858 POINT Origin;
8859 RECT rcErase;
8861 LISTVIEW_GetOrigin(infoPtr, &Origin);
8862 nFrom = min(nOldCount, nItems);
8863 nTo = max(nOldCount, nItems);
8865 if (infoPtr->uView == LV_VIEW_DETAILS)
8867 SetRect(&rcErase, 0, nFrom * infoPtr->nItemHeight, infoPtr->nItemWidth,
8868 nTo * infoPtr->nItemHeight);
8869 OffsetRect(&rcErase, Origin.x, Origin.y);
8870 if (IntersectRect(&rcErase, &rcErase, &infoPtr->rcList))
8871 LISTVIEW_InvalidateRect(infoPtr, &rcErase);
8873 else /* LV_VIEW_LIST */
8875 INT nPerCol = LISTVIEW_GetCountPerColumn(infoPtr);
8877 rcErase.left = (nFrom / nPerCol) * infoPtr->nItemWidth;
8878 rcErase.top = (nFrom % nPerCol) * infoPtr->nItemHeight;
8879 rcErase.right = rcErase.left + infoPtr->nItemWidth;
8880 rcErase.bottom = nPerCol * infoPtr->nItemHeight;
8881 OffsetRect(&rcErase, Origin.x, Origin.y);
8882 if (IntersectRect(&rcErase, &rcErase, &infoPtr->rcList))
8883 LISTVIEW_InvalidateRect(infoPtr, &rcErase);
8885 rcErase.left = (nFrom / nPerCol + 1) * infoPtr->nItemWidth;
8886 rcErase.top = 0;
8887 rcErase.right = (nTo / nPerCol + 1) * infoPtr->nItemWidth;
8888 rcErase.bottom = nPerCol * infoPtr->nItemHeight;
8889 OffsetRect(&rcErase, Origin.x, Origin.y);
8890 if (IntersectRect(&rcErase, &rcErase, &infoPtr->rcList))
8891 LISTVIEW_InvalidateRect(infoPtr, &rcErase);
8895 else
8897 /* According to MSDN for non-LVS_OWNERDATA this is just
8898 * a performance issue. The control allocates its internal
8899 * data structures for the number of items specified. It
8900 * cuts down on the number of memory allocations. Therefore
8901 * we will just issue a WARN here
8903 WARN("for non-ownerdata performance option not implemented.\n");
8906 return TRUE;
8909 /***
8910 * DESCRIPTION:
8911 * Sets the position of an item.
8913 * PARAMETER(S):
8914 * [I] infoPtr : valid pointer to the listview structure
8915 * [I] nItem : item index
8916 * [I] pt : coordinate
8918 * RETURN:
8919 * SUCCESS : TRUE
8920 * FAILURE : FALSE
8922 static BOOL LISTVIEW_SetItemPosition(LISTVIEW_INFO *infoPtr, INT nItem, const POINT *pt)
8924 POINT Origin, Pt;
8926 TRACE("(nItem=%d, pt=%s)\n", nItem, wine_dbgstr_point(pt));
8928 if (!pt || nItem < 0 || nItem >= infoPtr->nItemCount ||
8929 !(infoPtr->uView == LV_VIEW_ICON || infoPtr->uView == LV_VIEW_SMALLICON)) return FALSE;
8931 Pt = *pt;
8932 LISTVIEW_GetOrigin(infoPtr, &Origin);
8934 /* This point value seems to be an undocumented feature.
8935 * The best guess is that it means either at the origin,
8936 * or at true beginning of the list. I will assume the origin. */
8937 if ((Pt.x == -1) && (Pt.y == -1))
8938 Pt = Origin;
8940 if (infoPtr->uView == LV_VIEW_ICON)
8942 Pt.x -= (infoPtr->nItemWidth - infoPtr->iconSize.cx) / 2;
8943 Pt.y -= ICON_TOP_PADDING;
8945 Pt.x -= Origin.x;
8946 Pt.y -= Origin.y;
8948 return LISTVIEW_MoveIconTo(infoPtr, nItem, &Pt, FALSE);
8951 /***
8952 * DESCRIPTION:
8953 * Sets the state of one or many items.
8955 * PARAMETER(S):
8956 * [I] infoPtr : valid pointer to the listview structure
8957 * [I] nItem : item index
8958 * [I] item : item or subitem info
8960 * RETURN:
8961 * SUCCESS : TRUE
8962 * FAILURE : FALSE
8964 static BOOL LISTVIEW_SetItemState(LISTVIEW_INFO *infoPtr, INT nItem, const LVITEMW *item)
8966 BOOL ret = TRUE;
8967 LVITEMW lvItem;
8969 if (!item) return FALSE;
8971 lvItem.iItem = nItem;
8972 lvItem.iSubItem = 0;
8973 lvItem.mask = LVIF_STATE;
8974 lvItem.state = item->state;
8975 lvItem.stateMask = item->stateMask;
8976 TRACE("item=%s\n", debuglvitem_t(&lvItem, TRUE));
8978 if (nItem == -1)
8980 UINT oldstate = 0;
8981 DWORD old_mask;
8983 /* special case optimization for recurring attempt to deselect all */
8984 if (lvItem.state == 0 && lvItem.stateMask == LVIS_SELECTED && !LISTVIEW_GetSelectedCount(infoPtr))
8985 return TRUE;
8987 /* select all isn't allowed in LVS_SINGLESEL */
8988 if ((lvItem.state & lvItem.stateMask & LVIS_SELECTED) && (infoPtr->dwStyle & LVS_SINGLESEL))
8989 return FALSE;
8991 /* focus all isn't allowed */
8992 if (lvItem.state & lvItem.stateMask & LVIS_FOCUSED) return FALSE;
8994 old_mask = infoPtr->notify_mask & NOTIFY_MASK_ITEM_CHANGE;
8995 if (infoPtr->dwStyle & LVS_OWNERDATA)
8997 infoPtr->notify_mask &= ~NOTIFY_MASK_ITEM_CHANGE;
8998 if (!(lvItem.state & LVIS_SELECTED) && LISTVIEW_GetSelectedCount(infoPtr))
8999 oldstate |= LVIS_SELECTED;
9000 if (infoPtr->nFocusedItem != -1) oldstate |= LVIS_FOCUSED;
9003 /* apply to all items */
9004 for (lvItem.iItem = 0; lvItem.iItem < infoPtr->nItemCount; lvItem.iItem++)
9005 if (!LISTVIEW_SetItemT(infoPtr, &lvItem, TRUE)) ret = FALSE;
9007 if (infoPtr->dwStyle & LVS_OWNERDATA)
9009 NMLISTVIEW nmlv;
9011 infoPtr->notify_mask |= old_mask;
9013 nmlv.iItem = -1;
9014 nmlv.iSubItem = 0;
9015 nmlv.uNewState = lvItem.state & lvItem.stateMask;
9016 nmlv.uOldState = oldstate & lvItem.stateMask;
9017 nmlv.uChanged = LVIF_STATE;
9018 nmlv.ptAction.x = nmlv.ptAction.y = 0;
9019 nmlv.lParam = 0;
9021 notify_listview(infoPtr, LVN_ITEMCHANGED, &nmlv);
9024 else
9025 ret = LISTVIEW_SetItemT(infoPtr, &lvItem, TRUE);
9027 return ret;
9030 /***
9031 * DESCRIPTION:
9032 * Sets the text of an item or subitem.
9034 * PARAMETER(S):
9035 * [I] hwnd : window handle
9036 * [I] nItem : item index
9037 * [I] lpLVItem : item or subitem info
9038 * [I] isW : TRUE if input is Unicode
9040 * RETURN:
9041 * SUCCESS : TRUE
9042 * FAILURE : FALSE
9044 static BOOL LISTVIEW_SetItemTextT(LISTVIEW_INFO *infoPtr, INT nItem, const LVITEMW *lpLVItem, BOOL isW)
9046 LVITEMW lvItem;
9048 if (!lpLVItem || nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
9049 if (infoPtr->dwStyle & LVS_OWNERDATA) return FALSE;
9051 lvItem.iItem = nItem;
9052 lvItem.iSubItem = lpLVItem->iSubItem;
9053 lvItem.mask = LVIF_TEXT;
9054 lvItem.pszText = lpLVItem->pszText;
9055 lvItem.cchTextMax = lpLVItem->cchTextMax;
9057 TRACE("(nItem=%d, lpLVItem=%s, isW=%d)\n", nItem, debuglvitem_t(&lvItem, isW), isW);
9059 return LISTVIEW_SetItemT(infoPtr, &lvItem, isW);
9062 /***
9063 * DESCRIPTION:
9064 * Set item index that marks the start of a multiple selection.
9066 * PARAMETER(S):
9067 * [I] infoPtr : valid pointer to the listview structure
9068 * [I] nIndex : index
9070 * RETURN:
9071 * Index number or -1 if there is no selection mark.
9073 static INT LISTVIEW_SetSelectionMark(LISTVIEW_INFO *infoPtr, INT nIndex)
9075 INT nOldIndex = infoPtr->nSelectionMark;
9077 TRACE("(nIndex=%d)\n", nIndex);
9079 if (nIndex >= -1 && nIndex < infoPtr->nItemCount)
9080 infoPtr->nSelectionMark = nIndex;
9082 return nOldIndex;
9085 /***
9086 * DESCRIPTION:
9087 * Sets the text background color.
9089 * PARAMETER(S):
9090 * [I] infoPtr : valid pointer to the listview structure
9091 * [I] color : text background color
9093 * RETURN:
9094 * SUCCESS : TRUE
9095 * FAILURE : FALSE
9097 static BOOL LISTVIEW_SetTextBkColor(LISTVIEW_INFO *infoPtr, COLORREF color)
9099 TRACE("(color=%x)\n", color);
9101 infoPtr->clrTextBk = color;
9102 return TRUE;
9105 /***
9106 * DESCRIPTION:
9107 * Sets the text foreground color.
9109 * PARAMETER(S):
9110 * [I] infoPtr : valid pointer to the listview structure
9111 * [I] color : text color
9113 * RETURN:
9114 * SUCCESS : TRUE
9115 * FAILURE : FALSE
9117 static BOOL LISTVIEW_SetTextColor (LISTVIEW_INFO *infoPtr, COLORREF color)
9119 TRACE("(color=%x)\n", color);
9121 infoPtr->clrText = color;
9122 return TRUE;
9125 /***
9126 * DESCRIPTION:
9127 * Sets new ToolTip window to ListView control.
9129 * PARAMETER(S):
9130 * [I] infoPtr : valid pointer to the listview structure
9131 * [I] hwndNewToolTip : handle to new ToolTip
9133 * RETURN:
9134 * old tool tip
9136 static HWND LISTVIEW_SetToolTips( LISTVIEW_INFO *infoPtr, HWND hwndNewToolTip)
9138 HWND hwndOldToolTip = infoPtr->hwndToolTip;
9139 infoPtr->hwndToolTip = hwndNewToolTip;
9140 return hwndOldToolTip;
9144 * DESCRIPTION:
9145 * sets the Unicode character format flag for the control
9146 * PARAMETER(S):
9147 * [I] infoPtr :valid pointer to the listview structure
9148 * [I] fUnicode :true to switch to UNICODE false to switch to ANSI
9150 * RETURN:
9151 * Old Unicode Format
9153 static BOOL LISTVIEW_SetUnicodeFormat( LISTVIEW_INFO *infoPtr, BOOL unicode)
9155 SHORT rc = infoPtr->notifyFormat;
9156 infoPtr->notifyFormat = (unicode) ? NFR_UNICODE : NFR_ANSI;
9157 return rc == NFR_UNICODE;
9161 * DESCRIPTION:
9162 * sets the control view mode
9163 * PARAMETER(S):
9164 * [I] infoPtr :valid pointer to the listview structure
9165 * [I] nView :new view mode value
9167 * RETURN:
9168 * SUCCESS: 1
9169 * FAILURE: -1
9171 static INT LISTVIEW_SetView(LISTVIEW_INFO *infoPtr, DWORD nView)
9173 HIMAGELIST himl;
9175 if (infoPtr->uView == nView) return 1;
9177 if ((INT)nView < 0 || nView > LV_VIEW_MAX) return -1;
9178 if (nView == LV_VIEW_TILE)
9180 FIXME("View LV_VIEW_TILE unimplemented\n");
9181 return -1;
9184 infoPtr->uView = nView;
9186 SendMessageW(infoPtr->hwndEdit, WM_KILLFOCUS, 0, 0);
9187 ShowWindow(infoPtr->hwndHeader, SW_HIDE);
9189 ShowScrollBar(infoPtr->hwndSelf, SB_BOTH, FALSE);
9190 SetRectEmpty(&infoPtr->rcFocus);
9192 himl = (nView == LV_VIEW_ICON ? infoPtr->himlNormal : infoPtr->himlSmall);
9193 set_icon_size(&infoPtr->iconSize, himl, nView != LV_VIEW_ICON);
9195 switch (nView)
9197 case LV_VIEW_ICON:
9198 case LV_VIEW_SMALLICON:
9199 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
9200 break;
9201 case LV_VIEW_DETAILS:
9203 HDLAYOUT hl;
9204 WINDOWPOS wp;
9206 LISTVIEW_CreateHeader( infoPtr );
9208 hl.prc = &infoPtr->rcList;
9209 hl.pwpos = &wp;
9210 SendMessageW(infoPtr->hwndHeader, HDM_LAYOUT, 0, (LPARAM)&hl);
9211 SetWindowPos(infoPtr->hwndHeader, infoPtr->hwndSelf, wp.x, wp.y, wp.cx, wp.cy,
9212 wp.flags | ((infoPtr->dwStyle & LVS_NOCOLUMNHEADER) ? SWP_HIDEWINDOW : SWP_SHOWWINDOW));
9213 break;
9215 case LV_VIEW_LIST:
9216 break;
9219 LISTVIEW_UpdateItemSize(infoPtr);
9220 LISTVIEW_UpdateSize(infoPtr);
9221 LISTVIEW_UpdateScroll(infoPtr);
9222 LISTVIEW_InvalidateList(infoPtr);
9224 TRACE("nView=%d\n", nView);
9226 return 1;
9229 /* LISTVIEW_SetWorkAreas */
9231 struct sorting_context
9233 HDPA items;
9234 PFNLVCOMPARE compare_func;
9235 LPARAM lParam;
9238 /* DPA_Sort() callback used for LVM_SORTITEMS */
9239 static INT WINAPI LISTVIEW_CallBackCompare(LPVOID first, LPVOID second, LPARAM lParam)
9241 struct sorting_context *context = (struct sorting_context *)lParam;
9242 ITEM_INFO* lv_first = DPA_GetPtr( first, 0 );
9243 ITEM_INFO* lv_second = DPA_GetPtr( second, 0 );
9245 return context->compare_func(lv_first->lParam, lv_second->lParam, context->lParam);
9248 /* DPA_Sort() callback used for LVM_SORTITEMSEX */
9249 static INT WINAPI LISTVIEW_CallBackCompareEx(LPVOID first, LPVOID second, LPARAM lParam)
9251 struct sorting_context *context = (struct sorting_context *)lParam;
9252 INT first_idx = DPA_GetPtrIndex( context->items, first );
9253 INT second_idx = DPA_GetPtrIndex( context->items, second );
9255 return context->compare_func(first_idx, second_idx, context->lParam);
9258 /***
9259 * DESCRIPTION:
9260 * Sorts the listview items.
9262 * PARAMETER(S):
9263 * [I] infoPtr : valid pointer to the listview structure
9264 * [I] pfnCompare : application-defined value
9265 * [I] lParamSort : pointer to comparison callback
9266 * [I] IsEx : TRUE when LVM_SORTITEMSEX used
9268 * RETURN:
9269 * SUCCESS : TRUE
9270 * FAILURE : FALSE
9272 static BOOL LISTVIEW_SortItems(LISTVIEW_INFO *infoPtr, PFNLVCOMPARE pfnCompare,
9273 LPARAM lParamSort, BOOL IsEx)
9275 HDPA hdpaSubItems, hdpaItems;
9276 ITEM_INFO *lpItem;
9277 LPVOID selectionMarkItem = NULL;
9278 LPVOID focusedItem = NULL;
9279 struct sorting_context context;
9280 int i;
9282 TRACE("(pfnCompare=%p, lParamSort=%lx)\n", pfnCompare, lParamSort);
9284 if (infoPtr->dwStyle & LVS_OWNERDATA) return FALSE;
9286 if (!pfnCompare) return FALSE;
9287 if (!infoPtr->hdpaItems) return FALSE;
9289 /* if there are 0 or 1 items, there is no need to sort */
9290 if (infoPtr->nItemCount < 2) return TRUE;
9291 if (!(hdpaItems = DPA_Clone(infoPtr->hdpaItems, NULL))) return FALSE;
9293 /* clear selection */
9294 ranges_clear(infoPtr->selectionRanges);
9296 /* save selection mark and focused item */
9297 if (infoPtr->nSelectionMark >= 0)
9298 selectionMarkItem = DPA_GetPtr(infoPtr->hdpaItems, infoPtr->nSelectionMark);
9299 if (infoPtr->nFocusedItem >= 0)
9300 focusedItem = DPA_GetPtr(infoPtr->hdpaItems, infoPtr->nFocusedItem);
9302 context.items = hdpaItems;
9303 context.compare_func = pfnCompare;
9304 context.lParam = lParamSort;
9305 if (IsEx)
9306 DPA_Sort(hdpaItems, LISTVIEW_CallBackCompareEx, (LPARAM)&context);
9307 else
9308 DPA_Sort(hdpaItems, LISTVIEW_CallBackCompare, (LPARAM)&context);
9309 DPA_Destroy(infoPtr->hdpaItems);
9310 infoPtr->hdpaItems = hdpaItems;
9312 /* restore selection ranges */
9313 for (i=0; i < infoPtr->nItemCount; i++)
9315 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, i);
9316 lpItem = DPA_GetPtr(hdpaSubItems, 0);
9318 if (lpItem->state & LVIS_SELECTED)
9319 ranges_additem(infoPtr->selectionRanges, i);
9321 /* restore selection mark and focused item */
9322 infoPtr->nSelectionMark = DPA_GetPtrIndex(infoPtr->hdpaItems, selectionMarkItem);
9323 infoPtr->nFocusedItem = DPA_GetPtrIndex(infoPtr->hdpaItems, focusedItem);
9325 /* I believe nHotItem should be left alone, see LISTVIEW_ShiftIndices */
9327 /* refresh the display */
9328 LISTVIEW_InvalidateList(infoPtr);
9329 return TRUE;
9332 /***
9333 * DESCRIPTION:
9334 * Update theme handle after a theme change.
9336 * PARAMETER(S):
9337 * [I] infoPtr : valid pointer to the listview structure
9339 * RETURN:
9340 * SUCCESS : 0
9341 * FAILURE : something else
9343 static LRESULT LISTVIEW_ThemeChanged(const LISTVIEW_INFO *infoPtr)
9345 HTHEME theme = GetWindowTheme(infoPtr->hwndSelf);
9346 CloseThemeData(theme);
9347 OpenThemeData(infoPtr->hwndSelf, themeClass);
9348 return 0;
9351 /***
9352 * DESCRIPTION:
9353 * Updates an items or rearranges the listview control.
9355 * PARAMETER(S):
9356 * [I] infoPtr : valid pointer to the listview structure
9357 * [I] nItem : item index
9359 * RETURN:
9360 * SUCCESS : TRUE
9361 * FAILURE : FALSE
9363 static BOOL LISTVIEW_Update(LISTVIEW_INFO *infoPtr, INT nItem)
9365 TRACE("(nItem=%d)\n", nItem);
9367 if (nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
9369 /* rearrange with default alignment style */
9370 if (is_autoarrange(infoPtr))
9371 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
9372 else
9373 LISTVIEW_InvalidateItem(infoPtr, nItem);
9375 return TRUE;
9378 /***
9379 * DESCRIPTION:
9380 * Draw the track line at the place defined in the infoPtr structure.
9381 * The line is drawn with a XOR pen so drawing the line for the second time
9382 * in the same place erases the line.
9384 * PARAMETER(S):
9385 * [I] infoPtr : valid pointer to the listview structure
9387 * RETURN:
9388 * SUCCESS : TRUE
9389 * FAILURE : FALSE
9391 static BOOL LISTVIEW_DrawTrackLine(const LISTVIEW_INFO *infoPtr)
9393 HDC hdc;
9395 if (infoPtr->xTrackLine == -1)
9396 return FALSE;
9398 if (!(hdc = GetDC(infoPtr->hwndSelf)))
9399 return FALSE;
9400 PatBlt( hdc, infoPtr->xTrackLine, infoPtr->rcList.top,
9401 1, infoPtr->rcList.bottom - infoPtr->rcList.top, DSTINVERT );
9402 ReleaseDC(infoPtr->hwndSelf, hdc);
9403 return TRUE;
9406 /***
9407 * DESCRIPTION:
9408 * Called when an edit control should be displayed. This function is called after
9409 * we are sure that there was a single click - not a double click (this is a TIMERPROC).
9411 * PARAMETER(S):
9412 * [I] hwnd : Handle to the listview
9413 * [I] uMsg : WM_TIMER (ignored)
9414 * [I] idEvent : The timer ID interpreted as a pointer to a DELAYED_EDIT_ITEM struct
9415 * [I] dwTimer : The elapsed time (ignored)
9417 * RETURN:
9418 * None.
9420 static VOID CALLBACK LISTVIEW_DelayedEditItem(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime)
9422 DELAYED_ITEM_EDIT *editItem = (DELAYED_ITEM_EDIT *)idEvent;
9423 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)GetWindowLongPtrW(hwnd, 0);
9425 KillTimer(hwnd, idEvent);
9426 editItem->fEnabled = FALSE;
9427 /* check if the item is still selected */
9428 if (infoPtr->bFocus && LISTVIEW_GetItemState(infoPtr, editItem->iItem, LVIS_SELECTED))
9429 LISTVIEW_EditLabelT(infoPtr, editItem->iItem, TRUE);
9432 /***
9433 * DESCRIPTION:
9434 * Creates the listview control - the WM_NCCREATE phase.
9436 * PARAMETER(S):
9437 * [I] hwnd : window handle
9438 * [I] lpcs : the create parameters
9440 * RETURN:
9441 * Success: TRUE
9442 * Failure: FALSE
9444 static LRESULT LISTVIEW_NCCreate(HWND hwnd, WPARAM wParam, const CREATESTRUCTW *lpcs)
9446 LISTVIEW_INFO *infoPtr;
9447 LOGFONTW logFont;
9449 TRACE("(lpcs=%p)\n", lpcs);
9451 /* initialize info pointer */
9452 infoPtr = Alloc(sizeof(LISTVIEW_INFO));
9453 if (!infoPtr) return FALSE;
9455 SetWindowLongPtrW(hwnd, 0, (DWORD_PTR)infoPtr);
9457 infoPtr->hwndSelf = hwnd;
9458 infoPtr->dwStyle = lpcs->style; /* Note: may be changed in WM_CREATE */
9459 map_style_view(infoPtr);
9460 /* determine the type of structures to use */
9461 infoPtr->hwndNotify = lpcs->hwndParent;
9462 /* infoPtr->notifyFormat will be filled in WM_CREATE */
9464 /* initialize color information */
9465 infoPtr->clrBk = CLR_NONE;
9466 infoPtr->clrText = CLR_DEFAULT;
9467 infoPtr->clrTextBk = CLR_DEFAULT;
9468 LISTVIEW_SetBkColor(infoPtr, comctl32_color.clrWindow);
9470 /* set default values */
9471 infoPtr->nFocusedItem = -1;
9472 infoPtr->nSelectionMark = -1;
9473 infoPtr->nHotItem = -1;
9474 infoPtr->redraw = TRUE;
9475 infoPtr->bNoItemMetrics = TRUE;
9476 infoPtr->notify_mask = NOTIFY_MASK_UNMASK_ALL;
9477 infoPtr->autoSpacing = TRUE;
9478 infoPtr->iconSpacing.cx = GetSystemMetrics(SM_CXICONSPACING) - GetSystemMetrics(SM_CXICON);
9479 infoPtr->iconSpacing.cy = GetSystemMetrics(SM_CYICONSPACING) - GetSystemMetrics(SM_CYICON);
9480 infoPtr->nEditLabelItem = -1;
9481 infoPtr->nLButtonDownItem = -1;
9482 infoPtr->dwHoverTime = HOVER_DEFAULT; /* default system hover time */
9483 infoPtr->cWheelRemainder = 0;
9484 infoPtr->nMeasureItemHeight = 0;
9485 infoPtr->xTrackLine = -1; /* no track line */
9486 infoPtr->itemEdit.fEnabled = FALSE;
9487 infoPtr->iVersion = COMCTL32_VERSION;
9488 infoPtr->colRectsDirty = FALSE;
9489 infoPtr->selected_column = -1;
9491 /* get default font (icon title) */
9492 SystemParametersInfoW(SPI_GETICONTITLELOGFONT, 0, &logFont, 0);
9493 infoPtr->hDefaultFont = CreateFontIndirectW(&logFont);
9494 infoPtr->hFont = infoPtr->hDefaultFont;
9495 LISTVIEW_SaveTextMetrics(infoPtr);
9497 /* allocate memory for the data structure */
9498 if (!(infoPtr->selectionRanges = ranges_create(10))) goto fail;
9499 if (!(infoPtr->hdpaItems = DPA_Create(10))) goto fail;
9500 if (!(infoPtr->hdpaItemIds = DPA_Create(10))) goto fail;
9501 if (!(infoPtr->hdpaPosX = DPA_Create(10))) goto fail;
9502 if (!(infoPtr->hdpaPosY = DPA_Create(10))) goto fail;
9503 if (!(infoPtr->hdpaColumns = DPA_Create(10))) goto fail;
9505 return DefWindowProcW(hwnd, WM_NCCREATE, wParam, (LPARAM)lpcs);
9507 fail:
9508 DestroyWindow(infoPtr->hwndHeader);
9509 ranges_destroy(infoPtr->selectionRanges);
9510 DPA_Destroy(infoPtr->hdpaItems);
9511 DPA_Destroy(infoPtr->hdpaItemIds);
9512 DPA_Destroy(infoPtr->hdpaPosX);
9513 DPA_Destroy(infoPtr->hdpaPosY);
9514 DPA_Destroy(infoPtr->hdpaColumns);
9515 Free(infoPtr);
9516 return FALSE;
9519 /***
9520 * DESCRIPTION:
9521 * Creates the listview control - the WM_CREATE phase. Most of the data is
9522 * already set up in LISTVIEW_NCCreate
9524 * PARAMETER(S):
9525 * [I] hwnd : window handle
9526 * [I] lpcs : the create parameters
9528 * RETURN:
9529 * Success: 0
9530 * Failure: -1
9532 static LRESULT LISTVIEW_Create(HWND hwnd, const CREATESTRUCTW *lpcs)
9534 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)GetWindowLongPtrW(hwnd, 0);
9536 TRACE("(lpcs=%p, style=0x%08x)\n", lpcs, lpcs->style);
9538 infoPtr->dwStyle = lpcs->style;
9539 map_style_view(infoPtr);
9541 infoPtr->notifyFormat = SendMessageW(infoPtr->hwndNotify, WM_NOTIFYFORMAT,
9542 (WPARAM)infoPtr->hwndSelf, NF_QUERY);
9543 /* on error defaulting to ANSI notifications */
9544 if (infoPtr->notifyFormat == 0) infoPtr->notifyFormat = NFR_ANSI;
9545 TRACE("notify format=%d\n", infoPtr->notifyFormat);
9547 if ((infoPtr->uView == LV_VIEW_DETAILS) && (lpcs->style & WS_VISIBLE))
9549 if (LISTVIEW_CreateHeader(infoPtr) < 0) return -1;
9551 else
9552 infoPtr->hwndHeader = 0;
9554 /* init item size to avoid division by 0 */
9555 LISTVIEW_UpdateItemSize (infoPtr);
9556 LISTVIEW_UpdateSize (infoPtr);
9558 if (infoPtr->uView == LV_VIEW_DETAILS)
9560 if (!(LVS_NOCOLUMNHEADER & lpcs->style) && (WS_VISIBLE & lpcs->style))
9562 ShowWindow(infoPtr->hwndHeader, SW_SHOWNORMAL);
9564 LISTVIEW_UpdateScroll(infoPtr);
9565 /* send WM_MEASUREITEM notification */
9566 if (infoPtr->dwStyle & LVS_OWNERDRAWFIXED) notify_measureitem(infoPtr);
9569 OpenThemeData(hwnd, themeClass);
9571 /* initialize the icon sizes */
9572 set_icon_size(&infoPtr->iconSize, infoPtr->himlNormal, infoPtr->uView != LV_VIEW_ICON);
9573 set_icon_size(&infoPtr->iconStateSize, infoPtr->himlState, TRUE);
9574 return 0;
9577 /***
9578 * DESCRIPTION:
9579 * Destroys the listview control.
9581 * PARAMETER(S):
9582 * [I] infoPtr : valid pointer to the listview structure
9584 * RETURN:
9585 * Success: 0
9586 * Failure: -1
9588 static LRESULT LISTVIEW_Destroy(LISTVIEW_INFO *infoPtr)
9590 HTHEME theme = GetWindowTheme(infoPtr->hwndSelf);
9591 CloseThemeData(theme);
9593 /* delete all items */
9594 LISTVIEW_DeleteAllItems(infoPtr, TRUE);
9596 return 0;
9599 /***
9600 * DESCRIPTION:
9601 * Enables the listview control.
9603 * PARAMETER(S):
9604 * [I] infoPtr : valid pointer to the listview structure
9605 * [I] bEnable : specifies whether to enable or disable the window
9607 * RETURN:
9608 * SUCCESS : TRUE
9609 * FAILURE : FALSE
9611 static BOOL LISTVIEW_Enable(const LISTVIEW_INFO *infoPtr)
9613 if (infoPtr->dwStyle & LVS_OWNERDRAWFIXED)
9614 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
9615 return TRUE;
9618 /***
9619 * DESCRIPTION:
9620 * Erases the background of the listview control.
9622 * PARAMETER(S):
9623 * [I] infoPtr : valid pointer to the listview structure
9624 * [I] hdc : device context handle
9626 * RETURN:
9627 * SUCCESS : TRUE
9628 * FAILURE : FALSE
9630 static inline BOOL LISTVIEW_EraseBkgnd(const LISTVIEW_INFO *infoPtr, HDC hdc)
9632 RECT rc;
9634 TRACE("(hdc=%p)\n", hdc);
9636 if (!GetClipBox(hdc, &rc)) return FALSE;
9638 if (infoPtr->clrBk == CLR_NONE)
9640 if (infoPtr->dwLvExStyle & LVS_EX_TRANSPARENTBKGND)
9641 return SendMessageW(infoPtr->hwndNotify, WM_PRINTCLIENT,
9642 (WPARAM)hdc, PRF_ERASEBKGND);
9643 else
9644 return SendMessageW(infoPtr->hwndNotify, WM_ERASEBKGND, (WPARAM)hdc, 0);
9647 /* for double buffered controls we need to do this during refresh */
9648 if (infoPtr->dwLvExStyle & LVS_EX_DOUBLEBUFFER) return FALSE;
9650 return LISTVIEW_FillBkgnd(infoPtr, hdc, &rc);
9654 /***
9655 * DESCRIPTION:
9656 * Helper function for LISTVIEW_[HV]Scroll *only*.
9657 * Performs vertical/horizontal scrolling by a give amount.
9659 * PARAMETER(S):
9660 * [I] infoPtr : valid pointer to the listview structure
9661 * [I] dx : amount of horizontal scroll
9662 * [I] dy : amount of vertical scroll
9664 static void scroll_list(LISTVIEW_INFO *infoPtr, INT dx, INT dy)
9666 /* now we can scroll the list */
9667 ScrollWindowEx(infoPtr->hwndSelf, dx, dy, &infoPtr->rcList,
9668 &infoPtr->rcList, 0, 0, SW_ERASE | SW_INVALIDATE);
9669 /* if we have focus, adjust rect */
9670 OffsetRect(&infoPtr->rcFocus, dx, dy);
9671 UpdateWindow(infoPtr->hwndSelf);
9674 /***
9675 * DESCRIPTION:
9676 * Performs vertical scrolling.
9678 * PARAMETER(S):
9679 * [I] infoPtr : valid pointer to the listview structure
9680 * [I] nScrollCode : scroll code
9681 * [I] nScrollDiff : units to scroll in SB_INTERNAL mode, 0 otherwise
9682 * [I] hScrollWnd : scrollbar control window handle
9684 * RETURN:
9685 * Zero
9687 * NOTES:
9688 * SB_LINEUP/SB_LINEDOWN:
9689 * for LVS_ICON, LVS_SMALLICON is 37 by experiment
9690 * for LVS_REPORT is 1 line
9691 * for LVS_LIST cannot occur
9694 static LRESULT LISTVIEW_VScroll(LISTVIEW_INFO *infoPtr, INT nScrollCode,
9695 INT nScrollDiff)
9697 INT nOldScrollPos, nNewScrollPos;
9698 SCROLLINFO scrollInfo;
9699 BOOL is_an_icon;
9701 TRACE("(nScrollCode=%d(%s), nScrollDiff=%d)\n", nScrollCode,
9702 debugscrollcode(nScrollCode), nScrollDiff);
9704 if (infoPtr->hwndEdit) SendMessageW(infoPtr->hwndEdit, WM_KILLFOCUS, 0, 0);
9706 scrollInfo.cbSize = sizeof(SCROLLINFO);
9707 scrollInfo.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_TRACKPOS;
9709 is_an_icon = ((infoPtr->uView == LV_VIEW_ICON) || (infoPtr->uView == LV_VIEW_SMALLICON));
9711 if (!GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo)) return 1;
9713 nOldScrollPos = scrollInfo.nPos;
9714 switch (nScrollCode)
9716 case SB_INTERNAL:
9717 break;
9719 case SB_LINEUP:
9720 nScrollDiff = (is_an_icon) ? -LISTVIEW_SCROLL_ICON_LINE_SIZE : -1;
9721 break;
9723 case SB_LINEDOWN:
9724 nScrollDiff = (is_an_icon) ? LISTVIEW_SCROLL_ICON_LINE_SIZE : 1;
9725 break;
9727 case SB_PAGEUP:
9728 nScrollDiff = -scrollInfo.nPage;
9729 break;
9731 case SB_PAGEDOWN:
9732 nScrollDiff = scrollInfo.nPage;
9733 break;
9735 case SB_THUMBPOSITION:
9736 case SB_THUMBTRACK:
9737 nScrollDiff = scrollInfo.nTrackPos - scrollInfo.nPos;
9738 break;
9740 default:
9741 nScrollDiff = 0;
9744 /* quit right away if pos isn't changing */
9745 if (nScrollDiff == 0) return 0;
9747 /* calculate new position, and handle overflows */
9748 nNewScrollPos = scrollInfo.nPos + nScrollDiff;
9749 if (nScrollDiff > 0) {
9750 if (nNewScrollPos < nOldScrollPos ||
9751 nNewScrollPos > scrollInfo.nMax)
9752 nNewScrollPos = scrollInfo.nMax;
9753 } else {
9754 if (nNewScrollPos > nOldScrollPos ||
9755 nNewScrollPos < scrollInfo.nMin)
9756 nNewScrollPos = scrollInfo.nMin;
9759 /* set the new position, and reread in case it changed */
9760 scrollInfo.fMask = SIF_POS;
9761 scrollInfo.nPos = nNewScrollPos;
9762 nNewScrollPos = SetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo, TRUE);
9764 /* carry on only if it really changed */
9765 if (nNewScrollPos == nOldScrollPos) return 0;
9767 /* now adjust to client coordinates */
9768 nScrollDiff = nOldScrollPos - nNewScrollPos;
9769 if (infoPtr->uView == LV_VIEW_DETAILS) nScrollDiff *= infoPtr->nItemHeight;
9771 /* and scroll the window */
9772 scroll_list(infoPtr, 0, nScrollDiff);
9774 return 0;
9777 /***
9778 * DESCRIPTION:
9779 * Performs horizontal scrolling.
9781 * PARAMETER(S):
9782 * [I] infoPtr : valid pointer to the listview structure
9783 * [I] nScrollCode : scroll code
9784 * [I] nScrollDiff : units to scroll in SB_INTERNAL mode, 0 otherwise
9785 * [I] hScrollWnd : scrollbar control window handle
9787 * RETURN:
9788 * Zero
9790 * NOTES:
9791 * SB_LINELEFT/SB_LINERIGHT:
9792 * for LVS_ICON, LVS_SMALLICON 1 pixel
9793 * for LVS_REPORT is 1 pixel
9794 * for LVS_LIST is 1 column --> which is a 1 because the
9795 * scroll is based on columns not pixels
9798 static LRESULT LISTVIEW_HScroll(LISTVIEW_INFO *infoPtr, INT nScrollCode,
9799 INT nScrollDiff)
9801 INT nOldScrollPos, nNewScrollPos;
9802 SCROLLINFO scrollInfo;
9803 BOOL is_an_icon;
9805 TRACE("(nScrollCode=%d(%s), nScrollDiff=%d)\n", nScrollCode,
9806 debugscrollcode(nScrollCode), nScrollDiff);
9808 if (infoPtr->hwndEdit) SendMessageW(infoPtr->hwndEdit, WM_KILLFOCUS, 0, 0);
9810 scrollInfo.cbSize = sizeof(SCROLLINFO);
9811 scrollInfo.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_TRACKPOS;
9813 is_an_icon = ((infoPtr->uView == LV_VIEW_ICON) || (infoPtr->uView == LV_VIEW_SMALLICON));
9815 if (!GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo)) return 1;
9817 nOldScrollPos = scrollInfo.nPos;
9819 switch (nScrollCode)
9821 case SB_INTERNAL:
9822 break;
9824 case SB_LINELEFT:
9825 nScrollDiff = (is_an_icon) ? -LISTVIEW_SCROLL_ICON_LINE_SIZE : -1;
9826 break;
9828 case SB_LINERIGHT:
9829 nScrollDiff = (is_an_icon) ? LISTVIEW_SCROLL_ICON_LINE_SIZE : 1;
9830 break;
9832 case SB_PAGELEFT:
9833 nScrollDiff = -scrollInfo.nPage;
9834 break;
9836 case SB_PAGERIGHT:
9837 nScrollDiff = scrollInfo.nPage;
9838 break;
9840 case SB_THUMBPOSITION:
9841 case SB_THUMBTRACK:
9842 nScrollDiff = scrollInfo.nTrackPos - scrollInfo.nPos;
9843 break;
9845 default:
9846 nScrollDiff = 0;
9849 /* quit right away if pos isn't changing */
9850 if (nScrollDiff == 0) return 0;
9852 /* calculate new position, and handle overflows */
9853 nNewScrollPos = scrollInfo.nPos + nScrollDiff;
9854 if (nScrollDiff > 0) {
9855 if (nNewScrollPos < nOldScrollPos ||
9856 nNewScrollPos > scrollInfo.nMax)
9857 nNewScrollPos = scrollInfo.nMax;
9858 } else {
9859 if (nNewScrollPos > nOldScrollPos ||
9860 nNewScrollPos < scrollInfo.nMin)
9861 nNewScrollPos = scrollInfo.nMin;
9864 /* set the new position, and reread in case it changed */
9865 scrollInfo.fMask = SIF_POS;
9866 scrollInfo.nPos = nNewScrollPos;
9867 nNewScrollPos = SetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo, TRUE);
9869 /* carry on only if it really changed */
9870 if (nNewScrollPos == nOldScrollPos) return 0;
9872 LISTVIEW_UpdateHeaderSize(infoPtr, nNewScrollPos);
9874 /* now adjust to client coordinates */
9875 nScrollDiff = nOldScrollPos - nNewScrollPos;
9876 if (infoPtr->uView == LV_VIEW_LIST) nScrollDiff *= infoPtr->nItemWidth;
9878 /* and scroll the window */
9879 scroll_list(infoPtr, nScrollDiff, 0);
9881 return 0;
9884 static LRESULT LISTVIEW_MouseWheel(LISTVIEW_INFO *infoPtr, INT wheelDelta)
9886 INT pulScrollLines = 3;
9888 TRACE("(wheelDelta=%d)\n", wheelDelta);
9890 switch(infoPtr->uView)
9892 case LV_VIEW_ICON:
9893 case LV_VIEW_SMALLICON:
9895 * listview should be scrolled by a multiple of 37 dependently on its dimension or its visible item number
9896 * should be fixed in the future.
9898 LISTVIEW_VScroll(infoPtr, SB_INTERNAL, (wheelDelta > 0) ?
9899 -LISTVIEW_SCROLL_ICON_LINE_SIZE : LISTVIEW_SCROLL_ICON_LINE_SIZE);
9900 break;
9902 case LV_VIEW_DETAILS:
9903 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES,0, &pulScrollLines, 0);
9905 /* if scrolling changes direction, ignore left overs */
9906 if ((wheelDelta < 0 && infoPtr->cWheelRemainder < 0) ||
9907 (wheelDelta > 0 && infoPtr->cWheelRemainder > 0))
9908 infoPtr->cWheelRemainder += wheelDelta;
9909 else
9910 infoPtr->cWheelRemainder = wheelDelta;
9911 if (infoPtr->cWheelRemainder && pulScrollLines)
9913 int cLineScroll;
9914 pulScrollLines = min((UINT)LISTVIEW_GetCountPerColumn(infoPtr), pulScrollLines);
9915 cLineScroll = pulScrollLines * infoPtr->cWheelRemainder / WHEEL_DELTA;
9916 infoPtr->cWheelRemainder -= WHEEL_DELTA * cLineScroll / pulScrollLines;
9917 LISTVIEW_VScroll(infoPtr, SB_INTERNAL, -cLineScroll);
9919 break;
9921 case LV_VIEW_LIST:
9922 LISTVIEW_HScroll(infoPtr, (wheelDelta > 0) ? SB_LINELEFT : SB_LINERIGHT, 0);
9923 break;
9925 return 0;
9928 /***
9929 * DESCRIPTION:
9930 * ???
9932 * PARAMETER(S):
9933 * [I] infoPtr : valid pointer to the listview structure
9934 * [I] nVirtualKey : virtual key
9935 * [I] lKeyData : key data
9937 * RETURN:
9938 * Zero
9940 static LRESULT LISTVIEW_KeyDown(LISTVIEW_INFO *infoPtr, INT nVirtualKey, LONG lKeyData)
9942 HWND hwndSelf = infoPtr->hwndSelf;
9943 INT nItem = -1;
9944 NMLVKEYDOWN nmKeyDown;
9946 TRACE("(nVirtualKey=%d, lKeyData=%d)\n", nVirtualKey, lKeyData);
9948 /* send LVN_KEYDOWN notification */
9949 nmKeyDown.wVKey = nVirtualKey;
9950 nmKeyDown.flags = 0;
9951 notify_hdr(infoPtr, LVN_KEYDOWN, &nmKeyDown.hdr);
9952 if (!IsWindow(hwndSelf))
9953 return 0;
9955 switch (nVirtualKey)
9957 case VK_SPACE:
9958 nItem = infoPtr->nFocusedItem;
9959 if (infoPtr->dwLvExStyle & LVS_EX_CHECKBOXES)
9960 toggle_checkbox_state(infoPtr, infoPtr->nFocusedItem);
9961 break;
9963 case VK_RETURN:
9964 if ((infoPtr->nItemCount > 0) && (infoPtr->nFocusedItem != -1))
9966 if (!notify(infoPtr, NM_RETURN)) return 0;
9967 if (!notify(infoPtr, LVN_ITEMACTIVATE)) return 0;
9969 break;
9971 case VK_HOME:
9972 if (infoPtr->nItemCount > 0)
9973 nItem = 0;
9974 break;
9976 case VK_END:
9977 if (infoPtr->nItemCount > 0)
9978 nItem = infoPtr->nItemCount - 1;
9979 break;
9981 case VK_LEFT:
9982 nItem = LISTVIEW_GetNextItem(infoPtr, infoPtr->nFocusedItem, LVNI_TOLEFT);
9983 break;
9985 case VK_UP:
9986 nItem = LISTVIEW_GetNextItem(infoPtr, infoPtr->nFocusedItem, LVNI_ABOVE);
9987 break;
9989 case VK_RIGHT:
9990 nItem = LISTVIEW_GetNextItem(infoPtr, infoPtr->nFocusedItem, LVNI_TORIGHT);
9991 break;
9993 case VK_DOWN:
9994 nItem = LISTVIEW_GetNextItem(infoPtr, infoPtr->nFocusedItem, LVNI_BELOW);
9995 break;
9997 case VK_PRIOR:
9998 if (infoPtr->uView == LV_VIEW_DETAILS)
10000 INT topidx = LISTVIEW_GetTopIndex(infoPtr);
10001 if (infoPtr->nFocusedItem == topidx)
10002 nItem = topidx - LISTVIEW_GetCountPerColumn(infoPtr) + 1;
10003 else
10004 nItem = topidx;
10006 else
10007 nItem = infoPtr->nFocusedItem - LISTVIEW_GetCountPerColumn(infoPtr)
10008 * LISTVIEW_GetCountPerRow(infoPtr);
10009 if(nItem < 0) nItem = 0;
10010 break;
10012 case VK_NEXT:
10013 if (infoPtr->uView == LV_VIEW_DETAILS)
10015 INT topidx = LISTVIEW_GetTopIndex(infoPtr);
10016 INT cnt = LISTVIEW_GetCountPerColumn(infoPtr);
10017 if (infoPtr->nFocusedItem == topidx + cnt - 1)
10018 nItem = infoPtr->nFocusedItem + cnt - 1;
10019 else
10020 nItem = topidx + cnt - 1;
10022 else
10023 nItem = infoPtr->nFocusedItem + LISTVIEW_GetCountPerColumn(infoPtr)
10024 * LISTVIEW_GetCountPerRow(infoPtr);
10025 if(nItem >= infoPtr->nItemCount) nItem = infoPtr->nItemCount - 1;
10026 break;
10029 if ((nItem != -1) && (nItem != infoPtr->nFocusedItem || nVirtualKey == VK_SPACE))
10030 LISTVIEW_KeySelection(infoPtr, nItem, nVirtualKey == VK_SPACE);
10032 return 0;
10035 /***
10036 * DESCRIPTION:
10037 * Kills the focus.
10039 * PARAMETER(S):
10040 * [I] infoPtr : valid pointer to the listview structure
10042 * RETURN:
10043 * Zero
10045 static LRESULT LISTVIEW_KillFocus(LISTVIEW_INFO *infoPtr)
10047 TRACE("()\n");
10049 /* drop any left over scroll amount */
10050 infoPtr->cWheelRemainder = 0;
10052 /* if we did not have the focus, there's nothing more to do */
10053 if (!infoPtr->bFocus) return 0;
10055 /* send NM_KILLFOCUS notification */
10056 if (!notify(infoPtr, NM_KILLFOCUS)) return 0;
10058 /* if we have a focus rectangle, get rid of it */
10059 LISTVIEW_ShowFocusRect(infoPtr, FALSE);
10061 /* if have a marquee selection, stop it */
10062 if (infoPtr->bMarqueeSelect)
10064 /* Remove the marquee rectangle and release our mouse capture */
10065 LISTVIEW_InvalidateRect(infoPtr, &infoPtr->marqueeRect);
10066 ReleaseCapture();
10068 SetRectEmpty(&infoPtr->marqueeRect);
10070 infoPtr->bMarqueeSelect = FALSE;
10071 infoPtr->bScrolling = FALSE;
10072 KillTimer(infoPtr->hwndSelf, (UINT_PTR) infoPtr);
10075 /* set window focus flag */
10076 infoPtr->bFocus = FALSE;
10078 /* invalidate the selected items before resetting focus flag */
10079 LISTVIEW_InvalidateSelectedItems(infoPtr);
10081 return 0;
10084 /***
10085 * DESCRIPTION:
10086 * Processes double click messages (left mouse button).
10088 * PARAMETER(S):
10089 * [I] infoPtr : valid pointer to the listview structure
10090 * [I] wKey : key flag
10091 * [I] x,y : mouse coordinate
10093 * RETURN:
10094 * Zero
10096 static LRESULT LISTVIEW_LButtonDblClk(LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
10098 LVHITTESTINFO htInfo;
10100 TRACE("(key=%hu, X=%u, Y=%u)\n", wKey, x, y);
10102 /* Cancel the item edition if any */
10103 if (infoPtr->itemEdit.fEnabled)
10105 KillTimer(infoPtr->hwndSelf, (UINT_PTR)&infoPtr->itemEdit);
10106 infoPtr->itemEdit.fEnabled = FALSE;
10109 /* send NM_RELEASEDCAPTURE notification */
10110 if (!notify(infoPtr, NM_RELEASEDCAPTURE)) return 0;
10112 htInfo.pt.x = x;
10113 htInfo.pt.y = y;
10115 /* send NM_DBLCLK notification */
10116 LISTVIEW_HitTest(infoPtr, &htInfo, TRUE, FALSE);
10117 if (!notify_click(infoPtr, NM_DBLCLK, &htInfo)) return 0;
10119 /* To send the LVN_ITEMACTIVATE, it must be on an Item */
10120 if(htInfo.iItem != -1) notify_itemactivate(infoPtr,&htInfo);
10122 return 0;
10125 static LRESULT LISTVIEW_TrackMouse(const LISTVIEW_INFO *infoPtr, POINT pt)
10127 MSG msg;
10128 RECT r;
10130 r.top = r.bottom = pt.y;
10131 r.left = r.right = pt.x;
10133 InflateRect(&r, GetSystemMetrics(SM_CXDRAG), GetSystemMetrics(SM_CYDRAG));
10135 SetCapture(infoPtr->hwndSelf);
10137 while (1)
10139 if (PeekMessageW(&msg, 0, 0, 0, PM_REMOVE | PM_NOYIELD))
10141 if (msg.message == WM_MOUSEMOVE)
10143 pt.x = (short)LOWORD(msg.lParam);
10144 pt.y = (short)HIWORD(msg.lParam);
10145 if (PtInRect(&r, pt))
10146 continue;
10147 else
10149 ReleaseCapture();
10150 return 1;
10153 else if (msg.message >= WM_LBUTTONDOWN &&
10154 msg.message <= WM_RBUTTONDBLCLK)
10156 break;
10159 DispatchMessageW(&msg);
10162 if (GetCapture() != infoPtr->hwndSelf)
10163 return 0;
10166 ReleaseCapture();
10167 return 0;
10171 /***
10172 * DESCRIPTION:
10173 * Processes mouse down messages (left mouse button).
10175 * PARAMETERS:
10176 * infoPtr [I ] valid pointer to the listview structure
10177 * wKey [I ] key flag
10178 * x,y [I ] mouse coordinate
10180 * RETURN:
10181 * Zero
10183 static LRESULT LISTVIEW_LButtonDown(LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
10185 LVHITTESTINFO lvHitTestInfo;
10186 static BOOL bGroupSelect = TRUE;
10187 POINT pt = { x, y };
10188 INT nItem;
10190 TRACE("(key=%hu, X=%u, Y=%u)\n", wKey, x, y);
10192 /* send NM_RELEASEDCAPTURE notification */
10193 if (!notify(infoPtr, NM_RELEASEDCAPTURE)) return 0;
10195 /* set left button down flag and record the click position */
10196 infoPtr->bLButtonDown = TRUE;
10197 infoPtr->ptClickPos = pt;
10198 infoPtr->bDragging = FALSE;
10199 infoPtr->bMarqueeSelect = FALSE;
10200 infoPtr->bScrolling = FALSE;
10202 lvHitTestInfo.pt.x = x;
10203 lvHitTestInfo.pt.y = y;
10205 nItem = LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, TRUE);
10206 TRACE("at %s, nItem=%d\n", wine_dbgstr_point(&pt), nItem);
10207 if ((nItem >= 0) && (nItem < infoPtr->nItemCount))
10209 if ((infoPtr->dwLvExStyle & LVS_EX_CHECKBOXES) && (lvHitTestInfo.flags & LVHT_ONITEMSTATEICON))
10211 notify_click(infoPtr, NM_CLICK, &lvHitTestInfo);
10212 toggle_checkbox_state(infoPtr, nItem);
10213 infoPtr->bLButtonDown = FALSE;
10214 return 0;
10217 if (infoPtr->dwStyle & LVS_SINGLESEL)
10219 if (LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED))
10220 infoPtr->nEditLabelItem = nItem;
10221 else
10222 LISTVIEW_SetSelection(infoPtr, nItem);
10224 else
10226 if ((wKey & MK_CONTROL) && (wKey & MK_SHIFT))
10228 if (bGroupSelect)
10230 if (!LISTVIEW_AddGroupSelection(infoPtr, nItem)) return 0;
10231 LISTVIEW_SetItemFocus(infoPtr, nItem);
10232 infoPtr->nSelectionMark = nItem;
10234 else
10236 LVITEMW item;
10238 item.state = LVIS_SELECTED | LVIS_FOCUSED;
10239 item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
10241 LISTVIEW_SetItemState(infoPtr,nItem,&item);
10242 infoPtr->nSelectionMark = nItem;
10245 else if (wKey & MK_CONTROL)
10247 LVITEMW item;
10249 bGroupSelect = (LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED) == 0);
10251 item.state = (bGroupSelect ? LVIS_SELECTED : 0) | LVIS_FOCUSED;
10252 item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
10253 LISTVIEW_SetItemState(infoPtr, nItem, &item);
10254 infoPtr->nSelectionMark = nItem;
10256 else if (wKey & MK_SHIFT)
10258 LISTVIEW_SetGroupSelection(infoPtr, nItem);
10260 else
10262 if (LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED))
10264 infoPtr->nEditLabelItem = nItem;
10265 infoPtr->nLButtonDownItem = nItem;
10267 LISTVIEW_SetItemFocus(infoPtr, nItem);
10269 else
10270 /* set selection (clears other pre-existing selections) */
10271 LISTVIEW_SetSelection(infoPtr, nItem);
10275 if (!infoPtr->bFocus)
10276 SetFocus(infoPtr->hwndSelf);
10278 if (infoPtr->dwLvExStyle & LVS_EX_ONECLICKACTIVATE)
10279 if(lvHitTestInfo.iItem != -1) notify_itemactivate(infoPtr,&lvHitTestInfo);
10281 else
10283 if (!infoPtr->bFocus)
10284 SetFocus(infoPtr->hwndSelf);
10286 /* remove all selections */
10287 if (!(wKey & MK_CONTROL) && !(wKey & MK_SHIFT))
10288 LISTVIEW_DeselectAll(infoPtr);
10289 ReleaseCapture();
10292 return 0;
10295 /***
10296 * DESCRIPTION:
10297 * Processes mouse up messages (left mouse button).
10299 * PARAMETERS:
10300 * infoPtr [I ] valid pointer to the listview structure
10301 * wKey [I ] key flag
10302 * x,y [I ] mouse coordinate
10304 * RETURN:
10305 * Zero
10307 static LRESULT LISTVIEW_LButtonUp(LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
10309 LVHITTESTINFO lvHitTestInfo;
10311 TRACE("(key=%hu, X=%u, Y=%u)\n", wKey, x, y);
10313 if (!infoPtr->bLButtonDown) return 0;
10315 lvHitTestInfo.pt.x = x;
10316 lvHitTestInfo.pt.y = y;
10318 /* send NM_CLICK notification */
10319 LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, FALSE);
10320 if (!notify_click(infoPtr, NM_CLICK, &lvHitTestInfo)) return 0;
10322 /* set left button flag */
10323 infoPtr->bLButtonDown = FALSE;
10325 /* set a single selection, reset others */
10326 if(lvHitTestInfo.iItem == infoPtr->nLButtonDownItem && lvHitTestInfo.iItem != -1)
10327 LISTVIEW_SetSelection(infoPtr, infoPtr->nLButtonDownItem);
10328 infoPtr->nLButtonDownItem = -1;
10330 if (infoPtr->bDragging || infoPtr->bMarqueeSelect)
10332 /* Remove the marquee rectangle and release our mouse capture */
10333 if (infoPtr->bMarqueeSelect)
10335 LISTVIEW_InvalidateRect(infoPtr, &infoPtr->marqueeDrawRect);
10336 ReleaseCapture();
10339 SetRectEmpty(&infoPtr->marqueeRect);
10340 SetRectEmpty(&infoPtr->marqueeDrawRect);
10342 infoPtr->bDragging = FALSE;
10343 infoPtr->bMarqueeSelect = FALSE;
10344 infoPtr->bScrolling = FALSE;
10346 KillTimer(infoPtr->hwndSelf, (UINT_PTR) infoPtr);
10347 return 0;
10350 /* if we clicked on a selected item, edit the label */
10351 if(lvHitTestInfo.iItem == infoPtr->nEditLabelItem && (lvHitTestInfo.flags & LVHT_ONITEMLABEL))
10353 /* we want to make sure the user doesn't want to do a double click. So we will
10354 * delay the edit. WM_LBUTTONDBLCLICK will cancel the timer
10356 infoPtr->itemEdit.fEnabled = TRUE;
10357 infoPtr->itemEdit.iItem = lvHitTestInfo.iItem;
10358 SetTimer(infoPtr->hwndSelf,
10359 (UINT_PTR)&infoPtr->itemEdit,
10360 GetDoubleClickTime(),
10361 LISTVIEW_DelayedEditItem);
10364 return 0;
10367 /***
10368 * DESCRIPTION:
10369 * Destroys the listview control (called after WM_DESTROY).
10371 * PARAMETER(S):
10372 * [I] infoPtr : valid pointer to the listview structure
10374 * RETURN:
10375 * Zero
10377 static LRESULT LISTVIEW_NCDestroy(LISTVIEW_INFO *infoPtr)
10379 INT i;
10381 TRACE("()\n");
10383 /* destroy data structure */
10384 DPA_Destroy(infoPtr->hdpaItems);
10385 DPA_Destroy(infoPtr->hdpaItemIds);
10386 DPA_Destroy(infoPtr->hdpaPosX);
10387 DPA_Destroy(infoPtr->hdpaPosY);
10388 /* columns */
10389 for (i = 0; i < DPA_GetPtrCount(infoPtr->hdpaColumns); i++)
10390 Free(DPA_GetPtr(infoPtr->hdpaColumns, i));
10391 DPA_Destroy(infoPtr->hdpaColumns);
10392 ranges_destroy(infoPtr->selectionRanges);
10394 /* destroy image lists */
10395 if (!(infoPtr->dwStyle & LVS_SHAREIMAGELISTS))
10397 ImageList_Destroy(infoPtr->himlNormal);
10398 ImageList_Destroy(infoPtr->himlSmall);
10399 ImageList_Destroy(infoPtr->himlState);
10402 /* destroy font, bkgnd brush */
10403 infoPtr->hFont = 0;
10404 if (infoPtr->hDefaultFont) DeleteObject(infoPtr->hDefaultFont);
10405 if (infoPtr->clrBk != CLR_NONE) DeleteObject(infoPtr->hBkBrush);
10407 SetWindowLongPtrW(infoPtr->hwndSelf, 0, 0);
10409 /* free listview info pointer*/
10410 Free(infoPtr);
10412 return 0;
10415 /***
10416 * DESCRIPTION:
10417 * Handles notifications.
10419 * PARAMETER(S):
10420 * [I] infoPtr : valid pointer to the listview structure
10421 * [I] lpnmhdr : notification information
10423 * RETURN:
10424 * Zero
10426 static LRESULT LISTVIEW_Notify(LISTVIEW_INFO *infoPtr, NMHDR *lpnmhdr)
10428 NMHEADERW *lpnmh;
10430 TRACE("(lpnmhdr=%p)\n", lpnmhdr);
10432 if (!lpnmhdr || lpnmhdr->hwndFrom != infoPtr->hwndHeader) return 0;
10434 /* remember: HDN_LAST < HDN_FIRST */
10435 if (lpnmhdr->code > HDN_FIRST || lpnmhdr->code < HDN_LAST) return 0;
10436 lpnmh = (NMHEADERW *)lpnmhdr;
10438 if (lpnmh->iItem < 0 || lpnmh->iItem >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return 0;
10440 switch (lpnmhdr->code)
10442 case HDN_TRACKW:
10443 case HDN_TRACKA:
10445 COLUMN_INFO *lpColumnInfo;
10446 POINT ptOrigin;
10447 INT x;
10449 if (!lpnmh->pitem || !(lpnmh->pitem->mask & HDI_WIDTH))
10450 break;
10452 /* remove the old line (if any) */
10453 LISTVIEW_DrawTrackLine(infoPtr);
10455 /* compute & draw the new line */
10456 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, lpnmh->iItem);
10457 x = lpColumnInfo->rcHeader.left + lpnmh->pitem->cxy;
10458 LISTVIEW_GetOrigin(infoPtr, &ptOrigin);
10459 infoPtr->xTrackLine = x + ptOrigin.x;
10460 LISTVIEW_DrawTrackLine(infoPtr);
10461 return notify_forward_header(infoPtr, lpnmh);
10464 case HDN_ENDTRACKA:
10465 case HDN_ENDTRACKW:
10466 /* remove the track line (if any) */
10467 LISTVIEW_DrawTrackLine(infoPtr);
10468 infoPtr->xTrackLine = -1;
10469 return notify_forward_header(infoPtr, lpnmh);
10471 case HDN_BEGINDRAG:
10472 if ((infoPtr->dwLvExStyle & LVS_EX_HEADERDRAGDROP) == 0) return 1;
10473 return notify_forward_header(infoPtr, lpnmh);
10475 case HDN_ENDDRAG:
10476 infoPtr->colRectsDirty = TRUE;
10477 LISTVIEW_InvalidateList(infoPtr);
10478 return notify_forward_header(infoPtr, lpnmh);
10480 case HDN_ITEMCHANGEDW:
10481 case HDN_ITEMCHANGEDA:
10483 COLUMN_INFO *lpColumnInfo;
10484 HDITEMW hdi;
10485 INT dx, cxy;
10487 if (!lpnmh->pitem || !(lpnmh->pitem->mask & HDI_WIDTH))
10489 hdi.mask = HDI_WIDTH;
10490 if (!SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, lpnmh->iItem, (LPARAM)&hdi)) return 0;
10491 cxy = hdi.cxy;
10493 else
10494 cxy = lpnmh->pitem->cxy;
10496 /* determine how much we change since the last know position */
10497 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, lpnmh->iItem);
10498 dx = cxy - (lpColumnInfo->rcHeader.right - lpColumnInfo->rcHeader.left);
10499 if (dx != 0)
10501 lpColumnInfo->rcHeader.right += dx;
10503 hdi.mask = HDI_ORDER;
10504 SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, lpnmh->iItem, (LPARAM)&hdi);
10506 /* not the rightmost one */
10507 if (hdi.iOrder + 1 < DPA_GetPtrCount(infoPtr->hdpaColumns))
10509 INT nIndex = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX,
10510 hdi.iOrder + 1, 0);
10511 LISTVIEW_ScrollColumns(infoPtr, nIndex, dx);
10513 else
10515 /* only needs to update the scrolls */
10516 infoPtr->nItemWidth += dx;
10517 LISTVIEW_UpdateScroll(infoPtr);
10519 LISTVIEW_UpdateItemSize(infoPtr);
10520 if (infoPtr->uView == LV_VIEW_DETAILS && is_redrawing(infoPtr))
10522 POINT ptOrigin;
10523 RECT rcCol = lpColumnInfo->rcHeader;
10525 LISTVIEW_GetOrigin(infoPtr, &ptOrigin);
10526 OffsetRect(&rcCol, ptOrigin.x, 0);
10528 rcCol.top = infoPtr->rcList.top;
10529 rcCol.bottom = infoPtr->rcList.bottom;
10531 /* resizing left-aligned columns leaves most of the left side untouched */
10532 if ((lpColumnInfo->fmt & LVCFMT_JUSTIFYMASK) == LVCFMT_LEFT)
10534 INT nMaxDirty = infoPtr->nEllipsisWidth + infoPtr->ntmMaxCharWidth;
10535 if (dx > 0)
10536 nMaxDirty += dx;
10537 rcCol.left = max (rcCol.left, rcCol.right - nMaxDirty);
10540 /* when shrinking the last column clear the now unused field */
10541 if (hdi.iOrder == DPA_GetPtrCount(infoPtr->hdpaColumns) - 1)
10543 RECT right;
10545 rcCol.right -= dx;
10547 /* deal with right from rightmost column area */
10548 right.left = rcCol.right;
10549 right.top = rcCol.top;
10550 right.bottom = rcCol.bottom;
10551 right.right = infoPtr->rcList.right;
10553 LISTVIEW_InvalidateRect(infoPtr, &right);
10556 LISTVIEW_InvalidateRect(infoPtr, &rcCol);
10559 break;
10562 case HDN_ITEMCLICKW:
10563 case HDN_ITEMCLICKA:
10565 /* Handle sorting by Header Column */
10566 NMLISTVIEW nmlv;
10568 ZeroMemory(&nmlv, sizeof(NMLISTVIEW));
10569 nmlv.iItem = -1;
10570 nmlv.iSubItem = lpnmh->iItem;
10571 notify_listview(infoPtr, LVN_COLUMNCLICK, &nmlv);
10572 return notify_forward_header(infoPtr, lpnmh);
10575 case HDN_DIVIDERDBLCLICKW:
10576 case HDN_DIVIDERDBLCLICKA:
10577 /* FIXME: for LVS_EX_HEADERINALLVIEWS and not LV_VIEW_DETAILS
10578 we should use LVSCW_AUTOSIZE_USEHEADER, helper rework or
10579 split needed for that */
10580 LISTVIEW_SetColumnWidth(infoPtr, lpnmh->iItem, LVSCW_AUTOSIZE);
10581 return notify_forward_header(infoPtr, lpnmh);
10583 return 0;
10586 /***
10587 * DESCRIPTION:
10588 * Paint non-client area of control.
10590 * PARAMETER(S):
10591 * [I] infoPtr : valid pointer to the listview structureof the sender
10592 * [I] region : update region
10594 * RETURN:
10595 * 0 - frame was painted
10597 static LRESULT LISTVIEW_NCPaint(const LISTVIEW_INFO *infoPtr, HRGN region)
10599 LONG exstyle = GetWindowLongW (infoPtr->hwndSelf, GWL_EXSTYLE);
10600 HTHEME theme = GetWindowTheme (infoPtr->hwndSelf);
10601 HDC dc;
10602 RECT r;
10603 HRGN cliprgn;
10604 int cxEdge = GetSystemMetrics (SM_CXEDGE),
10605 cyEdge = GetSystemMetrics (SM_CYEDGE);
10607 if (!theme || !(exstyle & WS_EX_CLIENTEDGE))
10608 return DefWindowProcW (infoPtr->hwndSelf, WM_NCPAINT, (WPARAM)region, 0);
10610 GetWindowRect(infoPtr->hwndSelf, &r);
10612 cliprgn = CreateRectRgn (r.left + cxEdge, r.top + cyEdge,
10613 r.right - cxEdge, r.bottom - cyEdge);
10614 if (region != (HRGN)1)
10615 CombineRgn (cliprgn, cliprgn, region, RGN_AND);
10616 OffsetRect(&r, -r.left, -r.top);
10618 dc = GetDCEx(infoPtr->hwndSelf, region, DCX_WINDOW|DCX_INTERSECTRGN);
10620 if (IsThemeBackgroundPartiallyTransparent (theme, 0, 0))
10621 DrawThemeParentBackground(infoPtr->hwndSelf, dc, &r);
10622 DrawThemeBackground (theme, dc, 0, 0, &r, 0);
10623 ReleaseDC(infoPtr->hwndSelf, dc);
10625 /* Call default proc to get the scrollbars etc. painted */
10626 DefWindowProcW (infoPtr->hwndSelf, WM_NCPAINT, (WPARAM)cliprgn, 0);
10627 DeleteObject(cliprgn);
10629 return 0;
10632 /***
10633 * DESCRIPTION:
10634 * Determines the type of structure to use.
10636 * PARAMETER(S):
10637 * [I] infoPtr : valid pointer to the listview structureof the sender
10638 * [I] hwndFrom : listview window handle
10639 * [I] nCommand : command specifying the nature of the WM_NOTIFYFORMAT
10641 * RETURN:
10642 * Zero
10644 static LRESULT LISTVIEW_NotifyFormat(LISTVIEW_INFO *infoPtr, HWND hwndFrom, INT nCommand)
10646 TRACE("(hwndFrom=%p, nCommand=%d)\n", hwndFrom, nCommand);
10648 if (nCommand == NF_REQUERY)
10649 infoPtr->notifyFormat = SendMessageW(infoPtr->hwndNotify, WM_NOTIFYFORMAT, (WPARAM)infoPtr->hwndSelf, NF_QUERY);
10651 return infoPtr->notifyFormat;
10654 /***
10655 * DESCRIPTION:
10656 * Paints/Repaints the listview control. Internal use.
10658 * PARAMETER(S):
10659 * [I] infoPtr : valid pointer to the listview structure
10660 * [I] hdc : device context handle
10662 * RETURN:
10663 * Zero
10665 static LRESULT LISTVIEW_Paint(LISTVIEW_INFO *infoPtr, HDC hdc)
10667 TRACE("(hdc=%p)\n", hdc);
10669 if (infoPtr->bNoItemMetrics && infoPtr->nItemCount)
10671 infoPtr->bNoItemMetrics = FALSE;
10672 LISTVIEW_UpdateItemSize(infoPtr);
10673 if (infoPtr->uView == LV_VIEW_ICON || infoPtr->uView == LV_VIEW_SMALLICON)
10674 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
10675 LISTVIEW_UpdateScroll(infoPtr);
10678 if (infoPtr->hwndHeader) UpdateWindow(infoPtr->hwndHeader);
10680 if (hdc)
10681 LISTVIEW_Refresh(infoPtr, hdc, NULL);
10682 else
10684 PAINTSTRUCT ps;
10686 hdc = BeginPaint(infoPtr->hwndSelf, &ps);
10687 if (!hdc) return 1;
10688 LISTVIEW_Refresh(infoPtr, hdc, ps.fErase ? &ps.rcPaint : NULL);
10689 EndPaint(infoPtr->hwndSelf, &ps);
10692 return 0;
10695 /***
10696 * DESCRIPTION:
10697 * Paints/Repaints the listview control, WM_PAINT handler.
10699 * PARAMETER(S):
10700 * [I] infoPtr : valid pointer to the listview structure
10701 * [I] hdc : device context handle
10703 * RETURN:
10704 * Zero
10706 static inline LRESULT LISTVIEW_WMPaint(LISTVIEW_INFO *infoPtr, HDC hdc)
10708 TRACE("(hdc=%p)\n", hdc);
10710 if (!is_redrawing(infoPtr))
10711 return DefWindowProcW (infoPtr->hwndSelf, WM_PAINT, (WPARAM)hdc, 0);
10713 return LISTVIEW_Paint(infoPtr, hdc);
10716 /***
10717 * DESCRIPTION:
10718 * Paints/Repaints the listview control.
10720 * PARAMETER(S):
10721 * [I] infoPtr : valid pointer to the listview structure
10722 * [I] hdc : device context handle
10723 * [I] options : drawing options
10725 * RETURN:
10726 * Zero
10728 static LRESULT LISTVIEW_PrintClient(LISTVIEW_INFO *infoPtr, HDC hdc, DWORD options)
10730 if ((options & PRF_CHECKVISIBLE) && !IsWindowVisible(infoPtr->hwndSelf))
10731 return 0;
10733 if (options & ~(PRF_ERASEBKGND|PRF_CLIENT))
10734 FIXME("(hdc=%p options=0x%08x) partial stub\n", hdc, options);
10736 if (options & PRF_ERASEBKGND)
10737 LISTVIEW_EraseBkgnd(infoPtr, hdc);
10739 if (options & PRF_CLIENT)
10740 LISTVIEW_Paint(infoPtr, hdc);
10742 return 0;
10746 /***
10747 * DESCRIPTION:
10748 * Processes double click messages (right mouse button).
10750 * PARAMETER(S):
10751 * [I] infoPtr : valid pointer to the listview structure
10752 * [I] wKey : key flag
10753 * [I] x,y : mouse coordinate
10755 * RETURN:
10756 * Zero
10758 static LRESULT LISTVIEW_RButtonDblClk(const LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
10760 LVHITTESTINFO lvHitTestInfo;
10762 TRACE("(key=%hu,X=%u,Y=%u)\n", wKey, x, y);
10764 /* send NM_RELEASEDCAPTURE notification */
10765 if (!notify(infoPtr, NM_RELEASEDCAPTURE)) return 0;
10767 /* send NM_RDBLCLK notification */
10768 lvHitTestInfo.pt.x = x;
10769 lvHitTestInfo.pt.y = y;
10770 LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, FALSE);
10771 notify_click(infoPtr, NM_RDBLCLK, &lvHitTestInfo);
10773 return 0;
10776 /***
10777 * DESCRIPTION:
10778 * Processes WM_RBUTTONDOWN message and corresponding drag operation.
10780 * PARAMETER(S):
10781 * [I] infoPtr : valid pointer to the listview structure
10782 * [I] wKey : key flag
10783 * [I] x, y : mouse coordinate
10785 * RETURN:
10786 * Zero
10788 static LRESULT LISTVIEW_RButtonDown(LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
10790 LVHITTESTINFO ht;
10791 INT item;
10793 TRACE("(key=%hu, x=%d, y=%d)\n", wKey, x, y);
10795 /* send NM_RELEASEDCAPTURE notification */
10796 if (!notify(infoPtr, NM_RELEASEDCAPTURE)) return 0;
10798 /* determine the index of the selected item */
10799 ht.pt.x = x;
10800 ht.pt.y = y;
10801 item = LISTVIEW_HitTest(infoPtr, &ht, TRUE, TRUE);
10803 /* make sure the listview control window has the focus */
10804 if (!infoPtr->bFocus) SetFocus(infoPtr->hwndSelf);
10806 if ((item >= 0) && (item < infoPtr->nItemCount))
10808 LISTVIEW_SetItemFocus(infoPtr, item);
10809 if (!((wKey & MK_SHIFT) || (wKey & MK_CONTROL)) &&
10810 !LISTVIEW_GetItemState(infoPtr, item, LVIS_SELECTED))
10811 LISTVIEW_SetSelection(infoPtr, item);
10813 else
10814 LISTVIEW_DeselectAll(infoPtr);
10816 if (LISTVIEW_TrackMouse(infoPtr, ht.pt))
10818 if (ht.iItem != -1)
10820 NMLISTVIEW nmlv;
10822 memset(&nmlv, 0, sizeof(nmlv));
10823 nmlv.iItem = ht.iItem;
10824 nmlv.ptAction = ht.pt;
10826 notify_listview(infoPtr, LVN_BEGINRDRAG, &nmlv);
10829 else
10831 SetFocus(infoPtr->hwndSelf);
10833 ht.pt.x = x;
10834 ht.pt.y = y;
10835 LISTVIEW_HitTest(infoPtr, &ht, TRUE, FALSE);
10837 if (notify_click(infoPtr, NM_RCLICK, &ht))
10839 /* Send a WM_CONTEXTMENU message in response to the WM_RBUTTONUP */
10840 SendMessageW(infoPtr->hwndSelf, WM_CONTEXTMENU,
10841 (WPARAM)infoPtr->hwndSelf, (LPARAM)GetMessagePos());
10845 return 0;
10848 /***
10849 * DESCRIPTION:
10850 * Sets the cursor.
10852 * PARAMETER(S):
10853 * [I] infoPtr : valid pointer to the listview structure
10854 * [I] hwnd : window handle of window containing the cursor
10855 * [I] nHittest : hit-test code
10856 * [I] wMouseMsg : ideintifier of the mouse message
10858 * RETURN:
10859 * TRUE if cursor is set
10860 * FALSE otherwise
10862 static BOOL LISTVIEW_SetCursor(const LISTVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
10864 LVHITTESTINFO lvHitTestInfo;
10866 if (!LISTVIEW_IsHotTracking(infoPtr)) goto forward;
10868 if (!infoPtr->hHotCursor) goto forward;
10870 GetCursorPos(&lvHitTestInfo.pt);
10871 if (LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, FALSE, FALSE) < 0) goto forward;
10873 SetCursor(infoPtr->hHotCursor);
10875 return TRUE;
10877 forward:
10879 return DefWindowProcW(infoPtr->hwndSelf, WM_SETCURSOR, wParam, lParam);
10882 /***
10883 * DESCRIPTION:
10884 * Sets the focus.
10886 * PARAMETER(S):
10887 * [I] infoPtr : valid pointer to the listview structure
10888 * [I] hwndLoseFocus : handle of previously focused window
10890 * RETURN:
10891 * Zero
10893 static LRESULT LISTVIEW_SetFocus(LISTVIEW_INFO *infoPtr, HWND hwndLoseFocus)
10895 TRACE("(hwndLoseFocus=%p)\n", hwndLoseFocus);
10897 /* if we have the focus already, there's nothing to do */
10898 if (infoPtr->bFocus) return 0;
10900 /* send NM_SETFOCUS notification */
10901 if (!notify(infoPtr, NM_SETFOCUS)) return 0;
10903 /* set window focus flag */
10904 infoPtr->bFocus = TRUE;
10906 /* put the focus rect back on */
10907 LISTVIEW_ShowFocusRect(infoPtr, TRUE);
10909 /* redraw all visible selected items */
10910 LISTVIEW_InvalidateSelectedItems(infoPtr);
10912 return 0;
10915 /***
10916 * DESCRIPTION:
10917 * Sets the font.
10919 * PARAMETER(S):
10920 * [I] infoPtr : valid pointer to the listview structure
10921 * [I] fRedraw : font handle
10922 * [I] fRedraw : redraw flag
10924 * RETURN:
10925 * Zero
10927 static LRESULT LISTVIEW_SetFont(LISTVIEW_INFO *infoPtr, HFONT hFont, WORD fRedraw)
10929 HFONT oldFont = infoPtr->hFont;
10930 INT oldHeight = infoPtr->nItemHeight;
10932 TRACE("(hfont=%p,redraw=%hu)\n", hFont, fRedraw);
10934 infoPtr->hFont = hFont ? hFont : infoPtr->hDefaultFont;
10935 if (infoPtr->hFont == oldFont) return 0;
10937 LISTVIEW_SaveTextMetrics(infoPtr);
10939 infoPtr->nItemHeight = LISTVIEW_CalculateItemHeight(infoPtr);
10941 if (infoPtr->uView == LV_VIEW_DETAILS)
10943 SendMessageW(infoPtr->hwndHeader, WM_SETFONT, (WPARAM)hFont, MAKELPARAM(fRedraw, 0));
10944 LISTVIEW_UpdateSize(infoPtr);
10945 LISTVIEW_UpdateScroll(infoPtr);
10947 else if (infoPtr->nItemHeight != oldHeight)
10948 LISTVIEW_UpdateScroll(infoPtr);
10950 if (fRedraw) LISTVIEW_InvalidateList(infoPtr);
10952 return 0;
10955 /***
10956 * DESCRIPTION:
10957 * Message handling for WM_SETREDRAW.
10958 * For the Listview, it invalidates the entire window (the doc specifies otherwise)
10960 * PARAMETER(S):
10961 * [I] infoPtr : valid pointer to the listview structure
10962 * [I] redraw: state of redraw flag
10964 * RETURN:
10965 * Zero.
10967 static LRESULT LISTVIEW_SetRedraw(LISTVIEW_INFO *infoPtr, BOOL redraw)
10969 TRACE("old=%d, new=%d\n", infoPtr->redraw, redraw);
10971 if (infoPtr->redraw == !!redraw)
10972 return 0;
10974 if (!(infoPtr->redraw = !!redraw))
10975 return 0;
10977 if (is_autoarrange(infoPtr))
10978 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
10979 LISTVIEW_UpdateScroll(infoPtr);
10981 /* despite what the WM_SETREDRAW docs says, apps expect us
10982 * to invalidate the listview here... stupid! */
10983 LISTVIEW_InvalidateList(infoPtr);
10985 return 0;
10988 /***
10989 * DESCRIPTION:
10990 * Resizes the listview control. This function processes WM_SIZE
10991 * messages. At this time, the width and height are not used.
10993 * PARAMETER(S):
10994 * [I] infoPtr : valid pointer to the listview structure
10995 * [I] Width : new width
10996 * [I] Height : new height
10998 * RETURN:
10999 * Zero
11001 static LRESULT LISTVIEW_Size(LISTVIEW_INFO *infoPtr, int Width, int Height)
11003 RECT rcOld = infoPtr->rcList;
11005 TRACE("(width=%d, height=%d)\n", Width, Height);
11007 LISTVIEW_UpdateSize(infoPtr);
11008 if (EqualRect(&rcOld, &infoPtr->rcList)) return 0;
11010 /* do not bother with display related stuff if we're not redrawing */
11011 if (!is_redrawing(infoPtr)) return 0;
11013 if (is_autoarrange(infoPtr))
11014 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
11016 LISTVIEW_UpdateScroll(infoPtr);
11018 /* refresh all only for lists whose height changed significantly */
11019 if ((infoPtr->uView == LV_VIEW_LIST) &&
11020 (rcOld.bottom - rcOld.top) / infoPtr->nItemHeight !=
11021 (infoPtr->rcList.bottom - infoPtr->rcList.top) / infoPtr->nItemHeight)
11022 LISTVIEW_InvalidateList(infoPtr);
11024 return 0;
11027 /***
11028 * DESCRIPTION:
11029 * Sets the size information.
11031 * PARAMETER(S):
11032 * [I] infoPtr : valid pointer to the listview structure
11034 * RETURN:
11035 * None
11037 static void LISTVIEW_UpdateSize(LISTVIEW_INFO *infoPtr)
11039 TRACE("uView=%d, rcList(old)=%s\n", infoPtr->uView, wine_dbgstr_rect(&infoPtr->rcList));
11041 GetClientRect(infoPtr->hwndSelf, &infoPtr->rcList);
11043 if (infoPtr->uView == LV_VIEW_LIST)
11045 /* Apparently the "LIST" style is supposed to have the same
11046 * number of items in a column even if there is no scroll bar.
11047 * Since if a scroll bar already exists then the bottom is already
11048 * reduced, only reduce if the scroll bar does not currently exist.
11049 * The "2" is there to mimic the native control. I think it may be
11050 * related to either padding or edges. (GLA 7/2002)
11052 if (!(GetWindowLongW(infoPtr->hwndSelf, GWL_STYLE) & WS_HSCROLL))
11053 infoPtr->rcList.bottom -= GetSystemMetrics(SM_CYHSCROLL);
11054 infoPtr->rcList.bottom = max (infoPtr->rcList.bottom - 2, 0);
11057 /* When ListView control is created invisible, header isn't created right away. */
11058 if (infoPtr->hwndHeader)
11060 POINT origin;
11061 WINDOWPOS wp;
11062 HDLAYOUT hl;
11063 RECT rect;
11065 LISTVIEW_GetOrigin(infoPtr, &origin);
11067 rect = infoPtr->rcList;
11068 rect.left += origin.x;
11070 hl.prc = &rect;
11071 hl.pwpos = &wp;
11072 SendMessageW( infoPtr->hwndHeader, HDM_LAYOUT, 0, (LPARAM)&hl );
11073 TRACE(" wp.flags=0x%08x, wp=%d,%d (%dx%d)\n", wp.flags, wp.x, wp.y, wp.cx, wp.cy);
11075 if (LISTVIEW_IsHeaderEnabled(infoPtr))
11076 wp.flags |= SWP_SHOWWINDOW;
11077 else
11079 wp.flags |= SWP_HIDEWINDOW;
11080 wp.cy = 0;
11083 SetWindowPos(wp.hwnd, wp.hwndInsertAfter, wp.x, wp.y, wp.cx, wp.cy, wp.flags);
11084 TRACE(" after SWP wp=%d,%d (%dx%d)\n", wp.x, wp.y, wp.cx, wp.cy);
11086 infoPtr->rcList.top = max(wp.cy, 0);
11088 /* extra padding for grid */
11089 if (infoPtr->uView == LV_VIEW_DETAILS && infoPtr->dwLvExStyle & LVS_EX_GRIDLINES)
11090 infoPtr->rcList.top += 2;
11092 TRACE(" rcList=%s\n", wine_dbgstr_rect(&infoPtr->rcList));
11095 /***
11096 * DESCRIPTION:
11097 * Processes WM_STYLECHANGED messages.
11099 * PARAMETER(S):
11100 * [I] infoPtr : valid pointer to the listview structure
11101 * [I] wStyleType : window style type (normal or extended)
11102 * [I] lpss : window style information
11104 * RETURN:
11105 * Zero
11107 static INT LISTVIEW_StyleChanged(LISTVIEW_INFO *infoPtr, WPARAM wStyleType,
11108 const STYLESTRUCT *lpss)
11110 UINT uNewView, uOldView;
11111 UINT style;
11113 TRACE("(styletype=%lx, styleOld=0x%08x, styleNew=0x%08x)\n",
11114 wStyleType, lpss->styleOld, lpss->styleNew);
11116 if (wStyleType != GWL_STYLE || lpss->styleNew == infoPtr->dwStyle) return 0;
11118 infoPtr->dwStyle = lpss->styleNew;
11120 if (((lpss->styleOld & WS_HSCROLL) != 0)&&
11121 ((lpss->styleNew & WS_HSCROLL) == 0))
11122 ShowScrollBar(infoPtr->hwndSelf, SB_HORZ, FALSE);
11124 if (((lpss->styleOld & WS_VSCROLL) != 0)&&
11125 ((lpss->styleNew & WS_VSCROLL) == 0))
11126 ShowScrollBar(infoPtr->hwndSelf, SB_VERT, FALSE);
11128 uNewView = lpss->styleNew & LVS_TYPEMASK;
11129 uOldView = lpss->styleOld & LVS_TYPEMASK;
11131 if (uNewView != uOldView)
11133 HIMAGELIST himl;
11135 /* LVM_SETVIEW doesn't change window style bits within LVS_TYPEMASK,
11136 changing style updates current view only when view bits change. */
11137 map_style_view(infoPtr);
11138 SendMessageW(infoPtr->hwndEdit, WM_KILLFOCUS, 0, 0);
11139 ShowWindow(infoPtr->hwndHeader, SW_HIDE);
11141 ShowScrollBar(infoPtr->hwndSelf, SB_BOTH, FALSE);
11142 SetRectEmpty(&infoPtr->rcFocus);
11144 himl = (uNewView == LVS_ICON ? infoPtr->himlNormal : infoPtr->himlSmall);
11145 set_icon_size(&infoPtr->iconSize, himl, uNewView != LVS_ICON);
11147 if (uNewView == LVS_REPORT)
11149 HDLAYOUT hl;
11150 WINDOWPOS wp;
11152 LISTVIEW_CreateHeader( infoPtr );
11154 hl.prc = &infoPtr->rcList;
11155 hl.pwpos = &wp;
11156 SendMessageW( infoPtr->hwndHeader, HDM_LAYOUT, 0, (LPARAM)&hl );
11157 SetWindowPos(infoPtr->hwndHeader, infoPtr->hwndSelf, wp.x, wp.y, wp.cx, wp.cy,
11158 wp.flags | ((infoPtr->dwStyle & LVS_NOCOLUMNHEADER)
11159 ? SWP_HIDEWINDOW : SWP_SHOWWINDOW));
11162 LISTVIEW_UpdateItemSize(infoPtr);
11165 if (uNewView == LVS_REPORT || infoPtr->dwLvExStyle & LVS_EX_HEADERINALLVIEWS)
11167 if ((lpss->styleOld ^ lpss->styleNew) & LVS_NOCOLUMNHEADER)
11169 if (lpss->styleNew & LVS_NOCOLUMNHEADER)
11171 /* Turn off the header control */
11172 style = GetWindowLongW(infoPtr->hwndHeader, GWL_STYLE);
11173 TRACE("Hide header control, was 0x%08x\n", style);
11174 SetWindowLongW(infoPtr->hwndHeader, GWL_STYLE, style | HDS_HIDDEN);
11175 } else {
11176 /* Turn on the header control */
11177 if ((style = GetWindowLongW(infoPtr->hwndHeader, GWL_STYLE)) & HDS_HIDDEN)
11179 TRACE("Show header control, was 0x%08x\n", style);
11180 SetWindowLongW(infoPtr->hwndHeader, GWL_STYLE, (style & ~HDS_HIDDEN) | WS_VISIBLE);
11186 if ( (uNewView == LVS_ICON || uNewView == LVS_SMALLICON) &&
11187 (uNewView != uOldView || ((lpss->styleNew ^ lpss->styleOld) & LVS_ALIGNMASK)) )
11188 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
11190 /* update the size of the client area */
11191 LISTVIEW_UpdateSize(infoPtr);
11193 /* add scrollbars if needed */
11194 LISTVIEW_UpdateScroll(infoPtr);
11196 /* invalidate client area + erase background */
11197 LISTVIEW_InvalidateList(infoPtr);
11199 return 0;
11202 /***
11203 * DESCRIPTION:
11204 * Processes WM_STYLECHANGING messages.
11206 * PARAMETER(S):
11207 * [I] wStyleType : window style type (normal or extended)
11208 * [I0] lpss : window style information
11210 * RETURN:
11211 * Zero
11213 static INT LISTVIEW_StyleChanging(WPARAM wStyleType,
11214 STYLESTRUCT *lpss)
11216 TRACE("(styletype=%lx, styleOld=0x%08x, styleNew=0x%08x)\n",
11217 wStyleType, lpss->styleOld, lpss->styleNew);
11219 /* don't forward LVS_OWNERDATA only if not already set to */
11220 if ((lpss->styleNew ^ lpss->styleOld) & LVS_OWNERDATA)
11222 if (lpss->styleOld & LVS_OWNERDATA)
11223 lpss->styleNew |= LVS_OWNERDATA;
11224 else
11225 lpss->styleNew &= ~LVS_OWNERDATA;
11228 return 0;
11231 /***
11232 * DESCRIPTION:
11233 * Processes WM_SHOWWINDOW messages.
11235 * PARAMETER(S):
11236 * [I] infoPtr : valid pointer to the listview structure
11237 * [I] bShown : window is being shown (FALSE when hidden)
11238 * [I] iStatus : window show status
11240 * RETURN:
11241 * Zero
11243 static LRESULT LISTVIEW_ShowWindow(LISTVIEW_INFO *infoPtr, WPARAM bShown, LPARAM iStatus)
11245 /* header delayed creation */
11246 if ((infoPtr->uView == LV_VIEW_DETAILS) && bShown)
11248 LISTVIEW_CreateHeader(infoPtr);
11250 if (!(LVS_NOCOLUMNHEADER & infoPtr->dwStyle))
11251 ShowWindow(infoPtr->hwndHeader, SW_SHOWNORMAL);
11254 return DefWindowProcW(infoPtr->hwndSelf, WM_SHOWWINDOW, bShown, iStatus);
11257 /***
11258 * DESCRIPTION:
11259 * Processes CCM_GETVERSION messages.
11261 * PARAMETER(S):
11262 * [I] infoPtr : valid pointer to the listview structure
11264 * RETURN:
11265 * Current version
11267 static inline LRESULT LISTVIEW_GetVersion(const LISTVIEW_INFO *infoPtr)
11269 return infoPtr->iVersion;
11272 /***
11273 * DESCRIPTION:
11274 * Processes CCM_SETVERSION messages.
11276 * PARAMETER(S):
11277 * [I] infoPtr : valid pointer to the listview structure
11278 * [I] iVersion : version to be set
11280 * RETURN:
11281 * -1 when requested version is greater than DLL version;
11282 * previous version otherwise
11284 static LRESULT LISTVIEW_SetVersion(LISTVIEW_INFO *infoPtr, DWORD iVersion)
11286 INT iOldVersion = infoPtr->iVersion;
11288 if (iVersion > COMCTL32_VERSION)
11289 return -1;
11291 infoPtr->iVersion = iVersion;
11293 TRACE("new version %d\n", iVersion);
11295 return iOldVersion;
11298 /***
11299 * DESCRIPTION:
11300 * Window procedure of the listview control.
11303 static LRESULT WINAPI
11304 LISTVIEW_WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
11306 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)GetWindowLongPtrW(hwnd, 0);
11308 TRACE("(hwnd=%p uMsg=%x wParam=%lx lParam=%lx)\n", hwnd, uMsg, wParam, lParam);
11310 if (!infoPtr && (uMsg != WM_NCCREATE))
11311 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
11313 switch (uMsg)
11315 case LVM_APPROXIMATEVIEWRECT:
11316 return LISTVIEW_ApproximateViewRect(infoPtr, (INT)wParam,
11317 LOWORD(lParam), HIWORD(lParam));
11318 case LVM_ARRANGE:
11319 return LISTVIEW_Arrange(infoPtr, (INT)wParam);
11321 case LVM_CANCELEDITLABEL:
11322 return LISTVIEW_CancelEditLabel(infoPtr);
11324 case LVM_CREATEDRAGIMAGE:
11325 return (LRESULT)LISTVIEW_CreateDragImage(infoPtr, (INT)wParam, (LPPOINT)lParam);
11327 case LVM_DELETEALLITEMS:
11328 return LISTVIEW_DeleteAllItems(infoPtr, FALSE);
11330 case LVM_DELETECOLUMN:
11331 return LISTVIEW_DeleteColumn(infoPtr, (INT)wParam);
11333 case LVM_DELETEITEM:
11334 return LISTVIEW_DeleteItem(infoPtr, (INT)wParam);
11336 case LVM_EDITLABELA:
11337 case LVM_EDITLABELW:
11338 return (LRESULT)LISTVIEW_EditLabelT(infoPtr, (INT)wParam,
11339 uMsg == LVM_EDITLABELW);
11340 /* case LVM_ENABLEGROUPVIEW: */
11342 case LVM_ENSUREVISIBLE:
11343 return LISTVIEW_EnsureVisible(infoPtr, (INT)wParam, (BOOL)lParam);
11345 case LVM_FINDITEMW:
11346 return LISTVIEW_FindItemW(infoPtr, (INT)wParam, (LPLVFINDINFOW)lParam);
11348 case LVM_FINDITEMA:
11349 return LISTVIEW_FindItemA(infoPtr, (INT)wParam, (LPLVFINDINFOA)lParam);
11351 case LVM_GETBKCOLOR:
11352 return infoPtr->clrBk;
11354 /* case LVM_GETBKIMAGE: */
11356 case LVM_GETCALLBACKMASK:
11357 return infoPtr->uCallbackMask;
11359 case LVM_GETCOLUMNA:
11360 case LVM_GETCOLUMNW:
11361 return LISTVIEW_GetColumnT(infoPtr, (INT)wParam, (LPLVCOLUMNW)lParam,
11362 uMsg == LVM_GETCOLUMNW);
11364 case LVM_GETCOLUMNORDERARRAY:
11365 return LISTVIEW_GetColumnOrderArray(infoPtr, (INT)wParam, (LPINT)lParam);
11367 case LVM_GETCOLUMNWIDTH:
11368 return LISTVIEW_GetColumnWidth(infoPtr, (INT)wParam);
11370 case LVM_GETCOUNTPERPAGE:
11371 return LISTVIEW_GetCountPerPage(infoPtr);
11373 case LVM_GETEDITCONTROL:
11374 return (LRESULT)infoPtr->hwndEdit;
11376 case LVM_GETEXTENDEDLISTVIEWSTYLE:
11377 return infoPtr->dwLvExStyle;
11379 /* case LVM_GETGROUPINFO: */
11381 /* case LVM_GETGROUPMETRICS: */
11383 case LVM_GETHEADER:
11384 return (LRESULT)infoPtr->hwndHeader;
11386 case LVM_GETHOTCURSOR:
11387 return (LRESULT)infoPtr->hHotCursor;
11389 case LVM_GETHOTITEM:
11390 return infoPtr->nHotItem;
11392 case LVM_GETHOVERTIME:
11393 return infoPtr->dwHoverTime;
11395 case LVM_GETIMAGELIST:
11396 return (LRESULT)LISTVIEW_GetImageList(infoPtr, (INT)wParam);
11398 /* case LVM_GETINSERTMARK: */
11400 /* case LVM_GETINSERTMARKCOLOR: */
11402 /* case LVM_GETINSERTMARKRECT: */
11404 case LVM_GETISEARCHSTRINGA:
11405 case LVM_GETISEARCHSTRINGW:
11406 FIXME("LVM_GETISEARCHSTRING: unimplemented\n");
11407 return FALSE;
11409 case LVM_GETITEMA:
11410 case LVM_GETITEMW:
11411 return LISTVIEW_GetItemExtT(infoPtr, (LPLVITEMW)lParam, uMsg == LVM_GETITEMW);
11413 case LVM_GETITEMCOUNT:
11414 return infoPtr->nItemCount;
11416 case LVM_GETITEMPOSITION:
11417 return LISTVIEW_GetItemPosition(infoPtr, (INT)wParam, (LPPOINT)lParam);
11419 case LVM_GETITEMRECT:
11420 return LISTVIEW_GetItemRect(infoPtr, (INT)wParam, (LPRECT)lParam);
11422 case LVM_GETITEMSPACING:
11423 return LISTVIEW_GetItemSpacing(infoPtr, (BOOL)wParam);
11425 case LVM_GETITEMSTATE:
11426 return LISTVIEW_GetItemState(infoPtr, (INT)wParam, (UINT)lParam);
11428 case LVM_GETITEMTEXTA:
11429 case LVM_GETITEMTEXTW:
11430 return LISTVIEW_GetItemTextT(infoPtr, (INT)wParam, (LPLVITEMW)lParam,
11431 uMsg == LVM_GETITEMTEXTW);
11433 case LVM_GETNEXTITEM:
11434 return LISTVIEW_GetNextItem(infoPtr, (INT)wParam, LOWORD(lParam));
11436 case LVM_GETNUMBEROFWORKAREAS:
11437 FIXME("LVM_GETNUMBEROFWORKAREAS: unimplemented\n");
11438 return 1;
11440 case LVM_GETORIGIN:
11441 if (!lParam) return FALSE;
11442 if (infoPtr->uView == LV_VIEW_DETAILS ||
11443 infoPtr->uView == LV_VIEW_LIST) return FALSE;
11444 LISTVIEW_GetOrigin(infoPtr, (LPPOINT)lParam);
11445 return TRUE;
11447 /* case LVM_GETOUTLINECOLOR: */
11449 case LVM_GETSELECTEDCOLUMN:
11450 return infoPtr->selected_column;
11452 case LVM_GETSELECTEDCOUNT:
11453 return LISTVIEW_GetSelectedCount(infoPtr);
11455 case LVM_GETSELECTIONMARK:
11456 return infoPtr->nSelectionMark;
11458 case LVM_GETSTRINGWIDTHA:
11459 case LVM_GETSTRINGWIDTHW:
11460 return LISTVIEW_GetStringWidthT(infoPtr, (LPCWSTR)lParam,
11461 uMsg == LVM_GETSTRINGWIDTHW);
11463 case LVM_GETSUBITEMRECT:
11464 return LISTVIEW_GetSubItemRect(infoPtr, (UINT)wParam, (LPRECT)lParam);
11466 case LVM_GETTEXTBKCOLOR:
11467 return infoPtr->clrTextBk;
11469 case LVM_GETTEXTCOLOR:
11470 return infoPtr->clrText;
11472 /* case LVM_GETTILEINFO: */
11474 /* case LVM_GETTILEVIEWINFO: */
11476 case LVM_GETTOOLTIPS:
11477 if( !infoPtr->hwndToolTip )
11478 infoPtr->hwndToolTip = COMCTL32_CreateToolTip( hwnd );
11479 return (LRESULT)infoPtr->hwndToolTip;
11481 case LVM_GETTOPINDEX:
11482 return LISTVIEW_GetTopIndex(infoPtr);
11484 case LVM_GETUNICODEFORMAT:
11485 return (infoPtr->notifyFormat == NFR_UNICODE);
11487 case LVM_GETVIEW:
11488 return infoPtr->uView;
11490 case LVM_GETVIEWRECT:
11491 return LISTVIEW_GetViewRect(infoPtr, (LPRECT)lParam);
11493 case LVM_GETWORKAREAS:
11494 FIXME("LVM_GETWORKAREAS: unimplemented\n");
11495 return FALSE;
11497 /* case LVM_HASGROUP: */
11499 case LVM_HITTEST:
11500 return LISTVIEW_HitTest(infoPtr, (LPLVHITTESTINFO)lParam, FALSE, TRUE);
11502 case LVM_INSERTCOLUMNA:
11503 case LVM_INSERTCOLUMNW:
11504 return LISTVIEW_InsertColumnT(infoPtr, (INT)wParam, (LPLVCOLUMNW)lParam,
11505 uMsg == LVM_INSERTCOLUMNW);
11507 /* case LVM_INSERTGROUP: */
11509 /* case LVM_INSERTGROUPSORTED: */
11511 case LVM_INSERTITEMA:
11512 case LVM_INSERTITEMW:
11513 return LISTVIEW_InsertItemT(infoPtr, (LPLVITEMW)lParam, uMsg == LVM_INSERTITEMW);
11515 /* case LVM_INSERTMARKHITTEST: */
11517 /* case LVM_ISGROUPVIEWENABLED: */
11519 case LVM_ISITEMVISIBLE:
11520 return LISTVIEW_IsItemVisible(infoPtr, (INT)wParam);
11522 case LVM_MAPIDTOINDEX:
11523 return LISTVIEW_MapIdToIndex(infoPtr, (UINT)wParam);
11525 case LVM_MAPINDEXTOID:
11526 return LISTVIEW_MapIndexToId(infoPtr, (INT)wParam);
11528 /* case LVM_MOVEGROUP: */
11530 /* case LVM_MOVEITEMTOGROUP: */
11532 case LVM_REDRAWITEMS:
11533 return LISTVIEW_RedrawItems(infoPtr, (INT)wParam, (INT)lParam);
11535 /* case LVM_REMOVEALLGROUPS: */
11537 /* case LVM_REMOVEGROUP: */
11539 case LVM_SCROLL:
11540 return LISTVIEW_Scroll(infoPtr, (INT)wParam, (INT)lParam);
11542 case LVM_SETBKCOLOR:
11543 return LISTVIEW_SetBkColor(infoPtr, (COLORREF)lParam);
11545 /* case LVM_SETBKIMAGE: */
11547 case LVM_SETCALLBACKMASK:
11548 infoPtr->uCallbackMask = (UINT)wParam;
11549 return TRUE;
11551 case LVM_SETCOLUMNA:
11552 case LVM_SETCOLUMNW:
11553 return LISTVIEW_SetColumnT(infoPtr, (INT)wParam, (LPLVCOLUMNW)lParam,
11554 uMsg == LVM_SETCOLUMNW);
11556 case LVM_SETCOLUMNORDERARRAY:
11557 return LISTVIEW_SetColumnOrderArray(infoPtr, (INT)wParam, (LPINT)lParam);
11559 case LVM_SETCOLUMNWIDTH:
11560 return LISTVIEW_SetColumnWidth(infoPtr, (INT)wParam, (short)LOWORD(lParam));
11562 case LVM_SETEXTENDEDLISTVIEWSTYLE:
11563 return LISTVIEW_SetExtendedListViewStyle(infoPtr, (DWORD)wParam, (DWORD)lParam);
11565 /* case LVM_SETGROUPINFO: */
11567 /* case LVM_SETGROUPMETRICS: */
11569 case LVM_SETHOTCURSOR:
11570 return (LRESULT)LISTVIEW_SetHotCursor(infoPtr, (HCURSOR)lParam);
11572 case LVM_SETHOTITEM:
11573 return LISTVIEW_SetHotItem(infoPtr, (INT)wParam);
11575 case LVM_SETHOVERTIME:
11576 return LISTVIEW_SetHoverTime(infoPtr, (DWORD)lParam);
11578 case LVM_SETICONSPACING:
11579 if(lParam == -1)
11580 return LISTVIEW_SetIconSpacing(infoPtr, -1, -1);
11581 return LISTVIEW_SetIconSpacing(infoPtr, LOWORD(lParam), HIWORD(lParam));
11583 case LVM_SETIMAGELIST:
11584 return (LRESULT)LISTVIEW_SetImageList(infoPtr, (INT)wParam, (HIMAGELIST)lParam);
11586 /* case LVM_SETINFOTIP: */
11588 /* case LVM_SETINSERTMARK: */
11590 /* case LVM_SETINSERTMARKCOLOR: */
11592 case LVM_SETITEMA:
11593 case LVM_SETITEMW:
11595 if (infoPtr->dwStyle & LVS_OWNERDATA) return FALSE;
11596 return LISTVIEW_SetItemT(infoPtr, (LPLVITEMW)lParam, (uMsg == LVM_SETITEMW));
11599 case LVM_SETITEMCOUNT:
11600 return LISTVIEW_SetItemCount(infoPtr, (INT)wParam, (DWORD)lParam);
11602 case LVM_SETITEMPOSITION:
11604 POINT pt;
11605 pt.x = (short)LOWORD(lParam);
11606 pt.y = (short)HIWORD(lParam);
11607 return LISTVIEW_SetItemPosition(infoPtr, (INT)wParam, &pt);
11610 case LVM_SETITEMPOSITION32:
11611 return LISTVIEW_SetItemPosition(infoPtr, (INT)wParam, (POINT*)lParam);
11613 case LVM_SETITEMSTATE:
11614 return LISTVIEW_SetItemState(infoPtr, (INT)wParam, (LPLVITEMW)lParam);
11616 case LVM_SETITEMTEXTA:
11617 case LVM_SETITEMTEXTW:
11618 return LISTVIEW_SetItemTextT(infoPtr, (INT)wParam, (LPLVITEMW)lParam,
11619 uMsg == LVM_SETITEMTEXTW);
11621 /* case LVM_SETOUTLINECOLOR: */
11623 case LVM_SETSELECTEDCOLUMN:
11624 infoPtr->selected_column = (INT)wParam;
11625 return TRUE;
11627 case LVM_SETSELECTIONMARK:
11628 return LISTVIEW_SetSelectionMark(infoPtr, (INT)lParam);
11630 case LVM_SETTEXTBKCOLOR:
11631 return LISTVIEW_SetTextBkColor(infoPtr, (COLORREF)lParam);
11633 case LVM_SETTEXTCOLOR:
11634 return LISTVIEW_SetTextColor(infoPtr, (COLORREF)lParam);
11636 /* case LVM_SETTILEINFO: */
11638 /* case LVM_SETTILEVIEWINFO: */
11640 /* case LVM_SETTILEWIDTH: */
11642 case LVM_SETTOOLTIPS:
11643 return (LRESULT)LISTVIEW_SetToolTips(infoPtr, (HWND)lParam);
11645 case LVM_SETUNICODEFORMAT:
11646 return LISTVIEW_SetUnicodeFormat(infoPtr, wParam);
11648 case LVM_SETVIEW:
11649 return LISTVIEW_SetView(infoPtr, wParam);
11651 /* case LVM_SETWORKAREAS: */
11653 /* case LVM_SORTGROUPS: */
11655 case LVM_SORTITEMS:
11656 case LVM_SORTITEMSEX:
11657 return LISTVIEW_SortItems(infoPtr, (PFNLVCOMPARE)lParam, wParam,
11658 uMsg == LVM_SORTITEMSEX);
11659 case LVM_SUBITEMHITTEST:
11660 return LISTVIEW_HitTest(infoPtr, (LPLVHITTESTINFO)lParam, TRUE, FALSE);
11662 case LVM_UPDATE:
11663 return LISTVIEW_Update(infoPtr, (INT)wParam);
11665 case CCM_GETVERSION:
11666 return LISTVIEW_GetVersion(infoPtr);
11668 case CCM_SETVERSION:
11669 return LISTVIEW_SetVersion(infoPtr, wParam);
11671 case WM_CHAR:
11672 return LISTVIEW_ProcessLetterKeys( infoPtr, wParam, lParam );
11674 case WM_COMMAND:
11675 return LISTVIEW_Command(infoPtr, wParam, lParam);
11677 case WM_NCCREATE:
11678 return LISTVIEW_NCCreate(hwnd, wParam, (LPCREATESTRUCTW)lParam);
11680 case WM_CREATE:
11681 return LISTVIEW_Create(hwnd, (LPCREATESTRUCTW)lParam);
11683 case WM_DESTROY:
11684 return LISTVIEW_Destroy(infoPtr);
11686 case WM_ENABLE:
11687 return LISTVIEW_Enable(infoPtr);
11689 case WM_ERASEBKGND:
11690 return LISTVIEW_EraseBkgnd(infoPtr, (HDC)wParam);
11692 case WM_GETDLGCODE:
11693 return DLGC_WANTCHARS | DLGC_WANTARROWS;
11695 case WM_GETFONT:
11696 return (LRESULT)infoPtr->hFont;
11698 case WM_HSCROLL:
11699 return LISTVIEW_HScroll(infoPtr, (INT)LOWORD(wParam), 0);
11701 case WM_KEYDOWN:
11702 return LISTVIEW_KeyDown(infoPtr, (INT)wParam, (LONG)lParam);
11704 case WM_KILLFOCUS:
11705 return LISTVIEW_KillFocus(infoPtr);
11707 case WM_LBUTTONDBLCLK:
11708 return LISTVIEW_LButtonDblClk(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11710 case WM_LBUTTONDOWN:
11711 return LISTVIEW_LButtonDown(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11713 case WM_LBUTTONUP:
11714 return LISTVIEW_LButtonUp(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11716 case WM_MOUSEMOVE:
11717 return LISTVIEW_MouseMove (infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11719 case WM_MOUSEHOVER:
11720 return LISTVIEW_MouseHover(infoPtr, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11722 case WM_NCDESTROY:
11723 return LISTVIEW_NCDestroy(infoPtr);
11725 case WM_NCPAINT:
11726 return LISTVIEW_NCPaint(infoPtr, (HRGN)wParam);
11728 case WM_NOTIFY:
11729 return LISTVIEW_Notify(infoPtr, (LPNMHDR)lParam);
11731 case WM_NOTIFYFORMAT:
11732 return LISTVIEW_NotifyFormat(infoPtr, (HWND)wParam, (INT)lParam);
11734 case WM_PRINTCLIENT:
11735 return LISTVIEW_PrintClient(infoPtr, (HDC)wParam, (DWORD)lParam);
11737 case WM_PAINT:
11738 return LISTVIEW_WMPaint(infoPtr, (HDC)wParam);
11740 case WM_RBUTTONDBLCLK:
11741 return LISTVIEW_RButtonDblClk(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11743 case WM_RBUTTONDOWN:
11744 return LISTVIEW_RButtonDown(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11746 case WM_SETCURSOR:
11747 return LISTVIEW_SetCursor(infoPtr, wParam, lParam);
11749 case WM_SETFOCUS:
11750 return LISTVIEW_SetFocus(infoPtr, (HWND)wParam);
11752 case WM_SETFONT:
11753 return LISTVIEW_SetFont(infoPtr, (HFONT)wParam, (WORD)lParam);
11755 case WM_SETREDRAW:
11756 return LISTVIEW_SetRedraw(infoPtr, (BOOL)wParam);
11758 case WM_SHOWWINDOW:
11759 return LISTVIEW_ShowWindow(infoPtr, wParam, lParam);
11761 case WM_STYLECHANGED:
11762 return LISTVIEW_StyleChanged(infoPtr, wParam, (LPSTYLESTRUCT)lParam);
11764 case WM_STYLECHANGING:
11765 return LISTVIEW_StyleChanging(wParam, (LPSTYLESTRUCT)lParam);
11767 case WM_SYSCOLORCHANGE:
11768 COMCTL32_RefreshSysColors();
11769 return 0;
11771 /* case WM_TIMER: */
11772 case WM_THEMECHANGED:
11773 return LISTVIEW_ThemeChanged(infoPtr);
11775 case WM_VSCROLL:
11776 return LISTVIEW_VScroll(infoPtr, (INT)LOWORD(wParam), 0);
11778 case WM_MOUSEWHEEL:
11779 if (wParam & (MK_SHIFT | MK_CONTROL))
11780 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
11781 return LISTVIEW_MouseWheel(infoPtr, (short int)HIWORD(wParam));
11783 case WM_WINDOWPOSCHANGED:
11784 if (!(((WINDOWPOS *)lParam)->flags & SWP_NOSIZE))
11786 SetWindowPos(infoPtr->hwndSelf, 0, 0, 0, 0, 0, SWP_FRAMECHANGED | SWP_NOACTIVATE |
11787 SWP_NOZORDER | SWP_NOMOVE | SWP_NOSIZE);
11789 if ((infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && (infoPtr->uView == LV_VIEW_DETAILS))
11791 if (notify_measureitem(infoPtr)) LISTVIEW_InvalidateList(infoPtr);
11793 LISTVIEW_Size(infoPtr, ((WINDOWPOS *)lParam)->cx, ((WINDOWPOS *)lParam)->cy);
11795 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
11797 /* case WM_WININICHANGE: */
11799 default:
11800 if ((uMsg >= WM_USER) && (uMsg < WM_APP) && !COMCTL32_IsReflectedMessage(uMsg))
11801 ERR("unknown msg %04x wp=%08lx lp=%08lx\n", uMsg, wParam, lParam);
11803 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
11808 /***
11809 * DESCRIPTION:
11810 * Registers the window class.
11812 * PARAMETER(S):
11813 * None
11815 * RETURN:
11816 * None
11818 void LISTVIEW_Register(void)
11820 WNDCLASSW wndClass;
11822 ZeroMemory(&wndClass, sizeof(WNDCLASSW));
11823 wndClass.style = CS_GLOBALCLASS | CS_DBLCLKS;
11824 wndClass.lpfnWndProc = LISTVIEW_WindowProc;
11825 wndClass.cbClsExtra = 0;
11826 wndClass.cbWndExtra = sizeof(LISTVIEW_INFO *);
11827 wndClass.hCursor = LoadCursorW(0, (LPWSTR)IDC_ARROW);
11828 wndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
11829 wndClass.lpszClassName = WC_LISTVIEWW;
11830 RegisterClassW(&wndClass);
11833 /***
11834 * DESCRIPTION:
11835 * Unregisters the window class.
11837 * PARAMETER(S):
11838 * None
11840 * RETURN:
11841 * None
11843 void LISTVIEW_Unregister(void)
11845 UnregisterClassW(WC_LISTVIEWW, NULL);
11848 /***
11849 * DESCRIPTION:
11850 * Handle any WM_COMMAND messages
11852 * PARAMETER(S):
11853 * [I] infoPtr : valid pointer to the listview structure
11854 * [I] wParam : the first message parameter
11855 * [I] lParam : the second message parameter
11857 * RETURN:
11858 * Zero.
11860 static LRESULT LISTVIEW_Command(LISTVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
11863 TRACE("(%p %x %x %lx)\n", infoPtr, HIWORD(wParam), LOWORD(wParam), lParam);
11865 if (!infoPtr->hwndEdit) return 0;
11867 switch (HIWORD(wParam))
11869 case EN_UPDATE:
11872 * Adjust the edit window size
11874 WCHAR buffer[1024];
11875 HDC hdc = GetDC(infoPtr->hwndEdit);
11876 HFONT hFont, hOldFont = 0;
11877 RECT rect;
11878 SIZE sz;
11880 if (!infoPtr->hwndEdit || !hdc) return 0;
11881 GetWindowTextW(infoPtr->hwndEdit, buffer, ARRAY_SIZE(buffer));
11882 GetWindowRect(infoPtr->hwndEdit, &rect);
11884 /* Select font to get the right dimension of the string */
11885 hFont = (HFONT)SendMessageW(infoPtr->hwndEdit, WM_GETFONT, 0, 0);
11886 if (hFont)
11888 hOldFont = SelectObject(hdc, hFont);
11891 if (GetTextExtentPoint32W(hdc, buffer, lstrlenW(buffer), &sz))
11893 TEXTMETRICW textMetric;
11895 /* Add Extra spacing for the next character */
11896 GetTextMetricsW(hdc, &textMetric);
11897 sz.cx += (textMetric.tmMaxCharWidth * 2);
11899 SetWindowPos(infoPtr->hwndEdit, NULL, 0, 0, sz.cx,
11900 rect.bottom - rect.top, SWP_DRAWFRAME | SWP_NOMOVE | SWP_NOZORDER);
11902 if (hFont)
11903 SelectObject(hdc, hOldFont);
11905 ReleaseDC(infoPtr->hwndEdit, hdc);
11907 break;
11909 case EN_KILLFOCUS:
11911 if (infoPtr->notify_mask & NOTIFY_MASK_END_LABEL_EDIT)
11912 LISTVIEW_CancelEditLabel(infoPtr);
11913 break;
11916 default:
11917 return SendMessageW (infoPtr->hwndNotify, WM_COMMAND, wParam, lParam);
11920 return 0;