Fix crash-inducing typo/thinko.
[wine.git] / dlls / comctl32 / listview.c
blob5de02f4f1fc920d3f0458dc4c8a6551034409258
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;
724 TRACE("code=%d, lvht=%s\n", code, debuglvhittestinfo(lvht));
725 ZeroMemory(&nmlv, sizeof(nmlv));
726 nmlv.iItem = lvht->iItem;
727 nmlv.iSubItem = lvht->iSubItem;
728 nmlv.ptAction = lvht->pt;
729 return notify_listview(infoPtr, code, &nmlv);
732 static int get_ansi_notification(INT unicodeNotificationCode)
734 switch (unicodeNotificationCode)
736 case LVN_BEGINLABELEDITW: return LVN_BEGINLABELEDITA;
737 case LVN_ENDLABELEDITW: return LVN_ENDLABELEDITA;
738 case LVN_GETDISPINFOW: return LVN_GETDISPINFOA;
739 case LVN_SETDISPINFOW: return LVN_SETDISPINFOA;
740 case LVN_ODFINDITEMW: return LVN_ODFINDITEMA;
741 case LVN_GETINFOTIPW: return LVN_GETINFOTIPA;
743 ERR("unknown notification %x\n", unicodeNotificationCode);
744 assert(FALSE);
748 Send notification. depends on dispinfoW having same
749 structure as dispinfoA.
750 infoPtr : listview struct
751 notificationCode : *Unicode* notification code
752 pdi : dispinfo structure (can be unicode or ansi)
753 isW : TRUE if dispinfo is Unicode
755 static BOOL notify_dispinfoT(LISTVIEW_INFO *infoPtr, INT notificationCode, LPNMLVDISPINFOW pdi, BOOL isW)
757 BOOL bResult = FALSE;
758 BOOL convertToAnsi = FALSE, convertToUnicode = FALSE;
759 INT realNotifCode;
760 INT cchTempBufMax = 0, savCchTextMax = 0;
761 LPWSTR pszTempBuf = NULL, savPszText = NULL;
763 if ((pdi->item.mask & LVIF_TEXT) && is_textT(pdi->item.pszText, isW))
765 convertToAnsi = (isW && infoPtr->notifyFormat == NFR_ANSI);
766 convertToUnicode = (!isW && infoPtr->notifyFormat == NFR_UNICODE);
769 if (convertToAnsi || convertToUnicode)
771 if (notificationCode != LVN_GETDISPINFOW)
773 cchTempBufMax = convertToUnicode ?
774 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pdi->item.pszText, -1, NULL, 0):
775 WideCharToMultiByte(CP_ACP, 0, pdi->item.pszText, -1, NULL, 0, NULL, NULL);
777 else
779 cchTempBufMax = pdi->item.cchTextMax;
780 *pdi->item.pszText = 0; /* make sure we don't process garbage */
783 pszTempBuf = HeapAlloc(GetProcessHeap(), 0,
784 (convertToUnicode ? sizeof(WCHAR) : sizeof(CHAR)) * cchTempBufMax);
785 if (!pszTempBuf) return FALSE;
786 if (convertToUnicode)
787 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pdi->item.pszText, -1,
788 pszTempBuf, cchTempBufMax);
789 else
790 WideCharToMultiByte(CP_ACP, 0, pdi->item.pszText, -1, (LPSTR) pszTempBuf,
791 cchTempBufMax, NULL, NULL);
792 savCchTextMax = pdi->item.cchTextMax;
793 savPszText = pdi->item.pszText;
794 pdi->item.pszText = pszTempBuf;
795 pdi->item.cchTextMax = cchTempBufMax;
798 if (infoPtr->notifyFormat == NFR_ANSI)
799 realNotifCode = get_ansi_notification(notificationCode);
800 else
801 realNotifCode = notificationCode;
802 TRACE(" pdi->item=%s\n", debuglvitem_t(&pdi->item, infoPtr->notifyFormat != NFR_ANSI));
803 bResult = notify_hdr(infoPtr, realNotifCode, (LPNMHDR)pdi);
805 if (convertToUnicode || convertToAnsi)
807 if (convertToUnicode) /* note : pointer can be changed by app ! */
808 WideCharToMultiByte(CP_ACP, 0, pdi->item.pszText, -1, (LPSTR) savPszText,
809 savCchTextMax, NULL, NULL);
810 else
811 MultiByteToWideChar(CP_ACP, 0, (LPSTR) pdi->item.pszText, -1,
812 savPszText, savCchTextMax);
813 pdi->item.pszText = savPszText; /* restores our buffer */
814 pdi->item.cchTextMax = savCchTextMax;
815 HeapFree(GetProcessHeap(), 0, pszTempBuf);
817 return bResult;
820 static void customdraw_fill(NMLVCUSTOMDRAW *lpnmlvcd, LISTVIEW_INFO *infoPtr, HDC hdc, const RECT *rcBounds)
822 ZeroMemory(lpnmlvcd, sizeof(NMLVCUSTOMDRAW));
823 lpnmlvcd->nmcd.hdc = hdc;
824 lpnmlvcd->nmcd.rc = *rcBounds;
825 lpnmlvcd->clrTextBk = infoPtr->clrTextBk;
826 lpnmlvcd->clrText = infoPtr->clrText;
829 static inline DWORD notify_customdraw (LISTVIEW_INFO *infoPtr, DWORD dwDrawStage, NMLVCUSTOMDRAW *lpnmlvcd)
831 lpnmlvcd->nmcd.dwDrawStage = dwDrawStage;
832 return notify_hdr(infoPtr, NM_CUSTOMDRAW, &lpnmlvcd->nmcd.hdr);
835 /******** Item iterator functions **********************************/
837 static RANGES ranges_create(int count);
838 static void ranges_destroy(RANGES ranges);
839 static BOOL ranges_add(RANGES ranges, RANGE range);
840 static BOOL ranges_del(RANGES ranges, RANGE range);
841 static void ranges_dump(RANGES ranges);
843 static inline BOOL ranges_additem(RANGES ranges, INT nItem)
845 RANGE range = { nItem, nItem + 1 };
847 return ranges_add(ranges, range);
850 static inline BOOL ranges_delitem(RANGES ranges, INT nItem)
852 RANGE range = { nItem, nItem + 1 };
854 return ranges_del(ranges, range);
857 /***
858 * ITERATOR DOCUMENTATION
860 * The iterator functions allow for easy, and convenient iteration
861 * over items of iterest in the list. Typically, you create a
862 * iterator, use it, and destroy it, as such:
863 * ITERATOR i;
865 * iterator_xxxitems(&i, ...);
866 * while (iterator_{prev,next}(&i)
868 * //code which uses i.nItem
870 * iterator_destroy(&i);
872 * where xxx is either: framed, or visible.
873 * Note that it is important that the code destroys the iterator
874 * after it's done with it, as the creation of the iterator may
875 * allocate memory, which thus needs to be freed.
877 * You can iterate both forwards, and backwards through the list,
878 * by using iterator_next or iterator_prev respectively.
880 * Lower numbered items are draw on top of higher number items in
881 * LVS_ICON, and LVS_SMALLICON (which are the only modes where
882 * items may overlap). So, to test items, you should use
883 * iterator_next
884 * which lists the items top to bottom (in Z-order).
885 * For drawing items, you should use
886 * iterator_prev
887 * which lists the items bottom to top (in Z-order).
888 * If you keep iterating over the items after the end-of-items
889 * marker (-1) is returned, the iterator will start from the
890 * beginning. Typically, you don't need to test for -1,
891 * because iterator_{next,prev} will return TRUE if more items
892 * are to be iterated over, or FALSE otherwise.
894 * Note: the iterator is defined to be bidirectional. That is,
895 * any number of prev followed by any number of next, or
896 * five versa, should leave the iterator at the same item:
897 * prev * n, next * n = next * n, prev * n
899 * The iterator has a notion of a out-of-order, special item,
900 * which sits at the start of the list. This is used in
901 * LVS_ICON, and LVS_SMALLICON mode to handle the focused item,
902 * which needs to be first, as it may overlap other items.
904 * The code is a bit messy because we have:
905 * - a special item to deal with
906 * - simple range, or composite range
907 * - empty range.
908 * If find bugs, or want to add features, please make sure you
909 * always check/modify *both* iterator_prev, and iterator_next.
912 /****
913 * This function iterates through the items in increasing order,
914 * but prefixed by the special item, then -1. That is:
915 * special, 1, 2, 3, ..., n, -1.
916 * Each item is listed only once.
918 static inline BOOL iterator_next(ITERATOR* i)
920 if (i->nItem == -1)
922 i->nItem = i->nSpecial;
923 if (i->nItem != -1) return TRUE;
925 if (i->nItem == i->nSpecial)
927 if (i->ranges) i->index = 0;
928 goto pickarange;
931 i->nItem++;
932 testitem:
933 if (i->nItem == i->nSpecial) i->nItem++;
934 if (i->nItem < i->range.upper) return TRUE;
936 pickarange:
937 if (i->ranges)
939 if (i->index < i->ranges->hdpa->nItemCount)
940 i->range = *(RANGE*)DPA_GetPtr(i->ranges->hdpa, i->index++);
941 else goto end;
943 else if (i->nItem >= i->range.upper) goto end;
945 i->nItem = i->range.lower;
946 if (i->nItem >= 0) goto testitem;
947 end:
948 i->nItem = -1;
949 return FALSE;
952 /****
953 * This function iterates through the items in decreasing order,
954 * followed by the special item, then -1. That is:
955 * n, n-1, ..., 3, 2, 1, special, -1.
956 * Each item is listed only once.
958 static inline BOOL iterator_prev(ITERATOR* i)
960 BOOL start = FALSE;
962 if (i->nItem == -1)
964 start = TRUE;
965 if (i->ranges) i->index = i->ranges->hdpa->nItemCount;
966 goto pickarange;
968 if (i->nItem == i->nSpecial)
970 i->nItem = -1;
971 return FALSE;
974 testitem:
975 i->nItem--;
976 if (i->nItem == i->nSpecial) i->nItem--;
977 if (i->nItem >= i->range.lower) return TRUE;
979 pickarange:
980 if (i->ranges)
982 if (i->index > 0)
983 i->range = *(RANGE*)DPA_GetPtr(i->ranges->hdpa, --i->index);
984 else goto end;
986 else if (!start && i->nItem < i->range.lower) goto end;
988 i->nItem = i->range.upper;
989 if (i->nItem > 0) goto testitem;
990 end:
991 return (i->nItem = i->nSpecial) != -1;
994 static RANGE iterator_range(ITERATOR* i)
996 RANGE range;
998 if (!i->ranges) return i->range;
1000 range.lower = (*(RANGE*)DPA_GetPtr(i->ranges->hdpa, 0)).lower;
1001 range.upper = (*(RANGE*)DPA_GetPtr(i->ranges->hdpa, i->ranges->hdpa->nItemCount - 1)).upper;
1002 return range;
1005 /***
1006 * Releases resources associated with this ierator.
1008 static inline void iterator_destroy(ITERATOR* i)
1010 if (i->ranges) ranges_destroy(i->ranges);
1013 /***
1014 * Create an empty iterator.
1016 static inline BOOL iterator_empty(ITERATOR* i)
1018 ZeroMemory(i, sizeof(*i));
1019 i->nItem = i->nSpecial = i->range.lower = i->range.upper = -1;
1020 return TRUE;
1023 /***
1024 * Create an iterator over a range.
1026 static inline BOOL iterator_rangeitems(ITERATOR* i, RANGE range)
1028 iterator_empty(i);
1029 i->range = range;
1030 return TRUE;
1033 /***
1034 * Create an iterator over a bunch of ranges.
1035 * Please note that the iterator will take ownership of the ranges,
1036 * and will free them upon destruction.
1038 static inline BOOL iterator_rangesitems(ITERATOR* i, RANGES ranges)
1040 iterator_empty(i);
1041 i->ranges = ranges;
1042 return TRUE;
1045 /***
1046 * Creates an iterator over the items which intersect lprc.
1048 static BOOL iterator_frameditems(ITERATOR* i, LISTVIEW_INFO* infoPtr, const RECT *lprc)
1050 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
1051 RECT frame = *lprc, rcItem, rcTemp;
1052 POINT Origin;
1054 /* in case we fail, we want to return an empty iterator */
1055 if (!iterator_empty(i)) return FALSE;
1057 LISTVIEW_GetOrigin(infoPtr, &Origin);
1059 TRACE("(lprc=%s)\n", debugrect(lprc));
1060 OffsetRect(&frame, -Origin.x, -Origin.y);
1062 if (uView == LVS_ICON || uView == LVS_SMALLICON)
1064 INT nItem;
1066 if (uView == LVS_ICON && infoPtr->nFocusedItem != -1)
1068 LISTVIEW_GetItemBox(infoPtr, infoPtr->nFocusedItem, &rcItem);
1069 if (IntersectRect(&rcTemp, &rcItem, lprc))
1070 i->nSpecial = infoPtr->nFocusedItem;
1072 if (!(iterator_rangesitems(i, ranges_create(50)))) return FALSE;
1073 /* to do better here, we need to have PosX, and PosY sorted */
1074 TRACE("building icon ranges:\n");
1075 for (nItem = 0; nItem < infoPtr->nItemCount; nItem++)
1077 rcItem.left = (LONG)DPA_GetPtr(infoPtr->hdpaPosX, nItem);
1078 rcItem.top = (LONG)DPA_GetPtr(infoPtr->hdpaPosY, nItem);
1079 rcItem.right = rcItem.left + infoPtr->nItemWidth;
1080 rcItem.bottom = rcItem.top + infoPtr->nItemHeight;
1081 if (IntersectRect(&rcTemp, &rcItem, &frame))
1082 ranges_additem(i->ranges, nItem);
1084 return TRUE;
1086 else if (uView == LVS_REPORT)
1088 RANGE range;
1090 if (frame.left >= infoPtr->nItemWidth) return TRUE;
1091 if (frame.top >= infoPtr->nItemHeight * infoPtr->nItemCount) return TRUE;
1093 range.lower = max(frame.top / infoPtr->nItemHeight, 0);
1094 range.upper = min((frame.bottom - 1) / infoPtr->nItemHeight, infoPtr->nItemCount - 1) + 1;
1095 if (range.upper <= range.lower) return TRUE;
1096 if (!iterator_rangeitems(i, range)) return FALSE;
1097 TRACE(" report=%s\n", debugrange(&i->range));
1099 else
1101 INT nPerCol = max((infoPtr->rcList.bottom - infoPtr->rcList.top) / infoPtr->nItemHeight, 1);
1102 INT nFirstRow = max(frame.top / infoPtr->nItemHeight, 0);
1103 INT nLastRow = min((frame.bottom - 1) / infoPtr->nItemHeight, nPerCol - 1);
1104 INT nFirstCol = max(frame.left / infoPtr->nItemWidth, 0);
1105 INT nLastCol = min((frame.right - 1) / infoPtr->nItemWidth, (infoPtr->nItemCount + nPerCol - 1) / nPerCol);
1106 INT lower = nFirstCol * nPerCol + nFirstRow;
1107 RANGE item_range;
1108 INT nCol;
1110 TRACE("nPerCol=%d, nFirstRow=%d, nLastRow=%d, nFirstCol=%d, nLastCol=%d, lower=%d\n",
1111 nPerCol, nFirstRow, nLastRow, nFirstCol, nLastCol, lower);
1113 if (nLastCol < nFirstCol || nLastRow < nFirstRow) return TRUE;
1115 if (!(iterator_rangesitems(i, ranges_create(nLastCol - nFirstCol + 1)))) return FALSE;
1116 TRACE("building list ranges:\n");
1117 for (nCol = nFirstCol; nCol <= nLastCol; nCol++)
1119 item_range.lower = nCol * nPerCol + nFirstRow;
1120 if(item_range.lower >= infoPtr->nItemCount) break;
1121 item_range.upper = min(nCol * nPerCol + nLastRow + 1, infoPtr->nItemCount);
1122 TRACE(" list=%s\n", debugrange(&item_range));
1123 ranges_add(i->ranges, item_range);
1127 return TRUE;
1130 /***
1131 * Creates an iterator over the items which intersect the visible region of hdc.
1133 static BOOL iterator_visibleitems(ITERATOR *i, LISTVIEW_INFO *infoPtr, HDC hdc)
1135 POINT Origin, Position;
1136 RECT rcItem, rcClip;
1137 INT rgntype;
1139 rgntype = GetClipBox(hdc, &rcClip);
1140 if (rgntype == NULLREGION) return iterator_empty(i);
1141 if (!iterator_frameditems(i, infoPtr, &rcClip)) return FALSE;
1142 if (rgntype == SIMPLEREGION) return TRUE;
1144 /* first deal with the special item */
1145 if (i->nSpecial != -1)
1147 LISTVIEW_GetItemBox(infoPtr, i->nSpecial, &rcItem);
1148 if (!RectVisible(hdc, &rcItem)) i->nSpecial = -1;
1151 /* if we can't deal with the region, we'll just go with the simple range */
1152 LISTVIEW_GetOrigin(infoPtr, &Origin);
1153 TRACE("building visible range:\n");
1154 if (!i->ranges && i->range.lower < i->range.upper)
1156 if (!(i->ranges = ranges_create(50))) return TRUE;
1157 if (!ranges_add(i->ranges, i->range))
1159 ranges_destroy(i->ranges);
1160 i->ranges = 0;
1161 return TRUE;
1165 /* now delete the invisible items from the list */
1166 while(iterator_next(i))
1168 LISTVIEW_GetItemOrigin(infoPtr, i->nItem, &Position);
1169 rcItem.left = Position.x + Origin.x;
1170 rcItem.top = Position.y + Origin.y;
1171 rcItem.right = rcItem.left + infoPtr->nItemWidth;
1172 rcItem.bottom = rcItem.top + infoPtr->nItemHeight;
1173 if (!RectVisible(hdc, &rcItem))
1174 ranges_delitem(i->ranges, i->nItem);
1176 /* the iterator should restart on the next iterator_next */
1177 TRACE("done\n");
1179 return TRUE;
1182 /******** Misc helper functions ************************************/
1184 static inline LRESULT CallWindowProcT(WNDPROC proc, HWND hwnd, UINT uMsg,
1185 WPARAM wParam, LPARAM lParam, BOOL isW)
1187 if (isW) return CallWindowProcW(proc, hwnd, uMsg, wParam, lParam);
1188 else return CallWindowProcA(proc, hwnd, uMsg, wParam, lParam);
1191 static inline BOOL is_autoarrange(LISTVIEW_INFO *infoPtr)
1193 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
1195 return ((infoPtr->dwStyle & LVS_AUTOARRANGE) || infoPtr->bAutoarrange) &&
1196 (uView == LVS_ICON || uView == LVS_SMALLICON);
1199 /******** Internal API functions ************************************/
1201 static inline COLUMN_INFO * LISTVIEW_GetColumnInfo(LISTVIEW_INFO *infoPtr, INT nSubItem)
1203 assert (nSubItem >= 0 && nSubItem < infoPtr->hdpaColumns->nItemCount);
1204 return (COLUMN_INFO *)DPA_GetPtr(infoPtr->hdpaColumns, nSubItem);
1207 static inline void LISTVIEW_GetHeaderRect(LISTVIEW_INFO *infoPtr, INT nSubItem, RECT *lprc)
1209 *lprc = LISTVIEW_GetColumnInfo(infoPtr, nSubItem)->rcHeader;
1212 static inline BOOL LISTVIEW_GetItemW(LISTVIEW_INFO *infoPtr, LPLVITEMW lpLVItem)
1214 return LISTVIEW_GetItemT(infoPtr, lpLVItem, TRUE);
1217 /* Listview invlaidation functions: use _only_ these function to invalidate */
1219 static inline BOOL is_redrawing(LISTVIEW_INFO *infoPtr)
1221 return infoPtr->bRedraw && !infoPtr->bFirstPaint;
1224 static inline void LISTVIEW_InvalidateRect(LISTVIEW_INFO *infoPtr, const RECT* rect)
1226 if(!is_redrawing(infoPtr)) return;
1227 TRACE(" invalidating rect=%s\n", debugrect(rect));
1228 InvalidateRect(infoPtr->hwndSelf, rect, TRUE);
1231 static inline void LISTVIEW_InvalidateItem(LISTVIEW_INFO *infoPtr, INT nItem)
1233 RECT rcBox;
1235 if(!is_redrawing(infoPtr)) return;
1236 LISTVIEW_GetItemBox(infoPtr, nItem, &rcBox);
1237 LISTVIEW_InvalidateRect(infoPtr, &rcBox);
1240 static inline void LISTVIEW_InvalidateSubItem(LISTVIEW_INFO *infoPtr, INT nItem, INT nSubItem)
1242 POINT Origin, Position;
1243 RECT rcBox;
1245 if(!is_redrawing(infoPtr)) return;
1246 assert ((infoPtr->dwStyle & LVS_TYPEMASK) == LVS_REPORT);
1247 LISTVIEW_GetOrigin(infoPtr, &Origin);
1248 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position);
1249 LISTVIEW_GetHeaderRect(infoPtr, nSubItem, &rcBox);
1250 rcBox.top = 0;
1251 rcBox.bottom = infoPtr->nItemHeight;
1252 OffsetRect(&rcBox, Origin.x + Position.x, Origin.y + Position.y);
1253 LISTVIEW_InvalidateRect(infoPtr, &rcBox);
1256 static inline void LISTVIEW_InvalidateList(LISTVIEW_INFO *infoPtr)
1258 LISTVIEW_InvalidateRect(infoPtr, NULL);
1261 static inline void LISTVIEW_InvalidateColumn(LISTVIEW_INFO *infoPtr, INT nColumn)
1263 RECT rcCol;
1265 if(!is_redrawing(infoPtr)) return;
1266 LISTVIEW_GetHeaderRect(infoPtr, nColumn, &rcCol);
1267 rcCol.top = infoPtr->rcList.top;
1268 rcCol.bottom = infoPtr->rcList.bottom;
1269 LISTVIEW_InvalidateRect(infoPtr, &rcCol);
1272 /***
1273 * DESCRIPTION:
1274 * Retrieves the number of items that can fit vertically in the client area.
1276 * PARAMETER(S):
1277 * [I] infoPtr : valid pointer to the listview structure
1279 * RETURN:
1280 * Number of items per row.
1282 static inline INT LISTVIEW_GetCountPerRow(LISTVIEW_INFO *infoPtr)
1284 INT nListWidth = infoPtr->rcList.right - infoPtr->rcList.left;
1286 return max(nListWidth/infoPtr->nItemWidth, 1);
1289 /***
1290 * DESCRIPTION:
1291 * Retrieves the number of items that can fit horizontally in the client
1292 * area.
1294 * PARAMETER(S):
1295 * [I] infoPtr : valid pointer to the listview structure
1297 * RETURN:
1298 * Number of items per column.
1300 static inline INT LISTVIEW_GetCountPerColumn(LISTVIEW_INFO *infoPtr)
1302 INT nListHeight = infoPtr->rcList.bottom - infoPtr->rcList.top;
1304 return max(nListHeight / infoPtr->nItemHeight, 1);
1308 /*************************************************************************
1309 * LISTVIEW_ProcessLetterKeys
1311 * Processes keyboard messages generated by pressing the letter keys
1312 * on the keyboard.
1313 * What this does is perform a case insensitive search from the
1314 * current position with the following quirks:
1315 * - If two chars or more are pressed in quick succession we search
1316 * for the corresponding string (e.g. 'abc').
1317 * - If there is a delay we wipe away the current search string and
1318 * restart with just that char.
1319 * - If the user keeps pressing the same character, whether slowly or
1320 * fast, so that the search string is entirely composed of this
1321 * character ('aaaaa' for instance), then we search for first item
1322 * that starting with that character.
1323 * - If the user types the above character in quick succession, then
1324 * we must also search for the corresponding string ('aaaaa'), and
1325 * go to that string if there is a match.
1327 * PARAMETERS
1328 * [I] hwnd : handle to the window
1329 * [I] charCode : the character code, the actual character
1330 * [I] keyData : key data
1332 * RETURNS
1334 * Zero.
1336 * BUGS
1338 * - The current implementation has a list of characters it will
1339 * accept and it ignores averything else. In particular it will
1340 * ignore accentuated characters which seems to match what
1341 * Windows does. But I'm not sure it makes sense to follow
1342 * Windows there.
1343 * - We don't sound a beep when the search fails.
1345 * SEE ALSO
1347 * TREEVIEW_ProcessLetterKeys
1349 static INT LISTVIEW_ProcessLetterKeys(LISTVIEW_INFO *infoPtr, WPARAM charCode, LPARAM keyData)
1351 INT nItem;
1352 INT endidx,idx;
1353 LVITEMW item;
1354 WCHAR buffer[MAX_PATH];
1355 DWORD lastKeyPressTimestamp = infoPtr->lastKeyPressTimestamp;
1357 /* simple parameter checking */
1358 if (!charCode || !keyData) return 0;
1360 /* only allow the valid WM_CHARs through */
1361 if (!isalnum(charCode) &&
1362 charCode != '.' && charCode != '`' && charCode != '!' &&
1363 charCode != '@' && charCode != '#' && charCode != '$' &&
1364 charCode != '%' && charCode != '^' && charCode != '&' &&
1365 charCode != '*' && charCode != '(' && charCode != ')' &&
1366 charCode != '-' && charCode != '_' && charCode != '+' &&
1367 charCode != '=' && charCode != '\\'&& charCode != ']' &&
1368 charCode != '}' && charCode != '[' && charCode != '{' &&
1369 charCode != '/' && charCode != '?' && charCode != '>' &&
1370 charCode != '<' && charCode != ',' && charCode != '~')
1371 return 0;
1373 /* if there's one item or less, there is no where to go */
1374 if (infoPtr->nItemCount <= 1) return 0;
1376 /* update the search parameters */
1377 infoPtr->lastKeyPressTimestamp = GetTickCount();
1378 if (infoPtr->lastKeyPressTimestamp - lastKeyPressTimestamp < KEY_DELAY) {
1379 if (infoPtr->nSearchParamLength < MAX_PATH)
1380 infoPtr->szSearchParam[infoPtr->nSearchParamLength++]=charCode;
1381 if (infoPtr->charCode != charCode)
1382 infoPtr->charCode = charCode = 0;
1383 } else {
1384 infoPtr->charCode=charCode;
1385 infoPtr->szSearchParam[0]=charCode;
1386 infoPtr->nSearchParamLength=1;
1387 /* Redundant with the 1 char string */
1388 charCode=0;
1391 /* and search from the current position */
1392 nItem=-1;
1393 if (infoPtr->nFocusedItem >= 0) {
1394 endidx=infoPtr->nFocusedItem;
1395 idx=endidx;
1396 /* if looking for single character match,
1397 * then we must always move forward
1399 if (infoPtr->nSearchParamLength == 1)
1400 idx++;
1401 } else {
1402 endidx=infoPtr->nItemCount;
1403 idx=0;
1405 do {
1406 if (idx == infoPtr->nItemCount) {
1407 if (endidx == infoPtr->nItemCount || endidx == 0)
1408 break;
1409 idx=0;
1412 /* get item */
1413 item.mask = LVIF_TEXT;
1414 item.iItem = idx;
1415 item.iSubItem = 0;
1416 item.pszText = buffer;
1417 item.cchTextMax = MAX_PATH;
1418 if (!LISTVIEW_GetItemW(infoPtr, &item)) return 0;
1420 /* check for a match */
1421 if (lstrncmpiW(item.pszText,infoPtr->szSearchParam,infoPtr->nSearchParamLength) == 0) {
1422 nItem=idx;
1423 break;
1424 } else if ( (charCode != 0) && (nItem == -1) && (nItem != infoPtr->nFocusedItem) &&
1425 (lstrncmpiW(item.pszText,infoPtr->szSearchParam,1) == 0) ) {
1426 /* This would work but we must keep looking for a longer match */
1427 nItem=idx;
1429 idx++;
1430 } while (idx != endidx);
1432 if (nItem != -1)
1433 LISTVIEW_KeySelection(infoPtr, nItem);
1435 return 0;
1438 /*************************************************************************
1439 * LISTVIEW_UpdateHeaderSize [Internal]
1441 * Function to resize the header control
1443 * PARAMS
1444 * [I] hwnd : handle to a window
1445 * [I] nNewScrollPos : scroll pos to set
1447 * RETURNS
1448 * None.
1450 static void LISTVIEW_UpdateHeaderSize(LISTVIEW_INFO *infoPtr, INT nNewScrollPos)
1452 RECT winRect;
1453 POINT point[2];
1455 TRACE("nNewScrollPos=%d\n", nNewScrollPos);
1457 GetWindowRect(infoPtr->hwndHeader, &winRect);
1458 point[0].x = winRect.left;
1459 point[0].y = winRect.top;
1460 point[1].x = winRect.right;
1461 point[1].y = winRect.bottom;
1463 MapWindowPoints(HWND_DESKTOP, infoPtr->hwndSelf, point, 2);
1464 point[0].x = -nNewScrollPos;
1465 point[1].x += nNewScrollPos;
1467 SetWindowPos(infoPtr->hwndHeader,0,
1468 point[0].x,point[0].y,point[1].x,point[1].y,
1469 SWP_NOZORDER | SWP_NOACTIVATE);
1472 /***
1473 * DESCRIPTION:
1474 * Update the scrollbars. This functions should be called whenever
1475 * the content, size or view changes.
1477 * PARAMETER(S):
1478 * [I] infoPtr : valid pointer to the listview structure
1480 * RETURN:
1481 * None
1483 static void LISTVIEW_UpdateScroll(LISTVIEW_INFO *infoPtr)
1485 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
1486 SCROLLINFO horzInfo, vertInfo;
1488 if ((infoPtr->dwStyle & LVS_NOSCROLL) || !is_redrawing(infoPtr)) return;
1490 ZeroMemory(&horzInfo, sizeof(SCROLLINFO));
1491 horzInfo.cbSize = sizeof(SCROLLINFO);
1492 horzInfo.nPage = infoPtr->rcList.right - infoPtr->rcList.left;
1494 ZeroMemory(&vertInfo, sizeof(SCROLLINFO));
1495 vertInfo.cbSize = sizeof(SCROLLINFO);
1496 vertInfo.nPage = infoPtr->rcList.bottom - infoPtr->rcList.top;
1498 /* for now, we'll set info.nMax to the _count_, and adjust it later */
1499 if (uView == LVS_LIST)
1501 INT nPerCol = LISTVIEW_GetCountPerColumn(infoPtr);
1502 horzInfo.nMax = (infoPtr->nItemCount + nPerCol - 1) / nPerCol;
1503 horzInfo.nPage /= infoPtr->nItemWidth;
1505 else if (uView == LVS_REPORT)
1507 horzInfo.nMax = infoPtr->nItemWidth;
1508 vertInfo.nMax = infoPtr->nItemCount;
1509 vertInfo.nPage /= infoPtr->nItemHeight;
1511 else /* LVS_ICON, or LVS_SMALLICON */
1513 RECT rcView;
1515 if (LISTVIEW_GetViewRect(infoPtr, &rcView))
1517 horzInfo.nMax = rcView.right - rcView.left;
1518 vertInfo.nMax = rcView.bottom - rcView.top;
1522 horzInfo.fMask = SIF_RANGE | SIF_PAGE;
1523 horzInfo.nMax = max(horzInfo.nMax - 1, 0);
1524 SetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &horzInfo, TRUE);
1525 TRACE("horzInfo=%s\n", debugscrollinfo(&horzInfo));
1527 vertInfo.fMask = SIF_RANGE | SIF_PAGE;
1528 vertInfo.nMax = max(vertInfo.nMax - 1, 0);
1529 SetScrollInfo(infoPtr->hwndSelf, SB_VERT, &vertInfo, TRUE);
1530 TRACE("vertInfo=%s\n", debugscrollinfo(&vertInfo));
1532 /* Update the Header Control */
1533 if (uView == LVS_REPORT)
1535 horzInfo.fMask = SIF_POS;
1536 GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &horzInfo);
1537 LISTVIEW_UpdateHeaderSize(infoPtr, horzInfo.nPos);
1542 /***
1543 * DESCRIPTION:
1544 * Shows/hides the focus rectangle.
1546 * PARAMETER(S):
1547 * [I] infoPtr : valid pointer to the listview structure
1548 * [I] fShow : TRUE to show the focus, FALSE to hide it.
1550 * RETURN:
1551 * None
1553 static void LISTVIEW_ShowFocusRect(LISTVIEW_INFO *infoPtr, BOOL fShow)
1555 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
1556 HDC hdc;
1558 TRACE("fShow=%d, nItem=%d\n", fShow, infoPtr->nFocusedItem);
1560 if (infoPtr->nFocusedItem < 0) return;
1562 /* we need some gymnastics in ICON mode to handle large items */
1563 if ( (infoPtr->dwStyle & LVS_TYPEMASK) == LVS_ICON )
1565 RECT rcBox;
1567 LISTVIEW_GetItemBox(infoPtr, infoPtr->nFocusedItem, &rcBox);
1568 if ((rcBox.bottom - rcBox.top) > infoPtr->nItemHeight)
1570 LISTVIEW_InvalidateRect(infoPtr, &rcBox);
1571 return;
1575 if (!(hdc = GetDC(infoPtr->hwndSelf))) return;
1577 /* for some reason, owner draw should work only in report mode */
1578 if ((infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && (uView == LVS_REPORT))
1580 DRAWITEMSTRUCT dis;
1581 LVITEMW item;
1583 item.iItem = infoPtr->nFocusedItem;
1584 item.iSubItem = 0;
1585 item.mask = LVIF_PARAM;
1586 if (!LISTVIEW_GetItemW(infoPtr, &item)) goto done;
1588 ZeroMemory(&dis, sizeof(dis));
1589 dis.CtlType = ODT_LISTVIEW;
1590 dis.CtlID = GetWindowLongW(infoPtr->hwndSelf, GWL_ID);
1591 dis.itemID = item.iItem;
1592 dis.itemAction = ODA_FOCUS;
1593 if (fShow) dis.itemState |= ODS_FOCUS;
1594 dis.hwndItem = infoPtr->hwndSelf;
1595 dis.hDC = hdc;
1596 LISTVIEW_GetItemBox(infoPtr, dis.itemID, &dis.rcItem);
1597 dis.itemData = item.lParam;
1599 SendMessageW(GetParent(infoPtr->hwndSelf), WM_DRAWITEM, dis.CtlID, (LPARAM)&dis);
1601 else
1603 DrawFocusRect(hdc, &infoPtr->rcFocus);
1605 done:
1606 ReleaseDC(infoPtr->hwndSelf, hdc);
1609 /***
1610 * Invalidates all visible selected items.
1612 static void LISTVIEW_InvalidateSelectedItems(LISTVIEW_INFO *infoPtr)
1614 ITERATOR i;
1616 iterator_frameditems(&i, infoPtr, &infoPtr->rcList);
1617 while(iterator_next(&i))
1619 if (LISTVIEW_GetItemState(infoPtr, i.nItem, LVIS_SELECTED))
1620 LISTVIEW_InvalidateItem(infoPtr, i.nItem);
1622 iterator_destroy(&i);
1626 /***
1627 * DESCRIPTION: [INTERNAL]
1628 * Computes an item's (left,top) corner, relative to rcView.
1629 * That is, the position has NOT been made relative to the Origin.
1630 * This is deliberate, to avoid computing the Origin over, and
1631 * over again, when this function is call in a loop. Instead,
1632 * one ca factor the computation of the Origin before the loop,
1633 * and offset the value retured by this function, on every iteration.
1635 * PARAMETER(S):
1636 * [I] infoPtr : valid pointer to the listview structure
1637 * [I] nItem : item number
1638 * [O] lpptOrig : item top, left corner
1640 * RETURN:
1641 * None.
1643 static void LISTVIEW_GetItemOrigin(LISTVIEW_INFO *infoPtr, INT nItem, LPPOINT lpptPosition)
1645 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
1647 assert(nItem >= 0 && nItem < infoPtr->nItemCount);
1649 if ((uView == LVS_SMALLICON) || (uView == LVS_ICON))
1651 lpptPosition->x = (LONG)DPA_GetPtr(infoPtr->hdpaPosX, nItem);
1652 lpptPosition->y = (LONG)DPA_GetPtr(infoPtr->hdpaPosY, nItem);
1654 else if (uView == LVS_LIST)
1656 INT nCountPerColumn = LISTVIEW_GetCountPerColumn(infoPtr);
1657 lpptPosition->x = nItem / nCountPerColumn * infoPtr->nItemWidth;
1658 lpptPosition->y = nItem % nCountPerColumn * infoPtr->nItemHeight;
1660 else /* LVS_REPORT */
1662 lpptPosition->x = REPORT_MARGINX;
1663 lpptPosition->y = nItem * infoPtr->nItemHeight;
1667 /***
1668 * DESCRIPTION: [INTERNAL]
1669 * Compute the rectangles of an item. This is to localize all
1670 * the computations in one place. If you are not interested in some
1671 * of these values, simply pass in a NULL -- the fucntion is smart
1672 * enough to compute only what's necessary. The function computes
1673 * the standard rectangles (BOUNDS, ICON, LABEL) plus a non-standard
1674 * one, the BOX rectangle. This rectangle is very cheap to compute,
1675 * and is guaranteed to contain all the other rectangles. Computing
1676 * the ICON rect is also cheap, but all the others are potentaily
1677 * expensive. This gives an easy and effective optimization when
1678 * searching (like point inclusion, or rectangle intersection):
1679 * first test against the BOX, and if TRUE, test agains the desired
1680 * rectangle.
1681 * If the function does not have all the necessary information
1682 * to computed the requested rectangles, will crash with a
1683 * failed assertion. This is done so we catch all programming
1684 * errors, given that the function is called only from our code.
1686 * We have the following 'special' meanings for a few fields:
1687 * * If LVIS_FOCUSED is set, we assume the item has the focus
1688 * This is important in ICON mode, where it might get a larger
1689 * then usual rectange
1691 * Please note that subitem support works only in REPORT mode.
1693 * PARAMETER(S):
1694 * [I] infoPtr : valid pointer to the listview structure
1695 * [I] lpLVItem : item to compute the measures for
1696 * [O] lprcBox : ptr to Box rectangle
1697 * The internal LVIR_BOX rectangle
1698 * [0] lprcState : ptr to State icon rectangle
1699 * The internal LVIR_STATE rectangle
1700 * [O] lprcIcon : ptr to Icon rectangle
1701 * Same as LVM_GETITEMRECT with LVIR_ICON
1702 * [O] lprcLabel : ptr to Label rectangle
1703 * Same as LVM_GETITEMRECT with LVIR_LABEL
1705 * RETURN:
1706 * None.
1708 static void LISTVIEW_GetItemMetrics(LISTVIEW_INFO *infoPtr, const LVITEMW *lpLVItem,
1709 LPRECT lprcBox, LPRECT lprcState,
1710 LPRECT lprcIcon, LPRECT lprcLabel)
1712 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
1713 BOOL doState = FALSE, doIcon = FALSE, doLabel = FALSE, oversizedBox = FALSE;
1714 RECT Box, State, Icon, Label;
1715 COLUMN_INFO *lpColumnInfo = NULL;
1717 TRACE("(lpLVItem=%s)\n", debuglvitem_t(lpLVItem, TRUE));
1719 /* Be smart and try to figure out the minimum we have to do */
1720 if (lpLVItem->iSubItem) assert(uView == LVS_REPORT);
1721 if (uView == LVS_ICON && (lprcBox || lprcLabel))
1723 assert((lpLVItem->mask & LVIF_STATE) && (lpLVItem->stateMask & LVIS_FOCUSED));
1724 if (lpLVItem->state & LVIS_FOCUSED) oversizedBox = doLabel = TRUE;
1726 if (lprcLabel) doLabel = TRUE;
1727 if (doLabel || lprcIcon) doIcon = TRUE;
1728 if (doIcon || lprcState) doState = TRUE;
1730 /************************************************************/
1731 /* compute the box rectangle (it should be cheap to do) */
1732 /************************************************************/
1733 if (lpLVItem->iSubItem || uView == LVS_REPORT)
1734 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, lpLVItem->iSubItem);
1736 if (lpLVItem->iSubItem)
1738 Box = lpColumnInfo->rcHeader;
1740 else
1742 Box.left = 0;
1743 Box.right = infoPtr->nItemWidth;
1745 Box.top = 0;
1746 Box.bottom = infoPtr->nItemHeight;
1748 /************************************************************/
1749 /* compute STATEICON bounding box */
1750 /************************************************************/
1751 if (doState)
1753 if (uView == LVS_ICON)
1755 State.left = Box.left - infoPtr->iconStateSize.cx - 2;
1756 if (infoPtr->himlNormal)
1757 State.left += (infoPtr->nItemWidth - infoPtr->iconSize.cx) / 2;
1758 State.top = Box.top + infoPtr->iconSize.cy - infoPtr->iconStateSize.cy + 4;
1760 else
1762 /* we need the ident in report mode, if we don't have it, we fail */
1763 State.left = Box.left;
1764 if (uView == LVS_REPORT)
1766 State.left += REPORT_MARGINX;
1767 if (lpLVItem->iSubItem == 0)
1769 assert(lpLVItem->mask & LVIF_INDENT);
1770 State.left += infoPtr->iconSize.cx * lpLVItem->iIndent;
1773 State.top = Box.top;
1775 State.right = State.left;
1776 State.bottom = State.top;
1777 if (infoPtr->himlState && lpLVItem->iSubItem == 0)
1779 State.right += infoPtr->iconStateSize.cx;
1780 State.bottom += infoPtr->iconStateSize.cy;
1782 if (lprcState) *lprcState = State;
1783 TRACE(" - state=%s\n", debugrect(&State));
1786 /************************************************************/
1787 /* compute ICON bounding box (ala LVM_GETITEMRECT) */
1788 /************************************************************/
1789 if (doIcon)
1791 if (uView == LVS_ICON)
1793 Icon.left = Box.left;
1794 if (infoPtr->himlNormal)
1795 Icon.left += (infoPtr->nItemWidth - infoPtr->iconSize.cx) / 2;
1796 Icon.top = Box.top + ICON_TOP_PADDING;
1797 Icon.right = Icon.left;
1798 Icon.bottom = Icon.top;
1799 if (infoPtr->himlNormal)
1801 Icon.right += infoPtr->iconSize.cx;
1802 Icon.bottom += infoPtr->iconSize.cy;
1805 else /* LVS_SMALLICON, LVS_LIST or LVS_REPORT */
1807 Icon.left = State.right;
1808 if (!IsRectEmpty(&State)) Icon.left += IMAGE_PADDING;
1809 Icon.top = Box.top;
1810 Icon.right = Icon.left;
1811 if (infoPtr->himlSmall && (!lpColumnInfo || lpLVItem->iSubItem == 0 || (lpColumnInfo->fmt & LVCFMT_IMAGE)))
1812 Icon.right += infoPtr->iconSize.cx;
1813 Icon.bottom = Icon.top + infoPtr->nItemHeight;
1815 if(lprcIcon) *lprcIcon = Icon;
1816 TRACE(" - icon=%s\n", debugrect(&Icon));
1819 /************************************************************/
1820 /* compute LABEL bounding box (ala LVM_GETITEMRECT) */
1821 /************************************************************/
1822 if (doLabel)
1824 SIZE labelSize = { 0, 0 };
1826 /* calculate how far to the right can the label strech */
1827 Label.right = Box.right;
1828 if (uView == LVS_REPORT)
1830 if (lpLVItem->iSubItem == 0) Label = lpColumnInfo->rcHeader;
1831 Label.right -= REPORT_MARGINX;
1834 if (lpLVItem->iSubItem || ((infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && uView == LVS_REPORT))
1836 labelSize.cx = infoPtr->nItemWidth;
1837 labelSize.cy = infoPtr->nItemHeight;
1838 goto calc_label;
1841 /* we need the text in non owner draw mode */
1842 assert(lpLVItem->mask & LVIF_TEXT);
1843 if (is_textT(lpLVItem->pszText, TRUE))
1845 HFONT hFont = infoPtr->hFont ? infoPtr->hFont : infoPtr->hDefaultFont;
1846 HDC hdc = GetDC(infoPtr->hwndSelf);
1847 HFONT hOldFont = SelectObject(hdc, hFont);
1848 UINT uFormat;
1849 RECT rcText;
1851 /* compute rough rectangle where the label will go */
1852 SetRectEmpty(&rcText);
1853 rcText.right = infoPtr->nItemWidth - TRAILING_PADDING;
1854 rcText.bottom = infoPtr->nItemHeight;
1855 if (uView == LVS_ICON)
1856 rcText.bottom -= ICON_TOP_PADDING + infoPtr->iconSize.cy + ICON_BOTTOM_PADDING;
1858 /* now figure out the flags */
1859 if (uView == LVS_ICON)
1860 uFormat = oversizedBox ? LV_FL_DT_FLAGS : LV_ML_DT_FLAGS;
1861 else
1862 uFormat = LV_SL_DT_FLAGS;
1864 DrawTextW (hdc, lpLVItem->pszText, -1, &rcText, uFormat | DT_CALCRECT);
1866 labelSize.cx = min(rcText.right - rcText.left + TRAILING_PADDING, infoPtr->nItemWidth);
1867 labelSize.cy = rcText.bottom - rcText.top;
1869 SelectObject(hdc, hOldFont);
1870 ReleaseDC(infoPtr->hwndSelf, hdc);
1873 calc_label:
1874 if (uView == LVS_ICON)
1876 Label.left = Box.left + (infoPtr->nItemWidth - labelSize.cx) / 2;
1877 Label.top = Box.top + ICON_TOP_PADDING_HITABLE +
1878 infoPtr->iconSize.cy + ICON_BOTTOM_PADDING;
1879 Label.right = Label.left + labelSize.cx;
1880 Label.bottom = Label.top + infoPtr->nItemHeight;
1881 if (!oversizedBox && labelSize.cy > infoPtr->ntmHeight)
1883 labelSize.cy = min(Box.bottom - Label.top, labelSize.cy);
1884 labelSize.cy /= infoPtr->ntmHeight;
1885 labelSize.cy = max(labelSize.cy, 1);
1886 labelSize.cy *= infoPtr->ntmHeight;
1888 Label.bottom = Label.top + labelSize.cy + HEIGHT_PADDING;
1890 else /* LVS_SMALLICON, LVS_LIST or LVS_REPORT */
1892 Label.left = Icon.right;
1893 if (!IsRectEmpty(&Icon) || !IsRectEmpty(&State)) Label.left += IMAGE_PADDING;
1894 Label.top = Box.top;
1895 Label.right = min(Label.left + labelSize.cx, Label.right);
1896 Label.bottom = Label.top + infoPtr->nItemHeight;
1899 if (lprcLabel) *lprcLabel = Label;
1900 TRACE(" - label=%s\n", debugrect(&Label));
1903 /* Fix the Box if necessary */
1904 if (lprcBox)
1906 if (oversizedBox) UnionRect(lprcBox, &Box, &Label);
1907 else *lprcBox = Box;
1909 TRACE(" - box=%s\n", debugrect(&Box));
1912 /***
1913 * DESCRIPTION: [INTERNAL]
1915 * PARAMETER(S):
1916 * [I] infoPtr : valid pointer to the listview structure
1917 * [I] nItem : item number
1918 * [O] lprcBox : ptr to Box rectangle
1920 * RETURN:
1921 * None.
1923 static void LISTVIEW_GetItemBox(LISTVIEW_INFO *infoPtr, INT nItem, LPRECT lprcBox)
1925 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
1926 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
1927 POINT Position, Origin;
1928 LVITEMW lvItem;
1930 LISTVIEW_GetOrigin(infoPtr, &Origin);
1931 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position);
1933 /* Be smart and try to figure out the minimum we have to do */
1934 lvItem.mask = 0;
1935 if (uView == LVS_ICON && infoPtr->bFocus && LISTVIEW_GetItemState(infoPtr, nItem, LVIS_FOCUSED))
1936 lvItem.mask |= LVIF_TEXT;
1937 lvItem.iItem = nItem;
1938 lvItem.iSubItem = 0;
1939 lvItem.pszText = szDispText;
1940 lvItem.cchTextMax = DISP_TEXT_SIZE;
1941 if (lvItem.mask) LISTVIEW_GetItemW(infoPtr, &lvItem);
1942 if (uView == LVS_ICON)
1944 lvItem.mask |= LVIF_STATE;
1945 lvItem.stateMask = LVIS_FOCUSED;
1946 lvItem.state = (lvItem.mask & LVIF_TEXT ? LVIS_FOCUSED : 0);
1948 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, lprcBox, 0, 0, 0);
1950 OffsetRect(lprcBox, Position.x + Origin.x, Position.y + Origin.y);
1954 /***
1955 * DESCRIPTION:
1956 * Returns the current icon position, and advances it along the top.
1957 * The returned position is not offset by Origin.
1959 * PARAMETER(S):
1960 * [I] infoPtr : valid pointer to the listview structure
1961 * [O] lpPos : will get the current icon position
1963 * RETURN:
1964 * None
1966 static void LISTVIEW_NextIconPosTop(LISTVIEW_INFO *infoPtr, LPPOINT lpPos)
1968 INT nListWidth = infoPtr->rcList.right - infoPtr->rcList.left;
1970 *lpPos = infoPtr->currIconPos;
1972 infoPtr->currIconPos.x += infoPtr->nItemWidth;
1973 if (infoPtr->currIconPos.x + infoPtr->nItemWidth <= nListWidth) return;
1975 infoPtr->currIconPos.x = 0;
1976 infoPtr->currIconPos.y += infoPtr->nItemHeight;
1980 /***
1981 * DESCRIPTION:
1982 * Returns the current icon position, and advances it down the left edge.
1983 * The returned position is not offset by Origin.
1985 * PARAMETER(S):
1986 * [I] infoPtr : valid pointer to the listview structure
1987 * [O] lpPos : will get the current icon position
1989 * RETURN:
1990 * None
1992 static void LISTVIEW_NextIconPosLeft(LISTVIEW_INFO *infoPtr, LPPOINT lpPos)
1994 INT nListHeight = infoPtr->rcList.bottom - infoPtr->rcList.top;
1996 *lpPos = infoPtr->currIconPos;
1998 infoPtr->currIconPos.y += infoPtr->nItemHeight;
1999 if (infoPtr->currIconPos.y + infoPtr->nItemHeight <= nListHeight) return;
2001 infoPtr->currIconPos.x += infoPtr->nItemWidth;
2002 infoPtr->currIconPos.y = 0;
2006 /***
2007 * DESCRIPTION:
2008 * Moves an icon to the specified position.
2009 * It takes care of invalidating the item, etc.
2011 * PARAMETER(S):
2012 * [I] infoPtr : valid pointer to the listview structure
2013 * [I] nItem : the item to move
2014 * [I] lpPos : the new icon position
2015 * [I] isNew : flags the item as being new
2017 * RETURN:
2018 * Success: TRUE
2019 * Failure: FALSE
2021 static BOOL LISTVIEW_MoveIconTo(LISTVIEW_INFO *infoPtr, INT nItem, const POINT *lppt, BOOL isNew)
2023 POINT old;
2025 if (!isNew)
2027 old.x = (LONG)DPA_GetPtr(infoPtr->hdpaPosX, nItem);
2028 old.y = (LONG)DPA_GetPtr(infoPtr->hdpaPosY, nItem);
2030 if (lppt->x == old.x && lppt->y == old.y) return TRUE;
2031 LISTVIEW_InvalidateItem(infoPtr, nItem);
2034 /* Allocating a POINTER for every item is too resource intensive,
2035 * so we'll keep the (x,y) in different arrays */
2036 if (!DPA_SetPtr(infoPtr->hdpaPosX, nItem, (void *)lppt->x)) return FALSE;
2037 if (!DPA_SetPtr(infoPtr->hdpaPosY, nItem, (void *)lppt->y)) return FALSE;
2039 LISTVIEW_InvalidateItem(infoPtr, nItem);
2041 return TRUE;
2044 /***
2045 * DESCRIPTION:
2046 * Arranges listview items in icon display mode.
2048 * PARAMETER(S):
2049 * [I] infoPtr : valid pointer to the listview structure
2050 * [I] nAlignCode : alignment code
2052 * RETURN:
2053 * SUCCESS : TRUE
2054 * FAILURE : FALSE
2056 static BOOL LISTVIEW_Arrange(LISTVIEW_INFO *infoPtr, INT nAlignCode)
2058 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
2059 void (*next_pos)(LISTVIEW_INFO *, LPPOINT);
2060 POINT pos;
2061 INT i;
2063 if (uView != LVS_ICON && uView != LVS_SMALLICON) return FALSE;
2065 TRACE("nAlignCode=%d\n", nAlignCode);
2067 if (nAlignCode == LVA_DEFAULT)
2069 if (infoPtr->dwStyle & LVS_ALIGNLEFT) nAlignCode = LVA_ALIGNLEFT;
2070 else nAlignCode = LVA_ALIGNTOP;
2073 switch (nAlignCode)
2075 case LVA_ALIGNLEFT: next_pos = LISTVIEW_NextIconPosLeft; break;
2076 case LVA_ALIGNTOP: next_pos = LISTVIEW_NextIconPosTop; break;
2077 case LVA_SNAPTOGRID: next_pos = LISTVIEW_NextIconPosTop; break; /* FIXME */
2078 default: return FALSE;
2081 infoPtr->bAutoarrange = TRUE;
2082 infoPtr->currIconPos.x = infoPtr->currIconPos.y = 0;
2083 for (i = 0; i < infoPtr->nItemCount; i++)
2085 next_pos(infoPtr, &pos);
2086 LISTVIEW_MoveIconTo(infoPtr, i, &pos, FALSE);
2089 return TRUE;
2092 /***
2093 * DESCRIPTION:
2094 * Retrieves the bounding rectangle of all the items, not offset by Origin.
2096 * PARAMETER(S):
2097 * [I] infoPtr : valid pointer to the listview structure
2098 * [O] lprcView : bounding rectangle
2100 * RETURN:
2101 * SUCCESS : TRUE
2102 * FAILURE : FALSE
2104 static void LISTVIEW_GetAreaRect(LISTVIEW_INFO *infoPtr, LPRECT lprcView)
2106 INT i, x, y;
2108 SetRectEmpty(lprcView);
2110 switch (infoPtr->dwStyle & LVS_TYPEMASK)
2112 case LVS_ICON:
2113 case LVS_SMALLICON:
2114 for (i = 0; i < infoPtr->nItemCount; i++)
2116 x = (LONG)DPA_GetPtr(infoPtr->hdpaPosX, i);
2117 y = (LONG)DPA_GetPtr(infoPtr->hdpaPosX, i);
2118 lprcView->right = max(lprcView->right, x);
2119 lprcView->bottom = max(lprcView->bottom, y);
2121 if (infoPtr->nItemCount > 0)
2123 lprcView->right += infoPtr->nItemWidth;
2124 lprcView->bottom += infoPtr->nItemHeight;
2126 break;
2128 case LVS_LIST:
2129 y = LISTVIEW_GetCountPerColumn(infoPtr);
2130 x = infoPtr->nItemCount / y;
2131 if (infoPtr->nItemCount % y) x++;
2132 lprcView->right = x * infoPtr->nItemWidth;
2133 lprcView->bottom = y * infoPtr->nItemHeight;
2134 break;
2136 case LVS_REPORT:
2137 lprcView->right = infoPtr->nItemWidth;
2138 lprcView->bottom = infoPtr->nItemCount * infoPtr->nItemHeight;
2139 break;
2143 /***
2144 * DESCRIPTION:
2145 * Retrieves the bounding rectangle of all the items.
2147 * PARAMETER(S):
2148 * [I] infoPtr : valid pointer to the listview structure
2149 * [O] lprcView : bounding rectangle
2151 * RETURN:
2152 * SUCCESS : TRUE
2153 * FAILURE : FALSE
2155 static BOOL LISTVIEW_GetViewRect(LISTVIEW_INFO *infoPtr, LPRECT lprcView)
2157 POINT ptOrigin;
2159 TRACE("(lprcView=%p)\n", lprcView);
2161 if (!lprcView) return FALSE;
2163 LISTVIEW_GetOrigin(infoPtr, &ptOrigin);
2164 LISTVIEW_GetAreaRect(infoPtr, lprcView);
2165 OffsetRect(lprcView, ptOrigin.x, ptOrigin.y);
2167 TRACE("lprcView=%s\n", debugrect(lprcView));
2169 return TRUE;
2172 /***
2173 * DESCRIPTION:
2174 * Retrieves the subitem pointer associated with the subitem index.
2176 * PARAMETER(S):
2177 * [I] hdpaSubItems : DPA handle for a specific item
2178 * [I] nSubItem : index of subitem
2180 * RETURN:
2181 * SUCCESS : subitem pointer
2182 * FAILURE : NULL
2184 static SUBITEM_INFO* LISTVIEW_GetSubItemPtr(HDPA hdpaSubItems, INT nSubItem)
2186 SUBITEM_INFO *lpSubItem;
2187 INT i;
2189 /* we should binary search here if need be */
2190 for (i = 1; i < hdpaSubItems->nItemCount; i++)
2192 lpSubItem = (SUBITEM_INFO *)DPA_GetPtr(hdpaSubItems, i);
2193 if (lpSubItem->iSubItem == nSubItem)
2194 return lpSubItem;
2197 return NULL;
2201 /***
2202 * DESCRIPTION:
2203 * Caclulates the desired item width.
2205 * PARAMETER(S):
2206 * [I] infoPtr : valid pointer to the listview structure
2208 * RETURN:
2209 * The desired item width.
2211 static INT LISTVIEW_CalculateItemWidth(LISTVIEW_INFO *infoPtr)
2213 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
2214 INT nItemWidth = 0;
2216 TRACE("uView=%d\n", uView);
2218 if (uView == LVS_ICON)
2219 nItemWidth = infoPtr->iconSpacing.cx;
2220 else if (uView == LVS_REPORT)
2222 RECT rcHeader;
2224 if (infoPtr->hdpaColumns->nItemCount > 0)
2226 LISTVIEW_GetHeaderRect(infoPtr, infoPtr->hdpaColumns->nItemCount - 1, &rcHeader);
2227 nItemWidth = rcHeader.right;
2230 else /* LVS_SMALLICON, or LVS_LIST */
2232 INT i;
2234 for (i = 0; i < infoPtr->nItemCount; i++)
2235 nItemWidth = max(LISTVIEW_GetLabelWidth(infoPtr, i), nItemWidth);
2237 if (infoPtr->himlSmall) nItemWidth += infoPtr->iconSize.cx + IMAGE_PADDING;
2238 if (infoPtr->himlState) nItemWidth += infoPtr->iconStateSize.cx + IMAGE_PADDING;
2240 nItemWidth = max(DEFAULT_COLUMN_WIDTH, nItemWidth + WIDTH_PADDING);
2243 return max(nItemWidth, 1);
2246 /***
2247 * DESCRIPTION:
2248 * Caclulates the desired item height.
2250 * PARAMETER(S):
2251 * [I] infoPtr : valid pointer to the listview structure
2253 * RETURN:
2254 * The desired item height.
2256 static INT LISTVIEW_CalculateItemHeight(LISTVIEW_INFO *infoPtr)
2258 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
2259 INT nItemHeight;
2261 TRACE("uView=%d\n", uView);
2263 if (uView == LVS_ICON)
2264 nItemHeight = infoPtr->iconSpacing.cy;
2265 else
2267 nItemHeight = infoPtr->ntmHeight;
2268 if (infoPtr->himlState)
2269 nItemHeight = max(nItemHeight, infoPtr->iconStateSize.cy);
2270 if (infoPtr->himlSmall)
2271 nItemHeight = max(nItemHeight, infoPtr->iconSize.cy);
2272 if (infoPtr->himlState || infoPtr->himlSmall)
2273 nItemHeight += HEIGHT_PADDING;
2276 return max(nItemHeight, 1);
2279 /***
2280 * DESCRIPTION:
2281 * Updates the width, and height of an item.
2283 * PARAMETER(S):
2284 * [I] infoPtr : valid pointer to the listview structure
2286 * RETURN:
2287 * None.
2289 static inline void LISTVIEW_UpdateItemSize(LISTVIEW_INFO *infoPtr)
2291 infoPtr->nItemWidth = LISTVIEW_CalculateItemWidth(infoPtr);
2292 infoPtr->nItemHeight = LISTVIEW_CalculateItemHeight(infoPtr);
2296 /***
2297 * DESCRIPTION:
2298 * Retrieves and saves important text metrics info for the current
2299 * Listview font.
2301 * PARAMETER(S):
2302 * [I] infoPtr : valid pointer to the listview structure
2305 static void LISTVIEW_SaveTextMetrics(LISTVIEW_INFO *infoPtr)
2307 HDC hdc = GetDC(infoPtr->hwndSelf);
2308 HFONT hFont = infoPtr->hFont ? infoPtr->hFont : infoPtr->hDefaultFont;
2309 HFONT hOldFont = SelectObject(hdc, hFont);
2310 TEXTMETRICW tm;
2312 if (GetTextMetricsW(hdc, &tm))
2314 infoPtr->ntmHeight = tm.tmHeight;
2315 infoPtr->ntmAveCharWidth = tm.tmAveCharWidth;
2317 SelectObject(hdc, hOldFont);
2318 ReleaseDC(infoPtr->hwndSelf, hdc);
2320 TRACE("tmHeight=%d\n", infoPtr->ntmHeight);
2323 /***
2324 * DESCRIPTION:
2325 * A compare function for ranges
2327 * PARAMETER(S)
2328 * [I] range1 : pointer to range 1;
2329 * [I] range2 : pointer to range 2;
2330 * [I] flags : flags
2332 * RETURNS:
2333 * > 0 : if range 1 > range 2
2334 * < 0 : if range 2 > range 1
2335 * = 0 : if range intersects range 2
2337 static INT CALLBACK ranges_cmp(LPVOID range1, LPVOID range2, LPARAM flags)
2339 INT cmp;
2341 if (((RANGE*)range1)->upper <= ((RANGE*)range2)->lower)
2342 cmp = -1;
2343 else if (((RANGE*)range2)->upper <= ((RANGE*)range1)->lower)
2344 cmp = 1;
2345 else
2346 cmp = 0;
2348 TRACE("range1=%s, range2=%s, cmp=%d\n", debugrange((RANGE*)range1), debugrange((RANGE*)range2), cmp);
2350 return cmp;
2353 #if DEBUG_RANGES
2354 #define ranges_check(ranges, desc) ranges_assert(ranges, desc, __FUNCTION__, __LINE__)
2355 #else
2356 #define ranges_check(ranges, desc) do { } while(0)
2357 #endif
2359 static void ranges_assert(RANGES ranges, LPCSTR desc, const char *func, int line)
2361 INT i;
2362 RANGE *prev, *curr;
2364 TRACE("*** Checking %s:%d:%s ***\n", func, line, desc);
2365 assert (ranges);
2366 assert (ranges->hdpa->nItemCount >= 0);
2367 ranges_dump(ranges);
2368 prev = (RANGE *)DPA_GetPtr(ranges->hdpa, 0);
2369 if (ranges->hdpa->nItemCount > 0)
2370 assert (prev->lower >= 0 && prev->lower < prev->upper);
2371 for (i = 1; i < ranges->hdpa->nItemCount; i++)
2373 curr = (RANGE *)DPA_GetPtr(ranges->hdpa, i);
2374 assert (prev->upper <= curr->lower);
2375 assert (curr->lower < curr->upper);
2376 prev = curr;
2378 TRACE("--- Done checking---\n");
2381 static RANGES ranges_create(int count)
2383 RANGES ranges = (RANGES)COMCTL32_Alloc(sizeof(struct tagRANGES));
2384 if (!ranges) return NULL;
2385 ranges->hdpa = DPA_Create(count);
2386 if (ranges->hdpa) return ranges;
2387 COMCTL32_Free(ranges);
2388 return NULL;
2391 static void ranges_clear(RANGES ranges)
2393 INT i;
2395 for(i = 0; i < ranges->hdpa->nItemCount; i++)
2396 COMCTL32_Free(DPA_GetPtr(ranges->hdpa, i));
2397 DPA_DeleteAllPtrs(ranges->hdpa);
2401 static void ranges_destroy(RANGES ranges)
2403 ranges_clear(ranges);
2404 DPA_Destroy(ranges->hdpa);
2405 COMCTL32_Free(ranges);
2408 static RANGES ranges_clone(RANGES ranges)
2410 RANGES clone;
2411 INT i;
2413 if (!(clone = ranges_create(ranges->hdpa->nItemCount))) goto fail;
2415 for (i = 0; i < ranges->hdpa->nItemCount; i++)
2417 RANGE *newrng = (RANGE *)COMCTL32_Alloc(sizeof(RANGE));
2418 if (!newrng) goto fail;
2419 *newrng = *((RANGE*)DPA_GetPtr(ranges->hdpa, i));
2420 DPA_SetPtr(clone->hdpa, i, newrng);
2422 return clone;
2424 fail:
2425 TRACE ("clone failed\n");
2426 if (clone) ranges_destroy(clone);
2427 return NULL;
2430 static RANGES ranges_diff(RANGES ranges, RANGES sub)
2432 INT i;
2434 for (i = 0; i < sub->hdpa->nItemCount; i++)
2435 ranges_del(ranges, *((RANGE *)DPA_GetPtr(sub->hdpa, i)));
2437 return ranges;
2440 static void ranges_dump(RANGES ranges)
2442 INT i;
2444 for (i = 0; i < ranges->hdpa->nItemCount; i++)
2445 TRACE(" %s\n", debugrange(DPA_GetPtr(ranges->hdpa, i)));
2448 static inline BOOL ranges_contain(RANGES ranges, INT nItem)
2450 RANGE srchrng = { nItem, nItem + 1 };
2452 TRACE("(nItem=%d)\n", nItem);
2453 ranges_check(ranges, "before contain");
2454 return DPA_Search(ranges->hdpa, &srchrng, 0, ranges_cmp, 0, DPAS_SORTED) != -1;
2457 static INT ranges_itemcount(RANGES ranges)
2459 INT i, count = 0;
2461 for (i = 0; i < ranges->hdpa->nItemCount; i++)
2463 RANGE *sel = DPA_GetPtr(ranges->hdpa, i);
2464 count += sel->upper - sel->lower;
2467 return count;
2470 static BOOL ranges_shift(RANGES ranges, INT nItem, INT delta, INT nUpper)
2472 RANGE srchrng = { nItem, nItem + 1 }, *chkrng;
2473 INT index;
2475 index = DPA_Search(ranges->hdpa, &srchrng, 0, ranges_cmp, 0, DPAS_SORTED | DPAS_INSERTAFTER);
2476 if (index == -1) return TRUE;
2478 for (; index < ranges->hdpa->nItemCount; index++)
2480 chkrng = DPA_GetPtr(ranges->hdpa, index);
2481 if (chkrng->lower >= nItem)
2482 chkrng->lower = max(min(chkrng->lower + delta, nUpper - 1), 0);
2483 if (chkrng->upper > nItem)
2484 chkrng->upper = max(min(chkrng->upper + delta, nUpper), 0);
2486 return TRUE;
2489 static BOOL ranges_add(RANGES ranges, RANGE range)
2491 RANGE srchrgn;
2492 INT index;
2494 TRACE("(%s)\n", debugrange(&range));
2495 ranges_check(ranges, "before add");
2497 /* try find overlapping regions first */
2498 srchrgn.lower = range.lower - 1;
2499 srchrgn.upper = range.upper + 1;
2500 index = DPA_Search(ranges->hdpa, &srchrgn, 0, ranges_cmp, 0, DPAS_SORTED);
2502 if (index == -1)
2504 RANGE *newrgn;
2506 TRACE("Adding new range\n");
2508 /* create the brand new range to insert */
2509 newrgn = (RANGE *)COMCTL32_Alloc(sizeof(RANGE));
2510 if(!newrgn) goto fail;
2511 *newrgn = range;
2513 /* figure out where to insert it */
2514 index = DPA_Search(ranges->hdpa, newrgn, 0, ranges_cmp, 0, DPAS_SORTED | DPAS_INSERTAFTER);
2515 TRACE("index=%d\n", index);
2516 if (index == -1) index = 0;
2518 /* and get it over with */
2519 if (DPA_InsertPtr(ranges->hdpa, index, newrgn) == -1)
2521 COMCTL32_Free(newrgn);
2522 goto fail;
2525 else
2527 RANGE *chkrgn, *mrgrgn;
2528 INT fromindex, mergeindex;
2530 chkrgn = DPA_GetPtr(ranges->hdpa, index);
2531 TRACE("Merge with %s @%d\n", debugrange(chkrgn), index);
2533 chkrgn->lower = min(range.lower, chkrgn->lower);
2534 chkrgn->upper = max(range.upper, chkrgn->upper);
2536 TRACE("New range %s @%d\n", debugrange(chkrgn), index);
2538 /* merge now common anges */
2539 fromindex = 0;
2540 srchrgn.lower = chkrgn->lower - 1;
2541 srchrgn.upper = chkrgn->upper + 1;
2545 mergeindex = DPA_Search(ranges->hdpa, &srchrgn, fromindex, ranges_cmp, 0, 0);
2546 if (mergeindex == -1) break;
2547 if (mergeindex == index)
2549 fromindex = index + 1;
2550 continue;
2553 TRACE("Merge with index %i\n", mergeindex);
2555 mrgrgn = DPA_GetPtr(ranges->hdpa, mergeindex);
2556 chkrgn->lower = min(chkrgn->lower, mrgrgn->lower);
2557 chkrgn->upper = max(chkrgn->upper, mrgrgn->upper);
2558 COMCTL32_Free(mrgrgn);
2559 DPA_DeletePtr(ranges->hdpa, mergeindex);
2560 if (mergeindex < index) index --;
2561 } while(1);
2564 ranges_check(ranges, "after add");
2565 return TRUE;
2567 fail:
2568 ranges_check(ranges, "failed add");
2569 return FALSE;
2572 static BOOL ranges_del(RANGES ranges, RANGE range)
2574 RANGE *chkrgn;
2575 INT index;
2577 TRACE("(%s)\n", debugrange(&range));
2578 ranges_check(ranges, "before del");
2580 /* we don't use DPAS_SORTED here, since we need *
2581 * to find the first overlapping range */
2582 index = DPA_Search(ranges->hdpa, &range, 0, ranges_cmp, 0, 0);
2583 while(index != -1)
2585 chkrgn = DPA_GetPtr(ranges->hdpa, index);
2587 TRACE("Matches range %s @%d\n", debugrange(chkrgn), index);
2589 /* case 1: Same range */
2590 if ( (chkrgn->upper == range.upper) &&
2591 (chkrgn->lower == range.lower) )
2593 DPA_DeletePtr(ranges->hdpa, index);
2594 break;
2596 /* case 2: engulf */
2597 else if ( (chkrgn->upper <= range.upper) &&
2598 (chkrgn->lower >= range.lower) )
2600 DPA_DeletePtr(ranges->hdpa, index);
2602 /* case 3: overlap upper */
2603 else if ( (chkrgn->upper <= range.upper) &&
2604 (chkrgn->lower < range.lower) )
2606 chkrgn->upper = range.lower;
2608 /* case 4: overlap lower */
2609 else if ( (chkrgn->upper > range.upper) &&
2610 (chkrgn->lower >= range.lower) )
2612 chkrgn->lower = range.upper;
2613 break;
2615 /* case 5: fully internal */
2616 else
2618 RANGE tmprgn = *chkrgn, *newrgn;
2620 if (!(newrgn = (RANGE *)COMCTL32_Alloc(sizeof(RANGE)))) goto fail;
2621 newrgn->lower = chkrgn->lower;
2622 newrgn->upper = range.lower;
2623 chkrgn->lower = range.upper;
2624 if (DPA_InsertPtr(ranges->hdpa, index, newrgn) == -1)
2626 COMCTL32_Free(newrgn);
2627 goto fail;
2629 chkrgn = &tmprgn;
2630 break;
2633 index = DPA_Search(ranges->hdpa, &range, index, ranges_cmp, 0, 0);
2636 ranges_check(ranges, "after del");
2637 return TRUE;
2639 fail:
2640 ranges_check(ranges, "failed del");
2641 return FALSE;
2644 /***
2645 * DESCRIPTION:
2646 * Removes all selection ranges
2648 * Parameters(s):
2649 * [I] infoPtr : valid pointer to the listview structure
2650 * [I] toSkip : item range to skip removing the selection
2652 * RETURNS:
2653 * SUCCESS : TRUE
2654 * FAILURE : TRUE
2656 static BOOL LISTVIEW_DeselectAllSkipItems(LISTVIEW_INFO *infoPtr, RANGES toSkip)
2658 LVITEMW lvItem;
2659 ITERATOR i;
2660 RANGES clone;
2662 TRACE("()\n");
2664 lvItem.state = 0;
2665 lvItem.stateMask = LVIS_SELECTED;
2667 /* need to clone the DPA because callbacks can change it */
2668 if (!(clone = ranges_clone(infoPtr->selectionRanges))) return FALSE;
2669 iterator_rangesitems(&i, ranges_diff(clone, toSkip));
2670 while(iterator_next(&i))
2671 LISTVIEW_SetItemState(infoPtr, i.nItem, &lvItem);
2672 /* note that the iterator destructor will free the cloned range */
2673 iterator_destroy(&i);
2675 return TRUE;
2678 static inline BOOL LISTVIEW_DeselectAllSkipItem(LISTVIEW_INFO *infoPtr, INT nItem)
2680 RANGES toSkip;
2682 if (!(toSkip = ranges_create(1))) return FALSE;
2683 if (nItem != -1) ranges_additem(toSkip, nItem);
2684 LISTVIEW_DeselectAllSkipItems(infoPtr, toSkip);
2685 ranges_destroy(toSkip);
2686 return TRUE;
2689 static inline BOOL LISTVIEW_DeselectAll(LISTVIEW_INFO *infoPtr)
2691 return LISTVIEW_DeselectAllSkipItem(infoPtr, -1);
2694 /***
2695 * DESCRIPTION:
2696 * Retrieves the number of items that are marked as selected.
2698 * PARAMETER(S):
2699 * [I] infoPtr : valid pointer to the listview structure
2701 * RETURN:
2702 * Number of items selected.
2704 static INT LISTVIEW_GetSelectedCount(LISTVIEW_INFO *infoPtr)
2706 INT nSelectedCount = 0;
2708 if (infoPtr->uCallbackMask & LVIS_SELECTED)
2710 INT i;
2711 for (i = 0; i < infoPtr->nItemCount; i++)
2713 if (LISTVIEW_GetItemState(infoPtr, i, LVIS_SELECTED))
2714 nSelectedCount++;
2717 else
2718 nSelectedCount = ranges_itemcount(infoPtr->selectionRanges);
2720 TRACE("nSelectedCount=%d\n", nSelectedCount);
2721 return nSelectedCount;
2724 /***
2725 * DESCRIPTION:
2726 * Manages the item focus.
2728 * PARAMETER(S):
2729 * [I] infoPtr : valid pointer to the listview structure
2730 * [I] nItem : item index
2732 * RETURN:
2733 * TRUE : focused item changed
2734 * FALSE : focused item has NOT changed
2736 static inline BOOL LISTVIEW_SetItemFocus(LISTVIEW_INFO *infoPtr, INT nItem)
2738 INT oldFocus = infoPtr->nFocusedItem;
2739 LVITEMW lvItem;
2741 if (nItem == infoPtr->nFocusedItem) return FALSE;
2743 lvItem.state = nItem == -1 ? 0 : LVIS_FOCUSED;
2744 lvItem.stateMask = LVIS_FOCUSED;
2745 LISTVIEW_SetItemState(infoPtr, nItem == -1 ? infoPtr->nFocusedItem : nItem, &lvItem);
2747 return oldFocus != infoPtr->nFocusedItem;
2750 /* Helper function for LISTVIEW_ShiftIndices *only* */
2751 static INT shift_item(LISTVIEW_INFO *infoPtr, INT nShiftItem, INT nItem, INT direction)
2753 if (nShiftItem < nItem) return nShiftItem;
2755 if (nShiftItem > nItem) return nShiftItem + direction;
2757 if (direction > 0) return nShiftItem + direction;
2759 return min(nShiftItem, infoPtr->nItemCount - 1);
2763 * DESCRIPTION:
2764 * Updates the various indices after an item has been inserted or deleted.
2766 * PARAMETER(S):
2767 * [I] infoPtr : valid pointer to the listview structure
2768 * [I] nItem : item index
2769 * [I] direction : Direction of shift, +1 or -1.
2771 * RETURN:
2772 * None
2774 static void LISTVIEW_ShiftIndices(LISTVIEW_INFO *infoPtr, INT nItem, INT direction)
2776 INT nNewFocus;
2778 TRACE("Shifting %iu, %i steps\n", nItem, direction);
2780 ranges_shift(infoPtr->selectionRanges, nItem, direction, infoPtr->nItemCount);
2782 assert(abs(direction) == 1);
2784 infoPtr->nSelectionMark = shift_item(infoPtr, infoPtr->nSelectionMark, nItem, direction);
2786 nNewFocus = shift_item(infoPtr, infoPtr->nFocusedItem, nItem, direction);
2787 if (nNewFocus != infoPtr->nFocusedItem)
2788 LISTVIEW_SetItemFocus(infoPtr, nNewFocus);
2790 /* But we are not supposed to modify nHotItem! */
2795 * DESCRIPTION:
2796 * Adds a block of selections.
2798 * PARAMETER(S):
2799 * [I] infoPtr : valid pointer to the listview structure
2800 * [I] nItem : item index
2802 * RETURN:
2803 * None
2805 static void LISTVIEW_AddGroupSelection(LISTVIEW_INFO *infoPtr, INT nItem)
2807 INT nFirst = min(infoPtr->nSelectionMark, nItem);
2808 INT nLast = max(infoPtr->nSelectionMark, nItem);
2809 INT i;
2810 LVITEMW item;
2812 if (nFirst == -1) nFirst = nItem;
2814 item.state = LVIS_SELECTED;
2815 item.stateMask = LVIS_SELECTED;
2817 /* FIXME: this is not correct LVS_OWNERDATA
2818 * setting the item states individually will generate
2819 * a LVN_ITEMCHANGED notification for each one. Instead,
2820 * we have to send a LVN_ODSTATECHANGED notification.
2821 * See MSDN documentation for LVN_ITEMCHANGED.
2823 for (i = nFirst; i <= nLast; i++)
2824 LISTVIEW_SetItemState(infoPtr,i,&item);
2828 /***
2829 * DESCRIPTION:
2830 * Sets a single group selection.
2832 * PARAMETER(S):
2833 * [I] infoPtr : valid pointer to the listview structure
2834 * [I] nItem : item index
2836 * RETURN:
2837 * None
2839 static void LISTVIEW_SetGroupSelection(LISTVIEW_INFO *infoPtr, INT nItem)
2841 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
2842 RANGES selection;
2843 LVITEMW item;
2844 ITERATOR i;
2846 if (!(selection = ranges_create(100))) return;
2848 item.state = LVIS_SELECTED;
2849 item.stateMask = LVIS_SELECTED;
2851 if ((uView == LVS_LIST) || (uView == LVS_REPORT))
2853 if (infoPtr->nSelectionMark == -1)
2855 infoPtr->nSelectionMark = nItem;
2856 ranges_additem(selection, nItem);
2858 else
2860 RANGE sel;
2862 sel.lower = min(infoPtr->nSelectionMark, nItem);
2863 sel.upper = max(infoPtr->nSelectionMark, nItem) + 1;
2864 ranges_add(selection, sel);
2867 else
2869 RECT rcItem, rcSel, rcSelMark;
2870 POINT ptItem;
2872 rcItem.left = LVIR_BOUNDS;
2873 if (!LISTVIEW_GetItemRect(infoPtr, nItem, &rcItem)) return;
2874 rcSelMark.left = LVIR_BOUNDS;
2875 if (!LISTVIEW_GetItemRect(infoPtr, infoPtr->nSelectionMark, &rcSelMark)) return;
2876 UnionRect(&rcSel, &rcItem, &rcSelMark);
2877 iterator_frameditems(&i, infoPtr, &rcSel);
2878 while(iterator_next(&i))
2880 LISTVIEW_GetItemPosition(infoPtr, i.nItem, &ptItem);
2881 if (PtInRect(&rcSel, ptItem)) ranges_additem(selection, i.nItem);
2883 iterator_destroy(&i);
2886 LISTVIEW_DeselectAllSkipItems(infoPtr, selection);
2887 iterator_rangesitems(&i, selection);
2888 while(iterator_next(&i))
2889 LISTVIEW_SetItemState(infoPtr, i.nItem, &item);
2890 /* this will also destroy the selection */
2891 iterator_destroy(&i);
2893 LISTVIEW_SetItemFocus(infoPtr, nItem);
2896 /***
2897 * DESCRIPTION:
2898 * Sets a single selection.
2900 * PARAMETER(S):
2901 * [I] infoPtr : valid pointer to the listview structure
2902 * [I] nItem : item index
2904 * RETURN:
2905 * None
2907 static void LISTVIEW_SetSelection(LISTVIEW_INFO *infoPtr, INT nItem)
2909 LVITEMW lvItem;
2911 TRACE("nItem=%d\n", nItem);
2913 LISTVIEW_DeselectAllSkipItem(infoPtr, nItem);
2915 lvItem.state = LVIS_FOCUSED | LVIS_SELECTED;
2916 lvItem.stateMask = LVIS_FOCUSED | LVIS_SELECTED;
2917 LISTVIEW_SetItemState(infoPtr, nItem, &lvItem);
2919 infoPtr->nSelectionMark = nItem;
2922 /***
2923 * DESCRIPTION:
2924 * Set selection(s) with keyboard.
2926 * PARAMETER(S):
2927 * [I] infoPtr : valid pointer to the listview structure
2928 * [I] nItem : item index
2930 * RETURN:
2931 * SUCCESS : TRUE (needs to be repainted)
2932 * FAILURE : FALSE (nothing has changed)
2934 static BOOL LISTVIEW_KeySelection(LISTVIEW_INFO *infoPtr, INT nItem)
2936 /* FIXME: pass in the state */
2937 WORD wShift = HIWORD(GetKeyState(VK_SHIFT));
2938 WORD wCtrl = HIWORD(GetKeyState(VK_CONTROL));
2939 BOOL bResult = FALSE;
2941 if ((nItem >= 0) && (nItem < infoPtr->nItemCount))
2943 if (infoPtr->dwStyle & LVS_SINGLESEL)
2945 bResult = TRUE;
2946 LISTVIEW_SetSelection(infoPtr, nItem);
2948 else
2950 if (wShift)
2952 bResult = TRUE;
2953 LISTVIEW_SetGroupSelection(infoPtr, nItem);
2955 else if (wCtrl)
2957 bResult = LISTVIEW_SetItemFocus(infoPtr, nItem);
2959 else
2961 bResult = TRUE;
2962 LISTVIEW_SetSelection(infoPtr, nItem);
2965 ListView_EnsureVisible(infoPtr->hwndSelf, nItem, FALSE);
2968 UpdateWindow(infoPtr->hwndSelf); /* update client area */
2969 return bResult;
2973 /***
2974 * DESCRIPTION:
2975 * Called when the mouse is being actively tracked and has hovered for a specified
2976 * amount of time
2978 * PARAMETER(S):
2979 * [I] infoPtr : valid pointer to the listview structure
2980 * [I] fwKeys : key indicator
2981 * [I] pts : mouse position
2983 * RETURN:
2984 * 0 if the message was processed, non-zero if there was an error
2986 * INFO:
2987 * LVS_EX_TRACKSELECT: An item is automatically selected when the cursor remains
2988 * over the item for a certain period of time.
2991 static LRESULT LISTVIEW_MouseHover(LISTVIEW_INFO *infoPtr, WORD fwKyes, POINTS pts)
2993 if(infoPtr->dwLvExStyle & LVS_EX_TRACKSELECT)
2994 /* FIXME: select the item!!! */
2995 /*LISTVIEW_GetItemAtPt(infoPtr, pt)*/;
2997 return 0;
3000 /***
3001 * DESCRIPTION:
3002 * Called whenever WM_MOUSEMOVE is received.
3004 * PARAMETER(S):
3005 * [I] infoPtr : valid pointer to the listview structure
3006 * [I] fwKeys : key indicator
3007 * [I] pts : mouse position
3009 * RETURN:
3010 * 0 if the message is processed, non-zero if there was an error
3012 static LRESULT LISTVIEW_MouseMove(LISTVIEW_INFO *infoPtr, WORD fwKeys, POINTS pts)
3014 TRACKMOUSEEVENT trackinfo;
3016 /* see if we are supposed to be tracking mouse hovering */
3017 if(infoPtr->dwLvExStyle & LVS_EX_TRACKSELECT) {
3018 /* fill in the trackinfo struct */
3019 trackinfo.cbSize = sizeof(TRACKMOUSEEVENT);
3020 trackinfo.dwFlags = TME_QUERY;
3021 trackinfo.hwndTrack = infoPtr->hwndSelf;
3022 trackinfo.dwHoverTime = infoPtr->dwHoverTime;
3024 /* see if we are already tracking this hwnd */
3025 _TrackMouseEvent(&trackinfo);
3027 if(!(trackinfo.dwFlags & TME_HOVER)) {
3028 trackinfo.dwFlags = TME_HOVER;
3030 /* call TRACKMOUSEEVENT so we receive WM_MOUSEHOVER messages */
3031 _TrackMouseEvent(&trackinfo);
3035 return 0;
3039 /***
3040 * Tests wheather the item is assignable to a list with style lStyle
3042 static inline BOOL is_assignable_item(const LVITEMW *lpLVItem, LONG lStyle)
3044 if ( (lpLVItem->mask & LVIF_TEXT) &&
3045 (lpLVItem->pszText == LPSTR_TEXTCALLBACKW) &&
3046 (lStyle & (LVS_SORTASCENDING | LVS_SORTDESCENDING)) ) return FALSE;
3048 return TRUE;
3052 /***
3053 * DESCRIPTION:
3054 * Helper for LISTVIEW_SetItemT *only*: sets item attributes.
3056 * PARAMETER(S):
3057 * [I] infoPtr : valid pointer to the listview structure
3058 * [I] lpLVItem : valid pointer to new item atttributes
3059 * [I] isNew : the item being set is being inserted
3060 * [I] isW : TRUE if lpLVItem is Unicode, FALSE if it's ANSI
3061 * [O] bChanged : will be set to TRUE if the item really changed
3063 * RETURN:
3064 * SUCCESS : TRUE
3065 * FAILURE : FALSE
3067 static BOOL set_main_item(LISTVIEW_INFO *infoPtr, const LVITEMW *lpLVItem, BOOL isNew, BOOL isW, BOOL *bChanged)
3069 ITEM_INFO *lpItem;
3070 NMLISTVIEW nmlv;
3071 UINT uChanged = 0;
3072 LVITEMW item;
3074 TRACE("()\n");
3076 assert(lpLVItem->iItem >= 0 && lpLVItem->iItem < infoPtr->nItemCount);
3078 if (lpLVItem->mask == 0) return TRUE;
3080 if (infoPtr->dwStyle & LVS_OWNERDATA)
3082 /* a virtual listview we stores only selection and focus */
3083 if ((lpLVItem->mask & ~LVIF_STATE) || (lpLVItem->stateMask & ~(LVIS_FOCUSED | LVIS_SELECTED)))
3084 return FALSE;
3085 lpItem = NULL;
3087 else
3089 HDPA hdpaSubItems = (HDPA)DPA_GetPtr(infoPtr->hdpaItems, lpLVItem->iItem);
3090 lpItem = (ITEM_INFO *)DPA_GetPtr(hdpaSubItems, 0);
3091 assert (lpItem);
3094 /* we need to get the lParam and state of the item */
3095 item.iItem = lpLVItem->iItem;
3096 item.iSubItem = lpLVItem->iSubItem;
3097 item.mask = LVIF_STATE | LVIF_PARAM;
3098 item.stateMask = ~0;
3099 item.state = 0;
3100 item.lParam = 0;
3101 if (!isNew && !LISTVIEW_GetItemW(infoPtr, &item)) return FALSE;
3103 TRACE("oldState=%x, newState=%x\n", item.state, lpLVItem->state);
3104 /* determine what fields will change */
3105 if ((lpLVItem->mask & LVIF_STATE) && ((item.state ^ lpLVItem->state) & lpLVItem->stateMask & ~infoPtr->uCallbackMask))
3106 uChanged |= LVIF_STATE;
3108 if ((lpLVItem->mask & LVIF_IMAGE) && (lpItem->hdr.iImage != lpLVItem->iImage))
3109 uChanged |= LVIF_IMAGE;
3111 if ((lpLVItem->mask & LVIF_PARAM) && (lpItem->lParam != lpLVItem->lParam))
3112 uChanged |= LVIF_PARAM;
3114 if ((lpLVItem->mask & LVIF_INDENT) && (lpItem->iIndent != lpLVItem->iIndent))
3115 uChanged |= LVIF_INDENT;
3117 if ((lpLVItem->mask & LVIF_TEXT) && textcmpWT(lpItem->hdr.pszText, lpLVItem->pszText, isW))
3118 uChanged |= LVIF_TEXT;
3120 TRACE("uChanged=0x%x\n", uChanged);
3121 if (!uChanged) return TRUE;
3122 *bChanged = TRUE;
3124 ZeroMemory(&nmlv, sizeof(NMLISTVIEW));
3125 nmlv.iItem = lpLVItem->iItem;
3126 nmlv.uNewState = (item.state & ~lpLVItem->stateMask) | (lpLVItem->state & lpLVItem->stateMask);
3127 nmlv.uOldState = item.state;
3128 nmlv.uChanged = uChanged;
3129 nmlv.lParam = item.lParam;
3131 /* send LVN_ITEMCHANGING notification, if the item is not being inserted */
3132 /* and we are _NOT_ virtual (LVS_OWERNDATA) */
3133 if(lpItem && !isNew && notify_listview(infoPtr, LVN_ITEMCHANGING, &nmlv))
3134 return FALSE;
3136 /* copy information */
3137 if (lpLVItem->mask & LVIF_TEXT)
3138 textsetptrT(&lpItem->hdr.pszText, lpLVItem->pszText, isW);
3140 if (lpLVItem->mask & LVIF_IMAGE)
3141 lpItem->hdr.iImage = lpLVItem->iImage;
3143 if (lpLVItem->mask & LVIF_PARAM)
3144 lpItem->lParam = lpLVItem->lParam;
3146 if (lpLVItem->mask & LVIF_INDENT)
3147 lpItem->iIndent = lpLVItem->iIndent;
3149 if (uChanged & LVIF_STATE)
3151 if (lpLVItem->stateMask & ~infoPtr->uCallbackMask & ~(LVIS_FOCUSED | LVIS_SELECTED))
3153 lpItem->state &= ~lpLVItem->stateMask;
3154 lpItem->state |= (lpLVItem->state & lpLVItem->stateMask);
3156 if (lpLVItem->state & lpLVItem->stateMask & ~infoPtr->uCallbackMask & LVIS_SELECTED)
3158 if (infoPtr->dwStyle & LVS_SINGLESEL) LISTVIEW_DeselectAllSkipItem(infoPtr, lpLVItem->iItem);
3159 ranges_additem(infoPtr->selectionRanges, lpLVItem->iItem);
3161 else if (lpLVItem->stateMask & LVIS_SELECTED)
3162 ranges_delitem(infoPtr->selectionRanges, lpLVItem->iItem);
3164 /* if we are asked to change focus, and we manage it, do it */
3165 if (lpLVItem->state & lpLVItem->stateMask & ~infoPtr->uCallbackMask & LVIS_FOCUSED)
3167 if (lpLVItem->state & LVIS_FOCUSED)
3169 LISTVIEW_SetItemFocus(infoPtr, -1);
3170 infoPtr->nFocusedItem = lpLVItem->iItem;
3171 LISTVIEW_EnsureVisible(infoPtr, lpLVItem->iItem, FALSE);
3173 else if (infoPtr->nFocusedItem == lpLVItem->iItem)
3174 infoPtr->nFocusedItem = -1;
3178 /* if we're inserting the item, we're done */
3179 if (isNew) return TRUE;
3181 /* send LVN_ITEMCHANGED notification */
3182 if (lpLVItem->mask & LVIF_PARAM) nmlv.lParam = lpLVItem->lParam;
3183 notify_listview(infoPtr, LVN_ITEMCHANGED, &nmlv);
3185 return TRUE;
3188 /***
3189 * DESCRIPTION:
3190 * Helper for LISTVIEW_{Set,Insert}ItemT *only*: sets subitem attributes.
3192 * PARAMETER(S):
3193 * [I] infoPtr : valid pointer to the listview structure
3194 * [I] lpLVItem : valid pointer to new subitem atttributes
3195 * [I] isW : TRUE if lpLVItem is Unicode, FALSE if it's ANSI
3196 * [O] bChanged : will be set to TRUE if the item really changed
3198 * RETURN:
3199 * SUCCESS : TRUE
3200 * FAILURE : FALSE
3202 static BOOL set_sub_item(LISTVIEW_INFO *infoPtr, const LVITEMW *lpLVItem, BOOL isW, BOOL *bChanged)
3204 HDPA hdpaSubItems;
3205 SUBITEM_INFO *lpSubItem;
3207 /* we do not support subitems for virtual listviews */
3208 if (infoPtr->dwStyle & LVS_OWNERDATA) return FALSE;
3210 /* set subitem only if column is present */
3211 if (lpLVItem->iSubItem >= infoPtr->hdpaColumns->nItemCount) return FALSE;
3213 /* First do some sanity checks */
3214 if (lpLVItem->mask & ~(LVIF_TEXT | LVIF_IMAGE)) return FALSE;
3215 if (!(lpLVItem->mask & (LVIF_TEXT | LVIF_IMAGE))) return TRUE;
3217 /* get the subitem structure, and create it if not there */
3218 hdpaSubItems = (HDPA)DPA_GetPtr(infoPtr->hdpaItems, lpLVItem->iItem);
3219 assert (hdpaSubItems);
3221 lpSubItem = LISTVIEW_GetSubItemPtr(hdpaSubItems, lpLVItem->iSubItem);
3222 if (!lpSubItem)
3224 SUBITEM_INFO *tmpSubItem;
3225 INT i;
3227 lpSubItem = (SUBITEM_INFO *)COMCTL32_Alloc(sizeof(SUBITEM_INFO));
3228 if (!lpSubItem) return FALSE;
3229 /* we could binary search here, if need be...*/
3230 for (i = 1; i < hdpaSubItems->nItemCount; i++)
3232 tmpSubItem = (SUBITEM_INFO *)DPA_GetPtr(hdpaSubItems, i);
3233 if (tmpSubItem->iSubItem > lpLVItem->iSubItem) break;
3235 if (DPA_InsertPtr(hdpaSubItems, i, lpSubItem) == -1)
3237 COMCTL32_Free(lpSubItem);
3238 return FALSE;
3240 lpSubItem->iSubItem = lpLVItem->iSubItem;
3241 *bChanged = TRUE;
3244 if (lpLVItem->mask & LVIF_IMAGE)
3245 if (lpSubItem->hdr.iImage != lpLVItem->iImage)
3247 lpSubItem->hdr.iImage = lpLVItem->iImage;
3248 *bChanged = TRUE;
3251 if (lpLVItem->mask & LVIF_TEXT)
3252 if (lpSubItem->hdr.pszText != lpLVItem->pszText)
3254 textsetptrT(&lpSubItem->hdr.pszText, lpLVItem->pszText, isW);
3255 *bChanged = TRUE;
3258 return TRUE;
3261 /***
3262 * DESCRIPTION:
3263 * Sets item attributes.
3265 * PARAMETER(S):
3266 * [I] infoPtr : valid pointer to the listview structure
3267 * [I] lpLVItem : new item atttributes
3268 * [I] isW : TRUE if lpLVItem is Unicode, FALSE if it's ANSI
3270 * RETURN:
3271 * SUCCESS : TRUE
3272 * FAILURE : FALSE
3274 static BOOL LISTVIEW_SetItemT(LISTVIEW_INFO *infoPtr, const LVITEMW *lpLVItem, BOOL isW)
3276 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
3277 LPWSTR pszText = NULL;
3278 BOOL bResult, bChanged = FALSE;
3280 TRACE("(lpLVItem=%s, isW=%d)\n", debuglvitem_t(lpLVItem, isW), isW);
3282 if (!lpLVItem || lpLVItem->iItem < 0 || lpLVItem->iItem >= infoPtr->nItemCount)
3283 return FALSE;
3285 /* For efficiency, we transform the lpLVItem->pszText to Unicode here */
3286 if ((lpLVItem->mask & LVIF_TEXT) && is_textW(lpLVItem->pszText))
3288 pszText = lpLVItem->pszText;
3289 ((LVITEMW *)lpLVItem)->pszText = textdupTtoW(lpLVItem->pszText, isW);
3292 /* actually set the fields */
3293 if (!is_assignable_item(lpLVItem, infoPtr->dwStyle)) return FALSE;
3295 if (lpLVItem->iSubItem)
3296 bResult = set_sub_item(infoPtr, lpLVItem, TRUE, &bChanged);
3297 else
3298 bResult = set_main_item(infoPtr, lpLVItem, FALSE, TRUE, &bChanged);
3300 /* redraw item, if necessary */
3301 if (bChanged && !infoPtr->bIsDrawing)
3303 /* this little optimization eliminates some nasty flicker */
3304 if ( uView == LVS_REPORT && !(infoPtr->dwStyle & LVS_OWNERDRAWFIXED) &&
3305 (!(infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT) || lpLVItem->iSubItem) )
3306 LISTVIEW_InvalidateSubItem(infoPtr, lpLVItem->iItem, lpLVItem->iSubItem);
3307 else
3308 LISTVIEW_InvalidateItem(infoPtr, lpLVItem->iItem);
3310 /* restore text */
3311 if (pszText)
3313 textfreeT(lpLVItem->pszText, isW);
3314 ((LVITEMW *)lpLVItem)->pszText = pszText;
3317 return bResult;
3320 /***
3321 * DESCRIPTION:
3322 * Retrieves the index of the item at coordinate (0, 0) of the client area.
3324 * PARAMETER(S):
3325 * [I] infoPtr : valid pointer to the listview structure
3327 * RETURN:
3328 * item index
3330 static INT LISTVIEW_GetTopIndex(LISTVIEW_INFO *infoPtr)
3332 LONG lStyle = infoPtr->dwStyle;
3333 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
3334 INT nItem = 0;
3335 SCROLLINFO scrollInfo;
3337 scrollInfo.cbSize = sizeof(SCROLLINFO);
3338 scrollInfo.fMask = SIF_POS;
3340 if (uView == LVS_LIST)
3342 if ((lStyle & WS_HSCROLL) && GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo))
3343 nItem = scrollInfo.nPos * LISTVIEW_GetCountPerColumn(infoPtr);
3345 else if (uView == LVS_REPORT)
3347 if ((lStyle & WS_VSCROLL) && GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo))
3348 nItem = scrollInfo.nPos;
3350 else
3352 if ((lStyle & WS_VSCROLL) && GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo))
3353 nItem = LISTVIEW_GetCountPerRow(infoPtr) * (scrollInfo.nPos / infoPtr->nItemHeight);
3356 TRACE("nItem=%d\n", nItem);
3358 return nItem;
3362 /***
3363 * DESCRIPTION:
3364 * Erases the background of the given rectangle
3366 * PARAMETER(S):
3367 * [I] infoPtr : valid pointer to the listview structure
3368 * [I] hdc : device context handle
3369 * [I] lprcBox : clipping rectangle
3371 * RETURN:
3372 * Success: TRUE
3373 * Failure: FALSE
3375 static inline BOOL LISTVIEW_FillBkgnd(LISTVIEW_INFO *infoPtr, HDC hdc, const RECT *lprcBox)
3377 if (!infoPtr->hBkBrush) return FALSE;
3379 TRACE("(hdc=%p, lprcBox=%s, hBkBrush=%p)\n", hdc, debugrect(lprcBox), infoPtr->hBkBrush);
3381 return FillRect(hdc, lprcBox, infoPtr->hBkBrush);
3384 /***
3385 * DESCRIPTION:
3386 * Draws an item.
3388 * PARAMETER(S):
3389 * [I] infoPtr : valid pointer to the listview structure
3390 * [I] hdc : device context handle
3391 * [I] nItem : item index
3392 * [I] nSubItem : subitem index
3393 * [I] pos : item position in client coordinates
3394 * [I] cdmode : custom draw mode
3396 * RETURN:
3397 * Success: TRUE
3398 * Failure: FALSE
3400 static BOOL LISTVIEW_DrawItem(LISTVIEW_INFO *infoPtr, HDC hdc, INT nItem, INT nSubItem, POINT pos, DWORD cdmode)
3402 UINT uFormat, uView = infoPtr->dwStyle & LVS_TYPEMASK;
3403 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
3404 WCHAR szCallback[] = { '(', 'c', 'a', 'l', 'l', 'b', 'a', 'c', 'k', ')', 0 };
3405 DWORD cditemmode = CDRF_DODEFAULT;
3406 RECT* lprcFocus, rcSelect, rcBox, rcState, rcIcon, rcLabel;
3407 NMLVCUSTOMDRAW nmlvcd;
3408 HIMAGELIST himl;
3409 LVITEMW lvItem;
3411 TRACE("(hdc=%p, nItem=%d, nSubItem=%d, pos=%s)\n", hdc, nItem, nSubItem, debugpoint(&pos));
3413 /* get information needed for drawing the item */
3414 lvItem.mask = LVIF_TEXT | LVIF_IMAGE;
3415 if (nSubItem == 0) lvItem.mask |= LVIF_STATE | LVIF_PARAM;
3416 if (uView == LVS_REPORT) lvItem.mask |= LVIF_INDENT;
3417 lvItem.stateMask = LVIS_SELECTED | LVIS_FOCUSED | LVIS_STATEIMAGEMASK;
3418 lvItem.iItem = nItem;
3419 lvItem.iSubItem = nSubItem;
3420 lvItem.state = 0;
3421 lvItem.lParam = 0;
3422 lvItem.cchTextMax = DISP_TEXT_SIZE;
3423 lvItem.pszText = szDispText;
3424 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) return FALSE;
3425 if (nSubItem > 0 && (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT))
3426 lvItem.state = LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED);
3427 if (lvItem.pszText == LPSTR_TEXTCALLBACKW) lvItem.pszText = szCallback;
3428 TRACE(" lvItem=%s\n", debuglvitem_t(&lvItem, TRUE));
3430 /* now check if we need to update the focus rectangle */
3431 lprcFocus = infoPtr->bFocus && (lvItem.state & LVIS_FOCUSED) ? &infoPtr->rcFocus : 0;
3433 if (!lprcFocus) lvItem.state &= ~LVIS_FOCUSED;
3434 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, &rcBox, &rcState, &rcIcon, &rcLabel);
3435 OffsetRect(&rcBox, pos.x, pos.y);
3436 OffsetRect(&rcState, pos.x, pos.y);
3437 OffsetRect(&rcIcon, pos.x, pos.y);
3438 OffsetRect(&rcLabel, pos.x, pos.y);
3439 TRACE(" rcBox=%s, rcState=%s, rcIcon=%s. rcLabel=%s\n",
3440 debugrect(&rcBox), debugrect(&rcState), debugrect(&rcIcon), debugrect(&rcLabel));
3442 /* fill in the custom draw structure */
3443 customdraw_fill(&nmlvcd, infoPtr, hdc, &rcBox);
3444 nmlvcd.nmcd.dwItemSpec = lvItem.iItem;
3445 nmlvcd.iSubItem = lvItem.iSubItem;
3446 if (lvItem.state & LVIS_SELECTED) nmlvcd.nmcd.uItemState |= CDIS_SELECTED;
3447 if (lvItem.state & LVIS_FOCUSED) nmlvcd.nmcd.uItemState |= CDIS_FOCUS;
3448 if (lvItem.iItem == infoPtr->nHotItem) nmlvcd.nmcd.uItemState |= CDIS_HOT;
3449 nmlvcd.nmcd.lItemlParam = lvItem.lParam;
3451 if (cdmode & CDRF_NOTIFYITEMDRAW)
3452 cditemmode = notify_customdraw (infoPtr, CDDS_ITEMPREPAINT, &nmlvcd);
3453 if (cditemmode & CDRF_SKIPDEFAULT) goto postpaint;
3455 /* apprently, for selected items, we have to override the returned values */
3456 if (lvItem.state & LVIS_SELECTED)
3458 if (infoPtr->bFocus)
3460 nmlvcd.clrTextBk = comctl32_color.clrHighlight;
3461 nmlvcd.clrText = comctl32_color.clrHighlightText;
3463 else if (infoPtr->dwStyle & LVS_SHOWSELALWAYS)
3465 nmlvcd.clrTextBk = comctl32_color.clr3dFace;
3466 nmlvcd.clrText = comctl32_color.clrBtnText;
3470 /* in full row select, subitems, will just use main item's colors */
3471 if (nSubItem && uView == LVS_REPORT && (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT))
3472 nmlvcd.clrTextBk = CLR_NONE;
3474 /* state icons */
3475 if (infoPtr->himlState && !IsRectEmpty(&rcState))
3477 UINT uStateImage = (lvItem.state & LVIS_STATEIMAGEMASK) >> 12;
3478 if (uStateImage)
3480 TRACE("uStateImage=%d\n", uStateImage);
3481 ImageList_Draw(infoPtr->himlState, uStateImage - 1, hdc, rcState.left, rcState.top, ILD_NORMAL);
3485 /* small icons */
3486 himl = (uView == LVS_ICON ? infoPtr->himlNormal : infoPtr->himlSmall);
3487 if (himl && lvItem.iImage >= 0 && !IsRectEmpty(&rcIcon))
3489 TRACE("iImage=%d\n", lvItem.iImage);
3490 ImageList_Draw(himl, lvItem.iImage, hdc, rcIcon.left, rcIcon.top,
3491 (lvItem.state & LVIS_SELECTED) && (infoPtr->bFocus) ? ILD_SELECTED : ILD_NORMAL);
3494 /* Don't bother painting item being edited */
3495 if (infoPtr->hwndEdit && lprcFocus && nSubItem == 0) goto postpaint;
3497 /* Set the text attributes */
3498 if (nmlvcd.clrTextBk != CLR_NONE)
3500 SetBkMode(hdc, OPAQUE);
3501 SetBkColor(hdc, nmlvcd.clrTextBk == CLR_DEFAULT ? infoPtr->clrTextBkDefault : nmlvcd.clrTextBk);
3503 else
3504 SetBkMode(hdc, TRANSPARENT);
3505 SetTextColor(hdc, nmlvcd.clrText);
3507 /* draw the selection background, if we're drawing the main item */
3508 if (nSubItem == 0)
3510 rcSelect = rcLabel;
3511 if (uView == LVS_REPORT && (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT))
3512 rcSelect.right = rcBox.right;
3514 if (nmlvcd.clrTextBk != CLR_NONE)
3515 ExtTextOutW(hdc, rcSelect.left, rcSelect.top, ETO_OPAQUE, &rcSelect, 0, 0, 0);
3516 if(lprcFocus) *lprcFocus = rcSelect;
3519 /* figure out the text drawing flags */
3520 uFormat = (uView == LVS_ICON ? (lprcFocus ? LV_FL_DT_FLAGS : LV_ML_DT_FLAGS) : LV_SL_DT_FLAGS);
3521 if (uView == LVS_ICON)
3522 uFormat = (lprcFocus ? LV_FL_DT_FLAGS : LV_ML_DT_FLAGS);
3523 else if (nSubItem)
3525 switch (LISTVIEW_GetColumnInfo(infoPtr, nSubItem)->fmt & LVCFMT_JUSTIFYMASK)
3527 case LVCFMT_RIGHT: uFormat |= DT_RIGHT; break;
3528 case LVCFMT_CENTER: uFormat |= DT_CENTER; break;
3529 default: uFormat |= DT_LEFT;
3532 if (!(uFormat & (DT_RIGHT | DT_CENTER))) rcLabel.left += 2;
3533 DrawTextW(hdc, lvItem.pszText, -1, &rcLabel, uFormat);
3535 postpaint:
3536 if (cditemmode & CDRF_NOTIFYPOSTPAINT)
3537 notify_customdraw(infoPtr, CDDS_ITEMPOSTPAINT, &nmlvcd);
3538 return TRUE;
3541 /***
3542 * DESCRIPTION:
3543 * Draws listview items when in owner draw mode.
3545 * PARAMETER(S):
3546 * [I] infoPtr : valid pointer to the listview structure
3547 * [I] hdc : device context handle
3549 * RETURN:
3550 * None
3552 static void LISTVIEW_RefreshOwnerDraw(LISTVIEW_INFO *infoPtr, HDC hdc)
3554 UINT uID = GetWindowLongW(infoPtr->hwndSelf, GWL_ID);
3555 HWND hwndParent = GetParent(infoPtr->hwndSelf);
3556 POINT Origin, Position;
3557 DRAWITEMSTRUCT dis;
3558 LVITEMW item;
3559 ITERATOR i;
3561 TRACE("()\n");
3563 ZeroMemory(&dis, sizeof(dis));
3565 /* Get scroll info once before loop */
3566 LISTVIEW_GetOrigin(infoPtr, &Origin);
3568 /* figure out what we need to draw */
3569 iterator_visibleitems(&i, infoPtr, hdc);
3571 /* send cache hint notification */
3572 if (infoPtr->dwStyle & LVS_OWNERDATA)
3574 RANGE range = iterator_range(&i);
3575 NMLVCACHEHINT nmlv;
3577 ZeroMemory(&nmlv, sizeof(NMLVCACHEHINT));
3578 nmlv.iFrom = range.lower;
3579 nmlv.iTo = range.upper - 1;
3580 notify_hdr(infoPtr, LVN_ODCACHEHINT, &nmlv.hdr);
3583 /* iterate through the invalidated rows */
3584 while(iterator_next(&i))
3586 item.iItem = i.nItem;
3587 item.iSubItem = 0;
3588 item.mask = LVIF_PARAM | LVIF_STATE;
3589 item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
3590 if (!LISTVIEW_GetItemW(infoPtr, &item)) continue;
3592 dis.CtlType = ODT_LISTVIEW;
3593 dis.CtlID = uID;
3594 dis.itemID = item.iItem;
3595 dis.itemAction = ODA_DRAWENTIRE;
3596 dis.itemState = 0;
3597 if (item.state & LVIS_SELECTED) dis.itemState |= ODS_SELECTED;
3598 if (infoPtr->bFocus && (item.state & LVIS_FOCUSED)) dis.itemState |= ODS_FOCUS;
3599 dis.hwndItem = infoPtr->hwndSelf;
3600 dis.hDC = hdc;
3601 LISTVIEW_GetItemOrigin(infoPtr, dis.itemID, &Position);
3602 dis.rcItem.left = Position.x + Origin.x;
3603 dis.rcItem.right = dis.rcItem.left + infoPtr->nItemWidth;
3604 dis.rcItem.top = Position.y + Origin.y;
3605 dis.rcItem.bottom = dis.rcItem.top + infoPtr->nItemHeight;
3606 dis.itemData = item.lParam;
3608 TRACE("item=%s, rcItem=%s\n", debuglvitem_t(&item, TRUE), debugrect(&dis.rcItem));
3609 SendMessageW(hwndParent, WM_DRAWITEM, dis.CtlID, (LPARAM)&dis);
3611 iterator_destroy(&i);
3614 /***
3615 * DESCRIPTION:
3616 * Draws listview items when in report display mode.
3618 * PARAMETER(S):
3619 * [I] infoPtr : valid pointer to the listview structure
3620 * [I] hdc : device context handle
3621 * [I] cdmode : custom draw mode
3623 * RETURN:
3624 * None
3626 static void LISTVIEW_RefreshReport(LISTVIEW_INFO *infoPtr, HDC hdc, DWORD cdmode)
3628 INT rgntype;
3629 RECT rcClip, rcItem;
3630 POINT Origin, Position;
3631 RANGE colRange;
3632 ITERATOR i, j;
3634 TRACE("()\n");
3636 /* figure out what to draw */
3637 rgntype = GetClipBox(hdc, &rcClip);
3638 if (rgntype == NULLREGION) return;
3640 /* Get scroll info once before loop */
3641 LISTVIEW_GetOrigin(infoPtr, &Origin);
3643 /* narrow down the columns we need to paint */
3644 for(colRange.lower = 0; colRange.lower < infoPtr->hdpaColumns->nItemCount; colRange.lower++)
3646 LISTVIEW_GetHeaderRect(infoPtr, colRange.lower, &rcItem);
3647 if (rcItem.right + Origin.x >= rcClip.left) break;
3649 for(colRange.upper = infoPtr->hdpaColumns->nItemCount; colRange.upper > 0; colRange.upper--)
3651 LISTVIEW_GetHeaderRect(infoPtr, colRange.upper - 1, &rcItem);
3652 if (rcItem.left + Origin.x < rcClip.right) break;
3654 iterator_rangeitems(&j, colRange);
3656 /* in full row select, we _have_ to draw the main item */
3657 if (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT)
3658 j.nSpecial = 0;
3660 /* figure out what we need to draw */
3661 iterator_visibleitems(&i, infoPtr, hdc);
3663 /* iterate through the invalidated rows */
3664 while(iterator_next(&i))
3666 /* iterate through the invalidated columns */
3667 while(iterator_next(&j))
3669 LISTVIEW_GetItemOrigin(infoPtr, i.nItem, &Position);
3670 Position.x += Origin.x;
3671 Position.y += Origin.y;
3673 if (rgntype == COMPLEXREGION && !((infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT) && j.nItem == 0))
3675 LISTVIEW_GetHeaderRect(infoPtr, j.nItem, &rcItem);
3676 rcItem.top = 0;
3677 rcItem.bottom = infoPtr->nItemHeight;
3678 OffsetRect(&rcItem, Position.x, Position.y);
3679 if (!RectVisible(hdc, &rcItem)) continue;
3682 LISTVIEW_DrawItem(infoPtr, hdc, i.nItem, j.nItem, Position, cdmode);
3685 iterator_destroy(&i);
3688 /***
3689 * DESCRIPTION:
3690 * Draws listview items when in list display mode.
3692 * PARAMETER(S):
3693 * [I] infoPtr : valid pointer to the listview structure
3694 * [I] hdc : device context handle
3695 * [I] cdmode : custom draw mode
3697 * RETURN:
3698 * None
3700 static void LISTVIEW_RefreshList(LISTVIEW_INFO *infoPtr, HDC hdc, DWORD cdmode)
3702 POINT Origin, Position;
3703 ITERATOR i;
3705 /* Get scroll info once before loop */
3706 LISTVIEW_GetOrigin(infoPtr, &Origin);
3708 /* figure out what we need to draw */
3709 iterator_visibleitems(&i, infoPtr, hdc);
3711 while(iterator_prev(&i))
3713 LISTVIEW_GetItemOrigin(infoPtr, i.nItem, &Position);
3714 Position.x += Origin.x;
3715 Position.y += Origin.y;
3717 LISTVIEW_DrawItem(infoPtr, hdc, i.nItem, 0, Position, cdmode);
3719 iterator_destroy(&i);
3723 /***
3724 * DESCRIPTION:
3725 * Draws listview items.
3727 * PARAMETER(S):
3728 * [I] infoPtr : valid pointer to the listview structure
3729 * [I] hdc : device context handle
3731 * RETURN:
3732 * NoneX
3734 static void LISTVIEW_Refresh(LISTVIEW_INFO *infoPtr, HDC hdc)
3736 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
3737 COLORREF oldTextColor, oldClrTextBk, oldClrText;
3738 NMLVCUSTOMDRAW nmlvcd;
3739 HFONT hOldFont;
3740 DWORD cdmode;
3741 INT oldBkMode;
3742 RECT rcClient;
3744 LISTVIEW_DUMP(infoPtr);
3746 infoPtr->bIsDrawing = TRUE;
3748 /* save dc values we're gonna trash while drawing */
3749 hOldFont = SelectObject(hdc, infoPtr->hFont);
3750 oldBkMode = GetBkMode(hdc);
3751 infoPtr->clrTextBkDefault = GetBkColor(hdc);
3752 oldTextColor = GetTextColor(hdc);
3754 oldClrTextBk = infoPtr->clrTextBk;
3755 oldClrText = infoPtr->clrText;
3757 GetClientRect(infoPtr->hwndSelf, &rcClient);
3758 customdraw_fill(&nmlvcd, infoPtr, hdc, &rcClient);
3759 cdmode = notify_customdraw(infoPtr, CDDS_PREPAINT, &nmlvcd);
3760 if (cdmode & CDRF_SKIPDEFAULT) goto enddraw;
3762 /* Use these colors to draw the items */
3763 infoPtr->clrTextBk = nmlvcd.clrTextBk;
3764 infoPtr->clrText = nmlvcd.clrText;
3766 /* nothing to draw */
3767 if(infoPtr->nItemCount == 0) goto enddraw;
3769 if ((infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && (uView == LVS_REPORT))
3770 LISTVIEW_RefreshOwnerDraw(infoPtr, hdc);
3771 else
3773 if (uView == LVS_REPORT)
3774 LISTVIEW_RefreshReport(infoPtr, hdc, cdmode);
3775 else /* LVS_LIST, LVS_ICON or LVS_SMALLICON */
3776 LISTVIEW_RefreshList(infoPtr, hdc, cdmode);
3778 /* if we have a focus rect, draw it */
3779 if (infoPtr->bFocus)
3780 DrawFocusRect(hdc, &infoPtr->rcFocus);
3783 enddraw:
3784 if (cdmode & CDRF_NOTIFYPOSTPAINT)
3785 notify_customdraw(infoPtr, CDDS_POSTPAINT, &nmlvcd);
3787 infoPtr->clrTextBk = oldClrTextBk;
3788 infoPtr->clrText = oldClrText;
3790 SelectObject(hdc, hOldFont);
3791 SetBkMode(hdc, oldBkMode);
3792 SetBkColor(hdc, infoPtr->clrTextBkDefault);
3793 SetTextColor(hdc, oldTextColor);
3794 infoPtr->bIsDrawing = FALSE;
3798 /***
3799 * DESCRIPTION:
3800 * Calculates the approximate width and height of a given number of items.
3802 * PARAMETER(S):
3803 * [I] infoPtr : valid pointer to the listview structure
3804 * [I] nItemCount : number of items
3805 * [I] wWidth : width
3806 * [I] wHeight : height
3808 * RETURN:
3809 * Returns a DWORD. The width in the low word and the height in high word.
3811 static DWORD LISTVIEW_ApproximateViewRect(LISTVIEW_INFO *infoPtr, INT nItemCount,
3812 WORD wWidth, WORD wHeight)
3814 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
3815 INT nItemCountPerColumn = 1;
3816 INT nColumnCount = 0;
3817 DWORD dwViewRect = 0;
3819 if (nItemCount == -1)
3820 nItemCount = infoPtr->nItemCount;
3822 if (uView == LVS_LIST)
3824 if (wHeight == 0xFFFF)
3826 /* use current height */
3827 wHeight = infoPtr->rcList.bottom - infoPtr->rcList.top;
3830 if (wHeight < infoPtr->nItemHeight)
3831 wHeight = infoPtr->nItemHeight;
3833 if (nItemCount > 0)
3835 if (infoPtr->nItemHeight > 0)
3837 nItemCountPerColumn = wHeight / infoPtr->nItemHeight;
3838 if (nItemCountPerColumn == 0)
3839 nItemCountPerColumn = 1;
3841 if (nItemCount % nItemCountPerColumn != 0)
3842 nColumnCount = nItemCount / nItemCountPerColumn;
3843 else
3844 nColumnCount = nItemCount / nItemCountPerColumn + 1;
3848 /* Microsoft padding magic */
3849 wHeight = nItemCountPerColumn * infoPtr->nItemHeight + 2;
3850 wWidth = nColumnCount * infoPtr->nItemWidth + 2;
3852 dwViewRect = MAKELONG(wWidth, wHeight);
3854 else if (uView == LVS_REPORT)
3855 FIXME("uView == LVS_REPORT: not implemented\n");
3856 else if (uView == LVS_SMALLICON)
3857 FIXME("uView == LVS_SMALLICON: not implemented\n");
3858 else if (uView == LVS_ICON)
3859 FIXME("uView == LVS_ICON: not implemented\n");
3861 return dwViewRect;
3864 /* << LISTVIEW_CreateDragImage >> */
3867 /***
3868 * DESCRIPTION:
3869 * Removes all listview items and subitems.
3871 * PARAMETER(S):
3872 * [I] infoPtr : valid pointer to the listview structure
3874 * RETURN:
3875 * SUCCESS : TRUE
3876 * FAILURE : FALSE
3878 static BOOL LISTVIEW_DeleteAllItems(LISTVIEW_INFO *infoPtr)
3880 NMLISTVIEW nmlv;
3881 HDPA hdpaSubItems = NULL;
3882 BOOL bSuppress;
3883 ITEMHDR *hdrItem;
3884 INT i, j;
3886 TRACE("()\n");
3888 /* we do it directly, to avoid notifications */
3889 ranges_clear(infoPtr->selectionRanges);
3890 infoPtr->nSelectionMark = -1;
3891 infoPtr->nFocusedItem = -1;
3892 SetRectEmpty(&infoPtr->rcFocus);
3893 /* But we are supposed to leave nHotItem as is! */
3896 /* send LVN_DELETEALLITEMS notification */
3897 ZeroMemory(&nmlv, sizeof(NMLISTVIEW));
3898 nmlv.iItem = -1;
3899 bSuppress = notify_listview(infoPtr, LVN_DELETEALLITEMS, &nmlv);
3901 for (i = infoPtr->nItemCount - 1; i >= 0; i--)
3903 /* send LVN_DELETEITEM notification, if not supressed */
3904 if (!bSuppress)
3906 nmlv.iItem = i;
3907 notify_listview(infoPtr, LVN_DELETEITEM, &nmlv);
3909 if (!(infoPtr->dwStyle & LVS_OWNERDATA))
3911 hdpaSubItems = (HDPA)DPA_GetPtr(infoPtr->hdpaItems, i);
3912 for (j = 0; j < hdpaSubItems->nItemCount; j++)
3914 hdrItem = (ITEMHDR *)DPA_GetPtr(hdpaSubItems, j);
3915 if (is_textW(hdrItem->pszText)) COMCTL32_Free(hdrItem->pszText);
3916 COMCTL32_Free(hdrItem);
3918 DPA_Destroy(hdpaSubItems);
3919 DPA_DeletePtr(infoPtr->hdpaItems, i);
3921 DPA_DeletePtr(infoPtr->hdpaPosX, i);
3922 DPA_DeletePtr(infoPtr->hdpaPosY, i);
3923 infoPtr->nItemCount --;
3926 LISTVIEW_UpdateScroll(infoPtr);
3928 LISTVIEW_InvalidateList(infoPtr);
3930 return TRUE;
3933 /***
3934 * DESCRIPTION:
3935 * Scrolls, and updates the columns, when a column is changing width.
3937 * PARAMETER(S):
3938 * [I] infoPtr : valid pointer to the listview structure
3939 * [I] nColumn : column to scroll
3940 * [I] dx : amount of scroll, in pixels
3942 * RETURN:
3943 * None.
3945 static void LISTVIEW_ScrollColumns(LISTVIEW_INFO *infoPtr, INT nColumn, INT dx)
3947 COLUMN_INFO *lpColumnInfo;
3948 RECT rcOld, rcCol;
3949 INT nCol;
3951 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, min(nColumn, infoPtr->hdpaColumns->nItemCount - 1));
3952 rcCol = lpColumnInfo->rcHeader;
3953 if (nColumn >= infoPtr->hdpaColumns->nItemCount)
3954 rcCol.left = rcCol.right;
3956 /* ajust the other columns */
3957 for (nCol = nColumn; nCol < infoPtr->hdpaColumns->nItemCount; nCol++)
3959 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, nCol);
3960 lpColumnInfo->rcHeader.left += dx;
3961 lpColumnInfo->rcHeader.right += dx;
3964 /* do not update screen if not in report mode */
3965 if (!is_redrawing(infoPtr) || (infoPtr->dwStyle & LVS_TYPEMASK) != LVS_REPORT) return;
3967 /* if we have a focus, must first erase the focus rect */
3968 if (infoPtr->bFocus) LISTVIEW_ShowFocusRect(infoPtr, FALSE);
3970 /* Need to reset the item width when inserting a new column */
3971 infoPtr->nItemWidth += dx;
3973 LISTVIEW_UpdateScroll(infoPtr);
3975 /* scroll to cover the deleted column, and invalidate for redraw */
3976 rcOld = infoPtr->rcList;
3977 rcOld.left = rcCol.left;
3978 ScrollWindowEx(infoPtr->hwndSelf, dx, 0, &rcOld, &rcOld, 0, 0, SW_ERASE | SW_INVALIDATE);
3980 /* we can restore focus now */
3981 if (infoPtr->bFocus) LISTVIEW_ShowFocusRect(infoPtr, TRUE);
3984 /***
3985 * DESCRIPTION:
3986 * Removes a column from the listview control.
3988 * PARAMETER(S):
3989 * [I] infoPtr : valid pointer to the listview structure
3990 * [I] nColumn : column index
3992 * RETURN:
3993 * SUCCESS : TRUE
3994 * FAILURE : FALSE
3996 static BOOL LISTVIEW_DeleteColumn(LISTVIEW_INFO *infoPtr, INT nColumn)
3998 RECT rcCol;
4000 TRACE("nColumn=%d\n", nColumn);
4002 if (nColumn <= 0 || nColumn >= infoPtr->hdpaColumns->nItemCount) return FALSE;
4004 LISTVIEW_GetHeaderRect(infoPtr, nColumn, &rcCol);
4006 if (!Header_DeleteItem(infoPtr->hwndHeader, nColumn))
4007 return FALSE;
4009 COMCTL32_Free(DPA_GetPtr(infoPtr->hdpaColumns, nColumn));
4010 DPA_DeletePtr(infoPtr->hdpaColumns, nColumn);
4012 if (!(infoPtr->dwStyle & LVS_OWNERDATA))
4014 SUBITEM_INFO *lpSubItem, *lpDelItem;
4015 HDPA hdpaSubItems;
4016 INT nItem, nSubItem, i;
4018 for (nItem = 0; nItem < infoPtr->nItemCount; nItem++)
4020 hdpaSubItems = (HDPA)DPA_GetPtr(infoPtr->hdpaItems, nItem);
4021 nSubItem = 0;
4022 lpDelItem = 0;
4023 for (i = 1; i < hdpaSubItems->nItemCount; i++)
4025 lpSubItem = (SUBITEM_INFO *)DPA_GetPtr(hdpaSubItems, i);
4026 if (lpSubItem->iSubItem == nColumn)
4028 nSubItem = i;
4029 lpDelItem = lpSubItem;
4031 else if (lpSubItem->iSubItem > nColumn)
4033 lpSubItem->iSubItem--;
4037 /* if we found our subitem, zapp it */
4038 if (nSubItem > 0)
4040 /* free string */
4041 if (is_textW(lpDelItem->hdr.pszText))
4042 COMCTL32_Free(lpDelItem->hdr.pszText);
4044 /* free item */
4045 COMCTL32_Free(lpDelItem);
4047 /* free dpa memory */
4048 DPA_DeletePtr(hdpaSubItems, nSubItem);
4053 /* update the other column info */
4054 LISTVIEW_ScrollColumns(infoPtr, nColumn, -(rcCol.right - rcCol.left));
4056 return TRUE;
4059 /***
4060 * DESCRIPTION:
4061 * Invalidates the listview after an item's insertion or deletion.
4063 * PARAMETER(S):
4064 * [I] infoPtr : valid pointer to the listview structure
4065 * [I] nItem : item index
4066 * [I] dir : -1 if deleting, 1 if inserting
4068 * RETURN:
4069 * None
4071 static void LISTVIEW_ScrollOnInsert(LISTVIEW_INFO *infoPtr, INT nItem, INT dir)
4073 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
4074 INT nPerCol, nItemCol, nItemRow;
4075 RECT rcScroll;
4076 POINT Origin;
4078 /* if we don't refresh, what's the point of scrolling? */
4079 if (!is_redrawing(infoPtr)) return;
4081 assert (abs(dir) == 1);
4083 /* arrange icons if autoarrange is on */
4084 if (is_autoarrange(infoPtr))
4086 BOOL arrange = TRUE;
4087 if (dir < 0 && nItem >= infoPtr->nItemCount) arrange = FALSE;
4088 if (dir > 0 && nItem == infoPtr->nItemCount - 1) arrange = FALSE;
4089 if (arrange) LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
4092 /* scrollbars need updating */
4093 LISTVIEW_UpdateScroll(infoPtr);
4095 /* figure out the item's position */
4096 if (uView == LVS_REPORT)
4097 nPerCol = infoPtr->nItemCount + 1;
4098 else if (uView == LVS_LIST)
4099 nPerCol = LISTVIEW_GetCountPerColumn(infoPtr);
4100 else /* LVS_ICON, or LVS_SMALLICON */
4101 return;
4103 nItemCol = nItem / nPerCol;
4104 nItemRow = nItem % nPerCol;
4105 LISTVIEW_GetOrigin(infoPtr, &Origin);
4107 /* move the items below up a slot */
4108 rcScroll.left = nItemCol * infoPtr->nItemWidth;
4109 rcScroll.top = nItemRow * infoPtr->nItemHeight;
4110 rcScroll.right = rcScroll.left + infoPtr->nItemWidth;
4111 rcScroll.bottom = nPerCol * infoPtr->nItemHeight;
4112 OffsetRect(&rcScroll, Origin.x, Origin.y);
4113 TRACE("rcScroll=%s, dx=%d\n", debugrect(&rcScroll), dir * infoPtr->nItemHeight);
4114 if (IntersectRect(&rcScroll, &rcScroll, &infoPtr->rcList))
4116 TRACE("Scrolling rcScroll=%s, rcList=%s\n", debugrect(&rcScroll), debugrect(&infoPtr->rcList));
4117 ScrollWindowEx(infoPtr->hwndSelf, 0, dir * infoPtr->nItemHeight,
4118 &rcScroll, &rcScroll, 0, 0, SW_ERASE | SW_INVALIDATE);
4121 /* report has only that column, so we're done */
4122 if (uView == LVS_REPORT) return;
4124 /* now for LISTs, we have to deal with the columns to the right */
4125 rcScroll.left = (nItemCol + 1) * infoPtr->nItemWidth;
4126 rcScroll.top = 0;
4127 rcScroll.right = (infoPtr->nItemCount / nPerCol + 1) * infoPtr->nItemWidth;
4128 rcScroll.bottom = nPerCol * infoPtr->nItemHeight;
4129 OffsetRect(&rcScroll, Origin.x, Origin.y);
4130 if (IntersectRect(&rcScroll, &rcScroll, &infoPtr->rcList))
4131 ScrollWindowEx(infoPtr->hwndSelf, 0, dir * infoPtr->nItemHeight,
4132 &rcScroll, &rcScroll, 0, 0, SW_ERASE | SW_INVALIDATE);
4135 /***
4136 * DESCRIPTION:
4137 * Removes an item from the listview control.
4139 * PARAMETER(S):
4140 * [I] infoPtr : valid pointer to the listview structure
4141 * [I] nItem : item index
4143 * RETURN:
4144 * SUCCESS : TRUE
4145 * FAILURE : FALSE
4147 static BOOL LISTVIEW_DeleteItem(LISTVIEW_INFO *infoPtr, INT nItem)
4149 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
4150 NMLISTVIEW nmlv;
4151 LVITEMW item;
4153 TRACE("(nItem=%d)\n", nItem);
4155 if (nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
4157 /* remove selection, and focus */
4158 item.state = 0;
4159 item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
4160 LISTVIEW_SetItemState(infoPtr, nItem, &item);
4162 /* send LVN_DELETEITEM notification. */
4163 ZeroMemory(&nmlv, sizeof (NMLISTVIEW));
4164 nmlv.iItem = nItem;
4165 notify_listview(infoPtr, LVN_DELETEITEM, &nmlv);
4167 /* we need to do this here, because we'll be deleting stuff */
4168 if (uView == LVS_SMALLICON || uView == LVS_ICON)
4169 LISTVIEW_InvalidateItem(infoPtr, nItem);
4171 if (!(infoPtr->dwStyle & LVS_OWNERDATA))
4173 HDPA hdpaSubItems;
4174 ITEMHDR *hdrItem;
4175 INT i;
4177 hdpaSubItems = (HDPA)DPA_DeletePtr(infoPtr->hdpaItems, nItem);
4178 for (i = 0; i < hdpaSubItems->nItemCount; i++)
4180 hdrItem = (ITEMHDR *)DPA_GetPtr(hdpaSubItems, i);
4181 if (is_textW(hdrItem->pszText)) COMCTL32_Free(hdrItem->pszText);
4182 COMCTL32_Free(hdrItem);
4184 DPA_Destroy(hdpaSubItems);
4187 if (uView == LVS_SMALLICON || uView == LVS_ICON)
4189 DPA_DeletePtr(infoPtr->hdpaPosX, nItem);
4190 DPA_DeletePtr(infoPtr->hdpaPosY, nItem);
4193 infoPtr->nItemCount--;
4194 LISTVIEW_ShiftIndices(infoPtr, nItem, -1);
4196 /* now is the invalidation fun */
4197 LISTVIEW_ScrollOnInsert(infoPtr, nItem, -1);
4198 return TRUE;
4202 /***
4203 * DESCRIPTION:
4204 * Callback implementation for editlabel control
4206 * PARAMETER(S):
4207 * [I] infoPtr : valid pointer to the listview structure
4208 * [I] pszText : modified text
4209 * [I] isW : TRUE if psxText is Unicode, FALSE if it's ANSI
4211 * RETURN:
4212 * SUCCESS : TRUE
4213 * FAILURE : FALSE
4215 static BOOL LISTVIEW_EndEditLabelT(LISTVIEW_INFO *infoPtr, LPWSTR pszText, BOOL isW)
4217 NMLVDISPINFOW dispInfo;
4219 TRACE("(pszText=%s, isW=%d)\n", debugtext_t(pszText, isW), isW);
4221 ZeroMemory(&dispInfo, sizeof(dispInfo));
4222 dispInfo.item.mask = LVIF_PARAM | LVIF_STATE;
4223 dispInfo.item.iItem = infoPtr->nEditLabelItem;
4224 dispInfo.item.iSubItem = 0;
4225 dispInfo.item.stateMask = ~0;
4226 if (!LISTVIEW_GetItemW(infoPtr, &dispInfo.item)) return FALSE;
4227 /* add the text from the edit in */
4228 dispInfo.item.mask |= LVIF_TEXT;
4229 dispInfo.item.pszText = pszText;
4230 dispInfo.item.cchTextMax = textlenT(pszText, isW);
4232 /* Do we need to update the Item Text */
4233 if (!notify_dispinfoT(infoPtr, LVN_ENDLABELEDITW, &dispInfo, isW)) return FALSE;
4234 if (!pszText) return TRUE;
4236 ZeroMemory(&dispInfo, sizeof(dispInfo));
4237 dispInfo.item.mask = LVIF_TEXT;
4238 dispInfo.item.iItem = infoPtr->nEditLabelItem;
4239 dispInfo.item.iSubItem = 0;
4240 dispInfo.item.pszText = pszText;
4241 dispInfo.item.cchTextMax = textlenT(pszText, isW);
4242 return LISTVIEW_SetItemT(infoPtr, &dispInfo.item, isW);
4245 /***
4246 * DESCRIPTION:
4247 * Begin in place editing of specified list view item
4249 * PARAMETER(S):
4250 * [I] infoPtr : valid pointer to the listview structure
4251 * [I] nItem : item index
4252 * [I] isW : TRUE if it's a Unicode req, FALSE if ASCII
4254 * RETURN:
4255 * SUCCESS : TRUE
4256 * FAILURE : FALSE
4258 static HWND LISTVIEW_EditLabelT(LISTVIEW_INFO *infoPtr, INT nItem, BOOL isW)
4260 WCHAR szDispText[DISP_TEXT_SIZE] = { 0 };
4261 NMLVDISPINFOW dispInfo;
4262 RECT rect;
4264 TRACE("(nItem=%d, isW=%d)\n", nItem, isW);
4266 if (~infoPtr->dwStyle & LVS_EDITLABELS) return 0;
4267 if (nItem < 0 || nItem >= infoPtr->nItemCount) return 0;
4269 infoPtr->nEditLabelItem = nItem;
4271 /* Is the EditBox still there, if so remove it */
4272 if(infoPtr->hwndEdit != 0)
4274 SetFocus(infoPtr->hwndSelf);
4275 infoPtr->hwndEdit = 0;
4278 LISTVIEW_SetSelection(infoPtr, nItem);
4279 LISTVIEW_SetItemFocus(infoPtr, nItem);
4281 rect.left = LVIR_LABEL;
4282 if (!LISTVIEW_GetItemRect(infoPtr, nItem, &rect)) return 0;
4284 ZeroMemory(&dispInfo, sizeof(dispInfo));
4285 dispInfo.item.mask = LVIF_PARAM | LVIF_STATE | LVIF_TEXT;
4286 dispInfo.item.iItem = nItem;
4287 dispInfo.item.iSubItem = 0;
4288 dispInfo.item.stateMask = ~0;
4289 dispInfo.item.pszText = szDispText;
4290 dispInfo.item.cchTextMax = DISP_TEXT_SIZE;
4291 if (!LISTVIEW_GetItemT(infoPtr, &dispInfo.item, isW)) return 0;
4293 infoPtr->hwndEdit = CreateEditLabelT(infoPtr, dispInfo.item.pszText, WS_VISIBLE,
4294 rect.left-2, rect.top-1, 0, rect.bottom - rect.top+2, isW);
4295 if (!infoPtr->hwndEdit) return 0;
4297 if (notify_dispinfoT(infoPtr, LVN_BEGINLABELEDITW, &dispInfo, isW))
4299 SendMessageW(infoPtr->hwndEdit, WM_CLOSE, 0, 0);
4300 infoPtr->hwndEdit = 0;
4301 return 0;
4304 ShowWindow(infoPtr->hwndEdit, SW_NORMAL);
4305 SetFocus(infoPtr->hwndEdit);
4306 SendMessageW(infoPtr->hwndEdit, EM_SETSEL, 0, -1);
4307 return infoPtr->hwndEdit;
4311 /***
4312 * DESCRIPTION:
4313 * Ensures the specified item is visible, scrolling into view if necessary.
4315 * PARAMETER(S):
4316 * [I] infoPtr : valid pointer to the listview structure
4317 * [I] nItem : item index
4318 * [I] bPartial : partially or entirely visible
4320 * RETURN:
4321 * SUCCESS : TRUE
4322 * FAILURE : FALSE
4324 static BOOL LISTVIEW_EnsureVisible(LISTVIEW_INFO *infoPtr, INT nItem, BOOL bPartial)
4326 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
4327 INT nScrollPosHeight = 0;
4328 INT nScrollPosWidth = 0;
4329 INT nHorzAdjust = 0;
4330 INT nVertAdjust = 0;
4331 INT nHorzDiff = 0;
4332 INT nVertDiff = 0;
4333 RECT rcItem, rcTemp;
4335 rcItem.left = LVIR_BOUNDS;
4336 if (!LISTVIEW_GetItemRect(infoPtr, nItem, &rcItem)) return FALSE;
4338 if (bPartial && IntersectRect(&rcTemp, &infoPtr->rcList, &rcItem)) return TRUE;
4340 if (rcItem.left < infoPtr->rcList.left || rcItem.right > infoPtr->rcList.right)
4342 /* scroll left/right, but in LVS_REPORT mode */
4343 if (uView == LVS_LIST)
4344 nScrollPosWidth = infoPtr->nItemWidth;
4345 else if ((uView == LVS_SMALLICON) || (uView == LVS_ICON))
4346 nScrollPosWidth = 1;
4348 if (rcItem.left < infoPtr->rcList.left)
4350 nHorzAdjust = -1;
4351 if (uView != LVS_REPORT) nHorzDiff = rcItem.left - infoPtr->rcList.left;
4353 else
4355 nHorzAdjust = 1;
4356 if (uView != LVS_REPORT) nHorzDiff = rcItem.right - infoPtr->rcList.right;
4360 if (rcItem.top < infoPtr->rcList.top || rcItem.bottom > infoPtr->rcList.bottom)
4362 /* scroll up/down, but not in LVS_LIST mode */
4363 if (uView == LVS_REPORT)
4364 nScrollPosHeight = infoPtr->nItemHeight;
4365 else if ((uView == LVS_ICON) || (uView == LVS_SMALLICON))
4366 nScrollPosHeight = 1;
4368 if (rcItem.top < infoPtr->rcList.top)
4370 nVertAdjust = -1;
4371 if (uView != LVS_LIST) nVertDiff = rcItem.top - infoPtr->rcList.top;
4373 else
4375 nVertAdjust = 1;
4376 if (uView != LVS_LIST) nVertDiff = rcItem.bottom - infoPtr->rcList.bottom;
4380 if (!nScrollPosWidth && !nScrollPosHeight) return TRUE;
4382 if (nScrollPosWidth)
4384 INT diff = nHorzDiff / nScrollPosWidth;
4385 if (nHorzDiff % nScrollPosWidth) diff += nHorzAdjust;
4386 LISTVIEW_HScroll(infoPtr, SB_INTERNAL, diff, 0);
4389 if (nScrollPosHeight)
4391 INT diff = nVertDiff / nScrollPosHeight;
4392 if (nVertDiff % nScrollPosHeight) diff += nVertAdjust;
4393 LISTVIEW_VScroll(infoPtr, SB_INTERNAL, diff, 0);
4396 return TRUE;
4399 /***
4400 * DESCRIPTION:
4401 * Searches for an item with specific characteristics.
4403 * PARAMETER(S):
4404 * [I] hwnd : window handle
4405 * [I] nStart : base item index
4406 * [I] lpFindInfo : item information to look for
4408 * RETURN:
4409 * SUCCESS : index of item
4410 * FAILURE : -1
4412 static INT LISTVIEW_FindItemW(LISTVIEW_INFO *infoPtr, INT nStart,
4413 const LVFINDINFOW *lpFindInfo)
4415 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
4416 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
4417 BOOL bWrap = FALSE, bNearest = FALSE;
4418 INT nItem = nStart + 1, nLast = infoPtr->nItemCount, nNearestItem = -1;
4419 ULONG xdist, ydist, dist, mindist = 0x7fffffff;
4420 POINT Position, Destination;
4421 LVITEMW lvItem;
4423 if (!lpFindInfo || nItem < 0) return -1;
4425 lvItem.mask = 0;
4426 if (lpFindInfo->flags & (LVFI_STRING | LVFI_PARTIAL))
4428 lvItem.mask |= LVIF_TEXT;
4429 lvItem.pszText = szDispText;
4430 lvItem.cchTextMax = DISP_TEXT_SIZE;
4433 if (lpFindInfo->flags & LVFI_WRAP)
4434 bWrap = TRUE;
4436 if ((lpFindInfo->flags & LVFI_NEARESTXY) &&
4437 (uView == LVS_ICON || uView ==LVS_SMALLICON))
4439 POINT Origin;
4440 RECT rcArea;
4442 LISTVIEW_GetOrigin(infoPtr, &Origin);
4443 Destination.x = lpFindInfo->pt.x - Origin.x;
4444 Destination.y = lpFindInfo->pt.y - Origin.y;
4445 switch(lpFindInfo->vkDirection)
4447 case VK_DOWN: Destination.y += infoPtr->nItemHeight; break;
4448 case VK_UP: Destination.y -= infoPtr->nItemHeight; break;
4449 case VK_RIGHT: Destination.x += infoPtr->nItemWidth; break;
4450 case VK_LEFT: Destination.x -= infoPtr->nItemWidth; break;
4451 case VK_HOME: Destination.x = Destination.y = 0; break;
4452 case VK_NEXT: Destination.y += infoPtr->rcList.bottom - infoPtr->rcList.top; break;
4453 case VK_PRIOR: Destination.y -= infoPtr->rcList.bottom - infoPtr->rcList.top; break;
4454 case VK_END:
4455 LISTVIEW_GetAreaRect(infoPtr, &rcArea);
4456 Destination.x = rcArea.right;
4457 Destination.y = rcArea.bottom;
4458 break;
4459 default: ERR("Unknown vkDirection=%d\n", lpFindInfo->vkDirection);
4461 bNearest = TRUE;
4464 /* if LVFI_PARAM is specified, all other flags are ignored */
4465 if (lpFindInfo->flags & LVFI_PARAM)
4467 lvItem.mask |= LVIF_PARAM;
4468 bNearest = FALSE;
4469 lvItem.mask &= ~LVIF_TEXT;
4472 again:
4473 for (; nItem < nLast; nItem++)
4475 lvItem.iItem = nItem;
4476 lvItem.iSubItem = 0;
4477 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) continue;
4479 if (lvItem.mask & LVIF_PARAM)
4481 if (lpFindInfo->lParam == lvItem.lParam)
4482 return nItem;
4483 else
4484 continue;
4487 if (lvItem.mask & LVIF_TEXT)
4489 if (lpFindInfo->flags & LVFI_PARTIAL)
4491 if (strstrW(lvItem.pszText, lpFindInfo->psz) == NULL) continue;
4493 else
4495 if (lstrcmpW(lvItem.pszText, lpFindInfo->psz) != 0) continue;
4499 if (!bNearest) return nItem;
4501 /* This is very inefficient. To do a good job here,
4502 * we need a sorted array of (x,y) item positions */
4503 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position);
4505 /* compute the distance^2 to the destination */
4506 xdist = Destination.x - Position.x;
4507 ydist = Destination.y - Position.y;
4508 dist = xdist * xdist + ydist * ydist;
4510 /* remember the distance, and item if it's closer */
4511 if (dist < mindist)
4513 mindist = dist;
4514 nNearestItem = nItem;
4518 if (bWrap)
4520 nItem = 0;
4521 nLast = min(nStart + 1, infoPtr->nItemCount);
4522 bWrap = FALSE;
4523 goto again;
4526 return nNearestItem;
4529 /***
4530 * DESCRIPTION:
4531 * Searches for an item with specific characteristics.
4533 * PARAMETER(S):
4534 * [I] hwnd : window handle
4535 * [I] nStart : base item index
4536 * [I] lpFindInfo : item information to look for
4538 * RETURN:
4539 * SUCCESS : index of item
4540 * FAILURE : -1
4542 static INT LISTVIEW_FindItemA(LISTVIEW_INFO *infoPtr, INT nStart,
4543 const LVFINDINFOA *lpFindInfo)
4545 BOOL hasText = lpFindInfo->flags & (LVFI_STRING | LVFI_PARTIAL);
4546 LVFINDINFOW fiw;
4547 INT res;
4549 memcpy(&fiw, lpFindInfo, sizeof(fiw));
4550 if (hasText) fiw.psz = textdupTtoW((LPCWSTR)lpFindInfo->psz, FALSE);
4551 res = LISTVIEW_FindItemW(infoPtr, nStart, &fiw);
4552 if (hasText) textfreeT((LPWSTR)fiw.psz, FALSE);
4553 return res;
4556 /***
4557 * DESCRIPTION:
4558 * Retrieves the background image of the listview control.
4560 * PARAMETER(S):
4561 * [I] infoPtr : valid pointer to the listview structure
4562 * [O] lpBkImage : background image attributes
4564 * RETURN:
4565 * SUCCESS : TRUE
4566 * FAILURE : FALSE
4568 /* static BOOL LISTVIEW_GetBkImage(LISTVIEW_INFO *infoPtr, LPLVBKIMAGE lpBkImage) */
4569 /* { */
4570 /* FIXME (listview, "empty stub!\n"); */
4571 /* return FALSE; */
4572 /* } */
4574 /***
4575 * DESCRIPTION:
4576 * Retrieves column attributes.
4578 * PARAMETER(S):
4579 * [I] infoPtr : valid pointer to the listview structure
4580 * [I] nColumn : column index
4581 * [IO] lpColumn : column information
4582 * [I] isW : if TRUE, then lpColumn is a LPLVCOLUMNW
4583 * otherwise it is in fact a LPLVCOLUMNA
4585 * RETURN:
4586 * SUCCESS : TRUE
4587 * FAILURE : FALSE
4589 static BOOL LISTVIEW_GetColumnT(LISTVIEW_INFO *infoPtr, INT nColumn, LPLVCOLUMNW lpColumn, BOOL isW)
4591 COLUMN_INFO *lpColumnInfo;
4592 HDITEMW hdi;
4594 if (!lpColumn || nColumn < 0 || nColumn >= infoPtr->hdpaColumns->nItemCount) return FALSE;
4595 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, nColumn);
4597 /* initialize memory */
4598 ZeroMemory(&hdi, sizeof(hdi));
4600 if (lpColumn->mask & LVCF_TEXT)
4602 hdi.mask |= HDI_TEXT;
4603 hdi.pszText = lpColumn->pszText;
4604 hdi.cchTextMax = lpColumn->cchTextMax;
4607 if (lpColumn->mask & LVCF_IMAGE)
4608 hdi.mask |= HDI_IMAGE;
4610 if (lpColumn->mask & LVCF_ORDER)
4611 hdi.mask |= HDI_ORDER;
4613 if (!SendMessageW(infoPtr->hwndHeader, isW ? HDM_GETITEMW : HDM_GETITEMA, nColumn, (LPARAM)&hdi)) return FALSE;
4615 if (lpColumn->mask & LVCF_FMT)
4616 lpColumn->fmt = lpColumnInfo->fmt;
4618 if (lpColumn->mask & LVCF_WIDTH)
4619 lpColumn->cx = lpColumnInfo->rcHeader.right - lpColumnInfo->rcHeader.left;
4621 if (lpColumn->mask & LVCF_IMAGE)
4622 lpColumn->iImage = hdi.iImage;
4624 if (lpColumn->mask & LVCF_ORDER)
4625 lpColumn->iOrder = hdi.iOrder;
4627 return TRUE;
4631 static BOOL LISTVIEW_GetColumnOrderArray(LISTVIEW_INFO *infoPtr, INT iCount, LPINT lpiArray)
4633 INT i;
4635 if (!lpiArray)
4636 return FALSE;
4638 /* FIXME: little hack */
4639 for (i = 0; i < iCount; i++)
4640 lpiArray[i] = i;
4642 return TRUE;
4645 /***
4646 * DESCRIPTION:
4647 * Retrieves the column width.
4649 * PARAMETER(S):
4650 * [I] infoPtr : valid pointer to the listview structure
4651 * [I] int : column index
4653 * RETURN:
4654 * SUCCESS : column width
4655 * FAILURE : zero
4657 static INT LISTVIEW_GetColumnWidth(LISTVIEW_INFO *infoPtr, INT nColumn)
4659 INT nColumnWidth = 0;
4660 RECT rcHeader;
4662 TRACE("nColumn=%d\n", nColumn);
4664 /* we have a 'column' in LIST and REPORT mode only */
4665 switch(infoPtr->dwStyle & LVS_TYPEMASK)
4667 case LVS_LIST:
4668 nColumnWidth = infoPtr->nItemWidth;
4669 break;
4670 case LVS_REPORT:
4671 if (nColumn < 0 || nColumn >= infoPtr->hdpaColumns->nItemCount) return 0;
4672 LISTVIEW_GetHeaderRect(infoPtr, nColumn, &rcHeader);
4673 nColumnWidth = rcHeader.right - rcHeader.left;
4674 break;
4677 TRACE("nColumnWidth=%d\n", nColumnWidth);
4678 return nColumnWidth;
4681 /***
4682 * DESCRIPTION:
4683 * In list or report display mode, retrieves the number of items that can fit
4684 * vertically in the visible area. In icon or small icon display mode,
4685 * retrieves the total number of visible items.
4687 * PARAMETER(S):
4688 * [I] infoPtr : valid pointer to the listview structure
4690 * RETURN:
4691 * Number of fully visible items.
4693 static INT LISTVIEW_GetCountPerPage(LISTVIEW_INFO *infoPtr)
4695 switch (infoPtr->dwStyle & LVS_TYPEMASK)
4697 case LVS_ICON:
4698 case LVS_SMALLICON:
4699 return infoPtr->nItemCount;
4700 case LVS_REPORT:
4701 return LISTVIEW_GetCountPerColumn(infoPtr);
4702 case LVS_LIST:
4703 return LISTVIEW_GetCountPerRow(infoPtr) * LISTVIEW_GetCountPerColumn(infoPtr);
4705 assert(FALSE);
4708 /***
4709 * DESCRIPTION:
4710 * Retrieves an image list handle.
4712 * PARAMETER(S):
4713 * [I] infoPtr : valid pointer to the listview structure
4714 * [I] nImageList : image list identifier
4716 * RETURN:
4717 * SUCCESS : image list handle
4718 * FAILURE : NULL
4720 static HIMAGELIST LISTVIEW_GetImageList(LISTVIEW_INFO *infoPtr, INT nImageList)
4722 switch (nImageList)
4724 case LVSIL_NORMAL: return infoPtr->himlNormal;
4725 case LVSIL_SMALL: return infoPtr->himlSmall;
4726 case LVSIL_STATE: return infoPtr->himlState;
4728 return NULL;
4731 /* LISTVIEW_GetISearchString */
4733 /***
4734 * DESCRIPTION:
4735 * Retrieves item attributes.
4737 * PARAMETER(S):
4738 * [I] hwnd : window handle
4739 * [IO] lpLVItem : item info
4740 * [I] isW : if TRUE, then lpLVItem is a LPLVITEMW,
4741 * if FALSE, the lpLVItem is a LPLVITEMA.
4743 * NOTE:
4744 * This is the internal 'GetItem' interface -- it tries to
4745 * be smart, and avoids text copies, if possible, by modifing
4746 * lpLVItem->pszText to point to the text string. Please note
4747 * that this is not always possible (e.g. OWNERDATA), so on
4748 * entry you *must* supply valid values for pszText, and cchTextMax.
4749 * The only difference to the documented interface is that upon
4750 * return, you should use *only* the lpLVItem->pszText, rather than
4751 * the buffer pointer you provided on input. Most code already does
4752 * that, so it's not a problem.
4753 * For the two cases when the text must be copied (that is,
4754 * for LVM_GETITEM, and LVMGETITEMTEXT), use LISTVIEW_GetItemExtT.
4756 * RETURN:
4757 * SUCCESS : TRUE
4758 * FAILURE : FALSE
4760 static BOOL LISTVIEW_GetItemT(LISTVIEW_INFO *infoPtr, LPLVITEMW lpLVItem, BOOL isW)
4762 ITEMHDR callbackHdr = { LPSTR_TEXTCALLBACKW, I_IMAGECALLBACK };
4763 NMLVDISPINFOW dispInfo;
4764 ITEM_INFO *lpItem;
4765 ITEMHDR* pItemHdr;
4766 HDPA hdpaSubItems;
4768 TRACE("(lpLVItem=%s, isW=%d)\n", debuglvitem_t(lpLVItem, isW), isW);
4770 if (!lpLVItem || lpLVItem->iItem < 0 || lpLVItem->iItem >= infoPtr->nItemCount)
4771 return FALSE;
4773 if (lpLVItem->mask == 0) return TRUE;
4775 /* a quick optimization if all we're asked is the focus state
4776 * these queries are worth optimising since they are common,
4777 * and can be answered in constant time, without the heavy accesses */
4778 if ( (lpLVItem->mask == LVIF_STATE) && (lpLVItem->stateMask == LVIS_FOCUSED) &&
4779 !(infoPtr->uCallbackMask & LVIS_FOCUSED) )
4781 lpLVItem->state = 0;
4782 if (infoPtr->nFocusedItem == lpLVItem->iItem)
4783 lpLVItem->state |= LVIS_FOCUSED;
4784 return TRUE;
4787 ZeroMemory(&dispInfo, sizeof(dispInfo));
4789 /* if the app stores all the data, handle it separately */
4790 if (infoPtr->dwStyle & LVS_OWNERDATA)
4792 dispInfo.item.state = 0;
4794 /* apprently, we should not callback for lParam in LVS_OWNERDATA */
4795 if ((lpLVItem->mask & ~(LVIF_STATE | LVIF_PARAM)) || infoPtr->uCallbackMask)
4797 /* NOTE: copy only fields which we _know_ are initialized, some apps
4798 * depend on the uninitialized fields being 0 */
4799 dispInfo.item.mask = lpLVItem->mask & ~LVIF_PARAM;
4800 dispInfo.item.iItem = lpLVItem->iItem;
4801 dispInfo.item.iSubItem = lpLVItem->iSubItem;
4802 if (lpLVItem->mask & LVIF_TEXT)
4804 dispInfo.item.pszText = lpLVItem->pszText;
4805 dispInfo.item.cchTextMax = lpLVItem->cchTextMax;
4807 if (lpLVItem->mask & LVIF_STATE)
4808 dispInfo.item.stateMask = lpLVItem->stateMask & infoPtr->uCallbackMask;
4809 notify_dispinfoT(infoPtr, LVN_GETDISPINFOW, &dispInfo, isW);
4810 dispInfo.item.stateMask = lpLVItem->stateMask;
4811 *lpLVItem = dispInfo.item;
4812 TRACE(" getdispinfo(1):lpLVItem=%s\n", debuglvitem_t(lpLVItem, isW));
4815 /* make sure lParam is zeroed out */
4816 if (lpLVItem->mask & LVIF_PARAM) lpLVItem->lParam = 0;
4818 /* we store only a little state, so if we're not asked, we're done */
4819 if (!(lpLVItem->mask & LVIF_STATE) || lpLVItem->iSubItem) return TRUE;
4821 /* if focus is handled by us, report it */
4822 if ( lpLVItem->stateMask & ~infoPtr->uCallbackMask & LVIS_FOCUSED )
4824 lpLVItem->state &= ~LVIS_FOCUSED;
4825 if (infoPtr->nFocusedItem == lpLVItem->iItem)
4826 lpLVItem->state |= LVIS_FOCUSED;
4829 /* and do the same for selection, if we handle it */
4830 if ( lpLVItem->stateMask & ~infoPtr->uCallbackMask & LVIS_SELECTED )
4832 lpLVItem->state &= ~LVIS_SELECTED;
4833 if (ranges_contain(infoPtr->selectionRanges, lpLVItem->iItem))
4834 lpLVItem->state |= LVIS_SELECTED;
4837 return TRUE;
4840 /* find the item and subitem structures before we proceed */
4841 hdpaSubItems = (HDPA)DPA_GetPtr(infoPtr->hdpaItems, lpLVItem->iItem);
4842 lpItem = (ITEM_INFO *)DPA_GetPtr(hdpaSubItems, 0);
4843 assert (lpItem);
4845 if (lpLVItem->iSubItem)
4847 SUBITEM_INFO *lpSubItem = LISTVIEW_GetSubItemPtr(hdpaSubItems, lpLVItem->iSubItem);
4848 pItemHdr = lpSubItem ? &lpSubItem->hdr : &callbackHdr;
4850 else
4851 pItemHdr = &lpItem->hdr;
4853 /* Do we need to query the state from the app? */
4854 if ((lpLVItem->mask & LVIF_STATE) && infoPtr->uCallbackMask && lpLVItem->iSubItem == 0)
4856 dispInfo.item.mask |= LVIF_STATE;
4857 dispInfo.item.stateMask = infoPtr->uCallbackMask;
4860 /* Do we need to enquire about the image? */
4861 if ((lpLVItem->mask & LVIF_IMAGE) && pItemHdr->iImage == I_IMAGECALLBACK)
4862 dispInfo.item.mask |= LVIF_IMAGE;
4864 /* Apps depend on calling back for text if it is NULL or LPSTR_TEXTCALLBACKW */
4865 if ((lpLVItem->mask & LVIF_TEXT) && !is_textW(pItemHdr->pszText))
4867 dispInfo.item.mask |= LVIF_TEXT;
4868 dispInfo.item.pszText = lpLVItem->pszText;
4869 dispInfo.item.cchTextMax = lpLVItem->cchTextMax;
4870 if (dispInfo.item.pszText && dispInfo.item.cchTextMax > 0)
4871 *dispInfo.item.pszText = '\0';
4874 /* If we don't have all the requested info, query the application */
4875 if (dispInfo.item.mask != 0)
4877 dispInfo.item.iItem = lpLVItem->iItem;
4878 dispInfo.item.iSubItem = lpLVItem->iSubItem;
4879 dispInfo.item.lParam = lpItem->lParam;
4880 notify_dispinfoT(infoPtr, LVN_GETDISPINFOW, &dispInfo, isW);
4881 TRACE(" getdispinfo(2):item=%s\n", debuglvitem_t(&dispInfo.item, isW));
4884 /* we should not store values for subitems */
4885 if (lpLVItem->iSubItem) dispInfo.item.mask &= ~LVIF_DI_SETITEM;
4887 /* Now, handle the iImage field */
4888 if (dispInfo.item.mask & LVIF_IMAGE)
4890 lpLVItem->iImage = dispInfo.item.iImage;
4891 if ((dispInfo.item.mask & LVIF_DI_SETITEM) && pItemHdr->iImage == I_IMAGECALLBACK)
4892 pItemHdr->iImage = dispInfo.item.iImage;
4894 else if (lpLVItem->mask & LVIF_IMAGE)
4895 lpLVItem->iImage = pItemHdr->iImage;
4897 /* The pszText field */
4898 if (dispInfo.item.mask & LVIF_TEXT)
4900 if ((dispInfo.item.mask & LVIF_DI_SETITEM) && pItemHdr->pszText)
4901 textsetptrT(&pItemHdr->pszText, dispInfo.item.pszText, isW);
4903 lpLVItem->pszText = dispInfo.item.pszText;
4905 else if (lpLVItem->mask & LVIF_TEXT)
4907 if (isW) lpLVItem->pszText = pItemHdr->pszText;
4908 else textcpynT(lpLVItem->pszText, isW, pItemHdr->pszText, TRUE, lpLVItem->cchTextMax);
4911 /* if this is a subitem, we're done */
4912 if (lpLVItem->iSubItem) return TRUE;
4914 /* Next is the lParam field */
4915 if (dispInfo.item.mask & LVIF_PARAM)
4917 lpLVItem->lParam = dispInfo.item.lParam;
4918 if ((dispInfo.item.mask & LVIF_DI_SETITEM))
4919 lpItem->lParam = dispInfo.item.lParam;
4921 else if (lpLVItem->mask & LVIF_PARAM)
4922 lpLVItem->lParam = lpItem->lParam;
4924 /* ... the state field (this one is different due to uCallbackmask) */
4925 if (lpLVItem->mask & LVIF_STATE)
4927 lpLVItem->state = lpItem->state;
4928 if (dispInfo.item.mask & LVIF_STATE)
4930 lpLVItem->state &= ~dispInfo.item.stateMask;
4931 lpLVItem->state |= (dispInfo.item.state & dispInfo.item.stateMask);
4933 if ( lpLVItem->stateMask & ~infoPtr->uCallbackMask & LVIS_FOCUSED )
4935 lpLVItem->state &= ~LVIS_FOCUSED;
4936 if (infoPtr->nFocusedItem == lpLVItem->iItem)
4937 lpLVItem->state |= LVIS_FOCUSED;
4939 if ( lpLVItem->stateMask & ~infoPtr->uCallbackMask & LVIS_SELECTED )
4941 lpLVItem->state &= ~LVIS_SELECTED;
4942 if (ranges_contain(infoPtr->selectionRanges, lpLVItem->iItem))
4943 lpLVItem->state |= LVIS_SELECTED;
4947 /* and last, but not least, the indent field */
4948 if (lpLVItem->mask & LVIF_INDENT)
4949 lpLVItem->iIndent = lpItem->iIndent;
4951 return TRUE;
4954 /***
4955 * DESCRIPTION:
4956 * Retrieves item attributes.
4958 * PARAMETER(S):
4959 * [I] hwnd : window handle
4960 * [IO] lpLVItem : item info
4961 * [I] isW : if TRUE, then lpLVItem is a LPLVITEMW,
4962 * if FALSE, the lpLVItem is a LPLVITEMA.
4964 * NOTE:
4965 * This is the external 'GetItem' interface -- it properly copies
4966 * the text in the provided buffer.
4968 * RETURN:
4969 * SUCCESS : TRUE
4970 * FAILURE : FALSE
4972 static BOOL LISTVIEW_GetItemExtT(LISTVIEW_INFO *infoPtr, LPLVITEMW lpLVItem, BOOL isW)
4974 LPWSTR pszText;
4975 BOOL bResult;
4977 if (!lpLVItem || lpLVItem->iItem < 0 || lpLVItem->iItem >= infoPtr->nItemCount)
4978 return FALSE;
4980 pszText = lpLVItem->pszText;
4981 bResult = LISTVIEW_GetItemT(infoPtr, lpLVItem, isW);
4982 if (bResult && lpLVItem->pszText != pszText)
4983 textcpynT(pszText, isW, lpLVItem->pszText, isW, lpLVItem->cchTextMax);
4984 lpLVItem->pszText = pszText;
4986 return bResult;
4990 /***
4991 * DESCRIPTION:
4992 * Retrieves the position (upper-left) of the listview control item.
4993 * Note that for LVS_ICON style, the upper-left is that of the icon
4994 * and not the bounding box.
4996 * PARAMETER(S):
4997 * [I] infoPtr : valid pointer to the listview structure
4998 * [I] nItem : item index
4999 * [O] lpptPosition : coordinate information
5001 * RETURN:
5002 * SUCCESS : TRUE
5003 * FAILURE : FALSE
5005 static BOOL LISTVIEW_GetItemPosition(LISTVIEW_INFO *infoPtr, INT nItem, LPPOINT lpptPosition)
5007 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
5008 POINT Origin;
5010 TRACE("(nItem=%d, lpptPosition=%p)\n", nItem, lpptPosition);
5012 if (!lpptPosition || nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
5014 LISTVIEW_GetOrigin(infoPtr, &Origin);
5015 LISTVIEW_GetItemOrigin(infoPtr, nItem, lpptPosition);
5017 if (uView == LVS_ICON)
5019 lpptPosition->x += (infoPtr->nItemWidth - infoPtr->iconSize.cx) / 2;
5020 lpptPosition->y += ICON_TOP_PADDING;
5022 lpptPosition->x += Origin.x;
5023 lpptPosition->y += Origin.y;
5025 TRACE (" lpptPosition=%s\n", debugpoint(lpptPosition));
5026 return TRUE;
5030 /***
5031 * DESCRIPTION:
5032 * Retrieves the bounding rectangle for a listview control item.
5034 * PARAMETER(S):
5035 * [I] infoPtr : valid pointer to the listview structure
5036 * [I] nItem : item index
5037 * [IO] lprc : bounding rectangle coordinates
5038 * lprc->left specifies the portion of the item for which the bounding
5039 * rectangle will be retrieved.
5041 * LVIR_BOUNDS Returns the bounding rectangle of the entire item,
5042 * including the icon and label.
5044 * * For LVS_ICON
5045 * * Experiment shows that native control returns:
5046 * * width = min (48, length of text line)
5047 * * .left = position.x - (width - iconsize.cx)/2
5048 * * .right = .left + width
5049 * * height = #lines of text * ntmHeight + icon height + 8
5050 * * .top = position.y - 2
5051 * * .bottom = .top + height
5052 * * separation between items .y = itemSpacing.cy - height
5053 * * .x = itemSpacing.cx - width
5054 * LVIR_ICON Returns the bounding rectangle of the icon or small icon.
5056 * * For LVS_ICON
5057 * * Experiment shows that native control returns:
5058 * * width = iconSize.cx + 16
5059 * * .left = position.x - (width - iconsize.cx)/2
5060 * * .right = .left + width
5061 * * height = iconSize.cy + 4
5062 * * .top = position.y - 2
5063 * * .bottom = .top + height
5064 * * separation between items .y = itemSpacing.cy - height
5065 * * .x = itemSpacing.cx - width
5066 * LVIR_LABEL Returns the bounding rectangle of the item text.
5068 * * For LVS_ICON
5069 * * Experiment shows that native control returns:
5070 * * width = text length
5071 * * .left = position.x - width/2
5072 * * .right = .left + width
5073 * * height = ntmH * linecount + 2
5074 * * .top = position.y + iconSize.cy + 6
5075 * * .bottom = .top + height
5076 * * separation between items .y = itemSpacing.cy - height
5077 * * .x = itemSpacing.cx - width
5078 * LVIR_SELECTBOUNDS Returns the union of the LVIR_ICON and LVIR_LABEL
5079 * rectangles, but excludes columns in report view.
5081 * RETURN:
5082 * SUCCESS : TRUE
5083 * FAILURE : FALSE
5085 * NOTES
5086 * Note that the bounding rectangle of the label in the LVS_ICON view depends
5087 * upon whether the window has the focus currently and on whether the item
5088 * is the one with the focus. Ensure that the control's record of which
5089 * item has the focus agrees with the items' records.
5091 static BOOL LISTVIEW_GetItemRect(LISTVIEW_INFO *infoPtr, INT nItem, LPRECT lprc)
5093 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
5094 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
5095 BOOL doLabel = TRUE, oversizedBox = FALSE;
5096 POINT Position, Origin;
5097 LVITEMW lvItem;
5098 RECT label_rect;
5100 TRACE("(hwnd=%p, nItem=%d, lprc=%p)\n", infoPtr->hwndSelf, nItem, lprc);
5102 if (!lprc || nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
5104 LISTVIEW_GetOrigin(infoPtr, &Origin);
5105 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position);
5107 /* Be smart and try to figure out the minimum we have to do */
5108 if (lprc->left == LVIR_ICON) doLabel = FALSE;
5109 if (uView == LVS_REPORT && lprc->left == LVIR_BOUNDS) doLabel = FALSE;
5110 if (uView == LVS_ICON && lprc->left != LVIR_ICON &&
5111 infoPtr->bFocus && LISTVIEW_GetItemState(infoPtr, nItem, LVIS_FOCUSED))
5112 oversizedBox = TRUE;
5114 /* get what we need from the item before hand, so we make
5115 * only one request. This can speed up things, if data
5116 * is stored on the app side */
5117 lvItem.mask = 0;
5118 if (uView == LVS_REPORT) lvItem.mask |= LVIF_INDENT;
5119 if (doLabel) lvItem.mask |= LVIF_TEXT;
5120 lvItem.iItem = nItem;
5121 lvItem.iSubItem = 0;
5122 lvItem.pszText = szDispText;
5123 lvItem.cchTextMax = DISP_TEXT_SIZE;
5124 if (lvItem.mask && !LISTVIEW_GetItemW(infoPtr, &lvItem)) return FALSE;
5125 /* we got the state already up, simulate it here, to avoid a reget */
5126 if (uView == LVS_ICON && (lprc->left != LVIR_ICON))
5128 lvItem.mask |= LVIF_STATE;
5129 lvItem.stateMask = LVIS_FOCUSED;
5130 lvItem.state = (oversizedBox ? LVIS_FOCUSED : 0);
5133 if (uView == LVS_REPORT && (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT) && lprc->left == LVIR_SELECTBOUNDS)
5134 lprc->left = LVIR_BOUNDS;
5135 switch(lprc->left)
5137 case LVIR_ICON:
5138 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, NULL, NULL, lprc, NULL);
5139 break;
5141 case LVIR_LABEL:
5142 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, NULL, NULL, NULL, lprc);
5143 break;
5145 case LVIR_BOUNDS:
5146 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, lprc, NULL, NULL, NULL);
5147 break;
5149 case LVIR_SELECTBOUNDS:
5150 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, NULL, NULL, lprc, &label_rect);
5151 UnionRect(lprc, lprc, &label_rect);
5152 break;
5154 default:
5155 WARN("Unknown value: %d\n", lprc->left);
5156 return FALSE;
5159 OffsetRect(lprc, Position.x + Origin.x, Position.y + Origin.y);
5161 TRACE(" rect=%s\n", debugrect(lprc));
5163 return TRUE;
5166 /***
5167 * DESCRIPTION:
5168 * Retrieves the spacing between listview control items.
5170 * PARAMETER(S):
5171 * [I] infoPtr : valid pointer to the listview structure
5172 * [IO] lprc : rectangle to receive the output
5173 * on input, lprc->top = nSubItem
5174 * lprc->left = LVIR_ICON | LVIR_BOUNDS | LVIR_LABEL
5176 * NOTE: for subItem = 0, we should return the bounds of the _entire_ item,
5177 * not only those of the first column.
5178 * Fortunately, LISTVIEW_GetItemMetrics does the right thing.
5180 * RETURN:
5181 * TRUE: success
5182 * FALSE: failure
5184 static BOOL LISTVIEW_GetSubItemRect(LISTVIEW_INFO *infoPtr, INT nItem, LPRECT lprc)
5186 POINT Position, Origin;
5187 LVITEMW lvItem;
5189 if (!lprc || (infoPtr->dwStyle & LVS_TYPEMASK) != LVS_REPORT) return FALSE;
5191 TRACE("(nItem=%d, nSubItem=%d)\n", nItem, lprc->top);
5193 LISTVIEW_GetOrigin(infoPtr, &Origin);
5194 if (!LISTVIEW_GetItemPosition(infoPtr, nItem, &Position)) return FALSE;
5196 lvItem.mask = lprc->top == 0 ? LVIF_INDENT : 0;
5197 lvItem.iItem = nItem;
5198 lvItem.iSubItem = lprc->top;
5200 if (lvItem.mask && !LISTVIEW_GetItemW(infoPtr, &lvItem)) return FALSE;
5201 switch(lprc->left)
5203 case LVIR_ICON:
5204 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, NULL, NULL, lprc, NULL);
5205 break;
5207 case LVIR_LABEL:
5208 case LVIR_BOUNDS:
5209 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, lprc, NULL, NULL, NULL);
5210 break;
5212 default:
5213 ERR("Unknown bounds=%d\n", lprc->left);
5214 return FALSE;
5217 OffsetRect(lprc, Position.x + Origin.x, Position.y + Origin.y);
5218 return TRUE;
5222 /***
5223 * DESCRIPTION:
5224 * Retrieves the width of a label.
5226 * PARAMETER(S):
5227 * [I] infoPtr : valid pointer to the listview structure
5229 * RETURN:
5230 * SUCCESS : string width (in pixels)
5231 * FAILURE : zero
5233 static INT LISTVIEW_GetLabelWidth(LISTVIEW_INFO *infoPtr, INT nItem)
5235 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
5236 LVITEMW lvItem;
5238 TRACE("(nItem=%d)\n", nItem);
5240 lvItem.mask = LVIF_TEXT;
5241 lvItem.iItem = nItem;
5242 lvItem.iSubItem = 0;
5243 lvItem.pszText = szDispText;
5244 lvItem.cchTextMax = DISP_TEXT_SIZE;
5245 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) return 0;
5247 return LISTVIEW_GetStringWidthT(infoPtr, lvItem.pszText, TRUE);
5250 /***
5251 * DESCRIPTION:
5252 * Retrieves the spacing between listview control items.
5254 * PARAMETER(S):
5255 * [I] infoPtr : valid pointer to the listview structure
5256 * [I] bSmall : flag for small or large icon
5258 * RETURN:
5259 * Horizontal + vertical spacing
5261 static LONG LISTVIEW_GetItemSpacing(LISTVIEW_INFO *infoPtr, BOOL bSmall)
5263 LONG lResult;
5265 if (!bSmall)
5267 lResult = MAKELONG(infoPtr->iconSpacing.cx, infoPtr->iconSpacing.cy);
5269 else
5271 if ((infoPtr->dwStyle & LVS_TYPEMASK) == LVS_ICON)
5272 lResult = MAKELONG(DEFAULT_COLUMN_WIDTH, GetSystemMetrics(SM_CXSMICON)+HEIGHT_PADDING);
5273 else
5274 lResult = MAKELONG(infoPtr->nItemWidth, infoPtr->nItemHeight);
5276 return lResult;
5279 /***
5280 * DESCRIPTION:
5281 * Retrieves the state of a listview control item.
5283 * PARAMETER(S):
5284 * [I] infoPtr : valid pointer to the listview structure
5285 * [I] nItem : item index
5286 * [I] uMask : state mask
5288 * RETURN:
5289 * State specified by the mask.
5291 static UINT LISTVIEW_GetItemState(LISTVIEW_INFO *infoPtr, INT nItem, UINT uMask)
5293 LVITEMW lvItem;
5295 if (nItem < 0 || nItem >= infoPtr->nItemCount) return 0;
5297 lvItem.iItem = nItem;
5298 lvItem.iSubItem = 0;
5299 lvItem.mask = LVIF_STATE;
5300 lvItem.stateMask = uMask;
5301 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) return 0;
5303 return lvItem.state & uMask;
5306 /***
5307 * DESCRIPTION:
5308 * Retrieves the text of a listview control item or subitem.
5310 * PARAMETER(S):
5311 * [I] hwnd : window handle
5312 * [I] nItem : item index
5313 * [IO] lpLVItem : item information
5314 * [I] isW : TRUE if lpLVItem is Unicode
5316 * RETURN:
5317 * SUCCESS : string length
5318 * FAILURE : 0
5320 static INT LISTVIEW_GetItemTextT(LISTVIEW_INFO *infoPtr, INT nItem, LPLVITEMW lpLVItem, BOOL isW)
5322 if (!lpLVItem || nItem < 0 || nItem >= infoPtr->nItemCount) return 0;
5324 lpLVItem->mask = LVIF_TEXT;
5325 lpLVItem->iItem = nItem;
5326 if (!LISTVIEW_GetItemExtT(infoPtr, lpLVItem, isW)) return 0;
5328 return textlenT(lpLVItem->pszText, isW);
5331 /***
5332 * DESCRIPTION:
5333 * Searches for an item based on properties + relationships.
5335 * PARAMETER(S):
5336 * [I] infoPtr : valid pointer to the listview structure
5337 * [I] nItem : item index
5338 * [I] uFlags : relationship flag
5340 * RETURN:
5341 * SUCCESS : item index
5342 * FAILURE : -1
5344 static INT LISTVIEW_GetNextItem(LISTVIEW_INFO *infoPtr, INT nItem, UINT uFlags)
5346 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
5347 UINT uMask = 0;
5348 LVFINDINFOW lvFindInfo;
5349 INT nCountPerColumn;
5350 INT i;
5352 TRACE("nItem=%d, uFlags=%x, nItemCount=%d\n", nItem, uFlags, infoPtr->nItemCount);
5353 if (nItem < -1 || nItem >= infoPtr->nItemCount) return -1;
5355 ZeroMemory(&lvFindInfo, sizeof(lvFindInfo));
5357 if (uFlags & LVNI_CUT)
5358 uMask |= LVIS_CUT;
5360 if (uFlags & LVNI_DROPHILITED)
5361 uMask |= LVIS_DROPHILITED;
5363 if (uFlags & LVNI_FOCUSED)
5364 uMask |= LVIS_FOCUSED;
5366 if (uFlags & LVNI_SELECTED)
5367 uMask |= LVIS_SELECTED;
5369 /* if we're asked for the focused item, that's only one,
5370 * so it's worth optimizing */
5371 if (uFlags & LVNI_FOCUSED)
5373 if (!(LISTVIEW_GetItemState(infoPtr, infoPtr->nFocusedItem, uMask) & uMask) == uMask) return -1;
5374 return (infoPtr->nFocusedItem == nItem) ? -1 : infoPtr->nFocusedItem;
5377 if (uFlags & LVNI_ABOVE)
5379 if ((uView == LVS_LIST) || (uView == LVS_REPORT))
5381 while (nItem >= 0)
5383 nItem--;
5384 if ((ListView_GetItemState(infoPtr->hwndSelf, nItem, uMask) & uMask) == uMask)
5385 return nItem;
5388 else
5390 lvFindInfo.flags = LVFI_NEARESTXY;
5391 lvFindInfo.vkDirection = VK_UP;
5392 ListView_GetItemPosition(infoPtr->hwndSelf, nItem, &lvFindInfo.pt);
5393 while ((nItem = ListView_FindItemW(infoPtr->hwndSelf, nItem, &lvFindInfo)) != -1)
5395 if ((ListView_GetItemState(infoPtr->hwndSelf, nItem, uMask) & uMask) == uMask)
5396 return nItem;
5400 else if (uFlags & LVNI_BELOW)
5402 if ((uView == LVS_LIST) || (uView == LVS_REPORT))
5404 while (nItem < infoPtr->nItemCount)
5406 nItem++;
5407 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
5408 return nItem;
5411 else
5413 lvFindInfo.flags = LVFI_NEARESTXY;
5414 lvFindInfo.vkDirection = VK_DOWN;
5415 ListView_GetItemPosition(infoPtr->hwndSelf, nItem, &lvFindInfo.pt);
5416 while ((nItem = ListView_FindItemW(infoPtr->hwndSelf, nItem, &lvFindInfo)) != -1)
5418 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
5419 return nItem;
5423 else if (uFlags & LVNI_TOLEFT)
5425 if (uView == LVS_LIST)
5427 nCountPerColumn = LISTVIEW_GetCountPerColumn(infoPtr);
5428 while (nItem - nCountPerColumn >= 0)
5430 nItem -= nCountPerColumn;
5431 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
5432 return nItem;
5435 else if ((uView == LVS_SMALLICON) || (uView == LVS_ICON))
5437 lvFindInfo.flags = LVFI_NEARESTXY;
5438 lvFindInfo.vkDirection = VK_LEFT;
5439 ListView_GetItemPosition(infoPtr->hwndSelf, nItem, &lvFindInfo.pt);
5440 while ((nItem = ListView_FindItemW(infoPtr->hwndSelf, nItem, &lvFindInfo)) != -1)
5442 if ((ListView_GetItemState(infoPtr->hwndSelf, nItem, uMask) & uMask) == uMask)
5443 return nItem;
5447 else if (uFlags & LVNI_TORIGHT)
5449 if (uView == LVS_LIST)
5451 nCountPerColumn = LISTVIEW_GetCountPerColumn(infoPtr);
5452 while (nItem + nCountPerColumn < infoPtr->nItemCount)
5454 nItem += nCountPerColumn;
5455 if ((ListView_GetItemState(infoPtr->hwndSelf, nItem, uMask) & uMask) == uMask)
5456 return nItem;
5459 else if ((uView == LVS_SMALLICON) || (uView == LVS_ICON))
5461 lvFindInfo.flags = LVFI_NEARESTXY;
5462 lvFindInfo.vkDirection = VK_RIGHT;
5463 ListView_GetItemPosition(infoPtr->hwndSelf, nItem, &lvFindInfo.pt);
5464 while ((nItem = ListView_FindItemW(infoPtr->hwndSelf, nItem, &lvFindInfo)) != -1)
5466 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
5467 return nItem;
5471 else
5473 nItem++;
5475 /* search by index */
5476 for (i = nItem; i < infoPtr->nItemCount; i++)
5478 if ((LISTVIEW_GetItemState(infoPtr, i, uMask) & uMask) == uMask)
5479 return i;
5483 return -1;
5486 /* LISTVIEW_GetNumberOfWorkAreas */
5488 /***
5489 * DESCRIPTION:
5490 * Retrieves the origin coordinates when in icon or small icon display mode.
5492 * PARAMETER(S):
5493 * [I] infoPtr : valid pointer to the listview structure
5494 * [O] lpptOrigin : coordinate information
5496 * RETURN:
5497 * None.
5499 static void LISTVIEW_GetOrigin(LISTVIEW_INFO *infoPtr, LPPOINT lpptOrigin)
5501 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
5502 INT nHorzPos = 0, nVertPos = 0;
5503 SCROLLINFO scrollInfo;
5505 scrollInfo.cbSize = sizeof(SCROLLINFO);
5506 scrollInfo.fMask = SIF_POS;
5508 if ((infoPtr->dwStyle & WS_HSCROLL) && GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo))
5509 nHorzPos = scrollInfo.nPos;
5510 if ((infoPtr->dwStyle & WS_VSCROLL) && GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo))
5511 nVertPos = scrollInfo.nPos;
5513 TRACE("nHorzPos=%d, nVertPos=%d\n", nHorzPos, nVertPos);
5515 lpptOrigin->x = infoPtr->rcList.left;
5516 lpptOrigin->y = infoPtr->rcList.top;
5517 if (uView == LVS_LIST)
5518 nHorzPos *= infoPtr->nItemWidth;
5519 else if (uView == LVS_REPORT)
5520 nVertPos *= infoPtr->nItemHeight;
5522 lpptOrigin->x -= nHorzPos;
5523 lpptOrigin->y -= nVertPos;
5525 TRACE(" origin=%s\n", debugpoint(lpptOrigin));
5528 /***
5529 * DESCRIPTION:
5530 * Retrieves the width of a string.
5532 * PARAMETER(S):
5533 * [I] hwnd : window handle
5534 * [I] lpszText : text string to process
5535 * [I] isW : TRUE if lpszText is Unicode, FALSE otherwise
5537 * RETURN:
5538 * SUCCESS : string width (in pixels)
5539 * FAILURE : zero
5541 static INT LISTVIEW_GetStringWidthT(LISTVIEW_INFO *infoPtr, LPCWSTR lpszText, BOOL isW)
5543 SIZE stringSize;
5545 stringSize.cx = 0;
5546 if (is_textT(lpszText, isW))
5548 HFONT hFont = infoPtr->hFont ? infoPtr->hFont : infoPtr->hDefaultFont;
5549 HDC hdc = GetDC(infoPtr->hwndSelf);
5550 HFONT hOldFont = SelectObject(hdc, hFont);
5552 if (isW)
5553 GetTextExtentPointW(hdc, lpszText, lstrlenW(lpszText), &stringSize);
5554 else
5555 GetTextExtentPointA(hdc, (LPCSTR)lpszText, lstrlenA((LPCSTR)lpszText), &stringSize);
5556 SelectObject(hdc, hOldFont);
5557 ReleaseDC(infoPtr->hwndSelf, hdc);
5559 return stringSize.cx;
5562 /***
5563 * DESCRIPTION:
5564 * Determines which listview item is located at the specified position.
5566 * PARAMETER(S):
5567 * [I] infoPtr : valid pointer to the listview structure
5568 * [IO] lpht : hit test information
5569 * [I] subitem : fill out iSubItem.
5570 * [I] select : return the index only if the hit selects the item
5572 * NOTE:
5573 * (mm 20001022): We must not allow iSubItem to be touched, for
5574 * an app might pass only a structure with space up to iItem!
5575 * (MS Office 97 does that for instance in the file open dialog)
5577 * RETURN:
5578 * SUCCESS : item index
5579 * FAILURE : -1
5581 static INT LISTVIEW_HitTest(LISTVIEW_INFO *infoPtr, LPLVHITTESTINFO lpht, BOOL subitem, BOOL select)
5583 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
5584 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
5585 RECT rcBox, rcBounds, rcState, rcIcon, rcLabel, rcSearch;
5586 POINT Origin, Position, opt;
5587 LVITEMW lvItem;
5588 ITERATOR i;
5590 TRACE("(pt=%s, subitem=%d, select=%d)\n", debugpoint(&lpht->pt), subitem, select);
5592 lpht->flags = 0;
5593 lpht->iItem = -1;
5594 if (subitem) lpht->iSubItem = 0;
5596 if (infoPtr->rcList.left > lpht->pt.x)
5597 lpht->flags |= LVHT_TOLEFT;
5598 else if (infoPtr->rcList.right < lpht->pt.x)
5599 lpht->flags |= LVHT_TORIGHT;
5601 if (infoPtr->rcList.top > lpht->pt.y)
5602 lpht->flags |= LVHT_ABOVE;
5603 else if (infoPtr->rcList.bottom < lpht->pt.y)
5604 lpht->flags |= LVHT_BELOW;
5606 TRACE("lpht->flags=0x%x\n", lpht->flags);
5607 if (lpht->flags) return -1;
5609 lpht->flags |= LVHT_NOWHERE;
5611 LISTVIEW_GetOrigin(infoPtr, &Origin);
5613 /* first deal with the large items */
5614 rcSearch.left = lpht->pt.x;
5615 rcSearch.top = lpht->pt.y;
5616 rcSearch.right = rcSearch.left + 1;
5617 rcSearch.bottom = rcSearch.top + 1;
5619 iterator_frameditems(&i, infoPtr, &rcSearch);
5620 iterator_next(&i); /* go to first item in the sequence */
5621 lpht->iItem = i.nItem;
5622 iterator_destroy(&i);
5624 TRACE("lpht->iItem=%d\n", lpht->iItem);
5625 if (lpht->iItem == -1) return -1;
5627 lvItem.mask = LVIF_STATE | LVIF_TEXT;
5628 if (uView == LVS_REPORT) lvItem.mask |= LVIF_INDENT;
5629 lvItem.stateMask = LVIS_STATEIMAGEMASK;
5630 if (uView == LVS_ICON) lvItem.stateMask |= LVIS_FOCUSED;
5631 lvItem.iItem = lpht->iItem;
5632 lvItem.iSubItem = 0;
5633 lvItem.pszText = szDispText;
5634 lvItem.cchTextMax = DISP_TEXT_SIZE;
5635 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) return -1;
5636 if (!infoPtr->bFocus) lvItem.state &= ~LVIS_FOCUSED;
5638 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, &rcBox, &rcState, &rcIcon, &rcLabel);
5639 LISTVIEW_GetItemOrigin(infoPtr, lpht->iItem, &Position);
5640 opt.x = lpht->pt.x - Position.x - Origin.x;
5641 opt.y = lpht->pt.y - Position.y - Origin.y;
5643 if (uView == LVS_REPORT && (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT))
5644 rcBounds = rcBox;
5645 else
5646 UnionRect(&rcBounds, &rcIcon, &rcLabel);
5647 TRACE("rcBounds=%s\n", debugrect(&rcBounds));
5648 if (!PtInRect(&rcBounds, opt)) return -1;
5650 if (PtInRect(&rcIcon, opt))
5651 lpht->flags |= LVHT_ONITEMICON;
5652 else if (PtInRect(&rcLabel, opt))
5653 lpht->flags |= LVHT_ONITEMLABEL;
5654 else if (infoPtr->himlState && ((lvItem.state & LVIS_STATEIMAGEMASK) >> 12) && PtInRect(&rcState, opt))
5655 lpht->flags |= LVHT_ONITEMSTATEICON;
5656 if (lpht->flags & LVHT_ONITEM)
5657 lpht->flags &= ~LVHT_NOWHERE;
5659 TRACE("lpht->flags=0x%x\n", lpht->flags);
5660 if (uView == LVS_REPORT && lpht->iItem != -1 && subitem)
5662 INT j;
5664 rcBounds.right = rcBounds.left;
5665 for (j = 0; j < infoPtr->hdpaColumns->nItemCount; j++)
5667 rcBounds.left = rcBounds.right;
5668 rcBounds.right += LISTVIEW_GetColumnWidth(infoPtr, j);
5669 if (PtInRect(&rcBounds, opt))
5671 lpht->iSubItem = j;
5672 break;
5677 if (!select || lpht->iItem == -1) return lpht->iItem;
5679 if (uView == LVS_REPORT && (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT)) return lpht->iItem;
5681 if (uView == LVS_REPORT) UnionRect(&rcBounds, &rcIcon, &rcLabel);
5682 return PtInRect(&rcBounds, opt) ? lpht->iItem : -1;
5686 /* LISTVIEW_InsertCompare: callback routine for comparing pszText members of the LV_ITEMS
5687 in a LISTVIEW on insert. Passed to DPA_Sort in LISTVIEW_InsertItem.
5688 This function should only be used for inserting items into a sorted list (LVM_INSERTITEM)
5689 and not during the processing of a LVM_SORTITEMS message. Applications should provide
5690 their own sort proc. when sending LVM_SORTITEMS.
5692 /* Platform SDK:
5693 (remarks on LVITEM: LVM_INSERTITEM will insert the new item in the proper sort postion...
5695 LVS_SORTXXX must be specified,
5696 LVS_OWNERDRAW is not set,
5697 <item>.pszText is not LPSTR_TEXTCALLBACK.
5699 (LVS_SORT* flags): "For the LVS_SORTASCENDING... styles, item indices
5700 are sorted based on item text..."
5702 static INT WINAPI LISTVIEW_InsertCompare( LPVOID first, LPVOID second, LPARAM lParam)
5704 ITEM_INFO* lv_first = (ITEM_INFO*) DPA_GetPtr( (HDPA)first, 0 );
5705 ITEM_INFO* lv_second = (ITEM_INFO*) DPA_GetPtr( (HDPA)second, 0 );
5706 INT cmpv = textcmpWT(lv_first->hdr.pszText, lv_second->hdr.pszText, TRUE);
5708 /* if we're sorting descending, negate the return value */
5709 return (((LISTVIEW_INFO *)lParam)->dwStyle & LVS_SORTDESCENDING) ? -cmpv : cmpv;
5712 /***
5713 * nESCRIPTION:
5714 * Inserts a new item in the listview control.
5716 * PARAMETER(S):
5717 * [I] infoPtr : valid pointer to the listview structure
5718 * [I] lpLVItem : item information
5719 * [I] isW : TRUE if lpLVItem is Unicode, FALSE if it's ANSI
5721 * RETURN:
5722 * SUCCESS : new item index
5723 * FAILURE : -1
5725 static INT LISTVIEW_InsertItemT(LISTVIEW_INFO *infoPtr, const LVITEMW *lpLVItem, BOOL isW)
5727 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
5728 INT nItem;
5729 HDPA hdpaSubItems;
5730 NMLISTVIEW nmlv;
5731 ITEM_INFO *lpItem;
5732 BOOL is_sorted, has_changed;
5733 LVITEMW item;
5735 TRACE("(lpLVItem=%s, isW=%d)\n", debuglvitem_t(lpLVItem, isW), isW);
5737 if (infoPtr->dwStyle & LVS_OWNERDATA) return infoPtr->nItemCount++;
5739 /* make sure it's an item, and not a subitem; cannot insert a subitem */
5740 if (!lpLVItem || lpLVItem->iItem < 0 || lpLVItem->iSubItem) return -1;
5742 if (!is_assignable_item(lpLVItem, infoPtr->dwStyle)) return -1;
5744 if ( !(lpItem = (ITEM_INFO *)COMCTL32_Alloc(sizeof(ITEM_INFO))) )
5745 return -1;
5747 /* insert item in listview control data structure */
5748 if ( !(hdpaSubItems = DPA_Create(8)) ) goto fail;
5749 if ( !DPA_SetPtr(hdpaSubItems, 0, lpItem) ) assert (FALSE);
5751 is_sorted = (infoPtr->dwStyle & (LVS_SORTASCENDING | LVS_SORTDESCENDING)) &&
5752 !(infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && (LPSTR_TEXTCALLBACKW != lpLVItem->pszText);
5754 nItem = is_sorted ? infoPtr->nItemCount : min(lpLVItem->iItem, infoPtr->nItemCount);
5755 TRACE(" inserting at %d, sorted=%d, count=%d, iItem=%d\n", nItem, is_sorted, infoPtr->nItemCount, lpLVItem->iItem);
5756 nItem = DPA_InsertPtr( infoPtr->hdpaItems, nItem, hdpaSubItems );
5757 if (nItem == -1) goto fail;
5758 infoPtr->nItemCount++;
5760 /* set the item attributes */
5761 item = *lpLVItem;
5762 item.iItem = nItem;
5763 item.state &= ~LVIS_STATEIMAGEMASK;
5764 if (!set_main_item(infoPtr, &item, TRUE, isW, &has_changed)) goto undo;
5766 /* if we're sorted, sort the list, and update the index */
5767 if (is_sorted)
5769 DPA_Sort( infoPtr->hdpaItems, LISTVIEW_InsertCompare, (LPARAM)infoPtr );
5770 nItem = DPA_GetPtrIndex( infoPtr->hdpaItems, hdpaSubItems );
5771 assert(nItem != -1);
5774 /* make room for the position, if we are in the right mode */
5775 if ((uView == LVS_SMALLICON) || (uView == LVS_ICON))
5777 if (DPA_InsertPtr(infoPtr->hdpaPosX, nItem, 0) == -1)
5778 goto undo;
5779 if (DPA_InsertPtr(infoPtr->hdpaPosY, nItem, 0) == -1)
5781 DPA_DeletePtr(infoPtr->hdpaPosX, nItem);
5782 goto undo;
5786 /* Add the subitem list to the items array. Do this last in case we go to
5787 * fail during the above.
5789 LISTVIEW_ShiftIndices(infoPtr, nItem, 1);
5791 /* send LVN_INSERTITEM notification */
5792 ZeroMemory(&nmlv, sizeof(NMLISTVIEW));
5793 nmlv.iItem = nItem;
5794 nmlv.lParam = lpItem->lParam;
5795 notify_listview(infoPtr, LVN_INSERTITEM, &nmlv);
5797 /* align items (set position of each item) */
5798 if ((uView == LVS_SMALLICON || uView == LVS_ICON))
5800 POINT pt;
5802 if (infoPtr->dwStyle & LVS_ALIGNLEFT)
5803 LISTVIEW_NextIconPosLeft(infoPtr, &pt);
5804 else
5805 LISTVIEW_NextIconPosTop(infoPtr, &pt);
5807 LISTVIEW_MoveIconTo(infoPtr, nItem, &pt, TRUE);
5810 /* now is the invalidation fun */
5811 LISTVIEW_ScrollOnInsert(infoPtr, nItem, 1);
5812 return nItem;
5814 undo:
5815 DPA_DeletePtr(infoPtr->hdpaItems, nItem);
5816 infoPtr->nItemCount--;
5817 fail:
5818 DPA_DeletePtr(hdpaSubItems, 0);
5819 DPA_Destroy (hdpaSubItems);
5820 COMCTL32_Free (lpItem);
5821 return -1;
5824 /***
5825 * DESCRIPTION:
5826 * Redraws a range of items.
5828 * PARAMETER(S):
5829 * [I] infoPtr : valid pointer to the listview structure
5830 * [I] nFirst : first item
5831 * [I] nLast : last item
5833 * RETURN:
5834 * SUCCESS : TRUE
5835 * FAILURE : FALSE
5837 static BOOL LISTVIEW_RedrawItems(LISTVIEW_INFO *infoPtr, INT nFirst, INT nLast)
5839 INT i;
5841 if (nLast < nFirst || min(nFirst, nLast) < 0 ||
5842 max(nFirst, nLast) >= infoPtr->nItemCount)
5843 return FALSE;
5845 for (i = nFirst; i <= nLast; i++)
5846 LISTVIEW_InvalidateItem(infoPtr, i);
5848 return TRUE;
5851 /***
5852 * DESCRIPTION:
5853 * Scroll the content of a listview.
5855 * PARAMETER(S):
5856 * [I] infoPtr : valid pointer to the listview structure
5857 * [I] dx : horizontal scroll amount in pixels
5858 * [I] dy : vertical scroll amount in pixels
5860 * RETURN:
5861 * SUCCESS : TRUE
5862 * FAILURE : FALSE
5864 * COMMENTS:
5865 * If the control is in report mode (LVS_REPORT) the control can
5866 * be scrolled only in line increments. "dy" will be rounded to the
5867 * nearest number of pixels that are a whole line. Ex: if line height
5868 * is 16 and an 8 is passed, the list will be scrolled by 16. If a 7
5869 * is passed the the scroll will be 0. (per MSDN 7/2002)
5871 * For: (per experimentaion with native control and CSpy ListView)
5872 * LVS_ICON dy=1 = 1 pixel (vertical only)
5873 * dx ignored
5874 * LVS_SMALLICON dy=1 = 1 pixel (vertical only)
5875 * dx ignored
5876 * LVS_LIST dx=1 = 1 column (horizontal only)
5877 * but will only scroll 1 column per message
5878 * no matter what the value.
5879 * dy must be 0 or FALSE returned.
5880 * LVS_REPORT dx=1 = 1 pixel
5881 * dy= see above
5884 static BOOL LISTVIEW_Scroll(LISTVIEW_INFO *infoPtr, INT dx, INT dy)
5886 switch(infoPtr->dwStyle & LVS_TYPEMASK) {
5887 case LVS_REPORT:
5888 dy += (dy < 0 ? -1 : 1) * infoPtr->nItemHeight/2;
5889 dy /= infoPtr->nItemHeight;
5890 break;
5891 case LVS_LIST:
5892 if (dy != 0) return FALSE;
5893 break;
5894 default: /* icon */
5895 dx = 0;
5896 break;
5899 if (dx != 0) LISTVIEW_HScroll(infoPtr, SB_INTERNAL, dx, 0);
5900 if (dy != 0) LISTVIEW_VScroll(infoPtr, SB_INTERNAL, dy, 0);
5902 return TRUE;
5905 /***
5906 * DESCRIPTION:
5907 * Sets the background color.
5909 * PARAMETER(S):
5910 * [I] infoPtr : valid pointer to the listview structure
5911 * [I] clrBk : background color
5913 * RETURN:
5914 * SUCCESS : TRUE
5915 * FAILURE : FALSE
5917 static BOOL LISTVIEW_SetBkColor(LISTVIEW_INFO *infoPtr, COLORREF clrBk)
5919 TRACE("(clrBk=%lx)\n", clrBk);
5921 if(infoPtr->clrBk != clrBk) {
5922 if (infoPtr->clrBk != CLR_NONE) DeleteObject(infoPtr->hBkBrush);
5923 infoPtr->clrBk = clrBk;
5924 if (clrBk == CLR_NONE)
5925 infoPtr->hBkBrush = (HBRUSH)GetClassLongW(infoPtr->hwndSelf, GCL_HBRBACKGROUND);
5926 else
5927 infoPtr->hBkBrush = CreateSolidBrush(clrBk);
5928 LISTVIEW_InvalidateList(infoPtr);
5931 return TRUE;
5934 /* LISTVIEW_SetBkImage */
5936 /*** Helper for {Insert,Set}ColumnT *only* */
5937 static void column_fill_hditem(LISTVIEW_INFO *infoPtr, HDITEMW *lphdi, INT nColumn, const LVCOLUMNW *lpColumn, BOOL isW)
5939 if (lpColumn->mask & LVCF_FMT)
5941 /* format member is valid */
5942 lphdi->mask |= HDI_FORMAT;
5944 /* set text alignment (leftmost column must be left-aligned) */
5945 if (nColumn == 0 || lpColumn->fmt & LVCFMT_LEFT)
5946 lphdi->fmt |= HDF_LEFT;
5947 else if (lpColumn->fmt & LVCFMT_RIGHT)
5948 lphdi->fmt |= HDF_RIGHT;
5949 else if (lpColumn->fmt & LVCFMT_CENTER)
5950 lphdi->fmt |= HDF_CENTER;
5952 if (lpColumn->fmt & LVCFMT_BITMAP_ON_RIGHT)
5953 lphdi->fmt |= HDF_BITMAP_ON_RIGHT;
5955 if (lpColumn->fmt & LVCFMT_COL_HAS_IMAGES)
5957 lphdi->fmt |= HDF_IMAGE;
5958 lphdi->iImage = I_IMAGECALLBACK;
5962 if (lpColumn->mask & LVCF_WIDTH)
5964 lphdi->mask |= HDI_WIDTH;
5965 if(lpColumn->cx == LVSCW_AUTOSIZE_USEHEADER)
5967 /* make it fill the remainder of the controls width */
5968 RECT rcHeader;
5969 INT item_index;
5971 for(item_index = 0; item_index < (nColumn - 1); item_index++)
5973 LISTVIEW_GetHeaderRect(infoPtr, item_index, &rcHeader);
5974 lphdi->cxy += rcHeader.right - rcHeader.left;
5977 /* retrieve the layout of the header */
5978 GetClientRect(infoPtr->hwndSelf, &rcHeader);
5979 TRACE("start cxy=%d rcHeader=%s\n", lphdi->cxy, debugrect(&rcHeader));
5981 lphdi->cxy = (rcHeader.right - rcHeader.left) - lphdi->cxy;
5983 else
5984 lphdi->cxy = lpColumn->cx;
5987 if (lpColumn->mask & LVCF_TEXT)
5989 lphdi->mask |= HDI_TEXT | HDI_FORMAT;
5990 lphdi->fmt |= HDF_STRING;
5991 lphdi->pszText = lpColumn->pszText;
5992 lphdi->cchTextMax = textlenT(lpColumn->pszText, isW);
5995 if (lpColumn->mask & LVCF_IMAGE)
5997 lphdi->mask |= HDI_IMAGE;
5998 lphdi->iImage = lpColumn->iImage;
6001 if (lpColumn->mask & LVCF_ORDER)
6003 lphdi->mask |= HDI_ORDER;
6004 lphdi->iOrder = lpColumn->iOrder;
6009 /***
6010 * DESCRIPTION:
6011 * Inserts a new column.
6013 * PARAMETER(S):
6014 * [I] infoPtr : valid pointer to the listview structure
6015 * [I] nColumn : column index
6016 * [I] lpColumn : column information
6017 * [I] isW : TRUE if lpColumn is Unicode, FALSE otherwise
6019 * RETURN:
6020 * SUCCESS : new column index
6021 * FAILURE : -1
6023 static INT LISTVIEW_InsertColumnT(LISTVIEW_INFO *infoPtr, INT nColumn,
6024 const LVCOLUMNW *lpColumn, BOOL isW)
6026 COLUMN_INFO *lpColumnInfo;
6027 INT nNewColumn;
6028 HDITEMW hdi;
6030 TRACE("(nColumn=%d, lpColumn=%s, isW=%d)\n", nColumn, debuglvcolumn_t(lpColumn, isW), isW);
6032 if (!lpColumn || nColumn < 0 || nColumn > infoPtr->hdpaColumns->nItemCount) return -1;
6034 ZeroMemory(&hdi, sizeof(HDITEMW));
6035 column_fill_hditem(infoPtr, &hdi, nColumn, lpColumn, isW);
6037 /* insert item in header control */
6038 nNewColumn = SendMessageW(infoPtr->hwndHeader,
6039 isW ? HDM_INSERTITEMW : HDM_INSERTITEMA,
6040 (WPARAM)nColumn, (LPARAM)&hdi);
6041 if (nNewColumn == -1) return -1;
6042 if (nNewColumn != nColumn) ERR("nColumn=%d, nNewColumn=%d\n", nColumn, nNewColumn);
6044 /* create our own column info */
6045 if (!(lpColumnInfo = COMCTL32_Alloc(sizeof(COLUMN_INFO)))) goto fail;
6046 if (DPA_InsertPtr(infoPtr->hdpaColumns, nNewColumn, lpColumnInfo) == -1) goto fail;
6048 if (lpColumn->mask & LVCF_FMT) lpColumnInfo->fmt = lpColumn->fmt;
6049 if (!Header_GetItemRect(infoPtr->hwndHeader, nNewColumn, &lpColumnInfo->rcHeader)) goto fail;
6051 /* now we have to actually adjust the data */
6052 if (!(infoPtr->dwStyle & LVS_OWNERDATA) && infoPtr->nItemCount > 0)
6054 SUBITEM_INFO *lpSubItem, *lpMainItem, **lpNewItems = 0;
6055 HDPA hdpaSubItems;
6056 INT nItem, i;
6058 /* preallocate memory, so we can fail gracefully */
6059 if (nNewColumn == 0)
6061 lpNewItems = COMCTL32_Alloc(sizeof(SUBITEM_INFO *) * infoPtr->nItemCount);
6062 if (!lpNewItems) goto fail;
6063 for (i = 0; i < infoPtr->nItemCount; i++)
6064 if (!(lpNewItems[i] = COMCTL32_Alloc(sizeof(SUBITEM_INFO)))) break;
6065 if (i != infoPtr->nItemCount)
6067 for(; i >=0; i--) COMCTL32_Free(lpNewItems[i]);
6068 COMCTL32_Free(lpNewItems);
6069 goto fail;
6073 for (nItem = 0; nItem < infoPtr->nItemCount; nItem++)
6075 hdpaSubItems = (HDPA)DPA_GetPtr(infoPtr->hdpaItems, nItem);
6076 for (i = 1; i < hdpaSubItems->nItemCount; i++)
6078 lpSubItem = (SUBITEM_INFO *)DPA_GetPtr(hdpaSubItems, i);
6079 if (lpSubItem->iSubItem >= nNewColumn)
6080 lpSubItem->iSubItem++;
6083 /* for inserting column 0, we have to special-case the main item */
6084 if (nNewColumn == 0)
6086 lpMainItem = (SUBITEM_INFO *)DPA_GetPtr(hdpaSubItems, 0);
6087 lpSubItem = lpNewItems[nItem];
6088 lpSubItem->hdr = lpMainItem->hdr;
6089 lpSubItem->iSubItem = 1;
6090 ZeroMemory(&lpMainItem->hdr, sizeof(lpMainItem->hdr));
6091 lpMainItem->iSubItem = 0;
6092 DPA_InsertPtr(hdpaSubItems, 1, lpSubItem);
6096 COMCTL32_Free(lpNewItems);
6099 /* make space for the new column */
6100 LISTVIEW_ScrollColumns(infoPtr, nNewColumn + 1, lpColumnInfo->rcHeader.right - lpColumnInfo->rcHeader.left);
6102 return nNewColumn;
6104 fail:
6105 if (nNewColumn != -1) SendMessageW(infoPtr->hwndHeader, HDM_DELETEITEM, nNewColumn, 0);
6106 if (lpColumnInfo)
6108 DPA_DeletePtr(infoPtr->hdpaColumns, nNewColumn);
6109 COMCTL32_Free(lpColumnInfo);
6111 return -1;
6114 /***
6115 * DESCRIPTION:
6116 * Sets the attributes of a header item.
6118 * PARAMETER(S):
6119 * [I] infoPtr : valid pointer to the listview structure
6120 * [I] nColumn : column index
6121 * [I] lpColumn : column attributes
6122 * [I] isW: if TRUE, the lpColumn is a LPLVCOLUMNW, else it is a LPLVCOLUMNA
6124 * RETURN:
6125 * SUCCESS : TRUE
6126 * FAILURE : FALSE
6128 static BOOL LISTVIEW_SetColumnT(LISTVIEW_INFO *infoPtr, INT nColumn,
6129 const LVCOLUMNW *lpColumn, BOOL isW)
6131 HDITEMW hdi, hdiget;
6132 BOOL bResult;
6134 TRACE("(nColumn=%d, lpColumn=%s, isW=%d)\n", nColumn, debuglvcolumn_t(lpColumn, isW), isW);
6136 if (!lpColumn || nColumn < 0 || nColumn >= infoPtr->hdpaColumns->nItemCount) return FALSE;
6138 ZeroMemory(&hdi, sizeof(HDITEMW));
6139 if (lpColumn->mask & LVCF_FMT)
6141 hdi.mask |= HDI_FORMAT;
6142 hdiget.mask = HDI_FORMAT;
6143 if (Header_GetItemW(infoPtr->hwndHeader, nColumn, &hdiget))
6144 hdi.fmt = hdiget.fmt & HDF_STRING;
6146 column_fill_hditem(infoPtr, &hdi, nColumn, lpColumn, isW);
6148 /* set header item attributes */
6149 bResult = SendMessageW(infoPtr->hwndHeader, isW ? HDM_SETITEMW : HDM_SETITEMA, (WPARAM)nColumn, (LPARAM)&hdi);
6150 if (!bResult) return FALSE;
6152 if (lpColumn->mask & LVCF_FMT)
6154 COLUMN_INFO *lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, nColumn);
6155 int oldFmt = lpColumnInfo->fmt;
6157 lpColumnInfo->fmt = lpColumn->fmt;
6158 if ((oldFmt ^ lpColumn->fmt) & (LVCFMT_JUSTIFYMASK | LVCFMT_IMAGE))
6160 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
6161 if (uView == LVS_REPORT) LISTVIEW_InvalidateColumn(infoPtr, nColumn);
6165 return TRUE;
6168 /***
6169 * DESCRIPTION:
6170 * Sets the column order array
6172 * PARAMETERS:
6173 * [I] infoPtr : valid pointer to the listview structure
6174 * [I] iCount : number of elements in column order array
6175 * [I] lpiArray : pointer to column order array
6177 * RETURN:
6178 * SUCCESS : TRUE
6179 * FAILURE : FALSE
6181 static BOOL LISTVIEW_SetColumnOrderArray(LISTVIEW_INFO *infoPtr, INT iCount, const INT *lpiArray)
6183 FIXME("iCount %d lpiArray %p\n", iCount, lpiArray);
6185 if (!lpiArray)
6186 return FALSE;
6188 return TRUE;
6192 /***
6193 * DESCRIPTION:
6194 * Sets the width of a column
6196 * PARAMETERS:
6197 * [I] infoPtr : valid pointer to the listview structure
6198 * [I] nColumn : column index
6199 * [I] cx : column width
6201 * RETURN:
6202 * SUCCESS : TRUE
6203 * FAILURE : FALSE
6205 static BOOL LISTVIEW_SetColumnWidth(LISTVIEW_INFO *infoPtr, INT nColumn, INT cx)
6207 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
6208 WCHAR szDispText[DISP_TEXT_SIZE] = { 0 };
6209 INT max_cx = 0;
6210 HDITEMW hdi;
6212 TRACE("(nColumn=%d, cx=%d\n", nColumn, cx);
6214 /* set column width only if in report or list mode */
6215 if (uView != LVS_REPORT && uView != LVS_LIST) return FALSE;
6217 /* take care of invalid cx values */
6218 if(uView == LVS_REPORT && cx < -2) cx = LVSCW_AUTOSIZE;
6219 else if (uView == LVS_LIST && cx < 1) return FALSE;
6221 /* resize all columns if in LVS_LIST mode */
6222 if(uView == LVS_LIST)
6224 infoPtr->nItemWidth = cx;
6225 LISTVIEW_InvalidateList(infoPtr);
6226 return TRUE;
6229 if (nColumn < 0 || nColumn >= infoPtr->hdpaColumns->nItemCount) return FALSE;
6231 if (cx == LVSCW_AUTOSIZE || (cx == LVSCW_AUTOSIZE_USEHEADER && nColumn < infoPtr->hdpaColumns->nItemCount -1))
6233 INT nLabelWidth;
6234 LVITEMW lvItem;
6236 lvItem.mask = LVIF_TEXT;
6237 lvItem.iItem = 0;
6238 lvItem.iSubItem = nColumn;
6239 lvItem.pszText = szDispText;
6240 lvItem.cchTextMax = DISP_TEXT_SIZE;
6241 for (; lvItem.iItem < infoPtr->nItemCount; lvItem.iItem++)
6243 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) continue;
6244 nLabelWidth = LISTVIEW_GetStringWidthT(infoPtr, lvItem.pszText, TRUE);
6245 if (max_cx < nLabelWidth) max_cx = nLabelWidth;
6247 if (infoPtr->himlSmall && (nColumn == 0 || (LISTVIEW_GetColumnInfo(infoPtr, nColumn)->fmt & LVCFMT_IMAGE)))
6248 max_cx += infoPtr->iconSize.cx + IMAGE_PADDING;
6249 max_cx += REPORT_MARGINX + TRAILING_PADDING;
6252 /* autosize based on listview items width */
6253 if(cx == LVSCW_AUTOSIZE)
6254 cx = max_cx;
6255 else if(cx == LVSCW_AUTOSIZE_USEHEADER)
6257 /* if iCol is the last column make it fill the remainder of the controls width */
6258 if(nColumn == infoPtr->hdpaColumns->nItemCount - 1)
6260 RECT rcHeader;
6261 POINT Origin;
6263 LISTVIEW_GetOrigin(infoPtr, &Origin);
6264 LISTVIEW_GetHeaderRect(infoPtr, nColumn, &rcHeader);
6266 cx = infoPtr->rcList.right - Origin.x - rcHeader.left;
6268 else
6270 /* Despite what the MS docs say, if this is not the last
6271 column, then MS resizes the column to the width of the
6272 largest text string in the column, including headers
6273 and items. This is different from LVSCW_AUTOSIZE in that
6274 LVSCW_AUTOSIZE ignores the header string length. */
6275 cx = 0;
6277 /* retrieve header text */
6278 hdi.mask = HDI_TEXT;
6279 hdi.cchTextMax = DISP_TEXT_SIZE;
6280 hdi.pszText = szDispText;
6281 if (Header_GetItemW(infoPtr->hwndHeader, nColumn, (LPARAM)&hdi))
6283 HDC hdc = GetDC(infoPtr->hwndSelf);
6284 HFONT old_font = SelectObject(hdc, (HFONT)SendMessageW(infoPtr->hwndHeader, WM_GETFONT, 0, 0));
6285 SIZE size;
6287 if (GetTextExtentPoint32W(hdc, hdi.pszText, lstrlenW(hdi.pszText), &size))
6288 cx = size.cx;
6289 /* FIXME: Take into account the header image, if one is present */
6290 SelectObject(hdc, old_font);
6291 ReleaseDC(infoPtr->hwndSelf, hdc);
6293 cx = max (cx, max_cx);
6297 if (cx < 0) return FALSE;
6299 /* call header to update the column change */
6300 hdi.mask = HDI_WIDTH;
6301 hdi.cxy = cx;
6302 TRACE("hdi.cxy=%d\n", hdi.cxy);
6303 return Header_SetItemW(infoPtr->hwndHeader, nColumn, (LPARAM)&hdi);
6306 /***
6307 * DESCRIPTION:
6308 * Sets the extended listview style.
6310 * PARAMETERS:
6311 * [I] infoPtr : valid pointer to the listview structure
6312 * [I] dwMask : mask
6313 * [I] dwStyle : style
6315 * RETURN:
6316 * SUCCESS : previous style
6317 * FAILURE : 0
6319 static DWORD LISTVIEW_SetExtendedListViewStyle(LISTVIEW_INFO *infoPtr, DWORD dwMask, DWORD dwStyle)
6321 DWORD dwOldStyle = infoPtr->dwLvExStyle;
6323 /* set new style */
6324 if (dwMask)
6325 infoPtr->dwLvExStyle = (dwOldStyle & ~dwMask) | (dwStyle & dwMask);
6326 else
6327 infoPtr->dwLvExStyle = dwStyle;
6329 return dwOldStyle;
6332 /***
6333 * DESCRIPTION:
6334 * Sets the new hot cursor used during hot tracking and hover selection.
6336 * PARAMETER(S):
6337 * [I] infoPtr : valid pointer to the listview structure
6338 * [I} hCurosr : the new hot cursor handle
6340 * RETURN:
6341 * Returns the previous hot cursor
6343 static HCURSOR LISTVIEW_SetHotCursor(LISTVIEW_INFO *infoPtr, HCURSOR hCursor)
6345 HCURSOR oldCursor = infoPtr->hHotCursor;
6347 infoPtr->hHotCursor = hCursor;
6349 return oldCursor;
6353 /***
6354 * DESCRIPTION:
6355 * Sets the hot item index.
6357 * PARAMETERS:
6358 * [I] infoPtr : valid pointer to the listview structure
6359 * [I] iIndex : index
6361 * RETURN:
6362 * SUCCESS : previous hot item index
6363 * FAILURE : -1 (no hot item)
6365 static INT LISTVIEW_SetHotItem(LISTVIEW_INFO *infoPtr, INT iIndex)
6367 INT iOldIndex = infoPtr->nHotItem;
6369 infoPtr->nHotItem = iIndex;
6371 return iOldIndex;
6375 /***
6376 * DESCRIPTION:
6377 * Sets the amount of time the cursor must hover over an item before it is selected.
6379 * PARAMETER(S):
6380 * [I] infoPtr : valid pointer to the listview structure
6381 * [I] dwHoverTime : hover time, if -1 the hover time is set to the default
6383 * RETURN:
6384 * Returns the previous hover time
6386 static DWORD LISTVIEW_SetHoverTime(LISTVIEW_INFO *infoPtr, DWORD dwHoverTime)
6388 DWORD oldHoverTime = infoPtr->dwHoverTime;
6390 infoPtr->dwHoverTime = dwHoverTime;
6392 return oldHoverTime;
6395 /***
6396 * DESCRIPTION:
6397 * Sets spacing for icons of LVS_ICON style.
6399 * PARAMETER(S):
6400 * [I] infoPtr : valid pointer to the listview structure
6401 * [I] spacing : MAKELONG(cx, cy)
6403 * RETURN:
6404 * MAKELONG(oldcx, oldcy)
6406 static DWORD LISTVIEW_SetIconSpacing(LISTVIEW_INFO *infoPtr, DWORD spacing)
6408 INT cy = HIWORD(spacing), cx = LOWORD(spacing);
6409 DWORD oldspacing = MAKELONG(infoPtr->iconSpacing.cx, infoPtr->iconSpacing.cy);
6410 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
6412 TRACE("requested=(%d,%d)\n", cx, cy);
6414 /* this is supported only for LVS_ICON style */
6415 if (uView != LVS_ICON) return oldspacing;
6417 /* set to defaults, if instructed to */
6418 if (cx == -1) cx = GetSystemMetrics(SM_CXICONSPACING);
6419 if (cy == -1) cy = GetSystemMetrics(SM_CYICONSPACING);
6421 /* if 0 then compute width
6422 * FIXME: Should scan each item and determine max width of
6423 * icon or label, then make that the width */
6424 if (cx == 0)
6425 cx = infoPtr->iconSpacing.cx;
6427 /* if 0 then compute height */
6428 if (cy == 0)
6429 cy = infoPtr->iconSize.cy + 2 * infoPtr->ntmHeight +
6430 ICON_BOTTOM_PADDING + ICON_TOP_PADDING + LABEL_VERT_PADDING;
6433 infoPtr->iconSpacing.cx = cx;
6434 infoPtr->iconSpacing.cy = cy;
6436 TRACE("old=(%d,%d), new=(%d,%d), iconSize=(%ld,%ld), ntmH=%d\n",
6437 LOWORD(oldspacing), HIWORD(oldspacing), cx, cy,
6438 infoPtr->iconSize.cx, infoPtr->iconSize.cy,
6439 infoPtr->ntmHeight);
6441 /* these depend on the iconSpacing */
6442 LISTVIEW_UpdateItemSize(infoPtr);
6444 return oldspacing;
6447 inline void set_icon_size(SIZE *size, HIMAGELIST himl, BOOL small)
6449 INT cx, cy;
6451 if (himl && ImageList_GetIconSize(himl, &cx, &cy))
6453 size->cx = cx;
6454 size->cy = cy;
6456 else
6458 size->cx = GetSystemMetrics(small ? SM_CXSMICON : SM_CXICON);
6459 size->cy = GetSystemMetrics(small ? SM_CYSMICON : SM_CYICON);
6463 /***
6464 * DESCRIPTION:
6465 * Sets image lists.
6467 * PARAMETER(S):
6468 * [I] infoPtr : valid pointer to the listview structure
6469 * [I] nType : image list type
6470 * [I] himl : image list handle
6472 * RETURN:
6473 * SUCCESS : old image list
6474 * FAILURE : NULL
6476 static HIMAGELIST LISTVIEW_SetImageList(LISTVIEW_INFO *infoPtr, INT nType, HIMAGELIST himl)
6478 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
6479 INT oldHeight = infoPtr->nItemHeight;
6480 HIMAGELIST himlOld = 0;
6482 TRACE("(nType=%d, himl=%p\n", nType, himl);
6484 switch (nType)
6486 case LVSIL_NORMAL:
6487 himlOld = infoPtr->himlNormal;
6488 infoPtr->himlNormal = himl;
6489 if (uView == LVS_ICON) set_icon_size(&infoPtr->iconSize, himl, FALSE);
6490 LISTVIEW_SetIconSpacing(infoPtr, 0);
6491 break;
6493 case LVSIL_SMALL:
6494 himlOld = infoPtr->himlSmall;
6495 infoPtr->himlSmall = himl;
6496 if (uView != LVS_ICON) set_icon_size(&infoPtr->iconSize, himl, TRUE);
6497 break;
6499 case LVSIL_STATE:
6500 himlOld = infoPtr->himlState;
6501 infoPtr->himlState = himl;
6502 set_icon_size(&infoPtr->iconStateSize, himl, TRUE);
6503 ImageList_SetBkColor(infoPtr->himlState, CLR_NONE);
6504 break;
6506 default:
6507 ERR("Unknown icon type=%d\n", nType);
6508 return NULL;
6511 infoPtr->nItemHeight = LISTVIEW_CalculateItemHeight(infoPtr);
6512 if (infoPtr->nItemHeight != oldHeight)
6513 LISTVIEW_UpdateScroll(infoPtr);
6515 return himlOld;
6518 /***
6519 * DESCRIPTION:
6520 * Preallocates memory (does *not* set the actual count of items !)
6522 * PARAMETER(S):
6523 * [I] infoPtr : valid pointer to the listview structure
6524 * [I] nItems : item count (projected number of items to allocate)
6525 * [I] dwFlags : update flags
6527 * RETURN:
6528 * SUCCESS : TRUE
6529 * FAILURE : FALSE
6531 static BOOL LISTVIEW_SetItemCount(LISTVIEW_INFO *infoPtr, INT nItems, DWORD dwFlags)
6533 TRACE("(nItems=%d, dwFlags=%lx)\n", nItems, dwFlags);
6535 if (infoPtr->dwStyle & LVS_OWNERDATA)
6537 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
6538 INT nOldCount = infoPtr->nItemCount;
6540 if (nItems < nOldCount)
6542 RANGE range = { nItems, nOldCount };
6543 ranges_del(infoPtr->selectionRanges, range);
6544 if (infoPtr->nFocusedItem >= nItems)
6546 infoPtr->nFocusedItem = -1;
6547 SetRectEmpty(&infoPtr->rcFocus);
6551 infoPtr->nItemCount = nItems;
6552 LISTVIEW_UpdateScroll(infoPtr);
6554 /* the flags are valid only in ownerdata report and list modes */
6555 if (uView == LVS_ICON || uView == LVS_SMALLICON) dwFlags = 0;
6557 if (!(dwFlags & LVSICF_NOSCROLL) && infoPtr->nFocusedItem != -1)
6558 LISTVIEW_EnsureVisible(infoPtr, infoPtr->nFocusedItem, FALSE);
6560 if (!(dwFlags & LVSICF_NOINVALIDATEALL))
6561 LISTVIEW_InvalidateList(infoPtr);
6562 else
6564 INT nFrom, nTo;
6565 POINT Origin;
6566 RECT rcErase;
6568 LISTVIEW_GetOrigin(infoPtr, &Origin);
6569 nFrom = min(nOldCount, nItems);
6570 nTo = max(nOldCount, nItems);
6572 if (uView == LVS_REPORT)
6574 rcErase.left = 0;
6575 rcErase.top = nFrom * infoPtr->nItemHeight;
6576 rcErase.right = infoPtr->nItemWidth;
6577 rcErase.bottom = nTo * infoPtr->nItemHeight;
6578 OffsetRect(&rcErase, Origin.x, Origin.y);
6579 if (IntersectRect(&rcErase, &rcErase, &infoPtr->rcList))
6580 LISTVIEW_InvalidateRect(infoPtr, &rcErase);
6582 else /* LVS_LIST */
6584 INT nPerCol = LISTVIEW_GetCountPerColumn(infoPtr);
6586 rcErase.left = (nFrom / nPerCol) * infoPtr->nItemWidth;
6587 rcErase.top = (nFrom % nPerCol) * infoPtr->nItemHeight;
6588 rcErase.right = rcErase.left + infoPtr->nItemWidth;
6589 rcErase.bottom = nPerCol * infoPtr->nItemHeight;
6590 OffsetRect(&rcErase, Origin.x, Origin.y);
6591 if (IntersectRect(&rcErase, &rcErase, &infoPtr->rcList))
6592 LISTVIEW_InvalidateRect(infoPtr, &rcErase);
6594 rcErase.left = (nFrom / nPerCol + 1) * infoPtr->nItemWidth;
6595 rcErase.top = 0;
6596 rcErase.right = (nTo / nPerCol + 1) * infoPtr->nItemWidth;
6597 rcErase.bottom = nPerCol * infoPtr->nItemHeight;
6598 OffsetRect(&rcErase, Origin.x, Origin.y);
6599 if (IntersectRect(&rcErase, &rcErase, &infoPtr->rcList))
6600 LISTVIEW_InvalidateRect(infoPtr, &rcErase);
6604 else
6606 /* According to MSDN for non-LVS_OWNERDATA this is just
6607 * a performance issue. The control allocates its internal
6608 * data structures for the number of items specified. It
6609 * cuts down on the number of memory allocations. Therefore
6610 * we will just issue a WARN here
6612 WARN("for non-ownerdata performance option not implemented.\n");
6615 return TRUE;
6618 /***
6619 * DESCRIPTION:
6620 * Sets the position of an item.
6622 * PARAMETER(S):
6623 * [I] infoPtr : valid pointer to the listview structure
6624 * [I] nItem : item index
6625 * [I] pt : coordinate
6627 * RETURN:
6628 * SUCCESS : TRUE
6629 * FAILURE : FALSE
6631 static BOOL LISTVIEW_SetItemPosition(LISTVIEW_INFO *infoPtr, INT nItem, POINT pt)
6633 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
6634 POINT Origin;
6636 TRACE("(nItem=%d, &pt=%s\n", nItem, debugpoint(&pt));
6638 if (nItem < 0 || nItem >= infoPtr->nItemCount ||
6639 !(uView == LVS_ICON || uView == LVS_SMALLICON)) return FALSE;
6641 LISTVIEW_GetOrigin(infoPtr, &Origin);
6643 /* This point value seems to be an undocumented feature.
6644 * The best guess is that it means either at the origin,
6645 * or at true beginning of the list. I will assume the origin. */
6646 if ((pt.x == -1) && (pt.y == -1))
6647 pt = Origin;
6649 if (uView == LVS_ICON)
6651 pt.x -= (infoPtr->nItemWidth - infoPtr->iconSize.cx) / 2;
6652 pt.y -= ICON_TOP_PADDING;
6654 pt.x -= Origin.x;
6655 pt.y -= Origin.y;
6657 infoPtr->bAutoarrange = FALSE;
6659 return LISTVIEW_MoveIconTo(infoPtr, nItem, &pt, FALSE);
6662 /***
6663 * DESCRIPTION:
6664 * Sets the state of one or many items.
6666 * PARAMETER(S):
6667 * [I] infoPtr : valid pointer to the listview structure
6668 * [I] nItem : item index
6669 * [I] lpLVItem : item or subitem info
6671 * RETURN:
6672 * SUCCESS : TRUE
6673 * FAILURE : FALSE
6675 static BOOL LISTVIEW_SetItemState(LISTVIEW_INFO *infoPtr, INT nItem, const LVITEMW *lpLVItem)
6677 BOOL bResult = TRUE;
6678 LVITEMW lvItem;
6680 lvItem.iItem = nItem;
6681 lvItem.iSubItem = 0;
6682 lvItem.mask = LVIF_STATE;
6683 lvItem.state = lpLVItem->state;
6684 lvItem.stateMask = lpLVItem->stateMask;
6685 TRACE("lvItem=%s\n", debuglvitem_t(&lvItem, TRUE));
6687 if (nItem == -1)
6689 /* apply to all items */
6690 for (lvItem.iItem = 0; lvItem.iItem < infoPtr->nItemCount; lvItem.iItem++)
6691 if (!LISTVIEW_SetItemT(infoPtr, &lvItem, TRUE)) bResult = FALSE;
6693 else
6694 bResult = LISTVIEW_SetItemT(infoPtr, &lvItem, TRUE);
6696 return bResult;
6699 /***
6700 * DESCRIPTION:
6701 * Sets the text of an item or subitem.
6703 * PARAMETER(S):
6704 * [I] hwnd : window handle
6705 * [I] nItem : item index
6706 * [I] lpLVItem : item or subitem info
6707 * [I] isW : TRUE if input is Unicode
6709 * RETURN:
6710 * SUCCESS : TRUE
6711 * FAILURE : FALSE
6713 static BOOL LISTVIEW_SetItemTextT(LISTVIEW_INFO *infoPtr, INT nItem, const LVITEMW *lpLVItem, BOOL isW)
6715 LVITEMW lvItem;
6717 if (nItem < 0 && nItem >= infoPtr->nItemCount) return FALSE;
6719 lvItem.iItem = nItem;
6720 lvItem.iSubItem = lpLVItem->iSubItem;
6721 lvItem.mask = LVIF_TEXT;
6722 lvItem.pszText = lpLVItem->pszText;
6723 lvItem.cchTextMax = lpLVItem->cchTextMax;
6725 TRACE("(nItem=%d, lpLVItem=%s, isW=%d)\n", nItem, debuglvitem_t(&lvItem, isW), isW);
6727 return LISTVIEW_SetItemT(infoPtr, &lvItem, isW);
6730 /***
6731 * DESCRIPTION:
6732 * Set item index that marks the start of a multiple selection.
6734 * PARAMETER(S):
6735 * [I] infoPtr : valid pointer to the listview structure
6736 * [I] nIndex : index
6738 * RETURN:
6739 * Index number or -1 if there is no selection mark.
6741 static INT LISTVIEW_SetSelectionMark(LISTVIEW_INFO *infoPtr, INT nIndex)
6743 INT nOldIndex = infoPtr->nSelectionMark;
6745 TRACE("(nIndex=%d)\n", nIndex);
6747 infoPtr->nSelectionMark = nIndex;
6749 return nOldIndex;
6752 /***
6753 * DESCRIPTION:
6754 * Sets the text background color.
6756 * PARAMETER(S):
6757 * [I] infoPtr : valid pointer to the listview structure
6758 * [I] clrTextBk : text background color
6760 * RETURN:
6761 * SUCCESS : TRUE
6762 * FAILURE : FALSE
6764 static BOOL LISTVIEW_SetTextBkColor(LISTVIEW_INFO *infoPtr, COLORREF clrTextBk)
6766 TRACE("(clrTextBk=%lx)\n", clrTextBk);
6768 if (infoPtr->clrTextBk != clrTextBk)
6770 infoPtr->clrTextBk = clrTextBk;
6771 LISTVIEW_InvalidateList(infoPtr);
6774 return TRUE;
6777 /***
6778 * DESCRIPTION:
6779 * Sets the text foreground color.
6781 * PARAMETER(S):
6782 * [I] infoPtr : valid pointer to the listview structure
6783 * [I] clrText : text color
6785 * RETURN:
6786 * SUCCESS : TRUE
6787 * FAILURE : FALSE
6789 static BOOL LISTVIEW_SetTextColor (LISTVIEW_INFO *infoPtr, COLORREF clrText)
6791 TRACE("(clrText=%lx)\n", clrText);
6793 if (infoPtr->clrText != clrText)
6795 infoPtr->clrText = clrText;
6796 LISTVIEW_InvalidateList(infoPtr);
6799 return TRUE;
6802 /* LISTVIEW_SetToolTips */
6803 /* LISTVIEW_SetUnicodeFormat */
6804 /* LISTVIEW_SetWorkAreas */
6806 /***
6807 * DESCRIPTION:
6808 * Callback internally used by LISTVIEW_SortItems()
6810 * PARAMETER(S):
6811 * [I] first : pointer to first ITEM_INFO to compare
6812 * [I] second : pointer to second ITEM_INFO to compare
6813 * [I] lParam : HWND of control
6815 * RETURN:
6816 * if first comes before second : negative
6817 * if first comes after second : positive
6818 * if first and second are equivalent : zero
6820 static INT WINAPI LISTVIEW_CallBackCompare(LPVOID first, LPVOID second, LPARAM lParam)
6822 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)GetWindowLongW((HWND)lParam, 0);
6823 ITEM_INFO* lv_first = (ITEM_INFO*) DPA_GetPtr( (HDPA)first, 0 );
6824 ITEM_INFO* lv_second = (ITEM_INFO*) DPA_GetPtr( (HDPA)second, 0 );
6826 /* Forward the call to the client defined callback */
6827 return (infoPtr->pfnCompare)( lv_first->lParam , lv_second->lParam, infoPtr->lParamSort );
6830 /***
6831 * DESCRIPTION:
6832 * Sorts the listview items.
6834 * PARAMETER(S):
6835 * [I] infoPtr : valid pointer to the listview structure
6836 * [I] pfnCompare : application-defined value
6837 * [I] lParamSort : pointer to comparision callback
6839 * RETURN:
6840 * SUCCESS : TRUE
6841 * FAILURE : FALSE
6843 static BOOL LISTVIEW_SortItems(LISTVIEW_INFO *infoPtr, PFNLVCOMPARE pfnCompare, LPARAM lParamSort)
6845 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
6846 HDPA hdpaSubItems;
6847 ITEM_INFO *lpItem;
6848 LPVOID selectionMarkItem;
6849 LVITEMW item;
6850 int i;
6852 TRACE("(pfnCompare=%p, lParamSort=%lx)\n", pfnCompare, lParamSort);
6854 if (infoPtr->dwStyle & LVS_OWNERDATA) return FALSE;
6856 if (!infoPtr->hdpaItems) return FALSE;
6858 /* if there are 0 or 1 items, there is no need to sort */
6859 if (infoPtr->nItemCount < 2) return TRUE;
6861 if (infoPtr->nFocusedItem >= 0)
6863 hdpaSubItems = (HDPA)DPA_GetPtr(infoPtr->hdpaItems, infoPtr->nFocusedItem);
6864 lpItem = (ITEM_INFO *)DPA_GetPtr(hdpaSubItems, 0);
6865 if (lpItem) lpItem->state |= LVIS_FOCUSED;
6867 /* FIXME: go thorugh selected items and mark them so in lpItem->state */
6868 /* clear the lpItem->state for non-selected ones */
6869 /* remove the selection ranges */
6871 infoPtr->pfnCompare = pfnCompare;
6872 infoPtr->lParamSort = lParamSort;
6873 DPA_Sort(infoPtr->hdpaItems, LISTVIEW_CallBackCompare, (LPARAM)infoPtr->hwndSelf);
6875 /* Adjust selections and indices so that they are the way they should
6876 * be after the sort (otherwise, the list items move around, but
6877 * whatever is at the item's previous original position will be
6878 * selected instead)
6880 selectionMarkItem=(infoPtr->nSelectionMark>=0)?DPA_GetPtr(infoPtr->hdpaItems, infoPtr->nSelectionMark):NULL;
6881 for (i=0; i < infoPtr->nItemCount; i++)
6883 hdpaSubItems = (HDPA)DPA_GetPtr(infoPtr->hdpaItems, i);
6884 lpItem = (ITEM_INFO *)DPA_GetPtr(hdpaSubItems, 0);
6886 if (lpItem->state & LVIS_SELECTED)
6888 item.state = LVIS_SELECTED;
6889 item.stateMask = LVIS_SELECTED;
6890 LISTVIEW_SetItemState(infoPtr, i, &item);
6892 if (lpItem->state & LVIS_FOCUSED)
6894 infoPtr->nFocusedItem = i;
6895 lpItem->state &= ~LVIS_FOCUSED;
6898 if (selectionMarkItem != NULL)
6899 infoPtr->nSelectionMark = DPA_GetPtrIndex(infoPtr->hdpaItems, selectionMarkItem);
6900 /* I believe nHotItem should be left alone, see LISTVIEW_ShiftIndices */
6902 /* refresh the display */
6903 if (uView != LVS_ICON && uView != LVS_SMALLICON)
6904 LISTVIEW_InvalidateList(infoPtr);
6906 return TRUE;
6909 /***
6910 * DESCRIPTION:
6911 * Updates an items or rearranges the listview control.
6913 * PARAMETER(S):
6914 * [I] infoPtr : valid pointer to the listview structure
6915 * [I] nItem : item index
6917 * RETURN:
6918 * SUCCESS : TRUE
6919 * FAILURE : FALSE
6921 static BOOL LISTVIEW_Update(LISTVIEW_INFO *infoPtr, INT nItem)
6923 TRACE("(nItem=%d)\n", nItem);
6925 if (nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
6927 /* rearrange with default alignment style */
6928 if (is_autoarrange(infoPtr))
6929 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
6930 else
6931 LISTVIEW_InvalidateItem(infoPtr, nItem);
6933 return TRUE;
6937 /***
6938 * DESCRIPTION:
6939 * Creates the listview control.
6941 * PARAMETER(S):
6942 * [I] hwnd : window handle
6943 * [I] lpcs : the create parameters
6945 * RETURN:
6946 * Success: 0
6947 * Failure: -1
6949 static LRESULT LISTVIEW_Create(HWND hwnd, const CREATESTRUCTW *lpcs)
6951 LISTVIEW_INFO *infoPtr;
6952 UINT uView = lpcs->style & LVS_TYPEMASK;
6953 LOGFONTW logFont;
6955 TRACE("(lpcs=%p)\n", lpcs);
6957 /* initialize info pointer */
6958 infoPtr = (LISTVIEW_INFO *)COMCTL32_Alloc(sizeof(LISTVIEW_INFO));
6959 if (!infoPtr) return -1;
6961 SetWindowLongW(hwnd, 0, (LONG)infoPtr);
6963 infoPtr->hwndSelf = hwnd;
6964 infoPtr->dwStyle = lpcs->style;
6965 /* determine the type of structures to use */
6966 infoPtr->notifyFormat = SendMessageW(GetParent(infoPtr->hwndSelf), WM_NOTIFYFORMAT,
6967 (WPARAM)infoPtr->hwndSelf, (LPARAM)NF_QUERY);
6969 /* initialize color information */
6970 infoPtr->clrBk = CLR_NONE;
6971 infoPtr->clrText = comctl32_color.clrWindowText;
6972 infoPtr->clrTextBk = CLR_DEFAULT;
6973 LISTVIEW_SetBkColor(infoPtr, comctl32_color.clrWindow);
6975 /* set default values */
6976 infoPtr->nFocusedItem = -1;
6977 infoPtr->nSelectionMark = -1;
6978 infoPtr->nHotItem = -1;
6979 infoPtr->bRedraw = TRUE;
6980 infoPtr->bFirstPaint = TRUE;
6981 infoPtr->iconSpacing.cx = GetSystemMetrics(SM_CXICONSPACING);
6982 infoPtr->iconSpacing.cy = GetSystemMetrics(SM_CYICONSPACING);
6983 infoPtr->nEditLabelItem = -1;
6984 infoPtr->dwHoverTime = -1; /* default system hover time */
6986 /* get default font (icon title) */
6987 SystemParametersInfoW(SPI_GETICONTITLELOGFONT, 0, &logFont, 0);
6988 infoPtr->hDefaultFont = CreateFontIndirectW(&logFont);
6989 infoPtr->hFont = infoPtr->hDefaultFont;
6990 LISTVIEW_SaveTextMetrics(infoPtr);
6992 /* create header */
6993 infoPtr->hwndHeader = CreateWindowW(WC_HEADERW, (LPCWSTR)NULL,
6994 WS_CHILD | HDS_HORZ | (DWORD)((LVS_NOSORTHEADER & lpcs->style)?0:HDS_BUTTONS),
6995 0, 0, 0, 0, hwnd, (HMENU)0,
6996 lpcs->hInstance, NULL);
6998 /* set header unicode format */
6999 SendMessageW(infoPtr->hwndHeader, HDM_SETUNICODEFORMAT, (WPARAM)TRUE, (LPARAM)NULL);
7001 /* set header font */
7002 SendMessageW(infoPtr->hwndHeader, WM_SETFONT, (WPARAM)infoPtr->hFont, (LPARAM)TRUE);
7004 /* allocate memory for the selection ranges */
7005 if (!(infoPtr->selectionRanges = ranges_create(10))) return -1;
7007 /* allocate memory for the data structure */
7008 /* FIXME: what if we fail? */
7009 infoPtr->hdpaItems = DPA_Create(10);
7010 infoPtr->hdpaPosX = DPA_Create(10);
7011 infoPtr->hdpaPosY = DPA_Create(10);
7012 infoPtr->hdpaColumns = DPA_Create(10);
7014 /* initialize the icon sizes */
7015 set_icon_size(&infoPtr->iconSize, infoPtr->himlNormal, uView != LVS_ICON);
7016 set_icon_size(&infoPtr->iconStateSize, infoPtr->himlState, TRUE);
7018 /* init item size to avoid division by 0 */
7019 LISTVIEW_UpdateItemSize (infoPtr);
7021 if (uView == LVS_REPORT)
7023 if (!(LVS_NOCOLUMNHEADER & lpcs->style))
7025 ShowWindow(infoPtr->hwndHeader, SW_SHOWNORMAL);
7027 else
7029 /* set HDS_HIDDEN flag to hide the header bar */
7030 SetWindowLongW(infoPtr->hwndHeader, GWL_STYLE,
7031 GetWindowLongW(infoPtr->hwndHeader, GWL_STYLE) | HDS_HIDDEN);
7035 return 0;
7038 /***
7039 * DESCRIPTION:
7040 * Erases the background of the listview control.
7042 * PARAMETER(S):
7043 * [I] infoPtr : valid pointer to the listview structure
7044 * [I] hdc : device context handle
7046 * RETURN:
7047 * SUCCESS : TRUE
7048 * FAILURE : FALSE
7050 static inline BOOL LISTVIEW_EraseBkgnd(LISTVIEW_INFO *infoPtr, HDC hdc)
7052 RECT rc;
7054 TRACE("(hdc=%p)\n", hdc);
7056 if (!GetClipBox(hdc, &rc)) return FALSE;
7058 return LISTVIEW_FillBkgnd(infoPtr, hdc, &rc);
7062 /***
7063 * DESCRIPTION:
7064 * Helper function for LISTVIEW_[HV]Scroll *only*.
7065 * Performs vertical/horizontal scrolling by a give amount.
7067 * PARAMETER(S):
7068 * [I] infoPtr : valid pointer to the listview structure
7069 * [I] dx : amount of horizontal scroll
7070 * [I] dy : amount of vertical scroll
7072 static void scroll_list(LISTVIEW_INFO *infoPtr, INT dx, INT dy)
7074 /* now we can scroll the list */
7075 ScrollWindowEx(infoPtr->hwndSelf, dx, dy, &infoPtr->rcList,
7076 &infoPtr->rcList, 0, 0, SW_ERASE | SW_INVALIDATE);
7077 /* if we have focus, adjust rect */
7078 OffsetRect(&infoPtr->rcFocus, dx, dy);
7079 UpdateWindow(infoPtr->hwndSelf);
7082 /***
7083 * DESCRIPTION:
7084 * Performs vertical scrolling.
7086 * PARAMETER(S):
7087 * [I] infoPtr : valid pointer to the listview structure
7088 * [I] nScrollCode : scroll code
7089 * [I] nScrollDiff : units to scroll in SB_INTERNAL mode, 0 otherwise
7090 * [I] hScrollWnd : scrollbar control window handle
7092 * RETURN:
7093 * Zero
7095 * NOTES:
7096 * SB_LINEUP/SB_LINEDOWN:
7097 * for LVS_ICON, LVS_SMALLICON is 37 by experiment
7098 * for LVS_REPORT is 1 line
7099 * for LVS_LIST cannot occur
7102 static LRESULT LISTVIEW_VScroll(LISTVIEW_INFO *infoPtr, INT nScrollCode,
7103 INT nScrollDiff, HWND hScrollWnd)
7105 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
7106 INT nOldScrollPos, nNewScrollPos;
7107 SCROLLINFO scrollInfo;
7108 BOOL is_an_icon;
7110 TRACE("(nScrollCode=%d, nScrollDiff=%d)\n", nScrollCode, nScrollDiff);
7112 if (infoPtr->hwndEdit) SendMessageW(infoPtr->hwndEdit, WM_KILLFOCUS, 0, 0);
7114 scrollInfo.cbSize = sizeof(SCROLLINFO);
7115 scrollInfo.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_TRACKPOS;
7117 is_an_icon = ((uView == LVS_ICON) || (uView == LVS_SMALLICON));
7119 if (!GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo)) return 1;
7121 nOldScrollPos = scrollInfo.nPos;
7122 switch (nScrollCode)
7124 case SB_INTERNAL:
7125 break;
7127 case SB_LINEUP:
7128 nScrollDiff = (is_an_icon) ? -LISTVIEW_SCROLL_ICON_LINE_SIZE : -1;
7129 break;
7131 case SB_LINEDOWN:
7132 nScrollDiff = (is_an_icon) ? LISTVIEW_SCROLL_ICON_LINE_SIZE : 1;
7133 break;
7135 case SB_PAGEUP:
7136 nScrollDiff = -scrollInfo.nPage;
7137 break;
7139 case SB_PAGEDOWN:
7140 nScrollDiff = scrollInfo.nPage;
7141 break;
7143 case SB_THUMBPOSITION:
7144 case SB_THUMBTRACK:
7145 nScrollDiff = scrollInfo.nTrackPos - scrollInfo.nPos;
7146 break;
7148 default:
7149 nScrollDiff = 0;
7152 /* quit right away if pos isn't changing */
7153 if (nScrollDiff == 0) return 0;
7155 /* calculate new position, and handle overflows */
7156 nNewScrollPos = scrollInfo.nPos + nScrollDiff;
7157 if (nScrollDiff > 0) {
7158 if (nNewScrollPos < nOldScrollPos ||
7159 nNewScrollPos > scrollInfo.nMax)
7160 nNewScrollPos = scrollInfo.nMax;
7161 } else {
7162 if (nNewScrollPos > nOldScrollPos ||
7163 nNewScrollPos < scrollInfo.nMin)
7164 nNewScrollPos = scrollInfo.nMin;
7167 /* set the new position, and reread in case it changed */
7168 scrollInfo.fMask = SIF_POS;
7169 scrollInfo.nPos = nNewScrollPos;
7170 nNewScrollPos = SetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo, TRUE);
7172 /* carry on only if it really changed */
7173 if (nNewScrollPos == nOldScrollPos) return 0;
7175 /* now adjust to client coordinates */
7176 nScrollDiff = nOldScrollPos - nNewScrollPos;
7177 if (uView == LVS_REPORT) nScrollDiff *= infoPtr->nItemHeight;
7179 /* and scroll the window */
7180 scroll_list(infoPtr, 0, nScrollDiff);
7182 return 0;
7185 /***
7186 * DESCRIPTION:
7187 * Performs horizontal scrolling.
7189 * PARAMETER(S):
7190 * [I] infoPtr : valid pointer to the listview structure
7191 * [I] nScrollCode : scroll code
7192 * [I] nScrollDiff : units to scroll in SB_INTERNAL mode, 0 otherwise
7193 * [I] hScrollWnd : scrollbar control window handle
7195 * RETURN:
7196 * Zero
7198 * NOTES:
7199 * SB_LINELEFT/SB_LINERIGHT:
7200 * for LVS_ICON, LVS_SMALLICON 1 pixel
7201 * for LVS_REPORT is 1 pixel
7202 * for LVS_LIST is 1 column --> which is a 1 because the
7203 * scroll is based on columns not pixels
7206 static LRESULT LISTVIEW_HScroll(LISTVIEW_INFO *infoPtr, INT nScrollCode,
7207 INT nScrollDiff, HWND hScrollWnd)
7209 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
7210 INT nOldScrollPos, nNewScrollPos;
7211 SCROLLINFO scrollInfo;
7213 TRACE("(nScrollCode=%d, nScrollDiff=%d)\n", nScrollCode, nScrollDiff);
7215 if (infoPtr->hwndEdit) SendMessageW(infoPtr->hwndEdit, WM_KILLFOCUS, 0, 0);
7217 scrollInfo.cbSize = sizeof(SCROLLINFO);
7218 scrollInfo.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_TRACKPOS;
7220 if (!GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo)) return 1;
7222 nOldScrollPos = scrollInfo.nPos;
7224 switch (nScrollCode)
7226 case SB_INTERNAL:
7227 break;
7229 case SB_LINELEFT:
7230 nScrollDiff = -1;
7231 break;
7233 case SB_LINERIGHT:
7234 nScrollDiff = 1;
7235 break;
7237 case SB_PAGELEFT:
7238 nScrollDiff = -scrollInfo.nPage;
7239 break;
7241 case SB_PAGERIGHT:
7242 nScrollDiff = scrollInfo.nPage;
7243 break;
7245 case SB_THUMBPOSITION:
7246 case SB_THUMBTRACK:
7247 nScrollDiff = scrollInfo.nTrackPos - scrollInfo.nPos;
7248 break;
7250 default:
7251 nScrollDiff = 0;
7254 /* quit right away if pos isn't changing */
7255 if (nScrollDiff == 0) return 0;
7257 /* calculate new position, and handle overflows */
7258 nNewScrollPos = scrollInfo.nPos + nScrollDiff;
7259 if (nScrollDiff > 0) {
7260 if (nNewScrollPos < nOldScrollPos ||
7261 nNewScrollPos > scrollInfo.nMax)
7262 nNewScrollPos = scrollInfo.nMax;
7263 } else {
7264 if (nNewScrollPos > nOldScrollPos ||
7265 nNewScrollPos < scrollInfo.nMin)
7266 nNewScrollPos = scrollInfo.nMin;
7269 /* set the new position, and reread in case it changed */
7270 scrollInfo.fMask = SIF_POS;
7271 scrollInfo.nPos = nNewScrollPos;
7272 nNewScrollPos = SetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo, TRUE);
7274 /* carry on only if it really changed */
7275 if (nNewScrollPos == nOldScrollPos) return 0;
7277 if(uView == LVS_REPORT)
7278 LISTVIEW_UpdateHeaderSize(infoPtr, nNewScrollPos);
7280 /* now adjust to client coordinates */
7281 nScrollDiff = nOldScrollPos - nNewScrollPos;
7282 if (uView == LVS_LIST) nScrollDiff *= infoPtr->nItemWidth;
7284 /* and scroll the window */
7285 scroll_list(infoPtr, nScrollDiff, 0);
7287 return 0;
7290 static LRESULT LISTVIEW_MouseWheel(LISTVIEW_INFO *infoPtr, INT wheelDelta)
7292 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
7293 INT gcWheelDelta = 0;
7294 UINT pulScrollLines = 3;
7295 SCROLLINFO scrollInfo;
7297 TRACE("(wheelDelta=%d)\n", wheelDelta);
7299 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES,0, &pulScrollLines, 0);
7300 gcWheelDelta -= wheelDelta;
7302 scrollInfo.cbSize = sizeof(SCROLLINFO);
7303 scrollInfo.fMask = SIF_POS;
7305 switch(uView)
7307 case LVS_ICON:
7308 case LVS_SMALLICON:
7310 * listview should be scrolled by a multiple of 37 dependently on its dimension or its visible item number
7311 * should be fixed in the future.
7313 if (GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo))
7314 LISTVIEW_VScroll(infoPtr, SB_THUMBPOSITION,
7315 scrollInfo.nPos + (gcWheelDelta < 0) ?
7316 LISTVIEW_SCROLL_ICON_LINE_SIZE :
7317 -LISTVIEW_SCROLL_ICON_LINE_SIZE, 0);
7318 break;
7320 case LVS_REPORT:
7321 if (abs(gcWheelDelta) >= WHEEL_DELTA && pulScrollLines)
7323 if (GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo))
7325 int cLineScroll = min(LISTVIEW_GetCountPerColumn(infoPtr), pulScrollLines);
7326 cLineScroll *= (gcWheelDelta / WHEEL_DELTA);
7327 LISTVIEW_VScroll(infoPtr, SB_THUMBPOSITION, scrollInfo.nPos + cLineScroll, 0);
7330 break;
7332 case LVS_LIST:
7333 LISTVIEW_HScroll(infoPtr, (gcWheelDelta < 0) ? SB_LINELEFT : SB_LINERIGHT, 0, 0);
7334 break;
7336 return 0;
7339 /***
7340 * DESCRIPTION:
7341 * ???
7343 * PARAMETER(S):
7344 * [I] infoPtr : valid pointer to the listview structure
7345 * [I] nVirtualKey : virtual key
7346 * [I] lKeyData : key data
7348 * RETURN:
7349 * Zero
7351 static LRESULT LISTVIEW_KeyDown(LISTVIEW_INFO *infoPtr, INT nVirtualKey, LONG lKeyData)
7353 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
7354 INT nItem = -1;
7355 NMLVKEYDOWN nmKeyDown;
7357 TRACE("(nVirtualKey=%d, lKeyData=%ld)\n", nVirtualKey, lKeyData);
7359 /* send LVN_KEYDOWN notification */
7360 nmKeyDown.wVKey = nVirtualKey;
7361 nmKeyDown.flags = 0;
7362 notify_hdr(infoPtr, LVN_KEYDOWN, &nmKeyDown.hdr);
7364 switch (nVirtualKey)
7366 case VK_RETURN:
7367 if ((infoPtr->nItemCount > 0) && (infoPtr->nFocusedItem != -1))
7369 notify(infoPtr, NM_RETURN);
7370 notify(infoPtr, LVN_ITEMACTIVATE);
7372 break;
7374 case VK_HOME:
7375 if (infoPtr->nItemCount > 0)
7376 nItem = 0;
7377 break;
7379 case VK_END:
7380 if (infoPtr->nItemCount > 0)
7381 nItem = infoPtr->nItemCount - 1;
7382 break;
7384 case VK_LEFT:
7385 nItem = ListView_GetNextItem(infoPtr->hwndSelf, infoPtr->nFocusedItem, LVNI_TOLEFT);
7386 break;
7388 case VK_UP:
7389 nItem = ListView_GetNextItem(infoPtr->hwndSelf, infoPtr->nFocusedItem, LVNI_ABOVE);
7390 break;
7392 case VK_RIGHT:
7393 nItem = ListView_GetNextItem(infoPtr->hwndSelf, infoPtr->nFocusedItem, LVNI_TORIGHT);
7394 break;
7396 case VK_DOWN:
7397 nItem = ListView_GetNextItem(infoPtr->hwndSelf, infoPtr->nFocusedItem, LVNI_BELOW);
7398 break;
7400 case VK_PRIOR:
7401 if (uView == LVS_REPORT)
7402 nItem = infoPtr->nFocusedItem - LISTVIEW_GetCountPerColumn(infoPtr);
7403 else
7404 nItem = infoPtr->nFocusedItem - LISTVIEW_GetCountPerColumn(infoPtr)
7405 * LISTVIEW_GetCountPerRow(infoPtr);
7406 if(nItem < 0) nItem = 0;
7407 break;
7409 case VK_NEXT:
7410 if (uView == LVS_REPORT)
7411 nItem = infoPtr->nFocusedItem + LISTVIEW_GetCountPerColumn(infoPtr);
7412 else
7413 nItem = infoPtr->nFocusedItem + LISTVIEW_GetCountPerColumn(infoPtr)
7414 * LISTVIEW_GetCountPerRow(infoPtr);
7415 if(nItem >= infoPtr->nItemCount) nItem = infoPtr->nItemCount - 1;
7416 break;
7419 if ((nItem != -1) && (nItem != infoPtr->nFocusedItem))
7420 LISTVIEW_KeySelection(infoPtr, nItem);
7422 return 0;
7425 /***
7426 * DESCRIPTION:
7427 * Kills the focus.
7429 * PARAMETER(S):
7430 * [I] infoPtr : valid pointer to the listview structure
7432 * RETURN:
7433 * Zero
7435 static LRESULT LISTVIEW_KillFocus(LISTVIEW_INFO *infoPtr)
7437 TRACE("()\n");
7439 /* if we did not have the focus, there's nothing to do */
7440 if (!infoPtr->bFocus) return 0;
7442 /* send NM_KILLFOCUS notification */
7443 notify(infoPtr, NM_KILLFOCUS);
7445 /* if we have a focus rectagle, get rid of it */
7446 LISTVIEW_ShowFocusRect(infoPtr, FALSE);
7448 /* set window focus flag */
7449 infoPtr->bFocus = FALSE;
7451 /* invalidate the selected items before reseting focus flag */
7452 LISTVIEW_InvalidateSelectedItems(infoPtr);
7454 return 0;
7457 /***
7458 * DESCRIPTION:
7459 * Processes double click messages (left mouse button).
7461 * PARAMETER(S):
7462 * [I] infoPtr : valid pointer to the listview structure
7463 * [I] wKey : key flag
7464 * [I] pts : mouse coordinate
7466 * RETURN:
7467 * Zero
7469 static LRESULT LISTVIEW_LButtonDblClk(LISTVIEW_INFO *infoPtr, WORD wKey, POINTS pts)
7471 LVHITTESTINFO htInfo;
7473 TRACE("(key=%hu, X=%hu, Y=%hu)\n", wKey, pts.x, pts.y);
7475 /* send NM_RELEASEDCAPTURE notification */
7476 notify(infoPtr, NM_RELEASEDCAPTURE);
7478 htInfo.pt.x = pts.x;
7479 htInfo.pt.y = pts.y;
7481 /* send NM_DBLCLK notification */
7482 LISTVIEW_HitTest(infoPtr, &htInfo, TRUE, FALSE);
7483 notify_click(infoPtr, NM_DBLCLK, &htInfo);
7485 /* To send the LVN_ITEMACTIVATE, it must be on an Item */
7486 if(htInfo.iItem != -1) notify(infoPtr, LVN_ITEMACTIVATE);
7488 return 0;
7491 /***
7492 * DESCRIPTION:
7493 * Processes mouse down messages (left mouse button).
7495 * PARAMETER(S):
7496 * [I] infoPtr : valid pointer to the listview structure
7497 * [I] wKey : key flag
7498 * [I] pts : mouse coordinate
7500 * RETURN:
7501 * Zero
7503 static LRESULT LISTVIEW_LButtonDown(LISTVIEW_INFO *infoPtr, WORD wKey, POINTS pts)
7505 LVHITTESTINFO lvHitTestInfo;
7506 static BOOL bGroupSelect = TRUE;
7507 POINT pt = { pts.x, pts.y };
7508 INT nItem;
7510 TRACE("(key=%hu, X=%hu, Y=%hu)\n", wKey, pts.x, pts.y);
7512 /* send NM_RELEASEDCAPTURE notification */
7513 notify(infoPtr, NM_RELEASEDCAPTURE);
7515 if (!infoPtr->bFocus) SetFocus(infoPtr->hwndSelf);
7517 /* set left button down flag */
7518 infoPtr->bLButtonDown = TRUE;
7520 lvHitTestInfo.pt.x = pts.x;
7521 lvHitTestInfo.pt.y = pts.y;
7523 nItem = LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, TRUE);
7524 TRACE("at %s, nItem=%d\n", debugpoint(&pt), nItem);
7525 infoPtr->nEditLabelItem = -1;
7526 if ((nItem >= 0) && (nItem < infoPtr->nItemCount))
7528 if (infoPtr->dwStyle & LVS_SINGLESEL)
7530 if (LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED))
7531 infoPtr->nEditLabelItem = nItem;
7532 else
7533 LISTVIEW_SetSelection(infoPtr, nItem);
7535 else
7537 if ((wKey & MK_CONTROL) && (wKey & MK_SHIFT))
7539 if (bGroupSelect)
7541 LISTVIEW_AddGroupSelection(infoPtr, nItem);
7542 LISTVIEW_SetItemFocus(infoPtr, nItem);
7543 infoPtr->nSelectionMark = nItem;
7545 else
7547 LVITEMW item;
7549 item.state = LVIS_SELECTED | LVIS_FOCUSED;
7550 item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
7552 LISTVIEW_SetItemState(infoPtr,nItem,&item);
7553 infoPtr->nSelectionMark = nItem;
7556 else if (wKey & MK_CONTROL)
7558 LVITEMW item;
7560 bGroupSelect = (LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED) == 0);
7562 item.state = (bGroupSelect ? LVIS_SELECTED : 0) | LVIS_FOCUSED;
7563 item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
7564 LISTVIEW_SetItemState(infoPtr, nItem, &item);
7565 infoPtr->nSelectionMark = nItem;
7567 else if (wKey & MK_SHIFT)
7569 LISTVIEW_SetGroupSelection(infoPtr, nItem);
7571 else
7573 if (LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED))
7574 infoPtr->nEditLabelItem = nItem;
7576 /* set selection (clears other pre-existing selections) */
7577 LISTVIEW_SetSelection(infoPtr, nItem);
7581 else
7583 /* remove all selections */
7584 LISTVIEW_DeselectAll(infoPtr);
7587 return 0;
7590 /***
7591 * DESCRIPTION:
7592 * Processes mouse up messages (left mouse button).
7594 * PARAMETER(S):
7595 * [I] infoPtr : valid pointer to the listview structure
7596 * [I] wKey : key flag
7597 * [I] pts : mouse coordinate
7599 * RETURN:
7600 * Zero
7602 static LRESULT LISTVIEW_LButtonUp(LISTVIEW_INFO *infoPtr, WORD wKey, POINTS pts)
7604 LVHITTESTINFO lvHitTestInfo;
7606 TRACE("(key=%hu, X=%hu, Y=%hu)\n", wKey, pts.x, pts.y);
7608 if (!infoPtr->bLButtonDown) return 0;
7610 lvHitTestInfo.pt.x = pts.x;
7611 lvHitTestInfo.pt.y = pts.y;
7613 /* send NM_CLICK notification */
7614 LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, FALSE);
7615 notify_click(infoPtr, NM_CLICK, &lvHitTestInfo);
7617 /* set left button flag */
7618 infoPtr->bLButtonDown = FALSE;
7620 /* if we clicked on a selected item, edit the label */
7621 if(lvHitTestInfo.iItem == infoPtr->nEditLabelItem && (lvHitTestInfo.flags & LVHT_ONITEMLABEL))
7622 LISTVIEW_EditLabelT(infoPtr, lvHitTestInfo.iItem, TRUE);
7624 return 0;
7627 /***
7628 * DESCRIPTION:
7629 * Destroys the listview control (called after WM_DESTROY).
7631 * PARAMETER(S):
7632 * [I] infoPtr : valid pointer to the listview structure
7634 * RETURN:
7635 * Zero
7637 static LRESULT LISTVIEW_NCDestroy(LISTVIEW_INFO *infoPtr)
7639 TRACE("()\n");
7641 /* delete all items */
7642 LISTVIEW_DeleteAllItems(infoPtr);
7644 /* destroy data structure */
7645 DPA_Destroy(infoPtr->hdpaItems);
7646 if (infoPtr->selectionRanges) ranges_destroy(infoPtr->selectionRanges);
7648 /* destroy image lists */
7649 if (!(infoPtr->dwStyle & LVS_SHAREIMAGELISTS))
7651 if (infoPtr->himlNormal)
7652 ImageList_Destroy(infoPtr->himlNormal);
7653 if (infoPtr->himlSmall)
7654 ImageList_Destroy(infoPtr->himlSmall);
7655 if (infoPtr->himlState)
7656 ImageList_Destroy(infoPtr->himlState);
7659 /* destroy font, bkgnd brush */
7660 infoPtr->hFont = 0;
7661 if (infoPtr->hDefaultFont) DeleteObject(infoPtr->hDefaultFont);
7662 if (infoPtr->clrBk != CLR_NONE) DeleteObject(infoPtr->hBkBrush);
7664 /* free listview info pointer*/
7665 COMCTL32_Free(infoPtr);
7667 SetWindowLongW(infoPtr->hwndSelf, 0, 0);
7668 return 0;
7671 /***
7672 * DESCRIPTION:
7673 * Handles notifications from header.
7675 * PARAMETER(S):
7676 * [I] infoPtr : valid pointer to the listview structure
7677 * [I] nCtrlId : control identifier
7678 * [I] lpnmh : notification information
7680 * RETURN:
7681 * Zero
7683 static LRESULT LISTVIEW_HeaderNotification(LISTVIEW_INFO *infoPtr, const NMHEADERW *lpnmh)
7685 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
7687 TRACE("(lpnmh=%p)\n", lpnmh);
7689 if (!lpnmh || lpnmh->iItem < 0 || lpnmh->iItem >= infoPtr->hdpaColumns->nItemCount) return 0;
7691 switch (lpnmh->hdr.code)
7693 case HDN_TRACKW:
7694 case HDN_TRACKA:
7695 case HDN_ITEMCHANGEDW:
7696 case HDN_ITEMCHANGEDA:
7698 COLUMN_INFO *lpColumnInfo;
7699 INT dx, cxy;
7701 if (!lpnmh->pitem || !(lpnmh->pitem->mask & HDI_WIDTH))
7703 HDITEMW hdi;
7705 hdi.mask = HDI_WIDTH;
7706 if (!Header_GetItemW(infoPtr->hwndHeader, lpnmh->iItem, (LPARAM)&hdi)) return 0;
7707 cxy = hdi.cxy;
7709 else
7710 cxy = lpnmh->pitem->cxy;
7712 /* determine how much we change since the last know position */
7713 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, lpnmh->iItem);
7714 dx = cxy - (lpColumnInfo->rcHeader.right - lpColumnInfo->rcHeader.left);
7715 if (dx != 0)
7717 RECT rcCol = lpColumnInfo->rcHeader;
7719 lpColumnInfo->rcHeader.right += dx;
7720 LISTVIEW_ScrollColumns(infoPtr, lpnmh->iItem + 1, dx);
7721 if (uView == LVS_REPORT && is_redrawing(infoPtr))
7723 /* this trick works for left aligned columns only */
7724 if ((lpColumnInfo->fmt & LVCFMT_JUSTIFYMASK) == LVCFMT_LEFT)
7726 rcCol.right = min (rcCol.right, lpColumnInfo->rcHeader.right);
7727 rcCol.left = max (rcCol.left, rcCol.right - 3 * infoPtr->ntmAveCharWidth);
7729 rcCol.top = infoPtr->rcList.top;
7730 rcCol.bottom = infoPtr->rcList.bottom;
7731 LISTVIEW_InvalidateRect(infoPtr, &rcCol);
7735 break;
7737 case HDN_ITEMCLICKW:
7738 case HDN_ITEMCLICKA:
7740 /* Handle sorting by Header Column */
7741 NMLISTVIEW nmlv;
7743 ZeroMemory(&nmlv, sizeof(NMLISTVIEW));
7744 nmlv.iItem = -1;
7745 nmlv.iSubItem = lpnmh->iItem;
7746 notify_listview(infoPtr, LVN_COLUMNCLICK, &nmlv);
7748 break;
7751 return 0;
7754 /***
7755 * DESCRIPTION:
7756 * Determines the type of structure to use.
7758 * PARAMETER(S):
7759 * [I] infoPtr : valid pointer to the listview structureof the sender
7760 * [I] hwndFrom : listview window handle
7761 * [I] nCommand : command specifying the nature of the WM_NOTIFYFORMAT
7763 * RETURN:
7764 * Zero
7766 static LRESULT LISTVIEW_NotifyFormat(LISTVIEW_INFO *infoPtr, HWND hwndFrom, INT nCommand)
7768 TRACE("(hwndFrom=%p, nCommand=%d)\n", hwndFrom, nCommand);
7770 if (nCommand != NF_REQUERY) return 0;
7772 infoPtr->notifyFormat = SendMessageW(hwndFrom, WM_NOTIFYFORMAT, (WPARAM)infoPtr->hwndSelf, NF_QUERY);
7774 return 0;
7777 /***
7778 * DESCRIPTION:
7779 * Paints/Repaints the listview control.
7781 * PARAMETER(S):
7782 * [I] infoPtr : valid pointer to the listview structure
7783 * [I] hdc : device context handle
7785 * RETURN:
7786 * Zero
7788 static LRESULT LISTVIEW_Paint(LISTVIEW_INFO *infoPtr, HDC hdc)
7790 TRACE("(hdc=%p)\n", hdc);
7792 if (infoPtr->bFirstPaint)
7794 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
7796 infoPtr->bFirstPaint = FALSE;
7797 LISTVIEW_UpdateItemSize(infoPtr);
7798 if (uView == LVS_ICON || uView == LVS_SMALLICON)
7799 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
7800 LISTVIEW_UpdateScroll(infoPtr);
7802 if (hdc)
7803 LISTVIEW_Refresh(infoPtr, hdc);
7804 else
7806 PAINTSTRUCT ps;
7808 hdc = BeginPaint(infoPtr->hwndSelf, &ps);
7809 if (!hdc) return 1;
7810 if (ps.fErase) LISTVIEW_FillBkgnd(infoPtr, hdc, &ps.rcPaint);
7811 LISTVIEW_Refresh(infoPtr, hdc);
7812 EndPaint(infoPtr->hwndSelf, &ps);
7815 return 0;
7818 /***
7819 * DESCRIPTION:
7820 * Processes double click messages (right mouse button).
7822 * PARAMETER(S):
7823 * [I] infoPtr : valid pointer to the listview structure
7824 * [I] wKey : key flag
7825 * [I] pts : mouse coordinate
7827 * RETURN:
7828 * Zero
7830 static LRESULT LISTVIEW_RButtonDblClk(LISTVIEW_INFO *infoPtr, WORD wKey, POINTS pts)
7832 LVHITTESTINFO lvHitTestInfo;
7834 TRACE("(key=%hu,X=%hu,Y=%hu)\n", wKey, pts.x, pts.y);
7836 /* send NM_RELEASEDCAPTURE notification */
7837 notify(infoPtr, NM_RELEASEDCAPTURE);
7839 /* send NM_RDBLCLK notification */
7840 lvHitTestInfo.pt.x = pts.x;
7841 lvHitTestInfo.pt.y = pts.y;
7842 LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, FALSE);
7843 notify_click(infoPtr, NM_RDBLCLK, &lvHitTestInfo);
7845 return 0;
7848 /***
7849 * DESCRIPTION:
7850 * Processes mouse down messages (right mouse button).
7852 * PARAMETER(S):
7853 * [I] infoPtr : valid pointer to the listview structure
7854 * [I] wKey : key flag
7855 * [I] pts : mouse coordinate
7857 * RETURN:
7858 * Zero
7860 static LRESULT LISTVIEW_RButtonDown(LISTVIEW_INFO *infoPtr, WORD wKey, POINTS pts)
7862 LVHITTESTINFO lvHitTestInfo;
7863 INT nItem;
7865 TRACE("(key=%hu,X=%hu,Y=%hu)\n", wKey, pts.x, pts.y);
7867 /* send NM_RELEASEDCAPTURE notification */
7868 notify(infoPtr, NM_RELEASEDCAPTURE);
7870 /* make sure the listview control window has the focus */
7871 if (!infoPtr->bFocus) SetFocus(infoPtr->hwndSelf);
7873 /* set right button down flag */
7874 infoPtr->bRButtonDown = TRUE;
7876 /* determine the index of the selected item */
7877 lvHitTestInfo.pt.x = pts.x;
7878 lvHitTestInfo.pt.y = pts.y;
7879 nItem = LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, TRUE);
7881 if ((nItem >= 0) && (nItem < infoPtr->nItemCount))
7883 LISTVIEW_SetItemFocus(infoPtr, nItem);
7884 if (!((wKey & MK_SHIFT) || (wKey & MK_CONTROL)) &&
7885 !LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED))
7886 LISTVIEW_SetSelection(infoPtr, nItem);
7888 else
7890 LISTVIEW_DeselectAll(infoPtr);
7893 return 0;
7896 /***
7897 * DESCRIPTION:
7898 * Processes mouse up messages (right mouse button).
7900 * PARAMETER(S):
7901 * [I] infoPtr : valid pointer to the listview structure
7902 * [I] wKey : key flag
7903 * [I] pts : mouse coordinate
7905 * RETURN:
7906 * Zero
7908 static LRESULT LISTVIEW_RButtonUp(LISTVIEW_INFO *infoPtr, WORD wKey, POINTS pts)
7910 LVHITTESTINFO lvHitTestInfo;
7911 POINT pt;
7913 TRACE("(key=%hu,X=%hu,Y=%hu)\n", wKey, pts.x, pts.y);
7915 if (!infoPtr->bRButtonDown) return 0;
7917 /* set button flag */
7918 infoPtr->bRButtonDown = FALSE;
7920 /* Send NM_RClICK notification */
7921 lvHitTestInfo.pt.x = pts.x;
7922 lvHitTestInfo.pt.y = pts.y;
7923 LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, FALSE);
7924 notify_click(infoPtr, NM_RCLICK, &lvHitTestInfo);
7926 /* Change to screen coordinate for WM_CONTEXTMENU */
7927 pt = lvHitTestInfo.pt;
7928 ClientToScreen(infoPtr->hwndSelf, &pt);
7930 /* Send a WM_CONTEXTMENU message in response to the RBUTTONUP */
7931 SendMessageW(infoPtr->hwndSelf, WM_CONTEXTMENU,
7932 (WPARAM)infoPtr->hwndSelf, MAKELPARAM(pt.x, pt.y));
7934 return 0;
7938 /***
7939 * DESCRIPTION:
7940 * Sets the cursor.
7942 * PARAMETER(S):
7943 * [I] infoPtr : valid pointer to the listview structure
7944 * [I] hwnd : window handle of window containing the cursor
7945 * [I] nHittest : hit-test code
7946 * [I] wMouseMsg : ideintifier of the mouse message
7948 * RETURN:
7949 * TRUE if cursor is set
7950 * FALSE otherwise
7952 static BOOL LISTVIEW_SetCursor(LISTVIEW_INFO *infoPtr, HWND hwnd, UINT nHittest, UINT wMouseMsg)
7954 LVHITTESTINFO lvHitTestInfo;
7956 if(!(infoPtr->dwLvExStyle & LVS_EX_TRACKSELECT)) return FALSE;
7958 if(!infoPtr->hHotCursor) return FALSE;
7960 GetCursorPos(&lvHitTestInfo.pt);
7961 if (LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, FALSE, FALSE) < 0) return FALSE;
7963 SetCursor(infoPtr->hHotCursor);
7965 return TRUE;
7968 /***
7969 * DESCRIPTION:
7970 * Sets the focus.
7972 * PARAMETER(S):
7973 * [I] infoPtr : valid pointer to the listview structure
7974 * [I] hwndLoseFocus : handle of previously focused window
7976 * RETURN:
7977 * Zero
7979 static LRESULT LISTVIEW_SetFocus(LISTVIEW_INFO *infoPtr, HWND hwndLoseFocus)
7981 TRACE("(hwndLoseFocus=%p)\n", hwndLoseFocus);
7983 /* if we have the focus already, there's nothing to do */
7984 if (infoPtr->bFocus) return 0;
7986 /* send NM_SETFOCUS notification */
7987 notify(infoPtr, NM_SETFOCUS);
7989 /* set window focus flag */
7990 infoPtr->bFocus = TRUE;
7992 /* put the focus rect back on */
7993 LISTVIEW_ShowFocusRect(infoPtr, TRUE);
7995 /* redraw all visible selected items */
7996 LISTVIEW_InvalidateSelectedItems(infoPtr);
7998 return 0;
8001 /***
8002 * DESCRIPTION:
8003 * Sets the font.
8005 * PARAMETER(S):
8006 * [I] infoPtr : valid pointer to the listview structure
8007 * [I] fRedraw : font handle
8008 * [I] fRedraw : redraw flag
8010 * RETURN:
8011 * Zero
8013 static LRESULT LISTVIEW_SetFont(LISTVIEW_INFO *infoPtr, HFONT hFont, WORD fRedraw)
8015 HFONT oldFont = infoPtr->hFont;
8017 TRACE("(hfont=%p,redraw=%hu)\n", hFont, fRedraw);
8019 infoPtr->hFont = hFont ? hFont : infoPtr->hDefaultFont;
8020 if (infoPtr->hFont == oldFont) return 0;
8022 LISTVIEW_SaveTextMetrics(infoPtr);
8024 if ((infoPtr->dwStyle & LVS_TYPEMASK) == LVS_REPORT)
8025 SendMessageW(infoPtr->hwndHeader, WM_SETFONT, (WPARAM)hFont, MAKELPARAM(fRedraw, 0));
8027 if (fRedraw) LISTVIEW_InvalidateList(infoPtr);
8029 return 0;
8032 /***
8033 * DESCRIPTION:
8034 * Message handling for WM_SETREDRAW.
8035 * For the Listview, it invalidates the entire window (the doc specifies otherwise)
8037 * PARAMETER(S):
8038 * [I] infoPtr : valid pointer to the listview structure
8039 * [I] bRedraw: state of redraw flag
8041 * RETURN:
8042 * DefWinProc return value
8044 static LRESULT LISTVIEW_SetRedraw(LISTVIEW_INFO *infoPtr, BOOL bRedraw)
8046 TRACE("infoPtr->bRedraw=%d, bRedraw=%d\n", infoPtr->bRedraw, bRedraw);
8048 /* we can not use straight equality here because _any_ non-zero value is TRUE */
8049 if ((infoPtr->bRedraw && bRedraw) || (!infoPtr->bRedraw && !bRedraw)) return 0;
8051 infoPtr->bRedraw = bRedraw;
8053 if(!bRedraw) return 0;
8055 if (is_autoarrange(infoPtr))
8056 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
8057 LISTVIEW_UpdateScroll(infoPtr);
8059 /* despite what the WM_SETREDRAW docs says, apps expect us
8060 * to invalidate the listview here... stupid! */
8061 LISTVIEW_InvalidateList(infoPtr);
8063 return 0;
8066 /***
8067 * DESCRIPTION:
8068 * Resizes the listview control. This function processes WM_SIZE
8069 * messages. At this time, the width and height are not used.
8071 * PARAMETER(S):
8072 * [I] infoPtr : valid pointer to the listview structure
8073 * [I] Width : new width
8074 * [I] Height : new height
8076 * RETURN:
8077 * Zero
8079 static LRESULT LISTVIEW_Size(LISTVIEW_INFO *infoPtr, int Width, int Height)
8081 RECT rcOld = infoPtr->rcList;
8083 TRACE("(width=%d, height=%d)\n", Width, Height);
8085 LISTVIEW_UpdateSize(infoPtr);
8086 if (EqualRect(&rcOld, &infoPtr->rcList)) return 0;
8088 /* do not bother with display related stuff if we're not redrawing */
8089 if (!is_redrawing(infoPtr)) return 0;
8091 if (is_autoarrange(infoPtr))
8092 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
8094 LISTVIEW_UpdateScroll(infoPtr);
8096 /* refresh all only for lists whose height changed significantly */
8097 if ((infoPtr->dwStyle & LVS_TYPEMASK) == LVS_LIST &&
8098 (rcOld.bottom - rcOld.top) / infoPtr->nItemHeight !=
8099 (infoPtr->rcList.bottom - infoPtr->rcList.top) / infoPtr->nItemHeight)
8100 LISTVIEW_InvalidateList(infoPtr);
8102 return 0;
8105 /***
8106 * DESCRIPTION:
8107 * Sets the size information.
8109 * PARAMETER(S):
8110 * [I] infoPtr : valid pointer to the listview structure
8112 * RETURN:
8113 * None
8115 static void LISTVIEW_UpdateSize(LISTVIEW_INFO *infoPtr)
8117 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
8119 GetClientRect(infoPtr->hwndSelf, &infoPtr->rcList);
8121 if (uView == LVS_LIST)
8123 /* Apparently the "LIST" style is supposed to have the same
8124 * number of items in a column even if there is no scroll bar.
8125 * Since if a scroll bar already exists then the bottom is already
8126 * reduced, only reduce if the scroll bar does not currently exist.
8127 * The "2" is there to mimic the native control. I think it may be
8128 * related to either padding or edges. (GLA 7/2002)
8130 if (!(infoPtr->dwStyle & WS_HSCROLL))
8131 infoPtr->rcList.bottom -= GetSystemMetrics(SM_CYHSCROLL);
8132 infoPtr->rcList.bottom = max (infoPtr->rcList.bottom - 2, 0);
8134 else if (uView == LVS_REPORT)
8136 HDLAYOUT hl;
8137 WINDOWPOS wp;
8139 hl.prc = &infoPtr->rcList;
8140 hl.pwpos = &wp;
8141 Header_Layout(infoPtr->hwndHeader, &hl);
8143 SetWindowPos(wp.hwnd, wp.hwndInsertAfter, wp.x, wp.y, wp.cx, wp.cy, wp.flags);
8145 if (!(infoPtr->dwStyle & LVS_NOCOLUMNHEADER))
8146 infoPtr->rcList.top = max(wp.cy, 0);
8150 /***
8151 * DESCRIPTION:
8152 * Processes WM_STYLECHANGED messages.
8154 * PARAMETER(S):
8155 * [I] infoPtr : valid pointer to the listview structure
8156 * [I] wStyleType : window style type (normal or extended)
8157 * [I] lpss : window style information
8159 * RETURN:
8160 * Zero
8162 static INT LISTVIEW_StyleChanged(LISTVIEW_INFO *infoPtr, WPARAM wStyleType,
8163 const STYLESTRUCT *lpss)
8165 UINT uNewView = lpss->styleNew & LVS_TYPEMASK;
8166 UINT uOldView = lpss->styleOld & LVS_TYPEMASK;
8168 TRACE("(styletype=%x, styleOld=0x%08lx, styleNew=0x%08lx)\n",
8169 wStyleType, lpss->styleOld, lpss->styleNew);
8171 if (wStyleType != GWL_STYLE) return 0;
8173 /* FIXME: if LVS_NOSORTHEADER changed, update header */
8174 /* what if LVS_OWNERDATA changed? */
8175 /* or LVS_SINGLESEL */
8176 /* or LVS_SORT{AS,DES}CENDING */
8178 infoPtr->dwStyle = lpss->styleNew;
8180 if (((lpss->styleOld & WS_HSCROLL) != 0)&&
8181 ((lpss->styleNew & WS_HSCROLL) == 0))
8182 ShowScrollBar(infoPtr->hwndSelf, SB_HORZ, FALSE);
8184 if (((lpss->styleOld & WS_VSCROLL) != 0)&&
8185 ((lpss->styleNew & WS_VSCROLL) == 0))
8186 ShowScrollBar(infoPtr->hwndSelf, SB_VERT, FALSE);
8188 if (uNewView != uOldView)
8190 SIZE oldIconSize = infoPtr->iconSize;
8191 HIMAGELIST himl;
8193 SendMessageW(infoPtr->hwndEdit, WM_KILLFOCUS, 0, 0);
8194 ShowWindow(infoPtr->hwndHeader, SW_HIDE);
8196 ShowScrollBar(infoPtr->hwndSelf, SB_BOTH, FALSE);
8197 SetRectEmpty(&infoPtr->rcFocus);
8199 himl = (uNewView == LVS_ICON ? infoPtr->himlNormal : infoPtr->himlSmall);
8200 set_icon_size(&infoPtr->iconSize, himl, uNewView != LVS_ICON);
8202 if (uNewView == LVS_ICON)
8204 if ((infoPtr->iconSize.cx != oldIconSize.cx) || (infoPtr->iconSize.cy != oldIconSize.cy))
8206 TRACE("icon old size=(%ld,%ld), new size=(%ld,%ld)\n",
8207 oldIconSize.cx, oldIconSize.cy, infoPtr->iconSize.cx, infoPtr->iconSize.cy);
8208 LISTVIEW_SetIconSpacing(infoPtr, 0);
8211 else if (uNewView == LVS_REPORT)
8213 HDLAYOUT hl;
8214 WINDOWPOS wp;
8216 hl.prc = &infoPtr->rcList;
8217 hl.pwpos = &wp;
8218 Header_Layout(infoPtr->hwndHeader, &hl);
8219 SetWindowPos(infoPtr->hwndHeader, infoPtr->hwndSelf, wp.x, wp.y, wp.cx, wp.cy, wp.flags);
8222 LISTVIEW_UpdateItemSize(infoPtr);
8225 if (uNewView == LVS_REPORT)
8226 ShowWindow(infoPtr->hwndHeader, (LVS_NOCOLUMNHEADER & lpss->styleNew) ? SW_HIDE : SW_SHOWNORMAL);
8228 if ( (uNewView == LVS_ICON || uNewView == LVS_SMALLICON) &&
8229 (uNewView != uOldView || ((lpss->styleNew ^ lpss->styleOld) & LVS_ALIGNMASK)) )
8230 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
8232 /* update the size of the client area */
8233 LISTVIEW_UpdateSize(infoPtr);
8235 /* add scrollbars if needed */
8236 LISTVIEW_UpdateScroll(infoPtr);
8238 /* invalidate client area + erase background */
8239 LISTVIEW_InvalidateList(infoPtr);
8241 return 0;
8244 /***
8245 * DESCRIPTION:
8246 * Window procedure of the listview control.
8249 static LRESULT WINAPI
8250 LISTVIEW_WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
8252 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)GetWindowLongW(hwnd, 0);
8254 TRACE("(uMsg=%x wParam=%x lParam=%lx)\n", uMsg, wParam, lParam);
8256 if (!infoPtr && (uMsg != WM_CREATE))
8257 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
8259 if (infoPtr)
8261 infoPtr->dwStyle = GetWindowLongW(hwnd, GWL_STYLE);
8264 switch (uMsg)
8266 case LVM_APPROXIMATEVIEWRECT:
8267 return LISTVIEW_ApproximateViewRect(infoPtr, (INT)wParam,
8268 LOWORD(lParam), HIWORD(lParam));
8269 case LVM_ARRANGE:
8270 return LISTVIEW_Arrange(infoPtr, (INT)wParam);
8272 /* case LVM_CANCELEDITLABEL: */
8274 /* case LVM_CREATEDRAGIMAGE: */
8276 case LVM_DELETEALLITEMS:
8277 return LISTVIEW_DeleteAllItems(infoPtr);
8279 case LVM_DELETECOLUMN:
8280 return LISTVIEW_DeleteColumn(infoPtr, (INT)wParam);
8282 case LVM_DELETEITEM:
8283 return LISTVIEW_DeleteItem(infoPtr, (INT)wParam);
8285 case LVM_EDITLABELW:
8286 return (LRESULT)LISTVIEW_EditLabelT(infoPtr, (INT)wParam, TRUE);
8288 case LVM_EDITLABELA:
8289 return (LRESULT)LISTVIEW_EditLabelT(infoPtr, (INT)wParam, FALSE);
8291 /* case LVM_ENABLEGROUPVIEW: */
8293 case LVM_ENSUREVISIBLE:
8294 return LISTVIEW_EnsureVisible(infoPtr, (INT)wParam, (BOOL)lParam);
8296 case LVM_FINDITEMW:
8297 return LISTVIEW_FindItemW(infoPtr, (INT)wParam, (LPLVFINDINFOW)lParam);
8299 case LVM_FINDITEMA:
8300 return LISTVIEW_FindItemA(infoPtr, (INT)wParam, (LPLVFINDINFOA)lParam);
8302 case LVM_GETBKCOLOR:
8303 return infoPtr->clrBk;
8305 /* case LVM_GETBKIMAGE: */
8307 case LVM_GETCALLBACKMASK:
8308 return infoPtr->uCallbackMask;
8310 case LVM_GETCOLUMNA:
8311 return LISTVIEW_GetColumnT(infoPtr, (INT)wParam, (LPLVCOLUMNW)lParam, FALSE);
8313 case LVM_GETCOLUMNW:
8314 return LISTVIEW_GetColumnT(infoPtr, (INT)wParam, (LPLVCOLUMNW)lParam, TRUE);
8316 case LVM_GETCOLUMNORDERARRAY:
8317 return LISTVIEW_GetColumnOrderArray(infoPtr, (INT)wParam, (LPINT)lParam);
8319 case LVM_GETCOLUMNWIDTH:
8320 return LISTVIEW_GetColumnWidth(infoPtr, (INT)wParam);
8322 case LVM_GETCOUNTPERPAGE:
8323 return LISTVIEW_GetCountPerPage(infoPtr);
8325 case LVM_GETEDITCONTROL:
8326 return (LRESULT)infoPtr->hwndEdit;
8328 case LVM_GETEXTENDEDLISTVIEWSTYLE:
8329 return infoPtr->dwLvExStyle;
8331 /* case LVM_GETGROUPINFO: */
8333 /* case LVM_GETGROUPMETRICS: */
8335 case LVM_GETHEADER:
8336 return (LRESULT)infoPtr->hwndHeader;
8338 case LVM_GETHOTCURSOR:
8339 return (LRESULT)infoPtr->hHotCursor;
8341 case LVM_GETHOTITEM:
8342 return infoPtr->nHotItem;
8344 case LVM_GETHOVERTIME:
8345 return infoPtr->dwHoverTime;
8347 case LVM_GETIMAGELIST:
8348 return (LRESULT)LISTVIEW_GetImageList(infoPtr, (INT)wParam);
8350 /* case LVM_GETINSERTMARK: */
8352 /* case LVM_GETINSERTMARKCOLOR: */
8354 /* case LVM_GETINSERTMARKRECT: */
8356 case LVM_GETISEARCHSTRINGA:
8357 case LVM_GETISEARCHSTRINGW:
8358 FIXME("LVM_GETISEARCHSTRING: unimplemented\n");
8359 return FALSE;
8361 case LVM_GETITEMA:
8362 return LISTVIEW_GetItemExtT(infoPtr, (LPLVITEMW)lParam, FALSE);
8364 case LVM_GETITEMW:
8365 return LISTVIEW_GetItemExtT(infoPtr, (LPLVITEMW)lParam, TRUE);
8367 case LVM_GETITEMCOUNT:
8368 return infoPtr->nItemCount;
8370 case LVM_GETITEMPOSITION:
8371 return LISTVIEW_GetItemPosition(infoPtr, (INT)wParam, (LPPOINT)lParam);
8373 case LVM_GETITEMRECT:
8374 return LISTVIEW_GetItemRect(infoPtr, (INT)wParam, (LPRECT)lParam);
8376 case LVM_GETITEMSPACING:
8377 return LISTVIEW_GetItemSpacing(infoPtr, (BOOL)wParam);
8379 case LVM_GETITEMSTATE:
8380 return LISTVIEW_GetItemState(infoPtr, (INT)wParam, (UINT)lParam);
8382 case LVM_GETITEMTEXTA:
8383 return LISTVIEW_GetItemTextT(infoPtr, (INT)wParam, (LPLVITEMW)lParam, FALSE);
8385 case LVM_GETITEMTEXTW:
8386 return LISTVIEW_GetItemTextT(infoPtr, (INT)wParam, (LPLVITEMW)lParam, TRUE);
8388 case LVM_GETNEXTITEM:
8389 return LISTVIEW_GetNextItem(infoPtr, (INT)wParam, LOWORD(lParam));
8391 case LVM_GETNUMBEROFWORKAREAS:
8392 FIXME("LVM_GETNUMBEROFWORKAREAS: unimplemented\n");
8393 return 1;
8395 case LVM_GETORIGIN:
8396 if (!lParam) return FALSE;
8397 LISTVIEW_GetOrigin(infoPtr, (LPPOINT)lParam);
8398 return TRUE;
8400 /* case LVM_GETOUTLINECOLOR: */
8402 /* case LVM_GETSELECTEDCOLUMN: */
8404 case LVM_GETSELECTEDCOUNT:
8405 return LISTVIEW_GetSelectedCount(infoPtr);
8407 case LVM_GETSELECTIONMARK:
8408 return infoPtr->nSelectionMark;
8410 case LVM_GETSTRINGWIDTHA:
8411 return LISTVIEW_GetStringWidthT(infoPtr, (LPCWSTR)lParam, FALSE);
8413 case LVM_GETSTRINGWIDTHW:
8414 return LISTVIEW_GetStringWidthT(infoPtr, (LPCWSTR)lParam, TRUE);
8416 case LVM_GETSUBITEMRECT:
8417 return LISTVIEW_GetSubItemRect(infoPtr, (UINT)wParam, (LPRECT)lParam);
8419 case LVM_GETTEXTBKCOLOR:
8420 return infoPtr->clrTextBk;
8422 case LVM_GETTEXTCOLOR:
8423 return infoPtr->clrText;
8425 /* case LVM_GETTILEINFO: */
8427 /* case LVM_GETTILEVIEWINFO: */
8429 case LVM_GETTOOLTIPS:
8430 FIXME("LVM_GETTOOLTIPS: unimplemented\n");
8431 return FALSE;
8433 case LVM_GETTOPINDEX:
8434 return LISTVIEW_GetTopIndex(infoPtr);
8436 /*case LVM_GETUNICODEFORMAT:
8437 FIXME("LVM_GETUNICODEFORMAT: unimplemented\n");
8438 return FALSE;*/
8440 /* case LVM_GETVIEW: */
8442 case LVM_GETVIEWRECT:
8443 return LISTVIEW_GetViewRect(infoPtr, (LPRECT)lParam);
8445 case LVM_GETWORKAREAS:
8446 FIXME("LVM_GETWORKAREAS: unimplemented\n");
8447 return FALSE;
8449 /* case LVM_HASGROUP: */
8451 case LVM_HITTEST:
8452 return LISTVIEW_HitTest(infoPtr, (LPLVHITTESTINFO)lParam, FALSE, FALSE);
8454 case LVM_INSERTCOLUMNA:
8455 return LISTVIEW_InsertColumnT(infoPtr, (INT)wParam, (LPLVCOLUMNW)lParam, FALSE);
8457 case LVM_INSERTCOLUMNW:
8458 return LISTVIEW_InsertColumnT(infoPtr, (INT)wParam, (LPLVCOLUMNW)lParam, TRUE);
8460 /* case LVM_INSERTGROUP: */
8462 /* case LVM_INSERTGROUPSORTED: */
8464 case LVM_INSERTITEMA:
8465 return LISTVIEW_InsertItemT(infoPtr, (LPLVITEMW)lParam, FALSE);
8467 case LVM_INSERTITEMW:
8468 return LISTVIEW_InsertItemT(infoPtr, (LPLVITEMW)lParam, TRUE);
8470 /* case LVM_INSERTMARKHITTEST: */
8472 /* case LVM_ISGROUPVIEWENABLED: */
8474 /* case LVM_MAPIDTOINDEX: */
8476 /* case LVM_MAPINDEXTOID: */
8478 /* case LVM_MOVEGROUP: */
8480 /* case LVM_MOVEITEMTOGROUP: */
8482 case LVM_REDRAWITEMS:
8483 return LISTVIEW_RedrawItems(infoPtr, (INT)wParam, (INT)lParam);
8485 /* case LVM_REMOVEALLGROUPS: */
8487 /* case LVM_REMOVEGROUP: */
8489 case LVM_SCROLL:
8490 return LISTVIEW_Scroll(infoPtr, (INT)wParam, (INT)lParam);
8492 case LVM_SETBKCOLOR:
8493 return LISTVIEW_SetBkColor(infoPtr, (COLORREF)lParam);
8495 /* case LVM_SETBKIMAGE: */
8497 case LVM_SETCALLBACKMASK:
8498 infoPtr->uCallbackMask = (UINT)wParam;
8499 return TRUE;
8501 case LVM_SETCOLUMNA:
8502 return LISTVIEW_SetColumnT(infoPtr, (INT)wParam, (LPLVCOLUMNW)lParam, FALSE);
8504 case LVM_SETCOLUMNW:
8505 return LISTVIEW_SetColumnT(infoPtr, (INT)wParam, (LPLVCOLUMNW)lParam, TRUE);
8507 case LVM_SETCOLUMNORDERARRAY:
8508 return LISTVIEW_SetColumnOrderArray(infoPtr, (INT)wParam, (LPINT)lParam);
8510 case LVM_SETCOLUMNWIDTH:
8511 return LISTVIEW_SetColumnWidth(infoPtr, (INT)wParam, SLOWORD(lParam));
8513 case LVM_SETEXTENDEDLISTVIEWSTYLE:
8514 return LISTVIEW_SetExtendedListViewStyle(infoPtr, (DWORD)wParam, (DWORD)lParam);
8516 /* case LVM_SETGROUPINFO: */
8518 /* case LVM_SETGROUPMETRICS: */
8520 case LVM_SETHOTCURSOR:
8521 return (LRESULT)LISTVIEW_SetHotCursor(infoPtr, (HCURSOR)lParam);
8523 case LVM_SETHOTITEM:
8524 return LISTVIEW_SetHotItem(infoPtr, (INT)wParam);
8526 case LVM_SETHOVERTIME:
8527 return LISTVIEW_SetHoverTime(infoPtr, (DWORD)wParam);
8529 case LVM_SETICONSPACING:
8530 return LISTVIEW_SetIconSpacing(infoPtr, (DWORD)lParam);
8532 case LVM_SETIMAGELIST:
8533 return (LRESULT)LISTVIEW_SetImageList(infoPtr, (INT)wParam, (HIMAGELIST)lParam);
8535 /* case LVM_SETINFOTIP: */
8537 /* case LVM_SETINSERTMARK: */
8539 /* case LVM_SETINSERTMARKCOLOR: */
8541 case LVM_SETITEMA:
8542 return LISTVIEW_SetItemT(infoPtr, (LPLVITEMW)lParam, FALSE);
8544 case LVM_SETITEMW:
8545 return LISTVIEW_SetItemT(infoPtr, (LPLVITEMW)lParam, TRUE);
8547 case LVM_SETITEMCOUNT:
8548 return LISTVIEW_SetItemCount(infoPtr, (INT)wParam, (DWORD)lParam);
8550 case LVM_SETITEMPOSITION:
8552 POINT pt = { SLOWORD(lParam), SHIWORD(lParam) };
8553 return LISTVIEW_SetItemPosition(infoPtr, (INT)wParam, pt);
8556 case LVM_SETITEMPOSITION32:
8557 if (lParam == 0) return FALSE;
8558 return LISTVIEW_SetItemPosition(infoPtr, (INT)wParam, *((POINT*)lParam));
8560 case LVM_SETITEMSTATE:
8561 return LISTVIEW_SetItemState(infoPtr, (INT)wParam, (LPLVITEMW)lParam);
8563 case LVM_SETITEMTEXTA:
8564 return LISTVIEW_SetItemTextT(infoPtr, (INT)wParam, (LPLVITEMW)lParam, FALSE);
8566 case LVM_SETITEMTEXTW:
8567 return LISTVIEW_SetItemTextT(infoPtr, (INT)wParam, (LPLVITEMW)lParam, TRUE);
8569 /* case LVM_SETOUTLINECOLOR: */
8571 /* case LVM_SETSELECTEDCOLUMN: */
8573 case LVM_SETSELECTIONMARK:
8574 return LISTVIEW_SetSelectionMark(infoPtr, (INT)lParam);
8576 case LVM_SETTEXTBKCOLOR:
8577 return LISTVIEW_SetTextBkColor(infoPtr, (COLORREF)lParam);
8579 case LVM_SETTEXTCOLOR:
8580 return LISTVIEW_SetTextColor(infoPtr, (COLORREF)lParam);
8582 /* case LVM_SETTILEINFO: */
8584 /* case LVM_SETTILEVIEWINFO: */
8586 /* case LVM_SETTILEWIDTH: */
8588 /* case LVM_SETTOOLTIPS: */
8590 /* case LVM_SETUNICODEFORMAT: */
8592 /* case LVM_SETVIEW: */
8594 /* case LVM_SETWORKAREAS: */
8596 /* case LVM_SORTGROUPS: */
8598 case LVM_SORTITEMS:
8599 return LISTVIEW_SortItems(infoPtr, (PFNLVCOMPARE)lParam, (LPARAM)wParam);
8601 /* LVM_SORTITEMSEX: */
8603 case LVM_SUBITEMHITTEST:
8604 return LISTVIEW_HitTest(infoPtr, (LPLVHITTESTINFO)lParam, TRUE, FALSE);
8606 case LVM_UPDATE:
8607 return LISTVIEW_Update(infoPtr, (INT)wParam);
8609 case WM_CHAR:
8610 return LISTVIEW_ProcessLetterKeys( infoPtr, wParam, lParam );
8612 case WM_COMMAND:
8613 return LISTVIEW_Command(infoPtr, wParam, lParam);
8615 case WM_CREATE:
8616 return LISTVIEW_Create(hwnd, (LPCREATESTRUCTW)lParam);
8618 case WM_ERASEBKGND:
8619 return LISTVIEW_EraseBkgnd(infoPtr, (HDC)wParam);
8621 case WM_GETDLGCODE:
8622 return DLGC_WANTCHARS | DLGC_WANTARROWS;
8624 case WM_GETFONT:
8625 return (LRESULT)infoPtr->hFont;
8627 case WM_HSCROLL:
8628 return LISTVIEW_HScroll(infoPtr, (INT)LOWORD(wParam), 0, (HWND)lParam);
8630 case WM_KEYDOWN:
8631 return LISTVIEW_KeyDown(infoPtr, (INT)wParam, (LONG)lParam);
8633 case WM_KILLFOCUS:
8634 return LISTVIEW_KillFocus(infoPtr);
8636 case WM_LBUTTONDBLCLK:
8637 return LISTVIEW_LButtonDblClk(infoPtr, (WORD)wParam, MAKEPOINTS(lParam));
8639 case WM_LBUTTONDOWN:
8640 return LISTVIEW_LButtonDown(infoPtr, (WORD)wParam, MAKEPOINTS(lParam));
8642 case WM_LBUTTONUP:
8643 return LISTVIEW_LButtonUp(infoPtr, (WORD)wParam, MAKEPOINTS(lParam));
8645 case WM_MOUSEMOVE:
8646 return LISTVIEW_MouseMove (infoPtr, (WORD)wParam, MAKEPOINTS(lParam));
8648 case WM_MOUSEHOVER:
8649 return LISTVIEW_MouseHover(infoPtr, (WORD)wParam, MAKEPOINTS(lParam));
8651 case WM_NCDESTROY:
8652 return LISTVIEW_NCDestroy(infoPtr);
8654 case WM_NOTIFY:
8655 if (lParam && ((LPNMHDR)lParam)->hwndFrom == infoPtr->hwndHeader)
8656 return LISTVIEW_HeaderNotification(infoPtr, (LPNMHEADERW)lParam);
8657 else return 0;
8659 case WM_NOTIFYFORMAT:
8660 return LISTVIEW_NotifyFormat(infoPtr, (HWND)wParam, (INT)lParam);
8662 case WM_PAINT:
8663 return LISTVIEW_Paint(infoPtr, (HDC)wParam);
8665 case WM_RBUTTONDBLCLK:
8666 return LISTVIEW_RButtonDblClk(infoPtr, (WORD)wParam, MAKEPOINTS(lParam));
8668 case WM_RBUTTONDOWN:
8669 return LISTVIEW_RButtonDown(infoPtr, (WORD)wParam, MAKEPOINTS(lParam));
8671 case WM_RBUTTONUP:
8672 return LISTVIEW_RButtonUp(infoPtr, (WORD)wParam, MAKEPOINTS(lParam));
8674 case WM_SETCURSOR:
8675 if(LISTVIEW_SetCursor(infoPtr, (HWND)wParam, LOWORD(lParam), HIWORD(lParam)))
8676 return TRUE;
8677 goto fwd_msg;
8679 case WM_SETFOCUS:
8680 return LISTVIEW_SetFocus(infoPtr, (HWND)wParam);
8682 case WM_SETFONT:
8683 return LISTVIEW_SetFont(infoPtr, (HFONT)wParam, (WORD)lParam);
8685 case WM_SETREDRAW:
8686 return LISTVIEW_SetRedraw(infoPtr, (BOOL)wParam);
8688 case WM_SIZE:
8689 return LISTVIEW_Size(infoPtr, (int)SLOWORD(lParam), (int)SHIWORD(lParam));
8691 case WM_STYLECHANGED:
8692 return LISTVIEW_StyleChanged(infoPtr, wParam, (LPSTYLESTRUCT)lParam);
8694 case WM_SYSCOLORCHANGE:
8695 COMCTL32_RefreshSysColors();
8696 return 0;
8698 /* case WM_TIMER: */
8700 case WM_VSCROLL:
8701 return LISTVIEW_VScroll(infoPtr, (INT)LOWORD(wParam), 0, (HWND)lParam);
8703 case WM_MOUSEWHEEL:
8704 if (wParam & (MK_SHIFT | MK_CONTROL))
8705 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
8706 return LISTVIEW_MouseWheel(infoPtr, (short int)HIWORD(wParam));
8708 case WM_WINDOWPOSCHANGED:
8709 if (!(((WINDOWPOS *)lParam)->flags & SWP_NOSIZE))
8711 SetWindowPos(infoPtr->hwndSelf, 0, 0, 0, 0, 0, SWP_FRAMECHANGED | SWP_NOACTIVATE |
8712 SWP_NOZORDER | SWP_NOMOVE | SWP_NOSIZE);
8713 /* FIXME: why do we need this here? */
8714 LISTVIEW_UpdateSize(infoPtr);
8715 LISTVIEW_UpdateScroll(infoPtr);
8717 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
8719 /* case WM_WININICHANGE: */
8721 default:
8722 if ((uMsg >= WM_USER) && (uMsg < WM_APP))
8723 ERR("unknown msg %04x wp=%08x lp=%08lx\n", uMsg, wParam, lParam);
8725 fwd_msg:
8726 /* call default window procedure */
8727 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
8730 return 0;
8733 /***
8734 * DESCRIPTION:
8735 * Registers the window class.
8737 * PARAMETER(S):
8738 * None
8740 * RETURN:
8741 * None
8743 void LISTVIEW_Register(void)
8745 WNDCLASSW wndClass;
8747 ZeroMemory(&wndClass, sizeof(WNDCLASSW));
8748 wndClass.style = CS_GLOBALCLASS | CS_DBLCLKS;
8749 wndClass.lpfnWndProc = (WNDPROC)LISTVIEW_WindowProc;
8750 wndClass.cbClsExtra = 0;
8751 wndClass.cbWndExtra = sizeof(LISTVIEW_INFO *);
8752 wndClass.hCursor = LoadCursorW(0, IDC_ARROWW);
8753 wndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
8754 wndClass.lpszClassName = WC_LISTVIEWW;
8755 RegisterClassW(&wndClass);
8758 /***
8759 * DESCRIPTION:
8760 * Unregisters the window class.
8762 * PARAMETER(S):
8763 * None
8765 * RETURN:
8766 * None
8768 void LISTVIEW_Unregister(void)
8770 UnregisterClassW(WC_LISTVIEWW, (HINSTANCE)NULL);
8773 /***
8774 * DESCRIPTION:
8775 * Handle any WM_COMMAND messages
8777 * PARAMETER(S):
8778 * [I] infoPtr : valid pointer to the listview structure
8779 * [I] wParam : the first message parameter
8780 * [I] lParam : the second message parameter
8782 * RETURN:
8783 * Zero.
8785 static LRESULT LISTVIEW_Command(LISTVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
8787 switch (HIWORD(wParam))
8789 case EN_UPDATE:
8792 * Adjust the edit window size
8794 WCHAR buffer[1024];
8795 HDC hdc = GetDC(infoPtr->hwndEdit);
8796 HFONT hFont, hOldFont = 0;
8797 RECT rect;
8798 SIZE sz;
8799 int len;
8801 if (!infoPtr->hwndEdit || !hdc) return 0;
8802 len = GetWindowTextW(infoPtr->hwndEdit, buffer, sizeof(buffer)/sizeof(buffer[0]));
8803 GetWindowRect(infoPtr->hwndEdit, &rect);
8805 /* Select font to get the right dimension of the string */
8806 hFont = (HFONT)SendMessageW(infoPtr->hwndEdit, WM_GETFONT, 0, 0);
8807 if(hFont != 0)
8809 hOldFont = SelectObject(hdc, hFont);
8812 if (GetTextExtentPoint32W(hdc, buffer, lstrlenW(buffer), &sz))
8814 TEXTMETRICW textMetric;
8816 /* Add Extra spacing for the next character */
8817 GetTextMetricsW(hdc, &textMetric);
8818 sz.cx += (textMetric.tmMaxCharWidth * 2);
8820 SetWindowPos (
8821 infoPtr->hwndEdit,
8822 HWND_TOP,
8825 sz.cx,
8826 rect.bottom - rect.top,
8827 SWP_DRAWFRAME|SWP_NOMOVE);
8829 if(hFont != 0)
8830 SelectObject(hdc, hOldFont);
8832 ReleaseDC(infoPtr->hwndSelf, hdc);
8834 break;
8837 default:
8838 return SendMessageW (GetParent (infoPtr->hwndSelf), WM_COMMAND, wParam, lParam);
8841 return 0;
8845 /***
8846 * DESCRIPTION:
8847 * Subclassed edit control windproc function
8849 * PARAMETER(S):
8850 * [I] hwnd : the edit window handle
8851 * [I] uMsg : the message that is to be processed
8852 * [I] wParam : first message parameter
8853 * [I] lParam : second message parameter
8854 * [I] isW : TRUE if input is Unicode
8856 * RETURN:
8857 * Zero.
8859 static LRESULT EditLblWndProcT(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL isW)
8861 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)GetWindowLongW(GetParent(hwnd), 0);
8862 BOOL cancel = FALSE;
8864 TRACE("(hwnd=%p, uMsg=%x, wParam=%x, lParam=%lx, isW=%d)\n",
8865 hwnd, uMsg, wParam, lParam, isW);
8867 switch (uMsg)
8869 case WM_GETDLGCODE:
8870 return DLGC_WANTARROWS | DLGC_WANTALLKEYS;
8872 case WM_KILLFOCUS:
8873 break;
8875 case WM_DESTROY:
8877 WNDPROC editProc = infoPtr->EditWndProc;
8878 infoPtr->EditWndProc = 0;
8879 SetWindowLongW(hwnd, GWL_WNDPROC, (LONG)editProc);
8880 return CallWindowProcT(editProc, hwnd, uMsg, wParam, lParam, isW);
8883 case WM_KEYDOWN:
8884 if (VK_ESCAPE == (INT)wParam)
8886 cancel = TRUE;
8887 break;
8889 else if (VK_RETURN == (INT)wParam)
8890 break;
8892 default:
8893 return CallWindowProcT(infoPtr->EditWndProc, hwnd, uMsg, wParam, lParam, isW);
8896 /* kill the edit */
8897 if (infoPtr->hwndEdit)
8899 LPWSTR buffer = NULL;
8901 infoPtr->hwndEdit = 0;
8902 if (!cancel)
8904 DWORD len = isW ? GetWindowTextLengthW(hwnd) : GetWindowTextLengthA(hwnd);
8906 if (len)
8908 if ( (buffer = COMCTL32_Alloc((len+1) * (isW ? sizeof(WCHAR) : sizeof(CHAR)))) )
8910 if (isW) GetWindowTextW(hwnd, buffer, len+1);
8911 else GetWindowTextA(hwnd, (CHAR*)buffer, len+1);
8915 LISTVIEW_EndEditLabelT(infoPtr, buffer, isW);
8917 if (buffer) COMCTL32_Free(buffer);
8921 SendMessageW(hwnd, WM_CLOSE, 0, 0);
8922 return 0;
8925 /***
8926 * DESCRIPTION:
8927 * Subclassed edit control Unicode windproc function
8929 * PARAMETER(S):
8930 * [I] hwnd : the edit window handle
8931 * [I] uMsg : the message that is to be processed
8932 * [I] wParam : first message parameter
8933 * [I] lParam : second message parameter
8935 * RETURN:
8937 LRESULT CALLBACK EditLblWndProcW(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
8939 return EditLblWndProcT(hwnd, uMsg, wParam, lParam, TRUE);
8942 /***
8943 * DESCRIPTION:
8944 * Subclassed edit control ANSI windproc function
8946 * PARAMETER(S):
8947 * [I] hwnd : the edit window handle
8948 * [I] uMsg : the message that is to be processed
8949 * [I] wParam : first message parameter
8950 * [I] lParam : second message parameter
8952 * RETURN:
8954 LRESULT CALLBACK EditLblWndProcA(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
8956 return EditLblWndProcT(hwnd, uMsg, wParam, lParam, FALSE);
8959 /***
8960 * DESCRIPTION:
8961 * Creates a subclassed edit cotrol
8963 * PARAMETER(S):
8964 * [I] infoPtr : valid pointer to the listview structure
8965 * [I] text : initial text for the edit
8966 * [I] style : the window style
8967 * [I] isW : TRUE if input is Unicode
8969 * RETURN:
8971 static HWND CreateEditLabelT(LISTVIEW_INFO *infoPtr, LPCWSTR text, DWORD style,
8972 INT x, INT y, INT width, INT height, BOOL isW)
8974 WCHAR editName[5] = { 'E', 'd', 'i', 't', '\0' };
8975 HWND hedit;
8976 SIZE sz;
8977 HDC hdc;
8978 HDC hOldFont=0;
8979 TEXTMETRICW textMetric;
8980 HINSTANCE hinst = (HINSTANCE)GetWindowLongW(infoPtr->hwndSelf, GWL_HINSTANCE);
8982 TRACE("(text=%s, ..., isW=%d)\n", debugtext_t(text, isW), isW);
8984 style |= WS_CHILDWINDOW|WS_CLIPSIBLINGS|ES_LEFT|WS_BORDER;
8985 hdc = GetDC(infoPtr->hwndSelf);
8987 /* Select the font to get appropriate metric dimensions */
8988 if(infoPtr->hFont != 0)
8989 hOldFont = SelectObject(hdc, infoPtr->hFont);
8991 /*Get String Lenght in pixels */
8992 GetTextExtentPoint32W(hdc, text, lstrlenW(text), &sz);
8994 /*Add Extra spacing for the next character */
8995 GetTextMetricsW(hdc, &textMetric);
8996 sz.cx += (textMetric.tmMaxCharWidth * 2);
8998 if(infoPtr->hFont != 0)
8999 SelectObject(hdc, hOldFont);
9001 ReleaseDC(infoPtr->hwndSelf, hdc);
9002 if (isW)
9003 hedit = CreateWindowW(editName, text, style, x, y, sz.cx, height, infoPtr->hwndSelf, 0, hinst, 0);
9004 else
9005 hedit = CreateWindowA("Edit", (LPCSTR)text, style, x, y, sz.cx, height, infoPtr->hwndSelf, 0, hinst, 0);
9007 if (!hedit) return 0;
9009 infoPtr->EditWndProc = (WNDPROC)
9010 (isW ? SetWindowLongW(hedit, GWL_WNDPROC, (LONG)EditLblWndProcW) :
9011 SetWindowLongA(hedit, GWL_WNDPROC, (LONG)EditLblWndProcA) );
9013 SendMessageW(hedit, WM_SETFONT, (WPARAM)infoPtr->hFont, FALSE);
9015 return hedit;