Use iterators to render over the columns, in report mode.
[wine/wine-kai.git] / dlls / comctl32 / listview.c
blob08cd11f3d4fb27123d35fe01acd3a46b466b9a07
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 * -- LVSICF_NOINVALIDATEALL, LVSICF_NOSCROLL not implemented
52 * -- LISTVIEW_SortItems is broken
53 * -- LISTVIEW_StyleChanged doesn't handle some changes too well
55 * Speedups
56 * -- LISTVIEW_SetItemCount is too invalidation happy
57 * -- LISTVIEW_Size invalidates too much
58 * -- in sorted mode, LISTVIEW_InsertItemT sorts the array,
59 * instead of inserting in the right spot
60 * -- we should keep an ordered array of coordinates in iconic mode
61 * this would allow to frame items (iterator_frameditems),
62 * and find nearest item (LVFI_NEARESTXY) a lot more efficiently
64 * Flags
65 * -- LVIF_COLUMNS
66 * -- LVIF_GROUPID
67 * -- LVIF_NORECOMPUTE
69 * States
70 * -- LVIS_ACTIVATING (not currently supported by comctl32.dll version 6.0)
71 * -- LVIS_CUT
72 * -- LVIS_DROPHILITED
73 * -- LVIS_OVERLAYMASK
75 * Styles
76 * -- LVS_NOLABELWRAP
77 * -- LVS_NOSCROLL (see Q137520)
78 * -- LVS_SORTASCENDING, LVS_SORTDESCENDING
80 * Extended Styles
81 * -- LVS_EX_BORDERSELECT
82 * -- LVS_EX_CHECKBOXES
83 * -- LVS_EX_FLATSB
84 * -- LVS_EX_GRIDLINES
85 * -- LVS_EX_HEADERDRAGDROP
86 * -- LVS_EX_INFOTIP
87 * -- LVS_EX_LABELTIP
88 * -- LVS_EX_MULTIWORKAREAS
89 * -- LVS_EX_ONECLICKACTIVATE
90 * -- LVS_EX_REGIONAL
91 * -- LVS_EX_SIMPLESELECT
92 * -- LVS_EX_SUBITEMIMAGES
93 * -- LVS_EX_TRACKSELECT
94 * -- LVS_EX_TWOCLICKACTIVATE
95 * -- LVS_EX_UNDERLINECOLD
96 * -- LVS_EX_UNDERLINEHOT
98 * Notifications:
99 * -- LVN_BEGINDRAG, LVN_BEGINRDRAG
100 * -- LVN_BEGINSCROLL, LVN_ENDSCROLL
101 * -- LVN_GETINFOTIP
102 * -- LVN_HOTTRACK
103 * -- LVN_MARQUEEBEGIN
104 * -- LVN_ODFINDITEM
105 * -- LVN_ODSTATECHANGED
106 * -- LVN_SETDISPINFO
107 * -- NM_HOVER
109 * Messages:
110 * -- LVM_CANCELEDITLABEL
111 * -- LVM_CREATEDRAGIMAGE
112 * -- LVM_ENABLEGROUPVIEW
113 * -- LVM_GETBKIMAGE, LVM_SETBKIMAGE
114 * -- LVM_GETGROUPINFO, LVM_SETGROUPINFO
115 * -- LVM_GETGROUPMETRICS, LVM_SETGROUPMETRICS
116 * -- LVM_GETINSERTMARK, LVM_SETINSERTMARK
117 * -- LVM_GETINSERTMARKCOLOR, LVM_SETINSERTMARKCOLOR
118 * -- LVM_GETINSERTMARKRECT
119 * -- LVM_GETNUMBEROFWORKAREAS
120 * -- LVM_GETOUTLINECOLOR, LVM_SETOUTLINECOLOR
121 * -- LVM_GETSELECTEDCOLUMN, LVM_SETSELECTEDCOLUMN
122 * -- LVM_GETISEARCHSTRINGW, LVM_GETISEARCHSTRINGA
123 * -- LVM_GETTILEINFO, LVM_SETTILEINFO
124 * -- LVM_GETTILEVIEWINFO, LVM_SETTILEVIEWINFO
125 * -- LVM_GETTOOLTIPS, LVM_SETTOOLTIPS
126 * -- LVM_GETUNICODEFORMAT, LVM_SETUNICODEFORMAT
127 * -- LVM_GETVIEW, LVM_SETVIEW
128 * -- LVM_GETWORKAREAS, LVM_SETWORKAREAS
129 * -- LVM_HASGROUP, LVM_INSERTGROUP, LVM_REMOVEGROUP, LVM_REMOVEALLGROUPS
130 * -- LVM_INSERTGROUPSORTED
131 * -- LVM_INSERTMARKHITTEST
132 * -- LVM_ISGROUPVIEWENABLED
133 * -- LVM_MAPIDTOINDEX, LVM_MAPINDEXTOID
134 * -- LVM_MOVEGROUP
135 * -- LVM_MOVEITEMTOGROUP
136 * -- LVM_SETINFOTIP
137 * -- LVM_SETTILEWIDTH
138 * -- LVM_SORTGROUPS
139 * -- LVM_SORTITEMSEX
141 * Known differences in message stream from native control (not known if
142 * these differences cause problems):
143 * LVM_INSERTITEM issues LVM_SETITEMSTATE and LVM_SETITEM in certain cases.
144 * LVM_SETITEM does not always issue LVN_ITEMCHANGING/LVN_ITEMCHANGED.
145 * WM_CREATE does not issue WM_QUERYUISTATE and associated registry
146 * processing for "USEDOUBLECLICKTIME".
149 #include "config.h"
150 #include "wine/port.h"
152 #include <assert.h>
153 #include <ctype.h>
154 #include <string.h>
155 #include <stdlib.h>
156 #include <stdio.h>
158 #include "winbase.h"
159 #include "winnt.h"
160 #include "heap.h"
161 #include "commctrl.h"
162 #include "comctl32.h"
164 #include "wine/debug.h"
165 #include "wine/unicode.h"
167 WINE_DEFAULT_DEBUG_CHANNEL(listview);
169 /* make sure you set this to 0 for production use! */
170 #define DEBUG_RANGES 1
172 typedef struct tagCOLUMN_INFO
174 RECT rcHeader; /* tracks the header's rectangle */
175 int fmt; /* same as LVCOLUMN.fmt */
176 } COLUMN_INFO;
178 typedef struct tagITEMHDR
180 LPWSTR pszText;
181 INT iImage;
182 } ITEMHDR, *LPITEMHDR;
184 typedef struct tagSUBITEM_INFO
186 ITEMHDR hdr;
187 INT iSubItem;
188 } SUBITEM_INFO;
190 typedef struct tagITEM_INFO
192 ITEMHDR hdr;
193 UINT state;
194 LPARAM lParam;
195 INT iIndent;
196 } ITEM_INFO;
198 typedef struct tagRANGE
200 INT lower;
201 INT upper;
202 } RANGE;
204 typedef struct tagRANGES
206 HDPA hdpa;
207 } *RANGES;
209 typedef struct tagITERATOR
211 INT nItem;
212 INT nSpecial;
213 RANGE range;
214 RANGES ranges;
215 INT index;
216 } ITERATOR;
218 typedef struct tagLISTVIEW_INFO
220 HWND hwndSelf;
221 HBRUSH hBkBrush;
222 COLORREF clrBk;
223 COLORREF clrText;
224 COLORREF clrTextBk;
225 COLORREF clrTextBkDefault;
226 HIMAGELIST himlNormal;
227 HIMAGELIST himlSmall;
228 HIMAGELIST himlState;
229 BOOL bLButtonDown;
230 BOOL bRButtonDown;
231 INT nItemHeight;
232 INT nItemWidth;
233 RANGES selectionRanges;
234 INT nSelectionMark;
235 INT nHotItem;
236 SHORT notifyFormat;
237 RECT rcList; /* This rectangle is really the window
238 * client rectangle possibly reduced by the
239 * horizontal scroll bar and/or header - see
240 * LISTVIEW_UpdateSize. This rectangle offset
241 * by the LISTVIEW_GetOrigin value is in
242 * client coordinates */
243 SIZE iconSize;
244 SIZE iconSpacing;
245 SIZE iconStateSize;
246 UINT uCallbackMask;
247 HWND hwndHeader;
248 HCURSOR hHotCursor;
249 HFONT hDefaultFont;
250 HFONT hFont;
251 INT ntmHeight; /* Some cached metrics of the font used */
252 INT ntmAveCharWidth; /* by the listview to draw items */
253 BOOL bRedraw; /* Turns on/off repaints & invalidations */
254 BOOL bFirstPaint; /* Flags if the control has never painted before */
255 BOOL bAutoarrange; /* Autoarrange flag when NOT in LVS_AUTOARRANGE */
256 BOOL bFocus;
257 INT nFocusedItem;
258 RECT rcFocus;
259 DWORD dwStyle; /* the cached window GWL_STYLE */
260 DWORD dwLvExStyle; /* extended listview style */
261 INT nItemCount; /* the number of items in the list */
262 HDPA hdpaItems; /* array ITEM_INFO pointers */
263 HDPA hdpaPosX; /* maintains the (X, Y) coordinates of the */
264 HDPA hdpaPosY; /* items in LVS_ICON, and LVS_SMALLICON modes */
265 HDPA hdpaColumns; /* array of COLUMN_INFO pointers */
266 POINT currIconPos; /* this is the position next icon will be placed */
267 PFNLVCOMPARE pfnCompare;
268 LPARAM lParamSort;
269 HWND hwndEdit;
270 WNDPROC EditWndProc;
271 INT nEditLabelItem;
272 DWORD dwHoverTime;
274 DWORD lastKeyPressTimestamp;
275 WPARAM charCode;
276 INT nSearchParamLength;
277 WCHAR szSearchParam[ MAX_PATH ];
278 BOOL bIsDrawing;
279 } LISTVIEW_INFO;
282 * constants
284 /* How many we debug buffer to allocate */
285 #define DEBUG_BUFFERS 20
286 /* The size of a single debug bbuffer */
287 #define DEBUG_BUFFER_SIZE 256
289 /* Internal interface to LISTVIEW_HScroll and LISTVIEW_VScroll */
290 #define SB_INTERNAL -1
292 /* maximum size of a label */
293 #define DISP_TEXT_SIZE 512
295 /* padding for items in list and small icon display modes */
296 #define WIDTH_PADDING 12
298 /* padding for items in list, report and small icon display modes */
299 #define HEIGHT_PADDING 1
301 /* offset of items in report display mode */
302 #define REPORT_MARGINX 2
304 /* padding for icon in large icon display mode
305 * ICON_TOP_PADDING_NOTHITABLE - space between top of box and area
306 * that HITTEST will see.
307 * ICON_TOP_PADDING_HITABLE - spacing between above and icon.
308 * ICON_TOP_PADDING - sum of the two above.
309 * ICON_BOTTOM_PADDING - between bottom of icon and top of text
310 * LABEL_VERT_PADDING - between bottom of text and end of box
312 * ICON_LR_PADDING - additional width above icon size.
313 * ICON_LR_HALF - half of the above value
315 #define ICON_TOP_PADDING_NOTHITABLE 2
316 #define ICON_TOP_PADDING_HITABLE 2
317 #define ICON_TOP_PADDING (ICON_TOP_PADDING_NOTHITABLE + ICON_TOP_PADDING_HITABLE)
318 #define ICON_BOTTOM_PADDING 4
319 #define LABEL_VERT_PADDING 7
320 #define ICON_LR_PADDING 16
321 #define ICON_LR_HALF (ICON_LR_PADDING/2)
323 /* default label width for items in list and small icon display modes */
324 #define DEFAULT_LABEL_WIDTH 40
326 /* default column width for items in list display mode */
327 #define DEFAULT_COLUMN_WIDTH 128
329 /* Size of "line" scroll for V & H scrolls */
330 #define LISTVIEW_SCROLL_ICON_LINE_SIZE 37
332 /* Padding betwen image and label */
333 #define IMAGE_PADDING 2
335 /* Padding behind the label */
336 #define TRAILING_PADDING 5
338 /* Border for the icon caption */
339 #define CAPTION_BORDER 2
341 /* Standard DrawText flags */
342 #define LV_ML_DT_FLAGS (DT_TOP | DT_NOPREFIX | DT_EDITCONTROL | DT_CENTER | DT_WORDBREAK | DT_WORD_ELLIPSIS | DT_END_ELLIPSIS)
343 #define LV_FL_DT_FLAGS (DT_TOP | DT_NOPREFIX | DT_EDITCONTROL | DT_CENTER | DT_WORDBREAK | DT_NOCLIP)
344 #define LV_SL_DT_FLAGS (DT_TOP | DT_EDITCONTROL | DT_SINGLELINE | DT_WORD_ELLIPSIS | DT_END_ELLIPSIS)
346 /* The time in milisecods to reset the search in the list */
347 #define KEY_DELAY 450
349 /* Dump the LISTVIEW_INFO structure to the debug channel */
350 #define LISTVIEW_DUMP(iP) do { \
351 TRACE("hwndSelf=%p, clrBk=0x%06lx, clrText=0x%06lx, clrTextBk=0x%06lx, ItemHeight=%d, ItemWidth=%d, Style=0x%08lx\n", \
352 iP->hwndSelf, iP->clrBk, iP->clrText, iP->clrTextBk, \
353 iP->nItemHeight, iP->nItemWidth, infoPtr->dwStyle); \
354 TRACE("hwndSelf=%p, himlNor=%p, himlSml=%p, himlState=%p, Focused=%d, Hot=%d, exStyle=0x%08lx, Focus=%d\n", \
355 iP->hwndSelf, iP->himlNormal, iP->himlSmall, iP->himlState, \
356 iP->nFocusedItem, iP->nHotItem, iP->dwLvExStyle, iP->bFocus ); \
357 TRACE("hwndSelf=%p, ntmH=%d, icSz.cx=%ld, icSz.cy=%ld, icSp.cx=%ld, icSp.cy=%ld, notifyFmt=%d\n", \
358 iP->hwndSelf, iP->ntmHeight, iP->iconSize.cx, iP->iconSize.cy, \
359 iP->iconSpacing.cx, iP->iconSpacing.cy, iP->notifyFormat); \
360 TRACE("hwndSelf=%p, rcList=%s\n", iP->hwndSelf, debugrect(&iP->rcList)); \
361 } while(0)
365 * forward declarations
367 static BOOL LISTVIEW_GetItemT(LISTVIEW_INFO *, LPLVITEMW, BOOL);
368 static void LISTVIEW_GetItemBox(LISTVIEW_INFO *, INT, LPRECT);
369 static void LISTVIEW_GetItemOrigin(LISTVIEW_INFO *, INT, LPPOINT);
370 static BOOL LISTVIEW_GetItemPosition(LISTVIEW_INFO *, INT, LPPOINT);
371 static BOOL LISTVIEW_GetItemRect(LISTVIEW_INFO *, INT, LPRECT);
372 static INT LISTVIEW_GetLabelWidth(LISTVIEW_INFO *, INT);
373 static void LISTVIEW_GetOrigin(LISTVIEW_INFO *, LPPOINT);
374 static BOOL LISTVIEW_GetViewRect(LISTVIEW_INFO *, LPRECT);
375 static void LISTVIEW_SetGroupSelection(LISTVIEW_INFO *, INT);
376 static BOOL LISTVIEW_SetItemT(LISTVIEW_INFO *, LPLVITEMW, BOOL);
377 static void LISTVIEW_UpdateScroll(LISTVIEW_INFO *);
378 static void LISTVIEW_SetSelection(LISTVIEW_INFO *, INT);
379 static BOOL LISTVIEW_UpdateSize(LISTVIEW_INFO *);
380 static HWND LISTVIEW_EditLabelT(LISTVIEW_INFO *, INT, BOOL);
381 static LRESULT LISTVIEW_Command(LISTVIEW_INFO *, WPARAM, LPARAM);
382 static BOOL LISTVIEW_SortItems(LISTVIEW_INFO *, PFNLVCOMPARE, LPARAM);
383 static INT LISTVIEW_GetStringWidthT(LISTVIEW_INFO *, LPCWSTR, BOOL);
384 static BOOL LISTVIEW_KeySelection(LISTVIEW_INFO *, INT);
385 static UINT LISTVIEW_GetItemState(LISTVIEW_INFO *, INT, UINT);
386 static BOOL LISTVIEW_SetItemState(LISTVIEW_INFO *, INT, LPLVITEMW);
387 static LRESULT LISTVIEW_VScroll(LISTVIEW_INFO *, INT, INT, HWND);
388 static LRESULT LISTVIEW_HScroll(LISTVIEW_INFO *, INT, INT, HWND);
389 static INT LISTVIEW_GetTopIndex(LISTVIEW_INFO *);
390 static BOOL LISTVIEW_EnsureVisible(LISTVIEW_INFO *, INT, BOOL);
391 static HWND CreateEditLabelT(LISTVIEW_INFO *, LPCWSTR, DWORD, INT, INT, INT, INT, BOOL);
393 /******** Text handling functions *************************************/
395 /* A text pointer is either NULL, LPSTR_TEXTCALLBACK, or points to a
396 * text string. The string may be ANSI or Unicode, in which case
397 * the boolean isW tells us the type of the string.
399 * The name of the function tell what type of strings it expects:
400 * W: Unicode, T: ANSI/Unicode - function of isW
403 static inline BOOL is_textW(LPCWSTR text)
405 return text != NULL && text != LPSTR_TEXTCALLBACKW;
408 static inline BOOL is_textT(LPCWSTR text, BOOL isW)
410 /* we can ignore isW since LPSTR_TEXTCALLBACKW == LPSTR_TEXTCALLBACKA */
411 return is_textW(text);
414 static inline int textlenT(LPCWSTR text, BOOL isW)
416 return !is_textT(text, isW) ? 0 :
417 isW ? lstrlenW(text) : lstrlenA((LPCSTR)text);
420 static inline void textcpynT(LPWSTR dest, BOOL isDestW, LPCWSTR src, BOOL isSrcW, INT max)
422 if (isDestW)
423 if (isSrcW) lstrcpynW(dest, src, max);
424 else MultiByteToWideChar(CP_ACP, 0, (LPCSTR)src, -1, dest, max);
425 else
426 if (isSrcW) WideCharToMultiByte(CP_ACP, 0, src, -1, (LPSTR)dest, max, NULL, NULL);
427 else lstrcpynA((LPSTR)dest, (LPCSTR)src, max);
430 static inline LPWSTR textdupTtoW(LPCWSTR text, BOOL isW)
432 LPWSTR wstr = (LPWSTR)text;
434 if (!isW && is_textT(text, isW))
436 INT len = MultiByteToWideChar(CP_ACP, 0, (LPCSTR)text, -1, NULL, 0);
437 wstr = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
438 if (wstr) MultiByteToWideChar(CP_ACP, 0, (LPCSTR)text, -1, wstr, len);
440 TRACE(" wstr=%s\n", text == LPSTR_TEXTCALLBACKW ? "(callback)" : debugstr_w(wstr));
441 return wstr;
444 static inline void textfreeT(LPWSTR wstr, BOOL isW)
446 if (!isW && is_textT(wstr, isW)) HeapFree(GetProcessHeap(), 0, wstr);
450 * dest is a pointer to a Unicode string
451 * src is a pointer to a string (Unicode if isW, ANSI if !isW)
453 static BOOL textsetptrT(LPWSTR *dest, LPWSTR src, BOOL isW)
455 BOOL bResult = TRUE;
457 if (src == LPSTR_TEXTCALLBACKW)
459 if (is_textW(*dest)) COMCTL32_Free(*dest);
460 *dest = LPSTR_TEXTCALLBACKW;
462 else
464 LPWSTR pszText = textdupTtoW(src, isW);
465 if (*dest == LPSTR_TEXTCALLBACKW) *dest = NULL;
466 bResult = Str_SetPtrW(dest, pszText);
467 textfreeT(pszText, isW);
469 return bResult;
473 * compares a Unicode to a Unicode/ANSI text string
475 static inline int textcmpWT(LPWSTR aw, LPWSTR bt, BOOL isW)
477 if (!aw) return bt ? -1 : 0;
478 if (!bt) return aw ? 1 : 0;
479 if (aw == LPSTR_TEXTCALLBACKW)
480 return bt == LPSTR_TEXTCALLBACKW ? 0 : -1;
481 if (bt != LPSTR_TEXTCALLBACKW)
483 LPWSTR bw = textdupTtoW(bt, isW);
484 int r = bw ? lstrcmpW(aw, bw) : 1;
485 textfreeT(bw, isW);
486 return r;
489 return 1;
492 static inline int lstrncmpiW(LPCWSTR s1, LPCWSTR s2, int n)
494 int res;
496 n = min(min(n, strlenW(s1)), strlenW(s2));
497 res = CompareStringW(LOCALE_USER_DEFAULT, NORM_IGNORECASE, s1, n, s2, n);
498 return res ? res - sizeof(WCHAR) : res;
501 /******** Debugging functions *****************************************/
503 static inline LPCSTR debugtext_t(LPCWSTR text, BOOL isW)
505 if (text == LPSTR_TEXTCALLBACKW) return "(callback)";
506 return isW ? debugstr_w(text) : debugstr_a((LPCSTR)text);
509 static inline LPCSTR debugtext_tn(LPCWSTR text, BOOL isW, INT n)
511 if (text == LPSTR_TEXTCALLBACKW) return "(callback)";
512 n = min(textlenT(text, isW), n);
513 return isW ? debugstr_wn(text, n) : debugstr_an((LPCSTR)text, n);
516 static char* debug_getbuf()
518 static int index = 0;
519 static char buffers[DEBUG_BUFFERS][DEBUG_BUFFER_SIZE];
520 return buffers[index++ % DEBUG_BUFFERS];
523 static inline char* debugrange(const RANGE* lprng)
525 if (lprng)
527 char* buf = debug_getbuf();
528 snprintf(buf, DEBUG_BUFFER_SIZE, "[%d, %d)", lprng->lower, lprng->upper);
529 return buf;
530 } else return "(null)";
533 static inline char* debugpoint(const POINT* lppt)
535 if (lppt)
537 char* buf = debug_getbuf();
538 snprintf(buf, DEBUG_BUFFER_SIZE, "(%ld, %ld)", lppt->x, lppt->y);
539 return buf;
540 } else return "(null)";
543 static inline char* debugrect(const RECT* rect)
545 if (rect)
547 char* buf = debug_getbuf();
548 snprintf(buf, DEBUG_BUFFER_SIZE, "[(%d, %d);(%d, %d)]",
549 rect->left, rect->top, rect->right, rect->bottom);
550 return buf;
551 } else return "(null)";
554 static char * debugscrollinfo(const SCROLLINFO *pScrollInfo)
556 char* buf = debug_getbuf(), *text = buf;
557 int len, size = DEBUG_BUFFER_SIZE;
559 if (pScrollInfo == NULL) return "(null)";
560 len = snprintf(buf, size, "{cbSize=%d, ", pScrollInfo->cbSize);
561 if (len == -1) goto end; buf += len; size -= len;
562 if (pScrollInfo->fMask & SIF_RANGE)
563 len = snprintf(buf, size, "nMin=%d, nMax=%d, ", pScrollInfo->nMin, pScrollInfo->nMax);
564 else len = 0;
565 if (len == -1) goto end; buf += len; size -= len;
566 if (pScrollInfo->fMask & SIF_PAGE)
567 len = snprintf(buf, size, "nPage=%u, ", pScrollInfo->nPage);
568 else len = 0;
569 if (len == -1) goto end; buf += len; size -= len;
570 if (pScrollInfo->fMask & SIF_POS)
571 len = snprintf(buf, size, "nPos=%d, ", pScrollInfo->nPos);
572 else len = 0;
573 if (len == -1) goto end; buf += len; size -= len;
574 if (pScrollInfo->fMask & SIF_TRACKPOS)
575 len = snprintf(buf, size, "nTrackPos=%d, ", pScrollInfo->nTrackPos);
576 else len = 0;
577 if (len == -1) goto end; buf += len; size -= len;
578 goto undo;
579 end:
580 buf = text + strlen(text);
581 undo:
582 if (buf - text > 2) { buf[-2] = '}'; buf[-1] = 0; }
583 return text;
586 static char* debugnmlistview(LPNMLISTVIEW plvnm)
588 if (plvnm)
590 char* buf = debug_getbuf();
591 snprintf(buf, DEBUG_BUFFER_SIZE, "iItem=%d, iSubItem=%d, uNewState=0x%x,"
592 " uOldState=0x%x, uChanged=0x%x, ptAction=%s, lParam=%ld\n",
593 plvnm->iItem, plvnm->iSubItem, plvnm->uNewState, plvnm->uOldState,
594 plvnm->uChanged, debugpoint(&plvnm->ptAction), plvnm->lParam);
595 return buf;
596 } else return "(null)";
599 static char* debuglvitem_t(LPLVITEMW lpLVItem, BOOL isW)
601 char* buf = debug_getbuf(), *text = buf;
602 int len, size = DEBUG_BUFFER_SIZE;
604 if (lpLVItem == NULL) return "(null)";
605 len = snprintf(buf, size, "{iItem=%d, iSubItem=%d, ", lpLVItem->iItem, lpLVItem->iSubItem);
606 if (len == -1) goto end; buf += len; size -= len;
607 if (lpLVItem->mask & LVIF_STATE)
608 len = snprintf(buf, size, "state=%x, stateMask=%x, ", lpLVItem->state, lpLVItem->stateMask);
609 else len = 0;
610 if (len == -1) goto end; buf += len; size -= len;
611 if (lpLVItem->mask & LVIF_TEXT)
612 len = snprintf(buf, size, "pszText=%s, cchTextMax=%d, ", debugtext_tn(lpLVItem->pszText, isW, 80), lpLVItem->cchTextMax);
613 else len = 0;
614 if (len == -1) goto end; buf += len; size -= len;
615 if (lpLVItem->mask & LVIF_IMAGE)
616 len = snprintf(buf, size, "iImage=%d, ", lpLVItem->iImage);
617 else len = 0;
618 if (len == -1) goto end; buf += len; size -= len;
619 if (lpLVItem->mask & LVIF_PARAM)
620 len = snprintf(buf, size, "lParam=%lx, ", lpLVItem->lParam);
621 else len = 0;
622 if (len == -1) goto end; buf += len; size -= len;
623 if (lpLVItem->mask & LVIF_INDENT)
624 len = snprintf(buf, size, "iIndent=%d, ", lpLVItem->iIndent);
625 else len = 0;
626 if (len == -1) goto end; buf += len; size -= len;
627 goto undo;
628 end:
629 buf = text + strlen(text);
630 undo:
631 if (buf - text > 2) { buf[-2] = '}'; buf[-1] = 0; }
632 return text;
635 static char* debuglvcolumn_t(LPLVCOLUMNW lpColumn, BOOL isW)
637 char* buf = debug_getbuf(), *text = buf;
638 int len, size = DEBUG_BUFFER_SIZE;
640 if (lpColumn == NULL) return "(null)";
641 len = snprintf(buf, size, "{");
642 if (len == -1) goto end; buf += len; size -= len;
643 if (lpColumn->mask & LVCF_SUBITEM)
644 len = snprintf(buf, size, "iSubItem=%d, ", lpColumn->iSubItem);
645 else len = 0;
646 if (len == -1) goto end; buf += len; size -= len;
647 if (lpColumn->mask & LVCF_FMT)
648 len = snprintf(buf, size, "fmt=%x, ", lpColumn->fmt);
649 else len = 0;
650 if (len == -1) goto end; buf += len; size -= len;
651 if (lpColumn->mask & LVCF_WIDTH)
652 len = snprintf(buf, size, "cx=%d, ", lpColumn->cx);
653 else len = 0;
654 if (len == -1) goto end; buf += len; size -= len;
655 if (lpColumn->mask & LVCF_TEXT)
656 len = snprintf(buf, size, "pszText=%s, cchTextMax=%d, ", debugtext_tn(lpColumn->pszText, isW, 80), lpColumn->cchTextMax);
657 else len = 0;
658 if (len == -1) goto end; buf += len; size -= len;
659 if (lpColumn->mask & LVCF_IMAGE)
660 len = snprintf(buf, size, "iImage=%d, ", lpColumn->iImage);
661 else len = 0;
662 if (len == -1) goto end; buf += len; size -= len;
663 if (lpColumn->mask & LVCF_ORDER)
664 len = snprintf(buf, size, "iOrder=%d, ", lpColumn->iOrder);
665 else len = 0;
666 if (len == -1) goto end; buf += len; size -= len;
667 goto undo;
668 end:
669 buf = text + strlen(text);
670 undo:
671 if (buf - text > 2) { buf[-2] = '}'; buf[-1] = 0; }
672 return text;
676 /******** Notification functions i************************************/
678 static LRESULT notify_hdr(LISTVIEW_INFO *infoPtr, INT code, LPNMHDR pnmh)
680 LRESULT result;
682 TRACE("(code=%d)\n", code);
684 pnmh->hwndFrom = infoPtr->hwndSelf;
685 pnmh->idFrom = GetWindowLongW(infoPtr->hwndSelf, GWL_ID);
686 pnmh->code = code;
687 result = SendMessageW(GetParent(infoPtr->hwndSelf), WM_NOTIFY,
688 (WPARAM)pnmh->idFrom, (LPARAM)pnmh);
690 TRACE(" <= %ld\n", result);
692 return result;
695 static inline LRESULT notify(LISTVIEW_INFO *infoPtr, INT code)
697 NMHDR nmh;
698 return notify_hdr(infoPtr, code, &nmh);
701 static inline void notify_itemactivate(LISTVIEW_INFO *infoPtr)
703 notify(infoPtr, LVN_ITEMACTIVATE);
706 static inline LRESULT notify_listview(LISTVIEW_INFO *infoPtr, INT code, LPNMLISTVIEW plvnm)
708 TRACE("(code=%d, plvnm=%s)\n", code, debugnmlistview(plvnm));
709 return notify_hdr(infoPtr, code, (LPNMHDR)plvnm);
712 static LRESULT notify_click(LISTVIEW_INFO *infoPtr, INT code, LVHITTESTINFO *lvht)
714 NMLISTVIEW nmlv;
716 ZeroMemory(&nmlv, sizeof(nmlv));
717 nmlv.iItem = lvht->iItem;
718 nmlv.iSubItem = lvht->iSubItem;
719 nmlv.ptAction = lvht->pt;
720 return notify_listview(infoPtr, code, &nmlv);
723 static int get_ansi_notification(INT unicodeNotificationCode)
725 switch (unicodeNotificationCode)
727 case LVN_BEGINLABELEDITW: return LVN_BEGINLABELEDITA;
728 case LVN_ENDLABELEDITW: return LVN_ENDLABELEDITA;
729 case LVN_GETDISPINFOW: return LVN_GETDISPINFOA;
730 case LVN_SETDISPINFOW: return LVN_SETDISPINFOA;
731 case LVN_ODFINDITEMW: return LVN_ODFINDITEMA;
732 case LVN_GETINFOTIPW: return LVN_GETINFOTIPA;
734 ERR("unknown notification %x\n", unicodeNotificationCode);
735 assert(FALSE);
739 Send notification. depends on dispinfoW having same
740 structure as dispinfoA.
741 infoPtr : listview struct
742 notificationCode : *Unicode* notification code
743 pdi : dispinfo structure (can be unicode or ansi)
744 isW : TRUE if dispinfo is Unicode
746 static BOOL notify_dispinfoT(LISTVIEW_INFO *infoPtr, INT notificationCode, LPNMLVDISPINFOW pdi, BOOL isW)
748 BOOL bResult = FALSE;
749 BOOL convertToAnsi = FALSE, convertToUnicode = FALSE;
750 INT realNotifCode;
751 INT cchTempBufMax = 0, savCchTextMax = 0;
752 LPWSTR pszTempBuf = NULL, savPszText = NULL;
754 if ((pdi->item.mask & LVIF_TEXT) && is_textT(pdi->item.pszText, isW))
756 convertToAnsi = (isW && infoPtr->notifyFormat == NFR_ANSI);
757 convertToUnicode = (!isW && infoPtr->notifyFormat == NFR_UNICODE);
760 if (convertToAnsi || convertToUnicode)
762 if (notificationCode != LVN_GETDISPINFOW)
764 cchTempBufMax = convertToUnicode ?
765 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pdi->item.pszText, -1, NULL, 0):
766 WideCharToMultiByte(CP_ACP, 0, pdi->item.pszText, -1, NULL, 0, NULL, NULL);
768 else
770 cchTempBufMax = pdi->item.cchTextMax;
771 *pdi->item.pszText = 0; /* make sure we don't process garbage */
774 pszTempBuf = HeapAlloc(GetProcessHeap(), 0,
775 (convertToUnicode ? sizeof(WCHAR) : sizeof(CHAR)) * cchTempBufMax);
776 if (!pszTempBuf) return FALSE;
777 if (convertToUnicode)
778 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pdi->item.pszText, -1,
779 pszTempBuf, cchTempBufMax);
780 else
781 WideCharToMultiByte(CP_ACP, 0, pdi->item.pszText, -1, (LPSTR) pszTempBuf,
782 cchTempBufMax, NULL, NULL);
783 savCchTextMax = pdi->item.cchTextMax;
784 savPszText = pdi->item.pszText;
785 pdi->item.pszText = pszTempBuf;
786 pdi->item.cchTextMax = cchTempBufMax;
789 if (infoPtr->notifyFormat == NFR_ANSI)
790 realNotifCode = get_ansi_notification(notificationCode);
791 else
792 realNotifCode = notificationCode;
793 TRACE(" pdi->item=%s\n", debuglvitem_t(&pdi->item, infoPtr->notifyFormat != NFR_ANSI));
794 bResult = notify_hdr(infoPtr, realNotifCode, (LPNMHDR)pdi);
796 if (convertToUnicode || convertToAnsi)
798 if (convertToUnicode) /* note : pointer can be changed by app ! */
799 WideCharToMultiByte(CP_ACP, 0, pdi->item.pszText, -1, (LPSTR) savPszText,
800 savCchTextMax, NULL, NULL);
801 else
802 MultiByteToWideChar(CP_ACP, 0, (LPSTR) pdi->item.pszText, -1,
803 savPszText, savCchTextMax);
804 pdi->item.pszText = savPszText; /* restores our buffer */
805 pdi->item.cchTextMax = savCchTextMax;
806 HeapFree(GetProcessHeap(), 0, pszTempBuf);
808 return bResult;
811 static void customdraw_fill(NMLVCUSTOMDRAW *lpnmlvcd, LISTVIEW_INFO *infoPtr, HDC hdc, LPRECT rcBounds)
813 ZeroMemory(lpnmlvcd, sizeof(NMLVCUSTOMDRAW));
814 lpnmlvcd->nmcd.hdc = hdc;
815 lpnmlvcd->nmcd.rc = *rcBounds;
816 lpnmlvcd->clrTextBk = infoPtr->clrTextBk;
817 lpnmlvcd->clrText = infoPtr->clrText;
820 static inline DWORD notify_customdraw (LISTVIEW_INFO *infoPtr, DWORD dwDrawStage, NMLVCUSTOMDRAW *lpnmlvcd)
822 lpnmlvcd->nmcd.dwDrawStage = dwDrawStage;
823 return notify_hdr(infoPtr, NM_CUSTOMDRAW, &lpnmlvcd->nmcd.hdr);
826 /******** Item iterator functions **********************************/
828 static RANGES ranges_create(int count);
829 static void ranges_destroy(RANGES ranges);
830 static BOOL ranges_add(RANGES ranges, RANGE range);
831 static BOOL ranges_del(RANGES ranges, RANGE range);
832 static void ranges_dump(RANGES ranges);
834 static inline BOOL ranges_additem(RANGES ranges, INT nItem)
836 RANGE range = { nItem, nItem + 1 };
838 return ranges_add(ranges, range);
841 static inline BOOL ranges_delitem(RANGES ranges, INT nItem)
843 RANGE range = { nItem, nItem + 1 };
845 return ranges_del(ranges, range);
848 /***
849 * ITERATOR DOCUMENTATION
851 * The iterator functions allow for easy, and convenient iteration
852 * over items of iterest in the list. Typically, you create a
853 * iterator, use it, and destroy it, as such:
854 * ITERATOR i;
856 * iterator_xxxitems(&i, ...);
857 * while (iterator_{prev,next}(&i)
859 * //code which uses i.nItem
861 * iterator_destroy(&i);
863 * where xxx is either: framed, or visible.
864 * Note that it is important that the code destroys the iterator
865 * after it's done with it, as the creation of the iterator may
866 * allocate memory, which thus needs to be freed.
868 * You can iterate both forwards, and backwards through the list,
869 * by using iterator_next or iterator_prev respectively.
871 * Lower numbered items are draw on top of higher number items in
872 * LVS_ICON, and LVS_SMALLICON (which are the only modes where
873 * items may overlap). So, to test items, you should use
874 * iterator_next
875 * which lists the items top to bottom (in Z-order).
876 * For drawing items, you should use
877 * iterator_prev
878 * which lists the items bottom to top (in Z-order).
879 * If you keep iterating over the items after the end-of-items
880 * marker (-1) is returned, the iterator will start from the
881 * beginning. Typically, you don't need to test for -1,
882 * because iterator_{next,prev} will return TRUE if more items
883 * are to be iterated over, or FALSE otherwise.
885 * Note: the iterator is defined to be bidirectional. That is,
886 * any number of prev followed by any number of next, or
887 * five versa, should leave the iterator at the same item:
888 * prev * n, next * n = next * n, prev * n
890 * The iterator has a notion of a out-of-order, special item,
891 * which sits at the start of the list. This is used in
892 * LVS_ICON, and LVS_SMALLICON mode to handle the focused item,
893 * which needs to be first, as it may overlap other items.
895 * The code is a bit messy because we have:
896 * - a special item to deal with
897 * - simple range, or composite range
898 * - empty range.
899 * If find bugs, or want to add features, please make sure you
900 * always check/modify *both* iterator_prev, and iterator_next.
903 /****
904 * This function iterates through the items in increasing order,
905 * but prefixed by the special item, then -1. That is:
906 * special, 1, 2, 3, ..., n, -1.
907 * Each item is listed only once.
909 static inline BOOL iterator_next(ITERATOR* i)
911 if (i->nItem == -1)
913 i->nItem = i->nSpecial;
914 if (i->nItem != -1) return TRUE;
916 if (i->nItem == i->nSpecial)
918 if (i->ranges) i->index = 0;
919 goto pickarange;
922 i->nItem++;
923 testitem:
924 if (i->nItem == i->nSpecial) i->nItem++;
925 if (i->nItem < i->range.upper) return TRUE;
927 pickarange:
928 if (i->ranges)
930 if (i->index < i->ranges->hdpa->nItemCount)
931 i->range = *(RANGE*)DPA_GetPtr(i->ranges->hdpa, i->index++);
932 else goto end;
934 else if (i->nItem >= i->range.upper) goto end;
936 i->nItem = i->range.lower;
937 if (i->nItem >= 0) goto testitem;
938 end:
939 i->nItem = -1;
940 return FALSE;
943 /****
944 * This function iterates through the items in decreasing order,
945 * followed by the special item, then -1. That is:
946 * n, n-1, ..., 3, 2, 1, special, -1.
947 * Each item is listed only once.
949 static inline BOOL iterator_prev(ITERATOR* i)
951 BOOL start = FALSE;
953 if (i->nItem == -1)
955 start = TRUE;
956 if (i->ranges) i->index = i->ranges->hdpa->nItemCount;
957 goto pickarange;
959 if (i->nItem == i->nSpecial)
961 i->nItem = -1;
962 return FALSE;
965 testitem:
966 i->nItem--;
967 if (i->nItem == i->nSpecial) i->nItem--;
968 if (i->nItem >= i->range.lower) return TRUE;
970 pickarange:
971 if (i->ranges)
973 if (i->index > 0)
974 i->range = *(RANGE*)DPA_GetPtr(i->ranges->hdpa, --i->index);
975 else goto end;
977 else if (!start && i->nItem < i->range.lower) goto end;
979 i->nItem = i->range.upper;
980 if (i->nItem > 0) goto testitem;
981 end:
982 return (i->nItem = i->nSpecial) != -1;
985 static RANGE iterator_range(ITERATOR* i)
987 RANGE range;
989 if (!i->ranges) return i->range;
991 range.lower = (*(RANGE*)DPA_GetPtr(i->ranges->hdpa, 0)).lower;
992 range.upper = (*(RANGE*)DPA_GetPtr(i->ranges->hdpa, i->ranges->hdpa->nItemCount - 1)).upper;
993 return range;
996 /***
997 * Releases resources associated with this ierator.
999 static inline void iterator_destroy(ITERATOR* i)
1001 if (i->ranges) ranges_destroy(i->ranges);
1004 /***
1005 * Create an empty iterator.
1007 static inline BOOL iterator_empty(ITERATOR* i)
1009 ZeroMemory(i, sizeof(*i));
1010 i->nItem = i->nSpecial = i->range.lower = i->range.upper = -1;
1011 return TRUE;
1014 /***
1015 * Create an iterator over a range.
1017 static inline BOOL iterator_rangeitems(ITERATOR* i, RANGE range)
1019 iterator_empty(i);
1020 i->range = range;
1021 return TRUE;
1024 /***
1025 * Create an iterator over a bunch of ranges.
1026 * Please note that the iterator will take ownership of the ranges,
1027 * and will free them upon destruction.
1029 static inline BOOL iterator_rangesitems(ITERATOR* i, RANGES ranges)
1031 iterator_empty(i);
1032 i->ranges = ranges;
1033 return TRUE;
1036 /***
1037 * Creates an iterator over the items which intersect lprc.
1039 static BOOL iterator_frameditems(ITERATOR* i, LISTVIEW_INFO* infoPtr, const RECT* lprc)
1041 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
1042 RECT frame = *lprc, rcItem, rcTemp;
1043 POINT Origin;
1045 /* in case we fail, we want to return an empty iterator */
1046 if (!iterator_empty(i)) return FALSE;
1048 LISTVIEW_GetOrigin(infoPtr, &Origin);
1050 TRACE("(lprc=%s)\n", debugrect(lprc));
1051 OffsetRect(&frame, -Origin.x, -Origin.y);
1053 if (uView == LVS_ICON || uView == LVS_SMALLICON)
1055 INT nItem;
1057 if (uView == LVS_ICON && infoPtr->nFocusedItem != -1)
1059 LISTVIEW_GetItemBox(infoPtr, infoPtr->nFocusedItem, &rcItem);
1060 if (IntersectRect(&rcTemp, &rcItem, lprc))
1061 i->nSpecial = infoPtr->nFocusedItem;
1063 if (!(iterator_rangesitems(i, ranges_create(50)))) return FALSE;
1064 /* to do better here, we need to have PosX, and PosY sorted */
1065 TRACE("building icon ranges:\n");
1066 for (nItem = 0; nItem < infoPtr->nItemCount; nItem++)
1068 rcItem.left = (LONG)DPA_GetPtr(infoPtr->hdpaPosX, nItem);
1069 rcItem.top = (LONG)DPA_GetPtr(infoPtr->hdpaPosY, nItem);
1070 rcItem.right = rcItem.left + infoPtr->nItemWidth;
1071 rcItem.bottom = rcItem.top + infoPtr->nItemHeight;
1072 if (IntersectRect(&rcTemp, &rcItem, &frame))
1073 ranges_additem(i->ranges, nItem);
1075 return TRUE;
1077 else if (uView == LVS_REPORT)
1079 RANGE range;
1081 if (frame.left >= infoPtr->nItemWidth) return TRUE;
1082 if (frame.top >= infoPtr->nItemHeight * infoPtr->nItemCount) return TRUE;
1084 range.lower = max(frame.top / infoPtr->nItemHeight, 0);
1085 range.upper = min((frame.bottom - 1) / infoPtr->nItemHeight, infoPtr->nItemCount - 1) + 1;
1086 if (range.upper <= range.lower) return TRUE;
1087 if (!iterator_rangeitems(i, range)) return FALSE;
1088 TRACE(" report=%s\n", debugrange(&i->range));
1090 else
1092 INT nPerCol = max((infoPtr->rcList.bottom - infoPtr->rcList.top) / infoPtr->nItemHeight, 1);
1093 INT nFirstRow = max(frame.top / infoPtr->nItemHeight, 0);
1094 INT nLastRow = min((frame.bottom - 1) / infoPtr->nItemHeight, nPerCol - 1);
1095 INT nFirstCol = max(frame.left / infoPtr->nItemWidth, 0);
1096 INT nLastCol = min((frame.right - 1) / infoPtr->nItemWidth, (infoPtr->nItemCount + nPerCol - 1) / nPerCol);
1097 INT lower = nFirstCol * nPerCol + nFirstRow;
1098 RANGE item_range;
1099 INT nCol;
1101 TRACE("nPerCol=%d, nFirstRow=%d, nLastRow=%d, nFirstCol=%d, nLastCol=%d, lower=%d\n",
1102 nPerCol, nFirstRow, nLastRow, nFirstCol, nLastCol, lower);
1104 if (nLastCol < nFirstCol || nLastRow < nFirstRow) return TRUE;
1106 if (!(iterator_rangesitems(i, ranges_create(nLastCol - nFirstCol + 1)))) return FALSE;
1107 TRACE("building list ranges:\n");
1108 for (nCol = nFirstCol; nCol <= nLastCol; nCol++)
1110 item_range.lower = nCol * nPerCol + nFirstRow;
1111 if(item_range.lower >= infoPtr->nItemCount) break;
1112 item_range.upper = min(nCol * nPerCol + nLastRow + 1, infoPtr->nItemCount);
1113 TRACE(" list=%s\n", debugrange(&item_range));
1114 ranges_add(i->ranges, item_range);
1118 return TRUE;
1121 /***
1122 * Creates an iterator over the items which intersect the visible region of hdc.
1124 static BOOL iterator_visibleitems(ITERATOR* i, LISTVIEW_INFO *infoPtr, HDC hdc)
1126 POINT Origin, Position;
1127 RECT rcItem, rcClip;
1128 INT rgntype;
1130 rgntype = GetClipBox(hdc, &rcClip);
1131 if (rgntype == NULLREGION) return iterator_empty(i);
1132 if (!iterator_frameditems(i, infoPtr, &rcClip)) return FALSE;
1133 if (rgntype == SIMPLEREGION) return TRUE;
1135 /* first deal with the special item */
1136 if (i->nSpecial != -1)
1138 LISTVIEW_GetItemBox(infoPtr, i->nSpecial, &rcItem);
1139 if (!RectVisible(hdc, &rcItem)) i->nSpecial = -1;
1142 /* if we can't deal with the region, we'll just go with the simple range */
1143 LISTVIEW_GetOrigin(infoPtr, &Origin);
1144 TRACE("building visible range:\n");
1145 if (!i->ranges && i->range.lower < i->range.upper)
1147 if (!(i->ranges = ranges_create(50))) return TRUE;
1148 if (!ranges_add(i->ranges, i->range))
1150 ranges_destroy(i->ranges);
1151 i->ranges = 0;
1152 return TRUE;
1156 /* now delete the invisible items from the list */
1157 while(iterator_next(i))
1159 LISTVIEW_GetItemOrigin(infoPtr, i->nItem, &Position);
1160 rcItem.left = Position.x + Origin.x;
1161 rcItem.top = Position.y + Origin.y;
1162 rcItem.right = rcItem.left + infoPtr->nItemWidth;
1163 rcItem.bottom = rcItem.top + infoPtr->nItemHeight;
1164 if (!RectVisible(hdc, &rcItem))
1165 ranges_delitem(i->ranges, i->nItem);
1167 /* the iterator should restart on the next iterator_next */
1168 TRACE("done\n");
1170 return TRUE;
1173 /******** Misc helper functions ************************************/
1175 static inline LRESULT CallWindowProcT(WNDPROC proc, HWND hwnd, UINT uMsg,
1176 WPARAM wParam, LPARAM lParam, BOOL isW)
1178 if (isW) return CallWindowProcW(proc, hwnd, uMsg, wParam, lParam);
1179 else return CallWindowProcA(proc, hwnd, uMsg, wParam, lParam);
1182 static inline BOOL is_autoarrange(LISTVIEW_INFO *infoPtr)
1184 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
1186 return ((infoPtr->dwStyle & LVS_AUTOARRANGE) || infoPtr->bAutoarrange) &&
1187 (uView == LVS_ICON || uView == LVS_SMALLICON);
1190 /******** Internal API functions ************************************/
1192 static inline COLUMN_INFO * LISTVIEW_GetColumnInfo(LISTVIEW_INFO *infoPtr, INT nSubItem)
1194 assert (nSubItem >= 0 && nSubItem < infoPtr->hdpaColumns->nItemCount);
1195 return (COLUMN_INFO *)DPA_GetPtr(infoPtr->hdpaColumns, nSubItem);
1198 static inline void LISTVIEW_GetHeaderRect(LISTVIEW_INFO *infoPtr, INT nSubItem, RECT *lprc)
1200 *lprc = LISTVIEW_GetColumnInfo(infoPtr, nSubItem)->rcHeader;
1203 static inline BOOL LISTVIEW_GetItemW(LISTVIEW_INFO *infoPtr, LPLVITEMW lpLVItem)
1205 return LISTVIEW_GetItemT(infoPtr, lpLVItem, TRUE);
1208 /* Listview invlaidation functions: use _only_ these function to invalidate */
1210 static inline BOOL is_redrawing(LISTVIEW_INFO *infoPtr)
1212 return infoPtr->bRedraw && !infoPtr->bFirstPaint;
1215 static inline void LISTVIEW_InvalidateRect(LISTVIEW_INFO *infoPtr, const RECT*rect)
1217 if(!is_redrawing(infoPtr)) return;
1218 TRACE(" invalidating rect=%s\n", debugrect(rect));
1219 InvalidateRect(infoPtr->hwndSelf, rect, TRUE);
1222 static inline void LISTVIEW_InvalidateItem(LISTVIEW_INFO *infoPtr, INT nItem)
1224 RECT rcBox;
1226 if(!is_redrawing(infoPtr)) return;
1227 LISTVIEW_GetItemBox(infoPtr, nItem, &rcBox);
1228 LISTVIEW_InvalidateRect(infoPtr, &rcBox);
1231 static inline void LISTVIEW_InvalidateSubItem(LISTVIEW_INFO *infoPtr, INT nItem, INT nSubItem)
1233 POINT Origin, Position;
1234 RECT rcBox;
1236 if(!is_redrawing(infoPtr)) return;
1237 assert ((infoPtr->dwStyle & LVS_TYPEMASK) == LVS_REPORT);
1238 LISTVIEW_GetOrigin(infoPtr, &Origin);
1239 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position);
1240 LISTVIEW_GetHeaderRect(infoPtr, nSubItem, &rcBox);
1241 rcBox.top = 0;
1242 rcBox.bottom = infoPtr->nItemHeight;
1243 OffsetRect(&rcBox, Origin.x + Position.x, Origin.y + Position.y);
1244 LISTVIEW_InvalidateRect(infoPtr, &rcBox);
1247 static inline void LISTVIEW_InvalidateList(LISTVIEW_INFO *infoPtr)
1249 LISTVIEW_InvalidateRect(infoPtr, NULL);
1252 static inline void LISTVIEW_InvalidateColumn(LISTVIEW_INFO *infoPtr, INT nColumn)
1254 RECT rcCol;
1256 if(!is_redrawing(infoPtr)) return;
1257 LISTVIEW_GetHeaderRect(infoPtr, nColumn, &rcCol);
1258 rcCol.top = infoPtr->rcList.top;
1259 rcCol.bottom = infoPtr->rcList.bottom;
1260 LISTVIEW_InvalidateRect(infoPtr, &rcCol);
1263 /***
1264 * DESCRIPTION:
1265 * Retrieves the number of items that can fit vertically in the client area.
1267 * PARAMETER(S):
1268 * [I] infoPtr : valid pointer to the listview structure
1270 * RETURN:
1271 * Number of items per row.
1273 static inline INT LISTVIEW_GetCountPerRow(LISTVIEW_INFO *infoPtr)
1275 INT nListWidth = infoPtr->rcList.right - infoPtr->rcList.left;
1277 return max(nListWidth/infoPtr->nItemWidth, 1);
1280 /***
1281 * DESCRIPTION:
1282 * Retrieves the number of items that can fit horizontally in the client
1283 * area.
1285 * PARAMETER(S):
1286 * [I] infoPtr : valid pointer to the listview structure
1288 * RETURN:
1289 * Number of items per column.
1291 static inline INT LISTVIEW_GetCountPerColumn(LISTVIEW_INFO *infoPtr)
1293 INT nListHeight = infoPtr->rcList.bottom - infoPtr->rcList.top;
1295 return max(nListHeight / infoPtr->nItemHeight, 1);
1299 /*************************************************************************
1300 * LISTVIEW_ProcessLetterKeys
1302 * Processes keyboard messages generated by pressing the letter keys
1303 * on the keyboard.
1304 * What this does is perform a case insensitive search from the
1305 * current position with the following quirks:
1306 * - If two chars or more are pressed in quick succession we search
1307 * for the corresponding string (e.g. 'abc').
1308 * - If there is a delay we wipe away the current search string and
1309 * restart with just that char.
1310 * - If the user keeps pressing the same character, whether slowly or
1311 * fast, so that the search string is entirely composed of this
1312 * character ('aaaaa' for instance), then we search for first item
1313 * that starting with that character.
1314 * - If the user types the above character in quick succession, then
1315 * we must also search for the corresponding string ('aaaaa'), and
1316 * go to that string if there is a match.
1318 * PARAMETERS
1319 * [I] hwnd : handle to the window
1320 * [I] charCode : the character code, the actual character
1321 * [I] keyData : key data
1323 * RETURNS
1325 * Zero.
1327 * BUGS
1329 * - The current implementation has a list of characters it will
1330 * accept and it ignores averything else. In particular it will
1331 * ignore accentuated characters which seems to match what
1332 * Windows does. But I'm not sure it makes sense to follow
1333 * Windows there.
1334 * - We don't sound a beep when the search fails.
1336 * SEE ALSO
1338 * TREEVIEW_ProcessLetterKeys
1340 static INT LISTVIEW_ProcessLetterKeys(LISTVIEW_INFO *infoPtr, WPARAM charCode, LPARAM keyData)
1342 INT nItem;
1343 INT endidx,idx;
1344 LVITEMW item;
1345 WCHAR buffer[MAX_PATH];
1346 DWORD lastKeyPressTimestamp = infoPtr->lastKeyPressTimestamp;
1348 /* simple parameter checking */
1349 if (!charCode || !keyData) return 0;
1351 /* only allow the valid WM_CHARs through */
1352 if (!isalnum(charCode) &&
1353 charCode != '.' && charCode != '`' && charCode != '!' &&
1354 charCode != '@' && charCode != '#' && charCode != '$' &&
1355 charCode != '%' && charCode != '^' && charCode != '&' &&
1356 charCode != '*' && charCode != '(' && charCode != ')' &&
1357 charCode != '-' && charCode != '_' && charCode != '+' &&
1358 charCode != '=' && charCode != '\\'&& charCode != ']' &&
1359 charCode != '}' && charCode != '[' && charCode != '{' &&
1360 charCode != '/' && charCode != '?' && charCode != '>' &&
1361 charCode != '<' && charCode != ',' && charCode != '~')
1362 return 0;
1364 /* if there's one item or less, there is no where to go */
1365 if (infoPtr->nItemCount <= 1) return 0;
1367 /* update the search parameters */
1368 infoPtr->lastKeyPressTimestamp = GetTickCount();
1369 if (infoPtr->lastKeyPressTimestamp - lastKeyPressTimestamp < KEY_DELAY) {
1370 if (infoPtr->nSearchParamLength < MAX_PATH)
1371 infoPtr->szSearchParam[infoPtr->nSearchParamLength++]=charCode;
1372 if (infoPtr->charCode != charCode)
1373 infoPtr->charCode = charCode = 0;
1374 } else {
1375 infoPtr->charCode=charCode;
1376 infoPtr->szSearchParam[0]=charCode;
1377 infoPtr->nSearchParamLength=1;
1378 /* Redundant with the 1 char string */
1379 charCode=0;
1382 /* and search from the current position */
1383 nItem=-1;
1384 if (infoPtr->nFocusedItem >= 0) {
1385 endidx=infoPtr->nFocusedItem;
1386 idx=endidx;
1387 /* if looking for single character match,
1388 * then we must always move forward
1390 if (infoPtr->nSearchParamLength == 1)
1391 idx++;
1392 } else {
1393 endidx=infoPtr->nItemCount;
1394 idx=0;
1396 do {
1397 if (idx == infoPtr->nItemCount) {
1398 if (endidx == infoPtr->nItemCount || endidx == 0)
1399 break;
1400 idx=0;
1403 /* get item */
1404 item.mask = LVIF_TEXT;
1405 item.iItem = idx;
1406 item.iSubItem = 0;
1407 item.pszText = buffer;
1408 item.cchTextMax = MAX_PATH;
1409 if (!LISTVIEW_GetItemW(infoPtr, &item)) return 0;
1411 /* check for a match */
1412 if (lstrncmpiW(item.pszText,infoPtr->szSearchParam,infoPtr->nSearchParamLength) == 0) {
1413 nItem=idx;
1414 break;
1415 } else if ( (charCode != 0) && (nItem == -1) && (nItem != infoPtr->nFocusedItem) &&
1416 (lstrncmpiW(item.pszText,infoPtr->szSearchParam,1) == 0) ) {
1417 /* This would work but we must keep looking for a longer match */
1418 nItem=idx;
1420 idx++;
1421 } while (idx != endidx);
1423 if (nItem != -1)
1424 LISTVIEW_KeySelection(infoPtr, nItem);
1426 return 0;
1429 /*************************************************************************
1430 * LISTVIEW_UpdateHeaderSize [Internal]
1432 * Function to resize the header control
1434 * PARAMS
1435 * [I] hwnd : handle to a window
1436 * [I] nNewScrollPos : scroll pos to set
1438 * RETURNS
1439 * None.
1441 static void LISTVIEW_UpdateHeaderSize(LISTVIEW_INFO *infoPtr, INT nNewScrollPos)
1443 RECT winRect;
1444 POINT point[2];
1446 TRACE("nNewScrollPos=%d\n", nNewScrollPos);
1448 GetWindowRect(infoPtr->hwndHeader, &winRect);
1449 point[0].x = winRect.left;
1450 point[0].y = winRect.top;
1451 point[1].x = winRect.right;
1452 point[1].y = winRect.bottom;
1454 MapWindowPoints(HWND_DESKTOP, infoPtr->hwndSelf, point, 2);
1455 point[0].x = -nNewScrollPos;
1456 point[1].x += nNewScrollPos;
1458 SetWindowPos(infoPtr->hwndHeader,0,
1459 point[0].x,point[0].y,point[1].x,point[1].y,
1460 SWP_NOZORDER | SWP_NOACTIVATE);
1463 /***
1464 * DESCRIPTION:
1465 * Update the scrollbars. This functions should be called whenever
1466 * the content, size or view changes.
1468 * PARAMETER(S):
1469 * [I] infoPtr : valid pointer to the listview structure
1471 * RETURN:
1472 * None
1474 static void LISTVIEW_UpdateScroll(LISTVIEW_INFO *infoPtr)
1476 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
1477 SCROLLINFO horzInfo, vertInfo;
1479 if (infoPtr->dwStyle & LVS_NOSCROLL) return;
1480 if (!is_redrawing(infoPtr)) return;
1482 ZeroMemory(&horzInfo, sizeof(SCROLLINFO));
1483 horzInfo.cbSize = sizeof(SCROLLINFO);
1484 horzInfo.nPage = infoPtr->rcList.right - infoPtr->rcList.left;
1486 ZeroMemory(&vertInfo, sizeof(SCROLLINFO));
1487 vertInfo.cbSize = sizeof(SCROLLINFO);
1488 vertInfo.nPage = infoPtr->rcList.bottom - infoPtr->rcList.top;
1490 /* for now, we'll set info.nMax to the _count_, and adjust it later */
1491 if (uView == LVS_LIST)
1493 INT nPerCol = LISTVIEW_GetCountPerColumn(infoPtr);
1494 horzInfo.nMax = (infoPtr->nItemCount + nPerCol - 1) / nPerCol;
1495 horzInfo.nPage /= infoPtr->nItemWidth;
1497 else if (uView == LVS_REPORT)
1499 horzInfo.nMax = infoPtr->nItemWidth;
1500 vertInfo.nMax = infoPtr->nItemCount;
1501 vertInfo.nPage /= infoPtr->nItemHeight;
1503 else /* LVS_ICON, or LVS_SMALLICON */
1505 RECT rcView;
1507 if (LISTVIEW_GetViewRect(infoPtr, &rcView))
1509 horzInfo.nMax = rcView.right - rcView.left;
1510 vertInfo.nMax = rcView.bottom - rcView.top;
1514 horzInfo.fMask = SIF_RANGE | SIF_PAGE;
1515 horzInfo.nMax = max(horzInfo.nMax - 1, 0);
1516 SetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &horzInfo, TRUE);
1517 TRACE("horzInfo=%s\n", debugscrollinfo(&horzInfo));
1519 vertInfo.fMask = SIF_RANGE | SIF_PAGE;
1520 vertInfo.nMax = max(vertInfo.nMax - 1, 0);
1521 SetScrollInfo(infoPtr->hwndSelf, SB_VERT, &vertInfo, TRUE);
1522 TRACE("vertInfo=%s\n", debugscrollinfo(&vertInfo));
1524 /* Update the Header Control */
1525 if (uView == LVS_REPORT)
1527 horzInfo.fMask = SIF_POS;
1528 GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &horzInfo);
1529 LISTVIEW_UpdateHeaderSize(infoPtr, horzInfo.nPos);
1534 /***
1535 * DESCRIPTION:
1536 * Shows/hides the focus rectangle.
1538 * PARAMETER(S):
1539 * [I] infoPtr : valid pointer to the listview structure
1540 * [I] fShow : TRUE to show the focus, FALSE to hide it.
1542 * RETURN:
1543 * None
1545 static void LISTVIEW_ShowFocusRect(LISTVIEW_INFO *infoPtr, BOOL fShow)
1547 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
1548 HDC hdc;
1550 TRACE("fShow=%d, nItem=%d\n", fShow, infoPtr->nFocusedItem);
1552 if (infoPtr->nFocusedItem < 0) return;
1554 /* we need some gymnastics in ICON mode to handle large items */
1555 if ( (infoPtr->dwStyle & LVS_TYPEMASK) == LVS_ICON )
1557 RECT rcBox;
1559 LISTVIEW_GetItemBox(infoPtr, infoPtr->nFocusedItem, &rcBox);
1560 if ((rcBox.bottom - rcBox.top) > infoPtr->nItemHeight)
1562 LISTVIEW_InvalidateRect(infoPtr, &rcBox);
1563 return;
1567 if (!(hdc = GetDC(infoPtr->hwndSelf))) return;
1569 /* for some reason, owner draw should work only in report mode */
1570 if ((infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && (uView == LVS_REPORT))
1572 DRAWITEMSTRUCT dis;
1573 LVITEMW item;
1575 item.iItem = infoPtr->nFocusedItem;
1576 item.iSubItem = 0;
1577 item.mask = LVIF_PARAM;
1578 if (!LISTVIEW_GetItemW(infoPtr, &item)) goto done;
1580 ZeroMemory(&dis, sizeof(dis));
1581 dis.CtlType = ODT_LISTVIEW;
1582 dis.CtlID = GetWindowLongW(infoPtr->hwndSelf, GWL_ID);
1583 dis.itemID = item.iItem;
1584 dis.itemAction = ODA_FOCUS;
1585 if (fShow) dis.itemState |= ODS_FOCUS;
1586 dis.hwndItem = infoPtr->hwndSelf;
1587 dis.hDC = hdc;
1588 LISTVIEW_GetItemBox(infoPtr, dis.itemID, &dis.rcItem);
1589 dis.itemData = item.lParam;
1591 SendMessageW(GetParent(infoPtr->hwndSelf), WM_DRAWITEM, dis.CtlID, (LPARAM)&dis);
1593 else
1595 DrawFocusRect(hdc, &infoPtr->rcFocus);
1597 done:
1598 ReleaseDC(infoPtr->hwndSelf, hdc);
1601 /***
1602 * Invalidates all visible selected items.
1604 static void LISTVIEW_InvalidateSelectedItems(LISTVIEW_INFO *infoPtr)
1606 ITERATOR i;
1608 iterator_frameditems(&i, infoPtr, &infoPtr->rcList);
1609 while(iterator_next(&i))
1611 if (LISTVIEW_GetItemState(infoPtr, i.nItem, LVIS_SELECTED))
1612 LISTVIEW_InvalidateItem(infoPtr, i.nItem);
1614 iterator_destroy(&i);
1618 /***
1619 * DESCRIPTION: [INTERNAL]
1620 * Computes an item's (left,top) corner, relative to rcView.
1621 * That is, the position has NOT been made relative to the Origin.
1622 * This is deliberate, to avoid computing the Origin over, and
1623 * over again, when this function is call in a loop. Instead,
1624 * one ca factor the computation of the Origin before the loop,
1625 * and offset the value retured by this function, on every iteration.
1627 * PARAMETER(S):
1628 * [I] infoPtr : valid pointer to the listview structure
1629 * [I] nItem : item number
1630 * [O] lpptOrig : item top, left corner
1632 * RETURN:
1633 * None.
1635 static void LISTVIEW_GetItemOrigin(LISTVIEW_INFO *infoPtr, INT nItem, LPPOINT lpptPosition)
1637 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
1639 assert(nItem >= 0 && nItem < infoPtr->nItemCount);
1641 if ((uView == LVS_SMALLICON) || (uView == LVS_ICON))
1643 lpptPosition->x = (LONG)DPA_GetPtr(infoPtr->hdpaPosX, nItem);
1644 lpptPosition->y = (LONG)DPA_GetPtr(infoPtr->hdpaPosY, nItem);
1646 else if (uView == LVS_LIST)
1648 INT nCountPerColumn = LISTVIEW_GetCountPerColumn(infoPtr);
1649 lpptPosition->x = nItem / nCountPerColumn * infoPtr->nItemWidth;
1650 lpptPosition->y = nItem % nCountPerColumn * infoPtr->nItemHeight;
1652 else /* LVS_REPORT */
1654 lpptPosition->x = REPORT_MARGINX;
1655 lpptPosition->y = nItem * infoPtr->nItemHeight;
1659 /***
1660 * DESCRIPTION: [INTERNAL]
1661 * Compute the rectangles of an item. This is to localize all
1662 * the computations in one place. If you are not interested in some
1663 * of these values, simply pass in a NULL -- the fucntion is smart
1664 * enough to compute only what's necessary. The function computes
1665 * the standard rectangles (BOUNDS, ICON, LABEL) plus a non-standard
1666 * one, the BOX rectangle. This rectangle is very cheap to compute,
1667 * and is guaranteed to contain all the other rectangles. Computing
1668 * the ICON rect is also cheap, but all the others are potentaily
1669 * expensive. This gives an easy and effective optimization when
1670 * searching (like point inclusion, or rectangle intersection):
1671 * first test against the BOX, and if TRUE, test agains the desired
1672 * rectangle.
1673 * If the function does not have all the necessary information
1674 * to computed the requested rectangles, will crash with a
1675 * failed assertion. This is done so we catch all programming
1676 * errors, given that the function is called only from our code.
1678 * We have the following 'special' meanings for a few fields:
1679 * * If LVIS_FOCUSED is set, we assume the item has the focus
1680 * This is important in ICON mode, where it might get a larger
1681 * then usual rectange
1683 * Please note that subitem support works only in REPORT mode.
1685 * PARAMETER(S):
1686 * [I] infoPtr : valid pointer to the listview structure
1687 * [I] lpLVItem : item to compute the measures for
1688 * [O] lprcBox : ptr to Box rectangle
1689 * The internal LVIR_BOX rectangle
1690 * [0] lprcState : ptr to State icon rectangle
1691 * The internal LVIR_STATE rectangle
1692 * [O] lprcIcon : ptr to Icon rectangle
1693 * Same as LVM_GETITEMRECT with LVIR_ICON
1694 * [O] lprcLabel : ptr to Label rectangle
1695 * Same as LVM_GETITEMRECT with LVIR_LABEL
1697 * RETURN:
1698 * None.
1700 static void LISTVIEW_GetItemMetrics(LISTVIEW_INFO *infoPtr, LVITEMW *lpLVItem,
1701 LPRECT lprcBox, LPRECT lprcState,
1702 LPRECT lprcIcon, LPRECT lprcLabel)
1704 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
1705 BOOL doState = FALSE, doIcon = FALSE, doLabel = FALSE, oversizedBox = FALSE;
1706 RECT Box, State, Icon, Label;
1707 COLUMN_INFO *lpColumnInfo = NULL;
1709 TRACE("(lpLVItem=%s)\n", debuglvitem_t(lpLVItem, TRUE));
1711 /* Be smart and try to figure out the minimum we have to do */
1712 if (lpLVItem->iSubItem) assert(uView == LVS_REPORT);
1713 if (uView == LVS_ICON && (lprcBox || lprcLabel))
1715 assert((lpLVItem->mask & LVIF_STATE) && (lpLVItem->stateMask & LVIS_FOCUSED));
1716 if (lpLVItem->state & LVIS_FOCUSED) oversizedBox = doLabel = TRUE;
1718 if (lprcLabel) doLabel = TRUE;
1719 if (doLabel || lprcIcon) doIcon = TRUE;
1720 if (doIcon || lprcState) doState = TRUE;
1722 /************************************************************/
1723 /* compute the box rectangle (it should be cheap to do) */
1724 /************************************************************/
1725 if (lpLVItem->iSubItem || uView == LVS_REPORT)
1726 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, lpLVItem->iSubItem);
1728 if (lpLVItem->iSubItem)
1730 Box = lpColumnInfo->rcHeader;
1732 else
1734 Box.left = 0;
1735 Box.right = infoPtr->nItemWidth;
1737 Box.top = 0;
1738 Box.bottom = infoPtr->nItemHeight;
1740 /************************************************************/
1741 /* compute STATEICON bounding box */
1742 /************************************************************/
1743 if (doState)
1745 if (uView == LVS_ICON)
1747 State.left = Box.left - infoPtr->iconStateSize.cx - 2;
1748 if (infoPtr->himlNormal)
1749 State.left += (infoPtr->nItemWidth - infoPtr->iconSize.cx) / 2;
1750 State.top = Box.top + infoPtr->iconSize.cy - infoPtr->iconStateSize.cy + 4;
1752 else
1754 /* we need the ident in report mode, if we don't have it, we fail */
1755 State.left = Box.left;
1756 if (uView == LVS_REPORT)
1758 State.left += REPORT_MARGINX;
1759 if (lpLVItem->iSubItem == 0)
1761 assert(lpLVItem->mask & LVIF_INDENT);
1762 State.left += infoPtr->iconSize.cx * lpLVItem->iIndent;
1765 State.top = Box.top;
1767 State.right = State.left;
1768 State.bottom = State.top;
1769 if (infoPtr->himlState && lpLVItem->iSubItem == 0)
1771 State.right += infoPtr->iconStateSize.cx;
1772 State.bottom += infoPtr->iconStateSize.cy;
1774 if (lprcState) *lprcState = State;
1775 TRACE(" - state=%s\n", debugrect(&State));
1778 /************************************************************/
1779 /* compute ICON bounding box (ala LVM_GETITEMRECT) */
1780 /************************************************************/
1781 if (doIcon)
1783 if (uView == LVS_ICON)
1785 Icon.left = Box.left;
1786 if (infoPtr->himlNormal)
1787 Icon.left += (infoPtr->nItemWidth - infoPtr->iconSize.cx) / 2;
1788 Icon.top = Box.top + ICON_TOP_PADDING;
1789 Icon.right = Icon.left;
1790 Icon.bottom = Icon.top;
1791 if (infoPtr->himlNormal)
1793 Icon.right += infoPtr->iconSize.cx;
1794 Icon.bottom += infoPtr->iconSize.cy;
1797 else /* LVS_SMALLICON, LVS_LIST or LVS_REPORT */
1799 Icon.left = State.right;
1800 if (!IsRectEmpty(&State)) Icon.left += IMAGE_PADDING;
1801 Icon.top = Box.top;
1802 Icon.right = Icon.left;
1803 if (infoPtr->himlSmall && (!lpColumnInfo || lpLVItem->iSubItem == 0 || (lpColumnInfo->fmt & LVCFMT_IMAGE)))
1804 Icon.right += infoPtr->iconSize.cx;
1805 Icon.bottom = Icon.top + infoPtr->nItemHeight;
1807 if(lprcIcon) *lprcIcon = Icon;
1808 TRACE(" - icon=%s\n", debugrect(&Icon));
1811 /************************************************************/
1812 /* compute LABEL bounding box (ala LVM_GETITEMRECT) */
1813 /************************************************************/
1814 if (doLabel)
1816 SIZE labelSize = { 0, 0 };
1818 /* calculate how far to the right can the label strech */
1819 Label.right = Box.right;
1820 if (uView == LVS_REPORT)
1822 if (lpLVItem->iSubItem == 0) Label = lpColumnInfo->rcHeader;
1823 Label.right -= REPORT_MARGINX;
1826 if (lpLVItem->iSubItem || ((infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && uView == LVS_REPORT))
1828 labelSize.cx = infoPtr->nItemWidth;
1829 labelSize.cy = infoPtr->nItemHeight;
1830 goto calc_label;
1833 /* we need the text in non owner draw mode */
1834 assert(lpLVItem->mask & LVIF_TEXT);
1835 if (is_textT(lpLVItem->pszText, TRUE))
1837 HFONT hFont = infoPtr->hFont ? infoPtr->hFont : infoPtr->hDefaultFont;
1838 HDC hdc = GetDC(infoPtr->hwndSelf);
1839 HFONT hOldFont = SelectObject(hdc, hFont);
1840 UINT uFormat;
1841 RECT rcText;
1843 /* compute rough rectangle where the label will go */
1844 SetRectEmpty(&rcText);
1845 rcText.right = infoPtr->nItemWidth - TRAILING_PADDING;
1846 rcText.bottom = infoPtr->nItemHeight;
1847 if (uView == LVS_ICON)
1848 rcText.bottom -= ICON_TOP_PADDING + infoPtr->iconSize.cy + ICON_BOTTOM_PADDING;
1850 /* now figure out the flags */
1851 if (uView == LVS_ICON)
1852 uFormat = oversizedBox ? LV_FL_DT_FLAGS : LV_ML_DT_FLAGS;
1853 else
1854 uFormat = LV_SL_DT_FLAGS;
1856 DrawTextW (hdc, lpLVItem->pszText, -1, &rcText, uFormat | DT_CALCRECT);
1858 labelSize.cx = min(rcText.right - rcText.left + TRAILING_PADDING, infoPtr->nItemWidth);
1859 labelSize.cy = rcText.bottom - rcText.top;
1861 SelectObject(hdc, hOldFont);
1862 ReleaseDC(infoPtr->hwndSelf, hdc);
1865 calc_label:
1866 if (uView == LVS_ICON)
1868 Label.left = Box.left + (infoPtr->nItemWidth - labelSize.cx) / 2;
1869 Label.top = Box.top + ICON_TOP_PADDING_HITABLE +
1870 infoPtr->iconSize.cy + ICON_BOTTOM_PADDING;
1871 Label.right = Label.left + labelSize.cx;
1872 Label.bottom = Label.top + infoPtr->nItemHeight;
1873 if (!oversizedBox && labelSize.cy > infoPtr->ntmHeight)
1875 labelSize.cy = min(Box.bottom - Label.top, labelSize.cy);
1876 labelSize.cy /= infoPtr->ntmHeight;
1877 labelSize.cy = max(labelSize.cy, 1);
1878 labelSize.cy *= infoPtr->ntmHeight;
1880 Label.bottom = Label.top + labelSize.cy + HEIGHT_PADDING;
1882 else /* LVS_SMALLICON, LVS_LIST or LVS_REPORT */
1884 Label.left = Icon.right;
1885 if (!IsRectEmpty(&Icon) || !IsRectEmpty(&State)) Label.left += IMAGE_PADDING;
1886 Label.top = Box.top;
1887 Label.right = min(Label.left + labelSize.cx, Label.right);
1888 Label.bottom = Label.top + infoPtr->nItemHeight;
1891 if (lprcLabel) *lprcLabel = Label;
1892 TRACE(" - label=%s\n", debugrect(&Label));
1895 /* Fix the Box if necessary */
1896 if (lprcBox)
1898 if (oversizedBox) UnionRect(lprcBox, &Box, &Label);
1899 else *lprcBox = Box;
1901 TRACE(" - box=%s\n", debugrect(&Box));
1904 /***
1905 * DESCRIPTION: [INTERNAL]
1907 * PARAMETER(S):
1908 * [I] infoPtr : valid pointer to the listview structure
1909 * [I] nItem : item number
1910 * [O] lprcBox : ptr to Box rectangle
1912 * RETURN:
1913 * None.
1915 static void LISTVIEW_GetItemBox(LISTVIEW_INFO *infoPtr, INT nItem, LPRECT lprcBox)
1917 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
1918 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
1919 POINT Position, Origin;
1920 LVITEMW lvItem;
1922 LISTVIEW_GetOrigin(infoPtr, &Origin);
1923 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position);
1925 /* Be smart and try to figure out the minimum we have to do */
1926 lvItem.mask = 0;
1927 if (uView == LVS_ICON && infoPtr->bFocus && LISTVIEW_GetItemState(infoPtr, nItem, LVIS_FOCUSED))
1928 lvItem.mask |= LVIF_TEXT;
1929 lvItem.iItem = nItem;
1930 lvItem.iSubItem = 0;
1931 lvItem.pszText = szDispText;
1932 lvItem.cchTextMax = DISP_TEXT_SIZE;
1933 if (lvItem.mask) LISTVIEW_GetItemW(infoPtr, &lvItem);
1934 if (uView == LVS_ICON)
1936 lvItem.mask |= LVIF_STATE;
1937 lvItem.stateMask = LVIS_FOCUSED;
1938 lvItem.state = (lvItem.mask & LVIF_TEXT ? LVIS_FOCUSED : 0);
1940 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, lprcBox, 0, 0, 0);
1942 OffsetRect(lprcBox, Position.x + Origin.x, Position.y + Origin.y);
1946 /***
1947 * DESCRIPTION:
1948 * Returns the current icon position, and advances it along the top.
1949 * The returned position is not offset by Origin.
1951 * PARAMETER(S):
1952 * [I] infoPtr : valid pointer to the listview structure
1953 * [O] lpPos : will get the current icon position
1955 * RETURN:
1956 * None
1958 static void LISTVIEW_NextIconPosTop(LISTVIEW_INFO *infoPtr, LPPOINT lpPos)
1960 INT nListWidth = infoPtr->rcList.right - infoPtr->rcList.left;
1962 *lpPos = infoPtr->currIconPos;
1964 infoPtr->currIconPos.x += infoPtr->nItemWidth;
1965 if (infoPtr->currIconPos.x + infoPtr->nItemWidth <= nListWidth) return;
1967 infoPtr->currIconPos.x = 0;
1968 infoPtr->currIconPos.y += infoPtr->nItemHeight;
1972 /***
1973 * DESCRIPTION:
1974 * Returns the current icon position, and advances it down the left edge.
1975 * The returned position is not offset by Origin.
1977 * PARAMETER(S):
1978 * [I] infoPtr : valid pointer to the listview structure
1979 * [O] lpPos : will get the current icon position
1981 * RETURN:
1982 * None
1984 static void LISTVIEW_NextIconPosLeft(LISTVIEW_INFO *infoPtr, LPPOINT lpPos)
1986 INT nListHeight = infoPtr->rcList.bottom - infoPtr->rcList.top;
1988 *lpPos = infoPtr->currIconPos;
1990 infoPtr->currIconPos.y += infoPtr->nItemHeight;
1991 if (infoPtr->currIconPos.y + infoPtr->nItemHeight <= nListHeight) return;
1993 infoPtr->currIconPos.x += infoPtr->nItemWidth;
1994 infoPtr->currIconPos.y = 0;
1998 /***
1999 * DESCRIPTION:
2000 * Moves an icon to the specified position.
2001 * It takes care of invalidating the item, etc.
2003 * PARAMETER(S):
2004 * [I] infoPtr : valid pointer to the listview structure
2005 * [I] nItem : the item to move
2006 * [I] lpPos : the new icon position
2007 * [I] isNew : flags the item as being new
2009 * RETURN:
2010 * Success: TRUE
2011 * Failure: FALSE
2013 static BOOL LISTVIEW_MoveIconTo(LISTVIEW_INFO *infoPtr, INT nItem, LPPOINT lppt, BOOL isNew)
2015 POINT old;
2017 if (!isNew)
2019 old.x = (LONG)DPA_GetPtr(infoPtr->hdpaPosX, nItem);
2020 old.y = (LONG)DPA_GetPtr(infoPtr->hdpaPosY, nItem);
2022 if (lppt->x == old.x && lppt->y == old.y) return TRUE;
2023 LISTVIEW_InvalidateItem(infoPtr, nItem);
2026 /* Allocating a POINTER for every item is too resource intensive,
2027 * so we'll keep the (x,y) in different arrays */
2028 if (!DPA_SetPtr(infoPtr->hdpaPosX, nItem, (void *)lppt->x)) return FALSE;
2029 if (!DPA_SetPtr(infoPtr->hdpaPosY, nItem, (void *)lppt->y)) return FALSE;
2031 LISTVIEW_InvalidateItem(infoPtr, nItem);
2033 return TRUE;
2036 /***
2037 * DESCRIPTION:
2038 * Arranges listview items in icon display mode.
2040 * PARAMETER(S):
2041 * [I] infoPtr : valid pointer to the listview structure
2042 * [I] nAlignCode : alignment code
2044 * RETURN:
2045 * SUCCESS : TRUE
2046 * FAILURE : FALSE
2048 static BOOL LISTVIEW_Arrange(LISTVIEW_INFO *infoPtr, INT nAlignCode)
2050 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
2051 void (*next_pos)(LISTVIEW_INFO *, LPPOINT);
2052 POINT pos;
2053 INT i;
2055 if (uView != LVS_ICON && uView != LVS_SMALLICON) return FALSE;
2057 TRACE("nAlignCode=%d\n", nAlignCode);
2059 if (nAlignCode == LVA_DEFAULT)
2061 if (infoPtr->dwStyle & LVS_ALIGNLEFT) nAlignCode = LVA_ALIGNLEFT;
2062 else nAlignCode = LVA_ALIGNTOP;
2065 switch (nAlignCode)
2067 case LVA_ALIGNLEFT: next_pos = LISTVIEW_NextIconPosLeft; break;
2068 case LVA_ALIGNTOP: next_pos = LISTVIEW_NextIconPosTop; break;
2069 case LVA_SNAPTOGRID: next_pos = LISTVIEW_NextIconPosTop; break; /* FIXME */
2070 default: return FALSE;
2073 infoPtr->bAutoarrange = TRUE;
2074 infoPtr->currIconPos.x = infoPtr->currIconPos.y = 0;
2075 for (i = 0; i < infoPtr->nItemCount; i++)
2077 next_pos(infoPtr, &pos);
2078 LISTVIEW_MoveIconTo(infoPtr, i, &pos, FALSE);
2081 return TRUE;
2084 /***
2085 * DESCRIPTION:
2086 * Retrieves the bounding rectangle of all the items, not offset by Origin.
2088 * PARAMETER(S):
2089 * [I] infoPtr : valid pointer to the listview structure
2090 * [O] lprcView : bounding rectangle
2092 * RETURN:
2093 * SUCCESS : TRUE
2094 * FAILURE : FALSE
2096 static void LISTVIEW_GetAreaRect(LISTVIEW_INFO *infoPtr, LPRECT lprcView)
2098 INT i, x, y;
2100 SetRectEmpty(lprcView);
2102 switch (infoPtr->dwStyle & LVS_TYPEMASK)
2104 case LVS_ICON:
2105 case LVS_SMALLICON:
2106 for (i = 0; i < infoPtr->nItemCount; i++)
2108 x = (LONG)DPA_GetPtr(infoPtr->hdpaPosX, i);
2109 y = (LONG)DPA_GetPtr(infoPtr->hdpaPosX, i);
2110 lprcView->right = max(lprcView->right, x);
2111 lprcView->bottom = max(lprcView->bottom, y);
2113 if (infoPtr->nItemCount > 0)
2115 lprcView->right += infoPtr->nItemWidth;
2116 lprcView->bottom += infoPtr->nItemHeight;
2118 break;
2120 case LVS_LIST:
2121 y = LISTVIEW_GetCountPerColumn(infoPtr);
2122 x = infoPtr->nItemCount / y;
2123 if (infoPtr->nItemCount % y) x++;
2124 lprcView->right = x * infoPtr->nItemWidth;
2125 lprcView->bottom = y * infoPtr->nItemHeight;
2126 break;
2128 case LVS_REPORT:
2129 lprcView->right = infoPtr->nItemWidth;
2130 lprcView->bottom = infoPtr->nItemCount * infoPtr->nItemHeight;
2131 break;
2135 /***
2136 * DESCRIPTION:
2137 * Retrieves the bounding rectangle of all the items.
2139 * PARAMETER(S):
2140 * [I] infoPtr : valid pointer to the listview structure
2141 * [O] lprcView : bounding rectangle
2143 * RETURN:
2144 * SUCCESS : TRUE
2145 * FAILURE : FALSE
2147 static BOOL LISTVIEW_GetViewRect(LISTVIEW_INFO *infoPtr, LPRECT lprcView)
2149 POINT ptOrigin;
2151 TRACE("(lprcView=%p)\n", lprcView);
2153 if (!lprcView) return FALSE;
2155 LISTVIEW_GetOrigin(infoPtr, &ptOrigin);
2156 LISTVIEW_GetAreaRect(infoPtr, lprcView);
2157 OffsetRect(lprcView, ptOrigin.x, ptOrigin.y);
2159 TRACE("lprcView=%s\n", debugrect(lprcView));
2161 return TRUE;
2164 /***
2165 * DESCRIPTION:
2166 * Retrieves the subitem pointer associated with the subitem index.
2168 * PARAMETER(S):
2169 * [I] hdpaSubItems : DPA handle for a specific item
2170 * [I] nSubItem : index of subitem
2172 * RETURN:
2173 * SUCCESS : subitem pointer
2174 * FAILURE : NULL
2176 static SUBITEM_INFO* LISTVIEW_GetSubItemPtr(HDPA hdpaSubItems, INT nSubItem)
2178 SUBITEM_INFO *lpSubItem;
2179 INT i;
2181 /* we should binary search here if need be */
2182 for (i = 1; i < hdpaSubItems->nItemCount; i++)
2184 lpSubItem = (SUBITEM_INFO *)DPA_GetPtr(hdpaSubItems, i);
2185 if (lpSubItem->iSubItem == nSubItem)
2186 return lpSubItem;
2189 return NULL;
2193 /***
2194 * DESCRIPTION:
2195 * Caclulates the desired item width.
2197 * PARAMETER(S):
2198 * [I] infoPtr : valid pointer to the listview structure
2200 * RETURN:
2201 * The desired item width.
2203 static INT LISTVIEW_CalculateItemWidth(LISTVIEW_INFO *infoPtr)
2205 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
2206 INT nItemWidth = 0;
2208 TRACE("uView=%d\n", uView);
2210 if (uView == LVS_ICON)
2211 nItemWidth = infoPtr->iconSpacing.cx;
2212 else if (uView == LVS_REPORT)
2214 RECT rcHeader;
2216 if (infoPtr->hdpaColumns->nItemCount > 0)
2218 LISTVIEW_GetHeaderRect(infoPtr, infoPtr->hdpaColumns->nItemCount - 1, &rcHeader);
2219 nItemWidth = rcHeader.right;
2222 else /* LVS_SMALLICON, or LVS_LIST */
2224 INT i;
2226 for (i = 0; i < infoPtr->nItemCount; i++)
2227 nItemWidth = max(LISTVIEW_GetLabelWidth(infoPtr, i), nItemWidth);
2229 if (infoPtr->himlSmall) nItemWidth += infoPtr->iconSize.cx + IMAGE_PADDING;
2230 if (infoPtr->himlState) nItemWidth += infoPtr->iconStateSize.cx + IMAGE_PADDING;
2232 nItemWidth = max(DEFAULT_COLUMN_WIDTH, nItemWidth + WIDTH_PADDING);
2235 return max(nItemWidth, 1);
2238 /***
2239 * DESCRIPTION:
2240 * Caclulates the desired item height.
2242 * PARAMETER(S):
2243 * [I] infoPtr : valid pointer to the listview structure
2245 * RETURN:
2246 * The desired item height.
2248 static INT LISTVIEW_CalculateItemHeight(LISTVIEW_INFO *infoPtr)
2250 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
2251 INT nItemHeight;
2253 TRACE("uView=%d\n", uView);
2255 if (uView == LVS_ICON)
2256 nItemHeight = infoPtr->iconSpacing.cy;
2257 else
2259 nItemHeight = infoPtr->ntmHeight;
2260 if (infoPtr->himlState)
2261 nItemHeight = max(nItemHeight, infoPtr->iconStateSize.cy);
2262 if (infoPtr->himlSmall)
2263 nItemHeight = max(nItemHeight, infoPtr->iconSize.cy);
2264 if (infoPtr->himlState || infoPtr->himlSmall)
2265 nItemHeight += HEIGHT_PADDING;
2268 return max(nItemHeight, 1);
2271 /***
2272 * DESCRIPTION:
2273 * Updates the width, and height of an item.
2275 * PARAMETER(S):
2276 * [I] infoPtr : valid pointer to the listview structure
2278 * RETURN:
2279 * None.
2281 static inline void LISTVIEW_UpdateItemSize(LISTVIEW_INFO *infoPtr)
2283 infoPtr->nItemWidth = LISTVIEW_CalculateItemWidth(infoPtr);
2284 infoPtr->nItemHeight = LISTVIEW_CalculateItemHeight(infoPtr);
2288 /***
2289 * DESCRIPTION:
2290 * Retrieves and saves important text metrics info for the current
2291 * Listview font.
2293 * PARAMETER(S):
2294 * [I] infoPtr : valid pointer to the listview structure
2297 static void LISTVIEW_SaveTextMetrics(LISTVIEW_INFO *infoPtr)
2299 HDC hdc = GetDC(infoPtr->hwndSelf);
2300 HFONT hFont = infoPtr->hFont ? infoPtr->hFont : infoPtr->hDefaultFont;
2301 HFONT hOldFont = SelectObject(hdc, hFont);
2302 TEXTMETRICW tm;
2304 if (GetTextMetricsW(hdc, &tm))
2306 infoPtr->ntmHeight = tm.tmHeight;
2307 infoPtr->ntmAveCharWidth = tm.tmAveCharWidth;
2309 SelectObject(hdc, hOldFont);
2310 ReleaseDC(infoPtr->hwndSelf, hdc);
2312 TRACE("tmHeight=%d\n", infoPtr->ntmHeight);
2315 /***
2316 * DESCRIPTION:
2317 * A compare function for ranges
2319 * PARAMETER(S)
2320 * [I] range1 : pointer to range 1;
2321 * [I] range2 : pointer to range 2;
2322 * [I] flags : flags
2324 * RETURNS:
2325 * > 0 : if range 1 > range 2
2326 * < 0 : if range 2 > range 1
2327 * = 0 : if range intersects range 2
2329 static INT CALLBACK ranges_cmp(LPVOID range1, LPVOID range2, LPARAM flags)
2331 INT cmp;
2333 if (((RANGE*)range1)->upper <= ((RANGE*)range2)->lower)
2334 cmp = -1;
2335 else if (((RANGE*)range2)->upper <= ((RANGE*)range1)->lower)
2336 cmp = 1;
2337 else
2338 cmp = 0;
2340 TRACE("range1=%s, range2=%s, cmp=%d\n", debugrange((RANGE*)range1), debugrange((RANGE*)range2), cmp);
2342 return cmp;
2345 #if DEBUG_RANGES
2346 #define ranges_check(ranges, desc) ranges_assert(ranges, desc, __FUNCTION__, __LINE__)
2347 #else
2348 #define ranges_check(ranges, desc) do { } while(0)
2349 #endif
2351 static void ranges_assert(RANGES ranges, LPCSTR desc, const char *func, int line)
2353 INT i;
2354 RANGE *prev, *curr;
2356 TRACE("*** Checking %s:%d:%s ***\n", func, line, desc);
2357 assert (ranges);
2358 assert (ranges->hdpa->nItemCount >= 0);
2359 ranges_dump(ranges);
2360 prev = (RANGE *)DPA_GetPtr(ranges->hdpa, 0);
2361 if (ranges->hdpa->nItemCount > 0)
2362 assert (prev->lower >= 0 && prev->lower < prev->upper);
2363 for (i = 1; i < ranges->hdpa->nItemCount; i++)
2365 curr = (RANGE *)DPA_GetPtr(ranges->hdpa, i);
2366 assert (prev->upper <= curr->lower);
2367 assert (curr->lower < curr->upper);
2368 prev = curr;
2370 TRACE("--- Done checking---\n");
2373 static RANGES ranges_create(int count)
2375 RANGES ranges = (RANGES)COMCTL32_Alloc(sizeof(struct tagRANGES));
2376 if (!ranges) return NULL;
2377 ranges->hdpa = DPA_Create(count);
2378 if (ranges->hdpa) return ranges;
2379 COMCTL32_Free(ranges);
2380 return NULL;
2383 static void ranges_clear(RANGES ranges)
2385 INT i;
2387 for(i = 0; i < ranges->hdpa->nItemCount; i++)
2388 COMCTL32_Free(DPA_GetPtr(ranges->hdpa, i));
2389 DPA_DeleteAllPtrs(ranges->hdpa);
2393 static void ranges_destroy(RANGES ranges)
2395 ranges_clear(ranges);
2396 DPA_Destroy(ranges->hdpa);
2397 COMCTL32_Free(ranges);
2400 static RANGES ranges_clone(RANGES ranges)
2402 RANGES clone;
2403 INT i;
2405 if (!(clone = ranges_create(ranges->hdpa->nItemCount))) goto fail;
2407 for (i = 0; i < ranges->hdpa->nItemCount; i++)
2409 RANGE *newrng = (RANGE *)COMCTL32_Alloc(sizeof(RANGE));
2410 if (!newrng) goto fail;
2411 *newrng = *((RANGE*)DPA_GetPtr(ranges->hdpa, i));
2412 DPA_SetPtr(clone->hdpa, i, newrng);
2414 return clone;
2416 fail:
2417 TRACE ("clone failed\n");
2418 if (clone) ranges_destroy(clone);
2419 return NULL;
2422 static RANGES ranges_diff(RANGES ranges, RANGES sub)
2424 INT i;
2426 for (i = 0; i < sub->hdpa->nItemCount; i++)
2427 ranges_del(ranges, *((RANGE *)DPA_GetPtr(sub->hdpa, i)));
2429 return ranges;
2432 static void ranges_dump(RANGES ranges)
2434 INT i;
2436 for (i = 0; i < ranges->hdpa->nItemCount; i++)
2437 TRACE(" %s\n", debugrange(DPA_GetPtr(ranges->hdpa, i)));
2440 static inline BOOL ranges_contain(RANGES ranges, INT nItem)
2442 RANGE srchrng = { nItem, nItem + 1 };
2444 TRACE("(nItem=%d)\n", nItem);
2445 ranges_check(ranges, "before contain");
2446 return DPA_Search(ranges->hdpa, &srchrng, 0, ranges_cmp, 0, DPAS_SORTED) != -1;
2449 static INT ranges_itemcount(RANGES ranges)
2451 INT i, count = 0;
2453 for (i = 0; i < ranges->hdpa->nItemCount; i++)
2455 RANGE *sel = DPA_GetPtr(ranges->hdpa, i);
2456 count += sel->upper - sel->lower;
2459 return count;
2462 static BOOL ranges_shift(RANGES ranges, INT nItem, INT delta, INT nUpper)
2464 RANGE srchrng = { nItem, nItem + 1 }, *chkrng;
2465 INT index;
2467 index = DPA_Search(ranges->hdpa, &srchrng, 0, ranges_cmp, 0, DPAS_SORTED | DPAS_INSERTAFTER);
2468 if (index == -1) return TRUE;
2470 for (; index < ranges->hdpa->nItemCount; index++)
2472 chkrng = DPA_GetPtr(ranges->hdpa, index);
2473 if (chkrng->lower >= nItem)
2474 chkrng->lower = max(min(chkrng->lower + delta, nUpper - 1), 0);
2475 if (chkrng->upper > nItem)
2476 chkrng->upper = max(min(chkrng->upper + delta, nUpper), 0);
2478 return TRUE;
2481 static BOOL ranges_add(RANGES ranges, RANGE range)
2483 RANGE srchrgn;
2484 INT index;
2486 TRACE("(%s)\n", debugrange(&range));
2487 ranges_check(ranges, "before add");
2489 /* try find overlapping regions first */
2490 srchrgn.lower = range.lower - 1;
2491 srchrgn.upper = range.upper + 1;
2492 index = DPA_Search(ranges->hdpa, &srchrgn, 0, ranges_cmp, 0, DPAS_SORTED);
2494 if (index == -1)
2496 RANGE *newrgn;
2498 TRACE("Adding new range\n");
2500 /* create the brand new range to insert */
2501 newrgn = (RANGE *)COMCTL32_Alloc(sizeof(RANGE));
2502 if(!newrgn) goto fail;
2503 *newrgn = range;
2505 /* figure out where to insert it */
2506 index = DPA_Search(ranges->hdpa, newrgn, 0, ranges_cmp, 0, DPAS_SORTED | DPAS_INSERTAFTER);
2507 TRACE("index=%d\n", index);
2508 if (index == -1) index = 0;
2510 /* and get it over with */
2511 if (DPA_InsertPtr(ranges->hdpa, index, newrgn) == -1)
2513 COMCTL32_Free(newrgn);
2514 goto fail;
2517 else
2519 RANGE *chkrgn, *mrgrgn;
2520 INT fromindex, mergeindex;
2522 chkrgn = DPA_GetPtr(ranges->hdpa, index);
2523 TRACE("Merge with %s @%d\n", debugrange(chkrgn), index);
2525 chkrgn->lower = min(range.lower, chkrgn->lower);
2526 chkrgn->upper = max(range.upper, chkrgn->upper);
2528 TRACE("New range %s @%d\n", debugrange(chkrgn), index);
2530 /* merge now common anges */
2531 fromindex = 0;
2532 srchrgn.lower = chkrgn->lower - 1;
2533 srchrgn.upper = chkrgn->upper + 1;
2537 mergeindex = DPA_Search(ranges->hdpa, &srchrgn, fromindex, ranges_cmp, 0, 0);
2538 if (mergeindex == -1) break;
2539 if (mergeindex == index)
2541 fromindex = index + 1;
2542 continue;
2545 TRACE("Merge with index %i\n", mergeindex);
2547 mrgrgn = DPA_GetPtr(ranges->hdpa, mergeindex);
2548 chkrgn->lower = min(chkrgn->lower, mrgrgn->lower);
2549 chkrgn->upper = max(chkrgn->upper, mrgrgn->upper);
2550 COMCTL32_Free(mrgrgn);
2551 DPA_DeletePtr(ranges->hdpa, mergeindex);
2552 if (mergeindex < index) index --;
2553 } while(1);
2556 ranges_check(ranges, "after add");
2557 return TRUE;
2559 fail:
2560 ranges_check(ranges, "failed add");
2561 return FALSE;
2564 static BOOL ranges_del(RANGES ranges, RANGE range)
2566 RANGE *chkrgn;
2567 INT index;
2569 TRACE("(%s)\n", debugrange(&range));
2570 ranges_check(ranges, "before del");
2572 /* we don't use DPAS_SORTED here, since we need *
2573 * to find the first overlapping range */
2574 index = DPA_Search(ranges->hdpa, &range, 0, ranges_cmp, 0, 0);
2575 while(index != -1)
2577 chkrgn = DPA_GetPtr(ranges->hdpa, index);
2579 TRACE("Matches range %s @%d\n", debugrange(chkrgn), index);
2581 /* case 1: Same range */
2582 if ( (chkrgn->upper == range.upper) &&
2583 (chkrgn->lower == range.lower) )
2585 DPA_DeletePtr(ranges->hdpa, index);
2586 break;
2588 /* case 2: engulf */
2589 else if ( (chkrgn->upper <= range.upper) &&
2590 (chkrgn->lower >= range.lower) )
2592 DPA_DeletePtr(ranges->hdpa, index);
2594 /* case 3: overlap upper */
2595 else if ( (chkrgn->upper <= range.upper) &&
2596 (chkrgn->lower < range.lower) )
2598 chkrgn->upper = range.lower;
2600 /* case 4: overlap lower */
2601 else if ( (chkrgn->upper > range.upper) &&
2602 (chkrgn->lower >= range.lower) )
2604 chkrgn->lower = range.upper;
2605 break;
2607 /* case 5: fully internal */
2608 else
2610 RANGE tmprgn = *chkrgn, *newrgn;
2612 if (!(newrgn = (RANGE *)COMCTL32_Alloc(sizeof(RANGE)))) goto fail;
2613 newrgn->lower = chkrgn->lower;
2614 newrgn->upper = range.lower;
2615 chkrgn->lower = range.upper;
2616 if (DPA_InsertPtr(ranges->hdpa, index, newrgn) == -1)
2618 COMCTL32_Free(newrgn);
2619 goto fail;
2621 chkrgn = &tmprgn;
2622 break;
2625 index = DPA_Search(ranges->hdpa, &range, index, ranges_cmp, 0, 0);
2628 ranges_check(ranges, "after del");
2629 return TRUE;
2631 fail:
2632 ranges_check(ranges, "failed del");
2633 return FALSE;
2636 /***
2637 * DESCRIPTION:
2638 * Removes all selection ranges
2640 * Parameters(s):
2641 * [I] infoPtr : valid pointer to the listview structure
2642 * [I] toSkip : item range to skip removing the selection
2644 * RETURNS:
2645 * SUCCESS : TRUE
2646 * FAILURE : TRUE
2648 static BOOL LISTVIEW_DeselectAllSkipItems(LISTVIEW_INFO *infoPtr, RANGES toSkip)
2650 LVITEMW lvItem;
2651 ITERATOR i;
2652 RANGES clone;
2654 TRACE("()\n");
2656 lvItem.state = 0;
2657 lvItem.stateMask = LVIS_SELECTED;
2659 /* need to clone the DPA because callbacks can change it */
2660 if (!(clone = ranges_clone(infoPtr->selectionRanges))) return FALSE;
2661 iterator_rangesitems(&i, ranges_diff(clone, toSkip));
2662 while(iterator_next(&i))
2663 LISTVIEW_SetItemState(infoPtr, i.nItem, &lvItem);
2664 /* note that the iterator destructor will free the cloned range */
2665 iterator_destroy(&i);
2667 return TRUE;
2670 static inline BOOL LISTVIEW_DeselectAllSkipItem(LISTVIEW_INFO *infoPtr, INT nItem)
2672 RANGES toSkip;
2674 if (!(toSkip = ranges_create(1))) return FALSE;
2675 if (nItem != -1) ranges_additem(toSkip, nItem);
2676 LISTVIEW_DeselectAllSkipItems(infoPtr, toSkip);
2677 ranges_destroy(toSkip);
2678 return TRUE;
2681 static inline BOOL LISTVIEW_DeselectAll(LISTVIEW_INFO *infoPtr)
2683 return LISTVIEW_DeselectAllSkipItem(infoPtr, -1);
2686 /***
2687 * DESCRIPTION:
2688 * Retrieves the number of items that are marked as selected.
2690 * PARAMETER(S):
2691 * [I] infoPtr : valid pointer to the listview structure
2693 * RETURN:
2694 * Number of items selected.
2696 static INT LISTVIEW_GetSelectedCount(LISTVIEW_INFO *infoPtr)
2698 INT nSelectedCount = 0;
2700 if (infoPtr->uCallbackMask & LVIS_SELECTED)
2702 INT i;
2703 for (i = 0; i < infoPtr->nItemCount; i++)
2705 if (LISTVIEW_GetItemState(infoPtr, i, LVIS_SELECTED))
2706 nSelectedCount++;
2709 else
2710 nSelectedCount = ranges_itemcount(infoPtr->selectionRanges);
2712 TRACE("nSelectedCount=%d\n", nSelectedCount);
2713 return nSelectedCount;
2716 /***
2717 * DESCRIPTION:
2718 * Manages the item focus.
2720 * PARAMETER(S):
2721 * [I] infoPtr : valid pointer to the listview structure
2722 * [I] nItem : item index
2724 * RETURN:
2725 * TRUE : focused item changed
2726 * FALSE : focused item has NOT changed
2728 static inline BOOL LISTVIEW_SetItemFocus(LISTVIEW_INFO *infoPtr, INT nItem)
2730 INT oldFocus = infoPtr->nFocusedItem;
2731 LVITEMW lvItem;
2733 if (nItem == infoPtr->nFocusedItem) return FALSE;
2735 lvItem.state = nItem == -1 ? 0 : LVIS_FOCUSED;
2736 lvItem.stateMask = LVIS_FOCUSED;
2737 LISTVIEW_SetItemState(infoPtr, nItem == -1 ? infoPtr->nFocusedItem : nItem, &lvItem);
2739 return oldFocus != infoPtr->nFocusedItem;
2742 /* Helper function for LISTVIEW_ShiftIndices *only* */
2743 static INT shift_item(LISTVIEW_INFO *infoPtr, INT nShiftItem, INT nItem, INT direction)
2745 if (nShiftItem < nItem) return nShiftItem;
2747 if (nShiftItem > nItem) return nShiftItem + direction;
2749 if (direction > 0) return nShiftItem + direction;
2751 return min(nShiftItem, infoPtr->nItemCount - 1);
2755 * DESCRIPTION:
2756 * Updates the various indices after an item has been inserted or deleted.
2758 * PARAMETER(S):
2759 * [I] infoPtr : valid pointer to the listview structure
2760 * [I] nItem : item index
2761 * [I] direction : Direction of shift, +1 or -1.
2763 * RETURN:
2764 * None
2766 static void LISTVIEW_ShiftIndices(LISTVIEW_INFO *infoPtr, INT nItem, INT direction)
2768 INT nNewFocus;
2770 TRACE("Shifting %iu, %i steps\n", nItem, direction);
2772 ranges_shift(infoPtr->selectionRanges, nItem, direction, infoPtr->nItemCount);
2774 assert(abs(direction) == 1);
2776 infoPtr->nSelectionMark = shift_item(infoPtr, infoPtr->nSelectionMark, nItem, direction);
2778 nNewFocus = shift_item(infoPtr, infoPtr->nFocusedItem, nItem, direction);
2779 if (nNewFocus != infoPtr->nFocusedItem)
2780 LISTVIEW_SetItemFocus(infoPtr, nNewFocus);
2782 /* But we are not supposed to modify nHotItem! */
2787 * DESCRIPTION:
2788 * Adds a block of selections.
2790 * PARAMETER(S):
2791 * [I] infoPtr : valid pointer to the listview structure
2792 * [I] nItem : item index
2794 * RETURN:
2795 * None
2797 static void LISTVIEW_AddGroupSelection(LISTVIEW_INFO *infoPtr, INT nItem)
2799 INT nFirst = min(infoPtr->nSelectionMark, nItem);
2800 INT nLast = max(infoPtr->nSelectionMark, nItem);
2801 INT i;
2802 LVITEMW item;
2804 if (nFirst == -1) nFirst = nItem;
2806 item.state = LVIS_SELECTED;
2807 item.stateMask = LVIS_SELECTED;
2809 /* FIXME: this is not correct LVS_OWNERDATA
2810 * setting the item states individually will generate
2811 * a LVN_ITEMCHANGED notification for each one. Instead,
2812 * we have to send a LVN_ODSTATECHANGED notification.
2813 * See MSDN documentation for LVN_ITEMCHANGED.
2815 for (i = nFirst; i <= nLast; i++)
2816 LISTVIEW_SetItemState(infoPtr,i,&item);
2820 /***
2821 * DESCRIPTION:
2822 * Sets a single group selection.
2824 * PARAMETER(S):
2825 * [I] infoPtr : valid pointer to the listview structure
2826 * [I] nItem : item index
2828 * RETURN:
2829 * None
2831 static void LISTVIEW_SetGroupSelection(LISTVIEW_INFO *infoPtr, INT nItem)
2833 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
2834 RANGES selection;
2835 LVITEMW item;
2836 ITERATOR i;
2838 if (!(selection = ranges_create(100))) return;
2840 item.state = LVIS_SELECTED;
2841 item.stateMask = LVIS_SELECTED;
2843 if ((uView == LVS_LIST) || (uView == LVS_REPORT))
2845 if (infoPtr->nSelectionMark == -1)
2847 infoPtr->nSelectionMark = nItem;
2848 ranges_additem(selection, nItem);
2850 else
2852 RANGE sel;
2854 sel.lower = min(infoPtr->nSelectionMark, nItem);
2855 sel.upper = max(infoPtr->nSelectionMark, nItem) + 1;
2856 ranges_add(selection, sel);
2859 else
2861 RECT rcItem, rcSel, rcSelMark;
2862 POINT ptItem;
2864 rcItem.left = LVIR_BOUNDS;
2865 if (!LISTVIEW_GetItemRect(infoPtr, nItem, &rcItem)) return;
2866 rcSelMark.left = LVIR_BOUNDS;
2867 if (!LISTVIEW_GetItemRect(infoPtr, infoPtr->nSelectionMark, &rcSelMark)) return;
2868 UnionRect(&rcSel, &rcItem, &rcSelMark);
2869 iterator_frameditems(&i, infoPtr, &rcSel);
2870 while(iterator_next(&i))
2872 LISTVIEW_GetItemPosition(infoPtr, i.nItem, &ptItem);
2873 if (PtInRect(&rcSel, ptItem)) ranges_additem(selection, i.nItem);
2875 iterator_destroy(&i);
2878 LISTVIEW_DeselectAllSkipItems(infoPtr, selection);
2879 iterator_rangesitems(&i, selection);
2880 while(iterator_next(&i))
2881 LISTVIEW_SetItemState(infoPtr, i.nItem, &item);
2882 /* this will also destroy the selection */
2883 iterator_destroy(&i);
2885 LISTVIEW_SetItemFocus(infoPtr, nItem);
2888 /***
2889 * DESCRIPTION:
2890 * Sets a single selection.
2892 * PARAMETER(S):
2893 * [I] infoPtr : valid pointer to the listview structure
2894 * [I] nItem : item index
2896 * RETURN:
2897 * None
2899 static void LISTVIEW_SetSelection(LISTVIEW_INFO *infoPtr, INT nItem)
2901 LVITEMW lvItem;
2903 TRACE("nItem=%d\n", nItem);
2905 LISTVIEW_DeselectAllSkipItem(infoPtr, nItem);
2907 lvItem.state = LVIS_FOCUSED | LVIS_SELECTED;
2908 lvItem.stateMask = LVIS_FOCUSED | LVIS_SELECTED;
2909 LISTVIEW_SetItemState(infoPtr, nItem, &lvItem);
2911 infoPtr->nSelectionMark = nItem;
2914 /***
2915 * DESCRIPTION:
2916 * Set selection(s) with keyboard.
2918 * PARAMETER(S):
2919 * [I] infoPtr : valid pointer to the listview structure
2920 * [I] nItem : item index
2922 * RETURN:
2923 * SUCCESS : TRUE (needs to be repainted)
2924 * FAILURE : FALSE (nothing has changed)
2926 static BOOL LISTVIEW_KeySelection(LISTVIEW_INFO *infoPtr, INT nItem)
2928 /* FIXME: pass in the state */
2929 WORD wShift = HIWORD(GetKeyState(VK_SHIFT));
2930 WORD wCtrl = HIWORD(GetKeyState(VK_CONTROL));
2931 BOOL bResult = FALSE;
2933 if ((nItem >= 0) && (nItem < infoPtr->nItemCount))
2935 if (infoPtr->dwStyle & LVS_SINGLESEL)
2937 bResult = TRUE;
2938 LISTVIEW_SetSelection(infoPtr, nItem);
2940 else
2942 if (wShift)
2944 bResult = TRUE;
2945 LISTVIEW_SetGroupSelection(infoPtr, nItem);
2947 else if (wCtrl)
2949 bResult = LISTVIEW_SetItemFocus(infoPtr, nItem);
2951 else
2953 bResult = TRUE;
2954 LISTVIEW_SetSelection(infoPtr, nItem);
2957 ListView_EnsureVisible(infoPtr->hwndSelf, nItem, FALSE);
2960 UpdateWindow(infoPtr->hwndSelf); /* update client area */
2961 return bResult;
2965 /***
2966 * DESCRIPTION:
2967 * Called when the mouse is being actively tracked and has hovered for a specified
2968 * amount of time
2970 * PARAMETER(S):
2971 * [I] infoPtr : valid pointer to the listview structure
2972 * [I] fwKeys : key indicator
2973 * [I] pts : mouse position
2975 * RETURN:
2976 * 0 if the message was processed, non-zero if there was an error
2978 * INFO:
2979 * LVS_EX_TRACKSELECT: An item is automatically selected when the cursor remains
2980 * over the item for a certain period of time.
2983 static LRESULT LISTVIEW_MouseHover(LISTVIEW_INFO *infoPtr, WORD fwKyes, POINTS pts)
2985 if(infoPtr->dwLvExStyle & LVS_EX_TRACKSELECT)
2986 /* FIXME: select the item!!! */
2987 /*LISTVIEW_GetItemAtPt(infoPtr, pt)*/;
2989 return 0;
2992 /***
2993 * DESCRIPTION:
2994 * Called whenever WM_MOUSEMOVE is received.
2996 * PARAMETER(S):
2997 * [I] infoPtr : valid pointer to the listview structure
2998 * [I] fwKeys : key indicator
2999 * [I] pts : mouse position
3001 * RETURN:
3002 * 0 if the message is processed, non-zero if there was an error
3004 static LRESULT LISTVIEW_MouseMove(LISTVIEW_INFO *infoPtr, WORD fwKeys, POINTS pts)
3006 TRACKMOUSEEVENT trackinfo;
3008 /* see if we are supposed to be tracking mouse hovering */
3009 if(infoPtr->dwLvExStyle & LVS_EX_TRACKSELECT) {
3010 /* fill in the trackinfo struct */
3011 trackinfo.cbSize = sizeof(TRACKMOUSEEVENT);
3012 trackinfo.dwFlags = TME_QUERY;
3013 trackinfo.hwndTrack = infoPtr->hwndSelf;
3014 trackinfo.dwHoverTime = infoPtr->dwHoverTime;
3016 /* see if we are already tracking this hwnd */
3017 _TrackMouseEvent(&trackinfo);
3019 if(!(trackinfo.dwFlags & TME_HOVER)) {
3020 trackinfo.dwFlags = TME_HOVER;
3022 /* call TRACKMOUSEEVENT so we receive WM_MOUSEHOVER messages */
3023 _TrackMouseEvent(&trackinfo);
3027 return 0;
3031 /***
3032 * Tests wheather the item is assignable to a list with style lStyle
3034 static inline BOOL is_assignable_item(LPLVITEMW lpLVItem, LONG lStyle)
3036 if ( (lpLVItem->mask & LVIF_TEXT) &&
3037 (lpLVItem->pszText == LPSTR_TEXTCALLBACKW) &&
3038 (lStyle & (LVS_SORTASCENDING | LVS_SORTDESCENDING)) ) return FALSE;
3040 return TRUE;
3044 /***
3045 * DESCRIPTION:
3046 * Helper for LISTVIEW_SetItemT *only*: sets item attributes.
3048 * PARAMETER(S):
3049 * [I] infoPtr : valid pointer to the listview structure
3050 * [I] lpLVItem : valid pointer to new item atttributes
3051 * [I] isNew : the item being set is being inserted
3052 * [I] isW : TRUE if lpLVItem is Unicode, FALSE if it's ANSI
3053 * [O] bChanged : will be set to TRUE if the item really changed
3055 * RETURN:
3056 * SUCCESS : TRUE
3057 * FAILURE : FALSE
3059 static BOOL set_main_item(LISTVIEW_INFO *infoPtr, LPLVITEMW lpLVItem, BOOL isNew, BOOL isW, BOOL *bChanged)
3061 ITEM_INFO *lpItem;
3062 NMLISTVIEW nmlv;
3063 UINT uChanged = 0;
3064 LVITEMW item;
3066 TRACE("()\n");
3068 assert(lpLVItem->iItem >= 0 && lpLVItem->iItem < infoPtr->nItemCount);
3070 if (lpLVItem->mask == 0) return TRUE;
3072 if (infoPtr->dwStyle & LVS_OWNERDATA)
3074 /* a virtual listview we stores only selection and focus */
3075 if ((lpLVItem->mask & ~LVIF_STATE) || (lpLVItem->stateMask & ~(LVIS_FOCUSED | LVIS_SELECTED)))
3076 return FALSE;
3077 lpItem = NULL;
3079 else
3081 HDPA hdpaSubItems = (HDPA)DPA_GetPtr(infoPtr->hdpaItems, lpLVItem->iItem);
3082 lpItem = (ITEM_INFO *)DPA_GetPtr(hdpaSubItems, 0);
3083 assert (lpItem);
3086 /* we need to get the lParam and state of the item */
3087 item.iItem = lpLVItem->iItem;
3088 item.iSubItem = lpLVItem->iSubItem;
3089 item.mask = LVIF_STATE | LVIF_PARAM;
3090 item.stateMask = ~0;
3091 item.state = 0;
3092 item.lParam = 0;
3093 if (!isNew && !LISTVIEW_GetItemW(infoPtr, &item)) return FALSE;
3095 TRACE("oldState=%x, newState=%x\n", item.state, lpLVItem->state);
3096 /* determine what fields will change */
3097 if ((lpLVItem->mask & LVIF_STATE) && ((item.state ^ lpLVItem->state) & lpLVItem->stateMask & ~infoPtr->uCallbackMask))
3098 uChanged |= LVIF_STATE;
3100 if ((lpLVItem->mask & LVIF_IMAGE) && (lpItem->hdr.iImage != lpLVItem->iImage))
3101 uChanged |= LVIF_IMAGE;
3103 if ((lpLVItem->mask & LVIF_PARAM) && (lpItem->lParam != lpLVItem->lParam))
3104 uChanged |= LVIF_PARAM;
3106 if ((lpLVItem->mask & LVIF_INDENT) && (lpItem->iIndent != lpLVItem->iIndent))
3107 uChanged |= LVIF_INDENT;
3109 if ((lpLVItem->mask & LVIF_TEXT) && textcmpWT(lpItem->hdr.pszText, lpLVItem->pszText, isW))
3110 uChanged |= LVIF_TEXT;
3112 TRACE("uChanged=0x%x\n", uChanged);
3113 if (!uChanged) return TRUE;
3114 *bChanged = TRUE;
3116 ZeroMemory(&nmlv, sizeof(NMLISTVIEW));
3117 nmlv.iItem = lpLVItem->iItem;
3118 nmlv.uNewState = (item.state & ~lpLVItem->stateMask) | (lpLVItem->state & lpLVItem->stateMask);
3119 nmlv.uOldState = item.state;
3120 nmlv.uChanged = uChanged;
3121 nmlv.lParam = item.lParam;
3123 /* send LVN_ITEMCHANGING notification, if the item is not being inserted */
3124 /* and we are _NOT_ virtual (LVS_OWERNDATA) */
3125 if(lpItem && !isNew && notify_listview(infoPtr, LVN_ITEMCHANGING, &nmlv))
3126 return FALSE;
3128 /* copy information */
3129 if (lpLVItem->mask & LVIF_TEXT)
3130 textsetptrT(&lpItem->hdr.pszText, lpLVItem->pszText, isW);
3132 if (lpLVItem->mask & LVIF_IMAGE)
3133 lpItem->hdr.iImage = lpLVItem->iImage;
3135 if (lpLVItem->mask & LVIF_PARAM)
3136 lpItem->lParam = lpLVItem->lParam;
3138 if (lpLVItem->mask & LVIF_INDENT)
3139 lpItem->iIndent = lpLVItem->iIndent;
3141 if (uChanged & LVIF_STATE)
3143 if (lpLVItem->stateMask & ~infoPtr->uCallbackMask & ~(LVIS_FOCUSED | LVIS_SELECTED))
3145 lpItem->state &= ~lpLVItem->stateMask;
3146 lpItem->state |= (lpLVItem->state & lpLVItem->stateMask);
3148 if (lpLVItem->state & lpLVItem->stateMask & ~infoPtr->uCallbackMask & LVIS_SELECTED)
3150 if (infoPtr->dwStyle & LVS_SINGLESEL) LISTVIEW_DeselectAllSkipItem(infoPtr, lpLVItem->iItem);
3151 ranges_additem(infoPtr->selectionRanges, lpLVItem->iItem);
3153 else if (lpLVItem->stateMask & LVIS_SELECTED)
3154 ranges_delitem(infoPtr->selectionRanges, lpLVItem->iItem);
3156 /* if we are asked to change focus, and we manage it, do it */
3157 if (lpLVItem->state & lpLVItem->stateMask & ~infoPtr->uCallbackMask & LVIS_FOCUSED)
3159 if (lpLVItem->state & LVIS_FOCUSED)
3161 LISTVIEW_SetItemFocus(infoPtr, -1);
3162 infoPtr->nFocusedItem = lpLVItem->iItem;
3163 LISTVIEW_EnsureVisible(infoPtr, lpLVItem->iItem, FALSE);
3165 else if (infoPtr->nFocusedItem == lpLVItem->iItem)
3166 infoPtr->nFocusedItem = -1;
3170 /* if we're inserting the item, we're done */
3171 if (isNew) return TRUE;
3173 /* send LVN_ITEMCHANGED notification */
3174 if (lpLVItem->mask & LVIF_PARAM) nmlv.lParam = lpLVItem->lParam;
3175 notify_listview(infoPtr, LVN_ITEMCHANGED, &nmlv);
3177 return TRUE;
3180 /***
3181 * DESCRIPTION:
3182 * Helper for LISTVIEW_{Set,Insert}ItemT *only*: sets subitem attributes.
3184 * PARAMETER(S):
3185 * [I] infoPtr : valid pointer to the listview structure
3186 * [I] lpLVItem : valid pointer to new subitem atttributes
3187 * [I] isW : TRUE if lpLVItem is Unicode, FALSE if it's ANSI
3188 * [O] bChanged : will be set to TRUE if the item really changed
3190 * RETURN:
3191 * SUCCESS : TRUE
3192 * FAILURE : FALSE
3194 static BOOL set_sub_item(LISTVIEW_INFO *infoPtr, LPLVITEMW lpLVItem, BOOL isW, BOOL *bChanged)
3196 HDPA hdpaSubItems;
3197 SUBITEM_INFO *lpSubItem;
3199 /* we do not support subitems for virtual listviews */
3200 if (infoPtr->dwStyle & LVS_OWNERDATA) return FALSE;
3202 /* set subitem only if column is present */
3203 if (lpLVItem->iSubItem >= infoPtr->hdpaColumns->nItemCount) return FALSE;
3205 /* First do some sanity checks */
3206 if (lpLVItem->mask & ~(LVIF_TEXT | LVIF_IMAGE)) return FALSE;
3207 if (!(lpLVItem->mask & (LVIF_TEXT | LVIF_IMAGE))) return TRUE;
3209 /* get the subitem structure, and create it if not there */
3210 hdpaSubItems = (HDPA)DPA_GetPtr(infoPtr->hdpaItems, lpLVItem->iItem);
3211 assert (hdpaSubItems);
3213 lpSubItem = LISTVIEW_GetSubItemPtr(hdpaSubItems, lpLVItem->iSubItem);
3214 if (!lpSubItem)
3216 SUBITEM_INFO *tmpSubItem;
3217 INT i;
3219 lpSubItem = (SUBITEM_INFO *)COMCTL32_Alloc(sizeof(SUBITEM_INFO));
3220 if (!lpSubItem) return FALSE;
3221 /* we could binary search here, if need be...*/
3222 for (i = 1; i < hdpaSubItems->nItemCount; i++)
3224 tmpSubItem = (SUBITEM_INFO *)DPA_GetPtr(hdpaSubItems, i);
3225 if (tmpSubItem->iSubItem > lpLVItem->iSubItem) break;
3227 if (DPA_InsertPtr(hdpaSubItems, i, lpSubItem) == -1)
3229 COMCTL32_Free(lpSubItem);
3230 return FALSE;
3232 lpSubItem->iSubItem = lpLVItem->iSubItem;
3233 *bChanged = TRUE;
3236 if (lpLVItem->mask & LVIF_IMAGE)
3237 if (lpSubItem->hdr.iImage != lpLVItem->iImage)
3239 lpSubItem->hdr.iImage = lpLVItem->iImage;
3240 *bChanged = TRUE;
3243 if (lpLVItem->mask & LVIF_TEXT)
3244 if (lpSubItem->hdr.pszText != lpLVItem->pszText)
3246 textsetptrT(&lpSubItem->hdr.pszText, lpLVItem->pszText, isW);
3247 *bChanged = TRUE;
3250 return TRUE;
3253 /***
3254 * DESCRIPTION:
3255 * Sets item attributes.
3257 * PARAMETER(S):
3258 * [I] infoPtr : valid pointer to the listview structure
3259 * [I] lpLVItem : new item atttributes
3260 * [I] isW : TRUE if lpLVItem is Unicode, FALSE if it's ANSI
3262 * RETURN:
3263 * SUCCESS : TRUE
3264 * FAILURE : FALSE
3266 static BOOL LISTVIEW_SetItemT(LISTVIEW_INFO *infoPtr, LPLVITEMW lpLVItem, BOOL isW)
3268 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
3269 LPWSTR pszText = NULL;
3270 BOOL bResult, bChanged = FALSE;
3272 TRACE("(lpLVItem=%s, isW=%d)\n", debuglvitem_t(lpLVItem, isW), isW);
3274 if (!lpLVItem || lpLVItem->iItem < 0 || lpLVItem->iItem >= infoPtr->nItemCount)
3275 return FALSE;
3277 /* For efficiency, we transform the lpLVItem->pszText to Unicode here */
3278 if ((lpLVItem->mask & LVIF_TEXT) && is_textW(lpLVItem->pszText))
3280 pszText = lpLVItem->pszText;
3281 lpLVItem->pszText = textdupTtoW(lpLVItem->pszText, isW);
3284 /* actually set the fields */
3285 if (!is_assignable_item(lpLVItem, infoPtr->dwStyle)) return FALSE;
3287 if (lpLVItem->iSubItem)
3288 bResult = set_sub_item(infoPtr, lpLVItem, TRUE, &bChanged);
3289 else
3290 bResult = set_main_item(infoPtr, lpLVItem, FALSE, TRUE, &bChanged);
3292 /* redraw item, if necessary */
3293 if (bChanged && !infoPtr->bIsDrawing)
3295 /* this little optimization eliminates some nasty flicker */
3296 if ( uView == LVS_REPORT && !(infoPtr->dwStyle & LVS_OWNERDRAWFIXED) &&
3297 (!(infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT) || lpLVItem->iSubItem) )
3298 LISTVIEW_InvalidateSubItem(infoPtr, lpLVItem->iItem, lpLVItem->iSubItem);
3299 else
3300 LISTVIEW_InvalidateItem(infoPtr, lpLVItem->iItem);
3302 /* restore text */
3303 if (pszText)
3305 textfreeT(lpLVItem->pszText, isW);
3306 lpLVItem->pszText = pszText;
3309 return bResult;
3312 /***
3313 * DESCRIPTION:
3314 * Retrieves the index of the item at coordinate (0, 0) of the client area.
3316 * PARAMETER(S):
3317 * [I] infoPtr : valid pointer to the listview structure
3319 * RETURN:
3320 * item index
3322 static INT LISTVIEW_GetTopIndex(LISTVIEW_INFO *infoPtr)
3324 LONG lStyle = infoPtr->dwStyle;
3325 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
3326 INT nItem = 0;
3327 SCROLLINFO scrollInfo;
3329 scrollInfo.cbSize = sizeof(SCROLLINFO);
3330 scrollInfo.fMask = SIF_POS;
3332 if (uView == LVS_LIST)
3334 if ((lStyle & WS_HSCROLL) && GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo))
3335 nItem = scrollInfo.nPos * LISTVIEW_GetCountPerColumn(infoPtr);
3337 else if (uView == LVS_REPORT)
3339 if ((lStyle & WS_VSCROLL) && GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo))
3340 nItem = scrollInfo.nPos;
3342 else
3344 if ((lStyle & WS_VSCROLL) && GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo))
3345 nItem = LISTVIEW_GetCountPerRow(infoPtr) * (scrollInfo.nPos / infoPtr->nItemHeight);
3348 TRACE("nItem=%d\n", nItem);
3350 return nItem;
3354 /***
3355 * DESCRIPTION:
3356 * Erases the background of the given rectangle
3358 * PARAMETER(S):
3359 * [I] infoPtr : valid pointer to the listview structure
3360 * [I] hdc : device context handle
3361 * [I] lprcBox : clipping rectangle
3363 * RETURN:
3364 * Success: TRUE
3365 * Failure: FALSE
3367 static inline BOOL LISTVIEW_FillBkgnd(LISTVIEW_INFO *infoPtr, HDC hdc, const RECT* lprcBox)
3369 if (!infoPtr->hBkBrush) return FALSE;
3371 TRACE("(hdc=%p, lprcBox=%s, hBkBrush=%p)\n", hdc, debugrect(lprcBox), infoPtr->hBkBrush);
3373 return FillRect(hdc, lprcBox, infoPtr->hBkBrush);
3376 /***
3377 * DESCRIPTION:
3378 * Draws an item.
3380 * PARAMETER(S):
3381 * [I] infoPtr : valid pointer to the listview structure
3382 * [I] hdc : device context handle
3383 * [I] nItem : item index
3384 * [I] nSubItem : subitem index
3385 * [I] pos : item position in client coordinates
3386 * [I] cdmode : custom draw mode
3388 * RETURN:
3389 * Success: TRUE
3390 * Failure: FALSE
3392 static BOOL LISTVIEW_DrawItem(LISTVIEW_INFO *infoPtr, HDC hdc, INT nItem, INT nSubItem, POINT pos, DWORD cdmode)
3394 UINT uFormat, uView = infoPtr->dwStyle & LVS_TYPEMASK;
3395 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
3396 WCHAR szCallback[] = { '(', 'c', 'a', 'l', 'l', 'b', 'a', 'c', 'k', ')', 0 };
3397 DWORD cditemmode = CDRF_DODEFAULT;
3398 RECT* lprcFocus, rcSelect, rcBox, rcState, rcIcon, rcLabel;
3399 NMLVCUSTOMDRAW nmlvcd;
3400 HIMAGELIST himl;
3401 LVITEMW lvItem;
3403 TRACE("(hdc=%p, nItem=%d, nSubItem=%d, pos=%s)\n", hdc, nItem, nSubItem, debugpoint(&pos));
3405 /* get information needed for drawing the item */
3406 lvItem.mask = LVIF_TEXT | LVIF_IMAGE;
3407 if (nSubItem == 0) lvItem.mask |= LVIF_STATE | LVIF_PARAM;
3408 if (uView == LVS_REPORT) lvItem.mask |= LVIF_INDENT;
3409 lvItem.stateMask = LVIS_SELECTED | LVIS_FOCUSED | LVIS_STATEIMAGEMASK;
3410 lvItem.iItem = nItem;
3411 lvItem.iSubItem = nSubItem;
3412 lvItem.state = 0;
3413 lvItem.lParam = 0;
3414 lvItem.cchTextMax = DISP_TEXT_SIZE;
3415 lvItem.pszText = szDispText;
3416 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) return FALSE;
3417 if (nSubItem > 0 && (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT))
3418 lvItem.state = LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED);
3419 if (lvItem.pszText == LPSTR_TEXTCALLBACKW) lvItem.pszText = szCallback;
3420 TRACE(" lvItem=%s\n", debuglvitem_t(&lvItem, TRUE));
3422 /* now check if we need to update the focus rectangle */
3423 lprcFocus = infoPtr->bFocus && (lvItem.state & LVIS_FOCUSED) ? &infoPtr->rcFocus : 0;
3425 if (!lprcFocus) lvItem.state &= ~LVIS_FOCUSED;
3426 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, &rcBox, &rcState, &rcIcon, &rcLabel);
3427 OffsetRect(&rcBox, pos.x, pos.y);
3428 OffsetRect(&rcState, pos.x, pos.y);
3429 OffsetRect(&rcIcon, pos.x, pos.y);
3430 OffsetRect(&rcLabel, pos.x, pos.y);
3431 TRACE(" rcBox=%s, rcState=%s, rcIcon=%s. rcLabel=%s\n",
3432 debugrect(&rcBox), debugrect(&rcState), debugrect(&rcIcon), debugrect(&rcLabel));
3434 /* fill in the custom draw structure */
3435 customdraw_fill(&nmlvcd, infoPtr, hdc, &rcBox);
3436 nmlvcd.nmcd.dwItemSpec = lvItem.iItem;
3437 nmlvcd.iSubItem = lvItem.iSubItem;
3438 if (lvItem.state & LVIS_SELECTED) nmlvcd.nmcd.uItemState |= CDIS_SELECTED;
3439 if (lvItem.state & LVIS_FOCUSED) nmlvcd.nmcd.uItemState |= CDIS_FOCUS;
3440 if (lvItem.iItem == infoPtr->nHotItem) nmlvcd.nmcd.uItemState |= CDIS_HOT;
3441 nmlvcd.nmcd.lItemlParam = lvItem.lParam;
3443 if (cdmode & CDRF_NOTIFYITEMDRAW)
3444 cditemmode = notify_customdraw (infoPtr, CDDS_ITEMPREPAINT, &nmlvcd);
3445 if (cditemmode & CDRF_SKIPDEFAULT) goto postpaint;
3447 /* apprently, for selected items, we have to override the returned values */
3448 if (lvItem.state & LVIS_SELECTED)
3450 if (infoPtr->bFocus)
3452 nmlvcd.clrTextBk = comctl32_color.clrHighlight;
3453 nmlvcd.clrText = comctl32_color.clrHighlightText;
3455 else if (infoPtr->dwStyle & LVS_SHOWSELALWAYS)
3457 nmlvcd.clrTextBk = comctl32_color.clr3dFace;
3458 nmlvcd.clrText = comctl32_color.clrBtnText;
3462 /* in full row select, subitems, will just use main item's colors */
3463 if (nSubItem && uView == LVS_REPORT && (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT))
3464 nmlvcd.clrTextBk = CLR_NONE;
3466 /* state icons */
3467 if (infoPtr->himlState && !IsRectEmpty(&rcState))
3469 UINT uStateImage = (lvItem.state & LVIS_STATEIMAGEMASK) >> 12;
3470 if (uStateImage)
3472 TRACE("uStateImage=%d\n", uStateImage);
3473 ImageList_Draw(infoPtr->himlState, uStateImage - 1, hdc, rcState.left, rcState.top, ILD_NORMAL);
3477 /* small icons */
3478 himl = (uView == LVS_ICON ? infoPtr->himlNormal : infoPtr->himlSmall);
3479 if (himl && lvItem.iImage >= 0 && !IsRectEmpty(&rcIcon))
3481 TRACE("iImage=%d\n", lvItem.iImage);
3482 ImageList_Draw(himl, lvItem.iImage, hdc, rcIcon.left, rcIcon.top,
3483 (lvItem.state & LVIS_SELECTED) && (infoPtr->bFocus) ? ILD_SELECTED : ILD_NORMAL);
3486 /* Don't bother painting item being edited */
3487 if (infoPtr->hwndEdit && lprcFocus && nSubItem == 0) goto postpaint;
3489 /* Set the text attributes */
3490 if (nmlvcd.clrTextBk != CLR_NONE)
3492 SetBkMode(hdc, OPAQUE);
3493 SetBkColor(hdc, nmlvcd.clrTextBk == CLR_DEFAULT ? infoPtr->clrTextBkDefault : nmlvcd.clrTextBk);
3495 else
3496 SetBkMode(hdc, TRANSPARENT);
3497 SetTextColor(hdc, nmlvcd.clrText);
3499 /* draw the selection background, if we're drawing the main item */
3500 if (nSubItem == 0)
3502 rcSelect = rcLabel;
3503 if (uView == LVS_REPORT && (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT))
3504 rcSelect.right = rcBox.right;
3506 if (nmlvcd.clrTextBk != CLR_NONE)
3507 ExtTextOutW(hdc, rcSelect.left, rcSelect.top, ETO_OPAQUE, &rcSelect, 0, 0, 0);
3508 if(lprcFocus) *lprcFocus = rcSelect;
3511 /* figure out the text drawing flags */
3512 uFormat = (uView == LVS_ICON ? (lprcFocus ? LV_FL_DT_FLAGS : LV_ML_DT_FLAGS) : LV_SL_DT_FLAGS);
3513 if (uView == LVS_ICON)
3514 uFormat = (lprcFocus ? LV_FL_DT_FLAGS : LV_ML_DT_FLAGS);
3515 else if (nSubItem)
3517 switch (LISTVIEW_GetColumnInfo(infoPtr, nSubItem)->fmt & LVCFMT_JUSTIFYMASK)
3519 case LVCFMT_RIGHT: uFormat |= DT_RIGHT; break;
3520 case LVCFMT_CENTER: uFormat |= DT_CENTER; break;
3521 default: uFormat |= DT_LEFT;
3524 if (!(uFormat & (DT_RIGHT | DT_CENTER))) rcLabel.left += 2;
3525 DrawTextW(hdc, lvItem.pszText, -1, &rcLabel, uFormat);
3527 postpaint:
3528 if (cditemmode & CDRF_NOTIFYPOSTPAINT)
3529 notify_customdraw(infoPtr, CDDS_ITEMPOSTPAINT, &nmlvcd);
3530 return TRUE;
3533 /***
3534 * DESCRIPTION:
3535 * Draws listview items when in owner draw mode.
3537 * PARAMETER(S):
3538 * [I] infoPtr : valid pointer to the listview structure
3539 * [I] hdc : device context handle
3541 * RETURN:
3542 * None
3544 static void LISTVIEW_RefreshOwnerDraw(LISTVIEW_INFO *infoPtr, HDC hdc)
3546 UINT uID = GetWindowLongW(infoPtr->hwndSelf, GWL_ID);
3547 HWND hwndParent = GetParent(infoPtr->hwndSelf);
3548 POINT Origin, Position;
3549 DRAWITEMSTRUCT dis;
3550 LVITEMW item;
3551 ITERATOR i;
3553 TRACE("()\n");
3555 ZeroMemory(&dis, sizeof(dis));
3557 /* Get scroll info once before loop */
3558 LISTVIEW_GetOrigin(infoPtr, &Origin);
3560 /* figure out what we need to draw */
3561 iterator_visibleitems(&i, infoPtr, hdc);
3563 /* send cache hint notification */
3564 if (infoPtr->dwStyle & LVS_OWNERDATA)
3566 RANGE range = iterator_range(&i);
3567 NMLVCACHEHINT nmlv;
3569 ZeroMemory(&nmlv, sizeof(NMLVCACHEHINT));
3570 nmlv.iFrom = range.lower;
3571 nmlv.iTo = range.upper - 1;
3572 notify_hdr(infoPtr, LVN_ODCACHEHINT, &nmlv.hdr);
3575 /* iterate through the invalidated rows */
3576 while(iterator_next(&i))
3578 item.iItem = i.nItem;
3579 item.iSubItem = 0;
3580 item.mask = LVIF_PARAM | LVIF_STATE;
3581 item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
3582 if (!LISTVIEW_GetItemW(infoPtr, &item)) continue;
3584 dis.CtlType = ODT_LISTVIEW;
3585 dis.CtlID = uID;
3586 dis.itemID = item.iItem;
3587 dis.itemAction = ODA_DRAWENTIRE;
3588 dis.itemState = 0;
3589 if (item.state & LVIS_SELECTED) dis.itemState |= ODS_SELECTED;
3590 if (infoPtr->bFocus && (item.state & LVIS_FOCUSED)) dis.itemState |= ODS_FOCUS;
3591 dis.hwndItem = infoPtr->hwndSelf;
3592 dis.hDC = hdc;
3593 LISTVIEW_GetItemOrigin(infoPtr, dis.itemID, &Position);
3594 dis.rcItem.left = Position.x + Origin.x;
3595 dis.rcItem.right = dis.rcItem.left + infoPtr->nItemWidth;
3596 dis.rcItem.top = Position.y + Origin.y;
3597 dis.rcItem.bottom = dis.rcItem.top + infoPtr->nItemHeight;
3598 dis.itemData = item.lParam;
3600 TRACE("item=%s, rcItem=%s\n", debuglvitem_t(&item, TRUE), debugrect(&dis.rcItem));
3601 SendMessageW(hwndParent, WM_DRAWITEM, dis.CtlID, (LPARAM)&dis);
3603 iterator_destroy(&i);
3606 /***
3607 * DESCRIPTION:
3608 * Draws listview items when in report display mode.
3610 * PARAMETER(S):
3611 * [I] infoPtr : valid pointer to the listview structure
3612 * [I] hdc : device context handle
3613 * [I] cdmode : custom draw mode
3615 * RETURN:
3616 * None
3618 static void LISTVIEW_RefreshReport(LISTVIEW_INFO *infoPtr, HDC hdc, DWORD cdmode)
3620 INT rgntype;
3621 RECT rcClip, rcItem;
3622 POINT Origin, Position;
3623 RANGE colRange;
3624 ITERATOR i, j;
3626 TRACE("()\n");
3628 /* figure out what to draw */
3629 rgntype = GetClipBox(hdc, &rcClip);
3630 if (rgntype == NULLREGION) return;
3632 /* Get scroll info once before loop */
3633 LISTVIEW_GetOrigin(infoPtr, &Origin);
3635 /* narrow down the columns we need to paint */
3636 for(colRange.lower = 0; colRange.lower < infoPtr->hdpaColumns->nItemCount; colRange.lower++)
3638 LISTVIEW_GetHeaderRect(infoPtr, colRange.lower, &rcItem);
3639 if (rcItem.right + Origin.x >= rcClip.left) break;
3641 for(colRange.upper = infoPtr->hdpaColumns->nItemCount; colRange.upper > 0; colRange.upper--)
3643 LISTVIEW_GetHeaderRect(infoPtr, colRange.upper - 1, &rcItem);
3644 if (rcItem.left + Origin.x < rcClip.right) break;
3646 iterator_rangeitems(&j, colRange);
3648 /* figure out what we need to draw */
3649 iterator_visibleitems(&i, infoPtr, hdc);
3651 /* iterate through the invalidated rows */
3652 while(iterator_next(&i))
3654 /* iterate through the invalidated columns */
3655 while(iterator_next(&j))
3657 LISTVIEW_GetItemOrigin(infoPtr, i.nItem, &Position);
3658 Position.x += Origin.x;
3659 Position.y += Origin.y;
3661 if (rgntype == COMPLEXREGION)
3663 LISTVIEW_GetHeaderRect(infoPtr, j.nItem, &rcItem);
3664 rcItem.top = 0;
3665 rcItem.bottom = infoPtr->nItemHeight;
3666 OffsetRect(&rcItem, Position.x, Position.y);
3667 if (!RectVisible(hdc, &rcItem)) continue;
3670 LISTVIEW_DrawItem(infoPtr, hdc, i.nItem, j.nItem, Position, cdmode);
3673 iterator_destroy(&i);
3676 /***
3677 * DESCRIPTION:
3678 * Draws listview items when in list display mode.
3680 * PARAMETER(S):
3681 * [I] infoPtr : valid pointer to the listview structure
3682 * [I] hdc : device context handle
3683 * [I] cdmode : custom draw mode
3685 * RETURN:
3686 * None
3688 static void LISTVIEW_RefreshList(LISTVIEW_INFO *infoPtr, HDC hdc, DWORD cdmode)
3690 POINT Origin, Position;
3691 ITERATOR i;
3693 /* Get scroll info once before loop */
3694 LISTVIEW_GetOrigin(infoPtr, &Origin);
3696 /* figure out what we need to draw */
3697 iterator_visibleitems(&i, infoPtr, hdc);
3699 while(iterator_prev(&i))
3701 LISTVIEW_GetItemOrigin(infoPtr, i.nItem, &Position);
3702 Position.x += Origin.x;
3703 Position.y += Origin.y;
3705 LISTVIEW_DrawItem(infoPtr, hdc, i.nItem, 0, Position, cdmode);
3707 iterator_destroy(&i);
3711 /***
3712 * DESCRIPTION:
3713 * Draws listview items.
3715 * PARAMETER(S):
3716 * [I] infoPtr : valid pointer to the listview structure
3717 * [I] hdc : device context handle
3719 * RETURN:
3720 * NoneX
3722 static void LISTVIEW_Refresh(LISTVIEW_INFO *infoPtr, HDC hdc)
3724 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
3725 COLORREF oldTextColor, oldClrTextBk, oldClrText;
3726 NMLVCUSTOMDRAW nmlvcd;
3727 HFONT hOldFont;
3728 DWORD cdmode;
3729 INT oldBkMode;
3730 RECT rcClient;
3732 LISTVIEW_DUMP(infoPtr);
3734 infoPtr->bIsDrawing = TRUE;
3736 /* save dc values we're gonna trash while drawing */
3737 hOldFont = SelectObject(hdc, infoPtr->hFont);
3738 oldBkMode = GetBkMode(hdc);
3739 infoPtr->clrTextBkDefault = GetBkColor(hdc);
3740 oldTextColor = GetTextColor(hdc);
3742 oldClrTextBk = infoPtr->clrTextBk;
3743 oldClrText = infoPtr->clrText;
3745 GetClientRect(infoPtr->hwndSelf, &rcClient);
3746 customdraw_fill(&nmlvcd, infoPtr, hdc, &rcClient);
3747 cdmode = notify_customdraw(infoPtr, CDDS_PREPAINT, &nmlvcd);
3748 if (cdmode & CDRF_SKIPDEFAULT) goto enddraw;
3750 /* Use these colors to draw the items */
3751 infoPtr->clrTextBk = nmlvcd.clrTextBk;
3752 infoPtr->clrText = nmlvcd.clrText;
3754 /* nothing to draw */
3755 if(infoPtr->nItemCount == 0) goto enddraw;
3757 if ((infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && (uView == LVS_REPORT))
3758 LISTVIEW_RefreshOwnerDraw(infoPtr, hdc);
3759 else
3761 if (uView == LVS_REPORT)
3762 LISTVIEW_RefreshReport(infoPtr, hdc, cdmode);
3763 else /* LVS_LIST, LVS_ICON or LVS_SMALLICON */
3764 LISTVIEW_RefreshList(infoPtr, hdc, cdmode);
3766 /* if we have a focus rect, draw it */
3767 if (infoPtr->bFocus)
3768 DrawFocusRect(hdc, &infoPtr->rcFocus);
3771 enddraw:
3772 if (cdmode & CDRF_NOTIFYPOSTPAINT)
3773 notify_customdraw(infoPtr, CDDS_POSTPAINT, &nmlvcd);
3775 infoPtr->clrTextBk = oldClrTextBk;
3776 infoPtr->clrText = oldClrText;
3778 SelectObject(hdc, hOldFont);
3779 SetBkMode(hdc, oldBkMode);
3780 SetBkColor(hdc, infoPtr->clrTextBkDefault);
3781 SetTextColor(hdc, oldTextColor);
3782 infoPtr->bIsDrawing = FALSE;
3786 /***
3787 * DESCRIPTION:
3788 * Calculates the approximate width and height of a given number of items.
3790 * PARAMETER(S):
3791 * [I] infoPtr : valid pointer to the listview structure
3792 * [I] nItemCount : number of items
3793 * [I] wWidth : width
3794 * [I] wHeight : height
3796 * RETURN:
3797 * Returns a DWORD. The width in the low word and the height in high word.
3799 static DWORD LISTVIEW_ApproximateViewRect(LISTVIEW_INFO *infoPtr, INT nItemCount,
3800 WORD wWidth, WORD wHeight)
3802 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
3803 INT nItemCountPerColumn = 1;
3804 INT nColumnCount = 0;
3805 DWORD dwViewRect = 0;
3807 if (nItemCount == -1)
3808 nItemCount = infoPtr->nItemCount;
3810 if (uView == LVS_LIST)
3812 if (wHeight == 0xFFFF)
3814 /* use current height */
3815 wHeight = infoPtr->rcList.bottom - infoPtr->rcList.top;
3818 if (wHeight < infoPtr->nItemHeight)
3819 wHeight = infoPtr->nItemHeight;
3821 if (nItemCount > 0)
3823 if (infoPtr->nItemHeight > 0)
3825 nItemCountPerColumn = wHeight / infoPtr->nItemHeight;
3826 if (nItemCountPerColumn == 0)
3827 nItemCountPerColumn = 1;
3829 if (nItemCount % nItemCountPerColumn != 0)
3830 nColumnCount = nItemCount / nItemCountPerColumn;
3831 else
3832 nColumnCount = nItemCount / nItemCountPerColumn + 1;
3836 /* Microsoft padding magic */
3837 wHeight = nItemCountPerColumn * infoPtr->nItemHeight + 2;
3838 wWidth = nColumnCount * infoPtr->nItemWidth + 2;
3840 dwViewRect = MAKELONG(wWidth, wHeight);
3842 else if (uView == LVS_REPORT)
3843 FIXME("uView == LVS_REPORT: not implemented\n");
3844 else if (uView == LVS_SMALLICON)
3845 FIXME("uView == LVS_SMALLICON: not implemented\n");
3846 else if (uView == LVS_ICON)
3847 FIXME("uView == LVS_ICON: not implemented\n");
3849 return dwViewRect;
3852 /* << LISTVIEW_CreateDragImage >> */
3855 /***
3856 * DESCRIPTION:
3857 * Removes all listview items and subitems.
3859 * PARAMETER(S):
3860 * [I] infoPtr : valid pointer to the listview structure
3862 * RETURN:
3863 * SUCCESS : TRUE
3864 * FAILURE : FALSE
3866 static BOOL LISTVIEW_DeleteAllItems(LISTVIEW_INFO *infoPtr)
3868 NMLISTVIEW nmlv;
3869 HDPA hdpaSubItems = NULL;
3870 BOOL bSuppress;
3871 ITEMHDR *hdrItem;
3872 INT i, j;
3874 TRACE("()\n");
3876 /* we do it directly, to avoid notifications */
3877 ranges_clear(infoPtr->selectionRanges);
3878 infoPtr->nSelectionMark = -1;
3879 infoPtr->nFocusedItem = -1;
3880 SetRectEmpty(&infoPtr->rcFocus);
3881 /* But we are supposed to leave nHotItem as is! */
3884 /* send LVN_DELETEALLITEMS notification */
3885 ZeroMemory(&nmlv, sizeof(NMLISTVIEW));
3886 nmlv.iItem = -1;
3887 bSuppress = notify_listview(infoPtr, LVN_DELETEALLITEMS, &nmlv);
3889 for (i = infoPtr->nItemCount - 1; i >= 0; i--)
3891 /* send LVN_DELETEITEM notification, if not supressed */
3892 if (!bSuppress)
3894 nmlv.iItem = i;
3895 notify_listview(infoPtr, LVN_DELETEITEM, &nmlv);
3897 if (!(infoPtr->dwStyle & LVS_OWNERDATA))
3899 hdpaSubItems = (HDPA)DPA_GetPtr(infoPtr->hdpaItems, i);
3900 for (j = 0; j < hdpaSubItems->nItemCount; j++)
3902 hdrItem = (ITEMHDR *)DPA_GetPtr(hdpaSubItems, j);
3903 if (is_textW(hdrItem->pszText)) COMCTL32_Free(hdrItem->pszText);
3904 COMCTL32_Free(hdrItem);
3906 DPA_Destroy(hdpaSubItems);
3907 DPA_DeletePtr(infoPtr->hdpaItems, i);
3909 DPA_DeletePtr(infoPtr->hdpaPosX, i);
3910 DPA_DeletePtr(infoPtr->hdpaPosY, i);
3911 infoPtr->nItemCount --;
3914 LISTVIEW_UpdateScroll(infoPtr);
3916 LISTVIEW_InvalidateList(infoPtr);
3918 return TRUE;
3921 /***
3922 * DESCRIPTION:
3923 * Scrolls, and updates the columns, when a column is changing width.
3925 * PARAMETER(S):
3926 * [I] infoPtr : valid pointer to the listview structure
3927 * [I] nColumn : column to scroll
3928 * [I] dx : amount of scroll, in pixels
3930 * RETURN:
3931 * None.
3933 static void LISTVIEW_ScrollColumns(LISTVIEW_INFO *infoPtr, INT nColumn, INT dx)
3935 COLUMN_INFO *lpColumnInfo;
3936 RECT rcOld, rcCol;
3937 INT nCol;
3939 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, min(nColumn, infoPtr->hdpaColumns->nItemCount - 1));
3940 rcCol = lpColumnInfo->rcHeader;
3941 if (nColumn >= infoPtr->hdpaColumns->nItemCount)
3942 rcCol.left = rcCol.right;
3944 /* ajust the other columns */
3945 for (nCol = nColumn; nCol < infoPtr->hdpaColumns->nItemCount; nCol++)
3947 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, nCol);
3948 lpColumnInfo->rcHeader.left += dx;
3949 lpColumnInfo->rcHeader.right += dx;
3952 /* do not update screen if not in report mode */
3953 if (!is_redrawing(infoPtr) || (infoPtr->dwStyle & LVS_TYPEMASK) != LVS_REPORT) return;
3955 /* if we have a focus, must first erase the focus rect */
3956 if (infoPtr->bFocus) LISTVIEW_ShowFocusRect(infoPtr, FALSE);
3958 /* Need to reset the item width when inserting a new column */
3959 infoPtr->nItemWidth += dx;
3961 LISTVIEW_UpdateScroll(infoPtr);
3963 /* scroll to cover the deleted column, and invalidate for redraw */
3964 rcOld = infoPtr->rcList;
3965 rcOld.left = rcCol.left;
3966 ScrollWindowEx(infoPtr->hwndSelf, dx, 0, &rcOld, &rcOld, 0, 0, SW_ERASE | SW_INVALIDATE);
3968 /* we can restore focus now */
3969 if (infoPtr->bFocus) LISTVIEW_ShowFocusRect(infoPtr, TRUE);
3972 /***
3973 * DESCRIPTION:
3974 * Removes a column from the listview control.
3976 * PARAMETER(S):
3977 * [I] infoPtr : valid pointer to the listview structure
3978 * [I] nColumn : column index
3980 * RETURN:
3981 * SUCCESS : TRUE
3982 * FAILURE : FALSE
3984 static BOOL LISTVIEW_DeleteColumn(LISTVIEW_INFO *infoPtr, INT nColumn)
3986 RECT rcCol;
3988 TRACE("nColumn=%d\n", nColumn);
3990 if (nColumn <= 0 || nColumn >= infoPtr->hdpaColumns->nItemCount) return FALSE;
3992 LISTVIEW_GetHeaderRect(infoPtr, nColumn, &rcCol);
3994 if (!Header_DeleteItem(infoPtr->hwndHeader, nColumn))
3995 return FALSE;
3997 COMCTL32_Free(DPA_GetPtr(infoPtr->hdpaColumns, nColumn));
3998 DPA_DeletePtr(infoPtr->hdpaColumns, nColumn);
4000 if (!(infoPtr->dwStyle & LVS_OWNERDATA))
4002 SUBITEM_INFO *lpSubItem, *lpDelItem;
4003 HDPA hdpaSubItems;
4004 INT nItem, nSubItem, i;
4006 for (nItem = 0; nItem < infoPtr->nItemCount; nItem++)
4008 hdpaSubItems = (HDPA)DPA_GetPtr(infoPtr->hdpaItems, nItem);
4009 nSubItem = 0;
4010 lpDelItem = 0;
4011 for (i = 1; i < hdpaSubItems->nItemCount; i++)
4013 lpSubItem = (SUBITEM_INFO *)DPA_GetPtr(hdpaSubItems, i);
4014 if (lpSubItem->iSubItem == nColumn)
4016 nSubItem = i;
4017 lpDelItem = lpSubItem;
4019 else if (lpSubItem->iSubItem > nColumn)
4021 lpSubItem->iSubItem--;
4025 /* if we found our subitem, zapp it */
4026 if (nSubItem > 0)
4028 /* free string */
4029 if (is_textW(lpDelItem->hdr.pszText))
4030 COMCTL32_Free(lpDelItem->hdr.pszText);
4032 /* free item */
4033 COMCTL32_Free(lpDelItem);
4035 /* free dpa memory */
4036 DPA_DeletePtr(hdpaSubItems, nSubItem);
4041 /* update the other column info */
4042 LISTVIEW_ScrollColumns(infoPtr, nColumn, -(rcCol.right - rcCol.left));
4044 return TRUE;
4047 /***
4048 * DESCRIPTION:
4049 * Invalidates the listview after an item's insertion or deletion.
4051 * PARAMETER(S):
4052 * [I] infoPtr : valid pointer to the listview structure
4053 * [I] nItem : item index
4054 * [I] dir : -1 if deleting, 1 if inserting
4056 * RETURN:
4057 * None
4059 static void LISTVIEW_ScrollOnInsert(LISTVIEW_INFO *infoPtr, INT nItem, INT dir)
4061 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
4062 INT nPerCol, nItemCol, nItemRow;
4063 RECT rcScroll;
4064 POINT Origin;
4066 /* if we don't refresh, what's the point of scrolling? */
4067 if (!is_redrawing(infoPtr)) return;
4069 assert (abs(dir) == 1);
4071 /* arrange icons if autoarrange is on */
4072 if (is_autoarrange(infoPtr))
4074 BOOL arrange = TRUE;
4075 if (dir < 0 && nItem >= infoPtr->nItemCount) arrange = FALSE;
4076 if (dir > 0 && nItem == infoPtr->nItemCount - 1) arrange = FALSE;
4077 if (arrange) LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
4080 /* scrollbars need updating */
4081 LISTVIEW_UpdateScroll(infoPtr);
4083 /* figure out the item's position */
4084 if (uView == LVS_REPORT)
4085 nPerCol = infoPtr->nItemCount + 1;
4086 else if (uView == LVS_LIST)
4087 nPerCol = LISTVIEW_GetCountPerColumn(infoPtr);
4088 else /* LVS_ICON, or LVS_SMALLICON */
4089 return;
4091 nItemCol = nItem / nPerCol;
4092 nItemRow = nItem % nPerCol;
4093 LISTVIEW_GetOrigin(infoPtr, &Origin);
4095 /* move the items below up a slot */
4096 rcScroll.left = nItemCol * infoPtr->nItemWidth;
4097 rcScroll.top = nItemRow * infoPtr->nItemHeight;
4098 rcScroll.right = rcScroll.left + infoPtr->nItemWidth;
4099 rcScroll.bottom = nPerCol * infoPtr->nItemHeight;
4100 OffsetRect(&rcScroll, Origin.x, Origin.y);
4101 if (IntersectRect(&rcScroll, &rcScroll, &infoPtr->rcList))
4102 ScrollWindowEx(infoPtr->hwndSelf, 0, dir * infoPtr->nItemHeight,
4103 &rcScroll, &rcScroll, 0, 0, SW_ERASE | SW_INVALIDATE);
4105 /* report has only that column, so we're done */
4106 if (uView == LVS_REPORT) return;
4108 /* now for LISTs, we have to deal with the columns to the right */
4109 rcScroll.left = (nItemCol + 1) * infoPtr->nItemWidth;
4110 rcScroll.top = 0;
4111 rcScroll.right = (infoPtr->nItemCount / nPerCol + 1) * infoPtr->nItemWidth;
4112 rcScroll.bottom = nPerCol * infoPtr->nItemHeight;
4113 OffsetRect(&rcScroll, Origin.x, Origin.y);
4114 if (IntersectRect(&rcScroll, &rcScroll, &infoPtr->rcList))
4115 ScrollWindowEx(infoPtr->hwndSelf, 0, dir * infoPtr->nItemHeight,
4116 &rcScroll, &rcScroll, 0, 0, SW_ERASE | SW_INVALIDATE);
4119 /***
4120 * DESCRIPTION:
4121 * Removes an item from the listview control.
4123 * PARAMETER(S):
4124 * [I] infoPtr : valid pointer to the listview structure
4125 * [I] nItem : item index
4127 * RETURN:
4128 * SUCCESS : TRUE
4129 * FAILURE : FALSE
4131 static BOOL LISTVIEW_DeleteItem(LISTVIEW_INFO *infoPtr, INT nItem)
4133 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
4134 NMLISTVIEW nmlv;
4135 LVITEMW item;
4137 TRACE("(nItem=%d)\n", nItem);
4139 if (nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
4141 /* remove selection, and focus */
4142 item.state = 0;
4143 item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
4144 LISTVIEW_SetItemState(infoPtr, nItem, &item);
4146 /* send LVN_DELETEITEM notification. */
4147 ZeroMemory(&nmlv, sizeof (NMLISTVIEW));
4148 nmlv.iItem = nItem;
4149 notify_listview(infoPtr, LVN_DELETEITEM, &nmlv);
4151 /* we need to do this here, because we'll be deleting stuff */
4152 if (uView == LVS_SMALLICON || uView == LVS_ICON)
4153 LISTVIEW_InvalidateItem(infoPtr, nItem);
4155 if (!(infoPtr->dwStyle & LVS_OWNERDATA))
4157 HDPA hdpaSubItems;
4158 ITEMHDR *hdrItem;
4159 INT i;
4161 hdpaSubItems = (HDPA)DPA_DeletePtr(infoPtr->hdpaItems, nItem);
4162 for (i = 0; i < hdpaSubItems->nItemCount; i++)
4164 hdrItem = (ITEMHDR *)DPA_GetPtr(hdpaSubItems, i);
4165 if (is_textW(hdrItem->pszText)) COMCTL32_Free(hdrItem->pszText);
4166 COMCTL32_Free(hdrItem);
4168 DPA_Destroy(hdpaSubItems);
4171 if (uView == LVS_SMALLICON || uView == LVS_ICON)
4173 DPA_DeletePtr(infoPtr->hdpaPosX, nItem);
4174 DPA_DeletePtr(infoPtr->hdpaPosY, nItem);
4177 infoPtr->nItemCount--;
4178 LISTVIEW_ShiftIndices(infoPtr, nItem, -1);
4180 /* now is the invalidation fun */
4181 LISTVIEW_ScrollOnInsert(infoPtr, nItem, -1);
4182 return TRUE;
4186 /***
4187 * DESCRIPTION:
4188 * Callback implementation for editlabel control
4190 * PARAMETER(S):
4191 * [I] infoPtr : valid pointer to the listview structure
4192 * [I] pszText : modified text
4193 * [I] isW : TRUE if psxText is Unicode, FALSE if it's ANSI
4195 * RETURN:
4196 * SUCCESS : TRUE
4197 * FAILURE : FALSE
4199 static BOOL LISTVIEW_EndEditLabelT(LISTVIEW_INFO *infoPtr, LPWSTR pszText, BOOL isW)
4201 NMLVDISPINFOW dispInfo;
4203 TRACE("(pszText=%s, isW=%d)\n", debugtext_t(pszText, isW), isW);
4205 ZeroMemory(&dispInfo, sizeof(dispInfo));
4206 dispInfo.item.mask = LVIF_PARAM | LVIF_STATE;
4207 dispInfo.item.iItem = infoPtr->nEditLabelItem;
4208 dispInfo.item.iSubItem = 0;
4209 dispInfo.item.stateMask = ~0;
4210 if (!LISTVIEW_GetItemW(infoPtr, &dispInfo.item)) return FALSE;
4211 /* add the text from the edit in */
4212 dispInfo.item.mask |= LVIF_TEXT;
4213 dispInfo.item.pszText = pszText;
4214 dispInfo.item.cchTextMax = textlenT(pszText, isW);
4216 /* Do we need to update the Item Text */
4217 if (!notify_dispinfoT(infoPtr, LVN_ENDLABELEDITW, &dispInfo, isW)) return FALSE;
4218 if (!pszText) return TRUE;
4220 ZeroMemory(&dispInfo, sizeof(dispInfo));
4221 dispInfo.item.mask = LVIF_TEXT;
4222 dispInfo.item.iItem = infoPtr->nEditLabelItem;
4223 dispInfo.item.iSubItem = 0;
4224 dispInfo.item.pszText = pszText;
4225 dispInfo.item.cchTextMax = textlenT(pszText, isW);
4226 return LISTVIEW_SetItemT(infoPtr, &dispInfo.item, isW);
4229 /***
4230 * DESCRIPTION:
4231 * Begin in place editing of specified list view item
4233 * PARAMETER(S):
4234 * [I] infoPtr : valid pointer to the listview structure
4235 * [I] nItem : item index
4236 * [I] isW : TRUE if it's a Unicode req, FALSE if ASCII
4238 * RETURN:
4239 * SUCCESS : TRUE
4240 * FAILURE : FALSE
4242 static HWND LISTVIEW_EditLabelT(LISTVIEW_INFO *infoPtr, INT nItem, BOOL isW)
4244 WCHAR szDispText[DISP_TEXT_SIZE] = { 0 };
4245 NMLVDISPINFOW dispInfo;
4246 RECT rect;
4248 TRACE("(nItem=%d, isW=%d)\n", nItem, isW);
4250 if (~infoPtr->dwStyle & LVS_EDITLABELS) return 0;
4251 if (nItem < 0 || nItem >= infoPtr->nItemCount) return 0;
4253 infoPtr->nEditLabelItem = nItem;
4255 /* Is the EditBox still there, if so remove it */
4256 if(infoPtr->hwndEdit != 0)
4258 SetFocus(infoPtr->hwndSelf);
4259 infoPtr->hwndEdit = 0;
4262 LISTVIEW_SetSelection(infoPtr, nItem);
4263 LISTVIEW_SetItemFocus(infoPtr, nItem);
4265 rect.left = LVIR_LABEL;
4266 if (!LISTVIEW_GetItemRect(infoPtr, nItem, &rect)) return 0;
4268 ZeroMemory(&dispInfo, sizeof(dispInfo));
4269 dispInfo.item.mask = LVIF_PARAM | LVIF_STATE | LVIF_TEXT;
4270 dispInfo.item.iItem = nItem;
4271 dispInfo.item.iSubItem = 0;
4272 dispInfo.item.stateMask = ~0;
4273 dispInfo.item.pszText = szDispText;
4274 dispInfo.item.cchTextMax = DISP_TEXT_SIZE;
4275 if (!LISTVIEW_GetItemT(infoPtr, &dispInfo.item, isW)) return 0;
4277 infoPtr->hwndEdit = CreateEditLabelT(infoPtr, dispInfo.item.pszText, WS_VISIBLE,
4278 rect.left-2, rect.top-1, 0, rect.bottom - rect.top+2, isW);
4279 if (!infoPtr->hwndEdit) return 0;
4281 if (notify_dispinfoT(infoPtr, LVN_BEGINLABELEDITW, &dispInfo, isW))
4283 SendMessageW(infoPtr->hwndEdit, WM_CLOSE, 0, 0);
4284 infoPtr->hwndEdit = 0;
4285 return 0;
4288 ShowWindow(infoPtr->hwndEdit, SW_NORMAL);
4289 SetFocus(infoPtr->hwndEdit);
4290 SendMessageW(infoPtr->hwndEdit, EM_SETSEL, 0, -1);
4291 return infoPtr->hwndEdit;
4295 /***
4296 * DESCRIPTION:
4297 * Ensures the specified item is visible, scrolling into view if necessary.
4299 * PARAMETER(S):
4300 * [I] infoPtr : valid pointer to the listview structure
4301 * [I] nItem : item index
4302 * [I] bPartial : partially or entirely visible
4304 * RETURN:
4305 * SUCCESS : TRUE
4306 * FAILURE : FALSE
4308 static BOOL LISTVIEW_EnsureVisible(LISTVIEW_INFO *infoPtr, INT nItem, BOOL bPartial)
4310 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
4311 INT nScrollPosHeight = 0;
4312 INT nScrollPosWidth = 0;
4313 INT nHorzAdjust = 0;
4314 INT nVertAdjust = 0;
4315 INT nHorzDiff = 0;
4316 INT nVertDiff = 0;
4317 RECT rcItem, rcTemp;
4319 rcItem.left = LVIR_BOUNDS;
4320 if (!LISTVIEW_GetItemRect(infoPtr, nItem, &rcItem)) return FALSE;
4322 if (bPartial && IntersectRect(&rcTemp, &infoPtr->rcList, &rcItem)) return TRUE;
4324 if (rcItem.left < infoPtr->rcList.left || rcItem.right > infoPtr->rcList.right)
4326 /* scroll left/right, but in LVS_REPORT mode */
4327 if (uView == LVS_LIST)
4328 nScrollPosWidth = infoPtr->nItemWidth;
4329 else if ((uView == LVS_SMALLICON) || (uView == LVS_ICON))
4330 nScrollPosWidth = 1;
4332 if (rcItem.left < infoPtr->rcList.left)
4334 nHorzAdjust = -1;
4335 if (uView != LVS_REPORT) nHorzDiff = rcItem.left - infoPtr->rcList.left;
4337 else
4339 nHorzAdjust = 1;
4340 if (uView != LVS_REPORT) nHorzDiff = rcItem.right - infoPtr->rcList.right;
4344 if (rcItem.top < infoPtr->rcList.top || rcItem.bottom > infoPtr->rcList.bottom)
4346 /* scroll up/down, but not in LVS_LIST mode */
4347 if (uView == LVS_REPORT)
4348 nScrollPosHeight = infoPtr->nItemHeight;
4349 else if ((uView == LVS_ICON) || (uView == LVS_SMALLICON))
4350 nScrollPosHeight = 1;
4352 if (rcItem.top < infoPtr->rcList.top)
4354 nVertAdjust = -1;
4355 if (uView != LVS_LIST) nVertDiff = rcItem.top - infoPtr->rcList.top;
4357 else
4359 nVertAdjust = 1;
4360 if (uView != LVS_LIST) nVertDiff = rcItem.bottom - infoPtr->rcList.bottom;
4364 if (!nScrollPosWidth && !nScrollPosHeight) return TRUE;
4366 if (nScrollPosWidth)
4368 INT diff = nHorzDiff / nScrollPosWidth;
4369 if (nHorzDiff % nScrollPosWidth) diff += nHorzAdjust;
4370 LISTVIEW_HScroll(infoPtr, SB_INTERNAL, diff, 0);
4373 if (nScrollPosHeight)
4375 INT diff = nVertDiff / nScrollPosHeight;
4376 if (nVertDiff % nScrollPosHeight) diff += nVertAdjust;
4377 LISTVIEW_VScroll(infoPtr, SB_INTERNAL, diff, 0);
4380 return TRUE;
4383 /***
4384 * DESCRIPTION:
4385 * Searches for an item with specific characteristics.
4387 * PARAMETER(S):
4388 * [I] hwnd : window handle
4389 * [I] nStart : base item index
4390 * [I] lpFindInfo : item information to look for
4392 * RETURN:
4393 * SUCCESS : index of item
4394 * FAILURE : -1
4396 static INT LISTVIEW_FindItemW(LISTVIEW_INFO *infoPtr, INT nStart,
4397 LPLVFINDINFOW lpFindInfo)
4399 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
4400 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
4401 BOOL bWrap = FALSE, bNearest = FALSE;
4402 INT nItem = nStart + 1, nLast = infoPtr->nItemCount, nNearestItem = -1;
4403 ULONG xdist, ydist, dist, mindist = 0x7fffffff;
4404 POINT Position, Destination;
4405 LVITEMW lvItem;
4407 if (!lpFindInfo || nItem < 0) return -1;
4409 lvItem.mask = 0;
4410 if (lpFindInfo->flags & (LVFI_STRING | LVFI_PARTIAL))
4412 lvItem.mask |= LVIF_TEXT;
4413 lvItem.pszText = szDispText;
4414 lvItem.cchTextMax = DISP_TEXT_SIZE;
4417 if (lpFindInfo->flags & LVFI_WRAP)
4418 bWrap = TRUE;
4420 if ((lpFindInfo->flags & LVFI_NEARESTXY) &&
4421 (uView == LVS_ICON || uView ==LVS_SMALLICON))
4423 POINT Origin;
4424 RECT rcArea;
4426 LISTVIEW_GetOrigin(infoPtr, &Origin);
4427 Destination.x = lpFindInfo->pt.x - Origin.x;
4428 Destination.y = lpFindInfo->pt.y - Origin.y;
4429 switch(lpFindInfo->vkDirection)
4431 case VK_DOWN: Destination.y += infoPtr->nItemHeight; break;
4432 case VK_UP: Destination.y -= infoPtr->nItemHeight; break;
4433 case VK_RIGHT: Destination.x += infoPtr->nItemWidth; break;
4434 case VK_LEFT: Destination.x -= infoPtr->nItemWidth; break;
4435 case VK_HOME: Destination.x = Destination.y = 0; break;
4436 case VK_NEXT: Destination.y += infoPtr->rcList.bottom - infoPtr->rcList.top; break;
4437 case VK_PRIOR: Destination.y -= infoPtr->rcList.bottom - infoPtr->rcList.top; break;
4438 case VK_END:
4439 LISTVIEW_GetAreaRect(infoPtr, &rcArea);
4440 Destination.x = rcArea.right;
4441 Destination.y = rcArea.bottom;
4442 break;
4443 default: ERR("Unknown vkDirection=%d\n", lpFindInfo->vkDirection);
4445 bNearest = TRUE;
4448 /* if LVFI_PARAM is specified, all other flags are ignored */
4449 if (lpFindInfo->flags & LVFI_PARAM)
4451 lvItem.mask |= LVIF_PARAM;
4452 bNearest = FALSE;
4453 lvItem.mask &= ~LVIF_TEXT;
4456 again:
4457 for (; nItem < nLast; nItem++)
4459 lvItem.iItem = nItem;
4460 lvItem.iSubItem = 0;
4461 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) continue;
4463 if (lvItem.mask & LVIF_PARAM)
4465 if (lpFindInfo->lParam == lvItem.lParam)
4466 return nItem;
4467 else
4468 continue;
4471 if (lvItem.mask & LVIF_TEXT)
4473 if (lpFindInfo->flags & LVFI_PARTIAL)
4475 if (strstrW(lvItem.pszText, lpFindInfo->psz) == NULL) continue;
4477 else
4479 if (lstrcmpW(lvItem.pszText, lpFindInfo->psz) != 0) continue;
4483 if (!bNearest) return nItem;
4485 /* This is very inefficient. To do a good job here,
4486 * we need a sorted array of (x,y) item positions */
4487 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position);
4489 /* compute the distance^2 to the destination */
4490 xdist = Destination.x - Position.x;
4491 ydist = Destination.y - Position.y;
4492 dist = xdist * xdist + ydist * ydist;
4494 /* remember the distance, and item if it's closer */
4495 if (dist < mindist)
4497 mindist = dist;
4498 nNearestItem = nItem;
4502 if (bWrap)
4504 nItem = 0;
4505 nLast = min(nStart + 1, infoPtr->nItemCount);
4506 bWrap = FALSE;
4507 goto again;
4510 return nNearestItem;
4513 /***
4514 * DESCRIPTION:
4515 * Searches for an item with specific characteristics.
4517 * PARAMETER(S):
4518 * [I] hwnd : window handle
4519 * [I] nStart : base item index
4520 * [I] lpFindInfo : item information to look for
4522 * RETURN:
4523 * SUCCESS : index of item
4524 * FAILURE : -1
4526 static INT LISTVIEW_FindItemA(LISTVIEW_INFO *infoPtr, INT nStart,
4527 LPLVFINDINFOA lpFindInfo)
4529 BOOL hasText = lpFindInfo->flags & (LVFI_STRING | LVFI_PARTIAL);
4530 LVFINDINFOW fiw;
4531 INT res;
4533 memcpy(&fiw, lpFindInfo, sizeof(fiw));
4534 if (hasText) fiw.psz = textdupTtoW((LPCWSTR)lpFindInfo->psz, FALSE);
4535 res = LISTVIEW_FindItemW(infoPtr, nStart, &fiw);
4536 if (hasText) textfreeT((LPWSTR)fiw.psz, FALSE);
4537 return res;
4540 /***
4541 * DESCRIPTION:
4542 * Retrieves the background image of the listview control.
4544 * PARAMETER(S):
4545 * [I] infoPtr : valid pointer to the listview structure
4546 * [O] lpBkImage : background image attributes
4548 * RETURN:
4549 * SUCCESS : TRUE
4550 * FAILURE : FALSE
4552 /* static BOOL LISTVIEW_GetBkImage(LISTVIEW_INFO *infoPtr, LPLVBKIMAGE lpBkImage) */
4553 /* { */
4554 /* FIXME (listview, "empty stub!\n"); */
4555 /* return FALSE; */
4556 /* } */
4558 /***
4559 * DESCRIPTION:
4560 * Retrieves column attributes.
4562 * PARAMETER(S):
4563 * [I] infoPtr : valid pointer to the listview structure
4564 * [I] nColumn : column index
4565 * [IO] lpColumn : column information
4566 * [I] isW : if TRUE, then lpColumn is a LPLVCOLUMNW
4567 * otherwise it is in fact a LPLVCOLUMNA
4569 * RETURN:
4570 * SUCCESS : TRUE
4571 * FAILURE : FALSE
4573 static BOOL LISTVIEW_GetColumnT(LISTVIEW_INFO *infoPtr, INT nColumn, LPLVCOLUMNW lpColumn, BOOL isW)
4575 COLUMN_INFO *lpColumnInfo;
4576 HDITEMW hdi;
4578 if (!lpColumn || nColumn < 0 || nColumn >= infoPtr->hdpaColumns->nItemCount) return FALSE;
4579 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, nColumn);
4581 /* initialize memory */
4582 ZeroMemory(&hdi, sizeof(hdi));
4584 if (lpColumn->mask & LVCF_TEXT)
4586 hdi.mask |= HDI_TEXT;
4587 hdi.pszText = lpColumn->pszText;
4588 hdi.cchTextMax = lpColumn->cchTextMax;
4591 if (lpColumn->mask & LVCF_IMAGE)
4592 hdi.mask |= HDI_IMAGE;
4594 if (lpColumn->mask & LVCF_ORDER)
4595 hdi.mask |= HDI_ORDER;
4597 if (!SendMessageW(infoPtr->hwndHeader, isW ? HDM_GETITEMW : HDM_GETITEMA, nColumn, (LPARAM)&hdi)) return FALSE;
4599 if (lpColumn->mask & LVCF_FMT)
4600 lpColumn->fmt = lpColumnInfo->fmt;
4602 if (lpColumn->mask & LVCF_WIDTH)
4603 lpColumn->cx = lpColumnInfo->rcHeader.right - lpColumnInfo->rcHeader.left;
4605 if (lpColumn->mask & LVCF_IMAGE)
4606 lpColumn->iImage = hdi.iImage;
4608 if (lpColumn->mask & LVCF_ORDER)
4609 lpColumn->iOrder = hdi.iOrder;
4611 return TRUE;
4615 static BOOL LISTVIEW_GetColumnOrderArray(LISTVIEW_INFO *infoPtr, INT iCount, LPINT lpiArray)
4617 INT i;
4619 if (!lpiArray)
4620 return FALSE;
4622 /* FIXME: little hack */
4623 for (i = 0; i < iCount; i++)
4624 lpiArray[i] = i;
4626 return TRUE;
4629 /***
4630 * DESCRIPTION:
4631 * Retrieves the column width.
4633 * PARAMETER(S):
4634 * [I] infoPtr : valid pointer to the listview structure
4635 * [I] int : column index
4637 * RETURN:
4638 * SUCCESS : column width
4639 * FAILURE : zero
4641 static INT LISTVIEW_GetColumnWidth(LISTVIEW_INFO *infoPtr, INT nColumn)
4643 INT nColumnWidth = 0;
4644 RECT rcHeader;
4646 TRACE("nColumn=%d\n", nColumn);
4648 /* we have a 'column' in LIST and REPORT mode only */
4649 switch(infoPtr->dwStyle & LVS_TYPEMASK)
4651 case LVS_LIST:
4652 nColumnWidth = infoPtr->nItemWidth;
4653 break;
4654 case LVS_REPORT:
4655 if (nColumn < 0 || nColumn >= infoPtr->hdpaColumns->nItemCount) return 0;
4656 LISTVIEW_GetHeaderRect(infoPtr, nColumn, &rcHeader);
4657 nColumnWidth = rcHeader.right - rcHeader.left;
4658 break;
4661 TRACE("nColumnWidth=%d\n", nColumnWidth);
4662 return nColumnWidth;
4665 /***
4666 * DESCRIPTION:
4667 * In list or report display mode, retrieves the number of items that can fit
4668 * vertically in the visible area. In icon or small icon display mode,
4669 * retrieves the total number of visible items.
4671 * PARAMETER(S):
4672 * [I] infoPtr : valid pointer to the listview structure
4674 * RETURN:
4675 * Number of fully visible items.
4677 static INT LISTVIEW_GetCountPerPage(LISTVIEW_INFO *infoPtr)
4679 switch (infoPtr->dwStyle & LVS_TYPEMASK)
4681 case LVS_ICON:
4682 case LVS_SMALLICON:
4683 return infoPtr->nItemCount;
4684 case LVS_REPORT:
4685 return LISTVIEW_GetCountPerColumn(infoPtr);
4686 case LVS_LIST:
4687 return LISTVIEW_GetCountPerRow(infoPtr) * LISTVIEW_GetCountPerColumn(infoPtr);
4689 assert(FALSE);
4692 /***
4693 * DESCRIPTION:
4694 * Retrieves an image list handle.
4696 * PARAMETER(S):
4697 * [I] infoPtr : valid pointer to the listview structure
4698 * [I] nImageList : image list identifier
4700 * RETURN:
4701 * SUCCESS : image list handle
4702 * FAILURE : NULL
4704 static HIMAGELIST LISTVIEW_GetImageList(LISTVIEW_INFO *infoPtr, INT nImageList)
4706 switch (nImageList)
4708 case LVSIL_NORMAL: return infoPtr->himlNormal;
4709 case LVSIL_SMALL: return infoPtr->himlSmall;
4710 case LVSIL_STATE: return infoPtr->himlState;
4712 return NULL;
4715 /* LISTVIEW_GetISearchString */
4717 /***
4718 * DESCRIPTION:
4719 * Retrieves item attributes.
4721 * PARAMETER(S):
4722 * [I] hwnd : window handle
4723 * [IO] lpLVItem : item info
4724 * [I] isW : if TRUE, then lpLVItem is a LPLVITEMW,
4725 * if FALSE, the lpLVItem is a LPLVITEMA.
4727 * NOTE:
4728 * This is the internal 'GetItem' interface -- it tries to
4729 * be smart, and avoids text copies, if possible, by modifing
4730 * lpLVItem->pszText to point to the text string. Please note
4731 * that this is not always possible (e.g. OWNERDATA), so on
4732 * entry you *must* supply valid values for pszText, and cchTextMax.
4733 * The only difference to the documented interface is that upon
4734 * return, you should use *only* the lpLVItem->pszText, rather than
4735 * the buffer pointer you provided on input. Most code already does
4736 * that, so it's not a problem.
4737 * For the two cases when the text must be copied (that is,
4738 * for LVM_GETITEM, and LVMGETITEMTEXT), use LISTVIEW_GetItemExtT.
4740 * RETURN:
4741 * SUCCESS : TRUE
4742 * FAILURE : FALSE
4744 static BOOL LISTVIEW_GetItemT(LISTVIEW_INFO *infoPtr, LPLVITEMW lpLVItem, BOOL isW)
4746 ITEMHDR callbackHdr = { LPSTR_TEXTCALLBACKW, I_IMAGECALLBACK };
4747 NMLVDISPINFOW dispInfo;
4748 ITEM_INFO *lpItem;
4749 ITEMHDR* pItemHdr;
4750 HDPA hdpaSubItems;
4752 TRACE("(lpLVItem=%s, isW=%d)\n", debuglvitem_t(lpLVItem, isW), isW);
4754 if (!lpLVItem || lpLVItem->iItem < 0 || lpLVItem->iItem >= infoPtr->nItemCount)
4755 return FALSE;
4757 if (lpLVItem->mask == 0) return TRUE;
4759 /* a quick optimization if all we're asked is the focus state
4760 * these queries are worth optimising since they are common,
4761 * and can be answered in constant time, without the heavy accesses */
4762 if ( (lpLVItem->mask == LVIF_STATE) && (lpLVItem->stateMask == LVIS_FOCUSED) &&
4763 !(infoPtr->uCallbackMask & LVIS_FOCUSED) )
4765 lpLVItem->state = 0;
4766 if (infoPtr->nFocusedItem == lpLVItem->iItem)
4767 lpLVItem->state |= LVIS_FOCUSED;
4768 return TRUE;
4771 ZeroMemory(&dispInfo, sizeof(dispInfo));
4773 /* if the app stores all the data, handle it separately */
4774 if (infoPtr->dwStyle & LVS_OWNERDATA)
4776 dispInfo.item.state = 0;
4778 /* apprently, we should not callback for lParam in LVS_OWNERDATA */
4779 if ((lpLVItem->mask & ~(LVIF_STATE | LVIF_PARAM)) || infoPtr->uCallbackMask)
4781 /* NOTE: copy only fields which we _know_ are initialized, some apps
4782 * depend on the uninitialized fields being 0 */
4783 dispInfo.item.mask = lpLVItem->mask & ~LVIF_PARAM;
4784 dispInfo.item.iItem = lpLVItem->iItem;
4785 dispInfo.item.iSubItem = lpLVItem->iSubItem;
4786 if (lpLVItem->mask & LVIF_TEXT)
4788 dispInfo.item.pszText = lpLVItem->pszText;
4789 dispInfo.item.cchTextMax = lpLVItem->cchTextMax;
4791 if (lpLVItem->mask & LVIF_STATE)
4792 dispInfo.item.stateMask = lpLVItem->stateMask & infoPtr->uCallbackMask;
4793 notify_dispinfoT(infoPtr, LVN_GETDISPINFOW, &dispInfo, isW);
4794 dispInfo.item.stateMask = lpLVItem->stateMask;
4795 *lpLVItem = dispInfo.item;
4796 TRACE(" getdispinfo(1):lpLVItem=%s\n", debuglvitem_t(lpLVItem, isW));
4799 /* make sure lParam is zeroed out */
4800 if (lpLVItem->mask & LVIF_PARAM) lpLVItem->lParam = 0;
4802 /* we store only a little state, so if we're not asked, we're done */
4803 if (!(lpLVItem->mask & LVIF_STATE) || lpLVItem->iSubItem) return TRUE;
4805 /* if focus is handled by us, report it */
4806 if ( lpLVItem->stateMask & ~infoPtr->uCallbackMask & LVIS_FOCUSED )
4808 lpLVItem->state &= ~LVIS_FOCUSED;
4809 if (infoPtr->nFocusedItem == lpLVItem->iItem)
4810 lpLVItem->state |= LVIS_FOCUSED;
4813 /* and do the same for selection, if we handle it */
4814 if ( lpLVItem->stateMask & ~infoPtr->uCallbackMask & LVIS_SELECTED )
4816 lpLVItem->state &= ~LVIS_SELECTED;
4817 if (ranges_contain(infoPtr->selectionRanges, lpLVItem->iItem))
4818 lpLVItem->state |= LVIS_SELECTED;
4821 return TRUE;
4824 /* find the item and subitem structures before we proceed */
4825 hdpaSubItems = (HDPA)DPA_GetPtr(infoPtr->hdpaItems, lpLVItem->iItem);
4826 lpItem = (ITEM_INFO *)DPA_GetPtr(hdpaSubItems, 0);
4827 assert (lpItem);
4829 if (lpLVItem->iSubItem)
4831 SUBITEM_INFO *lpSubItem = LISTVIEW_GetSubItemPtr(hdpaSubItems, lpLVItem->iSubItem);
4832 pItemHdr = lpSubItem ? &lpSubItem->hdr : &callbackHdr;
4834 else
4835 pItemHdr = &lpItem->hdr;
4837 /* Do we need to query the state from the app? */
4838 if ((lpLVItem->mask & LVIF_STATE) && infoPtr->uCallbackMask && lpLVItem->iSubItem == 0)
4840 dispInfo.item.mask |= LVIF_STATE;
4841 dispInfo.item.stateMask = infoPtr->uCallbackMask;
4844 /* Do we need to enquire about the image? */
4845 if ((lpLVItem->mask & LVIF_IMAGE) && pItemHdr->iImage == I_IMAGECALLBACK)
4846 dispInfo.item.mask |= LVIF_IMAGE;
4848 /* Apps depend on calling back for text if it is NULL or LPSTR_TEXTCALLBACKW */
4849 if ((lpLVItem->mask & LVIF_TEXT) && !is_textW(pItemHdr->pszText))
4851 dispInfo.item.mask |= LVIF_TEXT;
4852 dispInfo.item.pszText = lpLVItem->pszText;
4853 dispInfo.item.cchTextMax = lpLVItem->cchTextMax;
4854 if (dispInfo.item.pszText && dispInfo.item.cchTextMax > 0)
4855 *dispInfo.item.pszText = '\0';
4858 /* If we don't have all the requested info, query the application */
4859 if (dispInfo.item.mask != 0)
4861 dispInfo.item.iItem = lpLVItem->iItem;
4862 dispInfo.item.iSubItem = lpLVItem->iSubItem;
4863 dispInfo.item.lParam = lpItem->lParam;
4864 notify_dispinfoT(infoPtr, LVN_GETDISPINFOW, &dispInfo, isW);
4865 TRACE(" getdispinfo(2):item=%s\n", debuglvitem_t(&dispInfo.item, isW));
4868 /* we should not store values for subitems */
4869 if (lpLVItem->iSubItem) dispInfo.item.mask &= ~LVIF_DI_SETITEM;
4871 /* Now, handle the iImage field */
4872 if (dispInfo.item.mask & LVIF_IMAGE)
4874 lpLVItem->iImage = dispInfo.item.iImage;
4875 if ((dispInfo.item.mask & LVIF_DI_SETITEM) && pItemHdr->iImage == I_IMAGECALLBACK)
4876 pItemHdr->iImage = dispInfo.item.iImage;
4878 else if (lpLVItem->mask & LVIF_IMAGE)
4879 lpLVItem->iImage = pItemHdr->iImage;
4881 /* The pszText field */
4882 if (dispInfo.item.mask & LVIF_TEXT)
4884 if ((dispInfo.item.mask & LVIF_DI_SETITEM) && pItemHdr->pszText)
4885 textsetptrT(&pItemHdr->pszText, dispInfo.item.pszText, isW);
4887 lpLVItem->pszText = dispInfo.item.pszText;
4889 else if (lpLVItem->mask & LVIF_TEXT)
4891 if (isW) lpLVItem->pszText = pItemHdr->pszText;
4892 else textcpynT(lpLVItem->pszText, isW, pItemHdr->pszText, TRUE, lpLVItem->cchTextMax);
4895 /* if this is a subitem, we're done */
4896 if (lpLVItem->iSubItem) return TRUE;
4898 /* Next is the lParam field */
4899 if (dispInfo.item.mask & LVIF_PARAM)
4901 lpLVItem->lParam = dispInfo.item.lParam;
4902 if ((dispInfo.item.mask & LVIF_DI_SETITEM))
4903 lpItem->lParam = dispInfo.item.lParam;
4905 else if (lpLVItem->mask & LVIF_PARAM)
4906 lpLVItem->lParam = lpItem->lParam;
4908 /* ... the state field (this one is different due to uCallbackmask) */
4909 if (lpLVItem->mask & LVIF_STATE)
4911 lpLVItem->state = lpItem->state;
4912 if (dispInfo.item.mask & LVIF_STATE)
4914 lpLVItem->state &= ~dispInfo.item.stateMask;
4915 lpLVItem->state |= (dispInfo.item.state & dispInfo.item.stateMask);
4917 if ( lpLVItem->stateMask & ~infoPtr->uCallbackMask & LVIS_FOCUSED )
4919 lpLVItem->state &= ~LVIS_FOCUSED;
4920 if (infoPtr->nFocusedItem == lpLVItem->iItem)
4921 lpLVItem->state |= LVIS_FOCUSED;
4923 if ( lpLVItem->stateMask & ~infoPtr->uCallbackMask & LVIS_SELECTED )
4925 lpLVItem->state &= ~LVIS_SELECTED;
4926 if (ranges_contain(infoPtr->selectionRanges, lpLVItem->iItem))
4927 lpLVItem->state |= LVIS_SELECTED;
4931 /* and last, but not least, the indent field */
4932 if (lpLVItem->mask & LVIF_INDENT)
4933 lpLVItem->iIndent = lpItem->iIndent;
4935 return TRUE;
4938 /***
4939 * DESCRIPTION:
4940 * Retrieves item attributes.
4942 * PARAMETER(S):
4943 * [I] hwnd : window handle
4944 * [IO] lpLVItem : item info
4945 * [I] isW : if TRUE, then lpLVItem is a LPLVITEMW,
4946 * if FALSE, the lpLVItem is a LPLVITEMA.
4948 * NOTE:
4949 * This is the external 'GetItem' interface -- it properly copies
4950 * the text in the provided buffer.
4952 * RETURN:
4953 * SUCCESS : TRUE
4954 * FAILURE : FALSE
4956 static BOOL LISTVIEW_GetItemExtT(LISTVIEW_INFO *infoPtr, LPLVITEMW lpLVItem, BOOL isW)
4958 LPWSTR pszText;
4959 BOOL bResult;
4961 if (!lpLVItem || lpLVItem->iItem < 0 || lpLVItem->iItem >= infoPtr->nItemCount)
4962 return FALSE;
4964 pszText = lpLVItem->pszText;
4965 bResult = LISTVIEW_GetItemT(infoPtr, lpLVItem, isW);
4966 if (bResult && lpLVItem->pszText != pszText)
4967 textcpynT(pszText, isW, lpLVItem->pszText, isW, lpLVItem->cchTextMax);
4968 lpLVItem->pszText = pszText;
4970 return bResult;
4974 /***
4975 * DESCRIPTION:
4976 * Retrieves the position (upper-left) of the listview control item.
4977 * Note that for LVS_ICON style, the upper-left is that of the icon
4978 * and not the bounding box.
4980 * PARAMETER(S):
4981 * [I] infoPtr : valid pointer to the listview structure
4982 * [I] nItem : item index
4983 * [O] lpptPosition : coordinate information
4985 * RETURN:
4986 * SUCCESS : TRUE
4987 * FAILURE : FALSE
4989 static BOOL LISTVIEW_GetItemPosition(LISTVIEW_INFO *infoPtr, INT nItem, LPPOINT lpptPosition)
4991 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
4992 POINT Origin;
4994 TRACE("(nItem=%d, lpptPosition=%p)\n", nItem, lpptPosition);
4996 if (!lpptPosition || nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
4998 LISTVIEW_GetOrigin(infoPtr, &Origin);
4999 LISTVIEW_GetItemOrigin(infoPtr, nItem, lpptPosition);
5001 if (uView == LVS_ICON)
5003 lpptPosition->x += (infoPtr->nItemWidth - infoPtr->iconSize.cx) / 2;
5004 lpptPosition->y += ICON_TOP_PADDING;
5006 lpptPosition->x += Origin.x;
5007 lpptPosition->y += Origin.y;
5009 TRACE (" lpptPosition=%s\n", debugpoint(lpptPosition));
5010 return TRUE;
5014 /***
5015 * DESCRIPTION:
5016 * Retrieves the bounding rectangle for a listview control item.
5018 * PARAMETER(S):
5019 * [I] infoPtr : valid pointer to the listview structure
5020 * [I] nItem : item index
5021 * [IO] lprc : bounding rectangle coordinates
5022 * lprc->left specifies the portion of the item for which the bounding
5023 * rectangle will be retrieved.
5025 * LVIR_BOUNDS Returns the bounding rectangle of the entire item,
5026 * including the icon and label.
5028 * * For LVS_ICON
5029 * * Experiment shows that native control returns:
5030 * * width = min (48, length of text line)
5031 * * .left = position.x - (width - iconsize.cx)/2
5032 * * .right = .left + width
5033 * * height = #lines of text * ntmHeight + icon height + 8
5034 * * .top = position.y - 2
5035 * * .bottom = .top + height
5036 * * separation between items .y = itemSpacing.cy - height
5037 * * .x = itemSpacing.cx - width
5038 * LVIR_ICON Returns the bounding rectangle of the icon or small icon.
5040 * * For LVS_ICON
5041 * * Experiment shows that native control returns:
5042 * * width = iconSize.cx + 16
5043 * * .left = position.x - (width - iconsize.cx)/2
5044 * * .right = .left + width
5045 * * height = iconSize.cy + 4
5046 * * .top = position.y - 2
5047 * * .bottom = .top + height
5048 * * separation between items .y = itemSpacing.cy - height
5049 * * .x = itemSpacing.cx - width
5050 * LVIR_LABEL Returns the bounding rectangle of the item text.
5052 * * For LVS_ICON
5053 * * Experiment shows that native control returns:
5054 * * width = text length
5055 * * .left = position.x - width/2
5056 * * .right = .left + width
5057 * * height = ntmH * linecount + 2
5058 * * .top = position.y + iconSize.cy + 6
5059 * * .bottom = .top + height
5060 * * separation between items .y = itemSpacing.cy - height
5061 * * .x = itemSpacing.cx - width
5062 * LVIR_SELECTBOUNDS Returns the union of the LVIR_ICON and LVIR_LABEL
5063 * rectangles, but excludes columns in report view.
5065 * RETURN:
5066 * SUCCESS : TRUE
5067 * FAILURE : FALSE
5069 * NOTES
5070 * Note that the bounding rectangle of the label in the LVS_ICON view depends
5071 * upon whether the window has the focus currently and on whether the item
5072 * is the one with the focus. Ensure that the control's record of which
5073 * item has the focus agrees with the items' records.
5075 static BOOL LISTVIEW_GetItemRect(LISTVIEW_INFO *infoPtr, INT nItem, LPRECT lprc)
5077 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
5078 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
5079 BOOL doLabel = TRUE, oversizedBox = FALSE;
5080 POINT Position, Origin;
5081 LVITEMW lvItem;
5082 RECT label_rect;
5084 TRACE("(hwnd=%p, nItem=%d, lprc=%p)\n", infoPtr->hwndSelf, nItem, lprc);
5086 if (!lprc || nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
5088 LISTVIEW_GetOrigin(infoPtr, &Origin);
5089 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position);
5091 /* Be smart and try to figure out the minimum we have to do */
5092 if (lprc->left == LVIR_ICON) doLabel = FALSE;
5093 if (uView == LVS_REPORT && lprc->left == LVIR_BOUNDS) doLabel = FALSE;
5094 if (uView == LVS_ICON && lprc->left != LVIR_ICON &&
5095 infoPtr->bFocus && LISTVIEW_GetItemState(infoPtr, nItem, LVIS_FOCUSED))
5096 oversizedBox = TRUE;
5098 /* get what we need from the item before hand, so we make
5099 * only one request. This can speed up things, if data
5100 * is stored on the app side */
5101 lvItem.mask = 0;
5102 if (uView == LVS_REPORT) lvItem.mask |= LVIF_INDENT;
5103 if (doLabel) lvItem.mask |= LVIF_TEXT;
5104 lvItem.iItem = nItem;
5105 lvItem.iSubItem = 0;
5106 lvItem.pszText = szDispText;
5107 lvItem.cchTextMax = DISP_TEXT_SIZE;
5108 if (lvItem.mask && !LISTVIEW_GetItemW(infoPtr, &lvItem)) return FALSE;
5109 /* we got the state already up, simulate it here, to avoid a reget */
5110 if (uView == LVS_ICON && (lprc->left != LVIR_ICON))
5112 lvItem.mask |= LVIF_STATE;
5113 lvItem.stateMask = LVIS_FOCUSED;
5114 lvItem.state = (oversizedBox ? LVIS_FOCUSED : 0);
5117 if (uView == LVS_REPORT && (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT) && lprc->left == LVIR_SELECTBOUNDS)
5118 lprc->left = LVIR_BOUNDS;
5119 switch(lprc->left)
5121 case LVIR_ICON:
5122 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, NULL, NULL, lprc, NULL);
5123 break;
5125 case LVIR_LABEL:
5126 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, NULL, NULL, NULL, lprc);
5127 break;
5129 case LVIR_BOUNDS:
5130 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, lprc, NULL, NULL, NULL);
5131 break;
5133 case LVIR_SELECTBOUNDS:
5134 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, NULL, NULL, lprc, &label_rect);
5135 UnionRect(lprc, lprc, &label_rect);
5136 break;
5138 default:
5139 WARN("Unknown value: %d\n", lprc->left);
5140 return FALSE;
5143 OffsetRect(lprc, Position.x + Origin.x, Position.y + Origin.y);
5145 TRACE(" rect=%s\n", debugrect(lprc));
5147 return TRUE;
5150 /***
5151 * DESCRIPTION:
5152 * Retrieves the spacing between listview control items.
5154 * PARAMETER(S):
5155 * [I] infoPtr : valid pointer to the listview structure
5156 * [IO] lprc : rectangle to receive the output
5157 * on input, lprc->top = nSubItem
5158 * lprc->left = LVIR_ICON | LVIR_BOUNDS | LVIR_LABEL
5160 * NOTE: for subItem = 0, we should return the bounds of the _entire_ item,
5161 * not only those of the first column.
5162 * Fortunately, LISTVIEW_GetItemMetrics does the right thing.
5164 * RETURN:
5165 * TRUE: success
5166 * FALSE: failure
5168 static BOOL LISTVIEW_GetSubItemRect(LISTVIEW_INFO *infoPtr, INT nItem, LPRECT lprc)
5170 POINT Position, Origin;
5171 LVITEMW lvItem;
5173 if (!lprc || (infoPtr->dwStyle & LVS_TYPEMASK) != LVS_REPORT) return FALSE;
5175 TRACE("(nItem=%d, nSubItem=%d)\n", nItem, lprc->top);
5177 LISTVIEW_GetOrigin(infoPtr, &Origin);
5178 if (!LISTVIEW_GetItemPosition(infoPtr, nItem, &Position)) return FALSE;
5180 lvItem.mask = lprc->top == 0 ? LVIF_INDENT : 0;
5181 lvItem.iItem = nItem;
5182 lvItem.iSubItem = lprc->top;
5184 if (lvItem.mask && !LISTVIEW_GetItemW(infoPtr, &lvItem)) return FALSE;
5185 switch(lprc->left)
5187 case LVIR_ICON:
5188 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, NULL, NULL, lprc, NULL);
5189 break;
5191 case LVIR_LABEL:
5192 case LVIR_BOUNDS:
5193 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, lprc, NULL, NULL, NULL);
5194 break;
5196 default:
5197 ERR("Unknown bounds=%d\n", lprc->left);
5198 return FALSE;
5201 OffsetRect(lprc, Position.x + Origin.x, Position.y + Origin.y);
5202 return TRUE;
5206 /***
5207 * DESCRIPTION:
5208 * Retrieves the width of a label.
5210 * PARAMETER(S):
5211 * [I] infoPtr : valid pointer to the listview structure
5213 * RETURN:
5214 * SUCCESS : string width (in pixels)
5215 * FAILURE : zero
5217 static INT LISTVIEW_GetLabelWidth(LISTVIEW_INFO *infoPtr, INT nItem)
5219 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
5220 LVITEMW lvItem;
5222 TRACE("(nItem=%d)\n", nItem);
5224 lvItem.mask = LVIF_TEXT;
5225 lvItem.iItem = nItem;
5226 lvItem.iSubItem = 0;
5227 lvItem.pszText = szDispText;
5228 lvItem.cchTextMax = DISP_TEXT_SIZE;
5229 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) return 0;
5231 return LISTVIEW_GetStringWidthT(infoPtr, lvItem.pszText, TRUE);
5234 /***
5235 * DESCRIPTION:
5236 * Retrieves the spacing between listview control items.
5238 * PARAMETER(S):
5239 * [I] infoPtr : valid pointer to the listview structure
5240 * [I] bSmall : flag for small or large icon
5242 * RETURN:
5243 * Horizontal + vertical spacing
5245 static LONG LISTVIEW_GetItemSpacing(LISTVIEW_INFO *infoPtr, BOOL bSmall)
5247 LONG lResult;
5249 if (!bSmall)
5251 lResult = MAKELONG(infoPtr->iconSpacing.cx, infoPtr->iconSpacing.cy);
5253 else
5255 if ((infoPtr->dwStyle & LVS_TYPEMASK) == LVS_ICON)
5256 lResult = MAKELONG(DEFAULT_COLUMN_WIDTH, GetSystemMetrics(SM_CXSMICON)+HEIGHT_PADDING);
5257 else
5258 lResult = MAKELONG(infoPtr->nItemWidth, infoPtr->nItemHeight);
5260 return lResult;
5263 /***
5264 * DESCRIPTION:
5265 * Retrieves the state of a listview control item.
5267 * PARAMETER(S):
5268 * [I] infoPtr : valid pointer to the listview structure
5269 * [I] nItem : item index
5270 * [I] uMask : state mask
5272 * RETURN:
5273 * State specified by the mask.
5275 static UINT LISTVIEW_GetItemState(LISTVIEW_INFO *infoPtr, INT nItem, UINT uMask)
5277 LVITEMW lvItem;
5279 if (nItem < 0 || nItem >= infoPtr->nItemCount) return 0;
5281 lvItem.iItem = nItem;
5282 lvItem.iSubItem = 0;
5283 lvItem.mask = LVIF_STATE;
5284 lvItem.stateMask = uMask;
5285 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) return 0;
5287 return lvItem.state & uMask;
5290 /***
5291 * DESCRIPTION:
5292 * Retrieves the text of a listview control item or subitem.
5294 * PARAMETER(S):
5295 * [I] hwnd : window handle
5296 * [I] nItem : item index
5297 * [IO] lpLVItem : item information
5298 * [I] isW : TRUE if lpLVItem is Unicode
5300 * RETURN:
5301 * SUCCESS : string length
5302 * FAILURE : 0
5304 static INT LISTVIEW_GetItemTextT(LISTVIEW_INFO *infoPtr, INT nItem, LPLVITEMW lpLVItem, BOOL isW)
5306 if (!lpLVItem || nItem < 0 || nItem >= infoPtr->nItemCount) return 0;
5308 lpLVItem->mask = LVIF_TEXT;
5309 lpLVItem->iItem = nItem;
5310 if (!LISTVIEW_GetItemExtT(infoPtr, lpLVItem, isW)) return 0;
5312 return textlenT(lpLVItem->pszText, isW);
5315 /***
5316 * DESCRIPTION:
5317 * Searches for an item based on properties + relationships.
5319 * PARAMETER(S):
5320 * [I] infoPtr : valid pointer to the listview structure
5321 * [I] nItem : item index
5322 * [I] uFlags : relationship flag
5324 * RETURN:
5325 * SUCCESS : item index
5326 * FAILURE : -1
5328 static INT LISTVIEW_GetNextItem(LISTVIEW_INFO *infoPtr, INT nItem, UINT uFlags)
5330 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
5331 UINT uMask = 0;
5332 LVFINDINFOW lvFindInfo;
5333 INT nCountPerColumn;
5334 INT i;
5336 TRACE("nItem=%d, uFlags=%x, nItemCount=%d\n", nItem, uFlags, infoPtr->nItemCount);
5337 if (nItem < -1 || nItem >= infoPtr->nItemCount) return -1;
5339 ZeroMemory(&lvFindInfo, sizeof(lvFindInfo));
5341 if (uFlags & LVNI_CUT)
5342 uMask |= LVIS_CUT;
5344 if (uFlags & LVNI_DROPHILITED)
5345 uMask |= LVIS_DROPHILITED;
5347 if (uFlags & LVNI_FOCUSED)
5348 uMask |= LVIS_FOCUSED;
5350 if (uFlags & LVNI_SELECTED)
5351 uMask |= LVIS_SELECTED;
5353 /* if we're asked for the focused item, that's only one,
5354 * so it's worth optimizing */
5355 if (uFlags & LVNI_FOCUSED)
5357 if (!(LISTVIEW_GetItemState(infoPtr, infoPtr->nFocusedItem, uMask) & uMask) == uMask) return -1;
5358 return (infoPtr->nFocusedItem == nItem) ? -1 : infoPtr->nFocusedItem;
5361 if (uFlags & LVNI_ABOVE)
5363 if ((uView == LVS_LIST) || (uView == LVS_REPORT))
5365 while (nItem >= 0)
5367 nItem--;
5368 if ((ListView_GetItemState(infoPtr->hwndSelf, nItem, uMask) & uMask) == uMask)
5369 return nItem;
5372 else
5374 lvFindInfo.flags = LVFI_NEARESTXY;
5375 lvFindInfo.vkDirection = VK_UP;
5376 ListView_GetItemPosition(infoPtr->hwndSelf, nItem, &lvFindInfo.pt);
5377 while ((nItem = ListView_FindItemW(infoPtr->hwndSelf, nItem, &lvFindInfo)) != -1)
5379 if ((ListView_GetItemState(infoPtr->hwndSelf, nItem, uMask) & uMask) == uMask)
5380 return nItem;
5384 else if (uFlags & LVNI_BELOW)
5386 if ((uView == LVS_LIST) || (uView == LVS_REPORT))
5388 while (nItem < infoPtr->nItemCount)
5390 nItem++;
5391 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
5392 return nItem;
5395 else
5397 lvFindInfo.flags = LVFI_NEARESTXY;
5398 lvFindInfo.vkDirection = VK_DOWN;
5399 ListView_GetItemPosition(infoPtr->hwndSelf, nItem, &lvFindInfo.pt);
5400 while ((nItem = ListView_FindItemW(infoPtr->hwndSelf, nItem, &lvFindInfo)) != -1)
5402 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
5403 return nItem;
5407 else if (uFlags & LVNI_TOLEFT)
5409 if (uView == LVS_LIST)
5411 nCountPerColumn = LISTVIEW_GetCountPerColumn(infoPtr);
5412 while (nItem - nCountPerColumn >= 0)
5414 nItem -= nCountPerColumn;
5415 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
5416 return nItem;
5419 else if ((uView == LVS_SMALLICON) || (uView == LVS_ICON))
5421 lvFindInfo.flags = LVFI_NEARESTXY;
5422 lvFindInfo.vkDirection = VK_LEFT;
5423 ListView_GetItemPosition(infoPtr->hwndSelf, nItem, &lvFindInfo.pt);
5424 while ((nItem = ListView_FindItemW(infoPtr->hwndSelf, nItem, &lvFindInfo)) != -1)
5426 if ((ListView_GetItemState(infoPtr->hwndSelf, nItem, uMask) & uMask) == uMask)
5427 return nItem;
5431 else if (uFlags & LVNI_TORIGHT)
5433 if (uView == LVS_LIST)
5435 nCountPerColumn = LISTVIEW_GetCountPerColumn(infoPtr);
5436 while (nItem + nCountPerColumn < infoPtr->nItemCount)
5438 nItem += nCountPerColumn;
5439 if ((ListView_GetItemState(infoPtr->hwndSelf, nItem, uMask) & uMask) == uMask)
5440 return nItem;
5443 else if ((uView == LVS_SMALLICON) || (uView == LVS_ICON))
5445 lvFindInfo.flags = LVFI_NEARESTXY;
5446 lvFindInfo.vkDirection = VK_RIGHT;
5447 ListView_GetItemPosition(infoPtr->hwndSelf, nItem, &lvFindInfo.pt);
5448 while ((nItem = ListView_FindItemW(infoPtr->hwndSelf, nItem, &lvFindInfo)) != -1)
5450 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
5451 return nItem;
5455 else
5457 nItem++;
5459 /* search by index */
5460 for (i = nItem; i < infoPtr->nItemCount; i++)
5462 if ((LISTVIEW_GetItemState(infoPtr, i, uMask) & uMask) == uMask)
5463 return i;
5467 return -1;
5470 /* LISTVIEW_GetNumberOfWorkAreas */
5472 /***
5473 * DESCRIPTION:
5474 * Retrieves the origin coordinates when in icon or small icon display mode.
5476 * PARAMETER(S):
5477 * [I] infoPtr : valid pointer to the listview structure
5478 * [O] lpptOrigin : coordinate information
5480 * RETURN:
5481 * None.
5483 static void LISTVIEW_GetOrigin(LISTVIEW_INFO *infoPtr, LPPOINT lpptOrigin)
5485 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
5486 INT nHorzPos = 0, nVertPos = 0;
5487 SCROLLINFO scrollInfo;
5489 scrollInfo.cbSize = sizeof(SCROLLINFO);
5490 scrollInfo.fMask = SIF_POS;
5492 if ((infoPtr->dwStyle & WS_HSCROLL) && GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo))
5493 nHorzPos = scrollInfo.nPos;
5494 if ((infoPtr->dwStyle & WS_VSCROLL) && GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo))
5495 nVertPos = scrollInfo.nPos;
5497 TRACE("nHorzPos=%d, nVertPos=%d\n", nHorzPos, nVertPos);
5499 lpptOrigin->x = infoPtr->rcList.left;
5500 lpptOrigin->y = infoPtr->rcList.top;
5501 if (uView == LVS_LIST)
5502 nHorzPos *= infoPtr->nItemWidth;
5503 else if (uView == LVS_REPORT)
5504 nVertPos *= infoPtr->nItemHeight;
5506 lpptOrigin->x -= nHorzPos;
5507 lpptOrigin->y -= nVertPos;
5509 TRACE(" origin=%s\n", debugpoint(lpptOrigin));
5512 /***
5513 * DESCRIPTION:
5514 * Retrieves the width of a string.
5516 * PARAMETER(S):
5517 * [I] hwnd : window handle
5518 * [I] lpszText : text string to process
5519 * [I] isW : TRUE if lpszText is Unicode, FALSE otherwise
5521 * RETURN:
5522 * SUCCESS : string width (in pixels)
5523 * FAILURE : zero
5525 static INT LISTVIEW_GetStringWidthT(LISTVIEW_INFO *infoPtr, LPCWSTR lpszText, BOOL isW)
5527 SIZE stringSize;
5529 stringSize.cx = 0;
5530 if (is_textT(lpszText, isW))
5532 HFONT hFont = infoPtr->hFont ? infoPtr->hFont : infoPtr->hDefaultFont;
5533 HDC hdc = GetDC(infoPtr->hwndSelf);
5534 HFONT hOldFont = SelectObject(hdc, hFont);
5536 if (isW)
5537 GetTextExtentPointW(hdc, lpszText, lstrlenW(lpszText), &stringSize);
5538 else
5539 GetTextExtentPointA(hdc, (LPCSTR)lpszText, lstrlenA((LPCSTR)lpszText), &stringSize);
5540 SelectObject(hdc, hOldFont);
5541 ReleaseDC(infoPtr->hwndSelf, hdc);
5543 return stringSize.cx;
5546 /***
5547 * DESCRIPTION:
5548 * Determines which listview item is located at the specified position.
5550 * PARAMETER(S):
5551 * [I] infoPtr : valid pointer to the listview structure
5552 * [IO] lpht : hit test information
5553 * [I] subitem : fill out iSubItem.
5554 * [I] select : return the index only if the hit selects the item
5556 * NOTE:
5557 * (mm 20001022): We must not allow iSubItem to be touched, for
5558 * an app might pass only a structure with space up to iItem!
5559 * (MS Office 97 does that for instance in the file open dialog)
5561 * RETURN:
5562 * SUCCESS : item index
5563 * FAILURE : -1
5565 static INT LISTVIEW_HitTest(LISTVIEW_INFO *infoPtr, LPLVHITTESTINFO lpht, BOOL subitem, BOOL select)
5567 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
5568 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
5569 RECT rcBox, rcBounds, rcState, rcIcon, rcLabel, rcSearch;
5570 POINT Origin, Position, opt;
5571 LVITEMW lvItem;
5572 ITERATOR i;
5574 TRACE("(pt=%s, subitem=%d, select=%d)\n", debugpoint(&lpht->pt), subitem, select);
5576 lpht->flags = 0;
5577 lpht->iItem = -1;
5578 if (subitem) lpht->iSubItem = 0;
5580 if (infoPtr->rcList.left > lpht->pt.x)
5581 lpht->flags |= LVHT_TOLEFT;
5582 else if (infoPtr->rcList.right < lpht->pt.x)
5583 lpht->flags |= LVHT_TORIGHT;
5585 if (infoPtr->rcList.top > lpht->pt.y)
5586 lpht->flags |= LVHT_ABOVE;
5587 else if (infoPtr->rcList.bottom < lpht->pt.y)
5588 lpht->flags |= LVHT_BELOW;
5590 TRACE("lpht->flags=0x%x\n", lpht->flags);
5591 if (lpht->flags) return -1;
5593 lpht->flags |= LVHT_NOWHERE;
5595 LISTVIEW_GetOrigin(infoPtr, &Origin);
5597 /* first deal with the large items */
5598 rcSearch.left = lpht->pt.x;
5599 rcSearch.top = lpht->pt.y;
5600 rcSearch.right = rcSearch.left + 1;
5601 rcSearch.bottom = rcSearch.top + 1;
5603 iterator_frameditems(&i, infoPtr, &rcSearch);
5604 iterator_next(&i); /* go to first item in the sequence */
5605 lpht->iItem = i.nItem;
5606 iterator_destroy(&i);
5608 TRACE("lpht->iItem=%d\n", lpht->iItem);
5609 if (lpht->iItem == -1) return -1;
5611 lvItem.mask = LVIF_STATE | LVIF_TEXT;
5612 if (uView == LVS_REPORT) lvItem.mask |= LVIF_INDENT;
5613 lvItem.stateMask = LVIS_STATEIMAGEMASK;
5614 if (uView == LVS_ICON) lvItem.stateMask |= LVIS_FOCUSED;
5615 lvItem.iItem = lpht->iItem;
5616 lvItem.iSubItem = 0;
5617 lvItem.pszText = szDispText;
5618 lvItem.cchTextMax = DISP_TEXT_SIZE;
5619 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) return -1;
5620 if (!infoPtr->bFocus) lvItem.state &= ~LVIS_FOCUSED;
5622 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, &rcBox, &rcState, &rcIcon, &rcLabel);
5623 LISTVIEW_GetItemOrigin(infoPtr, lpht->iItem, &Position);
5624 opt.x = lpht->pt.x - Position.x - Origin.x;
5625 opt.y = lpht->pt.y - Position.y - Origin.y;
5627 if (uView == LVS_REPORT && (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT))
5628 rcBounds = rcBox;
5629 else
5630 UnionRect(&rcBounds, &rcIcon, &rcLabel);
5631 TRACE("rcBounds=%s\n", debugrect(&rcBounds));
5632 if (!PtInRect(&rcBounds, opt)) return -1;
5634 if (PtInRect(&rcIcon, opt))
5635 lpht->flags |= LVHT_ONITEMICON;
5636 else if (PtInRect(&rcLabel, opt))
5637 lpht->flags |= LVHT_ONITEMLABEL;
5638 else if (infoPtr->himlState && ((lvItem.state & LVIS_STATEIMAGEMASK) >> 12) && PtInRect(&rcState, opt))
5639 lpht->flags |= LVHT_ONITEMSTATEICON;
5640 if (lpht->flags & LVHT_ONITEM)
5641 lpht->flags &= ~LVHT_NOWHERE;
5643 TRACE("lpht->flags=0x%x\n", lpht->flags);
5644 if (uView == LVS_REPORT && lpht->iItem != -1 && subitem)
5646 INT j;
5648 rcBounds.right = rcBounds.left;
5649 for (j = 0; j < infoPtr->hdpaColumns->nItemCount; j++)
5651 rcBounds.left = rcBounds.right;
5652 rcBounds.right += LISTVIEW_GetColumnWidth(infoPtr, j);
5653 if (PtInRect(&rcBounds, opt))
5655 lpht->iSubItem = j;
5656 break;
5661 if (!select || lpht->iItem == -1) return lpht->iItem;
5663 if (uView == LVS_REPORT && (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT)) return lpht->iItem;
5665 if (uView == LVS_REPORT) UnionRect(&rcBounds, &rcIcon, &rcLabel);
5666 return PtInRect(&rcBounds, opt) ? lpht->iItem : -1;
5670 /* LISTVIEW_InsertCompare: callback routine for comparing pszText members of the LV_ITEMS
5671 in a LISTVIEW on insert. Passed to DPA_Sort in LISTVIEW_InsertItem.
5672 This function should only be used for inserting items into a sorted list (LVM_INSERTITEM)
5673 and not during the processing of a LVM_SORTITEMS message. Applications should provide
5674 their own sort proc. when sending LVM_SORTITEMS.
5676 /* Platform SDK:
5677 (remarks on LVITEM: LVM_INSERTITEM will insert the new item in the proper sort postion...
5679 LVS_SORTXXX must be specified,
5680 LVS_OWNERDRAW is not set,
5681 <item>.pszText is not LPSTR_TEXTCALLBACK.
5683 (LVS_SORT* flags): "For the LVS_SORTASCENDING... styles, item indices
5684 are sorted based on item text..."
5686 static INT WINAPI LISTVIEW_InsertCompare( LPVOID first, LPVOID second, LPARAM lParam)
5688 ITEM_INFO* lv_first = (ITEM_INFO*) DPA_GetPtr( (HDPA)first, 0 );
5689 ITEM_INFO* lv_second = (ITEM_INFO*) DPA_GetPtr( (HDPA)second, 0 );
5690 INT cmpv = textcmpWT(lv_first->hdr.pszText, lv_second->hdr.pszText, TRUE);
5692 /* if we're sorting descending, negate the return value */
5693 return (((LISTVIEW_INFO *)lParam)->dwStyle & LVS_SORTDESCENDING) ? -cmpv : cmpv;
5696 /***
5697 * nESCRIPTION:
5698 * Inserts a new item in the listview control.
5700 * PARAMETER(S):
5701 * [I] infoPtr : valid pointer to the listview structure
5702 * [I] lpLVItem : item information
5703 * [I] isW : TRUE if lpLVItem is Unicode, FALSE if it's ANSI
5705 * RETURN:
5706 * SUCCESS : new item index
5707 * FAILURE : -1
5709 static INT LISTVIEW_InsertItemT(LISTVIEW_INFO *infoPtr, LPLVITEMW lpLVItem, BOOL isW)
5711 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
5712 INT nItem;
5713 HDPA hdpaSubItems;
5714 NMLISTVIEW nmlv;
5715 ITEM_INFO *lpItem;
5716 BOOL is_sorted, has_changed;
5717 LVITEMW item;
5719 TRACE("(lpLVItem=%s, isW=%d)\n", debuglvitem_t(lpLVItem, isW), isW);
5721 if (infoPtr->dwStyle & LVS_OWNERDATA) return infoPtr->nItemCount++;
5723 /* make sure it's an item, and not a subitem; cannot insert a subitem */
5724 if (!lpLVItem || lpLVItem->iItem < 0 || lpLVItem->iSubItem) return -1;
5726 if (!is_assignable_item(lpLVItem, infoPtr->dwStyle)) return -1;
5728 if ( !(lpItem = (ITEM_INFO *)COMCTL32_Alloc(sizeof(ITEM_INFO))) )
5729 return -1;
5731 /* insert item in listview control data structure */
5732 if ( !(hdpaSubItems = DPA_Create(8)) ) goto fail;
5733 if ( !DPA_SetPtr(hdpaSubItems, 0, lpItem) ) assert (FALSE);
5735 is_sorted = (infoPtr->dwStyle & (LVS_SORTASCENDING | LVS_SORTDESCENDING)) &&
5736 !(infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && (LPSTR_TEXTCALLBACKW != lpLVItem->pszText);
5738 nItem = is_sorted ? infoPtr->nItemCount : min(lpLVItem->iItem, infoPtr->nItemCount);
5739 TRACE(" inserting at %d, sorted=%d, count=%d, iItem=%d\n", nItem, is_sorted, infoPtr->nItemCount, lpLVItem->iItem);
5740 nItem = DPA_InsertPtr( infoPtr->hdpaItems, nItem, hdpaSubItems );
5741 if (nItem == -1) goto fail;
5742 infoPtr->nItemCount++;
5744 /* set the item attributes */
5745 item = *lpLVItem;
5746 item.iItem = nItem;
5747 item.state &= ~LVIS_STATEIMAGEMASK;
5748 if (!set_main_item(infoPtr, &item, TRUE, isW, &has_changed)) goto undo;
5750 /* if we're sorted, sort the list, and update the index */
5751 if (is_sorted)
5753 DPA_Sort( infoPtr->hdpaItems, LISTVIEW_InsertCompare, (LPARAM)infoPtr );
5754 nItem = DPA_GetPtrIndex( infoPtr->hdpaItems, hdpaSubItems );
5755 assert(nItem != -1);
5758 /* make room for the position, if we are in the right mode */
5759 if ((uView == LVS_SMALLICON) || (uView == LVS_ICON))
5761 if (DPA_InsertPtr(infoPtr->hdpaPosX, nItem, 0) == -1)
5762 goto undo;
5763 if (DPA_InsertPtr(infoPtr->hdpaPosY, nItem, 0) == -1)
5765 DPA_DeletePtr(infoPtr->hdpaPosX, nItem);
5766 goto undo;
5770 /* Add the subitem list to the items array. Do this last in case we go to
5771 * fail during the above.
5773 LISTVIEW_ShiftIndices(infoPtr, nItem, 1);
5775 /* send LVN_INSERTITEM notification */
5776 ZeroMemory(&nmlv, sizeof(NMLISTVIEW));
5777 nmlv.iItem = nItem;
5778 nmlv.lParam = lpItem->lParam;
5779 notify_listview(infoPtr, LVN_INSERTITEM, &nmlv);
5781 /* align items (set position of each item) */
5782 if ((uView == LVS_SMALLICON || uView == LVS_ICON))
5784 POINT pt;
5786 if (infoPtr->dwStyle & LVS_ALIGNLEFT)
5787 LISTVIEW_NextIconPosLeft(infoPtr, &pt);
5788 else
5789 LISTVIEW_NextIconPosTop(infoPtr, &pt);
5791 LISTVIEW_MoveIconTo(infoPtr, nItem, &pt, TRUE);
5794 /* now is the invalidation fun */
5795 LISTVIEW_ScrollOnInsert(infoPtr, nItem, 1);
5796 return nItem;
5798 undo:
5799 DPA_DeletePtr(infoPtr->hdpaItems, nItem);
5800 infoPtr->nItemCount--;
5801 fail:
5802 DPA_DeletePtr(hdpaSubItems, 0);
5803 DPA_Destroy (hdpaSubItems);
5804 COMCTL32_Free (lpItem);
5805 return -1;
5808 /***
5809 * DESCRIPTION:
5810 * Redraws a range of items.
5812 * PARAMETER(S):
5813 * [I] infoPtr : valid pointer to the listview structure
5814 * [I] nFirst : first item
5815 * [I] nLast : last item
5817 * RETURN:
5818 * SUCCESS : TRUE
5819 * FAILURE : FALSE
5821 static BOOL LISTVIEW_RedrawItems(LISTVIEW_INFO *infoPtr, INT nFirst, INT nLast)
5823 INT i;
5825 if (nLast < nFirst || min(nFirst, nLast) < 0 ||
5826 max(nFirst, nLast) >= infoPtr->nItemCount)
5827 return FALSE;
5829 for (i = nFirst; i <= nLast; i++)
5830 LISTVIEW_InvalidateItem(infoPtr, i);
5832 return TRUE;
5835 /***
5836 * DESCRIPTION:
5837 * Scroll the content of a listview.
5839 * PARAMETER(S):
5840 * [I] infoPtr : valid pointer to the listview structure
5841 * [I] dx : horizontal scroll amount in pixels
5842 * [I] dy : vertical scroll amount in pixels
5844 * RETURN:
5845 * SUCCESS : TRUE
5846 * FAILURE : FALSE
5848 * COMMENTS:
5849 * If the control is in report mode (LVS_REPORT) the control can
5850 * be scrolled only in line increments. "dy" will be rounded to the
5851 * nearest number of pixels that are a whole line. Ex: if line height
5852 * is 16 and an 8 is passed, the list will be scrolled by 16. If a 7
5853 * is passed the the scroll will be 0. (per MSDN 7/2002)
5855 * For: (per experimentaion with native control and CSpy ListView)
5856 * LVS_ICON dy=1 = 1 pixel (vertical only)
5857 * dx ignored
5858 * LVS_SMALLICON dy=1 = 1 pixel (vertical only)
5859 * dx ignored
5860 * LVS_LIST dx=1 = 1 column (horizontal only)
5861 * but will only scroll 1 column per message
5862 * no matter what the value.
5863 * dy must be 0 or FALSE returned.
5864 * LVS_REPORT dx=1 = 1 pixel
5865 * dy= see above
5868 static BOOL LISTVIEW_Scroll(LISTVIEW_INFO *infoPtr, INT dx, INT dy)
5870 switch(infoPtr->dwStyle & LVS_TYPEMASK) {
5871 case LVS_REPORT:
5872 dy += (dy < 0 ? -1 : 1) * infoPtr->nItemHeight/2;
5873 dy /= infoPtr->nItemHeight;
5874 break;
5875 case LVS_LIST:
5876 if (dy != 0) return FALSE;
5877 break;
5878 default: /* icon */
5879 dx = 0;
5880 break;
5883 if (dx != 0) LISTVIEW_HScroll(infoPtr, SB_INTERNAL, dx, 0);
5884 if (dy != 0) LISTVIEW_VScroll(infoPtr, SB_INTERNAL, dy, 0);
5886 return TRUE;
5889 /***
5890 * DESCRIPTION:
5891 * Sets the background color.
5893 * PARAMETER(S):
5894 * [I] infoPtr : valid pointer to the listview structure
5895 * [I] clrBk : background color
5897 * RETURN:
5898 * SUCCESS : TRUE
5899 * FAILURE : FALSE
5901 static BOOL LISTVIEW_SetBkColor(LISTVIEW_INFO *infoPtr, COLORREF clrBk)
5903 TRACE("(clrBk=%lx)\n", clrBk);
5905 if(infoPtr->clrBk != clrBk) {
5906 if (infoPtr->clrBk != CLR_NONE) DeleteObject(infoPtr->hBkBrush);
5907 infoPtr->clrBk = clrBk;
5908 if (clrBk == CLR_NONE)
5909 infoPtr->hBkBrush = (HBRUSH)GetClassLongW(infoPtr->hwndSelf, GCL_HBRBACKGROUND);
5910 else
5911 infoPtr->hBkBrush = CreateSolidBrush(clrBk);
5912 LISTVIEW_InvalidateList(infoPtr);
5915 return TRUE;
5918 /* LISTVIEW_SetBkImage */
5920 /*** Helper for {Insert,Set}ColumnT *only* */
5921 static void column_fill_hditem(LISTVIEW_INFO *infoPtr, HDITEMW *lphdi, INT nColumn, LPLVCOLUMNW lpColumn, BOOL isW)
5923 if (lpColumn->mask & LVCF_FMT)
5925 /* format member is valid */
5926 lphdi->mask |= HDI_FORMAT;
5928 /* set text alignment (leftmost column must be left-aligned) */
5929 if (nColumn == 0 || lpColumn->fmt & LVCFMT_LEFT)
5930 lphdi->fmt |= HDF_LEFT;
5931 else if (lpColumn->fmt & LVCFMT_RIGHT)
5932 lphdi->fmt |= HDF_RIGHT;
5933 else if (lpColumn->fmt & LVCFMT_CENTER)
5934 lphdi->fmt |= HDF_CENTER;
5936 if (lpColumn->fmt & LVCFMT_BITMAP_ON_RIGHT)
5937 lphdi->fmt |= HDF_BITMAP_ON_RIGHT;
5939 if (lpColumn->fmt & LVCFMT_COL_HAS_IMAGES)
5941 lphdi->fmt |= HDF_IMAGE;
5942 lphdi->iImage = I_IMAGECALLBACK;
5946 if (lpColumn->mask & LVCF_WIDTH)
5948 lphdi->mask |= HDI_WIDTH;
5949 if(lpColumn->cx == LVSCW_AUTOSIZE_USEHEADER)
5951 /* make it fill the remainder of the controls width */
5952 RECT rcHeader;
5953 INT item_index;
5955 for(item_index = 0; item_index < (nColumn - 1); item_index++)
5957 LISTVIEW_GetHeaderRect(infoPtr, item_index, &rcHeader);
5958 lphdi->cxy += rcHeader.right - rcHeader.left;
5961 /* retrieve the layout of the header */
5962 GetClientRect(infoPtr->hwndSelf, &rcHeader);
5963 TRACE("start cxy=%d rcHeader=%s\n", lphdi->cxy, debugrect(&rcHeader));
5965 lphdi->cxy = (rcHeader.right - rcHeader.left) - lphdi->cxy;
5967 else
5968 lphdi->cxy = lpColumn->cx;
5971 if (lpColumn->mask & LVCF_TEXT)
5973 lphdi->mask |= HDI_TEXT | HDI_FORMAT;
5974 lphdi->fmt |= HDF_STRING;
5975 lphdi->pszText = lpColumn->pszText;
5976 lphdi->cchTextMax = textlenT(lpColumn->pszText, isW);
5979 if (lpColumn->mask & LVCF_IMAGE)
5981 lphdi->mask |= HDI_IMAGE;
5982 lphdi->iImage = lpColumn->iImage;
5985 if (lpColumn->mask & LVCF_ORDER)
5987 lphdi->mask |= HDI_ORDER;
5988 lphdi->iOrder = lpColumn->iOrder;
5993 /***
5994 * DESCRIPTION:
5995 * Inserts a new column.
5997 * PARAMETER(S):
5998 * [I] infoPtr : valid pointer to the listview structure
5999 * [I] nColumn : column index
6000 * [I] lpColumn : column information
6001 * [I] isW : TRUE if lpColumn is Unicode, FALSE otherwise
6003 * RETURN:
6004 * SUCCESS : new column index
6005 * FAILURE : -1
6007 static INT LISTVIEW_InsertColumnT(LISTVIEW_INFO *infoPtr, INT nColumn,
6008 LPLVCOLUMNW lpColumn, BOOL isW)
6010 COLUMN_INFO *lpColumnInfo;
6011 INT nNewColumn;
6012 HDITEMW hdi;
6014 TRACE("(nColumn=%d, lpColumn=%s, isW=%d)\n", nColumn, debuglvcolumn_t(lpColumn, isW), isW);
6016 if (!lpColumn || nColumn < 0 || nColumn > infoPtr->hdpaColumns->nItemCount) return -1;
6018 ZeroMemory(&hdi, sizeof(HDITEMW));
6019 column_fill_hditem(infoPtr, &hdi, nColumn, lpColumn, isW);
6021 /* insert item in header control */
6022 nNewColumn = SendMessageW(infoPtr->hwndHeader,
6023 isW ? HDM_INSERTITEMW : HDM_INSERTITEMA,
6024 (WPARAM)nColumn, (LPARAM)&hdi);
6025 if (nNewColumn == -1) return -1;
6026 if (nNewColumn != nColumn) ERR("nColumn=%d, nNewColumn=%d\n", nColumn, nNewColumn);
6028 /* create our own column info */
6029 if (!(lpColumnInfo = COMCTL32_Alloc(sizeof(COLUMN_INFO)))) goto fail;
6030 if (DPA_InsertPtr(infoPtr->hdpaColumns, nNewColumn, lpColumnInfo) == -1) goto fail;
6032 if (lpColumn->mask & LVCF_FMT) lpColumnInfo->fmt = lpColumn->fmt;
6033 if (!Header_GetItemRect(infoPtr->hwndHeader, nNewColumn, &lpColumnInfo->rcHeader)) goto fail;
6035 /* now we have to actually adjust the data */
6036 if (!(infoPtr->dwStyle & LVS_OWNERDATA) && infoPtr->nItemCount > 0)
6038 SUBITEM_INFO *lpSubItem, *lpMainItem, **lpNewItems = 0;
6039 HDPA hdpaSubItems;
6040 INT nItem, i;
6042 /* preallocate memory, so we can fail gracefully */
6043 if (nNewColumn == 0)
6045 lpNewItems = COMCTL32_Alloc(sizeof(SUBITEM_INFO *) * infoPtr->nItemCount);
6046 if (!lpNewItems) goto fail;
6047 for (i = 0; i < infoPtr->nItemCount; i++)
6048 if (!(lpNewItems[i] = COMCTL32_Alloc(sizeof(SUBITEM_INFO)))) break;
6049 if (i != infoPtr->nItemCount)
6051 for(; i >=0; i--) COMCTL32_Free(lpNewItems[i]);
6052 COMCTL32_Free(lpNewItems);
6053 goto fail;
6057 for (nItem = 0; nItem < infoPtr->nItemCount; nItem++)
6059 hdpaSubItems = (HDPA)DPA_GetPtr(infoPtr->hdpaItems, nItem);
6060 for (i = 1; i < hdpaSubItems->nItemCount; i++)
6062 lpSubItem = (SUBITEM_INFO *)DPA_GetPtr(hdpaSubItems, i);
6063 if (lpSubItem->iSubItem >= nNewColumn)
6064 lpSubItem->iSubItem++;
6067 /* for inserting column 0, we have to special-case the main item */
6068 if (nNewColumn == 0)
6070 lpMainItem = (SUBITEM_INFO *)DPA_GetPtr(hdpaSubItems, 0);
6071 lpSubItem = lpNewItems[nItem];
6072 lpSubItem->hdr = lpMainItem->hdr;
6073 lpSubItem->iSubItem = 1;
6074 ZeroMemory(&lpMainItem->hdr, sizeof(lpMainItem->hdr));
6075 lpMainItem->iSubItem = 0;
6076 DPA_InsertPtr(hdpaSubItems, 1, lpSubItem);
6080 COMCTL32_Free(lpNewItems);
6083 /* make space for the new column */
6084 LISTVIEW_ScrollColumns(infoPtr, nNewColumn + 1, lpColumnInfo->rcHeader.right - lpColumnInfo->rcHeader.left);
6086 return nNewColumn;
6088 fail:
6089 if (nNewColumn != -1) SendMessageW(infoPtr->hwndHeader, HDM_DELETEITEM, nNewColumn, 0);
6090 if (lpColumnInfo)
6092 DPA_DeletePtr(infoPtr->hdpaColumns, nNewColumn);
6093 COMCTL32_Free(lpColumnInfo);
6095 return -1;
6098 /***
6099 * DESCRIPTION:
6100 * Sets the attributes of a header item.
6102 * PARAMETER(S):
6103 * [I] infoPtr : valid pointer to the listview structure
6104 * [I] nColumn : column index
6105 * [I] lpColumn : column attributes
6106 * [I] isW: if TRUE, the lpColumn is a LPLVCOLUMNW, else it is a LPLVCOLUMNA
6108 * RETURN:
6109 * SUCCESS : TRUE
6110 * FAILURE : FALSE
6112 static BOOL LISTVIEW_SetColumnT(LISTVIEW_INFO *infoPtr, INT nColumn,
6113 LPLVCOLUMNW lpColumn, BOOL isW)
6115 HDITEMW hdi, hdiget;
6116 BOOL bResult;
6118 TRACE("(nColumn=%d, lpColumn=%s, isW=%d)\n", nColumn, debuglvcolumn_t(lpColumn, isW), isW);
6120 if (!lpColumn || nColumn < 0 || nColumn >= infoPtr->hdpaColumns->nItemCount) return FALSE;
6122 ZeroMemory(&hdi, sizeof(HDITEMW));
6123 if (lpColumn->mask & LVCF_FMT)
6125 hdi.mask |= HDI_FORMAT;
6126 hdiget.mask = HDI_FORMAT;
6127 if (Header_GetItemW(infoPtr->hwndHeader, nColumn, &hdiget))
6128 hdi.fmt = hdiget.fmt & HDF_STRING;
6130 column_fill_hditem(infoPtr, &hdi, nColumn, lpColumn, isW);
6132 /* set header item attributes */
6133 bResult = SendMessageW(infoPtr->hwndHeader, isW ? HDM_SETITEMW : HDM_SETITEMA, (WPARAM)nColumn, (LPARAM)&hdi);
6134 if (!bResult) return FALSE;
6136 if (lpColumn->mask & LVCF_FMT)
6138 COLUMN_INFO *lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, nColumn);
6139 int oldFmt = lpColumnInfo->fmt;
6141 lpColumnInfo->fmt = lpColumn->fmt;
6142 if ((oldFmt ^ lpColumn->fmt) & (LVCFMT_JUSTIFYMASK | LVCFMT_IMAGE))
6144 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
6145 if (uView == LVS_REPORT) LISTVIEW_InvalidateColumn(infoPtr, nColumn);
6149 return TRUE;
6152 /***
6153 * DESCRIPTION:
6154 * Sets the column order array
6156 * PARAMETERS:
6157 * [I] infoPtr : valid pointer to the listview structure
6158 * [I] iCount : number of elements in column order array
6159 * [I] lpiArray : pointer to column order array
6161 * RETURN:
6162 * SUCCESS : TRUE
6163 * FAILURE : FALSE
6165 static BOOL LISTVIEW_SetColumnOrderArray(LISTVIEW_INFO *infoPtr, INT iCount, LPINT lpiArray)
6167 FIXME("iCount %d lpiArray %p\n", iCount, lpiArray);
6169 if (!lpiArray)
6170 return FALSE;
6172 return TRUE;
6176 /***
6177 * DESCRIPTION:
6178 * Sets the width of a column
6180 * PARAMETERS:
6181 * [I] infoPtr : valid pointer to the listview structure
6182 * [I] nColumn : column index
6183 * [I] cx : column width
6185 * RETURN:
6186 * SUCCESS : TRUE
6187 * FAILURE : FALSE
6189 static BOOL LISTVIEW_SetColumnWidth(LISTVIEW_INFO *infoPtr, INT nColumn, INT cx)
6191 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
6192 WCHAR szDispText[DISP_TEXT_SIZE] = { 0 };
6193 INT max_cx = 0;
6194 HDITEMW hdi;
6196 TRACE("(nColumn=%d, cx=%d\n", nColumn, cx);
6198 /* set column width only if in report or list mode */
6199 if (uView != LVS_REPORT && uView != LVS_LIST) return FALSE;
6201 /* take care of invalid cx values */
6202 if(uView == LVS_REPORT && cx < -2) cx = LVSCW_AUTOSIZE;
6203 else if (uView == LVS_LIST && cx < 1) return FALSE;
6205 /* resize all columns if in LVS_LIST mode */
6206 if(uView == LVS_LIST)
6208 infoPtr->nItemWidth = cx;
6209 LISTVIEW_InvalidateList(infoPtr);
6210 return TRUE;
6213 if (nColumn < 0 || nColumn >= infoPtr->hdpaColumns->nItemCount) return FALSE;
6215 if (cx == LVSCW_AUTOSIZE || (cx == LVSCW_AUTOSIZE_USEHEADER && nColumn < infoPtr->hdpaColumns->nItemCount -1))
6217 INT nLabelWidth;
6218 LVITEMW lvItem;
6220 lvItem.mask = LVIF_TEXT;
6221 lvItem.iItem = 0;
6222 lvItem.iSubItem = nColumn;
6223 lvItem.pszText = szDispText;
6224 lvItem.cchTextMax = DISP_TEXT_SIZE;
6225 for (; lvItem.iItem < infoPtr->nItemCount; lvItem.iItem++)
6227 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) continue;
6228 nLabelWidth = LISTVIEW_GetStringWidthT(infoPtr, lvItem.pszText, TRUE);
6229 if (max_cx < nLabelWidth) max_cx = nLabelWidth;
6231 if (infoPtr->himlSmall && (nColumn == 0 || (LISTVIEW_GetColumnInfo(infoPtr, nColumn)->fmt & LVCFMT_IMAGE)))
6232 max_cx += infoPtr->iconSize.cx + IMAGE_PADDING;
6233 max_cx += REPORT_MARGINX + TRAILING_PADDING;
6236 /* autosize based on listview items width */
6237 if(cx == LVSCW_AUTOSIZE)
6238 cx = max_cx;
6239 else if(cx == LVSCW_AUTOSIZE_USEHEADER)
6241 /* if iCol is the last column make it fill the remainder of the controls width */
6242 if(nColumn == infoPtr->hdpaColumns->nItemCount - 1)
6244 RECT rcHeader;
6245 POINT Origin;
6247 LISTVIEW_GetOrigin(infoPtr, &Origin);
6248 LISTVIEW_GetHeaderRect(infoPtr, nColumn, &rcHeader);
6250 cx = infoPtr->rcList.right - Origin.x - rcHeader.left;
6252 else
6254 /* Despite what the MS docs say, if this is not the last
6255 column, then MS resizes the column to the width of the
6256 largest text string in the column, including headers
6257 and items. This is different from LVSCW_AUTOSIZE in that
6258 LVSCW_AUTOSIZE ignores the header string length. */
6259 cx = 0;
6261 /* retrieve header text */
6262 hdi.mask = HDI_TEXT;
6263 hdi.cchTextMax = DISP_TEXT_SIZE;
6264 hdi.pszText = szDispText;
6265 if (Header_GetItemW(infoPtr->hwndHeader, nColumn, (LPARAM)&hdi))
6267 HDC hdc = GetDC(infoPtr->hwndSelf);
6268 HFONT old_font = SelectObject(hdc, (HFONT)SendMessageW(infoPtr->hwndHeader, WM_GETFONT, 0, 0));
6269 SIZE size;
6271 if (GetTextExtentPoint32W(hdc, hdi.pszText, lstrlenW(hdi.pszText), &size))
6272 cx = size.cx;
6273 /* FIXME: Take into account the header image, if one is present */
6274 SelectObject(hdc, old_font);
6275 ReleaseDC(infoPtr->hwndSelf, hdc);
6277 cx = max (cx, max_cx);
6281 if (cx < 0) return FALSE;
6283 /* call header to update the column change */
6284 hdi.mask = HDI_WIDTH;
6285 hdi.cxy = cx;
6286 TRACE("hdi.cxy=%d\n", hdi.cxy);
6287 return Header_SetItemW(infoPtr->hwndHeader, nColumn, (LPARAM)&hdi);
6290 /***
6291 * DESCRIPTION:
6292 * Sets the extended listview style.
6294 * PARAMETERS:
6295 * [I] infoPtr : valid pointer to the listview structure
6296 * [I] dwMask : mask
6297 * [I] dwStyle : style
6299 * RETURN:
6300 * SUCCESS : previous style
6301 * FAILURE : 0
6303 static DWORD LISTVIEW_SetExtendedListViewStyle(LISTVIEW_INFO *infoPtr, DWORD dwMask, DWORD dwStyle)
6305 DWORD dwOldStyle = infoPtr->dwLvExStyle;
6307 /* set new style */
6308 if (dwMask)
6309 infoPtr->dwLvExStyle = (dwOldStyle & ~dwMask) | (dwStyle & dwMask);
6310 else
6311 infoPtr->dwLvExStyle = dwStyle;
6313 return dwOldStyle;
6316 /***
6317 * DESCRIPTION:
6318 * Sets the new hot cursor used during hot tracking and hover selection.
6320 * PARAMETER(S):
6321 * [I] infoPtr : valid pointer to the listview structure
6322 * [I} hCurosr : the new hot cursor handle
6324 * RETURN:
6325 * Returns the previous hot cursor
6327 static HCURSOR LISTVIEW_SetHotCursor(LISTVIEW_INFO *infoPtr, HCURSOR hCursor)
6329 HCURSOR oldCursor = infoPtr->hHotCursor;
6331 infoPtr->hHotCursor = hCursor;
6333 return oldCursor;
6337 /***
6338 * DESCRIPTION:
6339 * Sets the hot item index.
6341 * PARAMETERS:
6342 * [I] infoPtr : valid pointer to the listview structure
6343 * [I] iIndex : index
6345 * RETURN:
6346 * SUCCESS : previous hot item index
6347 * FAILURE : -1 (no hot item)
6349 static INT LISTVIEW_SetHotItem(LISTVIEW_INFO *infoPtr, INT iIndex)
6351 INT iOldIndex = infoPtr->nHotItem;
6353 infoPtr->nHotItem = iIndex;
6355 return iOldIndex;
6359 /***
6360 * DESCRIPTION:
6361 * Sets the amount of time the cursor must hover over an item before it is selected.
6363 * PARAMETER(S):
6364 * [I] infoPtr : valid pointer to the listview structure
6365 * [I] dwHoverTime : hover time, if -1 the hover time is set to the default
6367 * RETURN:
6368 * Returns the previous hover time
6370 static DWORD LISTVIEW_SetHoverTime(LISTVIEW_INFO *infoPtr, DWORD dwHoverTime)
6372 DWORD oldHoverTime = infoPtr->dwHoverTime;
6374 infoPtr->dwHoverTime = dwHoverTime;
6376 return oldHoverTime;
6379 /***
6380 * DESCRIPTION:
6381 * Sets spacing for icons of LVS_ICON style.
6383 * PARAMETER(S):
6384 * [I] infoPtr : valid pointer to the listview structure
6385 * [I] spacing : MAKELONG(cx, cy)
6387 * RETURN:
6388 * MAKELONG(oldcx, oldcy)
6390 static DWORD LISTVIEW_SetIconSpacing(LISTVIEW_INFO *infoPtr, DWORD spacing)
6392 INT cy = HIWORD(spacing), cx = LOWORD(spacing);
6393 DWORD oldspacing = MAKELONG(infoPtr->iconSpacing.cx, infoPtr->iconSpacing.cy);
6394 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
6396 TRACE("requested=(%d,%d)\n", cx, cy);
6398 /* this is supported only for LVS_ICON style */
6399 if (uView != LVS_ICON) return oldspacing;
6401 /* set to defaults, if instructed to */
6402 if (cx == -1) cx = GetSystemMetrics(SM_CXICONSPACING);
6403 if (cy == -1) cy = GetSystemMetrics(SM_CYICONSPACING);
6405 /* if 0 then compute width
6406 * FIXME: Should scan each item and determine max width of
6407 * icon or label, then make that the width */
6408 if (cx == 0)
6409 cx = infoPtr->iconSpacing.cx;
6411 /* if 0 then compute height */
6412 if (cy == 0)
6413 cy = infoPtr->iconSize.cy + 2 * infoPtr->ntmHeight +
6414 ICON_BOTTOM_PADDING + ICON_TOP_PADDING + LABEL_VERT_PADDING;
6417 infoPtr->iconSpacing.cx = cx;
6418 infoPtr->iconSpacing.cy = cy;
6420 TRACE("old=(%d,%d), new=(%d,%d), iconSize=(%ld,%ld), ntmH=%d\n",
6421 LOWORD(oldspacing), HIWORD(oldspacing), cx, cy,
6422 infoPtr->iconSize.cx, infoPtr->iconSize.cy,
6423 infoPtr->ntmHeight);
6425 /* these depend on the iconSpacing */
6426 LISTVIEW_UpdateItemSize(infoPtr);
6428 return oldspacing;
6431 inline void set_icon_size(SIZE *size, HIMAGELIST himl, BOOL small)
6433 INT cx, cy;
6435 if (himl && ImageList_GetIconSize(himl, &cx, &cy))
6437 size->cx = cx;
6438 size->cy = cy;
6440 else
6442 size->cx = GetSystemMetrics(small ? SM_CXSMICON : SM_CXICON);
6443 size->cy = GetSystemMetrics(small ? SM_CYSMICON : SM_CYICON);
6447 /***
6448 * DESCRIPTION:
6449 * Sets image lists.
6451 * PARAMETER(S):
6452 * [I] infoPtr : valid pointer to the listview structure
6453 * [I] nType : image list type
6454 * [I] himl : image list handle
6456 * RETURN:
6457 * SUCCESS : old image list
6458 * FAILURE : NULL
6460 static HIMAGELIST LISTVIEW_SetImageList(LISTVIEW_INFO *infoPtr, INT nType, HIMAGELIST himl)
6462 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
6463 INT oldHeight = infoPtr->nItemHeight;
6464 HIMAGELIST himlOld = 0;
6466 TRACE("(nType=%d, himl=%p\n", nType, himl);
6468 switch (nType)
6470 case LVSIL_NORMAL:
6471 himlOld = infoPtr->himlNormal;
6472 infoPtr->himlNormal = himl;
6473 if (uView == LVS_ICON) set_icon_size(&infoPtr->iconSize, himl, FALSE);
6474 LISTVIEW_SetIconSpacing(infoPtr, 0);
6475 break;
6477 case LVSIL_SMALL:
6478 himlOld = infoPtr->himlSmall;
6479 infoPtr->himlSmall = himl;
6480 if (uView != LVS_ICON) set_icon_size(&infoPtr->iconSize, himl, TRUE);
6481 break;
6483 case LVSIL_STATE:
6484 himlOld = infoPtr->himlState;
6485 infoPtr->himlState = himl;
6486 set_icon_size(&infoPtr->iconStateSize, himl, TRUE);
6487 ImageList_SetBkColor(infoPtr->himlState, CLR_NONE);
6488 break;
6490 default:
6491 ERR("Unknown icon type=%d\n", nType);
6492 return NULL;
6495 infoPtr->nItemHeight = LISTVIEW_CalculateItemHeight(infoPtr);
6496 if (infoPtr->nItemHeight != oldHeight)
6497 LISTVIEW_UpdateScroll(infoPtr);
6499 return himlOld;
6502 /***
6503 * DESCRIPTION:
6504 * Preallocates memory (does *not* set the actual count of items !)
6506 * PARAMETER(S):
6507 * [I] infoPtr : valid pointer to the listview structure
6508 * [I] nItems : item count (projected number of items to allocate)
6509 * [I] dwFlags : update flags
6511 * RETURN:
6512 * SUCCESS : TRUE
6513 * FAILURE : FALSE
6515 static BOOL LISTVIEW_SetItemCount(LISTVIEW_INFO *infoPtr, INT nItems, DWORD dwFlags)
6517 TRACE("(nItems=%d, dwFlags=%lx)\n", nItems, dwFlags);
6519 if (infoPtr->dwStyle & LVS_OWNERDATA)
6521 int precount,topvisible;
6523 TRACE("LVS_OWNERDATA is set!\n");
6524 if (dwFlags & (LVSICF_NOINVALIDATEALL | LVSICF_NOSCROLL))
6525 FIXME("flags %s %s not implemented\n",
6526 (dwFlags & LVSICF_NOINVALIDATEALL) ? "LVSICF_NOINVALIDATEALL"
6527 : "",
6528 (dwFlags & LVSICF_NOSCROLL) ? "LVSICF_NOSCROLL" : "");
6530 LISTVIEW_DeselectAll(infoPtr);
6532 precount = infoPtr->nItemCount;
6533 topvisible = LISTVIEW_GetTopIndex(infoPtr) +
6534 LISTVIEW_GetCountPerColumn(infoPtr) + 1;
6536 infoPtr->nItemCount = nItems;
6537 LISTVIEW_UpdateItemSize(infoPtr);
6539 LISTVIEW_UpdateSize(infoPtr);
6540 LISTVIEW_UpdateScroll(infoPtr);
6542 if (min(precount,infoPtr->nItemCount) < topvisible)
6543 LISTVIEW_InvalidateList(infoPtr); /* FIXME: optimize */
6545 else
6547 /* According to MSDN for non-LVS_OWNERDATA this is just
6548 * a performance issue. The control allocates its internal
6549 * data structures for the number of items specified. It
6550 * cuts down on the number of memory allocations. Therefore
6551 * we will just issue a WARN here
6553 WARN("for non-ownerdata performance option not implemented.\n");
6556 return TRUE;
6559 /***
6560 * DESCRIPTION:
6561 * Sets the position of an item.
6563 * PARAMETER(S):
6564 * [I] infoPtr : valid pointer to the listview structure
6565 * [I] nItem : item index
6566 * [I] pt : coordinate
6568 * RETURN:
6569 * SUCCESS : TRUE
6570 * FAILURE : FALSE
6572 static BOOL LISTVIEW_SetItemPosition(LISTVIEW_INFO *infoPtr, INT nItem, POINT pt)
6574 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
6575 POINT Origin;
6577 TRACE("(nItem=%d, &pt=%s\n", nItem, debugpoint(&pt));
6579 if (nItem < 0 || nItem >= infoPtr->nItemCount ||
6580 !(uView == LVS_ICON || uView == LVS_SMALLICON)) return FALSE;
6582 LISTVIEW_GetOrigin(infoPtr, &Origin);
6584 /* This point value seems to be an undocumented feature.
6585 * The best guess is that it means either at the origin,
6586 * or at true beginning of the list. I will assume the origin. */
6587 if ((pt.x == -1) && (pt.y == -1))
6588 pt = Origin;
6590 if (uView == LVS_ICON)
6592 pt.x -= (infoPtr->nItemWidth - infoPtr->iconSize.cx) / 2;
6593 pt.y -= ICON_TOP_PADDING;
6595 pt.x -= Origin.x;
6596 pt.y -= Origin.y;
6598 infoPtr->bAutoarrange = FALSE;
6600 return LISTVIEW_MoveIconTo(infoPtr, nItem, &pt, FALSE);
6603 /***
6604 * DESCRIPTION:
6605 * Sets the state of one or many items.
6607 * PARAMETER(S):
6608 * [I] infoPtr : valid pointer to the listview structure
6609 * [I] nItem : item index
6610 * [I] lpLVItem : item or subitem info
6612 * RETURN:
6613 * SUCCESS : TRUE
6614 * FAILURE : FALSE
6616 static BOOL LISTVIEW_SetItemState(LISTVIEW_INFO *infoPtr, INT nItem, LPLVITEMW lpLVItem)
6618 BOOL bResult = TRUE;
6619 LVITEMW lvItem;
6621 lvItem.iItem = nItem;
6622 lvItem.iSubItem = 0;
6623 lvItem.mask = LVIF_STATE;
6624 lvItem.state = lpLVItem->state;
6625 lvItem.stateMask = lpLVItem->stateMask;
6626 TRACE("lvItem=%s\n", debuglvitem_t(&lvItem, TRUE));
6628 if (nItem == -1)
6630 /* apply to all items */
6631 for (lvItem.iItem = 0; lvItem.iItem < infoPtr->nItemCount; lvItem.iItem++)
6632 if (!LISTVIEW_SetItemT(infoPtr, &lvItem, TRUE)) bResult = FALSE;
6634 else
6635 bResult = LISTVIEW_SetItemT(infoPtr, &lvItem, TRUE);
6637 return bResult;
6640 /***
6641 * DESCRIPTION:
6642 * Sets the text of an item or subitem.
6644 * PARAMETER(S):
6645 * [I] hwnd : window handle
6646 * [I] nItem : item index
6647 * [I] lpLVItem : item or subitem info
6648 * [I] isW : TRUE if input is Unicode
6650 * RETURN:
6651 * SUCCESS : TRUE
6652 * FAILURE : FALSE
6654 static BOOL LISTVIEW_SetItemTextT(LISTVIEW_INFO *infoPtr, INT nItem, LPLVITEMW lpLVItem, BOOL isW)
6656 LVITEMW lvItem;
6658 if (nItem < 0 && nItem >= infoPtr->nItemCount) return FALSE;
6660 lvItem.iItem = nItem;
6661 lvItem.iSubItem = lpLVItem->iSubItem;
6662 lvItem.mask = LVIF_TEXT;
6663 lvItem.pszText = lpLVItem->pszText;
6664 lvItem.cchTextMax = lpLVItem->cchTextMax;
6666 TRACE("(nItem=%d, lpLVItem=%s, isW=%d)\n", nItem, debuglvitem_t(&lvItem, isW), isW);
6668 return LISTVIEW_SetItemT(infoPtr, &lvItem, isW);
6671 /***
6672 * DESCRIPTION:
6673 * Set item index that marks the start of a multiple selection.
6675 * PARAMETER(S):
6676 * [I] infoPtr : valid pointer to the listview structure
6677 * [I] nIndex : index
6679 * RETURN:
6680 * Index number or -1 if there is no selection mark.
6682 static INT LISTVIEW_SetSelectionMark(LISTVIEW_INFO *infoPtr, INT nIndex)
6684 INT nOldIndex = infoPtr->nSelectionMark;
6686 TRACE("(nIndex=%d)\n", nIndex);
6688 infoPtr->nSelectionMark = nIndex;
6690 return nOldIndex;
6693 /***
6694 * DESCRIPTION:
6695 * Sets the text background color.
6697 * PARAMETER(S):
6698 * [I] infoPtr : valid pointer to the listview structure
6699 * [I] clrTextBk : text background color
6701 * RETURN:
6702 * SUCCESS : TRUE
6703 * FAILURE : FALSE
6705 static BOOL LISTVIEW_SetTextBkColor(LISTVIEW_INFO *infoPtr, COLORREF clrTextBk)
6707 TRACE("(clrTextBk=%lx)\n", clrTextBk);
6709 if (infoPtr->clrTextBk != clrTextBk)
6711 infoPtr->clrTextBk = clrTextBk;
6712 LISTVIEW_InvalidateList(infoPtr);
6715 return TRUE;
6718 /***
6719 * DESCRIPTION:
6720 * Sets the text foreground color.
6722 * PARAMETER(S):
6723 * [I] infoPtr : valid pointer to the listview structure
6724 * [I] clrText : text color
6726 * RETURN:
6727 * SUCCESS : TRUE
6728 * FAILURE : FALSE
6730 static BOOL LISTVIEW_SetTextColor (LISTVIEW_INFO *infoPtr, COLORREF clrText)
6732 TRACE("(clrText=%lx)\n", clrText);
6734 if (infoPtr->clrText != clrText)
6736 infoPtr->clrText = clrText;
6737 LISTVIEW_InvalidateList(infoPtr);
6740 return TRUE;
6743 /* LISTVIEW_SetToolTips */
6744 /* LISTVIEW_SetUnicodeFormat */
6745 /* LISTVIEW_SetWorkAreas */
6747 /***
6748 * DESCRIPTION:
6749 * Callback internally used by LISTVIEW_SortItems()
6751 * PARAMETER(S):
6752 * [I] first : pointer to first ITEM_INFO to compare
6753 * [I] second : pointer to second ITEM_INFO to compare
6754 * [I] lParam : HWND of control
6756 * RETURN:
6757 * if first comes before second : negative
6758 * if first comes after second : positive
6759 * if first and second are equivalent : zero
6761 static INT WINAPI LISTVIEW_CallBackCompare(LPVOID first, LPVOID second, LPARAM lParam)
6763 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)GetWindowLongW((HWND)lParam, 0);
6764 ITEM_INFO* lv_first = (ITEM_INFO*) DPA_GetPtr( (HDPA)first, 0 );
6765 ITEM_INFO* lv_second = (ITEM_INFO*) DPA_GetPtr( (HDPA)second, 0 );
6767 /* Forward the call to the client defined callback */
6768 return (infoPtr->pfnCompare)( lv_first->lParam , lv_second->lParam, infoPtr->lParamSort );
6771 /***
6772 * DESCRIPTION:
6773 * Sorts the listview items.
6775 * PARAMETER(S):
6776 * [I] infoPtr : valid pointer to the listview structure
6777 * [I] pfnCompare : application-defined value
6778 * [I] lParamSort : pointer to comparision callback
6780 * RETURN:
6781 * SUCCESS : TRUE
6782 * FAILURE : FALSE
6784 static BOOL LISTVIEW_SortItems(LISTVIEW_INFO *infoPtr, PFNLVCOMPARE pfnCompare, LPARAM lParamSort)
6786 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
6787 HDPA hdpaSubItems;
6788 ITEM_INFO *lpItem;
6789 LPVOID selectionMarkItem;
6790 LVITEMW item;
6791 int i;
6793 TRACE("(pfnCompare=%p, lParamSort=%lx)\n", pfnCompare, lParamSort);
6795 if (infoPtr->dwStyle & LVS_OWNERDATA) return FALSE;
6797 if (!infoPtr->hdpaItems) return FALSE;
6799 /* if there are 0 or 1 items, there is no need to sort */
6800 if (infoPtr->nItemCount < 2) return TRUE;
6802 if (infoPtr->nFocusedItem >= 0)
6804 hdpaSubItems = (HDPA)DPA_GetPtr(infoPtr->hdpaItems, infoPtr->nFocusedItem);
6805 lpItem = (ITEM_INFO *)DPA_GetPtr(hdpaSubItems, 0);
6806 if (lpItem) lpItem->state |= LVIS_FOCUSED;
6808 /* FIXME: go thorugh selected items and mark them so in lpItem->state */
6809 /* clear the lpItem->state for non-selected ones */
6810 /* remove the selection ranges */
6812 infoPtr->pfnCompare = pfnCompare;
6813 infoPtr->lParamSort = lParamSort;
6814 DPA_Sort(infoPtr->hdpaItems, LISTVIEW_CallBackCompare, (LPARAM)infoPtr->hwndSelf);
6816 /* Adjust selections and indices so that they are the way they should
6817 * be after the sort (otherwise, the list items move around, but
6818 * whatever is at the item's previous original position will be
6819 * selected instead)
6821 selectionMarkItem=(infoPtr->nSelectionMark>=0)?DPA_GetPtr(infoPtr->hdpaItems, infoPtr->nSelectionMark):NULL;
6822 for (i=0; i < infoPtr->nItemCount; i++)
6824 hdpaSubItems = (HDPA)DPA_GetPtr(infoPtr->hdpaItems, i);
6825 lpItem = (ITEM_INFO *)DPA_GetPtr(hdpaSubItems, 0);
6827 if (lpItem->state & LVIS_SELECTED)
6829 item.state = LVIS_SELECTED;
6830 item.stateMask = LVIS_SELECTED;
6831 LISTVIEW_SetItemState(infoPtr, i, &item);
6833 if (lpItem->state & LVIS_FOCUSED)
6835 infoPtr->nFocusedItem = i;
6836 lpItem->state &= ~LVIS_FOCUSED;
6839 if (selectionMarkItem != NULL)
6840 infoPtr->nSelectionMark = DPA_GetPtrIndex(infoPtr->hdpaItems, selectionMarkItem);
6841 /* I believe nHotItem should be left alone, see LISTVIEW_ShiftIndices */
6843 /* refresh the display */
6844 if (uView != LVS_ICON && uView != LVS_SMALLICON)
6845 LISTVIEW_InvalidateList(infoPtr);
6847 return TRUE;
6850 /***
6851 * DESCRIPTION:
6852 * Updates an items or rearranges the listview control.
6854 * PARAMETER(S):
6855 * [I] infoPtr : valid pointer to the listview structure
6856 * [I] nItem : item index
6858 * RETURN:
6859 * SUCCESS : TRUE
6860 * FAILURE : FALSE
6862 static BOOL LISTVIEW_Update(LISTVIEW_INFO *infoPtr, INT nItem)
6864 TRACE("(nItem=%d)\n", nItem);
6866 if (nItem < 0 && nItem >= infoPtr->nItemCount) return FALSE;
6868 /* rearrange with default alignment style */
6869 if (is_autoarrange(infoPtr))
6870 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
6871 else
6872 LISTVIEW_InvalidateItem(infoPtr, nItem);
6874 return TRUE;
6878 /***
6879 * DESCRIPTION:
6880 * Creates the listview control.
6882 * PARAMETER(S):
6883 * [I] hwnd : window handle
6884 * [I] lpcs : the create parameters
6886 * RETURN:
6887 * Success: 0
6888 * Failure: -1
6890 static LRESULT LISTVIEW_Create(HWND hwnd, LPCREATESTRUCTW lpcs)
6892 LISTVIEW_INFO *infoPtr;
6893 UINT uView = lpcs->style & LVS_TYPEMASK;
6894 LOGFONTW logFont;
6896 TRACE("(lpcs=%p)\n", lpcs);
6898 /* initialize info pointer */
6899 infoPtr = (LISTVIEW_INFO *)COMCTL32_Alloc(sizeof(LISTVIEW_INFO));
6900 if (!infoPtr) return -1;
6902 SetWindowLongW(hwnd, 0, (LONG)infoPtr);
6904 infoPtr->hwndSelf = hwnd;
6905 infoPtr->dwStyle = lpcs->style;
6906 /* determine the type of structures to use */
6907 infoPtr->notifyFormat = SendMessageW(GetParent(infoPtr->hwndSelf), WM_NOTIFYFORMAT,
6908 (WPARAM)infoPtr->hwndSelf, (LPARAM)NF_QUERY);
6910 /* initialize color information */
6911 infoPtr->clrBk = CLR_NONE;
6912 infoPtr->clrText = comctl32_color.clrWindowText;
6913 infoPtr->clrTextBk = CLR_DEFAULT;
6914 LISTVIEW_SetBkColor(infoPtr, comctl32_color.clrWindow);
6916 /* set default values */
6917 infoPtr->nFocusedItem = -1;
6918 infoPtr->nSelectionMark = -1;
6919 infoPtr->nHotItem = -1;
6920 infoPtr->bRedraw = TRUE;
6921 infoPtr->bFirstPaint = TRUE;
6922 infoPtr->iconSpacing.cx = GetSystemMetrics(SM_CXICONSPACING);
6923 infoPtr->iconSpacing.cy = GetSystemMetrics(SM_CYICONSPACING);
6924 infoPtr->nEditLabelItem = -1;
6925 infoPtr->dwHoverTime = -1; /* default system hover time */
6927 /* get default font (icon title) */
6928 SystemParametersInfoW(SPI_GETICONTITLELOGFONT, 0, &logFont, 0);
6929 infoPtr->hDefaultFont = CreateFontIndirectW(&logFont);
6930 infoPtr->hFont = infoPtr->hDefaultFont;
6931 LISTVIEW_SaveTextMetrics(infoPtr);
6933 /* create header */
6934 infoPtr->hwndHeader = CreateWindowW(WC_HEADERW, (LPCWSTR)NULL,
6935 WS_CHILD | HDS_HORZ | (DWORD)((LVS_NOSORTHEADER & lpcs->style)?0:HDS_BUTTONS),
6936 0, 0, 0, 0, hwnd, (HMENU)0,
6937 lpcs->hInstance, NULL);
6939 /* set header unicode format */
6940 SendMessageW(infoPtr->hwndHeader, HDM_SETUNICODEFORMAT, (WPARAM)TRUE, (LPARAM)NULL);
6942 /* set header font */
6943 SendMessageW(infoPtr->hwndHeader, WM_SETFONT, (WPARAM)infoPtr->hFont, (LPARAM)TRUE);
6945 /* allocate memory for the selection ranges */
6946 if (!(infoPtr->selectionRanges = ranges_create(10))) return -1;
6948 /* allocate memory for the data structure */
6949 /* FIXME: what if we fail? */
6950 infoPtr->hdpaItems = DPA_Create(10);
6951 infoPtr->hdpaPosX = DPA_Create(10);
6952 infoPtr->hdpaPosY = DPA_Create(10);
6953 infoPtr->hdpaColumns = DPA_Create(10);
6955 /* initialize the icon sizes */
6956 set_icon_size(&infoPtr->iconSize, infoPtr->himlNormal, uView != LVS_ICON);
6957 set_icon_size(&infoPtr->iconStateSize, infoPtr->himlState, TRUE);
6959 /* init item size to avoid division by 0 */
6960 LISTVIEW_UpdateItemSize (infoPtr);
6962 if (uView == LVS_REPORT)
6964 if (!(LVS_NOCOLUMNHEADER & lpcs->style))
6966 ShowWindow(infoPtr->hwndHeader, SW_SHOWNORMAL);
6968 else
6970 /* set HDS_HIDDEN flag to hide the header bar */
6971 SetWindowLongW(infoPtr->hwndHeader, GWL_STYLE,
6972 GetWindowLongW(infoPtr->hwndHeader, GWL_STYLE) | HDS_HIDDEN);
6976 return 0;
6979 /***
6980 * DESCRIPTION:
6981 * Erases the background of the listview control.
6983 * PARAMETER(S):
6984 * [I] infoPtr : valid pointer to the listview structure
6985 * [I] hdc : device context handle
6987 * RETURN:
6988 * SUCCESS : TRUE
6989 * FAILURE : FALSE
6991 static inline BOOL LISTVIEW_EraseBkgnd(LISTVIEW_INFO *infoPtr, HDC hdc)
6993 RECT rc;
6995 TRACE("(hdc=%p)\n", hdc);
6997 if (!GetClipBox(hdc, &rc)) return FALSE;
6999 return LISTVIEW_FillBkgnd(infoPtr, hdc, &rc);
7003 /***
7004 * DESCRIPTION:
7005 * Helper function for LISTVIEW_[HV]Scroll *only*.
7006 * Performs vertical/horizontal scrolling by a give amount.
7008 * PARAMETER(S):
7009 * [I] infoPtr : valid pointer to the listview structure
7010 * [I] dx : amount of horizontal scroll
7011 * [I] dy : amount of vertical scroll
7013 static void scroll_list(LISTVIEW_INFO *infoPtr, INT dx, INT dy)
7015 /* now we can scroll the list */
7016 ScrollWindowEx(infoPtr->hwndSelf, dx, dy, &infoPtr->rcList,
7017 &infoPtr->rcList, 0, 0, SW_ERASE | SW_INVALIDATE);
7018 /* if we have focus, adjust rect */
7019 OffsetRect(&infoPtr->rcFocus, dx, dy);
7020 UpdateWindow(infoPtr->hwndSelf);
7023 /***
7024 * DESCRIPTION:
7025 * Performs vertical scrolling.
7027 * PARAMETER(S):
7028 * [I] infoPtr : valid pointer to the listview structure
7029 * [I] nScrollCode : scroll code
7030 * [I] nScrollDiff : units to scroll in SB_INTERNAL mode, 0 otherwise
7031 * [I] hScrollWnd : scrollbar control window handle
7033 * RETURN:
7034 * Zero
7036 * NOTES:
7037 * SB_LINEUP/SB_LINEDOWN:
7038 * for LVS_ICON, LVS_SMALLICON is 37 by experiment
7039 * for LVS_REPORT is 1 line
7040 * for LVS_LIST cannot occur
7043 static LRESULT LISTVIEW_VScroll(LISTVIEW_INFO *infoPtr, INT nScrollCode,
7044 INT nScrollDiff, HWND hScrollWnd)
7046 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
7047 INT nOldScrollPos, nNewScrollPos;
7048 SCROLLINFO scrollInfo;
7049 BOOL is_an_icon;
7051 TRACE("(nScrollCode=%d, nScrollDiff=%d)\n", nScrollCode, nScrollDiff);
7053 if (infoPtr->hwndEdit) SendMessageW(infoPtr->hwndEdit, WM_KILLFOCUS, 0, 0);
7055 scrollInfo.cbSize = sizeof(SCROLLINFO);
7056 scrollInfo.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_TRACKPOS;
7058 is_an_icon = ((uView == LVS_ICON) || (uView == LVS_SMALLICON));
7060 if (!GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo)) return 1;
7062 nOldScrollPos = scrollInfo.nPos;
7063 switch (nScrollCode)
7065 case SB_INTERNAL:
7066 break;
7068 case SB_LINEUP:
7069 nScrollDiff = (is_an_icon) ? -LISTVIEW_SCROLL_ICON_LINE_SIZE : -1;
7070 break;
7072 case SB_LINEDOWN:
7073 nScrollDiff = (is_an_icon) ? LISTVIEW_SCROLL_ICON_LINE_SIZE : 1;
7074 break;
7076 case SB_PAGEUP:
7077 nScrollDiff = -scrollInfo.nPage;
7078 break;
7080 case SB_PAGEDOWN:
7081 nScrollDiff = scrollInfo.nPage;
7082 break;
7084 case SB_THUMBPOSITION:
7085 case SB_THUMBTRACK:
7086 nScrollDiff = scrollInfo.nTrackPos - scrollInfo.nPos;
7087 break;
7089 default:
7090 nScrollDiff = 0;
7093 /* quit right away if pos isn't changing */
7094 if (nScrollDiff == 0) return 0;
7096 /* calculate new position, and handle overflows */
7097 nNewScrollPos = scrollInfo.nPos + nScrollDiff;
7098 if (nScrollDiff > 0) {
7099 if (nNewScrollPos < nOldScrollPos ||
7100 nNewScrollPos > scrollInfo.nMax)
7101 nNewScrollPos = scrollInfo.nMax;
7102 } else {
7103 if (nNewScrollPos > nOldScrollPos ||
7104 nNewScrollPos < scrollInfo.nMin)
7105 nNewScrollPos = scrollInfo.nMin;
7108 /* set the new position, and reread in case it changed */
7109 scrollInfo.fMask = SIF_POS;
7110 scrollInfo.nPos = nNewScrollPos;
7111 nNewScrollPos = SetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo, TRUE);
7113 /* carry on only if it really changed */
7114 if (nNewScrollPos == nOldScrollPos) return 0;
7116 /* now adjust to client coordinates */
7117 nScrollDiff = nOldScrollPos - nNewScrollPos;
7118 if (uView == LVS_REPORT) nScrollDiff *= infoPtr->nItemHeight;
7120 /* and scroll the window */
7121 scroll_list(infoPtr, 0, nScrollDiff);
7123 return 0;
7126 /***
7127 * DESCRIPTION:
7128 * Performs horizontal scrolling.
7130 * PARAMETER(S):
7131 * [I] infoPtr : valid pointer to the listview structure
7132 * [I] nScrollCode : scroll code
7133 * [I] nScrollDiff : units to scroll in SB_INTERNAL mode, 0 otherwise
7134 * [I] hScrollWnd : scrollbar control window handle
7136 * RETURN:
7137 * Zero
7139 * NOTES:
7140 * SB_LINELEFT/SB_LINERIGHT:
7141 * for LVS_ICON, LVS_SMALLICON 1 pixel
7142 * for LVS_REPORT is 1 pixel
7143 * for LVS_LIST is 1 column --> which is a 1 because the
7144 * scroll is based on columns not pixels
7147 static LRESULT LISTVIEW_HScroll(LISTVIEW_INFO *infoPtr, INT nScrollCode,
7148 INT nScrollDiff, HWND hScrollWnd)
7150 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
7151 INT nOldScrollPos, nNewScrollPos;
7152 SCROLLINFO scrollInfo;
7154 TRACE("(nScrollCode=%d, nScrollDiff=%d)\n", nScrollCode, nScrollDiff);
7156 if (infoPtr->hwndEdit) SendMessageW(infoPtr->hwndEdit, WM_KILLFOCUS, 0, 0);
7158 scrollInfo.cbSize = sizeof(SCROLLINFO);
7159 scrollInfo.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_TRACKPOS;
7161 if (!GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo)) return 1;
7163 nOldScrollPos = scrollInfo.nPos;
7165 switch (nScrollCode)
7167 case SB_INTERNAL:
7168 break;
7170 case SB_LINELEFT:
7171 nScrollDiff = -1;
7172 break;
7174 case SB_LINERIGHT:
7175 nScrollDiff = 1;
7176 break;
7178 case SB_PAGELEFT:
7179 nScrollDiff = -scrollInfo.nPage;
7180 break;
7182 case SB_PAGERIGHT:
7183 nScrollDiff = scrollInfo.nPage;
7184 break;
7186 case SB_THUMBPOSITION:
7187 case SB_THUMBTRACK:
7188 nScrollDiff = scrollInfo.nTrackPos - scrollInfo.nPos;
7189 break;
7191 default:
7192 nScrollDiff = 0;
7195 /* quit right away if pos isn't changing */
7196 if (nScrollDiff == 0) return 0;
7198 /* calculate new position, and handle overflows */
7199 nNewScrollPos = scrollInfo.nPos + nScrollDiff;
7200 if (nScrollDiff > 0) {
7201 if (nNewScrollPos < nOldScrollPos ||
7202 nNewScrollPos > scrollInfo.nMax)
7203 nNewScrollPos = scrollInfo.nMax;
7204 } else {
7205 if (nNewScrollPos > nOldScrollPos ||
7206 nNewScrollPos < scrollInfo.nMin)
7207 nNewScrollPos = scrollInfo.nMin;
7210 /* set the new position, and reread in case it changed */
7211 scrollInfo.fMask = SIF_POS;
7212 scrollInfo.nPos = nNewScrollPos;
7213 nNewScrollPos = SetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo, TRUE);
7215 /* carry on only if it really changed */
7216 if (nNewScrollPos == nOldScrollPos) return 0;
7218 if(uView == LVS_REPORT)
7219 LISTVIEW_UpdateHeaderSize(infoPtr, nNewScrollPos);
7221 /* now adjust to client coordinates */
7222 nScrollDiff = nOldScrollPos - nNewScrollPos;
7223 if (uView == LVS_LIST) nScrollDiff *= infoPtr->nItemWidth;
7225 /* and scroll the window */
7226 scroll_list(infoPtr, nScrollDiff, 0);
7228 return 0;
7231 static LRESULT LISTVIEW_MouseWheel(LISTVIEW_INFO *infoPtr, INT wheelDelta)
7233 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
7234 INT gcWheelDelta = 0;
7235 UINT pulScrollLines = 3;
7236 SCROLLINFO scrollInfo;
7238 TRACE("(wheelDelta=%d)\n", wheelDelta);
7240 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES,0, &pulScrollLines, 0);
7241 gcWheelDelta -= wheelDelta;
7243 scrollInfo.cbSize = sizeof(SCROLLINFO);
7244 scrollInfo.fMask = SIF_POS;
7246 switch(uView)
7248 case LVS_ICON:
7249 case LVS_SMALLICON:
7251 * listview should be scrolled by a multiple of 37 dependently on its dimension or its visible item number
7252 * should be fixed in the future.
7254 if (GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo))
7255 LISTVIEW_VScroll(infoPtr, SB_THUMBPOSITION,
7256 scrollInfo.nPos + (gcWheelDelta < 0) ?
7257 LISTVIEW_SCROLL_ICON_LINE_SIZE :
7258 -LISTVIEW_SCROLL_ICON_LINE_SIZE, 0);
7259 break;
7261 case LVS_REPORT:
7262 if (abs(gcWheelDelta) >= WHEEL_DELTA && pulScrollLines)
7264 if (GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo))
7266 int cLineScroll = min(LISTVIEW_GetCountPerColumn(infoPtr), pulScrollLines);
7267 cLineScroll *= (gcWheelDelta / WHEEL_DELTA);
7268 LISTVIEW_VScroll(infoPtr, SB_THUMBPOSITION, scrollInfo.nPos + cLineScroll, 0);
7271 break;
7273 case LVS_LIST:
7274 LISTVIEW_HScroll(infoPtr, (gcWheelDelta < 0) ? SB_LINELEFT : SB_LINERIGHT, 0, 0);
7275 break;
7277 return 0;
7280 /***
7281 * DESCRIPTION:
7282 * ???
7284 * PARAMETER(S):
7285 * [I] infoPtr : valid pointer to the listview structure
7286 * [I] nVirtualKey : virtual key
7287 * [I] lKeyData : key data
7289 * RETURN:
7290 * Zero
7292 static LRESULT LISTVIEW_KeyDown(LISTVIEW_INFO *infoPtr, INT nVirtualKey, LONG lKeyData)
7294 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
7295 INT nItem = -1;
7296 NMLVKEYDOWN nmKeyDown;
7298 TRACE("(nVirtualKey=%d, lKeyData=%ld)\n", nVirtualKey, lKeyData);
7300 /* send LVN_KEYDOWN notification */
7301 nmKeyDown.wVKey = nVirtualKey;
7302 nmKeyDown.flags = 0;
7303 notify_hdr(infoPtr, LVN_KEYDOWN, &nmKeyDown.hdr);
7305 switch (nVirtualKey)
7307 case VK_RETURN:
7308 if ((infoPtr->nItemCount > 0) && (infoPtr->nFocusedItem != -1))
7310 notify(infoPtr, NM_RETURN);
7311 notify(infoPtr, LVN_ITEMACTIVATE);
7313 break;
7315 case VK_HOME:
7316 if (infoPtr->nItemCount > 0)
7317 nItem = 0;
7318 break;
7320 case VK_END:
7321 if (infoPtr->nItemCount > 0)
7322 nItem = infoPtr->nItemCount - 1;
7323 break;
7325 case VK_LEFT:
7326 nItem = ListView_GetNextItem(infoPtr->hwndSelf, infoPtr->nFocusedItem, LVNI_TOLEFT);
7327 break;
7329 case VK_UP:
7330 nItem = ListView_GetNextItem(infoPtr->hwndSelf, infoPtr->nFocusedItem, LVNI_ABOVE);
7331 break;
7333 case VK_RIGHT:
7334 nItem = ListView_GetNextItem(infoPtr->hwndSelf, infoPtr->nFocusedItem, LVNI_TORIGHT);
7335 break;
7337 case VK_DOWN:
7338 nItem = ListView_GetNextItem(infoPtr->hwndSelf, infoPtr->nFocusedItem, LVNI_BELOW);
7339 break;
7341 case VK_PRIOR:
7342 if (uView == LVS_REPORT)
7343 nItem = infoPtr->nFocusedItem - LISTVIEW_GetCountPerColumn(infoPtr);
7344 else
7345 nItem = infoPtr->nFocusedItem - LISTVIEW_GetCountPerColumn(infoPtr)
7346 * LISTVIEW_GetCountPerRow(infoPtr);
7347 if(nItem < 0) nItem = 0;
7348 break;
7350 case VK_NEXT:
7351 if (uView == LVS_REPORT)
7352 nItem = infoPtr->nFocusedItem + LISTVIEW_GetCountPerColumn(infoPtr);
7353 else
7354 nItem = infoPtr->nFocusedItem + LISTVIEW_GetCountPerColumn(infoPtr)
7355 * LISTVIEW_GetCountPerRow(infoPtr);
7356 if(nItem >= infoPtr->nItemCount) nItem = infoPtr->nItemCount - 1;
7357 break;
7360 if ((nItem != -1) && (nItem != infoPtr->nFocusedItem))
7361 LISTVIEW_KeySelection(infoPtr, nItem);
7363 return 0;
7366 /***
7367 * DESCRIPTION:
7368 * Kills the focus.
7370 * PARAMETER(S):
7371 * [I] infoPtr : valid pointer to the listview structure
7373 * RETURN:
7374 * Zero
7376 static LRESULT LISTVIEW_KillFocus(LISTVIEW_INFO *infoPtr)
7378 TRACE("()\n");
7380 /* if we did not have the focus, there's nothing to do */
7381 if (!infoPtr->bFocus) return 0;
7383 /* send NM_KILLFOCUS notification */
7384 notify(infoPtr, NM_KILLFOCUS);
7386 /* if we have a focus rectagle, get rid of it */
7387 LISTVIEW_ShowFocusRect(infoPtr, FALSE);
7389 /* set window focus flag */
7390 infoPtr->bFocus = FALSE;
7392 /* invalidate the selected items before reseting focus flag */
7393 LISTVIEW_InvalidateSelectedItems(infoPtr);
7395 return 0;
7398 /***
7399 * DESCRIPTION:
7400 * Processes double click messages (left mouse button).
7402 * PARAMETER(S):
7403 * [I] infoPtr : valid pointer to the listview structure
7404 * [I] wKey : key flag
7405 * [I] pts : mouse coordinate
7407 * RETURN:
7408 * Zero
7410 static LRESULT LISTVIEW_LButtonDblClk(LISTVIEW_INFO *infoPtr, WORD wKey, POINTS pts)
7412 LVHITTESTINFO htInfo;
7414 TRACE("(key=%hu, X=%hu, Y=%hu)\n", wKey, pts.x, pts.y);
7416 /* send NM_RELEASEDCAPTURE notification */
7417 notify(infoPtr, NM_RELEASEDCAPTURE);
7419 htInfo.pt.x = pts.x;
7420 htInfo.pt.y = pts.y;
7422 /* send NM_DBLCLK notification */
7423 LISTVIEW_HitTest(infoPtr, &htInfo, TRUE, FALSE);
7424 notify_click(infoPtr, NM_DBLCLK, &htInfo);
7426 /* To send the LVN_ITEMACTIVATE, it must be on an Item */
7427 if(htInfo.iItem != -1) notify(infoPtr, LVN_ITEMACTIVATE);
7429 return 0;
7432 /***
7433 * DESCRIPTION:
7434 * Processes mouse down messages (left mouse button).
7436 * PARAMETER(S):
7437 * [I] infoPtr : valid pointer to the listview structure
7438 * [I] wKey : key flag
7439 * [I] pts : mouse coordinate
7441 * RETURN:
7442 * Zero
7444 static LRESULT LISTVIEW_LButtonDown(LISTVIEW_INFO *infoPtr, WORD wKey, POINTS pts)
7446 LVHITTESTINFO lvHitTestInfo;
7447 static BOOL bGroupSelect = TRUE;
7448 POINT pt = { pts.x, pts.y };
7449 INT nItem;
7451 TRACE("(key=%hu, X=%hu, Y=%hu)\n", wKey, pts.x, pts.y);
7453 /* send NM_RELEASEDCAPTURE notification */
7454 notify(infoPtr, NM_RELEASEDCAPTURE);
7456 if (!infoPtr->bFocus) SetFocus(infoPtr->hwndSelf);
7458 /* set left button down flag */
7459 infoPtr->bLButtonDown = TRUE;
7461 lvHitTestInfo.pt.x = pts.x;
7462 lvHitTestInfo.pt.y = pts.y;
7464 nItem = LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, TRUE);
7465 TRACE("at %s, nItem=%d\n", debugpoint(&pt), nItem);
7466 infoPtr->nEditLabelItem = -1;
7467 if ((nItem >= 0) && (nItem < infoPtr->nItemCount))
7469 if (infoPtr->dwStyle & LVS_SINGLESEL)
7471 if (LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED))
7472 infoPtr->nEditLabelItem = nItem;
7473 else
7474 LISTVIEW_SetSelection(infoPtr, nItem);
7476 else
7478 if ((wKey & MK_CONTROL) && (wKey & MK_SHIFT))
7480 if (bGroupSelect)
7482 LISTVIEW_AddGroupSelection(infoPtr, nItem);
7483 LISTVIEW_SetItemFocus(infoPtr, nItem);
7484 infoPtr->nSelectionMark = nItem;
7486 else
7488 LVITEMW item;
7490 item.state = LVIS_SELECTED | LVIS_FOCUSED;
7491 item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
7493 LISTVIEW_SetItemState(infoPtr,nItem,&item);
7494 infoPtr->nSelectionMark = nItem;
7497 else if (wKey & MK_CONTROL)
7499 LVITEMW item;
7501 bGroupSelect = (LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED) == 0);
7503 item.state = (bGroupSelect ? LVIS_SELECTED : 0) | LVIS_FOCUSED;
7504 item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
7505 LISTVIEW_SetItemState(infoPtr, nItem, &item);
7506 infoPtr->nSelectionMark = nItem;
7508 else if (wKey & MK_SHIFT)
7510 LISTVIEW_SetGroupSelection(infoPtr, nItem);
7512 else
7514 if (LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED))
7515 infoPtr->nEditLabelItem = nItem;
7517 /* set selection (clears other pre-existing selections) */
7518 LISTVIEW_SetSelection(infoPtr, nItem);
7522 else
7524 /* remove all selections */
7525 LISTVIEW_DeselectAll(infoPtr);
7528 return 0;
7531 /***
7532 * DESCRIPTION:
7533 * Processes mouse up messages (left mouse button).
7535 * PARAMETER(S):
7536 * [I] infoPtr : valid pointer to the listview structure
7537 * [I] wKey : key flag
7538 * [I] pts : mouse coordinate
7540 * RETURN:
7541 * Zero
7543 static LRESULT LISTVIEW_LButtonUp(LISTVIEW_INFO *infoPtr, WORD wKey, POINTS pts)
7545 LVHITTESTINFO lvHitTestInfo;
7547 TRACE("(key=%hu, X=%hu, Y=%hu)\n", wKey, pts.x, pts.y);
7549 if (!infoPtr->bLButtonDown) return 0;
7551 lvHitTestInfo.pt.x = pts.x;
7552 lvHitTestInfo.pt.y = pts.y;
7554 /* send NM_CLICK notification */
7555 LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, FALSE);
7556 notify_click(infoPtr, NM_CLICK, &lvHitTestInfo);
7558 /* set left button flag */
7559 infoPtr->bLButtonDown = FALSE;
7561 /* if we clicked on a selected item, edit the label */
7562 if(lvHitTestInfo.iItem == infoPtr->nEditLabelItem && (lvHitTestInfo.flags & LVHT_ONITEMLABEL))
7563 LISTVIEW_EditLabelT(infoPtr, lvHitTestInfo.iItem, TRUE);
7565 return 0;
7568 /***
7569 * DESCRIPTION:
7570 * Destroys the listview control (called after WM_DESTROY).
7572 * PARAMETER(S):
7573 * [I] infoPtr : valid pointer to the listview structure
7575 * RETURN:
7576 * Zero
7578 static LRESULT LISTVIEW_NCDestroy(LISTVIEW_INFO *infoPtr)
7580 TRACE("()\n");
7582 /* delete all items */
7583 LISTVIEW_DeleteAllItems(infoPtr);
7585 /* destroy data structure */
7586 DPA_Destroy(infoPtr->hdpaItems);
7587 if (infoPtr->selectionRanges) ranges_destroy(infoPtr->selectionRanges);
7589 /* destroy image lists */
7590 if (!(infoPtr->dwStyle & LVS_SHAREIMAGELISTS))
7592 if (infoPtr->himlNormal)
7593 ImageList_Destroy(infoPtr->himlNormal);
7594 if (infoPtr->himlSmall)
7595 ImageList_Destroy(infoPtr->himlSmall);
7596 if (infoPtr->himlState)
7597 ImageList_Destroy(infoPtr->himlState);
7600 /* destroy font, bkgnd brush */
7601 infoPtr->hFont = 0;
7602 if (infoPtr->hDefaultFont) DeleteObject(infoPtr->hDefaultFont);
7603 if (infoPtr->clrBk != CLR_NONE) DeleteObject(infoPtr->hBkBrush);
7605 /* free listview info pointer*/
7606 COMCTL32_Free(infoPtr);
7608 SetWindowLongW(infoPtr->hwndSelf, 0, 0);
7609 return 0;
7612 /***
7613 * DESCRIPTION:
7614 * Handles notifications from header.
7616 * PARAMETER(S):
7617 * [I] infoPtr : valid pointer to the listview structure
7618 * [I] nCtrlId : control identifier
7619 * [I] lpnmh : notification information
7621 * RETURN:
7622 * Zero
7624 static LRESULT LISTVIEW_HeaderNotification(LISTVIEW_INFO *infoPtr, LPNMHEADERW lpnmh)
7626 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
7628 TRACE("(lpnmh=%p)\n", lpnmh);
7630 if (!lpnmh || lpnmh->iItem < 0 || lpnmh->iItem >= infoPtr->hdpaColumns->nItemCount) return 0;
7632 switch (lpnmh->hdr.code)
7634 case HDN_TRACKW:
7635 case HDN_TRACKA:
7636 case HDN_ITEMCHANGEDW:
7637 case HDN_ITEMCHANGEDA:
7639 COLUMN_INFO *lpColumnInfo;
7640 INT dx, cxy;
7642 if (!lpnmh->pitem || !(lpnmh->pitem->mask & HDI_WIDTH))
7644 HDITEMW hdi;
7646 hdi.mask = HDI_WIDTH;
7647 if (!Header_GetItemW(infoPtr->hwndHeader, lpnmh->iItem, (LPARAM)&hdi)) return 0;
7648 cxy = hdi.cxy;
7650 else
7651 cxy = lpnmh->pitem->cxy;
7653 /* determine how much we change since the last know position */
7654 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, lpnmh->iItem);
7655 dx = cxy - (lpColumnInfo->rcHeader.right - lpColumnInfo->rcHeader.left);
7656 if (dx != 0)
7658 RECT rcCol = lpColumnInfo->rcHeader;
7660 lpColumnInfo->rcHeader.right += dx;
7661 LISTVIEW_ScrollColumns(infoPtr, lpnmh->iItem + 1, dx);
7662 if (uView == LVS_REPORT && is_redrawing(infoPtr))
7664 rcCol.right = min (rcCol.right, lpColumnInfo->rcHeader.right);
7665 rcCol.left = max (rcCol.left, rcCol.right - 3 * infoPtr->ntmAveCharWidth);
7666 rcCol.top = infoPtr->rcList.top;
7667 rcCol.bottom = infoPtr->rcList.bottom;
7668 LISTVIEW_InvalidateRect(infoPtr, &rcCol);
7672 break;
7674 case HDN_ITEMCLICKW:
7675 case HDN_ITEMCLICKA:
7677 /* Handle sorting by Header Column */
7678 NMLISTVIEW nmlv;
7680 ZeroMemory(&nmlv, sizeof(NMLISTVIEW));
7681 nmlv.iItem = -1;
7682 nmlv.iSubItem = lpnmh->iItem;
7683 notify_listview(infoPtr, LVN_COLUMNCLICK, &nmlv);
7685 break;
7688 return 0;
7691 /***
7692 * DESCRIPTION:
7693 * Determines the type of structure to use.
7695 * PARAMETER(S):
7696 * [I] infoPtr : valid pointer to the listview structureof the sender
7697 * [I] hwndFrom : listview window handle
7698 * [I] nCommand : command specifying the nature of the WM_NOTIFYFORMAT
7700 * RETURN:
7701 * Zero
7703 static LRESULT LISTVIEW_NotifyFormat(LISTVIEW_INFO *infoPtr, HWND hwndFrom, INT nCommand)
7705 TRACE("(hwndFrom=%p, nCommand=%d)\n", hwndFrom, nCommand);
7707 if (nCommand != NF_REQUERY) return 0;
7709 infoPtr->notifyFormat = SendMessageW(hwndFrom, WM_NOTIFYFORMAT, (WPARAM)infoPtr->hwndSelf, NF_QUERY);
7711 return 0;
7714 /***
7715 * DESCRIPTION:
7716 * Paints/Repaints the listview control.
7718 * PARAMETER(S):
7719 * [I] infoPtr : valid pointer to the listview structure
7720 * [I] hdc : device context handle
7722 * RETURN:
7723 * Zero
7725 static LRESULT LISTVIEW_Paint(LISTVIEW_INFO *infoPtr, HDC hdc)
7727 TRACE("(hdc=%p)\n", hdc);
7729 if (infoPtr->bFirstPaint)
7731 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
7733 infoPtr->bFirstPaint = FALSE;
7734 LISTVIEW_UpdateSize(infoPtr);
7735 LISTVIEW_UpdateItemSize(infoPtr);
7736 if (uView == LVS_ICON || uView == LVS_SMALLICON)
7737 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
7738 LISTVIEW_UpdateScroll(infoPtr);
7740 if (hdc)
7741 LISTVIEW_Refresh(infoPtr, hdc);
7742 else
7744 PAINTSTRUCT ps;
7746 hdc = BeginPaint(infoPtr->hwndSelf, &ps);
7747 if (!hdc) return 1;
7748 if (ps.fErase) LISTVIEW_FillBkgnd(infoPtr, hdc, &ps.rcPaint);
7749 LISTVIEW_Refresh(infoPtr, hdc);
7750 EndPaint(infoPtr->hwndSelf, &ps);
7753 return 0;
7756 /***
7757 * DESCRIPTION:
7758 * Processes double click messages (right mouse button).
7760 * PARAMETER(S):
7761 * [I] infoPtr : valid pointer to the listview structure
7762 * [I] wKey : key flag
7763 * [I] pts : mouse coordinate
7765 * RETURN:
7766 * Zero
7768 static LRESULT LISTVIEW_RButtonDblClk(LISTVIEW_INFO *infoPtr, WORD wKey, POINTS pts)
7770 LVHITTESTINFO lvHitTestInfo;
7772 TRACE("(key=%hu,X=%hu,Y=%hu)\n", wKey, pts.x, pts.y);
7774 /* send NM_RELEASEDCAPTURE notification */
7775 notify(infoPtr, NM_RELEASEDCAPTURE);
7777 /* send NM_RDBLCLK notification */
7778 lvHitTestInfo.pt.x = pts.x;
7779 lvHitTestInfo.pt.y = pts.y;
7780 LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, FALSE);
7781 notify_click(infoPtr, NM_RDBLCLK, &lvHitTestInfo);
7783 return 0;
7786 /***
7787 * DESCRIPTION:
7788 * Processes mouse down messages (right mouse button).
7790 * PARAMETER(S):
7791 * [I] infoPtr : valid pointer to the listview structure
7792 * [I] wKey : key flag
7793 * [I] pts : mouse coordinate
7795 * RETURN:
7796 * Zero
7798 static LRESULT LISTVIEW_RButtonDown(LISTVIEW_INFO *infoPtr, WORD wKey, POINTS pts)
7800 LVHITTESTINFO lvHitTestInfo;
7801 INT nItem;
7803 TRACE("(key=%hu,X=%hu,Y=%hu)\n", wKey, pts.x, pts.y);
7805 /* send NM_RELEASEDCAPTURE notification */
7806 notify(infoPtr, NM_RELEASEDCAPTURE);
7808 /* make sure the listview control window has the focus */
7809 if (!infoPtr->bFocus) SetFocus(infoPtr->hwndSelf);
7811 /* set right button down flag */
7812 infoPtr->bRButtonDown = TRUE;
7814 /* determine the index of the selected item */
7815 lvHitTestInfo.pt.x = pts.x;
7816 lvHitTestInfo.pt.y = pts.y;
7817 nItem = LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, TRUE);
7819 if ((nItem >= 0) && (nItem < infoPtr->nItemCount))
7821 LISTVIEW_SetItemFocus(infoPtr, nItem);
7822 if (!((wKey & MK_SHIFT) || (wKey & MK_CONTROL)) &&
7823 !LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED))
7824 LISTVIEW_SetSelection(infoPtr, nItem);
7826 else
7828 LISTVIEW_DeselectAll(infoPtr);
7831 return 0;
7834 /***
7835 * DESCRIPTION:
7836 * Processes mouse up messages (right mouse button).
7838 * PARAMETER(S):
7839 * [I] infoPtr : valid pointer to the listview structure
7840 * [I] wKey : key flag
7841 * [I] pts : mouse coordinate
7843 * RETURN:
7844 * Zero
7846 static LRESULT LISTVIEW_RButtonUp(LISTVIEW_INFO *infoPtr, WORD wKey, POINTS pts)
7848 LVHITTESTINFO lvHitTestInfo;
7849 POINT pt;
7851 TRACE("(key=%hu,X=%hu,Y=%hu)\n", wKey, pts.x, pts.y);
7853 if (!infoPtr->bRButtonDown) return 0;
7855 /* set button flag */
7856 infoPtr->bRButtonDown = FALSE;
7858 /* Send NM_RClICK notification */
7859 lvHitTestInfo.pt.x = pts.x;
7860 lvHitTestInfo.pt.y = pts.y;
7861 LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, FALSE);
7862 notify_click(infoPtr, NM_RCLICK, &lvHitTestInfo);
7864 /* Change to screen coordinate for WM_CONTEXTMENU */
7865 pt = lvHitTestInfo.pt;
7866 ClientToScreen(infoPtr->hwndSelf, &pt);
7868 /* Send a WM_CONTEXTMENU message in response to the RBUTTONUP */
7869 SendMessageW(infoPtr->hwndSelf, WM_CONTEXTMENU,
7870 (WPARAM)infoPtr->hwndSelf, MAKELPARAM(pt.x, pt.y));
7872 return 0;
7876 /***
7877 * DESCRIPTION:
7878 * Sets the cursor.
7880 * PARAMETER(S):
7881 * [I] infoPtr : valid pointer to the listview structure
7882 * [I] hwnd : window handle of window containing the cursor
7883 * [I] nHittest : hit-test code
7884 * [I] wMouseMsg : ideintifier of the mouse message
7886 * RETURN:
7887 * TRUE if cursor is set
7888 * FALSE otherwise
7890 static BOOL LISTVIEW_SetCursor(LISTVIEW_INFO *infoPtr, HWND hwnd, UINT nHittest, UINT wMouseMsg)
7892 LVHITTESTINFO lvHitTestInfo;
7894 if(!(infoPtr->dwLvExStyle & LVS_EX_TRACKSELECT)) return FALSE;
7896 if(!infoPtr->hHotCursor) return FALSE;
7898 GetCursorPos(&lvHitTestInfo.pt);
7899 if (LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, FALSE, FALSE) < 0) return FALSE;
7901 SetCursor(infoPtr->hHotCursor);
7903 return TRUE;
7906 /***
7907 * DESCRIPTION:
7908 * Sets the focus.
7910 * PARAMETER(S):
7911 * [I] infoPtr : valid pointer to the listview structure
7912 * [I] hwndLoseFocus : handle of previously focused window
7914 * RETURN:
7915 * Zero
7917 static LRESULT LISTVIEW_SetFocus(LISTVIEW_INFO *infoPtr, HWND hwndLoseFocus)
7919 TRACE("(hwndLoseFocus=%p)\n", hwndLoseFocus);
7921 /* if we have the focus already, there's nothing to do */
7922 if (infoPtr->bFocus) return 0;
7924 /* send NM_SETFOCUS notification */
7925 notify(infoPtr, NM_SETFOCUS);
7927 /* set window focus flag */
7928 infoPtr->bFocus = TRUE;
7930 /* put the focus rect back on */
7931 LISTVIEW_ShowFocusRect(infoPtr, TRUE);
7933 /* redraw all visible selected items */
7934 LISTVIEW_InvalidateSelectedItems(infoPtr);
7936 return 0;
7939 /***
7940 * DESCRIPTION:
7941 * Sets the font.
7943 * PARAMETER(S):
7944 * [I] infoPtr : valid pointer to the listview structure
7945 * [I] fRedraw : font handle
7946 * [I] fRedraw : redraw flag
7948 * RETURN:
7949 * Zero
7951 static LRESULT LISTVIEW_SetFont(LISTVIEW_INFO *infoPtr, HFONT hFont, WORD fRedraw)
7953 HFONT oldFont = infoPtr->hFont;
7955 TRACE("(hfont=%p,redraw=%hu)\n", hFont, fRedraw);
7957 infoPtr->hFont = hFont ? hFont : infoPtr->hDefaultFont;
7958 if (infoPtr->hFont == oldFont) return 0;
7960 LISTVIEW_SaveTextMetrics(infoPtr);
7962 if ((infoPtr->dwStyle & LVS_TYPEMASK) == LVS_REPORT)
7963 SendMessageW(infoPtr->hwndHeader, WM_SETFONT, (WPARAM)hFont, MAKELPARAM(fRedraw, 0));
7965 if (fRedraw) LISTVIEW_InvalidateList(infoPtr);
7967 return 0;
7970 /***
7971 * DESCRIPTION:
7972 * Message handling for WM_SETREDRAW.
7973 * For the Listview, it invalidates the entire window (the doc specifies otherwise)
7975 * PARAMETER(S):
7976 * [I] infoPtr : valid pointer to the listview structure
7977 * [I] bRedraw: state of redraw flag
7979 * RETURN:
7980 * DefWinProc return value
7982 static LRESULT LISTVIEW_SetRedraw(LISTVIEW_INFO *infoPtr, BOOL bRedraw)
7984 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
7986 infoPtr->bRedraw = bRedraw;
7988 if(!bRedraw) return 0;
7990 LISTVIEW_UpdateSize(infoPtr);
7991 if (uView == LVS_ICON || uView == LVS_SMALLICON)
7992 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
7993 LISTVIEW_UpdateScroll(infoPtr);
7994 LISTVIEW_InvalidateList(infoPtr);
7996 return 0;
7999 /***
8000 * DESCRIPTION:
8001 * Resizes the listview control. This function processes WM_SIZE
8002 * messages. At this time, the width and height are not used.
8004 * PARAMETER(S):
8005 * [I] infoPtr : valid pointer to the listview structure
8006 * [I] Width : new width
8007 * [I] Height : new height
8009 * RETURN:
8010 * Zero
8012 static LRESULT LISTVIEW_Size(LISTVIEW_INFO *infoPtr, int Width, int Height)
8014 TRACE("(width=%d, height=%d)\n", Width, Height);
8016 if (!is_redrawing(infoPtr)) return 0;
8018 if (!LISTVIEW_UpdateSize(infoPtr)) return 0;
8020 if (is_autoarrange(infoPtr))
8021 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
8023 LISTVIEW_UpdateScroll(infoPtr);
8025 LISTVIEW_InvalidateList(infoPtr); /* FIXME: optimize */
8027 return 0;
8030 /***
8031 * DESCRIPTION:
8032 * Sets the size information.
8034 * PARAMETER(S):
8035 * [I] infoPtr : valid pointer to the listview structure
8037 * RETURN:
8038 * Zero if no size change
8039 * 1 of size changed
8041 static BOOL LISTVIEW_UpdateSize(LISTVIEW_INFO *infoPtr)
8043 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
8044 RECT rcList;
8045 RECT rcOld;
8047 GetClientRect(infoPtr->hwndSelf, &rcList);
8048 CopyRect(&rcOld,&(infoPtr->rcList));
8049 infoPtr->rcList.left = 0;
8050 infoPtr->rcList.right = max(rcList.right - rcList.left, 1);
8051 infoPtr->rcList.top = 0;
8052 infoPtr->rcList.bottom = max(rcList.bottom - rcList.top, 1);
8054 if (uView == LVS_LIST)
8056 /* Apparently the "LIST" style is supposed to have the same
8057 * number of items in a column even if there is no scroll bar.
8058 * Since if a scroll bar already exists then the bottom is already
8059 * reduced, only reduce if the scroll bar does not currently exist.
8060 * The "2" is there to mimic the native control. I think it may be
8061 * related to either padding or edges. (GLA 7/2002)
8063 if (!(infoPtr->dwStyle & WS_HSCROLL))
8065 INT nHScrollHeight = GetSystemMetrics(SM_CYHSCROLL);
8066 if (infoPtr->rcList.bottom > nHScrollHeight)
8067 infoPtr->rcList.bottom -= (nHScrollHeight + 2);
8069 else
8071 if (infoPtr->rcList.bottom > 2)
8072 infoPtr->rcList.bottom -= 2;
8075 else if (uView == LVS_REPORT)
8077 HDLAYOUT hl;
8078 WINDOWPOS wp;
8080 hl.prc = &rcList;
8081 hl.pwpos = &wp;
8082 Header_Layout(infoPtr->hwndHeader, &hl);
8084 SetWindowPos(wp.hwnd, wp.hwndInsertAfter, wp.x, wp.y, wp.cx, wp.cy, wp.flags);
8086 if (!(LVS_NOCOLUMNHEADER & infoPtr->dwStyle))
8087 infoPtr->rcList.top = max(wp.cy, 0);
8089 return (EqualRect(&rcOld,&(infoPtr->rcList)));
8092 /***
8093 * DESCRIPTION:
8094 * Processes WM_STYLECHANGED messages.
8096 * PARAMETER(S):
8097 * [I] infoPtr : valid pointer to the listview structure
8098 * [I] wStyleType : window style type (normal or extended)
8099 * [I] lpss : window style information
8101 * RETURN:
8102 * Zero
8104 static INT LISTVIEW_StyleChanged(LISTVIEW_INFO *infoPtr, WPARAM wStyleType,
8105 LPSTYLESTRUCT lpss)
8107 UINT uNewView = lpss->styleNew & LVS_TYPEMASK;
8108 UINT uOldView = lpss->styleOld & LVS_TYPEMASK;
8110 TRACE("(styletype=%x, styleOld=0x%08lx, styleNew=0x%08lx)\n",
8111 wStyleType, lpss->styleOld, lpss->styleNew);
8113 if (wStyleType != GWL_STYLE) return 0;
8115 /* FIXME: if LVS_NOSORTHEADER changed, update header */
8116 /* what if LVS_OWNERDATA changed? */
8117 /* or LVS_SINGLESEL */
8118 /* or LVS_SORT{AS,DES}CENDING */
8120 infoPtr->dwStyle = lpss->styleNew;
8122 if (((lpss->styleOld & WS_HSCROLL) != 0)&&
8123 ((lpss->styleNew & WS_HSCROLL) == 0))
8124 ShowScrollBar(infoPtr->hwndSelf, SB_HORZ, FALSE);
8126 if (((lpss->styleOld & WS_VSCROLL) != 0)&&
8127 ((lpss->styleNew & WS_VSCROLL) == 0))
8128 ShowScrollBar(infoPtr->hwndSelf, SB_VERT, FALSE);
8130 if (uNewView != uOldView)
8132 SIZE oldIconSize = infoPtr->iconSize;
8133 HIMAGELIST himl;
8135 SendMessageW(infoPtr->hwndEdit, WM_KILLFOCUS, 0, 0);
8136 ShowWindow(infoPtr->hwndHeader, SW_HIDE);
8138 ShowScrollBar(infoPtr->hwndSelf, SB_BOTH, FALSE);
8139 SetRectEmpty(&infoPtr->rcFocus);
8141 himl = (uNewView == LVS_ICON ? infoPtr->himlNormal : infoPtr->himlSmall);
8142 set_icon_size(&infoPtr->iconSize, himl, uNewView != LVS_ICON);
8144 if (uNewView == LVS_ICON)
8146 if ((infoPtr->iconSize.cx != oldIconSize.cx) || (infoPtr->iconSize.cy != oldIconSize.cy))
8148 TRACE("icon old size=(%ld,%ld), new size=(%ld,%ld)\n",
8149 oldIconSize.cx, oldIconSize.cy, infoPtr->iconSize.cx, infoPtr->iconSize.cy);
8150 LISTVIEW_SetIconSpacing(infoPtr, 0);
8153 else if (uNewView == LVS_REPORT)
8155 HDLAYOUT hl;
8156 WINDOWPOS wp;
8158 hl.prc = &infoPtr->rcList;
8159 hl.pwpos = &wp;
8160 Header_Layout(infoPtr->hwndHeader, &hl);
8161 SetWindowPos(infoPtr->hwndHeader, infoPtr->hwndSelf, wp.x, wp.y, wp.cx, wp.cy, wp.flags);
8164 LISTVIEW_UpdateItemSize(infoPtr);
8167 if (uNewView == LVS_REPORT)
8168 ShowWindow(infoPtr->hwndHeader, (LVS_NOCOLUMNHEADER & lpss->styleNew) ? SW_HIDE : SW_SHOWNORMAL);
8170 if ( (uNewView == LVS_ICON || uNewView == LVS_SMALLICON) &&
8171 (uNewView != uOldView || ((lpss->styleNew ^ lpss->styleOld) & LVS_ALIGNMASK)) )
8172 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
8174 /* update the size of the client area */
8175 LISTVIEW_UpdateSize(infoPtr);
8177 /* add scrollbars if needed */
8178 LISTVIEW_UpdateScroll(infoPtr);
8180 /* invalidate client area + erase background */
8181 LISTVIEW_InvalidateList(infoPtr);
8183 return 0;
8186 /***
8187 * DESCRIPTION:
8188 * Window procedure of the listview control.
8191 static LRESULT WINAPI
8192 LISTVIEW_WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
8194 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)GetWindowLongW(hwnd, 0);
8196 TRACE("(uMsg=%x wParam=%x lParam=%lx)\n", uMsg, wParam, lParam);
8198 if (!infoPtr && (uMsg != WM_CREATE))
8199 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
8201 if (infoPtr)
8203 infoPtr->dwStyle = GetWindowLongW(hwnd, GWL_STYLE);
8206 switch (uMsg)
8208 case LVM_APPROXIMATEVIEWRECT:
8209 return LISTVIEW_ApproximateViewRect(infoPtr, (INT)wParam,
8210 LOWORD(lParam), HIWORD(lParam));
8211 case LVM_ARRANGE:
8212 return LISTVIEW_Arrange(infoPtr, (INT)wParam);
8214 /* case LVM_CANCELEDITLABEL: */
8216 /* case LVM_CREATEDRAGIMAGE: */
8218 case LVM_DELETEALLITEMS:
8219 return LISTVIEW_DeleteAllItems(infoPtr);
8221 case LVM_DELETECOLUMN:
8222 return LISTVIEW_DeleteColumn(infoPtr, (INT)wParam);
8224 case LVM_DELETEITEM:
8225 return LISTVIEW_DeleteItem(infoPtr, (INT)wParam);
8227 case LVM_EDITLABELW:
8228 return (LRESULT)LISTVIEW_EditLabelT(infoPtr, (INT)wParam, TRUE);
8230 case LVM_EDITLABELA:
8231 return (LRESULT)LISTVIEW_EditLabelT(infoPtr, (INT)wParam, FALSE);
8233 /* case LVM_ENABLEGROUPVIEW: */
8235 case LVM_ENSUREVISIBLE:
8236 return LISTVIEW_EnsureVisible(infoPtr, (INT)wParam, (BOOL)lParam);
8238 case LVM_FINDITEMW:
8239 return LISTVIEW_FindItemW(infoPtr, (INT)wParam, (LPLVFINDINFOW)lParam);
8241 case LVM_FINDITEMA:
8242 return LISTVIEW_FindItemA(infoPtr, (INT)wParam, (LPLVFINDINFOA)lParam);
8244 case LVM_GETBKCOLOR:
8245 return infoPtr->clrBk;
8247 /* case LVM_GETBKIMAGE: */
8249 case LVM_GETCALLBACKMASK:
8250 return infoPtr->uCallbackMask;
8252 case LVM_GETCOLUMNA:
8253 return LISTVIEW_GetColumnT(infoPtr, (INT)wParam, (LPLVCOLUMNW)lParam, FALSE);
8255 case LVM_GETCOLUMNW:
8256 return LISTVIEW_GetColumnT(infoPtr, (INT)wParam, (LPLVCOLUMNW)lParam, TRUE);
8258 case LVM_GETCOLUMNORDERARRAY:
8259 return LISTVIEW_GetColumnOrderArray(infoPtr, (INT)wParam, (LPINT)lParam);
8261 case LVM_GETCOLUMNWIDTH:
8262 return LISTVIEW_GetColumnWidth(infoPtr, (INT)wParam);
8264 case LVM_GETCOUNTPERPAGE:
8265 return LISTVIEW_GetCountPerPage(infoPtr);
8267 case LVM_GETEDITCONTROL:
8268 return (LRESULT)infoPtr->hwndEdit;
8270 case LVM_GETEXTENDEDLISTVIEWSTYLE:
8271 return infoPtr->dwLvExStyle;
8273 /* case LVM_GETGROUPINFO: */
8275 /* case LVM_GETGROUPMETRICS: */
8277 case LVM_GETHEADER:
8278 return (LRESULT)infoPtr->hwndHeader;
8280 case LVM_GETHOTCURSOR:
8281 return (LRESULT)infoPtr->hHotCursor;
8283 case LVM_GETHOTITEM:
8284 return infoPtr->nHotItem;
8286 case LVM_GETHOVERTIME:
8287 return infoPtr->dwHoverTime;
8289 case LVM_GETIMAGELIST:
8290 return (LRESULT)LISTVIEW_GetImageList(infoPtr, (INT)wParam);
8292 /* case LVM_GETINSERTMARK: */
8294 /* case LVM_GETINSERTMARKCOLOR: */
8296 /* case LVM_GETINSERTMARKRECT: */
8298 case LVM_GETISEARCHSTRINGA:
8299 case LVM_GETISEARCHSTRINGW:
8300 FIXME("LVM_GETISEARCHSTRING: unimplemented\n");
8301 return FALSE;
8303 case LVM_GETITEMA:
8304 return LISTVIEW_GetItemExtT(infoPtr, (LPLVITEMW)lParam, FALSE);
8306 case LVM_GETITEMW:
8307 return LISTVIEW_GetItemExtT(infoPtr, (LPLVITEMW)lParam, TRUE);
8309 case LVM_GETITEMCOUNT:
8310 return infoPtr->nItemCount;
8312 case LVM_GETITEMPOSITION:
8313 return LISTVIEW_GetItemPosition(infoPtr, (INT)wParam, (LPPOINT)lParam);
8315 case LVM_GETITEMRECT:
8316 return LISTVIEW_GetItemRect(infoPtr, (INT)wParam, (LPRECT)lParam);
8318 case LVM_GETITEMSPACING:
8319 return LISTVIEW_GetItemSpacing(infoPtr, (BOOL)wParam);
8321 case LVM_GETITEMSTATE:
8322 return LISTVIEW_GetItemState(infoPtr, (INT)wParam, (UINT)lParam);
8324 case LVM_GETITEMTEXTA:
8325 return LISTVIEW_GetItemTextT(infoPtr, (INT)wParam, (LPLVITEMW)lParam, FALSE);
8327 case LVM_GETITEMTEXTW:
8328 return LISTVIEW_GetItemTextT(infoPtr, (INT)wParam, (LPLVITEMW)lParam, TRUE);
8330 case LVM_GETNEXTITEM:
8331 return LISTVIEW_GetNextItem(infoPtr, (INT)wParam, LOWORD(lParam));
8333 case LVM_GETNUMBEROFWORKAREAS:
8334 FIXME("LVM_GETNUMBEROFWORKAREAS: unimplemented\n");
8335 return 1;
8337 case LVM_GETORIGIN:
8338 if (!lParam) return FALSE;
8339 LISTVIEW_GetOrigin(infoPtr, (LPPOINT)lParam);
8340 return TRUE;
8342 /* case LVM_GETOUTLINECOLOR: */
8344 /* case LVM_GETSELECTEDCOLUMN: */
8346 case LVM_GETSELECTEDCOUNT:
8347 return LISTVIEW_GetSelectedCount(infoPtr);
8349 case LVM_GETSELECTIONMARK:
8350 return infoPtr->nSelectionMark;
8352 case LVM_GETSTRINGWIDTHA:
8353 return LISTVIEW_GetStringWidthT(infoPtr, (LPCWSTR)lParam, FALSE);
8355 case LVM_GETSTRINGWIDTHW:
8356 return LISTVIEW_GetStringWidthT(infoPtr, (LPCWSTR)lParam, TRUE);
8358 case LVM_GETSUBITEMRECT:
8359 return LISTVIEW_GetSubItemRect(infoPtr, (UINT)wParam, (LPRECT)lParam);
8361 case LVM_GETTEXTBKCOLOR:
8362 return infoPtr->clrTextBk;
8364 case LVM_GETTEXTCOLOR:
8365 return infoPtr->clrText;
8367 /* case LVM_GETTILEINFO: */
8369 /* case LVM_GETTILEVIEWINFO: */
8371 case LVM_GETTOOLTIPS:
8372 FIXME("LVM_GETTOOLTIPS: unimplemented\n");
8373 return FALSE;
8375 case LVM_GETTOPINDEX:
8376 return LISTVIEW_GetTopIndex(infoPtr);
8378 /*case LVM_GETUNICODEFORMAT:
8379 FIXME("LVM_GETUNICODEFORMAT: unimplemented\n");
8380 return FALSE;*/
8382 /* case LVM_GETVIEW: */
8384 case LVM_GETVIEWRECT:
8385 return LISTVIEW_GetViewRect(infoPtr, (LPRECT)lParam);
8387 case LVM_GETWORKAREAS:
8388 FIXME("LVM_GETWORKAREAS: unimplemented\n");
8389 return FALSE;
8391 /* case LVM_HASGROUP: */
8393 case LVM_HITTEST:
8394 return LISTVIEW_HitTest(infoPtr, (LPLVHITTESTINFO)lParam, FALSE, FALSE);
8396 case LVM_INSERTCOLUMNA:
8397 return LISTVIEW_InsertColumnT(infoPtr, (INT)wParam, (LPLVCOLUMNW)lParam, FALSE);
8399 case LVM_INSERTCOLUMNW:
8400 return LISTVIEW_InsertColumnT(infoPtr, (INT)wParam, (LPLVCOLUMNW)lParam, TRUE);
8402 /* case LVM_INSERTGROUP: */
8404 /* case LVM_INSERTGROUPSORTED: */
8406 case LVM_INSERTITEMA:
8407 return LISTVIEW_InsertItemT(infoPtr, (LPLVITEMW)lParam, FALSE);
8409 case LVM_INSERTITEMW:
8410 return LISTVIEW_InsertItemT(infoPtr, (LPLVITEMW)lParam, TRUE);
8412 /* case LVM_INSERTMARKHITTEST: */
8414 /* case LVM_ISGROUPVIEWENABLED: */
8416 /* case LVM_MAPIDTOINDEX: */
8418 /* case LVM_MAPINDEXTOID: */
8420 /* case LVM_MOVEGROUP: */
8422 /* case LVM_MOVEITEMTOGROUP: */
8424 case LVM_REDRAWITEMS:
8425 return LISTVIEW_RedrawItems(infoPtr, (INT)wParam, (INT)lParam);
8427 /* case LVM_REMOVEALLGROUPS: */
8429 /* case LVM_REMOVEGROUP: */
8431 case LVM_SCROLL:
8432 return LISTVIEW_Scroll(infoPtr, (INT)wParam, (INT)lParam);
8434 case LVM_SETBKCOLOR:
8435 return LISTVIEW_SetBkColor(infoPtr, (COLORREF)lParam);
8437 /* case LVM_SETBKIMAGE: */
8439 case LVM_SETCALLBACKMASK:
8440 infoPtr->uCallbackMask = (UINT)wParam;
8441 return TRUE;
8443 case LVM_SETCOLUMNA:
8444 return LISTVIEW_SetColumnT(infoPtr, (INT)wParam, (LPLVCOLUMNW)lParam, FALSE);
8446 case LVM_SETCOLUMNW:
8447 return LISTVIEW_SetColumnT(infoPtr, (INT)wParam, (LPLVCOLUMNW)lParam, TRUE);
8449 case LVM_SETCOLUMNORDERARRAY:
8450 return LISTVIEW_SetColumnOrderArray(infoPtr, (INT)wParam, (LPINT)lParam);
8452 case LVM_SETCOLUMNWIDTH:
8453 return LISTVIEW_SetColumnWidth(infoPtr, (INT)wParam, SLOWORD(lParam));
8455 case LVM_SETEXTENDEDLISTVIEWSTYLE:
8456 return LISTVIEW_SetExtendedListViewStyle(infoPtr, (DWORD)wParam, (DWORD)lParam);
8458 /* case LVM_SETGROUPINFO: */
8460 /* case LVM_SETGROUPMETRICS: */
8462 case LVM_SETHOTCURSOR:
8463 return (LRESULT)LISTVIEW_SetHotCursor(infoPtr, (HCURSOR)lParam);
8465 case LVM_SETHOTITEM:
8466 return LISTVIEW_SetHotItem(infoPtr, (INT)wParam);
8468 case LVM_SETHOVERTIME:
8469 return LISTVIEW_SetHoverTime(infoPtr, (DWORD)wParam);
8471 case LVM_SETICONSPACING:
8472 return LISTVIEW_SetIconSpacing(infoPtr, (DWORD)lParam);
8474 case LVM_SETIMAGELIST:
8475 return (LRESULT)LISTVIEW_SetImageList(infoPtr, (INT)wParam, (HIMAGELIST)lParam);
8477 /* case LVM_SETINFOTIP: */
8479 /* case LVM_SETINSERTMARK: */
8481 /* case LVM_SETINSERTMARKCOLOR: */
8483 case LVM_SETITEMA:
8484 return LISTVIEW_SetItemT(infoPtr, (LPLVITEMW)lParam, FALSE);
8486 case LVM_SETITEMW:
8487 return LISTVIEW_SetItemT(infoPtr, (LPLVITEMW)lParam, TRUE);
8489 case LVM_SETITEMCOUNT:
8490 return LISTVIEW_SetItemCount(infoPtr, (INT)wParam, (DWORD)lParam);
8492 case LVM_SETITEMPOSITION:
8494 POINT pt = { SLOWORD(lParam), SHIWORD(lParam) };
8495 return LISTVIEW_SetItemPosition(infoPtr, (INT)wParam, pt);
8498 case LVM_SETITEMPOSITION32:
8499 if (lParam == 0) return FALSE;
8500 return LISTVIEW_SetItemPosition(infoPtr, (INT)wParam, *((POINT*)lParam));
8502 case LVM_SETITEMSTATE:
8503 return LISTVIEW_SetItemState(infoPtr, (INT)wParam, (LPLVITEMW)lParam);
8505 case LVM_SETITEMTEXTA:
8506 return LISTVIEW_SetItemTextT(infoPtr, (INT)wParam, (LPLVITEMW)lParam, FALSE);
8508 case LVM_SETITEMTEXTW:
8509 return LISTVIEW_SetItemTextT(infoPtr, (INT)wParam, (LPLVITEMW)lParam, TRUE);
8511 /* case LVM_SETOUTLINECOLOR: */
8513 /* case LVM_SETSELECTEDCOLUMN: */
8515 case LVM_SETSELECTIONMARK:
8516 return LISTVIEW_SetSelectionMark(infoPtr, (INT)lParam);
8518 case LVM_SETTEXTBKCOLOR:
8519 return LISTVIEW_SetTextBkColor(infoPtr, (COLORREF)lParam);
8521 case LVM_SETTEXTCOLOR:
8522 return LISTVIEW_SetTextColor(infoPtr, (COLORREF)lParam);
8524 /* case LVM_SETTILEINFO: */
8526 /* case LVM_SETTILEVIEWINFO: */
8528 /* case LVM_SETTILEWIDTH: */
8530 /* case LVM_SETTOOLTIPS: */
8532 /* case LVM_SETUNICODEFORMAT: */
8534 /* case LVM_SETVIEW: */
8536 /* case LVM_SETWORKAREAS: */
8538 /* case LVM_SORTGROUPS: */
8540 case LVM_SORTITEMS:
8541 return LISTVIEW_SortItems(infoPtr, (PFNLVCOMPARE)lParam, (LPARAM)wParam);
8543 /* LVM_SORTITEMSEX: */
8545 case LVM_SUBITEMHITTEST:
8546 return LISTVIEW_HitTest(infoPtr, (LPLVHITTESTINFO)lParam, TRUE, FALSE);
8548 case LVM_UPDATE:
8549 return LISTVIEW_Update(infoPtr, (INT)wParam);
8551 case WM_CHAR:
8552 return LISTVIEW_ProcessLetterKeys( infoPtr, wParam, lParam );
8554 case WM_COMMAND:
8555 return LISTVIEW_Command(infoPtr, wParam, lParam);
8557 case WM_CREATE:
8558 return LISTVIEW_Create(hwnd, (LPCREATESTRUCTW)lParam);
8560 case WM_ERASEBKGND:
8561 return LISTVIEW_EraseBkgnd(infoPtr, (HDC)wParam);
8563 case WM_GETDLGCODE:
8564 return DLGC_WANTCHARS | DLGC_WANTARROWS;
8566 case WM_GETFONT:
8567 return (LRESULT)infoPtr->hFont;
8569 case WM_HSCROLL:
8570 return LISTVIEW_HScroll(infoPtr, (INT)LOWORD(wParam), 0, (HWND)lParam);
8572 case WM_KEYDOWN:
8573 return LISTVIEW_KeyDown(infoPtr, (INT)wParam, (LONG)lParam);
8575 case WM_KILLFOCUS:
8576 return LISTVIEW_KillFocus(infoPtr);
8578 case WM_LBUTTONDBLCLK:
8579 return LISTVIEW_LButtonDblClk(infoPtr, (WORD)wParam, MAKEPOINTS(lParam));
8581 case WM_LBUTTONDOWN:
8582 return LISTVIEW_LButtonDown(infoPtr, (WORD)wParam, MAKEPOINTS(lParam));
8584 case WM_LBUTTONUP:
8585 return LISTVIEW_LButtonUp(infoPtr, (WORD)wParam, MAKEPOINTS(lParam));
8587 case WM_MOUSEMOVE:
8588 return LISTVIEW_MouseMove (infoPtr, (WORD)wParam, MAKEPOINTS(lParam));
8590 case WM_MOUSEHOVER:
8591 return LISTVIEW_MouseHover(infoPtr, (WORD)wParam, MAKEPOINTS(lParam));
8593 case WM_NCDESTROY:
8594 return LISTVIEW_NCDestroy(infoPtr);
8596 case WM_NOTIFY:
8597 if (lParam && ((LPNMHDR)lParam)->hwndFrom == infoPtr->hwndHeader)
8598 return LISTVIEW_HeaderNotification(infoPtr, (LPNMHEADERW)lParam);
8599 else return 0;
8601 case WM_NOTIFYFORMAT:
8602 return LISTVIEW_NotifyFormat(infoPtr, (HWND)wParam, (INT)lParam);
8604 case WM_PAINT:
8605 return LISTVIEW_Paint(infoPtr, (HDC)wParam);
8607 case WM_RBUTTONDBLCLK:
8608 return LISTVIEW_RButtonDblClk(infoPtr, (WORD)wParam, MAKEPOINTS(lParam));
8610 case WM_RBUTTONDOWN:
8611 return LISTVIEW_RButtonDown(infoPtr, (WORD)wParam, MAKEPOINTS(lParam));
8613 case WM_RBUTTONUP:
8614 return LISTVIEW_RButtonUp(infoPtr, (WORD)wParam, MAKEPOINTS(lParam));
8616 case WM_SETCURSOR:
8617 if(LISTVIEW_SetCursor(infoPtr, (HWND)wParam, LOWORD(lParam), HIWORD(lParam)))
8618 return TRUE;
8619 goto fwd_msg;
8621 case WM_SETFOCUS:
8622 return LISTVIEW_SetFocus(infoPtr, (HWND)wParam);
8624 case WM_SETFONT:
8625 return LISTVIEW_SetFont(infoPtr, (HFONT)wParam, (WORD)lParam);
8627 case WM_SETREDRAW:
8628 return LISTVIEW_SetRedraw(infoPtr, (BOOL)wParam);
8630 case WM_SIZE:
8631 return LISTVIEW_Size(infoPtr, (int)SLOWORD(lParam), (int)SHIWORD(lParam));
8633 case WM_STYLECHANGED:
8634 return LISTVIEW_StyleChanged(infoPtr, wParam, (LPSTYLESTRUCT)lParam);
8636 case WM_SYSCOLORCHANGE:
8637 COMCTL32_RefreshSysColors();
8638 return 0;
8640 /* case WM_TIMER: */
8642 case WM_VSCROLL:
8643 return LISTVIEW_VScroll(infoPtr, (INT)LOWORD(wParam), 0, (HWND)lParam);
8645 case WM_MOUSEWHEEL:
8646 if (wParam & (MK_SHIFT | MK_CONTROL))
8647 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
8648 return LISTVIEW_MouseWheel(infoPtr, (short int)HIWORD(wParam));
8650 case WM_WINDOWPOSCHANGED:
8651 if (!(((WINDOWPOS *)lParam)->flags & SWP_NOSIZE))
8653 SetWindowPos(infoPtr->hwndSelf, 0, 0, 0, 0, 0, SWP_FRAMECHANGED | SWP_NOACTIVATE |
8654 SWP_NOZORDER | SWP_NOMOVE | SWP_NOSIZE);
8655 /* FIXME: why do we need this here? */
8656 LISTVIEW_UpdateSize(infoPtr);
8657 LISTVIEW_UpdateScroll(infoPtr);
8659 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
8661 /* case WM_WININICHANGE: */
8663 default:
8664 if ((uMsg >= WM_USER) && (uMsg < WM_APP))
8665 ERR("unknown msg %04x wp=%08x lp=%08lx\n", uMsg, wParam, lParam);
8667 fwd_msg:
8668 /* call default window procedure */
8669 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
8672 return 0;
8675 /***
8676 * DESCRIPTION:
8677 * Registers the window class.
8679 * PARAMETER(S):
8680 * None
8682 * RETURN:
8683 * None
8685 void LISTVIEW_Register(void)
8687 WNDCLASSW wndClass;
8689 ZeroMemory(&wndClass, sizeof(WNDCLASSW));
8690 wndClass.style = CS_GLOBALCLASS | CS_DBLCLKS;
8691 wndClass.lpfnWndProc = (WNDPROC)LISTVIEW_WindowProc;
8692 wndClass.cbClsExtra = 0;
8693 wndClass.cbWndExtra = sizeof(LISTVIEW_INFO *);
8694 wndClass.hCursor = LoadCursorW(0, IDC_ARROWW);
8695 wndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
8696 wndClass.lpszClassName = WC_LISTVIEWW;
8697 RegisterClassW(&wndClass);
8700 /***
8701 * DESCRIPTION:
8702 * Unregisters the window class.
8704 * PARAMETER(S):
8705 * None
8707 * RETURN:
8708 * None
8710 void LISTVIEW_Unregister(void)
8712 UnregisterClassW(WC_LISTVIEWW, (HINSTANCE)NULL);
8715 /***
8716 * DESCRIPTION:
8717 * Handle any WM_COMMAND messages
8719 * PARAMETER(S):
8720 * [I] infoPtr : valid pointer to the listview structure
8721 * [I] wParam : the first message parameter
8722 * [I] lParam : the second message parameter
8724 * RETURN:
8725 * Zero.
8727 static LRESULT LISTVIEW_Command(LISTVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
8729 switch (HIWORD(wParam))
8731 case EN_UPDATE:
8734 * Adjust the edit window size
8736 WCHAR buffer[1024];
8737 HDC hdc = GetDC(infoPtr->hwndEdit);
8738 HFONT hFont, hOldFont = 0;
8739 RECT rect;
8740 SIZE sz;
8741 int len;
8743 if (!infoPtr->hwndEdit || !hdc) return 0;
8744 len = GetWindowTextW(infoPtr->hwndEdit, buffer, sizeof(buffer)/sizeof(buffer[0]));
8745 GetWindowRect(infoPtr->hwndEdit, &rect);
8747 /* Select font to get the right dimension of the string */
8748 hFont = (HFONT)SendMessageW(infoPtr->hwndEdit, WM_GETFONT, 0, 0);
8749 if(hFont != 0)
8751 hOldFont = SelectObject(hdc, hFont);
8754 if (GetTextExtentPoint32W(hdc, buffer, lstrlenW(buffer), &sz))
8756 TEXTMETRICW textMetric;
8758 /* Add Extra spacing for the next character */
8759 GetTextMetricsW(hdc, &textMetric);
8760 sz.cx += (textMetric.tmMaxCharWidth * 2);
8762 SetWindowPos (
8763 infoPtr->hwndEdit,
8764 HWND_TOP,
8767 sz.cx,
8768 rect.bottom - rect.top,
8769 SWP_DRAWFRAME|SWP_NOMOVE);
8771 if(hFont != 0)
8772 SelectObject(hdc, hOldFont);
8774 ReleaseDC(infoPtr->hwndSelf, hdc);
8776 break;
8779 default:
8780 return SendMessageW (GetParent (infoPtr->hwndSelf), WM_COMMAND, wParam, lParam);
8783 return 0;
8787 /***
8788 * DESCRIPTION:
8789 * Subclassed edit control windproc function
8791 * PARAMETER(S):
8792 * [I] hwnd : the edit window handle
8793 * [I] uMsg : the message that is to be processed
8794 * [I] wParam : first message parameter
8795 * [I] lParam : second message parameter
8796 * [I] isW : TRUE if input is Unicode
8798 * RETURN:
8799 * Zero.
8801 static LRESULT EditLblWndProcT(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL isW)
8803 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)GetWindowLongW(GetParent(hwnd), 0);
8804 BOOL cancel = FALSE;
8806 TRACE("(hwnd=%p, uMsg=%x, wParam=%x, lParam=%lx, isW=%d)\n",
8807 hwnd, uMsg, wParam, lParam, isW);
8809 switch (uMsg)
8811 case WM_GETDLGCODE:
8812 return DLGC_WANTARROWS | DLGC_WANTALLKEYS;
8814 case WM_KILLFOCUS:
8815 break;
8817 case WM_DESTROY:
8819 WNDPROC editProc = infoPtr->EditWndProc;
8820 infoPtr->EditWndProc = 0;
8821 SetWindowLongW(hwnd, GWL_WNDPROC, (LONG)editProc);
8822 return CallWindowProcT(editProc, hwnd, uMsg, wParam, lParam, isW);
8825 case WM_KEYDOWN:
8826 if (VK_ESCAPE == (INT)wParam)
8828 cancel = TRUE;
8829 break;
8831 else if (VK_RETURN == (INT)wParam)
8832 break;
8834 default:
8835 return CallWindowProcT(infoPtr->EditWndProc, hwnd, uMsg, wParam, lParam, isW);
8838 /* kill the edit */
8839 if (infoPtr->hwndEdit)
8841 LPWSTR buffer = NULL;
8843 infoPtr->hwndEdit = 0;
8844 if (!cancel)
8846 DWORD len = isW ? GetWindowTextLengthW(hwnd) : GetWindowTextLengthA(hwnd);
8848 if (len)
8850 if ( (buffer = COMCTL32_Alloc((len+1) * (isW ? sizeof(WCHAR) : sizeof(CHAR)))) )
8852 if (isW) GetWindowTextW(hwnd, buffer, len+1);
8853 else GetWindowTextA(hwnd, (CHAR*)buffer, len+1);
8857 LISTVIEW_EndEditLabelT(infoPtr, buffer, isW);
8859 if (buffer) COMCTL32_Free(buffer);
8863 SendMessageW(hwnd, WM_CLOSE, 0, 0);
8864 return 0;
8867 /***
8868 * DESCRIPTION:
8869 * Subclassed edit control Unicode windproc function
8871 * PARAMETER(S):
8872 * [I] hwnd : the edit window handle
8873 * [I] uMsg : the message that is to be processed
8874 * [I] wParam : first message parameter
8875 * [I] lParam : second message parameter
8877 * RETURN:
8879 LRESULT CALLBACK EditLblWndProcW(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
8881 return EditLblWndProcT(hwnd, uMsg, wParam, lParam, TRUE);
8884 /***
8885 * DESCRIPTION:
8886 * Subclassed edit control ANSI windproc function
8888 * PARAMETER(S):
8889 * [I] hwnd : the edit window handle
8890 * [I] uMsg : the message that is to be processed
8891 * [I] wParam : first message parameter
8892 * [I] lParam : second message parameter
8894 * RETURN:
8896 LRESULT CALLBACK EditLblWndProcA(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
8898 return EditLblWndProcT(hwnd, uMsg, wParam, lParam, FALSE);
8901 /***
8902 * DESCRIPTION:
8903 * Creates a subclassed edit cotrol
8905 * PARAMETER(S):
8906 * [I] infoPtr : valid pointer to the listview structure
8907 * [I] text : initial text for the edit
8908 * [I] style : the window style
8909 * [I] isW : TRUE if input is Unicode
8911 * RETURN:
8913 static HWND CreateEditLabelT(LISTVIEW_INFO *infoPtr, LPCWSTR text, DWORD style,
8914 INT x, INT y, INT width, INT height, BOOL isW)
8916 WCHAR editName[5] = { 'E', 'd', 'i', 't', '\0' };
8917 HWND hedit;
8918 SIZE sz;
8919 HDC hdc;
8920 HDC hOldFont=0;
8921 TEXTMETRICW textMetric;
8922 HINSTANCE hinst = (HINSTANCE)GetWindowLongW(infoPtr->hwndSelf, GWL_HINSTANCE);
8924 TRACE("(text=%s, ..., isW=%d)\n", debugtext_t(text, isW), isW);
8926 style |= WS_CHILDWINDOW|WS_CLIPSIBLINGS|ES_LEFT|WS_BORDER;
8927 hdc = GetDC(infoPtr->hwndSelf);
8929 /* Select the font to get appropriate metric dimensions */
8930 if(infoPtr->hFont != 0)
8931 hOldFont = SelectObject(hdc, infoPtr->hFont);
8933 /*Get String Lenght in pixels */
8934 GetTextExtentPoint32W(hdc, text, lstrlenW(text), &sz);
8936 /*Add Extra spacing for the next character */
8937 GetTextMetricsW(hdc, &textMetric);
8938 sz.cx += (textMetric.tmMaxCharWidth * 2);
8940 if(infoPtr->hFont != 0)
8941 SelectObject(hdc, hOldFont);
8943 ReleaseDC(infoPtr->hwndSelf, hdc);
8944 if (isW)
8945 hedit = CreateWindowW(editName, text, style, x, y, sz.cx, height, infoPtr->hwndSelf, 0, hinst, 0);
8946 else
8947 hedit = CreateWindowA("Edit", (LPCSTR)text, style, x, y, sz.cx, height, infoPtr->hwndSelf, 0, hinst, 0);
8949 if (!hedit) return 0;
8951 infoPtr->EditWndProc = (WNDPROC)
8952 (isW ? SetWindowLongW(hedit, GWL_WNDPROC, (LONG)EditLblWndProcW) :
8953 SetWindowLongA(hedit, GWL_WNDPROC, (LONG)EditLblWndProcA) );
8955 SendMessageW(hedit, WM_SETFONT, (WPARAM)infoPtr->hFont, FALSE);
8957 return hedit;