Setting the horizontal scrollbar can change the values for the
[wine/multimedia.git] / dlls / comctl32 / listview.c
blob4ebfa51a7d883f4a2097a5cbcec03e09b95a30ee
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
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 * NOTES
26 * This code was audited for completeness against the documented features
27 * of Comctl32.dll version 6.0 on Oct. 21, 2002, by Dimitrie O. Paun.
29 * Unless otherwise noted, we belive this code to be complete, as per
30 * the specification mentioned above.
31 * If you discover missing features, or bugs, please note them below.
33 * TODO:
35 * Features
36 * -- Hot item handling, mouse hovering
37 * -- Workareas support
38 * -- Tilemode support
39 * -- Groups support
41 * Bugs
42 * -- Expand large item in ICON mode when the cursor is flying over the icon or text.
43 * -- Support CustonDraw options for _WIN32_IE >= 0x560 (see NMLVCUSTOMDRAW docs.
44 * -- in LISTVIEW_AddGroupSelection, se whould send LVN_ODSTATECHANGED
45 * -- LVA_SNAPTOGRID not implemented
46 * -- LISTVIEW_ApproximateViewRect partially implemented
47 * -- LISTVIEW_[GS]etColumnOrderArray stubs
48 * -- LISTVIEW_GetNextItem is very inefficient
49 * -- LISTVIEW_SetColumnWidth ignores header images & bitmap
50 * -- LISTVIEW_SetIconSpacing is incomplete
51 * -- LISTVIEW_SortItems is broken
52 * -- LISTVIEW_StyleChanged doesn't handle some changes too well
54 * Speedups
55 * -- in sorted mode, LISTVIEW_InsertItemT sorts the array,
56 * instead of inserting in the right spot
57 * -- we should keep an ordered array of coordinates in iconic mode
58 * this would allow to frame items (iterator_frameditems),
59 * and find nearest item (LVFI_NEARESTXY) a lot more efficiently
61 * Flags
62 * -- LVIF_COLUMNS
63 * -- LVIF_GROUPID
64 * -- LVIF_NORECOMPUTE
66 * States
67 * -- LVIS_ACTIVATING (not currently supported by comctl32.dll version 6.0)
68 * -- LVIS_CUT
69 * -- LVIS_DROPHILITED
70 * -- LVIS_OVERLAYMASK
72 * Styles
73 * -- LVS_NOLABELWRAP
74 * -- LVS_NOSCROLL (see Q137520)
75 * -- LVS_SORTASCENDING, LVS_SORTDESCENDING
77 * Extended Styles
78 * -- LVS_EX_BORDERSELECT
79 * -- LVS_EX_CHECKBOXES
80 * -- LVS_EX_FLATSB
81 * -- LVS_EX_GRIDLINES
82 * -- LVS_EX_HEADERDRAGDROP
83 * -- LVS_EX_INFOTIP
84 * -- LVS_EX_LABELTIP
85 * -- LVS_EX_MULTIWORKAREAS
86 * -- LVS_EX_ONECLICKACTIVATE
87 * -- LVS_EX_REGIONAL
88 * -- LVS_EX_SIMPLESELECT
89 * -- LVS_EX_SUBITEMIMAGES
90 * -- LVS_EX_TRACKSELECT
91 * -- LVS_EX_TWOCLICKACTIVATE
92 * -- LVS_EX_UNDERLINECOLD
93 * -- LVS_EX_UNDERLINEHOT
95 * Notifications:
96 * -- LVN_BEGINDRAG, LVN_BEGINRDRAG
97 * -- LVN_BEGINSCROLL, LVN_ENDSCROLL
98 * -- LVN_GETINFOTIP
99 * -- LVN_HOTTRACK
100 * -- LVN_MARQUEEBEGIN
101 * -- LVN_ODFINDITEM
102 * -- LVN_ODSTATECHANGED
103 * -- LVN_SETDISPINFO
104 * -- NM_HOVER
106 * Messages:
107 * -- LVM_CANCELEDITLABEL
108 * -- LVM_CREATEDRAGIMAGE
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_GETTOOLTIPS, LVM_SETTOOLTIPS
123 * -- LVM_GETUNICODEFORMAT, LVM_SETUNICODEFORMAT
124 * -- LVM_GETVIEW, LVM_SETVIEW
125 * -- LVM_GETWORKAREAS, LVM_SETWORKAREAS
126 * -- LVM_HASGROUP, LVM_INSERTGROUP, LVM_REMOVEGROUP, LVM_REMOVEALLGROUPS
127 * -- LVM_INSERTGROUPSORTED
128 * -- LVM_INSERTMARKHITTEST
129 * -- LVM_ISGROUPVIEWENABLED
130 * -- LVM_MAPIDTOINDEX, LVM_MAPINDEXTOID
131 * -- LVM_MOVEGROUP
132 * -- LVM_MOVEITEMTOGROUP
133 * -- LVM_SETINFOTIP
134 * -- LVM_SETTILEWIDTH
135 * -- LVM_SORTGROUPS
136 * -- LVM_SORTITEMSEX
138 * Known differences in message stream from native control (not known if
139 * these differences cause problems):
140 * LVM_INSERTITEM issues LVM_SETITEMSTATE and LVM_SETITEM in certain cases.
141 * LVM_SETITEM does not always issue LVN_ITEMCHANGING/LVN_ITEMCHANGED.
142 * WM_CREATE does not issue WM_QUERYUISTATE and associated registry
143 * processing for "USEDOUBLECLICKTIME".
146 #include "config.h"
147 #include "wine/port.h"
149 #include <assert.h>
150 #include <ctype.h>
151 #include <string.h>
152 #include <stdlib.h>
153 #include <stdio.h>
155 #include "winbase.h"
156 #include "winnt.h"
157 #include "heap.h"
158 #include "commctrl.h"
159 #include "comctl32.h"
161 #include "wine/debug.h"
162 #include "wine/unicode.h"
164 WINE_DEFAULT_DEBUG_CHANNEL(listview);
166 /* make sure you set this to 0 for production use! */
167 #define DEBUG_RANGES 1
169 typedef struct tagCOLUMN_INFO
171 RECT rcHeader; /* tracks the header's rectangle */
172 int fmt; /* same as LVCOLUMN.fmt */
173 } COLUMN_INFO;
175 typedef struct tagITEMHDR
177 LPWSTR pszText;
178 INT iImage;
179 } ITEMHDR, *LPITEMHDR;
181 typedef struct tagSUBITEM_INFO
183 ITEMHDR hdr;
184 INT iSubItem;
185 } SUBITEM_INFO;
187 typedef struct tagITEM_INFO
189 ITEMHDR hdr;
190 UINT state;
191 LPARAM lParam;
192 INT iIndent;
193 } ITEM_INFO;
195 typedef struct tagRANGE
197 INT lower;
198 INT upper;
199 } RANGE;
201 typedef struct tagRANGES
203 HDPA hdpa;
204 } *RANGES;
206 typedef struct tagITERATOR
208 INT nItem;
209 INT nSpecial;
210 RANGE range;
211 RANGES ranges;
212 INT index;
213 } ITERATOR;
215 typedef struct tagLISTVIEW_INFO
217 HWND hwndSelf;
218 HBRUSH hBkBrush;
219 COLORREF clrBk;
220 COLORREF clrText;
221 COLORREF clrTextBk;
222 COLORREF clrTextBkDefault;
223 HIMAGELIST himlNormal;
224 HIMAGELIST himlSmall;
225 HIMAGELIST himlState;
226 BOOL bLButtonDown;
227 BOOL bRButtonDown;
228 INT nItemHeight;
229 INT nItemWidth;
230 RANGES selectionRanges;
231 INT nSelectionMark;
232 INT nHotItem;
233 SHORT notifyFormat;
234 RECT rcList; /* This rectangle is really the window
235 * client rectangle possibly reduced by the
236 * horizontal scroll bar and/or header - see
237 * LISTVIEW_UpdateSize. This rectangle offset
238 * by the LISTVIEW_GetOrigin value is in
239 * client coordinates */
240 SIZE iconSize;
241 SIZE iconSpacing;
242 SIZE iconStateSize;
243 UINT uCallbackMask;
244 HWND hwndHeader;
245 HCURSOR hHotCursor;
246 HFONT hDefaultFont;
247 HFONT hFont;
248 INT ntmHeight; /* Some cached metrics of the font used */
249 INT ntmAveCharWidth; /* by the listview to draw items */
250 BOOL bRedraw; /* Turns on/off repaints & invalidations */
251 BOOL bFirstPaint; /* Flags if the control has never painted before */
252 BOOL bAutoarrange; /* Autoarrange flag when NOT in LVS_AUTOARRANGE */
253 BOOL bFocus;
254 INT nFocusedItem;
255 RECT rcFocus;
256 DWORD dwStyle; /* the cached window GWL_STYLE */
257 DWORD dwLvExStyle; /* extended listview style */
258 INT nItemCount; /* the number of items in the list */
259 HDPA hdpaItems; /* array ITEM_INFO pointers */
260 HDPA hdpaPosX; /* maintains the (X, Y) coordinates of the */
261 HDPA hdpaPosY; /* items in LVS_ICON, and LVS_SMALLICON modes */
262 HDPA hdpaColumns; /* array of COLUMN_INFO pointers */
263 POINT currIconPos; /* this is the position next icon will be placed */
264 PFNLVCOMPARE pfnCompare;
265 LPARAM lParamSort;
266 HWND hwndEdit;
267 WNDPROC EditWndProc;
268 INT nEditLabelItem;
269 DWORD dwHoverTime;
271 DWORD lastKeyPressTimestamp;
272 WPARAM charCode;
273 INT nSearchParamLength;
274 WCHAR szSearchParam[ MAX_PATH ];
275 BOOL bIsDrawing;
276 } LISTVIEW_INFO;
279 * constants
281 /* How many we debug buffer to allocate */
282 #define DEBUG_BUFFERS 20
283 /* The size of a single debug bbuffer */
284 #define DEBUG_BUFFER_SIZE 256
286 /* Internal interface to LISTVIEW_HScroll and LISTVIEW_VScroll */
287 #define SB_INTERNAL -1
289 /* maximum size of a label */
290 #define DISP_TEXT_SIZE 512
292 /* padding for items in list and small icon display modes */
293 #define WIDTH_PADDING 12
295 /* padding for items in list, report and small icon display modes */
296 #define HEIGHT_PADDING 1
298 /* offset of items in report display mode */
299 #define REPORT_MARGINX 2
301 /* padding for icon in large icon display mode
302 * ICON_TOP_PADDING_NOTHITABLE - space between top of box and area
303 * that HITTEST will see.
304 * ICON_TOP_PADDING_HITABLE - spacing between above and icon.
305 * ICON_TOP_PADDING - sum of the two above.
306 * ICON_BOTTOM_PADDING - between bottom of icon and top of text
307 * LABEL_VERT_PADDING - between bottom of text and end of box
309 * ICON_LR_PADDING - additional width above icon size.
310 * ICON_LR_HALF - half of the above value
312 #define ICON_TOP_PADDING_NOTHITABLE 2
313 #define ICON_TOP_PADDING_HITABLE 2
314 #define ICON_TOP_PADDING (ICON_TOP_PADDING_NOTHITABLE + ICON_TOP_PADDING_HITABLE)
315 #define ICON_BOTTOM_PADDING 4
316 #define LABEL_VERT_PADDING 7
317 #define ICON_LR_PADDING 16
318 #define ICON_LR_HALF (ICON_LR_PADDING/2)
320 /* default label width for items in list and small icon display modes */
321 #define DEFAULT_LABEL_WIDTH 40
323 /* default column width for items in list display mode */
324 #define DEFAULT_COLUMN_WIDTH 128
326 /* Size of "line" scroll for V & H scrolls */
327 #define LISTVIEW_SCROLL_ICON_LINE_SIZE 37
329 /* Padding betwen image and label */
330 #define IMAGE_PADDING 2
332 /* Padding behind the label */
333 #define TRAILING_PADDING 5
335 /* Border for the icon caption */
336 #define CAPTION_BORDER 2
338 /* Standard DrawText flags */
339 #define LV_ML_DT_FLAGS (DT_TOP | DT_NOPREFIX | DT_EDITCONTROL | DT_CENTER | DT_WORDBREAK | DT_WORD_ELLIPSIS | DT_END_ELLIPSIS)
340 #define LV_FL_DT_FLAGS (DT_TOP | DT_NOPREFIX | DT_EDITCONTROL | DT_CENTER | DT_WORDBREAK | DT_NOCLIP)
341 #define LV_SL_DT_FLAGS (DT_TOP | DT_EDITCONTROL | DT_SINGLELINE | DT_WORD_ELLIPSIS | DT_END_ELLIPSIS)
343 /* The time in milliseconds to reset the search in the list */
344 #define KEY_DELAY 450
346 /* Dump the LISTVIEW_INFO structure to the debug channel */
347 #define LISTVIEW_DUMP(iP) do { \
348 TRACE("hwndSelf=%p, clrBk=0x%06lx, clrText=0x%06lx, clrTextBk=0x%06lx, ItemHeight=%d, ItemWidth=%d, Style=0x%08lx\n", \
349 iP->hwndSelf, iP->clrBk, iP->clrText, iP->clrTextBk, \
350 iP->nItemHeight, iP->nItemWidth, infoPtr->dwStyle); \
351 TRACE("hwndSelf=%p, himlNor=%p, himlSml=%p, himlState=%p, Focused=%d, Hot=%d, exStyle=0x%08lx, Focus=%d\n", \
352 iP->hwndSelf, iP->himlNormal, iP->himlSmall, iP->himlState, \
353 iP->nFocusedItem, iP->nHotItem, iP->dwLvExStyle, iP->bFocus ); \
354 TRACE("hwndSelf=%p, ntmH=%d, icSz.cx=%ld, icSz.cy=%ld, icSp.cx=%ld, icSp.cy=%ld, notifyFmt=%d\n", \
355 iP->hwndSelf, iP->ntmHeight, iP->iconSize.cx, iP->iconSize.cy, \
356 iP->iconSpacing.cx, iP->iconSpacing.cy, iP->notifyFormat); \
357 TRACE("hwndSelf=%p, rcList=%s\n", iP->hwndSelf, debugrect(&iP->rcList)); \
358 } while(0)
362 * forward declarations
364 static BOOL LISTVIEW_GetItemT(LISTVIEW_INFO *, LPLVITEMW, BOOL);
365 static void LISTVIEW_GetItemBox(LISTVIEW_INFO *, INT, LPRECT);
366 static void LISTVIEW_GetItemOrigin(LISTVIEW_INFO *, INT, LPPOINT);
367 static BOOL LISTVIEW_GetItemPosition(LISTVIEW_INFO *, INT, LPPOINT);
368 static BOOL LISTVIEW_GetItemRect(LISTVIEW_INFO *, INT, LPRECT);
369 static INT LISTVIEW_GetLabelWidth(LISTVIEW_INFO *, INT);
370 static void LISTVIEW_GetOrigin(LISTVIEW_INFO *, LPPOINT);
371 static BOOL LISTVIEW_GetViewRect(LISTVIEW_INFO *, LPRECT);
372 static void LISTVIEW_SetGroupSelection(LISTVIEW_INFO *, INT);
373 static BOOL LISTVIEW_SetItemT(LISTVIEW_INFO *, const LVITEMW *, BOOL);
374 static void LISTVIEW_UpdateScroll(LISTVIEW_INFO *);
375 static void LISTVIEW_SetSelection(LISTVIEW_INFO *, INT);
376 static void LISTVIEW_UpdateSize(LISTVIEW_INFO *);
377 static HWND LISTVIEW_EditLabelT(LISTVIEW_INFO *, INT, BOOL);
378 static LRESULT LISTVIEW_Command(LISTVIEW_INFO *, WPARAM, LPARAM);
379 static BOOL LISTVIEW_SortItems(LISTVIEW_INFO *, PFNLVCOMPARE, LPARAM);
380 static INT LISTVIEW_GetStringWidthT(LISTVIEW_INFO *, LPCWSTR, BOOL);
381 static BOOL LISTVIEW_KeySelection(LISTVIEW_INFO *, INT);
382 static UINT LISTVIEW_GetItemState(LISTVIEW_INFO *, INT, UINT);
383 static BOOL LISTVIEW_SetItemState(LISTVIEW_INFO *, INT, const LVITEMW *);
384 static LRESULT LISTVIEW_VScroll(LISTVIEW_INFO *, INT, INT, HWND);
385 static LRESULT LISTVIEW_HScroll(LISTVIEW_INFO *, INT, INT, HWND);
386 static INT LISTVIEW_GetTopIndex(LISTVIEW_INFO *);
387 static BOOL LISTVIEW_EnsureVisible(LISTVIEW_INFO *, INT, BOOL);
388 static HWND CreateEditLabelT(LISTVIEW_INFO *, LPCWSTR, DWORD, INT, INT, INT, INT, BOOL);
390 /******** Text handling functions *************************************/
392 /* A text pointer is either NULL, LPSTR_TEXTCALLBACK, or points to a
393 * text string. The string may be ANSI or Unicode, in which case
394 * the boolean isW tells us the type of the string.
396 * The name of the function tell what type of strings it expects:
397 * W: Unicode, T: ANSI/Unicode - function of isW
400 static inline BOOL is_textW(LPCWSTR text)
402 return text != NULL && text != LPSTR_TEXTCALLBACKW;
405 static inline BOOL is_textT(LPCWSTR text, BOOL isW)
407 /* we can ignore isW since LPSTR_TEXTCALLBACKW == LPSTR_TEXTCALLBACKA */
408 return is_textW(text);
411 static inline int textlenT(LPCWSTR text, BOOL isW)
413 return !is_textT(text, isW) ? 0 :
414 isW ? lstrlenW(text) : lstrlenA((LPCSTR)text);
417 static inline void textcpynT(LPWSTR dest, BOOL isDestW, LPCWSTR src, BOOL isSrcW, INT max)
419 if (isDestW)
420 if (isSrcW) lstrcpynW(dest, src, max);
421 else MultiByteToWideChar(CP_ACP, 0, (LPCSTR)src, -1, dest, max);
422 else
423 if (isSrcW) WideCharToMultiByte(CP_ACP, 0, src, -1, (LPSTR)dest, max, NULL, NULL);
424 else lstrcpynA((LPSTR)dest, (LPCSTR)src, max);
427 static inline LPWSTR textdupTtoW(LPCWSTR text, BOOL isW)
429 LPWSTR wstr = (LPWSTR)text;
431 if (!isW && is_textT(text, isW))
433 INT len = MultiByteToWideChar(CP_ACP, 0, (LPCSTR)text, -1, NULL, 0);
434 wstr = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
435 if (wstr) MultiByteToWideChar(CP_ACP, 0, (LPCSTR)text, -1, wstr, len);
437 TRACE(" wstr=%s\n", text == LPSTR_TEXTCALLBACKW ? "(callback)" : debugstr_w(wstr));
438 return wstr;
441 static inline void textfreeT(LPWSTR wstr, BOOL isW)
443 if (!isW && is_textT(wstr, isW)) HeapFree(GetProcessHeap(), 0, wstr);
447 * dest is a pointer to a Unicode string
448 * src is a pointer to a string (Unicode if isW, ANSI if !isW)
450 static BOOL textsetptrT(LPWSTR *dest, LPWSTR src, BOOL isW)
452 BOOL bResult = TRUE;
454 if (src == LPSTR_TEXTCALLBACKW)
456 if (is_textW(*dest)) COMCTL32_Free(*dest);
457 *dest = LPSTR_TEXTCALLBACKW;
459 else
461 LPWSTR pszText = textdupTtoW(src, isW);
462 if (*dest == LPSTR_TEXTCALLBACKW) *dest = NULL;
463 bResult = Str_SetPtrW(dest, pszText);
464 textfreeT(pszText, isW);
466 return bResult;
470 * compares a Unicode to a Unicode/ANSI text string
472 static inline int textcmpWT(LPCWSTR aw, LPCWSTR bt, BOOL isW)
474 if (!aw) return bt ? -1 : 0;
475 if (!bt) return aw ? 1 : 0;
476 if (aw == LPSTR_TEXTCALLBACKW)
477 return bt == LPSTR_TEXTCALLBACKW ? 0 : -1;
478 if (bt != LPSTR_TEXTCALLBACKW)
480 LPWSTR bw = textdupTtoW(bt, isW);
481 int r = bw ? lstrcmpW(aw, bw) : 1;
482 textfreeT(bw, isW);
483 return r;
486 return 1;
489 static inline int lstrncmpiW(LPCWSTR s1, LPCWSTR s2, int n)
491 int res;
493 n = min(min(n, strlenW(s1)), strlenW(s2));
494 res = CompareStringW(LOCALE_USER_DEFAULT, NORM_IGNORECASE, s1, n, s2, n);
495 return res ? res - sizeof(WCHAR) : res;
498 /******** Debugging functions *****************************************/
500 static inline LPCSTR debugtext_t(LPCWSTR text, BOOL isW)
502 if (text == LPSTR_TEXTCALLBACKW) return "(callback)";
503 return isW ? debugstr_w(text) : debugstr_a((LPCSTR)text);
506 static inline LPCSTR debugtext_tn(LPCWSTR text, BOOL isW, INT n)
508 if (text == LPSTR_TEXTCALLBACKW) return "(callback)";
509 n = min(textlenT(text, isW), n);
510 return isW ? debugstr_wn(text, n) : debugstr_an((LPCSTR)text, n);
513 static char* debug_getbuf()
515 static int index = 0;
516 static char buffers[DEBUG_BUFFERS][DEBUG_BUFFER_SIZE];
517 return buffers[index++ % DEBUG_BUFFERS];
520 static inline char* debugrange(const RANGE *lprng)
522 if (lprng)
524 char* buf = debug_getbuf();
525 snprintf(buf, DEBUG_BUFFER_SIZE, "[%d, %d)", lprng->lower, lprng->upper);
526 return buf;
527 } else return "(null)";
530 static inline char* debugpoint(const POINT *lppt)
532 if (lppt)
534 char* buf = debug_getbuf();
535 snprintf(buf, DEBUG_BUFFER_SIZE, "(%ld, %ld)", lppt->x, lppt->y);
536 return buf;
537 } else return "(null)";
540 static inline char* debugrect(const RECT *rect)
542 if (rect)
544 char* buf = debug_getbuf();
545 snprintf(buf, DEBUG_BUFFER_SIZE, "[(%d, %d);(%d, %d)]",
546 rect->left, rect->top, rect->right, rect->bottom);
547 return buf;
548 } else return "(null)";
551 static char * debugscrollinfo(const SCROLLINFO *pScrollInfo)
553 char* buf = debug_getbuf(), *text = buf;
554 int len, size = DEBUG_BUFFER_SIZE;
556 if (pScrollInfo == NULL) return "(null)";
557 len = snprintf(buf, size, "{cbSize=%d, ", pScrollInfo->cbSize);
558 if (len == -1) goto end; buf += len; size -= len;
559 if (pScrollInfo->fMask & SIF_RANGE)
560 len = snprintf(buf, size, "nMin=%d, nMax=%d, ", pScrollInfo->nMin, pScrollInfo->nMax);
561 else len = 0;
562 if (len == -1) goto end; buf += len; size -= len;
563 if (pScrollInfo->fMask & SIF_PAGE)
564 len = snprintf(buf, size, "nPage=%u, ", pScrollInfo->nPage);
565 else len = 0;
566 if (len == -1) goto end; buf += len; size -= len;
567 if (pScrollInfo->fMask & SIF_POS)
568 len = snprintf(buf, size, "nPos=%d, ", pScrollInfo->nPos);
569 else len = 0;
570 if (len == -1) goto end; buf += len; size -= len;
571 if (pScrollInfo->fMask & SIF_TRACKPOS)
572 len = snprintf(buf, size, "nTrackPos=%d, ", pScrollInfo->nTrackPos);
573 else len = 0;
574 if (len == -1) goto end; buf += len; size -= len;
575 goto undo;
576 end:
577 buf = text + strlen(text);
578 undo:
579 if (buf - text > 2) { buf[-2] = '}'; buf[-1] = 0; }
580 return text;
583 static char* debugnmlistview(const NMLISTVIEW *plvnm)
585 if (plvnm)
587 char* buf = debug_getbuf();
588 snprintf(buf, DEBUG_BUFFER_SIZE, "iItem=%d, iSubItem=%d, uNewState=0x%x,"
589 " uOldState=0x%x, uChanged=0x%x, ptAction=%s, lParam=%ld\n",
590 plvnm->iItem, plvnm->iSubItem, plvnm->uNewState, plvnm->uOldState,
591 plvnm->uChanged, debugpoint(&plvnm->ptAction), plvnm->lParam);
592 return buf;
593 } else return "(null)";
596 static char* debuglvitem_t(const LVITEMW *lpLVItem, BOOL isW)
598 char* buf = debug_getbuf(), *text = buf;
599 int len, size = DEBUG_BUFFER_SIZE;
601 if (lpLVItem == NULL) return "(null)";
602 len = snprintf(buf, size, "{iItem=%d, iSubItem=%d, ", lpLVItem->iItem, lpLVItem->iSubItem);
603 if (len == -1) goto end; buf += len; size -= len;
604 if (lpLVItem->mask & LVIF_STATE)
605 len = snprintf(buf, size, "state=%x, stateMask=%x, ", lpLVItem->state, lpLVItem->stateMask);
606 else len = 0;
607 if (len == -1) goto end; buf += len; size -= len;
608 if (lpLVItem->mask & LVIF_TEXT)
609 len = snprintf(buf, size, "pszText=%s, cchTextMax=%d, ", debugtext_tn(lpLVItem->pszText, isW, 80), lpLVItem->cchTextMax);
610 else len = 0;
611 if (len == -1) goto end; buf += len; size -= len;
612 if (lpLVItem->mask & LVIF_IMAGE)
613 len = snprintf(buf, size, "iImage=%d, ", lpLVItem->iImage);
614 else len = 0;
615 if (len == -1) goto end; buf += len; size -= len;
616 if (lpLVItem->mask & LVIF_PARAM)
617 len = snprintf(buf, size, "lParam=%lx, ", lpLVItem->lParam);
618 else len = 0;
619 if (len == -1) goto end; buf += len; size -= len;
620 if (lpLVItem->mask & LVIF_INDENT)
621 len = snprintf(buf, size, "iIndent=%d, ", lpLVItem->iIndent);
622 else len = 0;
623 if (len == -1) goto end; buf += len; size -= len;
624 goto undo;
625 end:
626 buf = text + strlen(text);
627 undo:
628 if (buf - text > 2) { buf[-2] = '}'; buf[-1] = 0; }
629 return text;
632 static char* debuglvcolumn_t(const LVCOLUMNW *lpColumn, BOOL isW)
634 char* buf = debug_getbuf(), *text = buf;
635 int len, size = DEBUG_BUFFER_SIZE;
637 if (lpColumn == NULL) return "(null)";
638 len = snprintf(buf, size, "{");
639 if (len == -1) goto end; buf += len; size -= len;
640 if (lpColumn->mask & LVCF_SUBITEM)
641 len = snprintf(buf, size, "iSubItem=%d, ", lpColumn->iSubItem);
642 else len = 0;
643 if (len == -1) goto end; buf += len; size -= len;
644 if (lpColumn->mask & LVCF_FMT)
645 len = snprintf(buf, size, "fmt=%x, ", lpColumn->fmt);
646 else len = 0;
647 if (len == -1) goto end; buf += len; size -= len;
648 if (lpColumn->mask & LVCF_WIDTH)
649 len = snprintf(buf, size, "cx=%d, ", lpColumn->cx);
650 else len = 0;
651 if (len == -1) goto end; buf += len; size -= len;
652 if (lpColumn->mask & LVCF_TEXT)
653 len = snprintf(buf, size, "pszText=%s, cchTextMax=%d, ", debugtext_tn(lpColumn->pszText, isW, 80), lpColumn->cchTextMax);
654 else len = 0;
655 if (len == -1) goto end; buf += len; size -= len;
656 if (lpColumn->mask & LVCF_IMAGE)
657 len = snprintf(buf, size, "iImage=%d, ", lpColumn->iImage);
658 else len = 0;
659 if (len == -1) goto end; buf += len; size -= len;
660 if (lpColumn->mask & LVCF_ORDER)
661 len = snprintf(buf, size, "iOrder=%d, ", lpColumn->iOrder);
662 else len = 0;
663 if (len == -1) goto end; buf += len; size -= len;
664 goto undo;
665 end:
666 buf = text + strlen(text);
667 undo:
668 if (buf - text > 2) { buf[-2] = '}'; buf[-1] = 0; }
669 return text;
673 static char* debuglvhittestinfo(const LVHITTESTINFO *lpht)
675 if (lpht)
677 char* buf = debug_getbuf();
678 snprintf(buf, DEBUG_BUFFER_SIZE, "{pt=%s, flags=0x%x, iItem=%d, iSubItem=%d}",
679 debugpoint(&lpht->pt), lpht->flags, lpht->iItem, lpht->iSubItem);
680 return buf;
681 } else return "(null)";
684 /******** Notification functions i************************************/
686 static LRESULT notify_hdr(LISTVIEW_INFO *infoPtr, INT code, LPNMHDR pnmh)
688 LRESULT result;
690 TRACE("(code=%d)\n", code);
692 pnmh->hwndFrom = infoPtr->hwndSelf;
693 pnmh->idFrom = GetWindowLongW(infoPtr->hwndSelf, GWL_ID);
694 pnmh->code = code;
695 result = SendMessageW(GetParent(infoPtr->hwndSelf), WM_NOTIFY,
696 (WPARAM)pnmh->idFrom, (LPARAM)pnmh);
698 TRACE(" <= %ld\n", result);
700 return result;
703 static inline LRESULT notify(LISTVIEW_INFO *infoPtr, INT code)
705 NMHDR nmh;
706 return notify_hdr(infoPtr, code, &nmh);
709 static inline void notify_itemactivate(LISTVIEW_INFO *infoPtr)
711 notify(infoPtr, LVN_ITEMACTIVATE);
714 static inline LRESULT notify_listview(LISTVIEW_INFO *infoPtr, INT code, LPNMLISTVIEW plvnm)
716 TRACE("(code=%d, plvnm=%s)\n", code, debugnmlistview(plvnm));
717 return notify_hdr(infoPtr, code, (LPNMHDR)plvnm);
720 static LRESULT notify_click(LISTVIEW_INFO *infoPtr, INT code, LVHITTESTINFO *lvht)
722 NMLISTVIEW nmlv;
723 LVITEMW item;
725 TRACE("code=%d, lvht=%s\n", code, debuglvhittestinfo(lvht));
726 ZeroMemory(&nmlv, sizeof(nmlv));
727 nmlv.iItem = lvht->iItem;
728 nmlv.iSubItem = lvht->iSubItem;
729 nmlv.ptAction = lvht->pt;
730 item.mask = LVIF_PARAM;
731 item.iItem = lvht->iItem;
732 item.iSubItem = 0;
733 if (LISTVIEW_GetItemT(infoPtr, &item, TRUE)) nmlv.lParam = item.lParam;
734 return notify_listview(infoPtr, code, &nmlv);
737 static void notify_deleteitem(LISTVIEW_INFO *infoPtr, INT nItem)
739 NMLISTVIEW nmlv;
740 LVITEMW item;
742 ZeroMemory(&nmlv, sizeof (NMLISTVIEW));
743 nmlv.iItem = nItem;
744 item.mask = LVIF_PARAM;
745 item.iItem = nItem;
746 item.iSubItem = 0;
747 if (LISTVIEW_GetItemT(infoPtr, &item, TRUE)) nmlv.lParam = item.lParam;
748 notify_listview(infoPtr, LVN_DELETEITEM, &nmlv);
751 static int get_ansi_notification(INT unicodeNotificationCode)
753 switch (unicodeNotificationCode)
755 case LVN_BEGINLABELEDITW: return LVN_BEGINLABELEDITA;
756 case LVN_ENDLABELEDITW: return LVN_ENDLABELEDITA;
757 case LVN_GETDISPINFOW: return LVN_GETDISPINFOA;
758 case LVN_SETDISPINFOW: return LVN_SETDISPINFOA;
759 case LVN_ODFINDITEMW: return LVN_ODFINDITEMA;
760 case LVN_GETINFOTIPW: return LVN_GETINFOTIPA;
762 ERR("unknown notification %x\n", unicodeNotificationCode);
763 assert(FALSE);
767 Send notification. depends on dispinfoW having same
768 structure as dispinfoA.
769 infoPtr : listview struct
770 notificationCode : *Unicode* notification code
771 pdi : dispinfo structure (can be unicode or ansi)
772 isW : TRUE if dispinfo is Unicode
774 static BOOL notify_dispinfoT(LISTVIEW_INFO *infoPtr, INT notificationCode, LPNMLVDISPINFOW pdi, BOOL isW)
776 BOOL bResult = FALSE;
777 BOOL convertToAnsi = FALSE, convertToUnicode = FALSE;
778 INT realNotifCode;
779 INT cchTempBufMax = 0, savCchTextMax = 0;
780 LPWSTR pszTempBuf = NULL, savPszText = NULL;
782 if ((pdi->item.mask & LVIF_TEXT) && is_textT(pdi->item.pszText, isW))
784 convertToAnsi = (isW && infoPtr->notifyFormat == NFR_ANSI);
785 convertToUnicode = (!isW && infoPtr->notifyFormat == NFR_UNICODE);
788 if (convertToAnsi || convertToUnicode)
790 if (notificationCode != LVN_GETDISPINFOW)
792 cchTempBufMax = convertToUnicode ?
793 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pdi->item.pszText, -1, NULL, 0):
794 WideCharToMultiByte(CP_ACP, 0, pdi->item.pszText, -1, NULL, 0, NULL, NULL);
796 else
798 cchTempBufMax = pdi->item.cchTextMax;
799 *pdi->item.pszText = 0; /* make sure we don't process garbage */
802 pszTempBuf = HeapAlloc(GetProcessHeap(), 0,
803 (convertToUnicode ? sizeof(WCHAR) : sizeof(CHAR)) * cchTempBufMax);
804 if (!pszTempBuf) return FALSE;
805 if (convertToUnicode)
806 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pdi->item.pszText, -1,
807 pszTempBuf, cchTempBufMax);
808 else
809 WideCharToMultiByte(CP_ACP, 0, pdi->item.pszText, -1, (LPSTR) pszTempBuf,
810 cchTempBufMax, NULL, NULL);
811 savCchTextMax = pdi->item.cchTextMax;
812 savPszText = pdi->item.pszText;
813 pdi->item.pszText = pszTempBuf;
814 pdi->item.cchTextMax = cchTempBufMax;
817 if (infoPtr->notifyFormat == NFR_ANSI)
818 realNotifCode = get_ansi_notification(notificationCode);
819 else
820 realNotifCode = notificationCode;
821 TRACE(" pdi->item=%s\n", debuglvitem_t(&pdi->item, infoPtr->notifyFormat != NFR_ANSI));
822 bResult = notify_hdr(infoPtr, realNotifCode, (LPNMHDR)pdi);
824 if (convertToUnicode || convertToAnsi)
826 if (convertToUnicode) /* note : pointer can be changed by app ! */
827 WideCharToMultiByte(CP_ACP, 0, pdi->item.pszText, -1, (LPSTR) savPszText,
828 savCchTextMax, NULL, NULL);
829 else
830 MultiByteToWideChar(CP_ACP, 0, (LPSTR) pdi->item.pszText, -1,
831 savPszText, savCchTextMax);
832 pdi->item.pszText = savPszText; /* restores our buffer */
833 pdi->item.cchTextMax = savCchTextMax;
834 HeapFree(GetProcessHeap(), 0, pszTempBuf);
836 return bResult;
839 static void customdraw_fill(NMLVCUSTOMDRAW *lpnmlvcd, LISTVIEW_INFO *infoPtr, HDC hdc, const RECT *rcBounds)
841 ZeroMemory(lpnmlvcd, sizeof(NMLVCUSTOMDRAW));
842 lpnmlvcd->nmcd.hdc = hdc;
843 lpnmlvcd->nmcd.rc = *rcBounds;
844 lpnmlvcd->clrTextBk = infoPtr->clrTextBk;
845 lpnmlvcd->clrText = infoPtr->clrText;
848 static inline DWORD notify_customdraw (LISTVIEW_INFO *infoPtr, DWORD dwDrawStage, NMLVCUSTOMDRAW *lpnmlvcd)
850 lpnmlvcd->nmcd.dwDrawStage = dwDrawStage;
851 return notify_hdr(infoPtr, NM_CUSTOMDRAW, &lpnmlvcd->nmcd.hdr);
854 /******** Item iterator functions **********************************/
856 static RANGES ranges_create(int count);
857 static void ranges_destroy(RANGES ranges);
858 static BOOL ranges_add(RANGES ranges, RANGE range);
859 static BOOL ranges_del(RANGES ranges, RANGE range);
860 static void ranges_dump(RANGES ranges);
862 static inline BOOL ranges_additem(RANGES ranges, INT nItem)
864 RANGE range = { nItem, nItem + 1 };
866 return ranges_add(ranges, range);
869 static inline BOOL ranges_delitem(RANGES ranges, INT nItem)
871 RANGE range = { nItem, nItem + 1 };
873 return ranges_del(ranges, range);
876 /***
877 * ITERATOR DOCUMENTATION
879 * The iterator functions allow for easy, and convenient iteration
880 * over items of iterest in the list. Typically, you create a
881 * iterator, use it, and destroy it, as such:
882 * ITERATOR i;
884 * iterator_xxxitems(&i, ...);
885 * while (iterator_{prev,next}(&i)
887 * //code which uses i.nItem
889 * iterator_destroy(&i);
891 * where xxx is either: framed, or visible.
892 * Note that it is important that the code destroys the iterator
893 * after it's done with it, as the creation of the iterator may
894 * allocate memory, which thus needs to be freed.
896 * You can iterate both forwards, and backwards through the list,
897 * by using iterator_next or iterator_prev respectively.
899 * Lower numbered items are draw on top of higher number items in
900 * LVS_ICON, and LVS_SMALLICON (which are the only modes where
901 * items may overlap). So, to test items, you should use
902 * iterator_next
903 * which lists the items top to bottom (in Z-order).
904 * For drawing items, you should use
905 * iterator_prev
906 * which lists the items bottom to top (in Z-order).
907 * If you keep iterating over the items after the end-of-items
908 * marker (-1) is returned, the iterator will start from the
909 * beginning. Typically, you don't need to test for -1,
910 * because iterator_{next,prev} will return TRUE if more items
911 * are to be iterated over, or FALSE otherwise.
913 * Note: the iterator is defined to be bidirectional. That is,
914 * any number of prev followed by any number of next, or
915 * five versa, should leave the iterator at the same item:
916 * prev * n, next * n = next * n, prev * n
918 * The iterator has a notion of a out-of-order, special item,
919 * which sits at the start of the list. This is used in
920 * LVS_ICON, and LVS_SMALLICON mode to handle the focused item,
921 * which needs to be first, as it may overlap other items.
923 * The code is a bit messy because we have:
924 * - a special item to deal with
925 * - simple range, or composite range
926 * - empty range.
927 * If find bugs, or want to add features, please make sure you
928 * always check/modify *both* iterator_prev, and iterator_next.
931 /****
932 * This function iterates through the items in increasing order,
933 * but prefixed by the special item, then -1. That is:
934 * special, 1, 2, 3, ..., n, -1.
935 * Each item is listed only once.
937 static inline BOOL iterator_next(ITERATOR* i)
939 if (i->nItem == -1)
941 i->nItem = i->nSpecial;
942 if (i->nItem != -1) return TRUE;
944 if (i->nItem == i->nSpecial)
946 if (i->ranges) i->index = 0;
947 goto pickarange;
950 i->nItem++;
951 testitem:
952 if (i->nItem == i->nSpecial) i->nItem++;
953 if (i->nItem < i->range.upper) return TRUE;
955 pickarange:
956 if (i->ranges)
958 if (i->index < i->ranges->hdpa->nItemCount)
959 i->range = *(RANGE*)DPA_GetPtr(i->ranges->hdpa, i->index++);
960 else goto end;
962 else if (i->nItem >= i->range.upper) goto end;
964 i->nItem = i->range.lower;
965 if (i->nItem >= 0) goto testitem;
966 end:
967 i->nItem = -1;
968 return FALSE;
971 /****
972 * This function iterates through the items in decreasing order,
973 * followed by the special item, then -1. That is:
974 * n, n-1, ..., 3, 2, 1, special, -1.
975 * Each item is listed only once.
977 static inline BOOL iterator_prev(ITERATOR* i)
979 BOOL start = FALSE;
981 if (i->nItem == -1)
983 start = TRUE;
984 if (i->ranges) i->index = i->ranges->hdpa->nItemCount;
985 goto pickarange;
987 if (i->nItem == i->nSpecial)
989 i->nItem = -1;
990 return FALSE;
993 testitem:
994 i->nItem--;
995 if (i->nItem == i->nSpecial) i->nItem--;
996 if (i->nItem >= i->range.lower) return TRUE;
998 pickarange:
999 if (i->ranges)
1001 if (i->index > 0)
1002 i->range = *(RANGE*)DPA_GetPtr(i->ranges->hdpa, --i->index);
1003 else goto end;
1005 else if (!start && i->nItem < i->range.lower) goto end;
1007 i->nItem = i->range.upper;
1008 if (i->nItem > 0) goto testitem;
1009 end:
1010 return (i->nItem = i->nSpecial) != -1;
1013 static RANGE iterator_range(ITERATOR* i)
1015 RANGE range;
1017 if (!i->ranges) return i->range;
1019 range.lower = (*(RANGE*)DPA_GetPtr(i->ranges->hdpa, 0)).lower;
1020 range.upper = (*(RANGE*)DPA_GetPtr(i->ranges->hdpa, i->ranges->hdpa->nItemCount - 1)).upper;
1021 return range;
1024 /***
1025 * Releases resources associated with this ierator.
1027 static inline void iterator_destroy(ITERATOR* i)
1029 if (i->ranges) ranges_destroy(i->ranges);
1032 /***
1033 * Create an empty iterator.
1035 static inline BOOL iterator_empty(ITERATOR* i)
1037 ZeroMemory(i, sizeof(*i));
1038 i->nItem = i->nSpecial = i->range.lower = i->range.upper = -1;
1039 return TRUE;
1042 /***
1043 * Create an iterator over a range.
1045 static inline BOOL iterator_rangeitems(ITERATOR* i, RANGE range)
1047 iterator_empty(i);
1048 i->range = range;
1049 return TRUE;
1052 /***
1053 * Create an iterator over a bunch of ranges.
1054 * Please note that the iterator will take ownership of the ranges,
1055 * and will free them upon destruction.
1057 static inline BOOL iterator_rangesitems(ITERATOR* i, RANGES ranges)
1059 iterator_empty(i);
1060 i->ranges = ranges;
1061 return TRUE;
1064 /***
1065 * Creates an iterator over the items which intersect lprc.
1067 static BOOL iterator_frameditems(ITERATOR* i, LISTVIEW_INFO* infoPtr, const RECT *lprc)
1069 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
1070 RECT frame = *lprc, rcItem, rcTemp;
1071 POINT Origin;
1073 /* in case we fail, we want to return an empty iterator */
1074 if (!iterator_empty(i)) return FALSE;
1076 LISTVIEW_GetOrigin(infoPtr, &Origin);
1078 TRACE("(lprc=%s)\n", debugrect(lprc));
1079 OffsetRect(&frame, -Origin.x, -Origin.y);
1081 if (uView == LVS_ICON || uView == LVS_SMALLICON)
1083 INT nItem;
1085 if (uView == LVS_ICON && infoPtr->nFocusedItem != -1)
1087 LISTVIEW_GetItemBox(infoPtr, infoPtr->nFocusedItem, &rcItem);
1088 if (IntersectRect(&rcTemp, &rcItem, lprc))
1089 i->nSpecial = infoPtr->nFocusedItem;
1091 if (!(iterator_rangesitems(i, ranges_create(50)))) return FALSE;
1092 /* to do better here, we need to have PosX, and PosY sorted */
1093 TRACE("building icon ranges:\n");
1094 for (nItem = 0; nItem < infoPtr->nItemCount; nItem++)
1096 rcItem.left = (LONG)DPA_GetPtr(infoPtr->hdpaPosX, nItem);
1097 rcItem.top = (LONG)DPA_GetPtr(infoPtr->hdpaPosY, nItem);
1098 rcItem.right = rcItem.left + infoPtr->nItemWidth;
1099 rcItem.bottom = rcItem.top + infoPtr->nItemHeight;
1100 if (IntersectRect(&rcTemp, &rcItem, &frame))
1101 ranges_additem(i->ranges, nItem);
1103 return TRUE;
1105 else if (uView == LVS_REPORT)
1107 RANGE range;
1109 if (frame.left >= infoPtr->nItemWidth) return TRUE;
1110 if (frame.top >= infoPtr->nItemHeight * infoPtr->nItemCount) return TRUE;
1112 range.lower = max(frame.top / infoPtr->nItemHeight, 0);
1113 range.upper = min((frame.bottom - 1) / infoPtr->nItemHeight, infoPtr->nItemCount - 1) + 1;
1114 if (range.upper <= range.lower) return TRUE;
1115 if (!iterator_rangeitems(i, range)) return FALSE;
1116 TRACE(" report=%s\n", debugrange(&i->range));
1118 else
1120 INT nPerCol = max((infoPtr->rcList.bottom - infoPtr->rcList.top) / infoPtr->nItemHeight, 1);
1121 INT nFirstRow = max(frame.top / infoPtr->nItemHeight, 0);
1122 INT nLastRow = min((frame.bottom - 1) / infoPtr->nItemHeight, nPerCol - 1);
1123 INT nFirstCol = max(frame.left / infoPtr->nItemWidth, 0);
1124 INT nLastCol = min((frame.right - 1) / infoPtr->nItemWidth, (infoPtr->nItemCount + nPerCol - 1) / nPerCol);
1125 INT lower = nFirstCol * nPerCol + nFirstRow;
1126 RANGE item_range;
1127 INT nCol;
1129 TRACE("nPerCol=%d, nFirstRow=%d, nLastRow=%d, nFirstCol=%d, nLastCol=%d, lower=%d\n",
1130 nPerCol, nFirstRow, nLastRow, nFirstCol, nLastCol, lower);
1132 if (nLastCol < nFirstCol || nLastRow < nFirstRow) return TRUE;
1134 if (!(iterator_rangesitems(i, ranges_create(nLastCol - nFirstCol + 1)))) return FALSE;
1135 TRACE("building list ranges:\n");
1136 for (nCol = nFirstCol; nCol <= nLastCol; nCol++)
1138 item_range.lower = nCol * nPerCol + nFirstRow;
1139 if(item_range.lower >= infoPtr->nItemCount) break;
1140 item_range.upper = min(nCol * nPerCol + nLastRow + 1, infoPtr->nItemCount);
1141 TRACE(" list=%s\n", debugrange(&item_range));
1142 ranges_add(i->ranges, item_range);
1146 return TRUE;
1149 /***
1150 * Creates an iterator over the items which intersect the visible region of hdc.
1152 static BOOL iterator_visibleitems(ITERATOR *i, LISTVIEW_INFO *infoPtr, HDC hdc)
1154 POINT Origin, Position;
1155 RECT rcItem, rcClip;
1156 INT rgntype;
1158 rgntype = GetClipBox(hdc, &rcClip);
1159 if (rgntype == NULLREGION) return iterator_empty(i);
1160 if (!iterator_frameditems(i, infoPtr, &rcClip)) return FALSE;
1161 if (rgntype == SIMPLEREGION) return TRUE;
1163 /* first deal with the special item */
1164 if (i->nSpecial != -1)
1166 LISTVIEW_GetItemBox(infoPtr, i->nSpecial, &rcItem);
1167 if (!RectVisible(hdc, &rcItem)) i->nSpecial = -1;
1170 /* if we can't deal with the region, we'll just go with the simple range */
1171 LISTVIEW_GetOrigin(infoPtr, &Origin);
1172 TRACE("building visible range:\n");
1173 if (!i->ranges && i->range.lower < i->range.upper)
1175 if (!(i->ranges = ranges_create(50))) return TRUE;
1176 if (!ranges_add(i->ranges, i->range))
1178 ranges_destroy(i->ranges);
1179 i->ranges = 0;
1180 return TRUE;
1184 /* now delete the invisible items from the list */
1185 while(iterator_next(i))
1187 LISTVIEW_GetItemOrigin(infoPtr, i->nItem, &Position);
1188 rcItem.left = Position.x + Origin.x;
1189 rcItem.top = Position.y + Origin.y;
1190 rcItem.right = rcItem.left + infoPtr->nItemWidth;
1191 rcItem.bottom = rcItem.top + infoPtr->nItemHeight;
1192 if (!RectVisible(hdc, &rcItem))
1193 ranges_delitem(i->ranges, i->nItem);
1195 /* the iterator should restart on the next iterator_next */
1196 TRACE("done\n");
1198 return TRUE;
1201 /******** Misc helper functions ************************************/
1203 static inline LRESULT CallWindowProcT(WNDPROC proc, HWND hwnd, UINT uMsg,
1204 WPARAM wParam, LPARAM lParam, BOOL isW)
1206 if (isW) return CallWindowProcW(proc, hwnd, uMsg, wParam, lParam);
1207 else return CallWindowProcA(proc, hwnd, uMsg, wParam, lParam);
1210 static inline BOOL is_autoarrange(LISTVIEW_INFO *infoPtr)
1212 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
1214 return ((infoPtr->dwStyle & LVS_AUTOARRANGE) || infoPtr->bAutoarrange) &&
1215 (uView == LVS_ICON || uView == LVS_SMALLICON);
1218 /******** Internal API functions ************************************/
1220 static inline COLUMN_INFO * LISTVIEW_GetColumnInfo(LISTVIEW_INFO *infoPtr, INT nSubItem)
1222 static COLUMN_INFO mainItem;
1224 if (nSubItem == 0 && infoPtr->hdpaColumns->nItemCount == 0) return &mainItem;
1225 assert (nSubItem >= 0 && nSubItem < infoPtr->hdpaColumns->nItemCount);
1226 return (COLUMN_INFO *)DPA_GetPtr(infoPtr->hdpaColumns, nSubItem);
1229 static inline void LISTVIEW_GetHeaderRect(LISTVIEW_INFO *infoPtr, INT nSubItem, RECT *lprc)
1231 *lprc = LISTVIEW_GetColumnInfo(infoPtr, nSubItem)->rcHeader;
1234 static inline BOOL LISTVIEW_GetItemW(LISTVIEW_INFO *infoPtr, LPLVITEMW lpLVItem)
1236 return LISTVIEW_GetItemT(infoPtr, lpLVItem, TRUE);
1239 /* Listview invlaidation functions: use _only_ these function to invalidate */
1241 static inline BOOL is_redrawing(LISTVIEW_INFO *infoPtr)
1243 return infoPtr->bRedraw && !infoPtr->bFirstPaint;
1246 static inline void LISTVIEW_InvalidateRect(LISTVIEW_INFO *infoPtr, const RECT* rect)
1248 if(!is_redrawing(infoPtr)) return;
1249 TRACE(" invalidating rect=%s\n", debugrect(rect));
1250 InvalidateRect(infoPtr->hwndSelf, rect, TRUE);
1253 static inline void LISTVIEW_InvalidateItem(LISTVIEW_INFO *infoPtr, INT nItem)
1255 RECT rcBox;
1257 if(!is_redrawing(infoPtr)) return;
1258 LISTVIEW_GetItemBox(infoPtr, nItem, &rcBox);
1259 LISTVIEW_InvalidateRect(infoPtr, &rcBox);
1262 static inline void LISTVIEW_InvalidateSubItem(LISTVIEW_INFO *infoPtr, INT nItem, INT nSubItem)
1264 POINT Origin, Position;
1265 RECT rcBox;
1267 if(!is_redrawing(infoPtr)) return;
1268 assert ((infoPtr->dwStyle & LVS_TYPEMASK) == LVS_REPORT);
1269 LISTVIEW_GetOrigin(infoPtr, &Origin);
1270 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position);
1271 LISTVIEW_GetHeaderRect(infoPtr, nSubItem, &rcBox);
1272 rcBox.top = 0;
1273 rcBox.bottom = infoPtr->nItemHeight;
1274 OffsetRect(&rcBox, Origin.x + Position.x, Origin.y + Position.y);
1275 LISTVIEW_InvalidateRect(infoPtr, &rcBox);
1278 static inline void LISTVIEW_InvalidateList(LISTVIEW_INFO *infoPtr)
1280 LISTVIEW_InvalidateRect(infoPtr, NULL);
1283 static inline void LISTVIEW_InvalidateColumn(LISTVIEW_INFO *infoPtr, INT nColumn)
1285 RECT rcCol;
1287 if(!is_redrawing(infoPtr)) return;
1288 LISTVIEW_GetHeaderRect(infoPtr, nColumn, &rcCol);
1289 rcCol.top = infoPtr->rcList.top;
1290 rcCol.bottom = infoPtr->rcList.bottom;
1291 LISTVIEW_InvalidateRect(infoPtr, &rcCol);
1294 /***
1295 * DESCRIPTION:
1296 * Retrieves the number of items that can fit vertically in the client area.
1298 * PARAMETER(S):
1299 * [I] infoPtr : valid pointer to the listview structure
1301 * RETURN:
1302 * Number of items per row.
1304 static inline INT LISTVIEW_GetCountPerRow(LISTVIEW_INFO *infoPtr)
1306 INT nListWidth = infoPtr->rcList.right - infoPtr->rcList.left;
1308 return max(nListWidth/infoPtr->nItemWidth, 1);
1311 /***
1312 * DESCRIPTION:
1313 * Retrieves the number of items that can fit horizontally in the client
1314 * area.
1316 * PARAMETER(S):
1317 * [I] infoPtr : valid pointer to the listview structure
1319 * RETURN:
1320 * Number of items per column.
1322 static inline INT LISTVIEW_GetCountPerColumn(LISTVIEW_INFO *infoPtr)
1324 INT nListHeight = infoPtr->rcList.bottom - infoPtr->rcList.top;
1326 return max(nListHeight / infoPtr->nItemHeight, 1);
1330 /*************************************************************************
1331 * LISTVIEW_ProcessLetterKeys
1333 * Processes keyboard messages generated by pressing the letter keys
1334 * on the keyboard.
1335 * What this does is perform a case insensitive search from the
1336 * current position with the following quirks:
1337 * - If two chars or more are pressed in quick succession we search
1338 * for the corresponding string (e.g. 'abc').
1339 * - If there is a delay we wipe away the current search string and
1340 * restart with just that char.
1341 * - If the user keeps pressing the same character, whether slowly or
1342 * fast, so that the search string is entirely composed of this
1343 * character ('aaaaa' for instance), then we search for first item
1344 * that starting with that character.
1345 * - If the user types the above character in quick succession, then
1346 * we must also search for the corresponding string ('aaaaa'), and
1347 * go to that string if there is a match.
1349 * PARAMETERS
1350 * [I] hwnd : handle to the window
1351 * [I] charCode : the character code, the actual character
1352 * [I] keyData : key data
1354 * RETURNS
1356 * Zero.
1358 * BUGS
1360 * - The current implementation has a list of characters it will
1361 * accept and it ignores averything else. In particular it will
1362 * ignore accentuated characters which seems to match what
1363 * Windows does. But I'm not sure it makes sense to follow
1364 * Windows there.
1365 * - We don't sound a beep when the search fails.
1367 * SEE ALSO
1369 * TREEVIEW_ProcessLetterKeys
1371 static INT LISTVIEW_ProcessLetterKeys(LISTVIEW_INFO *infoPtr, WPARAM charCode, LPARAM keyData)
1373 INT nItem;
1374 INT endidx,idx;
1375 LVITEMW item;
1376 WCHAR buffer[MAX_PATH];
1377 DWORD lastKeyPressTimestamp = infoPtr->lastKeyPressTimestamp;
1379 /* simple parameter checking */
1380 if (!charCode || !keyData) return 0;
1382 /* only allow the valid WM_CHARs through */
1383 if (!isalnum(charCode) &&
1384 charCode != '.' && charCode != '`' && charCode != '!' &&
1385 charCode != '@' && charCode != '#' && charCode != '$' &&
1386 charCode != '%' && charCode != '^' && charCode != '&' &&
1387 charCode != '*' && charCode != '(' && charCode != ')' &&
1388 charCode != '-' && charCode != '_' && charCode != '+' &&
1389 charCode != '=' && charCode != '\\'&& charCode != ']' &&
1390 charCode != '}' && charCode != '[' && charCode != '{' &&
1391 charCode != '/' && charCode != '?' && charCode != '>' &&
1392 charCode != '<' && charCode != ',' && charCode != '~')
1393 return 0;
1395 /* if there's one item or less, there is no where to go */
1396 if (infoPtr->nItemCount <= 1) return 0;
1398 /* update the search parameters */
1399 infoPtr->lastKeyPressTimestamp = GetTickCount();
1400 if (infoPtr->lastKeyPressTimestamp - lastKeyPressTimestamp < KEY_DELAY) {
1401 if (infoPtr->nSearchParamLength < MAX_PATH)
1402 infoPtr->szSearchParam[infoPtr->nSearchParamLength++]=charCode;
1403 if (infoPtr->charCode != charCode)
1404 infoPtr->charCode = charCode = 0;
1405 } else {
1406 infoPtr->charCode=charCode;
1407 infoPtr->szSearchParam[0]=charCode;
1408 infoPtr->nSearchParamLength=1;
1409 /* Redundant with the 1 char string */
1410 charCode=0;
1413 /* and search from the current position */
1414 nItem=-1;
1415 if (infoPtr->nFocusedItem >= 0) {
1416 endidx=infoPtr->nFocusedItem;
1417 idx=endidx;
1418 /* if looking for single character match,
1419 * then we must always move forward
1421 if (infoPtr->nSearchParamLength == 1)
1422 idx++;
1423 } else {
1424 endidx=infoPtr->nItemCount;
1425 idx=0;
1427 do {
1428 if (idx == infoPtr->nItemCount) {
1429 if (endidx == infoPtr->nItemCount || endidx == 0)
1430 break;
1431 idx=0;
1434 /* get item */
1435 item.mask = LVIF_TEXT;
1436 item.iItem = idx;
1437 item.iSubItem = 0;
1438 item.pszText = buffer;
1439 item.cchTextMax = MAX_PATH;
1440 if (!LISTVIEW_GetItemW(infoPtr, &item)) return 0;
1442 /* check for a match */
1443 if (lstrncmpiW(item.pszText,infoPtr->szSearchParam,infoPtr->nSearchParamLength) == 0) {
1444 nItem=idx;
1445 break;
1446 } else if ( (charCode != 0) && (nItem == -1) && (nItem != infoPtr->nFocusedItem) &&
1447 (lstrncmpiW(item.pszText,infoPtr->szSearchParam,1) == 0) ) {
1448 /* This would work but we must keep looking for a longer match */
1449 nItem=idx;
1451 idx++;
1452 } while (idx != endidx);
1454 if (nItem != -1)
1455 LISTVIEW_KeySelection(infoPtr, nItem);
1457 return 0;
1460 /*************************************************************************
1461 * LISTVIEW_UpdateHeaderSize [Internal]
1463 * Function to resize the header control
1465 * PARAMS
1466 * [I] hwnd : handle to a window
1467 * [I] nNewScrollPos : scroll pos to set
1469 * RETURNS
1470 * None.
1472 static void LISTVIEW_UpdateHeaderSize(LISTVIEW_INFO *infoPtr, INT nNewScrollPos)
1474 RECT winRect;
1475 POINT point[2];
1477 TRACE("nNewScrollPos=%d\n", nNewScrollPos);
1479 GetWindowRect(infoPtr->hwndHeader, &winRect);
1480 point[0].x = winRect.left;
1481 point[0].y = winRect.top;
1482 point[1].x = winRect.right;
1483 point[1].y = winRect.bottom;
1485 MapWindowPoints(HWND_DESKTOP, infoPtr->hwndSelf, point, 2);
1486 point[0].x = -nNewScrollPos;
1487 point[1].x += nNewScrollPos;
1489 SetWindowPos(infoPtr->hwndHeader,0,
1490 point[0].x,point[0].y,point[1].x,point[1].y,
1491 SWP_NOZORDER | SWP_NOACTIVATE);
1494 /***
1495 * DESCRIPTION:
1496 * Update the scrollbars. This functions should be called whenever
1497 * the content, size or view changes.
1499 * PARAMETER(S):
1500 * [I] infoPtr : valid pointer to the listview structure
1502 * RETURN:
1503 * None
1505 static void LISTVIEW_UpdateScroll(LISTVIEW_INFO *infoPtr)
1507 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
1508 SCROLLINFO horzInfo, vertInfo;
1510 if ((infoPtr->dwStyle & LVS_NOSCROLL) || !is_redrawing(infoPtr)) return;
1512 ZeroMemory(&horzInfo, sizeof(SCROLLINFO));
1513 horzInfo.cbSize = sizeof(SCROLLINFO);
1514 horzInfo.nPage = infoPtr->rcList.right - infoPtr->rcList.left;
1516 /* for now, we'll set info.nMax to the _count_, and adjust it later */
1517 if (uView == LVS_LIST)
1519 INT nPerCol = LISTVIEW_GetCountPerColumn(infoPtr);
1520 horzInfo.nMax = (infoPtr->nItemCount + nPerCol - 1) / nPerCol;
1521 horzInfo.nPage /= infoPtr->nItemWidth;
1523 else if (uView == LVS_REPORT)
1525 horzInfo.nMax = infoPtr->nItemWidth;
1527 else /* LVS_ICON, or LVS_SMALLICON */
1529 RECT rcView;
1531 if (LISTVIEW_GetViewRect(infoPtr, &rcView)) horzInfo.nMax = rcView.right - rcView.left;
1534 horzInfo.fMask = SIF_RANGE | SIF_PAGE;
1535 horzInfo.nMax = max(horzInfo.nMax - 1, 0);
1536 SetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &horzInfo, TRUE);
1537 TRACE("horzInfo=%s\n", debugscrollinfo(&horzInfo));
1539 /* Setting the horizontal scroll can change the listview size
1540 * (and potentially everything else) so we need to recompute
1541 * everything again for the vertical scroll
1544 ZeroMemory(&vertInfo, sizeof(SCROLLINFO));
1545 vertInfo.cbSize = sizeof(SCROLLINFO);
1546 vertInfo.nPage = infoPtr->rcList.bottom - infoPtr->rcList.top;
1548 if (uView == LVS_REPORT)
1550 vertInfo.nMax = infoPtr->nItemCount;
1551 vertInfo.nPage /= infoPtr->nItemHeight;
1553 else if (uView != LVS_LIST) /* LVS_ICON, or LVS_SMALLICON */
1555 RECT rcView;
1557 if (LISTVIEW_GetViewRect(infoPtr, &rcView)) vertInfo.nMax = rcView.bottom - rcView.top;
1560 vertInfo.fMask = SIF_RANGE | SIF_PAGE;
1561 vertInfo.nMax = max(vertInfo.nMax - 1, 0);
1562 SetScrollInfo(infoPtr->hwndSelf, SB_VERT, &vertInfo, TRUE);
1563 TRACE("vertInfo=%s\n", debugscrollinfo(&vertInfo));
1565 /* Update the Header Control */
1566 if (uView == LVS_REPORT)
1568 horzInfo.fMask = SIF_POS;
1569 GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &horzInfo);
1570 LISTVIEW_UpdateHeaderSize(infoPtr, horzInfo.nPos);
1575 /***
1576 * DESCRIPTION:
1577 * Shows/hides the focus rectangle.
1579 * PARAMETER(S):
1580 * [I] infoPtr : valid pointer to the listview structure
1581 * [I] fShow : TRUE to show the focus, FALSE to hide it.
1583 * RETURN:
1584 * None
1586 static void LISTVIEW_ShowFocusRect(LISTVIEW_INFO *infoPtr, BOOL fShow)
1588 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
1589 HDC hdc;
1591 TRACE("fShow=%d, nItem=%d\n", fShow, infoPtr->nFocusedItem);
1593 if (infoPtr->nFocusedItem < 0) return;
1595 /* we need some gymnastics in ICON mode to handle large items */
1596 if ( (infoPtr->dwStyle & LVS_TYPEMASK) == LVS_ICON )
1598 RECT rcBox;
1600 LISTVIEW_GetItemBox(infoPtr, infoPtr->nFocusedItem, &rcBox);
1601 if ((rcBox.bottom - rcBox.top) > infoPtr->nItemHeight)
1603 LISTVIEW_InvalidateRect(infoPtr, &rcBox);
1604 return;
1608 if (!(hdc = GetDC(infoPtr->hwndSelf))) return;
1610 /* for some reason, owner draw should work only in report mode */
1611 if ((infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && (uView == LVS_REPORT))
1613 DRAWITEMSTRUCT dis;
1614 LVITEMW item;
1616 item.iItem = infoPtr->nFocusedItem;
1617 item.iSubItem = 0;
1618 item.mask = LVIF_PARAM;
1619 if (!LISTVIEW_GetItemW(infoPtr, &item)) goto done;
1621 ZeroMemory(&dis, sizeof(dis));
1622 dis.CtlType = ODT_LISTVIEW;
1623 dis.CtlID = GetWindowLongW(infoPtr->hwndSelf, GWL_ID);
1624 dis.itemID = item.iItem;
1625 dis.itemAction = ODA_FOCUS;
1626 if (fShow) dis.itemState |= ODS_FOCUS;
1627 dis.hwndItem = infoPtr->hwndSelf;
1628 dis.hDC = hdc;
1629 LISTVIEW_GetItemBox(infoPtr, dis.itemID, &dis.rcItem);
1630 dis.itemData = item.lParam;
1632 SendMessageW(GetParent(infoPtr->hwndSelf), WM_DRAWITEM, dis.CtlID, (LPARAM)&dis);
1634 else
1636 DrawFocusRect(hdc, &infoPtr->rcFocus);
1638 done:
1639 ReleaseDC(infoPtr->hwndSelf, hdc);
1642 /***
1643 * Invalidates all visible selected items.
1645 static void LISTVIEW_InvalidateSelectedItems(LISTVIEW_INFO *infoPtr)
1647 ITERATOR i;
1649 iterator_frameditems(&i, infoPtr, &infoPtr->rcList);
1650 while(iterator_next(&i))
1652 if (LISTVIEW_GetItemState(infoPtr, i.nItem, LVIS_SELECTED))
1653 LISTVIEW_InvalidateItem(infoPtr, i.nItem);
1655 iterator_destroy(&i);
1659 /***
1660 * DESCRIPTION: [INTERNAL]
1661 * Computes an item's (left,top) corner, relative to rcView.
1662 * That is, the position has NOT been made relative to the Origin.
1663 * This is deliberate, to avoid computing the Origin over, and
1664 * over again, when this function is call in a loop. Instead,
1665 * one ca factor the computation of the Origin before the loop,
1666 * and offset the value retured by this function, on every iteration.
1668 * PARAMETER(S):
1669 * [I] infoPtr : valid pointer to the listview structure
1670 * [I] nItem : item number
1671 * [O] lpptOrig : item top, left corner
1673 * RETURN:
1674 * None.
1676 static void LISTVIEW_GetItemOrigin(LISTVIEW_INFO *infoPtr, INT nItem, LPPOINT lpptPosition)
1678 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
1680 assert(nItem >= 0 && nItem < infoPtr->nItemCount);
1682 if ((uView == LVS_SMALLICON) || (uView == LVS_ICON))
1684 lpptPosition->x = (LONG)DPA_GetPtr(infoPtr->hdpaPosX, nItem);
1685 lpptPosition->y = (LONG)DPA_GetPtr(infoPtr->hdpaPosY, nItem);
1687 else if (uView == LVS_LIST)
1689 INT nCountPerColumn = LISTVIEW_GetCountPerColumn(infoPtr);
1690 lpptPosition->x = nItem / nCountPerColumn * infoPtr->nItemWidth;
1691 lpptPosition->y = nItem % nCountPerColumn * infoPtr->nItemHeight;
1693 else /* LVS_REPORT */
1695 lpptPosition->x = REPORT_MARGINX;
1696 lpptPosition->y = nItem * infoPtr->nItemHeight;
1700 /***
1701 * DESCRIPTION: [INTERNAL]
1702 * Compute the rectangles of an item. This is to localize all
1703 * the computations in one place. If you are not interested in some
1704 * of these values, simply pass in a NULL -- the fucntion is smart
1705 * enough to compute only what's necessary. The function computes
1706 * the standard rectangles (BOUNDS, ICON, LABEL) plus a non-standard
1707 * one, the BOX rectangle. This rectangle is very cheap to compute,
1708 * and is guaranteed to contain all the other rectangles. Computing
1709 * the ICON rect is also cheap, but all the others are potentaily
1710 * expensive. This gives an easy and effective optimization when
1711 * searching (like point inclusion, or rectangle intersection):
1712 * first test against the BOX, and if TRUE, test agains the desired
1713 * rectangle.
1714 * If the function does not have all the necessary information
1715 * to computed the requested rectangles, will crash with a
1716 * failed assertion. This is done so we catch all programming
1717 * errors, given that the function is called only from our code.
1719 * We have the following 'special' meanings for a few fields:
1720 * * If LVIS_FOCUSED is set, we assume the item has the focus
1721 * This is important in ICON mode, where it might get a larger
1722 * then usual rectange
1724 * Please note that subitem support works only in REPORT mode.
1726 * PARAMETER(S):
1727 * [I] infoPtr : valid pointer to the listview structure
1728 * [I] lpLVItem : item to compute the measures for
1729 * [O] lprcBox : ptr to Box rectangle
1730 * The internal LVIR_BOX rectangle
1731 * [0] lprcState : ptr to State icon rectangle
1732 * The internal LVIR_STATE rectangle
1733 * [O] lprcIcon : ptr to Icon rectangle
1734 * Same as LVM_GETITEMRECT with LVIR_ICON
1735 * [O] lprcLabel : ptr to Label rectangle
1736 * Same as LVM_GETITEMRECT with LVIR_LABEL
1738 * RETURN:
1739 * None.
1741 static void LISTVIEW_GetItemMetrics(LISTVIEW_INFO *infoPtr, const LVITEMW *lpLVItem,
1742 LPRECT lprcBox, LPRECT lprcState,
1743 LPRECT lprcIcon, LPRECT lprcLabel)
1745 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
1746 BOOL doState = FALSE, doIcon = FALSE, doLabel = FALSE, oversizedBox = FALSE;
1747 RECT Box, State, Icon, Label;
1748 COLUMN_INFO *lpColumnInfo = NULL;
1750 TRACE("(lpLVItem=%s)\n", debuglvitem_t(lpLVItem, TRUE));
1752 /* Be smart and try to figure out the minimum we have to do */
1753 if (lpLVItem->iSubItem) assert(uView == LVS_REPORT);
1754 if (uView == LVS_ICON && (lprcBox || lprcLabel))
1756 assert((lpLVItem->mask & LVIF_STATE) && (lpLVItem->stateMask & LVIS_FOCUSED));
1757 if (lpLVItem->state & LVIS_FOCUSED) oversizedBox = doLabel = TRUE;
1759 if (lprcLabel) doLabel = TRUE;
1760 if (doLabel || lprcIcon) doIcon = TRUE;
1761 if (doIcon || lprcState) doState = TRUE;
1763 /************************************************************/
1764 /* compute the box rectangle (it should be cheap to do) */
1765 /************************************************************/
1766 if (lpLVItem->iSubItem || uView == LVS_REPORT)
1767 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, lpLVItem->iSubItem);
1769 if (lpLVItem->iSubItem)
1771 Box = lpColumnInfo->rcHeader;
1773 else
1775 Box.left = 0;
1776 Box.right = infoPtr->nItemWidth;
1778 Box.top = 0;
1779 Box.bottom = infoPtr->nItemHeight;
1781 /************************************************************/
1782 /* compute STATEICON bounding box */
1783 /************************************************************/
1784 if (doState)
1786 if (uView == LVS_ICON)
1788 State.left = Box.left - infoPtr->iconStateSize.cx - 2;
1789 if (infoPtr->himlNormal)
1790 State.left += (infoPtr->nItemWidth - infoPtr->iconSize.cx) / 2;
1791 State.top = Box.top + infoPtr->iconSize.cy - infoPtr->iconStateSize.cy + 4;
1793 else
1795 /* we need the ident in report mode, if we don't have it, we fail */
1796 State.left = Box.left;
1797 if (uView == LVS_REPORT)
1799 State.left += REPORT_MARGINX;
1800 if (lpLVItem->iSubItem == 0)
1802 assert(lpLVItem->mask & LVIF_INDENT);
1803 State.left += infoPtr->iconSize.cx * lpLVItem->iIndent;
1806 State.top = Box.top;
1808 State.right = State.left;
1809 State.bottom = State.top;
1810 if (infoPtr->himlState && lpLVItem->iSubItem == 0)
1812 State.right += infoPtr->iconStateSize.cx;
1813 State.bottom += infoPtr->iconStateSize.cy;
1815 if (lprcState) *lprcState = State;
1816 TRACE(" - state=%s\n", debugrect(&State));
1819 /************************************************************/
1820 /* compute ICON bounding box (ala LVM_GETITEMRECT) */
1821 /************************************************************/
1822 if (doIcon)
1824 if (uView == LVS_ICON)
1826 Icon.left = Box.left;
1827 if (infoPtr->himlNormal)
1828 Icon.left += (infoPtr->nItemWidth - infoPtr->iconSize.cx) / 2;
1829 Icon.top = Box.top + ICON_TOP_PADDING;
1830 Icon.right = Icon.left;
1831 Icon.bottom = Icon.top;
1832 if (infoPtr->himlNormal)
1834 Icon.right += infoPtr->iconSize.cx;
1835 Icon.bottom += infoPtr->iconSize.cy;
1838 else /* LVS_SMALLICON, LVS_LIST or LVS_REPORT */
1840 Icon.left = State.right;
1841 if (!IsRectEmpty(&State)) Icon.left += IMAGE_PADDING;
1842 Icon.top = Box.top;
1843 Icon.right = Icon.left;
1844 if (infoPtr->himlSmall && (!lpColumnInfo || lpLVItem->iSubItem == 0 || (lpColumnInfo->fmt & LVCFMT_IMAGE)))
1845 Icon.right += infoPtr->iconSize.cx;
1846 Icon.bottom = Icon.top + infoPtr->nItemHeight;
1848 if(lprcIcon) *lprcIcon = Icon;
1849 TRACE(" - icon=%s\n", debugrect(&Icon));
1852 /************************************************************/
1853 /* compute LABEL bounding box (ala LVM_GETITEMRECT) */
1854 /************************************************************/
1855 if (doLabel)
1857 SIZE labelSize = { 0, 0 };
1859 /* calculate how far to the right can the label strech */
1860 Label.right = Box.right;
1861 if (uView == LVS_REPORT)
1863 if (lpLVItem->iSubItem == 0) Label = lpColumnInfo->rcHeader;
1864 Label.right -= REPORT_MARGINX;
1867 if (lpLVItem->iSubItem || ((infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && uView == LVS_REPORT))
1869 labelSize.cx = infoPtr->nItemWidth;
1870 labelSize.cy = infoPtr->nItemHeight;
1871 goto calc_label;
1874 /* we need the text in non owner draw mode */
1875 assert(lpLVItem->mask & LVIF_TEXT);
1876 if (is_textT(lpLVItem->pszText, TRUE))
1878 HFONT hFont = infoPtr->hFont ? infoPtr->hFont : infoPtr->hDefaultFont;
1879 HDC hdc = GetDC(infoPtr->hwndSelf);
1880 HFONT hOldFont = SelectObject(hdc, hFont);
1881 UINT uFormat;
1882 RECT rcText;
1884 /* compute rough rectangle where the label will go */
1885 SetRectEmpty(&rcText);
1886 rcText.right = infoPtr->nItemWidth - TRAILING_PADDING;
1887 rcText.bottom = infoPtr->nItemHeight;
1888 if (uView == LVS_ICON)
1889 rcText.bottom -= ICON_TOP_PADDING + infoPtr->iconSize.cy + ICON_BOTTOM_PADDING;
1891 /* now figure out the flags */
1892 if (uView == LVS_ICON)
1893 uFormat = oversizedBox ? LV_FL_DT_FLAGS : LV_ML_DT_FLAGS;
1894 else
1895 uFormat = LV_SL_DT_FLAGS;
1897 DrawTextW (hdc, lpLVItem->pszText, -1, &rcText, uFormat | DT_CALCRECT);
1899 labelSize.cx = min(rcText.right - rcText.left + TRAILING_PADDING, infoPtr->nItemWidth);
1900 labelSize.cy = rcText.bottom - rcText.top;
1902 SelectObject(hdc, hOldFont);
1903 ReleaseDC(infoPtr->hwndSelf, hdc);
1906 calc_label:
1907 if (uView == LVS_ICON)
1909 Label.left = Box.left + (infoPtr->nItemWidth - labelSize.cx) / 2;
1910 Label.top = Box.top + ICON_TOP_PADDING_HITABLE +
1911 infoPtr->iconSize.cy + ICON_BOTTOM_PADDING;
1912 Label.right = Label.left + labelSize.cx;
1913 Label.bottom = Label.top + infoPtr->nItemHeight;
1914 if (!oversizedBox && labelSize.cy > infoPtr->ntmHeight)
1916 labelSize.cy = min(Box.bottom - Label.top, labelSize.cy);
1917 labelSize.cy /= infoPtr->ntmHeight;
1918 labelSize.cy = max(labelSize.cy, 1);
1919 labelSize.cy *= infoPtr->ntmHeight;
1921 Label.bottom = Label.top + labelSize.cy + HEIGHT_PADDING;
1923 else /* LVS_SMALLICON, LVS_LIST or LVS_REPORT */
1925 Label.left = Icon.right;
1926 if (!IsRectEmpty(&Icon) || !IsRectEmpty(&State)) Label.left += IMAGE_PADDING;
1927 Label.top = Box.top;
1928 Label.right = min(Label.left + labelSize.cx, Label.right);
1929 Label.bottom = Label.top + infoPtr->nItemHeight;
1932 if (lprcLabel) *lprcLabel = Label;
1933 TRACE(" - label=%s\n", debugrect(&Label));
1936 /* Fix the Box if necessary */
1937 if (lprcBox)
1939 if (oversizedBox) UnionRect(lprcBox, &Box, &Label);
1940 else *lprcBox = Box;
1942 TRACE(" - box=%s\n", debugrect(&Box));
1945 /***
1946 * DESCRIPTION: [INTERNAL]
1948 * PARAMETER(S):
1949 * [I] infoPtr : valid pointer to the listview structure
1950 * [I] nItem : item number
1951 * [O] lprcBox : ptr to Box rectangle
1953 * RETURN:
1954 * None.
1956 static void LISTVIEW_GetItemBox(LISTVIEW_INFO *infoPtr, INT nItem, LPRECT lprcBox)
1958 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
1959 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
1960 POINT Position, Origin;
1961 LVITEMW lvItem;
1963 LISTVIEW_GetOrigin(infoPtr, &Origin);
1964 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position);
1966 /* Be smart and try to figure out the minimum we have to do */
1967 lvItem.mask = 0;
1968 if (uView == LVS_ICON && infoPtr->bFocus && LISTVIEW_GetItemState(infoPtr, nItem, LVIS_FOCUSED))
1969 lvItem.mask |= LVIF_TEXT;
1970 lvItem.iItem = nItem;
1971 lvItem.iSubItem = 0;
1972 lvItem.pszText = szDispText;
1973 lvItem.cchTextMax = DISP_TEXT_SIZE;
1974 if (lvItem.mask) LISTVIEW_GetItemW(infoPtr, &lvItem);
1975 if (uView == LVS_ICON)
1977 lvItem.mask |= LVIF_STATE;
1978 lvItem.stateMask = LVIS_FOCUSED;
1979 lvItem.state = (lvItem.mask & LVIF_TEXT ? LVIS_FOCUSED : 0);
1981 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, lprcBox, 0, 0, 0);
1983 OffsetRect(lprcBox, Position.x + Origin.x, Position.y + Origin.y);
1987 /***
1988 * DESCRIPTION:
1989 * Returns the current icon position, and advances it along the top.
1990 * The returned position is not offset by Origin.
1992 * PARAMETER(S):
1993 * [I] infoPtr : valid pointer to the listview structure
1994 * [O] lpPos : will get the current icon position
1996 * RETURN:
1997 * None
1999 static void LISTVIEW_NextIconPosTop(LISTVIEW_INFO *infoPtr, LPPOINT lpPos)
2001 INT nListWidth = infoPtr->rcList.right - infoPtr->rcList.left;
2003 *lpPos = infoPtr->currIconPos;
2005 infoPtr->currIconPos.x += infoPtr->nItemWidth;
2006 if (infoPtr->currIconPos.x + infoPtr->nItemWidth <= nListWidth) return;
2008 infoPtr->currIconPos.x = 0;
2009 infoPtr->currIconPos.y += infoPtr->nItemHeight;
2013 /***
2014 * DESCRIPTION:
2015 * Returns the current icon position, and advances it down the left edge.
2016 * The returned position is not offset by Origin.
2018 * PARAMETER(S):
2019 * [I] infoPtr : valid pointer to the listview structure
2020 * [O] lpPos : will get the current icon position
2022 * RETURN:
2023 * None
2025 static void LISTVIEW_NextIconPosLeft(LISTVIEW_INFO *infoPtr, LPPOINT lpPos)
2027 INT nListHeight = infoPtr->rcList.bottom - infoPtr->rcList.top;
2029 *lpPos = infoPtr->currIconPos;
2031 infoPtr->currIconPos.y += infoPtr->nItemHeight;
2032 if (infoPtr->currIconPos.y + infoPtr->nItemHeight <= nListHeight) return;
2034 infoPtr->currIconPos.x += infoPtr->nItemWidth;
2035 infoPtr->currIconPos.y = 0;
2039 /***
2040 * DESCRIPTION:
2041 * Moves an icon to the specified position.
2042 * It takes care of invalidating the item, etc.
2044 * PARAMETER(S):
2045 * [I] infoPtr : valid pointer to the listview structure
2046 * [I] nItem : the item to move
2047 * [I] lpPos : the new icon position
2048 * [I] isNew : flags the item as being new
2050 * RETURN:
2051 * Success: TRUE
2052 * Failure: FALSE
2054 static BOOL LISTVIEW_MoveIconTo(LISTVIEW_INFO *infoPtr, INT nItem, const POINT *lppt, BOOL isNew)
2056 POINT old;
2058 if (!isNew)
2060 old.x = (LONG)DPA_GetPtr(infoPtr->hdpaPosX, nItem);
2061 old.y = (LONG)DPA_GetPtr(infoPtr->hdpaPosY, nItem);
2063 if (lppt->x == old.x && lppt->y == old.y) return TRUE;
2064 LISTVIEW_InvalidateItem(infoPtr, nItem);
2067 /* Allocating a POINTER for every item is too resource intensive,
2068 * so we'll keep the (x,y) in different arrays */
2069 if (!DPA_SetPtr(infoPtr->hdpaPosX, nItem, (void *)lppt->x)) return FALSE;
2070 if (!DPA_SetPtr(infoPtr->hdpaPosY, nItem, (void *)lppt->y)) return FALSE;
2072 LISTVIEW_InvalidateItem(infoPtr, nItem);
2074 return TRUE;
2077 /***
2078 * DESCRIPTION:
2079 * Arranges listview items in icon display mode.
2081 * PARAMETER(S):
2082 * [I] infoPtr : valid pointer to the listview structure
2083 * [I] nAlignCode : alignment code
2085 * RETURN:
2086 * SUCCESS : TRUE
2087 * FAILURE : FALSE
2089 static BOOL LISTVIEW_Arrange(LISTVIEW_INFO *infoPtr, INT nAlignCode)
2091 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
2092 void (*next_pos)(LISTVIEW_INFO *, LPPOINT);
2093 POINT pos;
2094 INT i;
2096 if (uView != LVS_ICON && uView != LVS_SMALLICON) return FALSE;
2098 TRACE("nAlignCode=%d\n", nAlignCode);
2100 if (nAlignCode == LVA_DEFAULT)
2102 if (infoPtr->dwStyle & LVS_ALIGNLEFT) nAlignCode = LVA_ALIGNLEFT;
2103 else nAlignCode = LVA_ALIGNTOP;
2106 switch (nAlignCode)
2108 case LVA_ALIGNLEFT: next_pos = LISTVIEW_NextIconPosLeft; break;
2109 case LVA_ALIGNTOP: next_pos = LISTVIEW_NextIconPosTop; break;
2110 case LVA_SNAPTOGRID: next_pos = LISTVIEW_NextIconPosTop; break; /* FIXME */
2111 default: return FALSE;
2114 infoPtr->bAutoarrange = TRUE;
2115 infoPtr->currIconPos.x = infoPtr->currIconPos.y = 0;
2116 for (i = 0; i < infoPtr->nItemCount; i++)
2118 next_pos(infoPtr, &pos);
2119 LISTVIEW_MoveIconTo(infoPtr, i, &pos, FALSE);
2122 return TRUE;
2125 /***
2126 * DESCRIPTION:
2127 * Retrieves the bounding rectangle of all the items, not offset by Origin.
2129 * PARAMETER(S):
2130 * [I] infoPtr : valid pointer to the listview structure
2131 * [O] lprcView : bounding rectangle
2133 * RETURN:
2134 * SUCCESS : TRUE
2135 * FAILURE : FALSE
2137 static void LISTVIEW_GetAreaRect(LISTVIEW_INFO *infoPtr, LPRECT lprcView)
2139 INT i, x, y;
2141 SetRectEmpty(lprcView);
2143 switch (infoPtr->dwStyle & LVS_TYPEMASK)
2145 case LVS_ICON:
2146 case LVS_SMALLICON:
2147 for (i = 0; i < infoPtr->nItemCount; i++)
2149 x = (LONG)DPA_GetPtr(infoPtr->hdpaPosX, i);
2150 y = (LONG)DPA_GetPtr(infoPtr->hdpaPosX, i);
2151 lprcView->right = max(lprcView->right, x);
2152 lprcView->bottom = max(lprcView->bottom, y);
2154 if (infoPtr->nItemCount > 0)
2156 lprcView->right += infoPtr->nItemWidth;
2157 lprcView->bottom += infoPtr->nItemHeight;
2159 break;
2161 case LVS_LIST:
2162 y = LISTVIEW_GetCountPerColumn(infoPtr);
2163 x = infoPtr->nItemCount / y;
2164 if (infoPtr->nItemCount % y) x++;
2165 lprcView->right = x * infoPtr->nItemWidth;
2166 lprcView->bottom = y * infoPtr->nItemHeight;
2167 break;
2169 case LVS_REPORT:
2170 lprcView->right = infoPtr->nItemWidth;
2171 lprcView->bottom = infoPtr->nItemCount * infoPtr->nItemHeight;
2172 break;
2176 /***
2177 * DESCRIPTION:
2178 * Retrieves the bounding rectangle of all the items.
2180 * PARAMETER(S):
2181 * [I] infoPtr : valid pointer to the listview structure
2182 * [O] lprcView : bounding rectangle
2184 * RETURN:
2185 * SUCCESS : TRUE
2186 * FAILURE : FALSE
2188 static BOOL LISTVIEW_GetViewRect(LISTVIEW_INFO *infoPtr, LPRECT lprcView)
2190 POINT ptOrigin;
2192 TRACE("(lprcView=%p)\n", lprcView);
2194 if (!lprcView) return FALSE;
2196 LISTVIEW_GetOrigin(infoPtr, &ptOrigin);
2197 LISTVIEW_GetAreaRect(infoPtr, lprcView);
2198 OffsetRect(lprcView, ptOrigin.x, ptOrigin.y);
2200 TRACE("lprcView=%s\n", debugrect(lprcView));
2202 return TRUE;
2205 /***
2206 * DESCRIPTION:
2207 * Retrieves the subitem pointer associated with the subitem index.
2209 * PARAMETER(S):
2210 * [I] hdpaSubItems : DPA handle for a specific item
2211 * [I] nSubItem : index of subitem
2213 * RETURN:
2214 * SUCCESS : subitem pointer
2215 * FAILURE : NULL
2217 static SUBITEM_INFO* LISTVIEW_GetSubItemPtr(HDPA hdpaSubItems, INT nSubItem)
2219 SUBITEM_INFO *lpSubItem;
2220 INT i;
2222 /* we should binary search here if need be */
2223 for (i = 1; i < hdpaSubItems->nItemCount; i++)
2225 lpSubItem = (SUBITEM_INFO *)DPA_GetPtr(hdpaSubItems, i);
2226 if (lpSubItem->iSubItem == nSubItem)
2227 return lpSubItem;
2230 return NULL;
2234 /***
2235 * DESCRIPTION:
2236 * Caclulates the desired item width.
2238 * PARAMETER(S):
2239 * [I] infoPtr : valid pointer to the listview structure
2241 * RETURN:
2242 * The desired item width.
2244 static INT LISTVIEW_CalculateItemWidth(LISTVIEW_INFO *infoPtr)
2246 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
2247 INT nItemWidth = 0;
2249 TRACE("uView=%d\n", uView);
2251 if (uView == LVS_ICON)
2252 nItemWidth = infoPtr->iconSpacing.cx;
2253 else if (uView == LVS_REPORT)
2255 RECT rcHeader;
2257 if (infoPtr->hdpaColumns->nItemCount > 0)
2259 LISTVIEW_GetHeaderRect(infoPtr, infoPtr->hdpaColumns->nItemCount - 1, &rcHeader);
2260 nItemWidth = rcHeader.right;
2263 else /* LVS_SMALLICON, or LVS_LIST */
2265 INT i;
2267 for (i = 0; i < infoPtr->nItemCount; i++)
2268 nItemWidth = max(LISTVIEW_GetLabelWidth(infoPtr, i), nItemWidth);
2270 if (infoPtr->himlSmall) nItemWidth += infoPtr->iconSize.cx + IMAGE_PADDING;
2271 if (infoPtr->himlState) nItemWidth += infoPtr->iconStateSize.cx + IMAGE_PADDING;
2273 nItemWidth = max(DEFAULT_COLUMN_WIDTH, nItemWidth + WIDTH_PADDING);
2276 return max(nItemWidth, 1);
2279 /***
2280 * DESCRIPTION:
2281 * Caclulates the desired item height.
2283 * PARAMETER(S):
2284 * [I] infoPtr : valid pointer to the listview structure
2286 * RETURN:
2287 * The desired item height.
2289 static INT LISTVIEW_CalculateItemHeight(LISTVIEW_INFO *infoPtr)
2291 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
2292 INT nItemHeight;
2294 TRACE("uView=%d\n", uView);
2296 if (uView == LVS_ICON)
2297 nItemHeight = infoPtr->iconSpacing.cy;
2298 else
2300 nItemHeight = infoPtr->ntmHeight;
2301 if (infoPtr->himlState)
2302 nItemHeight = max(nItemHeight, infoPtr->iconStateSize.cy);
2303 if (infoPtr->himlSmall)
2304 nItemHeight = max(nItemHeight, infoPtr->iconSize.cy);
2305 if (infoPtr->himlState || infoPtr->himlSmall)
2306 nItemHeight += HEIGHT_PADDING;
2309 return max(nItemHeight, 1);
2312 /***
2313 * DESCRIPTION:
2314 * Updates the width, and height of an item.
2316 * PARAMETER(S):
2317 * [I] infoPtr : valid pointer to the listview structure
2319 * RETURN:
2320 * None.
2322 static inline void LISTVIEW_UpdateItemSize(LISTVIEW_INFO *infoPtr)
2324 infoPtr->nItemWidth = LISTVIEW_CalculateItemWidth(infoPtr);
2325 infoPtr->nItemHeight = LISTVIEW_CalculateItemHeight(infoPtr);
2329 /***
2330 * DESCRIPTION:
2331 * Retrieves and saves important text metrics info for the current
2332 * Listview font.
2334 * PARAMETER(S):
2335 * [I] infoPtr : valid pointer to the listview structure
2338 static void LISTVIEW_SaveTextMetrics(LISTVIEW_INFO *infoPtr)
2340 HDC hdc = GetDC(infoPtr->hwndSelf);
2341 HFONT hFont = infoPtr->hFont ? infoPtr->hFont : infoPtr->hDefaultFont;
2342 HFONT hOldFont = SelectObject(hdc, hFont);
2343 TEXTMETRICW tm;
2345 if (GetTextMetricsW(hdc, &tm))
2347 infoPtr->ntmHeight = tm.tmHeight;
2348 infoPtr->ntmAveCharWidth = tm.tmAveCharWidth;
2350 SelectObject(hdc, hOldFont);
2351 ReleaseDC(infoPtr->hwndSelf, hdc);
2353 TRACE("tmHeight=%d\n", infoPtr->ntmHeight);
2356 /***
2357 * DESCRIPTION:
2358 * A compare function for ranges
2360 * PARAMETER(S)
2361 * [I] range1 : pointer to range 1;
2362 * [I] range2 : pointer to range 2;
2363 * [I] flags : flags
2365 * RETURNS:
2366 * > 0 : if range 1 > range 2
2367 * < 0 : if range 2 > range 1
2368 * = 0 : if range intersects range 2
2370 static INT CALLBACK ranges_cmp(LPVOID range1, LPVOID range2, LPARAM flags)
2372 INT cmp;
2374 if (((RANGE*)range1)->upper <= ((RANGE*)range2)->lower)
2375 cmp = -1;
2376 else if (((RANGE*)range2)->upper <= ((RANGE*)range1)->lower)
2377 cmp = 1;
2378 else
2379 cmp = 0;
2381 TRACE("range1=%s, range2=%s, cmp=%d\n", debugrange((RANGE*)range1), debugrange((RANGE*)range2), cmp);
2383 return cmp;
2386 #if DEBUG_RANGES
2387 #define ranges_check(ranges, desc) ranges_assert(ranges, desc, __FUNCTION__, __LINE__)
2388 #else
2389 #define ranges_check(ranges, desc) do { } while(0)
2390 #endif
2392 static void ranges_assert(RANGES ranges, LPCSTR desc, const char *func, int line)
2394 INT i;
2395 RANGE *prev, *curr;
2397 TRACE("*** Checking %s:%d:%s ***\n", func, line, desc);
2398 assert (ranges);
2399 assert (ranges->hdpa->nItemCount >= 0);
2400 ranges_dump(ranges);
2401 prev = (RANGE *)DPA_GetPtr(ranges->hdpa, 0);
2402 if (ranges->hdpa->nItemCount > 0)
2403 assert (prev->lower >= 0 && prev->lower < prev->upper);
2404 for (i = 1; i < ranges->hdpa->nItemCount; i++)
2406 curr = (RANGE *)DPA_GetPtr(ranges->hdpa, i);
2407 assert (prev->upper <= curr->lower);
2408 assert (curr->lower < curr->upper);
2409 prev = curr;
2411 TRACE("--- Done checking---\n");
2414 static RANGES ranges_create(int count)
2416 RANGES ranges = (RANGES)COMCTL32_Alloc(sizeof(struct tagRANGES));
2417 if (!ranges) return NULL;
2418 ranges->hdpa = DPA_Create(count);
2419 if (ranges->hdpa) return ranges;
2420 COMCTL32_Free(ranges);
2421 return NULL;
2424 static void ranges_clear(RANGES ranges)
2426 INT i;
2428 for(i = 0; i < ranges->hdpa->nItemCount; i++)
2429 COMCTL32_Free(DPA_GetPtr(ranges->hdpa, i));
2430 DPA_DeleteAllPtrs(ranges->hdpa);
2434 static void ranges_destroy(RANGES ranges)
2436 ranges_clear(ranges);
2437 DPA_Destroy(ranges->hdpa);
2438 COMCTL32_Free(ranges);
2441 static RANGES ranges_clone(RANGES ranges)
2443 RANGES clone;
2444 INT i;
2446 if (!(clone = ranges_create(ranges->hdpa->nItemCount))) goto fail;
2448 for (i = 0; i < ranges->hdpa->nItemCount; i++)
2450 RANGE *newrng = (RANGE *)COMCTL32_Alloc(sizeof(RANGE));
2451 if (!newrng) goto fail;
2452 *newrng = *((RANGE*)DPA_GetPtr(ranges->hdpa, i));
2453 DPA_SetPtr(clone->hdpa, i, newrng);
2455 return clone;
2457 fail:
2458 TRACE ("clone failed\n");
2459 if (clone) ranges_destroy(clone);
2460 return NULL;
2463 static RANGES ranges_diff(RANGES ranges, RANGES sub)
2465 INT i;
2467 for (i = 0; i < sub->hdpa->nItemCount; i++)
2468 ranges_del(ranges, *((RANGE *)DPA_GetPtr(sub->hdpa, i)));
2470 return ranges;
2473 static void ranges_dump(RANGES ranges)
2475 INT i;
2477 for (i = 0; i < ranges->hdpa->nItemCount; i++)
2478 TRACE(" %s\n", debugrange(DPA_GetPtr(ranges->hdpa, i)));
2481 static inline BOOL ranges_contain(RANGES ranges, INT nItem)
2483 RANGE srchrng = { nItem, nItem + 1 };
2485 TRACE("(nItem=%d)\n", nItem);
2486 ranges_check(ranges, "before contain");
2487 return DPA_Search(ranges->hdpa, &srchrng, 0, ranges_cmp, 0, DPAS_SORTED) != -1;
2490 static INT ranges_itemcount(RANGES ranges)
2492 INT i, count = 0;
2494 for (i = 0; i < ranges->hdpa->nItemCount; i++)
2496 RANGE *sel = DPA_GetPtr(ranges->hdpa, i);
2497 count += sel->upper - sel->lower;
2500 return count;
2503 static BOOL ranges_shift(RANGES ranges, INT nItem, INT delta, INT nUpper)
2505 RANGE srchrng = { nItem, nItem + 1 }, *chkrng;
2506 INT index;
2508 index = DPA_Search(ranges->hdpa, &srchrng, 0, ranges_cmp, 0, DPAS_SORTED | DPAS_INSERTAFTER);
2509 if (index == -1) return TRUE;
2511 for (; index < ranges->hdpa->nItemCount; index++)
2513 chkrng = DPA_GetPtr(ranges->hdpa, index);
2514 if (chkrng->lower >= nItem)
2515 chkrng->lower = max(min(chkrng->lower + delta, nUpper - 1), 0);
2516 if (chkrng->upper > nItem)
2517 chkrng->upper = max(min(chkrng->upper + delta, nUpper), 0);
2519 return TRUE;
2522 static BOOL ranges_add(RANGES ranges, RANGE range)
2524 RANGE srchrgn;
2525 INT index;
2527 TRACE("(%s)\n", debugrange(&range));
2528 ranges_check(ranges, "before add");
2530 /* try find overlapping regions first */
2531 srchrgn.lower = range.lower - 1;
2532 srchrgn.upper = range.upper + 1;
2533 index = DPA_Search(ranges->hdpa, &srchrgn, 0, ranges_cmp, 0, DPAS_SORTED);
2535 if (index == -1)
2537 RANGE *newrgn;
2539 TRACE("Adding new range\n");
2541 /* create the brand new range to insert */
2542 newrgn = (RANGE *)COMCTL32_Alloc(sizeof(RANGE));
2543 if(!newrgn) goto fail;
2544 *newrgn = range;
2546 /* figure out where to insert it */
2547 index = DPA_Search(ranges->hdpa, newrgn, 0, ranges_cmp, 0, DPAS_SORTED | DPAS_INSERTAFTER);
2548 TRACE("index=%d\n", index);
2549 if (index == -1) index = 0;
2551 /* and get it over with */
2552 if (DPA_InsertPtr(ranges->hdpa, index, newrgn) == -1)
2554 COMCTL32_Free(newrgn);
2555 goto fail;
2558 else
2560 RANGE *chkrgn, *mrgrgn;
2561 INT fromindex, mergeindex;
2563 chkrgn = DPA_GetPtr(ranges->hdpa, index);
2564 TRACE("Merge with %s @%d\n", debugrange(chkrgn), index);
2566 chkrgn->lower = min(range.lower, chkrgn->lower);
2567 chkrgn->upper = max(range.upper, chkrgn->upper);
2569 TRACE("New range %s @%d\n", debugrange(chkrgn), index);
2571 /* merge now common anges */
2572 fromindex = 0;
2573 srchrgn.lower = chkrgn->lower - 1;
2574 srchrgn.upper = chkrgn->upper + 1;
2578 mergeindex = DPA_Search(ranges->hdpa, &srchrgn, fromindex, ranges_cmp, 0, 0);
2579 if (mergeindex == -1) break;
2580 if (mergeindex == index)
2582 fromindex = index + 1;
2583 continue;
2586 TRACE("Merge with index %i\n", mergeindex);
2588 mrgrgn = DPA_GetPtr(ranges->hdpa, mergeindex);
2589 chkrgn->lower = min(chkrgn->lower, mrgrgn->lower);
2590 chkrgn->upper = max(chkrgn->upper, mrgrgn->upper);
2591 COMCTL32_Free(mrgrgn);
2592 DPA_DeletePtr(ranges->hdpa, mergeindex);
2593 if (mergeindex < index) index --;
2594 } while(1);
2597 ranges_check(ranges, "after add");
2598 return TRUE;
2600 fail:
2601 ranges_check(ranges, "failed add");
2602 return FALSE;
2605 static BOOL ranges_del(RANGES ranges, RANGE range)
2607 RANGE *chkrgn;
2608 INT index;
2610 TRACE("(%s)\n", debugrange(&range));
2611 ranges_check(ranges, "before del");
2613 /* we don't use DPAS_SORTED here, since we need *
2614 * to find the first overlapping range */
2615 index = DPA_Search(ranges->hdpa, &range, 0, ranges_cmp, 0, 0);
2616 while(index != -1)
2618 chkrgn = DPA_GetPtr(ranges->hdpa, index);
2620 TRACE("Matches range %s @%d\n", debugrange(chkrgn), index);
2622 /* case 1: Same range */
2623 if ( (chkrgn->upper == range.upper) &&
2624 (chkrgn->lower == range.lower) )
2626 DPA_DeletePtr(ranges->hdpa, index);
2627 break;
2629 /* case 2: engulf */
2630 else if ( (chkrgn->upper <= range.upper) &&
2631 (chkrgn->lower >= range.lower) )
2633 DPA_DeletePtr(ranges->hdpa, index);
2635 /* case 3: overlap upper */
2636 else if ( (chkrgn->upper <= range.upper) &&
2637 (chkrgn->lower < range.lower) )
2639 chkrgn->upper = range.lower;
2641 /* case 4: overlap lower */
2642 else if ( (chkrgn->upper > range.upper) &&
2643 (chkrgn->lower >= range.lower) )
2645 chkrgn->lower = range.upper;
2646 break;
2648 /* case 5: fully internal */
2649 else
2651 RANGE tmprgn = *chkrgn, *newrgn;
2653 if (!(newrgn = (RANGE *)COMCTL32_Alloc(sizeof(RANGE)))) goto fail;
2654 newrgn->lower = chkrgn->lower;
2655 newrgn->upper = range.lower;
2656 chkrgn->lower = range.upper;
2657 if (DPA_InsertPtr(ranges->hdpa, index, newrgn) == -1)
2659 COMCTL32_Free(newrgn);
2660 goto fail;
2662 chkrgn = &tmprgn;
2663 break;
2666 index = DPA_Search(ranges->hdpa, &range, index, ranges_cmp, 0, 0);
2669 ranges_check(ranges, "after del");
2670 return TRUE;
2672 fail:
2673 ranges_check(ranges, "failed del");
2674 return FALSE;
2677 /***
2678 * DESCRIPTION:
2679 * Removes all selection ranges
2681 * Parameters(s):
2682 * [I] infoPtr : valid pointer to the listview structure
2683 * [I] toSkip : item range to skip removing the selection
2685 * RETURNS:
2686 * SUCCESS : TRUE
2687 * FAILURE : TRUE
2689 static BOOL LISTVIEW_DeselectAllSkipItems(LISTVIEW_INFO *infoPtr, RANGES toSkip)
2691 LVITEMW lvItem;
2692 ITERATOR i;
2693 RANGES clone;
2695 TRACE("()\n");
2697 lvItem.state = 0;
2698 lvItem.stateMask = LVIS_SELECTED;
2700 /* need to clone the DPA because callbacks can change it */
2701 if (!(clone = ranges_clone(infoPtr->selectionRanges))) return FALSE;
2702 iterator_rangesitems(&i, ranges_diff(clone, toSkip));
2703 while(iterator_next(&i))
2704 LISTVIEW_SetItemState(infoPtr, i.nItem, &lvItem);
2705 /* note that the iterator destructor will free the cloned range */
2706 iterator_destroy(&i);
2708 return TRUE;
2711 static inline BOOL LISTVIEW_DeselectAllSkipItem(LISTVIEW_INFO *infoPtr, INT nItem)
2713 RANGES toSkip;
2715 if (!(toSkip = ranges_create(1))) return FALSE;
2716 if (nItem != -1) ranges_additem(toSkip, nItem);
2717 LISTVIEW_DeselectAllSkipItems(infoPtr, toSkip);
2718 ranges_destroy(toSkip);
2719 return TRUE;
2722 static inline BOOL LISTVIEW_DeselectAll(LISTVIEW_INFO *infoPtr)
2724 return LISTVIEW_DeselectAllSkipItem(infoPtr, -1);
2727 /***
2728 * DESCRIPTION:
2729 * Retrieves the number of items that are marked as selected.
2731 * PARAMETER(S):
2732 * [I] infoPtr : valid pointer to the listview structure
2734 * RETURN:
2735 * Number of items selected.
2737 static INT LISTVIEW_GetSelectedCount(LISTVIEW_INFO *infoPtr)
2739 INT nSelectedCount = 0;
2741 if (infoPtr->uCallbackMask & LVIS_SELECTED)
2743 INT i;
2744 for (i = 0; i < infoPtr->nItemCount; i++)
2746 if (LISTVIEW_GetItemState(infoPtr, i, LVIS_SELECTED))
2747 nSelectedCount++;
2750 else
2751 nSelectedCount = ranges_itemcount(infoPtr->selectionRanges);
2753 TRACE("nSelectedCount=%d\n", nSelectedCount);
2754 return nSelectedCount;
2757 /***
2758 * DESCRIPTION:
2759 * Manages the item focus.
2761 * PARAMETER(S):
2762 * [I] infoPtr : valid pointer to the listview structure
2763 * [I] nItem : item index
2765 * RETURN:
2766 * TRUE : focused item changed
2767 * FALSE : focused item has NOT changed
2769 static inline BOOL LISTVIEW_SetItemFocus(LISTVIEW_INFO *infoPtr, INT nItem)
2771 INT oldFocus = infoPtr->nFocusedItem;
2772 LVITEMW lvItem;
2774 if (nItem == infoPtr->nFocusedItem) return FALSE;
2776 lvItem.state = nItem == -1 ? 0 : LVIS_FOCUSED;
2777 lvItem.stateMask = LVIS_FOCUSED;
2778 LISTVIEW_SetItemState(infoPtr, nItem == -1 ? infoPtr->nFocusedItem : nItem, &lvItem);
2780 return oldFocus != infoPtr->nFocusedItem;
2783 /* Helper function for LISTVIEW_ShiftIndices *only* */
2784 static INT shift_item(LISTVIEW_INFO *infoPtr, INT nShiftItem, INT nItem, INT direction)
2786 if (nShiftItem < nItem) return nShiftItem;
2788 if (nShiftItem > nItem) return nShiftItem + direction;
2790 if (direction > 0) return nShiftItem + direction;
2792 return min(nShiftItem, infoPtr->nItemCount - 1);
2796 * DESCRIPTION:
2797 * Updates the various indices after an item has been inserted or deleted.
2799 * PARAMETER(S):
2800 * [I] infoPtr : valid pointer to the listview structure
2801 * [I] nItem : item index
2802 * [I] direction : Direction of shift, +1 or -1.
2804 * RETURN:
2805 * None
2807 static void LISTVIEW_ShiftIndices(LISTVIEW_INFO *infoPtr, INT nItem, INT direction)
2809 INT nNewFocus;
2811 TRACE("Shifting %iu, %i steps\n", nItem, direction);
2813 ranges_shift(infoPtr->selectionRanges, nItem, direction, infoPtr->nItemCount);
2815 assert(abs(direction) == 1);
2817 infoPtr->nSelectionMark = shift_item(infoPtr, infoPtr->nSelectionMark, nItem, direction);
2819 nNewFocus = shift_item(infoPtr, infoPtr->nFocusedItem, nItem, direction);
2820 if (nNewFocus != infoPtr->nFocusedItem)
2821 LISTVIEW_SetItemFocus(infoPtr, nNewFocus);
2823 /* But we are not supposed to modify nHotItem! */
2828 * DESCRIPTION:
2829 * Adds a block of selections.
2831 * PARAMETER(S):
2832 * [I] infoPtr : valid pointer to the listview structure
2833 * [I] nItem : item index
2835 * RETURN:
2836 * None
2838 static void LISTVIEW_AddGroupSelection(LISTVIEW_INFO *infoPtr, INT nItem)
2840 INT nFirst = min(infoPtr->nSelectionMark, nItem);
2841 INT nLast = max(infoPtr->nSelectionMark, nItem);
2842 INT i;
2843 LVITEMW item;
2845 if (nFirst == -1) nFirst = nItem;
2847 item.state = LVIS_SELECTED;
2848 item.stateMask = LVIS_SELECTED;
2850 /* FIXME: this is not correct LVS_OWNERDATA
2851 * setting the item states individually will generate
2852 * a LVN_ITEMCHANGED notification for each one. Instead,
2853 * we have to send a LVN_ODSTATECHANGED notification.
2854 * See MSDN documentation for LVN_ITEMCHANGED.
2856 for (i = nFirst; i <= nLast; i++)
2857 LISTVIEW_SetItemState(infoPtr,i,&item);
2861 /***
2862 * DESCRIPTION:
2863 * Sets a single group selection.
2865 * PARAMETER(S):
2866 * [I] infoPtr : valid pointer to the listview structure
2867 * [I] nItem : item index
2869 * RETURN:
2870 * None
2872 static void LISTVIEW_SetGroupSelection(LISTVIEW_INFO *infoPtr, INT nItem)
2874 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
2875 RANGES selection;
2876 LVITEMW item;
2877 ITERATOR i;
2879 if (!(selection = ranges_create(100))) return;
2881 item.state = LVIS_SELECTED;
2882 item.stateMask = LVIS_SELECTED;
2884 if ((uView == LVS_LIST) || (uView == LVS_REPORT))
2886 if (infoPtr->nSelectionMark == -1)
2888 infoPtr->nSelectionMark = nItem;
2889 ranges_additem(selection, nItem);
2891 else
2893 RANGE sel;
2895 sel.lower = min(infoPtr->nSelectionMark, nItem);
2896 sel.upper = max(infoPtr->nSelectionMark, nItem) + 1;
2897 ranges_add(selection, sel);
2900 else
2902 RECT rcItem, rcSel, rcSelMark;
2903 POINT ptItem;
2905 rcItem.left = LVIR_BOUNDS;
2906 if (!LISTVIEW_GetItemRect(infoPtr, nItem, &rcItem)) return;
2907 rcSelMark.left = LVIR_BOUNDS;
2908 if (!LISTVIEW_GetItemRect(infoPtr, infoPtr->nSelectionMark, &rcSelMark)) return;
2909 UnionRect(&rcSel, &rcItem, &rcSelMark);
2910 iterator_frameditems(&i, infoPtr, &rcSel);
2911 while(iterator_next(&i))
2913 LISTVIEW_GetItemPosition(infoPtr, i.nItem, &ptItem);
2914 if (PtInRect(&rcSel, ptItem)) ranges_additem(selection, i.nItem);
2916 iterator_destroy(&i);
2919 LISTVIEW_DeselectAllSkipItems(infoPtr, selection);
2920 iterator_rangesitems(&i, selection);
2921 while(iterator_next(&i))
2922 LISTVIEW_SetItemState(infoPtr, i.nItem, &item);
2923 /* this will also destroy the selection */
2924 iterator_destroy(&i);
2926 LISTVIEW_SetItemFocus(infoPtr, nItem);
2929 /***
2930 * DESCRIPTION:
2931 * Sets a single selection.
2933 * PARAMETER(S):
2934 * [I] infoPtr : valid pointer to the listview structure
2935 * [I] nItem : item index
2937 * RETURN:
2938 * None
2940 static void LISTVIEW_SetSelection(LISTVIEW_INFO *infoPtr, INT nItem)
2942 LVITEMW lvItem;
2944 TRACE("nItem=%d\n", nItem);
2946 LISTVIEW_DeselectAllSkipItem(infoPtr, nItem);
2948 lvItem.state = LVIS_FOCUSED | LVIS_SELECTED;
2949 lvItem.stateMask = LVIS_FOCUSED | LVIS_SELECTED;
2950 LISTVIEW_SetItemState(infoPtr, nItem, &lvItem);
2952 infoPtr->nSelectionMark = nItem;
2955 /***
2956 * DESCRIPTION:
2957 * Set selection(s) with keyboard.
2959 * PARAMETER(S):
2960 * [I] infoPtr : valid pointer to the listview structure
2961 * [I] nItem : item index
2963 * RETURN:
2964 * SUCCESS : TRUE (needs to be repainted)
2965 * FAILURE : FALSE (nothing has changed)
2967 static BOOL LISTVIEW_KeySelection(LISTVIEW_INFO *infoPtr, INT nItem)
2969 /* FIXME: pass in the state */
2970 WORD wShift = HIWORD(GetKeyState(VK_SHIFT));
2971 WORD wCtrl = HIWORD(GetKeyState(VK_CONTROL));
2972 BOOL bResult = FALSE;
2974 if ((nItem >= 0) && (nItem < infoPtr->nItemCount))
2976 if (infoPtr->dwStyle & LVS_SINGLESEL)
2978 bResult = TRUE;
2979 LISTVIEW_SetSelection(infoPtr, nItem);
2981 else
2983 if (wShift)
2985 bResult = TRUE;
2986 LISTVIEW_SetGroupSelection(infoPtr, nItem);
2988 else if (wCtrl)
2990 bResult = LISTVIEW_SetItemFocus(infoPtr, nItem);
2992 else
2994 bResult = TRUE;
2995 LISTVIEW_SetSelection(infoPtr, nItem);
2998 LISTVIEW_EnsureVisible(infoPtr, nItem, FALSE);
3001 UpdateWindow(infoPtr->hwndSelf); /* update client area */
3002 return bResult;
3006 /***
3007 * DESCRIPTION:
3008 * Called when the mouse is being actively tracked and has hovered for a specified
3009 * amount of time
3011 * PARAMETER(S):
3012 * [I] infoPtr : valid pointer to the listview structure
3013 * [I] fwKeys : key indicator
3014 * [I] pts : mouse position
3016 * RETURN:
3017 * 0 if the message was processed, non-zero if there was an error
3019 * INFO:
3020 * LVS_EX_TRACKSELECT: An item is automatically selected when the cursor remains
3021 * over the item for a certain period of time.
3024 static LRESULT LISTVIEW_MouseHover(LISTVIEW_INFO *infoPtr, WORD fwKyes, POINTS pts)
3026 if(infoPtr->dwLvExStyle & LVS_EX_TRACKSELECT)
3027 /* FIXME: select the item!!! */
3028 /*LISTVIEW_GetItemAtPt(infoPtr, pt)*/;
3030 return 0;
3033 /***
3034 * DESCRIPTION:
3035 * Called whenever WM_MOUSEMOVE is received.
3037 * PARAMETER(S):
3038 * [I] infoPtr : valid pointer to the listview structure
3039 * [I] fwKeys : key indicator
3040 * [I] pts : mouse position
3042 * RETURN:
3043 * 0 if the message is processed, non-zero if there was an error
3045 static LRESULT LISTVIEW_MouseMove(LISTVIEW_INFO *infoPtr, WORD fwKeys, POINTS pts)
3047 TRACKMOUSEEVENT trackinfo;
3049 /* see if we are supposed to be tracking mouse hovering */
3050 if(infoPtr->dwLvExStyle & LVS_EX_TRACKSELECT) {
3051 /* fill in the trackinfo struct */
3052 trackinfo.cbSize = sizeof(TRACKMOUSEEVENT);
3053 trackinfo.dwFlags = TME_QUERY;
3054 trackinfo.hwndTrack = infoPtr->hwndSelf;
3055 trackinfo.dwHoverTime = infoPtr->dwHoverTime;
3057 /* see if we are already tracking this hwnd */
3058 _TrackMouseEvent(&trackinfo);
3060 if(!(trackinfo.dwFlags & TME_HOVER)) {
3061 trackinfo.dwFlags = TME_HOVER;
3063 /* call TRACKMOUSEEVENT so we receive WM_MOUSEHOVER messages */
3064 _TrackMouseEvent(&trackinfo);
3068 return 0;
3072 /***
3073 * Tests wheather the item is assignable to a list with style lStyle
3075 static inline BOOL is_assignable_item(const LVITEMW *lpLVItem, LONG lStyle)
3077 if ( (lpLVItem->mask & LVIF_TEXT) &&
3078 (lpLVItem->pszText == LPSTR_TEXTCALLBACKW) &&
3079 (lStyle & (LVS_SORTASCENDING | LVS_SORTDESCENDING)) ) return FALSE;
3081 return TRUE;
3085 /***
3086 * DESCRIPTION:
3087 * Helper for LISTVIEW_SetItemT *only*: sets item attributes.
3089 * PARAMETER(S):
3090 * [I] infoPtr : valid pointer to the listview structure
3091 * [I] lpLVItem : valid pointer to new item atttributes
3092 * [I] isNew : the item being set is being inserted
3093 * [I] isW : TRUE if lpLVItem is Unicode, FALSE if it's ANSI
3094 * [O] bChanged : will be set to TRUE if the item really changed
3096 * RETURN:
3097 * SUCCESS : TRUE
3098 * FAILURE : FALSE
3100 static BOOL set_main_item(LISTVIEW_INFO *infoPtr, const LVITEMW *lpLVItem, BOOL isNew, BOOL isW, BOOL *bChanged)
3102 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
3103 ITEM_INFO *lpItem;
3104 NMLISTVIEW nmlv;
3105 UINT uChanged = 0;
3106 LVITEMW item;
3108 TRACE("()\n");
3110 assert(lpLVItem->iItem >= 0 && lpLVItem->iItem < infoPtr->nItemCount);
3112 if (lpLVItem->mask == 0) return TRUE;
3114 if (infoPtr->dwStyle & LVS_OWNERDATA)
3116 /* a virtual listview we stores only selection and focus */
3117 if (lpLVItem->mask & ~LVIF_STATE)
3118 return FALSE;
3119 lpItem = NULL;
3121 else
3123 HDPA hdpaSubItems = (HDPA)DPA_GetPtr(infoPtr->hdpaItems, lpLVItem->iItem);
3124 lpItem = (ITEM_INFO *)DPA_GetPtr(hdpaSubItems, 0);
3125 assert (lpItem);
3128 /* we need to get the lParam and state of the item */
3129 item.iItem = lpLVItem->iItem;
3130 item.iSubItem = lpLVItem->iSubItem;
3131 item.mask = LVIF_STATE | LVIF_PARAM;
3132 item.stateMask = ~0;
3133 item.state = 0;
3134 item.lParam = 0;
3135 if (!isNew && !LISTVIEW_GetItemW(infoPtr, &item)) return FALSE;
3137 TRACE("oldState=%x, newState=%x\n", item.state, lpLVItem->state);
3138 /* determine what fields will change */
3139 if ((lpLVItem->mask & LVIF_STATE) && ((item.state ^ lpLVItem->state) & lpLVItem->stateMask & ~infoPtr->uCallbackMask))
3140 uChanged |= LVIF_STATE;
3142 if ((lpLVItem->mask & LVIF_IMAGE) && (lpItem->hdr.iImage != lpLVItem->iImage))
3143 uChanged |= LVIF_IMAGE;
3145 if ((lpLVItem->mask & LVIF_PARAM) && (lpItem->lParam != lpLVItem->lParam))
3146 uChanged |= LVIF_PARAM;
3148 if ((lpLVItem->mask & LVIF_INDENT) && (lpItem->iIndent != lpLVItem->iIndent))
3149 uChanged |= LVIF_INDENT;
3151 if ((lpLVItem->mask & LVIF_TEXT) && textcmpWT(lpItem->hdr.pszText, lpLVItem->pszText, isW))
3152 uChanged |= LVIF_TEXT;
3154 TRACE("uChanged=0x%x\n", uChanged);
3155 if (!uChanged) return TRUE;
3156 *bChanged = TRUE;
3158 ZeroMemory(&nmlv, sizeof(NMLISTVIEW));
3159 nmlv.iItem = lpLVItem->iItem;
3160 nmlv.uNewState = (item.state & ~lpLVItem->stateMask) | (lpLVItem->state & lpLVItem->stateMask);
3161 nmlv.uOldState = item.state;
3162 nmlv.uChanged = uChanged;
3163 nmlv.lParam = item.lParam;
3165 /* send LVN_ITEMCHANGING notification, if the item is not being inserted */
3166 /* and we are _NOT_ virtual (LVS_OWERNDATA) */
3167 if(lpItem && !isNew && notify_listview(infoPtr, LVN_ITEMCHANGING, &nmlv))
3168 return FALSE;
3170 /* copy information */
3171 if (lpLVItem->mask & LVIF_TEXT)
3172 textsetptrT(&lpItem->hdr.pszText, lpLVItem->pszText, isW);
3174 if (lpLVItem->mask & LVIF_IMAGE)
3175 lpItem->hdr.iImage = lpLVItem->iImage;
3177 if (lpLVItem->mask & LVIF_PARAM)
3178 lpItem->lParam = lpLVItem->lParam;
3180 if (lpLVItem->mask & LVIF_INDENT)
3181 lpItem->iIndent = lpLVItem->iIndent;
3183 if (uChanged & LVIF_STATE)
3185 if (lpItem && (lpLVItem->stateMask & ~infoPtr->uCallbackMask & ~(LVIS_FOCUSED | LVIS_SELECTED)))
3187 lpItem->state &= ~lpLVItem->stateMask;
3188 lpItem->state |= (lpLVItem->state & lpLVItem->stateMask);
3190 if (lpLVItem->state & lpLVItem->stateMask & ~infoPtr->uCallbackMask & LVIS_SELECTED)
3192 if (infoPtr->dwStyle & LVS_SINGLESEL) LISTVIEW_DeselectAllSkipItem(infoPtr, lpLVItem->iItem);
3193 ranges_additem(infoPtr->selectionRanges, lpLVItem->iItem);
3195 else if (lpLVItem->stateMask & LVIS_SELECTED)
3196 ranges_delitem(infoPtr->selectionRanges, lpLVItem->iItem);
3198 /* if we are asked to change focus, and we manage it, do it */
3199 if (lpLVItem->state & lpLVItem->stateMask & ~infoPtr->uCallbackMask & LVIS_FOCUSED)
3201 if (lpLVItem->state & LVIS_FOCUSED)
3203 LISTVIEW_SetItemFocus(infoPtr, -1);
3204 infoPtr->nFocusedItem = lpLVItem->iItem;
3205 LISTVIEW_EnsureVisible(infoPtr, lpLVItem->iItem, uView == LVS_LIST);
3207 else if (infoPtr->nFocusedItem == lpLVItem->iItem)
3208 infoPtr->nFocusedItem = -1;
3212 /* if we're inserting the item, we're done */
3213 if (isNew) return TRUE;
3215 /* send LVN_ITEMCHANGED notification */
3216 if (lpLVItem->mask & LVIF_PARAM) nmlv.lParam = lpLVItem->lParam;
3217 notify_listview(infoPtr, LVN_ITEMCHANGED, &nmlv);
3219 return TRUE;
3222 /***
3223 * DESCRIPTION:
3224 * Helper for LISTVIEW_{Set,Insert}ItemT *only*: sets subitem attributes.
3226 * PARAMETER(S):
3227 * [I] infoPtr : valid pointer to the listview structure
3228 * [I] lpLVItem : valid pointer to new subitem atttributes
3229 * [I] isW : TRUE if lpLVItem is Unicode, FALSE if it's ANSI
3230 * [O] bChanged : will be set to TRUE if the item really changed
3232 * RETURN:
3233 * SUCCESS : TRUE
3234 * FAILURE : FALSE
3236 static BOOL set_sub_item(LISTVIEW_INFO *infoPtr, const LVITEMW *lpLVItem, BOOL isW, BOOL *bChanged)
3238 HDPA hdpaSubItems;
3239 SUBITEM_INFO *lpSubItem;
3241 /* we do not support subitems for virtual listviews */
3242 if (infoPtr->dwStyle & LVS_OWNERDATA) return FALSE;
3244 /* set subitem only if column is present */
3245 if (lpLVItem->iSubItem >= infoPtr->hdpaColumns->nItemCount) return FALSE;
3247 /* First do some sanity checks */
3248 if (lpLVItem->mask & ~(LVIF_TEXT | LVIF_IMAGE)) return FALSE;
3249 if (!(lpLVItem->mask & (LVIF_TEXT | LVIF_IMAGE))) return TRUE;
3251 /* get the subitem structure, and create it if not there */
3252 hdpaSubItems = (HDPA)DPA_GetPtr(infoPtr->hdpaItems, lpLVItem->iItem);
3253 assert (hdpaSubItems);
3255 lpSubItem = LISTVIEW_GetSubItemPtr(hdpaSubItems, lpLVItem->iSubItem);
3256 if (!lpSubItem)
3258 SUBITEM_INFO *tmpSubItem;
3259 INT i;
3261 lpSubItem = (SUBITEM_INFO *)COMCTL32_Alloc(sizeof(SUBITEM_INFO));
3262 if (!lpSubItem) return FALSE;
3263 /* we could binary search here, if need be...*/
3264 for (i = 1; i < hdpaSubItems->nItemCount; i++)
3266 tmpSubItem = (SUBITEM_INFO *)DPA_GetPtr(hdpaSubItems, i);
3267 if (tmpSubItem->iSubItem > lpLVItem->iSubItem) break;
3269 if (DPA_InsertPtr(hdpaSubItems, i, lpSubItem) == -1)
3271 COMCTL32_Free(lpSubItem);
3272 return FALSE;
3274 lpSubItem->iSubItem = lpLVItem->iSubItem;
3275 *bChanged = TRUE;
3278 if (lpLVItem->mask & LVIF_IMAGE)
3279 if (lpSubItem->hdr.iImage != lpLVItem->iImage)
3281 lpSubItem->hdr.iImage = lpLVItem->iImage;
3282 *bChanged = TRUE;
3285 if (lpLVItem->mask & LVIF_TEXT)
3286 if (lpSubItem->hdr.pszText != lpLVItem->pszText)
3288 textsetptrT(&lpSubItem->hdr.pszText, lpLVItem->pszText, isW);
3289 *bChanged = TRUE;
3292 return TRUE;
3295 /***
3296 * DESCRIPTION:
3297 * Sets item attributes.
3299 * PARAMETER(S):
3300 * [I] infoPtr : valid pointer to the listview structure
3301 * [I] lpLVItem : new item atttributes
3302 * [I] isW : TRUE if lpLVItem is Unicode, FALSE if it's ANSI
3304 * RETURN:
3305 * SUCCESS : TRUE
3306 * FAILURE : FALSE
3308 static BOOL LISTVIEW_SetItemT(LISTVIEW_INFO *infoPtr, const LVITEMW *lpLVItem, BOOL isW)
3310 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
3311 LPWSTR pszText = NULL;
3312 BOOL bResult, bChanged = FALSE;
3314 TRACE("(lpLVItem=%s, isW=%d)\n", debuglvitem_t(lpLVItem, isW), isW);
3316 if (!lpLVItem || lpLVItem->iItem < 0 || lpLVItem->iItem >= infoPtr->nItemCount)
3317 return FALSE;
3319 /* For efficiency, we transform the lpLVItem->pszText to Unicode here */
3320 if ((lpLVItem->mask & LVIF_TEXT) && is_textW(lpLVItem->pszText))
3322 pszText = lpLVItem->pszText;
3323 ((LVITEMW *)lpLVItem)->pszText = textdupTtoW(lpLVItem->pszText, isW);
3326 /* actually set the fields */
3327 if (!is_assignable_item(lpLVItem, infoPtr->dwStyle)) return FALSE;
3329 if (lpLVItem->iSubItem)
3330 bResult = set_sub_item(infoPtr, lpLVItem, TRUE, &bChanged);
3331 else
3332 bResult = set_main_item(infoPtr, lpLVItem, FALSE, TRUE, &bChanged);
3334 /* redraw item, if necessary */
3335 if (bChanged && !infoPtr->bIsDrawing)
3337 /* this little optimization eliminates some nasty flicker */
3338 if ( uView == LVS_REPORT && !(infoPtr->dwStyle & LVS_OWNERDRAWFIXED) &&
3339 (!(infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT) || lpLVItem->iSubItem) )
3340 LISTVIEW_InvalidateSubItem(infoPtr, lpLVItem->iItem, lpLVItem->iSubItem);
3341 else
3342 LISTVIEW_InvalidateItem(infoPtr, lpLVItem->iItem);
3344 /* restore text */
3345 if (pszText)
3347 textfreeT(lpLVItem->pszText, isW);
3348 ((LVITEMW *)lpLVItem)->pszText = pszText;
3351 return bResult;
3354 /***
3355 * DESCRIPTION:
3356 * Retrieves the index of the item at coordinate (0, 0) of the client area.
3358 * PARAMETER(S):
3359 * [I] infoPtr : valid pointer to the listview structure
3361 * RETURN:
3362 * item index
3364 static INT LISTVIEW_GetTopIndex(LISTVIEW_INFO *infoPtr)
3366 LONG lStyle = infoPtr->dwStyle;
3367 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
3368 INT nItem = 0;
3369 SCROLLINFO scrollInfo;
3371 scrollInfo.cbSize = sizeof(SCROLLINFO);
3372 scrollInfo.fMask = SIF_POS;
3374 if (uView == LVS_LIST)
3376 if ((lStyle & WS_HSCROLL) && GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo))
3377 nItem = scrollInfo.nPos * LISTVIEW_GetCountPerColumn(infoPtr);
3379 else if (uView == LVS_REPORT)
3381 if ((lStyle & WS_VSCROLL) && GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo))
3382 nItem = scrollInfo.nPos;
3384 else
3386 if ((lStyle & WS_VSCROLL) && GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo))
3387 nItem = LISTVIEW_GetCountPerRow(infoPtr) * (scrollInfo.nPos / infoPtr->nItemHeight);
3390 TRACE("nItem=%d\n", nItem);
3392 return nItem;
3396 /***
3397 * DESCRIPTION:
3398 * Erases the background of the given rectangle
3400 * PARAMETER(S):
3401 * [I] infoPtr : valid pointer to the listview structure
3402 * [I] hdc : device context handle
3403 * [I] lprcBox : clipping rectangle
3405 * RETURN:
3406 * Success: TRUE
3407 * Failure: FALSE
3409 static inline BOOL LISTVIEW_FillBkgnd(LISTVIEW_INFO *infoPtr, HDC hdc, const RECT *lprcBox)
3411 if (!infoPtr->hBkBrush) return FALSE;
3413 TRACE("(hdc=%p, lprcBox=%s, hBkBrush=%p)\n", hdc, debugrect(lprcBox), infoPtr->hBkBrush);
3415 return FillRect(hdc, lprcBox, infoPtr->hBkBrush);
3418 /***
3419 * DESCRIPTION:
3420 * Draws an item.
3422 * PARAMETER(S):
3423 * [I] infoPtr : valid pointer to the listview structure
3424 * [I] hdc : device context handle
3425 * [I] nItem : item index
3426 * [I] nSubItem : subitem index
3427 * [I] pos : item position in client coordinates
3428 * [I] cdmode : custom draw mode
3430 * RETURN:
3431 * Success: TRUE
3432 * Failure: FALSE
3434 static BOOL LISTVIEW_DrawItem(LISTVIEW_INFO *infoPtr, HDC hdc, INT nItem, INT nSubItem, POINT pos, DWORD cdmode)
3436 UINT uFormat, uView = infoPtr->dwStyle & LVS_TYPEMASK;
3437 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
3438 WCHAR szCallback[] = { '(', 'c', 'a', 'l', 'l', 'b', 'a', 'c', 'k', ')', 0 };
3439 DWORD cditemmode = CDRF_DODEFAULT;
3440 RECT* lprcFocus, rcSelect, rcBox, rcState, rcIcon, rcLabel;
3441 NMLVCUSTOMDRAW nmlvcd;
3442 HIMAGELIST himl;
3443 LVITEMW lvItem;
3445 TRACE("(hdc=%p, nItem=%d, nSubItem=%d, pos=%s)\n", hdc, nItem, nSubItem, debugpoint(&pos));
3447 /* get information needed for drawing the item */
3448 lvItem.mask = LVIF_TEXT | LVIF_IMAGE;
3449 if (nSubItem == 0) lvItem.mask |= LVIF_STATE | LVIF_PARAM;
3450 if (uView == LVS_REPORT) lvItem.mask |= LVIF_INDENT;
3451 lvItem.stateMask = LVIS_SELECTED | LVIS_FOCUSED | LVIS_STATEIMAGEMASK;
3452 lvItem.iItem = nItem;
3453 lvItem.iSubItem = nSubItem;
3454 lvItem.state = 0;
3455 lvItem.lParam = 0;
3456 lvItem.cchTextMax = DISP_TEXT_SIZE;
3457 lvItem.pszText = szDispText;
3458 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) return FALSE;
3459 if (nSubItem > 0 && (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT))
3460 lvItem.state = LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED);
3461 if (lvItem.pszText == LPSTR_TEXTCALLBACKW) lvItem.pszText = szCallback;
3462 TRACE(" lvItem=%s\n", debuglvitem_t(&lvItem, TRUE));
3464 /* now check if we need to update the focus rectangle */
3465 lprcFocus = infoPtr->bFocus && (lvItem.state & LVIS_FOCUSED) ? &infoPtr->rcFocus : 0;
3467 if (!lprcFocus) lvItem.state &= ~LVIS_FOCUSED;
3468 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, &rcBox, &rcState, &rcIcon, &rcLabel);
3469 OffsetRect(&rcBox, pos.x, pos.y);
3470 OffsetRect(&rcState, pos.x, pos.y);
3471 OffsetRect(&rcIcon, pos.x, pos.y);
3472 OffsetRect(&rcLabel, pos.x, pos.y);
3473 TRACE(" rcBox=%s, rcState=%s, rcIcon=%s. rcLabel=%s\n",
3474 debugrect(&rcBox), debugrect(&rcState), debugrect(&rcIcon), debugrect(&rcLabel));
3476 /* fill in the custom draw structure */
3477 customdraw_fill(&nmlvcd, infoPtr, hdc, &rcBox);
3478 nmlvcd.nmcd.dwItemSpec = lvItem.iItem;
3479 nmlvcd.iSubItem = lvItem.iSubItem;
3480 if (lvItem.state & LVIS_SELECTED) nmlvcd.nmcd.uItemState |= CDIS_SELECTED;
3481 if (lvItem.state & LVIS_FOCUSED) nmlvcd.nmcd.uItemState |= CDIS_FOCUS;
3482 if (lvItem.iItem == infoPtr->nHotItem) nmlvcd.nmcd.uItemState |= CDIS_HOT;
3483 nmlvcd.nmcd.lItemlParam = lvItem.lParam;
3485 if (cdmode & CDRF_NOTIFYITEMDRAW)
3486 cditemmode = notify_customdraw (infoPtr, CDDS_ITEMPREPAINT, &nmlvcd);
3487 if (cditemmode & CDRF_SKIPDEFAULT) goto postpaint;
3489 /* apprently, for selected items, we have to override the returned values */
3490 if (lvItem.state & LVIS_SELECTED)
3492 if (infoPtr->bFocus)
3494 nmlvcd.clrTextBk = comctl32_color.clrHighlight;
3495 nmlvcd.clrText = comctl32_color.clrHighlightText;
3497 else if (infoPtr->dwStyle & LVS_SHOWSELALWAYS)
3499 nmlvcd.clrTextBk = comctl32_color.clr3dFace;
3500 nmlvcd.clrText = comctl32_color.clrBtnText;
3504 /* in full row select, subitems, will just use main item's colors */
3505 if (nSubItem && uView == LVS_REPORT && (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT))
3506 nmlvcd.clrTextBk = CLR_NONE;
3508 /* state icons */
3509 if (infoPtr->himlState && !IsRectEmpty(&rcState))
3511 UINT uStateImage = (lvItem.state & LVIS_STATEIMAGEMASK) >> 12;
3512 if (uStateImage)
3514 TRACE("uStateImage=%d\n", uStateImage);
3515 ImageList_Draw(infoPtr->himlState, uStateImage - 1, hdc, rcState.left, rcState.top, ILD_NORMAL);
3519 /* small icons */
3520 himl = (uView == LVS_ICON ? infoPtr->himlNormal : infoPtr->himlSmall);
3521 if (himl && lvItem.iImage >= 0 && !IsRectEmpty(&rcIcon))
3523 TRACE("iImage=%d\n", lvItem.iImage);
3524 ImageList_Draw(himl, lvItem.iImage, hdc, rcIcon.left, rcIcon.top,
3525 (lvItem.state & LVIS_SELECTED) && (infoPtr->bFocus) ? ILD_SELECTED : ILD_NORMAL);
3528 /* Don't bother painting item being edited */
3529 if (infoPtr->hwndEdit && nItem == infoPtr->nEditLabelItem && nSubItem == 0) goto postpaint;
3531 /* Set the text attributes */
3532 if (nmlvcd.clrTextBk != CLR_NONE)
3534 SetBkMode(hdc, OPAQUE);
3535 SetBkColor(hdc, nmlvcd.clrTextBk == CLR_DEFAULT ? infoPtr->clrTextBkDefault : nmlvcd.clrTextBk);
3537 else
3538 SetBkMode(hdc, TRANSPARENT);
3539 SetTextColor(hdc, nmlvcd.clrText);
3541 /* draw the selection background, if we're drawing the main item */
3542 if (nSubItem == 0)
3544 rcSelect = rcLabel;
3545 if (uView == LVS_REPORT && (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT))
3546 rcSelect.right = rcBox.right;
3548 if (nmlvcd.clrTextBk != CLR_NONE)
3549 ExtTextOutW(hdc, rcSelect.left, rcSelect.top, ETO_OPAQUE, &rcSelect, 0, 0, 0);
3550 if(lprcFocus) *lprcFocus = rcSelect;
3553 /* figure out the text drawing flags */
3554 uFormat = (uView == LVS_ICON ? (lprcFocus ? LV_FL_DT_FLAGS : LV_ML_DT_FLAGS) : LV_SL_DT_FLAGS);
3555 if (uView == LVS_ICON)
3556 uFormat = (lprcFocus ? LV_FL_DT_FLAGS : LV_ML_DT_FLAGS);
3557 else if (nSubItem)
3559 switch (LISTVIEW_GetColumnInfo(infoPtr, nSubItem)->fmt & LVCFMT_JUSTIFYMASK)
3561 case LVCFMT_RIGHT: uFormat |= DT_RIGHT; break;
3562 case LVCFMT_CENTER: uFormat |= DT_CENTER; break;
3563 default: uFormat |= DT_LEFT;
3566 if (!(uFormat & (DT_RIGHT | DT_CENTER))) rcLabel.left += 2;
3567 DrawTextW(hdc, lvItem.pszText, -1, &rcLabel, uFormat);
3569 postpaint:
3570 if (cditemmode & CDRF_NOTIFYPOSTPAINT)
3571 notify_customdraw(infoPtr, CDDS_ITEMPOSTPAINT, &nmlvcd);
3572 return TRUE;
3575 /***
3576 * DESCRIPTION:
3577 * Draws listview items when in owner draw mode.
3579 * PARAMETER(S):
3580 * [I] infoPtr : valid pointer to the listview structure
3581 * [I] hdc : device context handle
3583 * RETURN:
3584 * None
3586 static void LISTVIEW_RefreshOwnerDraw(LISTVIEW_INFO *infoPtr, HDC hdc)
3588 UINT uID = GetWindowLongW(infoPtr->hwndSelf, GWL_ID);
3589 HWND hwndParent = GetParent(infoPtr->hwndSelf);
3590 POINT Origin, Position;
3591 DRAWITEMSTRUCT dis;
3592 LVITEMW item;
3593 ITERATOR i;
3595 TRACE("()\n");
3597 ZeroMemory(&dis, sizeof(dis));
3599 /* Get scroll info once before loop */
3600 LISTVIEW_GetOrigin(infoPtr, &Origin);
3602 /* figure out what we need to draw */
3603 iterator_visibleitems(&i, infoPtr, hdc);
3605 /* send cache hint notification */
3606 if (infoPtr->dwStyle & LVS_OWNERDATA)
3608 RANGE range = iterator_range(&i);
3609 NMLVCACHEHINT nmlv;
3611 ZeroMemory(&nmlv, sizeof(NMLVCACHEHINT));
3612 nmlv.iFrom = range.lower;
3613 nmlv.iTo = range.upper - 1;
3614 notify_hdr(infoPtr, LVN_ODCACHEHINT, &nmlv.hdr);
3617 /* iterate through the invalidated rows */
3618 while(iterator_next(&i))
3620 item.iItem = i.nItem;
3621 item.iSubItem = 0;
3622 item.mask = LVIF_PARAM | LVIF_STATE;
3623 item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
3624 if (!LISTVIEW_GetItemW(infoPtr, &item)) continue;
3626 dis.CtlType = ODT_LISTVIEW;
3627 dis.CtlID = uID;
3628 dis.itemID = item.iItem;
3629 dis.itemAction = ODA_DRAWENTIRE;
3630 dis.itemState = 0;
3631 if (item.state & LVIS_SELECTED) dis.itemState |= ODS_SELECTED;
3632 if (infoPtr->bFocus && (item.state & LVIS_FOCUSED)) dis.itemState |= ODS_FOCUS;
3633 dis.hwndItem = infoPtr->hwndSelf;
3634 dis.hDC = hdc;
3635 LISTVIEW_GetItemOrigin(infoPtr, dis.itemID, &Position);
3636 dis.rcItem.left = Position.x + Origin.x;
3637 dis.rcItem.right = dis.rcItem.left + infoPtr->nItemWidth;
3638 dis.rcItem.top = Position.y + Origin.y;
3639 dis.rcItem.bottom = dis.rcItem.top + infoPtr->nItemHeight;
3640 dis.itemData = item.lParam;
3642 TRACE("item=%s, rcItem=%s\n", debuglvitem_t(&item, TRUE), debugrect(&dis.rcItem));
3643 SendMessageW(hwndParent, WM_DRAWITEM, dis.CtlID, (LPARAM)&dis);
3645 iterator_destroy(&i);
3648 /***
3649 * DESCRIPTION:
3650 * Draws listview items when in report display mode.
3652 * PARAMETER(S):
3653 * [I] infoPtr : valid pointer to the listview structure
3654 * [I] hdc : device context handle
3655 * [I] cdmode : custom draw mode
3657 * RETURN:
3658 * None
3660 static void LISTVIEW_RefreshReport(LISTVIEW_INFO *infoPtr, HDC hdc, DWORD cdmode)
3662 INT rgntype;
3663 RECT rcClip, rcItem;
3664 POINT Origin, Position;
3665 RANGE colRange;
3666 ITERATOR i, j;
3668 TRACE("()\n");
3670 /* figure out what to draw */
3671 rgntype = GetClipBox(hdc, &rcClip);
3672 if (rgntype == NULLREGION) return;
3674 /* Get scroll info once before loop */
3675 LISTVIEW_GetOrigin(infoPtr, &Origin);
3677 /* narrow down the columns we need to paint */
3678 for(colRange.lower = 0; colRange.lower < infoPtr->hdpaColumns->nItemCount; colRange.lower++)
3680 LISTVIEW_GetHeaderRect(infoPtr, colRange.lower, &rcItem);
3681 if (rcItem.right + Origin.x >= rcClip.left) break;
3683 for(colRange.upper = infoPtr->hdpaColumns->nItemCount; colRange.upper > 0; colRange.upper--)
3685 LISTVIEW_GetHeaderRect(infoPtr, colRange.upper - 1, &rcItem);
3686 if (rcItem.left + Origin.x < rcClip.right) break;
3688 iterator_rangeitems(&j, colRange);
3690 /* in full row select, we _have_ to draw the main item */
3691 if (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT)
3692 j.nSpecial = 0;
3694 /* figure out what we need to draw */
3695 iterator_visibleitems(&i, infoPtr, hdc);
3697 /* iterate through the invalidated rows */
3698 while(iterator_next(&i))
3700 /* iterate through the invalidated columns */
3701 while(iterator_next(&j))
3703 LISTVIEW_GetItemOrigin(infoPtr, i.nItem, &Position);
3704 Position.x += Origin.x;
3705 Position.y += Origin.y;
3707 if (rgntype == COMPLEXREGION && !((infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT) && j.nItem == 0))
3709 LISTVIEW_GetHeaderRect(infoPtr, j.nItem, &rcItem);
3710 rcItem.top = 0;
3711 rcItem.bottom = infoPtr->nItemHeight;
3712 OffsetRect(&rcItem, Position.x, Position.y);
3713 if (!RectVisible(hdc, &rcItem)) continue;
3716 LISTVIEW_DrawItem(infoPtr, hdc, i.nItem, j.nItem, Position, cdmode);
3719 iterator_destroy(&i);
3722 /***
3723 * DESCRIPTION:
3724 * Draws listview items when in list display mode.
3726 * PARAMETER(S):
3727 * [I] infoPtr : valid pointer to the listview structure
3728 * [I] hdc : device context handle
3729 * [I] cdmode : custom draw mode
3731 * RETURN:
3732 * None
3734 static void LISTVIEW_RefreshList(LISTVIEW_INFO *infoPtr, HDC hdc, DWORD cdmode)
3736 POINT Origin, Position;
3737 ITERATOR i;
3739 /* Get scroll info once before loop */
3740 LISTVIEW_GetOrigin(infoPtr, &Origin);
3742 /* figure out what we need to draw */
3743 iterator_visibleitems(&i, infoPtr, hdc);
3745 while(iterator_prev(&i))
3747 LISTVIEW_GetItemOrigin(infoPtr, i.nItem, &Position);
3748 Position.x += Origin.x;
3749 Position.y += Origin.y;
3751 LISTVIEW_DrawItem(infoPtr, hdc, i.nItem, 0, Position, cdmode);
3753 iterator_destroy(&i);
3757 /***
3758 * DESCRIPTION:
3759 * Draws listview items.
3761 * PARAMETER(S):
3762 * [I] infoPtr : valid pointer to the listview structure
3763 * [I] hdc : device context handle
3765 * RETURN:
3766 * NoneX
3768 static void LISTVIEW_Refresh(LISTVIEW_INFO *infoPtr, HDC hdc)
3770 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
3771 COLORREF oldTextColor, oldClrTextBk, oldClrText;
3772 NMLVCUSTOMDRAW nmlvcd;
3773 HFONT hOldFont;
3774 DWORD cdmode;
3775 INT oldBkMode;
3776 RECT rcClient;
3778 LISTVIEW_DUMP(infoPtr);
3780 infoPtr->bIsDrawing = TRUE;
3782 /* save dc values we're gonna trash while drawing */
3783 hOldFont = SelectObject(hdc, infoPtr->hFont);
3784 oldBkMode = GetBkMode(hdc);
3785 infoPtr->clrTextBkDefault = GetBkColor(hdc);
3786 oldTextColor = GetTextColor(hdc);
3788 oldClrTextBk = infoPtr->clrTextBk;
3789 oldClrText = infoPtr->clrText;
3791 GetClientRect(infoPtr->hwndSelf, &rcClient);
3792 customdraw_fill(&nmlvcd, infoPtr, hdc, &rcClient);
3793 cdmode = notify_customdraw(infoPtr, CDDS_PREPAINT, &nmlvcd);
3794 if (cdmode & CDRF_SKIPDEFAULT) goto enddraw;
3796 /* Use these colors to draw the items */
3797 infoPtr->clrTextBk = nmlvcd.clrTextBk;
3798 infoPtr->clrText = nmlvcd.clrText;
3800 /* nothing to draw */
3801 if(infoPtr->nItemCount == 0) goto enddraw;
3803 if ((infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && (uView == LVS_REPORT))
3804 LISTVIEW_RefreshOwnerDraw(infoPtr, hdc);
3805 else
3807 if (uView == LVS_REPORT)
3808 LISTVIEW_RefreshReport(infoPtr, hdc, cdmode);
3809 else /* LVS_LIST, LVS_ICON or LVS_SMALLICON */
3810 LISTVIEW_RefreshList(infoPtr, hdc, cdmode);
3812 /* if we have a focus rect, draw it */
3813 if (infoPtr->bFocus)
3814 DrawFocusRect(hdc, &infoPtr->rcFocus);
3817 enddraw:
3818 if (cdmode & CDRF_NOTIFYPOSTPAINT)
3819 notify_customdraw(infoPtr, CDDS_POSTPAINT, &nmlvcd);
3821 infoPtr->clrTextBk = oldClrTextBk;
3822 infoPtr->clrText = oldClrText;
3824 SelectObject(hdc, hOldFont);
3825 SetBkMode(hdc, oldBkMode);
3826 SetBkColor(hdc, infoPtr->clrTextBkDefault);
3827 SetTextColor(hdc, oldTextColor);
3828 infoPtr->bIsDrawing = FALSE;
3832 /***
3833 * DESCRIPTION:
3834 * Calculates the approximate width and height of a given number of items.
3836 * PARAMETER(S):
3837 * [I] infoPtr : valid pointer to the listview structure
3838 * [I] nItemCount : number of items
3839 * [I] wWidth : width
3840 * [I] wHeight : height
3842 * RETURN:
3843 * Returns a DWORD. The width in the low word and the height in high word.
3845 static DWORD LISTVIEW_ApproximateViewRect(LISTVIEW_INFO *infoPtr, INT nItemCount,
3846 WORD wWidth, WORD wHeight)
3848 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
3849 INT nItemCountPerColumn = 1;
3850 INT nColumnCount = 0;
3851 DWORD dwViewRect = 0;
3853 if (nItemCount == -1)
3854 nItemCount = infoPtr->nItemCount;
3856 if (uView == LVS_LIST)
3858 if (wHeight == 0xFFFF)
3860 /* use current height */
3861 wHeight = infoPtr->rcList.bottom - infoPtr->rcList.top;
3864 if (wHeight < infoPtr->nItemHeight)
3865 wHeight = infoPtr->nItemHeight;
3867 if (nItemCount > 0)
3869 if (infoPtr->nItemHeight > 0)
3871 nItemCountPerColumn = wHeight / infoPtr->nItemHeight;
3872 if (nItemCountPerColumn == 0)
3873 nItemCountPerColumn = 1;
3875 if (nItemCount % nItemCountPerColumn != 0)
3876 nColumnCount = nItemCount / nItemCountPerColumn;
3877 else
3878 nColumnCount = nItemCount / nItemCountPerColumn + 1;
3882 /* Microsoft padding magic */
3883 wHeight = nItemCountPerColumn * infoPtr->nItemHeight + 2;
3884 wWidth = nColumnCount * infoPtr->nItemWidth + 2;
3886 dwViewRect = MAKELONG(wWidth, wHeight);
3888 else if (uView == LVS_REPORT)
3889 FIXME("uView == LVS_REPORT: not implemented\n");
3890 else if (uView == LVS_SMALLICON)
3891 FIXME("uView == LVS_SMALLICON: not implemented\n");
3892 else if (uView == LVS_ICON)
3893 FIXME("uView == LVS_ICON: not implemented\n");
3895 return dwViewRect;
3898 /* << LISTVIEW_CreateDragImage >> */
3901 /***
3902 * DESCRIPTION:
3903 * Removes all listview items and subitems.
3905 * PARAMETER(S):
3906 * [I] infoPtr : valid pointer to the listview structure
3908 * RETURN:
3909 * SUCCESS : TRUE
3910 * FAILURE : FALSE
3912 static BOOL LISTVIEW_DeleteAllItems(LISTVIEW_INFO *infoPtr)
3914 NMLISTVIEW nmlv;
3915 HDPA hdpaSubItems = NULL;
3916 BOOL bSuppress;
3917 ITEMHDR *hdrItem;
3918 INT i, j;
3920 TRACE("()\n");
3922 /* we do it directly, to avoid notifications */
3923 ranges_clear(infoPtr->selectionRanges);
3924 infoPtr->nSelectionMark = -1;
3925 infoPtr->nFocusedItem = -1;
3926 SetRectEmpty(&infoPtr->rcFocus);
3927 /* But we are supposed to leave nHotItem as is! */
3930 /* send LVN_DELETEALLITEMS notification */
3931 ZeroMemory(&nmlv, sizeof(NMLISTVIEW));
3932 nmlv.iItem = -1;
3933 bSuppress = notify_listview(infoPtr, LVN_DELETEALLITEMS, &nmlv);
3935 for (i = infoPtr->nItemCount - 1; i >= 0; i--)
3937 /* send LVN_DELETEITEM notification, if not supressed */
3938 if (!bSuppress) notify_deleteitem(infoPtr, i);
3939 if (!(infoPtr->dwStyle & LVS_OWNERDATA))
3941 hdpaSubItems = (HDPA)DPA_GetPtr(infoPtr->hdpaItems, i);
3942 for (j = 0; j < hdpaSubItems->nItemCount; j++)
3944 hdrItem = (ITEMHDR *)DPA_GetPtr(hdpaSubItems, j);
3945 if (is_textW(hdrItem->pszText)) COMCTL32_Free(hdrItem->pszText);
3946 COMCTL32_Free(hdrItem);
3948 DPA_Destroy(hdpaSubItems);
3949 DPA_DeletePtr(infoPtr->hdpaItems, i);
3951 DPA_DeletePtr(infoPtr->hdpaPosX, i);
3952 DPA_DeletePtr(infoPtr->hdpaPosY, i);
3953 infoPtr->nItemCount --;
3956 LISTVIEW_UpdateScroll(infoPtr);
3958 LISTVIEW_InvalidateList(infoPtr);
3960 return TRUE;
3963 /***
3964 * DESCRIPTION:
3965 * Scrolls, and updates the columns, when a column is changing width.
3967 * PARAMETER(S):
3968 * [I] infoPtr : valid pointer to the listview structure
3969 * [I] nColumn : column to scroll
3970 * [I] dx : amount of scroll, in pixels
3972 * RETURN:
3973 * None.
3975 static void LISTVIEW_ScrollColumns(LISTVIEW_INFO *infoPtr, INT nColumn, INT dx)
3977 COLUMN_INFO *lpColumnInfo;
3978 RECT rcOld, rcCol;
3979 INT nCol;
3981 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, min(nColumn, infoPtr->hdpaColumns->nItemCount - 1));
3982 rcCol = lpColumnInfo->rcHeader;
3983 if (nColumn >= infoPtr->hdpaColumns->nItemCount)
3984 rcCol.left = rcCol.right;
3986 /* ajust the other columns */
3987 for (nCol = nColumn; nCol < infoPtr->hdpaColumns->nItemCount; nCol++)
3989 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, nCol);
3990 lpColumnInfo->rcHeader.left += dx;
3991 lpColumnInfo->rcHeader.right += dx;
3994 /* do not update screen if not in report mode */
3995 if (!is_redrawing(infoPtr) || (infoPtr->dwStyle & LVS_TYPEMASK) != LVS_REPORT) return;
3997 /* if we have a focus, must first erase the focus rect */
3998 if (infoPtr->bFocus) LISTVIEW_ShowFocusRect(infoPtr, FALSE);
4000 /* Need to reset the item width when inserting a new column */
4001 infoPtr->nItemWidth += dx;
4003 LISTVIEW_UpdateScroll(infoPtr);
4005 /* scroll to cover the deleted column, and invalidate for redraw */
4006 rcOld = infoPtr->rcList;
4007 rcOld.left = rcCol.left;
4008 ScrollWindowEx(infoPtr->hwndSelf, dx, 0, &rcOld, &rcOld, 0, 0, SW_ERASE | SW_INVALIDATE);
4010 /* we can restore focus now */
4011 if (infoPtr->bFocus) LISTVIEW_ShowFocusRect(infoPtr, TRUE);
4014 /***
4015 * DESCRIPTION:
4016 * Removes a column from the listview control.
4018 * PARAMETER(S):
4019 * [I] infoPtr : valid pointer to the listview structure
4020 * [I] nColumn : column index
4022 * RETURN:
4023 * SUCCESS : TRUE
4024 * FAILURE : FALSE
4026 static BOOL LISTVIEW_DeleteColumn(LISTVIEW_INFO *infoPtr, INT nColumn)
4028 RECT rcCol;
4030 TRACE("nColumn=%d\n", nColumn);
4032 if (nColumn <= 0 || nColumn >= infoPtr->hdpaColumns->nItemCount) return FALSE;
4034 LISTVIEW_GetHeaderRect(infoPtr, nColumn, &rcCol);
4036 if (!Header_DeleteItem(infoPtr->hwndHeader, nColumn))
4037 return FALSE;
4039 COMCTL32_Free(DPA_GetPtr(infoPtr->hdpaColumns, nColumn));
4040 DPA_DeletePtr(infoPtr->hdpaColumns, nColumn);
4042 if (!(infoPtr->dwStyle & LVS_OWNERDATA))
4044 SUBITEM_INFO *lpSubItem, *lpDelItem;
4045 HDPA hdpaSubItems;
4046 INT nItem, nSubItem, i;
4048 for (nItem = 0; nItem < infoPtr->nItemCount; nItem++)
4050 hdpaSubItems = (HDPA)DPA_GetPtr(infoPtr->hdpaItems, nItem);
4051 nSubItem = 0;
4052 lpDelItem = 0;
4053 for (i = 1; i < hdpaSubItems->nItemCount; i++)
4055 lpSubItem = (SUBITEM_INFO *)DPA_GetPtr(hdpaSubItems, i);
4056 if (lpSubItem->iSubItem == nColumn)
4058 nSubItem = i;
4059 lpDelItem = lpSubItem;
4061 else if (lpSubItem->iSubItem > nColumn)
4063 lpSubItem->iSubItem--;
4067 /* if we found our subitem, zapp it */
4068 if (nSubItem > 0)
4070 /* free string */
4071 if (is_textW(lpDelItem->hdr.pszText))
4072 COMCTL32_Free(lpDelItem->hdr.pszText);
4074 /* free item */
4075 COMCTL32_Free(lpDelItem);
4077 /* free dpa memory */
4078 DPA_DeletePtr(hdpaSubItems, nSubItem);
4083 /* update the other column info */
4084 LISTVIEW_ScrollColumns(infoPtr, nColumn, -(rcCol.right - rcCol.left));
4086 return TRUE;
4089 /***
4090 * DESCRIPTION:
4091 * Invalidates the listview after an item's insertion or deletion.
4093 * PARAMETER(S):
4094 * [I] infoPtr : valid pointer to the listview structure
4095 * [I] nItem : item index
4096 * [I] dir : -1 if deleting, 1 if inserting
4098 * RETURN:
4099 * None
4101 static void LISTVIEW_ScrollOnInsert(LISTVIEW_INFO *infoPtr, INT nItem, INT dir)
4103 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
4104 INT nPerCol, nItemCol, nItemRow;
4105 RECT rcScroll;
4106 POINT Origin;
4108 /* if we don't refresh, what's the point of scrolling? */
4109 if (!is_redrawing(infoPtr)) return;
4111 assert (abs(dir) == 1);
4113 /* arrange icons if autoarrange is on */
4114 if (is_autoarrange(infoPtr))
4116 BOOL arrange = TRUE;
4117 if (dir < 0 && nItem >= infoPtr->nItemCount) arrange = FALSE;
4118 if (dir > 0 && nItem == infoPtr->nItemCount - 1) arrange = FALSE;
4119 if (arrange) LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
4122 /* scrollbars need updating */
4123 LISTVIEW_UpdateScroll(infoPtr);
4125 /* figure out the item's position */
4126 if (uView == LVS_REPORT)
4127 nPerCol = infoPtr->nItemCount + 1;
4128 else if (uView == LVS_LIST)
4129 nPerCol = LISTVIEW_GetCountPerColumn(infoPtr);
4130 else /* LVS_ICON, or LVS_SMALLICON */
4131 return;
4133 nItemCol = nItem / nPerCol;
4134 nItemRow = nItem % nPerCol;
4135 LISTVIEW_GetOrigin(infoPtr, &Origin);
4137 /* move the items below up a slot */
4138 rcScroll.left = nItemCol * infoPtr->nItemWidth;
4139 rcScroll.top = nItemRow * infoPtr->nItemHeight;
4140 rcScroll.right = rcScroll.left + infoPtr->nItemWidth;
4141 rcScroll.bottom = nPerCol * infoPtr->nItemHeight;
4142 OffsetRect(&rcScroll, Origin.x, Origin.y);
4143 TRACE("rcScroll=%s, dx=%d\n", debugrect(&rcScroll), dir * infoPtr->nItemHeight);
4144 if (IntersectRect(&rcScroll, &rcScroll, &infoPtr->rcList))
4146 TRACE("Scrolling rcScroll=%s, rcList=%s\n", debugrect(&rcScroll), debugrect(&infoPtr->rcList));
4147 ScrollWindowEx(infoPtr->hwndSelf, 0, dir * infoPtr->nItemHeight,
4148 &rcScroll, &rcScroll, 0, 0, SW_ERASE | SW_INVALIDATE);
4151 /* report has only that column, so we're done */
4152 if (uView == LVS_REPORT) return;
4154 /* now for LISTs, we have to deal with the columns to the right */
4155 rcScroll.left = (nItemCol + 1) * infoPtr->nItemWidth;
4156 rcScroll.top = 0;
4157 rcScroll.right = (infoPtr->nItemCount / nPerCol + 1) * infoPtr->nItemWidth;
4158 rcScroll.bottom = nPerCol * infoPtr->nItemHeight;
4159 OffsetRect(&rcScroll, Origin.x, Origin.y);
4160 if (IntersectRect(&rcScroll, &rcScroll, &infoPtr->rcList))
4161 ScrollWindowEx(infoPtr->hwndSelf, 0, dir * infoPtr->nItemHeight,
4162 &rcScroll, &rcScroll, 0, 0, SW_ERASE | SW_INVALIDATE);
4165 /***
4166 * DESCRIPTION:
4167 * Removes an item from the listview control.
4169 * PARAMETER(S):
4170 * [I] infoPtr : valid pointer to the listview structure
4171 * [I] nItem : item index
4173 * RETURN:
4174 * SUCCESS : TRUE
4175 * FAILURE : FALSE
4177 static BOOL LISTVIEW_DeleteItem(LISTVIEW_INFO *infoPtr, INT nItem)
4179 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
4180 LVITEMW item;
4182 TRACE("(nItem=%d)\n", nItem);
4184 if (nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
4186 /* remove selection, and focus */
4187 item.state = 0;
4188 item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
4189 LISTVIEW_SetItemState(infoPtr, nItem, &item);
4191 /* send LVN_DELETEITEM notification. */
4192 notify_deleteitem(infoPtr, nItem);
4194 /* we need to do this here, because we'll be deleting stuff */
4195 if (uView == LVS_SMALLICON || uView == LVS_ICON)
4196 LISTVIEW_InvalidateItem(infoPtr, nItem);
4198 if (!(infoPtr->dwStyle & LVS_OWNERDATA))
4200 HDPA hdpaSubItems;
4201 ITEMHDR *hdrItem;
4202 INT i;
4204 hdpaSubItems = (HDPA)DPA_DeletePtr(infoPtr->hdpaItems, nItem);
4205 for (i = 0; i < hdpaSubItems->nItemCount; i++)
4207 hdrItem = (ITEMHDR *)DPA_GetPtr(hdpaSubItems, i);
4208 if (is_textW(hdrItem->pszText)) COMCTL32_Free(hdrItem->pszText);
4209 COMCTL32_Free(hdrItem);
4211 DPA_Destroy(hdpaSubItems);
4214 if (uView == LVS_SMALLICON || uView == LVS_ICON)
4216 DPA_DeletePtr(infoPtr->hdpaPosX, nItem);
4217 DPA_DeletePtr(infoPtr->hdpaPosY, nItem);
4220 infoPtr->nItemCount--;
4221 LISTVIEW_ShiftIndices(infoPtr, nItem, -1);
4223 /* now is the invalidation fun */
4224 LISTVIEW_ScrollOnInsert(infoPtr, nItem, -1);
4225 return TRUE;
4229 /***
4230 * DESCRIPTION:
4231 * Callback implementation for editlabel control
4233 * PARAMETER(S):
4234 * [I] infoPtr : valid pointer to the listview structure
4235 * [I] pszText : modified text
4236 * [I] isW : TRUE if psxText is Unicode, FALSE if it's ANSI
4238 * RETURN:
4239 * SUCCESS : TRUE
4240 * FAILURE : FALSE
4242 static BOOL LISTVIEW_EndEditLabelT(LISTVIEW_INFO *infoPtr, LPWSTR pszText, BOOL isW)
4244 NMLVDISPINFOW dispInfo;
4246 TRACE("(pszText=%s, isW=%d)\n", debugtext_t(pszText, isW), isW);
4248 ZeroMemory(&dispInfo, sizeof(dispInfo));
4249 dispInfo.item.mask = LVIF_PARAM | LVIF_STATE;
4250 dispInfo.item.iItem = infoPtr->nEditLabelItem;
4251 dispInfo.item.iSubItem = 0;
4252 dispInfo.item.stateMask = ~0;
4253 if (!LISTVIEW_GetItemW(infoPtr, &dispInfo.item)) return FALSE;
4254 /* add the text from the edit in */
4255 dispInfo.item.mask |= LVIF_TEXT;
4256 dispInfo.item.pszText = pszText;
4257 dispInfo.item.cchTextMax = textlenT(pszText, isW);
4259 /* Do we need to update the Item Text */
4260 if (!notify_dispinfoT(infoPtr, LVN_ENDLABELEDITW, &dispInfo, isW)) return FALSE;
4261 if (!pszText) return TRUE;
4263 ZeroMemory(&dispInfo, sizeof(dispInfo));
4264 dispInfo.item.mask = LVIF_TEXT;
4265 dispInfo.item.iItem = infoPtr->nEditLabelItem;
4266 dispInfo.item.iSubItem = 0;
4267 dispInfo.item.pszText = pszText;
4268 dispInfo.item.cchTextMax = textlenT(pszText, isW);
4269 return LISTVIEW_SetItemT(infoPtr, &dispInfo.item, isW);
4272 /***
4273 * DESCRIPTION:
4274 * Begin in place editing of specified list view item
4276 * PARAMETER(S):
4277 * [I] infoPtr : valid pointer to the listview structure
4278 * [I] nItem : item index
4279 * [I] isW : TRUE if it's a Unicode req, FALSE if ASCII
4281 * RETURN:
4282 * SUCCESS : TRUE
4283 * FAILURE : FALSE
4285 static HWND LISTVIEW_EditLabelT(LISTVIEW_INFO *infoPtr, INT nItem, BOOL isW)
4287 WCHAR szDispText[DISP_TEXT_SIZE] = { 0 };
4288 NMLVDISPINFOW dispInfo;
4289 RECT rect;
4291 TRACE("(nItem=%d, isW=%d)\n", nItem, isW);
4293 if (~infoPtr->dwStyle & LVS_EDITLABELS) return 0;
4294 if (nItem < 0 || nItem >= infoPtr->nItemCount) return 0;
4296 infoPtr->nEditLabelItem = nItem;
4298 /* Is the EditBox still there, if so remove it */
4299 if(infoPtr->hwndEdit != 0)
4301 SetFocus(infoPtr->hwndSelf);
4302 infoPtr->hwndEdit = 0;
4305 LISTVIEW_SetSelection(infoPtr, nItem);
4306 LISTVIEW_SetItemFocus(infoPtr, nItem);
4307 LISTVIEW_InvalidateItem(infoPtr, nItem);
4309 rect.left = LVIR_LABEL;
4310 if (!LISTVIEW_GetItemRect(infoPtr, nItem, &rect)) return 0;
4312 ZeroMemory(&dispInfo, sizeof(dispInfo));
4313 dispInfo.item.mask = LVIF_PARAM | LVIF_STATE | LVIF_TEXT;
4314 dispInfo.item.iItem = nItem;
4315 dispInfo.item.iSubItem = 0;
4316 dispInfo.item.stateMask = ~0;
4317 dispInfo.item.pszText = szDispText;
4318 dispInfo.item.cchTextMax = DISP_TEXT_SIZE;
4319 if (!LISTVIEW_GetItemT(infoPtr, &dispInfo.item, isW)) return 0;
4321 infoPtr->hwndEdit = CreateEditLabelT(infoPtr, dispInfo.item.pszText, WS_VISIBLE,
4322 rect.left-2, rect.top-1, 0, rect.bottom - rect.top+2, isW);
4323 if (!infoPtr->hwndEdit) return 0;
4325 if (notify_dispinfoT(infoPtr, LVN_BEGINLABELEDITW, &dispInfo, isW))
4327 SendMessageW(infoPtr->hwndEdit, WM_CLOSE, 0, 0);
4328 infoPtr->hwndEdit = 0;
4329 return 0;
4332 ShowWindow(infoPtr->hwndEdit, SW_NORMAL);
4333 SetFocus(infoPtr->hwndEdit);
4334 SendMessageW(infoPtr->hwndEdit, EM_SETSEL, 0, -1);
4335 return infoPtr->hwndEdit;
4339 /***
4340 * DESCRIPTION:
4341 * Ensures the specified item is visible, scrolling into view if necessary.
4343 * PARAMETER(S):
4344 * [I] infoPtr : valid pointer to the listview structure
4345 * [I] nItem : item index
4346 * [I] bPartial : partially or entirely visible
4348 * RETURN:
4349 * SUCCESS : TRUE
4350 * FAILURE : FALSE
4352 static BOOL LISTVIEW_EnsureVisible(LISTVIEW_INFO *infoPtr, INT nItem, BOOL bPartial)
4354 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
4355 INT nScrollPosHeight = 0;
4356 INT nScrollPosWidth = 0;
4357 INT nHorzAdjust = 0;
4358 INT nVertAdjust = 0;
4359 INT nHorzDiff = 0;
4360 INT nVertDiff = 0;
4361 RECT rcItem, rcTemp;
4363 rcItem.left = LVIR_BOUNDS;
4364 if (!LISTVIEW_GetItemRect(infoPtr, nItem, &rcItem)) return FALSE;
4366 if (bPartial && IntersectRect(&rcTemp, &infoPtr->rcList, &rcItem)) return TRUE;
4368 if (rcItem.left < infoPtr->rcList.left || rcItem.right > infoPtr->rcList.right)
4370 /* scroll left/right, but in LVS_REPORT mode */
4371 if (uView == LVS_LIST)
4372 nScrollPosWidth = infoPtr->nItemWidth;
4373 else if ((uView == LVS_SMALLICON) || (uView == LVS_ICON))
4374 nScrollPosWidth = 1;
4376 if (rcItem.left < infoPtr->rcList.left)
4378 nHorzAdjust = -1;
4379 if (uView != LVS_REPORT) nHorzDiff = rcItem.left - infoPtr->rcList.left;
4381 else
4383 nHorzAdjust = 1;
4384 if (uView != LVS_REPORT) nHorzDiff = rcItem.right - infoPtr->rcList.right;
4388 if (rcItem.top < infoPtr->rcList.top || rcItem.bottom > infoPtr->rcList.bottom)
4390 /* scroll up/down, but not in LVS_LIST mode */
4391 if (uView == LVS_REPORT)
4392 nScrollPosHeight = infoPtr->nItemHeight;
4393 else if ((uView == LVS_ICON) || (uView == LVS_SMALLICON))
4394 nScrollPosHeight = 1;
4396 if (rcItem.top < infoPtr->rcList.top)
4398 nVertAdjust = -1;
4399 if (uView != LVS_LIST) nVertDiff = rcItem.top - infoPtr->rcList.top;
4401 else
4403 nVertAdjust = 1;
4404 if (uView != LVS_LIST) nVertDiff = rcItem.bottom - infoPtr->rcList.bottom;
4408 if (!nScrollPosWidth && !nScrollPosHeight) return TRUE;
4410 if (nScrollPosWidth)
4412 INT diff = nHorzDiff / nScrollPosWidth;
4413 if (nHorzDiff % nScrollPosWidth) diff += nHorzAdjust;
4414 LISTVIEW_HScroll(infoPtr, SB_INTERNAL, diff, 0);
4417 if (nScrollPosHeight)
4419 INT diff = nVertDiff / nScrollPosHeight;
4420 if (nVertDiff % nScrollPosHeight) diff += nVertAdjust;
4421 LISTVIEW_VScroll(infoPtr, SB_INTERNAL, diff, 0);
4424 return TRUE;
4427 /***
4428 * DESCRIPTION:
4429 * Searches for an item with specific characteristics.
4431 * PARAMETER(S):
4432 * [I] hwnd : window handle
4433 * [I] nStart : base item index
4434 * [I] lpFindInfo : item information to look for
4436 * RETURN:
4437 * SUCCESS : index of item
4438 * FAILURE : -1
4440 static INT LISTVIEW_FindItemW(LISTVIEW_INFO *infoPtr, INT nStart,
4441 const LVFINDINFOW *lpFindInfo)
4443 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
4444 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
4445 BOOL bWrap = FALSE, bNearest = FALSE;
4446 INT nItem = nStart + 1, nLast = infoPtr->nItemCount, nNearestItem = -1;
4447 ULONG xdist, ydist, dist, mindist = 0x7fffffff;
4448 POINT Position, Destination;
4449 LVITEMW lvItem;
4451 if (!lpFindInfo || nItem < 0) return -1;
4453 lvItem.mask = 0;
4454 if (lpFindInfo->flags & (LVFI_STRING | LVFI_PARTIAL))
4456 lvItem.mask |= LVIF_TEXT;
4457 lvItem.pszText = szDispText;
4458 lvItem.cchTextMax = DISP_TEXT_SIZE;
4461 if (lpFindInfo->flags & LVFI_WRAP)
4462 bWrap = TRUE;
4464 if ((lpFindInfo->flags & LVFI_NEARESTXY) &&
4465 (uView == LVS_ICON || uView ==LVS_SMALLICON))
4467 POINT Origin;
4468 RECT rcArea;
4470 LISTVIEW_GetOrigin(infoPtr, &Origin);
4471 Destination.x = lpFindInfo->pt.x - Origin.x;
4472 Destination.y = lpFindInfo->pt.y - Origin.y;
4473 switch(lpFindInfo->vkDirection)
4475 case VK_DOWN: Destination.y += infoPtr->nItemHeight; break;
4476 case VK_UP: Destination.y -= infoPtr->nItemHeight; break;
4477 case VK_RIGHT: Destination.x += infoPtr->nItemWidth; break;
4478 case VK_LEFT: Destination.x -= infoPtr->nItemWidth; break;
4479 case VK_HOME: Destination.x = Destination.y = 0; break;
4480 case VK_NEXT: Destination.y += infoPtr->rcList.bottom - infoPtr->rcList.top; break;
4481 case VK_PRIOR: Destination.y -= infoPtr->rcList.bottom - infoPtr->rcList.top; break;
4482 case VK_END:
4483 LISTVIEW_GetAreaRect(infoPtr, &rcArea);
4484 Destination.x = rcArea.right;
4485 Destination.y = rcArea.bottom;
4486 break;
4487 default: ERR("Unknown vkDirection=%d\n", lpFindInfo->vkDirection);
4489 bNearest = TRUE;
4492 /* if LVFI_PARAM is specified, all other flags are ignored */
4493 if (lpFindInfo->flags & LVFI_PARAM)
4495 lvItem.mask |= LVIF_PARAM;
4496 bNearest = FALSE;
4497 lvItem.mask &= ~LVIF_TEXT;
4500 again:
4501 for (; nItem < nLast; nItem++)
4503 lvItem.iItem = nItem;
4504 lvItem.iSubItem = 0;
4505 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) continue;
4507 if (lvItem.mask & LVIF_PARAM)
4509 if (lpFindInfo->lParam == lvItem.lParam)
4510 return nItem;
4511 else
4512 continue;
4515 if (lvItem.mask & LVIF_TEXT)
4517 if (lpFindInfo->flags & LVFI_PARTIAL)
4519 if (strstrW(lvItem.pszText, lpFindInfo->psz) == NULL) continue;
4521 else
4523 if (lstrcmpW(lvItem.pszText, lpFindInfo->psz) != 0) continue;
4527 if (!bNearest) return nItem;
4529 /* This is very inefficient. To do a good job here,
4530 * we need a sorted array of (x,y) item positions */
4531 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position);
4533 /* compute the distance^2 to the destination */
4534 xdist = Destination.x - Position.x;
4535 ydist = Destination.y - Position.y;
4536 dist = xdist * xdist + ydist * ydist;
4538 /* remember the distance, and item if it's closer */
4539 if (dist < mindist)
4541 mindist = dist;
4542 nNearestItem = nItem;
4546 if (bWrap)
4548 nItem = 0;
4549 nLast = min(nStart + 1, infoPtr->nItemCount);
4550 bWrap = FALSE;
4551 goto again;
4554 return nNearestItem;
4557 /***
4558 * DESCRIPTION:
4559 * Searches for an item with specific characteristics.
4561 * PARAMETER(S):
4562 * [I] hwnd : window handle
4563 * [I] nStart : base item index
4564 * [I] lpFindInfo : item information to look for
4566 * RETURN:
4567 * SUCCESS : index of item
4568 * FAILURE : -1
4570 static INT LISTVIEW_FindItemA(LISTVIEW_INFO *infoPtr, INT nStart,
4571 const LVFINDINFOA *lpFindInfo)
4573 BOOL hasText = lpFindInfo->flags & (LVFI_STRING | LVFI_PARTIAL);
4574 LVFINDINFOW fiw;
4575 INT res;
4577 memcpy(&fiw, lpFindInfo, sizeof(fiw));
4578 if (hasText) fiw.psz = textdupTtoW((LPCWSTR)lpFindInfo->psz, FALSE);
4579 res = LISTVIEW_FindItemW(infoPtr, nStart, &fiw);
4580 if (hasText) textfreeT((LPWSTR)fiw.psz, FALSE);
4581 return res;
4584 /***
4585 * DESCRIPTION:
4586 * Retrieves the background image of the listview control.
4588 * PARAMETER(S):
4589 * [I] infoPtr : valid pointer to the listview structure
4590 * [O] lpBkImage : background image attributes
4592 * RETURN:
4593 * SUCCESS : TRUE
4594 * FAILURE : FALSE
4596 /* static BOOL LISTVIEW_GetBkImage(LISTVIEW_INFO *infoPtr, LPLVBKIMAGE lpBkImage) */
4597 /* { */
4598 /* FIXME (listview, "empty stub!\n"); */
4599 /* return FALSE; */
4600 /* } */
4602 /***
4603 * DESCRIPTION:
4604 * Retrieves column attributes.
4606 * PARAMETER(S):
4607 * [I] infoPtr : valid pointer to the listview structure
4608 * [I] nColumn : column index
4609 * [IO] lpColumn : column information
4610 * [I] isW : if TRUE, then lpColumn is a LPLVCOLUMNW
4611 * otherwise it is in fact a LPLVCOLUMNA
4613 * RETURN:
4614 * SUCCESS : TRUE
4615 * FAILURE : FALSE
4617 static BOOL LISTVIEW_GetColumnT(LISTVIEW_INFO *infoPtr, INT nColumn, LPLVCOLUMNW lpColumn, BOOL isW)
4619 COLUMN_INFO *lpColumnInfo;
4620 HDITEMW hdi;
4622 if (!lpColumn || nColumn < 0 || nColumn >= infoPtr->hdpaColumns->nItemCount) return FALSE;
4623 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, nColumn);
4625 /* initialize memory */
4626 ZeroMemory(&hdi, sizeof(hdi));
4628 if (lpColumn->mask & LVCF_TEXT)
4630 hdi.mask |= HDI_TEXT;
4631 hdi.pszText = lpColumn->pszText;
4632 hdi.cchTextMax = lpColumn->cchTextMax;
4635 if (lpColumn->mask & LVCF_IMAGE)
4636 hdi.mask |= HDI_IMAGE;
4638 if (lpColumn->mask & LVCF_ORDER)
4639 hdi.mask |= HDI_ORDER;
4641 if (!SendMessageW(infoPtr->hwndHeader, isW ? HDM_GETITEMW : HDM_GETITEMA, nColumn, (LPARAM)&hdi)) return FALSE;
4643 if (lpColumn->mask & LVCF_FMT)
4644 lpColumn->fmt = lpColumnInfo->fmt;
4646 if (lpColumn->mask & LVCF_WIDTH)
4647 lpColumn->cx = lpColumnInfo->rcHeader.right - lpColumnInfo->rcHeader.left;
4649 if (lpColumn->mask & LVCF_IMAGE)
4650 lpColumn->iImage = hdi.iImage;
4652 if (lpColumn->mask & LVCF_ORDER)
4653 lpColumn->iOrder = hdi.iOrder;
4655 return TRUE;
4659 static BOOL LISTVIEW_GetColumnOrderArray(LISTVIEW_INFO *infoPtr, INT iCount, LPINT lpiArray)
4661 INT i;
4663 if (!lpiArray)
4664 return FALSE;
4666 /* FIXME: little hack */
4667 for (i = 0; i < iCount; i++)
4668 lpiArray[i] = i;
4670 return TRUE;
4673 /***
4674 * DESCRIPTION:
4675 * Retrieves the column width.
4677 * PARAMETER(S):
4678 * [I] infoPtr : valid pointer to the listview structure
4679 * [I] int : column index
4681 * RETURN:
4682 * SUCCESS : column width
4683 * FAILURE : zero
4685 static INT LISTVIEW_GetColumnWidth(LISTVIEW_INFO *infoPtr, INT nColumn)
4687 INT nColumnWidth = 0;
4688 RECT rcHeader;
4690 TRACE("nColumn=%d\n", nColumn);
4692 /* we have a 'column' in LIST and REPORT mode only */
4693 switch(infoPtr->dwStyle & LVS_TYPEMASK)
4695 case LVS_LIST:
4696 nColumnWidth = infoPtr->nItemWidth;
4697 break;
4698 case LVS_REPORT:
4699 if (nColumn < 0 || nColumn >= infoPtr->hdpaColumns->nItemCount) return 0;
4700 LISTVIEW_GetHeaderRect(infoPtr, nColumn, &rcHeader);
4701 nColumnWidth = rcHeader.right - rcHeader.left;
4702 break;
4705 TRACE("nColumnWidth=%d\n", nColumnWidth);
4706 return nColumnWidth;
4709 /***
4710 * DESCRIPTION:
4711 * In list or report display mode, retrieves the number of items that can fit
4712 * vertically in the visible area. In icon or small icon display mode,
4713 * retrieves the total number of visible items.
4715 * PARAMETER(S):
4716 * [I] infoPtr : valid pointer to the listview structure
4718 * RETURN:
4719 * Number of fully visible items.
4721 static INT LISTVIEW_GetCountPerPage(LISTVIEW_INFO *infoPtr)
4723 switch (infoPtr->dwStyle & LVS_TYPEMASK)
4725 case LVS_ICON:
4726 case LVS_SMALLICON:
4727 return infoPtr->nItemCount;
4728 case LVS_REPORT:
4729 return LISTVIEW_GetCountPerColumn(infoPtr);
4730 case LVS_LIST:
4731 return LISTVIEW_GetCountPerRow(infoPtr) * LISTVIEW_GetCountPerColumn(infoPtr);
4733 assert(FALSE);
4736 /***
4737 * DESCRIPTION:
4738 * Retrieves an image list handle.
4740 * PARAMETER(S):
4741 * [I] infoPtr : valid pointer to the listview structure
4742 * [I] nImageList : image list identifier
4744 * RETURN:
4745 * SUCCESS : image list handle
4746 * FAILURE : NULL
4748 static HIMAGELIST LISTVIEW_GetImageList(LISTVIEW_INFO *infoPtr, INT nImageList)
4750 switch (nImageList)
4752 case LVSIL_NORMAL: return infoPtr->himlNormal;
4753 case LVSIL_SMALL: return infoPtr->himlSmall;
4754 case LVSIL_STATE: return infoPtr->himlState;
4756 return NULL;
4759 /* LISTVIEW_GetISearchString */
4761 /***
4762 * DESCRIPTION:
4763 * Retrieves item attributes.
4765 * PARAMETER(S):
4766 * [I] hwnd : window handle
4767 * [IO] lpLVItem : item info
4768 * [I] isW : if TRUE, then lpLVItem is a LPLVITEMW,
4769 * if FALSE, the lpLVItem is a LPLVITEMA.
4771 * NOTE:
4772 * This is the internal 'GetItem' interface -- it tries to
4773 * be smart, and avoids text copies, if possible, by modifing
4774 * lpLVItem->pszText to point to the text string. Please note
4775 * that this is not always possible (e.g. OWNERDATA), so on
4776 * entry you *must* supply valid values for pszText, and cchTextMax.
4777 * The only difference to the documented interface is that upon
4778 * return, you should use *only* the lpLVItem->pszText, rather than
4779 * the buffer pointer you provided on input. Most code already does
4780 * that, so it's not a problem.
4781 * For the two cases when the text must be copied (that is,
4782 * for LVM_GETITEM, and LVMGETITEMTEXT), use LISTVIEW_GetItemExtT.
4784 * RETURN:
4785 * SUCCESS : TRUE
4786 * FAILURE : FALSE
4788 static BOOL LISTVIEW_GetItemT(LISTVIEW_INFO *infoPtr, LPLVITEMW lpLVItem, BOOL isW)
4790 ITEMHDR callbackHdr = { LPSTR_TEXTCALLBACKW, I_IMAGECALLBACK };
4791 NMLVDISPINFOW dispInfo;
4792 ITEM_INFO *lpItem;
4793 ITEMHDR* pItemHdr;
4794 HDPA hdpaSubItems;
4796 TRACE("(lpLVItem=%s, isW=%d)\n", debuglvitem_t(lpLVItem, isW), isW);
4798 if (!lpLVItem || lpLVItem->iItem < 0 || lpLVItem->iItem >= infoPtr->nItemCount)
4799 return FALSE;
4801 if (lpLVItem->mask == 0) return TRUE;
4803 /* a quick optimization if all we're asked is the focus state
4804 * these queries are worth optimising since they are common,
4805 * and can be answered in constant time, without the heavy accesses */
4806 if ( (lpLVItem->mask == LVIF_STATE) && (lpLVItem->stateMask == LVIS_FOCUSED) &&
4807 !(infoPtr->uCallbackMask & LVIS_FOCUSED) )
4809 lpLVItem->state = 0;
4810 if (infoPtr->nFocusedItem == lpLVItem->iItem)
4811 lpLVItem->state |= LVIS_FOCUSED;
4812 return TRUE;
4815 ZeroMemory(&dispInfo, sizeof(dispInfo));
4817 /* if the app stores all the data, handle it separately */
4818 if (infoPtr->dwStyle & LVS_OWNERDATA)
4820 dispInfo.item.state = 0;
4822 /* apprently, we should not callback for lParam in LVS_OWNERDATA */
4823 if ((lpLVItem->mask & ~(LVIF_STATE | LVIF_PARAM)) || infoPtr->uCallbackMask)
4825 /* NOTE: copy only fields which we _know_ are initialized, some apps
4826 * depend on the uninitialized fields being 0 */
4827 dispInfo.item.mask = lpLVItem->mask & ~LVIF_PARAM;
4828 dispInfo.item.iItem = lpLVItem->iItem;
4829 dispInfo.item.iSubItem = lpLVItem->iSubItem;
4830 if (lpLVItem->mask & LVIF_TEXT)
4832 dispInfo.item.pszText = lpLVItem->pszText;
4833 dispInfo.item.cchTextMax = lpLVItem->cchTextMax;
4835 if (lpLVItem->mask & LVIF_STATE)
4836 dispInfo.item.stateMask = lpLVItem->stateMask & infoPtr->uCallbackMask;
4837 notify_dispinfoT(infoPtr, LVN_GETDISPINFOW, &dispInfo, isW);
4838 dispInfo.item.stateMask = lpLVItem->stateMask;
4839 *lpLVItem = dispInfo.item;
4840 TRACE(" getdispinfo(1):lpLVItem=%s\n", debuglvitem_t(lpLVItem, isW));
4843 /* make sure lParam is zeroed out */
4844 if (lpLVItem->mask & LVIF_PARAM) lpLVItem->lParam = 0;
4846 /* we store only a little state, so if we're not asked, we're done */
4847 if (!(lpLVItem->mask & LVIF_STATE) || lpLVItem->iSubItem) return TRUE;
4849 /* if focus is handled by us, report it */
4850 if ( lpLVItem->stateMask & ~infoPtr->uCallbackMask & LVIS_FOCUSED )
4852 lpLVItem->state &= ~LVIS_FOCUSED;
4853 if (infoPtr->nFocusedItem == lpLVItem->iItem)
4854 lpLVItem->state |= LVIS_FOCUSED;
4857 /* and do the same for selection, if we handle it */
4858 if ( lpLVItem->stateMask & ~infoPtr->uCallbackMask & LVIS_SELECTED )
4860 lpLVItem->state &= ~LVIS_SELECTED;
4861 if (ranges_contain(infoPtr->selectionRanges, lpLVItem->iItem))
4862 lpLVItem->state |= LVIS_SELECTED;
4865 return TRUE;
4868 /* find the item and subitem structures before we proceed */
4869 hdpaSubItems = (HDPA)DPA_GetPtr(infoPtr->hdpaItems, lpLVItem->iItem);
4870 lpItem = (ITEM_INFO *)DPA_GetPtr(hdpaSubItems, 0);
4871 assert (lpItem);
4873 if (lpLVItem->iSubItem)
4875 SUBITEM_INFO *lpSubItem = LISTVIEW_GetSubItemPtr(hdpaSubItems, lpLVItem->iSubItem);
4876 pItemHdr = lpSubItem ? &lpSubItem->hdr : &callbackHdr;
4878 else
4879 pItemHdr = &lpItem->hdr;
4881 /* Do we need to query the state from the app? */
4882 if ((lpLVItem->mask & LVIF_STATE) && infoPtr->uCallbackMask && lpLVItem->iSubItem == 0)
4884 dispInfo.item.mask |= LVIF_STATE;
4885 dispInfo.item.stateMask = infoPtr->uCallbackMask;
4888 /* Do we need to enquire about the image? */
4889 if ((lpLVItem->mask & LVIF_IMAGE) && pItemHdr->iImage == I_IMAGECALLBACK)
4890 dispInfo.item.mask |= LVIF_IMAGE;
4892 /* Apps depend on calling back for text if it is NULL or LPSTR_TEXTCALLBACKW */
4893 if ((lpLVItem->mask & LVIF_TEXT) && !is_textW(pItemHdr->pszText))
4895 dispInfo.item.mask |= LVIF_TEXT;
4896 dispInfo.item.pszText = lpLVItem->pszText;
4897 dispInfo.item.cchTextMax = lpLVItem->cchTextMax;
4898 if (dispInfo.item.pszText && dispInfo.item.cchTextMax > 0)
4899 *dispInfo.item.pszText = '\0';
4902 /* If we don't have all the requested info, query the application */
4903 if (dispInfo.item.mask != 0)
4905 dispInfo.item.iItem = lpLVItem->iItem;
4906 dispInfo.item.iSubItem = lpLVItem->iSubItem;
4907 dispInfo.item.lParam = lpItem->lParam;
4908 notify_dispinfoT(infoPtr, LVN_GETDISPINFOW, &dispInfo, isW);
4909 TRACE(" getdispinfo(2):item=%s\n", debuglvitem_t(&dispInfo.item, isW));
4912 /* we should not store values for subitems */
4913 if (lpLVItem->iSubItem) dispInfo.item.mask &= ~LVIF_DI_SETITEM;
4915 /* Now, handle the iImage field */
4916 if (dispInfo.item.mask & LVIF_IMAGE)
4918 lpLVItem->iImage = dispInfo.item.iImage;
4919 if ((dispInfo.item.mask & LVIF_DI_SETITEM) && pItemHdr->iImage == I_IMAGECALLBACK)
4920 pItemHdr->iImage = dispInfo.item.iImage;
4922 else if (lpLVItem->mask & LVIF_IMAGE)
4923 lpLVItem->iImage = pItemHdr->iImage;
4925 /* The pszText field */
4926 if (dispInfo.item.mask & LVIF_TEXT)
4928 if ((dispInfo.item.mask & LVIF_DI_SETITEM) && pItemHdr->pszText)
4929 textsetptrT(&pItemHdr->pszText, dispInfo.item.pszText, isW);
4931 lpLVItem->pszText = dispInfo.item.pszText;
4933 else if (lpLVItem->mask & LVIF_TEXT)
4935 if (isW) lpLVItem->pszText = pItemHdr->pszText;
4936 else textcpynT(lpLVItem->pszText, isW, pItemHdr->pszText, TRUE, lpLVItem->cchTextMax);
4939 /* if this is a subitem, we're done */
4940 if (lpLVItem->iSubItem) return TRUE;
4942 /* Next is the lParam field */
4943 if (dispInfo.item.mask & LVIF_PARAM)
4945 lpLVItem->lParam = dispInfo.item.lParam;
4946 if ((dispInfo.item.mask & LVIF_DI_SETITEM))
4947 lpItem->lParam = dispInfo.item.lParam;
4949 else if (lpLVItem->mask & LVIF_PARAM)
4950 lpLVItem->lParam = lpItem->lParam;
4952 /* ... the state field (this one is different due to uCallbackmask) */
4953 if (lpLVItem->mask & LVIF_STATE)
4955 lpLVItem->state = lpItem->state;
4956 if (dispInfo.item.mask & LVIF_STATE)
4958 lpLVItem->state &= ~dispInfo.item.stateMask;
4959 lpLVItem->state |= (dispInfo.item.state & dispInfo.item.stateMask);
4961 if ( lpLVItem->stateMask & ~infoPtr->uCallbackMask & LVIS_FOCUSED )
4963 lpLVItem->state &= ~LVIS_FOCUSED;
4964 if (infoPtr->nFocusedItem == lpLVItem->iItem)
4965 lpLVItem->state |= LVIS_FOCUSED;
4967 if ( lpLVItem->stateMask & ~infoPtr->uCallbackMask & LVIS_SELECTED )
4969 lpLVItem->state &= ~LVIS_SELECTED;
4970 if (ranges_contain(infoPtr->selectionRanges, lpLVItem->iItem))
4971 lpLVItem->state |= LVIS_SELECTED;
4975 /* and last, but not least, the indent field */
4976 if (lpLVItem->mask & LVIF_INDENT)
4977 lpLVItem->iIndent = lpItem->iIndent;
4979 return TRUE;
4982 /***
4983 * DESCRIPTION:
4984 * Retrieves item attributes.
4986 * PARAMETER(S):
4987 * [I] hwnd : window handle
4988 * [IO] lpLVItem : item info
4989 * [I] isW : if TRUE, then lpLVItem is a LPLVITEMW,
4990 * if FALSE, the lpLVItem is a LPLVITEMA.
4992 * NOTE:
4993 * This is the external 'GetItem' interface -- it properly copies
4994 * the text in the provided buffer.
4996 * RETURN:
4997 * SUCCESS : TRUE
4998 * FAILURE : FALSE
5000 static BOOL LISTVIEW_GetItemExtT(LISTVIEW_INFO *infoPtr, LPLVITEMW lpLVItem, BOOL isW)
5002 LPWSTR pszText;
5003 BOOL bResult;
5005 if (!lpLVItem || lpLVItem->iItem < 0 || lpLVItem->iItem >= infoPtr->nItemCount)
5006 return FALSE;
5008 pszText = lpLVItem->pszText;
5009 bResult = LISTVIEW_GetItemT(infoPtr, lpLVItem, isW);
5010 if (bResult && lpLVItem->pszText != pszText)
5011 textcpynT(pszText, isW, lpLVItem->pszText, isW, lpLVItem->cchTextMax);
5012 lpLVItem->pszText = pszText;
5014 return bResult;
5018 /***
5019 * DESCRIPTION:
5020 * Retrieves the position (upper-left) of the listview control item.
5021 * Note that for LVS_ICON style, the upper-left is that of the icon
5022 * and not the bounding box.
5024 * PARAMETER(S):
5025 * [I] infoPtr : valid pointer to the listview structure
5026 * [I] nItem : item index
5027 * [O] lpptPosition : coordinate information
5029 * RETURN:
5030 * SUCCESS : TRUE
5031 * FAILURE : FALSE
5033 static BOOL LISTVIEW_GetItemPosition(LISTVIEW_INFO *infoPtr, INT nItem, LPPOINT lpptPosition)
5035 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
5036 POINT Origin;
5038 TRACE("(nItem=%d, lpptPosition=%p)\n", nItem, lpptPosition);
5040 if (!lpptPosition || nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
5042 LISTVIEW_GetOrigin(infoPtr, &Origin);
5043 LISTVIEW_GetItemOrigin(infoPtr, nItem, lpptPosition);
5045 if (uView == LVS_ICON)
5047 lpptPosition->x += (infoPtr->nItemWidth - infoPtr->iconSize.cx) / 2;
5048 lpptPosition->y += ICON_TOP_PADDING;
5050 lpptPosition->x += Origin.x;
5051 lpptPosition->y += Origin.y;
5053 TRACE (" lpptPosition=%s\n", debugpoint(lpptPosition));
5054 return TRUE;
5058 /***
5059 * DESCRIPTION:
5060 * Retrieves the bounding rectangle for a listview control item.
5062 * PARAMETER(S):
5063 * [I] infoPtr : valid pointer to the listview structure
5064 * [I] nItem : item index
5065 * [IO] lprc : bounding rectangle coordinates
5066 * lprc->left specifies the portion of the item for which the bounding
5067 * rectangle will be retrieved.
5069 * LVIR_BOUNDS Returns the bounding rectangle of the entire item,
5070 * including the icon and label.
5072 * * For LVS_ICON
5073 * * Experiment shows that native control returns:
5074 * * width = min (48, length of text line)
5075 * * .left = position.x - (width - iconsize.cx)/2
5076 * * .right = .left + width
5077 * * height = #lines of text * ntmHeight + icon height + 8
5078 * * .top = position.y - 2
5079 * * .bottom = .top + height
5080 * * separation between items .y = itemSpacing.cy - height
5081 * * .x = itemSpacing.cx - width
5082 * LVIR_ICON Returns the bounding rectangle of the icon or small icon.
5084 * * For LVS_ICON
5085 * * Experiment shows that native control returns:
5086 * * width = iconSize.cx + 16
5087 * * .left = position.x - (width - iconsize.cx)/2
5088 * * .right = .left + width
5089 * * height = iconSize.cy + 4
5090 * * .top = position.y - 2
5091 * * .bottom = .top + height
5092 * * separation between items .y = itemSpacing.cy - height
5093 * * .x = itemSpacing.cx - width
5094 * LVIR_LABEL Returns the bounding rectangle of the item text.
5096 * * For LVS_ICON
5097 * * Experiment shows that native control returns:
5098 * * width = text length
5099 * * .left = position.x - width/2
5100 * * .right = .left + width
5101 * * height = ntmH * linecount + 2
5102 * * .top = position.y + iconSize.cy + 6
5103 * * .bottom = .top + height
5104 * * separation between items .y = itemSpacing.cy - height
5105 * * .x = itemSpacing.cx - width
5106 * LVIR_SELECTBOUNDS Returns the union of the LVIR_ICON and LVIR_LABEL
5107 * rectangles, but excludes columns in report view.
5109 * RETURN:
5110 * SUCCESS : TRUE
5111 * FAILURE : FALSE
5113 * NOTES
5114 * Note that the bounding rectangle of the label in the LVS_ICON view depends
5115 * upon whether the window has the focus currently and on whether the item
5116 * is the one with the focus. Ensure that the control's record of which
5117 * item has the focus agrees with the items' records.
5119 static BOOL LISTVIEW_GetItemRect(LISTVIEW_INFO *infoPtr, INT nItem, LPRECT lprc)
5121 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
5122 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
5123 BOOL doLabel = TRUE, oversizedBox = FALSE;
5124 POINT Position, Origin;
5125 LVITEMW lvItem;
5126 RECT label_rect;
5128 TRACE("(hwnd=%p, nItem=%d, lprc=%p)\n", infoPtr->hwndSelf, nItem, lprc);
5130 if (!lprc || nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
5132 LISTVIEW_GetOrigin(infoPtr, &Origin);
5133 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position);
5135 /* Be smart and try to figure out the minimum we have to do */
5136 if (lprc->left == LVIR_ICON) doLabel = FALSE;
5137 if (uView == LVS_REPORT && lprc->left == LVIR_BOUNDS) doLabel = FALSE;
5138 if (uView == LVS_ICON && lprc->left != LVIR_ICON &&
5139 infoPtr->bFocus && LISTVIEW_GetItemState(infoPtr, nItem, LVIS_FOCUSED))
5140 oversizedBox = TRUE;
5142 /* get what we need from the item before hand, so we make
5143 * only one request. This can speed up things, if data
5144 * is stored on the app side */
5145 lvItem.mask = 0;
5146 if (uView == LVS_REPORT) lvItem.mask |= LVIF_INDENT;
5147 if (doLabel) lvItem.mask |= LVIF_TEXT;
5148 lvItem.iItem = nItem;
5149 lvItem.iSubItem = 0;
5150 lvItem.pszText = szDispText;
5151 lvItem.cchTextMax = DISP_TEXT_SIZE;
5152 if (lvItem.mask && !LISTVIEW_GetItemW(infoPtr, &lvItem)) return FALSE;
5153 /* we got the state already up, simulate it here, to avoid a reget */
5154 if (uView == LVS_ICON && (lprc->left != LVIR_ICON))
5156 lvItem.mask |= LVIF_STATE;
5157 lvItem.stateMask = LVIS_FOCUSED;
5158 lvItem.state = (oversizedBox ? LVIS_FOCUSED : 0);
5161 if (uView == LVS_REPORT && (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT) && lprc->left == LVIR_SELECTBOUNDS)
5162 lprc->left = LVIR_BOUNDS;
5163 switch(lprc->left)
5165 case LVIR_ICON:
5166 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, NULL, NULL, lprc, NULL);
5167 break;
5169 case LVIR_LABEL:
5170 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, NULL, NULL, NULL, lprc);
5171 break;
5173 case LVIR_BOUNDS:
5174 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, lprc, NULL, NULL, NULL);
5175 break;
5177 case LVIR_SELECTBOUNDS:
5178 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, NULL, NULL, lprc, &label_rect);
5179 UnionRect(lprc, lprc, &label_rect);
5180 break;
5182 default:
5183 WARN("Unknown value: %d\n", lprc->left);
5184 return FALSE;
5187 OffsetRect(lprc, Position.x + Origin.x, Position.y + Origin.y);
5189 TRACE(" rect=%s\n", debugrect(lprc));
5191 return TRUE;
5194 /***
5195 * DESCRIPTION:
5196 * Retrieves the spacing between listview control items.
5198 * PARAMETER(S):
5199 * [I] infoPtr : valid pointer to the listview structure
5200 * [IO] lprc : rectangle to receive the output
5201 * on input, lprc->top = nSubItem
5202 * lprc->left = LVIR_ICON | LVIR_BOUNDS | LVIR_LABEL
5204 * NOTE: for subItem = 0, we should return the bounds of the _entire_ item,
5205 * not only those of the first column.
5206 * Fortunately, LISTVIEW_GetItemMetrics does the right thing.
5208 * RETURN:
5209 * TRUE: success
5210 * FALSE: failure
5212 static BOOL LISTVIEW_GetSubItemRect(LISTVIEW_INFO *infoPtr, INT nItem, LPRECT lprc)
5214 POINT Position, Origin;
5215 LVITEMW lvItem;
5217 if (!lprc || (infoPtr->dwStyle & LVS_TYPEMASK) != LVS_REPORT) return FALSE;
5219 TRACE("(nItem=%d, nSubItem=%d)\n", nItem, lprc->top);
5221 LISTVIEW_GetOrigin(infoPtr, &Origin);
5222 if (!LISTVIEW_GetItemPosition(infoPtr, nItem, &Position)) return FALSE;
5224 lvItem.mask = lprc->top == 0 ? LVIF_INDENT : 0;
5225 lvItem.iItem = nItem;
5226 lvItem.iSubItem = lprc->top;
5228 if (lvItem.mask && !LISTVIEW_GetItemW(infoPtr, &lvItem)) return FALSE;
5229 switch(lprc->left)
5231 case LVIR_ICON:
5232 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, NULL, NULL, lprc, NULL);
5233 break;
5235 case LVIR_LABEL:
5236 case LVIR_BOUNDS:
5237 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, lprc, NULL, NULL, NULL);
5238 break;
5240 default:
5241 ERR("Unknown bounds=%d\n", lprc->left);
5242 return FALSE;
5245 OffsetRect(lprc, Position.x + Origin.x, Position.y + Origin.y);
5246 return TRUE;
5250 /***
5251 * DESCRIPTION:
5252 * Retrieves the width of a label.
5254 * PARAMETER(S):
5255 * [I] infoPtr : valid pointer to the listview structure
5257 * RETURN:
5258 * SUCCESS : string width (in pixels)
5259 * FAILURE : zero
5261 static INT LISTVIEW_GetLabelWidth(LISTVIEW_INFO *infoPtr, INT nItem)
5263 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
5264 LVITEMW lvItem;
5266 TRACE("(nItem=%d)\n", nItem);
5268 lvItem.mask = LVIF_TEXT;
5269 lvItem.iItem = nItem;
5270 lvItem.iSubItem = 0;
5271 lvItem.pszText = szDispText;
5272 lvItem.cchTextMax = DISP_TEXT_SIZE;
5273 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) return 0;
5275 return LISTVIEW_GetStringWidthT(infoPtr, lvItem.pszText, TRUE);
5278 /***
5279 * DESCRIPTION:
5280 * Retrieves the spacing between listview control items.
5282 * PARAMETER(S):
5283 * [I] infoPtr : valid pointer to the listview structure
5284 * [I] bSmall : flag for small or large icon
5286 * RETURN:
5287 * Horizontal + vertical spacing
5289 static LONG LISTVIEW_GetItemSpacing(LISTVIEW_INFO *infoPtr, BOOL bSmall)
5291 LONG lResult;
5293 if (!bSmall)
5295 lResult = MAKELONG(infoPtr->iconSpacing.cx, infoPtr->iconSpacing.cy);
5297 else
5299 if ((infoPtr->dwStyle & LVS_TYPEMASK) == LVS_ICON)
5300 lResult = MAKELONG(DEFAULT_COLUMN_WIDTH, GetSystemMetrics(SM_CXSMICON)+HEIGHT_PADDING);
5301 else
5302 lResult = MAKELONG(infoPtr->nItemWidth, infoPtr->nItemHeight);
5304 return lResult;
5307 /***
5308 * DESCRIPTION:
5309 * Retrieves the state of a listview control item.
5311 * PARAMETER(S):
5312 * [I] infoPtr : valid pointer to the listview structure
5313 * [I] nItem : item index
5314 * [I] uMask : state mask
5316 * RETURN:
5317 * State specified by the mask.
5319 static UINT LISTVIEW_GetItemState(LISTVIEW_INFO *infoPtr, INT nItem, UINT uMask)
5321 LVITEMW lvItem;
5323 if (nItem < 0 || nItem >= infoPtr->nItemCount) return 0;
5325 lvItem.iItem = nItem;
5326 lvItem.iSubItem = 0;
5327 lvItem.mask = LVIF_STATE;
5328 lvItem.stateMask = uMask;
5329 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) return 0;
5331 return lvItem.state & uMask;
5334 /***
5335 * DESCRIPTION:
5336 * Retrieves the text of a listview control item or subitem.
5338 * PARAMETER(S):
5339 * [I] hwnd : window handle
5340 * [I] nItem : item index
5341 * [IO] lpLVItem : item information
5342 * [I] isW : TRUE if lpLVItem is Unicode
5344 * RETURN:
5345 * SUCCESS : string length
5346 * FAILURE : 0
5348 static INT LISTVIEW_GetItemTextT(LISTVIEW_INFO *infoPtr, INT nItem, LPLVITEMW lpLVItem, BOOL isW)
5350 if (!lpLVItem || nItem < 0 || nItem >= infoPtr->nItemCount) return 0;
5352 lpLVItem->mask = LVIF_TEXT;
5353 lpLVItem->iItem = nItem;
5354 if (!LISTVIEW_GetItemExtT(infoPtr, lpLVItem, isW)) return 0;
5356 return textlenT(lpLVItem->pszText, isW);
5359 /***
5360 * DESCRIPTION:
5361 * Searches for an item based on properties + relationships.
5363 * PARAMETER(S):
5364 * [I] infoPtr : valid pointer to the listview structure
5365 * [I] nItem : item index
5366 * [I] uFlags : relationship flag
5368 * RETURN:
5369 * SUCCESS : item index
5370 * FAILURE : -1
5372 static INT LISTVIEW_GetNextItem(LISTVIEW_INFO *infoPtr, INT nItem, UINT uFlags)
5374 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
5375 UINT uMask = 0;
5376 LVFINDINFOW lvFindInfo;
5377 INT nCountPerColumn;
5378 INT i;
5380 TRACE("nItem=%d, uFlags=%x, nItemCount=%d\n", nItem, uFlags, infoPtr->nItemCount);
5381 if (nItem < -1 || nItem >= infoPtr->nItemCount) return -1;
5383 ZeroMemory(&lvFindInfo, sizeof(lvFindInfo));
5385 if (uFlags & LVNI_CUT)
5386 uMask |= LVIS_CUT;
5388 if (uFlags & LVNI_DROPHILITED)
5389 uMask |= LVIS_DROPHILITED;
5391 if (uFlags & LVNI_FOCUSED)
5392 uMask |= LVIS_FOCUSED;
5394 if (uFlags & LVNI_SELECTED)
5395 uMask |= LVIS_SELECTED;
5397 /* if we're asked for the focused item, that's only one,
5398 * so it's worth optimizing */
5399 if (uFlags & LVNI_FOCUSED)
5401 if (!(LISTVIEW_GetItemState(infoPtr, infoPtr->nFocusedItem, uMask) & uMask) == uMask) return -1;
5402 return (infoPtr->nFocusedItem == nItem) ? -1 : infoPtr->nFocusedItem;
5405 if (uFlags & LVNI_ABOVE)
5407 if ((uView == LVS_LIST) || (uView == LVS_REPORT))
5409 while (nItem >= 0)
5411 nItem--;
5412 if ((ListView_GetItemState(infoPtr->hwndSelf, nItem, uMask) & uMask) == uMask)
5413 return nItem;
5416 else
5418 lvFindInfo.flags = LVFI_NEARESTXY;
5419 lvFindInfo.vkDirection = VK_UP;
5420 ListView_GetItemPosition(infoPtr->hwndSelf, nItem, &lvFindInfo.pt);
5421 while ((nItem = ListView_FindItemW(infoPtr->hwndSelf, nItem, &lvFindInfo)) != -1)
5423 if ((ListView_GetItemState(infoPtr->hwndSelf, nItem, uMask) & uMask) == uMask)
5424 return nItem;
5428 else if (uFlags & LVNI_BELOW)
5430 if ((uView == LVS_LIST) || (uView == LVS_REPORT))
5432 while (nItem < infoPtr->nItemCount)
5434 nItem++;
5435 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
5436 return nItem;
5439 else
5441 lvFindInfo.flags = LVFI_NEARESTXY;
5442 lvFindInfo.vkDirection = VK_DOWN;
5443 ListView_GetItemPosition(infoPtr->hwndSelf, nItem, &lvFindInfo.pt);
5444 while ((nItem = ListView_FindItemW(infoPtr->hwndSelf, nItem, &lvFindInfo)) != -1)
5446 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
5447 return nItem;
5451 else if (uFlags & LVNI_TOLEFT)
5453 if (uView == LVS_LIST)
5455 nCountPerColumn = LISTVIEW_GetCountPerColumn(infoPtr);
5456 while (nItem - nCountPerColumn >= 0)
5458 nItem -= nCountPerColumn;
5459 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
5460 return nItem;
5463 else if ((uView == LVS_SMALLICON) || (uView == LVS_ICON))
5465 lvFindInfo.flags = LVFI_NEARESTXY;
5466 lvFindInfo.vkDirection = VK_LEFT;
5467 ListView_GetItemPosition(infoPtr->hwndSelf, nItem, &lvFindInfo.pt);
5468 while ((nItem = ListView_FindItemW(infoPtr->hwndSelf, nItem, &lvFindInfo)) != -1)
5470 if ((ListView_GetItemState(infoPtr->hwndSelf, nItem, uMask) & uMask) == uMask)
5471 return nItem;
5475 else if (uFlags & LVNI_TORIGHT)
5477 if (uView == LVS_LIST)
5479 nCountPerColumn = LISTVIEW_GetCountPerColumn(infoPtr);
5480 while (nItem + nCountPerColumn < infoPtr->nItemCount)
5482 nItem += nCountPerColumn;
5483 if ((ListView_GetItemState(infoPtr->hwndSelf, nItem, uMask) & uMask) == uMask)
5484 return nItem;
5487 else if ((uView == LVS_SMALLICON) || (uView == LVS_ICON))
5489 lvFindInfo.flags = LVFI_NEARESTXY;
5490 lvFindInfo.vkDirection = VK_RIGHT;
5491 ListView_GetItemPosition(infoPtr->hwndSelf, nItem, &lvFindInfo.pt);
5492 while ((nItem = ListView_FindItemW(infoPtr->hwndSelf, nItem, &lvFindInfo)) != -1)
5494 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
5495 return nItem;
5499 else
5501 nItem++;
5503 /* search by index */
5504 for (i = nItem; i < infoPtr->nItemCount; i++)
5506 if ((LISTVIEW_GetItemState(infoPtr, i, uMask) & uMask) == uMask)
5507 return i;
5511 return -1;
5514 /* LISTVIEW_GetNumberOfWorkAreas */
5516 /***
5517 * DESCRIPTION:
5518 * Retrieves the origin coordinates when in icon or small icon display mode.
5520 * PARAMETER(S):
5521 * [I] infoPtr : valid pointer to the listview structure
5522 * [O] lpptOrigin : coordinate information
5524 * RETURN:
5525 * None.
5527 static void LISTVIEW_GetOrigin(LISTVIEW_INFO *infoPtr, LPPOINT lpptOrigin)
5529 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
5530 INT nHorzPos = 0, nVertPos = 0;
5531 SCROLLINFO scrollInfo;
5533 scrollInfo.cbSize = sizeof(SCROLLINFO);
5534 scrollInfo.fMask = SIF_POS;
5536 if ((infoPtr->dwStyle & WS_HSCROLL) && GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo))
5537 nHorzPos = scrollInfo.nPos;
5538 if ((infoPtr->dwStyle & WS_VSCROLL) && GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo))
5539 nVertPos = scrollInfo.nPos;
5541 TRACE("nHorzPos=%d, nVertPos=%d\n", nHorzPos, nVertPos);
5543 lpptOrigin->x = infoPtr->rcList.left;
5544 lpptOrigin->y = infoPtr->rcList.top;
5545 if (uView == LVS_LIST)
5546 nHorzPos *= infoPtr->nItemWidth;
5547 else if (uView == LVS_REPORT)
5548 nVertPos *= infoPtr->nItemHeight;
5550 lpptOrigin->x -= nHorzPos;
5551 lpptOrigin->y -= nVertPos;
5553 TRACE(" origin=%s\n", debugpoint(lpptOrigin));
5556 /***
5557 * DESCRIPTION:
5558 * Retrieves the width of a string.
5560 * PARAMETER(S):
5561 * [I] hwnd : window handle
5562 * [I] lpszText : text string to process
5563 * [I] isW : TRUE if lpszText is Unicode, FALSE otherwise
5565 * RETURN:
5566 * SUCCESS : string width (in pixels)
5567 * FAILURE : zero
5569 static INT LISTVIEW_GetStringWidthT(LISTVIEW_INFO *infoPtr, LPCWSTR lpszText, BOOL isW)
5571 SIZE stringSize;
5573 stringSize.cx = 0;
5574 if (is_textT(lpszText, isW))
5576 HFONT hFont = infoPtr->hFont ? infoPtr->hFont : infoPtr->hDefaultFont;
5577 HDC hdc = GetDC(infoPtr->hwndSelf);
5578 HFONT hOldFont = SelectObject(hdc, hFont);
5580 if (isW)
5581 GetTextExtentPointW(hdc, lpszText, lstrlenW(lpszText), &stringSize);
5582 else
5583 GetTextExtentPointA(hdc, (LPCSTR)lpszText, lstrlenA((LPCSTR)lpszText), &stringSize);
5584 SelectObject(hdc, hOldFont);
5585 ReleaseDC(infoPtr->hwndSelf, hdc);
5587 return stringSize.cx;
5590 /***
5591 * DESCRIPTION:
5592 * Determines which listview item is located at the specified position.
5594 * PARAMETER(S):
5595 * [I] infoPtr : valid pointer to the listview structure
5596 * [IO] lpht : hit test information
5597 * [I] subitem : fill out iSubItem.
5598 * [I] select : return the index only if the hit selects the item
5600 * NOTE:
5601 * (mm 20001022): We must not allow iSubItem to be touched, for
5602 * an app might pass only a structure with space up to iItem!
5603 * (MS Office 97 does that for instance in the file open dialog)
5605 * RETURN:
5606 * SUCCESS : item index
5607 * FAILURE : -1
5609 static INT LISTVIEW_HitTest(LISTVIEW_INFO *infoPtr, LPLVHITTESTINFO lpht, BOOL subitem, BOOL select)
5611 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
5612 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
5613 RECT rcBox, rcBounds, rcState, rcIcon, rcLabel, rcSearch;
5614 POINT Origin, Position, opt;
5615 LVITEMW lvItem;
5616 ITERATOR i;
5618 TRACE("(pt=%s, subitem=%d, select=%d)\n", debugpoint(&lpht->pt), subitem, select);
5620 lpht->flags = 0;
5621 lpht->iItem = -1;
5622 if (subitem) lpht->iSubItem = 0;
5624 if (infoPtr->rcList.left > lpht->pt.x)
5625 lpht->flags |= LVHT_TOLEFT;
5626 else if (infoPtr->rcList.right < lpht->pt.x)
5627 lpht->flags |= LVHT_TORIGHT;
5629 if (infoPtr->rcList.top > lpht->pt.y)
5630 lpht->flags |= LVHT_ABOVE;
5631 else if (infoPtr->rcList.bottom < lpht->pt.y)
5632 lpht->flags |= LVHT_BELOW;
5634 TRACE("lpht->flags=0x%x\n", lpht->flags);
5635 if (lpht->flags) return -1;
5637 lpht->flags |= LVHT_NOWHERE;
5639 LISTVIEW_GetOrigin(infoPtr, &Origin);
5641 /* first deal with the large items */
5642 rcSearch.left = lpht->pt.x;
5643 rcSearch.top = lpht->pt.y;
5644 rcSearch.right = rcSearch.left + 1;
5645 rcSearch.bottom = rcSearch.top + 1;
5647 iterator_frameditems(&i, infoPtr, &rcSearch);
5648 iterator_next(&i); /* go to first item in the sequence */
5649 lpht->iItem = i.nItem;
5650 iterator_destroy(&i);
5652 TRACE("lpht->iItem=%d\n", lpht->iItem);
5653 if (lpht->iItem == -1) return -1;
5655 lvItem.mask = LVIF_STATE | LVIF_TEXT;
5656 if (uView == LVS_REPORT) lvItem.mask |= LVIF_INDENT;
5657 lvItem.stateMask = LVIS_STATEIMAGEMASK;
5658 if (uView == LVS_ICON) lvItem.stateMask |= LVIS_FOCUSED;
5659 lvItem.iItem = lpht->iItem;
5660 lvItem.iSubItem = 0;
5661 lvItem.pszText = szDispText;
5662 lvItem.cchTextMax = DISP_TEXT_SIZE;
5663 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) return -1;
5664 if (!infoPtr->bFocus) lvItem.state &= ~LVIS_FOCUSED;
5666 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, &rcBox, &rcState, &rcIcon, &rcLabel);
5667 LISTVIEW_GetItemOrigin(infoPtr, lpht->iItem, &Position);
5668 opt.x = lpht->pt.x - Position.x - Origin.x;
5669 opt.y = lpht->pt.y - Position.y - Origin.y;
5671 if (uView == LVS_REPORT && (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT))
5672 rcBounds = rcBox;
5673 else
5674 UnionRect(&rcBounds, &rcIcon, &rcLabel);
5675 TRACE("rcBounds=%s\n", debugrect(&rcBounds));
5676 if (!PtInRect(&rcBounds, opt)) return -1;
5678 if (PtInRect(&rcIcon, opt))
5679 lpht->flags |= LVHT_ONITEMICON;
5680 else if (PtInRect(&rcLabel, opt))
5681 lpht->flags |= LVHT_ONITEMLABEL;
5682 else if (infoPtr->himlState && ((lvItem.state & LVIS_STATEIMAGEMASK) >> 12) && PtInRect(&rcState, opt))
5683 lpht->flags |= LVHT_ONITEMSTATEICON;
5684 if (lpht->flags & LVHT_ONITEM)
5685 lpht->flags &= ~LVHT_NOWHERE;
5687 TRACE("lpht->flags=0x%x\n", lpht->flags);
5688 if (uView == LVS_REPORT && lpht->iItem != -1 && subitem)
5690 INT j;
5692 rcBounds.right = rcBounds.left;
5693 for (j = 0; j < infoPtr->hdpaColumns->nItemCount; j++)
5695 rcBounds.left = rcBounds.right;
5696 rcBounds.right += LISTVIEW_GetColumnWidth(infoPtr, j);
5697 if (PtInRect(&rcBounds, opt))
5699 lpht->iSubItem = j;
5700 break;
5705 if (!select || lpht->iItem == -1) return lpht->iItem;
5707 if (uView == LVS_REPORT && (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT)) return lpht->iItem;
5709 if (uView == LVS_REPORT) UnionRect(&rcBounds, &rcIcon, &rcLabel);
5710 return PtInRect(&rcBounds, opt) ? lpht->iItem : -1;
5714 /* LISTVIEW_InsertCompare: callback routine for comparing pszText members of the LV_ITEMS
5715 in a LISTVIEW on insert. Passed to DPA_Sort in LISTVIEW_InsertItem.
5716 This function should only be used for inserting items into a sorted list (LVM_INSERTITEM)
5717 and not during the processing of a LVM_SORTITEMS message. Applications should provide
5718 their own sort proc. when sending LVM_SORTITEMS.
5720 /* Platform SDK:
5721 (remarks on LVITEM: LVM_INSERTITEM will insert the new item in the proper sort postion...
5723 LVS_SORTXXX must be specified,
5724 LVS_OWNERDRAW is not set,
5725 <item>.pszText is not LPSTR_TEXTCALLBACK.
5727 (LVS_SORT* flags): "For the LVS_SORTASCENDING... styles, item indices
5728 are sorted based on item text..."
5730 static INT WINAPI LISTVIEW_InsertCompare( LPVOID first, LPVOID second, LPARAM lParam)
5732 ITEM_INFO* lv_first = (ITEM_INFO*) DPA_GetPtr( (HDPA)first, 0 );
5733 ITEM_INFO* lv_second = (ITEM_INFO*) DPA_GetPtr( (HDPA)second, 0 );
5734 INT cmpv = textcmpWT(lv_first->hdr.pszText, lv_second->hdr.pszText, TRUE);
5736 /* if we're sorting descending, negate the return value */
5737 return (((LISTVIEW_INFO *)lParam)->dwStyle & LVS_SORTDESCENDING) ? -cmpv : cmpv;
5740 /***
5741 * nESCRIPTION:
5742 * Inserts a new item in the listview control.
5744 * PARAMETER(S):
5745 * [I] infoPtr : valid pointer to the listview structure
5746 * [I] lpLVItem : item information
5747 * [I] isW : TRUE if lpLVItem is Unicode, FALSE if it's ANSI
5749 * RETURN:
5750 * SUCCESS : new item index
5751 * FAILURE : -1
5753 static INT LISTVIEW_InsertItemT(LISTVIEW_INFO *infoPtr, const LVITEMW *lpLVItem, BOOL isW)
5755 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
5756 INT nItem;
5757 HDPA hdpaSubItems;
5758 NMLISTVIEW nmlv;
5759 ITEM_INFO *lpItem;
5760 BOOL is_sorted, has_changed;
5761 LVITEMW item;
5763 TRACE("(lpLVItem=%s, isW=%d)\n", debuglvitem_t(lpLVItem, isW), isW);
5765 if (infoPtr->dwStyle & LVS_OWNERDATA) return infoPtr->nItemCount++;
5767 /* make sure it's an item, and not a subitem; cannot insert a subitem */
5768 if (!lpLVItem || lpLVItem->iItem < 0 || lpLVItem->iSubItem) return -1;
5770 if (!is_assignable_item(lpLVItem, infoPtr->dwStyle)) return -1;
5772 if ( !(lpItem = (ITEM_INFO *)COMCTL32_Alloc(sizeof(ITEM_INFO))) )
5773 return -1;
5775 /* insert item in listview control data structure */
5776 if ( !(hdpaSubItems = DPA_Create(8)) ) goto fail;
5777 if ( !DPA_SetPtr(hdpaSubItems, 0, lpItem) ) assert (FALSE);
5779 is_sorted = (infoPtr->dwStyle & (LVS_SORTASCENDING | LVS_SORTDESCENDING)) &&
5780 !(infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && (LPSTR_TEXTCALLBACKW != lpLVItem->pszText);
5782 nItem = is_sorted ? infoPtr->nItemCount : min(lpLVItem->iItem, infoPtr->nItemCount);
5783 TRACE(" inserting at %d, sorted=%d, count=%d, iItem=%d\n", nItem, is_sorted, infoPtr->nItemCount, lpLVItem->iItem);
5784 nItem = DPA_InsertPtr( infoPtr->hdpaItems, nItem, hdpaSubItems );
5785 if (nItem == -1) goto fail;
5786 infoPtr->nItemCount++;
5788 /* set the item attributes */
5789 item = *lpLVItem;
5790 item.iItem = nItem;
5791 item.state &= ~LVIS_STATEIMAGEMASK;
5792 if (!set_main_item(infoPtr, &item, TRUE, isW, &has_changed)) goto undo;
5794 /* if we're sorted, sort the list, and update the index */
5795 if (is_sorted)
5797 DPA_Sort( infoPtr->hdpaItems, LISTVIEW_InsertCompare, (LPARAM)infoPtr );
5798 nItem = DPA_GetPtrIndex( infoPtr->hdpaItems, hdpaSubItems );
5799 assert(nItem != -1);
5802 /* make room for the position, if we are in the right mode */
5803 if ((uView == LVS_SMALLICON) || (uView == LVS_ICON))
5805 if (DPA_InsertPtr(infoPtr->hdpaPosX, nItem, 0) == -1)
5806 goto undo;
5807 if (DPA_InsertPtr(infoPtr->hdpaPosY, nItem, 0) == -1)
5809 DPA_DeletePtr(infoPtr->hdpaPosX, nItem);
5810 goto undo;
5814 /* Add the subitem list to the items array. Do this last in case we go to
5815 * fail during the above.
5817 LISTVIEW_ShiftIndices(infoPtr, nItem, 1);
5819 /* send LVN_INSERTITEM notification */
5820 ZeroMemory(&nmlv, sizeof(NMLISTVIEW));
5821 nmlv.iItem = nItem;
5822 nmlv.lParam = lpItem->lParam;
5823 notify_listview(infoPtr, LVN_INSERTITEM, &nmlv);
5825 /* align items (set position of each item) */
5826 if ((uView == LVS_SMALLICON || uView == LVS_ICON))
5828 POINT pt;
5830 if (infoPtr->dwStyle & LVS_ALIGNLEFT)
5831 LISTVIEW_NextIconPosLeft(infoPtr, &pt);
5832 else
5833 LISTVIEW_NextIconPosTop(infoPtr, &pt);
5835 LISTVIEW_MoveIconTo(infoPtr, nItem, &pt, TRUE);
5838 /* now is the invalidation fun */
5839 LISTVIEW_ScrollOnInsert(infoPtr, nItem, 1);
5840 return nItem;
5842 undo:
5843 DPA_DeletePtr(infoPtr->hdpaItems, nItem);
5844 infoPtr->nItemCount--;
5845 fail:
5846 DPA_DeletePtr(hdpaSubItems, 0);
5847 DPA_Destroy (hdpaSubItems);
5848 COMCTL32_Free (lpItem);
5849 return -1;
5852 /***
5853 * DESCRIPTION:
5854 * Redraws a range of items.
5856 * PARAMETER(S):
5857 * [I] infoPtr : valid pointer to the listview structure
5858 * [I] nFirst : first item
5859 * [I] nLast : last item
5861 * RETURN:
5862 * SUCCESS : TRUE
5863 * FAILURE : FALSE
5865 static BOOL LISTVIEW_RedrawItems(LISTVIEW_INFO *infoPtr, INT nFirst, INT nLast)
5867 INT i;
5869 if (nLast < nFirst || min(nFirst, nLast) < 0 ||
5870 max(nFirst, nLast) >= infoPtr->nItemCount)
5871 return FALSE;
5873 for (i = nFirst; i <= nLast; i++)
5874 LISTVIEW_InvalidateItem(infoPtr, i);
5876 return TRUE;
5879 /***
5880 * DESCRIPTION:
5881 * Scroll the content of a listview.
5883 * PARAMETER(S):
5884 * [I] infoPtr : valid pointer to the listview structure
5885 * [I] dx : horizontal scroll amount in pixels
5886 * [I] dy : vertical scroll amount in pixels
5888 * RETURN:
5889 * SUCCESS : TRUE
5890 * FAILURE : FALSE
5892 * COMMENTS:
5893 * If the control is in report mode (LVS_REPORT) the control can
5894 * be scrolled only in line increments. "dy" will be rounded to the
5895 * nearest number of pixels that are a whole line. Ex: if line height
5896 * is 16 and an 8 is passed, the list will be scrolled by 16. If a 7
5897 * is passed the the scroll will be 0. (per MSDN 7/2002)
5899 * For: (per experimentaion with native control and CSpy ListView)
5900 * LVS_ICON dy=1 = 1 pixel (vertical only)
5901 * dx ignored
5902 * LVS_SMALLICON dy=1 = 1 pixel (vertical only)
5903 * dx ignored
5904 * LVS_LIST dx=1 = 1 column (horizontal only)
5905 * but will only scroll 1 column per message
5906 * no matter what the value.
5907 * dy must be 0 or FALSE returned.
5908 * LVS_REPORT dx=1 = 1 pixel
5909 * dy= see above
5912 static BOOL LISTVIEW_Scroll(LISTVIEW_INFO *infoPtr, INT dx, INT dy)
5914 switch(infoPtr->dwStyle & LVS_TYPEMASK) {
5915 case LVS_REPORT:
5916 dy += (dy < 0 ? -1 : 1) * infoPtr->nItemHeight/2;
5917 dy /= infoPtr->nItemHeight;
5918 break;
5919 case LVS_LIST:
5920 if (dy != 0) return FALSE;
5921 break;
5922 default: /* icon */
5923 dx = 0;
5924 break;
5927 if (dx != 0) LISTVIEW_HScroll(infoPtr, SB_INTERNAL, dx, 0);
5928 if (dy != 0) LISTVIEW_VScroll(infoPtr, SB_INTERNAL, dy, 0);
5930 return TRUE;
5933 /***
5934 * DESCRIPTION:
5935 * Sets the background color.
5937 * PARAMETER(S):
5938 * [I] infoPtr : valid pointer to the listview structure
5939 * [I] clrBk : background color
5941 * RETURN:
5942 * SUCCESS : TRUE
5943 * FAILURE : FALSE
5945 static BOOL LISTVIEW_SetBkColor(LISTVIEW_INFO *infoPtr, COLORREF clrBk)
5947 TRACE("(clrBk=%lx)\n", clrBk);
5949 if(infoPtr->clrBk != clrBk) {
5950 if (infoPtr->clrBk != CLR_NONE) DeleteObject(infoPtr->hBkBrush);
5951 infoPtr->clrBk = clrBk;
5952 if (clrBk == CLR_NONE)
5953 infoPtr->hBkBrush = (HBRUSH)GetClassLongW(infoPtr->hwndSelf, GCL_HBRBACKGROUND);
5954 else
5955 infoPtr->hBkBrush = CreateSolidBrush(clrBk);
5956 LISTVIEW_InvalidateList(infoPtr);
5959 return TRUE;
5962 /* LISTVIEW_SetBkImage */
5964 /*** Helper for {Insert,Set}ColumnT *only* */
5965 static void column_fill_hditem(LISTVIEW_INFO *infoPtr, HDITEMW *lphdi, INT nColumn, const LVCOLUMNW *lpColumn, BOOL isW)
5967 if (lpColumn->mask & LVCF_FMT)
5969 /* format member is valid */
5970 lphdi->mask |= HDI_FORMAT;
5972 /* set text alignment (leftmost column must be left-aligned) */
5973 if (nColumn == 0 || lpColumn->fmt & LVCFMT_LEFT)
5974 lphdi->fmt |= HDF_LEFT;
5975 else if (lpColumn->fmt & LVCFMT_RIGHT)
5976 lphdi->fmt |= HDF_RIGHT;
5977 else if (lpColumn->fmt & LVCFMT_CENTER)
5978 lphdi->fmt |= HDF_CENTER;
5980 if (lpColumn->fmt & LVCFMT_BITMAP_ON_RIGHT)
5981 lphdi->fmt |= HDF_BITMAP_ON_RIGHT;
5983 if (lpColumn->fmt & LVCFMT_COL_HAS_IMAGES)
5985 lphdi->fmt |= HDF_IMAGE;
5986 lphdi->iImage = I_IMAGECALLBACK;
5990 if (lpColumn->mask & LVCF_WIDTH)
5992 lphdi->mask |= HDI_WIDTH;
5993 if(lpColumn->cx == LVSCW_AUTOSIZE_USEHEADER)
5995 /* make it fill the remainder of the controls width */
5996 RECT rcHeader;
5997 INT item_index;
5999 for(item_index = 0; item_index < (nColumn - 1); item_index++)
6001 LISTVIEW_GetHeaderRect(infoPtr, item_index, &rcHeader);
6002 lphdi->cxy += rcHeader.right - rcHeader.left;
6005 /* retrieve the layout of the header */
6006 GetClientRect(infoPtr->hwndSelf, &rcHeader);
6007 TRACE("start cxy=%d rcHeader=%s\n", lphdi->cxy, debugrect(&rcHeader));
6009 lphdi->cxy = (rcHeader.right - rcHeader.left) - lphdi->cxy;
6011 else
6012 lphdi->cxy = lpColumn->cx;
6015 if (lpColumn->mask & LVCF_TEXT)
6017 lphdi->mask |= HDI_TEXT | HDI_FORMAT;
6018 lphdi->fmt |= HDF_STRING;
6019 lphdi->pszText = lpColumn->pszText;
6020 lphdi->cchTextMax = textlenT(lpColumn->pszText, isW);
6023 if (lpColumn->mask & LVCF_IMAGE)
6025 lphdi->mask |= HDI_IMAGE;
6026 lphdi->iImage = lpColumn->iImage;
6029 if (lpColumn->mask & LVCF_ORDER)
6031 lphdi->mask |= HDI_ORDER;
6032 lphdi->iOrder = lpColumn->iOrder;
6037 /***
6038 * DESCRIPTION:
6039 * Inserts a new column.
6041 * PARAMETER(S):
6042 * [I] infoPtr : valid pointer to the listview structure
6043 * [I] nColumn : column index
6044 * [I] lpColumn : column information
6045 * [I] isW : TRUE if lpColumn is Unicode, FALSE otherwise
6047 * RETURN:
6048 * SUCCESS : new column index
6049 * FAILURE : -1
6051 static INT LISTVIEW_InsertColumnT(LISTVIEW_INFO *infoPtr, INT nColumn,
6052 const LVCOLUMNW *lpColumn, BOOL isW)
6054 COLUMN_INFO *lpColumnInfo;
6055 INT nNewColumn;
6056 HDITEMW hdi;
6058 TRACE("(nColumn=%d, lpColumn=%s, isW=%d)\n", nColumn, debuglvcolumn_t(lpColumn, isW), isW);
6060 if (!lpColumn || nColumn < 0) return -1;
6061 nColumn = min(nColumn, infoPtr->hdpaColumns->nItemCount);
6063 ZeroMemory(&hdi, sizeof(HDITEMW));
6064 column_fill_hditem(infoPtr, &hdi, nColumn, lpColumn, isW);
6066 /* insert item in header control */
6067 nNewColumn = SendMessageW(infoPtr->hwndHeader,
6068 isW ? HDM_INSERTITEMW : HDM_INSERTITEMA,
6069 (WPARAM)nColumn, (LPARAM)&hdi);
6070 if (nNewColumn == -1) return -1;
6071 if (nNewColumn != nColumn) ERR("nColumn=%d, nNewColumn=%d\n", nColumn, nNewColumn);
6073 /* create our own column info */
6074 if (!(lpColumnInfo = COMCTL32_Alloc(sizeof(COLUMN_INFO)))) goto fail;
6075 if (DPA_InsertPtr(infoPtr->hdpaColumns, nNewColumn, lpColumnInfo) == -1) goto fail;
6077 if (lpColumn->mask & LVCF_FMT) lpColumnInfo->fmt = lpColumn->fmt;
6078 if (!Header_GetItemRect(infoPtr->hwndHeader, nNewColumn, &lpColumnInfo->rcHeader)) goto fail;
6080 /* now we have to actually adjust the data */
6081 if (!(infoPtr->dwStyle & LVS_OWNERDATA) && infoPtr->nItemCount > 0)
6083 SUBITEM_INFO *lpSubItem, *lpMainItem, **lpNewItems = 0;
6084 HDPA hdpaSubItems;
6085 INT nItem, i;
6087 /* preallocate memory, so we can fail gracefully */
6088 if (nNewColumn == 0)
6090 lpNewItems = COMCTL32_Alloc(sizeof(SUBITEM_INFO *) * infoPtr->nItemCount);
6091 if (!lpNewItems) goto fail;
6092 for (i = 0; i < infoPtr->nItemCount; i++)
6093 if (!(lpNewItems[i] = COMCTL32_Alloc(sizeof(SUBITEM_INFO)))) break;
6094 if (i != infoPtr->nItemCount)
6096 for(; i >=0; i--) COMCTL32_Free(lpNewItems[i]);
6097 COMCTL32_Free(lpNewItems);
6098 goto fail;
6102 for (nItem = 0; nItem < infoPtr->nItemCount; nItem++)
6104 hdpaSubItems = (HDPA)DPA_GetPtr(infoPtr->hdpaItems, nItem);
6105 for (i = 1; i < hdpaSubItems->nItemCount; i++)
6107 lpSubItem = (SUBITEM_INFO *)DPA_GetPtr(hdpaSubItems, i);
6108 if (lpSubItem->iSubItem >= nNewColumn)
6109 lpSubItem->iSubItem++;
6112 /* for inserting column 0, we have to special-case the main item */
6113 if (nNewColumn == 0)
6115 lpMainItem = (SUBITEM_INFO *)DPA_GetPtr(hdpaSubItems, 0);
6116 lpSubItem = lpNewItems[nItem];
6117 lpSubItem->hdr = lpMainItem->hdr;
6118 lpSubItem->iSubItem = 1;
6119 ZeroMemory(&lpMainItem->hdr, sizeof(lpMainItem->hdr));
6120 lpMainItem->iSubItem = 0;
6121 DPA_InsertPtr(hdpaSubItems, 1, lpSubItem);
6125 COMCTL32_Free(lpNewItems);
6128 /* make space for the new column */
6129 LISTVIEW_ScrollColumns(infoPtr, nNewColumn + 1, lpColumnInfo->rcHeader.right - lpColumnInfo->rcHeader.left);
6131 return nNewColumn;
6133 fail:
6134 if (nNewColumn != -1) SendMessageW(infoPtr->hwndHeader, HDM_DELETEITEM, nNewColumn, 0);
6135 if (lpColumnInfo)
6137 DPA_DeletePtr(infoPtr->hdpaColumns, nNewColumn);
6138 COMCTL32_Free(lpColumnInfo);
6140 return -1;
6143 /***
6144 * DESCRIPTION:
6145 * Sets the attributes of a header item.
6147 * PARAMETER(S):
6148 * [I] infoPtr : valid pointer to the listview structure
6149 * [I] nColumn : column index
6150 * [I] lpColumn : column attributes
6151 * [I] isW: if TRUE, the lpColumn is a LPLVCOLUMNW, else it is a LPLVCOLUMNA
6153 * RETURN:
6154 * SUCCESS : TRUE
6155 * FAILURE : FALSE
6157 static BOOL LISTVIEW_SetColumnT(LISTVIEW_INFO *infoPtr, INT nColumn,
6158 const LVCOLUMNW *lpColumn, BOOL isW)
6160 HDITEMW hdi, hdiget;
6161 BOOL bResult;
6163 TRACE("(nColumn=%d, lpColumn=%s, isW=%d)\n", nColumn, debuglvcolumn_t(lpColumn, isW), isW);
6165 if (!lpColumn || nColumn < 0 || nColumn >= infoPtr->hdpaColumns->nItemCount) return FALSE;
6167 ZeroMemory(&hdi, sizeof(HDITEMW));
6168 if (lpColumn->mask & LVCF_FMT)
6170 hdi.mask |= HDI_FORMAT;
6171 hdiget.mask = HDI_FORMAT;
6172 if (Header_GetItemW(infoPtr->hwndHeader, nColumn, &hdiget))
6173 hdi.fmt = hdiget.fmt & HDF_STRING;
6175 column_fill_hditem(infoPtr, &hdi, nColumn, lpColumn, isW);
6177 /* set header item attributes */
6178 bResult = SendMessageW(infoPtr->hwndHeader, isW ? HDM_SETITEMW : HDM_SETITEMA, (WPARAM)nColumn, (LPARAM)&hdi);
6179 if (!bResult) return FALSE;
6181 if (lpColumn->mask & LVCF_FMT)
6183 COLUMN_INFO *lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, nColumn);
6184 int oldFmt = lpColumnInfo->fmt;
6186 lpColumnInfo->fmt = lpColumn->fmt;
6187 if ((oldFmt ^ lpColumn->fmt) & (LVCFMT_JUSTIFYMASK | LVCFMT_IMAGE))
6189 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
6190 if (uView == LVS_REPORT) LISTVIEW_InvalidateColumn(infoPtr, nColumn);
6194 return TRUE;
6197 /***
6198 * DESCRIPTION:
6199 * Sets the column order array
6201 * PARAMETERS:
6202 * [I] infoPtr : valid pointer to the listview structure
6203 * [I] iCount : number of elements in column order array
6204 * [I] lpiArray : pointer to column order array
6206 * RETURN:
6207 * SUCCESS : TRUE
6208 * FAILURE : FALSE
6210 static BOOL LISTVIEW_SetColumnOrderArray(LISTVIEW_INFO *infoPtr, INT iCount, const INT *lpiArray)
6212 FIXME("iCount %d lpiArray %p\n", iCount, lpiArray);
6214 if (!lpiArray)
6215 return FALSE;
6217 return TRUE;
6221 /***
6222 * DESCRIPTION:
6223 * Sets the width of a column
6225 * PARAMETERS:
6226 * [I] infoPtr : valid pointer to the listview structure
6227 * [I] nColumn : column index
6228 * [I] cx : column width
6230 * RETURN:
6231 * SUCCESS : TRUE
6232 * FAILURE : FALSE
6234 static BOOL LISTVIEW_SetColumnWidth(LISTVIEW_INFO *infoPtr, INT nColumn, INT cx)
6236 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
6237 WCHAR szDispText[DISP_TEXT_SIZE] = { 0 };
6238 INT max_cx = 0;
6239 HDITEMW hdi;
6241 TRACE("(nColumn=%d, cx=%d\n", nColumn, cx);
6243 /* set column width only if in report or list mode */
6244 if (uView != LVS_REPORT && uView != LVS_LIST) return FALSE;
6246 /* take care of invalid cx values */
6247 if(uView == LVS_REPORT && cx < -2) cx = LVSCW_AUTOSIZE;
6248 else if (uView == LVS_LIST && cx < 1) return FALSE;
6250 /* resize all columns if in LVS_LIST mode */
6251 if(uView == LVS_LIST)
6253 infoPtr->nItemWidth = cx;
6254 LISTVIEW_InvalidateList(infoPtr);
6255 return TRUE;
6258 if (nColumn < 0 || nColumn >= infoPtr->hdpaColumns->nItemCount) return FALSE;
6260 if (cx == LVSCW_AUTOSIZE || (cx == LVSCW_AUTOSIZE_USEHEADER && nColumn < infoPtr->hdpaColumns->nItemCount -1))
6262 INT nLabelWidth;
6263 LVITEMW lvItem;
6265 lvItem.mask = LVIF_TEXT;
6266 lvItem.iItem = 0;
6267 lvItem.iSubItem = nColumn;
6268 lvItem.pszText = szDispText;
6269 lvItem.cchTextMax = DISP_TEXT_SIZE;
6270 for (; lvItem.iItem < infoPtr->nItemCount; lvItem.iItem++)
6272 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) continue;
6273 nLabelWidth = LISTVIEW_GetStringWidthT(infoPtr, lvItem.pszText, TRUE);
6274 if (max_cx < nLabelWidth) max_cx = nLabelWidth;
6276 if (infoPtr->himlSmall && (nColumn == 0 || (LISTVIEW_GetColumnInfo(infoPtr, nColumn)->fmt & LVCFMT_IMAGE)))
6277 max_cx += infoPtr->iconSize.cx + IMAGE_PADDING;
6278 max_cx += REPORT_MARGINX + TRAILING_PADDING;
6281 /* autosize based on listview items width */
6282 if(cx == LVSCW_AUTOSIZE)
6283 cx = max_cx;
6284 else if(cx == LVSCW_AUTOSIZE_USEHEADER)
6286 /* if iCol is the last column make it fill the remainder of the controls width */
6287 if(nColumn == infoPtr->hdpaColumns->nItemCount - 1)
6289 RECT rcHeader;
6290 POINT Origin;
6292 LISTVIEW_GetOrigin(infoPtr, &Origin);
6293 LISTVIEW_GetHeaderRect(infoPtr, nColumn, &rcHeader);
6295 cx = infoPtr->rcList.right - Origin.x - rcHeader.left;
6297 else
6299 /* Despite what the MS docs say, if this is not the last
6300 column, then MS resizes the column to the width of the
6301 largest text string in the column, including headers
6302 and items. This is different from LVSCW_AUTOSIZE in that
6303 LVSCW_AUTOSIZE ignores the header string length. */
6304 cx = 0;
6306 /* retrieve header text */
6307 hdi.mask = HDI_TEXT;
6308 hdi.cchTextMax = DISP_TEXT_SIZE;
6309 hdi.pszText = szDispText;
6310 if (Header_GetItemW(infoPtr->hwndHeader, nColumn, (LPARAM)&hdi))
6312 HDC hdc = GetDC(infoPtr->hwndSelf);
6313 HFONT old_font = SelectObject(hdc, (HFONT)SendMessageW(infoPtr->hwndHeader, WM_GETFONT, 0, 0));
6314 SIZE size;
6316 if (GetTextExtentPoint32W(hdc, hdi.pszText, lstrlenW(hdi.pszText), &size))
6317 cx = size.cx;
6318 /* FIXME: Take into account the header image, if one is present */
6319 SelectObject(hdc, old_font);
6320 ReleaseDC(infoPtr->hwndSelf, hdc);
6322 cx = max (cx, max_cx);
6326 if (cx < 0) return FALSE;
6328 /* call header to update the column change */
6329 hdi.mask = HDI_WIDTH;
6330 hdi.cxy = cx;
6331 TRACE("hdi.cxy=%d\n", hdi.cxy);
6332 return Header_SetItemW(infoPtr->hwndHeader, nColumn, (LPARAM)&hdi);
6335 /***
6336 * DESCRIPTION:
6337 * Sets the extended listview style.
6339 * PARAMETERS:
6340 * [I] infoPtr : valid pointer to the listview structure
6341 * [I] dwMask : mask
6342 * [I] dwStyle : style
6344 * RETURN:
6345 * SUCCESS : previous style
6346 * FAILURE : 0
6348 static DWORD LISTVIEW_SetExtendedListViewStyle(LISTVIEW_INFO *infoPtr, DWORD dwMask, DWORD dwStyle)
6350 DWORD dwOldStyle = infoPtr->dwLvExStyle;
6352 /* set new style */
6353 if (dwMask)
6354 infoPtr->dwLvExStyle = (dwOldStyle & ~dwMask) | (dwStyle & dwMask);
6355 else
6356 infoPtr->dwLvExStyle = dwStyle;
6358 return dwOldStyle;
6361 /***
6362 * DESCRIPTION:
6363 * Sets the new hot cursor used during hot tracking and hover selection.
6365 * PARAMETER(S):
6366 * [I] infoPtr : valid pointer to the listview structure
6367 * [I} hCurosr : the new hot cursor handle
6369 * RETURN:
6370 * Returns the previous hot cursor
6372 static HCURSOR LISTVIEW_SetHotCursor(LISTVIEW_INFO *infoPtr, HCURSOR hCursor)
6374 HCURSOR oldCursor = infoPtr->hHotCursor;
6376 infoPtr->hHotCursor = hCursor;
6378 return oldCursor;
6382 /***
6383 * DESCRIPTION:
6384 * Sets the hot item index.
6386 * PARAMETERS:
6387 * [I] infoPtr : valid pointer to the listview structure
6388 * [I] iIndex : index
6390 * RETURN:
6391 * SUCCESS : previous hot item index
6392 * FAILURE : -1 (no hot item)
6394 static INT LISTVIEW_SetHotItem(LISTVIEW_INFO *infoPtr, INT iIndex)
6396 INT iOldIndex = infoPtr->nHotItem;
6398 infoPtr->nHotItem = iIndex;
6400 return iOldIndex;
6404 /***
6405 * DESCRIPTION:
6406 * Sets the amount of time the cursor must hover over an item before it is selected.
6408 * PARAMETER(S):
6409 * [I] infoPtr : valid pointer to the listview structure
6410 * [I] dwHoverTime : hover time, if -1 the hover time is set to the default
6412 * RETURN:
6413 * Returns the previous hover time
6415 static DWORD LISTVIEW_SetHoverTime(LISTVIEW_INFO *infoPtr, DWORD dwHoverTime)
6417 DWORD oldHoverTime = infoPtr->dwHoverTime;
6419 infoPtr->dwHoverTime = dwHoverTime;
6421 return oldHoverTime;
6424 /***
6425 * DESCRIPTION:
6426 * Sets spacing for icons of LVS_ICON style.
6428 * PARAMETER(S):
6429 * [I] infoPtr : valid pointer to the listview structure
6430 * [I] spacing : MAKELONG(cx, cy)
6432 * RETURN:
6433 * MAKELONG(oldcx, oldcy)
6435 static DWORD LISTVIEW_SetIconSpacing(LISTVIEW_INFO *infoPtr, DWORD spacing)
6437 INT cy = HIWORD(spacing), cx = LOWORD(spacing);
6438 DWORD oldspacing = MAKELONG(infoPtr->iconSpacing.cx, infoPtr->iconSpacing.cy);
6439 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
6441 TRACE("requested=(%d,%d)\n", cx, cy);
6443 /* this is supported only for LVS_ICON style */
6444 if (uView != LVS_ICON) return oldspacing;
6446 /* set to defaults, if instructed to */
6447 if (cx == -1) cx = GetSystemMetrics(SM_CXICONSPACING);
6448 if (cy == -1) cy = GetSystemMetrics(SM_CYICONSPACING);
6450 /* if 0 then compute width
6451 * FIXME: Should scan each item and determine max width of
6452 * icon or label, then make that the width */
6453 if (cx == 0)
6454 cx = infoPtr->iconSpacing.cx;
6456 /* if 0 then compute height */
6457 if (cy == 0)
6458 cy = infoPtr->iconSize.cy + 2 * infoPtr->ntmHeight +
6459 ICON_BOTTOM_PADDING + ICON_TOP_PADDING + LABEL_VERT_PADDING;
6462 infoPtr->iconSpacing.cx = cx;
6463 infoPtr->iconSpacing.cy = cy;
6465 TRACE("old=(%d,%d), new=(%d,%d), iconSize=(%ld,%ld), ntmH=%d\n",
6466 LOWORD(oldspacing), HIWORD(oldspacing), cx, cy,
6467 infoPtr->iconSize.cx, infoPtr->iconSize.cy,
6468 infoPtr->ntmHeight);
6470 /* these depend on the iconSpacing */
6471 LISTVIEW_UpdateItemSize(infoPtr);
6473 return oldspacing;
6476 inline void set_icon_size(SIZE *size, HIMAGELIST himl, BOOL small)
6478 INT cx, cy;
6480 if (himl && ImageList_GetIconSize(himl, &cx, &cy))
6482 size->cx = cx;
6483 size->cy = cy;
6485 else
6487 size->cx = GetSystemMetrics(small ? SM_CXSMICON : SM_CXICON);
6488 size->cy = GetSystemMetrics(small ? SM_CYSMICON : SM_CYICON);
6492 /***
6493 * DESCRIPTION:
6494 * Sets image lists.
6496 * PARAMETER(S):
6497 * [I] infoPtr : valid pointer to the listview structure
6498 * [I] nType : image list type
6499 * [I] himl : image list handle
6501 * RETURN:
6502 * SUCCESS : old image list
6503 * FAILURE : NULL
6505 static HIMAGELIST LISTVIEW_SetImageList(LISTVIEW_INFO *infoPtr, INT nType, HIMAGELIST himl)
6507 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
6508 INT oldHeight = infoPtr->nItemHeight;
6509 HIMAGELIST himlOld = 0;
6511 TRACE("(nType=%d, himl=%p\n", nType, himl);
6513 switch (nType)
6515 case LVSIL_NORMAL:
6516 himlOld = infoPtr->himlNormal;
6517 infoPtr->himlNormal = himl;
6518 if (uView == LVS_ICON) set_icon_size(&infoPtr->iconSize, himl, FALSE);
6519 LISTVIEW_SetIconSpacing(infoPtr, 0);
6520 break;
6522 case LVSIL_SMALL:
6523 himlOld = infoPtr->himlSmall;
6524 infoPtr->himlSmall = himl;
6525 if (uView != LVS_ICON) set_icon_size(&infoPtr->iconSize, himl, TRUE);
6526 break;
6528 case LVSIL_STATE:
6529 himlOld = infoPtr->himlState;
6530 infoPtr->himlState = himl;
6531 set_icon_size(&infoPtr->iconStateSize, himl, TRUE);
6532 ImageList_SetBkColor(infoPtr->himlState, CLR_NONE);
6533 break;
6535 default:
6536 ERR("Unknown icon type=%d\n", nType);
6537 return NULL;
6540 infoPtr->nItemHeight = LISTVIEW_CalculateItemHeight(infoPtr);
6541 if (infoPtr->nItemHeight != oldHeight)
6542 LISTVIEW_UpdateScroll(infoPtr);
6544 return himlOld;
6547 /***
6548 * DESCRIPTION:
6549 * Preallocates memory (does *not* set the actual count of items !)
6551 * PARAMETER(S):
6552 * [I] infoPtr : valid pointer to the listview structure
6553 * [I] nItems : item count (projected number of items to allocate)
6554 * [I] dwFlags : update flags
6556 * RETURN:
6557 * SUCCESS : TRUE
6558 * FAILURE : FALSE
6560 static BOOL LISTVIEW_SetItemCount(LISTVIEW_INFO *infoPtr, INT nItems, DWORD dwFlags)
6562 TRACE("(nItems=%d, dwFlags=%lx)\n", nItems, dwFlags);
6564 if (infoPtr->dwStyle & LVS_OWNERDATA)
6566 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
6567 INT nOldCount = infoPtr->nItemCount;
6569 if (nItems < nOldCount)
6571 RANGE range = { nItems, nOldCount };
6572 ranges_del(infoPtr->selectionRanges, range);
6573 if (infoPtr->nFocusedItem >= nItems)
6575 infoPtr->nFocusedItem = -1;
6576 SetRectEmpty(&infoPtr->rcFocus);
6580 infoPtr->nItemCount = nItems;
6581 LISTVIEW_UpdateScroll(infoPtr);
6583 /* the flags are valid only in ownerdata report and list modes */
6584 if (uView == LVS_ICON || uView == LVS_SMALLICON) dwFlags = 0;
6586 if (!(dwFlags & LVSICF_NOSCROLL) && infoPtr->nFocusedItem != -1)
6587 LISTVIEW_EnsureVisible(infoPtr, infoPtr->nFocusedItem, FALSE);
6589 if (!(dwFlags & LVSICF_NOINVALIDATEALL))
6590 LISTVIEW_InvalidateList(infoPtr);
6591 else
6593 INT nFrom, nTo;
6594 POINT Origin;
6595 RECT rcErase;
6597 LISTVIEW_GetOrigin(infoPtr, &Origin);
6598 nFrom = min(nOldCount, nItems);
6599 nTo = max(nOldCount, nItems);
6601 if (uView == LVS_REPORT)
6603 rcErase.left = 0;
6604 rcErase.top = nFrom * infoPtr->nItemHeight;
6605 rcErase.right = infoPtr->nItemWidth;
6606 rcErase.bottom = nTo * infoPtr->nItemHeight;
6607 OffsetRect(&rcErase, Origin.x, Origin.y);
6608 if (IntersectRect(&rcErase, &rcErase, &infoPtr->rcList))
6609 LISTVIEW_InvalidateRect(infoPtr, &rcErase);
6611 else /* LVS_LIST */
6613 INT nPerCol = LISTVIEW_GetCountPerColumn(infoPtr);
6615 rcErase.left = (nFrom / nPerCol) * infoPtr->nItemWidth;
6616 rcErase.top = (nFrom % nPerCol) * infoPtr->nItemHeight;
6617 rcErase.right = rcErase.left + infoPtr->nItemWidth;
6618 rcErase.bottom = nPerCol * infoPtr->nItemHeight;
6619 OffsetRect(&rcErase, Origin.x, Origin.y);
6620 if (IntersectRect(&rcErase, &rcErase, &infoPtr->rcList))
6621 LISTVIEW_InvalidateRect(infoPtr, &rcErase);
6623 rcErase.left = (nFrom / nPerCol + 1) * infoPtr->nItemWidth;
6624 rcErase.top = 0;
6625 rcErase.right = (nTo / nPerCol + 1) * infoPtr->nItemWidth;
6626 rcErase.bottom = nPerCol * infoPtr->nItemHeight;
6627 OffsetRect(&rcErase, Origin.x, Origin.y);
6628 if (IntersectRect(&rcErase, &rcErase, &infoPtr->rcList))
6629 LISTVIEW_InvalidateRect(infoPtr, &rcErase);
6633 else
6635 /* According to MSDN for non-LVS_OWNERDATA this is just
6636 * a performance issue. The control allocates its internal
6637 * data structures for the number of items specified. It
6638 * cuts down on the number of memory allocations. Therefore
6639 * we will just issue a WARN here
6641 WARN("for non-ownerdata performance option not implemented.\n");
6644 return TRUE;
6647 /***
6648 * DESCRIPTION:
6649 * Sets the position of an item.
6651 * PARAMETER(S):
6652 * [I] infoPtr : valid pointer to the listview structure
6653 * [I] nItem : item index
6654 * [I] pt : coordinate
6656 * RETURN:
6657 * SUCCESS : TRUE
6658 * FAILURE : FALSE
6660 static BOOL LISTVIEW_SetItemPosition(LISTVIEW_INFO *infoPtr, INT nItem, POINT pt)
6662 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
6663 POINT Origin;
6665 TRACE("(nItem=%d, &pt=%s\n", nItem, debugpoint(&pt));
6667 if (nItem < 0 || nItem >= infoPtr->nItemCount ||
6668 !(uView == LVS_ICON || uView == LVS_SMALLICON)) return FALSE;
6670 LISTVIEW_GetOrigin(infoPtr, &Origin);
6672 /* This point value seems to be an undocumented feature.
6673 * The best guess is that it means either at the origin,
6674 * or at true beginning of the list. I will assume the origin. */
6675 if ((pt.x == -1) && (pt.y == -1))
6676 pt = Origin;
6678 if (uView == LVS_ICON)
6680 pt.x -= (infoPtr->nItemWidth - infoPtr->iconSize.cx) / 2;
6681 pt.y -= ICON_TOP_PADDING;
6683 pt.x -= Origin.x;
6684 pt.y -= Origin.y;
6686 infoPtr->bAutoarrange = FALSE;
6688 return LISTVIEW_MoveIconTo(infoPtr, nItem, &pt, FALSE);
6691 /***
6692 * DESCRIPTION:
6693 * Sets the state of one or many items.
6695 * PARAMETER(S):
6696 * [I] infoPtr : valid pointer to the listview structure
6697 * [I] nItem : item index
6698 * [I] lpLVItem : item or subitem info
6700 * RETURN:
6701 * SUCCESS : TRUE
6702 * FAILURE : FALSE
6704 static BOOL LISTVIEW_SetItemState(LISTVIEW_INFO *infoPtr, INT nItem, const LVITEMW *lpLVItem)
6706 BOOL bResult = TRUE;
6707 LVITEMW lvItem;
6709 lvItem.iItem = nItem;
6710 lvItem.iSubItem = 0;
6711 lvItem.mask = LVIF_STATE;
6712 lvItem.state = lpLVItem->state;
6713 lvItem.stateMask = lpLVItem->stateMask;
6714 TRACE("lvItem=%s\n", debuglvitem_t(&lvItem, TRUE));
6716 if (nItem == -1)
6718 /* apply to all items */
6719 for (lvItem.iItem = 0; lvItem.iItem < infoPtr->nItemCount; lvItem.iItem++)
6720 if (!LISTVIEW_SetItemT(infoPtr, &lvItem, TRUE)) bResult = FALSE;
6722 else
6723 bResult = LISTVIEW_SetItemT(infoPtr, &lvItem, TRUE);
6725 return bResult;
6728 /***
6729 * DESCRIPTION:
6730 * Sets the text of an item or subitem.
6732 * PARAMETER(S):
6733 * [I] hwnd : window handle
6734 * [I] nItem : item index
6735 * [I] lpLVItem : item or subitem info
6736 * [I] isW : TRUE if input is Unicode
6738 * RETURN:
6739 * SUCCESS : TRUE
6740 * FAILURE : FALSE
6742 static BOOL LISTVIEW_SetItemTextT(LISTVIEW_INFO *infoPtr, INT nItem, const LVITEMW *lpLVItem, BOOL isW)
6744 LVITEMW lvItem;
6746 if (nItem < 0 && nItem >= infoPtr->nItemCount) return FALSE;
6748 lvItem.iItem = nItem;
6749 lvItem.iSubItem = lpLVItem->iSubItem;
6750 lvItem.mask = LVIF_TEXT;
6751 lvItem.pszText = lpLVItem->pszText;
6752 lvItem.cchTextMax = lpLVItem->cchTextMax;
6754 TRACE("(nItem=%d, lpLVItem=%s, isW=%d)\n", nItem, debuglvitem_t(&lvItem, isW), isW);
6756 return LISTVIEW_SetItemT(infoPtr, &lvItem, isW);
6759 /***
6760 * DESCRIPTION:
6761 * Set item index that marks the start of a multiple selection.
6763 * PARAMETER(S):
6764 * [I] infoPtr : valid pointer to the listview structure
6765 * [I] nIndex : index
6767 * RETURN:
6768 * Index number or -1 if there is no selection mark.
6770 static INT LISTVIEW_SetSelectionMark(LISTVIEW_INFO *infoPtr, INT nIndex)
6772 INT nOldIndex = infoPtr->nSelectionMark;
6774 TRACE("(nIndex=%d)\n", nIndex);
6776 infoPtr->nSelectionMark = nIndex;
6778 return nOldIndex;
6781 /***
6782 * DESCRIPTION:
6783 * Sets the text background color.
6785 * PARAMETER(S):
6786 * [I] infoPtr : valid pointer to the listview structure
6787 * [I] clrTextBk : text background color
6789 * RETURN:
6790 * SUCCESS : TRUE
6791 * FAILURE : FALSE
6793 static BOOL LISTVIEW_SetTextBkColor(LISTVIEW_INFO *infoPtr, COLORREF clrTextBk)
6795 TRACE("(clrTextBk=%lx)\n", clrTextBk);
6797 if (infoPtr->clrTextBk != clrTextBk)
6799 infoPtr->clrTextBk = clrTextBk;
6800 LISTVIEW_InvalidateList(infoPtr);
6803 return TRUE;
6806 /***
6807 * DESCRIPTION:
6808 * Sets the text foreground color.
6810 * PARAMETER(S):
6811 * [I] infoPtr : valid pointer to the listview structure
6812 * [I] clrText : text color
6814 * RETURN:
6815 * SUCCESS : TRUE
6816 * FAILURE : FALSE
6818 static BOOL LISTVIEW_SetTextColor (LISTVIEW_INFO *infoPtr, COLORREF clrText)
6820 TRACE("(clrText=%lx)\n", clrText);
6822 if (infoPtr->clrText != clrText)
6824 infoPtr->clrText = clrText;
6825 LISTVIEW_InvalidateList(infoPtr);
6828 return TRUE;
6831 /* LISTVIEW_SetToolTips */
6832 /* LISTVIEW_SetUnicodeFormat */
6833 /* LISTVIEW_SetWorkAreas */
6835 /***
6836 * DESCRIPTION:
6837 * Callback internally used by LISTVIEW_SortItems()
6839 * PARAMETER(S):
6840 * [I] first : pointer to first ITEM_INFO to compare
6841 * [I] second : pointer to second ITEM_INFO to compare
6842 * [I] lParam : HWND of control
6844 * RETURN:
6845 * if first comes before second : negative
6846 * if first comes after second : positive
6847 * if first and second are equivalent : zero
6849 static INT WINAPI LISTVIEW_CallBackCompare(LPVOID first, LPVOID second, LPARAM lParam)
6851 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)GetWindowLongW((HWND)lParam, 0);
6852 ITEM_INFO* lv_first = (ITEM_INFO*) DPA_GetPtr( (HDPA)first, 0 );
6853 ITEM_INFO* lv_second = (ITEM_INFO*) DPA_GetPtr( (HDPA)second, 0 );
6855 /* Forward the call to the client defined callback */
6856 return (infoPtr->pfnCompare)( lv_first->lParam , lv_second->lParam, infoPtr->lParamSort );
6859 /***
6860 * DESCRIPTION:
6861 * Sorts the listview items.
6863 * PARAMETER(S):
6864 * [I] infoPtr : valid pointer to the listview structure
6865 * [I] pfnCompare : application-defined value
6866 * [I] lParamSort : pointer to comparision callback
6868 * RETURN:
6869 * SUCCESS : TRUE
6870 * FAILURE : FALSE
6872 static BOOL LISTVIEW_SortItems(LISTVIEW_INFO *infoPtr, PFNLVCOMPARE pfnCompare, LPARAM lParamSort)
6874 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
6875 HDPA hdpaSubItems;
6876 ITEM_INFO *lpItem;
6877 LPVOID selectionMarkItem;
6878 LVITEMW item;
6879 int i;
6881 TRACE("(pfnCompare=%p, lParamSort=%lx)\n", pfnCompare, lParamSort);
6883 if (infoPtr->dwStyle & LVS_OWNERDATA) return FALSE;
6885 if (!infoPtr->hdpaItems) return FALSE;
6887 /* if there are 0 or 1 items, there is no need to sort */
6888 if (infoPtr->nItemCount < 2) return TRUE;
6890 if (infoPtr->nFocusedItem >= 0)
6892 hdpaSubItems = (HDPA)DPA_GetPtr(infoPtr->hdpaItems, infoPtr->nFocusedItem);
6893 lpItem = (ITEM_INFO *)DPA_GetPtr(hdpaSubItems, 0);
6894 if (lpItem) lpItem->state |= LVIS_FOCUSED;
6896 /* FIXME: go thorugh selected items and mark them so in lpItem->state */
6897 /* clear the lpItem->state for non-selected ones */
6898 /* remove the selection ranges */
6900 infoPtr->pfnCompare = pfnCompare;
6901 infoPtr->lParamSort = lParamSort;
6902 DPA_Sort(infoPtr->hdpaItems, LISTVIEW_CallBackCompare, (LPARAM)infoPtr->hwndSelf);
6904 /* Adjust selections and indices so that they are the way they should
6905 * be after the sort (otherwise, the list items move around, but
6906 * whatever is at the item's previous original position will be
6907 * selected instead)
6909 selectionMarkItem=(infoPtr->nSelectionMark>=0)?DPA_GetPtr(infoPtr->hdpaItems, infoPtr->nSelectionMark):NULL;
6910 for (i=0; i < infoPtr->nItemCount; i++)
6912 hdpaSubItems = (HDPA)DPA_GetPtr(infoPtr->hdpaItems, i);
6913 lpItem = (ITEM_INFO *)DPA_GetPtr(hdpaSubItems, 0);
6915 if (lpItem->state & LVIS_SELECTED)
6917 item.state = LVIS_SELECTED;
6918 item.stateMask = LVIS_SELECTED;
6919 LISTVIEW_SetItemState(infoPtr, i, &item);
6921 if (lpItem->state & LVIS_FOCUSED)
6923 infoPtr->nFocusedItem = i;
6924 lpItem->state &= ~LVIS_FOCUSED;
6927 if (selectionMarkItem != NULL)
6928 infoPtr->nSelectionMark = DPA_GetPtrIndex(infoPtr->hdpaItems, selectionMarkItem);
6929 /* I believe nHotItem should be left alone, see LISTVIEW_ShiftIndices */
6931 /* refresh the display */
6932 if (uView != LVS_ICON && uView != LVS_SMALLICON)
6933 LISTVIEW_InvalidateList(infoPtr);
6935 return TRUE;
6938 /***
6939 * DESCRIPTION:
6940 * Updates an items or rearranges the listview control.
6942 * PARAMETER(S):
6943 * [I] infoPtr : valid pointer to the listview structure
6944 * [I] nItem : item index
6946 * RETURN:
6947 * SUCCESS : TRUE
6948 * FAILURE : FALSE
6950 static BOOL LISTVIEW_Update(LISTVIEW_INFO *infoPtr, INT nItem)
6952 TRACE("(nItem=%d)\n", nItem);
6954 if (nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
6956 /* rearrange with default alignment style */
6957 if (is_autoarrange(infoPtr))
6958 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
6959 else
6960 LISTVIEW_InvalidateItem(infoPtr, nItem);
6962 return TRUE;
6966 /***
6967 * DESCRIPTION:
6968 * Creates the listview control.
6970 * PARAMETER(S):
6971 * [I] hwnd : window handle
6972 * [I] lpcs : the create parameters
6974 * RETURN:
6975 * Success: 0
6976 * Failure: -1
6978 static LRESULT LISTVIEW_Create(HWND hwnd, const CREATESTRUCTW *lpcs)
6980 LISTVIEW_INFO *infoPtr;
6981 UINT uView = lpcs->style & LVS_TYPEMASK;
6982 LOGFONTW logFont;
6984 TRACE("(lpcs=%p)\n", lpcs);
6986 /* initialize info pointer */
6987 infoPtr = (LISTVIEW_INFO *)COMCTL32_Alloc(sizeof(LISTVIEW_INFO));
6988 if (!infoPtr) return -1;
6990 SetWindowLongW(hwnd, 0, (LONG)infoPtr);
6992 infoPtr->hwndSelf = hwnd;
6993 infoPtr->dwStyle = lpcs->style;
6994 /* determine the type of structures to use */
6995 infoPtr->notifyFormat = SendMessageW(GetParent(infoPtr->hwndSelf), WM_NOTIFYFORMAT,
6996 (WPARAM)infoPtr->hwndSelf, (LPARAM)NF_QUERY);
6998 /* initialize color information */
6999 infoPtr->clrBk = CLR_NONE;
7000 infoPtr->clrText = comctl32_color.clrWindowText;
7001 infoPtr->clrTextBk = CLR_DEFAULT;
7002 LISTVIEW_SetBkColor(infoPtr, comctl32_color.clrWindow);
7004 /* set default values */
7005 infoPtr->nFocusedItem = -1;
7006 infoPtr->nSelectionMark = -1;
7007 infoPtr->nHotItem = -1;
7008 infoPtr->bRedraw = TRUE;
7009 infoPtr->bFirstPaint = TRUE;
7010 infoPtr->iconSpacing.cx = GetSystemMetrics(SM_CXICONSPACING);
7011 infoPtr->iconSpacing.cy = GetSystemMetrics(SM_CYICONSPACING);
7012 infoPtr->nEditLabelItem = -1;
7013 infoPtr->dwHoverTime = -1; /* default system hover time */
7015 /* get default font (icon title) */
7016 SystemParametersInfoW(SPI_GETICONTITLELOGFONT, 0, &logFont, 0);
7017 infoPtr->hDefaultFont = CreateFontIndirectW(&logFont);
7018 infoPtr->hFont = infoPtr->hDefaultFont;
7019 LISTVIEW_SaveTextMetrics(infoPtr);
7021 /* create header */
7022 infoPtr->hwndHeader = CreateWindowW(WC_HEADERW, (LPCWSTR)NULL,
7023 WS_CHILD | HDS_HORZ | (DWORD)((LVS_NOSORTHEADER & lpcs->style)?0:HDS_BUTTONS),
7024 0, 0, 0, 0, hwnd, (HMENU)0,
7025 lpcs->hInstance, NULL);
7027 /* set header unicode format */
7028 SendMessageW(infoPtr->hwndHeader, HDM_SETUNICODEFORMAT, (WPARAM)TRUE, (LPARAM)NULL);
7030 /* set header font */
7031 SendMessageW(infoPtr->hwndHeader, WM_SETFONT, (WPARAM)infoPtr->hFont, (LPARAM)TRUE);
7033 /* allocate memory for the selection ranges */
7034 if (!(infoPtr->selectionRanges = ranges_create(10))) return -1;
7036 /* allocate memory for the data structure */
7037 /* FIXME: what if we fail? */
7038 infoPtr->hdpaItems = DPA_Create(10);
7039 infoPtr->hdpaPosX = DPA_Create(10);
7040 infoPtr->hdpaPosY = DPA_Create(10);
7041 infoPtr->hdpaColumns = DPA_Create(10);
7043 /* initialize the icon sizes */
7044 set_icon_size(&infoPtr->iconSize, infoPtr->himlNormal, uView != LVS_ICON);
7045 set_icon_size(&infoPtr->iconStateSize, infoPtr->himlState, TRUE);
7047 /* init item size to avoid division by 0 */
7048 LISTVIEW_UpdateItemSize (infoPtr);
7050 if (uView == LVS_REPORT)
7052 if (!(LVS_NOCOLUMNHEADER & lpcs->style))
7054 ShowWindow(infoPtr->hwndHeader, SW_SHOWNORMAL);
7056 else
7058 /* set HDS_HIDDEN flag to hide the header bar */
7059 SetWindowLongW(infoPtr->hwndHeader, GWL_STYLE,
7060 GetWindowLongW(infoPtr->hwndHeader, GWL_STYLE) | HDS_HIDDEN);
7064 return 0;
7067 /***
7068 * DESCRIPTION:
7069 * Erases the background of the listview control.
7071 * PARAMETER(S):
7072 * [I] infoPtr : valid pointer to the listview structure
7073 * [I] hdc : device context handle
7075 * RETURN:
7076 * SUCCESS : TRUE
7077 * FAILURE : FALSE
7079 static inline BOOL LISTVIEW_EraseBkgnd(LISTVIEW_INFO *infoPtr, HDC hdc)
7081 RECT rc;
7083 TRACE("(hdc=%p)\n", hdc);
7085 if (!GetClipBox(hdc, &rc)) return FALSE;
7087 return LISTVIEW_FillBkgnd(infoPtr, hdc, &rc);
7091 /***
7092 * DESCRIPTION:
7093 * Helper function for LISTVIEW_[HV]Scroll *only*.
7094 * Performs vertical/horizontal scrolling by a give amount.
7096 * PARAMETER(S):
7097 * [I] infoPtr : valid pointer to the listview structure
7098 * [I] dx : amount of horizontal scroll
7099 * [I] dy : amount of vertical scroll
7101 static void scroll_list(LISTVIEW_INFO *infoPtr, INT dx, INT dy)
7103 /* now we can scroll the list */
7104 ScrollWindowEx(infoPtr->hwndSelf, dx, dy, &infoPtr->rcList,
7105 &infoPtr->rcList, 0, 0, SW_ERASE | SW_INVALIDATE);
7106 /* if we have focus, adjust rect */
7107 OffsetRect(&infoPtr->rcFocus, dx, dy);
7108 UpdateWindow(infoPtr->hwndSelf);
7111 /***
7112 * DESCRIPTION:
7113 * Performs vertical scrolling.
7115 * PARAMETER(S):
7116 * [I] infoPtr : valid pointer to the listview structure
7117 * [I] nScrollCode : scroll code
7118 * [I] nScrollDiff : units to scroll in SB_INTERNAL mode, 0 otherwise
7119 * [I] hScrollWnd : scrollbar control window handle
7121 * RETURN:
7122 * Zero
7124 * NOTES:
7125 * SB_LINEUP/SB_LINEDOWN:
7126 * for LVS_ICON, LVS_SMALLICON is 37 by experiment
7127 * for LVS_REPORT is 1 line
7128 * for LVS_LIST cannot occur
7131 static LRESULT LISTVIEW_VScroll(LISTVIEW_INFO *infoPtr, INT nScrollCode,
7132 INT nScrollDiff, HWND hScrollWnd)
7134 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
7135 INT nOldScrollPos, nNewScrollPos;
7136 SCROLLINFO scrollInfo;
7137 BOOL is_an_icon;
7139 TRACE("(nScrollCode=%d, nScrollDiff=%d)\n", nScrollCode, nScrollDiff);
7141 if (infoPtr->hwndEdit) SendMessageW(infoPtr->hwndEdit, WM_KILLFOCUS, 0, 0);
7143 scrollInfo.cbSize = sizeof(SCROLLINFO);
7144 scrollInfo.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_TRACKPOS;
7146 is_an_icon = ((uView == LVS_ICON) || (uView == LVS_SMALLICON));
7148 if (!GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo)) return 1;
7150 nOldScrollPos = scrollInfo.nPos;
7151 switch (nScrollCode)
7153 case SB_INTERNAL:
7154 break;
7156 case SB_LINEUP:
7157 nScrollDiff = (is_an_icon) ? -LISTVIEW_SCROLL_ICON_LINE_SIZE : -1;
7158 break;
7160 case SB_LINEDOWN:
7161 nScrollDiff = (is_an_icon) ? LISTVIEW_SCROLL_ICON_LINE_SIZE : 1;
7162 break;
7164 case SB_PAGEUP:
7165 nScrollDiff = -scrollInfo.nPage;
7166 break;
7168 case SB_PAGEDOWN:
7169 nScrollDiff = scrollInfo.nPage;
7170 break;
7172 case SB_THUMBPOSITION:
7173 case SB_THUMBTRACK:
7174 nScrollDiff = scrollInfo.nTrackPos - scrollInfo.nPos;
7175 break;
7177 default:
7178 nScrollDiff = 0;
7181 /* quit right away if pos isn't changing */
7182 if (nScrollDiff == 0) return 0;
7184 /* calculate new position, and handle overflows */
7185 nNewScrollPos = scrollInfo.nPos + nScrollDiff;
7186 if (nScrollDiff > 0) {
7187 if (nNewScrollPos < nOldScrollPos ||
7188 nNewScrollPos > scrollInfo.nMax)
7189 nNewScrollPos = scrollInfo.nMax;
7190 } else {
7191 if (nNewScrollPos > nOldScrollPos ||
7192 nNewScrollPos < scrollInfo.nMin)
7193 nNewScrollPos = scrollInfo.nMin;
7196 /* set the new position, and reread in case it changed */
7197 scrollInfo.fMask = SIF_POS;
7198 scrollInfo.nPos = nNewScrollPos;
7199 nNewScrollPos = SetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo, TRUE);
7201 /* carry on only if it really changed */
7202 if (nNewScrollPos == nOldScrollPos) return 0;
7204 /* now adjust to client coordinates */
7205 nScrollDiff = nOldScrollPos - nNewScrollPos;
7206 if (uView == LVS_REPORT) nScrollDiff *= infoPtr->nItemHeight;
7208 /* and scroll the window */
7209 scroll_list(infoPtr, 0, nScrollDiff);
7211 return 0;
7214 /***
7215 * DESCRIPTION:
7216 * Performs horizontal scrolling.
7218 * PARAMETER(S):
7219 * [I] infoPtr : valid pointer to the listview structure
7220 * [I] nScrollCode : scroll code
7221 * [I] nScrollDiff : units to scroll in SB_INTERNAL mode, 0 otherwise
7222 * [I] hScrollWnd : scrollbar control window handle
7224 * RETURN:
7225 * Zero
7227 * NOTES:
7228 * SB_LINELEFT/SB_LINERIGHT:
7229 * for LVS_ICON, LVS_SMALLICON 1 pixel
7230 * for LVS_REPORT is 1 pixel
7231 * for LVS_LIST is 1 column --> which is a 1 because the
7232 * scroll is based on columns not pixels
7235 static LRESULT LISTVIEW_HScroll(LISTVIEW_INFO *infoPtr, INT nScrollCode,
7236 INT nScrollDiff, HWND hScrollWnd)
7238 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
7239 INT nOldScrollPos, nNewScrollPos;
7240 SCROLLINFO scrollInfo;
7242 TRACE("(nScrollCode=%d, nScrollDiff=%d)\n", nScrollCode, nScrollDiff);
7244 if (infoPtr->hwndEdit) SendMessageW(infoPtr->hwndEdit, WM_KILLFOCUS, 0, 0);
7246 scrollInfo.cbSize = sizeof(SCROLLINFO);
7247 scrollInfo.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_TRACKPOS;
7249 if (!GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo)) return 1;
7251 nOldScrollPos = scrollInfo.nPos;
7253 switch (nScrollCode)
7255 case SB_INTERNAL:
7256 break;
7258 case SB_LINELEFT:
7259 nScrollDiff = -1;
7260 break;
7262 case SB_LINERIGHT:
7263 nScrollDiff = 1;
7264 break;
7266 case SB_PAGELEFT:
7267 nScrollDiff = -scrollInfo.nPage;
7268 break;
7270 case SB_PAGERIGHT:
7271 nScrollDiff = scrollInfo.nPage;
7272 break;
7274 case SB_THUMBPOSITION:
7275 case SB_THUMBTRACK:
7276 nScrollDiff = scrollInfo.nTrackPos - scrollInfo.nPos;
7277 break;
7279 default:
7280 nScrollDiff = 0;
7283 /* quit right away if pos isn't changing */
7284 if (nScrollDiff == 0) return 0;
7286 /* calculate new position, and handle overflows */
7287 nNewScrollPos = scrollInfo.nPos + nScrollDiff;
7288 if (nScrollDiff > 0) {
7289 if (nNewScrollPos < nOldScrollPos ||
7290 nNewScrollPos > scrollInfo.nMax)
7291 nNewScrollPos = scrollInfo.nMax;
7292 } else {
7293 if (nNewScrollPos > nOldScrollPos ||
7294 nNewScrollPos < scrollInfo.nMin)
7295 nNewScrollPos = scrollInfo.nMin;
7298 /* set the new position, and reread in case it changed */
7299 scrollInfo.fMask = SIF_POS;
7300 scrollInfo.nPos = nNewScrollPos;
7301 nNewScrollPos = SetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo, TRUE);
7303 /* carry on only if it really changed */
7304 if (nNewScrollPos == nOldScrollPos) return 0;
7306 if(uView == LVS_REPORT)
7307 LISTVIEW_UpdateHeaderSize(infoPtr, nNewScrollPos);
7309 /* now adjust to client coordinates */
7310 nScrollDiff = nOldScrollPos - nNewScrollPos;
7311 if (uView == LVS_LIST) nScrollDiff *= infoPtr->nItemWidth;
7313 /* and scroll the window */
7314 scroll_list(infoPtr, nScrollDiff, 0);
7316 return 0;
7319 static LRESULT LISTVIEW_MouseWheel(LISTVIEW_INFO *infoPtr, INT wheelDelta)
7321 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
7322 INT gcWheelDelta = 0;
7323 UINT pulScrollLines = 3;
7324 SCROLLINFO scrollInfo;
7326 TRACE("(wheelDelta=%d)\n", wheelDelta);
7328 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES,0, &pulScrollLines, 0);
7329 gcWheelDelta -= wheelDelta;
7331 scrollInfo.cbSize = sizeof(SCROLLINFO);
7332 scrollInfo.fMask = SIF_POS;
7334 switch(uView)
7336 case LVS_ICON:
7337 case LVS_SMALLICON:
7339 * listview should be scrolled by a multiple of 37 dependently on its dimension or its visible item number
7340 * should be fixed in the future.
7342 LISTVIEW_VScroll(infoPtr, SB_INTERNAL, (gcWheelDelta < 0) ?
7343 LISTVIEW_SCROLL_ICON_LINE_SIZE : -LISTVIEW_SCROLL_ICON_LINE_SIZE, 0);
7344 break;
7346 case LVS_REPORT:
7347 if (abs(gcWheelDelta) >= WHEEL_DELTA && pulScrollLines)
7349 int cLineScroll = min(LISTVIEW_GetCountPerColumn(infoPtr), pulScrollLines);
7350 cLineScroll *= (gcWheelDelta / WHEEL_DELTA);
7351 LISTVIEW_VScroll(infoPtr, SB_INTERNAL, cLineScroll, 0);
7353 break;
7355 case LVS_LIST:
7356 LISTVIEW_HScroll(infoPtr, (gcWheelDelta < 0) ? SB_LINELEFT : SB_LINERIGHT, 0, 0);
7357 break;
7359 return 0;
7362 /***
7363 * DESCRIPTION:
7364 * ???
7366 * PARAMETER(S):
7367 * [I] infoPtr : valid pointer to the listview structure
7368 * [I] nVirtualKey : virtual key
7369 * [I] lKeyData : key data
7371 * RETURN:
7372 * Zero
7374 static LRESULT LISTVIEW_KeyDown(LISTVIEW_INFO *infoPtr, INT nVirtualKey, LONG lKeyData)
7376 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
7377 INT nItem = -1;
7378 NMLVKEYDOWN nmKeyDown;
7380 TRACE("(nVirtualKey=%d, lKeyData=%ld)\n", nVirtualKey, lKeyData);
7382 /* send LVN_KEYDOWN notification */
7383 nmKeyDown.wVKey = nVirtualKey;
7384 nmKeyDown.flags = 0;
7385 notify_hdr(infoPtr, LVN_KEYDOWN, &nmKeyDown.hdr);
7387 switch (nVirtualKey)
7389 case VK_RETURN:
7390 if ((infoPtr->nItemCount > 0) && (infoPtr->nFocusedItem != -1))
7392 notify(infoPtr, NM_RETURN);
7393 notify(infoPtr, LVN_ITEMACTIVATE);
7395 break;
7397 case VK_HOME:
7398 if (infoPtr->nItemCount > 0)
7399 nItem = 0;
7400 break;
7402 case VK_END:
7403 if (infoPtr->nItemCount > 0)
7404 nItem = infoPtr->nItemCount - 1;
7405 break;
7407 case VK_LEFT:
7408 nItem = ListView_GetNextItem(infoPtr->hwndSelf, infoPtr->nFocusedItem, LVNI_TOLEFT);
7409 break;
7411 case VK_UP:
7412 nItem = ListView_GetNextItem(infoPtr->hwndSelf, infoPtr->nFocusedItem, LVNI_ABOVE);
7413 break;
7415 case VK_RIGHT:
7416 nItem = ListView_GetNextItem(infoPtr->hwndSelf, infoPtr->nFocusedItem, LVNI_TORIGHT);
7417 break;
7419 case VK_DOWN:
7420 nItem = ListView_GetNextItem(infoPtr->hwndSelf, infoPtr->nFocusedItem, LVNI_BELOW);
7421 break;
7423 case VK_PRIOR:
7424 if (uView == LVS_REPORT)
7425 nItem = infoPtr->nFocusedItem - LISTVIEW_GetCountPerColumn(infoPtr);
7426 else
7427 nItem = infoPtr->nFocusedItem - LISTVIEW_GetCountPerColumn(infoPtr)
7428 * LISTVIEW_GetCountPerRow(infoPtr);
7429 if(nItem < 0) nItem = 0;
7430 break;
7432 case VK_NEXT:
7433 if (uView == LVS_REPORT)
7434 nItem = infoPtr->nFocusedItem + LISTVIEW_GetCountPerColumn(infoPtr);
7435 else
7436 nItem = infoPtr->nFocusedItem + LISTVIEW_GetCountPerColumn(infoPtr)
7437 * LISTVIEW_GetCountPerRow(infoPtr);
7438 if(nItem >= infoPtr->nItemCount) nItem = infoPtr->nItemCount - 1;
7439 break;
7442 if ((nItem != -1) && (nItem != infoPtr->nFocusedItem))
7443 LISTVIEW_KeySelection(infoPtr, nItem);
7445 return 0;
7448 /***
7449 * DESCRIPTION:
7450 * Kills the focus.
7452 * PARAMETER(S):
7453 * [I] infoPtr : valid pointer to the listview structure
7455 * RETURN:
7456 * Zero
7458 static LRESULT LISTVIEW_KillFocus(LISTVIEW_INFO *infoPtr)
7460 TRACE("()\n");
7462 /* if we did not have the focus, there's nothing to do */
7463 if (!infoPtr->bFocus) return 0;
7465 /* send NM_KILLFOCUS notification */
7466 notify(infoPtr, NM_KILLFOCUS);
7468 /* if we have a focus rectagle, get rid of it */
7469 LISTVIEW_ShowFocusRect(infoPtr, FALSE);
7471 /* set window focus flag */
7472 infoPtr->bFocus = FALSE;
7474 /* invalidate the selected items before reseting focus flag */
7475 LISTVIEW_InvalidateSelectedItems(infoPtr);
7477 return 0;
7480 /***
7481 * DESCRIPTION:
7482 * Processes double click messages (left mouse button).
7484 * PARAMETER(S):
7485 * [I] infoPtr : valid pointer to the listview structure
7486 * [I] wKey : key flag
7487 * [I] pts : mouse coordinate
7489 * RETURN:
7490 * Zero
7492 static LRESULT LISTVIEW_LButtonDblClk(LISTVIEW_INFO *infoPtr, WORD wKey, POINTS pts)
7494 LVHITTESTINFO htInfo;
7496 TRACE("(key=%hu, X=%hu, Y=%hu)\n", wKey, pts.x, pts.y);
7498 /* send NM_RELEASEDCAPTURE notification */
7499 notify(infoPtr, NM_RELEASEDCAPTURE);
7501 htInfo.pt.x = pts.x;
7502 htInfo.pt.y = pts.y;
7504 /* send NM_DBLCLK notification */
7505 LISTVIEW_HitTest(infoPtr, &htInfo, TRUE, FALSE);
7506 notify_click(infoPtr, NM_DBLCLK, &htInfo);
7508 /* To send the LVN_ITEMACTIVATE, it must be on an Item */
7509 if(htInfo.iItem != -1) notify(infoPtr, LVN_ITEMACTIVATE);
7511 return 0;
7514 /***
7515 * DESCRIPTION:
7516 * Processes mouse down messages (left mouse button).
7518 * PARAMETER(S):
7519 * [I] infoPtr : valid pointer to the listview structure
7520 * [I] wKey : key flag
7521 * [I] pts : mouse coordinate
7523 * RETURN:
7524 * Zero
7526 static LRESULT LISTVIEW_LButtonDown(LISTVIEW_INFO *infoPtr, WORD wKey, POINTS pts)
7528 LVHITTESTINFO lvHitTestInfo;
7529 static BOOL bGroupSelect = TRUE;
7530 POINT pt = { pts.x, pts.y };
7531 INT nItem;
7533 TRACE("(key=%hu, X=%hu, Y=%hu)\n", wKey, pts.x, pts.y);
7535 /* send NM_RELEASEDCAPTURE notification */
7536 notify(infoPtr, NM_RELEASEDCAPTURE);
7538 if (!infoPtr->bFocus) SetFocus(infoPtr->hwndSelf);
7540 /* set left button down flag */
7541 infoPtr->bLButtonDown = TRUE;
7543 lvHitTestInfo.pt.x = pts.x;
7544 lvHitTestInfo.pt.y = pts.y;
7546 nItem = LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, TRUE);
7547 TRACE("at %s, nItem=%d\n", debugpoint(&pt), nItem);
7548 infoPtr->nEditLabelItem = -1;
7549 if ((nItem >= 0) && (nItem < infoPtr->nItemCount))
7551 if (infoPtr->dwStyle & LVS_SINGLESEL)
7553 if (LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED))
7554 infoPtr->nEditLabelItem = nItem;
7555 else
7556 LISTVIEW_SetSelection(infoPtr, nItem);
7558 else
7560 if ((wKey & MK_CONTROL) && (wKey & MK_SHIFT))
7562 if (bGroupSelect)
7564 LISTVIEW_AddGroupSelection(infoPtr, nItem);
7565 LISTVIEW_SetItemFocus(infoPtr, nItem);
7566 infoPtr->nSelectionMark = nItem;
7568 else
7570 LVITEMW item;
7572 item.state = LVIS_SELECTED | LVIS_FOCUSED;
7573 item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
7575 LISTVIEW_SetItemState(infoPtr,nItem,&item);
7576 infoPtr->nSelectionMark = nItem;
7579 else if (wKey & MK_CONTROL)
7581 LVITEMW item;
7583 bGroupSelect = (LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED) == 0);
7585 item.state = (bGroupSelect ? LVIS_SELECTED : 0) | LVIS_FOCUSED;
7586 item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
7587 LISTVIEW_SetItemState(infoPtr, nItem, &item);
7588 infoPtr->nSelectionMark = nItem;
7590 else if (wKey & MK_SHIFT)
7592 LISTVIEW_SetGroupSelection(infoPtr, nItem);
7594 else
7596 if (LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED))
7597 infoPtr->nEditLabelItem = nItem;
7599 /* set selection (clears other pre-existing selections) */
7600 LISTVIEW_SetSelection(infoPtr, nItem);
7604 else
7606 /* remove all selections */
7607 LISTVIEW_DeselectAll(infoPtr);
7610 return 0;
7613 /***
7614 * DESCRIPTION:
7615 * Processes mouse up messages (left mouse button).
7617 * PARAMETER(S):
7618 * [I] infoPtr : valid pointer to the listview structure
7619 * [I] wKey : key flag
7620 * [I] pts : mouse coordinate
7622 * RETURN:
7623 * Zero
7625 static LRESULT LISTVIEW_LButtonUp(LISTVIEW_INFO *infoPtr, WORD wKey, POINTS pts)
7627 LVHITTESTINFO lvHitTestInfo;
7629 TRACE("(key=%hu, X=%hu, Y=%hu)\n", wKey, pts.x, pts.y);
7631 if (!infoPtr->bLButtonDown) return 0;
7633 lvHitTestInfo.pt.x = pts.x;
7634 lvHitTestInfo.pt.y = pts.y;
7636 /* send NM_CLICK notification */
7637 LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, FALSE);
7638 notify_click(infoPtr, NM_CLICK, &lvHitTestInfo);
7640 /* set left button flag */
7641 infoPtr->bLButtonDown = FALSE;
7643 /* if we clicked on a selected item, edit the label */
7644 if(lvHitTestInfo.iItem == infoPtr->nEditLabelItem && (lvHitTestInfo.flags & LVHT_ONITEMLABEL))
7645 LISTVIEW_EditLabelT(infoPtr, lvHitTestInfo.iItem, TRUE);
7647 return 0;
7650 /***
7651 * DESCRIPTION:
7652 * Destroys the listview control (called after WM_DESTROY).
7654 * PARAMETER(S):
7655 * [I] infoPtr : valid pointer to the listview structure
7657 * RETURN:
7658 * Zero
7660 static LRESULT LISTVIEW_NCDestroy(LISTVIEW_INFO *infoPtr)
7662 TRACE("()\n");
7664 /* delete all items */
7665 LISTVIEW_DeleteAllItems(infoPtr);
7667 /* destroy data structure */
7668 DPA_Destroy(infoPtr->hdpaItems);
7669 if (infoPtr->selectionRanges) ranges_destroy(infoPtr->selectionRanges);
7671 /* destroy image lists */
7672 if (!(infoPtr->dwStyle & LVS_SHAREIMAGELISTS))
7674 if (infoPtr->himlNormal)
7675 ImageList_Destroy(infoPtr->himlNormal);
7676 if (infoPtr->himlSmall)
7677 ImageList_Destroy(infoPtr->himlSmall);
7678 if (infoPtr->himlState)
7679 ImageList_Destroy(infoPtr->himlState);
7682 /* destroy font, bkgnd brush */
7683 infoPtr->hFont = 0;
7684 if (infoPtr->hDefaultFont) DeleteObject(infoPtr->hDefaultFont);
7685 if (infoPtr->clrBk != CLR_NONE) DeleteObject(infoPtr->hBkBrush);
7687 /* free listview info pointer*/
7688 COMCTL32_Free(infoPtr);
7690 SetWindowLongW(infoPtr->hwndSelf, 0, 0);
7691 return 0;
7694 /***
7695 * DESCRIPTION:
7696 * Handles notifications from header.
7698 * PARAMETER(S):
7699 * [I] infoPtr : valid pointer to the listview structure
7700 * [I] nCtrlId : control identifier
7701 * [I] lpnmh : notification information
7703 * RETURN:
7704 * Zero
7706 static LRESULT LISTVIEW_HeaderNotification(LISTVIEW_INFO *infoPtr, const NMHEADERW *lpnmh)
7708 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
7710 TRACE("(lpnmh=%p)\n", lpnmh);
7712 if (!lpnmh || lpnmh->iItem < 0 || lpnmh->iItem >= infoPtr->hdpaColumns->nItemCount) return 0;
7714 switch (lpnmh->hdr.code)
7716 case HDN_TRACKW:
7717 case HDN_TRACKA:
7718 case HDN_ITEMCHANGEDW:
7719 case HDN_ITEMCHANGEDA:
7721 COLUMN_INFO *lpColumnInfo;
7722 INT dx, cxy;
7724 if (!lpnmh->pitem || !(lpnmh->pitem->mask & HDI_WIDTH))
7726 HDITEMW hdi;
7728 hdi.mask = HDI_WIDTH;
7729 if (!Header_GetItemW(infoPtr->hwndHeader, lpnmh->iItem, (LPARAM)&hdi)) return 0;
7730 cxy = hdi.cxy;
7732 else
7733 cxy = lpnmh->pitem->cxy;
7735 /* determine how much we change since the last know position */
7736 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, lpnmh->iItem);
7737 dx = cxy - (lpColumnInfo->rcHeader.right - lpColumnInfo->rcHeader.left);
7738 if (dx != 0)
7740 RECT rcCol = lpColumnInfo->rcHeader;
7742 lpColumnInfo->rcHeader.right += dx;
7743 LISTVIEW_ScrollColumns(infoPtr, lpnmh->iItem + 1, dx);
7744 if (uView == LVS_REPORT && is_redrawing(infoPtr))
7746 /* this trick works for left aligned columns only */
7747 if ((lpColumnInfo->fmt & LVCFMT_JUSTIFYMASK) == LVCFMT_LEFT)
7749 rcCol.right = min (rcCol.right, lpColumnInfo->rcHeader.right);
7750 rcCol.left = max (rcCol.left, rcCol.right - 3 * infoPtr->ntmAveCharWidth);
7752 rcCol.top = infoPtr->rcList.top;
7753 rcCol.bottom = infoPtr->rcList.bottom;
7754 LISTVIEW_InvalidateRect(infoPtr, &rcCol);
7758 break;
7760 case HDN_ITEMCLICKW:
7761 case HDN_ITEMCLICKA:
7763 /* Handle sorting by Header Column */
7764 NMLISTVIEW nmlv;
7766 ZeroMemory(&nmlv, sizeof(NMLISTVIEW));
7767 nmlv.iItem = -1;
7768 nmlv.iSubItem = lpnmh->iItem;
7769 notify_listview(infoPtr, LVN_COLUMNCLICK, &nmlv);
7771 break;
7774 return 0;
7777 /***
7778 * DESCRIPTION:
7779 * Determines the type of structure to use.
7781 * PARAMETER(S):
7782 * [I] infoPtr : valid pointer to the listview structureof the sender
7783 * [I] hwndFrom : listview window handle
7784 * [I] nCommand : command specifying the nature of the WM_NOTIFYFORMAT
7786 * RETURN:
7787 * Zero
7789 static LRESULT LISTVIEW_NotifyFormat(LISTVIEW_INFO *infoPtr, HWND hwndFrom, INT nCommand)
7791 TRACE("(hwndFrom=%p, nCommand=%d)\n", hwndFrom, nCommand);
7793 if (nCommand != NF_REQUERY) return 0;
7795 infoPtr->notifyFormat = SendMessageW(hwndFrom, WM_NOTIFYFORMAT, (WPARAM)infoPtr->hwndSelf, NF_QUERY);
7797 return 0;
7800 /***
7801 * DESCRIPTION:
7802 * Paints/Repaints the listview control.
7804 * PARAMETER(S):
7805 * [I] infoPtr : valid pointer to the listview structure
7806 * [I] hdc : device context handle
7808 * RETURN:
7809 * Zero
7811 static LRESULT LISTVIEW_Paint(LISTVIEW_INFO *infoPtr, HDC hdc)
7813 TRACE("(hdc=%p)\n", hdc);
7815 if (infoPtr->bFirstPaint)
7817 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
7819 infoPtr->bFirstPaint = FALSE;
7820 LISTVIEW_UpdateItemSize(infoPtr);
7821 if (uView == LVS_ICON || uView == LVS_SMALLICON)
7822 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
7823 LISTVIEW_UpdateScroll(infoPtr);
7825 if (hdc)
7826 LISTVIEW_Refresh(infoPtr, hdc);
7827 else
7829 PAINTSTRUCT ps;
7831 hdc = BeginPaint(infoPtr->hwndSelf, &ps);
7832 if (!hdc) return 1;
7833 if (ps.fErase) LISTVIEW_FillBkgnd(infoPtr, hdc, &ps.rcPaint);
7834 LISTVIEW_Refresh(infoPtr, hdc);
7835 EndPaint(infoPtr->hwndSelf, &ps);
7838 return 0;
7841 /***
7842 * DESCRIPTION:
7843 * Processes double click messages (right mouse button).
7845 * PARAMETER(S):
7846 * [I] infoPtr : valid pointer to the listview structure
7847 * [I] wKey : key flag
7848 * [I] pts : mouse coordinate
7850 * RETURN:
7851 * Zero
7853 static LRESULT LISTVIEW_RButtonDblClk(LISTVIEW_INFO *infoPtr, WORD wKey, POINTS pts)
7855 LVHITTESTINFO lvHitTestInfo;
7857 TRACE("(key=%hu,X=%hu,Y=%hu)\n", wKey, pts.x, pts.y);
7859 /* send NM_RELEASEDCAPTURE notification */
7860 notify(infoPtr, NM_RELEASEDCAPTURE);
7862 /* send NM_RDBLCLK notification */
7863 lvHitTestInfo.pt.x = pts.x;
7864 lvHitTestInfo.pt.y = pts.y;
7865 LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, FALSE);
7866 notify_click(infoPtr, NM_RDBLCLK, &lvHitTestInfo);
7868 return 0;
7871 /***
7872 * DESCRIPTION:
7873 * Processes mouse down messages (right mouse button).
7875 * PARAMETER(S):
7876 * [I] infoPtr : valid pointer to the listview structure
7877 * [I] wKey : key flag
7878 * [I] pts : mouse coordinate
7880 * RETURN:
7881 * Zero
7883 static LRESULT LISTVIEW_RButtonDown(LISTVIEW_INFO *infoPtr, WORD wKey, POINTS pts)
7885 LVHITTESTINFO lvHitTestInfo;
7886 INT nItem;
7888 TRACE("(key=%hu,X=%hu,Y=%hu)\n", wKey, pts.x, pts.y);
7890 /* send NM_RELEASEDCAPTURE notification */
7891 notify(infoPtr, NM_RELEASEDCAPTURE);
7893 /* make sure the listview control window has the focus */
7894 if (!infoPtr->bFocus) SetFocus(infoPtr->hwndSelf);
7896 /* set right button down flag */
7897 infoPtr->bRButtonDown = TRUE;
7899 /* determine the index of the selected item */
7900 lvHitTestInfo.pt.x = pts.x;
7901 lvHitTestInfo.pt.y = pts.y;
7902 nItem = LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, TRUE);
7904 if ((nItem >= 0) && (nItem < infoPtr->nItemCount))
7906 LISTVIEW_SetItemFocus(infoPtr, nItem);
7907 if (!((wKey & MK_SHIFT) || (wKey & MK_CONTROL)) &&
7908 !LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED))
7909 LISTVIEW_SetSelection(infoPtr, nItem);
7911 else
7913 LISTVIEW_DeselectAll(infoPtr);
7916 return 0;
7919 /***
7920 * DESCRIPTION:
7921 * Processes mouse up messages (right mouse button).
7923 * PARAMETER(S):
7924 * [I] infoPtr : valid pointer to the listview structure
7925 * [I] wKey : key flag
7926 * [I] pts : mouse coordinate
7928 * RETURN:
7929 * Zero
7931 static LRESULT LISTVIEW_RButtonUp(LISTVIEW_INFO *infoPtr, WORD wKey, POINTS pts)
7933 LVHITTESTINFO lvHitTestInfo;
7934 POINT pt;
7936 TRACE("(key=%hu,X=%hu,Y=%hu)\n", wKey, pts.x, pts.y);
7938 if (!infoPtr->bRButtonDown) return 0;
7940 /* set button flag */
7941 infoPtr->bRButtonDown = FALSE;
7943 /* Send NM_RClICK notification */
7944 lvHitTestInfo.pt.x = pts.x;
7945 lvHitTestInfo.pt.y = pts.y;
7946 LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, FALSE);
7947 notify_click(infoPtr, NM_RCLICK, &lvHitTestInfo);
7949 /* Change to screen coordinate for WM_CONTEXTMENU */
7950 pt = lvHitTestInfo.pt;
7951 ClientToScreen(infoPtr->hwndSelf, &pt);
7953 /* Send a WM_CONTEXTMENU message in response to the RBUTTONUP */
7954 SendMessageW(infoPtr->hwndSelf, WM_CONTEXTMENU,
7955 (WPARAM)infoPtr->hwndSelf, MAKELPARAM(pt.x, pt.y));
7957 return 0;
7961 /***
7962 * DESCRIPTION:
7963 * Sets the cursor.
7965 * PARAMETER(S):
7966 * [I] infoPtr : valid pointer to the listview structure
7967 * [I] hwnd : window handle of window containing the cursor
7968 * [I] nHittest : hit-test code
7969 * [I] wMouseMsg : ideintifier of the mouse message
7971 * RETURN:
7972 * TRUE if cursor is set
7973 * FALSE otherwise
7975 static BOOL LISTVIEW_SetCursor(LISTVIEW_INFO *infoPtr, HWND hwnd, UINT nHittest, UINT wMouseMsg)
7977 LVHITTESTINFO lvHitTestInfo;
7979 if(!(infoPtr->dwLvExStyle & LVS_EX_TRACKSELECT)) return FALSE;
7981 if(!infoPtr->hHotCursor) return FALSE;
7983 GetCursorPos(&lvHitTestInfo.pt);
7984 if (LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, FALSE, FALSE) < 0) return FALSE;
7986 SetCursor(infoPtr->hHotCursor);
7988 return TRUE;
7991 /***
7992 * DESCRIPTION:
7993 * Sets the focus.
7995 * PARAMETER(S):
7996 * [I] infoPtr : valid pointer to the listview structure
7997 * [I] hwndLoseFocus : handle of previously focused window
7999 * RETURN:
8000 * Zero
8002 static LRESULT LISTVIEW_SetFocus(LISTVIEW_INFO *infoPtr, HWND hwndLoseFocus)
8004 TRACE("(hwndLoseFocus=%p)\n", hwndLoseFocus);
8006 /* if we have the focus already, there's nothing to do */
8007 if (infoPtr->bFocus) return 0;
8009 /* send NM_SETFOCUS notification */
8010 notify(infoPtr, NM_SETFOCUS);
8012 /* set window focus flag */
8013 infoPtr->bFocus = TRUE;
8015 /* put the focus rect back on */
8016 LISTVIEW_ShowFocusRect(infoPtr, TRUE);
8018 /* redraw all visible selected items */
8019 LISTVIEW_InvalidateSelectedItems(infoPtr);
8021 return 0;
8024 /***
8025 * DESCRIPTION:
8026 * Sets the font.
8028 * PARAMETER(S):
8029 * [I] infoPtr : valid pointer to the listview structure
8030 * [I] fRedraw : font handle
8031 * [I] fRedraw : redraw flag
8033 * RETURN:
8034 * Zero
8036 static LRESULT LISTVIEW_SetFont(LISTVIEW_INFO *infoPtr, HFONT hFont, WORD fRedraw)
8038 HFONT oldFont = infoPtr->hFont;
8040 TRACE("(hfont=%p,redraw=%hu)\n", hFont, fRedraw);
8042 infoPtr->hFont = hFont ? hFont : infoPtr->hDefaultFont;
8043 if (infoPtr->hFont == oldFont) return 0;
8045 LISTVIEW_SaveTextMetrics(infoPtr);
8047 if ((infoPtr->dwStyle & LVS_TYPEMASK) == LVS_REPORT)
8048 SendMessageW(infoPtr->hwndHeader, WM_SETFONT, (WPARAM)hFont, MAKELPARAM(fRedraw, 0));
8050 if (fRedraw) LISTVIEW_InvalidateList(infoPtr);
8052 return 0;
8055 /***
8056 * DESCRIPTION:
8057 * Message handling for WM_SETREDRAW.
8058 * For the Listview, it invalidates the entire window (the doc specifies otherwise)
8060 * PARAMETER(S):
8061 * [I] infoPtr : valid pointer to the listview structure
8062 * [I] bRedraw: state of redraw flag
8064 * RETURN:
8065 * DefWinProc return value
8067 static LRESULT LISTVIEW_SetRedraw(LISTVIEW_INFO *infoPtr, BOOL bRedraw)
8069 TRACE("infoPtr->bRedraw=%d, bRedraw=%d\n", infoPtr->bRedraw, bRedraw);
8071 /* we can not use straight equality here because _any_ non-zero value is TRUE */
8072 if ((infoPtr->bRedraw && bRedraw) || (!infoPtr->bRedraw && !bRedraw)) return 0;
8074 infoPtr->bRedraw = bRedraw;
8076 if(!bRedraw) return 0;
8078 if (is_autoarrange(infoPtr))
8079 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
8080 LISTVIEW_UpdateScroll(infoPtr);
8082 /* despite what the WM_SETREDRAW docs says, apps expect us
8083 * to invalidate the listview here... stupid! */
8084 LISTVIEW_InvalidateList(infoPtr);
8086 return 0;
8089 /***
8090 * DESCRIPTION:
8091 * Resizes the listview control. This function processes WM_SIZE
8092 * messages. At this time, the width and height are not used.
8094 * PARAMETER(S):
8095 * [I] infoPtr : valid pointer to the listview structure
8096 * [I] Width : new width
8097 * [I] Height : new height
8099 * RETURN:
8100 * Zero
8102 static LRESULT LISTVIEW_Size(LISTVIEW_INFO *infoPtr, int Width, int Height)
8104 RECT rcOld = infoPtr->rcList;
8106 TRACE("(width=%d, height=%d)\n", Width, Height);
8108 LISTVIEW_UpdateSize(infoPtr);
8109 if (EqualRect(&rcOld, &infoPtr->rcList)) return 0;
8111 /* do not bother with display related stuff if we're not redrawing */
8112 if (!is_redrawing(infoPtr)) return 0;
8114 if (is_autoarrange(infoPtr))
8115 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
8117 LISTVIEW_UpdateScroll(infoPtr);
8119 /* refresh all only for lists whose height changed significantly */
8120 if ((infoPtr->dwStyle & LVS_TYPEMASK) == LVS_LIST &&
8121 (rcOld.bottom - rcOld.top) / infoPtr->nItemHeight !=
8122 (infoPtr->rcList.bottom - infoPtr->rcList.top) / infoPtr->nItemHeight)
8123 LISTVIEW_InvalidateList(infoPtr);
8125 return 0;
8128 /***
8129 * DESCRIPTION:
8130 * Sets the size information.
8132 * PARAMETER(S):
8133 * [I] infoPtr : valid pointer to the listview structure
8135 * RETURN:
8136 * None
8138 static void LISTVIEW_UpdateSize(LISTVIEW_INFO *infoPtr)
8140 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
8142 TRACE("uView=%d, rcList(old)=%s\n", uView, debugrect(&infoPtr->rcList));
8144 GetClientRect(infoPtr->hwndSelf, &infoPtr->rcList);
8146 if (uView == LVS_LIST)
8148 /* Apparently the "LIST" style is supposed to have the same
8149 * number of items in a column even if there is no scroll bar.
8150 * Since if a scroll bar already exists then the bottom is already
8151 * reduced, only reduce if the scroll bar does not currently exist.
8152 * The "2" is there to mimic the native control. I think it may be
8153 * related to either padding or edges. (GLA 7/2002)
8155 if (!(infoPtr->dwStyle & WS_HSCROLL))
8156 infoPtr->rcList.bottom -= GetSystemMetrics(SM_CYHSCROLL);
8157 infoPtr->rcList.bottom = max (infoPtr->rcList.bottom - 2, 0);
8159 else if (uView == LVS_REPORT && !(infoPtr->dwStyle & LVS_NOCOLUMNHEADER))
8161 HDLAYOUT hl;
8162 WINDOWPOS wp;
8164 hl.prc = &infoPtr->rcList;
8165 hl.pwpos = &wp;
8166 Header_Layout(infoPtr->hwndHeader, &hl);
8168 SetWindowPos(wp.hwnd, wp.hwndInsertAfter, wp.x, wp.y, wp.cx, wp.cy, wp.flags);
8170 infoPtr->rcList.top = max(wp.cy, 0);
8173 TRACE(" rcList=%s\n", debugrect(&infoPtr->rcList));
8176 /***
8177 * DESCRIPTION:
8178 * Processes WM_STYLECHANGED messages.
8180 * PARAMETER(S):
8181 * [I] infoPtr : valid pointer to the listview structure
8182 * [I] wStyleType : window style type (normal or extended)
8183 * [I] lpss : window style information
8185 * RETURN:
8186 * Zero
8188 static INT LISTVIEW_StyleChanged(LISTVIEW_INFO *infoPtr, WPARAM wStyleType,
8189 const STYLESTRUCT *lpss)
8191 UINT uNewView = lpss->styleNew & LVS_TYPEMASK;
8192 UINT uOldView = lpss->styleOld & LVS_TYPEMASK;
8194 TRACE("(styletype=%x, styleOld=0x%08lx, styleNew=0x%08lx)\n",
8195 wStyleType, lpss->styleOld, lpss->styleNew);
8197 if (wStyleType != GWL_STYLE) return 0;
8199 /* FIXME: if LVS_NOSORTHEADER changed, update header */
8200 /* what if LVS_OWNERDATA changed? */
8201 /* or LVS_SINGLESEL */
8202 /* or LVS_SORT{AS,DES}CENDING */
8204 infoPtr->dwStyle = lpss->styleNew;
8206 if (((lpss->styleOld & WS_HSCROLL) != 0)&&
8207 ((lpss->styleNew & WS_HSCROLL) == 0))
8208 ShowScrollBar(infoPtr->hwndSelf, SB_HORZ, FALSE);
8210 if (((lpss->styleOld & WS_VSCROLL) != 0)&&
8211 ((lpss->styleNew & WS_VSCROLL) == 0))
8212 ShowScrollBar(infoPtr->hwndSelf, SB_VERT, FALSE);
8214 if (uNewView != uOldView)
8216 SIZE oldIconSize = infoPtr->iconSize;
8217 HIMAGELIST himl;
8219 SendMessageW(infoPtr->hwndEdit, WM_KILLFOCUS, 0, 0);
8220 ShowWindow(infoPtr->hwndHeader, SW_HIDE);
8222 ShowScrollBar(infoPtr->hwndSelf, SB_BOTH, FALSE);
8223 SetRectEmpty(&infoPtr->rcFocus);
8225 himl = (uNewView == LVS_ICON ? infoPtr->himlNormal : infoPtr->himlSmall);
8226 set_icon_size(&infoPtr->iconSize, himl, uNewView != LVS_ICON);
8228 if (uNewView == LVS_ICON)
8230 if ((infoPtr->iconSize.cx != oldIconSize.cx) || (infoPtr->iconSize.cy != oldIconSize.cy))
8232 TRACE("icon old size=(%ld,%ld), new size=(%ld,%ld)\n",
8233 oldIconSize.cx, oldIconSize.cy, infoPtr->iconSize.cx, infoPtr->iconSize.cy);
8234 LISTVIEW_SetIconSpacing(infoPtr, 0);
8237 else if (uNewView == LVS_REPORT)
8239 HDLAYOUT hl;
8240 WINDOWPOS wp;
8242 hl.prc = &infoPtr->rcList;
8243 hl.pwpos = &wp;
8244 Header_Layout(infoPtr->hwndHeader, &hl);
8245 SetWindowPos(infoPtr->hwndHeader, infoPtr->hwndSelf, wp.x, wp.y, wp.cx, wp.cy, wp.flags);
8248 LISTVIEW_UpdateItemSize(infoPtr);
8251 if (uNewView == LVS_REPORT)
8252 ShowWindow(infoPtr->hwndHeader, (lpss->styleNew & LVS_NOCOLUMNHEADER) ? SW_HIDE : SW_SHOWNORMAL);
8254 if ( (uNewView == LVS_ICON || uNewView == LVS_SMALLICON) &&
8255 (uNewView != uOldView || ((lpss->styleNew ^ lpss->styleOld) & LVS_ALIGNMASK)) )
8256 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
8258 /* update the size of the client area */
8259 LISTVIEW_UpdateSize(infoPtr);
8261 /* add scrollbars if needed */
8262 LISTVIEW_UpdateScroll(infoPtr);
8264 /* invalidate client area + erase background */
8265 LISTVIEW_InvalidateList(infoPtr);
8267 return 0;
8270 /***
8271 * DESCRIPTION:
8272 * Window procedure of the listview control.
8275 static LRESULT WINAPI
8276 LISTVIEW_WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
8278 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)GetWindowLongW(hwnd, 0);
8280 TRACE("(uMsg=%x wParam=%x lParam=%lx)\n", uMsg, wParam, lParam);
8282 if (!infoPtr && (uMsg != WM_CREATE))
8283 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
8285 if (infoPtr)
8287 infoPtr->dwStyle = GetWindowLongW(hwnd, GWL_STYLE);
8290 switch (uMsg)
8292 case LVM_APPROXIMATEVIEWRECT:
8293 return LISTVIEW_ApproximateViewRect(infoPtr, (INT)wParam,
8294 LOWORD(lParam), HIWORD(lParam));
8295 case LVM_ARRANGE:
8296 return LISTVIEW_Arrange(infoPtr, (INT)wParam);
8298 /* case LVM_CANCELEDITLABEL: */
8300 /* case LVM_CREATEDRAGIMAGE: */
8302 case LVM_DELETEALLITEMS:
8303 return LISTVIEW_DeleteAllItems(infoPtr);
8305 case LVM_DELETECOLUMN:
8306 return LISTVIEW_DeleteColumn(infoPtr, (INT)wParam);
8308 case LVM_DELETEITEM:
8309 return LISTVIEW_DeleteItem(infoPtr, (INT)wParam);
8311 case LVM_EDITLABELW:
8312 return (LRESULT)LISTVIEW_EditLabelT(infoPtr, (INT)wParam, TRUE);
8314 case LVM_EDITLABELA:
8315 return (LRESULT)LISTVIEW_EditLabelT(infoPtr, (INT)wParam, FALSE);
8317 /* case LVM_ENABLEGROUPVIEW: */
8319 case LVM_ENSUREVISIBLE:
8320 return LISTVIEW_EnsureVisible(infoPtr, (INT)wParam, (BOOL)lParam);
8322 case LVM_FINDITEMW:
8323 return LISTVIEW_FindItemW(infoPtr, (INT)wParam, (LPLVFINDINFOW)lParam);
8325 case LVM_FINDITEMA:
8326 return LISTVIEW_FindItemA(infoPtr, (INT)wParam, (LPLVFINDINFOA)lParam);
8328 case LVM_GETBKCOLOR:
8329 return infoPtr->clrBk;
8331 /* case LVM_GETBKIMAGE: */
8333 case LVM_GETCALLBACKMASK:
8334 return infoPtr->uCallbackMask;
8336 case LVM_GETCOLUMNA:
8337 return LISTVIEW_GetColumnT(infoPtr, (INT)wParam, (LPLVCOLUMNW)lParam, FALSE);
8339 case LVM_GETCOLUMNW:
8340 return LISTVIEW_GetColumnT(infoPtr, (INT)wParam, (LPLVCOLUMNW)lParam, TRUE);
8342 case LVM_GETCOLUMNORDERARRAY:
8343 return LISTVIEW_GetColumnOrderArray(infoPtr, (INT)wParam, (LPINT)lParam);
8345 case LVM_GETCOLUMNWIDTH:
8346 return LISTVIEW_GetColumnWidth(infoPtr, (INT)wParam);
8348 case LVM_GETCOUNTPERPAGE:
8349 return LISTVIEW_GetCountPerPage(infoPtr);
8351 case LVM_GETEDITCONTROL:
8352 return (LRESULT)infoPtr->hwndEdit;
8354 case LVM_GETEXTENDEDLISTVIEWSTYLE:
8355 return infoPtr->dwLvExStyle;
8357 /* case LVM_GETGROUPINFO: */
8359 /* case LVM_GETGROUPMETRICS: */
8361 case LVM_GETHEADER:
8362 return (LRESULT)infoPtr->hwndHeader;
8364 case LVM_GETHOTCURSOR:
8365 return (LRESULT)infoPtr->hHotCursor;
8367 case LVM_GETHOTITEM:
8368 return infoPtr->nHotItem;
8370 case LVM_GETHOVERTIME:
8371 return infoPtr->dwHoverTime;
8373 case LVM_GETIMAGELIST:
8374 return (LRESULT)LISTVIEW_GetImageList(infoPtr, (INT)wParam);
8376 /* case LVM_GETINSERTMARK: */
8378 /* case LVM_GETINSERTMARKCOLOR: */
8380 /* case LVM_GETINSERTMARKRECT: */
8382 case LVM_GETISEARCHSTRINGA:
8383 case LVM_GETISEARCHSTRINGW:
8384 FIXME("LVM_GETISEARCHSTRING: unimplemented\n");
8385 return FALSE;
8387 case LVM_GETITEMA:
8388 return LISTVIEW_GetItemExtT(infoPtr, (LPLVITEMW)lParam, FALSE);
8390 case LVM_GETITEMW:
8391 return LISTVIEW_GetItemExtT(infoPtr, (LPLVITEMW)lParam, TRUE);
8393 case LVM_GETITEMCOUNT:
8394 return infoPtr->nItemCount;
8396 case LVM_GETITEMPOSITION:
8397 return LISTVIEW_GetItemPosition(infoPtr, (INT)wParam, (LPPOINT)lParam);
8399 case LVM_GETITEMRECT:
8400 return LISTVIEW_GetItemRect(infoPtr, (INT)wParam, (LPRECT)lParam);
8402 case LVM_GETITEMSPACING:
8403 return LISTVIEW_GetItemSpacing(infoPtr, (BOOL)wParam);
8405 case LVM_GETITEMSTATE:
8406 return LISTVIEW_GetItemState(infoPtr, (INT)wParam, (UINT)lParam);
8408 case LVM_GETITEMTEXTA:
8409 return LISTVIEW_GetItemTextT(infoPtr, (INT)wParam, (LPLVITEMW)lParam, FALSE);
8411 case LVM_GETITEMTEXTW:
8412 return LISTVIEW_GetItemTextT(infoPtr, (INT)wParam, (LPLVITEMW)lParam, TRUE);
8414 case LVM_GETNEXTITEM:
8415 return LISTVIEW_GetNextItem(infoPtr, (INT)wParam, LOWORD(lParam));
8417 case LVM_GETNUMBEROFWORKAREAS:
8418 FIXME("LVM_GETNUMBEROFWORKAREAS: unimplemented\n");
8419 return 1;
8421 case LVM_GETORIGIN:
8422 if (!lParam) return FALSE;
8423 LISTVIEW_GetOrigin(infoPtr, (LPPOINT)lParam);
8424 return TRUE;
8426 /* case LVM_GETOUTLINECOLOR: */
8428 /* case LVM_GETSELECTEDCOLUMN: */
8430 case LVM_GETSELECTEDCOUNT:
8431 return LISTVIEW_GetSelectedCount(infoPtr);
8433 case LVM_GETSELECTIONMARK:
8434 return infoPtr->nSelectionMark;
8436 case LVM_GETSTRINGWIDTHA:
8437 return LISTVIEW_GetStringWidthT(infoPtr, (LPCWSTR)lParam, FALSE);
8439 case LVM_GETSTRINGWIDTHW:
8440 return LISTVIEW_GetStringWidthT(infoPtr, (LPCWSTR)lParam, TRUE);
8442 case LVM_GETSUBITEMRECT:
8443 return LISTVIEW_GetSubItemRect(infoPtr, (UINT)wParam, (LPRECT)lParam);
8445 case LVM_GETTEXTBKCOLOR:
8446 return infoPtr->clrTextBk;
8448 case LVM_GETTEXTCOLOR:
8449 return infoPtr->clrText;
8451 /* case LVM_GETTILEINFO: */
8453 /* case LVM_GETTILEVIEWINFO: */
8455 case LVM_GETTOOLTIPS:
8456 FIXME("LVM_GETTOOLTIPS: unimplemented\n");
8457 return FALSE;
8459 case LVM_GETTOPINDEX:
8460 return LISTVIEW_GetTopIndex(infoPtr);
8462 /*case LVM_GETUNICODEFORMAT:
8463 FIXME("LVM_GETUNICODEFORMAT: unimplemented\n");
8464 return FALSE;*/
8466 /* case LVM_GETVIEW: */
8468 case LVM_GETVIEWRECT:
8469 return LISTVIEW_GetViewRect(infoPtr, (LPRECT)lParam);
8471 case LVM_GETWORKAREAS:
8472 FIXME("LVM_GETWORKAREAS: unimplemented\n");
8473 return FALSE;
8475 /* case LVM_HASGROUP: */
8477 case LVM_HITTEST:
8478 return LISTVIEW_HitTest(infoPtr, (LPLVHITTESTINFO)lParam, FALSE, FALSE);
8480 case LVM_INSERTCOLUMNA:
8481 return LISTVIEW_InsertColumnT(infoPtr, (INT)wParam, (LPLVCOLUMNW)lParam, FALSE);
8483 case LVM_INSERTCOLUMNW:
8484 return LISTVIEW_InsertColumnT(infoPtr, (INT)wParam, (LPLVCOLUMNW)lParam, TRUE);
8486 /* case LVM_INSERTGROUP: */
8488 /* case LVM_INSERTGROUPSORTED: */
8490 case LVM_INSERTITEMA:
8491 return LISTVIEW_InsertItemT(infoPtr, (LPLVITEMW)lParam, FALSE);
8493 case LVM_INSERTITEMW:
8494 return LISTVIEW_InsertItemT(infoPtr, (LPLVITEMW)lParam, TRUE);
8496 /* case LVM_INSERTMARKHITTEST: */
8498 /* case LVM_ISGROUPVIEWENABLED: */
8500 /* case LVM_MAPIDTOINDEX: */
8502 /* case LVM_MAPINDEXTOID: */
8504 /* case LVM_MOVEGROUP: */
8506 /* case LVM_MOVEITEMTOGROUP: */
8508 case LVM_REDRAWITEMS:
8509 return LISTVIEW_RedrawItems(infoPtr, (INT)wParam, (INT)lParam);
8511 /* case LVM_REMOVEALLGROUPS: */
8513 /* case LVM_REMOVEGROUP: */
8515 case LVM_SCROLL:
8516 return LISTVIEW_Scroll(infoPtr, (INT)wParam, (INT)lParam);
8518 case LVM_SETBKCOLOR:
8519 return LISTVIEW_SetBkColor(infoPtr, (COLORREF)lParam);
8521 /* case LVM_SETBKIMAGE: */
8523 case LVM_SETCALLBACKMASK:
8524 infoPtr->uCallbackMask = (UINT)wParam;
8525 return TRUE;
8527 case LVM_SETCOLUMNA:
8528 return LISTVIEW_SetColumnT(infoPtr, (INT)wParam, (LPLVCOLUMNW)lParam, FALSE);
8530 case LVM_SETCOLUMNW:
8531 return LISTVIEW_SetColumnT(infoPtr, (INT)wParam, (LPLVCOLUMNW)lParam, TRUE);
8533 case LVM_SETCOLUMNORDERARRAY:
8534 return LISTVIEW_SetColumnOrderArray(infoPtr, (INT)wParam, (LPINT)lParam);
8536 case LVM_SETCOLUMNWIDTH:
8537 return LISTVIEW_SetColumnWidth(infoPtr, (INT)wParam, SLOWORD(lParam));
8539 case LVM_SETEXTENDEDLISTVIEWSTYLE:
8540 return LISTVIEW_SetExtendedListViewStyle(infoPtr, (DWORD)wParam, (DWORD)lParam);
8542 /* case LVM_SETGROUPINFO: */
8544 /* case LVM_SETGROUPMETRICS: */
8546 case LVM_SETHOTCURSOR:
8547 return (LRESULT)LISTVIEW_SetHotCursor(infoPtr, (HCURSOR)lParam);
8549 case LVM_SETHOTITEM:
8550 return LISTVIEW_SetHotItem(infoPtr, (INT)wParam);
8552 case LVM_SETHOVERTIME:
8553 return LISTVIEW_SetHoverTime(infoPtr, (DWORD)wParam);
8555 case LVM_SETICONSPACING:
8556 return LISTVIEW_SetIconSpacing(infoPtr, (DWORD)lParam);
8558 case LVM_SETIMAGELIST:
8559 return (LRESULT)LISTVIEW_SetImageList(infoPtr, (INT)wParam, (HIMAGELIST)lParam);
8561 /* case LVM_SETINFOTIP: */
8563 /* case LVM_SETINSERTMARK: */
8565 /* case LVM_SETINSERTMARKCOLOR: */
8567 case LVM_SETITEMA:
8568 return LISTVIEW_SetItemT(infoPtr, (LPLVITEMW)lParam, FALSE);
8570 case LVM_SETITEMW:
8571 return LISTVIEW_SetItemT(infoPtr, (LPLVITEMW)lParam, TRUE);
8573 case LVM_SETITEMCOUNT:
8574 return LISTVIEW_SetItemCount(infoPtr, (INT)wParam, (DWORD)lParam);
8576 case LVM_SETITEMPOSITION:
8578 POINT pt = { SLOWORD(lParam), SHIWORD(lParam) };
8579 return LISTVIEW_SetItemPosition(infoPtr, (INT)wParam, pt);
8582 case LVM_SETITEMPOSITION32:
8583 if (lParam == 0) return FALSE;
8584 return LISTVIEW_SetItemPosition(infoPtr, (INT)wParam, *((POINT*)lParam));
8586 case LVM_SETITEMSTATE:
8587 return LISTVIEW_SetItemState(infoPtr, (INT)wParam, (LPLVITEMW)lParam);
8589 case LVM_SETITEMTEXTA:
8590 return LISTVIEW_SetItemTextT(infoPtr, (INT)wParam, (LPLVITEMW)lParam, FALSE);
8592 case LVM_SETITEMTEXTW:
8593 return LISTVIEW_SetItemTextT(infoPtr, (INT)wParam, (LPLVITEMW)lParam, TRUE);
8595 /* case LVM_SETOUTLINECOLOR: */
8597 /* case LVM_SETSELECTEDCOLUMN: */
8599 case LVM_SETSELECTIONMARK:
8600 return LISTVIEW_SetSelectionMark(infoPtr, (INT)lParam);
8602 case LVM_SETTEXTBKCOLOR:
8603 return LISTVIEW_SetTextBkColor(infoPtr, (COLORREF)lParam);
8605 case LVM_SETTEXTCOLOR:
8606 return LISTVIEW_SetTextColor(infoPtr, (COLORREF)lParam);
8608 /* case LVM_SETTILEINFO: */
8610 /* case LVM_SETTILEVIEWINFO: */
8612 /* case LVM_SETTILEWIDTH: */
8614 /* case LVM_SETTOOLTIPS: */
8616 /* case LVM_SETUNICODEFORMAT: */
8618 /* case LVM_SETVIEW: */
8620 /* case LVM_SETWORKAREAS: */
8622 /* case LVM_SORTGROUPS: */
8624 case LVM_SORTITEMS:
8625 return LISTVIEW_SortItems(infoPtr, (PFNLVCOMPARE)lParam, (LPARAM)wParam);
8627 /* LVM_SORTITEMSEX: */
8629 case LVM_SUBITEMHITTEST:
8630 return LISTVIEW_HitTest(infoPtr, (LPLVHITTESTINFO)lParam, TRUE, FALSE);
8632 case LVM_UPDATE:
8633 return LISTVIEW_Update(infoPtr, (INT)wParam);
8635 case WM_CHAR:
8636 return LISTVIEW_ProcessLetterKeys( infoPtr, wParam, lParam );
8638 case WM_COMMAND:
8639 return LISTVIEW_Command(infoPtr, wParam, lParam);
8641 case WM_CREATE:
8642 return LISTVIEW_Create(hwnd, (LPCREATESTRUCTW)lParam);
8644 case WM_ERASEBKGND:
8645 return LISTVIEW_EraseBkgnd(infoPtr, (HDC)wParam);
8647 case WM_GETDLGCODE:
8648 return DLGC_WANTCHARS | DLGC_WANTARROWS;
8650 case WM_GETFONT:
8651 return (LRESULT)infoPtr->hFont;
8653 case WM_HSCROLL:
8654 return LISTVIEW_HScroll(infoPtr, (INT)LOWORD(wParam), 0, (HWND)lParam);
8656 case WM_KEYDOWN:
8657 return LISTVIEW_KeyDown(infoPtr, (INT)wParam, (LONG)lParam);
8659 case WM_KILLFOCUS:
8660 return LISTVIEW_KillFocus(infoPtr);
8662 case WM_LBUTTONDBLCLK:
8663 return LISTVIEW_LButtonDblClk(infoPtr, (WORD)wParam, MAKEPOINTS(lParam));
8665 case WM_LBUTTONDOWN:
8666 return LISTVIEW_LButtonDown(infoPtr, (WORD)wParam, MAKEPOINTS(lParam));
8668 case WM_LBUTTONUP:
8669 return LISTVIEW_LButtonUp(infoPtr, (WORD)wParam, MAKEPOINTS(lParam));
8671 case WM_MOUSEMOVE:
8672 return LISTVIEW_MouseMove (infoPtr, (WORD)wParam, MAKEPOINTS(lParam));
8674 case WM_MOUSEHOVER:
8675 return LISTVIEW_MouseHover(infoPtr, (WORD)wParam, MAKEPOINTS(lParam));
8677 case WM_NCDESTROY:
8678 return LISTVIEW_NCDestroy(infoPtr);
8680 case WM_NOTIFY:
8681 if (lParam && ((LPNMHDR)lParam)->hwndFrom == infoPtr->hwndHeader)
8682 return LISTVIEW_HeaderNotification(infoPtr, (LPNMHEADERW)lParam);
8683 else return 0;
8685 case WM_NOTIFYFORMAT:
8686 return LISTVIEW_NotifyFormat(infoPtr, (HWND)wParam, (INT)lParam);
8688 case WM_PAINT:
8689 return LISTVIEW_Paint(infoPtr, (HDC)wParam);
8691 case WM_RBUTTONDBLCLK:
8692 return LISTVIEW_RButtonDblClk(infoPtr, (WORD)wParam, MAKEPOINTS(lParam));
8694 case WM_RBUTTONDOWN:
8695 return LISTVIEW_RButtonDown(infoPtr, (WORD)wParam, MAKEPOINTS(lParam));
8697 case WM_RBUTTONUP:
8698 return LISTVIEW_RButtonUp(infoPtr, (WORD)wParam, MAKEPOINTS(lParam));
8700 case WM_SETCURSOR:
8701 if(LISTVIEW_SetCursor(infoPtr, (HWND)wParam, LOWORD(lParam), HIWORD(lParam)))
8702 return TRUE;
8703 goto fwd_msg;
8705 case WM_SETFOCUS:
8706 return LISTVIEW_SetFocus(infoPtr, (HWND)wParam);
8708 case WM_SETFONT:
8709 return LISTVIEW_SetFont(infoPtr, (HFONT)wParam, (WORD)lParam);
8711 case WM_SETREDRAW:
8712 return LISTVIEW_SetRedraw(infoPtr, (BOOL)wParam);
8714 case WM_SIZE:
8715 return LISTVIEW_Size(infoPtr, (int)SLOWORD(lParam), (int)SHIWORD(lParam));
8717 case WM_STYLECHANGED:
8718 return LISTVIEW_StyleChanged(infoPtr, wParam, (LPSTYLESTRUCT)lParam);
8720 case WM_SYSCOLORCHANGE:
8721 COMCTL32_RefreshSysColors();
8722 return 0;
8724 /* case WM_TIMER: */
8726 case WM_VSCROLL:
8727 return LISTVIEW_VScroll(infoPtr, (INT)LOWORD(wParam), 0, (HWND)lParam);
8729 case WM_MOUSEWHEEL:
8730 if (wParam & (MK_SHIFT | MK_CONTROL))
8731 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
8732 return LISTVIEW_MouseWheel(infoPtr, (short int)HIWORD(wParam));
8734 case WM_WINDOWPOSCHANGED:
8735 if (!(((WINDOWPOS *)lParam)->flags & SWP_NOSIZE))
8737 SetWindowPos(infoPtr->hwndSelf, 0, 0, 0, 0, 0, SWP_FRAMECHANGED | SWP_NOACTIVATE |
8738 SWP_NOZORDER | SWP_NOMOVE | SWP_NOSIZE);
8739 /* FIXME: why do we need this here? */
8740 LISTVIEW_UpdateSize(infoPtr);
8741 LISTVIEW_UpdateScroll(infoPtr);
8743 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
8745 /* case WM_WININICHANGE: */
8747 default:
8748 if ((uMsg >= WM_USER) && (uMsg < WM_APP))
8749 ERR("unknown msg %04x wp=%08x lp=%08lx\n", uMsg, wParam, lParam);
8751 fwd_msg:
8752 /* call default window procedure */
8753 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
8756 return 0;
8759 /***
8760 * DESCRIPTION:
8761 * Registers the window class.
8763 * PARAMETER(S):
8764 * None
8766 * RETURN:
8767 * None
8769 void LISTVIEW_Register(void)
8771 WNDCLASSW wndClass;
8773 ZeroMemory(&wndClass, sizeof(WNDCLASSW));
8774 wndClass.style = CS_GLOBALCLASS | CS_DBLCLKS;
8775 wndClass.lpfnWndProc = (WNDPROC)LISTVIEW_WindowProc;
8776 wndClass.cbClsExtra = 0;
8777 wndClass.cbWndExtra = sizeof(LISTVIEW_INFO *);
8778 wndClass.hCursor = LoadCursorW(0, IDC_ARROWW);
8779 wndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
8780 wndClass.lpszClassName = WC_LISTVIEWW;
8781 RegisterClassW(&wndClass);
8784 /***
8785 * DESCRIPTION:
8786 * Unregisters the window class.
8788 * PARAMETER(S):
8789 * None
8791 * RETURN:
8792 * None
8794 void LISTVIEW_Unregister(void)
8796 UnregisterClassW(WC_LISTVIEWW, (HINSTANCE)NULL);
8799 /***
8800 * DESCRIPTION:
8801 * Handle any WM_COMMAND messages
8803 * PARAMETER(S):
8804 * [I] infoPtr : valid pointer to the listview structure
8805 * [I] wParam : the first message parameter
8806 * [I] lParam : the second message parameter
8808 * RETURN:
8809 * Zero.
8811 static LRESULT LISTVIEW_Command(LISTVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
8813 switch (HIWORD(wParam))
8815 case EN_UPDATE:
8818 * Adjust the edit window size
8820 WCHAR buffer[1024];
8821 HDC hdc = GetDC(infoPtr->hwndEdit);
8822 HFONT hFont, hOldFont = 0;
8823 RECT rect;
8824 SIZE sz;
8825 int len;
8827 if (!infoPtr->hwndEdit || !hdc) return 0;
8828 len = GetWindowTextW(infoPtr->hwndEdit, buffer, sizeof(buffer)/sizeof(buffer[0]));
8829 GetWindowRect(infoPtr->hwndEdit, &rect);
8831 /* Select font to get the right dimension of the string */
8832 hFont = (HFONT)SendMessageW(infoPtr->hwndEdit, WM_GETFONT, 0, 0);
8833 if(hFont != 0)
8835 hOldFont = SelectObject(hdc, hFont);
8838 if (GetTextExtentPoint32W(hdc, buffer, lstrlenW(buffer), &sz))
8840 TEXTMETRICW textMetric;
8842 /* Add Extra spacing for the next character */
8843 GetTextMetricsW(hdc, &textMetric);
8844 sz.cx += (textMetric.tmMaxCharWidth * 2);
8846 SetWindowPos (
8847 infoPtr->hwndEdit,
8848 HWND_TOP,
8851 sz.cx,
8852 rect.bottom - rect.top,
8853 SWP_DRAWFRAME|SWP_NOMOVE);
8855 if(hFont != 0)
8856 SelectObject(hdc, hOldFont);
8858 ReleaseDC(infoPtr->hwndSelf, hdc);
8860 break;
8863 default:
8864 return SendMessageW (GetParent (infoPtr->hwndSelf), WM_COMMAND, wParam, lParam);
8867 return 0;
8871 /***
8872 * DESCRIPTION:
8873 * Subclassed edit control windproc function
8875 * PARAMETER(S):
8876 * [I] hwnd : the edit window handle
8877 * [I] uMsg : the message that is to be processed
8878 * [I] wParam : first message parameter
8879 * [I] lParam : second message parameter
8880 * [I] isW : TRUE if input is Unicode
8882 * RETURN:
8883 * Zero.
8885 static LRESULT EditLblWndProcT(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL isW)
8887 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)GetWindowLongW(GetParent(hwnd), 0);
8888 BOOL cancel = FALSE;
8890 TRACE("(hwnd=%p, uMsg=%x, wParam=%x, lParam=%lx, isW=%d)\n",
8891 hwnd, uMsg, wParam, lParam, isW);
8893 switch (uMsg)
8895 case WM_GETDLGCODE:
8896 return DLGC_WANTARROWS | DLGC_WANTALLKEYS;
8898 case WM_KILLFOCUS:
8899 break;
8901 case WM_DESTROY:
8903 WNDPROC editProc = infoPtr->EditWndProc;
8904 infoPtr->EditWndProc = 0;
8905 SetWindowLongW(hwnd, GWL_WNDPROC, (LONG)editProc);
8906 return CallWindowProcT(editProc, hwnd, uMsg, wParam, lParam, isW);
8909 case WM_KEYDOWN:
8910 if (VK_ESCAPE == (INT)wParam)
8912 cancel = TRUE;
8913 break;
8915 else if (VK_RETURN == (INT)wParam)
8916 break;
8918 default:
8919 return CallWindowProcT(infoPtr->EditWndProc, hwnd, uMsg, wParam, lParam, isW);
8922 /* kill the edit */
8923 if (infoPtr->hwndEdit)
8925 LPWSTR buffer = NULL;
8927 infoPtr->hwndEdit = 0;
8928 if (!cancel)
8930 DWORD len = isW ? GetWindowTextLengthW(hwnd) : GetWindowTextLengthA(hwnd);
8932 if (len)
8934 if ( (buffer = COMCTL32_Alloc((len+1) * (isW ? sizeof(WCHAR) : sizeof(CHAR)))) )
8936 if (isW) GetWindowTextW(hwnd, buffer, len+1);
8937 else GetWindowTextA(hwnd, (CHAR*)buffer, len+1);
8941 LISTVIEW_EndEditLabelT(infoPtr, buffer, isW);
8943 if (buffer) COMCTL32_Free(buffer);
8947 SendMessageW(hwnd, WM_CLOSE, 0, 0);
8948 return 0;
8951 /***
8952 * DESCRIPTION:
8953 * Subclassed edit control Unicode windproc function
8955 * PARAMETER(S):
8956 * [I] hwnd : the edit window handle
8957 * [I] uMsg : the message that is to be processed
8958 * [I] wParam : first message parameter
8959 * [I] lParam : second message parameter
8961 * RETURN:
8963 LRESULT CALLBACK EditLblWndProcW(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
8965 return EditLblWndProcT(hwnd, uMsg, wParam, lParam, TRUE);
8968 /***
8969 * DESCRIPTION:
8970 * Subclassed edit control ANSI windproc function
8972 * PARAMETER(S):
8973 * [I] hwnd : the edit window handle
8974 * [I] uMsg : the message that is to be processed
8975 * [I] wParam : first message parameter
8976 * [I] lParam : second message parameter
8978 * RETURN:
8980 LRESULT CALLBACK EditLblWndProcA(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
8982 return EditLblWndProcT(hwnd, uMsg, wParam, lParam, FALSE);
8985 /***
8986 * DESCRIPTION:
8987 * Creates a subclassed edit cotrol
8989 * PARAMETER(S):
8990 * [I] infoPtr : valid pointer to the listview structure
8991 * [I] text : initial text for the edit
8992 * [I] style : the window style
8993 * [I] isW : TRUE if input is Unicode
8995 * RETURN:
8997 static HWND CreateEditLabelT(LISTVIEW_INFO *infoPtr, LPCWSTR text, DWORD style,
8998 INT x, INT y, INT width, INT height, BOOL isW)
9000 WCHAR editName[5] = { 'E', 'd', 'i', 't', '\0' };
9001 HWND hedit;
9002 SIZE sz;
9003 HDC hdc;
9004 HDC hOldFont=0;
9005 TEXTMETRICW textMetric;
9006 HINSTANCE hinst = (HINSTANCE)GetWindowLongW(infoPtr->hwndSelf, GWL_HINSTANCE);
9008 TRACE("(text=%s, ..., isW=%d)\n", debugtext_t(text, isW), isW);
9010 style |= WS_CHILDWINDOW|WS_CLIPSIBLINGS|ES_LEFT|WS_BORDER;
9011 hdc = GetDC(infoPtr->hwndSelf);
9013 /* Select the font to get appropriate metric dimensions */
9014 if(infoPtr->hFont != 0)
9015 hOldFont = SelectObject(hdc, infoPtr->hFont);
9017 /*Get String Lenght in pixels */
9018 GetTextExtentPoint32W(hdc, text, lstrlenW(text), &sz);
9020 /*Add Extra spacing for the next character */
9021 GetTextMetricsW(hdc, &textMetric);
9022 sz.cx += (textMetric.tmMaxCharWidth * 2);
9024 if(infoPtr->hFont != 0)
9025 SelectObject(hdc, hOldFont);
9027 ReleaseDC(infoPtr->hwndSelf, hdc);
9028 if (isW)
9029 hedit = CreateWindowW(editName, text, style, x, y, sz.cx, height, infoPtr->hwndSelf, 0, hinst, 0);
9030 else
9031 hedit = CreateWindowA("Edit", (LPCSTR)text, style, x, y, sz.cx, height, infoPtr->hwndSelf, 0, hinst, 0);
9033 if (!hedit) return 0;
9035 infoPtr->EditWndProc = (WNDPROC)
9036 (isW ? SetWindowLongW(hedit, GWL_WNDPROC, (LONG)EditLblWndProcW) :
9037 SetWindowLongA(hedit, GWL_WNDPROC, (LONG)EditLblWndProcA) );
9039 SendMessageW(hedit, WM_SETFONT, (WPARAM)infoPtr->hFont, FALSE);
9041 return hedit;