push c4c845830c6aff14e1b16bbb8b4a57a7e0d6213f
[wine/hacks.git] / dlls / comctl32 / listview.c
blob73781b2dfa9a4f7616a360ccd2fafc4e5231bb16
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 Nikolay Sivov
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 * NOTES
27 * This code was audited for completeness against the documented features
28 * of Comctl32.dll version 6.0 on May. 20, 2005, by James Hawkins.
30 * Unless otherwise noted, we believe this code to be complete, as per
31 * the specification mentioned above.
32 * If you discover missing features, or bugs, please note them below.
34 * TODO:
36 * Default Message Processing
37 * -- WM_CREATE: create the icon and small icon image lists at this point only if
38 * the LVS_SHAREIMAGELISTS style is not specified.
39 * -- WM_WINDOWPOSCHANGED: arrange the list items if the current view is icon
40 * or small icon and the LVS_AUTOARRANGE style is specified.
41 * -- WM_TIMER
42 * -- WM_WININICHANGE
44 * Features
45 * -- Hot item handling, mouse hovering
46 * -- Workareas support
47 * -- Tilemode support
48 * -- Groups support
50 * Bugs
51 * -- Expand large item in ICON mode when the cursor is flying over the icon or text.
52 * -- Support CustomDraw options for _WIN32_IE >= 0x560 (see NMLVCUSTOMDRAW docs).
53 * -- LVA_SNAPTOGRID not implemented
54 * -- LISTVIEW_ApproximateViewRect partially implemented
55 * -- LISTVIEW_[GS]etColumnOrderArray stubs
56 * -- LISTVIEW_SetColumnWidth ignores header images & bitmap
57 * -- LISTVIEW_SetIconSpacing is incomplete
58 * -- LISTVIEW_StyleChanged doesn't handle some changes too well
60 * Speedups
61 * -- LISTVIEW_GetNextItem needs to be rewritten. It is currently
62 * linear in the number of items in the list, and this is
63 * unacceptable for large lists.
64 * -- if list is sorted by item text LISTVIEW_InsertItemT could use
65 * binary search to calculate item index (e.g. DPA_Search()).
66 * This requires sorted state to be reliably tracked in item modifiers.
67 * -- we should keep an ordered array of coordinates in iconic mode
68 * this would allow to frame items (iterator_frameditems),
69 * and find nearest item (LVFI_NEARESTXY) a lot more efficiently
71 * Flags
72 * -- LVIF_COLUMNS
73 * -- LVIF_GROUPID
75 * States
76 * -- LVIS_ACTIVATING (not currently supported by comctl32.dll version 6.0)
77 * -- LVIS_CUT
78 * -- LVIS_DROPHILITED
79 * -- LVIS_OVERLAYMASK
81 * Styles
82 * -- LVS_NOLABELWRAP
83 * -- LVS_NOSCROLL (see Q137520)
84 * -- LVS_ALIGNTOP
86 * Extended Styles
87 * -- LVS_EX_BORDERSELECT
88 * -- LVS_EX_FLATSB
89 * -- LVS_EX_HEADERDRAGDROP
90 * -- LVS_EX_INFOTIP
91 * -- LVS_EX_LABELTIP
92 * -- LVS_EX_MULTIWORKAREAS
93 * -- LVS_EX_REGIONAL
94 * -- LVS_EX_SIMPLESELECT
95 * -- LVS_EX_TWOCLICKACTIVATE
96 * -- LVS_EX_UNDERLINECOLD
97 * -- LVS_EX_UNDERLINEHOT
99 * Notifications:
100 * -- LVN_BEGINSCROLL, LVN_ENDSCROLL
101 * -- LVN_GETINFOTIP
102 * -- LVN_HOTTRACK
103 * -- LVN_MARQUEEBEGIN
104 * -- LVN_SETDISPINFO
105 * -- NM_HOVER
106 * -- LVN_BEGINRDRAG
108 * Messages:
109 * -- LVM_ENABLEGROUPVIEW
110 * -- LVM_GETBKIMAGE, LVM_SETBKIMAGE
111 * -- LVM_GETGROUPINFO, LVM_SETGROUPINFO
112 * -- LVM_GETGROUPMETRICS, LVM_SETGROUPMETRICS
113 * -- LVM_GETINSERTMARK, LVM_SETINSERTMARK
114 * -- LVM_GETINSERTMARKCOLOR, LVM_SETINSERTMARKCOLOR
115 * -- LVM_GETINSERTMARKRECT
116 * -- LVM_GETNUMBEROFWORKAREAS
117 * -- LVM_GETOUTLINECOLOR, LVM_SETOUTLINECOLOR
118 * -- LVM_GETSELECTEDCOLUMN, LVM_SETSELECTEDCOLUMN
119 * -- LVM_GETISEARCHSTRINGW, LVM_GETISEARCHSTRINGA
120 * -- LVM_GETTILEINFO, LVM_SETTILEINFO
121 * -- LVM_GETTILEVIEWINFO, LVM_SETTILEVIEWINFO
122 * -- LVM_GETWORKAREAS, LVM_SETWORKAREAS
123 * -- LVM_HASGROUP, LVM_INSERTGROUP, LVM_REMOVEGROUP, LVM_REMOVEALLGROUPS
124 * -- LVM_INSERTGROUPSORTED
125 * -- LVM_INSERTMARKHITTEST
126 * -- LVM_ISGROUPVIEWENABLED
127 * -- LVM_MOVEGROUP
128 * -- LVM_MOVEITEMTOGROUP
129 * -- LVM_SETINFOTIP
130 * -- LVM_SETTILEWIDTH
131 * -- LVM_SORTGROUPS
133 * Macros:
134 * -- ListView_GetHoverTime, ListView_SetHoverTime
135 * -- ListView_GetISearchString
136 * -- ListView_GetNumberOfWorkAreas
137 * -- ListView_GetWorkAreas, ListView_SetWorkAreas
139 * Functions:
140 * -- LVGroupComparE
142 * Known differences in message stream from native control (not known if
143 * these differences cause problems):
144 * LVM_INSERTITEM issues LVM_SETITEMSTATE and LVM_SETITEM in certain cases.
145 * LVM_SETITEM does not always issue LVN_ITEMCHANGING/LVN_ITEMCHANGED.
146 * WM_CREATE does not issue WM_QUERYUISTATE and associated registry
147 * processing for "USEDOUBLECLICKTIME".
150 #include "config.h"
151 #include "wine/port.h"
153 #include <assert.h>
154 #include <ctype.h>
155 #include <string.h>
156 #include <stdlib.h>
157 #include <stdarg.h>
158 #include <stdio.h>
160 #include "windef.h"
161 #include "winbase.h"
162 #include "winnt.h"
163 #include "wingdi.h"
164 #include "winuser.h"
165 #include "winnls.h"
166 #include "commctrl.h"
167 #include "comctl32.h"
168 #include "uxtheme.h"
170 #include "wine/debug.h"
171 #include "wine/unicode.h"
173 WINE_DEFAULT_DEBUG_CHANNEL(listview);
175 /* make sure you set this to 0 for production use! */
176 #define DEBUG_RANGES 1
178 typedef struct tagCOLUMN_INFO
180 RECT rcHeader; /* tracks the header's rectangle */
181 int fmt; /* same as LVCOLUMN.fmt */
182 } COLUMN_INFO;
184 typedef struct tagITEMHDR
186 LPWSTR pszText;
187 INT iImage;
188 } ITEMHDR, *LPITEMHDR;
190 typedef struct tagSUBITEM_INFO
192 ITEMHDR hdr;
193 INT iSubItem;
194 } SUBITEM_INFO;
196 typedef struct tagITEM_ID ITEM_ID;
198 typedef struct tagITEM_INFO
200 ITEMHDR hdr;
201 UINT state;
202 LPARAM lParam;
203 INT iIndent;
204 ITEM_ID *id;
205 } ITEM_INFO;
207 struct tagITEM_ID
209 UINT id; /* item id */
210 HDPA item; /* link to item data */
213 typedef struct tagRANGE
215 INT lower;
216 INT upper;
217 } RANGE;
219 typedef struct tagRANGES
221 HDPA hdpa;
222 } *RANGES;
224 typedef struct tagITERATOR
226 INT nItem;
227 INT nSpecial;
228 RANGE range;
229 RANGES ranges;
230 INT index;
231 } ITERATOR;
233 typedef struct tagDELAYED_ITEM_EDIT
235 BOOL fEnabled;
236 INT iItem;
237 } DELAYED_ITEM_EDIT;
239 typedef struct tagLISTVIEW_INFO
241 HWND hwndSelf;
242 HBRUSH hBkBrush;
243 COLORREF clrBk;
244 COLORREF clrText;
245 COLORREF clrTextBk;
246 HIMAGELIST himlNormal;
247 HIMAGELIST himlSmall;
248 HIMAGELIST himlState;
249 BOOL bLButtonDown;
250 BOOL bRButtonDown;
251 BOOL bDragging;
252 POINT ptClickPos; /* point where the user clicked */
253 BOOL bNoItemMetrics; /* flags if item metrics are not yet computed */
254 INT nItemHeight;
255 INT nItemWidth;
256 RANGES selectionRanges;
257 INT nSelectionMark;
258 INT nHotItem;
259 SHORT notifyFormat;
260 HWND hwndNotify;
261 RECT rcList; /* This rectangle is really the window
262 * client rectangle possibly reduced by the
263 * horizontal scroll bar and/or header - see
264 * LISTVIEW_UpdateSize. This rectangle offset
265 * by the LISTVIEW_GetOrigin value is in
266 * client coordinates */
267 SIZE iconSize;
268 SIZE iconSpacing;
269 SIZE iconStateSize;
270 UINT uCallbackMask;
271 HWND hwndHeader;
272 HCURSOR hHotCursor;
273 HFONT hDefaultFont;
274 HFONT hFont;
275 INT ntmHeight; /* Some cached metrics of the font used */
276 INT ntmMaxCharWidth; /* by the listview to draw items */
277 INT nEllipsisWidth;
278 BOOL bRedraw; /* Turns on/off repaints & invalidations */
279 BOOL bAutoarrange; /* Autoarrange flag when NOT in LVS_AUTOARRANGE */
280 BOOL bFocus;
281 BOOL bDoChangeNotify; /* send change notification messages? */
282 INT nFocusedItem;
283 RECT rcFocus;
284 DWORD dwStyle; /* the cached window GWL_STYLE */
285 DWORD dwLvExStyle; /* extended listview style */
286 DWORD uView; /* current view available through LVM_[G,S]ETVIEW */
287 INT nItemCount; /* the number of items in the list */
288 HDPA hdpaItems; /* array ITEM_INFO pointers */
289 HDPA hdpaItemIds; /* array of ITEM_ID pointers */
290 HDPA hdpaPosX; /* maintains the (X, Y) coordinates of the */
291 HDPA hdpaPosY; /* items in LVS_ICON, and LVS_SMALLICON modes */
292 HDPA hdpaColumns; /* array of COLUMN_INFO pointers */
293 POINT currIconPos; /* this is the position next icon will be placed */
294 PFNLVCOMPARE pfnCompare;
295 LPARAM lParamSort;
296 HWND hwndEdit;
297 WNDPROC EditWndProc;
298 INT nEditLabelItem;
299 INT nLButtonDownItem; /* tracks item to reset multiselection on WM_LBUTTONUP */
300 DWORD dwHoverTime;
301 HWND hwndToolTip;
303 DWORD cditemmode; /* Keep the custom draw flags for an item/row */
305 DWORD lastKeyPressTimestamp;
306 WPARAM charCode;
307 INT nSearchParamLength;
308 WCHAR szSearchParam[ MAX_PATH ];
309 BOOL bIsDrawing;
310 INT nMeasureItemHeight;
311 INT xTrackLine; /* The x coefficient of the track line or -1 if none */
312 DELAYED_ITEM_EDIT itemEdit; /* Pointer to this structure will be the timer ID */
314 DWORD iVersion; /* CCM_[G,S]ETVERSION */
315 } LISTVIEW_INFO;
318 * constants
320 /* How many we debug buffer to allocate */
321 #define DEBUG_BUFFERS 20
322 /* The size of a single debug bbuffer */
323 #define DEBUG_BUFFER_SIZE 256
325 /* Internal interface to LISTVIEW_HScroll and LISTVIEW_VScroll */
326 #define SB_INTERNAL -1
328 /* maximum size of a label */
329 #define DISP_TEXT_SIZE 512
331 /* padding for items in list and small icon display modes */
332 #define WIDTH_PADDING 12
334 /* padding for items in list, report and small icon display modes */
335 #define HEIGHT_PADDING 1
337 /* offset of items in report display mode */
338 #define REPORT_MARGINX 2
340 /* padding for icon in large icon display mode
341 * ICON_TOP_PADDING_NOTHITABLE - space between top of box and area
342 * that HITTEST will see.
343 * ICON_TOP_PADDING_HITABLE - spacing between above and icon.
344 * ICON_TOP_PADDING - sum of the two above.
345 * ICON_BOTTOM_PADDING - between bottom of icon and top of text
346 * LABEL_HOR_PADDING - between text and sides of box
347 * LABEL_VERT_PADDING - between bottom of text and end of box
349 * ICON_LR_PADDING - additional width above icon size.
350 * ICON_LR_HALF - half of the above value
352 #define ICON_TOP_PADDING_NOTHITABLE 2
353 #define ICON_TOP_PADDING_HITABLE 2
354 #define ICON_TOP_PADDING (ICON_TOP_PADDING_NOTHITABLE + ICON_TOP_PADDING_HITABLE)
355 #define ICON_BOTTOM_PADDING 4
356 #define LABEL_HOR_PADDING 5
357 #define LABEL_VERT_PADDING 7
358 #define ICON_LR_PADDING 16
359 #define ICON_LR_HALF (ICON_LR_PADDING/2)
361 /* default label width for items in list and small icon display modes */
362 #define DEFAULT_LABEL_WIDTH 40
363 /* maximum select rectangle width for empty text item in LV_VIEW_DETAILS */
364 #define MAX_EMPTYTEXT_SELECT_WIDTH 80
366 /* default column width for items in list display mode */
367 #define DEFAULT_COLUMN_WIDTH 128
369 /* Size of "line" scroll for V & H scrolls */
370 #define LISTVIEW_SCROLL_ICON_LINE_SIZE 37
372 /* Padding between image and label */
373 #define IMAGE_PADDING 2
375 /* Padding behind the label */
376 #define TRAILING_LABEL_PADDING 12
377 #define TRAILING_HEADER_PADDING 11
379 /* Border for the icon caption */
380 #define CAPTION_BORDER 2
382 /* Standard DrawText flags */
383 #define LV_ML_DT_FLAGS (DT_TOP | DT_NOPREFIX | DT_EDITCONTROL | DT_CENTER | DT_WORDBREAK | DT_WORD_ELLIPSIS | DT_END_ELLIPSIS)
384 #define LV_FL_DT_FLAGS (DT_TOP | DT_NOPREFIX | DT_EDITCONTROL | DT_CENTER | DT_WORDBREAK | DT_NOCLIP)
385 #define LV_SL_DT_FLAGS (DT_VCENTER | DT_NOPREFIX | DT_EDITCONTROL | DT_SINGLELINE | DT_WORD_ELLIPSIS | DT_END_ELLIPSIS)
387 /* Image index from state */
388 #define STATEIMAGEINDEX(x) (((x) & LVIS_STATEIMAGEMASK) >> 12)
390 /* The time in milliseconds to reset the search in the list */
391 #define KEY_DELAY 450
393 /* Dump the LISTVIEW_INFO structure to the debug channel */
394 #define LISTVIEW_DUMP(iP) do { \
395 TRACE("hwndSelf=%p, clrBk=0x%06x, clrText=0x%06x, clrTextBk=0x%06x, ItemHeight=%d, ItemWidth=%d, Style=0x%08x\n", \
396 iP->hwndSelf, iP->clrBk, iP->clrText, iP->clrTextBk, \
397 iP->nItemHeight, iP->nItemWidth, iP->dwStyle); \
398 TRACE("hwndSelf=%p, himlNor=%p, himlSml=%p, himlState=%p, Focused=%d, Hot=%d, exStyle=0x%08x, Focus=%d\n", \
399 iP->hwndSelf, iP->himlNormal, iP->himlSmall, iP->himlState, \
400 iP->nFocusedItem, iP->nHotItem, iP->dwLvExStyle, iP->bFocus ); \
401 TRACE("hwndSelf=%p, ntmH=%d, icSz.cx=%d, icSz.cy=%d, icSp.cx=%d, icSp.cy=%d, notifyFmt=%d\n", \
402 iP->hwndSelf, iP->ntmHeight, iP->iconSize.cx, iP->iconSize.cy, \
403 iP->iconSpacing.cx, iP->iconSpacing.cy, iP->notifyFormat); \
404 TRACE("hwndSelf=%p, rcList=%s\n", iP->hwndSelf, wine_dbgstr_rect(&iP->rcList)); \
405 } while(0)
407 static const WCHAR themeClass[] = {'L','i','s','t','V','i','e','w',0};
410 * forward declarations
412 static BOOL LISTVIEW_GetItemT(const LISTVIEW_INFO *, LPLVITEMW, BOOL);
413 static void LISTVIEW_GetItemBox(const LISTVIEW_INFO *, INT, LPRECT);
414 static void LISTVIEW_GetItemOrigin(const LISTVIEW_INFO *, INT, LPPOINT);
415 static BOOL LISTVIEW_GetItemPosition(const LISTVIEW_INFO *, INT, LPPOINT);
416 static BOOL LISTVIEW_GetItemRect(const LISTVIEW_INFO *, INT, LPRECT);
417 static INT LISTVIEW_GetLabelWidth(const LISTVIEW_INFO *, INT);
418 static void LISTVIEW_GetOrigin(const LISTVIEW_INFO *, LPPOINT);
419 static BOOL LISTVIEW_GetViewRect(const LISTVIEW_INFO *, LPRECT);
420 static void LISTVIEW_UpdateSize(LISTVIEW_INFO *);
421 static LRESULT LISTVIEW_Command(LISTVIEW_INFO *, WPARAM, LPARAM);
422 static INT LISTVIEW_GetStringWidthT(const LISTVIEW_INFO *, LPCWSTR, BOOL);
423 static BOOL LISTVIEW_KeySelection(LISTVIEW_INFO *, INT, BOOL);
424 static UINT LISTVIEW_GetItemState(const LISTVIEW_INFO *, INT, UINT);
425 static BOOL LISTVIEW_SetItemState(LISTVIEW_INFO *, INT, const LVITEMW *);
426 static LRESULT LISTVIEW_VScroll(LISTVIEW_INFO *, INT, INT, HWND);
427 static LRESULT LISTVIEW_HScroll(LISTVIEW_INFO *, INT, INT, HWND);
428 static BOOL LISTVIEW_EnsureVisible(LISTVIEW_INFO *, INT, BOOL);
429 static HWND CreateEditLabelT(LISTVIEW_INFO *, LPCWSTR, DWORD, BOOL);
430 static HIMAGELIST LISTVIEW_SetImageList(LISTVIEW_INFO *, INT, HIMAGELIST);
431 static INT LISTVIEW_HitTest(const LISTVIEW_INFO *, LPLVHITTESTINFO, BOOL, BOOL);
432 static BOOL LISTVIEW_EndEditLabelT(LISTVIEW_INFO *, BOOL, BOOL);
434 /******** Text handling functions *************************************/
436 /* A text pointer is either NULL, LPSTR_TEXTCALLBACK, or points to a
437 * text string. The string may be ANSI or Unicode, in which case
438 * the boolean isW tells us the type of the string.
440 * The name of the function tell what type of strings it expects:
441 * W: Unicode, T: ANSI/Unicode - function of isW
444 static inline BOOL is_textW(LPCWSTR text)
446 return text != NULL && text != LPSTR_TEXTCALLBACKW;
449 static inline BOOL is_textT(LPCWSTR text, BOOL isW)
451 /* we can ignore isW since LPSTR_TEXTCALLBACKW == LPSTR_TEXTCALLBACKA */
452 return is_textW(text);
455 static inline int textlenT(LPCWSTR text, BOOL isW)
457 return !is_textT(text, isW) ? 0 :
458 isW ? lstrlenW(text) : lstrlenA((LPCSTR)text);
461 static inline void textcpynT(LPWSTR dest, BOOL isDestW, LPCWSTR src, BOOL isSrcW, INT max)
463 if (isDestW)
464 if (isSrcW) lstrcpynW(dest, src, max);
465 else MultiByteToWideChar(CP_ACP, 0, (LPCSTR)src, -1, dest, max);
466 else
467 if (isSrcW) WideCharToMultiByte(CP_ACP, 0, src, -1, (LPSTR)dest, max, NULL, NULL);
468 else lstrcpynA((LPSTR)dest, (LPCSTR)src, max);
471 static inline LPWSTR textdupTtoW(LPCWSTR text, BOOL isW)
473 LPWSTR wstr = (LPWSTR)text;
475 if (!isW && is_textT(text, isW))
477 INT len = MultiByteToWideChar(CP_ACP, 0, (LPCSTR)text, -1, NULL, 0);
478 wstr = Alloc(len * sizeof(WCHAR));
479 if (wstr) MultiByteToWideChar(CP_ACP, 0, (LPCSTR)text, -1, wstr, len);
481 TRACE(" wstr=%s\n", text == LPSTR_TEXTCALLBACKW ? "(callback)" : debugstr_w(wstr));
482 return wstr;
485 static inline void textfreeT(LPWSTR wstr, BOOL isW)
487 if (!isW && is_textT(wstr, isW)) Free (wstr);
491 * dest is a pointer to a Unicode string
492 * src is a pointer to a string (Unicode if isW, ANSI if !isW)
494 static BOOL textsetptrT(LPWSTR *dest, LPCWSTR src, BOOL isW)
496 BOOL bResult = TRUE;
498 if (src == LPSTR_TEXTCALLBACKW)
500 if (is_textW(*dest)) Free(*dest);
501 *dest = LPSTR_TEXTCALLBACKW;
503 else
505 LPWSTR pszText = textdupTtoW(src, isW);
506 if (*dest == LPSTR_TEXTCALLBACKW) *dest = NULL;
507 bResult = Str_SetPtrW(dest, pszText);
508 textfreeT(pszText, isW);
510 return bResult;
514 * compares a Unicode to a Unicode/ANSI text string
516 static inline int textcmpWT(LPCWSTR aw, LPCWSTR bt, BOOL isW)
518 if (!aw) return bt ? -1 : 0;
519 if (!bt) return aw ? 1 : 0;
520 if (aw == LPSTR_TEXTCALLBACKW)
521 return bt == LPSTR_TEXTCALLBACKW ? 1 : -1;
522 if (bt != LPSTR_TEXTCALLBACKW)
524 LPWSTR bw = textdupTtoW(bt, isW);
525 int r = bw ? lstrcmpW(aw, bw) : 1;
526 textfreeT(bw, isW);
527 return r;
530 return 1;
533 static inline int lstrncmpiW(LPCWSTR s1, LPCWSTR s2, int n)
535 int res;
537 n = min(min(n, lstrlenW(s1)), lstrlenW(s2));
538 res = CompareStringW(LOCALE_USER_DEFAULT, NORM_IGNORECASE, s1, n, s2, n);
539 return res ? res - sizeof(WCHAR) : res;
542 /******** Debugging functions *****************************************/
544 static inline LPCSTR debugtext_t(LPCWSTR text, BOOL isW)
546 if (text == LPSTR_TEXTCALLBACKW) return "(callback)";
547 return isW ? debugstr_w(text) : debugstr_a((LPCSTR)text);
550 static inline LPCSTR debugtext_tn(LPCWSTR text, BOOL isW, INT n)
552 if (text == LPSTR_TEXTCALLBACKW) return "(callback)";
553 n = min(textlenT(text, isW), n);
554 return isW ? debugstr_wn(text, n) : debugstr_an((LPCSTR)text, n);
557 static char* debug_getbuf(void)
559 static int index = 0;
560 static char buffers[DEBUG_BUFFERS][DEBUG_BUFFER_SIZE];
561 return buffers[index++ % DEBUG_BUFFERS];
564 static inline const char* debugrange(const RANGE *lprng)
566 if (!lprng) return "(null)";
567 return wine_dbg_sprintf("[%d, %d]", lprng->lower, lprng->upper);
570 static const char* debugscrollinfo(const SCROLLINFO *pScrollInfo)
572 char* buf = debug_getbuf(), *text = buf;
573 int len, size = DEBUG_BUFFER_SIZE;
575 if (pScrollInfo == NULL) return "(null)";
576 len = snprintf(buf, size, "{cbSize=%d, ", pScrollInfo->cbSize);
577 if (len == -1) goto end; buf += len; size -= len;
578 if (pScrollInfo->fMask & SIF_RANGE)
579 len = snprintf(buf, size, "nMin=%d, nMax=%d, ", pScrollInfo->nMin, pScrollInfo->nMax);
580 else len = 0;
581 if (len == -1) goto end; buf += len; size -= len;
582 if (pScrollInfo->fMask & SIF_PAGE)
583 len = snprintf(buf, size, "nPage=%u, ", pScrollInfo->nPage);
584 else len = 0;
585 if (len == -1) goto end; buf += len; size -= len;
586 if (pScrollInfo->fMask & SIF_POS)
587 len = snprintf(buf, size, "nPos=%d, ", pScrollInfo->nPos);
588 else len = 0;
589 if (len == -1) goto end; buf += len; size -= len;
590 if (pScrollInfo->fMask & SIF_TRACKPOS)
591 len = snprintf(buf, size, "nTrackPos=%d, ", pScrollInfo->nTrackPos);
592 else len = 0;
593 if (len == -1) goto end; buf += len; size -= len;
594 goto undo;
595 end:
596 buf = text + strlen(text);
597 undo:
598 if (buf - text > 2) { buf[-2] = '}'; buf[-1] = 0; }
599 return text;
602 static const char* debugnmlistview(const NMLISTVIEW *plvnm)
604 if (!plvnm) return "(null)";
605 return wine_dbg_sprintf("iItem=%d, iSubItem=%d, uNewState=0x%x,"
606 " uOldState=0x%x, uChanged=0x%x, ptAction=%s, lParam=%ld",
607 plvnm->iItem, plvnm->iSubItem, plvnm->uNewState, plvnm->uOldState,
608 plvnm->uChanged, wine_dbgstr_point(&plvnm->ptAction), plvnm->lParam);
611 static const char* debuglvitem_t(const LVITEMW *lpLVItem, BOOL isW)
613 char* buf = debug_getbuf(), *text = buf;
614 int len, size = DEBUG_BUFFER_SIZE;
616 if (lpLVItem == NULL) return "(null)";
617 len = snprintf(buf, size, "{iItem=%d, iSubItem=%d, ", lpLVItem->iItem, lpLVItem->iSubItem);
618 if (len == -1) goto end; buf += len; size -= len;
619 if (lpLVItem->mask & LVIF_STATE)
620 len = snprintf(buf, size, "state=%x, stateMask=%x, ", lpLVItem->state, lpLVItem->stateMask);
621 else len = 0;
622 if (len == -1) goto end; buf += len; size -= len;
623 if (lpLVItem->mask & LVIF_TEXT)
624 len = snprintf(buf, size, "pszText=%s, cchTextMax=%d, ", debugtext_tn(lpLVItem->pszText, isW, 80), lpLVItem->cchTextMax);
625 else len = 0;
626 if (len == -1) goto end; buf += len; size -= len;
627 if (lpLVItem->mask & LVIF_IMAGE)
628 len = snprintf(buf, size, "iImage=%d, ", lpLVItem->iImage);
629 else len = 0;
630 if (len == -1) goto end; buf += len; size -= len;
631 if (lpLVItem->mask & LVIF_PARAM)
632 len = snprintf(buf, size, "lParam=%lx, ", lpLVItem->lParam);
633 else len = 0;
634 if (len == -1) goto end; buf += len; size -= len;
635 if (lpLVItem->mask & LVIF_INDENT)
636 len = snprintf(buf, size, "iIndent=%d, ", lpLVItem->iIndent);
637 else len = 0;
638 if (len == -1) goto end; buf += len; size -= len;
639 goto undo;
640 end:
641 buf = text + strlen(text);
642 undo:
643 if (buf - text > 2) { buf[-2] = '}'; buf[-1] = 0; }
644 return text;
647 static const char* debuglvcolumn_t(const LVCOLUMNW *lpColumn, BOOL isW)
649 char* buf = debug_getbuf(), *text = buf;
650 int len, size = DEBUG_BUFFER_SIZE;
652 if (lpColumn == NULL) return "(null)";
653 len = snprintf(buf, size, "{");
654 if (len == -1) goto end; buf += len; size -= len;
655 if (lpColumn->mask & LVCF_SUBITEM)
656 len = snprintf(buf, size, "iSubItem=%d, ", lpColumn->iSubItem);
657 else len = 0;
658 if (len == -1) goto end; buf += len; size -= len;
659 if (lpColumn->mask & LVCF_FMT)
660 len = snprintf(buf, size, "fmt=%x, ", lpColumn->fmt);
661 else len = 0;
662 if (len == -1) goto end; buf += len; size -= len;
663 if (lpColumn->mask & LVCF_WIDTH)
664 len = snprintf(buf, size, "cx=%d, ", lpColumn->cx);
665 else len = 0;
666 if (len == -1) goto end; buf += len; size -= len;
667 if (lpColumn->mask & LVCF_TEXT)
668 len = snprintf(buf, size, "pszText=%s, cchTextMax=%d, ", debugtext_tn(lpColumn->pszText, isW, 80), lpColumn->cchTextMax);
669 else len = 0;
670 if (len == -1) goto end; buf += len; size -= len;
671 if (lpColumn->mask & LVCF_IMAGE)
672 len = snprintf(buf, size, "iImage=%d, ", lpColumn->iImage);
673 else len = 0;
674 if (len == -1) goto end; buf += len; size -= len;
675 if (lpColumn->mask & LVCF_ORDER)
676 len = snprintf(buf, size, "iOrder=%d, ", lpColumn->iOrder);
677 else len = 0;
678 if (len == -1) goto end; buf += len; size -= len;
679 goto undo;
680 end:
681 buf = text + strlen(text);
682 undo:
683 if (buf - text > 2) { buf[-2] = '}'; buf[-1] = 0; }
684 return text;
687 static const char* debuglvhittestinfo(const LVHITTESTINFO *lpht)
689 if (!lpht) return "(null)";
691 return wine_dbg_sprintf("{pt=%s, flags=0x%x, iItem=%d, iSubItem=%d}",
692 wine_dbgstr_point(&lpht->pt), lpht->flags, lpht->iItem, lpht->iSubItem);
695 /* Return the corresponding text for a given scroll value */
696 static inline LPCSTR debugscrollcode(int nScrollCode)
698 switch(nScrollCode)
700 case SB_LINELEFT: return "SB_LINELEFT";
701 case SB_LINERIGHT: return "SB_LINERIGHT";
702 case SB_PAGELEFT: return "SB_PAGELEFT";
703 case SB_PAGERIGHT: return "SB_PAGERIGHT";
704 case SB_THUMBPOSITION: return "SB_THUMBPOSITION";
705 case SB_THUMBTRACK: return "SB_THUMBTRACK";
706 case SB_ENDSCROLL: return "SB_ENDSCROLL";
707 case SB_INTERNAL: return "SB_INTERNAL";
708 default: return "unknown";
713 /******** Notification functions ************************************/
715 static int get_ansi_notification(UINT unicodeNotificationCode)
717 switch (unicodeNotificationCode)
719 case LVN_BEGINLABELEDITW: return LVN_BEGINLABELEDITA;
720 case LVN_ENDLABELEDITW: return LVN_ENDLABELEDITA;
721 case LVN_GETDISPINFOW: return LVN_GETDISPINFOA;
722 case LVN_SETDISPINFOW: return LVN_SETDISPINFOA;
723 case LVN_ODFINDITEMW: return LVN_ODFINDITEMA;
724 case LVN_GETINFOTIPW: return LVN_GETINFOTIPA;
725 /* header forwards */
726 case HDN_TRACKW: return HDN_TRACKA;
727 case HDN_ENDTRACKW: return HDN_ENDTRACKA;
728 case HDN_BEGINDRAG: return HDN_BEGINDRAG;
729 case HDN_ENDDRAG: return HDN_ENDDRAG;
730 case HDN_ITEMCHANGINGW: return HDN_ITEMCHANGINGA;
731 case HDN_ITEMCHANGEDW: return HDN_ITEMCHANGEDA;
732 case HDN_ITEMCLICKW: return HDN_ITEMCLICKA;
733 case HDN_DIVIDERDBLCLICKW: return HDN_DIVIDERDBLCLICKA;
735 ERR("unknown notification %x\n", unicodeNotificationCode);
736 assert(FALSE);
737 return 0;
740 /* forwards header notifications to listview parent */
741 static LRESULT notify_forward_header(const LISTVIEW_INFO *infoPtr, const NMHEADERW *lpnmh)
743 NMHEADERA nmhA;
744 HDITEMA hditema;
745 HD_TEXTFILTERA textfilter;
746 LPSTR text = NULL, filter = NULL;
747 LRESULT ret;
749 /* on unicode format exit earlier */
750 if (infoPtr->notifyFormat == NFR_UNICODE)
751 return SendMessageW(infoPtr->hwndNotify, WM_NOTIFY,
752 (WPARAM)lpnmh->hdr.idFrom, (LPARAM)lpnmh);
754 /* header always supplies unicode notifications,
755 all we have to do is to convert strings to ANSI */
756 nmhA = *(NMHEADERA*)lpnmh;
757 if (lpnmh->pitem)
759 hditema = *(HDITEMA*)lpnmh->pitem;
760 nmhA.pitem = &hditema;
761 /* convert item text */
762 if (lpnmh->pitem->mask & HDI_TEXT)
764 hditema.pszText = NULL;
765 Str_SetPtrWtoA(&hditema.pszText, lpnmh->pitem->pszText);
766 text = hditema.pszText;
768 /* convert filter text */
769 if ((lpnmh->pitem->mask & HDI_FILTER) && (lpnmh->pitem->type == HDFT_ISSTRING) &&
770 lpnmh->pitem->pvFilter)
772 hditema.pvFilter = &textfilter;
773 textfilter = *(HD_TEXTFILTERA*)(lpnmh->pitem->pvFilter);
774 textfilter.pszText = NULL;
775 Str_SetPtrWtoA(&textfilter.pszText, ((HD_TEXTFILTERW*)lpnmh->pitem->pvFilter)->pszText);
776 filter = textfilter.pszText;
779 nmhA.hdr.code = get_ansi_notification(lpnmh->hdr.code);
781 ret = SendMessageW(infoPtr->hwndNotify, WM_NOTIFY,
782 (WPARAM)nmhA.hdr.idFrom, (LPARAM)&nmhA);
784 /* cleanup */
785 Free(text);
786 Free(filter);
788 return ret;
791 static LRESULT notify_hdr(const LISTVIEW_INFO *infoPtr, INT code, LPNMHDR pnmh)
793 LRESULT result;
795 TRACE("(code=%d)\n", code);
797 pnmh->hwndFrom = infoPtr->hwndSelf;
798 pnmh->idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
799 pnmh->code = code;
800 result = SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, pnmh->idFrom, (LPARAM)pnmh);
802 TRACE(" <= %ld\n", result);
804 return result;
807 static inline BOOL notify(const LISTVIEW_INFO *infoPtr, INT code)
809 NMHDR nmh;
810 HWND hwnd = infoPtr->hwndSelf;
811 notify_hdr(infoPtr, code, &nmh);
812 return IsWindow(hwnd);
815 static inline void notify_itemactivate(const LISTVIEW_INFO *infoPtr, const LVHITTESTINFO *htInfo)
817 NMITEMACTIVATE nmia;
818 LVITEMW item;
820 if (htInfo) {
821 nmia.uNewState = 0;
822 nmia.uOldState = 0;
823 nmia.uChanged = 0;
824 nmia.uKeyFlags = 0;
826 item.mask = LVIF_PARAM|LVIF_STATE;
827 item.iItem = htInfo->iItem;
828 item.iSubItem = 0;
829 if (LISTVIEW_GetItemT(infoPtr, &item, TRUE)) {
830 nmia.lParam = item.lParam;
831 nmia.uOldState = item.state;
832 nmia.uNewState = item.state | LVIS_ACTIVATING;
833 nmia.uChanged = LVIF_STATE;
836 nmia.iItem = htInfo->iItem;
837 nmia.iSubItem = htInfo->iSubItem;
838 nmia.ptAction = htInfo->pt;
840 if (GetKeyState(VK_SHIFT) & 0x8000) nmia.uKeyFlags |= LVKF_SHIFT;
841 if (GetKeyState(VK_CONTROL) & 0x8000) nmia.uKeyFlags |= LVKF_CONTROL;
842 if (GetKeyState(VK_MENU) & 0x8000) nmia.uKeyFlags |= LVKF_ALT;
844 notify_hdr(infoPtr, LVN_ITEMACTIVATE, (LPNMHDR)&nmia);
847 static inline LRESULT notify_listview(const LISTVIEW_INFO *infoPtr, INT code, LPNMLISTVIEW plvnm)
849 TRACE("(code=%d, plvnm=%s)\n", code, debugnmlistview(plvnm));
850 return notify_hdr(infoPtr, code, (LPNMHDR)plvnm);
853 static BOOL notify_click(const LISTVIEW_INFO *infoPtr, INT code, const LVHITTESTINFO *lvht)
855 NMITEMACTIVATE nmia;
856 LVITEMW item;
857 HWND hwnd = infoPtr->hwndSelf;
859 TRACE("code=%d, lvht=%s\n", code, debuglvhittestinfo(lvht));
860 ZeroMemory(&nmia, sizeof(nmia));
861 nmia.iItem = lvht->iItem;
862 nmia.iSubItem = lvht->iSubItem;
863 nmia.ptAction = lvht->pt;
864 item.mask = LVIF_PARAM;
865 item.iItem = lvht->iItem;
866 item.iSubItem = 0;
867 if (LISTVIEW_GetItemT(infoPtr, &item, TRUE)) nmia.lParam = item.lParam;
868 notify_hdr(infoPtr, code, (LPNMHDR)&nmia);
869 return IsWindow(hwnd);
872 static BOOL notify_deleteitem(const LISTVIEW_INFO *infoPtr, INT nItem)
874 NMLISTVIEW nmlv;
875 LVITEMW item;
876 HWND hwnd = infoPtr->hwndSelf;
878 ZeroMemory(&nmlv, sizeof (NMLISTVIEW));
879 nmlv.iItem = nItem;
880 item.mask = LVIF_PARAM;
881 item.iItem = nItem;
882 item.iSubItem = 0;
883 if (LISTVIEW_GetItemT(infoPtr, &item, TRUE)) nmlv.lParam = item.lParam;
884 notify_listview(infoPtr, LVN_DELETEITEM, &nmlv);
885 return IsWindow(hwnd);
889 Send notification. depends on dispinfoW having same
890 structure as dispinfoA.
891 infoPtr : listview struct
892 notificationCode : *Unicode* notification code
893 pdi : dispinfo structure (can be unicode or ansi)
894 isW : TRUE if dispinfo is Unicode
896 static BOOL notify_dispinfoT(const LISTVIEW_INFO *infoPtr, UINT notificationCode, LPNMLVDISPINFOW pdi, BOOL isW)
898 BOOL bResult = FALSE;
899 BOOL convertToAnsi = FALSE, convertToUnicode = FALSE;
900 INT cchTempBufMax = 0, savCchTextMax = 0;
901 UINT realNotifCode;
902 LPWSTR pszTempBuf = NULL, savPszText = NULL;
904 if ((pdi->item.mask & LVIF_TEXT) && is_textT(pdi->item.pszText, isW))
906 convertToAnsi = (isW && infoPtr->notifyFormat == NFR_ANSI);
907 convertToUnicode = (!isW && infoPtr->notifyFormat == NFR_UNICODE);
910 if (convertToAnsi || convertToUnicode)
912 if (notificationCode != LVN_GETDISPINFOW)
914 cchTempBufMax = convertToUnicode ?
915 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pdi->item.pszText, -1, NULL, 0):
916 WideCharToMultiByte(CP_ACP, 0, pdi->item.pszText, -1, NULL, 0, NULL, NULL);
918 else
920 cchTempBufMax = pdi->item.cchTextMax;
921 *pdi->item.pszText = 0; /* make sure we don't process garbage */
924 pszTempBuf = Alloc( (convertToUnicode ? sizeof(WCHAR) : sizeof(CHAR)) * cchTempBufMax);
925 if (!pszTempBuf) return FALSE;
927 if (convertToUnicode)
928 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pdi->item.pszText, -1,
929 pszTempBuf, cchTempBufMax);
930 else
931 WideCharToMultiByte(CP_ACP, 0, pdi->item.pszText, -1, (LPSTR) pszTempBuf,
932 cchTempBufMax, NULL, NULL);
934 savCchTextMax = pdi->item.cchTextMax;
935 savPszText = pdi->item.pszText;
936 pdi->item.pszText = pszTempBuf;
937 pdi->item.cchTextMax = cchTempBufMax;
940 if (infoPtr->notifyFormat == NFR_ANSI)
941 realNotifCode = get_ansi_notification(notificationCode);
942 else
943 realNotifCode = notificationCode;
944 TRACE(" pdi->item=%s\n", debuglvitem_t(&pdi->item, infoPtr->notifyFormat != NFR_ANSI));
945 bResult = notify_hdr(infoPtr, realNotifCode, &pdi->hdr);
947 if (convertToUnicode || convertToAnsi)
949 if (convertToUnicode) /* note : pointer can be changed by app ! */
950 WideCharToMultiByte(CP_ACP, 0, pdi->item.pszText, -1, (LPSTR) savPszText,
951 savCchTextMax, NULL, NULL);
952 else
953 MultiByteToWideChar(CP_ACP, 0, (LPSTR) pdi->item.pszText, -1,
954 savPszText, savCchTextMax);
955 pdi->item.pszText = savPszText; /* restores our buffer */
956 pdi->item.cchTextMax = savCchTextMax;
957 Free (pszTempBuf);
959 return bResult;
962 static void customdraw_fill(NMLVCUSTOMDRAW *lpnmlvcd, const LISTVIEW_INFO *infoPtr, HDC hdc,
963 const RECT *rcBounds, const LVITEMW *lplvItem)
965 ZeroMemory(lpnmlvcd, sizeof(NMLVCUSTOMDRAW));
966 lpnmlvcd->nmcd.hdc = hdc;
967 lpnmlvcd->nmcd.rc = *rcBounds;
968 lpnmlvcd->clrTextBk = infoPtr->clrTextBk;
969 lpnmlvcd->clrText = infoPtr->clrText;
970 if (!lplvItem) return;
971 lpnmlvcd->nmcd.dwItemSpec = lplvItem->iItem + 1;
972 lpnmlvcd->iSubItem = lplvItem->iSubItem;
973 if (lplvItem->state & LVIS_SELECTED) lpnmlvcd->nmcd.uItemState |= CDIS_SELECTED;
974 if (lplvItem->state & LVIS_FOCUSED) lpnmlvcd->nmcd.uItemState |= CDIS_FOCUS;
975 if (lplvItem->iItem == infoPtr->nHotItem) lpnmlvcd->nmcd.uItemState |= CDIS_HOT;
976 lpnmlvcd->nmcd.lItemlParam = lplvItem->lParam;
979 static inline DWORD notify_customdraw (const LISTVIEW_INFO *infoPtr, DWORD dwDrawStage, NMLVCUSTOMDRAW *lpnmlvcd)
981 BOOL isForItem = (lpnmlvcd->nmcd.dwItemSpec != 0);
982 DWORD result;
984 lpnmlvcd->nmcd.dwDrawStage = dwDrawStage;
985 if (isForItem) lpnmlvcd->nmcd.dwDrawStage |= CDDS_ITEM;
986 if (lpnmlvcd->iSubItem) lpnmlvcd->nmcd.dwDrawStage |= CDDS_SUBITEM;
987 if (isForItem) lpnmlvcd->nmcd.dwItemSpec--;
988 result = notify_hdr(infoPtr, NM_CUSTOMDRAW, &lpnmlvcd->nmcd.hdr);
989 if (isForItem) lpnmlvcd->nmcd.dwItemSpec++;
990 return result;
993 static void prepaint_setup (const LISTVIEW_INFO *infoPtr, HDC hdc, NMLVCUSTOMDRAW *lpnmlvcd, BOOL SubItem)
995 if (lpnmlvcd->clrTextBk == CLR_DEFAULT)
996 lpnmlvcd->clrTextBk = comctl32_color.clrWindow;
997 if (lpnmlvcd->clrText == CLR_DEFAULT)
998 lpnmlvcd->clrText = comctl32_color.clrWindowText;
1000 /* apparently, for selected items, we have to override the returned values */
1001 if (!SubItem)
1003 if (lpnmlvcd->nmcd.uItemState & CDIS_SELECTED)
1005 if (infoPtr->bFocus)
1007 lpnmlvcd->clrTextBk = comctl32_color.clrHighlight;
1008 lpnmlvcd->clrText = comctl32_color.clrHighlightText;
1010 else if (infoPtr->dwStyle & LVS_SHOWSELALWAYS)
1012 lpnmlvcd->clrTextBk = comctl32_color.clr3dFace;
1013 lpnmlvcd->clrText = comctl32_color.clrBtnText;
1018 /* Set the text attributes */
1019 if (lpnmlvcd->clrTextBk != CLR_NONE)
1021 SetBkMode(hdc, OPAQUE);
1022 SetBkColor(hdc,lpnmlvcd->clrTextBk);
1024 else
1025 SetBkMode(hdc, TRANSPARENT);
1026 SetTextColor(hdc, lpnmlvcd->clrText);
1029 static inline DWORD notify_postpaint (const LISTVIEW_INFO *infoPtr, NMLVCUSTOMDRAW *lpnmlvcd)
1031 return notify_customdraw(infoPtr, CDDS_POSTPAINT, lpnmlvcd);
1034 /* returns TRUE when repaint needed, FALSE otherwise */
1035 static BOOL notify_measureitem(LISTVIEW_INFO *infoPtr)
1037 MEASUREITEMSTRUCT mis;
1038 mis.CtlType = ODT_LISTVIEW;
1039 mis.CtlID = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
1040 mis.itemID = -1;
1041 mis.itemWidth = 0;
1042 mis.itemData = 0;
1043 mis.itemHeight= infoPtr->nItemHeight;
1044 SendMessageW(infoPtr->hwndNotify, WM_MEASUREITEM, mis.CtlID, (LPARAM)&mis);
1045 if (infoPtr->nItemHeight != max(mis.itemHeight, 1))
1047 infoPtr->nMeasureItemHeight = infoPtr->nItemHeight = max(mis.itemHeight, 1);
1048 return TRUE;
1050 return FALSE;
1053 /******** Item iterator functions **********************************/
1055 static RANGES ranges_create(int count);
1056 static void ranges_destroy(RANGES ranges);
1057 static BOOL ranges_add(RANGES ranges, RANGE range);
1058 static BOOL ranges_del(RANGES ranges, RANGE range);
1059 static void ranges_dump(RANGES ranges);
1061 static inline BOOL ranges_additem(RANGES ranges, INT nItem)
1063 RANGE range = { nItem, nItem + 1 };
1065 return ranges_add(ranges, range);
1068 static inline BOOL ranges_delitem(RANGES ranges, INT nItem)
1070 RANGE range = { nItem, nItem + 1 };
1072 return ranges_del(ranges, range);
1075 /***
1076 * ITERATOR DOCUMENTATION
1078 * The iterator functions allow for easy, and convenient iteration
1079 * over items of interest in the list. Typically, you create a
1080 * iterator, use it, and destroy it, as such:
1081 * ITERATOR i;
1083 * iterator_xxxitems(&i, ...);
1084 * while (iterator_{prev,next}(&i)
1086 * //code which uses i.nItem
1088 * iterator_destroy(&i);
1090 * where xxx is either: framed, or visible.
1091 * Note that it is important that the code destroys the iterator
1092 * after it's done with it, as the creation of the iterator may
1093 * allocate memory, which thus needs to be freed.
1095 * You can iterate both forwards, and backwards through the list,
1096 * by using iterator_next or iterator_prev respectively.
1098 * Lower numbered items are draw on top of higher number items in
1099 * LVS_ICON, and LVS_SMALLICON (which are the only modes where
1100 * items may overlap). So, to test items, you should use
1101 * iterator_next
1102 * which lists the items top to bottom (in Z-order).
1103 * For drawing items, you should use
1104 * iterator_prev
1105 * which lists the items bottom to top (in Z-order).
1106 * If you keep iterating over the items after the end-of-items
1107 * marker (-1) is returned, the iterator will start from the
1108 * beginning. Typically, you don't need to test for -1,
1109 * because iterator_{next,prev} will return TRUE if more items
1110 * are to be iterated over, or FALSE otherwise.
1112 * Note: the iterator is defined to be bidirectional. That is,
1113 * any number of prev followed by any number of next, or
1114 * five versa, should leave the iterator at the same item:
1115 * prev * n, next * n = next * n, prev * n
1117 * The iterator has a notion of an out-of-order, special item,
1118 * which sits at the start of the list. This is used in
1119 * LVS_ICON, and LVS_SMALLICON mode to handle the focused item,
1120 * which needs to be first, as it may overlap other items.
1122 * The code is a bit messy because we have:
1123 * - a special item to deal with
1124 * - simple range, or composite range
1125 * - empty range.
1126 * If you find bugs, or want to add features, please make sure you
1127 * always check/modify *both* iterator_prev, and iterator_next.
1130 /****
1131 * This function iterates through the items in increasing order,
1132 * but prefixed by the special item, then -1. That is:
1133 * special, 1, 2, 3, ..., n, -1.
1134 * Each item is listed only once.
1136 static inline BOOL iterator_next(ITERATOR* i)
1138 if (i->nItem == -1)
1140 i->nItem = i->nSpecial;
1141 if (i->nItem != -1) return TRUE;
1143 if (i->nItem == i->nSpecial)
1145 if (i->ranges) i->index = 0;
1146 goto pickarange;
1149 i->nItem++;
1150 testitem:
1151 if (i->nItem == i->nSpecial) i->nItem++;
1152 if (i->nItem < i->range.upper) return TRUE;
1154 pickarange:
1155 if (i->ranges)
1157 if (i->index < DPA_GetPtrCount(i->ranges->hdpa))
1158 i->range = *(RANGE*)DPA_GetPtr(i->ranges->hdpa, i->index++);
1159 else goto end;
1161 else if (i->nItem >= i->range.upper) goto end;
1163 i->nItem = i->range.lower;
1164 if (i->nItem >= 0) goto testitem;
1165 end:
1166 i->nItem = -1;
1167 return FALSE;
1170 /****
1171 * This function iterates through the items in decreasing order,
1172 * followed by the special item, then -1. That is:
1173 * n, n-1, ..., 3, 2, 1, special, -1.
1174 * Each item is listed only once.
1176 static inline BOOL iterator_prev(ITERATOR* i)
1178 BOOL start = FALSE;
1180 if (i->nItem == -1)
1182 start = TRUE;
1183 if (i->ranges) i->index = DPA_GetPtrCount(i->ranges->hdpa);
1184 goto pickarange;
1186 if (i->nItem == i->nSpecial)
1188 i->nItem = -1;
1189 return FALSE;
1192 testitem:
1193 i->nItem--;
1194 if (i->nItem == i->nSpecial) i->nItem--;
1195 if (i->nItem >= i->range.lower) return TRUE;
1197 pickarange:
1198 if (i->ranges)
1200 if (i->index > 0)
1201 i->range = *(RANGE*)DPA_GetPtr(i->ranges->hdpa, --i->index);
1202 else goto end;
1204 else if (!start && i->nItem < i->range.lower) goto end;
1206 i->nItem = i->range.upper;
1207 if (i->nItem > 0) goto testitem;
1208 end:
1209 return (i->nItem = i->nSpecial) != -1;
1212 static RANGE iterator_range(const ITERATOR *i)
1214 RANGE range;
1216 if (!i->ranges) return i->range;
1218 if (DPA_GetPtrCount(i->ranges->hdpa) > 0)
1220 range.lower = (*(RANGE*)DPA_GetPtr(i->ranges->hdpa, 0)).lower;
1221 range.upper = (*(RANGE*)DPA_GetPtr(i->ranges->hdpa, DPA_GetPtrCount(i->ranges->hdpa) - 1)).upper;
1223 else range.lower = range.upper = 0;
1225 return range;
1228 /***
1229 * Releases resources associated with this ierator.
1231 static inline void iterator_destroy(const ITERATOR *i)
1233 ranges_destroy(i->ranges);
1236 /***
1237 * Create an empty iterator.
1239 static inline BOOL iterator_empty(ITERATOR* i)
1241 ZeroMemory(i, sizeof(*i));
1242 i->nItem = i->nSpecial = i->range.lower = i->range.upper = -1;
1243 return TRUE;
1246 /***
1247 * Create an iterator over a range.
1249 static inline BOOL iterator_rangeitems(ITERATOR* i, RANGE range)
1251 iterator_empty(i);
1252 i->range = range;
1253 return TRUE;
1256 /***
1257 * Create an iterator over a bunch of ranges.
1258 * Please note that the iterator will take ownership of the ranges,
1259 * and will free them upon destruction.
1261 static inline BOOL iterator_rangesitems(ITERATOR* i, RANGES ranges)
1263 iterator_empty(i);
1264 i->ranges = ranges;
1265 return TRUE;
1268 /***
1269 * Creates an iterator over the items which intersect lprc.
1271 static BOOL iterator_frameditems(ITERATOR* i, const LISTVIEW_INFO* infoPtr, const RECT *lprc)
1273 RECT frame = *lprc, rcItem, rcTemp;
1274 POINT Origin;
1276 /* in case we fail, we want to return an empty iterator */
1277 if (!iterator_empty(i)) return FALSE;
1279 LISTVIEW_GetOrigin(infoPtr, &Origin);
1281 TRACE("(lprc=%s)\n", wine_dbgstr_rect(lprc));
1282 OffsetRect(&frame, -Origin.x, -Origin.y);
1284 if (infoPtr->uView == LV_VIEW_ICON || infoPtr->uView == LV_VIEW_SMALLICON)
1286 INT nItem;
1288 if (infoPtr->uView == LV_VIEW_ICON && infoPtr->nFocusedItem != -1)
1290 LISTVIEW_GetItemBox(infoPtr, infoPtr->nFocusedItem, &rcItem);
1291 if (IntersectRect(&rcTemp, &rcItem, lprc))
1292 i->nSpecial = infoPtr->nFocusedItem;
1294 if (!(iterator_rangesitems(i, ranges_create(50)))) return FALSE;
1295 /* to do better here, we need to have PosX, and PosY sorted */
1296 TRACE("building icon ranges:\n");
1297 for (nItem = 0; nItem < infoPtr->nItemCount; nItem++)
1299 rcItem.left = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosX, nItem);
1300 rcItem.top = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosY, nItem);
1301 rcItem.right = rcItem.left + infoPtr->nItemWidth;
1302 rcItem.bottom = rcItem.top + infoPtr->nItemHeight;
1303 if (IntersectRect(&rcTemp, &rcItem, &frame))
1304 ranges_additem(i->ranges, nItem);
1306 return TRUE;
1308 else if (infoPtr->uView == LV_VIEW_DETAILS)
1310 RANGE range;
1312 if (frame.left >= infoPtr->nItemWidth) return TRUE;
1313 if (frame.top >= infoPtr->nItemHeight * infoPtr->nItemCount) return TRUE;
1315 range.lower = max(frame.top / infoPtr->nItemHeight, 0);
1316 range.upper = min((frame.bottom - 1) / infoPtr->nItemHeight, infoPtr->nItemCount - 1) + 1;
1317 if (range.upper <= range.lower) return TRUE;
1318 if (!iterator_rangeitems(i, range)) return FALSE;
1319 TRACE(" report=%s\n", debugrange(&i->range));
1321 else
1323 INT nPerCol = max((infoPtr->rcList.bottom - infoPtr->rcList.top) / infoPtr->nItemHeight, 1);
1324 INT nFirstRow = max(frame.top / infoPtr->nItemHeight, 0);
1325 INT nLastRow = min((frame.bottom - 1) / infoPtr->nItemHeight, nPerCol - 1);
1326 INT nFirstCol;
1327 INT nLastCol;
1328 INT lower;
1329 RANGE item_range;
1330 INT nCol;
1332 if (infoPtr->nItemWidth)
1334 nFirstCol = max(frame.left / infoPtr->nItemWidth, 0);
1335 nLastCol = min((frame.right - 1) / infoPtr->nItemWidth, (infoPtr->nItemCount + nPerCol - 1) / nPerCol);
1337 else
1339 nFirstCol = max(frame.left, 0);
1340 nLastCol = min(frame.right - 1, (infoPtr->nItemCount + nPerCol - 1) / nPerCol);
1343 lower = nFirstCol * nPerCol + nFirstRow;
1345 TRACE("nPerCol=%d, nFirstRow=%d, nLastRow=%d, nFirstCol=%d, nLastCol=%d, lower=%d\n",
1346 nPerCol, nFirstRow, nLastRow, nFirstCol, nLastCol, lower);
1348 if (nLastCol < nFirstCol || nLastRow < nFirstRow) return TRUE;
1350 if (!(iterator_rangesitems(i, ranges_create(nLastCol - nFirstCol + 1)))) return FALSE;
1351 TRACE("building list ranges:\n");
1352 for (nCol = nFirstCol; nCol <= nLastCol; nCol++)
1354 item_range.lower = nCol * nPerCol + nFirstRow;
1355 if(item_range.lower >= infoPtr->nItemCount) break;
1356 item_range.upper = min(nCol * nPerCol + nLastRow + 1, infoPtr->nItemCount);
1357 TRACE(" list=%s\n", debugrange(&item_range));
1358 ranges_add(i->ranges, item_range);
1362 return TRUE;
1365 /***
1366 * Creates an iterator over the items which intersect the visible region of hdc.
1368 static BOOL iterator_visibleitems(ITERATOR *i, const LISTVIEW_INFO *infoPtr, HDC hdc)
1370 POINT Origin, Position;
1371 RECT rcItem, rcClip;
1372 INT rgntype;
1374 rgntype = GetClipBox(hdc, &rcClip);
1375 if (rgntype == NULLREGION) return iterator_empty(i);
1376 if (!iterator_frameditems(i, infoPtr, &rcClip)) return FALSE;
1377 if (rgntype == SIMPLEREGION) return TRUE;
1379 /* first deal with the special item */
1380 if (i->nSpecial != -1)
1382 LISTVIEW_GetItemBox(infoPtr, i->nSpecial, &rcItem);
1383 if (!RectVisible(hdc, &rcItem)) i->nSpecial = -1;
1386 /* if we can't deal with the region, we'll just go with the simple range */
1387 LISTVIEW_GetOrigin(infoPtr, &Origin);
1388 TRACE("building visible range:\n");
1389 if (!i->ranges && i->range.lower < i->range.upper)
1391 if (!(i->ranges = ranges_create(50))) return TRUE;
1392 if (!ranges_add(i->ranges, i->range))
1394 ranges_destroy(i->ranges);
1395 i->ranges = 0;
1396 return TRUE;
1400 /* now delete the invisible items from the list */
1401 while(iterator_next(i))
1403 LISTVIEW_GetItemOrigin(infoPtr, i->nItem, &Position);
1404 rcItem.left = Position.x + Origin.x;
1405 rcItem.top = Position.y + Origin.y;
1406 rcItem.right = rcItem.left + infoPtr->nItemWidth;
1407 rcItem.bottom = rcItem.top + infoPtr->nItemHeight;
1408 if (!RectVisible(hdc, &rcItem))
1409 ranges_delitem(i->ranges, i->nItem);
1411 /* the iterator should restart on the next iterator_next */
1412 TRACE("done\n");
1414 return TRUE;
1417 /******** Misc helper functions ************************************/
1419 static inline LRESULT CallWindowProcT(WNDPROC proc, HWND hwnd, UINT uMsg,
1420 WPARAM wParam, LPARAM lParam, BOOL isW)
1422 if (isW) return CallWindowProcW(proc, hwnd, uMsg, wParam, lParam);
1423 else return CallWindowProcA(proc, hwnd, uMsg, wParam, lParam);
1426 static inline BOOL is_autoarrange(const LISTVIEW_INFO *infoPtr)
1428 return ((infoPtr->dwStyle & LVS_AUTOARRANGE) || infoPtr->bAutoarrange) &&
1429 (infoPtr->uView == LV_VIEW_ICON || infoPtr->uView == LV_VIEW_SMALLICON);
1432 static void toggle_checkbox_state(LISTVIEW_INFO *infoPtr, INT nItem)
1434 DWORD state = STATEIMAGEINDEX(LISTVIEW_GetItemState(infoPtr, nItem, LVIS_STATEIMAGEMASK));
1435 if(state == 1 || state == 2)
1437 LVITEMW lvitem;
1438 state ^= 3;
1439 lvitem.state = INDEXTOSTATEIMAGEMASK(state);
1440 lvitem.stateMask = LVIS_STATEIMAGEMASK;
1441 LISTVIEW_SetItemState(infoPtr, nItem, &lvitem);
1445 /* this should be called after window style got updated,
1446 it used to reset view state to match current window style */
1447 static inline void map_style_view(LISTVIEW_INFO *infoPtr)
1449 switch (infoPtr->dwStyle & LVS_TYPEMASK)
1451 case LVS_ICON:
1452 infoPtr->uView = LV_VIEW_ICON;
1453 break;
1454 case LVS_REPORT:
1455 infoPtr->uView = LV_VIEW_DETAILS;
1456 break;
1457 case LVS_SMALLICON:
1458 infoPtr->uView = LV_VIEW_SMALLICON;
1459 break;
1460 case LVS_LIST:
1461 infoPtr->uView = LV_VIEW_LIST;
1465 /* computes next item id value */
1466 static DWORD get_next_itemid(const LISTVIEW_INFO *infoPtr)
1468 INT count = DPA_GetPtrCount(infoPtr->hdpaItemIds);
1470 if (count > 0)
1472 ITEM_ID *lpID = DPA_GetPtr(infoPtr->hdpaItemIds, count - 1);
1473 return lpID->id + 1;
1475 return 0;
1478 /******** Internal API functions ************************************/
1480 static inline COLUMN_INFO * LISTVIEW_GetColumnInfo(const LISTVIEW_INFO *infoPtr, INT nSubItem)
1482 static COLUMN_INFO mainItem;
1484 if (nSubItem == 0 && DPA_GetPtrCount(infoPtr->hdpaColumns) == 0) return &mainItem;
1485 assert (nSubItem >= 0 && nSubItem < DPA_GetPtrCount(infoPtr->hdpaColumns));
1486 return DPA_GetPtr(infoPtr->hdpaColumns, nSubItem);
1489 static INT LISTVIEW_CreateHeader(LISTVIEW_INFO *infoPtr)
1491 DWORD dFlags = WS_CHILD | HDS_HORZ | HDS_FULLDRAG | HDS_DRAGDROP;
1492 HINSTANCE hInst;
1494 if (infoPtr->hwndHeader) return 0;
1496 TRACE("Creating header for list %p\n", infoPtr->hwndSelf);
1498 /* setup creation flags */
1499 dFlags |= (LVS_NOSORTHEADER & infoPtr->dwStyle) ? 0 : HDS_BUTTONS;
1500 dFlags |= (LVS_NOCOLUMNHEADER & infoPtr->dwStyle) ? HDS_HIDDEN : 0;
1502 hInst = (HINSTANCE)GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_HINSTANCE);
1504 /* create header */
1505 infoPtr->hwndHeader = CreateWindowW(WC_HEADERW, NULL, dFlags,
1506 0, 0, 0, 0, infoPtr->hwndSelf, NULL, hInst, NULL);
1507 if (!infoPtr->hwndHeader) return -1;
1509 /* set header unicode format */
1510 SendMessageW(infoPtr->hwndHeader, HDM_SETUNICODEFORMAT, TRUE, 0);
1512 /* set header font */
1513 SendMessageW(infoPtr->hwndHeader, WM_SETFONT, (WPARAM)infoPtr->hFont, (LPARAM)TRUE);
1515 LISTVIEW_UpdateSize(infoPtr);
1517 return 0;
1520 static inline void LISTVIEW_GetHeaderRect(const LISTVIEW_INFO *infoPtr, INT nSubItem, LPRECT lprc)
1522 *lprc = LISTVIEW_GetColumnInfo(infoPtr, nSubItem)->rcHeader;
1525 static inline BOOL LISTVIEW_GetItemW(const LISTVIEW_INFO *infoPtr, LPLVITEMW lpLVItem)
1527 return LISTVIEW_GetItemT(infoPtr, lpLVItem, TRUE);
1530 /* used to handle collapse main item column case */
1531 static inline BOOL LISTVIEW_DrawFocusRect(const LISTVIEW_INFO *infoPtr, HDC hdc)
1533 return (infoPtr->rcFocus.left < infoPtr->rcFocus.right) ?
1534 DrawFocusRect(hdc, &infoPtr->rcFocus) : FALSE;
1537 /* Listview invalidation functions: use _only_ these functions to invalidate */
1539 static inline BOOL is_redrawing(const LISTVIEW_INFO *infoPtr)
1541 return infoPtr->bRedraw;
1544 static inline void LISTVIEW_InvalidateRect(const LISTVIEW_INFO *infoPtr, const RECT* rect)
1546 if(!is_redrawing(infoPtr)) return;
1547 TRACE(" invalidating rect=%s\n", wine_dbgstr_rect(rect));
1548 InvalidateRect(infoPtr->hwndSelf, rect, TRUE);
1551 static inline void LISTVIEW_InvalidateItem(const LISTVIEW_INFO *infoPtr, INT nItem)
1553 RECT rcBox;
1555 if(!is_redrawing(infoPtr)) return;
1556 LISTVIEW_GetItemBox(infoPtr, nItem, &rcBox);
1557 LISTVIEW_InvalidateRect(infoPtr, &rcBox);
1560 static inline void LISTVIEW_InvalidateSubItem(const LISTVIEW_INFO *infoPtr, INT nItem, INT nSubItem)
1562 POINT Origin, Position;
1563 RECT rcBox;
1565 if(!is_redrawing(infoPtr)) return;
1566 assert (infoPtr->uView == LV_VIEW_DETAILS);
1567 LISTVIEW_GetOrigin(infoPtr, &Origin);
1568 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position);
1569 LISTVIEW_GetHeaderRect(infoPtr, nSubItem, &rcBox);
1570 rcBox.top = 0;
1571 rcBox.bottom = infoPtr->nItemHeight;
1572 OffsetRect(&rcBox, Origin.x + Position.x, Origin.y + Position.y);
1573 LISTVIEW_InvalidateRect(infoPtr, &rcBox);
1576 static inline void LISTVIEW_InvalidateList(const LISTVIEW_INFO *infoPtr)
1578 LISTVIEW_InvalidateRect(infoPtr, NULL);
1581 static inline void LISTVIEW_InvalidateColumn(const LISTVIEW_INFO *infoPtr, INT nColumn)
1583 RECT rcCol;
1585 if(!is_redrawing(infoPtr)) return;
1586 LISTVIEW_GetHeaderRect(infoPtr, nColumn, &rcCol);
1587 rcCol.top = infoPtr->rcList.top;
1588 rcCol.bottom = infoPtr->rcList.bottom;
1589 LISTVIEW_InvalidateRect(infoPtr, &rcCol);
1592 /***
1593 * DESCRIPTION:
1594 * Retrieves the number of items that can fit vertically in the client area.
1596 * PARAMETER(S):
1597 * [I] infoPtr : valid pointer to the listview structure
1599 * RETURN:
1600 * Number of items per row.
1602 static inline INT LISTVIEW_GetCountPerRow(const LISTVIEW_INFO *infoPtr)
1604 INT nListWidth = infoPtr->rcList.right - infoPtr->rcList.left;
1606 return max(nListWidth/(infoPtr->nItemWidth ? infoPtr->nItemWidth : 1), 1);
1609 /***
1610 * DESCRIPTION:
1611 * Retrieves the number of items that can fit horizontally in the client
1612 * area.
1614 * PARAMETER(S):
1615 * [I] infoPtr : valid pointer to the listview structure
1617 * RETURN:
1618 * Number of items per column.
1620 static inline INT LISTVIEW_GetCountPerColumn(const LISTVIEW_INFO *infoPtr)
1622 INT nListHeight = infoPtr->rcList.bottom - infoPtr->rcList.top;
1624 return max(nListHeight / infoPtr->nItemHeight, 1);
1628 /*************************************************************************
1629 * LISTVIEW_ProcessLetterKeys
1631 * Processes keyboard messages generated by pressing the letter keys
1632 * on the keyboard.
1633 * What this does is perform a case insensitive search from the
1634 * current position with the following quirks:
1635 * - If two chars or more are pressed in quick succession we search
1636 * for the corresponding string (e.g. 'abc').
1637 * - If there is a delay we wipe away the current search string and
1638 * restart with just that char.
1639 * - If the user keeps pressing the same character, whether slowly or
1640 * fast, so that the search string is entirely composed of this
1641 * character ('aaaaa' for instance), then we search for first item
1642 * that starting with that character.
1643 * - If the user types the above character in quick succession, then
1644 * we must also search for the corresponding string ('aaaaa'), and
1645 * go to that string if there is a match.
1647 * PARAMETERS
1648 * [I] hwnd : handle to the window
1649 * [I] charCode : the character code, the actual character
1650 * [I] keyData : key data
1652 * RETURNS
1654 * Zero.
1656 * BUGS
1658 * - The current implementation has a list of characters it will
1659 * accept and it ignores everything else. In particular it will
1660 * ignore accentuated characters which seems to match what
1661 * Windows does. But I'm not sure it makes sense to follow
1662 * Windows there.
1663 * - We don't sound a beep when the search fails.
1665 * SEE ALSO
1667 * TREEVIEW_ProcessLetterKeys
1669 static INT LISTVIEW_ProcessLetterKeys(LISTVIEW_INFO *infoPtr, WPARAM charCode, LPARAM keyData)
1671 INT nItem;
1672 INT endidx,idx;
1673 LVITEMW item;
1674 WCHAR buffer[MAX_PATH];
1675 DWORD lastKeyPressTimestamp = infoPtr->lastKeyPressTimestamp;
1677 /* simple parameter checking */
1678 if (!charCode || !keyData) return 0;
1680 /* only allow the valid WM_CHARs through */
1681 if (!isalnumW(charCode) &&
1682 charCode != '.' && charCode != '`' && charCode != '!' &&
1683 charCode != '@' && charCode != '#' && charCode != '$' &&
1684 charCode != '%' && charCode != '^' && charCode != '&' &&
1685 charCode != '*' && charCode != '(' && charCode != ')' &&
1686 charCode != '-' && charCode != '_' && charCode != '+' &&
1687 charCode != '=' && charCode != '\\'&& charCode != ']' &&
1688 charCode != '}' && charCode != '[' && charCode != '{' &&
1689 charCode != '/' && charCode != '?' && charCode != '>' &&
1690 charCode != '<' && charCode != ',' && charCode != '~')
1691 return 0;
1693 /* if there's one item or less, there is no where to go */
1694 if (infoPtr->nItemCount <= 1) return 0;
1696 /* update the search parameters */
1697 infoPtr->lastKeyPressTimestamp = GetTickCount();
1698 if (infoPtr->lastKeyPressTimestamp - lastKeyPressTimestamp < KEY_DELAY) {
1699 if (infoPtr->nSearchParamLength < MAX_PATH-1)
1700 infoPtr->szSearchParam[infoPtr->nSearchParamLength++]=charCode;
1701 if (infoPtr->charCode != charCode)
1702 infoPtr->charCode = charCode = 0;
1703 } else {
1704 infoPtr->charCode=charCode;
1705 infoPtr->szSearchParam[0]=charCode;
1706 infoPtr->nSearchParamLength=1;
1707 /* Redundant with the 1 char string */
1708 charCode=0;
1711 /* and search from the current position */
1712 nItem=-1;
1713 if (infoPtr->nFocusedItem >= 0) {
1714 endidx=infoPtr->nFocusedItem;
1715 idx=endidx;
1716 /* if looking for single character match,
1717 * then we must always move forward
1719 if (infoPtr->nSearchParamLength == 1)
1720 idx++;
1721 } else {
1722 endidx=infoPtr->nItemCount;
1723 idx=0;
1726 /* Let application handle this for virtual listview */
1727 if (infoPtr->dwStyle & LVS_OWNERDATA)
1729 NMLVFINDITEMW nmlv;
1730 LVFINDINFOW lvfi;
1732 ZeroMemory(&lvfi, sizeof(lvfi));
1733 lvfi.flags = (LVFI_WRAP | LVFI_PARTIAL);
1734 infoPtr->szSearchParam[infoPtr->nSearchParamLength] = '\0';
1735 lvfi.psz = infoPtr->szSearchParam;
1736 nmlv.iStart = idx;
1737 nmlv.lvfi = lvfi;
1739 nItem = notify_hdr(infoPtr, LVN_ODFINDITEMW, (LPNMHDR)&nmlv.hdr);
1741 if (nItem != -1)
1742 LISTVIEW_KeySelection(infoPtr, nItem, FALSE);
1744 return 0;
1747 do {
1748 if (idx == infoPtr->nItemCount) {
1749 if (endidx == infoPtr->nItemCount || endidx == 0)
1750 break;
1751 idx=0;
1754 /* get item */
1755 item.mask = LVIF_TEXT;
1756 item.iItem = idx;
1757 item.iSubItem = 0;
1758 item.pszText = buffer;
1759 item.cchTextMax = MAX_PATH;
1760 if (!LISTVIEW_GetItemW(infoPtr, &item)) return 0;
1762 /* check for a match */
1763 if (lstrncmpiW(item.pszText,infoPtr->szSearchParam,infoPtr->nSearchParamLength) == 0) {
1764 nItem=idx;
1765 break;
1766 } else if ( (charCode != 0) && (nItem == -1) && (nItem != infoPtr->nFocusedItem) &&
1767 (lstrncmpiW(item.pszText,infoPtr->szSearchParam,1) == 0) ) {
1768 /* This would work but we must keep looking for a longer match */
1769 nItem=idx;
1771 idx++;
1772 } while (idx != endidx);
1774 if (nItem != -1)
1775 LISTVIEW_KeySelection(infoPtr, nItem, FALSE);
1777 return 0;
1780 /*************************************************************************
1781 * LISTVIEW_UpdateHeaderSize [Internal]
1783 * Function to resize the header control
1785 * PARAMS
1786 * [I] hwnd : handle to a window
1787 * [I] nNewScrollPos : scroll pos to set
1789 * RETURNS
1790 * None.
1792 static void LISTVIEW_UpdateHeaderSize(const LISTVIEW_INFO *infoPtr, INT nNewScrollPos)
1794 RECT winRect;
1795 POINT point[2];
1797 TRACE("nNewScrollPos=%d\n", nNewScrollPos);
1799 if (!infoPtr->hwndHeader) return;
1801 GetWindowRect(infoPtr->hwndHeader, &winRect);
1802 point[0].x = winRect.left;
1803 point[0].y = winRect.top;
1804 point[1].x = winRect.right;
1805 point[1].y = winRect.bottom;
1807 MapWindowPoints(HWND_DESKTOP, infoPtr->hwndSelf, point, 2);
1808 point[0].x = -nNewScrollPos;
1809 point[1].x += nNewScrollPos;
1811 SetWindowPos(infoPtr->hwndHeader,0,
1812 point[0].x,point[0].y,point[1].x,point[1].y,
1813 (infoPtr->dwStyle & LVS_NOCOLUMNHEADER) ? SWP_HIDEWINDOW : SWP_SHOWWINDOW |
1814 SWP_NOZORDER | SWP_NOACTIVATE);
1817 /***
1818 * DESCRIPTION:
1819 * Update the scrollbars. This functions should be called whenever
1820 * the content, size or view changes.
1822 * PARAMETER(S):
1823 * [I] infoPtr : valid pointer to the listview structure
1825 * RETURN:
1826 * None
1828 static void LISTVIEW_UpdateScroll(const LISTVIEW_INFO *infoPtr)
1830 SCROLLINFO horzInfo, vertInfo;
1831 INT dx, dy;
1833 if ((infoPtr->dwStyle & LVS_NOSCROLL) || !is_redrawing(infoPtr)) return;
1835 ZeroMemory(&horzInfo, sizeof(SCROLLINFO));
1836 horzInfo.cbSize = sizeof(SCROLLINFO);
1837 horzInfo.nPage = infoPtr->rcList.right - infoPtr->rcList.left;
1839 /* for now, we'll set info.nMax to the _count_, and adjust it later */
1840 if (infoPtr->uView == LV_VIEW_LIST)
1842 INT nPerCol = LISTVIEW_GetCountPerColumn(infoPtr);
1843 horzInfo.nMax = (infoPtr->nItemCount + nPerCol - 1) / nPerCol;
1845 /* scroll by at least one column per page */
1846 if(horzInfo.nPage < infoPtr->nItemWidth)
1847 horzInfo.nPage = infoPtr->nItemWidth;
1849 if (infoPtr->nItemWidth)
1850 horzInfo.nPage /= infoPtr->nItemWidth;
1852 else if (infoPtr->uView == LV_VIEW_DETAILS)
1854 horzInfo.nMax = infoPtr->nItemWidth;
1856 else /* LV_VIEW_ICON, or LV_VIEW_SMALLICON */
1858 RECT rcView;
1860 if (LISTVIEW_GetViewRect(infoPtr, &rcView)) horzInfo.nMax = rcView.right - rcView.left;
1863 horzInfo.fMask = SIF_RANGE | SIF_PAGE;
1864 horzInfo.nMax = max(horzInfo.nMax - 1, 0);
1865 dx = GetScrollPos(infoPtr->hwndSelf, SB_HORZ);
1866 dx -= SetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &horzInfo, TRUE);
1867 TRACE("horzInfo=%s\n", debugscrollinfo(&horzInfo));
1869 /* Setting the horizontal scroll can change the listview size
1870 * (and potentially everything else) so we need to recompute
1871 * everything again for the vertical scroll
1874 ZeroMemory(&vertInfo, sizeof(SCROLLINFO));
1875 vertInfo.cbSize = sizeof(SCROLLINFO);
1876 vertInfo.nPage = infoPtr->rcList.bottom - infoPtr->rcList.top;
1878 if (infoPtr->uView == LV_VIEW_DETAILS)
1880 vertInfo.nMax = infoPtr->nItemCount;
1882 /* scroll by at least one page */
1883 if(vertInfo.nPage < infoPtr->nItemHeight)
1884 vertInfo.nPage = infoPtr->nItemHeight;
1886 if (infoPtr->nItemHeight > 0)
1887 vertInfo.nPage /= infoPtr->nItemHeight;
1889 else if (infoPtr->uView != LV_VIEW_LIST) /* LV_VIEW_ICON, or LV_VIEW_SMALLICON */
1891 RECT rcView;
1893 if (LISTVIEW_GetViewRect(infoPtr, &rcView)) vertInfo.nMax = rcView.bottom - rcView.top;
1896 vertInfo.fMask = SIF_RANGE | SIF_PAGE;
1897 vertInfo.nMax = max(vertInfo.nMax - 1, 0);
1898 dy = GetScrollPos(infoPtr->hwndSelf, SB_VERT);
1899 dy -= SetScrollInfo(infoPtr->hwndSelf, SB_VERT, &vertInfo, TRUE);
1900 TRACE("vertInfo=%s\n", debugscrollinfo(&vertInfo));
1902 /* Change of the range may have changed the scroll pos. If so move the content */
1903 if (dx != 0 || dy != 0)
1905 RECT listRect;
1906 listRect = infoPtr->rcList;
1907 ScrollWindowEx(infoPtr->hwndSelf, dx, dy, &listRect, &listRect, 0, 0,
1908 SW_ERASE | SW_INVALIDATE);
1911 /* Update the Header Control */
1912 if (infoPtr->uView == LV_VIEW_DETAILS)
1914 horzInfo.fMask = SIF_POS;
1915 GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &horzInfo);
1916 LISTVIEW_UpdateHeaderSize(infoPtr, horzInfo.nPos);
1921 /***
1922 * DESCRIPTION:
1923 * Shows/hides the focus rectangle.
1925 * PARAMETER(S):
1926 * [I] infoPtr : valid pointer to the listview structure
1927 * [I] fShow : TRUE to show the focus, FALSE to hide it.
1929 * RETURN:
1930 * None
1932 static void LISTVIEW_ShowFocusRect(const LISTVIEW_INFO *infoPtr, BOOL fShow)
1934 HDC hdc;
1936 TRACE("fShow=%d, nItem=%d\n", fShow, infoPtr->nFocusedItem);
1938 if (infoPtr->nFocusedItem < 0) return;
1940 /* we need some gymnastics in ICON mode to handle large items */
1941 if (infoPtr->uView == LV_VIEW_ICON)
1943 RECT rcBox;
1945 LISTVIEW_GetItemBox(infoPtr, infoPtr->nFocusedItem, &rcBox);
1946 if ((rcBox.bottom - rcBox.top) > infoPtr->nItemHeight)
1948 LISTVIEW_InvalidateRect(infoPtr, &rcBox);
1949 return;
1953 if (!(hdc = GetDC(infoPtr->hwndSelf))) return;
1955 /* for some reason, owner draw should work only in report mode */
1956 if ((infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && (infoPtr->uView == LV_VIEW_DETAILS))
1958 DRAWITEMSTRUCT dis;
1959 LVITEMW item;
1961 HFONT hFont = infoPtr->hFont ? infoPtr->hFont : infoPtr->hDefaultFont;
1962 HFONT hOldFont = SelectObject(hdc, hFont);
1964 item.iItem = infoPtr->nFocusedItem;
1965 item.iSubItem = 0;
1966 item.mask = LVIF_PARAM;
1967 if (!LISTVIEW_GetItemW(infoPtr, &item)) goto done;
1969 ZeroMemory(&dis, sizeof(dis));
1970 dis.CtlType = ODT_LISTVIEW;
1971 dis.CtlID = (UINT)GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
1972 dis.itemID = item.iItem;
1973 dis.itemAction = ODA_FOCUS;
1974 if (fShow) dis.itemState |= ODS_FOCUS;
1975 dis.hwndItem = infoPtr->hwndSelf;
1976 dis.hDC = hdc;
1977 LISTVIEW_GetItemBox(infoPtr, dis.itemID, &dis.rcItem);
1978 dis.itemData = item.lParam;
1980 SendMessageW(infoPtr->hwndNotify, WM_DRAWITEM, dis.CtlID, (LPARAM)&dis);
1982 SelectObject(hdc, hOldFont);
1984 else
1986 LISTVIEW_DrawFocusRect(infoPtr, hdc);
1988 done:
1989 ReleaseDC(infoPtr->hwndSelf, hdc);
1992 /***
1993 * Invalidates all visible selected items.
1995 static void LISTVIEW_InvalidateSelectedItems(const LISTVIEW_INFO *infoPtr)
1997 ITERATOR i;
1999 iterator_frameditems(&i, infoPtr, &infoPtr->rcList);
2000 while(iterator_next(&i))
2002 if (LISTVIEW_GetItemState(infoPtr, i.nItem, LVIS_SELECTED))
2003 LISTVIEW_InvalidateItem(infoPtr, i.nItem);
2005 iterator_destroy(&i);
2009 /***
2010 * DESCRIPTION: [INTERNAL]
2011 * Computes an item's (left,top) corner, relative to rcView.
2012 * That is, the position has NOT been made relative to the Origin.
2013 * This is deliberate, to avoid computing the Origin over, and
2014 * over again, when this function is called in a loop. Instead,
2015 * one can factor the computation of the Origin before the loop,
2016 * and offset the value returned by this function, on every iteration.
2018 * PARAMETER(S):
2019 * [I] infoPtr : valid pointer to the listview structure
2020 * [I] nItem : item number
2021 * [O] lpptOrig : item top, left corner
2023 * RETURN:
2024 * None.
2026 static void LISTVIEW_GetItemOrigin(const LISTVIEW_INFO *infoPtr, INT nItem, LPPOINT lpptPosition)
2028 assert(nItem >= 0 && nItem < infoPtr->nItemCount);
2030 if ((infoPtr->uView == LV_VIEW_SMALLICON) || (infoPtr->uView == LV_VIEW_ICON))
2032 lpptPosition->x = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosX, nItem);
2033 lpptPosition->y = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosY, nItem);
2035 else if (infoPtr->uView == LV_VIEW_LIST)
2037 INT nCountPerColumn = LISTVIEW_GetCountPerColumn(infoPtr);
2038 lpptPosition->x = nItem / nCountPerColumn * infoPtr->nItemWidth;
2039 lpptPosition->y = nItem % nCountPerColumn * infoPtr->nItemHeight;
2041 else /* LV_VIEW_DETAILS */
2043 lpptPosition->x = REPORT_MARGINX;
2044 /* item is always at zero indexed column */
2045 if (DPA_GetPtrCount(infoPtr->hdpaColumns) > 0)
2046 lpptPosition->x += LISTVIEW_GetColumnInfo(infoPtr, 0)->rcHeader.left;
2047 lpptPosition->y = nItem * infoPtr->nItemHeight;
2051 /***
2052 * DESCRIPTION: [INTERNAL]
2053 * Compute the rectangles of an item. This is to localize all
2054 * the computations in one place. If you are not interested in some
2055 * of these values, simply pass in a NULL -- the function is smart
2056 * enough to compute only what's necessary. The function computes
2057 * the standard rectangles (BOUNDS, ICON, LABEL) plus a non-standard
2058 * one, the BOX rectangle. This rectangle is very cheap to compute,
2059 * and is guaranteed to contain all the other rectangles. Computing
2060 * the ICON rect is also cheap, but all the others are potentially
2061 * expensive. This gives an easy and effective optimization when
2062 * searching (like point inclusion, or rectangle intersection):
2063 * first test against the BOX, and if TRUE, test against the desired
2064 * rectangle.
2065 * If the function does not have all the necessary information
2066 * to computed the requested rectangles, will crash with a
2067 * failed assertion. This is done so we catch all programming
2068 * errors, given that the function is called only from our code.
2070 * We have the following 'special' meanings for a few fields:
2071 * * If LVIS_FOCUSED is set, we assume the item has the focus
2072 * This is important in ICON mode, where it might get a larger
2073 * then usual rectangle
2075 * Please note that subitem support works only in REPORT mode.
2077 * PARAMETER(S):
2078 * [I] infoPtr : valid pointer to the listview structure
2079 * [I] lpLVItem : item to compute the measures for
2080 * [O] lprcBox : ptr to Box rectangle
2081 * Same as LVM_GETITEMRECT with LVIR_BOUNDS
2082 * [0] lprcSelectBox : ptr to select box rectangle
2083 * Same as LVM_GETITEMRECT with LVIR_SELECTEDBOUNDS
2084 * [O] lprcIcon : ptr to Icon rectangle
2085 * Same as LVM_GETITEMRECT with LVIR_ICON
2086 * [O] lprcStateIcon: ptr to State Icon rectangle
2087 * [O] lprcLabel : ptr to Label rectangle
2088 * Same as LVM_GETITEMRECT with LVIR_LABEL
2090 * RETURN:
2091 * None.
2093 static void LISTVIEW_GetItemMetrics(const LISTVIEW_INFO *infoPtr, const LVITEMW *lpLVItem,
2094 LPRECT lprcBox, LPRECT lprcSelectBox,
2095 LPRECT lprcIcon, LPRECT lprcStateIcon, LPRECT lprcLabel)
2097 BOOL doSelectBox = FALSE, doIcon = FALSE, doLabel = FALSE, oversizedBox = FALSE;
2098 RECT Box, SelectBox, Icon, Label;
2099 COLUMN_INFO *lpColumnInfo = NULL;
2100 SIZE labelSize = { 0, 0 };
2102 TRACE("(lpLVItem=%s)\n", debuglvitem_t(lpLVItem, TRUE));
2104 /* Be smart and try to figure out the minimum we have to do */
2105 if (lpLVItem->iSubItem) assert(infoPtr->uView == LV_VIEW_DETAILS);
2106 if (infoPtr->uView == LV_VIEW_ICON && (lprcBox || lprcLabel))
2108 assert((lpLVItem->mask & LVIF_STATE) && (lpLVItem->stateMask & LVIS_FOCUSED));
2109 if (lpLVItem->state & LVIS_FOCUSED) oversizedBox = doLabel = TRUE;
2111 if (lprcSelectBox) doSelectBox = TRUE;
2112 if (lprcLabel) doLabel = TRUE;
2113 if (doLabel || lprcIcon || lprcStateIcon) doIcon = TRUE;
2114 if (doSelectBox)
2116 doIcon = TRUE;
2117 doLabel = TRUE;
2120 /************************************************************/
2121 /* compute the box rectangle (it should be cheap to do) */
2122 /************************************************************/
2123 if (lpLVItem->iSubItem || infoPtr->uView == LV_VIEW_DETAILS)
2124 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, lpLVItem->iSubItem);
2126 if (lpLVItem->iSubItem)
2128 Box = lpColumnInfo->rcHeader;
2130 else
2132 Box.left = 0;
2133 Box.right = infoPtr->nItemWidth;
2135 Box.top = 0;
2136 Box.bottom = infoPtr->nItemHeight;
2138 /******************************************************************/
2139 /* compute ICON bounding box (ala LVM_GETITEMRECT) and STATEICON */
2140 /******************************************************************/
2141 if (doIcon)
2143 LONG state_width = 0;
2145 if (infoPtr->himlState && lpLVItem->iSubItem == 0)
2146 state_width = infoPtr->iconStateSize.cx;
2148 if (infoPtr->uView == LV_VIEW_ICON)
2150 Icon.left = Box.left + state_width;
2151 if (infoPtr->himlNormal)
2152 Icon.left += (infoPtr->nItemWidth - infoPtr->iconSize.cx - state_width) / 2;
2153 Icon.top = Box.top + ICON_TOP_PADDING;
2154 Icon.right = Icon.left;
2155 Icon.bottom = Icon.top;
2156 if (infoPtr->himlNormal)
2158 Icon.right += infoPtr->iconSize.cx;
2159 Icon.bottom += infoPtr->iconSize.cy;
2162 else /* LV_VIEW_SMALLICON, LV_VIEW_LIST or LV_VIEW_DETAILS */
2164 Icon.left = Box.left + state_width;
2166 if (infoPtr->uView == LV_VIEW_DETAILS && lpLVItem->iSubItem == 0)
2168 /* we need the indent in report mode */
2169 assert(lpLVItem->mask & LVIF_INDENT);
2170 Icon.left += infoPtr->iconSize.cx * lpLVItem->iIndent + REPORT_MARGINX;
2173 Icon.top = Box.top;
2174 Icon.right = Icon.left;
2175 if (infoPtr->himlSmall &&
2176 (!lpColumnInfo || lpLVItem->iSubItem == 0 || (lpColumnInfo->fmt & LVCFMT_IMAGE) ||
2177 ((infoPtr->dwLvExStyle & LVS_EX_SUBITEMIMAGES) && lpLVItem->iImage != I_IMAGECALLBACK)))
2178 Icon.right += infoPtr->iconSize.cx;
2179 Icon.bottom = Icon.top + infoPtr->iconSize.cy;
2181 if(lprcIcon) *lprcIcon = Icon;
2182 TRACE(" - icon=%s\n", wine_dbgstr_rect(&Icon));
2184 /* TODO: is this correct? */
2185 if (lprcStateIcon)
2187 lprcStateIcon->left = Icon.left - state_width;
2188 lprcStateIcon->right = Icon.left;
2189 lprcStateIcon->top = Icon.top;
2190 lprcStateIcon->bottom = lprcStateIcon->top + infoPtr->iconSize.cy;
2191 TRACE(" - state icon=%s\n", wine_dbgstr_rect(lprcStateIcon));
2194 else Icon.right = 0;
2196 /************************************************************/
2197 /* compute LABEL bounding box (ala LVM_GETITEMRECT) */
2198 /************************************************************/
2199 if (doLabel)
2201 /* calculate how far to the right can the label stretch */
2202 Label.right = Box.right;
2203 if (infoPtr->uView == LV_VIEW_DETAILS)
2205 if (lpLVItem->iSubItem == 0) Label = lpColumnInfo->rcHeader;
2208 if (lpLVItem->iSubItem || ((infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && infoPtr->uView == LV_VIEW_DETAILS))
2210 labelSize.cx = infoPtr->nItemWidth;
2211 labelSize.cy = infoPtr->nItemHeight;
2212 goto calc_label;
2215 /* we need the text in non owner draw mode */
2216 assert(lpLVItem->mask & LVIF_TEXT);
2217 if (is_textT(lpLVItem->pszText, TRUE))
2219 HFONT hFont = infoPtr->hFont ? infoPtr->hFont : infoPtr->hDefaultFont;
2220 HDC hdc = GetDC(infoPtr->hwndSelf);
2221 HFONT hOldFont = SelectObject(hdc, hFont);
2222 UINT uFormat;
2223 RECT rcText;
2225 /* compute rough rectangle where the label will go */
2226 SetRectEmpty(&rcText);
2227 rcText.right = infoPtr->nItemWidth - TRAILING_LABEL_PADDING;
2228 rcText.bottom = infoPtr->nItemHeight;
2229 if (infoPtr->uView == LV_VIEW_ICON)
2230 rcText.bottom -= ICON_TOP_PADDING + infoPtr->iconSize.cy + ICON_BOTTOM_PADDING;
2232 /* now figure out the flags */
2233 if (infoPtr->uView == LV_VIEW_ICON)
2234 uFormat = oversizedBox ? LV_FL_DT_FLAGS : LV_ML_DT_FLAGS;
2235 else
2236 uFormat = LV_SL_DT_FLAGS;
2238 DrawTextW (hdc, lpLVItem->pszText, -1, &rcText, uFormat | DT_CALCRECT);
2240 if (rcText.right != rcText.left)
2241 labelSize.cx = min(rcText.right - rcText.left + TRAILING_LABEL_PADDING, infoPtr->nItemWidth);
2243 labelSize.cy = rcText.bottom - rcText.top;
2245 SelectObject(hdc, hOldFont);
2246 ReleaseDC(infoPtr->hwndSelf, hdc);
2249 calc_label:
2250 if (infoPtr->uView == LV_VIEW_ICON)
2252 Label.left = Box.left + (infoPtr->nItemWidth - labelSize.cx) / 2;
2253 Label.top = Box.top + ICON_TOP_PADDING_HITABLE +
2254 infoPtr->iconSize.cy + ICON_BOTTOM_PADDING;
2255 Label.right = Label.left + labelSize.cx;
2256 Label.bottom = Label.top + infoPtr->nItemHeight;
2257 if (!oversizedBox && labelSize.cy > infoPtr->ntmHeight)
2259 labelSize.cy = min(Box.bottom - Label.top, labelSize.cy);
2260 labelSize.cy /= infoPtr->ntmHeight;
2261 labelSize.cy = max(labelSize.cy, 1);
2262 labelSize.cy *= infoPtr->ntmHeight;
2264 Label.bottom = Label.top + labelSize.cy + HEIGHT_PADDING;
2266 else if (infoPtr->uView == LV_VIEW_DETAILS)
2268 Label.left = Icon.right;
2269 Label.top = Box.top;
2270 Label.right = lpColumnInfo->rcHeader.right;
2271 Label.bottom = Label.top + infoPtr->nItemHeight;
2273 else /* LV_VIEW_SMALLICON or LV_VIEW_LIST */
2275 Label.left = Icon.right;
2276 Label.top = Box.top;
2277 Label.right = min(Label.left + labelSize.cx, Label.right);
2278 Label.bottom = Label.top + infoPtr->nItemHeight;
2281 if (lprcLabel) *lprcLabel = Label;
2282 TRACE(" - label=%s\n", wine_dbgstr_rect(&Label));
2285 /************************************************************/
2286 /* compute SELECT bounding box */
2287 /************************************************************/
2288 if (doSelectBox)
2290 if (infoPtr->uView == LV_VIEW_DETAILS)
2292 SelectBox.left = Icon.left;
2293 SelectBox.top = Box.top;
2294 SelectBox.bottom = Box.bottom;
2296 if (labelSize.cx)
2297 SelectBox.right = min(Label.left + labelSize.cx, Label.right);
2298 else
2299 SelectBox.right = min(Label.left + MAX_EMPTYTEXT_SELECT_WIDTH, Label.right);
2301 else
2303 UnionRect(&SelectBox, &Icon, &Label);
2305 if (lprcSelectBox) *lprcSelectBox = SelectBox;
2306 TRACE(" - select box=%s\n", wine_dbgstr_rect(&SelectBox));
2309 /* Fix the Box if necessary */
2310 if (lprcBox)
2312 if (oversizedBox) UnionRect(lprcBox, &Box, &Label);
2313 else *lprcBox = Box;
2315 TRACE(" - box=%s\n", wine_dbgstr_rect(&Box));
2318 /***
2319 * DESCRIPTION: [INTERNAL]
2321 * PARAMETER(S):
2322 * [I] infoPtr : valid pointer to the listview structure
2323 * [I] nItem : item number
2324 * [O] lprcBox : ptr to Box rectangle
2326 * RETURN:
2327 * None.
2329 static void LISTVIEW_GetItemBox(const LISTVIEW_INFO *infoPtr, INT nItem, LPRECT lprcBox)
2331 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
2332 POINT Position, Origin;
2333 LVITEMW lvItem;
2335 LISTVIEW_GetOrigin(infoPtr, &Origin);
2336 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position);
2338 /* Be smart and try to figure out the minimum we have to do */
2339 lvItem.mask = 0;
2340 if (infoPtr->uView == LV_VIEW_ICON && infoPtr->bFocus && LISTVIEW_GetItemState(infoPtr, nItem, LVIS_FOCUSED))
2341 lvItem.mask |= LVIF_TEXT;
2342 lvItem.iItem = nItem;
2343 lvItem.iSubItem = 0;
2344 lvItem.pszText = szDispText;
2345 lvItem.cchTextMax = DISP_TEXT_SIZE;
2346 if (lvItem.mask) LISTVIEW_GetItemW(infoPtr, &lvItem);
2347 if (infoPtr->uView == LV_VIEW_ICON)
2349 lvItem.mask |= LVIF_STATE;
2350 lvItem.stateMask = LVIS_FOCUSED;
2351 lvItem.state = (lvItem.mask & LVIF_TEXT ? LVIS_FOCUSED : 0);
2353 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, lprcBox, 0, 0, 0, 0);
2355 if (infoPtr->uView == LV_VIEW_DETAILS && infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT &&
2356 SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX, 0, 0))
2358 OffsetRect(lprcBox, Origin.x, Position.y + Origin.y);
2360 else
2361 OffsetRect(lprcBox, Position.x + Origin.x, Position.y + Origin.y);
2364 /* LISTVIEW_MapIdToIndex helper */
2365 static INT CALLBACK MapIdSearchCompare(LPVOID p1, LPVOID p2, LPARAM lParam)
2367 ITEM_ID *id1 = (ITEM_ID*)p1;
2368 ITEM_ID *id2 = (ITEM_ID*)p2;
2370 if (id1->id == id2->id) return 0;
2372 return (id1->id < id2->id) ? -1 : 1;
2375 /***
2376 * DESCRIPTION:
2377 * Returns the item index for id specified.
2379 * PARAMETER(S):
2380 * [I] infoPtr : valid pointer to the listview structure
2381 * [I] iID : item id to get index for
2383 * RETURN:
2384 * Item index, or -1 on failure.
2386 static INT LISTVIEW_MapIdToIndex(const LISTVIEW_INFO *infoPtr, UINT iID)
2388 ITEM_ID ID;
2389 INT index;
2391 TRACE("iID=%d\n", iID);
2393 if (infoPtr->dwStyle & LVS_OWNERDATA) return -1;
2394 if (infoPtr->nItemCount == 0) return -1;
2396 ID.id = iID;
2397 index = DPA_Search(infoPtr->hdpaItemIds, &ID, -1, &MapIdSearchCompare, 0, DPAS_SORTED);
2399 if (index != -1)
2401 ITEM_ID *lpID = DPA_GetPtr(infoPtr->hdpaItemIds, index);
2402 return DPA_GetPtrIndex(infoPtr->hdpaItems, lpID->item);
2405 return -1;
2408 /***
2409 * DESCRIPTION:
2410 * Returns the item id for index given.
2412 * PARAMETER(S):
2413 * [I] infoPtr : valid pointer to the listview structure
2414 * [I] iItem : item index to get id for
2416 * RETURN:
2417 * Item id.
2419 static DWORD LISTVIEW_MapIndexToId(const LISTVIEW_INFO *infoPtr, INT iItem)
2421 ITEM_INFO *lpItem;
2422 HDPA hdpaSubItems;
2424 TRACE("iItem=%d\n", iItem);
2426 if (infoPtr->dwStyle & LVS_OWNERDATA) return -1;
2427 if (iItem < 0 || iItem >= infoPtr->nItemCount) return -1;
2429 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, iItem);
2430 lpItem = DPA_GetPtr(hdpaSubItems, 0);
2432 return lpItem->id->id;
2435 /***
2436 * DESCRIPTION:
2437 * Returns the current icon position, and advances it along the top.
2438 * The returned position is not offset by Origin.
2440 * PARAMETER(S):
2441 * [I] infoPtr : valid pointer to the listview structure
2442 * [O] lpPos : will get the current icon position
2444 * RETURN:
2445 * None
2447 static void LISTVIEW_NextIconPosTop(LISTVIEW_INFO *infoPtr, LPPOINT lpPos)
2449 INT nListWidth = infoPtr->rcList.right - infoPtr->rcList.left;
2451 *lpPos = infoPtr->currIconPos;
2453 infoPtr->currIconPos.x += infoPtr->nItemWidth;
2454 if (infoPtr->currIconPos.x + infoPtr->nItemWidth <= nListWidth) return;
2456 infoPtr->currIconPos.x = 0;
2457 infoPtr->currIconPos.y += infoPtr->nItemHeight;
2461 /***
2462 * DESCRIPTION:
2463 * Returns the current icon position, and advances it down the left edge.
2464 * The returned position is not offset by Origin.
2466 * PARAMETER(S):
2467 * [I] infoPtr : valid pointer to the listview structure
2468 * [O] lpPos : will get the current icon position
2470 * RETURN:
2471 * None
2473 static void LISTVIEW_NextIconPosLeft(LISTVIEW_INFO *infoPtr, LPPOINT lpPos)
2475 INT nListHeight = infoPtr->rcList.bottom - infoPtr->rcList.top;
2477 *lpPos = infoPtr->currIconPos;
2479 infoPtr->currIconPos.y += infoPtr->nItemHeight;
2480 if (infoPtr->currIconPos.y + infoPtr->nItemHeight <= nListHeight) return;
2482 infoPtr->currIconPos.x += infoPtr->nItemWidth;
2483 infoPtr->currIconPos.y = 0;
2487 /***
2488 * DESCRIPTION:
2489 * Moves an icon to the specified position.
2490 * It takes care of invalidating the item, etc.
2492 * PARAMETER(S):
2493 * [I] infoPtr : valid pointer to the listview structure
2494 * [I] nItem : the item to move
2495 * [I] lpPos : the new icon position
2496 * [I] isNew : flags the item as being new
2498 * RETURN:
2499 * Success: TRUE
2500 * Failure: FALSE
2502 static BOOL LISTVIEW_MoveIconTo(const LISTVIEW_INFO *infoPtr, INT nItem, const POINT *lppt, BOOL isNew)
2504 POINT old;
2506 if (!isNew)
2508 old.x = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosX, nItem);
2509 old.y = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosY, nItem);
2511 if (lppt->x == old.x && lppt->y == old.y) return TRUE;
2512 LISTVIEW_InvalidateItem(infoPtr, nItem);
2515 /* Allocating a POINTER for every item is too resource intensive,
2516 * so we'll keep the (x,y) in different arrays */
2517 if (!DPA_SetPtr(infoPtr->hdpaPosX, nItem, (void *)(LONG_PTR)lppt->x)) return FALSE;
2518 if (!DPA_SetPtr(infoPtr->hdpaPosY, nItem, (void *)(LONG_PTR)lppt->y)) return FALSE;
2520 LISTVIEW_InvalidateItem(infoPtr, nItem);
2522 return TRUE;
2525 /***
2526 * DESCRIPTION:
2527 * Arranges listview items in icon display mode.
2529 * PARAMETER(S):
2530 * [I] infoPtr : valid pointer to the listview structure
2531 * [I] nAlignCode : alignment code
2533 * RETURN:
2534 * SUCCESS : TRUE
2535 * FAILURE : FALSE
2537 static BOOL LISTVIEW_Arrange(LISTVIEW_INFO *infoPtr, INT nAlignCode)
2539 void (*next_pos)(LISTVIEW_INFO *, LPPOINT);
2540 POINT pos;
2541 INT i;
2543 if (infoPtr->uView != LV_VIEW_ICON && infoPtr->uView != LV_VIEW_SMALLICON) return FALSE;
2545 TRACE("nAlignCode=%d\n", nAlignCode);
2547 if (nAlignCode == LVA_DEFAULT)
2549 if (infoPtr->dwStyle & LVS_ALIGNLEFT) nAlignCode = LVA_ALIGNLEFT;
2550 else nAlignCode = LVA_ALIGNTOP;
2553 switch (nAlignCode)
2555 case LVA_ALIGNLEFT: next_pos = LISTVIEW_NextIconPosLeft; break;
2556 case LVA_ALIGNTOP: next_pos = LISTVIEW_NextIconPosTop; break;
2557 case LVA_SNAPTOGRID: next_pos = LISTVIEW_NextIconPosTop; break; /* FIXME */
2558 default: return FALSE;
2561 infoPtr->bAutoarrange = TRUE;
2562 infoPtr->currIconPos.x = infoPtr->currIconPos.y = 0;
2563 for (i = 0; i < infoPtr->nItemCount; i++)
2565 next_pos(infoPtr, &pos);
2566 LISTVIEW_MoveIconTo(infoPtr, i, &pos, FALSE);
2569 return TRUE;
2572 /***
2573 * DESCRIPTION:
2574 * Retrieves the bounding rectangle of all the items, not offset by Origin.
2575 * For LVS_REPORT always returns empty rectangle.
2577 * PARAMETER(S):
2578 * [I] infoPtr : valid pointer to the listview structure
2579 * [O] lprcView : bounding rectangle
2581 * RETURN:
2582 * SUCCESS : TRUE
2583 * FAILURE : FALSE
2585 static void LISTVIEW_GetAreaRect(const LISTVIEW_INFO *infoPtr, LPRECT lprcView)
2587 INT i, x, y;
2589 SetRectEmpty(lprcView);
2591 switch (infoPtr->uView)
2593 case LV_VIEW_ICON:
2594 case LV_VIEW_SMALLICON:
2595 for (i = 0; i < infoPtr->nItemCount; i++)
2597 x = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosX, i);
2598 y = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosY, i);
2599 lprcView->right = max(lprcView->right, x);
2600 lprcView->bottom = max(lprcView->bottom, y);
2602 if (infoPtr->nItemCount > 0)
2604 lprcView->right += infoPtr->nItemWidth;
2605 lprcView->bottom += infoPtr->nItemHeight;
2607 break;
2609 case LV_VIEW_LIST:
2610 y = LISTVIEW_GetCountPerColumn(infoPtr);
2611 x = infoPtr->nItemCount / y;
2612 if (infoPtr->nItemCount % y) x++;
2613 lprcView->right = x * infoPtr->nItemWidth;
2614 lprcView->bottom = y * infoPtr->nItemHeight;
2615 break;
2619 /***
2620 * DESCRIPTION:
2621 * Retrieves the bounding rectangle of all the items.
2623 * PARAMETER(S):
2624 * [I] infoPtr : valid pointer to the listview structure
2625 * [O] lprcView : bounding rectangle
2627 * RETURN:
2628 * SUCCESS : TRUE
2629 * FAILURE : FALSE
2631 static BOOL LISTVIEW_GetViewRect(const LISTVIEW_INFO *infoPtr, LPRECT lprcView)
2633 POINT ptOrigin;
2635 TRACE("(lprcView=%p)\n", lprcView);
2637 if (!lprcView) return FALSE;
2639 LISTVIEW_GetAreaRect(infoPtr, lprcView);
2641 if (infoPtr->uView != LV_VIEW_DETAILS)
2643 LISTVIEW_GetOrigin(infoPtr, &ptOrigin);
2644 OffsetRect(lprcView, ptOrigin.x, ptOrigin.y);
2647 TRACE("lprcView=%s\n", wine_dbgstr_rect(lprcView));
2649 return TRUE;
2652 /***
2653 * DESCRIPTION:
2654 * Retrieves the subitem pointer associated with the subitem index.
2656 * PARAMETER(S):
2657 * [I] hdpaSubItems : DPA handle for a specific item
2658 * [I] nSubItem : index of subitem
2660 * RETURN:
2661 * SUCCESS : subitem pointer
2662 * FAILURE : NULL
2664 static SUBITEM_INFO* LISTVIEW_GetSubItemPtr(HDPA hdpaSubItems, INT nSubItem)
2666 SUBITEM_INFO *lpSubItem;
2667 INT i;
2669 /* we should binary search here if need be */
2670 for (i = 1; i < DPA_GetPtrCount(hdpaSubItems); i++)
2672 lpSubItem = DPA_GetPtr(hdpaSubItems, i);
2673 if (lpSubItem->iSubItem == nSubItem)
2674 return lpSubItem;
2677 return NULL;
2681 /***
2682 * DESCRIPTION:
2683 * Calculates the desired item width.
2685 * PARAMETER(S):
2686 * [I] infoPtr : valid pointer to the listview structure
2688 * RETURN:
2689 * The desired item width.
2691 static INT LISTVIEW_CalculateItemWidth(const LISTVIEW_INFO *infoPtr)
2693 INT nItemWidth = 0;
2695 TRACE("uView=%d\n", infoPtr->uView);
2697 if (infoPtr->uView == LV_VIEW_ICON)
2698 nItemWidth = infoPtr->iconSpacing.cx;
2699 else if (infoPtr->uView == LV_VIEW_DETAILS)
2701 if (DPA_GetPtrCount(infoPtr->hdpaColumns) > 0)
2703 RECT rcHeader;
2704 INT index;
2706 index = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX,
2707 DPA_GetPtrCount(infoPtr->hdpaColumns) - 1, 0);
2709 LISTVIEW_GetHeaderRect(infoPtr, index, &rcHeader);
2710 nItemWidth = rcHeader.right;
2713 else /* LV_VIEW_SMALLICON, or LV_VIEW_LIST */
2715 INT i;
2717 for (i = 0; i < infoPtr->nItemCount; i++)
2718 nItemWidth = max(LISTVIEW_GetLabelWidth(infoPtr, i), nItemWidth);
2720 if (infoPtr->himlSmall) nItemWidth += infoPtr->iconSize.cx;
2721 if (infoPtr->himlState) nItemWidth += infoPtr->iconStateSize.cx;
2723 nItemWidth = max(DEFAULT_COLUMN_WIDTH, nItemWidth + WIDTH_PADDING);
2726 return nItemWidth;
2729 /***
2730 * DESCRIPTION:
2731 * Calculates the desired item height.
2733 * PARAMETER(S):
2734 * [I] infoPtr : valid pointer to the listview structure
2736 * RETURN:
2737 * The desired item height.
2739 static INT LISTVIEW_CalculateItemHeight(const LISTVIEW_INFO *infoPtr)
2741 INT nItemHeight;
2743 TRACE("uView=%d\n", infoPtr->uView);
2745 if (infoPtr->uView == LV_VIEW_ICON)
2746 nItemHeight = infoPtr->iconSpacing.cy;
2747 else
2749 nItemHeight = infoPtr->ntmHeight;
2750 if (infoPtr->uView == LV_VIEW_DETAILS && infoPtr->dwLvExStyle & LVS_EX_GRIDLINES)
2751 nItemHeight++;
2752 if (infoPtr->himlState)
2753 nItemHeight = max(nItemHeight, infoPtr->iconStateSize.cy);
2754 if (infoPtr->himlSmall)
2755 nItemHeight = max(nItemHeight, infoPtr->iconSize.cy);
2756 if (infoPtr->himlState || infoPtr->himlSmall)
2757 nItemHeight += HEIGHT_PADDING;
2758 if (infoPtr->nMeasureItemHeight > 0)
2759 nItemHeight = infoPtr->nMeasureItemHeight;
2762 return max(nItemHeight, 1);
2765 /***
2766 * DESCRIPTION:
2767 * Updates the width, and height of an item.
2769 * PARAMETER(S):
2770 * [I] infoPtr : valid pointer to the listview structure
2772 * RETURN:
2773 * None.
2775 static inline void LISTVIEW_UpdateItemSize(LISTVIEW_INFO *infoPtr)
2777 infoPtr->nItemWidth = LISTVIEW_CalculateItemWidth(infoPtr);
2778 infoPtr->nItemHeight = LISTVIEW_CalculateItemHeight(infoPtr);
2782 /***
2783 * DESCRIPTION:
2784 * Retrieves and saves important text metrics info for the current
2785 * Listview font.
2787 * PARAMETER(S):
2788 * [I] infoPtr : valid pointer to the listview structure
2791 static void LISTVIEW_SaveTextMetrics(LISTVIEW_INFO *infoPtr)
2793 HDC hdc = GetDC(infoPtr->hwndSelf);
2794 HFONT hFont = infoPtr->hFont ? infoPtr->hFont : infoPtr->hDefaultFont;
2795 HFONT hOldFont = SelectObject(hdc, hFont);
2796 TEXTMETRICW tm;
2797 SIZE sz;
2799 if (GetTextMetricsW(hdc, &tm))
2801 infoPtr->ntmHeight = tm.tmHeight;
2802 infoPtr->ntmMaxCharWidth = tm.tmMaxCharWidth;
2805 if (GetTextExtentPoint32A(hdc, "...", 3, &sz))
2806 infoPtr->nEllipsisWidth = sz.cx;
2808 SelectObject(hdc, hOldFont);
2809 ReleaseDC(infoPtr->hwndSelf, hdc);
2811 TRACE("tmHeight=%d\n", infoPtr->ntmHeight);
2814 /***
2815 * DESCRIPTION:
2816 * A compare function for ranges
2818 * PARAMETER(S)
2819 * [I] range1 : pointer to range 1;
2820 * [I] range2 : pointer to range 2;
2821 * [I] flags : flags
2823 * RETURNS:
2824 * > 0 : if range 1 > range 2
2825 * < 0 : if range 2 > range 1
2826 * = 0 : if range intersects range 2
2828 static INT CALLBACK ranges_cmp(LPVOID range1, LPVOID range2, LPARAM flags)
2830 INT cmp;
2832 if (((RANGE*)range1)->upper <= ((RANGE*)range2)->lower)
2833 cmp = -1;
2834 else if (((RANGE*)range2)->upper <= ((RANGE*)range1)->lower)
2835 cmp = 1;
2836 else
2837 cmp = 0;
2839 TRACE("range1=%s, range2=%s, cmp=%d\n", debugrange(range1), debugrange(range2), cmp);
2841 return cmp;
2844 #if DEBUG_RANGES
2845 #define ranges_check(ranges, desc) ranges_assert(ranges, desc, __FUNCTION__, __LINE__)
2846 #else
2847 #define ranges_check(ranges, desc) do { } while(0)
2848 #endif
2850 static void ranges_assert(RANGES ranges, LPCSTR desc, const char *func, int line)
2852 INT i;
2853 RANGE *prev, *curr;
2855 TRACE("*** Checking %s:%d:%s ***\n", func, line, desc);
2856 assert (ranges);
2857 assert (DPA_GetPtrCount(ranges->hdpa) >= 0);
2858 ranges_dump(ranges);
2859 if (DPA_GetPtrCount(ranges->hdpa) > 0)
2861 prev = DPA_GetPtr(ranges->hdpa, 0);
2862 assert (prev->lower >= 0 && prev->lower < prev->upper);
2863 for (i = 1; i < DPA_GetPtrCount(ranges->hdpa); i++)
2865 curr = DPA_GetPtr(ranges->hdpa, i);
2866 assert (prev->upper <= curr->lower);
2867 assert (curr->lower < curr->upper);
2868 prev = curr;
2871 TRACE("--- Done checking---\n");
2874 static RANGES ranges_create(int count)
2876 RANGES ranges = Alloc(sizeof(struct tagRANGES));
2877 if (!ranges) return NULL;
2878 ranges->hdpa = DPA_Create(count);
2879 if (ranges->hdpa) return ranges;
2880 Free(ranges);
2881 return NULL;
2884 static void ranges_clear(RANGES ranges)
2886 INT i;
2888 for(i = 0; i < DPA_GetPtrCount(ranges->hdpa); i++)
2889 Free(DPA_GetPtr(ranges->hdpa, i));
2890 DPA_DeleteAllPtrs(ranges->hdpa);
2894 static void ranges_destroy(RANGES ranges)
2896 if (!ranges) return;
2897 ranges_clear(ranges);
2898 DPA_Destroy(ranges->hdpa);
2899 Free(ranges);
2902 static RANGES ranges_clone(RANGES ranges)
2904 RANGES clone;
2905 INT i;
2907 if (!(clone = ranges_create(DPA_GetPtrCount(ranges->hdpa)))) goto fail;
2909 for (i = 0; i < DPA_GetPtrCount(ranges->hdpa); i++)
2911 RANGE *newrng = Alloc(sizeof(RANGE));
2912 if (!newrng) goto fail;
2913 *newrng = *((RANGE*)DPA_GetPtr(ranges->hdpa, i));
2914 DPA_SetPtr(clone->hdpa, i, newrng);
2916 return clone;
2918 fail:
2919 TRACE ("clone failed\n");
2920 ranges_destroy(clone);
2921 return NULL;
2924 static RANGES ranges_diff(RANGES ranges, RANGES sub)
2926 INT i;
2928 for (i = 0; i < DPA_GetPtrCount(sub->hdpa); i++)
2929 ranges_del(ranges, *((RANGE *)DPA_GetPtr(sub->hdpa, i)));
2931 return ranges;
2934 static void ranges_dump(RANGES ranges)
2936 INT i;
2938 for (i = 0; i < DPA_GetPtrCount(ranges->hdpa); i++)
2939 TRACE(" %s\n", debugrange(DPA_GetPtr(ranges->hdpa, i)));
2942 static inline BOOL ranges_contain(RANGES ranges, INT nItem)
2944 RANGE srchrng = { nItem, nItem + 1 };
2946 TRACE("(nItem=%d)\n", nItem);
2947 ranges_check(ranges, "before contain");
2948 return DPA_Search(ranges->hdpa, &srchrng, 0, ranges_cmp, 0, DPAS_SORTED) != -1;
2951 static INT ranges_itemcount(RANGES ranges)
2953 INT i, count = 0;
2955 for (i = 0; i < DPA_GetPtrCount(ranges->hdpa); i++)
2957 RANGE *sel = DPA_GetPtr(ranges->hdpa, i);
2958 count += sel->upper - sel->lower;
2961 return count;
2964 static BOOL ranges_shift(RANGES ranges, INT nItem, INT delta, INT nUpper)
2966 RANGE srchrng = { nItem, nItem + 1 }, *chkrng;
2967 INT index;
2969 index = DPA_Search(ranges->hdpa, &srchrng, 0, ranges_cmp, 0, DPAS_SORTED | DPAS_INSERTAFTER);
2970 if (index == -1) return TRUE;
2972 for (; index < DPA_GetPtrCount(ranges->hdpa); index++)
2974 chkrng = DPA_GetPtr(ranges->hdpa, index);
2975 if (chkrng->lower >= nItem)
2976 chkrng->lower = max(min(chkrng->lower + delta, nUpper - 1), 0);
2977 if (chkrng->upper > nItem)
2978 chkrng->upper = max(min(chkrng->upper + delta, nUpper), 0);
2980 return TRUE;
2983 static BOOL ranges_add(RANGES ranges, RANGE range)
2985 RANGE srchrgn;
2986 INT index;
2988 TRACE("(%s)\n", debugrange(&range));
2989 ranges_check(ranges, "before add");
2991 /* try find overlapping regions first */
2992 srchrgn.lower = range.lower - 1;
2993 srchrgn.upper = range.upper + 1;
2994 index = DPA_Search(ranges->hdpa, &srchrgn, 0, ranges_cmp, 0, DPAS_SORTED);
2996 if (index == -1)
2998 RANGE *newrgn;
3000 TRACE("Adding new range\n");
3002 /* create the brand new range to insert */
3003 newrgn = Alloc(sizeof(RANGE));
3004 if(!newrgn) goto fail;
3005 *newrgn = range;
3007 /* figure out where to insert it */
3008 index = DPA_Search(ranges->hdpa, newrgn, 0, ranges_cmp, 0, DPAS_SORTED | DPAS_INSERTAFTER);
3009 TRACE("index=%d\n", index);
3010 if (index == -1) index = 0;
3012 /* and get it over with */
3013 if (DPA_InsertPtr(ranges->hdpa, index, newrgn) == -1)
3015 Free(newrgn);
3016 goto fail;
3019 else
3021 RANGE *chkrgn, *mrgrgn;
3022 INT fromindex, mergeindex;
3024 chkrgn = DPA_GetPtr(ranges->hdpa, index);
3025 TRACE("Merge with %s @%d\n", debugrange(chkrgn), index);
3027 chkrgn->lower = min(range.lower, chkrgn->lower);
3028 chkrgn->upper = max(range.upper, chkrgn->upper);
3030 TRACE("New range %s @%d\n", debugrange(chkrgn), index);
3032 /* merge now common ranges */
3033 fromindex = 0;
3034 srchrgn.lower = chkrgn->lower - 1;
3035 srchrgn.upper = chkrgn->upper + 1;
3039 mergeindex = DPA_Search(ranges->hdpa, &srchrgn, fromindex, ranges_cmp, 0, 0);
3040 if (mergeindex == -1) break;
3041 if (mergeindex == index)
3043 fromindex = index + 1;
3044 continue;
3047 TRACE("Merge with index %i\n", mergeindex);
3049 mrgrgn = DPA_GetPtr(ranges->hdpa, mergeindex);
3050 chkrgn->lower = min(chkrgn->lower, mrgrgn->lower);
3051 chkrgn->upper = max(chkrgn->upper, mrgrgn->upper);
3052 Free(mrgrgn);
3053 DPA_DeletePtr(ranges->hdpa, mergeindex);
3054 if (mergeindex < index) index --;
3055 } while(1);
3058 ranges_check(ranges, "after add");
3059 return TRUE;
3061 fail:
3062 ranges_check(ranges, "failed add");
3063 return FALSE;
3066 static BOOL ranges_del(RANGES ranges, RANGE range)
3068 RANGE *chkrgn;
3069 INT index;
3071 TRACE("(%s)\n", debugrange(&range));
3072 ranges_check(ranges, "before del");
3074 /* we don't use DPAS_SORTED here, since we need *
3075 * to find the first overlapping range */
3076 index = DPA_Search(ranges->hdpa, &range, 0, ranges_cmp, 0, 0);
3077 while(index != -1)
3079 chkrgn = DPA_GetPtr(ranges->hdpa, index);
3081 TRACE("Matches range %s @%d\n", debugrange(chkrgn), index);
3083 /* case 1: Same range */
3084 if ( (chkrgn->upper == range.upper) &&
3085 (chkrgn->lower == range.lower) )
3087 DPA_DeletePtr(ranges->hdpa, index);
3088 break;
3090 /* case 2: engulf */
3091 else if ( (chkrgn->upper <= range.upper) &&
3092 (chkrgn->lower >= range.lower) )
3094 DPA_DeletePtr(ranges->hdpa, index);
3096 /* case 3: overlap upper */
3097 else if ( (chkrgn->upper <= range.upper) &&
3098 (chkrgn->lower < range.lower) )
3100 chkrgn->upper = range.lower;
3102 /* case 4: overlap lower */
3103 else if ( (chkrgn->upper > range.upper) &&
3104 (chkrgn->lower >= range.lower) )
3106 chkrgn->lower = range.upper;
3107 break;
3109 /* case 5: fully internal */
3110 else
3112 RANGE tmprgn = *chkrgn, *newrgn;
3114 if (!(newrgn = Alloc(sizeof(RANGE)))) goto fail;
3115 newrgn->lower = chkrgn->lower;
3116 newrgn->upper = range.lower;
3117 chkrgn->lower = range.upper;
3118 if (DPA_InsertPtr(ranges->hdpa, index, newrgn) == -1)
3120 Free(newrgn);
3121 goto fail;
3123 chkrgn = &tmprgn;
3124 break;
3127 index = DPA_Search(ranges->hdpa, &range, index, ranges_cmp, 0, 0);
3130 ranges_check(ranges, "after del");
3131 return TRUE;
3133 fail:
3134 ranges_check(ranges, "failed del");
3135 return FALSE;
3138 /***
3139 * DESCRIPTION:
3140 * Removes all selection ranges
3142 * Parameters(s):
3143 * [I] infoPtr : valid pointer to the listview structure
3144 * [I] toSkip : item range to skip removing the selection
3146 * RETURNS:
3147 * SUCCESS : TRUE
3148 * FAILURE : FALSE
3150 static BOOL LISTVIEW_DeselectAllSkipItems(LISTVIEW_INFO *infoPtr, RANGES toSkip)
3152 LVITEMW lvItem;
3153 ITERATOR i;
3154 RANGES clone;
3156 TRACE("()\n");
3158 lvItem.state = 0;
3159 lvItem.stateMask = LVIS_SELECTED;
3161 /* need to clone the DPA because callbacks can change it */
3162 if (!(clone = ranges_clone(infoPtr->selectionRanges))) return FALSE;
3163 iterator_rangesitems(&i, ranges_diff(clone, toSkip));
3164 while(iterator_next(&i))
3165 LISTVIEW_SetItemState(infoPtr, i.nItem, &lvItem);
3166 /* note that the iterator destructor will free the cloned range */
3167 iterator_destroy(&i);
3169 return TRUE;
3172 static inline BOOL LISTVIEW_DeselectAllSkipItem(LISTVIEW_INFO *infoPtr, INT nItem)
3174 RANGES toSkip;
3176 if (!(toSkip = ranges_create(1))) return FALSE;
3177 if (nItem != -1) ranges_additem(toSkip, nItem);
3178 LISTVIEW_DeselectAllSkipItems(infoPtr, toSkip);
3179 ranges_destroy(toSkip);
3180 return TRUE;
3183 static inline BOOL LISTVIEW_DeselectAll(LISTVIEW_INFO *infoPtr)
3185 return LISTVIEW_DeselectAllSkipItem(infoPtr, -1);
3188 /***
3189 * DESCRIPTION:
3190 * Retrieves the number of items that are marked as selected.
3192 * PARAMETER(S):
3193 * [I] infoPtr : valid pointer to the listview structure
3195 * RETURN:
3196 * Number of items selected.
3198 static INT LISTVIEW_GetSelectedCount(const LISTVIEW_INFO *infoPtr)
3200 INT nSelectedCount = 0;
3202 if (infoPtr->uCallbackMask & LVIS_SELECTED)
3204 INT i;
3205 for (i = 0; i < infoPtr->nItemCount; i++)
3207 if (LISTVIEW_GetItemState(infoPtr, i, LVIS_SELECTED))
3208 nSelectedCount++;
3211 else
3212 nSelectedCount = ranges_itemcount(infoPtr->selectionRanges);
3214 TRACE("nSelectedCount=%d\n", nSelectedCount);
3215 return nSelectedCount;
3218 /***
3219 * DESCRIPTION:
3220 * Manages the item focus.
3222 * PARAMETER(S):
3223 * [I] infoPtr : valid pointer to the listview structure
3224 * [I] nItem : item index
3226 * RETURN:
3227 * TRUE : focused item changed
3228 * FALSE : focused item has NOT changed
3230 static inline BOOL LISTVIEW_SetItemFocus(LISTVIEW_INFO *infoPtr, INT nItem)
3232 INT oldFocus = infoPtr->nFocusedItem;
3233 LVITEMW lvItem;
3235 if (nItem == infoPtr->nFocusedItem) return FALSE;
3237 lvItem.state = nItem == -1 ? 0 : LVIS_FOCUSED;
3238 lvItem.stateMask = LVIS_FOCUSED;
3239 LISTVIEW_SetItemState(infoPtr, nItem == -1 ? infoPtr->nFocusedItem : nItem, &lvItem);
3241 return oldFocus != infoPtr->nFocusedItem;
3244 /* Helper function for LISTVIEW_ShiftIndices *only* */
3245 static INT shift_item(const LISTVIEW_INFO *infoPtr, INT nShiftItem, INT nItem, INT direction)
3247 if (nShiftItem < nItem) return nShiftItem;
3249 if (nShiftItem > nItem) return nShiftItem + direction;
3251 if (direction > 0) return nShiftItem + direction;
3253 return min(nShiftItem, infoPtr->nItemCount - 1);
3257 * DESCRIPTION:
3258 * Updates the various indices after an item has been inserted or deleted.
3260 * PARAMETER(S):
3261 * [I] infoPtr : valid pointer to the listview structure
3262 * [I] nItem : item index
3263 * [I] direction : Direction of shift, +1 or -1.
3265 * RETURN:
3266 * None
3268 static void LISTVIEW_ShiftIndices(LISTVIEW_INFO *infoPtr, INT nItem, INT direction)
3270 INT nNewFocus;
3271 BOOL bOldChange;
3273 /* temporarily disable change notification while shifting items */
3274 bOldChange = infoPtr->bDoChangeNotify;
3275 infoPtr->bDoChangeNotify = FALSE;
3277 TRACE("Shifting %iu, %i steps\n", nItem, direction);
3279 ranges_shift(infoPtr->selectionRanges, nItem, direction, infoPtr->nItemCount);
3281 assert(abs(direction) == 1);
3283 infoPtr->nSelectionMark = shift_item(infoPtr, infoPtr->nSelectionMark, nItem, direction);
3285 nNewFocus = shift_item(infoPtr, infoPtr->nFocusedItem, nItem, direction);
3286 if (nNewFocus != infoPtr->nFocusedItem)
3287 LISTVIEW_SetItemFocus(infoPtr, nNewFocus);
3289 /* But we are not supposed to modify nHotItem! */
3291 infoPtr->bDoChangeNotify = bOldChange;
3296 * DESCRIPTION:
3297 * Adds a block of selections.
3299 * PARAMETER(S):
3300 * [I] infoPtr : valid pointer to the listview structure
3301 * [I] nItem : item index
3303 * RETURN:
3304 * Whether the window is still valid.
3306 static BOOL LISTVIEW_AddGroupSelection(LISTVIEW_INFO *infoPtr, INT nItem)
3308 INT nFirst = min(infoPtr->nSelectionMark, nItem);
3309 INT nLast = max(infoPtr->nSelectionMark, nItem);
3310 HWND hwndSelf = infoPtr->hwndSelf;
3311 NMLVODSTATECHANGE nmlv;
3312 LVITEMW item;
3313 BOOL bOldChange;
3314 INT i;
3316 /* Temporarily disable change notification
3317 * If the control is LVS_OWNERDATA, we need to send
3318 * only one LVN_ODSTATECHANGED notification.
3319 * See MSDN documentation for LVN_ITEMCHANGED.
3321 bOldChange = infoPtr->bDoChangeNotify;
3322 if (infoPtr->dwStyle & LVS_OWNERDATA) infoPtr->bDoChangeNotify = FALSE;
3324 if (nFirst == -1) nFirst = nItem;
3326 item.state = LVIS_SELECTED;
3327 item.stateMask = LVIS_SELECTED;
3329 for (i = nFirst; i <= nLast; i++)
3330 LISTVIEW_SetItemState(infoPtr,i,&item);
3332 ZeroMemory(&nmlv, sizeof(nmlv));
3333 nmlv.iFrom = nFirst;
3334 nmlv.iTo = nLast;
3335 nmlv.uNewState = 0;
3336 nmlv.uOldState = item.state;
3338 notify_hdr(infoPtr, LVN_ODSTATECHANGED, (LPNMHDR)&nmlv);
3339 if (!IsWindow(hwndSelf))
3340 return FALSE;
3341 infoPtr->bDoChangeNotify = bOldChange;
3342 return TRUE;
3346 /***
3347 * DESCRIPTION:
3348 * Sets a single group selection.
3350 * PARAMETER(S):
3351 * [I] infoPtr : valid pointer to the listview structure
3352 * [I] nItem : item index
3354 * RETURN:
3355 * None
3357 static void LISTVIEW_SetGroupSelection(LISTVIEW_INFO *infoPtr, INT nItem)
3359 RANGES selection;
3360 LVITEMW item;
3361 ITERATOR i;
3362 BOOL bOldChange;
3364 if (!(selection = ranges_create(100))) return;
3366 item.state = LVIS_SELECTED;
3367 item.stateMask = LVIS_SELECTED;
3369 if ((infoPtr->uView == LV_VIEW_LIST) || (infoPtr->uView == LV_VIEW_DETAILS))
3371 if (infoPtr->nSelectionMark == -1)
3373 infoPtr->nSelectionMark = nItem;
3374 ranges_additem(selection, nItem);
3376 else
3378 RANGE sel;
3380 sel.lower = min(infoPtr->nSelectionMark, nItem);
3381 sel.upper = max(infoPtr->nSelectionMark, nItem) + 1;
3382 ranges_add(selection, sel);
3385 else
3387 RECT rcItem, rcSel, rcSelMark;
3388 POINT ptItem;
3390 rcItem.left = LVIR_BOUNDS;
3391 if (!LISTVIEW_GetItemRect(infoPtr, nItem, &rcItem)) return;
3392 rcSelMark.left = LVIR_BOUNDS;
3393 if (!LISTVIEW_GetItemRect(infoPtr, infoPtr->nSelectionMark, &rcSelMark)) return;
3394 UnionRect(&rcSel, &rcItem, &rcSelMark);
3395 iterator_frameditems(&i, infoPtr, &rcSel);
3396 while(iterator_next(&i))
3398 LISTVIEW_GetItemPosition(infoPtr, i.nItem, &ptItem);
3399 if (PtInRect(&rcSel, ptItem)) ranges_additem(selection, i.nItem);
3401 iterator_destroy(&i);
3404 /* disable per item notifications on LVS_OWNERDATA style
3405 FIXME: single LVN_ODSTATECHANGED should be used */
3406 bOldChange = infoPtr->bDoChangeNotify;
3407 if (infoPtr->dwStyle & LVS_OWNERDATA) infoPtr->bDoChangeNotify = FALSE;
3409 LISTVIEW_DeselectAllSkipItems(infoPtr, selection);
3412 iterator_rangesitems(&i, selection);
3413 while(iterator_next(&i))
3414 LISTVIEW_SetItemState(infoPtr, i.nItem, &item);
3415 /* this will also destroy the selection */
3416 iterator_destroy(&i);
3418 infoPtr->bDoChangeNotify = bOldChange;
3420 LISTVIEW_SetItemFocus(infoPtr, nItem);
3423 /***
3424 * DESCRIPTION:
3425 * Sets a single selection.
3427 * PARAMETER(S):
3428 * [I] infoPtr : valid pointer to the listview structure
3429 * [I] nItem : item index
3431 * RETURN:
3432 * None
3434 static void LISTVIEW_SetSelection(LISTVIEW_INFO *infoPtr, INT nItem)
3436 LVITEMW lvItem;
3438 TRACE("nItem=%d\n", nItem);
3440 LISTVIEW_DeselectAllSkipItem(infoPtr, nItem);
3442 lvItem.state = LVIS_FOCUSED | LVIS_SELECTED;
3443 lvItem.stateMask = LVIS_FOCUSED | LVIS_SELECTED;
3444 LISTVIEW_SetItemState(infoPtr, nItem, &lvItem);
3446 infoPtr->nSelectionMark = nItem;
3449 /***
3450 * DESCRIPTION:
3451 * Set selection(s) with keyboard.
3453 * PARAMETER(S):
3454 * [I] infoPtr : valid pointer to the listview structure
3455 * [I] nItem : item index
3456 * [I] space : VK_SPACE code sent
3458 * RETURN:
3459 * SUCCESS : TRUE (needs to be repainted)
3460 * FAILURE : FALSE (nothing has changed)
3462 static BOOL LISTVIEW_KeySelection(LISTVIEW_INFO *infoPtr, INT nItem, BOOL space)
3464 /* FIXME: pass in the state */
3465 WORD wShift = HIWORD(GetKeyState(VK_SHIFT));
3466 WORD wCtrl = HIWORD(GetKeyState(VK_CONTROL));
3467 BOOL bResult = FALSE;
3469 TRACE("nItem=%d, wShift=%d, wCtrl=%d\n", nItem, wShift, wCtrl);
3470 if ((nItem >= 0) && (nItem < infoPtr->nItemCount))
3472 bResult = TRUE;
3474 if (infoPtr->dwStyle & LVS_SINGLESEL || (wShift == 0 && wCtrl == 0))
3475 LISTVIEW_SetSelection(infoPtr, nItem);
3476 else
3478 if (wShift)
3479 LISTVIEW_SetGroupSelection(infoPtr, nItem);
3480 else if (wCtrl)
3482 LVITEMW lvItem;
3483 lvItem.state = ~LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED);
3484 lvItem.stateMask = LVIS_SELECTED;
3485 if (space)
3487 LISTVIEW_SetItemState(infoPtr, nItem, &lvItem);
3488 if (lvItem.state & LVIS_SELECTED)
3489 infoPtr->nSelectionMark = nItem;
3491 bResult = LISTVIEW_SetItemFocus(infoPtr, nItem);
3494 LISTVIEW_EnsureVisible(infoPtr, nItem, FALSE);
3497 UpdateWindow(infoPtr->hwndSelf); /* update client area */
3498 return bResult;
3501 static BOOL LISTVIEW_GetItemAtPt(const LISTVIEW_INFO *infoPtr, LPLVITEMW lpLVItem, POINT pt)
3503 LVHITTESTINFO lvHitTestInfo;
3505 ZeroMemory(&lvHitTestInfo, sizeof(lvHitTestInfo));
3506 lvHitTestInfo.pt.x = pt.x;
3507 lvHitTestInfo.pt.y = pt.y;
3509 LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, FALSE);
3511 lpLVItem->mask = LVIF_PARAM;
3512 lpLVItem->iItem = lvHitTestInfo.iItem;
3513 lpLVItem->iSubItem = 0;
3515 return LISTVIEW_GetItemT(infoPtr, lpLVItem, TRUE);
3518 static inline BOOL LISTVIEW_isHotTracking(const LISTVIEW_INFO *infoPtr)
3520 return ((infoPtr->dwLvExStyle & LVS_EX_TRACKSELECT) ||
3521 (infoPtr->dwLvExStyle & LVS_EX_ONECLICKACTIVATE) ||
3522 (infoPtr->dwLvExStyle & LVS_EX_TWOCLICKACTIVATE));
3525 /***
3526 * DESCRIPTION:
3527 * Called when the mouse is being actively tracked and has hovered for a specified
3528 * amount of time
3530 * PARAMETER(S):
3531 * [I] infoPtr : valid pointer to the listview structure
3532 * [I] fwKeys : key indicator
3533 * [I] x,y : mouse position
3535 * RETURN:
3536 * 0 if the message was processed, non-zero if there was an error
3538 * INFO:
3539 * LVS_EX_TRACKSELECT: An item is automatically selected when the cursor remains
3540 * over the item for a certain period of time.
3543 static LRESULT LISTVIEW_MouseHover(LISTVIEW_INFO *infoPtr, WORD fwKeys, INT x, INT y)
3545 if (LISTVIEW_isHotTracking(infoPtr))
3547 LVITEMW item;
3548 POINT pt;
3550 pt.x = x;
3551 pt.y = y;
3553 if (LISTVIEW_GetItemAtPt(infoPtr, &item, pt))
3554 LISTVIEW_SetSelection(infoPtr, item.iItem);
3557 return 0;
3560 /***
3561 * DESCRIPTION:
3562 * Called whenever WM_MOUSEMOVE is received.
3564 * PARAMETER(S):
3565 * [I] infoPtr : valid pointer to the listview structure
3566 * [I] fwKeys : key indicator
3567 * [I] x,y : mouse position
3569 * RETURN:
3570 * 0 if the message is processed, non-zero if there was an error
3572 static LRESULT LISTVIEW_MouseMove(LISTVIEW_INFO *infoPtr, WORD fwKeys, INT x, INT y)
3574 TRACKMOUSEEVENT trackinfo;
3576 if (!(fwKeys & MK_LBUTTON))
3577 infoPtr->bLButtonDown = FALSE;
3579 if (infoPtr->bLButtonDown)
3581 POINT tmp;
3582 RECT rect;
3583 LVHITTESTINFO lvHitTestInfo;
3584 WORD wDragWidth = GetSystemMetrics(SM_CXDRAG);
3585 WORD wDragHeight= GetSystemMetrics(SM_CYDRAG);
3587 rect.left = infoPtr->ptClickPos.x - wDragWidth;
3588 rect.right = infoPtr->ptClickPos.x + wDragWidth;
3589 rect.top = infoPtr->ptClickPos.y - wDragHeight;
3590 rect.bottom = infoPtr->ptClickPos.y + wDragHeight;
3592 tmp.x = x;
3593 tmp.y = y;
3595 lvHitTestInfo.pt = tmp;
3596 LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, TRUE);
3598 /* reset item marker */
3599 if (infoPtr->nLButtonDownItem != lvHitTestInfo.iItem)
3600 infoPtr->nLButtonDownItem = -1;
3602 if (!PtInRect(&rect, tmp))
3604 /* this path covers the following:
3605 1. WM_LBUTTONDOWN over selected item (sets focus on it)
3606 2. change focus with keys
3607 3. move mouse over item from step 1 selects it and moves focus on it */
3608 if (infoPtr->nLButtonDownItem != -1 &&
3609 !LISTVIEW_GetItemState(infoPtr, infoPtr->nLButtonDownItem, LVIS_SELECTED))
3611 LVITEMW lvItem;
3613 lvItem.state = LVIS_FOCUSED | LVIS_SELECTED;
3614 lvItem.stateMask = LVIS_FOCUSED | LVIS_SELECTED;
3616 LISTVIEW_SetItemState(infoPtr, infoPtr->nLButtonDownItem, &lvItem);
3617 infoPtr->nLButtonDownItem = -1;
3620 if (!infoPtr->bDragging)
3622 NMLISTVIEW nmlv;
3624 lvHitTestInfo.pt = infoPtr->ptClickPos;
3625 LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, TRUE);
3627 ZeroMemory(&nmlv, sizeof(nmlv));
3628 nmlv.iItem = lvHitTestInfo.iItem;
3629 nmlv.ptAction = infoPtr->ptClickPos;
3631 notify_listview(infoPtr, LVN_BEGINDRAG, &nmlv);
3632 infoPtr->bDragging = TRUE;
3635 return 0;
3639 /* see if we are supposed to be tracking mouse hovering */
3640 if (LISTVIEW_isHotTracking(infoPtr)) {
3641 /* fill in the trackinfo struct */
3642 trackinfo.cbSize = sizeof(TRACKMOUSEEVENT);
3643 trackinfo.dwFlags = TME_QUERY;
3644 trackinfo.hwndTrack = infoPtr->hwndSelf;
3645 trackinfo.dwHoverTime = infoPtr->dwHoverTime;
3647 /* see if we are already tracking this hwnd */
3648 _TrackMouseEvent(&trackinfo);
3650 if(!(trackinfo.dwFlags & TME_HOVER)) {
3651 trackinfo.dwFlags = TME_HOVER;
3653 /* call TRACKMOUSEEVENT so we receive WM_MOUSEHOVER messages */
3654 _TrackMouseEvent(&trackinfo);
3658 return 0;
3662 /***
3663 * Tests whether the item is assignable to a list with style lStyle
3665 static inline BOOL is_assignable_item(const LVITEMW *lpLVItem, LONG lStyle)
3667 if ( (lpLVItem->mask & LVIF_TEXT) &&
3668 (lpLVItem->pszText == LPSTR_TEXTCALLBACKW) &&
3669 (lStyle & (LVS_SORTASCENDING | LVS_SORTDESCENDING)) ) return FALSE;
3671 return TRUE;
3675 /***
3676 * DESCRIPTION:
3677 * Helper for LISTVIEW_SetItemT *only*: sets item attributes.
3679 * PARAMETER(S):
3680 * [I] infoPtr : valid pointer to the listview structure
3681 * [I] lpLVItem : valid pointer to new item attributes
3682 * [I] isNew : the item being set is being inserted
3683 * [I] isW : TRUE if lpLVItem is Unicode, FALSE if it's ANSI
3684 * [O] bChanged : will be set to TRUE if the item really changed
3686 * RETURN:
3687 * SUCCESS : TRUE
3688 * FAILURE : FALSE
3690 static BOOL set_main_item(LISTVIEW_INFO *infoPtr, const LVITEMW *lpLVItem, BOOL isNew, BOOL isW, BOOL *bChanged)
3692 ITEM_INFO *lpItem;
3693 NMLISTVIEW nmlv;
3694 UINT uChanged = 0;
3695 LVITEMW item;
3696 /* stateMask is ignored for LVM_INSERTITEM */
3697 UINT stateMask = isNew ? ~0 : lpLVItem->stateMask;
3699 TRACE("()\n");
3701 assert(lpLVItem->iItem >= 0 && lpLVItem->iItem < infoPtr->nItemCount);
3703 if (lpLVItem->mask == 0) return TRUE;
3705 if (infoPtr->dwStyle & LVS_OWNERDATA)
3707 /* a virtual listview only stores selection and focus */
3708 if (lpLVItem->mask & ~LVIF_STATE)
3709 return FALSE;
3710 lpItem = NULL;
3712 else
3714 HDPA hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, lpLVItem->iItem);
3715 lpItem = DPA_GetPtr(hdpaSubItems, 0);
3716 assert (lpItem);
3719 /* we need to get the lParam and state of the item */
3720 item.iItem = lpLVItem->iItem;
3721 item.iSubItem = lpLVItem->iSubItem;
3722 item.mask = LVIF_STATE | LVIF_PARAM;
3723 item.stateMask = (infoPtr->dwStyle & LVS_OWNERDATA) ? LVIS_FOCUSED | LVIS_SELECTED : ~0;
3725 item.state = 0;
3726 item.lParam = 0;
3727 if (!isNew && !LISTVIEW_GetItemW(infoPtr, &item)) return FALSE;
3729 TRACE("oldState=%x, newState=%x\n", item.state, lpLVItem->state);
3730 /* determine what fields will change */
3731 if ((lpLVItem->mask & LVIF_STATE) && ((item.state ^ lpLVItem->state) & stateMask & ~infoPtr->uCallbackMask))
3732 uChanged |= LVIF_STATE;
3734 if ((lpLVItem->mask & LVIF_IMAGE) && (lpItem->hdr.iImage != lpLVItem->iImage))
3735 uChanged |= LVIF_IMAGE;
3737 if ((lpLVItem->mask & LVIF_PARAM) && (lpItem->lParam != lpLVItem->lParam))
3738 uChanged |= LVIF_PARAM;
3740 if ((lpLVItem->mask & LVIF_INDENT) && (lpItem->iIndent != lpLVItem->iIndent))
3741 uChanged |= LVIF_INDENT;
3743 if ((lpLVItem->mask & LVIF_TEXT) && textcmpWT(lpItem->hdr.pszText, lpLVItem->pszText, isW))
3744 uChanged |= LVIF_TEXT;
3746 TRACE("uChanged=0x%x\n", uChanged);
3747 if (!uChanged) return TRUE;
3748 *bChanged = TRUE;
3750 ZeroMemory(&nmlv, sizeof(NMLISTVIEW));
3751 nmlv.iItem = lpLVItem->iItem;
3752 nmlv.uNewState = (item.state & ~stateMask) | (lpLVItem->state & stateMask);
3753 nmlv.uOldState = item.state;
3754 nmlv.uChanged = uChanged;
3755 nmlv.lParam = item.lParam;
3757 /* send LVN_ITEMCHANGING notification, if the item is not being inserted */
3758 /* and we are _NOT_ virtual (LVS_OWNERDATA), and change notifications */
3759 /* are enabled */
3760 if(lpItem && !isNew && infoPtr->bDoChangeNotify)
3762 HWND hwndSelf = infoPtr->hwndSelf;
3764 if (notify_listview(infoPtr, LVN_ITEMCHANGING, &nmlv))
3765 return FALSE;
3766 if (!IsWindow(hwndSelf))
3767 return FALSE;
3770 /* copy information */
3771 if (lpLVItem->mask & LVIF_TEXT)
3772 textsetptrT(&lpItem->hdr.pszText, lpLVItem->pszText, isW);
3774 if (lpLVItem->mask & LVIF_IMAGE)
3775 lpItem->hdr.iImage = lpLVItem->iImage;
3777 if (lpLVItem->mask & LVIF_PARAM)
3778 lpItem->lParam = lpLVItem->lParam;
3780 if (lpLVItem->mask & LVIF_INDENT)
3781 lpItem->iIndent = lpLVItem->iIndent;
3783 if (uChanged & LVIF_STATE)
3785 if (lpItem && (stateMask & ~infoPtr->uCallbackMask))
3787 lpItem->state &= ~stateMask;
3788 lpItem->state |= (lpLVItem->state & stateMask);
3790 if (lpLVItem->state & stateMask & ~infoPtr->uCallbackMask & LVIS_SELECTED)
3792 if (infoPtr->dwStyle & LVS_SINGLESEL) LISTVIEW_DeselectAllSkipItem(infoPtr, lpLVItem->iItem);
3793 ranges_additem(infoPtr->selectionRanges, lpLVItem->iItem);
3795 else if (stateMask & LVIS_SELECTED)
3797 ranges_delitem(infoPtr->selectionRanges, lpLVItem->iItem);
3799 /* if we are asked to change focus, and we manage it, do it */
3800 if (stateMask & ~infoPtr->uCallbackMask & LVIS_FOCUSED)
3802 if (lpLVItem->state & LVIS_FOCUSED)
3804 if (infoPtr->nFocusedItem != -1)
3806 /* remove current focus */
3807 item.mask = LVIF_STATE;
3808 item.state = 0;
3809 item.stateMask = LVIS_FOCUSED;
3811 /* recurse with redrawing an item */
3812 LISTVIEW_SetItemState(infoPtr, infoPtr->nFocusedItem, &item);
3815 infoPtr->nFocusedItem = lpLVItem->iItem;
3816 LISTVIEW_EnsureVisible(infoPtr, lpLVItem->iItem, infoPtr->uView == LV_VIEW_LIST);
3818 else if (infoPtr->nFocusedItem == lpLVItem->iItem)
3820 infoPtr->nFocusedItem = -1;
3825 /* if we're inserting the item, we're done */
3826 if (isNew) return TRUE;
3828 /* send LVN_ITEMCHANGED notification */
3829 if (lpLVItem->mask & LVIF_PARAM) nmlv.lParam = lpLVItem->lParam;
3830 if (infoPtr->bDoChangeNotify) notify_listview(infoPtr, LVN_ITEMCHANGED, &nmlv);
3832 return TRUE;
3835 /***
3836 * DESCRIPTION:
3837 * Helper for LISTVIEW_{Set,Insert}ItemT *only*: sets subitem attributes.
3839 * PARAMETER(S):
3840 * [I] infoPtr : valid pointer to the listview structure
3841 * [I] lpLVItem : valid pointer to new subitem attributes
3842 * [I] isW : TRUE if lpLVItem is Unicode, FALSE if it's ANSI
3843 * [O] bChanged : will be set to TRUE if the item really changed
3845 * RETURN:
3846 * SUCCESS : TRUE
3847 * FAILURE : FALSE
3849 static BOOL set_sub_item(const LISTVIEW_INFO *infoPtr, const LVITEMW *lpLVItem, BOOL isW, BOOL *bChanged)
3851 HDPA hdpaSubItems;
3852 SUBITEM_INFO *lpSubItem;
3854 /* we do not support subitems for virtual listviews */
3855 if (infoPtr->dwStyle & LVS_OWNERDATA) return FALSE;
3857 /* set subitem only if column is present */
3858 if (lpLVItem->iSubItem >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return FALSE;
3860 /* First do some sanity checks */
3861 /* The LVIF_STATE flag is valid for subitems, but does not appear to be
3862 particularly useful. We currently do not actually do anything with
3863 the flag on subitems.
3865 if (lpLVItem->mask & ~(LVIF_TEXT | LVIF_IMAGE | LVIF_STATE)) return FALSE;
3866 if (!(lpLVItem->mask & (LVIF_TEXT | LVIF_IMAGE | LVIF_STATE))) return TRUE;
3868 /* get the subitem structure, and create it if not there */
3869 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, lpLVItem->iItem);
3870 assert (hdpaSubItems);
3872 lpSubItem = LISTVIEW_GetSubItemPtr(hdpaSubItems, lpLVItem->iSubItem);
3873 if (!lpSubItem)
3875 SUBITEM_INFO *tmpSubItem;
3876 INT i;
3878 lpSubItem = Alloc(sizeof(SUBITEM_INFO));
3879 if (!lpSubItem) return FALSE;
3880 /* we could binary search here, if need be...*/
3881 for (i = 1; i < DPA_GetPtrCount(hdpaSubItems); i++)
3883 tmpSubItem = DPA_GetPtr(hdpaSubItems, i);
3884 if (tmpSubItem->iSubItem > lpLVItem->iSubItem) break;
3886 if (DPA_InsertPtr(hdpaSubItems, i, lpSubItem) == -1)
3888 Free(lpSubItem);
3889 return FALSE;
3891 lpSubItem->iSubItem = lpLVItem->iSubItem;
3892 lpSubItem->hdr.iImage = I_IMAGECALLBACK;
3893 *bChanged = TRUE;
3896 if (lpLVItem->mask & LVIF_IMAGE)
3897 if (lpSubItem->hdr.iImage != lpLVItem->iImage)
3899 lpSubItem->hdr.iImage = lpLVItem->iImage;
3900 *bChanged = TRUE;
3903 if ((lpLVItem->mask & LVIF_TEXT) && textcmpWT(lpSubItem->hdr.pszText, lpLVItem->pszText, isW))
3905 textsetptrT(&lpSubItem->hdr.pszText, lpLVItem->pszText, isW);
3906 *bChanged = TRUE;
3909 return TRUE;
3912 /***
3913 * DESCRIPTION:
3914 * Sets item attributes.
3916 * PARAMETER(S):
3917 * [I] infoPtr : valid pointer to the listview structure
3918 * [I] lpLVItem : new item attributes
3919 * [I] isW : TRUE if lpLVItem is Unicode, FALSE if it's ANSI
3921 * RETURN:
3922 * SUCCESS : TRUE
3923 * FAILURE : FALSE
3925 static BOOL LISTVIEW_SetItemT(LISTVIEW_INFO *infoPtr, LVITEMW *lpLVItem, BOOL isW)
3927 HWND hwndSelf = infoPtr->hwndSelf;
3928 LPWSTR pszText = NULL;
3929 BOOL bResult, bChanged = FALSE;
3931 TRACE("(lpLVItem=%s, isW=%d)\n", debuglvitem_t(lpLVItem, isW), isW);
3933 if (!lpLVItem || lpLVItem->iItem < 0 || lpLVItem->iItem >= infoPtr->nItemCount)
3934 return FALSE;
3936 /* For efficiency, we transform the lpLVItem->pszText to Unicode here */
3937 if ((lpLVItem->mask & LVIF_TEXT) && is_textW(lpLVItem->pszText))
3939 pszText = lpLVItem->pszText;
3940 lpLVItem->pszText = textdupTtoW(lpLVItem->pszText, isW);
3943 /* actually set the fields */
3944 if (!is_assignable_item(lpLVItem, infoPtr->dwStyle)) return FALSE;
3946 if (lpLVItem->iSubItem)
3947 bResult = set_sub_item(infoPtr, lpLVItem, TRUE, &bChanged);
3948 else
3949 bResult = set_main_item(infoPtr, lpLVItem, FALSE, TRUE, &bChanged);
3950 if (!IsWindow(hwndSelf))
3951 return FALSE;
3953 /* redraw item, if necessary */
3954 if (bChanged && !infoPtr->bIsDrawing)
3956 /* this little optimization eliminates some nasty flicker */
3957 if ( infoPtr->uView == LV_VIEW_DETAILS && !(infoPtr->dwStyle & LVS_OWNERDRAWFIXED) &&
3958 !(infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT) &&
3959 lpLVItem->iSubItem > 0 && lpLVItem->iSubItem <= DPA_GetPtrCount(infoPtr->hdpaColumns) )
3960 LISTVIEW_InvalidateSubItem(infoPtr, lpLVItem->iItem, lpLVItem->iSubItem);
3961 else
3962 LISTVIEW_InvalidateItem(infoPtr, lpLVItem->iItem);
3964 /* restore text */
3965 if (pszText)
3967 textfreeT(lpLVItem->pszText, isW);
3968 lpLVItem->pszText = pszText;
3971 return bResult;
3974 /***
3975 * DESCRIPTION:
3976 * Retrieves the index of the item at coordinate (0, 0) of the client area.
3978 * PARAMETER(S):
3979 * [I] infoPtr : valid pointer to the listview structure
3981 * RETURN:
3982 * item index
3984 static INT LISTVIEW_GetTopIndex(const LISTVIEW_INFO *infoPtr)
3986 INT nItem = 0;
3987 SCROLLINFO scrollInfo;
3989 scrollInfo.cbSize = sizeof(SCROLLINFO);
3990 scrollInfo.fMask = SIF_POS;
3992 if (infoPtr->uView == LV_VIEW_LIST)
3994 if (GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo))
3995 nItem = scrollInfo.nPos * LISTVIEW_GetCountPerColumn(infoPtr);
3997 else if (infoPtr->uView == LV_VIEW_DETAILS)
3999 if (GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo))
4000 nItem = scrollInfo.nPos;
4002 else
4004 if (GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo))
4005 nItem = LISTVIEW_GetCountPerRow(infoPtr) * (scrollInfo.nPos / infoPtr->nItemHeight);
4008 TRACE("nItem=%d\n", nItem);
4010 return nItem;
4014 /***
4015 * DESCRIPTION:
4016 * Erases the background of the given rectangle
4018 * PARAMETER(S):
4019 * [I] infoPtr : valid pointer to the listview structure
4020 * [I] hdc : device context handle
4021 * [I] lprcBox : clipping rectangle
4023 * RETURN:
4024 * Success: TRUE
4025 * Failure: FALSE
4027 static inline BOOL LISTVIEW_FillBkgnd(const LISTVIEW_INFO *infoPtr, HDC hdc, const RECT *lprcBox)
4029 if (!infoPtr->hBkBrush) return FALSE;
4031 TRACE("(hdc=%p, lprcBox=%s, hBkBrush=%p)\n", hdc, wine_dbgstr_rect(lprcBox), infoPtr->hBkBrush);
4033 return FillRect(hdc, lprcBox, infoPtr->hBkBrush);
4036 /***
4037 * DESCRIPTION:
4038 * Draws an item.
4040 * PARAMETER(S):
4041 * [I] infoPtr : valid pointer to the listview structure
4042 * [I] hdc : device context handle
4043 * [I] nItem : item index
4044 * [I] nSubItem : subitem index
4045 * [I] pos : item position in client coordinates
4046 * [I] cdmode : custom draw mode
4048 * RETURN:
4049 * Success: TRUE
4050 * Failure: FALSE
4052 static BOOL LISTVIEW_DrawItem(LISTVIEW_INFO *infoPtr, HDC hdc, INT nItem, INT nSubItem, POINT pos, DWORD cdmode)
4054 UINT uFormat;
4055 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
4056 static WCHAR szCallback[] = { '(', 'c', 'a', 'l', 'l', 'b', 'a', 'c', 'k', ')', 0 };
4057 DWORD cdsubitemmode = CDRF_DODEFAULT;
4058 LPRECT lprcFocus;
4059 RECT rcSelect, rcBox, rcIcon, rcLabel, rcStateIcon;
4060 NMLVCUSTOMDRAW nmlvcd;
4061 HIMAGELIST himl;
4062 LVITEMW lvItem;
4063 HFONT hOldFont;
4065 TRACE("(hdc=%p, nItem=%d, nSubItem=%d, pos=%s)\n", hdc, nItem, nSubItem, wine_dbgstr_point(&pos));
4067 /* get information needed for drawing the item */
4068 lvItem.mask = LVIF_TEXT | LVIF_IMAGE | LVIF_PARAM;
4069 if (nSubItem == 0) lvItem.mask |= LVIF_STATE;
4070 if (infoPtr->uView == LV_VIEW_DETAILS) lvItem.mask |= LVIF_INDENT;
4071 lvItem.stateMask = LVIS_SELECTED | LVIS_FOCUSED | LVIS_STATEIMAGEMASK;
4072 lvItem.iItem = nItem;
4073 lvItem.iSubItem = nSubItem;
4074 lvItem.state = 0;
4075 lvItem.lParam = 0;
4076 lvItem.cchTextMax = DISP_TEXT_SIZE;
4077 lvItem.pszText = szDispText;
4078 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) return FALSE;
4079 if (nSubItem > 0 && (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT))
4080 lvItem.state = LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED);
4081 if (lvItem.pszText == LPSTR_TEXTCALLBACKW) lvItem.pszText = szCallback;
4082 TRACE(" lvItem=%s\n", debuglvitem_t(&lvItem, TRUE));
4084 /* now check if we need to update the focus rectangle */
4085 lprcFocus = infoPtr->bFocus && (lvItem.state & LVIS_FOCUSED) ? &infoPtr->rcFocus : 0;
4087 if (!lprcFocus) lvItem.state &= ~LVIS_FOCUSED;
4088 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, &rcBox, &rcSelect, &rcIcon, &rcStateIcon, &rcLabel);
4089 OffsetRect(&rcBox, pos.x, pos.y);
4090 OffsetRect(&rcSelect, pos.x, pos.y);
4091 OffsetRect(&rcIcon, pos.x, pos.y);
4092 OffsetRect(&rcStateIcon, pos.x, pos.y);
4093 OffsetRect(&rcLabel, pos.x, pos.y);
4094 TRACE(" rcBox=%s, rcSelect=%s, rcIcon=%s. rcLabel=%s\n",
4095 wine_dbgstr_rect(&rcBox), wine_dbgstr_rect(&rcSelect),
4096 wine_dbgstr_rect(&rcIcon), wine_dbgstr_rect(&rcLabel));
4098 /* fill in the custom draw structure */
4099 customdraw_fill(&nmlvcd, infoPtr, hdc, &rcBox, &lvItem);
4101 hOldFont = GetCurrentObject(hdc, OBJ_FONT);
4102 if (nSubItem > 0) cdmode = infoPtr->cditemmode;
4103 if (cdmode & CDRF_SKIPDEFAULT) goto postpaint;
4104 if (cdmode & CDRF_NOTIFYITEMDRAW)
4105 cdsubitemmode = notify_customdraw(infoPtr, CDDS_PREPAINT, &nmlvcd);
4106 if (nSubItem == 0) infoPtr->cditemmode = cdsubitemmode;
4107 if (cdsubitemmode & CDRF_SKIPDEFAULT) goto postpaint;
4108 /* we have to send a CDDS_SUBITEM customdraw explicitly for subitem 0 */
4109 if (nSubItem == 0 && cdsubitemmode == CDRF_NOTIFYITEMDRAW)
4111 cdsubitemmode = notify_customdraw(infoPtr, CDDS_SUBITEM | CDDS_ITEMPREPAINT, &nmlvcd);
4112 if (cdsubitemmode & CDRF_SKIPDEFAULT) goto postpaint;
4114 if (nSubItem == 0 || (cdmode & CDRF_NOTIFYITEMDRAW))
4115 prepaint_setup(infoPtr, hdc, &nmlvcd, FALSE);
4116 else if ((infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT) == FALSE)
4117 prepaint_setup(infoPtr, hdc, &nmlvcd, TRUE);
4119 /* in full row select, subitems, will just use main item's colors */
4120 if (nSubItem && infoPtr->uView == LV_VIEW_DETAILS && (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT))
4121 nmlvcd.clrTextBk = CLR_NONE;
4123 /* FIXME: temporary hack */
4124 rcSelect.left = rcLabel.left;
4126 /* draw the selection background, if we're drawing the main item */
4127 if (nSubItem == 0)
4129 /* in icon mode, the label rect is really what we want to draw the
4130 * background for */
4131 if (infoPtr->uView == LV_VIEW_ICON)
4132 rcSelect = rcLabel;
4134 if (infoPtr->uView == LV_VIEW_DETAILS && (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT))
4136 /* we have to update left focus bound too if item isn't in leftmost column
4137 and reduce right box bound */
4138 if (DPA_GetPtrCount(infoPtr->hdpaColumns) > 0)
4140 INT leftmost;
4142 if ((leftmost = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX, 0, 0)))
4144 INT Originx = pos.x - LISTVIEW_GetColumnInfo(infoPtr, 0)->rcHeader.left;
4145 INT index = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX,
4146 DPA_GetPtrCount(infoPtr->hdpaColumns) - 1, 0);
4148 rcBox.right = LISTVIEW_GetColumnInfo(infoPtr, index)->rcHeader.right + Originx;
4149 rcSelect.left = LISTVIEW_GetColumnInfo(infoPtr, leftmost)->rcHeader.left + Originx;
4153 rcSelect.right = rcBox.right;
4156 if (nmlvcd.clrTextBk != CLR_NONE)
4157 ExtTextOutW(hdc, rcSelect.left, rcSelect.top, ETO_OPAQUE, &rcSelect, NULL, 0, NULL);
4158 /* store new focus rectangle */
4159 if (infoPtr->nFocusedItem == nItem) infoPtr->rcFocus = rcSelect;
4162 /* state icons */
4163 if (infoPtr->himlState && STATEIMAGEINDEX(lvItem.state) && (nSubItem == 0))
4165 UINT uStateImage = STATEIMAGEINDEX(lvItem.state);
4166 if (uStateImage)
4168 TRACE("uStateImage=%d\n", uStateImage);
4169 ImageList_Draw(infoPtr->himlState, uStateImage - 1, hdc,
4170 rcStateIcon.left, rcStateIcon.top, ILD_NORMAL);
4174 /* small icons */
4175 himl = (infoPtr->uView == LV_VIEW_ICON ? infoPtr->himlNormal : infoPtr->himlSmall);
4176 if (himl && lvItem.iImage >= 0 && !IsRectEmpty(&rcIcon))
4178 TRACE("iImage=%d\n", lvItem.iImage);
4179 ImageList_DrawEx(himl, lvItem.iImage, hdc, rcIcon.left, rcIcon.top,
4180 rcIcon.right - rcIcon.left, rcIcon.bottom - rcIcon.top, infoPtr->clrBk, CLR_DEFAULT,
4181 (lvItem.state & LVIS_SELECTED) && (infoPtr->bFocus) ? ILD_SELECTED : ILD_NORMAL);
4184 /* Don't bother painting item being edited */
4185 if (infoPtr->hwndEdit && nItem == infoPtr->nEditLabelItem && nSubItem == 0) goto postpaint;
4187 /* figure out the text drawing flags */
4188 uFormat = (infoPtr->uView == LV_VIEW_ICON ? (lprcFocus ? LV_FL_DT_FLAGS : LV_ML_DT_FLAGS) : LV_SL_DT_FLAGS);
4189 if (infoPtr->uView == LV_VIEW_ICON)
4190 uFormat = (lprcFocus ? LV_FL_DT_FLAGS : LV_ML_DT_FLAGS);
4191 else if (nSubItem)
4193 switch (LISTVIEW_GetColumnInfo(infoPtr, nSubItem)->fmt & LVCFMT_JUSTIFYMASK)
4195 case LVCFMT_RIGHT: uFormat |= DT_RIGHT; break;
4196 case LVCFMT_CENTER: uFormat |= DT_CENTER; break;
4197 default: uFormat |= DT_LEFT;
4200 if (!(uFormat & (DT_RIGHT | DT_CENTER)))
4202 if (himl && lvItem.iImage >= 0 && !IsRectEmpty(&rcIcon)) rcLabel.left += IMAGE_PADDING;
4203 else rcLabel.left += LABEL_HOR_PADDING;
4205 else if (uFormat & DT_RIGHT) rcLabel.right -= LABEL_HOR_PADDING;
4207 /* for GRIDLINES reduce the bottom so the text formats correctly */
4208 if (infoPtr->uView == LV_VIEW_DETAILS && infoPtr->dwLvExStyle & LVS_EX_GRIDLINES)
4209 rcLabel.bottom--;
4211 DrawTextW(hdc, lvItem.pszText, -1, &rcLabel, uFormat);
4213 postpaint:
4214 if (cdsubitemmode & CDRF_NOTIFYPOSTPAINT)
4215 notify_postpaint(infoPtr, &nmlvcd);
4216 if (cdsubitemmode & CDRF_NEWFONT)
4217 SelectObject(hdc, hOldFont);
4218 return TRUE;
4221 /***
4222 * DESCRIPTION:
4223 * Draws listview items when in owner draw mode.
4225 * PARAMETER(S):
4226 * [I] infoPtr : valid pointer to the listview structure
4227 * [I] hdc : device context handle
4229 * RETURN:
4230 * None
4232 static void LISTVIEW_RefreshOwnerDraw(const LISTVIEW_INFO *infoPtr, ITERATOR *i, HDC hdc, DWORD cdmode)
4234 UINT uID = (UINT)GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
4235 DWORD cditemmode = CDRF_DODEFAULT;
4236 NMLVCUSTOMDRAW nmlvcd;
4237 POINT Origin, Position;
4238 DRAWITEMSTRUCT dis;
4239 LVITEMW item;
4241 TRACE("()\n");
4243 ZeroMemory(&dis, sizeof(dis));
4245 /* Get scroll info once before loop */
4246 LISTVIEW_GetOrigin(infoPtr, &Origin);
4248 /* iterate through the invalidated rows */
4249 while(iterator_next(i))
4251 item.iItem = i->nItem;
4252 item.iSubItem = 0;
4253 item.mask = LVIF_PARAM | LVIF_STATE;
4254 item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
4255 if (!LISTVIEW_GetItemW(infoPtr, &item)) continue;
4257 dis.CtlType = ODT_LISTVIEW;
4258 dis.CtlID = uID;
4259 dis.itemID = item.iItem;
4260 dis.itemAction = ODA_DRAWENTIRE;
4261 dis.itemState = 0;
4262 if (item.state & LVIS_SELECTED) dis.itemState |= ODS_SELECTED;
4263 if (infoPtr->bFocus && (item.state & LVIS_FOCUSED)) dis.itemState |= ODS_FOCUS;
4264 dis.hwndItem = infoPtr->hwndSelf;
4265 dis.hDC = hdc;
4266 LISTVIEW_GetItemOrigin(infoPtr, dis.itemID, &Position);
4267 dis.rcItem.left = Position.x + Origin.x;
4268 dis.rcItem.right = dis.rcItem.left + infoPtr->nItemWidth;
4269 dis.rcItem.top = Position.y + Origin.y;
4270 dis.rcItem.bottom = dis.rcItem.top + infoPtr->nItemHeight;
4271 dis.itemData = item.lParam;
4273 TRACE("item=%s, rcItem=%s\n", debuglvitem_t(&item, TRUE), wine_dbgstr_rect(&dis.rcItem));
4276 * Even if we do not send the CDRF_NOTIFYITEMDRAW we need to fill the nmlvcd
4277 * structure for the rest. of the paint cycle
4279 customdraw_fill(&nmlvcd, infoPtr, hdc, &dis.rcItem, &item);
4280 if (cdmode & CDRF_NOTIFYITEMDRAW)
4281 cditemmode = notify_customdraw(infoPtr, CDDS_PREPAINT, &nmlvcd);
4283 if (!(cditemmode & CDRF_SKIPDEFAULT))
4285 prepaint_setup (infoPtr, hdc, &nmlvcd, FALSE);
4286 SendMessageW(infoPtr->hwndNotify, WM_DRAWITEM, dis.CtlID, (LPARAM)&dis);
4289 if (cditemmode & CDRF_NOTIFYPOSTPAINT)
4290 notify_postpaint(infoPtr, &nmlvcd);
4294 /***
4295 * DESCRIPTION:
4296 * Draws listview items when in report display mode.
4298 * PARAMETER(S):
4299 * [I] infoPtr : valid pointer to the listview structure
4300 * [I] hdc : device context handle
4301 * [I] cdmode : custom draw mode
4303 * RETURN:
4304 * None
4306 static void LISTVIEW_RefreshReport(LISTVIEW_INFO *infoPtr, ITERATOR *i, HDC hdc, DWORD cdmode)
4308 INT rgntype;
4309 RECT rcClip, rcItem;
4310 POINT Origin, Position;
4311 RANGES colRanges;
4312 INT col, index;
4313 ITERATOR j;
4315 TRACE("()\n");
4317 /* figure out what to draw */
4318 rgntype = GetClipBox(hdc, &rcClip);
4319 if (rgntype == NULLREGION) return;
4321 /* Get scroll info once before loop */
4322 LISTVIEW_GetOrigin(infoPtr, &Origin);
4324 colRanges = ranges_create(DPA_GetPtrCount(infoPtr->hdpaColumns));
4326 /* narrow down the columns we need to paint */
4327 for(col = 0; col < DPA_GetPtrCount(infoPtr->hdpaColumns); col++)
4329 index = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX, col, 0);
4331 LISTVIEW_GetHeaderRect(infoPtr, index, &rcItem);
4332 if ((rcItem.right + Origin.x >= rcClip.left) && (rcItem.left + Origin.x < rcClip.right))
4333 ranges_additem(colRanges, index);
4335 iterator_rangesitems(&j, colRanges);
4337 /* in full row select, we _have_ to draw the main item */
4338 if (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT)
4339 j.nSpecial = 0;
4341 /* iterate through the invalidated rows */
4342 while(iterator_next(i))
4344 LISTVIEW_GetItemOrigin(infoPtr, i->nItem, &Position);
4345 Position.y += Origin.y;
4347 /* iterate through the invalidated columns */
4348 while(iterator_next(&j))
4350 LISTVIEW_GetHeaderRect(infoPtr, j.nItem, &rcItem);
4351 Position.x = (j.nItem == 0) ? rcItem.left + Origin.x : Origin.x;
4353 if (rgntype == COMPLEXREGION && !((infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT) && j.nItem == 0))
4355 rcItem.top = 0;
4356 rcItem.bottom = infoPtr->nItemHeight;
4357 OffsetRect(&rcItem, Position.x, Position.y);
4358 if (!RectVisible(hdc, &rcItem)) continue;
4361 LISTVIEW_DrawItem(infoPtr, hdc, i->nItem, j.nItem, Position, cdmode);
4364 iterator_destroy(&j);
4367 /***
4368 * DESCRIPTION:
4369 * Draws the gridlines if necessary when in report display mode.
4371 * PARAMETER(S):
4372 * [I] infoPtr : valid pointer to the listview structure
4373 * [I] hdc : device context handle
4375 * RETURN:
4376 * None
4378 static void LISTVIEW_RefreshReportGrid(LISTVIEW_INFO *infoPtr, HDC hdc)
4380 INT rgntype;
4381 INT y, itemheight;
4382 INT col, index;
4383 HPEN hPen, hOldPen;
4384 RECT rcClip, rcItem = {0};
4385 POINT Origin;
4386 RANGES colRanges;
4387 ITERATOR j;
4388 BOOL rmost = FALSE;
4390 TRACE("()\n");
4392 /* figure out what to draw */
4393 rgntype = GetClipBox(hdc, &rcClip);
4394 if (rgntype == NULLREGION) return;
4396 /* Get scroll info once before loop */
4397 LISTVIEW_GetOrigin(infoPtr, &Origin);
4399 colRanges = ranges_create(DPA_GetPtrCount(infoPtr->hdpaColumns));
4401 /* narrow down the columns we need to paint */
4402 for(col = 0; col < DPA_GetPtrCount(infoPtr->hdpaColumns); col++)
4404 index = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX, col, 0);
4406 LISTVIEW_GetHeaderRect(infoPtr, index, &rcItem);
4407 if ((rcItem.right + Origin.x >= rcClip.left) && (rcItem.left + Origin.x < rcClip.right))
4408 ranges_additem(colRanges, index);
4411 /* is right most vertical line visible? */
4412 if (DPA_GetPtrCount(infoPtr->hdpaColumns) > 0)
4414 index = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX, DPA_GetPtrCount(infoPtr->hdpaColumns) - 1, 0);
4415 LISTVIEW_GetHeaderRect(infoPtr, index, &rcItem);
4416 rmost = (rcItem.right + Origin.x < rcClip.right);
4419 if ((hPen = CreatePen( PS_SOLID, 1, comctl32_color.clr3dFace )))
4421 hOldPen = SelectObject ( hdc, hPen );
4423 /* draw the vertical lines for the columns */
4424 iterator_rangesitems(&j, colRanges);
4425 while(iterator_next(&j))
4427 LISTVIEW_GetHeaderRect(infoPtr, j.nItem, &rcItem);
4428 if (rcItem.left == 0) continue; /* skip leftmost column */
4429 rcItem.left += Origin.x;
4430 rcItem.right += Origin.x;
4431 rcItem.top = infoPtr->rcList.top;
4432 rcItem.bottom = infoPtr->rcList.bottom;
4433 TRACE("vert col=%d, rcItem=%s\n", j.nItem, wine_dbgstr_rect(&rcItem));
4434 MoveToEx (hdc, rcItem.left, rcItem.top, NULL);
4435 LineTo (hdc, rcItem.left, rcItem.bottom);
4437 iterator_destroy(&j);
4438 /* draw rightmost grid line if visible */
4439 if (rmost)
4441 index = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX,
4442 DPA_GetPtrCount(infoPtr->hdpaColumns) - 1, 0);
4443 LISTVIEW_GetHeaderRect(infoPtr, index, &rcItem);
4445 rcItem.right += Origin.x;
4447 MoveToEx (hdc, rcItem.right, infoPtr->rcList.top, NULL);
4448 LineTo (hdc, rcItem.right, infoPtr->rcList.bottom);
4451 /* draw the horizontial lines for the rows */
4452 itemheight = LISTVIEW_CalculateItemHeight(infoPtr);
4453 rcItem.left = infoPtr->rcList.left;
4454 rcItem.right = infoPtr->rcList.right;
4455 rcItem.bottom = rcItem.top = Origin.y - 1;
4456 MoveToEx(hdc, rcItem.left, rcItem.top, NULL);
4457 LineTo(hdc, rcItem.right, rcItem.top);
4458 for(y=itemheight-1+Origin.y; y<=infoPtr->rcList.bottom; y+=itemheight)
4460 rcItem.bottom = rcItem.top = y;
4461 TRACE("horz rcItem=%s\n", wine_dbgstr_rect(&rcItem));
4462 MoveToEx (hdc, rcItem.left, rcItem.top, NULL);
4463 LineTo (hdc, rcItem.right, rcItem.top);
4466 SelectObject( hdc, hOldPen );
4467 DeleteObject( hPen );
4471 /***
4472 * DESCRIPTION:
4473 * Draws listview items when in list display mode.
4475 * PARAMETER(S):
4476 * [I] infoPtr : valid pointer to the listview structure
4477 * [I] hdc : device context handle
4478 * [I] cdmode : custom draw mode
4480 * RETURN:
4481 * None
4483 static void LISTVIEW_RefreshList(LISTVIEW_INFO *infoPtr, ITERATOR *i, HDC hdc, DWORD cdmode)
4485 POINT Origin, Position;
4487 /* Get scroll info once before loop */
4488 LISTVIEW_GetOrigin(infoPtr, &Origin);
4490 while(iterator_prev(i))
4492 LISTVIEW_GetItemOrigin(infoPtr, i->nItem, &Position);
4493 Position.x += Origin.x;
4494 Position.y += Origin.y;
4496 LISTVIEW_DrawItem(infoPtr, hdc, i->nItem, 0, Position, cdmode);
4501 /***
4502 * DESCRIPTION:
4503 * Draws listview items.
4505 * PARAMETER(S):
4506 * [I] infoPtr : valid pointer to the listview structure
4507 * [I] hdc : device context handle
4508 * [I] prcErase : rect to be erased before refresh (may be NULL)
4510 * RETURN:
4511 * NoneX
4513 static void LISTVIEW_Refresh(LISTVIEW_INFO *infoPtr, HDC hdc, const RECT *prcErase)
4515 COLORREF oldTextColor = 0, oldBkColor = 0, oldClrTextBk, oldClrText;
4516 NMLVCUSTOMDRAW nmlvcd;
4517 HFONT hOldFont = 0;
4518 DWORD cdmode;
4519 INT oldBkMode = 0;
4520 RECT rcClient;
4521 ITERATOR i;
4522 HDC hdcOrig = hdc;
4523 HBITMAP hbmp = NULL;
4524 RANGE range;
4526 LISTVIEW_DUMP(infoPtr);
4528 if (infoPtr->dwLvExStyle & LVS_EX_DOUBLEBUFFER) {
4529 TRACE("double buffering\n");
4531 hdc = CreateCompatibleDC(hdcOrig);
4532 if (!hdc) {
4533 ERR("Failed to create DC for backbuffer\n");
4534 return;
4536 hbmp = CreateCompatibleBitmap(hdcOrig, infoPtr->rcList.right,
4537 infoPtr->rcList.bottom);
4538 if (!hbmp) {
4539 ERR("Failed to create bitmap for backbuffer\n");
4540 DeleteDC(hdc);
4541 return;
4544 SelectObject(hdc, hbmp);
4545 SelectObject(hdc, infoPtr->hFont);
4546 } else {
4547 /* Save dc values we're gonna trash while drawing
4548 * FIXME: Should be done in LISTVIEW_DrawItem() */
4549 hOldFont = SelectObject(hdc, infoPtr->hFont);
4550 oldBkMode = GetBkMode(hdc);
4551 oldBkColor = GetBkColor(hdc);
4552 oldTextColor = GetTextColor(hdc);
4555 infoPtr->bIsDrawing = TRUE;
4557 if (prcErase) {
4558 LISTVIEW_FillBkgnd(infoPtr, hdc, prcErase);
4559 } else if (infoPtr->dwLvExStyle & LVS_EX_DOUBLEBUFFER) {
4560 /* If no erasing was done (usually because RedrawWindow was called
4561 * with RDW_INVALIDATE only) we need to copy the old contents into
4562 * the backbuffer before continuing. */
4563 BitBlt(hdc, infoPtr->rcList.left, infoPtr->rcList.top,
4564 infoPtr->rcList.right - infoPtr->rcList.left,
4565 infoPtr->rcList.bottom - infoPtr->rcList.top,
4566 hdcOrig, infoPtr->rcList.left, infoPtr->rcList.top, SRCCOPY);
4569 /* FIXME: Shouldn't need to do this */
4570 oldClrTextBk = infoPtr->clrTextBk;
4571 oldClrText = infoPtr->clrText;
4573 infoPtr->cditemmode = CDRF_DODEFAULT;
4575 GetClientRect(infoPtr->hwndSelf, &rcClient);
4576 customdraw_fill(&nmlvcd, infoPtr, hdc, &rcClient, 0);
4577 cdmode = notify_customdraw(infoPtr, CDDS_PREPAINT, &nmlvcd);
4578 if (cdmode & CDRF_SKIPDEFAULT) goto enddraw;
4579 prepaint_setup(infoPtr, hdc, &nmlvcd, FALSE);
4581 /* Use these colors to draw the items */
4582 infoPtr->clrTextBk = nmlvcd.clrTextBk;
4583 infoPtr->clrText = nmlvcd.clrText;
4585 /* nothing to draw */
4586 if(infoPtr->nItemCount == 0) goto enddraw;
4588 /* figure out what we need to draw */
4589 iterator_visibleitems(&i, infoPtr, hdc);
4590 range = iterator_range(&i);
4592 /* send cache hint notification */
4593 if (infoPtr->dwStyle & LVS_OWNERDATA)
4595 NMLVCACHEHINT nmlv;
4597 ZeroMemory(&nmlv, sizeof(NMLVCACHEHINT));
4598 nmlv.iFrom = range.lower;
4599 nmlv.iTo = range.upper - 1;
4600 notify_hdr(infoPtr, LVN_ODCACHEHINT, &nmlv.hdr);
4603 if ((infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && (infoPtr->uView == LV_VIEW_DETAILS))
4604 LISTVIEW_RefreshOwnerDraw(infoPtr, &i, hdc, cdmode);
4605 else
4607 if (infoPtr->uView == LV_VIEW_DETAILS)
4608 LISTVIEW_RefreshReport(infoPtr, &i, hdc, cdmode);
4609 else /* LV_VIEW_LIST, LV_VIEW_ICON or LV_VIEW_SMALLICON */
4610 LISTVIEW_RefreshList(infoPtr, &i, hdc, cdmode);
4612 /* if we have a focus rect and it's visible, draw it */
4613 if (infoPtr->bFocus && range.lower <= infoPtr->nFocusedItem &&
4614 (range.upper - 1) >= infoPtr->nFocusedItem)
4615 LISTVIEW_DrawFocusRect(infoPtr, hdc);
4617 iterator_destroy(&i);
4619 enddraw:
4620 /* For LVS_EX_GRIDLINES go and draw lines */
4621 /* This includes the case where there were *no* items */
4622 if ((infoPtr->uView == LV_VIEW_DETAILS) && infoPtr->dwLvExStyle & LVS_EX_GRIDLINES)
4623 LISTVIEW_RefreshReportGrid(infoPtr, hdc);
4625 if (cdmode & CDRF_NOTIFYPOSTPAINT)
4626 notify_postpaint(infoPtr, &nmlvcd);
4628 infoPtr->clrTextBk = oldClrTextBk;
4629 infoPtr->clrText = oldClrText;
4631 if(hbmp) {
4632 BitBlt(hdcOrig, infoPtr->rcList.left, infoPtr->rcList.top,
4633 infoPtr->rcList.right - infoPtr->rcList.left,
4634 infoPtr->rcList.bottom - infoPtr->rcList.top,
4635 hdc, infoPtr->rcList.left, infoPtr->rcList.top, SRCCOPY);
4637 DeleteObject(hbmp);
4638 DeleteDC(hdc);
4639 } else {
4640 SelectObject(hdc, hOldFont);
4641 SetBkMode(hdc, oldBkMode);
4642 SetBkColor(hdc, oldBkColor);
4643 SetTextColor(hdc, oldTextColor);
4646 infoPtr->bIsDrawing = FALSE;
4650 /***
4651 * DESCRIPTION:
4652 * Calculates the approximate width and height of a given number of items.
4654 * PARAMETER(S):
4655 * [I] infoPtr : valid pointer to the listview structure
4656 * [I] nItemCount : number of items
4657 * [I] wWidth : width
4658 * [I] wHeight : height
4660 * RETURN:
4661 * Returns a DWORD. The width in the low word and the height in high word.
4663 static DWORD LISTVIEW_ApproximateViewRect(const LISTVIEW_INFO *infoPtr, INT nItemCount,
4664 WORD wWidth, WORD wHeight)
4666 INT nItemCountPerColumn = 1;
4667 INT nColumnCount = 0;
4668 DWORD dwViewRect = 0;
4670 if (nItemCount == -1)
4671 nItemCount = infoPtr->nItemCount;
4673 if (infoPtr->uView == LV_VIEW_LIST)
4675 if (wHeight == 0xFFFF)
4677 /* use current height */
4678 wHeight = infoPtr->rcList.bottom - infoPtr->rcList.top;
4681 if (wHeight < infoPtr->nItemHeight)
4682 wHeight = infoPtr->nItemHeight;
4684 if (nItemCount > 0)
4686 if (infoPtr->nItemHeight > 0)
4688 nItemCountPerColumn = wHeight / infoPtr->nItemHeight;
4689 if (nItemCountPerColumn == 0)
4690 nItemCountPerColumn = 1;
4692 if (nItemCount % nItemCountPerColumn != 0)
4693 nColumnCount = nItemCount / nItemCountPerColumn;
4694 else
4695 nColumnCount = nItemCount / nItemCountPerColumn + 1;
4699 /* Microsoft padding magic */
4700 wHeight = nItemCountPerColumn * infoPtr->nItemHeight + 2;
4701 wWidth = nColumnCount * infoPtr->nItemWidth + 2;
4703 dwViewRect = MAKELONG(wWidth, wHeight);
4705 else if (infoPtr->uView == LV_VIEW_DETAILS)
4707 RECT rcBox;
4709 if (infoPtr->nItemCount > 0)
4711 LISTVIEW_GetItemBox(infoPtr, 0, &rcBox);
4712 wWidth = rcBox.right - rcBox.left;
4713 wHeight = (rcBox.bottom - rcBox.top) * nItemCount;
4715 else
4717 /* use current height and width */
4718 if (wHeight == 0xffff)
4719 wHeight = infoPtr->rcList.bottom - infoPtr->rcList.top;
4720 if (wWidth == 0xffff)
4721 wWidth = infoPtr->rcList.right - infoPtr->rcList.left;
4724 dwViewRect = MAKELONG(wWidth, wHeight);
4726 else if (infoPtr->uView == LV_VIEW_SMALLICON)
4727 FIXME("uView == LV_VIEW_SMALLICON: not implemented\n");
4728 else if (infoPtr->uView == LV_VIEW_ICON)
4729 FIXME("uView == LV_VIEW_ICON: not implemented\n");
4731 return dwViewRect;
4734 /***
4735 * DESCRIPTION:
4736 * Cancel edit label with saving item text.
4738 * PARAMETER(S):
4739 * [I] infoPtr : valid pointer to the listview structure
4741 * RETURN:
4742 * Always returns TRUE.
4744 static LRESULT LISTVIEW_CancelEditLabel(LISTVIEW_INFO *infoPtr)
4746 /* handle value will be lost after LISTVIEW_EndEditLabelT */
4747 HWND edit = infoPtr->hwndEdit;
4749 LISTVIEW_EndEditLabelT(infoPtr, TRUE, IsWindowUnicode(infoPtr->hwndEdit));
4750 SendMessageW(edit, WM_CLOSE, 0, 0);
4752 return TRUE;
4755 /***
4756 * DESCRIPTION:
4757 * Create a drag image list for the specified item.
4759 * PARAMETER(S):
4760 * [I] infoPtr : valid pointer to the listview structure
4761 * [I] iItem : index of item
4762 * [O] lppt : Upper-left corner of the image
4764 * RETURN:
4765 * Returns a handle to the image list if successful, NULL otherwise.
4767 static HIMAGELIST LISTVIEW_CreateDragImage(LISTVIEW_INFO *infoPtr, INT iItem, LPPOINT lppt)
4769 RECT rcItem;
4770 SIZE size;
4771 POINT pos;
4772 HDC hdc, hdcOrig;
4773 HBITMAP hbmp, hOldbmp;
4774 HIMAGELIST dragList = 0;
4775 TRACE("iItem=%d Count=%d\n", iItem, infoPtr->nItemCount);
4777 if (iItem < 0 || iItem >= infoPtr->nItemCount)
4778 return 0;
4780 rcItem.left = LVIR_BOUNDS;
4781 if (!LISTVIEW_GetItemRect(infoPtr, iItem, &rcItem))
4782 return 0;
4784 lppt->x = rcItem.left;
4785 lppt->y = rcItem.top;
4787 size.cx = rcItem.right - rcItem.left;
4788 size.cy = rcItem.bottom - rcItem.top;
4790 hdcOrig = GetDC(infoPtr->hwndSelf);
4791 hdc = CreateCompatibleDC(hdcOrig);
4792 hbmp = CreateCompatibleBitmap(hdcOrig, size.cx, size.cy);
4793 hOldbmp = SelectObject(hdc, hbmp);
4795 rcItem.left = rcItem.top = 0;
4796 rcItem.right = size.cx;
4797 rcItem.bottom = size.cy;
4798 FillRect(hdc, &rcItem, infoPtr->hBkBrush);
4800 pos.x = pos.y = 0;
4801 if (LISTVIEW_DrawItem(infoPtr, hdc, iItem, 0, pos, infoPtr->cditemmode))
4803 dragList = ImageList_Create(size.cx, size.cy, ILC_COLOR, 10, 10);
4804 SelectObject(hdc, hOldbmp);
4805 ImageList_Add(dragList, hbmp, 0);
4807 else
4808 SelectObject(hdc, hOldbmp);
4810 DeleteObject(hbmp);
4811 DeleteDC(hdc);
4812 ReleaseDC(infoPtr->hwndSelf, hdcOrig);
4814 TRACE("ret=%p\n", dragList);
4816 return dragList;
4820 /***
4821 * DESCRIPTION:
4822 * Removes all listview items and subitems.
4824 * PARAMETER(S):
4825 * [I] infoPtr : valid pointer to the listview structure
4827 * RETURN:
4828 * SUCCESS : TRUE
4829 * FAILURE : FALSE
4831 static BOOL LISTVIEW_DeleteAllItems(LISTVIEW_INFO *infoPtr, BOOL destroy)
4833 NMLISTVIEW nmlv;
4834 HDPA hdpaSubItems = NULL;
4835 BOOL bSuppress;
4836 ITEMHDR *hdrItem;
4837 INT i, j;
4839 TRACE("()\n");
4841 /* we do it directly, to avoid notifications */
4842 ranges_clear(infoPtr->selectionRanges);
4843 infoPtr->nSelectionMark = -1;
4844 infoPtr->nFocusedItem = -1;
4845 SetRectEmpty(&infoPtr->rcFocus);
4846 /* But we are supposed to leave nHotItem as is! */
4849 /* send LVN_DELETEALLITEMS notification */
4850 ZeroMemory(&nmlv, sizeof(NMLISTVIEW));
4851 nmlv.iItem = -1;
4852 bSuppress = notify_listview(infoPtr, LVN_DELETEALLITEMS, &nmlv);
4854 for (i = infoPtr->nItemCount - 1; i >= 0; i--)
4856 if (!(infoPtr->dwStyle & LVS_OWNERDATA))
4858 /* send LVN_DELETEITEM notification, if not suppressed
4859 and if it is not a virtual listview */
4860 if (!bSuppress) notify_deleteitem(infoPtr, i);
4861 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, i);
4862 for (j = 0; j < DPA_GetPtrCount(hdpaSubItems); j++)
4864 hdrItem = DPA_GetPtr(hdpaSubItems, j);
4865 if (is_textW(hdrItem->pszText)) Free(hdrItem->pszText);
4866 Free(hdrItem);
4868 DPA_Destroy(hdpaSubItems);
4869 DPA_DeletePtr(infoPtr->hdpaItems, i);
4871 DPA_DeletePtr(infoPtr->hdpaPosX, i);
4872 DPA_DeletePtr(infoPtr->hdpaPosY, i);
4873 infoPtr->nItemCount --;
4876 if (!destroy)
4878 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
4879 LISTVIEW_UpdateScroll(infoPtr);
4881 LISTVIEW_InvalidateList(infoPtr);
4883 return TRUE;
4886 /***
4887 * DESCRIPTION:
4888 * Scrolls, and updates the columns, when a column is changing width.
4890 * PARAMETER(S):
4891 * [I] infoPtr : valid pointer to the listview structure
4892 * [I] nColumn : column to scroll
4893 * [I] dx : amount of scroll, in pixels
4895 * RETURN:
4896 * None.
4898 static void LISTVIEW_ScrollColumns(LISTVIEW_INFO *infoPtr, INT nColumn, INT dx)
4900 COLUMN_INFO *lpColumnInfo;
4901 RECT rcOld, rcCol;
4902 POINT ptOrigin;
4903 INT nCol;
4905 if (nColumn < 0 || DPA_GetPtrCount(infoPtr->hdpaColumns) < 1) return;
4906 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, min(nColumn, DPA_GetPtrCount(infoPtr->hdpaColumns) - 1));
4907 rcCol = lpColumnInfo->rcHeader;
4908 if (nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns))
4909 rcCol.left = rcCol.right;
4911 /* adjust the other columns */
4912 for (nCol = nColumn; nCol < DPA_GetPtrCount(infoPtr->hdpaColumns); nCol++)
4914 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, nCol);
4915 lpColumnInfo->rcHeader.left += dx;
4916 lpColumnInfo->rcHeader.right += dx;
4919 /* do not update screen if not in report mode */
4920 if (!is_redrawing(infoPtr) || infoPtr->uView != LV_VIEW_DETAILS) return;
4922 /* Need to reset the item width when inserting a new column */
4923 infoPtr->nItemWidth += dx;
4925 LISTVIEW_UpdateScroll(infoPtr);
4926 LISTVIEW_GetOrigin(infoPtr, &ptOrigin);
4928 /* scroll to cover the deleted column, and invalidate for redraw */
4929 rcOld = infoPtr->rcList;
4930 rcOld.left = ptOrigin.x + rcCol.left + dx;
4931 ScrollWindowEx(infoPtr->hwndSelf, dx, 0, &rcOld, &rcOld, 0, 0, SW_ERASE | SW_INVALIDATE);
4934 /***
4935 * DESCRIPTION:
4936 * Removes a column from the listview control.
4938 * PARAMETER(S):
4939 * [I] infoPtr : valid pointer to the listview structure
4940 * [I] nColumn : column index
4942 * RETURN:
4943 * SUCCESS : TRUE
4944 * FAILURE : FALSE
4946 static BOOL LISTVIEW_DeleteColumn(LISTVIEW_INFO *infoPtr, INT nColumn)
4948 RECT rcCol;
4950 TRACE("nColumn=%d\n", nColumn);
4952 if (nColumn < 0 || DPA_GetPtrCount(infoPtr->hdpaColumns) == 0
4953 || nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return FALSE;
4955 /* While the MSDN specifically says that column zero should not be deleted,
4956 what actually happens is that the column itself is deleted but no items or subitems
4957 are removed.
4960 LISTVIEW_GetHeaderRect(infoPtr, nColumn, &rcCol);
4962 if (!SendMessageW(infoPtr->hwndHeader, HDM_DELETEITEM, nColumn, 0))
4963 return FALSE;
4965 Free(DPA_GetPtr(infoPtr->hdpaColumns, nColumn));
4966 DPA_DeletePtr(infoPtr->hdpaColumns, nColumn);
4968 if (!(infoPtr->dwStyle & LVS_OWNERDATA) && nColumn)
4970 SUBITEM_INFO *lpSubItem, *lpDelItem;
4971 HDPA hdpaSubItems;
4972 INT nItem, nSubItem, i;
4974 for (nItem = 0; nItem < infoPtr->nItemCount; nItem++)
4976 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, nItem);
4977 nSubItem = 0;
4978 lpDelItem = 0;
4979 for (i = 1; i < DPA_GetPtrCount(hdpaSubItems); i++)
4981 lpSubItem = DPA_GetPtr(hdpaSubItems, i);
4982 if (lpSubItem->iSubItem == nColumn)
4984 nSubItem = i;
4985 lpDelItem = lpSubItem;
4987 else if (lpSubItem->iSubItem > nColumn)
4989 lpSubItem->iSubItem--;
4993 /* if we found our subitem, zapp it */
4994 if (nSubItem > 0)
4996 /* free string */
4997 if (is_textW(lpDelItem->hdr.pszText))
4998 Free(lpDelItem->hdr.pszText);
5000 /* free item */
5001 Free(lpDelItem);
5003 /* free dpa memory */
5004 DPA_DeletePtr(hdpaSubItems, nSubItem);
5009 /* update the other column info */
5010 LISTVIEW_UpdateItemSize(infoPtr);
5011 if(DPA_GetPtrCount(infoPtr->hdpaColumns) == 0)
5012 LISTVIEW_InvalidateList(infoPtr);
5013 else
5014 LISTVIEW_ScrollColumns(infoPtr, nColumn, -(rcCol.right - rcCol.left));
5016 return TRUE;
5019 /***
5020 * DESCRIPTION:
5021 * Invalidates the listview after an item's insertion or deletion.
5023 * PARAMETER(S):
5024 * [I] infoPtr : valid pointer to the listview structure
5025 * [I] nItem : item index
5026 * [I] dir : -1 if deleting, 1 if inserting
5028 * RETURN:
5029 * None
5031 static void LISTVIEW_ScrollOnInsert(LISTVIEW_INFO *infoPtr, INT nItem, INT dir)
5033 INT nPerCol, nItemCol, nItemRow;
5034 RECT rcScroll;
5035 POINT Origin;
5037 /* if we don't refresh, what's the point of scrolling? */
5038 if (!is_redrawing(infoPtr)) return;
5040 assert (abs(dir) == 1);
5042 /* arrange icons if autoarrange is on */
5043 if (is_autoarrange(infoPtr))
5045 BOOL arrange = TRUE;
5046 if (dir < 0 && nItem >= infoPtr->nItemCount) arrange = FALSE;
5047 if (dir > 0 && nItem == infoPtr->nItemCount - 1) arrange = FALSE;
5048 if (arrange) LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
5051 /* scrollbars need updating */
5052 LISTVIEW_UpdateScroll(infoPtr);
5054 /* figure out the item's position */
5055 if (infoPtr->uView == LV_VIEW_DETAILS)
5056 nPerCol = infoPtr->nItemCount + 1;
5057 else if (infoPtr->uView == LV_VIEW_LIST)
5058 nPerCol = LISTVIEW_GetCountPerColumn(infoPtr);
5059 else /* LV_VIEW_ICON, or LV_VIEW_SMALLICON */
5060 return;
5062 nItemCol = nItem / nPerCol;
5063 nItemRow = nItem % nPerCol;
5064 LISTVIEW_GetOrigin(infoPtr, &Origin);
5066 /* move the items below up a slot */
5067 rcScroll.left = nItemCol * infoPtr->nItemWidth;
5068 rcScroll.top = nItemRow * infoPtr->nItemHeight;
5069 rcScroll.right = rcScroll.left + infoPtr->nItemWidth;
5070 rcScroll.bottom = nPerCol * infoPtr->nItemHeight;
5071 OffsetRect(&rcScroll, Origin.x, Origin.y);
5072 TRACE("rcScroll=%s, dx=%d\n", wine_dbgstr_rect(&rcScroll), dir * infoPtr->nItemHeight);
5073 if (IntersectRect(&rcScroll, &rcScroll, &infoPtr->rcList))
5075 TRACE("Scrolling rcScroll=%s, rcList=%s\n", wine_dbgstr_rect(&rcScroll), wine_dbgstr_rect(&infoPtr->rcList));
5076 ScrollWindowEx(infoPtr->hwndSelf, 0, dir * infoPtr->nItemHeight,
5077 &rcScroll, &rcScroll, 0, 0, SW_ERASE | SW_INVALIDATE);
5080 /* report has only that column, so we're done */
5081 if (infoPtr->uView == LV_VIEW_DETAILS) return;
5083 /* now for LISTs, we have to deal with the columns to the right */
5084 rcScroll.left = (nItemCol + 1) * infoPtr->nItemWidth;
5085 rcScroll.top = 0;
5086 rcScroll.right = (infoPtr->nItemCount / nPerCol + 1) * infoPtr->nItemWidth;
5087 rcScroll.bottom = nPerCol * infoPtr->nItemHeight;
5088 OffsetRect(&rcScroll, Origin.x, Origin.y);
5089 if (IntersectRect(&rcScroll, &rcScroll, &infoPtr->rcList))
5090 ScrollWindowEx(infoPtr->hwndSelf, 0, dir * infoPtr->nItemHeight,
5091 &rcScroll, &rcScroll, 0, 0, SW_ERASE | SW_INVALIDATE);
5094 /***
5095 * DESCRIPTION:
5096 * Removes an item from the listview control.
5098 * PARAMETER(S):
5099 * [I] infoPtr : valid pointer to the listview structure
5100 * [I] nItem : item index
5102 * RETURN:
5103 * SUCCESS : TRUE
5104 * FAILURE : FALSE
5106 static BOOL LISTVIEW_DeleteItem(LISTVIEW_INFO *infoPtr, INT nItem)
5108 LVITEMW item;
5109 const BOOL is_icon = (infoPtr->uView == LV_VIEW_SMALLICON || infoPtr->uView == LV_VIEW_ICON);
5111 TRACE("(nItem=%d)\n", nItem);
5113 if (nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
5115 /* remove selection, and focus */
5116 item.state = 0;
5117 item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
5118 LISTVIEW_SetItemState(infoPtr, nItem, &item);
5120 /* send LVN_DELETEITEM notification. */
5121 if (!notify_deleteitem(infoPtr, nItem)) return FALSE;
5123 /* we need to do this here, because we'll be deleting stuff */
5124 if (is_icon)
5125 LISTVIEW_InvalidateItem(infoPtr, nItem);
5127 if (!(infoPtr->dwStyle & LVS_OWNERDATA))
5129 HDPA hdpaSubItems;
5130 ITEMHDR *hdrItem;
5131 ITEM_INFO *lpItem;
5132 ITEM_ID *lpID;
5133 INT i;
5135 hdpaSubItems = DPA_DeletePtr(infoPtr->hdpaItems, nItem);
5136 lpItem = DPA_GetPtr(hdpaSubItems, 0);
5138 /* free id struct */
5139 i = DPA_GetPtrIndex(infoPtr->hdpaItemIds, lpItem->id);
5140 lpID = DPA_GetPtr(infoPtr->hdpaItemIds, i);
5141 DPA_DeletePtr(infoPtr->hdpaItemIds, i);
5142 Free(lpID);
5143 for (i = 0; i < DPA_GetPtrCount(hdpaSubItems); i++)
5145 hdrItem = DPA_GetPtr(hdpaSubItems, i);
5146 if (is_textW(hdrItem->pszText)) Free(hdrItem->pszText);
5147 Free(hdrItem);
5149 DPA_Destroy(hdpaSubItems);
5152 if (is_icon)
5154 DPA_DeletePtr(infoPtr->hdpaPosX, nItem);
5155 DPA_DeletePtr(infoPtr->hdpaPosY, nItem);
5158 infoPtr->nItemCount--;
5159 LISTVIEW_ShiftIndices(infoPtr, nItem, -1);
5161 /* now is the invalidation fun */
5162 if (!is_icon)
5163 LISTVIEW_ScrollOnInsert(infoPtr, nItem, -1);
5164 return TRUE;
5168 /***
5169 * DESCRIPTION:
5170 * Callback implementation for editlabel control
5172 * PARAMETER(S):
5173 * [I] infoPtr : valid pointer to the listview structure
5174 * [I] storeText : store edit box text as item text
5175 * [I] isW : TRUE if psxText is Unicode, FALSE if it's ANSI
5177 * RETURN:
5178 * SUCCESS : TRUE
5179 * FAILURE : FALSE
5181 static BOOL LISTVIEW_EndEditLabelT(LISTVIEW_INFO *infoPtr, BOOL storeText, BOOL isW)
5183 HWND hwndSelf = infoPtr->hwndSelf;
5184 NMLVDISPINFOW dispInfo;
5185 INT editedItem = infoPtr->nEditLabelItem;
5186 BOOL bSame;
5187 WCHAR *pszText = NULL;
5188 BOOL res;
5190 if (storeText)
5192 DWORD len = isW ? GetWindowTextLengthW(infoPtr->hwndEdit) : GetWindowTextLengthA(infoPtr->hwndEdit);
5194 if (len)
5196 if ((pszText = Alloc((len+1) * (isW ? sizeof(WCHAR) : sizeof(CHAR)))))
5198 if (isW) GetWindowTextW(infoPtr->hwndEdit, pszText, len+1);
5199 else GetWindowTextA(infoPtr->hwndEdit, (CHAR*)pszText, len+1);
5204 TRACE("(pszText=%s, isW=%d)\n", debugtext_t(pszText, isW), isW);
5206 infoPtr->nEditLabelItem = -1;
5207 infoPtr->hwndEdit = 0;
5209 ZeroMemory(&dispInfo, sizeof(dispInfo));
5210 dispInfo.item.mask = LVIF_PARAM | LVIF_STATE | LVIF_TEXT;
5211 dispInfo.item.iItem = editedItem;
5212 dispInfo.item.iSubItem = 0;
5213 dispInfo.item.stateMask = ~0;
5214 if (!LISTVIEW_GetItemW(infoPtr, &dispInfo.item))
5216 res = FALSE;
5217 goto cleanup;
5220 if (isW)
5221 bSame = (lstrcmpW(dispInfo.item.pszText, pszText) == 0);
5222 else
5224 LPWSTR tmp = textdupTtoW(pszText, FALSE);
5225 bSame = (lstrcmpW(dispInfo.item.pszText, tmp) == 0);
5226 textfreeT(tmp, FALSE);
5228 if (bSame)
5230 res = TRUE;
5231 goto cleanup;
5234 /* add the text from the edit in */
5235 dispInfo.item.mask |= LVIF_TEXT;
5236 dispInfo.item.pszText = pszText;
5237 dispInfo.item.cchTextMax = textlenT(pszText, isW);
5239 /* Do we need to update the Item Text */
5240 if (!notify_dispinfoT(infoPtr, LVN_ENDLABELEDITW, &dispInfo, isW))
5242 res = FALSE;
5243 goto cleanup;
5245 if (!IsWindow(hwndSelf))
5247 res = FALSE;
5248 goto cleanup;
5250 if (!pszText) return TRUE;
5252 if (!(infoPtr->dwStyle & LVS_OWNERDATA))
5254 HDPA hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, editedItem);
5255 ITEM_INFO* lpItem = DPA_GetPtr(hdpaSubItems, 0);
5256 if (lpItem && lpItem->hdr.pszText == LPSTR_TEXTCALLBACKW)
5258 LISTVIEW_InvalidateItem(infoPtr, editedItem);
5259 res = TRUE;
5260 goto cleanup;
5264 ZeroMemory(&dispInfo, sizeof(dispInfo));
5265 dispInfo.item.mask = LVIF_TEXT;
5266 dispInfo.item.iItem = editedItem;
5267 dispInfo.item.iSubItem = 0;
5268 dispInfo.item.pszText = pszText;
5269 dispInfo.item.cchTextMax = textlenT(pszText, isW);
5270 res = LISTVIEW_SetItemT(infoPtr, &dispInfo.item, isW);
5272 cleanup:
5273 Free(pszText);
5275 return res;
5278 /***
5279 * DESCRIPTION:
5280 * Begin in place editing of specified list view item
5282 * PARAMETER(S):
5283 * [I] infoPtr : valid pointer to the listview structure
5284 * [I] nItem : item index
5285 * [I] isW : TRUE if it's a Unicode req, FALSE if ASCII
5287 * RETURN:
5288 * SUCCESS : TRUE
5289 * FAILURE : FALSE
5291 static HWND LISTVIEW_EditLabelT(LISTVIEW_INFO *infoPtr, INT nItem, BOOL isW)
5293 WCHAR szDispText[DISP_TEXT_SIZE] = { 0 };
5294 NMLVDISPINFOW dispInfo;
5295 RECT rect;
5296 SIZE sz;
5297 HWND hwndSelf = infoPtr->hwndSelf;
5298 HDC hdc;
5299 HFONT hOldFont = NULL;
5300 TEXTMETRICW textMetric;
5302 TRACE("(nItem=%d, isW=%d)\n", nItem, isW);
5304 if (~infoPtr->dwStyle & LVS_EDITLABELS) return 0;
5306 /* Is the EditBox still there, if so remove it */
5307 if(infoPtr->hwndEdit != 0)
5309 SetFocus(infoPtr->hwndSelf);
5310 infoPtr->hwndEdit = 0;
5313 if (nItem < 0 || nItem >= infoPtr->nItemCount) return 0;
5315 infoPtr->nEditLabelItem = nItem;
5317 LISTVIEW_SetSelection(infoPtr, nItem);
5318 LISTVIEW_SetItemFocus(infoPtr, nItem);
5319 LISTVIEW_InvalidateItem(infoPtr, nItem);
5321 rect.left = LVIR_LABEL;
5322 if (!LISTVIEW_GetItemRect(infoPtr, nItem, &rect)) return 0;
5324 ZeroMemory(&dispInfo, sizeof(dispInfo));
5325 dispInfo.item.mask = LVIF_PARAM | LVIF_STATE | LVIF_TEXT;
5326 dispInfo.item.iItem = nItem;
5327 dispInfo.item.iSubItem = 0;
5328 dispInfo.item.stateMask = ~0;
5329 dispInfo.item.pszText = szDispText;
5330 dispInfo.item.cchTextMax = DISP_TEXT_SIZE;
5331 if (!LISTVIEW_GetItemT(infoPtr, &dispInfo.item, isW)) return 0;
5333 infoPtr->hwndEdit = CreateEditLabelT(infoPtr, dispInfo.item.pszText, WS_VISIBLE, isW);
5334 if (!infoPtr->hwndEdit) return 0;
5336 if (notify_dispinfoT(infoPtr, LVN_BEGINLABELEDITW, &dispInfo, isW))
5338 if (!IsWindow(hwndSelf))
5339 return 0;
5340 SendMessageW(infoPtr->hwndEdit, WM_CLOSE, 0, 0);
5341 infoPtr->hwndEdit = 0;
5342 return 0;
5345 /* Now position and display edit box */
5346 hdc = GetDC(infoPtr->hwndSelf);
5348 /* Select the font to get appropriate metric dimensions */
5349 if(infoPtr->hFont != 0)
5350 hOldFont = SelectObject(hdc, infoPtr->hFont);
5352 /* Get String Length in pixels */
5353 GetTextExtentPoint32W(hdc, dispInfo.item.pszText, lstrlenW(dispInfo.item.pszText), &sz);
5355 /* Add Extra spacing for the next character */
5356 GetTextMetricsW(hdc, &textMetric);
5357 sz.cx += (textMetric.tmMaxCharWidth * 2);
5359 if(infoPtr->hFont != 0)
5360 SelectObject(hdc, hOldFont);
5362 ReleaseDC(infoPtr->hwndSelf, hdc);
5364 MoveWindow(infoPtr->hwndEdit, rect.left - 2, rect.top - 1, sz.cx,
5365 rect.bottom - rect.top + 2, FALSE);
5366 ShowWindow(infoPtr->hwndEdit, SW_NORMAL);
5367 SetFocus(infoPtr->hwndEdit);
5368 SendMessageW(infoPtr->hwndEdit, EM_SETSEL, 0, -1);
5369 return infoPtr->hwndEdit;
5373 /***
5374 * DESCRIPTION:
5375 * Ensures the specified item is visible, scrolling into view if necessary.
5377 * PARAMETER(S):
5378 * [I] infoPtr : valid pointer to the listview structure
5379 * [I] nItem : item index
5380 * [I] bPartial : partially or entirely visible
5382 * RETURN:
5383 * SUCCESS : TRUE
5384 * FAILURE : FALSE
5386 static BOOL LISTVIEW_EnsureVisible(LISTVIEW_INFO *infoPtr, INT nItem, BOOL bPartial)
5388 INT nScrollPosHeight = 0;
5389 INT nScrollPosWidth = 0;
5390 INT nHorzAdjust = 0;
5391 INT nVertAdjust = 0;
5392 INT nHorzDiff = 0;
5393 INT nVertDiff = 0;
5394 RECT rcItem, rcTemp;
5396 rcItem.left = LVIR_BOUNDS;
5397 if (!LISTVIEW_GetItemRect(infoPtr, nItem, &rcItem)) return FALSE;
5399 if (bPartial && IntersectRect(&rcTemp, &infoPtr->rcList, &rcItem)) return TRUE;
5401 if (rcItem.left < infoPtr->rcList.left || rcItem.right > infoPtr->rcList.right)
5403 /* scroll left/right, but in LV_VIEW_DETAILS mode */
5404 if (infoPtr->uView == LV_VIEW_LIST)
5405 nScrollPosWidth = infoPtr->nItemWidth;
5406 else if ((infoPtr->uView == LV_VIEW_SMALLICON) || (infoPtr->uView == LV_VIEW_ICON))
5407 nScrollPosWidth = 1;
5409 if (rcItem.left < infoPtr->rcList.left)
5411 nHorzAdjust = -1;
5412 if (infoPtr->uView != LV_VIEW_DETAILS) nHorzDiff = rcItem.left - infoPtr->rcList.left;
5414 else
5416 nHorzAdjust = 1;
5417 if (infoPtr->uView != LV_VIEW_DETAILS) nHorzDiff = rcItem.right - infoPtr->rcList.right;
5421 if (rcItem.top < infoPtr->rcList.top || rcItem.bottom > infoPtr->rcList.bottom)
5423 /* scroll up/down, but not in LVS_LIST mode */
5424 if (infoPtr->uView == LV_VIEW_DETAILS)
5425 nScrollPosHeight = infoPtr->nItemHeight;
5426 else if ((infoPtr->uView == LV_VIEW_ICON) || (infoPtr->uView == LV_VIEW_SMALLICON))
5427 nScrollPosHeight = 1;
5429 if (rcItem.top < infoPtr->rcList.top)
5431 nVertAdjust = -1;
5432 if (infoPtr->uView != LV_VIEW_LIST) nVertDiff = rcItem.top - infoPtr->rcList.top;
5434 else
5436 nVertAdjust = 1;
5437 if (infoPtr->uView != LV_VIEW_LIST) nVertDiff = rcItem.bottom - infoPtr->rcList.bottom;
5441 if (!nScrollPosWidth && !nScrollPosHeight) return TRUE;
5443 if (nScrollPosWidth)
5445 INT diff = nHorzDiff / nScrollPosWidth;
5446 if (nHorzDiff % nScrollPosWidth) diff += nHorzAdjust;
5447 LISTVIEW_HScroll(infoPtr, SB_INTERNAL, diff, 0);
5450 if (nScrollPosHeight)
5452 INT diff = nVertDiff / nScrollPosHeight;
5453 if (nVertDiff % nScrollPosHeight) diff += nVertAdjust;
5454 LISTVIEW_VScroll(infoPtr, SB_INTERNAL, diff, 0);
5457 return TRUE;
5460 /***
5461 * DESCRIPTION:
5462 * Searches for an item with specific characteristics.
5464 * PARAMETER(S):
5465 * [I] hwnd : window handle
5466 * [I] nStart : base item index
5467 * [I] lpFindInfo : item information to look for
5469 * RETURN:
5470 * SUCCESS : index of item
5471 * FAILURE : -1
5473 static INT LISTVIEW_FindItemW(const LISTVIEW_INFO *infoPtr, INT nStart,
5474 const LVFINDINFOW *lpFindInfo)
5476 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
5477 BOOL bWrap = FALSE, bNearest = FALSE;
5478 INT nItem = nStart + 1, nLast = infoPtr->nItemCount, nNearestItem = -1;
5479 ULONG xdist, ydist, dist, mindist = 0x7fffffff;
5480 POINT Position, Destination;
5481 LVITEMW lvItem;
5483 /* Search in virtual listviews should be done by application, not by
5484 listview control, so we just send LVN_ODFINDITEMW and return the result */
5485 if (infoPtr->dwStyle & LVS_OWNERDATA)
5487 NMLVFINDITEMW nmlv;
5489 nmlv.iStart = nStart;
5490 nmlv.lvfi = *lpFindInfo;
5491 return notify_hdr(infoPtr, LVN_ODFINDITEMW, (LPNMHDR)&nmlv.hdr);
5494 if (!lpFindInfo || nItem < 0) return -1;
5496 lvItem.mask = 0;
5497 if (lpFindInfo->flags & (LVFI_STRING | LVFI_PARTIAL))
5499 lvItem.mask |= LVIF_TEXT;
5500 lvItem.pszText = szDispText;
5501 lvItem.cchTextMax = DISP_TEXT_SIZE;
5504 if (lpFindInfo->flags & LVFI_WRAP)
5505 bWrap = TRUE;
5507 if ((lpFindInfo->flags & LVFI_NEARESTXY) &&
5508 (infoPtr->uView == LV_VIEW_ICON || infoPtr->uView == LV_VIEW_SMALLICON))
5510 POINT Origin;
5511 RECT rcArea;
5513 LISTVIEW_GetOrigin(infoPtr, &Origin);
5514 Destination.x = lpFindInfo->pt.x - Origin.x;
5515 Destination.y = lpFindInfo->pt.y - Origin.y;
5516 switch(lpFindInfo->vkDirection)
5518 case VK_DOWN: Destination.y += infoPtr->nItemHeight; break;
5519 case VK_UP: Destination.y -= infoPtr->nItemHeight; break;
5520 case VK_RIGHT: Destination.x += infoPtr->nItemWidth; break;
5521 case VK_LEFT: Destination.x -= infoPtr->nItemWidth; break;
5522 case VK_HOME: Destination.x = Destination.y = 0; break;
5523 case VK_NEXT: Destination.y += infoPtr->rcList.bottom - infoPtr->rcList.top; break;
5524 case VK_PRIOR: Destination.y -= infoPtr->rcList.bottom - infoPtr->rcList.top; break;
5525 case VK_END:
5526 LISTVIEW_GetAreaRect(infoPtr, &rcArea);
5527 Destination.x = rcArea.right;
5528 Destination.y = rcArea.bottom;
5529 break;
5530 default: ERR("Unknown vkDirection=%d\n", lpFindInfo->vkDirection);
5532 bNearest = TRUE;
5534 else Destination.x = Destination.y = 0;
5536 /* if LVFI_PARAM is specified, all other flags are ignored */
5537 if (lpFindInfo->flags & LVFI_PARAM)
5539 lvItem.mask |= LVIF_PARAM;
5540 bNearest = FALSE;
5541 lvItem.mask &= ~LVIF_TEXT;
5544 again:
5545 for (; nItem < nLast; nItem++)
5547 lvItem.iItem = nItem;
5548 lvItem.iSubItem = 0;
5549 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) continue;
5551 if (lvItem.mask & LVIF_PARAM)
5553 if (lpFindInfo->lParam == lvItem.lParam)
5554 return nItem;
5555 else
5556 continue;
5559 if (lvItem.mask & LVIF_TEXT)
5561 if (lpFindInfo->flags & LVFI_PARTIAL)
5563 if (strstrW(lvItem.pszText, lpFindInfo->psz) == NULL) continue;
5565 else
5567 if (lstrcmpW(lvItem.pszText, lpFindInfo->psz) != 0) continue;
5571 if (!bNearest) return nItem;
5573 /* This is very inefficient. To do a good job here,
5574 * we need a sorted array of (x,y) item positions */
5575 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position);
5577 /* compute the distance^2 to the destination */
5578 xdist = Destination.x - Position.x;
5579 ydist = Destination.y - Position.y;
5580 dist = xdist * xdist + ydist * ydist;
5582 /* remember the distance, and item if it's closer */
5583 if (dist < mindist)
5585 mindist = dist;
5586 nNearestItem = nItem;
5590 if (bWrap)
5592 nItem = 0;
5593 nLast = min(nStart + 1, infoPtr->nItemCount);
5594 bWrap = FALSE;
5595 goto again;
5598 return nNearestItem;
5601 /***
5602 * DESCRIPTION:
5603 * Searches for an item with specific characteristics.
5605 * PARAMETER(S):
5606 * [I] hwnd : window handle
5607 * [I] nStart : base item index
5608 * [I] lpFindInfo : item information to look for
5610 * RETURN:
5611 * SUCCESS : index of item
5612 * FAILURE : -1
5614 static INT LISTVIEW_FindItemA(const LISTVIEW_INFO *infoPtr, INT nStart,
5615 const LVFINDINFOA *lpFindInfo)
5617 BOOL hasText = lpFindInfo->flags & (LVFI_STRING | LVFI_PARTIAL);
5618 LVFINDINFOW fiw;
5619 INT res;
5620 LPWSTR strW = NULL;
5622 memcpy(&fiw, lpFindInfo, sizeof(fiw));
5623 if (hasText) fiw.psz = strW = textdupTtoW((LPCWSTR)lpFindInfo->psz, FALSE);
5624 res = LISTVIEW_FindItemW(infoPtr, nStart, &fiw);
5625 textfreeT(strW, FALSE);
5626 return res;
5629 /***
5630 * DESCRIPTION:
5631 * Retrieves the background image of the listview control.
5633 * PARAMETER(S):
5634 * [I] infoPtr : valid pointer to the listview structure
5635 * [O] lpBkImage : background image attributes
5637 * RETURN:
5638 * SUCCESS : TRUE
5639 * FAILURE : FALSE
5641 /* static BOOL LISTVIEW_GetBkImage(const LISTVIEW_INFO *infoPtr, LPLVBKIMAGE lpBkImage) */
5642 /* { */
5643 /* FIXME (listview, "empty stub!\n"); */
5644 /* return FALSE; */
5645 /* } */
5647 /***
5648 * DESCRIPTION:
5649 * Retrieves column attributes.
5651 * PARAMETER(S):
5652 * [I] infoPtr : valid pointer to the listview structure
5653 * [I] nColumn : column index
5654 * [IO] lpColumn : column information
5655 * [I] isW : if TRUE, then lpColumn is a LPLVCOLUMNW
5656 * otherwise it is in fact a LPLVCOLUMNA
5658 * RETURN:
5659 * SUCCESS : TRUE
5660 * FAILURE : FALSE
5662 static BOOL LISTVIEW_GetColumnT(const LISTVIEW_INFO *infoPtr, INT nColumn, LPLVCOLUMNW lpColumn, BOOL isW)
5664 COLUMN_INFO *lpColumnInfo;
5665 HDITEMW hdi;
5667 if (!lpColumn || nColumn < 0 || nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return FALSE;
5668 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, nColumn);
5670 /* initialize memory */
5671 ZeroMemory(&hdi, sizeof(hdi));
5673 if (lpColumn->mask & LVCF_TEXT)
5675 hdi.mask |= HDI_TEXT;
5676 hdi.pszText = lpColumn->pszText;
5677 hdi.cchTextMax = lpColumn->cchTextMax;
5680 if (lpColumn->mask & LVCF_IMAGE)
5681 hdi.mask |= HDI_IMAGE;
5683 if (lpColumn->mask & LVCF_ORDER)
5684 hdi.mask |= HDI_ORDER;
5686 if (lpColumn->mask & LVCF_SUBITEM)
5687 hdi.mask |= HDI_LPARAM;
5689 if (!SendMessageW(infoPtr->hwndHeader, isW ? HDM_GETITEMW : HDM_GETITEMA, nColumn, (LPARAM)&hdi)) return FALSE;
5691 if (lpColumn->mask & LVCF_FMT)
5692 lpColumn->fmt = lpColumnInfo->fmt;
5694 if (lpColumn->mask & LVCF_WIDTH)
5695 lpColumn->cx = lpColumnInfo->rcHeader.right - lpColumnInfo->rcHeader.left;
5697 if (lpColumn->mask & LVCF_IMAGE)
5698 lpColumn->iImage = hdi.iImage;
5700 if (lpColumn->mask & LVCF_ORDER)
5701 lpColumn->iOrder = hdi.iOrder;
5703 if (lpColumn->mask & LVCF_SUBITEM)
5704 lpColumn->iSubItem = hdi.lParam;
5706 return TRUE;
5710 static BOOL LISTVIEW_GetColumnOrderArray(const LISTVIEW_INFO *infoPtr, INT iCount, LPINT lpiArray)
5712 TRACE("iCount=%d, lpiArray=%p\n", iCount, lpiArray);
5714 if (!lpiArray)
5715 return FALSE;
5717 return SendMessageW(infoPtr->hwndHeader, HDM_GETORDERARRAY, iCount, (LPARAM)lpiArray);
5720 /***
5721 * DESCRIPTION:
5722 * Retrieves the column width.
5724 * PARAMETER(S):
5725 * [I] infoPtr : valid pointer to the listview structure
5726 * [I] int : column index
5728 * RETURN:
5729 * SUCCESS : column width
5730 * FAILURE : zero
5732 static INT LISTVIEW_GetColumnWidth(const LISTVIEW_INFO *infoPtr, INT nColumn)
5734 INT nColumnWidth = 0;
5735 HDITEMW hdItem;
5737 TRACE("nColumn=%d\n", nColumn);
5739 /* we have a 'column' in LIST and REPORT mode only */
5740 switch(infoPtr->uView)
5742 case LV_VIEW_LIST:
5743 nColumnWidth = infoPtr->nItemWidth;
5744 break;
5745 case LV_VIEW_DETAILS:
5746 /* We are not using LISTVIEW_GetHeaderRect as this data is updated only after a HDM_ITEMCHANGED.
5747 * There is an application that subclasses the listview, calls LVM_GETCOLUMNWIDTH in the
5748 * HDM_ITEMCHANGED handler and goes into infinite recursion if it receives old data.
5750 * TODO: should we do the same in LVM_GETCOLUMN?
5752 hdItem.mask = HDI_WIDTH;
5753 if (!SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, nColumn, (LPARAM)&hdItem))
5755 WARN("(%p): HDM_GETITEMW failed for item %d\n", infoPtr->hwndSelf, nColumn);
5756 return 0;
5758 nColumnWidth = hdItem.cxy;
5759 break;
5762 TRACE("nColumnWidth=%d\n", nColumnWidth);
5763 return nColumnWidth;
5766 /***
5767 * DESCRIPTION:
5768 * In list or report display mode, retrieves the number of items that can fit
5769 * vertically in the visible area. In icon or small icon display mode,
5770 * retrieves the total number of visible items.
5772 * PARAMETER(S):
5773 * [I] infoPtr : valid pointer to the listview structure
5775 * RETURN:
5776 * Number of fully visible items.
5778 static INT LISTVIEW_GetCountPerPage(const LISTVIEW_INFO *infoPtr)
5780 switch (infoPtr->uView)
5782 case LV_VIEW_ICON:
5783 case LV_VIEW_SMALLICON:
5784 return infoPtr->nItemCount;
5785 case LV_VIEW_DETAILS:
5786 return LISTVIEW_GetCountPerColumn(infoPtr);
5787 case LV_VIEW_LIST:
5788 return LISTVIEW_GetCountPerRow(infoPtr) * LISTVIEW_GetCountPerColumn(infoPtr);
5790 assert(FALSE);
5791 return 0;
5794 /***
5795 * DESCRIPTION:
5796 * Retrieves an image list handle.
5798 * PARAMETER(S):
5799 * [I] infoPtr : valid pointer to the listview structure
5800 * [I] nImageList : image list identifier
5802 * RETURN:
5803 * SUCCESS : image list handle
5804 * FAILURE : NULL
5806 static HIMAGELIST LISTVIEW_GetImageList(const LISTVIEW_INFO *infoPtr, INT nImageList)
5808 switch (nImageList)
5810 case LVSIL_NORMAL: return infoPtr->himlNormal;
5811 case LVSIL_SMALL: return infoPtr->himlSmall;
5812 case LVSIL_STATE: return infoPtr->himlState;
5814 return NULL;
5817 /* LISTVIEW_GetISearchString */
5819 /***
5820 * DESCRIPTION:
5821 * Retrieves item attributes.
5823 * PARAMETER(S):
5824 * [I] hwnd : window handle
5825 * [IO] lpLVItem : item info
5826 * [I] isW : if TRUE, then lpLVItem is a LPLVITEMW,
5827 * if FALSE, then lpLVItem is a LPLVITEMA.
5829 * NOTE:
5830 * This is the internal 'GetItem' interface -- it tries to
5831 * be smart and avoid text copies, if possible, by modifying
5832 * lpLVItem->pszText to point to the text string. Please note
5833 * that this is not always possible (e.g. OWNERDATA), so on
5834 * entry you *must* supply valid values for pszText, and cchTextMax.
5835 * The only difference to the documented interface is that upon
5836 * return, you should use *only* the lpLVItem->pszText, rather than
5837 * the buffer pointer you provided on input. Most code already does
5838 * that, so it's not a problem.
5839 * For the two cases when the text must be copied (that is,
5840 * for LVM_GETITEM, and LVM_GETITEMTEXT), use LISTVIEW_GetItemExtT.
5842 * RETURN:
5843 * SUCCESS : TRUE
5844 * FAILURE : FALSE
5846 static BOOL LISTVIEW_GetItemT(const LISTVIEW_INFO *infoPtr, LPLVITEMW lpLVItem, BOOL isW)
5848 ITEMHDR callbackHdr = { LPSTR_TEXTCALLBACKW, I_IMAGECALLBACK };
5849 NMLVDISPINFOW dispInfo;
5850 ITEM_INFO *lpItem;
5851 ITEMHDR* pItemHdr;
5852 HDPA hdpaSubItems;
5853 INT isubitem;
5855 TRACE("(lpLVItem=%s, isW=%d)\n", debuglvitem_t(lpLVItem, isW), isW);
5857 if (!lpLVItem || lpLVItem->iItem < 0 || lpLVItem->iItem >= infoPtr->nItemCount)
5858 return FALSE;
5860 if (lpLVItem->mask == 0) return TRUE;
5862 /* make a local copy */
5863 isubitem = lpLVItem->iSubItem;
5865 /* a quick optimization if all we're asked is the focus state
5866 * these queries are worth optimising since they are common,
5867 * and can be answered in constant time, without the heavy accesses */
5868 if ( (lpLVItem->mask == LVIF_STATE) && (lpLVItem->stateMask == LVIS_FOCUSED) &&
5869 !(infoPtr->uCallbackMask & LVIS_FOCUSED) )
5871 lpLVItem->state = 0;
5872 if (infoPtr->nFocusedItem == lpLVItem->iItem)
5873 lpLVItem->state |= LVIS_FOCUSED;
5874 return TRUE;
5877 ZeroMemory(&dispInfo, sizeof(dispInfo));
5879 /* if the app stores all the data, handle it separately */
5880 if (infoPtr->dwStyle & LVS_OWNERDATA)
5882 dispInfo.item.state = 0;
5884 /* apparently, we should not callback for lParam in LVS_OWNERDATA */
5885 if ((lpLVItem->mask & ~(LVIF_STATE | LVIF_PARAM)) ||
5886 ((lpLVItem->mask & LVIF_STATE) && (infoPtr->uCallbackMask & lpLVItem->stateMask)))
5888 UINT mask = lpLVItem->mask;
5890 /* NOTE: copy only fields which we _know_ are initialized, some apps
5891 * depend on the uninitialized fields being 0 */
5892 dispInfo.item.mask = lpLVItem->mask & ~LVIF_PARAM;
5893 dispInfo.item.iItem = lpLVItem->iItem;
5894 dispInfo.item.iSubItem = isubitem;
5895 if (lpLVItem->mask & LVIF_TEXT)
5897 if (lpLVItem->mask & LVIF_NORECOMPUTE)
5898 /* reset mask */
5899 dispInfo.item.mask &= ~(LVIF_TEXT | LVIF_NORECOMPUTE);
5900 else
5902 dispInfo.item.pszText = lpLVItem->pszText;
5903 dispInfo.item.cchTextMax = lpLVItem->cchTextMax;
5906 if (lpLVItem->mask & LVIF_STATE)
5907 dispInfo.item.stateMask = lpLVItem->stateMask & infoPtr->uCallbackMask;
5908 /* could be zeroed on LVIF_NORECOMPUTE case */
5909 if (dispInfo.item.mask != 0)
5911 notify_dispinfoT(infoPtr, LVN_GETDISPINFOW, &dispInfo, isW);
5912 dispInfo.item.stateMask = lpLVItem->stateMask;
5913 if (lpLVItem->mask & (LVIF_GROUPID|LVIF_COLUMNS))
5915 /* full size structure expected - _WIN32IE >= 0x560 */
5916 *lpLVItem = dispInfo.item;
5918 else if (lpLVItem->mask & LVIF_INDENT)
5920 /* indent member expected - _WIN32IE >= 0x300 */
5921 memcpy(lpLVItem, &dispInfo.item, offsetof( LVITEMW, iGroupId ));
5923 else
5925 /* minimal structure expected */
5926 memcpy(lpLVItem, &dispInfo.item, offsetof( LVITEMW, iIndent ));
5928 lpLVItem->mask = mask;
5929 TRACE(" getdispinfo(1):lpLVItem=%s\n", debuglvitem_t(lpLVItem, isW));
5933 /* make sure lParam is zeroed out */
5934 if (lpLVItem->mask & LVIF_PARAM) lpLVItem->lParam = 0;
5936 /* callback marked pointer required here */
5937 if ((lpLVItem->mask & LVIF_TEXT) && (lpLVItem->mask & LVIF_NORECOMPUTE))
5938 lpLVItem->pszText = LPSTR_TEXTCALLBACKW;
5940 /* we store only a little state, so if we're not asked, we're done */
5941 if (!(lpLVItem->mask & LVIF_STATE) || isubitem) return TRUE;
5943 /* if focus is handled by us, report it */
5944 if ( lpLVItem->stateMask & ~infoPtr->uCallbackMask & LVIS_FOCUSED )
5946 lpLVItem->state &= ~LVIS_FOCUSED;
5947 if (infoPtr->nFocusedItem == lpLVItem->iItem)
5948 lpLVItem->state |= LVIS_FOCUSED;
5951 /* and do the same for selection, if we handle it */
5952 if ( lpLVItem->stateMask & ~infoPtr->uCallbackMask & LVIS_SELECTED )
5954 lpLVItem->state &= ~LVIS_SELECTED;
5955 if (ranges_contain(infoPtr->selectionRanges, lpLVItem->iItem))
5956 lpLVItem->state |= LVIS_SELECTED;
5959 return TRUE;
5962 /* find the item and subitem structures before we proceed */
5963 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, lpLVItem->iItem);
5964 lpItem = DPA_GetPtr(hdpaSubItems, 0);
5965 assert (lpItem);
5967 if (isubitem)
5969 SUBITEM_INFO *lpSubItem = LISTVIEW_GetSubItemPtr(hdpaSubItems, isubitem);
5970 pItemHdr = lpSubItem ? &lpSubItem->hdr : &callbackHdr;
5971 if (!lpSubItem)
5973 WARN(" iSubItem invalid (%08x), ignored.\n", isubitem);
5974 isubitem = 0;
5977 else
5978 pItemHdr = &lpItem->hdr;
5980 /* Do we need to query the state from the app? */
5981 if ((lpLVItem->mask & LVIF_STATE) && infoPtr->uCallbackMask && isubitem == 0)
5983 dispInfo.item.mask |= LVIF_STATE;
5984 dispInfo.item.stateMask = infoPtr->uCallbackMask;
5987 /* Do we need to enquire about the image? */
5988 if ((lpLVItem->mask & LVIF_IMAGE) && pItemHdr->iImage == I_IMAGECALLBACK &&
5989 (isubitem == 0 || (infoPtr->dwLvExStyle & LVS_EX_SUBITEMIMAGES)))
5991 dispInfo.item.mask |= LVIF_IMAGE;
5992 dispInfo.item.iImage = I_IMAGECALLBACK;
5995 /* Only items support indentation */
5996 if ((lpLVItem->mask & LVIF_INDENT) && lpItem->iIndent == I_INDENTCALLBACK &&
5997 (isubitem == 0))
5999 dispInfo.item.mask |= LVIF_INDENT;
6000 dispInfo.item.iIndent = I_INDENTCALLBACK;
6003 /* Apps depend on calling back for text if it is NULL or LPSTR_TEXTCALLBACKW */
6004 if ((lpLVItem->mask & LVIF_TEXT) && !(lpLVItem->mask & LVIF_NORECOMPUTE) &&
6005 !is_textW(pItemHdr->pszText))
6007 dispInfo.item.mask |= LVIF_TEXT;
6008 dispInfo.item.pszText = lpLVItem->pszText;
6009 dispInfo.item.cchTextMax = lpLVItem->cchTextMax;
6010 if (dispInfo.item.pszText && dispInfo.item.cchTextMax > 0)
6011 *dispInfo.item.pszText = '\0';
6014 /* If we don't have all the requested info, query the application */
6015 if (dispInfo.item.mask != 0)
6017 dispInfo.item.iItem = lpLVItem->iItem;
6018 dispInfo.item.iSubItem = lpLVItem->iSubItem; /* yes: the original subitem */
6019 dispInfo.item.lParam = lpItem->lParam;
6020 notify_dispinfoT(infoPtr, LVN_GETDISPINFOW, &dispInfo, isW);
6021 TRACE(" getdispinfo(2):item=%s\n", debuglvitem_t(&dispInfo.item, isW));
6024 /* we should not store values for subitems */
6025 if (isubitem) dispInfo.item.mask &= ~LVIF_DI_SETITEM;
6027 /* Now, handle the iImage field */
6028 if (dispInfo.item.mask & LVIF_IMAGE)
6030 lpLVItem->iImage = dispInfo.item.iImage;
6031 if ((dispInfo.item.mask & LVIF_DI_SETITEM) && pItemHdr->iImage == I_IMAGECALLBACK)
6032 pItemHdr->iImage = dispInfo.item.iImage;
6034 else if (lpLVItem->mask & LVIF_IMAGE)
6036 if(isubitem == 0 || (infoPtr->dwLvExStyle & LVS_EX_SUBITEMIMAGES))
6037 lpLVItem->iImage = pItemHdr->iImage;
6038 else
6039 lpLVItem->iImage = 0;
6042 /* The pszText field */
6043 if (dispInfo.item.mask & LVIF_TEXT)
6045 if ((dispInfo.item.mask & LVIF_DI_SETITEM) && pItemHdr->pszText)
6046 textsetptrT(&pItemHdr->pszText, dispInfo.item.pszText, isW);
6048 lpLVItem->pszText = dispInfo.item.pszText;
6050 else if (lpLVItem->mask & LVIF_TEXT)
6052 /* if LVN_GETDISPINFO's disabled with LVIF_NORECOMPUTE return callback placeholder */
6053 if (isW || !is_textW(pItemHdr->pszText)) lpLVItem->pszText = pItemHdr->pszText;
6054 else textcpynT(lpLVItem->pszText, isW, pItemHdr->pszText, TRUE, lpLVItem->cchTextMax);
6057 /* Next is the lParam field */
6058 if (dispInfo.item.mask & LVIF_PARAM)
6060 lpLVItem->lParam = dispInfo.item.lParam;
6061 if ((dispInfo.item.mask & LVIF_DI_SETITEM))
6062 lpItem->lParam = dispInfo.item.lParam;
6064 else if (lpLVItem->mask & LVIF_PARAM)
6065 lpLVItem->lParam = lpItem->lParam;
6067 /* if this is a subitem, we're done */
6068 if (isubitem) return TRUE;
6070 /* ... the state field (this one is different due to uCallbackmask) */
6071 if (lpLVItem->mask & LVIF_STATE)
6073 lpLVItem->state = lpItem->state & lpLVItem->stateMask;
6074 if (dispInfo.item.mask & LVIF_STATE)
6076 lpLVItem->state &= ~dispInfo.item.stateMask;
6077 lpLVItem->state |= (dispInfo.item.state & dispInfo.item.stateMask);
6079 if ( lpLVItem->stateMask & ~infoPtr->uCallbackMask & LVIS_FOCUSED )
6081 lpLVItem->state &= ~LVIS_FOCUSED;
6082 if (infoPtr->nFocusedItem == lpLVItem->iItem)
6083 lpLVItem->state |= LVIS_FOCUSED;
6085 if ( lpLVItem->stateMask & ~infoPtr->uCallbackMask & LVIS_SELECTED )
6087 lpLVItem->state &= ~LVIS_SELECTED;
6088 if (ranges_contain(infoPtr->selectionRanges, lpLVItem->iItem))
6089 lpLVItem->state |= LVIS_SELECTED;
6093 /* and last, but not least, the indent field */
6094 if (dispInfo.item.mask & LVIF_INDENT)
6096 lpLVItem->iIndent = dispInfo.item.iIndent;
6097 if ((dispInfo.item.mask & LVIF_DI_SETITEM) && lpItem->iIndent == I_INDENTCALLBACK)
6098 lpItem->iIndent = dispInfo.item.iIndent;
6100 else if (lpLVItem->mask & LVIF_INDENT)
6102 lpLVItem->iIndent = lpItem->iIndent;
6105 return TRUE;
6108 /***
6109 * DESCRIPTION:
6110 * Retrieves item attributes.
6112 * PARAMETER(S):
6113 * [I] hwnd : window handle
6114 * [IO] lpLVItem : item info
6115 * [I] isW : if TRUE, then lpLVItem is a LPLVITEMW,
6116 * if FALSE, then lpLVItem is a LPLVITEMA.
6118 * NOTE:
6119 * This is the external 'GetItem' interface -- it properly copies
6120 * the text in the provided buffer.
6122 * RETURN:
6123 * SUCCESS : TRUE
6124 * FAILURE : FALSE
6126 static BOOL LISTVIEW_GetItemExtT(const LISTVIEW_INFO *infoPtr, LPLVITEMW lpLVItem, BOOL isW)
6128 LPWSTR pszText;
6129 BOOL bResult;
6131 if (!lpLVItem || lpLVItem->iItem < 0 || lpLVItem->iItem >= infoPtr->nItemCount)
6132 return FALSE;
6134 pszText = lpLVItem->pszText;
6135 bResult = LISTVIEW_GetItemT(infoPtr, lpLVItem, isW);
6136 if (bResult && lpLVItem->pszText != pszText)
6138 if (lpLVItem->pszText != LPSTR_TEXTCALLBACKW)
6139 textcpynT(pszText, isW, lpLVItem->pszText, isW, lpLVItem->cchTextMax);
6140 else
6141 pszText = LPSTR_TEXTCALLBACKW;
6143 lpLVItem->pszText = pszText;
6145 return bResult;
6149 /***
6150 * DESCRIPTION:
6151 * Retrieves the position (upper-left) of the listview control item.
6152 * Note that for LVS_ICON style, the upper-left is that of the icon
6153 * and not the bounding box.
6155 * PARAMETER(S):
6156 * [I] infoPtr : valid pointer to the listview structure
6157 * [I] nItem : item index
6158 * [O] lpptPosition : coordinate information
6160 * RETURN:
6161 * SUCCESS : TRUE
6162 * FAILURE : FALSE
6164 static BOOL LISTVIEW_GetItemPosition(const LISTVIEW_INFO *infoPtr, INT nItem, LPPOINT lpptPosition)
6166 POINT Origin;
6168 TRACE("(nItem=%d, lpptPosition=%p)\n", nItem, lpptPosition);
6170 if (!lpptPosition || nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
6172 LISTVIEW_GetOrigin(infoPtr, &Origin);
6173 LISTVIEW_GetItemOrigin(infoPtr, nItem, lpptPosition);
6175 if (infoPtr->uView == LV_VIEW_ICON)
6177 lpptPosition->x += (infoPtr->nItemWidth - infoPtr->iconSize.cx) / 2;
6178 lpptPosition->y += ICON_TOP_PADDING;
6180 lpptPosition->x += Origin.x;
6181 lpptPosition->y += Origin.y;
6183 TRACE (" lpptPosition=%s\n", wine_dbgstr_point(lpptPosition));
6184 return TRUE;
6188 /***
6189 * DESCRIPTION:
6190 * Retrieves the bounding rectangle for a listview control item.
6192 * PARAMETER(S):
6193 * [I] infoPtr : valid pointer to the listview structure
6194 * [I] nItem : item index
6195 * [IO] lprc : bounding rectangle coordinates
6196 * lprc->left specifies the portion of the item for which the bounding
6197 * rectangle will be retrieved.
6199 * LVIR_BOUNDS Returns the bounding rectangle of the entire item,
6200 * including the icon and label.
6202 * * For LVS_ICON
6203 * * Experiment shows that native control returns:
6204 * * width = min (48, length of text line)
6205 * * .left = position.x - (width - iconsize.cx)/2
6206 * * .right = .left + width
6207 * * height = #lines of text * ntmHeight + icon height + 8
6208 * * .top = position.y - 2
6209 * * .bottom = .top + height
6210 * * separation between items .y = itemSpacing.cy - height
6211 * * .x = itemSpacing.cx - width
6212 * LVIR_ICON Returns the bounding rectangle of the icon or small icon.
6214 * * For LVS_ICON
6215 * * Experiment shows that native control returns:
6216 * * width = iconSize.cx + 16
6217 * * .left = position.x - (width - iconsize.cx)/2
6218 * * .right = .left + width
6219 * * height = iconSize.cy + 4
6220 * * .top = position.y - 2
6221 * * .bottom = .top + height
6222 * * separation between items .y = itemSpacing.cy - height
6223 * * .x = itemSpacing.cx - width
6224 * LVIR_LABEL Returns the bounding rectangle of the item text.
6226 * * For LVS_ICON
6227 * * Experiment shows that native control returns:
6228 * * width = text length
6229 * * .left = position.x - width/2
6230 * * .right = .left + width
6231 * * height = ntmH * linecount + 2
6232 * * .top = position.y + iconSize.cy + 6
6233 * * .bottom = .top + height
6234 * * separation between items .y = itemSpacing.cy - height
6235 * * .x = itemSpacing.cx - width
6236 * LVIR_SELECTBOUNDS Returns the union of the LVIR_ICON and LVIR_LABEL
6237 * rectangles, but excludes columns in report view.
6239 * RETURN:
6240 * SUCCESS : TRUE
6241 * FAILURE : FALSE
6243 * NOTES
6244 * Note that the bounding rectangle of the label in the LVS_ICON view depends
6245 * upon whether the window has the focus currently and on whether the item
6246 * is the one with the focus. Ensure that the control's record of which
6247 * item has the focus agrees with the items' records.
6249 static BOOL LISTVIEW_GetItemRect(const LISTVIEW_INFO *infoPtr, INT nItem, LPRECT lprc)
6251 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
6252 BOOL doLabel = TRUE, oversizedBox = FALSE;
6253 POINT Position, Origin;
6254 LVITEMW lvItem;
6256 TRACE("(hwnd=%p, nItem=%d, lprc=%p)\n", infoPtr->hwndSelf, nItem, lprc);
6258 if (!lprc || nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
6260 LISTVIEW_GetOrigin(infoPtr, &Origin);
6261 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position);
6263 /* Be smart and try to figure out the minimum we have to do */
6264 if (lprc->left == LVIR_ICON) doLabel = FALSE;
6265 if (infoPtr->uView == LV_VIEW_DETAILS && lprc->left == LVIR_BOUNDS) doLabel = FALSE;
6266 if (infoPtr->uView == LV_VIEW_ICON && lprc->left != LVIR_ICON &&
6267 infoPtr->bFocus && LISTVIEW_GetItemState(infoPtr, nItem, LVIS_FOCUSED))
6268 oversizedBox = TRUE;
6270 /* get what we need from the item before hand, so we make
6271 * only one request. This can speed up things, if data
6272 * is stored on the app side */
6273 lvItem.mask = 0;
6274 if (infoPtr->uView == LV_VIEW_DETAILS) lvItem.mask |= LVIF_INDENT;
6275 if (doLabel) lvItem.mask |= LVIF_TEXT;
6276 lvItem.iItem = nItem;
6277 lvItem.iSubItem = 0;
6278 lvItem.pszText = szDispText;
6279 lvItem.cchTextMax = DISP_TEXT_SIZE;
6280 if (lvItem.mask && !LISTVIEW_GetItemW(infoPtr, &lvItem)) return FALSE;
6281 /* we got the state already up, simulate it here, to avoid a reget */
6282 if (infoPtr->uView == LV_VIEW_ICON && (lprc->left != LVIR_ICON))
6284 lvItem.mask |= LVIF_STATE;
6285 lvItem.stateMask = LVIS_FOCUSED;
6286 lvItem.state = (oversizedBox ? LVIS_FOCUSED : 0);
6289 if (infoPtr->uView == LV_VIEW_DETAILS && (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT) && lprc->left == LVIR_SELECTBOUNDS)
6290 lprc->left = LVIR_BOUNDS;
6291 switch(lprc->left)
6293 case LVIR_ICON:
6294 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, NULL, NULL, lprc, NULL, NULL);
6295 break;
6297 case LVIR_LABEL:
6298 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, NULL, NULL, NULL, NULL, lprc);
6299 break;
6301 case LVIR_BOUNDS:
6302 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, lprc, NULL, NULL, NULL, NULL);
6303 break;
6305 case LVIR_SELECTBOUNDS:
6306 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, NULL, lprc, NULL, NULL, NULL);
6307 break;
6309 default:
6310 WARN("Unknown value: %d\n", lprc->left);
6311 return FALSE;
6314 if (infoPtr->uView == LV_VIEW_DETAILS)
6315 OffsetRect(lprc, Origin.x, Position.y + Origin.y);
6316 else
6317 OffsetRect(lprc, Position.x + Origin.x, Position.y + Origin.y);
6319 TRACE(" rect=%s\n", wine_dbgstr_rect(lprc));
6321 return TRUE;
6324 /***
6325 * DESCRIPTION:
6326 * Retrieves the spacing between listview control items.
6328 * PARAMETER(S):
6329 * [I] infoPtr : valid pointer to the listview structure
6330 * [IO] lprc : rectangle to receive the output
6331 * on input, lprc->top = nSubItem
6332 * lprc->left = LVIR_ICON | LVIR_BOUNDS | LVIR_LABEL
6334 * NOTE: for subItem = 0, we should return the bounds of the _entire_ item,
6335 * not only those of the first column.
6336 * Fortunately, LISTVIEW_GetItemMetrics does the right thing.
6338 * RETURN:
6339 * TRUE: success
6340 * FALSE: failure
6342 static BOOL LISTVIEW_GetSubItemRect(const LISTVIEW_INFO *infoPtr, INT nItem, LPRECT lprc)
6344 POINT Position;
6345 LVITEMW lvItem;
6346 INT nColumn;
6348 if (!lprc) return FALSE;
6350 nColumn = lprc->top;
6352 TRACE("(nItem=%d, nSubItem=%d)\n", nItem, lprc->top);
6353 /* On WinNT, a subitem of '0' calls LISTVIEW_GetItemRect */
6354 if (lprc->top == 0)
6355 return LISTVIEW_GetItemRect(infoPtr, nItem, lprc);
6357 if (infoPtr->uView != LV_VIEW_DETAILS) return FALSE;
6359 /* special case for header items */
6360 if (nItem == -1)
6362 if (lprc->left != LVIR_BOUNDS)
6364 FIXME("Only LVIR_BOUNDS is implemented for header, got %d\n", lprc->left);
6365 return FALSE;
6368 if (infoPtr->hwndHeader)
6369 return SendMessageW(infoPtr->hwndHeader, HDM_GETITEMRECT, lprc->top, (LPARAM)lprc);
6370 else
6372 memset(lprc, 0, sizeof(RECT));
6373 return TRUE;
6377 if (!LISTVIEW_GetItemPosition(infoPtr, nItem, &Position)) return FALSE;
6379 if (nColumn < 0 || nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return FALSE;
6381 lvItem.mask = 0;
6382 lvItem.iItem = nItem;
6383 lvItem.iSubItem = nColumn;
6385 if (lvItem.mask && !LISTVIEW_GetItemW(infoPtr, &lvItem)) return FALSE;
6386 switch(lprc->left)
6388 case LVIR_ICON:
6389 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, NULL, NULL, lprc, NULL, NULL);
6390 break;
6392 case LVIR_LABEL:
6393 case LVIR_BOUNDS:
6394 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, lprc, NULL, NULL, NULL, NULL);
6395 break;
6397 default:
6398 ERR("Unknown bounds=%d\n", lprc->left);
6399 return FALSE;
6402 OffsetRect(lprc, 0, Position.y);
6403 return TRUE;
6407 /***
6408 * DESCRIPTION:
6409 * Retrieves the width of a label.
6411 * PARAMETER(S):
6412 * [I] infoPtr : valid pointer to the listview structure
6414 * RETURN:
6415 * SUCCESS : string width (in pixels)
6416 * FAILURE : zero
6418 static INT LISTVIEW_GetLabelWidth(const LISTVIEW_INFO *infoPtr, INT nItem)
6420 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
6421 LVITEMW lvItem;
6423 TRACE("(nItem=%d)\n", nItem);
6425 lvItem.mask = LVIF_TEXT;
6426 lvItem.iItem = nItem;
6427 lvItem.iSubItem = 0;
6428 lvItem.pszText = szDispText;
6429 lvItem.cchTextMax = DISP_TEXT_SIZE;
6430 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) return 0;
6432 return LISTVIEW_GetStringWidthT(infoPtr, lvItem.pszText, TRUE);
6435 /***
6436 * DESCRIPTION:
6437 * Retrieves the spacing between listview control items.
6439 * PARAMETER(S):
6440 * [I] infoPtr : valid pointer to the listview structure
6441 * [I] bSmall : flag for small or large icon
6443 * RETURN:
6444 * Horizontal + vertical spacing
6446 static LONG LISTVIEW_GetItemSpacing(const LISTVIEW_INFO *infoPtr, BOOL bSmall)
6448 LONG lResult;
6450 if (!bSmall)
6452 lResult = MAKELONG(infoPtr->iconSpacing.cx, infoPtr->iconSpacing.cy);
6454 else
6456 if (infoPtr->uView == LV_VIEW_ICON)
6457 lResult = MAKELONG(DEFAULT_COLUMN_WIDTH, GetSystemMetrics(SM_CXSMICON)+HEIGHT_PADDING);
6458 else
6459 lResult = MAKELONG(infoPtr->nItemWidth, infoPtr->nItemHeight);
6461 return lResult;
6464 /***
6465 * DESCRIPTION:
6466 * Retrieves the state of a listview control item.
6468 * PARAMETER(S):
6469 * [I] infoPtr : valid pointer to the listview structure
6470 * [I] nItem : item index
6471 * [I] uMask : state mask
6473 * RETURN:
6474 * State specified by the mask.
6476 static UINT LISTVIEW_GetItemState(const LISTVIEW_INFO *infoPtr, INT nItem, UINT uMask)
6478 LVITEMW lvItem;
6480 if (nItem < 0 || nItem >= infoPtr->nItemCount) return 0;
6482 lvItem.iItem = nItem;
6483 lvItem.iSubItem = 0;
6484 lvItem.mask = LVIF_STATE;
6485 lvItem.stateMask = uMask;
6486 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) return 0;
6488 return lvItem.state & uMask;
6491 /***
6492 * DESCRIPTION:
6493 * Retrieves the text of a listview control item or subitem.
6495 * PARAMETER(S):
6496 * [I] hwnd : window handle
6497 * [I] nItem : item index
6498 * [IO] lpLVItem : item information
6499 * [I] isW : TRUE if lpLVItem is Unicode
6501 * RETURN:
6502 * SUCCESS : string length
6503 * FAILURE : 0
6505 static INT LISTVIEW_GetItemTextT(const LISTVIEW_INFO *infoPtr, INT nItem, LPLVITEMW lpLVItem, BOOL isW)
6507 if (!lpLVItem || nItem < 0 || nItem >= infoPtr->nItemCount) return 0;
6509 lpLVItem->mask = LVIF_TEXT;
6510 lpLVItem->iItem = nItem;
6511 if (!LISTVIEW_GetItemExtT(infoPtr, lpLVItem, isW)) return 0;
6513 return textlenT(lpLVItem->pszText, isW);
6516 /***
6517 * DESCRIPTION:
6518 * Searches for an item based on properties + relationships.
6520 * PARAMETER(S):
6521 * [I] infoPtr : valid pointer to the listview structure
6522 * [I] nItem : item index
6523 * [I] uFlags : relationship flag
6525 * RETURN:
6526 * SUCCESS : item index
6527 * FAILURE : -1
6529 static INT LISTVIEW_GetNextItem(const LISTVIEW_INFO *infoPtr, INT nItem, UINT uFlags)
6531 UINT uMask = 0;
6532 LVFINDINFOW lvFindInfo;
6533 INT nCountPerColumn;
6534 INT nCountPerRow;
6535 INT i;
6537 TRACE("nItem=%d, uFlags=%x, nItemCount=%d\n", nItem, uFlags, infoPtr->nItemCount);
6538 if (nItem < -1 || nItem >= infoPtr->nItemCount) return -1;
6540 ZeroMemory(&lvFindInfo, sizeof(lvFindInfo));
6542 if (uFlags & LVNI_CUT)
6543 uMask |= LVIS_CUT;
6545 if (uFlags & LVNI_DROPHILITED)
6546 uMask |= LVIS_DROPHILITED;
6548 if (uFlags & LVNI_FOCUSED)
6549 uMask |= LVIS_FOCUSED;
6551 if (uFlags & LVNI_SELECTED)
6552 uMask |= LVIS_SELECTED;
6554 /* if we're asked for the focused item, that's only one,
6555 * so it's worth optimizing */
6556 if (uFlags & LVNI_FOCUSED)
6558 if ((LISTVIEW_GetItemState(infoPtr, infoPtr->nFocusedItem, uMask) & uMask) != uMask) return -1;
6559 return (infoPtr->nFocusedItem == nItem) ? -1 : infoPtr->nFocusedItem;
6562 if (uFlags & LVNI_ABOVE)
6564 if ((infoPtr->uView == LV_VIEW_LIST) || (infoPtr->uView == LV_VIEW_DETAILS))
6566 while (nItem >= 0)
6568 nItem--;
6569 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
6570 return nItem;
6573 else
6575 /* Special case for autoarrange - move 'til the top of a list */
6576 if (is_autoarrange(infoPtr))
6578 nCountPerRow = LISTVIEW_GetCountPerRow(infoPtr);
6579 while (nItem - nCountPerRow >= 0)
6581 nItem -= nCountPerRow;
6582 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
6583 return nItem;
6585 return -1;
6587 lvFindInfo.flags = LVFI_NEARESTXY;
6588 lvFindInfo.vkDirection = VK_UP;
6589 LISTVIEW_GetItemPosition(infoPtr, nItem, &lvFindInfo.pt);
6590 while ((nItem = LISTVIEW_FindItemW(infoPtr, nItem, &lvFindInfo)) != -1)
6592 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
6593 return nItem;
6597 else if (uFlags & LVNI_BELOW)
6599 if ((infoPtr->uView == LV_VIEW_LIST) || (infoPtr->uView == LV_VIEW_DETAILS))
6601 while (nItem < infoPtr->nItemCount)
6603 nItem++;
6604 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
6605 return nItem;
6608 else
6610 /* Special case for autoarrange - move 'til the bottom of a list */
6611 if (is_autoarrange(infoPtr))
6613 nCountPerRow = LISTVIEW_GetCountPerRow(infoPtr);
6614 while (nItem + nCountPerRow < infoPtr->nItemCount )
6616 nItem += nCountPerRow;
6617 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
6618 return nItem;
6620 return -1;
6622 lvFindInfo.flags = LVFI_NEARESTXY;
6623 lvFindInfo.vkDirection = VK_DOWN;
6624 LISTVIEW_GetItemPosition(infoPtr, nItem, &lvFindInfo.pt);
6625 while ((nItem = LISTVIEW_FindItemW(infoPtr, nItem, &lvFindInfo)) != -1)
6627 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
6628 return nItem;
6632 else if (uFlags & LVNI_TOLEFT)
6634 if (infoPtr->uView == LV_VIEW_LIST)
6636 nCountPerColumn = LISTVIEW_GetCountPerColumn(infoPtr);
6637 while (nItem - nCountPerColumn >= 0)
6639 nItem -= nCountPerColumn;
6640 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
6641 return nItem;
6644 else if ((infoPtr->uView == LV_VIEW_SMALLICON) || (infoPtr->uView == LV_VIEW_ICON))
6646 /* Special case for autoarrange - move 'til the beginning of a row */
6647 if (is_autoarrange(infoPtr))
6649 nCountPerRow = LISTVIEW_GetCountPerRow(infoPtr);
6650 while (nItem % nCountPerRow > 0)
6652 nItem --;
6653 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
6654 return nItem;
6656 return -1;
6658 lvFindInfo.flags = LVFI_NEARESTXY;
6659 lvFindInfo.vkDirection = VK_LEFT;
6660 LISTVIEW_GetItemPosition(infoPtr, nItem, &lvFindInfo.pt);
6661 while ((nItem = LISTVIEW_FindItemW(infoPtr, nItem, &lvFindInfo)) != -1)
6663 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
6664 return nItem;
6668 else if (uFlags & LVNI_TORIGHT)
6670 if (infoPtr->uView == LV_VIEW_LIST)
6672 nCountPerColumn = LISTVIEW_GetCountPerColumn(infoPtr);
6673 while (nItem + nCountPerColumn < infoPtr->nItemCount)
6675 nItem += nCountPerColumn;
6676 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
6677 return nItem;
6680 else if ((infoPtr->uView == LV_VIEW_SMALLICON) || (infoPtr->uView == LV_VIEW_ICON))
6682 /* Special case for autoarrange - move 'til the end of a row */
6683 if (is_autoarrange(infoPtr))
6685 nCountPerRow = LISTVIEW_GetCountPerRow(infoPtr);
6686 while (nItem % nCountPerRow < nCountPerRow - 1 )
6688 nItem ++;
6689 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
6690 return nItem;
6692 return -1;
6694 lvFindInfo.flags = LVFI_NEARESTXY;
6695 lvFindInfo.vkDirection = VK_RIGHT;
6696 LISTVIEW_GetItemPosition(infoPtr, nItem, &lvFindInfo.pt);
6697 while ((nItem = LISTVIEW_FindItemW(infoPtr, nItem, &lvFindInfo)) != -1)
6699 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
6700 return nItem;
6704 else
6706 nItem++;
6708 /* search by index */
6709 for (i = nItem; i < infoPtr->nItemCount; i++)
6711 if ((LISTVIEW_GetItemState(infoPtr, i, uMask) & uMask) == uMask)
6712 return i;
6716 return -1;
6719 /* LISTVIEW_GetNumberOfWorkAreas */
6721 /***
6722 * DESCRIPTION:
6723 * Retrieves the origin coordinates when in icon or small icon display mode.
6725 * PARAMETER(S):
6726 * [I] infoPtr : valid pointer to the listview structure
6727 * [O] lpptOrigin : coordinate information
6729 * RETURN:
6730 * None.
6732 static void LISTVIEW_GetOrigin(const LISTVIEW_INFO *infoPtr, LPPOINT lpptOrigin)
6734 INT nHorzPos = 0, nVertPos = 0;
6735 SCROLLINFO scrollInfo;
6737 scrollInfo.cbSize = sizeof(SCROLLINFO);
6738 scrollInfo.fMask = SIF_POS;
6740 if (GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo))
6741 nHorzPos = scrollInfo.nPos;
6742 if (GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo))
6743 nVertPos = scrollInfo.nPos;
6745 TRACE("nHorzPos=%d, nVertPos=%d\n", nHorzPos, nVertPos);
6747 lpptOrigin->x = infoPtr->rcList.left;
6748 lpptOrigin->y = infoPtr->rcList.top;
6749 if (infoPtr->uView == LV_VIEW_LIST)
6750 nHorzPos *= infoPtr->nItemWidth;
6751 else if (infoPtr->uView == LV_VIEW_DETAILS)
6752 nVertPos *= infoPtr->nItemHeight;
6754 lpptOrigin->x -= nHorzPos;
6755 lpptOrigin->y -= nVertPos;
6757 TRACE(" origin=%s\n", wine_dbgstr_point(lpptOrigin));
6760 /***
6761 * DESCRIPTION:
6762 * Retrieves the width of a string.
6764 * PARAMETER(S):
6765 * [I] hwnd : window handle
6766 * [I] lpszText : text string to process
6767 * [I] isW : TRUE if lpszText is Unicode, FALSE otherwise
6769 * RETURN:
6770 * SUCCESS : string width (in pixels)
6771 * FAILURE : zero
6773 static INT LISTVIEW_GetStringWidthT(const LISTVIEW_INFO *infoPtr, LPCWSTR lpszText, BOOL isW)
6775 SIZE stringSize;
6777 stringSize.cx = 0;
6778 if (is_textT(lpszText, isW))
6780 HFONT hFont = infoPtr->hFont ? infoPtr->hFont : infoPtr->hDefaultFont;
6781 HDC hdc = GetDC(infoPtr->hwndSelf);
6782 HFONT hOldFont = SelectObject(hdc, hFont);
6784 if (isW)
6785 GetTextExtentPointW(hdc, lpszText, lstrlenW(lpszText), &stringSize);
6786 else
6787 GetTextExtentPointA(hdc, (LPCSTR)lpszText, lstrlenA((LPCSTR)lpszText), &stringSize);
6788 SelectObject(hdc, hOldFont);
6789 ReleaseDC(infoPtr->hwndSelf, hdc);
6791 return stringSize.cx;
6794 /***
6795 * DESCRIPTION:
6796 * Determines which listview item is located at the specified position.
6798 * PARAMETER(S):
6799 * [I] infoPtr : valid pointer to the listview structure
6800 * [IO] lpht : hit test information
6801 * [I] subitem : fill out iSubItem.
6802 * [I] select : return the index only if the hit selects the item
6804 * NOTE:
6805 * (mm 20001022): We must not allow iSubItem to be touched, for
6806 * an app might pass only a structure with space up to iItem!
6807 * (MS Office 97 does that for instance in the file open dialog)
6809 * RETURN:
6810 * SUCCESS : item index
6811 * FAILURE : -1
6813 static INT LISTVIEW_HitTest(const LISTVIEW_INFO *infoPtr, LPLVHITTESTINFO lpht, BOOL subitem, BOOL select)
6815 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
6816 RECT rcBox, rcBounds, rcState, rcIcon, rcLabel, rcSearch;
6817 POINT Origin, Position, opt;
6818 LVITEMW lvItem;
6819 ITERATOR i;
6820 INT iItem;
6822 TRACE("(pt=%s, subitem=%d, select=%d)\n", wine_dbgstr_point(&lpht->pt), subitem, select);
6824 lpht->flags = 0;
6825 lpht->iItem = -1;
6826 if (subitem) lpht->iSubItem = 0;
6828 LISTVIEW_GetOrigin(infoPtr, &Origin);
6830 /* set whole list relation flags */
6831 if (subitem && infoPtr->uView == LV_VIEW_DETAILS)
6833 /* LVM_SUBITEMHITTEST checks left bound of possible client area */
6834 if (infoPtr->rcList.left > lpht->pt.x && Origin.x < lpht->pt.x)
6835 lpht->flags |= LVHT_TOLEFT;
6837 if (lpht->pt.y < infoPtr->rcList.top && lpht->pt.y >= 0)
6838 opt.y = lpht->pt.y + infoPtr->rcList.top;
6839 else
6840 opt.y = lpht->pt.y;
6842 if (infoPtr->rcList.bottom < opt.y)
6843 lpht->flags |= LVHT_BELOW;
6845 else
6847 if (infoPtr->rcList.left > lpht->pt.x)
6848 lpht->flags |= LVHT_TOLEFT;
6849 else if (infoPtr->rcList.right < lpht->pt.x)
6850 lpht->flags |= LVHT_TORIGHT;
6852 if (infoPtr->rcList.top > lpht->pt.y)
6853 lpht->flags |= LVHT_ABOVE;
6854 else if (infoPtr->rcList.bottom < lpht->pt.y)
6855 lpht->flags |= LVHT_BELOW;
6858 /* even if item is invalid try to find subitem */
6859 if (infoPtr->uView == LV_VIEW_DETAILS && subitem)
6861 RECT *pRect;
6862 INT j;
6864 opt.x = lpht->pt.x - Origin.x;
6866 lpht->iSubItem = -1;
6867 for (j = 0; j < DPA_GetPtrCount(infoPtr->hdpaColumns); j++)
6869 pRect = &LISTVIEW_GetColumnInfo(infoPtr, j)->rcHeader;
6871 if ((opt.x >= pRect->left) && (opt.x < pRect->right))
6873 lpht->iSubItem = j;
6874 break;
6877 TRACE("lpht->iSubItem=%d\n", lpht->iSubItem);
6880 TRACE("lpht->flags=0x%x\n", lpht->flags);
6881 if (lpht->flags) return -1;
6883 lpht->flags |= LVHT_NOWHERE;
6885 /* first deal with the large items */
6886 rcSearch.left = lpht->pt.x;
6887 rcSearch.top = lpht->pt.y;
6888 rcSearch.right = rcSearch.left + 1;
6889 rcSearch.bottom = rcSearch.top + 1;
6891 iterator_frameditems(&i, infoPtr, &rcSearch);
6892 iterator_next(&i); /* go to first item in the sequence */
6893 iItem = i.nItem;
6894 iterator_destroy(&i);
6896 TRACE("lpht->iItem=%d\n", iItem);
6897 if (iItem == -1) return -1;
6899 lvItem.mask = LVIF_STATE | LVIF_TEXT;
6900 if (infoPtr->uView == LV_VIEW_DETAILS) lvItem.mask |= LVIF_INDENT;
6901 lvItem.stateMask = LVIS_STATEIMAGEMASK;
6902 if (infoPtr->uView == LV_VIEW_ICON) lvItem.stateMask |= LVIS_FOCUSED;
6903 lvItem.iItem = iItem;
6904 lvItem.iSubItem = subitem ? lpht->iSubItem : 0;
6905 lvItem.pszText = szDispText;
6906 lvItem.cchTextMax = DISP_TEXT_SIZE;
6907 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) return -1;
6908 if (!infoPtr->bFocus) lvItem.state &= ~LVIS_FOCUSED;
6910 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, &rcBox, NULL, &rcIcon, &rcState, &rcLabel);
6911 LISTVIEW_GetItemOrigin(infoPtr, iItem, &Position);
6912 opt.x = lpht->pt.x - Position.x - Origin.x;
6914 if (lpht->pt.y < infoPtr->rcList.top && lpht->pt.y >= 0)
6915 opt.y = lpht->pt.y - Position.y - Origin.y + infoPtr->rcList.top;
6916 else
6917 opt.y = lpht->pt.y - Position.y - Origin.y;
6919 if (infoPtr->uView == LV_VIEW_DETAILS)
6921 rcBounds = rcBox;
6922 if (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT)
6923 opt.x = lpht->pt.x - Origin.x;
6925 else
6927 UnionRect(&rcBounds, &rcIcon, &rcLabel);
6928 UnionRect(&rcBounds, &rcBounds, &rcState);
6930 TRACE("rcBounds=%s\n", wine_dbgstr_rect(&rcBounds));
6931 if (!PtInRect(&rcBounds, opt)) return -1;
6933 if (PtInRect(&rcIcon, opt))
6934 lpht->flags |= LVHT_ONITEMICON;
6935 else if (PtInRect(&rcLabel, opt))
6936 lpht->flags |= LVHT_ONITEMLABEL;
6937 else if (infoPtr->himlState && PtInRect(&rcState, opt))
6938 lpht->flags |= LVHT_ONITEMSTATEICON;
6939 /* special case for LVS_EX_FULLROWSELECT */
6940 if (infoPtr->uView == LV_VIEW_DETAILS && infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT &&
6941 !(lpht->flags & LVHT_ONITEM))
6943 lpht->flags = LVHT_ONITEM | LVHT_ABOVE;
6945 if (lpht->flags & LVHT_ONITEM)
6946 lpht->flags &= ~LVHT_NOWHERE;
6947 TRACE("lpht->flags=0x%x\n", lpht->flags);
6949 if (select && !(infoPtr->uView == LV_VIEW_DETAILS &&
6950 ((infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT) ||
6951 (infoPtr->dwStyle & LVS_OWNERDRAWFIXED))))
6953 if (infoPtr->uView == LV_VIEW_DETAILS)
6955 /* get main item bounds */
6956 lvItem.iSubItem = 0;
6957 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, &rcBox, NULL, &rcIcon, &rcState, &rcLabel);
6958 UnionRect(&rcBounds, &rcIcon, &rcLabel);
6959 UnionRect(&rcBounds, &rcBounds, &rcState);
6961 if (!PtInRect(&rcBounds, opt)) iItem = -1;
6963 return lpht->iItem = iItem;
6966 /***
6967 * DESCRIPTION:
6968 * Inserts a new item in the listview control.
6970 * PARAMETER(S):
6971 * [I] infoPtr : valid pointer to the listview structure
6972 * [I] lpLVItem : item information
6973 * [I] isW : TRUE if lpLVItem is Unicode, FALSE if it's ANSI
6975 * RETURN:
6976 * SUCCESS : new item index
6977 * FAILURE : -1
6979 static INT LISTVIEW_InsertItemT(LISTVIEW_INFO *infoPtr, const LVITEMW *lpLVItem, BOOL isW)
6981 INT nItem;
6982 HDPA hdpaSubItems;
6983 NMLISTVIEW nmlv;
6984 ITEM_INFO *lpItem;
6985 ITEM_ID *lpID;
6986 BOOL is_sorted, has_changed;
6987 LVITEMW item;
6988 HWND hwndSelf = infoPtr->hwndSelf;
6990 TRACE("(lpLVItem=%s, isW=%d)\n", debuglvitem_t(lpLVItem, isW), isW);
6992 if (infoPtr->dwStyle & LVS_OWNERDATA) return infoPtr->nItemCount++;
6994 /* make sure it's an item, and not a subitem; cannot insert a subitem */
6995 if (!lpLVItem || lpLVItem->iSubItem) return -1;
6997 if (!is_assignable_item(lpLVItem, infoPtr->dwStyle)) return -1;
6999 if (!(lpItem = Alloc(sizeof(ITEM_INFO)))) return -1;
7001 /* insert item in listview control data structure */
7002 if ( !(hdpaSubItems = DPA_Create(8)) ) goto fail;
7003 if ( !DPA_SetPtr(hdpaSubItems, 0, lpItem) ) assert (FALSE);
7005 /* link with id struct */
7006 if (!(lpID = Alloc(sizeof(ITEM_ID)))) goto fail;
7007 lpItem->id = lpID;
7008 lpID->item = hdpaSubItems;
7009 lpID->id = get_next_itemid(infoPtr);
7010 if ( DPA_InsertPtr(infoPtr->hdpaItemIds, infoPtr->nItemCount, lpID) == -1) goto fail;
7012 is_sorted = (infoPtr->dwStyle & (LVS_SORTASCENDING | LVS_SORTDESCENDING)) &&
7013 !(infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && (LPSTR_TEXTCALLBACKW != lpLVItem->pszText);
7015 if (lpLVItem->iItem < 0 && !is_sorted) return -1;
7017 /* calculate new item index */
7018 if (is_sorted)
7020 HDPA hItem;
7021 ITEM_INFO *item_s;
7022 INT i = 0, cmpv;
7024 while (i < infoPtr->nItemCount)
7026 hItem = DPA_GetPtr( infoPtr->hdpaItems, i);
7027 item_s = (ITEM_INFO*)DPA_GetPtr(hItem, 0);
7029 cmpv = textcmpWT(item_s->hdr.pszText, lpLVItem->pszText, TRUE);
7030 if (infoPtr->dwStyle & LVS_SORTDESCENDING) cmpv *= -1;
7032 if (cmpv >= 0) break;
7033 i++;
7035 nItem = i;
7037 else
7038 nItem = min(lpLVItem->iItem, infoPtr->nItemCount);
7040 TRACE(" inserting at %d, sorted=%d, count=%d, iItem=%d\n", nItem, is_sorted, infoPtr->nItemCount, lpLVItem->iItem);
7041 nItem = DPA_InsertPtr( infoPtr->hdpaItems, nItem, hdpaSubItems );
7042 if (nItem == -1) goto fail;
7043 infoPtr->nItemCount++;
7045 /* shift indices first so they don't get tangled */
7046 LISTVIEW_ShiftIndices(infoPtr, nItem, 1);
7048 /* set the item attributes */
7049 if (lpLVItem->mask & (LVIF_GROUPID|LVIF_COLUMNS))
7051 /* full size structure expected - _WIN32IE >= 0x560 */
7052 item = *lpLVItem;
7054 else if (lpLVItem->mask & LVIF_INDENT)
7056 /* indent member expected - _WIN32IE >= 0x300 */
7057 memcpy(&item, lpLVItem, offsetof( LVITEMW, iGroupId ));
7059 else
7061 /* minimal structure expected */
7062 memcpy(&item, lpLVItem, offsetof( LVITEMW, iIndent ));
7064 item.iItem = nItem;
7065 if (infoPtr->dwLvExStyle & LVS_EX_CHECKBOXES)
7067 item.mask |= LVIF_STATE;
7068 item.stateMask |= LVIS_STATEIMAGEMASK;
7069 item.state &= ~LVIS_STATEIMAGEMASK;
7070 item.state |= INDEXTOSTATEIMAGEMASK(1);
7072 if (!set_main_item(infoPtr, &item, TRUE, isW, &has_changed)) goto undo;
7074 /* make room for the position, if we are in the right mode */
7075 if ((infoPtr->uView == LV_VIEW_SMALLICON) || (infoPtr->uView == LV_VIEW_ICON))
7077 if (DPA_InsertPtr(infoPtr->hdpaPosX, nItem, 0) == -1)
7078 goto undo;
7079 if (DPA_InsertPtr(infoPtr->hdpaPosY, nItem, 0) == -1)
7081 DPA_DeletePtr(infoPtr->hdpaPosX, nItem);
7082 goto undo;
7086 /* send LVN_INSERTITEM notification */
7087 ZeroMemory(&nmlv, sizeof(NMLISTVIEW));
7088 nmlv.iItem = nItem;
7089 nmlv.lParam = lpItem->lParam;
7090 notify_listview(infoPtr, LVN_INSERTITEM, &nmlv);
7091 if (!IsWindow(hwndSelf))
7092 return -1;
7094 /* align items (set position of each item) */
7095 if (infoPtr->uView == LV_VIEW_SMALLICON || infoPtr->uView == LV_VIEW_ICON)
7097 POINT pt;
7099 if (infoPtr->dwStyle & LVS_ALIGNLEFT)
7100 LISTVIEW_NextIconPosLeft(infoPtr, &pt);
7101 else
7102 LISTVIEW_NextIconPosTop(infoPtr, &pt);
7104 LISTVIEW_MoveIconTo(infoPtr, nItem, &pt, TRUE);
7107 /* now is the invalidation fun */
7108 LISTVIEW_ScrollOnInsert(infoPtr, nItem, 1);
7109 return nItem;
7111 undo:
7112 LISTVIEW_ShiftIndices(infoPtr, nItem, -1);
7113 DPA_DeletePtr(infoPtr->hdpaItems, nItem);
7114 infoPtr->nItemCount--;
7115 fail:
7116 DPA_DeletePtr(hdpaSubItems, 0);
7117 DPA_Destroy (hdpaSubItems);
7118 Free (lpItem);
7119 return -1;
7122 /***
7123 * DESCRIPTION:
7124 * Checks item visibility.
7126 * PARAMETER(S):
7127 * [I] infoPtr : valid pointer to the listview structure
7128 * [I] nFirst : item index to check for
7130 * RETURN:
7131 * Item visible : TRUE
7132 * Item invisible or failure : FALSE
7134 static BOOL LISTVIEW_IsItemVisible(const LISTVIEW_INFO *infoPtr, INT nItem)
7136 POINT Origin, Position;
7137 RECT rcItem;
7138 HDC hdc;
7139 BOOL ret;
7141 TRACE("nItem=%d\n", nItem);
7143 if (nItem < 0 || nItem >= DPA_GetPtrCount(infoPtr->hdpaItems)) return FALSE;
7145 LISTVIEW_GetOrigin(infoPtr, &Origin);
7146 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position);
7147 rcItem.left = Position.x + Origin.x;
7148 rcItem.top = Position.y + Origin.y;
7149 rcItem.right = rcItem.left + infoPtr->nItemWidth;
7150 rcItem.bottom = rcItem.top + infoPtr->nItemHeight;
7152 hdc = GetDC(infoPtr->hwndSelf);
7153 if (!hdc) return FALSE;
7154 ret = RectVisible(hdc, &rcItem);
7155 ReleaseDC(infoPtr->hwndSelf, hdc);
7157 return ret;
7160 /***
7161 * DESCRIPTION:
7162 * Redraws a range of items.
7164 * PARAMETER(S):
7165 * [I] infoPtr : valid pointer to the listview structure
7166 * [I] nFirst : first item
7167 * [I] nLast : last item
7169 * RETURN:
7170 * SUCCESS : TRUE
7171 * FAILURE : FALSE
7173 static BOOL LISTVIEW_RedrawItems(const LISTVIEW_INFO *infoPtr, INT nFirst, INT nLast)
7175 INT i;
7177 if (nLast < nFirst || min(nFirst, nLast) < 0 ||
7178 max(nFirst, nLast) >= infoPtr->nItemCount)
7179 return FALSE;
7181 for (i = nFirst; i <= nLast; i++)
7182 LISTVIEW_InvalidateItem(infoPtr, i);
7184 return TRUE;
7187 /***
7188 * DESCRIPTION:
7189 * Scroll the content of a listview.
7191 * PARAMETER(S):
7192 * [I] infoPtr : valid pointer to the listview structure
7193 * [I] dx : horizontal scroll amount in pixels
7194 * [I] dy : vertical scroll amount in pixels
7196 * RETURN:
7197 * SUCCESS : TRUE
7198 * FAILURE : FALSE
7200 * COMMENTS:
7201 * If the control is in report view (LV_VIEW_DETAILS) the control can
7202 * be scrolled only in line increments. "dy" will be rounded to the
7203 * nearest number of pixels that are a whole line. Ex: if line height
7204 * is 16 and an 8 is passed, the list will be scrolled by 16. If a 7
7205 * is passed, then the scroll will be 0. (per MSDN 7/2002)
7207 * For: (per experimentation with native control and CSpy ListView)
7208 * LV_VIEW_ICON dy=1 = 1 pixel (vertical only)
7209 * dx ignored
7210 * LV_VIEW_SMALLICON dy=1 = 1 pixel (vertical only)
7211 * dx ignored
7212 * LV_VIEW_LIST dx=1 = 1 column (horizontal only)
7213 * but will only scroll 1 column per message
7214 * no matter what the value.
7215 * dy must be 0 or FALSE returned.
7216 * LV_VIEW_DETAILS dx=1 = 1 pixel
7217 * dy= see above
7220 static BOOL LISTVIEW_Scroll(LISTVIEW_INFO *infoPtr, INT dx, INT dy)
7222 switch(infoPtr->uView) {
7223 case LV_VIEW_DETAILS:
7224 dy += (dy < 0 ? -1 : 1) * infoPtr->nItemHeight/2;
7225 dy /= infoPtr->nItemHeight;
7226 break;
7227 case LV_VIEW_LIST:
7228 if (dy != 0) return FALSE;
7229 break;
7230 default: /* icon */
7231 dx = 0;
7232 break;
7235 if (dx != 0) LISTVIEW_HScroll(infoPtr, SB_INTERNAL, dx, 0);
7236 if (dy != 0) LISTVIEW_VScroll(infoPtr, SB_INTERNAL, dy, 0);
7238 return TRUE;
7241 /***
7242 * DESCRIPTION:
7243 * Sets the background color.
7245 * PARAMETER(S):
7246 * [I] infoPtr : valid pointer to the listview structure
7247 * [I] clrBk : background color
7249 * RETURN:
7250 * SUCCESS : TRUE
7251 * FAILURE : FALSE
7253 static BOOL LISTVIEW_SetBkColor(LISTVIEW_INFO *infoPtr, COLORREF clrBk)
7255 TRACE("(clrBk=%x)\n", clrBk);
7257 if(infoPtr->clrBk != clrBk) {
7258 if (infoPtr->clrBk != CLR_NONE) DeleteObject(infoPtr->hBkBrush);
7259 infoPtr->clrBk = clrBk;
7260 if (clrBk == CLR_NONE)
7261 infoPtr->hBkBrush = (HBRUSH)GetClassLongPtrW(infoPtr->hwndSelf, GCLP_HBRBACKGROUND);
7262 else
7263 infoPtr->hBkBrush = CreateSolidBrush(clrBk);
7264 LISTVIEW_InvalidateList(infoPtr);
7267 return TRUE;
7270 /* LISTVIEW_SetBkImage */
7272 /*** Helper for {Insert,Set}ColumnT *only* */
7273 static void column_fill_hditem(const LISTVIEW_INFO *infoPtr, HDITEMW *lphdi, INT nColumn,
7274 const LVCOLUMNW *lpColumn, BOOL isW)
7276 if (lpColumn->mask & LVCF_FMT)
7278 /* format member is valid */
7279 lphdi->mask |= HDI_FORMAT;
7281 /* set text alignment (leftmost column must be left-aligned) */
7282 if (nColumn == 0 || (lpColumn->fmt & LVCFMT_JUSTIFYMASK) == LVCFMT_LEFT)
7283 lphdi->fmt |= HDF_LEFT;
7284 else if ((lpColumn->fmt & LVCFMT_JUSTIFYMASK) == LVCFMT_RIGHT)
7285 lphdi->fmt |= HDF_RIGHT;
7286 else if ((lpColumn->fmt & LVCFMT_JUSTIFYMASK) == LVCFMT_CENTER)
7287 lphdi->fmt |= HDF_CENTER;
7289 if (lpColumn->fmt & LVCFMT_BITMAP_ON_RIGHT)
7290 lphdi->fmt |= HDF_BITMAP_ON_RIGHT;
7292 if (lpColumn->fmt & LVCFMT_COL_HAS_IMAGES)
7294 lphdi->fmt |= HDF_IMAGE;
7295 lphdi->iImage = I_IMAGECALLBACK;
7299 if (lpColumn->mask & LVCF_WIDTH)
7301 lphdi->mask |= HDI_WIDTH;
7302 if(lpColumn->cx == LVSCW_AUTOSIZE_USEHEADER)
7304 /* make it fill the remainder of the controls width */
7305 RECT rcHeader;
7306 INT item_index;
7308 for(item_index = 0; item_index < (nColumn - 1); item_index++)
7310 LISTVIEW_GetHeaderRect(infoPtr, item_index, &rcHeader);
7311 lphdi->cxy += rcHeader.right - rcHeader.left;
7314 /* retrieve the layout of the header */
7315 GetClientRect(infoPtr->hwndSelf, &rcHeader);
7316 TRACE("start cxy=%d rcHeader=%s\n", lphdi->cxy, wine_dbgstr_rect(&rcHeader));
7318 lphdi->cxy = (rcHeader.right - rcHeader.left) - lphdi->cxy;
7320 else
7321 lphdi->cxy = lpColumn->cx;
7324 if (lpColumn->mask & LVCF_TEXT)
7326 lphdi->mask |= HDI_TEXT | HDI_FORMAT;
7327 lphdi->fmt |= HDF_STRING;
7328 lphdi->pszText = lpColumn->pszText;
7329 lphdi->cchTextMax = textlenT(lpColumn->pszText, isW);
7332 if (lpColumn->mask & LVCF_IMAGE)
7334 lphdi->mask |= HDI_IMAGE;
7335 lphdi->iImage = lpColumn->iImage;
7338 if (lpColumn->mask & LVCF_ORDER)
7340 lphdi->mask |= HDI_ORDER;
7341 lphdi->iOrder = lpColumn->iOrder;
7346 /***
7347 * DESCRIPTION:
7348 * Inserts a new column.
7350 * PARAMETER(S):
7351 * [I] infoPtr : valid pointer to the listview structure
7352 * [I] nColumn : column index
7353 * [I] lpColumn : column information
7354 * [I] isW : TRUE if lpColumn is Unicode, FALSE otherwise
7356 * RETURN:
7357 * SUCCESS : new column index
7358 * FAILURE : -1
7360 static INT LISTVIEW_InsertColumnT(LISTVIEW_INFO *infoPtr, INT nColumn,
7361 const LVCOLUMNW *lpColumn, BOOL isW)
7363 COLUMN_INFO *lpColumnInfo;
7364 INT nNewColumn;
7365 HDITEMW hdi;
7367 TRACE("(nColumn=%d, lpColumn=%s, isW=%d)\n", nColumn, debuglvcolumn_t(lpColumn, isW), isW);
7369 if (!lpColumn || nColumn < 0) return -1;
7370 nColumn = min(nColumn, DPA_GetPtrCount(infoPtr->hdpaColumns));
7372 ZeroMemory(&hdi, sizeof(HDITEMW));
7373 column_fill_hditem(infoPtr, &hdi, nColumn, lpColumn, isW);
7376 * A mask not including LVCF_WIDTH turns into a mask of width, width 10
7377 * (can be seen in SPY) otherwise column never gets added.
7379 if (!(lpColumn->mask & LVCF_WIDTH)) {
7380 hdi.mask |= HDI_WIDTH;
7381 hdi.cxy = 10;
7385 * when the iSubItem is available Windows copies it to the header lParam. It seems
7386 * to happen only in LVM_INSERTCOLUMN - not in LVM_SETCOLUMN
7388 if (lpColumn->mask & LVCF_SUBITEM)
7390 hdi.mask |= HDI_LPARAM;
7391 hdi.lParam = lpColumn->iSubItem;
7394 /* create header if not present */
7395 LISTVIEW_CreateHeader(infoPtr);
7396 if (!(LVS_NOCOLUMNHEADER & infoPtr->dwStyle) &&
7397 (infoPtr->uView == LV_VIEW_DETAILS) && (WS_VISIBLE & infoPtr->dwStyle))
7399 ShowWindow(infoPtr->hwndHeader, SW_SHOWNORMAL);
7402 /* insert item in header control */
7403 nNewColumn = SendMessageW(infoPtr->hwndHeader,
7404 isW ? HDM_INSERTITEMW : HDM_INSERTITEMA,
7405 (WPARAM)nColumn, (LPARAM)&hdi);
7406 if (nNewColumn == -1) return -1;
7407 if (nNewColumn != nColumn) ERR("nColumn=%d, nNewColumn=%d\n", nColumn, nNewColumn);
7409 /* create our own column info */
7410 if (!(lpColumnInfo = Alloc(sizeof(COLUMN_INFO)))) goto fail;
7411 if (DPA_InsertPtr(infoPtr->hdpaColumns, nNewColumn, lpColumnInfo) == -1) goto fail;
7413 if (lpColumn->mask & LVCF_FMT) lpColumnInfo->fmt = lpColumn->fmt;
7414 if (!SendMessageW(infoPtr->hwndHeader, HDM_GETITEMRECT, nNewColumn, (LPARAM)&lpColumnInfo->rcHeader))
7415 goto fail;
7417 /* now we have to actually adjust the data */
7418 if (!(infoPtr->dwStyle & LVS_OWNERDATA) && infoPtr->nItemCount > 0)
7420 SUBITEM_INFO *lpSubItem;
7421 HDPA hdpaSubItems;
7422 INT nItem, i;
7424 for (nItem = 0; nItem < infoPtr->nItemCount; nItem++)
7426 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, nItem);
7427 for (i = 1; i < DPA_GetPtrCount(hdpaSubItems); i++)
7429 lpSubItem = DPA_GetPtr(hdpaSubItems, i);
7430 if (lpSubItem->iSubItem >= nNewColumn)
7431 lpSubItem->iSubItem++;
7436 /* make space for the new column */
7437 LISTVIEW_ScrollColumns(infoPtr, nNewColumn + 1, lpColumnInfo->rcHeader.right - lpColumnInfo->rcHeader.left);
7438 LISTVIEW_UpdateItemSize(infoPtr);
7440 return nNewColumn;
7442 fail:
7443 if (nNewColumn != -1) SendMessageW(infoPtr->hwndHeader, HDM_DELETEITEM, nNewColumn, 0);
7444 if (lpColumnInfo)
7446 DPA_DeletePtr(infoPtr->hdpaColumns, nNewColumn);
7447 Free(lpColumnInfo);
7449 return -1;
7452 /***
7453 * DESCRIPTION:
7454 * Sets the attributes of a header item.
7456 * PARAMETER(S):
7457 * [I] infoPtr : valid pointer to the listview structure
7458 * [I] nColumn : column index
7459 * [I] lpColumn : column attributes
7460 * [I] isW: if TRUE, then lpColumn is a LPLVCOLUMNW, else it is a LPLVCOLUMNA
7462 * RETURN:
7463 * SUCCESS : TRUE
7464 * FAILURE : FALSE
7466 static BOOL LISTVIEW_SetColumnT(const LISTVIEW_INFO *infoPtr, INT nColumn,
7467 const LVCOLUMNW *lpColumn, BOOL isW)
7469 HDITEMW hdi, hdiget;
7470 BOOL bResult;
7472 TRACE("(nColumn=%d, lpColumn=%s, isW=%d)\n", nColumn, debuglvcolumn_t(lpColumn, isW), isW);
7474 if (!lpColumn || nColumn < 0 || nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return FALSE;
7476 ZeroMemory(&hdi, sizeof(HDITEMW));
7477 if (lpColumn->mask & LVCF_FMT)
7479 hdi.mask |= HDI_FORMAT;
7480 hdiget.mask = HDI_FORMAT;
7481 if (SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, nColumn, (LPARAM)&hdiget))
7482 hdi.fmt = hdiget.fmt & HDF_STRING;
7484 column_fill_hditem(infoPtr, &hdi, nColumn, lpColumn, isW);
7486 /* set header item attributes */
7487 bResult = SendMessageW(infoPtr->hwndHeader, isW ? HDM_SETITEMW : HDM_SETITEMA, (WPARAM)nColumn, (LPARAM)&hdi);
7488 if (!bResult) return FALSE;
7490 if (lpColumn->mask & LVCF_FMT)
7492 COLUMN_INFO *lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, nColumn);
7493 int oldFmt = lpColumnInfo->fmt;
7495 lpColumnInfo->fmt = lpColumn->fmt;
7496 if ((oldFmt ^ lpColumn->fmt) & (LVCFMT_JUSTIFYMASK | LVCFMT_IMAGE))
7498 if (infoPtr->uView == LV_VIEW_DETAILS) LISTVIEW_InvalidateColumn(infoPtr, nColumn);
7502 return TRUE;
7505 /***
7506 * DESCRIPTION:
7507 * Sets the column order array
7509 * PARAMETERS:
7510 * [I] infoPtr : valid pointer to the listview structure
7511 * [I] iCount : number of elements in column order array
7512 * [I] lpiArray : pointer to column order array
7514 * RETURN:
7515 * SUCCESS : TRUE
7516 * FAILURE : FALSE
7518 static BOOL LISTVIEW_SetColumnOrderArray(const LISTVIEW_INFO *infoPtr, INT iCount, const INT *lpiArray)
7520 FIXME("iCount %d lpiArray %p\n", iCount, lpiArray);
7522 if (!lpiArray)
7523 return FALSE;
7525 return TRUE;
7529 /***
7530 * DESCRIPTION:
7531 * Sets the width of a column
7533 * PARAMETERS:
7534 * [I] infoPtr : valid pointer to the listview structure
7535 * [I] nColumn : column index
7536 * [I] cx : column width
7538 * RETURN:
7539 * SUCCESS : TRUE
7540 * FAILURE : FALSE
7542 static BOOL LISTVIEW_SetColumnWidth(LISTVIEW_INFO *infoPtr, INT nColumn, INT cx)
7544 WCHAR szDispText[DISP_TEXT_SIZE] = { 0 };
7545 INT max_cx = 0;
7546 HDITEMW hdi;
7548 TRACE("(nColumn=%d, cx=%d\n", nColumn, cx);
7550 /* set column width only if in report or list mode */
7551 if (infoPtr->uView != LV_VIEW_DETAILS && infoPtr->uView != LV_VIEW_LIST) return FALSE;
7553 /* take care of invalid cx values */
7554 if(infoPtr->uView == LV_VIEW_DETAILS && cx < -2) cx = LVSCW_AUTOSIZE;
7555 else if (infoPtr->uView == LV_VIEW_LIST && cx < 1) return FALSE;
7557 /* resize all columns if in LV_VIEW_LIST mode */
7558 if(infoPtr->uView == LV_VIEW_LIST)
7560 infoPtr->nItemWidth = cx;
7561 LISTVIEW_InvalidateList(infoPtr);
7562 return TRUE;
7565 if (nColumn < 0 || nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return FALSE;
7567 if (cx == LVSCW_AUTOSIZE || (cx == LVSCW_AUTOSIZE_USEHEADER && nColumn < DPA_GetPtrCount(infoPtr->hdpaColumns) -1))
7569 INT nLabelWidth;
7570 LVITEMW lvItem;
7572 lvItem.mask = LVIF_TEXT;
7573 lvItem.iItem = 0;
7574 lvItem.iSubItem = nColumn;
7575 lvItem.pszText = szDispText;
7576 lvItem.cchTextMax = DISP_TEXT_SIZE;
7577 for (; lvItem.iItem < infoPtr->nItemCount; lvItem.iItem++)
7579 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) continue;
7580 nLabelWidth = LISTVIEW_GetStringWidthT(infoPtr, lvItem.pszText, TRUE);
7581 if (max_cx < nLabelWidth) max_cx = nLabelWidth;
7583 if (infoPtr->himlSmall && (nColumn == 0 || (LISTVIEW_GetColumnInfo(infoPtr, nColumn)->fmt & LVCFMT_IMAGE)))
7584 max_cx += infoPtr->iconSize.cx;
7585 max_cx += TRAILING_LABEL_PADDING;
7588 /* autosize based on listview items width */
7589 if(cx == LVSCW_AUTOSIZE)
7590 cx = max_cx;
7591 else if(cx == LVSCW_AUTOSIZE_USEHEADER)
7593 /* if iCol is the last column make it fill the remainder of the controls width */
7594 if(nColumn == DPA_GetPtrCount(infoPtr->hdpaColumns) - 1)
7596 RECT rcHeader;
7597 POINT Origin;
7599 LISTVIEW_GetOrigin(infoPtr, &Origin);
7600 LISTVIEW_GetHeaderRect(infoPtr, nColumn, &rcHeader);
7602 cx = infoPtr->rcList.right - Origin.x - rcHeader.left;
7604 else
7606 /* Despite what the MS docs say, if this is not the last
7607 column, then MS resizes the column to the width of the
7608 largest text string in the column, including headers
7609 and items. This is different from LVSCW_AUTOSIZE in that
7610 LVSCW_AUTOSIZE ignores the header string length. */
7611 cx = 0;
7613 /* retrieve header text */
7614 hdi.mask = HDI_TEXT;
7615 hdi.cchTextMax = DISP_TEXT_SIZE;
7616 hdi.pszText = szDispText;
7617 if (SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, nColumn, (LPARAM)&hdi))
7619 HDC hdc = GetDC(infoPtr->hwndSelf);
7620 HFONT old_font = SelectObject(hdc, (HFONT)SendMessageW(infoPtr->hwndHeader, WM_GETFONT, 0, 0));
7621 SIZE size;
7623 if (GetTextExtentPoint32W(hdc, hdi.pszText, lstrlenW(hdi.pszText), &size))
7624 cx = size.cx + TRAILING_HEADER_PADDING;
7625 /* FIXME: Take into account the header image, if one is present */
7626 SelectObject(hdc, old_font);
7627 ReleaseDC(infoPtr->hwndSelf, hdc);
7629 cx = max (cx, max_cx);
7633 if (cx < 0) return FALSE;
7635 /* call header to update the column change */
7636 hdi.mask = HDI_WIDTH;
7637 hdi.cxy = cx;
7638 TRACE("hdi.cxy=%d\n", hdi.cxy);
7639 return SendMessageW(infoPtr->hwndHeader, HDM_SETITEMW, nColumn, (LPARAM)&hdi);
7642 /***
7643 * Creates the checkbox imagelist. Helper for LISTVIEW_SetExtendedListViewStyle
7646 static HIMAGELIST LISTVIEW_CreateCheckBoxIL(const LISTVIEW_INFO *infoPtr)
7648 HDC hdc_wnd, hdc;
7649 HBITMAP hbm_im, hbm_mask, hbm_orig;
7650 RECT rc;
7651 HBRUSH hbr_white = GetStockObject(WHITE_BRUSH);
7652 HBRUSH hbr_black = GetStockObject(BLACK_BRUSH);
7653 HIMAGELIST himl;
7655 himl = ImageList_Create(GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON),
7656 ILC_COLOR | ILC_MASK, 2, 2);
7657 hdc_wnd = GetDC(infoPtr->hwndSelf);
7658 hdc = CreateCompatibleDC(hdc_wnd);
7659 hbm_im = CreateCompatibleBitmap(hdc_wnd, GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON));
7660 hbm_mask = CreateBitmap(GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), 1, 1, NULL);
7661 ReleaseDC(infoPtr->hwndSelf, hdc_wnd);
7663 rc.left = rc.top = 0;
7664 rc.right = GetSystemMetrics(SM_CXSMICON);
7665 rc.bottom = GetSystemMetrics(SM_CYSMICON);
7667 hbm_orig = SelectObject(hdc, hbm_mask);
7668 FillRect(hdc, &rc, hbr_white);
7669 InflateRect(&rc, -2, -2);
7670 FillRect(hdc, &rc, hbr_black);
7672 SelectObject(hdc, hbm_im);
7673 DrawFrameControl(hdc, &rc, DFC_BUTTON, DFCS_BUTTONCHECK | DFCS_MONO);
7674 SelectObject(hdc, hbm_orig);
7675 ImageList_Add(himl, hbm_im, hbm_mask);
7677 SelectObject(hdc, hbm_im);
7678 DrawFrameControl(hdc, &rc, DFC_BUTTON, DFCS_BUTTONCHECK | DFCS_MONO | DFCS_CHECKED);
7679 SelectObject(hdc, hbm_orig);
7680 ImageList_Add(himl, hbm_im, hbm_mask);
7682 DeleteObject(hbm_mask);
7683 DeleteObject(hbm_im);
7684 DeleteDC(hdc);
7686 return himl;
7689 /***
7690 * DESCRIPTION:
7691 * Sets the extended listview style.
7693 * PARAMETERS:
7694 * [I] infoPtr : valid pointer to the listview structure
7695 * [I] dwMask : mask
7696 * [I] dwStyle : style
7698 * RETURN:
7699 * SUCCESS : previous style
7700 * FAILURE : 0
7702 static DWORD LISTVIEW_SetExtendedListViewStyle(LISTVIEW_INFO *infoPtr, DWORD dwMask, DWORD dwExStyle)
7704 DWORD dwOldExStyle = infoPtr->dwLvExStyle;
7706 /* set new style */
7707 if (dwMask)
7708 infoPtr->dwLvExStyle = (dwOldExStyle & ~dwMask) | (dwExStyle & dwMask);
7709 else
7710 infoPtr->dwLvExStyle = dwExStyle;
7712 if((infoPtr->dwLvExStyle ^ dwOldExStyle) & LVS_EX_CHECKBOXES)
7714 HIMAGELIST himl = 0;
7715 if(infoPtr->dwLvExStyle & LVS_EX_CHECKBOXES)
7717 LVITEMW item;
7718 item.mask = LVIF_STATE;
7719 item.stateMask = LVIS_STATEIMAGEMASK;
7720 item.state = INDEXTOSTATEIMAGEMASK(1);
7721 LISTVIEW_SetItemState(infoPtr, -1, &item);
7723 himl = LISTVIEW_CreateCheckBoxIL(infoPtr);
7725 LISTVIEW_SetImageList(infoPtr, LVSIL_STATE, himl);
7728 if((infoPtr->dwLvExStyle ^ dwOldExStyle) & LVS_EX_HEADERDRAGDROP)
7730 DWORD dwStyle;
7732 /* if not already created */
7733 LISTVIEW_CreateHeader(infoPtr);
7735 dwStyle = GetWindowLongW(infoPtr->hwndHeader, GWL_STYLE);
7736 if (infoPtr->dwLvExStyle & LVS_EX_HEADERDRAGDROP)
7737 dwStyle |= HDS_DRAGDROP;
7738 else
7739 dwStyle &= ~HDS_DRAGDROP;
7740 SetWindowLongW(infoPtr->hwndHeader, GWL_STYLE, dwStyle);
7743 /* GRIDLINES adds decoration at top so changes sizes */
7744 if((infoPtr->dwLvExStyle ^ dwOldExStyle) & LVS_EX_GRIDLINES)
7746 LISTVIEW_UpdateSize(infoPtr);
7750 LISTVIEW_InvalidateList(infoPtr);
7751 return dwOldExStyle;
7754 /***
7755 * DESCRIPTION:
7756 * Sets the new hot cursor used during hot tracking and hover selection.
7758 * PARAMETER(S):
7759 * [I] infoPtr : valid pointer to the listview structure
7760 * [I] hCursor : the new hot cursor handle
7762 * RETURN:
7763 * Returns the previous hot cursor
7765 static HCURSOR LISTVIEW_SetHotCursor(LISTVIEW_INFO *infoPtr, HCURSOR hCursor)
7767 HCURSOR oldCursor = infoPtr->hHotCursor;
7769 infoPtr->hHotCursor = hCursor;
7771 return oldCursor;
7775 /***
7776 * DESCRIPTION:
7777 * Sets the hot item index.
7779 * PARAMETERS:
7780 * [I] infoPtr : valid pointer to the listview structure
7781 * [I] iIndex : index
7783 * RETURN:
7784 * SUCCESS : previous hot item index
7785 * FAILURE : -1 (no hot item)
7787 static INT LISTVIEW_SetHotItem(LISTVIEW_INFO *infoPtr, INT iIndex)
7789 INT iOldIndex = infoPtr->nHotItem;
7791 infoPtr->nHotItem = iIndex;
7793 return iOldIndex;
7797 /***
7798 * DESCRIPTION:
7799 * Sets the amount of time the cursor must hover over an item before it is selected.
7801 * PARAMETER(S):
7802 * [I] infoPtr : valid pointer to the listview structure
7803 * [I] dwHoverTime : hover time, if -1 the hover time is set to the default
7805 * RETURN:
7806 * Returns the previous hover time
7808 static DWORD LISTVIEW_SetHoverTime(LISTVIEW_INFO *infoPtr, DWORD dwHoverTime)
7810 DWORD oldHoverTime = infoPtr->dwHoverTime;
7812 infoPtr->dwHoverTime = dwHoverTime;
7814 return oldHoverTime;
7817 /***
7818 * DESCRIPTION:
7819 * Sets spacing for icons of LVS_ICON style.
7821 * PARAMETER(S):
7822 * [I] infoPtr : valid pointer to the listview structure
7823 * [I] cx : horizontal spacing (-1 = system spacing, 0 = autosize)
7824 * [I] cy : vertical spacing (-1 = system spacing, 0 = autosize)
7826 * RETURN:
7827 * MAKELONG(oldcx, oldcy)
7829 static DWORD LISTVIEW_SetIconSpacing(LISTVIEW_INFO *infoPtr, INT cx, INT cy)
7831 DWORD oldspacing = MAKELONG(infoPtr->iconSpacing.cx, infoPtr->iconSpacing.cy);
7833 TRACE("requested=(%d,%d)\n", cx, cy);
7835 /* this is supported only for LVS_ICON style */
7836 if (infoPtr->uView != LV_VIEW_ICON) return oldspacing;
7838 /* set to defaults, if instructed to */
7839 if (cx == -1) cx = GetSystemMetrics(SM_CXICONSPACING);
7840 if (cy == -1) cy = GetSystemMetrics(SM_CYICONSPACING);
7842 /* if 0 then compute width
7843 * FIXME: Should scan each item and determine max width of
7844 * icon or label, then make that the width */
7845 if (cx == 0)
7846 cx = infoPtr->iconSpacing.cx;
7848 /* if 0 then compute height */
7849 if (cy == 0)
7850 cy = infoPtr->iconSize.cy + 2 * infoPtr->ntmHeight +
7851 ICON_BOTTOM_PADDING + ICON_TOP_PADDING + LABEL_VERT_PADDING;
7854 infoPtr->iconSpacing.cx = cx;
7855 infoPtr->iconSpacing.cy = cy;
7857 TRACE("old=(%d,%d), new=(%d,%d), iconSize=(%d,%d), ntmH=%d\n",
7858 LOWORD(oldspacing), HIWORD(oldspacing), cx, cy,
7859 infoPtr->iconSize.cx, infoPtr->iconSize.cy,
7860 infoPtr->ntmHeight);
7862 /* these depend on the iconSpacing */
7863 LISTVIEW_UpdateItemSize(infoPtr);
7865 return oldspacing;
7868 static inline void set_icon_size(SIZE *size, HIMAGELIST himl, BOOL small)
7870 INT cx, cy;
7872 if (himl && ImageList_GetIconSize(himl, &cx, &cy))
7874 size->cx = cx;
7875 size->cy = cy;
7877 else
7879 size->cx = GetSystemMetrics(small ? SM_CXSMICON : SM_CXICON);
7880 size->cy = GetSystemMetrics(small ? SM_CYSMICON : SM_CYICON);
7884 /***
7885 * DESCRIPTION:
7886 * Sets image lists.
7888 * PARAMETER(S):
7889 * [I] infoPtr : valid pointer to the listview structure
7890 * [I] nType : image list type
7891 * [I] himl : image list handle
7893 * RETURN:
7894 * SUCCESS : old image list
7895 * FAILURE : NULL
7897 static HIMAGELIST LISTVIEW_SetImageList(LISTVIEW_INFO *infoPtr, INT nType, HIMAGELIST himl)
7899 INT oldHeight = infoPtr->nItemHeight;
7900 HIMAGELIST himlOld = 0;
7902 TRACE("(nType=%d, himl=%p\n", nType, himl);
7904 switch (nType)
7906 case LVSIL_NORMAL:
7907 himlOld = infoPtr->himlNormal;
7908 infoPtr->himlNormal = himl;
7909 if (infoPtr->uView == LV_VIEW_ICON) set_icon_size(&infoPtr->iconSize, himl, FALSE);
7910 LISTVIEW_SetIconSpacing(infoPtr, 0, 0);
7911 break;
7913 case LVSIL_SMALL:
7914 himlOld = infoPtr->himlSmall;
7915 infoPtr->himlSmall = himl;
7916 if (infoPtr->uView != LV_VIEW_ICON) set_icon_size(&infoPtr->iconSize, himl, TRUE);
7917 break;
7919 case LVSIL_STATE:
7920 himlOld = infoPtr->himlState;
7921 infoPtr->himlState = himl;
7922 set_icon_size(&infoPtr->iconStateSize, himl, TRUE);
7923 ImageList_SetBkColor(infoPtr->himlState, CLR_NONE);
7924 break;
7926 default:
7927 ERR("Unknown icon type=%d\n", nType);
7928 return NULL;
7931 infoPtr->nItemHeight = LISTVIEW_CalculateItemHeight(infoPtr);
7932 if (infoPtr->nItemHeight != oldHeight)
7933 LISTVIEW_UpdateScroll(infoPtr);
7935 return himlOld;
7938 /***
7939 * DESCRIPTION:
7940 * Preallocates memory (does *not* set the actual count of items !)
7942 * PARAMETER(S):
7943 * [I] infoPtr : valid pointer to the listview structure
7944 * [I] nItems : item count (projected number of items to allocate)
7945 * [I] dwFlags : update flags
7947 * RETURN:
7948 * SUCCESS : TRUE
7949 * FAILURE : FALSE
7951 static BOOL LISTVIEW_SetItemCount(LISTVIEW_INFO *infoPtr, INT nItems, DWORD dwFlags)
7953 TRACE("(nItems=%d, dwFlags=%x)\n", nItems, dwFlags);
7955 if (infoPtr->dwStyle & LVS_OWNERDATA)
7957 INT nOldCount = infoPtr->nItemCount;
7959 if (nItems < nOldCount)
7961 RANGE range = { nItems, nOldCount };
7962 ranges_del(infoPtr->selectionRanges, range);
7963 if (infoPtr->nFocusedItem >= nItems)
7965 LISTVIEW_SetItemFocus(infoPtr, -1);
7966 SetRectEmpty(&infoPtr->rcFocus);
7970 infoPtr->nItemCount = nItems;
7971 LISTVIEW_UpdateScroll(infoPtr);
7973 /* the flags are valid only in ownerdata report and list modes */
7974 if (infoPtr->uView == LV_VIEW_ICON || infoPtr->uView == LV_VIEW_SMALLICON) dwFlags = 0;
7976 if (!(dwFlags & LVSICF_NOSCROLL) && infoPtr->nFocusedItem != -1)
7977 LISTVIEW_EnsureVisible(infoPtr, infoPtr->nFocusedItem, FALSE);
7979 if (!(dwFlags & LVSICF_NOINVALIDATEALL))
7980 LISTVIEW_InvalidateList(infoPtr);
7981 else
7983 INT nFrom, nTo;
7984 POINT Origin;
7985 RECT rcErase;
7987 LISTVIEW_GetOrigin(infoPtr, &Origin);
7988 nFrom = min(nOldCount, nItems);
7989 nTo = max(nOldCount, nItems);
7991 if (infoPtr->uView == LV_VIEW_DETAILS)
7993 rcErase.left = 0;
7994 rcErase.top = nFrom * infoPtr->nItemHeight;
7995 rcErase.right = infoPtr->nItemWidth;
7996 rcErase.bottom = nTo * infoPtr->nItemHeight;
7997 OffsetRect(&rcErase, Origin.x, Origin.y);
7998 if (IntersectRect(&rcErase, &rcErase, &infoPtr->rcList))
7999 LISTVIEW_InvalidateRect(infoPtr, &rcErase);
8001 else /* LV_VIEW_LIST */
8003 INT nPerCol = LISTVIEW_GetCountPerColumn(infoPtr);
8005 rcErase.left = (nFrom / nPerCol) * infoPtr->nItemWidth;
8006 rcErase.top = (nFrom % nPerCol) * infoPtr->nItemHeight;
8007 rcErase.right = rcErase.left + infoPtr->nItemWidth;
8008 rcErase.bottom = nPerCol * infoPtr->nItemHeight;
8009 OffsetRect(&rcErase, Origin.x, Origin.y);
8010 if (IntersectRect(&rcErase, &rcErase, &infoPtr->rcList))
8011 LISTVIEW_InvalidateRect(infoPtr, &rcErase);
8013 rcErase.left = (nFrom / nPerCol + 1) * infoPtr->nItemWidth;
8014 rcErase.top = 0;
8015 rcErase.right = (nTo / nPerCol + 1) * infoPtr->nItemWidth;
8016 rcErase.bottom = nPerCol * infoPtr->nItemHeight;
8017 OffsetRect(&rcErase, Origin.x, Origin.y);
8018 if (IntersectRect(&rcErase, &rcErase, &infoPtr->rcList))
8019 LISTVIEW_InvalidateRect(infoPtr, &rcErase);
8023 else
8025 /* According to MSDN for non-LVS_OWNERDATA this is just
8026 * a performance issue. The control allocates its internal
8027 * data structures for the number of items specified. It
8028 * cuts down on the number of memory allocations. Therefore
8029 * we will just issue a WARN here
8031 WARN("for non-ownerdata performance option not implemented.\n");
8034 return TRUE;
8037 /***
8038 * DESCRIPTION:
8039 * Sets the position of an item.
8041 * PARAMETER(S):
8042 * [I] infoPtr : valid pointer to the listview structure
8043 * [I] nItem : item index
8044 * [I] pt : coordinate
8046 * RETURN:
8047 * SUCCESS : TRUE
8048 * FAILURE : FALSE
8050 static BOOL LISTVIEW_SetItemPosition(LISTVIEW_INFO *infoPtr, INT nItem, POINT pt)
8052 POINT Origin;
8054 TRACE("(nItem=%d, &pt=%s\n", nItem, wine_dbgstr_point(&pt));
8056 if (nItem < 0 || nItem >= infoPtr->nItemCount ||
8057 !(infoPtr->uView == LV_VIEW_ICON || infoPtr->uView == LV_VIEW_SMALLICON)) return FALSE;
8059 LISTVIEW_GetOrigin(infoPtr, &Origin);
8061 /* This point value seems to be an undocumented feature.
8062 * The best guess is that it means either at the origin,
8063 * or at true beginning of the list. I will assume the origin. */
8064 if ((pt.x == -1) && (pt.y == -1))
8065 pt = Origin;
8067 if (infoPtr->uView == LV_VIEW_ICON)
8069 pt.x -= (infoPtr->nItemWidth - infoPtr->iconSize.cx) / 2;
8070 pt.y -= ICON_TOP_PADDING;
8072 pt.x -= Origin.x;
8073 pt.y -= Origin.y;
8075 infoPtr->bAutoarrange = FALSE;
8077 return LISTVIEW_MoveIconTo(infoPtr, nItem, &pt, FALSE);
8080 /***
8081 * DESCRIPTION:
8082 * Sets the state of one or many items.
8084 * PARAMETER(S):
8085 * [I] infoPtr : valid pointer to the listview structure
8086 * [I] nItem : item index
8087 * [I] lpLVItem : item or subitem info
8089 * RETURN:
8090 * SUCCESS : TRUE
8091 * FAILURE : FALSE
8093 static BOOL LISTVIEW_SetItemState(LISTVIEW_INFO *infoPtr, INT nItem, const LVITEMW *lpLVItem)
8095 BOOL bResult = TRUE;
8096 LVITEMW lvItem;
8098 lvItem.iItem = nItem;
8099 lvItem.iSubItem = 0;
8100 lvItem.mask = LVIF_STATE;
8101 lvItem.state = lpLVItem->state;
8102 lvItem.stateMask = lpLVItem->stateMask;
8103 TRACE("lvItem=%s\n", debuglvitem_t(&lvItem, TRUE));
8105 if (nItem == -1)
8107 /* select all isn't allowed in LVS_SINGLESEL */
8108 if ((lvItem.state & lvItem.stateMask & LVIS_SELECTED) && (infoPtr->dwStyle & LVS_SINGLESEL))
8109 return FALSE;
8111 /* apply to all items */
8112 for (lvItem.iItem = 0; lvItem.iItem < infoPtr->nItemCount; lvItem.iItem++)
8113 if (!LISTVIEW_SetItemT(infoPtr, &lvItem, TRUE)) bResult = FALSE;
8115 else
8116 bResult = LISTVIEW_SetItemT(infoPtr, &lvItem, TRUE);
8119 * Update selection mark
8121 * Investigation on windows 2k showed that selection mark was updated
8122 * whenever a new selection was made, but if the selected item was
8123 * unselected it was not updated.
8125 * we are probably still not 100% accurate, but this at least sets the
8126 * proper selection mark when it is needed
8129 if (bResult && (lvItem.state & lvItem.stateMask & LVIS_SELECTED) &&
8130 (infoPtr->nSelectionMark == -1))
8132 int i;
8133 for (i = 0; i < infoPtr->nItemCount; i++)
8135 if (infoPtr->uCallbackMask & LVIS_SELECTED)
8137 if (LISTVIEW_GetItemState(infoPtr, i, LVIS_SELECTED))
8139 infoPtr->nSelectionMark = i;
8140 break;
8143 else if (ranges_contain(infoPtr->selectionRanges, i))
8145 infoPtr->nSelectionMark = i;
8146 break;
8151 return bResult;
8154 /***
8155 * DESCRIPTION:
8156 * Sets the text of an item or subitem.
8158 * PARAMETER(S):
8159 * [I] hwnd : window handle
8160 * [I] nItem : item index
8161 * [I] lpLVItem : item or subitem info
8162 * [I] isW : TRUE if input is Unicode
8164 * RETURN:
8165 * SUCCESS : TRUE
8166 * FAILURE : FALSE
8168 static BOOL LISTVIEW_SetItemTextT(LISTVIEW_INFO *infoPtr, INT nItem, const LVITEMW *lpLVItem, BOOL isW)
8170 LVITEMW lvItem;
8172 if (nItem < 0 && nItem >= infoPtr->nItemCount) return FALSE;
8174 lvItem.iItem = nItem;
8175 lvItem.iSubItem = lpLVItem->iSubItem;
8176 lvItem.mask = LVIF_TEXT;
8177 lvItem.pszText = lpLVItem->pszText;
8178 lvItem.cchTextMax = lpLVItem->cchTextMax;
8180 TRACE("(nItem=%d, lpLVItem=%s, isW=%d)\n", nItem, debuglvitem_t(&lvItem, isW), isW);
8182 return LISTVIEW_SetItemT(infoPtr, &lvItem, isW);
8185 /***
8186 * DESCRIPTION:
8187 * Set item index that marks the start of a multiple selection.
8189 * PARAMETER(S):
8190 * [I] infoPtr : valid pointer to the listview structure
8191 * [I] nIndex : index
8193 * RETURN:
8194 * Index number or -1 if there is no selection mark.
8196 static INT LISTVIEW_SetSelectionMark(LISTVIEW_INFO *infoPtr, INT nIndex)
8198 INT nOldIndex = infoPtr->nSelectionMark;
8200 TRACE("(nIndex=%d)\n", nIndex);
8202 infoPtr->nSelectionMark = nIndex;
8204 return nOldIndex;
8207 /***
8208 * DESCRIPTION:
8209 * Sets the text background color.
8211 * PARAMETER(S):
8212 * [I] infoPtr : valid pointer to the listview structure
8213 * [I] clrTextBk : text background color
8215 * RETURN:
8216 * SUCCESS : TRUE
8217 * FAILURE : FALSE
8219 static BOOL LISTVIEW_SetTextBkColor(LISTVIEW_INFO *infoPtr, COLORREF clrTextBk)
8221 TRACE("(clrTextBk=%x)\n", clrTextBk);
8223 if (infoPtr->clrTextBk != clrTextBk)
8225 infoPtr->clrTextBk = clrTextBk;
8226 LISTVIEW_InvalidateList(infoPtr);
8229 return TRUE;
8232 /***
8233 * DESCRIPTION:
8234 * Sets the text foreground color.
8236 * PARAMETER(S):
8237 * [I] infoPtr : valid pointer to the listview structure
8238 * [I] clrText : text color
8240 * RETURN:
8241 * SUCCESS : TRUE
8242 * FAILURE : FALSE
8244 static BOOL LISTVIEW_SetTextColor (LISTVIEW_INFO *infoPtr, COLORREF clrText)
8246 TRACE("(clrText=%x)\n", clrText);
8248 if (infoPtr->clrText != clrText)
8250 infoPtr->clrText = clrText;
8251 LISTVIEW_InvalidateList(infoPtr);
8254 return TRUE;
8257 /***
8258 * DESCRIPTION:
8259 * Sets new ToolTip window to ListView control.
8261 * PARAMETER(S):
8262 * [I] infoPtr : valid pointer to the listview structure
8263 * [I] hwndNewToolTip : handle to new ToolTip
8265 * RETURN:
8266 * old tool tip
8268 static HWND LISTVIEW_SetToolTips( LISTVIEW_INFO *infoPtr, HWND hwndNewToolTip)
8270 HWND hwndOldToolTip = infoPtr->hwndToolTip;
8271 infoPtr->hwndToolTip = hwndNewToolTip;
8272 return hwndOldToolTip;
8276 * DESCRIPTION:
8277 * sets the Unicode character format flag for the control
8278 * PARAMETER(S):
8279 * [I] infoPtr :valid pointer to the listview structure
8280 * [I] fUnicode :true to switch to UNICODE false to switch to ANSI
8282 * RETURN:
8283 * Old Unicode Format
8285 static BOOL LISTVIEW_SetUnicodeFormat( LISTVIEW_INFO *infoPtr, BOOL fUnicode)
8287 SHORT rc = infoPtr->notifyFormat;
8288 infoPtr->notifyFormat = (fUnicode) ? NFR_UNICODE : NFR_ANSI;
8289 return rc == NFR_UNICODE;
8293 * DESCRIPTION:
8294 * sets the control view mode
8295 * PARAMETER(S):
8296 * [I] infoPtr :valid pointer to the listview structure
8297 * [I] nView :new view mode value
8299 * RETURN:
8300 * SUCCESS: 1
8301 * FAILURE: -1
8303 static INT LISTVIEW_SetView(LISTVIEW_INFO *infoPtr, DWORD nView)
8305 SIZE oldIconSize = infoPtr->iconSize;
8306 HIMAGELIST himl;
8308 if (infoPtr->uView == nView) return 1;
8310 if ((INT)nView < 0 || nView > LV_VIEW_MAX) return -1;
8311 if (nView == LV_VIEW_TILE)
8313 FIXME("View LV_VIEW_TILE unimplemented\n");
8314 return -1;
8317 infoPtr->uView = nView;
8319 SendMessageW(infoPtr->hwndEdit, WM_KILLFOCUS, 0, 0);
8320 ShowWindow(infoPtr->hwndHeader, SW_HIDE);
8322 ShowScrollBar(infoPtr->hwndSelf, SB_BOTH, FALSE);
8323 SetRectEmpty(&infoPtr->rcFocus);
8325 himl = (nView == LV_VIEW_ICON ? infoPtr->himlNormal : infoPtr->himlSmall);
8326 set_icon_size(&infoPtr->iconSize, himl, nView != LV_VIEW_ICON);
8328 switch (nView)
8330 case LV_VIEW_ICON:
8331 if ((infoPtr->iconSize.cx != oldIconSize.cx) || (infoPtr->iconSize.cy != oldIconSize.cy))
8333 TRACE("icon old size=(%d,%d), new size=(%d,%d)\n",
8334 oldIconSize.cx, oldIconSize.cy, infoPtr->iconSize.cx, infoPtr->iconSize.cy);
8335 LISTVIEW_SetIconSpacing(infoPtr, 0, 0);
8337 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
8338 break;
8339 case LV_VIEW_SMALLICON:
8340 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
8341 break;
8342 case LV_VIEW_DETAILS:
8344 HDLAYOUT hl;
8345 WINDOWPOS wp;
8347 LISTVIEW_CreateHeader( infoPtr );
8349 hl.prc = &infoPtr->rcList;
8350 hl.pwpos = &wp;
8351 SendMessageW(infoPtr->hwndHeader, HDM_LAYOUT, 0, (LPARAM)&hl);
8352 SetWindowPos(infoPtr->hwndHeader, infoPtr->hwndSelf, wp.x, wp.y, wp.cx, wp.cy,
8353 wp.flags | ((infoPtr->dwStyle & LVS_NOCOLUMNHEADER) ? SWP_HIDEWINDOW : SWP_SHOWWINDOW));
8354 break;
8356 case LV_VIEW_LIST:
8357 break;
8360 LISTVIEW_UpdateItemSize(infoPtr);
8361 LISTVIEW_UpdateSize(infoPtr);
8362 LISTVIEW_UpdateScroll(infoPtr);
8363 LISTVIEW_InvalidateList(infoPtr);
8365 TRACE("nView=%d\n", nView);
8367 return 1;
8370 /* LISTVIEW_SetWorkAreas */
8372 /***
8373 * DESCRIPTION:
8374 * Callback internally used by LISTVIEW_SortItems() in response of LVM_SORTITEMS
8376 * PARAMETER(S):
8377 * [I] first : pointer to first ITEM_INFO to compare
8378 * [I] second : pointer to second ITEM_INFO to compare
8379 * [I] lParam : HWND of control
8381 * RETURN:
8382 * if first comes before second : negative
8383 * if first comes after second : positive
8384 * if first and second are equivalent : zero
8386 static INT WINAPI LISTVIEW_CallBackCompare(LPVOID first, LPVOID second, LPARAM lParam)
8388 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)lParam;
8389 ITEM_INFO* lv_first = DPA_GetPtr( first, 0 );
8390 ITEM_INFO* lv_second = DPA_GetPtr( second, 0 );
8392 /* Forward the call to the client defined callback */
8393 return (infoPtr->pfnCompare)( lv_first->lParam , lv_second->lParam, infoPtr->lParamSort );
8396 /***
8397 * DESCRIPTION:
8398 * Callback internally used by LISTVIEW_SortItems() in response of LVM_SORTITEMSEX
8400 * PARAMETER(S):
8401 * [I] first : pointer to first ITEM_INFO to compare
8402 * [I] second : pointer to second ITEM_INFO to compare
8403 * [I] lParam : HWND of control
8405 * RETURN:
8406 * if first comes before second : negative
8407 * if first comes after second : positive
8408 * if first and second are equivalent : zero
8410 static INT WINAPI LISTVIEW_CallBackCompareEx(LPVOID first, LPVOID second, LPARAM lParam)
8412 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)lParam;
8413 INT first_idx = DPA_GetPtrIndex( infoPtr->hdpaItems, first );
8414 INT second_idx = DPA_GetPtrIndex( infoPtr->hdpaItems, second );
8416 /* Forward the call to the client defined callback */
8417 return (infoPtr->pfnCompare)( first_idx, second_idx, infoPtr->lParamSort );
8420 /***
8421 * DESCRIPTION:
8422 * Sorts the listview items.
8424 * PARAMETER(S):
8425 * [I] infoPtr : valid pointer to the listview structure
8426 * [I] pfnCompare : application-defined value
8427 * [I] lParamSort : pointer to comparison callback
8428 * [I] IsEx : TRUE when LVM_SORTITEMSEX used
8430 * RETURN:
8431 * SUCCESS : TRUE
8432 * FAILURE : FALSE
8434 static BOOL LISTVIEW_SortItems(LISTVIEW_INFO *infoPtr, PFNLVCOMPARE pfnCompare,
8435 LPARAM lParamSort, BOOL IsEx)
8437 HDPA hdpaSubItems;
8438 ITEM_INFO *lpItem;
8439 LPVOID selectionMarkItem = NULL;
8440 LPVOID focusedItem = NULL;
8441 int i;
8443 TRACE("(pfnCompare=%p, lParamSort=%lx)\n", pfnCompare, lParamSort);
8445 if (infoPtr->dwStyle & LVS_OWNERDATA) return FALSE;
8447 if (!pfnCompare) return FALSE;
8448 if (!infoPtr->hdpaItems) return FALSE;
8450 /* if there are 0 or 1 items, there is no need to sort */
8451 if (infoPtr->nItemCount < 2) return TRUE;
8453 /* clear selection */
8454 ranges_clear(infoPtr->selectionRanges);
8456 /* save selection mark and focused item */
8457 if (infoPtr->nSelectionMark >= 0)
8458 selectionMarkItem = DPA_GetPtr(infoPtr->hdpaItems, infoPtr->nSelectionMark);
8459 if (infoPtr->nFocusedItem >= 0)
8460 focusedItem = DPA_GetPtr(infoPtr->hdpaItems, infoPtr->nFocusedItem);
8462 infoPtr->pfnCompare = pfnCompare;
8463 infoPtr->lParamSort = lParamSort;
8464 if (IsEx)
8465 DPA_Sort(infoPtr->hdpaItems, LISTVIEW_CallBackCompareEx, (LPARAM)infoPtr);
8466 else
8467 DPA_Sort(infoPtr->hdpaItems, LISTVIEW_CallBackCompare, (LPARAM)infoPtr);
8469 /* restore selection ranges */
8470 for (i=0; i < infoPtr->nItemCount; i++)
8472 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, i);
8473 lpItem = DPA_GetPtr(hdpaSubItems, 0);
8475 if (lpItem->state & LVIS_SELECTED)
8476 ranges_additem(infoPtr->selectionRanges, i);
8478 /* restore selection mark and focused item */
8479 infoPtr->nSelectionMark = DPA_GetPtrIndex(infoPtr->hdpaItems, selectionMarkItem);
8480 infoPtr->nFocusedItem = DPA_GetPtrIndex(infoPtr->hdpaItems, focusedItem);
8482 /* I believe nHotItem should be left alone, see LISTVIEW_ShiftIndices */
8484 /* refresh the display */
8485 if (infoPtr->uView != LV_VIEW_ICON && infoPtr->uView != LV_VIEW_SMALLICON)
8486 LISTVIEW_InvalidateList(infoPtr);
8488 return TRUE;
8491 /***
8492 * DESCRIPTION:
8493 * Update theme handle after a theme change.
8495 * PARAMETER(S):
8496 * [I] infoPtr : valid pointer to the listview structure
8498 * RETURN:
8499 * SUCCESS : 0
8500 * FAILURE : something else
8502 static LRESULT LISTVIEW_ThemeChanged(const LISTVIEW_INFO *infoPtr)
8504 HTHEME theme = GetWindowTheme(infoPtr->hwndSelf);
8505 CloseThemeData(theme);
8506 OpenThemeData(infoPtr->hwndSelf, themeClass);
8507 return 0;
8510 /***
8511 * DESCRIPTION:
8512 * Updates an items or rearranges the listview control.
8514 * PARAMETER(S):
8515 * [I] infoPtr : valid pointer to the listview structure
8516 * [I] nItem : item index
8518 * RETURN:
8519 * SUCCESS : TRUE
8520 * FAILURE : FALSE
8522 static BOOL LISTVIEW_Update(LISTVIEW_INFO *infoPtr, INT nItem)
8524 TRACE("(nItem=%d)\n", nItem);
8526 if (nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
8528 /* rearrange with default alignment style */
8529 if (is_autoarrange(infoPtr))
8530 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
8531 else
8532 LISTVIEW_InvalidateItem(infoPtr, nItem);
8534 return TRUE;
8537 /***
8538 * DESCRIPTION:
8539 * Draw the track line at the place defined in the infoPtr structure.
8540 * The line is drawn with a XOR pen so drawing the line for the second time
8541 * in the same place erases the line.
8543 * PARAMETER(S):
8544 * [I] infoPtr : valid pointer to the listview structure
8546 * RETURN:
8547 * SUCCESS : TRUE
8548 * FAILURE : FALSE
8550 static BOOL LISTVIEW_DrawTrackLine(const LISTVIEW_INFO *infoPtr)
8552 HPEN hOldPen;
8553 HDC hdc;
8554 INT oldROP;
8556 if (infoPtr->xTrackLine == -1)
8557 return FALSE;
8559 if (!(hdc = GetDC(infoPtr->hwndSelf)))
8560 return FALSE;
8561 hOldPen = SelectObject(hdc, GetStockObject(BLACK_PEN));
8562 oldROP = SetROP2(hdc, R2_XORPEN);
8563 MoveToEx(hdc, infoPtr->xTrackLine, infoPtr->rcList.top, NULL);
8564 LineTo(hdc, infoPtr->xTrackLine, infoPtr->rcList.bottom);
8565 SetROP2(hdc, oldROP);
8566 SelectObject(hdc, hOldPen);
8567 ReleaseDC(infoPtr->hwndSelf, hdc);
8568 return TRUE;
8571 /***
8572 * DESCRIPTION:
8573 * Called when an edit control should be displayed. This function is called after
8574 * we are sure that there was a single click - not a double click (this is a TIMERPROC).
8576 * PARAMETER(S):
8577 * [I] hwnd : Handle to the listview
8578 * [I] uMsg : WM_TIMER (ignored)
8579 * [I] idEvent : The timer ID interpreted as a pointer to a DELAYED_EDIT_ITEM struct
8580 * [I] dwTimer : The elapsed time (ignored)
8582 * RETURN:
8583 * None.
8585 static VOID CALLBACK LISTVIEW_DelayedEditItem(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime)
8587 DELAYED_ITEM_EDIT *editItem = (DELAYED_ITEM_EDIT *)idEvent;
8588 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)GetWindowLongPtrW(hwnd, 0);
8590 KillTimer(hwnd, idEvent);
8591 editItem->fEnabled = FALSE;
8592 /* check if the item is still selected */
8593 if (infoPtr->bFocus && LISTVIEW_GetItemState(infoPtr, editItem->iItem, LVIS_SELECTED))
8594 LISTVIEW_EditLabelT(infoPtr, editItem->iItem, TRUE);
8597 /***
8598 * DESCRIPTION:
8599 * Creates the listview control - the WM_NCCREATE phase.
8601 * PARAMETER(S):
8602 * [I] hwnd : window handle
8603 * [I] lpcs : the create parameters
8605 * RETURN:
8606 * Success: TRUE
8607 * Failure: FALSE
8609 static LRESULT LISTVIEW_NCCreate(HWND hwnd, const CREATESTRUCTW *lpcs)
8611 LISTVIEW_INFO *infoPtr;
8612 LOGFONTW logFont;
8614 TRACE("(lpcs=%p)\n", lpcs);
8616 /* initialize info pointer */
8617 infoPtr = Alloc(sizeof(LISTVIEW_INFO));
8618 if (!infoPtr) return FALSE;
8620 SetWindowLongPtrW(hwnd, 0, (DWORD_PTR)infoPtr);
8622 infoPtr->hwndSelf = hwnd;
8623 infoPtr->dwStyle = lpcs->style; /* Note: may be changed in WM_CREATE */
8624 map_style_view(infoPtr);
8625 /* determine the type of structures to use */
8626 infoPtr->hwndNotify = lpcs->hwndParent;
8627 /* infoPtr->notifyFormat will be filled in WM_CREATE */
8629 /* initialize color information */
8630 infoPtr->clrBk = CLR_NONE;
8631 infoPtr->clrText = CLR_DEFAULT;
8632 infoPtr->clrTextBk = CLR_DEFAULT;
8633 LISTVIEW_SetBkColor(infoPtr, comctl32_color.clrWindow);
8635 /* set default values */
8636 infoPtr->nFocusedItem = -1;
8637 infoPtr->nSelectionMark = -1;
8638 infoPtr->nHotItem = -1;
8639 infoPtr->bRedraw = TRUE;
8640 infoPtr->bNoItemMetrics = TRUE;
8641 infoPtr->bDoChangeNotify = TRUE;
8642 infoPtr->iconSpacing.cx = GetSystemMetrics(SM_CXICONSPACING);
8643 infoPtr->iconSpacing.cy = GetSystemMetrics(SM_CYICONSPACING);
8644 infoPtr->nEditLabelItem = -1;
8645 infoPtr->nLButtonDownItem = -1;
8646 infoPtr->dwHoverTime = -1; /* default system hover time */
8647 infoPtr->nMeasureItemHeight = 0;
8648 infoPtr->xTrackLine = -1; /* no track line */
8649 infoPtr->itemEdit.fEnabled = FALSE;
8650 infoPtr->iVersion = COMCTL32_VERSION;
8652 /* get default font (icon title) */
8653 SystemParametersInfoW(SPI_GETICONTITLELOGFONT, 0, &logFont, 0);
8654 infoPtr->hDefaultFont = CreateFontIndirectW(&logFont);
8655 infoPtr->hFont = infoPtr->hDefaultFont;
8656 LISTVIEW_SaveTextMetrics(infoPtr);
8658 /* allocate memory for the data structure */
8659 if (!(infoPtr->selectionRanges = ranges_create(10))) goto fail;
8660 if (!(infoPtr->hdpaItems = DPA_Create(10))) goto fail;
8661 if (!(infoPtr->hdpaItemIds = DPA_Create(10))) goto fail;
8662 if (!(infoPtr->hdpaPosX = DPA_Create(10))) goto fail;
8663 if (!(infoPtr->hdpaPosY = DPA_Create(10))) goto fail;
8664 if (!(infoPtr->hdpaColumns = DPA_Create(10))) goto fail;
8665 return TRUE;
8667 fail:
8668 DestroyWindow(infoPtr->hwndHeader);
8669 ranges_destroy(infoPtr->selectionRanges);
8670 DPA_Destroy(infoPtr->hdpaItems);
8671 DPA_Destroy(infoPtr->hdpaItemIds);
8672 DPA_Destroy(infoPtr->hdpaPosX);
8673 DPA_Destroy(infoPtr->hdpaPosY);
8674 DPA_Destroy(infoPtr->hdpaColumns);
8675 Free(infoPtr);
8676 return FALSE;
8679 /***
8680 * DESCRIPTION:
8681 * Creates the listview control - the WM_CREATE phase. Most of the data is
8682 * already set up in LISTVIEW_NCCreate
8684 * PARAMETER(S):
8685 * [I] hwnd : window handle
8686 * [I] lpcs : the create parameters
8688 * RETURN:
8689 * Success: 0
8690 * Failure: -1
8692 static LRESULT LISTVIEW_Create(HWND hwnd, const CREATESTRUCTW *lpcs)
8694 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)GetWindowLongPtrW(hwnd, 0);
8696 TRACE("(lpcs=%p)\n", lpcs);
8698 infoPtr->dwStyle = lpcs->style;
8699 map_style_view(infoPtr);
8701 infoPtr->notifyFormat = SendMessageW(infoPtr->hwndNotify, WM_NOTIFYFORMAT,
8702 (WPARAM)infoPtr->hwndSelf, (LPARAM)NF_QUERY);
8703 /* on error defaulting to ANSI notifications */
8704 if (infoPtr->notifyFormat == 0) infoPtr->notifyFormat = NFR_ANSI;
8706 if ((infoPtr->uView == LV_VIEW_DETAILS) && (lpcs->style & WS_VISIBLE))
8708 if (LISTVIEW_CreateHeader(infoPtr) < 0) return -1;
8710 else
8711 infoPtr->hwndHeader = 0;
8713 /* init item size to avoid division by 0 */
8714 LISTVIEW_UpdateItemSize (infoPtr);
8716 if (infoPtr->uView == LV_VIEW_DETAILS)
8718 if (!(LVS_NOCOLUMNHEADER & lpcs->style) && (WS_VISIBLE & lpcs->style))
8720 ShowWindow(infoPtr->hwndHeader, SW_SHOWNORMAL);
8722 LISTVIEW_UpdateScroll(infoPtr);
8723 /* send WM_MEASUREITEM notification */
8724 if (infoPtr->dwStyle & LVS_OWNERDRAWFIXED) notify_measureitem(infoPtr);
8727 OpenThemeData(hwnd, themeClass);
8729 /* initialize the icon sizes */
8730 set_icon_size(&infoPtr->iconSize, infoPtr->himlNormal, infoPtr->uView != LV_VIEW_ICON);
8731 set_icon_size(&infoPtr->iconStateSize, infoPtr->himlState, TRUE);
8732 return 0;
8735 /***
8736 * DESCRIPTION:
8737 * Destroys the listview control.
8739 * PARAMETER(S):
8740 * [I] infoPtr : valid pointer to the listview structure
8742 * RETURN:
8743 * Success: 0
8744 * Failure: -1
8746 static LRESULT LISTVIEW_Destroy(const LISTVIEW_INFO *infoPtr)
8748 HTHEME theme = GetWindowTheme(infoPtr->hwndSelf);
8749 CloseThemeData(theme);
8750 return 0;
8753 /***
8754 * DESCRIPTION:
8755 * Enables the listview control.
8757 * PARAMETER(S):
8758 * [I] infoPtr : valid pointer to the listview structure
8759 * [I] bEnable : specifies whether to enable or disable the window
8761 * RETURN:
8762 * SUCCESS : TRUE
8763 * FAILURE : FALSE
8765 static BOOL LISTVIEW_Enable(const LISTVIEW_INFO *infoPtr, BOOL bEnable)
8767 if (infoPtr->dwStyle & LVS_OWNERDRAWFIXED)
8768 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
8769 return TRUE;
8772 /***
8773 * DESCRIPTION:
8774 * Erases the background of the listview control.
8776 * PARAMETER(S):
8777 * [I] infoPtr : valid pointer to the listview structure
8778 * [I] hdc : device context handle
8780 * RETURN:
8781 * SUCCESS : TRUE
8782 * FAILURE : FALSE
8784 static inline BOOL LISTVIEW_EraseBkgnd(const LISTVIEW_INFO *infoPtr, HDC hdc)
8786 RECT rc;
8788 TRACE("(hdc=%p)\n", hdc);
8790 if (!GetClipBox(hdc, &rc)) return FALSE;
8792 if (infoPtr->clrBk == CLR_NONE)
8793 return SendMessageW(infoPtr->hwndNotify, WM_ERASEBKGND, (WPARAM)hdc, 0);
8795 /* for double buffered controls we need to do this during refresh */
8796 if (infoPtr->dwLvExStyle & LVS_EX_DOUBLEBUFFER) return FALSE;
8798 return LISTVIEW_FillBkgnd(infoPtr, hdc, &rc);
8802 /***
8803 * DESCRIPTION:
8804 * Helper function for LISTVIEW_[HV]Scroll *only*.
8805 * Performs vertical/horizontal scrolling by a give amount.
8807 * PARAMETER(S):
8808 * [I] infoPtr : valid pointer to the listview structure
8809 * [I] dx : amount of horizontal scroll
8810 * [I] dy : amount of vertical scroll
8812 static void scroll_list(LISTVIEW_INFO *infoPtr, INT dx, INT dy)
8814 /* now we can scroll the list */
8815 ScrollWindowEx(infoPtr->hwndSelf, dx, dy, &infoPtr->rcList,
8816 &infoPtr->rcList, 0, 0, SW_ERASE | SW_INVALIDATE);
8817 /* if we have focus, adjust rect */
8818 OffsetRect(&infoPtr->rcFocus, dx, dy);
8819 UpdateWindow(infoPtr->hwndSelf);
8822 /***
8823 * DESCRIPTION:
8824 * Performs vertical scrolling.
8826 * PARAMETER(S):
8827 * [I] infoPtr : valid pointer to the listview structure
8828 * [I] nScrollCode : scroll code
8829 * [I] nScrollDiff : units to scroll in SB_INTERNAL mode, 0 otherwise
8830 * [I] hScrollWnd : scrollbar control window handle
8832 * RETURN:
8833 * Zero
8835 * NOTES:
8836 * SB_LINEUP/SB_LINEDOWN:
8837 * for LVS_ICON, LVS_SMALLICON is 37 by experiment
8838 * for LVS_REPORT is 1 line
8839 * for LVS_LIST cannot occur
8842 static LRESULT LISTVIEW_VScroll(LISTVIEW_INFO *infoPtr, INT nScrollCode,
8843 INT nScrollDiff, HWND hScrollWnd)
8845 INT nOldScrollPos, nNewScrollPos;
8846 SCROLLINFO scrollInfo;
8847 BOOL is_an_icon;
8849 TRACE("(nScrollCode=%d(%s), nScrollDiff=%d)\n", nScrollCode,
8850 debugscrollcode(nScrollCode), nScrollDiff);
8852 if (infoPtr->hwndEdit) SendMessageW(infoPtr->hwndEdit, WM_KILLFOCUS, 0, 0);
8854 scrollInfo.cbSize = sizeof(SCROLLINFO);
8855 scrollInfo.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_TRACKPOS;
8857 is_an_icon = ((infoPtr->uView == LV_VIEW_ICON) || (infoPtr->uView == LV_VIEW_SMALLICON));
8859 if (!GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo)) return 1;
8861 nOldScrollPos = scrollInfo.nPos;
8862 switch (nScrollCode)
8864 case SB_INTERNAL:
8865 break;
8867 case SB_LINEUP:
8868 nScrollDiff = (is_an_icon) ? -LISTVIEW_SCROLL_ICON_LINE_SIZE : -1;
8869 break;
8871 case SB_LINEDOWN:
8872 nScrollDiff = (is_an_icon) ? LISTVIEW_SCROLL_ICON_LINE_SIZE : 1;
8873 break;
8875 case SB_PAGEUP:
8876 nScrollDiff = -scrollInfo.nPage;
8877 break;
8879 case SB_PAGEDOWN:
8880 nScrollDiff = scrollInfo.nPage;
8881 break;
8883 case SB_THUMBPOSITION:
8884 case SB_THUMBTRACK:
8885 nScrollDiff = scrollInfo.nTrackPos - scrollInfo.nPos;
8886 break;
8888 default:
8889 nScrollDiff = 0;
8892 /* quit right away if pos isn't changing */
8893 if (nScrollDiff == 0) return 0;
8895 /* calculate new position, and handle overflows */
8896 nNewScrollPos = scrollInfo.nPos + nScrollDiff;
8897 if (nScrollDiff > 0) {
8898 if (nNewScrollPos < nOldScrollPos ||
8899 nNewScrollPos > scrollInfo.nMax)
8900 nNewScrollPos = scrollInfo.nMax;
8901 } else {
8902 if (nNewScrollPos > nOldScrollPos ||
8903 nNewScrollPos < scrollInfo.nMin)
8904 nNewScrollPos = scrollInfo.nMin;
8907 /* set the new position, and reread in case it changed */
8908 scrollInfo.fMask = SIF_POS;
8909 scrollInfo.nPos = nNewScrollPos;
8910 nNewScrollPos = SetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo, TRUE);
8912 /* carry on only if it really changed */
8913 if (nNewScrollPos == nOldScrollPos) return 0;
8915 /* now adjust to client coordinates */
8916 nScrollDiff = nOldScrollPos - nNewScrollPos;
8917 if (infoPtr->uView == LV_VIEW_DETAILS) nScrollDiff *= infoPtr->nItemHeight;
8919 /* and scroll the window */
8920 scroll_list(infoPtr, 0, nScrollDiff);
8922 return 0;
8925 /***
8926 * DESCRIPTION:
8927 * Performs horizontal scrolling.
8929 * PARAMETER(S):
8930 * [I] infoPtr : valid pointer to the listview structure
8931 * [I] nScrollCode : scroll code
8932 * [I] nScrollDiff : units to scroll in SB_INTERNAL mode, 0 otherwise
8933 * [I] hScrollWnd : scrollbar control window handle
8935 * RETURN:
8936 * Zero
8938 * NOTES:
8939 * SB_LINELEFT/SB_LINERIGHT:
8940 * for LVS_ICON, LVS_SMALLICON 1 pixel
8941 * for LVS_REPORT is 1 pixel
8942 * for LVS_LIST is 1 column --> which is a 1 because the
8943 * scroll is based on columns not pixels
8946 static LRESULT LISTVIEW_HScroll(LISTVIEW_INFO *infoPtr, INT nScrollCode,
8947 INT nScrollDiff, HWND hScrollWnd)
8949 INT nOldScrollPos, nNewScrollPos;
8950 SCROLLINFO scrollInfo;
8952 TRACE("(nScrollCode=%d(%s), nScrollDiff=%d)\n", nScrollCode,
8953 debugscrollcode(nScrollCode), nScrollDiff);
8955 if (infoPtr->hwndEdit) SendMessageW(infoPtr->hwndEdit, WM_KILLFOCUS, 0, 0);
8957 scrollInfo.cbSize = sizeof(SCROLLINFO);
8958 scrollInfo.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_TRACKPOS;
8960 if (!GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo)) return 1;
8962 nOldScrollPos = scrollInfo.nPos;
8964 switch (nScrollCode)
8966 case SB_INTERNAL:
8967 break;
8969 case SB_LINELEFT:
8970 nScrollDiff = -1;
8971 break;
8973 case SB_LINERIGHT:
8974 nScrollDiff = 1;
8975 break;
8977 case SB_PAGELEFT:
8978 nScrollDiff = -scrollInfo.nPage;
8979 break;
8981 case SB_PAGERIGHT:
8982 nScrollDiff = scrollInfo.nPage;
8983 break;
8985 case SB_THUMBPOSITION:
8986 case SB_THUMBTRACK:
8987 nScrollDiff = scrollInfo.nTrackPos - scrollInfo.nPos;
8988 break;
8990 default:
8991 nScrollDiff = 0;
8994 /* quit right away if pos isn't changing */
8995 if (nScrollDiff == 0) return 0;
8997 /* calculate new position, and handle overflows */
8998 nNewScrollPos = scrollInfo.nPos + nScrollDiff;
8999 if (nScrollDiff > 0) {
9000 if (nNewScrollPos < nOldScrollPos ||
9001 nNewScrollPos > scrollInfo.nMax)
9002 nNewScrollPos = scrollInfo.nMax;
9003 } else {
9004 if (nNewScrollPos > nOldScrollPos ||
9005 nNewScrollPos < scrollInfo.nMin)
9006 nNewScrollPos = scrollInfo.nMin;
9009 /* set the new position, and reread in case it changed */
9010 scrollInfo.fMask = SIF_POS;
9011 scrollInfo.nPos = nNewScrollPos;
9012 nNewScrollPos = SetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo, TRUE);
9014 /* carry on only if it really changed */
9015 if (nNewScrollPos == nOldScrollPos) return 0;
9017 if (infoPtr->uView == LV_VIEW_DETAILS)
9018 LISTVIEW_UpdateHeaderSize(infoPtr, nNewScrollPos);
9020 /* now adjust to client coordinates */
9021 nScrollDiff = nOldScrollPos - nNewScrollPos;
9022 if (infoPtr->uView == LV_VIEW_LIST) nScrollDiff *= infoPtr->nItemWidth;
9024 /* and scroll the window */
9025 scroll_list(infoPtr, nScrollDiff, 0);
9027 return 0;
9030 static LRESULT LISTVIEW_MouseWheel(LISTVIEW_INFO *infoPtr, INT wheelDelta)
9032 INT gcWheelDelta = 0;
9033 INT pulScrollLines = 3;
9034 SCROLLINFO scrollInfo;
9036 TRACE("(wheelDelta=%d)\n", wheelDelta);
9038 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES,0, &pulScrollLines, 0);
9039 gcWheelDelta -= wheelDelta;
9041 scrollInfo.cbSize = sizeof(SCROLLINFO);
9042 scrollInfo.fMask = SIF_POS;
9044 switch(infoPtr->uView)
9046 case LV_VIEW_ICON:
9047 case LV_VIEW_SMALLICON:
9049 * listview should be scrolled by a multiple of 37 dependently on its dimension or its visible item number
9050 * should be fixed in the future.
9052 LISTVIEW_VScroll(infoPtr, SB_INTERNAL, (gcWheelDelta < 0) ?
9053 -LISTVIEW_SCROLL_ICON_LINE_SIZE : LISTVIEW_SCROLL_ICON_LINE_SIZE, 0);
9054 break;
9056 case LV_VIEW_DETAILS:
9057 if (abs(gcWheelDelta) >= WHEEL_DELTA && pulScrollLines)
9059 int cLineScroll = min(LISTVIEW_GetCountPerColumn(infoPtr), pulScrollLines);
9060 cLineScroll *= (gcWheelDelta / WHEEL_DELTA);
9061 LISTVIEW_VScroll(infoPtr, SB_INTERNAL, cLineScroll, 0);
9063 break;
9065 case LV_VIEW_LIST:
9066 LISTVIEW_HScroll(infoPtr, (gcWheelDelta < 0) ? SB_LINELEFT : SB_LINERIGHT, 0, 0);
9067 break;
9069 return 0;
9072 /***
9073 * DESCRIPTION:
9074 * ???
9076 * PARAMETER(S):
9077 * [I] infoPtr : valid pointer to the listview structure
9078 * [I] nVirtualKey : virtual key
9079 * [I] lKeyData : key data
9081 * RETURN:
9082 * Zero
9084 static LRESULT LISTVIEW_KeyDown(LISTVIEW_INFO *infoPtr, INT nVirtualKey, LONG lKeyData)
9086 HWND hwndSelf = infoPtr->hwndSelf;
9087 INT nItem = -1;
9088 NMLVKEYDOWN nmKeyDown;
9090 TRACE("(nVirtualKey=%d, lKeyData=%d)\n", nVirtualKey, lKeyData);
9092 /* send LVN_KEYDOWN notification */
9093 nmKeyDown.wVKey = nVirtualKey;
9094 nmKeyDown.flags = 0;
9095 notify_hdr(infoPtr, LVN_KEYDOWN, &nmKeyDown.hdr);
9096 if (!IsWindow(hwndSelf))
9097 return 0;
9099 switch (nVirtualKey)
9101 case VK_SPACE:
9102 nItem = infoPtr->nFocusedItem;
9103 if (infoPtr->dwLvExStyle & LVS_EX_CHECKBOXES)
9104 toggle_checkbox_state(infoPtr, infoPtr->nFocusedItem);
9105 break;
9107 case VK_RETURN:
9108 if ((infoPtr->nItemCount > 0) && (infoPtr->nFocusedItem != -1))
9110 if (!notify(infoPtr, NM_RETURN)) return 0;
9111 if (!notify(infoPtr, LVN_ITEMACTIVATE)) return 0;
9113 break;
9115 case VK_HOME:
9116 if (infoPtr->nItemCount > 0)
9117 nItem = 0;
9118 break;
9120 case VK_END:
9121 if (infoPtr->nItemCount > 0)
9122 nItem = infoPtr->nItemCount - 1;
9123 break;
9125 case VK_LEFT:
9126 nItem = LISTVIEW_GetNextItem(infoPtr, infoPtr->nFocusedItem, LVNI_TOLEFT);
9127 break;
9129 case VK_UP:
9130 nItem = LISTVIEW_GetNextItem(infoPtr, infoPtr->nFocusedItem, LVNI_ABOVE);
9131 break;
9133 case VK_RIGHT:
9134 nItem = LISTVIEW_GetNextItem(infoPtr, infoPtr->nFocusedItem, LVNI_TORIGHT);
9135 break;
9137 case VK_DOWN:
9138 nItem = LISTVIEW_GetNextItem(infoPtr, infoPtr->nFocusedItem, LVNI_BELOW);
9139 break;
9141 case VK_PRIOR:
9142 if (infoPtr->uView == LV_VIEW_DETAILS)
9144 INT topidx = LISTVIEW_GetTopIndex(infoPtr);
9145 if (infoPtr->nFocusedItem == topidx)
9146 nItem = topidx - LISTVIEW_GetCountPerColumn(infoPtr) + 1;
9147 else
9148 nItem = topidx;
9150 else
9151 nItem = infoPtr->nFocusedItem - LISTVIEW_GetCountPerColumn(infoPtr)
9152 * LISTVIEW_GetCountPerRow(infoPtr);
9153 if(nItem < 0) nItem = 0;
9154 break;
9156 case VK_NEXT:
9157 if (infoPtr->uView == LV_VIEW_DETAILS)
9159 INT topidx = LISTVIEW_GetTopIndex(infoPtr);
9160 INT cnt = LISTVIEW_GetCountPerColumn(infoPtr);
9161 if (infoPtr->nFocusedItem == topidx + cnt - 1)
9162 nItem = infoPtr->nFocusedItem + cnt - 1;
9163 else
9164 nItem = topidx + cnt - 1;
9166 else
9167 nItem = infoPtr->nFocusedItem + LISTVIEW_GetCountPerColumn(infoPtr)
9168 * LISTVIEW_GetCountPerRow(infoPtr);
9169 if(nItem >= infoPtr->nItemCount) nItem = infoPtr->nItemCount - 1;
9170 break;
9173 if ((nItem != -1) && (nItem != infoPtr->nFocusedItem || nVirtualKey == VK_SPACE))
9174 LISTVIEW_KeySelection(infoPtr, nItem, nVirtualKey == VK_SPACE);
9176 return 0;
9179 /***
9180 * DESCRIPTION:
9181 * Kills the focus.
9183 * PARAMETER(S):
9184 * [I] infoPtr : valid pointer to the listview structure
9186 * RETURN:
9187 * Zero
9189 static LRESULT LISTVIEW_KillFocus(LISTVIEW_INFO *infoPtr)
9191 TRACE("()\n");
9193 /* if we did not have the focus, there's nothing to do */
9194 if (!infoPtr->bFocus) return 0;
9196 /* send NM_KILLFOCUS notification */
9197 if (!notify(infoPtr, NM_KILLFOCUS)) return 0;
9199 /* if we have a focus rectangle, get rid of it */
9200 LISTVIEW_ShowFocusRect(infoPtr, FALSE);
9202 /* set window focus flag */
9203 infoPtr->bFocus = FALSE;
9205 /* invalidate the selected items before resetting focus flag */
9206 LISTVIEW_InvalidateSelectedItems(infoPtr);
9208 return 0;
9211 /***
9212 * DESCRIPTION:
9213 * Processes double click messages (left mouse button).
9215 * PARAMETER(S):
9216 * [I] infoPtr : valid pointer to the listview structure
9217 * [I] wKey : key flag
9218 * [I] x,y : mouse coordinate
9220 * RETURN:
9221 * Zero
9223 static LRESULT LISTVIEW_LButtonDblClk(LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
9225 LVHITTESTINFO htInfo;
9227 TRACE("(key=%hu, X=%hu, Y=%hu)\n", wKey, x, y);
9229 /* Cancel the item edition if any */
9230 if (infoPtr->itemEdit.fEnabled)
9232 KillTimer(infoPtr->hwndSelf, (UINT_PTR)&infoPtr->itemEdit);
9233 infoPtr->itemEdit.fEnabled = FALSE;
9236 /* send NM_RELEASEDCAPTURE notification */
9237 if (!notify(infoPtr, NM_RELEASEDCAPTURE)) return 0;
9239 htInfo.pt.x = x;
9240 htInfo.pt.y = y;
9242 /* send NM_DBLCLK notification */
9243 LISTVIEW_HitTest(infoPtr, &htInfo, TRUE, FALSE);
9244 if (!notify_click(infoPtr, NM_DBLCLK, &htInfo)) return 0;
9246 /* To send the LVN_ITEMACTIVATE, it must be on an Item */
9247 if(htInfo.iItem != -1) notify_itemactivate(infoPtr,&htInfo);
9249 return 0;
9252 /***
9253 * DESCRIPTION:
9254 * Processes mouse down messages (left mouse button).
9256 * PARAMETERS:
9257 * infoPtr [I ] valid pointer to the listview structure
9258 * wKey [I ] key flag
9259 * x,y [I ] mouse coordinate
9261 * RETURN:
9262 * Zero
9264 static LRESULT LISTVIEW_LButtonDown(LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
9266 LVHITTESTINFO lvHitTestInfo;
9267 static BOOL bGroupSelect = TRUE;
9268 POINT pt = { x, y };
9269 INT nItem;
9271 TRACE("(key=%hu, X=%hu, Y=%hu)\n", wKey, x, y);
9273 /* send NM_RELEASEDCAPTURE notification */
9274 if (!notify(infoPtr, NM_RELEASEDCAPTURE)) return 0;
9276 /* set left button down flag and record the click position */
9277 infoPtr->bLButtonDown = TRUE;
9278 infoPtr->ptClickPos = pt;
9279 infoPtr->bDragging = FALSE;
9281 lvHitTestInfo.pt.x = x;
9282 lvHitTestInfo.pt.y = y;
9284 nItem = LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, TRUE);
9285 TRACE("at %s, nItem=%d\n", wine_dbgstr_point(&pt), nItem);
9286 if ((nItem >= 0) && (nItem < infoPtr->nItemCount))
9288 if ((infoPtr->dwLvExStyle & LVS_EX_CHECKBOXES) && (lvHitTestInfo.flags & LVHT_ONITEMSTATEICON))
9290 toggle_checkbox_state(infoPtr, nItem);
9291 return 0;
9294 if (infoPtr->dwStyle & LVS_SINGLESEL)
9296 if (LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED))
9297 infoPtr->nEditLabelItem = nItem;
9298 else
9299 LISTVIEW_SetSelection(infoPtr, nItem);
9301 else
9303 if ((wKey & MK_CONTROL) && (wKey & MK_SHIFT))
9305 if (bGroupSelect)
9307 if (!LISTVIEW_AddGroupSelection(infoPtr, nItem)) return 0;
9308 LISTVIEW_SetItemFocus(infoPtr, nItem);
9309 infoPtr->nSelectionMark = nItem;
9311 else
9313 LVITEMW item;
9315 item.state = LVIS_SELECTED | LVIS_FOCUSED;
9316 item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
9318 LISTVIEW_SetItemState(infoPtr,nItem,&item);
9319 infoPtr->nSelectionMark = nItem;
9322 else if (wKey & MK_CONTROL)
9324 LVITEMW item;
9326 bGroupSelect = (LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED) == 0);
9328 item.state = (bGroupSelect ? LVIS_SELECTED : 0) | LVIS_FOCUSED;
9329 item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
9330 LISTVIEW_SetItemState(infoPtr, nItem, &item);
9331 infoPtr->nSelectionMark = nItem;
9333 else if (wKey & MK_SHIFT)
9335 LISTVIEW_SetGroupSelection(infoPtr, nItem);
9337 else
9339 if (LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED))
9341 infoPtr->nEditLabelItem = nItem;
9342 infoPtr->nLButtonDownItem = nItem;
9344 LISTVIEW_SetItemFocus(infoPtr, nItem);
9346 else
9347 /* set selection (clears other pre-existing selections) */
9348 LISTVIEW_SetSelection(infoPtr, nItem);
9352 if (infoPtr->dwLvExStyle & LVS_EX_ONECLICKACTIVATE)
9353 if(lvHitTestInfo.iItem != -1) notify_itemactivate(infoPtr,&lvHitTestInfo);
9355 else
9357 /* remove all selections */
9358 if (!(wKey & MK_CONTROL) && !(wKey & MK_SHIFT))
9359 LISTVIEW_DeselectAll(infoPtr);
9360 ReleaseCapture();
9363 return 0;
9366 /***
9367 * DESCRIPTION:
9368 * Processes mouse up messages (left mouse button).
9370 * PARAMETERS:
9371 * infoPtr [I ] valid pointer to the listview structure
9372 * wKey [I ] key flag
9373 * x,y [I ] mouse coordinate
9375 * RETURN:
9376 * Zero
9378 static LRESULT LISTVIEW_LButtonUp(LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
9380 LVHITTESTINFO lvHitTestInfo;
9382 TRACE("(key=%hu, X=%hu, Y=%hu)\n", wKey, x, y);
9384 if (!infoPtr->bLButtonDown) return 0;
9386 lvHitTestInfo.pt.x = x;
9387 lvHitTestInfo.pt.y = y;
9389 /* send NM_CLICK notification */
9390 LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, FALSE);
9391 if (!notify_click(infoPtr, NM_CLICK, &lvHitTestInfo)) return 0;
9393 /* set left button flag */
9394 infoPtr->bLButtonDown = FALSE;
9396 /* set a single selection, reset others */
9397 if(lvHitTestInfo.iItem == infoPtr->nLButtonDownItem && lvHitTestInfo.iItem != -1)
9398 LISTVIEW_SetSelection(infoPtr, infoPtr->nLButtonDownItem);
9399 infoPtr->nLButtonDownItem = -1;
9401 if (infoPtr->bDragging)
9403 infoPtr->bDragging = FALSE;
9404 return 0;
9407 /* if we clicked on a selected item, edit the label */
9408 if(lvHitTestInfo.iItem == infoPtr->nEditLabelItem && (lvHitTestInfo.flags & LVHT_ONITEMLABEL))
9410 /* we want to make sure the user doesn't want to do a double click. So we will
9411 * delay the edit. WM_LBUTTONDBLCLICK will cancel the timer
9413 infoPtr->itemEdit.fEnabled = TRUE;
9414 infoPtr->itemEdit.iItem = lvHitTestInfo.iItem;
9415 SetTimer(infoPtr->hwndSelf,
9416 (UINT_PTR)&infoPtr->itemEdit,
9417 GetDoubleClickTime(),
9418 LISTVIEW_DelayedEditItem);
9421 if (!infoPtr->bFocus)
9422 SetFocus(infoPtr->hwndSelf);
9424 return 0;
9427 /***
9428 * DESCRIPTION:
9429 * Destroys the listview control (called after WM_DESTROY).
9431 * PARAMETER(S):
9432 * [I] infoPtr : valid pointer to the listview structure
9434 * RETURN:
9435 * Zero
9437 static LRESULT LISTVIEW_NCDestroy(LISTVIEW_INFO *infoPtr)
9439 TRACE("()\n");
9441 /* delete all items */
9442 LISTVIEW_DeleteAllItems(infoPtr, TRUE);
9444 /* destroy data structure */
9445 DPA_Destroy(infoPtr->hdpaItems);
9446 DPA_Destroy(infoPtr->hdpaItemIds);
9447 DPA_Destroy(infoPtr->hdpaPosX);
9448 DPA_Destroy(infoPtr->hdpaPosY);
9449 DPA_Destroy(infoPtr->hdpaColumns);
9450 ranges_destroy(infoPtr->selectionRanges);
9452 /* destroy image lists */
9453 if (!(infoPtr->dwStyle & LVS_SHAREIMAGELISTS))
9455 if (infoPtr->himlNormal)
9456 ImageList_Destroy(infoPtr->himlNormal);
9457 if (infoPtr->himlSmall)
9458 ImageList_Destroy(infoPtr->himlSmall);
9459 if (infoPtr->himlState)
9460 ImageList_Destroy(infoPtr->himlState);
9463 /* destroy font, bkgnd brush */
9464 infoPtr->hFont = 0;
9465 if (infoPtr->hDefaultFont) DeleteObject(infoPtr->hDefaultFont);
9466 if (infoPtr->clrBk != CLR_NONE) DeleteObject(infoPtr->hBkBrush);
9468 SetWindowLongPtrW(infoPtr->hwndSelf, 0, 0);
9470 /* free listview info pointer*/
9471 Free(infoPtr);
9473 return 0;
9476 /***
9477 * DESCRIPTION:
9478 * Handles notifications from header.
9480 * PARAMETER(S):
9481 * [I] infoPtr : valid pointer to the listview structure
9482 * [I] nCtrlId : control identifier
9483 * [I] lpnmh : notification information
9485 * RETURN:
9486 * Zero
9488 static LRESULT LISTVIEW_HeaderNotification(LISTVIEW_INFO *infoPtr, const NMHEADERW *lpnmh)
9490 HWND hwndSelf = infoPtr->hwndSelf;
9492 TRACE("(lpnmh=%p)\n", lpnmh);
9494 if (!lpnmh || lpnmh->iItem < 0 || lpnmh->iItem >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return 0;
9496 switch (lpnmh->hdr.code)
9498 case HDN_TRACKW:
9499 case HDN_TRACKA:
9501 COLUMN_INFO *lpColumnInfo;
9502 POINT ptOrigin;
9503 INT x;
9505 if (!lpnmh->pitem || !(lpnmh->pitem->mask & HDI_WIDTH))
9506 break;
9508 /* remove the old line (if any) */
9509 LISTVIEW_DrawTrackLine(infoPtr);
9511 /* compute & draw the new line */
9512 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, lpnmh->iItem);
9513 x = lpColumnInfo->rcHeader.left + lpnmh->pitem->cxy;
9514 LISTVIEW_GetOrigin(infoPtr, &ptOrigin);
9515 infoPtr->xTrackLine = x + ptOrigin.x;
9516 LISTVIEW_DrawTrackLine(infoPtr);
9517 break;
9520 case HDN_ENDTRACKA:
9521 case HDN_ENDTRACKW:
9522 /* remove the track line (if any) */
9523 LISTVIEW_DrawTrackLine(infoPtr);
9524 infoPtr->xTrackLine = -1;
9525 break;
9527 case HDN_BEGINDRAG:
9528 notify_forward_header(infoPtr, lpnmh);
9529 return (infoPtr->dwLvExStyle & LVS_EX_HEADERDRAGDROP) == 0;
9531 case HDN_ENDDRAG:
9532 FIXME("Changing column order not implemented\n");
9533 notify_forward_header(infoPtr, lpnmh);
9534 return TRUE;
9536 case HDN_ITEMCHANGINGW:
9537 case HDN_ITEMCHANGINGA:
9538 return notify_forward_header(infoPtr, lpnmh);
9540 case HDN_ITEMCHANGEDW:
9541 case HDN_ITEMCHANGEDA:
9543 COLUMN_INFO *lpColumnInfo;
9544 INT dx, cxy;
9546 notify_forward_header(infoPtr, lpnmh);
9547 if (!IsWindow(hwndSelf))
9548 break;
9550 if (!lpnmh->pitem || !(lpnmh->pitem->mask & HDI_WIDTH))
9552 HDITEMW hdi;
9554 hdi.mask = HDI_WIDTH;
9555 if (!SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, lpnmh->iItem, (LPARAM)&hdi)) return 0;
9556 cxy = hdi.cxy;
9558 else
9559 cxy = lpnmh->pitem->cxy;
9561 /* determine how much we change since the last know position */
9562 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, lpnmh->iItem);
9563 dx = cxy - (lpColumnInfo->rcHeader.right - lpColumnInfo->rcHeader.left);
9564 if (dx != 0)
9566 lpColumnInfo->rcHeader.right += dx;
9567 if (lpnmh->iItem + 1 < DPA_GetPtrCount(infoPtr->hdpaColumns))
9568 LISTVIEW_ScrollColumns(infoPtr, lpnmh->iItem + 1, dx);
9569 else
9571 /* only needs to update the scrolls */
9572 infoPtr->nItemWidth += dx;
9573 LISTVIEW_UpdateScroll(infoPtr);
9575 LISTVIEW_UpdateItemSize(infoPtr);
9576 if (infoPtr->uView == LV_VIEW_DETAILS && is_redrawing(infoPtr))
9578 POINT ptOrigin;
9579 RECT rcCol = lpColumnInfo->rcHeader;
9581 LISTVIEW_GetOrigin(infoPtr, &ptOrigin);
9582 OffsetRect(&rcCol, ptOrigin.x, 0);
9584 rcCol.top = infoPtr->rcList.top;
9585 rcCol.bottom = infoPtr->rcList.bottom;
9587 /* resizing left-aligned columns leaves most of the left side untouched */
9588 if ((lpColumnInfo->fmt & LVCFMT_JUSTIFYMASK) == LVCFMT_LEFT)
9590 INT nMaxDirty = infoPtr->nEllipsisWidth + infoPtr->ntmMaxCharWidth;
9591 if (dx > 0)
9592 nMaxDirty += dx;
9593 rcCol.left = max (rcCol.left, rcCol.right - nMaxDirty);
9596 /* when shrinking the last column clear the now unused field */
9597 if (lpnmh->iItem == DPA_GetPtrCount(infoPtr->hdpaColumns) - 1)
9599 RECT right;
9601 rcCol.right -= dx;
9603 /* deal with right from rightmost column area */
9604 right.left = rcCol.right;
9605 right.top = rcCol.top;
9606 right.bottom = rcCol.bottom;
9607 right.right = infoPtr->rcList.right;
9609 LISTVIEW_InvalidateRect(infoPtr, &right);
9612 LISTVIEW_InvalidateRect(infoPtr, &rcCol);
9616 break;
9618 case HDN_ITEMCLICKW:
9619 case HDN_ITEMCLICKA:
9621 /* Handle sorting by Header Column */
9622 NMLISTVIEW nmlv;
9624 ZeroMemory(&nmlv, sizeof(NMLISTVIEW));
9625 nmlv.iItem = -1;
9626 nmlv.iSubItem = lpnmh->iItem;
9627 notify_listview(infoPtr, LVN_COLUMNCLICK, &nmlv);
9628 notify_forward_header(infoPtr, lpnmh);
9630 break;
9632 case HDN_DIVIDERDBLCLICKW:
9633 case HDN_DIVIDERDBLCLICKA:
9634 LISTVIEW_SetColumnWidth(infoPtr, lpnmh->iItem, LVSCW_AUTOSIZE);
9635 notify_forward_header(infoPtr, lpnmh);
9636 break;
9639 return 0;
9642 /***
9643 * DESCRIPTION:
9644 * Paint non-client area of control.
9646 * PARAMETER(S):
9647 * [I] infoPtr : valid pointer to the listview structureof the sender
9648 * [I] region : update region
9650 * RETURN:
9651 * TRUE - frame was painted
9652 * FALSE - call default window proc
9654 static BOOL LISTVIEW_NCPaint(const LISTVIEW_INFO *infoPtr, HRGN region)
9656 HTHEME theme = GetWindowTheme (infoPtr->hwndSelf);
9657 HDC dc;
9658 RECT r;
9659 HRGN cliprgn;
9660 int cxEdge = GetSystemMetrics (SM_CXEDGE),
9661 cyEdge = GetSystemMetrics (SM_CYEDGE);
9663 if (!theme) return FALSE;
9665 GetWindowRect(infoPtr->hwndSelf, &r);
9667 cliprgn = CreateRectRgn (r.left + cxEdge, r.top + cyEdge,
9668 r.right - cxEdge, r.bottom - cyEdge);
9669 if (region != (HRGN)1)
9670 CombineRgn (cliprgn, cliprgn, region, RGN_AND);
9671 OffsetRect(&r, -r.left, -r.top);
9673 dc = GetDCEx(infoPtr->hwndSelf, region, DCX_WINDOW|DCX_INTERSECTRGN);
9674 OffsetRect(&r, -r.left, -r.top);
9676 if (IsThemeBackgroundPartiallyTransparent (theme, 0, 0))
9677 DrawThemeParentBackground(infoPtr->hwndSelf, dc, &r);
9678 DrawThemeBackground (theme, dc, 0, 0, &r, 0);
9679 ReleaseDC(infoPtr->hwndSelf, dc);
9681 /* Call default proc to get the scrollbars etc. painted */
9682 DefWindowProcW (infoPtr->hwndSelf, WM_NCPAINT, (WPARAM)cliprgn, 0);
9684 return TRUE;
9687 /***
9688 * DESCRIPTION:
9689 * Determines the type of structure to use.
9691 * PARAMETER(S):
9692 * [I] infoPtr : valid pointer to the listview structureof the sender
9693 * [I] hwndFrom : listview window handle
9694 * [I] nCommand : command specifying the nature of the WM_NOTIFYFORMAT
9696 * RETURN:
9697 * Zero
9699 static LRESULT LISTVIEW_NotifyFormat(LISTVIEW_INFO *infoPtr, HWND hwndFrom, INT nCommand)
9701 TRACE("(hwndFrom=%p, nCommand=%d)\n", hwndFrom, nCommand);
9703 if (nCommand == NF_REQUERY)
9704 infoPtr->notifyFormat = SendMessageW(infoPtr->hwndNotify, WM_NOTIFYFORMAT, (WPARAM)infoPtr->hwndSelf, NF_QUERY);
9706 return infoPtr->notifyFormat;
9709 /***
9710 * DESCRIPTION:
9711 * Paints/Repaints the listview control.
9713 * PARAMETER(S):
9714 * [I] infoPtr : valid pointer to the listview structure
9715 * [I] hdc : device context handle
9717 * RETURN:
9718 * Zero
9720 static LRESULT LISTVIEW_Paint(LISTVIEW_INFO *infoPtr, HDC hdc)
9722 TRACE("(hdc=%p)\n", hdc);
9724 if (infoPtr->bNoItemMetrics && infoPtr->nItemCount)
9726 infoPtr->bNoItemMetrics = FALSE;
9727 LISTVIEW_UpdateItemSize(infoPtr);
9728 if (infoPtr->uView == LV_VIEW_ICON || infoPtr->uView == LV_VIEW_SMALLICON)
9729 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
9730 LISTVIEW_UpdateScroll(infoPtr);
9733 if (infoPtr->hwndHeader) UpdateWindow(infoPtr->hwndHeader);
9735 if (hdc)
9736 LISTVIEW_Refresh(infoPtr, hdc, NULL);
9737 else
9739 PAINTSTRUCT ps;
9741 hdc = BeginPaint(infoPtr->hwndSelf, &ps);
9742 if (!hdc) return 1;
9743 LISTVIEW_Refresh(infoPtr, hdc, ps.fErase ? &ps.rcPaint : NULL);
9744 EndPaint(infoPtr->hwndSelf, &ps);
9747 return 0;
9751 /***
9752 * DESCRIPTION:
9753 * Paints/Repaints the listview control.
9755 * PARAMETER(S):
9756 * [I] infoPtr : valid pointer to the listview structure
9757 * [I] hdc : device context handle
9758 * [I] options : drawing options
9760 * RETURN:
9761 * Zero
9763 static LRESULT LISTVIEW_PrintClient(LISTVIEW_INFO *infoPtr, HDC hdc, DWORD options)
9765 FIXME("Partial Stub: (hdc=%p options=0x%08x)\n", hdc, options);
9767 if ((options & PRF_CHECKVISIBLE) && !IsWindowVisible(infoPtr->hwndSelf))
9768 return 0;
9770 if (options & PRF_ERASEBKGND)
9771 LISTVIEW_EraseBkgnd(infoPtr, hdc);
9773 if (options & PRF_CLIENT)
9774 LISTVIEW_Paint(infoPtr, hdc);
9776 return 0;
9780 /***
9781 * DESCRIPTION:
9782 * Processes double click messages (right mouse button).
9784 * PARAMETER(S):
9785 * [I] infoPtr : valid pointer to the listview structure
9786 * [I] wKey : key flag
9787 * [I] x,y : mouse coordinate
9789 * RETURN:
9790 * Zero
9792 static LRESULT LISTVIEW_RButtonDblClk(const LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
9794 LVHITTESTINFO lvHitTestInfo;
9796 TRACE("(key=%hu,X=%hu,Y=%hu)\n", wKey, x, y);
9798 /* send NM_RELEASEDCAPTURE notification */
9799 if (!notify(infoPtr, NM_RELEASEDCAPTURE)) return 0;
9801 /* send NM_RDBLCLK notification */
9802 lvHitTestInfo.pt.x = x;
9803 lvHitTestInfo.pt.y = y;
9804 LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, FALSE);
9805 notify_click(infoPtr, NM_RDBLCLK, &lvHitTestInfo);
9807 return 0;
9810 /***
9811 * DESCRIPTION:
9812 * Processes mouse down messages (right mouse button).
9814 * PARAMETER(S):
9815 * [I] infoPtr : valid pointer to the listview structure
9816 * [I] wKey : key flag
9817 * [I] x,y : mouse coordinate
9819 * RETURN:
9820 * Zero
9822 static LRESULT LISTVIEW_RButtonDown(LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
9824 LVHITTESTINFO lvHitTestInfo;
9825 INT nItem;
9827 TRACE("(key=%hu,X=%hu,Y=%hu)\n", wKey, x, y);
9829 /* send NM_RELEASEDCAPTURE notification */
9830 if (!notify(infoPtr, NM_RELEASEDCAPTURE)) return 0;
9832 /* make sure the listview control window has the focus */
9833 if (!infoPtr->bFocus) SetFocus(infoPtr->hwndSelf);
9835 /* set right button down flag */
9836 infoPtr->bRButtonDown = TRUE;
9838 /* determine the index of the selected item */
9839 lvHitTestInfo.pt.x = x;
9840 lvHitTestInfo.pt.y = y;
9841 nItem = LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, TRUE);
9843 if ((nItem >= 0) && (nItem < infoPtr->nItemCount))
9845 LISTVIEW_SetItemFocus(infoPtr, nItem);
9846 if (!((wKey & MK_SHIFT) || (wKey & MK_CONTROL)) &&
9847 !LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED))
9848 LISTVIEW_SetSelection(infoPtr, nItem);
9850 else
9852 LISTVIEW_DeselectAll(infoPtr);
9855 return 0;
9858 /***
9859 * DESCRIPTION:
9860 * Processes mouse up messages (right mouse button).
9862 * PARAMETER(S):
9863 * [I] infoPtr : valid pointer to the listview structure
9864 * [I] wKey : key flag
9865 * [I] x,y : mouse coordinate
9867 * RETURN:
9868 * Zero
9870 static LRESULT LISTVIEW_RButtonUp(LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
9872 LVHITTESTINFO lvHitTestInfo;
9873 POINT pt;
9875 TRACE("(key=%hu,X=%hu,Y=%hu)\n", wKey, x, y);
9877 if (!infoPtr->bRButtonDown) return 0;
9879 /* set button flag */
9880 infoPtr->bRButtonDown = FALSE;
9882 /* Send NM_RCLICK notification */
9883 lvHitTestInfo.pt.x = x;
9884 lvHitTestInfo.pt.y = y;
9885 LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, FALSE);
9886 if (!notify_click(infoPtr, NM_RCLICK, &lvHitTestInfo)) return 0;
9888 /* Change to screen coordinate for WM_CONTEXTMENU */
9889 pt = lvHitTestInfo.pt;
9890 ClientToScreen(infoPtr->hwndSelf, &pt);
9892 /* Send a WM_CONTEXTMENU message in response to the RBUTTONUP */
9893 SendMessageW(infoPtr->hwndSelf, WM_CONTEXTMENU,
9894 (WPARAM)infoPtr->hwndSelf, MAKELPARAM(pt.x, pt.y));
9896 return 0;
9900 /***
9901 * DESCRIPTION:
9902 * Sets the cursor.
9904 * PARAMETER(S):
9905 * [I] infoPtr : valid pointer to the listview structure
9906 * [I] hwnd : window handle of window containing the cursor
9907 * [I] nHittest : hit-test code
9908 * [I] wMouseMsg : ideintifier of the mouse message
9910 * RETURN:
9911 * TRUE if cursor is set
9912 * FALSE otherwise
9914 static BOOL LISTVIEW_SetCursor(const LISTVIEW_INFO *infoPtr, HWND hwnd, UINT nHittest, UINT wMouseMsg)
9916 LVHITTESTINFO lvHitTestInfo;
9918 if(!(LISTVIEW_isHotTracking(infoPtr))) return FALSE;
9920 if(!infoPtr->hHotCursor) return FALSE;
9922 GetCursorPos(&lvHitTestInfo.pt);
9923 if (LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, FALSE, FALSE) < 0) return FALSE;
9925 SetCursor(infoPtr->hHotCursor);
9927 return TRUE;
9930 /***
9931 * DESCRIPTION:
9932 * Sets the focus.
9934 * PARAMETER(S):
9935 * [I] infoPtr : valid pointer to the listview structure
9936 * [I] hwndLoseFocus : handle of previously focused window
9938 * RETURN:
9939 * Zero
9941 static LRESULT LISTVIEW_SetFocus(LISTVIEW_INFO *infoPtr, HWND hwndLoseFocus)
9943 TRACE("(hwndLoseFocus=%p)\n", hwndLoseFocus);
9945 /* if we have the focus already, there's nothing to do */
9946 if (infoPtr->bFocus) return 0;
9948 /* send NM_SETFOCUS notification */
9949 if (!notify(infoPtr, NM_SETFOCUS)) return 0;
9951 /* set window focus flag */
9952 infoPtr->bFocus = TRUE;
9954 /* put the focus rect back on */
9955 LISTVIEW_ShowFocusRect(infoPtr, TRUE);
9957 /* redraw all visible selected items */
9958 LISTVIEW_InvalidateSelectedItems(infoPtr);
9960 return 0;
9963 /***
9964 * DESCRIPTION:
9965 * Sets the font.
9967 * PARAMETER(S):
9968 * [I] infoPtr : valid pointer to the listview structure
9969 * [I] fRedraw : font handle
9970 * [I] fRedraw : redraw flag
9972 * RETURN:
9973 * Zero
9975 static LRESULT LISTVIEW_SetFont(LISTVIEW_INFO *infoPtr, HFONT hFont, WORD fRedraw)
9977 HFONT oldFont = infoPtr->hFont;
9979 TRACE("(hfont=%p,redraw=%hu)\n", hFont, fRedraw);
9981 infoPtr->hFont = hFont ? hFont : infoPtr->hDefaultFont;
9982 if (infoPtr->hFont == oldFont) return 0;
9984 LISTVIEW_SaveTextMetrics(infoPtr);
9986 if (infoPtr->uView == LV_VIEW_DETAILS)
9988 SendMessageW(infoPtr->hwndHeader, WM_SETFONT, (WPARAM)hFont, MAKELPARAM(fRedraw, 0));
9989 LISTVIEW_UpdateSize(infoPtr);
9990 LISTVIEW_UpdateScroll(infoPtr);
9993 if (fRedraw) LISTVIEW_InvalidateList(infoPtr);
9995 return 0;
9998 /***
9999 * DESCRIPTION:
10000 * Message handling for WM_SETREDRAW.
10001 * For the Listview, it invalidates the entire window (the doc specifies otherwise)
10003 * PARAMETER(S):
10004 * [I] infoPtr : valid pointer to the listview structure
10005 * [I] bRedraw: state of redraw flag
10007 * RETURN:
10008 * DefWinProc return value
10010 static LRESULT LISTVIEW_SetRedraw(LISTVIEW_INFO *infoPtr, BOOL bRedraw)
10012 TRACE("infoPtr->bRedraw=%d, bRedraw=%d\n", infoPtr->bRedraw, bRedraw);
10014 /* we cannot use straight equality here because _any_ non-zero value is TRUE */
10015 if ((infoPtr->bRedraw && bRedraw) || (!infoPtr->bRedraw && !bRedraw)) return 0;
10017 infoPtr->bRedraw = bRedraw;
10019 if(!bRedraw) return 0;
10021 if (is_autoarrange(infoPtr))
10022 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
10023 LISTVIEW_UpdateScroll(infoPtr);
10025 /* despite what the WM_SETREDRAW docs says, apps expect us
10026 * to invalidate the listview here... stupid! */
10027 LISTVIEW_InvalidateList(infoPtr);
10029 return 0;
10032 /***
10033 * DESCRIPTION:
10034 * Resizes the listview control. This function processes WM_SIZE
10035 * messages. At this time, the width and height are not used.
10037 * PARAMETER(S):
10038 * [I] infoPtr : valid pointer to the listview structure
10039 * [I] Width : new width
10040 * [I] Height : new height
10042 * RETURN:
10043 * Zero
10045 static LRESULT LISTVIEW_Size(LISTVIEW_INFO *infoPtr, int Width, int Height)
10047 RECT rcOld = infoPtr->rcList;
10049 TRACE("(width=%d, height=%d)\n", Width, Height);
10051 LISTVIEW_UpdateSize(infoPtr);
10052 if (EqualRect(&rcOld, &infoPtr->rcList)) return 0;
10054 /* do not bother with display related stuff if we're not redrawing */
10055 if (!is_redrawing(infoPtr)) return 0;
10057 if (is_autoarrange(infoPtr))
10058 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
10060 LISTVIEW_UpdateScroll(infoPtr);
10062 /* refresh all only for lists whose height changed significantly */
10063 if ((infoPtr->uView == LV_VIEW_LIST) &&
10064 (rcOld.bottom - rcOld.top) / infoPtr->nItemHeight !=
10065 (infoPtr->rcList.bottom - infoPtr->rcList.top) / infoPtr->nItemHeight)
10066 LISTVIEW_InvalidateList(infoPtr);
10068 return 0;
10071 /***
10072 * DESCRIPTION:
10073 * Sets the size information.
10075 * PARAMETER(S):
10076 * [I] infoPtr : valid pointer to the listview structure
10078 * RETURN:
10079 * None
10081 static void LISTVIEW_UpdateSize(LISTVIEW_INFO *infoPtr)
10083 TRACE("uView=%d, rcList(old)=%s\n", infoPtr->uView, wine_dbgstr_rect(&infoPtr->rcList));
10085 GetClientRect(infoPtr->hwndSelf, &infoPtr->rcList);
10087 if (infoPtr->uView == LV_VIEW_LIST)
10089 /* Apparently the "LIST" style is supposed to have the same
10090 * number of items in a column even if there is no scroll bar.
10091 * Since if a scroll bar already exists then the bottom is already
10092 * reduced, only reduce if the scroll bar does not currently exist.
10093 * The "2" is there to mimic the native control. I think it may be
10094 * related to either padding or edges. (GLA 7/2002)
10096 if (!(infoPtr->dwStyle & WS_HSCROLL))
10097 infoPtr->rcList.bottom -= GetSystemMetrics(SM_CYHSCROLL);
10098 infoPtr->rcList.bottom = max (infoPtr->rcList.bottom - 2, 0);
10100 else if (infoPtr->uView == LV_VIEW_DETAILS)
10102 HDLAYOUT hl;
10103 WINDOWPOS wp;
10105 hl.prc = &infoPtr->rcList;
10106 hl.pwpos = &wp;
10107 SendMessageW( infoPtr->hwndHeader, HDM_LAYOUT, 0, (LPARAM)&hl );
10108 TRACE(" wp.flags=0x%08x, wp=%d,%d (%dx%d)\n", wp.flags, wp.x, wp.y, wp.cx, wp.cy);
10109 SetWindowPos(wp.hwnd, wp.hwndInsertAfter, wp.x, wp.y, wp.cx, wp.cy,
10110 wp.flags | ((infoPtr->dwStyle & LVS_NOCOLUMNHEADER)
10111 ? SWP_HIDEWINDOW : SWP_SHOWWINDOW));
10112 TRACE(" after SWP wp=%d,%d (%dx%d)\n", wp.x, wp.y, wp.cx, wp.cy);
10114 infoPtr->rcList.top = max(wp.cy, 0);
10115 infoPtr->rcList.top += (infoPtr->dwLvExStyle & LVS_EX_GRIDLINES) ? 2 : 0;
10118 TRACE(" rcList=%s\n", wine_dbgstr_rect(&infoPtr->rcList));
10121 /***
10122 * DESCRIPTION:
10123 * Processes WM_STYLECHANGED messages.
10125 * PARAMETER(S):
10126 * [I] infoPtr : valid pointer to the listview structure
10127 * [I] wStyleType : window style type (normal or extended)
10128 * [I] lpss : window style information
10130 * RETURN:
10131 * Zero
10133 static INT LISTVIEW_StyleChanged(LISTVIEW_INFO *infoPtr, WPARAM wStyleType,
10134 const STYLESTRUCT *lpss)
10136 UINT uNewView = lpss->styleNew & LVS_TYPEMASK;
10137 UINT uOldView = lpss->styleOld & LVS_TYPEMASK;
10138 UINT style;
10140 TRACE("(styletype=%lx, styleOld=0x%08x, styleNew=0x%08x)\n",
10141 wStyleType, lpss->styleOld, lpss->styleNew);
10143 if (wStyleType != GWL_STYLE) return 0;
10145 infoPtr->dwStyle = lpss->styleNew;
10146 map_style_view(infoPtr);
10148 if (((lpss->styleOld & WS_HSCROLL) != 0)&&
10149 ((lpss->styleNew & WS_HSCROLL) == 0))
10150 ShowScrollBar(infoPtr->hwndSelf, SB_HORZ, FALSE);
10152 if (((lpss->styleOld & WS_VSCROLL) != 0)&&
10153 ((lpss->styleNew & WS_VSCROLL) == 0))
10154 ShowScrollBar(infoPtr->hwndSelf, SB_VERT, FALSE);
10156 if (uNewView != uOldView)
10158 SIZE oldIconSize = infoPtr->iconSize;
10159 HIMAGELIST himl;
10161 SendMessageW(infoPtr->hwndEdit, WM_KILLFOCUS, 0, 0);
10162 ShowWindow(infoPtr->hwndHeader, SW_HIDE);
10164 ShowScrollBar(infoPtr->hwndSelf, SB_BOTH, FALSE);
10165 SetRectEmpty(&infoPtr->rcFocus);
10167 himl = (uNewView == LVS_ICON ? infoPtr->himlNormal : infoPtr->himlSmall);
10168 set_icon_size(&infoPtr->iconSize, himl, uNewView != LVS_ICON);
10170 if (uNewView == LVS_ICON)
10172 if ((infoPtr->iconSize.cx != oldIconSize.cx) || (infoPtr->iconSize.cy != oldIconSize.cy))
10174 TRACE("icon old size=(%d,%d), new size=(%d,%d)\n",
10175 oldIconSize.cx, oldIconSize.cy, infoPtr->iconSize.cx, infoPtr->iconSize.cy);
10176 LISTVIEW_SetIconSpacing(infoPtr, 0, 0);
10179 else if (uNewView == LVS_REPORT)
10181 HDLAYOUT hl;
10182 WINDOWPOS wp;
10184 LISTVIEW_CreateHeader( infoPtr );
10186 hl.prc = &infoPtr->rcList;
10187 hl.pwpos = &wp;
10188 SendMessageW( infoPtr->hwndHeader, HDM_LAYOUT, 0, (LPARAM)&hl );
10189 SetWindowPos(infoPtr->hwndHeader, infoPtr->hwndSelf, wp.x, wp.y, wp.cx, wp.cy,
10190 wp.flags | ((infoPtr->dwStyle & LVS_NOCOLUMNHEADER)
10191 ? SWP_HIDEWINDOW : SWP_SHOWWINDOW));
10194 LISTVIEW_UpdateItemSize(infoPtr);
10197 if (uNewView == LVS_REPORT)
10199 if ((lpss->styleOld ^ lpss->styleNew) & LVS_NOCOLUMNHEADER)
10201 if (lpss->styleNew & LVS_NOCOLUMNHEADER)
10203 /* Turn off the header control */
10204 style = GetWindowLongW(infoPtr->hwndHeader, GWL_STYLE);
10205 TRACE("Hide header control, was 0x%08x\n", style);
10206 SetWindowLongW(infoPtr->hwndHeader, GWL_STYLE, style | HDS_HIDDEN);
10207 } else {
10208 /* Turn on the header control */
10209 if ((style = GetWindowLongW(infoPtr->hwndHeader, GWL_STYLE)) & HDS_HIDDEN)
10211 TRACE("Show header control, was 0x%08x\n", style);
10212 SetWindowLongW(infoPtr->hwndHeader, GWL_STYLE, (style & ~HDS_HIDDEN) | WS_VISIBLE);
10218 if ( (uNewView == LVS_ICON || uNewView == LVS_SMALLICON) &&
10219 (uNewView != uOldView || ((lpss->styleNew ^ lpss->styleOld) & LVS_ALIGNMASK)) )
10220 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
10222 /* update the size of the client area */
10223 LISTVIEW_UpdateSize(infoPtr);
10225 /* add scrollbars if needed */
10226 LISTVIEW_UpdateScroll(infoPtr);
10228 /* invalidate client area + erase background */
10229 LISTVIEW_InvalidateList(infoPtr);
10231 return 0;
10234 /***
10235 * DESCRIPTION:
10236 * Processes WM_STYLECHANGING messages.
10238 * PARAMETER(S):
10239 * [I] infoPtr : valid pointer to the listview structure
10240 * [I] wStyleType : window style type (normal or extended)
10241 * [I0] lpss : window style information
10243 * RETURN:
10244 * Zero
10246 static INT LISTVIEW_StyleChanging(LISTVIEW_INFO *infoPtr, WPARAM wStyleType,
10247 STYLESTRUCT *lpss)
10249 TRACE("(styletype=%lx, styleOld=0x%08x, styleNew=0x%08x)\n",
10250 wStyleType, lpss->styleOld, lpss->styleNew);
10252 /* don't forward LVS_OWNERDATA only if not already set to */
10253 if ((lpss->styleNew ^ lpss->styleOld) & LVS_OWNERDATA)
10255 if (lpss->styleOld & LVS_OWNERDATA)
10256 lpss->styleNew |= LVS_OWNERDATA;
10257 else
10258 lpss->styleNew &= ~LVS_OWNERDATA;
10261 return 0;
10264 /***
10265 * DESCRIPTION:
10266 * Processes WM_SHOWWINDOW messages.
10268 * PARAMETER(S):
10269 * [I] infoPtr : valid pointer to the listview structure
10270 * [I] bShown : window is being shown (FALSE when hidden)
10271 * [I] iStatus : window show status
10273 * RETURN:
10274 * Zero
10276 static LRESULT LISTVIEW_ShowWindow(LISTVIEW_INFO *infoPtr, BOOL bShown, INT iStatus)
10278 /* header delayed creation */
10279 if ((infoPtr->uView == LV_VIEW_DETAILS) && bShown)
10281 LISTVIEW_CreateHeader(infoPtr);
10283 if (!(LVS_NOCOLUMNHEADER & infoPtr->dwStyle))
10284 ShowWindow(infoPtr->hwndHeader, SW_SHOWNORMAL);
10287 return 0;
10290 /***
10291 * DESCRIPTION:
10292 * Processes CCM_GETVERSION messages.
10294 * PARAMETER(S):
10295 * [I] infoPtr : valid pointer to the listview structure
10297 * RETURN:
10298 * Current version
10300 static inline LRESULT LISTVIEW_GetVersion(const LISTVIEW_INFO *infoPtr)
10302 return infoPtr->iVersion;
10305 /***
10306 * DESCRIPTION:
10307 * Processes CCM_SETVERSION messages.
10309 * PARAMETER(S):
10310 * [I] infoPtr : valid pointer to the listview structure
10311 * [I] iVersion : version to be set
10313 * RETURN:
10314 * -1 when requested version is greater than DLL version;
10315 * previous version otherwise
10317 static LRESULT LISTVIEW_SetVersion(LISTVIEW_INFO *infoPtr, DWORD iVersion)
10319 INT iOldVersion = infoPtr->iVersion;
10321 if (iVersion > COMCTL32_VERSION)
10322 return -1;
10324 infoPtr->iVersion = iVersion;
10326 TRACE("new version %d\n", iVersion);
10328 return iOldVersion;
10331 /***
10332 * DESCRIPTION:
10333 * Window procedure of the listview control.
10336 static LRESULT WINAPI
10337 LISTVIEW_WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
10339 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)GetWindowLongPtrW(hwnd, 0);
10341 TRACE("(uMsg=%x wParam=%lx lParam=%lx)\n", uMsg, wParam, lParam);
10343 if (!infoPtr && (uMsg != WM_NCCREATE))
10344 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
10346 switch (uMsg)
10348 case LVM_APPROXIMATEVIEWRECT:
10349 return LISTVIEW_ApproximateViewRect(infoPtr, (INT)wParam,
10350 LOWORD(lParam), HIWORD(lParam));
10351 case LVM_ARRANGE:
10352 return LISTVIEW_Arrange(infoPtr, (INT)wParam);
10354 case LVM_CANCELEDITLABEL:
10355 return LISTVIEW_CancelEditLabel(infoPtr);
10357 case LVM_CREATEDRAGIMAGE:
10358 return (LRESULT)LISTVIEW_CreateDragImage(infoPtr, (INT)wParam, (LPPOINT)lParam);
10360 case LVM_DELETEALLITEMS:
10361 return LISTVIEW_DeleteAllItems(infoPtr, FALSE);
10363 case LVM_DELETECOLUMN:
10364 return LISTVIEW_DeleteColumn(infoPtr, (INT)wParam);
10366 case LVM_DELETEITEM:
10367 return LISTVIEW_DeleteItem(infoPtr, (INT)wParam);
10369 case LVM_EDITLABELW:
10370 return (LRESULT)LISTVIEW_EditLabelT(infoPtr, (INT)wParam, TRUE);
10372 case LVM_EDITLABELA:
10373 return (LRESULT)LISTVIEW_EditLabelT(infoPtr, (INT)wParam, FALSE);
10375 /* case LVM_ENABLEGROUPVIEW: */
10377 case LVM_ENSUREVISIBLE:
10378 return LISTVIEW_EnsureVisible(infoPtr, (INT)wParam, (BOOL)lParam);
10380 case LVM_FINDITEMW:
10381 return LISTVIEW_FindItemW(infoPtr, (INT)wParam, (LPLVFINDINFOW)lParam);
10383 case LVM_FINDITEMA:
10384 return LISTVIEW_FindItemA(infoPtr, (INT)wParam, (LPLVFINDINFOA)lParam);
10386 case LVM_GETBKCOLOR:
10387 return infoPtr->clrBk;
10389 /* case LVM_GETBKIMAGE: */
10391 case LVM_GETCALLBACKMASK:
10392 return infoPtr->uCallbackMask;
10394 case LVM_GETCOLUMNA:
10395 return LISTVIEW_GetColumnT(infoPtr, (INT)wParam, (LPLVCOLUMNW)lParam, FALSE);
10397 case LVM_GETCOLUMNW:
10398 return LISTVIEW_GetColumnT(infoPtr, (INT)wParam, (LPLVCOLUMNW)lParam, TRUE);
10400 case LVM_GETCOLUMNORDERARRAY:
10401 return LISTVIEW_GetColumnOrderArray(infoPtr, (INT)wParam, (LPINT)lParam);
10403 case LVM_GETCOLUMNWIDTH:
10404 return LISTVIEW_GetColumnWidth(infoPtr, (INT)wParam);
10406 case LVM_GETCOUNTPERPAGE:
10407 return LISTVIEW_GetCountPerPage(infoPtr);
10409 case LVM_GETEDITCONTROL:
10410 return (LRESULT)infoPtr->hwndEdit;
10412 case LVM_GETEXTENDEDLISTVIEWSTYLE:
10413 return infoPtr->dwLvExStyle;
10415 /* case LVM_GETGROUPINFO: */
10417 /* case LVM_GETGROUPMETRICS: */
10419 case LVM_GETHEADER:
10420 return (LRESULT)infoPtr->hwndHeader;
10422 case LVM_GETHOTCURSOR:
10423 return (LRESULT)infoPtr->hHotCursor;
10425 case LVM_GETHOTITEM:
10426 return infoPtr->nHotItem;
10428 case LVM_GETHOVERTIME:
10429 return infoPtr->dwHoverTime;
10431 case LVM_GETIMAGELIST:
10432 return (LRESULT)LISTVIEW_GetImageList(infoPtr, (INT)wParam);
10434 /* case LVM_GETINSERTMARK: */
10436 /* case LVM_GETINSERTMARKCOLOR: */
10438 /* case LVM_GETINSERTMARKRECT: */
10440 case LVM_GETISEARCHSTRINGA:
10441 case LVM_GETISEARCHSTRINGW:
10442 FIXME("LVM_GETISEARCHSTRING: unimplemented\n");
10443 return FALSE;
10445 case LVM_GETITEMA:
10446 return LISTVIEW_GetItemExtT(infoPtr, (LPLVITEMW)lParam, FALSE);
10448 case LVM_GETITEMW:
10449 return LISTVIEW_GetItemExtT(infoPtr, (LPLVITEMW)lParam, TRUE);
10451 case LVM_GETITEMCOUNT:
10452 return infoPtr->nItemCount;
10454 case LVM_GETITEMPOSITION:
10455 return LISTVIEW_GetItemPosition(infoPtr, (INT)wParam, (LPPOINT)lParam);
10457 case LVM_GETITEMRECT:
10458 return LISTVIEW_GetItemRect(infoPtr, (INT)wParam, (LPRECT)lParam);
10460 case LVM_GETITEMSPACING:
10461 return LISTVIEW_GetItemSpacing(infoPtr, (BOOL)wParam);
10463 case LVM_GETITEMSTATE:
10464 return LISTVIEW_GetItemState(infoPtr, (INT)wParam, (UINT)lParam);
10466 case LVM_GETITEMTEXTA:
10467 return LISTVIEW_GetItemTextT(infoPtr, (INT)wParam, (LPLVITEMW)lParam, FALSE);
10469 case LVM_GETITEMTEXTW:
10470 return LISTVIEW_GetItemTextT(infoPtr, (INT)wParam, (LPLVITEMW)lParam, TRUE);
10472 case LVM_GETNEXTITEM:
10473 return LISTVIEW_GetNextItem(infoPtr, (INT)wParam, LOWORD(lParam));
10475 case LVM_GETNUMBEROFWORKAREAS:
10476 FIXME("LVM_GETNUMBEROFWORKAREAS: unimplemented\n");
10477 return 1;
10479 case LVM_GETORIGIN:
10480 if (!lParam) return FALSE;
10481 if (infoPtr->uView == LV_VIEW_DETAILS ||
10482 infoPtr->uView == LV_VIEW_LIST) return FALSE;
10483 LISTVIEW_GetOrigin(infoPtr, (LPPOINT)lParam);
10484 return TRUE;
10486 /* case LVM_GETOUTLINECOLOR: */
10488 /* case LVM_GETSELECTEDCOLUMN: */
10490 case LVM_GETSELECTEDCOUNT:
10491 return LISTVIEW_GetSelectedCount(infoPtr);
10493 case LVM_GETSELECTIONMARK:
10494 return infoPtr->nSelectionMark;
10496 case LVM_GETSTRINGWIDTHA:
10497 return LISTVIEW_GetStringWidthT(infoPtr, (LPCWSTR)lParam, FALSE);
10499 case LVM_GETSTRINGWIDTHW:
10500 return LISTVIEW_GetStringWidthT(infoPtr, (LPCWSTR)lParam, TRUE);
10502 case LVM_GETSUBITEMRECT:
10503 return LISTVIEW_GetSubItemRect(infoPtr, (UINT)wParam, (LPRECT)lParam);
10505 case LVM_GETTEXTBKCOLOR:
10506 return infoPtr->clrTextBk;
10508 case LVM_GETTEXTCOLOR:
10509 return infoPtr->clrText;
10511 /* case LVM_GETTILEINFO: */
10513 /* case LVM_GETTILEVIEWINFO: */
10515 case LVM_GETTOOLTIPS:
10516 if( !infoPtr->hwndToolTip )
10517 infoPtr->hwndToolTip = COMCTL32_CreateToolTip( hwnd );
10518 return (LRESULT)infoPtr->hwndToolTip;
10520 case LVM_GETTOPINDEX:
10521 return LISTVIEW_GetTopIndex(infoPtr);
10523 case LVM_GETUNICODEFORMAT:
10524 return (infoPtr->notifyFormat == NFR_UNICODE);
10526 case LVM_GETVIEW:
10527 return infoPtr->uView;
10529 case LVM_GETVIEWRECT:
10530 return LISTVIEW_GetViewRect(infoPtr, (LPRECT)lParam);
10532 case LVM_GETWORKAREAS:
10533 FIXME("LVM_GETWORKAREAS: unimplemented\n");
10534 return FALSE;
10536 /* case LVM_HASGROUP: */
10538 case LVM_HITTEST:
10539 return LISTVIEW_HitTest(infoPtr, (LPLVHITTESTINFO)lParam, FALSE, TRUE);
10541 case LVM_INSERTCOLUMNA:
10542 return LISTVIEW_InsertColumnT(infoPtr, (INT)wParam, (LPLVCOLUMNW)lParam, FALSE);
10544 case LVM_INSERTCOLUMNW:
10545 return LISTVIEW_InsertColumnT(infoPtr, (INT)wParam, (LPLVCOLUMNW)lParam, TRUE);
10547 /* case LVM_INSERTGROUP: */
10549 /* case LVM_INSERTGROUPSORTED: */
10551 case LVM_INSERTITEMA:
10552 return LISTVIEW_InsertItemT(infoPtr, (LPLVITEMW)lParam, FALSE);
10554 case LVM_INSERTITEMW:
10555 return LISTVIEW_InsertItemT(infoPtr, (LPLVITEMW)lParam, TRUE);
10557 /* case LVM_INSERTMARKHITTEST: */
10559 /* case LVM_ISGROUPVIEWENABLED: */
10561 case LVM_ISITEMVISIBLE:
10562 return LISTVIEW_IsItemVisible(infoPtr, (INT)wParam);
10564 case LVM_MAPIDTOINDEX:
10565 return LISTVIEW_MapIdToIndex(infoPtr, (UINT)wParam);
10567 case LVM_MAPINDEXTOID:
10568 return LISTVIEW_MapIndexToId(infoPtr, (INT)wParam);
10570 /* case LVM_MOVEGROUP: */
10572 /* case LVM_MOVEITEMTOGROUP: */
10574 case LVM_REDRAWITEMS:
10575 return LISTVIEW_RedrawItems(infoPtr, (INT)wParam, (INT)lParam);
10577 /* case LVM_REMOVEALLGROUPS: */
10579 /* case LVM_REMOVEGROUP: */
10581 case LVM_SCROLL:
10582 return LISTVIEW_Scroll(infoPtr, (INT)wParam, (INT)lParam);
10584 case LVM_SETBKCOLOR:
10585 return LISTVIEW_SetBkColor(infoPtr, (COLORREF)lParam);
10587 /* case LVM_SETBKIMAGE: */
10589 case LVM_SETCALLBACKMASK:
10590 infoPtr->uCallbackMask = (UINT)wParam;
10591 return TRUE;
10593 case LVM_SETCOLUMNA:
10594 return LISTVIEW_SetColumnT(infoPtr, (INT)wParam, (LPLVCOLUMNW)lParam, FALSE);
10596 case LVM_SETCOLUMNW:
10597 return LISTVIEW_SetColumnT(infoPtr, (INT)wParam, (LPLVCOLUMNW)lParam, TRUE);
10599 case LVM_SETCOLUMNORDERARRAY:
10600 return LISTVIEW_SetColumnOrderArray(infoPtr, (INT)wParam, (LPINT)lParam);
10602 case LVM_SETCOLUMNWIDTH:
10603 return LISTVIEW_SetColumnWidth(infoPtr, (INT)wParam, (short)LOWORD(lParam));
10605 case LVM_SETEXTENDEDLISTVIEWSTYLE:
10606 return LISTVIEW_SetExtendedListViewStyle(infoPtr, (DWORD)wParam, (DWORD)lParam);
10608 /* case LVM_SETGROUPINFO: */
10610 /* case LVM_SETGROUPMETRICS: */
10612 case LVM_SETHOTCURSOR:
10613 return (LRESULT)LISTVIEW_SetHotCursor(infoPtr, (HCURSOR)lParam);
10615 case LVM_SETHOTITEM:
10616 return LISTVIEW_SetHotItem(infoPtr, (INT)wParam);
10618 case LVM_SETHOVERTIME:
10619 return LISTVIEW_SetHoverTime(infoPtr, (DWORD)wParam);
10621 case LVM_SETICONSPACING:
10622 return LISTVIEW_SetIconSpacing(infoPtr, (short)LOWORD(lParam), (short)HIWORD(lParam));
10624 case LVM_SETIMAGELIST:
10625 return (LRESULT)LISTVIEW_SetImageList(infoPtr, (INT)wParam, (HIMAGELIST)lParam);
10627 /* case LVM_SETINFOTIP: */
10629 /* case LVM_SETINSERTMARK: */
10631 /* case LVM_SETINSERTMARKCOLOR: */
10633 case LVM_SETITEMA:
10634 case LVM_SETITEMW:
10636 if (infoPtr->dwStyle & LVS_OWNERDATA) return FALSE;
10637 return LISTVIEW_SetItemT(infoPtr, (LPLVITEMW)lParam, (uMsg == LVM_SETITEMW));
10640 case LVM_SETITEMCOUNT:
10641 return LISTVIEW_SetItemCount(infoPtr, (INT)wParam, (DWORD)lParam);
10643 case LVM_SETITEMPOSITION:
10645 POINT pt;
10646 pt.x = (short)LOWORD(lParam);
10647 pt.y = (short)HIWORD(lParam);
10648 return LISTVIEW_SetItemPosition(infoPtr, (INT)wParam, pt);
10651 case LVM_SETITEMPOSITION32:
10652 if (lParam == 0) return FALSE;
10653 return LISTVIEW_SetItemPosition(infoPtr, (INT)wParam, *((POINT*)lParam));
10655 case LVM_SETITEMSTATE:
10656 return LISTVIEW_SetItemState(infoPtr, (INT)wParam, (LPLVITEMW)lParam);
10658 case LVM_SETITEMTEXTA:
10659 return LISTVIEW_SetItemTextT(infoPtr, (INT)wParam, (LPLVITEMW)lParam, FALSE);
10661 case LVM_SETITEMTEXTW:
10662 return LISTVIEW_SetItemTextT(infoPtr, (INT)wParam, (LPLVITEMW)lParam, TRUE);
10664 /* case LVM_SETOUTLINECOLOR: */
10666 /* case LVM_SETSELECTEDCOLUMN: */
10668 case LVM_SETSELECTIONMARK:
10669 return LISTVIEW_SetSelectionMark(infoPtr, (INT)lParam);
10671 case LVM_SETTEXTBKCOLOR:
10672 return LISTVIEW_SetTextBkColor(infoPtr, (COLORREF)lParam);
10674 case LVM_SETTEXTCOLOR:
10675 return LISTVIEW_SetTextColor(infoPtr, (COLORREF)lParam);
10677 /* case LVM_SETTILEINFO: */
10679 /* case LVM_SETTILEVIEWINFO: */
10681 /* case LVM_SETTILEWIDTH: */
10683 case LVM_SETTOOLTIPS:
10684 return (LRESULT)LISTVIEW_SetToolTips(infoPtr, (HWND)lParam);
10686 case LVM_SETUNICODEFORMAT:
10687 return LISTVIEW_SetUnicodeFormat(infoPtr, wParam);
10689 case LVM_SETVIEW:
10690 return LISTVIEW_SetView(infoPtr, wParam);
10692 /* case LVM_SETWORKAREAS: */
10694 /* case LVM_SORTGROUPS: */
10696 case LVM_SORTITEMS:
10697 return LISTVIEW_SortItems(infoPtr, (PFNLVCOMPARE)lParam, (LPARAM)wParam, FALSE);
10699 case LVM_SORTITEMSEX:
10700 return LISTVIEW_SortItems(infoPtr, (PFNLVCOMPARE)lParam, (LPARAM)wParam, TRUE);
10702 case LVM_SUBITEMHITTEST:
10703 return LISTVIEW_HitTest(infoPtr, (LPLVHITTESTINFO)lParam, TRUE, FALSE);
10705 case LVM_UPDATE:
10706 return LISTVIEW_Update(infoPtr, (INT)wParam);
10708 case CCM_GETVERSION:
10709 return LISTVIEW_GetVersion(infoPtr);
10711 case CCM_SETVERSION:
10712 return LISTVIEW_SetVersion(infoPtr, wParam);
10714 case WM_CHAR:
10715 return LISTVIEW_ProcessLetterKeys( infoPtr, wParam, lParam );
10717 case WM_COMMAND:
10718 return LISTVIEW_Command(infoPtr, wParam, lParam);
10720 case WM_NCCREATE:
10721 return LISTVIEW_NCCreate(hwnd, (LPCREATESTRUCTW)lParam);
10723 case WM_CREATE:
10724 return LISTVIEW_Create(hwnd, (LPCREATESTRUCTW)lParam);
10726 case WM_DESTROY:
10727 return LISTVIEW_Destroy(infoPtr);
10729 case WM_ENABLE:
10730 return LISTVIEW_Enable(infoPtr, (BOOL)wParam);
10732 case WM_ERASEBKGND:
10733 return LISTVIEW_EraseBkgnd(infoPtr, (HDC)wParam);
10735 case WM_GETDLGCODE:
10736 return DLGC_WANTCHARS | DLGC_WANTARROWS;
10738 case WM_GETFONT:
10739 return (LRESULT)infoPtr->hFont;
10741 case WM_HSCROLL:
10742 return LISTVIEW_HScroll(infoPtr, (INT)LOWORD(wParam), 0, (HWND)lParam);
10744 case WM_KEYDOWN:
10745 return LISTVIEW_KeyDown(infoPtr, (INT)wParam, (LONG)lParam);
10747 case WM_KILLFOCUS:
10748 return LISTVIEW_KillFocus(infoPtr);
10750 case WM_LBUTTONDBLCLK:
10751 return LISTVIEW_LButtonDblClk(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
10753 case WM_LBUTTONDOWN:
10754 return LISTVIEW_LButtonDown(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
10756 case WM_LBUTTONUP:
10757 return LISTVIEW_LButtonUp(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
10759 case WM_MOUSEMOVE:
10760 return LISTVIEW_MouseMove (infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
10762 case WM_MOUSEHOVER:
10763 return LISTVIEW_MouseHover(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
10765 case WM_NCDESTROY:
10766 return LISTVIEW_NCDestroy(infoPtr);
10768 case WM_NCPAINT:
10769 if (LISTVIEW_NCPaint(infoPtr, (HRGN)wParam))
10770 return 0;
10771 goto fwd_msg;
10773 case WM_NOTIFY:
10774 if (lParam && ((LPNMHDR)lParam)->hwndFrom == infoPtr->hwndHeader)
10775 return LISTVIEW_HeaderNotification(infoPtr, (LPNMHEADERW)lParam);
10776 else return 0;
10778 case WM_NOTIFYFORMAT:
10779 return LISTVIEW_NotifyFormat(infoPtr, (HWND)wParam, (INT)lParam);
10781 case WM_PRINTCLIENT:
10782 return LISTVIEW_PrintClient(infoPtr, (HDC)wParam, (DWORD)lParam);
10784 case WM_PAINT:
10785 return LISTVIEW_Paint(infoPtr, (HDC)wParam);
10787 case WM_RBUTTONDBLCLK:
10788 return LISTVIEW_RButtonDblClk(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
10790 case WM_RBUTTONDOWN:
10791 return LISTVIEW_RButtonDown(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
10793 case WM_RBUTTONUP:
10794 return LISTVIEW_RButtonUp(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
10796 case WM_SETCURSOR:
10797 if(LISTVIEW_SetCursor(infoPtr, (HWND)wParam, LOWORD(lParam), HIWORD(lParam)))
10798 return TRUE;
10799 goto fwd_msg;
10801 case WM_SETFOCUS:
10802 return LISTVIEW_SetFocus(infoPtr, (HWND)wParam);
10804 case WM_SETFONT:
10805 return LISTVIEW_SetFont(infoPtr, (HFONT)wParam, (WORD)lParam);
10807 case WM_SETREDRAW:
10808 return LISTVIEW_SetRedraw(infoPtr, (BOOL)wParam);
10810 case WM_SHOWWINDOW:
10811 LISTVIEW_ShowWindow(infoPtr, (BOOL)wParam, (INT)lParam);
10812 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
10814 case WM_SIZE:
10815 return LISTVIEW_Size(infoPtr, (short)LOWORD(lParam), (short)HIWORD(lParam));
10817 case WM_STYLECHANGED:
10818 return LISTVIEW_StyleChanged(infoPtr, wParam, (LPSTYLESTRUCT)lParam);
10820 case WM_STYLECHANGING:
10821 return LISTVIEW_StyleChanging(infoPtr, wParam, (LPSTYLESTRUCT)lParam);
10823 case WM_SYSCOLORCHANGE:
10824 COMCTL32_RefreshSysColors();
10825 return 0;
10827 /* case WM_TIMER: */
10828 case WM_THEMECHANGED:
10829 return LISTVIEW_ThemeChanged(infoPtr);
10831 case WM_VSCROLL:
10832 return LISTVIEW_VScroll(infoPtr, (INT)LOWORD(wParam), 0, (HWND)lParam);
10834 case WM_MOUSEWHEEL:
10835 if (wParam & (MK_SHIFT | MK_CONTROL))
10836 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
10837 return LISTVIEW_MouseWheel(infoPtr, (short int)HIWORD(wParam));
10839 case WM_WINDOWPOSCHANGED:
10840 if (!(((WINDOWPOS *)lParam)->flags & SWP_NOSIZE))
10842 SetWindowPos(infoPtr->hwndSelf, 0, 0, 0, 0, 0, SWP_FRAMECHANGED | SWP_NOACTIVATE |
10843 SWP_NOZORDER | SWP_NOMOVE | SWP_NOSIZE);
10845 if ((infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && (infoPtr->uView == LV_VIEW_DETAILS))
10847 if (notify_measureitem(infoPtr)) LISTVIEW_InvalidateList(infoPtr);
10850 LISTVIEW_UpdateSize(infoPtr);
10851 LISTVIEW_UpdateScroll(infoPtr);
10853 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
10855 /* case WM_WININICHANGE: */
10857 default:
10858 if ((uMsg >= WM_USER) && (uMsg < WM_APP) && !COMCTL32_IsReflectedMessage(uMsg))
10859 ERR("unknown msg %04x wp=%08lx lp=%08lx\n", uMsg, wParam, lParam);
10861 fwd_msg:
10862 /* call default window procedure */
10863 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
10868 /***
10869 * DESCRIPTION:
10870 * Registers the window class.
10872 * PARAMETER(S):
10873 * None
10875 * RETURN:
10876 * None
10878 void LISTVIEW_Register(void)
10880 WNDCLASSW wndClass;
10882 ZeroMemory(&wndClass, sizeof(WNDCLASSW));
10883 wndClass.style = CS_GLOBALCLASS | CS_DBLCLKS;
10884 wndClass.lpfnWndProc = LISTVIEW_WindowProc;
10885 wndClass.cbClsExtra = 0;
10886 wndClass.cbWndExtra = sizeof(LISTVIEW_INFO *);
10887 wndClass.hCursor = LoadCursorW(0, (LPWSTR)IDC_ARROW);
10888 wndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
10889 wndClass.lpszClassName = WC_LISTVIEWW;
10890 RegisterClassW(&wndClass);
10893 /***
10894 * DESCRIPTION:
10895 * Unregisters the window class.
10897 * PARAMETER(S):
10898 * None
10900 * RETURN:
10901 * None
10903 void LISTVIEW_Unregister(void)
10905 UnregisterClassW(WC_LISTVIEWW, NULL);
10908 /***
10909 * DESCRIPTION:
10910 * Handle any WM_COMMAND messages
10912 * PARAMETER(S):
10913 * [I] infoPtr : valid pointer to the listview structure
10914 * [I] wParam : the first message parameter
10915 * [I] lParam : the second message parameter
10917 * RETURN:
10918 * Zero.
10920 static LRESULT LISTVIEW_Command(LISTVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
10922 switch (HIWORD(wParam))
10924 case EN_UPDATE:
10927 * Adjust the edit window size
10929 WCHAR buffer[1024];
10930 HDC hdc = GetDC(infoPtr->hwndEdit);
10931 HFONT hFont, hOldFont = 0;
10932 RECT rect;
10933 SIZE sz;
10935 if (!infoPtr->hwndEdit || !hdc) return 0;
10936 GetWindowTextW(infoPtr->hwndEdit, buffer, sizeof(buffer)/sizeof(buffer[0]));
10937 GetWindowRect(infoPtr->hwndEdit, &rect);
10939 /* Select font to get the right dimension of the string */
10940 hFont = (HFONT)SendMessageW(infoPtr->hwndEdit, WM_GETFONT, 0, 0);
10941 if(hFont != 0)
10943 hOldFont = SelectObject(hdc, hFont);
10946 if (GetTextExtentPoint32W(hdc, buffer, lstrlenW(buffer), &sz))
10948 TEXTMETRICW textMetric;
10950 /* Add Extra spacing for the next character */
10951 GetTextMetricsW(hdc, &textMetric);
10952 sz.cx += (textMetric.tmMaxCharWidth * 2);
10954 SetWindowPos (
10955 infoPtr->hwndEdit,
10956 HWND_TOP,
10959 sz.cx,
10960 rect.bottom - rect.top,
10961 SWP_DRAWFRAME|SWP_NOMOVE);
10963 if(hFont != 0)
10964 SelectObject(hdc, hOldFont);
10966 ReleaseDC(infoPtr->hwndEdit, hdc);
10968 break;
10970 case EN_KILLFOCUS:
10972 LISTVIEW_CancelEditLabel(infoPtr);
10975 default:
10976 return SendMessageW (infoPtr->hwndNotify, WM_COMMAND, wParam, lParam);
10979 return 0;
10983 /***
10984 * DESCRIPTION:
10985 * Subclassed edit control windproc function
10987 * PARAMETER(S):
10988 * [I] hwnd : the edit window handle
10989 * [I] uMsg : the message that is to be processed
10990 * [I] wParam : first message parameter
10991 * [I] lParam : second message parameter
10992 * [I] isW : TRUE if input is Unicode
10994 * RETURN:
10995 * Zero.
10997 static LRESULT EditLblWndProcT(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL isW)
10999 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)GetWindowLongPtrW(GetParent(hwnd), 0);
11000 BOOL save = TRUE;
11002 TRACE("(hwnd=%p, uMsg=%x, wParam=%lx, lParam=%lx, isW=%d)\n",
11003 hwnd, uMsg, wParam, lParam, isW);
11005 switch (uMsg)
11007 case WM_GETDLGCODE:
11008 return DLGC_WANTARROWS | DLGC_WANTALLKEYS;
11010 case WM_DESTROY:
11012 WNDPROC editProc = infoPtr->EditWndProc;
11013 infoPtr->EditWndProc = 0;
11014 SetWindowLongPtrW(hwnd, GWLP_WNDPROC, (DWORD_PTR)editProc);
11015 return CallWindowProcT(editProc, hwnd, uMsg, wParam, lParam, isW);
11018 case WM_KEYDOWN:
11019 if (VK_ESCAPE == (INT)wParam)
11021 save = FALSE;
11022 break;
11024 else if (VK_RETURN == (INT)wParam)
11025 break;
11027 default:
11028 return CallWindowProcT(infoPtr->EditWndProc, hwnd, uMsg, wParam, lParam, isW);
11031 /* kill the edit */
11032 if (infoPtr->hwndEdit)
11033 LISTVIEW_EndEditLabelT(infoPtr, save, isW);
11035 SendMessageW(hwnd, WM_CLOSE, 0, 0);
11036 return 0;
11039 /***
11040 * DESCRIPTION:
11041 * Subclassed edit control Unicode windproc function
11043 * PARAMETER(S):
11044 * [I] hwnd : the edit window handle
11045 * [I] uMsg : the message that is to be processed
11046 * [I] wParam : first message parameter
11047 * [I] lParam : second message parameter
11049 * RETURN:
11051 static LRESULT CALLBACK EditLblWndProcW(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
11053 return EditLblWndProcT(hwnd, uMsg, wParam, lParam, TRUE);
11056 /***
11057 * DESCRIPTION:
11058 * Subclassed edit control ANSI windproc function
11060 * PARAMETER(S):
11061 * [I] hwnd : the edit window handle
11062 * [I] uMsg : the message that is to be processed
11063 * [I] wParam : first message parameter
11064 * [I] lParam : second message parameter
11066 * RETURN:
11068 static LRESULT CALLBACK EditLblWndProcA(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
11070 return EditLblWndProcT(hwnd, uMsg, wParam, lParam, FALSE);
11073 /***
11074 * DESCRIPTION:
11075 * Creates a subclassed edit control
11077 * PARAMETER(S):
11078 * [I] infoPtr : valid pointer to the listview structure
11079 * [I] text : initial text for the edit
11080 * [I] style : the window style
11081 * [I] isW : TRUE if input is Unicode
11083 * RETURN:
11085 static HWND CreateEditLabelT(LISTVIEW_INFO *infoPtr, LPCWSTR text, DWORD style, BOOL isW)
11087 WCHAR editName[5] = { 'E', 'd', 'i', 't', '\0' };
11088 HWND hedit;
11089 HINSTANCE hinst = (HINSTANCE)GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_HINSTANCE);
11091 TRACE("(text=%s, ..., isW=%d)\n", debugtext_t(text, isW), isW);
11093 style |= WS_CHILDWINDOW|WS_CLIPSIBLINGS|ES_LEFT|ES_AUTOHSCROLL|WS_BORDER;
11095 /* Window will be resized and positioned after LVN_BEGINLABELEDIT */
11096 if (isW)
11097 hedit = CreateWindowW(editName, text, style, 0, 0, 0, 0, infoPtr->hwndSelf, 0, hinst, 0);
11098 else
11099 hedit = CreateWindowA("Edit", (LPCSTR)text, style, 0, 0, 0, 0, infoPtr->hwndSelf, 0, hinst, 0);
11101 if (!hedit) return 0;
11103 infoPtr->EditWndProc = (WNDPROC)
11104 (isW ? SetWindowLongPtrW(hedit, GWLP_WNDPROC, (DWORD_PTR)EditLblWndProcW) :
11105 SetWindowLongPtrA(hedit, GWLP_WNDPROC, (DWORD_PTR)EditLblWndProcA) );
11107 SendMessageW(hedit, WM_SETFONT, (WPARAM)infoPtr->hFont, FALSE);
11109 return hedit;