gdiplus/tests: Add tests for GdipMeasureString with StringFormatFlagsNoWrap.
[wine.git] / dlls / comctl32 / listview.c
blobe31c787c4a46d359cfba1442184485f46a75854c
1 /*
2 * Listview control
4 * Copyright 1998, 1999 Eric Kohl
5 * Copyright 1999 Luc Tourangeau
6 * Copyright 2000 Jason Mawdsley
7 * Copyright 2001 CodeWeavers Inc.
8 * Copyright 2002 Dimitrie O. Paun
9 * Copyright 2009-2015 Nikolay Sivov
10 * Copyright 2009 Owen Rudge for CodeWeavers
11 * Copyright 2012-2013 Daniel Jelinski
13 * This library is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU Lesser General Public
15 * License as published by the Free Software Foundation; either
16 * version 2.1 of the License, or (at your option) any later version.
18 * This library is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 * Lesser General Public License for more details.
23 * You should have received a copy of the GNU Lesser General Public
24 * License along with this library; if not, write to the Free Software
25 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
27 * TODO:
29 * Default Message Processing
30 * -- WM_CREATE: create the icon and small icon image lists at this point only if
31 * the LVS_SHAREIMAGELISTS style is not specified.
32 * -- WM_WINDOWPOSCHANGED: arrange the list items if the current view is icon
33 * or small icon and the LVS_AUTOARRANGE style is specified.
34 * -- WM_TIMER
35 * -- WM_WININICHANGE
37 * Features
38 * -- Hot item handling, mouse hovering
39 * -- Workareas support
40 * -- Tilemode support
41 * -- Groups support
43 * Bugs
44 * -- Expand large item in ICON mode when the cursor is flying over the icon or text.
45 * -- Support CustomDraw options for _WIN32_IE >= 0x560 (see NMLVCUSTOMDRAW docs).
46 * -- LVA_SNAPTOGRID not implemented
47 * -- LISTVIEW_ApproximateViewRect partially implemented
48 * -- LISTVIEW_StyleChanged doesn't handle some changes too well
50 * Speedups
51 * -- LISTVIEW_GetNextItem needs to be rewritten. It is currently
52 * linear in the number of items in the list, and this is
53 * unacceptable for large lists.
54 * -- if list is sorted by item text LISTVIEW_InsertItemT could use
55 * binary search to calculate item index (e.g. DPA_Search()).
56 * This requires sorted state to be reliably tracked in item modifiers.
57 * -- we should keep an ordered array of coordinates in iconic mode.
58 * This would allow framing items (iterator_frameditems),
59 * and finding the nearest item (LVFI_NEARESTXY) a lot more efficiently.
61 * Flags
62 * -- LVIF_COLUMNS
63 * -- LVIF_GROUPID
65 * States
66 * -- LVIS_ACTIVATING (not currently supported by comctl32.dll version 6.0)
67 * -- LVIS_DROPHILITED
69 * Styles
70 * -- LVS_NOLABELWRAP
71 * -- LVS_NOSCROLL (see Q137520)
72 * -- LVS_ALIGNTOP
74 * Extended Styles
75 * -- LVS_EX_BORDERSELECT
76 * -- LVS_EX_FLATSB
77 * -- LVS_EX_INFOTIP
78 * -- LVS_EX_LABELTIP
79 * -- LVS_EX_MULTIWORKAREAS
80 * -- LVS_EX_REGIONAL
81 * -- LVS_EX_SIMPLESELECT
82 * -- LVS_EX_TWOCLICKACTIVATE
83 * -- LVS_EX_UNDERLINECOLD
84 * -- LVS_EX_UNDERLINEHOT
86 * Notifications:
87 * -- LVN_BEGINSCROLL, LVN_ENDSCROLL
88 * -- LVN_GETINFOTIP
89 * -- LVN_HOTTRACK
90 * -- LVN_SETDISPINFO
92 * Messages:
93 * -- LVM_ENABLEGROUPVIEW
94 * -- LVM_GETBKIMAGE, LVM_SETBKIMAGE
95 * -- LVM_GETGROUPINFO, LVM_SETGROUPINFO
96 * -- LVM_GETGROUPMETRICS, LVM_SETGROUPMETRICS
97 * -- LVM_GETINSERTMARK, LVM_SETINSERTMARK
98 * -- LVM_GETINSERTMARKCOLOR, LVM_SETINSERTMARKCOLOR
99 * -- LVM_GETINSERTMARKRECT
100 * -- LVM_GETNUMBEROFWORKAREAS
101 * -- LVM_GETOUTLINECOLOR, LVM_SETOUTLINECOLOR
102 * -- LVM_GETISEARCHSTRINGW, LVM_GETISEARCHSTRINGA
103 * -- LVM_GETTILEINFO, LVM_SETTILEINFO
104 * -- LVM_GETTILEVIEWINFO, LVM_SETTILEVIEWINFO
105 * -- LVM_GETWORKAREAS, LVM_SETWORKAREAS
106 * -- LVM_HASGROUP, LVM_INSERTGROUP, LVM_REMOVEGROUP, LVM_REMOVEALLGROUPS
107 * -- LVM_INSERTGROUPSORTED
108 * -- LVM_INSERTMARKHITTEST
109 * -- LVM_ISGROUPVIEWENABLED
110 * -- LVM_MOVEGROUP
111 * -- LVM_MOVEITEMTOGROUP
112 * -- LVM_SETINFOTIP
113 * -- LVM_SETTILEWIDTH
114 * -- LVM_SORTGROUPS
116 * Macros:
117 * -- ListView_GetHoverTime, ListView_SetHoverTime
118 * -- ListView_GetISearchString
119 * -- ListView_GetNumberOfWorkAreas
120 * -- ListView_GetWorkAreas, ListView_SetWorkAreas
122 * Functions:
123 * -- LVGroupComparE
126 #include <assert.h>
127 #include <ctype.h>
128 #include <string.h>
129 #include <stdlib.h>
130 #include <stdarg.h>
131 #include <stdio.h>
133 #include "windef.h"
134 #include "winbase.h"
135 #include "winnt.h"
136 #include "wingdi.h"
137 #include "winuser.h"
138 #include "winnls.h"
139 #include "commctrl.h"
140 #include "comctl32.h"
141 #include "uxtheme.h"
142 #include "shlwapi.h"
144 #include "wine/debug.h"
146 WINE_DEFAULT_DEBUG_CHANNEL(listview);
148 typedef struct tagCOLUMN_INFO
150 RECT rcHeader; /* tracks the header's rectangle */
151 INT fmt; /* same as LVCOLUMN.fmt */
152 INT cxMin;
153 } COLUMN_INFO;
155 typedef struct tagITEMHDR
157 LPWSTR pszText;
158 INT iImage;
159 } ITEMHDR, *LPITEMHDR;
161 typedef struct tagSUBITEM_INFO
163 ITEMHDR hdr;
164 INT iSubItem;
165 } SUBITEM_INFO;
167 typedef struct tagITEM_ID ITEM_ID;
169 typedef struct tagITEM_INFO
171 ITEMHDR hdr;
172 UINT state;
173 LPARAM lParam;
174 INT iIndent;
175 ITEM_ID *id;
176 } ITEM_INFO;
178 struct tagITEM_ID
180 UINT id; /* item id */
181 HDPA item; /* link to item data */
184 typedef struct tagRANGE
186 INT lower;
187 INT upper;
188 } RANGE;
190 typedef struct tagRANGES
192 HDPA hdpa;
193 } *RANGES;
195 typedef struct tagITERATOR
197 INT nItem;
198 INT nSpecial;
199 RANGE range;
200 RANGES ranges;
201 INT index;
202 } ITERATOR;
204 typedef struct tagDELAYED_ITEM_EDIT
206 BOOL fEnabled;
207 INT iItem;
208 } DELAYED_ITEM_EDIT;
210 enum notification_mask
212 NOTIFY_MASK_ITEM_CHANGE = 0x1,
213 NOTIFY_MASK_END_LABEL_EDIT = 0x2,
214 NOTIFY_MASK_UNMASK_ALL = 0xffffffff
217 typedef struct tagLISTVIEW_INFO
219 /* control window */
220 HWND hwndSelf;
221 RECT rcList; /* This rectangle is really the window
222 * client rectangle possibly reduced by the
223 * horizontal scroll bar and/or header - see
224 * LISTVIEW_UpdateSize. This rectangle offset
225 * by the LISTVIEW_GetOrigin value is in
226 * client coordinates */
228 /* notification window */
229 SHORT notifyFormat;
230 HWND hwndNotify;
231 DWORD notify_mask;
232 UINT uCallbackMask;
234 /* tooltips */
235 HWND hwndToolTip;
237 /* items */
238 INT nItemCount; /* the number of items in the list */
239 HDPA hdpaItems; /* array ITEM_INFO pointers */
240 HDPA hdpaItemIds; /* array of ITEM_ID pointers */
241 HDPA hdpaPosX; /* maintains the (X, Y) coordinates of the */
242 HDPA hdpaPosY; /* items in LVS_ICON, and LVS_SMALLICON modes */
243 RANGES selectionRanges;
244 INT nSelectionMark; /* item to start next multiselection from */
245 INT nHotItem;
247 /* columns */
248 HDPA hdpaColumns; /* array of COLUMN_INFO pointers */
249 BOOL colRectsDirty; /* trigger column rectangles requery from header */
250 INT selected_column; /* index for LVM_SETSELECTEDCOLUMN/LVM_GETSELECTEDCOLUMN */
252 /* item metrics */
253 BOOL bNoItemMetrics; /* flags if item metrics are not yet computed */
254 INT nItemHeight;
255 INT nItemWidth;
257 /* sorting */
258 PFNLVCOMPARE pfnCompare; /* sorting callback pointer */
259 LPARAM lParamSort;
261 /* style */
262 DWORD dwStyle; /* the cached window GWL_STYLE */
263 DWORD dwLvExStyle; /* extended listview style */
264 DWORD uView; /* current view available through LVM_[G,S]ETVIEW */
266 /* edit item */
267 HWND hwndEdit;
268 WNDPROC EditWndProc;
269 INT nEditLabelItem;
270 DELAYED_ITEM_EDIT itemEdit; /* Pointer to this structure will be the timer ID */
272 /* icons */
273 HIMAGELIST himlNormal;
274 HIMAGELIST himlSmall;
275 HIMAGELIST himlState;
276 SIZE iconSize;
277 BOOL autoSpacing;
278 SIZE iconSpacing;
279 SIZE iconStateSize;
280 POINT currIconPos; /* this is the position next icon will be placed */
282 /* header */
283 HWND hwndHeader;
284 INT xTrackLine; /* The x coefficient of the track line or -1 if none */
286 /* marquee selection */
287 BOOL bMarqueeSelect; /* marquee selection/highlight underway */
288 BOOL bScrolling;
289 RECT marqueeRect; /* absolute coordinates of marquee selection */
290 RECT marqueeDrawRect; /* relative coordinates for drawing marquee */
291 POINT marqueeOrigin; /* absolute coordinates of marquee click origin */
293 /* focus drawing */
294 BOOL bFocus; /* control has focus */
295 INT nFocusedItem;
296 RECT rcFocus; /* focus bounds */
298 /* colors */
299 HBRUSH hBkBrush;
300 COLORREF clrBk;
301 COLORREF clrText;
302 COLORREF clrTextBk;
304 /* font */
305 HFONT hDefaultFont;
306 HFONT hFont;
307 INT ntmHeight; /* Some cached metrics of the font used */
308 INT ntmMaxCharWidth; /* by the listview to draw items */
309 INT nEllipsisWidth;
311 /* mouse operation */
312 BOOL bLButtonDown;
313 BOOL bDragging;
314 POINT ptClickPos; /* point where the user clicked */
315 INT nLButtonDownItem; /* tracks item to reset multiselection on WM_LBUTTONUP */
316 DWORD dwHoverTime;
317 HCURSOR hHotCursor;
318 INT cWheelRemainder;
320 /* keyboard operation */
321 DWORD lastKeyPressTimestamp;
322 WPARAM charCode;
323 INT nSearchParamLength;
324 WCHAR szSearchParam[ MAX_PATH ];
326 /* painting */
327 BOOL bIsDrawing; /* Drawing in progress */
328 INT nMeasureItemHeight; /* WM_MEASUREITEM result */
329 BOOL redraw; /* WM_SETREDRAW switch */
331 /* misc */
332 DWORD iVersion; /* CCM_[G,S]ETVERSION */
333 } LISTVIEW_INFO;
336 * constants
338 /* How many we debug buffer to allocate */
339 #define DEBUG_BUFFERS 20
340 /* The size of a single debug buffer */
341 #define DEBUG_BUFFER_SIZE 256
343 /* Internal interface to LISTVIEW_HScroll and LISTVIEW_VScroll */
344 #define SB_INTERNAL -1
346 /* maximum size of a label */
347 #define DISP_TEXT_SIZE 260
349 /* padding for items in list and small icon display modes */
350 #define WIDTH_PADDING 12
352 /* padding for items in list, report and small icon display modes */
353 #define HEIGHT_PADDING 1
355 /* offset of items in report display mode */
356 #define REPORT_MARGINX 2
358 /* padding for icon in large icon display mode
359 * ICON_TOP_PADDING_NOTHITABLE - space between top of box and area
360 * that HITTEST will see.
361 * ICON_TOP_PADDING_HITABLE - spacing between above and icon.
362 * ICON_TOP_PADDING - sum of the two above.
363 * ICON_BOTTOM_PADDING - between bottom of icon and top of text
364 * LABEL_HOR_PADDING - between text and sides of box
365 * LABEL_VERT_PADDING - between bottom of text and end of box
367 * ICON_LR_PADDING - additional width above icon size.
368 * ICON_LR_HALF - half of the above value
370 #define ICON_TOP_PADDING_NOTHITABLE 2
371 #define ICON_TOP_PADDING_HITABLE 2
372 #define ICON_TOP_PADDING (ICON_TOP_PADDING_NOTHITABLE + ICON_TOP_PADDING_HITABLE)
373 #define ICON_BOTTOM_PADDING 4
374 #define LABEL_HOR_PADDING 5
375 #define LABEL_VERT_PADDING 7
376 #define ICON_LR_PADDING 16
377 #define ICON_LR_HALF (ICON_LR_PADDING/2)
379 /* default label width for items in list and small icon display modes */
380 #define DEFAULT_LABEL_WIDTH 40
381 /* maximum select rectangle width for empty text item in LV_VIEW_DETAILS */
382 #define MAX_EMPTYTEXT_SELECT_WIDTH 80
384 /* default column width for items in list display mode */
385 #define DEFAULT_COLUMN_WIDTH 128
387 /* Size of "line" scroll for V & H scrolls */
388 #define LISTVIEW_SCROLL_ICON_LINE_SIZE 37
390 /* Padding between image and label */
391 #define IMAGE_PADDING 2
393 /* Padding behind the label */
394 #define TRAILING_LABEL_PADDING 12
395 #define TRAILING_HEADER_PADDING 11
397 /* Border for the icon caption */
398 #define CAPTION_BORDER 2
400 /* Standard DrawText flags */
401 #define LV_ML_DT_FLAGS (DT_TOP | DT_NOPREFIX | DT_EDITCONTROL | DT_CENTER | DT_WORDBREAK | DT_WORD_ELLIPSIS | DT_END_ELLIPSIS)
402 #define LV_FL_DT_FLAGS (DT_TOP | DT_NOPREFIX | DT_EDITCONTROL | DT_CENTER | DT_WORDBREAK | DT_NOCLIP)
403 #define LV_SL_DT_FLAGS (DT_VCENTER | DT_NOPREFIX | DT_EDITCONTROL | DT_SINGLELINE | DT_WORD_ELLIPSIS | DT_END_ELLIPSIS)
405 /* Image index from state */
406 #define STATEIMAGEINDEX(x) (((x) & LVIS_STATEIMAGEMASK) >> 12)
408 /* The time in milliseconds to reset the search in the list */
409 #define KEY_DELAY 450
411 /* Dump the LISTVIEW_INFO structure to the debug channel */
412 #define LISTVIEW_DUMP(iP) do { \
413 TRACE("hwndSelf=%p, clrBk=0x%06x, clrText=0x%06x, clrTextBk=0x%06x, ItemHeight=%d, ItemWidth=%d, Style=0x%08x\n", \
414 iP->hwndSelf, iP->clrBk, iP->clrText, iP->clrTextBk, \
415 iP->nItemHeight, iP->nItemWidth, iP->dwStyle); \
416 TRACE("hwndSelf=%p, himlNor=%p, himlSml=%p, himlState=%p, Focused=%d, Hot=%d, exStyle=0x%08x, Focus=%d\n", \
417 iP->hwndSelf, iP->himlNormal, iP->himlSmall, iP->himlState, \
418 iP->nFocusedItem, iP->nHotItem, iP->dwLvExStyle, iP->bFocus ); \
419 TRACE("hwndSelf=%p, ntmH=%d, icSz.cx=%d, icSz.cy=%d, icSp.cx=%d, icSp.cy=%d, notifyFmt=%d\n", \
420 iP->hwndSelf, iP->ntmHeight, iP->iconSize.cx, iP->iconSize.cy, \
421 iP->iconSpacing.cx, iP->iconSpacing.cy, iP->notifyFormat); \
422 TRACE("hwndSelf=%p, rcList=%s\n", iP->hwndSelf, wine_dbgstr_rect(&iP->rcList)); \
423 } while(0)
425 static const WCHAR themeClass[] = {'L','i','s','t','V','i','e','w',0};
428 * forward declarations
430 static BOOL LISTVIEW_GetItemT(const LISTVIEW_INFO *, LPLVITEMW, BOOL);
431 static void LISTVIEW_GetItemBox(const LISTVIEW_INFO *, INT, LPRECT);
432 static void LISTVIEW_GetItemOrigin(const LISTVIEW_INFO *, INT, LPPOINT);
433 static BOOL LISTVIEW_GetItemPosition(const LISTVIEW_INFO *, INT, LPPOINT);
434 static BOOL LISTVIEW_GetItemRect(const LISTVIEW_INFO *, INT, LPRECT);
435 static void LISTVIEW_GetOrigin(const LISTVIEW_INFO *, LPPOINT);
436 static BOOL LISTVIEW_GetViewRect(const LISTVIEW_INFO *, LPRECT);
437 static void LISTVIEW_UpdateSize(LISTVIEW_INFO *);
438 static LRESULT LISTVIEW_Command(LISTVIEW_INFO *, WPARAM, LPARAM);
439 static INT LISTVIEW_GetStringWidthT(const LISTVIEW_INFO *, LPCWSTR, BOOL);
440 static BOOL LISTVIEW_KeySelection(LISTVIEW_INFO *, INT, BOOL);
441 static UINT LISTVIEW_GetItemState(const LISTVIEW_INFO *, INT, UINT);
442 static BOOL LISTVIEW_SetItemState(LISTVIEW_INFO *, INT, const LVITEMW *);
443 static LRESULT LISTVIEW_VScroll(LISTVIEW_INFO *, INT, INT);
444 static LRESULT LISTVIEW_HScroll(LISTVIEW_INFO *, INT, INT);
445 static BOOL LISTVIEW_EnsureVisible(LISTVIEW_INFO *, INT, BOOL);
446 static HIMAGELIST LISTVIEW_SetImageList(LISTVIEW_INFO *, INT, HIMAGELIST);
447 static INT LISTVIEW_HitTest(const LISTVIEW_INFO *, LPLVHITTESTINFO, BOOL, BOOL);
448 static BOOL LISTVIEW_EndEditLabelT(LISTVIEW_INFO *, BOOL, BOOL);
449 static BOOL LISTVIEW_Scroll(LISTVIEW_INFO *, INT, INT);
451 /******** Text handling functions *************************************/
453 /* A text pointer is either NULL, LPSTR_TEXTCALLBACK, or points to a
454 * text string. The string may be ANSI or Unicode, in which case
455 * the boolean isW tells us the type of the string.
457 * The name of the function tell what type of strings it expects:
458 * W: Unicode, T: ANSI/Unicode - function of isW
461 static inline BOOL is_text(LPCWSTR text)
463 return text != NULL && text != LPSTR_TEXTCALLBACKW;
466 static inline int textlenT(LPCWSTR text, BOOL isW)
468 return !is_text(text) ? 0 :
469 isW ? lstrlenW(text) : lstrlenA((LPCSTR)text);
472 static inline void textcpynT(LPWSTR dest, BOOL isDestW, LPCWSTR src, BOOL isSrcW, INT max)
474 if (isDestW)
475 if (isSrcW) lstrcpynW(dest, src, max);
476 else MultiByteToWideChar(CP_ACP, 0, (LPCSTR)src, -1, dest, max);
477 else
478 if (isSrcW) WideCharToMultiByte(CP_ACP, 0, src, -1, (LPSTR)dest, max, NULL, NULL);
479 else lstrcpynA((LPSTR)dest, (LPCSTR)src, max);
482 static inline LPWSTR textdupTtoW(LPCWSTR text, BOOL isW)
484 LPWSTR wstr = (LPWSTR)text;
486 if (!isW && is_text(text))
488 INT len = MultiByteToWideChar(CP_ACP, 0, (LPCSTR)text, -1, NULL, 0);
489 wstr = Alloc(len * sizeof(WCHAR));
490 if (wstr) MultiByteToWideChar(CP_ACP, 0, (LPCSTR)text, -1, wstr, len);
492 TRACE(" wstr=%s\n", text == LPSTR_TEXTCALLBACKW ? "(callback)" : debugstr_w(wstr));
493 return wstr;
496 static inline void textfreeT(LPWSTR wstr, BOOL isW)
498 if (!isW && is_text(wstr)) Free (wstr);
502 * dest is a pointer to a Unicode string
503 * src is a pointer to a string (Unicode if isW, ANSI if !isW)
505 static BOOL textsetptrT(LPWSTR *dest, LPCWSTR src, BOOL isW)
507 BOOL bResult = TRUE;
509 if (src == LPSTR_TEXTCALLBACKW)
511 if (is_text(*dest)) Free(*dest);
512 *dest = LPSTR_TEXTCALLBACKW;
514 else
516 LPWSTR pszText = textdupTtoW(src, isW);
517 if (*dest == LPSTR_TEXTCALLBACKW) *dest = NULL;
518 bResult = Str_SetPtrW(dest, pszText);
519 textfreeT(pszText, isW);
521 return bResult;
525 * compares a Unicode to a Unicode/ANSI text string
527 static inline int textcmpWT(LPCWSTR aw, LPCWSTR bt, BOOL isW)
529 if (!aw) return bt ? -1 : 0;
530 if (!bt) return 1;
531 if (aw == LPSTR_TEXTCALLBACKW)
532 return bt == LPSTR_TEXTCALLBACKW ? 1 : -1;
533 if (bt != LPSTR_TEXTCALLBACKW)
535 LPWSTR bw = textdupTtoW(bt, isW);
536 int r = bw ? lstrcmpW(aw, bw) : 1;
537 textfreeT(bw, isW);
538 return r;
541 return 1;
544 /******** Debugging functions *****************************************/
546 static inline LPCSTR debugtext_t(LPCWSTR text, BOOL isW)
548 if (text == LPSTR_TEXTCALLBACKW) return "(callback)";
549 return isW ? debugstr_w(text) : debugstr_a((LPCSTR)text);
552 static inline LPCSTR debugtext_tn(LPCWSTR text, BOOL isW, INT n)
554 if (text == LPSTR_TEXTCALLBACKW) return "(callback)";
555 n = min(textlenT(text, isW), n);
556 return isW ? debugstr_wn(text, n) : debugstr_an((LPCSTR)text, n);
559 static char* debug_getbuf(void)
561 static int index = 0;
562 static char buffers[DEBUG_BUFFERS][DEBUG_BUFFER_SIZE];
563 return buffers[index++ % DEBUG_BUFFERS];
566 static inline const char* debugrange(const RANGE *lprng)
568 if (!lprng) return "(null)";
569 return wine_dbg_sprintf("[%d, %d]", lprng->lower, lprng->upper);
572 static const char* debugscrollinfo(const SCROLLINFO *pScrollInfo)
574 char* buf = debug_getbuf(), *text = buf;
575 int len, size = DEBUG_BUFFER_SIZE;
577 if (pScrollInfo == NULL) return "(null)";
578 len = snprintf(buf, size, "{cbSize=%u, ", pScrollInfo->cbSize);
579 if (len == -1) goto end;
580 buf += len; size -= len;
581 if (pScrollInfo->fMask & SIF_RANGE)
582 len = snprintf(buf, size, "nMin=%d, nMax=%d, ", pScrollInfo->nMin, pScrollInfo->nMax);
583 else len = 0;
584 if (len == -1) goto end;
585 buf += len; size -= len;
586 if (pScrollInfo->fMask & SIF_PAGE)
587 len = snprintf(buf, size, "nPage=%u, ", pScrollInfo->nPage);
588 else len = 0;
589 if (len == -1) goto end;
590 buf += len; size -= len;
591 if (pScrollInfo->fMask & SIF_POS)
592 len = snprintf(buf, size, "nPos=%d, ", pScrollInfo->nPos);
593 else len = 0;
594 if (len == -1) goto end;
595 buf += len; size -= len;
596 if (pScrollInfo->fMask & SIF_TRACKPOS)
597 len = snprintf(buf, size, "nTrackPos=%d, ", pScrollInfo->nTrackPos);
598 else len = 0;
599 if (len == -1) goto end;
600 buf += len;
601 goto undo;
602 end:
603 buf = text + strlen(text);
604 undo:
605 if (buf - text > 2) { buf[-2] = '}'; buf[-1] = 0; }
606 return text;
609 static const char* debugnmlistview(const NMLISTVIEW *plvnm)
611 if (!plvnm) return "(null)";
612 return wine_dbg_sprintf("iItem=%d, iSubItem=%d, uNewState=0x%x,"
613 " uOldState=0x%x, uChanged=0x%x, ptAction=%s, lParam=%ld",
614 plvnm->iItem, plvnm->iSubItem, plvnm->uNewState, plvnm->uOldState,
615 plvnm->uChanged, wine_dbgstr_point(&plvnm->ptAction), plvnm->lParam);
618 static const char* debuglvitem_t(const LVITEMW *lpLVItem, BOOL isW)
620 char* buf = debug_getbuf(), *text = buf;
621 int len, size = DEBUG_BUFFER_SIZE;
623 if (lpLVItem == NULL) return "(null)";
624 len = snprintf(buf, size, "{iItem=%d, iSubItem=%d, ", lpLVItem->iItem, lpLVItem->iSubItem);
625 if (len == -1) goto end;
626 buf += len; size -= len;
627 if (lpLVItem->mask & LVIF_STATE)
628 len = snprintf(buf, size, "state=%x, stateMask=%x, ", lpLVItem->state, lpLVItem->stateMask);
629 else len = 0;
630 if (len == -1) goto end;
631 buf += len; size -= len;
632 if (lpLVItem->mask & LVIF_TEXT)
633 len = snprintf(buf, size, "pszText=%s, cchTextMax=%d, ", debugtext_tn(lpLVItem->pszText, isW, 80), lpLVItem->cchTextMax);
634 else len = 0;
635 if (len == -1) goto end;
636 buf += len; size -= len;
637 if (lpLVItem->mask & LVIF_IMAGE)
638 len = snprintf(buf, size, "iImage=%d, ", lpLVItem->iImage);
639 else len = 0;
640 if (len == -1) goto end;
641 buf += len; size -= len;
642 if (lpLVItem->mask & LVIF_PARAM)
643 len = snprintf(buf, size, "lParam=%lx, ", lpLVItem->lParam);
644 else len = 0;
645 if (len == -1) goto end;
646 buf += len; size -= len;
647 if (lpLVItem->mask & LVIF_INDENT)
648 len = snprintf(buf, size, "iIndent=%d, ", lpLVItem->iIndent);
649 else len = 0;
650 if (len == -1) goto end;
651 buf += len;
652 goto undo;
653 end:
654 buf = text + strlen(text);
655 undo:
656 if (buf - text > 2) { buf[-2] = '}'; buf[-1] = 0; }
657 return text;
660 static const char* debuglvcolumn_t(const LVCOLUMNW *lpColumn, BOOL isW)
662 char* buf = debug_getbuf(), *text = buf;
663 int len, size = DEBUG_BUFFER_SIZE;
665 if (lpColumn == NULL) return "(null)";
666 len = snprintf(buf, size, "{");
667 if (len == -1) goto end;
668 buf += len; size -= len;
669 if (lpColumn->mask & LVCF_SUBITEM)
670 len = snprintf(buf, size, "iSubItem=%d, ", lpColumn->iSubItem);
671 else len = 0;
672 if (len == -1) goto end;
673 buf += len; size -= len;
674 if (lpColumn->mask & LVCF_FMT)
675 len = snprintf(buf, size, "fmt=%x, ", lpColumn->fmt);
676 else len = 0;
677 if (len == -1) goto end;
678 buf += len; size -= len;
679 if (lpColumn->mask & LVCF_WIDTH)
680 len = snprintf(buf, size, "cx=%d, ", lpColumn->cx);
681 else len = 0;
682 if (len == -1) goto end;
683 buf += len; size -= len;
684 if (lpColumn->mask & LVCF_TEXT)
685 len = snprintf(buf, size, "pszText=%s, cchTextMax=%d, ", debugtext_tn(lpColumn->pszText, isW, 80), lpColumn->cchTextMax);
686 else len = 0;
687 if (len == -1) goto end;
688 buf += len; size -= len;
689 if (lpColumn->mask & LVCF_IMAGE)
690 len = snprintf(buf, size, "iImage=%d, ", lpColumn->iImage);
691 else len = 0;
692 if (len == -1) goto end;
693 buf += len; size -= len;
694 if (lpColumn->mask & LVCF_ORDER)
695 len = snprintf(buf, size, "iOrder=%d, ", lpColumn->iOrder);
696 else len = 0;
697 if (len == -1) goto end;
698 buf += len;
699 goto undo;
700 end:
701 buf = text + strlen(text);
702 undo:
703 if (buf - text > 2) { buf[-2] = '}'; buf[-1] = 0; }
704 return text;
707 static const char* debuglvhittestinfo(const LVHITTESTINFO *lpht)
709 if (!lpht) return "(null)";
711 return wine_dbg_sprintf("{pt=%s, flags=0x%x, iItem=%d, iSubItem=%d}",
712 wine_dbgstr_point(&lpht->pt), lpht->flags, lpht->iItem, lpht->iSubItem);
715 /* Return the corresponding text for a given scroll value */
716 static inline LPCSTR debugscrollcode(int nScrollCode)
718 switch(nScrollCode)
720 case SB_LINELEFT: return "SB_LINELEFT";
721 case SB_LINERIGHT: return "SB_LINERIGHT";
722 case SB_PAGELEFT: return "SB_PAGELEFT";
723 case SB_PAGERIGHT: return "SB_PAGERIGHT";
724 case SB_THUMBPOSITION: return "SB_THUMBPOSITION";
725 case SB_THUMBTRACK: return "SB_THUMBTRACK";
726 case SB_ENDSCROLL: return "SB_ENDSCROLL";
727 case SB_INTERNAL: return "SB_INTERNAL";
728 default: return "unknown";
733 /******** Notification functions ************************************/
735 static int get_ansi_notification(UINT unicodeNotificationCode)
737 switch (unicodeNotificationCode)
739 case LVN_BEGINLABELEDITA:
740 case LVN_BEGINLABELEDITW: return LVN_BEGINLABELEDITA;
741 case LVN_ENDLABELEDITA:
742 case LVN_ENDLABELEDITW: return LVN_ENDLABELEDITA;
743 case LVN_GETDISPINFOA:
744 case LVN_GETDISPINFOW: return LVN_GETDISPINFOA;
745 case LVN_SETDISPINFOA:
746 case LVN_SETDISPINFOW: return LVN_SETDISPINFOA;
747 case LVN_ODFINDITEMA:
748 case LVN_ODFINDITEMW: return LVN_ODFINDITEMA;
749 case LVN_GETINFOTIPA:
750 case LVN_GETINFOTIPW: return LVN_GETINFOTIPA;
751 /* header forwards */
752 case HDN_TRACKA:
753 case HDN_TRACKW: return HDN_TRACKA;
754 case HDN_ENDTRACKA:
755 case HDN_ENDTRACKW: return HDN_ENDTRACKA;
756 case HDN_BEGINDRAG: return HDN_BEGINDRAG;
757 case HDN_ENDDRAG: return HDN_ENDDRAG;
758 case HDN_ITEMCHANGINGA:
759 case HDN_ITEMCHANGINGW: return HDN_ITEMCHANGINGA;
760 case HDN_ITEMCHANGEDA:
761 case HDN_ITEMCHANGEDW: return HDN_ITEMCHANGEDA;
762 case HDN_ITEMCLICKA:
763 case HDN_ITEMCLICKW: return HDN_ITEMCLICKA;
764 case HDN_DIVIDERDBLCLICKA:
765 case HDN_DIVIDERDBLCLICKW: return HDN_DIVIDERDBLCLICKA;
766 default: break;
768 FIXME("unknown notification %x\n", unicodeNotificationCode);
769 return unicodeNotificationCode;
772 /* forwards header notifications to listview parent */
773 static LRESULT notify_forward_header(const LISTVIEW_INFO *infoPtr, NMHEADERW *lpnmhW)
775 LPCWSTR text = NULL, filter = NULL;
776 LRESULT ret;
777 NMHEADERA *lpnmh = (NMHEADERA*) lpnmhW;
779 /* on unicode format exit earlier */
780 if (infoPtr->notifyFormat == NFR_UNICODE)
781 return SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, lpnmh->hdr.idFrom,
782 (LPARAM)lpnmh);
784 /* header always supplies unicode notifications,
785 all we have to do is to convert strings to ANSI */
786 if (lpnmh->pitem)
788 /* convert item text */
789 if (lpnmh->pitem->mask & HDI_TEXT)
791 text = (LPCWSTR)lpnmh->pitem->pszText;
792 lpnmh->pitem->pszText = NULL;
793 Str_SetPtrWtoA(&lpnmh->pitem->pszText, text);
795 /* convert filter text */
796 if ((lpnmh->pitem->mask & HDI_FILTER) && (lpnmh->pitem->type == HDFT_ISSTRING) &&
797 lpnmh->pitem->pvFilter)
799 filter = (LPCWSTR)((HD_TEXTFILTERA*)lpnmh->pitem->pvFilter)->pszText;
800 ((HD_TEXTFILTERA*)lpnmh->pitem->pvFilter)->pszText = NULL;
801 Str_SetPtrWtoA(&((HD_TEXTFILTERA*)lpnmh->pitem->pvFilter)->pszText, filter);
804 lpnmh->hdr.code = get_ansi_notification(lpnmh->hdr.code);
806 ret = SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, lpnmh->hdr.idFrom,
807 (LPARAM)lpnmh);
809 /* cleanup */
810 if(text)
812 Free(lpnmh->pitem->pszText);
813 lpnmh->pitem->pszText = (LPSTR)text;
815 if(filter)
817 Free(((HD_TEXTFILTERA*)lpnmh->pitem->pvFilter)->pszText);
818 ((HD_TEXTFILTERA*)lpnmh->pitem->pvFilter)->pszText = (LPSTR)filter;
821 return ret;
824 static LRESULT notify_hdr(const LISTVIEW_INFO *infoPtr, INT code, LPNMHDR pnmh)
826 LRESULT result;
828 TRACE("(code=%d)\n", code);
830 pnmh->hwndFrom = infoPtr->hwndSelf;
831 pnmh->idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
832 pnmh->code = code;
833 result = SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, pnmh->idFrom, (LPARAM)pnmh);
835 TRACE(" <= %ld\n", result);
837 return result;
840 static inline BOOL notify(const LISTVIEW_INFO *infoPtr, INT code)
842 NMHDR nmh;
843 HWND hwnd = infoPtr->hwndSelf;
844 notify_hdr(infoPtr, code, &nmh);
845 return IsWindow(hwnd);
848 static inline void notify_itemactivate(const LISTVIEW_INFO *infoPtr, const LVHITTESTINFO *htInfo)
850 NMITEMACTIVATE nmia;
851 LVITEMW item;
853 nmia.uNewState = 0;
854 nmia.uOldState = 0;
855 nmia.uChanged = 0;
856 nmia.uKeyFlags = 0;
858 item.mask = LVIF_PARAM|LVIF_STATE;
859 item.iItem = htInfo->iItem;
860 item.iSubItem = 0;
861 item.stateMask = (UINT)-1;
862 if (LISTVIEW_GetItemT(infoPtr, &item, TRUE)) {
863 nmia.lParam = item.lParam;
864 nmia.uOldState = item.state;
865 nmia.uNewState = item.state | LVIS_ACTIVATING;
866 nmia.uChanged = LVIF_STATE;
869 nmia.iItem = htInfo->iItem;
870 nmia.iSubItem = htInfo->iSubItem;
871 nmia.ptAction = htInfo->pt;
873 if (GetKeyState(VK_SHIFT) & 0x8000) nmia.uKeyFlags |= LVKF_SHIFT;
874 if (GetKeyState(VK_CONTROL) & 0x8000) nmia.uKeyFlags |= LVKF_CONTROL;
875 if (GetKeyState(VK_MENU) & 0x8000) nmia.uKeyFlags |= LVKF_ALT;
877 notify_hdr(infoPtr, LVN_ITEMACTIVATE, (LPNMHDR)&nmia);
880 static inline LRESULT notify_listview(const LISTVIEW_INFO *infoPtr, INT code, LPNMLISTVIEW plvnm)
882 TRACE("(code=%d, plvnm=%s)\n", code, debugnmlistview(plvnm));
883 return notify_hdr(infoPtr, code, (LPNMHDR)plvnm);
886 /* Handles NM_DBLCLK, NM_CLICK, NM_RDBLCLK, NM_RCLICK. Only NM_RCLICK return value is used. */
887 static BOOL notify_click(const LISTVIEW_INFO *infoPtr, INT code, const LVHITTESTINFO *lvht)
889 NMITEMACTIVATE nmia;
890 LVITEMW item;
891 HWND hwnd = infoPtr->hwndSelf;
892 LRESULT ret;
894 TRACE("code=%d, lvht=%s\n", code, debuglvhittestinfo(lvht));
895 ZeroMemory(&nmia, sizeof(nmia));
896 nmia.iItem = lvht->iItem;
897 nmia.iSubItem = lvht->iSubItem;
898 nmia.ptAction = lvht->pt;
899 item.mask = LVIF_PARAM;
900 item.iItem = lvht->iItem;
901 item.iSubItem = 0;
902 if (LISTVIEW_GetItemT(infoPtr, &item, TRUE)) nmia.lParam = item.lParam;
903 ret = notify_hdr(infoPtr, code, (NMHDR*)&nmia);
904 return IsWindow(hwnd) && (code == NM_RCLICK ? !ret : TRUE);
907 static BOOL notify_deleteitem(const LISTVIEW_INFO *infoPtr, INT nItem)
909 NMLISTVIEW nmlv;
910 LVITEMW item;
911 HWND hwnd = infoPtr->hwndSelf;
913 ZeroMemory(&nmlv, sizeof (NMLISTVIEW));
914 nmlv.iItem = nItem;
915 item.mask = LVIF_PARAM;
916 item.iItem = nItem;
917 item.iSubItem = 0;
918 if (LISTVIEW_GetItemT(infoPtr, &item, TRUE)) nmlv.lParam = item.lParam;
919 notify_listview(infoPtr, LVN_DELETEITEM, &nmlv);
920 return IsWindow(hwnd);
924 Send notification. depends on dispinfoW having same
925 structure as dispinfoA.
926 infoPtr : listview struct
927 code : *Unicode* notification code
928 pdi : dispinfo structure (can be unicode or ansi)
929 isW : TRUE if dispinfo is Unicode
931 static BOOL notify_dispinfoT(const LISTVIEW_INFO *infoPtr, UINT code, LPNMLVDISPINFOW pdi, BOOL isW)
933 INT length = 0, ret_length;
934 LPWSTR buffer = NULL, ret_text;
935 BOOL return_ansi = FALSE;
936 BOOL return_unicode = FALSE;
937 BOOL ret;
939 if ((pdi->item.mask & LVIF_TEXT) && is_text(pdi->item.pszText))
941 return_unicode = ( isW && infoPtr->notifyFormat == NFR_ANSI);
942 return_ansi = (!isW && infoPtr->notifyFormat == NFR_UNICODE);
945 ret_length = pdi->item.cchTextMax;
946 ret_text = pdi->item.pszText;
948 if (return_unicode || return_ansi)
950 if (code != LVN_GETDISPINFOW)
952 length = return_ansi ?
953 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pdi->item.pszText, -1, NULL, 0):
954 WideCharToMultiByte(CP_ACP, 0, pdi->item.pszText, -1, NULL, 0, NULL, NULL);
956 else
958 length = pdi->item.cchTextMax;
959 *pdi->item.pszText = 0; /* make sure we don't process garbage */
962 buffer = Alloc( (return_ansi ? sizeof(WCHAR) : sizeof(CHAR)) * length);
963 if (!buffer) return FALSE;
965 if (return_ansi)
966 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pdi->item.pszText, -1,
967 buffer, length);
968 else
969 WideCharToMultiByte(CP_ACP, 0, pdi->item.pszText, -1, (LPSTR) buffer,
970 length, NULL, NULL);
972 pdi->item.pszText = buffer;
973 pdi->item.cchTextMax = length;
976 if (infoPtr->notifyFormat == NFR_ANSI)
977 code = get_ansi_notification(code);
979 TRACE(" pdi->item=%s\n", debuglvitem_t(&pdi->item, infoPtr->notifyFormat != NFR_ANSI));
980 ret = notify_hdr(infoPtr, code, &pdi->hdr);
981 TRACE(" resulting code=%d\n", pdi->hdr.code);
983 if (return_ansi || return_unicode)
985 if (return_ansi && (pdi->hdr.code == LVN_GETDISPINFOA))
987 strcpy((char*)ret_text, (char*)pdi->item.pszText);
989 else if (return_unicode && (pdi->hdr.code == LVN_GETDISPINFOW))
991 lstrcpyW(ret_text, pdi->item.pszText);
993 else if (return_ansi) /* note : pointer can be changed by app ! */
995 WideCharToMultiByte(CP_ACP, 0, pdi->item.pszText, -1, (LPSTR) ret_text,
996 ret_length, NULL, NULL);
998 else
999 MultiByteToWideChar(CP_ACP, 0, (LPSTR) pdi->item.pszText, -1,
1000 ret_text, ret_length);
1002 pdi->item.pszText = ret_text; /* restores our buffer */
1003 pdi->item.cchTextMax = ret_length;
1005 Free(buffer);
1006 return ret;
1009 /* if dispinfo holder changed notification code then convert */
1010 if (!isW && (pdi->hdr.code == LVN_GETDISPINFOW) && (pdi->item.mask & LVIF_TEXT))
1012 length = WideCharToMultiByte(CP_ACP, 0, pdi->item.pszText, -1, NULL, 0, NULL, NULL);
1014 buffer = Alloc(length * sizeof(CHAR));
1015 if (!buffer) return FALSE;
1017 WideCharToMultiByte(CP_ACP, 0, pdi->item.pszText, -1, (LPSTR) buffer,
1018 ret_length, NULL, NULL);
1020 strcpy((LPSTR)pdi->item.pszText, (LPSTR)buffer);
1021 Free(buffer);
1024 return ret;
1027 static void customdraw_fill(NMLVCUSTOMDRAW *lpnmlvcd, const LISTVIEW_INFO *infoPtr, HDC hdc,
1028 const RECT *rcBounds, const LVITEMW *lplvItem)
1030 ZeroMemory(lpnmlvcd, sizeof(NMLVCUSTOMDRAW));
1031 lpnmlvcd->nmcd.hdc = hdc;
1032 lpnmlvcd->nmcd.rc = *rcBounds;
1033 lpnmlvcd->clrTextBk = infoPtr->clrTextBk;
1034 lpnmlvcd->clrText = infoPtr->clrText;
1035 if (!lplvItem) return;
1036 lpnmlvcd->nmcd.dwItemSpec = lplvItem->iItem + 1;
1037 lpnmlvcd->iSubItem = lplvItem->iSubItem;
1038 if (lplvItem->state & LVIS_SELECTED) lpnmlvcd->nmcd.uItemState |= CDIS_SELECTED;
1039 if (lplvItem->state & LVIS_FOCUSED) lpnmlvcd->nmcd.uItemState |= CDIS_FOCUS;
1040 if (lplvItem->iItem == infoPtr->nHotItem) lpnmlvcd->nmcd.uItemState |= CDIS_HOT;
1041 lpnmlvcd->nmcd.lItemlParam = lplvItem->lParam;
1044 static inline DWORD notify_customdraw (const LISTVIEW_INFO *infoPtr, DWORD dwDrawStage, NMLVCUSTOMDRAW *lpnmlvcd)
1046 BOOL isForItem = (lpnmlvcd->nmcd.dwItemSpec != 0);
1047 DWORD result;
1049 lpnmlvcd->nmcd.dwDrawStage = dwDrawStage;
1050 if (isForItem) lpnmlvcd->nmcd.dwDrawStage |= CDDS_ITEM;
1051 if (lpnmlvcd->iSubItem) lpnmlvcd->nmcd.dwDrawStage |= CDDS_SUBITEM;
1052 if (isForItem) lpnmlvcd->nmcd.dwItemSpec--;
1053 result = notify_hdr(infoPtr, NM_CUSTOMDRAW, &lpnmlvcd->nmcd.hdr);
1054 if (isForItem) lpnmlvcd->nmcd.dwItemSpec++;
1055 return result;
1058 static void prepaint_setup (const LISTVIEW_INFO *infoPtr, HDC hdc, const NMLVCUSTOMDRAW *cd, BOOL SubItem)
1060 COLORREF backcolor, textcolor;
1062 backcolor = cd->clrTextBk;
1063 textcolor = cd->clrText;
1065 /* apparently, for selected items, we have to override the returned values */
1066 if (!SubItem)
1068 if (cd->nmcd.uItemState & CDIS_SELECTED)
1070 if (infoPtr->bFocus)
1072 backcolor = comctl32_color.clrHighlight;
1073 textcolor = comctl32_color.clrHighlightText;
1075 else if (infoPtr->dwStyle & LVS_SHOWSELALWAYS)
1077 backcolor = comctl32_color.clr3dFace;
1078 textcolor = comctl32_color.clrBtnText;
1083 if (backcolor == CLR_DEFAULT)
1084 backcolor = comctl32_color.clrWindow;
1085 if (textcolor == CLR_DEFAULT)
1086 textcolor = comctl32_color.clrWindowText;
1088 /* Set the text attributes */
1089 if (backcolor != CLR_NONE)
1091 SetBkMode(hdc, OPAQUE);
1092 SetBkColor(hdc, backcolor);
1094 else
1095 SetBkMode(hdc, TRANSPARENT);
1096 SetTextColor(hdc, textcolor);
1099 static inline DWORD notify_postpaint (const LISTVIEW_INFO *infoPtr, NMLVCUSTOMDRAW *lpnmlvcd)
1101 return notify_customdraw(infoPtr, CDDS_POSTPAINT, lpnmlvcd);
1104 /* returns TRUE when repaint needed, FALSE otherwise */
1105 static BOOL notify_measureitem(LISTVIEW_INFO *infoPtr)
1107 MEASUREITEMSTRUCT mis;
1108 mis.CtlType = ODT_LISTVIEW;
1109 mis.CtlID = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
1110 mis.itemID = -1;
1111 mis.itemWidth = 0;
1112 mis.itemData = 0;
1113 mis.itemHeight= infoPtr->nItemHeight;
1114 SendMessageW(infoPtr->hwndNotify, WM_MEASUREITEM, mis.CtlID, (LPARAM)&mis);
1115 if (infoPtr->nItemHeight != max(mis.itemHeight, 1))
1117 infoPtr->nMeasureItemHeight = infoPtr->nItemHeight = max(mis.itemHeight, 1);
1118 return TRUE;
1120 return FALSE;
1123 /******** Item iterator functions **********************************/
1125 static RANGES ranges_create(int count);
1126 static void ranges_destroy(RANGES ranges);
1127 static BOOL ranges_add(RANGES ranges, RANGE range);
1128 static BOOL ranges_del(RANGES ranges, RANGE range);
1129 static void ranges_dump(RANGES ranges);
1131 static inline BOOL ranges_additem(RANGES ranges, INT nItem)
1133 RANGE range = { nItem, nItem + 1 };
1135 return ranges_add(ranges, range);
1138 static inline BOOL ranges_delitem(RANGES ranges, INT nItem)
1140 RANGE range = { nItem, nItem + 1 };
1142 return ranges_del(ranges, range);
1145 /***
1146 * ITERATOR DOCUMENTATION
1148 * The iterator functions allow for easy, and convenient iteration
1149 * over items of interest in the list. Typically, you create an
1150 * iterator, use it, and destroy it, as such:
1151 * ITERATOR i;
1153 * iterator_xxxitems(&i, ...);
1154 * while (iterator_{prev,next}(&i)
1156 * //code which uses i.nItem
1158 * iterator_destroy(&i);
1160 * where xxx is either: framed, or visible.
1161 * Note that it is important that the code destroys the iterator
1162 * after it's done with it, as the creation of the iterator may
1163 * allocate memory, which thus needs to be freed.
1165 * You can iterate both forwards, and backwards through the list,
1166 * by using iterator_next or iterator_prev respectively.
1168 * Lower numbered items are draw on top of higher number items in
1169 * LVS_ICON, and LVS_SMALLICON (which are the only modes where
1170 * items may overlap). So, to test items, you should use
1171 * iterator_next
1172 * which lists the items top to bottom (in Z-order).
1173 * For drawing items, you should use
1174 * iterator_prev
1175 * which lists the items bottom to top (in Z-order).
1176 * If you keep iterating over the items after the end-of-items
1177 * marker (-1) is returned, the iterator will start from the
1178 * beginning. Typically, you don't need to test for -1,
1179 * because iterator_{next,prev} will return TRUE if more items
1180 * are to be iterated over, or FALSE otherwise.
1182 * Note: the iterator is defined to be bidirectional. That is,
1183 * any number of prev followed by any number of next, or
1184 * five versa, should leave the iterator at the same item:
1185 * prev * n, next * n = next * n, prev * n
1187 * The iterator has a notion of an out-of-order, special item,
1188 * which sits at the start of the list. This is used in
1189 * LVS_ICON, and LVS_SMALLICON mode to handle the focused item,
1190 * which needs to be first, as it may overlap other items.
1192 * The code is a bit messy because we have:
1193 * - a special item to deal with
1194 * - simple range, or composite range
1195 * - empty range.
1196 * If you find bugs, or want to add features, please make sure you
1197 * always check/modify *both* iterator_prev, and iterator_next.
1200 /****
1201 * This function iterates through the items in increasing order,
1202 * but prefixed by the special item, then -1. That is:
1203 * special, 1, 2, 3, ..., n, -1.
1204 * Each item is listed only once.
1206 static inline BOOL iterator_next(ITERATOR* i)
1208 if (i->nItem == -1)
1210 i->nItem = i->nSpecial;
1211 if (i->nItem != -1) return TRUE;
1213 if (i->nItem == i->nSpecial)
1215 if (i->ranges) i->index = 0;
1216 goto pickarange;
1219 i->nItem++;
1220 testitem:
1221 if (i->nItem == i->nSpecial) i->nItem++;
1222 if (i->nItem < i->range.upper) return TRUE;
1224 pickarange:
1225 if (i->ranges)
1227 if (i->index < DPA_GetPtrCount(i->ranges->hdpa))
1228 i->range = *(RANGE*)DPA_GetPtr(i->ranges->hdpa, i->index++);
1229 else goto end;
1231 else if (i->nItem >= i->range.upper) goto end;
1233 i->nItem = i->range.lower;
1234 if (i->nItem >= 0) goto testitem;
1235 end:
1236 i->nItem = -1;
1237 return FALSE;
1240 /****
1241 * This function iterates through the items in decreasing order,
1242 * followed by the special item, then -1. That is:
1243 * n, n-1, ..., 3, 2, 1, special, -1.
1244 * Each item is listed only once.
1246 static inline BOOL iterator_prev(ITERATOR* i)
1248 BOOL start = FALSE;
1250 if (i->nItem == -1)
1252 start = TRUE;
1253 if (i->ranges) i->index = DPA_GetPtrCount(i->ranges->hdpa);
1254 goto pickarange;
1256 if (i->nItem == i->nSpecial)
1258 i->nItem = -1;
1259 return FALSE;
1262 testitem:
1263 i->nItem--;
1264 if (i->nItem == i->nSpecial) i->nItem--;
1265 if (i->nItem >= i->range.lower) return TRUE;
1267 pickarange:
1268 if (i->ranges)
1270 if (i->index > 0)
1271 i->range = *(RANGE*)DPA_GetPtr(i->ranges->hdpa, --i->index);
1272 else goto end;
1274 else if (!start && i->nItem < i->range.lower) goto end;
1276 i->nItem = i->range.upper;
1277 if (i->nItem > 0) goto testitem;
1278 end:
1279 return (i->nItem = i->nSpecial) != -1;
1282 static RANGE iterator_range(const ITERATOR *i)
1284 RANGE range;
1286 if (!i->ranges) return i->range;
1288 if (DPA_GetPtrCount(i->ranges->hdpa) > 0)
1290 range.lower = (*(RANGE*)DPA_GetPtr(i->ranges->hdpa, 0)).lower;
1291 range.upper = (*(RANGE*)DPA_GetPtr(i->ranges->hdpa, DPA_GetPtrCount(i->ranges->hdpa) - 1)).upper;
1293 else range.lower = range.upper = 0;
1295 return range;
1298 /***
1299 * Releases resources associated with this iterator.
1301 static inline void iterator_destroy(const ITERATOR *i)
1303 ranges_destroy(i->ranges);
1306 /***
1307 * Create an empty iterator.
1309 static inline void iterator_empty(ITERATOR* i)
1311 ZeroMemory(i, sizeof(*i));
1312 i->nItem = i->nSpecial = i->range.lower = i->range.upper = -1;
1315 /***
1316 * Create an iterator over a range.
1318 static inline void iterator_rangeitems(ITERATOR* i, RANGE range)
1320 iterator_empty(i);
1321 i->range = range;
1324 /***
1325 * Create an iterator over a bunch of ranges.
1326 * Please note that the iterator will take ownership of the ranges,
1327 * and will free them upon destruction.
1329 static inline void iterator_rangesitems(ITERATOR* i, RANGES ranges)
1331 iterator_empty(i);
1332 i->ranges = ranges;
1335 /***
1336 * Creates an iterator over the items which intersect frame.
1337 * Uses absolute coordinates rather than compensating for the current offset.
1339 static BOOL iterator_frameditems_absolute(ITERATOR* i, const LISTVIEW_INFO* infoPtr, const RECT *frame)
1341 RECT rcItem, rcTemp;
1342 RANGES ranges;
1344 TRACE("(frame=%s)\n", wine_dbgstr_rect(frame));
1346 /* in case we fail, we want to return an empty iterator */
1347 iterator_empty(i);
1349 if (infoPtr->nItemCount == 0)
1350 return TRUE;
1352 if (infoPtr->uView == LV_VIEW_ICON || infoPtr->uView == LV_VIEW_SMALLICON)
1354 INT nItem;
1356 if (infoPtr->uView == LV_VIEW_ICON && infoPtr->nFocusedItem != -1)
1358 LISTVIEW_GetItemBox(infoPtr, infoPtr->nFocusedItem, &rcItem);
1359 if (IntersectRect(&rcTemp, &rcItem, frame))
1360 i->nSpecial = infoPtr->nFocusedItem;
1362 if (!(ranges = ranges_create(50))) return FALSE;
1363 iterator_rangesitems(i, ranges);
1364 /* to do better here, we need to have PosX, and PosY sorted */
1365 TRACE("building icon ranges:\n");
1366 for (nItem = 0; nItem < infoPtr->nItemCount; nItem++)
1368 rcItem.left = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosX, nItem);
1369 rcItem.top = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosY, nItem);
1370 rcItem.right = rcItem.left + infoPtr->nItemWidth;
1371 rcItem.bottom = rcItem.top + infoPtr->nItemHeight;
1372 if (IntersectRect(&rcTemp, &rcItem, frame))
1373 ranges_additem(i->ranges, nItem);
1375 return TRUE;
1377 else if (infoPtr->uView == LV_VIEW_DETAILS)
1379 RANGE range;
1381 if (frame->left >= infoPtr->nItemWidth) return TRUE;
1382 if (frame->top >= infoPtr->nItemHeight * infoPtr->nItemCount) return TRUE;
1384 range.lower = max(frame->top / infoPtr->nItemHeight, 0);
1385 range.upper = min((frame->bottom - 1) / infoPtr->nItemHeight, infoPtr->nItemCount - 1) + 1;
1386 if (range.upper <= range.lower) return TRUE;
1387 iterator_rangeitems(i, range);
1388 TRACE(" report=%s\n", debugrange(&i->range));
1390 else
1392 INT nPerCol = max((infoPtr->rcList.bottom - infoPtr->rcList.top) / infoPtr->nItemHeight, 1);
1393 INT nFirstRow = max(frame->top / infoPtr->nItemHeight, 0);
1394 INT nLastRow = min((frame->bottom - 1) / infoPtr->nItemHeight, nPerCol - 1);
1395 INT nFirstCol;
1396 INT nLastCol;
1397 INT lower;
1398 RANGE item_range;
1399 INT nCol;
1401 if (infoPtr->nItemWidth)
1403 nFirstCol = max(frame->left / infoPtr->nItemWidth, 0);
1404 nLastCol = min((frame->right - 1) / infoPtr->nItemWidth, (infoPtr->nItemCount + nPerCol - 1) / nPerCol);
1406 else
1408 nFirstCol = max(frame->left, 0);
1409 nLastCol = min(frame->right - 1, (infoPtr->nItemCount + nPerCol - 1) / nPerCol);
1412 lower = nFirstCol * nPerCol + nFirstRow;
1414 TRACE("nPerCol=%d, nFirstRow=%d, nLastRow=%d, nFirstCol=%d, nLastCol=%d, lower=%d\n",
1415 nPerCol, nFirstRow, nLastRow, nFirstCol, nLastCol, lower);
1417 if (nLastCol < nFirstCol || nLastRow < nFirstRow) return TRUE;
1419 if (!(ranges = ranges_create(nLastCol - nFirstCol + 1))) return FALSE;
1420 iterator_rangesitems(i, ranges);
1421 TRACE("building list ranges:\n");
1422 for (nCol = nFirstCol; nCol <= nLastCol; nCol++)
1424 item_range.lower = nCol * nPerCol + nFirstRow;
1425 if(item_range.lower >= infoPtr->nItemCount) break;
1426 item_range.upper = min(nCol * nPerCol + nLastRow + 1, infoPtr->nItemCount);
1427 TRACE(" list=%s\n", debugrange(&item_range));
1428 ranges_add(i->ranges, item_range);
1432 return TRUE;
1435 /***
1436 * Creates an iterator over the items which intersect lprc.
1438 static BOOL iterator_frameditems(ITERATOR* i, const LISTVIEW_INFO* infoPtr, const RECT *lprc)
1440 RECT frame = *lprc;
1441 POINT Origin;
1443 TRACE("(lprc=%s)\n", wine_dbgstr_rect(lprc));
1445 LISTVIEW_GetOrigin(infoPtr, &Origin);
1446 OffsetRect(&frame, -Origin.x, -Origin.y);
1448 return iterator_frameditems_absolute(i, infoPtr, &frame);
1451 /***
1452 * Creates an iterator over the items which intersect the visible region of hdc.
1454 static BOOL iterator_visibleitems(ITERATOR *i, const LISTVIEW_INFO *infoPtr, HDC hdc)
1456 POINT Origin, Position;
1457 RECT rcItem, rcClip;
1458 INT rgntype;
1460 rgntype = GetClipBox(hdc, &rcClip);
1461 if (rgntype == NULLREGION)
1463 iterator_empty(i);
1464 return TRUE;
1466 if (!iterator_frameditems(i, infoPtr, &rcClip)) return FALSE;
1467 if (rgntype == SIMPLEREGION) return TRUE;
1469 /* first deal with the special item */
1470 if (i->nSpecial != -1)
1472 LISTVIEW_GetItemBox(infoPtr, i->nSpecial, &rcItem);
1473 if (!RectVisible(hdc, &rcItem)) i->nSpecial = -1;
1476 /* if we can't deal with the region, we'll just go with the simple range */
1477 LISTVIEW_GetOrigin(infoPtr, &Origin);
1478 TRACE("building visible range:\n");
1479 if (!i->ranges && i->range.lower < i->range.upper)
1481 if (!(i->ranges = ranges_create(50))) return TRUE;
1482 if (!ranges_add(i->ranges, i->range))
1484 ranges_destroy(i->ranges);
1485 i->ranges = 0;
1486 return TRUE;
1490 /* now delete the invisible items from the list */
1491 while(iterator_next(i))
1493 LISTVIEW_GetItemOrigin(infoPtr, i->nItem, &Position);
1494 rcItem.left = (infoPtr->uView == LV_VIEW_DETAILS) ? Origin.x : Position.x + Origin.x;
1495 rcItem.top = Position.y + Origin.y;
1496 rcItem.right = rcItem.left + infoPtr->nItemWidth;
1497 rcItem.bottom = rcItem.top + infoPtr->nItemHeight;
1498 if (!RectVisible(hdc, &rcItem))
1499 ranges_delitem(i->ranges, i->nItem);
1501 /* the iterator should restart on the next iterator_next */
1502 TRACE("done\n");
1504 return TRUE;
1507 /* Remove common elements from two iterators */
1508 /* Passed iterators have to point on the first elements */
1509 static BOOL iterator_remove_common_items(ITERATOR *iter1, ITERATOR *iter2)
1511 if(!iter1->ranges || !iter2->ranges) {
1512 int lower, upper;
1514 if(iter1->ranges || iter2->ranges ||
1515 (iter1->range.lower<iter2->range.lower && iter1->range.upper>iter2->range.upper) ||
1516 (iter1->range.lower>iter2->range.lower && iter1->range.upper<iter2->range.upper)) {
1517 ERR("result is not a one range iterator\n");
1518 return FALSE;
1521 if(iter1->range.lower==-1 || iter2->range.lower==-1)
1522 return TRUE;
1524 lower = iter1->range.lower;
1525 upper = iter1->range.upper;
1527 if(lower < iter2->range.lower)
1528 iter1->range.upper = iter2->range.lower;
1529 else if(upper > iter2->range.upper)
1530 iter1->range.lower = iter2->range.upper;
1531 else
1532 iter1->range.lower = iter1->range.upper = -1;
1534 if(iter2->range.lower < lower)
1535 iter2->range.upper = lower;
1536 else if(iter2->range.upper > upper)
1537 iter2->range.lower = upper;
1538 else
1539 iter2->range.lower = iter2->range.upper = -1;
1541 return TRUE;
1544 iterator_next(iter1);
1545 iterator_next(iter2);
1547 while(1) {
1548 if(iter1->nItem==-1 || iter2->nItem==-1)
1549 break;
1551 if(iter1->nItem == iter2->nItem) {
1552 int delete = iter1->nItem;
1554 iterator_prev(iter1);
1555 iterator_prev(iter2);
1556 ranges_delitem(iter1->ranges, delete);
1557 ranges_delitem(iter2->ranges, delete);
1558 iterator_next(iter1);
1559 iterator_next(iter2);
1560 } else if(iter1->nItem > iter2->nItem)
1561 iterator_next(iter2);
1562 else
1563 iterator_next(iter1);
1566 iter1->nItem = iter1->range.lower = iter1->range.upper = -1;
1567 iter2->nItem = iter2->range.lower = iter2->range.upper = -1;
1568 return TRUE;
1571 /******** Misc helper functions ************************************/
1573 static inline LRESULT CallWindowProcT(WNDPROC proc, HWND hwnd, UINT uMsg,
1574 WPARAM wParam, LPARAM lParam, BOOL isW)
1576 if (isW) return CallWindowProcW(proc, hwnd, uMsg, wParam, lParam);
1577 else return CallWindowProcA(proc, hwnd, uMsg, wParam, lParam);
1580 static inline BOOL is_autoarrange(const LISTVIEW_INFO *infoPtr)
1582 return (infoPtr->dwStyle & LVS_AUTOARRANGE) &&
1583 (infoPtr->uView == LV_VIEW_ICON || infoPtr->uView == LV_VIEW_SMALLICON);
1586 static void toggle_checkbox_state(LISTVIEW_INFO *infoPtr, INT nItem)
1588 DWORD state = STATEIMAGEINDEX(LISTVIEW_GetItemState(infoPtr, nItem, LVIS_STATEIMAGEMASK));
1589 if(state == 1 || state == 2)
1591 LVITEMW lvitem;
1592 state ^= 3;
1593 lvitem.state = INDEXTOSTATEIMAGEMASK(state);
1594 lvitem.stateMask = LVIS_STATEIMAGEMASK;
1595 LISTVIEW_SetItemState(infoPtr, nItem, &lvitem);
1599 /* this should be called after window style got updated,
1600 it used to reset view state to match current window style */
1601 static inline void map_style_view(LISTVIEW_INFO *infoPtr)
1603 switch (infoPtr->dwStyle & LVS_TYPEMASK)
1605 case LVS_ICON:
1606 infoPtr->uView = LV_VIEW_ICON;
1607 break;
1608 case LVS_REPORT:
1609 infoPtr->uView = LV_VIEW_DETAILS;
1610 break;
1611 case LVS_SMALLICON:
1612 infoPtr->uView = LV_VIEW_SMALLICON;
1613 break;
1614 case LVS_LIST:
1615 infoPtr->uView = LV_VIEW_LIST;
1619 /* computes next item id value */
1620 static DWORD get_next_itemid(const LISTVIEW_INFO *infoPtr)
1622 INT count = DPA_GetPtrCount(infoPtr->hdpaItemIds);
1624 if (count > 0)
1626 ITEM_ID *lpID = DPA_GetPtr(infoPtr->hdpaItemIds, count - 1);
1627 return lpID->id + 1;
1629 return 0;
1632 /******** Internal API functions ************************************/
1634 static inline COLUMN_INFO * LISTVIEW_GetColumnInfo(const LISTVIEW_INFO *infoPtr, INT nSubItem)
1636 static COLUMN_INFO mainItem;
1638 if (nSubItem == 0 && DPA_GetPtrCount(infoPtr->hdpaColumns) == 0) return &mainItem;
1639 assert (nSubItem >= 0 && nSubItem < DPA_GetPtrCount(infoPtr->hdpaColumns));
1641 /* update cached column rectangles */
1642 if (infoPtr->colRectsDirty)
1644 COLUMN_INFO *info;
1645 LISTVIEW_INFO *Ptr = (LISTVIEW_INFO*)infoPtr;
1646 INT i;
1648 for (i = 0; i < DPA_GetPtrCount(infoPtr->hdpaColumns); i++) {
1649 info = DPA_GetPtr(infoPtr->hdpaColumns, i);
1650 SendMessageW(infoPtr->hwndHeader, HDM_GETITEMRECT, i, (LPARAM)&info->rcHeader);
1652 Ptr->colRectsDirty = FALSE;
1655 return DPA_GetPtr(infoPtr->hdpaColumns, nSubItem);
1658 static INT LISTVIEW_CreateHeader(LISTVIEW_INFO *infoPtr)
1660 DWORD dFlags = WS_CHILD | HDS_HORZ | HDS_FULLDRAG | HDS_DRAGDROP;
1661 HINSTANCE hInst;
1663 if (infoPtr->hwndHeader) return 0;
1665 TRACE("Creating header for list %p\n", infoPtr->hwndSelf);
1667 /* setup creation flags */
1668 dFlags |= (LVS_NOSORTHEADER & infoPtr->dwStyle) ? 0 : HDS_BUTTONS;
1669 dFlags |= (LVS_NOCOLUMNHEADER & infoPtr->dwStyle) ? HDS_HIDDEN : 0;
1671 hInst = (HINSTANCE)GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_HINSTANCE);
1673 /* create header */
1674 infoPtr->hwndHeader = CreateWindowW(WC_HEADERW, NULL, dFlags,
1675 0, 0, 0, 0, infoPtr->hwndSelf, NULL, hInst, NULL);
1676 if (!infoPtr->hwndHeader) return -1;
1678 /* set header unicode format */
1679 SendMessageW(infoPtr->hwndHeader, HDM_SETUNICODEFORMAT, TRUE, 0);
1681 /* set header font */
1682 SendMessageW(infoPtr->hwndHeader, WM_SETFONT, (WPARAM)infoPtr->hFont, TRUE);
1684 /* set header image list */
1685 if (infoPtr->himlSmall)
1686 SendMessageW(infoPtr->hwndHeader, HDM_SETIMAGELIST, 0, (LPARAM)infoPtr->himlSmall);
1688 LISTVIEW_UpdateSize(infoPtr);
1690 return 0;
1693 static inline void LISTVIEW_GetHeaderRect(const LISTVIEW_INFO *infoPtr, INT nSubItem, LPRECT lprc)
1695 *lprc = LISTVIEW_GetColumnInfo(infoPtr, nSubItem)->rcHeader;
1698 static inline BOOL LISTVIEW_IsHeaderEnabled(const LISTVIEW_INFO *infoPtr)
1700 return (infoPtr->uView == LV_VIEW_DETAILS ||
1701 infoPtr->dwLvExStyle & LVS_EX_HEADERINALLVIEWS) &&
1702 !(infoPtr->dwStyle & LVS_NOCOLUMNHEADER);
1705 static inline BOOL LISTVIEW_GetItemW(const LISTVIEW_INFO *infoPtr, LPLVITEMW lpLVItem)
1707 return LISTVIEW_GetItemT(infoPtr, lpLVItem, TRUE);
1710 /* used to handle collapse main item column case */
1711 static inline BOOL LISTVIEW_DrawFocusRect(const LISTVIEW_INFO *infoPtr, HDC hdc)
1713 return (infoPtr->rcFocus.left < infoPtr->rcFocus.right) ?
1714 DrawFocusRect(hdc, &infoPtr->rcFocus) : FALSE;
1717 /* Listview invalidation functions: use _only_ these functions to invalidate */
1719 static inline BOOL is_redrawing(const LISTVIEW_INFO *infoPtr)
1721 return infoPtr->redraw;
1724 static inline void LISTVIEW_InvalidateRect(const LISTVIEW_INFO *infoPtr, const RECT* rect)
1726 if(!is_redrawing(infoPtr)) return;
1727 TRACE(" invalidating rect=%s\n", wine_dbgstr_rect(rect));
1728 InvalidateRect(infoPtr->hwndSelf, rect, TRUE);
1731 static inline void LISTVIEW_InvalidateItem(const LISTVIEW_INFO *infoPtr, INT nItem)
1733 RECT rcBox;
1735 if (!is_redrawing(infoPtr) || nItem < 0 || nItem >= infoPtr->nItemCount)
1736 return;
1738 LISTVIEW_GetItemBox(infoPtr, nItem, &rcBox);
1739 LISTVIEW_InvalidateRect(infoPtr, &rcBox);
1742 static inline void LISTVIEW_InvalidateSubItem(const LISTVIEW_INFO *infoPtr, INT nItem, INT nSubItem)
1744 POINT Origin, Position;
1745 RECT rcBox;
1747 if(!is_redrawing(infoPtr)) return;
1748 assert (infoPtr->uView == LV_VIEW_DETAILS);
1749 LISTVIEW_GetOrigin(infoPtr, &Origin);
1750 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position);
1751 LISTVIEW_GetHeaderRect(infoPtr, nSubItem, &rcBox);
1752 rcBox.top = 0;
1753 rcBox.bottom = infoPtr->nItemHeight;
1754 OffsetRect(&rcBox, Origin.x, Origin.y + Position.y);
1755 LISTVIEW_InvalidateRect(infoPtr, &rcBox);
1758 static inline void LISTVIEW_InvalidateList(const LISTVIEW_INFO *infoPtr)
1760 LISTVIEW_InvalidateRect(infoPtr, NULL);
1763 static inline void LISTVIEW_InvalidateColumn(const LISTVIEW_INFO *infoPtr, INT nColumn)
1765 RECT rcCol;
1767 if(!is_redrawing(infoPtr)) return;
1768 LISTVIEW_GetHeaderRect(infoPtr, nColumn, &rcCol);
1769 rcCol.top = infoPtr->rcList.top;
1770 rcCol.bottom = infoPtr->rcList.bottom;
1771 LISTVIEW_InvalidateRect(infoPtr, &rcCol);
1774 /***
1775 * DESCRIPTION:
1776 * Retrieves the number of items that can fit vertically in the client area.
1778 * PARAMETER(S):
1779 * [I] infoPtr : valid pointer to the listview structure
1781 * RETURN:
1782 * Number of items per row.
1784 static inline INT LISTVIEW_GetCountPerRow(const LISTVIEW_INFO *infoPtr)
1786 INT nListWidth = infoPtr->rcList.right - infoPtr->rcList.left;
1788 return max(nListWidth/(infoPtr->nItemWidth ? infoPtr->nItemWidth : 1), 1);
1791 /***
1792 * DESCRIPTION:
1793 * Retrieves the number of items that can fit horizontally in the client
1794 * area.
1796 * PARAMETER(S):
1797 * [I] infoPtr : valid pointer to the listview structure
1799 * RETURN:
1800 * Number of items per column.
1802 static inline INT LISTVIEW_GetCountPerColumn(const LISTVIEW_INFO *infoPtr)
1804 INT nListHeight = infoPtr->rcList.bottom - infoPtr->rcList.top;
1806 return infoPtr->nItemHeight ? max(nListHeight / infoPtr->nItemHeight, 1) : 0;
1810 /*************************************************************************
1811 * LISTVIEW_ProcessLetterKeys
1813 * Processes keyboard messages generated by pressing the letter keys
1814 * on the keyboard.
1815 * What this does is perform a case insensitive search from the
1816 * current position with the following quirks:
1817 * - If two chars or more are pressed in quick succession we search
1818 * for the corresponding string (e.g. 'abc').
1819 * - If there is a delay we wipe away the current search string and
1820 * restart with just that char.
1821 * - If the user keeps pressing the same character, whether slowly or
1822 * fast, so that the search string is entirely composed of this
1823 * character ('aaaaa' for instance), then we search for first item
1824 * that starting with that character.
1825 * - If the user types the above character in quick succession, then
1826 * we must also search for the corresponding string ('aaaaa'), and
1827 * go to that string if there is a match.
1829 * PARAMETERS
1830 * [I] hwnd : handle to the window
1831 * [I] charCode : the character code, the actual character
1832 * [I] keyData : key data
1834 * RETURNS
1836 * Zero.
1838 * BUGS
1840 * - The current implementation has a list of characters it will
1841 * accept and it ignores everything else. In particular it will
1842 * ignore accentuated characters which seems to match what
1843 * Windows does. But I'm not sure it makes sense to follow
1844 * Windows there.
1845 * - We don't sound a beep when the search fails.
1847 * SEE ALSO
1849 * TREEVIEW_ProcessLetterKeys
1851 static INT LISTVIEW_ProcessLetterKeys(LISTVIEW_INFO *infoPtr, WPARAM charCode, LPARAM keyData)
1853 WCHAR buffer[MAX_PATH];
1854 DWORD prevTime;
1855 LVITEMW item;
1856 int startidx;
1857 INT nItem;
1858 INT diff;
1860 /* simple parameter checking */
1861 if (!charCode || !keyData || infoPtr->nItemCount == 0) return 0;
1863 /* only allow the valid WM_CHARs through */
1864 if (!iswalnum(charCode) &&
1865 charCode != '.' && charCode != '`' && charCode != '!' &&
1866 charCode != '@' && charCode != '#' && charCode != '$' &&
1867 charCode != '%' && charCode != '^' && charCode != '&' &&
1868 charCode != '*' && charCode != '(' && charCode != ')' &&
1869 charCode != '-' && charCode != '_' && charCode != '+' &&
1870 charCode != '=' && charCode != '\\'&& charCode != ']' &&
1871 charCode != '}' && charCode != '[' && charCode != '{' &&
1872 charCode != '/' && charCode != '?' && charCode != '>' &&
1873 charCode != '<' && charCode != ',' && charCode != '~')
1874 return 0;
1876 /* update the search parameters */
1877 prevTime = infoPtr->lastKeyPressTimestamp;
1878 infoPtr->lastKeyPressTimestamp = GetTickCount();
1879 diff = infoPtr->lastKeyPressTimestamp - prevTime;
1881 if (diff >= 0 && diff < KEY_DELAY)
1883 if (infoPtr->nSearchParamLength < MAX_PATH - 1)
1884 infoPtr->szSearchParam[infoPtr->nSearchParamLength++] = charCode;
1886 if (infoPtr->charCode != charCode)
1887 infoPtr->charCode = charCode = 0;
1889 else
1891 infoPtr->charCode = charCode;
1892 infoPtr->szSearchParam[0] = charCode;
1893 infoPtr->nSearchParamLength = 1;
1896 /* should start from next after focused item, so next item that matches
1897 will be selected, if there isn't any and focused matches it will be selected
1898 on second search stage from beginning of the list */
1899 if (infoPtr->nFocusedItem >= 0 && infoPtr->nItemCount > 1)
1901 /* with some accumulated search data available start with current focus, otherwise
1902 it's excluded from search */
1903 startidx = infoPtr->nSearchParamLength > 1 ? infoPtr->nFocusedItem : infoPtr->nFocusedItem + 1;
1904 if (startidx == infoPtr->nItemCount) startidx = 0;
1906 else
1907 startidx = 0;
1909 /* let application handle this for virtual listview */
1910 if (infoPtr->dwStyle & LVS_OWNERDATA)
1912 NMLVFINDITEMW nmlv;
1914 memset(&nmlv.lvfi, 0, sizeof(nmlv.lvfi));
1915 nmlv.lvfi.flags = (LVFI_WRAP | LVFI_PARTIAL);
1916 nmlv.lvfi.psz = infoPtr->szSearchParam;
1917 nmlv.iStart = startidx;
1919 infoPtr->szSearchParam[infoPtr->nSearchParamLength] = 0;
1921 nItem = notify_hdr(infoPtr, LVN_ODFINDITEMW, (LPNMHDR)&nmlv.hdr);
1923 else
1925 int i = startidx, endidx;
1927 /* and search from the current position */
1928 nItem = -1;
1929 endidx = infoPtr->nItemCount;
1931 /* first search in [startidx, endidx), on failure continue in [0, startidx) */
1932 while (1)
1934 /* start from first item if not found with >= startidx */
1935 if (i == infoPtr->nItemCount && startidx > 0)
1937 endidx = startidx;
1938 startidx = 0;
1941 for (i = startidx; i < endidx; i++)
1943 /* retrieve text */
1944 item.mask = LVIF_TEXT;
1945 item.iItem = i;
1946 item.iSubItem = 0;
1947 item.pszText = buffer;
1948 item.cchTextMax = MAX_PATH;
1949 if (!LISTVIEW_GetItemW(infoPtr, &item)) return 0;
1951 if (!wcsnicmp(item.pszText, infoPtr->szSearchParam, infoPtr->nSearchParamLength))
1953 nItem = i;
1954 break;
1956 /* this is used to find first char match when search string is not available yet,
1957 otherwise every WM_CHAR will search to next item by first char, ignoring that we're
1958 already waiting for user to complete a string */
1959 else if (nItem == -1 && infoPtr->nSearchParamLength == 1 && !wcsnicmp(item.pszText, infoPtr->szSearchParam, 1))
1961 /* this would work but we must keep looking for a longer match */
1962 nItem = i;
1966 if ( nItem != -1 || /* found something */
1967 endidx != infoPtr->nItemCount || /* second search done */
1968 (startidx == 0 && endidx == infoPtr->nItemCount) /* full range for first search */ )
1969 break;
1973 if (nItem != -1)
1974 LISTVIEW_KeySelection(infoPtr, nItem, FALSE);
1976 return 0;
1979 /*************************************************************************
1980 * LISTVIEW_UpdateHeaderSize [Internal]
1982 * Function to resize the header control
1984 * PARAMS
1985 * [I] hwnd : handle to a window
1986 * [I] nNewScrollPos : scroll pos to set
1988 * RETURNS
1989 * None.
1991 static void LISTVIEW_UpdateHeaderSize(const LISTVIEW_INFO *infoPtr, INT nNewScrollPos)
1993 RECT winRect;
1994 POINT point[2];
1996 TRACE("nNewScrollPos=%d\n", nNewScrollPos);
1998 if (!infoPtr->hwndHeader) return;
2000 GetWindowRect(infoPtr->hwndHeader, &winRect);
2001 point[0].x = winRect.left;
2002 point[0].y = winRect.top;
2003 point[1].x = winRect.right;
2004 point[1].y = winRect.bottom;
2006 MapWindowPoints(HWND_DESKTOP, infoPtr->hwndSelf, point, 2);
2007 point[0].x = -nNewScrollPos;
2008 point[1].x += nNewScrollPos;
2010 SetWindowPos(infoPtr->hwndHeader,0,
2011 point[0].x,point[0].y,point[1].x,point[1].y,
2012 (infoPtr->dwStyle & LVS_NOCOLUMNHEADER) ? SWP_HIDEWINDOW : SWP_SHOWWINDOW |
2013 SWP_NOZORDER | SWP_NOACTIVATE);
2016 static INT LISTVIEW_UpdateHScroll(LISTVIEW_INFO *infoPtr)
2018 SCROLLINFO horzInfo;
2019 INT dx;
2021 ZeroMemory(&horzInfo, sizeof(SCROLLINFO));
2022 horzInfo.cbSize = sizeof(SCROLLINFO);
2023 horzInfo.nPage = infoPtr->rcList.right - infoPtr->rcList.left;
2025 /* for now, we'll set info.nMax to the _count_, and adjust it later */
2026 if (infoPtr->uView == LV_VIEW_LIST)
2028 INT nPerCol = LISTVIEW_GetCountPerColumn(infoPtr);
2029 horzInfo.nMax = (infoPtr->nItemCount + nPerCol - 1) / nPerCol;
2031 /* scroll by at least one column per page */
2032 if(horzInfo.nPage < infoPtr->nItemWidth)
2033 horzInfo.nPage = infoPtr->nItemWidth;
2035 if (infoPtr->nItemWidth)
2036 horzInfo.nPage /= infoPtr->nItemWidth;
2038 else if (infoPtr->uView == LV_VIEW_DETAILS)
2040 horzInfo.nMax = infoPtr->nItemWidth;
2042 else /* LV_VIEW_ICON, or LV_VIEW_SMALLICON */
2044 RECT rcView;
2046 if (LISTVIEW_GetViewRect(infoPtr, &rcView)) horzInfo.nMax = rcView.right - rcView.left;
2049 if (LISTVIEW_IsHeaderEnabled(infoPtr))
2051 if (DPA_GetPtrCount(infoPtr->hdpaColumns))
2053 RECT rcHeader;
2054 INT index;
2056 index = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX,
2057 DPA_GetPtrCount(infoPtr->hdpaColumns) - 1, 0);
2059 LISTVIEW_GetHeaderRect(infoPtr, index, &rcHeader);
2060 horzInfo.nMax = rcHeader.right;
2061 TRACE("horzInfo.nMax=%d\n", horzInfo.nMax);
2065 horzInfo.fMask = SIF_RANGE | SIF_PAGE;
2066 horzInfo.nMax = max(horzInfo.nMax - 1, 0);
2067 dx = GetScrollPos(infoPtr->hwndSelf, SB_HORZ);
2068 dx -= SetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &horzInfo, TRUE);
2069 TRACE("horzInfo=%s\n", debugscrollinfo(&horzInfo));
2071 /* Update the Header Control */
2072 if (infoPtr->hwndHeader)
2074 horzInfo.fMask = SIF_POS;
2075 GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &horzInfo);
2076 LISTVIEW_UpdateHeaderSize(infoPtr, horzInfo.nPos);
2079 LISTVIEW_UpdateSize(infoPtr);
2080 return dx;
2083 static INT LISTVIEW_UpdateVScroll(LISTVIEW_INFO *infoPtr)
2085 SCROLLINFO vertInfo;
2086 INT dy;
2088 ZeroMemory(&vertInfo, sizeof(SCROLLINFO));
2089 vertInfo.cbSize = sizeof(SCROLLINFO);
2090 vertInfo.nPage = infoPtr->rcList.bottom - infoPtr->rcList.top;
2092 if (infoPtr->uView == LV_VIEW_DETAILS)
2094 vertInfo.nMax = infoPtr->nItemCount;
2096 /* scroll by at least one page */
2097 if(vertInfo.nPage < infoPtr->nItemHeight)
2098 vertInfo.nPage = infoPtr->nItemHeight;
2100 if (infoPtr->nItemHeight > 0)
2101 vertInfo.nPage /= infoPtr->nItemHeight;
2103 else if (infoPtr->uView != LV_VIEW_LIST) /* LV_VIEW_ICON, or LV_VIEW_SMALLICON */
2105 RECT rcView;
2107 if (LISTVIEW_GetViewRect(infoPtr, &rcView)) vertInfo.nMax = rcView.bottom - rcView.top;
2110 vertInfo.fMask = SIF_RANGE | SIF_PAGE;
2111 vertInfo.nMax = max(vertInfo.nMax - 1, 0);
2112 dy = GetScrollPos(infoPtr->hwndSelf, SB_VERT);
2113 dy -= SetScrollInfo(infoPtr->hwndSelf, SB_VERT, &vertInfo, TRUE);
2114 TRACE("vertInfo=%s\n", debugscrollinfo(&vertInfo));
2116 LISTVIEW_UpdateSize(infoPtr);
2117 return dy;
2120 /***
2121 * DESCRIPTION:
2122 * Update the scrollbars. This function should be called whenever
2123 * the content, size or view changes.
2125 * PARAMETER(S):
2126 * [I] infoPtr : valid pointer to the listview structure
2128 * RETURN:
2129 * None
2131 static void LISTVIEW_UpdateScroll(LISTVIEW_INFO *infoPtr)
2133 INT dx, dy, pass;
2135 if ((infoPtr->dwStyle & LVS_NOSCROLL) || !is_redrawing(infoPtr)) return;
2137 /* Setting the horizontal scroll can change the listview size
2138 * (and potentially everything else) so we need to recompute
2139 * everything again for the vertical scroll and vice-versa
2141 for (dx = 0, dy = 0, pass = 0; pass <= 1; pass++)
2143 dx += LISTVIEW_UpdateHScroll(infoPtr);
2144 dy += LISTVIEW_UpdateVScroll(infoPtr);
2147 /* Change of the range may have changed the scroll pos. If so move the content */
2148 if (dx != 0 || dy != 0)
2150 RECT listRect;
2151 listRect = infoPtr->rcList;
2152 ScrollWindowEx(infoPtr->hwndSelf, dx, dy, &listRect, &listRect, 0, 0,
2153 SW_ERASE | SW_INVALIDATE);
2158 /***
2159 * DESCRIPTION:
2160 * Shows/hides the focus rectangle.
2162 * PARAMETER(S):
2163 * [I] infoPtr : valid pointer to the listview structure
2164 * [I] fShow : TRUE to show the focus, FALSE to hide it.
2166 * RETURN:
2167 * None
2169 static void LISTVIEW_ShowFocusRect(const LISTVIEW_INFO *infoPtr, BOOL fShow)
2171 HDC hdc;
2173 TRACE("fShow=%d, nItem=%d\n", fShow, infoPtr->nFocusedItem);
2175 if (infoPtr->nFocusedItem < 0) return;
2177 /* we need some gymnastics in ICON mode to handle large items */
2178 if (infoPtr->uView == LV_VIEW_ICON)
2180 RECT rcBox;
2182 LISTVIEW_GetItemBox(infoPtr, infoPtr->nFocusedItem, &rcBox);
2183 if ((rcBox.bottom - rcBox.top) > infoPtr->nItemHeight)
2185 LISTVIEW_InvalidateRect(infoPtr, &rcBox);
2186 return;
2190 if (!(hdc = GetDC(infoPtr->hwndSelf))) return;
2192 /* for some reason, owner draw should work only in report mode */
2193 if ((infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && (infoPtr->uView == LV_VIEW_DETAILS))
2195 DRAWITEMSTRUCT dis;
2196 LVITEMW item;
2198 HFONT hFont = infoPtr->hFont ? infoPtr->hFont : infoPtr->hDefaultFont;
2199 HFONT hOldFont = SelectObject(hdc, hFont);
2201 item.iItem = infoPtr->nFocusedItem;
2202 item.iSubItem = 0;
2203 item.mask = LVIF_PARAM;
2204 if (!LISTVIEW_GetItemW(infoPtr, &item)) goto done;
2206 ZeroMemory(&dis, sizeof(dis));
2207 dis.CtlType = ODT_LISTVIEW;
2208 dis.CtlID = (UINT)GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
2209 dis.itemID = item.iItem;
2210 dis.itemAction = ODA_FOCUS;
2211 if (fShow) dis.itemState |= ODS_FOCUS;
2212 dis.hwndItem = infoPtr->hwndSelf;
2213 dis.hDC = hdc;
2214 LISTVIEW_GetItemBox(infoPtr, dis.itemID, &dis.rcItem);
2215 dis.itemData = item.lParam;
2217 SendMessageW(infoPtr->hwndNotify, WM_DRAWITEM, dis.CtlID, (LPARAM)&dis);
2219 SelectObject(hdc, hOldFont);
2221 else
2222 LISTVIEW_InvalidateItem(infoPtr, infoPtr->nFocusedItem);
2224 done:
2225 ReleaseDC(infoPtr->hwndSelf, hdc);
2228 /***
2229 * Invalidates all visible selected items.
2231 static void LISTVIEW_InvalidateSelectedItems(const LISTVIEW_INFO *infoPtr)
2233 ITERATOR i;
2235 iterator_frameditems(&i, infoPtr, &infoPtr->rcList);
2236 while(iterator_next(&i))
2238 if (LISTVIEW_GetItemState(infoPtr, i.nItem, LVIS_SELECTED))
2239 LISTVIEW_InvalidateItem(infoPtr, i.nItem);
2241 iterator_destroy(&i);
2245 /***
2246 * DESCRIPTION: [INTERNAL]
2247 * Computes an item's (left,top) corner, relative to rcView.
2248 * That is, the position has NOT been made relative to the Origin.
2249 * This is deliberate, to avoid computing the Origin over, and
2250 * over again, when this function is called in a loop. Instead,
2251 * one can factor the computation of the Origin before the loop,
2252 * and offset the value returned by this function, on every iteration.
2254 * PARAMETER(S):
2255 * [I] infoPtr : valid pointer to the listview structure
2256 * [I] nItem : item number
2257 * [O] lpptOrig : item top, left corner
2259 * RETURN:
2260 * None.
2262 static void LISTVIEW_GetItemOrigin(const LISTVIEW_INFO *infoPtr, INT nItem, LPPOINT lpptPosition)
2264 assert(nItem >= 0 && nItem < infoPtr->nItemCount);
2266 if ((infoPtr->uView == LV_VIEW_SMALLICON) || (infoPtr->uView == LV_VIEW_ICON))
2268 lpptPosition->x = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosX, nItem);
2269 lpptPosition->y = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosY, nItem);
2271 else if (infoPtr->uView == LV_VIEW_LIST)
2273 INT nCountPerColumn = LISTVIEW_GetCountPerColumn(infoPtr);
2274 lpptPosition->x = nItem / nCountPerColumn * infoPtr->nItemWidth;
2275 lpptPosition->y = nItem % nCountPerColumn * infoPtr->nItemHeight;
2277 else /* LV_VIEW_DETAILS */
2279 lpptPosition->x = REPORT_MARGINX;
2280 /* item is always at zero indexed column */
2281 if (DPA_GetPtrCount(infoPtr->hdpaColumns) > 0)
2282 lpptPosition->x += LISTVIEW_GetColumnInfo(infoPtr, 0)->rcHeader.left;
2283 lpptPosition->y = nItem * infoPtr->nItemHeight;
2287 /***
2288 * DESCRIPTION: [INTERNAL]
2289 * Compute the rectangles of an item. This is to localize all
2290 * the computations in one place. If you are not interested in some
2291 * of these values, simply pass in a NULL -- the function is smart
2292 * enough to compute only what's necessary. The function computes
2293 * the standard rectangles (BOUNDS, ICON, LABEL) plus a non-standard
2294 * one, the BOX rectangle. This rectangle is very cheap to compute,
2295 * and is guaranteed to contain all the other rectangles. Computing
2296 * the ICON rect is also cheap, but all the others are potentially
2297 * expensive. This gives an easy and effective optimization when
2298 * searching (like point inclusion, or rectangle intersection):
2299 * first test against the BOX, and if TRUE, test against the desired
2300 * rectangle.
2301 * If the function does not have all the necessary information
2302 * to computed the requested rectangles, will crash with a
2303 * failed assertion. This is done so we catch all programming
2304 * errors, given that the function is called only from our code.
2306 * We have the following 'special' meanings for a few fields:
2307 * * If LVIS_FOCUSED is set, we assume the item has the focus
2308 * This is important in ICON mode, where it might get a larger
2309 * then usual rectangle
2311 * Please note that subitem support works only in REPORT mode.
2313 * PARAMETER(S):
2314 * [I] infoPtr : valid pointer to the listview structure
2315 * [I] lpLVItem : item to compute the measures for
2316 * [O] lprcBox : ptr to Box rectangle
2317 * Same as LVM_GETITEMRECT with LVIR_BOUNDS
2318 * [0] lprcSelectBox : ptr to select box rectangle
2319 * Same as LVM_GETITEMRECT with LVIR_SELECTEDBOUNDS
2320 * [O] lprcIcon : ptr to Icon rectangle
2321 * Same as LVM_GETITEMRECT with LVIR_ICON
2322 * [O] lprcStateIcon: ptr to State Icon rectangle
2323 * [O] lprcLabel : ptr to Label rectangle
2324 * Same as LVM_GETITEMRECT with LVIR_LABEL
2326 * RETURN:
2327 * None.
2329 static void LISTVIEW_GetItemMetrics(const LISTVIEW_INFO *infoPtr, const LVITEMW *lpLVItem,
2330 LPRECT lprcBox, LPRECT lprcSelectBox,
2331 LPRECT lprcIcon, LPRECT lprcStateIcon, LPRECT lprcLabel)
2333 BOOL doSelectBox = FALSE, doIcon = FALSE, doLabel = FALSE, oversizedBox = FALSE;
2334 RECT Box, SelectBox, Icon, Label;
2335 COLUMN_INFO *lpColumnInfo = NULL;
2336 SIZE labelSize = { 0, 0 };
2338 TRACE("(lpLVItem=%s)\n", debuglvitem_t(lpLVItem, TRUE));
2340 /* Be smart and try to figure out the minimum we have to do */
2341 if (lpLVItem->iSubItem) assert(infoPtr->uView == LV_VIEW_DETAILS);
2342 if (infoPtr->uView == LV_VIEW_ICON && (lprcBox || lprcLabel))
2344 assert((lpLVItem->mask & LVIF_STATE) && (lpLVItem->stateMask & LVIS_FOCUSED));
2345 if (lpLVItem->state & LVIS_FOCUSED) oversizedBox = doLabel = TRUE;
2347 if (lprcSelectBox) doSelectBox = TRUE;
2348 if (lprcLabel) doLabel = TRUE;
2349 if (doLabel || lprcIcon || lprcStateIcon) doIcon = TRUE;
2350 if (doSelectBox)
2352 doIcon = TRUE;
2353 doLabel = TRUE;
2356 /************************************************************/
2357 /* compute the box rectangle (it should be cheap to do) */
2358 /************************************************************/
2359 if (lpLVItem->iSubItem || infoPtr->uView == LV_VIEW_DETAILS)
2360 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, lpLVItem->iSubItem);
2362 if (lpLVItem->iSubItem)
2364 Box = lpColumnInfo->rcHeader;
2366 else
2368 Box.left = 0;
2369 Box.right = infoPtr->nItemWidth;
2371 Box.top = 0;
2372 Box.bottom = infoPtr->nItemHeight;
2374 /******************************************************************/
2375 /* compute ICON bounding box (ala LVM_GETITEMRECT) and STATEICON */
2376 /******************************************************************/
2377 if (doIcon)
2379 LONG state_width = 0;
2381 if (infoPtr->himlState && lpLVItem->iSubItem == 0)
2382 state_width = infoPtr->iconStateSize.cx;
2384 if (infoPtr->uView == LV_VIEW_ICON)
2386 Icon.left = Box.left + state_width;
2387 if (infoPtr->himlNormal)
2388 Icon.left += (infoPtr->nItemWidth - infoPtr->iconSize.cx - state_width) / 2;
2389 Icon.top = Box.top + ICON_TOP_PADDING;
2390 Icon.right = Icon.left;
2391 Icon.bottom = Icon.top;
2392 if (infoPtr->himlNormal)
2394 Icon.right += infoPtr->iconSize.cx;
2395 Icon.bottom += infoPtr->iconSize.cy;
2398 else /* LV_VIEW_SMALLICON, LV_VIEW_LIST or LV_VIEW_DETAILS */
2400 Icon.left = Box.left + state_width;
2402 if (infoPtr->uView == LV_VIEW_DETAILS && lpLVItem->iSubItem == 0)
2404 /* we need the indent in report mode */
2405 assert(lpLVItem->mask & LVIF_INDENT);
2406 Icon.left += infoPtr->iconSize.cx * lpLVItem->iIndent + REPORT_MARGINX;
2409 Icon.top = Box.top;
2410 Icon.right = Icon.left;
2411 if (infoPtr->himlSmall &&
2412 (!lpColumnInfo || lpLVItem->iSubItem == 0 ||
2413 ((infoPtr->dwLvExStyle & LVS_EX_SUBITEMIMAGES) && lpLVItem->iImage != I_IMAGECALLBACK)))
2414 Icon.right += infoPtr->iconSize.cx;
2415 Icon.bottom = Icon.top + infoPtr->iconSize.cy;
2417 if(lprcIcon) *lprcIcon = Icon;
2418 TRACE(" - icon=%s\n", wine_dbgstr_rect(&Icon));
2420 /* TODO: is this correct? */
2421 if (lprcStateIcon)
2423 lprcStateIcon->left = Icon.left - state_width;
2424 lprcStateIcon->right = Icon.left;
2425 lprcStateIcon->top = Icon.top;
2426 lprcStateIcon->bottom = lprcStateIcon->top + infoPtr->iconSize.cy;
2427 TRACE(" - state icon=%s\n", wine_dbgstr_rect(lprcStateIcon));
2430 else Icon.right = 0;
2432 /************************************************************/
2433 /* compute LABEL bounding box (ala LVM_GETITEMRECT) */
2434 /************************************************************/
2435 if (doLabel)
2437 /* calculate how far to the right can the label stretch */
2438 Label.right = Box.right;
2439 if (infoPtr->uView == LV_VIEW_DETAILS)
2441 if (lpLVItem->iSubItem == 0)
2443 /* we need a zero based rect here */
2444 Label = lpColumnInfo->rcHeader;
2445 OffsetRect(&Label, -Label.left, 0);
2449 if (lpLVItem->iSubItem || ((infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && infoPtr->uView == LV_VIEW_DETAILS))
2451 labelSize.cx = infoPtr->nItemWidth;
2452 labelSize.cy = infoPtr->nItemHeight;
2453 goto calc_label;
2456 /* we need the text in non owner draw mode */
2457 assert(lpLVItem->mask & LVIF_TEXT);
2458 if (is_text(lpLVItem->pszText))
2460 HFONT hFont = infoPtr->hFont ? infoPtr->hFont : infoPtr->hDefaultFont;
2461 HDC hdc = GetDC(infoPtr->hwndSelf);
2462 HFONT hOldFont = SelectObject(hdc, hFont);
2463 UINT uFormat;
2464 RECT rcText;
2466 /* compute rough rectangle where the label will go */
2467 SetRectEmpty(&rcText);
2468 rcText.right = infoPtr->nItemWidth - TRAILING_LABEL_PADDING;
2469 rcText.bottom = infoPtr->nItemHeight;
2470 if (infoPtr->uView == LV_VIEW_ICON)
2471 rcText.bottom -= ICON_TOP_PADDING + infoPtr->iconSize.cy + ICON_BOTTOM_PADDING;
2473 /* now figure out the flags */
2474 if (infoPtr->uView == LV_VIEW_ICON)
2475 uFormat = oversizedBox ? LV_FL_DT_FLAGS : LV_ML_DT_FLAGS;
2476 else
2477 uFormat = LV_SL_DT_FLAGS;
2479 DrawTextW (hdc, lpLVItem->pszText, -1, &rcText, uFormat | DT_CALCRECT);
2481 if (rcText.right != rcText.left)
2482 labelSize.cx = min(rcText.right - rcText.left + TRAILING_LABEL_PADDING, infoPtr->nItemWidth);
2484 labelSize.cy = rcText.bottom - rcText.top;
2486 SelectObject(hdc, hOldFont);
2487 ReleaseDC(infoPtr->hwndSelf, hdc);
2490 calc_label:
2491 if (infoPtr->uView == LV_VIEW_ICON)
2493 Label.left = Box.left + (infoPtr->nItemWidth - labelSize.cx) / 2;
2494 Label.top = Box.top + ICON_TOP_PADDING_HITABLE +
2495 infoPtr->iconSize.cy + ICON_BOTTOM_PADDING;
2496 Label.right = Label.left + labelSize.cx;
2497 Label.bottom = Label.top + infoPtr->nItemHeight;
2498 if (!oversizedBox && labelSize.cy > infoPtr->ntmHeight)
2500 labelSize.cy = min(Box.bottom - Label.top, labelSize.cy);
2501 labelSize.cy /= infoPtr->ntmHeight;
2502 labelSize.cy = max(labelSize.cy, 1);
2503 labelSize.cy *= infoPtr->ntmHeight;
2505 Label.bottom = Label.top + labelSize.cy + HEIGHT_PADDING;
2507 else if (infoPtr->uView == LV_VIEW_DETAILS)
2509 Label.left = Icon.right;
2510 Label.top = Box.top;
2511 Label.right = lpLVItem->iSubItem ? lpColumnInfo->rcHeader.right :
2512 lpColumnInfo->rcHeader.right - lpColumnInfo->rcHeader.left;
2513 Label.bottom = Label.top + infoPtr->nItemHeight;
2515 else /* LV_VIEW_SMALLICON or LV_VIEW_LIST */
2517 Label.left = Icon.right;
2518 Label.top = Box.top;
2519 Label.right = min(Label.left + labelSize.cx, Label.right);
2520 Label.bottom = Label.top + infoPtr->nItemHeight;
2523 if (lprcLabel) *lprcLabel = Label;
2524 TRACE(" - label=%s\n", wine_dbgstr_rect(&Label));
2527 /************************************************************/
2528 /* compute SELECT bounding box */
2529 /************************************************************/
2530 if (doSelectBox)
2532 if (infoPtr->uView == LV_VIEW_DETAILS)
2534 SelectBox.left = Icon.left;
2535 SelectBox.top = Box.top;
2536 SelectBox.bottom = Box.bottom;
2538 if (labelSize.cx)
2539 SelectBox.right = min(Label.left + labelSize.cx, Label.right);
2540 else
2541 SelectBox.right = min(Label.left + MAX_EMPTYTEXT_SELECT_WIDTH, Label.right);
2543 else
2545 UnionRect(&SelectBox, &Icon, &Label);
2547 if (lprcSelectBox) *lprcSelectBox = SelectBox;
2548 TRACE(" - select box=%s\n", wine_dbgstr_rect(&SelectBox));
2551 /* Fix the Box if necessary */
2552 if (lprcBox)
2554 if (oversizedBox) UnionRect(lprcBox, &Box, &Label);
2555 else *lprcBox = Box;
2557 TRACE(" - box=%s\n", wine_dbgstr_rect(&Box));
2560 /***
2561 * DESCRIPTION: [INTERNAL]
2563 * PARAMETER(S):
2564 * [I] infoPtr : valid pointer to the listview structure
2565 * [I] nItem : item number
2566 * [O] lprcBox : ptr to Box rectangle
2568 * RETURN:
2569 * None.
2571 static void LISTVIEW_GetItemBox(const LISTVIEW_INFO *infoPtr, INT nItem, LPRECT lprcBox)
2573 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
2574 POINT Position, Origin;
2575 LVITEMW lvItem;
2577 LISTVIEW_GetOrigin(infoPtr, &Origin);
2578 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position);
2580 /* Be smart and try to figure out the minimum we have to do */
2581 lvItem.mask = 0;
2582 if (infoPtr->uView == LV_VIEW_ICON && infoPtr->bFocus && LISTVIEW_GetItemState(infoPtr, nItem, LVIS_FOCUSED))
2583 lvItem.mask |= LVIF_TEXT;
2584 lvItem.iItem = nItem;
2585 lvItem.iSubItem = 0;
2586 lvItem.pszText = szDispText;
2587 lvItem.cchTextMax = DISP_TEXT_SIZE;
2588 if (lvItem.mask) LISTVIEW_GetItemW(infoPtr, &lvItem);
2589 if (infoPtr->uView == LV_VIEW_ICON)
2591 lvItem.mask |= LVIF_STATE;
2592 lvItem.stateMask = LVIS_FOCUSED;
2593 lvItem.state = (lvItem.mask & LVIF_TEXT ? LVIS_FOCUSED : 0);
2595 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, lprcBox, 0, 0, 0, 0);
2597 if (infoPtr->uView == LV_VIEW_DETAILS && infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT &&
2598 SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX, 0, 0))
2600 OffsetRect(lprcBox, Origin.x, Position.y + Origin.y);
2602 else
2603 OffsetRect(lprcBox, Position.x + Origin.x, Position.y + Origin.y);
2606 /* LISTVIEW_MapIdToIndex helper */
2607 static INT CALLBACK MapIdSearchCompare(LPVOID p1, LPVOID p2, LPARAM lParam)
2609 ITEM_ID *id1 = (ITEM_ID*)p1;
2610 ITEM_ID *id2 = (ITEM_ID*)p2;
2612 if (id1->id == id2->id) return 0;
2614 return (id1->id < id2->id) ? -1 : 1;
2617 /***
2618 * DESCRIPTION:
2619 * Returns the item index for id specified.
2621 * PARAMETER(S):
2622 * [I] infoPtr : valid pointer to the listview structure
2623 * [I] iID : item id to get index for
2625 * RETURN:
2626 * Item index, or -1 on failure.
2628 static INT LISTVIEW_MapIdToIndex(const LISTVIEW_INFO *infoPtr, UINT iID)
2630 ITEM_ID ID;
2631 INT index;
2633 TRACE("iID=%d\n", iID);
2635 if (infoPtr->dwStyle & LVS_OWNERDATA) return -1;
2636 if (infoPtr->nItemCount == 0) return -1;
2638 ID.id = iID;
2639 index = DPA_Search(infoPtr->hdpaItemIds, &ID, -1, MapIdSearchCompare, 0, DPAS_SORTED);
2641 if (index != -1)
2643 ITEM_ID *lpID = DPA_GetPtr(infoPtr->hdpaItemIds, index);
2644 return DPA_GetPtrIndex(infoPtr->hdpaItems, lpID->item);
2647 return -1;
2650 /***
2651 * DESCRIPTION:
2652 * Returns the item id for index given.
2654 * PARAMETER(S):
2655 * [I] infoPtr : valid pointer to the listview structure
2656 * [I] iItem : item index to get id for
2658 * RETURN:
2659 * Item id.
2661 static DWORD LISTVIEW_MapIndexToId(const LISTVIEW_INFO *infoPtr, INT iItem)
2663 ITEM_INFO *lpItem;
2664 HDPA hdpaSubItems;
2666 TRACE("iItem=%d\n", iItem);
2668 if (infoPtr->dwStyle & LVS_OWNERDATA) return -1;
2669 if (iItem < 0 || iItem >= infoPtr->nItemCount) return -1;
2671 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, iItem);
2672 lpItem = DPA_GetPtr(hdpaSubItems, 0);
2674 return lpItem->id->id;
2677 /***
2678 * DESCRIPTION:
2679 * Returns the current icon position, and advances it along the top.
2680 * The returned position is not offset by Origin.
2682 * PARAMETER(S):
2683 * [I] infoPtr : valid pointer to the listview structure
2684 * [O] lpPos : will get the current icon position
2686 * RETURN:
2687 * None
2689 static void LISTVIEW_NextIconPosTop(LISTVIEW_INFO *infoPtr, LPPOINT lpPos)
2691 INT nListWidth = infoPtr->rcList.right - infoPtr->rcList.left;
2693 *lpPos = infoPtr->currIconPos;
2695 infoPtr->currIconPos.x += infoPtr->nItemWidth;
2696 if (infoPtr->currIconPos.x + infoPtr->nItemWidth <= nListWidth) return;
2698 infoPtr->currIconPos.x = 0;
2699 infoPtr->currIconPos.y += infoPtr->nItemHeight;
2703 /***
2704 * DESCRIPTION:
2705 * Returns the current icon position, and advances it down the left edge.
2706 * The returned position is not offset by Origin.
2708 * PARAMETER(S):
2709 * [I] infoPtr : valid pointer to the listview structure
2710 * [O] lpPos : will get the current icon position
2712 * RETURN:
2713 * None
2715 static void LISTVIEW_NextIconPosLeft(LISTVIEW_INFO *infoPtr, LPPOINT lpPos)
2717 INT nListHeight = infoPtr->rcList.bottom - infoPtr->rcList.top;
2719 *lpPos = infoPtr->currIconPos;
2721 infoPtr->currIconPos.y += infoPtr->nItemHeight;
2722 if (infoPtr->currIconPos.y + infoPtr->nItemHeight <= nListHeight) return;
2724 infoPtr->currIconPos.x += infoPtr->nItemWidth;
2725 infoPtr->currIconPos.y = 0;
2729 /***
2730 * DESCRIPTION:
2731 * Moves an icon to the specified position.
2732 * It takes care of invalidating the item, etc.
2734 * PARAMETER(S):
2735 * [I] infoPtr : valid pointer to the listview structure
2736 * [I] nItem : the item to move
2737 * [I] lpPos : the new icon position
2738 * [I] isNew : flags the item as being new
2740 * RETURN:
2741 * Success: TRUE
2742 * Failure: FALSE
2744 static BOOL LISTVIEW_MoveIconTo(const LISTVIEW_INFO *infoPtr, INT nItem, const POINT *lppt, BOOL isNew)
2746 POINT old;
2748 if (!isNew)
2750 old.x = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosX, nItem);
2751 old.y = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosY, nItem);
2753 if (lppt->x == old.x && lppt->y == old.y) return TRUE;
2754 LISTVIEW_InvalidateItem(infoPtr, nItem);
2757 /* Allocating a POINTER for every item is too resource intensive,
2758 * so we'll keep the (x,y) in different arrays */
2759 if (!DPA_SetPtr(infoPtr->hdpaPosX, nItem, (void *)(LONG_PTR)lppt->x)) return FALSE;
2760 if (!DPA_SetPtr(infoPtr->hdpaPosY, nItem, (void *)(LONG_PTR)lppt->y)) return FALSE;
2762 LISTVIEW_InvalidateItem(infoPtr, nItem);
2764 return TRUE;
2767 /***
2768 * DESCRIPTION:
2769 * Arranges listview items in icon display mode.
2771 * PARAMETER(S):
2772 * [I] infoPtr : valid pointer to the listview structure
2773 * [I] nAlignCode : alignment code
2775 * RETURN:
2776 * SUCCESS : TRUE
2777 * FAILURE : FALSE
2779 static BOOL LISTVIEW_Arrange(LISTVIEW_INFO *infoPtr, INT nAlignCode)
2781 void (*next_pos)(LISTVIEW_INFO *, LPPOINT);
2782 POINT pos;
2783 INT i;
2785 if (infoPtr->uView != LV_VIEW_ICON && infoPtr->uView != LV_VIEW_SMALLICON) return FALSE;
2787 TRACE("nAlignCode=%d\n", nAlignCode);
2789 if (nAlignCode == LVA_DEFAULT)
2791 if (infoPtr->dwStyle & LVS_ALIGNLEFT) nAlignCode = LVA_ALIGNLEFT;
2792 else nAlignCode = LVA_ALIGNTOP;
2795 switch (nAlignCode)
2797 case LVA_ALIGNLEFT: next_pos = LISTVIEW_NextIconPosLeft; break;
2798 case LVA_ALIGNTOP: next_pos = LISTVIEW_NextIconPosTop; break;
2799 case LVA_SNAPTOGRID: next_pos = LISTVIEW_NextIconPosTop; break; /* FIXME */
2800 default: return FALSE;
2803 infoPtr->currIconPos.x = infoPtr->currIconPos.y = 0;
2804 for (i = 0; i < infoPtr->nItemCount; i++)
2806 next_pos(infoPtr, &pos);
2807 LISTVIEW_MoveIconTo(infoPtr, i, &pos, FALSE);
2810 return TRUE;
2813 /***
2814 * DESCRIPTION:
2815 * Retrieves the bounding rectangle of all the items, not offset by Origin.
2816 * For LVS_REPORT always returns empty rectangle.
2818 * PARAMETER(S):
2819 * [I] infoPtr : valid pointer to the listview structure
2820 * [O] lprcView : bounding rectangle
2822 * RETURN:
2823 * SUCCESS : TRUE
2824 * FAILURE : FALSE
2826 static void LISTVIEW_GetAreaRect(const LISTVIEW_INFO *infoPtr, LPRECT lprcView)
2828 INT i, x, y;
2830 SetRectEmpty(lprcView);
2832 switch (infoPtr->uView)
2834 case LV_VIEW_ICON:
2835 case LV_VIEW_SMALLICON:
2836 for (i = 0; i < infoPtr->nItemCount; i++)
2838 x = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosX, i);
2839 y = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosY, i);
2840 lprcView->right = max(lprcView->right, x);
2841 lprcView->bottom = max(lprcView->bottom, y);
2843 if (infoPtr->nItemCount > 0)
2845 lprcView->right += infoPtr->nItemWidth;
2846 lprcView->bottom += infoPtr->nItemHeight;
2848 break;
2850 case LV_VIEW_LIST:
2851 y = LISTVIEW_GetCountPerColumn(infoPtr);
2852 x = infoPtr->nItemCount / y;
2853 if (infoPtr->nItemCount % y) x++;
2854 lprcView->right = x * infoPtr->nItemWidth;
2855 lprcView->bottom = y * infoPtr->nItemHeight;
2856 break;
2860 /***
2861 * DESCRIPTION:
2862 * Retrieves the bounding rectangle of all the items.
2864 * PARAMETER(S):
2865 * [I] infoPtr : valid pointer to the listview structure
2866 * [O] lprcView : bounding rectangle
2868 * RETURN:
2869 * SUCCESS : TRUE
2870 * FAILURE : FALSE
2872 static BOOL LISTVIEW_GetViewRect(const LISTVIEW_INFO *infoPtr, LPRECT lprcView)
2874 POINT ptOrigin;
2876 TRACE("(lprcView=%p)\n", lprcView);
2878 if (!lprcView) return FALSE;
2880 LISTVIEW_GetAreaRect(infoPtr, lprcView);
2882 if (infoPtr->uView != LV_VIEW_DETAILS)
2884 LISTVIEW_GetOrigin(infoPtr, &ptOrigin);
2885 OffsetRect(lprcView, ptOrigin.x, ptOrigin.y);
2888 TRACE("lprcView=%s\n", wine_dbgstr_rect(lprcView));
2890 return TRUE;
2893 /***
2894 * DESCRIPTION:
2895 * Retrieves the subitem pointer associated with the subitem index.
2897 * PARAMETER(S):
2898 * [I] hdpaSubItems : DPA handle for a specific item
2899 * [I] nSubItem : index of subitem
2901 * RETURN:
2902 * SUCCESS : subitem pointer
2903 * FAILURE : NULL
2905 static SUBITEM_INFO* LISTVIEW_GetSubItemPtr(HDPA hdpaSubItems, INT nSubItem)
2907 SUBITEM_INFO *lpSubItem;
2908 INT i;
2910 /* we should binary search here if need be */
2911 for (i = 1; i < DPA_GetPtrCount(hdpaSubItems); i++)
2913 lpSubItem = DPA_GetPtr(hdpaSubItems, i);
2914 if (lpSubItem->iSubItem == nSubItem)
2915 return lpSubItem;
2918 return NULL;
2922 /***
2923 * DESCRIPTION:
2924 * Calculates the desired item width.
2926 * PARAMETER(S):
2927 * [I] infoPtr : valid pointer to the listview structure
2929 * RETURN:
2930 * The desired item width.
2932 static INT LISTVIEW_CalculateItemWidth(const LISTVIEW_INFO *infoPtr)
2934 INT nItemWidth = 0;
2936 TRACE("uView=%d\n", infoPtr->uView);
2938 if (infoPtr->uView == LV_VIEW_ICON)
2939 nItemWidth = infoPtr->iconSpacing.cx;
2940 else if (infoPtr->uView == LV_VIEW_DETAILS)
2942 if (DPA_GetPtrCount(infoPtr->hdpaColumns) > 0)
2944 RECT rcHeader;
2945 INT index;
2947 index = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX,
2948 DPA_GetPtrCount(infoPtr->hdpaColumns) - 1, 0);
2950 LISTVIEW_GetHeaderRect(infoPtr, index, &rcHeader);
2951 nItemWidth = rcHeader.right;
2954 else /* LV_VIEW_SMALLICON, or LV_VIEW_LIST */
2956 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
2957 LVITEMW lvItem;
2958 INT i;
2960 lvItem.mask = LVIF_TEXT;
2961 lvItem.iSubItem = 0;
2963 for (i = 0; i < infoPtr->nItemCount; i++)
2965 lvItem.iItem = i;
2966 lvItem.pszText = szDispText;
2967 lvItem.cchTextMax = DISP_TEXT_SIZE;
2968 if (LISTVIEW_GetItemW(infoPtr, &lvItem))
2969 nItemWidth = max(LISTVIEW_GetStringWidthT(infoPtr, lvItem.pszText, TRUE),
2970 nItemWidth);
2973 if (infoPtr->himlSmall) nItemWidth += infoPtr->iconSize.cx;
2974 if (infoPtr->himlState) nItemWidth += infoPtr->iconStateSize.cx;
2976 nItemWidth = max(DEFAULT_COLUMN_WIDTH, nItemWidth + WIDTH_PADDING);
2979 return nItemWidth;
2982 /***
2983 * DESCRIPTION:
2984 * Calculates the desired item height.
2986 * PARAMETER(S):
2987 * [I] infoPtr : valid pointer to the listview structure
2989 * RETURN:
2990 * The desired item height.
2992 static INT LISTVIEW_CalculateItemHeight(const LISTVIEW_INFO *infoPtr)
2994 INT nItemHeight;
2996 TRACE("uView=%d\n", infoPtr->uView);
2998 if (infoPtr->uView == LV_VIEW_ICON)
2999 nItemHeight = infoPtr->iconSpacing.cy;
3000 else
3002 nItemHeight = infoPtr->ntmHeight;
3003 if (infoPtr->himlState)
3004 nItemHeight = max(nItemHeight, infoPtr->iconStateSize.cy);
3005 if (infoPtr->himlSmall)
3006 nItemHeight = max(nItemHeight, infoPtr->iconSize.cy);
3007 nItemHeight += HEIGHT_PADDING;
3008 if (infoPtr->nMeasureItemHeight > 0)
3009 nItemHeight = infoPtr->nMeasureItemHeight;
3012 return max(nItemHeight, 1);
3015 /***
3016 * DESCRIPTION:
3017 * Updates the width, and height of an item.
3019 * PARAMETER(S):
3020 * [I] infoPtr : valid pointer to the listview structure
3022 * RETURN:
3023 * None.
3025 static inline void LISTVIEW_UpdateItemSize(LISTVIEW_INFO *infoPtr)
3027 infoPtr->nItemWidth = LISTVIEW_CalculateItemWidth(infoPtr);
3028 infoPtr->nItemHeight = LISTVIEW_CalculateItemHeight(infoPtr);
3032 /***
3033 * DESCRIPTION:
3034 * Retrieves and saves important text metrics info for the current
3035 * Listview font.
3037 * PARAMETER(S):
3038 * [I] infoPtr : valid pointer to the listview structure
3041 static void LISTVIEW_SaveTextMetrics(LISTVIEW_INFO *infoPtr)
3043 HDC hdc = GetDC(infoPtr->hwndSelf);
3044 HFONT hFont = infoPtr->hFont ? infoPtr->hFont : infoPtr->hDefaultFont;
3045 HFONT hOldFont = SelectObject(hdc, hFont);
3046 TEXTMETRICW tm;
3047 SIZE sz;
3049 if (GetTextMetricsW(hdc, &tm))
3051 infoPtr->ntmHeight = tm.tmHeight;
3052 infoPtr->ntmMaxCharWidth = tm.tmMaxCharWidth;
3055 if (GetTextExtentPoint32A(hdc, "...", 3, &sz))
3056 infoPtr->nEllipsisWidth = sz.cx;
3058 SelectObject(hdc, hOldFont);
3059 ReleaseDC(infoPtr->hwndSelf, hdc);
3061 TRACE("tmHeight=%d\n", infoPtr->ntmHeight);
3064 /***
3065 * DESCRIPTION:
3066 * A compare function for ranges
3068 * PARAMETER(S)
3069 * [I] range1 : pointer to range 1;
3070 * [I] range2 : pointer to range 2;
3071 * [I] flags : flags
3073 * RETURNS:
3074 * > 0 : if range 1 > range 2
3075 * < 0 : if range 2 > range 1
3076 * = 0 : if range intersects range 2
3078 static INT CALLBACK ranges_cmp(LPVOID range1, LPVOID range2, LPARAM flags)
3080 INT cmp;
3082 if (((RANGE*)range1)->upper <= ((RANGE*)range2)->lower)
3083 cmp = -1;
3084 else if (((RANGE*)range2)->upper <= ((RANGE*)range1)->lower)
3085 cmp = 1;
3086 else
3087 cmp = 0;
3089 TRACE("range1=%s, range2=%s, cmp=%d\n", debugrange(range1), debugrange(range2), cmp);
3091 return cmp;
3094 #define ranges_check(ranges, desc) if (TRACE_ON(listview)) ranges_assert(ranges, desc, __FILE__, __LINE__)
3096 static void ranges_assert(RANGES ranges, LPCSTR desc, const char *file, int line)
3098 INT i;
3099 RANGE *prev, *curr;
3101 TRACE("*** Checking %s:%d:%s ***\n", file, line, desc);
3102 assert (ranges);
3103 assert (DPA_GetPtrCount(ranges->hdpa) >= 0);
3104 ranges_dump(ranges);
3105 if (DPA_GetPtrCount(ranges->hdpa) > 0)
3107 prev = DPA_GetPtr(ranges->hdpa, 0);
3108 assert (prev->lower >= 0 && prev->lower < prev->upper);
3109 for (i = 1; i < DPA_GetPtrCount(ranges->hdpa); i++)
3111 curr = DPA_GetPtr(ranges->hdpa, i);
3112 assert (prev->upper <= curr->lower);
3113 assert (curr->lower < curr->upper);
3114 prev = curr;
3117 TRACE("--- Done checking---\n");
3120 static RANGES ranges_create(int count)
3122 RANGES ranges = Alloc(sizeof(struct tagRANGES));
3123 if (!ranges) return NULL;
3124 ranges->hdpa = DPA_Create(count);
3125 if (ranges->hdpa) return ranges;
3126 Free(ranges);
3127 return NULL;
3130 static void ranges_clear(RANGES ranges)
3132 INT i;
3134 for(i = 0; i < DPA_GetPtrCount(ranges->hdpa); i++)
3135 Free(DPA_GetPtr(ranges->hdpa, i));
3136 DPA_DeleteAllPtrs(ranges->hdpa);
3140 static void ranges_destroy(RANGES ranges)
3142 if (!ranges) return;
3143 ranges_clear(ranges);
3144 DPA_Destroy(ranges->hdpa);
3145 Free(ranges);
3148 static RANGES ranges_clone(RANGES ranges)
3150 RANGES clone;
3151 INT i;
3153 if (!(clone = ranges_create(DPA_GetPtrCount(ranges->hdpa)))) goto fail;
3155 for (i = 0; i < DPA_GetPtrCount(ranges->hdpa); i++)
3157 RANGE *newrng = Alloc(sizeof(RANGE));
3158 if (!newrng) goto fail;
3159 *newrng = *((RANGE*)DPA_GetPtr(ranges->hdpa, i));
3160 if (!DPA_SetPtr(clone->hdpa, i, newrng))
3162 Free(newrng);
3163 goto fail;
3166 return clone;
3168 fail:
3169 TRACE ("clone failed\n");
3170 ranges_destroy(clone);
3171 return NULL;
3174 static RANGES ranges_diff(RANGES ranges, RANGES sub)
3176 INT i;
3178 for (i = 0; i < DPA_GetPtrCount(sub->hdpa); i++)
3179 ranges_del(ranges, *((RANGE *)DPA_GetPtr(sub->hdpa, i)));
3181 return ranges;
3184 static void ranges_dump(RANGES ranges)
3186 INT i;
3188 for (i = 0; i < DPA_GetPtrCount(ranges->hdpa); i++)
3189 TRACE(" %s\n", debugrange(DPA_GetPtr(ranges->hdpa, i)));
3192 static inline BOOL ranges_contain(RANGES ranges, INT nItem)
3194 RANGE srchrng = { nItem, nItem + 1 };
3196 TRACE("(nItem=%d)\n", nItem);
3197 ranges_check(ranges, "before contain");
3198 return DPA_Search(ranges->hdpa, &srchrng, 0, ranges_cmp, 0, DPAS_SORTED) != -1;
3201 static INT ranges_itemcount(RANGES ranges)
3203 INT i, count = 0;
3205 for (i = 0; i < DPA_GetPtrCount(ranges->hdpa); i++)
3207 RANGE *sel = DPA_GetPtr(ranges->hdpa, i);
3208 count += sel->upper - sel->lower;
3211 return count;
3214 static BOOL ranges_shift(RANGES ranges, INT nItem, INT delta, INT nUpper)
3216 RANGE srchrng = { nItem, nItem + 1 }, *chkrng;
3217 INT index;
3219 index = DPA_Search(ranges->hdpa, &srchrng, 0, ranges_cmp, 0, DPAS_SORTED | DPAS_INSERTAFTER);
3220 if (index == -1) return TRUE;
3222 for (; index < DPA_GetPtrCount(ranges->hdpa); index++)
3224 chkrng = DPA_GetPtr(ranges->hdpa, index);
3225 if (chkrng->lower >= nItem)
3226 chkrng->lower = max(min(chkrng->lower + delta, nUpper - 1), 0);
3227 if (chkrng->upper > nItem)
3228 chkrng->upper = max(min(chkrng->upper + delta, nUpper), 0);
3230 return TRUE;
3233 static BOOL ranges_add(RANGES ranges, RANGE range)
3235 RANGE srchrgn;
3236 INT index;
3238 TRACE("(%s)\n", debugrange(&range));
3239 ranges_check(ranges, "before add");
3241 /* try find overlapping regions first */
3242 srchrgn.lower = range.lower - 1;
3243 srchrgn.upper = range.upper + 1;
3244 index = DPA_Search(ranges->hdpa, &srchrgn, 0, ranges_cmp, 0, DPAS_SORTED);
3246 if (index == -1)
3248 RANGE *newrgn;
3250 TRACE("Adding new range\n");
3252 /* create the brand new range to insert */
3253 newrgn = Alloc(sizeof(RANGE));
3254 if(!newrgn) goto fail;
3255 *newrgn = range;
3257 /* figure out where to insert it */
3258 index = DPA_Search(ranges->hdpa, newrgn, 0, ranges_cmp, 0, DPAS_SORTED | DPAS_INSERTAFTER);
3259 TRACE("index=%d\n", index);
3260 if (index == -1) index = 0;
3262 /* and get it over with */
3263 if (DPA_InsertPtr(ranges->hdpa, index, newrgn) == -1)
3265 Free(newrgn);
3266 goto fail;
3269 else
3271 RANGE *chkrgn, *mrgrgn;
3272 INT fromindex, mergeindex;
3274 chkrgn = DPA_GetPtr(ranges->hdpa, index);
3275 TRACE("Merge with %s @%d\n", debugrange(chkrgn), index);
3277 chkrgn->lower = min(range.lower, chkrgn->lower);
3278 chkrgn->upper = max(range.upper, chkrgn->upper);
3280 TRACE("New range %s @%d\n", debugrange(chkrgn), index);
3282 /* merge now common ranges */
3283 fromindex = 0;
3284 srchrgn.lower = chkrgn->lower - 1;
3285 srchrgn.upper = chkrgn->upper + 1;
3289 mergeindex = DPA_Search(ranges->hdpa, &srchrgn, fromindex, ranges_cmp, 0, 0);
3290 if (mergeindex == -1) break;
3291 if (mergeindex == index)
3293 fromindex = index + 1;
3294 continue;
3297 TRACE("Merge with index %i\n", mergeindex);
3299 mrgrgn = DPA_GetPtr(ranges->hdpa, mergeindex);
3300 chkrgn->lower = min(chkrgn->lower, mrgrgn->lower);
3301 chkrgn->upper = max(chkrgn->upper, mrgrgn->upper);
3302 Free(mrgrgn);
3303 DPA_DeletePtr(ranges->hdpa, mergeindex);
3304 if (mergeindex < index) index --;
3305 } while(1);
3308 ranges_check(ranges, "after add");
3309 return TRUE;
3311 fail:
3312 ranges_check(ranges, "failed add");
3313 return FALSE;
3316 static BOOL ranges_del(RANGES ranges, RANGE range)
3318 RANGE *chkrgn;
3319 INT index;
3321 TRACE("(%s)\n", debugrange(&range));
3322 ranges_check(ranges, "before del");
3324 /* we don't use DPAS_SORTED here, since we need *
3325 * to find the first overlapping range */
3326 index = DPA_Search(ranges->hdpa, &range, 0, ranges_cmp, 0, 0);
3327 while(index != -1)
3329 chkrgn = DPA_GetPtr(ranges->hdpa, index);
3331 TRACE("Matches range %s @%d\n", debugrange(chkrgn), index);
3333 /* case 1: Same range */
3334 if ( (chkrgn->upper == range.upper) &&
3335 (chkrgn->lower == range.lower) )
3337 DPA_DeletePtr(ranges->hdpa, index);
3338 Free(chkrgn);
3339 break;
3341 /* case 2: engulf */
3342 else if ( (chkrgn->upper <= range.upper) &&
3343 (chkrgn->lower >= range.lower) )
3345 DPA_DeletePtr(ranges->hdpa, index);
3346 Free(chkrgn);
3348 /* case 3: overlap upper */
3349 else if ( (chkrgn->upper <= range.upper) &&
3350 (chkrgn->lower < range.lower) )
3352 chkrgn->upper = range.lower;
3354 /* case 4: overlap lower */
3355 else if ( (chkrgn->upper > range.upper) &&
3356 (chkrgn->lower >= range.lower) )
3358 chkrgn->lower = range.upper;
3359 break;
3361 /* case 5: fully internal */
3362 else
3364 RANGE *newrgn;
3366 if (!(newrgn = Alloc(sizeof(RANGE)))) goto fail;
3367 newrgn->lower = chkrgn->lower;
3368 newrgn->upper = range.lower;
3369 chkrgn->lower = range.upper;
3370 if (DPA_InsertPtr(ranges->hdpa, index, newrgn) == -1)
3372 Free(newrgn);
3373 goto fail;
3375 break;
3378 index = DPA_Search(ranges->hdpa, &range, index, ranges_cmp, 0, 0);
3381 ranges_check(ranges, "after del");
3382 return TRUE;
3384 fail:
3385 ranges_check(ranges, "failed del");
3386 return FALSE;
3389 /***
3390 * DESCRIPTION:
3391 * Removes all selection ranges
3393 * Parameters(s):
3394 * [I] infoPtr : valid pointer to the listview structure
3395 * [I] toSkip : item range to skip removing the selection
3397 * RETURNS:
3398 * SUCCESS : TRUE
3399 * FAILURE : FALSE
3401 static BOOL LISTVIEW_DeselectAllSkipItems(LISTVIEW_INFO *infoPtr, RANGES toSkip)
3403 LVITEMW lvItem;
3404 ITERATOR i;
3405 RANGES clone;
3407 TRACE("()\n");
3409 lvItem.state = 0;
3410 lvItem.stateMask = LVIS_SELECTED;
3412 /* need to clone the DPA because callbacks can change it */
3413 if (!(clone = ranges_clone(infoPtr->selectionRanges))) return FALSE;
3414 iterator_rangesitems(&i, ranges_diff(clone, toSkip));
3415 while(iterator_next(&i))
3416 LISTVIEW_SetItemState(infoPtr, i.nItem, &lvItem);
3417 /* note that the iterator destructor will free the cloned range */
3418 iterator_destroy(&i);
3420 return TRUE;
3423 static inline BOOL LISTVIEW_DeselectAllSkipItem(LISTVIEW_INFO *infoPtr, INT nItem)
3425 RANGES toSkip;
3427 if (!(toSkip = ranges_create(1))) return FALSE;
3428 if (nItem != -1) ranges_additem(toSkip, nItem);
3429 LISTVIEW_DeselectAllSkipItems(infoPtr, toSkip);
3430 ranges_destroy(toSkip);
3431 return TRUE;
3434 static inline BOOL LISTVIEW_DeselectAll(LISTVIEW_INFO *infoPtr)
3436 return LISTVIEW_DeselectAllSkipItem(infoPtr, -1);
3439 /***
3440 * DESCRIPTION:
3441 * Retrieves the number of items that are marked as selected.
3443 * PARAMETER(S):
3444 * [I] infoPtr : valid pointer to the listview structure
3446 * RETURN:
3447 * Number of items selected.
3449 static INT LISTVIEW_GetSelectedCount(const LISTVIEW_INFO *infoPtr)
3451 INT nSelectedCount = 0;
3453 if (infoPtr->uCallbackMask & LVIS_SELECTED)
3455 INT i;
3456 for (i = 0; i < infoPtr->nItemCount; i++)
3458 if (LISTVIEW_GetItemState(infoPtr, i, LVIS_SELECTED))
3459 nSelectedCount++;
3462 else
3463 nSelectedCount = ranges_itemcount(infoPtr->selectionRanges);
3465 TRACE("nSelectedCount=%d\n", nSelectedCount);
3466 return nSelectedCount;
3469 /***
3470 * DESCRIPTION:
3471 * Manages the item focus.
3473 * PARAMETER(S):
3474 * [I] infoPtr : valid pointer to the listview structure
3475 * [I] nItem : item index
3477 * RETURN:
3478 * TRUE : focused item changed
3479 * FALSE : focused item has NOT changed
3481 static inline BOOL LISTVIEW_SetItemFocus(LISTVIEW_INFO *infoPtr, INT nItem)
3483 INT oldFocus = infoPtr->nFocusedItem;
3484 LVITEMW lvItem;
3486 if (nItem == infoPtr->nFocusedItem) return FALSE;
3488 lvItem.state = nItem == -1 ? 0 : LVIS_FOCUSED;
3489 lvItem.stateMask = LVIS_FOCUSED;
3490 LISTVIEW_SetItemState(infoPtr, nItem == -1 ? infoPtr->nFocusedItem : nItem, &lvItem);
3492 return oldFocus != infoPtr->nFocusedItem;
3495 static INT shift_item(const LISTVIEW_INFO *infoPtr, INT nShiftItem, INT nItem, INT direction)
3497 if (nShiftItem < nItem) return nShiftItem;
3499 if (nShiftItem > nItem) return nShiftItem + direction;
3501 if (direction > 0) return nShiftItem + direction;
3503 return min(nShiftItem, infoPtr->nItemCount - 1);
3506 /* This function updates focus index.
3508 Parameters:
3509 focus : current focus index
3510 item : index of item to be added/removed
3511 direction : add/remove flag
3513 static void LISTVIEW_ShiftFocus(LISTVIEW_INFO *infoPtr, INT focus, INT item, INT direction)
3515 DWORD old_mask = infoPtr->notify_mask & NOTIFY_MASK_ITEM_CHANGE;
3517 infoPtr->notify_mask &= ~NOTIFY_MASK_ITEM_CHANGE;
3518 focus = shift_item(infoPtr, focus, item, direction);
3519 if (focus != infoPtr->nFocusedItem)
3520 LISTVIEW_SetItemFocus(infoPtr, focus);
3521 infoPtr->notify_mask |= old_mask;
3525 * DESCRIPTION:
3526 * Updates the various indices after an item has been inserted or deleted.
3528 * PARAMETER(S):
3529 * [I] infoPtr : valid pointer to the listview structure
3530 * [I] nItem : item index
3531 * [I] direction : Direction of shift, +1 or -1.
3533 * RETURN:
3534 * None
3536 static void LISTVIEW_ShiftIndices(LISTVIEW_INFO *infoPtr, INT nItem, INT direction)
3538 TRACE("Shifting %i, %i steps\n", nItem, direction);
3540 ranges_shift(infoPtr->selectionRanges, nItem, direction, infoPtr->nItemCount);
3541 assert(abs(direction) == 1);
3542 infoPtr->nSelectionMark = shift_item(infoPtr, infoPtr->nSelectionMark, nItem, direction);
3544 /* But we are not supposed to modify nHotItem! */
3548 * DESCRIPTION:
3549 * Adds a block of selections.
3551 * PARAMETER(S):
3552 * [I] infoPtr : valid pointer to the listview structure
3553 * [I] nItem : item index
3555 * RETURN:
3556 * Whether the window is still valid.
3558 static BOOL LISTVIEW_AddGroupSelection(LISTVIEW_INFO *infoPtr, INT nItem)
3560 INT nFirst = min(infoPtr->nSelectionMark, nItem);
3561 INT nLast = max(infoPtr->nSelectionMark, nItem);
3562 HWND hwndSelf = infoPtr->hwndSelf;
3563 NMLVODSTATECHANGE nmlv;
3564 DWORD old_mask;
3565 LVITEMW item;
3566 INT i;
3568 /* Temporarily disable change notification
3569 * If the control is LVS_OWNERDATA, we need to send
3570 * only one LVN_ODSTATECHANGED notification.
3571 * See MSDN documentation for LVN_ITEMCHANGED.
3573 old_mask = infoPtr->notify_mask & NOTIFY_MASK_ITEM_CHANGE;
3574 if (infoPtr->dwStyle & LVS_OWNERDATA)
3575 infoPtr->notify_mask &= ~NOTIFY_MASK_ITEM_CHANGE;
3577 if (nFirst == -1) nFirst = nItem;
3579 item.state = LVIS_SELECTED;
3580 item.stateMask = LVIS_SELECTED;
3582 for (i = nFirst; i <= nLast; i++)
3583 LISTVIEW_SetItemState(infoPtr,i,&item);
3585 ZeroMemory(&nmlv, sizeof(nmlv));
3586 nmlv.iFrom = nFirst;
3587 nmlv.iTo = nLast;
3588 nmlv.uOldState = 0;
3589 nmlv.uNewState = item.state;
3591 notify_hdr(infoPtr, LVN_ODSTATECHANGED, (LPNMHDR)&nmlv);
3592 if (!IsWindow(hwndSelf))
3593 return FALSE;
3594 infoPtr->notify_mask |= old_mask;
3595 return TRUE;
3599 /***
3600 * DESCRIPTION:
3601 * Sets a single group selection.
3603 * PARAMETER(S):
3604 * [I] infoPtr : valid pointer to the listview structure
3605 * [I] nItem : item index
3607 * RETURN:
3608 * None
3610 static void LISTVIEW_SetGroupSelection(LISTVIEW_INFO *infoPtr, INT nItem)
3612 RANGES selection;
3613 DWORD old_mask;
3614 LVITEMW item;
3615 ITERATOR i;
3617 if (!(selection = ranges_create(100))) return;
3619 item.state = LVIS_SELECTED;
3620 item.stateMask = LVIS_SELECTED;
3622 if ((infoPtr->uView == LV_VIEW_LIST) || (infoPtr->uView == LV_VIEW_DETAILS))
3624 if (infoPtr->nSelectionMark == -1)
3626 infoPtr->nSelectionMark = nItem;
3627 ranges_additem(selection, nItem);
3629 else
3631 RANGE sel;
3633 sel.lower = min(infoPtr->nSelectionMark, nItem);
3634 sel.upper = max(infoPtr->nSelectionMark, nItem) + 1;
3635 ranges_add(selection, sel);
3638 else
3640 RECT rcItem, rcSel, rcSelMark;
3641 POINT ptItem;
3643 rcItem.left = LVIR_BOUNDS;
3644 if (!LISTVIEW_GetItemRect(infoPtr, nItem, &rcItem)) {
3645 ranges_destroy (selection);
3646 return;
3648 rcSelMark.left = LVIR_BOUNDS;
3649 if (!LISTVIEW_GetItemRect(infoPtr, infoPtr->nSelectionMark, &rcSelMark)) {
3650 ranges_destroy (selection);
3651 return;
3653 UnionRect(&rcSel, &rcItem, &rcSelMark);
3654 iterator_frameditems(&i, infoPtr, &rcSel);
3655 while(iterator_next(&i))
3657 LISTVIEW_GetItemPosition(infoPtr, i.nItem, &ptItem);
3658 if (PtInRect(&rcSel, ptItem)) ranges_additem(selection, i.nItem);
3660 iterator_destroy(&i);
3663 /* disable per item notifications on LVS_OWNERDATA style
3664 FIXME: single LVN_ODSTATECHANGED should be used */
3665 old_mask = infoPtr->notify_mask & NOTIFY_MASK_ITEM_CHANGE;
3666 if (infoPtr->dwStyle & LVS_OWNERDATA)
3667 infoPtr->notify_mask &= ~NOTIFY_MASK_ITEM_CHANGE;
3669 LISTVIEW_DeselectAllSkipItems(infoPtr, selection);
3672 iterator_rangesitems(&i, selection);
3673 while(iterator_next(&i))
3674 LISTVIEW_SetItemState(infoPtr, i.nItem, &item);
3675 /* this will also destroy the selection */
3676 iterator_destroy(&i);
3678 infoPtr->notify_mask |= old_mask;
3679 LISTVIEW_SetItemFocus(infoPtr, nItem);
3682 /***
3683 * DESCRIPTION:
3684 * Sets a single selection.
3686 * PARAMETER(S):
3687 * [I] infoPtr : valid pointer to the listview structure
3688 * [I] nItem : item index
3690 * RETURN:
3691 * None
3693 static void LISTVIEW_SetSelection(LISTVIEW_INFO *infoPtr, INT nItem)
3695 LVITEMW lvItem;
3697 TRACE("nItem=%d\n", nItem);
3699 LISTVIEW_DeselectAllSkipItem(infoPtr, nItem);
3701 lvItem.state = LVIS_FOCUSED | LVIS_SELECTED;
3702 lvItem.stateMask = LVIS_FOCUSED | LVIS_SELECTED;
3703 LISTVIEW_SetItemState(infoPtr, nItem, &lvItem);
3705 infoPtr->nSelectionMark = nItem;
3708 /***
3709 * DESCRIPTION:
3710 * Set selection(s) with keyboard.
3712 * PARAMETER(S):
3713 * [I] infoPtr : valid pointer to the listview structure
3714 * [I] nItem : item index
3715 * [I] space : VK_SPACE code sent
3717 * RETURN:
3718 * SUCCESS : TRUE (needs to be repainted)
3719 * FAILURE : FALSE (nothing has changed)
3721 static BOOL LISTVIEW_KeySelection(LISTVIEW_INFO *infoPtr, INT nItem, BOOL space)
3723 /* FIXME: pass in the state */
3724 WORD wShift = GetKeyState(VK_SHIFT) & 0x8000;
3725 WORD wCtrl = GetKeyState(VK_CONTROL) & 0x8000;
3726 BOOL bResult = FALSE;
3728 TRACE("nItem=%d, wShift=%d, wCtrl=%d\n", nItem, wShift, wCtrl);
3729 if ((nItem >= 0) && (nItem < infoPtr->nItemCount))
3731 bResult = TRUE;
3733 if (infoPtr->dwStyle & LVS_SINGLESEL || (wShift == 0 && wCtrl == 0))
3734 LISTVIEW_SetSelection(infoPtr, nItem);
3735 else
3737 if (wShift)
3738 LISTVIEW_SetGroupSelection(infoPtr, nItem);
3739 else if (wCtrl)
3741 LVITEMW lvItem;
3742 lvItem.state = ~LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED);
3743 lvItem.stateMask = LVIS_SELECTED;
3744 if (space)
3746 LISTVIEW_SetItemState(infoPtr, nItem, &lvItem);
3747 if (lvItem.state & LVIS_SELECTED)
3748 infoPtr->nSelectionMark = nItem;
3750 bResult = LISTVIEW_SetItemFocus(infoPtr, nItem);
3753 LISTVIEW_EnsureVisible(infoPtr, nItem, FALSE);
3756 UpdateWindow(infoPtr->hwndSelf); /* update client area */
3757 return bResult;
3760 static BOOL LISTVIEW_GetItemAtPt(const LISTVIEW_INFO *infoPtr, LPLVITEMW lpLVItem, POINT pt)
3762 LVHITTESTINFO lvHitTestInfo;
3764 ZeroMemory(&lvHitTestInfo, sizeof(lvHitTestInfo));
3765 lvHitTestInfo.pt.x = pt.x;
3766 lvHitTestInfo.pt.y = pt.y;
3768 LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, FALSE);
3770 lpLVItem->mask = LVIF_PARAM;
3771 lpLVItem->iItem = lvHitTestInfo.iItem;
3772 lpLVItem->iSubItem = 0;
3774 return LISTVIEW_GetItemT(infoPtr, lpLVItem, TRUE);
3777 static inline BOOL LISTVIEW_IsHotTracking(const LISTVIEW_INFO *infoPtr)
3779 return ((infoPtr->dwLvExStyle & LVS_EX_TRACKSELECT) ||
3780 (infoPtr->dwLvExStyle & LVS_EX_ONECLICKACTIVATE) ||
3781 (infoPtr->dwLvExStyle & LVS_EX_TWOCLICKACTIVATE));
3784 /***
3785 * DESCRIPTION:
3786 * Called when the mouse is being actively tracked and has hovered for a specified
3787 * amount of time
3789 * PARAMETER(S):
3790 * [I] infoPtr : valid pointer to the listview structure
3791 * [I] fwKeys : key indicator
3792 * [I] x,y : mouse position
3794 * RETURN:
3795 * 0 if the message was processed, non-zero if there was an error
3797 * INFO:
3798 * LVS_EX_TRACKSELECT: An item is automatically selected when the cursor remains
3799 * over the item for a certain period of time.
3802 static LRESULT LISTVIEW_MouseHover(LISTVIEW_INFO *infoPtr, INT x, INT y)
3804 NMHDR hdr;
3806 if (notify_hdr(infoPtr, NM_HOVER, &hdr)) return 0;
3808 if (LISTVIEW_IsHotTracking(infoPtr))
3810 LVITEMW item;
3811 POINT pt;
3813 pt.x = x;
3814 pt.y = y;
3816 if (LISTVIEW_GetItemAtPt(infoPtr, &item, pt))
3817 LISTVIEW_SetSelection(infoPtr, item.iItem);
3819 SetFocus(infoPtr->hwndSelf);
3822 return 0;
3825 #define SCROLL_LEFT 0x1
3826 #define SCROLL_RIGHT 0x2
3827 #define SCROLL_UP 0x4
3828 #define SCROLL_DOWN 0x8
3830 /***
3831 * DESCRIPTION:
3832 * Utility routine to draw and highlight items within a marquee selection rectangle.
3834 * PARAMETER(S):
3835 * [I] infoPtr : valid pointer to the listview structure
3836 * [I] coords_orig : original co-ordinates of the cursor
3837 * [I] coords_offs : offsetted coordinates of the cursor
3838 * [I] offset : offset amount
3839 * [I] scroll : Bitmask of which directions we should scroll, if at all
3841 * RETURN:
3842 * None.
3844 static void LISTVIEW_MarqueeHighlight(LISTVIEW_INFO *infoPtr, const POINT *coords_orig,
3845 INT scroll)
3847 BOOL controlDown = FALSE;
3848 LVITEMW item;
3849 ITERATOR old_elems, new_elems;
3850 RECT rect;
3851 POINT coords_offs, offset;
3853 /* Ensure coordinates are within client bounds */
3854 coords_offs.x = max(min(coords_orig->x, infoPtr->rcList.right), 0);
3855 coords_offs.y = max(min(coords_orig->y, infoPtr->rcList.bottom), 0);
3857 /* Get offset */
3858 LISTVIEW_GetOrigin(infoPtr, &offset);
3860 /* Offset coordinates by the appropriate amount */
3861 coords_offs.x -= offset.x;
3862 coords_offs.y -= offset.y;
3864 if (coords_offs.x > infoPtr->marqueeOrigin.x)
3866 rect.left = infoPtr->marqueeOrigin.x;
3867 rect.right = coords_offs.x;
3869 else
3871 rect.left = coords_offs.x;
3872 rect.right = infoPtr->marqueeOrigin.x;
3875 if (coords_offs.y > infoPtr->marqueeOrigin.y)
3877 rect.top = infoPtr->marqueeOrigin.y;
3878 rect.bottom = coords_offs.y;
3880 else
3882 rect.top = coords_offs.y;
3883 rect.bottom = infoPtr->marqueeOrigin.y;
3886 /* Cancel out the old marquee rectangle and draw the new one */
3887 LISTVIEW_InvalidateRect(infoPtr, &infoPtr->marqueeDrawRect);
3889 /* Scroll by the appropriate distance if applicable - speed up scrolling as
3890 the cursor is further away */
3892 if ((scroll & SCROLL_LEFT) && (coords_orig->x <= 0))
3893 LISTVIEW_Scroll(infoPtr, coords_orig->x, 0);
3895 if ((scroll & SCROLL_RIGHT) && (coords_orig->x >= infoPtr->rcList.right))
3896 LISTVIEW_Scroll(infoPtr, (coords_orig->x - infoPtr->rcList.right), 0);
3898 if ((scroll & SCROLL_UP) && (coords_orig->y <= 0))
3899 LISTVIEW_Scroll(infoPtr, 0, coords_orig->y);
3901 if ((scroll & SCROLL_DOWN) && (coords_orig->y >= infoPtr->rcList.bottom))
3902 LISTVIEW_Scroll(infoPtr, 0, (coords_orig->y - infoPtr->rcList.bottom));
3904 iterator_frameditems_absolute(&old_elems, infoPtr, &infoPtr->marqueeRect);
3906 infoPtr->marqueeRect = rect;
3907 infoPtr->marqueeDrawRect = rect;
3908 OffsetRect(&infoPtr->marqueeDrawRect, offset.x, offset.y);
3910 iterator_frameditems_absolute(&new_elems, infoPtr, &infoPtr->marqueeRect);
3911 iterator_remove_common_items(&old_elems, &new_elems);
3913 /* Iterate over no longer selected items */
3914 while (iterator_next(&old_elems))
3916 if (old_elems.nItem > -1)
3918 if (LISTVIEW_GetItemState(infoPtr, old_elems.nItem, LVIS_SELECTED) == LVIS_SELECTED)
3919 item.state = 0;
3920 else
3921 item.state = LVIS_SELECTED;
3923 item.stateMask = LVIS_SELECTED;
3925 LISTVIEW_SetItemState(infoPtr, old_elems.nItem, &item);
3928 iterator_destroy(&old_elems);
3931 /* Iterate over newly selected items */
3932 if (GetKeyState(VK_CONTROL) & 0x8000)
3933 controlDown = TRUE;
3935 while (iterator_next(&new_elems))
3937 if (new_elems.nItem > -1)
3939 /* If CTRL is pressed, invert. If not, always select the item. */
3940 if ((controlDown) && (LISTVIEW_GetItemState(infoPtr, new_elems.nItem, LVIS_SELECTED)))
3941 item.state = 0;
3942 else
3943 item.state = LVIS_SELECTED;
3945 item.stateMask = LVIS_SELECTED;
3947 LISTVIEW_SetItemState(infoPtr, new_elems.nItem, &item);
3950 iterator_destroy(&new_elems);
3952 LISTVIEW_InvalidateRect(infoPtr, &infoPtr->marqueeDrawRect);
3955 /***
3956 * DESCRIPTION:
3957 * Called when we are in a marquee selection that involves scrolling the listview (ie,
3958 * the cursor is outside the bounds of the client area). This is a TIMERPROC.
3960 * PARAMETER(S):
3961 * [I] hwnd : Handle to the listview
3962 * [I] uMsg : WM_TIMER (ignored)
3963 * [I] idEvent : The timer ID interpreted as a pointer to a LISTVIEW_INFO struct
3964 * [I] dwTimer : The elapsed time (ignored)
3966 * RETURN:
3967 * None.
3969 static VOID CALLBACK LISTVIEW_ScrollTimer(HWND hWnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime)
3971 LISTVIEW_INFO *infoPtr;
3972 SCROLLINFO scrollInfo;
3973 POINT coords;
3974 INT scroll = 0;
3976 infoPtr = (LISTVIEW_INFO *) idEvent;
3978 if (!infoPtr)
3979 return;
3981 /* Get the current cursor position and convert to client coordinates */
3982 GetCursorPos(&coords);
3983 ScreenToClient(hWnd, &coords);
3985 scrollInfo.cbSize = sizeof(SCROLLINFO);
3986 scrollInfo.fMask = SIF_ALL;
3988 /* Work out in which directions we can scroll */
3989 if (GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo))
3991 if (scrollInfo.nPos != scrollInfo.nMin)
3992 scroll |= SCROLL_UP;
3994 if (((scrollInfo.nPage + scrollInfo.nPos) - 1) != scrollInfo.nMax)
3995 scroll |= SCROLL_DOWN;
3998 if (GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo))
4000 if (scrollInfo.nPos != scrollInfo.nMin)
4001 scroll |= SCROLL_LEFT;
4003 if (((scrollInfo.nPage + scrollInfo.nPos) - 1) != scrollInfo.nMax)
4004 scroll |= SCROLL_RIGHT;
4007 if (((coords.x <= 0) && (scroll & SCROLL_LEFT)) ||
4008 ((coords.y <= 0) && (scroll & SCROLL_UP)) ||
4009 ((coords.x >= infoPtr->rcList.right) && (scroll & SCROLL_RIGHT)) ||
4010 ((coords.y >= infoPtr->rcList.bottom) && (scroll & SCROLL_DOWN)))
4012 LISTVIEW_MarqueeHighlight(infoPtr, &coords, scroll);
4016 /***
4017 * DESCRIPTION:
4018 * Called whenever WM_MOUSEMOVE is received.
4020 * PARAMETER(S):
4021 * [I] infoPtr : valid pointer to the listview structure
4022 * [I] fwKeys : key indicator
4023 * [I] x,y : mouse position
4025 * RETURN:
4026 * 0 if the message is processed, non-zero if there was an error
4028 static LRESULT LISTVIEW_MouseMove(LISTVIEW_INFO *infoPtr, WORD fwKeys, INT x, INT y)
4030 LVHITTESTINFO ht;
4031 RECT rect;
4032 POINT pt;
4034 pt.x = x;
4035 pt.y = y;
4037 if (!(fwKeys & MK_LBUTTON))
4038 infoPtr->bLButtonDown = FALSE;
4040 if (infoPtr->bLButtonDown)
4042 rect.left = rect.right = infoPtr->ptClickPos.x;
4043 rect.top = rect.bottom = infoPtr->ptClickPos.y;
4045 InflateRect(&rect, GetSystemMetrics(SM_CXDRAG), GetSystemMetrics(SM_CYDRAG));
4047 if (infoPtr->bMarqueeSelect)
4049 /* Enable the timer if we're going outside our bounds, in case the user doesn't
4050 move the mouse again */
4052 if ((x <= 0) || (y <= 0) || (x >= infoPtr->rcList.right) ||
4053 (y >= infoPtr->rcList.bottom))
4055 if (!infoPtr->bScrolling)
4057 infoPtr->bScrolling = TRUE;
4058 SetTimer(infoPtr->hwndSelf, (UINT_PTR) infoPtr, 1, LISTVIEW_ScrollTimer);
4061 else
4063 infoPtr->bScrolling = FALSE;
4064 KillTimer(infoPtr->hwndSelf, (UINT_PTR) infoPtr);
4067 LISTVIEW_MarqueeHighlight(infoPtr, &pt, 0);
4068 return 0;
4071 ht.pt = pt;
4072 LISTVIEW_HitTest(infoPtr, &ht, TRUE, TRUE);
4074 /* reset item marker */
4075 if (infoPtr->nLButtonDownItem != ht.iItem)
4076 infoPtr->nLButtonDownItem = -1;
4078 if (!PtInRect(&rect, pt))
4080 /* this path covers the following:
4081 1. WM_LBUTTONDOWN over selected item (sets focus on it)
4082 2. change focus with keys
4083 3. move mouse over item from step 1 selects it and moves focus on it */
4084 if (infoPtr->nLButtonDownItem != -1 &&
4085 !LISTVIEW_GetItemState(infoPtr, infoPtr->nLButtonDownItem, LVIS_SELECTED))
4087 LVITEMW lvItem;
4089 lvItem.state = LVIS_FOCUSED | LVIS_SELECTED;
4090 lvItem.stateMask = LVIS_FOCUSED | LVIS_SELECTED;
4092 LISTVIEW_SetItemState(infoPtr, infoPtr->nLButtonDownItem, &lvItem);
4093 infoPtr->nLButtonDownItem = -1;
4096 if (!infoPtr->bDragging)
4098 ht.pt = infoPtr->ptClickPos;
4099 LISTVIEW_HitTest(infoPtr, &ht, TRUE, TRUE);
4101 /* If the click is outside the range of an item, begin a
4102 highlight. If not, begin an item drag. */
4103 if (ht.iItem == -1)
4105 NMHDR hdr;
4107 /* If we're allowing multiple selections, send notification.
4108 If return value is non-zero, cancel. */
4109 if (!(infoPtr->dwStyle & LVS_SINGLESEL) && (notify_hdr(infoPtr, LVN_MARQUEEBEGIN, &hdr) == 0))
4111 /* Store the absolute coordinates of the click */
4112 POINT offset;
4113 LISTVIEW_GetOrigin(infoPtr, &offset);
4115 infoPtr->marqueeOrigin.x = infoPtr->ptClickPos.x - offset.x;
4116 infoPtr->marqueeOrigin.y = infoPtr->ptClickPos.y - offset.y;
4118 /* Begin selection and capture mouse */
4119 infoPtr->bMarqueeSelect = TRUE;
4120 SetCapture(infoPtr->hwndSelf);
4123 else
4125 NMLISTVIEW nmlv;
4127 ZeroMemory(&nmlv, sizeof(nmlv));
4128 nmlv.iItem = ht.iItem;
4129 nmlv.ptAction = infoPtr->ptClickPos;
4131 notify_listview(infoPtr, LVN_BEGINDRAG, &nmlv);
4132 infoPtr->bDragging = TRUE;
4136 return 0;
4140 /* see if we are supposed to be tracking mouse hovering */
4141 if (LISTVIEW_IsHotTracking(infoPtr)) {
4142 TRACKMOUSEEVENT trackinfo;
4143 DWORD flags;
4145 trackinfo.cbSize = sizeof(TRACKMOUSEEVENT);
4146 trackinfo.dwFlags = TME_QUERY;
4148 /* see if we are already tracking this hwnd */
4149 _TrackMouseEvent(&trackinfo);
4151 flags = TME_LEAVE;
4152 if(infoPtr->dwLvExStyle & LVS_EX_TRACKSELECT)
4153 flags |= TME_HOVER;
4155 if((trackinfo.dwFlags & flags) != flags || trackinfo.hwndTrack != infoPtr->hwndSelf) {
4156 trackinfo.dwFlags = flags;
4157 trackinfo.dwHoverTime = infoPtr->dwHoverTime;
4158 trackinfo.hwndTrack = infoPtr->hwndSelf;
4160 /* call TRACKMOUSEEVENT so we receive WM_MOUSEHOVER messages */
4161 _TrackMouseEvent(&trackinfo);
4165 return 0;
4169 /***
4170 * Tests whether the item is assignable to a list with style lStyle
4172 static inline BOOL is_assignable_item(const LVITEMW *lpLVItem, LONG lStyle)
4174 if ( (lpLVItem->mask & LVIF_TEXT) &&
4175 (lpLVItem->pszText == LPSTR_TEXTCALLBACKW) &&
4176 (lStyle & (LVS_SORTASCENDING | LVS_SORTDESCENDING)) ) return FALSE;
4178 return TRUE;
4182 /***
4183 * DESCRIPTION:
4184 * Helper for LISTVIEW_SetItemT and LISTVIEW_InsertItemT: sets item attributes.
4186 * PARAMETER(S):
4187 * [I] infoPtr : valid pointer to the listview structure
4188 * [I] lpLVItem : valid pointer to new item attributes
4189 * [I] isNew : the item being set is being inserted
4190 * [I] isW : TRUE if lpLVItem is Unicode, FALSE if it's ANSI
4191 * [O] bChanged : will be set to TRUE if the item really changed
4193 * RETURN:
4194 * SUCCESS : TRUE
4195 * FAILURE : FALSE
4197 static BOOL set_main_item(LISTVIEW_INFO *infoPtr, const LVITEMW *lpLVItem, BOOL isNew, BOOL isW, BOOL *bChanged)
4199 ITEM_INFO *lpItem;
4200 NMLISTVIEW nmlv;
4201 UINT uChanged = 0;
4202 LVITEMW item;
4203 /* stateMask is ignored for LVM_INSERTITEM */
4204 UINT stateMask = isNew ? ~0 : lpLVItem->stateMask;
4206 TRACE("()\n");
4208 assert(lpLVItem->iItem >= 0 && lpLVItem->iItem < infoPtr->nItemCount);
4210 if (lpLVItem->mask == 0) return TRUE;
4212 if (infoPtr->dwStyle & LVS_OWNERDATA)
4214 /* a virtual listview only stores selection and focus */
4215 if (lpLVItem->mask & ~LVIF_STATE)
4216 return FALSE;
4217 lpItem = NULL;
4219 else
4221 HDPA hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, lpLVItem->iItem);
4222 lpItem = DPA_GetPtr(hdpaSubItems, 0);
4223 assert (lpItem);
4226 /* we need to get the lParam and state of the item */
4227 item.iItem = lpLVItem->iItem;
4228 item.iSubItem = lpLVItem->iSubItem;
4229 item.mask = LVIF_STATE | LVIF_PARAM;
4230 item.stateMask = (infoPtr->dwStyle & LVS_OWNERDATA) ? LVIS_FOCUSED | LVIS_SELECTED : ~0;
4232 item.state = 0;
4233 item.lParam = 0;
4234 if (!isNew && !LISTVIEW_GetItemW(infoPtr, &item)) return FALSE;
4236 TRACE("oldState=%x, newState=%x\n", item.state, lpLVItem->state);
4237 /* determine what fields will change */
4238 if ((lpLVItem->mask & LVIF_STATE) && ((item.state ^ lpLVItem->state) & stateMask & ~infoPtr->uCallbackMask))
4239 uChanged |= LVIF_STATE;
4241 if ((lpLVItem->mask & LVIF_IMAGE) && (lpItem->hdr.iImage != lpLVItem->iImage))
4242 uChanged |= LVIF_IMAGE;
4244 if ((lpLVItem->mask & LVIF_PARAM) && (lpItem->lParam != lpLVItem->lParam))
4245 uChanged |= LVIF_PARAM;
4247 if ((lpLVItem->mask & LVIF_INDENT) && (lpItem->iIndent != lpLVItem->iIndent))
4248 uChanged |= LVIF_INDENT;
4250 if ((lpLVItem->mask & LVIF_TEXT) && textcmpWT(lpItem->hdr.pszText, lpLVItem->pszText, isW))
4251 uChanged |= LVIF_TEXT;
4253 TRACE("change mask=0x%x\n", uChanged);
4255 memset(&nmlv, 0, sizeof(NMLISTVIEW));
4256 nmlv.iItem = lpLVItem->iItem;
4257 if (lpLVItem->mask & LVIF_STATE)
4259 nmlv.uNewState = (item.state & ~stateMask) | (lpLVItem->state & stateMask);
4260 nmlv.uOldState = item.state;
4262 nmlv.uChanged = uChanged ? uChanged : lpLVItem->mask;
4263 nmlv.lParam = item.lParam;
4265 /* Send LVN_ITEMCHANGING notification, if the item is not being inserted
4266 and we are _NOT_ virtual (LVS_OWNERDATA), and change notifications
4267 are enabled. Even nothing really changed we still need to send this,
4268 in this case uChanged mask is just set to passed item mask. */
4269 if (lpItem && !isNew && (infoPtr->notify_mask & NOTIFY_MASK_ITEM_CHANGE))
4271 HWND hwndSelf = infoPtr->hwndSelf;
4273 if (notify_listview(infoPtr, LVN_ITEMCHANGING, &nmlv))
4274 return FALSE;
4275 if (!IsWindow(hwndSelf))
4276 return FALSE;
4279 /* When item is inserted we need to shift existing focus index if new item has lower index. */
4280 if (isNew && (stateMask & ~infoPtr->uCallbackMask & LVIS_FOCUSED) &&
4281 /* this means we won't hit a focus change path later */
4282 ((uChanged & LVIF_STATE) == 0 || (!(lpLVItem->state & LVIS_FOCUSED) && (infoPtr->nFocusedItem != lpLVItem->iItem))))
4284 if (infoPtr->nFocusedItem != -1 && (lpLVItem->iItem <= infoPtr->nFocusedItem))
4285 infoPtr->nFocusedItem++;
4288 if (!uChanged) return TRUE;
4289 *bChanged = TRUE;
4291 /* copy information */
4292 if (lpLVItem->mask & LVIF_TEXT)
4293 textsetptrT(&lpItem->hdr.pszText, lpLVItem->pszText, isW);
4295 if (lpLVItem->mask & LVIF_IMAGE)
4296 lpItem->hdr.iImage = lpLVItem->iImage;
4298 if (lpLVItem->mask & LVIF_PARAM)
4299 lpItem->lParam = lpLVItem->lParam;
4301 if (lpLVItem->mask & LVIF_INDENT)
4302 lpItem->iIndent = lpLVItem->iIndent;
4304 if (uChanged & LVIF_STATE)
4306 if (lpItem && (stateMask & ~infoPtr->uCallbackMask))
4308 lpItem->state &= ~stateMask;
4309 lpItem->state |= (lpLVItem->state & stateMask);
4311 if (lpLVItem->state & stateMask & ~infoPtr->uCallbackMask & LVIS_SELECTED)
4313 if (infoPtr->dwStyle & LVS_SINGLESEL) LISTVIEW_DeselectAllSkipItem(infoPtr, lpLVItem->iItem);
4314 ranges_additem(infoPtr->selectionRanges, lpLVItem->iItem);
4316 else if (stateMask & LVIS_SELECTED)
4318 ranges_delitem(infoPtr->selectionRanges, lpLVItem->iItem);
4320 /* If we are asked to change focus, and we manage it, do it.
4321 It's important to have all new item data stored at this point,
4322 because changing existing focus could result in a redrawing operation,
4323 which in turn could ask for disp data, application should see all data
4324 for inserted item when processing LVN_GETDISPINFO.
4326 The way this works application will see nested item change notifications -
4327 changed item notifications interrupted by ones from item losing focus. */
4328 if (stateMask & ~infoPtr->uCallbackMask & LVIS_FOCUSED)
4330 if (lpLVItem->state & LVIS_FOCUSED)
4332 /* update selection mark */
4333 if (infoPtr->nFocusedItem == -1 && infoPtr->nSelectionMark == -1)
4334 infoPtr->nSelectionMark = lpLVItem->iItem;
4336 if (infoPtr->nFocusedItem != -1)
4338 /* remove current focus */
4339 item.mask = LVIF_STATE;
4340 item.state = 0;
4341 item.stateMask = LVIS_FOCUSED;
4343 /* recurse with redrawing an item */
4344 LISTVIEW_SetItemState(infoPtr, infoPtr->nFocusedItem, &item);
4347 infoPtr->nFocusedItem = lpLVItem->iItem;
4348 LISTVIEW_EnsureVisible(infoPtr, lpLVItem->iItem, infoPtr->uView == LV_VIEW_LIST);
4350 else if (infoPtr->nFocusedItem == lpLVItem->iItem)
4352 infoPtr->nFocusedItem = -1;
4357 /* if we're inserting the item, we're done */
4358 if (isNew) return TRUE;
4360 /* send LVN_ITEMCHANGED notification */
4361 if (lpLVItem->mask & LVIF_PARAM) nmlv.lParam = lpLVItem->lParam;
4362 if (infoPtr->notify_mask & NOTIFY_MASK_ITEM_CHANGE)
4363 notify_listview(infoPtr, LVN_ITEMCHANGED, &nmlv);
4365 return TRUE;
4368 /***
4369 * DESCRIPTION:
4370 * Helper for LISTVIEW_{Set,Insert}ItemT *only*: sets subitem attributes.
4372 * PARAMETER(S):
4373 * [I] infoPtr : valid pointer to the listview structure
4374 * [I] lpLVItem : valid pointer to new subitem attributes
4375 * [I] isW : TRUE if lpLVItem is Unicode, FALSE if it's ANSI
4376 * [O] bChanged : will be set to TRUE if the item really changed
4378 * RETURN:
4379 * SUCCESS : TRUE
4380 * FAILURE : FALSE
4382 static BOOL set_sub_item(const LISTVIEW_INFO *infoPtr, const LVITEMW *lpLVItem, BOOL isW, BOOL *bChanged)
4384 HDPA hdpaSubItems;
4385 SUBITEM_INFO *lpSubItem;
4387 /* we do not support subitems for virtual listviews */
4388 if (infoPtr->dwStyle & LVS_OWNERDATA) return FALSE;
4390 /* set subitem only if column is present */
4391 if (lpLVItem->iSubItem >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return FALSE;
4393 /* First do some sanity checks */
4394 /* The LVIF_STATE flag is valid for subitems, but does not appear to be
4395 particularly useful. We currently do not actually do anything with
4396 the flag on subitems.
4398 if (lpLVItem->mask & ~(LVIF_TEXT | LVIF_IMAGE | LVIF_STATE | LVIF_DI_SETITEM)) return FALSE;
4399 if (!(lpLVItem->mask & (LVIF_TEXT | LVIF_IMAGE | LVIF_STATE))) return TRUE;
4401 /* get the subitem structure, and create it if not there */
4402 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, lpLVItem->iItem);
4403 assert (hdpaSubItems);
4405 lpSubItem = LISTVIEW_GetSubItemPtr(hdpaSubItems, lpLVItem->iSubItem);
4406 if (!lpSubItem)
4408 SUBITEM_INFO *tmpSubItem;
4409 INT i;
4411 lpSubItem = Alloc(sizeof(SUBITEM_INFO));
4412 if (!lpSubItem) return FALSE;
4413 /* we could binary search here, if need be...*/
4414 for (i = 1; i < DPA_GetPtrCount(hdpaSubItems); i++)
4416 tmpSubItem = DPA_GetPtr(hdpaSubItems, i);
4417 if (tmpSubItem->iSubItem > lpLVItem->iSubItem) break;
4419 if (DPA_InsertPtr(hdpaSubItems, i, lpSubItem) == -1)
4421 Free(lpSubItem);
4422 return FALSE;
4424 lpSubItem->iSubItem = lpLVItem->iSubItem;
4425 lpSubItem->hdr.iImage = I_IMAGECALLBACK;
4426 *bChanged = TRUE;
4429 if ((lpLVItem->mask & LVIF_IMAGE) && (lpSubItem->hdr.iImage != lpLVItem->iImage))
4431 lpSubItem->hdr.iImage = lpLVItem->iImage;
4432 *bChanged = TRUE;
4435 if ((lpLVItem->mask & LVIF_TEXT) && textcmpWT(lpSubItem->hdr.pszText, lpLVItem->pszText, isW))
4437 textsetptrT(&lpSubItem->hdr.pszText, lpLVItem->pszText, isW);
4438 *bChanged = TRUE;
4441 return TRUE;
4444 /***
4445 * DESCRIPTION:
4446 * Sets item attributes.
4448 * PARAMETER(S):
4449 * [I] infoPtr : valid pointer to the listview structure
4450 * [I] lpLVItem : new item attributes
4451 * [I] isW : TRUE if lpLVItem is Unicode, FALSE if it's ANSI
4453 * RETURN:
4454 * SUCCESS : TRUE
4455 * FAILURE : FALSE
4457 static BOOL LISTVIEW_SetItemT(LISTVIEW_INFO *infoPtr, LVITEMW *lpLVItem, BOOL isW)
4459 HWND hwndSelf = infoPtr->hwndSelf;
4460 LPWSTR pszText = NULL;
4461 BOOL bResult, bChanged = FALSE;
4462 RECT oldItemArea;
4464 TRACE("(lpLVItem=%s, isW=%d)\n", debuglvitem_t(lpLVItem, isW), isW);
4466 if (!lpLVItem || lpLVItem->iItem < 0 || lpLVItem->iItem >= infoPtr->nItemCount)
4467 return FALSE;
4469 /* Store old item area */
4470 LISTVIEW_GetItemBox(infoPtr, lpLVItem->iItem, &oldItemArea);
4472 /* For efficiency, we transform the lpLVItem->pszText to Unicode here */
4473 if ((lpLVItem->mask & LVIF_TEXT) && is_text(lpLVItem->pszText))
4475 pszText = lpLVItem->pszText;
4476 lpLVItem->pszText = textdupTtoW(lpLVItem->pszText, isW);
4479 /* actually set the fields */
4480 if (!is_assignable_item(lpLVItem, infoPtr->dwStyle)) return FALSE;
4482 if (lpLVItem->iSubItem)
4483 bResult = set_sub_item(infoPtr, lpLVItem, TRUE, &bChanged);
4484 else
4485 bResult = set_main_item(infoPtr, lpLVItem, FALSE, TRUE, &bChanged);
4486 if (!IsWindow(hwndSelf))
4487 return FALSE;
4489 /* redraw item, if necessary */
4490 if (bChanged && !infoPtr->bIsDrawing)
4492 /* this little optimization eliminates some nasty flicker */
4493 if ( infoPtr->uView == LV_VIEW_DETAILS && !(infoPtr->dwStyle & LVS_OWNERDRAWFIXED) &&
4494 !(infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT) &&
4495 lpLVItem->iSubItem > 0 && lpLVItem->iSubItem <= DPA_GetPtrCount(infoPtr->hdpaColumns) )
4496 LISTVIEW_InvalidateSubItem(infoPtr, lpLVItem->iItem, lpLVItem->iSubItem);
4497 else
4499 LISTVIEW_InvalidateRect(infoPtr, &oldItemArea);
4500 LISTVIEW_InvalidateItem(infoPtr, lpLVItem->iItem);
4503 /* restore text */
4504 if (pszText)
4506 textfreeT(lpLVItem->pszText, isW);
4507 lpLVItem->pszText = pszText;
4510 return bResult;
4513 /***
4514 * DESCRIPTION:
4515 * Retrieves the index of the item at coordinate (0, 0) of the client area.
4517 * PARAMETER(S):
4518 * [I] infoPtr : valid pointer to the listview structure
4520 * RETURN:
4521 * item index
4523 static INT LISTVIEW_GetTopIndex(const LISTVIEW_INFO *infoPtr)
4525 INT nItem = 0;
4526 SCROLLINFO scrollInfo;
4528 scrollInfo.cbSize = sizeof(SCROLLINFO);
4529 scrollInfo.fMask = SIF_POS;
4531 if (infoPtr->uView == LV_VIEW_LIST)
4533 if (GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo))
4534 nItem = scrollInfo.nPos * LISTVIEW_GetCountPerColumn(infoPtr);
4536 else if (infoPtr->uView == LV_VIEW_DETAILS)
4538 if (GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo))
4539 nItem = scrollInfo.nPos;
4541 else
4543 if (GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo))
4544 nItem = LISTVIEW_GetCountPerRow(infoPtr) * (scrollInfo.nPos / infoPtr->nItemHeight);
4547 TRACE("nItem=%d\n", nItem);
4549 return nItem;
4553 /***
4554 * DESCRIPTION:
4555 * Erases the background of the given rectangle
4557 * PARAMETER(S):
4558 * [I] infoPtr : valid pointer to the listview structure
4559 * [I] hdc : device context handle
4560 * [I] lprcBox : clipping rectangle
4562 * RETURN:
4563 * Success: TRUE
4564 * Failure: FALSE
4566 static inline BOOL LISTVIEW_FillBkgnd(const LISTVIEW_INFO *infoPtr, HDC hdc, const RECT *lprcBox)
4568 if (!infoPtr->hBkBrush) return FALSE;
4570 TRACE("(hdc=%p, lprcBox=%s, hBkBrush=%p)\n", hdc, wine_dbgstr_rect(lprcBox), infoPtr->hBkBrush);
4572 return FillRect(hdc, lprcBox, infoPtr->hBkBrush);
4575 /* Draw main item or subitem */
4576 static void LISTVIEW_DrawItemPart(LISTVIEW_INFO *infoPtr, LVITEMW *item, const NMLVCUSTOMDRAW *nmlvcd, const POINT *pos)
4578 RECT rcSelect, rcLabel, rcBox, rcStateIcon, rcIcon;
4579 const RECT *background;
4580 HIMAGELIST himl;
4581 UINT format;
4582 RECT *focus;
4584 /* now check if we need to update the focus rectangle */
4585 focus = infoPtr->bFocus && (item->state & LVIS_FOCUSED) ? &infoPtr->rcFocus : 0;
4586 if (!focus) item->state &= ~LVIS_FOCUSED;
4588 LISTVIEW_GetItemMetrics(infoPtr, item, &rcBox, &rcSelect, &rcIcon, &rcStateIcon, &rcLabel);
4589 OffsetRect(&rcBox, pos->x, pos->y);
4590 OffsetRect(&rcSelect, pos->x, pos->y);
4591 OffsetRect(&rcIcon, pos->x, pos->y);
4592 OffsetRect(&rcStateIcon, pos->x, pos->y);
4593 OffsetRect(&rcLabel, pos->x, pos->y);
4594 TRACE("%d: box=%s, select=%s, icon=%s. label=%s\n", item->iSubItem,
4595 wine_dbgstr_rect(&rcBox), wine_dbgstr_rect(&rcSelect),
4596 wine_dbgstr_rect(&rcIcon), wine_dbgstr_rect(&rcLabel));
4598 /* FIXME: temporary hack */
4599 rcSelect.left = rcLabel.left;
4601 if (infoPtr->uView == LV_VIEW_DETAILS && item->iSubItem == 0)
4603 if (!(infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT))
4604 OffsetRect(&rcSelect, LISTVIEW_GetColumnInfo(infoPtr, 0)->rcHeader.left, 0);
4605 OffsetRect(&rcIcon, LISTVIEW_GetColumnInfo(infoPtr, 0)->rcHeader.left, 0);
4606 OffsetRect(&rcStateIcon, LISTVIEW_GetColumnInfo(infoPtr, 0)->rcHeader.left, 0);
4607 OffsetRect(&rcLabel, LISTVIEW_GetColumnInfo(infoPtr, 0)->rcHeader.left, 0);
4610 /* in icon mode, the label rect is really what we want to draw the
4611 * background for */
4612 /* in detail mode, we want to paint background for label rect when
4613 * item is not selected or listview has full row select; otherwise paint
4614 * background for text only */
4615 if ( infoPtr->uView == LV_VIEW_ICON ||
4616 (infoPtr->uView == LV_VIEW_DETAILS && (!(item->state & LVIS_SELECTED) ||
4617 (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT))))
4618 background = &rcLabel;
4619 else
4620 background = &rcSelect;
4622 if (nmlvcd->clrTextBk != CLR_NONE)
4623 ExtTextOutW(nmlvcd->nmcd.hdc, background->left, background->top, ETO_OPAQUE, background, NULL, 0, NULL);
4625 if (item->state & LVIS_FOCUSED)
4627 if (infoPtr->uView == LV_VIEW_DETAILS)
4629 if (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT)
4631 /* we have to update left focus bound too if item isn't in leftmost column
4632 and reduce right box bound */
4633 if (DPA_GetPtrCount(infoPtr->hdpaColumns) > 0)
4635 INT leftmost;
4637 if ((leftmost = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX, 0, 0)))
4639 INT Originx = pos->x - LISTVIEW_GetColumnInfo(infoPtr, leftmost)->rcHeader.left;
4640 INT rightmost = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX,
4641 DPA_GetPtrCount(infoPtr->hdpaColumns) - 1, 0);
4643 rcBox.right = LISTVIEW_GetColumnInfo(infoPtr, rightmost)->rcHeader.right + Originx;
4644 rcSelect.left = LISTVIEW_GetColumnInfo(infoPtr, leftmost)->rcHeader.left + Originx;
4647 rcSelect.right = rcBox.right;
4649 infoPtr->rcFocus = rcSelect;
4651 else
4652 infoPtr->rcFocus = rcLabel;
4655 /* state icons */
4656 if (infoPtr->himlState && STATEIMAGEINDEX(item->state) && (item->iSubItem == 0))
4658 UINT stateimage = STATEIMAGEINDEX(item->state);
4659 if (stateimage)
4661 TRACE("stateimage=%d\n", stateimage);
4662 ImageList_Draw(infoPtr->himlState, stateimage-1, nmlvcd->nmcd.hdc, rcStateIcon.left, rcStateIcon.top, ILD_NORMAL);
4666 /* item icons */
4667 himl = (infoPtr->uView == LV_VIEW_ICON ? infoPtr->himlNormal : infoPtr->himlSmall);
4668 if (himl && item->iImage >= 0 && !IsRectEmpty(&rcIcon))
4670 UINT style;
4672 TRACE("iImage=%d\n", item->iImage);
4674 if (item->state & (LVIS_SELECTED | LVIS_CUT) && infoPtr->bFocus)
4675 style = ILD_SELECTED;
4676 else
4677 style = ILD_NORMAL;
4679 ImageList_DrawEx(himl, item->iImage, nmlvcd->nmcd.hdc, rcIcon.left, rcIcon.top,
4680 rcIcon.right - rcIcon.left, rcIcon.bottom - rcIcon.top, infoPtr->clrBk,
4681 item->state & LVIS_CUT ? RGB(255, 255, 255) : CLR_DEFAULT,
4682 style | (item->state & LVIS_OVERLAYMASK));
4685 /* Don't bother painting item being edited */
4686 if (infoPtr->hwndEdit && item->iItem == infoPtr->nEditLabelItem && item->iSubItem == 0) return;
4688 /* figure out the text drawing flags */
4689 format = (infoPtr->uView == LV_VIEW_ICON ? (focus ? LV_FL_DT_FLAGS : LV_ML_DT_FLAGS) : LV_SL_DT_FLAGS);
4690 if (infoPtr->uView == LV_VIEW_ICON)
4691 format = (focus ? LV_FL_DT_FLAGS : LV_ML_DT_FLAGS);
4692 else if (item->iSubItem)
4694 switch (LISTVIEW_GetColumnInfo(infoPtr, item->iSubItem)->fmt & LVCFMT_JUSTIFYMASK)
4696 case LVCFMT_RIGHT: format |= DT_RIGHT; break;
4697 case LVCFMT_CENTER: format |= DT_CENTER; break;
4698 default: format |= DT_LEFT;
4701 if (!(format & (DT_RIGHT | DT_CENTER)))
4703 if (himl && item->iImage >= 0 && !IsRectEmpty(&rcIcon)) rcLabel.left += IMAGE_PADDING;
4704 else rcLabel.left += LABEL_HOR_PADDING;
4706 else if (format & DT_RIGHT) rcLabel.right -= LABEL_HOR_PADDING;
4708 /* for GRIDLINES reduce the bottom so the text formats correctly */
4709 if (infoPtr->uView == LV_VIEW_DETAILS && infoPtr->dwLvExStyle & LVS_EX_GRIDLINES)
4710 rcLabel.bottom--;
4712 DrawTextW(nmlvcd->nmcd.hdc, item->pszText, -1, &rcLabel, format);
4715 /***
4716 * DESCRIPTION:
4717 * Draws an item.
4719 * PARAMETER(S):
4720 * [I] infoPtr : valid pointer to the listview structure
4721 * [I] hdc : device context handle
4722 * [I] nItem : item index
4723 * [I] nSubItem : subitem index
4724 * [I] pos : item position in client coordinates
4725 * [I] cdmode : custom draw mode
4727 * RETURN:
4728 * Success: TRUE
4729 * Failure: FALSE
4731 static BOOL LISTVIEW_DrawItem(LISTVIEW_INFO *infoPtr, HDC hdc, INT nItem, ITERATOR *subitems, POINT pos, DWORD cdmode)
4733 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
4734 static WCHAR callbackW[] = { '(', 'c', 'a', 'l', 'l', 'b', 'a', 'c', 'k', ')', 0 };
4735 DWORD cdsubitemmode = CDRF_DODEFAULT;
4736 RECT *focus, rcBox;
4737 NMLVCUSTOMDRAW nmlvcd;
4738 LVITEMW lvItem;
4740 TRACE("(hdc=%p, nItem=%d, subitems=%p, pos=%s)\n", hdc, nItem, subitems, wine_dbgstr_point(&pos));
4742 /* get information needed for drawing the item */
4743 lvItem.mask = LVIF_TEXT | LVIF_IMAGE | LVIF_PARAM | LVIF_STATE;
4744 if (infoPtr->uView == LV_VIEW_DETAILS) lvItem.mask |= LVIF_INDENT;
4745 lvItem.stateMask = LVIS_SELECTED | LVIS_FOCUSED | LVIS_STATEIMAGEMASK | LVIS_CUT | LVIS_OVERLAYMASK;
4746 lvItem.iItem = nItem;
4747 lvItem.iSubItem = 0;
4748 lvItem.state = 0;
4749 lvItem.lParam = 0;
4750 lvItem.cchTextMax = DISP_TEXT_SIZE;
4751 lvItem.pszText = szDispText;
4752 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) return FALSE;
4753 if (lvItem.pszText == LPSTR_TEXTCALLBACKW) lvItem.pszText = callbackW;
4754 TRACE(" lvItem=%s\n", debuglvitem_t(&lvItem, TRUE));
4756 /* now check if we need to update the focus rectangle */
4757 focus = infoPtr->bFocus && (lvItem.state & LVIS_FOCUSED) ? &infoPtr->rcFocus : 0;
4758 if (!focus) lvItem.state &= ~LVIS_FOCUSED;
4760 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, &rcBox, NULL, NULL, NULL, NULL);
4761 OffsetRect(&rcBox, pos.x, pos.y);
4763 /* Full custom draw stage sequence looks like this:
4765 LV_VIEW_DETAILS:
4767 - CDDS_ITEMPREPAINT
4768 - CDDS_ITEMPREPAINT|CDDS_SUBITEM | => sent n times, where n is number of subitems,
4769 CDDS_ITEMPOSTPAINT|CDDS_SUBITEM | including item itself
4770 - CDDS_ITEMPOSTPAINT
4772 other styles:
4774 - CDDS_ITEMPREPAINT
4775 - CDDS_ITEMPOSTPAINT
4778 /* fill in the custom draw structure */
4779 customdraw_fill(&nmlvcd, infoPtr, hdc, &rcBox, &lvItem);
4780 if (cdmode & CDRF_NOTIFYITEMDRAW)
4781 cdsubitemmode = notify_customdraw(infoPtr, CDDS_ITEMPREPAINT, &nmlvcd);
4782 if (cdsubitemmode & CDRF_SKIPDEFAULT) goto postpaint;
4784 if (subitems)
4786 while (iterator_next(subitems))
4788 DWORD subitemstage = CDRF_DODEFAULT;
4790 /* We need to query for each subitem, item's data (subitem == 0) is already here at this point */
4791 if (subitems->nItem)
4793 lvItem.mask = LVIF_TEXT | LVIF_IMAGE | LVIF_PARAM | LVIF_INDENT;
4794 lvItem.stateMask = LVIS_SELECTED | LVIS_FOCUSED | LVIS_STATEIMAGEMASK | LVIS_CUT | LVIS_OVERLAYMASK;
4795 lvItem.iItem = nItem;
4796 lvItem.iSubItem = subitems->nItem;
4797 lvItem.state = 0;
4798 lvItem.lParam = 0;
4799 lvItem.cchTextMax = DISP_TEXT_SIZE;
4800 lvItem.pszText = szDispText;
4801 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) return FALSE;
4802 if (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT)
4803 lvItem.state = LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED);
4804 if (lvItem.pszText == LPSTR_TEXTCALLBACKW) lvItem.pszText = callbackW;
4805 TRACE(" lvItem=%s\n", debuglvitem_t(&lvItem, TRUE));
4807 /* update custom draw data */
4808 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, &nmlvcd.nmcd.rc, NULL, NULL, NULL, NULL);
4809 OffsetRect(&nmlvcd.nmcd.rc, pos.x, pos.y);
4810 nmlvcd.iSubItem = subitems->nItem;
4813 if (cdsubitemmode & CDRF_NOTIFYSUBITEMDRAW)
4814 subitemstage = notify_customdraw(infoPtr, CDDS_SUBITEM | CDDS_ITEMPREPAINT, &nmlvcd);
4816 if (subitems->nItem == 0 || (cdmode & CDRF_NOTIFYITEMDRAW))
4817 prepaint_setup(infoPtr, hdc, &nmlvcd, FALSE);
4818 else if (!(infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT))
4819 prepaint_setup(infoPtr, hdc, &nmlvcd, TRUE);
4821 if (!(subitemstage & CDRF_SKIPDEFAULT))
4822 LISTVIEW_DrawItemPart(infoPtr, &lvItem, &nmlvcd, &pos);
4824 if (subitemstage & CDRF_NOTIFYPOSTPAINT)
4825 subitemstage = notify_customdraw(infoPtr, CDDS_SUBITEM | CDDS_ITEMPOSTPAINT, &nmlvcd);
4828 else
4830 prepaint_setup(infoPtr, hdc, &nmlvcd, FALSE);
4831 LISTVIEW_DrawItemPart(infoPtr, &lvItem, &nmlvcd, &pos);
4834 postpaint:
4835 if (cdsubitemmode & CDRF_NOTIFYPOSTPAINT)
4837 nmlvcd.iSubItem = 0;
4838 notify_customdraw(infoPtr, CDDS_ITEMPOSTPAINT, &nmlvcd);
4841 return TRUE;
4844 /***
4845 * DESCRIPTION:
4846 * Draws listview items when in owner draw mode.
4848 * PARAMETER(S):
4849 * [I] infoPtr : valid pointer to the listview structure
4850 * [I] hdc : device context handle
4852 * RETURN:
4853 * None
4855 static void LISTVIEW_RefreshOwnerDraw(const LISTVIEW_INFO *infoPtr, ITERATOR *i, HDC hdc, DWORD cdmode)
4857 UINT uID = (UINT)GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
4858 DWORD cditemmode = CDRF_DODEFAULT;
4859 NMLVCUSTOMDRAW nmlvcd;
4860 POINT Origin, Position;
4861 DRAWITEMSTRUCT dis;
4862 LVITEMW item;
4864 TRACE("()\n");
4866 ZeroMemory(&dis, sizeof(dis));
4868 /* Get scroll info once before loop */
4869 LISTVIEW_GetOrigin(infoPtr, &Origin);
4871 /* iterate through the invalidated rows */
4872 while(iterator_next(i))
4874 item.iItem = i->nItem;
4875 item.iSubItem = 0;
4876 item.mask = LVIF_PARAM | LVIF_STATE;
4877 item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
4878 if (!LISTVIEW_GetItemW(infoPtr, &item)) continue;
4880 dis.CtlType = ODT_LISTVIEW;
4881 dis.CtlID = uID;
4882 dis.itemID = item.iItem;
4883 dis.itemAction = ODA_DRAWENTIRE;
4884 dis.itemState = 0;
4885 if (item.state & LVIS_SELECTED) dis.itemState |= ODS_SELECTED;
4886 if (infoPtr->bFocus && (item.state & LVIS_FOCUSED)) dis.itemState |= ODS_FOCUS;
4887 dis.hwndItem = infoPtr->hwndSelf;
4888 dis.hDC = hdc;
4889 LISTVIEW_GetItemOrigin(infoPtr, dis.itemID, &Position);
4890 dis.rcItem.left = Position.x + Origin.x;
4891 dis.rcItem.right = dis.rcItem.left + infoPtr->nItemWidth;
4892 dis.rcItem.top = Position.y + Origin.y;
4893 dis.rcItem.bottom = dis.rcItem.top + infoPtr->nItemHeight;
4894 dis.itemData = item.lParam;
4896 TRACE("item=%s, rcItem=%s\n", debuglvitem_t(&item, TRUE), wine_dbgstr_rect(&dis.rcItem));
4899 * Even if we do not send the CDRF_NOTIFYITEMDRAW we need to fill the nmlvcd
4900 * structure for the rest. of the paint cycle
4902 customdraw_fill(&nmlvcd, infoPtr, hdc, &dis.rcItem, &item);
4903 if (cdmode & CDRF_NOTIFYITEMDRAW)
4904 cditemmode = notify_customdraw(infoPtr, CDDS_PREPAINT, &nmlvcd);
4906 if (!(cditemmode & CDRF_SKIPDEFAULT))
4908 prepaint_setup (infoPtr, hdc, &nmlvcd, FALSE);
4909 SendMessageW(infoPtr->hwndNotify, WM_DRAWITEM, dis.CtlID, (LPARAM)&dis);
4912 if (cditemmode & CDRF_NOTIFYPOSTPAINT)
4913 notify_postpaint(infoPtr, &nmlvcd);
4917 /***
4918 * DESCRIPTION:
4919 * Draws listview items when in report display mode.
4921 * PARAMETER(S):
4922 * [I] infoPtr : valid pointer to the listview structure
4923 * [I] hdc : device context handle
4924 * [I] cdmode : custom draw mode
4926 * RETURN:
4927 * None
4929 static void LISTVIEW_RefreshReport(LISTVIEW_INFO *infoPtr, ITERATOR *i, HDC hdc, DWORD cdmode)
4931 INT rgntype;
4932 RECT rcClip, rcItem;
4933 POINT Origin;
4934 RANGES colRanges;
4935 INT col;
4936 ITERATOR j;
4938 TRACE("()\n");
4940 /* figure out what to draw */
4941 rgntype = GetClipBox(hdc, &rcClip);
4942 if (rgntype == NULLREGION) return;
4944 /* Get scroll info once before loop */
4945 LISTVIEW_GetOrigin(infoPtr, &Origin);
4947 colRanges = ranges_create(DPA_GetPtrCount(infoPtr->hdpaColumns));
4949 /* narrow down the columns we need to paint */
4950 for(col = 0; col < DPA_GetPtrCount(infoPtr->hdpaColumns); col++)
4952 INT index = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX, col, 0);
4954 LISTVIEW_GetHeaderRect(infoPtr, index, &rcItem);
4955 if ((rcItem.right + Origin.x >= rcClip.left) && (rcItem.left + Origin.x < rcClip.right))
4956 ranges_additem(colRanges, index);
4958 iterator_rangesitems(&j, colRanges);
4960 /* in full row select, we _have_ to draw the main item */
4961 if (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT)
4962 j.nSpecial = 0;
4964 /* iterate through the invalidated rows */
4965 while(iterator_next(i))
4967 RANGES subitems;
4968 POINT Position;
4969 ITERATOR k;
4971 SelectObject(hdc, infoPtr->hFont);
4972 LISTVIEW_GetItemOrigin(infoPtr, i->nItem, &Position);
4973 Position.x = Origin.x;
4974 Position.y += Origin.y;
4976 subitems = ranges_create(DPA_GetPtrCount(infoPtr->hdpaColumns));
4978 /* iterate through the invalidated columns */
4979 while(iterator_next(&j))
4981 LISTVIEW_GetHeaderRect(infoPtr, j.nItem, &rcItem);
4983 if (rgntype == COMPLEXREGION && !((infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT) && j.nItem == 0))
4985 rcItem.top = 0;
4986 rcItem.bottom = infoPtr->nItemHeight;
4987 OffsetRect(&rcItem, Origin.x, Position.y);
4988 if (!RectVisible(hdc, &rcItem)) continue;
4991 ranges_additem(subitems, j.nItem);
4994 iterator_rangesitems(&k, subitems);
4995 LISTVIEW_DrawItem(infoPtr, hdc, i->nItem, &k, Position, cdmode);
4996 iterator_destroy(&k);
4998 iterator_destroy(&j);
5001 /***
5002 * DESCRIPTION:
5003 * Draws the gridlines if necessary when in report display mode.
5005 * PARAMETER(S):
5006 * [I] infoPtr : valid pointer to the listview structure
5007 * [I] hdc : device context handle
5009 * RETURN:
5010 * None
5012 static void LISTVIEW_RefreshReportGrid(LISTVIEW_INFO *infoPtr, HDC hdc)
5014 INT rgntype;
5015 INT y, itemheight;
5016 INT col, index;
5017 HPEN hPen, hOldPen;
5018 RECT rcClip, rcItem = {0};
5019 POINT Origin;
5020 RANGES colRanges;
5021 ITERATOR j;
5022 BOOL rmost = FALSE;
5024 TRACE("()\n");
5026 /* figure out what to draw */
5027 rgntype = GetClipBox(hdc, &rcClip);
5028 if (rgntype == NULLREGION) return;
5030 /* Get scroll info once before loop */
5031 LISTVIEW_GetOrigin(infoPtr, &Origin);
5033 colRanges = ranges_create(DPA_GetPtrCount(infoPtr->hdpaColumns));
5035 /* narrow down the columns we need to paint */
5036 for(col = 0; col < DPA_GetPtrCount(infoPtr->hdpaColumns); col++)
5038 index = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX, col, 0);
5040 LISTVIEW_GetHeaderRect(infoPtr, index, &rcItem);
5041 if ((rcItem.right + Origin.x >= rcClip.left) && (rcItem.left + Origin.x < rcClip.right))
5042 ranges_additem(colRanges, index);
5045 /* is right most vertical line visible? */
5046 if (DPA_GetPtrCount(infoPtr->hdpaColumns) > 0)
5048 index = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX, DPA_GetPtrCount(infoPtr->hdpaColumns) - 1, 0);
5049 LISTVIEW_GetHeaderRect(infoPtr, index, &rcItem);
5050 rmost = (rcItem.right + Origin.x < rcClip.right);
5053 if ((hPen = CreatePen( PS_SOLID, 1, comctl32_color.clr3dFace )))
5055 hOldPen = SelectObject ( hdc, hPen );
5057 /* draw the vertical lines for the columns */
5058 iterator_rangesitems(&j, colRanges);
5059 while(iterator_next(&j))
5061 LISTVIEW_GetHeaderRect(infoPtr, j.nItem, &rcItem);
5062 if (rcItem.left == 0) continue; /* skip leftmost column */
5063 rcItem.left += Origin.x;
5064 rcItem.right += Origin.x;
5065 rcItem.top = infoPtr->rcList.top;
5066 rcItem.bottom = infoPtr->rcList.bottom;
5067 TRACE("vert col=%d, rcItem=%s\n", j.nItem, wine_dbgstr_rect(&rcItem));
5068 MoveToEx (hdc, rcItem.left, rcItem.top, NULL);
5069 LineTo (hdc, rcItem.left, rcItem.bottom);
5071 iterator_destroy(&j);
5072 /* draw rightmost grid line if visible */
5073 if (rmost)
5075 index = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX,
5076 DPA_GetPtrCount(infoPtr->hdpaColumns) - 1, 0);
5077 LISTVIEW_GetHeaderRect(infoPtr, index, &rcItem);
5079 rcItem.right += Origin.x;
5081 MoveToEx (hdc, rcItem.right, infoPtr->rcList.top, NULL);
5082 LineTo (hdc, rcItem.right, infoPtr->rcList.bottom);
5085 /* draw the horizontal lines for the rows */
5086 itemheight = LISTVIEW_CalculateItemHeight(infoPtr);
5087 rcItem.left = infoPtr->rcList.left;
5088 rcItem.right = infoPtr->rcList.right;
5089 for(y = Origin.y > 1 ? Origin.y - 1 : itemheight - 1 + Origin.y % itemheight; y<=infoPtr->rcList.bottom; y+=itemheight)
5091 rcItem.bottom = rcItem.top = y;
5092 TRACE("horz rcItem=%s\n", wine_dbgstr_rect(&rcItem));
5093 MoveToEx (hdc, rcItem.left, rcItem.top, NULL);
5094 LineTo (hdc, rcItem.right, rcItem.top);
5097 SelectObject( hdc, hOldPen );
5098 DeleteObject( hPen );
5100 else
5101 ranges_destroy(colRanges);
5104 /***
5105 * DESCRIPTION:
5106 * Draws listview items when in list display mode.
5108 * PARAMETER(S):
5109 * [I] infoPtr : valid pointer to the listview structure
5110 * [I] hdc : device context handle
5111 * [I] cdmode : custom draw mode
5113 * RETURN:
5114 * None
5116 static void LISTVIEW_RefreshList(LISTVIEW_INFO *infoPtr, ITERATOR *i, HDC hdc, DWORD cdmode)
5118 POINT Origin, Position;
5120 /* Get scroll info once before loop */
5121 LISTVIEW_GetOrigin(infoPtr, &Origin);
5123 while(iterator_prev(i))
5125 SelectObject(hdc, infoPtr->hFont);
5126 LISTVIEW_GetItemOrigin(infoPtr, i->nItem, &Position);
5127 Position.x += Origin.x;
5128 Position.y += Origin.y;
5130 LISTVIEW_DrawItem(infoPtr, hdc, i->nItem, NULL, Position, cdmode);
5135 /***
5136 * DESCRIPTION:
5137 * Draws listview items.
5139 * PARAMETER(S):
5140 * [I] infoPtr : valid pointer to the listview structure
5141 * [I] hdc : device context handle
5142 * [I] prcErase : rect to be erased before refresh (may be NULL)
5144 * RETURN:
5145 * NoneX
5147 static void LISTVIEW_Refresh(LISTVIEW_INFO *infoPtr, HDC hdc, const RECT *prcErase)
5149 COLORREF oldTextColor = 0, oldBkColor = 0;
5150 NMLVCUSTOMDRAW nmlvcd;
5151 HFONT hOldFont = 0;
5152 DWORD cdmode;
5153 INT oldBkMode = 0;
5154 RECT rcClient;
5155 ITERATOR i;
5156 HDC hdcOrig = hdc;
5157 HBITMAP hbmp = NULL;
5158 RANGE range;
5160 LISTVIEW_DUMP(infoPtr);
5162 if (infoPtr->dwLvExStyle & LVS_EX_DOUBLEBUFFER) {
5163 TRACE("double buffering\n");
5165 hdc = CreateCompatibleDC(hdcOrig);
5166 if (!hdc) {
5167 ERR("Failed to create DC for backbuffer\n");
5168 return;
5170 hbmp = CreateCompatibleBitmap(hdcOrig, infoPtr->rcList.right,
5171 infoPtr->rcList.bottom);
5172 if (!hbmp) {
5173 ERR("Failed to create bitmap for backbuffer\n");
5174 DeleteDC(hdc);
5175 return;
5178 SelectObject(hdc, hbmp);
5179 SelectObject(hdc, infoPtr->hFont);
5181 if(GetClipBox(hdcOrig, &rcClient))
5182 IntersectClipRect(hdc, rcClient.left, rcClient.top, rcClient.right, rcClient.bottom);
5183 } else {
5184 /* Save dc values we're gonna trash while drawing
5185 * FIXME: Should be done in LISTVIEW_DrawItem() */
5186 hOldFont = SelectObject(hdc, infoPtr->hFont);
5187 oldBkMode = GetBkMode(hdc);
5188 oldBkColor = GetBkColor(hdc);
5189 oldTextColor = GetTextColor(hdc);
5192 infoPtr->bIsDrawing = TRUE;
5194 if (prcErase) {
5195 LISTVIEW_FillBkgnd(infoPtr, hdc, prcErase);
5196 } else if (infoPtr->dwLvExStyle & LVS_EX_DOUBLEBUFFER) {
5197 /* If no erasing was done (usually because RedrawWindow was called
5198 * with RDW_INVALIDATE only) we need to copy the old contents into
5199 * the backbuffer before continuing. */
5200 BitBlt(hdc, infoPtr->rcList.left, infoPtr->rcList.top,
5201 infoPtr->rcList.right - infoPtr->rcList.left,
5202 infoPtr->rcList.bottom - infoPtr->rcList.top,
5203 hdcOrig, infoPtr->rcList.left, infoPtr->rcList.top, SRCCOPY);
5206 GetClientRect(infoPtr->hwndSelf, &rcClient);
5207 customdraw_fill(&nmlvcd, infoPtr, hdc, &rcClient, 0);
5208 cdmode = notify_customdraw(infoPtr, CDDS_PREPAINT, &nmlvcd);
5209 if (cdmode & CDRF_SKIPDEFAULT) goto enddraw;
5211 /* nothing to draw */
5212 if(infoPtr->nItemCount == 0) goto enddraw;
5214 /* figure out what we need to draw */
5215 iterator_visibleitems(&i, infoPtr, hdc);
5216 range = iterator_range(&i);
5218 /* send cache hint notification */
5219 if (infoPtr->dwStyle & LVS_OWNERDATA)
5221 NMLVCACHEHINT nmlv;
5223 ZeroMemory(&nmlv, sizeof(NMLVCACHEHINT));
5224 nmlv.iFrom = range.lower;
5225 nmlv.iTo = range.upper - 1;
5226 notify_hdr(infoPtr, LVN_ODCACHEHINT, &nmlv.hdr);
5229 if ((infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && (infoPtr->uView == LV_VIEW_DETAILS))
5230 LISTVIEW_RefreshOwnerDraw(infoPtr, &i, hdc, cdmode);
5231 else
5233 if (infoPtr->uView == LV_VIEW_DETAILS)
5234 LISTVIEW_RefreshReport(infoPtr, &i, hdc, cdmode);
5235 else /* LV_VIEW_LIST, LV_VIEW_ICON or LV_VIEW_SMALLICON */
5236 LISTVIEW_RefreshList(infoPtr, &i, hdc, cdmode);
5238 /* if we have a focus rect and it's visible, draw it */
5239 if (infoPtr->bFocus && range.lower <= infoPtr->nFocusedItem &&
5240 (range.upper - 1) >= infoPtr->nFocusedItem)
5241 LISTVIEW_DrawFocusRect(infoPtr, hdc);
5243 iterator_destroy(&i);
5245 enddraw:
5246 /* For LVS_EX_GRIDLINES go and draw lines */
5247 /* This includes the case where there were *no* items */
5248 if ((infoPtr->uView == LV_VIEW_DETAILS) && infoPtr->dwLvExStyle & LVS_EX_GRIDLINES)
5249 LISTVIEW_RefreshReportGrid(infoPtr, hdc);
5251 /* Draw marquee rectangle if appropriate */
5252 if (infoPtr->bMarqueeSelect)
5253 DrawFocusRect(hdc, &infoPtr->marqueeDrawRect);
5255 if (cdmode & CDRF_NOTIFYPOSTPAINT)
5256 notify_postpaint(infoPtr, &nmlvcd);
5258 if(hbmp) {
5259 BitBlt(hdcOrig, infoPtr->rcList.left, infoPtr->rcList.top,
5260 infoPtr->rcList.right - infoPtr->rcList.left,
5261 infoPtr->rcList.bottom - infoPtr->rcList.top,
5262 hdc, infoPtr->rcList.left, infoPtr->rcList.top, SRCCOPY);
5264 DeleteObject(hbmp);
5265 DeleteDC(hdc);
5266 } else {
5267 SelectObject(hdc, hOldFont);
5268 SetBkMode(hdc, oldBkMode);
5269 SetBkColor(hdc, oldBkColor);
5270 SetTextColor(hdc, oldTextColor);
5273 infoPtr->bIsDrawing = FALSE;
5277 /***
5278 * DESCRIPTION:
5279 * Calculates the approximate width and height of a given number of items.
5281 * PARAMETER(S):
5282 * [I] infoPtr : valid pointer to the listview structure
5283 * [I] nItemCount : number of items
5284 * [I] wWidth : width
5285 * [I] wHeight : height
5287 * RETURN:
5288 * Returns a DWORD. The width in the low word and the height in high word.
5290 static DWORD LISTVIEW_ApproximateViewRect(const LISTVIEW_INFO *infoPtr, INT nItemCount,
5291 WORD wWidth, WORD wHeight)
5293 DWORD dwViewRect = 0;
5295 if (nItemCount == -1)
5296 nItemCount = infoPtr->nItemCount;
5298 if (infoPtr->uView == LV_VIEW_LIST)
5300 INT nItemCountPerColumn = 1;
5301 INT nColumnCount = 0;
5303 if (wHeight == 0xFFFF)
5305 /* use current height */
5306 wHeight = infoPtr->rcList.bottom - infoPtr->rcList.top;
5309 if (wHeight < infoPtr->nItemHeight)
5310 wHeight = infoPtr->nItemHeight;
5312 if (nItemCount > 0)
5314 if (infoPtr->nItemHeight > 0)
5316 nItemCountPerColumn = wHeight / infoPtr->nItemHeight;
5317 if (nItemCountPerColumn == 0)
5318 nItemCountPerColumn = 1;
5320 if (nItemCount % nItemCountPerColumn != 0)
5321 nColumnCount = nItemCount / nItemCountPerColumn;
5322 else
5323 nColumnCount = nItemCount / nItemCountPerColumn + 1;
5327 /* Microsoft padding magic */
5328 wHeight = nItemCountPerColumn * infoPtr->nItemHeight + 2;
5329 wWidth = nColumnCount * infoPtr->nItemWidth + 2;
5331 dwViewRect = MAKELONG(wWidth, wHeight);
5333 else if (infoPtr->uView == LV_VIEW_DETAILS)
5335 RECT rcBox;
5337 if (infoPtr->nItemCount > 0)
5339 LISTVIEW_GetItemBox(infoPtr, 0, &rcBox);
5340 wWidth = rcBox.right - rcBox.left;
5341 wHeight = (rcBox.bottom - rcBox.top) * nItemCount;
5343 else
5345 /* use current height and width */
5346 if (wHeight == 0xffff)
5347 wHeight = infoPtr->rcList.bottom - infoPtr->rcList.top;
5348 if (wWidth == 0xffff)
5349 wWidth = infoPtr->rcList.right - infoPtr->rcList.left;
5352 dwViewRect = MAKELONG(wWidth, wHeight);
5354 else if (infoPtr->uView == LV_VIEW_ICON)
5356 UINT rows,cols;
5357 UINT nItemWidth;
5358 UINT nItemHeight;
5360 nItemWidth = infoPtr->iconSpacing.cx;
5361 nItemHeight = infoPtr->iconSpacing.cy;
5363 if (wWidth == 0xffff)
5364 wWidth = infoPtr->rcList.right - infoPtr->rcList.left;
5366 if (wWidth < nItemWidth)
5367 wWidth = nItemWidth;
5369 cols = wWidth / nItemWidth;
5370 if (cols > nItemCount)
5371 cols = nItemCount;
5372 if (cols < 1)
5373 cols = 1;
5375 if (nItemCount)
5377 rows = nItemCount / cols;
5378 if (nItemCount % cols)
5379 rows++;
5381 else
5382 rows = 0;
5384 wHeight = (nItemHeight * rows)+2;
5385 wWidth = (nItemWidth * cols)+2;
5387 dwViewRect = MAKELONG(wWidth, wHeight);
5389 else if (infoPtr->uView == LV_VIEW_SMALLICON)
5390 FIXME("uView == LV_VIEW_SMALLICON: not implemented\n");
5392 return dwViewRect;
5395 /***
5396 * DESCRIPTION:
5397 * Cancel edit label with saving item text.
5399 * PARAMETER(S):
5400 * [I] infoPtr : valid pointer to the listview structure
5402 * RETURN:
5403 * Always returns TRUE.
5405 static LRESULT LISTVIEW_CancelEditLabel(LISTVIEW_INFO *infoPtr)
5407 if (infoPtr->hwndEdit)
5409 /* handle value will be lost after LISTVIEW_EndEditLabelT */
5410 HWND edit = infoPtr->hwndEdit;
5412 LISTVIEW_EndEditLabelT(infoPtr, TRUE, IsWindowUnicode(infoPtr->hwndEdit));
5413 SendMessageW(edit, WM_CLOSE, 0, 0);
5416 return TRUE;
5419 /***
5420 * DESCRIPTION:
5421 * Create a drag image list for the specified item.
5423 * PARAMETER(S):
5424 * [I] infoPtr : valid pointer to the listview structure
5425 * [I] iItem : index of item
5426 * [O] lppt : Upper-left corner of the image
5428 * RETURN:
5429 * Returns a handle to the image list if successful, NULL otherwise.
5431 static HIMAGELIST LISTVIEW_CreateDragImage(LISTVIEW_INFO *infoPtr, INT iItem, LPPOINT lppt)
5433 RECT rcItem;
5434 SIZE size;
5435 POINT pos;
5436 HDC hdc, hdcOrig;
5437 HBITMAP hbmp, hOldbmp;
5438 HFONT hOldFont;
5439 HIMAGELIST dragList = 0;
5440 TRACE("iItem=%d Count=%d\n", iItem, infoPtr->nItemCount);
5442 if (iItem < 0 || iItem >= infoPtr->nItemCount || !lppt)
5443 return 0;
5445 rcItem.left = LVIR_BOUNDS;
5446 if (!LISTVIEW_GetItemRect(infoPtr, iItem, &rcItem))
5447 return 0;
5449 lppt->x = rcItem.left;
5450 lppt->y = rcItem.top;
5452 size.cx = rcItem.right - rcItem.left;
5453 size.cy = rcItem.bottom - rcItem.top;
5455 hdcOrig = GetDC(infoPtr->hwndSelf);
5456 hdc = CreateCompatibleDC(hdcOrig);
5457 hbmp = CreateCompatibleBitmap(hdcOrig, size.cx, size.cy);
5458 hOldbmp = SelectObject(hdc, hbmp);
5459 hOldFont = SelectObject(hdc, infoPtr->hFont);
5461 SetRect(&rcItem, 0, 0, size.cx, size.cy);
5462 FillRect(hdc, &rcItem, infoPtr->hBkBrush);
5464 pos.x = pos.y = 0;
5465 if (LISTVIEW_DrawItem(infoPtr, hdc, iItem, NULL, pos, CDRF_DODEFAULT))
5467 dragList = ImageList_Create(size.cx, size.cy, ILC_COLOR, 10, 10);
5468 SelectObject(hdc, hOldbmp);
5469 ImageList_Add(dragList, hbmp, 0);
5471 else
5472 SelectObject(hdc, hOldbmp);
5474 SelectObject(hdc, hOldFont);
5475 DeleteObject(hbmp);
5476 DeleteDC(hdc);
5477 ReleaseDC(infoPtr->hwndSelf, hdcOrig);
5479 TRACE("ret=%p\n", dragList);
5481 return dragList;
5485 /***
5486 * DESCRIPTION:
5487 * Removes all listview items and subitems.
5489 * PARAMETER(S):
5490 * [I] infoPtr : valid pointer to the listview structure
5492 * RETURN:
5493 * SUCCESS : TRUE
5494 * FAILURE : FALSE
5496 static BOOL LISTVIEW_DeleteAllItems(LISTVIEW_INFO *infoPtr, BOOL destroy)
5498 HDPA hdpaSubItems = NULL;
5499 BOOL suppress = FALSE;
5500 ITEMHDR *hdrItem;
5501 ITEM_INFO *lpItem;
5502 ITEM_ID *lpID;
5503 INT i, j;
5505 TRACE("()\n");
5507 /* we do it directly, to avoid notifications */
5508 ranges_clear(infoPtr->selectionRanges);
5509 infoPtr->nSelectionMark = -1;
5510 infoPtr->nFocusedItem = -1;
5511 SetRectEmpty(&infoPtr->rcFocus);
5512 /* But we are supposed to leave nHotItem as is! */
5514 /* send LVN_DELETEALLITEMS notification */
5515 if (!(infoPtr->dwStyle & LVS_OWNERDATA) || !destroy)
5517 NMLISTVIEW nmlv;
5519 memset(&nmlv, 0, sizeof(NMLISTVIEW));
5520 nmlv.iItem = -1;
5521 suppress = notify_listview(infoPtr, LVN_DELETEALLITEMS, &nmlv);
5524 for (i = infoPtr->nItemCount - 1; i >= 0; i--)
5526 if (!(infoPtr->dwStyle & LVS_OWNERDATA))
5528 /* send LVN_DELETEITEM notification, if not suppressed
5529 and if it is not a virtual listview */
5530 if (!suppress) notify_deleteitem(infoPtr, i);
5531 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, i);
5532 lpItem = DPA_GetPtr(hdpaSubItems, 0);
5533 /* free id struct */
5534 j = DPA_GetPtrIndex(infoPtr->hdpaItemIds, lpItem->id);
5535 lpID = DPA_GetPtr(infoPtr->hdpaItemIds, j);
5536 DPA_DeletePtr(infoPtr->hdpaItemIds, j);
5537 Free(lpID);
5538 /* both item and subitem start with ITEMHDR header */
5539 for (j = 0; j < DPA_GetPtrCount(hdpaSubItems); j++)
5541 hdrItem = DPA_GetPtr(hdpaSubItems, j);
5542 if (is_text(hdrItem->pszText)) Free(hdrItem->pszText);
5543 Free(hdrItem);
5545 DPA_Destroy(hdpaSubItems);
5546 DPA_DeletePtr(infoPtr->hdpaItems, i);
5548 DPA_DeletePtr(infoPtr->hdpaPosX, i);
5549 DPA_DeletePtr(infoPtr->hdpaPosY, i);
5550 infoPtr->nItemCount --;
5553 if (!destroy)
5555 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
5556 LISTVIEW_UpdateScroll(infoPtr);
5558 LISTVIEW_InvalidateList(infoPtr);
5560 return TRUE;
5563 /***
5564 * DESCRIPTION:
5565 * Scrolls, and updates the columns, when a column is changing width.
5567 * PARAMETER(S):
5568 * [I] infoPtr : valid pointer to the listview structure
5569 * [I] nColumn : column to scroll
5570 * [I] dx : amount of scroll, in pixels
5572 * RETURN:
5573 * None.
5575 static void LISTVIEW_ScrollColumns(LISTVIEW_INFO *infoPtr, INT nColumn, INT dx)
5577 COLUMN_INFO *lpColumnInfo;
5578 RECT rcOld, rcCol;
5579 POINT ptOrigin;
5580 INT nCol;
5581 HDITEMW hdi;
5583 if (nColumn < 0 || DPA_GetPtrCount(infoPtr->hdpaColumns) < 1) return;
5584 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, min(nColumn, DPA_GetPtrCount(infoPtr->hdpaColumns) - 1));
5585 rcCol = lpColumnInfo->rcHeader;
5586 if (nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns))
5587 rcCol.left = rcCol.right;
5589 /* adjust the other columns */
5590 hdi.mask = HDI_ORDER;
5591 if (SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, nColumn, (LPARAM)&hdi))
5593 INT nOrder = hdi.iOrder;
5594 for (nCol = 0; nCol < DPA_GetPtrCount(infoPtr->hdpaColumns); nCol++)
5596 hdi.mask = HDI_ORDER;
5597 SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, nCol, (LPARAM)&hdi);
5598 if (hdi.iOrder >= nOrder) {
5599 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, nCol);
5600 lpColumnInfo->rcHeader.left += dx;
5601 lpColumnInfo->rcHeader.right += dx;
5606 /* do not update screen if not in report mode */
5607 if (!is_redrawing(infoPtr) || infoPtr->uView != LV_VIEW_DETAILS) return;
5609 /* Need to reset the item width when inserting a new column */
5610 infoPtr->nItemWidth += dx;
5612 LISTVIEW_UpdateScroll(infoPtr);
5613 LISTVIEW_GetOrigin(infoPtr, &ptOrigin);
5615 /* scroll to cover the deleted column, and invalidate for redraw */
5616 rcOld = infoPtr->rcList;
5617 rcOld.left = ptOrigin.x + rcCol.left + dx;
5618 ScrollWindowEx(infoPtr->hwndSelf, dx, 0, &rcOld, &rcOld, 0, 0, SW_ERASE | SW_INVALIDATE);
5621 /***
5622 * DESCRIPTION:
5623 * Removes a column from the listview control.
5625 * PARAMETER(S):
5626 * [I] infoPtr : valid pointer to the listview structure
5627 * [I] nColumn : column index
5629 * RETURN:
5630 * SUCCESS : TRUE
5631 * FAILURE : FALSE
5633 static BOOL LISTVIEW_DeleteColumn(LISTVIEW_INFO *infoPtr, INT nColumn)
5635 RECT rcCol;
5637 TRACE("nColumn=%d\n", nColumn);
5639 if (nColumn < 0 || nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns))
5640 return FALSE;
5642 /* While the MSDN specifically says that column zero should not be deleted,
5643 what actually happens is that the column itself is deleted but no items or subitems
5644 are removed.
5647 LISTVIEW_GetHeaderRect(infoPtr, nColumn, &rcCol);
5649 if (!SendMessageW(infoPtr->hwndHeader, HDM_DELETEITEM, nColumn, 0))
5650 return FALSE;
5652 Free(DPA_GetPtr(infoPtr->hdpaColumns, nColumn));
5653 DPA_DeletePtr(infoPtr->hdpaColumns, nColumn);
5655 if (!(infoPtr->dwStyle & LVS_OWNERDATA) && nColumn)
5657 SUBITEM_INFO *lpSubItem, *lpDelItem;
5658 HDPA hdpaSubItems;
5659 INT nItem, nSubItem, i;
5661 for (nItem = 0; nItem < infoPtr->nItemCount; nItem++)
5663 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, nItem);
5664 nSubItem = 0;
5665 lpDelItem = 0;
5666 for (i = 1; i < DPA_GetPtrCount(hdpaSubItems); i++)
5668 lpSubItem = DPA_GetPtr(hdpaSubItems, i);
5669 if (lpSubItem->iSubItem == nColumn)
5671 nSubItem = i;
5672 lpDelItem = lpSubItem;
5674 else if (lpSubItem->iSubItem > nColumn)
5676 lpSubItem->iSubItem--;
5680 /* if we found our subitem, zap it */
5681 if (nSubItem > 0)
5683 /* free string */
5684 if (is_text(lpDelItem->hdr.pszText))
5685 Free(lpDelItem->hdr.pszText);
5687 /* free item */
5688 Free(lpDelItem);
5690 /* free dpa memory */
5691 DPA_DeletePtr(hdpaSubItems, nSubItem);
5696 /* update the other column info */
5697 if(DPA_GetPtrCount(infoPtr->hdpaColumns) == 0)
5698 LISTVIEW_InvalidateList(infoPtr);
5699 else
5700 LISTVIEW_ScrollColumns(infoPtr, nColumn, -(rcCol.right - rcCol.left));
5701 LISTVIEW_UpdateItemSize(infoPtr);
5703 return TRUE;
5706 /***
5707 * DESCRIPTION:
5708 * Invalidates the listview after an item's insertion or deletion.
5710 * PARAMETER(S):
5711 * [I] infoPtr : valid pointer to the listview structure
5712 * [I] nItem : item index
5713 * [I] dir : -1 if deleting, 1 if inserting
5715 * RETURN:
5716 * None
5718 static void LISTVIEW_ScrollOnInsert(LISTVIEW_INFO *infoPtr, INT nItem, INT dir)
5720 INT nPerCol, nItemCol, nItemRow;
5721 RECT rcScroll;
5722 POINT Origin;
5724 /* if we don't refresh, what's the point of scrolling? */
5725 if (!is_redrawing(infoPtr)) return;
5727 assert (abs(dir) == 1);
5729 /* arrange icons if autoarrange is on */
5730 if (is_autoarrange(infoPtr))
5732 BOOL arrange = TRUE;
5733 if (dir < 0 && nItem >= infoPtr->nItemCount) arrange = FALSE;
5734 if (dir > 0 && nItem == infoPtr->nItemCount - 1) arrange = FALSE;
5735 if (arrange) LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
5738 /* scrollbars need updating */
5739 LISTVIEW_UpdateScroll(infoPtr);
5741 /* figure out the item's position */
5742 if (infoPtr->uView == LV_VIEW_DETAILS)
5743 nPerCol = infoPtr->nItemCount + 1;
5744 else if (infoPtr->uView == LV_VIEW_LIST)
5745 nPerCol = LISTVIEW_GetCountPerColumn(infoPtr);
5746 else /* LV_VIEW_ICON, or LV_VIEW_SMALLICON */
5747 return;
5749 nItemCol = nItem / nPerCol;
5750 nItemRow = nItem % nPerCol;
5751 LISTVIEW_GetOrigin(infoPtr, &Origin);
5753 /* move the items below up a slot */
5754 rcScroll.left = nItemCol * infoPtr->nItemWidth;
5755 rcScroll.top = nItemRow * infoPtr->nItemHeight;
5756 rcScroll.right = rcScroll.left + infoPtr->nItemWidth;
5757 rcScroll.bottom = nPerCol * infoPtr->nItemHeight;
5758 OffsetRect(&rcScroll, Origin.x, Origin.y);
5759 TRACE("rcScroll=%s, dx=%d\n", wine_dbgstr_rect(&rcScroll), dir * infoPtr->nItemHeight);
5760 if (IntersectRect(&rcScroll, &rcScroll, &infoPtr->rcList))
5762 TRACE("Invalidating rcScroll=%s, rcList=%s\n", wine_dbgstr_rect(&rcScroll), wine_dbgstr_rect(&infoPtr->rcList));
5763 InvalidateRect(infoPtr->hwndSelf, &rcScroll, TRUE);
5766 /* report has only that column, so we're done */
5767 if (infoPtr->uView == LV_VIEW_DETAILS) return;
5769 /* now for LISTs, we have to deal with the columns to the right */
5770 SetRect(&rcScroll, (nItemCol + 1) * infoPtr->nItemWidth, 0,
5771 (infoPtr->nItemCount / nPerCol + 1) * infoPtr->nItemWidth,
5772 nPerCol * infoPtr->nItemHeight);
5773 OffsetRect(&rcScroll, Origin.x, Origin.y);
5774 if (IntersectRect(&rcScroll, &rcScroll, &infoPtr->rcList))
5775 InvalidateRect(infoPtr->hwndSelf, &rcScroll, TRUE);
5778 /***
5779 * DESCRIPTION:
5780 * Removes an item from the listview control.
5782 * PARAMETER(S):
5783 * [I] infoPtr : valid pointer to the listview structure
5784 * [I] nItem : item index
5786 * RETURN:
5787 * SUCCESS : TRUE
5788 * FAILURE : FALSE
5790 static BOOL LISTVIEW_DeleteItem(LISTVIEW_INFO *infoPtr, INT nItem)
5792 LVITEMW item;
5793 const BOOL is_icon = (infoPtr->uView == LV_VIEW_SMALLICON || infoPtr->uView == LV_VIEW_ICON);
5794 INT focus = infoPtr->nFocusedItem;
5796 TRACE("(nItem=%d)\n", nItem);
5798 if (nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
5800 /* remove selection, and focus */
5801 item.state = 0;
5802 item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
5803 LISTVIEW_SetItemState(infoPtr, nItem, &item);
5805 /* send LVN_DELETEITEM notification. */
5806 if (!notify_deleteitem(infoPtr, nItem)) return FALSE;
5808 /* we need to do this here, because we'll be deleting stuff */
5809 if (is_icon)
5810 LISTVIEW_InvalidateItem(infoPtr, nItem);
5812 if (!(infoPtr->dwStyle & LVS_OWNERDATA))
5814 HDPA hdpaSubItems;
5815 ITEMHDR *hdrItem;
5816 ITEM_INFO *lpItem;
5817 ITEM_ID *lpID;
5818 INT i;
5820 hdpaSubItems = DPA_DeletePtr(infoPtr->hdpaItems, nItem);
5821 lpItem = DPA_GetPtr(hdpaSubItems, 0);
5823 /* free id struct */
5824 i = DPA_GetPtrIndex(infoPtr->hdpaItemIds, lpItem->id);
5825 lpID = DPA_GetPtr(infoPtr->hdpaItemIds, i);
5826 DPA_DeletePtr(infoPtr->hdpaItemIds, i);
5827 Free(lpID);
5828 for (i = 0; i < DPA_GetPtrCount(hdpaSubItems); i++)
5830 hdrItem = DPA_GetPtr(hdpaSubItems, i);
5831 if (is_text(hdrItem->pszText)) Free(hdrItem->pszText);
5832 Free(hdrItem);
5834 DPA_Destroy(hdpaSubItems);
5837 if (is_icon)
5839 DPA_DeletePtr(infoPtr->hdpaPosX, nItem);
5840 DPA_DeletePtr(infoPtr->hdpaPosY, nItem);
5843 infoPtr->nItemCount--;
5844 LISTVIEW_ShiftIndices(infoPtr, nItem, -1);
5845 LISTVIEW_ShiftFocus(infoPtr, focus, nItem, -1);
5847 /* now is the invalidation fun */
5848 if (!is_icon)
5849 LISTVIEW_ScrollOnInsert(infoPtr, nItem, -1);
5850 return TRUE;
5854 /***
5855 * DESCRIPTION:
5856 * Callback implementation for editlabel control
5858 * PARAMETER(S):
5859 * [I] infoPtr : valid pointer to the listview structure
5860 * [I] storeText : store edit box text as item text
5861 * [I] isW : TRUE if psxText is Unicode, FALSE if it's ANSI
5863 * RETURN:
5864 * SUCCESS : TRUE
5865 * FAILURE : FALSE
5867 static BOOL LISTVIEW_EndEditLabelT(LISTVIEW_INFO *infoPtr, BOOL storeText, BOOL isW)
5869 HWND hwndSelf = infoPtr->hwndSelf;
5870 WCHAR szDispText[DISP_TEXT_SIZE] = { 0 };
5871 NMLVDISPINFOW dispInfo;
5872 INT editedItem = infoPtr->nEditLabelItem;
5873 BOOL same;
5874 WCHAR *pszText = NULL;
5875 BOOL res;
5877 if (storeText)
5879 DWORD len = isW ? GetWindowTextLengthW(infoPtr->hwndEdit) : GetWindowTextLengthA(infoPtr->hwndEdit);
5881 if (len++)
5883 if (!(pszText = Alloc(len * (isW ? sizeof(WCHAR) : sizeof(CHAR)))))
5884 return FALSE;
5886 if (isW)
5887 GetWindowTextW(infoPtr->hwndEdit, pszText, len);
5888 else
5889 GetWindowTextA(infoPtr->hwndEdit, (CHAR*)pszText, len);
5893 TRACE("(pszText=%s, isW=%d)\n", debugtext_t(pszText, isW), isW);
5895 ZeroMemory(&dispInfo, sizeof(dispInfo));
5896 dispInfo.item.mask = LVIF_PARAM | LVIF_STATE | LVIF_TEXT;
5897 dispInfo.item.iItem = editedItem;
5898 dispInfo.item.iSubItem = 0;
5899 dispInfo.item.stateMask = ~0;
5900 dispInfo.item.pszText = szDispText;
5901 dispInfo.item.cchTextMax = DISP_TEXT_SIZE;
5902 if (!LISTVIEW_GetItemT(infoPtr, &dispInfo.item, isW))
5904 res = FALSE;
5905 goto cleanup;
5908 if (isW)
5909 same = (lstrcmpW(dispInfo.item.pszText, pszText) == 0);
5910 else
5912 LPWSTR tmp = textdupTtoW(pszText, FALSE);
5913 same = (lstrcmpW(dispInfo.item.pszText, tmp) == 0);
5914 textfreeT(tmp, FALSE);
5917 /* add the text from the edit in */
5918 dispInfo.item.mask |= LVIF_TEXT;
5919 dispInfo.item.pszText = same ? NULL : pszText;
5920 dispInfo.item.cchTextMax = textlenT(dispInfo.item.pszText, isW);
5922 infoPtr->notify_mask &= ~NOTIFY_MASK_END_LABEL_EDIT;
5924 /* Do we need to update the Item Text */
5925 res = notify_dispinfoT(infoPtr, LVN_ENDLABELEDITW, &dispInfo, isW);
5927 infoPtr->notify_mask |= NOTIFY_MASK_END_LABEL_EDIT;
5929 infoPtr->nEditLabelItem = -1;
5930 infoPtr->hwndEdit = 0;
5932 if (!res) goto cleanup;
5934 if (!IsWindow(hwndSelf))
5936 res = FALSE;
5937 goto cleanup;
5939 if (!pszText) return TRUE;
5940 if (same)
5942 res = TRUE;
5943 goto cleanup;
5946 if (!(infoPtr->dwStyle & LVS_OWNERDATA))
5948 HDPA hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, editedItem);
5949 ITEM_INFO* lpItem = DPA_GetPtr(hdpaSubItems, 0);
5950 if (lpItem && lpItem->hdr.pszText == LPSTR_TEXTCALLBACKW)
5952 LISTVIEW_InvalidateItem(infoPtr, editedItem);
5953 res = TRUE;
5954 goto cleanup;
5958 ZeroMemory(&dispInfo, sizeof(dispInfo));
5959 dispInfo.item.mask = LVIF_TEXT;
5960 dispInfo.item.iItem = editedItem;
5961 dispInfo.item.iSubItem = 0;
5962 dispInfo.item.pszText = pszText;
5963 dispInfo.item.cchTextMax = textlenT(pszText, isW);
5964 res = LISTVIEW_SetItemT(infoPtr, &dispInfo.item, isW);
5966 cleanup:
5967 Free(pszText);
5969 return res;
5972 /***
5973 * DESCRIPTION:
5974 * Subclassed edit control windproc function
5976 * PARAMETER(S):
5977 * [I] hwnd : the edit window handle
5978 * [I] uMsg : the message that is to be processed
5979 * [I] wParam : first message parameter
5980 * [I] lParam : second message parameter
5981 * [I] isW : TRUE if input is Unicode
5983 * RETURN:
5984 * Zero.
5986 static LRESULT EditLblWndProcT(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL isW)
5988 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)GetWindowLongPtrW(GetParent(hwnd), 0);
5989 BOOL save = TRUE;
5991 TRACE("(hwnd=%p, uMsg=%x, wParam=%lx, lParam=%lx, isW=%d)\n",
5992 hwnd, uMsg, wParam, lParam, isW);
5994 switch (uMsg)
5996 case WM_GETDLGCODE:
5997 return DLGC_WANTARROWS | DLGC_WANTALLKEYS;
5999 case WM_DESTROY:
6001 WNDPROC editProc = infoPtr->EditWndProc;
6002 infoPtr->EditWndProc = 0;
6003 SetWindowLongPtrW(hwnd, GWLP_WNDPROC, (DWORD_PTR)editProc);
6004 return CallWindowProcT(editProc, hwnd, uMsg, wParam, lParam, isW);
6007 case WM_KEYDOWN:
6008 if (VK_ESCAPE == (INT)wParam)
6010 save = FALSE;
6011 break;
6013 else if (VK_RETURN == (INT)wParam)
6014 break;
6016 default:
6017 return CallWindowProcT(infoPtr->EditWndProc, hwnd, uMsg, wParam, lParam, isW);
6020 /* kill the edit */
6021 if (infoPtr->hwndEdit)
6022 LISTVIEW_EndEditLabelT(infoPtr, save, isW);
6024 SendMessageW(hwnd, WM_CLOSE, 0, 0);
6025 return 0;
6028 /***
6029 * DESCRIPTION:
6030 * Subclassed edit control Unicode windproc function
6032 * PARAMETER(S):
6033 * [I] hwnd : the edit window handle
6034 * [I] uMsg : the message that is to be processed
6035 * [I] wParam : first message parameter
6036 * [I] lParam : second message parameter
6038 * RETURN:
6040 static LRESULT CALLBACK EditLblWndProcW(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
6042 return EditLblWndProcT(hwnd, uMsg, wParam, lParam, TRUE);
6045 /***
6046 * DESCRIPTION:
6047 * Subclassed edit control ANSI windproc function
6049 * PARAMETER(S):
6050 * [I] hwnd : the edit window handle
6051 * [I] uMsg : the message that is to be processed
6052 * [I] wParam : first message parameter
6053 * [I] lParam : second message parameter
6055 * RETURN:
6057 static LRESULT CALLBACK EditLblWndProcA(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
6059 return EditLblWndProcT(hwnd, uMsg, wParam, lParam, FALSE);
6062 /***
6063 * DESCRIPTION:
6064 * Creates a subclassed edit control
6066 * PARAMETER(S):
6067 * [I] infoPtr : valid pointer to the listview structure
6068 * [I] text : initial text for the edit
6069 * [I] style : the window style
6070 * [I] isW : TRUE if input is Unicode
6072 * RETURN:
6074 static HWND CreateEditLabelT(LISTVIEW_INFO *infoPtr, LPCWSTR text, BOOL isW)
6076 static const DWORD style = WS_CHILDWINDOW|WS_CLIPSIBLINGS|ES_LEFT|ES_AUTOHSCROLL|WS_BORDER|WS_VISIBLE;
6077 HINSTANCE hinst = (HINSTANCE)GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_HINSTANCE);
6078 HWND hedit;
6080 TRACE("(%p, text=%s, isW=%d)\n", infoPtr, debugtext_t(text, isW), isW);
6082 /* window will be resized and positioned after LVN_BEGINLABELEDIT */
6083 if (isW)
6084 hedit = CreateWindowW(WC_EDITW, text, style, 0, 0, 0, 0, infoPtr->hwndSelf, 0, hinst, 0);
6085 else
6086 hedit = CreateWindowA(WC_EDITA, (LPCSTR)text, style, 0, 0, 0, 0, infoPtr->hwndSelf, 0, hinst, 0);
6088 if (!hedit) return 0;
6090 infoPtr->EditWndProc = (WNDPROC)
6091 (isW ? SetWindowLongPtrW(hedit, GWLP_WNDPROC, (DWORD_PTR)EditLblWndProcW) :
6092 SetWindowLongPtrA(hedit, GWLP_WNDPROC, (DWORD_PTR)EditLblWndProcA) );
6094 SendMessageW(hedit, WM_SETFONT, (WPARAM)infoPtr->hFont, FALSE);
6095 SendMessageW(hedit, EM_SETLIMITTEXT, DISP_TEXT_SIZE-1, 0);
6097 return hedit;
6100 /***
6101 * DESCRIPTION:
6102 * Begin in place editing of specified list view item
6104 * PARAMETER(S):
6105 * [I] infoPtr : valid pointer to the listview structure
6106 * [I] nItem : item index
6107 * [I] isW : TRUE if it's a Unicode req, FALSE if ASCII
6109 * RETURN:
6110 * SUCCESS : TRUE
6111 * FAILURE : FALSE
6113 static HWND LISTVIEW_EditLabelT(LISTVIEW_INFO *infoPtr, INT nItem, BOOL isW)
6115 WCHAR disptextW[DISP_TEXT_SIZE] = { 0 };
6116 HWND hwndSelf = infoPtr->hwndSelf;
6117 NMLVDISPINFOW dispInfo;
6118 HFONT hOldFont = NULL;
6119 TEXTMETRICW tm;
6120 RECT rect;
6121 SIZE sz;
6122 HDC hdc;
6124 TRACE("(nItem=%d, isW=%d)\n", nItem, isW);
6126 if (~infoPtr->dwStyle & LVS_EDITLABELS) return 0;
6128 /* remove existing edit box */
6129 if (infoPtr->hwndEdit)
6131 SetFocus(infoPtr->hwndSelf);
6132 infoPtr->hwndEdit = 0;
6135 if (nItem < 0 || nItem >= infoPtr->nItemCount) return 0;
6137 infoPtr->nEditLabelItem = nItem;
6139 LISTVIEW_SetSelection(infoPtr, nItem);
6140 LISTVIEW_SetItemFocus(infoPtr, nItem);
6141 LISTVIEW_InvalidateItem(infoPtr, nItem);
6143 rect.left = LVIR_LABEL;
6144 if (!LISTVIEW_GetItemRect(infoPtr, nItem, &rect)) return 0;
6146 ZeroMemory(&dispInfo, sizeof(dispInfo));
6147 dispInfo.item.mask = LVIF_PARAM | LVIF_STATE | LVIF_TEXT;
6148 dispInfo.item.iItem = nItem;
6149 dispInfo.item.iSubItem = 0;
6150 dispInfo.item.stateMask = ~0;
6151 dispInfo.item.pszText = disptextW;
6152 dispInfo.item.cchTextMax = DISP_TEXT_SIZE;
6153 if (!LISTVIEW_GetItemT(infoPtr, &dispInfo.item, isW)) return 0;
6155 infoPtr->hwndEdit = CreateEditLabelT(infoPtr, dispInfo.item.pszText, isW);
6156 if (!infoPtr->hwndEdit) return 0;
6158 if (notify_dispinfoT(infoPtr, LVN_BEGINLABELEDITW, &dispInfo, isW))
6160 if (!IsWindow(hwndSelf))
6161 return 0;
6162 SendMessageW(infoPtr->hwndEdit, WM_CLOSE, 0, 0);
6163 infoPtr->hwndEdit = 0;
6164 return 0;
6167 TRACE("disp text=%s\n", debugtext_t(dispInfo.item.pszText, isW));
6169 /* position and display edit box */
6170 hdc = GetDC(infoPtr->hwndSelf);
6172 /* select the font to get appropriate metric dimensions */
6173 if (infoPtr->hFont)
6174 hOldFont = SelectObject(hdc, infoPtr->hFont);
6176 /* use real edit box content, it could be altered during LVN_BEGINLABELEDIT notification */
6177 GetWindowTextW(infoPtr->hwndEdit, disptextW, DISP_TEXT_SIZE);
6178 TRACE("edit box text=%s\n", debugstr_w(disptextW));
6180 /* get string length in pixels */
6181 GetTextExtentPoint32W(hdc, disptextW, lstrlenW(disptextW), &sz);
6183 /* add extra spacing for the next character */
6184 GetTextMetricsW(hdc, &tm);
6185 sz.cx += tm.tmMaxCharWidth * 2;
6187 if (infoPtr->hFont)
6188 SelectObject(hdc, hOldFont);
6190 ReleaseDC(infoPtr->hwndSelf, hdc);
6192 sz.cy = rect.bottom - rect.top + 2;
6193 rect.left -= 2;
6194 rect.top -= 1;
6195 TRACE("moving edit=(%d,%d)-(%d,%d)\n", rect.left, rect.top, sz.cx, sz.cy);
6196 MoveWindow(infoPtr->hwndEdit, rect.left, rect.top, sz.cx, sz.cy, FALSE);
6197 ShowWindow(infoPtr->hwndEdit, SW_NORMAL);
6198 SetFocus(infoPtr->hwndEdit);
6199 SendMessageW(infoPtr->hwndEdit, EM_SETSEL, 0, -1);
6200 return infoPtr->hwndEdit;
6204 /***
6205 * DESCRIPTION:
6206 * Ensures the specified item is visible, scrolling into view if necessary.
6208 * PARAMETER(S):
6209 * [I] infoPtr : valid pointer to the listview structure
6210 * [I] nItem : item index
6211 * [I] bPartial : partially or entirely visible
6213 * RETURN:
6214 * SUCCESS : TRUE
6215 * FAILURE : FALSE
6217 static BOOL LISTVIEW_EnsureVisible(LISTVIEW_INFO *infoPtr, INT nItem, BOOL bPartial)
6219 INT nScrollPosHeight = 0;
6220 INT nScrollPosWidth = 0;
6221 INT nHorzAdjust = 0;
6222 INT nVertAdjust = 0;
6223 INT nHorzDiff = 0;
6224 INT nVertDiff = 0;
6225 RECT rcItem, rcTemp;
6227 rcItem.left = LVIR_BOUNDS;
6228 if (!LISTVIEW_GetItemRect(infoPtr, nItem, &rcItem)) return FALSE;
6230 if (bPartial && IntersectRect(&rcTemp, &infoPtr->rcList, &rcItem)) return TRUE;
6232 if (rcItem.left < infoPtr->rcList.left || rcItem.right > infoPtr->rcList.right)
6234 /* scroll left/right, but in LV_VIEW_DETAILS mode */
6235 if (infoPtr->uView == LV_VIEW_LIST)
6236 nScrollPosWidth = infoPtr->nItemWidth;
6237 else if ((infoPtr->uView == LV_VIEW_SMALLICON) || (infoPtr->uView == LV_VIEW_ICON))
6238 nScrollPosWidth = 1;
6240 if (rcItem.left < infoPtr->rcList.left)
6242 nHorzAdjust = -1;
6243 if (infoPtr->uView != LV_VIEW_DETAILS) nHorzDiff = rcItem.left - infoPtr->rcList.left;
6245 else
6247 nHorzAdjust = 1;
6248 if (infoPtr->uView != LV_VIEW_DETAILS) nHorzDiff = rcItem.right - infoPtr->rcList.right;
6252 if (rcItem.top < infoPtr->rcList.top || rcItem.bottom > infoPtr->rcList.bottom)
6254 /* scroll up/down, but not in LVS_LIST mode */
6255 if (infoPtr->uView == LV_VIEW_DETAILS)
6256 nScrollPosHeight = infoPtr->nItemHeight;
6257 else if ((infoPtr->uView == LV_VIEW_ICON) || (infoPtr->uView == LV_VIEW_SMALLICON))
6258 nScrollPosHeight = 1;
6260 if (rcItem.top < infoPtr->rcList.top)
6262 nVertAdjust = -1;
6263 if (infoPtr->uView != LV_VIEW_LIST) nVertDiff = rcItem.top - infoPtr->rcList.top;
6265 else
6267 nVertAdjust = 1;
6268 if (infoPtr->uView != LV_VIEW_LIST) nVertDiff = rcItem.bottom - infoPtr->rcList.bottom;
6272 if (!nScrollPosWidth && !nScrollPosHeight) return TRUE;
6274 if (nScrollPosWidth)
6276 INT diff = nHorzDiff / nScrollPosWidth;
6277 if (nHorzDiff % nScrollPosWidth) diff += nHorzAdjust;
6278 LISTVIEW_HScroll(infoPtr, SB_INTERNAL, diff);
6281 if (nScrollPosHeight)
6283 INT diff = nVertDiff / nScrollPosHeight;
6284 if (nVertDiff % nScrollPosHeight) diff += nVertAdjust;
6285 LISTVIEW_VScroll(infoPtr, SB_INTERNAL, diff);
6288 return TRUE;
6291 /***
6292 * DESCRIPTION:
6293 * Searches for an item with specific characteristics.
6295 * PARAMETER(S):
6296 * [I] hwnd : window handle
6297 * [I] nStart : base item index
6298 * [I] lpFindInfo : item information to look for
6300 * RETURN:
6301 * SUCCESS : index of item
6302 * FAILURE : -1
6304 static INT LISTVIEW_FindItemW(const LISTVIEW_INFO *infoPtr, INT nStart,
6305 const LVFINDINFOW *lpFindInfo)
6307 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
6308 BOOL bWrap = FALSE, bNearest = FALSE;
6309 INT nItem = nStart + 1, nLast = infoPtr->nItemCount, nNearestItem = -1;
6310 ULONG xdist, ydist, dist, mindist = 0x7fffffff;
6311 POINT Position, Destination;
6312 int search_len = 0;
6313 LVITEMW lvItem;
6315 /* Search in virtual listviews should be done by application, not by
6316 listview control, so we just send LVN_ODFINDITEMW and return the result */
6317 if (infoPtr->dwStyle & LVS_OWNERDATA)
6319 NMLVFINDITEMW nmlv;
6321 nmlv.iStart = nStart;
6322 nmlv.lvfi = *lpFindInfo;
6323 return notify_hdr(infoPtr, LVN_ODFINDITEMW, (LPNMHDR)&nmlv.hdr);
6326 if (!lpFindInfo || nItem < 0) return -1;
6328 lvItem.mask = 0;
6329 if (lpFindInfo->flags & (LVFI_STRING | LVFI_PARTIAL | LVFI_SUBSTRING))
6331 lvItem.mask |= LVIF_TEXT;
6332 lvItem.pszText = szDispText;
6333 lvItem.cchTextMax = DISP_TEXT_SIZE;
6336 if (lpFindInfo->flags & LVFI_WRAP)
6337 bWrap = TRUE;
6339 if ((lpFindInfo->flags & LVFI_NEARESTXY) &&
6340 (infoPtr->uView == LV_VIEW_ICON || infoPtr->uView == LV_VIEW_SMALLICON))
6342 POINT Origin;
6343 RECT rcArea;
6345 LISTVIEW_GetOrigin(infoPtr, &Origin);
6346 Destination.x = lpFindInfo->pt.x - Origin.x;
6347 Destination.y = lpFindInfo->pt.y - Origin.y;
6348 switch(lpFindInfo->vkDirection)
6350 case VK_DOWN: Destination.y += infoPtr->nItemHeight; break;
6351 case VK_UP: Destination.y -= infoPtr->nItemHeight; break;
6352 case VK_RIGHT: Destination.x += infoPtr->nItemWidth; break;
6353 case VK_LEFT: Destination.x -= infoPtr->nItemWidth; break;
6354 case VK_HOME: Destination.x = Destination.y = 0; break;
6355 case VK_NEXT: Destination.y += infoPtr->rcList.bottom - infoPtr->rcList.top; break;
6356 case VK_PRIOR: Destination.y -= infoPtr->rcList.bottom - infoPtr->rcList.top; break;
6357 case VK_END:
6358 LISTVIEW_GetAreaRect(infoPtr, &rcArea);
6359 Destination.x = rcArea.right;
6360 Destination.y = rcArea.bottom;
6361 break;
6362 default: ERR("Unknown vkDirection=%d\n", lpFindInfo->vkDirection);
6364 bNearest = TRUE;
6366 else Destination.x = Destination.y = 0;
6368 /* if LVFI_PARAM is specified, all other flags are ignored */
6369 if (lpFindInfo->flags & LVFI_PARAM)
6371 lvItem.mask |= LVIF_PARAM;
6372 bNearest = FALSE;
6373 lvItem.mask &= ~LVIF_TEXT;
6376 nItem = bNearest ? -1 : nStart + 1;
6378 if (lpFindInfo->flags & (LVFI_PARTIAL | LVFI_SUBSTRING))
6379 search_len = lstrlenW(lpFindInfo->psz);
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 if (StrCmpNIW(lvItem.pszText, lpFindInfo->psz, search_len)) continue;
6403 else
6405 if (StrCmpIW(lvItem.pszText, lpFindInfo->psz)) continue;
6409 if (!bNearest) return nItem;
6411 /* This is very inefficient. To do a good job here,
6412 * we need a sorted array of (x,y) item positions */
6413 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position);
6415 /* compute the distance^2 to the destination */
6416 xdist = Destination.x - Position.x;
6417 ydist = Destination.y - Position.y;
6418 dist = xdist * xdist + ydist * ydist;
6420 /* remember the distance, and item if it's closer */
6421 if (dist < mindist)
6423 mindist = dist;
6424 nNearestItem = nItem;
6428 if (bWrap)
6430 nItem = 0;
6431 nLast = min(nStart + 1, infoPtr->nItemCount);
6432 bWrap = FALSE;
6433 goto again;
6436 return nNearestItem;
6439 /***
6440 * DESCRIPTION:
6441 * Searches for an item with specific characteristics.
6443 * PARAMETER(S):
6444 * [I] hwnd : window handle
6445 * [I] nStart : base item index
6446 * [I] lpFindInfo : item information to look for
6448 * RETURN:
6449 * SUCCESS : index of item
6450 * FAILURE : -1
6452 static INT LISTVIEW_FindItemA(const LISTVIEW_INFO *infoPtr, INT nStart,
6453 const LVFINDINFOA *lpFindInfo)
6455 LVFINDINFOW fiw;
6456 INT res;
6457 LPWSTR strW = NULL;
6459 memcpy(&fiw, lpFindInfo, sizeof(fiw));
6460 if (lpFindInfo->flags & (LVFI_STRING | LVFI_PARTIAL | LVFI_SUBSTRING))
6461 fiw.psz = strW = textdupTtoW((LPCWSTR)lpFindInfo->psz, FALSE);
6462 res = LISTVIEW_FindItemW(infoPtr, nStart, &fiw);
6463 textfreeT(strW, FALSE);
6464 return res;
6467 /***
6468 * DESCRIPTION:
6469 * Retrieves column attributes.
6471 * PARAMETER(S):
6472 * [I] infoPtr : valid pointer to the listview structure
6473 * [I] nColumn : column index
6474 * [IO] lpColumn : column information
6475 * [I] isW : if TRUE, then lpColumn is a LPLVCOLUMNW
6476 * otherwise it is in fact a LPLVCOLUMNA
6478 * RETURN:
6479 * SUCCESS : TRUE
6480 * FAILURE : FALSE
6482 static BOOL LISTVIEW_GetColumnT(const LISTVIEW_INFO *infoPtr, INT nColumn, LPLVCOLUMNW lpColumn, BOOL isW)
6484 COLUMN_INFO *lpColumnInfo;
6485 HDITEMW hdi;
6487 if (!lpColumn || nColumn < 0 || nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return FALSE;
6488 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, nColumn);
6490 /* initialize memory */
6491 ZeroMemory(&hdi, sizeof(hdi));
6493 if (lpColumn->mask & LVCF_TEXT)
6495 hdi.mask |= HDI_TEXT;
6496 hdi.pszText = lpColumn->pszText;
6497 hdi.cchTextMax = lpColumn->cchTextMax;
6500 if (lpColumn->mask & LVCF_IMAGE)
6501 hdi.mask |= HDI_IMAGE;
6503 if (lpColumn->mask & LVCF_ORDER)
6504 hdi.mask |= HDI_ORDER;
6506 if (lpColumn->mask & LVCF_SUBITEM)
6507 hdi.mask |= HDI_LPARAM;
6509 if (!SendMessageW(infoPtr->hwndHeader, isW ? HDM_GETITEMW : HDM_GETITEMA, nColumn, (LPARAM)&hdi)) return FALSE;
6511 if (lpColumn->mask & LVCF_FMT)
6512 lpColumn->fmt = lpColumnInfo->fmt;
6514 if (lpColumn->mask & LVCF_WIDTH)
6515 lpColumn->cx = lpColumnInfo->rcHeader.right - lpColumnInfo->rcHeader.left;
6517 if (lpColumn->mask & LVCF_IMAGE)
6518 lpColumn->iImage = hdi.iImage;
6520 if (lpColumn->mask & LVCF_ORDER)
6521 lpColumn->iOrder = hdi.iOrder;
6523 if (lpColumn->mask & LVCF_SUBITEM)
6524 lpColumn->iSubItem = hdi.lParam;
6526 if (lpColumn->mask & LVCF_MINWIDTH)
6527 lpColumn->cxMin = lpColumnInfo->cxMin;
6529 return TRUE;
6532 static inline BOOL LISTVIEW_GetColumnOrderArray(const LISTVIEW_INFO *infoPtr, INT iCount, LPINT lpiArray)
6534 if (!infoPtr->hwndHeader) return FALSE;
6535 return SendMessageW(infoPtr->hwndHeader, HDM_GETORDERARRAY, iCount, (LPARAM)lpiArray);
6538 /***
6539 * DESCRIPTION:
6540 * Retrieves the column width.
6542 * PARAMETER(S):
6543 * [I] infoPtr : valid pointer to the listview structure
6544 * [I] int : column index
6546 * RETURN:
6547 * SUCCESS : column width
6548 * FAILURE : zero
6550 static INT LISTVIEW_GetColumnWidth(const LISTVIEW_INFO *infoPtr, INT nColumn)
6552 INT nColumnWidth = 0;
6553 HDITEMW hdItem;
6555 TRACE("nColumn=%d\n", nColumn);
6557 /* we have a 'column' in LIST and REPORT mode only */
6558 switch(infoPtr->uView)
6560 case LV_VIEW_LIST:
6561 nColumnWidth = infoPtr->nItemWidth;
6562 break;
6563 case LV_VIEW_DETAILS:
6564 /* We are not using LISTVIEW_GetHeaderRect as this data is updated only after a HDN_ITEMCHANGED.
6565 * There is an application that subclasses the listview, calls LVM_GETCOLUMNWIDTH in the
6566 * HDN_ITEMCHANGED handler and goes into infinite recursion if it receives old data.
6568 hdItem.mask = HDI_WIDTH;
6569 if (!SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, nColumn, (LPARAM)&hdItem))
6571 WARN("(%p): HDM_GETITEMW failed for item %d\n", infoPtr->hwndSelf, nColumn);
6572 return 0;
6574 nColumnWidth = hdItem.cxy;
6575 break;
6578 TRACE("nColumnWidth=%d\n", nColumnWidth);
6579 return nColumnWidth;
6582 /***
6583 * DESCRIPTION:
6584 * In list or report display mode, retrieves the number of items that can fit
6585 * vertically in the visible area. In icon or small icon display mode,
6586 * retrieves the total number of visible items.
6588 * PARAMETER(S):
6589 * [I] infoPtr : valid pointer to the listview structure
6591 * RETURN:
6592 * Number of fully visible items.
6594 static INT LISTVIEW_GetCountPerPage(const LISTVIEW_INFO *infoPtr)
6596 switch (infoPtr->uView)
6598 case LV_VIEW_ICON:
6599 case LV_VIEW_SMALLICON:
6600 return infoPtr->nItemCount;
6601 case LV_VIEW_DETAILS:
6602 return LISTVIEW_GetCountPerColumn(infoPtr);
6603 case LV_VIEW_LIST:
6604 return LISTVIEW_GetCountPerRow(infoPtr) * LISTVIEW_GetCountPerColumn(infoPtr);
6606 assert(FALSE);
6607 return 0;
6610 /***
6611 * DESCRIPTION:
6612 * Retrieves an image list handle.
6614 * PARAMETER(S):
6615 * [I] infoPtr : valid pointer to the listview structure
6616 * [I] nImageList : image list identifier
6618 * RETURN:
6619 * SUCCESS : image list handle
6620 * FAILURE : NULL
6622 static HIMAGELIST LISTVIEW_GetImageList(const LISTVIEW_INFO *infoPtr, INT nImageList)
6624 switch (nImageList)
6626 case LVSIL_NORMAL: return infoPtr->himlNormal;
6627 case LVSIL_SMALL: return infoPtr->himlSmall;
6628 case LVSIL_STATE: return infoPtr->himlState;
6629 case LVSIL_GROUPHEADER:
6630 FIXME("LVSIL_GROUPHEADER not supported\n");
6631 break;
6632 default:
6633 WARN("got unknown imagelist index - %d\n", nImageList);
6635 return NULL;
6638 /* LISTVIEW_GetISearchString */
6640 /***
6641 * DESCRIPTION:
6642 * Retrieves item attributes.
6644 * PARAMETER(S):
6645 * [I] hwnd : window handle
6646 * [IO] lpLVItem : item info
6647 * [I] isW : if TRUE, then lpLVItem is a LPLVITEMW,
6648 * if FALSE, then lpLVItem is a LPLVITEMA.
6650 * NOTE:
6651 * This is the internal 'GetItem' interface -- it tries to
6652 * be smart and avoid text copies, if possible, by modifying
6653 * lpLVItem->pszText to point to the text string. Please note
6654 * that this is not always possible (e.g. OWNERDATA), so on
6655 * entry you *must* supply valid values for pszText, and cchTextMax.
6656 * The only difference to the documented interface is that upon
6657 * return, you should use *only* the lpLVItem->pszText, rather than
6658 * the buffer pointer you provided on input. Most code already does
6659 * that, so it's not a problem.
6660 * For the two cases when the text must be copied (that is,
6661 * for LVM_GETITEM, and LVM_GETITEMTEXT), use LISTVIEW_GetItemExtT.
6663 * RETURN:
6664 * SUCCESS : TRUE
6665 * FAILURE : FALSE
6667 static BOOL LISTVIEW_GetItemT(const LISTVIEW_INFO *infoPtr, LPLVITEMW lpLVItem, BOOL isW)
6669 ITEMHDR callbackHdr = { LPSTR_TEXTCALLBACKW, I_IMAGECALLBACK };
6670 BOOL is_subitem_invalid = FALSE;
6671 NMLVDISPINFOW dispInfo;
6672 ITEM_INFO *lpItem;
6673 ITEMHDR* pItemHdr;
6674 HDPA hdpaSubItems;
6676 TRACE("(item=%s, isW=%d)\n", debuglvitem_t(lpLVItem, isW), isW);
6678 if (!lpLVItem || lpLVItem->iItem < 0 || lpLVItem->iItem >= infoPtr->nItemCount)
6679 return FALSE;
6681 if (lpLVItem->mask == 0) return TRUE;
6682 TRACE("mask=%x\n", lpLVItem->mask);
6684 if (lpLVItem->iSubItem && (lpLVItem->mask & LVIF_STATE))
6685 lpLVItem->state = 0;
6687 /* a quick optimization if all we're asked is the focus state
6688 * these queries are worth optimising since they are common,
6689 * and can be answered in constant time, without the heavy accesses */
6690 if ( (lpLVItem->mask == LVIF_STATE) && (lpLVItem->stateMask == LVIS_FOCUSED) &&
6691 !(infoPtr->uCallbackMask & LVIS_FOCUSED) )
6693 lpLVItem->state = 0;
6694 if (infoPtr->nFocusedItem == lpLVItem->iItem && !lpLVItem->iSubItem)
6695 lpLVItem->state |= LVIS_FOCUSED;
6696 return TRUE;
6699 ZeroMemory(&dispInfo, sizeof(dispInfo));
6701 /* if the app stores all the data, handle it separately */
6702 if (infoPtr->dwStyle & LVS_OWNERDATA)
6704 dispInfo.item.state = 0;
6706 /* apparently, we should not callback for lParam in LVS_OWNERDATA */
6707 if ((lpLVItem->mask & ~(LVIF_STATE | LVIF_PARAM)) ||
6708 ((lpLVItem->mask & LVIF_STATE) && (infoPtr->uCallbackMask & lpLVItem->stateMask)))
6710 UINT mask = lpLVItem->mask;
6712 /* NOTE: copy only fields which we _know_ are initialized, some apps
6713 * depend on the uninitialized fields being 0 */
6714 dispInfo.item.mask = lpLVItem->mask & ~LVIF_PARAM;
6715 dispInfo.item.iItem = lpLVItem->iItem;
6716 dispInfo.item.iSubItem = lpLVItem->iSubItem;
6717 if (lpLVItem->mask & LVIF_TEXT)
6719 if (lpLVItem->mask & LVIF_NORECOMPUTE)
6720 /* reset mask */
6721 dispInfo.item.mask &= ~(LVIF_TEXT | LVIF_NORECOMPUTE);
6722 else
6724 dispInfo.item.pszText = lpLVItem->pszText;
6725 dispInfo.item.cchTextMax = lpLVItem->cchTextMax;
6728 if (lpLVItem->mask & LVIF_STATE)
6729 dispInfo.item.stateMask = lpLVItem->stateMask & infoPtr->uCallbackMask;
6730 /* could be zeroed on LVIF_NORECOMPUTE case */
6731 if (dispInfo.item.mask)
6733 notify_dispinfoT(infoPtr, LVN_GETDISPINFOW, &dispInfo, isW);
6734 dispInfo.item.stateMask = lpLVItem->stateMask;
6735 if (lpLVItem->mask & (LVIF_GROUPID|LVIF_COLUMNS))
6737 /* full size structure expected - _WIN32IE >= 0x560 */
6738 *lpLVItem = dispInfo.item;
6740 else if (lpLVItem->mask & LVIF_INDENT)
6742 /* indent member expected - _WIN32IE >= 0x300 */
6743 memcpy(lpLVItem, &dispInfo.item, offsetof( LVITEMW, iGroupId ));
6745 else
6747 /* minimal structure expected */
6748 memcpy(lpLVItem, &dispInfo.item, offsetof( LVITEMW, iIndent ));
6750 lpLVItem->mask = mask;
6751 TRACE(" getdispinfo(1):lpLVItem=%s\n", debuglvitem_t(lpLVItem, isW));
6755 /* make sure lParam is zeroed out */
6756 if (lpLVItem->mask & LVIF_PARAM) lpLVItem->lParam = 0;
6758 /* callback marked pointer required here */
6759 if ((lpLVItem->mask & LVIF_TEXT) && (lpLVItem->mask & LVIF_NORECOMPUTE))
6760 lpLVItem->pszText = LPSTR_TEXTCALLBACKW;
6762 /* we store only a little state, so if we're not asked, we're done */
6763 if (!(lpLVItem->mask & LVIF_STATE) || lpLVItem->iSubItem) return TRUE;
6765 /* if focus is handled by us, report it */
6766 if ( lpLVItem->stateMask & ~infoPtr->uCallbackMask & LVIS_FOCUSED )
6768 lpLVItem->state &= ~LVIS_FOCUSED;
6769 if (infoPtr->nFocusedItem == lpLVItem->iItem)
6770 lpLVItem->state |= LVIS_FOCUSED;
6773 /* and do the same for selection, if we handle it */
6774 if ( lpLVItem->stateMask & ~infoPtr->uCallbackMask & LVIS_SELECTED )
6776 lpLVItem->state &= ~LVIS_SELECTED;
6777 if (ranges_contain(infoPtr->selectionRanges, lpLVItem->iItem))
6778 lpLVItem->state |= LVIS_SELECTED;
6781 return TRUE;
6784 /* find the item and subitem structures before we proceed */
6785 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, lpLVItem->iItem);
6786 lpItem = DPA_GetPtr(hdpaSubItems, 0);
6787 assert (lpItem);
6789 if (lpLVItem->iSubItem)
6791 SUBITEM_INFO *lpSubItem = LISTVIEW_GetSubItemPtr(hdpaSubItems, lpLVItem->iSubItem);
6792 if (lpSubItem)
6793 pItemHdr = &lpSubItem->hdr;
6794 else
6796 pItemHdr = &callbackHdr;
6797 is_subitem_invalid = TRUE;
6800 else
6801 pItemHdr = &lpItem->hdr;
6803 /* Do we need to query the state from the app? */
6804 if ((lpLVItem->mask & LVIF_STATE) && infoPtr->uCallbackMask && (!lpLVItem->iSubItem || is_subitem_invalid))
6806 dispInfo.item.mask |= LVIF_STATE;
6807 dispInfo.item.stateMask = infoPtr->uCallbackMask;
6810 /* Do we need to enquire about the image? */
6811 if ((lpLVItem->mask & LVIF_IMAGE) && pItemHdr->iImage == I_IMAGECALLBACK &&
6812 (!lpLVItem->iSubItem || (infoPtr->dwLvExStyle & LVS_EX_SUBITEMIMAGES)))
6814 dispInfo.item.mask |= LVIF_IMAGE;
6815 dispInfo.item.iImage = I_IMAGECALLBACK;
6818 /* Only items support indentation */
6819 if ((lpLVItem->mask & LVIF_INDENT) && lpItem->iIndent == I_INDENTCALLBACK && !lpLVItem->iSubItem)
6821 dispInfo.item.mask |= LVIF_INDENT;
6822 dispInfo.item.iIndent = I_INDENTCALLBACK;
6825 /* Apps depend on calling back for text if it is NULL or LPSTR_TEXTCALLBACKW */
6826 if ((lpLVItem->mask & LVIF_TEXT) && !(lpLVItem->mask & LVIF_NORECOMPUTE) &&
6827 !is_text(pItemHdr->pszText))
6829 dispInfo.item.mask |= LVIF_TEXT;
6830 dispInfo.item.pszText = lpLVItem->pszText;
6831 dispInfo.item.cchTextMax = lpLVItem->cchTextMax;
6832 if (dispInfo.item.pszText && dispInfo.item.cchTextMax > 0)
6833 *dispInfo.item.pszText = '\0';
6836 /* If we don't have all the requested info, query the application */
6837 if (dispInfo.item.mask)
6839 dispInfo.item.iItem = lpLVItem->iItem;
6840 dispInfo.item.iSubItem = lpLVItem->iSubItem;
6841 dispInfo.item.lParam = lpItem->lParam;
6842 notify_dispinfoT(infoPtr, LVN_GETDISPINFOW, &dispInfo, isW);
6843 TRACE(" getdispinfo(2):item=%s\n", debuglvitem_t(&dispInfo.item, isW));
6846 /* we should not store values for subitems */
6847 if (lpLVItem->iSubItem) dispInfo.item.mask &= ~LVIF_DI_SETITEM;
6849 /* Now, handle the iImage field */
6850 if (dispInfo.item.mask & LVIF_IMAGE)
6852 lpLVItem->iImage = dispInfo.item.iImage;
6853 if ((dispInfo.item.mask & LVIF_DI_SETITEM) && pItemHdr->iImage == I_IMAGECALLBACK)
6854 pItemHdr->iImage = dispInfo.item.iImage;
6856 else if (lpLVItem->mask & LVIF_IMAGE)
6858 if (!lpLVItem->iSubItem || (infoPtr->dwLvExStyle & LVS_EX_SUBITEMIMAGES))
6859 lpLVItem->iImage = pItemHdr->iImage;
6860 else
6861 lpLVItem->iImage = 0;
6864 /* The pszText field */
6865 if (dispInfo.item.mask & LVIF_TEXT)
6867 if ((dispInfo.item.mask & LVIF_DI_SETITEM) && pItemHdr->pszText)
6868 textsetptrT(&pItemHdr->pszText, dispInfo.item.pszText, isW);
6870 lpLVItem->pszText = dispInfo.item.pszText;
6872 else if (lpLVItem->mask & LVIF_TEXT)
6874 /* if LVN_GETDISPINFO's disabled with LVIF_NORECOMPUTE return callback placeholder */
6875 if (isW || !is_text(pItemHdr->pszText)) lpLVItem->pszText = pItemHdr->pszText;
6876 else textcpynT(lpLVItem->pszText, isW, pItemHdr->pszText, TRUE, lpLVItem->cchTextMax);
6879 /* Next is the lParam field */
6880 if (dispInfo.item.mask & LVIF_PARAM)
6882 lpLVItem->lParam = dispInfo.item.lParam;
6883 if ((dispInfo.item.mask & LVIF_DI_SETITEM))
6884 lpItem->lParam = dispInfo.item.lParam;
6886 else if (lpLVItem->mask & LVIF_PARAM)
6887 lpLVItem->lParam = lpItem->lParam;
6889 /* if this is a subitem, we're done */
6890 if (lpLVItem->iSubItem) return TRUE;
6892 /* ... the state field (this one is different due to uCallbackmask) */
6893 if (lpLVItem->mask & LVIF_STATE)
6895 lpLVItem->state = lpItem->state & lpLVItem->stateMask;
6896 if (dispInfo.item.mask & LVIF_STATE)
6898 lpLVItem->state &= ~dispInfo.item.stateMask;
6899 lpLVItem->state |= (dispInfo.item.state & dispInfo.item.stateMask);
6901 if ( lpLVItem->stateMask & ~infoPtr->uCallbackMask & LVIS_FOCUSED )
6903 lpLVItem->state &= ~LVIS_FOCUSED;
6904 if (infoPtr->nFocusedItem == lpLVItem->iItem)
6905 lpLVItem->state |= LVIS_FOCUSED;
6907 if ( lpLVItem->stateMask & ~infoPtr->uCallbackMask & LVIS_SELECTED )
6909 lpLVItem->state &= ~LVIS_SELECTED;
6910 if (ranges_contain(infoPtr->selectionRanges, lpLVItem->iItem))
6911 lpLVItem->state |= LVIS_SELECTED;
6915 /* and last, but not least, the indent field */
6916 if (dispInfo.item.mask & LVIF_INDENT)
6918 lpLVItem->iIndent = dispInfo.item.iIndent;
6919 if ((dispInfo.item.mask & LVIF_DI_SETITEM) && lpItem->iIndent == I_INDENTCALLBACK)
6920 lpItem->iIndent = dispInfo.item.iIndent;
6922 else if (lpLVItem->mask & LVIF_INDENT)
6924 lpLVItem->iIndent = lpItem->iIndent;
6927 return TRUE;
6930 /***
6931 * DESCRIPTION:
6932 * Retrieves item attributes.
6934 * PARAMETER(S):
6935 * [I] hwnd : window handle
6936 * [IO] lpLVItem : item info
6937 * [I] isW : if TRUE, then lpLVItem is a LPLVITEMW,
6938 * if FALSE, then lpLVItem is a LPLVITEMA.
6940 * NOTE:
6941 * This is the external 'GetItem' interface -- it properly copies
6942 * the text in the provided buffer.
6944 * RETURN:
6945 * SUCCESS : TRUE
6946 * FAILURE : FALSE
6948 static BOOL LISTVIEW_GetItemExtT(const LISTVIEW_INFO *infoPtr, LPLVITEMW lpLVItem, BOOL isW)
6950 LPWSTR pszText;
6951 BOOL bResult;
6953 if (!lpLVItem || lpLVItem->iItem < 0 || lpLVItem->iItem >= infoPtr->nItemCount)
6954 return FALSE;
6956 pszText = lpLVItem->pszText;
6957 bResult = LISTVIEW_GetItemT(infoPtr, lpLVItem, isW);
6958 if (bResult && (lpLVItem->mask & LVIF_TEXT) && lpLVItem->pszText != pszText)
6960 if (lpLVItem->pszText != LPSTR_TEXTCALLBACKW)
6961 textcpynT(pszText, isW, lpLVItem->pszText, isW, lpLVItem->cchTextMax);
6962 else
6963 pszText = LPSTR_TEXTCALLBACKW;
6965 lpLVItem->pszText = pszText;
6967 return bResult;
6971 /***
6972 * DESCRIPTION:
6973 * Retrieves the position (upper-left) of the listview control item.
6974 * Note that for LVS_ICON style, the upper-left is that of the icon
6975 * and not the bounding box.
6977 * PARAMETER(S):
6978 * [I] infoPtr : valid pointer to the listview structure
6979 * [I] nItem : item index
6980 * [O] lpptPosition : coordinate information
6982 * RETURN:
6983 * SUCCESS : TRUE
6984 * FAILURE : FALSE
6986 static BOOL LISTVIEW_GetItemPosition(const LISTVIEW_INFO *infoPtr, INT nItem, LPPOINT lpptPosition)
6988 POINT Origin;
6990 TRACE("(nItem=%d, lpptPosition=%p)\n", nItem, lpptPosition);
6992 if (!lpptPosition || nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
6994 LISTVIEW_GetOrigin(infoPtr, &Origin);
6995 LISTVIEW_GetItemOrigin(infoPtr, nItem, lpptPosition);
6997 if (infoPtr->uView == LV_VIEW_ICON)
6999 lpptPosition->x += (infoPtr->nItemWidth - infoPtr->iconSize.cx) / 2;
7000 lpptPosition->y += ICON_TOP_PADDING;
7002 lpptPosition->x += Origin.x;
7003 lpptPosition->y += Origin.y;
7005 TRACE (" lpptPosition=%s\n", wine_dbgstr_point(lpptPosition));
7006 return TRUE;
7010 /***
7011 * DESCRIPTION:
7012 * Retrieves the bounding rectangle for a listview control item.
7014 * PARAMETER(S):
7015 * [I] infoPtr : valid pointer to the listview structure
7016 * [I] nItem : item index
7017 * [IO] lprc : bounding rectangle coordinates
7018 * lprc->left specifies the portion of the item for which the bounding
7019 * rectangle will be retrieved.
7021 * LVIR_BOUNDS Returns the bounding rectangle of the entire item,
7022 * including the icon and label.
7024 * * For LVS_ICON
7025 * * Experiment shows that native control returns:
7026 * * width = min (48, length of text line)
7027 * * .left = position.x - (width - iconsize.cx)/2
7028 * * .right = .left + width
7029 * * height = #lines of text * ntmHeight + icon height + 8
7030 * * .top = position.y - 2
7031 * * .bottom = .top + height
7032 * * separation between items .y = itemSpacing.cy - height
7033 * * .x = itemSpacing.cx - width
7034 * LVIR_ICON Returns the bounding rectangle of the icon or small icon.
7036 * * For LVS_ICON
7037 * * Experiment shows that native control returns:
7038 * * width = iconSize.cx + 16
7039 * * .left = position.x - (width - iconsize.cx)/2
7040 * * .right = .left + width
7041 * * height = iconSize.cy + 4
7042 * * .top = position.y - 2
7043 * * .bottom = .top + height
7044 * * separation between items .y = itemSpacing.cy - height
7045 * * .x = itemSpacing.cx - width
7046 * LVIR_LABEL Returns the bounding rectangle of the item text.
7048 * * For LVS_ICON
7049 * * Experiment shows that native control returns:
7050 * * width = text length
7051 * * .left = position.x - width/2
7052 * * .right = .left + width
7053 * * height = ntmH * linecount + 2
7054 * * .top = position.y + iconSize.cy + 6
7055 * * .bottom = .top + height
7056 * * separation between items .y = itemSpacing.cy - height
7057 * * .x = itemSpacing.cx - width
7058 * LVIR_SELECTBOUNDS Returns the union of the LVIR_ICON and LVIR_LABEL
7059 * rectangles, but excludes columns in report view.
7061 * RETURN:
7062 * SUCCESS : TRUE
7063 * FAILURE : FALSE
7065 * NOTES
7066 * Note that the bounding rectangle of the label in the LVS_ICON view depends
7067 * upon whether the window has the focus currently and on whether the item
7068 * is the one with the focus. Ensure that the control's record of which
7069 * item has the focus agrees with the items' records.
7071 static BOOL LISTVIEW_GetItemRect(const LISTVIEW_INFO *infoPtr, INT nItem, LPRECT lprc)
7073 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
7074 BOOL doLabel = TRUE, oversizedBox = FALSE;
7075 POINT Position, Origin;
7076 LVITEMW lvItem;
7077 LONG mode;
7079 TRACE("(hwnd=%p, nItem=%d, lprc=%p)\n", infoPtr->hwndSelf, nItem, lprc);
7081 if (!lprc || nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
7083 LISTVIEW_GetOrigin(infoPtr, &Origin);
7084 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position);
7086 /* Be smart and try to figure out the minimum we have to do */
7087 if (lprc->left == LVIR_ICON) doLabel = FALSE;
7088 if (infoPtr->uView == LV_VIEW_DETAILS && lprc->left == LVIR_BOUNDS) doLabel = FALSE;
7089 if (infoPtr->uView == LV_VIEW_ICON && lprc->left != LVIR_ICON &&
7090 infoPtr->bFocus && LISTVIEW_GetItemState(infoPtr, nItem, LVIS_FOCUSED))
7091 oversizedBox = TRUE;
7093 /* get what we need from the item before hand, so we make
7094 * only one request. This can speed up things, if data
7095 * is stored on the app side */
7096 lvItem.mask = 0;
7097 if (infoPtr->uView == LV_VIEW_DETAILS) lvItem.mask |= LVIF_INDENT;
7098 if (doLabel) lvItem.mask |= LVIF_TEXT;
7099 lvItem.iItem = nItem;
7100 lvItem.iSubItem = 0;
7101 lvItem.pszText = szDispText;
7102 lvItem.cchTextMax = DISP_TEXT_SIZE;
7103 if (lvItem.mask && !LISTVIEW_GetItemW(infoPtr, &lvItem)) return FALSE;
7104 /* we got the state already up, simulate it here, to avoid a reget */
7105 if (infoPtr->uView == LV_VIEW_ICON && (lprc->left != LVIR_ICON))
7107 lvItem.mask |= LVIF_STATE;
7108 lvItem.stateMask = LVIS_FOCUSED;
7109 lvItem.state = (oversizedBox ? LVIS_FOCUSED : 0);
7112 if (infoPtr->uView == LV_VIEW_DETAILS && (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT) && lprc->left == LVIR_SELECTBOUNDS)
7113 lprc->left = LVIR_BOUNDS;
7115 mode = lprc->left;
7116 switch(lprc->left)
7118 case LVIR_ICON:
7119 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, NULL, NULL, lprc, NULL, NULL);
7120 break;
7122 case LVIR_LABEL:
7123 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, NULL, NULL, NULL, NULL, lprc);
7124 break;
7126 case LVIR_BOUNDS:
7127 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, lprc, NULL, NULL, NULL, NULL);
7128 break;
7130 case LVIR_SELECTBOUNDS:
7131 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, NULL, lprc, NULL, NULL, NULL);
7132 break;
7134 default:
7135 WARN("Unknown value: %d\n", lprc->left);
7136 return FALSE;
7139 if (infoPtr->uView == LV_VIEW_DETAILS)
7141 if (mode != LVIR_BOUNDS)
7142 OffsetRect(lprc, Origin.x + LISTVIEW_GetColumnInfo(infoPtr, 0)->rcHeader.left,
7143 Position.y + Origin.y);
7144 else
7145 OffsetRect(lprc, Origin.x, Position.y + Origin.y);
7147 else
7148 OffsetRect(lprc, Position.x + Origin.x, Position.y + Origin.y);
7150 TRACE(" rect=%s\n", wine_dbgstr_rect(lprc));
7152 return TRUE;
7155 /***
7156 * DESCRIPTION:
7157 * Retrieves the spacing between listview control items.
7159 * PARAMETER(S):
7160 * [I] infoPtr : valid pointer to the listview structure
7161 * [IO] lprc : rectangle to receive the output
7162 * on input, lprc->top = nSubItem
7163 * lprc->left = LVIR_ICON | LVIR_BOUNDS | LVIR_LABEL
7165 * NOTE: for subItem = 0, we should return the bounds of the _entire_ item,
7166 * not only those of the first column.
7168 * RETURN:
7169 * TRUE: success
7170 * FALSE: failure
7172 static BOOL LISTVIEW_GetSubItemRect(const LISTVIEW_INFO *infoPtr, INT item, LPRECT lprc)
7174 RECT rect = { 0, 0, 0, 0 };
7175 POINT origin;
7176 INT y;
7178 if (!lprc) return FALSE;
7180 TRACE("(item=%d, subitem=%d, type=%d)\n", item, lprc->top, lprc->left);
7181 /* Subitem of '0' means item itself, and this works for all control view modes */
7182 if (lprc->top == 0)
7183 return LISTVIEW_GetItemRect(infoPtr, item, lprc);
7185 if (infoPtr->uView != LV_VIEW_DETAILS) return FALSE;
7187 LISTVIEW_GetOrigin(infoPtr, &origin);
7188 /* this works for any item index, no matter if it exists or not */
7189 y = item * infoPtr->nItemHeight + origin.y;
7191 if (infoPtr->hwndHeader && SendMessageW(infoPtr->hwndHeader, HDM_GETITEMRECT, lprc->top, (LPARAM)&rect))
7193 rect.top = 0;
7194 rect.bottom = infoPtr->nItemHeight;
7196 else
7198 /* Native implementation is broken for this case and garbage is left for left and right fields,
7199 we zero them to get predictable output */
7200 lprc->left = lprc->right = lprc->top = 0;
7201 lprc->bottom = infoPtr->nItemHeight;
7202 OffsetRect(lprc, origin.x, y);
7203 TRACE("return rect %s\n", wine_dbgstr_rect(lprc));
7204 return TRUE;
7207 switch (lprc->left)
7209 case LVIR_ICON:
7211 /* it doesn't matter if main item actually has an icon, if imagelist is set icon width is returned */
7212 if (infoPtr->himlSmall)
7213 rect.right = rect.left + infoPtr->iconSize.cx;
7214 else
7215 rect.right = rect.left;
7217 rect.bottom = rect.top + infoPtr->iconSize.cy;
7218 break;
7220 case LVIR_LABEL:
7221 case LVIR_BOUNDS:
7222 break;
7224 default:
7225 ERR("Unknown bounds=%d\n", lprc->left);
7226 return FALSE;
7229 OffsetRect(&rect, origin.x, y);
7230 *lprc = rect;
7231 TRACE("return rect %s\n", wine_dbgstr_rect(lprc));
7233 return TRUE;
7236 /***
7237 * DESCRIPTION:
7238 * Retrieves the spacing between listview control items.
7240 * PARAMETER(S):
7241 * [I] infoPtr : valid pointer to the listview structure
7242 * [I] bSmall : flag for small or large icon
7244 * RETURN:
7245 * Horizontal + vertical spacing
7247 static LONG LISTVIEW_GetItemSpacing(const LISTVIEW_INFO *infoPtr, BOOL bSmall)
7249 LONG lResult;
7251 if (!bSmall)
7253 lResult = MAKELONG(infoPtr->iconSpacing.cx, infoPtr->iconSpacing.cy);
7255 else
7257 if (infoPtr->uView == LV_VIEW_ICON)
7258 lResult = MAKELONG(DEFAULT_COLUMN_WIDTH, GetSystemMetrics(SM_CXSMICON)+HEIGHT_PADDING);
7259 else
7260 lResult = MAKELONG(infoPtr->nItemWidth, infoPtr->nItemHeight);
7262 return lResult;
7265 /***
7266 * DESCRIPTION:
7267 * Retrieves the state of a listview control item.
7269 * PARAMETER(S):
7270 * [I] infoPtr : valid pointer to the listview structure
7271 * [I] nItem : item index
7272 * [I] uMask : state mask
7274 * RETURN:
7275 * State specified by the mask.
7277 static UINT LISTVIEW_GetItemState(const LISTVIEW_INFO *infoPtr, INT nItem, UINT uMask)
7279 LVITEMW lvItem;
7281 if (nItem < 0 || nItem >= infoPtr->nItemCount) return 0;
7283 lvItem.iItem = nItem;
7284 lvItem.iSubItem = 0;
7285 lvItem.mask = LVIF_STATE;
7286 lvItem.stateMask = uMask;
7287 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) return 0;
7289 return lvItem.state & uMask;
7292 /***
7293 * DESCRIPTION:
7294 * Retrieves the text of a listview control item or subitem.
7296 * PARAMETER(S):
7297 * [I] hwnd : window handle
7298 * [I] nItem : item index
7299 * [IO] lpLVItem : item information
7300 * [I] isW : TRUE if lpLVItem is Unicode
7302 * RETURN:
7303 * SUCCESS : string length
7304 * FAILURE : 0
7306 static INT LISTVIEW_GetItemTextT(const LISTVIEW_INFO *infoPtr, INT nItem, LPLVITEMW lpLVItem, BOOL isW)
7308 if (!lpLVItem || nItem < 0 || nItem >= infoPtr->nItemCount) return 0;
7310 lpLVItem->mask = LVIF_TEXT;
7311 lpLVItem->iItem = nItem;
7312 if (!LISTVIEW_GetItemExtT(infoPtr, lpLVItem, isW)) return 0;
7314 return textlenT(lpLVItem->pszText, isW);
7317 /***
7318 * DESCRIPTION:
7319 * Searches for an item based on properties + relationships.
7321 * PARAMETER(S):
7322 * [I] infoPtr : valid pointer to the listview structure
7323 * [I] nItem : item index
7324 * [I] uFlags : relationship flag
7326 * RETURN:
7327 * SUCCESS : item index
7328 * FAILURE : -1
7330 static INT LISTVIEW_GetNextItem(const LISTVIEW_INFO *infoPtr, INT nItem, UINT uFlags)
7332 UINT uMask = 0;
7333 LVFINDINFOW lvFindInfo;
7334 INT nCountPerColumn;
7335 INT nCountPerRow;
7336 INT i;
7338 TRACE("nItem=%d, uFlags=%x, nItemCount=%d\n", nItem, uFlags, infoPtr->nItemCount);
7339 if (nItem < -1 || nItem >= infoPtr->nItemCount) return -1;
7341 ZeroMemory(&lvFindInfo, sizeof(lvFindInfo));
7343 if (uFlags & LVNI_CUT)
7344 uMask |= LVIS_CUT;
7346 if (uFlags & LVNI_DROPHILITED)
7347 uMask |= LVIS_DROPHILITED;
7349 if (uFlags & LVNI_FOCUSED)
7350 uMask |= LVIS_FOCUSED;
7352 if (uFlags & LVNI_SELECTED)
7353 uMask |= LVIS_SELECTED;
7355 /* if we're asked for the focused item, that's only one,
7356 * so it's worth optimizing */
7357 if (uFlags & LVNI_FOCUSED)
7359 if ((LISTVIEW_GetItemState(infoPtr, infoPtr->nFocusedItem, uMask) & uMask) != uMask) return -1;
7360 return (infoPtr->nFocusedItem == nItem) ? -1 : infoPtr->nFocusedItem;
7363 if (uFlags & LVNI_ABOVE)
7365 if ((infoPtr->uView == LV_VIEW_LIST) || (infoPtr->uView == LV_VIEW_DETAILS))
7367 while (nItem >= 0)
7369 nItem--;
7370 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7371 return nItem;
7374 else
7376 /* Special case for autoarrange - move 'til the top of a list */
7377 if (is_autoarrange(infoPtr))
7379 nCountPerRow = LISTVIEW_GetCountPerRow(infoPtr);
7380 while (nItem - nCountPerRow >= 0)
7382 nItem -= nCountPerRow;
7383 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7384 return nItem;
7386 return -1;
7388 lvFindInfo.flags = LVFI_NEARESTXY;
7389 lvFindInfo.vkDirection = VK_UP;
7390 LISTVIEW_GetItemPosition(infoPtr, nItem, &lvFindInfo.pt);
7391 while ((nItem = LISTVIEW_FindItemW(infoPtr, nItem, &lvFindInfo)) != -1)
7393 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7394 return nItem;
7398 else if (uFlags & LVNI_BELOW)
7400 if ((infoPtr->uView == LV_VIEW_LIST) || (infoPtr->uView == LV_VIEW_DETAILS))
7402 while (nItem < infoPtr->nItemCount)
7404 nItem++;
7405 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7406 return nItem;
7409 else
7411 /* Special case for autoarrange - move 'til the bottom of a list */
7412 if (is_autoarrange(infoPtr))
7414 nCountPerRow = LISTVIEW_GetCountPerRow(infoPtr);
7415 while (nItem + nCountPerRow < infoPtr->nItemCount )
7417 nItem += nCountPerRow;
7418 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7419 return nItem;
7421 return -1;
7423 lvFindInfo.flags = LVFI_NEARESTXY;
7424 lvFindInfo.vkDirection = VK_DOWN;
7425 LISTVIEW_GetItemPosition(infoPtr, nItem, &lvFindInfo.pt);
7426 while ((nItem = LISTVIEW_FindItemW(infoPtr, nItem, &lvFindInfo)) != -1)
7428 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7429 return nItem;
7433 else if (uFlags & LVNI_TOLEFT)
7435 if (infoPtr->uView == LV_VIEW_LIST)
7437 nCountPerColumn = LISTVIEW_GetCountPerColumn(infoPtr);
7438 while (nItem - nCountPerColumn >= 0)
7440 nItem -= nCountPerColumn;
7441 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7442 return nItem;
7445 else if ((infoPtr->uView == LV_VIEW_SMALLICON) || (infoPtr->uView == LV_VIEW_ICON))
7447 /* Special case for autoarrange - move 'til the beginning of a row */
7448 if (is_autoarrange(infoPtr))
7450 nCountPerRow = LISTVIEW_GetCountPerRow(infoPtr);
7451 while (nItem % nCountPerRow > 0)
7453 nItem --;
7454 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7455 return nItem;
7457 return -1;
7459 lvFindInfo.flags = LVFI_NEARESTXY;
7460 lvFindInfo.vkDirection = VK_LEFT;
7461 LISTVIEW_GetItemPosition(infoPtr, nItem, &lvFindInfo.pt);
7462 while ((nItem = LISTVIEW_FindItemW(infoPtr, nItem, &lvFindInfo)) != -1)
7464 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7465 return nItem;
7469 else if (uFlags & LVNI_TORIGHT)
7471 if (infoPtr->uView == LV_VIEW_LIST)
7473 nCountPerColumn = LISTVIEW_GetCountPerColumn(infoPtr);
7474 while (nItem + nCountPerColumn < infoPtr->nItemCount)
7476 nItem += nCountPerColumn;
7477 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7478 return nItem;
7481 else if ((infoPtr->uView == LV_VIEW_SMALLICON) || (infoPtr->uView == LV_VIEW_ICON))
7483 /* Special case for autoarrange - move 'til the end of a row */
7484 if (is_autoarrange(infoPtr))
7486 nCountPerRow = LISTVIEW_GetCountPerRow(infoPtr);
7487 while (nItem % nCountPerRow < nCountPerRow - 1 )
7489 nItem ++;
7490 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7491 return nItem;
7493 return -1;
7495 lvFindInfo.flags = LVFI_NEARESTXY;
7496 lvFindInfo.vkDirection = VK_RIGHT;
7497 LISTVIEW_GetItemPosition(infoPtr, nItem, &lvFindInfo.pt);
7498 while ((nItem = LISTVIEW_FindItemW(infoPtr, nItem, &lvFindInfo)) != -1)
7500 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7501 return nItem;
7505 else
7507 nItem++;
7509 /* search by index */
7510 for (i = nItem; i < infoPtr->nItemCount; i++)
7512 if ((LISTVIEW_GetItemState(infoPtr, i, uMask) & uMask) == uMask)
7513 return i;
7517 return -1;
7520 /* LISTVIEW_GetNumberOfWorkAreas */
7522 /***
7523 * DESCRIPTION:
7524 * Retrieves the origin coordinates when in icon or small icon display mode.
7526 * PARAMETER(S):
7527 * [I] infoPtr : valid pointer to the listview structure
7528 * [O] lpptOrigin : coordinate information
7530 * RETURN:
7531 * None.
7533 static void LISTVIEW_GetOrigin(const LISTVIEW_INFO *infoPtr, LPPOINT lpptOrigin)
7535 INT nHorzPos = 0, nVertPos = 0;
7536 SCROLLINFO scrollInfo;
7538 scrollInfo.cbSize = sizeof(SCROLLINFO);
7539 scrollInfo.fMask = SIF_POS;
7541 if (GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo))
7542 nHorzPos = scrollInfo.nPos;
7543 if (GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo))
7544 nVertPos = scrollInfo.nPos;
7546 TRACE("nHorzPos=%d, nVertPos=%d\n", nHorzPos, nVertPos);
7548 lpptOrigin->x = infoPtr->rcList.left;
7549 lpptOrigin->y = infoPtr->rcList.top;
7550 if (infoPtr->uView == LV_VIEW_LIST)
7551 nHorzPos *= infoPtr->nItemWidth;
7552 else if (infoPtr->uView == LV_VIEW_DETAILS)
7553 nVertPos *= infoPtr->nItemHeight;
7555 lpptOrigin->x -= nHorzPos;
7556 lpptOrigin->y -= nVertPos;
7558 TRACE(" origin=%s\n", wine_dbgstr_point(lpptOrigin));
7561 /***
7562 * DESCRIPTION:
7563 * Retrieves the width of a string.
7565 * PARAMETER(S):
7566 * [I] hwnd : window handle
7567 * [I] lpszText : text string to process
7568 * [I] isW : TRUE if lpszText is Unicode, FALSE otherwise
7570 * RETURN:
7571 * SUCCESS : string width (in pixels)
7572 * FAILURE : zero
7574 static INT LISTVIEW_GetStringWidthT(const LISTVIEW_INFO *infoPtr, LPCWSTR lpszText, BOOL isW)
7576 SIZE stringSize;
7578 stringSize.cx = 0;
7579 if (is_text(lpszText))
7581 HFONT hFont = infoPtr->hFont ? infoPtr->hFont : infoPtr->hDefaultFont;
7582 HDC hdc = GetDC(infoPtr->hwndSelf);
7583 HFONT hOldFont = SelectObject(hdc, hFont);
7585 if (isW)
7586 GetTextExtentPointW(hdc, lpszText, lstrlenW(lpszText), &stringSize);
7587 else
7588 GetTextExtentPointA(hdc, (LPCSTR)lpszText, lstrlenA((LPCSTR)lpszText), &stringSize);
7589 SelectObject(hdc, hOldFont);
7590 ReleaseDC(infoPtr->hwndSelf, hdc);
7592 return stringSize.cx;
7595 /***
7596 * DESCRIPTION:
7597 * Determines which listview item is located at the specified position.
7599 * PARAMETER(S):
7600 * [I] infoPtr : valid pointer to the listview structure
7601 * [IO] lpht : hit test information
7602 * [I] subitem : fill out iSubItem.
7603 * [I] select : return the index only if the hit selects the item
7605 * NOTE:
7606 * (mm 20001022): We must not allow iSubItem to be touched, for
7607 * an app might pass only a structure with space up to iItem!
7608 * (MS Office 97 does that for instance in the file open dialog)
7610 * RETURN:
7611 * SUCCESS : item index
7612 * FAILURE : -1
7614 static INT LISTVIEW_HitTest(const LISTVIEW_INFO *infoPtr, LPLVHITTESTINFO lpht, BOOL subitem, BOOL select)
7616 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
7617 RECT rcBox, rcBounds, rcState, rcIcon, rcLabel, rcSearch;
7618 POINT Origin, Position, opt;
7619 BOOL is_fullrow;
7620 LVITEMW lvItem;
7621 ITERATOR i;
7622 INT iItem;
7624 TRACE("(pt=%s, subitem=%d, select=%d)\n", wine_dbgstr_point(&lpht->pt), subitem, select);
7626 lpht->flags = 0;
7627 lpht->iItem = -1;
7628 if (subitem) lpht->iSubItem = 0;
7630 LISTVIEW_GetOrigin(infoPtr, &Origin);
7632 /* set whole list relation flags */
7633 if (subitem && infoPtr->uView == LV_VIEW_DETAILS)
7635 /* LVM_SUBITEMHITTEST checks left bound of possible client area */
7636 if (infoPtr->rcList.left > lpht->pt.x && Origin.x < lpht->pt.x)
7637 lpht->flags |= LVHT_TOLEFT;
7639 if (lpht->pt.y < infoPtr->rcList.top && lpht->pt.y >= 0)
7640 opt.y = lpht->pt.y + infoPtr->rcList.top;
7641 else
7642 opt.y = lpht->pt.y;
7644 if (infoPtr->rcList.bottom < opt.y)
7645 lpht->flags |= LVHT_BELOW;
7647 else
7649 if (infoPtr->rcList.left > lpht->pt.x)
7650 lpht->flags |= LVHT_TOLEFT;
7651 else if (infoPtr->rcList.right < lpht->pt.x)
7652 lpht->flags |= LVHT_TORIGHT;
7654 if (infoPtr->rcList.top > lpht->pt.y)
7655 lpht->flags |= LVHT_ABOVE;
7656 else if (infoPtr->rcList.bottom < lpht->pt.y)
7657 lpht->flags |= LVHT_BELOW;
7660 /* even if item is invalid try to find subitem */
7661 if (infoPtr->uView == LV_VIEW_DETAILS && subitem)
7663 RECT *pRect;
7664 INT j;
7666 opt.x = lpht->pt.x - Origin.x;
7668 lpht->iSubItem = -1;
7669 for (j = 0; j < DPA_GetPtrCount(infoPtr->hdpaColumns); j++)
7671 pRect = &LISTVIEW_GetColumnInfo(infoPtr, j)->rcHeader;
7673 if ((opt.x >= pRect->left) && (opt.x < pRect->right))
7675 lpht->iSubItem = j;
7676 break;
7679 TRACE("lpht->iSubItem=%d\n", lpht->iSubItem);
7681 /* if we're outside horizontal columns bounds there's nothing to test further */
7682 if (lpht->iSubItem == -1)
7684 lpht->iItem = -1;
7685 lpht->flags = LVHT_NOWHERE;
7686 return -1;
7690 TRACE("lpht->flags=0x%x\n", lpht->flags);
7691 if (lpht->flags) return -1;
7693 lpht->flags |= LVHT_NOWHERE;
7695 /* first deal with the large items */
7696 rcSearch.left = lpht->pt.x;
7697 rcSearch.top = lpht->pt.y;
7698 rcSearch.right = rcSearch.left + 1;
7699 rcSearch.bottom = rcSearch.top + 1;
7701 iterator_frameditems(&i, infoPtr, &rcSearch);
7702 iterator_next(&i); /* go to first item in the sequence */
7703 iItem = i.nItem;
7704 iterator_destroy(&i);
7706 TRACE("lpht->iItem=%d\n", iItem);
7707 if (iItem == -1) return -1;
7709 lvItem.mask = LVIF_STATE | LVIF_TEXT;
7710 if (infoPtr->uView == LV_VIEW_DETAILS) lvItem.mask |= LVIF_INDENT;
7711 lvItem.stateMask = LVIS_STATEIMAGEMASK;
7712 if (infoPtr->uView == LV_VIEW_ICON) lvItem.stateMask |= LVIS_FOCUSED;
7713 lvItem.iItem = iItem;
7714 lvItem.iSubItem = subitem ? lpht->iSubItem : 0;
7715 lvItem.pszText = szDispText;
7716 lvItem.cchTextMax = DISP_TEXT_SIZE;
7717 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) return -1;
7718 if (!infoPtr->bFocus) lvItem.state &= ~LVIS_FOCUSED;
7720 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, &rcBox, NULL, &rcIcon, &rcState, &rcLabel);
7721 LISTVIEW_GetItemOrigin(infoPtr, iItem, &Position);
7722 opt.x = lpht->pt.x - Position.x - Origin.x;
7724 if (lpht->pt.y < infoPtr->rcList.top && lpht->pt.y >= 0)
7725 opt.y = lpht->pt.y - Position.y - Origin.y + infoPtr->rcList.top;
7726 else
7727 opt.y = lpht->pt.y - Position.y - Origin.y;
7729 if (infoPtr->uView == LV_VIEW_DETAILS)
7731 rcBounds = rcBox;
7732 if (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT)
7733 opt.x = lpht->pt.x - Origin.x;
7735 else
7737 UnionRect(&rcBounds, &rcIcon, &rcLabel);
7738 UnionRect(&rcBounds, &rcBounds, &rcState);
7740 TRACE("rcBounds=%s\n", wine_dbgstr_rect(&rcBounds));
7741 if (!PtInRect(&rcBounds, opt)) return -1;
7743 /* That's a special case - row rectangle is used as item rectangle and
7744 returned flags contain all item parts. */
7745 is_fullrow = (infoPtr->uView == LV_VIEW_DETAILS) && ((infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT) || (infoPtr->dwStyle & LVS_OWNERDRAWFIXED));
7747 if (PtInRect(&rcIcon, opt))
7748 lpht->flags |= LVHT_ONITEMICON;
7749 else if (PtInRect(&rcLabel, opt))
7750 lpht->flags |= LVHT_ONITEMLABEL;
7751 else if (infoPtr->himlState && PtInRect(&rcState, opt))
7752 lpht->flags |= LVHT_ONITEMSTATEICON;
7753 if (is_fullrow && !(lpht->flags & LVHT_ONITEM))
7755 lpht->flags = LVHT_ONITEM | LVHT_ABOVE;
7757 if (lpht->flags & LVHT_ONITEM)
7758 lpht->flags &= ~LVHT_NOWHERE;
7759 TRACE("lpht->flags=0x%x\n", lpht->flags);
7761 if (select && !is_fullrow)
7763 if (infoPtr->uView == LV_VIEW_DETAILS)
7765 /* get main item bounds */
7766 lvItem.iSubItem = 0;
7767 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, &rcBox, NULL, &rcIcon, &rcState, &rcLabel);
7768 UnionRect(&rcBounds, &rcIcon, &rcLabel);
7769 UnionRect(&rcBounds, &rcBounds, &rcState);
7771 if (!PtInRect(&rcBounds, opt)) iItem = -1;
7773 return lpht->iItem = iItem;
7776 /***
7777 * DESCRIPTION:
7778 * Inserts a new item in the listview control.
7780 * PARAMETER(S):
7781 * [I] infoPtr : valid pointer to the listview structure
7782 * [I] lpLVItem : item information
7783 * [I] isW : TRUE if lpLVItem is Unicode, FALSE if it's ANSI
7785 * RETURN:
7786 * SUCCESS : new item index
7787 * FAILURE : -1
7789 static INT LISTVIEW_InsertItemT(LISTVIEW_INFO *infoPtr, const LVITEMW *lpLVItem, BOOL isW)
7791 INT nItem;
7792 HDPA hdpaSubItems;
7793 NMLISTVIEW nmlv;
7794 ITEM_INFO *lpItem;
7795 ITEM_ID *lpID;
7796 BOOL is_sorted, has_changed;
7797 LVITEMW item;
7798 HWND hwndSelf = infoPtr->hwndSelf;
7800 TRACE("(item=%s, isW=%d)\n", debuglvitem_t(lpLVItem, isW), isW);
7802 if (infoPtr->dwStyle & LVS_OWNERDATA) return infoPtr->nItemCount++;
7804 /* make sure it's an item, and not a subitem; cannot insert a subitem */
7805 if (!lpLVItem || lpLVItem->iSubItem) return -1;
7807 if (!is_assignable_item(lpLVItem, infoPtr->dwStyle)) return -1;
7809 if (!(lpItem = Alloc(sizeof(ITEM_INFO)))) return -1;
7811 /* insert item in listview control data structure */
7812 if ( !(hdpaSubItems = DPA_Create(8)) ) goto fail;
7813 if ( !DPA_SetPtr(hdpaSubItems, 0, lpItem) ) assert (FALSE);
7815 /* link with id struct */
7816 if (!(lpID = Alloc(sizeof(ITEM_ID)))) goto fail;
7817 lpItem->id = lpID;
7818 lpID->item = hdpaSubItems;
7819 lpID->id = get_next_itemid(infoPtr);
7820 if ( DPA_InsertPtr(infoPtr->hdpaItemIds, infoPtr->nItemCount, lpID) == -1) goto fail;
7822 is_sorted = (infoPtr->dwStyle & (LVS_SORTASCENDING | LVS_SORTDESCENDING)) &&
7823 !(infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && (LPSTR_TEXTCALLBACKW != lpLVItem->pszText);
7825 if (lpLVItem->iItem < 0 && !is_sorted) return -1;
7827 /* calculate new item index */
7828 if (is_sorted)
7830 HDPA hItem;
7831 ITEM_INFO *item_s;
7832 INT i = 0, cmpv;
7833 WCHAR *textW;
7835 textW = textdupTtoW(lpLVItem->pszText, isW);
7837 while (i < infoPtr->nItemCount)
7839 hItem = DPA_GetPtr( infoPtr->hdpaItems, i);
7840 item_s = DPA_GetPtr(hItem, 0);
7842 cmpv = textcmpWT(item_s->hdr.pszText, textW, TRUE);
7843 if (infoPtr->dwStyle & LVS_SORTDESCENDING) cmpv *= -1;
7845 if (cmpv >= 0) break;
7846 i++;
7849 textfreeT(textW, isW);
7851 nItem = i;
7853 else
7854 nItem = min(lpLVItem->iItem, infoPtr->nItemCount);
7856 TRACE("inserting at %d, sorted=%d, count=%d, iItem=%d\n", nItem, is_sorted, infoPtr->nItemCount, lpLVItem->iItem);
7857 nItem = DPA_InsertPtr( infoPtr->hdpaItems, nItem, hdpaSubItems );
7858 if (nItem == -1) goto fail;
7859 infoPtr->nItemCount++;
7861 /* shift indices first so they don't get tangled */
7862 LISTVIEW_ShiftIndices(infoPtr, nItem, 1);
7864 /* set the item attributes */
7865 if (lpLVItem->mask & (LVIF_GROUPID|LVIF_COLUMNS))
7867 /* full size structure expected - _WIN32IE >= 0x560 */
7868 item = *lpLVItem;
7870 else if (lpLVItem->mask & LVIF_INDENT)
7872 /* indent member expected - _WIN32IE >= 0x300 */
7873 memcpy(&item, lpLVItem, offsetof( LVITEMW, iGroupId ));
7875 else
7877 /* minimal structure expected */
7878 memcpy(&item, lpLVItem, offsetof( LVITEMW, iIndent ));
7880 item.iItem = nItem;
7881 if (infoPtr->dwLvExStyle & LVS_EX_CHECKBOXES)
7883 if (item.mask & LVIF_STATE)
7885 item.stateMask |= LVIS_STATEIMAGEMASK;
7886 item.state &= ~LVIS_STATEIMAGEMASK;
7887 item.state |= INDEXTOSTATEIMAGEMASK(1);
7889 else
7891 item.mask |= LVIF_STATE;
7892 item.stateMask = LVIS_STATEIMAGEMASK;
7893 item.state = INDEXTOSTATEIMAGEMASK(1);
7897 if (!set_main_item(infoPtr, &item, TRUE, isW, &has_changed)) goto undo;
7899 /* make room for the position, if we are in the right mode */
7900 if ((infoPtr->uView == LV_VIEW_SMALLICON) || (infoPtr->uView == LV_VIEW_ICON))
7902 if (DPA_InsertPtr(infoPtr->hdpaPosX, nItem, 0) == -1)
7903 goto undo;
7904 if (DPA_InsertPtr(infoPtr->hdpaPosY, nItem, 0) == -1)
7906 DPA_DeletePtr(infoPtr->hdpaPosX, nItem);
7907 goto undo;
7911 /* send LVN_INSERTITEM notification */
7912 memset(&nmlv, 0, sizeof(NMLISTVIEW));
7913 nmlv.iItem = nItem;
7914 nmlv.lParam = lpItem->lParam;
7915 notify_listview(infoPtr, LVN_INSERTITEM, &nmlv);
7916 if (!IsWindow(hwndSelf))
7917 return -1;
7919 /* align items (set position of each item) */
7920 if (infoPtr->uView == LV_VIEW_SMALLICON || infoPtr->uView == LV_VIEW_ICON)
7922 POINT pt;
7924 if (infoPtr->dwStyle & LVS_ALIGNLEFT)
7925 LISTVIEW_NextIconPosLeft(infoPtr, &pt);
7926 else
7927 LISTVIEW_NextIconPosTop(infoPtr, &pt);
7929 LISTVIEW_MoveIconTo(infoPtr, nItem, &pt, TRUE);
7932 /* now is the invalidation fun */
7933 LISTVIEW_ScrollOnInsert(infoPtr, nItem, 1);
7934 return nItem;
7936 undo:
7937 LISTVIEW_ShiftIndices(infoPtr, nItem, -1);
7938 LISTVIEW_ShiftFocus(infoPtr, infoPtr->nFocusedItem, nItem, -1);
7939 DPA_DeletePtr(infoPtr->hdpaItems, nItem);
7940 infoPtr->nItemCount--;
7941 fail:
7942 DPA_DeletePtr(hdpaSubItems, 0);
7943 DPA_Destroy (hdpaSubItems);
7944 Free (lpItem);
7945 return -1;
7948 /***
7949 * DESCRIPTION:
7950 * Checks item visibility.
7952 * PARAMETER(S):
7953 * [I] infoPtr : valid pointer to the listview structure
7954 * [I] nFirst : item index to check for
7956 * RETURN:
7957 * Item visible : TRUE
7958 * Item invisible or failure : FALSE
7960 static BOOL LISTVIEW_IsItemVisible(const LISTVIEW_INFO *infoPtr, INT nItem)
7962 POINT Origin, Position;
7963 RECT rcItem;
7964 HDC hdc;
7965 BOOL ret;
7967 TRACE("nItem=%d\n", nItem);
7969 if (nItem < 0 || nItem >= DPA_GetPtrCount(infoPtr->hdpaItems)) return FALSE;
7971 LISTVIEW_GetOrigin(infoPtr, &Origin);
7972 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position);
7973 rcItem.left = Position.x + Origin.x;
7974 rcItem.top = Position.y + Origin.y;
7975 rcItem.right = rcItem.left + infoPtr->nItemWidth;
7976 rcItem.bottom = rcItem.top + infoPtr->nItemHeight;
7978 hdc = GetDC(infoPtr->hwndSelf);
7979 if (!hdc) return FALSE;
7980 ret = RectVisible(hdc, &rcItem);
7981 ReleaseDC(infoPtr->hwndSelf, hdc);
7983 return ret;
7986 /***
7987 * DESCRIPTION:
7988 * Redraws a range of items.
7990 * PARAMETER(S):
7991 * [I] infoPtr : valid pointer to the listview structure
7992 * [I] nFirst : first item
7993 * [I] nLast : last item
7995 * RETURN:
7996 * SUCCESS : TRUE
7997 * FAILURE : FALSE
7999 static BOOL LISTVIEW_RedrawItems(const LISTVIEW_INFO *infoPtr, INT nFirst, INT nLast)
8001 INT i;
8003 for (i = max(nFirst, 0); i <= min(nLast, infoPtr->nItemCount - 1); i++)
8004 LISTVIEW_InvalidateItem(infoPtr, i);
8006 return TRUE;
8009 /***
8010 * DESCRIPTION:
8011 * Scroll the content of a listview.
8013 * PARAMETER(S):
8014 * [I] infoPtr : valid pointer to the listview structure
8015 * [I] dx : horizontal scroll amount in pixels
8016 * [I] dy : vertical scroll amount in pixels
8018 * RETURN:
8019 * SUCCESS : TRUE
8020 * FAILURE : FALSE
8022 * COMMENTS:
8023 * If the control is in report view (LV_VIEW_DETAILS) the control can
8024 * be scrolled only in line increments. "dy" will be rounded to the
8025 * nearest number of pixels that are a whole line. Ex: if line height
8026 * is 16 and an 8 is passed, the list will be scrolled by 16. If a 7
8027 * is passed, then the scroll will be 0. (per MSDN 7/2002)
8029 static BOOL LISTVIEW_Scroll(LISTVIEW_INFO *infoPtr, INT dx, INT dy)
8031 switch(infoPtr->uView) {
8032 case LV_VIEW_DETAILS:
8033 dy += (dy < 0 ? -1 : 1) * infoPtr->nItemHeight/2;
8034 dy /= infoPtr->nItemHeight;
8035 break;
8036 case LV_VIEW_LIST:
8037 if (dy != 0) return FALSE;
8038 break;
8039 default: /* icon */
8040 break;
8043 if (dx != 0) LISTVIEW_HScroll(infoPtr, SB_INTERNAL, dx);
8044 if (dy != 0) LISTVIEW_VScroll(infoPtr, SB_INTERNAL, dy);
8046 return TRUE;
8049 /***
8050 * DESCRIPTION:
8051 * Sets the background color.
8053 * PARAMETER(S):
8054 * [I] infoPtr : valid pointer to the listview structure
8055 * [I] color : background color
8057 * RETURN:
8058 * SUCCESS : TRUE
8059 * FAILURE : FALSE
8061 static BOOL LISTVIEW_SetBkColor(LISTVIEW_INFO *infoPtr, COLORREF color)
8063 TRACE("(color=%x)\n", color);
8065 if(infoPtr->clrBk != color) {
8066 if (infoPtr->clrBk != CLR_NONE) DeleteObject(infoPtr->hBkBrush);
8067 infoPtr->clrBk = color;
8068 if (color == CLR_NONE)
8069 infoPtr->hBkBrush = (HBRUSH)GetClassLongPtrW(infoPtr->hwndSelf, GCLP_HBRBACKGROUND);
8070 else
8072 infoPtr->hBkBrush = CreateSolidBrush(color);
8073 infoPtr->dwLvExStyle &= ~LVS_EX_TRANSPARENTBKGND;
8077 return TRUE;
8080 /* LISTVIEW_SetBkImage */
8082 /*** Helper for {Insert,Set}ColumnT *only* */
8083 static void column_fill_hditem(const LISTVIEW_INFO *infoPtr, HDITEMW *lphdi, INT nColumn,
8084 const LVCOLUMNW *lpColumn, BOOL isW)
8086 if (lpColumn->mask & LVCF_FMT)
8088 /* format member is valid */
8089 lphdi->mask |= HDI_FORMAT;
8091 /* set text alignment (leftmost column must be left-aligned) */
8092 if (nColumn == 0 || (lpColumn->fmt & LVCFMT_JUSTIFYMASK) == LVCFMT_LEFT)
8093 lphdi->fmt |= HDF_LEFT;
8094 else if ((lpColumn->fmt & LVCFMT_JUSTIFYMASK) == LVCFMT_RIGHT)
8095 lphdi->fmt |= HDF_RIGHT;
8096 else if ((lpColumn->fmt & LVCFMT_JUSTIFYMASK) == LVCFMT_CENTER)
8097 lphdi->fmt |= HDF_CENTER;
8099 if (lpColumn->fmt & LVCFMT_BITMAP_ON_RIGHT)
8100 lphdi->fmt |= HDF_BITMAP_ON_RIGHT;
8102 if (lpColumn->fmt & LVCFMT_COL_HAS_IMAGES)
8104 lphdi->fmt |= HDF_IMAGE;
8105 lphdi->iImage = I_IMAGECALLBACK;
8108 if (lpColumn->fmt & LVCFMT_FIXED_WIDTH)
8109 lphdi->fmt |= HDF_FIXEDWIDTH;
8112 if (lpColumn->mask & LVCF_WIDTH)
8114 lphdi->mask |= HDI_WIDTH;
8115 if(lpColumn->cx == LVSCW_AUTOSIZE_USEHEADER)
8117 /* make it fill the remainder of the controls width */
8118 RECT rcHeader;
8119 INT item_index;
8121 for(item_index = 0; item_index < (nColumn - 1); item_index++)
8123 LISTVIEW_GetHeaderRect(infoPtr, item_index, &rcHeader);
8124 lphdi->cxy += rcHeader.right - rcHeader.left;
8127 /* retrieve the layout of the header */
8128 GetClientRect(infoPtr->hwndSelf, &rcHeader);
8129 TRACE("start cxy=%d rcHeader=%s\n", lphdi->cxy, wine_dbgstr_rect(&rcHeader));
8131 lphdi->cxy = (rcHeader.right - rcHeader.left) - lphdi->cxy;
8133 else
8134 lphdi->cxy = lpColumn->cx;
8137 if (lpColumn->mask & LVCF_TEXT)
8139 lphdi->mask |= HDI_TEXT | HDI_FORMAT;
8140 lphdi->fmt |= HDF_STRING;
8141 lphdi->pszText = lpColumn->pszText;
8142 lphdi->cchTextMax = textlenT(lpColumn->pszText, isW);
8145 if (lpColumn->mask & LVCF_IMAGE)
8147 lphdi->mask |= HDI_IMAGE;
8148 lphdi->iImage = lpColumn->iImage;
8151 if (lpColumn->mask & LVCF_ORDER)
8153 lphdi->mask |= HDI_ORDER;
8154 lphdi->iOrder = lpColumn->iOrder;
8159 /***
8160 * DESCRIPTION:
8161 * Inserts a new column.
8163 * PARAMETER(S):
8164 * [I] infoPtr : valid pointer to the listview structure
8165 * [I] nColumn : column index
8166 * [I] lpColumn : column information
8167 * [I] isW : TRUE if lpColumn is Unicode, FALSE otherwise
8169 * RETURN:
8170 * SUCCESS : new column index
8171 * FAILURE : -1
8173 static INT LISTVIEW_InsertColumnT(LISTVIEW_INFO *infoPtr, INT nColumn,
8174 const LVCOLUMNW *lpColumn, BOOL isW)
8176 COLUMN_INFO *lpColumnInfo;
8177 INT nNewColumn;
8178 HDITEMW hdi;
8180 TRACE("(nColumn=%d, lpColumn=%s, isW=%d)\n", nColumn, debuglvcolumn_t(lpColumn, isW), isW);
8182 if (!lpColumn || nColumn < 0) return -1;
8183 nColumn = min(nColumn, DPA_GetPtrCount(infoPtr->hdpaColumns));
8185 ZeroMemory(&hdi, sizeof(HDITEMW));
8186 column_fill_hditem(infoPtr, &hdi, nColumn, lpColumn, isW);
8189 * A mask not including LVCF_WIDTH turns into a mask of width, width 10
8190 * (can be seen in SPY) otherwise column never gets added.
8192 if (!(lpColumn->mask & LVCF_WIDTH)) {
8193 hdi.mask |= HDI_WIDTH;
8194 hdi.cxy = 10;
8198 * when the iSubItem is available Windows copies it to the header lParam. It seems
8199 * to happen only in LVM_INSERTCOLUMN - not in LVM_SETCOLUMN
8201 if (lpColumn->mask & LVCF_SUBITEM)
8203 hdi.mask |= HDI_LPARAM;
8204 hdi.lParam = lpColumn->iSubItem;
8207 /* create header if not present */
8208 LISTVIEW_CreateHeader(infoPtr);
8209 if (!(LVS_NOCOLUMNHEADER & infoPtr->dwStyle) &&
8210 (infoPtr->uView == LV_VIEW_DETAILS) && (WS_VISIBLE & infoPtr->dwStyle))
8212 ShowWindow(infoPtr->hwndHeader, SW_SHOWNORMAL);
8215 /* insert item in header control */
8216 nNewColumn = SendMessageW(infoPtr->hwndHeader,
8217 isW ? HDM_INSERTITEMW : HDM_INSERTITEMA,
8218 nColumn, (LPARAM)&hdi);
8219 if (nNewColumn == -1) return -1;
8220 if (nNewColumn != nColumn) ERR("nColumn=%d, nNewColumn=%d\n", nColumn, nNewColumn);
8222 /* create our own column info */
8223 if (!(lpColumnInfo = Alloc(sizeof(COLUMN_INFO)))) goto fail;
8224 if (DPA_InsertPtr(infoPtr->hdpaColumns, nNewColumn, lpColumnInfo) == -1) goto fail;
8226 if (lpColumn->mask & LVCF_FMT) lpColumnInfo->fmt = lpColumn->fmt;
8227 if (lpColumn->mask & LVCF_MINWIDTH) lpColumnInfo->cxMin = lpColumn->cxMin;
8228 if (!SendMessageW(infoPtr->hwndHeader, HDM_GETITEMRECT, nNewColumn, (LPARAM)&lpColumnInfo->rcHeader))
8229 goto fail;
8231 /* now we have to actually adjust the data */
8232 if (!(infoPtr->dwStyle & LVS_OWNERDATA) && infoPtr->nItemCount > 0)
8234 SUBITEM_INFO *lpSubItem;
8235 HDPA hdpaSubItems;
8236 INT nItem, i;
8237 LVITEMW item;
8238 BOOL changed;
8240 item.iSubItem = nNewColumn;
8241 item.mask = LVIF_TEXT | LVIF_IMAGE;
8242 item.iImage = I_IMAGECALLBACK;
8243 item.pszText = LPSTR_TEXTCALLBACKW;
8245 for (nItem = 0; nItem < infoPtr->nItemCount; nItem++)
8247 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, nItem);
8248 for (i = 1; i < DPA_GetPtrCount(hdpaSubItems); i++)
8250 lpSubItem = DPA_GetPtr(hdpaSubItems, i);
8251 if (lpSubItem->iSubItem >= nNewColumn)
8252 lpSubItem->iSubItem++;
8255 /* add new subitem for each item */
8256 item.iItem = nItem;
8257 set_sub_item(infoPtr, &item, isW, &changed);
8261 /* make space for the new column */
8262 LISTVIEW_ScrollColumns(infoPtr, nNewColumn + 1, lpColumnInfo->rcHeader.right - lpColumnInfo->rcHeader.left);
8263 LISTVIEW_UpdateItemSize(infoPtr);
8265 return nNewColumn;
8267 fail:
8268 if (nNewColumn != -1) SendMessageW(infoPtr->hwndHeader, HDM_DELETEITEM, nNewColumn, 0);
8269 if (lpColumnInfo)
8271 DPA_DeletePtr(infoPtr->hdpaColumns, nNewColumn);
8272 Free(lpColumnInfo);
8274 return -1;
8277 /***
8278 * DESCRIPTION:
8279 * Sets the attributes of a header item.
8281 * PARAMETER(S):
8282 * [I] infoPtr : valid pointer to the listview structure
8283 * [I] nColumn : column index
8284 * [I] lpColumn : column attributes
8285 * [I] isW: if TRUE, then lpColumn is a LPLVCOLUMNW, else it is a LPLVCOLUMNA
8287 * RETURN:
8288 * SUCCESS : TRUE
8289 * FAILURE : FALSE
8291 static BOOL LISTVIEW_SetColumnT(const LISTVIEW_INFO *infoPtr, INT nColumn,
8292 const LVCOLUMNW *lpColumn, BOOL isW)
8294 HDITEMW hdi, hdiget;
8295 BOOL bResult;
8297 TRACE("(nColumn=%d, lpColumn=%s, isW=%d)\n", nColumn, debuglvcolumn_t(lpColumn, isW), isW);
8299 if (!lpColumn || nColumn < 0 || nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return FALSE;
8301 ZeroMemory(&hdi, sizeof(HDITEMW));
8302 if (lpColumn->mask & LVCF_FMT)
8304 hdi.mask |= HDI_FORMAT;
8305 hdiget.mask = HDI_FORMAT;
8306 if (SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, nColumn, (LPARAM)&hdiget))
8307 hdi.fmt = hdiget.fmt & HDF_STRING;
8309 column_fill_hditem(infoPtr, &hdi, nColumn, lpColumn, isW);
8311 /* set header item attributes */
8312 bResult = SendMessageW(infoPtr->hwndHeader, isW ? HDM_SETITEMW : HDM_SETITEMA, nColumn, (LPARAM)&hdi);
8313 if (!bResult) return FALSE;
8315 if (lpColumn->mask & LVCF_FMT)
8317 COLUMN_INFO *lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, nColumn);
8318 INT oldFmt = lpColumnInfo->fmt;
8320 lpColumnInfo->fmt = lpColumn->fmt;
8321 if ((oldFmt ^ lpColumn->fmt) & (LVCFMT_JUSTIFYMASK | LVCFMT_IMAGE))
8323 if (infoPtr->uView == LV_VIEW_DETAILS) LISTVIEW_InvalidateColumn(infoPtr, nColumn);
8327 if (lpColumn->mask & LVCF_MINWIDTH)
8328 LISTVIEW_GetColumnInfo(infoPtr, nColumn)->cxMin = lpColumn->cxMin;
8330 return TRUE;
8333 /***
8334 * DESCRIPTION:
8335 * Sets the column order array
8337 * PARAMETERS:
8338 * [I] infoPtr : valid pointer to the listview structure
8339 * [I] iCount : number of elements in column order array
8340 * [I] lpiArray : pointer to column order array
8342 * RETURN:
8343 * SUCCESS : TRUE
8344 * FAILURE : FALSE
8346 static BOOL LISTVIEW_SetColumnOrderArray(LISTVIEW_INFO *infoPtr, INT iCount, const INT *lpiArray)
8348 if (!infoPtr->hwndHeader) return FALSE;
8349 infoPtr->colRectsDirty = TRUE;
8350 return SendMessageW(infoPtr->hwndHeader, HDM_SETORDERARRAY, iCount, (LPARAM)lpiArray);
8353 /***
8354 * DESCRIPTION:
8355 * Sets the width of a column
8357 * PARAMETERS:
8358 * [I] infoPtr : valid pointer to the listview structure
8359 * [I] nColumn : column index
8360 * [I] cx : column width
8362 * RETURN:
8363 * SUCCESS : TRUE
8364 * FAILURE : FALSE
8366 static BOOL LISTVIEW_SetColumnWidth(LISTVIEW_INFO *infoPtr, INT nColumn, INT cx)
8368 WCHAR szDispText[DISP_TEXT_SIZE] = { 0 };
8369 INT max_cx = 0;
8370 HDITEMW hdi;
8372 TRACE("(nColumn=%d, cx=%d)\n", nColumn, cx);
8374 /* set column width only if in report or list mode */
8375 if (infoPtr->uView != LV_VIEW_DETAILS && infoPtr->uView != LV_VIEW_LIST) return FALSE;
8377 /* take care of invalid cx values - LVSCW_AUTOSIZE_* values are negative,
8378 with _USEHEADER being the lowest */
8379 if (infoPtr->uView == LV_VIEW_DETAILS && cx < LVSCW_AUTOSIZE_USEHEADER) cx = LVSCW_AUTOSIZE;
8380 else if (infoPtr->uView == LV_VIEW_LIST && cx <= 0) return FALSE;
8382 /* resize all columns if in LV_VIEW_LIST mode */
8383 if(infoPtr->uView == LV_VIEW_LIST)
8385 infoPtr->nItemWidth = cx;
8386 LISTVIEW_InvalidateList(infoPtr);
8387 return TRUE;
8390 if (nColumn < 0 || nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return FALSE;
8392 if (cx == LVSCW_AUTOSIZE || (cx == LVSCW_AUTOSIZE_USEHEADER && nColumn < DPA_GetPtrCount(infoPtr->hdpaColumns) -1))
8394 INT nLabelWidth;
8395 LVITEMW lvItem;
8397 lvItem.mask = LVIF_TEXT;
8398 lvItem.iItem = 0;
8399 lvItem.iSubItem = nColumn;
8400 lvItem.cchTextMax = DISP_TEXT_SIZE;
8401 for (; lvItem.iItem < infoPtr->nItemCount; lvItem.iItem++)
8403 lvItem.pszText = szDispText;
8404 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) continue;
8405 nLabelWidth = LISTVIEW_GetStringWidthT(infoPtr, lvItem.pszText, TRUE);
8406 if (max_cx < nLabelWidth) max_cx = nLabelWidth;
8408 if (infoPtr->himlSmall && (nColumn == 0 || (LISTVIEW_GetColumnInfo(infoPtr, nColumn)->fmt & LVCFMT_IMAGE)))
8409 max_cx += infoPtr->iconSize.cx;
8410 max_cx += TRAILING_LABEL_PADDING;
8411 if (nColumn == 0 && (infoPtr->dwLvExStyle & LVS_EX_CHECKBOXES))
8412 max_cx += GetSystemMetrics(SM_CXSMICON);
8415 /* autosize based on listview items width */
8416 if(cx == LVSCW_AUTOSIZE)
8417 cx = max_cx;
8418 else if(cx == LVSCW_AUTOSIZE_USEHEADER)
8420 /* if iCol is the last column make it fill the remainder of the controls width */
8421 if(nColumn == DPA_GetPtrCount(infoPtr->hdpaColumns) - 1)
8423 RECT rcHeader;
8424 POINT Origin;
8426 LISTVIEW_GetOrigin(infoPtr, &Origin);
8427 LISTVIEW_GetHeaderRect(infoPtr, nColumn, &rcHeader);
8429 cx = infoPtr->rcList.right - Origin.x - rcHeader.left;
8431 else
8433 /* Despite what the MS docs say, if this is not the last
8434 column, then MS resizes the column to the width of the
8435 largest text string in the column, including headers
8436 and items. This is different from LVSCW_AUTOSIZE in that
8437 LVSCW_AUTOSIZE ignores the header string length. */
8438 cx = 0;
8440 /* retrieve header text */
8441 hdi.mask = HDI_TEXT|HDI_FORMAT|HDI_IMAGE|HDI_BITMAP;
8442 hdi.cchTextMax = DISP_TEXT_SIZE;
8443 hdi.pszText = szDispText;
8444 if (SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, nColumn, (LPARAM)&hdi))
8446 HDC hdc = GetDC(infoPtr->hwndSelf);
8447 HFONT old_font = SelectObject(hdc, (HFONT)SendMessageW(infoPtr->hwndHeader, WM_GETFONT, 0, 0));
8448 HIMAGELIST himl = (HIMAGELIST)SendMessageW(infoPtr->hwndHeader, HDM_GETIMAGELIST, 0, 0);
8449 INT bitmap_margin = 0;
8450 SIZE size;
8452 if (GetTextExtentPoint32W(hdc, hdi.pszText, lstrlenW(hdi.pszText), &size))
8453 cx = size.cx + TRAILING_HEADER_PADDING;
8455 if (hdi.fmt & (HDF_IMAGE|HDF_BITMAP))
8456 bitmap_margin = SendMessageW(infoPtr->hwndHeader, HDM_GETBITMAPMARGIN, 0, 0);
8458 if ((hdi.fmt & HDF_IMAGE) && himl)
8460 INT icon_cx, icon_cy;
8462 if (!ImageList_GetIconSize(himl, &icon_cx, &icon_cy))
8463 cx += icon_cx + 2*bitmap_margin;
8465 else if (hdi.fmt & HDF_BITMAP)
8467 BITMAP bmp;
8469 GetObjectW(hdi.hbm, sizeof(BITMAP), &bmp);
8470 cx += bmp.bmWidth + 2*bitmap_margin;
8473 SelectObject(hdc, old_font);
8474 ReleaseDC(infoPtr->hwndSelf, hdc);
8476 cx = max (cx, max_cx);
8480 if (cx < 0) return FALSE;
8482 /* call header to update the column change */
8483 hdi.mask = HDI_WIDTH;
8484 hdi.cxy = max(cx, LISTVIEW_GetColumnInfo(infoPtr, nColumn)->cxMin);
8485 TRACE("hdi.cxy=%d\n", hdi.cxy);
8486 return SendMessageW(infoPtr->hwndHeader, HDM_SETITEMW, nColumn, (LPARAM)&hdi);
8489 /***
8490 * Creates the checkbox imagelist. Helper for LISTVIEW_SetExtendedListViewStyle
8493 static HIMAGELIST LISTVIEW_CreateCheckBoxIL(const LISTVIEW_INFO *infoPtr)
8495 HDC hdc_wnd, hdc;
8496 HBITMAP hbm_im, hbm_mask, hbm_orig;
8497 RECT rc;
8498 HBRUSH hbr_white = GetStockObject(WHITE_BRUSH);
8499 HBRUSH hbr_black = GetStockObject(BLACK_BRUSH);
8500 HIMAGELIST himl;
8502 himl = ImageList_Create(GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON),
8503 ILC_COLOR | ILC_MASK, 2, 2);
8504 hdc_wnd = GetDC(infoPtr->hwndSelf);
8505 hdc = CreateCompatibleDC(hdc_wnd);
8506 hbm_im = CreateCompatibleBitmap(hdc_wnd, GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON));
8507 hbm_mask = CreateBitmap(GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), 1, 1, NULL);
8508 ReleaseDC(infoPtr->hwndSelf, hdc_wnd);
8510 SetRect(&rc, 0, 0, GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON));
8511 hbm_orig = SelectObject(hdc, hbm_mask);
8512 FillRect(hdc, &rc, hbr_white);
8513 InflateRect(&rc, -2, -2);
8514 FillRect(hdc, &rc, hbr_black);
8516 SelectObject(hdc, hbm_im);
8517 DrawFrameControl(hdc, &rc, DFC_BUTTON, DFCS_BUTTONCHECK | DFCS_MONO);
8518 SelectObject(hdc, hbm_orig);
8519 ImageList_Add(himl, hbm_im, hbm_mask);
8521 SelectObject(hdc, hbm_im);
8522 DrawFrameControl(hdc, &rc, DFC_BUTTON, DFCS_BUTTONCHECK | DFCS_MONO | DFCS_CHECKED);
8523 SelectObject(hdc, hbm_orig);
8524 ImageList_Add(himl, hbm_im, hbm_mask);
8526 DeleteObject(hbm_mask);
8527 DeleteObject(hbm_im);
8528 DeleteDC(hdc);
8530 return himl;
8533 /***
8534 * DESCRIPTION:
8535 * Sets the extended listview style.
8537 * PARAMETERS:
8538 * [I] infoPtr : valid pointer to the listview structure
8539 * [I] dwMask : mask
8540 * [I] dwStyle : style
8542 * RETURN:
8543 * SUCCESS : previous style
8544 * FAILURE : 0
8546 static DWORD LISTVIEW_SetExtendedListViewStyle(LISTVIEW_INFO *infoPtr, DWORD mask, DWORD ex_style)
8548 DWORD old_ex_style = infoPtr->dwLvExStyle;
8550 TRACE("mask=0x%08x, ex_style=0x%08x\n", mask, ex_style);
8552 /* set new style */
8553 if (mask)
8554 infoPtr->dwLvExStyle = (old_ex_style & ~mask) | (ex_style & mask);
8555 else
8556 infoPtr->dwLvExStyle = ex_style;
8558 if((infoPtr->dwLvExStyle ^ old_ex_style) & LVS_EX_CHECKBOXES)
8560 HIMAGELIST himl = 0;
8561 if(infoPtr->dwLvExStyle & LVS_EX_CHECKBOXES)
8563 LVITEMW item;
8564 item.mask = LVIF_STATE;
8565 item.stateMask = LVIS_STATEIMAGEMASK;
8566 item.state = INDEXTOSTATEIMAGEMASK(1);
8567 LISTVIEW_SetItemState(infoPtr, -1, &item);
8569 himl = LISTVIEW_CreateCheckBoxIL(infoPtr);
8570 if(!(infoPtr->dwStyle & LVS_SHAREIMAGELISTS))
8571 ImageList_Destroy(infoPtr->himlState);
8573 himl = LISTVIEW_SetImageList(infoPtr, LVSIL_STATE, himl);
8574 /* checkbox list replaces previous custom list or... */
8575 if(((infoPtr->dwLvExStyle & LVS_EX_CHECKBOXES) &&
8576 !(infoPtr->dwStyle & LVS_SHAREIMAGELISTS)) ||
8577 /* ...previous was checkbox list */
8578 (old_ex_style & LVS_EX_CHECKBOXES))
8579 ImageList_Destroy(himl);
8582 if((infoPtr->dwLvExStyle ^ old_ex_style) & LVS_EX_HEADERDRAGDROP)
8584 DWORD style;
8586 /* if not already created */
8587 LISTVIEW_CreateHeader(infoPtr);
8589 style = GetWindowLongW(infoPtr->hwndHeader, GWL_STYLE);
8590 if (infoPtr->dwLvExStyle & LVS_EX_HEADERDRAGDROP)
8591 style |= HDS_DRAGDROP;
8592 else
8593 style &= ~HDS_DRAGDROP;
8594 SetWindowLongW(infoPtr->hwndHeader, GWL_STYLE, style);
8597 /* GRIDLINES adds decoration at top so changes sizes */
8598 if((infoPtr->dwLvExStyle ^ old_ex_style) & LVS_EX_GRIDLINES)
8600 LISTVIEW_CreateHeader(infoPtr);
8601 LISTVIEW_UpdateSize(infoPtr);
8604 if((infoPtr->dwLvExStyle ^ old_ex_style) & LVS_EX_FULLROWSELECT)
8606 LISTVIEW_CreateHeader(infoPtr);
8609 if((infoPtr->dwLvExStyle ^ old_ex_style) & LVS_EX_TRANSPARENTBKGND)
8611 if (infoPtr->dwLvExStyle & LVS_EX_TRANSPARENTBKGND)
8612 LISTVIEW_SetBkColor(infoPtr, CLR_NONE);
8615 if((infoPtr->dwLvExStyle ^ old_ex_style) & LVS_EX_HEADERINALLVIEWS)
8617 if (infoPtr->dwLvExStyle & LVS_EX_HEADERINALLVIEWS)
8618 LISTVIEW_CreateHeader(infoPtr);
8619 else
8620 ShowWindow(infoPtr->hwndHeader, SW_HIDE);
8621 LISTVIEW_UpdateSize(infoPtr);
8622 LISTVIEW_UpdateScroll(infoPtr);
8625 LISTVIEW_InvalidateList(infoPtr);
8626 return old_ex_style;
8629 /***
8630 * DESCRIPTION:
8631 * Sets the new hot cursor used during hot tracking and hover selection.
8633 * PARAMETER(S):
8634 * [I] infoPtr : valid pointer to the listview structure
8635 * [I] hCursor : the new hot cursor handle
8637 * RETURN:
8638 * Returns the previous hot cursor
8640 static HCURSOR LISTVIEW_SetHotCursor(LISTVIEW_INFO *infoPtr, HCURSOR hCursor)
8642 HCURSOR oldCursor = infoPtr->hHotCursor;
8644 infoPtr->hHotCursor = hCursor;
8646 return oldCursor;
8650 /***
8651 * DESCRIPTION:
8652 * Sets the hot item index.
8654 * PARAMETERS:
8655 * [I] infoPtr : valid pointer to the listview structure
8656 * [I] iIndex : index
8658 * RETURN:
8659 * SUCCESS : previous hot item index
8660 * FAILURE : -1 (no hot item)
8662 static INT LISTVIEW_SetHotItem(LISTVIEW_INFO *infoPtr, INT iIndex)
8664 INT iOldIndex = infoPtr->nHotItem;
8666 infoPtr->nHotItem = iIndex;
8668 return iOldIndex;
8672 /***
8673 * DESCRIPTION:
8674 * Sets the amount of time the cursor must hover over an item before it is selected.
8676 * PARAMETER(S):
8677 * [I] infoPtr : valid pointer to the listview structure
8678 * [I] dwHoverTime : hover time, if -1 the hover time is set to the default
8680 * RETURN:
8681 * Returns the previous hover time
8683 static DWORD LISTVIEW_SetHoverTime(LISTVIEW_INFO *infoPtr, DWORD dwHoverTime)
8685 DWORD oldHoverTime = infoPtr->dwHoverTime;
8687 infoPtr->dwHoverTime = dwHoverTime;
8689 return oldHoverTime;
8692 /***
8693 * DESCRIPTION:
8694 * Sets spacing for icons of LVS_ICON style.
8696 * PARAMETER(S):
8697 * [I] infoPtr : valid pointer to the listview structure
8698 * [I] cx : horizontal spacing (-1 = system spacing, 0 = autosize)
8699 * [I] cy : vertical spacing (-1 = system spacing, 0 = autosize)
8701 * RETURN:
8702 * MAKELONG(oldcx, oldcy)
8704 static DWORD LISTVIEW_SetIconSpacing(LISTVIEW_INFO *infoPtr, INT cx, INT cy)
8706 INT iconWidth = 0, iconHeight = 0;
8707 DWORD oldspacing = MAKELONG(infoPtr->iconSpacing.cx, infoPtr->iconSpacing.cy);
8709 TRACE("requested=(%d,%d)\n", cx, cy);
8711 /* set to defaults, if instructed to */
8712 if (cx == -1 && cy == -1)
8714 infoPtr->autoSpacing = TRUE;
8715 if (infoPtr->himlNormal)
8716 ImageList_GetIconSize(infoPtr->himlNormal, &iconWidth, &iconHeight);
8717 cx = GetSystemMetrics(SM_CXICONSPACING) - GetSystemMetrics(SM_CXICON) + iconWidth;
8718 cy = GetSystemMetrics(SM_CYICONSPACING) - GetSystemMetrics(SM_CYICON) + iconHeight;
8720 else
8721 infoPtr->autoSpacing = FALSE;
8723 /* if 0 then keep width */
8724 if (cx != 0)
8725 infoPtr->iconSpacing.cx = cx;
8727 /* if 0 then keep height */
8728 if (cy != 0)
8729 infoPtr->iconSpacing.cy = cy;
8731 TRACE("old=(%d,%d), new=(%d,%d), iconSize=(%d,%d), ntmH=%d\n",
8732 LOWORD(oldspacing), HIWORD(oldspacing), infoPtr->iconSpacing.cx, infoPtr->iconSpacing.cy,
8733 infoPtr->iconSize.cx, infoPtr->iconSize.cy,
8734 infoPtr->ntmHeight);
8736 /* these depend on the iconSpacing */
8737 LISTVIEW_UpdateItemSize(infoPtr);
8739 return oldspacing;
8742 static inline void set_icon_size(SIZE *size, HIMAGELIST himl, BOOL is_small)
8744 INT cx, cy;
8746 if (himl && ImageList_GetIconSize(himl, &cx, &cy))
8748 size->cx = cx;
8749 size->cy = cy;
8751 else
8753 size->cx = GetSystemMetrics(is_small ? SM_CXSMICON : SM_CXICON);
8754 size->cy = GetSystemMetrics(is_small ? SM_CYSMICON : SM_CYICON);
8758 /***
8759 * DESCRIPTION:
8760 * Sets image lists.
8762 * PARAMETER(S):
8763 * [I] infoPtr : valid pointer to the listview structure
8764 * [I] nType : image list type
8765 * [I] himl : image list handle
8767 * RETURN:
8768 * SUCCESS : old image list
8769 * FAILURE : NULL
8771 static HIMAGELIST LISTVIEW_SetImageList(LISTVIEW_INFO *infoPtr, INT nType, HIMAGELIST himl)
8773 INT oldHeight = infoPtr->nItemHeight;
8774 HIMAGELIST himlOld = 0;
8776 TRACE("(nType=%d, himl=%p)\n", nType, himl);
8778 switch (nType)
8780 case LVSIL_NORMAL:
8781 himlOld = infoPtr->himlNormal;
8782 infoPtr->himlNormal = himl;
8783 if (infoPtr->uView == LV_VIEW_ICON) set_icon_size(&infoPtr->iconSize, himl, FALSE);
8784 if (infoPtr->autoSpacing)
8785 LISTVIEW_SetIconSpacing(infoPtr, -1, -1);
8786 break;
8788 case LVSIL_SMALL:
8789 himlOld = infoPtr->himlSmall;
8790 infoPtr->himlSmall = himl;
8791 if (infoPtr->uView != LV_VIEW_ICON) set_icon_size(&infoPtr->iconSize, himl, TRUE);
8792 if (infoPtr->hwndHeader)
8793 SendMessageW(infoPtr->hwndHeader, HDM_SETIMAGELIST, 0, (LPARAM)himl);
8794 break;
8796 case LVSIL_STATE:
8797 himlOld = infoPtr->himlState;
8798 infoPtr->himlState = himl;
8799 set_icon_size(&infoPtr->iconStateSize, himl, TRUE);
8800 ImageList_SetBkColor(infoPtr->himlState, CLR_NONE);
8801 break;
8803 default:
8804 ERR("Unknown icon type=%d\n", nType);
8805 return NULL;
8808 infoPtr->nItemHeight = LISTVIEW_CalculateItemHeight(infoPtr);
8809 if (infoPtr->nItemHeight != oldHeight)
8810 LISTVIEW_UpdateScroll(infoPtr);
8812 return himlOld;
8815 /***
8816 * DESCRIPTION:
8817 * Preallocates memory (does *not* set the actual count of items !)
8819 * PARAMETER(S):
8820 * [I] infoPtr : valid pointer to the listview structure
8821 * [I] nItems : item count (projected number of items to allocate)
8822 * [I] dwFlags : update flags
8824 * RETURN:
8825 * SUCCESS : TRUE
8826 * FAILURE : FALSE
8828 static BOOL LISTVIEW_SetItemCount(LISTVIEW_INFO *infoPtr, INT nItems, DWORD dwFlags)
8830 TRACE("(nItems=%d, dwFlags=%x)\n", nItems, dwFlags);
8832 if (infoPtr->dwStyle & LVS_OWNERDATA)
8834 INT nOldCount = infoPtr->nItemCount;
8835 infoPtr->nItemCount = nItems;
8837 if (nItems < nOldCount)
8839 RANGE range = { nItems, nOldCount };
8840 ranges_del(infoPtr->selectionRanges, range);
8841 if (infoPtr->nFocusedItem >= nItems)
8843 LISTVIEW_SetItemFocus(infoPtr, -1);
8844 infoPtr->nFocusedItem = -1;
8845 SetRectEmpty(&infoPtr->rcFocus);
8849 LISTVIEW_UpdateScroll(infoPtr);
8851 /* the flags are valid only in ownerdata report and list modes */
8852 if (infoPtr->uView == LV_VIEW_ICON || infoPtr->uView == LV_VIEW_SMALLICON) dwFlags = 0;
8854 if (!(dwFlags & LVSICF_NOSCROLL) && infoPtr->nFocusedItem != -1)
8855 LISTVIEW_EnsureVisible(infoPtr, infoPtr->nFocusedItem, FALSE);
8857 if (!(dwFlags & LVSICF_NOINVALIDATEALL))
8858 LISTVIEW_InvalidateList(infoPtr);
8859 else
8861 INT nFrom, nTo;
8862 POINT Origin;
8863 RECT rcErase;
8865 LISTVIEW_GetOrigin(infoPtr, &Origin);
8866 nFrom = min(nOldCount, nItems);
8867 nTo = max(nOldCount, nItems);
8869 if (infoPtr->uView == LV_VIEW_DETAILS)
8871 SetRect(&rcErase, 0, nFrom * infoPtr->nItemHeight, infoPtr->nItemWidth,
8872 nTo * infoPtr->nItemHeight);
8873 OffsetRect(&rcErase, Origin.x, Origin.y);
8874 if (IntersectRect(&rcErase, &rcErase, &infoPtr->rcList))
8875 LISTVIEW_InvalidateRect(infoPtr, &rcErase);
8877 else /* LV_VIEW_LIST */
8879 INT nPerCol = LISTVIEW_GetCountPerColumn(infoPtr);
8881 rcErase.left = (nFrom / nPerCol) * infoPtr->nItemWidth;
8882 rcErase.top = (nFrom % nPerCol) * infoPtr->nItemHeight;
8883 rcErase.right = rcErase.left + infoPtr->nItemWidth;
8884 rcErase.bottom = nPerCol * infoPtr->nItemHeight;
8885 OffsetRect(&rcErase, Origin.x, Origin.y);
8886 if (IntersectRect(&rcErase, &rcErase, &infoPtr->rcList))
8887 LISTVIEW_InvalidateRect(infoPtr, &rcErase);
8889 rcErase.left = (nFrom / nPerCol + 1) * infoPtr->nItemWidth;
8890 rcErase.top = 0;
8891 rcErase.right = (nTo / nPerCol + 1) * infoPtr->nItemWidth;
8892 rcErase.bottom = nPerCol * infoPtr->nItemHeight;
8893 OffsetRect(&rcErase, Origin.x, Origin.y);
8894 if (IntersectRect(&rcErase, &rcErase, &infoPtr->rcList))
8895 LISTVIEW_InvalidateRect(infoPtr, &rcErase);
8899 else
8901 /* According to MSDN for non-LVS_OWNERDATA this is just
8902 * a performance issue. The control allocates its internal
8903 * data structures for the number of items specified. It
8904 * cuts down on the number of memory allocations. Therefore
8905 * we will just issue a WARN here
8907 WARN("for non-ownerdata performance option not implemented.\n");
8910 return TRUE;
8913 /***
8914 * DESCRIPTION:
8915 * Sets the position of an item.
8917 * PARAMETER(S):
8918 * [I] infoPtr : valid pointer to the listview structure
8919 * [I] nItem : item index
8920 * [I] pt : coordinate
8922 * RETURN:
8923 * SUCCESS : TRUE
8924 * FAILURE : FALSE
8926 static BOOL LISTVIEW_SetItemPosition(LISTVIEW_INFO *infoPtr, INT nItem, const POINT *pt)
8928 POINT Origin, Pt;
8930 TRACE("(nItem=%d, pt=%s)\n", nItem, wine_dbgstr_point(pt));
8932 if (!pt || nItem < 0 || nItem >= infoPtr->nItemCount ||
8933 !(infoPtr->uView == LV_VIEW_ICON || infoPtr->uView == LV_VIEW_SMALLICON)) return FALSE;
8935 Pt = *pt;
8936 LISTVIEW_GetOrigin(infoPtr, &Origin);
8938 /* This point value seems to be an undocumented feature.
8939 * The best guess is that it means either at the origin,
8940 * or at true beginning of the list. I will assume the origin. */
8941 if ((Pt.x == -1) && (Pt.y == -1))
8942 Pt = Origin;
8944 if (infoPtr->uView == LV_VIEW_ICON)
8946 Pt.x -= (infoPtr->nItemWidth - infoPtr->iconSize.cx) / 2;
8947 Pt.y -= ICON_TOP_PADDING;
8949 Pt.x -= Origin.x;
8950 Pt.y -= Origin.y;
8952 return LISTVIEW_MoveIconTo(infoPtr, nItem, &Pt, FALSE);
8955 /***
8956 * DESCRIPTION:
8957 * Sets the state of one or many items.
8959 * PARAMETER(S):
8960 * [I] infoPtr : valid pointer to the listview structure
8961 * [I] nItem : item index
8962 * [I] item : item or subitem info
8964 * RETURN:
8965 * SUCCESS : TRUE
8966 * FAILURE : FALSE
8968 static BOOL LISTVIEW_SetItemState(LISTVIEW_INFO *infoPtr, INT nItem, const LVITEMW *item)
8970 BOOL ret = TRUE;
8971 LVITEMW lvItem;
8973 if (!item) return FALSE;
8975 lvItem.iItem = nItem;
8976 lvItem.iSubItem = 0;
8977 lvItem.mask = LVIF_STATE;
8978 lvItem.state = item->state;
8979 lvItem.stateMask = item->stateMask;
8980 TRACE("item=%s\n", debuglvitem_t(&lvItem, TRUE));
8982 if (nItem == -1)
8984 UINT oldstate = 0;
8985 DWORD old_mask;
8987 /* special case optimization for recurring attempt to deselect all */
8988 if (lvItem.state == 0 && lvItem.stateMask == LVIS_SELECTED && !LISTVIEW_GetSelectedCount(infoPtr))
8989 return TRUE;
8991 /* select all isn't allowed in LVS_SINGLESEL */
8992 if ((lvItem.state & lvItem.stateMask & LVIS_SELECTED) && (infoPtr->dwStyle & LVS_SINGLESEL))
8993 return FALSE;
8995 /* focus all isn't allowed */
8996 if (lvItem.state & lvItem.stateMask & LVIS_FOCUSED) return FALSE;
8998 old_mask = infoPtr->notify_mask & NOTIFY_MASK_ITEM_CHANGE;
8999 if (infoPtr->dwStyle & LVS_OWNERDATA)
9001 infoPtr->notify_mask &= ~NOTIFY_MASK_ITEM_CHANGE;
9002 if (!(lvItem.state & LVIS_SELECTED) && LISTVIEW_GetSelectedCount(infoPtr))
9003 oldstate |= LVIS_SELECTED;
9004 if (infoPtr->nFocusedItem != -1) oldstate |= LVIS_FOCUSED;
9007 /* apply to all items */
9008 for (lvItem.iItem = 0; lvItem.iItem < infoPtr->nItemCount; lvItem.iItem++)
9009 if (!LISTVIEW_SetItemT(infoPtr, &lvItem, TRUE)) ret = FALSE;
9011 if (infoPtr->dwStyle & LVS_OWNERDATA)
9013 NMLISTVIEW nmlv;
9015 infoPtr->notify_mask |= old_mask;
9017 nmlv.iItem = -1;
9018 nmlv.iSubItem = 0;
9019 nmlv.uNewState = lvItem.state & lvItem.stateMask;
9020 nmlv.uOldState = oldstate & lvItem.stateMask;
9021 nmlv.uChanged = LVIF_STATE;
9022 nmlv.ptAction.x = nmlv.ptAction.y = 0;
9023 nmlv.lParam = 0;
9025 notify_listview(infoPtr, LVN_ITEMCHANGED, &nmlv);
9028 else
9029 ret = LISTVIEW_SetItemT(infoPtr, &lvItem, TRUE);
9031 return ret;
9034 /***
9035 * DESCRIPTION:
9036 * Sets the text of an item or subitem.
9038 * PARAMETER(S):
9039 * [I] hwnd : window handle
9040 * [I] nItem : item index
9041 * [I] lpLVItem : item or subitem info
9042 * [I] isW : TRUE if input is Unicode
9044 * RETURN:
9045 * SUCCESS : TRUE
9046 * FAILURE : FALSE
9048 static BOOL LISTVIEW_SetItemTextT(LISTVIEW_INFO *infoPtr, INT nItem, const LVITEMW *lpLVItem, BOOL isW)
9050 LVITEMW lvItem;
9052 if (!lpLVItem || nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
9053 if (infoPtr->dwStyle & LVS_OWNERDATA) return FALSE;
9055 lvItem.iItem = nItem;
9056 lvItem.iSubItem = lpLVItem->iSubItem;
9057 lvItem.mask = LVIF_TEXT;
9058 lvItem.pszText = lpLVItem->pszText;
9059 lvItem.cchTextMax = lpLVItem->cchTextMax;
9061 TRACE("(nItem=%d, lpLVItem=%s, isW=%d)\n", nItem, debuglvitem_t(&lvItem, isW), isW);
9063 return LISTVIEW_SetItemT(infoPtr, &lvItem, isW);
9066 /***
9067 * DESCRIPTION:
9068 * Set item index that marks the start of a multiple selection.
9070 * PARAMETER(S):
9071 * [I] infoPtr : valid pointer to the listview structure
9072 * [I] nIndex : index
9074 * RETURN:
9075 * Index number or -1 if there is no selection mark.
9077 static INT LISTVIEW_SetSelectionMark(LISTVIEW_INFO *infoPtr, INT nIndex)
9079 INT nOldIndex = infoPtr->nSelectionMark;
9081 TRACE("(nIndex=%d)\n", nIndex);
9083 if (nIndex >= -1 && nIndex < infoPtr->nItemCount)
9084 infoPtr->nSelectionMark = nIndex;
9086 return nOldIndex;
9089 /***
9090 * DESCRIPTION:
9091 * Sets the text background color.
9093 * PARAMETER(S):
9094 * [I] infoPtr : valid pointer to the listview structure
9095 * [I] color : text background color
9097 * RETURN:
9098 * SUCCESS : TRUE
9099 * FAILURE : FALSE
9101 static BOOL LISTVIEW_SetTextBkColor(LISTVIEW_INFO *infoPtr, COLORREF color)
9103 TRACE("(color=%x)\n", color);
9105 infoPtr->clrTextBk = color;
9106 return TRUE;
9109 /***
9110 * DESCRIPTION:
9111 * Sets the text foreground color.
9113 * PARAMETER(S):
9114 * [I] infoPtr : valid pointer to the listview structure
9115 * [I] color : text color
9117 * RETURN:
9118 * SUCCESS : TRUE
9119 * FAILURE : FALSE
9121 static BOOL LISTVIEW_SetTextColor (LISTVIEW_INFO *infoPtr, COLORREF color)
9123 TRACE("(color=%x)\n", color);
9125 infoPtr->clrText = color;
9126 return TRUE;
9129 /***
9130 * DESCRIPTION:
9131 * Sets new ToolTip window to ListView control.
9133 * PARAMETER(S):
9134 * [I] infoPtr : valid pointer to the listview structure
9135 * [I] hwndNewToolTip : handle to new ToolTip
9137 * RETURN:
9138 * old tool tip
9140 static HWND LISTVIEW_SetToolTips( LISTVIEW_INFO *infoPtr, HWND hwndNewToolTip)
9142 HWND hwndOldToolTip = infoPtr->hwndToolTip;
9143 infoPtr->hwndToolTip = hwndNewToolTip;
9144 return hwndOldToolTip;
9148 * DESCRIPTION:
9149 * sets the Unicode character format flag for the control
9150 * PARAMETER(S):
9151 * [I] infoPtr :valid pointer to the listview structure
9152 * [I] fUnicode :true to switch to UNICODE false to switch to ANSI
9154 * RETURN:
9155 * Old Unicode Format
9157 static BOOL LISTVIEW_SetUnicodeFormat( LISTVIEW_INFO *infoPtr, BOOL unicode)
9159 SHORT rc = infoPtr->notifyFormat;
9160 infoPtr->notifyFormat = (unicode) ? NFR_UNICODE : NFR_ANSI;
9161 return rc == NFR_UNICODE;
9165 * DESCRIPTION:
9166 * sets the control view mode
9167 * PARAMETER(S):
9168 * [I] infoPtr :valid pointer to the listview structure
9169 * [I] nView :new view mode value
9171 * RETURN:
9172 * SUCCESS: 1
9173 * FAILURE: -1
9175 static INT LISTVIEW_SetView(LISTVIEW_INFO *infoPtr, DWORD nView)
9177 HIMAGELIST himl;
9179 if (infoPtr->uView == nView) return 1;
9181 if ((INT)nView < 0 || nView > LV_VIEW_MAX) return -1;
9182 if (nView == LV_VIEW_TILE)
9184 FIXME("View LV_VIEW_TILE unimplemented\n");
9185 return -1;
9188 infoPtr->uView = nView;
9190 SendMessageW(infoPtr->hwndEdit, WM_KILLFOCUS, 0, 0);
9191 ShowWindow(infoPtr->hwndHeader, SW_HIDE);
9193 ShowScrollBar(infoPtr->hwndSelf, SB_BOTH, FALSE);
9194 SetRectEmpty(&infoPtr->rcFocus);
9196 himl = (nView == LV_VIEW_ICON ? infoPtr->himlNormal : infoPtr->himlSmall);
9197 set_icon_size(&infoPtr->iconSize, himl, nView != LV_VIEW_ICON);
9199 switch (nView)
9201 case LV_VIEW_ICON:
9202 case LV_VIEW_SMALLICON:
9203 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
9204 break;
9205 case LV_VIEW_DETAILS:
9207 HDLAYOUT hl;
9208 WINDOWPOS wp;
9210 LISTVIEW_CreateHeader( infoPtr );
9212 hl.prc = &infoPtr->rcList;
9213 hl.pwpos = &wp;
9214 SendMessageW(infoPtr->hwndHeader, HDM_LAYOUT, 0, (LPARAM)&hl);
9215 SetWindowPos(infoPtr->hwndHeader, infoPtr->hwndSelf, wp.x, wp.y, wp.cx, wp.cy,
9216 wp.flags | ((infoPtr->dwStyle & LVS_NOCOLUMNHEADER) ? SWP_HIDEWINDOW : SWP_SHOWWINDOW));
9217 break;
9219 case LV_VIEW_LIST:
9220 break;
9223 LISTVIEW_UpdateItemSize(infoPtr);
9224 LISTVIEW_UpdateSize(infoPtr);
9225 LISTVIEW_UpdateScroll(infoPtr);
9226 LISTVIEW_InvalidateList(infoPtr);
9228 TRACE("nView=%d\n", nView);
9230 return 1;
9233 /* LISTVIEW_SetWorkAreas */
9235 /***
9236 * DESCRIPTION:
9237 * Callback internally used by LISTVIEW_SortItems() in response of LVM_SORTITEMS
9239 * PARAMETER(S):
9240 * [I] first : pointer to first ITEM_INFO to compare
9241 * [I] second : pointer to second ITEM_INFO to compare
9242 * [I] lParam : HWND of control
9244 * RETURN:
9245 * if first comes before second : negative
9246 * if first comes after second : positive
9247 * if first and second are equivalent : zero
9249 static INT WINAPI LISTVIEW_CallBackCompare(LPVOID first, LPVOID second, LPARAM lParam)
9251 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)lParam;
9252 ITEM_INFO* lv_first = DPA_GetPtr( first, 0 );
9253 ITEM_INFO* lv_second = DPA_GetPtr( second, 0 );
9255 /* Forward the call to the client defined callback */
9256 return (infoPtr->pfnCompare)( lv_first->lParam , lv_second->lParam, infoPtr->lParamSort );
9259 /***
9260 * DESCRIPTION:
9261 * Callback internally used by LISTVIEW_SortItems() in response of LVM_SORTITEMSEX
9263 * PARAMETER(S):
9264 * [I] first : pointer to first ITEM_INFO to compare
9265 * [I] second : pointer to second ITEM_INFO to compare
9266 * [I] lParam : HWND of control
9268 * RETURN:
9269 * if first comes before second : negative
9270 * if first comes after second : positive
9271 * if first and second are equivalent : zero
9273 static INT WINAPI LISTVIEW_CallBackCompareEx(LPVOID first, LPVOID second, LPARAM lParam)
9275 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)lParam;
9276 INT first_idx = DPA_GetPtrIndex( infoPtr->hdpaItems, first );
9277 INT second_idx = DPA_GetPtrIndex( infoPtr->hdpaItems, second );
9279 /* Forward the call to the client defined callback */
9280 return (infoPtr->pfnCompare)( first_idx, second_idx, infoPtr->lParamSort );
9283 /***
9284 * DESCRIPTION:
9285 * Sorts the listview items.
9287 * PARAMETER(S):
9288 * [I] infoPtr : valid pointer to the listview structure
9289 * [I] pfnCompare : application-defined value
9290 * [I] lParamSort : pointer to comparison callback
9291 * [I] IsEx : TRUE when LVM_SORTITEMSEX used
9293 * RETURN:
9294 * SUCCESS : TRUE
9295 * FAILURE : FALSE
9297 static BOOL LISTVIEW_SortItems(LISTVIEW_INFO *infoPtr, PFNLVCOMPARE pfnCompare,
9298 LPARAM lParamSort, BOOL IsEx)
9300 HDPA hdpaSubItems;
9301 ITEM_INFO *lpItem;
9302 LPVOID selectionMarkItem = NULL;
9303 LPVOID focusedItem = NULL;
9304 int i;
9306 TRACE("(pfnCompare=%p, lParamSort=%lx)\n", pfnCompare, lParamSort);
9308 if (infoPtr->dwStyle & LVS_OWNERDATA) return FALSE;
9310 if (!pfnCompare) return FALSE;
9311 if (!infoPtr->hdpaItems) return FALSE;
9313 /* if there are 0 or 1 items, there is no need to sort */
9314 if (infoPtr->nItemCount < 2) return TRUE;
9316 /* clear selection */
9317 ranges_clear(infoPtr->selectionRanges);
9319 /* save selection mark and focused item */
9320 if (infoPtr->nSelectionMark >= 0)
9321 selectionMarkItem = DPA_GetPtr(infoPtr->hdpaItems, infoPtr->nSelectionMark);
9322 if (infoPtr->nFocusedItem >= 0)
9323 focusedItem = DPA_GetPtr(infoPtr->hdpaItems, infoPtr->nFocusedItem);
9325 infoPtr->pfnCompare = pfnCompare;
9326 infoPtr->lParamSort = lParamSort;
9327 if (IsEx)
9328 DPA_Sort(infoPtr->hdpaItems, LISTVIEW_CallBackCompareEx, (LPARAM)infoPtr);
9329 else
9330 DPA_Sort(infoPtr->hdpaItems, LISTVIEW_CallBackCompare, (LPARAM)infoPtr);
9332 /* restore selection ranges */
9333 for (i=0; i < infoPtr->nItemCount; i++)
9335 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, i);
9336 lpItem = DPA_GetPtr(hdpaSubItems, 0);
9338 if (lpItem->state & LVIS_SELECTED)
9339 ranges_additem(infoPtr->selectionRanges, i);
9341 /* restore selection mark and focused item */
9342 infoPtr->nSelectionMark = DPA_GetPtrIndex(infoPtr->hdpaItems, selectionMarkItem);
9343 infoPtr->nFocusedItem = DPA_GetPtrIndex(infoPtr->hdpaItems, focusedItem);
9345 /* I believe nHotItem should be left alone, see LISTVIEW_ShiftIndices */
9347 /* refresh the display */
9348 LISTVIEW_InvalidateList(infoPtr);
9349 return TRUE;
9352 /***
9353 * DESCRIPTION:
9354 * Update theme handle after a theme change.
9356 * PARAMETER(S):
9357 * [I] infoPtr : valid pointer to the listview structure
9359 * RETURN:
9360 * SUCCESS : 0
9361 * FAILURE : something else
9363 static LRESULT LISTVIEW_ThemeChanged(const LISTVIEW_INFO *infoPtr)
9365 HTHEME theme = GetWindowTheme(infoPtr->hwndSelf);
9366 CloseThemeData(theme);
9367 OpenThemeData(infoPtr->hwndSelf, themeClass);
9368 return 0;
9371 /***
9372 * DESCRIPTION:
9373 * Updates an items or rearranges the listview control.
9375 * PARAMETER(S):
9376 * [I] infoPtr : valid pointer to the listview structure
9377 * [I] nItem : item index
9379 * RETURN:
9380 * SUCCESS : TRUE
9381 * FAILURE : FALSE
9383 static BOOL LISTVIEW_Update(LISTVIEW_INFO *infoPtr, INT nItem)
9385 TRACE("(nItem=%d)\n", nItem);
9387 if (nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
9389 /* rearrange with default alignment style */
9390 if (is_autoarrange(infoPtr))
9391 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
9392 else
9393 LISTVIEW_InvalidateItem(infoPtr, nItem);
9395 return TRUE;
9398 /***
9399 * DESCRIPTION:
9400 * Draw the track line at the place defined in the infoPtr structure.
9401 * The line is drawn with a XOR pen so drawing the line for the second time
9402 * in the same place erases the line.
9404 * PARAMETER(S):
9405 * [I] infoPtr : valid pointer to the listview structure
9407 * RETURN:
9408 * SUCCESS : TRUE
9409 * FAILURE : FALSE
9411 static BOOL LISTVIEW_DrawTrackLine(const LISTVIEW_INFO *infoPtr)
9413 HDC hdc;
9415 if (infoPtr->xTrackLine == -1)
9416 return FALSE;
9418 if (!(hdc = GetDC(infoPtr->hwndSelf)))
9419 return FALSE;
9420 PatBlt( hdc, infoPtr->xTrackLine, infoPtr->rcList.top,
9421 1, infoPtr->rcList.bottom - infoPtr->rcList.top, DSTINVERT );
9422 ReleaseDC(infoPtr->hwndSelf, hdc);
9423 return TRUE;
9426 /***
9427 * DESCRIPTION:
9428 * Called when an edit control should be displayed. This function is called after
9429 * we are sure that there was a single click - not a double click (this is a TIMERPROC).
9431 * PARAMETER(S):
9432 * [I] hwnd : Handle to the listview
9433 * [I] uMsg : WM_TIMER (ignored)
9434 * [I] idEvent : The timer ID interpreted as a pointer to a DELAYED_EDIT_ITEM struct
9435 * [I] dwTimer : The elapsed time (ignored)
9437 * RETURN:
9438 * None.
9440 static VOID CALLBACK LISTVIEW_DelayedEditItem(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime)
9442 DELAYED_ITEM_EDIT *editItem = (DELAYED_ITEM_EDIT *)idEvent;
9443 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)GetWindowLongPtrW(hwnd, 0);
9445 KillTimer(hwnd, idEvent);
9446 editItem->fEnabled = FALSE;
9447 /* check if the item is still selected */
9448 if (infoPtr->bFocus && LISTVIEW_GetItemState(infoPtr, editItem->iItem, LVIS_SELECTED))
9449 LISTVIEW_EditLabelT(infoPtr, editItem->iItem, TRUE);
9452 /***
9453 * DESCRIPTION:
9454 * Creates the listview control - the WM_NCCREATE phase.
9456 * PARAMETER(S):
9457 * [I] hwnd : window handle
9458 * [I] lpcs : the create parameters
9460 * RETURN:
9461 * Success: TRUE
9462 * Failure: FALSE
9464 static LRESULT LISTVIEW_NCCreate(HWND hwnd, WPARAM wParam, const CREATESTRUCTW *lpcs)
9466 LISTVIEW_INFO *infoPtr;
9467 LOGFONTW logFont;
9469 TRACE("(lpcs=%p)\n", lpcs);
9471 /* initialize info pointer */
9472 infoPtr = Alloc(sizeof(LISTVIEW_INFO));
9473 if (!infoPtr) return FALSE;
9475 SetWindowLongPtrW(hwnd, 0, (DWORD_PTR)infoPtr);
9477 infoPtr->hwndSelf = hwnd;
9478 infoPtr->dwStyle = lpcs->style; /* Note: may be changed in WM_CREATE */
9479 map_style_view(infoPtr);
9480 /* determine the type of structures to use */
9481 infoPtr->hwndNotify = lpcs->hwndParent;
9482 /* infoPtr->notifyFormat will be filled in WM_CREATE */
9484 /* initialize color information */
9485 infoPtr->clrBk = CLR_NONE;
9486 infoPtr->clrText = CLR_DEFAULT;
9487 infoPtr->clrTextBk = CLR_DEFAULT;
9488 LISTVIEW_SetBkColor(infoPtr, comctl32_color.clrWindow);
9490 /* set default values */
9491 infoPtr->nFocusedItem = -1;
9492 infoPtr->nSelectionMark = -1;
9493 infoPtr->nHotItem = -1;
9494 infoPtr->redraw = TRUE;
9495 infoPtr->bNoItemMetrics = TRUE;
9496 infoPtr->notify_mask = NOTIFY_MASK_UNMASK_ALL;
9497 infoPtr->autoSpacing = TRUE;
9498 infoPtr->iconSpacing.cx = GetSystemMetrics(SM_CXICONSPACING) - GetSystemMetrics(SM_CXICON);
9499 infoPtr->iconSpacing.cy = GetSystemMetrics(SM_CYICONSPACING) - GetSystemMetrics(SM_CYICON);
9500 infoPtr->nEditLabelItem = -1;
9501 infoPtr->nLButtonDownItem = -1;
9502 infoPtr->dwHoverTime = HOVER_DEFAULT; /* default system hover time */
9503 infoPtr->cWheelRemainder = 0;
9504 infoPtr->nMeasureItemHeight = 0;
9505 infoPtr->xTrackLine = -1; /* no track line */
9506 infoPtr->itemEdit.fEnabled = FALSE;
9507 infoPtr->iVersion = COMCTL32_VERSION;
9508 infoPtr->colRectsDirty = FALSE;
9509 infoPtr->selected_column = -1;
9511 /* get default font (icon title) */
9512 SystemParametersInfoW(SPI_GETICONTITLELOGFONT, 0, &logFont, 0);
9513 infoPtr->hDefaultFont = CreateFontIndirectW(&logFont);
9514 infoPtr->hFont = infoPtr->hDefaultFont;
9515 LISTVIEW_SaveTextMetrics(infoPtr);
9517 /* allocate memory for the data structure */
9518 if (!(infoPtr->selectionRanges = ranges_create(10))) goto fail;
9519 if (!(infoPtr->hdpaItems = DPA_Create(10))) goto fail;
9520 if (!(infoPtr->hdpaItemIds = DPA_Create(10))) goto fail;
9521 if (!(infoPtr->hdpaPosX = DPA_Create(10))) goto fail;
9522 if (!(infoPtr->hdpaPosY = DPA_Create(10))) goto fail;
9523 if (!(infoPtr->hdpaColumns = DPA_Create(10))) goto fail;
9525 return DefWindowProcW(hwnd, WM_NCCREATE, wParam, (LPARAM)lpcs);
9527 fail:
9528 DestroyWindow(infoPtr->hwndHeader);
9529 ranges_destroy(infoPtr->selectionRanges);
9530 DPA_Destroy(infoPtr->hdpaItems);
9531 DPA_Destroy(infoPtr->hdpaItemIds);
9532 DPA_Destroy(infoPtr->hdpaPosX);
9533 DPA_Destroy(infoPtr->hdpaPosY);
9534 DPA_Destroy(infoPtr->hdpaColumns);
9535 Free(infoPtr);
9536 return FALSE;
9539 /***
9540 * DESCRIPTION:
9541 * Creates the listview control - the WM_CREATE phase. Most of the data is
9542 * already set up in LISTVIEW_NCCreate
9544 * PARAMETER(S):
9545 * [I] hwnd : window handle
9546 * [I] lpcs : the create parameters
9548 * RETURN:
9549 * Success: 0
9550 * Failure: -1
9552 static LRESULT LISTVIEW_Create(HWND hwnd, const CREATESTRUCTW *lpcs)
9554 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)GetWindowLongPtrW(hwnd, 0);
9556 TRACE("(lpcs=%p, style=0x%08x)\n", lpcs, lpcs->style);
9558 infoPtr->dwStyle = lpcs->style;
9559 map_style_view(infoPtr);
9561 infoPtr->notifyFormat = SendMessageW(infoPtr->hwndNotify, WM_NOTIFYFORMAT,
9562 (WPARAM)infoPtr->hwndSelf, NF_QUERY);
9563 /* on error defaulting to ANSI notifications */
9564 if (infoPtr->notifyFormat == 0) infoPtr->notifyFormat = NFR_ANSI;
9565 TRACE("notify format=%d\n", infoPtr->notifyFormat);
9567 if ((infoPtr->uView == LV_VIEW_DETAILS) && (lpcs->style & WS_VISIBLE))
9569 if (LISTVIEW_CreateHeader(infoPtr) < 0) return -1;
9571 else
9572 infoPtr->hwndHeader = 0;
9574 /* init item size to avoid division by 0 */
9575 LISTVIEW_UpdateItemSize (infoPtr);
9576 LISTVIEW_UpdateSize (infoPtr);
9578 if (infoPtr->uView == LV_VIEW_DETAILS)
9580 if (!(LVS_NOCOLUMNHEADER & lpcs->style) && (WS_VISIBLE & lpcs->style))
9582 ShowWindow(infoPtr->hwndHeader, SW_SHOWNORMAL);
9584 LISTVIEW_UpdateScroll(infoPtr);
9585 /* send WM_MEASUREITEM notification */
9586 if (infoPtr->dwStyle & LVS_OWNERDRAWFIXED) notify_measureitem(infoPtr);
9589 OpenThemeData(hwnd, themeClass);
9591 /* initialize the icon sizes */
9592 set_icon_size(&infoPtr->iconSize, infoPtr->himlNormal, infoPtr->uView != LV_VIEW_ICON);
9593 set_icon_size(&infoPtr->iconStateSize, infoPtr->himlState, TRUE);
9594 return 0;
9597 /***
9598 * DESCRIPTION:
9599 * Destroys the listview control.
9601 * PARAMETER(S):
9602 * [I] infoPtr : valid pointer to the listview structure
9604 * RETURN:
9605 * Success: 0
9606 * Failure: -1
9608 static LRESULT LISTVIEW_Destroy(LISTVIEW_INFO *infoPtr)
9610 HTHEME theme = GetWindowTheme(infoPtr->hwndSelf);
9611 CloseThemeData(theme);
9613 /* delete all items */
9614 LISTVIEW_DeleteAllItems(infoPtr, TRUE);
9616 return 0;
9619 /***
9620 * DESCRIPTION:
9621 * Enables the listview control.
9623 * PARAMETER(S):
9624 * [I] infoPtr : valid pointer to the listview structure
9625 * [I] bEnable : specifies whether to enable or disable the window
9627 * RETURN:
9628 * SUCCESS : TRUE
9629 * FAILURE : FALSE
9631 static BOOL LISTVIEW_Enable(const LISTVIEW_INFO *infoPtr)
9633 if (infoPtr->dwStyle & LVS_OWNERDRAWFIXED)
9634 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
9635 return TRUE;
9638 /***
9639 * DESCRIPTION:
9640 * Erases the background of the listview control.
9642 * PARAMETER(S):
9643 * [I] infoPtr : valid pointer to the listview structure
9644 * [I] hdc : device context handle
9646 * RETURN:
9647 * SUCCESS : TRUE
9648 * FAILURE : FALSE
9650 static inline BOOL LISTVIEW_EraseBkgnd(const LISTVIEW_INFO *infoPtr, HDC hdc)
9652 RECT rc;
9654 TRACE("(hdc=%p)\n", hdc);
9656 if (!GetClipBox(hdc, &rc)) return FALSE;
9658 if (infoPtr->clrBk == CLR_NONE)
9660 if (infoPtr->dwLvExStyle & LVS_EX_TRANSPARENTBKGND)
9661 return SendMessageW(infoPtr->hwndNotify, WM_PRINTCLIENT,
9662 (WPARAM)hdc, PRF_ERASEBKGND);
9663 else
9664 return SendMessageW(infoPtr->hwndNotify, WM_ERASEBKGND, (WPARAM)hdc, 0);
9667 /* for double buffered controls we need to do this during refresh */
9668 if (infoPtr->dwLvExStyle & LVS_EX_DOUBLEBUFFER) return FALSE;
9670 return LISTVIEW_FillBkgnd(infoPtr, hdc, &rc);
9674 /***
9675 * DESCRIPTION:
9676 * Helper function for LISTVIEW_[HV]Scroll *only*.
9677 * Performs vertical/horizontal scrolling by a give amount.
9679 * PARAMETER(S):
9680 * [I] infoPtr : valid pointer to the listview structure
9681 * [I] dx : amount of horizontal scroll
9682 * [I] dy : amount of vertical scroll
9684 static void scroll_list(LISTVIEW_INFO *infoPtr, INT dx, INT dy)
9686 /* now we can scroll the list */
9687 ScrollWindowEx(infoPtr->hwndSelf, dx, dy, &infoPtr->rcList,
9688 &infoPtr->rcList, 0, 0, SW_ERASE | SW_INVALIDATE);
9689 /* if we have focus, adjust rect */
9690 OffsetRect(&infoPtr->rcFocus, dx, dy);
9691 UpdateWindow(infoPtr->hwndSelf);
9694 /***
9695 * DESCRIPTION:
9696 * Performs vertical scrolling.
9698 * PARAMETER(S):
9699 * [I] infoPtr : valid pointer to the listview structure
9700 * [I] nScrollCode : scroll code
9701 * [I] nScrollDiff : units to scroll in SB_INTERNAL mode, 0 otherwise
9702 * [I] hScrollWnd : scrollbar control window handle
9704 * RETURN:
9705 * Zero
9707 * NOTES:
9708 * SB_LINEUP/SB_LINEDOWN:
9709 * for LVS_ICON, LVS_SMALLICON is 37 by experiment
9710 * for LVS_REPORT is 1 line
9711 * for LVS_LIST cannot occur
9714 static LRESULT LISTVIEW_VScroll(LISTVIEW_INFO *infoPtr, INT nScrollCode,
9715 INT nScrollDiff)
9717 INT nOldScrollPos, nNewScrollPos;
9718 SCROLLINFO scrollInfo;
9719 BOOL is_an_icon;
9721 TRACE("(nScrollCode=%d(%s), nScrollDiff=%d)\n", nScrollCode,
9722 debugscrollcode(nScrollCode), nScrollDiff);
9724 if (infoPtr->hwndEdit) SendMessageW(infoPtr->hwndEdit, WM_KILLFOCUS, 0, 0);
9726 scrollInfo.cbSize = sizeof(SCROLLINFO);
9727 scrollInfo.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_TRACKPOS;
9729 is_an_icon = ((infoPtr->uView == LV_VIEW_ICON) || (infoPtr->uView == LV_VIEW_SMALLICON));
9731 if (!GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo)) return 1;
9733 nOldScrollPos = scrollInfo.nPos;
9734 switch (nScrollCode)
9736 case SB_INTERNAL:
9737 break;
9739 case SB_LINEUP:
9740 nScrollDiff = (is_an_icon) ? -LISTVIEW_SCROLL_ICON_LINE_SIZE : -1;
9741 break;
9743 case SB_LINEDOWN:
9744 nScrollDiff = (is_an_icon) ? LISTVIEW_SCROLL_ICON_LINE_SIZE : 1;
9745 break;
9747 case SB_PAGEUP:
9748 nScrollDiff = -scrollInfo.nPage;
9749 break;
9751 case SB_PAGEDOWN:
9752 nScrollDiff = scrollInfo.nPage;
9753 break;
9755 case SB_THUMBPOSITION:
9756 case SB_THUMBTRACK:
9757 nScrollDiff = scrollInfo.nTrackPos - scrollInfo.nPos;
9758 break;
9760 default:
9761 nScrollDiff = 0;
9764 /* quit right away if pos isn't changing */
9765 if (nScrollDiff == 0) return 0;
9767 /* calculate new position, and handle overflows */
9768 nNewScrollPos = scrollInfo.nPos + nScrollDiff;
9769 if (nScrollDiff > 0) {
9770 if (nNewScrollPos < nOldScrollPos ||
9771 nNewScrollPos > scrollInfo.nMax)
9772 nNewScrollPos = scrollInfo.nMax;
9773 } else {
9774 if (nNewScrollPos > nOldScrollPos ||
9775 nNewScrollPos < scrollInfo.nMin)
9776 nNewScrollPos = scrollInfo.nMin;
9779 /* set the new position, and reread in case it changed */
9780 scrollInfo.fMask = SIF_POS;
9781 scrollInfo.nPos = nNewScrollPos;
9782 nNewScrollPos = SetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo, TRUE);
9784 /* carry on only if it really changed */
9785 if (nNewScrollPos == nOldScrollPos) return 0;
9787 /* now adjust to client coordinates */
9788 nScrollDiff = nOldScrollPos - nNewScrollPos;
9789 if (infoPtr->uView == LV_VIEW_DETAILS) nScrollDiff *= infoPtr->nItemHeight;
9791 /* and scroll the window */
9792 scroll_list(infoPtr, 0, nScrollDiff);
9794 return 0;
9797 /***
9798 * DESCRIPTION:
9799 * Performs horizontal scrolling.
9801 * PARAMETER(S):
9802 * [I] infoPtr : valid pointer to the listview structure
9803 * [I] nScrollCode : scroll code
9804 * [I] nScrollDiff : units to scroll in SB_INTERNAL mode, 0 otherwise
9805 * [I] hScrollWnd : scrollbar control window handle
9807 * RETURN:
9808 * Zero
9810 * NOTES:
9811 * SB_LINELEFT/SB_LINERIGHT:
9812 * for LVS_ICON, LVS_SMALLICON 1 pixel
9813 * for LVS_REPORT is 1 pixel
9814 * for LVS_LIST is 1 column --> which is a 1 because the
9815 * scroll is based on columns not pixels
9818 static LRESULT LISTVIEW_HScroll(LISTVIEW_INFO *infoPtr, INT nScrollCode,
9819 INT nScrollDiff)
9821 INT nOldScrollPos, nNewScrollPos;
9822 SCROLLINFO scrollInfo;
9823 BOOL is_an_icon;
9825 TRACE("(nScrollCode=%d(%s), nScrollDiff=%d)\n", nScrollCode,
9826 debugscrollcode(nScrollCode), nScrollDiff);
9828 if (infoPtr->hwndEdit) SendMessageW(infoPtr->hwndEdit, WM_KILLFOCUS, 0, 0);
9830 scrollInfo.cbSize = sizeof(SCROLLINFO);
9831 scrollInfo.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_TRACKPOS;
9833 is_an_icon = ((infoPtr->uView == LV_VIEW_ICON) || (infoPtr->uView == LV_VIEW_SMALLICON));
9835 if (!GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo)) return 1;
9837 nOldScrollPos = scrollInfo.nPos;
9839 switch (nScrollCode)
9841 case SB_INTERNAL:
9842 break;
9844 case SB_LINELEFT:
9845 nScrollDiff = (is_an_icon) ? -LISTVIEW_SCROLL_ICON_LINE_SIZE : -1;
9846 break;
9848 case SB_LINERIGHT:
9849 nScrollDiff = (is_an_icon) ? LISTVIEW_SCROLL_ICON_LINE_SIZE : 1;
9850 break;
9852 case SB_PAGELEFT:
9853 nScrollDiff = -scrollInfo.nPage;
9854 break;
9856 case SB_PAGERIGHT:
9857 nScrollDiff = scrollInfo.nPage;
9858 break;
9860 case SB_THUMBPOSITION:
9861 case SB_THUMBTRACK:
9862 nScrollDiff = scrollInfo.nTrackPos - scrollInfo.nPos;
9863 break;
9865 default:
9866 nScrollDiff = 0;
9869 /* quit right away if pos isn't changing */
9870 if (nScrollDiff == 0) return 0;
9872 /* calculate new position, and handle overflows */
9873 nNewScrollPos = scrollInfo.nPos + nScrollDiff;
9874 if (nScrollDiff > 0) {
9875 if (nNewScrollPos < nOldScrollPos ||
9876 nNewScrollPos > scrollInfo.nMax)
9877 nNewScrollPos = scrollInfo.nMax;
9878 } else {
9879 if (nNewScrollPos > nOldScrollPos ||
9880 nNewScrollPos < scrollInfo.nMin)
9881 nNewScrollPos = scrollInfo.nMin;
9884 /* set the new position, and reread in case it changed */
9885 scrollInfo.fMask = SIF_POS;
9886 scrollInfo.nPos = nNewScrollPos;
9887 nNewScrollPos = SetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo, TRUE);
9889 /* carry on only if it really changed */
9890 if (nNewScrollPos == nOldScrollPos) return 0;
9892 LISTVIEW_UpdateHeaderSize(infoPtr, nNewScrollPos);
9894 /* now adjust to client coordinates */
9895 nScrollDiff = nOldScrollPos - nNewScrollPos;
9896 if (infoPtr->uView == LV_VIEW_LIST) nScrollDiff *= infoPtr->nItemWidth;
9898 /* and scroll the window */
9899 scroll_list(infoPtr, nScrollDiff, 0);
9901 return 0;
9904 static LRESULT LISTVIEW_MouseWheel(LISTVIEW_INFO *infoPtr, INT wheelDelta)
9906 INT pulScrollLines = 3;
9908 TRACE("(wheelDelta=%d)\n", wheelDelta);
9910 switch(infoPtr->uView)
9912 case LV_VIEW_ICON:
9913 case LV_VIEW_SMALLICON:
9915 * listview should be scrolled by a multiple of 37 dependently on its dimension or its visible item number
9916 * should be fixed in the future.
9918 LISTVIEW_VScroll(infoPtr, SB_INTERNAL, (wheelDelta > 0) ?
9919 -LISTVIEW_SCROLL_ICON_LINE_SIZE : LISTVIEW_SCROLL_ICON_LINE_SIZE);
9920 break;
9922 case LV_VIEW_DETAILS:
9923 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES,0, &pulScrollLines, 0);
9925 /* if scrolling changes direction, ignore left overs */
9926 if ((wheelDelta < 0 && infoPtr->cWheelRemainder < 0) ||
9927 (wheelDelta > 0 && infoPtr->cWheelRemainder > 0))
9928 infoPtr->cWheelRemainder += wheelDelta;
9929 else
9930 infoPtr->cWheelRemainder = wheelDelta;
9931 if (infoPtr->cWheelRemainder && pulScrollLines)
9933 int cLineScroll;
9934 pulScrollLines = min((UINT)LISTVIEW_GetCountPerColumn(infoPtr), pulScrollLines);
9935 cLineScroll = pulScrollLines * infoPtr->cWheelRemainder / WHEEL_DELTA;
9936 infoPtr->cWheelRemainder -= WHEEL_DELTA * cLineScroll / pulScrollLines;
9937 LISTVIEW_VScroll(infoPtr, SB_INTERNAL, -cLineScroll);
9939 break;
9941 case LV_VIEW_LIST:
9942 LISTVIEW_HScroll(infoPtr, (wheelDelta > 0) ? SB_LINELEFT : SB_LINERIGHT, 0);
9943 break;
9945 return 0;
9948 /***
9949 * DESCRIPTION:
9950 * ???
9952 * PARAMETER(S):
9953 * [I] infoPtr : valid pointer to the listview structure
9954 * [I] nVirtualKey : virtual key
9955 * [I] lKeyData : key data
9957 * RETURN:
9958 * Zero
9960 static LRESULT LISTVIEW_KeyDown(LISTVIEW_INFO *infoPtr, INT nVirtualKey, LONG lKeyData)
9962 HWND hwndSelf = infoPtr->hwndSelf;
9963 INT nItem = -1;
9964 NMLVKEYDOWN nmKeyDown;
9966 TRACE("(nVirtualKey=%d, lKeyData=%d)\n", nVirtualKey, lKeyData);
9968 /* send LVN_KEYDOWN notification */
9969 nmKeyDown.wVKey = nVirtualKey;
9970 nmKeyDown.flags = 0;
9971 notify_hdr(infoPtr, LVN_KEYDOWN, &nmKeyDown.hdr);
9972 if (!IsWindow(hwndSelf))
9973 return 0;
9975 switch (nVirtualKey)
9977 case VK_SPACE:
9978 nItem = infoPtr->nFocusedItem;
9979 if (infoPtr->dwLvExStyle & LVS_EX_CHECKBOXES)
9980 toggle_checkbox_state(infoPtr, infoPtr->nFocusedItem);
9981 break;
9983 case VK_RETURN:
9984 if ((infoPtr->nItemCount > 0) && (infoPtr->nFocusedItem != -1))
9986 if (!notify(infoPtr, NM_RETURN)) return 0;
9987 if (!notify(infoPtr, LVN_ITEMACTIVATE)) return 0;
9989 break;
9991 case VK_HOME:
9992 if (infoPtr->nItemCount > 0)
9993 nItem = 0;
9994 break;
9996 case VK_END:
9997 if (infoPtr->nItemCount > 0)
9998 nItem = infoPtr->nItemCount - 1;
9999 break;
10001 case VK_LEFT:
10002 nItem = LISTVIEW_GetNextItem(infoPtr, infoPtr->nFocusedItem, LVNI_TOLEFT);
10003 break;
10005 case VK_UP:
10006 nItem = LISTVIEW_GetNextItem(infoPtr, infoPtr->nFocusedItem, LVNI_ABOVE);
10007 break;
10009 case VK_RIGHT:
10010 nItem = LISTVIEW_GetNextItem(infoPtr, infoPtr->nFocusedItem, LVNI_TORIGHT);
10011 break;
10013 case VK_DOWN:
10014 nItem = LISTVIEW_GetNextItem(infoPtr, infoPtr->nFocusedItem, LVNI_BELOW);
10015 break;
10017 case VK_PRIOR:
10018 if (infoPtr->uView == LV_VIEW_DETAILS)
10020 INT topidx = LISTVIEW_GetTopIndex(infoPtr);
10021 if (infoPtr->nFocusedItem == topidx)
10022 nItem = topidx - LISTVIEW_GetCountPerColumn(infoPtr) + 1;
10023 else
10024 nItem = topidx;
10026 else
10027 nItem = infoPtr->nFocusedItem - LISTVIEW_GetCountPerColumn(infoPtr)
10028 * LISTVIEW_GetCountPerRow(infoPtr);
10029 if(nItem < 0) nItem = 0;
10030 break;
10032 case VK_NEXT:
10033 if (infoPtr->uView == LV_VIEW_DETAILS)
10035 INT topidx = LISTVIEW_GetTopIndex(infoPtr);
10036 INT cnt = LISTVIEW_GetCountPerColumn(infoPtr);
10037 if (infoPtr->nFocusedItem == topidx + cnt - 1)
10038 nItem = infoPtr->nFocusedItem + cnt - 1;
10039 else
10040 nItem = topidx + cnt - 1;
10042 else
10043 nItem = infoPtr->nFocusedItem + LISTVIEW_GetCountPerColumn(infoPtr)
10044 * LISTVIEW_GetCountPerRow(infoPtr);
10045 if(nItem >= infoPtr->nItemCount) nItem = infoPtr->nItemCount - 1;
10046 break;
10049 if ((nItem != -1) && (nItem != infoPtr->nFocusedItem || nVirtualKey == VK_SPACE))
10050 LISTVIEW_KeySelection(infoPtr, nItem, nVirtualKey == VK_SPACE);
10052 return 0;
10055 /***
10056 * DESCRIPTION:
10057 * Kills the focus.
10059 * PARAMETER(S):
10060 * [I] infoPtr : valid pointer to the listview structure
10062 * RETURN:
10063 * Zero
10065 static LRESULT LISTVIEW_KillFocus(LISTVIEW_INFO *infoPtr)
10067 TRACE("()\n");
10069 /* drop any left over scroll amount */
10070 infoPtr->cWheelRemainder = 0;
10072 /* if we did not have the focus, there's nothing more to do */
10073 if (!infoPtr->bFocus) return 0;
10075 /* send NM_KILLFOCUS notification */
10076 if (!notify(infoPtr, NM_KILLFOCUS)) return 0;
10078 /* if we have a focus rectangle, get rid of it */
10079 LISTVIEW_ShowFocusRect(infoPtr, FALSE);
10081 /* if have a marquee selection, stop it */
10082 if (infoPtr->bMarqueeSelect)
10084 /* Remove the marquee rectangle and release our mouse capture */
10085 LISTVIEW_InvalidateRect(infoPtr, &infoPtr->marqueeRect);
10086 ReleaseCapture();
10088 SetRectEmpty(&infoPtr->marqueeRect);
10090 infoPtr->bMarqueeSelect = FALSE;
10091 infoPtr->bScrolling = FALSE;
10092 KillTimer(infoPtr->hwndSelf, (UINT_PTR) infoPtr);
10095 /* set window focus flag */
10096 infoPtr->bFocus = FALSE;
10098 /* invalidate the selected items before resetting focus flag */
10099 LISTVIEW_InvalidateSelectedItems(infoPtr);
10101 return 0;
10104 /***
10105 * DESCRIPTION:
10106 * Processes double click messages (left mouse button).
10108 * PARAMETER(S):
10109 * [I] infoPtr : valid pointer to the listview structure
10110 * [I] wKey : key flag
10111 * [I] x,y : mouse coordinate
10113 * RETURN:
10114 * Zero
10116 static LRESULT LISTVIEW_LButtonDblClk(LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
10118 LVHITTESTINFO htInfo;
10120 TRACE("(key=%hu, X=%u, Y=%u)\n", wKey, x, y);
10122 /* Cancel the item edition if any */
10123 if (infoPtr->itemEdit.fEnabled)
10125 KillTimer(infoPtr->hwndSelf, (UINT_PTR)&infoPtr->itemEdit);
10126 infoPtr->itemEdit.fEnabled = FALSE;
10129 /* send NM_RELEASEDCAPTURE notification */
10130 if (!notify(infoPtr, NM_RELEASEDCAPTURE)) return 0;
10132 htInfo.pt.x = x;
10133 htInfo.pt.y = y;
10135 /* send NM_DBLCLK notification */
10136 LISTVIEW_HitTest(infoPtr, &htInfo, TRUE, FALSE);
10137 if (!notify_click(infoPtr, NM_DBLCLK, &htInfo)) return 0;
10139 /* To send the LVN_ITEMACTIVATE, it must be on an Item */
10140 if(htInfo.iItem != -1) notify_itemactivate(infoPtr,&htInfo);
10142 return 0;
10145 static LRESULT LISTVIEW_TrackMouse(const LISTVIEW_INFO *infoPtr, POINT pt)
10147 MSG msg;
10148 RECT r;
10150 r.top = r.bottom = pt.y;
10151 r.left = r.right = pt.x;
10153 InflateRect(&r, GetSystemMetrics(SM_CXDRAG), GetSystemMetrics(SM_CYDRAG));
10155 SetCapture(infoPtr->hwndSelf);
10157 while (1)
10159 if (PeekMessageW(&msg, 0, 0, 0, PM_REMOVE | PM_NOYIELD))
10161 if (msg.message == WM_MOUSEMOVE)
10163 pt.x = (short)LOWORD(msg.lParam);
10164 pt.y = (short)HIWORD(msg.lParam);
10165 if (PtInRect(&r, pt))
10166 continue;
10167 else
10169 ReleaseCapture();
10170 return 1;
10173 else if (msg.message >= WM_LBUTTONDOWN &&
10174 msg.message <= WM_RBUTTONDBLCLK)
10176 break;
10179 DispatchMessageW(&msg);
10182 if (GetCapture() != infoPtr->hwndSelf)
10183 return 0;
10186 ReleaseCapture();
10187 return 0;
10191 /***
10192 * DESCRIPTION:
10193 * Processes mouse down messages (left mouse button).
10195 * PARAMETERS:
10196 * infoPtr [I ] valid pointer to the listview structure
10197 * wKey [I ] key flag
10198 * x,y [I ] mouse coordinate
10200 * RETURN:
10201 * Zero
10203 static LRESULT LISTVIEW_LButtonDown(LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
10205 LVHITTESTINFO lvHitTestInfo;
10206 static BOOL bGroupSelect = TRUE;
10207 POINT pt = { x, y };
10208 INT nItem;
10210 TRACE("(key=%hu, X=%u, Y=%u)\n", wKey, x, y);
10212 /* send NM_RELEASEDCAPTURE notification */
10213 if (!notify(infoPtr, NM_RELEASEDCAPTURE)) return 0;
10215 /* set left button down flag and record the click position */
10216 infoPtr->bLButtonDown = TRUE;
10217 infoPtr->ptClickPos = pt;
10218 infoPtr->bDragging = FALSE;
10219 infoPtr->bMarqueeSelect = FALSE;
10220 infoPtr->bScrolling = FALSE;
10222 lvHitTestInfo.pt.x = x;
10223 lvHitTestInfo.pt.y = y;
10225 nItem = LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, TRUE);
10226 TRACE("at %s, nItem=%d\n", wine_dbgstr_point(&pt), nItem);
10227 if ((nItem >= 0) && (nItem < infoPtr->nItemCount))
10229 if ((infoPtr->dwLvExStyle & LVS_EX_CHECKBOXES) && (lvHitTestInfo.flags & LVHT_ONITEMSTATEICON))
10231 notify_click(infoPtr, NM_CLICK, &lvHitTestInfo);
10232 toggle_checkbox_state(infoPtr, nItem);
10233 infoPtr->bLButtonDown = FALSE;
10234 return 0;
10237 if (infoPtr->dwStyle & LVS_SINGLESEL)
10239 if (LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED))
10240 infoPtr->nEditLabelItem = nItem;
10241 else
10242 LISTVIEW_SetSelection(infoPtr, nItem);
10244 else
10246 if ((wKey & MK_CONTROL) && (wKey & MK_SHIFT))
10248 if (bGroupSelect)
10250 if (!LISTVIEW_AddGroupSelection(infoPtr, nItem)) return 0;
10251 LISTVIEW_SetItemFocus(infoPtr, nItem);
10252 infoPtr->nSelectionMark = nItem;
10254 else
10256 LVITEMW item;
10258 item.state = LVIS_SELECTED | LVIS_FOCUSED;
10259 item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
10261 LISTVIEW_SetItemState(infoPtr,nItem,&item);
10262 infoPtr->nSelectionMark = nItem;
10265 else if (wKey & MK_CONTROL)
10267 LVITEMW item;
10269 bGroupSelect = (LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED) == 0);
10271 item.state = (bGroupSelect ? LVIS_SELECTED : 0) | LVIS_FOCUSED;
10272 item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
10273 LISTVIEW_SetItemState(infoPtr, nItem, &item);
10274 infoPtr->nSelectionMark = nItem;
10276 else if (wKey & MK_SHIFT)
10278 LISTVIEW_SetGroupSelection(infoPtr, nItem);
10280 else
10282 if (LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED))
10284 infoPtr->nEditLabelItem = nItem;
10285 infoPtr->nLButtonDownItem = nItem;
10287 LISTVIEW_SetItemFocus(infoPtr, nItem);
10289 else
10290 /* set selection (clears other pre-existing selections) */
10291 LISTVIEW_SetSelection(infoPtr, nItem);
10295 if (!infoPtr->bFocus)
10296 SetFocus(infoPtr->hwndSelf);
10298 if (infoPtr->dwLvExStyle & LVS_EX_ONECLICKACTIVATE)
10299 if(lvHitTestInfo.iItem != -1) notify_itemactivate(infoPtr,&lvHitTestInfo);
10301 else
10303 if (!infoPtr->bFocus)
10304 SetFocus(infoPtr->hwndSelf);
10306 /* remove all selections */
10307 if (!(wKey & MK_CONTROL) && !(wKey & MK_SHIFT))
10308 LISTVIEW_DeselectAll(infoPtr);
10309 ReleaseCapture();
10312 return 0;
10315 /***
10316 * DESCRIPTION:
10317 * Processes mouse up messages (left mouse button).
10319 * PARAMETERS:
10320 * infoPtr [I ] valid pointer to the listview structure
10321 * wKey [I ] key flag
10322 * x,y [I ] mouse coordinate
10324 * RETURN:
10325 * Zero
10327 static LRESULT LISTVIEW_LButtonUp(LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
10329 LVHITTESTINFO lvHitTestInfo;
10331 TRACE("(key=%hu, X=%u, Y=%u)\n", wKey, x, y);
10333 if (!infoPtr->bLButtonDown) return 0;
10335 lvHitTestInfo.pt.x = x;
10336 lvHitTestInfo.pt.y = y;
10338 /* send NM_CLICK notification */
10339 LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, FALSE);
10340 if (!notify_click(infoPtr, NM_CLICK, &lvHitTestInfo)) return 0;
10342 /* set left button flag */
10343 infoPtr->bLButtonDown = FALSE;
10345 /* set a single selection, reset others */
10346 if(lvHitTestInfo.iItem == infoPtr->nLButtonDownItem && lvHitTestInfo.iItem != -1)
10347 LISTVIEW_SetSelection(infoPtr, infoPtr->nLButtonDownItem);
10348 infoPtr->nLButtonDownItem = -1;
10350 if (infoPtr->bDragging || infoPtr->bMarqueeSelect)
10352 /* Remove the marquee rectangle and release our mouse capture */
10353 if (infoPtr->bMarqueeSelect)
10355 LISTVIEW_InvalidateRect(infoPtr, &infoPtr->marqueeDrawRect);
10356 ReleaseCapture();
10359 SetRectEmpty(&infoPtr->marqueeRect);
10360 SetRectEmpty(&infoPtr->marqueeDrawRect);
10362 infoPtr->bDragging = FALSE;
10363 infoPtr->bMarqueeSelect = FALSE;
10364 infoPtr->bScrolling = FALSE;
10366 KillTimer(infoPtr->hwndSelf, (UINT_PTR) infoPtr);
10367 return 0;
10370 /* if we clicked on a selected item, edit the label */
10371 if(lvHitTestInfo.iItem == infoPtr->nEditLabelItem && (lvHitTestInfo.flags & LVHT_ONITEMLABEL))
10373 /* we want to make sure the user doesn't want to do a double click. So we will
10374 * delay the edit. WM_LBUTTONDBLCLICK will cancel the timer
10376 infoPtr->itemEdit.fEnabled = TRUE;
10377 infoPtr->itemEdit.iItem = lvHitTestInfo.iItem;
10378 SetTimer(infoPtr->hwndSelf,
10379 (UINT_PTR)&infoPtr->itemEdit,
10380 GetDoubleClickTime(),
10381 LISTVIEW_DelayedEditItem);
10384 return 0;
10387 /***
10388 * DESCRIPTION:
10389 * Destroys the listview control (called after WM_DESTROY).
10391 * PARAMETER(S):
10392 * [I] infoPtr : valid pointer to the listview structure
10394 * RETURN:
10395 * Zero
10397 static LRESULT LISTVIEW_NCDestroy(LISTVIEW_INFO *infoPtr)
10399 INT i;
10401 TRACE("()\n");
10403 /* destroy data structure */
10404 DPA_Destroy(infoPtr->hdpaItems);
10405 DPA_Destroy(infoPtr->hdpaItemIds);
10406 DPA_Destroy(infoPtr->hdpaPosX);
10407 DPA_Destroy(infoPtr->hdpaPosY);
10408 /* columns */
10409 for (i = 0; i < DPA_GetPtrCount(infoPtr->hdpaColumns); i++)
10410 Free(DPA_GetPtr(infoPtr->hdpaColumns, i));
10411 DPA_Destroy(infoPtr->hdpaColumns);
10412 ranges_destroy(infoPtr->selectionRanges);
10414 /* destroy image lists */
10415 if (!(infoPtr->dwStyle & LVS_SHAREIMAGELISTS))
10417 ImageList_Destroy(infoPtr->himlNormal);
10418 ImageList_Destroy(infoPtr->himlSmall);
10419 ImageList_Destroy(infoPtr->himlState);
10422 /* destroy font, bkgnd brush */
10423 infoPtr->hFont = 0;
10424 if (infoPtr->hDefaultFont) DeleteObject(infoPtr->hDefaultFont);
10425 if (infoPtr->clrBk != CLR_NONE) DeleteObject(infoPtr->hBkBrush);
10427 SetWindowLongPtrW(infoPtr->hwndSelf, 0, 0);
10429 /* free listview info pointer*/
10430 Free(infoPtr);
10432 return 0;
10435 /***
10436 * DESCRIPTION:
10437 * Handles notifications.
10439 * PARAMETER(S):
10440 * [I] infoPtr : valid pointer to the listview structure
10441 * [I] lpnmhdr : notification information
10443 * RETURN:
10444 * Zero
10446 static LRESULT LISTVIEW_Notify(LISTVIEW_INFO *infoPtr, NMHDR *lpnmhdr)
10448 NMHEADERW *lpnmh;
10450 TRACE("(lpnmhdr=%p)\n", lpnmhdr);
10452 if (!lpnmhdr || lpnmhdr->hwndFrom != infoPtr->hwndHeader) return 0;
10454 /* remember: HDN_LAST < HDN_FIRST */
10455 if (lpnmhdr->code > HDN_FIRST || lpnmhdr->code < HDN_LAST) return 0;
10456 lpnmh = (NMHEADERW *)lpnmhdr;
10458 if (lpnmh->iItem < 0 || lpnmh->iItem >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return 0;
10460 switch (lpnmhdr->code)
10462 case HDN_TRACKW:
10463 case HDN_TRACKA:
10465 COLUMN_INFO *lpColumnInfo;
10466 POINT ptOrigin;
10467 INT x;
10469 if (!lpnmh->pitem || !(lpnmh->pitem->mask & HDI_WIDTH))
10470 break;
10472 /* remove the old line (if any) */
10473 LISTVIEW_DrawTrackLine(infoPtr);
10475 /* compute & draw the new line */
10476 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, lpnmh->iItem);
10477 x = lpColumnInfo->rcHeader.left + lpnmh->pitem->cxy;
10478 LISTVIEW_GetOrigin(infoPtr, &ptOrigin);
10479 infoPtr->xTrackLine = x + ptOrigin.x;
10480 LISTVIEW_DrawTrackLine(infoPtr);
10481 return notify_forward_header(infoPtr, lpnmh);
10484 case HDN_ENDTRACKA:
10485 case HDN_ENDTRACKW:
10486 /* remove the track line (if any) */
10487 LISTVIEW_DrawTrackLine(infoPtr);
10488 infoPtr->xTrackLine = -1;
10489 return notify_forward_header(infoPtr, lpnmh);
10491 case HDN_BEGINDRAG:
10492 if ((infoPtr->dwLvExStyle & LVS_EX_HEADERDRAGDROP) == 0) return 1;
10493 return notify_forward_header(infoPtr, lpnmh);
10495 case HDN_ENDDRAG:
10496 infoPtr->colRectsDirty = TRUE;
10497 LISTVIEW_InvalidateList(infoPtr);
10498 return notify_forward_header(infoPtr, lpnmh);
10500 case HDN_ITEMCHANGEDW:
10501 case HDN_ITEMCHANGEDA:
10503 COLUMN_INFO *lpColumnInfo;
10504 HDITEMW hdi;
10505 INT dx, cxy;
10507 if (!lpnmh->pitem || !(lpnmh->pitem->mask & HDI_WIDTH))
10509 hdi.mask = HDI_WIDTH;
10510 if (!SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, lpnmh->iItem, (LPARAM)&hdi)) return 0;
10511 cxy = hdi.cxy;
10513 else
10514 cxy = lpnmh->pitem->cxy;
10516 /* determine how much we change since the last know position */
10517 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, lpnmh->iItem);
10518 dx = cxy - (lpColumnInfo->rcHeader.right - lpColumnInfo->rcHeader.left);
10519 if (dx != 0)
10521 lpColumnInfo->rcHeader.right += dx;
10523 hdi.mask = HDI_ORDER;
10524 SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, lpnmh->iItem, (LPARAM)&hdi);
10526 /* not the rightmost one */
10527 if (hdi.iOrder + 1 < DPA_GetPtrCount(infoPtr->hdpaColumns))
10529 INT nIndex = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX,
10530 hdi.iOrder + 1, 0);
10531 LISTVIEW_ScrollColumns(infoPtr, nIndex, dx);
10533 else
10535 /* only needs to update the scrolls */
10536 infoPtr->nItemWidth += dx;
10537 LISTVIEW_UpdateScroll(infoPtr);
10539 LISTVIEW_UpdateItemSize(infoPtr);
10540 if (infoPtr->uView == LV_VIEW_DETAILS && is_redrawing(infoPtr))
10542 POINT ptOrigin;
10543 RECT rcCol = lpColumnInfo->rcHeader;
10545 LISTVIEW_GetOrigin(infoPtr, &ptOrigin);
10546 OffsetRect(&rcCol, ptOrigin.x, 0);
10548 rcCol.top = infoPtr->rcList.top;
10549 rcCol.bottom = infoPtr->rcList.bottom;
10551 /* resizing left-aligned columns leaves most of the left side untouched */
10552 if ((lpColumnInfo->fmt & LVCFMT_JUSTIFYMASK) == LVCFMT_LEFT)
10554 INT nMaxDirty = infoPtr->nEllipsisWidth + infoPtr->ntmMaxCharWidth;
10555 if (dx > 0)
10556 nMaxDirty += dx;
10557 rcCol.left = max (rcCol.left, rcCol.right - nMaxDirty);
10560 /* when shrinking the last column clear the now unused field */
10561 if (hdi.iOrder == DPA_GetPtrCount(infoPtr->hdpaColumns) - 1)
10563 RECT right;
10565 rcCol.right -= dx;
10567 /* deal with right from rightmost column area */
10568 right.left = rcCol.right;
10569 right.top = rcCol.top;
10570 right.bottom = rcCol.bottom;
10571 right.right = infoPtr->rcList.right;
10573 LISTVIEW_InvalidateRect(infoPtr, &right);
10576 LISTVIEW_InvalidateRect(infoPtr, &rcCol);
10579 break;
10582 case HDN_ITEMCLICKW:
10583 case HDN_ITEMCLICKA:
10585 /* Handle sorting by Header Column */
10586 NMLISTVIEW nmlv;
10588 ZeroMemory(&nmlv, sizeof(NMLISTVIEW));
10589 nmlv.iItem = -1;
10590 nmlv.iSubItem = lpnmh->iItem;
10591 notify_listview(infoPtr, LVN_COLUMNCLICK, &nmlv);
10592 return notify_forward_header(infoPtr, lpnmh);
10595 case HDN_DIVIDERDBLCLICKW:
10596 case HDN_DIVIDERDBLCLICKA:
10597 /* FIXME: for LVS_EX_HEADERINALLVIEWS and not LV_VIEW_DETAILS
10598 we should use LVSCW_AUTOSIZE_USEHEADER, helper rework or
10599 split needed for that */
10600 LISTVIEW_SetColumnWidth(infoPtr, lpnmh->iItem, LVSCW_AUTOSIZE);
10601 return notify_forward_header(infoPtr, lpnmh);
10603 return 0;
10606 /***
10607 * DESCRIPTION:
10608 * Paint non-client area of control.
10610 * PARAMETER(S):
10611 * [I] infoPtr : valid pointer to the listview structureof the sender
10612 * [I] region : update region
10614 * RETURN:
10615 * TRUE - frame was painted
10616 * FALSE - call default window proc
10618 static BOOL LISTVIEW_NCPaint(const LISTVIEW_INFO *infoPtr, HRGN region)
10620 HTHEME theme = GetWindowTheme (infoPtr->hwndSelf);
10621 HDC dc;
10622 RECT r;
10623 HRGN cliprgn;
10624 int cxEdge = GetSystemMetrics (SM_CXEDGE),
10625 cyEdge = GetSystemMetrics (SM_CYEDGE);
10627 if (!theme)
10628 return DefWindowProcW (infoPtr->hwndSelf, WM_NCPAINT, (WPARAM)region, 0);
10630 GetWindowRect(infoPtr->hwndSelf, &r);
10632 cliprgn = CreateRectRgn (r.left + cxEdge, r.top + cyEdge,
10633 r.right - cxEdge, r.bottom - cyEdge);
10634 if (region != (HRGN)1)
10635 CombineRgn (cliprgn, cliprgn, region, RGN_AND);
10636 OffsetRect(&r, -r.left, -r.top);
10638 dc = GetDCEx(infoPtr->hwndSelf, region, DCX_WINDOW|DCX_INTERSECTRGN);
10639 OffsetRect(&r, -r.left, -r.top);
10641 if (IsThemeBackgroundPartiallyTransparent (theme, 0, 0))
10642 DrawThemeParentBackground(infoPtr->hwndSelf, dc, &r);
10643 DrawThemeBackground (theme, dc, 0, 0, &r, 0);
10644 ReleaseDC(infoPtr->hwndSelf, dc);
10646 /* Call default proc to get the scrollbars etc. painted */
10647 DefWindowProcW (infoPtr->hwndSelf, WM_NCPAINT, (WPARAM)cliprgn, 0);
10649 return FALSE;
10652 /***
10653 * DESCRIPTION:
10654 * Determines the type of structure to use.
10656 * PARAMETER(S):
10657 * [I] infoPtr : valid pointer to the listview structureof the sender
10658 * [I] hwndFrom : listview window handle
10659 * [I] nCommand : command specifying the nature of the WM_NOTIFYFORMAT
10661 * RETURN:
10662 * Zero
10664 static LRESULT LISTVIEW_NotifyFormat(LISTVIEW_INFO *infoPtr, HWND hwndFrom, INT nCommand)
10666 TRACE("(hwndFrom=%p, nCommand=%d)\n", hwndFrom, nCommand);
10668 if (nCommand == NF_REQUERY)
10669 infoPtr->notifyFormat = SendMessageW(infoPtr->hwndNotify, WM_NOTIFYFORMAT, (WPARAM)infoPtr->hwndSelf, NF_QUERY);
10671 return infoPtr->notifyFormat;
10674 /***
10675 * DESCRIPTION:
10676 * Paints/Repaints the listview control. Internal use.
10678 * PARAMETER(S):
10679 * [I] infoPtr : valid pointer to the listview structure
10680 * [I] hdc : device context handle
10682 * RETURN:
10683 * Zero
10685 static LRESULT LISTVIEW_Paint(LISTVIEW_INFO *infoPtr, HDC hdc)
10687 TRACE("(hdc=%p)\n", hdc);
10689 if (infoPtr->bNoItemMetrics && infoPtr->nItemCount)
10691 infoPtr->bNoItemMetrics = FALSE;
10692 LISTVIEW_UpdateItemSize(infoPtr);
10693 if (infoPtr->uView == LV_VIEW_ICON || infoPtr->uView == LV_VIEW_SMALLICON)
10694 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
10695 LISTVIEW_UpdateScroll(infoPtr);
10698 if (infoPtr->hwndHeader) UpdateWindow(infoPtr->hwndHeader);
10700 if (hdc)
10701 LISTVIEW_Refresh(infoPtr, hdc, NULL);
10702 else
10704 PAINTSTRUCT ps;
10706 hdc = BeginPaint(infoPtr->hwndSelf, &ps);
10707 if (!hdc) return 1;
10708 LISTVIEW_Refresh(infoPtr, hdc, ps.fErase ? &ps.rcPaint : NULL);
10709 EndPaint(infoPtr->hwndSelf, &ps);
10712 return 0;
10715 /***
10716 * DESCRIPTION:
10717 * Paints/Repaints the listview control, WM_PAINT handler.
10719 * PARAMETER(S):
10720 * [I] infoPtr : valid pointer to the listview structure
10721 * [I] hdc : device context handle
10723 * RETURN:
10724 * Zero
10726 static inline LRESULT LISTVIEW_WMPaint(LISTVIEW_INFO *infoPtr, HDC hdc)
10728 TRACE("(hdc=%p)\n", hdc);
10730 if (!is_redrawing(infoPtr))
10731 return DefWindowProcW (infoPtr->hwndSelf, WM_PAINT, (WPARAM)hdc, 0);
10733 return LISTVIEW_Paint(infoPtr, hdc);
10736 /***
10737 * DESCRIPTION:
10738 * Paints/Repaints the listview control.
10740 * PARAMETER(S):
10741 * [I] infoPtr : valid pointer to the listview structure
10742 * [I] hdc : device context handle
10743 * [I] options : drawing options
10745 * RETURN:
10746 * Zero
10748 static LRESULT LISTVIEW_PrintClient(LISTVIEW_INFO *infoPtr, HDC hdc, DWORD options)
10750 if ((options & PRF_CHECKVISIBLE) && !IsWindowVisible(infoPtr->hwndSelf))
10751 return 0;
10753 if (options & ~(PRF_ERASEBKGND|PRF_CLIENT))
10754 FIXME("(hdc=%p options=0x%08x) partial stub\n", hdc, options);
10756 if (options & PRF_ERASEBKGND)
10757 LISTVIEW_EraseBkgnd(infoPtr, hdc);
10759 if (options & PRF_CLIENT)
10760 LISTVIEW_Paint(infoPtr, hdc);
10762 return 0;
10766 /***
10767 * DESCRIPTION:
10768 * Processes double click messages (right mouse button).
10770 * PARAMETER(S):
10771 * [I] infoPtr : valid pointer to the listview structure
10772 * [I] wKey : key flag
10773 * [I] x,y : mouse coordinate
10775 * RETURN:
10776 * Zero
10778 static LRESULT LISTVIEW_RButtonDblClk(const LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
10780 LVHITTESTINFO lvHitTestInfo;
10782 TRACE("(key=%hu,X=%u,Y=%u)\n", wKey, x, y);
10784 /* send NM_RELEASEDCAPTURE notification */
10785 if (!notify(infoPtr, NM_RELEASEDCAPTURE)) return 0;
10787 /* send NM_RDBLCLK notification */
10788 lvHitTestInfo.pt.x = x;
10789 lvHitTestInfo.pt.y = y;
10790 LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, FALSE);
10791 notify_click(infoPtr, NM_RDBLCLK, &lvHitTestInfo);
10793 return 0;
10796 /***
10797 * DESCRIPTION:
10798 * Processes WM_RBUTTONDOWN message and corresponding drag operation.
10800 * PARAMETER(S):
10801 * [I] infoPtr : valid pointer to the listview structure
10802 * [I] wKey : key flag
10803 * [I] x, y : mouse coordinate
10805 * RETURN:
10806 * Zero
10808 static LRESULT LISTVIEW_RButtonDown(LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
10810 LVHITTESTINFO ht;
10811 INT item;
10813 TRACE("(key=%hu, x=%d, y=%d)\n", wKey, x, y);
10815 /* send NM_RELEASEDCAPTURE notification */
10816 if (!notify(infoPtr, NM_RELEASEDCAPTURE)) return 0;
10818 /* determine the index of the selected item */
10819 ht.pt.x = x;
10820 ht.pt.y = y;
10821 item = LISTVIEW_HitTest(infoPtr, &ht, TRUE, TRUE);
10823 /* make sure the listview control window has the focus */
10824 if (!infoPtr->bFocus) SetFocus(infoPtr->hwndSelf);
10826 if ((item >= 0) && (item < infoPtr->nItemCount))
10828 LISTVIEW_SetItemFocus(infoPtr, item);
10829 if (!((wKey & MK_SHIFT) || (wKey & MK_CONTROL)) &&
10830 !LISTVIEW_GetItemState(infoPtr, item, LVIS_SELECTED))
10831 LISTVIEW_SetSelection(infoPtr, item);
10833 else
10834 LISTVIEW_DeselectAll(infoPtr);
10836 if (LISTVIEW_TrackMouse(infoPtr, ht.pt))
10838 if (ht.iItem != -1)
10840 NMLISTVIEW nmlv;
10842 memset(&nmlv, 0, sizeof(nmlv));
10843 nmlv.iItem = ht.iItem;
10844 nmlv.ptAction = ht.pt;
10846 notify_listview(infoPtr, LVN_BEGINRDRAG, &nmlv);
10849 else
10851 SetFocus(infoPtr->hwndSelf);
10853 ht.pt.x = x;
10854 ht.pt.y = y;
10855 LISTVIEW_HitTest(infoPtr, &ht, TRUE, FALSE);
10857 if (notify_click(infoPtr, NM_RCLICK, &ht))
10859 /* Send a WM_CONTEXTMENU message in response to the WM_RBUTTONUP */
10860 SendMessageW(infoPtr->hwndSelf, WM_CONTEXTMENU,
10861 (WPARAM)infoPtr->hwndSelf, (LPARAM)GetMessagePos());
10865 return 0;
10868 /***
10869 * DESCRIPTION:
10870 * Sets the cursor.
10872 * PARAMETER(S):
10873 * [I] infoPtr : valid pointer to the listview structure
10874 * [I] hwnd : window handle of window containing the cursor
10875 * [I] nHittest : hit-test code
10876 * [I] wMouseMsg : ideintifier of the mouse message
10878 * RETURN:
10879 * TRUE if cursor is set
10880 * FALSE otherwise
10882 static BOOL LISTVIEW_SetCursor(const LISTVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
10884 LVHITTESTINFO lvHitTestInfo;
10886 if (!LISTVIEW_IsHotTracking(infoPtr)) goto forward;
10888 if (!infoPtr->hHotCursor) goto forward;
10890 GetCursorPos(&lvHitTestInfo.pt);
10891 if (LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, FALSE, FALSE) < 0) goto forward;
10893 SetCursor(infoPtr->hHotCursor);
10895 return TRUE;
10897 forward:
10899 return DefWindowProcW(infoPtr->hwndSelf, WM_SETCURSOR, wParam, lParam);
10902 /***
10903 * DESCRIPTION:
10904 * Sets the focus.
10906 * PARAMETER(S):
10907 * [I] infoPtr : valid pointer to the listview structure
10908 * [I] hwndLoseFocus : handle of previously focused window
10910 * RETURN:
10911 * Zero
10913 static LRESULT LISTVIEW_SetFocus(LISTVIEW_INFO *infoPtr, HWND hwndLoseFocus)
10915 TRACE("(hwndLoseFocus=%p)\n", hwndLoseFocus);
10917 /* if we have the focus already, there's nothing to do */
10918 if (infoPtr->bFocus) return 0;
10920 /* send NM_SETFOCUS notification */
10921 if (!notify(infoPtr, NM_SETFOCUS)) return 0;
10923 /* set window focus flag */
10924 infoPtr->bFocus = TRUE;
10926 /* put the focus rect back on */
10927 LISTVIEW_ShowFocusRect(infoPtr, TRUE);
10929 /* redraw all visible selected items */
10930 LISTVIEW_InvalidateSelectedItems(infoPtr);
10932 return 0;
10935 /***
10936 * DESCRIPTION:
10937 * Sets the font.
10939 * PARAMETER(S):
10940 * [I] infoPtr : valid pointer to the listview structure
10941 * [I] fRedraw : font handle
10942 * [I] fRedraw : redraw flag
10944 * RETURN:
10945 * Zero
10947 static LRESULT LISTVIEW_SetFont(LISTVIEW_INFO *infoPtr, HFONT hFont, WORD fRedraw)
10949 HFONT oldFont = infoPtr->hFont;
10950 INT oldHeight = infoPtr->nItemHeight;
10952 TRACE("(hfont=%p,redraw=%hu)\n", hFont, fRedraw);
10954 infoPtr->hFont = hFont ? hFont : infoPtr->hDefaultFont;
10955 if (infoPtr->hFont == oldFont) return 0;
10957 LISTVIEW_SaveTextMetrics(infoPtr);
10959 infoPtr->nItemHeight = LISTVIEW_CalculateItemHeight(infoPtr);
10961 if (infoPtr->uView == LV_VIEW_DETAILS)
10963 SendMessageW(infoPtr->hwndHeader, WM_SETFONT, (WPARAM)hFont, MAKELPARAM(fRedraw, 0));
10964 LISTVIEW_UpdateSize(infoPtr);
10965 LISTVIEW_UpdateScroll(infoPtr);
10967 else if (infoPtr->nItemHeight != oldHeight)
10968 LISTVIEW_UpdateScroll(infoPtr);
10970 if (fRedraw) LISTVIEW_InvalidateList(infoPtr);
10972 return 0;
10975 /***
10976 * DESCRIPTION:
10977 * Message handling for WM_SETREDRAW.
10978 * For the Listview, it invalidates the entire window (the doc specifies otherwise)
10980 * PARAMETER(S):
10981 * [I] infoPtr : valid pointer to the listview structure
10982 * [I] redraw: state of redraw flag
10984 * RETURN:
10985 * Zero.
10987 static LRESULT LISTVIEW_SetRedraw(LISTVIEW_INFO *infoPtr, BOOL redraw)
10989 TRACE("old=%d, new=%d\n", infoPtr->redraw, redraw);
10991 if (infoPtr->redraw == !!redraw)
10992 return 0;
10994 if (!(infoPtr->redraw = !!redraw))
10995 return 0;
10997 if (is_autoarrange(infoPtr))
10998 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
10999 LISTVIEW_UpdateScroll(infoPtr);
11001 /* despite what the WM_SETREDRAW docs says, apps expect us
11002 * to invalidate the listview here... stupid! */
11003 LISTVIEW_InvalidateList(infoPtr);
11005 return 0;
11008 /***
11009 * DESCRIPTION:
11010 * Resizes the listview control. This function processes WM_SIZE
11011 * messages. At this time, the width and height are not used.
11013 * PARAMETER(S):
11014 * [I] infoPtr : valid pointer to the listview structure
11015 * [I] Width : new width
11016 * [I] Height : new height
11018 * RETURN:
11019 * Zero
11021 static LRESULT LISTVIEW_Size(LISTVIEW_INFO *infoPtr, int Width, int Height)
11023 RECT rcOld = infoPtr->rcList;
11025 TRACE("(width=%d, height=%d)\n", Width, Height);
11027 LISTVIEW_UpdateSize(infoPtr);
11028 if (EqualRect(&rcOld, &infoPtr->rcList)) return 0;
11030 /* do not bother with display related stuff if we're not redrawing */
11031 if (!is_redrawing(infoPtr)) return 0;
11033 if (is_autoarrange(infoPtr))
11034 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
11036 LISTVIEW_UpdateScroll(infoPtr);
11038 /* refresh all only for lists whose height changed significantly */
11039 if ((infoPtr->uView == LV_VIEW_LIST) &&
11040 (rcOld.bottom - rcOld.top) / infoPtr->nItemHeight !=
11041 (infoPtr->rcList.bottom - infoPtr->rcList.top) / infoPtr->nItemHeight)
11042 LISTVIEW_InvalidateList(infoPtr);
11044 return 0;
11047 /***
11048 * DESCRIPTION:
11049 * Sets the size information.
11051 * PARAMETER(S):
11052 * [I] infoPtr : valid pointer to the listview structure
11054 * RETURN:
11055 * None
11057 static void LISTVIEW_UpdateSize(LISTVIEW_INFO *infoPtr)
11059 TRACE("uView=%d, rcList(old)=%s\n", infoPtr->uView, wine_dbgstr_rect(&infoPtr->rcList));
11061 GetClientRect(infoPtr->hwndSelf, &infoPtr->rcList);
11063 if (infoPtr->uView == LV_VIEW_LIST)
11065 /* Apparently the "LIST" style is supposed to have the same
11066 * number of items in a column even if there is no scroll bar.
11067 * Since if a scroll bar already exists then the bottom is already
11068 * reduced, only reduce if the scroll bar does not currently exist.
11069 * The "2" is there to mimic the native control. I think it may be
11070 * related to either padding or edges. (GLA 7/2002)
11072 if (!(GetWindowLongW(infoPtr->hwndSelf, GWL_STYLE) & WS_HSCROLL))
11073 infoPtr->rcList.bottom -= GetSystemMetrics(SM_CYHSCROLL);
11074 infoPtr->rcList.bottom = max (infoPtr->rcList.bottom - 2, 0);
11077 /* When ListView control is created invisible, header isn't created right away. */
11078 if (infoPtr->hwndHeader)
11080 POINT origin;
11081 WINDOWPOS wp;
11082 HDLAYOUT hl;
11083 RECT rect;
11085 LISTVIEW_GetOrigin(infoPtr, &origin);
11087 rect = infoPtr->rcList;
11088 rect.left += origin.x;
11090 hl.prc = &rect;
11091 hl.pwpos = &wp;
11092 SendMessageW( infoPtr->hwndHeader, HDM_LAYOUT, 0, (LPARAM)&hl );
11093 TRACE(" wp.flags=0x%08x, wp=%d,%d (%dx%d)\n", wp.flags, wp.x, wp.y, wp.cx, wp.cy);
11095 if (LISTVIEW_IsHeaderEnabled(infoPtr))
11096 wp.flags |= SWP_SHOWWINDOW;
11097 else
11099 wp.flags |= SWP_HIDEWINDOW;
11100 wp.cy = 0;
11103 SetWindowPos(wp.hwnd, wp.hwndInsertAfter, wp.x, wp.y, wp.cx, wp.cy, wp.flags);
11104 TRACE(" after SWP wp=%d,%d (%dx%d)\n", wp.x, wp.y, wp.cx, wp.cy);
11106 infoPtr->rcList.top = max(wp.cy, 0);
11108 /* extra padding for grid */
11109 if (infoPtr->uView == LV_VIEW_DETAILS && infoPtr->dwLvExStyle & LVS_EX_GRIDLINES)
11110 infoPtr->rcList.top += 2;
11112 TRACE(" rcList=%s\n", wine_dbgstr_rect(&infoPtr->rcList));
11115 /***
11116 * DESCRIPTION:
11117 * Processes WM_STYLECHANGED messages.
11119 * PARAMETER(S):
11120 * [I] infoPtr : valid pointer to the listview structure
11121 * [I] wStyleType : window style type (normal or extended)
11122 * [I] lpss : window style information
11124 * RETURN:
11125 * Zero
11127 static INT LISTVIEW_StyleChanged(LISTVIEW_INFO *infoPtr, WPARAM wStyleType,
11128 const STYLESTRUCT *lpss)
11130 UINT uNewView, uOldView;
11131 UINT style;
11133 TRACE("(styletype=%lx, styleOld=0x%08x, styleNew=0x%08x)\n",
11134 wStyleType, lpss->styleOld, lpss->styleNew);
11136 if (wStyleType != GWL_STYLE || lpss->styleNew == infoPtr->dwStyle) return 0;
11138 infoPtr->dwStyle = lpss->styleNew;
11140 if (((lpss->styleOld & WS_HSCROLL) != 0)&&
11141 ((lpss->styleNew & WS_HSCROLL) == 0))
11142 ShowScrollBar(infoPtr->hwndSelf, SB_HORZ, FALSE);
11144 if (((lpss->styleOld & WS_VSCROLL) != 0)&&
11145 ((lpss->styleNew & WS_VSCROLL) == 0))
11146 ShowScrollBar(infoPtr->hwndSelf, SB_VERT, FALSE);
11148 uNewView = lpss->styleNew & LVS_TYPEMASK;
11149 uOldView = lpss->styleOld & LVS_TYPEMASK;
11151 if (uNewView != uOldView)
11153 HIMAGELIST himl;
11155 /* LVM_SETVIEW doesn't change window style bits within LVS_TYPEMASK,
11156 changing style updates current view only when view bits change. */
11157 map_style_view(infoPtr);
11158 SendMessageW(infoPtr->hwndEdit, WM_KILLFOCUS, 0, 0);
11159 ShowWindow(infoPtr->hwndHeader, SW_HIDE);
11161 ShowScrollBar(infoPtr->hwndSelf, SB_BOTH, FALSE);
11162 SetRectEmpty(&infoPtr->rcFocus);
11164 himl = (uNewView == LVS_ICON ? infoPtr->himlNormal : infoPtr->himlSmall);
11165 set_icon_size(&infoPtr->iconSize, himl, uNewView != LVS_ICON);
11167 if (uNewView == LVS_REPORT)
11169 HDLAYOUT hl;
11170 WINDOWPOS wp;
11172 LISTVIEW_CreateHeader( infoPtr );
11174 hl.prc = &infoPtr->rcList;
11175 hl.pwpos = &wp;
11176 SendMessageW( infoPtr->hwndHeader, HDM_LAYOUT, 0, (LPARAM)&hl );
11177 SetWindowPos(infoPtr->hwndHeader, infoPtr->hwndSelf, wp.x, wp.y, wp.cx, wp.cy,
11178 wp.flags | ((infoPtr->dwStyle & LVS_NOCOLUMNHEADER)
11179 ? SWP_HIDEWINDOW : SWP_SHOWWINDOW));
11182 LISTVIEW_UpdateItemSize(infoPtr);
11185 if (uNewView == LVS_REPORT || infoPtr->dwLvExStyle & LVS_EX_HEADERINALLVIEWS)
11187 if ((lpss->styleOld ^ lpss->styleNew) & LVS_NOCOLUMNHEADER)
11189 if (lpss->styleNew & LVS_NOCOLUMNHEADER)
11191 /* Turn off the header control */
11192 style = GetWindowLongW(infoPtr->hwndHeader, GWL_STYLE);
11193 TRACE("Hide header control, was 0x%08x\n", style);
11194 SetWindowLongW(infoPtr->hwndHeader, GWL_STYLE, style | HDS_HIDDEN);
11195 } else {
11196 /* Turn on the header control */
11197 if ((style = GetWindowLongW(infoPtr->hwndHeader, GWL_STYLE)) & HDS_HIDDEN)
11199 TRACE("Show header control, was 0x%08x\n", style);
11200 SetWindowLongW(infoPtr->hwndHeader, GWL_STYLE, (style & ~HDS_HIDDEN) | WS_VISIBLE);
11206 if ( (uNewView == LVS_ICON || uNewView == LVS_SMALLICON) &&
11207 (uNewView != uOldView || ((lpss->styleNew ^ lpss->styleOld) & LVS_ALIGNMASK)) )
11208 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
11210 /* update the size of the client area */
11211 LISTVIEW_UpdateSize(infoPtr);
11213 /* add scrollbars if needed */
11214 LISTVIEW_UpdateScroll(infoPtr);
11216 /* invalidate client area + erase background */
11217 LISTVIEW_InvalidateList(infoPtr);
11219 return 0;
11222 /***
11223 * DESCRIPTION:
11224 * Processes WM_STYLECHANGING messages.
11226 * PARAMETER(S):
11227 * [I] wStyleType : window style type (normal or extended)
11228 * [I0] lpss : window style information
11230 * RETURN:
11231 * Zero
11233 static INT LISTVIEW_StyleChanging(WPARAM wStyleType,
11234 STYLESTRUCT *lpss)
11236 TRACE("(styletype=%lx, styleOld=0x%08x, styleNew=0x%08x)\n",
11237 wStyleType, lpss->styleOld, lpss->styleNew);
11239 /* don't forward LVS_OWNERDATA only if not already set to */
11240 if ((lpss->styleNew ^ lpss->styleOld) & LVS_OWNERDATA)
11242 if (lpss->styleOld & LVS_OWNERDATA)
11243 lpss->styleNew |= LVS_OWNERDATA;
11244 else
11245 lpss->styleNew &= ~LVS_OWNERDATA;
11248 return 0;
11251 /***
11252 * DESCRIPTION:
11253 * Processes WM_SHOWWINDOW messages.
11255 * PARAMETER(S):
11256 * [I] infoPtr : valid pointer to the listview structure
11257 * [I] bShown : window is being shown (FALSE when hidden)
11258 * [I] iStatus : window show status
11260 * RETURN:
11261 * Zero
11263 static LRESULT LISTVIEW_ShowWindow(LISTVIEW_INFO *infoPtr, WPARAM bShown, LPARAM iStatus)
11265 /* header delayed creation */
11266 if ((infoPtr->uView == LV_VIEW_DETAILS) && bShown)
11268 LISTVIEW_CreateHeader(infoPtr);
11270 if (!(LVS_NOCOLUMNHEADER & infoPtr->dwStyle))
11271 ShowWindow(infoPtr->hwndHeader, SW_SHOWNORMAL);
11274 return DefWindowProcW(infoPtr->hwndSelf, WM_SHOWWINDOW, bShown, iStatus);
11277 /***
11278 * DESCRIPTION:
11279 * Processes CCM_GETVERSION messages.
11281 * PARAMETER(S):
11282 * [I] infoPtr : valid pointer to the listview structure
11284 * RETURN:
11285 * Current version
11287 static inline LRESULT LISTVIEW_GetVersion(const LISTVIEW_INFO *infoPtr)
11289 return infoPtr->iVersion;
11292 /***
11293 * DESCRIPTION:
11294 * Processes CCM_SETVERSION messages.
11296 * PARAMETER(S):
11297 * [I] infoPtr : valid pointer to the listview structure
11298 * [I] iVersion : version to be set
11300 * RETURN:
11301 * -1 when requested version is greater than DLL version;
11302 * previous version otherwise
11304 static LRESULT LISTVIEW_SetVersion(LISTVIEW_INFO *infoPtr, DWORD iVersion)
11306 INT iOldVersion = infoPtr->iVersion;
11308 if (iVersion > COMCTL32_VERSION)
11309 return -1;
11311 infoPtr->iVersion = iVersion;
11313 TRACE("new version %d\n", iVersion);
11315 return iOldVersion;
11318 /***
11319 * DESCRIPTION:
11320 * Window procedure of the listview control.
11323 static LRESULT WINAPI
11324 LISTVIEW_WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
11326 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)GetWindowLongPtrW(hwnd, 0);
11328 TRACE("(hwnd=%p uMsg=%x wParam=%lx lParam=%lx)\n", hwnd, uMsg, wParam, lParam);
11330 if (!infoPtr && (uMsg != WM_NCCREATE))
11331 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
11333 switch (uMsg)
11335 case LVM_APPROXIMATEVIEWRECT:
11336 return LISTVIEW_ApproximateViewRect(infoPtr, (INT)wParam,
11337 LOWORD(lParam), HIWORD(lParam));
11338 case LVM_ARRANGE:
11339 return LISTVIEW_Arrange(infoPtr, (INT)wParam);
11341 case LVM_CANCELEDITLABEL:
11342 return LISTVIEW_CancelEditLabel(infoPtr);
11344 case LVM_CREATEDRAGIMAGE:
11345 return (LRESULT)LISTVIEW_CreateDragImage(infoPtr, (INT)wParam, (LPPOINT)lParam);
11347 case LVM_DELETEALLITEMS:
11348 return LISTVIEW_DeleteAllItems(infoPtr, FALSE);
11350 case LVM_DELETECOLUMN:
11351 return LISTVIEW_DeleteColumn(infoPtr, (INT)wParam);
11353 case LVM_DELETEITEM:
11354 return LISTVIEW_DeleteItem(infoPtr, (INT)wParam);
11356 case LVM_EDITLABELA:
11357 case LVM_EDITLABELW:
11358 return (LRESULT)LISTVIEW_EditLabelT(infoPtr, (INT)wParam,
11359 uMsg == LVM_EDITLABELW);
11360 /* case LVM_ENABLEGROUPVIEW: */
11362 case LVM_ENSUREVISIBLE:
11363 return LISTVIEW_EnsureVisible(infoPtr, (INT)wParam, (BOOL)lParam);
11365 case LVM_FINDITEMW:
11366 return LISTVIEW_FindItemW(infoPtr, (INT)wParam, (LPLVFINDINFOW)lParam);
11368 case LVM_FINDITEMA:
11369 return LISTVIEW_FindItemA(infoPtr, (INT)wParam, (LPLVFINDINFOA)lParam);
11371 case LVM_GETBKCOLOR:
11372 return infoPtr->clrBk;
11374 /* case LVM_GETBKIMAGE: */
11376 case LVM_GETCALLBACKMASK:
11377 return infoPtr->uCallbackMask;
11379 case LVM_GETCOLUMNA:
11380 case LVM_GETCOLUMNW:
11381 return LISTVIEW_GetColumnT(infoPtr, (INT)wParam, (LPLVCOLUMNW)lParam,
11382 uMsg == LVM_GETCOLUMNW);
11384 case LVM_GETCOLUMNORDERARRAY:
11385 return LISTVIEW_GetColumnOrderArray(infoPtr, (INT)wParam, (LPINT)lParam);
11387 case LVM_GETCOLUMNWIDTH:
11388 return LISTVIEW_GetColumnWidth(infoPtr, (INT)wParam);
11390 case LVM_GETCOUNTPERPAGE:
11391 return LISTVIEW_GetCountPerPage(infoPtr);
11393 case LVM_GETEDITCONTROL:
11394 return (LRESULT)infoPtr->hwndEdit;
11396 case LVM_GETEXTENDEDLISTVIEWSTYLE:
11397 return infoPtr->dwLvExStyle;
11399 /* case LVM_GETGROUPINFO: */
11401 /* case LVM_GETGROUPMETRICS: */
11403 case LVM_GETHEADER:
11404 return (LRESULT)infoPtr->hwndHeader;
11406 case LVM_GETHOTCURSOR:
11407 return (LRESULT)infoPtr->hHotCursor;
11409 case LVM_GETHOTITEM:
11410 return infoPtr->nHotItem;
11412 case LVM_GETHOVERTIME:
11413 return infoPtr->dwHoverTime;
11415 case LVM_GETIMAGELIST:
11416 return (LRESULT)LISTVIEW_GetImageList(infoPtr, (INT)wParam);
11418 /* case LVM_GETINSERTMARK: */
11420 /* case LVM_GETINSERTMARKCOLOR: */
11422 /* case LVM_GETINSERTMARKRECT: */
11424 case LVM_GETISEARCHSTRINGA:
11425 case LVM_GETISEARCHSTRINGW:
11426 FIXME("LVM_GETISEARCHSTRING: unimplemented\n");
11427 return FALSE;
11429 case LVM_GETITEMA:
11430 case LVM_GETITEMW:
11431 return LISTVIEW_GetItemExtT(infoPtr, (LPLVITEMW)lParam, uMsg == LVM_GETITEMW);
11433 case LVM_GETITEMCOUNT:
11434 return infoPtr->nItemCount;
11436 case LVM_GETITEMPOSITION:
11437 return LISTVIEW_GetItemPosition(infoPtr, (INT)wParam, (LPPOINT)lParam);
11439 case LVM_GETITEMRECT:
11440 return LISTVIEW_GetItemRect(infoPtr, (INT)wParam, (LPRECT)lParam);
11442 case LVM_GETITEMSPACING:
11443 return LISTVIEW_GetItemSpacing(infoPtr, (BOOL)wParam);
11445 case LVM_GETITEMSTATE:
11446 return LISTVIEW_GetItemState(infoPtr, (INT)wParam, (UINT)lParam);
11448 case LVM_GETITEMTEXTA:
11449 case LVM_GETITEMTEXTW:
11450 return LISTVIEW_GetItemTextT(infoPtr, (INT)wParam, (LPLVITEMW)lParam,
11451 uMsg == LVM_GETITEMTEXTW);
11453 case LVM_GETNEXTITEM:
11454 return LISTVIEW_GetNextItem(infoPtr, (INT)wParam, LOWORD(lParam));
11456 case LVM_GETNUMBEROFWORKAREAS:
11457 FIXME("LVM_GETNUMBEROFWORKAREAS: unimplemented\n");
11458 return 1;
11460 case LVM_GETORIGIN:
11461 if (!lParam) return FALSE;
11462 if (infoPtr->uView == LV_VIEW_DETAILS ||
11463 infoPtr->uView == LV_VIEW_LIST) return FALSE;
11464 LISTVIEW_GetOrigin(infoPtr, (LPPOINT)lParam);
11465 return TRUE;
11467 /* case LVM_GETOUTLINECOLOR: */
11469 case LVM_GETSELECTEDCOLUMN:
11470 return infoPtr->selected_column;
11472 case LVM_GETSELECTEDCOUNT:
11473 return LISTVIEW_GetSelectedCount(infoPtr);
11475 case LVM_GETSELECTIONMARK:
11476 return infoPtr->nSelectionMark;
11478 case LVM_GETSTRINGWIDTHA:
11479 case LVM_GETSTRINGWIDTHW:
11480 return LISTVIEW_GetStringWidthT(infoPtr, (LPCWSTR)lParam,
11481 uMsg == LVM_GETSTRINGWIDTHW);
11483 case LVM_GETSUBITEMRECT:
11484 return LISTVIEW_GetSubItemRect(infoPtr, (UINT)wParam, (LPRECT)lParam);
11486 case LVM_GETTEXTBKCOLOR:
11487 return infoPtr->clrTextBk;
11489 case LVM_GETTEXTCOLOR:
11490 return infoPtr->clrText;
11492 /* case LVM_GETTILEINFO: */
11494 /* case LVM_GETTILEVIEWINFO: */
11496 case LVM_GETTOOLTIPS:
11497 if( !infoPtr->hwndToolTip )
11498 infoPtr->hwndToolTip = COMCTL32_CreateToolTip( hwnd );
11499 return (LRESULT)infoPtr->hwndToolTip;
11501 case LVM_GETTOPINDEX:
11502 return LISTVIEW_GetTopIndex(infoPtr);
11504 case LVM_GETUNICODEFORMAT:
11505 return (infoPtr->notifyFormat == NFR_UNICODE);
11507 case LVM_GETVIEW:
11508 return infoPtr->uView;
11510 case LVM_GETVIEWRECT:
11511 return LISTVIEW_GetViewRect(infoPtr, (LPRECT)lParam);
11513 case LVM_GETWORKAREAS:
11514 FIXME("LVM_GETWORKAREAS: unimplemented\n");
11515 return FALSE;
11517 /* case LVM_HASGROUP: */
11519 case LVM_HITTEST:
11520 return LISTVIEW_HitTest(infoPtr, (LPLVHITTESTINFO)lParam, FALSE, TRUE);
11522 case LVM_INSERTCOLUMNA:
11523 case LVM_INSERTCOLUMNW:
11524 return LISTVIEW_InsertColumnT(infoPtr, (INT)wParam, (LPLVCOLUMNW)lParam,
11525 uMsg == LVM_INSERTCOLUMNW);
11527 /* case LVM_INSERTGROUP: */
11529 /* case LVM_INSERTGROUPSORTED: */
11531 case LVM_INSERTITEMA:
11532 case LVM_INSERTITEMW:
11533 return LISTVIEW_InsertItemT(infoPtr, (LPLVITEMW)lParam, uMsg == LVM_INSERTITEMW);
11535 /* case LVM_INSERTMARKHITTEST: */
11537 /* case LVM_ISGROUPVIEWENABLED: */
11539 case LVM_ISITEMVISIBLE:
11540 return LISTVIEW_IsItemVisible(infoPtr, (INT)wParam);
11542 case LVM_MAPIDTOINDEX:
11543 return LISTVIEW_MapIdToIndex(infoPtr, (UINT)wParam);
11545 case LVM_MAPINDEXTOID:
11546 return LISTVIEW_MapIndexToId(infoPtr, (INT)wParam);
11548 /* case LVM_MOVEGROUP: */
11550 /* case LVM_MOVEITEMTOGROUP: */
11552 case LVM_REDRAWITEMS:
11553 return LISTVIEW_RedrawItems(infoPtr, (INT)wParam, (INT)lParam);
11555 /* case LVM_REMOVEALLGROUPS: */
11557 /* case LVM_REMOVEGROUP: */
11559 case LVM_SCROLL:
11560 return LISTVIEW_Scroll(infoPtr, (INT)wParam, (INT)lParam);
11562 case LVM_SETBKCOLOR:
11563 return LISTVIEW_SetBkColor(infoPtr, (COLORREF)lParam);
11565 /* case LVM_SETBKIMAGE: */
11567 case LVM_SETCALLBACKMASK:
11568 infoPtr->uCallbackMask = (UINT)wParam;
11569 return TRUE;
11571 case LVM_SETCOLUMNA:
11572 case LVM_SETCOLUMNW:
11573 return LISTVIEW_SetColumnT(infoPtr, (INT)wParam, (LPLVCOLUMNW)lParam,
11574 uMsg == LVM_SETCOLUMNW);
11576 case LVM_SETCOLUMNORDERARRAY:
11577 return LISTVIEW_SetColumnOrderArray(infoPtr, (INT)wParam, (LPINT)lParam);
11579 case LVM_SETCOLUMNWIDTH:
11580 return LISTVIEW_SetColumnWidth(infoPtr, (INT)wParam, (short)LOWORD(lParam));
11582 case LVM_SETEXTENDEDLISTVIEWSTYLE:
11583 return LISTVIEW_SetExtendedListViewStyle(infoPtr, (DWORD)wParam, (DWORD)lParam);
11585 /* case LVM_SETGROUPINFO: */
11587 /* case LVM_SETGROUPMETRICS: */
11589 case LVM_SETHOTCURSOR:
11590 return (LRESULT)LISTVIEW_SetHotCursor(infoPtr, (HCURSOR)lParam);
11592 case LVM_SETHOTITEM:
11593 return LISTVIEW_SetHotItem(infoPtr, (INT)wParam);
11595 case LVM_SETHOVERTIME:
11596 return LISTVIEW_SetHoverTime(infoPtr, (DWORD)lParam);
11598 case LVM_SETICONSPACING:
11599 if(lParam == -1)
11600 return LISTVIEW_SetIconSpacing(infoPtr, -1, -1);
11601 return LISTVIEW_SetIconSpacing(infoPtr, LOWORD(lParam), HIWORD(lParam));
11603 case LVM_SETIMAGELIST:
11604 return (LRESULT)LISTVIEW_SetImageList(infoPtr, (INT)wParam, (HIMAGELIST)lParam);
11606 /* case LVM_SETINFOTIP: */
11608 /* case LVM_SETINSERTMARK: */
11610 /* case LVM_SETINSERTMARKCOLOR: */
11612 case LVM_SETITEMA:
11613 case LVM_SETITEMW:
11615 if (infoPtr->dwStyle & LVS_OWNERDATA) return FALSE;
11616 return LISTVIEW_SetItemT(infoPtr, (LPLVITEMW)lParam, (uMsg == LVM_SETITEMW));
11619 case LVM_SETITEMCOUNT:
11620 return LISTVIEW_SetItemCount(infoPtr, (INT)wParam, (DWORD)lParam);
11622 case LVM_SETITEMPOSITION:
11624 POINT pt;
11625 pt.x = (short)LOWORD(lParam);
11626 pt.y = (short)HIWORD(lParam);
11627 return LISTVIEW_SetItemPosition(infoPtr, (INT)wParam, &pt);
11630 case LVM_SETITEMPOSITION32:
11631 return LISTVIEW_SetItemPosition(infoPtr, (INT)wParam, (POINT*)lParam);
11633 case LVM_SETITEMSTATE:
11634 return LISTVIEW_SetItemState(infoPtr, (INT)wParam, (LPLVITEMW)lParam);
11636 case LVM_SETITEMTEXTA:
11637 case LVM_SETITEMTEXTW:
11638 return LISTVIEW_SetItemTextT(infoPtr, (INT)wParam, (LPLVITEMW)lParam,
11639 uMsg == LVM_SETITEMTEXTW);
11641 /* case LVM_SETOUTLINECOLOR: */
11643 case LVM_SETSELECTEDCOLUMN:
11644 infoPtr->selected_column = (INT)wParam;
11645 return TRUE;
11647 case LVM_SETSELECTIONMARK:
11648 return LISTVIEW_SetSelectionMark(infoPtr, (INT)lParam);
11650 case LVM_SETTEXTBKCOLOR:
11651 return LISTVIEW_SetTextBkColor(infoPtr, (COLORREF)lParam);
11653 case LVM_SETTEXTCOLOR:
11654 return LISTVIEW_SetTextColor(infoPtr, (COLORREF)lParam);
11656 /* case LVM_SETTILEINFO: */
11658 /* case LVM_SETTILEVIEWINFO: */
11660 /* case LVM_SETTILEWIDTH: */
11662 case LVM_SETTOOLTIPS:
11663 return (LRESULT)LISTVIEW_SetToolTips(infoPtr, (HWND)lParam);
11665 case LVM_SETUNICODEFORMAT:
11666 return LISTVIEW_SetUnicodeFormat(infoPtr, wParam);
11668 case LVM_SETVIEW:
11669 return LISTVIEW_SetView(infoPtr, wParam);
11671 /* case LVM_SETWORKAREAS: */
11673 /* case LVM_SORTGROUPS: */
11675 case LVM_SORTITEMS:
11676 case LVM_SORTITEMSEX:
11677 return LISTVIEW_SortItems(infoPtr, (PFNLVCOMPARE)lParam, wParam,
11678 uMsg == LVM_SORTITEMSEX);
11679 case LVM_SUBITEMHITTEST:
11680 return LISTVIEW_HitTest(infoPtr, (LPLVHITTESTINFO)lParam, TRUE, FALSE);
11682 case LVM_UPDATE:
11683 return LISTVIEW_Update(infoPtr, (INT)wParam);
11685 case CCM_GETVERSION:
11686 return LISTVIEW_GetVersion(infoPtr);
11688 case CCM_SETVERSION:
11689 return LISTVIEW_SetVersion(infoPtr, wParam);
11691 case WM_CHAR:
11692 return LISTVIEW_ProcessLetterKeys( infoPtr, wParam, lParam );
11694 case WM_COMMAND:
11695 return LISTVIEW_Command(infoPtr, wParam, lParam);
11697 case WM_NCCREATE:
11698 return LISTVIEW_NCCreate(hwnd, wParam, (LPCREATESTRUCTW)lParam);
11700 case WM_CREATE:
11701 return LISTVIEW_Create(hwnd, (LPCREATESTRUCTW)lParam);
11703 case WM_DESTROY:
11704 return LISTVIEW_Destroy(infoPtr);
11706 case WM_ENABLE:
11707 return LISTVIEW_Enable(infoPtr);
11709 case WM_ERASEBKGND:
11710 return LISTVIEW_EraseBkgnd(infoPtr, (HDC)wParam);
11712 case WM_GETDLGCODE:
11713 return DLGC_WANTCHARS | DLGC_WANTARROWS;
11715 case WM_GETFONT:
11716 return (LRESULT)infoPtr->hFont;
11718 case WM_HSCROLL:
11719 return LISTVIEW_HScroll(infoPtr, (INT)LOWORD(wParam), 0);
11721 case WM_KEYDOWN:
11722 return LISTVIEW_KeyDown(infoPtr, (INT)wParam, (LONG)lParam);
11724 case WM_KILLFOCUS:
11725 return LISTVIEW_KillFocus(infoPtr);
11727 case WM_LBUTTONDBLCLK:
11728 return LISTVIEW_LButtonDblClk(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11730 case WM_LBUTTONDOWN:
11731 return LISTVIEW_LButtonDown(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11733 case WM_LBUTTONUP:
11734 return LISTVIEW_LButtonUp(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11736 case WM_MOUSEMOVE:
11737 return LISTVIEW_MouseMove (infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11739 case WM_MOUSEHOVER:
11740 return LISTVIEW_MouseHover(infoPtr, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11742 case WM_NCDESTROY:
11743 return LISTVIEW_NCDestroy(infoPtr);
11745 case WM_NCPAINT:
11746 return LISTVIEW_NCPaint(infoPtr, (HRGN)wParam);
11748 case WM_NOTIFY:
11749 return LISTVIEW_Notify(infoPtr, (LPNMHDR)lParam);
11751 case WM_NOTIFYFORMAT:
11752 return LISTVIEW_NotifyFormat(infoPtr, (HWND)wParam, (INT)lParam);
11754 case WM_PRINTCLIENT:
11755 return LISTVIEW_PrintClient(infoPtr, (HDC)wParam, (DWORD)lParam);
11757 case WM_PAINT:
11758 return LISTVIEW_WMPaint(infoPtr, (HDC)wParam);
11760 case WM_RBUTTONDBLCLK:
11761 return LISTVIEW_RButtonDblClk(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11763 case WM_RBUTTONDOWN:
11764 return LISTVIEW_RButtonDown(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11766 case WM_SETCURSOR:
11767 return LISTVIEW_SetCursor(infoPtr, wParam, lParam);
11769 case WM_SETFOCUS:
11770 return LISTVIEW_SetFocus(infoPtr, (HWND)wParam);
11772 case WM_SETFONT:
11773 return LISTVIEW_SetFont(infoPtr, (HFONT)wParam, (WORD)lParam);
11775 case WM_SETREDRAW:
11776 return LISTVIEW_SetRedraw(infoPtr, (BOOL)wParam);
11778 case WM_SHOWWINDOW:
11779 return LISTVIEW_ShowWindow(infoPtr, wParam, lParam);
11781 case WM_STYLECHANGED:
11782 return LISTVIEW_StyleChanged(infoPtr, wParam, (LPSTYLESTRUCT)lParam);
11784 case WM_STYLECHANGING:
11785 return LISTVIEW_StyleChanging(wParam, (LPSTYLESTRUCT)lParam);
11787 case WM_SYSCOLORCHANGE:
11788 COMCTL32_RefreshSysColors();
11789 return 0;
11791 /* case WM_TIMER: */
11792 case WM_THEMECHANGED:
11793 return LISTVIEW_ThemeChanged(infoPtr);
11795 case WM_VSCROLL:
11796 return LISTVIEW_VScroll(infoPtr, (INT)LOWORD(wParam), 0);
11798 case WM_MOUSEWHEEL:
11799 if (wParam & (MK_SHIFT | MK_CONTROL))
11800 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
11801 return LISTVIEW_MouseWheel(infoPtr, (short int)HIWORD(wParam));
11803 case WM_WINDOWPOSCHANGED:
11804 if (!(((WINDOWPOS *)lParam)->flags & SWP_NOSIZE))
11806 SetWindowPos(infoPtr->hwndSelf, 0, 0, 0, 0, 0, SWP_FRAMECHANGED | SWP_NOACTIVATE |
11807 SWP_NOZORDER | SWP_NOMOVE | SWP_NOSIZE);
11809 if ((infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && (infoPtr->uView == LV_VIEW_DETAILS))
11811 if (notify_measureitem(infoPtr)) LISTVIEW_InvalidateList(infoPtr);
11813 LISTVIEW_Size(infoPtr, ((WINDOWPOS *)lParam)->cx, ((WINDOWPOS *)lParam)->cy);
11815 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
11817 /* case WM_WININICHANGE: */
11819 default:
11820 if ((uMsg >= WM_USER) && (uMsg < WM_APP) && !COMCTL32_IsReflectedMessage(uMsg))
11821 ERR("unknown msg %04x wp=%08lx lp=%08lx\n", uMsg, wParam, lParam);
11823 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
11828 /***
11829 * DESCRIPTION:
11830 * Registers the window class.
11832 * PARAMETER(S):
11833 * None
11835 * RETURN:
11836 * None
11838 void LISTVIEW_Register(void)
11840 WNDCLASSW wndClass;
11842 ZeroMemory(&wndClass, sizeof(WNDCLASSW));
11843 wndClass.style = CS_GLOBALCLASS | CS_DBLCLKS;
11844 wndClass.lpfnWndProc = LISTVIEW_WindowProc;
11845 wndClass.cbClsExtra = 0;
11846 wndClass.cbWndExtra = sizeof(LISTVIEW_INFO *);
11847 wndClass.hCursor = LoadCursorW(0, (LPWSTR)IDC_ARROW);
11848 wndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
11849 wndClass.lpszClassName = WC_LISTVIEWW;
11850 RegisterClassW(&wndClass);
11853 /***
11854 * DESCRIPTION:
11855 * Unregisters the window class.
11857 * PARAMETER(S):
11858 * None
11860 * RETURN:
11861 * None
11863 void LISTVIEW_Unregister(void)
11865 UnregisterClassW(WC_LISTVIEWW, NULL);
11868 /***
11869 * DESCRIPTION:
11870 * Handle any WM_COMMAND messages
11872 * PARAMETER(S):
11873 * [I] infoPtr : valid pointer to the listview structure
11874 * [I] wParam : the first message parameter
11875 * [I] lParam : the second message parameter
11877 * RETURN:
11878 * Zero.
11880 static LRESULT LISTVIEW_Command(LISTVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
11883 TRACE("(%p %x %x %lx)\n", infoPtr, HIWORD(wParam), LOWORD(wParam), lParam);
11885 if (!infoPtr->hwndEdit) return 0;
11887 switch (HIWORD(wParam))
11889 case EN_UPDATE:
11892 * Adjust the edit window size
11894 WCHAR buffer[1024];
11895 HDC hdc = GetDC(infoPtr->hwndEdit);
11896 HFONT hFont, hOldFont = 0;
11897 RECT rect;
11898 SIZE sz;
11900 if (!infoPtr->hwndEdit || !hdc) return 0;
11901 GetWindowTextW(infoPtr->hwndEdit, buffer, ARRAY_SIZE(buffer));
11902 GetWindowRect(infoPtr->hwndEdit, &rect);
11904 /* Select font to get the right dimension of the string */
11905 hFont = (HFONT)SendMessageW(infoPtr->hwndEdit, WM_GETFONT, 0, 0);
11906 if (hFont)
11908 hOldFont = SelectObject(hdc, hFont);
11911 if (GetTextExtentPoint32W(hdc, buffer, lstrlenW(buffer), &sz))
11913 TEXTMETRICW textMetric;
11915 /* Add Extra spacing for the next character */
11916 GetTextMetricsW(hdc, &textMetric);
11917 sz.cx += (textMetric.tmMaxCharWidth * 2);
11919 SetWindowPos(infoPtr->hwndEdit, NULL, 0, 0, sz.cx,
11920 rect.bottom - rect.top, SWP_DRAWFRAME | SWP_NOMOVE | SWP_NOZORDER);
11922 if (hFont)
11923 SelectObject(hdc, hOldFont);
11925 ReleaseDC(infoPtr->hwndEdit, hdc);
11927 break;
11929 case EN_KILLFOCUS:
11931 if (infoPtr->notify_mask & NOTIFY_MASK_END_LABEL_EDIT)
11932 LISTVIEW_CancelEditLabel(infoPtr);
11933 break;
11936 default:
11937 return SendMessageW (infoPtr->hwndNotify, WM_COMMAND, wParam, lParam);
11940 return 0;