Generate custom draw notifications for ownerdraw listviews (based on a
[wine/wine64.git] / dlls / comctl32 / listview.c
blob2a57aeac8888daa04ed4f85f6eacf225591d48c0
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_SetColumnWidth ignores header images & bitmap
49 * -- LISTVIEW_SetIconSpacing is incomplete
50 * -- LISTVIEW_SortItems is broken
51 * -- LISTVIEW_StyleChanged doesn't handle some changes too well
53 * Speedups
54 * -- LISTVIEW_GetNextItem needs to be rewritten. It is currently
55 * linear in the number of items in the list, and this is
56 * unacceptable for large lists.
57 * -- in sorted mode, LISTVIEW_InsertItemT sorts the array,
58 * instead of inserting in the right spot
59 * -- we should keep an ordered array of coordinates in iconic mode
60 * this would allow to frame items (iterator_frameditems),
61 * and find nearest item (LVFI_NEARESTXY) a lot more efficiently
63 * Flags
64 * -- LVIF_COLUMNS
65 * -- LVIF_GROUPID
66 * -- LVIF_NORECOMPUTE
68 * States
69 * -- LVIS_ACTIVATING (not currently supported by comctl32.dll version 6.0)
70 * -- LVIS_CUT
71 * -- LVIS_DROPHILITED
72 * -- LVIS_OVERLAYMASK
74 * Styles
75 * -- LVS_NOLABELWRAP
76 * -- LVS_NOSCROLL (see Q137520)
77 * -- LVS_SORTASCENDING, LVS_SORTDESCENDING
79 * Extended Styles
80 * -- LVS_EX_BORDERSELECT
81 * -- LVS_EX_CHECKBOXES
82 * -- LVS_EX_FLATSB
83 * -- LVS_EX_GRIDLINES
84 * -- LVS_EX_HEADERDRAGDROP
85 * -- LVS_EX_INFOTIP
86 * -- LVS_EX_LABELTIP
87 * -- LVS_EX_MULTIWORKAREAS
88 * -- LVS_EX_ONECLICKACTIVATE
89 * -- LVS_EX_REGIONAL
90 * -- LVS_EX_SIMPLESELECT
91 * -- LVS_EX_SUBITEMIMAGES
92 * -- LVS_EX_TRACKSELECT
93 * -- LVS_EX_TWOCLICKACTIVATE
94 * -- LVS_EX_UNDERLINECOLD
95 * -- LVS_EX_UNDERLINEHOT
97 * Notifications:
98 * -- LVN_BEGINDRAG, LVN_BEGINRDRAG
99 * -- LVN_BEGINSCROLL, LVN_ENDSCROLL
100 * -- LVN_GETINFOTIP
101 * -- LVN_HOTTRACK
102 * -- LVN_MARQUEEBEGIN
103 * -- LVN_ODFINDITEM
104 * -- LVN_ODSTATECHANGED
105 * -- LVN_SETDISPINFO
106 * -- NM_HOVER
108 * Messages:
109 * -- LVM_CANCELEDITLABEL
110 * -- LVM_CREATEDRAGIMAGE
111 * -- LVM_ENABLEGROUPVIEW
112 * -- LVM_GETBKIMAGE, LVM_SETBKIMAGE
113 * -- LVM_GETGROUPINFO, LVM_SETGROUPINFO
114 * -- LVM_GETGROUPMETRICS, LVM_SETGROUPMETRICS
115 * -- LVM_GETINSERTMARK, LVM_SETINSERTMARK
116 * -- LVM_GETINSERTMARKCOLOR, LVM_SETINSERTMARKCOLOR
117 * -- LVM_GETINSERTMARKRECT
118 * -- LVM_GETNUMBEROFWORKAREAS
119 * -- LVM_GETOUTLINECOLOR, LVM_SETOUTLINECOLOR
120 * -- LVM_GETSELECTEDCOLUMN, LVM_SETSELECTEDCOLUMN
121 * -- LVM_GETISEARCHSTRINGW, LVM_GETISEARCHSTRINGA
122 * -- LVM_GETTILEINFO, LVM_SETTILEINFO
123 * -- LVM_GETTILEVIEWINFO, LVM_SETTILEVIEWINFO
124 * -- LVM_GETTOOLTIPS, LVM_SETTOOLTIPS
125 * -- LVM_GETUNICODEFORMAT, LVM_SETUNICODEFORMAT
126 * -- LVM_GETVIEW, LVM_SETVIEW
127 * -- LVM_GETWORKAREAS, LVM_SETWORKAREAS
128 * -- LVM_HASGROUP, LVM_INSERTGROUP, LVM_REMOVEGROUP, LVM_REMOVEALLGROUPS
129 * -- LVM_INSERTGROUPSORTED
130 * -- LVM_INSERTMARKHITTEST
131 * -- LVM_ISGROUPVIEWENABLED
132 * -- LVM_MAPIDTOINDEX, LVM_MAPINDEXTOID
133 * -- LVM_MOVEGROUP
134 * -- LVM_MOVEITEMTOGROUP
135 * -- LVM_SETINFOTIP
136 * -- LVM_SETTILEWIDTH
137 * -- LVM_SORTGROUPS
138 * -- LVM_SORTITEMSEX
140 * Known differences in message stream from native control (not known if
141 * these differences cause problems):
142 * LVM_INSERTITEM issues LVM_SETITEMSTATE and LVM_SETITEM in certain cases.
143 * LVM_SETITEM does not always issue LVN_ITEMCHANGING/LVN_ITEMCHANGED.
144 * WM_CREATE does not issue WM_QUERYUISTATE and associated registry
145 * processing for "USEDOUBLECLICKTIME".
148 #include "config.h"
149 #include "wine/port.h"
151 #include <assert.h>
152 #include <ctype.h>
153 #include <string.h>
154 #include <stdlib.h>
155 #include <stdio.h>
157 #include "winbase.h"
158 #include "winnt.h"
159 #include "commctrl.h"
160 #include "comctl32.h"
162 #include "wine/debug.h"
163 #include "wine/unicode.h"
165 WINE_DEFAULT_DEBUG_CHANNEL(listview);
167 /* make sure you set this to 0 for production use! */
168 #define DEBUG_RANGES 1
170 typedef struct tagCOLUMN_INFO
172 RECT rcHeader; /* tracks the header's rectangle */
173 int fmt; /* same as LVCOLUMN.fmt */
174 } COLUMN_INFO;
176 typedef struct tagITEMHDR
178 LPWSTR pszText;
179 INT iImage;
180 } ITEMHDR, *LPITEMHDR;
182 typedef struct tagSUBITEM_INFO
184 ITEMHDR hdr;
185 INT iSubItem;
186 } SUBITEM_INFO;
188 typedef struct tagITEM_INFO
190 ITEMHDR hdr;
191 UINT state;
192 LPARAM lParam;
193 INT iIndent;
194 } ITEM_INFO;
196 typedef struct tagRANGE
198 INT lower;
199 INT upper;
200 } RANGE;
202 typedef struct tagRANGES
204 HDPA hdpa;
205 } *RANGES;
207 typedef struct tagITERATOR
209 INT nItem;
210 INT nSpecial;
211 RANGE range;
212 RANGES ranges;
213 INT index;
214 } ITERATOR;
216 typedef struct tagLISTVIEW_INFO
218 HWND hwndSelf;
219 HBRUSH hBkBrush;
220 COLORREF clrBk;
221 COLORREF clrText;
222 COLORREF clrTextBk;
223 COLORREF clrTextBkDefault;
224 HIMAGELIST himlNormal;
225 HIMAGELIST himlSmall;
226 HIMAGELIST himlState;
227 BOOL bLButtonDown;
228 BOOL bRButtonDown;
229 BOOL bNoItemMetrics; /* flags if item metrics are not yet computed */
230 INT nItemHeight;
231 INT nItemWidth;
232 RANGES selectionRanges;
233 INT nSelectionMark;
234 INT nHotItem;
235 SHORT notifyFormat;
236 RECT rcList; /* This rectangle is really the window
237 * client rectangle possibly reduced by the
238 * horizontal scroll bar and/or header - see
239 * LISTVIEW_UpdateSize. This rectangle offset
240 * by the LISTVIEW_GetOrigin value is in
241 * client coordinates */
242 SIZE iconSize;
243 SIZE iconSpacing;
244 SIZE iconStateSize;
245 UINT uCallbackMask;
246 HWND hwndHeader;
247 HCURSOR hHotCursor;
248 HFONT hDefaultFont;
249 HFONT hFont;
250 INT ntmHeight; /* Some cached metrics of the font used */
251 INT ntmAveCharWidth; /* by the listview to draw items */
252 BOOL bRedraw; /* Turns on/off repaints & invalidations */
253 BOOL bFirstPaint; /* Flags if the control has never painted before */
254 BOOL bAutoarrange; /* Autoarrange flag when NOT in LVS_AUTOARRANGE */
255 BOOL bFocus;
256 BOOL bDoChangeNotify; /* send change notification messages? */
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_HOR_PADDING - between text and sides of box
311 * LABEL_VERT_PADDING - between bottom of text and end of box
313 * ICON_LR_PADDING - additional width above icon size.
314 * ICON_LR_HALF - half of the above value
316 #define ICON_TOP_PADDING_NOTHITABLE 2
317 #define ICON_TOP_PADDING_HITABLE 2
318 #define ICON_TOP_PADDING (ICON_TOP_PADDING_NOTHITABLE + ICON_TOP_PADDING_HITABLE)
319 #define ICON_BOTTOM_PADDING 4
320 #define LABEL_HOR_PADDING 5
321 #define LABEL_VERT_PADDING 7
322 #define ICON_LR_PADDING 16
323 #define ICON_LR_HALF (ICON_LR_PADDING/2)
325 /* default label width for items in list and small icon display modes */
326 #define DEFAULT_LABEL_WIDTH 40
328 /* default column width for items in list display mode */
329 #define DEFAULT_COLUMN_WIDTH 128
331 /* Size of "line" scroll for V & H scrolls */
332 #define LISTVIEW_SCROLL_ICON_LINE_SIZE 37
334 /* Padding betwen image and label */
335 #define IMAGE_PADDING 2
337 /* Padding behind the label */
338 #define TRAILING_LABEL_PADDING 12
339 #define TRAILING_HEADER_PADDING 11
341 /* Border for the icon caption */
342 #define CAPTION_BORDER 2
344 /* Standard DrawText flags */
345 #define LV_ML_DT_FLAGS (DT_TOP | DT_NOPREFIX | DT_EDITCONTROL | DT_CENTER | DT_WORDBREAK | DT_WORD_ELLIPSIS | DT_END_ELLIPSIS)
346 #define LV_FL_DT_FLAGS (DT_TOP | DT_NOPREFIX | DT_EDITCONTROL | DT_CENTER | DT_WORDBREAK | DT_NOCLIP)
347 #define LV_SL_DT_FLAGS (DT_TOP | DT_EDITCONTROL | DT_SINGLELINE | DT_WORD_ELLIPSIS | DT_END_ELLIPSIS)
349 /* The time in milliseconds to reset the search in the list */
350 #define KEY_DELAY 450
352 /* Dump the LISTVIEW_INFO structure to the debug channel */
353 #define LISTVIEW_DUMP(iP) do { \
354 TRACE("hwndSelf=%p, clrBk=0x%06lx, clrText=0x%06lx, clrTextBk=0x%06lx, ItemHeight=%d, ItemWidth=%d, Style=0x%08lx\n", \
355 iP->hwndSelf, iP->clrBk, iP->clrText, iP->clrTextBk, \
356 iP->nItemHeight, iP->nItemWidth, infoPtr->dwStyle); \
357 TRACE("hwndSelf=%p, himlNor=%p, himlSml=%p, himlState=%p, Focused=%d, Hot=%d, exStyle=0x%08lx, Focus=%d\n", \
358 iP->hwndSelf, iP->himlNormal, iP->himlSmall, iP->himlState, \
359 iP->nFocusedItem, iP->nHotItem, iP->dwLvExStyle, iP->bFocus ); \
360 TRACE("hwndSelf=%p, ntmH=%d, icSz.cx=%ld, icSz.cy=%ld, icSp.cx=%ld, icSp.cy=%ld, notifyFmt=%d\n", \
361 iP->hwndSelf, iP->ntmHeight, iP->iconSize.cx, iP->iconSize.cy, \
362 iP->iconSpacing.cx, iP->iconSpacing.cy, iP->notifyFormat); \
363 TRACE("hwndSelf=%p, rcList=%s\n", iP->hwndSelf, debugrect(&iP->rcList)); \
364 } while(0)
367 * forward declarations
369 static BOOL LISTVIEW_GetItemT(LISTVIEW_INFO *, LPLVITEMW, BOOL);
370 static void LISTVIEW_GetItemBox(LISTVIEW_INFO *, INT, LPRECT);
371 static void LISTVIEW_GetItemOrigin(LISTVIEW_INFO *, INT, LPPOINT);
372 static BOOL LISTVIEW_GetItemPosition(LISTVIEW_INFO *, INT, LPPOINT);
373 static BOOL LISTVIEW_GetItemRect(LISTVIEW_INFO *, INT, LPRECT);
374 static INT LISTVIEW_GetLabelWidth(LISTVIEW_INFO *, INT);
375 static void LISTVIEW_GetOrigin(LISTVIEW_INFO *, LPPOINT);
376 static BOOL LISTVIEW_GetViewRect(LISTVIEW_INFO *, LPRECT);
377 static void LISTVIEW_SetGroupSelection(LISTVIEW_INFO *, INT);
378 static BOOL LISTVIEW_SetItemT(LISTVIEW_INFO *, const LVITEMW *, BOOL);
379 static void LISTVIEW_UpdateScroll(LISTVIEW_INFO *);
380 static void LISTVIEW_SetSelection(LISTVIEW_INFO *, INT);
381 static void LISTVIEW_UpdateSize(LISTVIEW_INFO *);
382 static HWND LISTVIEW_EditLabelT(LISTVIEW_INFO *, INT, BOOL);
383 static LRESULT LISTVIEW_Command(LISTVIEW_INFO *, WPARAM, LPARAM);
384 static BOOL LISTVIEW_SortItems(LISTVIEW_INFO *, PFNLVCOMPARE, LPARAM);
385 static INT LISTVIEW_GetStringWidthT(LISTVIEW_INFO *, LPCWSTR, BOOL);
386 static BOOL LISTVIEW_KeySelection(LISTVIEW_INFO *, INT);
387 static UINT LISTVIEW_GetItemState(LISTVIEW_INFO *, INT, UINT);
388 static BOOL LISTVIEW_SetItemState(LISTVIEW_INFO *, INT, const LVITEMW *);
389 static LRESULT LISTVIEW_VScroll(LISTVIEW_INFO *, INT, INT, HWND);
390 static LRESULT LISTVIEW_HScroll(LISTVIEW_INFO *, INT, INT, HWND);
391 static INT LISTVIEW_GetTopIndex(LISTVIEW_INFO *);
392 static BOOL LISTVIEW_EnsureVisible(LISTVIEW_INFO *, INT, BOOL);
393 static HWND CreateEditLabelT(LISTVIEW_INFO *, LPCWSTR, DWORD, INT, INT, INT, INT, BOOL);
395 /******** Text handling functions *************************************/
397 /* A text pointer is either NULL, LPSTR_TEXTCALLBACK, or points to a
398 * text string. The string may be ANSI or Unicode, in which case
399 * the boolean isW tells us the type of the string.
401 * The name of the function tell what type of strings it expects:
402 * W: Unicode, T: ANSI/Unicode - function of isW
405 static inline BOOL is_textW(LPCWSTR text)
407 return text != NULL && text != LPSTR_TEXTCALLBACKW;
410 static inline BOOL is_textT(LPCWSTR text, BOOL isW)
412 /* we can ignore isW since LPSTR_TEXTCALLBACKW == LPSTR_TEXTCALLBACKA */
413 return is_textW(text);
416 static inline int textlenT(LPCWSTR text, BOOL isW)
418 return !is_textT(text, isW) ? 0 :
419 isW ? lstrlenW(text) : lstrlenA((LPCSTR)text);
422 static inline void textcpynT(LPWSTR dest, BOOL isDestW, LPCWSTR src, BOOL isSrcW, INT max)
424 if (isDestW)
425 if (isSrcW) lstrcpynW(dest, src, max);
426 else MultiByteToWideChar(CP_ACP, 0, (LPCSTR)src, -1, dest, max);
427 else
428 if (isSrcW) WideCharToMultiByte(CP_ACP, 0, src, -1, (LPSTR)dest, max, NULL, NULL);
429 else lstrcpynA((LPSTR)dest, (LPCSTR)src, max);
432 static inline LPWSTR textdupTtoW(LPCWSTR text, BOOL isW)
434 LPWSTR wstr = (LPWSTR)text;
436 if (!isW && is_textT(text, isW))
438 INT len = MultiByteToWideChar(CP_ACP, 0, (LPCSTR)text, -1, NULL, 0);
439 wstr = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
440 if (wstr) MultiByteToWideChar(CP_ACP, 0, (LPCSTR)text, -1, wstr, len);
442 TRACE(" wstr=%s\n", text == LPSTR_TEXTCALLBACKW ? "(callback)" : debugstr_w(wstr));
443 return wstr;
446 static inline void textfreeT(LPWSTR wstr, BOOL isW)
448 if (!isW && is_textT(wstr, isW)) HeapFree(GetProcessHeap(), 0, wstr);
452 * dest is a pointer to a Unicode string
453 * src is a pointer to a string (Unicode if isW, ANSI if !isW)
455 static BOOL textsetptrT(LPWSTR *dest, LPWSTR src, BOOL isW)
457 BOOL bResult = TRUE;
459 if (src == LPSTR_TEXTCALLBACKW)
461 if (is_textW(*dest)) COMCTL32_Free(*dest);
462 *dest = LPSTR_TEXTCALLBACKW;
464 else
466 LPWSTR pszText = textdupTtoW(src, isW);
467 if (*dest == LPSTR_TEXTCALLBACKW) *dest = NULL;
468 bResult = Str_SetPtrW(dest, pszText);
469 textfreeT(pszText, isW);
471 return bResult;
475 * compares a Unicode to a Unicode/ANSI text string
477 static inline int textcmpWT(LPCWSTR aw, LPCWSTR bt, BOOL isW)
479 if (!aw) return bt ? -1 : 0;
480 if (!bt) return aw ? 1 : 0;
481 if (aw == LPSTR_TEXTCALLBACKW)
482 return bt == LPSTR_TEXTCALLBACKW ? 0 : -1;
483 if (bt != LPSTR_TEXTCALLBACKW)
485 LPWSTR bw = textdupTtoW(bt, isW);
486 int r = bw ? lstrcmpW(aw, bw) : 1;
487 textfreeT(bw, isW);
488 return r;
491 return 1;
494 static inline int lstrncmpiW(LPCWSTR s1, LPCWSTR s2, int n)
496 int res;
498 n = min(min(n, strlenW(s1)), strlenW(s2));
499 res = CompareStringW(LOCALE_USER_DEFAULT, NORM_IGNORECASE, s1, n, s2, n);
500 return res ? res - sizeof(WCHAR) : res;
503 /******** Debugging functions *****************************************/
505 static inline LPCSTR debugtext_t(LPCWSTR text, BOOL isW)
507 if (text == LPSTR_TEXTCALLBACKW) return "(callback)";
508 return isW ? debugstr_w(text) : debugstr_a((LPCSTR)text);
511 static inline LPCSTR debugtext_tn(LPCWSTR text, BOOL isW, INT n)
513 if (text == LPSTR_TEXTCALLBACKW) return "(callback)";
514 n = min(textlenT(text, isW), n);
515 return isW ? debugstr_wn(text, n) : debugstr_an((LPCSTR)text, n);
518 static char* debug_getbuf()
520 static int index = 0;
521 static char buffers[DEBUG_BUFFERS][DEBUG_BUFFER_SIZE];
522 return buffers[index++ % DEBUG_BUFFERS];
525 static inline char* debugrange(const RANGE *lprng)
527 if (lprng)
529 char* buf = debug_getbuf();
530 snprintf(buf, DEBUG_BUFFER_SIZE, "[%d, %d)", lprng->lower, lprng->upper);
531 return buf;
532 } else return "(null)";
535 static inline char* debugpoint(const POINT *lppt)
537 if (lppt)
539 char* buf = debug_getbuf();
540 snprintf(buf, DEBUG_BUFFER_SIZE, "(%ld, %ld)", lppt->x, lppt->y);
541 return buf;
542 } else return "(null)";
545 static inline char* debugrect(const RECT *rect)
547 if (rect)
549 char* buf = debug_getbuf();
550 snprintf(buf, DEBUG_BUFFER_SIZE, "[(%ld, %ld);(%ld, %ld)]",
551 rect->left, rect->top, rect->right, rect->bottom);
552 return buf;
553 } else return "(null)";
556 static char * debugscrollinfo(const SCROLLINFO *pScrollInfo)
558 char* buf = debug_getbuf(), *text = buf;
559 int len, size = DEBUG_BUFFER_SIZE;
561 if (pScrollInfo == NULL) return "(null)";
562 len = snprintf(buf, size, "{cbSize=%d, ", pScrollInfo->cbSize);
563 if (len == -1) goto end; buf += len; size -= len;
564 if (pScrollInfo->fMask & SIF_RANGE)
565 len = snprintf(buf, size, "nMin=%d, nMax=%d, ", pScrollInfo->nMin, pScrollInfo->nMax);
566 else len = 0;
567 if (len == -1) goto end; buf += len; size -= len;
568 if (pScrollInfo->fMask & SIF_PAGE)
569 len = snprintf(buf, size, "nPage=%u, ", pScrollInfo->nPage);
570 else len = 0;
571 if (len == -1) goto end; buf += len; size -= len;
572 if (pScrollInfo->fMask & SIF_POS)
573 len = snprintf(buf, size, "nPos=%d, ", pScrollInfo->nPos);
574 else len = 0;
575 if (len == -1) goto end; buf += len; size -= len;
576 if (pScrollInfo->fMask & SIF_TRACKPOS)
577 len = snprintf(buf, size, "nTrackPos=%d, ", pScrollInfo->nTrackPos);
578 else len = 0;
579 if (len == -1) goto end; buf += len; size -= len;
580 goto undo;
581 end:
582 buf = text + strlen(text);
583 undo:
584 if (buf - text > 2) { buf[-2] = '}'; buf[-1] = 0; }
585 return text;
588 static char* debugnmlistview(const NMLISTVIEW *plvnm)
590 if (plvnm)
592 char* buf = debug_getbuf();
593 snprintf(buf, DEBUG_BUFFER_SIZE, "iItem=%d, iSubItem=%d, uNewState=0x%x,"
594 " uOldState=0x%x, uChanged=0x%x, ptAction=%s, lParam=%ld\n",
595 plvnm->iItem, plvnm->iSubItem, plvnm->uNewState, plvnm->uOldState,
596 plvnm->uChanged, debugpoint(&plvnm->ptAction), plvnm->lParam);
597 return buf;
598 } else return "(null)";
601 static char* debuglvitem_t(const LVITEMW *lpLVItem, BOOL isW)
603 char* buf = debug_getbuf(), *text = buf;
604 int len, size = DEBUG_BUFFER_SIZE;
606 if (lpLVItem == NULL) return "(null)";
607 len = snprintf(buf, size, "{iItem=%d, iSubItem=%d, ", lpLVItem->iItem, lpLVItem->iSubItem);
608 if (len == -1) goto end; buf += len; size -= len;
609 if (lpLVItem->mask & LVIF_STATE)
610 len = snprintf(buf, size, "state=%x, stateMask=%x, ", lpLVItem->state, lpLVItem->stateMask);
611 else len = 0;
612 if (len == -1) goto end; buf += len; size -= len;
613 if (lpLVItem->mask & LVIF_TEXT)
614 len = snprintf(buf, size, "pszText=%s, cchTextMax=%d, ", debugtext_tn(lpLVItem->pszText, isW, 80), lpLVItem->cchTextMax);
615 else len = 0;
616 if (len == -1) goto end; buf += len; size -= len;
617 if (lpLVItem->mask & LVIF_IMAGE)
618 len = snprintf(buf, size, "iImage=%d, ", lpLVItem->iImage);
619 else len = 0;
620 if (len == -1) goto end; buf += len; size -= len;
621 if (lpLVItem->mask & LVIF_PARAM)
622 len = snprintf(buf, size, "lParam=%lx, ", lpLVItem->lParam);
623 else len = 0;
624 if (len == -1) goto end; buf += len; size -= len;
625 if (lpLVItem->mask & LVIF_INDENT)
626 len = snprintf(buf, size, "iIndent=%d, ", lpLVItem->iIndent);
627 else len = 0;
628 if (len == -1) goto end; buf += len; size -= len;
629 goto undo;
630 end:
631 buf = text + strlen(text);
632 undo:
633 if (buf - text > 2) { buf[-2] = '}'; buf[-1] = 0; }
634 return text;
637 static char* debuglvcolumn_t(const LVCOLUMNW *lpColumn, BOOL isW)
639 char* buf = debug_getbuf(), *text = buf;
640 int len, size = DEBUG_BUFFER_SIZE;
642 if (lpColumn == NULL) return "(null)";
643 len = snprintf(buf, size, "{");
644 if (len == -1) goto end; buf += len; size -= len;
645 if (lpColumn->mask & LVCF_SUBITEM)
646 len = snprintf(buf, size, "iSubItem=%d, ", lpColumn->iSubItem);
647 else len = 0;
648 if (len == -1) goto end; buf += len; size -= len;
649 if (lpColumn->mask & LVCF_FMT)
650 len = snprintf(buf, size, "fmt=%x, ", lpColumn->fmt);
651 else len = 0;
652 if (len == -1) goto end; buf += len; size -= len;
653 if (lpColumn->mask & LVCF_WIDTH)
654 len = snprintf(buf, size, "cx=%d, ", lpColumn->cx);
655 else len = 0;
656 if (len == -1) goto end; buf += len; size -= len;
657 if (lpColumn->mask & LVCF_TEXT)
658 len = snprintf(buf, size, "pszText=%s, cchTextMax=%d, ", debugtext_tn(lpColumn->pszText, isW, 80), lpColumn->cchTextMax);
659 else len = 0;
660 if (len == -1) goto end; buf += len; size -= len;
661 if (lpColumn->mask & LVCF_IMAGE)
662 len = snprintf(buf, size, "iImage=%d, ", lpColumn->iImage);
663 else len = 0;
664 if (len == -1) goto end; buf += len; size -= len;
665 if (lpColumn->mask & LVCF_ORDER)
666 len = snprintf(buf, size, "iOrder=%d, ", lpColumn->iOrder);
667 else len = 0;
668 if (len == -1) goto end; buf += len; size -= len;
669 goto undo;
670 end:
671 buf = text + strlen(text);
672 undo:
673 if (buf - text > 2) { buf[-2] = '}'; buf[-1] = 0; }
674 return text;
677 static char* debuglvhittestinfo(const LVHITTESTINFO *lpht)
679 if (lpht)
681 char* buf = debug_getbuf();
682 snprintf(buf, DEBUG_BUFFER_SIZE, "{pt=%s, flags=0x%x, iItem=%d, iSubItem=%d}",
683 debugpoint(&lpht->pt), lpht->flags, lpht->iItem, lpht->iSubItem);
684 return buf;
685 } else return "(null)";
688 /* Return the corresponding text for a given scroll value */
689 static inline LPCSTR debugscrollcode(int nScrollCode)
691 switch(nScrollCode)
693 case SB_LINELEFT: return "SB_LINELEFT";
694 case SB_LINERIGHT: return "SB_LINERIGHT";
695 case SB_PAGELEFT: return "SB_PAGELEFT";
696 case SB_PAGERIGHT: return "SB_PAGERIGHT";
697 case SB_THUMBPOSITION: return "SB_THUMBPOSITION";
698 case SB_THUMBTRACK: return "SB_THUMBTRACK";
699 case SB_ENDSCROLL: return "SB_ENDSCROLL";
700 case SB_INTERNAL: return "SB_INTERNAL";
701 default: return "unknown";
706 /******** Notification functions i************************************/
708 static LRESULT notify_hdr(LISTVIEW_INFO *infoPtr, INT code, LPNMHDR pnmh)
710 LRESULT result;
712 TRACE("(code=%d)\n", code);
714 pnmh->hwndFrom = infoPtr->hwndSelf;
715 pnmh->idFrom = GetWindowLongW(infoPtr->hwndSelf, GWL_ID);
716 pnmh->code = code;
717 result = SendMessageW(GetParent(infoPtr->hwndSelf), WM_NOTIFY,
718 (WPARAM)pnmh->idFrom, (LPARAM)pnmh);
720 TRACE(" <= %ld\n", result);
722 return result;
725 static inline LRESULT notify(LISTVIEW_INFO *infoPtr, INT code)
727 NMHDR nmh;
728 return notify_hdr(infoPtr, code, &nmh);
731 static inline void notify_itemactivate(LISTVIEW_INFO *infoPtr)
733 notify(infoPtr, LVN_ITEMACTIVATE);
736 static inline LRESULT notify_listview(LISTVIEW_INFO *infoPtr, INT code, LPNMLISTVIEW plvnm)
738 TRACE("(code=%d, plvnm=%s)\n", code, debugnmlistview(plvnm));
739 return notify_hdr(infoPtr, code, (LPNMHDR)plvnm);
742 static LRESULT notify_click(LISTVIEW_INFO *infoPtr, INT code, LVHITTESTINFO *lvht)
744 NMLISTVIEW nmlv;
745 LVITEMW item;
747 TRACE("code=%d, lvht=%s\n", code, debuglvhittestinfo(lvht));
748 ZeroMemory(&nmlv, sizeof(nmlv));
749 nmlv.iItem = lvht->iItem;
750 nmlv.iSubItem = lvht->iSubItem;
751 nmlv.ptAction = lvht->pt;
752 item.mask = LVIF_PARAM;
753 item.iItem = lvht->iItem;
754 item.iSubItem = 0;
755 if (LISTVIEW_GetItemT(infoPtr, &item, TRUE)) nmlv.lParam = item.lParam;
756 return notify_listview(infoPtr, code, &nmlv);
759 static void notify_deleteitem(LISTVIEW_INFO *infoPtr, INT nItem)
761 NMLISTVIEW nmlv;
762 LVITEMW item;
764 ZeroMemory(&nmlv, sizeof (NMLISTVIEW));
765 nmlv.iItem = nItem;
766 item.mask = LVIF_PARAM;
767 item.iItem = nItem;
768 item.iSubItem = 0;
769 if (LISTVIEW_GetItemT(infoPtr, &item, TRUE)) nmlv.lParam = item.lParam;
770 notify_listview(infoPtr, LVN_DELETEITEM, &nmlv);
773 static int get_ansi_notification(INT unicodeNotificationCode)
775 switch (unicodeNotificationCode)
777 case LVN_BEGINLABELEDITW: return LVN_BEGINLABELEDITA;
778 case LVN_ENDLABELEDITW: return LVN_ENDLABELEDITA;
779 case LVN_GETDISPINFOW: return LVN_GETDISPINFOA;
780 case LVN_SETDISPINFOW: return LVN_SETDISPINFOA;
781 case LVN_ODFINDITEMW: return LVN_ODFINDITEMA;
782 case LVN_GETINFOTIPW: return LVN_GETINFOTIPA;
784 ERR("unknown notification %x\n", unicodeNotificationCode);
785 assert(FALSE);
786 return 0;
790 With testing on Windows 2000 it looks like the notify format
791 has nothing to do with this message. It ALWAYS seems to be
792 in ansi format.
794 infoPtr : listview struct
795 notificationCode : *Unicode* notification code
796 pdi : dispinfo structure (can be unicode or ansi)
797 isW : TRUE if dispinfo is Unicode
799 static BOOL notify_dispinfoT(LISTVIEW_INFO *infoPtr, INT notificationCode, LPNMLVDISPINFOW pdi, BOOL isW)
801 BOOL bResult = FALSE;
802 BOOL convertToAnsi = FALSE;
803 INT cchTempBufMax = 0, savCchTextMax = 0;
804 LPWSTR pszTempBuf = NULL, savPszText = NULL;
806 if ((pdi->item.mask & LVIF_TEXT) && is_textT(pdi->item.pszText, isW))
807 convertToAnsi = isW;
809 if (convertToAnsi)
811 if (notificationCode != LVN_GETDISPINFOW)
813 cchTempBufMax = WideCharToMultiByte(CP_ACP, 0, pdi->item.pszText,
814 -1, NULL, 0, NULL, NULL);
816 else
818 cchTempBufMax = pdi->item.cchTextMax;
819 *pdi->item.pszText = 0; /* make sure we don't process garbage */
822 pszTempBuf = HeapAlloc(GetProcessHeap(), 0, sizeof(CHAR) *
823 cchTempBufMax);
824 if (!pszTempBuf) return FALSE;
826 WideCharToMultiByte(CP_ACP, 0, pdi->item.pszText, -1, (LPSTR)
827 pszTempBuf, cchTempBufMax, NULL, NULL);
829 savCchTextMax = pdi->item.cchTextMax;
830 savPszText = pdi->item.pszText;
831 pdi->item.pszText = pszTempBuf;
832 pdi->item.cchTextMax = cchTempBufMax;
835 TRACE(" pdi->item=%s\n", debuglvitem_t(&pdi->item, infoPtr->notifyFormat !=
836 NFR_ANSI));
838 bResult = notify_hdr(infoPtr, get_ansi_notification(notificationCode),
839 (LPNMHDR)pdi);
841 if (convertToAnsi)
843 MultiByteToWideChar(CP_ACP, 0, (LPSTR) pdi->item.pszText, -1,
844 savPszText, savCchTextMax);
845 pdi->item.pszText = savPszText; /* restores our buffer */
846 pdi->item.cchTextMax = savCchTextMax;
847 HeapFree(GetProcessHeap(), 0, pszTempBuf);
849 return bResult;
852 static void customdraw_fill(NMLVCUSTOMDRAW *lpnmlvcd, LISTVIEW_INFO *infoPtr, HDC hdc,
853 const RECT *rcBounds, const LVITEMW *lplvItem)
855 ZeroMemory(lpnmlvcd, sizeof(NMLVCUSTOMDRAW));
856 lpnmlvcd->nmcd.hdc = hdc;
857 lpnmlvcd->nmcd.rc = *rcBounds;
858 lpnmlvcd->clrTextBk = infoPtr->clrTextBk;
859 lpnmlvcd->clrText = infoPtr->clrText;
860 if (!lplvItem) return;
861 lpnmlvcd->nmcd.dwItemSpec = lplvItem->iItem;
862 lpnmlvcd->iSubItem = lplvItem->iSubItem;
863 if (lplvItem->state & LVIS_SELECTED) lpnmlvcd->nmcd.uItemState |= CDIS_SELECTED;
864 if (lplvItem->state & LVIS_FOCUSED) lpnmlvcd->nmcd.uItemState |= CDIS_FOCUS;
865 if (lplvItem->iItem == infoPtr->nHotItem) lpnmlvcd->nmcd.uItemState |= CDIS_HOT;
866 lpnmlvcd->nmcd.lItemlParam = lplvItem->lParam;
869 static inline DWORD notify_customdraw (LISTVIEW_INFO *infoPtr, DWORD dwDrawStage, NMLVCUSTOMDRAW *lpnmlvcd)
871 lpnmlvcd->nmcd.dwDrawStage = dwDrawStage;
872 if (lpnmlvcd->nmcd.dwItemSpec) lpnmlvcd->nmcd.dwDrawStage |= CDDS_ITEM;
873 return notify_hdr(infoPtr, NM_CUSTOMDRAW, &lpnmlvcd->nmcd.hdr);
876 static DWORD notify_prepaint (LISTVIEW_INFO *infoPtr, HDC hdc, NMLVCUSTOMDRAW *lpnmlvcd)
878 BOOL isSelected = lpnmlvcd->nmcd.uItemState & CDIS_SELECTED;
879 DWORD cditemmode = notify_customdraw(infoPtr, CDDS_PREPAINT, lpnmlvcd);
881 if (cditemmode & CDRF_SKIPDEFAULT) return cditemmode;
883 /* apprently, for selected items, we have to override the returned values */
884 if (isSelected)
886 if (infoPtr->bFocus)
888 lpnmlvcd->clrTextBk = comctl32_color.clrHighlight;
889 lpnmlvcd->clrText = comctl32_color.clrHighlightText;
891 else if (infoPtr->dwStyle & LVS_SHOWSELALWAYS)
893 lpnmlvcd->clrTextBk = comctl32_color.clr3dFace;
894 lpnmlvcd->clrText = comctl32_color.clrBtnText;
898 /* Set the text attributes */
899 if (lpnmlvcd->clrTextBk != CLR_NONE)
901 SetBkMode(hdc, OPAQUE);
902 if (lpnmlvcd->clrTextBk == CLR_DEFAULT)
903 SetBkColor(hdc, infoPtr->clrTextBkDefault);
904 else
905 SetBkColor(hdc,lpnmlvcd->clrTextBk);
907 else
908 SetBkMode(hdc, TRANSPARENT);
909 SetTextColor(hdc, lpnmlvcd->clrText);
911 return cditemmode;
914 static inline DWORD notify_postpaint (LISTVIEW_INFO *infoPtr, NMLVCUSTOMDRAW *lpnmlvcd)
916 return notify_customdraw(infoPtr, CDDS_POSTPAINT, lpnmlvcd);
919 /******** Item iterator functions **********************************/
921 static RANGES ranges_create(int count);
922 static void ranges_destroy(RANGES ranges);
923 static BOOL ranges_add(RANGES ranges, RANGE range);
924 static BOOL ranges_del(RANGES ranges, RANGE range);
925 static void ranges_dump(RANGES ranges);
927 static inline BOOL ranges_additem(RANGES ranges, INT nItem)
929 RANGE range = { nItem, nItem + 1 };
931 return ranges_add(ranges, range);
934 static inline BOOL ranges_delitem(RANGES ranges, INT nItem)
936 RANGE range = { nItem, nItem + 1 };
938 return ranges_del(ranges, range);
941 /***
942 * ITERATOR DOCUMENTATION
944 * The iterator functions allow for easy, and convenient iteration
945 * over items of iterest in the list. Typically, you create a
946 * iterator, use it, and destroy it, as such:
947 * ITERATOR i;
949 * iterator_xxxitems(&i, ...);
950 * while (iterator_{prev,next}(&i)
952 * //code which uses i.nItem
954 * iterator_destroy(&i);
956 * where xxx is either: framed, or visible.
957 * Note that it is important that the code destroys the iterator
958 * after it's done with it, as the creation of the iterator may
959 * allocate memory, which thus needs to be freed.
961 * You can iterate both forwards, and backwards through the list,
962 * by using iterator_next or iterator_prev respectively.
964 * Lower numbered items are draw on top of higher number items in
965 * LVS_ICON, and LVS_SMALLICON (which are the only modes where
966 * items may overlap). So, to test items, you should use
967 * iterator_next
968 * which lists the items top to bottom (in Z-order).
969 * For drawing items, you should use
970 * iterator_prev
971 * which lists the items bottom to top (in Z-order).
972 * If you keep iterating over the items after the end-of-items
973 * marker (-1) is returned, the iterator will start from the
974 * beginning. Typically, you don't need to test for -1,
975 * because iterator_{next,prev} will return TRUE if more items
976 * are to be iterated over, or FALSE otherwise.
978 * Note: the iterator is defined to be bidirectional. That is,
979 * any number of prev followed by any number of next, or
980 * five versa, should leave the iterator at the same item:
981 * prev * n, next * n = next * n, prev * n
983 * The iterator has a notion of a out-of-order, special item,
984 * which sits at the start of the list. This is used in
985 * LVS_ICON, and LVS_SMALLICON mode to handle the focused item,
986 * which needs to be first, as it may overlap other items.
988 * The code is a bit messy because we have:
989 * - a special item to deal with
990 * - simple range, or composite range
991 * - empty range.
992 * If you find bugs, or want to add features, please make sure you
993 * always check/modify *both* iterator_prev, and iterator_next.
996 /****
997 * This function iterates through the items in increasing order,
998 * but prefixed by the special item, then -1. That is:
999 * special, 1, 2, 3, ..., n, -1.
1000 * Each item is listed only once.
1002 static inline BOOL iterator_next(ITERATOR* i)
1004 if (i->nItem == -1)
1006 i->nItem = i->nSpecial;
1007 if (i->nItem != -1) return TRUE;
1009 if (i->nItem == i->nSpecial)
1011 if (i->ranges) i->index = 0;
1012 goto pickarange;
1015 i->nItem++;
1016 testitem:
1017 if (i->nItem == i->nSpecial) i->nItem++;
1018 if (i->nItem < i->range.upper) return TRUE;
1020 pickarange:
1021 if (i->ranges)
1023 if (i->index < i->ranges->hdpa->nItemCount)
1024 i->range = *(RANGE*)DPA_GetPtr(i->ranges->hdpa, i->index++);
1025 else goto end;
1027 else if (i->nItem >= i->range.upper) goto end;
1029 i->nItem = i->range.lower;
1030 if (i->nItem >= 0) goto testitem;
1031 end:
1032 i->nItem = -1;
1033 return FALSE;
1036 /****
1037 * This function iterates through the items in decreasing order,
1038 * followed by the special item, then -1. That is:
1039 * n, n-1, ..., 3, 2, 1, special, -1.
1040 * Each item is listed only once.
1042 static inline BOOL iterator_prev(ITERATOR* i)
1044 BOOL start = FALSE;
1046 if (i->nItem == -1)
1048 start = TRUE;
1049 if (i->ranges) i->index = i->ranges->hdpa->nItemCount;
1050 goto pickarange;
1052 if (i->nItem == i->nSpecial)
1054 i->nItem = -1;
1055 return FALSE;
1058 testitem:
1059 i->nItem--;
1060 if (i->nItem == i->nSpecial) i->nItem--;
1061 if (i->nItem >= i->range.lower) return TRUE;
1063 pickarange:
1064 if (i->ranges)
1066 if (i->index > 0)
1067 i->range = *(RANGE*)DPA_GetPtr(i->ranges->hdpa, --i->index);
1068 else goto end;
1070 else if (!start && i->nItem < i->range.lower) goto end;
1072 i->nItem = i->range.upper;
1073 if (i->nItem > 0) goto testitem;
1074 end:
1075 return (i->nItem = i->nSpecial) != -1;
1078 static RANGE iterator_range(ITERATOR* i)
1080 RANGE range;
1082 if (!i->ranges) return i->range;
1084 range.lower = (*(RANGE*)DPA_GetPtr(i->ranges->hdpa, 0)).lower;
1085 range.upper = (*(RANGE*)DPA_GetPtr(i->ranges->hdpa, i->ranges->hdpa->nItemCount - 1)).upper;
1086 return range;
1089 /***
1090 * Releases resources associated with this ierator.
1092 static inline void iterator_destroy(ITERATOR* i)
1094 ranges_destroy(i->ranges);
1097 /***
1098 * Create an empty iterator.
1100 static inline BOOL iterator_empty(ITERATOR* i)
1102 ZeroMemory(i, sizeof(*i));
1103 i->nItem = i->nSpecial = i->range.lower = i->range.upper = -1;
1104 return TRUE;
1107 /***
1108 * Create an iterator over a range.
1110 static inline BOOL iterator_rangeitems(ITERATOR* i, RANGE range)
1112 iterator_empty(i);
1113 i->range = range;
1114 return TRUE;
1117 /***
1118 * Create an iterator over a bunch of ranges.
1119 * Please note that the iterator will take ownership of the ranges,
1120 * and will free them upon destruction.
1122 static inline BOOL iterator_rangesitems(ITERATOR* i, RANGES ranges)
1124 iterator_empty(i);
1125 i->ranges = ranges;
1126 return TRUE;
1129 /***
1130 * Creates an iterator over the items which intersect lprc.
1132 static BOOL iterator_frameditems(ITERATOR* i, LISTVIEW_INFO* infoPtr, const RECT *lprc)
1134 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
1135 RECT frame = *lprc, rcItem, rcTemp;
1136 POINT Origin;
1138 /* in case we fail, we want to return an empty iterator */
1139 if (!iterator_empty(i)) return FALSE;
1141 LISTVIEW_GetOrigin(infoPtr, &Origin);
1143 TRACE("(lprc=%s)\n", debugrect(lprc));
1144 OffsetRect(&frame, -Origin.x, -Origin.y);
1146 if (uView == LVS_ICON || uView == LVS_SMALLICON)
1148 INT nItem;
1150 if (uView == LVS_ICON && infoPtr->nFocusedItem != -1)
1152 LISTVIEW_GetItemBox(infoPtr, infoPtr->nFocusedItem, &rcItem);
1153 if (IntersectRect(&rcTemp, &rcItem, lprc))
1154 i->nSpecial = infoPtr->nFocusedItem;
1156 if (!(iterator_rangesitems(i, ranges_create(50)))) return FALSE;
1157 /* to do better here, we need to have PosX, and PosY sorted */
1158 TRACE("building icon ranges:\n");
1159 for (nItem = 0; nItem < infoPtr->nItemCount; nItem++)
1161 rcItem.left = (LONG)DPA_GetPtr(infoPtr->hdpaPosX, nItem);
1162 rcItem.top = (LONG)DPA_GetPtr(infoPtr->hdpaPosY, nItem);
1163 rcItem.right = rcItem.left + infoPtr->nItemWidth;
1164 rcItem.bottom = rcItem.top + infoPtr->nItemHeight;
1165 if (IntersectRect(&rcTemp, &rcItem, &frame))
1166 ranges_additem(i->ranges, nItem);
1168 return TRUE;
1170 else if (uView == LVS_REPORT)
1172 RANGE range;
1174 if (frame.left >= infoPtr->nItemWidth) return TRUE;
1175 if (frame.top >= infoPtr->nItemHeight * infoPtr->nItemCount) return TRUE;
1177 range.lower = max(frame.top / infoPtr->nItemHeight, 0);
1178 range.upper = min((frame.bottom - 1) / infoPtr->nItemHeight, infoPtr->nItemCount - 1) + 1;
1179 if (range.upper <= range.lower) return TRUE;
1180 if (!iterator_rangeitems(i, range)) return FALSE;
1181 TRACE(" report=%s\n", debugrange(&i->range));
1183 else
1185 INT nPerCol = max((infoPtr->rcList.bottom - infoPtr->rcList.top) / infoPtr->nItemHeight, 1);
1186 INT nFirstRow = max(frame.top / infoPtr->nItemHeight, 0);
1187 INT nLastRow = min((frame.bottom - 1) / infoPtr->nItemHeight, nPerCol - 1);
1188 INT nFirstCol = max(frame.left / infoPtr->nItemWidth, 0);
1189 INT nLastCol = min((frame.right - 1) / infoPtr->nItemWidth, (infoPtr->nItemCount + nPerCol - 1) / nPerCol);
1190 INT lower = nFirstCol * nPerCol + nFirstRow;
1191 RANGE item_range;
1192 INT nCol;
1194 TRACE("nPerCol=%d, nFirstRow=%d, nLastRow=%d, nFirstCol=%d, nLastCol=%d, lower=%d\n",
1195 nPerCol, nFirstRow, nLastRow, nFirstCol, nLastCol, lower);
1197 if (nLastCol < nFirstCol || nLastRow < nFirstRow) return TRUE;
1199 if (!(iterator_rangesitems(i, ranges_create(nLastCol - nFirstCol + 1)))) return FALSE;
1200 TRACE("building list ranges:\n");
1201 for (nCol = nFirstCol; nCol <= nLastCol; nCol++)
1203 item_range.lower = nCol * nPerCol + nFirstRow;
1204 if(item_range.lower >= infoPtr->nItemCount) break;
1205 item_range.upper = min(nCol * nPerCol + nLastRow + 1, infoPtr->nItemCount);
1206 TRACE(" list=%s\n", debugrange(&item_range));
1207 ranges_add(i->ranges, item_range);
1211 return TRUE;
1214 /***
1215 * Creates an iterator over the items which intersect the visible region of hdc.
1217 static BOOL iterator_visibleitems(ITERATOR *i, LISTVIEW_INFO *infoPtr, HDC hdc)
1219 POINT Origin, Position;
1220 RECT rcItem, rcClip;
1221 INT rgntype;
1223 rgntype = GetClipBox(hdc, &rcClip);
1224 if (rgntype == NULLREGION) return iterator_empty(i);
1225 if (!iterator_frameditems(i, infoPtr, &rcClip)) return FALSE;
1226 if (rgntype == SIMPLEREGION) return TRUE;
1228 /* first deal with the special item */
1229 if (i->nSpecial != -1)
1231 LISTVIEW_GetItemBox(infoPtr, i->nSpecial, &rcItem);
1232 if (!RectVisible(hdc, &rcItem)) i->nSpecial = -1;
1235 /* if we can't deal with the region, we'll just go with the simple range */
1236 LISTVIEW_GetOrigin(infoPtr, &Origin);
1237 TRACE("building visible range:\n");
1238 if (!i->ranges && i->range.lower < i->range.upper)
1240 if (!(i->ranges = ranges_create(50))) return TRUE;
1241 if (!ranges_add(i->ranges, i->range))
1243 ranges_destroy(i->ranges);
1244 i->ranges = 0;
1245 return TRUE;
1249 /* now delete the invisible items from the list */
1250 while(iterator_next(i))
1252 LISTVIEW_GetItemOrigin(infoPtr, i->nItem, &Position);
1253 rcItem.left = Position.x + Origin.x;
1254 rcItem.top = Position.y + Origin.y;
1255 rcItem.right = rcItem.left + infoPtr->nItemWidth;
1256 rcItem.bottom = rcItem.top + infoPtr->nItemHeight;
1257 if (!RectVisible(hdc, &rcItem))
1258 ranges_delitem(i->ranges, i->nItem);
1260 /* the iterator should restart on the next iterator_next */
1261 TRACE("done\n");
1263 return TRUE;
1266 /******** Misc helper functions ************************************/
1268 static inline LRESULT CallWindowProcT(WNDPROC proc, HWND hwnd, UINT uMsg,
1269 WPARAM wParam, LPARAM lParam, BOOL isW)
1271 if (isW) return CallWindowProcW(proc, hwnd, uMsg, wParam, lParam);
1272 else return CallWindowProcA(proc, hwnd, uMsg, wParam, lParam);
1275 static inline BOOL is_autoarrange(LISTVIEW_INFO *infoPtr)
1277 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
1279 return ((infoPtr->dwStyle & LVS_AUTOARRANGE) || infoPtr->bAutoarrange) &&
1280 (uView == LVS_ICON || uView == LVS_SMALLICON);
1283 /******** Internal API functions ************************************/
1285 static inline COLUMN_INFO * LISTVIEW_GetColumnInfo(LISTVIEW_INFO *infoPtr, INT nSubItem)
1287 static COLUMN_INFO mainItem;
1289 if (nSubItem == 0 && infoPtr->hdpaColumns->nItemCount == 0) return &mainItem;
1290 assert (nSubItem >= 0 && nSubItem < infoPtr->hdpaColumns->nItemCount);
1291 return (COLUMN_INFO *)DPA_GetPtr(infoPtr->hdpaColumns, nSubItem);
1294 static inline void LISTVIEW_GetHeaderRect(LISTVIEW_INFO *infoPtr, INT nSubItem, RECT *lprc)
1296 *lprc = LISTVIEW_GetColumnInfo(infoPtr, nSubItem)->rcHeader;
1299 static inline BOOL LISTVIEW_GetItemW(LISTVIEW_INFO *infoPtr, LPLVITEMW lpLVItem)
1301 return LISTVIEW_GetItemT(infoPtr, lpLVItem, TRUE);
1304 /* Listview invalidation functions: use _only_ these functions to invalidate */
1306 static inline BOOL is_redrawing(LISTVIEW_INFO *infoPtr)
1308 return infoPtr->bRedraw && !infoPtr->bFirstPaint;
1311 static inline void LISTVIEW_InvalidateRect(LISTVIEW_INFO *infoPtr, const RECT* rect)
1313 if(!is_redrawing(infoPtr)) return;
1314 TRACE(" invalidating rect=%s\n", debugrect(rect));
1315 InvalidateRect(infoPtr->hwndSelf, rect, TRUE);
1318 static inline void LISTVIEW_InvalidateItem(LISTVIEW_INFO *infoPtr, INT nItem)
1320 RECT rcBox;
1322 if(!is_redrawing(infoPtr)) return;
1323 LISTVIEW_GetItemBox(infoPtr, nItem, &rcBox);
1324 LISTVIEW_InvalidateRect(infoPtr, &rcBox);
1327 static inline void LISTVIEW_InvalidateSubItem(LISTVIEW_INFO *infoPtr, INT nItem, INT nSubItem)
1329 POINT Origin, Position;
1330 RECT rcBox;
1332 if(!is_redrawing(infoPtr)) return;
1333 assert ((infoPtr->dwStyle & LVS_TYPEMASK) == LVS_REPORT);
1334 LISTVIEW_GetOrigin(infoPtr, &Origin);
1335 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position);
1336 LISTVIEW_GetHeaderRect(infoPtr, nSubItem, &rcBox);
1337 rcBox.top = 0;
1338 rcBox.bottom = infoPtr->nItemHeight;
1339 OffsetRect(&rcBox, Origin.x + Position.x, Origin.y + Position.y);
1340 LISTVIEW_InvalidateRect(infoPtr, &rcBox);
1343 static inline void LISTVIEW_InvalidateList(LISTVIEW_INFO *infoPtr)
1345 LISTVIEW_InvalidateRect(infoPtr, NULL);
1348 static inline void LISTVIEW_InvalidateColumn(LISTVIEW_INFO *infoPtr, INT nColumn)
1350 RECT rcCol;
1352 if(!is_redrawing(infoPtr)) return;
1353 LISTVIEW_GetHeaderRect(infoPtr, nColumn, &rcCol);
1354 rcCol.top = infoPtr->rcList.top;
1355 rcCol.bottom = infoPtr->rcList.bottom;
1356 LISTVIEW_InvalidateRect(infoPtr, &rcCol);
1359 /***
1360 * DESCRIPTION:
1361 * Retrieves the number of items that can fit vertically in the client area.
1363 * PARAMETER(S):
1364 * [I] infoPtr : valid pointer to the listview structure
1366 * RETURN:
1367 * Number of items per row.
1369 static inline INT LISTVIEW_GetCountPerRow(LISTVIEW_INFO *infoPtr)
1371 INT nListWidth = infoPtr->rcList.right - infoPtr->rcList.left;
1373 return max(nListWidth/infoPtr->nItemWidth, 1);
1376 /***
1377 * DESCRIPTION:
1378 * Retrieves the number of items that can fit horizontally in the client
1379 * area.
1381 * PARAMETER(S):
1382 * [I] infoPtr : valid pointer to the listview structure
1384 * RETURN:
1385 * Number of items per column.
1387 static inline INT LISTVIEW_GetCountPerColumn(LISTVIEW_INFO *infoPtr)
1389 INT nListHeight = infoPtr->rcList.bottom - infoPtr->rcList.top;
1391 return max(nListHeight / infoPtr->nItemHeight, 1);
1395 /*************************************************************************
1396 * LISTVIEW_ProcessLetterKeys
1398 * Processes keyboard messages generated by pressing the letter keys
1399 * on the keyboard.
1400 * What this does is perform a case insensitive search from the
1401 * current position with the following quirks:
1402 * - If two chars or more are pressed in quick succession we search
1403 * for the corresponding string (e.g. 'abc').
1404 * - If there is a delay we wipe away the current search string and
1405 * restart with just that char.
1406 * - If the user keeps pressing the same character, whether slowly or
1407 * fast, so that the search string is entirely composed of this
1408 * character ('aaaaa' for instance), then we search for first item
1409 * that starting with that character.
1410 * - If the user types the above character in quick succession, then
1411 * we must also search for the corresponding string ('aaaaa'), and
1412 * go to that string if there is a match.
1414 * PARAMETERS
1415 * [I] hwnd : handle to the window
1416 * [I] charCode : the character code, the actual character
1417 * [I] keyData : key data
1419 * RETURNS
1421 * Zero.
1423 * BUGS
1425 * - The current implementation has a list of characters it will
1426 * accept and it ignores averything else. In particular it will
1427 * ignore accentuated characters which seems to match what
1428 * Windows does. But I'm not sure it makes sense to follow
1429 * Windows there.
1430 * - We don't sound a beep when the search fails.
1432 * SEE ALSO
1434 * TREEVIEW_ProcessLetterKeys
1436 static INT LISTVIEW_ProcessLetterKeys(LISTVIEW_INFO *infoPtr, WPARAM charCode, LPARAM keyData)
1438 INT nItem;
1439 INT endidx,idx;
1440 LVITEMW item;
1441 WCHAR buffer[MAX_PATH];
1442 DWORD lastKeyPressTimestamp = infoPtr->lastKeyPressTimestamp;
1444 /* simple parameter checking */
1445 if (!charCode || !keyData) return 0;
1447 /* only allow the valid WM_CHARs through */
1448 if (!isalnum(charCode) &&
1449 charCode != '.' && charCode != '`' && charCode != '!' &&
1450 charCode != '@' && charCode != '#' && charCode != '$' &&
1451 charCode != '%' && charCode != '^' && charCode != '&' &&
1452 charCode != '*' && charCode != '(' && charCode != ')' &&
1453 charCode != '-' && charCode != '_' && charCode != '+' &&
1454 charCode != '=' && charCode != '\\'&& charCode != ']' &&
1455 charCode != '}' && charCode != '[' && charCode != '{' &&
1456 charCode != '/' && charCode != '?' && charCode != '>' &&
1457 charCode != '<' && charCode != ',' && charCode != '~')
1458 return 0;
1460 /* if there's one item or less, there is no where to go */
1461 if (infoPtr->nItemCount <= 1) return 0;
1463 /* update the search parameters */
1464 infoPtr->lastKeyPressTimestamp = GetTickCount();
1465 if (infoPtr->lastKeyPressTimestamp - lastKeyPressTimestamp < KEY_DELAY) {
1466 if (infoPtr->nSearchParamLength < MAX_PATH)
1467 infoPtr->szSearchParam[infoPtr->nSearchParamLength++]=charCode;
1468 if (infoPtr->charCode != charCode)
1469 infoPtr->charCode = charCode = 0;
1470 } else {
1471 infoPtr->charCode=charCode;
1472 infoPtr->szSearchParam[0]=charCode;
1473 infoPtr->nSearchParamLength=1;
1474 /* Redundant with the 1 char string */
1475 charCode=0;
1478 /* and search from the current position */
1479 nItem=-1;
1480 if (infoPtr->nFocusedItem >= 0) {
1481 endidx=infoPtr->nFocusedItem;
1482 idx=endidx;
1483 /* if looking for single character match,
1484 * then we must always move forward
1486 if (infoPtr->nSearchParamLength == 1)
1487 idx++;
1488 } else {
1489 endidx=infoPtr->nItemCount;
1490 idx=0;
1492 do {
1493 if (idx == infoPtr->nItemCount) {
1494 if (endidx == infoPtr->nItemCount || endidx == 0)
1495 break;
1496 idx=0;
1499 /* get item */
1500 item.mask = LVIF_TEXT;
1501 item.iItem = idx;
1502 item.iSubItem = 0;
1503 item.pszText = buffer;
1504 item.cchTextMax = MAX_PATH;
1505 if (!LISTVIEW_GetItemW(infoPtr, &item)) return 0;
1507 /* check for a match */
1508 if (lstrncmpiW(item.pszText,infoPtr->szSearchParam,infoPtr->nSearchParamLength) == 0) {
1509 nItem=idx;
1510 break;
1511 } else if ( (charCode != 0) && (nItem == -1) && (nItem != infoPtr->nFocusedItem) &&
1512 (lstrncmpiW(item.pszText,infoPtr->szSearchParam,1) == 0) ) {
1513 /* This would work but we must keep looking for a longer match */
1514 nItem=idx;
1516 idx++;
1517 } while (idx != endidx);
1519 if (nItem != -1)
1520 LISTVIEW_KeySelection(infoPtr, nItem);
1522 return 0;
1525 /*************************************************************************
1526 * LISTVIEW_UpdateHeaderSize [Internal]
1528 * Function to resize the header control
1530 * PARAMS
1531 * [I] hwnd : handle to a window
1532 * [I] nNewScrollPos : scroll pos to set
1534 * RETURNS
1535 * None.
1537 static void LISTVIEW_UpdateHeaderSize(LISTVIEW_INFO *infoPtr, INT nNewScrollPos)
1539 RECT winRect;
1540 POINT point[2];
1542 TRACE("nNewScrollPos=%d\n", nNewScrollPos);
1544 GetWindowRect(infoPtr->hwndHeader, &winRect);
1545 point[0].x = winRect.left;
1546 point[0].y = winRect.top;
1547 point[1].x = winRect.right;
1548 point[1].y = winRect.bottom;
1550 MapWindowPoints(HWND_DESKTOP, infoPtr->hwndSelf, point, 2);
1551 point[0].x = -nNewScrollPos;
1552 point[1].x += nNewScrollPos;
1554 SetWindowPos(infoPtr->hwndHeader,0,
1555 point[0].x,point[0].y,point[1].x,point[1].y,
1556 SWP_NOZORDER | SWP_NOACTIVATE);
1559 /***
1560 * DESCRIPTION:
1561 * Update the scrollbars. This functions should be called whenever
1562 * the content, size or view changes.
1564 * PARAMETER(S):
1565 * [I] infoPtr : valid pointer to the listview structure
1567 * RETURN:
1568 * None
1570 static void LISTVIEW_UpdateScroll(LISTVIEW_INFO *infoPtr)
1572 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
1573 SCROLLINFO horzInfo, vertInfo;
1575 if ((infoPtr->dwStyle & LVS_NOSCROLL) || !is_redrawing(infoPtr)) return;
1577 ZeroMemory(&horzInfo, sizeof(SCROLLINFO));
1578 horzInfo.cbSize = sizeof(SCROLLINFO);
1579 horzInfo.nPage = infoPtr->rcList.right - infoPtr->rcList.left;
1581 /* for now, we'll set info.nMax to the _count_, and adjust it later */
1582 if (uView == LVS_LIST)
1584 INT nPerCol = LISTVIEW_GetCountPerColumn(infoPtr);
1585 horzInfo.nMax = (infoPtr->nItemCount + nPerCol - 1) / nPerCol;
1587 /* scroll by at least one column per page */
1588 if(horzInfo.nPage < infoPtr->nItemWidth)
1589 horzInfo.nPage = infoPtr->nItemWidth;
1591 horzInfo.nPage /= infoPtr->nItemWidth;
1593 else if (uView == LVS_REPORT)
1595 horzInfo.nMax = infoPtr->nItemWidth;
1597 else /* LVS_ICON, or LVS_SMALLICON */
1599 RECT rcView;
1601 if (LISTVIEW_GetViewRect(infoPtr, &rcView)) horzInfo.nMax = rcView.right - rcView.left;
1604 horzInfo.fMask = SIF_RANGE | SIF_PAGE;
1605 horzInfo.nMax = max(horzInfo.nMax - 1, 0);
1606 SetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &horzInfo, TRUE);
1607 TRACE("horzInfo=%s\n", debugscrollinfo(&horzInfo));
1609 /* Setting the horizontal scroll can change the listview size
1610 * (and potentially everything else) so we need to recompute
1611 * everything again for the vertical scroll
1614 ZeroMemory(&vertInfo, sizeof(SCROLLINFO));
1615 vertInfo.cbSize = sizeof(SCROLLINFO);
1616 vertInfo.nPage = infoPtr->rcList.bottom - infoPtr->rcList.top;
1618 if (uView == LVS_REPORT)
1620 vertInfo.nMax = infoPtr->nItemCount;
1622 /* scroll by at least one page */
1623 if(vertInfo.nPage < infoPtr->nItemHeight)
1624 vertInfo.nPage = infoPtr->nItemHeight;
1626 vertInfo.nPage /= infoPtr->nItemHeight;
1628 else if (uView != LVS_LIST) /* LVS_ICON, or LVS_SMALLICON */
1630 RECT rcView;
1632 if (LISTVIEW_GetViewRect(infoPtr, &rcView)) vertInfo.nMax = rcView.bottom - rcView.top;
1635 vertInfo.fMask = SIF_RANGE | SIF_PAGE;
1636 vertInfo.nMax = max(vertInfo.nMax - 1, 0);
1637 SetScrollInfo(infoPtr->hwndSelf, SB_VERT, &vertInfo, TRUE);
1638 TRACE("vertInfo=%s\n", debugscrollinfo(&vertInfo));
1640 /* Update the Header Control */
1641 if (uView == LVS_REPORT)
1643 horzInfo.fMask = SIF_POS;
1644 GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &horzInfo);
1645 LISTVIEW_UpdateHeaderSize(infoPtr, horzInfo.nPos);
1650 /***
1651 * DESCRIPTION:
1652 * Shows/hides the focus rectangle.
1654 * PARAMETER(S):
1655 * [I] infoPtr : valid pointer to the listview structure
1656 * [I] fShow : TRUE to show the focus, FALSE to hide it.
1658 * RETURN:
1659 * None
1661 static void LISTVIEW_ShowFocusRect(LISTVIEW_INFO *infoPtr, BOOL fShow)
1663 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
1664 HDC hdc;
1666 TRACE("fShow=%d, nItem=%d\n", fShow, infoPtr->nFocusedItem);
1668 if (infoPtr->nFocusedItem < 0) return;
1670 /* we need some gymnastics in ICON mode to handle large items */
1671 if ( (infoPtr->dwStyle & LVS_TYPEMASK) == LVS_ICON )
1673 RECT rcBox;
1675 LISTVIEW_GetItemBox(infoPtr, infoPtr->nFocusedItem, &rcBox);
1676 if ((rcBox.bottom - rcBox.top) > infoPtr->nItemHeight)
1678 LISTVIEW_InvalidateRect(infoPtr, &rcBox);
1679 return;
1683 if (!(hdc = GetDC(infoPtr->hwndSelf))) return;
1685 /* for some reason, owner draw should work only in report mode */
1686 if ((infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && (uView == LVS_REPORT))
1688 DRAWITEMSTRUCT dis;
1689 LVITEMW item;
1691 item.iItem = infoPtr->nFocusedItem;
1692 item.iSubItem = 0;
1693 item.mask = LVIF_PARAM;
1694 if (!LISTVIEW_GetItemW(infoPtr, &item)) goto done;
1696 ZeroMemory(&dis, sizeof(dis));
1697 dis.CtlType = ODT_LISTVIEW;
1698 dis.CtlID = GetWindowLongW(infoPtr->hwndSelf, GWL_ID);
1699 dis.itemID = item.iItem;
1700 dis.itemAction = ODA_FOCUS;
1701 if (fShow) dis.itemState |= ODS_FOCUS;
1702 dis.hwndItem = infoPtr->hwndSelf;
1703 dis.hDC = hdc;
1704 LISTVIEW_GetItemBox(infoPtr, dis.itemID, &dis.rcItem);
1705 dis.itemData = item.lParam;
1707 SendMessageW(GetParent(infoPtr->hwndSelf), WM_DRAWITEM, dis.CtlID, (LPARAM)&dis);
1709 else
1711 DrawFocusRect(hdc, &infoPtr->rcFocus);
1713 done:
1714 ReleaseDC(infoPtr->hwndSelf, hdc);
1717 /***
1718 * Invalidates all visible selected items.
1720 static void LISTVIEW_InvalidateSelectedItems(LISTVIEW_INFO *infoPtr)
1722 ITERATOR i;
1724 iterator_frameditems(&i, infoPtr, &infoPtr->rcList);
1725 while(iterator_next(&i))
1727 if (LISTVIEW_GetItemState(infoPtr, i.nItem, LVIS_SELECTED))
1728 LISTVIEW_InvalidateItem(infoPtr, i.nItem);
1730 iterator_destroy(&i);
1734 /***
1735 * DESCRIPTION: [INTERNAL]
1736 * Computes an item's (left,top) corner, relative to rcView.
1737 * That is, the position has NOT been made relative to the Origin.
1738 * This is deliberate, to avoid computing the Origin over, and
1739 * over again, when this function is call in a loop. Instead,
1740 * one ca factor the computation of the Origin before the loop,
1741 * and offset the value retured by this function, on every iteration.
1743 * PARAMETER(S):
1744 * [I] infoPtr : valid pointer to the listview structure
1745 * [I] nItem : item number
1746 * [O] lpptOrig : item top, left corner
1748 * RETURN:
1749 * None.
1751 static void LISTVIEW_GetItemOrigin(LISTVIEW_INFO *infoPtr, INT nItem, LPPOINT lpptPosition)
1753 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
1755 assert(nItem >= 0 && nItem < infoPtr->nItemCount);
1757 if ((uView == LVS_SMALLICON) || (uView == LVS_ICON))
1759 lpptPosition->x = (LONG)DPA_GetPtr(infoPtr->hdpaPosX, nItem);
1760 lpptPosition->y = (LONG)DPA_GetPtr(infoPtr->hdpaPosY, nItem);
1762 else if (uView == LVS_LIST)
1764 INT nCountPerColumn = LISTVIEW_GetCountPerColumn(infoPtr);
1765 lpptPosition->x = nItem / nCountPerColumn * infoPtr->nItemWidth;
1766 lpptPosition->y = nItem % nCountPerColumn * infoPtr->nItemHeight;
1768 else /* LVS_REPORT */
1770 lpptPosition->x = 0;
1771 lpptPosition->y = nItem * infoPtr->nItemHeight;
1775 /***
1776 * DESCRIPTION: [INTERNAL]
1777 * Compute the rectangles of an item. This is to localize all
1778 * the computations in one place. If you are not interested in some
1779 * of these values, simply pass in a NULL -- the fucntion is smart
1780 * enough to compute only what's necessary. The function computes
1781 * the standard rectangles (BOUNDS, ICON, LABEL) plus a non-standard
1782 * one, the BOX rectangle. This rectangle is very cheap to compute,
1783 * and is guaranteed to contain all the other rectangles. Computing
1784 * the ICON rect is also cheap, but all the others are potentaily
1785 * expensive. This gives an easy and effective optimization when
1786 * searching (like point inclusion, or rectangle intersection):
1787 * first test against the BOX, and if TRUE, test agains the desired
1788 * rectangle.
1789 * If the function does not have all the necessary information
1790 * to computed the requested rectangles, will crash with a
1791 * failed assertion. This is done so we catch all programming
1792 * errors, given that the function is called only from our code.
1794 * We have the following 'special' meanings for a few fields:
1795 * * If LVIS_FOCUSED is set, we assume the item has the focus
1796 * This is important in ICON mode, where it might get a larger
1797 * then usual rectange
1799 * Please note that subitem support works only in REPORT mode.
1801 * PARAMETER(S):
1802 * [I] infoPtr : valid pointer to the listview structure
1803 * [I] lpLVItem : item to compute the measures for
1804 * [O] lprcBox : ptr to Box rectangle
1805 * The internal LVIR_BOX rectangle
1806 * [0] lprcState : ptr to State icon rectangle
1807 * The internal LVIR_STATE rectangle
1808 * [O] lprcIcon : ptr to Icon rectangle
1809 * Same as LVM_GETITEMRECT with LVIR_ICON
1810 * [O] lprcLabel : ptr to Label rectangle
1811 * Same as LVM_GETITEMRECT with LVIR_LABEL
1813 * RETURN:
1814 * None.
1816 static void LISTVIEW_GetItemMetrics(LISTVIEW_INFO *infoPtr, const LVITEMW *lpLVItem,
1817 LPRECT lprcBox, LPRECT lprcState,
1818 LPRECT lprcIcon, LPRECT lprcLabel)
1820 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
1821 BOOL doState = FALSE, doIcon = FALSE, doLabel = FALSE, oversizedBox = FALSE;
1822 RECT Box, State, Icon, Label;
1823 COLUMN_INFO *lpColumnInfo = NULL;
1825 TRACE("(lpLVItem=%s)\n", debuglvitem_t(lpLVItem, TRUE));
1827 /* Be smart and try to figure out the minimum we have to do */
1828 if (lpLVItem->iSubItem) assert(uView == LVS_REPORT);
1829 if (uView == LVS_ICON && (lprcBox || lprcLabel))
1831 assert((lpLVItem->mask & LVIF_STATE) && (lpLVItem->stateMask & LVIS_FOCUSED));
1832 if (lpLVItem->state & LVIS_FOCUSED) oversizedBox = doLabel = TRUE;
1834 if (lprcLabel) doLabel = TRUE;
1835 if (doLabel || lprcIcon) doIcon = TRUE;
1836 if (doIcon || lprcState) doState = TRUE;
1838 /************************************************************/
1839 /* compute the box rectangle (it should be cheap to do) */
1840 /************************************************************/
1841 if (lpLVItem->iSubItem || uView == LVS_REPORT)
1842 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, lpLVItem->iSubItem);
1844 if (lpLVItem->iSubItem)
1846 Box = lpColumnInfo->rcHeader;
1848 else
1850 Box.left = 0;
1851 Box.right = infoPtr->nItemWidth;
1853 Box.top = 0;
1854 Box.bottom = infoPtr->nItemHeight;
1856 /************************************************************/
1857 /* compute STATEICON bounding box */
1858 /************************************************************/
1859 if (doState)
1861 if (uView == LVS_ICON)
1863 State.left = Box.left - infoPtr->iconStateSize.cx - 2;
1864 if (infoPtr->himlNormal)
1865 State.left += (infoPtr->nItemWidth - infoPtr->iconSize.cx) / 2;
1866 State.top = Box.top + infoPtr->iconSize.cy - infoPtr->iconStateSize.cy + 4;
1868 else
1870 /* we need the ident in report mode, if we don't have it, we fail */
1871 State.left = Box.left;
1872 if (uView == LVS_REPORT)
1874 if (lpLVItem->iSubItem == 0)
1876 State.left += REPORT_MARGINX;
1877 assert(lpLVItem->mask & LVIF_INDENT);
1878 State.left += infoPtr->iconSize.cx * lpLVItem->iIndent;
1881 State.top = Box.top;
1883 State.right = State.left;
1884 State.bottom = State.top;
1885 if (infoPtr->himlState && lpLVItem->iSubItem == 0)
1887 State.right += infoPtr->iconStateSize.cx;
1888 State.bottom += infoPtr->iconStateSize.cy;
1890 if (lprcState) *lprcState = State;
1891 TRACE(" - state=%s\n", debugrect(&State));
1894 /************************************************************/
1895 /* compute ICON bounding box (ala LVM_GETITEMRECT) */
1896 /************************************************************/
1897 if (doIcon)
1899 if (uView == LVS_ICON)
1901 Icon.left = Box.left;
1902 if (infoPtr->himlNormal)
1903 Icon.left += (infoPtr->nItemWidth - infoPtr->iconSize.cx) / 2;
1904 Icon.top = Box.top + ICON_TOP_PADDING;
1905 Icon.right = Icon.left;
1906 Icon.bottom = Icon.top;
1907 if (infoPtr->himlNormal)
1909 Icon.right += infoPtr->iconSize.cx;
1910 Icon.bottom += infoPtr->iconSize.cy;
1913 else /* LVS_SMALLICON, LVS_LIST or LVS_REPORT */
1915 Icon.left = State.right;
1916 Icon.top = Box.top;
1917 Icon.right = Icon.left;
1918 if (infoPtr->himlSmall && (!lpColumnInfo || lpLVItem->iSubItem == 0 || (lpColumnInfo->fmt & LVCFMT_IMAGE)))
1919 Icon.right += infoPtr->iconSize.cx;
1920 Icon.bottom = Icon.top + infoPtr->nItemHeight;
1922 if(lprcIcon) *lprcIcon = Icon;
1923 TRACE(" - icon=%s\n", debugrect(&Icon));
1926 /************************************************************/
1927 /* compute LABEL bounding box (ala LVM_GETITEMRECT) */
1928 /************************************************************/
1929 if (doLabel)
1931 SIZE labelSize = { 0, 0 };
1933 /* calculate how far to the right can the label strech */
1934 Label.right = Box.right;
1935 if (uView == LVS_REPORT)
1937 if (lpLVItem->iSubItem == 0) Label = lpColumnInfo->rcHeader;
1940 if (lpLVItem->iSubItem || ((infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && uView == LVS_REPORT))
1942 labelSize.cx = infoPtr->nItemWidth;
1943 labelSize.cy = infoPtr->nItemHeight;
1944 goto calc_label;
1947 /* we need the text in non owner draw mode */
1948 assert(lpLVItem->mask & LVIF_TEXT);
1949 if (is_textT(lpLVItem->pszText, TRUE))
1951 HFONT hFont = infoPtr->hFont ? infoPtr->hFont : infoPtr->hDefaultFont;
1952 HDC hdc = GetDC(infoPtr->hwndSelf);
1953 HFONT hOldFont = SelectObject(hdc, hFont);
1954 UINT uFormat;
1955 RECT rcText;
1957 /* compute rough rectangle where the label will go */
1958 SetRectEmpty(&rcText);
1959 rcText.right = infoPtr->nItemWidth - TRAILING_LABEL_PADDING;
1960 rcText.bottom = infoPtr->nItemHeight;
1961 if (uView == LVS_ICON)
1962 rcText.bottom -= ICON_TOP_PADDING + infoPtr->iconSize.cy + ICON_BOTTOM_PADDING;
1964 /* now figure out the flags */
1965 if (uView == LVS_ICON)
1966 uFormat = oversizedBox ? LV_FL_DT_FLAGS : LV_ML_DT_FLAGS;
1967 else
1968 uFormat = LV_SL_DT_FLAGS;
1970 DrawTextW (hdc, lpLVItem->pszText, -1, &rcText, uFormat | DT_CALCRECT);
1972 labelSize.cx = min(rcText.right - rcText.left + TRAILING_LABEL_PADDING, infoPtr->nItemWidth);
1973 labelSize.cy = rcText.bottom - rcText.top;
1975 SelectObject(hdc, hOldFont);
1976 ReleaseDC(infoPtr->hwndSelf, hdc);
1979 calc_label:
1980 if (uView == LVS_ICON)
1982 Label.left = Box.left + (infoPtr->nItemWidth - labelSize.cx) / 2;
1983 Label.top = Box.top + ICON_TOP_PADDING_HITABLE +
1984 infoPtr->iconSize.cy + ICON_BOTTOM_PADDING;
1985 Label.right = Label.left + labelSize.cx;
1986 Label.bottom = Label.top + infoPtr->nItemHeight;
1987 if (!oversizedBox && labelSize.cy > infoPtr->ntmHeight)
1989 labelSize.cy = min(Box.bottom - Label.top, labelSize.cy);
1990 labelSize.cy /= infoPtr->ntmHeight;
1991 labelSize.cy = max(labelSize.cy, 1);
1992 labelSize.cy *= infoPtr->ntmHeight;
1994 Label.bottom = Label.top + labelSize.cy + HEIGHT_PADDING;
1996 else /* LVS_SMALLICON, LVS_LIST or LVS_REPORT */
1998 Label.left = Icon.right;
1999 Label.top = Box.top;
2000 Label.right = min(Label.left + labelSize.cx, Label.right);
2001 Label.bottom = Label.top + infoPtr->nItemHeight;
2004 if (lprcLabel) *lprcLabel = Label;
2005 TRACE(" - label=%s\n", debugrect(&Label));
2008 /* Fix the Box if necessary */
2009 if (lprcBox)
2011 if (oversizedBox) UnionRect(lprcBox, &Box, &Label);
2012 else *lprcBox = Box;
2014 TRACE(" - box=%s\n", debugrect(&Box));
2017 /***
2018 * DESCRIPTION: [INTERNAL]
2020 * PARAMETER(S):
2021 * [I] infoPtr : valid pointer to the listview structure
2022 * [I] nItem : item number
2023 * [O] lprcBox : ptr to Box rectangle
2025 * RETURN:
2026 * None.
2028 static void LISTVIEW_GetItemBox(LISTVIEW_INFO *infoPtr, INT nItem, LPRECT lprcBox)
2030 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
2031 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
2032 POINT Position, Origin;
2033 LVITEMW lvItem;
2035 LISTVIEW_GetOrigin(infoPtr, &Origin);
2036 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position);
2038 /* Be smart and try to figure out the minimum we have to do */
2039 lvItem.mask = 0;
2040 if (uView == LVS_ICON && infoPtr->bFocus && LISTVIEW_GetItemState(infoPtr, nItem, LVIS_FOCUSED))
2041 lvItem.mask |= LVIF_TEXT;
2042 lvItem.iItem = nItem;
2043 lvItem.iSubItem = 0;
2044 lvItem.pszText = szDispText;
2045 lvItem.cchTextMax = DISP_TEXT_SIZE;
2046 if (lvItem.mask) LISTVIEW_GetItemW(infoPtr, &lvItem);
2047 if (uView == LVS_ICON)
2049 lvItem.mask |= LVIF_STATE;
2050 lvItem.stateMask = LVIS_FOCUSED;
2051 lvItem.state = (lvItem.mask & LVIF_TEXT ? LVIS_FOCUSED : 0);
2053 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, lprcBox, 0, 0, 0);
2055 OffsetRect(lprcBox, Position.x + Origin.x, Position.y + Origin.y);
2059 /***
2060 * DESCRIPTION:
2061 * Returns the current icon position, and advances it along the top.
2062 * The returned position is not offset by Origin.
2064 * PARAMETER(S):
2065 * [I] infoPtr : valid pointer to the listview structure
2066 * [O] lpPos : will get the current icon position
2068 * RETURN:
2069 * None
2071 static void LISTVIEW_NextIconPosTop(LISTVIEW_INFO *infoPtr, LPPOINT lpPos)
2073 INT nListWidth = infoPtr->rcList.right - infoPtr->rcList.left;
2075 *lpPos = infoPtr->currIconPos;
2077 infoPtr->currIconPos.x += infoPtr->nItemWidth;
2078 if (infoPtr->currIconPos.x + infoPtr->nItemWidth <= nListWidth) return;
2080 infoPtr->currIconPos.x = 0;
2081 infoPtr->currIconPos.y += infoPtr->nItemHeight;
2085 /***
2086 * DESCRIPTION:
2087 * Returns the current icon position, and advances it down the left edge.
2088 * The returned position is not offset by Origin.
2090 * PARAMETER(S):
2091 * [I] infoPtr : valid pointer to the listview structure
2092 * [O] lpPos : will get the current icon position
2094 * RETURN:
2095 * None
2097 static void LISTVIEW_NextIconPosLeft(LISTVIEW_INFO *infoPtr, LPPOINT lpPos)
2099 INT nListHeight = infoPtr->rcList.bottom - infoPtr->rcList.top;
2101 *lpPos = infoPtr->currIconPos;
2103 infoPtr->currIconPos.y += infoPtr->nItemHeight;
2104 if (infoPtr->currIconPos.y + infoPtr->nItemHeight <= nListHeight) return;
2106 infoPtr->currIconPos.x += infoPtr->nItemWidth;
2107 infoPtr->currIconPos.y = 0;
2111 /***
2112 * DESCRIPTION:
2113 * Moves an icon to the specified position.
2114 * It takes care of invalidating the item, etc.
2116 * PARAMETER(S):
2117 * [I] infoPtr : valid pointer to the listview structure
2118 * [I] nItem : the item to move
2119 * [I] lpPos : the new icon position
2120 * [I] isNew : flags the item as being new
2122 * RETURN:
2123 * Success: TRUE
2124 * Failure: FALSE
2126 static BOOL LISTVIEW_MoveIconTo(LISTVIEW_INFO *infoPtr, INT nItem, const POINT *lppt, BOOL isNew)
2128 POINT old;
2130 if (!isNew)
2132 old.x = (LONG)DPA_GetPtr(infoPtr->hdpaPosX, nItem);
2133 old.y = (LONG)DPA_GetPtr(infoPtr->hdpaPosY, nItem);
2135 if (lppt->x == old.x && lppt->y == old.y) return TRUE;
2136 LISTVIEW_InvalidateItem(infoPtr, nItem);
2139 /* Allocating a POINTER for every item is too resource intensive,
2140 * so we'll keep the (x,y) in different arrays */
2141 if (!DPA_SetPtr(infoPtr->hdpaPosX, nItem, (void *)lppt->x)) return FALSE;
2142 if (!DPA_SetPtr(infoPtr->hdpaPosY, nItem, (void *)lppt->y)) return FALSE;
2144 LISTVIEW_InvalidateItem(infoPtr, nItem);
2146 return TRUE;
2149 /***
2150 * DESCRIPTION:
2151 * Arranges listview items in icon display mode.
2153 * PARAMETER(S):
2154 * [I] infoPtr : valid pointer to the listview structure
2155 * [I] nAlignCode : alignment code
2157 * RETURN:
2158 * SUCCESS : TRUE
2159 * FAILURE : FALSE
2161 static BOOL LISTVIEW_Arrange(LISTVIEW_INFO *infoPtr, INT nAlignCode)
2163 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
2164 void (*next_pos)(LISTVIEW_INFO *, LPPOINT);
2165 POINT pos;
2166 INT i;
2168 if (uView != LVS_ICON && uView != LVS_SMALLICON) return FALSE;
2170 TRACE("nAlignCode=%d\n", nAlignCode);
2172 if (nAlignCode == LVA_DEFAULT)
2174 if (infoPtr->dwStyle & LVS_ALIGNLEFT) nAlignCode = LVA_ALIGNLEFT;
2175 else nAlignCode = LVA_ALIGNTOP;
2178 switch (nAlignCode)
2180 case LVA_ALIGNLEFT: next_pos = LISTVIEW_NextIconPosLeft; break;
2181 case LVA_ALIGNTOP: next_pos = LISTVIEW_NextIconPosTop; break;
2182 case LVA_SNAPTOGRID: next_pos = LISTVIEW_NextIconPosTop; break; /* FIXME */
2183 default: return FALSE;
2186 infoPtr->bAutoarrange = TRUE;
2187 infoPtr->currIconPos.x = infoPtr->currIconPos.y = 0;
2188 for (i = 0; i < infoPtr->nItemCount; i++)
2190 next_pos(infoPtr, &pos);
2191 LISTVIEW_MoveIconTo(infoPtr, i, &pos, FALSE);
2194 return TRUE;
2197 /***
2198 * DESCRIPTION:
2199 * Retrieves the bounding rectangle of all the items, not offset by Origin.
2201 * PARAMETER(S):
2202 * [I] infoPtr : valid pointer to the listview structure
2203 * [O] lprcView : bounding rectangle
2205 * RETURN:
2206 * SUCCESS : TRUE
2207 * FAILURE : FALSE
2209 static void LISTVIEW_GetAreaRect(LISTVIEW_INFO *infoPtr, LPRECT lprcView)
2211 INT i, x, y;
2213 SetRectEmpty(lprcView);
2215 switch (infoPtr->dwStyle & LVS_TYPEMASK)
2217 case LVS_ICON:
2218 case LVS_SMALLICON:
2219 for (i = 0; i < infoPtr->nItemCount; i++)
2221 x = (LONG)DPA_GetPtr(infoPtr->hdpaPosX, i);
2222 y = (LONG)DPA_GetPtr(infoPtr->hdpaPosY, i);
2223 lprcView->right = max(lprcView->right, x);
2224 lprcView->bottom = max(lprcView->bottom, y);
2226 if (infoPtr->nItemCount > 0)
2228 lprcView->right += infoPtr->nItemWidth;
2229 lprcView->bottom += infoPtr->nItemHeight;
2231 break;
2233 case LVS_LIST:
2234 y = LISTVIEW_GetCountPerColumn(infoPtr);
2235 x = infoPtr->nItemCount / y;
2236 if (infoPtr->nItemCount % y) x++;
2237 lprcView->right = x * infoPtr->nItemWidth;
2238 lprcView->bottom = y * infoPtr->nItemHeight;
2239 break;
2241 case LVS_REPORT:
2242 lprcView->right = infoPtr->nItemWidth;
2243 lprcView->bottom = infoPtr->nItemCount * infoPtr->nItemHeight;
2244 break;
2248 /***
2249 * DESCRIPTION:
2250 * Retrieves the bounding rectangle of all the items.
2252 * PARAMETER(S):
2253 * [I] infoPtr : valid pointer to the listview structure
2254 * [O] lprcView : bounding rectangle
2256 * RETURN:
2257 * SUCCESS : TRUE
2258 * FAILURE : FALSE
2260 static BOOL LISTVIEW_GetViewRect(LISTVIEW_INFO *infoPtr, LPRECT lprcView)
2262 POINT ptOrigin;
2264 TRACE("(lprcView=%p)\n", lprcView);
2266 if (!lprcView) return FALSE;
2268 LISTVIEW_GetOrigin(infoPtr, &ptOrigin);
2269 LISTVIEW_GetAreaRect(infoPtr, lprcView);
2270 OffsetRect(lprcView, ptOrigin.x, ptOrigin.y);
2272 TRACE("lprcView=%s\n", debugrect(lprcView));
2274 return TRUE;
2277 /***
2278 * DESCRIPTION:
2279 * Retrieves the subitem pointer associated with the subitem index.
2281 * PARAMETER(S):
2282 * [I] hdpaSubItems : DPA handle for a specific item
2283 * [I] nSubItem : index of subitem
2285 * RETURN:
2286 * SUCCESS : subitem pointer
2287 * FAILURE : NULL
2289 static SUBITEM_INFO* LISTVIEW_GetSubItemPtr(HDPA hdpaSubItems, INT nSubItem)
2291 SUBITEM_INFO *lpSubItem;
2292 INT i;
2294 /* we should binary search here if need be */
2295 for (i = 1; i < hdpaSubItems->nItemCount; i++)
2297 lpSubItem = (SUBITEM_INFO *)DPA_GetPtr(hdpaSubItems, i);
2298 if (lpSubItem->iSubItem == nSubItem)
2299 return lpSubItem;
2302 return NULL;
2306 /***
2307 * DESCRIPTION:
2308 * Caclulates the desired item width.
2310 * PARAMETER(S):
2311 * [I] infoPtr : valid pointer to the listview structure
2313 * RETURN:
2314 * The desired item width.
2316 static INT LISTVIEW_CalculateItemWidth(LISTVIEW_INFO *infoPtr)
2318 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
2319 INT nItemWidth = 0;
2321 TRACE("uView=%d\n", uView);
2323 if (uView == LVS_ICON)
2324 nItemWidth = infoPtr->iconSpacing.cx;
2325 else if (uView == LVS_REPORT)
2327 RECT rcHeader;
2329 if (infoPtr->hdpaColumns->nItemCount > 0)
2331 LISTVIEW_GetHeaderRect(infoPtr, infoPtr->hdpaColumns->nItemCount - 1, &rcHeader);
2332 nItemWidth = rcHeader.right;
2335 else /* LVS_SMALLICON, or LVS_LIST */
2337 INT i;
2339 for (i = 0; i < infoPtr->nItemCount; i++)
2340 nItemWidth = max(LISTVIEW_GetLabelWidth(infoPtr, i), nItemWidth);
2342 if (infoPtr->himlSmall) nItemWidth += infoPtr->iconSize.cx;
2343 if (infoPtr->himlState) nItemWidth += infoPtr->iconStateSize.cx;
2345 nItemWidth = max(DEFAULT_COLUMN_WIDTH, nItemWidth + WIDTH_PADDING);
2348 return max(nItemWidth, 1);
2351 /***
2352 * DESCRIPTION:
2353 * Caclulates the desired item height.
2355 * PARAMETER(S):
2356 * [I] infoPtr : valid pointer to the listview structure
2358 * RETURN:
2359 * The desired item height.
2361 static INT LISTVIEW_CalculateItemHeight(LISTVIEW_INFO *infoPtr)
2363 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
2364 INT nItemHeight;
2366 TRACE("uView=%d\n", uView);
2368 if (uView == LVS_ICON)
2369 nItemHeight = infoPtr->iconSpacing.cy;
2370 else
2372 nItemHeight = infoPtr->ntmHeight;
2373 if (infoPtr->himlState)
2374 nItemHeight = max(nItemHeight, infoPtr->iconStateSize.cy);
2375 if (infoPtr->himlSmall)
2376 nItemHeight = max(nItemHeight, infoPtr->iconSize.cy);
2377 if (infoPtr->himlState || infoPtr->himlSmall)
2378 nItemHeight += HEIGHT_PADDING;
2381 return max(nItemHeight, 1);
2384 /***
2385 * DESCRIPTION:
2386 * Updates the width, and height of an item.
2388 * PARAMETER(S):
2389 * [I] infoPtr : valid pointer to the listview structure
2391 * RETURN:
2392 * None.
2394 static inline void LISTVIEW_UpdateItemSize(LISTVIEW_INFO *infoPtr)
2396 infoPtr->nItemWidth = LISTVIEW_CalculateItemWidth(infoPtr);
2397 infoPtr->nItemHeight = LISTVIEW_CalculateItemHeight(infoPtr);
2401 /***
2402 * DESCRIPTION:
2403 * Retrieves and saves important text metrics info for the current
2404 * Listview font.
2406 * PARAMETER(S):
2407 * [I] infoPtr : valid pointer to the listview structure
2410 static void LISTVIEW_SaveTextMetrics(LISTVIEW_INFO *infoPtr)
2412 HDC hdc = GetDC(infoPtr->hwndSelf);
2413 HFONT hFont = infoPtr->hFont ? infoPtr->hFont : infoPtr->hDefaultFont;
2414 HFONT hOldFont = SelectObject(hdc, hFont);
2415 TEXTMETRICW tm;
2417 if (GetTextMetricsW(hdc, &tm))
2419 infoPtr->ntmHeight = tm.tmHeight;
2420 infoPtr->ntmAveCharWidth = tm.tmAveCharWidth;
2422 SelectObject(hdc, hOldFont);
2423 ReleaseDC(infoPtr->hwndSelf, hdc);
2425 TRACE("tmHeight=%d\n", infoPtr->ntmHeight);
2428 /***
2429 * DESCRIPTION:
2430 * A compare function for ranges
2432 * PARAMETER(S)
2433 * [I] range1 : pointer to range 1;
2434 * [I] range2 : pointer to range 2;
2435 * [I] flags : flags
2437 * RETURNS:
2438 * > 0 : if range 1 > range 2
2439 * < 0 : if range 2 > range 1
2440 * = 0 : if range intersects range 2
2442 static INT CALLBACK ranges_cmp(LPVOID range1, LPVOID range2, LPARAM flags)
2444 INT cmp;
2446 if (((RANGE*)range1)->upper <= ((RANGE*)range2)->lower)
2447 cmp = -1;
2448 else if (((RANGE*)range2)->upper <= ((RANGE*)range1)->lower)
2449 cmp = 1;
2450 else
2451 cmp = 0;
2453 TRACE("range1=%s, range2=%s, cmp=%d\n", debugrange((RANGE*)range1), debugrange((RANGE*)range2), cmp);
2455 return cmp;
2458 #if DEBUG_RANGES
2459 #define ranges_check(ranges, desc) ranges_assert(ranges, desc, __FUNCTION__, __LINE__)
2460 #else
2461 #define ranges_check(ranges, desc) do { } while(0)
2462 #endif
2464 static void ranges_assert(RANGES ranges, LPCSTR desc, const char *func, int line)
2466 INT i;
2467 RANGE *prev, *curr;
2469 TRACE("*** Checking %s:%d:%s ***\n", func, line, desc);
2470 assert (ranges);
2471 assert (ranges->hdpa->nItemCount >= 0);
2472 ranges_dump(ranges);
2473 prev = (RANGE *)DPA_GetPtr(ranges->hdpa, 0);
2474 if (ranges->hdpa->nItemCount > 0)
2475 assert (prev->lower >= 0 && prev->lower < prev->upper);
2476 for (i = 1; i < ranges->hdpa->nItemCount; i++)
2478 curr = (RANGE *)DPA_GetPtr(ranges->hdpa, i);
2479 assert (prev->upper <= curr->lower);
2480 assert (curr->lower < curr->upper);
2481 prev = curr;
2483 TRACE("--- Done checking---\n");
2486 static RANGES ranges_create(int count)
2488 RANGES ranges = (RANGES)COMCTL32_Alloc(sizeof(struct tagRANGES));
2489 if (!ranges) return NULL;
2490 ranges->hdpa = DPA_Create(count);
2491 if (ranges->hdpa) return ranges;
2492 COMCTL32_Free(ranges);
2493 return NULL;
2496 static void ranges_clear(RANGES ranges)
2498 INT i;
2500 for(i = 0; i < ranges->hdpa->nItemCount; i++)
2501 COMCTL32_Free(DPA_GetPtr(ranges->hdpa, i));
2502 DPA_DeleteAllPtrs(ranges->hdpa);
2506 static void ranges_destroy(RANGES ranges)
2508 if (!ranges) return;
2509 ranges_clear(ranges);
2510 DPA_Destroy(ranges->hdpa);
2511 COMCTL32_Free(ranges);
2514 static RANGES ranges_clone(RANGES ranges)
2516 RANGES clone;
2517 INT i;
2519 if (!(clone = ranges_create(ranges->hdpa->nItemCount))) goto fail;
2521 for (i = 0; i < ranges->hdpa->nItemCount; i++)
2523 RANGE *newrng = (RANGE *)COMCTL32_Alloc(sizeof(RANGE));
2524 if (!newrng) goto fail;
2525 *newrng = *((RANGE*)DPA_GetPtr(ranges->hdpa, i));
2526 DPA_SetPtr(clone->hdpa, i, newrng);
2528 return clone;
2530 fail:
2531 TRACE ("clone failed\n");
2532 ranges_destroy(clone);
2533 return NULL;
2536 static RANGES ranges_diff(RANGES ranges, RANGES sub)
2538 INT i;
2540 for (i = 0; i < sub->hdpa->nItemCount; i++)
2541 ranges_del(ranges, *((RANGE *)DPA_GetPtr(sub->hdpa, i)));
2543 return ranges;
2546 static void ranges_dump(RANGES ranges)
2548 INT i;
2550 for (i = 0; i < ranges->hdpa->nItemCount; i++)
2551 TRACE(" %s\n", debugrange(DPA_GetPtr(ranges->hdpa, i)));
2554 static inline BOOL ranges_contain(RANGES ranges, INT nItem)
2556 RANGE srchrng = { nItem, nItem + 1 };
2558 TRACE("(nItem=%d)\n", nItem);
2559 ranges_check(ranges, "before contain");
2560 return DPA_Search(ranges->hdpa, &srchrng, 0, ranges_cmp, 0, DPAS_SORTED) != -1;
2563 static INT ranges_itemcount(RANGES ranges)
2565 INT i, count = 0;
2567 for (i = 0; i < ranges->hdpa->nItemCount; i++)
2569 RANGE *sel = DPA_GetPtr(ranges->hdpa, i);
2570 count += sel->upper - sel->lower;
2573 return count;
2576 static BOOL ranges_shift(RANGES ranges, INT nItem, INT delta, INT nUpper)
2578 RANGE srchrng = { nItem, nItem + 1 }, *chkrng;
2579 INT index;
2581 index = DPA_Search(ranges->hdpa, &srchrng, 0, ranges_cmp, 0, DPAS_SORTED | DPAS_INSERTAFTER);
2582 if (index == -1) return TRUE;
2584 for (; index < ranges->hdpa->nItemCount; index++)
2586 chkrng = DPA_GetPtr(ranges->hdpa, index);
2587 if (chkrng->lower >= nItem)
2588 chkrng->lower = max(min(chkrng->lower + delta, nUpper - 1), 0);
2589 if (chkrng->upper > nItem)
2590 chkrng->upper = max(min(chkrng->upper + delta, nUpper), 0);
2592 return TRUE;
2595 static BOOL ranges_add(RANGES ranges, RANGE range)
2597 RANGE srchrgn;
2598 INT index;
2600 TRACE("(%s)\n", debugrange(&range));
2601 ranges_check(ranges, "before add");
2603 /* try find overlapping regions first */
2604 srchrgn.lower = range.lower - 1;
2605 srchrgn.upper = range.upper + 1;
2606 index = DPA_Search(ranges->hdpa, &srchrgn, 0, ranges_cmp, 0, DPAS_SORTED);
2608 if (index == -1)
2610 RANGE *newrgn;
2612 TRACE("Adding new range\n");
2614 /* create the brand new range to insert */
2615 newrgn = (RANGE *)COMCTL32_Alloc(sizeof(RANGE));
2616 if(!newrgn) goto fail;
2617 *newrgn = range;
2619 /* figure out where to insert it */
2620 index = DPA_Search(ranges->hdpa, newrgn, 0, ranges_cmp, 0, DPAS_SORTED | DPAS_INSERTAFTER);
2621 TRACE("index=%d\n", index);
2622 if (index == -1) index = 0;
2624 /* and get it over with */
2625 if (DPA_InsertPtr(ranges->hdpa, index, newrgn) == -1)
2627 COMCTL32_Free(newrgn);
2628 goto fail;
2631 else
2633 RANGE *chkrgn, *mrgrgn;
2634 INT fromindex, mergeindex;
2636 chkrgn = DPA_GetPtr(ranges->hdpa, index);
2637 TRACE("Merge with %s @%d\n", debugrange(chkrgn), index);
2639 chkrgn->lower = min(range.lower, chkrgn->lower);
2640 chkrgn->upper = max(range.upper, chkrgn->upper);
2642 TRACE("New range %s @%d\n", debugrange(chkrgn), index);
2644 /* merge now common anges */
2645 fromindex = 0;
2646 srchrgn.lower = chkrgn->lower - 1;
2647 srchrgn.upper = chkrgn->upper + 1;
2651 mergeindex = DPA_Search(ranges->hdpa, &srchrgn, fromindex, ranges_cmp, 0, 0);
2652 if (mergeindex == -1) break;
2653 if (mergeindex == index)
2655 fromindex = index + 1;
2656 continue;
2659 TRACE("Merge with index %i\n", mergeindex);
2661 mrgrgn = DPA_GetPtr(ranges->hdpa, mergeindex);
2662 chkrgn->lower = min(chkrgn->lower, mrgrgn->lower);
2663 chkrgn->upper = max(chkrgn->upper, mrgrgn->upper);
2664 COMCTL32_Free(mrgrgn);
2665 DPA_DeletePtr(ranges->hdpa, mergeindex);
2666 if (mergeindex < index) index --;
2667 } while(1);
2670 ranges_check(ranges, "after add");
2671 return TRUE;
2673 fail:
2674 ranges_check(ranges, "failed add");
2675 return FALSE;
2678 static BOOL ranges_del(RANGES ranges, RANGE range)
2680 RANGE *chkrgn;
2681 INT index;
2683 TRACE("(%s)\n", debugrange(&range));
2684 ranges_check(ranges, "before del");
2686 /* we don't use DPAS_SORTED here, since we need *
2687 * to find the first overlapping range */
2688 index = DPA_Search(ranges->hdpa, &range, 0, ranges_cmp, 0, 0);
2689 while(index != -1)
2691 chkrgn = DPA_GetPtr(ranges->hdpa, index);
2693 TRACE("Matches range %s @%d\n", debugrange(chkrgn), index);
2695 /* case 1: Same range */
2696 if ( (chkrgn->upper == range.upper) &&
2697 (chkrgn->lower == range.lower) )
2699 DPA_DeletePtr(ranges->hdpa, index);
2700 break;
2702 /* case 2: engulf */
2703 else if ( (chkrgn->upper <= range.upper) &&
2704 (chkrgn->lower >= range.lower) )
2706 DPA_DeletePtr(ranges->hdpa, index);
2708 /* case 3: overlap upper */
2709 else if ( (chkrgn->upper <= range.upper) &&
2710 (chkrgn->lower < range.lower) )
2712 chkrgn->upper = range.lower;
2714 /* case 4: overlap lower */
2715 else if ( (chkrgn->upper > range.upper) &&
2716 (chkrgn->lower >= range.lower) )
2718 chkrgn->lower = range.upper;
2719 break;
2721 /* case 5: fully internal */
2722 else
2724 RANGE tmprgn = *chkrgn, *newrgn;
2726 if (!(newrgn = (RANGE *)COMCTL32_Alloc(sizeof(RANGE)))) goto fail;
2727 newrgn->lower = chkrgn->lower;
2728 newrgn->upper = range.lower;
2729 chkrgn->lower = range.upper;
2730 if (DPA_InsertPtr(ranges->hdpa, index, newrgn) == -1)
2732 COMCTL32_Free(newrgn);
2733 goto fail;
2735 chkrgn = &tmprgn;
2736 break;
2739 index = DPA_Search(ranges->hdpa, &range, index, ranges_cmp, 0, 0);
2742 ranges_check(ranges, "after del");
2743 return TRUE;
2745 fail:
2746 ranges_check(ranges, "failed del");
2747 return FALSE;
2750 /***
2751 * DESCRIPTION:
2752 * Removes all selection ranges
2754 * Parameters(s):
2755 * [I] infoPtr : valid pointer to the listview structure
2756 * [I] toSkip : item range to skip removing the selection
2758 * RETURNS:
2759 * SUCCESS : TRUE
2760 * FAILURE : TRUE
2762 static BOOL LISTVIEW_DeselectAllSkipItems(LISTVIEW_INFO *infoPtr, RANGES toSkip)
2764 LVITEMW lvItem;
2765 ITERATOR i;
2766 RANGES clone;
2768 TRACE("()\n");
2770 lvItem.state = 0;
2771 lvItem.stateMask = LVIS_SELECTED;
2773 /* need to clone the DPA because callbacks can change it */
2774 if (!(clone = ranges_clone(infoPtr->selectionRanges))) return FALSE;
2775 iterator_rangesitems(&i, ranges_diff(clone, toSkip));
2776 while(iterator_next(&i))
2777 LISTVIEW_SetItemState(infoPtr, i.nItem, &lvItem);
2778 /* note that the iterator destructor will free the cloned range */
2779 iterator_destroy(&i);
2781 return TRUE;
2784 static inline BOOL LISTVIEW_DeselectAllSkipItem(LISTVIEW_INFO *infoPtr, INT nItem)
2786 RANGES toSkip;
2788 if (!(toSkip = ranges_create(1))) return FALSE;
2789 if (nItem != -1) ranges_additem(toSkip, nItem);
2790 LISTVIEW_DeselectAllSkipItems(infoPtr, toSkip);
2791 ranges_destroy(toSkip);
2792 return TRUE;
2795 static inline BOOL LISTVIEW_DeselectAll(LISTVIEW_INFO *infoPtr)
2797 return LISTVIEW_DeselectAllSkipItem(infoPtr, -1);
2800 /***
2801 * DESCRIPTION:
2802 * Retrieves the number of items that are marked as selected.
2804 * PARAMETER(S):
2805 * [I] infoPtr : valid pointer to the listview structure
2807 * RETURN:
2808 * Number of items selected.
2810 static INT LISTVIEW_GetSelectedCount(LISTVIEW_INFO *infoPtr)
2812 INT nSelectedCount = 0;
2814 if (infoPtr->uCallbackMask & LVIS_SELECTED)
2816 INT i;
2817 for (i = 0; i < infoPtr->nItemCount; i++)
2819 if (LISTVIEW_GetItemState(infoPtr, i, LVIS_SELECTED))
2820 nSelectedCount++;
2823 else
2824 nSelectedCount = ranges_itemcount(infoPtr->selectionRanges);
2826 TRACE("nSelectedCount=%d\n", nSelectedCount);
2827 return nSelectedCount;
2830 /***
2831 * DESCRIPTION:
2832 * Manages the item focus.
2834 * PARAMETER(S):
2835 * [I] infoPtr : valid pointer to the listview structure
2836 * [I] nItem : item index
2838 * RETURN:
2839 * TRUE : focused item changed
2840 * FALSE : focused item has NOT changed
2842 static inline BOOL LISTVIEW_SetItemFocus(LISTVIEW_INFO *infoPtr, INT nItem)
2844 INT oldFocus = infoPtr->nFocusedItem;
2845 LVITEMW lvItem;
2847 if (nItem == infoPtr->nFocusedItem) return FALSE;
2849 lvItem.state = nItem == -1 ? 0 : LVIS_FOCUSED;
2850 lvItem.stateMask = LVIS_FOCUSED;
2851 LISTVIEW_SetItemState(infoPtr, nItem == -1 ? infoPtr->nFocusedItem : nItem, &lvItem);
2853 return oldFocus != infoPtr->nFocusedItem;
2856 /* Helper function for LISTVIEW_ShiftIndices *only* */
2857 static INT shift_item(LISTVIEW_INFO *infoPtr, INT nShiftItem, INT nItem, INT direction)
2859 if (nShiftItem < nItem) return nShiftItem;
2861 if (nShiftItem > nItem) return nShiftItem + direction;
2863 if (direction > 0) return nShiftItem + direction;
2865 return min(nShiftItem, infoPtr->nItemCount - 1);
2869 * DESCRIPTION:
2870 * Updates the various indices after an item has been inserted or deleted.
2872 * PARAMETER(S):
2873 * [I] infoPtr : valid pointer to the listview structure
2874 * [I] nItem : item index
2875 * [I] direction : Direction of shift, +1 or -1.
2877 * RETURN:
2878 * None
2880 static void LISTVIEW_ShiftIndices(LISTVIEW_INFO *infoPtr, INT nItem, INT direction)
2882 INT nNewFocus;
2883 BOOL bOldChange;
2885 /* temporarily disable change notification while shifting items */
2886 bOldChange = infoPtr->bDoChangeNotify;
2887 infoPtr->bDoChangeNotify = FALSE;
2889 TRACE("Shifting %iu, %i steps\n", nItem, direction);
2891 ranges_shift(infoPtr->selectionRanges, nItem, direction, infoPtr->nItemCount);
2893 assert(abs(direction) == 1);
2895 infoPtr->nSelectionMark = shift_item(infoPtr, infoPtr->nSelectionMark, nItem, direction);
2897 nNewFocus = shift_item(infoPtr, infoPtr->nFocusedItem, nItem, direction);
2898 if (nNewFocus != infoPtr->nFocusedItem)
2899 LISTVIEW_SetItemFocus(infoPtr, nNewFocus);
2901 /* But we are not supposed to modify nHotItem! */
2903 infoPtr->bDoChangeNotify = bOldChange;
2908 * DESCRIPTION:
2909 * Adds a block of selections.
2911 * PARAMETER(S):
2912 * [I] infoPtr : valid pointer to the listview structure
2913 * [I] nItem : item index
2915 * RETURN:
2916 * None
2918 static void LISTVIEW_AddGroupSelection(LISTVIEW_INFO *infoPtr, INT nItem)
2920 INT nFirst = min(infoPtr->nSelectionMark, nItem);
2921 INT nLast = max(infoPtr->nSelectionMark, nItem);
2922 INT i;
2923 LVITEMW item;
2925 if (nFirst == -1) nFirst = nItem;
2927 item.state = LVIS_SELECTED;
2928 item.stateMask = LVIS_SELECTED;
2930 /* FIXME: this is not correct LVS_OWNERDATA
2931 * setting the item states individually will generate
2932 * a LVN_ITEMCHANGED notification for each one. Instead,
2933 * we have to send a LVN_ODSTATECHANGED notification.
2934 * See MSDN documentation for LVN_ITEMCHANGED.
2936 for (i = nFirst; i <= nLast; i++)
2937 LISTVIEW_SetItemState(infoPtr,i,&item);
2941 /***
2942 * DESCRIPTION:
2943 * Sets a single group selection.
2945 * PARAMETER(S):
2946 * [I] infoPtr : valid pointer to the listview structure
2947 * [I] nItem : item index
2949 * RETURN:
2950 * None
2952 static void LISTVIEW_SetGroupSelection(LISTVIEW_INFO *infoPtr, INT nItem)
2954 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
2955 RANGES selection;
2956 LVITEMW item;
2957 ITERATOR i;
2959 if (!(selection = ranges_create(100))) return;
2961 item.state = LVIS_SELECTED;
2962 item.stateMask = LVIS_SELECTED;
2964 if ((uView == LVS_LIST) || (uView == LVS_REPORT))
2966 if (infoPtr->nSelectionMark == -1)
2968 infoPtr->nSelectionMark = nItem;
2969 ranges_additem(selection, nItem);
2971 else
2973 RANGE sel;
2975 sel.lower = min(infoPtr->nSelectionMark, nItem);
2976 sel.upper = max(infoPtr->nSelectionMark, nItem) + 1;
2977 ranges_add(selection, sel);
2980 else
2982 RECT rcItem, rcSel, rcSelMark;
2983 POINT ptItem;
2985 rcItem.left = LVIR_BOUNDS;
2986 if (!LISTVIEW_GetItemRect(infoPtr, nItem, &rcItem)) return;
2987 rcSelMark.left = LVIR_BOUNDS;
2988 if (!LISTVIEW_GetItemRect(infoPtr, infoPtr->nSelectionMark, &rcSelMark)) return;
2989 UnionRect(&rcSel, &rcItem, &rcSelMark);
2990 iterator_frameditems(&i, infoPtr, &rcSel);
2991 while(iterator_next(&i))
2993 LISTVIEW_GetItemPosition(infoPtr, i.nItem, &ptItem);
2994 if (PtInRect(&rcSel, ptItem)) ranges_additem(selection, i.nItem);
2996 iterator_destroy(&i);
2999 LISTVIEW_DeselectAllSkipItems(infoPtr, selection);
3000 iterator_rangesitems(&i, selection);
3001 while(iterator_next(&i))
3002 LISTVIEW_SetItemState(infoPtr, i.nItem, &item);
3003 /* this will also destroy the selection */
3004 iterator_destroy(&i);
3006 LISTVIEW_SetItemFocus(infoPtr, nItem);
3009 /***
3010 * DESCRIPTION:
3011 * Sets a single selection.
3013 * PARAMETER(S):
3014 * [I] infoPtr : valid pointer to the listview structure
3015 * [I] nItem : item index
3017 * RETURN:
3018 * None
3020 static void LISTVIEW_SetSelection(LISTVIEW_INFO *infoPtr, INT nItem)
3022 LVITEMW lvItem;
3024 TRACE("nItem=%d\n", nItem);
3026 LISTVIEW_DeselectAllSkipItem(infoPtr, nItem);
3028 lvItem.state = LVIS_FOCUSED | LVIS_SELECTED;
3029 lvItem.stateMask = LVIS_FOCUSED | LVIS_SELECTED;
3030 LISTVIEW_SetItemState(infoPtr, nItem, &lvItem);
3032 infoPtr->nSelectionMark = nItem;
3035 /***
3036 * DESCRIPTION:
3037 * Set selection(s) with keyboard.
3039 * PARAMETER(S):
3040 * [I] infoPtr : valid pointer to the listview structure
3041 * [I] nItem : item index
3043 * RETURN:
3044 * SUCCESS : TRUE (needs to be repainted)
3045 * FAILURE : FALSE (nothing has changed)
3047 static BOOL LISTVIEW_KeySelection(LISTVIEW_INFO *infoPtr, INT nItem)
3049 /* FIXME: pass in the state */
3050 WORD wShift = HIWORD(GetKeyState(VK_SHIFT));
3051 WORD wCtrl = HIWORD(GetKeyState(VK_CONTROL));
3052 BOOL bResult = FALSE;
3054 if ((nItem >= 0) && (nItem < infoPtr->nItemCount))
3056 if (infoPtr->dwStyle & LVS_SINGLESEL)
3058 bResult = TRUE;
3059 LISTVIEW_SetSelection(infoPtr, nItem);
3061 else
3063 if (wShift)
3065 bResult = TRUE;
3066 LISTVIEW_SetGroupSelection(infoPtr, nItem);
3068 else if (wCtrl)
3070 bResult = LISTVIEW_SetItemFocus(infoPtr, nItem);
3072 else
3074 bResult = TRUE;
3075 LISTVIEW_SetSelection(infoPtr, nItem);
3078 LISTVIEW_EnsureVisible(infoPtr, nItem, FALSE);
3081 UpdateWindow(infoPtr->hwndSelf); /* update client area */
3082 return bResult;
3086 /***
3087 * DESCRIPTION:
3088 * Called when the mouse is being actively tracked and has hovered for a specified
3089 * amount of time
3091 * PARAMETER(S):
3092 * [I] infoPtr : valid pointer to the listview structure
3093 * [I] fwKeys : key indicator
3094 * [I] pts : mouse position
3096 * RETURN:
3097 * 0 if the message was processed, non-zero if there was an error
3099 * INFO:
3100 * LVS_EX_TRACKSELECT: An item is automatically selected when the cursor remains
3101 * over the item for a certain period of time.
3104 static LRESULT LISTVIEW_MouseHover(LISTVIEW_INFO *infoPtr, WORD fwKyes, POINTS pts)
3106 if(infoPtr->dwLvExStyle & LVS_EX_TRACKSELECT)
3107 /* FIXME: select the item!!! */
3108 /*LISTVIEW_GetItemAtPt(infoPtr, pt)*/;
3110 return 0;
3113 /***
3114 * DESCRIPTION:
3115 * Called whenever WM_MOUSEMOVE is received.
3117 * PARAMETER(S):
3118 * [I] infoPtr : valid pointer to the listview structure
3119 * [I] fwKeys : key indicator
3120 * [I] pts : mouse position
3122 * RETURN:
3123 * 0 if the message is processed, non-zero if there was an error
3125 static LRESULT LISTVIEW_MouseMove(LISTVIEW_INFO *infoPtr, WORD fwKeys, POINTS pts)
3127 TRACKMOUSEEVENT trackinfo;
3129 /* see if we are supposed to be tracking mouse hovering */
3130 if(infoPtr->dwLvExStyle & LVS_EX_TRACKSELECT) {
3131 /* fill in the trackinfo struct */
3132 trackinfo.cbSize = sizeof(TRACKMOUSEEVENT);
3133 trackinfo.dwFlags = TME_QUERY;
3134 trackinfo.hwndTrack = infoPtr->hwndSelf;
3135 trackinfo.dwHoverTime = infoPtr->dwHoverTime;
3137 /* see if we are already tracking this hwnd */
3138 _TrackMouseEvent(&trackinfo);
3140 if(!(trackinfo.dwFlags & TME_HOVER)) {
3141 trackinfo.dwFlags = TME_HOVER;
3143 /* call TRACKMOUSEEVENT so we receive WM_MOUSEHOVER messages */
3144 _TrackMouseEvent(&trackinfo);
3148 return 0;
3152 /***
3153 * Tests wheather the item is assignable to a list with style lStyle
3155 static inline BOOL is_assignable_item(const LVITEMW *lpLVItem, LONG lStyle)
3157 if ( (lpLVItem->mask & LVIF_TEXT) &&
3158 (lpLVItem->pszText == LPSTR_TEXTCALLBACKW) &&
3159 (lStyle & (LVS_SORTASCENDING | LVS_SORTDESCENDING)) ) return FALSE;
3161 return TRUE;
3165 /***
3166 * DESCRIPTION:
3167 * Helper for LISTVIEW_SetItemT *only*: sets item attributes.
3169 * PARAMETER(S):
3170 * [I] infoPtr : valid pointer to the listview structure
3171 * [I] lpLVItem : valid pointer to new item atttributes
3172 * [I] isNew : the item being set is being inserted
3173 * [I] isW : TRUE if lpLVItem is Unicode, FALSE if it's ANSI
3174 * [O] bChanged : will be set to TRUE if the item really changed
3176 * RETURN:
3177 * SUCCESS : TRUE
3178 * FAILURE : FALSE
3180 static BOOL set_main_item(LISTVIEW_INFO *infoPtr, const LVITEMW *lpLVItem, BOOL isNew, BOOL isW, BOOL *bChanged)
3182 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
3183 ITEM_INFO *lpItem;
3184 NMLISTVIEW nmlv;
3185 UINT uChanged = 0;
3186 LVITEMW item;
3188 TRACE("()\n");
3190 assert(lpLVItem->iItem >= 0 && lpLVItem->iItem < infoPtr->nItemCount);
3192 if (lpLVItem->mask == 0) return TRUE;
3194 if (infoPtr->dwStyle & LVS_OWNERDATA)
3196 /* a virtual listview we stores only selection and focus */
3197 if (lpLVItem->mask & ~LVIF_STATE)
3198 return FALSE;
3199 lpItem = NULL;
3201 else
3203 HDPA hdpaSubItems = (HDPA)DPA_GetPtr(infoPtr->hdpaItems, lpLVItem->iItem);
3204 lpItem = (ITEM_INFO *)DPA_GetPtr(hdpaSubItems, 0);
3205 assert (lpItem);
3208 /* we need to get the lParam and state of the item */
3209 item.iItem = lpLVItem->iItem;
3210 item.iSubItem = lpLVItem->iSubItem;
3211 item.mask = LVIF_STATE | LVIF_PARAM;
3212 item.stateMask = ~0;
3213 item.state = 0;
3214 item.lParam = 0;
3215 if (!isNew && !LISTVIEW_GetItemW(infoPtr, &item)) return FALSE;
3217 TRACE("oldState=%x, newState=%x\n", item.state, lpLVItem->state);
3218 /* determine what fields will change */
3219 if ((lpLVItem->mask & LVIF_STATE) && ((item.state ^ lpLVItem->state) & lpLVItem->stateMask & ~infoPtr->uCallbackMask))
3220 uChanged |= LVIF_STATE;
3222 if ((lpLVItem->mask & LVIF_IMAGE) && (lpItem->hdr.iImage != lpLVItem->iImage))
3223 uChanged |= LVIF_IMAGE;
3225 if ((lpLVItem->mask & LVIF_PARAM) && (lpItem->lParam != lpLVItem->lParam))
3226 uChanged |= LVIF_PARAM;
3228 if ((lpLVItem->mask & LVIF_INDENT) && (lpItem->iIndent != lpLVItem->iIndent))
3229 uChanged |= LVIF_INDENT;
3231 if ((lpLVItem->mask & LVIF_TEXT) && textcmpWT(lpItem->hdr.pszText, lpLVItem->pszText, isW))
3232 uChanged |= LVIF_TEXT;
3234 TRACE("uChanged=0x%x\n", uChanged);
3235 if (!uChanged) return TRUE;
3236 *bChanged = TRUE;
3238 ZeroMemory(&nmlv, sizeof(NMLISTVIEW));
3239 nmlv.iItem = lpLVItem->iItem;
3240 nmlv.uNewState = (item.state & ~lpLVItem->stateMask) | (lpLVItem->state & lpLVItem->stateMask);
3241 nmlv.uOldState = item.state;
3242 nmlv.uChanged = uChanged;
3243 nmlv.lParam = item.lParam;
3245 /* send LVN_ITEMCHANGING notification, if the item is not being inserted */
3246 /* and we are _NOT_ virtual (LVS_OWERNDATA), and change notifications */
3247 /* are enabled */
3248 if(lpItem && !isNew && infoPtr->bDoChangeNotify &&
3249 notify_listview(infoPtr, LVN_ITEMCHANGING, &nmlv))
3250 return FALSE;
3252 /* copy information */
3253 if (lpLVItem->mask & LVIF_TEXT)
3254 textsetptrT(&lpItem->hdr.pszText, lpLVItem->pszText, isW);
3256 if (lpLVItem->mask & LVIF_IMAGE)
3257 lpItem->hdr.iImage = lpLVItem->iImage;
3259 if (lpLVItem->mask & LVIF_PARAM)
3260 lpItem->lParam = lpLVItem->lParam;
3262 if (lpLVItem->mask & LVIF_INDENT)
3263 lpItem->iIndent = lpLVItem->iIndent;
3265 if (uChanged & LVIF_STATE)
3267 if (lpItem && (lpLVItem->stateMask & ~infoPtr->uCallbackMask & ~(LVIS_FOCUSED | LVIS_SELECTED)))
3269 lpItem->state &= ~lpLVItem->stateMask;
3270 lpItem->state |= (lpLVItem->state & lpLVItem->stateMask);
3272 if (lpLVItem->state & lpLVItem->stateMask & ~infoPtr->uCallbackMask & LVIS_SELECTED)
3274 if (infoPtr->dwStyle & LVS_SINGLESEL) LISTVIEW_DeselectAllSkipItem(infoPtr, lpLVItem->iItem);
3275 ranges_additem(infoPtr->selectionRanges, lpLVItem->iItem);
3277 else if (lpLVItem->stateMask & LVIS_SELECTED)
3278 ranges_delitem(infoPtr->selectionRanges, lpLVItem->iItem);
3280 /* if we are asked to change focus, and we manage it, do it */
3281 if (lpLVItem->state & lpLVItem->stateMask & ~infoPtr->uCallbackMask & LVIS_FOCUSED)
3283 if (lpLVItem->state & LVIS_FOCUSED)
3285 LISTVIEW_SetItemFocus(infoPtr, -1);
3286 infoPtr->nFocusedItem = lpLVItem->iItem;
3287 LISTVIEW_EnsureVisible(infoPtr, lpLVItem->iItem, uView == LVS_LIST);
3289 else if (infoPtr->nFocusedItem == lpLVItem->iItem)
3290 infoPtr->nFocusedItem = -1;
3294 /* if we're inserting the item, we're done */
3295 if (isNew) return TRUE;
3297 /* send LVN_ITEMCHANGED notification */
3298 if (lpLVItem->mask & LVIF_PARAM) nmlv.lParam = lpLVItem->lParam;
3299 if (infoPtr->bDoChangeNotify) notify_listview(infoPtr, LVN_ITEMCHANGED, &nmlv);
3301 return TRUE;
3304 /***
3305 * DESCRIPTION:
3306 * Helper for LISTVIEW_{Set,Insert}ItemT *only*: sets subitem attributes.
3308 * PARAMETER(S):
3309 * [I] infoPtr : valid pointer to the listview structure
3310 * [I] lpLVItem : valid pointer to new subitem atttributes
3311 * [I] isW : TRUE if lpLVItem is Unicode, FALSE if it's ANSI
3312 * [O] bChanged : will be set to TRUE if the item really changed
3314 * RETURN:
3315 * SUCCESS : TRUE
3316 * FAILURE : FALSE
3318 static BOOL set_sub_item(LISTVIEW_INFO *infoPtr, const LVITEMW *lpLVItem, BOOL isW, BOOL *bChanged)
3320 HDPA hdpaSubItems;
3321 SUBITEM_INFO *lpSubItem;
3323 /* we do not support subitems for virtual listviews */
3324 if (infoPtr->dwStyle & LVS_OWNERDATA) return FALSE;
3326 /* set subitem only if column is present */
3327 if (lpLVItem->iSubItem >= infoPtr->hdpaColumns->nItemCount) return FALSE;
3329 /* First do some sanity checks */
3330 if (lpLVItem->mask & ~(LVIF_TEXT | LVIF_IMAGE)) return FALSE;
3331 if (!(lpLVItem->mask & (LVIF_TEXT | LVIF_IMAGE))) return TRUE;
3333 /* get the subitem structure, and create it if not there */
3334 hdpaSubItems = (HDPA)DPA_GetPtr(infoPtr->hdpaItems, lpLVItem->iItem);
3335 assert (hdpaSubItems);
3337 lpSubItem = LISTVIEW_GetSubItemPtr(hdpaSubItems, lpLVItem->iSubItem);
3338 if (!lpSubItem)
3340 SUBITEM_INFO *tmpSubItem;
3341 INT i;
3343 lpSubItem = (SUBITEM_INFO *)COMCTL32_Alloc(sizeof(SUBITEM_INFO));
3344 if (!lpSubItem) return FALSE;
3345 /* we could binary search here, if need be...*/
3346 for (i = 1; i < hdpaSubItems->nItemCount; i++)
3348 tmpSubItem = (SUBITEM_INFO *)DPA_GetPtr(hdpaSubItems, i);
3349 if (tmpSubItem->iSubItem > lpLVItem->iSubItem) break;
3351 if (DPA_InsertPtr(hdpaSubItems, i, lpSubItem) == -1)
3353 COMCTL32_Free(lpSubItem);
3354 return FALSE;
3356 lpSubItem->iSubItem = lpLVItem->iSubItem;
3357 *bChanged = TRUE;
3360 if (lpLVItem->mask & LVIF_IMAGE)
3361 if (lpSubItem->hdr.iImage != lpLVItem->iImage)
3363 lpSubItem->hdr.iImage = lpLVItem->iImage;
3364 *bChanged = TRUE;
3367 if (lpLVItem->mask & LVIF_TEXT)
3368 if (lpSubItem->hdr.pszText != lpLVItem->pszText)
3370 textsetptrT(&lpSubItem->hdr.pszText, lpLVItem->pszText, isW);
3371 *bChanged = TRUE;
3374 return TRUE;
3377 /***
3378 * DESCRIPTION:
3379 * Sets item attributes.
3381 * PARAMETER(S):
3382 * [I] infoPtr : valid pointer to the listview structure
3383 * [I] lpLVItem : new item atttributes
3384 * [I] isW : TRUE if lpLVItem is Unicode, FALSE if it's ANSI
3386 * RETURN:
3387 * SUCCESS : TRUE
3388 * FAILURE : FALSE
3390 static BOOL LISTVIEW_SetItemT(LISTVIEW_INFO *infoPtr, const LVITEMW *lpLVItem, BOOL isW)
3392 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
3393 LPWSTR pszText = NULL;
3394 BOOL bResult, bChanged = FALSE;
3396 TRACE("(lpLVItem=%s, isW=%d)\n", debuglvitem_t(lpLVItem, isW), isW);
3398 if (!lpLVItem || lpLVItem->iItem < 0 || lpLVItem->iItem >= infoPtr->nItemCount)
3399 return FALSE;
3401 /* For efficiency, we transform the lpLVItem->pszText to Unicode here */
3402 if ((lpLVItem->mask & LVIF_TEXT) && is_textW(lpLVItem->pszText))
3404 pszText = lpLVItem->pszText;
3405 ((LVITEMW *)lpLVItem)->pszText = textdupTtoW(lpLVItem->pszText, isW);
3408 /* actually set the fields */
3409 if (!is_assignable_item(lpLVItem, infoPtr->dwStyle)) return FALSE;
3411 if (lpLVItem->iSubItem)
3412 bResult = set_sub_item(infoPtr, lpLVItem, TRUE, &bChanged);
3413 else
3414 bResult = set_main_item(infoPtr, lpLVItem, FALSE, TRUE, &bChanged);
3416 /* redraw item, if necessary */
3417 if (bChanged && !infoPtr->bIsDrawing)
3419 /* this little optimization eliminates some nasty flicker */
3420 if ( uView == LVS_REPORT && !(infoPtr->dwStyle & LVS_OWNERDRAWFIXED) &&
3421 (!(infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT) || lpLVItem->iSubItem) )
3422 LISTVIEW_InvalidateSubItem(infoPtr, lpLVItem->iItem, lpLVItem->iSubItem);
3423 else
3424 LISTVIEW_InvalidateItem(infoPtr, lpLVItem->iItem);
3426 /* restore text */
3427 if (pszText)
3429 textfreeT(lpLVItem->pszText, isW);
3430 ((LVITEMW *)lpLVItem)->pszText = pszText;
3433 return bResult;
3436 /***
3437 * DESCRIPTION:
3438 * Retrieves the index of the item at coordinate (0, 0) of the client area.
3440 * PARAMETER(S):
3441 * [I] infoPtr : valid pointer to the listview structure
3443 * RETURN:
3444 * item index
3446 static INT LISTVIEW_GetTopIndex(LISTVIEW_INFO *infoPtr)
3448 LONG lStyle = infoPtr->dwStyle;
3449 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
3450 INT nItem = 0;
3451 SCROLLINFO scrollInfo;
3453 scrollInfo.cbSize = sizeof(SCROLLINFO);
3454 scrollInfo.fMask = SIF_POS;
3456 if (uView == LVS_LIST)
3458 if ((lStyle & WS_HSCROLL) && GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo))
3459 nItem = scrollInfo.nPos * LISTVIEW_GetCountPerColumn(infoPtr);
3461 else if (uView == LVS_REPORT)
3463 if ((lStyle & WS_VSCROLL) && GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo))
3464 nItem = scrollInfo.nPos;
3466 else
3468 if ((lStyle & WS_VSCROLL) && GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo))
3469 nItem = LISTVIEW_GetCountPerRow(infoPtr) * (scrollInfo.nPos / infoPtr->nItemHeight);
3472 TRACE("nItem=%d\n", nItem);
3474 return nItem;
3478 /***
3479 * DESCRIPTION:
3480 * Erases the background of the given rectangle
3482 * PARAMETER(S):
3483 * [I] infoPtr : valid pointer to the listview structure
3484 * [I] hdc : device context handle
3485 * [I] lprcBox : clipping rectangle
3487 * RETURN:
3488 * Success: TRUE
3489 * Failure: FALSE
3491 static inline BOOL LISTVIEW_FillBkgnd(LISTVIEW_INFO *infoPtr, HDC hdc, const RECT *lprcBox)
3493 if (!infoPtr->hBkBrush) return FALSE;
3495 TRACE("(hdc=%p, lprcBox=%s, hBkBrush=%p)\n", hdc, debugrect(lprcBox), infoPtr->hBkBrush);
3497 return FillRect(hdc, lprcBox, infoPtr->hBkBrush);
3500 /***
3501 * DESCRIPTION:
3502 * Draws an item.
3504 * PARAMETER(S):
3505 * [I] infoPtr : valid pointer to the listview structure
3506 * [I] hdc : device context handle
3507 * [I] nItem : item index
3508 * [I] nSubItem : subitem index
3509 * [I] pos : item position in client coordinates
3510 * [I] cdmode : custom draw mode
3512 * RETURN:
3513 * Success: TRUE
3514 * Failure: FALSE
3516 static BOOL LISTVIEW_DrawItem(LISTVIEW_INFO *infoPtr, HDC hdc, INT nItem, INT nSubItem, POINT pos, DWORD cdmode)
3518 UINT uFormat, uView = infoPtr->dwStyle & LVS_TYPEMASK;
3519 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
3520 WCHAR szCallback[] = { '(', 'c', 'a', 'l', 'l', 'b', 'a', 'c', 'k', ')', 0 };
3521 DWORD cditemmode = CDRF_DODEFAULT;
3522 RECT* lprcFocus, rcSelect, rcBox, rcState, rcIcon, rcLabel;
3523 NMLVCUSTOMDRAW nmlvcd;
3524 HIMAGELIST himl;
3525 LVITEMW lvItem;
3527 TRACE("(hdc=%p, nItem=%d, nSubItem=%d, pos=%s)\n", hdc, nItem, nSubItem, debugpoint(&pos));
3529 /* get information needed for drawing the item */
3530 lvItem.mask = LVIF_TEXT | LVIF_IMAGE;
3531 if (nSubItem == 0) lvItem.mask |= LVIF_STATE | LVIF_PARAM;
3532 if (uView == LVS_REPORT) lvItem.mask |= LVIF_INDENT;
3533 lvItem.stateMask = LVIS_SELECTED | LVIS_FOCUSED | LVIS_STATEIMAGEMASK;
3534 lvItem.iItem = nItem;
3535 lvItem.iSubItem = nSubItem;
3536 lvItem.state = 0;
3537 lvItem.lParam = 0;
3538 lvItem.cchTextMax = DISP_TEXT_SIZE;
3539 lvItem.pszText = szDispText;
3540 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) return FALSE;
3541 if (nSubItem > 0 && (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT))
3542 lvItem.state = LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED);
3543 if (lvItem.pszText == LPSTR_TEXTCALLBACKW) lvItem.pszText = szCallback;
3544 TRACE(" lvItem=%s\n", debuglvitem_t(&lvItem, TRUE));
3546 /* now check if we need to update the focus rectangle */
3547 lprcFocus = infoPtr->bFocus && (lvItem.state & LVIS_FOCUSED) ? &infoPtr->rcFocus : 0;
3549 if (!lprcFocus) lvItem.state &= ~LVIS_FOCUSED;
3550 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, &rcBox, &rcState, &rcIcon, &rcLabel);
3551 OffsetRect(&rcBox, pos.x, pos.y);
3552 OffsetRect(&rcState, pos.x, pos.y);
3553 OffsetRect(&rcIcon, pos.x, pos.y);
3554 OffsetRect(&rcLabel, pos.x, pos.y);
3555 TRACE(" rcBox=%s, rcState=%s, rcIcon=%s. rcLabel=%s\n",
3556 debugrect(&rcBox), debugrect(&rcState), debugrect(&rcIcon), debugrect(&rcLabel));
3558 /* fill in the custom draw structure */
3559 customdraw_fill(&nmlvcd, infoPtr, hdc, &rcBox, &lvItem);
3561 if (cdmode & CDRF_NOTIFYITEMDRAW)
3562 cditemmode = notify_prepaint (infoPtr, hdc, &nmlvcd);
3563 if (cditemmode & CDRF_SKIPDEFAULT) goto postpaint;
3565 /* in full row select, subitems, will just use main item's colors */
3566 if (nSubItem && uView == LVS_REPORT && (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT))
3567 nmlvcd.clrTextBk = CLR_NONE;
3569 /* state icons */
3570 if (infoPtr->himlState && !IsRectEmpty(&rcState))
3572 UINT uStateImage = (lvItem.state & LVIS_STATEIMAGEMASK) >> 12;
3573 if (uStateImage)
3575 TRACE("uStateImage=%d\n", uStateImage);
3576 ImageList_Draw(infoPtr->himlState, uStateImage - 1, hdc, rcState.left, rcState.top, ILD_NORMAL);
3580 /* small icons */
3581 himl = (uView == LVS_ICON ? infoPtr->himlNormal : infoPtr->himlSmall);
3582 if (himl && lvItem.iImage >= 0 && !IsRectEmpty(&rcIcon))
3584 TRACE("iImage=%d\n", lvItem.iImage);
3585 ImageList_Draw(himl, lvItem.iImage, hdc, rcIcon.left, rcIcon.top,
3586 (lvItem.state & LVIS_SELECTED) && (infoPtr->bFocus) ? ILD_SELECTED : ILD_NORMAL);
3589 /* Don't bother painting item being edited */
3590 if (infoPtr->hwndEdit && nItem == infoPtr->nEditLabelItem && nSubItem == 0) goto postpaint;
3592 /* draw the selection background, if we're drawing the main item */
3593 if (nSubItem == 0)
3595 rcSelect = rcLabel;
3596 if (uView == LVS_REPORT && (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT))
3597 rcSelect.right = rcBox.right;
3599 if (nmlvcd.clrTextBk != CLR_NONE)
3600 ExtTextOutW(hdc, rcSelect.left, rcSelect.top, ETO_OPAQUE, &rcSelect, 0, 0, 0);
3601 if(lprcFocus) *lprcFocus = rcSelect;
3604 /* figure out the text drawing flags */
3605 uFormat = (uView == LVS_ICON ? (lprcFocus ? LV_FL_DT_FLAGS : LV_ML_DT_FLAGS) : LV_SL_DT_FLAGS);
3606 if (uView == LVS_ICON)
3607 uFormat = (lprcFocus ? LV_FL_DT_FLAGS : LV_ML_DT_FLAGS);
3608 else if (nSubItem)
3610 switch (LISTVIEW_GetColumnInfo(infoPtr, nSubItem)->fmt & LVCFMT_JUSTIFYMASK)
3612 case LVCFMT_RIGHT: uFormat |= DT_RIGHT; break;
3613 case LVCFMT_CENTER: uFormat |= DT_CENTER; break;
3614 default: uFormat |= DT_LEFT;
3617 if (!(uFormat & (DT_RIGHT | DT_CENTER)))
3619 if (himl && lvItem.iImage >= 0 && !IsRectEmpty(&rcIcon)) rcLabel.left += IMAGE_PADDING;
3620 else rcLabel.left += LABEL_HOR_PADDING;
3622 else if (uFormat & DT_RIGHT) rcLabel.right -= LABEL_HOR_PADDING;
3623 DrawTextW(hdc, lvItem.pszText, -1, &rcLabel, uFormat);
3625 postpaint:
3626 if (cditemmode & CDRF_NOTIFYPOSTPAINT)
3627 notify_postpaint(infoPtr, &nmlvcd);
3628 return TRUE;
3631 /***
3632 * DESCRIPTION:
3633 * Draws listview items when in owner draw mode.
3635 * PARAMETER(S):
3636 * [I] infoPtr : valid pointer to the listview structure
3637 * [I] hdc : device context handle
3639 * RETURN:
3640 * None
3642 static void LISTVIEW_RefreshOwnerDraw(LISTVIEW_INFO *infoPtr, ITERATOR *i, HDC hdc, DWORD cdmode)
3644 UINT uID = GetWindowLongW(infoPtr->hwndSelf, GWL_ID);
3645 HWND hwndParent = GetParent(infoPtr->hwndSelf);
3646 DWORD cditemmode = CDRF_DODEFAULT;
3647 NMLVCUSTOMDRAW nmlvcd;
3648 POINT Origin, Position;
3649 DRAWITEMSTRUCT dis;
3650 LVITEMW item;
3652 TRACE("()\n");
3654 ZeroMemory(&dis, sizeof(dis));
3656 /* Get scroll info once before loop */
3657 LISTVIEW_GetOrigin(infoPtr, &Origin);
3659 /* iterate through the invalidated rows */
3660 while(iterator_next(i))
3662 item.iItem = i->nItem;
3663 item.iSubItem = 0;
3664 item.mask = LVIF_PARAM | LVIF_STATE;
3665 item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
3666 if (!LISTVIEW_GetItemW(infoPtr, &item)) continue;
3668 dis.CtlType = ODT_LISTVIEW;
3669 dis.CtlID = uID;
3670 dis.itemID = item.iItem;
3671 dis.itemAction = ODA_DRAWENTIRE;
3672 dis.itemState = 0;
3673 if (item.state & LVIS_SELECTED) dis.itemState |= ODS_SELECTED;
3674 if (infoPtr->bFocus && (item.state & LVIS_FOCUSED)) dis.itemState |= ODS_FOCUS;
3675 dis.hwndItem = infoPtr->hwndSelf;
3676 dis.hDC = hdc;
3677 LISTVIEW_GetItemOrigin(infoPtr, dis.itemID, &Position);
3678 dis.rcItem.left = Position.x + Origin.x;
3679 dis.rcItem.right = dis.rcItem.left + infoPtr->nItemWidth;
3680 dis.rcItem.top = Position.y + Origin.y;
3681 dis.rcItem.bottom = dis.rcItem.top + infoPtr->nItemHeight;
3682 dis.itemData = item.lParam;
3684 TRACE("item=%s, rcItem=%s\n", debuglvitem_t(&item, TRUE), debugrect(&dis.rcItem));
3686 if (cdmode & CDRF_NOTIFYITEMDRAW)
3688 customdraw_fill(&nmlvcd, infoPtr, hdc, &dis.rcItem, &item);
3689 cditemmode = notify_prepaint (infoPtr, hdc, &nmlvcd);
3692 if (!(cditemmode & CDRF_SKIPDEFAULT))
3693 SendMessageW(hwndParent, WM_DRAWITEM, dis.CtlID, (LPARAM)&dis);
3695 if (cditemmode & CDRF_NOTIFYPOSTPAINT)
3696 notify_postpaint(infoPtr, &nmlvcd);
3700 /***
3701 * DESCRIPTION:
3702 * Draws listview items when in report display mode.
3704 * PARAMETER(S):
3705 * [I] infoPtr : valid pointer to the listview structure
3706 * [I] hdc : device context handle
3707 * [I] cdmode : custom draw mode
3709 * RETURN:
3710 * None
3712 static void LISTVIEW_RefreshReport(LISTVIEW_INFO *infoPtr, ITERATOR *i, HDC hdc, DWORD cdmode)
3714 INT rgntype;
3715 RECT rcClip, rcItem;
3716 POINT Origin, Position;
3717 RANGE colRange;
3718 ITERATOR j;
3720 TRACE("()\n");
3722 /* figure out what to draw */
3723 rgntype = GetClipBox(hdc, &rcClip);
3724 if (rgntype == NULLREGION) return;
3726 /* Get scroll info once before loop */
3727 LISTVIEW_GetOrigin(infoPtr, &Origin);
3729 /* narrow down the columns we need to paint */
3730 for(colRange.lower = 0; colRange.lower < infoPtr->hdpaColumns->nItemCount; colRange.lower++)
3732 LISTVIEW_GetHeaderRect(infoPtr, colRange.lower, &rcItem);
3733 if (rcItem.right + Origin.x >= rcClip.left) break;
3735 for(colRange.upper = infoPtr->hdpaColumns->nItemCount; colRange.upper > 0; colRange.upper--)
3737 LISTVIEW_GetHeaderRect(infoPtr, colRange.upper - 1, &rcItem);
3738 if (rcItem.left + Origin.x < rcClip.right) break;
3740 iterator_rangeitems(&j, colRange);
3742 /* in full row select, we _have_ to draw the main item */
3743 if (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT)
3744 j.nSpecial = 0;
3746 /* iterate through the invalidated rows */
3747 while(iterator_next(i))
3749 /* iterate through the invalidated columns */
3750 while(iterator_next(&j))
3752 LISTVIEW_GetItemOrigin(infoPtr, i->nItem, &Position);
3753 Position.x += Origin.x;
3754 Position.y += Origin.y;
3756 if (rgntype == COMPLEXREGION && !((infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT) && j.nItem == 0))
3758 LISTVIEW_GetHeaderRect(infoPtr, j.nItem, &rcItem);
3759 rcItem.top = 0;
3760 rcItem.bottom = infoPtr->nItemHeight;
3761 OffsetRect(&rcItem, Position.x, Position.y);
3762 if (!RectVisible(hdc, &rcItem)) continue;
3765 LISTVIEW_DrawItem(infoPtr, hdc, i->nItem, j.nItem, Position, cdmode);
3768 iterator_destroy(&j);
3771 /***
3772 * DESCRIPTION:
3773 * Draws listview items when in list display mode.
3775 * PARAMETER(S):
3776 * [I] infoPtr : valid pointer to the listview structure
3777 * [I] hdc : device context handle
3778 * [I] cdmode : custom draw mode
3780 * RETURN:
3781 * None
3783 static void LISTVIEW_RefreshList(LISTVIEW_INFO *infoPtr, ITERATOR *i, HDC hdc, DWORD cdmode)
3785 POINT Origin, Position;
3787 /* Get scroll info once before loop */
3788 LISTVIEW_GetOrigin(infoPtr, &Origin);
3790 while(iterator_prev(i))
3792 LISTVIEW_GetItemOrigin(infoPtr, i->nItem, &Position);
3793 Position.x += Origin.x;
3794 Position.y += Origin.y;
3796 LISTVIEW_DrawItem(infoPtr, hdc, i->nItem, 0, Position, cdmode);
3801 /***
3802 * DESCRIPTION:
3803 * Draws listview items.
3805 * PARAMETER(S):
3806 * [I] infoPtr : valid pointer to the listview structure
3807 * [I] hdc : device context handle
3809 * RETURN:
3810 * NoneX
3812 static void LISTVIEW_Refresh(LISTVIEW_INFO *infoPtr, HDC hdc)
3814 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
3815 COLORREF oldTextColor, oldClrTextBk, oldClrText;
3816 NMLVCUSTOMDRAW nmlvcd;
3817 HFONT hOldFont;
3818 DWORD cdmode;
3819 INT oldBkMode;
3820 RECT rcClient;
3821 ITERATOR i;
3823 LISTVIEW_DUMP(infoPtr);
3825 infoPtr->bIsDrawing = TRUE;
3827 /* save dc values we're gonna trash while drawing */
3828 hOldFont = SelectObject(hdc, infoPtr->hFont);
3829 oldBkMode = GetBkMode(hdc);
3830 infoPtr->clrTextBkDefault = GetBkColor(hdc);
3831 oldTextColor = GetTextColor(hdc);
3833 oldClrTextBk = infoPtr->clrTextBk;
3834 oldClrText = infoPtr->clrText;
3836 GetClientRect(infoPtr->hwndSelf, &rcClient);
3837 customdraw_fill(&nmlvcd, infoPtr, hdc, &rcClient, 0);
3838 cdmode = notify_prepaint(infoPtr, hdc, &nmlvcd);
3839 if (cdmode & CDRF_SKIPDEFAULT) goto enddraw;
3841 /* Use these colors to draw the items */
3842 infoPtr->clrTextBk = nmlvcd.clrTextBk;
3843 infoPtr->clrText = nmlvcd.clrText;
3845 /* nothing to draw */
3846 if(infoPtr->nItemCount == 0) goto enddraw;
3848 /* figure out what we need to draw */
3849 iterator_visibleitems(&i, infoPtr, hdc);
3851 /* send cache hint notification */
3852 if (infoPtr->dwStyle & LVS_OWNERDATA)
3854 RANGE range = iterator_range(&i);
3855 NMLVCACHEHINT nmlv;
3857 ZeroMemory(&nmlv, sizeof(NMLVCACHEHINT));
3858 nmlv.iFrom = range.lower;
3859 nmlv.iTo = range.upper - 1;
3860 notify_hdr(infoPtr, LVN_ODCACHEHINT, &nmlv.hdr);
3863 if ((infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && (uView == LVS_REPORT))
3864 LISTVIEW_RefreshOwnerDraw(infoPtr, &i, hdc, cdmode);
3865 else
3867 if (uView == LVS_REPORT)
3868 LISTVIEW_RefreshReport(infoPtr, &i, hdc, cdmode);
3869 else /* LVS_LIST, LVS_ICON or LVS_SMALLICON */
3870 LISTVIEW_RefreshList(infoPtr, &i, hdc, cdmode);
3872 /* if we have a focus rect, draw it */
3873 if (infoPtr->bFocus)
3874 DrawFocusRect(hdc, &infoPtr->rcFocus);
3876 iterator_destroy(&i);
3878 enddraw:
3879 if (cdmode & CDRF_NOTIFYPOSTPAINT)
3880 notify_postpaint(infoPtr, &nmlvcd);
3882 infoPtr->clrTextBk = oldClrTextBk;
3883 infoPtr->clrText = oldClrText;
3885 SelectObject(hdc, hOldFont);
3886 SetBkMode(hdc, oldBkMode);
3887 SetBkColor(hdc, infoPtr->clrTextBkDefault);
3888 SetTextColor(hdc, oldTextColor);
3889 infoPtr->bIsDrawing = FALSE;
3893 /***
3894 * DESCRIPTION:
3895 * Calculates the approximate width and height of a given number of items.
3897 * PARAMETER(S):
3898 * [I] infoPtr : valid pointer to the listview structure
3899 * [I] nItemCount : number of items
3900 * [I] wWidth : width
3901 * [I] wHeight : height
3903 * RETURN:
3904 * Returns a DWORD. The width in the low word and the height in high word.
3906 static DWORD LISTVIEW_ApproximateViewRect(LISTVIEW_INFO *infoPtr, INT nItemCount,
3907 WORD wWidth, WORD wHeight)
3909 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
3910 INT nItemCountPerColumn = 1;
3911 INT nColumnCount = 0;
3912 DWORD dwViewRect = 0;
3914 if (nItemCount == -1)
3915 nItemCount = infoPtr->nItemCount;
3917 if (uView == LVS_LIST)
3919 if (wHeight == 0xFFFF)
3921 /* use current height */
3922 wHeight = infoPtr->rcList.bottom - infoPtr->rcList.top;
3925 if (wHeight < infoPtr->nItemHeight)
3926 wHeight = infoPtr->nItemHeight;
3928 if (nItemCount > 0)
3930 if (infoPtr->nItemHeight > 0)
3932 nItemCountPerColumn = wHeight / infoPtr->nItemHeight;
3933 if (nItemCountPerColumn == 0)
3934 nItemCountPerColumn = 1;
3936 if (nItemCount % nItemCountPerColumn != 0)
3937 nColumnCount = nItemCount / nItemCountPerColumn;
3938 else
3939 nColumnCount = nItemCount / nItemCountPerColumn + 1;
3943 /* Microsoft padding magic */
3944 wHeight = nItemCountPerColumn * infoPtr->nItemHeight + 2;
3945 wWidth = nColumnCount * infoPtr->nItemWidth + 2;
3947 dwViewRect = MAKELONG(wWidth, wHeight);
3949 else if (uView == LVS_REPORT)
3950 FIXME("uView == LVS_REPORT: not implemented\n");
3951 else if (uView == LVS_SMALLICON)
3952 FIXME("uView == LVS_SMALLICON: not implemented\n");
3953 else if (uView == LVS_ICON)
3954 FIXME("uView == LVS_ICON: not implemented\n");
3956 return dwViewRect;
3959 /* << LISTVIEW_CreateDragImage >> */
3962 /***
3963 * DESCRIPTION:
3964 * Removes all listview items and subitems.
3966 * PARAMETER(S):
3967 * [I] infoPtr : valid pointer to the listview structure
3969 * RETURN:
3970 * SUCCESS : TRUE
3971 * FAILURE : FALSE
3973 static BOOL LISTVIEW_DeleteAllItems(LISTVIEW_INFO *infoPtr)
3975 NMLISTVIEW nmlv;
3976 HDPA hdpaSubItems = NULL;
3977 BOOL bSuppress;
3978 ITEMHDR *hdrItem;
3979 INT i, j;
3981 TRACE("()\n");
3983 /* we do it directly, to avoid notifications */
3984 ranges_clear(infoPtr->selectionRanges);
3985 infoPtr->nSelectionMark = -1;
3986 infoPtr->nFocusedItem = -1;
3987 SetRectEmpty(&infoPtr->rcFocus);
3988 /* But we are supposed to leave nHotItem as is! */
3991 /* send LVN_DELETEALLITEMS notification */
3992 ZeroMemory(&nmlv, sizeof(NMLISTVIEW));
3993 nmlv.iItem = -1;
3994 bSuppress = notify_listview(infoPtr, LVN_DELETEALLITEMS, &nmlv);
3996 for (i = infoPtr->nItemCount - 1; i >= 0; i--)
3998 /* send LVN_DELETEITEM notification, if not supressed */
3999 if (!bSuppress) notify_deleteitem(infoPtr, i);
4000 if (!(infoPtr->dwStyle & LVS_OWNERDATA))
4002 hdpaSubItems = (HDPA)DPA_GetPtr(infoPtr->hdpaItems, i);
4003 for (j = 0; j < hdpaSubItems->nItemCount; j++)
4005 hdrItem = (ITEMHDR *)DPA_GetPtr(hdpaSubItems, j);
4006 if (is_textW(hdrItem->pszText)) COMCTL32_Free(hdrItem->pszText);
4007 COMCTL32_Free(hdrItem);
4009 DPA_Destroy(hdpaSubItems);
4010 DPA_DeletePtr(infoPtr->hdpaItems, i);
4012 DPA_DeletePtr(infoPtr->hdpaPosX, i);
4013 DPA_DeletePtr(infoPtr->hdpaPosY, i);
4014 infoPtr->nItemCount --;
4017 LISTVIEW_UpdateScroll(infoPtr);
4019 LISTVIEW_InvalidateList(infoPtr);
4021 return TRUE;
4024 /***
4025 * DESCRIPTION:
4026 * Scrolls, and updates the columns, when a column is changing width.
4028 * PARAMETER(S):
4029 * [I] infoPtr : valid pointer to the listview structure
4030 * [I] nColumn : column to scroll
4031 * [I] dx : amount of scroll, in pixels
4033 * RETURN:
4034 * None.
4036 static void LISTVIEW_ScrollColumns(LISTVIEW_INFO *infoPtr, INT nColumn, INT dx)
4038 COLUMN_INFO *lpColumnInfo;
4039 RECT rcOld, rcCol;
4040 INT nCol;
4042 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, min(nColumn, infoPtr->hdpaColumns->nItemCount - 1));
4043 rcCol = lpColumnInfo->rcHeader;
4044 if (nColumn >= infoPtr->hdpaColumns->nItemCount)
4045 rcCol.left = rcCol.right;
4047 /* ajust the other columns */
4048 for (nCol = nColumn; nCol < infoPtr->hdpaColumns->nItemCount; nCol++)
4050 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, nCol);
4051 lpColumnInfo->rcHeader.left += dx;
4052 lpColumnInfo->rcHeader.right += dx;
4055 /* do not update screen if not in report mode */
4056 if (!is_redrawing(infoPtr) || (infoPtr->dwStyle & LVS_TYPEMASK) != LVS_REPORT) return;
4058 /* if we have a focus, must first erase the focus rect */
4059 if (infoPtr->bFocus) LISTVIEW_ShowFocusRect(infoPtr, FALSE);
4061 /* Need to reset the item width when inserting a new column */
4062 infoPtr->nItemWidth += dx;
4064 LISTVIEW_UpdateScroll(infoPtr);
4066 /* scroll to cover the deleted column, and invalidate for redraw */
4067 rcOld = infoPtr->rcList;
4068 rcOld.left = rcCol.left;
4069 ScrollWindowEx(infoPtr->hwndSelf, dx, 0, &rcOld, &rcOld, 0, 0, SW_ERASE | SW_INVALIDATE);
4071 /* we can restore focus now */
4072 if (infoPtr->bFocus) LISTVIEW_ShowFocusRect(infoPtr, TRUE);
4075 /***
4076 * DESCRIPTION:
4077 * Removes a column from the listview control.
4079 * PARAMETER(S):
4080 * [I] infoPtr : valid pointer to the listview structure
4081 * [I] nColumn : column index
4083 * RETURN:
4084 * SUCCESS : TRUE
4085 * FAILURE : FALSE
4087 static BOOL LISTVIEW_DeleteColumn(LISTVIEW_INFO *infoPtr, INT nColumn)
4089 RECT rcCol;
4091 TRACE("nColumn=%d\n", nColumn);
4093 if (nColumn < 0 || infoPtr->hdpaColumns->nItemCount == 0
4094 || nColumn >= infoPtr->hdpaColumns->nItemCount) return FALSE;
4096 /* While the MSDN specifically says that column zero should not be deleted,
4097 it does in fact work on WinNT, and at least one app depends on it. On
4098 WinNT, deleting column zero deletes the last column of items but the
4099 first header. Since no app will ever depend on that bizarre behavior,
4100 we just delete the last column including the header.
4102 if (nColumn == 0)
4103 nColumn = infoPtr->hdpaColumns->nItemCount - 1;
4105 LISTVIEW_GetHeaderRect(infoPtr, nColumn, &rcCol);
4107 if (!Header_DeleteItem(infoPtr->hwndHeader, nColumn))
4108 return FALSE;
4110 COMCTL32_Free(DPA_GetPtr(infoPtr->hdpaColumns, nColumn));
4111 DPA_DeletePtr(infoPtr->hdpaColumns, nColumn);
4113 if (!(infoPtr->dwStyle & LVS_OWNERDATA))
4115 SUBITEM_INFO *lpSubItem, *lpDelItem;
4116 HDPA hdpaSubItems;
4117 INT nItem, nSubItem, i;
4119 if (nColumn == 0)
4120 return LISTVIEW_DeleteAllItems(infoPtr);
4122 for (nItem = 0; nItem < infoPtr->nItemCount; nItem++)
4124 hdpaSubItems = (HDPA)DPA_GetPtr(infoPtr->hdpaItems, nItem);
4125 nSubItem = 0;
4126 lpDelItem = 0;
4127 for (i = 1; i < hdpaSubItems->nItemCount; i++)
4129 lpSubItem = (SUBITEM_INFO *)DPA_GetPtr(hdpaSubItems, i);
4130 if (lpSubItem->iSubItem == nColumn)
4132 nSubItem = i;
4133 lpDelItem = lpSubItem;
4135 else if (lpSubItem->iSubItem > nColumn)
4137 lpSubItem->iSubItem--;
4141 /* if we found our subitem, zapp it */
4142 if (nSubItem > 0)
4144 /* free string */
4145 if (is_textW(lpDelItem->hdr.pszText))
4146 COMCTL32_Free(lpDelItem->hdr.pszText);
4148 /* free item */
4149 COMCTL32_Free(lpDelItem);
4151 /* free dpa memory */
4152 DPA_DeletePtr(hdpaSubItems, nSubItem);
4157 /* update the other column info */
4158 LISTVIEW_ScrollColumns(infoPtr, nColumn, -(rcCol.right - rcCol.left));
4160 return TRUE;
4163 /***
4164 * DESCRIPTION:
4165 * Invalidates the listview after an item's insertion or deletion.
4167 * PARAMETER(S):
4168 * [I] infoPtr : valid pointer to the listview structure
4169 * [I] nItem : item index
4170 * [I] dir : -1 if deleting, 1 if inserting
4172 * RETURN:
4173 * None
4175 static void LISTVIEW_ScrollOnInsert(LISTVIEW_INFO *infoPtr, INT nItem, INT dir)
4177 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
4178 INT nPerCol, nItemCol, nItemRow;
4179 RECT rcScroll;
4180 POINT Origin;
4182 /* if we don't refresh, what's the point of scrolling? */
4183 if (!is_redrawing(infoPtr)) return;
4185 assert (abs(dir) == 1);
4187 /* arrange icons if autoarrange is on */
4188 if (is_autoarrange(infoPtr))
4190 BOOL arrange = TRUE;
4191 if (dir < 0 && nItem >= infoPtr->nItemCount) arrange = FALSE;
4192 if (dir > 0 && nItem == infoPtr->nItemCount - 1) arrange = FALSE;
4193 if (arrange) LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
4196 /* scrollbars need updating */
4197 LISTVIEW_UpdateScroll(infoPtr);
4199 /* figure out the item's position */
4200 if (uView == LVS_REPORT)
4201 nPerCol = infoPtr->nItemCount + 1;
4202 else if (uView == LVS_LIST)
4203 nPerCol = LISTVIEW_GetCountPerColumn(infoPtr);
4204 else /* LVS_ICON, or LVS_SMALLICON */
4205 return;
4207 nItemCol = nItem / nPerCol;
4208 nItemRow = nItem % nPerCol;
4209 LISTVIEW_GetOrigin(infoPtr, &Origin);
4211 /* move the items below up a slot */
4212 rcScroll.left = nItemCol * infoPtr->nItemWidth;
4213 rcScroll.top = nItemRow * infoPtr->nItemHeight;
4214 rcScroll.right = rcScroll.left + infoPtr->nItemWidth;
4215 rcScroll.bottom = nPerCol * infoPtr->nItemHeight;
4216 OffsetRect(&rcScroll, Origin.x, Origin.y);
4217 TRACE("rcScroll=%s, dx=%d\n", debugrect(&rcScroll), dir * infoPtr->nItemHeight);
4218 if (IntersectRect(&rcScroll, &rcScroll, &infoPtr->rcList))
4220 TRACE("Scrolling rcScroll=%s, rcList=%s\n", debugrect(&rcScroll), debugrect(&infoPtr->rcList));
4221 ScrollWindowEx(infoPtr->hwndSelf, 0, dir * infoPtr->nItemHeight,
4222 &rcScroll, &rcScroll, 0, 0, SW_ERASE | SW_INVALIDATE);
4225 /* report has only that column, so we're done */
4226 if (uView == LVS_REPORT) return;
4228 /* now for LISTs, we have to deal with the columns to the right */
4229 rcScroll.left = (nItemCol + 1) * infoPtr->nItemWidth;
4230 rcScroll.top = 0;
4231 rcScroll.right = (infoPtr->nItemCount / nPerCol + 1) * infoPtr->nItemWidth;
4232 rcScroll.bottom = nPerCol * infoPtr->nItemHeight;
4233 OffsetRect(&rcScroll, Origin.x, Origin.y);
4234 if (IntersectRect(&rcScroll, &rcScroll, &infoPtr->rcList))
4235 ScrollWindowEx(infoPtr->hwndSelf, 0, dir * infoPtr->nItemHeight,
4236 &rcScroll, &rcScroll, 0, 0, SW_ERASE | SW_INVALIDATE);
4239 /***
4240 * DESCRIPTION:
4241 * Removes an item from the listview control.
4243 * PARAMETER(S):
4244 * [I] infoPtr : valid pointer to the listview structure
4245 * [I] nItem : item index
4247 * RETURN:
4248 * SUCCESS : TRUE
4249 * FAILURE : FALSE
4251 static BOOL LISTVIEW_DeleteItem(LISTVIEW_INFO *infoPtr, INT nItem)
4253 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
4254 LVITEMW item;
4256 TRACE("(nItem=%d)\n", nItem);
4258 if (nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
4260 /* remove selection, and focus */
4261 item.state = 0;
4262 item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
4263 LISTVIEW_SetItemState(infoPtr, nItem, &item);
4265 /* send LVN_DELETEITEM notification. */
4266 notify_deleteitem(infoPtr, nItem);
4268 /* we need to do this here, because we'll be deleting stuff */
4269 if (uView == LVS_SMALLICON || uView == LVS_ICON)
4270 LISTVIEW_InvalidateItem(infoPtr, nItem);
4272 if (!(infoPtr->dwStyle & LVS_OWNERDATA))
4274 HDPA hdpaSubItems;
4275 ITEMHDR *hdrItem;
4276 INT i;
4278 hdpaSubItems = (HDPA)DPA_DeletePtr(infoPtr->hdpaItems, nItem);
4279 for (i = 0; i < hdpaSubItems->nItemCount; i++)
4281 hdrItem = (ITEMHDR *)DPA_GetPtr(hdpaSubItems, i);
4282 if (is_textW(hdrItem->pszText)) COMCTL32_Free(hdrItem->pszText);
4283 COMCTL32_Free(hdrItem);
4285 DPA_Destroy(hdpaSubItems);
4288 if (uView == LVS_SMALLICON || uView == LVS_ICON)
4290 DPA_DeletePtr(infoPtr->hdpaPosX, nItem);
4291 DPA_DeletePtr(infoPtr->hdpaPosY, nItem);
4294 infoPtr->nItemCount--;
4295 LISTVIEW_ShiftIndices(infoPtr, nItem, -1);
4297 /* now is the invalidation fun */
4298 LISTVIEW_ScrollOnInsert(infoPtr, nItem, -1);
4299 return TRUE;
4303 /***
4304 * DESCRIPTION:
4305 * Callback implementation for editlabel control
4307 * PARAMETER(S):
4308 * [I] infoPtr : valid pointer to the listview structure
4309 * [I] pszText : modified text
4310 * [I] isW : TRUE if psxText is Unicode, FALSE if it's ANSI
4312 * RETURN:
4313 * SUCCESS : TRUE
4314 * FAILURE : FALSE
4316 static BOOL LISTVIEW_EndEditLabelT(LISTVIEW_INFO *infoPtr, LPWSTR pszText, BOOL isW)
4318 NMLVDISPINFOW dispInfo;
4320 TRACE("(pszText=%s, isW=%d)\n", debugtext_t(pszText, isW), isW);
4322 ZeroMemory(&dispInfo, sizeof(dispInfo));
4323 dispInfo.item.mask = LVIF_PARAM | LVIF_STATE;
4324 dispInfo.item.iItem = infoPtr->nEditLabelItem;
4325 dispInfo.item.iSubItem = 0;
4326 dispInfo.item.stateMask = ~0;
4327 if (!LISTVIEW_GetItemW(infoPtr, &dispInfo.item)) return FALSE;
4328 /* add the text from the edit in */
4329 dispInfo.item.mask |= LVIF_TEXT;
4330 dispInfo.item.pszText = pszText;
4331 dispInfo.item.cchTextMax = textlenT(pszText, isW);
4333 /* Do we need to update the Item Text */
4334 if (!notify_dispinfoT(infoPtr, LVN_ENDLABELEDITW, &dispInfo, isW)) return FALSE;
4335 if (!pszText) return TRUE;
4337 ZeroMemory(&dispInfo, sizeof(dispInfo));
4338 dispInfo.item.mask = LVIF_TEXT;
4339 dispInfo.item.iItem = infoPtr->nEditLabelItem;
4340 dispInfo.item.iSubItem = 0;
4341 dispInfo.item.pszText = pszText;
4342 dispInfo.item.cchTextMax = textlenT(pszText, isW);
4343 return LISTVIEW_SetItemT(infoPtr, &dispInfo.item, isW);
4346 /***
4347 * DESCRIPTION:
4348 * Begin in place editing of specified list view item
4350 * PARAMETER(S):
4351 * [I] infoPtr : valid pointer to the listview structure
4352 * [I] nItem : item index
4353 * [I] isW : TRUE if it's a Unicode req, FALSE if ASCII
4355 * RETURN:
4356 * SUCCESS : TRUE
4357 * FAILURE : FALSE
4359 static HWND LISTVIEW_EditLabelT(LISTVIEW_INFO *infoPtr, INT nItem, BOOL isW)
4361 WCHAR szDispText[DISP_TEXT_SIZE] = { 0 };
4362 NMLVDISPINFOW dispInfo;
4363 RECT rect;
4365 TRACE("(nItem=%d, isW=%d)\n", nItem, isW);
4367 if (~infoPtr->dwStyle & LVS_EDITLABELS) return 0;
4368 if (nItem < 0 || nItem >= infoPtr->nItemCount) return 0;
4370 infoPtr->nEditLabelItem = nItem;
4372 /* Is the EditBox still there, if so remove it */
4373 if(infoPtr->hwndEdit != 0)
4375 SetFocus(infoPtr->hwndSelf);
4376 infoPtr->hwndEdit = 0;
4379 LISTVIEW_SetSelection(infoPtr, nItem);
4380 LISTVIEW_SetItemFocus(infoPtr, nItem);
4381 LISTVIEW_InvalidateItem(infoPtr, nItem);
4383 rect.left = LVIR_LABEL;
4384 if (!LISTVIEW_GetItemRect(infoPtr, nItem, &rect)) return 0;
4386 ZeroMemory(&dispInfo, sizeof(dispInfo));
4387 dispInfo.item.mask = LVIF_PARAM | LVIF_STATE | LVIF_TEXT;
4388 dispInfo.item.iItem = nItem;
4389 dispInfo.item.iSubItem = 0;
4390 dispInfo.item.stateMask = ~0;
4391 dispInfo.item.pszText = szDispText;
4392 dispInfo.item.cchTextMax = DISP_TEXT_SIZE;
4393 if (!LISTVIEW_GetItemT(infoPtr, &dispInfo.item, isW)) return 0;
4395 infoPtr->hwndEdit = CreateEditLabelT(infoPtr, dispInfo.item.pszText, WS_VISIBLE,
4396 rect.left-2, rect.top-1, 0, rect.bottom - rect.top+2, isW);
4397 if (!infoPtr->hwndEdit) return 0;
4399 if (notify_dispinfoT(infoPtr, LVN_BEGINLABELEDITW, &dispInfo, isW))
4401 SendMessageW(infoPtr->hwndEdit, WM_CLOSE, 0, 0);
4402 infoPtr->hwndEdit = 0;
4403 return 0;
4406 ShowWindow(infoPtr->hwndEdit, SW_NORMAL);
4407 SetFocus(infoPtr->hwndEdit);
4408 SendMessageW(infoPtr->hwndEdit, EM_SETSEL, 0, -1);
4409 return infoPtr->hwndEdit;
4413 /***
4414 * DESCRIPTION:
4415 * Ensures the specified item is visible, scrolling into view if necessary.
4417 * PARAMETER(S):
4418 * [I] infoPtr : valid pointer to the listview structure
4419 * [I] nItem : item index
4420 * [I] bPartial : partially or entirely visible
4422 * RETURN:
4423 * SUCCESS : TRUE
4424 * FAILURE : FALSE
4426 static BOOL LISTVIEW_EnsureVisible(LISTVIEW_INFO *infoPtr, INT nItem, BOOL bPartial)
4428 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
4429 INT nScrollPosHeight = 0;
4430 INT nScrollPosWidth = 0;
4431 INT nHorzAdjust = 0;
4432 INT nVertAdjust = 0;
4433 INT nHorzDiff = 0;
4434 INT nVertDiff = 0;
4435 RECT rcItem, rcTemp;
4437 rcItem.left = LVIR_BOUNDS;
4438 if (!LISTVIEW_GetItemRect(infoPtr, nItem, &rcItem)) return FALSE;
4440 if (bPartial && IntersectRect(&rcTemp, &infoPtr->rcList, &rcItem)) return TRUE;
4442 if (rcItem.left < infoPtr->rcList.left || rcItem.right > infoPtr->rcList.right)
4444 /* scroll left/right, but in LVS_REPORT mode */
4445 if (uView == LVS_LIST)
4446 nScrollPosWidth = infoPtr->nItemWidth;
4447 else if ((uView == LVS_SMALLICON) || (uView == LVS_ICON))
4448 nScrollPosWidth = 1;
4450 if (rcItem.left < infoPtr->rcList.left)
4452 nHorzAdjust = -1;
4453 if (uView != LVS_REPORT) nHorzDiff = rcItem.left - infoPtr->rcList.left;
4455 else
4457 nHorzAdjust = 1;
4458 if (uView != LVS_REPORT) nHorzDiff = rcItem.right - infoPtr->rcList.right;
4462 if (rcItem.top < infoPtr->rcList.top || rcItem.bottom > infoPtr->rcList.bottom)
4464 /* scroll up/down, but not in LVS_LIST mode */
4465 if (uView == LVS_REPORT)
4466 nScrollPosHeight = infoPtr->nItemHeight;
4467 else if ((uView == LVS_ICON) || (uView == LVS_SMALLICON))
4468 nScrollPosHeight = 1;
4470 if (rcItem.top < infoPtr->rcList.top)
4472 nVertAdjust = -1;
4473 if (uView != LVS_LIST) nVertDiff = rcItem.top - infoPtr->rcList.top;
4475 else
4477 nVertAdjust = 1;
4478 if (uView != LVS_LIST) nVertDiff = rcItem.bottom - infoPtr->rcList.bottom;
4482 if (!nScrollPosWidth && !nScrollPosHeight) return TRUE;
4484 if (nScrollPosWidth)
4486 INT diff = nHorzDiff / nScrollPosWidth;
4487 if (nHorzDiff % nScrollPosWidth) diff += nHorzAdjust;
4488 LISTVIEW_HScroll(infoPtr, SB_INTERNAL, diff, 0);
4491 if (nScrollPosHeight)
4493 INT diff = nVertDiff / nScrollPosHeight;
4494 if (nVertDiff % nScrollPosHeight) diff += nVertAdjust;
4495 LISTVIEW_VScroll(infoPtr, SB_INTERNAL, diff, 0);
4498 return TRUE;
4501 /***
4502 * DESCRIPTION:
4503 * Searches for an item with specific characteristics.
4505 * PARAMETER(S):
4506 * [I] hwnd : window handle
4507 * [I] nStart : base item index
4508 * [I] lpFindInfo : item information to look for
4510 * RETURN:
4511 * SUCCESS : index of item
4512 * FAILURE : -1
4514 static INT LISTVIEW_FindItemW(LISTVIEW_INFO *infoPtr, INT nStart,
4515 const LVFINDINFOW *lpFindInfo)
4517 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
4518 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
4519 BOOL bWrap = FALSE, bNearest = FALSE;
4520 INT nItem = nStart + 1, nLast = infoPtr->nItemCount, nNearestItem = -1;
4521 ULONG xdist, ydist, dist, mindist = 0x7fffffff;
4522 POINT Position, Destination;
4523 LVITEMW lvItem;
4525 if (!lpFindInfo || nItem < 0) return -1;
4527 lvItem.mask = 0;
4528 if (lpFindInfo->flags & (LVFI_STRING | LVFI_PARTIAL))
4530 lvItem.mask |= LVIF_TEXT;
4531 lvItem.pszText = szDispText;
4532 lvItem.cchTextMax = DISP_TEXT_SIZE;
4535 if (lpFindInfo->flags & LVFI_WRAP)
4536 bWrap = TRUE;
4538 if ((lpFindInfo->flags & LVFI_NEARESTXY) &&
4539 (uView == LVS_ICON || uView ==LVS_SMALLICON))
4541 POINT Origin;
4542 RECT rcArea;
4544 LISTVIEW_GetOrigin(infoPtr, &Origin);
4545 Destination.x = lpFindInfo->pt.x - Origin.x;
4546 Destination.y = lpFindInfo->pt.y - Origin.y;
4547 switch(lpFindInfo->vkDirection)
4549 case VK_DOWN: Destination.y += infoPtr->nItemHeight; break;
4550 case VK_UP: Destination.y -= infoPtr->nItemHeight; break;
4551 case VK_RIGHT: Destination.x += infoPtr->nItemWidth; break;
4552 case VK_LEFT: Destination.x -= infoPtr->nItemWidth; break;
4553 case VK_HOME: Destination.x = Destination.y = 0; break;
4554 case VK_NEXT: Destination.y += infoPtr->rcList.bottom - infoPtr->rcList.top; break;
4555 case VK_PRIOR: Destination.y -= infoPtr->rcList.bottom - infoPtr->rcList.top; break;
4556 case VK_END:
4557 LISTVIEW_GetAreaRect(infoPtr, &rcArea);
4558 Destination.x = rcArea.right;
4559 Destination.y = rcArea.bottom;
4560 break;
4561 default: ERR("Unknown vkDirection=%d\n", lpFindInfo->vkDirection);
4563 bNearest = TRUE;
4566 /* if LVFI_PARAM is specified, all other flags are ignored */
4567 if (lpFindInfo->flags & LVFI_PARAM)
4569 lvItem.mask |= LVIF_PARAM;
4570 bNearest = FALSE;
4571 lvItem.mask &= ~LVIF_TEXT;
4574 again:
4575 for (; nItem < nLast; nItem++)
4577 lvItem.iItem = nItem;
4578 lvItem.iSubItem = 0;
4579 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) continue;
4581 if (lvItem.mask & LVIF_PARAM)
4583 if (lpFindInfo->lParam == lvItem.lParam)
4584 return nItem;
4585 else
4586 continue;
4589 if (lvItem.mask & LVIF_TEXT)
4591 if (lpFindInfo->flags & LVFI_PARTIAL)
4593 if (strstrW(lvItem.pszText, lpFindInfo->psz) == NULL) continue;
4595 else
4597 if (lstrcmpW(lvItem.pszText, lpFindInfo->psz) != 0) continue;
4601 if (!bNearest) return nItem;
4603 /* This is very inefficient. To do a good job here,
4604 * we need a sorted array of (x,y) item positions */
4605 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position);
4607 /* compute the distance^2 to the destination */
4608 xdist = Destination.x - Position.x;
4609 ydist = Destination.y - Position.y;
4610 dist = xdist * xdist + ydist * ydist;
4612 /* remember the distance, and item if it's closer */
4613 if (dist < mindist)
4615 mindist = dist;
4616 nNearestItem = nItem;
4620 if (bWrap)
4622 nItem = 0;
4623 nLast = min(nStart + 1, infoPtr->nItemCount);
4624 bWrap = FALSE;
4625 goto again;
4628 return nNearestItem;
4631 /***
4632 * DESCRIPTION:
4633 * Searches for an item with specific characteristics.
4635 * PARAMETER(S):
4636 * [I] hwnd : window handle
4637 * [I] nStart : base item index
4638 * [I] lpFindInfo : item information to look for
4640 * RETURN:
4641 * SUCCESS : index of item
4642 * FAILURE : -1
4644 static INT LISTVIEW_FindItemA(LISTVIEW_INFO *infoPtr, INT nStart,
4645 const LVFINDINFOA *lpFindInfo)
4647 BOOL hasText = lpFindInfo->flags & (LVFI_STRING | LVFI_PARTIAL);
4648 LVFINDINFOW fiw;
4649 INT res;
4651 memcpy(&fiw, lpFindInfo, sizeof(fiw));
4652 if (hasText) fiw.psz = textdupTtoW((LPCWSTR)lpFindInfo->psz, FALSE);
4653 res = LISTVIEW_FindItemW(infoPtr, nStart, &fiw);
4654 if (hasText) textfreeT((LPWSTR)fiw.psz, FALSE);
4655 return res;
4658 /***
4659 * DESCRIPTION:
4660 * Retrieves the background image of the listview control.
4662 * PARAMETER(S):
4663 * [I] infoPtr : valid pointer to the listview structure
4664 * [O] lpBkImage : background image attributes
4666 * RETURN:
4667 * SUCCESS : TRUE
4668 * FAILURE : FALSE
4670 /* static BOOL LISTVIEW_GetBkImage(LISTVIEW_INFO *infoPtr, LPLVBKIMAGE lpBkImage) */
4671 /* { */
4672 /* FIXME (listview, "empty stub!\n"); */
4673 /* return FALSE; */
4674 /* } */
4676 /***
4677 * DESCRIPTION:
4678 * Retrieves column attributes.
4680 * PARAMETER(S):
4681 * [I] infoPtr : valid pointer to the listview structure
4682 * [I] nColumn : column index
4683 * [IO] lpColumn : column information
4684 * [I] isW : if TRUE, then lpColumn is a LPLVCOLUMNW
4685 * otherwise it is in fact a LPLVCOLUMNA
4687 * RETURN:
4688 * SUCCESS : TRUE
4689 * FAILURE : FALSE
4691 static BOOL LISTVIEW_GetColumnT(LISTVIEW_INFO *infoPtr, INT nColumn, LPLVCOLUMNW lpColumn, BOOL isW)
4693 COLUMN_INFO *lpColumnInfo;
4694 HDITEMW hdi;
4696 if (!lpColumn || nColumn < 0 || nColumn >= infoPtr->hdpaColumns->nItemCount) return FALSE;
4697 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, nColumn);
4699 /* initialize memory */
4700 ZeroMemory(&hdi, sizeof(hdi));
4702 if (lpColumn->mask & LVCF_TEXT)
4704 hdi.mask |= HDI_TEXT;
4705 hdi.pszText = lpColumn->pszText;
4706 hdi.cchTextMax = lpColumn->cchTextMax;
4709 if (lpColumn->mask & LVCF_IMAGE)
4710 hdi.mask |= HDI_IMAGE;
4712 if (lpColumn->mask & LVCF_ORDER)
4713 hdi.mask |= HDI_ORDER;
4715 if (!SendMessageW(infoPtr->hwndHeader, isW ? HDM_GETITEMW : HDM_GETITEMA, nColumn, (LPARAM)&hdi)) return FALSE;
4717 if (lpColumn->mask & LVCF_FMT)
4718 lpColumn->fmt = lpColumnInfo->fmt;
4720 if (lpColumn->mask & LVCF_WIDTH)
4721 lpColumn->cx = lpColumnInfo->rcHeader.right - lpColumnInfo->rcHeader.left;
4723 if (lpColumn->mask & LVCF_IMAGE)
4724 lpColumn->iImage = hdi.iImage;
4726 if (lpColumn->mask & LVCF_ORDER)
4727 lpColumn->iOrder = hdi.iOrder;
4729 return TRUE;
4733 static BOOL LISTVIEW_GetColumnOrderArray(LISTVIEW_INFO *infoPtr, INT iCount, LPINT lpiArray)
4735 INT i;
4737 if (!lpiArray)
4738 return FALSE;
4740 /* FIXME: little hack */
4741 for (i = 0; i < iCount; i++)
4742 lpiArray[i] = i;
4744 return TRUE;
4747 /***
4748 * DESCRIPTION:
4749 * Retrieves the column width.
4751 * PARAMETER(S):
4752 * [I] infoPtr : valid pointer to the listview structure
4753 * [I] int : column index
4755 * RETURN:
4756 * SUCCESS : column width
4757 * FAILURE : zero
4759 static INT LISTVIEW_GetColumnWidth(LISTVIEW_INFO *infoPtr, INT nColumn)
4761 INT nColumnWidth = 0;
4762 RECT rcHeader;
4764 TRACE("nColumn=%d\n", nColumn);
4766 /* we have a 'column' in LIST and REPORT mode only */
4767 switch(infoPtr->dwStyle & LVS_TYPEMASK)
4769 case LVS_LIST:
4770 nColumnWidth = infoPtr->nItemWidth;
4771 break;
4772 case LVS_REPORT:
4773 if (nColumn < 0 || nColumn >= infoPtr->hdpaColumns->nItemCount) return 0;
4774 LISTVIEW_GetHeaderRect(infoPtr, nColumn, &rcHeader);
4775 nColumnWidth = rcHeader.right - rcHeader.left;
4776 break;
4779 TRACE("nColumnWidth=%d\n", nColumnWidth);
4780 return nColumnWidth;
4783 /***
4784 * DESCRIPTION:
4785 * In list or report display mode, retrieves the number of items that can fit
4786 * vertically in the visible area. In icon or small icon display mode,
4787 * retrieves the total number of visible items.
4789 * PARAMETER(S):
4790 * [I] infoPtr : valid pointer to the listview structure
4792 * RETURN:
4793 * Number of fully visible items.
4795 static INT LISTVIEW_GetCountPerPage(LISTVIEW_INFO *infoPtr)
4797 switch (infoPtr->dwStyle & LVS_TYPEMASK)
4799 case LVS_ICON:
4800 case LVS_SMALLICON:
4801 return infoPtr->nItemCount;
4802 case LVS_REPORT:
4803 return LISTVIEW_GetCountPerColumn(infoPtr);
4804 case LVS_LIST:
4805 return LISTVIEW_GetCountPerRow(infoPtr) * LISTVIEW_GetCountPerColumn(infoPtr);
4807 assert(FALSE);
4808 return 0;
4811 /***
4812 * DESCRIPTION:
4813 * Retrieves an image list handle.
4815 * PARAMETER(S):
4816 * [I] infoPtr : valid pointer to the listview structure
4817 * [I] nImageList : image list identifier
4819 * RETURN:
4820 * SUCCESS : image list handle
4821 * FAILURE : NULL
4823 static HIMAGELIST LISTVIEW_GetImageList(LISTVIEW_INFO *infoPtr, INT nImageList)
4825 switch (nImageList)
4827 case LVSIL_NORMAL: return infoPtr->himlNormal;
4828 case LVSIL_SMALL: return infoPtr->himlSmall;
4829 case LVSIL_STATE: return infoPtr->himlState;
4831 return NULL;
4834 /* LISTVIEW_GetISearchString */
4836 /***
4837 * DESCRIPTION:
4838 * Retrieves item attributes.
4840 * PARAMETER(S):
4841 * [I] hwnd : window handle
4842 * [IO] lpLVItem : item info
4843 * [I] isW : if TRUE, then lpLVItem is a LPLVITEMW,
4844 * if FALSE, the lpLVItem is a LPLVITEMA.
4846 * NOTE:
4847 * This is the internal 'GetItem' interface -- it tries to
4848 * be smart, and avoids text copies, if possible, by modifing
4849 * lpLVItem->pszText to point to the text string. Please note
4850 * that this is not always possible (e.g. OWNERDATA), so on
4851 * entry you *must* supply valid values for pszText, and cchTextMax.
4852 * The only difference to the documented interface is that upon
4853 * return, you should use *only* the lpLVItem->pszText, rather than
4854 * the buffer pointer you provided on input. Most code already does
4855 * that, so it's not a problem.
4856 * For the two cases when the text must be copied (that is,
4857 * for LVM_GETITEM, and LVMGETITEMTEXT), use LISTVIEW_GetItemExtT.
4859 * RETURN:
4860 * SUCCESS : TRUE
4861 * FAILURE : FALSE
4863 static BOOL LISTVIEW_GetItemT(LISTVIEW_INFO *infoPtr, LPLVITEMW lpLVItem, BOOL isW)
4865 ITEMHDR callbackHdr = { LPSTR_TEXTCALLBACKW, I_IMAGECALLBACK };
4866 NMLVDISPINFOW dispInfo;
4867 ITEM_INFO *lpItem;
4868 ITEMHDR* pItemHdr;
4869 HDPA hdpaSubItems;
4871 TRACE("(lpLVItem=%s, isW=%d)\n", debuglvitem_t(lpLVItem, isW), isW);
4873 if (!lpLVItem || lpLVItem->iItem < 0 || lpLVItem->iItem >= infoPtr->nItemCount)
4874 return FALSE;
4876 if (lpLVItem->mask == 0) return TRUE;
4878 /* a quick optimization if all we're asked is the focus state
4879 * these queries are worth optimising since they are common,
4880 * and can be answered in constant time, without the heavy accesses */
4881 if ( (lpLVItem->mask == LVIF_STATE) && (lpLVItem->stateMask == LVIS_FOCUSED) &&
4882 !(infoPtr->uCallbackMask & LVIS_FOCUSED) )
4884 lpLVItem->state = 0;
4885 if (infoPtr->nFocusedItem == lpLVItem->iItem)
4886 lpLVItem->state |= LVIS_FOCUSED;
4887 return TRUE;
4890 ZeroMemory(&dispInfo, sizeof(dispInfo));
4892 /* if the app stores all the data, handle it separately */
4893 if (infoPtr->dwStyle & LVS_OWNERDATA)
4895 dispInfo.item.state = 0;
4897 /* apprently, we should not callback for lParam in LVS_OWNERDATA */
4898 if ((lpLVItem->mask & ~(LVIF_STATE | LVIF_PARAM)) || infoPtr->uCallbackMask)
4900 /* NOTE: copy only fields which we _know_ are initialized, some apps
4901 * depend on the uninitialized fields being 0 */
4902 dispInfo.item.mask = lpLVItem->mask & ~LVIF_PARAM;
4903 dispInfo.item.iItem = lpLVItem->iItem;
4904 dispInfo.item.iSubItem = lpLVItem->iSubItem;
4905 if (lpLVItem->mask & LVIF_TEXT)
4907 dispInfo.item.pszText = lpLVItem->pszText;
4908 dispInfo.item.cchTextMax = lpLVItem->cchTextMax;
4910 if (lpLVItem->mask & LVIF_STATE)
4911 dispInfo.item.stateMask = lpLVItem->stateMask & infoPtr->uCallbackMask;
4912 notify_dispinfoT(infoPtr, LVN_GETDISPINFOW, &dispInfo, isW);
4913 dispInfo.item.stateMask = lpLVItem->stateMask;
4914 *lpLVItem = dispInfo.item;
4915 TRACE(" getdispinfo(1):lpLVItem=%s\n", debuglvitem_t(lpLVItem, isW));
4918 /* make sure lParam is zeroed out */
4919 if (lpLVItem->mask & LVIF_PARAM) lpLVItem->lParam = 0;
4921 /* we store only a little state, so if we're not asked, we're done */
4922 if (!(lpLVItem->mask & LVIF_STATE) || lpLVItem->iSubItem) return TRUE;
4924 /* if focus is handled by us, report it */
4925 if ( lpLVItem->stateMask & ~infoPtr->uCallbackMask & LVIS_FOCUSED )
4927 lpLVItem->state &= ~LVIS_FOCUSED;
4928 if (infoPtr->nFocusedItem == lpLVItem->iItem)
4929 lpLVItem->state |= LVIS_FOCUSED;
4932 /* and do the same for selection, if we handle it */
4933 if ( lpLVItem->stateMask & ~infoPtr->uCallbackMask & LVIS_SELECTED )
4935 lpLVItem->state &= ~LVIS_SELECTED;
4936 if (ranges_contain(infoPtr->selectionRanges, lpLVItem->iItem))
4937 lpLVItem->state |= LVIS_SELECTED;
4940 return TRUE;
4943 /* find the item and subitem structures before we proceed */
4944 hdpaSubItems = (HDPA)DPA_GetPtr(infoPtr->hdpaItems, lpLVItem->iItem);
4945 lpItem = (ITEM_INFO *)DPA_GetPtr(hdpaSubItems, 0);
4946 assert (lpItem);
4948 if (lpLVItem->iSubItem)
4950 SUBITEM_INFO *lpSubItem = LISTVIEW_GetSubItemPtr(hdpaSubItems, lpLVItem->iSubItem);
4951 pItemHdr = lpSubItem ? &lpSubItem->hdr : &callbackHdr;
4953 else
4954 pItemHdr = &lpItem->hdr;
4956 /* Do we need to query the state from the app? */
4957 if ((lpLVItem->mask & LVIF_STATE) && infoPtr->uCallbackMask && lpLVItem->iSubItem == 0)
4959 dispInfo.item.mask |= LVIF_STATE;
4960 dispInfo.item.stateMask = infoPtr->uCallbackMask;
4963 /* Do we need to enquire about the image? */
4964 if ((lpLVItem->mask & LVIF_IMAGE) && pItemHdr->iImage == I_IMAGECALLBACK)
4965 dispInfo.item.mask |= LVIF_IMAGE;
4967 /* Apps depend on calling back for text if it is NULL or LPSTR_TEXTCALLBACKW */
4968 if ((lpLVItem->mask & LVIF_TEXT) && !is_textW(pItemHdr->pszText))
4970 dispInfo.item.mask |= LVIF_TEXT;
4971 dispInfo.item.pszText = lpLVItem->pszText;
4972 dispInfo.item.cchTextMax = lpLVItem->cchTextMax;
4973 if (dispInfo.item.pszText && dispInfo.item.cchTextMax > 0)
4974 *dispInfo.item.pszText = '\0';
4977 /* If we don't have all the requested info, query the application */
4978 if (dispInfo.item.mask != 0)
4980 dispInfo.item.iItem = lpLVItem->iItem;
4981 dispInfo.item.iSubItem = lpLVItem->iSubItem;
4982 dispInfo.item.lParam = lpItem->lParam;
4983 notify_dispinfoT(infoPtr, LVN_GETDISPINFOW, &dispInfo, isW);
4984 TRACE(" getdispinfo(2):item=%s\n", debuglvitem_t(&dispInfo.item, isW));
4987 /* we should not store values for subitems */
4988 if (lpLVItem->iSubItem) dispInfo.item.mask &= ~LVIF_DI_SETITEM;
4990 /* Now, handle the iImage field */
4991 if (dispInfo.item.mask & LVIF_IMAGE)
4993 lpLVItem->iImage = dispInfo.item.iImage;
4994 if ((dispInfo.item.mask & LVIF_DI_SETITEM) && pItemHdr->iImage == I_IMAGECALLBACK)
4995 pItemHdr->iImage = dispInfo.item.iImage;
4997 else if (lpLVItem->mask & LVIF_IMAGE)
4998 lpLVItem->iImage = pItemHdr->iImage;
5000 /* The pszText field */
5001 if (dispInfo.item.mask & LVIF_TEXT)
5003 if ((dispInfo.item.mask & LVIF_DI_SETITEM) && pItemHdr->pszText)
5004 textsetptrT(&pItemHdr->pszText, dispInfo.item.pszText, isW);
5006 lpLVItem->pszText = dispInfo.item.pszText;
5008 else if (lpLVItem->mask & LVIF_TEXT)
5010 if (isW) lpLVItem->pszText = pItemHdr->pszText;
5011 else textcpynT(lpLVItem->pszText, isW, pItemHdr->pszText, TRUE, lpLVItem->cchTextMax);
5014 /* if this is a subitem, we're done */
5015 if (lpLVItem->iSubItem) return TRUE;
5017 /* Next is the lParam field */
5018 if (dispInfo.item.mask & LVIF_PARAM)
5020 lpLVItem->lParam = dispInfo.item.lParam;
5021 if ((dispInfo.item.mask & LVIF_DI_SETITEM))
5022 lpItem->lParam = dispInfo.item.lParam;
5024 else if (lpLVItem->mask & LVIF_PARAM)
5025 lpLVItem->lParam = lpItem->lParam;
5027 /* ... the state field (this one is different due to uCallbackmask) */
5028 if (lpLVItem->mask & LVIF_STATE)
5030 lpLVItem->state = lpItem->state;
5031 if (dispInfo.item.mask & LVIF_STATE)
5033 lpLVItem->state &= ~dispInfo.item.stateMask;
5034 lpLVItem->state |= (dispInfo.item.state & dispInfo.item.stateMask);
5036 if ( lpLVItem->stateMask & ~infoPtr->uCallbackMask & LVIS_FOCUSED )
5038 lpLVItem->state &= ~LVIS_FOCUSED;
5039 if (infoPtr->nFocusedItem == lpLVItem->iItem)
5040 lpLVItem->state |= LVIS_FOCUSED;
5042 if ( lpLVItem->stateMask & ~infoPtr->uCallbackMask & LVIS_SELECTED )
5044 lpLVItem->state &= ~LVIS_SELECTED;
5045 if (ranges_contain(infoPtr->selectionRanges, lpLVItem->iItem))
5046 lpLVItem->state |= LVIS_SELECTED;
5050 /* and last, but not least, the indent field */
5051 if (lpLVItem->mask & LVIF_INDENT)
5052 lpLVItem->iIndent = lpItem->iIndent;
5054 return TRUE;
5057 /***
5058 * DESCRIPTION:
5059 * Retrieves item attributes.
5061 * PARAMETER(S):
5062 * [I] hwnd : window handle
5063 * [IO] lpLVItem : item info
5064 * [I] isW : if TRUE, then lpLVItem is a LPLVITEMW,
5065 * if FALSE, the lpLVItem is a LPLVITEMA.
5067 * NOTE:
5068 * This is the external 'GetItem' interface -- it properly copies
5069 * the text in the provided buffer.
5071 * RETURN:
5072 * SUCCESS : TRUE
5073 * FAILURE : FALSE
5075 static BOOL LISTVIEW_GetItemExtT(LISTVIEW_INFO *infoPtr, LPLVITEMW lpLVItem, BOOL isW)
5077 LPWSTR pszText;
5078 BOOL bResult;
5080 if (!lpLVItem || lpLVItem->iItem < 0 || lpLVItem->iItem >= infoPtr->nItemCount)
5081 return FALSE;
5083 pszText = lpLVItem->pszText;
5084 bResult = LISTVIEW_GetItemT(infoPtr, lpLVItem, isW);
5085 if (bResult && lpLVItem->pszText != pszText)
5086 textcpynT(pszText, isW, lpLVItem->pszText, isW, lpLVItem->cchTextMax);
5087 lpLVItem->pszText = pszText;
5089 return bResult;
5093 /***
5094 * DESCRIPTION:
5095 * Retrieves the position (upper-left) of the listview control item.
5096 * Note that for LVS_ICON style, the upper-left is that of the icon
5097 * and not the bounding box.
5099 * PARAMETER(S):
5100 * [I] infoPtr : valid pointer to the listview structure
5101 * [I] nItem : item index
5102 * [O] lpptPosition : coordinate information
5104 * RETURN:
5105 * SUCCESS : TRUE
5106 * FAILURE : FALSE
5108 static BOOL LISTVIEW_GetItemPosition(LISTVIEW_INFO *infoPtr, INT nItem, LPPOINT lpptPosition)
5110 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
5111 POINT Origin;
5113 TRACE("(nItem=%d, lpptPosition=%p)\n", nItem, lpptPosition);
5115 if (!lpptPosition || nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
5117 LISTVIEW_GetOrigin(infoPtr, &Origin);
5118 LISTVIEW_GetItemOrigin(infoPtr, nItem, lpptPosition);
5120 if (uView == LVS_ICON)
5122 lpptPosition->x += (infoPtr->nItemWidth - infoPtr->iconSize.cx) / 2;
5123 lpptPosition->y += ICON_TOP_PADDING;
5125 lpptPosition->x += Origin.x;
5126 lpptPosition->y += Origin.y;
5128 TRACE (" lpptPosition=%s\n", debugpoint(lpptPosition));
5129 return TRUE;
5133 /***
5134 * DESCRIPTION:
5135 * Retrieves the bounding rectangle for a listview control item.
5137 * PARAMETER(S):
5138 * [I] infoPtr : valid pointer to the listview structure
5139 * [I] nItem : item index
5140 * [IO] lprc : bounding rectangle coordinates
5141 * lprc->left specifies the portion of the item for which the bounding
5142 * rectangle will be retrieved.
5144 * LVIR_BOUNDS Returns the bounding rectangle of the entire item,
5145 * including the icon and label.
5147 * * For LVS_ICON
5148 * * Experiment shows that native control returns:
5149 * * width = min (48, length of text line)
5150 * * .left = position.x - (width - iconsize.cx)/2
5151 * * .right = .left + width
5152 * * height = #lines of text * ntmHeight + icon height + 8
5153 * * .top = position.y - 2
5154 * * .bottom = .top + height
5155 * * separation between items .y = itemSpacing.cy - height
5156 * * .x = itemSpacing.cx - width
5157 * LVIR_ICON Returns the bounding rectangle of the icon or small icon.
5159 * * For LVS_ICON
5160 * * Experiment shows that native control returns:
5161 * * width = iconSize.cx + 16
5162 * * .left = position.x - (width - iconsize.cx)/2
5163 * * .right = .left + width
5164 * * height = iconSize.cy + 4
5165 * * .top = position.y - 2
5166 * * .bottom = .top + height
5167 * * separation between items .y = itemSpacing.cy - height
5168 * * .x = itemSpacing.cx - width
5169 * LVIR_LABEL Returns the bounding rectangle of the item text.
5171 * * For LVS_ICON
5172 * * Experiment shows that native control returns:
5173 * * width = text length
5174 * * .left = position.x - width/2
5175 * * .right = .left + width
5176 * * height = ntmH * linecount + 2
5177 * * .top = position.y + iconSize.cy + 6
5178 * * .bottom = .top + height
5179 * * separation between items .y = itemSpacing.cy - height
5180 * * .x = itemSpacing.cx - width
5181 * LVIR_SELECTBOUNDS Returns the union of the LVIR_ICON and LVIR_LABEL
5182 * rectangles, but excludes columns in report view.
5184 * RETURN:
5185 * SUCCESS : TRUE
5186 * FAILURE : FALSE
5188 * NOTES
5189 * Note that the bounding rectangle of the label in the LVS_ICON view depends
5190 * upon whether the window has the focus currently and on whether the item
5191 * is the one with the focus. Ensure that the control's record of which
5192 * item has the focus agrees with the items' records.
5194 static BOOL LISTVIEW_GetItemRect(LISTVIEW_INFO *infoPtr, INT nItem, LPRECT lprc)
5196 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
5197 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
5198 BOOL doLabel = TRUE, oversizedBox = FALSE;
5199 POINT Position, Origin;
5200 LVITEMW lvItem;
5201 RECT label_rect;
5203 TRACE("(hwnd=%p, nItem=%d, lprc=%p)\n", infoPtr->hwndSelf, nItem, lprc);
5205 if (!lprc || nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
5207 LISTVIEW_GetOrigin(infoPtr, &Origin);
5208 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position);
5210 /* Be smart and try to figure out the minimum we have to do */
5211 if (lprc->left == LVIR_ICON) doLabel = FALSE;
5212 if (uView == LVS_REPORT && lprc->left == LVIR_BOUNDS) doLabel = FALSE;
5213 if (uView == LVS_ICON && lprc->left != LVIR_ICON &&
5214 infoPtr->bFocus && LISTVIEW_GetItemState(infoPtr, nItem, LVIS_FOCUSED))
5215 oversizedBox = TRUE;
5217 /* get what we need from the item before hand, so we make
5218 * only one request. This can speed up things, if data
5219 * is stored on the app side */
5220 lvItem.mask = 0;
5221 if (uView == LVS_REPORT) lvItem.mask |= LVIF_INDENT;
5222 if (doLabel) lvItem.mask |= LVIF_TEXT;
5223 lvItem.iItem = nItem;
5224 lvItem.iSubItem = 0;
5225 lvItem.pszText = szDispText;
5226 lvItem.cchTextMax = DISP_TEXT_SIZE;
5227 if (lvItem.mask && !LISTVIEW_GetItemW(infoPtr, &lvItem)) return FALSE;
5228 /* we got the state already up, simulate it here, to avoid a reget */
5229 if (uView == LVS_ICON && (lprc->left != LVIR_ICON))
5231 lvItem.mask |= LVIF_STATE;
5232 lvItem.stateMask = LVIS_FOCUSED;
5233 lvItem.state = (oversizedBox ? LVIS_FOCUSED : 0);
5236 if (uView == LVS_REPORT && (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT) && lprc->left == LVIR_SELECTBOUNDS)
5237 lprc->left = LVIR_BOUNDS;
5238 switch(lprc->left)
5240 case LVIR_ICON:
5241 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, NULL, NULL, lprc, NULL);
5242 break;
5244 case LVIR_LABEL:
5245 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, NULL, NULL, NULL, lprc);
5246 break;
5248 case LVIR_BOUNDS:
5249 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, lprc, NULL, NULL, NULL);
5250 break;
5252 case LVIR_SELECTBOUNDS:
5253 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, NULL, NULL, lprc, &label_rect);
5254 UnionRect(lprc, lprc, &label_rect);
5255 break;
5257 default:
5258 WARN("Unknown value: %ld\n", lprc->left);
5259 return FALSE;
5262 OffsetRect(lprc, Position.x + Origin.x, Position.y + Origin.y);
5264 TRACE(" rect=%s\n", debugrect(lprc));
5266 return TRUE;
5269 /***
5270 * DESCRIPTION:
5271 * Retrieves the spacing between listview control items.
5273 * PARAMETER(S):
5274 * [I] infoPtr : valid pointer to the listview structure
5275 * [IO] lprc : rectangle to receive the output
5276 * on input, lprc->top = nSubItem
5277 * lprc->left = LVIR_ICON | LVIR_BOUNDS | LVIR_LABEL
5279 * NOTE: for subItem = 0, we should return the bounds of the _entire_ item,
5280 * not only those of the first column.
5281 * Fortunately, LISTVIEW_GetItemMetrics does the right thing.
5283 * RETURN:
5284 * TRUE: success
5285 * FALSE: failure
5287 static BOOL LISTVIEW_GetSubItemRect(LISTVIEW_INFO *infoPtr, INT nItem, LPRECT lprc)
5289 POINT Position;
5290 LVITEMW lvItem;
5292 if (!lprc || (infoPtr->dwStyle & LVS_TYPEMASK) != LVS_REPORT) return FALSE;
5294 TRACE("(nItem=%d, nSubItem=%ld)\n", nItem, lprc->top);
5295 /* On WinNT, a subitem of '0' calls LISTVIEW_GetItemRect */
5296 if (lprc->top == 0)
5297 return LISTVIEW_GetItemRect(infoPtr, nItem, lprc);
5299 if (!LISTVIEW_GetItemPosition(infoPtr, nItem, &Position)) return FALSE;
5301 lvItem.mask = lprc->top == 0 ? LVIF_INDENT : 0;
5302 lvItem.iItem = nItem;
5303 lvItem.iSubItem = lprc->top;
5305 if (lvItem.mask && !LISTVIEW_GetItemW(infoPtr, &lvItem)) return FALSE;
5306 switch(lprc->left)
5308 case LVIR_ICON:
5309 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, NULL, NULL, lprc, NULL);
5310 break;
5312 case LVIR_LABEL:
5313 case LVIR_BOUNDS:
5314 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, lprc, NULL, NULL, NULL);
5315 break;
5317 default:
5318 ERR("Unknown bounds=%ld\n", lprc->left);
5319 return FALSE;
5322 OffsetRect(lprc, Position.x, Position.y);
5323 return TRUE;
5327 /***
5328 * DESCRIPTION:
5329 * Retrieves the width of a label.
5331 * PARAMETER(S):
5332 * [I] infoPtr : valid pointer to the listview structure
5334 * RETURN:
5335 * SUCCESS : string width (in pixels)
5336 * FAILURE : zero
5338 static INT LISTVIEW_GetLabelWidth(LISTVIEW_INFO *infoPtr, INT nItem)
5340 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
5341 LVITEMW lvItem;
5343 TRACE("(nItem=%d)\n", nItem);
5345 lvItem.mask = LVIF_TEXT;
5346 lvItem.iItem = nItem;
5347 lvItem.iSubItem = 0;
5348 lvItem.pszText = szDispText;
5349 lvItem.cchTextMax = DISP_TEXT_SIZE;
5350 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) return 0;
5352 return LISTVIEW_GetStringWidthT(infoPtr, lvItem.pszText, TRUE);
5355 /***
5356 * DESCRIPTION:
5357 * Retrieves the spacing between listview control items.
5359 * PARAMETER(S):
5360 * [I] infoPtr : valid pointer to the listview structure
5361 * [I] bSmall : flag for small or large icon
5363 * RETURN:
5364 * Horizontal + vertical spacing
5366 static LONG LISTVIEW_GetItemSpacing(LISTVIEW_INFO *infoPtr, BOOL bSmall)
5368 LONG lResult;
5370 if (!bSmall)
5372 lResult = MAKELONG(infoPtr->iconSpacing.cx, infoPtr->iconSpacing.cy);
5374 else
5376 if ((infoPtr->dwStyle & LVS_TYPEMASK) == LVS_ICON)
5377 lResult = MAKELONG(DEFAULT_COLUMN_WIDTH, GetSystemMetrics(SM_CXSMICON)+HEIGHT_PADDING);
5378 else
5379 lResult = MAKELONG(infoPtr->nItemWidth, infoPtr->nItemHeight);
5381 return lResult;
5384 /***
5385 * DESCRIPTION:
5386 * Retrieves the state of a listview control item.
5388 * PARAMETER(S):
5389 * [I] infoPtr : valid pointer to the listview structure
5390 * [I] nItem : item index
5391 * [I] uMask : state mask
5393 * RETURN:
5394 * State specified by the mask.
5396 static UINT LISTVIEW_GetItemState(LISTVIEW_INFO *infoPtr, INT nItem, UINT uMask)
5398 LVITEMW lvItem;
5400 if (nItem < 0 || nItem >= infoPtr->nItemCount) return 0;
5402 lvItem.iItem = nItem;
5403 lvItem.iSubItem = 0;
5404 lvItem.mask = LVIF_STATE;
5405 lvItem.stateMask = uMask;
5406 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) return 0;
5408 return lvItem.state & uMask;
5411 /***
5412 * DESCRIPTION:
5413 * Retrieves the text of a listview control item or subitem.
5415 * PARAMETER(S):
5416 * [I] hwnd : window handle
5417 * [I] nItem : item index
5418 * [IO] lpLVItem : item information
5419 * [I] isW : TRUE if lpLVItem is Unicode
5421 * RETURN:
5422 * SUCCESS : string length
5423 * FAILURE : 0
5425 static INT LISTVIEW_GetItemTextT(LISTVIEW_INFO *infoPtr, INT nItem, LPLVITEMW lpLVItem, BOOL isW)
5427 if (!lpLVItem || nItem < 0 || nItem >= infoPtr->nItemCount) return 0;
5429 lpLVItem->mask = LVIF_TEXT;
5430 lpLVItem->iItem = nItem;
5431 if (!LISTVIEW_GetItemExtT(infoPtr, lpLVItem, isW)) return 0;
5433 return textlenT(lpLVItem->pszText, isW);
5436 /***
5437 * DESCRIPTION:
5438 * Searches for an item based on properties + relationships.
5440 * PARAMETER(S):
5441 * [I] infoPtr : valid pointer to the listview structure
5442 * [I] nItem : item index
5443 * [I] uFlags : relationship flag
5445 * RETURN:
5446 * SUCCESS : item index
5447 * FAILURE : -1
5449 static INT LISTVIEW_GetNextItem(LISTVIEW_INFO *infoPtr, INT nItem, UINT uFlags)
5451 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
5452 UINT uMask = 0;
5453 LVFINDINFOW lvFindInfo;
5454 INT nCountPerColumn;
5455 INT nCountPerRow;
5456 INT i;
5458 TRACE("nItem=%d, uFlags=%x, nItemCount=%d\n", nItem, uFlags, infoPtr->nItemCount);
5459 if (nItem < -1 || nItem >= infoPtr->nItemCount) return -1;
5461 ZeroMemory(&lvFindInfo, sizeof(lvFindInfo));
5463 if (uFlags & LVNI_CUT)
5464 uMask |= LVIS_CUT;
5466 if (uFlags & LVNI_DROPHILITED)
5467 uMask |= LVIS_DROPHILITED;
5469 if (uFlags & LVNI_FOCUSED)
5470 uMask |= LVIS_FOCUSED;
5472 if (uFlags & LVNI_SELECTED)
5473 uMask |= LVIS_SELECTED;
5475 /* if we're asked for the focused item, that's only one,
5476 * so it's worth optimizing */
5477 if (uFlags & LVNI_FOCUSED)
5479 if (!(LISTVIEW_GetItemState(infoPtr, infoPtr->nFocusedItem, uMask) & uMask) == uMask) return -1;
5480 return (infoPtr->nFocusedItem == nItem) ? -1 : infoPtr->nFocusedItem;
5483 if (uFlags & LVNI_ABOVE)
5485 if ((uView == LVS_LIST) || (uView == LVS_REPORT))
5487 while (nItem >= 0)
5489 nItem--;
5490 if ((ListView_GetItemState(infoPtr->hwndSelf, nItem, uMask) & uMask) == uMask)
5491 return nItem;
5494 else
5496 /* Special case for autoarrange - move 'til the top of a list */
5497 if (is_autoarrange(infoPtr))
5499 nCountPerRow = LISTVIEW_GetCountPerRow(infoPtr);
5500 while (nItem - nCountPerRow >= 0)
5502 nItem -= nCountPerRow;
5503 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
5504 return nItem;
5506 return -1;
5508 lvFindInfo.flags = LVFI_NEARESTXY;
5509 lvFindInfo.vkDirection = VK_UP;
5510 ListView_GetItemPosition(infoPtr->hwndSelf, nItem, &lvFindInfo.pt);
5511 while ((nItem = ListView_FindItemW(infoPtr->hwndSelf, nItem, &lvFindInfo)) != -1)
5513 if ((ListView_GetItemState(infoPtr->hwndSelf, nItem, uMask) & uMask) == uMask)
5514 return nItem;
5518 else if (uFlags & LVNI_BELOW)
5520 if ((uView == LVS_LIST) || (uView == LVS_REPORT))
5522 while (nItem < infoPtr->nItemCount)
5524 nItem++;
5525 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
5526 return nItem;
5529 else
5531 /* Special case for autoarrange - move 'til the bottom of a list */
5532 if (is_autoarrange(infoPtr))
5534 nCountPerRow = LISTVIEW_GetCountPerRow(infoPtr);
5535 while (nItem + nCountPerRow < infoPtr->nItemCount )
5537 nItem += nCountPerRow;
5538 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
5539 return nItem;
5541 return -1;
5543 lvFindInfo.flags = LVFI_NEARESTXY;
5544 lvFindInfo.vkDirection = VK_DOWN;
5545 ListView_GetItemPosition(infoPtr->hwndSelf, nItem, &lvFindInfo.pt);
5546 while ((nItem = ListView_FindItemW(infoPtr->hwndSelf, nItem, &lvFindInfo)) != -1)
5548 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
5549 return nItem;
5553 else if (uFlags & LVNI_TOLEFT)
5555 if (uView == LVS_LIST)
5557 nCountPerColumn = LISTVIEW_GetCountPerColumn(infoPtr);
5558 while (nItem - nCountPerColumn >= 0)
5560 nItem -= nCountPerColumn;
5561 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
5562 return nItem;
5565 else if ((uView == LVS_SMALLICON) || (uView == LVS_ICON))
5567 /* Special case for autoarrange - move 'ti the beginning of a row */
5568 if (is_autoarrange(infoPtr))
5570 nCountPerRow = LISTVIEW_GetCountPerRow(infoPtr);
5571 while (nItem % nCountPerRow > 0)
5573 nItem --;
5574 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
5575 return nItem;
5577 return -1;
5579 lvFindInfo.flags = LVFI_NEARESTXY;
5580 lvFindInfo.vkDirection = VK_LEFT;
5581 ListView_GetItemPosition(infoPtr->hwndSelf, nItem, &lvFindInfo.pt);
5582 while ((nItem = ListView_FindItemW(infoPtr->hwndSelf, nItem, &lvFindInfo)) != -1)
5584 if ((ListView_GetItemState(infoPtr->hwndSelf, nItem, uMask) & uMask) == uMask)
5585 return nItem;
5589 else if (uFlags & LVNI_TORIGHT)
5591 if (uView == LVS_LIST)
5593 nCountPerColumn = LISTVIEW_GetCountPerColumn(infoPtr);
5594 while (nItem + nCountPerColumn < infoPtr->nItemCount)
5596 nItem += nCountPerColumn;
5597 if ((ListView_GetItemState(infoPtr->hwndSelf, nItem, uMask) & uMask) == uMask)
5598 return nItem;
5601 else if ((uView == LVS_SMALLICON) || (uView == LVS_ICON))
5603 /* Special case for autoarrange - move 'til the end of a row */
5604 if (is_autoarrange(infoPtr))
5606 nCountPerRow = LISTVIEW_GetCountPerRow(infoPtr);
5607 while (nItem % nCountPerRow < nCountPerRow - 1 )
5609 nItem ++;
5610 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
5611 return nItem;
5613 return -1;
5615 lvFindInfo.flags = LVFI_NEARESTXY;
5616 lvFindInfo.vkDirection = VK_RIGHT;
5617 ListView_GetItemPosition(infoPtr->hwndSelf, nItem, &lvFindInfo.pt);
5618 while ((nItem = ListView_FindItemW(infoPtr->hwndSelf, nItem, &lvFindInfo)) != -1)
5620 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
5621 return nItem;
5625 else
5627 nItem++;
5629 /* search by index */
5630 for (i = nItem; i < infoPtr->nItemCount; i++)
5632 if ((LISTVIEW_GetItemState(infoPtr, i, uMask) & uMask) == uMask)
5633 return i;
5637 return -1;
5640 /* LISTVIEW_GetNumberOfWorkAreas */
5642 /***
5643 * DESCRIPTION:
5644 * Retrieves the origin coordinates when in icon or small icon display mode.
5646 * PARAMETER(S):
5647 * [I] infoPtr : valid pointer to the listview structure
5648 * [O] lpptOrigin : coordinate information
5650 * RETURN:
5651 * None.
5653 static void LISTVIEW_GetOrigin(LISTVIEW_INFO *infoPtr, LPPOINT lpptOrigin)
5655 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
5656 INT nHorzPos = 0, nVertPos = 0;
5657 SCROLLINFO scrollInfo;
5659 scrollInfo.cbSize = sizeof(SCROLLINFO);
5660 scrollInfo.fMask = SIF_POS;
5662 if ((infoPtr->dwStyle & WS_HSCROLL) && GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo))
5663 nHorzPos = scrollInfo.nPos;
5664 if ((infoPtr->dwStyle & WS_VSCROLL) && GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo))
5665 nVertPos = scrollInfo.nPos;
5667 TRACE("nHorzPos=%d, nVertPos=%d\n", nHorzPos, nVertPos);
5669 lpptOrigin->x = infoPtr->rcList.left;
5670 lpptOrigin->y = infoPtr->rcList.top;
5671 if (uView == LVS_LIST)
5672 nHorzPos *= infoPtr->nItemWidth;
5673 else if (uView == LVS_REPORT)
5674 nVertPos *= infoPtr->nItemHeight;
5676 lpptOrigin->x -= nHorzPos;
5677 lpptOrigin->y -= nVertPos;
5679 TRACE(" origin=%s\n", debugpoint(lpptOrigin));
5682 /***
5683 * DESCRIPTION:
5684 * Retrieves the width of a string.
5686 * PARAMETER(S):
5687 * [I] hwnd : window handle
5688 * [I] lpszText : text string to process
5689 * [I] isW : TRUE if lpszText is Unicode, FALSE otherwise
5691 * RETURN:
5692 * SUCCESS : string width (in pixels)
5693 * FAILURE : zero
5695 static INT LISTVIEW_GetStringWidthT(LISTVIEW_INFO *infoPtr, LPCWSTR lpszText, BOOL isW)
5697 SIZE stringSize;
5699 stringSize.cx = 0;
5700 if (is_textT(lpszText, isW))
5702 HFONT hFont = infoPtr->hFont ? infoPtr->hFont : infoPtr->hDefaultFont;
5703 HDC hdc = GetDC(infoPtr->hwndSelf);
5704 HFONT hOldFont = SelectObject(hdc, hFont);
5706 if (isW)
5707 GetTextExtentPointW(hdc, lpszText, lstrlenW(lpszText), &stringSize);
5708 else
5709 GetTextExtentPointA(hdc, (LPCSTR)lpszText, lstrlenA((LPCSTR)lpszText), &stringSize);
5710 SelectObject(hdc, hOldFont);
5711 ReleaseDC(infoPtr->hwndSelf, hdc);
5713 return stringSize.cx;
5716 /***
5717 * DESCRIPTION:
5718 * Determines which listview item is located at the specified position.
5720 * PARAMETER(S):
5721 * [I] infoPtr : valid pointer to the listview structure
5722 * [IO] lpht : hit test information
5723 * [I] subitem : fill out iSubItem.
5724 * [I] select : return the index only if the hit selects the item
5726 * NOTE:
5727 * (mm 20001022): We must not allow iSubItem to be touched, for
5728 * an app might pass only a structure with space up to iItem!
5729 * (MS Office 97 does that for instance in the file open dialog)
5731 * RETURN:
5732 * SUCCESS : item index
5733 * FAILURE : -1
5735 static INT LISTVIEW_HitTest(LISTVIEW_INFO *infoPtr, LPLVHITTESTINFO lpht, BOOL subitem, BOOL select)
5737 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
5738 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
5739 RECT rcBox, rcBounds, rcState, rcIcon, rcLabel, rcSearch;
5740 POINT Origin, Position, opt;
5741 LVITEMW lvItem;
5742 ITERATOR i;
5744 TRACE("(pt=%s, subitem=%d, select=%d)\n", debugpoint(&lpht->pt), subitem, select);
5746 lpht->flags = 0;
5747 lpht->iItem = -1;
5748 if (subitem) lpht->iSubItem = 0;
5750 if (infoPtr->rcList.left > lpht->pt.x)
5751 lpht->flags |= LVHT_TOLEFT;
5752 else if (infoPtr->rcList.right < lpht->pt.x)
5753 lpht->flags |= LVHT_TORIGHT;
5755 if (infoPtr->rcList.top > lpht->pt.y)
5756 lpht->flags |= LVHT_ABOVE;
5757 else if (infoPtr->rcList.bottom < lpht->pt.y)
5758 lpht->flags |= LVHT_BELOW;
5760 TRACE("lpht->flags=0x%x\n", lpht->flags);
5761 if (lpht->flags) return -1;
5763 lpht->flags |= LVHT_NOWHERE;
5765 LISTVIEW_GetOrigin(infoPtr, &Origin);
5767 /* first deal with the large items */
5768 rcSearch.left = lpht->pt.x;
5769 rcSearch.top = lpht->pt.y;
5770 rcSearch.right = rcSearch.left + 1;
5771 rcSearch.bottom = rcSearch.top + 1;
5773 iterator_frameditems(&i, infoPtr, &rcSearch);
5774 iterator_next(&i); /* go to first item in the sequence */
5775 lpht->iItem = i.nItem;
5776 iterator_destroy(&i);
5778 TRACE("lpht->iItem=%d\n", lpht->iItem);
5779 if (lpht->iItem == -1) return -1;
5781 lvItem.mask = LVIF_STATE | LVIF_TEXT;
5782 if (uView == LVS_REPORT) lvItem.mask |= LVIF_INDENT;
5783 lvItem.stateMask = LVIS_STATEIMAGEMASK;
5784 if (uView == LVS_ICON) lvItem.stateMask |= LVIS_FOCUSED;
5785 lvItem.iItem = lpht->iItem;
5786 lvItem.iSubItem = 0;
5787 lvItem.pszText = szDispText;
5788 lvItem.cchTextMax = DISP_TEXT_SIZE;
5789 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) return -1;
5790 if (!infoPtr->bFocus) lvItem.state &= ~LVIS_FOCUSED;
5792 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, &rcBox, &rcState, &rcIcon, &rcLabel);
5793 LISTVIEW_GetItemOrigin(infoPtr, lpht->iItem, &Position);
5794 opt.x = lpht->pt.x - Position.x - Origin.x;
5795 opt.y = lpht->pt.y - Position.y - Origin.y;
5797 if (uView == LVS_REPORT && (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT))
5798 rcBounds = rcBox;
5799 else
5800 UnionRect(&rcBounds, &rcIcon, &rcLabel);
5801 TRACE("rcBounds=%s\n", debugrect(&rcBounds));
5802 if (!PtInRect(&rcBounds, opt)) return -1;
5804 if (PtInRect(&rcIcon, opt))
5805 lpht->flags |= LVHT_ONITEMICON;
5806 else if (PtInRect(&rcLabel, opt))
5807 lpht->flags |= LVHT_ONITEMLABEL;
5808 else if (infoPtr->himlState && ((lvItem.state & LVIS_STATEIMAGEMASK) >> 12) && PtInRect(&rcState, opt))
5809 lpht->flags |= LVHT_ONITEMSTATEICON;
5810 if (lpht->flags & LVHT_ONITEM)
5811 lpht->flags &= ~LVHT_NOWHERE;
5813 TRACE("lpht->flags=0x%x\n", lpht->flags);
5814 if (uView == LVS_REPORT && lpht->iItem != -1 && subitem)
5816 INT j;
5818 rcBounds.right = rcBounds.left;
5819 for (j = 0; j < infoPtr->hdpaColumns->nItemCount; j++)
5821 rcBounds.left = rcBounds.right;
5822 rcBounds.right += LISTVIEW_GetColumnWidth(infoPtr, j);
5823 if (PtInRect(&rcBounds, opt))
5825 lpht->iSubItem = j;
5826 break;
5831 if (!select || lpht->iItem == -1) return lpht->iItem;
5833 if (uView == LVS_REPORT && (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT)) return lpht->iItem;
5835 if (uView == LVS_REPORT) UnionRect(&rcBounds, &rcIcon, &rcLabel);
5836 return PtInRect(&rcBounds, opt) ? lpht->iItem : -1;
5840 /* LISTVIEW_InsertCompare: callback routine for comparing pszText members of the LV_ITEMS
5841 in a LISTVIEW on insert. Passed to DPA_Sort in LISTVIEW_InsertItem.
5842 This function should only be used for inserting items into a sorted list (LVM_INSERTITEM)
5843 and not during the processing of a LVM_SORTITEMS message. Applications should provide
5844 their own sort proc. when sending LVM_SORTITEMS.
5846 /* Platform SDK:
5847 (remarks on LVITEM: LVM_INSERTITEM will insert the new item in the proper sort postion...
5849 LVS_SORTXXX must be specified,
5850 LVS_OWNERDRAW is not set,
5851 <item>.pszText is not LPSTR_TEXTCALLBACK.
5853 (LVS_SORT* flags): "For the LVS_SORTASCENDING... styles, item indices
5854 are sorted based on item text..."
5856 static INT WINAPI LISTVIEW_InsertCompare( LPVOID first, LPVOID second, LPARAM lParam)
5858 ITEM_INFO* lv_first = (ITEM_INFO*) DPA_GetPtr( (HDPA)first, 0 );
5859 ITEM_INFO* lv_second = (ITEM_INFO*) DPA_GetPtr( (HDPA)second, 0 );
5860 INT cmpv = textcmpWT(lv_first->hdr.pszText, lv_second->hdr.pszText, TRUE);
5862 /* if we're sorting descending, negate the return value */
5863 return (((LISTVIEW_INFO *)lParam)->dwStyle & LVS_SORTDESCENDING) ? -cmpv : cmpv;
5866 /***
5867 * nESCRIPTION:
5868 * Inserts a new item in the listview control.
5870 * PARAMETER(S):
5871 * [I] infoPtr : valid pointer to the listview structure
5872 * [I] lpLVItem : item information
5873 * [I] isW : TRUE if lpLVItem is Unicode, FALSE if it's ANSI
5875 * RETURN:
5876 * SUCCESS : new item index
5877 * FAILURE : -1
5879 static INT LISTVIEW_InsertItemT(LISTVIEW_INFO *infoPtr, const LVITEMW *lpLVItem, BOOL isW)
5881 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
5882 INT nItem;
5883 HDPA hdpaSubItems;
5884 NMLISTVIEW nmlv;
5885 ITEM_INFO *lpItem;
5886 BOOL is_sorted, has_changed;
5887 LVITEMW item;
5889 TRACE("(lpLVItem=%s, isW=%d)\n", debuglvitem_t(lpLVItem, isW), isW);
5891 if (infoPtr->dwStyle & LVS_OWNERDATA) return infoPtr->nItemCount++;
5893 /* make sure it's an item, and not a subitem; cannot insert a subitem */
5894 if (!lpLVItem || lpLVItem->iItem < 0 || lpLVItem->iSubItem) return -1;
5896 if (!is_assignable_item(lpLVItem, infoPtr->dwStyle)) return -1;
5898 if ( !(lpItem = (ITEM_INFO *)COMCTL32_Alloc(sizeof(ITEM_INFO))) )
5899 return -1;
5901 /* insert item in listview control data structure */
5902 if ( !(hdpaSubItems = DPA_Create(8)) ) goto fail;
5903 if ( !DPA_SetPtr(hdpaSubItems, 0, lpItem) ) assert (FALSE);
5905 is_sorted = (infoPtr->dwStyle & (LVS_SORTASCENDING | LVS_SORTDESCENDING)) &&
5906 !(infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && (LPSTR_TEXTCALLBACKW != lpLVItem->pszText);
5908 nItem = is_sorted ? infoPtr->nItemCount : min(lpLVItem->iItem, infoPtr->nItemCount);
5909 TRACE(" inserting at %d, sorted=%d, count=%d, iItem=%d\n", nItem, is_sorted, infoPtr->nItemCount, lpLVItem->iItem);
5910 nItem = DPA_InsertPtr( infoPtr->hdpaItems, nItem, hdpaSubItems );
5911 if (nItem == -1) goto fail;
5912 infoPtr->nItemCount++;
5914 /* set the item attributes */
5915 item = *lpLVItem;
5916 item.iItem = nItem;
5917 item.state &= ~LVIS_STATEIMAGEMASK;
5918 if (!set_main_item(infoPtr, &item, TRUE, isW, &has_changed)) goto undo;
5920 /* if we're sorted, sort the list, and update the index */
5921 if (is_sorted)
5923 DPA_Sort( infoPtr->hdpaItems, LISTVIEW_InsertCompare, (LPARAM)infoPtr );
5924 nItem = DPA_GetPtrIndex( infoPtr->hdpaItems, hdpaSubItems );
5925 assert(nItem != -1);
5928 /* make room for the position, if we are in the right mode */
5929 if ((uView == LVS_SMALLICON) || (uView == LVS_ICON))
5931 if (DPA_InsertPtr(infoPtr->hdpaPosX, nItem, 0) == -1)
5932 goto undo;
5933 if (DPA_InsertPtr(infoPtr->hdpaPosY, nItem, 0) == -1)
5935 DPA_DeletePtr(infoPtr->hdpaPosX, nItem);
5936 goto undo;
5940 /* Add the subitem list to the items array. Do this last in case we go to
5941 * fail during the above.
5943 LISTVIEW_ShiftIndices(infoPtr, nItem, 1);
5945 /* send LVN_INSERTITEM notification */
5946 ZeroMemory(&nmlv, sizeof(NMLISTVIEW));
5947 nmlv.iItem = nItem;
5948 nmlv.lParam = lpItem->lParam;
5949 notify_listview(infoPtr, LVN_INSERTITEM, &nmlv);
5951 /* align items (set position of each item) */
5952 if ((uView == LVS_SMALLICON || uView == LVS_ICON))
5954 POINT pt;
5956 if (infoPtr->dwStyle & LVS_ALIGNLEFT)
5957 LISTVIEW_NextIconPosLeft(infoPtr, &pt);
5958 else
5959 LISTVIEW_NextIconPosTop(infoPtr, &pt);
5961 LISTVIEW_MoveIconTo(infoPtr, nItem, &pt, TRUE);
5964 /* now is the invalidation fun */
5965 LISTVIEW_ScrollOnInsert(infoPtr, nItem, 1);
5966 return nItem;
5968 undo:
5969 DPA_DeletePtr(infoPtr->hdpaItems, nItem);
5970 infoPtr->nItemCount--;
5971 fail:
5972 DPA_DeletePtr(hdpaSubItems, 0);
5973 DPA_Destroy (hdpaSubItems);
5974 COMCTL32_Free (lpItem);
5975 return -1;
5978 /***
5979 * DESCRIPTION:
5980 * Redraws a range of items.
5982 * PARAMETER(S):
5983 * [I] infoPtr : valid pointer to the listview structure
5984 * [I] nFirst : first item
5985 * [I] nLast : last item
5987 * RETURN:
5988 * SUCCESS : TRUE
5989 * FAILURE : FALSE
5991 static BOOL LISTVIEW_RedrawItems(LISTVIEW_INFO *infoPtr, INT nFirst, INT nLast)
5993 INT i;
5995 if (nLast < nFirst || min(nFirst, nLast) < 0 ||
5996 max(nFirst, nLast) >= infoPtr->nItemCount)
5997 return FALSE;
5999 for (i = nFirst; i <= nLast; i++)
6000 LISTVIEW_InvalidateItem(infoPtr, i);
6002 return TRUE;
6005 /***
6006 * DESCRIPTION:
6007 * Scroll the content of a listview.
6009 * PARAMETER(S):
6010 * [I] infoPtr : valid pointer to the listview structure
6011 * [I] dx : horizontal scroll amount in pixels
6012 * [I] dy : vertical scroll amount in pixels
6014 * RETURN:
6015 * SUCCESS : TRUE
6016 * FAILURE : FALSE
6018 * COMMENTS:
6019 * If the control is in report mode (LVS_REPORT) the control can
6020 * be scrolled only in line increments. "dy" will be rounded to the
6021 * nearest number of pixels that are a whole line. Ex: if line height
6022 * is 16 and an 8 is passed, the list will be scrolled by 16. If a 7
6023 * is passed the the scroll will be 0. (per MSDN 7/2002)
6025 * For: (per experimentaion with native control and CSpy ListView)
6026 * LVS_ICON dy=1 = 1 pixel (vertical only)
6027 * dx ignored
6028 * LVS_SMALLICON dy=1 = 1 pixel (vertical only)
6029 * dx ignored
6030 * LVS_LIST dx=1 = 1 column (horizontal only)
6031 * but will only scroll 1 column per message
6032 * no matter what the value.
6033 * dy must be 0 or FALSE returned.
6034 * LVS_REPORT dx=1 = 1 pixel
6035 * dy= see above
6038 static BOOL LISTVIEW_Scroll(LISTVIEW_INFO *infoPtr, INT dx, INT dy)
6040 switch(infoPtr->dwStyle & LVS_TYPEMASK) {
6041 case LVS_REPORT:
6042 dy += (dy < 0 ? -1 : 1) * infoPtr->nItemHeight/2;
6043 dy /= infoPtr->nItemHeight;
6044 break;
6045 case LVS_LIST:
6046 if (dy != 0) return FALSE;
6047 break;
6048 default: /* icon */
6049 dx = 0;
6050 break;
6053 if (dx != 0) LISTVIEW_HScroll(infoPtr, SB_INTERNAL, dx, 0);
6054 if (dy != 0) LISTVIEW_VScroll(infoPtr, SB_INTERNAL, dy, 0);
6056 return TRUE;
6059 /***
6060 * DESCRIPTION:
6061 * Sets the background color.
6063 * PARAMETER(S):
6064 * [I] infoPtr : valid pointer to the listview structure
6065 * [I] clrBk : background color
6067 * RETURN:
6068 * SUCCESS : TRUE
6069 * FAILURE : FALSE
6071 static BOOL LISTVIEW_SetBkColor(LISTVIEW_INFO *infoPtr, COLORREF clrBk)
6073 TRACE("(clrBk=%lx)\n", clrBk);
6075 if(infoPtr->clrBk != clrBk) {
6076 if (infoPtr->clrBk != CLR_NONE) DeleteObject(infoPtr->hBkBrush);
6077 infoPtr->clrBk = clrBk;
6078 if (clrBk == CLR_NONE)
6079 infoPtr->hBkBrush = (HBRUSH)GetClassLongW(infoPtr->hwndSelf, GCL_HBRBACKGROUND);
6080 else
6081 infoPtr->hBkBrush = CreateSolidBrush(clrBk);
6082 LISTVIEW_InvalidateList(infoPtr);
6085 return TRUE;
6088 /* LISTVIEW_SetBkImage */
6090 /*** Helper for {Insert,Set}ColumnT *only* */
6091 static void column_fill_hditem(LISTVIEW_INFO *infoPtr, HDITEMW *lphdi, INT nColumn, const LVCOLUMNW *lpColumn, BOOL isW)
6093 if (lpColumn->mask & LVCF_FMT)
6095 /* format member is valid */
6096 lphdi->mask |= HDI_FORMAT;
6098 /* set text alignment (leftmost column must be left-aligned) */
6099 if (nColumn == 0 || lpColumn->fmt & LVCFMT_LEFT)
6100 lphdi->fmt |= HDF_LEFT;
6101 else if (lpColumn->fmt & LVCFMT_RIGHT)
6102 lphdi->fmt |= HDF_RIGHT;
6103 else if (lpColumn->fmt & LVCFMT_CENTER)
6104 lphdi->fmt |= HDF_CENTER;
6106 if (lpColumn->fmt & LVCFMT_BITMAP_ON_RIGHT)
6107 lphdi->fmt |= HDF_BITMAP_ON_RIGHT;
6109 if (lpColumn->fmt & LVCFMT_COL_HAS_IMAGES)
6111 lphdi->fmt |= HDF_IMAGE;
6112 lphdi->iImage = I_IMAGECALLBACK;
6116 if (lpColumn->mask & LVCF_WIDTH)
6118 lphdi->mask |= HDI_WIDTH;
6119 if(lpColumn->cx == LVSCW_AUTOSIZE_USEHEADER)
6121 /* make it fill the remainder of the controls width */
6122 RECT rcHeader;
6123 INT item_index;
6125 for(item_index = 0; item_index < (nColumn - 1); item_index++)
6127 LISTVIEW_GetHeaderRect(infoPtr, item_index, &rcHeader);
6128 lphdi->cxy += rcHeader.right - rcHeader.left;
6131 /* retrieve the layout of the header */
6132 GetClientRect(infoPtr->hwndSelf, &rcHeader);
6133 TRACE("start cxy=%d rcHeader=%s\n", lphdi->cxy, debugrect(&rcHeader));
6135 lphdi->cxy = (rcHeader.right - rcHeader.left) - lphdi->cxy;
6137 else
6138 lphdi->cxy = lpColumn->cx;
6141 if (lpColumn->mask & LVCF_TEXT)
6143 lphdi->mask |= HDI_TEXT | HDI_FORMAT;
6144 lphdi->fmt |= HDF_STRING;
6145 lphdi->pszText = lpColumn->pszText;
6146 lphdi->cchTextMax = textlenT(lpColumn->pszText, isW);
6149 if (lpColumn->mask & LVCF_IMAGE)
6151 lphdi->mask |= HDI_IMAGE;
6152 lphdi->iImage = lpColumn->iImage;
6155 if (lpColumn->mask & LVCF_ORDER)
6157 lphdi->mask |= HDI_ORDER;
6158 lphdi->iOrder = lpColumn->iOrder;
6163 /***
6164 * DESCRIPTION:
6165 * Inserts a new column.
6167 * PARAMETER(S):
6168 * [I] infoPtr : valid pointer to the listview structure
6169 * [I] nColumn : column index
6170 * [I] lpColumn : column information
6171 * [I] isW : TRUE if lpColumn is Unicode, FALSE otherwise
6173 * RETURN:
6174 * SUCCESS : new column index
6175 * FAILURE : -1
6177 static INT LISTVIEW_InsertColumnT(LISTVIEW_INFO *infoPtr, INT nColumn,
6178 const LVCOLUMNW *lpColumn, BOOL isW)
6180 COLUMN_INFO *lpColumnInfo;
6181 INT nNewColumn;
6182 HDITEMW hdi;
6184 TRACE("(nColumn=%d, lpColumn=%s, isW=%d)\n", nColumn, debuglvcolumn_t(lpColumn, isW), isW);
6186 if (!lpColumn || nColumn < 0) return -1;
6187 nColumn = min(nColumn, infoPtr->hdpaColumns->nItemCount);
6189 ZeroMemory(&hdi, sizeof(HDITEMW));
6190 column_fill_hditem(infoPtr, &hdi, nColumn, lpColumn, isW);
6192 /* insert item in header control */
6193 nNewColumn = SendMessageW(infoPtr->hwndHeader,
6194 isW ? HDM_INSERTITEMW : HDM_INSERTITEMA,
6195 (WPARAM)nColumn, (LPARAM)&hdi);
6196 if (nNewColumn == -1) return -1;
6197 if (nNewColumn != nColumn) ERR("nColumn=%d, nNewColumn=%d\n", nColumn, nNewColumn);
6199 /* create our own column info */
6200 if (!(lpColumnInfo = COMCTL32_Alloc(sizeof(COLUMN_INFO)))) goto fail;
6201 if (DPA_InsertPtr(infoPtr->hdpaColumns, nNewColumn, lpColumnInfo) == -1) goto fail;
6203 if (lpColumn->mask & LVCF_FMT) lpColumnInfo->fmt = lpColumn->fmt;
6204 if (!Header_GetItemRect(infoPtr->hwndHeader, nNewColumn, &lpColumnInfo->rcHeader)) goto fail;
6206 /* now we have to actually adjust the data */
6207 if (!(infoPtr->dwStyle & LVS_OWNERDATA) && infoPtr->nItemCount > 0)
6209 SUBITEM_INFO *lpSubItem, *lpMainItem, **lpNewItems = 0;
6210 HDPA hdpaSubItems;
6211 INT nItem, i;
6213 /* preallocate memory, so we can fail gracefully */
6214 if (nNewColumn == 0)
6216 lpNewItems = COMCTL32_Alloc(sizeof(SUBITEM_INFO *) * infoPtr->nItemCount);
6217 if (!lpNewItems) goto fail;
6218 for (i = 0; i < infoPtr->nItemCount; i++)
6219 if (!(lpNewItems[i] = COMCTL32_Alloc(sizeof(SUBITEM_INFO)))) break;
6220 if (i != infoPtr->nItemCount)
6222 for(; i >=0; i--) COMCTL32_Free(lpNewItems[i]);
6223 COMCTL32_Free(lpNewItems);
6224 goto fail;
6228 for (nItem = 0; nItem < infoPtr->nItemCount; nItem++)
6230 hdpaSubItems = (HDPA)DPA_GetPtr(infoPtr->hdpaItems, nItem);
6231 for (i = 1; i < hdpaSubItems->nItemCount; i++)
6233 lpSubItem = (SUBITEM_INFO *)DPA_GetPtr(hdpaSubItems, i);
6234 if (lpSubItem->iSubItem >= nNewColumn)
6235 lpSubItem->iSubItem++;
6238 /* for inserting column 0, we have to special-case the main item */
6239 if (nNewColumn == 0)
6241 lpMainItem = (SUBITEM_INFO *)DPA_GetPtr(hdpaSubItems, 0);
6242 lpSubItem = lpNewItems[nItem];
6243 lpSubItem->hdr = lpMainItem->hdr;
6244 lpSubItem->iSubItem = 1;
6245 ZeroMemory(&lpMainItem->hdr, sizeof(lpMainItem->hdr));
6246 lpMainItem->iSubItem = 0;
6247 DPA_InsertPtr(hdpaSubItems, 1, lpSubItem);
6251 COMCTL32_Free(lpNewItems);
6254 /* make space for the new column */
6255 LISTVIEW_ScrollColumns(infoPtr, nNewColumn + 1, lpColumnInfo->rcHeader.right - lpColumnInfo->rcHeader.left);
6257 return nNewColumn;
6259 fail:
6260 if (nNewColumn != -1) SendMessageW(infoPtr->hwndHeader, HDM_DELETEITEM, nNewColumn, 0);
6261 if (lpColumnInfo)
6263 DPA_DeletePtr(infoPtr->hdpaColumns, nNewColumn);
6264 COMCTL32_Free(lpColumnInfo);
6266 return -1;
6269 /***
6270 * DESCRIPTION:
6271 * Sets the attributes of a header item.
6273 * PARAMETER(S):
6274 * [I] infoPtr : valid pointer to the listview structure
6275 * [I] nColumn : column index
6276 * [I] lpColumn : column attributes
6277 * [I] isW: if TRUE, the lpColumn is a LPLVCOLUMNW, else it is a LPLVCOLUMNA
6279 * RETURN:
6280 * SUCCESS : TRUE
6281 * FAILURE : FALSE
6283 static BOOL LISTVIEW_SetColumnT(LISTVIEW_INFO *infoPtr, INT nColumn,
6284 const LVCOLUMNW *lpColumn, BOOL isW)
6286 HDITEMW hdi, hdiget;
6287 BOOL bResult;
6289 TRACE("(nColumn=%d, lpColumn=%s, isW=%d)\n", nColumn, debuglvcolumn_t(lpColumn, isW), isW);
6291 if (!lpColumn || nColumn < 0 || nColumn >= infoPtr->hdpaColumns->nItemCount) return FALSE;
6293 ZeroMemory(&hdi, sizeof(HDITEMW));
6294 if (lpColumn->mask & LVCF_FMT)
6296 hdi.mask |= HDI_FORMAT;
6297 hdiget.mask = HDI_FORMAT;
6298 if (Header_GetItemW(infoPtr->hwndHeader, nColumn, &hdiget))
6299 hdi.fmt = hdiget.fmt & HDF_STRING;
6301 column_fill_hditem(infoPtr, &hdi, nColumn, lpColumn, isW);
6303 /* set header item attributes */
6304 bResult = SendMessageW(infoPtr->hwndHeader, isW ? HDM_SETITEMW : HDM_SETITEMA, (WPARAM)nColumn, (LPARAM)&hdi);
6305 if (!bResult) return FALSE;
6307 if (lpColumn->mask & LVCF_FMT)
6309 COLUMN_INFO *lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, nColumn);
6310 int oldFmt = lpColumnInfo->fmt;
6312 lpColumnInfo->fmt = lpColumn->fmt;
6313 if ((oldFmt ^ lpColumn->fmt) & (LVCFMT_JUSTIFYMASK | LVCFMT_IMAGE))
6315 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
6316 if (uView == LVS_REPORT) LISTVIEW_InvalidateColumn(infoPtr, nColumn);
6320 return TRUE;
6323 /***
6324 * DESCRIPTION:
6325 * Sets the column order array
6327 * PARAMETERS:
6328 * [I] infoPtr : valid pointer to the listview structure
6329 * [I] iCount : number of elements in column order array
6330 * [I] lpiArray : pointer to column order array
6332 * RETURN:
6333 * SUCCESS : TRUE
6334 * FAILURE : FALSE
6336 static BOOL LISTVIEW_SetColumnOrderArray(LISTVIEW_INFO *infoPtr, INT iCount, const INT *lpiArray)
6338 FIXME("iCount %d lpiArray %p\n", iCount, lpiArray);
6340 if (!lpiArray)
6341 return FALSE;
6343 return TRUE;
6347 /***
6348 * DESCRIPTION:
6349 * Sets the width of a column
6351 * PARAMETERS:
6352 * [I] infoPtr : valid pointer to the listview structure
6353 * [I] nColumn : column index
6354 * [I] cx : column width
6356 * RETURN:
6357 * SUCCESS : TRUE
6358 * FAILURE : FALSE
6360 static BOOL LISTVIEW_SetColumnWidth(LISTVIEW_INFO *infoPtr, INT nColumn, INT cx)
6362 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
6363 WCHAR szDispText[DISP_TEXT_SIZE] = { 0 };
6364 INT max_cx = 0;
6365 HDITEMW hdi;
6367 TRACE("(nColumn=%d, cx=%d\n", nColumn, cx);
6369 /* set column width only if in report or list mode */
6370 if (uView != LVS_REPORT && uView != LVS_LIST) return FALSE;
6372 /* take care of invalid cx values */
6373 if(uView == LVS_REPORT && cx < -2) cx = LVSCW_AUTOSIZE;
6374 else if (uView == LVS_LIST && cx < 1) return FALSE;
6376 /* resize all columns if in LVS_LIST mode */
6377 if(uView == LVS_LIST)
6379 infoPtr->nItemWidth = cx;
6380 LISTVIEW_InvalidateList(infoPtr);
6381 return TRUE;
6384 if (nColumn < 0 || nColumn >= infoPtr->hdpaColumns->nItemCount) return FALSE;
6386 if (cx == LVSCW_AUTOSIZE || (cx == LVSCW_AUTOSIZE_USEHEADER && nColumn < infoPtr->hdpaColumns->nItemCount -1))
6388 INT nLabelWidth;
6389 LVITEMW lvItem;
6391 lvItem.mask = LVIF_TEXT;
6392 lvItem.iItem = 0;
6393 lvItem.iSubItem = nColumn;
6394 lvItem.pszText = szDispText;
6395 lvItem.cchTextMax = DISP_TEXT_SIZE;
6396 for (; lvItem.iItem < infoPtr->nItemCount; lvItem.iItem++)
6398 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) continue;
6399 nLabelWidth = LISTVIEW_GetStringWidthT(infoPtr, lvItem.pszText, TRUE);
6400 if (max_cx < nLabelWidth) max_cx = nLabelWidth;
6402 if (infoPtr->himlSmall && (nColumn == 0 || (LISTVIEW_GetColumnInfo(infoPtr, nColumn)->fmt & LVCFMT_IMAGE)))
6403 max_cx += infoPtr->iconSize.cx;
6404 max_cx += TRAILING_LABEL_PADDING;
6407 /* autosize based on listview items width */
6408 if(cx == LVSCW_AUTOSIZE)
6409 cx = max_cx;
6410 else if(cx == LVSCW_AUTOSIZE_USEHEADER)
6412 /* if iCol is the last column make it fill the remainder of the controls width */
6413 if(nColumn == infoPtr->hdpaColumns->nItemCount - 1)
6415 RECT rcHeader;
6416 POINT Origin;
6418 LISTVIEW_GetOrigin(infoPtr, &Origin);
6419 LISTVIEW_GetHeaderRect(infoPtr, nColumn, &rcHeader);
6421 cx = infoPtr->rcList.right - Origin.x - rcHeader.left;
6423 else
6425 /* Despite what the MS docs say, if this is not the last
6426 column, then MS resizes the column to the width of the
6427 largest text string in the column, including headers
6428 and items. This is different from LVSCW_AUTOSIZE in that
6429 LVSCW_AUTOSIZE ignores the header string length. */
6430 cx = 0;
6432 /* retrieve header text */
6433 hdi.mask = HDI_TEXT;
6434 hdi.cchTextMax = DISP_TEXT_SIZE;
6435 hdi.pszText = szDispText;
6436 if (Header_GetItemW(infoPtr->hwndHeader, nColumn, (LPARAM)&hdi))
6438 HDC hdc = GetDC(infoPtr->hwndSelf);
6439 HFONT old_font = SelectObject(hdc, (HFONT)SendMessageW(infoPtr->hwndHeader, WM_GETFONT, 0, 0));
6440 SIZE size;
6442 if (GetTextExtentPoint32W(hdc, hdi.pszText, lstrlenW(hdi.pszText), &size))
6443 cx = size.cx + TRAILING_HEADER_PADDING;
6444 /* FIXME: Take into account the header image, if one is present */
6445 SelectObject(hdc, old_font);
6446 ReleaseDC(infoPtr->hwndSelf, hdc);
6448 cx = max (cx, max_cx);
6452 if (cx < 0) return FALSE;
6454 /* call header to update the column change */
6455 hdi.mask = HDI_WIDTH;
6456 hdi.cxy = cx;
6457 TRACE("hdi.cxy=%d\n", hdi.cxy);
6458 return Header_SetItemW(infoPtr->hwndHeader, nColumn, (LPARAM)&hdi);
6461 /***
6462 * DESCRIPTION:
6463 * Sets the extended listview style.
6465 * PARAMETERS:
6466 * [I] infoPtr : valid pointer to the listview structure
6467 * [I] dwMask : mask
6468 * [I] dwStyle : style
6470 * RETURN:
6471 * SUCCESS : previous style
6472 * FAILURE : 0
6474 static DWORD LISTVIEW_SetExtendedListViewStyle(LISTVIEW_INFO *infoPtr, DWORD dwMask, DWORD dwStyle)
6476 DWORD dwOldStyle = infoPtr->dwLvExStyle;
6478 /* set new style */
6479 if (dwMask)
6480 infoPtr->dwLvExStyle = (dwOldStyle & ~dwMask) | (dwStyle & dwMask);
6481 else
6482 infoPtr->dwLvExStyle = dwStyle;
6484 return dwOldStyle;
6487 /***
6488 * DESCRIPTION:
6489 * Sets the new hot cursor used during hot tracking and hover selection.
6491 * PARAMETER(S):
6492 * [I] infoPtr : valid pointer to the listview structure
6493 * [I} hCurosr : the new hot cursor handle
6495 * RETURN:
6496 * Returns the previous hot cursor
6498 static HCURSOR LISTVIEW_SetHotCursor(LISTVIEW_INFO *infoPtr, HCURSOR hCursor)
6500 HCURSOR oldCursor = infoPtr->hHotCursor;
6502 infoPtr->hHotCursor = hCursor;
6504 return oldCursor;
6508 /***
6509 * DESCRIPTION:
6510 * Sets the hot item index.
6512 * PARAMETERS:
6513 * [I] infoPtr : valid pointer to the listview structure
6514 * [I] iIndex : index
6516 * RETURN:
6517 * SUCCESS : previous hot item index
6518 * FAILURE : -1 (no hot item)
6520 static INT LISTVIEW_SetHotItem(LISTVIEW_INFO *infoPtr, INT iIndex)
6522 INT iOldIndex = infoPtr->nHotItem;
6524 infoPtr->nHotItem = iIndex;
6526 return iOldIndex;
6530 /***
6531 * DESCRIPTION:
6532 * Sets the amount of time the cursor must hover over an item before it is selected.
6534 * PARAMETER(S):
6535 * [I] infoPtr : valid pointer to the listview structure
6536 * [I] dwHoverTime : hover time, if -1 the hover time is set to the default
6538 * RETURN:
6539 * Returns the previous hover time
6541 static DWORD LISTVIEW_SetHoverTime(LISTVIEW_INFO *infoPtr, DWORD dwHoverTime)
6543 DWORD oldHoverTime = infoPtr->dwHoverTime;
6545 infoPtr->dwHoverTime = dwHoverTime;
6547 return oldHoverTime;
6550 /***
6551 * DESCRIPTION:
6552 * Sets spacing for icons of LVS_ICON style.
6554 * PARAMETER(S):
6555 * [I] infoPtr : valid pointer to the listview structure
6556 * [I] cx : horizontal spacing (-1 = system spacing, 0 = autosize)
6557 * [I] cy : vertical spacing (-1 = system spacing, 0 = autosize)
6559 * RETURN:
6560 * MAKELONG(oldcx, oldcy)
6562 static DWORD LISTVIEW_SetIconSpacing(LISTVIEW_INFO *infoPtr, INT cx, INT cy)
6564 DWORD oldspacing = MAKELONG(infoPtr->iconSpacing.cx, infoPtr->iconSpacing.cy);
6565 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
6567 TRACE("requested=(%d,%d)\n", cx, cy);
6569 /* this is supported only for LVS_ICON style */
6570 if (uView != LVS_ICON) return oldspacing;
6572 /* set to defaults, if instructed to */
6573 if (cx == -1) cx = GetSystemMetrics(SM_CXICONSPACING);
6574 if (cy == -1) cy = GetSystemMetrics(SM_CYICONSPACING);
6576 /* if 0 then compute width
6577 * FIXME: Should scan each item and determine max width of
6578 * icon or label, then make that the width */
6579 if (cx == 0)
6580 cx = infoPtr->iconSpacing.cx;
6582 /* if 0 then compute height */
6583 if (cy == 0)
6584 cy = infoPtr->iconSize.cy + 2 * infoPtr->ntmHeight +
6585 ICON_BOTTOM_PADDING + ICON_TOP_PADDING + LABEL_VERT_PADDING;
6588 infoPtr->iconSpacing.cx = cx;
6589 infoPtr->iconSpacing.cy = cy;
6591 TRACE("old=(%d,%d), new=(%d,%d), iconSize=(%ld,%ld), ntmH=%d\n",
6592 LOWORD(oldspacing), HIWORD(oldspacing), cx, cy,
6593 infoPtr->iconSize.cx, infoPtr->iconSize.cy,
6594 infoPtr->ntmHeight);
6596 /* these depend on the iconSpacing */
6597 LISTVIEW_UpdateItemSize(infoPtr);
6599 return oldspacing;
6602 inline void set_icon_size(SIZE *size, HIMAGELIST himl, BOOL small)
6604 INT cx, cy;
6606 if (himl && ImageList_GetIconSize(himl, &cx, &cy))
6608 size->cx = cx;
6609 size->cy = cy;
6611 else
6613 size->cx = GetSystemMetrics(small ? SM_CXSMICON : SM_CXICON);
6614 size->cy = GetSystemMetrics(small ? SM_CYSMICON : SM_CYICON);
6618 /***
6619 * DESCRIPTION:
6620 * Sets image lists.
6622 * PARAMETER(S):
6623 * [I] infoPtr : valid pointer to the listview structure
6624 * [I] nType : image list type
6625 * [I] himl : image list handle
6627 * RETURN:
6628 * SUCCESS : old image list
6629 * FAILURE : NULL
6631 static HIMAGELIST LISTVIEW_SetImageList(LISTVIEW_INFO *infoPtr, INT nType, HIMAGELIST himl)
6633 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
6634 INT oldHeight = infoPtr->nItemHeight;
6635 HIMAGELIST himlOld = 0;
6637 TRACE("(nType=%d, himl=%p\n", nType, himl);
6639 switch (nType)
6641 case LVSIL_NORMAL:
6642 himlOld = infoPtr->himlNormal;
6643 infoPtr->himlNormal = himl;
6644 if (uView == LVS_ICON) set_icon_size(&infoPtr->iconSize, himl, FALSE);
6645 LISTVIEW_SetIconSpacing(infoPtr, 0, 0);
6646 break;
6648 case LVSIL_SMALL:
6649 himlOld = infoPtr->himlSmall;
6650 infoPtr->himlSmall = himl;
6651 if (uView != LVS_ICON) set_icon_size(&infoPtr->iconSize, himl, TRUE);
6652 break;
6654 case LVSIL_STATE:
6655 himlOld = infoPtr->himlState;
6656 infoPtr->himlState = himl;
6657 set_icon_size(&infoPtr->iconStateSize, himl, TRUE);
6658 ImageList_SetBkColor(infoPtr->himlState, CLR_NONE);
6659 break;
6661 default:
6662 ERR("Unknown icon type=%d\n", nType);
6663 return NULL;
6666 infoPtr->nItemHeight = LISTVIEW_CalculateItemHeight(infoPtr);
6667 if (infoPtr->nItemHeight != oldHeight)
6668 LISTVIEW_UpdateScroll(infoPtr);
6670 return himlOld;
6673 /***
6674 * DESCRIPTION:
6675 * Preallocates memory (does *not* set the actual count of items !)
6677 * PARAMETER(S):
6678 * [I] infoPtr : valid pointer to the listview structure
6679 * [I] nItems : item count (projected number of items to allocate)
6680 * [I] dwFlags : update flags
6682 * RETURN:
6683 * SUCCESS : TRUE
6684 * FAILURE : FALSE
6686 static BOOL LISTVIEW_SetItemCount(LISTVIEW_INFO *infoPtr, INT nItems, DWORD dwFlags)
6688 TRACE("(nItems=%d, dwFlags=%lx)\n", nItems, dwFlags);
6690 if (infoPtr->dwStyle & LVS_OWNERDATA)
6692 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
6693 INT nOldCount = infoPtr->nItemCount;
6695 if (nItems < nOldCount)
6697 RANGE range = { nItems, nOldCount };
6698 ranges_del(infoPtr->selectionRanges, range);
6699 if (infoPtr->nFocusedItem >= nItems)
6701 infoPtr->nFocusedItem = -1;
6702 SetRectEmpty(&infoPtr->rcFocus);
6706 infoPtr->nItemCount = nItems;
6707 LISTVIEW_UpdateScroll(infoPtr);
6709 /* the flags are valid only in ownerdata report and list modes */
6710 if (uView == LVS_ICON || uView == LVS_SMALLICON) dwFlags = 0;
6712 if (!(dwFlags & LVSICF_NOSCROLL) && infoPtr->nFocusedItem != -1)
6713 LISTVIEW_EnsureVisible(infoPtr, infoPtr->nFocusedItem, FALSE);
6715 if (!(dwFlags & LVSICF_NOINVALIDATEALL))
6716 LISTVIEW_InvalidateList(infoPtr);
6717 else
6719 INT nFrom, nTo;
6720 POINT Origin;
6721 RECT rcErase;
6723 LISTVIEW_GetOrigin(infoPtr, &Origin);
6724 nFrom = min(nOldCount, nItems);
6725 nTo = max(nOldCount, nItems);
6727 if (uView == LVS_REPORT)
6729 rcErase.left = 0;
6730 rcErase.top = nFrom * infoPtr->nItemHeight;
6731 rcErase.right = infoPtr->nItemWidth;
6732 rcErase.bottom = nTo * infoPtr->nItemHeight;
6733 OffsetRect(&rcErase, Origin.x, Origin.y);
6734 if (IntersectRect(&rcErase, &rcErase, &infoPtr->rcList))
6735 LISTVIEW_InvalidateRect(infoPtr, &rcErase);
6737 else /* LVS_LIST */
6739 INT nPerCol = LISTVIEW_GetCountPerColumn(infoPtr);
6741 rcErase.left = (nFrom / nPerCol) * infoPtr->nItemWidth;
6742 rcErase.top = (nFrom % nPerCol) * infoPtr->nItemHeight;
6743 rcErase.right = rcErase.left + infoPtr->nItemWidth;
6744 rcErase.bottom = nPerCol * infoPtr->nItemHeight;
6745 OffsetRect(&rcErase, Origin.x, Origin.y);
6746 if (IntersectRect(&rcErase, &rcErase, &infoPtr->rcList))
6747 LISTVIEW_InvalidateRect(infoPtr, &rcErase);
6749 rcErase.left = (nFrom / nPerCol + 1) * infoPtr->nItemWidth;
6750 rcErase.top = 0;
6751 rcErase.right = (nTo / nPerCol + 1) * infoPtr->nItemWidth;
6752 rcErase.bottom = nPerCol * infoPtr->nItemHeight;
6753 OffsetRect(&rcErase, Origin.x, Origin.y);
6754 if (IntersectRect(&rcErase, &rcErase, &infoPtr->rcList))
6755 LISTVIEW_InvalidateRect(infoPtr, &rcErase);
6759 else
6761 /* According to MSDN for non-LVS_OWNERDATA this is just
6762 * a performance issue. The control allocates its internal
6763 * data structures for the number of items specified. It
6764 * cuts down on the number of memory allocations. Therefore
6765 * we will just issue a WARN here
6767 WARN("for non-ownerdata performance option not implemented.\n");
6770 return TRUE;
6773 /***
6774 * DESCRIPTION:
6775 * Sets the position of an item.
6777 * PARAMETER(S):
6778 * [I] infoPtr : valid pointer to the listview structure
6779 * [I] nItem : item index
6780 * [I] pt : coordinate
6782 * RETURN:
6783 * SUCCESS : TRUE
6784 * FAILURE : FALSE
6786 static BOOL LISTVIEW_SetItemPosition(LISTVIEW_INFO *infoPtr, INT nItem, POINT pt)
6788 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
6789 POINT Origin;
6791 TRACE("(nItem=%d, &pt=%s\n", nItem, debugpoint(&pt));
6793 if (nItem < 0 || nItem >= infoPtr->nItemCount ||
6794 !(uView == LVS_ICON || uView == LVS_SMALLICON)) return FALSE;
6796 LISTVIEW_GetOrigin(infoPtr, &Origin);
6798 /* This point value seems to be an undocumented feature.
6799 * The best guess is that it means either at the origin,
6800 * or at true beginning of the list. I will assume the origin. */
6801 if ((pt.x == -1) && (pt.y == -1))
6802 pt = Origin;
6804 if (uView == LVS_ICON)
6806 pt.x -= (infoPtr->nItemWidth - infoPtr->iconSize.cx) / 2;
6807 pt.y -= ICON_TOP_PADDING;
6809 pt.x -= Origin.x;
6810 pt.y -= Origin.y;
6812 infoPtr->bAutoarrange = FALSE;
6814 return LISTVIEW_MoveIconTo(infoPtr, nItem, &pt, FALSE);
6817 /***
6818 * DESCRIPTION:
6819 * Sets the state of one or many items.
6821 * PARAMETER(S):
6822 * [I] infoPtr : valid pointer to the listview structure
6823 * [I] nItem : item index
6824 * [I] lpLVItem : item or subitem info
6826 * RETURN:
6827 * SUCCESS : TRUE
6828 * FAILURE : FALSE
6830 static BOOL LISTVIEW_SetItemState(LISTVIEW_INFO *infoPtr, INT nItem, const LVITEMW *lpLVItem)
6832 BOOL bResult = TRUE;
6833 LVITEMW lvItem;
6835 lvItem.iItem = nItem;
6836 lvItem.iSubItem = 0;
6837 lvItem.mask = LVIF_STATE;
6838 lvItem.state = lpLVItem->state;
6839 lvItem.stateMask = lpLVItem->stateMask;
6840 TRACE("lvItem=%s\n", debuglvitem_t(&lvItem, TRUE));
6842 if (nItem == -1)
6844 /* apply to all items */
6845 for (lvItem.iItem = 0; lvItem.iItem < infoPtr->nItemCount; lvItem.iItem++)
6846 if (!LISTVIEW_SetItemT(infoPtr, &lvItem, TRUE)) bResult = FALSE;
6848 else
6849 bResult = LISTVIEW_SetItemT(infoPtr, &lvItem, TRUE);
6851 return bResult;
6854 /***
6855 * DESCRIPTION:
6856 * Sets the text of an item or subitem.
6858 * PARAMETER(S):
6859 * [I] hwnd : window handle
6860 * [I] nItem : item index
6861 * [I] lpLVItem : item or subitem info
6862 * [I] isW : TRUE if input is Unicode
6864 * RETURN:
6865 * SUCCESS : TRUE
6866 * FAILURE : FALSE
6868 static BOOL LISTVIEW_SetItemTextT(LISTVIEW_INFO *infoPtr, INT nItem, const LVITEMW *lpLVItem, BOOL isW)
6870 LVITEMW lvItem;
6872 if (nItem < 0 && nItem >= infoPtr->nItemCount) return FALSE;
6874 lvItem.iItem = nItem;
6875 lvItem.iSubItem = lpLVItem->iSubItem;
6876 lvItem.mask = LVIF_TEXT;
6877 lvItem.pszText = lpLVItem->pszText;
6878 lvItem.cchTextMax = lpLVItem->cchTextMax;
6880 TRACE("(nItem=%d, lpLVItem=%s, isW=%d)\n", nItem, debuglvitem_t(&lvItem, isW), isW);
6882 return LISTVIEW_SetItemT(infoPtr, &lvItem, isW);
6885 /***
6886 * DESCRIPTION:
6887 * Set item index that marks the start of a multiple selection.
6889 * PARAMETER(S):
6890 * [I] infoPtr : valid pointer to the listview structure
6891 * [I] nIndex : index
6893 * RETURN:
6894 * Index number or -1 if there is no selection mark.
6896 static INT LISTVIEW_SetSelectionMark(LISTVIEW_INFO *infoPtr, INT nIndex)
6898 INT nOldIndex = infoPtr->nSelectionMark;
6900 TRACE("(nIndex=%d)\n", nIndex);
6902 infoPtr->nSelectionMark = nIndex;
6904 return nOldIndex;
6907 /***
6908 * DESCRIPTION:
6909 * Sets the text background color.
6911 * PARAMETER(S):
6912 * [I] infoPtr : valid pointer to the listview structure
6913 * [I] clrTextBk : text background color
6915 * RETURN:
6916 * SUCCESS : TRUE
6917 * FAILURE : FALSE
6919 static BOOL LISTVIEW_SetTextBkColor(LISTVIEW_INFO *infoPtr, COLORREF clrTextBk)
6921 TRACE("(clrTextBk=%lx)\n", clrTextBk);
6923 if (infoPtr->clrTextBk != clrTextBk)
6925 infoPtr->clrTextBk = clrTextBk;
6926 LISTVIEW_InvalidateList(infoPtr);
6929 return TRUE;
6932 /***
6933 * DESCRIPTION:
6934 * Sets the text foreground color.
6936 * PARAMETER(S):
6937 * [I] infoPtr : valid pointer to the listview structure
6938 * [I] clrText : text color
6940 * RETURN:
6941 * SUCCESS : TRUE
6942 * FAILURE : FALSE
6944 static BOOL LISTVIEW_SetTextColor (LISTVIEW_INFO *infoPtr, COLORREF clrText)
6946 TRACE("(clrText=%lx)\n", clrText);
6948 if (infoPtr->clrText != clrText)
6950 infoPtr->clrText = clrText;
6951 LISTVIEW_InvalidateList(infoPtr);
6954 return TRUE;
6957 /* LISTVIEW_SetToolTips */
6958 /* LISTVIEW_SetUnicodeFormat */
6959 /* LISTVIEW_SetWorkAreas */
6961 /***
6962 * DESCRIPTION:
6963 * Callback internally used by LISTVIEW_SortItems()
6965 * PARAMETER(S):
6966 * [I] first : pointer to first ITEM_INFO to compare
6967 * [I] second : pointer to second ITEM_INFO to compare
6968 * [I] lParam : HWND of control
6970 * RETURN:
6971 * if first comes before second : negative
6972 * if first comes after second : positive
6973 * if first and second are equivalent : zero
6975 static INT WINAPI LISTVIEW_CallBackCompare(LPVOID first, LPVOID second, LPARAM lParam)
6977 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)GetWindowLongW((HWND)lParam, 0);
6978 ITEM_INFO* lv_first = (ITEM_INFO*) DPA_GetPtr( (HDPA)first, 0 );
6979 ITEM_INFO* lv_second = (ITEM_INFO*) DPA_GetPtr( (HDPA)second, 0 );
6981 /* Forward the call to the client defined callback */
6982 return (infoPtr->pfnCompare)( lv_first->lParam , lv_second->lParam, infoPtr->lParamSort );
6985 /***
6986 * DESCRIPTION:
6987 * Sorts the listview items.
6989 * PARAMETER(S):
6990 * [I] infoPtr : valid pointer to the listview structure
6991 * [I] pfnCompare : application-defined value
6992 * [I] lParamSort : pointer to comparision callback
6994 * RETURN:
6995 * SUCCESS : TRUE
6996 * FAILURE : FALSE
6998 static BOOL LISTVIEW_SortItems(LISTVIEW_INFO *infoPtr, PFNLVCOMPARE pfnCompare, LPARAM lParamSort)
7000 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
7001 HDPA hdpaSubItems;
7002 ITEM_INFO *lpItem;
7003 LPVOID selectionMarkItem;
7004 LVITEMW item;
7005 int i;
7007 TRACE("(pfnCompare=%p, lParamSort=%lx)\n", pfnCompare, lParamSort);
7009 if (infoPtr->dwStyle & LVS_OWNERDATA) return FALSE;
7011 if (!infoPtr->hdpaItems) return FALSE;
7013 /* if there are 0 or 1 items, there is no need to sort */
7014 if (infoPtr->nItemCount < 2) return TRUE;
7016 if (infoPtr->nFocusedItem >= 0)
7018 hdpaSubItems = (HDPA)DPA_GetPtr(infoPtr->hdpaItems, infoPtr->nFocusedItem);
7019 lpItem = (ITEM_INFO *)DPA_GetPtr(hdpaSubItems, 0);
7020 if (lpItem) lpItem->state |= LVIS_FOCUSED;
7022 /* FIXME: go thorugh selected items and mark them so in lpItem->state */
7023 /* clear the lpItem->state for non-selected ones */
7024 /* remove the selection ranges */
7026 infoPtr->pfnCompare = pfnCompare;
7027 infoPtr->lParamSort = lParamSort;
7028 DPA_Sort(infoPtr->hdpaItems, LISTVIEW_CallBackCompare, (LPARAM)infoPtr->hwndSelf);
7030 /* Adjust selections and indices so that they are the way they should
7031 * be after the sort (otherwise, the list items move around, but
7032 * whatever is at the item's previous original position will be
7033 * selected instead)
7035 selectionMarkItem=(infoPtr->nSelectionMark>=0)?DPA_GetPtr(infoPtr->hdpaItems, infoPtr->nSelectionMark):NULL;
7036 for (i=0; i < infoPtr->nItemCount; i++)
7038 hdpaSubItems = (HDPA)DPA_GetPtr(infoPtr->hdpaItems, i);
7039 lpItem = (ITEM_INFO *)DPA_GetPtr(hdpaSubItems, 0);
7041 if (lpItem->state & LVIS_SELECTED)
7043 item.state = LVIS_SELECTED;
7044 item.stateMask = LVIS_SELECTED;
7045 LISTVIEW_SetItemState(infoPtr, i, &item);
7047 if (lpItem->state & LVIS_FOCUSED)
7049 infoPtr->nFocusedItem = i;
7050 lpItem->state &= ~LVIS_FOCUSED;
7053 if (selectionMarkItem != NULL)
7054 infoPtr->nSelectionMark = DPA_GetPtrIndex(infoPtr->hdpaItems, selectionMarkItem);
7055 /* I believe nHotItem should be left alone, see LISTVIEW_ShiftIndices */
7057 /* refresh the display */
7058 if (uView != LVS_ICON && uView != LVS_SMALLICON)
7059 LISTVIEW_InvalidateList(infoPtr);
7061 return TRUE;
7064 /***
7065 * DESCRIPTION:
7066 * Updates an items or rearranges the listview control.
7068 * PARAMETER(S):
7069 * [I] infoPtr : valid pointer to the listview structure
7070 * [I] nItem : item index
7072 * RETURN:
7073 * SUCCESS : TRUE
7074 * FAILURE : FALSE
7076 static BOOL LISTVIEW_Update(LISTVIEW_INFO *infoPtr, INT nItem)
7078 TRACE("(nItem=%d)\n", nItem);
7080 if (nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
7082 /* rearrange with default alignment style */
7083 if (is_autoarrange(infoPtr))
7084 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
7085 else
7086 LISTVIEW_InvalidateItem(infoPtr, nItem);
7088 return TRUE;
7092 /***
7093 * DESCRIPTION:
7094 * Creates the listview control.
7096 * PARAMETER(S):
7097 * [I] hwnd : window handle
7098 * [I] lpcs : the create parameters
7100 * RETURN:
7101 * Success: 0
7102 * Failure: -1
7104 static LRESULT LISTVIEW_Create(HWND hwnd, const CREATESTRUCTW *lpcs)
7106 LISTVIEW_INFO *infoPtr;
7107 UINT uView = lpcs->style & LVS_TYPEMASK;
7108 LOGFONTW logFont;
7110 TRACE("(lpcs=%p)\n", lpcs);
7112 /* initialize info pointer */
7113 infoPtr = (LISTVIEW_INFO *)COMCTL32_Alloc(sizeof(LISTVIEW_INFO));
7114 if (!infoPtr) return -1;
7116 SetWindowLongW(hwnd, 0, (LONG)infoPtr);
7118 infoPtr->hwndSelf = hwnd;
7119 infoPtr->dwStyle = lpcs->style;
7120 /* determine the type of structures to use */
7121 infoPtr->notifyFormat = SendMessageW(GetParent(infoPtr->hwndSelf), WM_NOTIFYFORMAT,
7122 (WPARAM)infoPtr->hwndSelf, (LPARAM)NF_QUERY);
7124 /* initialize color information */
7125 infoPtr->clrBk = CLR_NONE;
7126 infoPtr->clrText = comctl32_color.clrWindowText;
7127 infoPtr->clrTextBk = CLR_DEFAULT;
7128 LISTVIEW_SetBkColor(infoPtr, comctl32_color.clrWindow);
7130 /* set default values */
7131 infoPtr->nFocusedItem = -1;
7132 infoPtr->nSelectionMark = -1;
7133 infoPtr->nHotItem = -1;
7134 infoPtr->bRedraw = TRUE;
7135 infoPtr->bFirstPaint = TRUE;
7136 infoPtr->bNoItemMetrics = TRUE;
7137 infoPtr->bDoChangeNotify = TRUE;
7138 infoPtr->iconSpacing.cx = GetSystemMetrics(SM_CXICONSPACING);
7139 infoPtr->iconSpacing.cy = GetSystemMetrics(SM_CYICONSPACING);
7140 infoPtr->nEditLabelItem = -1;
7141 infoPtr->dwHoverTime = -1; /* default system hover time */
7143 /* get default font (icon title) */
7144 SystemParametersInfoW(SPI_GETICONTITLELOGFONT, 0, &logFont, 0);
7145 infoPtr->hDefaultFont = CreateFontIndirectW(&logFont);
7146 infoPtr->hFont = infoPtr->hDefaultFont;
7147 LISTVIEW_SaveTextMetrics(infoPtr);
7149 /* create header */
7150 infoPtr->hwndHeader = CreateWindowW(WC_HEADERW, NULL,
7151 WS_CHILD | HDS_HORZ | (DWORD)((LVS_NOSORTHEADER & lpcs->style)?0:HDS_BUTTONS),
7152 0, 0, 0, 0, hwnd, NULL,
7153 lpcs->hInstance, NULL);
7154 if (!infoPtr->hwndHeader) goto fail;
7156 /* set header unicode format */
7157 SendMessageW(infoPtr->hwndHeader, HDM_SETUNICODEFORMAT, (WPARAM)TRUE, (LPARAM)NULL);
7159 /* set header font */
7160 SendMessageW(infoPtr->hwndHeader, WM_SETFONT, (WPARAM)infoPtr->hFont, (LPARAM)TRUE);
7162 /* allocate memory for the data structure */
7163 if (!(infoPtr->selectionRanges = ranges_create(10))) goto fail;
7164 if (!(infoPtr->hdpaItems = DPA_Create(10))) goto fail;
7165 if (!(infoPtr->hdpaPosX = DPA_Create(10))) goto fail;
7166 if (!(infoPtr->hdpaPosY = DPA_Create(10))) goto fail;
7167 if (!(infoPtr->hdpaColumns = DPA_Create(10))) goto fail;
7169 /* initialize the icon sizes */
7170 set_icon_size(&infoPtr->iconSize, infoPtr->himlNormal, uView != LVS_ICON);
7171 set_icon_size(&infoPtr->iconStateSize, infoPtr->himlState, TRUE);
7173 /* init item size to avoid division by 0 */
7174 LISTVIEW_UpdateItemSize (infoPtr);
7176 if (uView == LVS_REPORT)
7178 if (!(LVS_NOCOLUMNHEADER & lpcs->style))
7180 ShowWindow(infoPtr->hwndHeader, SW_SHOWNORMAL);
7182 else
7184 /* set HDS_HIDDEN flag to hide the header bar */
7185 SetWindowLongW(infoPtr->hwndHeader, GWL_STYLE,
7186 GetWindowLongW(infoPtr->hwndHeader, GWL_STYLE) | HDS_HIDDEN);
7190 return 0;
7192 fail:
7193 DestroyWindow(infoPtr->hwndHeader);
7194 ranges_destroy(infoPtr->selectionRanges);
7195 DPA_Destroy(infoPtr->hdpaItems);
7196 DPA_Destroy(infoPtr->hdpaPosX);
7197 DPA_Destroy(infoPtr->hdpaPosY);
7198 DPA_Destroy(infoPtr->hdpaColumns);
7199 COMCTL32_Free(infoPtr);
7200 return -1;
7203 /***
7204 * DESCRIPTION:
7205 * Erases the background of the listview control.
7207 * PARAMETER(S):
7208 * [I] infoPtr : valid pointer to the listview structure
7209 * [I] hdc : device context handle
7211 * RETURN:
7212 * SUCCESS : TRUE
7213 * FAILURE : FALSE
7215 static inline BOOL LISTVIEW_EraseBkgnd(LISTVIEW_INFO *infoPtr, HDC hdc)
7217 RECT rc;
7219 TRACE("(hdc=%p)\n", hdc);
7221 if (!GetClipBox(hdc, &rc)) return FALSE;
7223 return LISTVIEW_FillBkgnd(infoPtr, hdc, &rc);
7227 /***
7228 * DESCRIPTION:
7229 * Helper function for LISTVIEW_[HV]Scroll *only*.
7230 * Performs vertical/horizontal scrolling by a give amount.
7232 * PARAMETER(S):
7233 * [I] infoPtr : valid pointer to the listview structure
7234 * [I] dx : amount of horizontal scroll
7235 * [I] dy : amount of vertical scroll
7237 static void scroll_list(LISTVIEW_INFO *infoPtr, INT dx, INT dy)
7239 /* now we can scroll the list */
7240 ScrollWindowEx(infoPtr->hwndSelf, dx, dy, &infoPtr->rcList,
7241 &infoPtr->rcList, 0, 0, SW_ERASE | SW_INVALIDATE);
7242 /* if we have focus, adjust rect */
7243 OffsetRect(&infoPtr->rcFocus, dx, dy);
7244 UpdateWindow(infoPtr->hwndSelf);
7247 /***
7248 * DESCRIPTION:
7249 * Performs vertical scrolling.
7251 * PARAMETER(S):
7252 * [I] infoPtr : valid pointer to the listview structure
7253 * [I] nScrollCode : scroll code
7254 * [I] nScrollDiff : units to scroll in SB_INTERNAL mode, 0 otherwise
7255 * [I] hScrollWnd : scrollbar control window handle
7257 * RETURN:
7258 * Zero
7260 * NOTES:
7261 * SB_LINEUP/SB_LINEDOWN:
7262 * for LVS_ICON, LVS_SMALLICON is 37 by experiment
7263 * for LVS_REPORT is 1 line
7264 * for LVS_LIST cannot occur
7267 static LRESULT LISTVIEW_VScroll(LISTVIEW_INFO *infoPtr, INT nScrollCode,
7268 INT nScrollDiff, HWND hScrollWnd)
7270 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
7271 INT nOldScrollPos, nNewScrollPos;
7272 SCROLLINFO scrollInfo;
7273 BOOL is_an_icon;
7275 TRACE("(nScrollCode=%d(%s), nScrollDiff=%d)\n", nScrollCode,
7276 debugscrollcode(nScrollCode), nScrollDiff);
7278 if (infoPtr->hwndEdit) SendMessageW(infoPtr->hwndEdit, WM_KILLFOCUS, 0, 0);
7280 scrollInfo.cbSize = sizeof(SCROLLINFO);
7281 scrollInfo.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_TRACKPOS;
7283 is_an_icon = ((uView == LVS_ICON) || (uView == LVS_SMALLICON));
7285 if (!GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo)) return 1;
7287 nOldScrollPos = scrollInfo.nPos;
7288 switch (nScrollCode)
7290 case SB_INTERNAL:
7291 break;
7293 case SB_LINEUP:
7294 nScrollDiff = (is_an_icon) ? -LISTVIEW_SCROLL_ICON_LINE_SIZE : -1;
7295 break;
7297 case SB_LINEDOWN:
7298 nScrollDiff = (is_an_icon) ? LISTVIEW_SCROLL_ICON_LINE_SIZE : 1;
7299 break;
7301 case SB_PAGEUP:
7302 nScrollDiff = -scrollInfo.nPage;
7303 break;
7305 case SB_PAGEDOWN:
7306 nScrollDiff = scrollInfo.nPage;
7307 break;
7309 case SB_THUMBPOSITION:
7310 case SB_THUMBTRACK:
7311 nScrollDiff = scrollInfo.nTrackPos - scrollInfo.nPos;
7312 break;
7314 default:
7315 nScrollDiff = 0;
7318 /* quit right away if pos isn't changing */
7319 if (nScrollDiff == 0) return 0;
7321 /* calculate new position, and handle overflows */
7322 nNewScrollPos = scrollInfo.nPos + nScrollDiff;
7323 if (nScrollDiff > 0) {
7324 if (nNewScrollPos < nOldScrollPos ||
7325 nNewScrollPos > scrollInfo.nMax)
7326 nNewScrollPos = scrollInfo.nMax;
7327 } else {
7328 if (nNewScrollPos > nOldScrollPos ||
7329 nNewScrollPos < scrollInfo.nMin)
7330 nNewScrollPos = scrollInfo.nMin;
7333 /* set the new position, and reread in case it changed */
7334 scrollInfo.fMask = SIF_POS;
7335 scrollInfo.nPos = nNewScrollPos;
7336 nNewScrollPos = SetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo, TRUE);
7338 /* carry on only if it really changed */
7339 if (nNewScrollPos == nOldScrollPos) return 0;
7341 /* now adjust to client coordinates */
7342 nScrollDiff = nOldScrollPos - nNewScrollPos;
7343 if (uView == LVS_REPORT) nScrollDiff *= infoPtr->nItemHeight;
7345 /* and scroll the window */
7346 scroll_list(infoPtr, 0, nScrollDiff);
7348 return 0;
7351 /***
7352 * DESCRIPTION:
7353 * Performs horizontal scrolling.
7355 * PARAMETER(S):
7356 * [I] infoPtr : valid pointer to the listview structure
7357 * [I] nScrollCode : scroll code
7358 * [I] nScrollDiff : units to scroll in SB_INTERNAL mode, 0 otherwise
7359 * [I] hScrollWnd : scrollbar control window handle
7361 * RETURN:
7362 * Zero
7364 * NOTES:
7365 * SB_LINELEFT/SB_LINERIGHT:
7366 * for LVS_ICON, LVS_SMALLICON 1 pixel
7367 * for LVS_REPORT is 1 pixel
7368 * for LVS_LIST is 1 column --> which is a 1 because the
7369 * scroll is based on columns not pixels
7372 static LRESULT LISTVIEW_HScroll(LISTVIEW_INFO *infoPtr, INT nScrollCode,
7373 INT nScrollDiff, HWND hScrollWnd)
7375 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
7376 INT nOldScrollPos, nNewScrollPos;
7377 SCROLLINFO scrollInfo;
7379 TRACE("(nScrollCode=%d(%s), nScrollDiff=%d)\n", nScrollCode,
7380 debugscrollcode(nScrollCode), nScrollDiff);
7382 if (infoPtr->hwndEdit) SendMessageW(infoPtr->hwndEdit, WM_KILLFOCUS, 0, 0);
7384 scrollInfo.cbSize = sizeof(SCROLLINFO);
7385 scrollInfo.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_TRACKPOS;
7387 if (!GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo)) return 1;
7389 nOldScrollPos = scrollInfo.nPos;
7391 switch (nScrollCode)
7393 case SB_INTERNAL:
7394 break;
7396 case SB_LINELEFT:
7397 nScrollDiff = -1;
7398 break;
7400 case SB_LINERIGHT:
7401 nScrollDiff = 1;
7402 break;
7404 case SB_PAGELEFT:
7405 nScrollDiff = -scrollInfo.nPage;
7406 break;
7408 case SB_PAGERIGHT:
7409 nScrollDiff = scrollInfo.nPage;
7410 break;
7412 case SB_THUMBPOSITION:
7413 case SB_THUMBTRACK:
7414 nScrollDiff = scrollInfo.nTrackPos - scrollInfo.nPos;
7415 break;
7417 default:
7418 nScrollDiff = 0;
7421 /* quit right away if pos isn't changing */
7422 if (nScrollDiff == 0) return 0;
7424 /* calculate new position, and handle overflows */
7425 nNewScrollPos = scrollInfo.nPos + nScrollDiff;
7426 if (nScrollDiff > 0) {
7427 if (nNewScrollPos < nOldScrollPos ||
7428 nNewScrollPos > scrollInfo.nMax)
7429 nNewScrollPos = scrollInfo.nMax;
7430 } else {
7431 if (nNewScrollPos > nOldScrollPos ||
7432 nNewScrollPos < scrollInfo.nMin)
7433 nNewScrollPos = scrollInfo.nMin;
7436 /* set the new position, and reread in case it changed */
7437 scrollInfo.fMask = SIF_POS;
7438 scrollInfo.nPos = nNewScrollPos;
7439 nNewScrollPos = SetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo, TRUE);
7441 /* carry on only if it really changed */
7442 if (nNewScrollPos == nOldScrollPos) return 0;
7444 if(uView == LVS_REPORT)
7445 LISTVIEW_UpdateHeaderSize(infoPtr, nNewScrollPos);
7447 /* now adjust to client coordinates */
7448 nScrollDiff = nOldScrollPos - nNewScrollPos;
7449 if (uView == LVS_LIST) nScrollDiff *= infoPtr->nItemWidth;
7451 /* and scroll the window */
7452 scroll_list(infoPtr, nScrollDiff, 0);
7454 return 0;
7457 static LRESULT LISTVIEW_MouseWheel(LISTVIEW_INFO *infoPtr, INT wheelDelta)
7459 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
7460 INT gcWheelDelta = 0;
7461 UINT pulScrollLines = 3;
7462 SCROLLINFO scrollInfo;
7464 TRACE("(wheelDelta=%d)\n", wheelDelta);
7466 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES,0, &pulScrollLines, 0);
7467 gcWheelDelta -= wheelDelta;
7469 scrollInfo.cbSize = sizeof(SCROLLINFO);
7470 scrollInfo.fMask = SIF_POS;
7472 switch(uView)
7474 case LVS_ICON:
7475 case LVS_SMALLICON:
7477 * listview should be scrolled by a multiple of 37 dependently on its dimension or its visible item number
7478 * should be fixed in the future.
7480 LISTVIEW_VScroll(infoPtr, SB_INTERNAL, (gcWheelDelta < 0) ?
7481 -LISTVIEW_SCROLL_ICON_LINE_SIZE : LISTVIEW_SCROLL_ICON_LINE_SIZE, 0);
7482 break;
7484 case LVS_REPORT:
7485 if (abs(gcWheelDelta) >= WHEEL_DELTA && pulScrollLines)
7487 int cLineScroll = min(LISTVIEW_GetCountPerColumn(infoPtr), pulScrollLines);
7488 cLineScroll *= (gcWheelDelta / WHEEL_DELTA);
7489 LISTVIEW_VScroll(infoPtr, SB_INTERNAL, cLineScroll, 0);
7491 break;
7493 case LVS_LIST:
7494 LISTVIEW_HScroll(infoPtr, (gcWheelDelta < 0) ? SB_LINELEFT : SB_LINERIGHT, 0, 0);
7495 break;
7497 return 0;
7500 /***
7501 * DESCRIPTION:
7502 * ???
7504 * PARAMETER(S):
7505 * [I] infoPtr : valid pointer to the listview structure
7506 * [I] nVirtualKey : virtual key
7507 * [I] lKeyData : key data
7509 * RETURN:
7510 * Zero
7512 static LRESULT LISTVIEW_KeyDown(LISTVIEW_INFO *infoPtr, INT nVirtualKey, LONG lKeyData)
7514 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
7515 INT nItem = -1;
7516 NMLVKEYDOWN nmKeyDown;
7518 TRACE("(nVirtualKey=%d, lKeyData=%ld)\n", nVirtualKey, lKeyData);
7520 /* send LVN_KEYDOWN notification */
7521 nmKeyDown.wVKey = nVirtualKey;
7522 nmKeyDown.flags = 0;
7523 notify_hdr(infoPtr, LVN_KEYDOWN, &nmKeyDown.hdr);
7525 switch (nVirtualKey)
7527 case VK_RETURN:
7528 if ((infoPtr->nItemCount > 0) && (infoPtr->nFocusedItem != -1))
7530 notify(infoPtr, NM_RETURN);
7531 notify(infoPtr, LVN_ITEMACTIVATE);
7533 break;
7535 case VK_HOME:
7536 if (infoPtr->nItemCount > 0)
7537 nItem = 0;
7538 break;
7540 case VK_END:
7541 if (infoPtr->nItemCount > 0)
7542 nItem = infoPtr->nItemCount - 1;
7543 break;
7545 case VK_LEFT:
7546 nItem = ListView_GetNextItem(infoPtr->hwndSelf, infoPtr->nFocusedItem, LVNI_TOLEFT);
7547 break;
7549 case VK_UP:
7550 nItem = ListView_GetNextItem(infoPtr->hwndSelf, infoPtr->nFocusedItem, LVNI_ABOVE);
7551 break;
7553 case VK_RIGHT:
7554 nItem = ListView_GetNextItem(infoPtr->hwndSelf, infoPtr->nFocusedItem, LVNI_TORIGHT);
7555 break;
7557 case VK_DOWN:
7558 nItem = ListView_GetNextItem(infoPtr->hwndSelf, infoPtr->nFocusedItem, LVNI_BELOW);
7559 break;
7561 case VK_PRIOR:
7562 if (uView == LVS_REPORT)
7563 nItem = infoPtr->nFocusedItem - LISTVIEW_GetCountPerColumn(infoPtr);
7564 else
7565 nItem = infoPtr->nFocusedItem - LISTVIEW_GetCountPerColumn(infoPtr)
7566 * LISTVIEW_GetCountPerRow(infoPtr);
7567 if(nItem < 0) nItem = 0;
7568 break;
7570 case VK_NEXT:
7571 if (uView == LVS_REPORT)
7572 nItem = infoPtr->nFocusedItem + LISTVIEW_GetCountPerColumn(infoPtr);
7573 else
7574 nItem = infoPtr->nFocusedItem + LISTVIEW_GetCountPerColumn(infoPtr)
7575 * LISTVIEW_GetCountPerRow(infoPtr);
7576 if(nItem >= infoPtr->nItemCount) nItem = infoPtr->nItemCount - 1;
7577 break;
7580 if ((nItem != -1) && (nItem != infoPtr->nFocusedItem))
7581 LISTVIEW_KeySelection(infoPtr, nItem);
7583 return 0;
7586 /***
7587 * DESCRIPTION:
7588 * Kills the focus.
7590 * PARAMETER(S):
7591 * [I] infoPtr : valid pointer to the listview structure
7593 * RETURN:
7594 * Zero
7596 static LRESULT LISTVIEW_KillFocus(LISTVIEW_INFO *infoPtr)
7598 TRACE("()\n");
7600 /* if we did not have the focus, there's nothing to do */
7601 if (!infoPtr->bFocus) return 0;
7603 /* send NM_KILLFOCUS notification */
7604 notify(infoPtr, NM_KILLFOCUS);
7606 /* if we have a focus rectagle, get rid of it */
7607 LISTVIEW_ShowFocusRect(infoPtr, FALSE);
7609 /* set window focus flag */
7610 infoPtr->bFocus = FALSE;
7612 /* invalidate the selected items before reseting focus flag */
7613 LISTVIEW_InvalidateSelectedItems(infoPtr);
7615 return 0;
7618 /***
7619 * DESCRIPTION:
7620 * Processes double click messages (left mouse button).
7622 * PARAMETER(S):
7623 * [I] infoPtr : valid pointer to the listview structure
7624 * [I] wKey : key flag
7625 * [I] pts : mouse coordinate
7627 * RETURN:
7628 * Zero
7630 static LRESULT LISTVIEW_LButtonDblClk(LISTVIEW_INFO *infoPtr, WORD wKey, POINTS pts)
7632 LVHITTESTINFO htInfo;
7634 TRACE("(key=%hu, X=%hu, Y=%hu)\n", wKey, pts.x, pts.y);
7636 /* send NM_RELEASEDCAPTURE notification */
7637 notify(infoPtr, NM_RELEASEDCAPTURE);
7639 htInfo.pt.x = pts.x;
7640 htInfo.pt.y = pts.y;
7642 /* send NM_DBLCLK notification */
7643 LISTVIEW_HitTest(infoPtr, &htInfo, TRUE, FALSE);
7644 notify_click(infoPtr, NM_DBLCLK, &htInfo);
7646 /* To send the LVN_ITEMACTIVATE, it must be on an Item */
7647 if(htInfo.iItem != -1) notify(infoPtr, LVN_ITEMACTIVATE);
7649 return 0;
7652 /***
7653 * DESCRIPTION:
7654 * Processes mouse down messages (left mouse button).
7656 * PARAMETER(S):
7657 * [I] infoPtr : valid pointer to the listview structure
7658 * [I] wKey : key flag
7659 * [I] pts : mouse coordinate
7661 * RETURN:
7662 * Zero
7664 static LRESULT LISTVIEW_LButtonDown(LISTVIEW_INFO *infoPtr, WORD wKey, POINTS pts)
7666 LVHITTESTINFO lvHitTestInfo;
7667 static BOOL bGroupSelect = TRUE;
7668 POINT pt = { pts.x, pts.y };
7669 INT nItem;
7671 TRACE("(key=%hu, X=%hu, Y=%hu)\n", wKey, pts.x, pts.y);
7673 /* send NM_RELEASEDCAPTURE notification */
7674 notify(infoPtr, NM_RELEASEDCAPTURE);
7676 if (!infoPtr->bFocus) SetFocus(infoPtr->hwndSelf);
7678 /* set left button down flag */
7679 infoPtr->bLButtonDown = TRUE;
7681 lvHitTestInfo.pt.x = pts.x;
7682 lvHitTestInfo.pt.y = pts.y;
7684 nItem = LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, TRUE);
7685 TRACE("at %s, nItem=%d\n", debugpoint(&pt), nItem);
7686 infoPtr->nEditLabelItem = -1;
7687 if ((nItem >= 0) && (nItem < infoPtr->nItemCount))
7689 if (infoPtr->dwStyle & LVS_SINGLESEL)
7691 if (LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED))
7692 infoPtr->nEditLabelItem = nItem;
7693 else
7694 LISTVIEW_SetSelection(infoPtr, nItem);
7696 else
7698 if ((wKey & MK_CONTROL) && (wKey & MK_SHIFT))
7700 if (bGroupSelect)
7702 LISTVIEW_AddGroupSelection(infoPtr, nItem);
7703 LISTVIEW_SetItemFocus(infoPtr, nItem);
7704 infoPtr->nSelectionMark = nItem;
7706 else
7708 LVITEMW item;
7710 item.state = LVIS_SELECTED | LVIS_FOCUSED;
7711 item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
7713 LISTVIEW_SetItemState(infoPtr,nItem,&item);
7714 infoPtr->nSelectionMark = nItem;
7717 else if (wKey & MK_CONTROL)
7719 LVITEMW item;
7721 bGroupSelect = (LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED) == 0);
7723 item.state = (bGroupSelect ? LVIS_SELECTED : 0) | LVIS_FOCUSED;
7724 item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
7725 LISTVIEW_SetItemState(infoPtr, nItem, &item);
7726 infoPtr->nSelectionMark = nItem;
7728 else if (wKey & MK_SHIFT)
7730 LISTVIEW_SetGroupSelection(infoPtr, nItem);
7732 else
7734 if (LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED))
7735 infoPtr->nEditLabelItem = nItem;
7737 /* set selection (clears other pre-existing selections) */
7738 LISTVIEW_SetSelection(infoPtr, nItem);
7742 else
7744 /* remove all selections */
7745 LISTVIEW_DeselectAll(infoPtr);
7748 return 0;
7751 /***
7752 * DESCRIPTION:
7753 * Processes mouse up messages (left mouse button).
7755 * PARAMETER(S):
7756 * [I] infoPtr : valid pointer to the listview structure
7757 * [I] wKey : key flag
7758 * [I] pts : mouse coordinate
7760 * RETURN:
7761 * Zero
7763 static LRESULT LISTVIEW_LButtonUp(LISTVIEW_INFO *infoPtr, WORD wKey, POINTS pts)
7765 LVHITTESTINFO lvHitTestInfo;
7767 TRACE("(key=%hu, X=%hu, Y=%hu)\n", wKey, pts.x, pts.y);
7769 if (!infoPtr->bLButtonDown) return 0;
7771 lvHitTestInfo.pt.x = pts.x;
7772 lvHitTestInfo.pt.y = pts.y;
7774 /* send NM_CLICK notification */
7775 LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, FALSE);
7776 notify_click(infoPtr, NM_CLICK, &lvHitTestInfo);
7778 /* set left button flag */
7779 infoPtr->bLButtonDown = FALSE;
7781 /* if we clicked on a selected item, edit the label */
7782 if(lvHitTestInfo.iItem == infoPtr->nEditLabelItem && (lvHitTestInfo.flags & LVHT_ONITEMLABEL))
7783 LISTVIEW_EditLabelT(infoPtr, lvHitTestInfo.iItem, TRUE);
7785 return 0;
7788 /***
7789 * DESCRIPTION:
7790 * Destroys the listview control (called after WM_DESTROY).
7792 * PARAMETER(S):
7793 * [I] infoPtr : valid pointer to the listview structure
7795 * RETURN:
7796 * Zero
7798 static LRESULT LISTVIEW_NCDestroy(LISTVIEW_INFO *infoPtr)
7800 TRACE("()\n");
7802 /* delete all items */
7803 LISTVIEW_DeleteAllItems(infoPtr);
7805 /* destroy data structure */
7806 DPA_Destroy(infoPtr->hdpaItems);
7807 DPA_Destroy(infoPtr->hdpaPosX);
7808 DPA_Destroy(infoPtr->hdpaPosY);
7809 DPA_Destroy(infoPtr->hdpaColumns);
7810 ranges_destroy(infoPtr->selectionRanges);
7812 /* destroy image lists */
7813 if (!(infoPtr->dwStyle & LVS_SHAREIMAGELISTS))
7815 if (infoPtr->himlNormal)
7816 ImageList_Destroy(infoPtr->himlNormal);
7817 if (infoPtr->himlSmall)
7818 ImageList_Destroy(infoPtr->himlSmall);
7819 if (infoPtr->himlState)
7820 ImageList_Destroy(infoPtr->himlState);
7823 /* destroy font, bkgnd brush */
7824 infoPtr->hFont = 0;
7825 if (infoPtr->hDefaultFont) DeleteObject(infoPtr->hDefaultFont);
7826 if (infoPtr->clrBk != CLR_NONE) DeleteObject(infoPtr->hBkBrush);
7828 /* free listview info pointer*/
7829 COMCTL32_Free(infoPtr);
7831 SetWindowLongW(infoPtr->hwndSelf, 0, 0);
7832 return 0;
7835 /***
7836 * DESCRIPTION:
7837 * Handles notifications from header.
7839 * PARAMETER(S):
7840 * [I] infoPtr : valid pointer to the listview structure
7841 * [I] nCtrlId : control identifier
7842 * [I] lpnmh : notification information
7844 * RETURN:
7845 * Zero
7847 static LRESULT LISTVIEW_HeaderNotification(LISTVIEW_INFO *infoPtr, const NMHEADERW *lpnmh)
7849 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
7851 TRACE("(lpnmh=%p)\n", lpnmh);
7853 if (!lpnmh || lpnmh->iItem < 0 || lpnmh->iItem >= infoPtr->hdpaColumns->nItemCount) return 0;
7855 switch (lpnmh->hdr.code)
7857 case HDN_TRACKW:
7858 case HDN_TRACKA:
7859 case HDN_ITEMCHANGEDW:
7860 case HDN_ITEMCHANGEDA:
7862 COLUMN_INFO *lpColumnInfo;
7863 INT dx, cxy;
7865 if (!lpnmh->pitem || !(lpnmh->pitem->mask & HDI_WIDTH))
7867 HDITEMW hdi;
7869 hdi.mask = HDI_WIDTH;
7870 if (!Header_GetItemW(infoPtr->hwndHeader, lpnmh->iItem, (LPARAM)&hdi)) return 0;
7871 cxy = hdi.cxy;
7873 else
7874 cxy = lpnmh->pitem->cxy;
7876 /* determine how much we change since the last know position */
7877 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, lpnmh->iItem);
7878 dx = cxy - (lpColumnInfo->rcHeader.right - lpColumnInfo->rcHeader.left);
7879 if (dx != 0)
7881 RECT rcCol = lpColumnInfo->rcHeader;
7883 lpColumnInfo->rcHeader.right += dx;
7884 LISTVIEW_ScrollColumns(infoPtr, lpnmh->iItem + 1, dx);
7885 if (uView == LVS_REPORT && is_redrawing(infoPtr))
7887 /* this trick works for left aligned columns only */
7888 if ((lpColumnInfo->fmt & LVCFMT_JUSTIFYMASK) == LVCFMT_LEFT)
7890 rcCol.right = min (rcCol.right, lpColumnInfo->rcHeader.right);
7891 rcCol.left = max (rcCol.left, rcCol.right - 3 * infoPtr->ntmAveCharWidth);
7893 rcCol.top = infoPtr->rcList.top;
7894 rcCol.bottom = infoPtr->rcList.bottom;
7895 LISTVIEW_InvalidateRect(infoPtr, &rcCol);
7899 break;
7901 case HDN_ITEMCLICKW:
7902 case HDN_ITEMCLICKA:
7904 /* Handle sorting by Header Column */
7905 NMLISTVIEW nmlv;
7907 ZeroMemory(&nmlv, sizeof(NMLISTVIEW));
7908 nmlv.iItem = -1;
7909 nmlv.iSubItem = lpnmh->iItem;
7910 notify_listview(infoPtr, LVN_COLUMNCLICK, &nmlv);
7912 break;
7915 return 0;
7918 /***
7919 * DESCRIPTION:
7920 * Determines the type of structure to use.
7922 * PARAMETER(S):
7923 * [I] infoPtr : valid pointer to the listview structureof the sender
7924 * [I] hwndFrom : listview window handle
7925 * [I] nCommand : command specifying the nature of the WM_NOTIFYFORMAT
7927 * RETURN:
7928 * Zero
7930 static LRESULT LISTVIEW_NotifyFormat(LISTVIEW_INFO *infoPtr, HWND hwndFrom, INT nCommand)
7932 TRACE("(hwndFrom=%p, nCommand=%d)\n", hwndFrom, nCommand);
7934 if (nCommand != NF_REQUERY) return 0;
7936 infoPtr->notifyFormat = SendMessageW(hwndFrom, WM_NOTIFYFORMAT, (WPARAM)infoPtr->hwndSelf, NF_QUERY);
7938 return 0;
7941 /***
7942 * DESCRIPTION:
7943 * Paints/Repaints the listview control.
7945 * PARAMETER(S):
7946 * [I] infoPtr : valid pointer to the listview structure
7947 * [I] hdc : device context handle
7949 * RETURN:
7950 * Zero
7952 static LRESULT LISTVIEW_Paint(LISTVIEW_INFO *infoPtr, HDC hdc)
7954 TRACE("(hdc=%p)\n", hdc);
7956 infoPtr->bFirstPaint = FALSE;
7957 if (infoPtr->bNoItemMetrics && infoPtr->nItemCount)
7959 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
7961 infoPtr->bNoItemMetrics = FALSE;
7962 LISTVIEW_UpdateItemSize(infoPtr);
7963 if (uView == LVS_ICON || uView == LVS_SMALLICON)
7964 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
7965 LISTVIEW_UpdateScroll(infoPtr);
7967 if (hdc)
7968 LISTVIEW_Refresh(infoPtr, hdc);
7969 else
7971 PAINTSTRUCT ps;
7973 hdc = BeginPaint(infoPtr->hwndSelf, &ps);
7974 if (!hdc) return 1;
7975 if (ps.fErase) LISTVIEW_FillBkgnd(infoPtr, hdc, &ps.rcPaint);
7976 LISTVIEW_Refresh(infoPtr, hdc);
7977 EndPaint(infoPtr->hwndSelf, &ps);
7980 return 0;
7983 /***
7984 * DESCRIPTION:
7985 * Processes double click messages (right mouse button).
7987 * PARAMETER(S):
7988 * [I] infoPtr : valid pointer to the listview structure
7989 * [I] wKey : key flag
7990 * [I] pts : mouse coordinate
7992 * RETURN:
7993 * Zero
7995 static LRESULT LISTVIEW_RButtonDblClk(LISTVIEW_INFO *infoPtr, WORD wKey, POINTS pts)
7997 LVHITTESTINFO lvHitTestInfo;
7999 TRACE("(key=%hu,X=%hu,Y=%hu)\n", wKey, pts.x, pts.y);
8001 /* send NM_RELEASEDCAPTURE notification */
8002 notify(infoPtr, NM_RELEASEDCAPTURE);
8004 /* send NM_RDBLCLK notification */
8005 lvHitTestInfo.pt.x = pts.x;
8006 lvHitTestInfo.pt.y = pts.y;
8007 LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, FALSE);
8008 notify_click(infoPtr, NM_RDBLCLK, &lvHitTestInfo);
8010 return 0;
8013 /***
8014 * DESCRIPTION:
8015 * Processes mouse down messages (right mouse button).
8017 * PARAMETER(S):
8018 * [I] infoPtr : valid pointer to the listview structure
8019 * [I] wKey : key flag
8020 * [I] pts : mouse coordinate
8022 * RETURN:
8023 * Zero
8025 static LRESULT LISTVIEW_RButtonDown(LISTVIEW_INFO *infoPtr, WORD wKey, POINTS pts)
8027 LVHITTESTINFO lvHitTestInfo;
8028 INT nItem;
8030 TRACE("(key=%hu,X=%hu,Y=%hu)\n", wKey, pts.x, pts.y);
8032 /* send NM_RELEASEDCAPTURE notification */
8033 notify(infoPtr, NM_RELEASEDCAPTURE);
8035 /* make sure the listview control window has the focus */
8036 if (!infoPtr->bFocus) SetFocus(infoPtr->hwndSelf);
8038 /* set right button down flag */
8039 infoPtr->bRButtonDown = TRUE;
8041 /* determine the index of the selected item */
8042 lvHitTestInfo.pt.x = pts.x;
8043 lvHitTestInfo.pt.y = pts.y;
8044 nItem = LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, TRUE);
8046 if ((nItem >= 0) && (nItem < infoPtr->nItemCount))
8048 LISTVIEW_SetItemFocus(infoPtr, nItem);
8049 if (!((wKey & MK_SHIFT) || (wKey & MK_CONTROL)) &&
8050 !LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED))
8051 LISTVIEW_SetSelection(infoPtr, nItem);
8053 else
8055 LISTVIEW_DeselectAll(infoPtr);
8058 return 0;
8061 /***
8062 * DESCRIPTION:
8063 * Processes mouse up messages (right mouse button).
8065 * PARAMETER(S):
8066 * [I] infoPtr : valid pointer to the listview structure
8067 * [I] wKey : key flag
8068 * [I] pts : mouse coordinate
8070 * RETURN:
8071 * Zero
8073 static LRESULT LISTVIEW_RButtonUp(LISTVIEW_INFO *infoPtr, WORD wKey, POINTS pts)
8075 LVHITTESTINFO lvHitTestInfo;
8076 POINT pt;
8078 TRACE("(key=%hu,X=%hu,Y=%hu)\n", wKey, pts.x, pts.y);
8080 if (!infoPtr->bRButtonDown) return 0;
8082 /* set button flag */
8083 infoPtr->bRButtonDown = FALSE;
8085 /* Send NM_RClICK notification */
8086 lvHitTestInfo.pt.x = pts.x;
8087 lvHitTestInfo.pt.y = pts.y;
8088 LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, FALSE);
8089 notify_click(infoPtr, NM_RCLICK, &lvHitTestInfo);
8091 /* Change to screen coordinate for WM_CONTEXTMENU */
8092 pt = lvHitTestInfo.pt;
8093 ClientToScreen(infoPtr->hwndSelf, &pt);
8095 /* Send a WM_CONTEXTMENU message in response to the RBUTTONUP */
8096 SendMessageW(infoPtr->hwndSelf, WM_CONTEXTMENU,
8097 (WPARAM)infoPtr->hwndSelf, MAKELPARAM(pt.x, pt.y));
8099 return 0;
8103 /***
8104 * DESCRIPTION:
8105 * Sets the cursor.
8107 * PARAMETER(S):
8108 * [I] infoPtr : valid pointer to the listview structure
8109 * [I] hwnd : window handle of window containing the cursor
8110 * [I] nHittest : hit-test code
8111 * [I] wMouseMsg : ideintifier of the mouse message
8113 * RETURN:
8114 * TRUE if cursor is set
8115 * FALSE otherwise
8117 static BOOL LISTVIEW_SetCursor(LISTVIEW_INFO *infoPtr, HWND hwnd, UINT nHittest, UINT wMouseMsg)
8119 LVHITTESTINFO lvHitTestInfo;
8121 if(!(infoPtr->dwLvExStyle & LVS_EX_TRACKSELECT)) return FALSE;
8123 if(!infoPtr->hHotCursor) return FALSE;
8125 GetCursorPos(&lvHitTestInfo.pt);
8126 if (LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, FALSE, FALSE) < 0) return FALSE;
8128 SetCursor(infoPtr->hHotCursor);
8130 return TRUE;
8133 /***
8134 * DESCRIPTION:
8135 * Sets the focus.
8137 * PARAMETER(S):
8138 * [I] infoPtr : valid pointer to the listview structure
8139 * [I] hwndLoseFocus : handle of previously focused window
8141 * RETURN:
8142 * Zero
8144 static LRESULT LISTVIEW_SetFocus(LISTVIEW_INFO *infoPtr, HWND hwndLoseFocus)
8146 TRACE("(hwndLoseFocus=%p)\n", hwndLoseFocus);
8148 /* if we have the focus already, there's nothing to do */
8149 if (infoPtr->bFocus) return 0;
8151 /* send NM_SETFOCUS notification */
8152 notify(infoPtr, NM_SETFOCUS);
8154 /* set window focus flag */
8155 infoPtr->bFocus = TRUE;
8157 /* put the focus rect back on */
8158 LISTVIEW_ShowFocusRect(infoPtr, TRUE);
8160 /* redraw all visible selected items */
8161 LISTVIEW_InvalidateSelectedItems(infoPtr);
8163 return 0;
8166 /***
8167 * DESCRIPTION:
8168 * Sets the font.
8170 * PARAMETER(S):
8171 * [I] infoPtr : valid pointer to the listview structure
8172 * [I] fRedraw : font handle
8173 * [I] fRedraw : redraw flag
8175 * RETURN:
8176 * Zero
8178 static LRESULT LISTVIEW_SetFont(LISTVIEW_INFO *infoPtr, HFONT hFont, WORD fRedraw)
8180 HFONT oldFont = infoPtr->hFont;
8182 TRACE("(hfont=%p,redraw=%hu)\n", hFont, fRedraw);
8184 infoPtr->hFont = hFont ? hFont : infoPtr->hDefaultFont;
8185 if (infoPtr->hFont == oldFont) return 0;
8187 LISTVIEW_SaveTextMetrics(infoPtr);
8189 if ((infoPtr->dwStyle & LVS_TYPEMASK) == LVS_REPORT)
8190 SendMessageW(infoPtr->hwndHeader, WM_SETFONT, (WPARAM)hFont, MAKELPARAM(fRedraw, 0));
8192 if (fRedraw) LISTVIEW_InvalidateList(infoPtr);
8194 return 0;
8197 /***
8198 * DESCRIPTION:
8199 * Message handling for WM_SETREDRAW.
8200 * For the Listview, it invalidates the entire window (the doc specifies otherwise)
8202 * PARAMETER(S):
8203 * [I] infoPtr : valid pointer to the listview structure
8204 * [I] bRedraw: state of redraw flag
8206 * RETURN:
8207 * DefWinProc return value
8209 static LRESULT LISTVIEW_SetRedraw(LISTVIEW_INFO *infoPtr, BOOL bRedraw)
8211 TRACE("infoPtr->bRedraw=%d, bRedraw=%d\n", infoPtr->bRedraw, bRedraw);
8213 /* we can not use straight equality here because _any_ non-zero value is TRUE */
8214 if ((infoPtr->bRedraw && bRedraw) || (!infoPtr->bRedraw && !bRedraw)) return 0;
8216 infoPtr->bRedraw = bRedraw;
8218 if(!bRedraw) return 0;
8220 if (is_autoarrange(infoPtr))
8221 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
8222 LISTVIEW_UpdateScroll(infoPtr);
8224 /* despite what the WM_SETREDRAW docs says, apps expect us
8225 * to invalidate the listview here... stupid! */
8226 LISTVIEW_InvalidateList(infoPtr);
8228 return 0;
8231 /***
8232 * DESCRIPTION:
8233 * Resizes the listview control. This function processes WM_SIZE
8234 * messages. At this time, the width and height are not used.
8236 * PARAMETER(S):
8237 * [I] infoPtr : valid pointer to the listview structure
8238 * [I] Width : new width
8239 * [I] Height : new height
8241 * RETURN:
8242 * Zero
8244 static LRESULT LISTVIEW_Size(LISTVIEW_INFO *infoPtr, int Width, int Height)
8246 RECT rcOld = infoPtr->rcList;
8248 TRACE("(width=%d, height=%d)\n", Width, Height);
8250 LISTVIEW_UpdateSize(infoPtr);
8251 if (EqualRect(&rcOld, &infoPtr->rcList)) return 0;
8253 /* do not bother with display related stuff if we're not redrawing */
8254 if (!is_redrawing(infoPtr)) return 0;
8256 if (is_autoarrange(infoPtr))
8257 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
8259 LISTVIEW_UpdateScroll(infoPtr);
8261 /* refresh all only for lists whose height changed significantly */
8262 if ((infoPtr->dwStyle & LVS_TYPEMASK) == LVS_LIST &&
8263 (rcOld.bottom - rcOld.top) / infoPtr->nItemHeight !=
8264 (infoPtr->rcList.bottom - infoPtr->rcList.top) / infoPtr->nItemHeight)
8265 LISTVIEW_InvalidateList(infoPtr);
8267 return 0;
8270 /***
8271 * DESCRIPTION:
8272 * Sets the size information.
8274 * PARAMETER(S):
8275 * [I] infoPtr : valid pointer to the listview structure
8277 * RETURN:
8278 * None
8280 static void LISTVIEW_UpdateSize(LISTVIEW_INFO *infoPtr)
8282 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
8284 TRACE("uView=%d, rcList(old)=%s\n", uView, debugrect(&infoPtr->rcList));
8286 GetClientRect(infoPtr->hwndSelf, &infoPtr->rcList);
8288 if (uView == LVS_LIST)
8290 /* Apparently the "LIST" style is supposed to have the same
8291 * number of items in a column even if there is no scroll bar.
8292 * Since if a scroll bar already exists then the bottom is already
8293 * reduced, only reduce if the scroll bar does not currently exist.
8294 * The "2" is there to mimic the native control. I think it may be
8295 * related to either padding or edges. (GLA 7/2002)
8297 if (!(infoPtr->dwStyle & WS_HSCROLL))
8298 infoPtr->rcList.bottom -= GetSystemMetrics(SM_CYHSCROLL);
8299 infoPtr->rcList.bottom = max (infoPtr->rcList.bottom - 2, 0);
8301 else if (uView == LVS_REPORT && !(infoPtr->dwStyle & LVS_NOCOLUMNHEADER))
8303 HDLAYOUT hl;
8304 WINDOWPOS wp;
8306 hl.prc = &infoPtr->rcList;
8307 hl.pwpos = &wp;
8308 Header_Layout(infoPtr->hwndHeader, &hl);
8310 SetWindowPos(wp.hwnd, wp.hwndInsertAfter, wp.x, wp.y, wp.cx, wp.cy, wp.flags);
8312 infoPtr->rcList.top = max(wp.cy, 0);
8315 TRACE(" rcList=%s\n", debugrect(&infoPtr->rcList));
8318 /***
8319 * DESCRIPTION:
8320 * Processes WM_STYLECHANGED messages.
8322 * PARAMETER(S):
8323 * [I] infoPtr : valid pointer to the listview structure
8324 * [I] wStyleType : window style type (normal or extended)
8325 * [I] lpss : window style information
8327 * RETURN:
8328 * Zero
8330 static INT LISTVIEW_StyleChanged(LISTVIEW_INFO *infoPtr, WPARAM wStyleType,
8331 const STYLESTRUCT *lpss)
8333 UINT uNewView = lpss->styleNew & LVS_TYPEMASK;
8334 UINT uOldView = lpss->styleOld & LVS_TYPEMASK;
8336 TRACE("(styletype=%x, styleOld=0x%08lx, styleNew=0x%08lx)\n",
8337 wStyleType, lpss->styleOld, lpss->styleNew);
8339 if (wStyleType != GWL_STYLE) return 0;
8341 /* FIXME: if LVS_NOSORTHEADER changed, update header */
8342 /* what if LVS_OWNERDATA changed? */
8343 /* or LVS_SINGLESEL */
8344 /* or LVS_SORT{AS,DES}CENDING */
8346 infoPtr->dwStyle = lpss->styleNew;
8348 if (((lpss->styleOld & WS_HSCROLL) != 0)&&
8349 ((lpss->styleNew & WS_HSCROLL) == 0))
8350 ShowScrollBar(infoPtr->hwndSelf, SB_HORZ, FALSE);
8352 if (((lpss->styleOld & WS_VSCROLL) != 0)&&
8353 ((lpss->styleNew & WS_VSCROLL) == 0))
8354 ShowScrollBar(infoPtr->hwndSelf, SB_VERT, FALSE);
8356 if (uNewView != uOldView)
8358 SIZE oldIconSize = infoPtr->iconSize;
8359 HIMAGELIST himl;
8361 SendMessageW(infoPtr->hwndEdit, WM_KILLFOCUS, 0, 0);
8362 ShowWindow(infoPtr->hwndHeader, SW_HIDE);
8364 ShowScrollBar(infoPtr->hwndSelf, SB_BOTH, FALSE);
8365 SetRectEmpty(&infoPtr->rcFocus);
8367 himl = (uNewView == LVS_ICON ? infoPtr->himlNormal : infoPtr->himlSmall);
8368 set_icon_size(&infoPtr->iconSize, himl, uNewView != LVS_ICON);
8370 if (uNewView == LVS_ICON)
8372 if ((infoPtr->iconSize.cx != oldIconSize.cx) || (infoPtr->iconSize.cy != oldIconSize.cy))
8374 TRACE("icon old size=(%ld,%ld), new size=(%ld,%ld)\n",
8375 oldIconSize.cx, oldIconSize.cy, infoPtr->iconSize.cx, infoPtr->iconSize.cy);
8376 LISTVIEW_SetIconSpacing(infoPtr, 0, 0);
8379 else if (uNewView == LVS_REPORT)
8381 HDLAYOUT hl;
8382 WINDOWPOS wp;
8384 hl.prc = &infoPtr->rcList;
8385 hl.pwpos = &wp;
8386 Header_Layout(infoPtr->hwndHeader, &hl);
8387 SetWindowPos(infoPtr->hwndHeader, infoPtr->hwndSelf, wp.x, wp.y, wp.cx, wp.cy, wp.flags);
8390 LISTVIEW_UpdateItemSize(infoPtr);
8393 if (uNewView == LVS_REPORT)
8394 ShowWindow(infoPtr->hwndHeader, (lpss->styleNew & LVS_NOCOLUMNHEADER) ? SW_HIDE : SW_SHOWNORMAL);
8396 if ( (uNewView == LVS_ICON || uNewView == LVS_SMALLICON) &&
8397 (uNewView != uOldView || ((lpss->styleNew ^ lpss->styleOld) & LVS_ALIGNMASK)) )
8398 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
8400 /* update the size of the client area */
8401 LISTVIEW_UpdateSize(infoPtr);
8403 /* add scrollbars if needed */
8404 LISTVIEW_UpdateScroll(infoPtr);
8406 /* invalidate client area + erase background */
8407 LISTVIEW_InvalidateList(infoPtr);
8409 return 0;
8412 /***
8413 * DESCRIPTION:
8414 * Window procedure of the listview control.
8417 static LRESULT WINAPI
8418 LISTVIEW_WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
8420 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)GetWindowLongW(hwnd, 0);
8422 TRACE("(uMsg=%x wParam=%x lParam=%lx)\n", uMsg, wParam, lParam);
8424 if (!infoPtr && (uMsg != WM_CREATE))
8425 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
8427 if (infoPtr)
8429 infoPtr->dwStyle = GetWindowLongW(hwnd, GWL_STYLE);
8432 switch (uMsg)
8434 case LVM_APPROXIMATEVIEWRECT:
8435 return LISTVIEW_ApproximateViewRect(infoPtr, (INT)wParam,
8436 LOWORD(lParam), HIWORD(lParam));
8437 case LVM_ARRANGE:
8438 return LISTVIEW_Arrange(infoPtr, (INT)wParam);
8440 /* case LVM_CANCELEDITLABEL: */
8442 /* case LVM_CREATEDRAGIMAGE: */
8444 case LVM_DELETEALLITEMS:
8445 return LISTVIEW_DeleteAllItems(infoPtr);
8447 case LVM_DELETECOLUMN:
8448 return LISTVIEW_DeleteColumn(infoPtr, (INT)wParam);
8450 case LVM_DELETEITEM:
8451 return LISTVIEW_DeleteItem(infoPtr, (INT)wParam);
8453 case LVM_EDITLABELW:
8454 return (LRESULT)LISTVIEW_EditLabelT(infoPtr, (INT)wParam, TRUE);
8456 case LVM_EDITLABELA:
8457 return (LRESULT)LISTVIEW_EditLabelT(infoPtr, (INT)wParam, FALSE);
8459 /* case LVM_ENABLEGROUPVIEW: */
8461 case LVM_ENSUREVISIBLE:
8462 return LISTVIEW_EnsureVisible(infoPtr, (INT)wParam, (BOOL)lParam);
8464 case LVM_FINDITEMW:
8465 return LISTVIEW_FindItemW(infoPtr, (INT)wParam, (LPLVFINDINFOW)lParam);
8467 case LVM_FINDITEMA:
8468 return LISTVIEW_FindItemA(infoPtr, (INT)wParam, (LPLVFINDINFOA)lParam);
8470 case LVM_GETBKCOLOR:
8471 return infoPtr->clrBk;
8473 /* case LVM_GETBKIMAGE: */
8475 case LVM_GETCALLBACKMASK:
8476 return infoPtr->uCallbackMask;
8478 case LVM_GETCOLUMNA:
8479 return LISTVIEW_GetColumnT(infoPtr, (INT)wParam, (LPLVCOLUMNW)lParam, FALSE);
8481 case LVM_GETCOLUMNW:
8482 return LISTVIEW_GetColumnT(infoPtr, (INT)wParam, (LPLVCOLUMNW)lParam, TRUE);
8484 case LVM_GETCOLUMNORDERARRAY:
8485 return LISTVIEW_GetColumnOrderArray(infoPtr, (INT)wParam, (LPINT)lParam);
8487 case LVM_GETCOLUMNWIDTH:
8488 return LISTVIEW_GetColumnWidth(infoPtr, (INT)wParam);
8490 case LVM_GETCOUNTPERPAGE:
8491 return LISTVIEW_GetCountPerPage(infoPtr);
8493 case LVM_GETEDITCONTROL:
8494 return (LRESULT)infoPtr->hwndEdit;
8496 case LVM_GETEXTENDEDLISTVIEWSTYLE:
8497 return infoPtr->dwLvExStyle;
8499 /* case LVM_GETGROUPINFO: */
8501 /* case LVM_GETGROUPMETRICS: */
8503 case LVM_GETHEADER:
8504 return (LRESULT)infoPtr->hwndHeader;
8506 case LVM_GETHOTCURSOR:
8507 return (LRESULT)infoPtr->hHotCursor;
8509 case LVM_GETHOTITEM:
8510 return infoPtr->nHotItem;
8512 case LVM_GETHOVERTIME:
8513 return infoPtr->dwHoverTime;
8515 case LVM_GETIMAGELIST:
8516 return (LRESULT)LISTVIEW_GetImageList(infoPtr, (INT)wParam);
8518 /* case LVM_GETINSERTMARK: */
8520 /* case LVM_GETINSERTMARKCOLOR: */
8522 /* case LVM_GETINSERTMARKRECT: */
8524 case LVM_GETISEARCHSTRINGA:
8525 case LVM_GETISEARCHSTRINGW:
8526 FIXME("LVM_GETISEARCHSTRING: unimplemented\n");
8527 return FALSE;
8529 case LVM_GETITEMA:
8530 return LISTVIEW_GetItemExtT(infoPtr, (LPLVITEMW)lParam, FALSE);
8532 case LVM_GETITEMW:
8533 return LISTVIEW_GetItemExtT(infoPtr, (LPLVITEMW)lParam, TRUE);
8535 case LVM_GETITEMCOUNT:
8536 return infoPtr->nItemCount;
8538 case LVM_GETITEMPOSITION:
8539 return LISTVIEW_GetItemPosition(infoPtr, (INT)wParam, (LPPOINT)lParam);
8541 case LVM_GETITEMRECT:
8542 return LISTVIEW_GetItemRect(infoPtr, (INT)wParam, (LPRECT)lParam);
8544 case LVM_GETITEMSPACING:
8545 return LISTVIEW_GetItemSpacing(infoPtr, (BOOL)wParam);
8547 case LVM_GETITEMSTATE:
8548 return LISTVIEW_GetItemState(infoPtr, (INT)wParam, (UINT)lParam);
8550 case LVM_GETITEMTEXTA:
8551 return LISTVIEW_GetItemTextT(infoPtr, (INT)wParam, (LPLVITEMW)lParam, FALSE);
8553 case LVM_GETITEMTEXTW:
8554 return LISTVIEW_GetItemTextT(infoPtr, (INT)wParam, (LPLVITEMW)lParam, TRUE);
8556 case LVM_GETNEXTITEM:
8557 return LISTVIEW_GetNextItem(infoPtr, (INT)wParam, LOWORD(lParam));
8559 case LVM_GETNUMBEROFWORKAREAS:
8560 FIXME("LVM_GETNUMBEROFWORKAREAS: unimplemented\n");
8561 return 1;
8563 case LVM_GETORIGIN:
8564 if (!lParam) return FALSE;
8565 LISTVIEW_GetOrigin(infoPtr, (LPPOINT)lParam);
8566 return TRUE;
8568 /* case LVM_GETOUTLINECOLOR: */
8570 /* case LVM_GETSELECTEDCOLUMN: */
8572 case LVM_GETSELECTEDCOUNT:
8573 return LISTVIEW_GetSelectedCount(infoPtr);
8575 case LVM_GETSELECTIONMARK:
8576 return infoPtr->nSelectionMark;
8578 case LVM_GETSTRINGWIDTHA:
8579 return LISTVIEW_GetStringWidthT(infoPtr, (LPCWSTR)lParam, FALSE);
8581 case LVM_GETSTRINGWIDTHW:
8582 return LISTVIEW_GetStringWidthT(infoPtr, (LPCWSTR)lParam, TRUE);
8584 case LVM_GETSUBITEMRECT:
8585 return LISTVIEW_GetSubItemRect(infoPtr, (UINT)wParam, (LPRECT)lParam);
8587 case LVM_GETTEXTBKCOLOR:
8588 return infoPtr->clrTextBk;
8590 case LVM_GETTEXTCOLOR:
8591 return infoPtr->clrText;
8593 /* case LVM_GETTILEINFO: */
8595 /* case LVM_GETTILEVIEWINFO: */
8597 case LVM_GETTOOLTIPS:
8598 FIXME("LVM_GETTOOLTIPS: unimplemented\n");
8599 return FALSE;
8601 case LVM_GETTOPINDEX:
8602 return LISTVIEW_GetTopIndex(infoPtr);
8604 /*case LVM_GETUNICODEFORMAT:
8605 FIXME("LVM_GETUNICODEFORMAT: unimplemented\n");
8606 return FALSE;*/
8608 /* case LVM_GETVIEW: */
8610 case LVM_GETVIEWRECT:
8611 return LISTVIEW_GetViewRect(infoPtr, (LPRECT)lParam);
8613 case LVM_GETWORKAREAS:
8614 FIXME("LVM_GETWORKAREAS: unimplemented\n");
8615 return FALSE;
8617 /* case LVM_HASGROUP: */
8619 case LVM_HITTEST:
8620 return LISTVIEW_HitTest(infoPtr, (LPLVHITTESTINFO)lParam, FALSE, FALSE);
8622 case LVM_INSERTCOLUMNA:
8623 return LISTVIEW_InsertColumnT(infoPtr, (INT)wParam, (LPLVCOLUMNW)lParam, FALSE);
8625 case LVM_INSERTCOLUMNW:
8626 return LISTVIEW_InsertColumnT(infoPtr, (INT)wParam, (LPLVCOLUMNW)lParam, TRUE);
8628 /* case LVM_INSERTGROUP: */
8630 /* case LVM_INSERTGROUPSORTED: */
8632 case LVM_INSERTITEMA:
8633 return LISTVIEW_InsertItemT(infoPtr, (LPLVITEMW)lParam, FALSE);
8635 case LVM_INSERTITEMW:
8636 return LISTVIEW_InsertItemT(infoPtr, (LPLVITEMW)lParam, TRUE);
8638 /* case LVM_INSERTMARKHITTEST: */
8640 /* case LVM_ISGROUPVIEWENABLED: */
8642 /* case LVM_MAPIDTOINDEX: */
8644 /* case LVM_MAPINDEXTOID: */
8646 /* case LVM_MOVEGROUP: */
8648 /* case LVM_MOVEITEMTOGROUP: */
8650 case LVM_REDRAWITEMS:
8651 return LISTVIEW_RedrawItems(infoPtr, (INT)wParam, (INT)lParam);
8653 /* case LVM_REMOVEALLGROUPS: */
8655 /* case LVM_REMOVEGROUP: */
8657 case LVM_SCROLL:
8658 return LISTVIEW_Scroll(infoPtr, (INT)wParam, (INT)lParam);
8660 case LVM_SETBKCOLOR:
8661 return LISTVIEW_SetBkColor(infoPtr, (COLORREF)lParam);
8663 /* case LVM_SETBKIMAGE: */
8665 case LVM_SETCALLBACKMASK:
8666 infoPtr->uCallbackMask = (UINT)wParam;
8667 return TRUE;
8669 case LVM_SETCOLUMNA:
8670 return LISTVIEW_SetColumnT(infoPtr, (INT)wParam, (LPLVCOLUMNW)lParam, FALSE);
8672 case LVM_SETCOLUMNW:
8673 return LISTVIEW_SetColumnT(infoPtr, (INT)wParam, (LPLVCOLUMNW)lParam, TRUE);
8675 case LVM_SETCOLUMNORDERARRAY:
8676 return LISTVIEW_SetColumnOrderArray(infoPtr, (INT)wParam, (LPINT)lParam);
8678 case LVM_SETCOLUMNWIDTH:
8679 return LISTVIEW_SetColumnWidth(infoPtr, (INT)wParam, SLOWORD(lParam));
8681 case LVM_SETEXTENDEDLISTVIEWSTYLE:
8682 return LISTVIEW_SetExtendedListViewStyle(infoPtr, (DWORD)wParam, (DWORD)lParam);
8684 /* case LVM_SETGROUPINFO: */
8686 /* case LVM_SETGROUPMETRICS: */
8688 case LVM_SETHOTCURSOR:
8689 return (LRESULT)LISTVIEW_SetHotCursor(infoPtr, (HCURSOR)lParam);
8691 case LVM_SETHOTITEM:
8692 return LISTVIEW_SetHotItem(infoPtr, (INT)wParam);
8694 case LVM_SETHOVERTIME:
8695 return LISTVIEW_SetHoverTime(infoPtr, (DWORD)wParam);
8697 case LVM_SETICONSPACING:
8698 return LISTVIEW_SetIconSpacing(infoPtr, SLOWORD(lParam), SHIWORD(lParam));
8700 case LVM_SETIMAGELIST:
8701 return (LRESULT)LISTVIEW_SetImageList(infoPtr, (INT)wParam, (HIMAGELIST)lParam);
8703 /* case LVM_SETINFOTIP: */
8705 /* case LVM_SETINSERTMARK: */
8707 /* case LVM_SETINSERTMARKCOLOR: */
8709 case LVM_SETITEMA:
8710 return LISTVIEW_SetItemT(infoPtr, (LPLVITEMW)lParam, FALSE);
8712 case LVM_SETITEMW:
8713 return LISTVIEW_SetItemT(infoPtr, (LPLVITEMW)lParam, TRUE);
8715 case LVM_SETITEMCOUNT:
8716 return LISTVIEW_SetItemCount(infoPtr, (INT)wParam, (DWORD)lParam);
8718 case LVM_SETITEMPOSITION:
8720 POINT pt = { SLOWORD(lParam), SHIWORD(lParam) };
8721 return LISTVIEW_SetItemPosition(infoPtr, (INT)wParam, pt);
8724 case LVM_SETITEMPOSITION32:
8725 if (lParam == 0) return FALSE;
8726 return LISTVIEW_SetItemPosition(infoPtr, (INT)wParam, *((POINT*)lParam));
8728 case LVM_SETITEMSTATE:
8729 return LISTVIEW_SetItemState(infoPtr, (INT)wParam, (LPLVITEMW)lParam);
8731 case LVM_SETITEMTEXTA:
8732 return LISTVIEW_SetItemTextT(infoPtr, (INT)wParam, (LPLVITEMW)lParam, FALSE);
8734 case LVM_SETITEMTEXTW:
8735 return LISTVIEW_SetItemTextT(infoPtr, (INT)wParam, (LPLVITEMW)lParam, TRUE);
8737 /* case LVM_SETOUTLINECOLOR: */
8739 /* case LVM_SETSELECTEDCOLUMN: */
8741 case LVM_SETSELECTIONMARK:
8742 return LISTVIEW_SetSelectionMark(infoPtr, (INT)lParam);
8744 case LVM_SETTEXTBKCOLOR:
8745 return LISTVIEW_SetTextBkColor(infoPtr, (COLORREF)lParam);
8747 case LVM_SETTEXTCOLOR:
8748 return LISTVIEW_SetTextColor(infoPtr, (COLORREF)lParam);
8750 /* case LVM_SETTILEINFO: */
8752 /* case LVM_SETTILEVIEWINFO: */
8754 /* case LVM_SETTILEWIDTH: */
8756 /* case LVM_SETTOOLTIPS: */
8758 /* case LVM_SETUNICODEFORMAT: */
8760 /* case LVM_SETVIEW: */
8762 /* case LVM_SETWORKAREAS: */
8764 /* case LVM_SORTGROUPS: */
8766 case LVM_SORTITEMS:
8767 return LISTVIEW_SortItems(infoPtr, (PFNLVCOMPARE)lParam, (LPARAM)wParam);
8769 /* LVM_SORTITEMSEX: */
8771 case LVM_SUBITEMHITTEST:
8772 return LISTVIEW_HitTest(infoPtr, (LPLVHITTESTINFO)lParam, TRUE, FALSE);
8774 case LVM_UPDATE:
8775 return LISTVIEW_Update(infoPtr, (INT)wParam);
8777 case WM_CHAR:
8778 return LISTVIEW_ProcessLetterKeys( infoPtr, wParam, lParam );
8780 case WM_COMMAND:
8781 return LISTVIEW_Command(infoPtr, wParam, lParam);
8783 case WM_CREATE:
8784 return LISTVIEW_Create(hwnd, (LPCREATESTRUCTW)lParam);
8786 case WM_ERASEBKGND:
8787 return LISTVIEW_EraseBkgnd(infoPtr, (HDC)wParam);
8789 case WM_GETDLGCODE:
8790 return DLGC_WANTCHARS | DLGC_WANTARROWS;
8792 case WM_GETFONT:
8793 return (LRESULT)infoPtr->hFont;
8795 case WM_HSCROLL:
8796 return LISTVIEW_HScroll(infoPtr, (INT)LOWORD(wParam), 0, (HWND)lParam);
8798 case WM_KEYDOWN:
8799 return LISTVIEW_KeyDown(infoPtr, (INT)wParam, (LONG)lParam);
8801 case WM_KILLFOCUS:
8802 return LISTVIEW_KillFocus(infoPtr);
8804 case WM_LBUTTONDBLCLK:
8805 return LISTVIEW_LButtonDblClk(infoPtr, (WORD)wParam, MAKEPOINTS(lParam));
8807 case WM_LBUTTONDOWN:
8808 return LISTVIEW_LButtonDown(infoPtr, (WORD)wParam, MAKEPOINTS(lParam));
8810 case WM_LBUTTONUP:
8811 return LISTVIEW_LButtonUp(infoPtr, (WORD)wParam, MAKEPOINTS(lParam));
8813 case WM_MOUSEMOVE:
8814 return LISTVIEW_MouseMove (infoPtr, (WORD)wParam, MAKEPOINTS(lParam));
8816 case WM_MOUSEHOVER:
8817 return LISTVIEW_MouseHover(infoPtr, (WORD)wParam, MAKEPOINTS(lParam));
8819 case WM_NCDESTROY:
8820 return LISTVIEW_NCDestroy(infoPtr);
8822 case WM_NOTIFY:
8823 if (lParam && ((LPNMHDR)lParam)->hwndFrom == infoPtr->hwndHeader)
8824 return LISTVIEW_HeaderNotification(infoPtr, (LPNMHEADERW)lParam);
8825 else return 0;
8827 case WM_NOTIFYFORMAT:
8828 return LISTVIEW_NotifyFormat(infoPtr, (HWND)wParam, (INT)lParam);
8830 case WM_PAINT:
8831 return LISTVIEW_Paint(infoPtr, (HDC)wParam);
8833 case WM_RBUTTONDBLCLK:
8834 return LISTVIEW_RButtonDblClk(infoPtr, (WORD)wParam, MAKEPOINTS(lParam));
8836 case WM_RBUTTONDOWN:
8837 return LISTVIEW_RButtonDown(infoPtr, (WORD)wParam, MAKEPOINTS(lParam));
8839 case WM_RBUTTONUP:
8840 return LISTVIEW_RButtonUp(infoPtr, (WORD)wParam, MAKEPOINTS(lParam));
8842 case WM_SETCURSOR:
8843 if(LISTVIEW_SetCursor(infoPtr, (HWND)wParam, LOWORD(lParam), HIWORD(lParam)))
8844 return TRUE;
8845 goto fwd_msg;
8847 case WM_SETFOCUS:
8848 return LISTVIEW_SetFocus(infoPtr, (HWND)wParam);
8850 case WM_SETFONT:
8851 return LISTVIEW_SetFont(infoPtr, (HFONT)wParam, (WORD)lParam);
8853 case WM_SETREDRAW:
8854 return LISTVIEW_SetRedraw(infoPtr, (BOOL)wParam);
8856 case WM_SIZE:
8857 return LISTVIEW_Size(infoPtr, (int)SLOWORD(lParam), (int)SHIWORD(lParam));
8859 case WM_STYLECHANGED:
8860 return LISTVIEW_StyleChanged(infoPtr, wParam, (LPSTYLESTRUCT)lParam);
8862 case WM_SYSCOLORCHANGE:
8863 COMCTL32_RefreshSysColors();
8864 return 0;
8866 /* case WM_TIMER: */
8868 case WM_VSCROLL:
8869 return LISTVIEW_VScroll(infoPtr, (INT)LOWORD(wParam), 0, (HWND)lParam);
8871 case WM_MOUSEWHEEL:
8872 if (wParam & (MK_SHIFT | MK_CONTROL))
8873 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
8874 return LISTVIEW_MouseWheel(infoPtr, (short int)HIWORD(wParam));
8876 case WM_WINDOWPOSCHANGED:
8877 if (!(((WINDOWPOS *)lParam)->flags & SWP_NOSIZE))
8879 SetWindowPos(infoPtr->hwndSelf, 0, 0, 0, 0, 0, SWP_FRAMECHANGED | SWP_NOACTIVATE |
8880 SWP_NOZORDER | SWP_NOMOVE | SWP_NOSIZE);
8881 LISTVIEW_UpdateSize(infoPtr);
8882 LISTVIEW_UpdateScroll(infoPtr);
8884 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
8886 /* case WM_WININICHANGE: */
8888 default:
8889 if ((uMsg >= WM_USER) && (uMsg < WM_APP))
8890 ERR("unknown msg %04x wp=%08x lp=%08lx\n", uMsg, wParam, lParam);
8892 fwd_msg:
8893 /* call default window procedure */
8894 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
8897 return 0;
8900 /***
8901 * DESCRIPTION:
8902 * Registers the window class.
8904 * PARAMETER(S):
8905 * None
8907 * RETURN:
8908 * None
8910 void LISTVIEW_Register(void)
8912 WNDCLASSW wndClass;
8914 ZeroMemory(&wndClass, sizeof(WNDCLASSW));
8915 wndClass.style = CS_GLOBALCLASS | CS_DBLCLKS;
8916 wndClass.lpfnWndProc = (WNDPROC)LISTVIEW_WindowProc;
8917 wndClass.cbClsExtra = 0;
8918 wndClass.cbWndExtra = sizeof(LISTVIEW_INFO *);
8919 wndClass.hCursor = LoadCursorW(0, IDC_ARROWW);
8920 wndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
8921 wndClass.lpszClassName = WC_LISTVIEWW;
8922 RegisterClassW(&wndClass);
8925 /***
8926 * DESCRIPTION:
8927 * Unregisters the window class.
8929 * PARAMETER(S):
8930 * None
8932 * RETURN:
8933 * None
8935 void LISTVIEW_Unregister(void)
8937 UnregisterClassW(WC_LISTVIEWW, NULL);
8940 /***
8941 * DESCRIPTION:
8942 * Handle any WM_COMMAND messages
8944 * PARAMETER(S):
8945 * [I] infoPtr : valid pointer to the listview structure
8946 * [I] wParam : the first message parameter
8947 * [I] lParam : the second message parameter
8949 * RETURN:
8950 * Zero.
8952 static LRESULT LISTVIEW_Command(LISTVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
8954 switch (HIWORD(wParam))
8956 case EN_UPDATE:
8959 * Adjust the edit window size
8961 WCHAR buffer[1024];
8962 HDC hdc = GetDC(infoPtr->hwndEdit);
8963 HFONT hFont, hOldFont = 0;
8964 RECT rect;
8965 SIZE sz;
8966 int len;
8968 if (!infoPtr->hwndEdit || !hdc) return 0;
8969 len = GetWindowTextW(infoPtr->hwndEdit, buffer, sizeof(buffer)/sizeof(buffer[0]));
8970 GetWindowRect(infoPtr->hwndEdit, &rect);
8972 /* Select font to get the right dimension of the string */
8973 hFont = (HFONT)SendMessageW(infoPtr->hwndEdit, WM_GETFONT, 0, 0);
8974 if(hFont != 0)
8976 hOldFont = SelectObject(hdc, hFont);
8979 if (GetTextExtentPoint32W(hdc, buffer, lstrlenW(buffer), &sz))
8981 TEXTMETRICW textMetric;
8983 /* Add Extra spacing for the next character */
8984 GetTextMetricsW(hdc, &textMetric);
8985 sz.cx += (textMetric.tmMaxCharWidth * 2);
8987 SetWindowPos (
8988 infoPtr->hwndEdit,
8989 HWND_TOP,
8992 sz.cx,
8993 rect.bottom - rect.top,
8994 SWP_DRAWFRAME|SWP_NOMOVE);
8996 if(hFont != 0)
8997 SelectObject(hdc, hOldFont);
8999 ReleaseDC(infoPtr->hwndSelf, hdc);
9001 break;
9004 default:
9005 return SendMessageW (GetParent (infoPtr->hwndSelf), WM_COMMAND, wParam, lParam);
9008 return 0;
9012 /***
9013 * DESCRIPTION:
9014 * Subclassed edit control windproc function
9016 * PARAMETER(S):
9017 * [I] hwnd : the edit window handle
9018 * [I] uMsg : the message that is to be processed
9019 * [I] wParam : first message parameter
9020 * [I] lParam : second message parameter
9021 * [I] isW : TRUE if input is Unicode
9023 * RETURN:
9024 * Zero.
9026 static LRESULT EditLblWndProcT(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL isW)
9028 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)GetWindowLongW(GetParent(hwnd), 0);
9029 BOOL cancel = FALSE;
9031 TRACE("(hwnd=%p, uMsg=%x, wParam=%x, lParam=%lx, isW=%d)\n",
9032 hwnd, uMsg, wParam, lParam, isW);
9034 switch (uMsg)
9036 case WM_GETDLGCODE:
9037 return DLGC_WANTARROWS | DLGC_WANTALLKEYS;
9039 case WM_KILLFOCUS:
9040 break;
9042 case WM_DESTROY:
9044 WNDPROC editProc = infoPtr->EditWndProc;
9045 infoPtr->EditWndProc = 0;
9046 SetWindowLongW(hwnd, GWL_WNDPROC, (LONG)editProc);
9047 return CallWindowProcT(editProc, hwnd, uMsg, wParam, lParam, isW);
9050 case WM_KEYDOWN:
9051 if (VK_ESCAPE == (INT)wParam)
9053 cancel = TRUE;
9054 break;
9056 else if (VK_RETURN == (INT)wParam)
9057 break;
9059 default:
9060 return CallWindowProcT(infoPtr->EditWndProc, hwnd, uMsg, wParam, lParam, isW);
9063 /* kill the edit */
9064 if (infoPtr->hwndEdit)
9066 LPWSTR buffer = NULL;
9068 infoPtr->hwndEdit = 0;
9069 if (!cancel)
9071 DWORD len = isW ? GetWindowTextLengthW(hwnd) : GetWindowTextLengthA(hwnd);
9073 if (len)
9075 if ( (buffer = COMCTL32_Alloc((len+1) * (isW ? sizeof(WCHAR) : sizeof(CHAR)))) )
9077 if (isW) GetWindowTextW(hwnd, buffer, len+1);
9078 else GetWindowTextA(hwnd, (CHAR*)buffer, len+1);
9082 LISTVIEW_EndEditLabelT(infoPtr, buffer, isW);
9084 if (buffer) COMCTL32_Free(buffer);
9088 SendMessageW(hwnd, WM_CLOSE, 0, 0);
9089 return 0;
9092 /***
9093 * DESCRIPTION:
9094 * Subclassed edit control Unicode windproc function
9096 * PARAMETER(S):
9097 * [I] hwnd : the edit window handle
9098 * [I] uMsg : the message that is to be processed
9099 * [I] wParam : first message parameter
9100 * [I] lParam : second message parameter
9102 * RETURN:
9104 LRESULT CALLBACK EditLblWndProcW(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
9106 return EditLblWndProcT(hwnd, uMsg, wParam, lParam, TRUE);
9109 /***
9110 * DESCRIPTION:
9111 * Subclassed edit control ANSI windproc function
9113 * PARAMETER(S):
9114 * [I] hwnd : the edit window handle
9115 * [I] uMsg : the message that is to be processed
9116 * [I] wParam : first message parameter
9117 * [I] lParam : second message parameter
9119 * RETURN:
9121 LRESULT CALLBACK EditLblWndProcA(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
9123 return EditLblWndProcT(hwnd, uMsg, wParam, lParam, FALSE);
9126 /***
9127 * DESCRIPTION:
9128 * Creates a subclassed edit cotrol
9130 * PARAMETER(S):
9131 * [I] infoPtr : valid pointer to the listview structure
9132 * [I] text : initial text for the edit
9133 * [I] style : the window style
9134 * [I] isW : TRUE if input is Unicode
9136 * RETURN:
9138 static HWND CreateEditLabelT(LISTVIEW_INFO *infoPtr, LPCWSTR text, DWORD style,
9139 INT x, INT y, INT width, INT height, BOOL isW)
9141 WCHAR editName[5] = { 'E', 'd', 'i', 't', '\0' };
9142 HWND hedit;
9143 SIZE sz;
9144 HDC hdc;
9145 HDC hOldFont=0;
9146 TEXTMETRICW textMetric;
9147 HINSTANCE hinst = (HINSTANCE)GetWindowLongW(infoPtr->hwndSelf, GWL_HINSTANCE);
9149 TRACE("(text=%s, ..., isW=%d)\n", debugtext_t(text, isW), isW);
9151 style |= WS_CHILDWINDOW|WS_CLIPSIBLINGS|ES_LEFT|WS_BORDER;
9152 hdc = GetDC(infoPtr->hwndSelf);
9154 /* Select the font to get appropriate metric dimensions */
9155 if(infoPtr->hFont != 0)
9156 hOldFont = SelectObject(hdc, infoPtr->hFont);
9158 /*Get String Lenght in pixels */
9159 GetTextExtentPoint32W(hdc, text, lstrlenW(text), &sz);
9161 /*Add Extra spacing for the next character */
9162 GetTextMetricsW(hdc, &textMetric);
9163 sz.cx += (textMetric.tmMaxCharWidth * 2);
9165 if(infoPtr->hFont != 0)
9166 SelectObject(hdc, hOldFont);
9168 ReleaseDC(infoPtr->hwndSelf, hdc);
9169 if (isW)
9170 hedit = CreateWindowW(editName, text, style, x, y, sz.cx, height, infoPtr->hwndSelf, 0, hinst, 0);
9171 else
9172 hedit = CreateWindowA("Edit", (LPCSTR)text, style, x, y, sz.cx, height, infoPtr->hwndSelf, 0, hinst, 0);
9174 if (!hedit) return 0;
9176 infoPtr->EditWndProc = (WNDPROC)
9177 (isW ? SetWindowLongW(hedit, GWL_WNDPROC, (LONG)EditLblWndProcW) :
9178 SetWindowLongA(hedit, GWL_WNDPROC, (LONG)EditLblWndProcA) );
9180 SendMessageW(hedit, WM_SETFONT, (WPARAM)infoPtr->hFont, FALSE);
9182 return hedit;