api-ms-win-core-comm-l1-1-0: Add dll.
[wine.git] / dlls / comctl32 / listview.c
blob777b40f2fe625aa14feb410007617c3b30546f77
1 /*
2 * Listview control
4 * Copyright 1998, 1999 Eric Kohl
5 * Copyright 1999 Luc Tourangeau
6 * Copyright 2000 Jason Mawdsley
7 * Copyright 2001 CodeWeavers Inc.
8 * Copyright 2002 Dimitrie O. Paun
9 * Copyright 2009-2015 Nikolay Sivov
10 * Copyright 2009 Owen Rudge for CodeWeavers
11 * Copyright 2012-2013 Daniel Jelinski
13 * This library is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU Lesser General Public
15 * License as published by the Free Software Foundation; either
16 * version 2.1 of the License, or (at your option) any later version.
18 * This library is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 * Lesser General Public License for more details.
23 * You should have received a copy of the GNU Lesser General Public
24 * License along with this library; if not, write to the Free Software
25 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
27 * TODO:
29 * Default Message Processing
30 * -- WM_CREATE: create the icon and small icon image lists at this point only if
31 * the LVS_SHAREIMAGELISTS style is not specified.
32 * -- WM_WINDOWPOSCHANGED: arrange the list items if the current view is icon
33 * or small icon and the LVS_AUTOARRANGE style is specified.
34 * -- WM_TIMER
35 * -- WM_WININICHANGE
37 * Features
38 * -- Hot item handling, mouse hovering
39 * -- Workareas support
40 * -- Tilemode support
41 * -- Groups support
43 * Bugs
44 * -- Expand large item in ICON mode when the cursor is flying over the icon or text.
45 * -- Support CustomDraw options for _WIN32_IE >= 0x560 (see NMLVCUSTOMDRAW docs).
46 * -- LVA_SNAPTOGRID not implemented
47 * -- LISTVIEW_ApproximateViewRect partially implemented
48 * -- LISTVIEW_StyleChanged doesn't handle some changes too well
50 * Speedups
51 * -- LISTVIEW_GetNextItem needs to be rewritten. It is currently
52 * linear in the number of items in the list, and this is
53 * unacceptable for large lists.
54 * -- if list is sorted by item text LISTVIEW_InsertItemT could use
55 * binary search to calculate item index (e.g. DPA_Search()).
56 * This requires sorted state to be reliably tracked in item modifiers.
57 * -- we should keep an ordered array of coordinates in iconic mode.
58 * This would allow framing items (iterator_frameditems),
59 * and finding the nearest item (LVFI_NEARESTXY) a lot more efficiently.
61 * Flags
62 * -- LVIF_COLUMNS
63 * -- LVIF_GROUPID
65 * States
66 * -- LVIS_ACTIVATING (not currently supported by comctl32.dll version 6.0)
67 * -- LVIS_DROPHILITED
69 * Styles
70 * -- LVS_NOLABELWRAP
71 * -- LVS_NOSCROLL (see Q137520)
72 * -- LVS_ALIGNTOP
74 * Extended Styles
75 * -- LVS_EX_BORDERSELECT
76 * -- LVS_EX_FLATSB
77 * -- LVS_EX_INFOTIP
78 * -- LVS_EX_LABELTIP
79 * -- LVS_EX_MULTIWORKAREAS
80 * -- LVS_EX_REGIONAL
81 * -- LVS_EX_SIMPLESELECT
82 * -- LVS_EX_TWOCLICKACTIVATE
83 * -- LVS_EX_UNDERLINECOLD
84 * -- LVS_EX_UNDERLINEHOT
86 * Notifications:
87 * -- LVN_BEGINSCROLL, LVN_ENDSCROLL
88 * -- LVN_GETINFOTIP
89 * -- LVN_HOTTRACK
90 * -- LVN_SETDISPINFO
92 * Messages:
93 * -- LVM_ENABLEGROUPVIEW
94 * -- LVM_GETBKIMAGE, LVM_SETBKIMAGE
95 * -- LVM_GETGROUPINFO, LVM_SETGROUPINFO
96 * -- LVM_GETGROUPMETRICS, LVM_SETGROUPMETRICS
97 * -- LVM_GETINSERTMARK, LVM_SETINSERTMARK
98 * -- LVM_GETINSERTMARKCOLOR, LVM_SETINSERTMARKCOLOR
99 * -- LVM_GETINSERTMARKRECT
100 * -- LVM_GETNUMBEROFWORKAREAS
101 * -- LVM_GETOUTLINECOLOR, LVM_SETOUTLINECOLOR
102 * -- LVM_GETSELECTEDCOLUMN, LVM_SETSELECTEDCOLUMN
103 * -- LVM_GETISEARCHSTRINGW, LVM_GETISEARCHSTRINGA
104 * -- LVM_GETTILEINFO, LVM_SETTILEINFO
105 * -- LVM_GETTILEVIEWINFO, LVM_SETTILEVIEWINFO
106 * -- LVM_GETWORKAREAS, LVM_SETWORKAREAS
107 * -- LVM_HASGROUP, LVM_INSERTGROUP, LVM_REMOVEGROUP, LVM_REMOVEALLGROUPS
108 * -- LVM_INSERTGROUPSORTED
109 * -- LVM_INSERTMARKHITTEST
110 * -- LVM_ISGROUPVIEWENABLED
111 * -- LVM_MOVEGROUP
112 * -- LVM_MOVEITEMTOGROUP
113 * -- LVM_SETINFOTIP
114 * -- LVM_SETTILEWIDTH
115 * -- LVM_SORTGROUPS
117 * Macros:
118 * -- ListView_GetHoverTime, ListView_SetHoverTime
119 * -- ListView_GetISearchString
120 * -- ListView_GetNumberOfWorkAreas
121 * -- ListView_GetWorkAreas, ListView_SetWorkAreas
123 * Functions:
124 * -- LVGroupComparE
127 #include "config.h"
128 #include "wine/port.h"
130 #include <assert.h>
131 #include <ctype.h>
132 #include <string.h>
133 #include <stdlib.h>
134 #include <stdarg.h>
135 #include <stdio.h>
137 #include "windef.h"
138 #include "winbase.h"
139 #include "winnt.h"
140 #include "wingdi.h"
141 #include "winuser.h"
142 #include "winnls.h"
143 #include "commctrl.h"
144 #include "comctl32.h"
145 #include "uxtheme.h"
147 #include "wine/debug.h"
148 #include "wine/unicode.h"
150 WINE_DEFAULT_DEBUG_CHANNEL(listview);
152 typedef struct tagCOLUMN_INFO
154 RECT rcHeader; /* tracks the header's rectangle */
155 INT fmt; /* same as LVCOLUMN.fmt */
156 INT cxMin;
157 } COLUMN_INFO;
159 typedef struct tagITEMHDR
161 LPWSTR pszText;
162 INT iImage;
163 } ITEMHDR, *LPITEMHDR;
165 typedef struct tagSUBITEM_INFO
167 ITEMHDR hdr;
168 INT iSubItem;
169 } SUBITEM_INFO;
171 typedef struct tagITEM_ID ITEM_ID;
173 typedef struct tagITEM_INFO
175 ITEMHDR hdr;
176 UINT state;
177 LPARAM lParam;
178 INT iIndent;
179 ITEM_ID *id;
180 } ITEM_INFO;
182 struct tagITEM_ID
184 UINT id; /* item id */
185 HDPA item; /* link to item data */
188 typedef struct tagRANGE
190 INT lower;
191 INT upper;
192 } RANGE;
194 typedef struct tagRANGES
196 HDPA hdpa;
197 } *RANGES;
199 typedef struct tagITERATOR
201 INT nItem;
202 INT nSpecial;
203 RANGE range;
204 RANGES ranges;
205 INT index;
206 } ITERATOR;
208 typedef struct tagDELAYED_ITEM_EDIT
210 BOOL fEnabled;
211 INT iItem;
212 } DELAYED_ITEM_EDIT;
214 enum notification_mask
216 NOTIFY_MASK_ITEM_CHANGE = 0x1,
217 NOTIFY_MASK_END_LABEL_EDIT = 0x2,
218 NOTIFY_MASK_UNMASK_ALL = 0xffffffff
221 typedef struct tagLISTVIEW_INFO
223 /* control window */
224 HWND hwndSelf;
225 RECT rcList; /* This rectangle is really the window
226 * client rectangle possibly reduced by the
227 * horizontal scroll bar and/or header - see
228 * LISTVIEW_UpdateSize. This rectangle offset
229 * by the LISTVIEW_GetOrigin value is in
230 * client coordinates */
232 /* notification window */
233 SHORT notifyFormat;
234 HWND hwndNotify;
235 DWORD notify_mask;
236 UINT uCallbackMask;
238 /* tooltips */
239 HWND hwndToolTip;
241 /* items */
242 INT nItemCount; /* the number of items in the list */
243 HDPA hdpaItems; /* array ITEM_INFO pointers */
244 HDPA hdpaItemIds; /* array of ITEM_ID pointers */
245 HDPA hdpaPosX; /* maintains the (X, Y) coordinates of the */
246 HDPA hdpaPosY; /* items in LVS_ICON, and LVS_SMALLICON modes */
247 RANGES selectionRanges;
248 INT nSelectionMark; /* item to start next multiselection from */
249 INT nHotItem;
251 /* columns */
252 HDPA hdpaColumns; /* array of COLUMN_INFO pointers */
253 BOOL colRectsDirty; /* trigger column rectangles requery from header */
255 /* item metrics */
256 BOOL bNoItemMetrics; /* flags if item metrics are not yet computed */
257 INT nItemHeight;
258 INT nItemWidth;
260 /* sorting */
261 PFNLVCOMPARE pfnCompare; /* sorting callback pointer */
262 LPARAM lParamSort;
264 /* style */
265 DWORD dwStyle; /* the cached window GWL_STYLE */
266 DWORD dwLvExStyle; /* extended listview style */
267 DWORD uView; /* current view available through LVM_[G,S]ETVIEW */
269 /* edit item */
270 HWND hwndEdit;
271 WNDPROC EditWndProc;
272 INT nEditLabelItem;
273 DELAYED_ITEM_EDIT itemEdit; /* Pointer to this structure will be the timer ID */
275 /* icons */
276 HIMAGELIST himlNormal;
277 HIMAGELIST himlSmall;
278 HIMAGELIST himlState;
279 SIZE iconSize;
280 BOOL autoSpacing;
281 SIZE iconSpacing;
282 SIZE iconStateSize;
283 POINT currIconPos; /* this is the position next icon will be placed */
285 /* header */
286 HWND hwndHeader;
287 INT xTrackLine; /* The x coefficient of the track line or -1 if none */
289 /* marquee selection */
290 BOOL bMarqueeSelect; /* marquee selection/highlight underway */
291 BOOL bScrolling;
292 RECT marqueeRect; /* absolute coordinates of marquee selection */
293 RECT marqueeDrawRect; /* relative coordinates for drawing marquee */
294 POINT marqueeOrigin; /* absolute coordinates of marquee click origin */
296 /* focus drawing */
297 BOOL bFocus; /* control has focus */
298 INT nFocusedItem;
299 RECT rcFocus; /* focus bounds */
301 /* colors */
302 HBRUSH hBkBrush;
303 COLORREF clrBk;
304 COLORREF clrText;
305 COLORREF clrTextBk;
307 /* font */
308 HFONT hDefaultFont;
309 HFONT hFont;
310 INT ntmHeight; /* Some cached metrics of the font used */
311 INT ntmMaxCharWidth; /* by the listview to draw items */
312 INT nEllipsisWidth;
314 /* mouse operation */
315 BOOL bLButtonDown;
316 BOOL bDragging;
317 POINT ptClickPos; /* point where the user clicked */
318 INT nLButtonDownItem; /* tracks item to reset multiselection on WM_LBUTTONUP */
319 DWORD dwHoverTime;
320 HCURSOR hHotCursor;
321 INT cWheelRemainder;
323 /* keyboard operation */
324 DWORD lastKeyPressTimestamp;
325 WPARAM charCode;
326 INT nSearchParamLength;
327 WCHAR szSearchParam[ MAX_PATH ];
329 /* painting */
330 BOOL bIsDrawing; /* Drawing in progress */
331 INT nMeasureItemHeight; /* WM_MEASUREITEM result */
332 BOOL redraw; /* WM_SETREDRAW switch */
334 /* misc */
335 DWORD iVersion; /* CCM_[G,S]ETVERSION */
336 } LISTVIEW_INFO;
339 * constants
341 /* How many we debug buffer to allocate */
342 #define DEBUG_BUFFERS 20
343 /* The size of a single debug buffer */
344 #define DEBUG_BUFFER_SIZE 256
346 /* Internal interface to LISTVIEW_HScroll and LISTVIEW_VScroll */
347 #define SB_INTERNAL -1
349 /* maximum size of a label */
350 #define DISP_TEXT_SIZE 260
352 /* padding for items in list and small icon display modes */
353 #define WIDTH_PADDING 12
355 /* padding for items in list, report and small icon display modes */
356 #define HEIGHT_PADDING 1
358 /* offset of items in report display mode */
359 #define REPORT_MARGINX 2
361 /* padding for icon in large icon display mode
362 * ICON_TOP_PADDING_NOTHITABLE - space between top of box and area
363 * that HITTEST will see.
364 * ICON_TOP_PADDING_HITABLE - spacing between above and icon.
365 * ICON_TOP_PADDING - sum of the two above.
366 * ICON_BOTTOM_PADDING - between bottom of icon and top of text
367 * LABEL_HOR_PADDING - between text and sides of box
368 * LABEL_VERT_PADDING - between bottom of text and end of box
370 * ICON_LR_PADDING - additional width above icon size.
371 * ICON_LR_HALF - half of the above value
373 #define ICON_TOP_PADDING_NOTHITABLE 2
374 #define ICON_TOP_PADDING_HITABLE 2
375 #define ICON_TOP_PADDING (ICON_TOP_PADDING_NOTHITABLE + ICON_TOP_PADDING_HITABLE)
376 #define ICON_BOTTOM_PADDING 4
377 #define LABEL_HOR_PADDING 5
378 #define LABEL_VERT_PADDING 7
379 #define ICON_LR_PADDING 16
380 #define ICON_LR_HALF (ICON_LR_PADDING/2)
382 /* default label width for items in list and small icon display modes */
383 #define DEFAULT_LABEL_WIDTH 40
384 /* maximum select rectangle width for empty text item in LV_VIEW_DETAILS */
385 #define MAX_EMPTYTEXT_SELECT_WIDTH 80
387 /* default column width for items in list display mode */
388 #define DEFAULT_COLUMN_WIDTH 128
390 /* Size of "line" scroll for V & H scrolls */
391 #define LISTVIEW_SCROLL_ICON_LINE_SIZE 37
393 /* Padding between image and label */
394 #define IMAGE_PADDING 2
396 /* Padding behind the label */
397 #define TRAILING_LABEL_PADDING 12
398 #define TRAILING_HEADER_PADDING 11
400 /* Border for the icon caption */
401 #define CAPTION_BORDER 2
403 /* Standard DrawText flags */
404 #define LV_ML_DT_FLAGS (DT_TOP | DT_NOPREFIX | DT_EDITCONTROL | DT_CENTER | DT_WORDBREAK | DT_WORD_ELLIPSIS | DT_END_ELLIPSIS)
405 #define LV_FL_DT_FLAGS (DT_TOP | DT_NOPREFIX | DT_EDITCONTROL | DT_CENTER | DT_WORDBREAK | DT_NOCLIP)
406 #define LV_SL_DT_FLAGS (DT_VCENTER | DT_NOPREFIX | DT_EDITCONTROL | DT_SINGLELINE | DT_WORD_ELLIPSIS | DT_END_ELLIPSIS)
408 /* Image index from state */
409 #define STATEIMAGEINDEX(x) (((x) & LVIS_STATEIMAGEMASK) >> 12)
411 /* The time in milliseconds to reset the search in the list */
412 #define KEY_DELAY 450
414 /* Dump the LISTVIEW_INFO structure to the debug channel */
415 #define LISTVIEW_DUMP(iP) do { \
416 TRACE("hwndSelf=%p, clrBk=0x%06x, clrText=0x%06x, clrTextBk=0x%06x, ItemHeight=%d, ItemWidth=%d, Style=0x%08x\n", \
417 iP->hwndSelf, iP->clrBk, iP->clrText, iP->clrTextBk, \
418 iP->nItemHeight, iP->nItemWidth, iP->dwStyle); \
419 TRACE("hwndSelf=%p, himlNor=%p, himlSml=%p, himlState=%p, Focused=%d, Hot=%d, exStyle=0x%08x, Focus=%d\n", \
420 iP->hwndSelf, iP->himlNormal, iP->himlSmall, iP->himlState, \
421 iP->nFocusedItem, iP->nHotItem, iP->dwLvExStyle, iP->bFocus ); \
422 TRACE("hwndSelf=%p, ntmH=%d, icSz.cx=%d, icSz.cy=%d, icSp.cx=%d, icSp.cy=%d, notifyFmt=%d\n", \
423 iP->hwndSelf, iP->ntmHeight, iP->iconSize.cx, iP->iconSize.cy, \
424 iP->iconSpacing.cx, iP->iconSpacing.cy, iP->notifyFormat); \
425 TRACE("hwndSelf=%p, rcList=%s\n", iP->hwndSelf, wine_dbgstr_rect(&iP->rcList)); \
426 } while(0)
428 static const WCHAR themeClass[] = {'L','i','s','t','V','i','e','w',0};
431 * forward declarations
433 static BOOL LISTVIEW_GetItemT(const LISTVIEW_INFO *, LPLVITEMW, BOOL);
434 static void LISTVIEW_GetItemBox(const LISTVIEW_INFO *, INT, LPRECT);
435 static void LISTVIEW_GetItemOrigin(const LISTVIEW_INFO *, INT, LPPOINT);
436 static BOOL LISTVIEW_GetItemPosition(const LISTVIEW_INFO *, INT, LPPOINT);
437 static BOOL LISTVIEW_GetItemRect(const LISTVIEW_INFO *, INT, LPRECT);
438 static void LISTVIEW_GetOrigin(const LISTVIEW_INFO *, LPPOINT);
439 static BOOL LISTVIEW_GetViewRect(const LISTVIEW_INFO *, LPRECT);
440 static void LISTVIEW_UpdateSize(LISTVIEW_INFO *);
441 static LRESULT LISTVIEW_Command(LISTVIEW_INFO *, WPARAM, LPARAM);
442 static INT LISTVIEW_GetStringWidthT(const LISTVIEW_INFO *, LPCWSTR, BOOL);
443 static BOOL LISTVIEW_KeySelection(LISTVIEW_INFO *, INT, BOOL);
444 static UINT LISTVIEW_GetItemState(const LISTVIEW_INFO *, INT, UINT);
445 static BOOL LISTVIEW_SetItemState(LISTVIEW_INFO *, INT, const LVITEMW *);
446 static LRESULT LISTVIEW_VScroll(LISTVIEW_INFO *, INT, INT);
447 static LRESULT LISTVIEW_HScroll(LISTVIEW_INFO *, INT, INT);
448 static BOOL LISTVIEW_EnsureVisible(LISTVIEW_INFO *, INT, BOOL);
449 static HIMAGELIST LISTVIEW_SetImageList(LISTVIEW_INFO *, INT, HIMAGELIST);
450 static INT LISTVIEW_HitTest(const LISTVIEW_INFO *, LPLVHITTESTINFO, BOOL, BOOL);
451 static BOOL LISTVIEW_EndEditLabelT(LISTVIEW_INFO *, BOOL, BOOL);
452 static BOOL LISTVIEW_Scroll(LISTVIEW_INFO *, INT, INT);
454 /******** Text handling functions *************************************/
456 /* A text pointer is either NULL, LPSTR_TEXTCALLBACK, or points to a
457 * text string. The string may be ANSI or Unicode, in which case
458 * the boolean isW tells us the type of the string.
460 * The name of the function tell what type of strings it expects:
461 * W: Unicode, T: ANSI/Unicode - function of isW
464 static inline BOOL is_text(LPCWSTR text)
466 return text != NULL && text != LPSTR_TEXTCALLBACKW;
469 static inline int textlenT(LPCWSTR text, BOOL isW)
471 return !is_text(text) ? 0 :
472 isW ? lstrlenW(text) : lstrlenA((LPCSTR)text);
475 static inline void textcpynT(LPWSTR dest, BOOL isDestW, LPCWSTR src, BOOL isSrcW, INT max)
477 if (isDestW)
478 if (isSrcW) lstrcpynW(dest, src, max);
479 else MultiByteToWideChar(CP_ACP, 0, (LPCSTR)src, -1, dest, max);
480 else
481 if (isSrcW) WideCharToMultiByte(CP_ACP, 0, src, -1, (LPSTR)dest, max, NULL, NULL);
482 else lstrcpynA((LPSTR)dest, (LPCSTR)src, max);
485 static inline LPWSTR textdupTtoW(LPCWSTR text, BOOL isW)
487 LPWSTR wstr = (LPWSTR)text;
489 if (!isW && is_text(text))
491 INT len = MultiByteToWideChar(CP_ACP, 0, (LPCSTR)text, -1, NULL, 0);
492 wstr = Alloc(len * sizeof(WCHAR));
493 if (wstr) MultiByteToWideChar(CP_ACP, 0, (LPCSTR)text, -1, wstr, len);
495 TRACE(" wstr=%s\n", text == LPSTR_TEXTCALLBACKW ? "(callback)" : debugstr_w(wstr));
496 return wstr;
499 static inline void textfreeT(LPWSTR wstr, BOOL isW)
501 if (!isW && is_text(wstr)) Free (wstr);
505 * dest is a pointer to a Unicode string
506 * src is a pointer to a string (Unicode if isW, ANSI if !isW)
508 static BOOL textsetptrT(LPWSTR *dest, LPCWSTR src, BOOL isW)
510 BOOL bResult = TRUE;
512 if (src == LPSTR_TEXTCALLBACKW)
514 if (is_text(*dest)) Free(*dest);
515 *dest = LPSTR_TEXTCALLBACKW;
517 else
519 LPWSTR pszText = textdupTtoW(src, isW);
520 if (*dest == LPSTR_TEXTCALLBACKW) *dest = NULL;
521 bResult = Str_SetPtrW(dest, pszText);
522 textfreeT(pszText, isW);
524 return bResult;
528 * compares a Unicode to a Unicode/ANSI text string
530 static inline int textcmpWT(LPCWSTR aw, LPCWSTR bt, BOOL isW)
532 if (!aw) return bt ? -1 : 0;
533 if (!bt) return 1;
534 if (aw == LPSTR_TEXTCALLBACKW)
535 return bt == LPSTR_TEXTCALLBACKW ? 1 : -1;
536 if (bt != LPSTR_TEXTCALLBACKW)
538 LPWSTR bw = textdupTtoW(bt, isW);
539 int r = bw ? lstrcmpW(aw, bw) : 1;
540 textfreeT(bw, isW);
541 return r;
544 return 1;
547 static inline int lstrncmpiW(LPCWSTR s1, LPCWSTR s2, int n)
549 n = min(min(n, lstrlenW(s1)), lstrlenW(s2));
550 return CompareStringW(LOCALE_USER_DEFAULT, NORM_IGNORECASE, s1, n, s2, n) - CSTR_EQUAL;
553 /******** Debugging functions *****************************************/
555 static inline LPCSTR debugtext_t(LPCWSTR text, BOOL isW)
557 if (text == LPSTR_TEXTCALLBACKW) return "(callback)";
558 return isW ? debugstr_w(text) : debugstr_a((LPCSTR)text);
561 static inline LPCSTR debugtext_tn(LPCWSTR text, BOOL isW, INT n)
563 if (text == LPSTR_TEXTCALLBACKW) return "(callback)";
564 n = min(textlenT(text, isW), n);
565 return isW ? debugstr_wn(text, n) : debugstr_an((LPCSTR)text, n);
568 static char* debug_getbuf(void)
570 static int index = 0;
571 static char buffers[DEBUG_BUFFERS][DEBUG_BUFFER_SIZE];
572 return buffers[index++ % DEBUG_BUFFERS];
575 static inline const char* debugrange(const RANGE *lprng)
577 if (!lprng) return "(null)";
578 return wine_dbg_sprintf("[%d, %d]", lprng->lower, lprng->upper);
581 static const char* debugscrollinfo(const SCROLLINFO *pScrollInfo)
583 char* buf = debug_getbuf(), *text = buf;
584 int len, size = DEBUG_BUFFER_SIZE;
586 if (pScrollInfo == NULL) return "(null)";
587 len = snprintf(buf, size, "{cbSize=%u, ", pScrollInfo->cbSize);
588 if (len == -1) goto end;
589 buf += len; size -= len;
590 if (pScrollInfo->fMask & SIF_RANGE)
591 len = snprintf(buf, size, "nMin=%d, nMax=%d, ", pScrollInfo->nMin, pScrollInfo->nMax);
592 else len = 0;
593 if (len == -1) goto end;
594 buf += len; size -= len;
595 if (pScrollInfo->fMask & SIF_PAGE)
596 len = snprintf(buf, size, "nPage=%u, ", pScrollInfo->nPage);
597 else len = 0;
598 if (len == -1) goto end;
599 buf += len; size -= len;
600 if (pScrollInfo->fMask & SIF_POS)
601 len = snprintf(buf, size, "nPos=%d, ", pScrollInfo->nPos);
602 else len = 0;
603 if (len == -1) goto end;
604 buf += len; size -= len;
605 if (pScrollInfo->fMask & SIF_TRACKPOS)
606 len = snprintf(buf, size, "nTrackPos=%d, ", pScrollInfo->nTrackPos);
607 else len = 0;
608 if (len == -1) goto end;
609 buf += len;
610 goto undo;
611 end:
612 buf = text + strlen(text);
613 undo:
614 if (buf - text > 2) { buf[-2] = '}'; buf[-1] = 0; }
615 return text;
618 static const char* debugnmlistview(const NMLISTVIEW *plvnm)
620 if (!plvnm) return "(null)";
621 return wine_dbg_sprintf("iItem=%d, iSubItem=%d, uNewState=0x%x,"
622 " uOldState=0x%x, uChanged=0x%x, ptAction=%s, lParam=%ld",
623 plvnm->iItem, plvnm->iSubItem, plvnm->uNewState, plvnm->uOldState,
624 plvnm->uChanged, wine_dbgstr_point(&plvnm->ptAction), plvnm->lParam);
627 static const char* debuglvitem_t(const LVITEMW *lpLVItem, BOOL isW)
629 char* buf = debug_getbuf(), *text = buf;
630 int len, size = DEBUG_BUFFER_SIZE;
632 if (lpLVItem == NULL) return "(null)";
633 len = snprintf(buf, size, "{iItem=%d, iSubItem=%d, ", lpLVItem->iItem, lpLVItem->iSubItem);
634 if (len == -1) goto end;
635 buf += len; size -= len;
636 if (lpLVItem->mask & LVIF_STATE)
637 len = snprintf(buf, size, "state=%x, stateMask=%x, ", lpLVItem->state, lpLVItem->stateMask);
638 else len = 0;
639 if (len == -1) goto end;
640 buf += len; size -= len;
641 if (lpLVItem->mask & LVIF_TEXT)
642 len = snprintf(buf, size, "pszText=%s, cchTextMax=%d, ", debugtext_tn(lpLVItem->pszText, isW, 80), lpLVItem->cchTextMax);
643 else len = 0;
644 if (len == -1) goto end;
645 buf += len; size -= len;
646 if (lpLVItem->mask & LVIF_IMAGE)
647 len = snprintf(buf, size, "iImage=%d, ", lpLVItem->iImage);
648 else len = 0;
649 if (len == -1) goto end;
650 buf += len; size -= len;
651 if (lpLVItem->mask & LVIF_PARAM)
652 len = snprintf(buf, size, "lParam=%lx, ", lpLVItem->lParam);
653 else len = 0;
654 if (len == -1) goto end;
655 buf += len; size -= len;
656 if (lpLVItem->mask & LVIF_INDENT)
657 len = snprintf(buf, size, "iIndent=%d, ", lpLVItem->iIndent);
658 else len = 0;
659 if (len == -1) goto end;
660 buf += len;
661 goto undo;
662 end:
663 buf = text + strlen(text);
664 undo:
665 if (buf - text > 2) { buf[-2] = '}'; buf[-1] = 0; }
666 return text;
669 static const char* debuglvcolumn_t(const LVCOLUMNW *lpColumn, BOOL isW)
671 char* buf = debug_getbuf(), *text = buf;
672 int len, size = DEBUG_BUFFER_SIZE;
674 if (lpColumn == NULL) return "(null)";
675 len = snprintf(buf, size, "{");
676 if (len == -1) goto end;
677 buf += len; size -= len;
678 if (lpColumn->mask & LVCF_SUBITEM)
679 len = snprintf(buf, size, "iSubItem=%d, ", lpColumn->iSubItem);
680 else len = 0;
681 if (len == -1) goto end;
682 buf += len; size -= len;
683 if (lpColumn->mask & LVCF_FMT)
684 len = snprintf(buf, size, "fmt=%x, ", lpColumn->fmt);
685 else len = 0;
686 if (len == -1) goto end;
687 buf += len; size -= len;
688 if (lpColumn->mask & LVCF_WIDTH)
689 len = snprintf(buf, size, "cx=%d, ", lpColumn->cx);
690 else len = 0;
691 if (len == -1) goto end;
692 buf += len; size -= len;
693 if (lpColumn->mask & LVCF_TEXT)
694 len = snprintf(buf, size, "pszText=%s, cchTextMax=%d, ", debugtext_tn(lpColumn->pszText, isW, 80), lpColumn->cchTextMax);
695 else len = 0;
696 if (len == -1) goto end;
697 buf += len; size -= len;
698 if (lpColumn->mask & LVCF_IMAGE)
699 len = snprintf(buf, size, "iImage=%d, ", lpColumn->iImage);
700 else len = 0;
701 if (len == -1) goto end;
702 buf += len; size -= len;
703 if (lpColumn->mask & LVCF_ORDER)
704 len = snprintf(buf, size, "iOrder=%d, ", lpColumn->iOrder);
705 else len = 0;
706 if (len == -1) goto end;
707 buf += len;
708 goto undo;
709 end:
710 buf = text + strlen(text);
711 undo:
712 if (buf - text > 2) { buf[-2] = '}'; buf[-1] = 0; }
713 return text;
716 static const char* debuglvhittestinfo(const LVHITTESTINFO *lpht)
718 if (!lpht) return "(null)";
720 return wine_dbg_sprintf("{pt=%s, flags=0x%x, iItem=%d, iSubItem=%d}",
721 wine_dbgstr_point(&lpht->pt), lpht->flags, lpht->iItem, lpht->iSubItem);
724 /* Return the corresponding text for a given scroll value */
725 static inline LPCSTR debugscrollcode(int nScrollCode)
727 switch(nScrollCode)
729 case SB_LINELEFT: return "SB_LINELEFT";
730 case SB_LINERIGHT: return "SB_LINERIGHT";
731 case SB_PAGELEFT: return "SB_PAGELEFT";
732 case SB_PAGERIGHT: return "SB_PAGERIGHT";
733 case SB_THUMBPOSITION: return "SB_THUMBPOSITION";
734 case SB_THUMBTRACK: return "SB_THUMBTRACK";
735 case SB_ENDSCROLL: return "SB_ENDSCROLL";
736 case SB_INTERNAL: return "SB_INTERNAL";
737 default: return "unknown";
742 /******** Notification functions ************************************/
744 static int get_ansi_notification(UINT unicodeNotificationCode)
746 switch (unicodeNotificationCode)
748 case LVN_BEGINLABELEDITA:
749 case LVN_BEGINLABELEDITW: return LVN_BEGINLABELEDITA;
750 case LVN_ENDLABELEDITA:
751 case LVN_ENDLABELEDITW: return LVN_ENDLABELEDITA;
752 case LVN_GETDISPINFOA:
753 case LVN_GETDISPINFOW: return LVN_GETDISPINFOA;
754 case LVN_SETDISPINFOA:
755 case LVN_SETDISPINFOW: return LVN_SETDISPINFOA;
756 case LVN_ODFINDITEMA:
757 case LVN_ODFINDITEMW: return LVN_ODFINDITEMA;
758 case LVN_GETINFOTIPA:
759 case LVN_GETINFOTIPW: return LVN_GETINFOTIPA;
760 /* header forwards */
761 case HDN_TRACKA:
762 case HDN_TRACKW: return HDN_TRACKA;
763 case HDN_ENDTRACKA:
764 case HDN_ENDTRACKW: return HDN_ENDTRACKA;
765 case HDN_BEGINDRAG: return HDN_BEGINDRAG;
766 case HDN_ENDDRAG: return HDN_ENDDRAG;
767 case HDN_ITEMCHANGINGA:
768 case HDN_ITEMCHANGINGW: return HDN_ITEMCHANGINGA;
769 case HDN_ITEMCHANGEDA:
770 case HDN_ITEMCHANGEDW: return HDN_ITEMCHANGEDA;
771 case HDN_ITEMCLICKA:
772 case HDN_ITEMCLICKW: return HDN_ITEMCLICKA;
773 case HDN_DIVIDERDBLCLICKA:
774 case HDN_DIVIDERDBLCLICKW: return HDN_DIVIDERDBLCLICKA;
775 default: break;
777 FIXME("unknown notification %x\n", unicodeNotificationCode);
778 return unicodeNotificationCode;
781 /* forwards header notifications to listview parent */
782 static LRESULT notify_forward_header(const LISTVIEW_INFO *infoPtr, NMHEADERW *lpnmhW)
784 LPCWSTR text = NULL, filter = NULL;
785 LRESULT ret;
786 NMHEADERA *lpnmh = (NMHEADERA*) lpnmhW;
788 /* on unicode format exit earlier */
789 if (infoPtr->notifyFormat == NFR_UNICODE)
790 return SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, lpnmh->hdr.idFrom,
791 (LPARAM)lpnmh);
793 /* header always supplies unicode notifications,
794 all we have to do is to convert strings to ANSI */
795 if (lpnmh->pitem)
797 /* convert item text */
798 if (lpnmh->pitem->mask & HDI_TEXT)
800 text = (LPCWSTR)lpnmh->pitem->pszText;
801 lpnmh->pitem->pszText = NULL;
802 Str_SetPtrWtoA(&lpnmh->pitem->pszText, text);
804 /* convert filter text */
805 if ((lpnmh->pitem->mask & HDI_FILTER) && (lpnmh->pitem->type == HDFT_ISSTRING) &&
806 lpnmh->pitem->pvFilter)
808 filter = (LPCWSTR)((HD_TEXTFILTERA*)lpnmh->pitem->pvFilter)->pszText;
809 ((HD_TEXTFILTERA*)lpnmh->pitem->pvFilter)->pszText = NULL;
810 Str_SetPtrWtoA(&((HD_TEXTFILTERA*)lpnmh->pitem->pvFilter)->pszText, filter);
813 lpnmh->hdr.code = get_ansi_notification(lpnmh->hdr.code);
815 ret = SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, lpnmh->hdr.idFrom,
816 (LPARAM)lpnmh);
818 /* cleanup */
819 if(text)
821 Free(lpnmh->pitem->pszText);
822 lpnmh->pitem->pszText = (LPSTR)text;
824 if(filter)
826 Free(((HD_TEXTFILTERA*)lpnmh->pitem->pvFilter)->pszText);
827 ((HD_TEXTFILTERA*)lpnmh->pitem->pvFilter)->pszText = (LPSTR)filter;
830 return ret;
833 static LRESULT notify_hdr(const LISTVIEW_INFO *infoPtr, INT code, LPNMHDR pnmh)
835 LRESULT result;
837 TRACE("(code=%d)\n", code);
839 pnmh->hwndFrom = infoPtr->hwndSelf;
840 pnmh->idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
841 pnmh->code = code;
842 result = SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, pnmh->idFrom, (LPARAM)pnmh);
844 TRACE(" <= %ld\n", result);
846 return result;
849 static inline BOOL notify(const LISTVIEW_INFO *infoPtr, INT code)
851 NMHDR nmh;
852 HWND hwnd = infoPtr->hwndSelf;
853 notify_hdr(infoPtr, code, &nmh);
854 return IsWindow(hwnd);
857 static inline void notify_itemactivate(const LISTVIEW_INFO *infoPtr, const LVHITTESTINFO *htInfo)
859 NMITEMACTIVATE nmia;
860 LVITEMW item;
862 nmia.uNewState = 0;
863 nmia.uOldState = 0;
864 nmia.uChanged = 0;
865 nmia.uKeyFlags = 0;
867 item.mask = LVIF_PARAM|LVIF_STATE;
868 item.iItem = htInfo->iItem;
869 item.iSubItem = 0;
870 item.stateMask = (UINT)-1;
871 if (LISTVIEW_GetItemT(infoPtr, &item, TRUE)) {
872 nmia.lParam = item.lParam;
873 nmia.uOldState = item.state;
874 nmia.uNewState = item.state | LVIS_ACTIVATING;
875 nmia.uChanged = LVIF_STATE;
878 nmia.iItem = htInfo->iItem;
879 nmia.iSubItem = htInfo->iSubItem;
880 nmia.ptAction = htInfo->pt;
882 if (GetKeyState(VK_SHIFT) & 0x8000) nmia.uKeyFlags |= LVKF_SHIFT;
883 if (GetKeyState(VK_CONTROL) & 0x8000) nmia.uKeyFlags |= LVKF_CONTROL;
884 if (GetKeyState(VK_MENU) & 0x8000) nmia.uKeyFlags |= LVKF_ALT;
886 notify_hdr(infoPtr, LVN_ITEMACTIVATE, (LPNMHDR)&nmia);
889 static inline LRESULT notify_listview(const LISTVIEW_INFO *infoPtr, INT code, LPNMLISTVIEW plvnm)
891 TRACE("(code=%d, plvnm=%s)\n", code, debugnmlistview(plvnm));
892 return notify_hdr(infoPtr, code, (LPNMHDR)plvnm);
895 /* Handles NM_DBLCLK, NM_CLICK, NM_RDBLCLK, NM_RCLICK. Only NM_RCLICK return value is used. */
896 static BOOL notify_click(const LISTVIEW_INFO *infoPtr, INT code, const LVHITTESTINFO *lvht)
898 NMITEMACTIVATE nmia;
899 LVITEMW item;
900 HWND hwnd = infoPtr->hwndSelf;
901 LRESULT ret;
903 TRACE("code=%d, lvht=%s\n", code, debuglvhittestinfo(lvht));
904 ZeroMemory(&nmia, sizeof(nmia));
905 nmia.iItem = lvht->iItem;
906 nmia.iSubItem = lvht->iSubItem;
907 nmia.ptAction = lvht->pt;
908 item.mask = LVIF_PARAM;
909 item.iItem = lvht->iItem;
910 item.iSubItem = 0;
911 if (LISTVIEW_GetItemT(infoPtr, &item, TRUE)) nmia.lParam = item.lParam;
912 ret = notify_hdr(infoPtr, code, (NMHDR*)&nmia);
913 return IsWindow(hwnd) && (code == NM_RCLICK ? !ret : TRUE);
916 static BOOL notify_deleteitem(const LISTVIEW_INFO *infoPtr, INT nItem)
918 NMLISTVIEW nmlv;
919 LVITEMW item;
920 HWND hwnd = infoPtr->hwndSelf;
922 ZeroMemory(&nmlv, sizeof (NMLISTVIEW));
923 nmlv.iItem = nItem;
924 item.mask = LVIF_PARAM;
925 item.iItem = nItem;
926 item.iSubItem = 0;
927 if (LISTVIEW_GetItemT(infoPtr, &item, TRUE)) nmlv.lParam = item.lParam;
928 notify_listview(infoPtr, LVN_DELETEITEM, &nmlv);
929 return IsWindow(hwnd);
933 Send notification. depends on dispinfoW having same
934 structure as dispinfoA.
935 infoPtr : listview struct
936 code : *Unicode* notification code
937 pdi : dispinfo structure (can be unicode or ansi)
938 isW : TRUE if dispinfo is Unicode
940 static BOOL notify_dispinfoT(const LISTVIEW_INFO *infoPtr, UINT code, LPNMLVDISPINFOW pdi, BOOL isW)
942 INT length = 0, ret_length;
943 LPWSTR buffer = NULL, ret_text;
944 BOOL return_ansi = FALSE;
945 BOOL return_unicode = FALSE;
946 BOOL ret;
948 if ((pdi->item.mask & LVIF_TEXT) && is_text(pdi->item.pszText))
950 return_unicode = ( isW && infoPtr->notifyFormat == NFR_ANSI);
951 return_ansi = (!isW && infoPtr->notifyFormat == NFR_UNICODE);
954 ret_length = pdi->item.cchTextMax;
955 ret_text = pdi->item.pszText;
957 if (return_unicode || return_ansi)
959 if (code != LVN_GETDISPINFOW)
961 length = return_ansi ?
962 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pdi->item.pszText, -1, NULL, 0):
963 WideCharToMultiByte(CP_ACP, 0, pdi->item.pszText, -1, NULL, 0, NULL, NULL);
965 else
967 length = pdi->item.cchTextMax;
968 *pdi->item.pszText = 0; /* make sure we don't process garbage */
971 buffer = Alloc( (return_ansi ? sizeof(WCHAR) : sizeof(CHAR)) * length);
972 if (!buffer) return FALSE;
974 if (return_ansi)
975 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pdi->item.pszText, -1,
976 buffer, length);
977 else
978 WideCharToMultiByte(CP_ACP, 0, pdi->item.pszText, -1, (LPSTR) buffer,
979 length, NULL, NULL);
981 pdi->item.pszText = buffer;
982 pdi->item.cchTextMax = length;
985 if (infoPtr->notifyFormat == NFR_ANSI)
986 code = get_ansi_notification(code);
988 TRACE(" pdi->item=%s\n", debuglvitem_t(&pdi->item, infoPtr->notifyFormat != NFR_ANSI));
989 ret = notify_hdr(infoPtr, code, &pdi->hdr);
990 TRACE(" resulting code=%d\n", pdi->hdr.code);
992 if (return_ansi || return_unicode)
994 if (return_ansi && (pdi->hdr.code == LVN_GETDISPINFOA))
996 strcpy((char*)ret_text, (char*)pdi->item.pszText);
998 else if (return_unicode && (pdi->hdr.code == LVN_GETDISPINFOW))
1000 strcpyW(ret_text, pdi->item.pszText);
1002 else if (return_ansi) /* note : pointer can be changed by app ! */
1004 WideCharToMultiByte(CP_ACP, 0, pdi->item.pszText, -1, (LPSTR) ret_text,
1005 ret_length, NULL, NULL);
1007 else
1008 MultiByteToWideChar(CP_ACP, 0, (LPSTR) pdi->item.pszText, -1,
1009 ret_text, ret_length);
1011 pdi->item.pszText = ret_text; /* restores our buffer */
1012 pdi->item.cchTextMax = ret_length;
1014 Free(buffer);
1015 return ret;
1018 /* if dispinfo holder changed notification code then convert */
1019 if (!isW && (pdi->hdr.code == LVN_GETDISPINFOW) && (pdi->item.mask & LVIF_TEXT))
1021 length = WideCharToMultiByte(CP_ACP, 0, pdi->item.pszText, -1, NULL, 0, NULL, NULL);
1023 buffer = Alloc(length * sizeof(CHAR));
1024 if (!buffer) return FALSE;
1026 WideCharToMultiByte(CP_ACP, 0, pdi->item.pszText, -1, (LPSTR) buffer,
1027 ret_length, NULL, NULL);
1029 strcpy((LPSTR)pdi->item.pszText, (LPSTR)buffer);
1030 Free(buffer);
1033 return ret;
1036 static void customdraw_fill(NMLVCUSTOMDRAW *lpnmlvcd, const LISTVIEW_INFO *infoPtr, HDC hdc,
1037 const RECT *rcBounds, const LVITEMW *lplvItem)
1039 ZeroMemory(lpnmlvcd, sizeof(NMLVCUSTOMDRAW));
1040 lpnmlvcd->nmcd.hdc = hdc;
1041 lpnmlvcd->nmcd.rc = *rcBounds;
1042 lpnmlvcd->clrTextBk = infoPtr->clrTextBk;
1043 lpnmlvcd->clrText = infoPtr->clrText;
1044 if (!lplvItem) return;
1045 lpnmlvcd->nmcd.dwItemSpec = lplvItem->iItem + 1;
1046 lpnmlvcd->iSubItem = lplvItem->iSubItem;
1047 if (lplvItem->state & LVIS_SELECTED) lpnmlvcd->nmcd.uItemState |= CDIS_SELECTED;
1048 if (lplvItem->state & LVIS_FOCUSED) lpnmlvcd->nmcd.uItemState |= CDIS_FOCUS;
1049 if (lplvItem->iItem == infoPtr->nHotItem) lpnmlvcd->nmcd.uItemState |= CDIS_HOT;
1050 lpnmlvcd->nmcd.lItemlParam = lplvItem->lParam;
1053 static inline DWORD notify_customdraw (const LISTVIEW_INFO *infoPtr, DWORD dwDrawStage, NMLVCUSTOMDRAW *lpnmlvcd)
1055 BOOL isForItem = (lpnmlvcd->nmcd.dwItemSpec != 0);
1056 DWORD result;
1058 lpnmlvcd->nmcd.dwDrawStage = dwDrawStage;
1059 if (isForItem) lpnmlvcd->nmcd.dwDrawStage |= CDDS_ITEM;
1060 if (lpnmlvcd->iSubItem) lpnmlvcd->nmcd.dwDrawStage |= CDDS_SUBITEM;
1061 if (isForItem) lpnmlvcd->nmcd.dwItemSpec--;
1062 result = notify_hdr(infoPtr, NM_CUSTOMDRAW, &lpnmlvcd->nmcd.hdr);
1063 if (isForItem) lpnmlvcd->nmcd.dwItemSpec++;
1064 return result;
1067 static void prepaint_setup (const LISTVIEW_INFO *infoPtr, HDC hdc, NMLVCUSTOMDRAW *lpnmlvcd, BOOL SubItem)
1069 COLORREF backcolor, textcolor;
1071 /* apparently, for selected items, we have to override the returned values */
1072 if (!SubItem)
1074 if (lpnmlvcd->nmcd.uItemState & CDIS_SELECTED)
1076 if (infoPtr->bFocus)
1078 lpnmlvcd->clrTextBk = comctl32_color.clrHighlight;
1079 lpnmlvcd->clrText = comctl32_color.clrHighlightText;
1081 else if (infoPtr->dwStyle & LVS_SHOWSELALWAYS)
1083 lpnmlvcd->clrTextBk = comctl32_color.clr3dFace;
1084 lpnmlvcd->clrText = comctl32_color.clrBtnText;
1089 backcolor = lpnmlvcd->clrTextBk;
1090 textcolor = lpnmlvcd->clrText;
1092 if (backcolor == CLR_DEFAULT)
1093 backcolor = comctl32_color.clrWindow;
1094 if (textcolor == CLR_DEFAULT)
1095 textcolor = comctl32_color.clrWindowText;
1097 /* Set the text attributes */
1098 if (backcolor != CLR_NONE)
1100 SetBkMode(hdc, OPAQUE);
1101 SetBkColor(hdc, backcolor);
1103 else
1104 SetBkMode(hdc, TRANSPARENT);
1105 SetTextColor(hdc, textcolor);
1108 static inline DWORD notify_postpaint (const LISTVIEW_INFO *infoPtr, NMLVCUSTOMDRAW *lpnmlvcd)
1110 return notify_customdraw(infoPtr, CDDS_POSTPAINT, lpnmlvcd);
1113 /* returns TRUE when repaint needed, FALSE otherwise */
1114 static BOOL notify_measureitem(LISTVIEW_INFO *infoPtr)
1116 MEASUREITEMSTRUCT mis;
1117 mis.CtlType = ODT_LISTVIEW;
1118 mis.CtlID = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
1119 mis.itemID = -1;
1120 mis.itemWidth = 0;
1121 mis.itemData = 0;
1122 mis.itemHeight= infoPtr->nItemHeight;
1123 SendMessageW(infoPtr->hwndNotify, WM_MEASUREITEM, mis.CtlID, (LPARAM)&mis);
1124 if (infoPtr->nItemHeight != max(mis.itemHeight, 1))
1126 infoPtr->nMeasureItemHeight = infoPtr->nItemHeight = max(mis.itemHeight, 1);
1127 return TRUE;
1129 return FALSE;
1132 /******** Item iterator functions **********************************/
1134 static RANGES ranges_create(int count);
1135 static void ranges_destroy(RANGES ranges);
1136 static BOOL ranges_add(RANGES ranges, RANGE range);
1137 static BOOL ranges_del(RANGES ranges, RANGE range);
1138 static void ranges_dump(RANGES ranges);
1140 static inline BOOL ranges_additem(RANGES ranges, INT nItem)
1142 RANGE range = { nItem, nItem + 1 };
1144 return ranges_add(ranges, range);
1147 static inline BOOL ranges_delitem(RANGES ranges, INT nItem)
1149 RANGE range = { nItem, nItem + 1 };
1151 return ranges_del(ranges, range);
1154 /***
1155 * ITERATOR DOCUMENTATION
1157 * The iterator functions allow for easy, and convenient iteration
1158 * over items of interest in the list. Typically, you create an
1159 * iterator, use it, and destroy it, as such:
1160 * ITERATOR i;
1162 * iterator_xxxitems(&i, ...);
1163 * while (iterator_{prev,next}(&i)
1165 * //code which uses i.nItem
1167 * iterator_destroy(&i);
1169 * where xxx is either: framed, or visible.
1170 * Note that it is important that the code destroys the iterator
1171 * after it's done with it, as the creation of the iterator may
1172 * allocate memory, which thus needs to be freed.
1174 * You can iterate both forwards, and backwards through the list,
1175 * by using iterator_next or iterator_prev respectively.
1177 * Lower numbered items are draw on top of higher number items in
1178 * LVS_ICON, and LVS_SMALLICON (which are the only modes where
1179 * items may overlap). So, to test items, you should use
1180 * iterator_next
1181 * which lists the items top to bottom (in Z-order).
1182 * For drawing items, you should use
1183 * iterator_prev
1184 * which lists the items bottom to top (in Z-order).
1185 * If you keep iterating over the items after the end-of-items
1186 * marker (-1) is returned, the iterator will start from the
1187 * beginning. Typically, you don't need to test for -1,
1188 * because iterator_{next,prev} will return TRUE if more items
1189 * are to be iterated over, or FALSE otherwise.
1191 * Note: the iterator is defined to be bidirectional. That is,
1192 * any number of prev followed by any number of next, or
1193 * five versa, should leave the iterator at the same item:
1194 * prev * n, next * n = next * n, prev * n
1196 * The iterator has a notion of an out-of-order, special item,
1197 * which sits at the start of the list. This is used in
1198 * LVS_ICON, and LVS_SMALLICON mode to handle the focused item,
1199 * which needs to be first, as it may overlap other items.
1201 * The code is a bit messy because we have:
1202 * - a special item to deal with
1203 * - simple range, or composite range
1204 * - empty range.
1205 * If you find bugs, or want to add features, please make sure you
1206 * always check/modify *both* iterator_prev, and iterator_next.
1209 /****
1210 * This function iterates through the items in increasing order,
1211 * but prefixed by the special item, then -1. That is:
1212 * special, 1, 2, 3, ..., n, -1.
1213 * Each item is listed only once.
1215 static inline BOOL iterator_next(ITERATOR* i)
1217 if (i->nItem == -1)
1219 i->nItem = i->nSpecial;
1220 if (i->nItem != -1) return TRUE;
1222 if (i->nItem == i->nSpecial)
1224 if (i->ranges) i->index = 0;
1225 goto pickarange;
1228 i->nItem++;
1229 testitem:
1230 if (i->nItem == i->nSpecial) i->nItem++;
1231 if (i->nItem < i->range.upper) return TRUE;
1233 pickarange:
1234 if (i->ranges)
1236 if (i->index < DPA_GetPtrCount(i->ranges->hdpa))
1237 i->range = *(RANGE*)DPA_GetPtr(i->ranges->hdpa, i->index++);
1238 else goto end;
1240 else if (i->nItem >= i->range.upper) goto end;
1242 i->nItem = i->range.lower;
1243 if (i->nItem >= 0) goto testitem;
1244 end:
1245 i->nItem = -1;
1246 return FALSE;
1249 /****
1250 * This function iterates through the items in decreasing order,
1251 * followed by the special item, then -1. That is:
1252 * n, n-1, ..., 3, 2, 1, special, -1.
1253 * Each item is listed only once.
1255 static inline BOOL iterator_prev(ITERATOR* i)
1257 BOOL start = FALSE;
1259 if (i->nItem == -1)
1261 start = TRUE;
1262 if (i->ranges) i->index = DPA_GetPtrCount(i->ranges->hdpa);
1263 goto pickarange;
1265 if (i->nItem == i->nSpecial)
1267 i->nItem = -1;
1268 return FALSE;
1271 testitem:
1272 i->nItem--;
1273 if (i->nItem == i->nSpecial) i->nItem--;
1274 if (i->nItem >= i->range.lower) return TRUE;
1276 pickarange:
1277 if (i->ranges)
1279 if (i->index > 0)
1280 i->range = *(RANGE*)DPA_GetPtr(i->ranges->hdpa, --i->index);
1281 else goto end;
1283 else if (!start && i->nItem < i->range.lower) goto end;
1285 i->nItem = i->range.upper;
1286 if (i->nItem > 0) goto testitem;
1287 end:
1288 return (i->nItem = i->nSpecial) != -1;
1291 static RANGE iterator_range(const ITERATOR *i)
1293 RANGE range;
1295 if (!i->ranges) return i->range;
1297 if (DPA_GetPtrCount(i->ranges->hdpa) > 0)
1299 range.lower = (*(RANGE*)DPA_GetPtr(i->ranges->hdpa, 0)).lower;
1300 range.upper = (*(RANGE*)DPA_GetPtr(i->ranges->hdpa, DPA_GetPtrCount(i->ranges->hdpa) - 1)).upper;
1302 else range.lower = range.upper = 0;
1304 return range;
1307 /***
1308 * Releases resources associated with this iterator.
1310 static inline void iterator_destroy(const ITERATOR *i)
1312 ranges_destroy(i->ranges);
1315 /***
1316 * Create an empty iterator.
1318 static inline BOOL iterator_empty(ITERATOR* i)
1320 ZeroMemory(i, sizeof(*i));
1321 i->nItem = i->nSpecial = i->range.lower = i->range.upper = -1;
1322 return TRUE;
1325 /***
1326 * Create an iterator over a range.
1328 static inline BOOL iterator_rangeitems(ITERATOR* i, RANGE range)
1330 iterator_empty(i);
1331 i->range = range;
1332 return TRUE;
1335 /***
1336 * Create an iterator over a bunch of ranges.
1337 * Please note that the iterator will take ownership of the ranges,
1338 * and will free them upon destruction.
1340 static inline BOOL iterator_rangesitems(ITERATOR* i, RANGES ranges)
1342 iterator_empty(i);
1343 i->ranges = ranges;
1344 return TRUE;
1347 /***
1348 * Creates an iterator over the items which intersect frame.
1349 * Uses absolute coordinates rather than compensating for the current offset.
1351 static BOOL iterator_frameditems_absolute(ITERATOR* i, const LISTVIEW_INFO* infoPtr, const RECT *frame)
1353 RECT rcItem, rcTemp;
1355 /* in case we fail, we want to return an empty iterator */
1356 if (!iterator_empty(i)) return FALSE;
1358 TRACE("(frame=%s)\n", wine_dbgstr_rect(frame));
1360 if (infoPtr->uView == LV_VIEW_ICON || infoPtr->uView == LV_VIEW_SMALLICON)
1362 INT nItem;
1364 if (infoPtr->uView == LV_VIEW_ICON && infoPtr->nFocusedItem != -1)
1366 LISTVIEW_GetItemBox(infoPtr, infoPtr->nFocusedItem, &rcItem);
1367 if (IntersectRect(&rcTemp, &rcItem, frame))
1368 i->nSpecial = infoPtr->nFocusedItem;
1370 if (!(iterator_rangesitems(i, ranges_create(50)))) return FALSE;
1371 /* to do better here, we need to have PosX, and PosY sorted */
1372 TRACE("building icon ranges:\n");
1373 for (nItem = 0; nItem < infoPtr->nItemCount; nItem++)
1375 rcItem.left = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosX, nItem);
1376 rcItem.top = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosY, nItem);
1377 rcItem.right = rcItem.left + infoPtr->nItemWidth;
1378 rcItem.bottom = rcItem.top + infoPtr->nItemHeight;
1379 if (IntersectRect(&rcTemp, &rcItem, frame))
1380 ranges_additem(i->ranges, nItem);
1382 return TRUE;
1384 else if (infoPtr->uView == LV_VIEW_DETAILS)
1386 RANGE range;
1388 if (frame->left >= infoPtr->nItemWidth) return TRUE;
1389 if (frame->top >= infoPtr->nItemHeight * infoPtr->nItemCount) return TRUE;
1391 range.lower = max(frame->top / infoPtr->nItemHeight, 0);
1392 range.upper = min((frame->bottom - 1) / infoPtr->nItemHeight, infoPtr->nItemCount - 1) + 1;
1393 if (range.upper <= range.lower) return TRUE;
1394 if (!iterator_rangeitems(i, range)) return FALSE;
1395 TRACE(" report=%s\n", debugrange(&i->range));
1397 else
1399 INT nPerCol = max((infoPtr->rcList.bottom - infoPtr->rcList.top) / infoPtr->nItemHeight, 1);
1400 INT nFirstRow = max(frame->top / infoPtr->nItemHeight, 0);
1401 INT nLastRow = min((frame->bottom - 1) / infoPtr->nItemHeight, nPerCol - 1);
1402 INT nFirstCol;
1403 INT nLastCol;
1404 INT lower;
1405 RANGE item_range;
1406 INT nCol;
1408 if (infoPtr->nItemWidth)
1410 nFirstCol = max(frame->left / infoPtr->nItemWidth, 0);
1411 nLastCol = min((frame->right - 1) / infoPtr->nItemWidth, (infoPtr->nItemCount + nPerCol - 1) / nPerCol);
1413 else
1415 nFirstCol = max(frame->left, 0);
1416 nLastCol = min(frame->right - 1, (infoPtr->nItemCount + nPerCol - 1) / nPerCol);
1419 lower = nFirstCol * nPerCol + nFirstRow;
1421 TRACE("nPerCol=%d, nFirstRow=%d, nLastRow=%d, nFirstCol=%d, nLastCol=%d, lower=%d\n",
1422 nPerCol, nFirstRow, nLastRow, nFirstCol, nLastCol, lower);
1424 if (nLastCol < nFirstCol || nLastRow < nFirstRow) return TRUE;
1426 if (!(iterator_rangesitems(i, ranges_create(nLastCol - nFirstCol + 1)))) return FALSE;
1427 TRACE("building list ranges:\n");
1428 for (nCol = nFirstCol; nCol <= nLastCol; nCol++)
1430 item_range.lower = nCol * nPerCol + nFirstRow;
1431 if(item_range.lower >= infoPtr->nItemCount) break;
1432 item_range.upper = min(nCol * nPerCol + nLastRow + 1, infoPtr->nItemCount);
1433 TRACE(" list=%s\n", debugrange(&item_range));
1434 ranges_add(i->ranges, item_range);
1438 return TRUE;
1441 /***
1442 * Creates an iterator over the items which intersect lprc.
1444 static BOOL iterator_frameditems(ITERATOR* i, const LISTVIEW_INFO* infoPtr, const RECT *lprc)
1446 RECT frame = *lprc;
1447 POINT Origin;
1449 TRACE("(lprc=%s)\n", wine_dbgstr_rect(lprc));
1451 LISTVIEW_GetOrigin(infoPtr, &Origin);
1452 OffsetRect(&frame, -Origin.x, -Origin.y);
1454 return iterator_frameditems_absolute(i, infoPtr, &frame);
1457 /***
1458 * Creates an iterator over the items which intersect the visible region of hdc.
1460 static BOOL iterator_visibleitems(ITERATOR *i, const LISTVIEW_INFO *infoPtr, HDC hdc)
1462 POINT Origin, Position;
1463 RECT rcItem, rcClip;
1464 INT rgntype;
1466 rgntype = GetClipBox(hdc, &rcClip);
1467 if (rgntype == NULLREGION) return iterator_empty(i);
1468 if (!iterator_frameditems(i, infoPtr, &rcClip)) return FALSE;
1469 if (rgntype == SIMPLEREGION) return TRUE;
1471 /* first deal with the special item */
1472 if (i->nSpecial != -1)
1474 LISTVIEW_GetItemBox(infoPtr, i->nSpecial, &rcItem);
1475 if (!RectVisible(hdc, &rcItem)) i->nSpecial = -1;
1478 /* if we can't deal with the region, we'll just go with the simple range */
1479 LISTVIEW_GetOrigin(infoPtr, &Origin);
1480 TRACE("building visible range:\n");
1481 if (!i->ranges && i->range.lower < i->range.upper)
1483 if (!(i->ranges = ranges_create(50))) return TRUE;
1484 if (!ranges_add(i->ranges, i->range))
1486 ranges_destroy(i->ranges);
1487 i->ranges = 0;
1488 return TRUE;
1492 /* now delete the invisible items from the list */
1493 while(iterator_next(i))
1495 LISTVIEW_GetItemOrigin(infoPtr, i->nItem, &Position);
1496 rcItem.left = (infoPtr->uView == LV_VIEW_DETAILS) ? Origin.x : Position.x + Origin.x;
1497 rcItem.top = Position.y + Origin.y;
1498 rcItem.right = rcItem.left + infoPtr->nItemWidth;
1499 rcItem.bottom = rcItem.top + infoPtr->nItemHeight;
1500 if (!RectVisible(hdc, &rcItem))
1501 ranges_delitem(i->ranges, i->nItem);
1503 /* the iterator should restart on the next iterator_next */
1504 TRACE("done\n");
1506 return TRUE;
1509 /* Remove common elements from two iterators */
1510 /* Passed iterators have to point on the first elements */
1511 static BOOL iterator_remove_common_items(ITERATOR *iter1, ITERATOR *iter2)
1513 if(!iter1->ranges || !iter2->ranges) {
1514 int lower, upper;
1516 if(iter1->ranges || iter2->ranges ||
1517 (iter1->range.lower<iter2->range.lower && iter1->range.upper>iter2->range.upper) ||
1518 (iter1->range.lower>iter2->range.lower && iter1->range.upper<iter2->range.upper)) {
1519 ERR("result is not a one range iterator\n");
1520 return FALSE;
1523 if(iter1->range.lower==-1 || iter2->range.lower==-1)
1524 return TRUE;
1526 lower = iter1->range.lower;
1527 upper = iter1->range.upper;
1529 if(lower < iter2->range.lower)
1530 iter1->range.upper = iter2->range.lower;
1531 else if(upper > iter2->range.upper)
1532 iter1->range.lower = iter2->range.upper;
1533 else
1534 iter1->range.lower = iter1->range.upper = -1;
1536 if(iter2->range.lower < lower)
1537 iter2->range.upper = lower;
1538 else if(iter2->range.upper > upper)
1539 iter2->range.lower = upper;
1540 else
1541 iter2->range.lower = iter2->range.upper = -1;
1543 return TRUE;
1546 iterator_next(iter1);
1547 iterator_next(iter2);
1549 while(1) {
1550 if(iter1->nItem==-1 || iter2->nItem==-1)
1551 break;
1553 if(iter1->nItem == iter2->nItem) {
1554 int delete = iter1->nItem;
1556 iterator_prev(iter1);
1557 iterator_prev(iter2);
1558 ranges_delitem(iter1->ranges, delete);
1559 ranges_delitem(iter2->ranges, delete);
1560 iterator_next(iter1);
1561 iterator_next(iter2);
1562 } else if(iter1->nItem > iter2->nItem)
1563 iterator_next(iter2);
1564 else
1565 iterator_next(iter1);
1568 iter1->nItem = iter1->range.lower = iter1->range.upper = -1;
1569 iter2->nItem = iter2->range.lower = iter2->range.upper = -1;
1570 return TRUE;
1573 /******** Misc helper functions ************************************/
1575 static inline LRESULT CallWindowProcT(WNDPROC proc, HWND hwnd, UINT uMsg,
1576 WPARAM wParam, LPARAM lParam, BOOL isW)
1578 if (isW) return CallWindowProcW(proc, hwnd, uMsg, wParam, lParam);
1579 else return CallWindowProcA(proc, hwnd, uMsg, wParam, lParam);
1582 static inline BOOL is_autoarrange(const LISTVIEW_INFO *infoPtr)
1584 return (infoPtr->dwStyle & LVS_AUTOARRANGE) &&
1585 (infoPtr->uView == LV_VIEW_ICON || infoPtr->uView == LV_VIEW_SMALLICON);
1588 static void toggle_checkbox_state(LISTVIEW_INFO *infoPtr, INT nItem)
1590 DWORD state = STATEIMAGEINDEX(LISTVIEW_GetItemState(infoPtr, nItem, LVIS_STATEIMAGEMASK));
1591 if(state == 1 || state == 2)
1593 LVITEMW lvitem;
1594 state ^= 3;
1595 lvitem.state = INDEXTOSTATEIMAGEMASK(state);
1596 lvitem.stateMask = LVIS_STATEIMAGEMASK;
1597 LISTVIEW_SetItemState(infoPtr, nItem, &lvitem);
1601 /* this should be called after window style got updated,
1602 it used to reset view state to match current window style */
1603 static inline void map_style_view(LISTVIEW_INFO *infoPtr)
1605 switch (infoPtr->dwStyle & LVS_TYPEMASK)
1607 case LVS_ICON:
1608 infoPtr->uView = LV_VIEW_ICON;
1609 break;
1610 case LVS_REPORT:
1611 infoPtr->uView = LV_VIEW_DETAILS;
1612 break;
1613 case LVS_SMALLICON:
1614 infoPtr->uView = LV_VIEW_SMALLICON;
1615 break;
1616 case LVS_LIST:
1617 infoPtr->uView = LV_VIEW_LIST;
1621 /* computes next item id value */
1622 static DWORD get_next_itemid(const LISTVIEW_INFO *infoPtr)
1624 INT count = DPA_GetPtrCount(infoPtr->hdpaItemIds);
1626 if (count > 0)
1628 ITEM_ID *lpID = DPA_GetPtr(infoPtr->hdpaItemIds, count - 1);
1629 return lpID->id + 1;
1631 return 0;
1634 /******** Internal API functions ************************************/
1636 static inline COLUMN_INFO * LISTVIEW_GetColumnInfo(const LISTVIEW_INFO *infoPtr, INT nSubItem)
1638 static COLUMN_INFO mainItem;
1640 if (nSubItem == 0 && DPA_GetPtrCount(infoPtr->hdpaColumns) == 0) return &mainItem;
1641 assert (nSubItem >= 0 && nSubItem < DPA_GetPtrCount(infoPtr->hdpaColumns));
1643 /* update cached column rectangles */
1644 if (infoPtr->colRectsDirty)
1646 COLUMN_INFO *info;
1647 LISTVIEW_INFO *Ptr = (LISTVIEW_INFO*)infoPtr;
1648 INT i;
1650 for (i = 0; i < DPA_GetPtrCount(infoPtr->hdpaColumns); i++) {
1651 info = DPA_GetPtr(infoPtr->hdpaColumns, i);
1652 SendMessageW(infoPtr->hwndHeader, HDM_GETITEMRECT, i, (LPARAM)&info->rcHeader);
1654 Ptr->colRectsDirty = FALSE;
1657 return DPA_GetPtr(infoPtr->hdpaColumns, nSubItem);
1660 static INT LISTVIEW_CreateHeader(LISTVIEW_INFO *infoPtr)
1662 DWORD dFlags = WS_CHILD | HDS_HORZ | HDS_FULLDRAG | HDS_DRAGDROP;
1663 HINSTANCE hInst;
1665 if (infoPtr->hwndHeader) return 0;
1667 TRACE("Creating header for list %p\n", infoPtr->hwndSelf);
1669 /* setup creation flags */
1670 dFlags |= (LVS_NOSORTHEADER & infoPtr->dwStyle) ? 0 : HDS_BUTTONS;
1671 dFlags |= (LVS_NOCOLUMNHEADER & infoPtr->dwStyle) ? HDS_HIDDEN : 0;
1673 hInst = (HINSTANCE)GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_HINSTANCE);
1675 /* create header */
1676 infoPtr->hwndHeader = CreateWindowW(WC_HEADERW, NULL, dFlags,
1677 0, 0, 0, 0, infoPtr->hwndSelf, NULL, hInst, NULL);
1678 if (!infoPtr->hwndHeader) return -1;
1680 /* set header unicode format */
1681 SendMessageW(infoPtr->hwndHeader, HDM_SETUNICODEFORMAT, TRUE, 0);
1683 /* set header font */
1684 SendMessageW(infoPtr->hwndHeader, WM_SETFONT, (WPARAM)infoPtr->hFont, TRUE);
1686 /* set header image list */
1687 if (infoPtr->himlSmall)
1688 SendMessageW(infoPtr->hwndHeader, HDM_SETIMAGELIST, 0, (LPARAM)infoPtr->himlSmall);
1690 LISTVIEW_UpdateSize(infoPtr);
1692 return 0;
1695 static inline void LISTVIEW_GetHeaderRect(const LISTVIEW_INFO *infoPtr, INT nSubItem, LPRECT lprc)
1697 *lprc = LISTVIEW_GetColumnInfo(infoPtr, nSubItem)->rcHeader;
1700 static inline BOOL LISTVIEW_IsHeaderEnabled(const LISTVIEW_INFO *infoPtr)
1702 return (infoPtr->uView == LV_VIEW_DETAILS ||
1703 infoPtr->dwLvExStyle & LVS_EX_HEADERINALLVIEWS) &&
1704 !(infoPtr->dwStyle & LVS_NOCOLUMNHEADER);
1707 static inline BOOL LISTVIEW_GetItemW(const LISTVIEW_INFO *infoPtr, LPLVITEMW lpLVItem)
1709 return LISTVIEW_GetItemT(infoPtr, lpLVItem, TRUE);
1712 /* used to handle collapse main item column case */
1713 static inline BOOL LISTVIEW_DrawFocusRect(const LISTVIEW_INFO *infoPtr, HDC hdc)
1715 return (infoPtr->rcFocus.left < infoPtr->rcFocus.right) ?
1716 DrawFocusRect(hdc, &infoPtr->rcFocus) : FALSE;
1719 /* Listview invalidation functions: use _only_ these functions to invalidate */
1721 static inline BOOL is_redrawing(const LISTVIEW_INFO *infoPtr)
1723 return infoPtr->redraw;
1726 static inline void LISTVIEW_InvalidateRect(const LISTVIEW_INFO *infoPtr, const RECT* rect)
1728 if(!is_redrawing(infoPtr)) return;
1729 TRACE(" invalidating rect=%s\n", wine_dbgstr_rect(rect));
1730 InvalidateRect(infoPtr->hwndSelf, rect, TRUE);
1733 static inline void LISTVIEW_InvalidateItem(const LISTVIEW_INFO *infoPtr, INT nItem)
1735 RECT rcBox;
1737 if (!is_redrawing(infoPtr) || nItem < 0 || nItem >= infoPtr->nItemCount)
1738 return;
1740 LISTVIEW_GetItemBox(infoPtr, nItem, &rcBox);
1741 LISTVIEW_InvalidateRect(infoPtr, &rcBox);
1744 static inline void LISTVIEW_InvalidateSubItem(const LISTVIEW_INFO *infoPtr, INT nItem, INT nSubItem)
1746 POINT Origin, Position;
1747 RECT rcBox;
1749 if(!is_redrawing(infoPtr)) return;
1750 assert (infoPtr->uView == LV_VIEW_DETAILS);
1751 LISTVIEW_GetOrigin(infoPtr, &Origin);
1752 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position);
1753 LISTVIEW_GetHeaderRect(infoPtr, nSubItem, &rcBox);
1754 rcBox.top = 0;
1755 rcBox.bottom = infoPtr->nItemHeight;
1756 OffsetRect(&rcBox, Origin.x + Position.x, Origin.y + Position.y);
1757 LISTVIEW_InvalidateRect(infoPtr, &rcBox);
1760 static inline void LISTVIEW_InvalidateList(const LISTVIEW_INFO *infoPtr)
1762 LISTVIEW_InvalidateRect(infoPtr, NULL);
1765 static inline void LISTVIEW_InvalidateColumn(const LISTVIEW_INFO *infoPtr, INT nColumn)
1767 RECT rcCol;
1769 if(!is_redrawing(infoPtr)) return;
1770 LISTVIEW_GetHeaderRect(infoPtr, nColumn, &rcCol);
1771 rcCol.top = infoPtr->rcList.top;
1772 rcCol.bottom = infoPtr->rcList.bottom;
1773 LISTVIEW_InvalidateRect(infoPtr, &rcCol);
1776 /***
1777 * DESCRIPTION:
1778 * Retrieves the number of items that can fit vertically in the client area.
1780 * PARAMETER(S):
1781 * [I] infoPtr : valid pointer to the listview structure
1783 * RETURN:
1784 * Number of items per row.
1786 static inline INT LISTVIEW_GetCountPerRow(const LISTVIEW_INFO *infoPtr)
1788 INT nListWidth = infoPtr->rcList.right - infoPtr->rcList.left;
1790 return max(nListWidth/(infoPtr->nItemWidth ? infoPtr->nItemWidth : 1), 1);
1793 /***
1794 * DESCRIPTION:
1795 * Retrieves the number of items that can fit horizontally in the client
1796 * area.
1798 * PARAMETER(S):
1799 * [I] infoPtr : valid pointer to the listview structure
1801 * RETURN:
1802 * Number of items per column.
1804 static inline INT LISTVIEW_GetCountPerColumn(const LISTVIEW_INFO *infoPtr)
1806 INT nListHeight = infoPtr->rcList.bottom - infoPtr->rcList.top;
1808 return max(nListHeight / infoPtr->nItemHeight, 1);
1812 /*************************************************************************
1813 * LISTVIEW_ProcessLetterKeys
1815 * Processes keyboard messages generated by pressing the letter keys
1816 * on the keyboard.
1817 * What this does is perform a case insensitive search from the
1818 * current position with the following quirks:
1819 * - If two chars or more are pressed in quick succession we search
1820 * for the corresponding string (e.g. 'abc').
1821 * - If there is a delay we wipe away the current search string and
1822 * restart with just that char.
1823 * - If the user keeps pressing the same character, whether slowly or
1824 * fast, so that the search string is entirely composed of this
1825 * character ('aaaaa' for instance), then we search for first item
1826 * that starting with that character.
1827 * - If the user types the above character in quick succession, then
1828 * we must also search for the corresponding string ('aaaaa'), and
1829 * go to that string if there is a match.
1831 * PARAMETERS
1832 * [I] hwnd : handle to the window
1833 * [I] charCode : the character code, the actual character
1834 * [I] keyData : key data
1836 * RETURNS
1838 * Zero.
1840 * BUGS
1842 * - The current implementation has a list of characters it will
1843 * accept and it ignores everything else. In particular it will
1844 * ignore accentuated characters which seems to match what
1845 * Windows does. But I'm not sure it makes sense to follow
1846 * Windows there.
1847 * - We don't sound a beep when the search fails.
1849 * SEE ALSO
1851 * TREEVIEW_ProcessLetterKeys
1853 static INT LISTVIEW_ProcessLetterKeys(LISTVIEW_INFO *infoPtr, WPARAM charCode, LPARAM keyData)
1855 WCHAR buffer[MAX_PATH];
1856 DWORD prevTime;
1857 LVITEMW item;
1858 int startidx;
1859 INT nItem;
1860 INT diff;
1862 /* simple parameter checking */
1863 if (!charCode || !keyData || infoPtr->nItemCount == 0) return 0;
1865 /* only allow the valid WM_CHARs through */
1866 if (!isalnumW(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 charCode != '}' && charCode != '[' && charCode != '{' &&
1874 charCode != '/' && charCode != '?' && charCode != '>' &&
1875 charCode != '<' && charCode != ',' && charCode != '~')
1876 return 0;
1878 /* update the search parameters */
1879 prevTime = infoPtr->lastKeyPressTimestamp;
1880 infoPtr->lastKeyPressTimestamp = GetTickCount();
1881 diff = infoPtr->lastKeyPressTimestamp - prevTime;
1883 if (diff >= 0 && diff < KEY_DELAY)
1885 if (infoPtr->nSearchParamLength < MAX_PATH - 1)
1886 infoPtr->szSearchParam[infoPtr->nSearchParamLength++] = charCode;
1888 if (infoPtr->charCode != charCode)
1889 infoPtr->charCode = charCode = 0;
1891 else
1893 infoPtr->charCode = charCode;
1894 infoPtr->szSearchParam[0] = charCode;
1895 infoPtr->nSearchParamLength = 1;
1898 /* should start from next after focused item, so next item that matches
1899 will be selected, if there isn't any and focused matches it will be selected
1900 on second search stage from beginning of the list */
1901 if (infoPtr->nFocusedItem >= 0 && infoPtr->nItemCount > 1)
1903 /* with some accumulated search data available start with current focus, otherwise
1904 it's excluded from search */
1905 startidx = infoPtr->nSearchParamLength > 1 ? infoPtr->nFocusedItem : infoPtr->nFocusedItem + 1;
1906 if (startidx == infoPtr->nItemCount) startidx = 0;
1908 else
1909 startidx = 0;
1911 /* let application handle this for virtual listview */
1912 if (infoPtr->dwStyle & LVS_OWNERDATA)
1914 NMLVFINDITEMW nmlv;
1916 memset(&nmlv.lvfi, 0, sizeof(nmlv.lvfi));
1917 nmlv.lvfi.flags = (LVFI_WRAP | LVFI_PARTIAL);
1918 nmlv.lvfi.psz = infoPtr->szSearchParam;
1919 nmlv.iStart = startidx;
1921 infoPtr->szSearchParam[infoPtr->nSearchParamLength] = 0;
1923 nItem = notify_hdr(infoPtr, LVN_ODFINDITEMW, (LPNMHDR)&nmlv.hdr);
1925 else
1927 int i = startidx, endidx;
1929 /* and search from the current position */
1930 nItem = -1;
1931 endidx = infoPtr->nItemCount;
1933 /* first search in [startidx, endidx), on failure continue in [0, startidx) */
1934 while (1)
1936 /* start from first item if not found with >= startidx */
1937 if (i == infoPtr->nItemCount && startidx > 0)
1939 endidx = startidx;
1940 startidx = 0;
1943 for (i = startidx; i < endidx; i++)
1945 /* retrieve text */
1946 item.mask = LVIF_TEXT;
1947 item.iItem = i;
1948 item.iSubItem = 0;
1949 item.pszText = buffer;
1950 item.cchTextMax = MAX_PATH;
1951 if (!LISTVIEW_GetItemW(infoPtr, &item)) return 0;
1953 if (!lstrncmpiW(item.pszText, infoPtr->szSearchParam, infoPtr->nSearchParamLength))
1955 nItem = i;
1956 break;
1958 /* this is used to find first char match when search string is not available yet,
1959 otherwise every WM_CHAR will search to next item by first char, ignoring that we're
1960 already waiting for user to complete a string */
1961 else if (nItem == -1 && infoPtr->nSearchParamLength == 1 && !lstrncmpiW(item.pszText, infoPtr->szSearchParam, 1))
1963 /* this would work but we must keep looking for a longer match */
1964 nItem = i;
1968 if ( nItem != -1 || /* found something */
1969 endidx != infoPtr->nItemCount || /* second search done */
1970 (startidx == 0 && endidx == infoPtr->nItemCount) /* full range for first search */ )
1971 break;
1975 if (nItem != -1)
1976 LISTVIEW_KeySelection(infoPtr, nItem, FALSE);
1978 return 0;
1981 /*************************************************************************
1982 * LISTVIEW_UpdateHeaderSize [Internal]
1984 * Function to resize the header control
1986 * PARAMS
1987 * [I] hwnd : handle to a window
1988 * [I] nNewScrollPos : scroll pos to set
1990 * RETURNS
1991 * None.
1993 static void LISTVIEW_UpdateHeaderSize(const LISTVIEW_INFO *infoPtr, INT nNewScrollPos)
1995 RECT winRect;
1996 POINT point[2];
1998 TRACE("nNewScrollPos=%d\n", nNewScrollPos);
2000 if (!infoPtr->hwndHeader) return;
2002 GetWindowRect(infoPtr->hwndHeader, &winRect);
2003 point[0].x = winRect.left;
2004 point[0].y = winRect.top;
2005 point[1].x = winRect.right;
2006 point[1].y = winRect.bottom;
2008 MapWindowPoints(HWND_DESKTOP, infoPtr->hwndSelf, point, 2);
2009 point[0].x = -nNewScrollPos;
2010 point[1].x += nNewScrollPos;
2012 SetWindowPos(infoPtr->hwndHeader,0,
2013 point[0].x,point[0].y,point[1].x,point[1].y,
2014 (infoPtr->dwStyle & LVS_NOCOLUMNHEADER) ? SWP_HIDEWINDOW : SWP_SHOWWINDOW |
2015 SWP_NOZORDER | SWP_NOACTIVATE);
2018 static INT LISTVIEW_UpdateHScroll(LISTVIEW_INFO *infoPtr)
2020 SCROLLINFO horzInfo;
2021 INT dx;
2023 ZeroMemory(&horzInfo, sizeof(SCROLLINFO));
2024 horzInfo.cbSize = sizeof(SCROLLINFO);
2025 horzInfo.nPage = infoPtr->rcList.right - infoPtr->rcList.left;
2027 /* for now, we'll set info.nMax to the _count_, and adjust it later */
2028 if (infoPtr->uView == LV_VIEW_LIST)
2030 INT nPerCol = LISTVIEW_GetCountPerColumn(infoPtr);
2031 horzInfo.nMax = (infoPtr->nItemCount + nPerCol - 1) / nPerCol;
2033 /* scroll by at least one column per page */
2034 if(horzInfo.nPage < infoPtr->nItemWidth)
2035 horzInfo.nPage = infoPtr->nItemWidth;
2037 if (infoPtr->nItemWidth)
2038 horzInfo.nPage /= infoPtr->nItemWidth;
2040 else if (infoPtr->uView == LV_VIEW_DETAILS)
2042 horzInfo.nMax = infoPtr->nItemWidth;
2044 else /* LV_VIEW_ICON, or LV_VIEW_SMALLICON */
2046 RECT rcView;
2048 if (LISTVIEW_GetViewRect(infoPtr, &rcView)) horzInfo.nMax = rcView.right - rcView.left;
2051 if (LISTVIEW_IsHeaderEnabled(infoPtr))
2053 if (DPA_GetPtrCount(infoPtr->hdpaColumns))
2055 RECT rcHeader;
2056 INT index;
2058 index = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX,
2059 DPA_GetPtrCount(infoPtr->hdpaColumns) - 1, 0);
2061 LISTVIEW_GetHeaderRect(infoPtr, index, &rcHeader);
2062 horzInfo.nMax = rcHeader.right;
2063 TRACE("horzInfo.nMax=%d\n", horzInfo.nMax);
2067 horzInfo.fMask = SIF_RANGE | SIF_PAGE;
2068 horzInfo.nMax = max(horzInfo.nMax - 1, 0);
2069 dx = GetScrollPos(infoPtr->hwndSelf, SB_HORZ);
2070 dx -= SetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &horzInfo, TRUE);
2071 TRACE("horzInfo=%s\n", debugscrollinfo(&horzInfo));
2073 /* Update the Header Control */
2074 if (infoPtr->hwndHeader)
2076 horzInfo.fMask = SIF_POS;
2077 GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &horzInfo);
2078 LISTVIEW_UpdateHeaderSize(infoPtr, horzInfo.nPos);
2081 LISTVIEW_UpdateSize(infoPtr);
2082 return dx;
2085 static INT LISTVIEW_UpdateVScroll(LISTVIEW_INFO *infoPtr)
2087 SCROLLINFO vertInfo;
2088 INT dy;
2090 ZeroMemory(&vertInfo, sizeof(SCROLLINFO));
2091 vertInfo.cbSize = sizeof(SCROLLINFO);
2092 vertInfo.nPage = infoPtr->rcList.bottom - infoPtr->rcList.top;
2094 if (infoPtr->uView == LV_VIEW_DETAILS)
2096 vertInfo.nMax = infoPtr->nItemCount;
2098 /* scroll by at least one page */
2099 if(vertInfo.nPage < infoPtr->nItemHeight)
2100 vertInfo.nPage = infoPtr->nItemHeight;
2102 if (infoPtr->nItemHeight > 0)
2103 vertInfo.nPage /= infoPtr->nItemHeight;
2105 else if (infoPtr->uView != LV_VIEW_LIST) /* LV_VIEW_ICON, or LV_VIEW_SMALLICON */
2107 RECT rcView;
2109 if (LISTVIEW_GetViewRect(infoPtr, &rcView)) vertInfo.nMax = rcView.bottom - rcView.top;
2112 vertInfo.fMask = SIF_RANGE | SIF_PAGE;
2113 vertInfo.nMax = max(vertInfo.nMax - 1, 0);
2114 dy = GetScrollPos(infoPtr->hwndSelf, SB_VERT);
2115 dy -= SetScrollInfo(infoPtr->hwndSelf, SB_VERT, &vertInfo, TRUE);
2116 TRACE("vertInfo=%s\n", debugscrollinfo(&vertInfo));
2118 LISTVIEW_UpdateSize(infoPtr);
2119 return dy;
2122 /***
2123 * DESCRIPTION:
2124 * Update the scrollbars. This function should be called whenever
2125 * the content, size or view changes.
2127 * PARAMETER(S):
2128 * [I] infoPtr : valid pointer to the listview structure
2130 * RETURN:
2131 * None
2133 static void LISTVIEW_UpdateScroll(LISTVIEW_INFO *infoPtr)
2135 INT dx, dy, pass;
2137 if ((infoPtr->dwStyle & LVS_NOSCROLL) || !is_redrawing(infoPtr)) return;
2139 /* Setting the horizontal scroll can change the listview size
2140 * (and potentially everything else) so we need to recompute
2141 * everything again for the vertical scroll and vice-versa
2143 for (dx = 0, dy = 0, pass = 0; pass <= 1; pass++)
2145 dx += LISTVIEW_UpdateHScroll(infoPtr);
2146 dy += LISTVIEW_UpdateVScroll(infoPtr);
2149 /* Change of the range may have changed the scroll pos. If so move the content */
2150 if (dx != 0 || dy != 0)
2152 RECT listRect;
2153 listRect = infoPtr->rcList;
2154 ScrollWindowEx(infoPtr->hwndSelf, dx, dy, &listRect, &listRect, 0, 0,
2155 SW_ERASE | SW_INVALIDATE);
2160 /***
2161 * DESCRIPTION:
2162 * Shows/hides the focus rectangle.
2164 * PARAMETER(S):
2165 * [I] infoPtr : valid pointer to the listview structure
2166 * [I] fShow : TRUE to show the focus, FALSE to hide it.
2168 * RETURN:
2169 * None
2171 static void LISTVIEW_ShowFocusRect(const LISTVIEW_INFO *infoPtr, BOOL fShow)
2173 HDC hdc;
2175 TRACE("fShow=%d, nItem=%d\n", fShow, infoPtr->nFocusedItem);
2177 if (infoPtr->nFocusedItem < 0) return;
2179 /* we need some gymnastics in ICON mode to handle large items */
2180 if (infoPtr->uView == LV_VIEW_ICON)
2182 RECT rcBox;
2184 LISTVIEW_GetItemBox(infoPtr, infoPtr->nFocusedItem, &rcBox);
2185 if ((rcBox.bottom - rcBox.top) > infoPtr->nItemHeight)
2187 LISTVIEW_InvalidateRect(infoPtr, &rcBox);
2188 return;
2192 if (!(hdc = GetDC(infoPtr->hwndSelf))) return;
2194 /* for some reason, owner draw should work only in report mode */
2195 if ((infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && (infoPtr->uView == LV_VIEW_DETAILS))
2197 DRAWITEMSTRUCT dis;
2198 LVITEMW item;
2200 HFONT hFont = infoPtr->hFont ? infoPtr->hFont : infoPtr->hDefaultFont;
2201 HFONT hOldFont = SelectObject(hdc, hFont);
2203 item.iItem = infoPtr->nFocusedItem;
2204 item.iSubItem = 0;
2205 item.mask = LVIF_PARAM;
2206 if (!LISTVIEW_GetItemW(infoPtr, &item)) goto done;
2208 ZeroMemory(&dis, sizeof(dis));
2209 dis.CtlType = ODT_LISTVIEW;
2210 dis.CtlID = (UINT)GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
2211 dis.itemID = item.iItem;
2212 dis.itemAction = ODA_FOCUS;
2213 if (fShow) dis.itemState |= ODS_FOCUS;
2214 dis.hwndItem = infoPtr->hwndSelf;
2215 dis.hDC = hdc;
2216 LISTVIEW_GetItemBox(infoPtr, dis.itemID, &dis.rcItem);
2217 dis.itemData = item.lParam;
2219 SendMessageW(infoPtr->hwndNotify, WM_DRAWITEM, dis.CtlID, (LPARAM)&dis);
2221 SelectObject(hdc, hOldFont);
2223 else
2224 LISTVIEW_InvalidateItem(infoPtr, infoPtr->nFocusedItem);
2226 done:
2227 ReleaseDC(infoPtr->hwndSelf, hdc);
2230 /***
2231 * Invalidates all visible selected items.
2233 static void LISTVIEW_InvalidateSelectedItems(const LISTVIEW_INFO *infoPtr)
2235 ITERATOR i;
2237 iterator_frameditems(&i, infoPtr, &infoPtr->rcList);
2238 while(iterator_next(&i))
2240 if (LISTVIEW_GetItemState(infoPtr, i.nItem, LVIS_SELECTED))
2241 LISTVIEW_InvalidateItem(infoPtr, i.nItem);
2243 iterator_destroy(&i);
2247 /***
2248 * DESCRIPTION: [INTERNAL]
2249 * Computes an item's (left,top) corner, relative to rcView.
2250 * That is, the position has NOT been made relative to the Origin.
2251 * This is deliberate, to avoid computing the Origin over, and
2252 * over again, when this function is called in a loop. Instead,
2253 * one can factor the computation of the Origin before the loop,
2254 * and offset the value returned by this function, on every iteration.
2256 * PARAMETER(S):
2257 * [I] infoPtr : valid pointer to the listview structure
2258 * [I] nItem : item number
2259 * [O] lpptOrig : item top, left corner
2261 * RETURN:
2262 * None.
2264 static void LISTVIEW_GetItemOrigin(const LISTVIEW_INFO *infoPtr, INT nItem, LPPOINT lpptPosition)
2266 assert(nItem >= 0 && nItem < infoPtr->nItemCount);
2268 if ((infoPtr->uView == LV_VIEW_SMALLICON) || (infoPtr->uView == LV_VIEW_ICON))
2270 lpptPosition->x = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosX, nItem);
2271 lpptPosition->y = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosY, nItem);
2273 else if (infoPtr->uView == LV_VIEW_LIST)
2275 INT nCountPerColumn = LISTVIEW_GetCountPerColumn(infoPtr);
2276 lpptPosition->x = nItem / nCountPerColumn * infoPtr->nItemWidth;
2277 lpptPosition->y = nItem % nCountPerColumn * infoPtr->nItemHeight;
2279 else /* LV_VIEW_DETAILS */
2281 lpptPosition->x = REPORT_MARGINX;
2282 /* item is always at zero indexed column */
2283 if (DPA_GetPtrCount(infoPtr->hdpaColumns) > 0)
2284 lpptPosition->x += LISTVIEW_GetColumnInfo(infoPtr, 0)->rcHeader.left;
2285 lpptPosition->y = nItem * infoPtr->nItemHeight;
2289 /***
2290 * DESCRIPTION: [INTERNAL]
2291 * Compute the rectangles of an item. This is to localize all
2292 * the computations in one place. If you are not interested in some
2293 * of these values, simply pass in a NULL -- the function is smart
2294 * enough to compute only what's necessary. The function computes
2295 * the standard rectangles (BOUNDS, ICON, LABEL) plus a non-standard
2296 * one, the BOX rectangle. This rectangle is very cheap to compute,
2297 * and is guaranteed to contain all the other rectangles. Computing
2298 * the ICON rect is also cheap, but all the others are potentially
2299 * expensive. This gives an easy and effective optimization when
2300 * searching (like point inclusion, or rectangle intersection):
2301 * first test against the BOX, and if TRUE, test against the desired
2302 * rectangle.
2303 * If the function does not have all the necessary information
2304 * to computed the requested rectangles, will crash with a
2305 * failed assertion. This is done so we catch all programming
2306 * errors, given that the function is called only from our code.
2308 * We have the following 'special' meanings for a few fields:
2309 * * If LVIS_FOCUSED is set, we assume the item has the focus
2310 * This is important in ICON mode, where it might get a larger
2311 * then usual rectangle
2313 * Please note that subitem support works only in REPORT mode.
2315 * PARAMETER(S):
2316 * [I] infoPtr : valid pointer to the listview structure
2317 * [I] lpLVItem : item to compute the measures for
2318 * [O] lprcBox : ptr to Box rectangle
2319 * Same as LVM_GETITEMRECT with LVIR_BOUNDS
2320 * [0] lprcSelectBox : ptr to select box rectangle
2321 * Same as LVM_GETITEMRECT with LVIR_SELECTEDBOUNDS
2322 * [O] lprcIcon : ptr to Icon rectangle
2323 * Same as LVM_GETITEMRECT with LVIR_ICON
2324 * [O] lprcStateIcon: ptr to State Icon rectangle
2325 * [O] lprcLabel : ptr to Label rectangle
2326 * Same as LVM_GETITEMRECT with LVIR_LABEL
2328 * RETURN:
2329 * None.
2331 static void LISTVIEW_GetItemMetrics(const LISTVIEW_INFO *infoPtr, const LVITEMW *lpLVItem,
2332 LPRECT lprcBox, LPRECT lprcSelectBox,
2333 LPRECT lprcIcon, LPRECT lprcStateIcon, LPRECT lprcLabel)
2335 BOOL doSelectBox = FALSE, doIcon = FALSE, doLabel = FALSE, oversizedBox = FALSE;
2336 RECT Box, SelectBox, Icon, Label;
2337 COLUMN_INFO *lpColumnInfo = NULL;
2338 SIZE labelSize = { 0, 0 };
2340 TRACE("(lpLVItem=%s)\n", debuglvitem_t(lpLVItem, TRUE));
2342 /* Be smart and try to figure out the minimum we have to do */
2343 if (lpLVItem->iSubItem) assert(infoPtr->uView == LV_VIEW_DETAILS);
2344 if (infoPtr->uView == LV_VIEW_ICON && (lprcBox || lprcLabel))
2346 assert((lpLVItem->mask & LVIF_STATE) && (lpLVItem->stateMask & LVIS_FOCUSED));
2347 if (lpLVItem->state & LVIS_FOCUSED) oversizedBox = doLabel = TRUE;
2349 if (lprcSelectBox) doSelectBox = TRUE;
2350 if (lprcLabel) doLabel = TRUE;
2351 if (doLabel || lprcIcon || lprcStateIcon) doIcon = TRUE;
2352 if (doSelectBox)
2354 doIcon = TRUE;
2355 doLabel = TRUE;
2358 /************************************************************/
2359 /* compute the box rectangle (it should be cheap to do) */
2360 /************************************************************/
2361 if (lpLVItem->iSubItem || infoPtr->uView == LV_VIEW_DETAILS)
2362 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, lpLVItem->iSubItem);
2364 if (lpLVItem->iSubItem)
2366 Box = lpColumnInfo->rcHeader;
2368 else
2370 Box.left = 0;
2371 Box.right = infoPtr->nItemWidth;
2373 Box.top = 0;
2374 Box.bottom = infoPtr->nItemHeight;
2376 /******************************************************************/
2377 /* compute ICON bounding box (ala LVM_GETITEMRECT) and STATEICON */
2378 /******************************************************************/
2379 if (doIcon)
2381 LONG state_width = 0;
2383 if (infoPtr->himlState && lpLVItem->iSubItem == 0)
2384 state_width = infoPtr->iconStateSize.cx;
2386 if (infoPtr->uView == LV_VIEW_ICON)
2388 Icon.left = Box.left + state_width;
2389 if (infoPtr->himlNormal)
2390 Icon.left += (infoPtr->nItemWidth - infoPtr->iconSize.cx - state_width) / 2;
2391 Icon.top = Box.top + ICON_TOP_PADDING;
2392 Icon.right = Icon.left;
2393 Icon.bottom = Icon.top;
2394 if (infoPtr->himlNormal)
2396 Icon.right += infoPtr->iconSize.cx;
2397 Icon.bottom += infoPtr->iconSize.cy;
2400 else /* LV_VIEW_SMALLICON, LV_VIEW_LIST or LV_VIEW_DETAILS */
2402 Icon.left = Box.left + state_width;
2404 if (infoPtr->uView == LV_VIEW_DETAILS && lpLVItem->iSubItem == 0)
2406 /* we need the indent in report mode */
2407 assert(lpLVItem->mask & LVIF_INDENT);
2408 Icon.left += infoPtr->iconSize.cx * lpLVItem->iIndent + REPORT_MARGINX;
2411 Icon.top = Box.top;
2412 Icon.right = Icon.left;
2413 if (infoPtr->himlSmall &&
2414 (!lpColumnInfo || lpLVItem->iSubItem == 0 ||
2415 ((infoPtr->dwLvExStyle & LVS_EX_SUBITEMIMAGES) && lpLVItem->iImage != I_IMAGECALLBACK)))
2416 Icon.right += infoPtr->iconSize.cx;
2417 Icon.bottom = Icon.top + infoPtr->iconSize.cy;
2419 if(lprcIcon) *lprcIcon = Icon;
2420 TRACE(" - icon=%s\n", wine_dbgstr_rect(&Icon));
2422 /* TODO: is this correct? */
2423 if (lprcStateIcon)
2425 lprcStateIcon->left = Icon.left - state_width;
2426 lprcStateIcon->right = Icon.left;
2427 lprcStateIcon->top = Icon.top;
2428 lprcStateIcon->bottom = lprcStateIcon->top + infoPtr->iconSize.cy;
2429 TRACE(" - state icon=%s\n", wine_dbgstr_rect(lprcStateIcon));
2432 else Icon.right = 0;
2434 /************************************************************/
2435 /* compute LABEL bounding box (ala LVM_GETITEMRECT) */
2436 /************************************************************/
2437 if (doLabel)
2439 /* calculate how far to the right can the label stretch */
2440 Label.right = Box.right;
2441 if (infoPtr->uView == LV_VIEW_DETAILS)
2443 if (lpLVItem->iSubItem == 0)
2445 /* we need a zero based rect here */
2446 Label = lpColumnInfo->rcHeader;
2447 OffsetRect(&Label, -Label.left, 0);
2451 if (lpLVItem->iSubItem || ((infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && infoPtr->uView == LV_VIEW_DETAILS))
2453 labelSize.cx = infoPtr->nItemWidth;
2454 labelSize.cy = infoPtr->nItemHeight;
2455 goto calc_label;
2458 /* we need the text in non owner draw mode */
2459 assert(lpLVItem->mask & LVIF_TEXT);
2460 if (is_text(lpLVItem->pszText))
2462 HFONT hFont = infoPtr->hFont ? infoPtr->hFont : infoPtr->hDefaultFont;
2463 HDC hdc = GetDC(infoPtr->hwndSelf);
2464 HFONT hOldFont = SelectObject(hdc, hFont);
2465 UINT uFormat;
2466 RECT rcText;
2468 /* compute rough rectangle where the label will go */
2469 SetRectEmpty(&rcText);
2470 rcText.right = infoPtr->nItemWidth - TRAILING_LABEL_PADDING;
2471 rcText.bottom = infoPtr->nItemHeight;
2472 if (infoPtr->uView == LV_VIEW_ICON)
2473 rcText.bottom -= ICON_TOP_PADDING + infoPtr->iconSize.cy + ICON_BOTTOM_PADDING;
2475 /* now figure out the flags */
2476 if (infoPtr->uView == LV_VIEW_ICON)
2477 uFormat = oversizedBox ? LV_FL_DT_FLAGS : LV_ML_DT_FLAGS;
2478 else
2479 uFormat = LV_SL_DT_FLAGS;
2481 DrawTextW (hdc, lpLVItem->pszText, -1, &rcText, uFormat | DT_CALCRECT);
2483 if (rcText.right != rcText.left)
2484 labelSize.cx = min(rcText.right - rcText.left + TRAILING_LABEL_PADDING, infoPtr->nItemWidth);
2486 labelSize.cy = rcText.bottom - rcText.top;
2488 SelectObject(hdc, hOldFont);
2489 ReleaseDC(infoPtr->hwndSelf, hdc);
2492 calc_label:
2493 if (infoPtr->uView == LV_VIEW_ICON)
2495 Label.left = Box.left + (infoPtr->nItemWidth - labelSize.cx) / 2;
2496 Label.top = Box.top + ICON_TOP_PADDING_HITABLE +
2497 infoPtr->iconSize.cy + ICON_BOTTOM_PADDING;
2498 Label.right = Label.left + labelSize.cx;
2499 Label.bottom = Label.top + infoPtr->nItemHeight;
2500 if (!oversizedBox && labelSize.cy > infoPtr->ntmHeight)
2502 labelSize.cy = min(Box.bottom - Label.top, labelSize.cy);
2503 labelSize.cy /= infoPtr->ntmHeight;
2504 labelSize.cy = max(labelSize.cy, 1);
2505 labelSize.cy *= infoPtr->ntmHeight;
2507 Label.bottom = Label.top + labelSize.cy + HEIGHT_PADDING;
2509 else if (infoPtr->uView == LV_VIEW_DETAILS)
2511 Label.left = Icon.right;
2512 Label.top = Box.top;
2513 Label.right = lpLVItem->iSubItem ? lpColumnInfo->rcHeader.right :
2514 lpColumnInfo->rcHeader.right - lpColumnInfo->rcHeader.left;
2515 Label.bottom = Label.top + infoPtr->nItemHeight;
2517 else /* LV_VIEW_SMALLICON or LV_VIEW_LIST */
2519 Label.left = Icon.right;
2520 Label.top = Box.top;
2521 Label.right = min(Label.left + labelSize.cx, Label.right);
2522 Label.bottom = Label.top + infoPtr->nItemHeight;
2525 if (lprcLabel) *lprcLabel = Label;
2526 TRACE(" - label=%s\n", wine_dbgstr_rect(&Label));
2529 /************************************************************/
2530 /* compute SELECT bounding box */
2531 /************************************************************/
2532 if (doSelectBox)
2534 if (infoPtr->uView == LV_VIEW_DETAILS)
2536 SelectBox.left = Icon.left;
2537 SelectBox.top = Box.top;
2538 SelectBox.bottom = Box.bottom;
2540 if (labelSize.cx)
2541 SelectBox.right = min(Label.left + labelSize.cx, Label.right);
2542 else
2543 SelectBox.right = min(Label.left + MAX_EMPTYTEXT_SELECT_WIDTH, Label.right);
2545 else
2547 UnionRect(&SelectBox, &Icon, &Label);
2549 if (lprcSelectBox) *lprcSelectBox = SelectBox;
2550 TRACE(" - select box=%s\n", wine_dbgstr_rect(&SelectBox));
2553 /* Fix the Box if necessary */
2554 if (lprcBox)
2556 if (oversizedBox) UnionRect(lprcBox, &Box, &Label);
2557 else *lprcBox = Box;
2559 TRACE(" - box=%s\n", wine_dbgstr_rect(&Box));
2562 /***
2563 * DESCRIPTION: [INTERNAL]
2565 * PARAMETER(S):
2566 * [I] infoPtr : valid pointer to the listview structure
2567 * [I] nItem : item number
2568 * [O] lprcBox : ptr to Box rectangle
2570 * RETURN:
2571 * None.
2573 static void LISTVIEW_GetItemBox(const LISTVIEW_INFO *infoPtr, INT nItem, LPRECT lprcBox)
2575 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
2576 POINT Position, Origin;
2577 LVITEMW lvItem;
2579 LISTVIEW_GetOrigin(infoPtr, &Origin);
2580 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position);
2582 /* Be smart and try to figure out the minimum we have to do */
2583 lvItem.mask = 0;
2584 if (infoPtr->uView == LV_VIEW_ICON && infoPtr->bFocus && LISTVIEW_GetItemState(infoPtr, nItem, LVIS_FOCUSED))
2585 lvItem.mask |= LVIF_TEXT;
2586 lvItem.iItem = nItem;
2587 lvItem.iSubItem = 0;
2588 lvItem.pszText = szDispText;
2589 lvItem.cchTextMax = DISP_TEXT_SIZE;
2590 if (lvItem.mask) LISTVIEW_GetItemW(infoPtr, &lvItem);
2591 if (infoPtr->uView == LV_VIEW_ICON)
2593 lvItem.mask |= LVIF_STATE;
2594 lvItem.stateMask = LVIS_FOCUSED;
2595 lvItem.state = (lvItem.mask & LVIF_TEXT ? LVIS_FOCUSED : 0);
2597 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, lprcBox, 0, 0, 0, 0);
2599 if (infoPtr->uView == LV_VIEW_DETAILS && infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT &&
2600 SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX, 0, 0))
2602 OffsetRect(lprcBox, Origin.x, Position.y + Origin.y);
2604 else
2605 OffsetRect(lprcBox, Position.x + Origin.x, Position.y + Origin.y);
2608 /* LISTVIEW_MapIdToIndex helper */
2609 static INT CALLBACK MapIdSearchCompare(LPVOID p1, LPVOID p2, LPARAM lParam)
2611 ITEM_ID *id1 = (ITEM_ID*)p1;
2612 ITEM_ID *id2 = (ITEM_ID*)p2;
2614 if (id1->id == id2->id) return 0;
2616 return (id1->id < id2->id) ? -1 : 1;
2619 /***
2620 * DESCRIPTION:
2621 * Returns the item index for id specified.
2623 * PARAMETER(S):
2624 * [I] infoPtr : valid pointer to the listview structure
2625 * [I] iID : item id to get index for
2627 * RETURN:
2628 * Item index, or -1 on failure.
2630 static INT LISTVIEW_MapIdToIndex(const LISTVIEW_INFO *infoPtr, UINT iID)
2632 ITEM_ID ID;
2633 INT index;
2635 TRACE("iID=%d\n", iID);
2637 if (infoPtr->dwStyle & LVS_OWNERDATA) return -1;
2638 if (infoPtr->nItemCount == 0) return -1;
2640 ID.id = iID;
2641 index = DPA_Search(infoPtr->hdpaItemIds, &ID, -1, MapIdSearchCompare, 0, DPAS_SORTED);
2643 if (index != -1)
2645 ITEM_ID *lpID = DPA_GetPtr(infoPtr->hdpaItemIds, index);
2646 return DPA_GetPtrIndex(infoPtr->hdpaItems, lpID->item);
2649 return -1;
2652 /***
2653 * DESCRIPTION:
2654 * Returns the item id for index given.
2656 * PARAMETER(S):
2657 * [I] infoPtr : valid pointer to the listview structure
2658 * [I] iItem : item index to get id for
2660 * RETURN:
2661 * Item id.
2663 static DWORD LISTVIEW_MapIndexToId(const LISTVIEW_INFO *infoPtr, INT iItem)
2665 ITEM_INFO *lpItem;
2666 HDPA hdpaSubItems;
2668 TRACE("iItem=%d\n", iItem);
2670 if (infoPtr->dwStyle & LVS_OWNERDATA) return -1;
2671 if (iItem < 0 || iItem >= infoPtr->nItemCount) return -1;
2673 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, iItem);
2674 lpItem = DPA_GetPtr(hdpaSubItems, 0);
2676 return lpItem->id->id;
2679 /***
2680 * DESCRIPTION:
2681 * Returns the current icon position, and advances it along the top.
2682 * The returned position is not offset by Origin.
2684 * PARAMETER(S):
2685 * [I] infoPtr : valid pointer to the listview structure
2686 * [O] lpPos : will get the current icon position
2688 * RETURN:
2689 * None
2691 static void LISTVIEW_NextIconPosTop(LISTVIEW_INFO *infoPtr, LPPOINT lpPos)
2693 INT nListWidth = infoPtr->rcList.right - infoPtr->rcList.left;
2695 *lpPos = infoPtr->currIconPos;
2697 infoPtr->currIconPos.x += infoPtr->nItemWidth;
2698 if (infoPtr->currIconPos.x + infoPtr->nItemWidth <= nListWidth) return;
2700 infoPtr->currIconPos.x = 0;
2701 infoPtr->currIconPos.y += infoPtr->nItemHeight;
2705 /***
2706 * DESCRIPTION:
2707 * Returns the current icon position, and advances it down the left edge.
2708 * The returned position is not offset by Origin.
2710 * PARAMETER(S):
2711 * [I] infoPtr : valid pointer to the listview structure
2712 * [O] lpPos : will get the current icon position
2714 * RETURN:
2715 * None
2717 static void LISTVIEW_NextIconPosLeft(LISTVIEW_INFO *infoPtr, LPPOINT lpPos)
2719 INT nListHeight = infoPtr->rcList.bottom - infoPtr->rcList.top;
2721 *lpPos = infoPtr->currIconPos;
2723 infoPtr->currIconPos.y += infoPtr->nItemHeight;
2724 if (infoPtr->currIconPos.y + infoPtr->nItemHeight <= nListHeight) return;
2726 infoPtr->currIconPos.x += infoPtr->nItemWidth;
2727 infoPtr->currIconPos.y = 0;
2731 /***
2732 * DESCRIPTION:
2733 * Moves an icon to the specified position.
2734 * It takes care of invalidating the item, etc.
2736 * PARAMETER(S):
2737 * [I] infoPtr : valid pointer to the listview structure
2738 * [I] nItem : the item to move
2739 * [I] lpPos : the new icon position
2740 * [I] isNew : flags the item as being new
2742 * RETURN:
2743 * Success: TRUE
2744 * Failure: FALSE
2746 static BOOL LISTVIEW_MoveIconTo(const LISTVIEW_INFO *infoPtr, INT nItem, const POINT *lppt, BOOL isNew)
2748 POINT old;
2750 if (!isNew)
2752 old.x = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosX, nItem);
2753 old.y = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosY, nItem);
2755 if (lppt->x == old.x && lppt->y == old.y) return TRUE;
2756 LISTVIEW_InvalidateItem(infoPtr, nItem);
2759 /* Allocating a POINTER for every item is too resource intensive,
2760 * so we'll keep the (x,y) in different arrays */
2761 if (!DPA_SetPtr(infoPtr->hdpaPosX, nItem, (void *)(LONG_PTR)lppt->x)) return FALSE;
2762 if (!DPA_SetPtr(infoPtr->hdpaPosY, nItem, (void *)(LONG_PTR)lppt->y)) return FALSE;
2764 LISTVIEW_InvalidateItem(infoPtr, nItem);
2766 return TRUE;
2769 /***
2770 * DESCRIPTION:
2771 * Arranges listview items in icon display mode.
2773 * PARAMETER(S):
2774 * [I] infoPtr : valid pointer to the listview structure
2775 * [I] nAlignCode : alignment code
2777 * RETURN:
2778 * SUCCESS : TRUE
2779 * FAILURE : FALSE
2781 static BOOL LISTVIEW_Arrange(LISTVIEW_INFO *infoPtr, INT nAlignCode)
2783 void (*next_pos)(LISTVIEW_INFO *, LPPOINT);
2784 POINT pos;
2785 INT i;
2787 if (infoPtr->uView != LV_VIEW_ICON && infoPtr->uView != LV_VIEW_SMALLICON) return FALSE;
2789 TRACE("nAlignCode=%d\n", nAlignCode);
2791 if (nAlignCode == LVA_DEFAULT)
2793 if (infoPtr->dwStyle & LVS_ALIGNLEFT) nAlignCode = LVA_ALIGNLEFT;
2794 else nAlignCode = LVA_ALIGNTOP;
2797 switch (nAlignCode)
2799 case LVA_ALIGNLEFT: next_pos = LISTVIEW_NextIconPosLeft; break;
2800 case LVA_ALIGNTOP: next_pos = LISTVIEW_NextIconPosTop; break;
2801 case LVA_SNAPTOGRID: next_pos = LISTVIEW_NextIconPosTop; break; /* FIXME */
2802 default: return FALSE;
2805 infoPtr->currIconPos.x = infoPtr->currIconPos.y = 0;
2806 for (i = 0; i < infoPtr->nItemCount; i++)
2808 next_pos(infoPtr, &pos);
2809 LISTVIEW_MoveIconTo(infoPtr, i, &pos, FALSE);
2812 return TRUE;
2815 /***
2816 * DESCRIPTION:
2817 * Retrieves the bounding rectangle of all the items, not offset by Origin.
2818 * For LVS_REPORT always returns empty rectangle.
2820 * PARAMETER(S):
2821 * [I] infoPtr : valid pointer to the listview structure
2822 * [O] lprcView : bounding rectangle
2824 * RETURN:
2825 * SUCCESS : TRUE
2826 * FAILURE : FALSE
2828 static void LISTVIEW_GetAreaRect(const LISTVIEW_INFO *infoPtr, LPRECT lprcView)
2830 INT i, x, y;
2832 SetRectEmpty(lprcView);
2834 switch (infoPtr->uView)
2836 case LV_VIEW_ICON:
2837 case LV_VIEW_SMALLICON:
2838 for (i = 0; i < infoPtr->nItemCount; i++)
2840 x = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosX, i);
2841 y = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosY, i);
2842 lprcView->right = max(lprcView->right, x);
2843 lprcView->bottom = max(lprcView->bottom, y);
2845 if (infoPtr->nItemCount > 0)
2847 lprcView->right += infoPtr->nItemWidth;
2848 lprcView->bottom += infoPtr->nItemHeight;
2850 break;
2852 case LV_VIEW_LIST:
2853 y = LISTVIEW_GetCountPerColumn(infoPtr);
2854 x = infoPtr->nItemCount / y;
2855 if (infoPtr->nItemCount % y) x++;
2856 lprcView->right = x * infoPtr->nItemWidth;
2857 lprcView->bottom = y * infoPtr->nItemHeight;
2858 break;
2862 /***
2863 * DESCRIPTION:
2864 * Retrieves the bounding rectangle of all the items.
2866 * PARAMETER(S):
2867 * [I] infoPtr : valid pointer to the listview structure
2868 * [O] lprcView : bounding rectangle
2870 * RETURN:
2871 * SUCCESS : TRUE
2872 * FAILURE : FALSE
2874 static BOOL LISTVIEW_GetViewRect(const LISTVIEW_INFO *infoPtr, LPRECT lprcView)
2876 POINT ptOrigin;
2878 TRACE("(lprcView=%p)\n", lprcView);
2880 if (!lprcView) return FALSE;
2882 LISTVIEW_GetAreaRect(infoPtr, lprcView);
2884 if (infoPtr->uView != LV_VIEW_DETAILS)
2886 LISTVIEW_GetOrigin(infoPtr, &ptOrigin);
2887 OffsetRect(lprcView, ptOrigin.x, ptOrigin.y);
2890 TRACE("lprcView=%s\n", wine_dbgstr_rect(lprcView));
2892 return TRUE;
2895 /***
2896 * DESCRIPTION:
2897 * Retrieves the subitem pointer associated with the subitem index.
2899 * PARAMETER(S):
2900 * [I] hdpaSubItems : DPA handle for a specific item
2901 * [I] nSubItem : index of subitem
2903 * RETURN:
2904 * SUCCESS : subitem pointer
2905 * FAILURE : NULL
2907 static SUBITEM_INFO* LISTVIEW_GetSubItemPtr(HDPA hdpaSubItems, INT nSubItem)
2909 SUBITEM_INFO *lpSubItem;
2910 INT i;
2912 /* we should binary search here if need be */
2913 for (i = 1; i < DPA_GetPtrCount(hdpaSubItems); i++)
2915 lpSubItem = DPA_GetPtr(hdpaSubItems, i);
2916 if (lpSubItem->iSubItem == nSubItem)
2917 return lpSubItem;
2920 return NULL;
2924 /***
2925 * DESCRIPTION:
2926 * Calculates the desired item width.
2928 * PARAMETER(S):
2929 * [I] infoPtr : valid pointer to the listview structure
2931 * RETURN:
2932 * The desired item width.
2934 static INT LISTVIEW_CalculateItemWidth(const LISTVIEW_INFO *infoPtr)
2936 INT nItemWidth = 0;
2938 TRACE("uView=%d\n", infoPtr->uView);
2940 if (infoPtr->uView == LV_VIEW_ICON)
2941 nItemWidth = infoPtr->iconSpacing.cx;
2942 else if (infoPtr->uView == LV_VIEW_DETAILS)
2944 if (DPA_GetPtrCount(infoPtr->hdpaColumns) > 0)
2946 RECT rcHeader;
2947 INT index;
2949 index = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX,
2950 DPA_GetPtrCount(infoPtr->hdpaColumns) - 1, 0);
2952 LISTVIEW_GetHeaderRect(infoPtr, index, &rcHeader);
2953 nItemWidth = rcHeader.right;
2956 else /* LV_VIEW_SMALLICON, or LV_VIEW_LIST */
2958 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
2959 LVITEMW lvItem;
2960 INT i;
2962 lvItem.mask = LVIF_TEXT;
2963 lvItem.iSubItem = 0;
2965 for (i = 0; i < infoPtr->nItemCount; i++)
2967 lvItem.iItem = i;
2968 lvItem.pszText = szDispText;
2969 lvItem.cchTextMax = DISP_TEXT_SIZE;
2970 if (LISTVIEW_GetItemW(infoPtr, &lvItem))
2971 nItemWidth = max(LISTVIEW_GetStringWidthT(infoPtr, lvItem.pszText, TRUE),
2972 nItemWidth);
2975 if (infoPtr->himlSmall) nItemWidth += infoPtr->iconSize.cx;
2976 if (infoPtr->himlState) nItemWidth += infoPtr->iconStateSize.cx;
2978 nItemWidth = max(DEFAULT_COLUMN_WIDTH, nItemWidth + WIDTH_PADDING);
2981 return nItemWidth;
2984 /***
2985 * DESCRIPTION:
2986 * Calculates the desired item height.
2988 * PARAMETER(S):
2989 * [I] infoPtr : valid pointer to the listview structure
2991 * RETURN:
2992 * The desired item height.
2994 static INT LISTVIEW_CalculateItemHeight(const LISTVIEW_INFO *infoPtr)
2996 INT nItemHeight;
2998 TRACE("uView=%d\n", infoPtr->uView);
3000 if (infoPtr->uView == LV_VIEW_ICON)
3001 nItemHeight = infoPtr->iconSpacing.cy;
3002 else
3004 nItemHeight = infoPtr->ntmHeight;
3005 if (infoPtr->himlState)
3006 nItemHeight = max(nItemHeight, infoPtr->iconStateSize.cy);
3007 if (infoPtr->himlSmall)
3008 nItemHeight = max(nItemHeight, infoPtr->iconSize.cy);
3009 nItemHeight += HEIGHT_PADDING;
3010 if (infoPtr->nMeasureItemHeight > 0)
3011 nItemHeight = infoPtr->nMeasureItemHeight;
3014 return max(nItemHeight, 1);
3017 /***
3018 * DESCRIPTION:
3019 * Updates the width, and height of an item.
3021 * PARAMETER(S):
3022 * [I] infoPtr : valid pointer to the listview structure
3024 * RETURN:
3025 * None.
3027 static inline void LISTVIEW_UpdateItemSize(LISTVIEW_INFO *infoPtr)
3029 infoPtr->nItemWidth = LISTVIEW_CalculateItemWidth(infoPtr);
3030 infoPtr->nItemHeight = LISTVIEW_CalculateItemHeight(infoPtr);
3034 /***
3035 * DESCRIPTION:
3036 * Retrieves and saves important text metrics info for the current
3037 * Listview font.
3039 * PARAMETER(S):
3040 * [I] infoPtr : valid pointer to the listview structure
3043 static void LISTVIEW_SaveTextMetrics(LISTVIEW_INFO *infoPtr)
3045 HDC hdc = GetDC(infoPtr->hwndSelf);
3046 HFONT hFont = infoPtr->hFont ? infoPtr->hFont : infoPtr->hDefaultFont;
3047 HFONT hOldFont = SelectObject(hdc, hFont);
3048 TEXTMETRICW tm;
3049 SIZE sz;
3051 if (GetTextMetricsW(hdc, &tm))
3053 infoPtr->ntmHeight = tm.tmHeight;
3054 infoPtr->ntmMaxCharWidth = tm.tmMaxCharWidth;
3057 if (GetTextExtentPoint32A(hdc, "...", 3, &sz))
3058 infoPtr->nEllipsisWidth = sz.cx;
3060 SelectObject(hdc, hOldFont);
3061 ReleaseDC(infoPtr->hwndSelf, hdc);
3063 TRACE("tmHeight=%d\n", infoPtr->ntmHeight);
3066 /***
3067 * DESCRIPTION:
3068 * A compare function for ranges
3070 * PARAMETER(S)
3071 * [I] range1 : pointer to range 1;
3072 * [I] range2 : pointer to range 2;
3073 * [I] flags : flags
3075 * RETURNS:
3076 * > 0 : if range 1 > range 2
3077 * < 0 : if range 2 > range 1
3078 * = 0 : if range intersects range 2
3080 static INT CALLBACK ranges_cmp(LPVOID range1, LPVOID range2, LPARAM flags)
3082 INT cmp;
3084 if (((RANGE*)range1)->upper <= ((RANGE*)range2)->lower)
3085 cmp = -1;
3086 else if (((RANGE*)range2)->upper <= ((RANGE*)range1)->lower)
3087 cmp = 1;
3088 else
3089 cmp = 0;
3091 TRACE("range1=%s, range2=%s, cmp=%d\n", debugrange(range1), debugrange(range2), cmp);
3093 return cmp;
3096 #define ranges_check(ranges, desc) if (TRACE_ON(listview)) ranges_assert(ranges, desc, __FILE__, __LINE__)
3098 static void ranges_assert(RANGES ranges, LPCSTR desc, const char *file, int line)
3100 INT i;
3101 RANGE *prev, *curr;
3103 TRACE("*** Checking %s:%d:%s ***\n", file, line, desc);
3104 assert (ranges);
3105 assert (DPA_GetPtrCount(ranges->hdpa) >= 0);
3106 ranges_dump(ranges);
3107 if (DPA_GetPtrCount(ranges->hdpa) > 0)
3109 prev = DPA_GetPtr(ranges->hdpa, 0);
3110 assert (prev->lower >= 0 && prev->lower < prev->upper);
3111 for (i = 1; i < DPA_GetPtrCount(ranges->hdpa); i++)
3113 curr = DPA_GetPtr(ranges->hdpa, i);
3114 assert (prev->upper <= curr->lower);
3115 assert (curr->lower < curr->upper);
3116 prev = curr;
3119 TRACE("--- Done checking---\n");
3122 static RANGES ranges_create(int count)
3124 RANGES ranges = Alloc(sizeof(struct tagRANGES));
3125 if (!ranges) return NULL;
3126 ranges->hdpa = DPA_Create(count);
3127 if (ranges->hdpa) return ranges;
3128 Free(ranges);
3129 return NULL;
3132 static void ranges_clear(RANGES ranges)
3134 INT i;
3136 for(i = 0; i < DPA_GetPtrCount(ranges->hdpa); i++)
3137 Free(DPA_GetPtr(ranges->hdpa, i));
3138 DPA_DeleteAllPtrs(ranges->hdpa);
3142 static void ranges_destroy(RANGES ranges)
3144 if (!ranges) return;
3145 ranges_clear(ranges);
3146 DPA_Destroy(ranges->hdpa);
3147 Free(ranges);
3150 static RANGES ranges_clone(RANGES ranges)
3152 RANGES clone;
3153 INT i;
3155 if (!(clone = ranges_create(DPA_GetPtrCount(ranges->hdpa)))) goto fail;
3157 for (i = 0; i < DPA_GetPtrCount(ranges->hdpa); i++)
3159 RANGE *newrng = Alloc(sizeof(RANGE));
3160 if (!newrng) goto fail;
3161 *newrng = *((RANGE*)DPA_GetPtr(ranges->hdpa, i));
3162 if (!DPA_SetPtr(clone->hdpa, i, newrng))
3164 Free(newrng);
3165 goto fail;
3168 return clone;
3170 fail:
3171 TRACE ("clone failed\n");
3172 ranges_destroy(clone);
3173 return NULL;
3176 static RANGES ranges_diff(RANGES ranges, RANGES sub)
3178 INT i;
3180 for (i = 0; i < DPA_GetPtrCount(sub->hdpa); i++)
3181 ranges_del(ranges, *((RANGE *)DPA_GetPtr(sub->hdpa, i)));
3183 return ranges;
3186 static void ranges_dump(RANGES ranges)
3188 INT i;
3190 for (i = 0; i < DPA_GetPtrCount(ranges->hdpa); i++)
3191 TRACE(" %s\n", debugrange(DPA_GetPtr(ranges->hdpa, i)));
3194 static inline BOOL ranges_contain(RANGES ranges, INT nItem)
3196 RANGE srchrng = { nItem, nItem + 1 };
3198 TRACE("(nItem=%d)\n", nItem);
3199 ranges_check(ranges, "before contain");
3200 return DPA_Search(ranges->hdpa, &srchrng, 0, ranges_cmp, 0, DPAS_SORTED) != -1;
3203 static INT ranges_itemcount(RANGES ranges)
3205 INT i, count = 0;
3207 for (i = 0; i < DPA_GetPtrCount(ranges->hdpa); i++)
3209 RANGE *sel = DPA_GetPtr(ranges->hdpa, i);
3210 count += sel->upper - sel->lower;
3213 return count;
3216 static BOOL ranges_shift(RANGES ranges, INT nItem, INT delta, INT nUpper)
3218 RANGE srchrng = { nItem, nItem + 1 }, *chkrng;
3219 INT index;
3221 index = DPA_Search(ranges->hdpa, &srchrng, 0, ranges_cmp, 0, DPAS_SORTED | DPAS_INSERTAFTER);
3222 if (index == -1) return TRUE;
3224 for (; index < DPA_GetPtrCount(ranges->hdpa); index++)
3226 chkrng = DPA_GetPtr(ranges->hdpa, index);
3227 if (chkrng->lower >= nItem)
3228 chkrng->lower = max(min(chkrng->lower + delta, nUpper - 1), 0);
3229 if (chkrng->upper > nItem)
3230 chkrng->upper = max(min(chkrng->upper + delta, nUpper), 0);
3232 return TRUE;
3235 static BOOL ranges_add(RANGES ranges, RANGE range)
3237 RANGE srchrgn;
3238 INT index;
3240 TRACE("(%s)\n", debugrange(&range));
3241 ranges_check(ranges, "before add");
3243 /* try find overlapping regions first */
3244 srchrgn.lower = range.lower - 1;
3245 srchrgn.upper = range.upper + 1;
3246 index = DPA_Search(ranges->hdpa, &srchrgn, 0, ranges_cmp, 0, DPAS_SORTED);
3248 if (index == -1)
3250 RANGE *newrgn;
3252 TRACE("Adding new range\n");
3254 /* create the brand new range to insert */
3255 newrgn = Alloc(sizeof(RANGE));
3256 if(!newrgn) goto fail;
3257 *newrgn = range;
3259 /* figure out where to insert it */
3260 index = DPA_Search(ranges->hdpa, newrgn, 0, ranges_cmp, 0, DPAS_SORTED | DPAS_INSERTAFTER);
3261 TRACE("index=%d\n", index);
3262 if (index == -1) index = 0;
3264 /* and get it over with */
3265 if (DPA_InsertPtr(ranges->hdpa, index, newrgn) == -1)
3267 Free(newrgn);
3268 goto fail;
3271 else
3273 RANGE *chkrgn, *mrgrgn;
3274 INT fromindex, mergeindex;
3276 chkrgn = DPA_GetPtr(ranges->hdpa, index);
3277 TRACE("Merge with %s @%d\n", debugrange(chkrgn), index);
3279 chkrgn->lower = min(range.lower, chkrgn->lower);
3280 chkrgn->upper = max(range.upper, chkrgn->upper);
3282 TRACE("New range %s @%d\n", debugrange(chkrgn), index);
3284 /* merge now common ranges */
3285 fromindex = 0;
3286 srchrgn.lower = chkrgn->lower - 1;
3287 srchrgn.upper = chkrgn->upper + 1;
3291 mergeindex = DPA_Search(ranges->hdpa, &srchrgn, fromindex, ranges_cmp, 0, 0);
3292 if (mergeindex == -1) break;
3293 if (mergeindex == index)
3295 fromindex = index + 1;
3296 continue;
3299 TRACE("Merge with index %i\n", mergeindex);
3301 mrgrgn = DPA_GetPtr(ranges->hdpa, mergeindex);
3302 chkrgn->lower = min(chkrgn->lower, mrgrgn->lower);
3303 chkrgn->upper = max(chkrgn->upper, mrgrgn->upper);
3304 Free(mrgrgn);
3305 DPA_DeletePtr(ranges->hdpa, mergeindex);
3306 if (mergeindex < index) index --;
3307 } while(1);
3310 ranges_check(ranges, "after add");
3311 return TRUE;
3313 fail:
3314 ranges_check(ranges, "failed add");
3315 return FALSE;
3318 static BOOL ranges_del(RANGES ranges, RANGE range)
3320 RANGE *chkrgn;
3321 INT index;
3323 TRACE("(%s)\n", debugrange(&range));
3324 ranges_check(ranges, "before del");
3326 /* we don't use DPAS_SORTED here, since we need *
3327 * to find the first overlapping range */
3328 index = DPA_Search(ranges->hdpa, &range, 0, ranges_cmp, 0, 0);
3329 while(index != -1)
3331 chkrgn = DPA_GetPtr(ranges->hdpa, index);
3333 TRACE("Matches range %s @%d\n", debugrange(chkrgn), index);
3335 /* case 1: Same range */
3336 if ( (chkrgn->upper == range.upper) &&
3337 (chkrgn->lower == range.lower) )
3339 DPA_DeletePtr(ranges->hdpa, index);
3340 Free(chkrgn);
3341 break;
3343 /* case 2: engulf */
3344 else if ( (chkrgn->upper <= range.upper) &&
3345 (chkrgn->lower >= range.lower) )
3347 DPA_DeletePtr(ranges->hdpa, index);
3348 Free(chkrgn);
3350 /* case 3: overlap upper */
3351 else if ( (chkrgn->upper <= range.upper) &&
3352 (chkrgn->lower < range.lower) )
3354 chkrgn->upper = range.lower;
3356 /* case 4: overlap lower */
3357 else if ( (chkrgn->upper > range.upper) &&
3358 (chkrgn->lower >= range.lower) )
3360 chkrgn->lower = range.upper;
3361 break;
3363 /* case 5: fully internal */
3364 else
3366 RANGE *newrgn;
3368 if (!(newrgn = Alloc(sizeof(RANGE)))) goto fail;
3369 newrgn->lower = chkrgn->lower;
3370 newrgn->upper = range.lower;
3371 chkrgn->lower = range.upper;
3372 if (DPA_InsertPtr(ranges->hdpa, index, newrgn) == -1)
3374 Free(newrgn);
3375 goto fail;
3377 break;
3380 index = DPA_Search(ranges->hdpa, &range, index, ranges_cmp, 0, 0);
3383 ranges_check(ranges, "after del");
3384 return TRUE;
3386 fail:
3387 ranges_check(ranges, "failed del");
3388 return FALSE;
3391 /***
3392 * DESCRIPTION:
3393 * Removes all selection ranges
3395 * Parameters(s):
3396 * [I] infoPtr : valid pointer to the listview structure
3397 * [I] toSkip : item range to skip removing the selection
3399 * RETURNS:
3400 * SUCCESS : TRUE
3401 * FAILURE : FALSE
3403 static BOOL LISTVIEW_DeselectAllSkipItems(LISTVIEW_INFO *infoPtr, RANGES toSkip)
3405 LVITEMW lvItem;
3406 ITERATOR i;
3407 RANGES clone;
3409 TRACE("()\n");
3411 lvItem.state = 0;
3412 lvItem.stateMask = LVIS_SELECTED;
3414 /* need to clone the DPA because callbacks can change it */
3415 if (!(clone = ranges_clone(infoPtr->selectionRanges))) return FALSE;
3416 iterator_rangesitems(&i, ranges_diff(clone, toSkip));
3417 while(iterator_next(&i))
3418 LISTVIEW_SetItemState(infoPtr, i.nItem, &lvItem);
3419 /* note that the iterator destructor will free the cloned range */
3420 iterator_destroy(&i);
3422 return TRUE;
3425 static inline BOOL LISTVIEW_DeselectAllSkipItem(LISTVIEW_INFO *infoPtr, INT nItem)
3427 RANGES toSkip;
3429 if (!(toSkip = ranges_create(1))) return FALSE;
3430 if (nItem != -1) ranges_additem(toSkip, nItem);
3431 LISTVIEW_DeselectAllSkipItems(infoPtr, toSkip);
3432 ranges_destroy(toSkip);
3433 return TRUE;
3436 static inline BOOL LISTVIEW_DeselectAll(LISTVIEW_INFO *infoPtr)
3438 return LISTVIEW_DeselectAllSkipItem(infoPtr, -1);
3441 /***
3442 * DESCRIPTION:
3443 * Retrieves the number of items that are marked as selected.
3445 * PARAMETER(S):
3446 * [I] infoPtr : valid pointer to the listview structure
3448 * RETURN:
3449 * Number of items selected.
3451 static INT LISTVIEW_GetSelectedCount(const LISTVIEW_INFO *infoPtr)
3453 INT nSelectedCount = 0;
3455 if (infoPtr->uCallbackMask & LVIS_SELECTED)
3457 INT i;
3458 for (i = 0; i < infoPtr->nItemCount; i++)
3460 if (LISTVIEW_GetItemState(infoPtr, i, LVIS_SELECTED))
3461 nSelectedCount++;
3464 else
3465 nSelectedCount = ranges_itemcount(infoPtr->selectionRanges);
3467 TRACE("nSelectedCount=%d\n", nSelectedCount);
3468 return nSelectedCount;
3471 /***
3472 * DESCRIPTION:
3473 * Manages the item focus.
3475 * PARAMETER(S):
3476 * [I] infoPtr : valid pointer to the listview structure
3477 * [I] nItem : item index
3479 * RETURN:
3480 * TRUE : focused item changed
3481 * FALSE : focused item has NOT changed
3483 static inline BOOL LISTVIEW_SetItemFocus(LISTVIEW_INFO *infoPtr, INT nItem)
3485 INT oldFocus = infoPtr->nFocusedItem;
3486 LVITEMW lvItem;
3488 if (nItem == infoPtr->nFocusedItem) return FALSE;
3490 lvItem.state = nItem == -1 ? 0 : LVIS_FOCUSED;
3491 lvItem.stateMask = LVIS_FOCUSED;
3492 LISTVIEW_SetItemState(infoPtr, nItem == -1 ? infoPtr->nFocusedItem : nItem, &lvItem);
3494 return oldFocus != infoPtr->nFocusedItem;
3497 static INT shift_item(const LISTVIEW_INFO *infoPtr, INT nShiftItem, INT nItem, INT direction)
3499 if (nShiftItem < nItem) return nShiftItem;
3501 if (nShiftItem > nItem) return nShiftItem + direction;
3503 if (direction > 0) return nShiftItem + direction;
3505 return min(nShiftItem, infoPtr->nItemCount - 1);
3508 /* This function updates focus index.
3510 Parameters:
3511 focus : current focus index
3512 item : index of item to be added/removed
3513 direction : add/remove flag
3515 static void LISTVIEW_ShiftFocus(LISTVIEW_INFO *infoPtr, INT focus, INT item, INT direction)
3517 DWORD old_mask = infoPtr->notify_mask & NOTIFY_MASK_ITEM_CHANGE;
3519 infoPtr->notify_mask &= ~NOTIFY_MASK_ITEM_CHANGE;
3520 focus = shift_item(infoPtr, focus, item, direction);
3521 if (focus != infoPtr->nFocusedItem)
3522 LISTVIEW_SetItemFocus(infoPtr, focus);
3523 infoPtr->notify_mask |= old_mask;
3527 * DESCRIPTION:
3528 * Updates the various indices after an item has been inserted or deleted.
3530 * PARAMETER(S):
3531 * [I] infoPtr : valid pointer to the listview structure
3532 * [I] nItem : item index
3533 * [I] direction : Direction of shift, +1 or -1.
3535 * RETURN:
3536 * None
3538 static void LISTVIEW_ShiftIndices(LISTVIEW_INFO *infoPtr, INT nItem, INT direction)
3540 TRACE("Shifting %i, %i steps\n", nItem, direction);
3542 ranges_shift(infoPtr->selectionRanges, nItem, direction, infoPtr->nItemCount);
3543 assert(abs(direction) == 1);
3544 infoPtr->nSelectionMark = shift_item(infoPtr, infoPtr->nSelectionMark, nItem, direction);
3546 /* But we are not supposed to modify nHotItem! */
3550 * DESCRIPTION:
3551 * Adds a block of selections.
3553 * PARAMETER(S):
3554 * [I] infoPtr : valid pointer to the listview structure
3555 * [I] nItem : item index
3557 * RETURN:
3558 * Whether the window is still valid.
3560 static BOOL LISTVIEW_AddGroupSelection(LISTVIEW_INFO *infoPtr, INT nItem)
3562 INT nFirst = min(infoPtr->nSelectionMark, nItem);
3563 INT nLast = max(infoPtr->nSelectionMark, nItem);
3564 HWND hwndSelf = infoPtr->hwndSelf;
3565 NMLVODSTATECHANGE nmlv;
3566 DWORD old_mask;
3567 LVITEMW item;
3568 INT i;
3570 /* Temporarily disable change notification
3571 * If the control is LVS_OWNERDATA, we need to send
3572 * only one LVN_ODSTATECHANGED notification.
3573 * See MSDN documentation for LVN_ITEMCHANGED.
3575 old_mask = infoPtr->notify_mask & NOTIFY_MASK_ITEM_CHANGE;
3576 if (infoPtr->dwStyle & LVS_OWNERDATA)
3577 infoPtr->notify_mask &= ~NOTIFY_MASK_ITEM_CHANGE;
3579 if (nFirst == -1) nFirst = nItem;
3581 item.state = LVIS_SELECTED;
3582 item.stateMask = LVIS_SELECTED;
3584 for (i = nFirst; i <= nLast; i++)
3585 LISTVIEW_SetItemState(infoPtr,i,&item);
3587 ZeroMemory(&nmlv, sizeof(nmlv));
3588 nmlv.iFrom = nFirst;
3589 nmlv.iTo = nLast;
3590 nmlv.uOldState = 0;
3591 nmlv.uNewState = item.state;
3593 notify_hdr(infoPtr, LVN_ODSTATECHANGED, (LPNMHDR)&nmlv);
3594 if (!IsWindow(hwndSelf))
3595 return FALSE;
3596 infoPtr->notify_mask |= old_mask;
3597 return TRUE;
3601 /***
3602 * DESCRIPTION:
3603 * Sets a single group selection.
3605 * PARAMETER(S):
3606 * [I] infoPtr : valid pointer to the listview structure
3607 * [I] nItem : item index
3609 * RETURN:
3610 * None
3612 static void LISTVIEW_SetGroupSelection(LISTVIEW_INFO *infoPtr, INT nItem)
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 FIXME: single LVN_ODSTATECHANGED should be used */
3667 old_mask = infoPtr->notify_mask & NOTIFY_MASK_ITEM_CHANGE;
3668 if (infoPtr->dwStyle & LVS_OWNERDATA)
3669 infoPtr->notify_mask &= ~NOTIFY_MASK_ITEM_CHANGE;
3671 LISTVIEW_DeselectAllSkipItems(infoPtr, selection);
3674 iterator_rangesitems(&i, selection);
3675 while(iterator_next(&i))
3676 LISTVIEW_SetItemState(infoPtr, i.nItem, &item);
3677 /* this will also destroy the selection */
3678 iterator_destroy(&i);
3680 infoPtr->notify_mask |= old_mask;
3681 LISTVIEW_SetItemFocus(infoPtr, nItem);
3684 /***
3685 * DESCRIPTION:
3686 * Sets a single selection.
3688 * PARAMETER(S):
3689 * [I] infoPtr : valid pointer to the listview structure
3690 * [I] nItem : item index
3692 * RETURN:
3693 * None
3695 static void LISTVIEW_SetSelection(LISTVIEW_INFO *infoPtr, INT nItem)
3697 LVITEMW lvItem;
3699 TRACE("nItem=%d\n", nItem);
3701 LISTVIEW_DeselectAllSkipItem(infoPtr, nItem);
3703 lvItem.state = LVIS_FOCUSED | LVIS_SELECTED;
3704 lvItem.stateMask = LVIS_FOCUSED | LVIS_SELECTED;
3705 LISTVIEW_SetItemState(infoPtr, nItem, &lvItem);
3707 infoPtr->nSelectionMark = nItem;
3710 /***
3711 * DESCRIPTION:
3712 * Set selection(s) with keyboard.
3714 * PARAMETER(S):
3715 * [I] infoPtr : valid pointer to the listview structure
3716 * [I] nItem : item index
3717 * [I] space : VK_SPACE code sent
3719 * RETURN:
3720 * SUCCESS : TRUE (needs to be repainted)
3721 * FAILURE : FALSE (nothing has changed)
3723 static BOOL LISTVIEW_KeySelection(LISTVIEW_INFO *infoPtr, INT nItem, BOOL space)
3725 /* FIXME: pass in the state */
3726 WORD wShift = GetKeyState(VK_SHIFT) & 0x8000;
3727 WORD wCtrl = GetKeyState(VK_CONTROL) & 0x8000;
3728 BOOL bResult = FALSE;
3730 TRACE("nItem=%d, wShift=%d, wCtrl=%d\n", nItem, wShift, wCtrl);
3731 if ((nItem >= 0) && (nItem < infoPtr->nItemCount))
3733 bResult = TRUE;
3735 if (infoPtr->dwStyle & LVS_SINGLESEL || (wShift == 0 && wCtrl == 0))
3736 LISTVIEW_SetSelection(infoPtr, nItem);
3737 else
3739 if (wShift)
3740 LISTVIEW_SetGroupSelection(infoPtr, nItem);
3741 else if (wCtrl)
3743 LVITEMW lvItem;
3744 lvItem.state = ~LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED);
3745 lvItem.stateMask = LVIS_SELECTED;
3746 if (space)
3748 LISTVIEW_SetItemState(infoPtr, nItem, &lvItem);
3749 if (lvItem.state & LVIS_SELECTED)
3750 infoPtr->nSelectionMark = nItem;
3752 bResult = LISTVIEW_SetItemFocus(infoPtr, nItem);
3755 LISTVIEW_EnsureVisible(infoPtr, nItem, FALSE);
3758 UpdateWindow(infoPtr->hwndSelf); /* update client area */
3759 return bResult;
3762 static BOOL LISTVIEW_GetItemAtPt(const LISTVIEW_INFO *infoPtr, LPLVITEMW lpLVItem, POINT pt)
3764 LVHITTESTINFO lvHitTestInfo;
3766 ZeroMemory(&lvHitTestInfo, sizeof(lvHitTestInfo));
3767 lvHitTestInfo.pt.x = pt.x;
3768 lvHitTestInfo.pt.y = pt.y;
3770 LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, FALSE);
3772 lpLVItem->mask = LVIF_PARAM;
3773 lpLVItem->iItem = lvHitTestInfo.iItem;
3774 lpLVItem->iSubItem = 0;
3776 return LISTVIEW_GetItemT(infoPtr, lpLVItem, TRUE);
3779 static inline BOOL LISTVIEW_IsHotTracking(const LISTVIEW_INFO *infoPtr)
3781 return ((infoPtr->dwLvExStyle & LVS_EX_TRACKSELECT) ||
3782 (infoPtr->dwLvExStyle & LVS_EX_ONECLICKACTIVATE) ||
3783 (infoPtr->dwLvExStyle & LVS_EX_TWOCLICKACTIVATE));
3786 /***
3787 * DESCRIPTION:
3788 * Called when the mouse is being actively tracked and has hovered for a specified
3789 * amount of time
3791 * PARAMETER(S):
3792 * [I] infoPtr : valid pointer to the listview structure
3793 * [I] fwKeys : key indicator
3794 * [I] x,y : mouse position
3796 * RETURN:
3797 * 0 if the message was processed, non-zero if there was an error
3799 * INFO:
3800 * LVS_EX_TRACKSELECT: An item is automatically selected when the cursor remains
3801 * over the item for a certain period of time.
3804 static LRESULT LISTVIEW_MouseHover(LISTVIEW_INFO *infoPtr, INT x, INT y)
3806 NMHDR hdr;
3808 if (notify_hdr(infoPtr, NM_HOVER, &hdr)) return 0;
3810 if (LISTVIEW_IsHotTracking(infoPtr))
3812 LVITEMW item;
3813 POINT pt;
3815 pt.x = x;
3816 pt.y = y;
3818 if (LISTVIEW_GetItemAtPt(infoPtr, &item, pt))
3819 LISTVIEW_SetSelection(infoPtr, item.iItem);
3821 SetFocus(infoPtr->hwndSelf);
3824 return 0;
3827 #define SCROLL_LEFT 0x1
3828 #define SCROLL_RIGHT 0x2
3829 #define SCROLL_UP 0x4
3830 #define SCROLL_DOWN 0x8
3832 /***
3833 * DESCRIPTION:
3834 * Utility routine to draw and highlight items within a marquee selection rectangle.
3836 * PARAMETER(S):
3837 * [I] infoPtr : valid pointer to the listview structure
3838 * [I] coords_orig : original co-ordinates of the cursor
3839 * [I] coords_offs : offsetted coordinates of the cursor
3840 * [I] offset : offset amount
3841 * [I] scroll : Bitmask of which directions we should scroll, if at all
3843 * RETURN:
3844 * None.
3846 static void LISTVIEW_MarqueeHighlight(LISTVIEW_INFO *infoPtr, const POINT *coords_orig,
3847 INT scroll)
3849 BOOL controlDown = FALSE;
3850 LVITEMW item;
3851 ITERATOR old_elems, new_elems;
3852 RECT rect;
3853 POINT coords_offs, offset;
3855 /* Ensure coordinates are within client bounds */
3856 coords_offs.x = max(min(coords_orig->x, infoPtr->rcList.right), 0);
3857 coords_offs.y = max(min(coords_orig->y, infoPtr->rcList.bottom), 0);
3859 /* Get offset */
3860 LISTVIEW_GetOrigin(infoPtr, &offset);
3862 /* Offset coordinates by the appropriate amount */
3863 coords_offs.x -= offset.x;
3864 coords_offs.y -= offset.y;
3866 if (coords_offs.x > infoPtr->marqueeOrigin.x)
3868 rect.left = infoPtr->marqueeOrigin.x;
3869 rect.right = coords_offs.x;
3871 else
3873 rect.left = coords_offs.x;
3874 rect.right = infoPtr->marqueeOrigin.x;
3877 if (coords_offs.y > infoPtr->marqueeOrigin.y)
3879 rect.top = infoPtr->marqueeOrigin.y;
3880 rect.bottom = coords_offs.y;
3882 else
3884 rect.top = coords_offs.y;
3885 rect.bottom = infoPtr->marqueeOrigin.y;
3888 /* Cancel out the old marquee rectangle and draw the new one */
3889 LISTVIEW_InvalidateRect(infoPtr, &infoPtr->marqueeDrawRect);
3891 /* Scroll by the appropriate distance if applicable - speed up scrolling as
3892 the cursor is further away */
3894 if ((scroll & SCROLL_LEFT) && (coords_orig->x <= 0))
3895 LISTVIEW_Scroll(infoPtr, coords_orig->x, 0);
3897 if ((scroll & SCROLL_RIGHT) && (coords_orig->x >= infoPtr->rcList.right))
3898 LISTVIEW_Scroll(infoPtr, (coords_orig->x - infoPtr->rcList.right), 0);
3900 if ((scroll & SCROLL_UP) && (coords_orig->y <= 0))
3901 LISTVIEW_Scroll(infoPtr, 0, coords_orig->y);
3903 if ((scroll & SCROLL_DOWN) && (coords_orig->y >= infoPtr->rcList.bottom))
3904 LISTVIEW_Scroll(infoPtr, 0, (coords_orig->y - infoPtr->rcList.bottom));
3906 iterator_frameditems_absolute(&old_elems, infoPtr, &infoPtr->marqueeRect);
3908 infoPtr->marqueeRect = rect;
3909 infoPtr->marqueeDrawRect = rect;
3910 OffsetRect(&infoPtr->marqueeDrawRect, offset.x, offset.y);
3912 iterator_frameditems_absolute(&new_elems, infoPtr, &infoPtr->marqueeRect);
3913 iterator_remove_common_items(&old_elems, &new_elems);
3915 /* Iterate over no longer selected items */
3916 while (iterator_next(&old_elems))
3918 if (old_elems.nItem > -1)
3920 if (LISTVIEW_GetItemState(infoPtr, old_elems.nItem, LVIS_SELECTED) == LVIS_SELECTED)
3921 item.state = 0;
3922 else
3923 item.state = LVIS_SELECTED;
3925 item.stateMask = LVIS_SELECTED;
3927 LISTVIEW_SetItemState(infoPtr, old_elems.nItem, &item);
3930 iterator_destroy(&old_elems);
3933 /* Iterate over newly selected items */
3934 if (GetKeyState(VK_CONTROL) & 0x8000)
3935 controlDown = TRUE;
3937 while (iterator_next(&new_elems))
3939 if (new_elems.nItem > -1)
3941 /* If CTRL is pressed, invert. If not, always select the item. */
3942 if ((controlDown) && (LISTVIEW_GetItemState(infoPtr, new_elems.nItem, LVIS_SELECTED)))
3943 item.state = 0;
3944 else
3945 item.state = LVIS_SELECTED;
3947 item.stateMask = LVIS_SELECTED;
3949 LISTVIEW_SetItemState(infoPtr, new_elems.nItem, &item);
3952 iterator_destroy(&new_elems);
3954 LISTVIEW_InvalidateRect(infoPtr, &infoPtr->marqueeDrawRect);
3957 /***
3958 * DESCRIPTION:
3959 * Called when we are in a marquee selection that involves scrolling the listview (ie,
3960 * the cursor is outside the bounds of the client area). This is a TIMERPROC.
3962 * PARAMETER(S):
3963 * [I] hwnd : Handle to the listview
3964 * [I] uMsg : WM_TIMER (ignored)
3965 * [I] idEvent : The timer ID interpreted as a pointer to a LISTVIEW_INFO struct
3966 * [I] dwTimer : The elapsed time (ignored)
3968 * RETURN:
3969 * None.
3971 static VOID CALLBACK LISTVIEW_ScrollTimer(HWND hWnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime)
3973 LISTVIEW_INFO *infoPtr;
3974 SCROLLINFO scrollInfo;
3975 POINT coords;
3976 INT scroll = 0;
3978 infoPtr = (LISTVIEW_INFO *) idEvent;
3980 if (!infoPtr)
3981 return;
3983 /* Get the current cursor position and convert to client coordinates */
3984 GetCursorPos(&coords);
3985 ScreenToClient(hWnd, &coords);
3987 scrollInfo.cbSize = sizeof(SCROLLINFO);
3988 scrollInfo.fMask = SIF_ALL;
3990 /* Work out in which directions we can scroll */
3991 if (GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo))
3993 if (scrollInfo.nPos != scrollInfo.nMin)
3994 scroll |= SCROLL_UP;
3996 if (((scrollInfo.nPage + scrollInfo.nPos) - 1) != scrollInfo.nMax)
3997 scroll |= SCROLL_DOWN;
4000 if (GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo))
4002 if (scrollInfo.nPos != scrollInfo.nMin)
4003 scroll |= SCROLL_LEFT;
4005 if (((scrollInfo.nPage + scrollInfo.nPos) - 1) != scrollInfo.nMax)
4006 scroll |= SCROLL_RIGHT;
4009 if (((coords.x <= 0) && (scroll & SCROLL_LEFT)) ||
4010 ((coords.y <= 0) && (scroll & SCROLL_UP)) ||
4011 ((coords.x >= infoPtr->rcList.right) && (scroll & SCROLL_RIGHT)) ||
4012 ((coords.y >= infoPtr->rcList.bottom) && (scroll & SCROLL_DOWN)))
4014 LISTVIEW_MarqueeHighlight(infoPtr, &coords, scroll);
4018 /***
4019 * DESCRIPTION:
4020 * Called whenever WM_MOUSEMOVE is received.
4022 * PARAMETER(S):
4023 * [I] infoPtr : valid pointer to the listview structure
4024 * [I] fwKeys : key indicator
4025 * [I] x,y : mouse position
4027 * RETURN:
4028 * 0 if the message is processed, non-zero if there was an error
4030 static LRESULT LISTVIEW_MouseMove(LISTVIEW_INFO *infoPtr, WORD fwKeys, INT x, INT y)
4032 LVHITTESTINFO ht;
4033 RECT rect;
4034 POINT pt;
4036 pt.x = x;
4037 pt.y = y;
4039 if (!(fwKeys & MK_LBUTTON))
4040 infoPtr->bLButtonDown = FALSE;
4042 if (infoPtr->bLButtonDown)
4044 rect.left = rect.right = infoPtr->ptClickPos.x;
4045 rect.top = rect.bottom = infoPtr->ptClickPos.y;
4047 InflateRect(&rect, GetSystemMetrics(SM_CXDRAG), GetSystemMetrics(SM_CYDRAG));
4049 if (infoPtr->bMarqueeSelect)
4051 /* Enable the timer if we're going outside our bounds, in case the user doesn't
4052 move the mouse again */
4054 if ((x <= 0) || (y <= 0) || (x >= infoPtr->rcList.right) ||
4055 (y >= infoPtr->rcList.bottom))
4057 if (!infoPtr->bScrolling)
4059 infoPtr->bScrolling = TRUE;
4060 SetTimer(infoPtr->hwndSelf, (UINT_PTR) infoPtr, 1, LISTVIEW_ScrollTimer);
4063 else
4065 infoPtr->bScrolling = FALSE;
4066 KillTimer(infoPtr->hwndSelf, (UINT_PTR) infoPtr);
4069 LISTVIEW_MarqueeHighlight(infoPtr, &pt, 0);
4070 return 0;
4073 ht.pt = pt;
4074 LISTVIEW_HitTest(infoPtr, &ht, TRUE, TRUE);
4076 /* reset item marker */
4077 if (infoPtr->nLButtonDownItem != ht.iItem)
4078 infoPtr->nLButtonDownItem = -1;
4080 if (!PtInRect(&rect, pt))
4082 /* this path covers the following:
4083 1. WM_LBUTTONDOWN over selected item (sets focus on it)
4084 2. change focus with keys
4085 3. move mouse over item from step 1 selects it and moves focus on it */
4086 if (infoPtr->nLButtonDownItem != -1 &&
4087 !LISTVIEW_GetItemState(infoPtr, infoPtr->nLButtonDownItem, LVIS_SELECTED))
4089 LVITEMW lvItem;
4091 lvItem.state = LVIS_FOCUSED | LVIS_SELECTED;
4092 lvItem.stateMask = LVIS_FOCUSED | LVIS_SELECTED;
4094 LISTVIEW_SetItemState(infoPtr, infoPtr->nLButtonDownItem, &lvItem);
4095 infoPtr->nLButtonDownItem = -1;
4098 if (!infoPtr->bDragging)
4100 ht.pt = infoPtr->ptClickPos;
4101 LISTVIEW_HitTest(infoPtr, &ht, TRUE, TRUE);
4103 /* If the click is outside the range of an item, begin a
4104 highlight. If not, begin an item drag. */
4105 if (ht.iItem == -1)
4107 NMHDR hdr;
4109 /* If we're allowing multiple selections, send notification.
4110 If return value is non-zero, cancel. */
4111 if (!(infoPtr->dwStyle & LVS_SINGLESEL) && (notify_hdr(infoPtr, LVN_MARQUEEBEGIN, &hdr) == 0))
4113 /* Store the absolute coordinates of the click */
4114 POINT offset;
4115 LISTVIEW_GetOrigin(infoPtr, &offset);
4117 infoPtr->marqueeOrigin.x = infoPtr->ptClickPos.x - offset.x;
4118 infoPtr->marqueeOrigin.y = infoPtr->ptClickPos.y - offset.y;
4120 /* Begin selection and capture mouse */
4121 infoPtr->bMarqueeSelect = TRUE;
4122 SetCapture(infoPtr->hwndSelf);
4125 else
4127 NMLISTVIEW nmlv;
4129 ZeroMemory(&nmlv, sizeof(nmlv));
4130 nmlv.iItem = ht.iItem;
4131 nmlv.ptAction = infoPtr->ptClickPos;
4133 notify_listview(infoPtr, LVN_BEGINDRAG, &nmlv);
4134 infoPtr->bDragging = TRUE;
4138 return 0;
4142 /* see if we are supposed to be tracking mouse hovering */
4143 if (LISTVIEW_IsHotTracking(infoPtr)) {
4144 TRACKMOUSEEVENT trackinfo;
4145 DWORD flags;
4147 trackinfo.cbSize = sizeof(TRACKMOUSEEVENT);
4148 trackinfo.dwFlags = TME_QUERY;
4150 /* see if we are already tracking this hwnd */
4151 _TrackMouseEvent(&trackinfo);
4153 flags = TME_LEAVE;
4154 if(infoPtr->dwLvExStyle & LVS_EX_TRACKSELECT)
4155 flags |= TME_HOVER;
4157 if((trackinfo.dwFlags & flags) != flags || trackinfo.hwndTrack != infoPtr->hwndSelf) {
4158 trackinfo.dwFlags = flags;
4159 trackinfo.dwHoverTime = infoPtr->dwHoverTime;
4160 trackinfo.hwndTrack = infoPtr->hwndSelf;
4162 /* call TRACKMOUSEEVENT so we receive WM_MOUSEHOVER messages */
4163 _TrackMouseEvent(&trackinfo);
4167 return 0;
4171 /***
4172 * Tests whether the item is assignable to a list with style lStyle
4174 static inline BOOL is_assignable_item(const LVITEMW *lpLVItem, LONG lStyle)
4176 if ( (lpLVItem->mask & LVIF_TEXT) &&
4177 (lpLVItem->pszText == LPSTR_TEXTCALLBACKW) &&
4178 (lStyle & (LVS_SORTASCENDING | LVS_SORTDESCENDING)) ) return FALSE;
4180 return TRUE;
4184 /***
4185 * DESCRIPTION:
4186 * Helper for LISTVIEW_SetItemT and LISTVIEW_InsertItemT: sets item attributes.
4188 * PARAMETER(S):
4189 * [I] infoPtr : valid pointer to the listview structure
4190 * [I] lpLVItem : valid pointer to new item attributes
4191 * [I] isNew : the item being set is being inserted
4192 * [I] isW : TRUE if lpLVItem is Unicode, FALSE if it's ANSI
4193 * [O] bChanged : will be set to TRUE if the item really changed
4195 * RETURN:
4196 * SUCCESS : TRUE
4197 * FAILURE : FALSE
4199 static BOOL set_main_item(LISTVIEW_INFO *infoPtr, const LVITEMW *lpLVItem, BOOL isNew, BOOL isW, BOOL *bChanged)
4201 ITEM_INFO *lpItem;
4202 NMLISTVIEW nmlv;
4203 UINT uChanged = 0;
4204 LVITEMW item;
4205 /* stateMask is ignored for LVM_INSERTITEM */
4206 UINT stateMask = isNew ? ~0 : lpLVItem->stateMask;
4208 TRACE("()\n");
4210 assert(lpLVItem->iItem >= 0 && lpLVItem->iItem < infoPtr->nItemCount);
4212 if (lpLVItem->mask == 0) return TRUE;
4214 if (infoPtr->dwStyle & LVS_OWNERDATA)
4216 /* a virtual listview only stores selection and focus */
4217 if (lpLVItem->mask & ~LVIF_STATE)
4218 return FALSE;
4219 lpItem = NULL;
4221 else
4223 HDPA hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, lpLVItem->iItem);
4224 lpItem = DPA_GetPtr(hdpaSubItems, 0);
4225 assert (lpItem);
4228 /* we need to get the lParam and state of the item */
4229 item.iItem = lpLVItem->iItem;
4230 item.iSubItem = lpLVItem->iSubItem;
4231 item.mask = LVIF_STATE | LVIF_PARAM;
4232 item.stateMask = (infoPtr->dwStyle & LVS_OWNERDATA) ? LVIS_FOCUSED | LVIS_SELECTED : ~0;
4234 item.state = 0;
4235 item.lParam = 0;
4236 if (!isNew && !LISTVIEW_GetItemW(infoPtr, &item)) return FALSE;
4238 TRACE("oldState=%x, newState=%x\n", item.state, lpLVItem->state);
4239 /* determine what fields will change */
4240 if ((lpLVItem->mask & LVIF_STATE) && ((item.state ^ lpLVItem->state) & stateMask & ~infoPtr->uCallbackMask))
4241 uChanged |= LVIF_STATE;
4243 if ((lpLVItem->mask & LVIF_IMAGE) && (lpItem->hdr.iImage != lpLVItem->iImage))
4244 uChanged |= LVIF_IMAGE;
4246 if ((lpLVItem->mask & LVIF_PARAM) && (lpItem->lParam != lpLVItem->lParam))
4247 uChanged |= LVIF_PARAM;
4249 if ((lpLVItem->mask & LVIF_INDENT) && (lpItem->iIndent != lpLVItem->iIndent))
4250 uChanged |= LVIF_INDENT;
4252 if ((lpLVItem->mask & LVIF_TEXT) && textcmpWT(lpItem->hdr.pszText, lpLVItem->pszText, isW))
4253 uChanged |= LVIF_TEXT;
4255 TRACE("change mask=0x%x\n", uChanged);
4257 memset(&nmlv, 0, sizeof(NMLISTVIEW));
4258 nmlv.iItem = lpLVItem->iItem;
4259 nmlv.uNewState = (item.state & ~stateMask) | (lpLVItem->state & stateMask);
4260 nmlv.uOldState = item.state;
4261 nmlv.uChanged = uChanged ? uChanged : lpLVItem->mask;
4262 nmlv.lParam = item.lParam;
4264 /* Send LVN_ITEMCHANGING notification, if the item is not being inserted
4265 and we are _NOT_ virtual (LVS_OWNERDATA), and change notifications
4266 are enabled. Even nothing really changed we still need to send this,
4267 in this case uChanged mask is just set to passed item mask. */
4268 if (lpItem && !isNew && (infoPtr->notify_mask & NOTIFY_MASK_ITEM_CHANGE))
4270 HWND hwndSelf = infoPtr->hwndSelf;
4272 if (notify_listview(infoPtr, LVN_ITEMCHANGING, &nmlv))
4273 return FALSE;
4274 if (!IsWindow(hwndSelf))
4275 return FALSE;
4278 /* When item is inserted we need to shift existing focus index if new item has lower index. */
4279 if (isNew && (stateMask & ~infoPtr->uCallbackMask & LVIS_FOCUSED) &&
4280 /* this means we won't hit a focus change path later */
4281 ((uChanged & LVIF_STATE) == 0 || (!(lpLVItem->state & LVIS_FOCUSED) && (infoPtr->nFocusedItem != lpLVItem->iItem))))
4283 if (infoPtr->nFocusedItem != -1 && (lpLVItem->iItem <= infoPtr->nFocusedItem))
4284 infoPtr->nFocusedItem++;
4287 if (!uChanged) return TRUE;
4288 *bChanged = TRUE;
4290 /* copy information */
4291 if (lpLVItem->mask & LVIF_TEXT)
4292 textsetptrT(&lpItem->hdr.pszText, lpLVItem->pszText, isW);
4294 if (lpLVItem->mask & LVIF_IMAGE)
4295 lpItem->hdr.iImage = lpLVItem->iImage;
4297 if (lpLVItem->mask & LVIF_PARAM)
4298 lpItem->lParam = lpLVItem->lParam;
4300 if (lpLVItem->mask & LVIF_INDENT)
4301 lpItem->iIndent = lpLVItem->iIndent;
4303 if (uChanged & LVIF_STATE)
4305 if (lpItem && (stateMask & ~infoPtr->uCallbackMask))
4307 lpItem->state &= ~stateMask;
4308 lpItem->state |= (lpLVItem->state & stateMask);
4310 if (lpLVItem->state & stateMask & ~infoPtr->uCallbackMask & LVIS_SELECTED)
4312 if (infoPtr->dwStyle & LVS_SINGLESEL) LISTVIEW_DeselectAllSkipItem(infoPtr, lpLVItem->iItem);
4313 ranges_additem(infoPtr->selectionRanges, lpLVItem->iItem);
4315 else if (stateMask & LVIS_SELECTED)
4317 ranges_delitem(infoPtr->selectionRanges, lpLVItem->iItem);
4319 /* If we are asked to change focus, and we manage it, do it.
4320 It's important to have all new item data stored at this point,
4321 because changing existing focus could result in a redrawing operation,
4322 which in turn could ask for disp data, application should see all data
4323 for inserted item when processing LVN_GETDISPINFO.
4325 The way this works application will see nested item change notifications -
4326 changed item notifications interrupted by ones from item losing focus. */
4327 if (stateMask & ~infoPtr->uCallbackMask & LVIS_FOCUSED)
4329 if (lpLVItem->state & LVIS_FOCUSED)
4331 /* update selection mark */
4332 if (infoPtr->nFocusedItem == -1 && infoPtr->nSelectionMark == -1)
4333 infoPtr->nSelectionMark = lpLVItem->iItem;
4335 if (infoPtr->nFocusedItem != -1)
4337 /* remove current focus */
4338 item.mask = LVIF_STATE;
4339 item.state = 0;
4340 item.stateMask = LVIS_FOCUSED;
4342 /* recurse with redrawing an item */
4343 LISTVIEW_SetItemState(infoPtr, infoPtr->nFocusedItem, &item);
4346 infoPtr->nFocusedItem = lpLVItem->iItem;
4347 LISTVIEW_EnsureVisible(infoPtr, lpLVItem->iItem, infoPtr->uView == LV_VIEW_LIST);
4349 else if (infoPtr->nFocusedItem == lpLVItem->iItem)
4351 infoPtr->nFocusedItem = -1;
4356 /* if we're inserting the item, we're done */
4357 if (isNew) return TRUE;
4359 /* send LVN_ITEMCHANGED notification */
4360 if (lpLVItem->mask & LVIF_PARAM) nmlv.lParam = lpLVItem->lParam;
4361 if (infoPtr->notify_mask & NOTIFY_MASK_ITEM_CHANGE)
4362 notify_listview(infoPtr, LVN_ITEMCHANGED, &nmlv);
4364 return TRUE;
4367 /***
4368 * DESCRIPTION:
4369 * Helper for LISTVIEW_{Set,Insert}ItemT *only*: sets subitem attributes.
4371 * PARAMETER(S):
4372 * [I] infoPtr : valid pointer to the listview structure
4373 * [I] lpLVItem : valid pointer to new subitem attributes
4374 * [I] isW : TRUE if lpLVItem is Unicode, FALSE if it's ANSI
4375 * [O] bChanged : will be set to TRUE if the item really changed
4377 * RETURN:
4378 * SUCCESS : TRUE
4379 * FAILURE : FALSE
4381 static BOOL set_sub_item(const LISTVIEW_INFO *infoPtr, const LVITEMW *lpLVItem, BOOL isW, BOOL *bChanged)
4383 HDPA hdpaSubItems;
4384 SUBITEM_INFO *lpSubItem;
4386 /* we do not support subitems for virtual listviews */
4387 if (infoPtr->dwStyle & LVS_OWNERDATA) return FALSE;
4389 /* set subitem only if column is present */
4390 if (lpLVItem->iSubItem >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return FALSE;
4392 /* First do some sanity checks */
4393 /* The LVIF_STATE flag is valid for subitems, but does not appear to be
4394 particularly useful. We currently do not actually do anything with
4395 the flag on subitems.
4397 if (lpLVItem->mask & ~(LVIF_TEXT | LVIF_IMAGE | LVIF_STATE | LVIF_DI_SETITEM)) return FALSE;
4398 if (!(lpLVItem->mask & (LVIF_TEXT | LVIF_IMAGE | LVIF_STATE))) return TRUE;
4400 /* get the subitem structure, and create it if not there */
4401 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, lpLVItem->iItem);
4402 assert (hdpaSubItems);
4404 lpSubItem = LISTVIEW_GetSubItemPtr(hdpaSubItems, lpLVItem->iSubItem);
4405 if (!lpSubItem)
4407 SUBITEM_INFO *tmpSubItem;
4408 INT i;
4410 lpSubItem = Alloc(sizeof(SUBITEM_INFO));
4411 if (!lpSubItem) return FALSE;
4412 /* we could binary search here, if need be...*/
4413 for (i = 1; i < DPA_GetPtrCount(hdpaSubItems); i++)
4415 tmpSubItem = DPA_GetPtr(hdpaSubItems, i);
4416 if (tmpSubItem->iSubItem > lpLVItem->iSubItem) break;
4418 if (DPA_InsertPtr(hdpaSubItems, i, lpSubItem) == -1)
4420 Free(lpSubItem);
4421 return FALSE;
4423 lpSubItem->iSubItem = lpLVItem->iSubItem;
4424 lpSubItem->hdr.iImage = I_IMAGECALLBACK;
4425 *bChanged = TRUE;
4428 if ((lpLVItem->mask & LVIF_IMAGE) && (lpSubItem->hdr.iImage != lpLVItem->iImage))
4430 lpSubItem->hdr.iImage = lpLVItem->iImage;
4431 *bChanged = TRUE;
4434 if ((lpLVItem->mask & LVIF_TEXT) && textcmpWT(lpSubItem->hdr.pszText, lpLVItem->pszText, isW))
4436 textsetptrT(&lpSubItem->hdr.pszText, lpLVItem->pszText, isW);
4437 *bChanged = TRUE;
4440 return TRUE;
4443 /***
4444 * DESCRIPTION:
4445 * Sets item attributes.
4447 * PARAMETER(S):
4448 * [I] infoPtr : valid pointer to the listview structure
4449 * [I] lpLVItem : new item attributes
4450 * [I] isW : TRUE if lpLVItem is Unicode, FALSE if it's ANSI
4452 * RETURN:
4453 * SUCCESS : TRUE
4454 * FAILURE : FALSE
4456 static BOOL LISTVIEW_SetItemT(LISTVIEW_INFO *infoPtr, LVITEMW *lpLVItem, BOOL isW)
4458 HWND hwndSelf = infoPtr->hwndSelf;
4459 LPWSTR pszText = NULL;
4460 BOOL bResult, bChanged = FALSE;
4461 RECT oldItemArea;
4463 TRACE("(lpLVItem=%s, isW=%d)\n", debuglvitem_t(lpLVItem, isW), isW);
4465 if (!lpLVItem || lpLVItem->iItem < 0 || lpLVItem->iItem >= infoPtr->nItemCount)
4466 return FALSE;
4468 /* Store old item area */
4469 LISTVIEW_GetItemBox(infoPtr, lpLVItem->iItem, &oldItemArea);
4471 /* For efficiency, we transform the lpLVItem->pszText to Unicode here */
4472 if ((lpLVItem->mask & LVIF_TEXT) && is_text(lpLVItem->pszText))
4474 pszText = lpLVItem->pszText;
4475 lpLVItem->pszText = textdupTtoW(lpLVItem->pszText, isW);
4478 /* actually set the fields */
4479 if (!is_assignable_item(lpLVItem, infoPtr->dwStyle)) return FALSE;
4481 if (lpLVItem->iSubItem)
4482 bResult = set_sub_item(infoPtr, lpLVItem, TRUE, &bChanged);
4483 else
4484 bResult = set_main_item(infoPtr, lpLVItem, FALSE, TRUE, &bChanged);
4485 if (!IsWindow(hwndSelf))
4486 return FALSE;
4488 /* redraw item, if necessary */
4489 if (bChanged && !infoPtr->bIsDrawing)
4491 /* this little optimization eliminates some nasty flicker */
4492 if ( infoPtr->uView == LV_VIEW_DETAILS && !(infoPtr->dwStyle & LVS_OWNERDRAWFIXED) &&
4493 !(infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT) &&
4494 lpLVItem->iSubItem > 0 && lpLVItem->iSubItem <= DPA_GetPtrCount(infoPtr->hdpaColumns) )
4495 LISTVIEW_InvalidateSubItem(infoPtr, lpLVItem->iItem, lpLVItem->iSubItem);
4496 else
4498 LISTVIEW_InvalidateRect(infoPtr, &oldItemArea);
4499 LISTVIEW_InvalidateItem(infoPtr, lpLVItem->iItem);
4502 /* restore text */
4503 if (pszText)
4505 textfreeT(lpLVItem->pszText, isW);
4506 lpLVItem->pszText = pszText;
4509 return bResult;
4512 /***
4513 * DESCRIPTION:
4514 * Retrieves the index of the item at coordinate (0, 0) of the client area.
4516 * PARAMETER(S):
4517 * [I] infoPtr : valid pointer to the listview structure
4519 * RETURN:
4520 * item index
4522 static INT LISTVIEW_GetTopIndex(const LISTVIEW_INFO *infoPtr)
4524 INT nItem = 0;
4525 SCROLLINFO scrollInfo;
4527 scrollInfo.cbSize = sizeof(SCROLLINFO);
4528 scrollInfo.fMask = SIF_POS;
4530 if (infoPtr->uView == LV_VIEW_LIST)
4532 if (GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo))
4533 nItem = scrollInfo.nPos * LISTVIEW_GetCountPerColumn(infoPtr);
4535 else if (infoPtr->uView == LV_VIEW_DETAILS)
4537 if (GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo))
4538 nItem = scrollInfo.nPos;
4540 else
4542 if (GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo))
4543 nItem = LISTVIEW_GetCountPerRow(infoPtr) * (scrollInfo.nPos / infoPtr->nItemHeight);
4546 TRACE("nItem=%d\n", nItem);
4548 return nItem;
4552 /***
4553 * DESCRIPTION:
4554 * Erases the background of the given rectangle
4556 * PARAMETER(S):
4557 * [I] infoPtr : valid pointer to the listview structure
4558 * [I] hdc : device context handle
4559 * [I] lprcBox : clipping rectangle
4561 * RETURN:
4562 * Success: TRUE
4563 * Failure: FALSE
4565 static inline BOOL LISTVIEW_FillBkgnd(const LISTVIEW_INFO *infoPtr, HDC hdc, const RECT *lprcBox)
4567 if (!infoPtr->hBkBrush) return FALSE;
4569 TRACE("(hdc=%p, lprcBox=%s, hBkBrush=%p)\n", hdc, wine_dbgstr_rect(lprcBox), infoPtr->hBkBrush);
4571 return FillRect(hdc, lprcBox, infoPtr->hBkBrush);
4574 /* Draw main item or subitem */
4575 static void LISTVIEW_DrawItemPart(LISTVIEW_INFO *infoPtr, LVITEMW *item, const NMLVCUSTOMDRAW *nmlvcd, const POINT *pos)
4577 RECT rcSelect, rcLabel, rcBox, rcStateIcon, rcIcon;
4578 const RECT *background;
4579 HIMAGELIST himl;
4580 UINT format;
4581 RECT *focus;
4583 /* now check if we need to update the focus rectangle */
4584 focus = infoPtr->bFocus && (item->state & LVIS_FOCUSED) ? &infoPtr->rcFocus : 0;
4585 if (!focus) item->state &= ~LVIS_FOCUSED;
4587 LISTVIEW_GetItemMetrics(infoPtr, item, &rcBox, &rcSelect, &rcIcon, &rcStateIcon, &rcLabel);
4588 OffsetRect(&rcBox, pos->x, pos->y);
4589 OffsetRect(&rcSelect, pos->x, pos->y);
4590 OffsetRect(&rcIcon, pos->x, pos->y);
4591 OffsetRect(&rcStateIcon, pos->x, pos->y);
4592 OffsetRect(&rcLabel, pos->x, pos->y);
4593 TRACE("%d: box=%s, select=%s, icon=%s. label=%s\n", item->iSubItem,
4594 wine_dbgstr_rect(&rcBox), wine_dbgstr_rect(&rcSelect),
4595 wine_dbgstr_rect(&rcIcon), wine_dbgstr_rect(&rcLabel));
4597 /* FIXME: temporary hack */
4598 rcSelect.left = rcLabel.left;
4600 if (infoPtr->uView == LV_VIEW_DETAILS && item->iSubItem == 0)
4602 if (!(infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT))
4603 OffsetRect(&rcSelect, LISTVIEW_GetColumnInfo(infoPtr, 0)->rcHeader.left, 0);
4604 OffsetRect(&rcIcon, LISTVIEW_GetColumnInfo(infoPtr, 0)->rcHeader.left, 0);
4605 OffsetRect(&rcStateIcon, LISTVIEW_GetColumnInfo(infoPtr, 0)->rcHeader.left, 0);
4606 OffsetRect(&rcLabel, LISTVIEW_GetColumnInfo(infoPtr, 0)->rcHeader.left, 0);
4609 /* in icon mode, the label rect is really what we want to draw the
4610 * background for */
4611 /* in detail mode, we want to paint background for label rect when
4612 * item is not selected or listview has full row select; otherwise paint
4613 * background for text only */
4614 if ( infoPtr->uView == LV_VIEW_ICON ||
4615 (infoPtr->uView == LV_VIEW_DETAILS && (!(item->state & LVIS_SELECTED) ||
4616 (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT))))
4617 background = &rcLabel;
4618 else
4619 background = &rcSelect;
4621 if (nmlvcd->clrTextBk != CLR_NONE)
4622 ExtTextOutW(nmlvcd->nmcd.hdc, background->left, background->top, ETO_OPAQUE, background, NULL, 0, NULL);
4624 if (item->state & LVIS_FOCUSED)
4626 if (infoPtr->uView == LV_VIEW_DETAILS)
4628 if (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT)
4630 /* we have to update left focus bound too if item isn't in leftmost column
4631 and reduce right box bound */
4632 if (DPA_GetPtrCount(infoPtr->hdpaColumns) > 0)
4634 INT leftmost;
4636 if ((leftmost = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX, 0, 0)))
4638 INT Originx = pos->x - LISTVIEW_GetColumnInfo(infoPtr, leftmost)->rcHeader.left;
4639 INT rightmost = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX,
4640 DPA_GetPtrCount(infoPtr->hdpaColumns) - 1, 0);
4642 rcBox.right = LISTVIEW_GetColumnInfo(infoPtr, rightmost)->rcHeader.right + Originx;
4643 rcSelect.left = LISTVIEW_GetColumnInfo(infoPtr, leftmost)->rcHeader.left + Originx;
4646 rcSelect.right = rcBox.right;
4648 infoPtr->rcFocus = rcSelect;
4650 else
4651 infoPtr->rcFocus = rcLabel;
4654 /* state icons */
4655 if (infoPtr->himlState && STATEIMAGEINDEX(item->state) && (item->iSubItem == 0))
4657 UINT stateimage = STATEIMAGEINDEX(item->state);
4658 if (stateimage)
4660 TRACE("stateimage=%d\n", stateimage);
4661 ImageList_Draw(infoPtr->himlState, stateimage-1, nmlvcd->nmcd.hdc, rcStateIcon.left, rcStateIcon.top, ILD_NORMAL);
4665 /* item icons */
4666 himl = (infoPtr->uView == LV_VIEW_ICON ? infoPtr->himlNormal : infoPtr->himlSmall);
4667 if (himl && item->iImage >= 0 && !IsRectEmpty(&rcIcon))
4669 UINT style;
4671 TRACE("iImage=%d\n", item->iImage);
4673 if (item->state & (LVIS_SELECTED | LVIS_CUT) && infoPtr->bFocus)
4674 style = ILD_SELECTED;
4675 else
4676 style = ILD_NORMAL;
4678 ImageList_DrawEx(himl, item->iImage, nmlvcd->nmcd.hdc, rcIcon.left, rcIcon.top,
4679 rcIcon.right - rcIcon.left, rcIcon.bottom - rcIcon.top, infoPtr->clrBk,
4680 item->state & LVIS_CUT ? RGB(255, 255, 255) : CLR_DEFAULT,
4681 style | (item->state & LVIS_OVERLAYMASK));
4684 /* Don't bother painting item being edited */
4685 if (infoPtr->hwndEdit && item->iItem == infoPtr->nEditLabelItem && item->iSubItem == 0) return;
4687 /* figure out the text drawing flags */
4688 format = (infoPtr->uView == LV_VIEW_ICON ? (focus ? LV_FL_DT_FLAGS : LV_ML_DT_FLAGS) : LV_SL_DT_FLAGS);
4689 if (infoPtr->uView == LV_VIEW_ICON)
4690 format = (focus ? LV_FL_DT_FLAGS : LV_ML_DT_FLAGS);
4691 else if (item->iSubItem)
4693 switch (LISTVIEW_GetColumnInfo(infoPtr, item->iSubItem)->fmt & LVCFMT_JUSTIFYMASK)
4695 case LVCFMT_RIGHT: format |= DT_RIGHT; break;
4696 case LVCFMT_CENTER: format |= DT_CENTER; break;
4697 default: format |= DT_LEFT;
4700 if (!(format & (DT_RIGHT | DT_CENTER)))
4702 if (himl && item->iImage >= 0 && !IsRectEmpty(&rcIcon)) rcLabel.left += IMAGE_PADDING;
4703 else rcLabel.left += LABEL_HOR_PADDING;
4705 else if (format & DT_RIGHT) rcLabel.right -= LABEL_HOR_PADDING;
4707 /* for GRIDLINES reduce the bottom so the text formats correctly */
4708 if (infoPtr->uView == LV_VIEW_DETAILS && infoPtr->dwLvExStyle & LVS_EX_GRIDLINES)
4709 rcLabel.bottom--;
4711 DrawTextW(nmlvcd->nmcd.hdc, item->pszText, -1, &rcLabel, format);
4714 /***
4715 * DESCRIPTION:
4716 * Draws an item.
4718 * PARAMETER(S):
4719 * [I] infoPtr : valid pointer to the listview structure
4720 * [I] hdc : device context handle
4721 * [I] nItem : item index
4722 * [I] nSubItem : subitem index
4723 * [I] pos : item position in client coordinates
4724 * [I] cdmode : custom draw mode
4726 * RETURN:
4727 * Success: TRUE
4728 * Failure: FALSE
4730 static BOOL LISTVIEW_DrawItem(LISTVIEW_INFO *infoPtr, HDC hdc, INT nItem, ITERATOR *subitems, POINT pos, DWORD cdmode)
4732 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
4733 static WCHAR callbackW[] = { '(', 'c', 'a', 'l', 'l', 'b', 'a', 'c', 'k', ')', 0 };
4734 DWORD cdsubitemmode = CDRF_DODEFAULT;
4735 RECT *focus, rcBox;
4736 NMLVCUSTOMDRAW nmlvcd;
4737 LVITEMW lvItem;
4739 TRACE("(hdc=%p, nItem=%d, subitems=%p, pos=%s)\n", hdc, nItem, subitems, wine_dbgstr_point(&pos));
4741 /* get information needed for drawing the item */
4742 lvItem.mask = LVIF_TEXT | LVIF_IMAGE | LVIF_PARAM | LVIF_STATE;
4743 if (infoPtr->uView == LV_VIEW_DETAILS) lvItem.mask |= LVIF_INDENT;
4744 lvItem.stateMask = LVIS_SELECTED | LVIS_FOCUSED | LVIS_STATEIMAGEMASK | LVIS_CUT | LVIS_OVERLAYMASK;
4745 lvItem.iItem = nItem;
4746 lvItem.iSubItem = 0;
4747 lvItem.state = 0;
4748 lvItem.lParam = 0;
4749 lvItem.cchTextMax = DISP_TEXT_SIZE;
4750 lvItem.pszText = szDispText;
4751 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) return FALSE;
4752 if (lvItem.pszText == LPSTR_TEXTCALLBACKW) lvItem.pszText = callbackW;
4753 TRACE(" lvItem=%s\n", debuglvitem_t(&lvItem, TRUE));
4755 /* now check if we need to update the focus rectangle */
4756 focus = infoPtr->bFocus && (lvItem.state & LVIS_FOCUSED) ? &infoPtr->rcFocus : 0;
4757 if (!focus) lvItem.state &= ~LVIS_FOCUSED;
4759 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, &rcBox, NULL, NULL, NULL, NULL);
4760 OffsetRect(&rcBox, pos.x, pos.y);
4762 /* Full custom draw stage sequence looks like this:
4764 LV_VIEW_DETAILS:
4766 - CDDS_ITEMPREPAINT
4767 - CDDS_ITEMPREPAINT|CDDS_SUBITEM | => sent n times, where n is number of subitems,
4768 CDDS_ITEMPOSTPAINT|CDDS_SUBITEM | including item itself
4769 - CDDS_ITEMPOSTPAINT
4771 other styles:
4773 - CDDS_ITEMPREPAINT
4774 - CDDS_ITEMPOSTPAINT
4777 /* fill in the custom draw structure */
4778 customdraw_fill(&nmlvcd, infoPtr, hdc, &rcBox, &lvItem);
4779 if (cdmode & CDRF_NOTIFYITEMDRAW)
4780 cdsubitemmode = notify_customdraw(infoPtr, CDDS_ITEMPREPAINT, &nmlvcd);
4781 if (cdsubitemmode & CDRF_SKIPDEFAULT) goto postpaint;
4783 if (subitems)
4785 while (iterator_next(subitems))
4787 DWORD subitemstage = CDRF_DODEFAULT;
4789 /* We need to query for each subitem, item's data (subitem == 0) is already here at this point */
4790 if (subitems->nItem)
4792 lvItem.mask = LVIF_TEXT | LVIF_IMAGE | LVIF_PARAM | LVIF_INDENT;
4793 lvItem.stateMask = LVIS_SELECTED | LVIS_FOCUSED | LVIS_STATEIMAGEMASK | LVIS_CUT | LVIS_OVERLAYMASK;
4794 lvItem.iItem = nItem;
4795 lvItem.iSubItem = subitems->nItem;
4796 lvItem.state = 0;
4797 lvItem.lParam = 0;
4798 lvItem.cchTextMax = DISP_TEXT_SIZE;
4799 lvItem.pszText = szDispText;
4800 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) return FALSE;
4801 if (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT)
4802 lvItem.state = LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED);
4803 if (lvItem.pszText == LPSTR_TEXTCALLBACKW) lvItem.pszText = callbackW;
4804 TRACE(" lvItem=%s\n", debuglvitem_t(&lvItem, TRUE));
4806 /* update custom draw data */
4807 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, &nmlvcd.nmcd.rc, NULL, NULL, NULL, NULL);
4808 OffsetRect(&nmlvcd.nmcd.rc, pos.x, pos.y);
4809 nmlvcd.iSubItem = subitems->nItem;
4812 if (cdsubitemmode & CDRF_NOTIFYSUBITEMDRAW)
4813 subitemstage = notify_customdraw(infoPtr, CDDS_SUBITEM | CDDS_ITEMPREPAINT, &nmlvcd);
4814 else
4816 nmlvcd.clrTextBk = infoPtr->clrTextBk;
4817 nmlvcd.clrText = infoPtr->clrText;
4820 if (subitems->nItem == 0 || (cdmode & CDRF_NOTIFYITEMDRAW))
4821 prepaint_setup(infoPtr, hdc, &nmlvcd, FALSE);
4822 else if (!(infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT))
4823 prepaint_setup(infoPtr, hdc, &nmlvcd, TRUE);
4825 if (!(subitemstage & CDRF_SKIPDEFAULT))
4826 LISTVIEW_DrawItemPart(infoPtr, &lvItem, &nmlvcd, &pos);
4828 if (subitemstage & CDRF_NOTIFYPOSTPAINT)
4829 subitemstage = notify_customdraw(infoPtr, CDDS_SUBITEM | CDDS_ITEMPOSTPAINT, &nmlvcd);
4832 else
4834 prepaint_setup(infoPtr, hdc, &nmlvcd, FALSE);
4835 LISTVIEW_DrawItemPart(infoPtr, &lvItem, &nmlvcd, &pos);
4838 postpaint:
4839 if (cdsubitemmode & CDRF_NOTIFYPOSTPAINT)
4841 nmlvcd.iSubItem = 0;
4842 notify_customdraw(infoPtr, CDDS_ITEMPOSTPAINT, &nmlvcd);
4845 return TRUE;
4848 /***
4849 * DESCRIPTION:
4850 * Draws listview items when in owner draw mode.
4852 * PARAMETER(S):
4853 * [I] infoPtr : valid pointer to the listview structure
4854 * [I] hdc : device context handle
4856 * RETURN:
4857 * None
4859 static void LISTVIEW_RefreshOwnerDraw(const LISTVIEW_INFO *infoPtr, ITERATOR *i, HDC hdc, DWORD cdmode)
4861 UINT uID = (UINT)GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
4862 DWORD cditemmode = CDRF_DODEFAULT;
4863 NMLVCUSTOMDRAW nmlvcd;
4864 POINT Origin, Position;
4865 DRAWITEMSTRUCT dis;
4866 LVITEMW item;
4868 TRACE("()\n");
4870 ZeroMemory(&dis, sizeof(dis));
4872 /* Get scroll info once before loop */
4873 LISTVIEW_GetOrigin(infoPtr, &Origin);
4875 /* iterate through the invalidated rows */
4876 while(iterator_next(i))
4878 item.iItem = i->nItem;
4879 item.iSubItem = 0;
4880 item.mask = LVIF_PARAM | LVIF_STATE;
4881 item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
4882 if (!LISTVIEW_GetItemW(infoPtr, &item)) continue;
4884 dis.CtlType = ODT_LISTVIEW;
4885 dis.CtlID = uID;
4886 dis.itemID = item.iItem;
4887 dis.itemAction = ODA_DRAWENTIRE;
4888 dis.itemState = 0;
4889 if (item.state & LVIS_SELECTED) dis.itemState |= ODS_SELECTED;
4890 if (infoPtr->bFocus && (item.state & LVIS_FOCUSED)) dis.itemState |= ODS_FOCUS;
4891 dis.hwndItem = infoPtr->hwndSelf;
4892 dis.hDC = hdc;
4893 LISTVIEW_GetItemOrigin(infoPtr, dis.itemID, &Position);
4894 dis.rcItem.left = Position.x + Origin.x;
4895 dis.rcItem.right = dis.rcItem.left + infoPtr->nItemWidth;
4896 dis.rcItem.top = Position.y + Origin.y;
4897 dis.rcItem.bottom = dis.rcItem.top + infoPtr->nItemHeight;
4898 dis.itemData = item.lParam;
4900 TRACE("item=%s, rcItem=%s\n", debuglvitem_t(&item, TRUE), wine_dbgstr_rect(&dis.rcItem));
4903 * Even if we do not send the CDRF_NOTIFYITEMDRAW we need to fill the nmlvcd
4904 * structure for the rest. of the paint cycle
4906 customdraw_fill(&nmlvcd, infoPtr, hdc, &dis.rcItem, &item);
4907 if (cdmode & CDRF_NOTIFYITEMDRAW)
4908 cditemmode = notify_customdraw(infoPtr, CDDS_PREPAINT, &nmlvcd);
4910 if (!(cditemmode & CDRF_SKIPDEFAULT))
4912 prepaint_setup (infoPtr, hdc, &nmlvcd, FALSE);
4913 SendMessageW(infoPtr->hwndNotify, WM_DRAWITEM, dis.CtlID, (LPARAM)&dis);
4916 if (cditemmode & CDRF_NOTIFYPOSTPAINT)
4917 notify_postpaint(infoPtr, &nmlvcd);
4921 /***
4922 * DESCRIPTION:
4923 * Draws listview items when in report display mode.
4925 * PARAMETER(S):
4926 * [I] infoPtr : valid pointer to the listview structure
4927 * [I] hdc : device context handle
4928 * [I] cdmode : custom draw mode
4930 * RETURN:
4931 * None
4933 static void LISTVIEW_RefreshReport(LISTVIEW_INFO *infoPtr, ITERATOR *i, HDC hdc, DWORD cdmode)
4935 INT rgntype;
4936 RECT rcClip, rcItem;
4937 POINT Origin;
4938 RANGES colRanges;
4939 INT col;
4940 ITERATOR j;
4942 TRACE("()\n");
4944 /* figure out what to draw */
4945 rgntype = GetClipBox(hdc, &rcClip);
4946 if (rgntype == NULLREGION) return;
4948 /* Get scroll info once before loop */
4949 LISTVIEW_GetOrigin(infoPtr, &Origin);
4951 colRanges = ranges_create(DPA_GetPtrCount(infoPtr->hdpaColumns));
4953 /* narrow down the columns we need to paint */
4954 for(col = 0; col < DPA_GetPtrCount(infoPtr->hdpaColumns); col++)
4956 INT index = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX, col, 0);
4958 LISTVIEW_GetHeaderRect(infoPtr, index, &rcItem);
4959 if ((rcItem.right + Origin.x >= rcClip.left) && (rcItem.left + Origin.x < rcClip.right))
4960 ranges_additem(colRanges, index);
4962 iterator_rangesitems(&j, colRanges);
4964 /* in full row select, we _have_ to draw the main item */
4965 if (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT)
4966 j.nSpecial = 0;
4968 /* iterate through the invalidated rows */
4969 while(iterator_next(i))
4971 RANGES subitems;
4972 POINT Position;
4973 ITERATOR k;
4975 SelectObject(hdc, infoPtr->hFont);
4976 LISTVIEW_GetItemOrigin(infoPtr, i->nItem, &Position);
4977 Position.x = Origin.x;
4978 Position.y += Origin.y;
4980 subitems = ranges_create(DPA_GetPtrCount(infoPtr->hdpaColumns));
4982 /* iterate through the invalidated columns */
4983 while(iterator_next(&j))
4985 LISTVIEW_GetHeaderRect(infoPtr, j.nItem, &rcItem);
4987 if (rgntype == COMPLEXREGION && !((infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT) && j.nItem == 0))
4989 rcItem.top = 0;
4990 rcItem.bottom = infoPtr->nItemHeight;
4991 OffsetRect(&rcItem, Origin.x, Position.y);
4992 if (!RectVisible(hdc, &rcItem)) continue;
4995 ranges_additem(subitems, j.nItem);
4998 iterator_rangesitems(&k, subitems);
4999 LISTVIEW_DrawItem(infoPtr, hdc, i->nItem, &k, Position, cdmode);
5000 iterator_destroy(&k);
5002 iterator_destroy(&j);
5005 /***
5006 * DESCRIPTION:
5007 * Draws the gridlines if necessary when in report display mode.
5009 * PARAMETER(S):
5010 * [I] infoPtr : valid pointer to the listview structure
5011 * [I] hdc : device context handle
5013 * RETURN:
5014 * None
5016 static void LISTVIEW_RefreshReportGrid(LISTVIEW_INFO *infoPtr, HDC hdc)
5018 INT rgntype;
5019 INT y, itemheight;
5020 INT col, index;
5021 HPEN hPen, hOldPen;
5022 RECT rcClip, rcItem = {0};
5023 POINT Origin;
5024 RANGES colRanges;
5025 ITERATOR j;
5026 BOOL rmost = FALSE;
5028 TRACE("()\n");
5030 /* figure out what to draw */
5031 rgntype = GetClipBox(hdc, &rcClip);
5032 if (rgntype == NULLREGION) return;
5034 /* Get scroll info once before loop */
5035 LISTVIEW_GetOrigin(infoPtr, &Origin);
5037 colRanges = ranges_create(DPA_GetPtrCount(infoPtr->hdpaColumns));
5039 /* narrow down the columns we need to paint */
5040 for(col = 0; col < DPA_GetPtrCount(infoPtr->hdpaColumns); col++)
5042 index = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX, col, 0);
5044 LISTVIEW_GetHeaderRect(infoPtr, index, &rcItem);
5045 if ((rcItem.right + Origin.x >= rcClip.left) && (rcItem.left + Origin.x < rcClip.right))
5046 ranges_additem(colRanges, index);
5049 /* is right most vertical line visible? */
5050 if (DPA_GetPtrCount(infoPtr->hdpaColumns) > 0)
5052 index = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX, DPA_GetPtrCount(infoPtr->hdpaColumns) - 1, 0);
5053 LISTVIEW_GetHeaderRect(infoPtr, index, &rcItem);
5054 rmost = (rcItem.right + Origin.x < rcClip.right);
5057 if ((hPen = CreatePen( PS_SOLID, 1, comctl32_color.clr3dFace )))
5059 hOldPen = SelectObject ( hdc, hPen );
5061 /* draw the vertical lines for the columns */
5062 iterator_rangesitems(&j, colRanges);
5063 while(iterator_next(&j))
5065 LISTVIEW_GetHeaderRect(infoPtr, j.nItem, &rcItem);
5066 if (rcItem.left == 0) continue; /* skip leftmost column */
5067 rcItem.left += Origin.x;
5068 rcItem.right += Origin.x;
5069 rcItem.top = infoPtr->rcList.top;
5070 rcItem.bottom = infoPtr->rcList.bottom;
5071 TRACE("vert col=%d, rcItem=%s\n", j.nItem, wine_dbgstr_rect(&rcItem));
5072 MoveToEx (hdc, rcItem.left, rcItem.top, NULL);
5073 LineTo (hdc, rcItem.left, rcItem.bottom);
5075 iterator_destroy(&j);
5076 /* draw rightmost grid line if visible */
5077 if (rmost)
5079 index = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX,
5080 DPA_GetPtrCount(infoPtr->hdpaColumns) - 1, 0);
5081 LISTVIEW_GetHeaderRect(infoPtr, index, &rcItem);
5083 rcItem.right += Origin.x;
5085 MoveToEx (hdc, rcItem.right, infoPtr->rcList.top, NULL);
5086 LineTo (hdc, rcItem.right, infoPtr->rcList.bottom);
5089 /* draw the horizontal lines for the rows */
5090 itemheight = LISTVIEW_CalculateItemHeight(infoPtr);
5091 rcItem.left = infoPtr->rcList.left;
5092 rcItem.right = infoPtr->rcList.right;
5093 for(y = Origin.y > 1 ? Origin.y - 1 : itemheight - 1 + Origin.y % itemheight; y<=infoPtr->rcList.bottom; y+=itemheight)
5095 rcItem.bottom = rcItem.top = y;
5096 TRACE("horz rcItem=%s\n", wine_dbgstr_rect(&rcItem));
5097 MoveToEx (hdc, rcItem.left, rcItem.top, NULL);
5098 LineTo (hdc, rcItem.right, rcItem.top);
5101 SelectObject( hdc, hOldPen );
5102 DeleteObject( hPen );
5104 else
5105 ranges_destroy(colRanges);
5108 /***
5109 * DESCRIPTION:
5110 * Draws listview items when in list display mode.
5112 * PARAMETER(S):
5113 * [I] infoPtr : valid pointer to the listview structure
5114 * [I] hdc : device context handle
5115 * [I] cdmode : custom draw mode
5117 * RETURN:
5118 * None
5120 static void LISTVIEW_RefreshList(LISTVIEW_INFO *infoPtr, ITERATOR *i, HDC hdc, DWORD cdmode)
5122 POINT Origin, Position;
5124 /* Get scroll info once before loop */
5125 LISTVIEW_GetOrigin(infoPtr, &Origin);
5127 while(iterator_prev(i))
5129 SelectObject(hdc, infoPtr->hFont);
5130 LISTVIEW_GetItemOrigin(infoPtr, i->nItem, &Position);
5131 Position.x += Origin.x;
5132 Position.y += Origin.y;
5134 LISTVIEW_DrawItem(infoPtr, hdc, i->nItem, NULL, Position, cdmode);
5139 /***
5140 * DESCRIPTION:
5141 * Draws listview items.
5143 * PARAMETER(S):
5144 * [I] infoPtr : valid pointer to the listview structure
5145 * [I] hdc : device context handle
5146 * [I] prcErase : rect to be erased before refresh (may be NULL)
5148 * RETURN:
5149 * NoneX
5151 static void LISTVIEW_Refresh(LISTVIEW_INFO *infoPtr, HDC hdc, const RECT *prcErase)
5153 COLORREF oldTextColor = 0, oldBkColor = 0;
5154 NMLVCUSTOMDRAW nmlvcd;
5155 HFONT hOldFont = 0;
5156 DWORD cdmode;
5157 INT oldBkMode = 0;
5158 RECT rcClient;
5159 ITERATOR i;
5160 HDC hdcOrig = hdc;
5161 HBITMAP hbmp = NULL;
5162 RANGE range;
5164 LISTVIEW_DUMP(infoPtr);
5166 if (infoPtr->dwLvExStyle & LVS_EX_DOUBLEBUFFER) {
5167 TRACE("double buffering\n");
5169 hdc = CreateCompatibleDC(hdcOrig);
5170 if (!hdc) {
5171 ERR("Failed to create DC for backbuffer\n");
5172 return;
5174 hbmp = CreateCompatibleBitmap(hdcOrig, infoPtr->rcList.right,
5175 infoPtr->rcList.bottom);
5176 if (!hbmp) {
5177 ERR("Failed to create bitmap for backbuffer\n");
5178 DeleteDC(hdc);
5179 return;
5182 SelectObject(hdc, hbmp);
5183 SelectObject(hdc, infoPtr->hFont);
5185 if(GetClipBox(hdcOrig, &rcClient))
5186 IntersectClipRect(hdc, rcClient.left, rcClient.top, rcClient.right, rcClient.bottom);
5187 } else {
5188 /* Save dc values we're gonna trash while drawing
5189 * FIXME: Should be done in LISTVIEW_DrawItem() */
5190 hOldFont = SelectObject(hdc, infoPtr->hFont);
5191 oldBkMode = GetBkMode(hdc);
5192 oldBkColor = GetBkColor(hdc);
5193 oldTextColor = GetTextColor(hdc);
5196 infoPtr->bIsDrawing = TRUE;
5198 if (prcErase) {
5199 LISTVIEW_FillBkgnd(infoPtr, hdc, prcErase);
5200 } else if (infoPtr->dwLvExStyle & LVS_EX_DOUBLEBUFFER) {
5201 /* If no erasing was done (usually because RedrawWindow was called
5202 * with RDW_INVALIDATE only) we need to copy the old contents into
5203 * the backbuffer before continuing. */
5204 BitBlt(hdc, infoPtr->rcList.left, infoPtr->rcList.top,
5205 infoPtr->rcList.right - infoPtr->rcList.left,
5206 infoPtr->rcList.bottom - infoPtr->rcList.top,
5207 hdcOrig, infoPtr->rcList.left, infoPtr->rcList.top, SRCCOPY);
5210 GetClientRect(infoPtr->hwndSelf, &rcClient);
5211 customdraw_fill(&nmlvcd, infoPtr, hdc, &rcClient, 0);
5212 cdmode = notify_customdraw(infoPtr, CDDS_PREPAINT, &nmlvcd);
5213 if (cdmode & CDRF_SKIPDEFAULT) goto enddraw;
5215 /* nothing to draw */
5216 if(infoPtr->nItemCount == 0) goto enddraw;
5218 /* figure out what we need to draw */
5219 iterator_visibleitems(&i, infoPtr, hdc);
5220 range = iterator_range(&i);
5222 /* send cache hint notification */
5223 if (infoPtr->dwStyle & LVS_OWNERDATA)
5225 NMLVCACHEHINT nmlv;
5227 ZeroMemory(&nmlv, sizeof(NMLVCACHEHINT));
5228 nmlv.iFrom = range.lower;
5229 nmlv.iTo = range.upper - 1;
5230 notify_hdr(infoPtr, LVN_ODCACHEHINT, &nmlv.hdr);
5233 if ((infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && (infoPtr->uView == LV_VIEW_DETAILS))
5234 LISTVIEW_RefreshOwnerDraw(infoPtr, &i, hdc, cdmode);
5235 else
5237 if (infoPtr->uView == LV_VIEW_DETAILS)
5238 LISTVIEW_RefreshReport(infoPtr, &i, hdc, cdmode);
5239 else /* LV_VIEW_LIST, LV_VIEW_ICON or LV_VIEW_SMALLICON */
5240 LISTVIEW_RefreshList(infoPtr, &i, hdc, cdmode);
5242 /* if we have a focus rect and it's visible, draw it */
5243 if (infoPtr->bFocus && range.lower <= infoPtr->nFocusedItem &&
5244 (range.upper - 1) >= infoPtr->nFocusedItem)
5245 LISTVIEW_DrawFocusRect(infoPtr, hdc);
5247 iterator_destroy(&i);
5249 enddraw:
5250 /* For LVS_EX_GRIDLINES go and draw lines */
5251 /* This includes the case where there were *no* items */
5252 if ((infoPtr->uView == LV_VIEW_DETAILS) && infoPtr->dwLvExStyle & LVS_EX_GRIDLINES)
5253 LISTVIEW_RefreshReportGrid(infoPtr, hdc);
5255 /* Draw marquee rectangle if appropriate */
5256 if (infoPtr->bMarqueeSelect)
5257 DrawFocusRect(hdc, &infoPtr->marqueeDrawRect);
5259 if (cdmode & CDRF_NOTIFYPOSTPAINT)
5260 notify_postpaint(infoPtr, &nmlvcd);
5262 if(hbmp) {
5263 BitBlt(hdcOrig, infoPtr->rcList.left, infoPtr->rcList.top,
5264 infoPtr->rcList.right - infoPtr->rcList.left,
5265 infoPtr->rcList.bottom - infoPtr->rcList.top,
5266 hdc, infoPtr->rcList.left, infoPtr->rcList.top, SRCCOPY);
5268 DeleteObject(hbmp);
5269 DeleteDC(hdc);
5270 } else {
5271 SelectObject(hdc, hOldFont);
5272 SetBkMode(hdc, oldBkMode);
5273 SetBkColor(hdc, oldBkColor);
5274 SetTextColor(hdc, oldTextColor);
5277 infoPtr->bIsDrawing = FALSE;
5281 /***
5282 * DESCRIPTION:
5283 * Calculates the approximate width and height of a given number of items.
5285 * PARAMETER(S):
5286 * [I] infoPtr : valid pointer to the listview structure
5287 * [I] nItemCount : number of items
5288 * [I] wWidth : width
5289 * [I] wHeight : height
5291 * RETURN:
5292 * Returns a DWORD. The width in the low word and the height in high word.
5294 static DWORD LISTVIEW_ApproximateViewRect(const LISTVIEW_INFO *infoPtr, INT nItemCount,
5295 WORD wWidth, WORD wHeight)
5297 DWORD dwViewRect = 0;
5299 if (nItemCount == -1)
5300 nItemCount = infoPtr->nItemCount;
5302 if (infoPtr->uView == LV_VIEW_LIST)
5304 INT nItemCountPerColumn = 1;
5305 INT nColumnCount = 0;
5307 if (wHeight == 0xFFFF)
5309 /* use current height */
5310 wHeight = infoPtr->rcList.bottom - infoPtr->rcList.top;
5313 if (wHeight < infoPtr->nItemHeight)
5314 wHeight = infoPtr->nItemHeight;
5316 if (nItemCount > 0)
5318 if (infoPtr->nItemHeight > 0)
5320 nItemCountPerColumn = wHeight / infoPtr->nItemHeight;
5321 if (nItemCountPerColumn == 0)
5322 nItemCountPerColumn = 1;
5324 if (nItemCount % nItemCountPerColumn != 0)
5325 nColumnCount = nItemCount / nItemCountPerColumn;
5326 else
5327 nColumnCount = nItemCount / nItemCountPerColumn + 1;
5331 /* Microsoft padding magic */
5332 wHeight = nItemCountPerColumn * infoPtr->nItemHeight + 2;
5333 wWidth = nColumnCount * infoPtr->nItemWidth + 2;
5335 dwViewRect = MAKELONG(wWidth, wHeight);
5337 else if (infoPtr->uView == LV_VIEW_DETAILS)
5339 RECT rcBox;
5341 if (infoPtr->nItemCount > 0)
5343 LISTVIEW_GetItemBox(infoPtr, 0, &rcBox);
5344 wWidth = rcBox.right - rcBox.left;
5345 wHeight = (rcBox.bottom - rcBox.top) * nItemCount;
5347 else
5349 /* use current height and width */
5350 if (wHeight == 0xffff)
5351 wHeight = infoPtr->rcList.bottom - infoPtr->rcList.top;
5352 if (wWidth == 0xffff)
5353 wWidth = infoPtr->rcList.right - infoPtr->rcList.left;
5356 dwViewRect = MAKELONG(wWidth, wHeight);
5358 else if (infoPtr->uView == LV_VIEW_ICON)
5360 UINT rows,cols;
5361 UINT nItemWidth;
5362 UINT nItemHeight;
5364 nItemWidth = infoPtr->iconSpacing.cx;
5365 nItemHeight = infoPtr->iconSpacing.cy;
5367 if (wWidth == 0xffff)
5368 wWidth = infoPtr->rcList.right - infoPtr->rcList.left;
5370 if (wWidth < nItemWidth)
5371 wWidth = nItemWidth;
5373 cols = wWidth / nItemWidth;
5374 if (cols > nItemCount)
5375 cols = nItemCount;
5376 if (cols < 1)
5377 cols = 1;
5379 if (nItemCount)
5381 rows = nItemCount / cols;
5382 if (nItemCount % cols)
5383 rows++;
5385 else
5386 rows = 0;
5388 wHeight = (nItemHeight * rows)+2;
5389 wWidth = (nItemWidth * cols)+2;
5391 dwViewRect = MAKELONG(wWidth, wHeight);
5393 else if (infoPtr->uView == LV_VIEW_SMALLICON)
5394 FIXME("uView == LV_VIEW_SMALLICON: not implemented\n");
5396 return dwViewRect;
5399 /***
5400 * DESCRIPTION:
5401 * Cancel edit label with saving item text.
5403 * PARAMETER(S):
5404 * [I] infoPtr : valid pointer to the listview structure
5406 * RETURN:
5407 * Always returns TRUE.
5409 static LRESULT LISTVIEW_CancelEditLabel(LISTVIEW_INFO *infoPtr)
5411 if (infoPtr->hwndEdit)
5413 /* handle value will be lost after LISTVIEW_EndEditLabelT */
5414 HWND edit = infoPtr->hwndEdit;
5416 LISTVIEW_EndEditLabelT(infoPtr, TRUE, IsWindowUnicode(infoPtr->hwndEdit));
5417 SendMessageW(edit, WM_CLOSE, 0, 0);
5420 return TRUE;
5423 /***
5424 * DESCRIPTION:
5425 * Create a drag image list for the specified item.
5427 * PARAMETER(S):
5428 * [I] infoPtr : valid pointer to the listview structure
5429 * [I] iItem : index of item
5430 * [O] lppt : Upper-left corner of the image
5432 * RETURN:
5433 * Returns a handle to the image list if successful, NULL otherwise.
5435 static HIMAGELIST LISTVIEW_CreateDragImage(LISTVIEW_INFO *infoPtr, INT iItem, LPPOINT lppt)
5437 RECT rcItem;
5438 SIZE size;
5439 POINT pos;
5440 HDC hdc, hdcOrig;
5441 HBITMAP hbmp, hOldbmp;
5442 HFONT hOldFont;
5443 HIMAGELIST dragList = 0;
5444 TRACE("iItem=%d Count=%d\n", iItem, infoPtr->nItemCount);
5446 if (iItem < 0 || iItem >= infoPtr->nItemCount || !lppt)
5447 return 0;
5449 rcItem.left = LVIR_BOUNDS;
5450 if (!LISTVIEW_GetItemRect(infoPtr, iItem, &rcItem))
5451 return 0;
5453 lppt->x = rcItem.left;
5454 lppt->y = rcItem.top;
5456 size.cx = rcItem.right - rcItem.left;
5457 size.cy = rcItem.bottom - rcItem.top;
5459 hdcOrig = GetDC(infoPtr->hwndSelf);
5460 hdc = CreateCompatibleDC(hdcOrig);
5461 hbmp = CreateCompatibleBitmap(hdcOrig, size.cx, size.cy);
5462 hOldbmp = SelectObject(hdc, hbmp);
5463 hOldFont = SelectObject(hdc, infoPtr->hFont);
5465 SetRect(&rcItem, 0, 0, size.cx, size.cy);
5466 FillRect(hdc, &rcItem, infoPtr->hBkBrush);
5468 pos.x = pos.y = 0;
5469 if (LISTVIEW_DrawItem(infoPtr, hdc, iItem, NULL, pos, CDRF_DODEFAULT))
5471 dragList = ImageList_Create(size.cx, size.cy, ILC_COLOR, 10, 10);
5472 SelectObject(hdc, hOldbmp);
5473 ImageList_Add(dragList, hbmp, 0);
5475 else
5476 SelectObject(hdc, hOldbmp);
5478 SelectObject(hdc, hOldFont);
5479 DeleteObject(hbmp);
5480 DeleteDC(hdc);
5481 ReleaseDC(infoPtr->hwndSelf, hdcOrig);
5483 TRACE("ret=%p\n", dragList);
5485 return dragList;
5489 /***
5490 * DESCRIPTION:
5491 * Removes all listview items and subitems.
5493 * PARAMETER(S):
5494 * [I] infoPtr : valid pointer to the listview structure
5496 * RETURN:
5497 * SUCCESS : TRUE
5498 * FAILURE : FALSE
5500 static BOOL LISTVIEW_DeleteAllItems(LISTVIEW_INFO *infoPtr, BOOL destroy)
5502 HDPA hdpaSubItems = NULL;
5503 BOOL suppress = FALSE;
5504 ITEMHDR *hdrItem;
5505 ITEM_INFO *lpItem;
5506 ITEM_ID *lpID;
5507 INT i, j;
5509 TRACE("()\n");
5511 /* we do it directly, to avoid notifications */
5512 ranges_clear(infoPtr->selectionRanges);
5513 infoPtr->nSelectionMark = -1;
5514 infoPtr->nFocusedItem = -1;
5515 SetRectEmpty(&infoPtr->rcFocus);
5516 /* But we are supposed to leave nHotItem as is! */
5518 /* send LVN_DELETEALLITEMS notification */
5519 if (!(infoPtr->dwStyle & LVS_OWNERDATA) || !destroy)
5521 NMLISTVIEW nmlv;
5523 memset(&nmlv, 0, sizeof(NMLISTVIEW));
5524 nmlv.iItem = -1;
5525 suppress = notify_listview(infoPtr, LVN_DELETEALLITEMS, &nmlv);
5528 for (i = infoPtr->nItemCount - 1; i >= 0; i--)
5530 if (!(infoPtr->dwStyle & LVS_OWNERDATA))
5532 /* send LVN_DELETEITEM notification, if not suppressed
5533 and if it is not a virtual listview */
5534 if (!suppress) notify_deleteitem(infoPtr, i);
5535 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, i);
5536 lpItem = DPA_GetPtr(hdpaSubItems, 0);
5537 /* free id struct */
5538 j = DPA_GetPtrIndex(infoPtr->hdpaItemIds, lpItem->id);
5539 lpID = DPA_GetPtr(infoPtr->hdpaItemIds, j);
5540 DPA_DeletePtr(infoPtr->hdpaItemIds, j);
5541 Free(lpID);
5542 /* both item and subitem start with ITEMHDR header */
5543 for (j = 0; j < DPA_GetPtrCount(hdpaSubItems); j++)
5545 hdrItem = DPA_GetPtr(hdpaSubItems, j);
5546 if (is_text(hdrItem->pszText)) Free(hdrItem->pszText);
5547 Free(hdrItem);
5549 DPA_Destroy(hdpaSubItems);
5550 DPA_DeletePtr(infoPtr->hdpaItems, i);
5552 DPA_DeletePtr(infoPtr->hdpaPosX, i);
5553 DPA_DeletePtr(infoPtr->hdpaPosY, i);
5554 infoPtr->nItemCount --;
5557 if (!destroy)
5559 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
5560 LISTVIEW_UpdateScroll(infoPtr);
5562 LISTVIEW_InvalidateList(infoPtr);
5564 return TRUE;
5567 /***
5568 * DESCRIPTION:
5569 * Scrolls, and updates the columns, when a column is changing width.
5571 * PARAMETER(S):
5572 * [I] infoPtr : valid pointer to the listview structure
5573 * [I] nColumn : column to scroll
5574 * [I] dx : amount of scroll, in pixels
5576 * RETURN:
5577 * None.
5579 static void LISTVIEW_ScrollColumns(LISTVIEW_INFO *infoPtr, INT nColumn, INT dx)
5581 COLUMN_INFO *lpColumnInfo;
5582 RECT rcOld, rcCol;
5583 POINT ptOrigin;
5584 INT nCol;
5585 HDITEMW hdi;
5587 if (nColumn < 0 || DPA_GetPtrCount(infoPtr->hdpaColumns) < 1) return;
5588 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, min(nColumn, DPA_GetPtrCount(infoPtr->hdpaColumns) - 1));
5589 rcCol = lpColumnInfo->rcHeader;
5590 if (nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns))
5591 rcCol.left = rcCol.right;
5593 /* adjust the other columns */
5594 hdi.mask = HDI_ORDER;
5595 if (SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, nColumn, (LPARAM)&hdi))
5597 INT nOrder = hdi.iOrder;
5598 for (nCol = 0; nCol < DPA_GetPtrCount(infoPtr->hdpaColumns); nCol++)
5600 hdi.mask = HDI_ORDER;
5601 SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, nCol, (LPARAM)&hdi);
5602 if (hdi.iOrder >= nOrder) {
5603 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, nCol);
5604 lpColumnInfo->rcHeader.left += dx;
5605 lpColumnInfo->rcHeader.right += dx;
5610 /* do not update screen if not in report mode */
5611 if (!is_redrawing(infoPtr) || infoPtr->uView != LV_VIEW_DETAILS) return;
5613 /* Need to reset the item width when inserting a new column */
5614 infoPtr->nItemWidth += dx;
5616 LISTVIEW_UpdateScroll(infoPtr);
5617 LISTVIEW_GetOrigin(infoPtr, &ptOrigin);
5619 /* scroll to cover the deleted column, and invalidate for redraw */
5620 rcOld = infoPtr->rcList;
5621 rcOld.left = ptOrigin.x + rcCol.left + dx;
5622 ScrollWindowEx(infoPtr->hwndSelf, dx, 0, &rcOld, &rcOld, 0, 0, SW_ERASE | SW_INVALIDATE);
5625 /***
5626 * DESCRIPTION:
5627 * Removes a column from the listview control.
5629 * PARAMETER(S):
5630 * [I] infoPtr : valid pointer to the listview structure
5631 * [I] nColumn : column index
5633 * RETURN:
5634 * SUCCESS : TRUE
5635 * FAILURE : FALSE
5637 static BOOL LISTVIEW_DeleteColumn(LISTVIEW_INFO *infoPtr, INT nColumn)
5639 RECT rcCol;
5641 TRACE("nColumn=%d\n", nColumn);
5643 if (nColumn < 0 || nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns))
5644 return FALSE;
5646 /* While the MSDN specifically says that column zero should not be deleted,
5647 what actually happens is that the column itself is deleted but no items or subitems
5648 are removed.
5651 LISTVIEW_GetHeaderRect(infoPtr, nColumn, &rcCol);
5653 if (!SendMessageW(infoPtr->hwndHeader, HDM_DELETEITEM, nColumn, 0))
5654 return FALSE;
5656 Free(DPA_GetPtr(infoPtr->hdpaColumns, nColumn));
5657 DPA_DeletePtr(infoPtr->hdpaColumns, nColumn);
5659 if (!(infoPtr->dwStyle & LVS_OWNERDATA) && nColumn)
5661 SUBITEM_INFO *lpSubItem, *lpDelItem;
5662 HDPA hdpaSubItems;
5663 INT nItem, nSubItem, i;
5665 for (nItem = 0; nItem < infoPtr->nItemCount; nItem++)
5667 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, nItem);
5668 nSubItem = 0;
5669 lpDelItem = 0;
5670 for (i = 1; i < DPA_GetPtrCount(hdpaSubItems); i++)
5672 lpSubItem = DPA_GetPtr(hdpaSubItems, i);
5673 if (lpSubItem->iSubItem == nColumn)
5675 nSubItem = i;
5676 lpDelItem = lpSubItem;
5678 else if (lpSubItem->iSubItem > nColumn)
5680 lpSubItem->iSubItem--;
5684 /* if we found our subitem, zap it */
5685 if (nSubItem > 0)
5687 /* free string */
5688 if (is_text(lpDelItem->hdr.pszText))
5689 Free(lpDelItem->hdr.pszText);
5691 /* free item */
5692 Free(lpDelItem);
5694 /* free dpa memory */
5695 DPA_DeletePtr(hdpaSubItems, nSubItem);
5700 /* update the other column info */
5701 if(DPA_GetPtrCount(infoPtr->hdpaColumns) == 0)
5702 LISTVIEW_InvalidateList(infoPtr);
5703 else
5704 LISTVIEW_ScrollColumns(infoPtr, nColumn, -(rcCol.right - rcCol.left));
5705 LISTVIEW_UpdateItemSize(infoPtr);
5707 return TRUE;
5710 /***
5711 * DESCRIPTION:
5712 * Invalidates the listview after an item's insertion or deletion.
5714 * PARAMETER(S):
5715 * [I] infoPtr : valid pointer to the listview structure
5716 * [I] nItem : item index
5717 * [I] dir : -1 if deleting, 1 if inserting
5719 * RETURN:
5720 * None
5722 static void LISTVIEW_ScrollOnInsert(LISTVIEW_INFO *infoPtr, INT nItem, INT dir)
5724 INT nPerCol, nItemCol, nItemRow;
5725 RECT rcScroll;
5726 POINT Origin;
5728 /* if we don't refresh, what's the point of scrolling? */
5729 if (!is_redrawing(infoPtr)) return;
5731 assert (abs(dir) == 1);
5733 /* arrange icons if autoarrange is on */
5734 if (is_autoarrange(infoPtr))
5736 BOOL arrange = TRUE;
5737 if (dir < 0 && nItem >= infoPtr->nItemCount) arrange = FALSE;
5738 if (dir > 0 && nItem == infoPtr->nItemCount - 1) arrange = FALSE;
5739 if (arrange) LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
5742 /* scrollbars need updating */
5743 LISTVIEW_UpdateScroll(infoPtr);
5745 /* figure out the item's position */
5746 if (infoPtr->uView == LV_VIEW_DETAILS)
5747 nPerCol = infoPtr->nItemCount + 1;
5748 else if (infoPtr->uView == LV_VIEW_LIST)
5749 nPerCol = LISTVIEW_GetCountPerColumn(infoPtr);
5750 else /* LV_VIEW_ICON, or LV_VIEW_SMALLICON */
5751 return;
5753 nItemCol = nItem / nPerCol;
5754 nItemRow = nItem % nPerCol;
5755 LISTVIEW_GetOrigin(infoPtr, &Origin);
5757 /* move the items below up a slot */
5758 rcScroll.left = nItemCol * infoPtr->nItemWidth;
5759 rcScroll.top = nItemRow * infoPtr->nItemHeight;
5760 rcScroll.right = rcScroll.left + infoPtr->nItemWidth;
5761 rcScroll.bottom = nPerCol * infoPtr->nItemHeight;
5762 OffsetRect(&rcScroll, Origin.x, Origin.y);
5763 TRACE("rcScroll=%s, dx=%d\n", wine_dbgstr_rect(&rcScroll), dir * infoPtr->nItemHeight);
5764 if (IntersectRect(&rcScroll, &rcScroll, &infoPtr->rcList))
5766 TRACE("Invalidating rcScroll=%s, rcList=%s\n", wine_dbgstr_rect(&rcScroll), wine_dbgstr_rect(&infoPtr->rcList));
5767 InvalidateRect(infoPtr->hwndSelf, &rcScroll, TRUE);
5770 /* report has only that column, so we're done */
5771 if (infoPtr->uView == LV_VIEW_DETAILS) return;
5773 /* now for LISTs, we have to deal with the columns to the right */
5774 SetRect(&rcScroll, (nItemCol + 1) * infoPtr->nItemWidth, 0,
5775 (infoPtr->nItemCount / nPerCol + 1) * infoPtr->nItemWidth,
5776 nPerCol * infoPtr->nItemHeight);
5777 OffsetRect(&rcScroll, Origin.x, Origin.y);
5778 if (IntersectRect(&rcScroll, &rcScroll, &infoPtr->rcList))
5779 InvalidateRect(infoPtr->hwndSelf, &rcScroll, TRUE);
5782 /***
5783 * DESCRIPTION:
5784 * Removes an item from the listview control.
5786 * PARAMETER(S):
5787 * [I] infoPtr : valid pointer to the listview structure
5788 * [I] nItem : item index
5790 * RETURN:
5791 * SUCCESS : TRUE
5792 * FAILURE : FALSE
5794 static BOOL LISTVIEW_DeleteItem(LISTVIEW_INFO *infoPtr, INT nItem)
5796 LVITEMW item;
5797 const BOOL is_icon = (infoPtr->uView == LV_VIEW_SMALLICON || infoPtr->uView == LV_VIEW_ICON);
5798 INT focus = infoPtr->nFocusedItem;
5800 TRACE("(nItem=%d)\n", nItem);
5802 if (nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
5804 /* remove selection, and focus */
5805 item.state = 0;
5806 item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
5807 LISTVIEW_SetItemState(infoPtr, nItem, &item);
5809 /* send LVN_DELETEITEM notification. */
5810 if (!notify_deleteitem(infoPtr, nItem)) return FALSE;
5812 /* we need to do this here, because we'll be deleting stuff */
5813 if (is_icon)
5814 LISTVIEW_InvalidateItem(infoPtr, nItem);
5816 if (!(infoPtr->dwStyle & LVS_OWNERDATA))
5818 HDPA hdpaSubItems;
5819 ITEMHDR *hdrItem;
5820 ITEM_INFO *lpItem;
5821 ITEM_ID *lpID;
5822 INT i;
5824 hdpaSubItems = DPA_DeletePtr(infoPtr->hdpaItems, nItem);
5825 lpItem = DPA_GetPtr(hdpaSubItems, 0);
5827 /* free id struct */
5828 i = DPA_GetPtrIndex(infoPtr->hdpaItemIds, lpItem->id);
5829 lpID = DPA_GetPtr(infoPtr->hdpaItemIds, i);
5830 DPA_DeletePtr(infoPtr->hdpaItemIds, i);
5831 Free(lpID);
5832 for (i = 0; i < DPA_GetPtrCount(hdpaSubItems); i++)
5834 hdrItem = DPA_GetPtr(hdpaSubItems, i);
5835 if (is_text(hdrItem->pszText)) Free(hdrItem->pszText);
5836 Free(hdrItem);
5838 DPA_Destroy(hdpaSubItems);
5841 if (is_icon)
5843 DPA_DeletePtr(infoPtr->hdpaPosX, nItem);
5844 DPA_DeletePtr(infoPtr->hdpaPosY, nItem);
5847 infoPtr->nItemCount--;
5848 LISTVIEW_ShiftIndices(infoPtr, nItem, -1);
5849 LISTVIEW_ShiftFocus(infoPtr, focus, nItem, -1);
5851 /* now is the invalidation fun */
5852 if (!is_icon)
5853 LISTVIEW_ScrollOnInsert(infoPtr, nItem, -1);
5854 return TRUE;
5858 /***
5859 * DESCRIPTION:
5860 * Callback implementation for editlabel control
5862 * PARAMETER(S):
5863 * [I] infoPtr : valid pointer to the listview structure
5864 * [I] storeText : store edit box text as item text
5865 * [I] isW : TRUE if psxText is Unicode, FALSE if it's ANSI
5867 * RETURN:
5868 * SUCCESS : TRUE
5869 * FAILURE : FALSE
5871 static BOOL LISTVIEW_EndEditLabelT(LISTVIEW_INFO *infoPtr, BOOL storeText, BOOL isW)
5873 HWND hwndSelf = infoPtr->hwndSelf;
5874 WCHAR szDispText[DISP_TEXT_SIZE] = { 0 };
5875 NMLVDISPINFOW dispInfo;
5876 INT editedItem = infoPtr->nEditLabelItem;
5877 BOOL same;
5878 WCHAR *pszText = NULL;
5879 BOOL res;
5881 if (storeText)
5883 DWORD len = isW ? GetWindowTextLengthW(infoPtr->hwndEdit) : GetWindowTextLengthA(infoPtr->hwndEdit);
5885 if (len++)
5887 if (!(pszText = Alloc(len * (isW ? sizeof(WCHAR) : sizeof(CHAR)))))
5888 return FALSE;
5890 if (isW)
5891 GetWindowTextW(infoPtr->hwndEdit, pszText, len);
5892 else
5893 GetWindowTextA(infoPtr->hwndEdit, (CHAR*)pszText, len);
5897 TRACE("(pszText=%s, isW=%d)\n", debugtext_t(pszText, isW), isW);
5899 ZeroMemory(&dispInfo, sizeof(dispInfo));
5900 dispInfo.item.mask = LVIF_PARAM | LVIF_STATE | LVIF_TEXT;
5901 dispInfo.item.iItem = editedItem;
5902 dispInfo.item.iSubItem = 0;
5903 dispInfo.item.stateMask = ~0;
5904 dispInfo.item.pszText = szDispText;
5905 dispInfo.item.cchTextMax = DISP_TEXT_SIZE;
5906 if (!LISTVIEW_GetItemT(infoPtr, &dispInfo.item, isW))
5908 res = FALSE;
5909 goto cleanup;
5912 if (isW)
5913 same = (lstrcmpW(dispInfo.item.pszText, pszText) == 0);
5914 else
5916 LPWSTR tmp = textdupTtoW(pszText, FALSE);
5917 same = (lstrcmpW(dispInfo.item.pszText, tmp) == 0);
5918 textfreeT(tmp, FALSE);
5921 /* add the text from the edit in */
5922 dispInfo.item.mask |= LVIF_TEXT;
5923 dispInfo.item.pszText = same ? NULL : pszText;
5924 dispInfo.item.cchTextMax = textlenT(dispInfo.item.pszText, isW);
5926 infoPtr->notify_mask &= ~NOTIFY_MASK_END_LABEL_EDIT;
5928 /* Do we need to update the Item Text */
5929 res = notify_dispinfoT(infoPtr, LVN_ENDLABELEDITW, &dispInfo, isW);
5931 infoPtr->notify_mask |= NOTIFY_MASK_END_LABEL_EDIT;
5933 infoPtr->nEditLabelItem = -1;
5934 infoPtr->hwndEdit = 0;
5936 if (!res) goto cleanup;
5938 if (!IsWindow(hwndSelf))
5940 res = FALSE;
5941 goto cleanup;
5943 if (!pszText) return TRUE;
5944 if (same)
5946 res = TRUE;
5947 goto cleanup;
5950 if (!(infoPtr->dwStyle & LVS_OWNERDATA))
5952 HDPA hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, editedItem);
5953 ITEM_INFO* lpItem = DPA_GetPtr(hdpaSubItems, 0);
5954 if (lpItem && lpItem->hdr.pszText == LPSTR_TEXTCALLBACKW)
5956 LISTVIEW_InvalidateItem(infoPtr, editedItem);
5957 res = TRUE;
5958 goto cleanup;
5962 ZeroMemory(&dispInfo, sizeof(dispInfo));
5963 dispInfo.item.mask = LVIF_TEXT;
5964 dispInfo.item.iItem = editedItem;
5965 dispInfo.item.iSubItem = 0;
5966 dispInfo.item.pszText = pszText;
5967 dispInfo.item.cchTextMax = textlenT(pszText, isW);
5968 res = LISTVIEW_SetItemT(infoPtr, &dispInfo.item, isW);
5970 cleanup:
5971 Free(pszText);
5973 return res;
5976 /***
5977 * DESCRIPTION:
5978 * Subclassed edit control windproc function
5980 * PARAMETER(S):
5981 * [I] hwnd : the edit window handle
5982 * [I] uMsg : the message that is to be processed
5983 * [I] wParam : first message parameter
5984 * [I] lParam : second message parameter
5985 * [I] isW : TRUE if input is Unicode
5987 * RETURN:
5988 * Zero.
5990 static LRESULT EditLblWndProcT(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL isW)
5992 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)GetWindowLongPtrW(GetParent(hwnd), 0);
5993 BOOL save = TRUE;
5995 TRACE("(hwnd=%p, uMsg=%x, wParam=%lx, lParam=%lx, isW=%d)\n",
5996 hwnd, uMsg, wParam, lParam, isW);
5998 switch (uMsg)
6000 case WM_GETDLGCODE:
6001 return DLGC_WANTARROWS | DLGC_WANTALLKEYS;
6003 case WM_DESTROY:
6005 WNDPROC editProc = infoPtr->EditWndProc;
6006 infoPtr->EditWndProc = 0;
6007 SetWindowLongPtrW(hwnd, GWLP_WNDPROC, (DWORD_PTR)editProc);
6008 return CallWindowProcT(editProc, hwnd, uMsg, wParam, lParam, isW);
6011 case WM_KEYDOWN:
6012 if (VK_ESCAPE == (INT)wParam)
6014 save = FALSE;
6015 break;
6017 else if (VK_RETURN == (INT)wParam)
6018 break;
6020 default:
6021 return CallWindowProcT(infoPtr->EditWndProc, hwnd, uMsg, wParam, lParam, isW);
6024 /* kill the edit */
6025 if (infoPtr->hwndEdit)
6026 LISTVIEW_EndEditLabelT(infoPtr, save, isW);
6028 SendMessageW(hwnd, WM_CLOSE, 0, 0);
6029 return 0;
6032 /***
6033 * DESCRIPTION:
6034 * Subclassed edit control Unicode windproc function
6036 * PARAMETER(S):
6037 * [I] hwnd : the edit window handle
6038 * [I] uMsg : the message that is to be processed
6039 * [I] wParam : first message parameter
6040 * [I] lParam : second message parameter
6042 * RETURN:
6044 static LRESULT CALLBACK EditLblWndProcW(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
6046 return EditLblWndProcT(hwnd, uMsg, wParam, lParam, TRUE);
6049 /***
6050 * DESCRIPTION:
6051 * Subclassed edit control ANSI windproc function
6053 * PARAMETER(S):
6054 * [I] hwnd : the edit window handle
6055 * [I] uMsg : the message that is to be processed
6056 * [I] wParam : first message parameter
6057 * [I] lParam : second message parameter
6059 * RETURN:
6061 static LRESULT CALLBACK EditLblWndProcA(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
6063 return EditLblWndProcT(hwnd, uMsg, wParam, lParam, FALSE);
6066 /***
6067 * DESCRIPTION:
6068 * Creates a subclassed edit control
6070 * PARAMETER(S):
6071 * [I] infoPtr : valid pointer to the listview structure
6072 * [I] text : initial text for the edit
6073 * [I] style : the window style
6074 * [I] isW : TRUE if input is Unicode
6076 * RETURN:
6078 static HWND CreateEditLabelT(LISTVIEW_INFO *infoPtr, LPCWSTR text, BOOL isW)
6080 static const DWORD style = WS_CHILDWINDOW|WS_CLIPSIBLINGS|ES_LEFT|ES_AUTOHSCROLL|WS_BORDER|WS_VISIBLE;
6081 HINSTANCE hinst = (HINSTANCE)GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_HINSTANCE);
6082 HWND hedit;
6084 TRACE("(%p, text=%s, isW=%d)\n", infoPtr, debugtext_t(text, isW), isW);
6086 /* window will be resized and positioned after LVN_BEGINLABELEDIT */
6087 if (isW)
6088 hedit = CreateWindowW(WC_EDITW, text, style, 0, 0, 0, 0, infoPtr->hwndSelf, 0, hinst, 0);
6089 else
6090 hedit = CreateWindowA(WC_EDITA, (LPCSTR)text, style, 0, 0, 0, 0, infoPtr->hwndSelf, 0, hinst, 0);
6092 if (!hedit) return 0;
6094 infoPtr->EditWndProc = (WNDPROC)
6095 (isW ? SetWindowLongPtrW(hedit, GWLP_WNDPROC, (DWORD_PTR)EditLblWndProcW) :
6096 SetWindowLongPtrA(hedit, GWLP_WNDPROC, (DWORD_PTR)EditLblWndProcA) );
6098 SendMessageW(hedit, WM_SETFONT, (WPARAM)infoPtr->hFont, FALSE);
6099 SendMessageW(hedit, EM_SETLIMITTEXT, DISP_TEXT_SIZE-1, 0);
6101 return hedit;
6104 /***
6105 * DESCRIPTION:
6106 * Begin in place editing of specified list view item
6108 * PARAMETER(S):
6109 * [I] infoPtr : valid pointer to the listview structure
6110 * [I] nItem : item index
6111 * [I] isW : TRUE if it's a Unicode req, FALSE if ASCII
6113 * RETURN:
6114 * SUCCESS : TRUE
6115 * FAILURE : FALSE
6117 static HWND LISTVIEW_EditLabelT(LISTVIEW_INFO *infoPtr, INT nItem, BOOL isW)
6119 WCHAR disptextW[DISP_TEXT_SIZE] = { 0 };
6120 HWND hwndSelf = infoPtr->hwndSelf;
6121 NMLVDISPINFOW dispInfo;
6122 HFONT hOldFont = NULL;
6123 TEXTMETRICW tm;
6124 RECT rect;
6125 SIZE sz;
6126 HDC hdc;
6128 TRACE("(nItem=%d, isW=%d)\n", nItem, isW);
6130 if (~infoPtr->dwStyle & LVS_EDITLABELS) return 0;
6132 /* remove existing edit box */
6133 if (infoPtr->hwndEdit)
6135 SetFocus(infoPtr->hwndSelf);
6136 infoPtr->hwndEdit = 0;
6139 if (nItem < 0 || nItem >= infoPtr->nItemCount) return 0;
6141 infoPtr->nEditLabelItem = nItem;
6143 LISTVIEW_SetSelection(infoPtr, nItem);
6144 LISTVIEW_SetItemFocus(infoPtr, nItem);
6145 LISTVIEW_InvalidateItem(infoPtr, nItem);
6147 rect.left = LVIR_LABEL;
6148 if (!LISTVIEW_GetItemRect(infoPtr, nItem, &rect)) return 0;
6150 ZeroMemory(&dispInfo, sizeof(dispInfo));
6151 dispInfo.item.mask = LVIF_PARAM | LVIF_STATE | LVIF_TEXT;
6152 dispInfo.item.iItem = nItem;
6153 dispInfo.item.iSubItem = 0;
6154 dispInfo.item.stateMask = ~0;
6155 dispInfo.item.pszText = disptextW;
6156 dispInfo.item.cchTextMax = DISP_TEXT_SIZE;
6157 if (!LISTVIEW_GetItemT(infoPtr, &dispInfo.item, isW)) return 0;
6159 infoPtr->hwndEdit = CreateEditLabelT(infoPtr, dispInfo.item.pszText, isW);
6160 if (!infoPtr->hwndEdit) return 0;
6162 if (notify_dispinfoT(infoPtr, LVN_BEGINLABELEDITW, &dispInfo, isW))
6164 if (!IsWindow(hwndSelf))
6165 return 0;
6166 SendMessageW(infoPtr->hwndEdit, WM_CLOSE, 0, 0);
6167 infoPtr->hwndEdit = 0;
6168 return 0;
6171 TRACE("disp text=%s\n", debugtext_t(dispInfo.item.pszText, isW));
6173 /* position and display edit box */
6174 hdc = GetDC(infoPtr->hwndSelf);
6176 /* select the font to get appropriate metric dimensions */
6177 if (infoPtr->hFont)
6178 hOldFont = SelectObject(hdc, infoPtr->hFont);
6180 /* use real edit box content, it could be altered during LVN_BEGINLABELEDIT notification */
6181 GetWindowTextW(infoPtr->hwndEdit, disptextW, DISP_TEXT_SIZE);
6182 TRACE("edit box text=%s\n", debugstr_w(disptextW));
6184 /* get string length in pixels */
6185 GetTextExtentPoint32W(hdc, disptextW, lstrlenW(disptextW), &sz);
6187 /* add extra spacing for the next character */
6188 GetTextMetricsW(hdc, &tm);
6189 sz.cx += tm.tmMaxCharWidth * 2;
6191 if (infoPtr->hFont)
6192 SelectObject(hdc, hOldFont);
6194 ReleaseDC(infoPtr->hwndSelf, hdc);
6196 sz.cy = rect.bottom - rect.top + 2;
6197 rect.left -= 2;
6198 rect.top -= 1;
6199 TRACE("moving edit=(%d,%d)-(%d,%d)\n", rect.left, rect.top, sz.cx, sz.cy);
6200 MoveWindow(infoPtr->hwndEdit, rect.left, rect.top, sz.cx, sz.cy, FALSE);
6201 ShowWindow(infoPtr->hwndEdit, SW_NORMAL);
6202 SetFocus(infoPtr->hwndEdit);
6203 SendMessageW(infoPtr->hwndEdit, EM_SETSEL, 0, -1);
6204 return infoPtr->hwndEdit;
6208 /***
6209 * DESCRIPTION:
6210 * Ensures the specified item is visible, scrolling into view if necessary.
6212 * PARAMETER(S):
6213 * [I] infoPtr : valid pointer to the listview structure
6214 * [I] nItem : item index
6215 * [I] bPartial : partially or entirely visible
6217 * RETURN:
6218 * SUCCESS : TRUE
6219 * FAILURE : FALSE
6221 static BOOL LISTVIEW_EnsureVisible(LISTVIEW_INFO *infoPtr, INT nItem, BOOL bPartial)
6223 INT nScrollPosHeight = 0;
6224 INT nScrollPosWidth = 0;
6225 INT nHorzAdjust = 0;
6226 INT nVertAdjust = 0;
6227 INT nHorzDiff = 0;
6228 INT nVertDiff = 0;
6229 RECT rcItem, rcTemp;
6231 rcItem.left = LVIR_BOUNDS;
6232 if (!LISTVIEW_GetItemRect(infoPtr, nItem, &rcItem)) return FALSE;
6234 if (bPartial && IntersectRect(&rcTemp, &infoPtr->rcList, &rcItem)) return TRUE;
6236 if (rcItem.left < infoPtr->rcList.left || rcItem.right > infoPtr->rcList.right)
6238 /* scroll left/right, but in LV_VIEW_DETAILS mode */
6239 if (infoPtr->uView == LV_VIEW_LIST)
6240 nScrollPosWidth = infoPtr->nItemWidth;
6241 else if ((infoPtr->uView == LV_VIEW_SMALLICON) || (infoPtr->uView == LV_VIEW_ICON))
6242 nScrollPosWidth = 1;
6244 if (rcItem.left < infoPtr->rcList.left)
6246 nHorzAdjust = -1;
6247 if (infoPtr->uView != LV_VIEW_DETAILS) nHorzDiff = rcItem.left - infoPtr->rcList.left;
6249 else
6251 nHorzAdjust = 1;
6252 if (infoPtr->uView != LV_VIEW_DETAILS) nHorzDiff = rcItem.right - infoPtr->rcList.right;
6256 if (rcItem.top < infoPtr->rcList.top || rcItem.bottom > infoPtr->rcList.bottom)
6258 /* scroll up/down, but not in LVS_LIST mode */
6259 if (infoPtr->uView == LV_VIEW_DETAILS)
6260 nScrollPosHeight = infoPtr->nItemHeight;
6261 else if ((infoPtr->uView == LV_VIEW_ICON) || (infoPtr->uView == LV_VIEW_SMALLICON))
6262 nScrollPosHeight = 1;
6264 if (rcItem.top < infoPtr->rcList.top)
6266 nVertAdjust = -1;
6267 if (infoPtr->uView != LV_VIEW_LIST) nVertDiff = rcItem.top - infoPtr->rcList.top;
6269 else
6271 nVertAdjust = 1;
6272 if (infoPtr->uView != LV_VIEW_LIST) nVertDiff = rcItem.bottom - infoPtr->rcList.bottom;
6276 if (!nScrollPosWidth && !nScrollPosHeight) return TRUE;
6278 if (nScrollPosWidth)
6280 INT diff = nHorzDiff / nScrollPosWidth;
6281 if (nHorzDiff % nScrollPosWidth) diff += nHorzAdjust;
6282 LISTVIEW_HScroll(infoPtr, SB_INTERNAL, diff);
6285 if (nScrollPosHeight)
6287 INT diff = nVertDiff / nScrollPosHeight;
6288 if (nVertDiff % nScrollPosHeight) diff += nVertAdjust;
6289 LISTVIEW_VScroll(infoPtr, SB_INTERNAL, diff);
6292 return TRUE;
6295 /***
6296 * DESCRIPTION:
6297 * Searches for an item with specific characteristics.
6299 * PARAMETER(S):
6300 * [I] hwnd : window handle
6301 * [I] nStart : base item index
6302 * [I] lpFindInfo : item information to look for
6304 * RETURN:
6305 * SUCCESS : index of item
6306 * FAILURE : -1
6308 static INT LISTVIEW_FindItemW(const LISTVIEW_INFO *infoPtr, INT nStart,
6309 const LVFINDINFOW *lpFindInfo)
6311 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
6312 BOOL bWrap = FALSE, bNearest = FALSE;
6313 INT nItem = nStart + 1, nLast = infoPtr->nItemCount, nNearestItem = -1;
6314 ULONG xdist, ydist, dist, mindist = 0x7fffffff;
6315 POINT Position, Destination;
6316 LVITEMW lvItem;
6318 /* Search in virtual listviews should be done by application, not by
6319 listview control, so we just send LVN_ODFINDITEMW and return the result */
6320 if (infoPtr->dwStyle & LVS_OWNERDATA)
6322 NMLVFINDITEMW nmlv;
6324 nmlv.iStart = nStart;
6325 nmlv.lvfi = *lpFindInfo;
6326 return notify_hdr(infoPtr, LVN_ODFINDITEMW, (LPNMHDR)&nmlv.hdr);
6329 if (!lpFindInfo || nItem < 0) return -1;
6331 lvItem.mask = 0;
6332 if (lpFindInfo->flags & (LVFI_STRING | LVFI_PARTIAL | LVFI_SUBSTRING))
6334 lvItem.mask |= LVIF_TEXT;
6335 lvItem.pszText = szDispText;
6336 lvItem.cchTextMax = DISP_TEXT_SIZE;
6339 if (lpFindInfo->flags & LVFI_WRAP)
6340 bWrap = TRUE;
6342 if ((lpFindInfo->flags & LVFI_NEARESTXY) &&
6343 (infoPtr->uView == LV_VIEW_ICON || infoPtr->uView == LV_VIEW_SMALLICON))
6345 POINT Origin;
6346 RECT rcArea;
6348 LISTVIEW_GetOrigin(infoPtr, &Origin);
6349 Destination.x = lpFindInfo->pt.x - Origin.x;
6350 Destination.y = lpFindInfo->pt.y - Origin.y;
6351 switch(lpFindInfo->vkDirection)
6353 case VK_DOWN: Destination.y += infoPtr->nItemHeight; break;
6354 case VK_UP: Destination.y -= infoPtr->nItemHeight; break;
6355 case VK_RIGHT: Destination.x += infoPtr->nItemWidth; break;
6356 case VK_LEFT: Destination.x -= infoPtr->nItemWidth; break;
6357 case VK_HOME: Destination.x = Destination.y = 0; break;
6358 case VK_NEXT: Destination.y += infoPtr->rcList.bottom - infoPtr->rcList.top; break;
6359 case VK_PRIOR: Destination.y -= infoPtr->rcList.bottom - infoPtr->rcList.top; break;
6360 case VK_END:
6361 LISTVIEW_GetAreaRect(infoPtr, &rcArea);
6362 Destination.x = rcArea.right;
6363 Destination.y = rcArea.bottom;
6364 break;
6365 default: ERR("Unknown vkDirection=%d\n", lpFindInfo->vkDirection);
6367 bNearest = TRUE;
6369 else Destination.x = Destination.y = 0;
6371 /* if LVFI_PARAM is specified, all other flags are ignored */
6372 if (lpFindInfo->flags & LVFI_PARAM)
6374 lvItem.mask |= LVIF_PARAM;
6375 bNearest = FALSE;
6376 lvItem.mask &= ~LVIF_TEXT;
6379 nItem = bNearest ? -1 : nStart + 1;
6381 again:
6382 for (; nItem < nLast; nItem++)
6384 lvItem.iItem = nItem;
6385 lvItem.iSubItem = 0;
6386 lvItem.pszText = szDispText;
6387 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) continue;
6389 if (lvItem.mask & LVIF_PARAM)
6391 if (lpFindInfo->lParam == lvItem.lParam)
6392 return nItem;
6393 else
6394 continue;
6397 if (lvItem.mask & LVIF_TEXT)
6399 if (lpFindInfo->flags & (LVFI_PARTIAL | LVFI_SUBSTRING))
6401 WCHAR *p = strstrW(lvItem.pszText, lpFindInfo->psz);
6402 if (!p || p != lvItem.pszText) continue;
6404 else
6406 if (lstrcmpW(lvItem.pszText, lpFindInfo->psz) != 0) continue;
6410 if (!bNearest) return nItem;
6412 /* This is very inefficient. To do a good job here,
6413 * we need a sorted array of (x,y) item positions */
6414 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position);
6416 /* compute the distance^2 to the destination */
6417 xdist = Destination.x - Position.x;
6418 ydist = Destination.y - Position.y;
6419 dist = xdist * xdist + ydist * ydist;
6421 /* remember the distance, and item if it's closer */
6422 if (dist < mindist)
6424 mindist = dist;
6425 nNearestItem = nItem;
6429 if (bWrap)
6431 nItem = 0;
6432 nLast = min(nStart + 1, infoPtr->nItemCount);
6433 bWrap = FALSE;
6434 goto again;
6437 return nNearestItem;
6440 /***
6441 * DESCRIPTION:
6442 * Searches for an item with specific characteristics.
6444 * PARAMETER(S):
6445 * [I] hwnd : window handle
6446 * [I] nStart : base item index
6447 * [I] lpFindInfo : item information to look for
6449 * RETURN:
6450 * SUCCESS : index of item
6451 * FAILURE : -1
6453 static INT LISTVIEW_FindItemA(const LISTVIEW_INFO *infoPtr, INT nStart,
6454 const LVFINDINFOA *lpFindInfo)
6456 LVFINDINFOW fiw;
6457 INT res;
6458 LPWSTR strW = NULL;
6460 memcpy(&fiw, lpFindInfo, sizeof(fiw));
6461 if (lpFindInfo->flags & (LVFI_STRING | LVFI_PARTIAL | LVFI_SUBSTRING))
6462 fiw.psz = strW = textdupTtoW((LPCWSTR)lpFindInfo->psz, FALSE);
6463 res = LISTVIEW_FindItemW(infoPtr, nStart, &fiw);
6464 textfreeT(strW, FALSE);
6465 return res;
6468 /***
6469 * DESCRIPTION:
6470 * Retrieves column attributes.
6472 * PARAMETER(S):
6473 * [I] infoPtr : valid pointer to the listview structure
6474 * [I] nColumn : column index
6475 * [IO] lpColumn : column information
6476 * [I] isW : if TRUE, then lpColumn is a LPLVCOLUMNW
6477 * otherwise it is in fact a LPLVCOLUMNA
6479 * RETURN:
6480 * SUCCESS : TRUE
6481 * FAILURE : FALSE
6483 static BOOL LISTVIEW_GetColumnT(const LISTVIEW_INFO *infoPtr, INT nColumn, LPLVCOLUMNW lpColumn, BOOL isW)
6485 COLUMN_INFO *lpColumnInfo;
6486 HDITEMW hdi;
6488 if (!lpColumn || nColumn < 0 || nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return FALSE;
6489 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, nColumn);
6491 /* initialize memory */
6492 ZeroMemory(&hdi, sizeof(hdi));
6494 if (lpColumn->mask & LVCF_TEXT)
6496 hdi.mask |= HDI_TEXT;
6497 hdi.pszText = lpColumn->pszText;
6498 hdi.cchTextMax = lpColumn->cchTextMax;
6501 if (lpColumn->mask & LVCF_IMAGE)
6502 hdi.mask |= HDI_IMAGE;
6504 if (lpColumn->mask & LVCF_ORDER)
6505 hdi.mask |= HDI_ORDER;
6507 if (lpColumn->mask & LVCF_SUBITEM)
6508 hdi.mask |= HDI_LPARAM;
6510 if (!SendMessageW(infoPtr->hwndHeader, isW ? HDM_GETITEMW : HDM_GETITEMA, nColumn, (LPARAM)&hdi)) return FALSE;
6512 if (lpColumn->mask & LVCF_FMT)
6513 lpColumn->fmt = lpColumnInfo->fmt;
6515 if (lpColumn->mask & LVCF_WIDTH)
6516 lpColumn->cx = lpColumnInfo->rcHeader.right - lpColumnInfo->rcHeader.left;
6518 if (lpColumn->mask & LVCF_IMAGE)
6519 lpColumn->iImage = hdi.iImage;
6521 if (lpColumn->mask & LVCF_ORDER)
6522 lpColumn->iOrder = hdi.iOrder;
6524 if (lpColumn->mask & LVCF_SUBITEM)
6525 lpColumn->iSubItem = hdi.lParam;
6527 if (lpColumn->mask & LVCF_MINWIDTH)
6528 lpColumn->cxMin = lpColumnInfo->cxMin;
6530 return TRUE;
6533 static inline BOOL LISTVIEW_GetColumnOrderArray(const LISTVIEW_INFO *infoPtr, INT iCount, LPINT lpiArray)
6535 if (!infoPtr->hwndHeader) return FALSE;
6536 return SendMessageW(infoPtr->hwndHeader, HDM_GETORDERARRAY, iCount, (LPARAM)lpiArray);
6539 /***
6540 * DESCRIPTION:
6541 * Retrieves the column width.
6543 * PARAMETER(S):
6544 * [I] infoPtr : valid pointer to the listview structure
6545 * [I] int : column index
6547 * RETURN:
6548 * SUCCESS : column width
6549 * FAILURE : zero
6551 static INT LISTVIEW_GetColumnWidth(const LISTVIEW_INFO *infoPtr, INT nColumn)
6553 INT nColumnWidth = 0;
6554 HDITEMW hdItem;
6556 TRACE("nColumn=%d\n", nColumn);
6558 /* we have a 'column' in LIST and REPORT mode only */
6559 switch(infoPtr->uView)
6561 case LV_VIEW_LIST:
6562 nColumnWidth = infoPtr->nItemWidth;
6563 break;
6564 case LV_VIEW_DETAILS:
6565 /* We are not using LISTVIEW_GetHeaderRect as this data is updated only after a HDN_ITEMCHANGED.
6566 * There is an application that subclasses the listview, calls LVM_GETCOLUMNWIDTH in the
6567 * HDN_ITEMCHANGED handler and goes into infinite recursion if it receives old data.
6569 hdItem.mask = HDI_WIDTH;
6570 if (!SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, nColumn, (LPARAM)&hdItem))
6572 WARN("(%p): HDM_GETITEMW failed for item %d\n", infoPtr->hwndSelf, nColumn);
6573 return 0;
6575 nColumnWidth = hdItem.cxy;
6576 break;
6579 TRACE("nColumnWidth=%d\n", nColumnWidth);
6580 return nColumnWidth;
6583 /***
6584 * DESCRIPTION:
6585 * In list or report display mode, retrieves the number of items that can fit
6586 * vertically in the visible area. In icon or small icon display mode,
6587 * retrieves the total number of visible items.
6589 * PARAMETER(S):
6590 * [I] infoPtr : valid pointer to the listview structure
6592 * RETURN:
6593 * Number of fully visible items.
6595 static INT LISTVIEW_GetCountPerPage(const LISTVIEW_INFO *infoPtr)
6597 switch (infoPtr->uView)
6599 case LV_VIEW_ICON:
6600 case LV_VIEW_SMALLICON:
6601 return infoPtr->nItemCount;
6602 case LV_VIEW_DETAILS:
6603 return LISTVIEW_GetCountPerColumn(infoPtr);
6604 case LV_VIEW_LIST:
6605 return LISTVIEW_GetCountPerRow(infoPtr) * LISTVIEW_GetCountPerColumn(infoPtr);
6607 assert(FALSE);
6608 return 0;
6611 /***
6612 * DESCRIPTION:
6613 * Retrieves an image list handle.
6615 * PARAMETER(S):
6616 * [I] infoPtr : valid pointer to the listview structure
6617 * [I] nImageList : image list identifier
6619 * RETURN:
6620 * SUCCESS : image list handle
6621 * FAILURE : NULL
6623 static HIMAGELIST LISTVIEW_GetImageList(const LISTVIEW_INFO *infoPtr, INT nImageList)
6625 switch (nImageList)
6627 case LVSIL_NORMAL: return infoPtr->himlNormal;
6628 case LVSIL_SMALL: return infoPtr->himlSmall;
6629 case LVSIL_STATE: return infoPtr->himlState;
6630 case LVSIL_GROUPHEADER:
6631 FIXME("LVSIL_GROUPHEADER not supported\n");
6632 break;
6633 default:
6634 WARN("got unknown imagelist index - %d\n", nImageList);
6636 return NULL;
6639 /* LISTVIEW_GetISearchString */
6641 /***
6642 * DESCRIPTION:
6643 * Retrieves item attributes.
6645 * PARAMETER(S):
6646 * [I] hwnd : window handle
6647 * [IO] lpLVItem : item info
6648 * [I] isW : if TRUE, then lpLVItem is a LPLVITEMW,
6649 * if FALSE, then lpLVItem is a LPLVITEMA.
6651 * NOTE:
6652 * This is the internal 'GetItem' interface -- it tries to
6653 * be smart and avoid text copies, if possible, by modifying
6654 * lpLVItem->pszText to point to the text string. Please note
6655 * that this is not always possible (e.g. OWNERDATA), so on
6656 * entry you *must* supply valid values for pszText, and cchTextMax.
6657 * The only difference to the documented interface is that upon
6658 * return, you should use *only* the lpLVItem->pszText, rather than
6659 * the buffer pointer you provided on input. Most code already does
6660 * that, so it's not a problem.
6661 * For the two cases when the text must be copied (that is,
6662 * for LVM_GETITEM, and LVM_GETITEMTEXT), use LISTVIEW_GetItemExtT.
6664 * RETURN:
6665 * SUCCESS : TRUE
6666 * FAILURE : FALSE
6668 static BOOL LISTVIEW_GetItemT(const LISTVIEW_INFO *infoPtr, LPLVITEMW lpLVItem, BOOL isW)
6670 ITEMHDR callbackHdr = { LPSTR_TEXTCALLBACKW, I_IMAGECALLBACK };
6671 NMLVDISPINFOW dispInfo;
6672 ITEM_INFO *lpItem;
6673 ITEMHDR* pItemHdr;
6674 HDPA hdpaSubItems;
6675 INT isubitem;
6677 TRACE("(item=%s, isW=%d)\n", debuglvitem_t(lpLVItem, isW), isW);
6679 if (!lpLVItem || lpLVItem->iItem < 0 || lpLVItem->iItem >= infoPtr->nItemCount)
6680 return FALSE;
6682 if (lpLVItem->mask == 0) return TRUE;
6683 TRACE("mask=%x\n", lpLVItem->mask);
6685 /* make a local copy */
6686 isubitem = lpLVItem->iSubItem;
6688 if (isubitem && (lpLVItem->mask & LVIF_STATE))
6689 lpLVItem->state = 0;
6691 /* a quick optimization if all we're asked is the focus state
6692 * these queries are worth optimising since they are common,
6693 * and can be answered in constant time, without the heavy accesses */
6694 if ( (lpLVItem->mask == LVIF_STATE) && (lpLVItem->stateMask == LVIS_FOCUSED) &&
6695 !(infoPtr->uCallbackMask & LVIS_FOCUSED) )
6697 lpLVItem->state = 0;
6698 if (infoPtr->nFocusedItem == lpLVItem->iItem && isubitem == 0)
6699 lpLVItem->state |= LVIS_FOCUSED;
6700 return TRUE;
6703 ZeroMemory(&dispInfo, sizeof(dispInfo));
6705 /* if the app stores all the data, handle it separately */
6706 if (infoPtr->dwStyle & LVS_OWNERDATA)
6708 dispInfo.item.state = 0;
6710 /* apparently, we should not callback for lParam in LVS_OWNERDATA */
6711 if ((lpLVItem->mask & ~(LVIF_STATE | LVIF_PARAM)) ||
6712 ((lpLVItem->mask & LVIF_STATE) && (infoPtr->uCallbackMask & lpLVItem->stateMask)))
6714 UINT mask = lpLVItem->mask;
6716 /* NOTE: copy only fields which we _know_ are initialized, some apps
6717 * depend on the uninitialized fields being 0 */
6718 dispInfo.item.mask = lpLVItem->mask & ~LVIF_PARAM;
6719 dispInfo.item.iItem = lpLVItem->iItem;
6720 dispInfo.item.iSubItem = isubitem;
6721 if (lpLVItem->mask & LVIF_TEXT)
6723 if (lpLVItem->mask & LVIF_NORECOMPUTE)
6724 /* reset mask */
6725 dispInfo.item.mask &= ~(LVIF_TEXT | LVIF_NORECOMPUTE);
6726 else
6728 dispInfo.item.pszText = lpLVItem->pszText;
6729 dispInfo.item.cchTextMax = lpLVItem->cchTextMax;
6732 if (lpLVItem->mask & LVIF_STATE)
6733 dispInfo.item.stateMask = lpLVItem->stateMask & infoPtr->uCallbackMask;
6734 /* could be zeroed on LVIF_NORECOMPUTE case */
6735 if (dispInfo.item.mask)
6737 notify_dispinfoT(infoPtr, LVN_GETDISPINFOW, &dispInfo, isW);
6738 dispInfo.item.stateMask = lpLVItem->stateMask;
6739 if (lpLVItem->mask & (LVIF_GROUPID|LVIF_COLUMNS))
6741 /* full size structure expected - _WIN32IE >= 0x560 */
6742 *lpLVItem = dispInfo.item;
6744 else if (lpLVItem->mask & LVIF_INDENT)
6746 /* indent member expected - _WIN32IE >= 0x300 */
6747 memcpy(lpLVItem, &dispInfo.item, offsetof( LVITEMW, iGroupId ));
6749 else
6751 /* minimal structure expected */
6752 memcpy(lpLVItem, &dispInfo.item, offsetof( LVITEMW, iIndent ));
6754 lpLVItem->mask = mask;
6755 TRACE(" getdispinfo(1):lpLVItem=%s\n", debuglvitem_t(lpLVItem, isW));
6759 /* make sure lParam is zeroed out */
6760 if (lpLVItem->mask & LVIF_PARAM) lpLVItem->lParam = 0;
6762 /* callback marked pointer required here */
6763 if ((lpLVItem->mask & LVIF_TEXT) && (lpLVItem->mask & LVIF_NORECOMPUTE))
6764 lpLVItem->pszText = LPSTR_TEXTCALLBACKW;
6766 /* we store only a little state, so if we're not asked, we're done */
6767 if (!(lpLVItem->mask & LVIF_STATE) || isubitem) return TRUE;
6769 /* if focus is handled by us, report it */
6770 if ( lpLVItem->stateMask & ~infoPtr->uCallbackMask & LVIS_FOCUSED )
6772 lpLVItem->state &= ~LVIS_FOCUSED;
6773 if (infoPtr->nFocusedItem == lpLVItem->iItem)
6774 lpLVItem->state |= LVIS_FOCUSED;
6777 /* and do the same for selection, if we handle it */
6778 if ( lpLVItem->stateMask & ~infoPtr->uCallbackMask & LVIS_SELECTED )
6780 lpLVItem->state &= ~LVIS_SELECTED;
6781 if (ranges_contain(infoPtr->selectionRanges, lpLVItem->iItem))
6782 lpLVItem->state |= LVIS_SELECTED;
6785 return TRUE;
6788 /* find the item and subitem structures before we proceed */
6789 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, lpLVItem->iItem);
6790 lpItem = DPA_GetPtr(hdpaSubItems, 0);
6791 assert (lpItem);
6793 if (isubitem)
6795 SUBITEM_INFO *lpSubItem = LISTVIEW_GetSubItemPtr(hdpaSubItems, isubitem);
6796 pItemHdr = lpSubItem ? &lpSubItem->hdr : &callbackHdr;
6797 if (!lpSubItem)
6799 WARN(" iSubItem invalid (%08x), ignored.\n", isubitem);
6800 isubitem = 0;
6803 else
6804 pItemHdr = &lpItem->hdr;
6806 /* Do we need to query the state from the app? */
6807 if ((lpLVItem->mask & LVIF_STATE) && infoPtr->uCallbackMask && isubitem == 0)
6809 dispInfo.item.mask |= LVIF_STATE;
6810 dispInfo.item.stateMask = infoPtr->uCallbackMask;
6813 /* Do we need to enquire about the image? */
6814 if ((lpLVItem->mask & LVIF_IMAGE) && pItemHdr->iImage == I_IMAGECALLBACK &&
6815 (isubitem == 0 || (infoPtr->dwLvExStyle & LVS_EX_SUBITEMIMAGES)))
6817 dispInfo.item.mask |= LVIF_IMAGE;
6818 dispInfo.item.iImage = I_IMAGECALLBACK;
6821 /* Only items support indentation */
6822 if ((lpLVItem->mask & LVIF_INDENT) && lpItem->iIndent == I_INDENTCALLBACK &&
6823 (isubitem == 0))
6825 dispInfo.item.mask |= LVIF_INDENT;
6826 dispInfo.item.iIndent = I_INDENTCALLBACK;
6829 /* Apps depend on calling back for text if it is NULL or LPSTR_TEXTCALLBACKW */
6830 if ((lpLVItem->mask & LVIF_TEXT) && !(lpLVItem->mask & LVIF_NORECOMPUTE) &&
6831 !is_text(pItemHdr->pszText))
6833 dispInfo.item.mask |= LVIF_TEXT;
6834 dispInfo.item.pszText = lpLVItem->pszText;
6835 dispInfo.item.cchTextMax = lpLVItem->cchTextMax;
6836 if (dispInfo.item.pszText && dispInfo.item.cchTextMax > 0)
6837 *dispInfo.item.pszText = '\0';
6840 /* If we don't have all the requested info, query the application */
6841 if (dispInfo.item.mask)
6843 dispInfo.item.iItem = lpLVItem->iItem;
6844 dispInfo.item.iSubItem = lpLVItem->iSubItem; /* yes: the original subitem */
6845 dispInfo.item.lParam = lpItem->lParam;
6846 notify_dispinfoT(infoPtr, LVN_GETDISPINFOW, &dispInfo, isW);
6847 TRACE(" getdispinfo(2):item=%s\n", debuglvitem_t(&dispInfo.item, isW));
6850 /* we should not store values for subitems */
6851 if (isubitem) dispInfo.item.mask &= ~LVIF_DI_SETITEM;
6853 /* Now, handle the iImage field */
6854 if (dispInfo.item.mask & LVIF_IMAGE)
6856 lpLVItem->iImage = dispInfo.item.iImage;
6857 if ((dispInfo.item.mask & LVIF_DI_SETITEM) && pItemHdr->iImage == I_IMAGECALLBACK)
6858 pItemHdr->iImage = dispInfo.item.iImage;
6860 else if (lpLVItem->mask & LVIF_IMAGE)
6862 if(isubitem == 0 || (infoPtr->dwLvExStyle & LVS_EX_SUBITEMIMAGES))
6863 lpLVItem->iImage = pItemHdr->iImage;
6864 else
6865 lpLVItem->iImage = 0;
6868 /* The pszText field */
6869 if (dispInfo.item.mask & LVIF_TEXT)
6871 if ((dispInfo.item.mask & LVIF_DI_SETITEM) && pItemHdr->pszText)
6872 textsetptrT(&pItemHdr->pszText, dispInfo.item.pszText, isW);
6874 lpLVItem->pszText = dispInfo.item.pszText;
6876 else if (lpLVItem->mask & LVIF_TEXT)
6878 /* if LVN_GETDISPINFO's disabled with LVIF_NORECOMPUTE return callback placeholder */
6879 if (isW || !is_text(pItemHdr->pszText)) lpLVItem->pszText = pItemHdr->pszText;
6880 else textcpynT(lpLVItem->pszText, isW, pItemHdr->pszText, TRUE, lpLVItem->cchTextMax);
6883 /* Next is the lParam field */
6884 if (dispInfo.item.mask & LVIF_PARAM)
6886 lpLVItem->lParam = dispInfo.item.lParam;
6887 if ((dispInfo.item.mask & LVIF_DI_SETITEM))
6888 lpItem->lParam = dispInfo.item.lParam;
6890 else if (lpLVItem->mask & LVIF_PARAM)
6891 lpLVItem->lParam = lpItem->lParam;
6893 /* if this is a subitem, we're done */
6894 if (isubitem) return TRUE;
6896 /* ... the state field (this one is different due to uCallbackmask) */
6897 if (lpLVItem->mask & LVIF_STATE)
6899 lpLVItem->state = lpItem->state & lpLVItem->stateMask;
6900 if (dispInfo.item.mask & LVIF_STATE)
6902 lpLVItem->state &= ~dispInfo.item.stateMask;
6903 lpLVItem->state |= (dispInfo.item.state & dispInfo.item.stateMask);
6905 if ( lpLVItem->stateMask & ~infoPtr->uCallbackMask & LVIS_FOCUSED )
6907 lpLVItem->state &= ~LVIS_FOCUSED;
6908 if (infoPtr->nFocusedItem == lpLVItem->iItem)
6909 lpLVItem->state |= LVIS_FOCUSED;
6911 if ( lpLVItem->stateMask & ~infoPtr->uCallbackMask & LVIS_SELECTED )
6913 lpLVItem->state &= ~LVIS_SELECTED;
6914 if (ranges_contain(infoPtr->selectionRanges, lpLVItem->iItem))
6915 lpLVItem->state |= LVIS_SELECTED;
6919 /* and last, but not least, the indent field */
6920 if (dispInfo.item.mask & LVIF_INDENT)
6922 lpLVItem->iIndent = dispInfo.item.iIndent;
6923 if ((dispInfo.item.mask & LVIF_DI_SETITEM) && lpItem->iIndent == I_INDENTCALLBACK)
6924 lpItem->iIndent = dispInfo.item.iIndent;
6926 else if (lpLVItem->mask & LVIF_INDENT)
6928 lpLVItem->iIndent = lpItem->iIndent;
6931 return TRUE;
6934 /***
6935 * DESCRIPTION:
6936 * Retrieves item attributes.
6938 * PARAMETER(S):
6939 * [I] hwnd : window handle
6940 * [IO] lpLVItem : item info
6941 * [I] isW : if TRUE, then lpLVItem is a LPLVITEMW,
6942 * if FALSE, then lpLVItem is a LPLVITEMA.
6944 * NOTE:
6945 * This is the external 'GetItem' interface -- it properly copies
6946 * the text in the provided buffer.
6948 * RETURN:
6949 * SUCCESS : TRUE
6950 * FAILURE : FALSE
6952 static BOOL LISTVIEW_GetItemExtT(const LISTVIEW_INFO *infoPtr, LPLVITEMW lpLVItem, BOOL isW)
6954 LPWSTR pszText;
6955 BOOL bResult;
6957 if (!lpLVItem || lpLVItem->iItem < 0 || lpLVItem->iItem >= infoPtr->nItemCount)
6958 return FALSE;
6960 pszText = lpLVItem->pszText;
6961 bResult = LISTVIEW_GetItemT(infoPtr, lpLVItem, isW);
6962 if (bResult && (lpLVItem->mask & LVIF_TEXT) && lpLVItem->pszText != pszText)
6964 if (lpLVItem->pszText != LPSTR_TEXTCALLBACKW)
6965 textcpynT(pszText, isW, lpLVItem->pszText, isW, lpLVItem->cchTextMax);
6966 else
6967 pszText = LPSTR_TEXTCALLBACKW;
6969 lpLVItem->pszText = pszText;
6971 return bResult;
6975 /***
6976 * DESCRIPTION:
6977 * Retrieves the position (upper-left) of the listview control item.
6978 * Note that for LVS_ICON style, the upper-left is that of the icon
6979 * and not the bounding box.
6981 * PARAMETER(S):
6982 * [I] infoPtr : valid pointer to the listview structure
6983 * [I] nItem : item index
6984 * [O] lpptPosition : coordinate information
6986 * RETURN:
6987 * SUCCESS : TRUE
6988 * FAILURE : FALSE
6990 static BOOL LISTVIEW_GetItemPosition(const LISTVIEW_INFO *infoPtr, INT nItem, LPPOINT lpptPosition)
6992 POINT Origin;
6994 TRACE("(nItem=%d, lpptPosition=%p)\n", nItem, lpptPosition);
6996 if (!lpptPosition || nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
6998 LISTVIEW_GetOrigin(infoPtr, &Origin);
6999 LISTVIEW_GetItemOrigin(infoPtr, nItem, lpptPosition);
7001 if (infoPtr->uView == LV_VIEW_ICON)
7003 lpptPosition->x += (infoPtr->nItemWidth - infoPtr->iconSize.cx) / 2;
7004 lpptPosition->y += ICON_TOP_PADDING;
7006 lpptPosition->x += Origin.x;
7007 lpptPosition->y += Origin.y;
7009 TRACE (" lpptPosition=%s\n", wine_dbgstr_point(lpptPosition));
7010 return TRUE;
7014 /***
7015 * DESCRIPTION:
7016 * Retrieves the bounding rectangle for a listview control item.
7018 * PARAMETER(S):
7019 * [I] infoPtr : valid pointer to the listview structure
7020 * [I] nItem : item index
7021 * [IO] lprc : bounding rectangle coordinates
7022 * lprc->left specifies the portion of the item for which the bounding
7023 * rectangle will be retrieved.
7025 * LVIR_BOUNDS Returns the bounding rectangle of the entire item,
7026 * including the icon and label.
7028 * * For LVS_ICON
7029 * * Experiment shows that native control returns:
7030 * * width = min (48, length of text line)
7031 * * .left = position.x - (width - iconsize.cx)/2
7032 * * .right = .left + width
7033 * * height = #lines of text * ntmHeight + icon height + 8
7034 * * .top = position.y - 2
7035 * * .bottom = .top + height
7036 * * separation between items .y = itemSpacing.cy - height
7037 * * .x = itemSpacing.cx - width
7038 * LVIR_ICON Returns the bounding rectangle of the icon or small icon.
7040 * * For LVS_ICON
7041 * * Experiment shows that native control returns:
7042 * * width = iconSize.cx + 16
7043 * * .left = position.x - (width - iconsize.cx)/2
7044 * * .right = .left + width
7045 * * height = iconSize.cy + 4
7046 * * .top = position.y - 2
7047 * * .bottom = .top + height
7048 * * separation between items .y = itemSpacing.cy - height
7049 * * .x = itemSpacing.cx - width
7050 * LVIR_LABEL Returns the bounding rectangle of the item text.
7052 * * For LVS_ICON
7053 * * Experiment shows that native control returns:
7054 * * width = text length
7055 * * .left = position.x - width/2
7056 * * .right = .left + width
7057 * * height = ntmH * linecount + 2
7058 * * .top = position.y + iconSize.cy + 6
7059 * * .bottom = .top + height
7060 * * separation between items .y = itemSpacing.cy - height
7061 * * .x = itemSpacing.cx - width
7062 * LVIR_SELECTBOUNDS Returns the union of the LVIR_ICON and LVIR_LABEL
7063 * rectangles, but excludes columns in report view.
7065 * RETURN:
7066 * SUCCESS : TRUE
7067 * FAILURE : FALSE
7069 * NOTES
7070 * Note that the bounding rectangle of the label in the LVS_ICON view depends
7071 * upon whether the window has the focus currently and on whether the item
7072 * is the one with the focus. Ensure that the control's record of which
7073 * item has the focus agrees with the items' records.
7075 static BOOL LISTVIEW_GetItemRect(const LISTVIEW_INFO *infoPtr, INT nItem, LPRECT lprc)
7077 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
7078 BOOL doLabel = TRUE, oversizedBox = FALSE;
7079 POINT Position, Origin;
7080 LVITEMW lvItem;
7081 LONG mode;
7083 TRACE("(hwnd=%p, nItem=%d, lprc=%p)\n", infoPtr->hwndSelf, nItem, lprc);
7085 if (!lprc || nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
7087 LISTVIEW_GetOrigin(infoPtr, &Origin);
7088 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position);
7090 /* Be smart and try to figure out the minimum we have to do */
7091 if (lprc->left == LVIR_ICON) doLabel = FALSE;
7092 if (infoPtr->uView == LV_VIEW_DETAILS && lprc->left == LVIR_BOUNDS) doLabel = FALSE;
7093 if (infoPtr->uView == LV_VIEW_ICON && lprc->left != LVIR_ICON &&
7094 infoPtr->bFocus && LISTVIEW_GetItemState(infoPtr, nItem, LVIS_FOCUSED))
7095 oversizedBox = TRUE;
7097 /* get what we need from the item before hand, so we make
7098 * only one request. This can speed up things, if data
7099 * is stored on the app side */
7100 lvItem.mask = 0;
7101 if (infoPtr->uView == LV_VIEW_DETAILS) lvItem.mask |= LVIF_INDENT;
7102 if (doLabel) lvItem.mask |= LVIF_TEXT;
7103 lvItem.iItem = nItem;
7104 lvItem.iSubItem = 0;
7105 lvItem.pszText = szDispText;
7106 lvItem.cchTextMax = DISP_TEXT_SIZE;
7107 if (lvItem.mask && !LISTVIEW_GetItemW(infoPtr, &lvItem)) return FALSE;
7108 /* we got the state already up, simulate it here, to avoid a reget */
7109 if (infoPtr->uView == LV_VIEW_ICON && (lprc->left != LVIR_ICON))
7111 lvItem.mask |= LVIF_STATE;
7112 lvItem.stateMask = LVIS_FOCUSED;
7113 lvItem.state = (oversizedBox ? LVIS_FOCUSED : 0);
7116 if (infoPtr->uView == LV_VIEW_DETAILS && (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT) && lprc->left == LVIR_SELECTBOUNDS)
7117 lprc->left = LVIR_BOUNDS;
7119 mode = lprc->left;
7120 switch(lprc->left)
7122 case LVIR_ICON:
7123 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, NULL, NULL, lprc, NULL, NULL);
7124 break;
7126 case LVIR_LABEL:
7127 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, NULL, NULL, NULL, NULL, lprc);
7128 break;
7130 case LVIR_BOUNDS:
7131 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, lprc, NULL, NULL, NULL, NULL);
7132 break;
7134 case LVIR_SELECTBOUNDS:
7135 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, NULL, lprc, NULL, NULL, NULL);
7136 break;
7138 default:
7139 WARN("Unknown value: %d\n", lprc->left);
7140 return FALSE;
7143 if (infoPtr->uView == LV_VIEW_DETAILS)
7145 if (mode != LVIR_BOUNDS)
7146 OffsetRect(lprc, Origin.x + LISTVIEW_GetColumnInfo(infoPtr, 0)->rcHeader.left,
7147 Position.y + Origin.y);
7148 else
7149 OffsetRect(lprc, Origin.x, Position.y + Origin.y);
7151 else
7152 OffsetRect(lprc, Position.x + Origin.x, Position.y + Origin.y);
7154 TRACE(" rect=%s\n", wine_dbgstr_rect(lprc));
7156 return TRUE;
7159 /***
7160 * DESCRIPTION:
7161 * Retrieves the spacing between listview control items.
7163 * PARAMETER(S):
7164 * [I] infoPtr : valid pointer to the listview structure
7165 * [IO] lprc : rectangle to receive the output
7166 * on input, lprc->top = nSubItem
7167 * lprc->left = LVIR_ICON | LVIR_BOUNDS | LVIR_LABEL
7169 * NOTE: for subItem = 0, we should return the bounds of the _entire_ item,
7170 * not only those of the first column.
7172 * RETURN:
7173 * TRUE: success
7174 * FALSE: failure
7176 static BOOL LISTVIEW_GetSubItemRect(const LISTVIEW_INFO *infoPtr, INT item, LPRECT lprc)
7178 RECT rect = { 0, 0, 0, 0 };
7179 POINT origin;
7180 INT y;
7182 if (!lprc) return FALSE;
7184 TRACE("(item=%d, subitem=%d, type=%d)\n", item, lprc->top, lprc->left);
7185 /* Subitem of '0' means item itself, and this works for all control view modes */
7186 if (lprc->top == 0)
7187 return LISTVIEW_GetItemRect(infoPtr, item, lprc);
7189 if (infoPtr->uView != LV_VIEW_DETAILS) return FALSE;
7191 LISTVIEW_GetOrigin(infoPtr, &origin);
7192 /* this works for any item index, no matter if it exists or not */
7193 y = item * infoPtr->nItemHeight + origin.y;
7195 if (infoPtr->hwndHeader && SendMessageW(infoPtr->hwndHeader, HDM_GETITEMRECT, lprc->top, (LPARAM)&rect))
7197 rect.top = 0;
7198 rect.bottom = infoPtr->nItemHeight;
7200 else
7202 /* Native implementation is broken for this case and garbage is left for left and right fields,
7203 we zero them to get predictable output */
7204 lprc->left = lprc->right = lprc->top = 0;
7205 lprc->bottom = infoPtr->nItemHeight;
7206 OffsetRect(lprc, origin.x, y);
7207 TRACE("return rect %s\n", wine_dbgstr_rect(lprc));
7208 return TRUE;
7211 switch (lprc->left)
7213 case LVIR_ICON:
7215 /* it doesn't matter if main item actually has an icon, if imagelist is set icon width is returned */
7216 if (infoPtr->himlSmall)
7217 rect.right = rect.left + infoPtr->iconSize.cx;
7218 else
7219 rect.right = rect.left;
7221 rect.bottom = rect.top + infoPtr->iconSize.cy;
7222 break;
7224 case LVIR_LABEL:
7225 case LVIR_BOUNDS:
7226 break;
7228 default:
7229 ERR("Unknown bounds=%d\n", lprc->left);
7230 return FALSE;
7233 OffsetRect(&rect, origin.x, y);
7234 *lprc = rect;
7235 TRACE("return rect %s\n", wine_dbgstr_rect(lprc));
7237 return TRUE;
7240 /***
7241 * DESCRIPTION:
7242 * Retrieves the spacing between listview control items.
7244 * PARAMETER(S):
7245 * [I] infoPtr : valid pointer to the listview structure
7246 * [I] bSmall : flag for small or large icon
7248 * RETURN:
7249 * Horizontal + vertical spacing
7251 static LONG LISTVIEW_GetItemSpacing(const LISTVIEW_INFO *infoPtr, BOOL bSmall)
7253 LONG lResult;
7255 if (!bSmall)
7257 lResult = MAKELONG(infoPtr->iconSpacing.cx, infoPtr->iconSpacing.cy);
7259 else
7261 if (infoPtr->uView == LV_VIEW_ICON)
7262 lResult = MAKELONG(DEFAULT_COLUMN_WIDTH, GetSystemMetrics(SM_CXSMICON)+HEIGHT_PADDING);
7263 else
7264 lResult = MAKELONG(infoPtr->nItemWidth, infoPtr->nItemHeight);
7266 return lResult;
7269 /***
7270 * DESCRIPTION:
7271 * Retrieves the state of a listview control item.
7273 * PARAMETER(S):
7274 * [I] infoPtr : valid pointer to the listview structure
7275 * [I] nItem : item index
7276 * [I] uMask : state mask
7278 * RETURN:
7279 * State specified by the mask.
7281 static UINT LISTVIEW_GetItemState(const LISTVIEW_INFO *infoPtr, INT nItem, UINT uMask)
7283 LVITEMW lvItem;
7285 if (nItem < 0 || nItem >= infoPtr->nItemCount) return 0;
7287 lvItem.iItem = nItem;
7288 lvItem.iSubItem = 0;
7289 lvItem.mask = LVIF_STATE;
7290 lvItem.stateMask = uMask;
7291 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) return 0;
7293 return lvItem.state & uMask;
7296 /***
7297 * DESCRIPTION:
7298 * Retrieves the text of a listview control item or subitem.
7300 * PARAMETER(S):
7301 * [I] hwnd : window handle
7302 * [I] nItem : item index
7303 * [IO] lpLVItem : item information
7304 * [I] isW : TRUE if lpLVItem is Unicode
7306 * RETURN:
7307 * SUCCESS : string length
7308 * FAILURE : 0
7310 static INT LISTVIEW_GetItemTextT(const LISTVIEW_INFO *infoPtr, INT nItem, LPLVITEMW lpLVItem, BOOL isW)
7312 if (!lpLVItem || nItem < 0 || nItem >= infoPtr->nItemCount) return 0;
7314 lpLVItem->mask = LVIF_TEXT;
7315 lpLVItem->iItem = nItem;
7316 if (!LISTVIEW_GetItemExtT(infoPtr, lpLVItem, isW)) return 0;
7318 return textlenT(lpLVItem->pszText, isW);
7321 /***
7322 * DESCRIPTION:
7323 * Searches for an item based on properties + relationships.
7325 * PARAMETER(S):
7326 * [I] infoPtr : valid pointer to the listview structure
7327 * [I] nItem : item index
7328 * [I] uFlags : relationship flag
7330 * RETURN:
7331 * SUCCESS : item index
7332 * FAILURE : -1
7334 static INT LISTVIEW_GetNextItem(const LISTVIEW_INFO *infoPtr, INT nItem, UINT uFlags)
7336 UINT uMask = 0;
7337 LVFINDINFOW lvFindInfo;
7338 INT nCountPerColumn;
7339 INT nCountPerRow;
7340 INT i;
7342 TRACE("nItem=%d, uFlags=%x, nItemCount=%d\n", nItem, uFlags, infoPtr->nItemCount);
7343 if (nItem < -1 || nItem >= infoPtr->nItemCount) return -1;
7345 ZeroMemory(&lvFindInfo, sizeof(lvFindInfo));
7347 if (uFlags & LVNI_CUT)
7348 uMask |= LVIS_CUT;
7350 if (uFlags & LVNI_DROPHILITED)
7351 uMask |= LVIS_DROPHILITED;
7353 if (uFlags & LVNI_FOCUSED)
7354 uMask |= LVIS_FOCUSED;
7356 if (uFlags & LVNI_SELECTED)
7357 uMask |= LVIS_SELECTED;
7359 /* if we're asked for the focused item, that's only one,
7360 * so it's worth optimizing */
7361 if (uFlags & LVNI_FOCUSED)
7363 if ((LISTVIEW_GetItemState(infoPtr, infoPtr->nFocusedItem, uMask) & uMask) != uMask) return -1;
7364 return (infoPtr->nFocusedItem == nItem) ? -1 : infoPtr->nFocusedItem;
7367 if (uFlags & LVNI_ABOVE)
7369 if ((infoPtr->uView == LV_VIEW_LIST) || (infoPtr->uView == LV_VIEW_DETAILS))
7371 while (nItem >= 0)
7373 nItem--;
7374 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7375 return nItem;
7378 else
7380 /* Special case for autoarrange - move 'til the top of a list */
7381 if (is_autoarrange(infoPtr))
7383 nCountPerRow = LISTVIEW_GetCountPerRow(infoPtr);
7384 while (nItem - nCountPerRow >= 0)
7386 nItem -= nCountPerRow;
7387 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7388 return nItem;
7390 return -1;
7392 lvFindInfo.flags = LVFI_NEARESTXY;
7393 lvFindInfo.vkDirection = VK_UP;
7394 LISTVIEW_GetItemPosition(infoPtr, nItem, &lvFindInfo.pt);
7395 while ((nItem = LISTVIEW_FindItemW(infoPtr, nItem, &lvFindInfo)) != -1)
7397 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7398 return nItem;
7402 else if (uFlags & LVNI_BELOW)
7404 if ((infoPtr->uView == LV_VIEW_LIST) || (infoPtr->uView == LV_VIEW_DETAILS))
7406 while (nItem < infoPtr->nItemCount)
7408 nItem++;
7409 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7410 return nItem;
7413 else
7415 /* Special case for autoarrange - move 'til the bottom of a list */
7416 if (is_autoarrange(infoPtr))
7418 nCountPerRow = LISTVIEW_GetCountPerRow(infoPtr);
7419 while (nItem + nCountPerRow < infoPtr->nItemCount )
7421 nItem += nCountPerRow;
7422 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7423 return nItem;
7425 return -1;
7427 lvFindInfo.flags = LVFI_NEARESTXY;
7428 lvFindInfo.vkDirection = VK_DOWN;
7429 LISTVIEW_GetItemPosition(infoPtr, nItem, &lvFindInfo.pt);
7430 while ((nItem = LISTVIEW_FindItemW(infoPtr, nItem, &lvFindInfo)) != -1)
7432 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7433 return nItem;
7437 else if (uFlags & LVNI_TOLEFT)
7439 if (infoPtr->uView == LV_VIEW_LIST)
7441 nCountPerColumn = LISTVIEW_GetCountPerColumn(infoPtr);
7442 while (nItem - nCountPerColumn >= 0)
7444 nItem -= nCountPerColumn;
7445 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7446 return nItem;
7449 else if ((infoPtr->uView == LV_VIEW_SMALLICON) || (infoPtr->uView == LV_VIEW_ICON))
7451 /* Special case for autoarrange - move 'til the beginning of a row */
7452 if (is_autoarrange(infoPtr))
7454 nCountPerRow = LISTVIEW_GetCountPerRow(infoPtr);
7455 while (nItem % nCountPerRow > 0)
7457 nItem --;
7458 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7459 return nItem;
7461 return -1;
7463 lvFindInfo.flags = LVFI_NEARESTXY;
7464 lvFindInfo.vkDirection = VK_LEFT;
7465 LISTVIEW_GetItemPosition(infoPtr, nItem, &lvFindInfo.pt);
7466 while ((nItem = LISTVIEW_FindItemW(infoPtr, nItem, &lvFindInfo)) != -1)
7468 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7469 return nItem;
7473 else if (uFlags & LVNI_TORIGHT)
7475 if (infoPtr->uView == LV_VIEW_LIST)
7477 nCountPerColumn = LISTVIEW_GetCountPerColumn(infoPtr);
7478 while (nItem + nCountPerColumn < infoPtr->nItemCount)
7480 nItem += nCountPerColumn;
7481 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7482 return nItem;
7485 else if ((infoPtr->uView == LV_VIEW_SMALLICON) || (infoPtr->uView == LV_VIEW_ICON))
7487 /* Special case for autoarrange - move 'til the end of a row */
7488 if (is_autoarrange(infoPtr))
7490 nCountPerRow = LISTVIEW_GetCountPerRow(infoPtr);
7491 while (nItem % nCountPerRow < nCountPerRow - 1 )
7493 nItem ++;
7494 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7495 return nItem;
7497 return -1;
7499 lvFindInfo.flags = LVFI_NEARESTXY;
7500 lvFindInfo.vkDirection = VK_RIGHT;
7501 LISTVIEW_GetItemPosition(infoPtr, nItem, &lvFindInfo.pt);
7502 while ((nItem = LISTVIEW_FindItemW(infoPtr, nItem, &lvFindInfo)) != -1)
7504 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7505 return nItem;
7509 else
7511 nItem++;
7513 /* search by index */
7514 for (i = nItem; i < infoPtr->nItemCount; i++)
7516 if ((LISTVIEW_GetItemState(infoPtr, i, uMask) & uMask) == uMask)
7517 return i;
7521 return -1;
7524 /* LISTVIEW_GetNumberOfWorkAreas */
7526 /***
7527 * DESCRIPTION:
7528 * Retrieves the origin coordinates when in icon or small icon display mode.
7530 * PARAMETER(S):
7531 * [I] infoPtr : valid pointer to the listview structure
7532 * [O] lpptOrigin : coordinate information
7534 * RETURN:
7535 * None.
7537 static void LISTVIEW_GetOrigin(const LISTVIEW_INFO *infoPtr, LPPOINT lpptOrigin)
7539 INT nHorzPos = 0, nVertPos = 0;
7540 SCROLLINFO scrollInfo;
7542 scrollInfo.cbSize = sizeof(SCROLLINFO);
7543 scrollInfo.fMask = SIF_POS;
7545 if (GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo))
7546 nHorzPos = scrollInfo.nPos;
7547 if (GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo))
7548 nVertPos = scrollInfo.nPos;
7550 TRACE("nHorzPos=%d, nVertPos=%d\n", nHorzPos, nVertPos);
7552 lpptOrigin->x = infoPtr->rcList.left;
7553 lpptOrigin->y = infoPtr->rcList.top;
7554 if (infoPtr->uView == LV_VIEW_LIST)
7555 nHorzPos *= infoPtr->nItemWidth;
7556 else if (infoPtr->uView == LV_VIEW_DETAILS)
7557 nVertPos *= infoPtr->nItemHeight;
7559 lpptOrigin->x -= nHorzPos;
7560 lpptOrigin->y -= nVertPos;
7562 TRACE(" origin=%s\n", wine_dbgstr_point(lpptOrigin));
7565 /***
7566 * DESCRIPTION:
7567 * Retrieves the width of a string.
7569 * PARAMETER(S):
7570 * [I] hwnd : window handle
7571 * [I] lpszText : text string to process
7572 * [I] isW : TRUE if lpszText is Unicode, FALSE otherwise
7574 * RETURN:
7575 * SUCCESS : string width (in pixels)
7576 * FAILURE : zero
7578 static INT LISTVIEW_GetStringWidthT(const LISTVIEW_INFO *infoPtr, LPCWSTR lpszText, BOOL isW)
7580 SIZE stringSize;
7582 stringSize.cx = 0;
7583 if (is_text(lpszText))
7585 HFONT hFont = infoPtr->hFont ? infoPtr->hFont : infoPtr->hDefaultFont;
7586 HDC hdc = GetDC(infoPtr->hwndSelf);
7587 HFONT hOldFont = SelectObject(hdc, hFont);
7589 if (isW)
7590 GetTextExtentPointW(hdc, lpszText, lstrlenW(lpszText), &stringSize);
7591 else
7592 GetTextExtentPointA(hdc, (LPCSTR)lpszText, lstrlenA((LPCSTR)lpszText), &stringSize);
7593 SelectObject(hdc, hOldFont);
7594 ReleaseDC(infoPtr->hwndSelf, hdc);
7596 return stringSize.cx;
7599 /***
7600 * DESCRIPTION:
7601 * Determines which listview item is located at the specified position.
7603 * PARAMETER(S):
7604 * [I] infoPtr : valid pointer to the listview structure
7605 * [IO] lpht : hit test information
7606 * [I] subitem : fill out iSubItem.
7607 * [I] select : return the index only if the hit selects the item
7609 * NOTE:
7610 * (mm 20001022): We must not allow iSubItem to be touched, for
7611 * an app might pass only a structure with space up to iItem!
7612 * (MS Office 97 does that for instance in the file open dialog)
7614 * RETURN:
7615 * SUCCESS : item index
7616 * FAILURE : -1
7618 static INT LISTVIEW_HitTest(const LISTVIEW_INFO *infoPtr, LPLVHITTESTINFO lpht, BOOL subitem, BOOL select)
7620 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
7621 RECT rcBox, rcBounds, rcState, rcIcon, rcLabel, rcSearch;
7622 POINT Origin, Position, opt;
7623 BOOL is_fullrow;
7624 LVITEMW lvItem;
7625 ITERATOR i;
7626 INT iItem;
7628 TRACE("(pt=%s, subitem=%d, select=%d)\n", wine_dbgstr_point(&lpht->pt), subitem, select);
7630 lpht->flags = 0;
7631 lpht->iItem = -1;
7632 if (subitem) lpht->iSubItem = 0;
7634 LISTVIEW_GetOrigin(infoPtr, &Origin);
7636 /* set whole list relation flags */
7637 if (subitem && infoPtr->uView == LV_VIEW_DETAILS)
7639 /* LVM_SUBITEMHITTEST checks left bound of possible client area */
7640 if (infoPtr->rcList.left > lpht->pt.x && Origin.x < lpht->pt.x)
7641 lpht->flags |= LVHT_TOLEFT;
7643 if (lpht->pt.y < infoPtr->rcList.top && lpht->pt.y >= 0)
7644 opt.y = lpht->pt.y + infoPtr->rcList.top;
7645 else
7646 opt.y = lpht->pt.y;
7648 if (infoPtr->rcList.bottom < opt.y)
7649 lpht->flags |= LVHT_BELOW;
7651 else
7653 if (infoPtr->rcList.left > lpht->pt.x)
7654 lpht->flags |= LVHT_TOLEFT;
7655 else if (infoPtr->rcList.right < lpht->pt.x)
7656 lpht->flags |= LVHT_TORIGHT;
7658 if (infoPtr->rcList.top > lpht->pt.y)
7659 lpht->flags |= LVHT_ABOVE;
7660 else if (infoPtr->rcList.bottom < lpht->pt.y)
7661 lpht->flags |= LVHT_BELOW;
7664 /* even if item is invalid try to find subitem */
7665 if (infoPtr->uView == LV_VIEW_DETAILS && subitem)
7667 RECT *pRect;
7668 INT j;
7670 opt.x = lpht->pt.x - Origin.x;
7672 lpht->iSubItem = -1;
7673 for (j = 0; j < DPA_GetPtrCount(infoPtr->hdpaColumns); j++)
7675 pRect = &LISTVIEW_GetColumnInfo(infoPtr, j)->rcHeader;
7677 if ((opt.x >= pRect->left) && (opt.x < pRect->right))
7679 lpht->iSubItem = j;
7680 break;
7683 TRACE("lpht->iSubItem=%d\n", lpht->iSubItem);
7685 /* if we're outside horizontal columns bounds there's nothing to test further */
7686 if (lpht->iSubItem == -1)
7688 lpht->iItem = -1;
7689 lpht->flags = LVHT_NOWHERE;
7690 return -1;
7694 TRACE("lpht->flags=0x%x\n", lpht->flags);
7695 if (lpht->flags) return -1;
7697 lpht->flags |= LVHT_NOWHERE;
7699 /* first deal with the large items */
7700 rcSearch.left = lpht->pt.x;
7701 rcSearch.top = lpht->pt.y;
7702 rcSearch.right = rcSearch.left + 1;
7703 rcSearch.bottom = rcSearch.top + 1;
7705 iterator_frameditems(&i, infoPtr, &rcSearch);
7706 iterator_next(&i); /* go to first item in the sequence */
7707 iItem = i.nItem;
7708 iterator_destroy(&i);
7710 TRACE("lpht->iItem=%d\n", iItem);
7711 if (iItem == -1) return -1;
7713 lvItem.mask = LVIF_STATE | LVIF_TEXT;
7714 if (infoPtr->uView == LV_VIEW_DETAILS) lvItem.mask |= LVIF_INDENT;
7715 lvItem.stateMask = LVIS_STATEIMAGEMASK;
7716 if (infoPtr->uView == LV_VIEW_ICON) lvItem.stateMask |= LVIS_FOCUSED;
7717 lvItem.iItem = iItem;
7718 lvItem.iSubItem = subitem ? lpht->iSubItem : 0;
7719 lvItem.pszText = szDispText;
7720 lvItem.cchTextMax = DISP_TEXT_SIZE;
7721 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) return -1;
7722 if (!infoPtr->bFocus) lvItem.state &= ~LVIS_FOCUSED;
7724 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, &rcBox, NULL, &rcIcon, &rcState, &rcLabel);
7725 LISTVIEW_GetItemOrigin(infoPtr, iItem, &Position);
7726 opt.x = lpht->pt.x - Position.x - Origin.x;
7728 if (lpht->pt.y < infoPtr->rcList.top && lpht->pt.y >= 0)
7729 opt.y = lpht->pt.y - Position.y - Origin.y + infoPtr->rcList.top;
7730 else
7731 opt.y = lpht->pt.y - Position.y - Origin.y;
7733 if (infoPtr->uView == LV_VIEW_DETAILS)
7735 rcBounds = rcBox;
7736 if (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT)
7737 opt.x = lpht->pt.x - Origin.x;
7739 else
7741 UnionRect(&rcBounds, &rcIcon, &rcLabel);
7742 UnionRect(&rcBounds, &rcBounds, &rcState);
7744 TRACE("rcBounds=%s\n", wine_dbgstr_rect(&rcBounds));
7745 if (!PtInRect(&rcBounds, opt)) return -1;
7747 /* That's a special case - row rectangle is used as item rectangle and
7748 returned flags contain all item parts. */
7749 is_fullrow = (infoPtr->uView == LV_VIEW_DETAILS) && ((infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT) || (infoPtr->dwStyle & LVS_OWNERDRAWFIXED));
7751 if (PtInRect(&rcIcon, opt))
7752 lpht->flags |= LVHT_ONITEMICON;
7753 else if (PtInRect(&rcLabel, opt))
7754 lpht->flags |= LVHT_ONITEMLABEL;
7755 else if (infoPtr->himlState && PtInRect(&rcState, opt))
7756 lpht->flags |= LVHT_ONITEMSTATEICON;
7757 if (is_fullrow && !(lpht->flags & LVHT_ONITEM))
7759 lpht->flags = LVHT_ONITEM | LVHT_ABOVE;
7761 if (lpht->flags & LVHT_ONITEM)
7762 lpht->flags &= ~LVHT_NOWHERE;
7763 TRACE("lpht->flags=0x%x\n", lpht->flags);
7765 if (select && !is_fullrow)
7767 if (infoPtr->uView == LV_VIEW_DETAILS)
7769 /* get main item bounds */
7770 lvItem.iSubItem = 0;
7771 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, &rcBox, NULL, &rcIcon, &rcState, &rcLabel);
7772 UnionRect(&rcBounds, &rcIcon, &rcLabel);
7773 UnionRect(&rcBounds, &rcBounds, &rcState);
7775 if (!PtInRect(&rcBounds, opt)) iItem = -1;
7777 return lpht->iItem = iItem;
7780 /***
7781 * DESCRIPTION:
7782 * Inserts a new item in the listview control.
7784 * PARAMETER(S):
7785 * [I] infoPtr : valid pointer to the listview structure
7786 * [I] lpLVItem : item information
7787 * [I] isW : TRUE if lpLVItem is Unicode, FALSE if it's ANSI
7789 * RETURN:
7790 * SUCCESS : new item index
7791 * FAILURE : -1
7793 static INT LISTVIEW_InsertItemT(LISTVIEW_INFO *infoPtr, const LVITEMW *lpLVItem, BOOL isW)
7795 INT nItem;
7796 HDPA hdpaSubItems;
7797 NMLISTVIEW nmlv;
7798 ITEM_INFO *lpItem;
7799 ITEM_ID *lpID;
7800 BOOL is_sorted, has_changed;
7801 LVITEMW item;
7802 HWND hwndSelf = infoPtr->hwndSelf;
7804 TRACE("(item=%s, isW=%d)\n", debuglvitem_t(lpLVItem, isW), isW);
7806 if (infoPtr->dwStyle & LVS_OWNERDATA) return infoPtr->nItemCount++;
7808 /* make sure it's an item, and not a subitem; cannot insert a subitem */
7809 if (!lpLVItem || lpLVItem->iSubItem) return -1;
7811 if (!is_assignable_item(lpLVItem, infoPtr->dwStyle)) return -1;
7813 if (!(lpItem = Alloc(sizeof(ITEM_INFO)))) return -1;
7815 /* insert item in listview control data structure */
7816 if ( !(hdpaSubItems = DPA_Create(8)) ) goto fail;
7817 if ( !DPA_SetPtr(hdpaSubItems, 0, lpItem) ) assert (FALSE);
7819 /* link with id struct */
7820 if (!(lpID = Alloc(sizeof(ITEM_ID)))) goto fail;
7821 lpItem->id = lpID;
7822 lpID->item = hdpaSubItems;
7823 lpID->id = get_next_itemid(infoPtr);
7824 if ( DPA_InsertPtr(infoPtr->hdpaItemIds, infoPtr->nItemCount, lpID) == -1) goto fail;
7826 is_sorted = (infoPtr->dwStyle & (LVS_SORTASCENDING | LVS_SORTDESCENDING)) &&
7827 !(infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && (LPSTR_TEXTCALLBACKW != lpLVItem->pszText);
7829 if (lpLVItem->iItem < 0 && !is_sorted) return -1;
7831 /* calculate new item index */
7832 if (is_sorted)
7834 HDPA hItem;
7835 ITEM_INFO *item_s;
7836 INT i = 0, cmpv;
7837 WCHAR *textW;
7839 textW = textdupTtoW(lpLVItem->pszText, isW);
7841 while (i < infoPtr->nItemCount)
7843 hItem = DPA_GetPtr( infoPtr->hdpaItems, i);
7844 item_s = DPA_GetPtr(hItem, 0);
7846 cmpv = textcmpWT(item_s->hdr.pszText, textW, TRUE);
7847 if (infoPtr->dwStyle & LVS_SORTDESCENDING) cmpv *= -1;
7849 if (cmpv >= 0) break;
7850 i++;
7853 textfreeT(textW, isW);
7855 nItem = i;
7857 else
7858 nItem = min(lpLVItem->iItem, infoPtr->nItemCount);
7860 TRACE("inserting at %d, sorted=%d, count=%d, iItem=%d\n", nItem, is_sorted, infoPtr->nItemCount, lpLVItem->iItem);
7861 nItem = DPA_InsertPtr( infoPtr->hdpaItems, nItem, hdpaSubItems );
7862 if (nItem == -1) goto fail;
7863 infoPtr->nItemCount++;
7865 /* shift indices first so they don't get tangled */
7866 LISTVIEW_ShiftIndices(infoPtr, nItem, 1);
7868 /* set the item attributes */
7869 if (lpLVItem->mask & (LVIF_GROUPID|LVIF_COLUMNS))
7871 /* full size structure expected - _WIN32IE >= 0x560 */
7872 item = *lpLVItem;
7874 else if (lpLVItem->mask & LVIF_INDENT)
7876 /* indent member expected - _WIN32IE >= 0x300 */
7877 memcpy(&item, lpLVItem, offsetof( LVITEMW, iGroupId ));
7879 else
7881 /* minimal structure expected */
7882 memcpy(&item, lpLVItem, offsetof( LVITEMW, iIndent ));
7884 item.iItem = nItem;
7885 if (infoPtr->dwLvExStyle & LVS_EX_CHECKBOXES)
7887 if (item.mask & LVIF_STATE)
7889 item.stateMask |= LVIS_STATEIMAGEMASK;
7890 item.state &= ~LVIS_STATEIMAGEMASK;
7891 item.state |= INDEXTOSTATEIMAGEMASK(1);
7893 else
7895 item.mask |= LVIF_STATE;
7896 item.stateMask = LVIS_STATEIMAGEMASK;
7897 item.state = INDEXTOSTATEIMAGEMASK(1);
7901 if (!set_main_item(infoPtr, &item, TRUE, isW, &has_changed)) goto undo;
7903 /* make room for the position, if we are in the right mode */
7904 if ((infoPtr->uView == LV_VIEW_SMALLICON) || (infoPtr->uView == LV_VIEW_ICON))
7906 if (DPA_InsertPtr(infoPtr->hdpaPosX, nItem, 0) == -1)
7907 goto undo;
7908 if (DPA_InsertPtr(infoPtr->hdpaPosY, nItem, 0) == -1)
7910 DPA_DeletePtr(infoPtr->hdpaPosX, nItem);
7911 goto undo;
7915 /* send LVN_INSERTITEM notification */
7916 memset(&nmlv, 0, sizeof(NMLISTVIEW));
7917 nmlv.iItem = nItem;
7918 nmlv.lParam = lpItem->lParam;
7919 notify_listview(infoPtr, LVN_INSERTITEM, &nmlv);
7920 if (!IsWindow(hwndSelf))
7921 return -1;
7923 /* align items (set position of each item) */
7924 if (infoPtr->uView == LV_VIEW_SMALLICON || infoPtr->uView == LV_VIEW_ICON)
7926 POINT pt;
7928 if (infoPtr->dwStyle & LVS_ALIGNLEFT)
7929 LISTVIEW_NextIconPosLeft(infoPtr, &pt);
7930 else
7931 LISTVIEW_NextIconPosTop(infoPtr, &pt);
7933 LISTVIEW_MoveIconTo(infoPtr, nItem, &pt, TRUE);
7936 /* now is the invalidation fun */
7937 LISTVIEW_ScrollOnInsert(infoPtr, nItem, 1);
7938 return nItem;
7940 undo:
7941 LISTVIEW_ShiftIndices(infoPtr, nItem, -1);
7942 LISTVIEW_ShiftFocus(infoPtr, infoPtr->nFocusedItem, nItem, -1);
7943 DPA_DeletePtr(infoPtr->hdpaItems, nItem);
7944 infoPtr->nItemCount--;
7945 fail:
7946 DPA_DeletePtr(hdpaSubItems, 0);
7947 DPA_Destroy (hdpaSubItems);
7948 Free (lpItem);
7949 return -1;
7952 /***
7953 * DESCRIPTION:
7954 * Checks item visibility.
7956 * PARAMETER(S):
7957 * [I] infoPtr : valid pointer to the listview structure
7958 * [I] nFirst : item index to check for
7960 * RETURN:
7961 * Item visible : TRUE
7962 * Item invisible or failure : FALSE
7964 static BOOL LISTVIEW_IsItemVisible(const LISTVIEW_INFO *infoPtr, INT nItem)
7966 POINT Origin, Position;
7967 RECT rcItem;
7968 HDC hdc;
7969 BOOL ret;
7971 TRACE("nItem=%d\n", nItem);
7973 if (nItem < 0 || nItem >= DPA_GetPtrCount(infoPtr->hdpaItems)) return FALSE;
7975 LISTVIEW_GetOrigin(infoPtr, &Origin);
7976 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position);
7977 rcItem.left = Position.x + Origin.x;
7978 rcItem.top = Position.y + Origin.y;
7979 rcItem.right = rcItem.left + infoPtr->nItemWidth;
7980 rcItem.bottom = rcItem.top + infoPtr->nItemHeight;
7982 hdc = GetDC(infoPtr->hwndSelf);
7983 if (!hdc) return FALSE;
7984 ret = RectVisible(hdc, &rcItem);
7985 ReleaseDC(infoPtr->hwndSelf, hdc);
7987 return ret;
7990 /***
7991 * DESCRIPTION:
7992 * Redraws a range of items.
7994 * PARAMETER(S):
7995 * [I] infoPtr : valid pointer to the listview structure
7996 * [I] nFirst : first item
7997 * [I] nLast : last item
7999 * RETURN:
8000 * SUCCESS : TRUE
8001 * FAILURE : FALSE
8003 static BOOL LISTVIEW_RedrawItems(const LISTVIEW_INFO *infoPtr, INT nFirst, INT nLast)
8005 INT i;
8007 for (i = max(nFirst, 0); i <= min(nLast, infoPtr->nItemCount - 1); i++)
8008 LISTVIEW_InvalidateItem(infoPtr, i);
8010 return TRUE;
8013 /***
8014 * DESCRIPTION:
8015 * Scroll the content of a listview.
8017 * PARAMETER(S):
8018 * [I] infoPtr : valid pointer to the listview structure
8019 * [I] dx : horizontal scroll amount in pixels
8020 * [I] dy : vertical scroll amount in pixels
8022 * RETURN:
8023 * SUCCESS : TRUE
8024 * FAILURE : FALSE
8026 * COMMENTS:
8027 * If the control is in report view (LV_VIEW_DETAILS) the control can
8028 * be scrolled only in line increments. "dy" will be rounded to the
8029 * nearest number of pixels that are a whole line. Ex: if line height
8030 * is 16 and an 8 is passed, the list will be scrolled by 16. If a 7
8031 * is passed, then the scroll will be 0. (per MSDN 7/2002)
8033 static BOOL LISTVIEW_Scroll(LISTVIEW_INFO *infoPtr, INT dx, INT dy)
8035 switch(infoPtr->uView) {
8036 case LV_VIEW_DETAILS:
8037 dy += (dy < 0 ? -1 : 1) * infoPtr->nItemHeight/2;
8038 dy /= infoPtr->nItemHeight;
8039 break;
8040 case LV_VIEW_LIST:
8041 if (dy != 0) return FALSE;
8042 break;
8043 default: /* icon */
8044 break;
8047 if (dx != 0) LISTVIEW_HScroll(infoPtr, SB_INTERNAL, dx);
8048 if (dy != 0) LISTVIEW_VScroll(infoPtr, SB_INTERNAL, dy);
8050 return TRUE;
8053 /***
8054 * DESCRIPTION:
8055 * Sets the background color.
8057 * PARAMETER(S):
8058 * [I] infoPtr : valid pointer to the listview structure
8059 * [I] color : background color
8061 * RETURN:
8062 * SUCCESS : TRUE
8063 * FAILURE : FALSE
8065 static BOOL LISTVIEW_SetBkColor(LISTVIEW_INFO *infoPtr, COLORREF color)
8067 TRACE("(color=%x)\n", color);
8069 if(infoPtr->clrBk != color) {
8070 if (infoPtr->clrBk != CLR_NONE) DeleteObject(infoPtr->hBkBrush);
8071 infoPtr->clrBk = color;
8072 if (color == CLR_NONE)
8073 infoPtr->hBkBrush = (HBRUSH)GetClassLongPtrW(infoPtr->hwndSelf, GCLP_HBRBACKGROUND);
8074 else
8076 infoPtr->hBkBrush = CreateSolidBrush(color);
8077 infoPtr->dwLvExStyle &= ~LVS_EX_TRANSPARENTBKGND;
8081 return TRUE;
8084 /* LISTVIEW_SetBkImage */
8086 /*** Helper for {Insert,Set}ColumnT *only* */
8087 static void column_fill_hditem(const LISTVIEW_INFO *infoPtr, HDITEMW *lphdi, INT nColumn,
8088 const LVCOLUMNW *lpColumn, BOOL isW)
8090 if (lpColumn->mask & LVCF_FMT)
8092 /* format member is valid */
8093 lphdi->mask |= HDI_FORMAT;
8095 /* set text alignment (leftmost column must be left-aligned) */
8096 if (nColumn == 0 || (lpColumn->fmt & LVCFMT_JUSTIFYMASK) == LVCFMT_LEFT)
8097 lphdi->fmt |= HDF_LEFT;
8098 else if ((lpColumn->fmt & LVCFMT_JUSTIFYMASK) == LVCFMT_RIGHT)
8099 lphdi->fmt |= HDF_RIGHT;
8100 else if ((lpColumn->fmt & LVCFMT_JUSTIFYMASK) == LVCFMT_CENTER)
8101 lphdi->fmt |= HDF_CENTER;
8103 if (lpColumn->fmt & LVCFMT_BITMAP_ON_RIGHT)
8104 lphdi->fmt |= HDF_BITMAP_ON_RIGHT;
8106 if (lpColumn->fmt & LVCFMT_COL_HAS_IMAGES)
8108 lphdi->fmt |= HDF_IMAGE;
8109 lphdi->iImage = I_IMAGECALLBACK;
8112 if (lpColumn->fmt & LVCFMT_FIXED_WIDTH)
8113 lphdi->fmt |= HDF_FIXEDWIDTH;
8116 if (lpColumn->mask & LVCF_WIDTH)
8118 lphdi->mask |= HDI_WIDTH;
8119 if(lpColumn->cx == LVSCW_AUTOSIZE_USEHEADER)
8121 /* make it fill the remainder of the controls width */
8122 RECT rcHeader;
8123 INT item_index;
8125 for(item_index = 0; item_index < (nColumn - 1); item_index++)
8127 LISTVIEW_GetHeaderRect(infoPtr, item_index, &rcHeader);
8128 lphdi->cxy += rcHeader.right - rcHeader.left;
8131 /* retrieve the layout of the header */
8132 GetClientRect(infoPtr->hwndSelf, &rcHeader);
8133 TRACE("start cxy=%d rcHeader=%s\n", lphdi->cxy, wine_dbgstr_rect(&rcHeader));
8135 lphdi->cxy = (rcHeader.right - rcHeader.left) - lphdi->cxy;
8137 else
8138 lphdi->cxy = lpColumn->cx;
8141 if (lpColumn->mask & LVCF_TEXT)
8143 lphdi->mask |= HDI_TEXT | HDI_FORMAT;
8144 lphdi->fmt |= HDF_STRING;
8145 lphdi->pszText = lpColumn->pszText;
8146 lphdi->cchTextMax = textlenT(lpColumn->pszText, isW);
8149 if (lpColumn->mask & LVCF_IMAGE)
8151 lphdi->mask |= HDI_IMAGE;
8152 lphdi->iImage = lpColumn->iImage;
8155 if (lpColumn->mask & LVCF_ORDER)
8157 lphdi->mask |= HDI_ORDER;
8158 lphdi->iOrder = lpColumn->iOrder;
8163 /***
8164 * DESCRIPTION:
8165 * Inserts a new column.
8167 * PARAMETER(S):
8168 * [I] infoPtr : valid pointer to the listview structure
8169 * [I] nColumn : column index
8170 * [I] lpColumn : column information
8171 * [I] isW : TRUE if lpColumn is Unicode, FALSE otherwise
8173 * RETURN:
8174 * SUCCESS : new column index
8175 * FAILURE : -1
8177 static INT LISTVIEW_InsertColumnT(LISTVIEW_INFO *infoPtr, INT nColumn,
8178 const LVCOLUMNW *lpColumn, BOOL isW)
8180 COLUMN_INFO *lpColumnInfo;
8181 INT nNewColumn;
8182 HDITEMW hdi;
8184 TRACE("(nColumn=%d, lpColumn=%s, isW=%d)\n", nColumn, debuglvcolumn_t(lpColumn, isW), isW);
8186 if (!lpColumn || nColumn < 0) return -1;
8187 nColumn = min(nColumn, DPA_GetPtrCount(infoPtr->hdpaColumns));
8189 ZeroMemory(&hdi, sizeof(HDITEMW));
8190 column_fill_hditem(infoPtr, &hdi, nColumn, lpColumn, isW);
8193 * A mask not including LVCF_WIDTH turns into a mask of width, width 10
8194 * (can be seen in SPY) otherwise column never gets added.
8196 if (!(lpColumn->mask & LVCF_WIDTH)) {
8197 hdi.mask |= HDI_WIDTH;
8198 hdi.cxy = 10;
8202 * when the iSubItem is available Windows copies it to the header lParam. It seems
8203 * to happen only in LVM_INSERTCOLUMN - not in LVM_SETCOLUMN
8205 if (lpColumn->mask & LVCF_SUBITEM)
8207 hdi.mask |= HDI_LPARAM;
8208 hdi.lParam = lpColumn->iSubItem;
8211 /* create header if not present */
8212 LISTVIEW_CreateHeader(infoPtr);
8213 if (!(LVS_NOCOLUMNHEADER & infoPtr->dwStyle) &&
8214 (infoPtr->uView == LV_VIEW_DETAILS) && (WS_VISIBLE & infoPtr->dwStyle))
8216 ShowWindow(infoPtr->hwndHeader, SW_SHOWNORMAL);
8219 /* insert item in header control */
8220 nNewColumn = SendMessageW(infoPtr->hwndHeader,
8221 isW ? HDM_INSERTITEMW : HDM_INSERTITEMA,
8222 nColumn, (LPARAM)&hdi);
8223 if (nNewColumn == -1) return -1;
8224 if (nNewColumn != nColumn) ERR("nColumn=%d, nNewColumn=%d\n", nColumn, nNewColumn);
8226 /* create our own column info */
8227 if (!(lpColumnInfo = Alloc(sizeof(COLUMN_INFO)))) goto fail;
8228 if (DPA_InsertPtr(infoPtr->hdpaColumns, nNewColumn, lpColumnInfo) == -1) goto fail;
8230 if (lpColumn->mask & LVCF_FMT) lpColumnInfo->fmt = lpColumn->fmt;
8231 if (lpColumn->mask & LVCF_MINWIDTH) lpColumnInfo->cxMin = lpColumn->cxMin;
8232 if (!SendMessageW(infoPtr->hwndHeader, HDM_GETITEMRECT, nNewColumn, (LPARAM)&lpColumnInfo->rcHeader))
8233 goto fail;
8235 /* now we have to actually adjust the data */
8236 if (!(infoPtr->dwStyle & LVS_OWNERDATA) && infoPtr->nItemCount > 0)
8238 SUBITEM_INFO *lpSubItem;
8239 HDPA hdpaSubItems;
8240 INT nItem, i;
8241 LVITEMW item;
8242 BOOL changed;
8244 item.iSubItem = nNewColumn;
8245 item.mask = LVIF_TEXT | LVIF_IMAGE;
8246 item.iImage = I_IMAGECALLBACK;
8247 item.pszText = LPSTR_TEXTCALLBACKW;
8249 for (nItem = 0; nItem < infoPtr->nItemCount; nItem++)
8251 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, nItem);
8252 for (i = 1; i < DPA_GetPtrCount(hdpaSubItems); i++)
8254 lpSubItem = DPA_GetPtr(hdpaSubItems, i);
8255 if (lpSubItem->iSubItem >= nNewColumn)
8256 lpSubItem->iSubItem++;
8259 /* add new subitem for each item */
8260 item.iItem = nItem;
8261 set_sub_item(infoPtr, &item, isW, &changed);
8265 /* make space for the new column */
8266 LISTVIEW_ScrollColumns(infoPtr, nNewColumn + 1, lpColumnInfo->rcHeader.right - lpColumnInfo->rcHeader.left);
8267 LISTVIEW_UpdateItemSize(infoPtr);
8269 return nNewColumn;
8271 fail:
8272 if (nNewColumn != -1) SendMessageW(infoPtr->hwndHeader, HDM_DELETEITEM, nNewColumn, 0);
8273 if (lpColumnInfo)
8275 DPA_DeletePtr(infoPtr->hdpaColumns, nNewColumn);
8276 Free(lpColumnInfo);
8278 return -1;
8281 /***
8282 * DESCRIPTION:
8283 * Sets the attributes of a header item.
8285 * PARAMETER(S):
8286 * [I] infoPtr : valid pointer to the listview structure
8287 * [I] nColumn : column index
8288 * [I] lpColumn : column attributes
8289 * [I] isW: if TRUE, then lpColumn is a LPLVCOLUMNW, else it is a LPLVCOLUMNA
8291 * RETURN:
8292 * SUCCESS : TRUE
8293 * FAILURE : FALSE
8295 static BOOL LISTVIEW_SetColumnT(const LISTVIEW_INFO *infoPtr, INT nColumn,
8296 const LVCOLUMNW *lpColumn, BOOL isW)
8298 HDITEMW hdi, hdiget;
8299 BOOL bResult;
8301 TRACE("(nColumn=%d, lpColumn=%s, isW=%d)\n", nColumn, debuglvcolumn_t(lpColumn, isW), isW);
8303 if (!lpColumn || nColumn < 0 || nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return FALSE;
8305 ZeroMemory(&hdi, sizeof(HDITEMW));
8306 if (lpColumn->mask & LVCF_FMT)
8308 hdi.mask |= HDI_FORMAT;
8309 hdiget.mask = HDI_FORMAT;
8310 if (SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, nColumn, (LPARAM)&hdiget))
8311 hdi.fmt = hdiget.fmt & HDF_STRING;
8313 column_fill_hditem(infoPtr, &hdi, nColumn, lpColumn, isW);
8315 /* set header item attributes */
8316 bResult = SendMessageW(infoPtr->hwndHeader, isW ? HDM_SETITEMW : HDM_SETITEMA, nColumn, (LPARAM)&hdi);
8317 if (!bResult) return FALSE;
8319 if (lpColumn->mask & LVCF_FMT)
8321 COLUMN_INFO *lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, nColumn);
8322 INT oldFmt = lpColumnInfo->fmt;
8324 lpColumnInfo->fmt = lpColumn->fmt;
8325 if ((oldFmt ^ lpColumn->fmt) & (LVCFMT_JUSTIFYMASK | LVCFMT_IMAGE))
8327 if (infoPtr->uView == LV_VIEW_DETAILS) LISTVIEW_InvalidateColumn(infoPtr, nColumn);
8331 if (lpColumn->mask & LVCF_MINWIDTH)
8332 LISTVIEW_GetColumnInfo(infoPtr, nColumn)->cxMin = lpColumn->cxMin;
8334 return TRUE;
8337 /***
8338 * DESCRIPTION:
8339 * Sets the column order array
8341 * PARAMETERS:
8342 * [I] infoPtr : valid pointer to the listview structure
8343 * [I] iCount : number of elements in column order array
8344 * [I] lpiArray : pointer to column order array
8346 * RETURN:
8347 * SUCCESS : TRUE
8348 * FAILURE : FALSE
8350 static BOOL LISTVIEW_SetColumnOrderArray(LISTVIEW_INFO *infoPtr, INT iCount, const INT *lpiArray)
8352 if (!infoPtr->hwndHeader) return FALSE;
8353 infoPtr->colRectsDirty = TRUE;
8354 return SendMessageW(infoPtr->hwndHeader, HDM_SETORDERARRAY, iCount, (LPARAM)lpiArray);
8357 /***
8358 * DESCRIPTION:
8359 * Sets the width of a column
8361 * PARAMETERS:
8362 * [I] infoPtr : valid pointer to the listview structure
8363 * [I] nColumn : column index
8364 * [I] cx : column width
8366 * RETURN:
8367 * SUCCESS : TRUE
8368 * FAILURE : FALSE
8370 static BOOL LISTVIEW_SetColumnWidth(LISTVIEW_INFO *infoPtr, INT nColumn, INT cx)
8372 WCHAR szDispText[DISP_TEXT_SIZE] = { 0 };
8373 INT max_cx = 0;
8374 HDITEMW hdi;
8376 TRACE("(nColumn=%d, cx=%d)\n", nColumn, cx);
8378 /* set column width only if in report or list mode */
8379 if (infoPtr->uView != LV_VIEW_DETAILS && infoPtr->uView != LV_VIEW_LIST) return FALSE;
8381 /* take care of invalid cx values - LVSCW_AUTOSIZE_* values are negative,
8382 with _USEHEADER being the lowest */
8383 if (infoPtr->uView == LV_VIEW_DETAILS && cx < LVSCW_AUTOSIZE_USEHEADER) cx = LVSCW_AUTOSIZE;
8384 else if (infoPtr->uView == LV_VIEW_LIST && cx <= 0) return FALSE;
8386 /* resize all columns if in LV_VIEW_LIST mode */
8387 if(infoPtr->uView == LV_VIEW_LIST)
8389 infoPtr->nItemWidth = cx;
8390 LISTVIEW_InvalidateList(infoPtr);
8391 return TRUE;
8394 if (nColumn < 0 || nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return FALSE;
8396 if (cx == LVSCW_AUTOSIZE || (cx == LVSCW_AUTOSIZE_USEHEADER && nColumn < DPA_GetPtrCount(infoPtr->hdpaColumns) -1))
8398 INT nLabelWidth;
8399 LVITEMW lvItem;
8401 lvItem.mask = LVIF_TEXT;
8402 lvItem.iItem = 0;
8403 lvItem.iSubItem = nColumn;
8404 lvItem.cchTextMax = DISP_TEXT_SIZE;
8405 for (; lvItem.iItem < infoPtr->nItemCount; lvItem.iItem++)
8407 lvItem.pszText = szDispText;
8408 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) continue;
8409 nLabelWidth = LISTVIEW_GetStringWidthT(infoPtr, lvItem.pszText, TRUE);
8410 if (max_cx < nLabelWidth) max_cx = nLabelWidth;
8412 if (infoPtr->himlSmall && (nColumn == 0 || (LISTVIEW_GetColumnInfo(infoPtr, nColumn)->fmt & LVCFMT_IMAGE)))
8413 max_cx += infoPtr->iconSize.cx;
8414 max_cx += TRAILING_LABEL_PADDING;
8415 if (nColumn == 0 && (infoPtr->dwLvExStyle & LVS_EX_CHECKBOXES))
8416 max_cx += GetSystemMetrics(SM_CXSMICON);
8419 /* autosize based on listview items width */
8420 if(cx == LVSCW_AUTOSIZE)
8421 cx = max_cx;
8422 else if(cx == LVSCW_AUTOSIZE_USEHEADER)
8424 /* if iCol is the last column make it fill the remainder of the controls width */
8425 if(nColumn == DPA_GetPtrCount(infoPtr->hdpaColumns) - 1)
8427 RECT rcHeader;
8428 POINT Origin;
8430 LISTVIEW_GetOrigin(infoPtr, &Origin);
8431 LISTVIEW_GetHeaderRect(infoPtr, nColumn, &rcHeader);
8433 cx = infoPtr->rcList.right - Origin.x - rcHeader.left;
8435 else
8437 /* Despite what the MS docs say, if this is not the last
8438 column, then MS resizes the column to the width of the
8439 largest text string in the column, including headers
8440 and items. This is different from LVSCW_AUTOSIZE in that
8441 LVSCW_AUTOSIZE ignores the header string length. */
8442 cx = 0;
8444 /* retrieve header text */
8445 hdi.mask = HDI_TEXT|HDI_FORMAT|HDI_IMAGE|HDI_BITMAP;
8446 hdi.cchTextMax = DISP_TEXT_SIZE;
8447 hdi.pszText = szDispText;
8448 if (SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, nColumn, (LPARAM)&hdi))
8450 HDC hdc = GetDC(infoPtr->hwndSelf);
8451 HFONT old_font = SelectObject(hdc, (HFONT)SendMessageW(infoPtr->hwndHeader, WM_GETFONT, 0, 0));
8452 HIMAGELIST himl = (HIMAGELIST)SendMessageW(infoPtr->hwndHeader, HDM_GETIMAGELIST, 0, 0);
8453 INT bitmap_margin = 0;
8454 SIZE size;
8456 if (GetTextExtentPoint32W(hdc, hdi.pszText, lstrlenW(hdi.pszText), &size))
8457 cx = size.cx + TRAILING_HEADER_PADDING;
8459 if (hdi.fmt & (HDF_IMAGE|HDF_BITMAP))
8460 bitmap_margin = SendMessageW(infoPtr->hwndHeader, HDM_GETBITMAPMARGIN, 0, 0);
8462 if ((hdi.fmt & HDF_IMAGE) && himl)
8464 INT icon_cx, icon_cy;
8466 if (!ImageList_GetIconSize(himl, &icon_cx, &icon_cy))
8467 cx += icon_cx + 2*bitmap_margin;
8469 else if (hdi.fmt & HDF_BITMAP)
8471 BITMAP bmp;
8473 GetObjectW(hdi.hbm, sizeof(BITMAP), &bmp);
8474 cx += bmp.bmWidth + 2*bitmap_margin;
8477 SelectObject(hdc, old_font);
8478 ReleaseDC(infoPtr->hwndSelf, hdc);
8480 cx = max (cx, max_cx);
8484 if (cx < 0) return FALSE;
8486 /* call header to update the column change */
8487 hdi.mask = HDI_WIDTH;
8488 hdi.cxy = max(cx, LISTVIEW_GetColumnInfo(infoPtr, nColumn)->cxMin);
8489 TRACE("hdi.cxy=%d\n", hdi.cxy);
8490 return SendMessageW(infoPtr->hwndHeader, HDM_SETITEMW, nColumn, (LPARAM)&hdi);
8493 /***
8494 * Creates the checkbox imagelist. Helper for LISTVIEW_SetExtendedListViewStyle
8497 static HIMAGELIST LISTVIEW_CreateCheckBoxIL(const LISTVIEW_INFO *infoPtr)
8499 HDC hdc_wnd, hdc;
8500 HBITMAP hbm_im, hbm_mask, hbm_orig;
8501 RECT rc;
8502 HBRUSH hbr_white = GetStockObject(WHITE_BRUSH);
8503 HBRUSH hbr_black = GetStockObject(BLACK_BRUSH);
8504 HIMAGELIST himl;
8506 himl = ImageList_Create(GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON),
8507 ILC_COLOR | ILC_MASK, 2, 2);
8508 hdc_wnd = GetDC(infoPtr->hwndSelf);
8509 hdc = CreateCompatibleDC(hdc_wnd);
8510 hbm_im = CreateCompatibleBitmap(hdc_wnd, GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON));
8511 hbm_mask = CreateBitmap(GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), 1, 1, NULL);
8512 ReleaseDC(infoPtr->hwndSelf, hdc_wnd);
8514 SetRect(&rc, 0, 0, GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON));
8515 hbm_orig = SelectObject(hdc, hbm_mask);
8516 FillRect(hdc, &rc, hbr_white);
8517 InflateRect(&rc, -2, -2);
8518 FillRect(hdc, &rc, hbr_black);
8520 SelectObject(hdc, hbm_im);
8521 DrawFrameControl(hdc, &rc, DFC_BUTTON, DFCS_BUTTONCHECK | DFCS_MONO);
8522 SelectObject(hdc, hbm_orig);
8523 ImageList_Add(himl, hbm_im, hbm_mask);
8525 SelectObject(hdc, hbm_im);
8526 DrawFrameControl(hdc, &rc, DFC_BUTTON, DFCS_BUTTONCHECK | DFCS_MONO | DFCS_CHECKED);
8527 SelectObject(hdc, hbm_orig);
8528 ImageList_Add(himl, hbm_im, hbm_mask);
8530 DeleteObject(hbm_mask);
8531 DeleteObject(hbm_im);
8532 DeleteDC(hdc);
8534 return himl;
8537 /***
8538 * DESCRIPTION:
8539 * Sets the extended listview style.
8541 * PARAMETERS:
8542 * [I] infoPtr : valid pointer to the listview structure
8543 * [I] dwMask : mask
8544 * [I] dwStyle : style
8546 * RETURN:
8547 * SUCCESS : previous style
8548 * FAILURE : 0
8550 static DWORD LISTVIEW_SetExtendedListViewStyle(LISTVIEW_INFO *infoPtr, DWORD mask, DWORD ex_style)
8552 DWORD old_ex_style = infoPtr->dwLvExStyle;
8554 TRACE("mask=0x%08x, ex_style=0x%08x\n", mask, ex_style);
8556 /* set new style */
8557 if (mask)
8558 infoPtr->dwLvExStyle = (old_ex_style & ~mask) | (ex_style & mask);
8559 else
8560 infoPtr->dwLvExStyle = ex_style;
8562 if((infoPtr->dwLvExStyle ^ old_ex_style) & LVS_EX_CHECKBOXES)
8564 HIMAGELIST himl = 0;
8565 if(infoPtr->dwLvExStyle & LVS_EX_CHECKBOXES)
8567 LVITEMW item;
8568 item.mask = LVIF_STATE;
8569 item.stateMask = LVIS_STATEIMAGEMASK;
8570 item.state = INDEXTOSTATEIMAGEMASK(1);
8571 LISTVIEW_SetItemState(infoPtr, -1, &item);
8573 himl = LISTVIEW_CreateCheckBoxIL(infoPtr);
8574 if(!(infoPtr->dwStyle & LVS_SHAREIMAGELISTS))
8575 ImageList_Destroy(infoPtr->himlState);
8577 himl = LISTVIEW_SetImageList(infoPtr, LVSIL_STATE, himl);
8578 /* checkbox list replaces previous custom list or... */
8579 if(((infoPtr->dwLvExStyle & LVS_EX_CHECKBOXES) &&
8580 !(infoPtr->dwStyle & LVS_SHAREIMAGELISTS)) ||
8581 /* ...previous was checkbox list */
8582 (old_ex_style & LVS_EX_CHECKBOXES))
8583 ImageList_Destroy(himl);
8586 if((infoPtr->dwLvExStyle ^ old_ex_style) & LVS_EX_HEADERDRAGDROP)
8588 DWORD style;
8590 /* if not already created */
8591 LISTVIEW_CreateHeader(infoPtr);
8593 style = GetWindowLongW(infoPtr->hwndHeader, GWL_STYLE);
8594 if (infoPtr->dwLvExStyle & LVS_EX_HEADERDRAGDROP)
8595 style |= HDS_DRAGDROP;
8596 else
8597 style &= ~HDS_DRAGDROP;
8598 SetWindowLongW(infoPtr->hwndHeader, GWL_STYLE, style);
8601 /* GRIDLINES adds decoration at top so changes sizes */
8602 if((infoPtr->dwLvExStyle ^ old_ex_style) & LVS_EX_GRIDLINES)
8604 LISTVIEW_CreateHeader(infoPtr);
8605 LISTVIEW_UpdateSize(infoPtr);
8608 if((infoPtr->dwLvExStyle ^ old_ex_style) & LVS_EX_FULLROWSELECT)
8610 LISTVIEW_CreateHeader(infoPtr);
8613 if((infoPtr->dwLvExStyle ^ old_ex_style) & LVS_EX_TRANSPARENTBKGND)
8615 if (infoPtr->dwLvExStyle & LVS_EX_TRANSPARENTBKGND)
8616 LISTVIEW_SetBkColor(infoPtr, CLR_NONE);
8619 if((infoPtr->dwLvExStyle ^ old_ex_style) & LVS_EX_HEADERINALLVIEWS)
8621 if (infoPtr->dwLvExStyle & LVS_EX_HEADERINALLVIEWS)
8622 LISTVIEW_CreateHeader(infoPtr);
8623 else
8624 ShowWindow(infoPtr->hwndHeader, SW_HIDE);
8625 LISTVIEW_UpdateSize(infoPtr);
8626 LISTVIEW_UpdateScroll(infoPtr);
8629 LISTVIEW_InvalidateList(infoPtr);
8630 return old_ex_style;
8633 /***
8634 * DESCRIPTION:
8635 * Sets the new hot cursor used during hot tracking and hover selection.
8637 * PARAMETER(S):
8638 * [I] infoPtr : valid pointer to the listview structure
8639 * [I] hCursor : the new hot cursor handle
8641 * RETURN:
8642 * Returns the previous hot cursor
8644 static HCURSOR LISTVIEW_SetHotCursor(LISTVIEW_INFO *infoPtr, HCURSOR hCursor)
8646 HCURSOR oldCursor = infoPtr->hHotCursor;
8648 infoPtr->hHotCursor = hCursor;
8650 return oldCursor;
8654 /***
8655 * DESCRIPTION:
8656 * Sets the hot item index.
8658 * PARAMETERS:
8659 * [I] infoPtr : valid pointer to the listview structure
8660 * [I] iIndex : index
8662 * RETURN:
8663 * SUCCESS : previous hot item index
8664 * FAILURE : -1 (no hot item)
8666 static INT LISTVIEW_SetHotItem(LISTVIEW_INFO *infoPtr, INT iIndex)
8668 INT iOldIndex = infoPtr->nHotItem;
8670 infoPtr->nHotItem = iIndex;
8672 return iOldIndex;
8676 /***
8677 * DESCRIPTION:
8678 * Sets the amount of time the cursor must hover over an item before it is selected.
8680 * PARAMETER(S):
8681 * [I] infoPtr : valid pointer to the listview structure
8682 * [I] dwHoverTime : hover time, if -1 the hover time is set to the default
8684 * RETURN:
8685 * Returns the previous hover time
8687 static DWORD LISTVIEW_SetHoverTime(LISTVIEW_INFO *infoPtr, DWORD dwHoverTime)
8689 DWORD oldHoverTime = infoPtr->dwHoverTime;
8691 infoPtr->dwHoverTime = dwHoverTime;
8693 return oldHoverTime;
8696 /***
8697 * DESCRIPTION:
8698 * Sets spacing for icons of LVS_ICON style.
8700 * PARAMETER(S):
8701 * [I] infoPtr : valid pointer to the listview structure
8702 * [I] cx : horizontal spacing (-1 = system spacing, 0 = autosize)
8703 * [I] cy : vertical spacing (-1 = system spacing, 0 = autosize)
8705 * RETURN:
8706 * MAKELONG(oldcx, oldcy)
8708 static DWORD LISTVIEW_SetIconSpacing(LISTVIEW_INFO *infoPtr, INT cx, INT cy)
8710 INT iconWidth = 0, iconHeight = 0;
8711 DWORD oldspacing = MAKELONG(infoPtr->iconSpacing.cx, infoPtr->iconSpacing.cy);
8713 TRACE("requested=(%d,%d)\n", cx, cy);
8715 /* set to defaults, if instructed to */
8716 if (cx == -1 && cy == -1)
8718 infoPtr->autoSpacing = TRUE;
8719 if (infoPtr->himlNormal)
8720 ImageList_GetIconSize(infoPtr->himlNormal, &iconWidth, &iconHeight);
8721 cx = GetSystemMetrics(SM_CXICONSPACING) - GetSystemMetrics(SM_CXICON) + iconWidth;
8722 cy = GetSystemMetrics(SM_CYICONSPACING) - GetSystemMetrics(SM_CYICON) + iconHeight;
8724 else
8725 infoPtr->autoSpacing = FALSE;
8727 /* if 0 then keep width */
8728 if (cx != 0)
8729 infoPtr->iconSpacing.cx = cx;
8731 /* if 0 then keep height */
8732 if (cy != 0)
8733 infoPtr->iconSpacing.cy = cy;
8735 TRACE("old=(%d,%d), new=(%d,%d), iconSize=(%d,%d), ntmH=%d\n",
8736 LOWORD(oldspacing), HIWORD(oldspacing), infoPtr->iconSpacing.cx, infoPtr->iconSpacing.cy,
8737 infoPtr->iconSize.cx, infoPtr->iconSize.cy,
8738 infoPtr->ntmHeight);
8740 /* these depend on the iconSpacing */
8741 LISTVIEW_UpdateItemSize(infoPtr);
8743 return oldspacing;
8746 static inline void set_icon_size(SIZE *size, HIMAGELIST himl, BOOL small)
8748 INT cx, cy;
8750 if (himl && ImageList_GetIconSize(himl, &cx, &cy))
8752 size->cx = cx;
8753 size->cy = cy;
8755 else
8757 size->cx = GetSystemMetrics(small ? SM_CXSMICON : SM_CXICON);
8758 size->cy = GetSystemMetrics(small ? SM_CYSMICON : SM_CYICON);
8762 /***
8763 * DESCRIPTION:
8764 * Sets image lists.
8766 * PARAMETER(S):
8767 * [I] infoPtr : valid pointer to the listview structure
8768 * [I] nType : image list type
8769 * [I] himl : image list handle
8771 * RETURN:
8772 * SUCCESS : old image list
8773 * FAILURE : NULL
8775 static HIMAGELIST LISTVIEW_SetImageList(LISTVIEW_INFO *infoPtr, INT nType, HIMAGELIST himl)
8777 INT oldHeight = infoPtr->nItemHeight;
8778 HIMAGELIST himlOld = 0;
8780 TRACE("(nType=%d, himl=%p)\n", nType, himl);
8782 switch (nType)
8784 case LVSIL_NORMAL:
8785 himlOld = infoPtr->himlNormal;
8786 infoPtr->himlNormal = himl;
8787 if (infoPtr->uView == LV_VIEW_ICON) set_icon_size(&infoPtr->iconSize, himl, FALSE);
8788 if (infoPtr->autoSpacing)
8789 LISTVIEW_SetIconSpacing(infoPtr, -1, -1);
8790 break;
8792 case LVSIL_SMALL:
8793 himlOld = infoPtr->himlSmall;
8794 infoPtr->himlSmall = himl;
8795 if (infoPtr->uView != LV_VIEW_ICON) set_icon_size(&infoPtr->iconSize, himl, TRUE);
8796 if (infoPtr->hwndHeader)
8797 SendMessageW(infoPtr->hwndHeader, HDM_SETIMAGELIST, 0, (LPARAM)himl);
8798 break;
8800 case LVSIL_STATE:
8801 himlOld = infoPtr->himlState;
8802 infoPtr->himlState = himl;
8803 set_icon_size(&infoPtr->iconStateSize, himl, TRUE);
8804 ImageList_SetBkColor(infoPtr->himlState, CLR_NONE);
8805 break;
8807 default:
8808 ERR("Unknown icon type=%d\n", nType);
8809 return NULL;
8812 infoPtr->nItemHeight = LISTVIEW_CalculateItemHeight(infoPtr);
8813 if (infoPtr->nItemHeight != oldHeight)
8814 LISTVIEW_UpdateScroll(infoPtr);
8816 return himlOld;
8819 /***
8820 * DESCRIPTION:
8821 * Preallocates memory (does *not* set the actual count of items !)
8823 * PARAMETER(S):
8824 * [I] infoPtr : valid pointer to the listview structure
8825 * [I] nItems : item count (projected number of items to allocate)
8826 * [I] dwFlags : update flags
8828 * RETURN:
8829 * SUCCESS : TRUE
8830 * FAILURE : FALSE
8832 static BOOL LISTVIEW_SetItemCount(LISTVIEW_INFO *infoPtr, INT nItems, DWORD dwFlags)
8834 TRACE("(nItems=%d, dwFlags=%x)\n", nItems, dwFlags);
8836 if (infoPtr->dwStyle & LVS_OWNERDATA)
8838 INT nOldCount = infoPtr->nItemCount;
8839 infoPtr->nItemCount = nItems;
8841 if (nItems < nOldCount)
8843 RANGE range = { nItems, nOldCount };
8844 ranges_del(infoPtr->selectionRanges, range);
8845 if (infoPtr->nFocusedItem >= nItems)
8847 LISTVIEW_SetItemFocus(infoPtr, -1);
8848 infoPtr->nFocusedItem = -1;
8849 SetRectEmpty(&infoPtr->rcFocus);
8853 LISTVIEW_UpdateScroll(infoPtr);
8855 /* the flags are valid only in ownerdata report and list modes */
8856 if (infoPtr->uView == LV_VIEW_ICON || infoPtr->uView == LV_VIEW_SMALLICON) dwFlags = 0;
8858 if (!(dwFlags & LVSICF_NOSCROLL) && infoPtr->nFocusedItem != -1)
8859 LISTVIEW_EnsureVisible(infoPtr, infoPtr->nFocusedItem, FALSE);
8861 if (!(dwFlags & LVSICF_NOINVALIDATEALL))
8862 LISTVIEW_InvalidateList(infoPtr);
8863 else
8865 INT nFrom, nTo;
8866 POINT Origin;
8867 RECT rcErase;
8869 LISTVIEW_GetOrigin(infoPtr, &Origin);
8870 nFrom = min(nOldCount, nItems);
8871 nTo = max(nOldCount, nItems);
8873 if (infoPtr->uView == LV_VIEW_DETAILS)
8875 SetRect(&rcErase, 0, nFrom * infoPtr->nItemHeight, infoPtr->nItemWidth,
8876 nTo * infoPtr->nItemHeight);
8877 OffsetRect(&rcErase, Origin.x, Origin.y);
8878 if (IntersectRect(&rcErase, &rcErase, &infoPtr->rcList))
8879 LISTVIEW_InvalidateRect(infoPtr, &rcErase);
8881 else /* LV_VIEW_LIST */
8883 INT nPerCol = LISTVIEW_GetCountPerColumn(infoPtr);
8885 rcErase.left = (nFrom / nPerCol) * infoPtr->nItemWidth;
8886 rcErase.top = (nFrom % nPerCol) * infoPtr->nItemHeight;
8887 rcErase.right = rcErase.left + infoPtr->nItemWidth;
8888 rcErase.bottom = nPerCol * infoPtr->nItemHeight;
8889 OffsetRect(&rcErase, Origin.x, Origin.y);
8890 if (IntersectRect(&rcErase, &rcErase, &infoPtr->rcList))
8891 LISTVIEW_InvalidateRect(infoPtr, &rcErase);
8893 rcErase.left = (nFrom / nPerCol + 1) * infoPtr->nItemWidth;
8894 rcErase.top = 0;
8895 rcErase.right = (nTo / nPerCol + 1) * infoPtr->nItemWidth;
8896 rcErase.bottom = nPerCol * infoPtr->nItemHeight;
8897 OffsetRect(&rcErase, Origin.x, Origin.y);
8898 if (IntersectRect(&rcErase, &rcErase, &infoPtr->rcList))
8899 LISTVIEW_InvalidateRect(infoPtr, &rcErase);
8903 else
8905 /* According to MSDN for non-LVS_OWNERDATA this is just
8906 * a performance issue. The control allocates its internal
8907 * data structures for the number of items specified. It
8908 * cuts down on the number of memory allocations. Therefore
8909 * we will just issue a WARN here
8911 WARN("for non-ownerdata performance option not implemented.\n");
8914 return TRUE;
8917 /***
8918 * DESCRIPTION:
8919 * Sets the position of an item.
8921 * PARAMETER(S):
8922 * [I] infoPtr : valid pointer to the listview structure
8923 * [I] nItem : item index
8924 * [I] pt : coordinate
8926 * RETURN:
8927 * SUCCESS : TRUE
8928 * FAILURE : FALSE
8930 static BOOL LISTVIEW_SetItemPosition(LISTVIEW_INFO *infoPtr, INT nItem, const POINT *pt)
8932 POINT Origin, Pt;
8934 TRACE("(nItem=%d, pt=%s)\n", nItem, wine_dbgstr_point(pt));
8936 if (!pt || nItem < 0 || nItem >= infoPtr->nItemCount ||
8937 !(infoPtr->uView == LV_VIEW_ICON || infoPtr->uView == LV_VIEW_SMALLICON)) return FALSE;
8939 Pt = *pt;
8940 LISTVIEW_GetOrigin(infoPtr, &Origin);
8942 /* This point value seems to be an undocumented feature.
8943 * The best guess is that it means either at the origin,
8944 * or at true beginning of the list. I will assume the origin. */
8945 if ((Pt.x == -1) && (Pt.y == -1))
8946 Pt = Origin;
8948 if (infoPtr->uView == LV_VIEW_ICON)
8950 Pt.x -= (infoPtr->nItemWidth - infoPtr->iconSize.cx) / 2;
8951 Pt.y -= ICON_TOP_PADDING;
8953 Pt.x -= Origin.x;
8954 Pt.y -= Origin.y;
8956 return LISTVIEW_MoveIconTo(infoPtr, nItem, &Pt, FALSE);
8959 /***
8960 * DESCRIPTION:
8961 * Sets the state of one or many items.
8963 * PARAMETER(S):
8964 * [I] infoPtr : valid pointer to the listview structure
8965 * [I] nItem : item index
8966 * [I] item : item or subitem info
8968 * RETURN:
8969 * SUCCESS : TRUE
8970 * FAILURE : FALSE
8972 static BOOL LISTVIEW_SetItemState(LISTVIEW_INFO *infoPtr, INT nItem, const LVITEMW *item)
8974 BOOL ret = TRUE;
8975 LVITEMW lvItem;
8977 if (!item) return FALSE;
8979 lvItem.iItem = nItem;
8980 lvItem.iSubItem = 0;
8981 lvItem.mask = LVIF_STATE;
8982 lvItem.state = item->state;
8983 lvItem.stateMask = item->stateMask;
8984 TRACE("item=%s\n", debuglvitem_t(&lvItem, TRUE));
8986 if (nItem == -1)
8988 UINT oldstate = 0;
8989 DWORD old_mask;
8991 /* special case optimization for recurring attempt to deselect all */
8992 if (lvItem.state == 0 && lvItem.stateMask == LVIS_SELECTED && !LISTVIEW_GetSelectedCount(infoPtr))
8993 return TRUE;
8995 /* select all isn't allowed in LVS_SINGLESEL */
8996 if ((lvItem.state & lvItem.stateMask & LVIS_SELECTED) && (infoPtr->dwStyle & LVS_SINGLESEL))
8997 return FALSE;
8999 /* focus all isn't allowed */
9000 if (lvItem.state & lvItem.stateMask & LVIS_FOCUSED) return FALSE;
9002 old_mask = infoPtr->notify_mask & NOTIFY_MASK_ITEM_CHANGE;
9003 if (infoPtr->dwStyle & LVS_OWNERDATA)
9005 infoPtr->notify_mask &= ~NOTIFY_MASK_ITEM_CHANGE;
9006 if (!(lvItem.state & LVIS_SELECTED) && LISTVIEW_GetSelectedCount(infoPtr))
9007 oldstate |= LVIS_SELECTED;
9008 if (infoPtr->nFocusedItem != -1) oldstate |= LVIS_FOCUSED;
9011 /* apply to all items */
9012 for (lvItem.iItem = 0; lvItem.iItem < infoPtr->nItemCount; lvItem.iItem++)
9013 if (!LISTVIEW_SetItemT(infoPtr, &lvItem, TRUE)) ret = FALSE;
9015 if (infoPtr->dwStyle & LVS_OWNERDATA)
9017 NMLISTVIEW nmlv;
9019 infoPtr->notify_mask |= old_mask;
9021 nmlv.iItem = -1;
9022 nmlv.iSubItem = 0;
9023 nmlv.uNewState = lvItem.state & lvItem.stateMask;
9024 nmlv.uOldState = oldstate & lvItem.stateMask;
9025 nmlv.uChanged = LVIF_STATE;
9026 nmlv.ptAction.x = nmlv.ptAction.y = 0;
9027 nmlv.lParam = 0;
9029 notify_listview(infoPtr, LVN_ITEMCHANGED, &nmlv);
9032 else
9033 ret = LISTVIEW_SetItemT(infoPtr, &lvItem, TRUE);
9035 return ret;
9038 /***
9039 * DESCRIPTION:
9040 * Sets the text of an item or subitem.
9042 * PARAMETER(S):
9043 * [I] hwnd : window handle
9044 * [I] nItem : item index
9045 * [I] lpLVItem : item or subitem info
9046 * [I] isW : TRUE if input is Unicode
9048 * RETURN:
9049 * SUCCESS : TRUE
9050 * FAILURE : FALSE
9052 static BOOL LISTVIEW_SetItemTextT(LISTVIEW_INFO *infoPtr, INT nItem, const LVITEMW *lpLVItem, BOOL isW)
9054 LVITEMW lvItem;
9056 if (!lpLVItem || nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
9057 if (infoPtr->dwStyle & LVS_OWNERDATA) return FALSE;
9059 lvItem.iItem = nItem;
9060 lvItem.iSubItem = lpLVItem->iSubItem;
9061 lvItem.mask = LVIF_TEXT;
9062 lvItem.pszText = lpLVItem->pszText;
9063 lvItem.cchTextMax = lpLVItem->cchTextMax;
9065 TRACE("(nItem=%d, lpLVItem=%s, isW=%d)\n", nItem, debuglvitem_t(&lvItem, isW), isW);
9067 return LISTVIEW_SetItemT(infoPtr, &lvItem, isW);
9070 /***
9071 * DESCRIPTION:
9072 * Set item index that marks the start of a multiple selection.
9074 * PARAMETER(S):
9075 * [I] infoPtr : valid pointer to the listview structure
9076 * [I] nIndex : index
9078 * RETURN:
9079 * Index number or -1 if there is no selection mark.
9081 static INT LISTVIEW_SetSelectionMark(LISTVIEW_INFO *infoPtr, INT nIndex)
9083 INT nOldIndex = infoPtr->nSelectionMark;
9085 TRACE("(nIndex=%d)\n", nIndex);
9087 if (nIndex >= -1 && nIndex < infoPtr->nItemCount)
9088 infoPtr->nSelectionMark = nIndex;
9090 return nOldIndex;
9093 /***
9094 * DESCRIPTION:
9095 * Sets the text background color.
9097 * PARAMETER(S):
9098 * [I] infoPtr : valid pointer to the listview structure
9099 * [I] color : text background color
9101 * RETURN:
9102 * SUCCESS : TRUE
9103 * FAILURE : FALSE
9105 static BOOL LISTVIEW_SetTextBkColor(LISTVIEW_INFO *infoPtr, COLORREF color)
9107 TRACE("(color=%x)\n", color);
9109 infoPtr->clrTextBk = color;
9110 return TRUE;
9113 /***
9114 * DESCRIPTION:
9115 * Sets the text foreground color.
9117 * PARAMETER(S):
9118 * [I] infoPtr : valid pointer to the listview structure
9119 * [I] color : text color
9121 * RETURN:
9122 * SUCCESS : TRUE
9123 * FAILURE : FALSE
9125 static BOOL LISTVIEW_SetTextColor (LISTVIEW_INFO *infoPtr, COLORREF color)
9127 TRACE("(color=%x)\n", color);
9129 infoPtr->clrText = color;
9130 return TRUE;
9133 /***
9134 * DESCRIPTION:
9135 * Sets new ToolTip window to ListView control.
9137 * PARAMETER(S):
9138 * [I] infoPtr : valid pointer to the listview structure
9139 * [I] hwndNewToolTip : handle to new ToolTip
9141 * RETURN:
9142 * old tool tip
9144 static HWND LISTVIEW_SetToolTips( LISTVIEW_INFO *infoPtr, HWND hwndNewToolTip)
9146 HWND hwndOldToolTip = infoPtr->hwndToolTip;
9147 infoPtr->hwndToolTip = hwndNewToolTip;
9148 return hwndOldToolTip;
9152 * DESCRIPTION:
9153 * sets the Unicode character format flag for the control
9154 * PARAMETER(S):
9155 * [I] infoPtr :valid pointer to the listview structure
9156 * [I] fUnicode :true to switch to UNICODE false to switch to ANSI
9158 * RETURN:
9159 * Old Unicode Format
9161 static BOOL LISTVIEW_SetUnicodeFormat( LISTVIEW_INFO *infoPtr, BOOL unicode)
9163 SHORT rc = infoPtr->notifyFormat;
9164 infoPtr->notifyFormat = (unicode) ? NFR_UNICODE : NFR_ANSI;
9165 return rc == NFR_UNICODE;
9169 * DESCRIPTION:
9170 * sets the control view mode
9171 * PARAMETER(S):
9172 * [I] infoPtr :valid pointer to the listview structure
9173 * [I] nView :new view mode value
9175 * RETURN:
9176 * SUCCESS: 1
9177 * FAILURE: -1
9179 static INT LISTVIEW_SetView(LISTVIEW_INFO *infoPtr, DWORD nView)
9181 HIMAGELIST himl;
9183 if (infoPtr->uView == nView) return 1;
9185 if ((INT)nView < 0 || nView > LV_VIEW_MAX) return -1;
9186 if (nView == LV_VIEW_TILE)
9188 FIXME("View LV_VIEW_TILE unimplemented\n");
9189 return -1;
9192 infoPtr->uView = nView;
9194 SendMessageW(infoPtr->hwndEdit, WM_KILLFOCUS, 0, 0);
9195 ShowWindow(infoPtr->hwndHeader, SW_HIDE);
9197 ShowScrollBar(infoPtr->hwndSelf, SB_BOTH, FALSE);
9198 SetRectEmpty(&infoPtr->rcFocus);
9200 himl = (nView == LV_VIEW_ICON ? infoPtr->himlNormal : infoPtr->himlSmall);
9201 set_icon_size(&infoPtr->iconSize, himl, nView != LV_VIEW_ICON);
9203 switch (nView)
9205 case LV_VIEW_ICON:
9206 case LV_VIEW_SMALLICON:
9207 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
9208 break;
9209 case LV_VIEW_DETAILS:
9211 HDLAYOUT hl;
9212 WINDOWPOS wp;
9214 LISTVIEW_CreateHeader( infoPtr );
9216 hl.prc = &infoPtr->rcList;
9217 hl.pwpos = &wp;
9218 SendMessageW(infoPtr->hwndHeader, HDM_LAYOUT, 0, (LPARAM)&hl);
9219 SetWindowPos(infoPtr->hwndHeader, infoPtr->hwndSelf, wp.x, wp.y, wp.cx, wp.cy,
9220 wp.flags | ((infoPtr->dwStyle & LVS_NOCOLUMNHEADER) ? SWP_HIDEWINDOW : SWP_SHOWWINDOW));
9221 break;
9223 case LV_VIEW_LIST:
9224 break;
9227 LISTVIEW_UpdateItemSize(infoPtr);
9228 LISTVIEW_UpdateSize(infoPtr);
9229 LISTVIEW_UpdateScroll(infoPtr);
9230 LISTVIEW_InvalidateList(infoPtr);
9232 TRACE("nView=%d\n", nView);
9234 return 1;
9237 /* LISTVIEW_SetWorkAreas */
9239 /***
9240 * DESCRIPTION:
9241 * Callback internally used by LISTVIEW_SortItems() in response of LVM_SORTITEMS
9243 * PARAMETER(S):
9244 * [I] first : pointer to first ITEM_INFO to compare
9245 * [I] second : pointer to second ITEM_INFO to compare
9246 * [I] lParam : HWND of control
9248 * RETURN:
9249 * if first comes before second : negative
9250 * if first comes after second : positive
9251 * if first and second are equivalent : zero
9253 static INT WINAPI LISTVIEW_CallBackCompare(LPVOID first, LPVOID second, LPARAM lParam)
9255 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)lParam;
9256 ITEM_INFO* lv_first = DPA_GetPtr( first, 0 );
9257 ITEM_INFO* lv_second = DPA_GetPtr( second, 0 );
9259 /* Forward the call to the client defined callback */
9260 return (infoPtr->pfnCompare)( lv_first->lParam , lv_second->lParam, infoPtr->lParamSort );
9263 /***
9264 * DESCRIPTION:
9265 * Callback internally used by LISTVIEW_SortItems() in response of LVM_SORTITEMSEX
9267 * PARAMETER(S):
9268 * [I] first : pointer to first ITEM_INFO to compare
9269 * [I] second : pointer to second ITEM_INFO to compare
9270 * [I] lParam : HWND of control
9272 * RETURN:
9273 * if first comes before second : negative
9274 * if first comes after second : positive
9275 * if first and second are equivalent : zero
9277 static INT WINAPI LISTVIEW_CallBackCompareEx(LPVOID first, LPVOID second, LPARAM lParam)
9279 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)lParam;
9280 INT first_idx = DPA_GetPtrIndex( infoPtr->hdpaItems, first );
9281 INT second_idx = DPA_GetPtrIndex( infoPtr->hdpaItems, second );
9283 /* Forward the call to the client defined callback */
9284 return (infoPtr->pfnCompare)( first_idx, second_idx, infoPtr->lParamSort );
9287 /***
9288 * DESCRIPTION:
9289 * Sorts the listview items.
9291 * PARAMETER(S):
9292 * [I] infoPtr : valid pointer to the listview structure
9293 * [I] pfnCompare : application-defined value
9294 * [I] lParamSort : pointer to comparison callback
9295 * [I] IsEx : TRUE when LVM_SORTITEMSEX used
9297 * RETURN:
9298 * SUCCESS : TRUE
9299 * FAILURE : FALSE
9301 static BOOL LISTVIEW_SortItems(LISTVIEW_INFO *infoPtr, PFNLVCOMPARE pfnCompare,
9302 LPARAM lParamSort, BOOL IsEx)
9304 HDPA hdpaSubItems;
9305 ITEM_INFO *lpItem;
9306 LPVOID selectionMarkItem = NULL;
9307 LPVOID focusedItem = NULL;
9308 int i;
9310 TRACE("(pfnCompare=%p, lParamSort=%lx)\n", pfnCompare, lParamSort);
9312 if (infoPtr->dwStyle & LVS_OWNERDATA) return FALSE;
9314 if (!pfnCompare) return FALSE;
9315 if (!infoPtr->hdpaItems) return FALSE;
9317 /* if there are 0 or 1 items, there is no need to sort */
9318 if (infoPtr->nItemCount < 2) return TRUE;
9320 /* clear selection */
9321 ranges_clear(infoPtr->selectionRanges);
9323 /* save selection mark and focused item */
9324 if (infoPtr->nSelectionMark >= 0)
9325 selectionMarkItem = DPA_GetPtr(infoPtr->hdpaItems, infoPtr->nSelectionMark);
9326 if (infoPtr->nFocusedItem >= 0)
9327 focusedItem = DPA_GetPtr(infoPtr->hdpaItems, infoPtr->nFocusedItem);
9329 infoPtr->pfnCompare = pfnCompare;
9330 infoPtr->lParamSort = lParamSort;
9331 if (IsEx)
9332 DPA_Sort(infoPtr->hdpaItems, LISTVIEW_CallBackCompareEx, (LPARAM)infoPtr);
9333 else
9334 DPA_Sort(infoPtr->hdpaItems, LISTVIEW_CallBackCompare, (LPARAM)infoPtr);
9336 /* restore selection ranges */
9337 for (i=0; i < infoPtr->nItemCount; i++)
9339 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, i);
9340 lpItem = DPA_GetPtr(hdpaSubItems, 0);
9342 if (lpItem->state & LVIS_SELECTED)
9343 ranges_additem(infoPtr->selectionRanges, i);
9345 /* restore selection mark and focused item */
9346 infoPtr->nSelectionMark = DPA_GetPtrIndex(infoPtr->hdpaItems, selectionMarkItem);
9347 infoPtr->nFocusedItem = DPA_GetPtrIndex(infoPtr->hdpaItems, focusedItem);
9349 /* I believe nHotItem should be left alone, see LISTVIEW_ShiftIndices */
9351 /* refresh the display */
9352 LISTVIEW_InvalidateList(infoPtr);
9353 return TRUE;
9356 /***
9357 * DESCRIPTION:
9358 * Update theme handle after a theme change.
9360 * PARAMETER(S):
9361 * [I] infoPtr : valid pointer to the listview structure
9363 * RETURN:
9364 * SUCCESS : 0
9365 * FAILURE : something else
9367 static LRESULT LISTVIEW_ThemeChanged(const LISTVIEW_INFO *infoPtr)
9369 HTHEME theme = GetWindowTheme(infoPtr->hwndSelf);
9370 CloseThemeData(theme);
9371 OpenThemeData(infoPtr->hwndSelf, themeClass);
9372 return 0;
9375 /***
9376 * DESCRIPTION:
9377 * Updates an items or rearranges the listview control.
9379 * PARAMETER(S):
9380 * [I] infoPtr : valid pointer to the listview structure
9381 * [I] nItem : item index
9383 * RETURN:
9384 * SUCCESS : TRUE
9385 * FAILURE : FALSE
9387 static BOOL LISTVIEW_Update(LISTVIEW_INFO *infoPtr, INT nItem)
9389 TRACE("(nItem=%d)\n", nItem);
9391 if (nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
9393 /* rearrange with default alignment style */
9394 if (is_autoarrange(infoPtr))
9395 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
9396 else
9397 LISTVIEW_InvalidateItem(infoPtr, nItem);
9399 return TRUE;
9402 /***
9403 * DESCRIPTION:
9404 * Draw the track line at the place defined in the infoPtr structure.
9405 * The line is drawn with a XOR pen so drawing the line for the second time
9406 * in the same place erases the line.
9408 * PARAMETER(S):
9409 * [I] infoPtr : valid pointer to the listview structure
9411 * RETURN:
9412 * SUCCESS : TRUE
9413 * FAILURE : FALSE
9415 static BOOL LISTVIEW_DrawTrackLine(const LISTVIEW_INFO *infoPtr)
9417 HDC hdc;
9419 if (infoPtr->xTrackLine == -1)
9420 return FALSE;
9422 if (!(hdc = GetDC(infoPtr->hwndSelf)))
9423 return FALSE;
9424 PatBlt( hdc, infoPtr->xTrackLine, infoPtr->rcList.top,
9425 1, infoPtr->rcList.bottom - infoPtr->rcList.top, DSTINVERT );
9426 ReleaseDC(infoPtr->hwndSelf, hdc);
9427 return TRUE;
9430 /***
9431 * DESCRIPTION:
9432 * Called when an edit control should be displayed. This function is called after
9433 * we are sure that there was a single click - not a double click (this is a TIMERPROC).
9435 * PARAMETER(S):
9436 * [I] hwnd : Handle to the listview
9437 * [I] uMsg : WM_TIMER (ignored)
9438 * [I] idEvent : The timer ID interpreted as a pointer to a DELAYED_EDIT_ITEM struct
9439 * [I] dwTimer : The elapsed time (ignored)
9441 * RETURN:
9442 * None.
9444 static VOID CALLBACK LISTVIEW_DelayedEditItem(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime)
9446 DELAYED_ITEM_EDIT *editItem = (DELAYED_ITEM_EDIT *)idEvent;
9447 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)GetWindowLongPtrW(hwnd, 0);
9449 KillTimer(hwnd, idEvent);
9450 editItem->fEnabled = FALSE;
9451 /* check if the item is still selected */
9452 if (infoPtr->bFocus && LISTVIEW_GetItemState(infoPtr, editItem->iItem, LVIS_SELECTED))
9453 LISTVIEW_EditLabelT(infoPtr, editItem->iItem, TRUE);
9456 /***
9457 * DESCRIPTION:
9458 * Creates the listview control - the WM_NCCREATE phase.
9460 * PARAMETER(S):
9461 * [I] hwnd : window handle
9462 * [I] lpcs : the create parameters
9464 * RETURN:
9465 * Success: TRUE
9466 * Failure: FALSE
9468 static LRESULT LISTVIEW_NCCreate(HWND hwnd, WPARAM wParam, const CREATESTRUCTW *lpcs)
9470 LISTVIEW_INFO *infoPtr;
9471 LOGFONTW logFont;
9473 TRACE("(lpcs=%p)\n", lpcs);
9475 /* initialize info pointer */
9476 infoPtr = Alloc(sizeof(LISTVIEW_INFO));
9477 if (!infoPtr) return FALSE;
9479 SetWindowLongPtrW(hwnd, 0, (DWORD_PTR)infoPtr);
9481 infoPtr->hwndSelf = hwnd;
9482 infoPtr->dwStyle = lpcs->style; /* Note: may be changed in WM_CREATE */
9483 map_style_view(infoPtr);
9484 /* determine the type of structures to use */
9485 infoPtr->hwndNotify = lpcs->hwndParent;
9486 /* infoPtr->notifyFormat will be filled in WM_CREATE */
9488 /* initialize color information */
9489 infoPtr->clrBk = CLR_NONE;
9490 infoPtr->clrText = CLR_DEFAULT;
9491 infoPtr->clrTextBk = CLR_DEFAULT;
9492 LISTVIEW_SetBkColor(infoPtr, comctl32_color.clrWindow);
9494 /* set default values */
9495 infoPtr->nFocusedItem = -1;
9496 infoPtr->nSelectionMark = -1;
9497 infoPtr->nHotItem = -1;
9498 infoPtr->redraw = TRUE;
9499 infoPtr->bNoItemMetrics = TRUE;
9500 infoPtr->notify_mask = NOTIFY_MASK_UNMASK_ALL;
9501 infoPtr->autoSpacing = TRUE;
9502 infoPtr->iconSpacing.cx = GetSystemMetrics(SM_CXICONSPACING) - GetSystemMetrics(SM_CXICON);
9503 infoPtr->iconSpacing.cy = GetSystemMetrics(SM_CYICONSPACING) - GetSystemMetrics(SM_CYICON);
9504 infoPtr->nEditLabelItem = -1;
9505 infoPtr->nLButtonDownItem = -1;
9506 infoPtr->dwHoverTime = HOVER_DEFAULT; /* default system hover time */
9507 infoPtr->cWheelRemainder = 0;
9508 infoPtr->nMeasureItemHeight = 0;
9509 infoPtr->xTrackLine = -1; /* no track line */
9510 infoPtr->itemEdit.fEnabled = FALSE;
9511 infoPtr->iVersion = COMCTL32_VERSION;
9512 infoPtr->colRectsDirty = FALSE;
9514 /* get default font (icon title) */
9515 SystemParametersInfoW(SPI_GETICONTITLELOGFONT, 0, &logFont, 0);
9516 infoPtr->hDefaultFont = CreateFontIndirectW(&logFont);
9517 infoPtr->hFont = infoPtr->hDefaultFont;
9518 LISTVIEW_SaveTextMetrics(infoPtr);
9520 /* allocate memory for the data structure */
9521 if (!(infoPtr->selectionRanges = ranges_create(10))) goto fail;
9522 if (!(infoPtr->hdpaItems = DPA_Create(10))) goto fail;
9523 if (!(infoPtr->hdpaItemIds = DPA_Create(10))) goto fail;
9524 if (!(infoPtr->hdpaPosX = DPA_Create(10))) goto fail;
9525 if (!(infoPtr->hdpaPosY = DPA_Create(10))) goto fail;
9526 if (!(infoPtr->hdpaColumns = DPA_Create(10))) goto fail;
9528 return DefWindowProcW(hwnd, WM_NCCREATE, wParam, (LPARAM)lpcs);
9530 fail:
9531 DestroyWindow(infoPtr->hwndHeader);
9532 ranges_destroy(infoPtr->selectionRanges);
9533 DPA_Destroy(infoPtr->hdpaItems);
9534 DPA_Destroy(infoPtr->hdpaItemIds);
9535 DPA_Destroy(infoPtr->hdpaPosX);
9536 DPA_Destroy(infoPtr->hdpaPosY);
9537 DPA_Destroy(infoPtr->hdpaColumns);
9538 Free(infoPtr);
9539 return FALSE;
9542 /***
9543 * DESCRIPTION:
9544 * Creates the listview control - the WM_CREATE phase. Most of the data is
9545 * already set up in LISTVIEW_NCCreate
9547 * PARAMETER(S):
9548 * [I] hwnd : window handle
9549 * [I] lpcs : the create parameters
9551 * RETURN:
9552 * Success: 0
9553 * Failure: -1
9555 static LRESULT LISTVIEW_Create(HWND hwnd, const CREATESTRUCTW *lpcs)
9557 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)GetWindowLongPtrW(hwnd, 0);
9559 TRACE("(lpcs=%p, style=0x%08x)\n", lpcs, lpcs->style);
9561 infoPtr->dwStyle = lpcs->style;
9562 map_style_view(infoPtr);
9564 infoPtr->notifyFormat = SendMessageW(infoPtr->hwndNotify, WM_NOTIFYFORMAT,
9565 (WPARAM)infoPtr->hwndSelf, NF_QUERY);
9566 /* on error defaulting to ANSI notifications */
9567 if (infoPtr->notifyFormat == 0) infoPtr->notifyFormat = NFR_ANSI;
9568 TRACE("notify format=%d\n", infoPtr->notifyFormat);
9570 if ((infoPtr->uView == LV_VIEW_DETAILS) && (lpcs->style & WS_VISIBLE))
9572 if (LISTVIEW_CreateHeader(infoPtr) < 0) return -1;
9574 else
9575 infoPtr->hwndHeader = 0;
9577 /* init item size to avoid division by 0 */
9578 LISTVIEW_UpdateItemSize (infoPtr);
9579 LISTVIEW_UpdateSize (infoPtr);
9581 if (infoPtr->uView == LV_VIEW_DETAILS)
9583 if (!(LVS_NOCOLUMNHEADER & lpcs->style) && (WS_VISIBLE & lpcs->style))
9585 ShowWindow(infoPtr->hwndHeader, SW_SHOWNORMAL);
9587 LISTVIEW_UpdateScroll(infoPtr);
9588 /* send WM_MEASUREITEM notification */
9589 if (infoPtr->dwStyle & LVS_OWNERDRAWFIXED) notify_measureitem(infoPtr);
9592 OpenThemeData(hwnd, themeClass);
9594 /* initialize the icon sizes */
9595 set_icon_size(&infoPtr->iconSize, infoPtr->himlNormal, infoPtr->uView != LV_VIEW_ICON);
9596 set_icon_size(&infoPtr->iconStateSize, infoPtr->himlState, TRUE);
9597 return 0;
9600 /***
9601 * DESCRIPTION:
9602 * Destroys the listview control.
9604 * PARAMETER(S):
9605 * [I] infoPtr : valid pointer to the listview structure
9607 * RETURN:
9608 * Success: 0
9609 * Failure: -1
9611 static LRESULT LISTVIEW_Destroy(LISTVIEW_INFO *infoPtr)
9613 HTHEME theme = GetWindowTheme(infoPtr->hwndSelf);
9614 CloseThemeData(theme);
9616 /* delete all items */
9617 LISTVIEW_DeleteAllItems(infoPtr, TRUE);
9619 return 0;
9622 /***
9623 * DESCRIPTION:
9624 * Enables the listview control.
9626 * PARAMETER(S):
9627 * [I] infoPtr : valid pointer to the listview structure
9628 * [I] bEnable : specifies whether to enable or disable the window
9630 * RETURN:
9631 * SUCCESS : TRUE
9632 * FAILURE : FALSE
9634 static BOOL LISTVIEW_Enable(const LISTVIEW_INFO *infoPtr)
9636 if (infoPtr->dwStyle & LVS_OWNERDRAWFIXED)
9637 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
9638 return TRUE;
9641 /***
9642 * DESCRIPTION:
9643 * Erases the background of the listview control.
9645 * PARAMETER(S):
9646 * [I] infoPtr : valid pointer to the listview structure
9647 * [I] hdc : device context handle
9649 * RETURN:
9650 * SUCCESS : TRUE
9651 * FAILURE : FALSE
9653 static inline BOOL LISTVIEW_EraseBkgnd(const LISTVIEW_INFO *infoPtr, HDC hdc)
9655 RECT rc;
9657 TRACE("(hdc=%p)\n", hdc);
9659 if (!GetClipBox(hdc, &rc)) return FALSE;
9661 if (infoPtr->clrBk == CLR_NONE)
9663 if (infoPtr->dwLvExStyle & LVS_EX_TRANSPARENTBKGND)
9664 return SendMessageW(infoPtr->hwndNotify, WM_PRINTCLIENT,
9665 (WPARAM)hdc, PRF_ERASEBKGND);
9666 else
9667 return SendMessageW(infoPtr->hwndNotify, WM_ERASEBKGND, (WPARAM)hdc, 0);
9670 /* for double buffered controls we need to do this during refresh */
9671 if (infoPtr->dwLvExStyle & LVS_EX_DOUBLEBUFFER) return FALSE;
9673 return LISTVIEW_FillBkgnd(infoPtr, hdc, &rc);
9677 /***
9678 * DESCRIPTION:
9679 * Helper function for LISTVIEW_[HV]Scroll *only*.
9680 * Performs vertical/horizontal scrolling by a give amount.
9682 * PARAMETER(S):
9683 * [I] infoPtr : valid pointer to the listview structure
9684 * [I] dx : amount of horizontal scroll
9685 * [I] dy : amount of vertical scroll
9687 static void scroll_list(LISTVIEW_INFO *infoPtr, INT dx, INT dy)
9689 /* now we can scroll the list */
9690 ScrollWindowEx(infoPtr->hwndSelf, dx, dy, &infoPtr->rcList,
9691 &infoPtr->rcList, 0, 0, SW_ERASE | SW_INVALIDATE);
9692 /* if we have focus, adjust rect */
9693 OffsetRect(&infoPtr->rcFocus, dx, dy);
9694 UpdateWindow(infoPtr->hwndSelf);
9697 /***
9698 * DESCRIPTION:
9699 * Performs vertical scrolling.
9701 * PARAMETER(S):
9702 * [I] infoPtr : valid pointer to the listview structure
9703 * [I] nScrollCode : scroll code
9704 * [I] nScrollDiff : units to scroll in SB_INTERNAL mode, 0 otherwise
9705 * [I] hScrollWnd : scrollbar control window handle
9707 * RETURN:
9708 * Zero
9710 * NOTES:
9711 * SB_LINEUP/SB_LINEDOWN:
9712 * for LVS_ICON, LVS_SMALLICON is 37 by experiment
9713 * for LVS_REPORT is 1 line
9714 * for LVS_LIST cannot occur
9717 static LRESULT LISTVIEW_VScroll(LISTVIEW_INFO *infoPtr, INT nScrollCode,
9718 INT nScrollDiff)
9720 INT nOldScrollPos, nNewScrollPos;
9721 SCROLLINFO scrollInfo;
9722 BOOL is_an_icon;
9724 TRACE("(nScrollCode=%d(%s), nScrollDiff=%d)\n", nScrollCode,
9725 debugscrollcode(nScrollCode), nScrollDiff);
9727 if (infoPtr->hwndEdit) SendMessageW(infoPtr->hwndEdit, WM_KILLFOCUS, 0, 0);
9729 scrollInfo.cbSize = sizeof(SCROLLINFO);
9730 scrollInfo.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_TRACKPOS;
9732 is_an_icon = ((infoPtr->uView == LV_VIEW_ICON) || (infoPtr->uView == LV_VIEW_SMALLICON));
9734 if (!GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo)) return 1;
9736 nOldScrollPos = scrollInfo.nPos;
9737 switch (nScrollCode)
9739 case SB_INTERNAL:
9740 break;
9742 case SB_LINEUP:
9743 nScrollDiff = (is_an_icon) ? -LISTVIEW_SCROLL_ICON_LINE_SIZE : -1;
9744 break;
9746 case SB_LINEDOWN:
9747 nScrollDiff = (is_an_icon) ? LISTVIEW_SCROLL_ICON_LINE_SIZE : 1;
9748 break;
9750 case SB_PAGEUP:
9751 nScrollDiff = -scrollInfo.nPage;
9752 break;
9754 case SB_PAGEDOWN:
9755 nScrollDiff = scrollInfo.nPage;
9756 break;
9758 case SB_THUMBPOSITION:
9759 case SB_THUMBTRACK:
9760 nScrollDiff = scrollInfo.nTrackPos - scrollInfo.nPos;
9761 break;
9763 default:
9764 nScrollDiff = 0;
9767 /* quit right away if pos isn't changing */
9768 if (nScrollDiff == 0) return 0;
9770 /* calculate new position, and handle overflows */
9771 nNewScrollPos = scrollInfo.nPos + nScrollDiff;
9772 if (nScrollDiff > 0) {
9773 if (nNewScrollPos < nOldScrollPos ||
9774 nNewScrollPos > scrollInfo.nMax)
9775 nNewScrollPos = scrollInfo.nMax;
9776 } else {
9777 if (nNewScrollPos > nOldScrollPos ||
9778 nNewScrollPos < scrollInfo.nMin)
9779 nNewScrollPos = scrollInfo.nMin;
9782 /* set the new position, and reread in case it changed */
9783 scrollInfo.fMask = SIF_POS;
9784 scrollInfo.nPos = nNewScrollPos;
9785 nNewScrollPos = SetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo, TRUE);
9787 /* carry on only if it really changed */
9788 if (nNewScrollPos == nOldScrollPos) return 0;
9790 /* now adjust to client coordinates */
9791 nScrollDiff = nOldScrollPos - nNewScrollPos;
9792 if (infoPtr->uView == LV_VIEW_DETAILS) nScrollDiff *= infoPtr->nItemHeight;
9794 /* and scroll the window */
9795 scroll_list(infoPtr, 0, nScrollDiff);
9797 return 0;
9800 /***
9801 * DESCRIPTION:
9802 * Performs horizontal scrolling.
9804 * PARAMETER(S):
9805 * [I] infoPtr : valid pointer to the listview structure
9806 * [I] nScrollCode : scroll code
9807 * [I] nScrollDiff : units to scroll in SB_INTERNAL mode, 0 otherwise
9808 * [I] hScrollWnd : scrollbar control window handle
9810 * RETURN:
9811 * Zero
9813 * NOTES:
9814 * SB_LINELEFT/SB_LINERIGHT:
9815 * for LVS_ICON, LVS_SMALLICON 1 pixel
9816 * for LVS_REPORT is 1 pixel
9817 * for LVS_LIST is 1 column --> which is a 1 because the
9818 * scroll is based on columns not pixels
9821 static LRESULT LISTVIEW_HScroll(LISTVIEW_INFO *infoPtr, INT nScrollCode,
9822 INT nScrollDiff)
9824 INT nOldScrollPos, nNewScrollPos;
9825 SCROLLINFO scrollInfo;
9826 BOOL is_an_icon;
9828 TRACE("(nScrollCode=%d(%s), nScrollDiff=%d)\n", nScrollCode,
9829 debugscrollcode(nScrollCode), nScrollDiff);
9831 if (infoPtr->hwndEdit) SendMessageW(infoPtr->hwndEdit, WM_KILLFOCUS, 0, 0);
9833 scrollInfo.cbSize = sizeof(SCROLLINFO);
9834 scrollInfo.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_TRACKPOS;
9836 is_an_icon = ((infoPtr->uView == LV_VIEW_ICON) || (infoPtr->uView == LV_VIEW_SMALLICON));
9838 if (!GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo)) return 1;
9840 nOldScrollPos = scrollInfo.nPos;
9842 switch (nScrollCode)
9844 case SB_INTERNAL:
9845 break;
9847 case SB_LINELEFT:
9848 nScrollDiff = (is_an_icon) ? -LISTVIEW_SCROLL_ICON_LINE_SIZE : -1;
9849 break;
9851 case SB_LINERIGHT:
9852 nScrollDiff = (is_an_icon) ? LISTVIEW_SCROLL_ICON_LINE_SIZE : 1;
9853 break;
9855 case SB_PAGELEFT:
9856 nScrollDiff = -scrollInfo.nPage;
9857 break;
9859 case SB_PAGERIGHT:
9860 nScrollDiff = scrollInfo.nPage;
9861 break;
9863 case SB_THUMBPOSITION:
9864 case SB_THUMBTRACK:
9865 nScrollDiff = scrollInfo.nTrackPos - scrollInfo.nPos;
9866 break;
9868 default:
9869 nScrollDiff = 0;
9872 /* quit right away if pos isn't changing */
9873 if (nScrollDiff == 0) return 0;
9875 /* calculate new position, and handle overflows */
9876 nNewScrollPos = scrollInfo.nPos + nScrollDiff;
9877 if (nScrollDiff > 0) {
9878 if (nNewScrollPos < nOldScrollPos ||
9879 nNewScrollPos > scrollInfo.nMax)
9880 nNewScrollPos = scrollInfo.nMax;
9881 } else {
9882 if (nNewScrollPos > nOldScrollPos ||
9883 nNewScrollPos < scrollInfo.nMin)
9884 nNewScrollPos = scrollInfo.nMin;
9887 /* set the new position, and reread in case it changed */
9888 scrollInfo.fMask = SIF_POS;
9889 scrollInfo.nPos = nNewScrollPos;
9890 nNewScrollPos = SetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo, TRUE);
9892 /* carry on only if it really changed */
9893 if (nNewScrollPos == nOldScrollPos) return 0;
9895 if (infoPtr->hwndHeader) LISTVIEW_UpdateHeaderSize(infoPtr, nNewScrollPos);
9897 /* now adjust to client coordinates */
9898 nScrollDiff = nOldScrollPos - nNewScrollPos;
9899 if (infoPtr->uView == LV_VIEW_LIST) nScrollDiff *= infoPtr->nItemWidth;
9901 /* and scroll the window */
9902 scroll_list(infoPtr, nScrollDiff, 0);
9904 return 0;
9907 static LRESULT LISTVIEW_MouseWheel(LISTVIEW_INFO *infoPtr, INT wheelDelta)
9909 UINT pulScrollLines = 3;
9911 TRACE("(wheelDelta=%d)\n", wheelDelta);
9913 switch(infoPtr->uView)
9915 case LV_VIEW_ICON:
9916 case LV_VIEW_SMALLICON:
9918 * listview should be scrolled by a multiple of 37 dependently on its dimension or its visible item number
9919 * should be fixed in the future.
9921 LISTVIEW_VScroll(infoPtr, SB_INTERNAL, (wheelDelta > 0) ?
9922 -LISTVIEW_SCROLL_ICON_LINE_SIZE : LISTVIEW_SCROLL_ICON_LINE_SIZE);
9923 break;
9925 case LV_VIEW_DETAILS:
9926 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES,0, &pulScrollLines, 0);
9928 /* if scrolling changes direction, ignore left overs */
9929 if ((wheelDelta < 0 && infoPtr->cWheelRemainder < 0) ||
9930 (wheelDelta > 0 && infoPtr->cWheelRemainder > 0))
9931 infoPtr->cWheelRemainder += wheelDelta;
9932 else
9933 infoPtr->cWheelRemainder = wheelDelta;
9934 if (infoPtr->cWheelRemainder && pulScrollLines)
9936 int cLineScroll;
9937 pulScrollLines = min((UINT)LISTVIEW_GetCountPerColumn(infoPtr), pulScrollLines);
9938 cLineScroll = pulScrollLines * (float)infoPtr->cWheelRemainder / WHEEL_DELTA;
9939 infoPtr->cWheelRemainder -= WHEEL_DELTA * cLineScroll / (int)pulScrollLines;
9940 LISTVIEW_VScroll(infoPtr, SB_INTERNAL, -cLineScroll);
9942 break;
9944 case LV_VIEW_LIST:
9945 LISTVIEW_HScroll(infoPtr, (wheelDelta > 0) ? SB_LINELEFT : SB_LINERIGHT, 0);
9946 break;
9948 return 0;
9951 /***
9952 * DESCRIPTION:
9953 * ???
9955 * PARAMETER(S):
9956 * [I] infoPtr : valid pointer to the listview structure
9957 * [I] nVirtualKey : virtual key
9958 * [I] lKeyData : key data
9960 * RETURN:
9961 * Zero
9963 static LRESULT LISTVIEW_KeyDown(LISTVIEW_INFO *infoPtr, INT nVirtualKey, LONG lKeyData)
9965 HWND hwndSelf = infoPtr->hwndSelf;
9966 INT nItem = -1;
9967 NMLVKEYDOWN nmKeyDown;
9969 TRACE("(nVirtualKey=%d, lKeyData=%d)\n", nVirtualKey, lKeyData);
9971 /* send LVN_KEYDOWN notification */
9972 nmKeyDown.wVKey = nVirtualKey;
9973 nmKeyDown.flags = 0;
9974 notify_hdr(infoPtr, LVN_KEYDOWN, &nmKeyDown.hdr);
9975 if (!IsWindow(hwndSelf))
9976 return 0;
9978 switch (nVirtualKey)
9980 case VK_SPACE:
9981 nItem = infoPtr->nFocusedItem;
9982 if (infoPtr->dwLvExStyle & LVS_EX_CHECKBOXES)
9983 toggle_checkbox_state(infoPtr, infoPtr->nFocusedItem);
9984 break;
9986 case VK_RETURN:
9987 if ((infoPtr->nItemCount > 0) && (infoPtr->nFocusedItem != -1))
9989 if (!notify(infoPtr, NM_RETURN)) return 0;
9990 if (!notify(infoPtr, LVN_ITEMACTIVATE)) return 0;
9992 break;
9994 case VK_HOME:
9995 if (infoPtr->nItemCount > 0)
9996 nItem = 0;
9997 break;
9999 case VK_END:
10000 if (infoPtr->nItemCount > 0)
10001 nItem = infoPtr->nItemCount - 1;
10002 break;
10004 case VK_LEFT:
10005 nItem = LISTVIEW_GetNextItem(infoPtr, infoPtr->nFocusedItem, LVNI_TOLEFT);
10006 break;
10008 case VK_UP:
10009 nItem = LISTVIEW_GetNextItem(infoPtr, infoPtr->nFocusedItem, LVNI_ABOVE);
10010 break;
10012 case VK_RIGHT:
10013 nItem = LISTVIEW_GetNextItem(infoPtr, infoPtr->nFocusedItem, LVNI_TORIGHT);
10014 break;
10016 case VK_DOWN:
10017 nItem = LISTVIEW_GetNextItem(infoPtr, infoPtr->nFocusedItem, LVNI_BELOW);
10018 break;
10020 case VK_PRIOR:
10021 if (infoPtr->uView == LV_VIEW_DETAILS)
10023 INT topidx = LISTVIEW_GetTopIndex(infoPtr);
10024 if (infoPtr->nFocusedItem == topidx)
10025 nItem = topidx - LISTVIEW_GetCountPerColumn(infoPtr) + 1;
10026 else
10027 nItem = topidx;
10029 else
10030 nItem = infoPtr->nFocusedItem - LISTVIEW_GetCountPerColumn(infoPtr)
10031 * LISTVIEW_GetCountPerRow(infoPtr);
10032 if(nItem < 0) nItem = 0;
10033 break;
10035 case VK_NEXT:
10036 if (infoPtr->uView == LV_VIEW_DETAILS)
10038 INT topidx = LISTVIEW_GetTopIndex(infoPtr);
10039 INT cnt = LISTVIEW_GetCountPerColumn(infoPtr);
10040 if (infoPtr->nFocusedItem == topidx + cnt - 1)
10041 nItem = infoPtr->nFocusedItem + cnt - 1;
10042 else
10043 nItem = topidx + cnt - 1;
10045 else
10046 nItem = infoPtr->nFocusedItem + LISTVIEW_GetCountPerColumn(infoPtr)
10047 * LISTVIEW_GetCountPerRow(infoPtr);
10048 if(nItem >= infoPtr->nItemCount) nItem = infoPtr->nItemCount - 1;
10049 break;
10052 if ((nItem != -1) && (nItem != infoPtr->nFocusedItem || nVirtualKey == VK_SPACE))
10053 LISTVIEW_KeySelection(infoPtr, nItem, nVirtualKey == VK_SPACE);
10055 return 0;
10058 /***
10059 * DESCRIPTION:
10060 * Kills the focus.
10062 * PARAMETER(S):
10063 * [I] infoPtr : valid pointer to the listview structure
10065 * RETURN:
10066 * Zero
10068 static LRESULT LISTVIEW_KillFocus(LISTVIEW_INFO *infoPtr)
10070 TRACE("()\n");
10072 /* drop any left over scroll amount */
10073 infoPtr->cWheelRemainder = 0;
10075 /* if we did not have the focus, there's nothing more to do */
10076 if (!infoPtr->bFocus) return 0;
10078 /* send NM_KILLFOCUS notification */
10079 if (!notify(infoPtr, NM_KILLFOCUS)) return 0;
10081 /* if we have a focus rectangle, get rid of it */
10082 LISTVIEW_ShowFocusRect(infoPtr, FALSE);
10084 /* if have a marquee selection, stop it */
10085 if (infoPtr->bMarqueeSelect)
10087 /* Remove the marquee rectangle and release our mouse capture */
10088 LISTVIEW_InvalidateRect(infoPtr, &infoPtr->marqueeRect);
10089 ReleaseCapture();
10091 SetRectEmpty(&infoPtr->marqueeRect);
10093 infoPtr->bMarqueeSelect = FALSE;
10094 infoPtr->bScrolling = FALSE;
10095 KillTimer(infoPtr->hwndSelf, (UINT_PTR) infoPtr);
10098 /* set window focus flag */
10099 infoPtr->bFocus = FALSE;
10101 /* invalidate the selected items before resetting focus flag */
10102 LISTVIEW_InvalidateSelectedItems(infoPtr);
10104 return 0;
10107 /***
10108 * DESCRIPTION:
10109 * Processes double click messages (left mouse button).
10111 * PARAMETER(S):
10112 * [I] infoPtr : valid pointer to the listview structure
10113 * [I] wKey : key flag
10114 * [I] x,y : mouse coordinate
10116 * RETURN:
10117 * Zero
10119 static LRESULT LISTVIEW_LButtonDblClk(LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
10121 LVHITTESTINFO htInfo;
10123 TRACE("(key=%hu, X=%u, Y=%u)\n", wKey, x, y);
10125 /* Cancel the item edition if any */
10126 if (infoPtr->itemEdit.fEnabled)
10128 KillTimer(infoPtr->hwndSelf, (UINT_PTR)&infoPtr->itemEdit);
10129 infoPtr->itemEdit.fEnabled = FALSE;
10132 /* send NM_RELEASEDCAPTURE notification */
10133 if (!notify(infoPtr, NM_RELEASEDCAPTURE)) return 0;
10135 htInfo.pt.x = x;
10136 htInfo.pt.y = y;
10138 /* send NM_DBLCLK notification */
10139 LISTVIEW_HitTest(infoPtr, &htInfo, TRUE, FALSE);
10140 if (!notify_click(infoPtr, NM_DBLCLK, &htInfo)) return 0;
10142 /* To send the LVN_ITEMACTIVATE, it must be on an Item */
10143 if(htInfo.iItem != -1) notify_itemactivate(infoPtr,&htInfo);
10145 return 0;
10148 static LRESULT LISTVIEW_TrackMouse(const LISTVIEW_INFO *infoPtr, POINT pt)
10150 MSG msg;
10151 RECT r;
10153 r.top = r.bottom = pt.y;
10154 r.left = r.right = pt.x;
10156 InflateRect(&r, GetSystemMetrics(SM_CXDRAG), GetSystemMetrics(SM_CYDRAG));
10158 SetCapture(infoPtr->hwndSelf);
10160 while (1)
10162 if (PeekMessageW(&msg, 0, 0, 0, PM_REMOVE | PM_NOYIELD))
10164 if (msg.message == WM_MOUSEMOVE)
10166 pt.x = (short)LOWORD(msg.lParam);
10167 pt.y = (short)HIWORD(msg.lParam);
10168 if (PtInRect(&r, pt))
10169 continue;
10170 else
10172 ReleaseCapture();
10173 return 1;
10176 else if (msg.message >= WM_LBUTTONDOWN &&
10177 msg.message <= WM_RBUTTONDBLCLK)
10179 break;
10182 DispatchMessageW(&msg);
10185 if (GetCapture() != infoPtr->hwndSelf)
10186 return 0;
10189 ReleaseCapture();
10190 return 0;
10194 /***
10195 * DESCRIPTION:
10196 * Processes mouse down messages (left mouse button).
10198 * PARAMETERS:
10199 * infoPtr [I ] valid pointer to the listview structure
10200 * wKey [I ] key flag
10201 * x,y [I ] mouse coordinate
10203 * RETURN:
10204 * Zero
10206 static LRESULT LISTVIEW_LButtonDown(LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
10208 LVHITTESTINFO lvHitTestInfo;
10209 static BOOL bGroupSelect = TRUE;
10210 POINT pt = { x, y };
10211 INT nItem;
10213 TRACE("(key=%hu, X=%u, Y=%u)\n", wKey, x, y);
10215 /* send NM_RELEASEDCAPTURE notification */
10216 if (!notify(infoPtr, NM_RELEASEDCAPTURE)) return 0;
10218 /* set left button down flag and record the click position */
10219 infoPtr->bLButtonDown = TRUE;
10220 infoPtr->ptClickPos = pt;
10221 infoPtr->bDragging = FALSE;
10222 infoPtr->bMarqueeSelect = FALSE;
10223 infoPtr->bScrolling = FALSE;
10225 lvHitTestInfo.pt.x = x;
10226 lvHitTestInfo.pt.y = y;
10228 nItem = LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, TRUE);
10229 TRACE("at %s, nItem=%d\n", wine_dbgstr_point(&pt), nItem);
10230 if ((nItem >= 0) && (nItem < infoPtr->nItemCount))
10232 if ((infoPtr->dwLvExStyle & LVS_EX_CHECKBOXES) && (lvHitTestInfo.flags & LVHT_ONITEMSTATEICON))
10234 toggle_checkbox_state(infoPtr, nItem);
10235 return 0;
10238 if (infoPtr->dwStyle & LVS_SINGLESEL)
10240 if (LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED))
10241 infoPtr->nEditLabelItem = nItem;
10242 else
10243 LISTVIEW_SetSelection(infoPtr, nItem);
10245 else
10247 if ((wKey & MK_CONTROL) && (wKey & MK_SHIFT))
10249 if (bGroupSelect)
10251 if (!LISTVIEW_AddGroupSelection(infoPtr, nItem)) return 0;
10252 LISTVIEW_SetItemFocus(infoPtr, nItem);
10253 infoPtr->nSelectionMark = nItem;
10255 else
10257 LVITEMW item;
10259 item.state = LVIS_SELECTED | LVIS_FOCUSED;
10260 item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
10262 LISTVIEW_SetItemState(infoPtr,nItem,&item);
10263 infoPtr->nSelectionMark = nItem;
10266 else if (wKey & MK_CONTROL)
10268 LVITEMW item;
10270 bGroupSelect = (LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED) == 0);
10272 item.state = (bGroupSelect ? LVIS_SELECTED : 0) | LVIS_FOCUSED;
10273 item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
10274 LISTVIEW_SetItemState(infoPtr, nItem, &item);
10275 infoPtr->nSelectionMark = nItem;
10277 else if (wKey & MK_SHIFT)
10279 LISTVIEW_SetGroupSelection(infoPtr, nItem);
10281 else
10283 if (LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED))
10285 infoPtr->nEditLabelItem = nItem;
10286 infoPtr->nLButtonDownItem = nItem;
10288 LISTVIEW_SetItemFocus(infoPtr, nItem);
10290 else
10291 /* set selection (clears other pre-existing selections) */
10292 LISTVIEW_SetSelection(infoPtr, nItem);
10296 if (!infoPtr->bFocus)
10297 SetFocus(infoPtr->hwndSelf);
10299 if (infoPtr->dwLvExStyle & LVS_EX_ONECLICKACTIVATE)
10300 if(lvHitTestInfo.iItem != -1) notify_itemactivate(infoPtr,&lvHitTestInfo);
10302 else
10304 if (!infoPtr->bFocus)
10305 SetFocus(infoPtr->hwndSelf);
10307 /* remove all selections */
10308 if (!(wKey & MK_CONTROL) && !(wKey & MK_SHIFT))
10309 LISTVIEW_DeselectAll(infoPtr);
10310 ReleaseCapture();
10313 return 0;
10316 /***
10317 * DESCRIPTION:
10318 * Processes mouse up messages (left mouse button).
10320 * PARAMETERS:
10321 * infoPtr [I ] valid pointer to the listview structure
10322 * wKey [I ] key flag
10323 * x,y [I ] mouse coordinate
10325 * RETURN:
10326 * Zero
10328 static LRESULT LISTVIEW_LButtonUp(LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
10330 LVHITTESTINFO lvHitTestInfo;
10332 TRACE("(key=%hu, X=%u, Y=%u)\n", wKey, x, y);
10334 if (!infoPtr->bLButtonDown) return 0;
10336 lvHitTestInfo.pt.x = x;
10337 lvHitTestInfo.pt.y = y;
10339 /* send NM_CLICK notification */
10340 LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, FALSE);
10341 if (!notify_click(infoPtr, NM_CLICK, &lvHitTestInfo)) return 0;
10343 /* set left button flag */
10344 infoPtr->bLButtonDown = FALSE;
10346 /* set a single selection, reset others */
10347 if(lvHitTestInfo.iItem == infoPtr->nLButtonDownItem && lvHitTestInfo.iItem != -1)
10348 LISTVIEW_SetSelection(infoPtr, infoPtr->nLButtonDownItem);
10349 infoPtr->nLButtonDownItem = -1;
10351 if (infoPtr->bDragging || infoPtr->bMarqueeSelect)
10353 /* Remove the marquee rectangle and release our mouse capture */
10354 if (infoPtr->bMarqueeSelect)
10356 LISTVIEW_InvalidateRect(infoPtr, &infoPtr->marqueeDrawRect);
10357 ReleaseCapture();
10360 SetRectEmpty(&infoPtr->marqueeRect);
10361 SetRectEmpty(&infoPtr->marqueeDrawRect);
10363 infoPtr->bDragging = FALSE;
10364 infoPtr->bMarqueeSelect = FALSE;
10365 infoPtr->bScrolling = FALSE;
10367 KillTimer(infoPtr->hwndSelf, (UINT_PTR) infoPtr);
10368 return 0;
10371 /* if we clicked on a selected item, edit the label */
10372 if(lvHitTestInfo.iItem == infoPtr->nEditLabelItem && (lvHitTestInfo.flags & LVHT_ONITEMLABEL))
10374 /* we want to make sure the user doesn't want to do a double click. So we will
10375 * delay the edit. WM_LBUTTONDBLCLICK will cancel the timer
10377 infoPtr->itemEdit.fEnabled = TRUE;
10378 infoPtr->itemEdit.iItem = lvHitTestInfo.iItem;
10379 SetTimer(infoPtr->hwndSelf,
10380 (UINT_PTR)&infoPtr->itemEdit,
10381 GetDoubleClickTime(),
10382 LISTVIEW_DelayedEditItem);
10385 return 0;
10388 /***
10389 * DESCRIPTION:
10390 * Destroys the listview control (called after WM_DESTROY).
10392 * PARAMETER(S):
10393 * [I] infoPtr : valid pointer to the listview structure
10395 * RETURN:
10396 * Zero
10398 static LRESULT LISTVIEW_NCDestroy(LISTVIEW_INFO *infoPtr)
10400 INT i;
10402 TRACE("()\n");
10404 /* destroy data structure */
10405 DPA_Destroy(infoPtr->hdpaItems);
10406 DPA_Destroy(infoPtr->hdpaItemIds);
10407 DPA_Destroy(infoPtr->hdpaPosX);
10408 DPA_Destroy(infoPtr->hdpaPosY);
10409 /* columns */
10410 for (i = 0; i < DPA_GetPtrCount(infoPtr->hdpaColumns); i++)
10411 Free(DPA_GetPtr(infoPtr->hdpaColumns, i));
10412 DPA_Destroy(infoPtr->hdpaColumns);
10413 ranges_destroy(infoPtr->selectionRanges);
10415 /* destroy image lists */
10416 if (!(infoPtr->dwStyle & LVS_SHAREIMAGELISTS))
10418 ImageList_Destroy(infoPtr->himlNormal);
10419 ImageList_Destroy(infoPtr->himlSmall);
10420 ImageList_Destroy(infoPtr->himlState);
10423 /* destroy font, bkgnd brush */
10424 infoPtr->hFont = 0;
10425 if (infoPtr->hDefaultFont) DeleteObject(infoPtr->hDefaultFont);
10426 if (infoPtr->clrBk != CLR_NONE) DeleteObject(infoPtr->hBkBrush);
10428 SetWindowLongPtrW(infoPtr->hwndSelf, 0, 0);
10430 /* free listview info pointer*/
10431 Free(infoPtr);
10433 return 0;
10436 /***
10437 * DESCRIPTION:
10438 * Handles notifications.
10440 * PARAMETER(S):
10441 * [I] infoPtr : valid pointer to the listview structure
10442 * [I] lpnmhdr : notification information
10444 * RETURN:
10445 * Zero
10447 static LRESULT LISTVIEW_Notify(LISTVIEW_INFO *infoPtr, NMHDR *lpnmhdr)
10449 NMHEADERW *lpnmh;
10451 TRACE("(lpnmhdr=%p)\n", lpnmhdr);
10453 if (!lpnmhdr || lpnmhdr->hwndFrom != infoPtr->hwndHeader) return 0;
10455 /* remember: HDN_LAST < HDN_FIRST */
10456 if (lpnmhdr->code > HDN_FIRST || lpnmhdr->code < HDN_LAST) return 0;
10457 lpnmh = (NMHEADERW *)lpnmhdr;
10459 if (lpnmh->iItem < 0 || lpnmh->iItem >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return 0;
10461 switch (lpnmhdr->code)
10463 case HDN_TRACKW:
10464 case HDN_TRACKA:
10466 COLUMN_INFO *lpColumnInfo;
10467 POINT ptOrigin;
10468 INT x;
10470 if (!lpnmh->pitem || !(lpnmh->pitem->mask & HDI_WIDTH))
10471 break;
10473 /* remove the old line (if any) */
10474 LISTVIEW_DrawTrackLine(infoPtr);
10476 /* compute & draw the new line */
10477 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, lpnmh->iItem);
10478 x = lpColumnInfo->rcHeader.left + lpnmh->pitem->cxy;
10479 LISTVIEW_GetOrigin(infoPtr, &ptOrigin);
10480 infoPtr->xTrackLine = x + ptOrigin.x;
10481 LISTVIEW_DrawTrackLine(infoPtr);
10482 return notify_forward_header(infoPtr, lpnmh);
10485 case HDN_ENDTRACKA:
10486 case HDN_ENDTRACKW:
10487 /* remove the track line (if any) */
10488 LISTVIEW_DrawTrackLine(infoPtr);
10489 infoPtr->xTrackLine = -1;
10490 return notify_forward_header(infoPtr, lpnmh);
10492 case HDN_BEGINDRAG:
10493 if ((infoPtr->dwLvExStyle & LVS_EX_HEADERDRAGDROP) == 0) return 1;
10494 return notify_forward_header(infoPtr, lpnmh);
10496 case HDN_ENDDRAG:
10497 infoPtr->colRectsDirty = TRUE;
10498 LISTVIEW_InvalidateList(infoPtr);
10499 return notify_forward_header(infoPtr, lpnmh);
10501 case HDN_ITEMCHANGEDW:
10502 case HDN_ITEMCHANGEDA:
10504 COLUMN_INFO *lpColumnInfo;
10505 HDITEMW hdi;
10506 INT dx, cxy;
10508 if (!lpnmh->pitem || !(lpnmh->pitem->mask & HDI_WIDTH))
10510 hdi.mask = HDI_WIDTH;
10511 if (!SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, lpnmh->iItem, (LPARAM)&hdi)) return 0;
10512 cxy = hdi.cxy;
10514 else
10515 cxy = lpnmh->pitem->cxy;
10517 /* determine how much we change since the last know position */
10518 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, lpnmh->iItem);
10519 dx = cxy - (lpColumnInfo->rcHeader.right - lpColumnInfo->rcHeader.left);
10520 if (dx != 0)
10522 lpColumnInfo->rcHeader.right += dx;
10524 hdi.mask = HDI_ORDER;
10525 SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, lpnmh->iItem, (LPARAM)&hdi);
10527 /* not the rightmost one */
10528 if (hdi.iOrder + 1 < DPA_GetPtrCount(infoPtr->hdpaColumns))
10530 INT nIndex = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX,
10531 hdi.iOrder + 1, 0);
10532 LISTVIEW_ScrollColumns(infoPtr, nIndex, dx);
10534 else
10536 /* only needs to update the scrolls */
10537 infoPtr->nItemWidth += dx;
10538 LISTVIEW_UpdateScroll(infoPtr);
10540 LISTVIEW_UpdateItemSize(infoPtr);
10541 if (infoPtr->uView == LV_VIEW_DETAILS && is_redrawing(infoPtr))
10543 POINT ptOrigin;
10544 RECT rcCol = lpColumnInfo->rcHeader;
10546 LISTVIEW_GetOrigin(infoPtr, &ptOrigin);
10547 OffsetRect(&rcCol, ptOrigin.x, 0);
10549 rcCol.top = infoPtr->rcList.top;
10550 rcCol.bottom = infoPtr->rcList.bottom;
10552 /* resizing left-aligned columns leaves most of the left side untouched */
10553 if ((lpColumnInfo->fmt & LVCFMT_JUSTIFYMASK) == LVCFMT_LEFT)
10555 INT nMaxDirty = infoPtr->nEllipsisWidth + infoPtr->ntmMaxCharWidth;
10556 if (dx > 0)
10557 nMaxDirty += dx;
10558 rcCol.left = max (rcCol.left, rcCol.right - nMaxDirty);
10561 /* when shrinking the last column clear the now unused field */
10562 if (hdi.iOrder == DPA_GetPtrCount(infoPtr->hdpaColumns) - 1)
10564 RECT right;
10566 rcCol.right -= dx;
10568 /* deal with right from rightmost column area */
10569 right.left = rcCol.right;
10570 right.top = rcCol.top;
10571 right.bottom = rcCol.bottom;
10572 right.right = infoPtr->rcList.right;
10574 LISTVIEW_InvalidateRect(infoPtr, &right);
10577 LISTVIEW_InvalidateRect(infoPtr, &rcCol);
10580 break;
10583 case HDN_ITEMCLICKW:
10584 case HDN_ITEMCLICKA:
10586 /* Handle sorting by Header Column */
10587 NMLISTVIEW nmlv;
10589 ZeroMemory(&nmlv, sizeof(NMLISTVIEW));
10590 nmlv.iItem = -1;
10591 nmlv.iSubItem = lpnmh->iItem;
10592 notify_listview(infoPtr, LVN_COLUMNCLICK, &nmlv);
10593 return notify_forward_header(infoPtr, lpnmh);
10596 case HDN_DIVIDERDBLCLICKW:
10597 case HDN_DIVIDERDBLCLICKA:
10598 /* FIXME: for LVS_EX_HEADERINALLVIEWS and not LV_VIEW_DETAILS
10599 we should use LVSCW_AUTOSIZE_USEHEADER, helper rework or
10600 split needed for that */
10601 LISTVIEW_SetColumnWidth(infoPtr, lpnmh->iItem, LVSCW_AUTOSIZE);
10602 return notify_forward_header(infoPtr, lpnmh);
10604 return 0;
10607 /***
10608 * DESCRIPTION:
10609 * Paint non-client area of control.
10611 * PARAMETER(S):
10612 * [I] infoPtr : valid pointer to the listview structureof the sender
10613 * [I] region : update region
10615 * RETURN:
10616 * TRUE - frame was painted
10617 * FALSE - call default window proc
10619 static BOOL LISTVIEW_NCPaint(const LISTVIEW_INFO *infoPtr, HRGN region)
10621 HTHEME theme = GetWindowTheme (infoPtr->hwndSelf);
10622 HDC dc;
10623 RECT r;
10624 HRGN cliprgn;
10625 int cxEdge = GetSystemMetrics (SM_CXEDGE),
10626 cyEdge = GetSystemMetrics (SM_CYEDGE);
10628 if (!theme)
10629 return DefWindowProcW (infoPtr->hwndSelf, WM_NCPAINT, (WPARAM)region, 0);
10631 GetWindowRect(infoPtr->hwndSelf, &r);
10633 cliprgn = CreateRectRgn (r.left + cxEdge, r.top + cyEdge,
10634 r.right - cxEdge, r.bottom - cyEdge);
10635 if (region != (HRGN)1)
10636 CombineRgn (cliprgn, cliprgn, region, RGN_AND);
10637 OffsetRect(&r, -r.left, -r.top);
10639 dc = GetDCEx(infoPtr->hwndSelf, region, DCX_WINDOW|DCX_INTERSECTRGN);
10640 OffsetRect(&r, -r.left, -r.top);
10642 if (IsThemeBackgroundPartiallyTransparent (theme, 0, 0))
10643 DrawThemeParentBackground(infoPtr->hwndSelf, dc, &r);
10644 DrawThemeBackground (theme, dc, 0, 0, &r, 0);
10645 ReleaseDC(infoPtr->hwndSelf, dc);
10647 /* Call default proc to get the scrollbars etc. painted */
10648 DefWindowProcW (infoPtr->hwndSelf, WM_NCPAINT, (WPARAM)cliprgn, 0);
10650 return FALSE;
10653 /***
10654 * DESCRIPTION:
10655 * Determines the type of structure to use.
10657 * PARAMETER(S):
10658 * [I] infoPtr : valid pointer to the listview structureof the sender
10659 * [I] hwndFrom : listview window handle
10660 * [I] nCommand : command specifying the nature of the WM_NOTIFYFORMAT
10662 * RETURN:
10663 * Zero
10665 static LRESULT LISTVIEW_NotifyFormat(LISTVIEW_INFO *infoPtr, HWND hwndFrom, INT nCommand)
10667 TRACE("(hwndFrom=%p, nCommand=%d)\n", hwndFrom, nCommand);
10669 if (nCommand == NF_REQUERY)
10670 infoPtr->notifyFormat = SendMessageW(infoPtr->hwndNotify, WM_NOTIFYFORMAT, (WPARAM)infoPtr->hwndSelf, NF_QUERY);
10672 return infoPtr->notifyFormat;
10675 /***
10676 * DESCRIPTION:
10677 * Paints/Repaints the listview control. Internal use.
10679 * PARAMETER(S):
10680 * [I] infoPtr : valid pointer to the listview structure
10681 * [I] hdc : device context handle
10683 * RETURN:
10684 * Zero
10686 static LRESULT LISTVIEW_Paint(LISTVIEW_INFO *infoPtr, HDC hdc)
10688 TRACE("(hdc=%p)\n", hdc);
10690 if (infoPtr->bNoItemMetrics && infoPtr->nItemCount)
10692 infoPtr->bNoItemMetrics = FALSE;
10693 LISTVIEW_UpdateItemSize(infoPtr);
10694 if (infoPtr->uView == LV_VIEW_ICON || infoPtr->uView == LV_VIEW_SMALLICON)
10695 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
10696 LISTVIEW_UpdateScroll(infoPtr);
10699 if (infoPtr->hwndHeader) UpdateWindow(infoPtr->hwndHeader);
10701 if (hdc)
10702 LISTVIEW_Refresh(infoPtr, hdc, NULL);
10703 else
10705 PAINTSTRUCT ps;
10707 hdc = BeginPaint(infoPtr->hwndSelf, &ps);
10708 if (!hdc) return 1;
10709 LISTVIEW_Refresh(infoPtr, hdc, ps.fErase ? &ps.rcPaint : NULL);
10710 EndPaint(infoPtr->hwndSelf, &ps);
10713 return 0;
10716 /***
10717 * DESCRIPTION:
10718 * Paints/Repaints the listview control, WM_PAINT handler.
10720 * PARAMETER(S):
10721 * [I] infoPtr : valid pointer to the listview structure
10722 * [I] hdc : device context handle
10724 * RETURN:
10725 * Zero
10727 static inline LRESULT LISTVIEW_WMPaint(LISTVIEW_INFO *infoPtr, HDC hdc)
10729 TRACE("(hdc=%p)\n", hdc);
10731 if (!is_redrawing(infoPtr))
10732 return DefWindowProcW (infoPtr->hwndSelf, WM_PAINT, (WPARAM)hdc, 0);
10734 return LISTVIEW_Paint(infoPtr, hdc);
10737 /***
10738 * DESCRIPTION:
10739 * Paints/Repaints the listview control.
10741 * PARAMETER(S):
10742 * [I] infoPtr : valid pointer to the listview structure
10743 * [I] hdc : device context handle
10744 * [I] options : drawing options
10746 * RETURN:
10747 * Zero
10749 static LRESULT LISTVIEW_PrintClient(LISTVIEW_INFO *infoPtr, HDC hdc, DWORD options)
10751 if ((options & PRF_CHECKVISIBLE) && !IsWindowVisible(infoPtr->hwndSelf))
10752 return 0;
10754 if (options & ~(PRF_ERASEBKGND|PRF_CLIENT))
10755 FIXME("(hdc=%p options=0x%08x) partial stub\n", hdc, options);
10757 if (options & PRF_ERASEBKGND)
10758 LISTVIEW_EraseBkgnd(infoPtr, hdc);
10760 if (options & PRF_CLIENT)
10761 LISTVIEW_Paint(infoPtr, hdc);
10763 return 0;
10767 /***
10768 * DESCRIPTION:
10769 * Processes double click messages (right mouse button).
10771 * PARAMETER(S):
10772 * [I] infoPtr : valid pointer to the listview structure
10773 * [I] wKey : key flag
10774 * [I] x,y : mouse coordinate
10776 * RETURN:
10777 * Zero
10779 static LRESULT LISTVIEW_RButtonDblClk(const LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
10781 LVHITTESTINFO lvHitTestInfo;
10783 TRACE("(key=%hu,X=%u,Y=%u)\n", wKey, x, y);
10785 /* send NM_RELEASEDCAPTURE notification */
10786 if (!notify(infoPtr, NM_RELEASEDCAPTURE)) return 0;
10788 /* send NM_RDBLCLK notification */
10789 lvHitTestInfo.pt.x = x;
10790 lvHitTestInfo.pt.y = y;
10791 LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, FALSE);
10792 notify_click(infoPtr, NM_RDBLCLK, &lvHitTestInfo);
10794 return 0;
10797 /***
10798 * DESCRIPTION:
10799 * Processes WM_RBUTTONDOWN message and corresponding drag operation.
10801 * PARAMETER(S):
10802 * [I] infoPtr : valid pointer to the listview structure
10803 * [I] wKey : key flag
10804 * [I] x, y : mouse coordinate
10806 * RETURN:
10807 * Zero
10809 static LRESULT LISTVIEW_RButtonDown(LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
10811 LVHITTESTINFO ht;
10812 INT item;
10814 TRACE("(key=%hu, x=%d, y=%d)\n", wKey, x, y);
10816 /* send NM_RELEASEDCAPTURE notification */
10817 if (!notify(infoPtr, NM_RELEASEDCAPTURE)) return 0;
10819 /* determine the index of the selected item */
10820 ht.pt.x = x;
10821 ht.pt.y = y;
10822 item = LISTVIEW_HitTest(infoPtr, &ht, TRUE, TRUE);
10824 /* make sure the listview control window has the focus */
10825 if (!infoPtr->bFocus) SetFocus(infoPtr->hwndSelf);
10827 if ((item >= 0) && (item < infoPtr->nItemCount))
10829 LISTVIEW_SetItemFocus(infoPtr, item);
10830 if (!((wKey & MK_SHIFT) || (wKey & MK_CONTROL)) &&
10831 !LISTVIEW_GetItemState(infoPtr, item, LVIS_SELECTED))
10832 LISTVIEW_SetSelection(infoPtr, item);
10834 else
10835 LISTVIEW_DeselectAll(infoPtr);
10837 if (LISTVIEW_TrackMouse(infoPtr, ht.pt))
10839 if (ht.iItem != -1)
10841 NMLISTVIEW nmlv;
10843 memset(&nmlv, 0, sizeof(nmlv));
10844 nmlv.iItem = ht.iItem;
10845 nmlv.ptAction = ht.pt;
10847 notify_listview(infoPtr, LVN_BEGINRDRAG, &nmlv);
10850 else
10852 SetFocus(infoPtr->hwndSelf);
10854 ht.pt.x = x;
10855 ht.pt.y = y;
10856 LISTVIEW_HitTest(infoPtr, &ht, TRUE, FALSE);
10858 if (notify_click(infoPtr, NM_RCLICK, &ht))
10860 /* Send a WM_CONTEXTMENU message in response to the WM_RBUTTONUP */
10861 SendMessageW(infoPtr->hwndSelf, WM_CONTEXTMENU,
10862 (WPARAM)infoPtr->hwndSelf, (LPARAM)GetMessagePos());
10866 return 0;
10869 /***
10870 * DESCRIPTION:
10871 * Sets the cursor.
10873 * PARAMETER(S):
10874 * [I] infoPtr : valid pointer to the listview structure
10875 * [I] hwnd : window handle of window containing the cursor
10876 * [I] nHittest : hit-test code
10877 * [I] wMouseMsg : ideintifier of the mouse message
10879 * RETURN:
10880 * TRUE if cursor is set
10881 * FALSE otherwise
10883 static BOOL LISTVIEW_SetCursor(const LISTVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
10885 LVHITTESTINFO lvHitTestInfo;
10887 if (!LISTVIEW_IsHotTracking(infoPtr)) goto forward;
10889 if (!infoPtr->hHotCursor) goto forward;
10891 GetCursorPos(&lvHitTestInfo.pt);
10892 if (LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, FALSE, FALSE) < 0) goto forward;
10894 SetCursor(infoPtr->hHotCursor);
10896 return TRUE;
10898 forward:
10900 return DefWindowProcW(infoPtr->hwndSelf, WM_SETCURSOR, wParam, lParam);
10903 /***
10904 * DESCRIPTION:
10905 * Sets the focus.
10907 * PARAMETER(S):
10908 * [I] infoPtr : valid pointer to the listview structure
10909 * [I] hwndLoseFocus : handle of previously focused window
10911 * RETURN:
10912 * Zero
10914 static LRESULT LISTVIEW_SetFocus(LISTVIEW_INFO *infoPtr, HWND hwndLoseFocus)
10916 TRACE("(hwndLoseFocus=%p)\n", hwndLoseFocus);
10918 /* if we have the focus already, there's nothing to do */
10919 if (infoPtr->bFocus) return 0;
10921 /* send NM_SETFOCUS notification */
10922 if (!notify(infoPtr, NM_SETFOCUS)) return 0;
10924 /* set window focus flag */
10925 infoPtr->bFocus = TRUE;
10927 /* put the focus rect back on */
10928 LISTVIEW_ShowFocusRect(infoPtr, TRUE);
10930 /* redraw all visible selected items */
10931 LISTVIEW_InvalidateSelectedItems(infoPtr);
10933 return 0;
10936 /***
10937 * DESCRIPTION:
10938 * Sets the font.
10940 * PARAMETER(S):
10941 * [I] infoPtr : valid pointer to the listview structure
10942 * [I] fRedraw : font handle
10943 * [I] fRedraw : redraw flag
10945 * RETURN:
10946 * Zero
10948 static LRESULT LISTVIEW_SetFont(LISTVIEW_INFO *infoPtr, HFONT hFont, WORD fRedraw)
10950 HFONT oldFont = infoPtr->hFont;
10951 INT oldHeight = infoPtr->nItemHeight;
10953 TRACE("(hfont=%p,redraw=%hu)\n", hFont, fRedraw);
10955 infoPtr->hFont = hFont ? hFont : infoPtr->hDefaultFont;
10956 if (infoPtr->hFont == oldFont) return 0;
10958 LISTVIEW_SaveTextMetrics(infoPtr);
10960 infoPtr->nItemHeight = LISTVIEW_CalculateItemHeight(infoPtr);
10962 if (infoPtr->uView == LV_VIEW_DETAILS)
10964 SendMessageW(infoPtr->hwndHeader, WM_SETFONT, (WPARAM)hFont, MAKELPARAM(fRedraw, 0));
10965 LISTVIEW_UpdateSize(infoPtr);
10966 LISTVIEW_UpdateScroll(infoPtr);
10968 else if (infoPtr->nItemHeight != oldHeight)
10969 LISTVIEW_UpdateScroll(infoPtr);
10971 if (fRedraw) LISTVIEW_InvalidateList(infoPtr);
10973 return 0;
10976 /***
10977 * DESCRIPTION:
10978 * Message handling for WM_SETREDRAW.
10979 * For the Listview, it invalidates the entire window (the doc specifies otherwise)
10981 * PARAMETER(S):
10982 * [I] infoPtr : valid pointer to the listview structure
10983 * [I] redraw: state of redraw flag
10985 * RETURN:
10986 * Zero.
10988 static LRESULT LISTVIEW_SetRedraw(LISTVIEW_INFO *infoPtr, BOOL redraw)
10990 TRACE("old=%d, new=%d\n", infoPtr->redraw, redraw);
10992 if (infoPtr->redraw == !!redraw)
10993 return 0;
10995 if (!(infoPtr->redraw = !!redraw))
10996 return 0;
10998 if (is_autoarrange(infoPtr))
10999 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
11000 LISTVIEW_UpdateScroll(infoPtr);
11002 /* despite what the WM_SETREDRAW docs says, apps expect us
11003 * to invalidate the listview here... stupid! */
11004 LISTVIEW_InvalidateList(infoPtr);
11006 return 0;
11009 /***
11010 * DESCRIPTION:
11011 * Resizes the listview control. This function processes WM_SIZE
11012 * messages. At this time, the width and height are not used.
11014 * PARAMETER(S):
11015 * [I] infoPtr : valid pointer to the listview structure
11016 * [I] Width : new width
11017 * [I] Height : new height
11019 * RETURN:
11020 * Zero
11022 static LRESULT LISTVIEW_Size(LISTVIEW_INFO *infoPtr, int Width, int Height)
11024 RECT rcOld = infoPtr->rcList;
11026 TRACE("(width=%d, height=%d)\n", Width, Height);
11028 LISTVIEW_UpdateSize(infoPtr);
11029 if (EqualRect(&rcOld, &infoPtr->rcList)) return 0;
11031 /* do not bother with display related stuff if we're not redrawing */
11032 if (!is_redrawing(infoPtr)) return 0;
11034 if (is_autoarrange(infoPtr))
11035 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
11037 LISTVIEW_UpdateScroll(infoPtr);
11039 /* refresh all only for lists whose height changed significantly */
11040 if ((infoPtr->uView == LV_VIEW_LIST) &&
11041 (rcOld.bottom - rcOld.top) / infoPtr->nItemHeight !=
11042 (infoPtr->rcList.bottom - infoPtr->rcList.top) / infoPtr->nItemHeight)
11043 LISTVIEW_InvalidateList(infoPtr);
11045 return 0;
11048 /***
11049 * DESCRIPTION:
11050 * Sets the size information.
11052 * PARAMETER(S):
11053 * [I] infoPtr : valid pointer to the listview structure
11055 * RETURN:
11056 * None
11058 static void LISTVIEW_UpdateSize(LISTVIEW_INFO *infoPtr)
11060 TRACE("uView=%d, rcList(old)=%s\n", infoPtr->uView, wine_dbgstr_rect(&infoPtr->rcList));
11062 GetClientRect(infoPtr->hwndSelf, &infoPtr->rcList);
11064 if (infoPtr->uView == LV_VIEW_LIST)
11066 /* Apparently the "LIST" style is supposed to have the same
11067 * number of items in a column even if there is no scroll bar.
11068 * Since if a scroll bar already exists then the bottom is already
11069 * reduced, only reduce if the scroll bar does not currently exist.
11070 * The "2" is there to mimic the native control. I think it may be
11071 * related to either padding or edges. (GLA 7/2002)
11073 if (!(GetWindowLongW(infoPtr->hwndSelf, GWL_STYLE) & WS_HSCROLL))
11074 infoPtr->rcList.bottom -= GetSystemMetrics(SM_CYHSCROLL);
11075 infoPtr->rcList.bottom = max (infoPtr->rcList.bottom - 2, 0);
11078 /* if control created invisible header isn't created */
11079 if (infoPtr->hwndHeader)
11081 HDLAYOUT hl;
11082 WINDOWPOS wp;
11084 hl.prc = &infoPtr->rcList;
11085 hl.pwpos = &wp;
11086 SendMessageW( infoPtr->hwndHeader, HDM_LAYOUT, 0, (LPARAM)&hl );
11087 TRACE(" wp.flags=0x%08x, wp=%d,%d (%dx%d)\n", wp.flags, wp.x, wp.y, wp.cx, wp.cy);
11089 if (LISTVIEW_IsHeaderEnabled(infoPtr))
11090 wp.flags |= SWP_SHOWWINDOW;
11091 else
11093 wp.flags |= SWP_HIDEWINDOW;
11094 wp.cy = 0;
11097 SetWindowPos(wp.hwnd, wp.hwndInsertAfter, wp.x, wp.y, wp.cx, wp.cy, wp.flags);
11098 TRACE(" after SWP wp=%d,%d (%dx%d)\n", wp.x, wp.y, wp.cx, wp.cy);
11100 infoPtr->rcList.top = max(wp.cy, 0);
11102 /* extra padding for grid */
11103 if (infoPtr->uView == LV_VIEW_DETAILS && infoPtr->dwLvExStyle & LVS_EX_GRIDLINES)
11104 infoPtr->rcList.top += 2;
11106 TRACE(" rcList=%s\n", wine_dbgstr_rect(&infoPtr->rcList));
11109 /***
11110 * DESCRIPTION:
11111 * Processes WM_STYLECHANGED messages.
11113 * PARAMETER(S):
11114 * [I] infoPtr : valid pointer to the listview structure
11115 * [I] wStyleType : window style type (normal or extended)
11116 * [I] lpss : window style information
11118 * RETURN:
11119 * Zero
11121 static INT LISTVIEW_StyleChanged(LISTVIEW_INFO *infoPtr, WPARAM wStyleType,
11122 const STYLESTRUCT *lpss)
11124 UINT uNewView, uOldView;
11125 UINT style;
11127 TRACE("(styletype=%lx, styleOld=0x%08x, styleNew=0x%08x)\n",
11128 wStyleType, lpss->styleOld, lpss->styleNew);
11130 if (wStyleType != GWL_STYLE || lpss->styleNew == infoPtr->dwStyle) return 0;
11132 infoPtr->dwStyle = lpss->styleNew;
11134 if (((lpss->styleOld & WS_HSCROLL) != 0)&&
11135 ((lpss->styleNew & WS_HSCROLL) == 0))
11136 ShowScrollBar(infoPtr->hwndSelf, SB_HORZ, FALSE);
11138 if (((lpss->styleOld & WS_VSCROLL) != 0)&&
11139 ((lpss->styleNew & WS_VSCROLL) == 0))
11140 ShowScrollBar(infoPtr->hwndSelf, SB_VERT, FALSE);
11142 uNewView = lpss->styleNew & LVS_TYPEMASK;
11143 uOldView = lpss->styleOld & LVS_TYPEMASK;
11145 if (uNewView != uOldView)
11147 HIMAGELIST himl;
11149 /* LVM_SETVIEW doesn't change window style bits within LVS_TYPEMASK,
11150 changing style updates current view only when view bits change. */
11151 map_style_view(infoPtr);
11152 SendMessageW(infoPtr->hwndEdit, WM_KILLFOCUS, 0, 0);
11153 ShowWindow(infoPtr->hwndHeader, SW_HIDE);
11155 ShowScrollBar(infoPtr->hwndSelf, SB_BOTH, FALSE);
11156 SetRectEmpty(&infoPtr->rcFocus);
11158 himl = (uNewView == LVS_ICON ? infoPtr->himlNormal : infoPtr->himlSmall);
11159 set_icon_size(&infoPtr->iconSize, himl, uNewView != LVS_ICON);
11161 if (uNewView == LVS_REPORT)
11163 HDLAYOUT hl;
11164 WINDOWPOS wp;
11166 LISTVIEW_CreateHeader( infoPtr );
11168 hl.prc = &infoPtr->rcList;
11169 hl.pwpos = &wp;
11170 SendMessageW( infoPtr->hwndHeader, HDM_LAYOUT, 0, (LPARAM)&hl );
11171 SetWindowPos(infoPtr->hwndHeader, infoPtr->hwndSelf, wp.x, wp.y, wp.cx, wp.cy,
11172 wp.flags | ((infoPtr->dwStyle & LVS_NOCOLUMNHEADER)
11173 ? SWP_HIDEWINDOW : SWP_SHOWWINDOW));
11176 LISTVIEW_UpdateItemSize(infoPtr);
11179 if (uNewView == LVS_REPORT || infoPtr->dwLvExStyle & LVS_EX_HEADERINALLVIEWS)
11181 if ((lpss->styleOld ^ lpss->styleNew) & LVS_NOCOLUMNHEADER)
11183 if (lpss->styleNew & LVS_NOCOLUMNHEADER)
11185 /* Turn off the header control */
11186 style = GetWindowLongW(infoPtr->hwndHeader, GWL_STYLE);
11187 TRACE("Hide header control, was 0x%08x\n", style);
11188 SetWindowLongW(infoPtr->hwndHeader, GWL_STYLE, style | HDS_HIDDEN);
11189 } else {
11190 /* Turn on the header control */
11191 if ((style = GetWindowLongW(infoPtr->hwndHeader, GWL_STYLE)) & HDS_HIDDEN)
11193 TRACE("Show header control, was 0x%08x\n", style);
11194 SetWindowLongW(infoPtr->hwndHeader, GWL_STYLE, (style & ~HDS_HIDDEN) | WS_VISIBLE);
11200 if ( (uNewView == LVS_ICON || uNewView == LVS_SMALLICON) &&
11201 (uNewView != uOldView || ((lpss->styleNew ^ lpss->styleOld) & LVS_ALIGNMASK)) )
11202 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
11204 /* update the size of the client area */
11205 LISTVIEW_UpdateSize(infoPtr);
11207 /* add scrollbars if needed */
11208 LISTVIEW_UpdateScroll(infoPtr);
11210 /* invalidate client area + erase background */
11211 LISTVIEW_InvalidateList(infoPtr);
11213 return 0;
11216 /***
11217 * DESCRIPTION:
11218 * Processes WM_STYLECHANGING messages.
11220 * PARAMETER(S):
11221 * [I] wStyleType : window style type (normal or extended)
11222 * [I0] lpss : window style information
11224 * RETURN:
11225 * Zero
11227 static INT LISTVIEW_StyleChanging(WPARAM wStyleType,
11228 STYLESTRUCT *lpss)
11230 TRACE("(styletype=%lx, styleOld=0x%08x, styleNew=0x%08x)\n",
11231 wStyleType, lpss->styleOld, lpss->styleNew);
11233 /* don't forward LVS_OWNERDATA only if not already set to */
11234 if ((lpss->styleNew ^ lpss->styleOld) & LVS_OWNERDATA)
11236 if (lpss->styleOld & LVS_OWNERDATA)
11237 lpss->styleNew |= LVS_OWNERDATA;
11238 else
11239 lpss->styleNew &= ~LVS_OWNERDATA;
11242 return 0;
11245 /***
11246 * DESCRIPTION:
11247 * Processes WM_SHOWWINDOW messages.
11249 * PARAMETER(S):
11250 * [I] infoPtr : valid pointer to the listview structure
11251 * [I] bShown : window is being shown (FALSE when hidden)
11252 * [I] iStatus : window show status
11254 * RETURN:
11255 * Zero
11257 static LRESULT LISTVIEW_ShowWindow(LISTVIEW_INFO *infoPtr, WPARAM bShown, LPARAM iStatus)
11259 /* header delayed creation */
11260 if ((infoPtr->uView == LV_VIEW_DETAILS) && bShown)
11262 LISTVIEW_CreateHeader(infoPtr);
11264 if (!(LVS_NOCOLUMNHEADER & infoPtr->dwStyle))
11265 ShowWindow(infoPtr->hwndHeader, SW_SHOWNORMAL);
11268 return DefWindowProcW(infoPtr->hwndSelf, WM_SHOWWINDOW, bShown, iStatus);
11271 /***
11272 * DESCRIPTION:
11273 * Processes CCM_GETVERSION messages.
11275 * PARAMETER(S):
11276 * [I] infoPtr : valid pointer to the listview structure
11278 * RETURN:
11279 * Current version
11281 static inline LRESULT LISTVIEW_GetVersion(const LISTVIEW_INFO *infoPtr)
11283 return infoPtr->iVersion;
11286 /***
11287 * DESCRIPTION:
11288 * Processes CCM_SETVERSION messages.
11290 * PARAMETER(S):
11291 * [I] infoPtr : valid pointer to the listview structure
11292 * [I] iVersion : version to be set
11294 * RETURN:
11295 * -1 when requested version is greater than DLL version;
11296 * previous version otherwise
11298 static LRESULT LISTVIEW_SetVersion(LISTVIEW_INFO *infoPtr, DWORD iVersion)
11300 INT iOldVersion = infoPtr->iVersion;
11302 if (iVersion > COMCTL32_VERSION)
11303 return -1;
11305 infoPtr->iVersion = iVersion;
11307 TRACE("new version %d\n", iVersion);
11309 return iOldVersion;
11312 /***
11313 * DESCRIPTION:
11314 * Window procedure of the listview control.
11317 static LRESULT WINAPI
11318 LISTVIEW_WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
11320 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)GetWindowLongPtrW(hwnd, 0);
11322 TRACE("(hwnd=%p uMsg=%x wParam=%lx lParam=%lx)\n", hwnd, uMsg, wParam, lParam);
11324 if (!infoPtr && (uMsg != WM_NCCREATE))
11325 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
11327 switch (uMsg)
11329 case LVM_APPROXIMATEVIEWRECT:
11330 return LISTVIEW_ApproximateViewRect(infoPtr, (INT)wParam,
11331 LOWORD(lParam), HIWORD(lParam));
11332 case LVM_ARRANGE:
11333 return LISTVIEW_Arrange(infoPtr, (INT)wParam);
11335 case LVM_CANCELEDITLABEL:
11336 return LISTVIEW_CancelEditLabel(infoPtr);
11338 case LVM_CREATEDRAGIMAGE:
11339 return (LRESULT)LISTVIEW_CreateDragImage(infoPtr, (INT)wParam, (LPPOINT)lParam);
11341 case LVM_DELETEALLITEMS:
11342 return LISTVIEW_DeleteAllItems(infoPtr, FALSE);
11344 case LVM_DELETECOLUMN:
11345 return LISTVIEW_DeleteColumn(infoPtr, (INT)wParam);
11347 case LVM_DELETEITEM:
11348 return LISTVIEW_DeleteItem(infoPtr, (INT)wParam);
11350 case LVM_EDITLABELA:
11351 case LVM_EDITLABELW:
11352 return (LRESULT)LISTVIEW_EditLabelT(infoPtr, (INT)wParam,
11353 uMsg == LVM_EDITLABELW);
11354 /* case LVM_ENABLEGROUPVIEW: */
11356 case LVM_ENSUREVISIBLE:
11357 return LISTVIEW_EnsureVisible(infoPtr, (INT)wParam, (BOOL)lParam);
11359 case LVM_FINDITEMW:
11360 return LISTVIEW_FindItemW(infoPtr, (INT)wParam, (LPLVFINDINFOW)lParam);
11362 case LVM_FINDITEMA:
11363 return LISTVIEW_FindItemA(infoPtr, (INT)wParam, (LPLVFINDINFOA)lParam);
11365 case LVM_GETBKCOLOR:
11366 return infoPtr->clrBk;
11368 /* case LVM_GETBKIMAGE: */
11370 case LVM_GETCALLBACKMASK:
11371 return infoPtr->uCallbackMask;
11373 case LVM_GETCOLUMNA:
11374 case LVM_GETCOLUMNW:
11375 return LISTVIEW_GetColumnT(infoPtr, (INT)wParam, (LPLVCOLUMNW)lParam,
11376 uMsg == LVM_GETCOLUMNW);
11378 case LVM_GETCOLUMNORDERARRAY:
11379 return LISTVIEW_GetColumnOrderArray(infoPtr, (INT)wParam, (LPINT)lParam);
11381 case LVM_GETCOLUMNWIDTH:
11382 return LISTVIEW_GetColumnWidth(infoPtr, (INT)wParam);
11384 case LVM_GETCOUNTPERPAGE:
11385 return LISTVIEW_GetCountPerPage(infoPtr);
11387 case LVM_GETEDITCONTROL:
11388 return (LRESULT)infoPtr->hwndEdit;
11390 case LVM_GETEXTENDEDLISTVIEWSTYLE:
11391 return infoPtr->dwLvExStyle;
11393 /* case LVM_GETGROUPINFO: */
11395 /* case LVM_GETGROUPMETRICS: */
11397 case LVM_GETHEADER:
11398 return (LRESULT)infoPtr->hwndHeader;
11400 case LVM_GETHOTCURSOR:
11401 return (LRESULT)infoPtr->hHotCursor;
11403 case LVM_GETHOTITEM:
11404 return infoPtr->nHotItem;
11406 case LVM_GETHOVERTIME:
11407 return infoPtr->dwHoverTime;
11409 case LVM_GETIMAGELIST:
11410 return (LRESULT)LISTVIEW_GetImageList(infoPtr, (INT)wParam);
11412 /* case LVM_GETINSERTMARK: */
11414 /* case LVM_GETINSERTMARKCOLOR: */
11416 /* case LVM_GETINSERTMARKRECT: */
11418 case LVM_GETISEARCHSTRINGA:
11419 case LVM_GETISEARCHSTRINGW:
11420 FIXME("LVM_GETISEARCHSTRING: unimplemented\n");
11421 return FALSE;
11423 case LVM_GETITEMA:
11424 case LVM_GETITEMW:
11425 return LISTVIEW_GetItemExtT(infoPtr, (LPLVITEMW)lParam, uMsg == LVM_GETITEMW);
11427 case LVM_GETITEMCOUNT:
11428 return infoPtr->nItemCount;
11430 case LVM_GETITEMPOSITION:
11431 return LISTVIEW_GetItemPosition(infoPtr, (INT)wParam, (LPPOINT)lParam);
11433 case LVM_GETITEMRECT:
11434 return LISTVIEW_GetItemRect(infoPtr, (INT)wParam, (LPRECT)lParam);
11436 case LVM_GETITEMSPACING:
11437 return LISTVIEW_GetItemSpacing(infoPtr, (BOOL)wParam);
11439 case LVM_GETITEMSTATE:
11440 return LISTVIEW_GetItemState(infoPtr, (INT)wParam, (UINT)lParam);
11442 case LVM_GETITEMTEXTA:
11443 case LVM_GETITEMTEXTW:
11444 return LISTVIEW_GetItemTextT(infoPtr, (INT)wParam, (LPLVITEMW)lParam,
11445 uMsg == LVM_GETITEMTEXTW);
11447 case LVM_GETNEXTITEM:
11448 return LISTVIEW_GetNextItem(infoPtr, (INT)wParam, LOWORD(lParam));
11450 case LVM_GETNUMBEROFWORKAREAS:
11451 FIXME("LVM_GETNUMBEROFWORKAREAS: unimplemented\n");
11452 return 1;
11454 case LVM_GETORIGIN:
11455 if (!lParam) return FALSE;
11456 if (infoPtr->uView == LV_VIEW_DETAILS ||
11457 infoPtr->uView == LV_VIEW_LIST) return FALSE;
11458 LISTVIEW_GetOrigin(infoPtr, (LPPOINT)lParam);
11459 return TRUE;
11461 /* case LVM_GETOUTLINECOLOR: */
11463 /* case LVM_GETSELECTEDCOLUMN: */
11465 case LVM_GETSELECTEDCOUNT:
11466 return LISTVIEW_GetSelectedCount(infoPtr);
11468 case LVM_GETSELECTIONMARK:
11469 return infoPtr->nSelectionMark;
11471 case LVM_GETSTRINGWIDTHA:
11472 case LVM_GETSTRINGWIDTHW:
11473 return LISTVIEW_GetStringWidthT(infoPtr, (LPCWSTR)lParam,
11474 uMsg == LVM_GETSTRINGWIDTHW);
11476 case LVM_GETSUBITEMRECT:
11477 return LISTVIEW_GetSubItemRect(infoPtr, (UINT)wParam, (LPRECT)lParam);
11479 case LVM_GETTEXTBKCOLOR:
11480 return infoPtr->clrTextBk;
11482 case LVM_GETTEXTCOLOR:
11483 return infoPtr->clrText;
11485 /* case LVM_GETTILEINFO: */
11487 /* case LVM_GETTILEVIEWINFO: */
11489 case LVM_GETTOOLTIPS:
11490 if( !infoPtr->hwndToolTip )
11491 infoPtr->hwndToolTip = COMCTL32_CreateToolTip( hwnd );
11492 return (LRESULT)infoPtr->hwndToolTip;
11494 case LVM_GETTOPINDEX:
11495 return LISTVIEW_GetTopIndex(infoPtr);
11497 case LVM_GETUNICODEFORMAT:
11498 return (infoPtr->notifyFormat == NFR_UNICODE);
11500 case LVM_GETVIEW:
11501 return infoPtr->uView;
11503 case LVM_GETVIEWRECT:
11504 return LISTVIEW_GetViewRect(infoPtr, (LPRECT)lParam);
11506 case LVM_GETWORKAREAS:
11507 FIXME("LVM_GETWORKAREAS: unimplemented\n");
11508 return FALSE;
11510 /* case LVM_HASGROUP: */
11512 case LVM_HITTEST:
11513 return LISTVIEW_HitTest(infoPtr, (LPLVHITTESTINFO)lParam, FALSE, TRUE);
11515 case LVM_INSERTCOLUMNA:
11516 case LVM_INSERTCOLUMNW:
11517 return LISTVIEW_InsertColumnT(infoPtr, (INT)wParam, (LPLVCOLUMNW)lParam,
11518 uMsg == LVM_INSERTCOLUMNW);
11520 /* case LVM_INSERTGROUP: */
11522 /* case LVM_INSERTGROUPSORTED: */
11524 case LVM_INSERTITEMA:
11525 case LVM_INSERTITEMW:
11526 return LISTVIEW_InsertItemT(infoPtr, (LPLVITEMW)lParam, uMsg == LVM_INSERTITEMW);
11528 /* case LVM_INSERTMARKHITTEST: */
11530 /* case LVM_ISGROUPVIEWENABLED: */
11532 case LVM_ISITEMVISIBLE:
11533 return LISTVIEW_IsItemVisible(infoPtr, (INT)wParam);
11535 case LVM_MAPIDTOINDEX:
11536 return LISTVIEW_MapIdToIndex(infoPtr, (UINT)wParam);
11538 case LVM_MAPINDEXTOID:
11539 return LISTVIEW_MapIndexToId(infoPtr, (INT)wParam);
11541 /* case LVM_MOVEGROUP: */
11543 /* case LVM_MOVEITEMTOGROUP: */
11545 case LVM_REDRAWITEMS:
11546 return LISTVIEW_RedrawItems(infoPtr, (INT)wParam, (INT)lParam);
11548 /* case LVM_REMOVEALLGROUPS: */
11550 /* case LVM_REMOVEGROUP: */
11552 case LVM_SCROLL:
11553 return LISTVIEW_Scroll(infoPtr, (INT)wParam, (INT)lParam);
11555 case LVM_SETBKCOLOR:
11556 return LISTVIEW_SetBkColor(infoPtr, (COLORREF)lParam);
11558 /* case LVM_SETBKIMAGE: */
11560 case LVM_SETCALLBACKMASK:
11561 infoPtr->uCallbackMask = (UINT)wParam;
11562 return TRUE;
11564 case LVM_SETCOLUMNA:
11565 case LVM_SETCOLUMNW:
11566 return LISTVIEW_SetColumnT(infoPtr, (INT)wParam, (LPLVCOLUMNW)lParam,
11567 uMsg == LVM_SETCOLUMNW);
11569 case LVM_SETCOLUMNORDERARRAY:
11570 return LISTVIEW_SetColumnOrderArray(infoPtr, (INT)wParam, (LPINT)lParam);
11572 case LVM_SETCOLUMNWIDTH:
11573 return LISTVIEW_SetColumnWidth(infoPtr, (INT)wParam, (short)LOWORD(lParam));
11575 case LVM_SETEXTENDEDLISTVIEWSTYLE:
11576 return LISTVIEW_SetExtendedListViewStyle(infoPtr, (DWORD)wParam, (DWORD)lParam);
11578 /* case LVM_SETGROUPINFO: */
11580 /* case LVM_SETGROUPMETRICS: */
11582 case LVM_SETHOTCURSOR:
11583 return (LRESULT)LISTVIEW_SetHotCursor(infoPtr, (HCURSOR)lParam);
11585 case LVM_SETHOTITEM:
11586 return LISTVIEW_SetHotItem(infoPtr, (INT)wParam);
11588 case LVM_SETHOVERTIME:
11589 return LISTVIEW_SetHoverTime(infoPtr, (DWORD)lParam);
11591 case LVM_SETICONSPACING:
11592 if(lParam == -1)
11593 return LISTVIEW_SetIconSpacing(infoPtr, -1, -1);
11594 return LISTVIEW_SetIconSpacing(infoPtr, LOWORD(lParam), HIWORD(lParam));
11596 case LVM_SETIMAGELIST:
11597 return (LRESULT)LISTVIEW_SetImageList(infoPtr, (INT)wParam, (HIMAGELIST)lParam);
11599 /* case LVM_SETINFOTIP: */
11601 /* case LVM_SETINSERTMARK: */
11603 /* case LVM_SETINSERTMARKCOLOR: */
11605 case LVM_SETITEMA:
11606 case LVM_SETITEMW:
11608 if (infoPtr->dwStyle & LVS_OWNERDATA) return FALSE;
11609 return LISTVIEW_SetItemT(infoPtr, (LPLVITEMW)lParam, (uMsg == LVM_SETITEMW));
11612 case LVM_SETITEMCOUNT:
11613 return LISTVIEW_SetItemCount(infoPtr, (INT)wParam, (DWORD)lParam);
11615 case LVM_SETITEMPOSITION:
11617 POINT pt;
11618 pt.x = (short)LOWORD(lParam);
11619 pt.y = (short)HIWORD(lParam);
11620 return LISTVIEW_SetItemPosition(infoPtr, (INT)wParam, &pt);
11623 case LVM_SETITEMPOSITION32:
11624 return LISTVIEW_SetItemPosition(infoPtr, (INT)wParam, (POINT*)lParam);
11626 case LVM_SETITEMSTATE:
11627 return LISTVIEW_SetItemState(infoPtr, (INT)wParam, (LPLVITEMW)lParam);
11629 case LVM_SETITEMTEXTA:
11630 case LVM_SETITEMTEXTW:
11631 return LISTVIEW_SetItemTextT(infoPtr, (INT)wParam, (LPLVITEMW)lParam,
11632 uMsg == LVM_SETITEMTEXTW);
11634 /* case LVM_SETOUTLINECOLOR: */
11636 /* case LVM_SETSELECTEDCOLUMN: */
11638 case LVM_SETSELECTIONMARK:
11639 return LISTVIEW_SetSelectionMark(infoPtr, (INT)lParam);
11641 case LVM_SETTEXTBKCOLOR:
11642 return LISTVIEW_SetTextBkColor(infoPtr, (COLORREF)lParam);
11644 case LVM_SETTEXTCOLOR:
11645 return LISTVIEW_SetTextColor(infoPtr, (COLORREF)lParam);
11647 /* case LVM_SETTILEINFO: */
11649 /* case LVM_SETTILEVIEWINFO: */
11651 /* case LVM_SETTILEWIDTH: */
11653 case LVM_SETTOOLTIPS:
11654 return (LRESULT)LISTVIEW_SetToolTips(infoPtr, (HWND)lParam);
11656 case LVM_SETUNICODEFORMAT:
11657 return LISTVIEW_SetUnicodeFormat(infoPtr, wParam);
11659 case LVM_SETVIEW:
11660 return LISTVIEW_SetView(infoPtr, wParam);
11662 /* case LVM_SETWORKAREAS: */
11664 /* case LVM_SORTGROUPS: */
11666 case LVM_SORTITEMS:
11667 case LVM_SORTITEMSEX:
11668 return LISTVIEW_SortItems(infoPtr, (PFNLVCOMPARE)lParam, wParam,
11669 uMsg == LVM_SORTITEMSEX);
11670 case LVM_SUBITEMHITTEST:
11671 return LISTVIEW_HitTest(infoPtr, (LPLVHITTESTINFO)lParam, TRUE, FALSE);
11673 case LVM_UPDATE:
11674 return LISTVIEW_Update(infoPtr, (INT)wParam);
11676 case CCM_GETVERSION:
11677 return LISTVIEW_GetVersion(infoPtr);
11679 case CCM_SETVERSION:
11680 return LISTVIEW_SetVersion(infoPtr, wParam);
11682 case WM_CHAR:
11683 return LISTVIEW_ProcessLetterKeys( infoPtr, wParam, lParam );
11685 case WM_COMMAND:
11686 return LISTVIEW_Command(infoPtr, wParam, lParam);
11688 case WM_NCCREATE:
11689 return LISTVIEW_NCCreate(hwnd, wParam, (LPCREATESTRUCTW)lParam);
11691 case WM_CREATE:
11692 return LISTVIEW_Create(hwnd, (LPCREATESTRUCTW)lParam);
11694 case WM_DESTROY:
11695 return LISTVIEW_Destroy(infoPtr);
11697 case WM_ENABLE:
11698 return LISTVIEW_Enable(infoPtr);
11700 case WM_ERASEBKGND:
11701 return LISTVIEW_EraseBkgnd(infoPtr, (HDC)wParam);
11703 case WM_GETDLGCODE:
11704 return DLGC_WANTCHARS | DLGC_WANTARROWS;
11706 case WM_GETFONT:
11707 return (LRESULT)infoPtr->hFont;
11709 case WM_HSCROLL:
11710 return LISTVIEW_HScroll(infoPtr, (INT)LOWORD(wParam), 0);
11712 case WM_KEYDOWN:
11713 return LISTVIEW_KeyDown(infoPtr, (INT)wParam, (LONG)lParam);
11715 case WM_KILLFOCUS:
11716 return LISTVIEW_KillFocus(infoPtr);
11718 case WM_LBUTTONDBLCLK:
11719 return LISTVIEW_LButtonDblClk(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11721 case WM_LBUTTONDOWN:
11722 return LISTVIEW_LButtonDown(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11724 case WM_LBUTTONUP:
11725 return LISTVIEW_LButtonUp(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11727 case WM_MOUSEMOVE:
11728 return LISTVIEW_MouseMove (infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11730 case WM_MOUSEHOVER:
11731 return LISTVIEW_MouseHover(infoPtr, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11733 case WM_NCDESTROY:
11734 return LISTVIEW_NCDestroy(infoPtr);
11736 case WM_NCPAINT:
11737 return LISTVIEW_NCPaint(infoPtr, (HRGN)wParam);
11739 case WM_NOTIFY:
11740 return LISTVIEW_Notify(infoPtr, (LPNMHDR)lParam);
11742 case WM_NOTIFYFORMAT:
11743 return LISTVIEW_NotifyFormat(infoPtr, (HWND)wParam, (INT)lParam);
11745 case WM_PRINTCLIENT:
11746 return LISTVIEW_PrintClient(infoPtr, (HDC)wParam, (DWORD)lParam);
11748 case WM_PAINT:
11749 return LISTVIEW_WMPaint(infoPtr, (HDC)wParam);
11751 case WM_RBUTTONDBLCLK:
11752 return LISTVIEW_RButtonDblClk(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11754 case WM_RBUTTONDOWN:
11755 return LISTVIEW_RButtonDown(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11757 case WM_SETCURSOR:
11758 return LISTVIEW_SetCursor(infoPtr, wParam, lParam);
11760 case WM_SETFOCUS:
11761 return LISTVIEW_SetFocus(infoPtr, (HWND)wParam);
11763 case WM_SETFONT:
11764 return LISTVIEW_SetFont(infoPtr, (HFONT)wParam, (WORD)lParam);
11766 case WM_SETREDRAW:
11767 return LISTVIEW_SetRedraw(infoPtr, (BOOL)wParam);
11769 case WM_SHOWWINDOW:
11770 return LISTVIEW_ShowWindow(infoPtr, wParam, lParam);
11772 case WM_STYLECHANGED:
11773 return LISTVIEW_StyleChanged(infoPtr, wParam, (LPSTYLESTRUCT)lParam);
11775 case WM_STYLECHANGING:
11776 return LISTVIEW_StyleChanging(wParam, (LPSTYLESTRUCT)lParam);
11778 case WM_SYSCOLORCHANGE:
11779 COMCTL32_RefreshSysColors();
11780 return 0;
11782 /* case WM_TIMER: */
11783 case WM_THEMECHANGED:
11784 return LISTVIEW_ThemeChanged(infoPtr);
11786 case WM_VSCROLL:
11787 return LISTVIEW_VScroll(infoPtr, (INT)LOWORD(wParam), 0);
11789 case WM_MOUSEWHEEL:
11790 if (wParam & (MK_SHIFT | MK_CONTROL))
11791 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
11792 return LISTVIEW_MouseWheel(infoPtr, (short int)HIWORD(wParam));
11794 case WM_WINDOWPOSCHANGED:
11795 if (!(((WINDOWPOS *)lParam)->flags & SWP_NOSIZE))
11797 SetWindowPos(infoPtr->hwndSelf, 0, 0, 0, 0, 0, SWP_FRAMECHANGED | SWP_NOACTIVATE |
11798 SWP_NOZORDER | SWP_NOMOVE | SWP_NOSIZE);
11800 if ((infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && (infoPtr->uView == LV_VIEW_DETAILS))
11802 if (notify_measureitem(infoPtr)) LISTVIEW_InvalidateList(infoPtr);
11804 LISTVIEW_Size(infoPtr, ((WINDOWPOS *)lParam)->cx, ((WINDOWPOS *)lParam)->cy);
11806 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
11808 /* case WM_WININICHANGE: */
11810 default:
11811 if ((uMsg >= WM_USER) && (uMsg < WM_APP) && !COMCTL32_IsReflectedMessage(uMsg))
11812 ERR("unknown msg %04x wp=%08lx lp=%08lx\n", uMsg, wParam, lParam);
11814 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
11819 /***
11820 * DESCRIPTION:
11821 * Registers the window class.
11823 * PARAMETER(S):
11824 * None
11826 * RETURN:
11827 * None
11829 void LISTVIEW_Register(void)
11831 WNDCLASSW wndClass;
11833 ZeroMemory(&wndClass, sizeof(WNDCLASSW));
11834 wndClass.style = CS_GLOBALCLASS | CS_DBLCLKS;
11835 wndClass.lpfnWndProc = LISTVIEW_WindowProc;
11836 wndClass.cbClsExtra = 0;
11837 wndClass.cbWndExtra = sizeof(LISTVIEW_INFO *);
11838 wndClass.hCursor = LoadCursorW(0, (LPWSTR)IDC_ARROW);
11839 wndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
11840 wndClass.lpszClassName = WC_LISTVIEWW;
11841 RegisterClassW(&wndClass);
11844 /***
11845 * DESCRIPTION:
11846 * Unregisters the window class.
11848 * PARAMETER(S):
11849 * None
11851 * RETURN:
11852 * None
11854 void LISTVIEW_Unregister(void)
11856 UnregisterClassW(WC_LISTVIEWW, NULL);
11859 /***
11860 * DESCRIPTION:
11861 * Handle any WM_COMMAND messages
11863 * PARAMETER(S):
11864 * [I] infoPtr : valid pointer to the listview structure
11865 * [I] wParam : the first message parameter
11866 * [I] lParam : the second message parameter
11868 * RETURN:
11869 * Zero.
11871 static LRESULT LISTVIEW_Command(LISTVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
11874 TRACE("(%p %x %x %lx)\n", infoPtr, HIWORD(wParam), LOWORD(wParam), lParam);
11876 if (!infoPtr->hwndEdit) return 0;
11878 switch (HIWORD(wParam))
11880 case EN_UPDATE:
11883 * Adjust the edit window size
11885 WCHAR buffer[1024];
11886 HDC hdc = GetDC(infoPtr->hwndEdit);
11887 HFONT hFont, hOldFont = 0;
11888 RECT rect;
11889 SIZE sz;
11891 if (!infoPtr->hwndEdit || !hdc) return 0;
11892 GetWindowTextW(infoPtr->hwndEdit, buffer, ARRAY_SIZE(buffer));
11893 GetWindowRect(infoPtr->hwndEdit, &rect);
11895 /* Select font to get the right dimension of the string */
11896 hFont = (HFONT)SendMessageW(infoPtr->hwndEdit, WM_GETFONT, 0, 0);
11897 if (hFont)
11899 hOldFont = SelectObject(hdc, hFont);
11902 if (GetTextExtentPoint32W(hdc, buffer, lstrlenW(buffer), &sz))
11904 TEXTMETRICW textMetric;
11906 /* Add Extra spacing for the next character */
11907 GetTextMetricsW(hdc, &textMetric);
11908 sz.cx += (textMetric.tmMaxCharWidth * 2);
11910 SetWindowPos(infoPtr->hwndEdit, NULL, 0, 0, sz.cx,
11911 rect.bottom - rect.top, SWP_DRAWFRAME | SWP_NOMOVE | SWP_NOZORDER);
11913 if (hFont)
11914 SelectObject(hdc, hOldFont);
11916 ReleaseDC(infoPtr->hwndEdit, hdc);
11918 break;
11920 case EN_KILLFOCUS:
11922 if (infoPtr->notify_mask & NOTIFY_MASK_END_LABEL_EDIT)
11923 LISTVIEW_CancelEditLabel(infoPtr);
11924 break;
11927 default:
11928 return SendMessageW (infoPtr->hwndNotify, WM_COMMAND, wParam, lParam);
11931 return 0;