windows.media: Partially implement IClosedCaptionPropertiesStatics_get_FontSize.
[wine.git] / dlls / comctl32 / listview.c
blobd060fd5c977acaca6bbf0c8eecd45da03b18be46
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
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 "vsstyle.h"
143 #include "shlwapi.h"
145 #include "wine/debug.h"
147 WINE_DEFAULT_DEBUG_CHANNEL(listview);
149 typedef struct tagCOLUMN_INFO
151 RECT rcHeader; /* tracks the header's rectangle */
152 INT fmt; /* same as LVCOLUMN.fmt */
153 INT cxMin;
154 } COLUMN_INFO;
156 typedef struct tagITEMHDR
158 LPWSTR pszText;
159 INT iImage;
160 } ITEMHDR, *LPITEMHDR;
162 typedef struct tagSUBITEM_INFO
164 ITEMHDR hdr;
165 INT iSubItem;
166 } SUBITEM_INFO;
168 typedef struct tagITEM_ID ITEM_ID;
170 typedef struct tagITEM_INFO
172 ITEMHDR hdr;
173 UINT state;
174 LPARAM lParam;
175 INT iIndent;
176 ITEM_ID *id;
177 } ITEM_INFO;
179 struct tagITEM_ID
181 UINT id; /* item id */
182 HDPA item; /* link to item data */
185 typedef struct tagRANGE
187 INT lower;
188 INT upper;
189 } RANGE;
191 typedef struct tagRANGES
193 HDPA hdpa;
194 } *RANGES;
196 typedef struct tagITERATOR
198 INT nItem;
199 INT nSpecial;
200 RANGE range;
201 RANGES ranges;
202 INT index;
203 } ITERATOR;
205 typedef struct tagDELAYED_ITEM_EDIT
207 BOOL fEnabled;
208 INT iItem;
209 } DELAYED_ITEM_EDIT;
211 enum notification_mask
213 NOTIFY_MASK_ITEM_CHANGE = 0x1,
214 NOTIFY_MASK_END_LABEL_EDIT = 0x2,
215 NOTIFY_MASK_UNMASK_ALL = 0xffffffff
218 typedef struct tagLISTVIEW_INFO
220 /* control window */
221 HWND hwndSelf;
222 RECT rcList; /* This rectangle is really the window
223 * client rectangle possibly reduced by the
224 * horizontal scroll bar and/or header - see
225 * LISTVIEW_UpdateSize. This rectangle offset
226 * by the LISTVIEW_GetOrigin value is in
227 * client coordinates */
229 /* notification window */
230 SHORT notifyFormat;
231 HWND hwndNotify;
232 DWORD notify_mask;
233 UINT uCallbackMask;
235 /* tooltips */
236 HWND hwndToolTip;
238 /* items */
239 INT nItemCount; /* the number of items in the list */
240 HDPA hdpaItems; /* array ITEM_INFO pointers */
241 HDPA hdpaItemIds; /* array of ITEM_ID pointers */
242 HDPA hdpaPosX; /* maintains the (X, Y) coordinates of the */
243 HDPA hdpaPosY; /* items in LVS_ICON, and LVS_SMALLICON modes */
244 RANGES selectionRanges;
245 INT nSelectionMark; /* item to start next multiselection from */
246 INT nHotItem;
248 /* columns */
249 HDPA hdpaColumns; /* array of COLUMN_INFO pointers */
250 BOOL colRectsDirty; /* trigger column rectangles requery from header */
251 INT selected_column; /* index for LVM_SETSELECTEDCOLUMN/LVM_GETSELECTEDCOLUMN */
253 /* item metrics */
254 BOOL bNoItemMetrics; /* flags if item metrics are not yet computed */
255 INT nItemHeight;
256 INT nItemWidth;
258 /* style */
259 DWORD dwStyle; /* the cached window GWL_STYLE */
260 DWORD dwLvExStyle; /* extended listview style */
261 DWORD uView; /* current view available through LVM_[G,S]ETVIEW */
263 /* edit item */
264 HWND hwndEdit;
265 WNDPROC EditWndProc;
266 INT nEditLabelItem;
267 DELAYED_ITEM_EDIT itemEdit; /* Pointer to this structure will be the timer ID */
269 /* icons */
270 HIMAGELIST himlNormal;
271 HIMAGELIST himlSmall;
272 HIMAGELIST himlState;
273 SIZE iconSize;
274 BOOL autoSpacing;
275 SIZE iconSpacing;
276 SIZE iconStateSize;
277 POINT currIconPos; /* this is the position next icon will be placed */
279 /* header */
280 HWND hwndHeader;
281 INT xTrackLine; /* The x coefficient of the track line or -1 if none */
283 /* marquee selection */
284 BOOL bMarqueeSelect; /* marquee selection/highlight underway */
285 BOOL bScrolling;
286 RECT marqueeRect; /* absolute coordinates of marquee selection */
287 RECT marqueeDrawRect; /* relative coordinates for drawing marquee */
288 POINT marqueeOrigin; /* absolute coordinates of marquee click origin */
290 /* focus drawing */
291 BOOL bFocus; /* control has focus */
292 INT nFocusedItem;
293 RECT rcFocus; /* focus bounds */
295 /* colors */
296 HBRUSH hBkBrush;
297 COLORREF clrBk;
298 COLORREF clrText;
299 COLORREF clrTextBk;
300 HBITMAP hBkBitmap;
302 /* font */
303 HFONT hDefaultFont;
304 HFONT hFont;
305 INT ntmHeight; /* Some cached metrics of the font used */
306 INT ntmMaxCharWidth; /* by the listview to draw items */
307 INT nEllipsisWidth;
309 /* mouse operation */
310 BOOL bLButtonDown;
311 BOOL bDragging;
312 POINT ptClickPos; /* point where the user clicked */
313 INT nLButtonDownItem; /* tracks item to reset multiselection on WM_LBUTTONUP */
314 DWORD dwHoverTime;
315 HCURSOR hHotCursor;
316 INT cWheelRemainder;
318 /* keyboard operation */
319 DWORD lastKeyPressTimestamp;
320 WPARAM charCode;
321 INT nSearchParamLength;
322 WCHAR szSearchParam[ MAX_PATH ];
324 /* painting */
325 BOOL bIsDrawing; /* Drawing in progress */
326 INT nMeasureItemHeight; /* WM_MEASUREITEM result */
327 BOOL redraw; /* WM_SETREDRAW switch */
329 /* misc */
330 DWORD iVersion; /* CCM_[G,S]ETVERSION */
331 } LISTVIEW_INFO;
334 * constants
336 /* How many we debug buffer to allocate */
337 #define DEBUG_BUFFERS 20
338 /* The size of a single debug buffer */
339 #define DEBUG_BUFFER_SIZE 256
341 /* Internal interface to LISTVIEW_HScroll and LISTVIEW_VScroll */
342 #define SB_INTERNAL -1
344 /* maximum size of a label */
345 #define DISP_TEXT_SIZE 260
347 /* padding for items in list and small icon display modes */
348 #define WIDTH_PADDING 12
350 /* padding for items in list, report and small icon display modes */
351 #define HEIGHT_PADDING 1
353 /* offset of items in report display mode */
354 #define REPORT_MARGINX 2
356 /* padding for icon in large icon display mode
357 * ICON_TOP_PADDING_NOTHITABLE - space between top of box and area
358 * that HITTEST will see.
359 * ICON_TOP_PADDING_HITABLE - spacing between above and icon.
360 * ICON_TOP_PADDING - sum of the two above.
361 * ICON_BOTTOM_PADDING - between bottom of icon and top of text
362 * LABEL_HOR_PADDING - between text and sides of box
363 * LABEL_VERT_PADDING - between bottom of text and end of box
365 * ICON_LR_PADDING - additional width above icon size.
366 * ICON_LR_HALF - half of the above value
368 #define ICON_TOP_PADDING_NOTHITABLE 2
369 #define ICON_TOP_PADDING_HITABLE 2
370 #define ICON_TOP_PADDING (ICON_TOP_PADDING_NOTHITABLE + ICON_TOP_PADDING_HITABLE)
371 #define ICON_BOTTOM_PADDING 4
372 #define LABEL_HOR_PADDING 5
373 #define LABEL_VERT_PADDING 7
374 #define ICON_LR_PADDING 16
375 #define ICON_LR_HALF (ICON_LR_PADDING/2)
377 /* default label width for items in list and small icon display modes */
378 #define DEFAULT_LABEL_WIDTH 40
379 /* maximum select rectangle width for empty text item in LV_VIEW_DETAILS */
380 #define MAX_EMPTYTEXT_SELECT_WIDTH 80
382 /* default column width for items in list display mode */
383 #define DEFAULT_COLUMN_WIDTH 128
385 /* Size of "line" scroll for V & H scrolls */
386 #define LISTVIEW_SCROLL_ICON_LINE_SIZE 37
388 /* Padding between image and label */
389 #define IMAGE_PADDING 2
391 /* Padding behind the label */
392 #define TRAILING_LABEL_PADDING 12
393 #define TRAILING_HEADER_PADDING 11
395 /* Border for the icon caption */
396 #define CAPTION_BORDER 2
398 /* Standard DrawText flags */
399 #define LV_ML_DT_FLAGS (DT_TOP | DT_NOPREFIX | DT_EDITCONTROL | DT_CENTER | DT_WORDBREAK | DT_WORD_ELLIPSIS | DT_END_ELLIPSIS)
400 #define LV_FL_DT_FLAGS (DT_TOP | DT_NOPREFIX | DT_EDITCONTROL | DT_CENTER | DT_WORDBREAK | DT_NOCLIP)
401 #define LV_SL_DT_FLAGS (DT_VCENTER | DT_NOPREFIX | DT_EDITCONTROL | DT_SINGLELINE | DT_WORD_ELLIPSIS | DT_END_ELLIPSIS)
403 /* Image index from state */
404 #define STATEIMAGEINDEX(x) (((x) & LVIS_STATEIMAGEMASK) >> 12)
406 /* The time in milliseconds to reset the search in the list */
407 #define KEY_DELAY 450
409 /* Dump the LISTVIEW_INFO structure to the debug channel */
410 #define LISTVIEW_DUMP(iP) do { \
411 TRACE("hwndSelf=%p, clrBk=%#lx, clrText=%#lx, clrTextBk=%#lx, ItemHeight=%d, ItemWidth=%d, Style=%#lx\n", \
412 iP->hwndSelf, iP->clrBk, iP->clrText, iP->clrTextBk, \
413 iP->nItemHeight, iP->nItemWidth, iP->dwStyle); \
414 TRACE("hwndSelf=%p, himlNor=%p, himlSml=%p, himlState=%p, Focused=%d, Hot=%d, exStyle=%#lx, Focus=%d\n", \
415 iP->hwndSelf, iP->himlNormal, iP->himlSmall, iP->himlState, \
416 iP->nFocusedItem, iP->nHotItem, iP->dwLvExStyle, iP->bFocus ); \
417 TRACE("hwndSelf=%p, ntmH=%d, icSz.cx=%ld, icSz.cy=%ld, icSp.cx=%ld, icSp.cy=%ld, notifyFmt=%d\n", \
418 iP->hwndSelf, iP->ntmHeight, iP->iconSize.cx, iP->iconSize.cy, \
419 iP->iconSpacing.cx, iP->iconSpacing.cy, iP->notifyFormat); \
420 TRACE("hwndSelf=%p, rcList=%s\n", iP->hwndSelf, wine_dbgstr_rect(&iP->rcList)); \
421 } while(0)
423 static const WCHAR themeClass[] = L"ListView";
426 * forward declarations
428 static BOOL LISTVIEW_GetItemT(const LISTVIEW_INFO *, LPLVITEMW, BOOL);
429 static void LISTVIEW_GetItemBox(const LISTVIEW_INFO *, INT, LPRECT);
430 static void LISTVIEW_GetItemOrigin(const LISTVIEW_INFO *, INT, LPPOINT);
431 static BOOL LISTVIEW_GetItemPosition(const LISTVIEW_INFO *, INT, LPPOINT);
432 static BOOL LISTVIEW_GetItemRect(const LISTVIEW_INFO *, INT, LPRECT);
433 static void LISTVIEW_GetOrigin(const LISTVIEW_INFO *, LPPOINT);
434 static BOOL LISTVIEW_GetViewRect(const LISTVIEW_INFO *, LPRECT);
435 static void LISTVIEW_UpdateSize(LISTVIEW_INFO *);
436 static LRESULT LISTVIEW_Command(LISTVIEW_INFO *, WPARAM, LPARAM);
437 static INT LISTVIEW_GetStringWidthT(const LISTVIEW_INFO *, LPCWSTR, BOOL);
438 static BOOL LISTVIEW_KeySelection(LISTVIEW_INFO *, INT, BOOL);
439 static UINT LISTVIEW_GetItemState(const LISTVIEW_INFO *, INT, UINT);
440 static BOOL LISTVIEW_SetItemState(LISTVIEW_INFO *, INT, const LVITEMW *);
441 static VOID LISTVIEW_SetOwnerDataState(LISTVIEW_INFO *, INT, INT, const LVITEMW *);
442 static LRESULT LISTVIEW_VScroll(LISTVIEW_INFO *, INT, INT);
443 static LRESULT LISTVIEW_HScroll(LISTVIEW_INFO *, INT, INT);
444 static BOOL LISTVIEW_EnsureVisible(LISTVIEW_INFO *, INT, BOOL);
445 static HIMAGELIST LISTVIEW_SetImageList(LISTVIEW_INFO *, INT, HIMAGELIST);
446 static INT LISTVIEW_HitTest(const LISTVIEW_INFO *, LPLVHITTESTINFO, BOOL, BOOL);
447 static BOOL LISTVIEW_EndEditLabelT(LISTVIEW_INFO *, BOOL, BOOL);
448 static BOOL LISTVIEW_Scroll(LISTVIEW_INFO *, INT, INT);
450 /******** Text handling functions *************************************/
452 /* A text pointer is either NULL, LPSTR_TEXTCALLBACK, or points to a
453 * text string. The string may be ANSI or Unicode, in which case
454 * the boolean isW tells us the type of the string.
456 * The name of the function tell what type of strings it expects:
457 * W: Unicode, T: ANSI/Unicode - function of isW
460 static inline BOOL is_text(LPCWSTR text)
462 return text != NULL && text != LPSTR_TEXTCALLBACKW;
465 static inline int textlenT(LPCWSTR text, BOOL isW)
467 return !is_text(text) ? 0 :
468 isW ? lstrlenW(text) : lstrlenA((LPCSTR)text);
471 static inline void textcpynT(LPWSTR dest, BOOL isDestW, LPCWSTR src, BOOL isSrcW, INT max)
473 if (isDestW)
474 if (isSrcW) lstrcpynW(dest, src, max);
475 else MultiByteToWideChar(CP_ACP, 0, (LPCSTR)src, -1, dest, max);
476 else
477 if (isSrcW) WideCharToMultiByte(CP_ACP, 0, src, -1, (LPSTR)dest, max, NULL, NULL);
478 else lstrcpynA((LPSTR)dest, (LPCSTR)src, max);
481 static inline LPWSTR textdupTtoW(LPCWSTR text, BOOL isW)
483 LPWSTR wstr = (LPWSTR)text;
485 if (!isW && is_text(text))
487 INT len = MultiByteToWideChar(CP_ACP, 0, (LPCSTR)text, -1, NULL, 0);
488 wstr = Alloc(len * sizeof(WCHAR));
489 if (wstr) MultiByteToWideChar(CP_ACP, 0, (LPCSTR)text, -1, wstr, len);
491 TRACE(" wstr=%s\n", text == LPSTR_TEXTCALLBACKW ? "(callback)" : debugstr_w(wstr));
492 return wstr;
495 static inline void textfreeT(LPWSTR wstr, BOOL isW)
497 if (!isW && is_text(wstr)) Free (wstr);
501 * dest is a pointer to a Unicode string
502 * src is a pointer to a string (Unicode if isW, ANSI if !isW)
504 static BOOL textsetptrT(LPWSTR *dest, LPCWSTR src, BOOL isW)
506 BOOL bResult = TRUE;
508 if (src == LPSTR_TEXTCALLBACKW)
510 if (is_text(*dest)) Free(*dest);
511 *dest = LPSTR_TEXTCALLBACKW;
513 else
515 LPWSTR pszText = textdupTtoW(src, isW);
516 if (*dest == LPSTR_TEXTCALLBACKW) *dest = NULL;
517 bResult = Str_SetPtrW(dest, pszText);
518 textfreeT(pszText, isW);
520 return bResult;
524 * compares a Unicode to a Unicode/ANSI text string
526 static inline int textcmpWT(LPCWSTR aw, LPCWSTR bt, BOOL isW)
528 if (!aw) return bt ? -1 : 0;
529 if (!bt) return 1;
530 if (aw == LPSTR_TEXTCALLBACKW)
531 return bt == LPSTR_TEXTCALLBACKW ? 1 : -1;
532 if (bt != LPSTR_TEXTCALLBACKW)
534 LPWSTR bw = textdupTtoW(bt, isW);
535 int r = bw ? lstrcmpW(aw, bw) : 1;
536 textfreeT(bw, isW);
537 return r;
540 return 1;
543 /******** Debugging functions *****************************************/
545 static inline LPCSTR debugtext_t(LPCWSTR text, BOOL isW)
547 if (text == LPSTR_TEXTCALLBACKW) return "(callback)";
548 return isW ? debugstr_w(text) : debugstr_a((LPCSTR)text);
551 static inline LPCSTR debugtext_tn(LPCWSTR text, BOOL isW, INT n)
553 if (text == LPSTR_TEXTCALLBACKW) return "(callback)";
554 n = min(textlenT(text, isW), n);
555 return isW ? debugstr_wn(text, n) : debugstr_an((LPCSTR)text, n);
558 static char* debug_getbuf(void)
560 static int index = 0;
561 static char buffers[DEBUG_BUFFERS][DEBUG_BUFFER_SIZE];
562 return buffers[index++ % DEBUG_BUFFERS];
565 static inline const char* debugrange(const RANGE *lprng)
567 if (!lprng) return "(null)";
568 return wine_dbg_sprintf("[%d, %d]", lprng->lower, lprng->upper);
571 static const char* debugscrollinfo(const SCROLLINFO *pScrollInfo)
573 char* buf = debug_getbuf(), *text = buf;
574 int len, size = DEBUG_BUFFER_SIZE;
576 if (pScrollInfo == NULL) return "(null)";
577 len = snprintf(buf, size, "{cbSize=%u, ", pScrollInfo->cbSize);
578 if (len == -1) goto end;
579 buf += len; size -= len;
580 if (pScrollInfo->fMask & SIF_RANGE)
581 len = snprintf(buf, size, "nMin=%d, nMax=%d, ", pScrollInfo->nMin, pScrollInfo->nMax);
582 else len = 0;
583 if (len == -1) goto end;
584 buf += len; size -= len;
585 if (pScrollInfo->fMask & SIF_PAGE)
586 len = snprintf(buf, size, "nPage=%u, ", pScrollInfo->nPage);
587 else len = 0;
588 if (len == -1) goto end;
589 buf += len; size -= len;
590 if (pScrollInfo->fMask & SIF_POS)
591 len = snprintf(buf, size, "nPos=%d, ", pScrollInfo->nPos);
592 else len = 0;
593 if (len == -1) goto end;
594 buf += len; size -= len;
595 if (pScrollInfo->fMask & SIF_TRACKPOS)
596 len = snprintf(buf, size, "nTrackPos=%d, ", pScrollInfo->nTrackPos);
597 else len = 0;
598 if (len == -1) goto end;
599 buf += len;
600 goto undo;
601 end:
602 buf = text + strlen(text);
603 undo:
604 if (buf - text > 2) { buf[-2] = '}'; buf[-1] = 0; }
605 return text;
608 static const char* debugnmlistview(const NMLISTVIEW *plvnm)
610 if (!plvnm) return "(null)";
611 return wine_dbg_sprintf("iItem=%d, iSubItem=%d, uNewState=0x%x,"
612 " uOldState=0x%x, uChanged=0x%x, ptAction=%s, lParam=%Id",
613 plvnm->iItem, plvnm->iSubItem, plvnm->uNewState, plvnm->uOldState,
614 plvnm->uChanged, wine_dbgstr_point(&plvnm->ptAction), plvnm->lParam);
617 static const char* debuglvitem_t(const LVITEMW *lpLVItem, BOOL isW)
619 char* buf = debug_getbuf(), *text = buf;
620 int len, size = DEBUG_BUFFER_SIZE;
622 if (lpLVItem == NULL) return "(null)";
623 len = snprintf(buf, size, "{iItem=%d, iSubItem=%d, ", lpLVItem->iItem, lpLVItem->iSubItem);
624 if (len == -1) goto end;
625 buf += len; size -= len;
626 if (lpLVItem->mask & LVIF_STATE)
627 len = snprintf(buf, size, "state=%x, stateMask=%x, ", lpLVItem->state, lpLVItem->stateMask);
628 else len = 0;
629 if (len == -1) goto end;
630 buf += len; size -= len;
631 if (lpLVItem->mask & LVIF_TEXT)
632 len = snprintf(buf, size, "pszText=%s, cchTextMax=%d, ", debugtext_tn(lpLVItem->pszText, isW, 80), lpLVItem->cchTextMax);
633 else len = 0;
634 if (len == -1) goto end;
635 buf += len; size -= len;
636 if (lpLVItem->mask & LVIF_IMAGE)
637 len = snprintf(buf, size, "iImage=%d, ", lpLVItem->iImage);
638 else len = 0;
639 if (len == -1) goto end;
640 buf += len; size -= len;
641 if (lpLVItem->mask & LVIF_PARAM)
642 len = snprintf(buf, size, "lParam=%Ix, ", lpLVItem->lParam);
643 else len = 0;
644 if (len == -1) goto end;
645 buf += len; size -= len;
646 if (lpLVItem->mask & LVIF_INDENT)
647 len = snprintf(buf, size, "iIndent=%d, ", lpLVItem->iIndent);
648 else len = 0;
649 if (len == -1) goto end;
650 buf += len;
651 goto undo;
652 end:
653 buf = text + strlen(text);
654 undo:
655 if (buf - text > 2) { buf[-2] = '}'; buf[-1] = 0; }
656 return text;
659 static const char* debuglvcolumn_t(const LVCOLUMNW *lpColumn, BOOL isW)
661 char* buf = debug_getbuf(), *text = buf;
662 int len, size = DEBUG_BUFFER_SIZE;
664 if (lpColumn == NULL) return "(null)";
665 len = snprintf(buf, size, "{");
666 if (len == -1) goto end;
667 buf += len; size -= len;
668 if (lpColumn->mask & LVCF_SUBITEM)
669 len = snprintf(buf, size, "iSubItem=%d, ", lpColumn->iSubItem);
670 else len = 0;
671 if (len == -1) goto end;
672 buf += len; size -= len;
673 if (lpColumn->mask & LVCF_FMT)
674 len = snprintf(buf, size, "fmt=%x, ", lpColumn->fmt);
675 else len = 0;
676 if (len == -1) goto end;
677 buf += len; size -= len;
678 if (lpColumn->mask & LVCF_WIDTH)
679 len = snprintf(buf, size, "cx=%d, ", lpColumn->cx);
680 else len = 0;
681 if (len == -1) goto end;
682 buf += len; size -= len;
683 if (lpColumn->mask & LVCF_TEXT)
684 len = snprintf(buf, size, "pszText=%s, cchTextMax=%d, ", debugtext_tn(lpColumn->pszText, isW, 80), lpColumn->cchTextMax);
685 else len = 0;
686 if (len == -1) goto end;
687 buf += len; size -= len;
688 if (lpColumn->mask & LVCF_IMAGE)
689 len = snprintf(buf, size, "iImage=%d, ", lpColumn->iImage);
690 else len = 0;
691 if (len == -1) goto end;
692 buf += len; size -= len;
693 if (lpColumn->mask & LVCF_ORDER)
694 len = snprintf(buf, size, "iOrder=%d, ", lpColumn->iOrder);
695 else len = 0;
696 if (len == -1) goto end;
697 buf += len;
698 goto undo;
699 end:
700 buf = text + strlen(text);
701 undo:
702 if (buf - text > 2) { buf[-2] = '}'; buf[-1] = 0; }
703 return text;
706 static const char* debuglvhittestinfo(const LVHITTESTINFO *lpht)
708 if (!lpht) return "(null)";
710 return wine_dbg_sprintf("{pt=%s, flags=0x%x, iItem=%d, iSubItem=%d}",
711 wine_dbgstr_point(&lpht->pt), lpht->flags, lpht->iItem, lpht->iSubItem);
714 /* Return the corresponding text for a given scroll value */
715 static inline LPCSTR debugscrollcode(int nScrollCode)
717 switch(nScrollCode)
719 case SB_LINELEFT: return "SB_LINELEFT";
720 case SB_LINERIGHT: return "SB_LINERIGHT";
721 case SB_PAGELEFT: return "SB_PAGELEFT";
722 case SB_PAGERIGHT: return "SB_PAGERIGHT";
723 case SB_THUMBPOSITION: return "SB_THUMBPOSITION";
724 case SB_THUMBTRACK: return "SB_THUMBTRACK";
725 case SB_ENDSCROLL: return "SB_ENDSCROLL";
726 case SB_INTERNAL: return "SB_INTERNAL";
727 default: return "unknown";
732 /******** Notification functions ************************************/
734 static int get_ansi_notification(UINT unicodeNotificationCode)
736 switch (unicodeNotificationCode)
738 case LVN_BEGINLABELEDITA:
739 case LVN_BEGINLABELEDITW: return LVN_BEGINLABELEDITA;
740 case LVN_ENDLABELEDITA:
741 case LVN_ENDLABELEDITW: return LVN_ENDLABELEDITA;
742 case LVN_GETDISPINFOA:
743 case LVN_GETDISPINFOW: return LVN_GETDISPINFOA;
744 case LVN_SETDISPINFOA:
745 case LVN_SETDISPINFOW: return LVN_SETDISPINFOA;
746 case LVN_ODFINDITEMA:
747 case LVN_ODFINDITEMW: return LVN_ODFINDITEMA;
748 case LVN_GETINFOTIPA:
749 case LVN_GETINFOTIPW: return LVN_GETINFOTIPA;
750 /* header forwards */
751 case HDN_TRACKA:
752 case HDN_TRACKW: return HDN_TRACKA;
753 case HDN_ENDTRACKA:
754 case HDN_ENDTRACKW: return HDN_ENDTRACKA;
755 case HDN_BEGINDRAG: return HDN_BEGINDRAG;
756 case HDN_ENDDRAG: return HDN_ENDDRAG;
757 case HDN_ITEMCHANGINGA:
758 case HDN_ITEMCHANGINGW: return HDN_ITEMCHANGINGA;
759 case HDN_ITEMCHANGEDA:
760 case HDN_ITEMCHANGEDW: return HDN_ITEMCHANGEDA;
761 case HDN_ITEMCLICKA:
762 case HDN_ITEMCLICKW: return HDN_ITEMCLICKA;
763 case HDN_DIVIDERDBLCLICKA:
764 case HDN_DIVIDERDBLCLICKW: return HDN_DIVIDERDBLCLICKA;
765 default: break;
767 FIXME("unknown notification %x\n", unicodeNotificationCode);
768 return unicodeNotificationCode;
771 /* forwards header notifications to listview parent */
772 static LRESULT notify_forward_header(const LISTVIEW_INFO *infoPtr, NMHEADERW *lpnmhW)
774 LPCWSTR text = NULL, filter = NULL;
775 LRESULT ret;
776 NMHEADERA *lpnmh = (NMHEADERA*) lpnmhW;
778 /* on unicode format exit earlier */
779 if (infoPtr->notifyFormat == NFR_UNICODE)
780 return SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, lpnmh->hdr.idFrom,
781 (LPARAM)lpnmh);
783 /* header always supplies unicode notifications,
784 all we have to do is to convert strings to ANSI */
785 if (lpnmh->pitem)
787 /* convert item text */
788 if (lpnmh->pitem->mask & HDI_TEXT)
790 text = (LPCWSTR)lpnmh->pitem->pszText;
791 lpnmh->pitem->pszText = NULL;
792 Str_SetPtrWtoA(&lpnmh->pitem->pszText, text);
794 /* convert filter text */
795 if ((lpnmh->pitem->mask & HDI_FILTER) && (lpnmh->pitem->type == HDFT_ISSTRING) &&
796 lpnmh->pitem->pvFilter)
798 filter = (LPCWSTR)((HD_TEXTFILTERA*)lpnmh->pitem->pvFilter)->pszText;
799 ((HD_TEXTFILTERA*)lpnmh->pitem->pvFilter)->pszText = NULL;
800 Str_SetPtrWtoA(&((HD_TEXTFILTERA*)lpnmh->pitem->pvFilter)->pszText, filter);
803 lpnmh->hdr.code = get_ansi_notification(lpnmh->hdr.code);
805 ret = SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, lpnmh->hdr.idFrom,
806 (LPARAM)lpnmh);
808 /* cleanup */
809 if(text)
811 Free(lpnmh->pitem->pszText);
812 lpnmh->pitem->pszText = (LPSTR)text;
814 if(filter)
816 Free(((HD_TEXTFILTERA*)lpnmh->pitem->pvFilter)->pszText);
817 ((HD_TEXTFILTERA*)lpnmh->pitem->pvFilter)->pszText = (LPSTR)filter;
820 return ret;
823 static LRESULT notify_hdr(const LISTVIEW_INFO *infoPtr, INT code, LPNMHDR pnmh)
825 LRESULT result;
827 TRACE("(code=%d)\n", code);
829 pnmh->hwndFrom = infoPtr->hwndSelf;
830 pnmh->idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
831 pnmh->code = code;
832 result = SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, pnmh->idFrom, (LPARAM)pnmh);
834 TRACE(" <= %Id\n", result);
836 return result;
839 static inline BOOL notify(const LISTVIEW_INFO *infoPtr, INT code)
841 NMHDR nmh;
842 HWND hwnd = infoPtr->hwndSelf;
843 notify_hdr(infoPtr, code, &nmh);
844 return IsWindow(hwnd);
847 static inline void notify_itemactivate(const LISTVIEW_INFO *infoPtr, const LVHITTESTINFO *htInfo)
849 NMITEMACTIVATE nmia;
850 LVITEMW item;
852 nmia.uNewState = 0;
853 nmia.uOldState = 0;
854 nmia.uChanged = 0;
855 nmia.uKeyFlags = 0;
857 item.mask = LVIF_PARAM|LVIF_STATE;
858 item.iItem = htInfo->iItem;
859 item.iSubItem = 0;
860 item.stateMask = (UINT)-1;
861 if (LISTVIEW_GetItemT(infoPtr, &item, TRUE)) {
862 nmia.lParam = item.lParam;
863 nmia.uOldState = item.state;
864 nmia.uNewState = item.state | LVIS_ACTIVATING;
865 nmia.uChanged = LVIF_STATE;
868 nmia.iItem = htInfo->iItem;
869 nmia.iSubItem = htInfo->iSubItem;
870 nmia.ptAction = htInfo->pt;
872 if (GetKeyState(VK_SHIFT) & 0x8000) nmia.uKeyFlags |= LVKF_SHIFT;
873 if (GetKeyState(VK_CONTROL) & 0x8000) nmia.uKeyFlags |= LVKF_CONTROL;
874 if (GetKeyState(VK_MENU) & 0x8000) nmia.uKeyFlags |= LVKF_ALT;
876 notify_hdr(infoPtr, LVN_ITEMACTIVATE, (LPNMHDR)&nmia);
879 static inline LRESULT notify_listview(const LISTVIEW_INFO *infoPtr, INT code, LPNMLISTVIEW plvnm)
881 TRACE("(code=%d, plvnm=%s)\n", code, debugnmlistview(plvnm));
882 return notify_hdr(infoPtr, code, (LPNMHDR)plvnm);
885 /* Handles NM_DBLCLK, NM_CLICK, NM_RDBLCLK, NM_RCLICK. Only NM_RCLICK return value is used. */
886 static BOOL notify_click(const LISTVIEW_INFO *infoPtr, INT code, const LVHITTESTINFO *lvht)
888 NMITEMACTIVATE nmia;
889 LVITEMW item;
890 HWND hwnd = infoPtr->hwndSelf;
891 LRESULT ret;
893 TRACE("code=%d, lvht=%s\n", code, debuglvhittestinfo(lvht));
894 ZeroMemory(&nmia, sizeof(nmia));
895 nmia.iItem = lvht->iItem;
896 nmia.iSubItem = lvht->iSubItem;
897 nmia.ptAction = lvht->pt;
898 item.mask = LVIF_PARAM;
899 item.iItem = lvht->iItem;
900 item.iSubItem = 0;
901 if (LISTVIEW_GetItemT(infoPtr, &item, TRUE)) nmia.lParam = item.lParam;
902 ret = notify_hdr(infoPtr, code, (NMHDR*)&nmia);
903 return IsWindow(hwnd) && (code == NM_RCLICK ? !ret : TRUE);
906 static BOOL notify_deleteitem(const LISTVIEW_INFO *infoPtr, INT nItem)
908 NMLISTVIEW nmlv;
909 LVITEMW item;
910 HWND hwnd = infoPtr->hwndSelf;
912 ZeroMemory(&nmlv, sizeof (NMLISTVIEW));
913 nmlv.iItem = nItem;
914 item.mask = LVIF_PARAM;
915 item.iItem = nItem;
916 item.iSubItem = 0;
917 if (LISTVIEW_GetItemT(infoPtr, &item, TRUE)) nmlv.lParam = item.lParam;
918 notify_listview(infoPtr, LVN_DELETEITEM, &nmlv);
919 return IsWindow(hwnd);
923 Send notification. depends on dispinfoW having same
924 structure as dispinfoA.
925 infoPtr : listview struct
926 code : *Unicode* notification code
927 pdi : dispinfo structure (can be unicode or ansi)
928 isW : TRUE if dispinfo is Unicode
930 static BOOL notify_dispinfoT(const LISTVIEW_INFO *infoPtr, UINT code, LPNMLVDISPINFOW pdi, BOOL isW)
932 INT length = 0, ret_length;
933 LPWSTR buffer = NULL, ret_text;
934 BOOL return_ansi = FALSE;
935 BOOL return_unicode = FALSE;
936 BOOL ret;
938 if ((pdi->item.mask & LVIF_TEXT) && is_text(pdi->item.pszText))
940 return_unicode = ( isW && infoPtr->notifyFormat == NFR_ANSI);
941 return_ansi = (!isW && infoPtr->notifyFormat == NFR_UNICODE);
944 ret_length = pdi->item.cchTextMax;
945 ret_text = pdi->item.pszText;
947 if (return_unicode || return_ansi)
949 if (code != LVN_GETDISPINFOW)
951 length = return_ansi ?
952 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pdi->item.pszText, -1, NULL, 0):
953 WideCharToMultiByte(CP_ACP, 0, pdi->item.pszText, -1, NULL, 0, NULL, NULL);
955 else
957 length = pdi->item.cchTextMax;
958 *pdi->item.pszText = 0; /* make sure we don't process garbage */
961 buffer = Alloc( (return_ansi ? sizeof(WCHAR) : sizeof(CHAR)) * length);
962 if (!buffer) return FALSE;
964 if (return_ansi)
965 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pdi->item.pszText, -1,
966 buffer, length);
967 else
968 WideCharToMultiByte(CP_ACP, 0, pdi->item.pszText, -1, (LPSTR) buffer,
969 length, NULL, NULL);
971 pdi->item.pszText = buffer;
972 pdi->item.cchTextMax = length;
975 if (infoPtr->notifyFormat == NFR_ANSI)
976 code = get_ansi_notification(code);
978 TRACE(" pdi->item=%s\n", debuglvitem_t(&pdi->item, infoPtr->notifyFormat != NFR_ANSI));
979 ret = notify_hdr(infoPtr, code, &pdi->hdr);
980 TRACE(" resulting code=%d\n", pdi->hdr.code);
982 if (return_ansi || return_unicode)
984 if (return_ansi && (pdi->hdr.code == LVN_GETDISPINFOA))
986 strcpy((char*)ret_text, (char*)pdi->item.pszText);
988 else if (return_unicode && (pdi->hdr.code == LVN_GETDISPINFOW))
990 lstrcpyW(ret_text, pdi->item.pszText);
992 else if (return_ansi) /* note : pointer can be changed by app ! */
994 WideCharToMultiByte(CP_ACP, 0, pdi->item.pszText, -1, (LPSTR) ret_text,
995 ret_length, NULL, NULL);
997 else
998 MultiByteToWideChar(CP_ACP, 0, (LPSTR) pdi->item.pszText, -1,
999 ret_text, ret_length);
1001 pdi->item.pszText = ret_text; /* restores our buffer */
1002 pdi->item.cchTextMax = ret_length;
1004 Free(buffer);
1005 return ret;
1008 /* if dispinfo holder changed notification code then convert */
1009 if (!isW && (pdi->hdr.code == LVN_GETDISPINFOW) && (pdi->item.mask & LVIF_TEXT))
1011 length = WideCharToMultiByte(CP_ACP, 0, pdi->item.pszText, -1, NULL, 0, NULL, NULL);
1013 buffer = Alloc(length * sizeof(CHAR));
1014 if (!buffer) return FALSE;
1016 WideCharToMultiByte(CP_ACP, 0, pdi->item.pszText, -1, (LPSTR) buffer,
1017 ret_length, NULL, NULL);
1019 strcpy((LPSTR)pdi->item.pszText, (LPSTR)buffer);
1020 Free(buffer);
1023 return ret;
1026 static void customdraw_fill(NMLVCUSTOMDRAW *lpnmlvcd, const LISTVIEW_INFO *infoPtr, HDC hdc,
1027 const RECT *rcBounds, const LVITEMW *lplvItem)
1029 ZeroMemory(lpnmlvcd, sizeof(NMLVCUSTOMDRAW));
1030 lpnmlvcd->nmcd.hdc = hdc;
1031 lpnmlvcd->nmcd.rc = *rcBounds;
1032 lpnmlvcd->clrTextBk = infoPtr->clrTextBk;
1033 lpnmlvcd->clrText = infoPtr->clrText;
1034 if (!lplvItem) return;
1035 lpnmlvcd->nmcd.dwItemSpec = lplvItem->iItem + 1;
1036 lpnmlvcd->iSubItem = lplvItem->iSubItem;
1037 if (lplvItem->state & LVIS_SELECTED) lpnmlvcd->nmcd.uItemState |= CDIS_SELECTED;
1038 if (lplvItem->state & LVIS_FOCUSED) lpnmlvcd->nmcd.uItemState |= CDIS_FOCUS;
1039 if (lplvItem->iItem == infoPtr->nHotItem) lpnmlvcd->nmcd.uItemState |= CDIS_HOT;
1040 lpnmlvcd->nmcd.lItemlParam = lplvItem->lParam;
1043 static inline DWORD notify_customdraw (const LISTVIEW_INFO *infoPtr, DWORD dwDrawStage, NMLVCUSTOMDRAW *lpnmlvcd)
1045 BOOL isForItem = (lpnmlvcd->nmcd.dwItemSpec != 0);
1046 DWORD result;
1048 lpnmlvcd->nmcd.dwDrawStage = dwDrawStage;
1049 if (isForItem) lpnmlvcd->nmcd.dwDrawStage |= CDDS_ITEM;
1050 if (lpnmlvcd->iSubItem) lpnmlvcd->nmcd.dwDrawStage |= CDDS_SUBITEM;
1051 if (isForItem) lpnmlvcd->nmcd.dwItemSpec--;
1052 result = notify_hdr(infoPtr, NM_CUSTOMDRAW, &lpnmlvcd->nmcd.hdr);
1053 if (isForItem) lpnmlvcd->nmcd.dwItemSpec++;
1054 return result;
1057 static void prepaint_setup (const LISTVIEW_INFO *infoPtr, HDC hdc, const NMLVCUSTOMDRAW *cd, BOOL SubItem)
1059 COLORREF backcolor, textcolor;
1061 backcolor = cd->clrTextBk;
1062 textcolor = cd->clrText;
1064 /* apparently, for selected items, we have to override the returned values */
1065 if (!SubItem)
1067 if (cd->nmcd.uItemState & CDIS_SELECTED)
1069 if (infoPtr->bFocus)
1071 backcolor = comctl32_color.clrHighlight;
1072 textcolor = comctl32_color.clrHighlightText;
1074 else if (infoPtr->dwStyle & LVS_SHOWSELALWAYS)
1076 backcolor = comctl32_color.clr3dFace;
1077 textcolor = comctl32_color.clrBtnText;
1082 if (backcolor == CLR_DEFAULT)
1083 backcolor = comctl32_color.clrWindow;
1084 if (textcolor == CLR_DEFAULT)
1085 textcolor = comctl32_color.clrWindowText;
1087 /* Set the text attributes */
1088 if (backcolor != CLR_NONE)
1090 SetBkMode(hdc, OPAQUE);
1091 SetBkColor(hdc, backcolor);
1093 else
1094 SetBkMode(hdc, TRANSPARENT);
1095 SetTextColor(hdc, textcolor);
1098 static inline DWORD notify_postpaint (const LISTVIEW_INFO *infoPtr, NMLVCUSTOMDRAW *lpnmlvcd)
1100 return notify_customdraw(infoPtr, CDDS_POSTPAINT, lpnmlvcd);
1103 /* returns TRUE when repaint needed, FALSE otherwise */
1104 static BOOL notify_measureitem(LISTVIEW_INFO *infoPtr)
1106 MEASUREITEMSTRUCT mis;
1107 mis.CtlType = ODT_LISTVIEW;
1108 mis.CtlID = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
1109 mis.itemID = -1;
1110 mis.itemWidth = 0;
1111 mis.itemData = 0;
1112 mis.itemHeight= infoPtr->nItemHeight;
1113 SendMessageW(infoPtr->hwndNotify, WM_MEASUREITEM, mis.CtlID, (LPARAM)&mis);
1114 if (infoPtr->nItemHeight != max(mis.itemHeight, 1))
1116 infoPtr->nMeasureItemHeight = infoPtr->nItemHeight = max(mis.itemHeight, 1);
1117 return TRUE;
1119 return FALSE;
1122 /******** Item iterator functions **********************************/
1124 static RANGES ranges_create(int count);
1125 static void ranges_destroy(RANGES ranges);
1126 static BOOL ranges_add(RANGES ranges, RANGE range);
1127 static BOOL ranges_del(RANGES ranges, RANGE range);
1128 static void ranges_dump(RANGES ranges);
1130 static inline BOOL ranges_additem(RANGES ranges, INT nItem)
1132 RANGE range = { nItem, nItem + 1 };
1134 return ranges_add(ranges, range);
1137 static inline BOOL ranges_delitem(RANGES ranges, INT nItem)
1139 RANGE range = { nItem, nItem + 1 };
1141 return ranges_del(ranges, range);
1144 /***
1145 * ITERATOR DOCUMENTATION
1147 * The iterator functions allow for easy, and convenient iteration
1148 * over items of interest in the list. Typically, you create an
1149 * iterator, use it, and destroy it, as such:
1150 * ITERATOR i;
1152 * iterator_xxxitems(&i, ...);
1153 * while (iterator_{prev,next}(&i)
1155 * //code which uses i.nItem
1157 * iterator_destroy(&i);
1159 * where xxx is either: framed, or visible.
1160 * Note that it is important that the code destroys the iterator
1161 * after it's done with it, as the creation of the iterator may
1162 * allocate memory, which thus needs to be freed.
1164 * You can iterate both forwards, and backwards through the list,
1165 * by using iterator_next or iterator_prev respectively.
1167 * Lower numbered items are draw on top of higher number items in
1168 * LVS_ICON, and LVS_SMALLICON (which are the only modes where
1169 * items may overlap). So, to test items, you should use
1170 * iterator_next
1171 * which lists the items top to bottom (in Z-order).
1172 * For drawing items, you should use
1173 * iterator_prev
1174 * which lists the items bottom to top (in Z-order).
1175 * If you keep iterating over the items after the end-of-items
1176 * marker (-1) is returned, the iterator will start from the
1177 * beginning. Typically, you don't need to test for -1,
1178 * because iterator_{next,prev} will return TRUE if more items
1179 * are to be iterated over, or FALSE otherwise.
1181 * Note: the iterator is defined to be bidirectional. That is,
1182 * any number of prev followed by any number of next, or
1183 * five versa, should leave the iterator at the same item:
1184 * prev * n, next * n = next * n, prev * n
1186 * The iterator has a notion of an out-of-order, special item,
1187 * which sits at the start of the list. This is used in
1188 * LVS_ICON, and LVS_SMALLICON mode to handle the focused item,
1189 * which needs to be first, as it may overlap other items.
1191 * The code is a bit messy because we have:
1192 * - a special item to deal with
1193 * - simple range, or composite range
1194 * - empty range.
1195 * If you find bugs, or want to add features, please make sure you
1196 * always check/modify *both* iterator_prev, and iterator_next.
1199 /****
1200 * This function iterates through the items in increasing order,
1201 * but prefixed by the special item, then -1. That is:
1202 * special, 1, 2, 3, ..., n, -1.
1203 * Each item is listed only once.
1205 static inline BOOL iterator_next(ITERATOR* i)
1207 if (i->nItem == -1)
1209 i->nItem = i->nSpecial;
1210 if (i->nItem != -1) return TRUE;
1212 if (i->nItem == i->nSpecial)
1214 if (i->ranges) i->index = 0;
1215 goto pickarange;
1218 i->nItem++;
1219 testitem:
1220 if (i->nItem == i->nSpecial) i->nItem++;
1221 if (i->nItem < i->range.upper) return TRUE;
1223 pickarange:
1224 if (i->ranges)
1226 if (i->index < DPA_GetPtrCount(i->ranges->hdpa))
1227 i->range = *(RANGE*)DPA_GetPtr(i->ranges->hdpa, i->index++);
1228 else goto end;
1230 else if (i->nItem >= i->range.upper) goto end;
1232 i->nItem = i->range.lower;
1233 if (i->nItem >= 0) goto testitem;
1234 end:
1235 i->nItem = -1;
1236 return FALSE;
1239 /****
1240 * This function iterates through the items in decreasing order,
1241 * followed by the special item, then -1. That is:
1242 * n, n-1, ..., 3, 2, 1, special, -1.
1243 * Each item is listed only once.
1245 static inline BOOL iterator_prev(ITERATOR* i)
1247 BOOL start = FALSE;
1249 if (i->nItem == -1)
1251 start = TRUE;
1252 if (i->ranges) i->index = DPA_GetPtrCount(i->ranges->hdpa);
1253 goto pickarange;
1255 if (i->nItem == i->nSpecial)
1257 i->nItem = -1;
1258 return FALSE;
1261 testitem:
1262 i->nItem--;
1263 if (i->nItem == i->nSpecial) i->nItem--;
1264 if (i->nItem >= i->range.lower) return TRUE;
1266 pickarange:
1267 if (i->ranges)
1269 if (i->index > 0)
1270 i->range = *(RANGE*)DPA_GetPtr(i->ranges->hdpa, --i->index);
1271 else goto end;
1273 else if (!start && i->nItem < i->range.lower) goto end;
1275 i->nItem = i->range.upper;
1276 if (i->nItem > 0) goto testitem;
1277 end:
1278 return (i->nItem = i->nSpecial) != -1;
1281 static RANGE iterator_range(const ITERATOR *i)
1283 RANGE range;
1285 if (!i->ranges) return i->range;
1287 if (DPA_GetPtrCount(i->ranges->hdpa) > 0)
1289 range.lower = (*(RANGE*)DPA_GetPtr(i->ranges->hdpa, 0)).lower;
1290 range.upper = (*(RANGE*)DPA_GetPtr(i->ranges->hdpa, DPA_GetPtrCount(i->ranges->hdpa) - 1)).upper;
1292 else range.lower = range.upper = 0;
1294 return range;
1297 /***
1298 * Releases resources associated with this iterator.
1300 static inline void iterator_destroy(const ITERATOR *i)
1302 ranges_destroy(i->ranges);
1305 /***
1306 * Create an empty iterator.
1308 static inline void iterator_empty(ITERATOR* i)
1310 ZeroMemory(i, sizeof(*i));
1311 i->nItem = i->nSpecial = i->range.lower = i->range.upper = -1;
1314 /***
1315 * Create an iterator over a range.
1317 static inline void iterator_rangeitems(ITERATOR* i, RANGE range)
1319 iterator_empty(i);
1320 i->range = range;
1323 /***
1324 * Create an iterator over a bunch of ranges.
1325 * Please note that the iterator will take ownership of the ranges,
1326 * and will free them upon destruction.
1328 static inline void iterator_rangesitems(ITERATOR* i, RANGES ranges)
1330 iterator_empty(i);
1331 i->ranges = ranges;
1334 /***
1335 * Creates an iterator over the items which intersect frame.
1336 * Uses absolute coordinates rather than compensating for the current offset.
1338 static BOOL iterator_frameditems_absolute(ITERATOR* i, const LISTVIEW_INFO* infoPtr, const RECT *frame)
1340 RECT rcItem, rcTemp;
1341 RANGES ranges;
1343 TRACE("(frame=%s)\n", wine_dbgstr_rect(frame));
1345 /* in case we fail, we want to return an empty iterator */
1346 iterator_empty(i);
1348 if (infoPtr->nItemCount == 0)
1349 return TRUE;
1351 if (infoPtr->uView == LV_VIEW_ICON || infoPtr->uView == LV_VIEW_SMALLICON)
1353 INT nItem;
1355 if (infoPtr->uView == LV_VIEW_ICON && infoPtr->nFocusedItem != -1)
1357 LISTVIEW_GetItemBox(infoPtr, infoPtr->nFocusedItem, &rcItem);
1358 if (IntersectRect(&rcTemp, &rcItem, frame))
1359 i->nSpecial = infoPtr->nFocusedItem;
1361 if (!(ranges = ranges_create(50))) return FALSE;
1362 iterator_rangesitems(i, ranges);
1363 /* to do better here, we need to have PosX, and PosY sorted */
1364 TRACE("building icon ranges:\n");
1365 for (nItem = 0; nItem < infoPtr->nItemCount; nItem++)
1367 rcItem.left = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosX, nItem);
1368 rcItem.top = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosY, nItem);
1369 rcItem.right = rcItem.left + infoPtr->nItemWidth;
1370 rcItem.bottom = rcItem.top + infoPtr->nItemHeight;
1371 if (IntersectRect(&rcTemp, &rcItem, frame))
1372 ranges_additem(i->ranges, nItem);
1374 return TRUE;
1376 else if (infoPtr->uView == LV_VIEW_DETAILS)
1378 RANGE range;
1380 if (frame->left >= infoPtr->nItemWidth) return TRUE;
1381 if (frame->top >= infoPtr->nItemHeight * infoPtr->nItemCount) return TRUE;
1383 range.lower = max(frame->top / infoPtr->nItemHeight, 0);
1384 range.upper = min((frame->bottom - 1) / infoPtr->nItemHeight, infoPtr->nItemCount - 1) + 1;
1385 if (range.upper <= range.lower) return TRUE;
1386 iterator_rangeitems(i, range);
1387 TRACE(" report=%s\n", debugrange(&i->range));
1389 else
1391 INT nPerCol = max((infoPtr->rcList.bottom - infoPtr->rcList.top) / infoPtr->nItemHeight, 1);
1392 INT nFirstRow = max(frame->top / infoPtr->nItemHeight, 0);
1393 INT nLastRow = min((frame->bottom - 1) / infoPtr->nItemHeight, nPerCol - 1);
1394 INT nFirstCol;
1395 INT nLastCol;
1396 INT lower;
1397 RANGE item_range;
1398 INT nCol;
1400 if (infoPtr->nItemWidth)
1402 nFirstCol = max(frame->left / infoPtr->nItemWidth, 0);
1403 nLastCol = min((frame->right - 1) / infoPtr->nItemWidth, (infoPtr->nItemCount + nPerCol - 1) / nPerCol);
1405 else
1407 nFirstCol = max(frame->left, 0);
1408 nLastCol = min(frame->right - 1, (infoPtr->nItemCount + nPerCol - 1) / nPerCol);
1411 lower = nFirstCol * nPerCol + nFirstRow;
1413 TRACE("nPerCol=%d, nFirstRow=%d, nLastRow=%d, nFirstCol=%d, nLastCol=%d, lower=%d\n",
1414 nPerCol, nFirstRow, nLastRow, nFirstCol, nLastCol, lower);
1416 if (nLastCol < nFirstCol || nLastRow < nFirstRow) return TRUE;
1418 if (!(ranges = ranges_create(nLastCol - nFirstCol + 1))) return FALSE;
1419 iterator_rangesitems(i, ranges);
1420 TRACE("building list ranges:\n");
1421 for (nCol = nFirstCol; nCol <= nLastCol; nCol++)
1423 item_range.lower = nCol * nPerCol + nFirstRow;
1424 if(item_range.lower >= infoPtr->nItemCount) break;
1425 item_range.upper = min(nCol * nPerCol + nLastRow + 1, infoPtr->nItemCount);
1426 TRACE(" list=%s\n", debugrange(&item_range));
1427 ranges_add(i->ranges, item_range);
1431 return TRUE;
1434 /***
1435 * Creates an iterator over the items which intersect lprc.
1437 static BOOL iterator_frameditems(ITERATOR* i, const LISTVIEW_INFO* infoPtr, const RECT *lprc)
1439 RECT frame = *lprc;
1440 POINT Origin;
1442 TRACE("(lprc=%s)\n", wine_dbgstr_rect(lprc));
1444 LISTVIEW_GetOrigin(infoPtr, &Origin);
1445 OffsetRect(&frame, -Origin.x, -Origin.y);
1447 return iterator_frameditems_absolute(i, infoPtr, &frame);
1450 /***
1451 * Creates an iterator over the items which intersect the visible region of hdc.
1453 static BOOL iterator_visibleitems(ITERATOR *i, const LISTVIEW_INFO *infoPtr, HDC hdc)
1455 POINT Origin, Position;
1456 RECT rcItem, rcClip;
1457 INT rgntype;
1459 rgntype = GetClipBox(hdc, &rcClip);
1460 if (rgntype == NULLREGION)
1462 iterator_empty(i);
1463 return TRUE;
1465 if (!iterator_frameditems(i, infoPtr, &rcClip)) return FALSE;
1466 if (rgntype == SIMPLEREGION) return TRUE;
1468 /* first deal with the special item */
1469 if (i->nSpecial != -1)
1471 LISTVIEW_GetItemBox(infoPtr, i->nSpecial, &rcItem);
1472 if (!RectVisible(hdc, &rcItem)) i->nSpecial = -1;
1475 /* if we can't deal with the region, we'll just go with the simple range */
1476 LISTVIEW_GetOrigin(infoPtr, &Origin);
1477 TRACE("building visible range:\n");
1478 if (!i->ranges && i->range.lower < i->range.upper)
1480 if (!(i->ranges = ranges_create(50))) return TRUE;
1481 if (!ranges_add(i->ranges, i->range))
1483 ranges_destroy(i->ranges);
1484 i->ranges = 0;
1485 return TRUE;
1489 /* now delete the invisible items from the list */
1490 while(iterator_next(i))
1492 LISTVIEW_GetItemOrigin(infoPtr, i->nItem, &Position);
1493 rcItem.left = (infoPtr->uView == LV_VIEW_DETAILS) ? Origin.x : Position.x + Origin.x;
1494 rcItem.top = Position.y + Origin.y;
1495 rcItem.right = rcItem.left + infoPtr->nItemWidth;
1496 rcItem.bottom = rcItem.top + infoPtr->nItemHeight;
1497 if (!RectVisible(hdc, &rcItem))
1498 ranges_delitem(i->ranges, i->nItem);
1500 /* the iterator should restart on the next iterator_next */
1501 TRACE("done\n");
1503 return TRUE;
1506 /* Remove common elements from two iterators */
1507 /* Passed iterators have to point on the first elements */
1508 static BOOL iterator_remove_common_items(ITERATOR *iter1, ITERATOR *iter2)
1510 if(!iter1->ranges || !iter2->ranges) {
1511 int lower, upper;
1513 if(iter1->ranges || iter2->ranges ||
1514 (iter1->range.lower<iter2->range.lower && iter1->range.upper>iter2->range.upper) ||
1515 (iter1->range.lower>iter2->range.lower && iter1->range.upper<iter2->range.upper)) {
1516 ERR("result is not a one range iterator\n");
1517 return FALSE;
1520 if(iter1->range.lower==-1 || iter2->range.lower==-1)
1521 return TRUE;
1523 lower = iter1->range.lower;
1524 upper = iter1->range.upper;
1526 if(lower < iter2->range.lower)
1527 iter1->range.upper = iter2->range.lower;
1528 else if(upper > iter2->range.upper)
1529 iter1->range.lower = iter2->range.upper;
1530 else
1531 iter1->range.lower = iter1->range.upper = -1;
1533 if(iter2->range.lower < lower)
1534 iter2->range.upper = lower;
1535 else if(iter2->range.upper > upper)
1536 iter2->range.lower = upper;
1537 else
1538 iter2->range.lower = iter2->range.upper = -1;
1540 return TRUE;
1543 iterator_next(iter1);
1544 iterator_next(iter2);
1546 while(1) {
1547 if(iter1->nItem==-1 || iter2->nItem==-1)
1548 break;
1550 if(iter1->nItem == iter2->nItem) {
1551 int delete = iter1->nItem;
1553 iterator_prev(iter1);
1554 iterator_prev(iter2);
1555 ranges_delitem(iter1->ranges, delete);
1556 ranges_delitem(iter2->ranges, delete);
1557 iterator_next(iter1);
1558 iterator_next(iter2);
1559 } else if(iter1->nItem > iter2->nItem)
1560 iterator_next(iter2);
1561 else
1562 iterator_next(iter1);
1565 iter1->nItem = iter1->range.lower = iter1->range.upper = -1;
1566 iter2->nItem = iter2->range.lower = iter2->range.upper = -1;
1567 return TRUE;
1570 /******** Misc helper functions ************************************/
1572 static inline LRESULT CallWindowProcT(WNDPROC proc, HWND hwnd, UINT uMsg,
1573 WPARAM wParam, LPARAM lParam, BOOL isW)
1575 if (isW) return CallWindowProcW(proc, hwnd, uMsg, wParam, lParam);
1576 else return CallWindowProcA(proc, hwnd, uMsg, wParam, lParam);
1579 static inline BOOL is_autoarrange(const LISTVIEW_INFO *infoPtr)
1581 return (infoPtr->dwStyle & LVS_AUTOARRANGE) &&
1582 (infoPtr->uView == LV_VIEW_ICON || infoPtr->uView == LV_VIEW_SMALLICON);
1585 static void toggle_checkbox_state(LISTVIEW_INFO *infoPtr, INT nItem)
1587 DWORD state = STATEIMAGEINDEX(LISTVIEW_GetItemState(infoPtr, nItem, LVIS_STATEIMAGEMASK));
1588 if(state == 1 || state == 2)
1590 LVITEMW lvitem;
1591 state ^= 3;
1592 lvitem.state = INDEXTOSTATEIMAGEMASK(state);
1593 lvitem.stateMask = LVIS_STATEIMAGEMASK;
1594 LISTVIEW_SetItemState(infoPtr, nItem, &lvitem);
1598 /* this should be called after window style got updated,
1599 it used to reset view state to match current window style */
1600 static inline void map_style_view(LISTVIEW_INFO *infoPtr)
1602 switch (infoPtr->dwStyle & LVS_TYPEMASK)
1604 case LVS_ICON:
1605 infoPtr->uView = LV_VIEW_ICON;
1606 break;
1607 case LVS_REPORT:
1608 infoPtr->uView = LV_VIEW_DETAILS;
1609 break;
1610 case LVS_SMALLICON:
1611 infoPtr->uView = LV_VIEW_SMALLICON;
1612 break;
1613 case LVS_LIST:
1614 infoPtr->uView = LV_VIEW_LIST;
1618 /* computes next item id value */
1619 static DWORD get_next_itemid(const LISTVIEW_INFO *infoPtr)
1621 INT count = DPA_GetPtrCount(infoPtr->hdpaItemIds);
1623 if (count > 0)
1625 ITEM_ID *lpID = DPA_GetPtr(infoPtr->hdpaItemIds, count - 1);
1626 return lpID->id + 1;
1628 return 0;
1631 /******** Internal API functions ************************************/
1633 static inline COLUMN_INFO * LISTVIEW_GetColumnInfo(const LISTVIEW_INFO *infoPtr, INT nSubItem)
1635 static COLUMN_INFO mainItem;
1637 if (nSubItem == 0 && DPA_GetPtrCount(infoPtr->hdpaColumns) == 0) return &mainItem;
1638 assert (nSubItem >= 0 && nSubItem < DPA_GetPtrCount(infoPtr->hdpaColumns));
1640 /* update cached column rectangles */
1641 if (infoPtr->colRectsDirty)
1643 COLUMN_INFO *info;
1644 LISTVIEW_INFO *Ptr = (LISTVIEW_INFO*)infoPtr;
1645 INT i;
1647 for (i = 0; i < DPA_GetPtrCount(infoPtr->hdpaColumns); i++) {
1648 info = DPA_GetPtr(infoPtr->hdpaColumns, i);
1649 SendMessageW(infoPtr->hwndHeader, HDM_GETITEMRECT, i, (LPARAM)&info->rcHeader);
1651 Ptr->colRectsDirty = FALSE;
1654 return DPA_GetPtr(infoPtr->hdpaColumns, nSubItem);
1657 static INT LISTVIEW_CreateHeader(LISTVIEW_INFO *infoPtr)
1659 DWORD dFlags = WS_CHILD | HDS_HORZ | HDS_FULLDRAG | HDS_DRAGDROP;
1660 HINSTANCE hInst;
1662 if (infoPtr->hwndHeader) return 0;
1664 TRACE("Creating header for list %p\n", infoPtr->hwndSelf);
1666 /* setup creation flags */
1667 dFlags |= (LVS_NOSORTHEADER & infoPtr->dwStyle) ? 0 : HDS_BUTTONS;
1668 dFlags |= (LVS_NOCOLUMNHEADER & infoPtr->dwStyle) ? HDS_HIDDEN : 0;
1670 hInst = (HINSTANCE)GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_HINSTANCE);
1672 /* create header */
1673 infoPtr->hwndHeader = CreateWindowW(WC_HEADERW, NULL, dFlags,
1674 0, 0, 0, 0, infoPtr->hwndSelf, NULL, hInst, NULL);
1675 if (!infoPtr->hwndHeader) return -1;
1677 /* set header unicode format */
1678 SendMessageW(infoPtr->hwndHeader, HDM_SETUNICODEFORMAT, TRUE, 0);
1680 /* set header font */
1681 SendMessageW(infoPtr->hwndHeader, WM_SETFONT, (WPARAM)infoPtr->hFont, TRUE);
1683 /* set header image list */
1684 if (infoPtr->himlSmall)
1685 SendMessageW(infoPtr->hwndHeader, HDM_SETIMAGELIST, 0, (LPARAM)infoPtr->himlSmall);
1687 LISTVIEW_UpdateSize(infoPtr);
1689 return 0;
1692 static inline void LISTVIEW_GetHeaderRect(const LISTVIEW_INFO *infoPtr, INT nSubItem, LPRECT lprc)
1694 *lprc = LISTVIEW_GetColumnInfo(infoPtr, nSubItem)->rcHeader;
1697 static inline BOOL LISTVIEW_IsHeaderEnabled(const LISTVIEW_INFO *infoPtr)
1699 return (infoPtr->uView == LV_VIEW_DETAILS ||
1700 infoPtr->dwLvExStyle & LVS_EX_HEADERINALLVIEWS) &&
1701 !(infoPtr->dwStyle & LVS_NOCOLUMNHEADER);
1704 static inline BOOL LISTVIEW_GetItemW(const LISTVIEW_INFO *infoPtr, LPLVITEMW lpLVItem)
1706 return LISTVIEW_GetItemT(infoPtr, lpLVItem, TRUE);
1709 /* used to handle collapse main item column case */
1710 static inline BOOL LISTVIEW_DrawFocusRect(const LISTVIEW_INFO *infoPtr, HDC hdc)
1712 return (infoPtr->rcFocus.left < infoPtr->rcFocus.right) ?
1713 DrawFocusRect(hdc, &infoPtr->rcFocus) : FALSE;
1716 /* Listview invalidation functions: use _only_ these functions to invalidate */
1718 static inline BOOL is_redrawing(const LISTVIEW_INFO *infoPtr)
1720 return infoPtr->redraw;
1723 static inline void LISTVIEW_InvalidateRect(const LISTVIEW_INFO *infoPtr, const RECT* rect)
1725 if(!is_redrawing(infoPtr)) return;
1726 TRACE(" invalidating rect=%s\n", wine_dbgstr_rect(rect));
1727 InvalidateRect(infoPtr->hwndSelf, rect, TRUE);
1730 static inline void LISTVIEW_InvalidateItem(const LISTVIEW_INFO *infoPtr, INT nItem)
1732 RECT rcBox;
1734 if (!is_redrawing(infoPtr) || nItem < 0 || nItem >= infoPtr->nItemCount)
1735 return;
1737 LISTVIEW_GetItemBox(infoPtr, nItem, &rcBox);
1738 LISTVIEW_InvalidateRect(infoPtr, &rcBox);
1741 static inline void LISTVIEW_InvalidateSubItem(const LISTVIEW_INFO *infoPtr, INT nItem, INT nSubItem)
1743 POINT Origin, Position;
1744 RECT rcBox;
1746 if(!is_redrawing(infoPtr)) return;
1747 assert (infoPtr->uView == LV_VIEW_DETAILS);
1748 LISTVIEW_GetOrigin(infoPtr, &Origin);
1749 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position);
1750 LISTVIEW_GetHeaderRect(infoPtr, nSubItem, &rcBox);
1751 rcBox.top = 0;
1752 rcBox.bottom = infoPtr->nItemHeight;
1753 OffsetRect(&rcBox, Origin.x, Origin.y + Position.y);
1754 LISTVIEW_InvalidateRect(infoPtr, &rcBox);
1757 static inline void LISTVIEW_InvalidateList(const LISTVIEW_INFO *infoPtr)
1759 LISTVIEW_InvalidateRect(infoPtr, NULL);
1762 static inline void LISTVIEW_InvalidateColumn(const LISTVIEW_INFO *infoPtr, INT nColumn)
1764 RECT rcCol;
1766 if(!is_redrawing(infoPtr)) return;
1767 LISTVIEW_GetHeaderRect(infoPtr, nColumn, &rcCol);
1768 rcCol.top = infoPtr->rcList.top;
1769 rcCol.bottom = infoPtr->rcList.bottom;
1770 LISTVIEW_InvalidateRect(infoPtr, &rcCol);
1773 /***
1774 * DESCRIPTION:
1775 * Retrieves the number of items that can fit vertically in the client area.
1777 * PARAMETER(S):
1778 * [I] infoPtr : valid pointer to the listview structure
1780 * RETURN:
1781 * Number of items per row.
1783 static inline INT LISTVIEW_GetCountPerRow(const LISTVIEW_INFO *infoPtr)
1785 INT nListWidth = infoPtr->rcList.right - infoPtr->rcList.left;
1787 return max(nListWidth/(infoPtr->nItemWidth ? infoPtr->nItemWidth : 1), 1);
1790 /***
1791 * DESCRIPTION:
1792 * Retrieves the number of items that can fit horizontally in the client
1793 * area.
1795 * PARAMETER(S):
1796 * [I] infoPtr : valid pointer to the listview structure
1798 * RETURN:
1799 * Number of items per column.
1801 static inline INT LISTVIEW_GetCountPerColumn(const LISTVIEW_INFO *infoPtr)
1803 INT nListHeight = infoPtr->rcList.bottom - infoPtr->rcList.top;
1805 return infoPtr->nItemHeight ? max(nListHeight / infoPtr->nItemHeight, 1) : 0;
1809 /*************************************************************************
1810 * LISTVIEW_ProcessLetterKeys
1812 * Processes keyboard messages generated by pressing the letter keys
1813 * on the keyboard.
1814 * What this does is perform a case insensitive search from the
1815 * current position with the following quirks:
1816 * - If two chars or more are pressed in quick succession we search
1817 * for the corresponding string (e.g. 'abc').
1818 * - If there is a delay we wipe away the current search string and
1819 * restart with just that char.
1820 * - If the user keeps pressing the same character, whether slowly or
1821 * fast, so that the search string is entirely composed of this
1822 * character ('aaaaa' for instance), then we search for first item
1823 * that starting with that character.
1824 * - If the user types the above character in quick succession, then
1825 * we must also search for the corresponding string ('aaaaa'), and
1826 * go to that string if there is a match.
1828 * PARAMETERS
1829 * [I] hwnd : handle to the window
1830 * [I] charCode : the character code, the actual character
1831 * [I] keyData : key data
1833 * RETURNS
1835 * Zero.
1837 * BUGS
1839 * - The current implementation has a list of characters it will
1840 * accept and it ignores everything else. In particular it will
1841 * ignore accentuated characters which seems to match what
1842 * Windows does. But I'm not sure it makes sense to follow
1843 * Windows there.
1844 * - We don't sound a beep when the search fails.
1846 * SEE ALSO
1848 * TREEVIEW_ProcessLetterKeys
1850 static INT LISTVIEW_ProcessLetterKeys(LISTVIEW_INFO *infoPtr, WPARAM charCode, LPARAM keyData)
1852 WCHAR buffer[MAX_PATH];
1853 DWORD prevTime;
1854 LVITEMW item;
1855 int startidx;
1856 INT nItem;
1857 INT diff;
1859 /* simple parameter checking */
1860 if (!charCode || !keyData || infoPtr->nItemCount == 0) return 0;
1862 /* only allow the valid WM_CHARs through */
1863 if (!iswalnum(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 charCode != '}' && charCode != '[' && charCode != '{' &&
1871 charCode != '/' && charCode != '?' && charCode != '>' &&
1872 charCode != '<' && charCode != ',' && charCode != '~')
1873 return 0;
1875 /* update the search parameters */
1876 prevTime = infoPtr->lastKeyPressTimestamp;
1877 infoPtr->lastKeyPressTimestamp = GetTickCount();
1878 diff = infoPtr->lastKeyPressTimestamp - prevTime;
1880 if (diff >= 0 && diff < KEY_DELAY)
1882 if (infoPtr->nSearchParamLength < MAX_PATH - 1)
1883 infoPtr->szSearchParam[infoPtr->nSearchParamLength++] = charCode;
1885 if (infoPtr->charCode != charCode)
1886 infoPtr->charCode = charCode = 0;
1888 else
1890 infoPtr->charCode = charCode;
1891 infoPtr->szSearchParam[0] = charCode;
1892 infoPtr->nSearchParamLength = 1;
1895 /* should start from next after focused item, so next item that matches
1896 will be selected, if there isn't any and focused matches it will be selected
1897 on second search stage from beginning of the list */
1898 if (infoPtr->nFocusedItem >= 0 && infoPtr->nItemCount > 1)
1900 /* with some accumulated search data available start with current focus, otherwise
1901 it's excluded from search */
1902 startidx = infoPtr->nSearchParamLength > 1 ? infoPtr->nFocusedItem : infoPtr->nFocusedItem + 1;
1903 if (startidx == infoPtr->nItemCount) startidx = 0;
1905 else
1906 startidx = 0;
1908 /* let application handle this for virtual listview */
1909 if (infoPtr->dwStyle & LVS_OWNERDATA)
1911 NMLVFINDITEMW nmlv;
1913 memset(&nmlv.lvfi, 0, sizeof(nmlv.lvfi));
1914 nmlv.lvfi.flags = (LVFI_WRAP | LVFI_PARTIAL);
1915 nmlv.lvfi.psz = infoPtr->szSearchParam;
1916 nmlv.iStart = startidx;
1918 infoPtr->szSearchParam[infoPtr->nSearchParamLength] = 0;
1920 nItem = notify_hdr(infoPtr, LVN_ODFINDITEMW, (LPNMHDR)&nmlv.hdr);
1922 else
1924 int i = startidx, endidx;
1926 /* and search from the current position */
1927 nItem = -1;
1928 endidx = infoPtr->nItemCount;
1930 /* first search in [startidx, endidx), on failure continue in [0, startidx) */
1931 while (1)
1933 /* start from first item if not found with >= startidx */
1934 if (i == infoPtr->nItemCount && startidx > 0)
1936 endidx = startidx;
1937 startidx = 0;
1940 for (i = startidx; i < endidx; i++)
1942 /* retrieve text */
1943 item.mask = LVIF_TEXT;
1944 item.iItem = i;
1945 item.iSubItem = 0;
1946 item.pszText = buffer;
1947 item.cchTextMax = MAX_PATH;
1948 if (!LISTVIEW_GetItemW(infoPtr, &item)) return 0;
1950 if (!wcsnicmp(item.pszText, infoPtr->szSearchParam, infoPtr->nSearchParamLength))
1952 nItem = i;
1953 break;
1955 /* this is used to find first char match when search string is not available yet,
1956 otherwise every WM_CHAR will search to next item by first char, ignoring that we're
1957 already waiting for user to complete a string */
1958 else if (nItem == -1 && infoPtr->nSearchParamLength == 1 && !wcsnicmp(item.pszText, infoPtr->szSearchParam, 1))
1960 /* this would work but we must keep looking for a longer match */
1961 nItem = i;
1965 if ( nItem != -1 || /* found something */
1966 endidx != infoPtr->nItemCount || /* second search done */
1967 (startidx == 0 && endidx == infoPtr->nItemCount) /* full range for first search */ )
1968 break;
1972 if (nItem != -1)
1973 LISTVIEW_KeySelection(infoPtr, nItem, FALSE);
1975 return 0;
1978 /*************************************************************************
1979 * LISTVIEW_UpdateHeaderSize [Internal]
1981 * Function to resize the header control
1983 * PARAMS
1984 * [I] hwnd : handle to a window
1985 * [I] nNewScrollPos : scroll pos to set
1987 * RETURNS
1988 * None.
1990 static void LISTVIEW_UpdateHeaderSize(const LISTVIEW_INFO *infoPtr, INT nNewScrollPos)
1992 RECT winRect;
1993 POINT point[2];
1995 TRACE("nNewScrollPos=%d\n", nNewScrollPos);
1997 if (!infoPtr->hwndHeader) return;
1999 GetWindowRect(infoPtr->hwndHeader, &winRect);
2000 point[0].x = winRect.left;
2001 point[0].y = winRect.top;
2002 point[1].x = winRect.right;
2003 point[1].y = winRect.bottom;
2005 MapWindowPoints(HWND_DESKTOP, infoPtr->hwndSelf, point, 2);
2006 point[0].x = -nNewScrollPos;
2007 point[1].x += nNewScrollPos;
2009 SetWindowPos(infoPtr->hwndHeader,0,
2010 point[0].x,point[0].y,point[1].x,point[1].y,
2011 (infoPtr->dwStyle & LVS_NOCOLUMNHEADER) ? SWP_HIDEWINDOW : SWP_SHOWWINDOW |
2012 SWP_NOZORDER | SWP_NOACTIVATE);
2015 static INT LISTVIEW_UpdateHScroll(LISTVIEW_INFO *infoPtr)
2017 SCROLLINFO horzInfo;
2018 INT dx;
2020 ZeroMemory(&horzInfo, sizeof(SCROLLINFO));
2021 horzInfo.cbSize = sizeof(SCROLLINFO);
2022 horzInfo.nPage = infoPtr->rcList.right - infoPtr->rcList.left;
2024 /* for now, we'll set info.nMax to the _count_, and adjust it later */
2025 if (infoPtr->uView == LV_VIEW_LIST)
2027 INT nPerCol = LISTVIEW_GetCountPerColumn(infoPtr);
2028 horzInfo.nMax = (infoPtr->nItemCount + nPerCol - 1) / nPerCol;
2030 /* scroll by at least one column per page */
2031 if(horzInfo.nPage < infoPtr->nItemWidth)
2032 horzInfo.nPage = infoPtr->nItemWidth;
2034 if (infoPtr->nItemWidth)
2035 horzInfo.nPage /= infoPtr->nItemWidth;
2037 else if (infoPtr->uView == LV_VIEW_DETAILS)
2039 horzInfo.nMax = infoPtr->nItemWidth;
2041 else /* LV_VIEW_ICON, or LV_VIEW_SMALLICON */
2043 RECT rcView;
2045 if (LISTVIEW_GetViewRect(infoPtr, &rcView)) horzInfo.nMax = rcView.right - rcView.left;
2048 if (LISTVIEW_IsHeaderEnabled(infoPtr))
2050 if (DPA_GetPtrCount(infoPtr->hdpaColumns))
2052 RECT rcHeader;
2053 INT index;
2055 index = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX,
2056 DPA_GetPtrCount(infoPtr->hdpaColumns) - 1, 0);
2058 LISTVIEW_GetHeaderRect(infoPtr, index, &rcHeader);
2059 horzInfo.nMax = rcHeader.right;
2060 TRACE("horzInfo.nMax=%d\n", horzInfo.nMax);
2064 horzInfo.fMask = SIF_RANGE | SIF_PAGE;
2065 horzInfo.nMax = max(horzInfo.nMax - 1, 0);
2066 dx = GetScrollPos(infoPtr->hwndSelf, SB_HORZ);
2067 dx -= SetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &horzInfo, TRUE);
2068 TRACE("horzInfo=%s\n", debugscrollinfo(&horzInfo));
2070 /* Update the Header Control */
2071 if (infoPtr->hwndHeader)
2073 horzInfo.fMask = SIF_POS;
2074 GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &horzInfo);
2075 LISTVIEW_UpdateHeaderSize(infoPtr, horzInfo.nPos);
2078 LISTVIEW_UpdateSize(infoPtr);
2079 return dx;
2082 static INT LISTVIEW_UpdateVScroll(LISTVIEW_INFO *infoPtr)
2084 SCROLLINFO vertInfo;
2085 INT dy;
2087 ZeroMemory(&vertInfo, sizeof(SCROLLINFO));
2088 vertInfo.cbSize = sizeof(SCROLLINFO);
2089 vertInfo.nPage = infoPtr->rcList.bottom - infoPtr->rcList.top;
2091 if (infoPtr->uView == LV_VIEW_DETAILS)
2093 vertInfo.nMax = infoPtr->nItemCount;
2095 /* scroll by at least one page */
2096 if(vertInfo.nPage < infoPtr->nItemHeight)
2097 vertInfo.nPage = infoPtr->nItemHeight;
2099 if (infoPtr->nItemHeight > 0)
2100 vertInfo.nPage /= infoPtr->nItemHeight;
2102 else if (infoPtr->uView != LV_VIEW_LIST) /* LV_VIEW_ICON, or LV_VIEW_SMALLICON */
2104 RECT rcView;
2106 if (LISTVIEW_GetViewRect(infoPtr, &rcView)) vertInfo.nMax = rcView.bottom - rcView.top;
2109 vertInfo.fMask = SIF_RANGE | SIF_PAGE;
2110 vertInfo.nMax = max(vertInfo.nMax - 1, 0);
2111 dy = GetScrollPos(infoPtr->hwndSelf, SB_VERT);
2112 dy -= SetScrollInfo(infoPtr->hwndSelf, SB_VERT, &vertInfo, TRUE);
2113 TRACE("vertInfo=%s\n", debugscrollinfo(&vertInfo));
2115 LISTVIEW_UpdateSize(infoPtr);
2116 return dy;
2119 /***
2120 * DESCRIPTION:
2121 * Update the scrollbars. This function should be called whenever
2122 * the content, size or view changes.
2124 * PARAMETER(S):
2125 * [I] infoPtr : valid pointer to the listview structure
2127 * RETURN:
2128 * None
2130 static void LISTVIEW_UpdateScroll(LISTVIEW_INFO *infoPtr)
2132 INT dx, dy, pass;
2134 if ((infoPtr->dwStyle & LVS_NOSCROLL) || !is_redrawing(infoPtr)) return;
2136 /* Setting the horizontal scroll can change the listview size
2137 * (and potentially everything else) so we need to recompute
2138 * everything again for the vertical scroll and vice-versa
2140 for (dx = 0, dy = 0, pass = 0; pass <= 1; pass++)
2142 dx += LISTVIEW_UpdateHScroll(infoPtr);
2143 dy += LISTVIEW_UpdateVScroll(infoPtr);
2146 /* Change of the range may have changed the scroll pos. If so move the content */
2147 if (dx != 0 || dy != 0)
2149 RECT listRect;
2150 listRect = infoPtr->rcList;
2151 ScrollWindowEx(infoPtr->hwndSelf, dx, dy, &listRect, &listRect, 0, 0,
2152 SW_ERASE | SW_INVALIDATE);
2157 /***
2158 * DESCRIPTION:
2159 * Shows/hides the focus rectangle.
2161 * PARAMETER(S):
2162 * [I] infoPtr : valid pointer to the listview structure
2163 * [I] fShow : TRUE to show the focus, FALSE to hide it.
2165 * RETURN:
2166 * None
2168 static void LISTVIEW_ShowFocusRect(const LISTVIEW_INFO *infoPtr, BOOL fShow)
2170 HDC hdc;
2172 TRACE("fShow=%d, nItem=%d\n", fShow, infoPtr->nFocusedItem);
2174 if (infoPtr->nFocusedItem < 0) return;
2176 /* we need some gymnastics in ICON mode to handle large items */
2177 if (infoPtr->uView == LV_VIEW_ICON)
2179 RECT rcBox;
2181 LISTVIEW_GetItemBox(infoPtr, infoPtr->nFocusedItem, &rcBox);
2182 if ((rcBox.bottom - rcBox.top) > infoPtr->nItemHeight)
2184 LISTVIEW_InvalidateRect(infoPtr, &rcBox);
2185 return;
2189 if (!(hdc = GetDC(infoPtr->hwndSelf))) return;
2191 /* for some reason, owner draw should work only in report mode */
2192 if ((infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && (infoPtr->uView == LV_VIEW_DETAILS))
2194 DRAWITEMSTRUCT dis;
2195 LVITEMW item;
2197 HFONT hFont = infoPtr->hFont ? infoPtr->hFont : infoPtr->hDefaultFont;
2198 HFONT hOldFont = SelectObject(hdc, hFont);
2200 item.iItem = infoPtr->nFocusedItem;
2201 item.iSubItem = 0;
2202 item.mask = LVIF_PARAM;
2203 if (!LISTVIEW_GetItemW(infoPtr, &item)) goto done;
2205 ZeroMemory(&dis, sizeof(dis));
2206 dis.CtlType = ODT_LISTVIEW;
2207 dis.CtlID = (UINT)GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
2208 dis.itemID = item.iItem;
2209 dis.itemAction = ODA_FOCUS;
2210 if (fShow) dis.itemState |= ODS_FOCUS;
2211 dis.hwndItem = infoPtr->hwndSelf;
2212 dis.hDC = hdc;
2213 LISTVIEW_GetItemBox(infoPtr, dis.itemID, &dis.rcItem);
2214 dis.itemData = item.lParam;
2216 SendMessageW(infoPtr->hwndNotify, WM_DRAWITEM, dis.CtlID, (LPARAM)&dis);
2218 SelectObject(hdc, hOldFont);
2220 else
2221 LISTVIEW_InvalidateItem(infoPtr, infoPtr->nFocusedItem);
2223 done:
2224 ReleaseDC(infoPtr->hwndSelf, hdc);
2227 /***
2228 * Invalidates all visible selected items.
2230 static void LISTVIEW_InvalidateSelectedItems(const LISTVIEW_INFO *infoPtr)
2232 ITERATOR i;
2234 iterator_frameditems(&i, infoPtr, &infoPtr->rcList);
2235 while(iterator_next(&i))
2237 if (LISTVIEW_GetItemState(infoPtr, i.nItem, LVIS_SELECTED))
2238 LISTVIEW_InvalidateItem(infoPtr, i.nItem);
2240 iterator_destroy(&i);
2244 /***
2245 * DESCRIPTION: [INTERNAL]
2246 * Computes an item's (left,top) corner, relative to rcView.
2247 * That is, the position has NOT been made relative to the Origin.
2248 * This is deliberate, to avoid computing the Origin over, and
2249 * over again, when this function is called in a loop. Instead,
2250 * one can factor the computation of the Origin before the loop,
2251 * and offset the value returned by this function, on every iteration.
2253 * PARAMETER(S):
2254 * [I] infoPtr : valid pointer to the listview structure
2255 * [I] nItem : item number
2256 * [O] lpptOrig : item top, left corner
2258 * RETURN:
2259 * None.
2261 static void LISTVIEW_GetItemOrigin(const LISTVIEW_INFO *infoPtr, INT nItem, LPPOINT lpptPosition)
2263 assert(nItem >= 0 && nItem < infoPtr->nItemCount);
2265 if ((infoPtr->uView == LV_VIEW_SMALLICON) || (infoPtr->uView == LV_VIEW_ICON))
2267 lpptPosition->x = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosX, nItem);
2268 lpptPosition->y = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosY, nItem);
2270 else if (infoPtr->uView == LV_VIEW_LIST)
2272 INT nCountPerColumn = LISTVIEW_GetCountPerColumn(infoPtr);
2273 lpptPosition->x = nItem / nCountPerColumn * infoPtr->nItemWidth;
2274 lpptPosition->y = nItem % nCountPerColumn * infoPtr->nItemHeight;
2276 else /* LV_VIEW_DETAILS */
2278 lpptPosition->x = REPORT_MARGINX;
2279 /* item is always at zero indexed column */
2280 if (DPA_GetPtrCount(infoPtr->hdpaColumns) > 0)
2281 lpptPosition->x += LISTVIEW_GetColumnInfo(infoPtr, 0)->rcHeader.left;
2282 lpptPosition->y = nItem * infoPtr->nItemHeight;
2286 /***
2287 * DESCRIPTION: [INTERNAL]
2288 * Compute the rectangles of an item. This is to localize all
2289 * the computations in one place. If you are not interested in some
2290 * of these values, simply pass in a NULL -- the function is smart
2291 * enough to compute only what's necessary. The function computes
2292 * the standard rectangles (BOUNDS, ICON, LABEL) plus a non-standard
2293 * one, the BOX rectangle. This rectangle is very cheap to compute,
2294 * and is guaranteed to contain all the other rectangles. Computing
2295 * the ICON rect is also cheap, but all the others are potentially
2296 * expensive. This gives an easy and effective optimization when
2297 * searching (like point inclusion, or rectangle intersection):
2298 * first test against the BOX, and if TRUE, test against the desired
2299 * rectangle.
2300 * If the function does not have all the necessary information
2301 * to computed the requested rectangles, will crash with a
2302 * failed assertion. This is done so we catch all programming
2303 * errors, given that the function is called only from our code.
2305 * We have the following 'special' meanings for a few fields:
2306 * * If LVIS_FOCUSED is set, we assume the item has the focus
2307 * This is important in ICON mode, where it might get a larger
2308 * then usual rectangle
2310 * Please note that subitem support works only in REPORT mode.
2312 * PARAMETER(S):
2313 * [I] infoPtr : valid pointer to the listview structure
2314 * [I] lpLVItem : item to compute the measures for
2315 * [O] lprcBox : ptr to Box rectangle
2316 * Same as LVM_GETITEMRECT with LVIR_BOUNDS
2317 * [0] lprcSelectBox : ptr to select box rectangle
2318 * Same as LVM_GETITEMRECT with LVIR_SELECTEDBOUNDS
2319 * [O] lprcIcon : ptr to Icon rectangle
2320 * Same as LVM_GETITEMRECT with LVIR_ICON
2321 * [O] lprcStateIcon: ptr to State Icon rectangle
2322 * [O] lprcLabel : ptr to Label rectangle
2323 * Same as LVM_GETITEMRECT with LVIR_LABEL
2325 * RETURN:
2326 * None.
2328 static void LISTVIEW_GetItemMetrics(const LISTVIEW_INFO *infoPtr, const LVITEMW *lpLVItem,
2329 LPRECT lprcBox, LPRECT lprcSelectBox,
2330 LPRECT lprcIcon, LPRECT lprcStateIcon, LPRECT lprcLabel)
2332 BOOL doSelectBox = FALSE, doIcon = FALSE, doLabel = FALSE, oversizedBox = FALSE;
2333 RECT Box, SelectBox, Icon, Label;
2334 COLUMN_INFO *lpColumnInfo = NULL;
2335 SIZE labelSize = { 0, 0 };
2337 TRACE("(lpLVItem=%s)\n", debuglvitem_t(lpLVItem, TRUE));
2339 /* Be smart and try to figure out the minimum we have to do */
2340 if (lpLVItem->iSubItem) assert(infoPtr->uView == LV_VIEW_DETAILS);
2341 if (infoPtr->uView == LV_VIEW_ICON && (lprcBox || lprcLabel))
2343 assert((lpLVItem->mask & LVIF_STATE) && (lpLVItem->stateMask & LVIS_FOCUSED));
2344 if (lpLVItem->state & LVIS_FOCUSED) oversizedBox = doLabel = TRUE;
2346 if (lprcSelectBox) doSelectBox = TRUE;
2347 if (lprcLabel) doLabel = TRUE;
2348 if (doLabel || lprcIcon || lprcStateIcon) doIcon = TRUE;
2349 if (doSelectBox)
2351 doIcon = TRUE;
2352 doLabel = TRUE;
2355 /************************************************************/
2356 /* compute the box rectangle (it should be cheap to do) */
2357 /************************************************************/
2358 if (lpLVItem->iSubItem || infoPtr->uView == LV_VIEW_DETAILS)
2359 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, lpLVItem->iSubItem);
2361 if (lpLVItem->iSubItem)
2363 Box = lpColumnInfo->rcHeader;
2365 else
2367 Box.left = 0;
2368 Box.right = infoPtr->nItemWidth;
2370 Box.top = 0;
2371 Box.bottom = infoPtr->nItemHeight;
2373 /******************************************************************/
2374 /* compute ICON bounding box (ala LVM_GETITEMRECT) and STATEICON */
2375 /******************************************************************/
2376 if (doIcon)
2378 LONG state_width = 0;
2380 if (infoPtr->himlState && lpLVItem->iSubItem == 0)
2381 state_width = infoPtr->iconStateSize.cx;
2383 if (infoPtr->uView == LV_VIEW_ICON)
2385 Icon.left = Box.left + state_width;
2386 if (infoPtr->himlNormal)
2387 Icon.left += (infoPtr->nItemWidth - infoPtr->iconSize.cx - state_width) / 2;
2388 Icon.top = Box.top + ICON_TOP_PADDING;
2389 Icon.right = Icon.left;
2390 Icon.bottom = Icon.top;
2391 if (infoPtr->himlNormal)
2393 Icon.right += infoPtr->iconSize.cx;
2394 Icon.bottom += infoPtr->iconSize.cy;
2397 else /* LV_VIEW_SMALLICON, LV_VIEW_LIST or LV_VIEW_DETAILS */
2399 Icon.left = Box.left + state_width;
2401 if (infoPtr->uView == LV_VIEW_DETAILS && lpLVItem->iSubItem == 0)
2403 /* we need the indent in report mode */
2404 assert(lpLVItem->mask & LVIF_INDENT);
2405 Icon.left += infoPtr->iconSize.cx * lpLVItem->iIndent + REPORT_MARGINX;
2408 Icon.top = Box.top;
2409 Icon.right = Icon.left;
2410 if (infoPtr->himlSmall &&
2411 (!lpColumnInfo || lpLVItem->iSubItem == 0 ||
2412 ((infoPtr->dwLvExStyle & LVS_EX_SUBITEMIMAGES) && lpLVItem->iImage != I_IMAGECALLBACK)))
2413 Icon.right += infoPtr->iconSize.cx;
2414 Icon.bottom = Icon.top + infoPtr->iconSize.cy;
2416 if(lprcIcon) *lprcIcon = Icon;
2417 TRACE(" - icon=%s\n", wine_dbgstr_rect(&Icon));
2419 /* TODO: is this correct? */
2420 if (lprcStateIcon)
2422 lprcStateIcon->left = Icon.left - state_width;
2423 lprcStateIcon->right = Icon.left;
2424 lprcStateIcon->top = Icon.top;
2425 lprcStateIcon->bottom = lprcStateIcon->top + infoPtr->iconSize.cy;
2426 TRACE(" - state icon=%s\n", wine_dbgstr_rect(lprcStateIcon));
2429 else Icon.right = 0;
2431 /************************************************************/
2432 /* compute LABEL bounding box (ala LVM_GETITEMRECT) */
2433 /************************************************************/
2434 if (doLabel)
2436 /* calculate how far to the right can the label stretch */
2437 Label.right = Box.right;
2438 if (infoPtr->uView == LV_VIEW_DETAILS)
2440 if (lpLVItem->iSubItem == 0)
2442 /* we need a zero based rect here */
2443 Label = lpColumnInfo->rcHeader;
2444 OffsetRect(&Label, -Label.left, 0);
2448 if (lpLVItem->iSubItem || ((infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && infoPtr->uView == LV_VIEW_DETAILS))
2450 labelSize.cx = infoPtr->nItemWidth;
2451 labelSize.cy = infoPtr->nItemHeight;
2452 goto calc_label;
2455 /* we need the text in non owner draw mode */
2456 assert(lpLVItem->mask & LVIF_TEXT);
2457 if (is_text(lpLVItem->pszText))
2459 HFONT hFont = infoPtr->hFont ? infoPtr->hFont : infoPtr->hDefaultFont;
2460 HDC hdc = GetDC(infoPtr->hwndSelf);
2461 HFONT hOldFont = SelectObject(hdc, hFont);
2462 UINT uFormat;
2463 RECT rcText;
2465 /* compute rough rectangle where the label will go */
2466 SetRectEmpty(&rcText);
2467 rcText.right = infoPtr->nItemWidth - TRAILING_LABEL_PADDING;
2468 rcText.bottom = infoPtr->nItemHeight;
2469 if (infoPtr->uView == LV_VIEW_ICON)
2470 rcText.bottom -= ICON_TOP_PADDING + infoPtr->iconSize.cy + ICON_BOTTOM_PADDING;
2472 /* now figure out the flags */
2473 if (infoPtr->uView == LV_VIEW_ICON)
2474 uFormat = oversizedBox ? LV_FL_DT_FLAGS : LV_ML_DT_FLAGS;
2475 else
2476 uFormat = LV_SL_DT_FLAGS;
2478 DrawTextW (hdc, lpLVItem->pszText, -1, &rcText, uFormat | DT_CALCRECT);
2480 if (rcText.right != rcText.left)
2481 labelSize.cx = min(rcText.right - rcText.left + TRAILING_LABEL_PADDING, infoPtr->nItemWidth);
2483 labelSize.cy = rcText.bottom - rcText.top;
2485 SelectObject(hdc, hOldFont);
2486 ReleaseDC(infoPtr->hwndSelf, hdc);
2489 calc_label:
2490 if (infoPtr->uView == LV_VIEW_ICON)
2492 Label.left = Box.left + (infoPtr->nItemWidth - labelSize.cx) / 2;
2493 Label.top = Box.top + ICON_TOP_PADDING_HITABLE +
2494 infoPtr->iconSize.cy + ICON_BOTTOM_PADDING;
2495 Label.right = Label.left + labelSize.cx;
2496 Label.bottom = Label.top + infoPtr->nItemHeight;
2497 if (!oversizedBox && labelSize.cy > infoPtr->ntmHeight)
2499 labelSize.cy = min(Box.bottom - Label.top, labelSize.cy);
2500 labelSize.cy /= infoPtr->ntmHeight;
2501 labelSize.cy = max(labelSize.cy, 1);
2502 labelSize.cy *= infoPtr->ntmHeight;
2504 Label.bottom = Label.top + labelSize.cy + HEIGHT_PADDING;
2506 else if (infoPtr->uView == LV_VIEW_DETAILS)
2508 Label.left = Icon.right;
2509 Label.top = Box.top;
2510 Label.right = lpLVItem->iSubItem ? lpColumnInfo->rcHeader.right :
2511 lpColumnInfo->rcHeader.right - lpColumnInfo->rcHeader.left;
2512 Label.bottom = Label.top + infoPtr->nItemHeight;
2514 else /* LV_VIEW_SMALLICON or LV_VIEW_LIST */
2516 Label.left = Icon.right;
2517 Label.top = Box.top;
2518 Label.right = min(Label.left + labelSize.cx, Label.right);
2519 Label.bottom = Label.top + infoPtr->nItemHeight;
2522 if (lprcLabel) *lprcLabel = Label;
2523 TRACE(" - label=%s\n", wine_dbgstr_rect(&Label));
2526 /************************************************************/
2527 /* compute SELECT bounding box */
2528 /************************************************************/
2529 if (doSelectBox)
2531 if (infoPtr->uView == LV_VIEW_DETAILS)
2533 SelectBox.left = Icon.left;
2534 SelectBox.top = Box.top;
2535 SelectBox.bottom = Box.bottom;
2537 if (labelSize.cx)
2538 SelectBox.right = min(Label.left + labelSize.cx, Label.right);
2539 else
2540 SelectBox.right = min(Label.left + MAX_EMPTYTEXT_SELECT_WIDTH, Label.right);
2542 else
2544 UnionRect(&SelectBox, &Icon, &Label);
2546 if (lprcSelectBox) *lprcSelectBox = SelectBox;
2547 TRACE(" - select box=%s\n", wine_dbgstr_rect(&SelectBox));
2550 /* Fix the Box if necessary */
2551 if (lprcBox)
2553 if (oversizedBox) UnionRect(lprcBox, &Box, &Label);
2554 else *lprcBox = Box;
2556 TRACE(" - box=%s\n", wine_dbgstr_rect(&Box));
2559 /***
2560 * DESCRIPTION: [INTERNAL]
2562 * PARAMETER(S):
2563 * [I] infoPtr : valid pointer to the listview structure
2564 * [I] nItem : item number
2565 * [O] lprcBox : ptr to Box rectangle
2567 * RETURN:
2568 * None.
2570 static void LISTVIEW_GetItemBox(const LISTVIEW_INFO *infoPtr, INT nItem, LPRECT lprcBox)
2572 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
2573 POINT Position, Origin;
2574 LVITEMW lvItem;
2576 LISTVIEW_GetOrigin(infoPtr, &Origin);
2577 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position);
2579 /* Be smart and try to figure out the minimum we have to do */
2580 lvItem.mask = 0;
2581 if (infoPtr->uView == LV_VIEW_ICON && infoPtr->bFocus && LISTVIEW_GetItemState(infoPtr, nItem, LVIS_FOCUSED))
2582 lvItem.mask |= LVIF_TEXT;
2583 lvItem.iItem = nItem;
2584 lvItem.iSubItem = 0;
2585 lvItem.pszText = szDispText;
2586 lvItem.cchTextMax = DISP_TEXT_SIZE;
2587 if (lvItem.mask) LISTVIEW_GetItemW(infoPtr, &lvItem);
2588 if (infoPtr->uView == LV_VIEW_ICON)
2590 lvItem.mask |= LVIF_STATE;
2591 lvItem.stateMask = LVIS_FOCUSED;
2592 lvItem.state = (lvItem.mask & LVIF_TEXT ? LVIS_FOCUSED : 0);
2594 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, lprcBox, 0, 0, 0, 0);
2596 if (infoPtr->uView == LV_VIEW_DETAILS && infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT &&
2597 SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX, 0, 0))
2599 OffsetRect(lprcBox, Origin.x, Position.y + Origin.y);
2601 else
2602 OffsetRect(lprcBox, Position.x + Origin.x, Position.y + Origin.y);
2605 /* LISTVIEW_MapIdToIndex helper */
2606 static INT CALLBACK MapIdSearchCompare(LPVOID p1, LPVOID p2, LPARAM lParam)
2608 ITEM_ID *id1 = (ITEM_ID*)p1;
2609 ITEM_ID *id2 = (ITEM_ID*)p2;
2611 if (id1->id == id2->id) return 0;
2613 return (id1->id < id2->id) ? -1 : 1;
2616 /***
2617 * DESCRIPTION:
2618 * Returns the item index for id specified.
2620 * PARAMETER(S):
2621 * [I] infoPtr : valid pointer to the listview structure
2622 * [I] iID : item id to get index for
2624 * RETURN:
2625 * Item index, or -1 on failure.
2627 static INT LISTVIEW_MapIdToIndex(const LISTVIEW_INFO *infoPtr, UINT iID)
2629 ITEM_ID ID;
2630 INT index;
2632 TRACE("iID=%d\n", iID);
2634 if (infoPtr->dwStyle & LVS_OWNERDATA) return -1;
2635 if (infoPtr->nItemCount == 0) return -1;
2637 ID.id = iID;
2638 index = DPA_Search(infoPtr->hdpaItemIds, &ID, -1, MapIdSearchCompare, 0, DPAS_SORTED);
2640 if (index != -1)
2642 ITEM_ID *lpID = DPA_GetPtr(infoPtr->hdpaItemIds, index);
2643 return DPA_GetPtrIndex(infoPtr->hdpaItems, lpID->item);
2646 return -1;
2649 /***
2650 * DESCRIPTION:
2651 * Returns the item id for index given.
2653 * PARAMETER(S):
2654 * [I] infoPtr : valid pointer to the listview structure
2655 * [I] iItem : item index to get id for
2657 * RETURN:
2658 * Item id.
2660 static DWORD LISTVIEW_MapIndexToId(const LISTVIEW_INFO *infoPtr, INT iItem)
2662 ITEM_INFO *lpItem;
2663 HDPA hdpaSubItems;
2665 TRACE("iItem=%d\n", iItem);
2667 if (infoPtr->dwStyle & LVS_OWNERDATA) return -1;
2668 if (iItem < 0 || iItem >= infoPtr->nItemCount) return -1;
2670 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, iItem);
2671 lpItem = DPA_GetPtr(hdpaSubItems, 0);
2673 return lpItem->id->id;
2676 /***
2677 * DESCRIPTION:
2678 * Returns the current icon position, and advances it along the top.
2679 * The returned position is not offset by Origin.
2681 * PARAMETER(S):
2682 * [I] infoPtr : valid pointer to the listview structure
2683 * [O] lpPos : will get the current icon position
2685 * RETURN:
2686 * None
2688 static void LISTVIEW_NextIconPosTop(LISTVIEW_INFO *infoPtr, LPPOINT lpPos)
2690 INT nListWidth = infoPtr->rcList.right - infoPtr->rcList.left;
2692 *lpPos = infoPtr->currIconPos;
2694 infoPtr->currIconPos.x += infoPtr->nItemWidth;
2695 if (infoPtr->currIconPos.x + infoPtr->nItemWidth <= nListWidth) return;
2697 infoPtr->currIconPos.x = 0;
2698 infoPtr->currIconPos.y += infoPtr->nItemHeight;
2702 /***
2703 * DESCRIPTION:
2704 * Returns the current icon position, and advances it down the left edge.
2705 * The returned position is not offset by Origin.
2707 * PARAMETER(S):
2708 * [I] infoPtr : valid pointer to the listview structure
2709 * [O] lpPos : will get the current icon position
2711 * RETURN:
2712 * None
2714 static void LISTVIEW_NextIconPosLeft(LISTVIEW_INFO *infoPtr, LPPOINT lpPos)
2716 INT nListHeight = infoPtr->rcList.bottom - infoPtr->rcList.top;
2718 *lpPos = infoPtr->currIconPos;
2720 infoPtr->currIconPos.y += infoPtr->nItemHeight;
2721 if (infoPtr->currIconPos.y + infoPtr->nItemHeight <= nListHeight) return;
2723 infoPtr->currIconPos.x += infoPtr->nItemWidth;
2724 infoPtr->currIconPos.y = 0;
2728 /***
2729 * DESCRIPTION:
2730 * Moves an icon to the specified position.
2731 * It takes care of invalidating the item, etc.
2733 * PARAMETER(S):
2734 * [I] infoPtr : valid pointer to the listview structure
2735 * [I] nItem : the item to move
2736 * [I] lpPos : the new icon position
2737 * [I] isNew : flags the item as being new
2739 * RETURN:
2740 * Success: TRUE
2741 * Failure: FALSE
2743 static BOOL LISTVIEW_MoveIconTo(const LISTVIEW_INFO *infoPtr, INT nItem, const POINT *lppt, BOOL isNew)
2745 POINT old;
2747 if (!isNew)
2749 old.x = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosX, nItem);
2750 old.y = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosY, nItem);
2752 if (lppt->x == old.x && lppt->y == old.y) return TRUE;
2753 LISTVIEW_InvalidateItem(infoPtr, nItem);
2756 /* Allocating a POINTER for every item is too resource intensive,
2757 * so we'll keep the (x,y) in different arrays */
2758 if (!DPA_SetPtr(infoPtr->hdpaPosX, nItem, (void *)(LONG_PTR)lppt->x)) return FALSE;
2759 if (!DPA_SetPtr(infoPtr->hdpaPosY, nItem, (void *)(LONG_PTR)lppt->y)) return FALSE;
2761 LISTVIEW_InvalidateItem(infoPtr, nItem);
2763 return TRUE;
2766 /***
2767 * DESCRIPTION:
2768 * Arranges listview items in icon display mode.
2770 * PARAMETER(S):
2771 * [I] infoPtr : valid pointer to the listview structure
2772 * [I] nAlignCode : alignment code
2774 * RETURN:
2775 * SUCCESS : TRUE
2776 * FAILURE : FALSE
2778 static BOOL LISTVIEW_Arrange(LISTVIEW_INFO *infoPtr, INT nAlignCode)
2780 void (*next_pos)(LISTVIEW_INFO *, LPPOINT);
2781 POINT pos;
2782 INT i;
2784 if (infoPtr->uView != LV_VIEW_ICON && infoPtr->uView != LV_VIEW_SMALLICON) return FALSE;
2786 TRACE("nAlignCode=%d\n", nAlignCode);
2788 if (nAlignCode == LVA_DEFAULT)
2790 if (infoPtr->dwStyle & LVS_ALIGNLEFT) nAlignCode = LVA_ALIGNLEFT;
2791 else nAlignCode = LVA_ALIGNTOP;
2794 switch (nAlignCode)
2796 case LVA_ALIGNLEFT: next_pos = LISTVIEW_NextIconPosLeft; break;
2797 case LVA_ALIGNTOP: next_pos = LISTVIEW_NextIconPosTop; break;
2798 case LVA_SNAPTOGRID: next_pos = LISTVIEW_NextIconPosTop; break; /* FIXME */
2799 default: return FALSE;
2802 infoPtr->currIconPos.x = infoPtr->currIconPos.y = 0;
2803 for (i = 0; i < infoPtr->nItemCount; i++)
2805 next_pos(infoPtr, &pos);
2806 LISTVIEW_MoveIconTo(infoPtr, i, &pos, FALSE);
2809 return TRUE;
2812 /***
2813 * DESCRIPTION:
2814 * Retrieves the bounding rectangle of all the items, not offset by Origin.
2815 * For LVS_REPORT always returns empty rectangle.
2817 * PARAMETER(S):
2818 * [I] infoPtr : valid pointer to the listview structure
2819 * [O] lprcView : bounding rectangle
2821 * RETURN:
2822 * SUCCESS : TRUE
2823 * FAILURE : FALSE
2825 static void LISTVIEW_GetAreaRect(const LISTVIEW_INFO *infoPtr, LPRECT lprcView)
2827 INT i, x, y;
2829 SetRectEmpty(lprcView);
2831 switch (infoPtr->uView)
2833 case LV_VIEW_ICON:
2834 case LV_VIEW_SMALLICON:
2835 for (i = 0; i < infoPtr->nItemCount; i++)
2837 x = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosX, i);
2838 y = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosY, i);
2839 lprcView->right = max(lprcView->right, x);
2840 lprcView->bottom = max(lprcView->bottom, y);
2842 if (infoPtr->nItemCount > 0)
2844 lprcView->right += infoPtr->nItemWidth;
2845 lprcView->bottom += infoPtr->nItemHeight;
2847 break;
2849 case LV_VIEW_LIST:
2850 y = LISTVIEW_GetCountPerColumn(infoPtr);
2851 x = infoPtr->nItemCount / y;
2852 if (infoPtr->nItemCount % y) x++;
2853 lprcView->right = x * infoPtr->nItemWidth;
2854 lprcView->bottom = y * infoPtr->nItemHeight;
2855 break;
2859 /***
2860 * DESCRIPTION:
2861 * Retrieves the bounding rectangle of all the items.
2863 * PARAMETER(S):
2864 * [I] infoPtr : valid pointer to the listview structure
2865 * [O] lprcView : bounding rectangle
2867 * RETURN:
2868 * SUCCESS : TRUE
2869 * FAILURE : FALSE
2871 static BOOL LISTVIEW_GetViewRect(const LISTVIEW_INFO *infoPtr, LPRECT lprcView)
2873 POINT ptOrigin;
2875 TRACE("(lprcView=%p)\n", lprcView);
2877 if (!lprcView) return FALSE;
2879 LISTVIEW_GetAreaRect(infoPtr, lprcView);
2881 if (infoPtr->uView != LV_VIEW_DETAILS)
2883 LISTVIEW_GetOrigin(infoPtr, &ptOrigin);
2884 OffsetRect(lprcView, ptOrigin.x, ptOrigin.y);
2887 TRACE("lprcView=%s\n", wine_dbgstr_rect(lprcView));
2889 return TRUE;
2892 /***
2893 * DESCRIPTION:
2894 * Retrieves the subitem pointer associated with the subitem index.
2896 * PARAMETER(S):
2897 * [I] hdpaSubItems : DPA handle for a specific item
2898 * [I] nSubItem : index of subitem
2900 * RETURN:
2901 * SUCCESS : subitem pointer
2902 * FAILURE : NULL
2904 static SUBITEM_INFO* LISTVIEW_GetSubItemPtr(HDPA hdpaSubItems, INT nSubItem)
2906 SUBITEM_INFO *lpSubItem;
2907 INT i;
2909 /* we should binary search here if need be */
2910 for (i = 1; i < DPA_GetPtrCount(hdpaSubItems); i++)
2912 lpSubItem = DPA_GetPtr(hdpaSubItems, i);
2913 if (lpSubItem->iSubItem == nSubItem)
2914 return lpSubItem;
2917 return NULL;
2921 /***
2922 * DESCRIPTION:
2923 * Calculates the desired item width.
2925 * PARAMETER(S):
2926 * [I] infoPtr : valid pointer to the listview structure
2928 * RETURN:
2929 * The desired item width.
2931 static INT LISTVIEW_CalculateItemWidth(const LISTVIEW_INFO *infoPtr)
2933 INT nItemWidth = 0;
2935 TRACE("view %ld\n", infoPtr->uView);
2937 if (infoPtr->uView == LV_VIEW_ICON)
2938 nItemWidth = infoPtr->iconSpacing.cx;
2939 else if (infoPtr->uView == LV_VIEW_DETAILS)
2941 if (DPA_GetPtrCount(infoPtr->hdpaColumns) > 0)
2943 RECT rcHeader;
2944 INT index;
2946 index = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX,
2947 DPA_GetPtrCount(infoPtr->hdpaColumns) - 1, 0);
2949 LISTVIEW_GetHeaderRect(infoPtr, index, &rcHeader);
2950 nItemWidth = rcHeader.right;
2953 else /* LV_VIEW_SMALLICON, or LV_VIEW_LIST */
2955 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
2956 LVITEMW lvItem;
2957 INT i;
2959 lvItem.mask = LVIF_TEXT;
2960 lvItem.iSubItem = 0;
2962 for (i = 0; i < infoPtr->nItemCount; i++)
2964 lvItem.iItem = i;
2965 lvItem.pszText = szDispText;
2966 lvItem.cchTextMax = DISP_TEXT_SIZE;
2967 if (LISTVIEW_GetItemW(infoPtr, &lvItem))
2968 nItemWidth = max(LISTVIEW_GetStringWidthT(infoPtr, lvItem.pszText, TRUE),
2969 nItemWidth);
2972 if (infoPtr->himlSmall) nItemWidth += infoPtr->iconSize.cx;
2973 if (infoPtr->himlState) nItemWidth += infoPtr->iconStateSize.cx;
2975 nItemWidth = max(DEFAULT_COLUMN_WIDTH, nItemWidth + WIDTH_PADDING);
2978 return nItemWidth;
2981 /***
2982 * DESCRIPTION:
2983 * Calculates the desired item height.
2985 * PARAMETER(S):
2986 * [I] infoPtr : valid pointer to the listview structure
2988 * RETURN:
2989 * The desired item height.
2991 static INT LISTVIEW_CalculateItemHeight(const LISTVIEW_INFO *infoPtr)
2993 INT nItemHeight;
2995 TRACE("view %ld\n", infoPtr->uView);
2997 if (infoPtr->uView == LV_VIEW_ICON)
2998 nItemHeight = infoPtr->iconSpacing.cy;
2999 else
3001 nItemHeight = infoPtr->ntmHeight;
3002 if (infoPtr->himlState)
3003 nItemHeight = max(nItemHeight, infoPtr->iconStateSize.cy);
3004 if (infoPtr->himlSmall)
3005 nItemHeight = max(nItemHeight, infoPtr->iconSize.cy);
3006 nItemHeight += HEIGHT_PADDING;
3007 if (infoPtr->nMeasureItemHeight > 0)
3008 nItemHeight = infoPtr->nMeasureItemHeight;
3011 return max(nItemHeight, 1);
3014 /***
3015 * DESCRIPTION:
3016 * Updates the width, and height of an item.
3018 * PARAMETER(S):
3019 * [I] infoPtr : valid pointer to the listview structure
3021 * RETURN:
3022 * None.
3024 static inline void LISTVIEW_UpdateItemSize(LISTVIEW_INFO *infoPtr)
3026 infoPtr->nItemWidth = LISTVIEW_CalculateItemWidth(infoPtr);
3027 infoPtr->nItemHeight = LISTVIEW_CalculateItemHeight(infoPtr);
3031 /***
3032 * DESCRIPTION:
3033 * Retrieves and saves important text metrics info for the current
3034 * Listview font.
3036 * PARAMETER(S):
3037 * [I] infoPtr : valid pointer to the listview structure
3040 static void LISTVIEW_SaveTextMetrics(LISTVIEW_INFO *infoPtr)
3042 HDC hdc = GetDC(infoPtr->hwndSelf);
3043 HFONT hFont = infoPtr->hFont ? infoPtr->hFont : infoPtr->hDefaultFont;
3044 HFONT hOldFont = SelectObject(hdc, hFont);
3045 TEXTMETRICW tm;
3046 SIZE sz;
3048 if (GetTextMetricsW(hdc, &tm))
3050 infoPtr->ntmHeight = tm.tmHeight;
3051 infoPtr->ntmMaxCharWidth = tm.tmMaxCharWidth;
3054 if (GetTextExtentPoint32A(hdc, "...", 3, &sz))
3055 infoPtr->nEllipsisWidth = sz.cx;
3057 SelectObject(hdc, hOldFont);
3058 ReleaseDC(infoPtr->hwndSelf, hdc);
3060 TRACE("tmHeight=%d\n", infoPtr->ntmHeight);
3063 /***
3064 * DESCRIPTION:
3065 * A compare function for ranges
3067 * PARAMETER(S)
3068 * [I] range1 : pointer to range 1;
3069 * [I] range2 : pointer to range 2;
3070 * [I] flags : flags
3072 * RETURNS:
3073 * > 0 : if range 1 > range 2
3074 * < 0 : if range 2 > range 1
3075 * = 0 : if range intersects range 2
3077 static INT CALLBACK ranges_cmp(LPVOID range1, LPVOID range2, LPARAM flags)
3079 INT cmp;
3081 if (((RANGE*)range1)->upper <= ((RANGE*)range2)->lower)
3082 cmp = -1;
3083 else if (((RANGE*)range2)->upper <= ((RANGE*)range1)->lower)
3084 cmp = 1;
3085 else
3086 cmp = 0;
3088 TRACE("range1=%s, range2=%s, cmp=%d\n", debugrange(range1), debugrange(range2), cmp);
3090 return cmp;
3093 #define ranges_check(ranges, desc) if (TRACE_ON(listview)) ranges_assert(ranges, desc, __FILE__, __LINE__)
3095 static void ranges_assert(RANGES ranges, LPCSTR desc, const char *file, int line)
3097 INT i;
3098 RANGE *prev, *curr;
3100 TRACE("*** Checking %s:%d:%s ***\n", file, line, desc);
3101 assert (ranges);
3102 assert (DPA_GetPtrCount(ranges->hdpa) >= 0);
3103 ranges_dump(ranges);
3104 if (DPA_GetPtrCount(ranges->hdpa) > 0)
3106 prev = DPA_GetPtr(ranges->hdpa, 0);
3107 assert (prev->lower >= 0 && prev->lower < prev->upper);
3108 for (i = 1; i < DPA_GetPtrCount(ranges->hdpa); i++)
3110 curr = DPA_GetPtr(ranges->hdpa, i);
3111 assert (prev->upper <= curr->lower);
3112 assert (curr->lower < curr->upper);
3113 prev = curr;
3116 TRACE("--- Done checking---\n");
3119 static RANGES ranges_create(int count)
3121 RANGES ranges = Alloc(sizeof(struct tagRANGES));
3122 if (!ranges) return NULL;
3123 ranges->hdpa = DPA_Create(count);
3124 if (ranges->hdpa) return ranges;
3125 Free(ranges);
3126 return NULL;
3129 static void ranges_clear(RANGES ranges)
3131 INT i;
3133 for(i = 0; i < DPA_GetPtrCount(ranges->hdpa); i++)
3134 Free(DPA_GetPtr(ranges->hdpa, i));
3135 DPA_DeleteAllPtrs(ranges->hdpa);
3139 static void ranges_destroy(RANGES ranges)
3141 if (!ranges) return;
3142 ranges_clear(ranges);
3143 DPA_Destroy(ranges->hdpa);
3144 Free(ranges);
3147 static RANGES ranges_clone(RANGES ranges)
3149 RANGES clone;
3150 INT i;
3152 if (!(clone = ranges_create(DPA_GetPtrCount(ranges->hdpa)))) goto fail;
3154 for (i = 0; i < DPA_GetPtrCount(ranges->hdpa); i++)
3156 RANGE *newrng = Alloc(sizeof(RANGE));
3157 if (!newrng) goto fail;
3158 *newrng = *((RANGE*)DPA_GetPtr(ranges->hdpa, i));
3159 if (!DPA_SetPtr(clone->hdpa, i, newrng))
3161 Free(newrng);
3162 goto fail;
3165 return clone;
3167 fail:
3168 TRACE ("clone failed\n");
3169 ranges_destroy(clone);
3170 return NULL;
3173 static RANGES ranges_diff(RANGES ranges, RANGES sub)
3175 INT i;
3177 for (i = 0; i < DPA_GetPtrCount(sub->hdpa); i++)
3178 ranges_del(ranges, *((RANGE *)DPA_GetPtr(sub->hdpa, i)));
3180 return ranges;
3183 static void ranges_dump(RANGES ranges)
3185 INT i;
3187 for (i = 0; i < DPA_GetPtrCount(ranges->hdpa); i++)
3188 TRACE(" %s\n", debugrange(DPA_GetPtr(ranges->hdpa, i)));
3191 static inline BOOL ranges_contain(RANGES ranges, INT nItem)
3193 RANGE srchrng = { nItem, nItem + 1 };
3195 TRACE("(nItem=%d)\n", nItem);
3196 ranges_check(ranges, "before contain");
3197 return DPA_Search(ranges->hdpa, &srchrng, 0, ranges_cmp, 0, DPAS_SORTED) != -1;
3200 static INT ranges_itemcount(RANGES ranges)
3202 INT i, count = 0;
3204 for (i = 0; i < DPA_GetPtrCount(ranges->hdpa); i++)
3206 RANGE *sel = DPA_GetPtr(ranges->hdpa, i);
3207 count += sel->upper - sel->lower;
3210 return count;
3213 static BOOL ranges_shift(RANGES ranges, INT nItem, INT delta, INT nUpper)
3215 RANGE srchrng = { nItem, nItem + 1 }, *chkrng;
3216 INT index;
3218 index = DPA_Search(ranges->hdpa, &srchrng, 0, ranges_cmp, 0, DPAS_SORTED | DPAS_INSERTAFTER);
3219 if (index == -1) return TRUE;
3221 for (; index < DPA_GetPtrCount(ranges->hdpa); index++)
3223 chkrng = DPA_GetPtr(ranges->hdpa, index);
3224 if (chkrng->lower >= nItem)
3225 chkrng->lower = max(min(chkrng->lower + delta, nUpper - 1), 0);
3226 if (chkrng->upper > nItem)
3227 chkrng->upper = max(min(chkrng->upper + delta, nUpper), 0);
3229 return TRUE;
3232 static BOOL ranges_add(RANGES ranges, RANGE range)
3234 RANGE srchrgn;
3235 INT index;
3237 TRACE("(%s)\n", debugrange(&range));
3238 ranges_check(ranges, "before add");
3240 /* try find overlapping regions first */
3241 srchrgn.lower = range.lower - 1;
3242 srchrgn.upper = range.upper + 1;
3243 index = DPA_Search(ranges->hdpa, &srchrgn, 0, ranges_cmp, 0, DPAS_SORTED);
3245 if (index == -1)
3247 RANGE *newrgn;
3249 TRACE("Adding new range\n");
3251 /* create the brand new range to insert */
3252 newrgn = Alloc(sizeof(RANGE));
3253 if(!newrgn) goto fail;
3254 *newrgn = range;
3256 /* figure out where to insert it */
3257 index = DPA_Search(ranges->hdpa, newrgn, 0, ranges_cmp, 0, DPAS_SORTED | DPAS_INSERTAFTER);
3258 TRACE("index=%d\n", index);
3259 if (index == -1) index = 0;
3261 /* and get it over with */
3262 if (DPA_InsertPtr(ranges->hdpa, index, newrgn) == -1)
3264 Free(newrgn);
3265 goto fail;
3268 else
3270 RANGE *chkrgn, *mrgrgn;
3271 INT fromindex, mergeindex;
3273 chkrgn = DPA_GetPtr(ranges->hdpa, index);
3274 TRACE("Merge with %s @%d\n", debugrange(chkrgn), index);
3276 chkrgn->lower = min(range.lower, chkrgn->lower);
3277 chkrgn->upper = max(range.upper, chkrgn->upper);
3279 TRACE("New range %s @%d\n", debugrange(chkrgn), index);
3281 /* merge now common ranges */
3282 fromindex = 0;
3283 srchrgn.lower = chkrgn->lower - 1;
3284 srchrgn.upper = chkrgn->upper + 1;
3288 mergeindex = DPA_Search(ranges->hdpa, &srchrgn, fromindex, ranges_cmp, 0, 0);
3289 if (mergeindex == -1) break;
3290 if (mergeindex == index)
3292 fromindex = index + 1;
3293 continue;
3296 TRACE("Merge with index %i\n", mergeindex);
3298 mrgrgn = DPA_GetPtr(ranges->hdpa, mergeindex);
3299 chkrgn->lower = min(chkrgn->lower, mrgrgn->lower);
3300 chkrgn->upper = max(chkrgn->upper, mrgrgn->upper);
3301 Free(mrgrgn);
3302 DPA_DeletePtr(ranges->hdpa, mergeindex);
3303 if (mergeindex < index) index --;
3304 } while(1);
3307 ranges_check(ranges, "after add");
3308 return TRUE;
3310 fail:
3311 ranges_check(ranges, "failed add");
3312 return FALSE;
3315 static BOOL ranges_del(RANGES ranges, RANGE range)
3317 RANGE *chkrgn;
3318 INT index;
3320 TRACE("(%s)\n", debugrange(&range));
3321 ranges_check(ranges, "before del");
3323 /* we don't use DPAS_SORTED here, since we need *
3324 * to find the first overlapping range */
3325 index = DPA_Search(ranges->hdpa, &range, 0, ranges_cmp, 0, 0);
3326 while(index != -1)
3328 chkrgn = DPA_GetPtr(ranges->hdpa, index);
3330 TRACE("Matches range %s @%d\n", debugrange(chkrgn), index);
3332 /* case 1: Same range */
3333 if ( (chkrgn->upper == range.upper) &&
3334 (chkrgn->lower == range.lower) )
3336 DPA_DeletePtr(ranges->hdpa, index);
3337 Free(chkrgn);
3338 break;
3340 /* case 2: engulf */
3341 else if ( (chkrgn->upper <= range.upper) &&
3342 (chkrgn->lower >= range.lower) )
3344 DPA_DeletePtr(ranges->hdpa, index);
3345 Free(chkrgn);
3347 /* case 3: overlap upper */
3348 else if ( (chkrgn->upper <= range.upper) &&
3349 (chkrgn->lower < range.lower) )
3351 chkrgn->upper = range.lower;
3353 /* case 4: overlap lower */
3354 else if ( (chkrgn->upper > range.upper) &&
3355 (chkrgn->lower >= range.lower) )
3357 chkrgn->lower = range.upper;
3358 break;
3360 /* case 5: fully internal */
3361 else
3363 RANGE *newrgn;
3365 if (!(newrgn = Alloc(sizeof(RANGE)))) goto fail;
3366 newrgn->lower = chkrgn->lower;
3367 newrgn->upper = range.lower;
3368 chkrgn->lower = range.upper;
3369 if (DPA_InsertPtr(ranges->hdpa, index, newrgn) == -1)
3371 Free(newrgn);
3372 goto fail;
3374 break;
3377 index = DPA_Search(ranges->hdpa, &range, index, ranges_cmp, 0, 0);
3380 ranges_check(ranges, "after del");
3381 return TRUE;
3383 fail:
3384 ranges_check(ranges, "failed del");
3385 return FALSE;
3388 /***
3389 * DESCRIPTION:
3390 * Removes all selection ranges
3392 * Parameters(s):
3393 * [I] infoPtr : valid pointer to the listview structure
3394 * [I] toSkip : item range to skip removing the selection
3396 * RETURNS:
3397 * SUCCESS : TRUE
3398 * FAILURE : FALSE
3400 static BOOL LISTVIEW_DeselectAllSkipItems(LISTVIEW_INFO *infoPtr, RANGES toSkip)
3402 LVITEMW lvItem;
3403 ITERATOR i;
3404 RANGES clone;
3406 TRACE("()\n");
3408 lvItem.state = 0;
3409 lvItem.stateMask = LVIS_SELECTED;
3411 /* Only send one deselect all (-1) notification for LVS_OWNERDATA style */
3412 if (infoPtr->dwStyle & LVS_OWNERDATA)
3414 LISTVIEW_SetItemState(infoPtr, -1, &lvItem);
3415 return TRUE;
3418 /* need to clone the DPA because callbacks can change it */
3419 if (!(clone = ranges_clone(infoPtr->selectionRanges))) return FALSE;
3420 iterator_rangesitems(&i, ranges_diff(clone, toSkip));
3421 while(iterator_next(&i))
3422 LISTVIEW_SetItemState(infoPtr, i.nItem, &lvItem);
3423 /* note that the iterator destructor will free the cloned range */
3424 iterator_destroy(&i);
3426 return TRUE;
3429 static inline BOOL LISTVIEW_DeselectAllSkipItem(LISTVIEW_INFO *infoPtr, INT nItem)
3431 RANGES toSkip;
3433 if (!(toSkip = ranges_create(1))) return FALSE;
3434 if (nItem != -1) ranges_additem(toSkip, nItem);
3435 LISTVIEW_DeselectAllSkipItems(infoPtr, toSkip);
3436 ranges_destroy(toSkip);
3437 return TRUE;
3440 static inline BOOL LISTVIEW_DeselectAll(LISTVIEW_INFO *infoPtr)
3442 return LISTVIEW_DeselectAllSkipItem(infoPtr, -1);
3445 /***
3446 * DESCRIPTION:
3447 * Retrieves the number of items that are marked as selected.
3449 * PARAMETER(S):
3450 * [I] infoPtr : valid pointer to the listview structure
3452 * RETURN:
3453 * Number of items selected.
3455 static INT LISTVIEW_GetSelectedCount(const LISTVIEW_INFO *infoPtr)
3457 INT nSelectedCount = 0;
3459 if (infoPtr->uCallbackMask & LVIS_SELECTED)
3461 INT i;
3462 for (i = 0; i < infoPtr->nItemCount; i++)
3464 if (LISTVIEW_GetItemState(infoPtr, i, LVIS_SELECTED))
3465 nSelectedCount++;
3468 else
3469 nSelectedCount = ranges_itemcount(infoPtr->selectionRanges);
3471 TRACE("nSelectedCount=%d\n", nSelectedCount);
3472 return nSelectedCount;
3475 /***
3476 * DESCRIPTION:
3477 * Manages the item focus.
3479 * PARAMETER(S):
3480 * [I] infoPtr : valid pointer to the listview structure
3481 * [I] nItem : item index
3483 * RETURN:
3484 * TRUE : focused item changed
3485 * FALSE : focused item has NOT changed
3487 static inline BOOL LISTVIEW_SetItemFocus(LISTVIEW_INFO *infoPtr, INT nItem)
3489 INT oldFocus = infoPtr->nFocusedItem;
3490 LVITEMW lvItem;
3492 if (nItem == infoPtr->nFocusedItem) return FALSE;
3494 lvItem.state = nItem == -1 ? 0 : LVIS_FOCUSED;
3495 lvItem.stateMask = LVIS_FOCUSED;
3496 LISTVIEW_SetItemState(infoPtr, nItem == -1 ? infoPtr->nFocusedItem : nItem, &lvItem);
3498 return oldFocus != infoPtr->nFocusedItem;
3501 static INT shift_item(const LISTVIEW_INFO *infoPtr, INT nShiftItem, INT nItem, INT direction)
3503 if (nShiftItem < nItem) return nShiftItem;
3505 if (nShiftItem > nItem) return nShiftItem + direction;
3507 if (direction > 0) return nShiftItem + direction;
3509 return min(nShiftItem, infoPtr->nItemCount - 1);
3512 /* This function updates focus index.
3514 Parameters:
3515 focus : current focus index
3516 item : index of item to be added/removed
3517 direction : add/remove flag
3519 static void LISTVIEW_ShiftFocus(LISTVIEW_INFO *infoPtr, INT focus, INT item, INT direction)
3521 DWORD old_mask = infoPtr->notify_mask & NOTIFY_MASK_ITEM_CHANGE;
3523 infoPtr->notify_mask &= ~NOTIFY_MASK_ITEM_CHANGE;
3524 focus = shift_item(infoPtr, focus, item, direction);
3525 if (focus != infoPtr->nFocusedItem)
3526 LISTVIEW_SetItemFocus(infoPtr, focus);
3527 infoPtr->notify_mask |= old_mask;
3531 * DESCRIPTION:
3532 * Updates the various indices after an item has been inserted or deleted.
3534 * PARAMETER(S):
3535 * [I] infoPtr : valid pointer to the listview structure
3536 * [I] nItem : item index
3537 * [I] direction : Direction of shift, +1 or -1.
3539 * RETURN:
3540 * None
3542 static void LISTVIEW_ShiftIndices(LISTVIEW_INFO *infoPtr, INT nItem, INT direction)
3544 TRACE("Shifting %i, %i steps\n", nItem, direction);
3546 ranges_shift(infoPtr->selectionRanges, nItem, direction, infoPtr->nItemCount);
3547 assert(abs(direction) == 1);
3548 infoPtr->nSelectionMark = shift_item(infoPtr, infoPtr->nSelectionMark, nItem, direction);
3550 /* But we are not supposed to modify nHotItem! */
3554 * DESCRIPTION:
3555 * Adds a block of selections.
3557 * PARAMETER(S):
3558 * [I] infoPtr : valid pointer to the listview structure
3559 * [I] nItem : item index
3561 * RETURN:
3562 * Whether the window is still valid.
3564 static BOOL LISTVIEW_AddGroupSelection(LISTVIEW_INFO *infoPtr, INT nItem)
3566 INT nFirst = min(infoPtr->nSelectionMark, nItem);
3567 INT nLast = max(infoPtr->nSelectionMark, nItem);
3568 HWND hwndSelf = infoPtr->hwndSelf;
3569 DWORD old_mask;
3570 LVITEMW item;
3571 INT i;
3573 /* Temporarily disable change notification
3574 * If the control is LVS_OWNERDATA, we need to send
3575 * only one LVN_ODSTATECHANGED notification.
3576 * See MSDN documentation for LVN_ITEMCHANGED.
3578 old_mask = infoPtr->notify_mask & NOTIFY_MASK_ITEM_CHANGE;
3579 if (infoPtr->dwStyle & LVS_OWNERDATA)
3580 infoPtr->notify_mask &= ~NOTIFY_MASK_ITEM_CHANGE;
3582 if (nFirst == -1) nFirst = nItem;
3584 item.state = LVIS_SELECTED;
3585 item.stateMask = LVIS_SELECTED;
3587 for (i = nFirst; i <= nLast; i++)
3588 LISTVIEW_SetItemState(infoPtr,i,&item);
3590 if (infoPtr->dwStyle & LVS_OWNERDATA)
3591 LISTVIEW_SetOwnerDataState(infoPtr, nFirst, nLast, &item);
3593 if (!IsWindow(hwndSelf))
3594 return FALSE;
3595 infoPtr->notify_mask |= old_mask;
3596 return TRUE;
3600 /***
3601 * DESCRIPTION:
3602 * Sets a single group selection.
3604 * PARAMETER(S):
3605 * [I] infoPtr : valid pointer to the listview structure
3606 * [I] nItem : item index
3608 * RETURN:
3609 * None
3611 static void LISTVIEW_SetGroupSelection(LISTVIEW_INFO *infoPtr, INT nItem)
3613 INT nFirst = -1, nLast = -1;
3614 RANGES selection;
3615 DWORD old_mask;
3616 LVITEMW item;
3617 ITERATOR i;
3619 if (!(selection = ranges_create(100))) return;
3621 item.state = LVIS_SELECTED;
3622 item.stateMask = LVIS_SELECTED;
3624 if ((infoPtr->uView == LV_VIEW_LIST) || (infoPtr->uView == LV_VIEW_DETAILS))
3626 if (infoPtr->nSelectionMark == -1)
3628 infoPtr->nSelectionMark = nItem;
3629 ranges_additem(selection, nItem);
3631 else
3633 RANGE sel;
3635 sel.lower = min(infoPtr->nSelectionMark, nItem);
3636 sel.upper = max(infoPtr->nSelectionMark, nItem) + 1;
3637 ranges_add(selection, sel);
3640 else
3642 RECT rcItem, rcSel, rcSelMark;
3643 POINT ptItem;
3645 rcItem.left = LVIR_BOUNDS;
3646 if (!LISTVIEW_GetItemRect(infoPtr, nItem, &rcItem)) {
3647 ranges_destroy (selection);
3648 return;
3650 rcSelMark.left = LVIR_BOUNDS;
3651 if (!LISTVIEW_GetItemRect(infoPtr, infoPtr->nSelectionMark, &rcSelMark)) {
3652 ranges_destroy (selection);
3653 return;
3655 UnionRect(&rcSel, &rcItem, &rcSelMark);
3656 iterator_frameditems(&i, infoPtr, &rcSel);
3657 while(iterator_next(&i))
3659 LISTVIEW_GetItemPosition(infoPtr, i.nItem, &ptItem);
3660 if (PtInRect(&rcSel, ptItem)) ranges_additem(selection, i.nItem);
3662 iterator_destroy(&i);
3665 /* Disable per item notifications on LVS_OWNERDATA style */
3666 old_mask = infoPtr->notify_mask & NOTIFY_MASK_ITEM_CHANGE;
3667 if (infoPtr->dwStyle & LVS_OWNERDATA)
3668 infoPtr->notify_mask &= ~NOTIFY_MASK_ITEM_CHANGE;
3670 LISTVIEW_DeselectAllSkipItems(infoPtr, selection);
3672 iterator_rangesitems(&i, selection);
3673 while(iterator_next(&i))
3675 /* Find the range for LVN_ODSTATECHANGED */
3676 if (nFirst == -1)
3677 nFirst = i.nItem;
3678 nLast = i.nItem;
3679 LISTVIEW_SetItemState(infoPtr, i.nItem, &item);
3681 /* this will also destroy the selection */
3682 iterator_destroy(&i);
3684 if (infoPtr->dwStyle & LVS_OWNERDATA)
3685 LISTVIEW_SetOwnerDataState(infoPtr, nFirst, nLast, &item);
3687 infoPtr->notify_mask |= old_mask;
3688 LISTVIEW_SetItemFocus(infoPtr, nItem);
3691 /***
3692 * DESCRIPTION:
3693 * Sets a single selection.
3695 * PARAMETER(S):
3696 * [I] infoPtr : valid pointer to the listview structure
3697 * [I] nItem : item index
3699 * RETURN:
3700 * None
3702 static void LISTVIEW_SetSelection(LISTVIEW_INFO *infoPtr, INT nItem)
3704 LVITEMW lvItem;
3706 TRACE("nItem=%d\n", nItem);
3708 LISTVIEW_DeselectAllSkipItem(infoPtr, nItem);
3710 lvItem.state = LVIS_FOCUSED | LVIS_SELECTED;
3711 lvItem.stateMask = LVIS_FOCUSED | LVIS_SELECTED;
3712 LISTVIEW_SetItemState(infoPtr, nItem, &lvItem);
3714 infoPtr->nSelectionMark = nItem;
3717 /***
3718 * DESCRIPTION:
3719 * Set selection(s) with keyboard.
3721 * PARAMETER(S):
3722 * [I] infoPtr : valid pointer to the listview structure
3723 * [I] nItem : item index
3724 * [I] space : VK_SPACE code sent
3726 * RETURN:
3727 * SUCCESS : TRUE (needs to be repainted)
3728 * FAILURE : FALSE (nothing has changed)
3730 static BOOL LISTVIEW_KeySelection(LISTVIEW_INFO *infoPtr, INT nItem, BOOL space)
3732 /* FIXME: pass in the state */
3733 WORD wShift = GetKeyState(VK_SHIFT) & 0x8000;
3734 WORD wCtrl = GetKeyState(VK_CONTROL) & 0x8000;
3735 BOOL bResult = FALSE;
3737 TRACE("nItem=%d, wShift=%d, wCtrl=%d\n", nItem, wShift, wCtrl);
3738 if ((nItem >= 0) && (nItem < infoPtr->nItemCount))
3740 bResult = TRUE;
3742 if (infoPtr->dwStyle & LVS_SINGLESEL || (wShift == 0 && wCtrl == 0))
3743 LISTVIEW_SetSelection(infoPtr, nItem);
3744 else
3746 if (wShift)
3747 LISTVIEW_SetGroupSelection(infoPtr, nItem);
3748 else if (wCtrl)
3750 LVITEMW lvItem;
3751 lvItem.state = ~LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED);
3752 lvItem.stateMask = LVIS_SELECTED;
3753 if (space)
3755 LISTVIEW_SetItemState(infoPtr, nItem, &lvItem);
3756 if (lvItem.state & LVIS_SELECTED)
3757 infoPtr->nSelectionMark = nItem;
3759 bResult = LISTVIEW_SetItemFocus(infoPtr, nItem);
3762 LISTVIEW_EnsureVisible(infoPtr, nItem, FALSE);
3765 UpdateWindow(infoPtr->hwndSelf); /* update client area */
3766 return bResult;
3769 static BOOL LISTVIEW_GetItemAtPt(const LISTVIEW_INFO *infoPtr, LPLVITEMW lpLVItem, POINT pt)
3771 LVHITTESTINFO lvHitTestInfo;
3773 ZeroMemory(&lvHitTestInfo, sizeof(lvHitTestInfo));
3774 lvHitTestInfo.pt.x = pt.x;
3775 lvHitTestInfo.pt.y = pt.y;
3777 LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, FALSE);
3779 lpLVItem->mask = LVIF_PARAM;
3780 lpLVItem->iItem = lvHitTestInfo.iItem;
3781 lpLVItem->iSubItem = 0;
3783 return LISTVIEW_GetItemT(infoPtr, lpLVItem, TRUE);
3786 static inline BOOL LISTVIEW_IsHotTracking(const LISTVIEW_INFO *infoPtr)
3788 return ((infoPtr->dwLvExStyle & LVS_EX_TRACKSELECT) ||
3789 (infoPtr->dwLvExStyle & LVS_EX_ONECLICKACTIVATE) ||
3790 (infoPtr->dwLvExStyle & LVS_EX_TWOCLICKACTIVATE));
3793 /***
3794 * DESCRIPTION:
3795 * Called when the mouse is being actively tracked and has hovered for a specified
3796 * amount of time
3798 * PARAMETER(S):
3799 * [I] infoPtr : valid pointer to the listview structure
3800 * [I] fwKeys : key indicator
3801 * [I] x,y : mouse position
3803 * RETURN:
3804 * 0 if the message was processed, non-zero if there was an error
3806 * INFO:
3807 * LVS_EX_TRACKSELECT: An item is automatically selected when the cursor remains
3808 * over the item for a certain period of time.
3811 static LRESULT LISTVIEW_MouseHover(LISTVIEW_INFO *infoPtr, INT x, INT y)
3813 NMHDR hdr;
3815 if (notify_hdr(infoPtr, NM_HOVER, &hdr)) return 0;
3817 if (LISTVIEW_IsHotTracking(infoPtr))
3819 LVITEMW item;
3820 POINT pt;
3822 pt.x = x;
3823 pt.y = y;
3825 if (LISTVIEW_GetItemAtPt(infoPtr, &item, pt))
3826 LISTVIEW_SetSelection(infoPtr, item.iItem);
3828 SetFocus(infoPtr->hwndSelf);
3831 return 0;
3834 #define SCROLL_LEFT 0x1
3835 #define SCROLL_RIGHT 0x2
3836 #define SCROLL_UP 0x4
3837 #define SCROLL_DOWN 0x8
3839 /***
3840 * DESCRIPTION:
3841 * Utility routine to draw and highlight items within a marquee selection rectangle.
3843 * PARAMETER(S):
3844 * [I] infoPtr : valid pointer to the listview structure
3845 * [I] coords_orig : original co-ordinates of the cursor
3846 * [I] coords_offs : offsetted coordinates of the cursor
3847 * [I] offset : offset amount
3848 * [I] scroll : Bitmask of which directions we should scroll, if at all
3850 * RETURN:
3851 * None.
3853 static void LISTVIEW_MarqueeHighlight(LISTVIEW_INFO *infoPtr, const POINT *coords_orig,
3854 INT scroll)
3856 BOOL controlDown = FALSE;
3857 LVITEMW item;
3858 ITERATOR old_elems, new_elems;
3859 RECT rect;
3860 POINT coords_offs, offset;
3862 /* Ensure coordinates are within client bounds */
3863 coords_offs.x = max(min(coords_orig->x, infoPtr->rcList.right), 0);
3864 coords_offs.y = max(min(coords_orig->y, infoPtr->rcList.bottom), 0);
3866 /* Get offset */
3867 LISTVIEW_GetOrigin(infoPtr, &offset);
3869 /* Offset coordinates by the appropriate amount */
3870 coords_offs.x -= offset.x;
3871 coords_offs.y -= offset.y;
3873 if (coords_offs.x > infoPtr->marqueeOrigin.x)
3875 rect.left = infoPtr->marqueeOrigin.x;
3876 rect.right = coords_offs.x;
3878 else
3880 rect.left = coords_offs.x;
3881 rect.right = infoPtr->marqueeOrigin.x;
3884 if (coords_offs.y > infoPtr->marqueeOrigin.y)
3886 rect.top = infoPtr->marqueeOrigin.y;
3887 rect.bottom = coords_offs.y;
3889 else
3891 rect.top = coords_offs.y;
3892 rect.bottom = infoPtr->marqueeOrigin.y;
3895 /* Cancel out the old marquee rectangle and draw the new one */
3896 LISTVIEW_InvalidateRect(infoPtr, &infoPtr->marqueeDrawRect);
3898 /* Scroll by the appropriate distance if applicable - speed up scrolling as
3899 the cursor is further away */
3901 if ((scroll & SCROLL_LEFT) && (coords_orig->x <= 0))
3902 LISTVIEW_Scroll(infoPtr, coords_orig->x, 0);
3904 if ((scroll & SCROLL_RIGHT) && (coords_orig->x >= infoPtr->rcList.right))
3905 LISTVIEW_Scroll(infoPtr, (coords_orig->x - infoPtr->rcList.right), 0);
3907 if ((scroll & SCROLL_UP) && (coords_orig->y <= 0))
3908 LISTVIEW_Scroll(infoPtr, 0, coords_orig->y);
3910 if ((scroll & SCROLL_DOWN) && (coords_orig->y >= infoPtr->rcList.bottom))
3911 LISTVIEW_Scroll(infoPtr, 0, (coords_orig->y - infoPtr->rcList.bottom));
3913 iterator_frameditems_absolute(&old_elems, infoPtr, &infoPtr->marqueeRect);
3915 infoPtr->marqueeRect = rect;
3916 infoPtr->marqueeDrawRect = rect;
3917 OffsetRect(&infoPtr->marqueeDrawRect, offset.x, offset.y);
3919 iterator_frameditems_absolute(&new_elems, infoPtr, &infoPtr->marqueeRect);
3920 iterator_remove_common_items(&old_elems, &new_elems);
3922 /* Iterate over no longer selected items */
3923 while (iterator_next(&old_elems))
3925 if (old_elems.nItem > -1)
3927 if (LISTVIEW_GetItemState(infoPtr, old_elems.nItem, LVIS_SELECTED) == LVIS_SELECTED)
3928 item.state = 0;
3929 else
3930 item.state = LVIS_SELECTED;
3932 item.stateMask = LVIS_SELECTED;
3934 LISTVIEW_SetItemState(infoPtr, old_elems.nItem, &item);
3937 iterator_destroy(&old_elems);
3940 /* Iterate over newly selected items */
3941 if (GetKeyState(VK_CONTROL) & 0x8000)
3942 controlDown = TRUE;
3944 while (iterator_next(&new_elems))
3946 if (new_elems.nItem > -1)
3948 /* If CTRL is pressed, invert. If not, always select the item. */
3949 if ((controlDown) && (LISTVIEW_GetItemState(infoPtr, new_elems.nItem, LVIS_SELECTED)))
3950 item.state = 0;
3951 else
3952 item.state = LVIS_SELECTED;
3954 item.stateMask = LVIS_SELECTED;
3956 LISTVIEW_SetItemState(infoPtr, new_elems.nItem, &item);
3959 iterator_destroy(&new_elems);
3961 LISTVIEW_InvalidateRect(infoPtr, &infoPtr->marqueeDrawRect);
3964 /***
3965 * DESCRIPTION:
3966 * Called when we are in a marquee selection that involves scrolling the listview (ie,
3967 * the cursor is outside the bounds of the client area). This is a TIMERPROC.
3969 * PARAMETER(S):
3970 * [I] hwnd : Handle to the listview
3971 * [I] uMsg : WM_TIMER (ignored)
3972 * [I] idEvent : The timer ID interpreted as a pointer to a LISTVIEW_INFO struct
3973 * [I] dwTimer : The elapsed time (ignored)
3975 * RETURN:
3976 * None.
3978 static VOID CALLBACK LISTVIEW_ScrollTimer(HWND hWnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime)
3980 LISTVIEW_INFO *infoPtr;
3981 SCROLLINFO scrollInfo;
3982 POINT coords;
3983 INT scroll = 0;
3985 infoPtr = (LISTVIEW_INFO *) idEvent;
3987 if (!infoPtr)
3988 return;
3990 /* Get the current cursor position and convert to client coordinates */
3991 GetCursorPos(&coords);
3992 ScreenToClient(hWnd, &coords);
3994 scrollInfo.cbSize = sizeof(SCROLLINFO);
3995 scrollInfo.fMask = SIF_ALL;
3997 /* Work out in which directions we can scroll */
3998 if (GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo))
4000 if (scrollInfo.nPos != scrollInfo.nMin)
4001 scroll |= SCROLL_UP;
4003 if (((scrollInfo.nPage + scrollInfo.nPos) - 1) != scrollInfo.nMax)
4004 scroll |= SCROLL_DOWN;
4007 if (GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo))
4009 if (scrollInfo.nPos != scrollInfo.nMin)
4010 scroll |= SCROLL_LEFT;
4012 if (((scrollInfo.nPage + scrollInfo.nPos) - 1) != scrollInfo.nMax)
4013 scroll |= SCROLL_RIGHT;
4016 if (((coords.x <= 0) && (scroll & SCROLL_LEFT)) ||
4017 ((coords.y <= 0) && (scroll & SCROLL_UP)) ||
4018 ((coords.x >= infoPtr->rcList.right) && (scroll & SCROLL_RIGHT)) ||
4019 ((coords.y >= infoPtr->rcList.bottom) && (scroll & SCROLL_DOWN)))
4021 LISTVIEW_MarqueeHighlight(infoPtr, &coords, scroll);
4025 /***
4026 * DESCRIPTION:
4027 * Called whenever WM_MOUSEMOVE is received.
4029 * PARAMETER(S):
4030 * [I] infoPtr : valid pointer to the listview structure
4031 * [I] fwKeys : key indicator
4032 * [I] x,y : mouse position
4034 * RETURN:
4035 * 0 if the message is processed, non-zero if there was an error
4037 static LRESULT LISTVIEW_MouseMove(LISTVIEW_INFO *infoPtr, WORD fwKeys, INT x, INT y)
4039 LVHITTESTINFO ht;
4040 RECT rect;
4041 POINT pt;
4043 pt.x = x;
4044 pt.y = y;
4046 if (!(fwKeys & MK_LBUTTON))
4047 infoPtr->bLButtonDown = FALSE;
4049 if (infoPtr->bLButtonDown)
4051 rect.left = rect.right = infoPtr->ptClickPos.x;
4052 rect.top = rect.bottom = infoPtr->ptClickPos.y;
4054 InflateRect(&rect, GetSystemMetrics(SM_CXDRAG), GetSystemMetrics(SM_CYDRAG));
4056 if (infoPtr->bMarqueeSelect)
4058 /* Enable the timer if we're going outside our bounds, in case the user doesn't
4059 move the mouse again */
4061 if ((x <= 0) || (y <= 0) || (x >= infoPtr->rcList.right) ||
4062 (y >= infoPtr->rcList.bottom))
4064 if (!infoPtr->bScrolling)
4066 infoPtr->bScrolling = TRUE;
4067 SetTimer(infoPtr->hwndSelf, (UINT_PTR) infoPtr, 1, LISTVIEW_ScrollTimer);
4070 else
4072 infoPtr->bScrolling = FALSE;
4073 KillTimer(infoPtr->hwndSelf, (UINT_PTR) infoPtr);
4076 LISTVIEW_MarqueeHighlight(infoPtr, &pt, 0);
4077 return 0;
4080 ht.pt = pt;
4081 LISTVIEW_HitTest(infoPtr, &ht, TRUE, TRUE);
4083 /* reset item marker */
4084 if (infoPtr->nLButtonDownItem != ht.iItem)
4085 infoPtr->nLButtonDownItem = -1;
4087 if (!PtInRect(&rect, pt))
4089 /* this path covers the following:
4090 1. WM_LBUTTONDOWN over selected item (sets focus on it)
4091 2. change focus with keys
4092 3. move mouse over item from step 1 selects it and moves focus on it */
4093 if (infoPtr->nLButtonDownItem != -1 &&
4094 !LISTVIEW_GetItemState(infoPtr, infoPtr->nLButtonDownItem, LVIS_SELECTED))
4096 LVITEMW lvItem;
4098 lvItem.state = LVIS_FOCUSED | LVIS_SELECTED;
4099 lvItem.stateMask = LVIS_FOCUSED | LVIS_SELECTED;
4101 LISTVIEW_SetItemState(infoPtr, infoPtr->nLButtonDownItem, &lvItem);
4102 infoPtr->nLButtonDownItem = -1;
4105 if (!infoPtr->bDragging)
4107 ht.pt = infoPtr->ptClickPos;
4108 LISTVIEW_HitTest(infoPtr, &ht, TRUE, TRUE);
4110 /* If the click is outside the range of an item, begin a
4111 highlight. If not, begin an item drag. */
4112 if (ht.iItem == -1)
4114 NMHDR hdr;
4116 /* If we're allowing multiple selections, send notification.
4117 If return value is non-zero, cancel. */
4118 if (!(infoPtr->dwStyle & LVS_SINGLESEL) && (notify_hdr(infoPtr, LVN_MARQUEEBEGIN, &hdr) == 0))
4120 /* Store the absolute coordinates of the click */
4121 POINT offset;
4122 LISTVIEW_GetOrigin(infoPtr, &offset);
4124 infoPtr->marqueeOrigin.x = infoPtr->ptClickPos.x - offset.x;
4125 infoPtr->marqueeOrigin.y = infoPtr->ptClickPos.y - offset.y;
4127 /* Begin selection and capture mouse */
4128 infoPtr->bMarqueeSelect = TRUE;
4129 infoPtr->marqueeRect = rect;
4130 SetCapture(infoPtr->hwndSelf);
4133 else
4135 NMLISTVIEW nmlv;
4137 ZeroMemory(&nmlv, sizeof(nmlv));
4138 nmlv.iItem = ht.iItem;
4139 nmlv.ptAction = infoPtr->ptClickPos;
4141 notify_listview(infoPtr, LVN_BEGINDRAG, &nmlv);
4142 infoPtr->bDragging = TRUE;
4146 return 0;
4150 /* see if we are supposed to be tracking mouse hovering */
4151 if (LISTVIEW_IsHotTracking(infoPtr)) {
4152 TRACKMOUSEEVENT trackinfo;
4153 DWORD flags;
4155 trackinfo.cbSize = sizeof(TRACKMOUSEEVENT);
4156 trackinfo.dwFlags = TME_QUERY;
4158 /* see if we are already tracking this hwnd */
4159 _TrackMouseEvent(&trackinfo);
4161 flags = TME_LEAVE;
4162 if(infoPtr->dwLvExStyle & LVS_EX_TRACKSELECT)
4163 flags |= TME_HOVER;
4165 if((trackinfo.dwFlags & flags) != flags || trackinfo.hwndTrack != infoPtr->hwndSelf) {
4166 trackinfo.dwFlags = flags;
4167 trackinfo.dwHoverTime = infoPtr->dwHoverTime;
4168 trackinfo.hwndTrack = infoPtr->hwndSelf;
4170 /* call TRACKMOUSEEVENT so we receive WM_MOUSEHOVER messages */
4171 _TrackMouseEvent(&trackinfo);
4175 return 0;
4179 /***
4180 * Tests whether the item is assignable to a list with style lStyle
4182 static inline BOOL is_assignable_item(const LVITEMW *lpLVItem, LONG lStyle)
4184 if ( (lpLVItem->mask & LVIF_TEXT) &&
4185 (lpLVItem->pszText == LPSTR_TEXTCALLBACKW) &&
4186 (lStyle & (LVS_SORTASCENDING | LVS_SORTDESCENDING)) ) return FALSE;
4188 return TRUE;
4192 /***
4193 * DESCRIPTION:
4194 * Helper for LISTVIEW_SetItemT and LISTVIEW_InsertItemT: sets item attributes.
4196 * PARAMETER(S):
4197 * [I] infoPtr : valid pointer to the listview structure
4198 * [I] lpLVItem : valid pointer to new item attributes
4199 * [I] isNew : the item being set is being inserted
4200 * [I] isW : TRUE if lpLVItem is Unicode, FALSE if it's ANSI
4201 * [O] bChanged : will be set to TRUE if the item really changed
4203 * RETURN:
4204 * SUCCESS : TRUE
4205 * FAILURE : FALSE
4207 static BOOL set_main_item(LISTVIEW_INFO *infoPtr, const LVITEMW *lpLVItem, BOOL isNew, BOOL isW, BOOL *bChanged)
4209 ITEM_INFO *lpItem;
4210 NMLISTVIEW nmlv;
4211 UINT uChanged = 0;
4212 LVITEMW item;
4213 /* stateMask is ignored for LVM_INSERTITEM */
4214 UINT stateMask = isNew ? ~0 : lpLVItem->stateMask;
4215 BOOL send_change_notification;
4217 TRACE("()\n");
4219 assert(lpLVItem->iItem >= 0 && lpLVItem->iItem < infoPtr->nItemCount);
4221 if (lpLVItem->mask == 0) return TRUE;
4223 if (infoPtr->dwStyle & LVS_OWNERDATA)
4225 /* a virtual listview only stores selection and focus */
4226 if (lpLVItem->mask & ~LVIF_STATE)
4227 return FALSE;
4228 lpItem = NULL;
4230 else
4232 HDPA hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, lpLVItem->iItem);
4233 lpItem = DPA_GetPtr(hdpaSubItems, 0);
4234 assert (lpItem);
4237 /* we need to get the lParam and state of the item */
4238 item.iItem = lpLVItem->iItem;
4239 item.iSubItem = lpLVItem->iSubItem;
4240 item.mask = LVIF_STATE | LVIF_PARAM;
4241 item.stateMask = (infoPtr->dwStyle & LVS_OWNERDATA) ? LVIS_FOCUSED | LVIS_SELECTED : ~0;
4243 item.state = 0;
4244 item.lParam = 0;
4245 if (!isNew && !LISTVIEW_GetItemW(infoPtr, &item)) return FALSE;
4247 TRACE("oldState=%x, newState=%x\n", item.state, lpLVItem->state);
4248 /* determine what fields will change */
4249 if ((lpLVItem->mask & LVIF_STATE) && ((item.state ^ lpLVItem->state) & stateMask & ~infoPtr->uCallbackMask))
4250 uChanged |= LVIF_STATE;
4252 if ((lpLVItem->mask & LVIF_IMAGE) && (lpItem->hdr.iImage != lpLVItem->iImage))
4253 uChanged |= LVIF_IMAGE;
4255 if ((lpLVItem->mask & LVIF_PARAM) && (lpItem->lParam != lpLVItem->lParam))
4256 uChanged |= LVIF_PARAM;
4258 if ((lpLVItem->mask & LVIF_INDENT) && (lpItem->iIndent != lpLVItem->iIndent))
4259 uChanged |= LVIF_INDENT;
4261 if ((lpLVItem->mask & LVIF_TEXT) && textcmpWT(lpItem->hdr.pszText, lpLVItem->pszText, isW))
4262 uChanged |= LVIF_TEXT;
4264 TRACE("change mask=0x%x\n", uChanged);
4266 memset(&nmlv, 0, sizeof(NMLISTVIEW));
4267 nmlv.iItem = lpLVItem->iItem;
4268 if (lpLVItem->mask & LVIF_STATE)
4270 nmlv.uNewState = (item.state & ~stateMask) | (lpLVItem->state & stateMask);
4271 nmlv.uOldState = item.state;
4273 nmlv.uChanged = isNew ? LVIF_STATE : (uChanged ? uChanged : lpLVItem->mask);
4274 nmlv.lParam = item.lParam;
4276 /* Send change notification if the item is not being inserted, or inserted (selected|focused),
4277 and we are _NOT_ virtual (LVS_OWNERDATA), and change notifications
4278 are enabled. Even if nothing really changed we still need to send this,
4279 in this case uChanged mask is just set to passed item mask. */
4281 send_change_notification = !isNew;
4282 send_change_notification |= (uChanged & LVIF_STATE) && (lpLVItem->state & (LVIS_FOCUSED | LVIS_SELECTED));
4283 send_change_notification &= !!(infoPtr->notify_mask & NOTIFY_MASK_ITEM_CHANGE);
4285 if (lpItem && send_change_notification)
4287 HWND hwndSelf = infoPtr->hwndSelf;
4289 if (notify_listview(infoPtr, LVN_ITEMCHANGING, &nmlv))
4290 return FALSE;
4291 if (!IsWindow(hwndSelf))
4292 return FALSE;
4295 /* When item is inserted we need to shift existing focus index if new item has lower index. */
4296 if (isNew && (stateMask & ~infoPtr->uCallbackMask & LVIS_FOCUSED) &&
4297 /* this means we won't hit a focus change path later */
4298 ((uChanged & LVIF_STATE) == 0 || (!(lpLVItem->state & LVIS_FOCUSED) && (infoPtr->nFocusedItem != lpLVItem->iItem))))
4300 if (infoPtr->nFocusedItem != -1 && (lpLVItem->iItem <= infoPtr->nFocusedItem))
4301 infoPtr->nFocusedItem++;
4304 if (!uChanged) return TRUE;
4305 *bChanged = TRUE;
4307 /* copy information */
4308 if (lpLVItem->mask & LVIF_TEXT)
4309 textsetptrT(&lpItem->hdr.pszText, lpLVItem->pszText, isW);
4311 if (lpLVItem->mask & LVIF_IMAGE)
4312 lpItem->hdr.iImage = lpLVItem->iImage;
4314 if (lpLVItem->mask & LVIF_PARAM)
4315 lpItem->lParam = lpLVItem->lParam;
4317 if (lpLVItem->mask & LVIF_INDENT)
4318 lpItem->iIndent = lpLVItem->iIndent;
4320 if (uChanged & LVIF_STATE)
4322 if (lpItem && (stateMask & ~infoPtr->uCallbackMask))
4324 lpItem->state &= ~stateMask;
4325 lpItem->state |= (lpLVItem->state & stateMask);
4327 if (lpLVItem->state & stateMask & ~infoPtr->uCallbackMask & LVIS_SELECTED)
4329 if (infoPtr->dwStyle & LVS_SINGLESEL) LISTVIEW_DeselectAllSkipItem(infoPtr, lpLVItem->iItem);
4330 ranges_additem(infoPtr->selectionRanges, lpLVItem->iItem);
4332 else if (stateMask & LVIS_SELECTED)
4334 ranges_delitem(infoPtr->selectionRanges, lpLVItem->iItem);
4336 /* If we are asked to change focus, and we manage it, do it.
4337 It's important to have all new item data stored at this point,
4338 because changing existing focus could result in a redrawing operation,
4339 which in turn could ask for disp data, application should see all data
4340 for inserted item when processing LVN_GETDISPINFO.
4342 The way this works application will see nested item change notifications -
4343 changed item notifications interrupted by ones from item losing focus. */
4344 if (stateMask & ~infoPtr->uCallbackMask & LVIS_FOCUSED)
4346 if (lpLVItem->state & LVIS_FOCUSED)
4348 /* update selection mark */
4349 if (infoPtr->nFocusedItem == -1 && infoPtr->nSelectionMark == -1)
4350 infoPtr->nSelectionMark = lpLVItem->iItem;
4352 if (infoPtr->nFocusedItem != -1)
4354 /* remove current focus */
4355 item.mask = LVIF_STATE;
4356 item.state = 0;
4357 item.stateMask = LVIS_FOCUSED;
4359 /* recurse with redrawing an item */
4360 LISTVIEW_SetItemState(infoPtr, infoPtr->nFocusedItem, &item);
4363 infoPtr->nFocusedItem = lpLVItem->iItem;
4364 LISTVIEW_EnsureVisible(infoPtr, lpLVItem->iItem, infoPtr->uView == LV_VIEW_LIST);
4366 else if (infoPtr->nFocusedItem == lpLVItem->iItem)
4368 infoPtr->nFocusedItem = -1;
4373 if (send_change_notification)
4375 if (lpLVItem->mask & LVIF_PARAM) nmlv.lParam = lpLVItem->lParam;
4376 notify_listview(infoPtr, LVN_ITEMCHANGED, &nmlv);
4379 return TRUE;
4382 /***
4383 * DESCRIPTION:
4384 * Helper for LISTVIEW_{Set,Insert}ItemT *only*: sets subitem attributes.
4386 * PARAMETER(S):
4387 * [I] infoPtr : valid pointer to the listview structure
4388 * [I] lpLVItem : valid pointer to new subitem attributes
4389 * [I] isW : TRUE if lpLVItem is Unicode, FALSE if it's ANSI
4390 * [O] bChanged : will be set to TRUE if the item really changed
4392 * RETURN:
4393 * SUCCESS : TRUE
4394 * FAILURE : FALSE
4396 static BOOL set_sub_item(const LISTVIEW_INFO *infoPtr, const LVITEMW *lpLVItem, BOOL isW, BOOL *bChanged)
4398 HDPA hdpaSubItems;
4399 SUBITEM_INFO *lpSubItem;
4401 /* we do not support subitems for virtual listviews */
4402 if (infoPtr->dwStyle & LVS_OWNERDATA) return FALSE;
4404 /* set subitem only if column is present */
4405 if (lpLVItem->iSubItem >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return FALSE;
4407 /* First do some sanity checks */
4408 /* The LVIF_STATE flag is valid for subitems, but does not appear to be
4409 particularly useful. We currently do not actually do anything with
4410 the flag on subitems.
4412 if (lpLVItem->mask & ~(LVIF_TEXT | LVIF_IMAGE | LVIF_STATE | LVIF_DI_SETITEM)) return FALSE;
4413 if (!(lpLVItem->mask & (LVIF_TEXT | LVIF_IMAGE | LVIF_STATE))) return TRUE;
4415 /* get the subitem structure, and create it if not there */
4416 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, lpLVItem->iItem);
4417 assert (hdpaSubItems);
4419 lpSubItem = LISTVIEW_GetSubItemPtr(hdpaSubItems, lpLVItem->iSubItem);
4420 if (!lpSubItem)
4422 SUBITEM_INFO *tmpSubItem;
4423 INT i;
4425 lpSubItem = Alloc(sizeof(SUBITEM_INFO));
4426 if (!lpSubItem) return FALSE;
4427 /* we could binary search here, if need be...*/
4428 for (i = 1; i < DPA_GetPtrCount(hdpaSubItems); i++)
4430 tmpSubItem = DPA_GetPtr(hdpaSubItems, i);
4431 if (tmpSubItem->iSubItem > lpLVItem->iSubItem) break;
4433 if (DPA_InsertPtr(hdpaSubItems, i, lpSubItem) == -1)
4435 Free(lpSubItem);
4436 return FALSE;
4438 lpSubItem->iSubItem = lpLVItem->iSubItem;
4439 lpSubItem->hdr.iImage = I_IMAGECALLBACK;
4440 *bChanged = TRUE;
4443 if ((lpLVItem->mask & LVIF_IMAGE) && (lpSubItem->hdr.iImage != lpLVItem->iImage))
4445 lpSubItem->hdr.iImage = lpLVItem->iImage;
4446 *bChanged = TRUE;
4449 if ((lpLVItem->mask & LVIF_TEXT) && textcmpWT(lpSubItem->hdr.pszText, lpLVItem->pszText, isW))
4451 textsetptrT(&lpSubItem->hdr.pszText, lpLVItem->pszText, isW);
4452 *bChanged = TRUE;
4455 return TRUE;
4458 /***
4459 * DESCRIPTION:
4460 * Sets item attributes.
4462 * PARAMETER(S):
4463 * [I] infoPtr : valid pointer to the listview structure
4464 * [I] lpLVItem : new item attributes
4465 * [I] isW : TRUE if lpLVItem is Unicode, FALSE if it's ANSI
4467 * RETURN:
4468 * SUCCESS : TRUE
4469 * FAILURE : FALSE
4471 static BOOL LISTVIEW_SetItemT(LISTVIEW_INFO *infoPtr, LVITEMW *lpLVItem, BOOL isW)
4473 HWND hwndSelf = infoPtr->hwndSelf;
4474 LPWSTR pszText = NULL;
4475 BOOL bResult, bChanged = FALSE;
4476 RECT oldItemArea;
4478 TRACE("(lpLVItem=%s, isW=%d)\n", debuglvitem_t(lpLVItem, isW), isW);
4480 if (!lpLVItem || lpLVItem->iItem < 0 || lpLVItem->iItem >= infoPtr->nItemCount)
4481 return FALSE;
4483 /* Store old item area */
4484 LISTVIEW_GetItemBox(infoPtr, lpLVItem->iItem, &oldItemArea);
4486 /* For efficiency, we transform the lpLVItem->pszText to Unicode here */
4487 if ((lpLVItem->mask & LVIF_TEXT) && is_text(lpLVItem->pszText))
4489 pszText = lpLVItem->pszText;
4490 lpLVItem->pszText = textdupTtoW(lpLVItem->pszText, isW);
4493 /* actually set the fields */
4494 if (!is_assignable_item(lpLVItem, infoPtr->dwStyle)) return FALSE;
4496 if (lpLVItem->iSubItem)
4497 bResult = set_sub_item(infoPtr, lpLVItem, TRUE, &bChanged);
4498 else
4499 bResult = set_main_item(infoPtr, lpLVItem, FALSE, TRUE, &bChanged);
4500 if (!IsWindow(hwndSelf))
4501 return FALSE;
4503 /* redraw item, if necessary */
4504 if (bChanged && !infoPtr->bIsDrawing)
4506 /* this little optimization eliminates some nasty flicker */
4507 if ( infoPtr->uView == LV_VIEW_DETAILS && !(infoPtr->dwStyle & LVS_OWNERDRAWFIXED) &&
4508 !(infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT) &&
4509 lpLVItem->iSubItem > 0 && lpLVItem->iSubItem <= DPA_GetPtrCount(infoPtr->hdpaColumns) )
4510 LISTVIEW_InvalidateSubItem(infoPtr, lpLVItem->iItem, lpLVItem->iSubItem);
4511 else
4513 LISTVIEW_InvalidateRect(infoPtr, &oldItemArea);
4514 LISTVIEW_InvalidateItem(infoPtr, lpLVItem->iItem);
4517 /* restore text */
4518 if (pszText)
4520 textfreeT(lpLVItem->pszText, isW);
4521 lpLVItem->pszText = pszText;
4524 return bResult;
4527 /***
4528 * DESCRIPTION:
4529 * Retrieves the index of the item at coordinate (0, 0) of the client area.
4531 * PARAMETER(S):
4532 * [I] infoPtr : valid pointer to the listview structure
4534 * RETURN:
4535 * item index
4537 static INT LISTVIEW_GetTopIndex(const LISTVIEW_INFO *infoPtr)
4539 INT nItem = 0;
4540 SCROLLINFO scrollInfo;
4542 scrollInfo.cbSize = sizeof(SCROLLINFO);
4543 scrollInfo.fMask = SIF_POS;
4545 if (infoPtr->uView == LV_VIEW_LIST)
4547 if (GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo))
4548 nItem = scrollInfo.nPos * LISTVIEW_GetCountPerColumn(infoPtr);
4550 else if (infoPtr->uView == LV_VIEW_DETAILS)
4552 if (GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo))
4553 nItem = scrollInfo.nPos;
4555 else
4557 if (GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo))
4558 nItem = LISTVIEW_GetCountPerRow(infoPtr) * (scrollInfo.nPos / infoPtr->nItemHeight);
4561 TRACE("nItem=%d\n", nItem);
4563 return nItem;
4566 static void LISTVIEW_DrawBackgroundBitmap(const LISTVIEW_INFO *infoPtr, HDC hdc, const RECT *lprcBox)
4568 HDC mem_hdc;
4570 if (!infoPtr->hBkBitmap)
4571 return;
4573 TRACE("(hdc=%p, lprcBox=%s, hBkBitmap=%p)\n", hdc, wine_dbgstr_rect(lprcBox), infoPtr->hBkBitmap);
4575 mem_hdc = CreateCompatibleDC(hdc);
4576 SelectObject(mem_hdc, infoPtr->hBkBitmap);
4577 BitBlt(hdc, lprcBox->left, lprcBox->top, lprcBox->right - lprcBox->left,
4578 lprcBox->bottom - lprcBox->top, mem_hdc, lprcBox->left, lprcBox->top, SRCCOPY);
4579 DeleteDC(mem_hdc);
4582 /***
4583 * DESCRIPTION:
4584 * Erases the background of the given rectangle
4586 * PARAMETER(S):
4587 * [I] infoPtr : valid pointer to the listview structure
4588 * [I] hdc : device context handle
4589 * [I] lprcBox : clipping rectangle
4591 * RETURN:
4592 * Success: TRUE
4593 * Failure: FALSE
4595 static BOOL LISTVIEW_FillBkgnd(const LISTVIEW_INFO *infoPtr, HDC hdc, const RECT *lprcBox)
4597 if (infoPtr->hBkBrush)
4599 TRACE("(hdc=%p, lprcBox=%s, hBkBrush=%p)\n", hdc, wine_dbgstr_rect(lprcBox), infoPtr->hBkBrush);
4601 FillRect(hdc, lprcBox, infoPtr->hBkBrush);
4604 LISTVIEW_DrawBackgroundBitmap(infoPtr, hdc, lprcBox);
4605 return TRUE;
4608 /* Draw main item or subitem */
4609 static void LISTVIEW_DrawItemPart(LISTVIEW_INFO *infoPtr, LVITEMW *item, const NMLVCUSTOMDRAW *nmlvcd, const POINT *pos)
4611 RECT rcSelect, rcLabel, rcBox, rcStateIcon, rcIcon;
4612 const RECT *background;
4613 HIMAGELIST himl;
4614 UINT format;
4615 RECT *focus;
4617 /* now check if we need to update the focus rectangle */
4618 focus = infoPtr->bFocus && (item->state & LVIS_FOCUSED) ? &infoPtr->rcFocus : 0;
4619 if (!focus) item->state &= ~LVIS_FOCUSED;
4621 LISTVIEW_GetItemMetrics(infoPtr, item, &rcBox, &rcSelect, &rcIcon, &rcStateIcon, &rcLabel);
4622 OffsetRect(&rcBox, pos->x, pos->y);
4623 OffsetRect(&rcSelect, pos->x, pos->y);
4624 OffsetRect(&rcIcon, pos->x, pos->y);
4625 OffsetRect(&rcStateIcon, pos->x, pos->y);
4626 OffsetRect(&rcLabel, pos->x, pos->y);
4627 TRACE("%d: box=%s, select=%s, icon=%s. label=%s\n", item->iSubItem,
4628 wine_dbgstr_rect(&rcBox), wine_dbgstr_rect(&rcSelect),
4629 wine_dbgstr_rect(&rcIcon), wine_dbgstr_rect(&rcLabel));
4631 /* FIXME: temporary hack */
4632 rcSelect.left = rcLabel.left;
4634 if (infoPtr->uView == LV_VIEW_DETAILS && item->iSubItem == 0)
4636 if (!(infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT))
4637 OffsetRect(&rcSelect, LISTVIEW_GetColumnInfo(infoPtr, 0)->rcHeader.left, 0);
4638 OffsetRect(&rcIcon, LISTVIEW_GetColumnInfo(infoPtr, 0)->rcHeader.left, 0);
4639 OffsetRect(&rcStateIcon, LISTVIEW_GetColumnInfo(infoPtr, 0)->rcHeader.left, 0);
4640 OffsetRect(&rcLabel, LISTVIEW_GetColumnInfo(infoPtr, 0)->rcHeader.left, 0);
4643 /* in icon mode, the label rect is really what we want to draw the
4644 * background for */
4645 /* in detail mode, we want to paint background for label rect when
4646 * item is not selected or listview has full row select; otherwise paint
4647 * background for text only */
4648 if ( infoPtr->uView == LV_VIEW_ICON ||
4649 (infoPtr->uView == LV_VIEW_DETAILS && (!(item->state & LVIS_SELECTED) ||
4650 (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT))))
4651 background = &rcLabel;
4652 else
4653 background = &rcSelect;
4655 if (nmlvcd->clrTextBk != CLR_NONE)
4656 ExtTextOutW(nmlvcd->nmcd.hdc, background->left, background->top, ETO_OPAQUE, background, NULL, 0, NULL);
4658 if (item->state & LVIS_FOCUSED)
4660 if (infoPtr->uView == LV_VIEW_DETAILS)
4662 if (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT)
4664 /* we have to update left focus bound too if item isn't in leftmost column
4665 and reduce right box bound */
4666 if (DPA_GetPtrCount(infoPtr->hdpaColumns) > 0)
4668 INT leftmost;
4670 if ((leftmost = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX, 0, 0)))
4672 INT Originx = pos->x - LISTVIEW_GetColumnInfo(infoPtr, leftmost)->rcHeader.left;
4673 INT rightmost = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX,
4674 DPA_GetPtrCount(infoPtr->hdpaColumns) - 1, 0);
4676 rcBox.right = LISTVIEW_GetColumnInfo(infoPtr, rightmost)->rcHeader.right + Originx;
4677 rcSelect.left = LISTVIEW_GetColumnInfo(infoPtr, leftmost)->rcHeader.left + Originx;
4680 rcSelect.right = rcBox.right;
4682 infoPtr->rcFocus = rcSelect;
4684 else
4685 infoPtr->rcFocus = rcLabel;
4688 /* state icons */
4689 if (infoPtr->himlState && STATEIMAGEINDEX(item->state) && (item->iSubItem == 0))
4691 UINT stateimage = STATEIMAGEINDEX(item->state);
4692 if (stateimage)
4694 TRACE("stateimage=%d\n", stateimage);
4695 ImageList_Draw(infoPtr->himlState, stateimage-1, nmlvcd->nmcd.hdc, rcStateIcon.left, rcStateIcon.top, ILD_NORMAL);
4699 /* item icons */
4700 himl = (infoPtr->uView == LV_VIEW_ICON ? infoPtr->himlNormal : infoPtr->himlSmall);
4701 if (himl && item->iImage >= 0 && !IsRectEmpty(&rcIcon))
4703 UINT style;
4705 TRACE("iImage=%d\n", item->iImage);
4707 if (item->state & (LVIS_SELECTED | LVIS_CUT) && infoPtr->bFocus)
4708 style = ILD_SELECTED;
4709 else
4710 style = ILD_NORMAL;
4712 ImageList_DrawEx(himl, item->iImage, nmlvcd->nmcd.hdc, rcIcon.left, rcIcon.top,
4713 rcIcon.right - rcIcon.left, rcIcon.bottom - rcIcon.top, infoPtr->clrBk,
4714 item->state & LVIS_CUT ? RGB(255, 255, 255) : CLR_DEFAULT,
4715 style | (item->state & LVIS_OVERLAYMASK));
4718 /* Don't bother painting item being edited */
4719 if (infoPtr->hwndEdit && item->iItem == infoPtr->nEditLabelItem && item->iSubItem == 0) return;
4721 /* figure out the text drawing flags */
4722 format = (infoPtr->uView == LV_VIEW_ICON ? (focus ? LV_FL_DT_FLAGS : LV_ML_DT_FLAGS) : LV_SL_DT_FLAGS);
4723 if (infoPtr->uView == LV_VIEW_ICON)
4724 format = (focus ? LV_FL_DT_FLAGS : LV_ML_DT_FLAGS);
4725 else if (item->iSubItem)
4727 switch (LISTVIEW_GetColumnInfo(infoPtr, item->iSubItem)->fmt & LVCFMT_JUSTIFYMASK)
4729 case LVCFMT_RIGHT: format |= DT_RIGHT; break;
4730 case LVCFMT_CENTER: format |= DT_CENTER; break;
4731 default: format |= DT_LEFT;
4734 if (!(format & (DT_RIGHT | DT_CENTER)))
4736 if (himl && item->iImage >= 0 && !IsRectEmpty(&rcIcon)) rcLabel.left += IMAGE_PADDING;
4737 else rcLabel.left += LABEL_HOR_PADDING;
4739 else if (format & DT_RIGHT) rcLabel.right -= LABEL_HOR_PADDING;
4741 /* for GRIDLINES reduce the bottom so the text formats correctly */
4742 if (infoPtr->uView == LV_VIEW_DETAILS && infoPtr->dwLvExStyle & LVS_EX_GRIDLINES)
4743 rcLabel.bottom--;
4745 DrawTextW(nmlvcd->nmcd.hdc, item->pszText, -1, &rcLabel, format);
4748 /***
4749 * DESCRIPTION:
4750 * Draws an item.
4752 * PARAMETER(S):
4753 * [I] infoPtr : valid pointer to the listview structure
4754 * [I] hdc : device context handle
4755 * [I] nItem : item index
4756 * [I] nSubItem : subitem index
4757 * [I] pos : item position in client coordinates
4758 * [I] cdmode : custom draw mode
4760 * RETURN:
4761 * Success: TRUE
4762 * Failure: FALSE
4764 static BOOL LISTVIEW_DrawItem(LISTVIEW_INFO *infoPtr, HDC hdc, INT nItem, ITERATOR *subitems, POINT pos, DWORD cdmode)
4766 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
4767 static WCHAR callbackW[] = L"(callback)";
4768 DWORD cdsubitemmode = CDRF_DODEFAULT;
4769 RECT *focus, rcBox;
4770 NMLVCUSTOMDRAW nmlvcd;
4771 LVITEMW lvItem;
4773 TRACE("(hdc=%p, nItem=%d, subitems=%p, pos=%s)\n", hdc, nItem, subitems, wine_dbgstr_point(&pos));
4775 /* get information needed for drawing the item */
4776 lvItem.mask = LVIF_TEXT | LVIF_IMAGE | LVIF_PARAM | LVIF_STATE;
4777 if (infoPtr->uView == LV_VIEW_DETAILS) lvItem.mask |= LVIF_INDENT;
4778 lvItem.stateMask = LVIS_SELECTED | LVIS_FOCUSED | LVIS_STATEIMAGEMASK | LVIS_CUT | LVIS_OVERLAYMASK;
4779 lvItem.iItem = nItem;
4780 lvItem.iSubItem = 0;
4781 lvItem.state = 0;
4782 lvItem.lParam = 0;
4783 lvItem.cchTextMax = DISP_TEXT_SIZE;
4784 lvItem.pszText = szDispText;
4785 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) return FALSE;
4786 if (lvItem.pszText == LPSTR_TEXTCALLBACKW) lvItem.pszText = callbackW;
4787 TRACE(" lvItem=%s\n", debuglvitem_t(&lvItem, TRUE));
4789 /* now check if we need to update the focus rectangle */
4790 focus = infoPtr->bFocus && (lvItem.state & LVIS_FOCUSED) ? &infoPtr->rcFocus : 0;
4791 if (!focus) lvItem.state &= ~LVIS_FOCUSED;
4793 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, &rcBox, NULL, NULL, NULL, NULL);
4794 OffsetRect(&rcBox, pos.x, pos.y);
4796 /* Full custom draw stage sequence looks like this:
4798 LV_VIEW_DETAILS:
4800 - CDDS_ITEMPREPAINT
4801 - CDDS_ITEMPREPAINT|CDDS_SUBITEM | => sent n times, where n is number of subitems,
4802 CDDS_ITEMPOSTPAINT|CDDS_SUBITEM | including item itself
4803 - CDDS_ITEMPOSTPAINT
4805 other styles:
4807 - CDDS_ITEMPREPAINT
4808 - CDDS_ITEMPOSTPAINT
4811 /* fill in the custom draw structure */
4812 customdraw_fill(&nmlvcd, infoPtr, hdc, &rcBox, &lvItem);
4813 if (cdmode & CDRF_NOTIFYITEMDRAW)
4814 cdsubitemmode = notify_customdraw(infoPtr, CDDS_ITEMPREPAINT, &nmlvcd);
4815 if (cdsubitemmode & CDRF_SKIPDEFAULT) goto postpaint;
4817 if (subitems)
4819 while (iterator_next(subitems))
4821 DWORD subitemstage = CDRF_DODEFAULT;
4823 /* We need to query for each subitem, item's data (subitem == 0) is already here at this point */
4824 if (subitems->nItem)
4826 lvItem.mask = LVIF_TEXT | LVIF_IMAGE | LVIF_PARAM | LVIF_INDENT;
4827 lvItem.stateMask = LVIS_SELECTED | LVIS_FOCUSED | LVIS_STATEIMAGEMASK | LVIS_CUT | LVIS_OVERLAYMASK;
4828 lvItem.iItem = nItem;
4829 lvItem.iSubItem = subitems->nItem;
4830 lvItem.state = 0;
4831 lvItem.lParam = 0;
4832 lvItem.cchTextMax = DISP_TEXT_SIZE;
4833 lvItem.pszText = szDispText;
4834 szDispText[0] = 0;
4835 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) return FALSE;
4836 if (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT)
4837 lvItem.state = LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED);
4838 if (lvItem.pszText == LPSTR_TEXTCALLBACKW) lvItem.pszText = callbackW;
4839 TRACE(" lvItem=%s\n", debuglvitem_t(&lvItem, TRUE));
4841 /* update custom draw data */
4842 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, &nmlvcd.nmcd.rc, NULL, NULL, NULL, NULL);
4843 OffsetRect(&nmlvcd.nmcd.rc, pos.x, pos.y);
4844 nmlvcd.iSubItem = subitems->nItem;
4847 if (cdsubitemmode & CDRF_NOTIFYSUBITEMDRAW)
4848 subitemstage = notify_customdraw(infoPtr, CDDS_SUBITEM | CDDS_ITEMPREPAINT, &nmlvcd);
4850 if (subitems->nItem == 0 || (cdmode & CDRF_NOTIFYITEMDRAW))
4851 prepaint_setup(infoPtr, hdc, &nmlvcd, FALSE);
4852 else if (!(infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT))
4853 prepaint_setup(infoPtr, hdc, &nmlvcd, TRUE);
4855 if (!(subitemstage & CDRF_SKIPDEFAULT))
4856 LISTVIEW_DrawItemPart(infoPtr, &lvItem, &nmlvcd, &pos);
4858 if (subitemstage & CDRF_NOTIFYPOSTPAINT)
4859 subitemstage = notify_customdraw(infoPtr, CDDS_SUBITEM | CDDS_ITEMPOSTPAINT, &nmlvcd);
4862 else
4864 prepaint_setup(infoPtr, hdc, &nmlvcd, FALSE);
4865 LISTVIEW_DrawItemPart(infoPtr, &lvItem, &nmlvcd, &pos);
4868 postpaint:
4869 if (cdsubitemmode & CDRF_NOTIFYPOSTPAINT)
4871 nmlvcd.iSubItem = 0;
4872 notify_customdraw(infoPtr, CDDS_ITEMPOSTPAINT, &nmlvcd);
4875 return TRUE;
4878 /***
4879 * DESCRIPTION:
4880 * Draws listview items when in owner draw mode.
4882 * PARAMETER(S):
4883 * [I] infoPtr : valid pointer to the listview structure
4884 * [I] hdc : device context handle
4886 * RETURN:
4887 * None
4889 static void LISTVIEW_RefreshOwnerDraw(const LISTVIEW_INFO *infoPtr, ITERATOR *i, HDC hdc, DWORD cdmode)
4891 UINT uID = (UINT)GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
4892 DWORD cditemmode = CDRF_DODEFAULT;
4893 NMLVCUSTOMDRAW nmlvcd;
4894 POINT Origin, Position;
4895 DRAWITEMSTRUCT dis;
4896 LVITEMW item;
4898 TRACE("()\n");
4900 ZeroMemory(&dis, sizeof(dis));
4902 /* Get scroll info once before loop */
4903 LISTVIEW_GetOrigin(infoPtr, &Origin);
4905 /* iterate through the invalidated rows */
4906 while(iterator_next(i))
4908 item.iItem = i->nItem;
4909 item.iSubItem = 0;
4910 item.mask = LVIF_PARAM | LVIF_STATE;
4911 item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
4912 if (!LISTVIEW_GetItemW(infoPtr, &item)) continue;
4914 dis.CtlType = ODT_LISTVIEW;
4915 dis.CtlID = uID;
4916 dis.itemID = item.iItem;
4917 dis.itemAction = ODA_DRAWENTIRE;
4918 dis.itemState = 0;
4919 if (item.state & LVIS_SELECTED) dis.itemState |= ODS_SELECTED;
4920 if (infoPtr->bFocus && (item.state & LVIS_FOCUSED)) dis.itemState |= ODS_FOCUS;
4921 dis.hwndItem = infoPtr->hwndSelf;
4922 dis.hDC = hdc;
4923 LISTVIEW_GetItemOrigin(infoPtr, dis.itemID, &Position);
4924 dis.rcItem.left = Position.x + Origin.x;
4925 dis.rcItem.right = dis.rcItem.left + infoPtr->nItemWidth;
4926 dis.rcItem.top = Position.y + Origin.y;
4927 dis.rcItem.bottom = dis.rcItem.top + infoPtr->nItemHeight;
4928 dis.itemData = item.lParam;
4930 TRACE("item=%s, rcItem=%s\n", debuglvitem_t(&item, TRUE), wine_dbgstr_rect(&dis.rcItem));
4933 * Even if we do not send the CDRF_NOTIFYITEMDRAW we need to fill the nmlvcd
4934 * structure for the rest. of the paint cycle
4936 customdraw_fill(&nmlvcd, infoPtr, hdc, &dis.rcItem, &item);
4937 if (cdmode & CDRF_NOTIFYITEMDRAW)
4938 cditemmode = notify_customdraw(infoPtr, CDDS_PREPAINT, &nmlvcd);
4940 if (!(cditemmode & CDRF_SKIPDEFAULT))
4942 prepaint_setup (infoPtr, hdc, &nmlvcd, FALSE);
4943 SendMessageW(infoPtr->hwndNotify, WM_DRAWITEM, dis.CtlID, (LPARAM)&dis);
4946 if (cditemmode & CDRF_NOTIFYPOSTPAINT)
4947 notify_postpaint(infoPtr, &nmlvcd);
4951 /***
4952 * DESCRIPTION:
4953 * Draws listview items when in report display mode.
4955 * PARAMETER(S):
4956 * [I] infoPtr : valid pointer to the listview structure
4957 * [I] hdc : device context handle
4958 * [I] cdmode : custom draw mode
4960 * RETURN:
4961 * None
4963 static void LISTVIEW_RefreshReport(LISTVIEW_INFO *infoPtr, ITERATOR *i, HDC hdc, DWORD cdmode)
4965 INT rgntype;
4966 RECT rcClip, rcItem;
4967 POINT Origin;
4968 RANGES colRanges;
4969 INT col;
4970 ITERATOR j;
4972 TRACE("()\n");
4974 /* figure out what to draw */
4975 rgntype = GetClipBox(hdc, &rcClip);
4976 if (rgntype == NULLREGION) return;
4978 /* Get scroll info once before loop */
4979 LISTVIEW_GetOrigin(infoPtr, &Origin);
4981 colRanges = ranges_create(DPA_GetPtrCount(infoPtr->hdpaColumns));
4983 /* narrow down the columns we need to paint */
4984 for(col = 0; col < DPA_GetPtrCount(infoPtr->hdpaColumns); col++)
4986 INT index = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX, col, 0);
4988 LISTVIEW_GetHeaderRect(infoPtr, index, &rcItem);
4989 if ((rcItem.right + Origin.x >= rcClip.left) && (rcItem.left + Origin.x < rcClip.right))
4990 ranges_additem(colRanges, index);
4992 iterator_rangesitems(&j, colRanges);
4994 /* in full row select, we _have_ to draw the main item */
4995 if (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT)
4996 j.nSpecial = 0;
4998 /* iterate through the invalidated rows */
4999 while(iterator_next(i))
5001 RANGES subitems;
5002 POINT Position;
5003 ITERATOR k;
5005 SelectObject(hdc, infoPtr->hFont);
5006 LISTVIEW_GetItemOrigin(infoPtr, i->nItem, &Position);
5007 Position.x = Origin.x;
5008 Position.y += Origin.y;
5010 subitems = ranges_create(DPA_GetPtrCount(infoPtr->hdpaColumns));
5012 /* iterate through the invalidated columns */
5013 while(iterator_next(&j))
5015 LISTVIEW_GetHeaderRect(infoPtr, j.nItem, &rcItem);
5017 if (rgntype == COMPLEXREGION && !((infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT) && j.nItem == 0))
5019 rcItem.top = 0;
5020 rcItem.bottom = infoPtr->nItemHeight;
5021 OffsetRect(&rcItem, Origin.x, Position.y);
5022 if (!RectVisible(hdc, &rcItem)) continue;
5025 ranges_additem(subitems, j.nItem);
5028 iterator_rangesitems(&k, subitems);
5029 LISTVIEW_DrawItem(infoPtr, hdc, i->nItem, &k, Position, cdmode);
5030 iterator_destroy(&k);
5032 iterator_destroy(&j);
5035 /***
5036 * DESCRIPTION:
5037 * Draws the gridlines if necessary when in report display mode.
5039 * PARAMETER(S):
5040 * [I] infoPtr : valid pointer to the listview structure
5041 * [I] hdc : device context handle
5043 * RETURN:
5044 * None
5046 static void LISTVIEW_RefreshReportGrid(LISTVIEW_INFO *infoPtr, HDC hdc)
5048 INT rgntype;
5049 INT y, itemheight;
5050 INT col, index;
5051 HPEN hPen, hOldPen;
5052 RECT rcClip, rcItem = {0};
5053 POINT Origin;
5054 RANGES colRanges;
5055 ITERATOR j;
5056 BOOL rmost = FALSE;
5058 TRACE("()\n");
5060 /* figure out what to draw */
5061 rgntype = GetClipBox(hdc, &rcClip);
5062 if (rgntype == NULLREGION) return;
5064 /* Get scroll info once before loop */
5065 LISTVIEW_GetOrigin(infoPtr, &Origin);
5067 colRanges = ranges_create(DPA_GetPtrCount(infoPtr->hdpaColumns));
5069 /* narrow down the columns we need to paint */
5070 for(col = 0; col < DPA_GetPtrCount(infoPtr->hdpaColumns); col++)
5072 index = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX, col, 0);
5074 LISTVIEW_GetHeaderRect(infoPtr, index, &rcItem);
5075 if ((rcItem.right + Origin.x >= rcClip.left) && (rcItem.left + Origin.x < rcClip.right))
5076 ranges_additem(colRanges, index);
5079 /* is right most vertical line visible? */
5080 if (DPA_GetPtrCount(infoPtr->hdpaColumns) > 0)
5082 index = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX, DPA_GetPtrCount(infoPtr->hdpaColumns) - 1, 0);
5083 LISTVIEW_GetHeaderRect(infoPtr, index, &rcItem);
5084 rmost = (rcItem.right + Origin.x < rcClip.right);
5087 if ((hPen = CreatePen( PS_SOLID, 1, comctl32_color.clr3dFace )))
5089 hOldPen = SelectObject ( hdc, hPen );
5091 /* draw the vertical lines for the columns */
5092 iterator_rangesitems(&j, colRanges);
5093 while(iterator_next(&j))
5095 LISTVIEW_GetHeaderRect(infoPtr, j.nItem, &rcItem);
5096 if (rcItem.left == 0) continue; /* skip leftmost column */
5097 rcItem.left += Origin.x;
5098 rcItem.right += Origin.x;
5099 rcItem.top = infoPtr->rcList.top;
5100 rcItem.bottom = infoPtr->rcList.bottom;
5101 TRACE("vert col=%d, rcItem=%s\n", j.nItem, wine_dbgstr_rect(&rcItem));
5102 MoveToEx (hdc, rcItem.left, rcItem.top, NULL);
5103 LineTo (hdc, rcItem.left, rcItem.bottom);
5105 iterator_destroy(&j);
5106 /* draw rightmost grid line if visible */
5107 if (rmost)
5109 index = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX,
5110 DPA_GetPtrCount(infoPtr->hdpaColumns) - 1, 0);
5111 LISTVIEW_GetHeaderRect(infoPtr, index, &rcItem);
5113 rcItem.right += Origin.x;
5115 MoveToEx (hdc, rcItem.right, infoPtr->rcList.top, NULL);
5116 LineTo (hdc, rcItem.right, infoPtr->rcList.bottom);
5119 /* draw the horizontal lines for the rows */
5120 itemheight = LISTVIEW_CalculateItemHeight(infoPtr);
5121 rcItem.left = infoPtr->rcList.left;
5122 rcItem.right = infoPtr->rcList.right;
5123 for(y = Origin.y > 1 ? Origin.y - 1 : itemheight - 1 + Origin.y % itemheight; y<=infoPtr->rcList.bottom; y+=itemheight)
5125 rcItem.bottom = rcItem.top = y;
5126 TRACE("horz rcItem=%s\n", wine_dbgstr_rect(&rcItem));
5127 MoveToEx (hdc, rcItem.left, rcItem.top, NULL);
5128 LineTo (hdc, rcItem.right, rcItem.top);
5131 SelectObject( hdc, hOldPen );
5132 DeleteObject( hPen );
5134 else
5135 ranges_destroy(colRanges);
5138 /***
5139 * DESCRIPTION:
5140 * Draws listview items when in list display mode.
5142 * PARAMETER(S):
5143 * [I] infoPtr : valid pointer to the listview structure
5144 * [I] hdc : device context handle
5145 * [I] cdmode : custom draw mode
5147 * RETURN:
5148 * None
5150 static void LISTVIEW_RefreshList(LISTVIEW_INFO *infoPtr, ITERATOR *i, HDC hdc, DWORD cdmode)
5152 POINT Origin, Position;
5154 /* Get scroll info once before loop */
5155 LISTVIEW_GetOrigin(infoPtr, &Origin);
5157 while(iterator_prev(i))
5159 SelectObject(hdc, infoPtr->hFont);
5160 LISTVIEW_GetItemOrigin(infoPtr, i->nItem, &Position);
5161 Position.x += Origin.x;
5162 Position.y += Origin.y;
5164 LISTVIEW_DrawItem(infoPtr, hdc, i->nItem, NULL, Position, cdmode);
5169 /***
5170 * DESCRIPTION:
5171 * Draws listview items.
5173 * PARAMETER(S):
5174 * [I] infoPtr : valid pointer to the listview structure
5175 * [I] hdc : device context handle
5176 * [I] prcErase : rect to be erased before refresh (may be NULL)
5178 * RETURN:
5179 * NoneX
5181 static void LISTVIEW_Refresh(LISTVIEW_INFO *infoPtr, HDC hdc, const RECT *prcErase)
5183 COLORREF oldTextColor = 0, oldBkColor = 0;
5184 NMLVCUSTOMDRAW nmlvcd;
5185 HFONT hOldFont = 0;
5186 DWORD cdmode;
5187 INT oldBkMode = 0;
5188 RECT rcClient;
5189 ITERATOR i;
5190 HDC hdcOrig = hdc;
5191 HBITMAP hbmp = NULL;
5192 RANGE range;
5194 LISTVIEW_DUMP(infoPtr);
5196 if (infoPtr->dwLvExStyle & LVS_EX_DOUBLEBUFFER) {
5197 TRACE("double buffering\n");
5199 hdc = CreateCompatibleDC(hdcOrig);
5200 if (!hdc) {
5201 ERR("Failed to create DC for backbuffer\n");
5202 return;
5204 hbmp = CreateCompatibleBitmap(hdcOrig, infoPtr->rcList.right,
5205 infoPtr->rcList.bottom);
5206 if (!hbmp) {
5207 ERR("Failed to create bitmap for backbuffer\n");
5208 DeleteDC(hdc);
5209 return;
5212 SelectObject(hdc, hbmp);
5213 SelectObject(hdc, infoPtr->hFont);
5215 if(GetClipBox(hdcOrig, &rcClient))
5216 IntersectClipRect(hdc, rcClient.left, rcClient.top, rcClient.right, rcClient.bottom);
5217 } else {
5218 /* Save dc values we're gonna trash while drawing
5219 * FIXME: Should be done in LISTVIEW_DrawItem() */
5220 hOldFont = SelectObject(hdc, infoPtr->hFont);
5221 oldBkMode = GetBkMode(hdc);
5222 oldBkColor = GetBkColor(hdc);
5223 oldTextColor = GetTextColor(hdc);
5226 infoPtr->bIsDrawing = TRUE;
5228 if (prcErase) {
5229 LISTVIEW_FillBkgnd(infoPtr, hdc, prcErase);
5230 } else if (infoPtr->dwLvExStyle & LVS_EX_DOUBLEBUFFER) {
5231 /* If no erasing was done (usually because RedrawWindow was called
5232 * with RDW_INVALIDATE only) we need to copy the old contents into
5233 * the backbuffer before continuing. */
5234 BitBlt(hdc, infoPtr->rcList.left, infoPtr->rcList.top,
5235 infoPtr->rcList.right - infoPtr->rcList.left,
5236 infoPtr->rcList.bottom - infoPtr->rcList.top,
5237 hdcOrig, infoPtr->rcList.left, infoPtr->rcList.top, SRCCOPY);
5240 GetClientRect(infoPtr->hwndSelf, &rcClient);
5241 customdraw_fill(&nmlvcd, infoPtr, hdc, &rcClient, 0);
5242 cdmode = notify_customdraw(infoPtr, CDDS_PREPAINT, &nmlvcd);
5243 if (cdmode & CDRF_SKIPDEFAULT) goto enddraw;
5245 /* nothing to draw */
5246 if(infoPtr->nItemCount == 0) goto enddraw;
5248 /* figure out what we need to draw */
5249 iterator_visibleitems(&i, infoPtr, hdc);
5250 range = iterator_range(&i);
5252 /* send cache hint notification */
5253 if (infoPtr->dwStyle & LVS_OWNERDATA)
5255 NMLVCACHEHINT nmlv;
5257 ZeroMemory(&nmlv, sizeof(NMLVCACHEHINT));
5258 nmlv.iFrom = range.lower;
5259 nmlv.iTo = range.upper - 1;
5260 notify_hdr(infoPtr, LVN_ODCACHEHINT, &nmlv.hdr);
5263 if ((infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && (infoPtr->uView == LV_VIEW_DETAILS))
5264 LISTVIEW_RefreshOwnerDraw(infoPtr, &i, hdc, cdmode);
5265 else
5267 if (infoPtr->uView == LV_VIEW_DETAILS)
5268 LISTVIEW_RefreshReport(infoPtr, &i, hdc, cdmode);
5269 else /* LV_VIEW_LIST, LV_VIEW_ICON or LV_VIEW_SMALLICON */
5270 LISTVIEW_RefreshList(infoPtr, &i, hdc, cdmode);
5272 /* if we have a focus rect and it's visible, draw it */
5273 if (infoPtr->bFocus && range.lower <= infoPtr->nFocusedItem &&
5274 (range.upper - 1) >= infoPtr->nFocusedItem)
5275 LISTVIEW_DrawFocusRect(infoPtr, hdc);
5277 iterator_destroy(&i);
5279 enddraw:
5280 /* For LVS_EX_GRIDLINES go and draw lines */
5281 /* This includes the case where there were *no* items */
5282 if ((infoPtr->uView == LV_VIEW_DETAILS) && infoPtr->dwLvExStyle & LVS_EX_GRIDLINES)
5283 LISTVIEW_RefreshReportGrid(infoPtr, hdc);
5285 /* Draw marquee rectangle if appropriate */
5286 if (infoPtr->bMarqueeSelect)
5287 DrawFocusRect(hdc, &infoPtr->marqueeDrawRect);
5289 if (cdmode & CDRF_NOTIFYPOSTPAINT)
5290 notify_postpaint(infoPtr, &nmlvcd);
5292 if(hbmp) {
5293 BitBlt(hdcOrig, infoPtr->rcList.left, infoPtr->rcList.top,
5294 infoPtr->rcList.right - infoPtr->rcList.left,
5295 infoPtr->rcList.bottom - infoPtr->rcList.top,
5296 hdc, infoPtr->rcList.left, infoPtr->rcList.top, SRCCOPY);
5298 DeleteObject(hbmp);
5299 DeleteDC(hdc);
5300 } else {
5301 SelectObject(hdc, hOldFont);
5302 SetBkMode(hdc, oldBkMode);
5303 SetBkColor(hdc, oldBkColor);
5304 SetTextColor(hdc, oldTextColor);
5307 infoPtr->bIsDrawing = FALSE;
5311 /***
5312 * DESCRIPTION:
5313 * Calculates the approximate width and height of a given number of items.
5315 * PARAMETER(S):
5316 * [I] infoPtr : valid pointer to the listview structure
5317 * [I] nItemCount : number of items
5318 * [I] wWidth : width
5319 * [I] wHeight : height
5321 * RETURN:
5322 * Returns a DWORD. The width in the low word and the height in high word.
5324 static DWORD LISTVIEW_ApproximateViewRect(const LISTVIEW_INFO *infoPtr, INT nItemCount,
5325 WORD wWidth, WORD wHeight)
5327 DWORD dwViewRect = 0;
5329 if (nItemCount == -1)
5330 nItemCount = infoPtr->nItemCount;
5332 if (infoPtr->uView == LV_VIEW_LIST)
5334 INT nItemCountPerColumn = 1;
5335 INT nColumnCount = 0;
5337 if (wHeight == 0xFFFF)
5339 /* use current height */
5340 wHeight = infoPtr->rcList.bottom - infoPtr->rcList.top;
5343 if (wHeight < infoPtr->nItemHeight)
5344 wHeight = infoPtr->nItemHeight;
5346 if (nItemCount > 0)
5348 if (infoPtr->nItemHeight > 0)
5350 nItemCountPerColumn = wHeight / infoPtr->nItemHeight;
5351 if (nItemCountPerColumn == 0)
5352 nItemCountPerColumn = 1;
5354 if (nItemCount % nItemCountPerColumn != 0)
5355 nColumnCount = nItemCount / nItemCountPerColumn;
5356 else
5357 nColumnCount = nItemCount / nItemCountPerColumn + 1;
5361 /* Microsoft padding magic */
5362 wHeight = nItemCountPerColumn * infoPtr->nItemHeight + 2;
5363 wWidth = nColumnCount * infoPtr->nItemWidth + 2;
5365 dwViewRect = MAKELONG(wWidth, wHeight);
5367 else if (infoPtr->uView == LV_VIEW_DETAILS)
5369 RECT rcBox;
5371 if (infoPtr->nItemCount > 0)
5373 LISTVIEW_GetItemBox(infoPtr, 0, &rcBox);
5374 wWidth = rcBox.right - rcBox.left;
5375 wHeight = (rcBox.bottom - rcBox.top) * nItemCount;
5377 else
5379 /* use current height and width */
5380 if (wHeight == 0xffff)
5381 wHeight = infoPtr->rcList.bottom - infoPtr->rcList.top;
5382 if (wWidth == 0xffff)
5383 wWidth = infoPtr->rcList.right - infoPtr->rcList.left;
5386 dwViewRect = MAKELONG(wWidth, wHeight);
5388 else if (infoPtr->uView == LV_VIEW_ICON)
5390 UINT rows,cols;
5391 UINT nItemWidth;
5392 UINT nItemHeight;
5394 nItemWidth = infoPtr->iconSpacing.cx;
5395 nItemHeight = infoPtr->iconSpacing.cy;
5397 if (wWidth == 0xffff)
5398 wWidth = infoPtr->rcList.right - infoPtr->rcList.left;
5400 if (wWidth < nItemWidth)
5401 wWidth = nItemWidth;
5403 cols = wWidth / nItemWidth;
5404 if (cols > nItemCount)
5405 cols = nItemCount;
5406 if (cols < 1)
5407 cols = 1;
5409 if (nItemCount)
5411 rows = nItemCount / cols;
5412 if (nItemCount % cols)
5413 rows++;
5415 else
5416 rows = 0;
5418 wHeight = (nItemHeight * rows)+2;
5419 wWidth = (nItemWidth * cols)+2;
5421 dwViewRect = MAKELONG(wWidth, wHeight);
5423 else if (infoPtr->uView == LV_VIEW_SMALLICON)
5424 FIXME("uView == LV_VIEW_SMALLICON: not implemented\n");
5426 return dwViewRect;
5429 /***
5430 * DESCRIPTION:
5431 * Cancel edit label with saving item text.
5433 * PARAMETER(S):
5434 * [I] infoPtr : valid pointer to the listview structure
5436 * RETURN:
5437 * Always returns TRUE.
5439 static LRESULT LISTVIEW_CancelEditLabel(LISTVIEW_INFO *infoPtr)
5441 if (infoPtr->hwndEdit)
5443 /* handle value will be lost after LISTVIEW_EndEditLabelT */
5444 HWND edit = infoPtr->hwndEdit;
5446 LISTVIEW_EndEditLabelT(infoPtr, TRUE, IsWindowUnicode(infoPtr->hwndEdit));
5447 SendMessageW(edit, WM_CLOSE, 0, 0);
5450 return TRUE;
5453 /***
5454 * DESCRIPTION:
5455 * Create a drag image list for the specified item.
5457 * PARAMETER(S):
5458 * [I] infoPtr : valid pointer to the listview structure
5459 * [I] iItem : index of item
5460 * [O] lppt : Upper-left corner of the image
5462 * RETURN:
5463 * Returns a handle to the image list if successful, NULL otherwise.
5465 static HIMAGELIST LISTVIEW_CreateDragImage(LISTVIEW_INFO *infoPtr, INT iItem, LPPOINT lppt)
5467 RECT rcItem;
5468 SIZE size;
5469 POINT pos;
5470 HDC hdc, hdcOrig;
5471 HBITMAP hbmp, hOldbmp;
5472 HFONT hOldFont;
5473 HIMAGELIST dragList = 0;
5474 TRACE("iItem=%d Count=%d\n", iItem, infoPtr->nItemCount);
5476 if (iItem < 0 || iItem >= infoPtr->nItemCount || !lppt)
5477 return 0;
5479 rcItem.left = LVIR_BOUNDS;
5480 if (!LISTVIEW_GetItemRect(infoPtr, iItem, &rcItem))
5481 return 0;
5483 lppt->x = rcItem.left;
5484 lppt->y = rcItem.top;
5486 size.cx = rcItem.right - rcItem.left;
5487 size.cy = rcItem.bottom - rcItem.top;
5489 hdcOrig = GetDC(infoPtr->hwndSelf);
5490 hdc = CreateCompatibleDC(hdcOrig);
5491 hbmp = CreateCompatibleBitmap(hdcOrig, size.cx, size.cy);
5492 hOldbmp = SelectObject(hdc, hbmp);
5493 hOldFont = SelectObject(hdc, infoPtr->hFont);
5495 SetRect(&rcItem, 0, 0, size.cx, size.cy);
5496 FillRect(hdc, &rcItem, infoPtr->hBkBrush);
5498 pos.x = pos.y = 0;
5499 if (LISTVIEW_DrawItem(infoPtr, hdc, iItem, NULL, pos, CDRF_DODEFAULT))
5501 dragList = ImageList_Create(size.cx, size.cy, ILC_COLOR, 10, 10);
5502 SelectObject(hdc, hOldbmp);
5503 ImageList_Add(dragList, hbmp, 0);
5505 else
5506 SelectObject(hdc, hOldbmp);
5508 SelectObject(hdc, hOldFont);
5509 DeleteObject(hbmp);
5510 DeleteDC(hdc);
5511 ReleaseDC(infoPtr->hwndSelf, hdcOrig);
5513 TRACE("ret=%p\n", dragList);
5515 return dragList;
5519 /***
5520 * DESCRIPTION:
5521 * Removes all listview items and subitems.
5523 * PARAMETER(S):
5524 * [I] infoPtr : valid pointer to the listview structure
5526 * RETURN:
5527 * SUCCESS : TRUE
5528 * FAILURE : FALSE
5530 static BOOL LISTVIEW_DeleteAllItems(LISTVIEW_INFO *infoPtr, BOOL destroy)
5532 HDPA hdpaSubItems = NULL;
5533 BOOL suppress = FALSE;
5534 ITEMHDR *hdrItem;
5535 ITEM_INFO *lpItem;
5536 ITEM_ID *lpID;
5537 INT i, j;
5539 TRACE("()\n");
5541 /* we do it directly, to avoid notifications */
5542 ranges_clear(infoPtr->selectionRanges);
5543 infoPtr->nSelectionMark = -1;
5544 infoPtr->nFocusedItem = -1;
5545 SetRectEmpty(&infoPtr->rcFocus);
5546 /* But we are supposed to leave nHotItem as is! */
5548 /* send LVN_DELETEALLITEMS notification */
5549 if (!(infoPtr->dwStyle & LVS_OWNERDATA) || !destroy)
5551 NMLISTVIEW nmlv;
5553 memset(&nmlv, 0, sizeof(NMLISTVIEW));
5554 nmlv.iItem = -1;
5555 suppress = notify_listview(infoPtr, LVN_DELETEALLITEMS, &nmlv);
5558 for (i = infoPtr->nItemCount - 1; i >= 0; i--)
5560 if (!(infoPtr->dwStyle & LVS_OWNERDATA))
5562 /* send LVN_DELETEITEM notification, if not suppressed
5563 and if it is not a virtual listview */
5564 if (!suppress) notify_deleteitem(infoPtr, i);
5565 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, i);
5566 lpItem = DPA_GetPtr(hdpaSubItems, 0);
5567 /* free id struct */
5568 j = DPA_GetPtrIndex(infoPtr->hdpaItemIds, lpItem->id);
5569 lpID = DPA_GetPtr(infoPtr->hdpaItemIds, j);
5570 DPA_DeletePtr(infoPtr->hdpaItemIds, j);
5571 Free(lpID);
5572 /* both item and subitem start with ITEMHDR header */
5573 for (j = 0; j < DPA_GetPtrCount(hdpaSubItems); j++)
5575 hdrItem = DPA_GetPtr(hdpaSubItems, j);
5576 if (is_text(hdrItem->pszText)) Free(hdrItem->pszText);
5577 Free(hdrItem);
5579 DPA_Destroy(hdpaSubItems);
5580 DPA_DeletePtr(infoPtr->hdpaItems, i);
5582 DPA_DeletePtr(infoPtr->hdpaPosX, i);
5583 DPA_DeletePtr(infoPtr->hdpaPosY, i);
5584 infoPtr->nItemCount --;
5587 if (!destroy)
5589 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
5590 LISTVIEW_UpdateScroll(infoPtr);
5592 LISTVIEW_InvalidateList(infoPtr);
5594 return TRUE;
5597 /***
5598 * DESCRIPTION:
5599 * Scrolls, and updates the columns, when a column is changing width.
5601 * PARAMETER(S):
5602 * [I] infoPtr : valid pointer to the listview structure
5603 * [I] nColumn : column to scroll
5604 * [I] dx : amount of scroll, in pixels
5606 * RETURN:
5607 * None.
5609 static void LISTVIEW_ScrollColumns(LISTVIEW_INFO *infoPtr, INT nColumn, INT dx)
5611 COLUMN_INFO *lpColumnInfo;
5612 RECT rcOld, rcCol;
5613 POINT ptOrigin;
5614 INT nCol;
5615 HDITEMW hdi;
5617 if (nColumn < 0 || DPA_GetPtrCount(infoPtr->hdpaColumns) < 1) return;
5618 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, min(nColumn, DPA_GetPtrCount(infoPtr->hdpaColumns) - 1));
5619 rcCol = lpColumnInfo->rcHeader;
5620 if (nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns))
5621 rcCol.left = rcCol.right;
5623 /* adjust the other columns */
5624 hdi.mask = HDI_ORDER;
5625 if (SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, nColumn, (LPARAM)&hdi))
5627 INT nOrder = hdi.iOrder;
5628 for (nCol = 0; nCol < DPA_GetPtrCount(infoPtr->hdpaColumns); nCol++)
5630 hdi.mask = HDI_ORDER;
5631 SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, nCol, (LPARAM)&hdi);
5632 if (hdi.iOrder >= nOrder) {
5633 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, nCol);
5634 lpColumnInfo->rcHeader.left += dx;
5635 lpColumnInfo->rcHeader.right += dx;
5640 /* do not update screen if not in report mode */
5641 if (!is_redrawing(infoPtr) || infoPtr->uView != LV_VIEW_DETAILS) return;
5643 /* Need to reset the item width when inserting a new column */
5644 infoPtr->nItemWidth += dx;
5646 LISTVIEW_UpdateScroll(infoPtr);
5647 LISTVIEW_GetOrigin(infoPtr, &ptOrigin);
5649 /* scroll to cover the deleted column, and invalidate for redraw */
5650 rcOld = infoPtr->rcList;
5651 rcOld.left = ptOrigin.x + rcCol.left + dx;
5652 ScrollWindowEx(infoPtr->hwndSelf, dx, 0, &rcOld, &rcOld, 0, 0, SW_ERASE | SW_INVALIDATE);
5655 /***
5656 * DESCRIPTION:
5657 * Removes a column from the listview control.
5659 * PARAMETER(S):
5660 * [I] infoPtr : valid pointer to the listview structure
5661 * [I] nColumn : column index
5663 * RETURN:
5664 * SUCCESS : TRUE
5665 * FAILURE : FALSE
5667 static BOOL LISTVIEW_DeleteColumn(LISTVIEW_INFO *infoPtr, INT nColumn)
5669 RECT rcCol;
5671 TRACE("nColumn=%d\n", nColumn);
5673 if (nColumn < 0 || nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns))
5674 return FALSE;
5676 /* While the MSDN specifically says that column zero should not be deleted,
5677 what actually happens is that the column itself is deleted but no items or subitems
5678 are removed.
5681 LISTVIEW_GetHeaderRect(infoPtr, nColumn, &rcCol);
5683 if (!SendMessageW(infoPtr->hwndHeader, HDM_DELETEITEM, nColumn, 0))
5684 return FALSE;
5686 Free(DPA_GetPtr(infoPtr->hdpaColumns, nColumn));
5687 DPA_DeletePtr(infoPtr->hdpaColumns, nColumn);
5689 if (!(infoPtr->dwStyle & LVS_OWNERDATA) && nColumn)
5691 SUBITEM_INFO *lpSubItem, *lpDelItem;
5692 HDPA hdpaSubItems;
5693 INT nItem, nSubItem, i;
5695 for (nItem = 0; nItem < infoPtr->nItemCount; nItem++)
5697 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, nItem);
5698 nSubItem = 0;
5699 lpDelItem = 0;
5700 for (i = 1; i < DPA_GetPtrCount(hdpaSubItems); i++)
5702 lpSubItem = DPA_GetPtr(hdpaSubItems, i);
5703 if (lpSubItem->iSubItem == nColumn)
5705 nSubItem = i;
5706 lpDelItem = lpSubItem;
5708 else if (lpSubItem->iSubItem > nColumn)
5710 lpSubItem->iSubItem--;
5714 /* if we found our subitem, zap it */
5715 if (nSubItem > 0)
5717 /* free string */
5718 if (is_text(lpDelItem->hdr.pszText))
5719 Free(lpDelItem->hdr.pszText);
5721 /* free item */
5722 Free(lpDelItem);
5724 /* free dpa memory */
5725 DPA_DeletePtr(hdpaSubItems, nSubItem);
5730 /* update the other column info */
5731 if(DPA_GetPtrCount(infoPtr->hdpaColumns) == 0)
5732 LISTVIEW_InvalidateList(infoPtr);
5733 else
5734 LISTVIEW_ScrollColumns(infoPtr, nColumn, -(rcCol.right - rcCol.left));
5735 LISTVIEW_UpdateItemSize(infoPtr);
5737 return TRUE;
5740 /***
5741 * DESCRIPTION:
5742 * Invalidates the listview after an item's insertion or deletion.
5744 * PARAMETER(S):
5745 * [I] infoPtr : valid pointer to the listview structure
5746 * [I] nItem : item index
5747 * [I] dir : -1 if deleting, 1 if inserting
5749 * RETURN:
5750 * None
5752 static void LISTVIEW_ScrollOnInsert(LISTVIEW_INFO *infoPtr, INT nItem, INT dir)
5754 INT nPerCol, nItemCol, nItemRow;
5755 RECT rcScroll;
5756 POINT Origin;
5758 /* if we don't refresh, what's the point of scrolling? */
5759 if (!is_redrawing(infoPtr)) return;
5761 assert (abs(dir) == 1);
5763 /* arrange icons if autoarrange is on */
5764 if (is_autoarrange(infoPtr))
5766 BOOL arrange = TRUE;
5767 if (dir < 0 && nItem >= infoPtr->nItemCount) arrange = FALSE;
5768 if (dir > 0 && nItem == infoPtr->nItemCount - 1) arrange = FALSE;
5769 if (arrange) LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
5772 /* scrollbars need updating */
5773 LISTVIEW_UpdateScroll(infoPtr);
5775 /* figure out the item's position */
5776 if (infoPtr->uView == LV_VIEW_DETAILS)
5777 nPerCol = infoPtr->nItemCount + 1;
5778 else if (infoPtr->uView == LV_VIEW_LIST)
5779 nPerCol = LISTVIEW_GetCountPerColumn(infoPtr);
5780 else /* LV_VIEW_ICON, or LV_VIEW_SMALLICON */
5781 return;
5783 nItemCol = nItem / nPerCol;
5784 nItemRow = nItem % nPerCol;
5785 LISTVIEW_GetOrigin(infoPtr, &Origin);
5787 /* move the items below up a slot */
5788 rcScroll.left = nItemCol * infoPtr->nItemWidth;
5789 rcScroll.top = nItemRow * infoPtr->nItemHeight;
5790 rcScroll.right = rcScroll.left + infoPtr->nItemWidth;
5791 rcScroll.bottom = nPerCol * infoPtr->nItemHeight;
5792 OffsetRect(&rcScroll, Origin.x, Origin.y);
5793 TRACE("rcScroll=%s, dx=%d\n", wine_dbgstr_rect(&rcScroll), dir * infoPtr->nItemHeight);
5794 if (IntersectRect(&rcScroll, &rcScroll, &infoPtr->rcList))
5796 TRACE("Invalidating rcScroll=%s, rcList=%s\n", wine_dbgstr_rect(&rcScroll), wine_dbgstr_rect(&infoPtr->rcList));
5797 InvalidateRect(infoPtr->hwndSelf, &rcScroll, TRUE);
5800 /* report has only that column, so we're done */
5801 if (infoPtr->uView == LV_VIEW_DETAILS) return;
5803 /* now for LISTs, we have to deal with the columns to the right */
5804 SetRect(&rcScroll, (nItemCol + 1) * infoPtr->nItemWidth, 0,
5805 (infoPtr->nItemCount / nPerCol + 1) * infoPtr->nItemWidth,
5806 nPerCol * infoPtr->nItemHeight);
5807 OffsetRect(&rcScroll, Origin.x, Origin.y);
5808 if (IntersectRect(&rcScroll, &rcScroll, &infoPtr->rcList))
5809 InvalidateRect(infoPtr->hwndSelf, &rcScroll, TRUE);
5812 /***
5813 * DESCRIPTION:
5814 * Removes an item from the listview control.
5816 * PARAMETER(S):
5817 * [I] infoPtr : valid pointer to the listview structure
5818 * [I] nItem : item index
5820 * RETURN:
5821 * SUCCESS : TRUE
5822 * FAILURE : FALSE
5824 static BOOL LISTVIEW_DeleteItem(LISTVIEW_INFO *infoPtr, INT nItem)
5826 LVITEMW item;
5827 const BOOL is_icon = (infoPtr->uView == LV_VIEW_SMALLICON || infoPtr->uView == LV_VIEW_ICON);
5828 INT focus = infoPtr->nFocusedItem;
5830 TRACE("(nItem=%d)\n", nItem);
5832 if (nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
5834 /* remove selection, and focus */
5835 item.state = 0;
5836 item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
5837 LISTVIEW_SetItemState(infoPtr, nItem, &item);
5839 /* send LVN_DELETEITEM notification. */
5840 if (!notify_deleteitem(infoPtr, nItem)) return FALSE;
5842 /* we need to do this here, because we'll be deleting stuff */
5843 if (is_icon)
5844 LISTVIEW_InvalidateItem(infoPtr, nItem);
5846 if (!(infoPtr->dwStyle & LVS_OWNERDATA))
5848 HDPA hdpaSubItems;
5849 ITEMHDR *hdrItem;
5850 ITEM_INFO *lpItem;
5851 ITEM_ID *lpID;
5852 INT i;
5854 hdpaSubItems = DPA_DeletePtr(infoPtr->hdpaItems, nItem);
5855 lpItem = DPA_GetPtr(hdpaSubItems, 0);
5857 /* free id struct */
5858 i = DPA_GetPtrIndex(infoPtr->hdpaItemIds, lpItem->id);
5859 lpID = DPA_GetPtr(infoPtr->hdpaItemIds, i);
5860 DPA_DeletePtr(infoPtr->hdpaItemIds, i);
5861 Free(lpID);
5862 for (i = 0; i < DPA_GetPtrCount(hdpaSubItems); i++)
5864 hdrItem = DPA_GetPtr(hdpaSubItems, i);
5865 if (is_text(hdrItem->pszText)) Free(hdrItem->pszText);
5866 Free(hdrItem);
5868 DPA_Destroy(hdpaSubItems);
5871 if (is_icon)
5873 DPA_DeletePtr(infoPtr->hdpaPosX, nItem);
5874 DPA_DeletePtr(infoPtr->hdpaPosY, nItem);
5877 infoPtr->nItemCount--;
5878 LISTVIEW_ShiftIndices(infoPtr, nItem, -1);
5879 LISTVIEW_ShiftFocus(infoPtr, focus, nItem, -1);
5881 /* now is the invalidation fun */
5882 if (!is_icon)
5883 LISTVIEW_ScrollOnInsert(infoPtr, nItem, -1);
5884 return TRUE;
5888 /***
5889 * DESCRIPTION:
5890 * Callback implementation for editlabel control
5892 * PARAMETER(S):
5893 * [I] infoPtr : valid pointer to the listview structure
5894 * [I] storeText : store edit box text as item text
5895 * [I] isW : TRUE if psxText is Unicode, FALSE if it's ANSI
5897 * RETURN:
5898 * SUCCESS : TRUE
5899 * FAILURE : FALSE
5901 static BOOL LISTVIEW_EndEditLabelT(LISTVIEW_INFO *infoPtr, BOOL storeText, BOOL isW)
5903 HWND hwndSelf = infoPtr->hwndSelf;
5904 WCHAR szDispText[DISP_TEXT_SIZE] = { 0 };
5905 NMLVDISPINFOW dispInfo;
5906 INT editedItem = infoPtr->nEditLabelItem;
5907 BOOL same;
5908 WCHAR *pszText = NULL;
5909 BOOL res;
5911 if (storeText)
5913 DWORD len = isW ? GetWindowTextLengthW(infoPtr->hwndEdit) : GetWindowTextLengthA(infoPtr->hwndEdit);
5915 if (len++)
5917 if (!(pszText = Alloc(len * (isW ? sizeof(WCHAR) : sizeof(CHAR)))))
5918 return FALSE;
5920 if (isW)
5921 GetWindowTextW(infoPtr->hwndEdit, pszText, len);
5922 else
5923 GetWindowTextA(infoPtr->hwndEdit, (CHAR*)pszText, len);
5927 TRACE("(pszText=%s, isW=%d)\n", debugtext_t(pszText, isW), isW);
5929 ZeroMemory(&dispInfo, sizeof(dispInfo));
5930 dispInfo.item.mask = LVIF_PARAM | LVIF_STATE | LVIF_TEXT;
5931 dispInfo.item.iItem = editedItem;
5932 dispInfo.item.iSubItem = 0;
5933 dispInfo.item.stateMask = ~0;
5934 dispInfo.item.pszText = szDispText;
5935 dispInfo.item.cchTextMax = DISP_TEXT_SIZE;
5936 if (!LISTVIEW_GetItemT(infoPtr, &dispInfo.item, isW))
5938 res = FALSE;
5939 goto cleanup;
5942 if (isW)
5943 same = (lstrcmpW(dispInfo.item.pszText, pszText) == 0);
5944 else
5946 LPWSTR tmp = textdupTtoW(pszText, FALSE);
5947 same = (lstrcmpW(dispInfo.item.pszText, tmp) == 0);
5948 textfreeT(tmp, FALSE);
5951 /* add the text from the edit in */
5952 dispInfo.item.mask |= LVIF_TEXT;
5953 dispInfo.item.pszText = same ? NULL : pszText;
5954 dispInfo.item.cchTextMax = textlenT(dispInfo.item.pszText, isW);
5956 infoPtr->notify_mask &= ~NOTIFY_MASK_END_LABEL_EDIT;
5958 /* Do we need to update the Item Text */
5959 res = notify_dispinfoT(infoPtr, LVN_ENDLABELEDITW, &dispInfo, isW);
5961 infoPtr->notify_mask |= NOTIFY_MASK_END_LABEL_EDIT;
5963 infoPtr->nEditLabelItem = -1;
5964 infoPtr->hwndEdit = 0;
5966 if (!res) goto cleanup;
5968 if (!IsWindow(hwndSelf))
5970 res = FALSE;
5971 goto cleanup;
5973 if (!pszText) return TRUE;
5974 if (same)
5976 res = TRUE;
5977 goto cleanup;
5980 if (!(infoPtr->dwStyle & LVS_OWNERDATA))
5982 HDPA hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, editedItem);
5983 ITEM_INFO* lpItem = DPA_GetPtr(hdpaSubItems, 0);
5984 if (lpItem && lpItem->hdr.pszText == LPSTR_TEXTCALLBACKW)
5986 LISTVIEW_InvalidateItem(infoPtr, editedItem);
5987 res = TRUE;
5988 goto cleanup;
5992 ZeroMemory(&dispInfo, sizeof(dispInfo));
5993 dispInfo.item.mask = LVIF_TEXT;
5994 dispInfo.item.iItem = editedItem;
5995 dispInfo.item.iSubItem = 0;
5996 dispInfo.item.pszText = pszText;
5997 dispInfo.item.cchTextMax = textlenT(pszText, isW);
5998 res = LISTVIEW_SetItemT(infoPtr, &dispInfo.item, isW);
6000 cleanup:
6001 Free(pszText);
6003 return res;
6006 static LRESULT EditLblWndProcT(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL isW)
6008 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)GetWindowLongPtrW(GetParent(hwnd), 0);
6009 BOOL save = TRUE;
6011 TRACE("hwnd %p, uMsg %x, wParam %Ix, lParam %Ix, isW %d\n", hwnd, uMsg, wParam, lParam, isW);
6013 switch (uMsg)
6015 case WM_GETDLGCODE:
6016 return DLGC_WANTARROWS | DLGC_WANTALLKEYS;
6018 case WM_DESTROY:
6020 WNDPROC editProc = infoPtr->EditWndProc;
6021 infoPtr->EditWndProc = 0;
6022 SetWindowLongPtrW(hwnd, GWLP_WNDPROC, (DWORD_PTR)editProc);
6023 return CallWindowProcT(editProc, hwnd, uMsg, wParam, lParam, isW);
6026 case WM_KEYDOWN:
6027 if (VK_ESCAPE == (INT)wParam)
6029 save = FALSE;
6030 break;
6032 else if (VK_RETURN == (INT)wParam)
6033 break;
6035 default:
6036 return CallWindowProcT(infoPtr->EditWndProc, hwnd, uMsg, wParam, lParam, isW);
6039 /* kill the edit */
6040 if (infoPtr->hwndEdit)
6041 LISTVIEW_EndEditLabelT(infoPtr, save, isW);
6043 SendMessageW(hwnd, WM_CLOSE, 0, 0);
6044 return 0;
6047 static LRESULT CALLBACK EditLblWndProcW(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
6049 return EditLblWndProcT(hwnd, uMsg, wParam, lParam, TRUE);
6052 static LRESULT CALLBACK EditLblWndProcA(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
6054 return EditLblWndProcT(hwnd, uMsg, wParam, lParam, FALSE);
6057 static HWND CreateEditLabelT(LISTVIEW_INFO *infoPtr, LPCWSTR text, BOOL isW)
6059 static const DWORD style = WS_CHILDWINDOW|WS_CLIPSIBLINGS|ES_LEFT|ES_AUTOHSCROLL|WS_BORDER|WS_VISIBLE;
6060 HINSTANCE hinst = (HINSTANCE)GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_HINSTANCE);
6061 HWND hedit;
6063 TRACE("(%p, text=%s, isW=%d)\n", infoPtr, debugtext_t(text, isW), isW);
6065 /* window will be resized and positioned after LVN_BEGINLABELEDIT */
6066 if (isW)
6067 hedit = CreateWindowW(WC_EDITW, text, style, 0, 0, 0, 0, infoPtr->hwndSelf, 0, hinst, 0);
6068 else
6069 hedit = CreateWindowA(WC_EDITA, (LPCSTR)text, style, 0, 0, 0, 0, infoPtr->hwndSelf, 0, hinst, 0);
6071 if (!hedit) return 0;
6073 infoPtr->EditWndProc = (WNDPROC)
6074 (isW ? SetWindowLongPtrW(hedit, GWLP_WNDPROC, (DWORD_PTR)EditLblWndProcW) :
6075 SetWindowLongPtrA(hedit, GWLP_WNDPROC, (DWORD_PTR)EditLblWndProcA) );
6077 SendMessageW(hedit, WM_SETFONT, (WPARAM)infoPtr->hFont, FALSE);
6078 SendMessageW(hedit, EM_SETLIMITTEXT, DISP_TEXT_SIZE-1, 0);
6080 return hedit;
6083 static HWND LISTVIEW_EditLabelT(LISTVIEW_INFO *infoPtr, INT nItem, BOOL isW)
6085 WCHAR disptextW[DISP_TEXT_SIZE] = { 0 };
6086 HWND hwndSelf = infoPtr->hwndSelf;
6087 NMLVDISPINFOW dispInfo;
6088 HFONT hOldFont = NULL;
6089 TEXTMETRICW tm;
6090 RECT rect;
6091 SIZE sz;
6092 HDC hdc;
6094 TRACE("(nItem=%d, isW=%d)\n", nItem, isW);
6096 if (~infoPtr->dwStyle & LVS_EDITLABELS) return 0;
6098 /* remove existing edit box */
6099 if (infoPtr->hwndEdit)
6101 SetFocus(infoPtr->hwndSelf);
6102 infoPtr->hwndEdit = 0;
6105 if (nItem < 0 || nItem >= infoPtr->nItemCount) return 0;
6107 infoPtr->nEditLabelItem = nItem;
6109 LISTVIEW_SetSelection(infoPtr, nItem);
6110 LISTVIEW_SetItemFocus(infoPtr, nItem);
6111 LISTVIEW_InvalidateItem(infoPtr, nItem);
6113 rect.left = LVIR_LABEL;
6114 if (!LISTVIEW_GetItemRect(infoPtr, nItem, &rect)) return 0;
6116 ZeroMemory(&dispInfo, sizeof(dispInfo));
6117 dispInfo.item.mask = LVIF_PARAM | LVIF_STATE | LVIF_TEXT;
6118 dispInfo.item.iItem = nItem;
6119 dispInfo.item.iSubItem = 0;
6120 dispInfo.item.stateMask = ~0;
6121 dispInfo.item.pszText = disptextW;
6122 dispInfo.item.cchTextMax = DISP_TEXT_SIZE;
6123 if (!LISTVIEW_GetItemT(infoPtr, &dispInfo.item, isW)) return 0;
6125 infoPtr->hwndEdit = CreateEditLabelT(infoPtr, dispInfo.item.pszText, isW);
6126 if (!infoPtr->hwndEdit) return 0;
6128 if (notify_dispinfoT(infoPtr, LVN_BEGINLABELEDITW, &dispInfo, isW))
6130 if (!IsWindow(hwndSelf))
6131 return 0;
6132 SendMessageW(infoPtr->hwndEdit, WM_CLOSE, 0, 0);
6133 infoPtr->hwndEdit = 0;
6134 return 0;
6137 TRACE("disp text=%s\n", debugtext_t(dispInfo.item.pszText, isW));
6139 /* position and display edit box */
6140 hdc = GetDC(infoPtr->hwndSelf);
6142 /* select the font to get appropriate metric dimensions */
6143 if (infoPtr->hFont)
6144 hOldFont = SelectObject(hdc, infoPtr->hFont);
6146 /* use real edit box content, it could be altered during LVN_BEGINLABELEDIT notification */
6147 GetWindowTextW(infoPtr->hwndEdit, disptextW, DISP_TEXT_SIZE);
6148 TRACE("edit box text=%s\n", debugstr_w(disptextW));
6150 /* get string length in pixels */
6151 GetTextExtentPoint32W(hdc, disptextW, lstrlenW(disptextW), &sz);
6153 /* add extra spacing for the next character */
6154 GetTextMetricsW(hdc, &tm);
6155 sz.cx += tm.tmMaxCharWidth * 2;
6157 if (infoPtr->hFont)
6158 SelectObject(hdc, hOldFont);
6160 ReleaseDC(infoPtr->hwndSelf, hdc);
6162 sz.cy = rect.bottom - rect.top + 2;
6163 rect.left -= 2;
6164 rect.top -= 1;
6165 TRACE("moving edit (%ld,%ld)-(%ld,%ld)\n", rect.left, rect.top, sz.cx, sz.cy);
6166 MoveWindow(infoPtr->hwndEdit, rect.left, rect.top, sz.cx, sz.cy, FALSE);
6167 ShowWindow(infoPtr->hwndEdit, SW_NORMAL);
6168 SetFocus(infoPtr->hwndEdit);
6169 SendMessageW(infoPtr->hwndEdit, EM_SETSEL, 0, -1);
6170 return infoPtr->hwndEdit;
6174 /***
6175 * DESCRIPTION:
6176 * Ensures the specified item is visible, scrolling into view if necessary.
6178 * PARAMETER(S):
6179 * [I] infoPtr : valid pointer to the listview structure
6180 * [I] nItem : item index
6181 * [I] bPartial : partially or entirely visible
6183 * RETURN:
6184 * SUCCESS : TRUE
6185 * FAILURE : FALSE
6187 static BOOL LISTVIEW_EnsureVisible(LISTVIEW_INFO *infoPtr, INT nItem, BOOL bPartial)
6189 INT nScrollPosHeight = 0;
6190 INT nScrollPosWidth = 0;
6191 INT nHorzAdjust = 0;
6192 INT nVertAdjust = 0;
6193 INT nHorzDiff = 0;
6194 INT nVertDiff = 0;
6195 RECT rcItem, rcTemp;
6197 rcItem.left = LVIR_BOUNDS;
6198 if (!LISTVIEW_GetItemRect(infoPtr, nItem, &rcItem)) return FALSE;
6200 if (bPartial && IntersectRect(&rcTemp, &infoPtr->rcList, &rcItem)) return TRUE;
6202 if (rcItem.left < infoPtr->rcList.left || rcItem.right > infoPtr->rcList.right)
6204 /* scroll left/right, but in LV_VIEW_DETAILS mode */
6205 if (infoPtr->uView == LV_VIEW_LIST)
6206 nScrollPosWidth = infoPtr->nItemWidth;
6207 else if ((infoPtr->uView == LV_VIEW_SMALLICON) || (infoPtr->uView == LV_VIEW_ICON))
6208 nScrollPosWidth = 1;
6210 if (rcItem.left < infoPtr->rcList.left)
6212 nHorzAdjust = -1;
6213 if (infoPtr->uView != LV_VIEW_DETAILS) nHorzDiff = rcItem.left - infoPtr->rcList.left;
6215 else
6217 nHorzAdjust = 1;
6218 if (infoPtr->uView != LV_VIEW_DETAILS) nHorzDiff = rcItem.right - infoPtr->rcList.right;
6222 if (rcItem.top < infoPtr->rcList.top || rcItem.bottom > infoPtr->rcList.bottom)
6224 /* scroll up/down, but not in LVS_LIST mode */
6225 if (infoPtr->uView == LV_VIEW_DETAILS)
6226 nScrollPosHeight = infoPtr->nItemHeight;
6227 else if ((infoPtr->uView == LV_VIEW_ICON) || (infoPtr->uView == LV_VIEW_SMALLICON))
6228 nScrollPosHeight = 1;
6230 if (rcItem.top < infoPtr->rcList.top)
6232 nVertAdjust = -1;
6233 if (infoPtr->uView != LV_VIEW_LIST) nVertDiff = rcItem.top - infoPtr->rcList.top;
6235 else
6237 nVertAdjust = 1;
6238 if (infoPtr->uView != LV_VIEW_LIST) nVertDiff = rcItem.bottom - infoPtr->rcList.bottom;
6242 if (!nScrollPosWidth && !nScrollPosHeight) return TRUE;
6244 if (nScrollPosWidth)
6246 INT diff = nHorzDiff / nScrollPosWidth;
6247 if (nHorzDiff % nScrollPosWidth) diff += nHorzAdjust;
6248 LISTVIEW_HScroll(infoPtr, SB_INTERNAL, diff);
6251 if (nScrollPosHeight)
6253 INT diff = nVertDiff / nScrollPosHeight;
6254 if (nVertDiff % nScrollPosHeight) diff += nVertAdjust;
6255 LISTVIEW_VScroll(infoPtr, SB_INTERNAL, diff);
6258 return TRUE;
6261 /***
6262 * DESCRIPTION:
6263 * Searches for an item with specific characteristics.
6265 * PARAMETER(S):
6266 * [I] hwnd : window handle
6267 * [I] nStart : base item index
6268 * [I] lpFindInfo : item information to look for
6270 * RETURN:
6271 * SUCCESS : index of item
6272 * FAILURE : -1
6274 static INT LISTVIEW_FindItemW(const LISTVIEW_INFO *infoPtr, INT nStart,
6275 const LVFINDINFOW *lpFindInfo)
6277 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
6278 BOOL bWrap = FALSE, bNearest = FALSE;
6279 INT nItem = nStart + 1, nLast = infoPtr->nItemCount, nNearestItem = -1;
6280 ULONG xdist, ydist, dist, mindist = 0x7fffffff;
6281 POINT Position, Destination;
6282 int search_len = 0;
6283 LVITEMW lvItem;
6285 /* Search in virtual listviews should be done by application, not by
6286 listview control, so we just send LVN_ODFINDITEMW and return the result */
6287 if (infoPtr->dwStyle & LVS_OWNERDATA)
6289 NMLVFINDITEMW nmlv;
6291 nmlv.iStart = nStart;
6292 nmlv.lvfi = *lpFindInfo;
6293 return notify_hdr(infoPtr, LVN_ODFINDITEMW, (LPNMHDR)&nmlv.hdr);
6296 if (!lpFindInfo || nItem < 0) return -1;
6298 lvItem.mask = 0;
6299 if (lpFindInfo->flags & (LVFI_STRING | LVFI_PARTIAL | LVFI_SUBSTRING))
6301 lvItem.mask |= LVIF_TEXT;
6302 lvItem.pszText = szDispText;
6303 lvItem.cchTextMax = DISP_TEXT_SIZE;
6306 if (lpFindInfo->flags & LVFI_WRAP)
6307 bWrap = TRUE;
6309 if ((lpFindInfo->flags & LVFI_NEARESTXY) &&
6310 (infoPtr->uView == LV_VIEW_ICON || infoPtr->uView == LV_VIEW_SMALLICON))
6312 POINT Origin;
6313 RECT rcArea;
6315 LISTVIEW_GetOrigin(infoPtr, &Origin);
6316 Destination.x = lpFindInfo->pt.x - Origin.x;
6317 Destination.y = lpFindInfo->pt.y - Origin.y;
6318 switch(lpFindInfo->vkDirection)
6320 case VK_DOWN: Destination.y += infoPtr->nItemHeight; break;
6321 case VK_UP: Destination.y -= infoPtr->nItemHeight; break;
6322 case VK_RIGHT: Destination.x += infoPtr->nItemWidth; break;
6323 case VK_LEFT: Destination.x -= infoPtr->nItemWidth; break;
6324 case VK_HOME: Destination.x = Destination.y = 0; break;
6325 case VK_NEXT: Destination.y += infoPtr->rcList.bottom - infoPtr->rcList.top; break;
6326 case VK_PRIOR: Destination.y -= infoPtr->rcList.bottom - infoPtr->rcList.top; break;
6327 case VK_END:
6328 LISTVIEW_GetAreaRect(infoPtr, &rcArea);
6329 Destination.x = rcArea.right;
6330 Destination.y = rcArea.bottom;
6331 break;
6332 default: ERR("Unknown vkDirection=%d\n", lpFindInfo->vkDirection);
6334 bNearest = TRUE;
6336 else Destination.x = Destination.y = 0;
6338 /* if LVFI_PARAM is specified, all other flags are ignored */
6339 if (lpFindInfo->flags & LVFI_PARAM)
6341 lvItem.mask |= LVIF_PARAM;
6342 bNearest = FALSE;
6343 lvItem.mask &= ~LVIF_TEXT;
6346 nItem = bNearest ? -1 : nStart + 1;
6348 if (lpFindInfo->flags & (LVFI_PARTIAL | LVFI_SUBSTRING))
6349 search_len = lstrlenW(lpFindInfo->psz);
6351 again:
6352 for (; nItem < nLast; nItem++)
6354 lvItem.iItem = nItem;
6355 lvItem.iSubItem = 0;
6356 lvItem.pszText = szDispText;
6357 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) continue;
6359 if (lvItem.mask & LVIF_PARAM)
6361 if (lpFindInfo->lParam == lvItem.lParam)
6362 return nItem;
6363 else
6364 continue;
6367 if (lvItem.mask & LVIF_TEXT)
6369 if (lpFindInfo->flags & (LVFI_PARTIAL | LVFI_SUBSTRING))
6371 if (StrCmpNIW(lvItem.pszText, lpFindInfo->psz, search_len)) continue;
6373 else
6375 if (StrCmpIW(lvItem.pszText, lpFindInfo->psz)) continue;
6379 if (!bNearest) return nItem;
6381 /* This is very inefficient. To do a good job here,
6382 * we need a sorted array of (x,y) item positions */
6383 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position);
6385 /* compute the distance^2 to the destination */
6386 xdist = Destination.x - Position.x;
6387 ydist = Destination.y - Position.y;
6388 dist = xdist * xdist + ydist * ydist;
6390 /* remember the distance, and item if it's closer */
6391 if (dist < mindist)
6393 mindist = dist;
6394 nNearestItem = nItem;
6398 if (bWrap)
6400 nItem = 0;
6401 nLast = min(nStart + 1, infoPtr->nItemCount);
6402 bWrap = FALSE;
6403 goto again;
6406 return nNearestItem;
6409 /***
6410 * DESCRIPTION:
6411 * Searches for an item with specific characteristics.
6413 * PARAMETER(S):
6414 * [I] hwnd : window handle
6415 * [I] nStart : base item index
6416 * [I] lpFindInfo : item information to look for
6418 * RETURN:
6419 * SUCCESS : index of item
6420 * FAILURE : -1
6422 static INT LISTVIEW_FindItemA(const LISTVIEW_INFO *infoPtr, INT nStart,
6423 const LVFINDINFOA *lpFindInfo)
6425 LVFINDINFOW fiw;
6426 INT res;
6427 LPWSTR strW = NULL;
6429 memcpy(&fiw, lpFindInfo, sizeof(fiw));
6430 if (lpFindInfo->flags & (LVFI_STRING | LVFI_PARTIAL | LVFI_SUBSTRING))
6431 fiw.psz = strW = textdupTtoW((LPCWSTR)lpFindInfo->psz, FALSE);
6432 res = LISTVIEW_FindItemW(infoPtr, nStart, &fiw);
6433 textfreeT(strW, FALSE);
6434 return res;
6437 /***
6438 * DESCRIPTION:
6439 * Retrieves column attributes.
6441 * PARAMETER(S):
6442 * [I] infoPtr : valid pointer to the listview structure
6443 * [I] nColumn : column index
6444 * [IO] lpColumn : column information
6445 * [I] isW : if TRUE, then lpColumn is a LPLVCOLUMNW
6446 * otherwise it is in fact a LPLVCOLUMNA
6448 * RETURN:
6449 * SUCCESS : TRUE
6450 * FAILURE : FALSE
6452 static BOOL LISTVIEW_GetColumnT(const LISTVIEW_INFO *infoPtr, INT nColumn, LPLVCOLUMNW lpColumn, BOOL isW)
6454 COLUMN_INFO *lpColumnInfo;
6455 HDITEMW hdi;
6457 if (!lpColumn || nColumn < 0 || nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return FALSE;
6458 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, nColumn);
6460 /* initialize memory */
6461 ZeroMemory(&hdi, sizeof(hdi));
6463 if (lpColumn->mask & LVCF_TEXT)
6465 hdi.mask |= HDI_TEXT;
6466 hdi.pszText = lpColumn->pszText;
6467 hdi.cchTextMax = lpColumn->cchTextMax;
6470 if (lpColumn->mask & LVCF_IMAGE)
6471 hdi.mask |= HDI_IMAGE;
6473 if (lpColumn->mask & LVCF_ORDER)
6474 hdi.mask |= HDI_ORDER;
6476 if (lpColumn->mask & LVCF_SUBITEM)
6477 hdi.mask |= HDI_LPARAM;
6479 if (!SendMessageW(infoPtr->hwndHeader, isW ? HDM_GETITEMW : HDM_GETITEMA, nColumn, (LPARAM)&hdi)) return FALSE;
6481 if (lpColumn->mask & LVCF_FMT)
6482 lpColumn->fmt = lpColumnInfo->fmt;
6484 if (lpColumn->mask & LVCF_WIDTH)
6485 lpColumn->cx = lpColumnInfo->rcHeader.right - lpColumnInfo->rcHeader.left;
6487 if (lpColumn->mask & LVCF_IMAGE)
6488 lpColumn->iImage = hdi.iImage;
6490 if (lpColumn->mask & LVCF_ORDER)
6491 lpColumn->iOrder = hdi.iOrder;
6493 if (lpColumn->mask & LVCF_SUBITEM)
6494 lpColumn->iSubItem = hdi.lParam;
6496 if (lpColumn->mask & LVCF_MINWIDTH)
6497 lpColumn->cxMin = lpColumnInfo->cxMin;
6499 return TRUE;
6502 static inline BOOL LISTVIEW_GetColumnOrderArray(const LISTVIEW_INFO *infoPtr, INT iCount, LPINT lpiArray)
6504 if (!infoPtr->hwndHeader) return FALSE;
6505 return SendMessageW(infoPtr->hwndHeader, HDM_GETORDERARRAY, iCount, (LPARAM)lpiArray);
6508 /***
6509 * DESCRIPTION:
6510 * Retrieves the column width.
6512 * PARAMETER(S):
6513 * [I] infoPtr : valid pointer to the listview structure
6514 * [I] int : column index
6516 * RETURN:
6517 * SUCCESS : column width
6518 * FAILURE : zero
6520 static INT LISTVIEW_GetColumnWidth(const LISTVIEW_INFO *infoPtr, INT nColumn)
6522 INT nColumnWidth = 0;
6523 HDITEMW hdItem;
6525 TRACE("nColumn=%d\n", nColumn);
6527 /* we have a 'column' in LIST and REPORT mode only */
6528 switch(infoPtr->uView)
6530 case LV_VIEW_LIST:
6531 nColumnWidth = infoPtr->nItemWidth;
6532 break;
6533 case LV_VIEW_DETAILS:
6534 /* We are not using LISTVIEW_GetHeaderRect as this data is updated only after a HDN_ITEMCHANGED.
6535 * There is an application that subclasses the listview, calls LVM_GETCOLUMNWIDTH in the
6536 * HDN_ITEMCHANGED handler and goes into infinite recursion if it receives old data.
6538 hdItem.mask = HDI_WIDTH;
6539 if (!SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, nColumn, (LPARAM)&hdItem))
6541 WARN("(%p): HDM_GETITEMW failed for item %d\n", infoPtr->hwndSelf, nColumn);
6542 return 0;
6544 nColumnWidth = hdItem.cxy;
6545 break;
6548 TRACE("nColumnWidth=%d\n", nColumnWidth);
6549 return nColumnWidth;
6552 /***
6553 * DESCRIPTION:
6554 * In list or report display mode, retrieves the number of items that can fit
6555 * vertically in the visible area. In icon or small icon display mode,
6556 * retrieves the total number of visible items.
6558 * PARAMETER(S):
6559 * [I] infoPtr : valid pointer to the listview structure
6561 * RETURN:
6562 * Number of fully visible items.
6564 static INT LISTVIEW_GetCountPerPage(const LISTVIEW_INFO *infoPtr)
6566 switch (infoPtr->uView)
6568 case LV_VIEW_ICON:
6569 case LV_VIEW_SMALLICON:
6570 return infoPtr->nItemCount;
6571 case LV_VIEW_DETAILS:
6572 return LISTVIEW_GetCountPerColumn(infoPtr);
6573 case LV_VIEW_LIST:
6574 return LISTVIEW_GetCountPerRow(infoPtr) * LISTVIEW_GetCountPerColumn(infoPtr);
6576 assert(FALSE);
6577 return 0;
6580 /***
6581 * DESCRIPTION:
6582 * Retrieves an image list handle.
6584 * PARAMETER(S):
6585 * [I] infoPtr : valid pointer to the listview structure
6586 * [I] nImageList : image list identifier
6588 * RETURN:
6589 * SUCCESS : image list handle
6590 * FAILURE : NULL
6592 static HIMAGELIST LISTVIEW_GetImageList(const LISTVIEW_INFO *infoPtr, INT nImageList)
6594 switch (nImageList)
6596 case LVSIL_NORMAL: return infoPtr->himlNormal;
6597 case LVSIL_SMALL: return infoPtr->himlSmall;
6598 case LVSIL_STATE: return infoPtr->himlState;
6599 case LVSIL_GROUPHEADER:
6600 FIXME("LVSIL_GROUPHEADER not supported\n");
6601 break;
6602 default:
6603 WARN("got unknown imagelist index - %d\n", nImageList);
6605 return NULL;
6608 /* LISTVIEW_GetISearchString */
6610 /***
6611 * DESCRIPTION:
6612 * Retrieves item attributes.
6614 * PARAMETER(S):
6615 * [I] hwnd : window handle
6616 * [IO] lpLVItem : item info
6617 * [I] isW : if TRUE, then lpLVItem is a LPLVITEMW,
6618 * if FALSE, then lpLVItem is a LPLVITEMA.
6620 * NOTE:
6621 * This is the internal 'GetItem' interface -- it tries to
6622 * be smart and avoid text copies, if possible, by modifying
6623 * lpLVItem->pszText to point to the text string. Please note
6624 * that this is not always possible (e.g. OWNERDATA), so on
6625 * entry you *must* supply valid values for pszText, and cchTextMax.
6626 * The only difference to the documented interface is that upon
6627 * return, you should use *only* the lpLVItem->pszText, rather than
6628 * the buffer pointer you provided on input. Most code already does
6629 * that, so it's not a problem.
6630 * For the two cases when the text must be copied (that is,
6631 * for LVM_GETITEM, and LVM_GETITEMTEXT), use LISTVIEW_GetItemExtT.
6633 * RETURN:
6634 * SUCCESS : TRUE
6635 * FAILURE : FALSE
6637 static BOOL LISTVIEW_GetItemT(const LISTVIEW_INFO *infoPtr, LPLVITEMW lpLVItem, BOOL isW)
6639 ITEMHDR callbackHdr = { LPSTR_TEXTCALLBACKW, I_IMAGECALLBACK };
6640 BOOL is_subitem_invalid = FALSE;
6641 NMLVDISPINFOW dispInfo;
6642 ITEM_INFO *lpItem;
6643 ITEMHDR* pItemHdr;
6644 HDPA hdpaSubItems;
6646 TRACE("(item=%s, isW=%d)\n", debuglvitem_t(lpLVItem, isW), isW);
6648 if (!lpLVItem || lpLVItem->iItem < 0 || lpLVItem->iItem >= infoPtr->nItemCount)
6649 return FALSE;
6651 if (lpLVItem->mask == 0) return TRUE;
6652 TRACE("mask=%x\n", lpLVItem->mask);
6654 if (lpLVItem->iSubItem && (lpLVItem->mask & LVIF_STATE))
6655 lpLVItem->state = 0;
6657 /* a quick optimization if all we're asked is the focus state
6658 * these queries are worth optimising since they are common,
6659 * and can be answered in constant time, without the heavy accesses */
6660 if ( (lpLVItem->mask == LVIF_STATE) && (lpLVItem->stateMask == LVIS_FOCUSED) &&
6661 !(infoPtr->uCallbackMask & LVIS_FOCUSED) )
6663 lpLVItem->state = 0;
6664 if (infoPtr->nFocusedItem == lpLVItem->iItem && !lpLVItem->iSubItem)
6665 lpLVItem->state |= LVIS_FOCUSED;
6666 return TRUE;
6669 ZeroMemory(&dispInfo, sizeof(dispInfo));
6671 /* if the app stores all the data, handle it separately */
6672 if (infoPtr->dwStyle & LVS_OWNERDATA)
6674 dispInfo.item.state = 0;
6676 /* apparently, we should not callback for lParam in LVS_OWNERDATA */
6677 if ((lpLVItem->mask & ~(LVIF_STATE | LVIF_PARAM)) ||
6678 ((lpLVItem->mask & LVIF_STATE) && (infoPtr->uCallbackMask & lpLVItem->stateMask)))
6680 UINT mask = lpLVItem->mask;
6682 /* NOTE: copy only fields which we _know_ are initialized, some apps
6683 * depend on the uninitialized fields being 0 */
6684 dispInfo.item.mask = lpLVItem->mask & ~LVIF_PARAM;
6685 dispInfo.item.iItem = lpLVItem->iItem;
6686 dispInfo.item.iSubItem = lpLVItem->iSubItem;
6687 if (lpLVItem->mask & LVIF_TEXT)
6689 if (lpLVItem->mask & LVIF_NORECOMPUTE)
6690 /* reset mask */
6691 dispInfo.item.mask &= ~(LVIF_TEXT | LVIF_NORECOMPUTE);
6692 else
6694 dispInfo.item.pszText = lpLVItem->pszText;
6695 dispInfo.item.cchTextMax = lpLVItem->cchTextMax;
6698 if (lpLVItem->mask & LVIF_STATE)
6699 dispInfo.item.stateMask = lpLVItem->stateMask & infoPtr->uCallbackMask;
6700 /* could be zeroed on LVIF_NORECOMPUTE case */
6701 if (dispInfo.item.mask)
6703 notify_dispinfoT(infoPtr, LVN_GETDISPINFOW, &dispInfo, isW);
6704 dispInfo.item.stateMask = lpLVItem->stateMask;
6705 if (lpLVItem->mask & (LVIF_GROUPID|LVIF_COLUMNS))
6707 /* full size structure expected - _WIN32IE >= 0x560 */
6708 *lpLVItem = dispInfo.item;
6710 else if (lpLVItem->mask & LVIF_INDENT)
6712 /* indent member expected - _WIN32IE >= 0x300 */
6713 memcpy(lpLVItem, &dispInfo.item, offsetof( LVITEMW, iGroupId ));
6715 else
6717 /* minimal structure expected */
6718 memcpy(lpLVItem, &dispInfo.item, offsetof( LVITEMW, iIndent ));
6720 lpLVItem->mask = mask;
6721 TRACE(" getdispinfo(1):lpLVItem=%s\n", debuglvitem_t(lpLVItem, isW));
6725 /* make sure lParam is zeroed out */
6726 if (lpLVItem->mask & LVIF_PARAM) lpLVItem->lParam = 0;
6728 /* callback marked pointer required here */
6729 if ((lpLVItem->mask & LVIF_TEXT) && (lpLVItem->mask & LVIF_NORECOMPUTE))
6730 lpLVItem->pszText = LPSTR_TEXTCALLBACKW;
6732 /* we store only a little state, so if we're not asked, we're done */
6733 if (!(lpLVItem->mask & LVIF_STATE) || lpLVItem->iSubItem) return TRUE;
6735 /* if focus is handled by us, report it */
6736 if ( lpLVItem->stateMask & ~infoPtr->uCallbackMask & LVIS_FOCUSED )
6738 lpLVItem->state &= ~LVIS_FOCUSED;
6739 if (infoPtr->nFocusedItem == lpLVItem->iItem)
6740 lpLVItem->state |= LVIS_FOCUSED;
6743 /* and do the same for selection, if we handle it */
6744 if ( lpLVItem->stateMask & ~infoPtr->uCallbackMask & LVIS_SELECTED )
6746 lpLVItem->state &= ~LVIS_SELECTED;
6747 if (ranges_contain(infoPtr->selectionRanges, lpLVItem->iItem))
6748 lpLVItem->state |= LVIS_SELECTED;
6751 return TRUE;
6754 /* find the item and subitem structures before we proceed */
6755 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, lpLVItem->iItem);
6756 lpItem = DPA_GetPtr(hdpaSubItems, 0);
6757 assert (lpItem);
6759 if (lpLVItem->iSubItem)
6761 SUBITEM_INFO *lpSubItem = LISTVIEW_GetSubItemPtr(hdpaSubItems, lpLVItem->iSubItem);
6762 if (lpSubItem)
6763 pItemHdr = &lpSubItem->hdr;
6764 else
6766 pItemHdr = &callbackHdr;
6767 is_subitem_invalid = TRUE;
6770 else
6771 pItemHdr = &lpItem->hdr;
6773 /* Do we need to query the state from the app? */
6774 if ((lpLVItem->mask & LVIF_STATE) && infoPtr->uCallbackMask && (!lpLVItem->iSubItem || is_subitem_invalid))
6776 dispInfo.item.mask |= LVIF_STATE;
6777 dispInfo.item.stateMask = infoPtr->uCallbackMask;
6780 /* Do we need to enquire about the image? */
6781 if ((lpLVItem->mask & LVIF_IMAGE) && pItemHdr->iImage == I_IMAGECALLBACK &&
6782 (!lpLVItem->iSubItem || (infoPtr->dwLvExStyle & LVS_EX_SUBITEMIMAGES)))
6784 dispInfo.item.mask |= LVIF_IMAGE;
6785 dispInfo.item.iImage = I_IMAGECALLBACK;
6788 /* Only items support indentation */
6789 if ((lpLVItem->mask & LVIF_INDENT) && lpItem->iIndent == I_INDENTCALLBACK && !lpLVItem->iSubItem)
6791 dispInfo.item.mask |= LVIF_INDENT;
6792 dispInfo.item.iIndent = I_INDENTCALLBACK;
6795 /* Apps depend on calling back for text if it is NULL or LPSTR_TEXTCALLBACKW */
6796 if ((lpLVItem->mask & LVIF_TEXT) && !(lpLVItem->mask & LVIF_NORECOMPUTE) &&
6797 !is_text(pItemHdr->pszText))
6799 dispInfo.item.mask |= LVIF_TEXT;
6800 dispInfo.item.pszText = lpLVItem->pszText;
6801 dispInfo.item.cchTextMax = lpLVItem->cchTextMax;
6802 if (dispInfo.item.pszText && dispInfo.item.cchTextMax > 0)
6803 *dispInfo.item.pszText = '\0';
6806 /* If we don't have all the requested info, query the application */
6807 if (dispInfo.item.mask)
6809 dispInfo.item.iItem = lpLVItem->iItem;
6810 dispInfo.item.iSubItem = lpLVItem->iSubItem;
6811 dispInfo.item.lParam = lpItem->lParam;
6812 notify_dispinfoT(infoPtr, LVN_GETDISPINFOW, &dispInfo, isW);
6813 TRACE(" getdispinfo(2):item=%s\n", debuglvitem_t(&dispInfo.item, isW));
6816 /* we should not store values for subitems */
6817 if (lpLVItem->iSubItem) dispInfo.item.mask &= ~LVIF_DI_SETITEM;
6819 /* Now, handle the iImage field */
6820 if (dispInfo.item.mask & LVIF_IMAGE)
6822 lpLVItem->iImage = dispInfo.item.iImage;
6823 if ((dispInfo.item.mask & LVIF_DI_SETITEM) && pItemHdr->iImage == I_IMAGECALLBACK)
6824 pItemHdr->iImage = dispInfo.item.iImage;
6826 else if (lpLVItem->mask & LVIF_IMAGE)
6828 if (!lpLVItem->iSubItem || (infoPtr->dwLvExStyle & LVS_EX_SUBITEMIMAGES))
6829 lpLVItem->iImage = pItemHdr->iImage;
6830 else
6831 lpLVItem->iImage = 0;
6834 /* The pszText field */
6835 if (dispInfo.item.mask & LVIF_TEXT)
6837 if ((dispInfo.item.mask & LVIF_DI_SETITEM) && pItemHdr->pszText)
6838 textsetptrT(&pItemHdr->pszText, dispInfo.item.pszText, isW);
6840 lpLVItem->pszText = dispInfo.item.pszText;
6842 else if (lpLVItem->mask & LVIF_TEXT)
6844 /* if LVN_GETDISPINFO's disabled with LVIF_NORECOMPUTE return callback placeholder */
6845 if (isW || !is_text(pItemHdr->pszText)) lpLVItem->pszText = pItemHdr->pszText;
6846 else textcpynT(lpLVItem->pszText, isW, pItemHdr->pszText, TRUE, lpLVItem->cchTextMax);
6849 /* Next is the lParam field */
6850 if (dispInfo.item.mask & LVIF_PARAM)
6852 lpLVItem->lParam = dispInfo.item.lParam;
6853 if ((dispInfo.item.mask & LVIF_DI_SETITEM))
6854 lpItem->lParam = dispInfo.item.lParam;
6856 else if (lpLVItem->mask & LVIF_PARAM)
6857 lpLVItem->lParam = lpItem->lParam;
6859 /* if this is a subitem, we're done */
6860 if (lpLVItem->iSubItem) return TRUE;
6862 /* ... the state field (this one is different due to uCallbackmask) */
6863 if (lpLVItem->mask & LVIF_STATE)
6865 lpLVItem->state = lpItem->state & lpLVItem->stateMask;
6866 if (dispInfo.item.mask & LVIF_STATE)
6868 lpLVItem->state &= ~dispInfo.item.stateMask;
6869 lpLVItem->state |= (dispInfo.item.state & dispInfo.item.stateMask);
6871 if ( lpLVItem->stateMask & ~infoPtr->uCallbackMask & LVIS_FOCUSED )
6873 lpLVItem->state &= ~LVIS_FOCUSED;
6874 if (infoPtr->nFocusedItem == lpLVItem->iItem)
6875 lpLVItem->state |= LVIS_FOCUSED;
6877 if ( lpLVItem->stateMask & ~infoPtr->uCallbackMask & LVIS_SELECTED )
6879 lpLVItem->state &= ~LVIS_SELECTED;
6880 if (ranges_contain(infoPtr->selectionRanges, lpLVItem->iItem))
6881 lpLVItem->state |= LVIS_SELECTED;
6885 /* and last, but not least, the indent field */
6886 if (dispInfo.item.mask & LVIF_INDENT)
6888 lpLVItem->iIndent = dispInfo.item.iIndent;
6889 if ((dispInfo.item.mask & LVIF_DI_SETITEM) && lpItem->iIndent == I_INDENTCALLBACK)
6890 lpItem->iIndent = dispInfo.item.iIndent;
6892 else if (lpLVItem->mask & LVIF_INDENT)
6894 lpLVItem->iIndent = lpItem->iIndent;
6897 return TRUE;
6900 /***
6901 * DESCRIPTION:
6902 * Retrieves item attributes.
6904 * PARAMETER(S):
6905 * [I] hwnd : window handle
6906 * [IO] lpLVItem : item info
6907 * [I] isW : if TRUE, then lpLVItem is a LPLVITEMW,
6908 * if FALSE, then lpLVItem is a LPLVITEMA.
6910 * NOTE:
6911 * This is the external 'GetItem' interface -- it properly copies
6912 * the text in the provided buffer.
6914 * RETURN:
6915 * SUCCESS : TRUE
6916 * FAILURE : FALSE
6918 static BOOL LISTVIEW_GetItemExtT(const LISTVIEW_INFO *infoPtr, LPLVITEMW lpLVItem, BOOL isW)
6920 LPWSTR pszText;
6921 BOOL bResult;
6923 if (!lpLVItem || lpLVItem->iItem < 0 || lpLVItem->iItem >= infoPtr->nItemCount)
6924 return FALSE;
6926 pszText = lpLVItem->pszText;
6927 bResult = LISTVIEW_GetItemT(infoPtr, lpLVItem, isW);
6928 if (bResult && (lpLVItem->mask & LVIF_TEXT) && lpLVItem->pszText != pszText)
6930 if (lpLVItem->pszText != LPSTR_TEXTCALLBACKW)
6931 textcpynT(pszText, isW, lpLVItem->pszText, isW, lpLVItem->cchTextMax);
6932 else
6933 pszText = LPSTR_TEXTCALLBACKW;
6935 lpLVItem->pszText = pszText;
6937 return bResult;
6941 /***
6942 * DESCRIPTION:
6943 * Retrieves the position (upper-left) of the listview control item.
6944 * Note that for LVS_ICON style, the upper-left is that of the icon
6945 * and not the bounding box.
6947 * PARAMETER(S):
6948 * [I] infoPtr : valid pointer to the listview structure
6949 * [I] nItem : item index
6950 * [O] lpptPosition : coordinate information
6952 * RETURN:
6953 * SUCCESS : TRUE
6954 * FAILURE : FALSE
6956 static BOOL LISTVIEW_GetItemPosition(const LISTVIEW_INFO *infoPtr, INT nItem, LPPOINT lpptPosition)
6958 POINT Origin;
6960 TRACE("(nItem=%d, lpptPosition=%p)\n", nItem, lpptPosition);
6962 if (!lpptPosition || nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
6964 LISTVIEW_GetOrigin(infoPtr, &Origin);
6965 LISTVIEW_GetItemOrigin(infoPtr, nItem, lpptPosition);
6967 if (infoPtr->uView == LV_VIEW_ICON)
6969 lpptPosition->x += (infoPtr->nItemWidth - infoPtr->iconSize.cx) / 2;
6970 lpptPosition->y += ICON_TOP_PADDING;
6972 lpptPosition->x += Origin.x;
6973 lpptPosition->y += Origin.y;
6975 TRACE (" lpptPosition=%s\n", wine_dbgstr_point(lpptPosition));
6976 return TRUE;
6980 /***
6981 * DESCRIPTION:
6982 * Retrieves the bounding rectangle for a listview control item.
6984 * PARAMETER(S):
6985 * [I] infoPtr : valid pointer to the listview structure
6986 * [I] nItem : item index
6987 * [IO] lprc : bounding rectangle coordinates
6988 * lprc->left specifies the portion of the item for which the bounding
6989 * rectangle will be retrieved.
6991 * LVIR_BOUNDS Returns the bounding rectangle of the entire item,
6992 * including the icon and label.
6994 * * For LVS_ICON
6995 * * Experiment shows that native control returns:
6996 * * width = min (48, length of text line)
6997 * * .left = position.x - (width - iconsize.cx)/2
6998 * * .right = .left + width
6999 * * height = #lines of text * ntmHeight + icon height + 8
7000 * * .top = position.y - 2
7001 * * .bottom = .top + height
7002 * * separation between items .y = itemSpacing.cy - height
7003 * * .x = itemSpacing.cx - width
7004 * LVIR_ICON Returns the bounding rectangle of the icon or small icon.
7006 * * For LVS_ICON
7007 * * Experiment shows that native control returns:
7008 * * width = iconSize.cx + 16
7009 * * .left = position.x - (width - iconsize.cx)/2
7010 * * .right = .left + width
7011 * * height = iconSize.cy + 4
7012 * * .top = position.y - 2
7013 * * .bottom = .top + height
7014 * * separation between items .y = itemSpacing.cy - height
7015 * * .x = itemSpacing.cx - width
7016 * LVIR_LABEL Returns the bounding rectangle of the item text.
7018 * * For LVS_ICON
7019 * * Experiment shows that native control returns:
7020 * * width = text length
7021 * * .left = position.x - width/2
7022 * * .right = .left + width
7023 * * height = ntmH * linecount + 2
7024 * * .top = position.y + iconSize.cy + 6
7025 * * .bottom = .top + height
7026 * * separation between items .y = itemSpacing.cy - height
7027 * * .x = itemSpacing.cx - width
7028 * LVIR_SELECTBOUNDS Returns the union of the LVIR_ICON and LVIR_LABEL
7029 * rectangles, but excludes columns in report view.
7031 * RETURN:
7032 * SUCCESS : TRUE
7033 * FAILURE : FALSE
7035 * NOTES
7036 * Note that the bounding rectangle of the label in the LVS_ICON view depends
7037 * upon whether the window has the focus currently and on whether the item
7038 * is the one with the focus. Ensure that the control's record of which
7039 * item has the focus agrees with the items' records.
7041 static BOOL LISTVIEW_GetItemRect(const LISTVIEW_INFO *infoPtr, INT nItem, LPRECT lprc)
7043 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
7044 BOOL doLabel = TRUE, oversizedBox = FALSE;
7045 POINT Position, Origin;
7046 LVITEMW lvItem;
7047 LONG mode;
7049 TRACE("(hwnd=%p, nItem=%d, lprc=%p)\n", infoPtr->hwndSelf, nItem, lprc);
7051 if (!lprc || nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
7053 LISTVIEW_GetOrigin(infoPtr, &Origin);
7054 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position);
7056 /* Be smart and try to figure out the minimum we have to do */
7057 if (lprc->left == LVIR_ICON) doLabel = FALSE;
7058 if (infoPtr->uView == LV_VIEW_DETAILS && lprc->left == LVIR_BOUNDS) doLabel = FALSE;
7059 if (infoPtr->uView == LV_VIEW_ICON && lprc->left != LVIR_ICON &&
7060 infoPtr->bFocus && LISTVIEW_GetItemState(infoPtr, nItem, LVIS_FOCUSED))
7061 oversizedBox = TRUE;
7063 /* get what we need from the item before hand, so we make
7064 * only one request. This can speed up things, if data
7065 * is stored on the app side */
7066 lvItem.mask = 0;
7067 if (infoPtr->uView == LV_VIEW_DETAILS) lvItem.mask |= LVIF_INDENT;
7068 if (doLabel) lvItem.mask |= LVIF_TEXT;
7069 lvItem.iItem = nItem;
7070 lvItem.iSubItem = 0;
7071 lvItem.pszText = szDispText;
7072 lvItem.cchTextMax = DISP_TEXT_SIZE;
7073 if (lvItem.mask && !LISTVIEW_GetItemW(infoPtr, &lvItem)) return FALSE;
7074 /* we got the state already up, simulate it here, to avoid a reget */
7075 if (infoPtr->uView == LV_VIEW_ICON && (lprc->left != LVIR_ICON))
7077 lvItem.mask |= LVIF_STATE;
7078 lvItem.stateMask = LVIS_FOCUSED;
7079 lvItem.state = (oversizedBox ? LVIS_FOCUSED : 0);
7082 if (infoPtr->uView == LV_VIEW_DETAILS && (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT) && lprc->left == LVIR_SELECTBOUNDS)
7083 lprc->left = LVIR_BOUNDS;
7085 mode = lprc->left;
7086 switch(lprc->left)
7088 case LVIR_ICON:
7089 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, NULL, NULL, lprc, NULL, NULL);
7090 break;
7092 case LVIR_LABEL:
7093 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, NULL, NULL, NULL, NULL, lprc);
7094 break;
7096 case LVIR_BOUNDS:
7097 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, lprc, NULL, NULL, NULL, NULL);
7098 break;
7100 case LVIR_SELECTBOUNDS:
7101 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, NULL, lprc, NULL, NULL, NULL);
7102 break;
7104 default:
7105 WARN("Unknown value: %ld\n", lprc->left);
7106 return FALSE;
7109 if (infoPtr->uView == LV_VIEW_DETAILS)
7111 if (mode != LVIR_BOUNDS)
7112 OffsetRect(lprc, Origin.x + LISTVIEW_GetColumnInfo(infoPtr, 0)->rcHeader.left,
7113 Position.y + Origin.y);
7114 else
7115 OffsetRect(lprc, Origin.x, Position.y + Origin.y);
7117 else
7118 OffsetRect(lprc, Position.x + Origin.x, Position.y + Origin.y);
7120 TRACE(" rect=%s\n", wine_dbgstr_rect(lprc));
7122 return TRUE;
7125 /***
7126 * DESCRIPTION:
7127 * Retrieves the spacing between listview control items.
7129 * PARAMETER(S):
7130 * [I] infoPtr : valid pointer to the listview structure
7131 * [IO] lprc : rectangle to receive the output
7132 * on input, lprc->top = nSubItem
7133 * lprc->left = LVIR_ICON | LVIR_BOUNDS | LVIR_LABEL
7135 * NOTE: for subItem = 0, we should return the bounds of the _entire_ item,
7136 * not only those of the first column.
7138 * RETURN:
7139 * TRUE: success
7140 * FALSE: failure
7142 static BOOL LISTVIEW_GetSubItemRect(const LISTVIEW_INFO *infoPtr, INT item, LPRECT lprc)
7144 RECT rect = { 0, 0, 0, 0 };
7145 POINT origin;
7146 INT y;
7148 if (!lprc) return FALSE;
7150 TRACE("item %d, subitem %ld, type %ld\n", item, lprc->top, lprc->left);
7152 /* Subitem of '0' means item itself, and this works for all control view modes */
7153 if (lprc->top == 0)
7154 return LISTVIEW_GetItemRect(infoPtr, item, lprc);
7156 if (infoPtr->uView != LV_VIEW_DETAILS) return FALSE;
7158 LISTVIEW_GetOrigin(infoPtr, &origin);
7159 /* this works for any item index, no matter if it exists or not */
7160 y = item * infoPtr->nItemHeight + origin.y;
7162 if (infoPtr->hwndHeader && SendMessageW(infoPtr->hwndHeader, HDM_GETITEMRECT, lprc->top, (LPARAM)&rect))
7164 rect.top = 0;
7165 rect.bottom = infoPtr->nItemHeight;
7167 else
7169 /* Native implementation is broken for this case and garbage is left for left and right fields,
7170 we zero them to get predictable output */
7171 lprc->left = lprc->right = lprc->top = 0;
7172 lprc->bottom = infoPtr->nItemHeight;
7173 OffsetRect(lprc, origin.x, y);
7174 TRACE("return rect %s\n", wine_dbgstr_rect(lprc));
7175 return TRUE;
7178 switch (lprc->left)
7180 case LVIR_ICON:
7182 /* it doesn't matter if main item actually has an icon, if imagelist is set icon width is returned */
7183 if (infoPtr->himlSmall)
7184 rect.right = rect.left + infoPtr->iconSize.cx;
7185 else
7186 rect.right = rect.left;
7188 rect.bottom = rect.top + infoPtr->iconSize.cy;
7189 break;
7191 case LVIR_LABEL:
7192 case LVIR_BOUNDS:
7193 break;
7195 default:
7196 ERR("Unknown bounds %ld\n", lprc->left);
7197 return FALSE;
7200 OffsetRect(&rect, origin.x, y);
7201 *lprc = rect;
7202 TRACE("return rect %s\n", wine_dbgstr_rect(lprc));
7204 return TRUE;
7207 /***
7208 * DESCRIPTION:
7209 * Retrieves the spacing between listview control items.
7211 * PARAMETER(S):
7212 * [I] infoPtr : valid pointer to the listview structure
7213 * [I] bSmall : flag for small or large icon
7215 * RETURN:
7216 * Horizontal + vertical spacing
7218 static LONG LISTVIEW_GetItemSpacing(const LISTVIEW_INFO *infoPtr, BOOL bSmall)
7220 LONG lResult;
7222 if (!bSmall)
7224 lResult = MAKELONG(infoPtr->iconSpacing.cx, infoPtr->iconSpacing.cy);
7226 else
7228 if (infoPtr->uView == LV_VIEW_ICON)
7229 lResult = MAKELONG(DEFAULT_COLUMN_WIDTH, GetSystemMetrics(SM_CXSMICON)+HEIGHT_PADDING);
7230 else
7231 lResult = MAKELONG(infoPtr->nItemWidth, infoPtr->nItemHeight);
7233 return lResult;
7236 /***
7237 * DESCRIPTION:
7238 * Retrieves the state of a listview control item.
7240 * PARAMETER(S):
7241 * [I] infoPtr : valid pointer to the listview structure
7242 * [I] nItem : item index
7243 * [I] uMask : state mask
7245 * RETURN:
7246 * State specified by the mask.
7248 static UINT LISTVIEW_GetItemState(const LISTVIEW_INFO *infoPtr, INT nItem, UINT uMask)
7250 LVITEMW lvItem;
7252 if (nItem < 0 || nItem >= infoPtr->nItemCount) return 0;
7254 lvItem.iItem = nItem;
7255 lvItem.iSubItem = 0;
7256 lvItem.mask = LVIF_STATE;
7257 lvItem.stateMask = uMask;
7258 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) return 0;
7260 return lvItem.state & uMask;
7263 /***
7264 * DESCRIPTION:
7265 * Retrieves the text of a listview control item or subitem.
7267 * PARAMETER(S):
7268 * [I] hwnd : window handle
7269 * [I] nItem : item index
7270 * [IO] lpLVItem : item information
7271 * [I] isW : TRUE if lpLVItem is Unicode
7273 * RETURN:
7274 * SUCCESS : string length
7275 * FAILURE : 0
7277 static INT LISTVIEW_GetItemTextT(const LISTVIEW_INFO *infoPtr, INT nItem, LPLVITEMW lpLVItem, BOOL isW)
7279 if (!lpLVItem || nItem < 0 || nItem >= infoPtr->nItemCount) return 0;
7281 lpLVItem->mask = LVIF_TEXT;
7282 lpLVItem->iItem = nItem;
7283 if (!LISTVIEW_GetItemExtT(infoPtr, lpLVItem, isW)) return 0;
7285 return textlenT(lpLVItem->pszText, isW);
7288 /***
7289 * DESCRIPTION:
7290 * Searches for an item based on properties + relationships.
7292 * PARAMETER(S):
7293 * [I] infoPtr : valid pointer to the listview structure
7294 * [I] nItem : item index
7295 * [I] uFlags : relationship flag
7297 * RETURN:
7298 * SUCCESS : item index
7299 * FAILURE : -1
7301 static INT LISTVIEW_GetNextItem(const LISTVIEW_INFO *infoPtr, INT nItem, UINT uFlags)
7303 UINT uMask = 0;
7304 LVFINDINFOW lvFindInfo;
7305 INT nCountPerColumn;
7306 INT nCountPerRow;
7307 INT i;
7309 TRACE("nItem=%d, uFlags=%x, nItemCount=%d\n", nItem, uFlags, infoPtr->nItemCount);
7310 if (nItem < -1 || nItem >= infoPtr->nItemCount) return -1;
7312 ZeroMemory(&lvFindInfo, sizeof(lvFindInfo));
7314 if (uFlags & LVNI_CUT)
7315 uMask |= LVIS_CUT;
7317 if (uFlags & LVNI_DROPHILITED)
7318 uMask |= LVIS_DROPHILITED;
7320 if (uFlags & LVNI_FOCUSED)
7321 uMask |= LVIS_FOCUSED;
7323 if (uFlags & LVNI_SELECTED)
7324 uMask |= LVIS_SELECTED;
7326 /* if we're asked for the focused item, that's only one,
7327 * so it's worth optimizing */
7328 if (uFlags & LVNI_FOCUSED)
7330 if ((LISTVIEW_GetItemState(infoPtr, infoPtr->nFocusedItem, uMask) & uMask) != uMask) return -1;
7331 return (infoPtr->nFocusedItem == nItem) ? -1 : infoPtr->nFocusedItem;
7334 if (uFlags & LVNI_ABOVE)
7336 if ((infoPtr->uView == LV_VIEW_LIST) || (infoPtr->uView == LV_VIEW_DETAILS))
7338 while (nItem >= 0)
7340 nItem--;
7341 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7342 return nItem;
7345 else
7347 /* Special case for autoarrange - move 'til the top of a list */
7348 if (is_autoarrange(infoPtr))
7350 nCountPerRow = LISTVIEW_GetCountPerRow(infoPtr);
7351 while (nItem - nCountPerRow >= 0)
7353 nItem -= nCountPerRow;
7354 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7355 return nItem;
7357 return -1;
7359 lvFindInfo.flags = LVFI_NEARESTXY;
7360 lvFindInfo.vkDirection = VK_UP;
7361 LISTVIEW_GetItemPosition(infoPtr, nItem, &lvFindInfo.pt);
7362 while ((nItem = LISTVIEW_FindItemW(infoPtr, nItem, &lvFindInfo)) != -1)
7364 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7365 return nItem;
7369 else if (uFlags & LVNI_BELOW)
7371 if ((infoPtr->uView == LV_VIEW_LIST) || (infoPtr->uView == LV_VIEW_DETAILS))
7373 while (nItem < infoPtr->nItemCount)
7375 nItem++;
7376 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7377 return nItem;
7380 else
7382 /* Special case for autoarrange - move 'til the bottom of a list */
7383 if (is_autoarrange(infoPtr))
7385 nCountPerRow = LISTVIEW_GetCountPerRow(infoPtr);
7386 while (nItem + nCountPerRow < infoPtr->nItemCount )
7388 nItem += nCountPerRow;
7389 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7390 return nItem;
7392 return -1;
7394 lvFindInfo.flags = LVFI_NEARESTXY;
7395 lvFindInfo.vkDirection = VK_DOWN;
7396 LISTVIEW_GetItemPosition(infoPtr, nItem, &lvFindInfo.pt);
7397 while ((nItem = LISTVIEW_FindItemW(infoPtr, nItem, &lvFindInfo)) != -1)
7399 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7400 return nItem;
7404 else if (uFlags & LVNI_TOLEFT)
7406 if (infoPtr->uView == LV_VIEW_LIST)
7408 nCountPerColumn = LISTVIEW_GetCountPerColumn(infoPtr);
7409 while (nItem - nCountPerColumn >= 0)
7411 nItem -= nCountPerColumn;
7412 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7413 return nItem;
7416 else if ((infoPtr->uView == LV_VIEW_SMALLICON) || (infoPtr->uView == LV_VIEW_ICON))
7418 /* Special case for autoarrange - move 'til the beginning of a row */
7419 if (is_autoarrange(infoPtr))
7421 nCountPerRow = LISTVIEW_GetCountPerRow(infoPtr);
7422 while (nItem % nCountPerRow > 0)
7424 nItem --;
7425 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7426 return nItem;
7428 return -1;
7430 lvFindInfo.flags = LVFI_NEARESTXY;
7431 lvFindInfo.vkDirection = VK_LEFT;
7432 LISTVIEW_GetItemPosition(infoPtr, nItem, &lvFindInfo.pt);
7433 while ((nItem = LISTVIEW_FindItemW(infoPtr, nItem, &lvFindInfo)) != -1)
7435 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7436 return nItem;
7440 else if (uFlags & LVNI_TORIGHT)
7442 if (infoPtr->uView == LV_VIEW_LIST)
7444 nCountPerColumn = LISTVIEW_GetCountPerColumn(infoPtr);
7445 while (nItem + nCountPerColumn < infoPtr->nItemCount)
7447 nItem += nCountPerColumn;
7448 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7449 return nItem;
7452 else if ((infoPtr->uView == LV_VIEW_SMALLICON) || (infoPtr->uView == LV_VIEW_ICON))
7454 /* Special case for autoarrange - move 'til the end of a row */
7455 if (is_autoarrange(infoPtr))
7457 nCountPerRow = LISTVIEW_GetCountPerRow(infoPtr);
7458 while (nItem % nCountPerRow < nCountPerRow - 1 )
7460 nItem ++;
7461 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7462 return nItem;
7464 return -1;
7466 lvFindInfo.flags = LVFI_NEARESTXY;
7467 lvFindInfo.vkDirection = VK_RIGHT;
7468 LISTVIEW_GetItemPosition(infoPtr, nItem, &lvFindInfo.pt);
7469 while ((nItem = LISTVIEW_FindItemW(infoPtr, nItem, &lvFindInfo)) != -1)
7471 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7472 return nItem;
7476 else
7478 nItem++;
7480 /* search by index */
7481 for (i = nItem; i < infoPtr->nItemCount; i++)
7483 if ((LISTVIEW_GetItemState(infoPtr, i, uMask) & uMask) == uMask)
7484 return i;
7488 return -1;
7491 static BOOL LISTVIEW_GetNextItemIndex(const LISTVIEW_INFO *infoPtr, LVITEMINDEX *index, UINT flags)
7493 /* FIXME: specified item group is ignored */
7495 if (!index)
7496 return FALSE;
7498 index->iItem = LISTVIEW_GetNextItem(infoPtr, index->iItem, flags);
7499 return index->iItem != -1;
7502 /* LISTVIEW_GetNumberOfWorkAreas */
7504 /***
7505 * DESCRIPTION:
7506 * Retrieves the origin coordinates when in icon or small icon display mode.
7508 * PARAMETER(S):
7509 * [I] infoPtr : valid pointer to the listview structure
7510 * [O] lpptOrigin : coordinate information
7512 * RETURN:
7513 * None.
7515 static void LISTVIEW_GetOrigin(const LISTVIEW_INFO *infoPtr, LPPOINT lpptOrigin)
7517 INT nHorzPos = 0, nVertPos = 0;
7518 SCROLLINFO scrollInfo;
7520 scrollInfo.cbSize = sizeof(SCROLLINFO);
7521 scrollInfo.fMask = SIF_POS;
7523 if (GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo))
7524 nHorzPos = scrollInfo.nPos;
7525 if (GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo))
7526 nVertPos = scrollInfo.nPos;
7528 TRACE("nHorzPos=%d, nVertPos=%d\n", nHorzPos, nVertPos);
7530 lpptOrigin->x = infoPtr->rcList.left;
7531 lpptOrigin->y = infoPtr->rcList.top;
7532 if (infoPtr->uView == LV_VIEW_LIST)
7533 nHorzPos *= infoPtr->nItemWidth;
7534 else if (infoPtr->uView == LV_VIEW_DETAILS)
7535 nVertPos *= infoPtr->nItemHeight;
7537 lpptOrigin->x -= nHorzPos;
7538 lpptOrigin->y -= nVertPos;
7540 TRACE(" origin=%s\n", wine_dbgstr_point(lpptOrigin));
7543 /***
7544 * DESCRIPTION:
7545 * Retrieves the width of a string.
7547 * PARAMETER(S):
7548 * [I] hwnd : window handle
7549 * [I] lpszText : text string to process
7550 * [I] isW : TRUE if lpszText is Unicode, FALSE otherwise
7552 * RETURN:
7553 * SUCCESS : string width (in pixels)
7554 * FAILURE : zero
7556 static INT LISTVIEW_GetStringWidthT(const LISTVIEW_INFO *infoPtr, LPCWSTR lpszText, BOOL isW)
7558 SIZE stringSize;
7560 stringSize.cx = 0;
7561 if (is_text(lpszText))
7563 HFONT hFont = infoPtr->hFont ? infoPtr->hFont : infoPtr->hDefaultFont;
7564 HDC hdc = GetDC(infoPtr->hwndSelf);
7565 HFONT hOldFont = SelectObject(hdc, hFont);
7567 if (isW)
7568 GetTextExtentPointW(hdc, lpszText, lstrlenW(lpszText), &stringSize);
7569 else
7570 GetTextExtentPointA(hdc, (LPCSTR)lpszText, lstrlenA((LPCSTR)lpszText), &stringSize);
7571 SelectObject(hdc, hOldFont);
7572 ReleaseDC(infoPtr->hwndSelf, hdc);
7574 return stringSize.cx;
7577 /***
7578 * DESCRIPTION:
7579 * Determines which listview item is located at the specified position.
7581 * PARAMETER(S):
7582 * [I] infoPtr : valid pointer to the listview structure
7583 * [IO] lpht : hit test information
7584 * [I] subitem : fill out iSubItem.
7585 * [I] select : return the index only if the hit selects the item
7587 * NOTE:
7588 * (mm 20001022): We must not allow iSubItem to be touched, for
7589 * an app might pass only a structure with space up to iItem!
7590 * (MS Office 97 does that for instance in the file open dialog)
7592 * RETURN:
7593 * SUCCESS : item index
7594 * FAILURE : -1
7596 static INT LISTVIEW_HitTest(const LISTVIEW_INFO *infoPtr, LPLVHITTESTINFO lpht, BOOL subitem, BOOL select)
7598 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
7599 RECT rcBox, rcBounds, rcState, rcIcon, rcLabel, rcSearch;
7600 POINT Origin, Position, opt;
7601 BOOL is_fullrow;
7602 LVITEMW lvItem;
7603 ITERATOR i;
7604 INT iItem;
7606 TRACE("(pt=%s, subitem=%d, select=%d)\n", wine_dbgstr_point(&lpht->pt), subitem, select);
7608 lpht->flags = 0;
7609 lpht->iItem = -1;
7610 if (subitem) lpht->iSubItem = 0;
7612 LISTVIEW_GetOrigin(infoPtr, &Origin);
7614 /* set whole list relation flags */
7615 if (subitem && infoPtr->uView == LV_VIEW_DETAILS)
7617 /* LVM_SUBITEMHITTEST checks left bound of possible client area */
7618 if (infoPtr->rcList.left > lpht->pt.x && Origin.x < lpht->pt.x)
7619 lpht->flags |= LVHT_TOLEFT;
7621 if (lpht->pt.y < infoPtr->rcList.top && lpht->pt.y >= 0)
7622 opt.y = lpht->pt.y + infoPtr->rcList.top;
7623 else
7624 opt.y = lpht->pt.y;
7626 if (infoPtr->rcList.bottom < opt.y)
7627 lpht->flags |= LVHT_BELOW;
7629 else
7631 if (infoPtr->rcList.left > lpht->pt.x)
7632 lpht->flags |= LVHT_TOLEFT;
7633 else if (infoPtr->rcList.right < lpht->pt.x)
7634 lpht->flags |= LVHT_TORIGHT;
7636 if (infoPtr->rcList.top > lpht->pt.y)
7637 lpht->flags |= LVHT_ABOVE;
7638 else if (infoPtr->rcList.bottom < lpht->pt.y)
7639 lpht->flags |= LVHT_BELOW;
7642 /* even if item is invalid try to find subitem */
7643 if (infoPtr->uView == LV_VIEW_DETAILS && subitem)
7645 RECT *pRect;
7646 INT j;
7648 opt.x = lpht->pt.x - Origin.x;
7650 lpht->iSubItem = -1;
7651 for (j = 0; j < DPA_GetPtrCount(infoPtr->hdpaColumns); j++)
7653 pRect = &LISTVIEW_GetColumnInfo(infoPtr, j)->rcHeader;
7655 if ((opt.x >= pRect->left) && (opt.x < pRect->right))
7657 lpht->iSubItem = j;
7658 break;
7661 TRACE("lpht->iSubItem=%d\n", lpht->iSubItem);
7663 /* if we're outside horizontal columns bounds there's nothing to test further */
7664 if (lpht->iSubItem == -1)
7666 lpht->iItem = -1;
7667 lpht->flags = LVHT_NOWHERE;
7668 return -1;
7672 TRACE("lpht->flags=0x%x\n", lpht->flags);
7673 if (lpht->flags) return -1;
7675 lpht->flags |= LVHT_NOWHERE;
7677 /* first deal with the large items */
7678 rcSearch.left = lpht->pt.x;
7679 rcSearch.top = lpht->pt.y;
7680 rcSearch.right = rcSearch.left + 1;
7681 rcSearch.bottom = rcSearch.top + 1;
7683 iterator_frameditems(&i, infoPtr, &rcSearch);
7684 iterator_next(&i); /* go to first item in the sequence */
7685 iItem = i.nItem;
7686 iterator_destroy(&i);
7688 TRACE("lpht->iItem=%d\n", iItem);
7689 if (iItem == -1) return -1;
7691 lvItem.mask = LVIF_STATE | LVIF_TEXT;
7692 if (infoPtr->uView == LV_VIEW_DETAILS) lvItem.mask |= LVIF_INDENT;
7693 lvItem.stateMask = LVIS_STATEIMAGEMASK;
7694 if (infoPtr->uView == LV_VIEW_ICON) lvItem.stateMask |= LVIS_FOCUSED;
7695 lvItem.iItem = iItem;
7696 lvItem.iSubItem = subitem ? lpht->iSubItem : 0;
7697 lvItem.pszText = szDispText;
7698 lvItem.cchTextMax = DISP_TEXT_SIZE;
7699 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) return -1;
7700 if (!infoPtr->bFocus) lvItem.state &= ~LVIS_FOCUSED;
7702 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, &rcBox, NULL, &rcIcon, &rcState, &rcLabel);
7703 LISTVIEW_GetItemOrigin(infoPtr, iItem, &Position);
7704 opt.x = lpht->pt.x - Position.x - Origin.x;
7706 if (lpht->pt.y < infoPtr->rcList.top && lpht->pt.y >= 0)
7707 opt.y = lpht->pt.y - Position.y - Origin.y + infoPtr->rcList.top;
7708 else
7709 opt.y = lpht->pt.y - Position.y - Origin.y;
7711 if (infoPtr->uView == LV_VIEW_DETAILS)
7713 rcBounds = rcBox;
7714 if (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT)
7715 opt.x = lpht->pt.x - Origin.x;
7717 else
7719 UnionRect(&rcBounds, &rcIcon, &rcLabel);
7720 UnionRect(&rcBounds, &rcBounds, &rcState);
7722 TRACE("rcBounds=%s\n", wine_dbgstr_rect(&rcBounds));
7723 if (!PtInRect(&rcBounds, opt)) return -1;
7725 /* That's a special case - row rectangle is used as item rectangle and
7726 returned flags contain all item parts. */
7727 is_fullrow = (infoPtr->uView == LV_VIEW_DETAILS) && ((infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT) || (infoPtr->dwStyle & LVS_OWNERDRAWFIXED));
7729 if (PtInRect(&rcIcon, opt))
7730 lpht->flags |= LVHT_ONITEMICON;
7731 else if (PtInRect(&rcLabel, opt))
7732 lpht->flags |= LVHT_ONITEMLABEL;
7733 else if (infoPtr->himlState && PtInRect(&rcState, opt))
7734 lpht->flags |= LVHT_ONITEMSTATEICON;
7735 if (is_fullrow && !(lpht->flags & LVHT_ONITEM))
7737 lpht->flags = LVHT_ONITEM | LVHT_ABOVE;
7739 if (lpht->flags & LVHT_ONITEM)
7740 lpht->flags &= ~LVHT_NOWHERE;
7741 TRACE("lpht->flags=0x%x\n", lpht->flags);
7743 if (select && !is_fullrow)
7745 if (infoPtr->uView == LV_VIEW_DETAILS)
7747 /* get main item bounds */
7748 lvItem.iSubItem = 0;
7749 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, &rcBox, NULL, &rcIcon, &rcState, &rcLabel);
7750 UnionRect(&rcBounds, &rcIcon, &rcLabel);
7751 UnionRect(&rcBounds, &rcBounds, &rcState);
7753 if (!PtInRect(&rcBounds, opt)) iItem = -1;
7755 return lpht->iItem = iItem;
7758 /***
7759 * DESCRIPTION:
7760 * Inserts a new item in the listview control.
7762 * PARAMETER(S):
7763 * [I] infoPtr : valid pointer to the listview structure
7764 * [I] lpLVItem : item information
7765 * [I] isW : TRUE if lpLVItem is Unicode, FALSE if it's ANSI
7767 * RETURN:
7768 * SUCCESS : new item index
7769 * FAILURE : -1
7771 static INT LISTVIEW_InsertItemT(LISTVIEW_INFO *infoPtr, const LVITEMW *lpLVItem, BOOL isW)
7773 INT nItem;
7774 HDPA hdpaSubItems;
7775 NMLISTVIEW nmlv;
7776 ITEM_INFO *lpItem;
7777 ITEM_ID *lpID;
7778 BOOL is_sorted, has_changed;
7779 LVITEMW item;
7780 HWND hwndSelf = infoPtr->hwndSelf;
7782 TRACE("(item=%s, isW=%d)\n", debuglvitem_t(lpLVItem, isW), isW);
7784 if (infoPtr->dwStyle & LVS_OWNERDATA) return infoPtr->nItemCount++;
7786 /* make sure it's an item, and not a subitem; cannot insert a subitem */
7787 if (!lpLVItem || lpLVItem->iSubItem) return -1;
7789 if (!is_assignable_item(lpLVItem, infoPtr->dwStyle)) return -1;
7791 if (!(lpItem = Alloc(sizeof(ITEM_INFO)))) return -1;
7793 /* insert item in listview control data structure */
7794 if ( !(hdpaSubItems = DPA_Create(8)) ) goto fail;
7795 if ( !DPA_SetPtr(hdpaSubItems, 0, lpItem) ) assert (FALSE);
7797 /* link with id struct */
7798 if (!(lpID = Alloc(sizeof(ITEM_ID)))) goto fail;
7799 lpItem->id = lpID;
7800 lpID->item = hdpaSubItems;
7801 lpID->id = get_next_itemid(infoPtr);
7802 if ( DPA_InsertPtr(infoPtr->hdpaItemIds, infoPtr->nItemCount, lpID) == -1) goto fail;
7804 is_sorted = (infoPtr->dwStyle & (LVS_SORTASCENDING | LVS_SORTDESCENDING)) &&
7805 !(infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && (LPSTR_TEXTCALLBACKW != lpLVItem->pszText);
7807 if (lpLVItem->iItem < 0 && !is_sorted) return -1;
7809 /* calculate new item index */
7810 if (is_sorted)
7812 HDPA hItem;
7813 ITEM_INFO *item_s;
7814 INT i = 0, cmpv;
7815 WCHAR *textW;
7817 textW = textdupTtoW(lpLVItem->pszText, isW);
7819 while (i < infoPtr->nItemCount)
7821 hItem = DPA_GetPtr( infoPtr->hdpaItems, i);
7822 item_s = DPA_GetPtr(hItem, 0);
7824 cmpv = textcmpWT(item_s->hdr.pszText, textW, TRUE);
7825 if (infoPtr->dwStyle & LVS_SORTDESCENDING) cmpv *= -1;
7827 if (cmpv >= 0) break;
7828 i++;
7831 textfreeT(textW, isW);
7833 nItem = i;
7835 else
7836 nItem = min(lpLVItem->iItem, infoPtr->nItemCount);
7838 TRACE("inserting at %d, sorted=%d, count=%d, iItem=%d\n", nItem, is_sorted, infoPtr->nItemCount, lpLVItem->iItem);
7839 nItem = DPA_InsertPtr( infoPtr->hdpaItems, nItem, hdpaSubItems );
7840 if (nItem == -1) goto fail;
7841 infoPtr->nItemCount++;
7843 /* shift indices first so they don't get tangled */
7844 LISTVIEW_ShiftIndices(infoPtr, nItem, 1);
7846 /* set the item attributes */
7847 if (lpLVItem->mask & (LVIF_GROUPID|LVIF_COLUMNS))
7849 /* full size structure expected - _WIN32IE >= 0x560 */
7850 item = *lpLVItem;
7852 else if (lpLVItem->mask & LVIF_INDENT)
7854 /* indent member expected - _WIN32IE >= 0x300 */
7855 memcpy(&item, lpLVItem, offsetof( LVITEMW, iGroupId ));
7857 else
7859 /* minimal structure expected */
7860 memcpy(&item, lpLVItem, offsetof( LVITEMW, iIndent ));
7862 item.iItem = nItem;
7863 if (infoPtr->dwLvExStyle & LVS_EX_CHECKBOXES)
7865 if (item.mask & LVIF_STATE)
7867 item.stateMask |= LVIS_STATEIMAGEMASK;
7868 item.state &= ~LVIS_STATEIMAGEMASK;
7869 item.state |= INDEXTOSTATEIMAGEMASK(1);
7871 else
7873 item.mask |= LVIF_STATE;
7874 item.stateMask = LVIS_STATEIMAGEMASK;
7875 item.state = INDEXTOSTATEIMAGEMASK(1);
7879 if (!set_main_item(infoPtr, &item, TRUE, isW, &has_changed)) goto undo;
7881 /* make room for the position, if we are in the right mode */
7882 if ((infoPtr->uView == LV_VIEW_SMALLICON) || (infoPtr->uView == LV_VIEW_ICON))
7884 if (DPA_InsertPtr(infoPtr->hdpaPosX, nItem, 0) == -1)
7885 goto undo;
7886 if (DPA_InsertPtr(infoPtr->hdpaPosY, nItem, 0) == -1)
7888 DPA_DeletePtr(infoPtr->hdpaPosX, nItem);
7889 goto undo;
7893 /* send LVN_INSERTITEM notification */
7894 memset(&nmlv, 0, sizeof(NMLISTVIEW));
7895 nmlv.iItem = nItem;
7896 nmlv.lParam = lpItem->lParam;
7897 notify_listview(infoPtr, LVN_INSERTITEM, &nmlv);
7898 if (!IsWindow(hwndSelf))
7899 return -1;
7901 /* align items (set position of each item) */
7902 if (infoPtr->uView == LV_VIEW_SMALLICON || infoPtr->uView == LV_VIEW_ICON)
7904 POINT pt;
7906 if (infoPtr->dwStyle & LVS_ALIGNLEFT)
7907 LISTVIEW_NextIconPosLeft(infoPtr, &pt);
7908 else
7909 LISTVIEW_NextIconPosTop(infoPtr, &pt);
7911 LISTVIEW_MoveIconTo(infoPtr, nItem, &pt, TRUE);
7914 /* now is the invalidation fun */
7915 LISTVIEW_ScrollOnInsert(infoPtr, nItem, 1);
7916 return nItem;
7918 undo:
7919 LISTVIEW_ShiftIndices(infoPtr, nItem, -1);
7920 LISTVIEW_ShiftFocus(infoPtr, infoPtr->nFocusedItem, nItem, -1);
7921 DPA_DeletePtr(infoPtr->hdpaItems, nItem);
7922 infoPtr->nItemCount--;
7923 fail:
7924 DPA_DeletePtr(hdpaSubItems, 0);
7925 DPA_Destroy (hdpaSubItems);
7926 Free (lpItem);
7927 return -1;
7930 /***
7931 * DESCRIPTION:
7932 * Checks item visibility.
7934 * PARAMETER(S):
7935 * [I] infoPtr : valid pointer to the listview structure
7936 * [I] nFirst : item index to check for
7938 * RETURN:
7939 * Item visible : TRUE
7940 * Item invisible or failure : FALSE
7942 static BOOL LISTVIEW_IsItemVisible(const LISTVIEW_INFO *infoPtr, INT nItem)
7944 POINT Origin, Position;
7945 RECT rcItem;
7946 HDC hdc;
7947 BOOL ret;
7949 TRACE("nItem=%d\n", nItem);
7951 if (nItem < 0 || nItem >= DPA_GetPtrCount(infoPtr->hdpaItems)) return FALSE;
7953 LISTVIEW_GetOrigin(infoPtr, &Origin);
7954 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position);
7955 rcItem.left = Position.x + Origin.x;
7956 rcItem.top = Position.y + Origin.y;
7957 rcItem.right = rcItem.left + infoPtr->nItemWidth;
7958 rcItem.bottom = rcItem.top + infoPtr->nItemHeight;
7960 hdc = GetDC(infoPtr->hwndSelf);
7961 if (!hdc) return FALSE;
7962 ret = RectVisible(hdc, &rcItem);
7963 ReleaseDC(infoPtr->hwndSelf, hdc);
7965 return ret;
7968 /***
7969 * DESCRIPTION:
7970 * Redraws a range of items.
7972 * PARAMETER(S):
7973 * [I] infoPtr : valid pointer to the listview structure
7974 * [I] nFirst : first item
7975 * [I] nLast : last item
7977 * RETURN:
7978 * SUCCESS : TRUE
7979 * FAILURE : FALSE
7981 static BOOL LISTVIEW_RedrawItems(const LISTVIEW_INFO *infoPtr, INT nFirst, INT nLast)
7983 INT i;
7985 for (i = max(nFirst, 0); i <= min(nLast, infoPtr->nItemCount - 1); i++)
7986 LISTVIEW_InvalidateItem(infoPtr, i);
7988 return TRUE;
7991 /***
7992 * DESCRIPTION:
7993 * Scroll the content of a listview.
7995 * PARAMETER(S):
7996 * [I] infoPtr : valid pointer to the listview structure
7997 * [I] dx : horizontal scroll amount in pixels
7998 * [I] dy : vertical scroll amount in pixels
8000 * RETURN:
8001 * SUCCESS : TRUE
8002 * FAILURE : FALSE
8004 * COMMENTS:
8005 * If the control is in report view (LV_VIEW_DETAILS) the control can
8006 * be scrolled only in line increments. "dy" will be rounded to the
8007 * nearest number of pixels that are a whole line. Ex: if line height
8008 * is 16 and an 8 is passed, the list will be scrolled by 16. If a 7
8009 * is passed, then the scroll will be 0. (per MSDN 7/2002)
8011 static BOOL LISTVIEW_Scroll(LISTVIEW_INFO *infoPtr, INT dx, INT dy)
8013 switch(infoPtr->uView) {
8014 case LV_VIEW_DETAILS:
8015 dy += (dy < 0 ? -1 : 1) * infoPtr->nItemHeight/2;
8016 dy /= infoPtr->nItemHeight;
8017 break;
8018 case LV_VIEW_LIST:
8019 if (dy != 0) return FALSE;
8020 break;
8021 default: /* icon */
8022 break;
8025 if (dx != 0) LISTVIEW_HScroll(infoPtr, SB_INTERNAL, dx);
8026 if (dy != 0) LISTVIEW_VScroll(infoPtr, SB_INTERNAL, dy);
8028 return TRUE;
8031 /***
8032 * DESCRIPTION:
8033 * Sets the background color.
8035 * PARAMETER(S):
8036 * [I] infoPtr : valid pointer to the listview structure
8037 * [I] color : background color
8039 * RETURN:
8040 * SUCCESS : TRUE
8041 * FAILURE : FALSE
8043 static BOOL LISTVIEW_SetBkColor(LISTVIEW_INFO *infoPtr, COLORREF color)
8045 TRACE("color %lx\n", color);
8047 if(infoPtr->clrBk != color) {
8048 if (infoPtr->clrBk != CLR_NONE) DeleteObject(infoPtr->hBkBrush);
8049 infoPtr->clrBk = color;
8050 if (color == CLR_NONE)
8051 infoPtr->hBkBrush = (HBRUSH)GetClassLongPtrW(infoPtr->hwndSelf, GCLP_HBRBACKGROUND);
8052 else
8054 infoPtr->hBkBrush = CreateSolidBrush(color);
8055 infoPtr->dwLvExStyle &= ~LVS_EX_TRANSPARENTBKGND;
8059 return TRUE;
8062 static BOOL LISTVIEW_SetBkImage(LISTVIEW_INFO *infoPtr, const LVBKIMAGEW *image, BOOL isW)
8064 TRACE("%08lx, %p, %p, %u, %d, %d\n", image->ulFlags, image->hbm, image->pszImage,
8065 image->cchImageMax, image->xOffsetPercent, image->yOffsetPercent);
8067 if (image->ulFlags & ~LVBKIF_SOURCE_MASK)
8068 FIXME("unsupported flags %08lx\n", image->ulFlags & ~LVBKIF_SOURCE_MASK);
8070 if (image->xOffsetPercent || image->yOffsetPercent)
8071 FIXME("unsupported offset %d,%d\n", image->xOffsetPercent, image->yOffsetPercent);
8073 switch (image->ulFlags & LVBKIF_SOURCE_MASK)
8075 case LVBKIF_SOURCE_NONE:
8076 if (infoPtr->hBkBitmap)
8078 DeleteObject(infoPtr->hBkBitmap);
8079 infoPtr->hBkBitmap = NULL;
8081 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
8082 break;
8084 case LVBKIF_SOURCE_HBITMAP:
8086 BITMAP bm;
8088 if (infoPtr->hBkBitmap)
8090 DeleteObject(infoPtr->hBkBitmap);
8091 infoPtr->hBkBitmap = NULL;
8093 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
8094 if (GetObjectW(image->hbm, sizeof(bm), &bm) == sizeof(bm))
8096 infoPtr->hBkBitmap = image->hbm;
8097 return TRUE;
8099 break;
8102 case LVBKIF_SOURCE_URL:
8103 FIXME("LVBKIF_SOURCE_URL: %s\n", isW ? debugstr_w(image->pszImage) : debugstr_a((LPCSTR)image->pszImage));
8104 break;
8107 return FALSE;
8110 /*** Helper for {Insert,Set}ColumnT *only* */
8111 static void column_fill_hditem(const LISTVIEW_INFO *infoPtr, HDITEMW *lphdi, INT nColumn,
8112 const LVCOLUMNW *lpColumn, BOOL isW)
8114 if (lpColumn->mask & LVCF_FMT)
8116 /* format member is valid */
8117 lphdi->mask |= HDI_FORMAT;
8119 /* set text alignment (leftmost column must be left-aligned) */
8120 if (nColumn == 0 || (lpColumn->fmt & LVCFMT_JUSTIFYMASK) == LVCFMT_LEFT)
8121 lphdi->fmt |= HDF_LEFT;
8122 else if ((lpColumn->fmt & LVCFMT_JUSTIFYMASK) == LVCFMT_RIGHT)
8123 lphdi->fmt |= HDF_RIGHT;
8124 else if ((lpColumn->fmt & LVCFMT_JUSTIFYMASK) == LVCFMT_CENTER)
8125 lphdi->fmt |= HDF_CENTER;
8127 if (lpColumn->fmt & LVCFMT_BITMAP_ON_RIGHT)
8128 lphdi->fmt |= HDF_BITMAP_ON_RIGHT;
8130 if (lpColumn->fmt & LVCFMT_COL_HAS_IMAGES)
8132 lphdi->fmt |= HDF_IMAGE;
8133 lphdi->iImage = I_IMAGECALLBACK;
8136 if (lpColumn->fmt & LVCFMT_FIXED_WIDTH)
8137 lphdi->fmt |= HDF_FIXEDWIDTH;
8140 if (lpColumn->mask & LVCF_WIDTH)
8142 lphdi->mask |= HDI_WIDTH;
8143 if(lpColumn->cx == LVSCW_AUTOSIZE_USEHEADER)
8145 /* make it fill the remainder of the controls width */
8146 RECT rcHeader;
8147 INT item_index;
8149 for(item_index = 0; item_index < (nColumn - 1); item_index++)
8151 LISTVIEW_GetHeaderRect(infoPtr, item_index, &rcHeader);
8152 lphdi->cxy += rcHeader.right - rcHeader.left;
8155 /* retrieve the layout of the header */
8156 GetClientRect(infoPtr->hwndSelf, &rcHeader);
8157 TRACE("start cxy=%d rcHeader=%s\n", lphdi->cxy, wine_dbgstr_rect(&rcHeader));
8159 lphdi->cxy = (rcHeader.right - rcHeader.left) - lphdi->cxy;
8161 else
8162 lphdi->cxy = lpColumn->cx;
8165 if (lpColumn->mask & LVCF_TEXT)
8167 lphdi->mask |= HDI_TEXT | HDI_FORMAT;
8168 lphdi->fmt |= HDF_STRING;
8169 lphdi->pszText = lpColumn->pszText;
8170 lphdi->cchTextMax = textlenT(lpColumn->pszText, isW);
8173 if (lpColumn->mask & LVCF_IMAGE)
8175 lphdi->mask |= HDI_IMAGE;
8176 lphdi->iImage = lpColumn->iImage;
8179 if (lpColumn->mask & LVCF_ORDER)
8181 lphdi->mask |= HDI_ORDER;
8182 lphdi->iOrder = lpColumn->iOrder;
8187 /***
8188 * DESCRIPTION:
8189 * Inserts a new column.
8191 * PARAMETER(S):
8192 * [I] infoPtr : valid pointer to the listview structure
8193 * [I] nColumn : column index
8194 * [I] lpColumn : column information
8195 * [I] isW : TRUE if lpColumn is Unicode, FALSE otherwise
8197 * RETURN:
8198 * SUCCESS : new column index
8199 * FAILURE : -1
8201 static INT LISTVIEW_InsertColumnT(LISTVIEW_INFO *infoPtr, INT nColumn,
8202 const LVCOLUMNW *lpColumn, BOOL isW)
8204 COLUMN_INFO *lpColumnInfo;
8205 INT nNewColumn;
8206 HDITEMW hdi;
8208 TRACE("(nColumn=%d, lpColumn=%s, isW=%d)\n", nColumn, debuglvcolumn_t(lpColumn, isW), isW);
8210 if (!lpColumn || nColumn < 0) return -1;
8211 nColumn = min(nColumn, DPA_GetPtrCount(infoPtr->hdpaColumns));
8213 ZeroMemory(&hdi, sizeof(HDITEMW));
8214 column_fill_hditem(infoPtr, &hdi, nColumn, lpColumn, isW);
8217 * A mask not including LVCF_WIDTH turns into a mask of width, width 10
8218 * (can be seen in SPY) otherwise column never gets added.
8220 if (!(lpColumn->mask & LVCF_WIDTH)) {
8221 hdi.mask |= HDI_WIDTH;
8222 hdi.cxy = 10;
8226 * when the iSubItem is available Windows copies it to the header lParam. It seems
8227 * to happen only in LVM_INSERTCOLUMN - not in LVM_SETCOLUMN
8229 if (lpColumn->mask & LVCF_SUBITEM)
8231 hdi.mask |= HDI_LPARAM;
8232 hdi.lParam = lpColumn->iSubItem;
8235 /* create header if not present */
8236 LISTVIEW_CreateHeader(infoPtr);
8237 if (!(LVS_NOCOLUMNHEADER & infoPtr->dwStyle) &&
8238 (infoPtr->uView == LV_VIEW_DETAILS) && (WS_VISIBLE & infoPtr->dwStyle))
8240 ShowWindow(infoPtr->hwndHeader, SW_SHOWNORMAL);
8243 /* insert item in header control */
8244 nNewColumn = SendMessageW(infoPtr->hwndHeader,
8245 isW ? HDM_INSERTITEMW : HDM_INSERTITEMA,
8246 nColumn, (LPARAM)&hdi);
8247 if (nNewColumn == -1) return -1;
8248 if (nNewColumn != nColumn) ERR("nColumn=%d, nNewColumn=%d\n", nColumn, nNewColumn);
8250 /* create our own column info */
8251 if (!(lpColumnInfo = Alloc(sizeof(COLUMN_INFO)))) goto fail;
8252 if (DPA_InsertPtr(infoPtr->hdpaColumns, nNewColumn, lpColumnInfo) == -1) goto fail;
8254 if (lpColumn->mask & LVCF_FMT) lpColumnInfo->fmt = lpColumn->fmt;
8255 if (lpColumn->mask & LVCF_MINWIDTH) lpColumnInfo->cxMin = lpColumn->cxMin;
8256 if (!SendMessageW(infoPtr->hwndHeader, HDM_GETITEMRECT, nNewColumn, (LPARAM)&lpColumnInfo->rcHeader))
8257 goto fail;
8259 /* now we have to actually adjust the data */
8260 if (!(infoPtr->dwStyle & LVS_OWNERDATA) && infoPtr->nItemCount > 0)
8262 SUBITEM_INFO *lpSubItem;
8263 HDPA hdpaSubItems;
8264 INT nItem, i;
8265 LVITEMW item;
8266 BOOL changed;
8268 item.iSubItem = nNewColumn;
8269 item.mask = LVIF_TEXT | LVIF_IMAGE;
8270 item.iImage = I_IMAGECALLBACK;
8271 item.pszText = LPSTR_TEXTCALLBACKW;
8273 for (nItem = 0; nItem < infoPtr->nItemCount; nItem++)
8275 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, nItem);
8276 for (i = 1; i < DPA_GetPtrCount(hdpaSubItems); i++)
8278 lpSubItem = DPA_GetPtr(hdpaSubItems, i);
8279 if (lpSubItem->iSubItem >= nNewColumn)
8280 lpSubItem->iSubItem++;
8283 /* add new subitem for each item */
8284 item.iItem = nItem;
8285 set_sub_item(infoPtr, &item, isW, &changed);
8289 /* make space for the new column */
8290 LISTVIEW_ScrollColumns(infoPtr, nNewColumn + 1, lpColumnInfo->rcHeader.right - lpColumnInfo->rcHeader.left);
8291 LISTVIEW_UpdateItemSize(infoPtr);
8293 return nNewColumn;
8295 fail:
8296 if (nNewColumn != -1) SendMessageW(infoPtr->hwndHeader, HDM_DELETEITEM, nNewColumn, 0);
8297 if (lpColumnInfo)
8299 DPA_DeletePtr(infoPtr->hdpaColumns, nNewColumn);
8300 Free(lpColumnInfo);
8302 return -1;
8305 /***
8306 * DESCRIPTION:
8307 * Sets the attributes of a header item.
8309 * PARAMETER(S):
8310 * [I] infoPtr : valid pointer to the listview structure
8311 * [I] nColumn : column index
8312 * [I] lpColumn : column attributes
8313 * [I] isW: if TRUE, then lpColumn is a LPLVCOLUMNW, else it is a LPLVCOLUMNA
8315 * RETURN:
8316 * SUCCESS : TRUE
8317 * FAILURE : FALSE
8319 static BOOL LISTVIEW_SetColumnT(const LISTVIEW_INFO *infoPtr, INT nColumn,
8320 const LVCOLUMNW *lpColumn, BOOL isW)
8322 HDITEMW hdi, hdiget;
8323 BOOL bResult;
8325 TRACE("(nColumn=%d, lpColumn=%s, isW=%d)\n", nColumn, debuglvcolumn_t(lpColumn, isW), isW);
8327 if (!lpColumn || nColumn < 0 || nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return FALSE;
8329 ZeroMemory(&hdi, sizeof(HDITEMW));
8330 if (lpColumn->mask & LVCF_FMT)
8332 hdi.mask |= HDI_FORMAT;
8333 hdiget.mask = HDI_FORMAT;
8334 if (SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, nColumn, (LPARAM)&hdiget))
8335 hdi.fmt = hdiget.fmt & HDF_STRING;
8337 column_fill_hditem(infoPtr, &hdi, nColumn, lpColumn, isW);
8339 /* set header item attributes */
8340 bResult = SendMessageW(infoPtr->hwndHeader, isW ? HDM_SETITEMW : HDM_SETITEMA, nColumn, (LPARAM)&hdi);
8341 if (!bResult) return FALSE;
8343 if (lpColumn->mask & LVCF_FMT)
8345 COLUMN_INFO *lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, nColumn);
8346 INT oldFmt = lpColumnInfo->fmt;
8348 lpColumnInfo->fmt = lpColumn->fmt;
8349 if ((oldFmt ^ lpColumn->fmt) & (LVCFMT_JUSTIFYMASK | LVCFMT_IMAGE))
8351 if (infoPtr->uView == LV_VIEW_DETAILS) LISTVIEW_InvalidateColumn(infoPtr, nColumn);
8355 if (lpColumn->mask & LVCF_MINWIDTH)
8356 LISTVIEW_GetColumnInfo(infoPtr, nColumn)->cxMin = lpColumn->cxMin;
8358 return TRUE;
8361 /***
8362 * DESCRIPTION:
8363 * Sets the column order array
8365 * PARAMETERS:
8366 * [I] infoPtr : valid pointer to the listview structure
8367 * [I] iCount : number of elements in column order array
8368 * [I] lpiArray : pointer to column order array
8370 * RETURN:
8371 * SUCCESS : TRUE
8372 * FAILURE : FALSE
8374 static BOOL LISTVIEW_SetColumnOrderArray(LISTVIEW_INFO *infoPtr, INT iCount, const INT *lpiArray)
8376 if (!infoPtr->hwndHeader) return FALSE;
8377 infoPtr->colRectsDirty = TRUE;
8378 return SendMessageW(infoPtr->hwndHeader, HDM_SETORDERARRAY, iCount, (LPARAM)lpiArray);
8381 /***
8382 * DESCRIPTION:
8383 * Sets the width of a column
8385 * PARAMETERS:
8386 * [I] infoPtr : valid pointer to the listview structure
8387 * [I] nColumn : column index
8388 * [I] cx : column width
8390 * RETURN:
8391 * SUCCESS : TRUE
8392 * FAILURE : FALSE
8394 static BOOL LISTVIEW_SetColumnWidth(LISTVIEW_INFO *infoPtr, INT nColumn, INT cx)
8396 WCHAR szDispText[DISP_TEXT_SIZE] = { 0 };
8397 INT max_cx = 0;
8398 HDITEMW hdi;
8400 TRACE("(nColumn=%d, cx=%d)\n", nColumn, cx);
8402 /* set column width only if in report or list mode */
8403 if (infoPtr->uView != LV_VIEW_DETAILS && infoPtr->uView != LV_VIEW_LIST) return FALSE;
8405 /* take care of invalid cx values - LVSCW_AUTOSIZE_* values are negative,
8406 with _USEHEADER being the lowest */
8407 if (infoPtr->uView == LV_VIEW_DETAILS && cx < LVSCW_AUTOSIZE_USEHEADER) cx = LVSCW_AUTOSIZE;
8408 else if (infoPtr->uView == LV_VIEW_LIST && cx <= 0) return FALSE;
8410 /* resize all columns if in LV_VIEW_LIST mode */
8411 if(infoPtr->uView == LV_VIEW_LIST)
8413 infoPtr->nItemWidth = cx;
8414 LISTVIEW_InvalidateList(infoPtr);
8415 return TRUE;
8418 if (nColumn < 0 || nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return FALSE;
8420 if (cx == LVSCW_AUTOSIZE || (cx == LVSCW_AUTOSIZE_USEHEADER && nColumn < DPA_GetPtrCount(infoPtr->hdpaColumns) -1))
8422 INT nLabelWidth;
8423 LVITEMW lvItem;
8425 lvItem.mask = LVIF_TEXT;
8426 lvItem.iItem = 0;
8427 lvItem.iSubItem = nColumn;
8428 lvItem.cchTextMax = DISP_TEXT_SIZE;
8429 for (; lvItem.iItem < infoPtr->nItemCount; lvItem.iItem++)
8431 lvItem.pszText = szDispText;
8432 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) continue;
8433 nLabelWidth = LISTVIEW_GetStringWidthT(infoPtr, lvItem.pszText, TRUE);
8434 if (max_cx < nLabelWidth) max_cx = nLabelWidth;
8436 if (infoPtr->himlSmall && (nColumn == 0 || (LISTVIEW_GetColumnInfo(infoPtr, nColumn)->fmt & LVCFMT_IMAGE)))
8437 max_cx += infoPtr->iconSize.cx;
8438 max_cx += TRAILING_LABEL_PADDING;
8439 if (nColumn == 0 && (infoPtr->dwLvExStyle & LVS_EX_CHECKBOXES))
8440 max_cx += GetSystemMetrics(SM_CXSMICON);
8443 /* autosize based on listview items width */
8444 if(cx == LVSCW_AUTOSIZE)
8445 cx = max_cx;
8446 else if(cx == LVSCW_AUTOSIZE_USEHEADER)
8448 /* if iCol is the last column make it fill the remainder of the controls width */
8449 if(nColumn == DPA_GetPtrCount(infoPtr->hdpaColumns) - 1)
8451 RECT rcHeader;
8452 POINT Origin;
8454 LISTVIEW_GetOrigin(infoPtr, &Origin);
8455 LISTVIEW_GetHeaderRect(infoPtr, nColumn, &rcHeader);
8457 cx = infoPtr->rcList.right - Origin.x - rcHeader.left;
8459 else
8461 /* Despite what the MS docs say, if this is not the last
8462 column, then MS resizes the column to the width of the
8463 largest text string in the column, including headers
8464 and items. This is different from LVSCW_AUTOSIZE in that
8465 LVSCW_AUTOSIZE ignores the header string length. */
8466 cx = 0;
8468 /* retrieve header text */
8469 hdi.mask = HDI_TEXT|HDI_FORMAT|HDI_IMAGE|HDI_BITMAP;
8470 hdi.cchTextMax = DISP_TEXT_SIZE;
8471 hdi.pszText = szDispText;
8472 if (SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, nColumn, (LPARAM)&hdi))
8474 HDC hdc = GetDC(infoPtr->hwndSelf);
8475 HFONT old_font = SelectObject(hdc, (HFONT)SendMessageW(infoPtr->hwndHeader, WM_GETFONT, 0, 0));
8476 HIMAGELIST himl = (HIMAGELIST)SendMessageW(infoPtr->hwndHeader, HDM_GETIMAGELIST, 0, 0);
8477 INT bitmap_margin = 0;
8478 SIZE size;
8480 if (GetTextExtentPoint32W(hdc, hdi.pszText, lstrlenW(hdi.pszText), &size))
8481 cx = size.cx + TRAILING_HEADER_PADDING;
8483 if (hdi.fmt & (HDF_IMAGE|HDF_BITMAP))
8484 bitmap_margin = SendMessageW(infoPtr->hwndHeader, HDM_GETBITMAPMARGIN, 0, 0);
8486 if ((hdi.fmt & HDF_IMAGE) && himl)
8488 INT icon_cx, icon_cy;
8490 if (!ImageList_GetIconSize(himl, &icon_cx, &icon_cy))
8491 cx += icon_cx + 2*bitmap_margin;
8493 else if (hdi.fmt & HDF_BITMAP)
8495 BITMAP bmp;
8497 GetObjectW(hdi.hbm, sizeof(BITMAP), &bmp);
8498 cx += bmp.bmWidth + 2*bitmap_margin;
8501 SelectObject(hdc, old_font);
8502 ReleaseDC(infoPtr->hwndSelf, hdc);
8504 cx = max (cx, max_cx);
8508 if (cx < 0) return FALSE;
8510 /* call header to update the column change */
8511 hdi.mask = HDI_WIDTH;
8512 hdi.cxy = max(cx, LISTVIEW_GetColumnInfo(infoPtr, nColumn)->cxMin);
8513 TRACE("hdi.cxy=%d\n", hdi.cxy);
8514 return SendMessageW(infoPtr->hwndHeader, HDM_SETITEMW, nColumn, (LPARAM)&hdi);
8517 /***
8518 * Creates the checkbox imagelist. Helper for LISTVIEW_SetExtendedListViewStyle
8521 static HIMAGELIST LISTVIEW_CreateThemedCheckBoxImageList(const LISTVIEW_INFO *info)
8523 HBITMAP bitmap, old_bitmap;
8524 HIMAGELIST image_list;
8525 HDC hdc, mem_hdc;
8526 HTHEME theme;
8527 RECT rect;
8528 SIZE size;
8530 if (!GetWindowTheme(info->hwndSelf))
8531 return NULL;
8533 theme = OpenThemeDataForDpi(NULL, L"Button", GetDpiForWindow(info->hwndSelf));
8534 if (!theme)
8535 return NULL;
8537 hdc = GetDC(info->hwndSelf);
8538 GetThemePartSize(theme, hdc, BP_CHECKBOX, 0, NULL, TS_DRAW, &size);
8539 SetRect(&rect, 0, 0, size.cx, size.cy);
8540 image_list = ImageList_Create(size.cx, size.cy, ILC_COLOR32, 2, 2);
8541 mem_hdc = CreateCompatibleDC(hdc);
8542 bitmap = CreateCompatibleBitmap(hdc, size.cx, size.cy);
8543 old_bitmap = SelectObject(mem_hdc, bitmap);
8544 ReleaseDC(info->hwndSelf, hdc);
8546 if (IsThemeBackgroundPartiallyTransparent(theme, BP_CHECKBOX, CBS_UNCHECKEDNORMAL))
8547 FillRect(mem_hdc, &rect, (HBRUSH)(COLOR_WINDOW + 1));
8548 DrawThemeBackground(theme, mem_hdc, BP_CHECKBOX, CBS_UNCHECKEDNORMAL, &rect, NULL);
8549 ImageList_Add(image_list, bitmap, NULL);
8551 if (IsThemeBackgroundPartiallyTransparent(theme, BP_CHECKBOX, CBS_CHECKEDNORMAL))
8552 FillRect(mem_hdc, &rect, (HBRUSH)(COLOR_WINDOW + 1));
8553 DrawThemeBackground(theme, mem_hdc, BP_CHECKBOX, CBS_CHECKEDNORMAL, &rect, NULL);
8554 ImageList_Add(image_list, bitmap, NULL);
8556 SelectObject(mem_hdc, old_bitmap);
8557 DeleteObject(bitmap);
8558 DeleteDC(mem_hdc);
8559 CloseThemeData(theme);
8560 return image_list;
8563 static HIMAGELIST LISTVIEW_CreateCheckBoxIL(const LISTVIEW_INFO *infoPtr)
8565 HDC hdc_wnd, hdc;
8566 HBITMAP hbm_im, hbm_mask, hbm_orig;
8567 RECT rc;
8568 HBRUSH hbr_white, hbr_black;
8569 HIMAGELIST himl;
8571 himl = LISTVIEW_CreateThemedCheckBoxImageList(infoPtr);
8572 if (himl)
8573 return himl;
8575 hbr_white = GetStockObject(WHITE_BRUSH);
8576 hbr_black = GetStockObject(BLACK_BRUSH);
8577 himl = ImageList_Create(GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON),
8578 ILC_COLOR | ILC_MASK, 2, 2);
8579 hdc_wnd = GetDC(infoPtr->hwndSelf);
8580 hdc = CreateCompatibleDC(hdc_wnd);
8581 hbm_im = CreateCompatibleBitmap(hdc_wnd, GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON));
8582 hbm_mask = CreateBitmap(GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), 1, 1, NULL);
8583 ReleaseDC(infoPtr->hwndSelf, hdc_wnd);
8585 SetRect(&rc, 0, 0, GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON));
8586 hbm_orig = SelectObject(hdc, hbm_mask);
8587 FillRect(hdc, &rc, hbr_white);
8588 InflateRect(&rc, -2, -2);
8589 FillRect(hdc, &rc, hbr_black);
8591 SelectObject(hdc, hbm_im);
8592 DrawFrameControl(hdc, &rc, DFC_BUTTON, DFCS_BUTTONCHECK | DFCS_MONO);
8593 SelectObject(hdc, hbm_orig);
8594 ImageList_Add(himl, hbm_im, hbm_mask);
8596 SelectObject(hdc, hbm_im);
8597 DrawFrameControl(hdc, &rc, DFC_BUTTON, DFCS_BUTTONCHECK | DFCS_MONO | DFCS_CHECKED);
8598 SelectObject(hdc, hbm_orig);
8599 ImageList_Add(himl, hbm_im, hbm_mask);
8601 DeleteObject(hbm_mask);
8602 DeleteObject(hbm_im);
8603 DeleteDC(hdc);
8605 return himl;
8608 /***
8609 * DESCRIPTION:
8610 * Sets the extended listview style.
8612 * PARAMETERS:
8613 * [I] infoPtr : valid pointer to the listview structure
8614 * [I] dwMask : mask
8615 * [I] dwStyle : style
8617 * RETURN:
8618 * SUCCESS : previous style
8619 * FAILURE : 0
8621 static DWORD LISTVIEW_SetExtendedListViewStyle(LISTVIEW_INFO *infoPtr, DWORD mask, DWORD ex_style)
8623 DWORD old_ex_style = infoPtr->dwLvExStyle;
8625 TRACE("mask %#lx, ex_style %#lx\n", mask, ex_style);
8627 /* set new style */
8628 if (mask)
8629 infoPtr->dwLvExStyle = (old_ex_style & ~mask) | (ex_style & mask);
8630 else
8631 infoPtr->dwLvExStyle = ex_style;
8633 if((infoPtr->dwLvExStyle ^ old_ex_style) & LVS_EX_CHECKBOXES)
8635 HIMAGELIST himl = 0;
8636 if(infoPtr->dwLvExStyle & LVS_EX_CHECKBOXES)
8638 LVITEMW item;
8639 item.mask = LVIF_STATE;
8640 item.stateMask = LVIS_STATEIMAGEMASK;
8641 item.state = INDEXTOSTATEIMAGEMASK(1);
8642 LISTVIEW_SetItemState(infoPtr, -1, &item);
8644 himl = LISTVIEW_CreateCheckBoxIL(infoPtr);
8645 if(!(infoPtr->dwStyle & LVS_SHAREIMAGELISTS))
8646 ImageList_Destroy(infoPtr->himlState);
8648 himl = LISTVIEW_SetImageList(infoPtr, LVSIL_STATE, himl);
8649 /* checkbox list replaces previous custom list or... */
8650 if(((infoPtr->dwLvExStyle & LVS_EX_CHECKBOXES) &&
8651 !(infoPtr->dwStyle & LVS_SHAREIMAGELISTS)) ||
8652 /* ...previous was checkbox list */
8653 (old_ex_style & LVS_EX_CHECKBOXES))
8654 ImageList_Destroy(himl);
8657 if((infoPtr->dwLvExStyle ^ old_ex_style) & LVS_EX_HEADERDRAGDROP)
8659 DWORD style;
8661 /* if not already created */
8662 LISTVIEW_CreateHeader(infoPtr);
8664 style = GetWindowLongW(infoPtr->hwndHeader, GWL_STYLE);
8665 if (infoPtr->dwLvExStyle & LVS_EX_HEADERDRAGDROP)
8666 style |= HDS_DRAGDROP;
8667 else
8668 style &= ~HDS_DRAGDROP;
8669 SetWindowLongW(infoPtr->hwndHeader, GWL_STYLE, style);
8672 /* GRIDLINES adds decoration at top so changes sizes */
8673 if((infoPtr->dwLvExStyle ^ old_ex_style) & LVS_EX_GRIDLINES)
8675 LISTVIEW_CreateHeader(infoPtr);
8676 LISTVIEW_UpdateSize(infoPtr);
8679 if((infoPtr->dwLvExStyle ^ old_ex_style) & LVS_EX_FULLROWSELECT)
8681 LISTVIEW_CreateHeader(infoPtr);
8684 if((infoPtr->dwLvExStyle ^ old_ex_style) & LVS_EX_TRANSPARENTBKGND)
8686 if (infoPtr->dwLvExStyle & LVS_EX_TRANSPARENTBKGND)
8687 LISTVIEW_SetBkColor(infoPtr, CLR_NONE);
8690 if((infoPtr->dwLvExStyle ^ old_ex_style) & LVS_EX_HEADERINALLVIEWS)
8692 if (infoPtr->dwLvExStyle & LVS_EX_HEADERINALLVIEWS)
8693 LISTVIEW_CreateHeader(infoPtr);
8694 else
8695 ShowWindow(infoPtr->hwndHeader, SW_HIDE);
8696 LISTVIEW_UpdateSize(infoPtr);
8697 LISTVIEW_UpdateScroll(infoPtr);
8700 LISTVIEW_InvalidateList(infoPtr);
8701 return old_ex_style;
8704 /***
8705 * DESCRIPTION:
8706 * Sets the new hot cursor used during hot tracking and hover selection.
8708 * PARAMETER(S):
8709 * [I] infoPtr : valid pointer to the listview structure
8710 * [I] hCursor : the new hot cursor handle
8712 * RETURN:
8713 * Returns the previous hot cursor
8715 static HCURSOR LISTVIEW_SetHotCursor(LISTVIEW_INFO *infoPtr, HCURSOR hCursor)
8717 HCURSOR oldCursor = infoPtr->hHotCursor;
8719 infoPtr->hHotCursor = hCursor;
8721 return oldCursor;
8725 /***
8726 * DESCRIPTION:
8727 * Sets the hot item index.
8729 * PARAMETERS:
8730 * [I] infoPtr : valid pointer to the listview structure
8731 * [I] iIndex : index
8733 * RETURN:
8734 * SUCCESS : previous hot item index
8735 * FAILURE : -1 (no hot item)
8737 static INT LISTVIEW_SetHotItem(LISTVIEW_INFO *infoPtr, INT iIndex)
8739 INT iOldIndex = infoPtr->nHotItem;
8741 infoPtr->nHotItem = iIndex;
8743 return iOldIndex;
8747 /***
8748 * DESCRIPTION:
8749 * Sets the amount of time the cursor must hover over an item before it is selected.
8751 * PARAMETER(S):
8752 * [I] infoPtr : valid pointer to the listview structure
8753 * [I] dwHoverTime : hover time, if -1 the hover time is set to the default
8755 * RETURN:
8756 * Returns the previous hover time
8758 static DWORD LISTVIEW_SetHoverTime(LISTVIEW_INFO *infoPtr, DWORD dwHoverTime)
8760 DWORD oldHoverTime = infoPtr->dwHoverTime;
8762 infoPtr->dwHoverTime = dwHoverTime;
8764 return oldHoverTime;
8767 /***
8768 * DESCRIPTION:
8769 * Sets spacing for icons of LVS_ICON style.
8771 * PARAMETER(S):
8772 * [I] infoPtr : valid pointer to the listview structure
8773 * [I] cx : horizontal spacing (-1 = system spacing, 0 = autosize)
8774 * [I] cy : vertical spacing (-1 = system spacing, 0 = autosize)
8776 * RETURN:
8777 * MAKELONG(oldcx, oldcy)
8779 static DWORD LISTVIEW_SetIconSpacing(LISTVIEW_INFO *infoPtr, INT cx, INT cy)
8781 INT iconWidth = 0, iconHeight = 0;
8782 DWORD oldspacing = MAKELONG(infoPtr->iconSpacing.cx, infoPtr->iconSpacing.cy);
8784 TRACE("requested=(%d,%d)\n", cx, cy);
8786 /* set to defaults, if instructed to */
8787 if (cx == -1 && cy == -1)
8789 infoPtr->autoSpacing = TRUE;
8790 if (infoPtr->himlNormal)
8791 ImageList_GetIconSize(infoPtr->himlNormal, &iconWidth, &iconHeight);
8792 cx = GetSystemMetrics(SM_CXICONSPACING) - GetSystemMetrics(SM_CXICON) + iconWidth;
8793 cy = GetSystemMetrics(SM_CYICONSPACING) - GetSystemMetrics(SM_CYICON) + iconHeight;
8795 else
8796 infoPtr->autoSpacing = FALSE;
8798 /* if 0 then keep width */
8799 if (cx != 0)
8800 infoPtr->iconSpacing.cx = cx;
8802 /* if 0 then keep height */
8803 if (cy != 0)
8804 infoPtr->iconSpacing.cy = cy;
8806 TRACE("old=(%d,%d), new=(%ld,%ld), iconSize=(%ld,%ld), ntmH=%d\n",
8807 LOWORD(oldspacing), HIWORD(oldspacing), infoPtr->iconSpacing.cx, infoPtr->iconSpacing.cy,
8808 infoPtr->iconSize.cx, infoPtr->iconSize.cy,
8809 infoPtr->ntmHeight);
8811 /* these depend on the iconSpacing */
8812 LISTVIEW_UpdateItemSize(infoPtr);
8814 return oldspacing;
8817 static inline void set_icon_size(SIZE *size, HIMAGELIST himl, BOOL is_small)
8819 INT cx, cy;
8821 if (himl && ImageList_GetIconSize(himl, &cx, &cy))
8823 size->cx = cx;
8824 size->cy = cy;
8826 else
8828 size->cx = GetSystemMetrics(is_small ? SM_CXSMICON : SM_CXICON);
8829 size->cy = GetSystemMetrics(is_small ? SM_CYSMICON : SM_CYICON);
8833 /***
8834 * DESCRIPTION:
8835 * Sets image lists.
8837 * PARAMETER(S):
8838 * [I] infoPtr : valid pointer to the listview structure
8839 * [I] nType : image list type
8840 * [I] himl : image list handle
8842 * RETURN:
8843 * SUCCESS : old image list
8844 * FAILURE : NULL
8846 static HIMAGELIST LISTVIEW_SetImageList(LISTVIEW_INFO *infoPtr, INT nType, HIMAGELIST himl)
8848 INT oldHeight = infoPtr->nItemHeight;
8849 HIMAGELIST himlOld = 0;
8851 TRACE("(nType=%d, himl=%p)\n", nType, himl);
8853 switch (nType)
8855 case LVSIL_NORMAL:
8856 himlOld = infoPtr->himlNormal;
8857 infoPtr->himlNormal = himl;
8858 if (infoPtr->uView == LV_VIEW_ICON) set_icon_size(&infoPtr->iconSize, himl, FALSE);
8859 if (infoPtr->autoSpacing)
8860 LISTVIEW_SetIconSpacing(infoPtr, -1, -1);
8861 break;
8863 case LVSIL_SMALL:
8864 himlOld = infoPtr->himlSmall;
8865 infoPtr->himlSmall = himl;
8866 if (infoPtr->uView != LV_VIEW_ICON) set_icon_size(&infoPtr->iconSize, himl, TRUE);
8867 if (infoPtr->hwndHeader)
8868 SendMessageW(infoPtr->hwndHeader, HDM_SETIMAGELIST, 0, (LPARAM)himl);
8869 break;
8871 case LVSIL_STATE:
8872 himlOld = infoPtr->himlState;
8873 infoPtr->himlState = himl;
8874 set_icon_size(&infoPtr->iconStateSize, himl, TRUE);
8875 ImageList_SetBkColor(infoPtr->himlState, CLR_NONE);
8876 break;
8878 default:
8879 ERR("Unknown icon type=%d\n", nType);
8880 return NULL;
8883 infoPtr->nItemHeight = LISTVIEW_CalculateItemHeight(infoPtr);
8884 if (infoPtr->nItemHeight != oldHeight)
8885 LISTVIEW_UpdateScroll(infoPtr);
8887 return himlOld;
8890 /***
8891 * DESCRIPTION:
8892 * Preallocates memory (does *not* set the actual count of items !)
8894 * PARAMETER(S):
8895 * [I] infoPtr : valid pointer to the listview structure
8896 * [I] nItems : item count (projected number of items to allocate)
8897 * [I] dwFlags : update flags
8899 * RETURN:
8900 * SUCCESS : TRUE
8901 * FAILURE : FALSE
8903 static BOOL LISTVIEW_SetItemCount(LISTVIEW_INFO *infoPtr, INT nItems, DWORD dwFlags)
8905 TRACE("nItems %d, flags %#lx\n", nItems, dwFlags);
8907 if (infoPtr->dwStyle & LVS_OWNERDATA)
8909 INT nOldCount = infoPtr->nItemCount;
8910 infoPtr->nItemCount = nItems;
8912 if (nItems < nOldCount)
8914 RANGE range = { nItems, nOldCount };
8915 ranges_del(infoPtr->selectionRanges, range);
8916 if (infoPtr->nFocusedItem >= nItems)
8918 LISTVIEW_SetItemFocus(infoPtr, -1);
8919 infoPtr->nFocusedItem = -1;
8920 SetRectEmpty(&infoPtr->rcFocus);
8924 LISTVIEW_UpdateScroll(infoPtr);
8926 /* the flags are valid only in ownerdata report and list modes */
8927 if (infoPtr->uView == LV_VIEW_ICON || infoPtr->uView == LV_VIEW_SMALLICON) dwFlags = 0;
8929 if (!(dwFlags & LVSICF_NOSCROLL) && infoPtr->nFocusedItem != -1)
8930 LISTVIEW_EnsureVisible(infoPtr, infoPtr->nFocusedItem, FALSE);
8932 if (!(dwFlags & LVSICF_NOINVALIDATEALL))
8933 LISTVIEW_InvalidateList(infoPtr);
8934 else
8936 INT nFrom, nTo;
8937 POINT Origin;
8938 RECT rcErase;
8940 LISTVIEW_GetOrigin(infoPtr, &Origin);
8941 nFrom = min(nOldCount, nItems);
8942 nTo = max(nOldCount, nItems);
8944 if (infoPtr->uView == LV_VIEW_DETAILS)
8946 SetRect(&rcErase, 0, nFrom * infoPtr->nItemHeight, infoPtr->nItemWidth,
8947 nTo * infoPtr->nItemHeight);
8948 OffsetRect(&rcErase, Origin.x, Origin.y);
8949 if (IntersectRect(&rcErase, &rcErase, &infoPtr->rcList))
8950 LISTVIEW_InvalidateRect(infoPtr, &rcErase);
8952 else /* LV_VIEW_LIST */
8954 INT nPerCol = LISTVIEW_GetCountPerColumn(infoPtr);
8956 rcErase.left = (nFrom / nPerCol) * infoPtr->nItemWidth;
8957 rcErase.top = (nFrom % nPerCol) * infoPtr->nItemHeight;
8958 rcErase.right = rcErase.left + infoPtr->nItemWidth;
8959 rcErase.bottom = nPerCol * infoPtr->nItemHeight;
8960 OffsetRect(&rcErase, Origin.x, Origin.y);
8961 if (IntersectRect(&rcErase, &rcErase, &infoPtr->rcList))
8962 LISTVIEW_InvalidateRect(infoPtr, &rcErase);
8964 rcErase.left = (nFrom / nPerCol + 1) * infoPtr->nItemWidth;
8965 rcErase.top = 0;
8966 rcErase.right = (nTo / nPerCol + 1) * infoPtr->nItemWidth;
8967 rcErase.bottom = nPerCol * infoPtr->nItemHeight;
8968 OffsetRect(&rcErase, Origin.x, Origin.y);
8969 if (IntersectRect(&rcErase, &rcErase, &infoPtr->rcList))
8970 LISTVIEW_InvalidateRect(infoPtr, &rcErase);
8974 else
8976 /* According to MSDN for non-LVS_OWNERDATA this is just
8977 * a performance issue. The control allocates its internal
8978 * data structures for the number of items specified. It
8979 * cuts down on the number of memory allocations. Therefore
8980 * we will just issue a WARN here
8982 WARN("for non-ownerdata performance option not implemented.\n");
8985 return TRUE;
8988 /***
8989 * DESCRIPTION:
8990 * Sets the position of an item.
8992 * PARAMETER(S):
8993 * [I] infoPtr : valid pointer to the listview structure
8994 * [I] nItem : item index
8995 * [I] pt : coordinate
8997 * RETURN:
8998 * SUCCESS : TRUE
8999 * FAILURE : FALSE
9001 static BOOL LISTVIEW_SetItemPosition(LISTVIEW_INFO *infoPtr, INT nItem, const POINT *pt)
9003 POINT Origin, Pt;
9005 TRACE("(nItem=%d, pt=%s)\n", nItem, wine_dbgstr_point(pt));
9007 if (!pt || nItem < 0 || nItem >= infoPtr->nItemCount ||
9008 !(infoPtr->uView == LV_VIEW_ICON || infoPtr->uView == LV_VIEW_SMALLICON)) return FALSE;
9010 Pt = *pt;
9011 LISTVIEW_GetOrigin(infoPtr, &Origin);
9013 /* This point value seems to be an undocumented feature.
9014 * The best guess is that it means either at the origin,
9015 * or at true beginning of the list. I will assume the origin. */
9016 if ((Pt.x == -1) && (Pt.y == -1))
9017 Pt = Origin;
9019 if (infoPtr->uView == LV_VIEW_ICON)
9021 Pt.x -= (infoPtr->nItemWidth - infoPtr->iconSize.cx) / 2;
9022 Pt.y -= ICON_TOP_PADDING;
9024 Pt.x -= Origin.x;
9025 Pt.y -= Origin.y;
9027 return LISTVIEW_MoveIconTo(infoPtr, nItem, &Pt, FALSE);
9030 /* Make sure to also disable per item notifications via the notification mask. */
9031 static VOID LISTVIEW_SetOwnerDataState(LISTVIEW_INFO *infoPtr, INT nFirst, INT nLast, const LVITEMW *item)
9033 NMLVODSTATECHANGE nmlv;
9035 if (nFirst == nLast) return;
9036 if (!item) return;
9038 ZeroMemory(&nmlv, sizeof(nmlv));
9039 nmlv.iFrom = nFirst;
9040 nmlv.iTo = nLast;
9041 nmlv.uOldState = 0;
9042 nmlv.uNewState = item->state;
9044 notify_hdr(infoPtr, LVN_ODSTATECHANGED, (LPNMHDR)&nmlv);
9047 /***
9048 * DESCRIPTION:
9049 * Sets the state of one or many items.
9051 * PARAMETER(S):
9052 * [I] infoPtr : valid pointer to the listview structure
9053 * [I] nItem : item index
9054 * [I] item : item or subitem info
9056 * RETURN:
9057 * SUCCESS : TRUE
9058 * FAILURE : FALSE
9060 static BOOL LISTVIEW_SetItemState(LISTVIEW_INFO *infoPtr, INT nItem, const LVITEMW *item)
9062 BOOL ret = TRUE;
9063 LVITEMW lvItem;
9065 if (!item) return FALSE;
9067 lvItem.iItem = nItem;
9068 lvItem.iSubItem = 0;
9069 lvItem.mask = LVIF_STATE;
9070 lvItem.state = item->state;
9071 lvItem.stateMask = item->stateMask;
9072 TRACE("item=%s\n", debuglvitem_t(&lvItem, TRUE));
9074 if (nItem == -1)
9076 UINT oldstate = 0;
9077 DWORD old_mask;
9079 /* special case optimization for recurring attempt to deselect all */
9080 if (lvItem.state == 0 && lvItem.stateMask == LVIS_SELECTED && !LISTVIEW_GetSelectedCount(infoPtr))
9081 return TRUE;
9083 /* select all isn't allowed in LVS_SINGLESEL */
9084 if ((lvItem.state & lvItem.stateMask & LVIS_SELECTED) && (infoPtr->dwStyle & LVS_SINGLESEL))
9085 return FALSE;
9087 /* focus all isn't allowed */
9088 if (lvItem.state & lvItem.stateMask & LVIS_FOCUSED) return FALSE;
9090 old_mask = infoPtr->notify_mask & NOTIFY_MASK_ITEM_CHANGE;
9091 if (infoPtr->dwStyle & LVS_OWNERDATA)
9093 infoPtr->notify_mask &= ~NOTIFY_MASK_ITEM_CHANGE;
9094 if (!(lvItem.state & LVIS_SELECTED) && LISTVIEW_GetSelectedCount(infoPtr))
9095 oldstate |= LVIS_SELECTED;
9096 if (infoPtr->nFocusedItem != -1) oldstate |= LVIS_FOCUSED;
9099 /* apply to all items */
9100 for (lvItem.iItem = 0; lvItem.iItem < infoPtr->nItemCount; lvItem.iItem++)
9101 if (!LISTVIEW_SetItemT(infoPtr, &lvItem, TRUE)) ret = FALSE;
9103 if (infoPtr->dwStyle & LVS_OWNERDATA)
9105 NMLISTVIEW nmlv;
9107 infoPtr->notify_mask |= old_mask;
9109 nmlv.iItem = -1;
9110 nmlv.iSubItem = 0;
9111 nmlv.uNewState = lvItem.state & lvItem.stateMask;
9112 nmlv.uOldState = oldstate & lvItem.stateMask;
9113 nmlv.uChanged = LVIF_STATE;
9114 nmlv.ptAction.x = nmlv.ptAction.y = 0;
9115 nmlv.lParam = 0;
9117 notify_listview(infoPtr, LVN_ITEMCHANGED, &nmlv);
9120 else
9121 ret = LISTVIEW_SetItemT(infoPtr, &lvItem, TRUE);
9123 return ret;
9126 /***
9127 * DESCRIPTION:
9128 * Sets the text of an item or subitem.
9130 * PARAMETER(S):
9131 * [I] hwnd : window handle
9132 * [I] nItem : item index
9133 * [I] lpLVItem : item or subitem info
9134 * [I] isW : TRUE if input is Unicode
9136 * RETURN:
9137 * SUCCESS : TRUE
9138 * FAILURE : FALSE
9140 static BOOL LISTVIEW_SetItemTextT(LISTVIEW_INFO *infoPtr, INT nItem, const LVITEMW *lpLVItem, BOOL isW)
9142 LVITEMW lvItem;
9144 if (!lpLVItem || nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
9145 if (infoPtr->dwStyle & LVS_OWNERDATA) return FALSE;
9147 lvItem.iItem = nItem;
9148 lvItem.iSubItem = lpLVItem->iSubItem;
9149 lvItem.mask = LVIF_TEXT;
9150 lvItem.pszText = lpLVItem->pszText;
9151 lvItem.cchTextMax = lpLVItem->cchTextMax;
9153 TRACE("(nItem=%d, lpLVItem=%s, isW=%d)\n", nItem, debuglvitem_t(&lvItem, isW), isW);
9155 return LISTVIEW_SetItemT(infoPtr, &lvItem, isW);
9158 /***
9159 * DESCRIPTION:
9160 * Set item index that marks the start of a multiple selection.
9162 * PARAMETER(S):
9163 * [I] infoPtr : valid pointer to the listview structure
9164 * [I] nIndex : index
9166 * RETURN:
9167 * Index number or -1 if there is no selection mark.
9169 static INT LISTVIEW_SetSelectionMark(LISTVIEW_INFO *infoPtr, INT nIndex)
9171 INT nOldIndex = infoPtr->nSelectionMark;
9173 TRACE("(nIndex=%d)\n", nIndex);
9175 if (nIndex >= -1 && nIndex < infoPtr->nItemCount)
9176 infoPtr->nSelectionMark = nIndex;
9178 return nOldIndex;
9181 /***
9182 * DESCRIPTION:
9183 * Sets the text background color.
9185 * PARAMETER(S):
9186 * [I] infoPtr : valid pointer to the listview structure
9187 * [I] color : text background color
9189 * RETURN:
9190 * SUCCESS : TRUE
9191 * FAILURE : FALSE
9193 static BOOL LISTVIEW_SetTextBkColor(LISTVIEW_INFO *infoPtr, COLORREF color)
9195 TRACE("color %#lx\n", color);
9197 infoPtr->clrTextBk = color;
9198 return TRUE;
9201 /***
9202 * DESCRIPTION:
9203 * Sets the text foreground color.
9205 * PARAMETER(S):
9206 * [I] infoPtr : valid pointer to the listview structure
9207 * [I] color : text color
9209 * RETURN:
9210 * SUCCESS : TRUE
9211 * FAILURE : FALSE
9213 static BOOL LISTVIEW_SetTextColor (LISTVIEW_INFO *infoPtr, COLORREF color)
9215 TRACE("color %#lx\n", color);
9217 infoPtr->clrText = color;
9218 return TRUE;
9221 /***
9222 * DESCRIPTION:
9223 * Sets new ToolTip window to ListView control.
9225 * PARAMETER(S):
9226 * [I] infoPtr : valid pointer to the listview structure
9227 * [I] hwndNewToolTip : handle to new ToolTip
9229 * RETURN:
9230 * old tool tip
9232 static HWND LISTVIEW_SetToolTips( LISTVIEW_INFO *infoPtr, HWND hwndNewToolTip)
9234 HWND hwndOldToolTip = infoPtr->hwndToolTip;
9235 infoPtr->hwndToolTip = hwndNewToolTip;
9236 return hwndOldToolTip;
9240 * DESCRIPTION:
9241 * sets the Unicode character format flag for the control
9242 * PARAMETER(S):
9243 * [I] infoPtr :valid pointer to the listview structure
9244 * [I] fUnicode :true to switch to UNICODE false to switch to ANSI
9246 * RETURN:
9247 * Old Unicode Format
9249 static BOOL LISTVIEW_SetUnicodeFormat( LISTVIEW_INFO *infoPtr, BOOL unicode)
9251 SHORT rc = infoPtr->notifyFormat;
9252 infoPtr->notifyFormat = (unicode) ? NFR_UNICODE : NFR_ANSI;
9253 return rc == NFR_UNICODE;
9257 * DESCRIPTION:
9258 * sets the control view mode
9259 * PARAMETER(S):
9260 * [I] infoPtr :valid pointer to the listview structure
9261 * [I] nView :new view mode value
9263 * RETURN:
9264 * SUCCESS: 1
9265 * FAILURE: -1
9267 static INT LISTVIEW_SetView(LISTVIEW_INFO *infoPtr, DWORD nView)
9269 HIMAGELIST himl;
9271 if (infoPtr->uView == nView) return 1;
9273 if ((INT)nView < 0 || nView > LV_VIEW_MAX) return -1;
9274 if (nView == LV_VIEW_TILE)
9276 FIXME("View LV_VIEW_TILE unimplemented\n");
9277 return -1;
9280 infoPtr->uView = nView;
9282 SendMessageW(infoPtr->hwndEdit, WM_KILLFOCUS, 0, 0);
9283 ShowWindow(infoPtr->hwndHeader, SW_HIDE);
9285 ShowScrollBar(infoPtr->hwndSelf, SB_BOTH, FALSE);
9286 SetRectEmpty(&infoPtr->rcFocus);
9288 himl = (nView == LV_VIEW_ICON ? infoPtr->himlNormal : infoPtr->himlSmall);
9289 set_icon_size(&infoPtr->iconSize, himl, nView != LV_VIEW_ICON);
9291 switch (nView)
9293 case LV_VIEW_ICON:
9294 case LV_VIEW_SMALLICON:
9295 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
9296 break;
9297 case LV_VIEW_DETAILS:
9299 HDLAYOUT hl;
9300 WINDOWPOS wp;
9302 LISTVIEW_CreateHeader( infoPtr );
9304 hl.prc = &infoPtr->rcList;
9305 hl.pwpos = &wp;
9306 SendMessageW(infoPtr->hwndHeader, HDM_LAYOUT, 0, (LPARAM)&hl);
9307 SetWindowPos(infoPtr->hwndHeader, infoPtr->hwndSelf, wp.x, wp.y, wp.cx, wp.cy,
9308 wp.flags | ((infoPtr->dwStyle & LVS_NOCOLUMNHEADER) ? SWP_HIDEWINDOW : SWP_SHOWWINDOW));
9309 break;
9311 case LV_VIEW_LIST:
9312 break;
9315 LISTVIEW_UpdateItemSize(infoPtr);
9316 LISTVIEW_UpdateSize(infoPtr);
9317 LISTVIEW_UpdateScroll(infoPtr);
9318 LISTVIEW_InvalidateList(infoPtr);
9320 TRACE("nView %ld\n", nView);
9322 return 1;
9325 /* LISTVIEW_SetWorkAreas */
9327 struct sorting_context
9329 HDPA items;
9330 PFNLVCOMPARE compare_func;
9331 LPARAM lParam;
9334 /* DPA_Sort() callback used for LVM_SORTITEMS */
9335 static INT WINAPI LISTVIEW_CallBackCompare(LPVOID first, LPVOID second, LPARAM lParam)
9337 struct sorting_context *context = (struct sorting_context *)lParam;
9338 ITEM_INFO* lv_first = DPA_GetPtr( first, 0 );
9339 ITEM_INFO* lv_second = DPA_GetPtr( second, 0 );
9341 return context->compare_func(lv_first->lParam, lv_second->lParam, context->lParam);
9344 /* DPA_Sort() callback used for LVM_SORTITEMSEX */
9345 static INT WINAPI LISTVIEW_CallBackCompareEx(LPVOID first, LPVOID second, LPARAM lParam)
9347 struct sorting_context *context = (struct sorting_context *)lParam;
9348 INT first_idx = DPA_GetPtrIndex( context->items, first );
9349 INT second_idx = DPA_GetPtrIndex( context->items, second );
9351 return context->compare_func(first_idx, second_idx, context->lParam);
9354 /***
9355 * DESCRIPTION:
9356 * Sorts the listview items.
9358 * PARAMETER(S):
9359 * [I] infoPtr : valid pointer to the listview structure
9360 * [I] pfnCompare : application-defined value
9361 * [I] lParamSort : pointer to comparison callback
9362 * [I] IsEx : TRUE when LVM_SORTITEMSEX used
9364 * RETURN:
9365 * SUCCESS : TRUE
9366 * FAILURE : FALSE
9368 static BOOL LISTVIEW_SortItems(LISTVIEW_INFO *infoPtr, PFNLVCOMPARE pfnCompare,
9369 LPARAM lParamSort, BOOL IsEx)
9371 HDPA hdpaSubItems, hdpaItems;
9372 ITEM_INFO *lpItem;
9373 LPVOID selectionMarkItem = NULL;
9374 LPVOID focusedItem = NULL;
9375 struct sorting_context context;
9376 int i;
9378 TRACE("pfnCompare %p, lParamSort %Ix\n", pfnCompare, lParamSort);
9380 if (infoPtr->dwStyle & LVS_OWNERDATA) return FALSE;
9382 if (!pfnCompare) return FALSE;
9383 if (!infoPtr->hdpaItems) return FALSE;
9385 /* if there are 0 or 1 items, there is no need to sort */
9386 if (infoPtr->nItemCount < 2) return TRUE;
9387 if (!(hdpaItems = DPA_Clone(infoPtr->hdpaItems, NULL))) return FALSE;
9389 /* clear selection */
9390 ranges_clear(infoPtr->selectionRanges);
9392 /* save selection mark and focused item */
9393 if (infoPtr->nSelectionMark >= 0)
9394 selectionMarkItem = DPA_GetPtr(infoPtr->hdpaItems, infoPtr->nSelectionMark);
9395 if (infoPtr->nFocusedItem >= 0)
9396 focusedItem = DPA_GetPtr(infoPtr->hdpaItems, infoPtr->nFocusedItem);
9398 context.items = hdpaItems;
9399 context.compare_func = pfnCompare;
9400 context.lParam = lParamSort;
9401 if (IsEx)
9402 DPA_Sort(hdpaItems, LISTVIEW_CallBackCompareEx, (LPARAM)&context);
9403 else
9404 DPA_Sort(hdpaItems, LISTVIEW_CallBackCompare, (LPARAM)&context);
9405 DPA_Destroy(infoPtr->hdpaItems);
9406 infoPtr->hdpaItems = hdpaItems;
9408 /* restore selection ranges */
9409 for (i=0; i < infoPtr->nItemCount; i++)
9411 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, i);
9412 lpItem = DPA_GetPtr(hdpaSubItems, 0);
9414 if (lpItem->state & LVIS_SELECTED)
9415 ranges_additem(infoPtr->selectionRanges, i);
9417 /* restore selection mark and focused item */
9418 infoPtr->nSelectionMark = DPA_GetPtrIndex(infoPtr->hdpaItems, selectionMarkItem);
9419 infoPtr->nFocusedItem = DPA_GetPtrIndex(infoPtr->hdpaItems, focusedItem);
9421 /* I believe nHotItem should be left alone, see LISTVIEW_ShiftIndices */
9423 /* refresh the display */
9424 LISTVIEW_InvalidateList(infoPtr);
9425 return TRUE;
9428 /***
9429 * DESCRIPTION:
9430 * Update theme handle after a theme change.
9432 * PARAMETER(S):
9433 * [I] infoPtr : valid pointer to the listview structure
9435 * RETURN:
9436 * SUCCESS : 0
9437 * FAILURE : something else
9439 static LRESULT LISTVIEW_ThemeChanged(const LISTVIEW_INFO *infoPtr)
9441 HTHEME theme = GetWindowTheme(infoPtr->hwndSelf);
9442 CloseThemeData(theme);
9443 OpenThemeData(infoPtr->hwndSelf, themeClass);
9444 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
9445 return 0;
9448 /***
9449 * DESCRIPTION:
9450 * Updates an items or rearranges the listview control.
9452 * PARAMETER(S):
9453 * [I] infoPtr : valid pointer to the listview structure
9454 * [I] nItem : item index
9456 * RETURN:
9457 * SUCCESS : TRUE
9458 * FAILURE : FALSE
9460 static BOOL LISTVIEW_Update(LISTVIEW_INFO *infoPtr, INT nItem)
9462 TRACE("(nItem=%d)\n", nItem);
9464 if (nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
9466 /* rearrange with default alignment style */
9467 if (is_autoarrange(infoPtr))
9468 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
9469 else
9470 LISTVIEW_InvalidateItem(infoPtr, nItem);
9472 return TRUE;
9475 /***
9476 * DESCRIPTION:
9477 * Draw the track line at the place defined in the infoPtr structure.
9478 * The line is drawn with a XOR pen so drawing the line for the second time
9479 * in the same place erases the line.
9481 * PARAMETER(S):
9482 * [I] infoPtr : valid pointer to the listview structure
9484 * RETURN:
9485 * SUCCESS : TRUE
9486 * FAILURE : FALSE
9488 static BOOL LISTVIEW_DrawTrackLine(const LISTVIEW_INFO *infoPtr)
9490 HDC hdc;
9492 if (infoPtr->xTrackLine == -1)
9493 return FALSE;
9495 if (!(hdc = GetDC(infoPtr->hwndSelf)))
9496 return FALSE;
9497 PatBlt( hdc, infoPtr->xTrackLine, infoPtr->rcList.top,
9498 1, infoPtr->rcList.bottom - infoPtr->rcList.top, DSTINVERT );
9499 ReleaseDC(infoPtr->hwndSelf, hdc);
9500 return TRUE;
9503 /***
9504 * DESCRIPTION:
9505 * Called when an edit control should be displayed. This function is called after
9506 * we are sure that there was a single click - not a double click (this is a TIMERPROC).
9508 * PARAMETER(S):
9509 * [I] hwnd : Handle to the listview
9510 * [I] uMsg : WM_TIMER (ignored)
9511 * [I] idEvent : The timer ID interpreted as a pointer to a DELAYED_EDIT_ITEM struct
9512 * [I] dwTimer : The elapsed time (ignored)
9514 * RETURN:
9515 * None.
9517 static VOID CALLBACK LISTVIEW_DelayedEditItem(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime)
9519 DELAYED_ITEM_EDIT *editItem = (DELAYED_ITEM_EDIT *)idEvent;
9520 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)GetWindowLongPtrW(hwnd, 0);
9522 KillTimer(hwnd, idEvent);
9523 editItem->fEnabled = FALSE;
9524 /* check if the item is still selected */
9525 if (infoPtr->bFocus && LISTVIEW_GetItemState(infoPtr, editItem->iItem, LVIS_SELECTED))
9526 LISTVIEW_EditLabelT(infoPtr, editItem->iItem, TRUE);
9529 /***
9530 * DESCRIPTION:
9531 * Creates the listview control - the WM_NCCREATE phase.
9533 * PARAMETER(S):
9534 * [I] hwnd : window handle
9535 * [I] lpcs : the create parameters
9537 * RETURN:
9538 * Success: TRUE
9539 * Failure: FALSE
9541 static LRESULT LISTVIEW_NCCreate(HWND hwnd, WPARAM wParam, const CREATESTRUCTW *lpcs)
9543 LISTVIEW_INFO *infoPtr;
9544 LOGFONTW logFont;
9546 TRACE("(lpcs=%p)\n", lpcs);
9548 /* initialize info pointer */
9549 infoPtr = Alloc(sizeof(LISTVIEW_INFO));
9550 if (!infoPtr) return FALSE;
9552 SetWindowLongPtrW(hwnd, 0, (DWORD_PTR)infoPtr);
9554 infoPtr->hwndSelf = hwnd;
9555 infoPtr->dwStyle = lpcs->style; /* Note: may be changed in WM_CREATE */
9556 map_style_view(infoPtr);
9557 /* determine the type of structures to use */
9558 infoPtr->hwndNotify = lpcs->hwndParent;
9559 /* infoPtr->notifyFormat will be filled in WM_CREATE */
9561 /* initialize color information */
9562 infoPtr->clrBk = CLR_NONE;
9563 infoPtr->clrText = CLR_DEFAULT;
9564 infoPtr->clrTextBk = CLR_DEFAULT;
9565 LISTVIEW_SetBkColor(infoPtr, comctl32_color.clrWindow);
9567 /* set default values */
9568 infoPtr->nFocusedItem = -1;
9569 infoPtr->nSelectionMark = -1;
9570 infoPtr->nHotItem = -1;
9571 infoPtr->redraw = TRUE;
9572 infoPtr->bNoItemMetrics = TRUE;
9573 infoPtr->notify_mask = NOTIFY_MASK_UNMASK_ALL;
9574 infoPtr->autoSpacing = TRUE;
9575 infoPtr->iconSpacing.cx = GetSystemMetrics(SM_CXICONSPACING) - GetSystemMetrics(SM_CXICON);
9576 infoPtr->iconSpacing.cy = GetSystemMetrics(SM_CYICONSPACING) - GetSystemMetrics(SM_CYICON);
9577 infoPtr->nEditLabelItem = -1;
9578 infoPtr->nLButtonDownItem = -1;
9579 infoPtr->dwHoverTime = HOVER_DEFAULT; /* default system hover time */
9580 infoPtr->cWheelRemainder = 0;
9581 infoPtr->nMeasureItemHeight = 0;
9582 infoPtr->xTrackLine = -1; /* no track line */
9583 infoPtr->itemEdit.fEnabled = FALSE;
9584 infoPtr->iVersion = COMCTL32_VERSION;
9585 infoPtr->colRectsDirty = FALSE;
9586 infoPtr->selected_column = -1;
9588 /* get default font (icon title) */
9589 SystemParametersInfoW(SPI_GETICONTITLELOGFONT, 0, &logFont, 0);
9590 infoPtr->hDefaultFont = CreateFontIndirectW(&logFont);
9591 infoPtr->hFont = infoPtr->hDefaultFont;
9592 LISTVIEW_SaveTextMetrics(infoPtr);
9594 /* allocate memory for the data structure */
9595 if (!(infoPtr->selectionRanges = ranges_create(10))) goto fail;
9596 if (!(infoPtr->hdpaItems = DPA_Create(10))) goto fail;
9597 if (!(infoPtr->hdpaItemIds = DPA_Create(10))) goto fail;
9598 if (!(infoPtr->hdpaPosX = DPA_Create(10))) goto fail;
9599 if (!(infoPtr->hdpaPosY = DPA_Create(10))) goto fail;
9600 if (!(infoPtr->hdpaColumns = DPA_Create(10))) goto fail;
9602 return DefWindowProcW(hwnd, WM_NCCREATE, wParam, (LPARAM)lpcs);
9604 fail:
9605 DestroyWindow(infoPtr->hwndHeader);
9606 ranges_destroy(infoPtr->selectionRanges);
9607 DPA_Destroy(infoPtr->hdpaItems);
9608 DPA_Destroy(infoPtr->hdpaItemIds);
9609 DPA_Destroy(infoPtr->hdpaPosX);
9610 DPA_Destroy(infoPtr->hdpaPosY);
9611 DPA_Destroy(infoPtr->hdpaColumns);
9612 Free(infoPtr);
9613 return FALSE;
9616 /***
9617 * DESCRIPTION:
9618 * Creates the listview control - the WM_CREATE phase. Most of the data is
9619 * already set up in LISTVIEW_NCCreate
9621 * PARAMETER(S):
9622 * [I] hwnd : window handle
9623 * [I] lpcs : the create parameters
9625 * RETURN:
9626 * Success: 0
9627 * Failure: -1
9629 static LRESULT LISTVIEW_Create(HWND hwnd, const CREATESTRUCTW *lpcs)
9631 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)GetWindowLongPtrW(hwnd, 0);
9633 TRACE("lpcs %p, style %#lx\n", lpcs, lpcs->style);
9635 infoPtr->dwStyle = lpcs->style;
9636 map_style_view(infoPtr);
9638 infoPtr->notifyFormat = SendMessageW(infoPtr->hwndNotify, WM_NOTIFYFORMAT,
9639 (WPARAM)infoPtr->hwndSelf, NF_QUERY);
9640 /* on error defaulting to ANSI notifications */
9641 if (infoPtr->notifyFormat == 0) infoPtr->notifyFormat = NFR_ANSI;
9642 TRACE("notify format=%d\n", infoPtr->notifyFormat);
9644 if ((infoPtr->uView == LV_VIEW_DETAILS) && (lpcs->style & WS_VISIBLE))
9646 if (LISTVIEW_CreateHeader(infoPtr) < 0) return -1;
9648 else
9649 infoPtr->hwndHeader = 0;
9651 /* init item size to avoid division by 0 */
9652 LISTVIEW_UpdateItemSize (infoPtr);
9653 LISTVIEW_UpdateSize (infoPtr);
9655 if (infoPtr->uView == LV_VIEW_DETAILS)
9657 if (!(LVS_NOCOLUMNHEADER & lpcs->style) && (WS_VISIBLE & lpcs->style))
9659 ShowWindow(infoPtr->hwndHeader, SW_SHOWNORMAL);
9661 LISTVIEW_UpdateScroll(infoPtr);
9662 /* send WM_MEASUREITEM notification */
9663 if (infoPtr->dwStyle & LVS_OWNERDRAWFIXED) notify_measureitem(infoPtr);
9666 OpenThemeData(hwnd, themeClass);
9668 /* initialize the icon sizes */
9669 set_icon_size(&infoPtr->iconSize, infoPtr->himlNormal, infoPtr->uView != LV_VIEW_ICON);
9670 set_icon_size(&infoPtr->iconStateSize, infoPtr->himlState, TRUE);
9671 return 0;
9674 /***
9675 * DESCRIPTION:
9676 * Destroys the listview control.
9678 * PARAMETER(S):
9679 * [I] infoPtr : valid pointer to the listview structure
9681 * RETURN:
9682 * Success: 0
9683 * Failure: -1
9685 static LRESULT LISTVIEW_Destroy(LISTVIEW_INFO *infoPtr)
9687 HTHEME theme = GetWindowTheme(infoPtr->hwndSelf);
9688 CloseThemeData(theme);
9690 /* delete all items */
9691 LISTVIEW_DeleteAllItems(infoPtr, TRUE);
9693 return 0;
9696 /***
9697 * DESCRIPTION:
9698 * Enables the listview control.
9700 * PARAMETER(S):
9701 * [I] infoPtr : valid pointer to the listview structure
9702 * [I] bEnable : specifies whether to enable or disable the window
9704 * RETURN:
9705 * SUCCESS : TRUE
9706 * FAILURE : FALSE
9708 static BOOL LISTVIEW_Enable(const LISTVIEW_INFO *infoPtr)
9710 if (infoPtr->dwStyle & LVS_OWNERDRAWFIXED)
9711 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
9712 return TRUE;
9715 /***
9716 * DESCRIPTION:
9717 * Erases the background of the listview control.
9719 * PARAMETER(S):
9720 * [I] infoPtr : valid pointer to the listview structure
9721 * [I] hdc : device context handle
9723 * RETURN:
9724 * SUCCESS : TRUE
9725 * FAILURE : FALSE
9727 static inline BOOL LISTVIEW_EraseBkgnd(const LISTVIEW_INFO *infoPtr, HDC hdc)
9729 RECT rc;
9731 TRACE("(hdc=%p)\n", hdc);
9733 if (!GetClipBox(hdc, &rc)) return FALSE;
9735 if (infoPtr->clrBk == CLR_NONE)
9737 if (infoPtr->dwLvExStyle & LVS_EX_TRANSPARENTBKGND)
9738 SendMessageW(infoPtr->hwndNotify, WM_PRINTCLIENT, (WPARAM)hdc, PRF_ERASEBKGND);
9739 else
9740 SendMessageW(infoPtr->hwndNotify, WM_ERASEBKGND, (WPARAM)hdc, 0);
9741 LISTVIEW_DrawBackgroundBitmap(infoPtr, hdc, &rc);
9742 return TRUE;
9745 /* for double buffered controls we need to do this during refresh */
9746 if (infoPtr->dwLvExStyle & LVS_EX_DOUBLEBUFFER) return FALSE;
9748 return LISTVIEW_FillBkgnd(infoPtr, hdc, &rc);
9752 /***
9753 * DESCRIPTION:
9754 * Helper function for LISTVIEW_[HV]Scroll *only*.
9755 * Performs vertical/horizontal scrolling by a give amount.
9757 * PARAMETER(S):
9758 * [I] infoPtr : valid pointer to the listview structure
9759 * [I] dx : amount of horizontal scroll
9760 * [I] dy : amount of vertical scroll
9762 static void scroll_list(LISTVIEW_INFO *infoPtr, INT dx, INT dy)
9764 /* now we can scroll the list */
9765 ScrollWindowEx(infoPtr->hwndSelf, dx, dy, &infoPtr->rcList,
9766 &infoPtr->rcList, 0, 0, SW_ERASE | SW_INVALIDATE);
9767 /* if we have focus, adjust rect */
9768 OffsetRect(&infoPtr->rcFocus, dx, dy);
9769 UpdateWindow(infoPtr->hwndSelf);
9772 /***
9773 * DESCRIPTION:
9774 * Performs vertical scrolling.
9776 * PARAMETER(S):
9777 * [I] infoPtr : valid pointer to the listview structure
9778 * [I] nScrollCode : scroll code
9779 * [I] nScrollDiff : units to scroll in SB_INTERNAL mode, 0 otherwise
9780 * [I] hScrollWnd : scrollbar control window handle
9782 * RETURN:
9783 * Zero
9785 * NOTES:
9786 * SB_LINEUP/SB_LINEDOWN:
9787 * for LVS_ICON, LVS_SMALLICON is 37 by experiment
9788 * for LVS_REPORT is 1 line
9789 * for LVS_LIST cannot occur
9792 static LRESULT LISTVIEW_VScroll(LISTVIEW_INFO *infoPtr, INT nScrollCode,
9793 INT nScrollDiff)
9795 INT nOldScrollPos, nNewScrollPos;
9796 SCROLLINFO scrollInfo;
9797 BOOL is_an_icon;
9799 TRACE("(nScrollCode=%d(%s), nScrollDiff=%d)\n", nScrollCode,
9800 debugscrollcode(nScrollCode), nScrollDiff);
9802 if (infoPtr->hwndEdit) SendMessageW(infoPtr->hwndEdit, WM_KILLFOCUS, 0, 0);
9804 scrollInfo.cbSize = sizeof(SCROLLINFO);
9805 scrollInfo.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_TRACKPOS;
9807 is_an_icon = ((infoPtr->uView == LV_VIEW_ICON) || (infoPtr->uView == LV_VIEW_SMALLICON));
9809 if (!GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo)) return 1;
9811 nOldScrollPos = scrollInfo.nPos;
9812 switch (nScrollCode)
9814 case SB_INTERNAL:
9815 break;
9817 case SB_LINEUP:
9818 nScrollDiff = (is_an_icon) ? -LISTVIEW_SCROLL_ICON_LINE_SIZE : -1;
9819 break;
9821 case SB_LINEDOWN:
9822 nScrollDiff = (is_an_icon) ? LISTVIEW_SCROLL_ICON_LINE_SIZE : 1;
9823 break;
9825 case SB_PAGEUP:
9826 nScrollDiff = -scrollInfo.nPage;
9827 break;
9829 case SB_PAGEDOWN:
9830 nScrollDiff = scrollInfo.nPage;
9831 break;
9833 case SB_THUMBPOSITION:
9834 case SB_THUMBTRACK:
9835 nScrollDiff = scrollInfo.nTrackPos - scrollInfo.nPos;
9836 break;
9838 default:
9839 nScrollDiff = 0;
9842 /* quit right away if pos isn't changing */
9843 if (nScrollDiff == 0) return 0;
9845 /* calculate new position, and handle overflows */
9846 nNewScrollPos = scrollInfo.nPos + nScrollDiff;
9847 if (nScrollDiff > 0) {
9848 if (nNewScrollPos < nOldScrollPos ||
9849 nNewScrollPos > scrollInfo.nMax)
9850 nNewScrollPos = scrollInfo.nMax;
9851 } else {
9852 if (nNewScrollPos > nOldScrollPos ||
9853 nNewScrollPos < scrollInfo.nMin)
9854 nNewScrollPos = scrollInfo.nMin;
9857 /* set the new position, and reread in case it changed */
9858 scrollInfo.fMask = SIF_POS;
9859 scrollInfo.nPos = nNewScrollPos;
9860 nNewScrollPos = SetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo, TRUE);
9862 /* carry on only if it really changed */
9863 if (nNewScrollPos == nOldScrollPos) return 0;
9865 /* now adjust to client coordinates */
9866 nScrollDiff = nOldScrollPos - nNewScrollPos;
9867 if (infoPtr->uView == LV_VIEW_DETAILS) nScrollDiff *= infoPtr->nItemHeight;
9869 /* and scroll the window */
9870 scroll_list(infoPtr, 0, nScrollDiff);
9872 return 0;
9875 /***
9876 * DESCRIPTION:
9877 * Performs horizontal scrolling.
9879 * PARAMETER(S):
9880 * [I] infoPtr : valid pointer to the listview structure
9881 * [I] nScrollCode : scroll code
9882 * [I] nScrollDiff : units to scroll in SB_INTERNAL mode, 0 otherwise
9883 * [I] hScrollWnd : scrollbar control window handle
9885 * RETURN:
9886 * Zero
9888 * NOTES:
9889 * SB_LINELEFT/SB_LINERIGHT:
9890 * for LVS_ICON, LVS_SMALLICON 1 pixel
9891 * for LVS_REPORT is 1 pixel
9892 * for LVS_LIST is 1 column --> which is a 1 because the
9893 * scroll is based on columns not pixels
9896 static LRESULT LISTVIEW_HScroll(LISTVIEW_INFO *infoPtr, INT nScrollCode,
9897 INT nScrollDiff)
9899 INT nOldScrollPos, nNewScrollPos;
9900 SCROLLINFO scrollInfo;
9901 BOOL is_an_icon;
9903 TRACE("(nScrollCode=%d(%s), nScrollDiff=%d)\n", nScrollCode,
9904 debugscrollcode(nScrollCode), nScrollDiff);
9906 if (infoPtr->hwndEdit) SendMessageW(infoPtr->hwndEdit, WM_KILLFOCUS, 0, 0);
9908 scrollInfo.cbSize = sizeof(SCROLLINFO);
9909 scrollInfo.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_TRACKPOS;
9911 is_an_icon = ((infoPtr->uView == LV_VIEW_ICON) || (infoPtr->uView == LV_VIEW_SMALLICON));
9913 if (!GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo)) return 1;
9915 nOldScrollPos = scrollInfo.nPos;
9917 switch (nScrollCode)
9919 case SB_INTERNAL:
9920 break;
9922 case SB_LINELEFT:
9923 nScrollDiff = (is_an_icon) ? -LISTVIEW_SCROLL_ICON_LINE_SIZE : -1;
9924 break;
9926 case SB_LINERIGHT:
9927 nScrollDiff = (is_an_icon) ? LISTVIEW_SCROLL_ICON_LINE_SIZE : 1;
9928 break;
9930 case SB_PAGELEFT:
9931 nScrollDiff = -scrollInfo.nPage;
9932 break;
9934 case SB_PAGERIGHT:
9935 nScrollDiff = scrollInfo.nPage;
9936 break;
9938 case SB_THUMBPOSITION:
9939 case SB_THUMBTRACK:
9940 nScrollDiff = scrollInfo.nTrackPos - scrollInfo.nPos;
9941 break;
9943 default:
9944 nScrollDiff = 0;
9947 /* quit right away if pos isn't changing */
9948 if (nScrollDiff == 0) return 0;
9950 /* calculate new position, and handle overflows */
9951 nNewScrollPos = scrollInfo.nPos + nScrollDiff;
9952 if (nScrollDiff > 0) {
9953 if (nNewScrollPos < nOldScrollPos ||
9954 nNewScrollPos > scrollInfo.nMax)
9955 nNewScrollPos = scrollInfo.nMax;
9956 } else {
9957 if (nNewScrollPos > nOldScrollPos ||
9958 nNewScrollPos < scrollInfo.nMin)
9959 nNewScrollPos = scrollInfo.nMin;
9962 /* set the new position, and reread in case it changed */
9963 scrollInfo.fMask = SIF_POS;
9964 scrollInfo.nPos = nNewScrollPos;
9965 nNewScrollPos = SetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo, TRUE);
9967 /* carry on only if it really changed */
9968 if (nNewScrollPos == nOldScrollPos) return 0;
9970 LISTVIEW_UpdateHeaderSize(infoPtr, nNewScrollPos);
9972 /* now adjust to client coordinates */
9973 nScrollDiff = nOldScrollPos - nNewScrollPos;
9974 if (infoPtr->uView == LV_VIEW_LIST) nScrollDiff *= infoPtr->nItemWidth;
9976 /* and scroll the window */
9977 scroll_list(infoPtr, nScrollDiff, 0);
9979 return 0;
9982 static LRESULT LISTVIEW_MouseWheel(LISTVIEW_INFO *infoPtr, INT wheelDelta)
9984 INT pulScrollLines = 3;
9986 TRACE("(wheelDelta=%d)\n", wheelDelta);
9988 switch(infoPtr->uView)
9990 case LV_VIEW_ICON:
9991 case LV_VIEW_SMALLICON:
9993 * listview should be scrolled by a multiple of 37 dependently on its dimension or its visible item number
9994 * should be fixed in the future.
9996 LISTVIEW_VScroll(infoPtr, SB_INTERNAL, (wheelDelta > 0) ?
9997 -LISTVIEW_SCROLL_ICON_LINE_SIZE : LISTVIEW_SCROLL_ICON_LINE_SIZE);
9998 break;
10000 case LV_VIEW_DETAILS:
10001 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES,0, &pulScrollLines, 0);
10003 /* if scrolling changes direction, ignore left overs */
10004 if ((wheelDelta < 0 && infoPtr->cWheelRemainder < 0) ||
10005 (wheelDelta > 0 && infoPtr->cWheelRemainder > 0))
10006 infoPtr->cWheelRemainder += wheelDelta;
10007 else
10008 infoPtr->cWheelRemainder = wheelDelta;
10009 if (infoPtr->cWheelRemainder && pulScrollLines)
10011 int cLineScroll;
10012 pulScrollLines = min((UINT)LISTVIEW_GetCountPerColumn(infoPtr), pulScrollLines);
10013 cLineScroll = pulScrollLines * infoPtr->cWheelRemainder / WHEEL_DELTA;
10014 infoPtr->cWheelRemainder -= WHEEL_DELTA * cLineScroll / pulScrollLines;
10015 LISTVIEW_VScroll(infoPtr, SB_INTERNAL, -cLineScroll);
10017 break;
10019 case LV_VIEW_LIST:
10020 LISTVIEW_HScroll(infoPtr, (wheelDelta > 0) ? SB_LINELEFT : SB_LINERIGHT, 0);
10021 break;
10023 return 0;
10026 /***
10027 * DESCRIPTION:
10028 * ???
10030 * PARAMETER(S):
10031 * [I] infoPtr : valid pointer to the listview structure
10032 * [I] nVirtualKey : virtual key
10033 * [I] lKeyData : key data
10035 * RETURN:
10036 * Zero
10038 static LRESULT LISTVIEW_KeyDown(LISTVIEW_INFO *infoPtr, INT nVirtualKey, LONG lKeyData)
10040 HWND hwndSelf = infoPtr->hwndSelf;
10041 INT nItem = -1;
10042 NMLVKEYDOWN nmKeyDown;
10044 TRACE("(nVirtualKey=%d, lKeyData=%ld)\n", nVirtualKey, lKeyData);
10046 /* send LVN_KEYDOWN notification */
10047 nmKeyDown.wVKey = nVirtualKey;
10048 nmKeyDown.flags = 0;
10049 notify_hdr(infoPtr, LVN_KEYDOWN, &nmKeyDown.hdr);
10050 if (!IsWindow(hwndSelf))
10051 return 0;
10053 switch (nVirtualKey)
10055 case VK_SPACE:
10056 nItem = infoPtr->nFocusedItem;
10057 if (infoPtr->dwLvExStyle & LVS_EX_CHECKBOXES)
10058 toggle_checkbox_state(infoPtr, infoPtr->nFocusedItem);
10059 break;
10061 case VK_RETURN:
10062 if ((infoPtr->nItemCount > 0) && (infoPtr->nFocusedItem != -1))
10064 if (!notify(infoPtr, NM_RETURN)) return 0;
10065 if (!notify(infoPtr, LVN_ITEMACTIVATE)) return 0;
10067 break;
10069 case VK_HOME:
10070 if (infoPtr->nItemCount > 0)
10071 nItem = 0;
10072 break;
10074 case VK_END:
10075 if (infoPtr->nItemCount > 0)
10076 nItem = infoPtr->nItemCount - 1;
10077 break;
10079 case VK_LEFT:
10080 nItem = LISTVIEW_GetNextItem(infoPtr, infoPtr->nFocusedItem, LVNI_TOLEFT);
10081 break;
10083 case VK_UP:
10084 nItem = LISTVIEW_GetNextItem(infoPtr, infoPtr->nFocusedItem, LVNI_ABOVE);
10085 break;
10087 case VK_RIGHT:
10088 nItem = LISTVIEW_GetNextItem(infoPtr, infoPtr->nFocusedItem, LVNI_TORIGHT);
10089 break;
10091 case VK_DOWN:
10092 nItem = LISTVIEW_GetNextItem(infoPtr, infoPtr->nFocusedItem, LVNI_BELOW);
10093 break;
10095 case VK_PRIOR:
10096 if (infoPtr->uView == LV_VIEW_DETAILS)
10098 INT topidx = LISTVIEW_GetTopIndex(infoPtr);
10099 if (infoPtr->nFocusedItem == topidx)
10100 nItem = topidx - LISTVIEW_GetCountPerColumn(infoPtr) + 1;
10101 else
10102 nItem = topidx;
10104 else
10105 nItem = infoPtr->nFocusedItem - LISTVIEW_GetCountPerColumn(infoPtr)
10106 * LISTVIEW_GetCountPerRow(infoPtr);
10107 if(nItem < 0) nItem = 0;
10108 break;
10110 case VK_NEXT:
10111 if (infoPtr->uView == LV_VIEW_DETAILS)
10113 INT topidx = LISTVIEW_GetTopIndex(infoPtr);
10114 INT cnt = LISTVIEW_GetCountPerColumn(infoPtr);
10115 if (infoPtr->nFocusedItem == topidx + cnt - 1)
10116 nItem = infoPtr->nFocusedItem + cnt - 1;
10117 else
10118 nItem = topidx + cnt - 1;
10120 else
10121 nItem = infoPtr->nFocusedItem + LISTVIEW_GetCountPerColumn(infoPtr)
10122 * LISTVIEW_GetCountPerRow(infoPtr);
10123 if(nItem >= infoPtr->nItemCount) nItem = infoPtr->nItemCount - 1;
10124 break;
10127 if ((nItem != -1) && (nItem != infoPtr->nFocusedItem || nVirtualKey == VK_SPACE))
10128 LISTVIEW_KeySelection(infoPtr, nItem, nVirtualKey == VK_SPACE);
10130 return 0;
10133 /***
10134 * DESCRIPTION:
10135 * Kills the focus.
10137 * PARAMETER(S):
10138 * [I] infoPtr : valid pointer to the listview structure
10140 * RETURN:
10141 * Zero
10143 static LRESULT LISTVIEW_KillFocus(LISTVIEW_INFO *infoPtr)
10145 TRACE("()\n");
10147 /* drop any left over scroll amount */
10148 infoPtr->cWheelRemainder = 0;
10150 /* if we did not have the focus, there's nothing more to do */
10151 if (!infoPtr->bFocus) return 0;
10153 /* send NM_KILLFOCUS notification */
10154 if (!notify(infoPtr, NM_KILLFOCUS)) return 0;
10156 /* if we have a focus rectangle, get rid of it */
10157 LISTVIEW_ShowFocusRect(infoPtr, FALSE);
10159 /* if have a marquee selection, stop it */
10160 if (infoPtr->bMarqueeSelect)
10162 /* Remove the marquee rectangle and release our mouse capture */
10163 LISTVIEW_InvalidateRect(infoPtr, &infoPtr->marqueeRect);
10164 ReleaseCapture();
10166 SetRectEmpty(&infoPtr->marqueeRect);
10168 infoPtr->bMarqueeSelect = FALSE;
10169 infoPtr->bScrolling = FALSE;
10170 KillTimer(infoPtr->hwndSelf, (UINT_PTR) infoPtr);
10173 /* set window focus flag */
10174 infoPtr->bFocus = FALSE;
10176 /* invalidate the selected items before resetting focus flag */
10177 LISTVIEW_InvalidateSelectedItems(infoPtr);
10179 return 0;
10182 /***
10183 * DESCRIPTION:
10184 * Processes double click messages (left mouse button).
10186 * PARAMETER(S):
10187 * [I] infoPtr : valid pointer to the listview structure
10188 * [I] wKey : key flag
10189 * [I] x,y : mouse coordinate
10191 * RETURN:
10192 * Zero
10194 static LRESULT LISTVIEW_LButtonDblClk(LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
10196 LVHITTESTINFO htInfo;
10198 TRACE("(key=%hu, X=%u, Y=%u)\n", wKey, x, y);
10200 /* Cancel the item edition if any */
10201 if (infoPtr->itemEdit.fEnabled)
10203 KillTimer(infoPtr->hwndSelf, (UINT_PTR)&infoPtr->itemEdit);
10204 infoPtr->itemEdit.fEnabled = FALSE;
10207 /* send NM_RELEASEDCAPTURE notification */
10208 if (!notify(infoPtr, NM_RELEASEDCAPTURE)) return 0;
10210 htInfo.pt.x = x;
10211 htInfo.pt.y = y;
10213 /* send NM_DBLCLK notification */
10214 LISTVIEW_HitTest(infoPtr, &htInfo, TRUE, FALSE);
10215 if (!notify_click(infoPtr, NM_DBLCLK, &htInfo)) return 0;
10217 /* To send the LVN_ITEMACTIVATE, it must be on an Item */
10218 if(htInfo.iItem != -1) notify_itemactivate(infoPtr,&htInfo);
10220 return 0;
10223 static LRESULT LISTVIEW_TrackMouse(const LISTVIEW_INFO *infoPtr, POINT pt)
10225 MSG msg;
10226 RECT r;
10228 r.top = r.bottom = pt.y;
10229 r.left = r.right = pt.x;
10231 InflateRect(&r, GetSystemMetrics(SM_CXDRAG), GetSystemMetrics(SM_CYDRAG));
10233 SetCapture(infoPtr->hwndSelf);
10235 while (1)
10237 if (PeekMessageW(&msg, 0, 0, 0, PM_REMOVE | PM_NOYIELD))
10239 if (msg.message == WM_MOUSEMOVE)
10241 pt.x = (short)LOWORD(msg.lParam);
10242 pt.y = (short)HIWORD(msg.lParam);
10243 if (PtInRect(&r, pt))
10244 continue;
10245 else
10247 ReleaseCapture();
10248 return 1;
10251 else if (msg.message >= WM_LBUTTONDOWN &&
10252 msg.message <= WM_RBUTTONDBLCLK)
10254 break;
10257 DispatchMessageW(&msg);
10260 if (GetCapture() != infoPtr->hwndSelf)
10261 return 0;
10264 ReleaseCapture();
10265 return 0;
10269 /***
10270 * DESCRIPTION:
10271 * Processes mouse down messages (left mouse button).
10273 * PARAMETERS:
10274 * infoPtr [I ] valid pointer to the listview structure
10275 * wKey [I ] key flag
10276 * x,y [I ] mouse coordinate
10278 * RETURN:
10279 * Zero
10281 static LRESULT LISTVIEW_LButtonDown(LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
10283 LVHITTESTINFO lvHitTestInfo;
10284 static BOOL bGroupSelect = TRUE;
10285 POINT pt = { x, y };
10286 INT nItem;
10288 TRACE("(key=%hu, X=%u, Y=%u)\n", wKey, x, y);
10290 /* send NM_RELEASEDCAPTURE notification */
10291 if (!notify(infoPtr, NM_RELEASEDCAPTURE)) return 0;
10293 /* set left button down flag and record the click position */
10294 infoPtr->bLButtonDown = TRUE;
10295 infoPtr->ptClickPos = pt;
10296 infoPtr->bDragging = FALSE;
10297 infoPtr->bMarqueeSelect = FALSE;
10298 infoPtr->bScrolling = FALSE;
10300 lvHitTestInfo.pt.x = x;
10301 lvHitTestInfo.pt.y = y;
10303 nItem = LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, TRUE);
10304 TRACE("at %s, nItem=%d\n", wine_dbgstr_point(&pt), nItem);
10305 if ((nItem >= 0) && (nItem < infoPtr->nItemCount))
10307 if ((infoPtr->dwLvExStyle & LVS_EX_CHECKBOXES) && (lvHitTestInfo.flags & LVHT_ONITEMSTATEICON))
10309 notify_click(infoPtr, NM_CLICK, &lvHitTestInfo);
10310 toggle_checkbox_state(infoPtr, nItem);
10311 infoPtr->bLButtonDown = FALSE;
10312 return 0;
10315 if (infoPtr->dwStyle & LVS_SINGLESEL)
10317 if (LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED))
10318 infoPtr->nEditLabelItem = nItem;
10319 else
10320 LISTVIEW_SetSelection(infoPtr, nItem);
10322 else
10324 if ((wKey & MK_CONTROL) && (wKey & MK_SHIFT))
10326 if (bGroupSelect)
10328 if (!LISTVIEW_AddGroupSelection(infoPtr, nItem)) return 0;
10329 LISTVIEW_SetItemFocus(infoPtr, nItem);
10330 infoPtr->nSelectionMark = nItem;
10332 else
10334 LVITEMW item;
10336 item.state = LVIS_SELECTED | LVIS_FOCUSED;
10337 item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
10339 LISTVIEW_SetItemState(infoPtr,nItem,&item);
10340 infoPtr->nSelectionMark = nItem;
10343 else if (wKey & MK_CONTROL)
10345 LVITEMW item;
10347 bGroupSelect = (LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED) == 0);
10349 item.state = (bGroupSelect ? LVIS_SELECTED : 0) | LVIS_FOCUSED;
10350 item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
10351 LISTVIEW_SetItemState(infoPtr, nItem, &item);
10352 infoPtr->nSelectionMark = nItem;
10354 else if (wKey & MK_SHIFT)
10356 LISTVIEW_SetGroupSelection(infoPtr, nItem);
10358 else
10360 if (LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED))
10362 infoPtr->nEditLabelItem = nItem;
10363 infoPtr->nLButtonDownItem = nItem;
10365 LISTVIEW_SetItemFocus(infoPtr, nItem);
10367 else
10368 /* set selection (clears other pre-existing selections) */
10369 LISTVIEW_SetSelection(infoPtr, nItem);
10373 if (!infoPtr->bFocus)
10374 SetFocus(infoPtr->hwndSelf);
10376 if (infoPtr->dwLvExStyle & LVS_EX_ONECLICKACTIVATE)
10377 if(lvHitTestInfo.iItem != -1) notify_itemactivate(infoPtr,&lvHitTestInfo);
10379 else
10381 if (!infoPtr->bFocus)
10382 SetFocus(infoPtr->hwndSelf);
10384 /* remove all selections */
10385 if (!(wKey & MK_CONTROL) && !(wKey & MK_SHIFT))
10386 LISTVIEW_DeselectAll(infoPtr);
10387 ReleaseCapture();
10390 return 0;
10393 /***
10394 * DESCRIPTION:
10395 * Processes mouse up messages (left mouse button).
10397 * PARAMETERS:
10398 * infoPtr [I ] valid pointer to the listview structure
10399 * wKey [I ] key flag
10400 * x,y [I ] mouse coordinate
10402 * RETURN:
10403 * Zero
10405 static LRESULT LISTVIEW_LButtonUp(LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
10407 LVHITTESTINFO lvHitTestInfo;
10409 TRACE("(key=%hu, X=%u, Y=%u)\n", wKey, x, y);
10411 if (!infoPtr->bLButtonDown) return 0;
10413 lvHitTestInfo.pt.x = x;
10414 lvHitTestInfo.pt.y = y;
10416 /* send NM_CLICK notification */
10417 LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, FALSE);
10418 if (!notify_click(infoPtr, NM_CLICK, &lvHitTestInfo)) return 0;
10420 /* set left button flag */
10421 infoPtr->bLButtonDown = FALSE;
10423 /* set a single selection, reset others */
10424 if(lvHitTestInfo.iItem == infoPtr->nLButtonDownItem && lvHitTestInfo.iItem != -1)
10425 LISTVIEW_SetSelection(infoPtr, infoPtr->nLButtonDownItem);
10426 infoPtr->nLButtonDownItem = -1;
10428 if (infoPtr->bDragging || infoPtr->bMarqueeSelect)
10430 /* Remove the marquee rectangle and release our mouse capture */
10431 if (infoPtr->bMarqueeSelect)
10433 LISTVIEW_InvalidateRect(infoPtr, &infoPtr->marqueeDrawRect);
10434 ReleaseCapture();
10437 SetRectEmpty(&infoPtr->marqueeRect);
10438 SetRectEmpty(&infoPtr->marqueeDrawRect);
10440 infoPtr->bDragging = FALSE;
10441 infoPtr->bMarqueeSelect = FALSE;
10442 infoPtr->bScrolling = FALSE;
10444 KillTimer(infoPtr->hwndSelf, (UINT_PTR) infoPtr);
10445 return 0;
10448 /* if we clicked on a selected item, edit the label */
10449 if(lvHitTestInfo.iItem == infoPtr->nEditLabelItem && (lvHitTestInfo.flags & LVHT_ONITEMLABEL))
10451 /* we want to make sure the user doesn't want to do a double click. So we will
10452 * delay the edit. WM_LBUTTONDBLCLICK will cancel the timer
10454 infoPtr->itemEdit.fEnabled = TRUE;
10455 infoPtr->itemEdit.iItem = lvHitTestInfo.iItem;
10456 SetTimer(infoPtr->hwndSelf,
10457 (UINT_PTR)&infoPtr->itemEdit,
10458 GetDoubleClickTime(),
10459 LISTVIEW_DelayedEditItem);
10462 return 0;
10465 /***
10466 * DESCRIPTION:
10467 * Destroys the listview control (called after WM_DESTROY).
10469 * PARAMETER(S):
10470 * [I] infoPtr : valid pointer to the listview structure
10472 * RETURN:
10473 * Zero
10475 static LRESULT LISTVIEW_NCDestroy(LISTVIEW_INFO *infoPtr)
10477 INT i;
10479 TRACE("()\n");
10481 /* destroy data structure */
10482 DPA_Destroy(infoPtr->hdpaItems);
10483 DPA_Destroy(infoPtr->hdpaItemIds);
10484 DPA_Destroy(infoPtr->hdpaPosX);
10485 DPA_Destroy(infoPtr->hdpaPosY);
10486 /* columns */
10487 for (i = 0; i < DPA_GetPtrCount(infoPtr->hdpaColumns); i++)
10488 Free(DPA_GetPtr(infoPtr->hdpaColumns, i));
10489 DPA_Destroy(infoPtr->hdpaColumns);
10490 ranges_destroy(infoPtr->selectionRanges);
10492 /* destroy image lists */
10493 if (!(infoPtr->dwStyle & LVS_SHAREIMAGELISTS))
10495 ImageList_Destroy(infoPtr->himlNormal);
10496 ImageList_Destroy(infoPtr->himlSmall);
10497 ImageList_Destroy(infoPtr->himlState);
10500 /* destroy font, bkgnd brush */
10501 infoPtr->hFont = 0;
10502 if (infoPtr->hDefaultFont) DeleteObject(infoPtr->hDefaultFont);
10503 if (infoPtr->clrBk != CLR_NONE) DeleteObject(infoPtr->hBkBrush);
10504 if (infoPtr->hBkBitmap) DeleteObject(infoPtr->hBkBitmap);
10506 SetWindowLongPtrW(infoPtr->hwndSelf, 0, 0);
10508 /* free listview info pointer*/
10509 Free(infoPtr);
10511 return 0;
10514 /***
10515 * DESCRIPTION:
10516 * Handles notifications.
10518 * PARAMETER(S):
10519 * [I] infoPtr : valid pointer to the listview structure
10520 * [I] lpnmhdr : notification information
10522 * RETURN:
10523 * Zero
10525 static LRESULT LISTVIEW_Notify(LISTVIEW_INFO *infoPtr, NMHDR *lpnmhdr)
10527 NMHEADERW *lpnmh;
10529 TRACE("(lpnmhdr=%p)\n", lpnmhdr);
10531 if (!lpnmhdr || lpnmhdr->hwndFrom != infoPtr->hwndHeader) return 0;
10533 /* remember: HDN_LAST < HDN_FIRST */
10534 if (lpnmhdr->code > HDN_FIRST || lpnmhdr->code < HDN_LAST) return 0;
10535 lpnmh = (NMHEADERW *)lpnmhdr;
10537 if (lpnmh->iItem < 0 || lpnmh->iItem >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return 0;
10539 switch (lpnmhdr->code)
10541 case HDN_TRACKW:
10542 case HDN_TRACKA:
10544 COLUMN_INFO *lpColumnInfo;
10545 POINT ptOrigin;
10546 INT x;
10548 if (!lpnmh->pitem || !(lpnmh->pitem->mask & HDI_WIDTH))
10549 break;
10551 /* remove the old line (if any) */
10552 LISTVIEW_DrawTrackLine(infoPtr);
10554 /* compute & draw the new line */
10555 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, lpnmh->iItem);
10556 x = lpColumnInfo->rcHeader.left + lpnmh->pitem->cxy;
10557 LISTVIEW_GetOrigin(infoPtr, &ptOrigin);
10558 infoPtr->xTrackLine = x + ptOrigin.x;
10559 LISTVIEW_DrawTrackLine(infoPtr);
10560 return notify_forward_header(infoPtr, lpnmh);
10563 case HDN_ENDTRACKA:
10564 case HDN_ENDTRACKW:
10565 /* remove the track line (if any) */
10566 LISTVIEW_DrawTrackLine(infoPtr);
10567 infoPtr->xTrackLine = -1;
10568 return notify_forward_header(infoPtr, lpnmh);
10570 case HDN_BEGINDRAG:
10571 if ((infoPtr->dwLvExStyle & LVS_EX_HEADERDRAGDROP) == 0) return 1;
10572 return notify_forward_header(infoPtr, lpnmh);
10574 case HDN_ENDDRAG:
10575 infoPtr->colRectsDirty = TRUE;
10576 LISTVIEW_InvalidateList(infoPtr);
10577 return notify_forward_header(infoPtr, lpnmh);
10579 case HDN_ITEMCHANGEDW:
10580 case HDN_ITEMCHANGEDA:
10582 COLUMN_INFO *lpColumnInfo;
10583 HDITEMW hdi;
10584 INT dx, cxy;
10586 if (!lpnmh->pitem || !(lpnmh->pitem->mask & HDI_WIDTH))
10588 hdi.mask = HDI_WIDTH;
10589 if (!SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, lpnmh->iItem, (LPARAM)&hdi)) return 0;
10590 cxy = hdi.cxy;
10592 else
10593 cxy = lpnmh->pitem->cxy;
10595 /* determine how much we change since the last know position */
10596 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, lpnmh->iItem);
10597 dx = cxy - (lpColumnInfo->rcHeader.right - lpColumnInfo->rcHeader.left);
10598 if (dx != 0)
10600 lpColumnInfo->rcHeader.right += dx;
10602 hdi.mask = HDI_ORDER;
10603 SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, lpnmh->iItem, (LPARAM)&hdi);
10605 /* not the rightmost one */
10606 if (hdi.iOrder + 1 < DPA_GetPtrCount(infoPtr->hdpaColumns))
10608 INT nIndex = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX,
10609 hdi.iOrder + 1, 0);
10610 LISTVIEW_ScrollColumns(infoPtr, nIndex, dx);
10612 else
10614 /* only needs to update the scrolls */
10615 infoPtr->nItemWidth += dx;
10616 LISTVIEW_UpdateScroll(infoPtr);
10618 LISTVIEW_UpdateItemSize(infoPtr);
10619 if (infoPtr->uView == LV_VIEW_DETAILS && is_redrawing(infoPtr))
10621 POINT ptOrigin;
10622 RECT rcCol = lpColumnInfo->rcHeader;
10624 LISTVIEW_GetOrigin(infoPtr, &ptOrigin);
10625 OffsetRect(&rcCol, ptOrigin.x, 0);
10627 rcCol.top = infoPtr->rcList.top;
10628 rcCol.bottom = infoPtr->rcList.bottom;
10630 /* resizing left-aligned columns leaves most of the left side untouched */
10631 if ((lpColumnInfo->fmt & LVCFMT_JUSTIFYMASK) == LVCFMT_LEFT)
10633 INT nMaxDirty = infoPtr->nEllipsisWidth + infoPtr->ntmMaxCharWidth;
10634 if (dx > 0)
10635 nMaxDirty += dx;
10636 rcCol.left = max (rcCol.left, rcCol.right - nMaxDirty);
10639 /* when shrinking the last column clear the now unused field */
10640 if (hdi.iOrder == DPA_GetPtrCount(infoPtr->hdpaColumns) - 1)
10642 RECT right;
10644 rcCol.right -= dx;
10646 /* deal with right from rightmost column area */
10647 right.left = rcCol.right;
10648 right.top = rcCol.top;
10649 right.bottom = rcCol.bottom;
10650 right.right = infoPtr->rcList.right;
10652 LISTVIEW_InvalidateRect(infoPtr, &right);
10655 LISTVIEW_InvalidateRect(infoPtr, &rcCol);
10658 break;
10661 case HDN_ITEMCLICKW:
10662 case HDN_ITEMCLICKA:
10664 /* Handle sorting by Header Column */
10665 NMLISTVIEW nmlv;
10667 ZeroMemory(&nmlv, sizeof(NMLISTVIEW));
10668 nmlv.iItem = -1;
10669 nmlv.iSubItem = lpnmh->iItem;
10670 notify_listview(infoPtr, LVN_COLUMNCLICK, &nmlv);
10671 return notify_forward_header(infoPtr, lpnmh);
10674 case HDN_DIVIDERDBLCLICKW:
10675 case HDN_DIVIDERDBLCLICKA:
10676 /* FIXME: for LVS_EX_HEADERINALLVIEWS and not LV_VIEW_DETAILS
10677 we should use LVSCW_AUTOSIZE_USEHEADER, helper rework or
10678 split needed for that */
10679 LISTVIEW_SetColumnWidth(infoPtr, lpnmh->iItem, LVSCW_AUTOSIZE);
10680 return notify_forward_header(infoPtr, lpnmh);
10682 return 0;
10685 /***
10686 * DESCRIPTION:
10687 * Paint non-client area of control.
10689 * PARAMETER(S):
10690 * [I] infoPtr : valid pointer to the listview structureof the sender
10691 * [I] region : update region
10693 * RETURN:
10694 * 0 - frame was painted
10696 static LRESULT LISTVIEW_NCPaint(const LISTVIEW_INFO *infoPtr, HRGN region)
10698 LONG exstyle = GetWindowLongW (infoPtr->hwndSelf, GWL_EXSTYLE);
10699 HTHEME theme = GetWindowTheme (infoPtr->hwndSelf);
10700 RECT r, window_rect;
10701 HDC dc;
10702 HRGN cliprgn;
10703 int cxEdge = GetSystemMetrics (SM_CXEDGE),
10704 cyEdge = GetSystemMetrics (SM_CYEDGE);
10706 if (!theme || !(exstyle & WS_EX_CLIENTEDGE))
10707 return DefWindowProcW (infoPtr->hwndSelf, WM_NCPAINT, (WPARAM)region, 0);
10709 GetWindowRect(infoPtr->hwndSelf, &r);
10711 cliprgn = CreateRectRgn (r.left + cxEdge, r.top + cyEdge,
10712 r.right - cxEdge, r.bottom - cyEdge);
10713 if (region != (HRGN)1)
10714 CombineRgn (cliprgn, cliprgn, region, RGN_AND);
10716 OffsetRect(&r, -r.left, -r.top);
10717 if (infoPtr->hwndHeader && LISTVIEW_IsHeaderEnabled(infoPtr))
10719 GetWindowRect(infoPtr->hwndHeader, &window_rect);
10720 r.top = min(r.bottom, r.top + window_rect.bottom - window_rect.top);
10723 dc = GetDCEx(infoPtr->hwndSelf, region, DCX_WINDOW|DCX_INTERSECTRGN);
10725 if (IsThemeBackgroundPartiallyTransparent (theme, 0, 0))
10726 DrawThemeParentBackground(infoPtr->hwndSelf, dc, &r);
10727 DrawThemeBackground (theme, dc, 0, 0, &r, 0);
10728 ReleaseDC(infoPtr->hwndSelf, dc);
10730 /* Call default proc to get the scrollbars etc. painted */
10731 DefWindowProcW (infoPtr->hwndSelf, WM_NCPAINT, (WPARAM)cliprgn, 0);
10732 DeleteObject(cliprgn);
10734 return 0;
10737 /***
10738 * DESCRIPTION:
10739 * Determines the type of structure to use.
10741 * PARAMETER(S):
10742 * [I] infoPtr : valid pointer to the listview structureof the sender
10743 * [I] hwndFrom : listview window handle
10744 * [I] nCommand : command specifying the nature of the WM_NOTIFYFORMAT
10746 * RETURN:
10747 * Zero
10749 static LRESULT LISTVIEW_NotifyFormat(LISTVIEW_INFO *infoPtr, HWND hwndFrom, INT nCommand)
10751 TRACE("(hwndFrom=%p, nCommand=%d)\n", hwndFrom, nCommand);
10753 if (nCommand == NF_REQUERY)
10754 infoPtr->notifyFormat = SendMessageW(infoPtr->hwndNotify, WM_NOTIFYFORMAT, (WPARAM)infoPtr->hwndSelf, NF_QUERY);
10756 return infoPtr->notifyFormat;
10759 /***
10760 * DESCRIPTION:
10761 * Paints/Repaints the listview control. Internal use.
10763 * PARAMETER(S):
10764 * [I] infoPtr : valid pointer to the listview structure
10765 * [I] hdc : device context handle
10767 * RETURN:
10768 * Zero
10770 static LRESULT LISTVIEW_Paint(LISTVIEW_INFO *infoPtr, HDC hdc)
10772 TRACE("(hdc=%p)\n", hdc);
10774 if (infoPtr->bNoItemMetrics && infoPtr->nItemCount)
10776 infoPtr->bNoItemMetrics = FALSE;
10777 LISTVIEW_UpdateItemSize(infoPtr);
10778 if (infoPtr->uView == LV_VIEW_ICON || infoPtr->uView == LV_VIEW_SMALLICON)
10779 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
10780 LISTVIEW_UpdateScroll(infoPtr);
10783 if (infoPtr->hwndHeader) UpdateWindow(infoPtr->hwndHeader);
10785 if (hdc)
10786 LISTVIEW_Refresh(infoPtr, hdc, NULL);
10787 else
10789 PAINTSTRUCT ps;
10791 hdc = BeginPaint(infoPtr->hwndSelf, &ps);
10792 if (!hdc) return 1;
10793 LISTVIEW_Refresh(infoPtr, hdc, ps.fErase ? &ps.rcPaint : NULL);
10794 EndPaint(infoPtr->hwndSelf, &ps);
10797 return 0;
10800 /***
10801 * DESCRIPTION:
10802 * Paints/Repaints the listview control, WM_PAINT handler.
10804 * PARAMETER(S):
10805 * [I] infoPtr : valid pointer to the listview structure
10806 * [I] hdc : device context handle
10808 * RETURN:
10809 * Zero
10811 static inline LRESULT LISTVIEW_WMPaint(LISTVIEW_INFO *infoPtr, HDC hdc)
10813 TRACE("(hdc=%p)\n", hdc);
10815 if (!is_redrawing(infoPtr))
10816 return DefWindowProcW (infoPtr->hwndSelf, WM_PAINT, (WPARAM)hdc, 0);
10818 return LISTVIEW_Paint(infoPtr, hdc);
10821 /***
10822 * DESCRIPTION:
10823 * Paints/Repaints the listview control.
10825 * PARAMETER(S):
10826 * [I] infoPtr : valid pointer to the listview structure
10827 * [I] hdc : device context handle
10828 * [I] options : drawing options
10830 * RETURN:
10831 * Zero
10833 static LRESULT LISTVIEW_PrintClient(LISTVIEW_INFO *infoPtr, HDC hdc, DWORD options)
10835 if ((options & PRF_CHECKVISIBLE) && !IsWindowVisible(infoPtr->hwndSelf))
10836 return 0;
10838 if (options & ~(PRF_ERASEBKGND|PRF_CLIENT))
10839 FIXME("(hdc=%p options %#lx) partial stub\n", hdc, options);
10841 if (options & PRF_ERASEBKGND)
10842 LISTVIEW_EraseBkgnd(infoPtr, hdc);
10844 if (options & PRF_CLIENT)
10845 LISTVIEW_Paint(infoPtr, hdc);
10847 return 0;
10851 /***
10852 * DESCRIPTION:
10853 * Processes double click messages (right mouse button).
10855 * PARAMETER(S):
10856 * [I] infoPtr : valid pointer to the listview structure
10857 * [I] wKey : key flag
10858 * [I] x,y : mouse coordinate
10860 * RETURN:
10861 * Zero
10863 static LRESULT LISTVIEW_RButtonDblClk(const LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
10865 LVHITTESTINFO lvHitTestInfo;
10867 TRACE("(key=%hu,X=%u,Y=%u)\n", wKey, x, y);
10869 /* send NM_RELEASEDCAPTURE notification */
10870 if (!notify(infoPtr, NM_RELEASEDCAPTURE)) return 0;
10872 /* send NM_RDBLCLK notification */
10873 lvHitTestInfo.pt.x = x;
10874 lvHitTestInfo.pt.y = y;
10875 LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, FALSE);
10876 notify_click(infoPtr, NM_RDBLCLK, &lvHitTestInfo);
10878 return 0;
10881 /***
10882 * DESCRIPTION:
10883 * Processes WM_RBUTTONDOWN message and corresponding drag operation.
10885 * PARAMETER(S):
10886 * [I] infoPtr : valid pointer to the listview structure
10887 * [I] wKey : key flag
10888 * [I] x, y : mouse coordinate
10890 * RETURN:
10891 * Zero
10893 static LRESULT LISTVIEW_RButtonDown(LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
10895 LVHITTESTINFO ht;
10896 INT item;
10898 TRACE("(key=%hu, x=%d, y=%d)\n", wKey, x, y);
10900 /* send NM_RELEASEDCAPTURE notification */
10901 if (!notify(infoPtr, NM_RELEASEDCAPTURE)) return 0;
10903 /* determine the index of the selected item */
10904 ht.pt.x = x;
10905 ht.pt.y = y;
10906 item = LISTVIEW_HitTest(infoPtr, &ht, TRUE, TRUE);
10908 /* make sure the listview control window has the focus */
10909 if (!infoPtr->bFocus) SetFocus(infoPtr->hwndSelf);
10911 if ((item >= 0) && (item < infoPtr->nItemCount))
10913 LISTVIEW_SetItemFocus(infoPtr, item);
10914 if (!((wKey & MK_SHIFT) || (wKey & MK_CONTROL)) &&
10915 !LISTVIEW_GetItemState(infoPtr, item, LVIS_SELECTED))
10916 LISTVIEW_SetSelection(infoPtr, item);
10918 else
10919 LISTVIEW_DeselectAll(infoPtr);
10921 if (LISTVIEW_TrackMouse(infoPtr, ht.pt))
10923 if (ht.iItem != -1)
10925 NMLISTVIEW nmlv;
10927 memset(&nmlv, 0, sizeof(nmlv));
10928 nmlv.iItem = ht.iItem;
10929 nmlv.ptAction = ht.pt;
10931 notify_listview(infoPtr, LVN_BEGINRDRAG, &nmlv);
10934 else
10936 SetFocus(infoPtr->hwndSelf);
10938 ht.pt.x = x;
10939 ht.pt.y = y;
10940 LISTVIEW_HitTest(infoPtr, &ht, TRUE, FALSE);
10942 if (notify_click(infoPtr, NM_RCLICK, &ht))
10944 /* Send a WM_CONTEXTMENU message in response to the WM_RBUTTONUP */
10945 SendMessageW(infoPtr->hwndSelf, WM_CONTEXTMENU,
10946 (WPARAM)infoPtr->hwndSelf, (LPARAM)GetMessagePos());
10950 return 0;
10953 /***
10954 * DESCRIPTION:
10955 * Sets the cursor.
10957 * PARAMETER(S):
10958 * [I] infoPtr : valid pointer to the listview structure
10959 * [I] hwnd : window handle of window containing the cursor
10960 * [I] nHittest : hit-test code
10961 * [I] wMouseMsg : ideintifier of the mouse message
10963 * RETURN:
10964 * TRUE if cursor is set
10965 * FALSE otherwise
10967 static BOOL LISTVIEW_SetCursor(const LISTVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
10969 LVHITTESTINFO lvHitTestInfo;
10971 if (!LISTVIEW_IsHotTracking(infoPtr)) goto forward;
10973 if (!infoPtr->hHotCursor) goto forward;
10975 GetCursorPos(&lvHitTestInfo.pt);
10976 if (LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, FALSE, FALSE) < 0) goto forward;
10978 SetCursor(infoPtr->hHotCursor);
10980 return TRUE;
10982 forward:
10984 return DefWindowProcW(infoPtr->hwndSelf, WM_SETCURSOR, wParam, lParam);
10987 /***
10988 * DESCRIPTION:
10989 * Sets the focus.
10991 * PARAMETER(S):
10992 * [I] infoPtr : valid pointer to the listview structure
10993 * [I] hwndLoseFocus : handle of previously focused window
10995 * RETURN:
10996 * Zero
10998 static LRESULT LISTVIEW_SetFocus(LISTVIEW_INFO *infoPtr, HWND hwndLoseFocus)
11000 TRACE("(hwndLoseFocus=%p)\n", hwndLoseFocus);
11002 /* if we have the focus already, there's nothing to do */
11003 if (infoPtr->bFocus) return 0;
11005 /* send NM_SETFOCUS notification */
11006 if (!notify(infoPtr, NM_SETFOCUS)) return 0;
11008 /* set window focus flag */
11009 infoPtr->bFocus = TRUE;
11011 /* put the focus rect back on */
11012 LISTVIEW_ShowFocusRect(infoPtr, TRUE);
11014 /* redraw all visible selected items */
11015 LISTVIEW_InvalidateSelectedItems(infoPtr);
11017 return 0;
11020 /***
11021 * DESCRIPTION:
11022 * Sets the font.
11024 * PARAMETER(S):
11025 * [I] infoPtr : valid pointer to the listview structure
11026 * [I] fRedraw : font handle
11027 * [I] fRedraw : redraw flag
11029 * RETURN:
11030 * Zero
11032 static LRESULT LISTVIEW_SetFont(LISTVIEW_INFO *infoPtr, HFONT hFont, WORD fRedraw)
11034 HFONT oldFont = infoPtr->hFont;
11035 INT oldHeight = infoPtr->nItemHeight;
11037 TRACE("(hfont=%p,redraw=%hu)\n", hFont, fRedraw);
11039 infoPtr->hFont = hFont ? hFont : infoPtr->hDefaultFont;
11040 if (infoPtr->hFont == oldFont) return 0;
11042 LISTVIEW_SaveTextMetrics(infoPtr);
11044 infoPtr->nItemHeight = LISTVIEW_CalculateItemHeight(infoPtr);
11046 if (infoPtr->uView == LV_VIEW_DETAILS)
11048 SendMessageW(infoPtr->hwndHeader, WM_SETFONT, (WPARAM)hFont, MAKELPARAM(fRedraw, 0));
11049 LISTVIEW_UpdateSize(infoPtr);
11050 LISTVIEW_UpdateScroll(infoPtr);
11052 else if (infoPtr->nItemHeight != oldHeight)
11053 LISTVIEW_UpdateScroll(infoPtr);
11055 if (fRedraw) LISTVIEW_InvalidateList(infoPtr);
11057 return 0;
11060 /***
11061 * DESCRIPTION:
11062 * Message handling for WM_SETREDRAW.
11063 * For the Listview, it invalidates the entire window (the doc specifies otherwise)
11065 * PARAMETER(S):
11066 * [I] infoPtr : valid pointer to the listview structure
11067 * [I] redraw: state of redraw flag
11069 * RETURN:
11070 * Zero.
11072 static LRESULT LISTVIEW_SetRedraw(LISTVIEW_INFO *infoPtr, BOOL redraw)
11074 TRACE("old=%d, new=%d\n", infoPtr->redraw, redraw);
11076 if (infoPtr->redraw == !!redraw)
11077 return 0;
11079 if (!(infoPtr->redraw = !!redraw))
11080 return 0;
11082 if (is_autoarrange(infoPtr))
11083 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
11084 LISTVIEW_UpdateScroll(infoPtr);
11086 /* despite what the WM_SETREDRAW docs says, apps expect us
11087 * to invalidate the listview here... stupid! */
11088 LISTVIEW_InvalidateList(infoPtr);
11090 return 0;
11093 /***
11094 * DESCRIPTION:
11095 * Resizes the listview control. This function processes WM_SIZE
11096 * messages. At this time, the width and height are not used.
11098 * PARAMETER(S):
11099 * [I] infoPtr : valid pointer to the listview structure
11100 * [I] Width : new width
11101 * [I] Height : new height
11103 * RETURN:
11104 * Zero
11106 static LRESULT LISTVIEW_Size(LISTVIEW_INFO *infoPtr, int Width, int Height)
11108 RECT rcOld = infoPtr->rcList;
11110 TRACE("(width=%d, height=%d)\n", Width, Height);
11112 LISTVIEW_UpdateSize(infoPtr);
11113 if (EqualRect(&rcOld, &infoPtr->rcList)) return 0;
11115 /* do not bother with display related stuff if we're not redrawing */
11116 if (!is_redrawing(infoPtr)) return 0;
11118 if (is_autoarrange(infoPtr))
11119 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
11121 LISTVIEW_UpdateScroll(infoPtr);
11123 /* refresh all only for lists whose height changed significantly */
11124 if ((infoPtr->uView == LV_VIEW_LIST) &&
11125 (rcOld.bottom - rcOld.top) / infoPtr->nItemHeight !=
11126 (infoPtr->rcList.bottom - infoPtr->rcList.top) / infoPtr->nItemHeight)
11127 LISTVIEW_InvalidateList(infoPtr);
11129 return 0;
11132 /***
11133 * DESCRIPTION:
11134 * Sets the size information.
11136 * PARAMETER(S):
11137 * [I] infoPtr : valid pointer to the listview structure
11139 * RETURN:
11140 * None
11142 static void LISTVIEW_UpdateSize(LISTVIEW_INFO *infoPtr)
11144 TRACE("uView %ld, rcList(old)=%s\n", infoPtr->uView, wine_dbgstr_rect(&infoPtr->rcList));
11146 GetClientRect(infoPtr->hwndSelf, &infoPtr->rcList);
11148 if (infoPtr->uView == LV_VIEW_LIST)
11150 /* Apparently the "LIST" style is supposed to have the same
11151 * number of items in a column even if there is no scroll bar.
11152 * Since if a scroll bar already exists then the bottom is already
11153 * reduced, only reduce if the scroll bar does not currently exist.
11154 * The "2" is there to mimic the native control. I think it may be
11155 * related to either padding or edges. (GLA 7/2002)
11157 if (!(GetWindowLongW(infoPtr->hwndSelf, GWL_STYLE) & WS_HSCROLL))
11158 infoPtr->rcList.bottom -= GetSystemMetrics(SM_CYHSCROLL);
11159 infoPtr->rcList.bottom = max (infoPtr->rcList.bottom - 2, 0);
11162 /* When ListView control is created invisible, header isn't created right away. */
11163 if (infoPtr->hwndHeader)
11165 POINT origin;
11166 WINDOWPOS wp;
11167 HDLAYOUT hl;
11168 RECT rect;
11170 LISTVIEW_GetOrigin(infoPtr, &origin);
11172 rect = infoPtr->rcList;
11173 rect.left += origin.x;
11175 hl.prc = &rect;
11176 hl.pwpos = &wp;
11177 SendMessageW( infoPtr->hwndHeader, HDM_LAYOUT, 0, (LPARAM)&hl );
11178 TRACE(" wp.flags=0x%08x, wp=%d,%d (%dx%d)\n", wp.flags, wp.x, wp.y, wp.cx, wp.cy);
11180 if (LISTVIEW_IsHeaderEnabled(infoPtr))
11181 wp.flags |= SWP_SHOWWINDOW;
11182 else
11184 wp.flags |= SWP_HIDEWINDOW;
11185 wp.cy = 0;
11188 SetWindowPos(wp.hwnd, wp.hwndInsertAfter, wp.x, wp.y, wp.cx, wp.cy, wp.flags);
11189 TRACE(" after SWP wp=%d,%d (%dx%d)\n", wp.x, wp.y, wp.cx, wp.cy);
11191 infoPtr->rcList.top = max(wp.cy, 0);
11193 /* extra padding for grid */
11194 if (infoPtr->uView == LV_VIEW_DETAILS && infoPtr->dwLvExStyle & LVS_EX_GRIDLINES)
11195 infoPtr->rcList.top += 2;
11197 TRACE(" rcList=%s\n", wine_dbgstr_rect(&infoPtr->rcList));
11200 /***
11201 * DESCRIPTION:
11202 * Processes WM_STYLECHANGED messages.
11204 * PARAMETER(S):
11205 * [I] infoPtr : valid pointer to the listview structure
11206 * [I] wStyleType : window style type (normal or extended)
11207 * [I] lpss : window style information
11209 * RETURN:
11210 * Zero
11212 static INT LISTVIEW_StyleChanged(LISTVIEW_INFO *infoPtr, WPARAM wStyleType,
11213 const STYLESTRUCT *lpss)
11215 UINT uNewView, uOldView;
11216 UINT style;
11218 TRACE("styletype %Ix, styleOld %#lx, styleNew %#lx\n",
11219 wStyleType, lpss->styleOld, lpss->styleNew);
11221 if (wStyleType != GWL_STYLE || lpss->styleNew == infoPtr->dwStyle) return 0;
11223 infoPtr->dwStyle = lpss->styleNew;
11225 if (((lpss->styleOld & WS_HSCROLL) != 0)&&
11226 ((lpss->styleNew & WS_HSCROLL) == 0))
11227 ShowScrollBar(infoPtr->hwndSelf, SB_HORZ, FALSE);
11229 if (((lpss->styleOld & WS_VSCROLL) != 0)&&
11230 ((lpss->styleNew & WS_VSCROLL) == 0))
11231 ShowScrollBar(infoPtr->hwndSelf, SB_VERT, FALSE);
11233 uNewView = lpss->styleNew & LVS_TYPEMASK;
11234 uOldView = lpss->styleOld & LVS_TYPEMASK;
11236 if (uNewView != uOldView)
11238 HIMAGELIST himl;
11240 /* LVM_SETVIEW doesn't change window style bits within LVS_TYPEMASK,
11241 changing style updates current view only when view bits change. */
11242 map_style_view(infoPtr);
11243 SendMessageW(infoPtr->hwndEdit, WM_KILLFOCUS, 0, 0);
11244 ShowWindow(infoPtr->hwndHeader, SW_HIDE);
11246 ShowScrollBar(infoPtr->hwndSelf, SB_BOTH, FALSE);
11247 SetRectEmpty(&infoPtr->rcFocus);
11249 himl = (uNewView == LVS_ICON ? infoPtr->himlNormal : infoPtr->himlSmall);
11250 set_icon_size(&infoPtr->iconSize, himl, uNewView != LVS_ICON);
11252 if (uNewView == LVS_REPORT)
11254 HDLAYOUT hl;
11255 WINDOWPOS wp;
11257 LISTVIEW_CreateHeader( infoPtr );
11259 hl.prc = &infoPtr->rcList;
11260 hl.pwpos = &wp;
11261 SendMessageW( infoPtr->hwndHeader, HDM_LAYOUT, 0, (LPARAM)&hl );
11262 SetWindowPos(infoPtr->hwndHeader, infoPtr->hwndSelf, wp.x, wp.y, wp.cx, wp.cy,
11263 wp.flags | ((infoPtr->dwStyle & LVS_NOCOLUMNHEADER)
11264 ? SWP_HIDEWINDOW : SWP_SHOWWINDOW));
11267 LISTVIEW_UpdateItemSize(infoPtr);
11270 if (uNewView == LVS_REPORT || infoPtr->dwLvExStyle & LVS_EX_HEADERINALLVIEWS)
11272 if ((lpss->styleOld ^ lpss->styleNew) & LVS_NOCOLUMNHEADER)
11274 if (lpss->styleNew & LVS_NOCOLUMNHEADER)
11276 /* Turn off the header control */
11277 style = GetWindowLongW(infoPtr->hwndHeader, GWL_STYLE);
11278 TRACE("Hide header control, was 0x%08x\n", style);
11279 SetWindowLongW(infoPtr->hwndHeader, GWL_STYLE, style | HDS_HIDDEN);
11280 } else {
11281 /* Turn on the header control */
11282 if ((style = GetWindowLongW(infoPtr->hwndHeader, GWL_STYLE)) & HDS_HIDDEN)
11284 TRACE("Show header control, was 0x%08x\n", style);
11285 SetWindowLongW(infoPtr->hwndHeader, GWL_STYLE, (style & ~HDS_HIDDEN) | WS_VISIBLE);
11291 if ( (uNewView == LVS_ICON || uNewView == LVS_SMALLICON) &&
11292 (uNewView != uOldView || ((lpss->styleNew ^ lpss->styleOld) & LVS_ALIGNMASK)) )
11293 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
11295 /* update the size of the client area */
11296 LISTVIEW_UpdateSize(infoPtr);
11298 /* add scrollbars if needed */
11299 LISTVIEW_UpdateScroll(infoPtr);
11301 return 0;
11304 /***
11305 * DESCRIPTION:
11306 * Processes WM_STYLECHANGING messages.
11308 * PARAMETER(S):
11309 * [I] wStyleType : window style type (normal or extended)
11310 * [I0] lpss : window style information
11312 * RETURN:
11313 * Zero
11315 static INT LISTVIEW_StyleChanging(WPARAM wStyleType,
11316 STYLESTRUCT *lpss)
11318 TRACE("styletype %Ix, styleOld %#lx, styleNew %#lx\n",
11319 wStyleType, lpss->styleOld, lpss->styleNew);
11321 /* don't forward LVS_OWNERDATA only if not already set to */
11322 if ((lpss->styleNew ^ lpss->styleOld) & LVS_OWNERDATA)
11324 if (lpss->styleOld & LVS_OWNERDATA)
11325 lpss->styleNew |= LVS_OWNERDATA;
11326 else
11327 lpss->styleNew &= ~LVS_OWNERDATA;
11330 return 0;
11333 /***
11334 * DESCRIPTION:
11335 * Processes WM_SHOWWINDOW messages.
11337 * PARAMETER(S):
11338 * [I] infoPtr : valid pointer to the listview structure
11339 * [I] bShown : window is being shown (FALSE when hidden)
11340 * [I] iStatus : window show status
11342 * RETURN:
11343 * Zero
11345 static LRESULT LISTVIEW_ShowWindow(LISTVIEW_INFO *infoPtr, WPARAM bShown, LPARAM iStatus)
11347 /* header delayed creation */
11348 if ((infoPtr->uView == LV_VIEW_DETAILS) && bShown)
11350 LISTVIEW_CreateHeader(infoPtr);
11352 if (!(LVS_NOCOLUMNHEADER & infoPtr->dwStyle))
11353 ShowWindow(infoPtr->hwndHeader, SW_SHOWNORMAL);
11356 return DefWindowProcW(infoPtr->hwndSelf, WM_SHOWWINDOW, bShown, iStatus);
11359 /***
11360 * DESCRIPTION:
11361 * Processes CCM_GETVERSION messages.
11363 * PARAMETER(S):
11364 * [I] infoPtr : valid pointer to the listview structure
11366 * RETURN:
11367 * Current version
11369 static inline LRESULT LISTVIEW_GetVersion(const LISTVIEW_INFO *infoPtr)
11371 return infoPtr->iVersion;
11374 /***
11375 * DESCRIPTION:
11376 * Processes CCM_SETVERSION messages.
11378 * PARAMETER(S):
11379 * [I] infoPtr : valid pointer to the listview structure
11380 * [I] iVersion : version to be set
11382 * RETURN:
11383 * -1 when requested version is greater than DLL version;
11384 * previous version otherwise
11386 static LRESULT LISTVIEW_SetVersion(LISTVIEW_INFO *infoPtr, DWORD iVersion)
11388 INT iOldVersion = infoPtr->iVersion;
11390 if (iVersion > COMCTL32_VERSION)
11391 return -1;
11393 infoPtr->iVersion = iVersion;
11395 TRACE("new version %ld\n", iVersion);
11397 return iOldVersion;
11400 /***
11401 * DESCRIPTION:
11402 * Window procedure of the listview control.
11405 static LRESULT WINAPI
11406 LISTVIEW_WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
11408 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)GetWindowLongPtrW(hwnd, 0);
11410 TRACE("hwnd %p, uMsg %x, wParam %Ix, lParam %Ix\n", hwnd, uMsg, wParam, lParam);
11412 if (!infoPtr && (uMsg != WM_NCCREATE))
11413 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
11415 switch (uMsg)
11417 case LVM_APPROXIMATEVIEWRECT:
11418 return LISTVIEW_ApproximateViewRect(infoPtr, (INT)wParam,
11419 LOWORD(lParam), HIWORD(lParam));
11420 case LVM_ARRANGE:
11421 return LISTVIEW_Arrange(infoPtr, (INT)wParam);
11423 case LVM_CANCELEDITLABEL:
11424 return LISTVIEW_CancelEditLabel(infoPtr);
11426 case LVM_CREATEDRAGIMAGE:
11427 return (LRESULT)LISTVIEW_CreateDragImage(infoPtr, (INT)wParam, (LPPOINT)lParam);
11429 case LVM_DELETEALLITEMS:
11430 return LISTVIEW_DeleteAllItems(infoPtr, FALSE);
11432 case LVM_DELETECOLUMN:
11433 return LISTVIEW_DeleteColumn(infoPtr, (INT)wParam);
11435 case LVM_DELETEITEM:
11436 return LISTVIEW_DeleteItem(infoPtr, (INT)wParam);
11438 case LVM_EDITLABELA:
11439 case LVM_EDITLABELW:
11440 return (LRESULT)LISTVIEW_EditLabelT(infoPtr, (INT)wParam,
11441 uMsg == LVM_EDITLABELW);
11442 /* case LVM_ENABLEGROUPVIEW: */
11444 case LVM_ENSUREVISIBLE:
11445 return LISTVIEW_EnsureVisible(infoPtr, (INT)wParam, (BOOL)lParam);
11447 case LVM_FINDITEMW:
11448 return LISTVIEW_FindItemW(infoPtr, (INT)wParam, (LPLVFINDINFOW)lParam);
11450 case LVM_FINDITEMA:
11451 return LISTVIEW_FindItemA(infoPtr, (INT)wParam, (LPLVFINDINFOA)lParam);
11453 case LVM_GETBKCOLOR:
11454 return infoPtr->clrBk;
11456 /* case LVM_GETBKIMAGE: */
11458 case LVM_GETCALLBACKMASK:
11459 return infoPtr->uCallbackMask;
11461 case LVM_GETCOLUMNA:
11462 case LVM_GETCOLUMNW:
11463 return LISTVIEW_GetColumnT(infoPtr, (INT)wParam, (LPLVCOLUMNW)lParam,
11464 uMsg == LVM_GETCOLUMNW);
11466 case LVM_GETCOLUMNORDERARRAY:
11467 return LISTVIEW_GetColumnOrderArray(infoPtr, (INT)wParam, (LPINT)lParam);
11469 case LVM_GETCOLUMNWIDTH:
11470 return LISTVIEW_GetColumnWidth(infoPtr, (INT)wParam);
11472 case LVM_GETCOUNTPERPAGE:
11473 return LISTVIEW_GetCountPerPage(infoPtr);
11475 case LVM_GETEDITCONTROL:
11476 return (LRESULT)infoPtr->hwndEdit;
11478 case LVM_GETEXTENDEDLISTVIEWSTYLE:
11479 return infoPtr->dwLvExStyle;
11481 /* case LVM_GETGROUPINFO: */
11483 /* case LVM_GETGROUPMETRICS: */
11485 case LVM_GETHEADER:
11486 return (LRESULT)infoPtr->hwndHeader;
11488 case LVM_GETHOTCURSOR:
11489 return (LRESULT)infoPtr->hHotCursor;
11491 case LVM_GETHOTITEM:
11492 return infoPtr->nHotItem;
11494 case LVM_GETHOVERTIME:
11495 return infoPtr->dwHoverTime;
11497 case LVM_GETIMAGELIST:
11498 return (LRESULT)LISTVIEW_GetImageList(infoPtr, (INT)wParam);
11500 /* case LVM_GETINSERTMARK: */
11502 /* case LVM_GETINSERTMARKCOLOR: */
11504 /* case LVM_GETINSERTMARKRECT: */
11506 case LVM_GETISEARCHSTRINGA:
11507 case LVM_GETISEARCHSTRINGW:
11508 FIXME("LVM_GETISEARCHSTRING: unimplemented\n");
11509 return FALSE;
11511 case LVM_GETITEMA:
11512 case LVM_GETITEMW:
11513 return LISTVIEW_GetItemExtT(infoPtr, (LPLVITEMW)lParam, uMsg == LVM_GETITEMW);
11515 case LVM_GETITEMCOUNT:
11516 return infoPtr->nItemCount;
11518 case LVM_GETITEMPOSITION:
11519 return LISTVIEW_GetItemPosition(infoPtr, (INT)wParam, (LPPOINT)lParam);
11521 case LVM_GETITEMRECT:
11522 return LISTVIEW_GetItemRect(infoPtr, (INT)wParam, (LPRECT)lParam);
11524 case LVM_GETITEMSPACING:
11525 return LISTVIEW_GetItemSpacing(infoPtr, (BOOL)wParam);
11527 case LVM_GETITEMSTATE:
11528 return LISTVIEW_GetItemState(infoPtr, (INT)wParam, (UINT)lParam);
11530 case LVM_GETITEMTEXTA:
11531 case LVM_GETITEMTEXTW:
11532 return LISTVIEW_GetItemTextT(infoPtr, (INT)wParam, (LPLVITEMW)lParam,
11533 uMsg == LVM_GETITEMTEXTW);
11535 case LVM_GETNEXTITEM:
11536 return LISTVIEW_GetNextItem(infoPtr, (INT)wParam, LOWORD(lParam));
11538 case LVM_GETNEXTITEMINDEX:
11539 return LISTVIEW_GetNextItemIndex(infoPtr, (LVITEMINDEX *)wParam, lParam);
11541 case LVM_GETNUMBEROFWORKAREAS:
11542 FIXME("LVM_GETNUMBEROFWORKAREAS: unimplemented\n");
11543 return 1;
11545 case LVM_GETORIGIN:
11546 if (!lParam) return FALSE;
11547 if (infoPtr->uView == LV_VIEW_DETAILS ||
11548 infoPtr->uView == LV_VIEW_LIST) return FALSE;
11549 LISTVIEW_GetOrigin(infoPtr, (LPPOINT)lParam);
11550 return TRUE;
11552 /* case LVM_GETOUTLINECOLOR: */
11554 case LVM_GETSELECTEDCOLUMN:
11555 return infoPtr->selected_column;
11557 case LVM_GETSELECTEDCOUNT:
11558 return LISTVIEW_GetSelectedCount(infoPtr);
11560 case LVM_GETSELECTIONMARK:
11561 return infoPtr->nSelectionMark;
11563 case LVM_GETSTRINGWIDTHA:
11564 case LVM_GETSTRINGWIDTHW:
11565 return LISTVIEW_GetStringWidthT(infoPtr, (LPCWSTR)lParam,
11566 uMsg == LVM_GETSTRINGWIDTHW);
11568 case LVM_GETSUBITEMRECT:
11569 return LISTVIEW_GetSubItemRect(infoPtr, (UINT)wParam, (LPRECT)lParam);
11571 case LVM_GETTEXTBKCOLOR:
11572 return infoPtr->clrTextBk;
11574 case LVM_GETTEXTCOLOR:
11575 return infoPtr->clrText;
11577 /* case LVM_GETTILEINFO: */
11579 /* case LVM_GETTILEVIEWINFO: */
11581 case LVM_GETTOOLTIPS:
11582 if( !infoPtr->hwndToolTip )
11583 infoPtr->hwndToolTip = COMCTL32_CreateToolTip( hwnd );
11584 return (LRESULT)infoPtr->hwndToolTip;
11586 case LVM_GETTOPINDEX:
11587 return LISTVIEW_GetTopIndex(infoPtr);
11589 case LVM_GETUNICODEFORMAT:
11590 return (infoPtr->notifyFormat == NFR_UNICODE);
11592 case LVM_GETVIEW:
11593 return infoPtr->uView;
11595 case LVM_GETVIEWRECT:
11596 return LISTVIEW_GetViewRect(infoPtr, (LPRECT)lParam);
11598 case LVM_GETWORKAREAS:
11599 FIXME("LVM_GETWORKAREAS: unimplemented\n");
11600 return FALSE;
11602 /* case LVM_HASGROUP: */
11604 case LVM_HITTEST:
11605 return LISTVIEW_HitTest(infoPtr, (LPLVHITTESTINFO)lParam, FALSE, TRUE);
11607 case LVM_INSERTCOLUMNA:
11608 case LVM_INSERTCOLUMNW:
11609 return LISTVIEW_InsertColumnT(infoPtr, (INT)wParam, (LPLVCOLUMNW)lParam,
11610 uMsg == LVM_INSERTCOLUMNW);
11612 /* case LVM_INSERTGROUP: */
11614 /* case LVM_INSERTGROUPSORTED: */
11616 case LVM_INSERTITEMA:
11617 case LVM_INSERTITEMW:
11618 return LISTVIEW_InsertItemT(infoPtr, (LPLVITEMW)lParam, uMsg == LVM_INSERTITEMW);
11620 /* case LVM_INSERTMARKHITTEST: */
11622 /* case LVM_ISGROUPVIEWENABLED: */
11624 case LVM_ISITEMVISIBLE:
11625 return LISTVIEW_IsItemVisible(infoPtr, (INT)wParam);
11627 case LVM_MAPIDTOINDEX:
11628 return LISTVIEW_MapIdToIndex(infoPtr, (UINT)wParam);
11630 case LVM_MAPINDEXTOID:
11631 return LISTVIEW_MapIndexToId(infoPtr, (INT)wParam);
11633 /* case LVM_MOVEGROUP: */
11635 /* case LVM_MOVEITEMTOGROUP: */
11637 case LVM_REDRAWITEMS:
11638 return LISTVIEW_RedrawItems(infoPtr, (INT)wParam, (INT)lParam);
11640 /* case LVM_REMOVEALLGROUPS: */
11642 /* case LVM_REMOVEGROUP: */
11644 case LVM_SCROLL:
11645 return LISTVIEW_Scroll(infoPtr, (INT)wParam, (INT)lParam);
11647 case LVM_SETBKCOLOR:
11648 return LISTVIEW_SetBkColor(infoPtr, (COLORREF)lParam);
11650 case LVM_SETBKIMAGEA:
11651 case LVM_SETBKIMAGEW:
11652 return LISTVIEW_SetBkImage(infoPtr, (LVBKIMAGEW *)lParam, uMsg == LVM_SETBKIMAGEW);
11654 case LVM_SETCALLBACKMASK:
11655 infoPtr->uCallbackMask = (UINT)wParam;
11656 return TRUE;
11658 case LVM_SETCOLUMNA:
11659 case LVM_SETCOLUMNW:
11660 return LISTVIEW_SetColumnT(infoPtr, (INT)wParam, (LPLVCOLUMNW)lParam,
11661 uMsg == LVM_SETCOLUMNW);
11663 case LVM_SETCOLUMNORDERARRAY:
11664 return LISTVIEW_SetColumnOrderArray(infoPtr, (INT)wParam, (LPINT)lParam);
11666 case LVM_SETCOLUMNWIDTH:
11667 return LISTVIEW_SetColumnWidth(infoPtr, (INT)wParam, (short)LOWORD(lParam));
11669 case LVM_SETEXTENDEDLISTVIEWSTYLE:
11670 return LISTVIEW_SetExtendedListViewStyle(infoPtr, (DWORD)wParam, (DWORD)lParam);
11672 /* case LVM_SETGROUPINFO: */
11674 /* case LVM_SETGROUPMETRICS: */
11676 case LVM_SETHOTCURSOR:
11677 return (LRESULT)LISTVIEW_SetHotCursor(infoPtr, (HCURSOR)lParam);
11679 case LVM_SETHOTITEM:
11680 return LISTVIEW_SetHotItem(infoPtr, (INT)wParam);
11682 case LVM_SETHOVERTIME:
11683 return LISTVIEW_SetHoverTime(infoPtr, (DWORD)lParam);
11685 case LVM_SETICONSPACING:
11686 if(lParam == -1)
11687 return LISTVIEW_SetIconSpacing(infoPtr, -1, -1);
11688 return LISTVIEW_SetIconSpacing(infoPtr, LOWORD(lParam), HIWORD(lParam));
11690 case LVM_SETIMAGELIST:
11691 return (LRESULT)LISTVIEW_SetImageList(infoPtr, (INT)wParam, (HIMAGELIST)lParam);
11693 /* case LVM_SETINFOTIP: */
11695 /* case LVM_SETINSERTMARK: */
11697 /* case LVM_SETINSERTMARKCOLOR: */
11699 case LVM_SETITEMA:
11700 case LVM_SETITEMW:
11702 if (infoPtr->dwStyle & LVS_OWNERDATA) return FALSE;
11703 return LISTVIEW_SetItemT(infoPtr, (LPLVITEMW)lParam, (uMsg == LVM_SETITEMW));
11706 case LVM_SETITEMCOUNT:
11707 return LISTVIEW_SetItemCount(infoPtr, (INT)wParam, (DWORD)lParam);
11709 case LVM_SETITEMPOSITION:
11711 POINT pt;
11712 pt.x = (short)LOWORD(lParam);
11713 pt.y = (short)HIWORD(lParam);
11714 return LISTVIEW_SetItemPosition(infoPtr, (INT)wParam, &pt);
11717 case LVM_SETITEMPOSITION32:
11718 return LISTVIEW_SetItemPosition(infoPtr, (INT)wParam, (POINT*)lParam);
11720 case LVM_SETITEMSTATE:
11721 return LISTVIEW_SetItemState(infoPtr, (INT)wParam, (LPLVITEMW)lParam);
11723 case LVM_SETITEMTEXTA:
11724 case LVM_SETITEMTEXTW:
11725 return LISTVIEW_SetItemTextT(infoPtr, (INT)wParam, (LPLVITEMW)lParam,
11726 uMsg == LVM_SETITEMTEXTW);
11728 /* case LVM_SETOUTLINECOLOR: */
11730 case LVM_SETSELECTEDCOLUMN:
11731 infoPtr->selected_column = (INT)wParam;
11732 return TRUE;
11734 case LVM_SETSELECTIONMARK:
11735 return LISTVIEW_SetSelectionMark(infoPtr, (INT)lParam);
11737 case LVM_SETTEXTBKCOLOR:
11738 return LISTVIEW_SetTextBkColor(infoPtr, (COLORREF)lParam);
11740 case LVM_SETTEXTCOLOR:
11741 return LISTVIEW_SetTextColor(infoPtr, (COLORREF)lParam);
11743 /* case LVM_SETTILEINFO: */
11745 /* case LVM_SETTILEVIEWINFO: */
11747 /* case LVM_SETTILEWIDTH: */
11749 case LVM_SETTOOLTIPS:
11750 return (LRESULT)LISTVIEW_SetToolTips(infoPtr, (HWND)lParam);
11752 case LVM_SETUNICODEFORMAT:
11753 return LISTVIEW_SetUnicodeFormat(infoPtr, wParam);
11755 case LVM_SETVIEW:
11756 return LISTVIEW_SetView(infoPtr, wParam);
11758 /* case LVM_SETWORKAREAS: */
11760 /* case LVM_SORTGROUPS: */
11762 case LVM_SORTITEMS:
11763 case LVM_SORTITEMSEX:
11764 return LISTVIEW_SortItems(infoPtr, (PFNLVCOMPARE)lParam, wParam,
11765 uMsg == LVM_SORTITEMSEX);
11766 case LVM_SUBITEMHITTEST:
11767 return LISTVIEW_HitTest(infoPtr, (LPLVHITTESTINFO)lParam, TRUE, FALSE);
11769 case LVM_UPDATE:
11770 return LISTVIEW_Update(infoPtr, (INT)wParam);
11772 case CCM_GETVERSION:
11773 return LISTVIEW_GetVersion(infoPtr);
11775 case CCM_SETVERSION:
11776 return LISTVIEW_SetVersion(infoPtr, wParam);
11778 case WM_CHAR:
11779 return LISTVIEW_ProcessLetterKeys( infoPtr, wParam, lParam );
11781 case WM_COMMAND:
11782 return LISTVIEW_Command(infoPtr, wParam, lParam);
11784 case WM_NCCREATE:
11785 return LISTVIEW_NCCreate(hwnd, wParam, (LPCREATESTRUCTW)lParam);
11787 case WM_CREATE:
11788 return LISTVIEW_Create(hwnd, (LPCREATESTRUCTW)lParam);
11790 case WM_DESTROY:
11791 return LISTVIEW_Destroy(infoPtr);
11793 case WM_ENABLE:
11794 return LISTVIEW_Enable(infoPtr);
11796 case WM_ERASEBKGND:
11797 return LISTVIEW_EraseBkgnd(infoPtr, (HDC)wParam);
11799 case WM_GETDLGCODE:
11800 return DLGC_WANTCHARS | DLGC_WANTARROWS;
11802 case WM_GETFONT:
11803 return (LRESULT)infoPtr->hFont;
11805 case WM_HSCROLL:
11806 return LISTVIEW_HScroll(infoPtr, (INT)LOWORD(wParam), 0);
11808 case WM_KEYDOWN:
11809 return LISTVIEW_KeyDown(infoPtr, (INT)wParam, (LONG)lParam);
11811 case WM_KILLFOCUS:
11812 return LISTVIEW_KillFocus(infoPtr);
11814 case WM_LBUTTONDBLCLK:
11815 return LISTVIEW_LButtonDblClk(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11817 case WM_LBUTTONDOWN:
11818 return LISTVIEW_LButtonDown(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11820 case WM_LBUTTONUP:
11821 return LISTVIEW_LButtonUp(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11823 case WM_MOUSEMOVE:
11824 return LISTVIEW_MouseMove (infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11826 case WM_MOUSEHOVER:
11827 return LISTVIEW_MouseHover(infoPtr, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11829 case WM_NCDESTROY:
11830 return LISTVIEW_NCDestroy(infoPtr);
11832 case WM_NCPAINT:
11833 return LISTVIEW_NCPaint(infoPtr, (HRGN)wParam);
11835 case WM_NOTIFY:
11836 return LISTVIEW_Notify(infoPtr, (LPNMHDR)lParam);
11838 case WM_NOTIFYFORMAT:
11839 return LISTVIEW_NotifyFormat(infoPtr, (HWND)wParam, (INT)lParam);
11841 case WM_PRINTCLIENT:
11842 return LISTVIEW_PrintClient(infoPtr, (HDC)wParam, (DWORD)lParam);
11844 case WM_PAINT:
11845 return LISTVIEW_WMPaint(infoPtr, (HDC)wParam);
11847 case WM_RBUTTONDBLCLK:
11848 return LISTVIEW_RButtonDblClk(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11850 case WM_RBUTTONDOWN:
11851 return LISTVIEW_RButtonDown(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11853 case WM_SETCURSOR:
11854 return LISTVIEW_SetCursor(infoPtr, wParam, lParam);
11856 case WM_SETFOCUS:
11857 return LISTVIEW_SetFocus(infoPtr, (HWND)wParam);
11859 case WM_SETFONT:
11860 return LISTVIEW_SetFont(infoPtr, (HFONT)wParam, (WORD)lParam);
11862 case WM_SETREDRAW:
11863 return LISTVIEW_SetRedraw(infoPtr, (BOOL)wParam);
11865 case WM_SHOWWINDOW:
11866 return LISTVIEW_ShowWindow(infoPtr, wParam, lParam);
11868 case WM_STYLECHANGED:
11869 return LISTVIEW_StyleChanged(infoPtr, wParam, (LPSTYLESTRUCT)lParam);
11871 case WM_STYLECHANGING:
11872 return LISTVIEW_StyleChanging(wParam, (LPSTYLESTRUCT)lParam);
11874 case WM_SYSCOLORCHANGE:
11875 COMCTL32_RefreshSysColors();
11876 return 0;
11878 /* case WM_TIMER: */
11879 case WM_THEMECHANGED:
11880 return LISTVIEW_ThemeChanged(infoPtr);
11882 case WM_VSCROLL:
11883 return LISTVIEW_VScroll(infoPtr, (INT)LOWORD(wParam), 0);
11885 case WM_MOUSEWHEEL:
11886 if (wParam & (MK_SHIFT | MK_CONTROL))
11887 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
11888 return LISTVIEW_MouseWheel(infoPtr, (short int)HIWORD(wParam));
11890 case WM_WINDOWPOSCHANGED:
11891 if (!(((WINDOWPOS *)lParam)->flags & SWP_NOSIZE))
11893 SetWindowPos(infoPtr->hwndSelf, 0, 0, 0, 0, 0, SWP_FRAMECHANGED | SWP_NOACTIVATE |
11894 SWP_NOZORDER | SWP_NOMOVE | SWP_NOSIZE);
11896 if ((infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && (infoPtr->uView == LV_VIEW_DETAILS))
11898 if (notify_measureitem(infoPtr)) LISTVIEW_InvalidateList(infoPtr);
11900 LISTVIEW_Size(infoPtr, ((WINDOWPOS *)lParam)->cx, ((WINDOWPOS *)lParam)->cy);
11902 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
11904 /* case WM_WININICHANGE: */
11906 default:
11907 if ((uMsg >= WM_USER) && (uMsg < WM_APP) && !COMCTL32_IsReflectedMessage(uMsg))
11908 ERR("unknown msg %04x, wp %Ix, lp %Ix\n", uMsg, wParam, lParam);
11910 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
11915 /***
11916 * DESCRIPTION:
11917 * Registers the window class.
11919 * PARAMETER(S):
11920 * None
11922 * RETURN:
11923 * None
11925 void LISTVIEW_Register(void)
11927 WNDCLASSW wndClass;
11929 ZeroMemory(&wndClass, sizeof(WNDCLASSW));
11930 wndClass.style = CS_GLOBALCLASS | CS_DBLCLKS;
11931 wndClass.lpfnWndProc = LISTVIEW_WindowProc;
11932 wndClass.cbClsExtra = 0;
11933 wndClass.cbWndExtra = sizeof(LISTVIEW_INFO *);
11934 wndClass.hCursor = LoadCursorW(0, (LPWSTR)IDC_ARROW);
11935 wndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
11936 wndClass.lpszClassName = WC_LISTVIEWW;
11937 RegisterClassW(&wndClass);
11940 /***
11941 * DESCRIPTION:
11942 * Unregisters the window class.
11944 * PARAMETER(S):
11945 * None
11947 * RETURN:
11948 * None
11950 void LISTVIEW_Unregister(void)
11952 UnregisterClassW(WC_LISTVIEWW, NULL);
11955 /***
11956 * DESCRIPTION:
11957 * Handle any WM_COMMAND messages
11959 * PARAMETER(S):
11960 * [I] infoPtr : valid pointer to the listview structure
11961 * [I] wParam : the first message parameter
11962 * [I] lParam : the second message parameter
11964 * RETURN:
11965 * Zero.
11967 static LRESULT LISTVIEW_Command(LISTVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
11970 TRACE("%p, %x, %x, %Ix\n", infoPtr, HIWORD(wParam), LOWORD(wParam), lParam);
11972 if (!infoPtr->hwndEdit) return 0;
11974 switch (HIWORD(wParam))
11976 case EN_UPDATE:
11979 * Adjust the edit window size
11981 WCHAR buffer[1024];
11982 HDC hdc = GetDC(infoPtr->hwndEdit);
11983 HFONT hFont, hOldFont = 0;
11984 RECT rect;
11985 SIZE sz;
11987 if (!infoPtr->hwndEdit || !hdc) return 0;
11988 GetWindowTextW(infoPtr->hwndEdit, buffer, ARRAY_SIZE(buffer));
11989 GetWindowRect(infoPtr->hwndEdit, &rect);
11991 /* Select font to get the right dimension of the string */
11992 hFont = (HFONT)SendMessageW(infoPtr->hwndEdit, WM_GETFONT, 0, 0);
11993 if (hFont)
11995 hOldFont = SelectObject(hdc, hFont);
11998 if (GetTextExtentPoint32W(hdc, buffer, lstrlenW(buffer), &sz))
12000 TEXTMETRICW textMetric;
12002 /* Add Extra spacing for the next character */
12003 GetTextMetricsW(hdc, &textMetric);
12004 sz.cx += (textMetric.tmMaxCharWidth * 2);
12006 SetWindowPos(infoPtr->hwndEdit, NULL, 0, 0, sz.cx,
12007 rect.bottom - rect.top, SWP_DRAWFRAME | SWP_NOMOVE | SWP_NOZORDER);
12009 if (hFont)
12010 SelectObject(hdc, hOldFont);
12012 ReleaseDC(infoPtr->hwndEdit, hdc);
12014 break;
12016 case EN_KILLFOCUS:
12018 if (infoPtr->notify_mask & NOTIFY_MASK_END_LABEL_EDIT)
12019 LISTVIEW_CancelEditLabel(infoPtr);
12020 break;
12023 default:
12024 return SendMessageW (infoPtr->hwndNotify, WM_COMMAND, wParam, lParam);
12027 return 0;