More traces to help us make sense of the output.
[wine.git] / dlls / comctl32 / listview.c
blob6eac81d0f1012e3a664755a72f7ac4d9ed6b5ba3
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
25 * Listview control implementation.
27 * TODO:
28 * -- Hot item handling.
29 * -- Expand large item in ICON mode when the cursor is flying over the icon or text.
30 * -- Support CustonDraw options for _WIN32_IE >= 0x560 (see NMLVCUSTOMDRAW docs)
32 * Notifications:
33 * LISTVIEW_Notify : most notifications from children (editbox and header)
35 * Data structure:
36 * LISTVIEW_SetItemCount : not completed for non OWNERDATA
38 * Advanced functionality:
39 * LISTVIEW_GetNumberOfWorkAreas : not implemented
40 * LISTVIEW_GetISearchString : not implemented
41 * LISTVIEW_GetBkImage : not implemented
42 * LISTVIEW_SetBkImage : not implemented
43 * LISTVIEW_GetColumnOrderArray : simple hack only
44 * LISTVIEW_SetColumnOrderArray : simple hack only
45 * LISTVIEW_Arrange : empty stub
46 * LISTVIEW_ApproximateViewRect : incomplete
47 * LISTVIEW_Update : not completed
49 * Known differences in message stream from native control (not known if
50 * these differences cause problems):
51 * LVM_INSERTITEM issues LVM_SETITEMSTATE and LVM_SETITEM in certain cases.
52 * LVM_SETITEM does not always issue LVN_ITEMCHANGING/LVN_ITEMCHANGED.
53 * WM_CREATE does not issue WM_QUERYUISTATE and associated registry
54 * processing for "USEDOUBLECLICKTIME".
58 #include "config.h"
59 #include "wine/port.h"
61 #include <assert.h>
62 #include <ctype.h>
63 #include <string.h>
64 #include <stdlib.h>
65 #include <stdio.h>
67 #include "winbase.h"
68 #include "winnt.h"
69 #include "heap.h"
70 #include "commctrl.h"
71 #include "comctl32.h"
72 #include "wine/debug.h"
74 WINE_DEFAULT_DEBUG_CHANNEL(listview);
76 /* make sure you set this to 0 for production use! */
77 #define DEBUG_RANGES 1
79 typedef struct tagCOLUMN_INFO
81 RECT rcHeader; /* tracks the header's rectangle */
82 UINT align; /* one of DT_{LEFT,CENTER,RIGHT} */
83 BOOL hasImage; /* on/off switch for column images */
84 } COLUMN_INFO;
86 typedef struct tagITEMHDR
88 LPWSTR pszText;
89 INT iImage;
90 } ITEMHDR, *LPITEMHDR;
92 typedef struct tagLISTVIEW_SUBITEM
94 ITEMHDR hdr;
95 INT iSubItem;
96 } LISTVIEW_SUBITEM;
98 typedef struct tagLISTVIEW_ITEM
100 ITEMHDR hdr;
101 UINT state;
102 LPARAM lParam;
103 INT iIndent;
104 } LISTVIEW_ITEM;
106 typedef struct tagRANGE
108 INT lower;
109 INT upper;
110 } RANGE;
112 typedef struct tagRANGES
114 HDPA hdpa;
115 } *RANGES;
117 typedef struct tagITERATOR
119 INT nItem;
120 INT nSpecial;
121 RANGE range;
122 RANGES ranges;
123 INT index;
124 } ITERATOR;
126 typedef struct tagLISTVIEW_INFO
128 HWND hwndSelf;
129 HBRUSH hBkBrush;
130 COLORREF clrBk;
131 COLORREF clrText;
132 COLORREF clrTextBk;
133 COLORREF clrTextBkDefault;
134 HIMAGELIST himlNormal;
135 HIMAGELIST himlSmall;
136 HIMAGELIST himlState;
137 BOOL bLButtonDown;
138 BOOL bRButtonDown;
139 INT nItemHeight;
140 INT nItemWidth;
141 RANGES selectionRanges;
142 INT nSelectionMark;
143 INT nHotItem;
144 SHORT notifyFormat;
145 RECT rcList; /* This rectangle is really the window
146 * client rectangle possibly reduced by the
147 * horizontal scroll bar and/or header - see
148 * LISTVIEW_UpdateSize. This rectangle offset
149 * by the LISTVIEW_GetOrigin value is in
150 * client coordinates */
151 RECT rcView; /* This rectangle contains all items -
152 * contructed in LISTVIEW_AlignTop and
153 * LISTVIEW_AlignLeft */
154 SIZE iconSize;
155 SIZE iconSpacing;
156 SIZE iconStateSize;
157 UINT uCallbackMask;
158 HWND hwndHeader;
159 HFONT hDefaultFont;
160 HCURSOR hHotCursor;
161 HFONT hFont;
162 INT ntmHeight; /* from GetTextMetrics from above font */
163 BOOL bRedraw;
164 BOOL bFocus;
165 INT nFocusedItem;
166 RECT rcFocus;
167 DWORD dwStyle; /* the cached window GWL_STYLE */
168 DWORD dwLvExStyle; /* extended listview style */
169 INT nItemCount; /* the number of items in the list */
170 HDPA hdpaItems; /* array ITEM_INFO pointers */
171 HDPA hdpaPosX; /* maintains the (X, Y) coordinates of the */
172 HDPA hdpaPosY; /* items in LVS_ICON, and LVS_SMALLICON modes */
173 HDPA hdpaColumns; /* array of COLUMN_INFO pointers */
174 PFNLVCOMPARE pfnCompare;
175 LPARAM lParamSort;
176 HWND hwndEdit;
177 WNDPROC EditWndProc;
178 INT nEditLabelItem;
179 DWORD dwHoverTime;
181 DWORD lastKeyPressTimestamp;
182 WPARAM charCode;
183 INT nSearchParamLength;
184 WCHAR szSearchParam[ MAX_PATH ];
185 BOOL bIsDrawing;
186 } LISTVIEW_INFO;
189 * constants
191 /* How many we debug buffer to allocate */
192 #define DEBUG_BUFFERS 20
193 /* The size of a single debug bbuffer */
194 #define DEBUG_BUFFER_SIZE 256
196 /* Internal interface to LISTVIEW_HScroll and LISTVIEW_VScroll */
197 #define SB_INTERNAL -1
199 /* maximum size of a label */
200 #define DISP_TEXT_SIZE 512
202 /* padding for items in list and small icon display modes */
203 #define WIDTH_PADDING 12
205 /* padding for items in list, report and small icon display modes */
206 #define HEIGHT_PADDING 1
208 /* offset of items in report display mode */
209 #define REPORT_MARGINX 2
211 /* padding for icon in large icon display mode
212 * ICON_TOP_PADDING_NOTHITABLE - space between top of box and area
213 * that HITTEST will see.
214 * ICON_TOP_PADDING_HITABLE - spacing between above and icon.
215 * ICON_TOP_PADDING - sum of the two above.
216 * ICON_BOTTOM_PADDING - between bottom of icon and top of text
217 * LABEL_VERT_PADDING - between bottom of text and end of box
219 * ICON_LR_PADDING - additional width above icon size.
220 * ICON_LR_HALF - half of the above value
222 #define ICON_TOP_PADDING_NOTHITABLE 2
223 #define ICON_TOP_PADDING_HITABLE 2
224 #define ICON_TOP_PADDING (ICON_TOP_PADDING_NOTHITABLE + ICON_TOP_PADDING_HITABLE)
225 #define ICON_BOTTOM_PADDING 4
226 #define LABEL_VERT_PADDING 7
227 #define ICON_LR_PADDING 16
228 #define ICON_LR_HALF (ICON_LR_PADDING/2)
230 /* default label width for items in list and small icon display modes */
231 #define DEFAULT_LABEL_WIDTH 40
233 /* default column width for items in list display mode */
234 #define DEFAULT_COLUMN_WIDTH 128
236 /* Size of "line" scroll for V & H scrolls */
237 #define LISTVIEW_SCROLL_ICON_LINE_SIZE 37
239 /* Padding betwen image and label */
240 #define IMAGE_PADDING 2
242 /* Padding behind the label */
243 #define TRAILING_PADDING 5
245 /* Border for the icon caption */
246 #define CAPTION_BORDER 2
248 /* Standard DrawText flags */
249 #define LV_ML_DT_FLAGS (DT_TOP | DT_NOPREFIX | DT_EDITCONTROL | DT_CENTER | DT_WORDBREAK | DT_WORD_ELLIPSIS | DT_END_ELLIPSIS)
250 #define LV_FL_DT_FLAGS (DT_TOP | DT_NOPREFIX | DT_EDITCONTROL | DT_CENTER | DT_WORDBREAK | DT_NOCLIP)
251 #define LV_SL_DT_FLAGS (DT_TOP | DT_EDITCONTROL | DT_SINGLELINE | DT_WORD_ELLIPSIS | DT_END_ELLIPSIS)
253 /* The time in milisecods to reset the search in the list */
254 #define KEY_DELAY 450
256 /* Dump the LISTVIEW_INFO structure to the debug channel */
257 #define LISTVIEW_DUMP(iP) do { \
258 TRACE("hwndSelf=%08x, clrBk=0x%06lx, clrText=0x%06lx, clrTextBk=0x%06lx, ItemHeight=%d, ItemWidth=%d, Style=0x%08lx\n", \
259 iP->hwndSelf, iP->clrBk, iP->clrText, iP->clrTextBk, \
260 iP->nItemHeight, iP->nItemWidth, infoPtr->dwStyle); \
261 TRACE("hwndSelf=%08x, himlNor=%p, himlSml=%p, himlState=%p, Focused=%d, Hot=%d, exStyle=0x%08lx, Focus=%s\n", \
262 iP->hwndSelf, iP->himlNormal, iP->himlSmall, iP->himlState, \
263 iP->nFocusedItem, iP->nHotItem, iP->dwLvExStyle, \
264 (iP->bFocus) ? "true" : "false"); \
265 TRACE("hwndSelf=%08x, ntmH=%d, icSz.cx=%ld, icSz.cy=%ld, icSp.cx=%ld, icSp.cy=%ld, notifyFmt=%d\n", \
266 iP->hwndSelf, iP->ntmHeight, iP->iconSize.cx, iP->iconSize.cy, \
267 iP->iconSpacing.cx, iP->iconSpacing.cy, iP->notifyFormat); \
268 TRACE("hwndSelf=%08x, rcList=(%d,%d)-(%d,%d), rcView=(%d,%d)-(%d,%d)\n", \
269 iP->hwndSelf, \
270 iP->rcList.left, iP->rcList.top, iP->rcList.right, iP->rcList.bottom, \
271 iP->rcView.left, iP->rcView.top, iP->rcView.right, iP->rcView.bottom); \
272 } while(0)
276 * forward declarations
278 static BOOL LISTVIEW_GetItemT(LISTVIEW_INFO *, LPLVITEMW, BOOL);
279 static BOOL LISTVIEW_GetItemBox(LISTVIEW_INFO *, INT, LPRECT);
280 static void LISTVIEW_AlignLeft(LISTVIEW_INFO *);
281 static void LISTVIEW_AlignTop(LISTVIEW_INFO *);
282 static void LISTVIEW_AddGroupSelection(LISTVIEW_INFO *, INT);
283 static INT LISTVIEW_CalculateMaxHeight(LISTVIEW_INFO *);
284 static BOOL LISTVIEW_GetItemOrigin(LISTVIEW_INFO *, INT, LPPOINT);
285 static BOOL LISTVIEW_GetItemPosition(LISTVIEW_INFO *, INT, LPPOINT);
286 static BOOL LISTVIEW_GetItemRect(LISTVIEW_INFO *, INT, LPRECT);
287 static INT LISTVIEW_CalculateMaxWidth(LISTVIEW_INFO *);
288 static INT LISTVIEW_GetLabelWidth(LISTVIEW_INFO *, INT);
289 static INT LISTVIEW_GetColumnWidth(LISTVIEW_INFO *, INT);
290 static BOOL LISTVIEW_GetOrigin(LISTVIEW_INFO *, LPPOINT);
291 static BOOL LISTVIEW_GetViewRect(LISTVIEW_INFO *, LPRECT);
292 static void LISTVIEW_SetGroupSelection(LISTVIEW_INFO *, INT);
293 static BOOL LISTVIEW_SetItemT(LISTVIEW_INFO *, LPLVITEMW, BOOL);
294 static BOOL LISTVIEW_SetItemPosition(LISTVIEW_INFO *, INT, POINT);
295 static void LISTVIEW_UpdateScroll(LISTVIEW_INFO *);
296 static void LISTVIEW_SetSelection(LISTVIEW_INFO *, INT);
297 static BOOL LISTVIEW_UpdateSize(LISTVIEW_INFO *);
298 static void LISTVIEW_UnsupportedStyles(LONG);
299 static HWND LISTVIEW_EditLabelT(LISTVIEW_INFO *, INT, BOOL);
300 static LRESULT LISTVIEW_Command(LISTVIEW_INFO *, WPARAM, LPARAM);
301 static LRESULT LISTVIEW_SortItems(LISTVIEW_INFO *, PFNLVCOMPARE, LPARAM);
302 static LRESULT LISTVIEW_GetStringWidthT(LISTVIEW_INFO *, LPCWSTR, BOOL);
303 static BOOL LISTVIEW_KeySelection(LISTVIEW_INFO *, INT);
304 static LRESULT LISTVIEW_GetItemState(LISTVIEW_INFO *, INT, UINT);
305 static LRESULT LISTVIEW_SetItemState(LISTVIEW_INFO *, INT, LPLVITEMW);
306 static LRESULT LISTVIEW_GetColumnT(LISTVIEW_INFO *, INT, LPLVCOLUMNW, BOOL);
307 static LRESULT LISTVIEW_VScroll(LISTVIEW_INFO *, INT, INT, HWND);
308 static LRESULT LISTVIEW_HScroll(LISTVIEW_INFO *, INT, INT, HWND);
309 static INT LISTVIEW_GetTopIndex(LISTVIEW_INFO *);
310 static BOOL LISTVIEW_EnsureVisible(LISTVIEW_INFO *, INT, BOOL);
311 static HWND CreateEditLabelT(LISTVIEW_INFO *, LPCWSTR, DWORD, INT, INT, INT, INT, BOOL);
313 /******** Text handling functions *************************************/
315 /* A text pointer is either NULL, LPSTR_TEXTCALLBACK, or points to a
316 * text string. The string may be ANSI or Unicode, in which case
317 * the boolean isW tells us the type of the string.
319 * The name of the function tell what type of strings it expects:
320 * W: Unicode, T: ANSI/Unicode - function of isW
323 static inline BOOL is_textW(LPCWSTR text)
325 return text != NULL && text != LPSTR_TEXTCALLBACKW;
328 static inline BOOL is_textT(LPCWSTR text, BOOL isW)
330 /* we can ignore isW since LPSTR_TEXTCALLBACKW == LPSTR_TEXTCALLBACKA */
331 return is_textW(text);
334 static inline int textlenT(LPCWSTR text, BOOL isW)
336 return !is_textT(text, isW) ? 0 :
337 isW ? lstrlenW(text) : lstrlenA((LPCSTR)text);
340 static inline void textcpynT(LPWSTR dest, BOOL isDestW, LPCWSTR src, BOOL isSrcW, INT max)
342 if (isDestW)
343 if (isSrcW) lstrcpynW(dest, src, max);
344 else MultiByteToWideChar(CP_ACP, 0, (LPCSTR)src, -1, dest, max);
345 else
346 if (isSrcW) WideCharToMultiByte(CP_ACP, 0, src, -1, (LPSTR)dest, max, NULL, NULL);
347 else lstrcpynA((LPSTR)dest, (LPCSTR)src, max);
350 static inline LPWSTR textdupTtoW(LPCWSTR text, BOOL isW)
352 LPWSTR wstr = (LPWSTR)text;
354 if (!isW && is_textT(text, isW))
356 INT len = MultiByteToWideChar(CP_ACP, 0, (LPCSTR)text, -1, NULL, 0);
357 wstr = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
358 if (wstr) MultiByteToWideChar(CP_ACP, 0, (LPCSTR)text, -1, wstr, len);
360 TRACE(" wstr=%s\n", text == LPSTR_TEXTCALLBACKW ? "(callback)" : debugstr_w(wstr));
361 return wstr;
364 static inline void textfreeT(LPWSTR wstr, BOOL isW)
366 if (!isW && is_textT(wstr, isW)) HeapFree(GetProcessHeap(), 0, wstr);
370 * dest is a pointer to a Unicode string
371 * src is a pointer to a string (Unicode if isW, ANSI if !isW)
373 static BOOL textsetptrT(LPWSTR *dest, LPWSTR src, BOOL isW)
375 BOOL bResult = TRUE;
377 if (src == LPSTR_TEXTCALLBACKW)
379 if (is_textW(*dest)) COMCTL32_Free(*dest);
380 *dest = LPSTR_TEXTCALLBACKW;
382 else
384 LPWSTR pszText = textdupTtoW(src, isW);
385 if (*dest == LPSTR_TEXTCALLBACKW) *dest = NULL;
386 bResult = Str_SetPtrW(dest, pszText);
387 textfreeT(pszText, isW);
389 return bResult;
393 * compares a Unicode to a Unicode/ANSI text string
395 static inline int textcmpWT(LPWSTR aw, LPWSTR bt, BOOL isW)
397 if (!aw) return bt ? -1 : 0;
398 if (!bt) return aw ? 1 : 0;
399 if (aw == LPSTR_TEXTCALLBACKW)
400 return bt == LPSTR_TEXTCALLBACKW ? 0 : -1;
401 if (bt != LPSTR_TEXTCALLBACKW)
403 LPWSTR bw = textdupTtoW(bt, isW);
404 int r = bw ? lstrcmpW(aw, bw) : 1;
405 textfreeT(bw, isW);
406 return r;
409 return 1;
412 static inline int lstrncmpiW(LPCWSTR s1, LPCWSTR s2, int n)
414 int res;
416 n = min(min(n, strlenW(s1)), strlenW(s2));
417 res = CompareStringW(LOCALE_USER_DEFAULT, NORM_IGNORECASE, s1, n, s2, n);
418 return res ? res - sizeof(WCHAR) : res;
421 /******** Debugging functions *****************************************/
423 static inline LPCSTR debugtext_t(LPCWSTR text, BOOL isW)
425 if (text == LPSTR_TEXTCALLBACKW) return "(callback)";
426 return isW ? debugstr_w(text) : debugstr_a((LPCSTR)text);
429 static inline LPCSTR debugtext_tn(LPCWSTR text, BOOL isW, INT n)
431 if (text == LPSTR_TEXTCALLBACKW) return "(callback)";
432 n = min(textlenT(text, isW), n);
433 return isW ? debugstr_wn(text, n) : debugstr_an((LPCSTR)text, n);
436 static char* debug_getbuf()
438 static int index = 0;
439 static char buffers[DEBUG_BUFFERS][DEBUG_BUFFER_SIZE];
440 return buffers[index++ % DEBUG_BUFFERS];
443 static inline char* debugrange(const RANGE* lprng)
445 if (lprng)
447 char* buf = debug_getbuf();
448 snprintf(buf, DEBUG_BUFFER_SIZE, "[%d, %d)", lprng->lower, lprng->upper);
449 return buf;
450 } else return "(null)";
453 static inline char* debugpoint(const POINT* lppt)
455 if (lppt)
457 char* buf = debug_getbuf();
458 snprintf(buf, DEBUG_BUFFER_SIZE, "(%ld, %ld)", lppt->x, lppt->y);
459 return buf;
460 } else return "(null)";
463 static inline char* debugrect(const RECT* rect)
465 if (rect)
467 char* buf = debug_getbuf();
468 snprintf(buf, DEBUG_BUFFER_SIZE, "[(%d, %d);(%d, %d)]",
469 rect->left, rect->top, rect->right, rect->bottom);
470 return buf;
471 } else return "(null)";
474 static char* debugnmlistview(LPNMLISTVIEW plvnm)
476 if (plvnm)
478 char* buf = debug_getbuf();
479 snprintf(buf, DEBUG_BUFFER_SIZE, "iItem=%d, iSubItem=%d, uNewState=0x%x,"
480 " uOldState=0x%x, uChanged=0x%x, ptAction=%s, lParam=%ld\n",
481 plvnm->iItem, plvnm->iSubItem, plvnm->uNewState, plvnm->uOldState,
482 plvnm->uChanged, debugpoint(&plvnm->ptAction), plvnm->lParam);
483 return buf;
484 } else return "(null)";
487 static char* debuglvitem_t(LPLVITEMW lpLVItem, BOOL isW)
489 char* buf = debug_getbuf(), *text = buf;
490 int len, size = DEBUG_BUFFER_SIZE;
492 if (lpLVItem == NULL) return "(null)";
493 len = snprintf(buf, size, "{iItem=%d, iSubItem=%d, ", lpLVItem->iItem, lpLVItem->iSubItem);
494 if (len == -1) goto end; buf += len; size -= len;
495 if (lpLVItem->mask & LVIF_STATE)
496 len = snprintf(buf, size, "state=%x, stateMask=%x, ", lpLVItem->state, lpLVItem->stateMask);
497 else len = 0;
498 if (len == -1) goto end; buf += len; size -= len;
499 if (lpLVItem->mask & LVIF_TEXT)
500 len = snprintf(buf, size, "pszText=%s, cchTextMax=%d, ", debugtext_tn(lpLVItem->pszText, isW, 80), lpLVItem->cchTextMax);
501 else len = 0;
502 if (len == -1) goto end; buf += len; size -= len;
503 if (lpLVItem->mask & LVIF_IMAGE)
504 len = snprintf(buf, size, "iImage=%d, ", lpLVItem->iImage);
505 else len = 0;
506 if (len == -1) goto end; buf += len; size -= len;
507 if (lpLVItem->mask & LVIF_PARAM)
508 len = snprintf(buf, size, "lParam=%lx, ", lpLVItem->lParam);
509 else len = 0;
510 if (len == -1) goto end; buf += len; size -= len;
511 if (lpLVItem->mask & LVIF_INDENT)
512 len = snprintf(buf, size, "iIndent=%d, ", lpLVItem->iIndent);
513 else len = 0;
514 if (len == -1) goto end; buf += len; size -= len;
515 goto undo;
516 end:
517 buf = text + strlen(text);
518 undo:
519 if (buf - text > 2) buf[-2] = '}'; buf[-1] = 0;
520 return text;
523 static char* debuglvcolumn_t(LPLVCOLUMNW lpColumn, BOOL isW)
525 char* buf = debug_getbuf(), *text = buf;
526 int len, size = DEBUG_BUFFER_SIZE;
528 if (lpColumn == NULL) return "(null)";
529 len = snprintf(buf, size, "{");
530 if (len == -1) goto end; buf += len; size -= len;
531 if (lpColumn->mask & LVCF_SUBITEM)
532 len = snprintf(buf, size, "iSubItem=%d, ", lpColumn->iSubItem);
533 else len = 0;
534 if (len == -1) goto end; buf += len; size -= len;
535 if (lpColumn->mask & LVCF_FMT)
536 len = snprintf(buf, size, "fmt=%x, ", lpColumn->fmt);
537 else len = 0;
538 if (len == -1) goto end; buf += len; size -= len;
539 if (lpColumn->mask & LVCF_WIDTH)
540 len = snprintf(buf, size, "cx=%d, ", lpColumn->cx);
541 else len = 0;
542 if (len == -1) goto end; buf += len; size -= len;
543 if (lpColumn->mask & LVCF_TEXT)
544 len = snprintf(buf, size, "pszText=%s, cchTextMax=%d, ", debugtext_tn(lpColumn->pszText, isW, 80), lpColumn->cchTextMax);
545 else len = 0;
546 if (len == -1) goto end; buf += len; size -= len;
547 if (lpColumn->mask & LVCF_IMAGE)
548 len = snprintf(buf, size, "iImage=%d, ", lpColumn->iImage);
549 else len = 0;
550 if (len == -1) goto end; buf += len; size -= len;
551 if (lpColumn->mask & LVCF_ORDER)
552 len = snprintf(buf, size, "iOrder=%d, ", lpColumn->iOrder);
553 else len = 0;
554 if (len == -1) goto end; buf += len; size -= len;
555 goto undo;
556 end:
557 buf = text + strlen(text);
558 undo:
559 if (buf - text > 2) buf[-2] = '}'; buf[-1] = 0;
560 return text;
564 /******** Notification functions i************************************/
566 static LRESULT notify_hdr(LISTVIEW_INFO *infoPtr, INT code, LPNMHDR pnmh)
568 LRESULT result;
570 TRACE("(code=%d)\n", code);
572 pnmh->hwndFrom = infoPtr->hwndSelf;
573 pnmh->idFrom = GetWindowLongW(infoPtr->hwndSelf, GWL_ID);
574 pnmh->code = code;
575 result = SendMessageW(GetParent(infoPtr->hwndSelf), WM_NOTIFY,
576 (WPARAM)pnmh->idFrom, (LPARAM)pnmh);
578 TRACE(" <= %ld\n", result);
580 return result;
583 static inline LRESULT notify(LISTVIEW_INFO *infoPtr, INT code)
585 NMHDR nmh;
586 return notify_hdr(infoPtr, code, &nmh);
589 static inline void notify_itemactivate(LISTVIEW_INFO *infoPtr)
591 notify(infoPtr, LVN_ITEMACTIVATE);
594 static inline LRESULT notify_listview(LISTVIEW_INFO *infoPtr, INT code, LPNMLISTVIEW plvnm)
596 TRACE("(code=%d, plvnm=%s)\n", code, debugnmlistview(plvnm));
597 return notify_hdr(infoPtr, code, (LPNMHDR)plvnm);
600 static LRESULT notify_click(LISTVIEW_INFO *infoPtr, INT code, LVHITTESTINFO *lvht)
602 NMLISTVIEW nmlv;
604 ZeroMemory(&nmlv, sizeof(nmlv));
605 nmlv.iItem = lvht->iItem;
606 nmlv.iSubItem = lvht->iSubItem;
607 nmlv.ptAction = lvht->pt;
608 return notify_listview(infoPtr, code, &nmlv);
611 static int get_ansi_notification(INT unicodeNotificationCode)
613 switch (unicodeNotificationCode)
615 case LVN_BEGINLABELEDITW: return LVN_BEGINLABELEDITA;
616 case LVN_ENDLABELEDITW: return LVN_ENDLABELEDITA;
617 case LVN_GETDISPINFOW: return LVN_GETDISPINFOA;
618 case LVN_SETDISPINFOW: return LVN_SETDISPINFOA;
619 case LVN_ODFINDITEMW: return LVN_ODFINDITEMA;
620 case LVN_GETINFOTIPW: return LVN_GETINFOTIPA;
622 ERR("unknown notification %x\n", unicodeNotificationCode);
623 assert(FALSE);
627 Send notification. depends on dispinfoW having same
628 structure as dispinfoA.
629 infoPtr : listview struct
630 notificationCode : *Unicode* notification code
631 pdi : dispinfo structure (can be unicode or ansi)
632 isW : TRUE if dispinfo is Unicode
634 static BOOL notify_dispinfoT(LISTVIEW_INFO *infoPtr, INT notificationCode, LPNMLVDISPINFOW pdi, BOOL isW)
636 BOOL bResult = FALSE;
637 BOOL convertToAnsi = FALSE, convertToUnicode = FALSE;
638 INT realNotifCode;
639 INT cchTempBufMax = 0, savCchTextMax = 0;
640 LPWSTR pszTempBuf = NULL, savPszText = NULL;
642 if ((pdi->item.mask & LVIF_TEXT) && is_textT(pdi->item.pszText, isW))
644 convertToAnsi = (isW && infoPtr->notifyFormat == NFR_ANSI);
645 convertToUnicode = (!isW && infoPtr->notifyFormat == NFR_UNICODE);
648 if (convertToAnsi || convertToUnicode)
650 if (notificationCode != LVN_GETDISPINFOW)
652 cchTempBufMax = convertToUnicode ?
653 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pdi->item.pszText, -1, NULL, 0):
654 WideCharToMultiByte(CP_ACP, 0, pdi->item.pszText, -1, NULL, 0, NULL, NULL);
656 else
658 cchTempBufMax = pdi->item.cchTextMax;
659 *pdi->item.pszText = 0; /* make sure we don't process garbage */
662 pszTempBuf = HeapAlloc(GetProcessHeap(), 0,
663 (convertToUnicode ? sizeof(WCHAR) : sizeof(CHAR)) * cchTempBufMax);
664 if (!pszTempBuf) return FALSE;
665 if (convertToUnicode)
666 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pdi->item.pszText, -1,
667 pszTempBuf, cchTempBufMax);
668 else
669 WideCharToMultiByte(CP_ACP, 0, pdi->item.pszText, -1, (LPSTR) pszTempBuf,
670 cchTempBufMax, NULL, NULL);
671 savCchTextMax = pdi->item.cchTextMax;
672 savPszText = pdi->item.pszText;
673 pdi->item.pszText = pszTempBuf;
674 pdi->item.cchTextMax = cchTempBufMax;
677 if (infoPtr->notifyFormat == NFR_ANSI)
678 realNotifCode = get_ansi_notification(notificationCode);
679 else
680 realNotifCode = notificationCode;
681 TRACE(" pdi->item=%s\n", debuglvitem_t(&pdi->item, infoPtr->notifyFormat != NFR_ANSI));
682 bResult = notify_hdr(infoPtr, realNotifCode, (LPNMHDR)pdi);
684 if (convertToUnicode || convertToAnsi)
686 if (convertToUnicode) /* note : pointer can be changed by app ! */
687 WideCharToMultiByte(CP_ACP, 0, pdi->item.pszText, -1, (LPSTR) savPszText,
688 savCchTextMax, NULL, NULL);
689 else
690 MultiByteToWideChar(CP_ACP, 0, (LPSTR) pdi->item.pszText, -1,
691 savPszText, savCchTextMax);
692 pdi->item.pszText = savPszText; /* restores our buffer */
693 pdi->item.cchTextMax = savCchTextMax;
694 HeapFree(GetProcessHeap(), 0, pszTempBuf);
696 return bResult;
699 static void customdraw_fill(NMLVCUSTOMDRAW *lpnmlvcd, LISTVIEW_INFO *infoPtr, HDC hdc, LPRECT rcBounds)
701 ZeroMemory(lpnmlvcd, sizeof(NMLVCUSTOMDRAW));
702 lpnmlvcd->nmcd.hdc = hdc;
703 lpnmlvcd->nmcd.rc = *rcBounds;
704 lpnmlvcd->clrTextBk = infoPtr->clrTextBk;
705 lpnmlvcd->clrText = infoPtr->clrText;
708 static inline DWORD notify_customdraw (LISTVIEW_INFO *infoPtr, DWORD dwDrawStage, NMLVCUSTOMDRAW *lpnmlvcd)
710 lpnmlvcd->nmcd.dwDrawStage = dwDrawStage;
711 return notify_hdr(infoPtr, NM_CUSTOMDRAW, &lpnmlvcd->nmcd.hdr);
714 /******** Item iterator functions **********************************/
716 static RANGES ranges_create(int count);
717 static void ranges_destroy(RANGES ranges);
718 static BOOL ranges_add(RANGES ranges, RANGE range);
719 static BOOL ranges_del(RANGES ranges, RANGE range);
720 static void ranges_dump(RANGES ranges);
722 static inline BOOL ranges_additem(RANGES ranges, INT nItem)
724 RANGE range = { nItem, nItem + 1 };
726 return ranges_add(ranges, range);
729 static inline BOOL ranges_delitem(RANGES ranges, INT nItem)
731 RANGE range = { nItem, nItem + 1 };
733 return ranges_del(ranges, range);
736 /***
737 * ITERATOR DOCUMENTATION
739 * The iterator functions allow for easy, and convenient iteration
740 * over items of iterest in the list. Typically, you create a
741 * iterator, use it, and destroy it, as such:
742 * ITERATOR i;
744 * iterator_xxxitems(&i, ...);
745 * while (iterator_{prev,next}(&i)
747 * //code which uses i.nItem
749 * iterator_destroy(&i);
751 * where xxx is either: framed, or visible.
752 * Note that it is important that the code destroys the iterator
753 * after it's done with it, as the creation of the iterator may
754 * allocate memory, which thus needs to be freed.
756 * You can iterate both forwards, and backwards through the list,
757 * by using iterator_next or iterator_prev respectively.
759 * Lower numbered items are draw on top of higher number items in
760 * LVS_ICON, and LVS_SMALLICON (which are the only modes where
761 * items may overlap). So, to test items, you should use
762 * iterator_next
763 * which lists the items top to bottom (in Z-order).
764 * For drawing items, you should use
765 * iterator_prev
766 * which lists the items bottom to top (in Z-order).
767 * If you keep iterating over the items after the end-of-items
768 * marker (-1) is returned, the iterator will start from the
769 * beginning. Typically, you don't need to test for -1,
770 * because iterator_{next,prev} will return TRUE if more items
771 * are to be iterated over, or FALSE otherwise.
773 * Note: the iterator is defined to be bidirectional. That is,
774 * any number of prev followed by any number of next, or
775 * five versa, should leave the iterator at the same item:
776 * prev * n, next * n = next * n, prev * n
778 * The iterator has a notion of a out-of-order, special item,
779 * which sits at the start of the list. This is used in
780 * LVS_ICON, and LVS_SMALLICON mode to handle the focused item,
781 * which needs to be first, as it may overlap other items.
783 * The code is a bit messy because we have:
784 * - a special item to deal with
785 * - simple range, or composite range
786 * - empty range.
787 * If find bugs, or want to add features, please make sure you
788 * always check/modify *both* iterator_prev, and iterator_next.
791 /****
792 * This function iterates through the items in increasing order,
793 * but prefixed by the special item, then -1. That is:
794 * special, 1, 2, 3, ..., n, -1.
795 * Each item is listed only once.
797 static inline BOOL iterator_next(ITERATOR* i)
799 if (i->nItem == -1)
801 i->nItem = i->nSpecial;
802 if (i->nItem != -1) return TRUE;
804 if (i->nItem == i->nSpecial)
806 if (i->ranges) i->index = 0;
807 goto pickarange;
810 i->nItem++;
811 testitem:
812 if (i->nItem == i->nSpecial) i->nItem++;
813 if (i->nItem < i->range.upper) return TRUE;
815 pickarange:
816 if (i->ranges)
818 if (i->index < i->ranges->hdpa->nItemCount)
819 i->range = *(RANGE*)DPA_GetPtr(i->ranges->hdpa, i->index++);
820 else goto end;
822 else if (i->nItem >= i->range.upper) goto end;
824 i->nItem = i->range.lower;
825 if (i->nItem >= 0) goto testitem;
826 end:
827 i->nItem = -1;
828 return FALSE;
831 /****
832 * This function iterates through the items in decreasing order,
833 * followed by the special item, then -1. That is:
834 * n, n-1, ..., 3, 2, 1, special, -1.
835 * Each item is listed only once.
837 static inline BOOL iterator_prev(ITERATOR* i)
839 BOOL start = FALSE;
841 if (i->nItem == -1)
843 start = TRUE;
844 if (i->ranges) i->index = i->ranges->hdpa->nItemCount;
845 goto pickarange;
847 if (i->nItem == i->nSpecial)
849 i->nItem = -1;
850 return FALSE;
853 testitem:
854 i->nItem--;
855 if (i->nItem == i->nSpecial) i->nItem--;
856 if (i->nItem >= i->range.lower) return TRUE;
858 pickarange:
859 if (i->ranges)
861 if (i->index > 0)
862 i->range = *(RANGE*)DPA_GetPtr(i->ranges->hdpa, --i->index);
863 else goto end;
865 else if (!start && i->nItem < i->range.lower) goto end;
867 i->nItem = i->range.upper;
868 if (i->nItem > 0) goto testitem;
869 end:
870 return (i->nItem = i->nSpecial) != -1;
873 static RANGE iterator_range(ITERATOR* i)
875 RANGE range;
877 if (!i->ranges) return i->range;
879 range.lower = (*(RANGE*)DPA_GetPtr(i->ranges->hdpa, 0)).lower;
880 range.upper = (*(RANGE*)DPA_GetPtr(i->ranges->hdpa, i->ranges->hdpa->nItemCount - 1)).upper;
881 return range;
884 /***
885 * Releases resources associated with this ierator.
887 static inline void iterator_destroy(ITERATOR* i)
889 if (i->ranges) ranges_destroy(i->ranges);
892 /***
893 * Create an empty iterator.
895 static inline BOOL iterator_empty(ITERATOR* i)
897 ZeroMemory(i, sizeof(*i));
898 i->nItem = i->nSpecial = i->range.lower = i->range.upper = -1;
899 return TRUE;
903 /***
904 * Create an iterator over a bunch of ranges.
905 * Please note that the iterator will take ownership of the ranges,
906 * and will free them upon destruction.
908 static inline BOOL iterator_ranges(ITERATOR* i, RANGES ranges)
910 iterator_empty(i);
911 i->ranges = ranges;
912 return TRUE;
915 /***
916 * Creates an iterator over the items which intersect lprc.
918 static BOOL iterator_frameditems(ITERATOR* i, LISTVIEW_INFO* infoPtr, const RECT* lprc)
920 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
921 RECT frame = *lprc, rcItem, rcTemp;
922 POINT Origin;
924 /* in case we fail, we want to return an empty iterator */
925 if (!iterator_empty(i)) return FALSE;
927 if (!LISTVIEW_GetOrigin(infoPtr, &Origin)) return FALSE;
929 TRACE("(lprc=%s)\n", debugrect(lprc));
930 OffsetRect(&frame, -Origin.x, -Origin.y);
932 if (uView == LVS_ICON || uView == LVS_SMALLICON)
934 INT nItem;
936 if (uView == LVS_ICON)
938 if (LISTVIEW_GetItemBox(infoPtr, infoPtr->nFocusedItem, &rcItem) && IntersectRect(&rcTemp, &rcItem, lprc))
939 i->nSpecial = infoPtr->nFocusedItem;
941 if (!(i->ranges = ranges_create(50))) return FALSE;
942 /* to do better here, we need to have PosX, and PosY sorted */
943 TRACE("building icon ranges:\n");
944 for (nItem = 0; nItem < infoPtr->nItemCount; nItem++)
946 rcItem.left = (LONG)DPA_GetPtr(infoPtr->hdpaPosX, nItem);
947 rcItem.top = (LONG)DPA_GetPtr(infoPtr->hdpaPosY, nItem);
948 rcItem.right = rcItem.left + infoPtr->nItemWidth;
949 rcItem.bottom = rcItem.top + infoPtr->nItemHeight;
950 if (IntersectRect(&rcTemp, &rcItem, &frame))
951 ranges_additem(i->ranges, nItem);
953 if (TRACE_ON(listview))
955 TRACE(" icon items:\n");
956 ranges_dump(i->ranges);
958 return TRUE;
960 else if (uView == LVS_REPORT)
962 INT lower, upper;
964 if (frame.left >= infoPtr->nItemWidth) return TRUE;
965 if (frame.top >= infoPtr->nItemHeight * infoPtr->nItemCount) return TRUE;
967 lower = max(frame.top / infoPtr->nItemHeight, 0);
968 upper = min((frame.bottom - 1) / infoPtr->nItemHeight, infoPtr->nItemCount - 1);
969 if (upper < lower) return TRUE;
970 i->range.lower = lower;
971 i->range.upper = upper + 1;
972 TRACE(" report=%s\n", debugrange(&i->range));
974 else
976 INT nPerCol = max((infoPtr->rcList.bottom - infoPtr->rcList.top) / infoPtr->nItemHeight, 1);
977 INT nFirstRow = max(frame.top / infoPtr->nItemHeight, 0);
978 INT nLastRow = min((frame.bottom - 1) / infoPtr->nItemHeight, nPerCol - 1);
979 INT nFirstCol = max(frame.left / infoPtr->nItemWidth, 0);
980 INT nLastCol = min((frame.right - 1) / infoPtr->nItemWidth, (infoPtr->nItemCount + nPerCol - 1) / nPerCol);
981 INT lower = nFirstCol * nPerCol + nFirstRow;
982 RANGE item_range;
983 INT nCol;
985 TRACE("nPerCol=%d, nFirstRow=%d, nLastRow=%d, nFirstCol=%d, nLastCol=%d, lower=%d\n",
986 nPerCol, nFirstRow, nLastRow, nFirstCol, nLastCol, lower);
988 if (nLastCol < nFirstCol || nLastRow < nFirstRow) return TRUE;
990 if (!(i->ranges = ranges_create(nLastCol - nFirstCol + 1))) return FALSE;
991 TRACE("building list ranges:\n");
992 for (nCol = nFirstCol; nCol <= nLastCol; nCol++)
994 item_range.lower = nCol * nPerCol + nFirstRow;
995 if(item_range.lower >= infoPtr->nItemCount) break;
996 item_range.upper = min(nCol * nPerCol + nLastRow + 1, infoPtr->nItemCount);
997 TRACE(" list=%s\n", debugrange(&item_range));
998 ranges_add(i->ranges, item_range);
1002 return TRUE;
1005 /***
1006 * Creates an iterator over the items which intersect the visible region of hdc.
1008 static BOOL iterator_visibleitems(ITERATOR* i, LISTVIEW_INFO *infoPtr, HDC hdc)
1010 POINT Origin, Position;
1011 RECT rcItem, rcClip;
1012 INT rgntype;
1014 rgntype = GetClipBox(hdc, &rcClip);
1015 if (rgntype == NULLREGION) return iterator_empty(i);
1016 if (!iterator_frameditems(i, infoPtr, &rcClip)) return FALSE;
1017 if (rgntype == SIMPLEREGION) return TRUE;
1019 /* first deal with the special item */
1020 if (LISTVIEW_GetItemBox(infoPtr, i->nSpecial, &rcItem) && !RectVisible(hdc, &rcItem))
1021 i->nSpecial = -1;
1023 /* if we can't deal with the region, we'll just go with the simple range */
1024 if (!LISTVIEW_GetOrigin(infoPtr, &Origin)) return TRUE;
1025 TRACE("building visible range:\n");
1026 if (!i->ranges)
1028 if (!(i->ranges = ranges_create(50))) return TRUE;
1029 if (!ranges_add(i->ranges, i->range))
1031 ranges_destroy(i->ranges);
1032 i->ranges = 0;
1033 return TRUE;
1037 /* now delete the invisible items from the list */
1038 while(iterator_next(i))
1040 if (!LISTVIEW_GetItemOrigin(infoPtr, i->nItem, &Position)) continue;
1041 rcItem.left = Position.x + Origin.x;
1042 rcItem.top = Position.y + Origin.y;
1043 rcItem.right = rcItem.left + infoPtr->nItemWidth;
1044 rcItem.bottom = rcItem.top + infoPtr->nItemHeight;
1045 if (!RectVisible(hdc, &rcItem))
1046 ranges_delitem(i->ranges, i->nItem);
1048 /* the iterator should restart on the next iterator_next */
1049 TRACE("done\n");
1051 return TRUE;
1054 /******** Misc helper functions ************************************/
1056 static inline LRESULT CallWindowProcT(WNDPROC proc, HWND hwnd, UINT uMsg,
1057 WPARAM wParam, LPARAM lParam, BOOL isW)
1059 if (isW) return CallWindowProcW(proc, hwnd, uMsg, wParam, lParam);
1060 else return CallWindowProcA(proc, hwnd, uMsg, wParam, lParam);
1063 /******** Internal API functions ************************************/
1065 /* The Invalidate* are macros, so we preserve the caller location */
1066 #define LISTVIEW_InvalidateRect(infoPtr, rect) while(infoPtr->bRedraw) { \
1067 TRACE(" invalidating rect=%s\n", debugrect(rect)); \
1068 InvalidateRect(infoPtr->hwndSelf, rect, TRUE); \
1069 break;\
1072 #define LISTVIEW_InvalidateItem(infoPtr, nItem) do { \
1073 RECT rcBox; \
1074 if(LISTVIEW_GetItemBox(infoPtr, nItem, &rcBox)) \
1075 LISTVIEW_InvalidateRect(infoPtr, &rcBox); \
1076 } while (0)
1078 #define LISTVIEW_InvalidateSubItem(infoPtr, nItem, nSubItem) do { \
1079 POINT Origin, Position; \
1080 RECT rcBox; \
1081 if (LISTVIEW_GetOrigin(infoPtr, &Origin) && \
1082 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position) && \
1083 LISTVIEW_GetHeaderRect(infoPtr, nSubItem, &rcBox)) { \
1084 OffsetRect(&rcBox, Origin.x + Position.x, Origin.y + Position.y); \
1085 LISTVIEW_InvalidateRect(infoPtr, &rcBox); \
1087 } while (0)
1089 #define LISTVIEW_InvalidateList(infoPtr)\
1090 LISTVIEW_InvalidateRect(infoPtr, NULL)
1093 static inline BOOL LISTVIEW_GetHeaderRect(LISTVIEW_INFO *infoPtr, INT nSubItem, RECT *lprc)
1095 COLUMN_INFO *columnInfo;
1097 columnInfo = (COLUMN_INFO *)DPA_GetPtr(infoPtr->hdpaColumns, nSubItem);
1098 if (!columnInfo) return FALSE;
1099 *lprc = columnInfo->rcHeader;
1100 return TRUE;
1103 static inline BOOL LISTVIEW_GetItemW(LISTVIEW_INFO *infoPtr, LPLVITEMW lpLVItem)
1105 return LISTVIEW_GetItemT(infoPtr, lpLVItem, TRUE);
1108 /***
1109 * DESCRIPTION:
1110 * Retrieves the number of items that can fit vertically in the client area.
1112 * PARAMETER(S):
1113 * [I] infoPtr : valid pointer to the listview structure
1115 * RETURN:
1116 * Number of items per row.
1118 static inline INT LISTVIEW_GetCountPerRow(LISTVIEW_INFO *infoPtr)
1120 INT nListWidth = infoPtr->rcList.right - infoPtr->rcList.left;
1122 return max(nListWidth/infoPtr->nItemWidth, 1);
1125 /***
1126 * DESCRIPTION:
1127 * Retrieves the number of items that can fit horizontally in the client
1128 * area.
1130 * PARAMETER(S):
1131 * [I] infoPtr : valid pointer to the listview structure
1133 * RETURN:
1134 * Number of items per column.
1136 static inline INT LISTVIEW_GetCountPerColumn(LISTVIEW_INFO *infoPtr)
1138 INT nListHeight = infoPtr->rcList.bottom - infoPtr->rcList.top;
1140 return max(nListHeight / infoPtr->nItemHeight, 1);
1144 /*************************************************************************
1145 * LISTVIEW_ProcessLetterKeys
1147 * Processes keyboard messages generated by pressing the letter keys
1148 * on the keyboard.
1149 * What this does is perform a case insensitive search from the
1150 * current position with the following quirks:
1151 * - If two chars or more are pressed in quick succession we search
1152 * for the corresponding string (e.g. 'abc').
1153 * - If there is a delay we wipe away the current search string and
1154 * restart with just that char.
1155 * - If the user keeps pressing the same character, whether slowly or
1156 * fast, so that the search string is entirely composed of this
1157 * character ('aaaaa' for instance), then we search for first item
1158 * that starting with that character.
1159 * - If the user types the above character in quick succession, then
1160 * we must also search for the corresponding string ('aaaaa'), and
1161 * go to that string if there is a match.
1163 * PARAMETERS
1164 * [I] hwnd : handle to the window
1165 * [I] charCode : the character code, the actual character
1166 * [I] keyData : key data
1168 * RETURNS
1170 * Zero.
1172 * BUGS
1174 * - The current implementation has a list of characters it will
1175 * accept and it ignores averything else. In particular it will
1176 * ignore accentuated characters which seems to match what
1177 * Windows does. But I'm not sure it makes sense to follow
1178 * Windows there.
1179 * - We don't sound a beep when the search fails.
1181 * SEE ALSO
1183 * TREEVIEW_ProcessLetterKeys
1185 static INT LISTVIEW_ProcessLetterKeys(LISTVIEW_INFO *infoPtr, WPARAM charCode, LPARAM keyData)
1187 INT nItem;
1188 INT endidx,idx;
1189 LVITEMW item;
1190 WCHAR buffer[MAX_PATH];
1191 DWORD lastKeyPressTimestamp = infoPtr->lastKeyPressTimestamp;
1193 /* simple parameter checking */
1194 if (!charCode || !keyData) return 0;
1196 /* only allow the valid WM_CHARs through */
1197 if (!isalnum(charCode) &&
1198 charCode != '.' && charCode != '`' && charCode != '!' &&
1199 charCode != '@' && charCode != '#' && charCode != '$' &&
1200 charCode != '%' && charCode != '^' && charCode != '&' &&
1201 charCode != '*' && charCode != '(' && charCode != ')' &&
1202 charCode != '-' && charCode != '_' && charCode != '+' &&
1203 charCode != '=' && charCode != '\\'&& charCode != ']' &&
1204 charCode != '}' && charCode != '[' && charCode != '{' &&
1205 charCode != '/' && charCode != '?' && charCode != '>' &&
1206 charCode != '<' && charCode != ',' && charCode != '~')
1207 return 0;
1209 /* if there's one item or less, there is no where to go */
1210 if (infoPtr->nItemCount <= 1) return 0;
1212 /* update the search parameters */
1213 infoPtr->lastKeyPressTimestamp = GetTickCount();
1214 if (infoPtr->lastKeyPressTimestamp - lastKeyPressTimestamp < KEY_DELAY) {
1215 if (infoPtr->nSearchParamLength < MAX_PATH)
1216 infoPtr->szSearchParam[infoPtr->nSearchParamLength++]=charCode;
1217 if (infoPtr->charCode != charCode)
1218 infoPtr->charCode = charCode = 0;
1219 } else {
1220 infoPtr->charCode=charCode;
1221 infoPtr->szSearchParam[0]=charCode;
1222 infoPtr->nSearchParamLength=1;
1223 /* Redundant with the 1 char string */
1224 charCode=0;
1227 /* and search from the current position */
1228 nItem=-1;
1229 if (infoPtr->nFocusedItem >= 0) {
1230 endidx=infoPtr->nFocusedItem;
1231 idx=endidx;
1232 /* if looking for single character match,
1233 * then we must always move forward
1235 if (infoPtr->nSearchParamLength == 1)
1236 idx++;
1237 } else {
1238 endidx=infoPtr->nItemCount;
1239 idx=0;
1241 do {
1242 if (idx == infoPtr->nItemCount) {
1243 if (endidx == infoPtr->nItemCount || endidx == 0)
1244 break;
1245 idx=0;
1248 /* get item */
1249 item.mask = LVIF_TEXT;
1250 item.iItem = idx;
1251 item.iSubItem = 0;
1252 item.pszText = buffer;
1253 item.cchTextMax = MAX_PATH;
1254 if (!LISTVIEW_GetItemW(infoPtr, &item)) return 0;
1256 /* check for a match */
1257 if (lstrncmpiW(item.pszText,infoPtr->szSearchParam,infoPtr->nSearchParamLength) == 0) {
1258 nItem=idx;
1259 break;
1260 } else if ( (charCode != 0) && (nItem == -1) && (nItem != infoPtr->nFocusedItem) &&
1261 (lstrncmpiW(item.pszText,infoPtr->szSearchParam,1) == 0) ) {
1262 /* This would work but we must keep looking for a longer match */
1263 nItem=idx;
1265 idx++;
1266 } while (idx != endidx);
1268 if (nItem != -1)
1269 LISTVIEW_KeySelection(infoPtr, nItem);
1271 return 0;
1274 /*************************************************************************
1275 * LISTVIEW_UpdateHeaderSize [Internal]
1277 * Function to resize the header control
1279 * PARAMS
1280 * hwnd [I] handle to a window
1281 * nNewScrollPos [I] Scroll Pos to Set
1283 * RETURNS
1284 * nothing
1286 * NOTES
1288 static void LISTVIEW_UpdateHeaderSize(LISTVIEW_INFO *infoPtr, INT nNewScrollPos)
1290 RECT winRect;
1291 POINT point[2];
1293 TRACE("nNewScrollPos=%d\n", nNewScrollPos);
1295 GetWindowRect(infoPtr->hwndHeader, &winRect);
1296 point[0].x = winRect.left;
1297 point[0].y = winRect.top;
1298 point[1].x = winRect.right;
1299 point[1].y = winRect.bottom;
1301 MapWindowPoints(HWND_DESKTOP, infoPtr->hwndSelf, point, 2);
1302 point[0].x = -nNewScrollPos;
1303 point[1].x += nNewScrollPos;
1305 SetWindowPos(infoPtr->hwndHeader,0,
1306 point[0].x,point[0].y,point[1].x,point[1].y,
1307 SWP_NOZORDER | SWP_NOACTIVATE);
1310 /***
1311 * DESCRIPTION:
1312 * Update the scrollbars. This functions should be called whenever
1313 * the content, size or view changes.
1315 * PARAMETER(S):
1316 * [I] infoPtr : valid pointer to the listview structure
1318 * RETURN:
1319 * None
1321 static void LISTVIEW_UpdateScroll(LISTVIEW_INFO *infoPtr)
1323 LONG lStyle = infoPtr->dwStyle;
1324 UINT uView = lStyle & LVS_TYPEMASK;
1325 INT nListHeight = infoPtr->rcList.bottom - infoPtr->rcList.top;
1326 INT nListWidth = infoPtr->rcList.right - infoPtr->rcList.left;
1327 SCROLLINFO scrollInfo;
1329 if (lStyle & LVS_NOSCROLL) return;
1331 scrollInfo.cbSize = sizeof(SCROLLINFO);
1333 if (uView == LVS_LIST)
1335 /* update horizontal scrollbar */
1336 INT nCountPerColumn = LISTVIEW_GetCountPerColumn(infoPtr);
1337 INT nCountPerRow = LISTVIEW_GetCountPerRow(infoPtr);
1339 TRACE("items=%d, perColumn=%d, perRow=%d\n",
1340 infoPtr->nItemCount, nCountPerColumn, nCountPerRow);
1342 scrollInfo.nMin = 0;
1343 scrollInfo.nMax = infoPtr->nItemCount / nCountPerColumn;
1344 if((infoPtr->nItemCount % nCountPerColumn) == 0)
1345 scrollInfo.nMax--;
1346 if (scrollInfo.nMax < 0) scrollInfo.nMax = 0;
1347 scrollInfo.nPos = ListView_GetTopIndex(infoPtr->hwndSelf) / nCountPerColumn;
1348 scrollInfo.nPage = nCountPerRow;
1349 scrollInfo.fMask = SIF_RANGE | SIF_POS | SIF_PAGE;
1350 SetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo, TRUE);
1351 ShowScrollBar(infoPtr->hwndSelf, SB_VERT, FALSE);
1353 else if (uView == LVS_REPORT)
1355 BOOL test;
1357 /* update vertical scrollbar */
1358 scrollInfo.nMin = 0;
1359 scrollInfo.nMax = infoPtr->nItemCount - 1;
1360 scrollInfo.nPos = ListView_GetTopIndex(infoPtr->hwndSelf);
1361 scrollInfo.nPage = LISTVIEW_GetCountPerColumn(infoPtr);
1362 scrollInfo.fMask = SIF_RANGE | SIF_POS | SIF_PAGE;
1363 test = (scrollInfo.nMin >= scrollInfo.nMax - max((INT)scrollInfo.nPage - 1, 0));
1364 TRACE("LVS_REPORT Vert. nMax=%d, nPage=%d, test=%d\n",
1365 scrollInfo.nMax, scrollInfo.nPage, test);
1366 SetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo, TRUE);
1367 ShowScrollBar(infoPtr->hwndSelf, SB_VERT, (test) ? FALSE : TRUE);
1369 /* update horizontal scrollbar */
1370 nListWidth = infoPtr->rcList.right - infoPtr->rcList.left;
1371 scrollInfo.fMask = SIF_POS;
1372 if (!GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo)
1373 || infoPtr->nItemCount == 0)
1375 scrollInfo.nPos = 0;
1377 scrollInfo.nMin = 0;
1378 scrollInfo.nMax = max(infoPtr->nItemWidth, 0)-1;
1379 scrollInfo.nPage = nListWidth;
1380 scrollInfo.fMask = SIF_RANGE | SIF_POS | SIF_PAGE ;
1381 test = (scrollInfo.nMin >= scrollInfo.nMax - max((INT)scrollInfo.nPage - 1, 0));
1382 TRACE("LVS_REPORT Horz. nMax=%d, nPage=%d, test=%d\n",
1383 scrollInfo.nMax, scrollInfo.nPage, test);
1384 SetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo, TRUE);
1385 ShowScrollBar(infoPtr->hwndSelf, SB_HORZ, (test) ? FALSE : TRUE);
1387 /* Update the Header Control */
1388 scrollInfo.fMask = SIF_POS;
1389 GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo);
1390 LISTVIEW_UpdateHeaderSize(infoPtr, scrollInfo.nPos);
1393 else
1395 RECT rcView;
1397 if (LISTVIEW_GetViewRect(infoPtr, &rcView))
1399 INT nViewWidth = rcView.right - rcView.left;
1400 INT nViewHeight = rcView.bottom - rcView.top;
1402 /* Update Horizontal Scrollbar */
1403 scrollInfo.fMask = SIF_POS;
1404 if (!GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo)
1405 || infoPtr->nItemCount == 0)
1407 scrollInfo.nPos = 0;
1409 scrollInfo.nMin = 0;
1410 scrollInfo.nMax = max(nViewWidth, 0)-1;
1411 scrollInfo.nPage = nListWidth;
1412 scrollInfo.fMask = SIF_RANGE | SIF_POS | SIF_PAGE;
1413 TRACE("LVS_ICON/SMALLICON Horz.\n");
1414 SetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo, TRUE);
1416 /* Update Vertical Scrollbar */
1417 nListHeight = infoPtr->rcList.bottom - infoPtr->rcList.top;
1418 scrollInfo.fMask = SIF_POS;
1419 if (!GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo)
1420 || infoPtr->nItemCount == 0)
1422 scrollInfo.nPos = 0;
1424 scrollInfo.nMin = 0;
1425 scrollInfo.nMax = max(nViewHeight,0)-1;
1426 scrollInfo.nPage = nListHeight;
1427 scrollInfo.fMask = SIF_RANGE | SIF_POS | SIF_PAGE;
1428 TRACE("LVS_ICON/SMALLICON Vert.\n");
1429 SetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo, TRUE);
1435 /***
1436 * DESCRIPTION:
1437 * Shows/hides the focus rectangle.
1439 * PARAMETER(S):
1440 * [I] infoPtr : valid pointer to the listview structure
1441 * [I] fShow : TRUE to show the focus, FALSE to hide it.
1443 * RETURN:
1444 * None
1446 static void LISTVIEW_ShowFocusRect(LISTVIEW_INFO *infoPtr, BOOL fShow)
1448 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
1449 HDC hdc;
1451 TRACE("fShow=%d, nItem=%d\n", fShow, infoPtr->nFocusedItem);
1453 if (infoPtr->nFocusedItem < 0) return;
1455 /* we need some gymnastics in ICON mode to handle large items */
1456 if ( (infoPtr->dwStyle & LVS_TYPEMASK) == LVS_ICON )
1458 RECT rcBox;
1460 if (!LISTVIEW_GetItemBox(infoPtr, infoPtr->nFocusedItem, &rcBox))
1461 return;
1462 if ((rcBox.bottom - rcBox.top) > infoPtr->nItemHeight)
1464 LISTVIEW_InvalidateRect(infoPtr, &rcBox);
1465 return;
1469 if (!(hdc = GetDC(infoPtr->hwndSelf))) return;
1471 /* for some reason, owner draw should work only in report mode */
1472 if ((infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && (uView == LVS_REPORT))
1474 DRAWITEMSTRUCT dis;
1475 LVITEMW item;
1477 item.iItem = infoPtr->nFocusedItem;
1478 item.iSubItem = 0;
1479 item.mask = LVIF_PARAM;
1480 if (!LISTVIEW_GetItemW(infoPtr, &item)) goto done;
1482 ZeroMemory(&dis, sizeof(dis));
1483 dis.CtlType = ODT_LISTVIEW;
1484 dis.CtlID = GetWindowLongW(infoPtr->hwndSelf, GWL_ID);
1485 dis.itemID = item.iItem;
1486 dis.itemAction = ODA_FOCUS;
1487 if (fShow) dis.itemState |= ODS_FOCUS;
1488 dis.hwndItem = infoPtr->hwndSelf;
1489 dis.hDC = hdc;
1490 if (!LISTVIEW_GetItemBox(infoPtr, dis.itemID, &dis.rcItem)) goto done;
1491 dis.itemData = item.lParam;
1493 SendMessageW(GetParent(infoPtr->hwndSelf), WM_DRAWITEM, dis.CtlID, (LPARAM)&dis);
1495 else
1497 DrawFocusRect(hdc, &infoPtr->rcFocus);
1499 done:
1500 ReleaseDC(infoPtr->hwndSelf, hdc);
1503 /***
1504 * Invalidates all visible selected items.
1506 static void LISTVIEW_InvalidateSelectedItems(LISTVIEW_INFO *infoPtr)
1508 ITERATOR i;
1510 iterator_frameditems(&i, infoPtr, &infoPtr->rcList);
1511 while(iterator_next(&i))
1513 if (LISTVIEW_GetItemState(infoPtr, i.nItem, LVIS_SELECTED))
1514 LISTVIEW_InvalidateItem(infoPtr, i.nItem);
1516 iterator_destroy(&i);
1520 /***
1521 * DESCRIPTION:
1522 * Prints a message for unsupported window styles.
1523 * A kind of TODO list for window styles.
1525 * PARAMETER(S):
1526 * [I] LONG : window style
1528 * RETURN:
1529 * None
1531 static void LISTVIEW_UnsupportedStyles(LONG lStyle)
1533 if ((LVS_TYPESTYLEMASK & lStyle) == LVS_NOSCROLL)
1534 FIXME(" LVS_NOSCROLL\n");
1536 if (lStyle & LVS_NOLABELWRAP)
1537 FIXME(" LVS_NOLABELWRAP\n");
1539 if (lStyle & LVS_SORTASCENDING)
1540 FIXME(" LVS_SORTASCENDING\n");
1542 if (lStyle & LVS_SORTDESCENDING)
1543 FIXME(" LVS_SORTDESCENDING\n");
1547 /***
1548 * DESCRIPTION: [INTERNAL]
1549 * Computes an item's (left,top) corner, relative to rcView.
1550 * That is, the position has NOT been made relative to the Origin.
1551 * This is deliberate, to avoid computing the Origin over, and
1552 * over again, when this function is call in a loop. Instead,
1553 * one ca factor the computation of the Origin before the loop,
1554 * and offset the value retured by this function, on every iteration.
1556 * PARAMETER(S):
1557 * [I] infoPtr : valid pointer to the listview structure
1558 * [I] nItem : item number
1559 * [O] lpptOrig : item top, left corner
1561 * RETURN:
1562 * TRUE if computations OK
1563 * FALSE otherwise
1565 static BOOL LISTVIEW_GetItemOrigin(LISTVIEW_INFO *infoPtr, INT nItem, LPPOINT lpptPosition)
1567 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
1569 if (nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
1571 if ((uView == LVS_SMALLICON) || (uView == LVS_ICON))
1573 lpptPosition->x = (LONG)DPA_GetPtr(infoPtr->hdpaPosX, nItem);
1574 lpptPosition->y = (LONG)DPA_GetPtr(infoPtr->hdpaPosY, nItem);
1576 else if (uView == LVS_LIST)
1578 INT nCountPerColumn = LISTVIEW_GetCountPerColumn(infoPtr);
1579 lpptPosition->x = nItem / nCountPerColumn * infoPtr->nItemWidth;
1580 lpptPosition->y = nItem % nCountPerColumn * infoPtr->nItemHeight;
1582 else /* LVS_REPORT */
1584 lpptPosition->x = REPORT_MARGINX;
1585 lpptPosition->y = nItem * infoPtr->nItemHeight;
1588 return TRUE;
1591 /***
1592 * DESCRIPTION: [INTERNAL]
1593 * Compute the rectangles of an item. This is to localize all
1594 * the computations in one place. If you are not interested in some
1595 * of these values, simply pass in a NULL -- the fucntion is smart
1596 * enough to compute only what's necessary. The function computes
1597 * the standard rectangles (BOUNDS, ICON, LABEL) plus a non-standard
1598 * one, the BOX rectangle. This rectangle is very cheap to compute,
1599 * and is guaranteed to contain all the other rectangles. Computing
1600 * the ICON rect is also cheap, but all the others are potentaily
1601 * expensive. This gives an easy and effective optimization when
1602 * searching (like point inclusion, or rectangle intersection):
1603 * first test against the BOX, and if TRUE, test agains the desired
1604 * rectangle.
1605 * If the function does not have all the necessary information
1606 * to computed the requested rectangles, will crash with a
1607 * failed assertion. This is done so we catch all programming
1608 * errors, given that the function is called only from our code.
1610 * We have the following 'special' meanings for a few fields:
1611 * * If LVIS_FOCUSED is set, we assume the item has the focus
1612 * This is important in ICON mode, where it might get a larger
1613 * then usual rectange
1615 * Please note that subitem support works only in REPORT mode.
1617 * PARAMETER(S):
1618 * [I] infoPtr : valid pointer to the listview structure
1619 * [I] lpLVItem : item to compute the measures for
1620 * [O] lprcBox : ptr to Box rectangle
1621 * The internal LVIR_BOX rectangle
1622 * [0] lprcState : ptr to State icon rectangle
1623 * The internal LVIR_STATE rectangle
1624 * [O] lprcIcon : ptr to Icon rectangle
1625 * Same as LVM_GETITEMRECT with LVIR_ICON
1626 * [O] lprcLabel : ptr to Label rectangle
1627 * Same as LVM_GETITEMRECT with LVIR_LABEL
1629 * RETURN:
1630 * TRUE if computations OK
1631 * FALSE otherwise
1633 static BOOL LISTVIEW_GetItemMetrics(LISTVIEW_INFO *infoPtr, LVITEMW *lpLVItem,
1634 LPRECT lprcBox, LPRECT lprcState,
1635 LPRECT lprcIcon, LPRECT lprcLabel)
1637 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
1638 BOOL doState = FALSE, doIcon = FALSE, doLabel = FALSE, oversizedBox = FALSE;
1639 RECT Box, State, Icon, Label;
1641 TRACE("(lpLVItem=%s)\n", debuglvitem_t(lpLVItem, TRUE));
1643 /* Be smart and try to figure out the minimum we have to do */
1644 if (lpLVItem->iSubItem) assert(uView == LVS_REPORT);
1645 if (uView == LVS_ICON && (lprcBox || lprcLabel))
1647 assert((lpLVItem->mask & LVIF_STATE) && (lpLVItem->stateMask & LVIS_FOCUSED));
1648 if (lpLVItem->state & LVIS_FOCUSED) oversizedBox = doLabel = TRUE;
1650 if (lprcLabel) doLabel = TRUE;
1651 if (doLabel || lprcIcon) doIcon = TRUE;
1652 if (doIcon || lprcState) doState = TRUE;
1654 /************************************************************/
1655 /* compute the box rectangle (it should be cheap to do) */
1656 /************************************************************/
1657 if (lpLVItem->iSubItem)
1659 if (!LISTVIEW_GetHeaderRect(infoPtr, lpLVItem->iSubItem, &Box)) return FALSE;
1661 else
1663 Box.left = 0;
1664 Box.right = infoPtr->nItemWidth;
1666 Box.top = 0;
1667 Box.bottom = infoPtr->nItemHeight;
1669 /************************************************************/
1670 /* compute STATEICON bounding box */
1671 /************************************************************/
1672 if (doState)
1674 if (uView == LVS_ICON)
1676 State.left = Box.left - infoPtr->iconStateSize.cx - 2;
1677 if (infoPtr->himlNormal)
1678 State.left += (infoPtr->nItemWidth - infoPtr->iconSize.cx) / 2;
1679 State.top = Box.top + infoPtr->iconSize.cy - infoPtr->iconStateSize.cy + 4;
1681 else
1683 /* we need the ident in report mode, if we don't have it, we fail */
1684 State.left = Box.left;
1685 if (uView == LVS_REPORT)
1687 State.left += REPORT_MARGINX;
1688 if (lpLVItem->iSubItem == 0)
1690 assert(lpLVItem->mask & LVIF_INDENT);
1691 State.left += infoPtr->iconSize.cx * lpLVItem->iIndent;
1694 State.top = Box.top;
1696 State.right = State.left;
1697 State.bottom = State.top;
1698 if (infoPtr->himlState && lpLVItem->iSubItem == 0)
1700 State.right += infoPtr->iconStateSize.cx;
1701 State.bottom += infoPtr->iconStateSize.cy;
1703 if (lprcState) *lprcState = State;
1704 TRACE(" - state=%s\n", debugrect(&State));
1707 /************************************************************/
1708 /* compute ICON bounding box (ala LVM_GETITEMRECT) */
1709 /************************************************************/
1710 if (doIcon)
1712 if (uView == LVS_ICON)
1714 Icon.left = Box.left;
1715 if (infoPtr->himlNormal)
1716 Icon.left += (infoPtr->nItemWidth - infoPtr->iconSize.cx) / 2;
1717 Icon.top = Box.top + ICON_TOP_PADDING;
1718 Icon.right = Icon.left;
1719 Icon.bottom = Icon.top;
1720 if (infoPtr->himlNormal)
1722 Icon.right += infoPtr->iconSize.cx;
1723 Icon.bottom += infoPtr->iconSize.cy;
1726 else /* LVS_SMALLICON, LVS_LIST or LVS_REPORT */
1728 Icon.left = State.right;
1729 if (!IsRectEmpty(&State)) Icon.left += IMAGE_PADDING;
1730 Icon.top = Box.top;
1731 Icon.right = Icon.left;
1732 /* FIXME: add suport for icons for subitems */
1733 if (infoPtr->himlSmall && lpLVItem->iSubItem == 0) Icon.right += infoPtr->iconSize.cx;
1734 Icon.bottom = Icon.top + infoPtr->nItemHeight;
1736 if(lprcIcon) *lprcIcon = Icon;
1737 TRACE(" - icon=%s\n", debugrect(&Icon));
1740 /************************************************************/
1741 /* compute LABEL bounding box (ala LVM_GETITEMRECT) */
1742 /************************************************************/
1743 if (doLabel)
1745 SIZE labelSize = { 0, 0 };
1747 /* calculate how far to the right can the label strech */
1748 Label.right = Box.right;
1749 if (uView == LVS_REPORT)
1751 if (lpLVItem->iSubItem == 0 && !LISTVIEW_GetHeaderRect(infoPtr, 0, &Label)) return FALSE;
1752 Label.right -= REPORT_MARGINX;
1755 if (lpLVItem->iSubItem || ((infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && uView == LVS_REPORT))
1757 labelSize.cx = infoPtr->nItemWidth;
1758 labelSize.cy = infoPtr->nItemHeight;
1759 goto calc_label;
1762 /* we need the text in non owner draw mode */
1763 assert(lpLVItem->mask & LVIF_TEXT);
1764 if (is_textT(lpLVItem->pszText, TRUE))
1766 HFONT hFont = infoPtr->hFont ? infoPtr->hFont : infoPtr->hDefaultFont;
1767 HDC hdc = GetDC(infoPtr->hwndSelf);
1768 HFONT hOldFont = SelectObject(hdc, hFont);
1769 UINT uFormat;
1770 RECT rcText;
1772 /* compute rough rectangle where the label will go */
1773 SetRectEmpty(&rcText);
1774 rcText.right = infoPtr->nItemWidth - TRAILING_PADDING;
1775 rcText.bottom = infoPtr->nItemHeight;
1776 if (uView == LVS_ICON)
1777 rcText.bottom -= ICON_TOP_PADDING + infoPtr->iconSize.cy + ICON_BOTTOM_PADDING;
1779 /* now figure out the flags */
1780 if (uView == LVS_ICON)
1781 uFormat = oversizedBox ? LV_FL_DT_FLAGS : LV_ML_DT_FLAGS;
1782 else
1783 uFormat = LV_SL_DT_FLAGS;
1785 DrawTextW (hdc, lpLVItem->pszText, -1, &rcText, uFormat | DT_CALCRECT);
1787 labelSize.cx = min(rcText.right - rcText.left + TRAILING_PADDING, infoPtr->nItemWidth);
1788 labelSize.cy = rcText.bottom - rcText.top;
1790 SelectObject(hdc, hOldFont);
1791 ReleaseDC(infoPtr->hwndSelf, hdc);
1794 calc_label:
1795 if (uView == LVS_ICON)
1797 Label.left = Box.left + (infoPtr->nItemWidth - labelSize.cx) / 2;
1798 Label.top = Box.top + ICON_TOP_PADDING_HITABLE +
1799 infoPtr->iconSize.cy + ICON_BOTTOM_PADDING;
1800 Label.right = Label.left + labelSize.cx;
1801 Label.bottom = Label.top + infoPtr->nItemHeight;
1802 if (!oversizedBox && labelSize.cy > infoPtr->ntmHeight)
1804 labelSize.cy = min(Box.bottom - Label.top, labelSize.cy);
1805 labelSize.cy /= infoPtr->ntmHeight;
1806 labelSize.cy = max(labelSize.cy, 1);
1807 labelSize.cy *= infoPtr->ntmHeight;
1809 Label.bottom = Label.top + labelSize.cy + HEIGHT_PADDING;
1811 else /* LVS_SMALLICON, LVS_LIST or LVS_REPORT */
1813 Label.left = Icon.right;
1814 if (!IsRectEmpty(&Icon) || !IsRectEmpty(&State)) Label.left += IMAGE_PADDING;
1815 Label.top = Box.top;
1816 Label.right = min(Label.left + labelSize.cx, Label.right);
1817 Label.bottom = Label.top + infoPtr->nItemHeight;
1820 if (lprcLabel) *lprcLabel = Label;
1821 TRACE(" - label=%s\n", debugrect(&Label));
1824 /* Fix the Box if necessary */
1825 if (lprcBox)
1827 if (oversizedBox) UnionRect(lprcBox, &Box, &Label);
1828 else *lprcBox = Box;
1830 TRACE(" - box=%s\n", debugrect(&Box));
1832 return TRUE;
1835 /***
1836 * DESCRIPTION: [INTERNAL]
1838 * PARAMETER(S):
1839 * [I] infoPtr : valid pointer to the listview structure
1840 * [I] nItem : item number
1841 * [O] lprcBox : ptr to Box rectangle
1843 * RETURN:
1844 * TRUE if computations OK
1845 * FALSE otherwise
1847 static BOOL LISTVIEW_GetItemBox(LISTVIEW_INFO *infoPtr, INT nItem, LPRECT lprcBox)
1849 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
1850 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
1851 POINT Position, Origin;
1852 LVITEMW lvItem;
1854 if (!LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position)) return FALSE;
1855 if (!LISTVIEW_GetOrigin(infoPtr, &Origin)) return FALSE;
1857 /* Be smart and try to figure out the minimum we have to do */
1858 lvItem.mask = 0;
1859 if (uView == LVS_ICON && infoPtr->bFocus && LISTVIEW_GetItemState(infoPtr, nItem, LVIS_FOCUSED))
1860 lvItem.mask |= LVIF_TEXT;
1861 lvItem.iItem = nItem;
1862 lvItem.iSubItem = 0;
1863 lvItem.pszText = szDispText;
1864 lvItem.cchTextMax = DISP_TEXT_SIZE;
1865 if (lvItem.mask && !LISTVIEW_GetItemW(infoPtr, &lvItem)) return FALSE;
1866 if (uView == LVS_ICON)
1868 lvItem.mask |= LVIF_STATE;
1869 lvItem.stateMask = LVIS_FOCUSED;
1870 lvItem.state = (lvItem.mask & LVIF_TEXT ? LVIS_FOCUSED : 0);
1872 if (!LISTVIEW_GetItemMetrics(infoPtr, &lvItem, lprcBox, 0, 0, 0)) return FALSE;
1874 OffsetRect(lprcBox, Position.x + Origin.x, Position.y + Origin.y);
1876 return TRUE;
1879 /***
1880 * DESCRIPTION:
1881 * Aligns the items with the top edge of the window.
1883 * PARAMETER(S):
1884 * [I] infoPtr : valid pointer to the listview structure
1886 * RETURN:
1887 * None
1889 static void LISTVIEW_AlignTop(LISTVIEW_INFO *infoPtr)
1891 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
1892 INT nListWidth = infoPtr->rcList.right - infoPtr->rcList.left;
1893 POINT ptItem;
1894 RECT rcView;
1895 INT i, off_x=0, off_y=0;
1897 if ((uView == LVS_SMALLICON) || (uView == LVS_ICON))
1899 /* Since SetItemPosition uses upper-left of icon, and for
1900 style=LVS_ICON the icon is not left adjusted, get the offset */
1901 if (uView == LVS_ICON)
1903 off_y = ICON_TOP_PADDING;
1904 off_x = (infoPtr->iconSpacing.cx - infoPtr->iconSize.cx) / 2;
1906 ptItem.x = off_x;
1907 ptItem.y = off_y;
1908 ZeroMemory(&rcView, sizeof(RECT));
1909 TRACE("Icon off.x=%d, off.y=%d, left=%d, right=%d\n",
1910 off_x, off_y,
1911 infoPtr->rcList.left, infoPtr->rcList.right);
1913 if (nListWidth > infoPtr->nItemWidth)
1915 for (i = 0; i < infoPtr->nItemCount; i++)
1917 if ((ptItem.x-off_x) + infoPtr->nItemWidth > nListWidth)
1919 ptItem.x = off_x;
1920 ptItem.y += infoPtr->nItemHeight;
1923 LISTVIEW_SetItemPosition(infoPtr, i, ptItem);
1924 ptItem.x += infoPtr->nItemWidth;
1925 rcView.right = max(rcView.right, ptItem.x);
1928 rcView.right -= off_x;
1929 rcView.bottom = (ptItem.y-off_y) + infoPtr->nItemHeight;
1931 else
1933 for (i = 0; i < infoPtr->nItemCount; i++)
1935 LISTVIEW_SetItemPosition(infoPtr, i, ptItem);
1936 ptItem.y += infoPtr->nItemHeight;
1939 rcView.right = infoPtr->nItemWidth;
1940 rcView.bottom = ptItem.y-off_y;
1943 infoPtr->rcView = rcView;
1947 /***
1948 * DESCRIPTION:
1949 * Aligns the items with the left edge of the window.
1951 * PARAMETER(S):
1952 * [I] infoPtr : valid pointer to the listview structure
1954 * RETURN:
1955 * None
1957 static void LISTVIEW_AlignLeft(LISTVIEW_INFO *infoPtr)
1959 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
1960 INT nListHeight = infoPtr->rcList.bottom - infoPtr->rcList.top;
1961 POINT ptItem;
1962 RECT rcView;
1963 INT i, off_x=0, off_y=0;
1965 if ((uView == LVS_SMALLICON) || (uView == LVS_ICON))
1967 /* Since SetItemPosition uses upper-left of icon, and for
1968 style=LVS_ICON the icon is not left adjusted, get the offset */
1969 if (uView == LVS_ICON)
1971 off_y = ICON_TOP_PADDING;
1972 off_x = (infoPtr->iconSpacing.cx - infoPtr->iconSize.cx) / 2;
1974 ptItem.x = off_x;
1975 ptItem.y = off_y;
1976 ZeroMemory(&rcView, sizeof(RECT));
1977 TRACE("Icon off.x=%d, off.y=%d\n", off_x, off_y);
1979 if (nListHeight > infoPtr->nItemHeight)
1981 for (i = 0; i < infoPtr->nItemCount; i++)
1983 if (ptItem.y + infoPtr->nItemHeight > nListHeight)
1985 ptItem.y = off_y;
1986 ptItem.x += infoPtr->nItemWidth;
1989 LISTVIEW_SetItemPosition(infoPtr, i, ptItem);
1990 ptItem.y += infoPtr->nItemHeight;
1991 rcView.bottom = max(rcView.bottom, ptItem.y);
1994 rcView.right = ptItem.x + infoPtr->nItemWidth;
1996 else
1998 for (i = 0; i < infoPtr->nItemCount; i++)
2000 LISTVIEW_SetItemPosition(infoPtr, i, ptItem);
2001 ptItem.x += infoPtr->nItemWidth;
2004 rcView.bottom = infoPtr->nItemHeight;
2005 rcView.right = ptItem.x;
2008 infoPtr->rcView = rcView;
2013 /***
2014 * DESCRIPTION:
2015 * Retrieves the bounding rectangle of all the items.
2017 * PARAMETER(S):
2018 * [I] infoPtr : valid pointer to the listview structure
2019 * [O] lprcView : bounding rectangle
2021 * RETURN:
2022 * SUCCESS : TRUE
2023 * FAILURE : FALSE
2025 static BOOL LISTVIEW_GetViewRect(LISTVIEW_INFO *infoPtr, LPRECT lprcView)
2027 POINT ptOrigin;
2029 TRACE("(lprcView=%p)\n", lprcView);
2031 if (!lprcView) return FALSE;
2033 if (!LISTVIEW_GetOrigin(infoPtr, &ptOrigin)) return FALSE;
2035 *lprcView = infoPtr->rcView;
2036 OffsetRect(lprcView, ptOrigin.x, ptOrigin.y);
2038 TRACE("lprcView=%s\n", debugrect(lprcView));
2040 return TRUE;
2043 /***
2044 * DESCRIPTION:
2045 * Retrieves the subitem pointer associated with the subitem index.
2047 * PARAMETER(S):
2048 * [I] HDPA : DPA handle for a specific item
2049 * [I] INT : index of subitem
2051 * RETURN:
2052 * SUCCESS : subitem pointer
2053 * FAILURE : NULL
2055 static LISTVIEW_SUBITEM* LISTVIEW_GetSubItemPtr(HDPA hdpaSubItems, INT nSubItem)
2057 LISTVIEW_SUBITEM *lpSubItem;
2058 INT i;
2060 /* we should binary search here if need be */
2061 for (i = 1; i < hdpaSubItems->nItemCount; i++)
2063 lpSubItem = (LISTVIEW_SUBITEM *) DPA_GetPtr(hdpaSubItems, i);
2064 if (lpSubItem && (lpSubItem->iSubItem == nSubItem))
2065 return lpSubItem;
2068 return NULL;
2072 /***
2073 * DESCRIPTION:
2074 * Calculates the width of a specific item.
2076 * PARAMETER(S):
2077 * [I] infoPtr : valid pointer to the listview structure
2078 * [I] nItem : item to calculate width, or -1 for max of all
2080 * RETURN:
2081 * Returns the width of an item width an item.
2083 static INT LISTVIEW_CalculateItemWidth(LISTVIEW_INFO *infoPtr, INT nItem)
2085 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
2086 INT nItemWidth = 0, i;
2088 if (uView == LVS_ICON)
2089 nItemWidth = infoPtr->iconSpacing.cx;
2090 else if (uView == LVS_REPORT)
2092 RECT rcHeaderItem;
2094 /* calculate width of header */
2095 for (i = 0; i < infoPtr->hdpaColumns->nItemCount; i++)
2096 if (LISTVIEW_GetHeaderRect(infoPtr, i, &rcHeaderItem))
2097 nItemWidth += (rcHeaderItem.right - rcHeaderItem.left);
2099 else
2101 INT nLabelWidth;
2103 if (infoPtr->nItemCount == 0) return DEFAULT_COLUMN_WIDTH;
2105 /* get width of string */
2106 if (nItem == -1)
2108 for (i = 0; i < infoPtr->nItemCount; i++)
2110 nLabelWidth = LISTVIEW_GetLabelWidth(infoPtr, i);
2111 nItemWidth = max(nItemWidth, nLabelWidth);
2114 else
2115 nItemWidth = LISTVIEW_GetLabelWidth(infoPtr, nItem);
2116 if (!nItemWidth) return DEFAULT_COLUMN_WIDTH;
2117 nItemWidth += WIDTH_PADDING;
2118 if (infoPtr->himlSmall) nItemWidth += infoPtr->iconSize.cx;
2119 if (infoPtr->himlState) nItemWidth += infoPtr->iconStateSize.cx;
2120 if (nItem == -1) nItemWidth = max(DEFAULT_COLUMN_WIDTH, nItemWidth);
2123 return max(nItemWidth, 1);
2126 /***
2127 * DESCRIPTION:
2128 * Calculates the max width of any item in the list.
2130 * PARAMETER(S):
2131 * [I] infoPtr : valid pointer to the listview structure
2132 * [I] LONG : window style
2134 * RETURN:
2135 * Returns item width.
2137 static inline INT LISTVIEW_CalculateMaxWidth(LISTVIEW_INFO *infoPtr)
2139 return LISTVIEW_CalculateItemWidth(infoPtr, -1);
2142 /***
2143 * DESCRIPTION:
2144 * Retrieves and saves important text metrics info for the current
2145 * Listview font.
2147 * PARAMETER(S):
2148 * [I] infoPtr : valid pointer to the listview structure
2151 static void LISTVIEW_SaveTextMetrics(LISTVIEW_INFO *infoPtr)
2153 HDC hdc = GetDC(infoPtr->hwndSelf);
2154 HFONT hFont = infoPtr->hFont ? infoPtr->hFont : infoPtr->hDefaultFont;
2155 HFONT hOldFont = SelectObject(hdc, hFont);
2156 TEXTMETRICW tm;
2158 if (GetTextMetricsW(hdc, &tm))
2159 infoPtr->ntmHeight = tm.tmHeight;
2160 SelectObject(hdc, hOldFont);
2161 ReleaseDC(infoPtr->hwndSelf, hdc);
2163 TRACE("tmHeight=%d\n", infoPtr->ntmHeight);
2167 /***
2168 * DESCRIPTION:
2169 * Calculates the height of an item.
2171 * PARAMETER(S):
2172 * [I] infoPtr : valid pointer to the listview structure
2174 * RETURN:
2175 * Returns item height.
2177 static INT LISTVIEW_CalculateMaxHeight(LISTVIEW_INFO *infoPtr)
2179 INT nItemHeight;
2181 if ((infoPtr->dwStyle & LVS_TYPEMASK) == LVS_ICON)
2182 nItemHeight = infoPtr->iconSpacing.cy;
2183 else
2185 nItemHeight = infoPtr->ntmHeight;
2186 if (infoPtr->himlState)
2187 nItemHeight = max(nItemHeight, infoPtr->iconStateSize.cy);
2188 if (infoPtr->himlSmall)
2189 nItemHeight = max(nItemHeight, infoPtr->iconSize.cy);
2190 if (infoPtr->himlState || infoPtr->himlSmall)
2191 nItemHeight += HEIGHT_PADDING;
2193 return nItemHeight;
2196 /***
2197 * DESCRIPTION:
2198 * A compare function for ranges
2200 * PARAMETER(S)
2201 * [I] range1 : pointer to range 1;
2202 * [I] range2 : pointer to range 2;
2203 * [I] flags : flags
2205 * RETURNS:
2206 * > 0 : if range 1 > range 2
2207 * < 0 : if range 2 > range 1
2208 * = 0 : if range intersects range 2
2210 static INT CALLBACK ranges_cmp(LPVOID range1, LPVOID range2, LPARAM flags)
2212 INT cmp;
2214 if (((RANGE*)range1)->upper <= ((RANGE*)range2)->lower)
2215 cmp = -1;
2216 else if (((RANGE*)range2)->upper <= ((RANGE*)range1)->lower)
2217 cmp = 1;
2218 else
2219 cmp = 0;
2221 TRACE("range1=%s, range2=%s, cmp=%d\n", debugrange((RANGE*)range1), debugrange((RANGE*)range2), cmp);
2223 return cmp;
2226 #if DEBUG_RANGES
2227 #define ranges_check ranges_assert
2228 #else
2229 #define ranges_check(ranges, desc) do { } while(0)
2230 #endif
2232 static void ranges_assert(RANGES ranges, LPCSTR desc)
2234 INT i;
2235 RANGE *prev, *curr;
2237 assert (ranges);
2238 assert (ranges->hdpa->nItemCount >= 0);
2239 if (ranges->hdpa->nItemCount == 0) return;
2240 TRACE("*** Checking %s ***\n", desc);
2241 ranges_dump(ranges);
2242 assert ((prev = (RANGE *)DPA_GetPtr(ranges->hdpa, 0))->lower >= 0);
2243 /* assert (((RANGE *)DPA_GetPtr(ranges->hdpa, ranges->hdpa->nItemCount - 1))->upper <= nUpper); */
2244 for (i = 1; i < ranges->hdpa->nItemCount; i++)
2246 curr = (RANGE *)DPA_GetPtr(ranges->hdpa, i);
2247 assert (prev->upper <= curr->lower);
2248 prev = curr;
2250 TRACE("--- Done checking---\n");
2253 static RANGES ranges_create(int count)
2255 RANGES ranges = (RANGES)COMCTL32_Alloc(sizeof(struct tagRANGES));
2256 if (!ranges) return NULL;
2257 ranges->hdpa = DPA_Create(count);
2258 if (ranges->hdpa) return ranges;
2259 COMCTL32_Free(ranges);
2260 return NULL;
2263 static void ranges_destroy(RANGES ranges)
2265 if (!ranges) return;
2266 if (ranges->hdpa)
2268 INT i;
2270 for(i = 0; i < ranges->hdpa->nItemCount; i++)
2271 COMCTL32_Free(DPA_GetPtr(ranges->hdpa, i));
2272 DPA_Destroy(ranges->hdpa);
2273 ranges->hdpa = NULL;
2275 COMCTL32_Free(ranges);
2278 static RANGES ranges_clone(RANGES ranges)
2280 RANGES clone;
2281 INT i;
2283 if (!ranges) return NULL;
2284 clone = ranges_create(ranges->hdpa->nItemCount);
2285 if (!clone) return NULL;
2287 for (i = 0; i < ranges->hdpa->nItemCount; i++)
2289 RANGE *newrng = (RANGE *)COMCTL32_Alloc(sizeof(RANGE));
2290 if (!newrng)
2292 ranges_destroy(clone);
2293 return NULL;
2295 *newrng = *((RANGE*)DPA_GetPtr(ranges->hdpa, i));
2296 DPA_InsertPtr(clone->hdpa, i, newrng);
2298 return clone;
2301 static RANGES ranges_diff(RANGES ranges, RANGES sub)
2303 INT i;
2305 if (!ranges || !sub) return ranges;
2307 for (i = 0; i < sub->hdpa->nItemCount; i++)
2308 ranges_del(ranges, *((RANGE *)DPA_GetPtr(sub->hdpa, i)));
2310 return ranges;
2313 static void ranges_dump(RANGES ranges)
2315 INT i;
2317 if (!ranges) return;
2318 for (i = 0; i < ranges->hdpa->nItemCount; i++)
2319 TRACE(" %s\n", debugrange(DPA_GetPtr(ranges->hdpa, i)));
2322 static inline BOOL ranges_contain(RANGES ranges, INT nItem)
2324 RANGE srchrng = { nItem, nItem + 1 };
2326 TRACE("(nItem=%d)\n", nItem);
2327 if (!ranges) return FALSE;
2328 if (TRACE_ON(listview)) ranges_dump(ranges);
2329 ranges_check(ranges, "before contain");
2330 return DPA_Search(ranges->hdpa, &srchrng, 0, ranges_cmp, 0, DPAS_SORTED) != -1;
2333 static INT ranges_itemcount(RANGES ranges)
2335 INT i, count = 0;
2337 if (!ranges) return 0;
2338 for (i = 0; i < ranges->hdpa->nItemCount; i++)
2340 RANGE *sel = DPA_GetPtr(ranges->hdpa, i);
2341 count += sel->upper - sel->lower;
2344 return count;
2347 static BOOL ranges_shift(RANGES ranges, INT nItem, INT delta, INT nUpper)
2349 RANGE srchrng = { nItem, nItem + 1 }, *chkrng;
2350 INT index;
2352 if (!ranges) return FALSE;
2353 index = DPA_Search(ranges->hdpa, &srchrng, 0, ranges_cmp, 0, DPAS_SORTED | DPAS_INSERTAFTER);
2354 if (index == -1) return TRUE;
2356 for (;index < ranges->hdpa->nItemCount; index++)
2358 chkrng = DPA_GetPtr(ranges->hdpa, index);
2359 if (chkrng->lower >= nItem)
2360 chkrng->lower = max(min(chkrng->lower + delta, nUpper - 1), 0);
2361 if (chkrng->upper > nItem)
2362 chkrng->upper = max(min(chkrng->upper + delta, nUpper), 0);
2364 return TRUE;
2367 static BOOL ranges_add(RANGES ranges, RANGE range)
2369 RANGE srchrgn;
2370 INT index;
2372 TRACE("(%s)\n", debugrange(&range));
2373 ranges_check(ranges, "before add");
2374 if (!ranges) goto fail;
2376 /* try find overlapping regions first */
2377 srchrgn.lower = range.lower - 1;
2378 srchrgn.upper = range.upper + 1;
2379 index = DPA_Search(ranges->hdpa, &srchrgn, 0, ranges_cmp, 0, DPAS_SORTED);
2381 if (index == -1)
2383 RANGE *newrgn;
2385 TRACE("Adding new range\n");
2387 /* create the brand new range to insert */
2388 newrgn = (RANGE *)COMCTL32_Alloc(sizeof(RANGE));
2389 if(!newrgn) goto fail;
2390 *newrgn = range;
2392 /* figure out where to insert it */
2393 index = DPA_Search(ranges->hdpa, newrgn, 0, ranges_cmp, 0, DPAS_SORTED | DPAS_INSERTAFTER);
2394 TRACE("index=%d\n", index);
2395 if (index == -1) index = 0;
2397 /* and get it over with */
2398 DPA_InsertPtr(ranges->hdpa, index, newrgn);
2400 else
2402 RANGE *chkrgn, *mrgrgn;
2403 INT fromindex, mergeindex;
2405 chkrgn = DPA_GetPtr(ranges->hdpa, index);
2406 if (!chkrgn) goto fail;
2407 TRACE("Merge with %s @%d\n", debugrange(chkrgn), index);
2409 chkrgn->lower = min(range.lower, chkrgn->lower);
2410 chkrgn->upper = max(range.upper, chkrgn->upper);
2412 TRACE("New range %s @%d\n", debugrange(chkrgn), index);
2414 /* merge now common anges */
2415 fromindex = 0;
2416 srchrgn.lower = chkrgn->lower - 1;
2417 srchrgn.upper = chkrgn->upper + 1;
2421 mergeindex = DPA_Search(ranges->hdpa, &srchrgn, fromindex, ranges_cmp, 0, 0);
2422 if (mergeindex == -1) break;
2423 if (mergeindex == index)
2425 fromindex = index + 1;
2426 continue;
2429 TRACE("Merge with index %i\n", mergeindex);
2431 mrgrgn = DPA_GetPtr(ranges->hdpa, mergeindex);
2432 if (!mrgrgn) goto fail;
2434 chkrgn->lower = min(chkrgn->lower, mrgrgn->lower);
2435 chkrgn->upper = max(chkrgn->upper, mrgrgn->upper);
2436 COMCTL32_Free(mrgrgn);
2437 DPA_DeletePtr(ranges->hdpa, mergeindex);
2438 if (mergeindex < index) index --;
2439 } while(1);
2442 ranges_check(ranges, "after add");
2443 if (TRACE_ON(listview)) ranges_dump(ranges);
2444 return TRUE;
2446 fail:
2447 ranges_check(ranges, "failed add");
2448 return FALSE;
2451 static BOOL ranges_del(RANGES ranges, RANGE range)
2453 RANGE *chkrgn;
2454 INT index;
2456 TRACE("(%s)\n", debugrange(&range));
2457 ranges_check(ranges, "before del");
2458 if (!ranges) goto fail;
2460 /* we don't use DPAS_SORTED here, since we need *
2461 * to find the first overlapping range */
2462 index = DPA_Search(ranges->hdpa, &range, 0, ranges_cmp, 0, 0);
2463 while(index != -1)
2465 chkrgn = DPA_GetPtr(ranges->hdpa, index);
2466 if (!chkrgn) goto fail;
2468 TRACE("Matches range %s @%d\n", debugrange(chkrgn), index);
2470 /* case 1: Same range */
2471 if ( (chkrgn->upper == range.upper) &&
2472 (chkrgn->lower == range.lower) )
2474 DPA_DeletePtr(ranges->hdpa, index);
2475 break;
2477 /* case 2: engulf */
2478 else if ( (chkrgn->upper <= range.upper) &&
2479 (chkrgn->lower >= range.lower) )
2481 DPA_DeletePtr(ranges->hdpa, index);
2483 /* case 3: overlap upper */
2484 else if ( (chkrgn->upper <= range.upper) &&
2485 (chkrgn->lower < range.lower) )
2487 chkrgn->upper = range.lower;
2489 /* case 4: overlap lower */
2490 else if ( (chkrgn->upper > range.upper) &&
2491 (chkrgn->lower >= range.lower) )
2493 chkrgn->lower = range.upper;
2494 break;
2496 /* case 5: fully internal */
2497 else
2499 RANGE tmprgn = *chkrgn, *newrgn;
2501 if (!(newrgn = (RANGE *)COMCTL32_Alloc(sizeof(RANGE)))) goto fail;
2502 newrgn->lower = chkrgn->lower;
2503 newrgn->upper = range.lower;
2504 chkrgn->lower = range.upper;
2505 DPA_InsertPtr(ranges->hdpa, index, newrgn);
2506 chkrgn = &tmprgn;
2507 break;
2510 index = DPA_Search(ranges->hdpa, &range, index, ranges_cmp, 0, 0);
2513 ranges_check(ranges, "after del");
2514 return TRUE;
2516 fail:
2517 ranges_check(ranges, "failed del");
2518 return FALSE;
2521 /***
2522 * DESCRIPTION:
2523 * Removes all selection ranges
2525 * Parameters(s):
2526 * [I] infoPtr : valid pointer to the listview structure
2527 * [I] toSkip : item range to skip removing the selection
2529 * RETURNS:
2530 * SUCCESS : TRUE
2531 * FAILURE : TRUE
2533 static BOOL LISTVIEW_DeselectAllSkipItems(LISTVIEW_INFO *infoPtr, RANGES toSkip)
2535 LVITEMW lvItem;
2536 ITERATOR i;
2538 TRACE("()\n");
2539 if (TRACE_ON(listview)) ranges_dump(toSkip);
2541 lvItem.state = 0;
2542 lvItem.stateMask = LVIS_SELECTED;
2544 /* need to clone the DPA because callbacks can change it */
2545 iterator_ranges(&i, ranges_diff(ranges_clone(infoPtr->selectionRanges), toSkip));
2546 while(iterator_next(&i))
2547 LISTVIEW_SetItemState(infoPtr, i.nItem, &lvItem);
2548 /* note that the iterator destructor will free the cloned range */
2549 iterator_destroy(&i);
2551 return TRUE;
2554 static inline BOOL LISTVIEW_DeselectAllSkipItem(LISTVIEW_INFO *infoPtr, INT nItem)
2556 RANGES toSkip = ranges_create(1);
2558 if (!toSkip) return FALSE;
2559 if (nItem != -1) ranges_additem(toSkip, nItem);
2560 LISTVIEW_DeselectAllSkipItems(infoPtr, toSkip);
2561 ranges_destroy(toSkip);
2562 return TRUE;
2565 static inline BOOL LISTVIEW_DeselectAll(LISTVIEW_INFO *infoPtr)
2567 return LISTVIEW_DeselectAllSkipItem(infoPtr, -1);
2570 /***
2571 * DESCRIPTION:
2572 * Retrieves the number of items that are marked as selected.
2574 * PARAMETER(S):
2575 * [I] infoPtr : valid pointer to the listview structure
2577 * RETURN:
2578 * Number of items selected.
2580 static INT LISTVIEW_GetSelectedCount(LISTVIEW_INFO *infoPtr)
2582 INT nSelectedCount = 0;
2584 if (infoPtr->uCallbackMask & LVIS_SELECTED)
2586 INT i;
2587 for (i = 0; i < infoPtr->nItemCount; i++)
2589 if (LISTVIEW_GetItemState(infoPtr, i, LVIS_SELECTED))
2590 nSelectedCount++;
2593 else
2594 nSelectedCount = ranges_itemcount(infoPtr->selectionRanges);
2596 TRACE("nSelectedCount=%d\n", nSelectedCount);
2597 return nSelectedCount;
2600 /***
2601 * DESCRIPTION:
2602 * Manages the item focus.
2604 * PARAMETER(S):
2605 * [I] infoPtr : valid pointer to the listview structure
2606 * [I] INT : item index
2608 * RETURN:
2609 * TRUE : focused item changed
2610 * FALSE : focused item has NOT changed
2612 static inline BOOL LISTVIEW_SetItemFocus(LISTVIEW_INFO *infoPtr, INT nItem)
2614 INT oldFocus = infoPtr->nFocusedItem;
2615 LVITEMW lvItem;
2617 if (nItem == infoPtr->nFocusedItem) return FALSE;
2619 lvItem.state = nItem == -1 ? 0 : LVIS_FOCUSED;
2620 lvItem.stateMask = LVIS_FOCUSED;
2621 LISTVIEW_SetItemState(infoPtr, nItem, &lvItem);
2623 return oldFocus != infoPtr->nFocusedItem;
2626 /* Helper function for LISTVIEW_ShiftIndices *only* */
2627 static INT shift_item(LISTVIEW_INFO *infoPtr, INT nShiftItem, INT nItem, INT direction)
2629 if (nShiftItem < nItem) return nShiftItem;
2631 if (nShiftItem > nItem) return nShiftItem + direction;
2633 if (direction > 0) return nShiftItem + direction;
2635 return min(nShiftItem, infoPtr->nItemCount - 1);
2639 * DESCRIPTION:
2640 * Updates the various indices after an item has been inserted or deleted.
2642 * PARAMETER(S):
2643 * [I] infoPtr : valid pointer to the listview structure
2644 * [I] nItem : item index
2645 * [I] direction : Direction of shift, +1 or -1.
2647 * RETURN:
2648 * None
2650 static void LISTVIEW_ShiftIndices(LISTVIEW_INFO *infoPtr, INT nItem, INT direction)
2652 INT nNewFocus;
2654 TRACE("Shifting %iu, %i steps\n", nItem, direction);
2656 ranges_shift(infoPtr->selectionRanges, nItem, direction, infoPtr->nItemCount);
2658 assert(abs(direction) == 1);
2660 infoPtr->nSelectionMark = shift_item(infoPtr, infoPtr->nSelectionMark, nItem, direction);
2662 nNewFocus = shift_item(infoPtr, infoPtr->nFocusedItem, nItem, direction);
2663 if (nNewFocus != infoPtr->nFocusedItem)
2664 LISTVIEW_SetItemFocus(infoPtr, nNewFocus);
2666 /* But we are not supposed to modify nHotItem! */
2671 * DESCRIPTION:
2672 * Adds a block of selections.
2674 * PARAMETER(S):
2675 * [I] infoPtr : valid pointer to the listview structure
2676 * [I] INT : item index
2678 * RETURN:
2679 * None
2681 static void LISTVIEW_AddGroupSelection(LISTVIEW_INFO *infoPtr, INT nItem)
2683 INT nFirst = min(infoPtr->nSelectionMark, nItem);
2684 INT nLast = max(infoPtr->nSelectionMark, nItem);
2685 INT i;
2686 LVITEMW item;
2688 if (nFirst == -1)
2689 nFirst = nItem;
2691 item.state = LVIS_SELECTED;
2692 item.stateMask = LVIS_SELECTED;
2694 /* FIXME: this is not correct LVS_OWNERDATA
2695 * See docu for LVN_ITEMCHANGED. Is there something similar for
2696 * RemoveGroupSelection (is there such a thing?)?
2698 for (i = nFirst; i <= nLast; i++)
2699 LISTVIEW_SetItemState(infoPtr,i,&item);
2701 LISTVIEW_SetItemFocus(infoPtr, nItem);
2702 infoPtr->nSelectionMark = nItem;
2706 /***
2707 * DESCRIPTION:
2708 * Sets a single group selection.
2710 * PARAMETER(S):
2711 * [I] infoPtr : valid pointer to the listview structure
2712 * [I] INT : item index
2714 * RETURN:
2715 * None
2717 static void LISTVIEW_SetGroupSelection(LISTVIEW_INFO *infoPtr, INT nItem)
2719 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
2720 RANGES selection;
2721 LVITEMW item;
2722 ITERATOR i;
2724 if (!(selection = ranges_create(100))) return;
2726 item.state = LVIS_SELECTED;
2727 item.stateMask = LVIS_SELECTED;
2729 if ((uView == LVS_LIST) || (uView == LVS_REPORT))
2731 if (infoPtr->nSelectionMark == -1)
2733 infoPtr->nSelectionMark = nItem;
2734 ranges_additem(selection, nItem);
2736 else
2738 RANGE sel;
2740 sel.lower = min(infoPtr->nSelectionMark, nItem);
2741 sel.upper = max(infoPtr->nSelectionMark, nItem) + 1;
2742 ranges_add(selection, sel);
2745 else
2747 RECT rcItem, rcSel, rcSelMark;
2748 POINT ptItem;
2750 rcItem.left = LVIR_BOUNDS;
2751 if (!LISTVIEW_GetItemRect(infoPtr, nItem, &rcItem)) return;
2752 rcSelMark.left = LVIR_BOUNDS;
2753 if (!LISTVIEW_GetItemRect(infoPtr, infoPtr->nSelectionMark, &rcSelMark)) return;
2754 UnionRect(&rcSel, &rcItem, &rcSelMark);
2755 iterator_frameditems(&i, infoPtr, &rcSel);
2756 while(iterator_next(&i))
2758 LISTVIEW_GetItemPosition(infoPtr, i.nItem, &ptItem);
2759 if (PtInRect(&rcSel, ptItem)) ranges_additem(selection, i.nItem);
2761 iterator_destroy(&i);
2764 LISTVIEW_DeselectAllSkipItems(infoPtr, selection);
2765 iterator_ranges(&i, selection);
2766 while(iterator_next(&i))
2767 LISTVIEW_SetItemState(infoPtr, i.nItem, &item);
2768 /* this will also destroy the selection */
2769 iterator_destroy(&i);
2771 LISTVIEW_SetItemFocus(infoPtr, nItem);
2774 /***
2775 * DESCRIPTION:
2776 * Sets a single selection.
2778 * PARAMETER(S):
2779 * [I] infoPtr : valid pointer to the listview structure
2780 * [I] INT : item index
2782 * RETURN:
2783 * None
2785 static void LISTVIEW_SetSelection(LISTVIEW_INFO *infoPtr, INT nItem)
2787 LVITEMW lvItem;
2789 TRACE("nItem=%d\n", nItem);
2791 LISTVIEW_DeselectAllSkipItem(infoPtr, nItem);
2793 lvItem.state = LVIS_FOCUSED | LVIS_SELECTED;
2794 lvItem.stateMask = LVIS_FOCUSED | LVIS_SELECTED;
2795 LISTVIEW_SetItemState(infoPtr, nItem, &lvItem);
2797 infoPtr->nSelectionMark = nItem;
2800 /***
2801 * DESCRIPTION:
2802 * Set selection(s) with keyboard.
2804 * PARAMETER(S):
2805 * [I] infoPtr : valid pointer to the listview structure
2806 * [I] INT : item index
2808 * RETURN:
2809 * SUCCESS : TRUE (needs to be repainted)
2810 * FAILURE : FALSE (nothing has changed)
2812 static BOOL LISTVIEW_KeySelection(LISTVIEW_INFO *infoPtr, INT nItem)
2814 /* FIXME: pass in the state */
2815 LONG lStyle = infoPtr->dwStyle;
2816 WORD wShift = HIWORD(GetKeyState(VK_SHIFT));
2817 WORD wCtrl = HIWORD(GetKeyState(VK_CONTROL));
2818 BOOL bResult = FALSE;
2820 if ((nItem >= 0) && (nItem < infoPtr->nItemCount))
2822 if (lStyle & LVS_SINGLESEL)
2824 bResult = TRUE;
2825 LISTVIEW_SetSelection(infoPtr, nItem);
2827 else
2829 if (wShift)
2831 bResult = TRUE;
2832 LISTVIEW_SetGroupSelection(infoPtr, nItem);
2834 else if (wCtrl)
2836 bResult = LISTVIEW_SetItemFocus(infoPtr, nItem);
2838 else
2840 bResult = TRUE;
2841 LISTVIEW_SetSelection(infoPtr, nItem);
2844 ListView_EnsureVisible(infoPtr->hwndSelf, nItem, FALSE);
2847 UpdateWindow(infoPtr->hwndSelf); /* update client area */
2848 return bResult;
2852 /***
2853 * DESCRIPTION:
2854 * Called when the mouse is being actively tracked and has hovered for a specified
2855 * amount of time
2857 * PARAMETER(S):
2858 * [I] infoPtr : valid pointer to the listview structure
2859 * [I] fwKeys : key indicator
2860 * [I] pts : mouse position
2862 * RETURN:
2863 * 0 if the message was processed, non-zero if there was an error
2865 * INFO:
2866 * LVS_EX_TRACKSELECT: An item is automatically selected when the cursor remains
2867 * over the item for a certain period of time.
2870 static LRESULT LISTVIEW_MouseHover(LISTVIEW_INFO *infoPtr, WORD fwKyes, POINTS pts)
2872 if(infoPtr->dwLvExStyle & LVS_EX_TRACKSELECT)
2873 /* FIXME: select the item!!! */
2874 /*LISTVIEW_GetItemAtPt(infoPtr, pt)*/;
2876 return 0;
2879 /***
2880 * DESCRIPTION:
2881 * Called whenever WM_MOUSEMOVE is received.
2883 * PARAMETER(S):
2884 * [I] infoPtr : valid pointer to the listview structure
2885 * [I] fwKeys : key indicator
2886 * [I] pts : mouse position
2888 * RETURN:
2889 * 0 if the message is processed, non-zero if there was an error
2891 static LRESULT LISTVIEW_MouseMove(LISTVIEW_INFO *infoPtr, WORD fwKeys, POINTS pts)
2893 TRACKMOUSEEVENT trackinfo;
2895 /* see if we are supposed to be tracking mouse hovering */
2896 if(infoPtr->dwLvExStyle & LVS_EX_TRACKSELECT) {
2897 /* fill in the trackinfo struct */
2898 trackinfo.cbSize = sizeof(TRACKMOUSEEVENT);
2899 trackinfo.dwFlags = TME_QUERY;
2900 trackinfo.hwndTrack = infoPtr->hwndSelf;
2901 trackinfo.dwHoverTime = infoPtr->dwHoverTime;
2903 /* see if we are already tracking this hwnd */
2904 _TrackMouseEvent(&trackinfo);
2906 if(!(trackinfo.dwFlags & TME_HOVER)) {
2907 trackinfo.dwFlags = TME_HOVER;
2909 /* call TRACKMOUSEEVENT so we receive WM_MOUSEHOVER messages */
2910 _TrackMouseEvent(&trackinfo);
2914 return 0;
2918 /***
2919 * Tests wheather the item is assignable to a list with style lStyle
2921 static inline BOOL is_assignable_item(LPLVITEMW lpLVItem, LONG lStyle)
2923 if ( (lpLVItem->mask & LVIF_TEXT) &&
2924 (lpLVItem->pszText == LPSTR_TEXTCALLBACKW) &&
2925 (lStyle & (LVS_SORTASCENDING | LVS_SORTDESCENDING)) ) return FALSE;
2927 return TRUE;
2931 /***
2932 * DESCRIPTION:
2933 * Helper for LISTVIEW_SetItemT *only*: sets item attributes.
2935 * PARAMETER(S):
2936 * [I] infoPtr : valid pointer to the listview structure
2937 * [I] lpLVItem : valid pointer to new item atttributes
2938 * [I] isNew : the item being set is being inserted
2939 * [I] isW : TRUE if lpLVItem is Unicode, FALSE if it's ANSI
2940 * [O] bChanged : will be set to TRUE if the item really changed
2942 * RETURN:
2943 * SUCCESS : TRUE
2944 * FAILURE : FALSE
2946 static BOOL set_main_item(LISTVIEW_INFO *infoPtr, LPLVITEMW lpLVItem, BOOL isNew, BOOL isW, BOOL *bChanged)
2948 LISTVIEW_ITEM *lpItem;
2949 NMLISTVIEW nmlv;
2950 UINT uChanged = 0;
2951 LVITEMW item;
2953 TRACE("()\n");
2955 assert(lpLVItem->iItem >= 0 && lpLVItem->iItem < infoPtr->nItemCount);
2957 if (lpLVItem->mask == 0) return TRUE;
2959 if (infoPtr->dwStyle & LVS_OWNERDATA)
2961 /* a virtual listview we stores only selection and focus */
2962 if ((lpLVItem->mask & ~LVIF_STATE) || (lpLVItem->stateMask & ~(LVIS_FOCUSED | LVIS_SELECTED)))
2963 return FALSE;
2964 lpItem = NULL;
2966 else
2968 HDPA hdpaSubItems = (HDPA)DPA_GetPtr(infoPtr->hdpaItems, lpLVItem->iItem);
2969 if (!hdpaSubItems) return FALSE;
2970 if (!(lpItem = (LISTVIEW_ITEM *)DPA_GetPtr(hdpaSubItems, 0))) return FALSE;
2973 /* we need to get the lParam and state of the item */
2974 item.iItem = lpLVItem->iItem;
2975 item.iSubItem = lpLVItem->iSubItem;
2976 item.mask = LVIF_STATE | LVIF_PARAM;
2977 item.stateMask = ~0;
2978 item.state = 0;
2979 item.lParam = 0;
2980 if (!isNew && !LISTVIEW_GetItemT(infoPtr, &item, TRUE)) return FALSE;
2982 TRACE("oldState=%x, newState=%x\n", item.state, lpLVItem->state);
2983 /* determine what fields will change */
2984 if ((lpLVItem->mask & LVIF_STATE) && ((item.state ^ lpLVItem->state) & lpLVItem->stateMask & ~infoPtr->uCallbackMask))
2985 uChanged |= LVIF_STATE;
2987 if ((lpLVItem->mask & LVIF_IMAGE) && (lpItem->hdr.iImage != lpLVItem->iImage))
2988 uChanged |= LVIF_IMAGE;
2990 if ((lpLVItem->mask & LVIF_PARAM) && (lpItem->lParam != lpLVItem->lParam))
2991 uChanged |= LVIF_PARAM;
2993 if ((lpLVItem->mask & LVIF_INDENT) && (lpItem->iIndent != lpLVItem->iIndent))
2994 uChanged |= LVIF_INDENT;
2996 if ((lpLVItem->mask & LVIF_TEXT) && textcmpWT(lpItem->hdr.pszText, lpLVItem->pszText, isW))
2997 uChanged |= LVIF_TEXT;
2999 TRACE("uChanged=0x%x\n", uChanged);
3000 if (!uChanged) return TRUE;
3001 *bChanged = TRUE;
3003 ZeroMemory(&nmlv, sizeof(NMLISTVIEW));
3004 nmlv.iItem = lpLVItem->iItem;
3005 nmlv.uNewState = (item.state & ~lpLVItem->stateMask) | (lpLVItem->state & lpLVItem->stateMask);
3006 nmlv.uOldState = item.state;
3007 nmlv.uChanged = uChanged;
3008 nmlv.lParam = item.lParam;
3010 /* send LVN_ITEMCHANGING notification, if the item is not being inserted */
3011 /* and we are _NOT_ virtual (LVS_OWERNDATA) */
3012 if(lpItem && !isNew && notify_listview(infoPtr, LVN_ITEMCHANGING, &nmlv))
3013 return FALSE;
3015 /* copy information */
3016 if (lpLVItem->mask & LVIF_TEXT)
3017 textsetptrT(&lpItem->hdr.pszText, lpLVItem->pszText, isW);
3019 if (lpLVItem->mask & LVIF_IMAGE)
3020 lpItem->hdr.iImage = lpLVItem->iImage;
3022 if (lpLVItem->mask & LVIF_PARAM)
3023 lpItem->lParam = lpLVItem->lParam;
3025 if (lpLVItem->mask & LVIF_INDENT)
3026 lpItem->iIndent = lpLVItem->iIndent;
3028 if (uChanged & LVIF_STATE)
3030 if (lpLVItem->stateMask & ~infoPtr->uCallbackMask & ~(LVIS_FOCUSED | LVIS_SELECTED))
3032 lpItem->state &= ~lpLVItem->stateMask;
3033 lpItem->state |= (lpLVItem->state & lpLVItem->stateMask);
3035 if (lpLVItem->state & lpLVItem->stateMask & ~infoPtr->uCallbackMask & LVIS_SELECTED)
3037 if (infoPtr->dwStyle & LVS_SINGLESEL) LISTVIEW_DeselectAllSkipItem(infoPtr, lpLVItem->iItem);
3038 ranges_additem(infoPtr->selectionRanges, lpLVItem->iItem);
3040 else if (lpLVItem->stateMask & LVIS_SELECTED)
3041 ranges_delitem(infoPtr->selectionRanges, lpLVItem->iItem);
3043 /* if we are asked to change focus, and we manage it, do it */
3044 if (lpLVItem->state & lpLVItem->stateMask & ~infoPtr->uCallbackMask & LVIS_FOCUSED)
3046 if (lpLVItem->state & LVIS_FOCUSED)
3048 LISTVIEW_SetItemFocus(infoPtr, -1);
3049 infoPtr->nFocusedItem = lpLVItem->iItem;
3050 LISTVIEW_EnsureVisible(infoPtr, lpLVItem->iItem, FALSE);
3052 else if (infoPtr->nFocusedItem == lpLVItem->iItem)
3053 infoPtr->nFocusedItem = -1;
3057 /* if we're inserting the item, we're done */
3058 if (isNew) return TRUE;
3060 /* send LVN_ITEMCHANGED notification */
3061 if (lpLVItem->mask & LVIF_PARAM) nmlv.lParam = lpLVItem->lParam;
3062 notify_listview(infoPtr, LVN_ITEMCHANGED, &nmlv);
3064 return TRUE;
3067 /***
3068 * DESCRIPTION:
3069 * Helper for LISTVIEW_{Set,Insert}ItemT *only*: sets subitem attributes.
3071 * PARAMETER(S):
3072 * [I] infoPtr : valid pointer to the listview structure
3073 * [I] lpLVItem : valid pointer to new subitem atttributes
3074 * [I] isW : TRUE if lpLVItem is Unicode, FALSE if it's ANSI
3075 * [O] bChanged : will be set to TRUE if the item really changed
3077 * RETURN:
3078 * SUCCESS : TRUE
3079 * FAILURE : FALSE
3081 static BOOL set_sub_item(LISTVIEW_INFO *infoPtr, LPLVITEMW lpLVItem, BOOL isW, BOOL *bChanged)
3083 HDPA hdpaSubItems;
3084 LISTVIEW_SUBITEM *lpSubItem;
3086 /* we do not support subitems for virtual listviews */
3087 if (infoPtr->dwStyle & LVS_OWNERDATA) return FALSE;
3089 /* set subitem only if column is present */
3090 if (lpLVItem->iSubItem >= infoPtr->hdpaColumns->nItemCount) return FALSE;
3092 /* First do some sanity checks */
3093 if (lpLVItem->mask & ~(LVIF_TEXT | LVIF_IMAGE)) return FALSE;
3094 if (!(lpLVItem->mask & (LVIF_TEXT | LVIF_IMAGE))) return TRUE;
3096 /* get the subitem structure, and create it if not there */
3097 hdpaSubItems = (HDPA)DPA_GetPtr(infoPtr->hdpaItems, lpLVItem->iItem);
3098 if (!hdpaSubItems) return FALSE;
3100 lpSubItem = LISTVIEW_GetSubItemPtr(hdpaSubItems, lpLVItem->iSubItem);
3101 if (!lpSubItem)
3103 LISTVIEW_SUBITEM *tmpSubItem;
3104 INT i;
3106 lpSubItem = (LISTVIEW_SUBITEM *)COMCTL32_Alloc(sizeof(LISTVIEW_SUBITEM));
3107 if (!lpSubItem) return FALSE;
3108 /* we could binary search here, if need be...*/
3109 for (i = 1; i < hdpaSubItems->nItemCount; i++)
3111 tmpSubItem = (LISTVIEW_SUBITEM *)DPA_GetPtr(hdpaSubItems, i);
3112 if (tmpSubItem && tmpSubItem->iSubItem > lpLVItem->iSubItem) break;
3114 if (DPA_InsertPtr(hdpaSubItems, i, lpSubItem) == -1)
3116 COMCTL32_Free(lpSubItem);
3117 return FALSE;
3119 lpSubItem->iSubItem = lpLVItem->iSubItem;
3120 *bChanged = TRUE;
3123 if (lpLVItem->mask & LVIF_IMAGE)
3124 if (lpSubItem->hdr.iImage != lpLVItem->iImage)
3126 lpSubItem->hdr.iImage = lpLVItem->iImage;
3127 *bChanged = TRUE;
3130 if (lpLVItem->mask & LVIF_TEXT)
3131 if (lpSubItem->hdr.pszText != lpLVItem->pszText)
3133 textsetptrT(&lpSubItem->hdr.pszText, lpLVItem->pszText, isW);
3134 *bChanged = TRUE;
3137 return TRUE;
3140 /***
3141 * DESCRIPTION:
3142 * Sets item attributes.
3144 * PARAMETER(S):
3145 * [I] infoPtr : valid pointer to the listview structure
3146 * [I] LPLVITEM : new item atttributes
3147 * [I] isW : TRUE if lpLVItem is Unicode, FALSE if it's ANSI
3149 * RETURN:
3150 * SUCCESS : TRUE
3151 * FAILURE : FALSE
3153 static BOOL LISTVIEW_SetItemT(LISTVIEW_INFO *infoPtr, LPLVITEMW lpLVItem, BOOL isW)
3155 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
3156 LPWSTR pszText = NULL;
3157 BOOL bResult, bChanged = FALSE;
3159 TRACE("(lpLVItem=%s, isW=%d)\n", debuglvitem_t(lpLVItem, isW), isW);
3161 if (!lpLVItem || lpLVItem->iItem < 0 || lpLVItem->iItem >= infoPtr->nItemCount)
3162 return FALSE;
3164 /* For efficiency, we transform the lpLVItem->pszText to Unicode here */
3165 if ((lpLVItem->mask & LVIF_TEXT) && is_textW(lpLVItem->pszText))
3167 pszText = lpLVItem->pszText;
3168 lpLVItem->pszText = textdupTtoW(lpLVItem->pszText, isW);
3171 /* actually set the fields */
3172 if (!is_assignable_item(lpLVItem, infoPtr->dwStyle)) return FALSE;
3174 if (lpLVItem->iSubItem)
3175 bResult = set_sub_item(infoPtr, lpLVItem, TRUE, &bChanged);
3176 else
3177 bResult = set_main_item(infoPtr, lpLVItem, FALSE, TRUE, &bChanged);
3179 /* redraw item, if necessary */
3180 if (bChanged && !infoPtr->bIsDrawing)
3182 /* this little optimization eliminates some nasty flicker */
3183 if ( uView == LVS_REPORT && !(infoPtr->dwStyle & LVS_OWNERDRAWFIXED) &&
3184 (!(infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT) || lpLVItem->iSubItem) )
3185 LISTVIEW_InvalidateSubItem(infoPtr, lpLVItem->iItem, lpLVItem->iSubItem);
3186 else
3187 LISTVIEW_InvalidateItem(infoPtr, lpLVItem->iItem);
3189 /* restore text */
3190 if (pszText)
3192 textfreeT(lpLVItem->pszText, isW);
3193 lpLVItem->pszText = pszText;
3196 return bResult;
3199 /***
3200 * DESCRIPTION:
3201 * Retrieves the index of the item at coordinate (0, 0) of the client area.
3203 * PARAMETER(S):
3204 * [I] infoPtr : valid pointer to the listview structure
3206 * RETURN:
3207 * item index
3209 static INT LISTVIEW_GetTopIndex(LISTVIEW_INFO *infoPtr)
3211 LONG lStyle = infoPtr->dwStyle;
3212 UINT uView = lStyle & LVS_TYPEMASK;
3213 INT nItem = 0;
3214 SCROLLINFO scrollInfo;
3216 scrollInfo.cbSize = sizeof(SCROLLINFO);
3217 scrollInfo.fMask = SIF_POS;
3219 if (uView == LVS_LIST)
3221 if ((lStyle & WS_HSCROLL) && GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo))
3222 nItem = scrollInfo.nPos * LISTVIEW_GetCountPerColumn(infoPtr);
3224 else if (uView == LVS_REPORT)
3226 if ((lStyle & WS_VSCROLL) && GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo))
3227 nItem = scrollInfo.nPos;
3229 else
3231 if ((lStyle & WS_VSCROLL) && GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo))
3232 nItem = LISTVIEW_GetCountPerRow(infoPtr) * (scrollInfo.nPos / infoPtr->nItemHeight);
3235 TRACE("nItem=%d\n", nItem);
3237 return nItem;
3241 /***
3242 * DESCRIPTION:
3243 * Erases the background of the given rectangle
3245 * PARAMETER(S):
3246 * [I] infoPtr : valid pointer to the listview structure
3247 * [I] hdc : device context handle
3248 * [I] lprcBox : clipping rectangle
3250 * RETURN:
3251 * Success: TRUE
3252 * Failure: FALSE
3254 static inline BOOL LISTVIEW_FillBkgnd(LISTVIEW_INFO *infoPtr, HDC hdc, const RECT* lprcBox)
3256 if (!infoPtr->hBkBrush) return FALSE;
3258 TRACE("(hdc=%x, lprcBox=%s, hBkBrush=%x)\n", hdc, debugrect(lprcBox), infoPtr->hBkBrush);
3260 return FillRect(hdc, lprcBox, infoPtr->hBkBrush);
3263 /***
3264 * DESCRIPTION:
3265 * Draws an item.
3267 * PARAMETER(S):
3268 * [I] infoPtr : valid pointer to the listview structure
3269 * [I] hdc : device context handle
3270 * [I] nItem : item index
3271 * [I] nSubItem : subitem index
3272 * [I] pos : item position in client coordinates
3273 * [I] cdmode : custom draw mode
3275 * RETURN:
3276 * Success: TRUE
3277 * Failure: FALSE
3279 static BOOL LISTVIEW_DrawItem(LISTVIEW_INFO *infoPtr, HDC hdc, INT nItem, INT nSubItem, POINT pos, DWORD cdmode)
3281 UINT uFormat, uView = infoPtr->dwStyle & LVS_TYPEMASK;
3282 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
3283 DWORD cditemmode = CDRF_DODEFAULT;
3284 RECT* lprcFocus, rcSelect, rcBox, rcState, rcIcon, rcLabel;
3285 NMLVCUSTOMDRAW nmlvcd;
3286 HIMAGELIST himl;
3287 LVITEMW lvItem;
3289 TRACE("(hdc=%x, nItem=%d, nSubItem=%d, pos=%s)\n", hdc, nItem, nSubItem, debugpoint(&pos));
3291 /* get information needed for drawing the item */
3292 lvItem.mask = LVIF_TEXT | LVIF_IMAGE;
3293 if (nSubItem == 0) lvItem.mask |= LVIF_STATE;
3294 if (uView == LVS_REPORT) lvItem.mask |= LVIF_INDENT;
3295 lvItem.stateMask = LVIS_SELECTED | LVIS_FOCUSED | LVIS_STATEIMAGEMASK;
3296 lvItem.iItem = nItem;
3297 lvItem.iSubItem = nSubItem;
3298 lvItem.cchTextMax = DISP_TEXT_SIZE;
3299 lvItem.pszText = szDispText;
3300 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) return FALSE;
3301 TRACE(" lvItem=%s\n", debuglvitem_t(&lvItem, TRUE));
3303 /* now check if we need to update the focus rectangle */
3304 lprcFocus = infoPtr->bFocus && (lvItem.state & LVIS_FOCUSED) ? &infoPtr->rcFocus : 0;
3306 if (!lprcFocus) lvItem.state &= ~LVIS_FOCUSED;
3307 if (!LISTVIEW_GetItemMetrics(infoPtr, &lvItem, &rcBox, &rcState, &rcIcon, &rcLabel)) return FALSE;
3308 OffsetRect(&rcBox, pos.x, pos.y);
3309 OffsetRect(&rcState, pos.x, pos.y);
3310 OffsetRect(&rcIcon, pos.x, pos.y);
3311 OffsetRect(&rcLabel, pos.x, pos.y);
3312 TRACE(" rcBox=%s, rcState=%s, rcIcon=%s. rcLabel=%s\n",
3313 debugrect(&rcBox), debugrect(&rcState), debugrect(&rcIcon), debugrect(&rcLabel));
3315 /* fill in the custom draw structure */
3316 customdraw_fill(&nmlvcd, infoPtr, hdc, &rcBox);
3317 nmlvcd.nmcd.dwItemSpec = lvItem.iItem;
3318 nmlvcd.iSubItem = lvItem.iSubItem;
3319 if (lvItem.state & LVIS_SELECTED) nmlvcd.nmcd.uItemState |= CDIS_SELECTED;
3320 if (lvItem.state & LVIS_FOCUSED) nmlvcd.nmcd.uItemState |= CDIS_FOCUS;
3321 if (lvItem.iItem == infoPtr->nHotItem) nmlvcd.nmcd.uItemState |= CDIS_HOT;
3322 nmlvcd.nmcd.lItemlParam = lvItem.lParam;
3323 if ((lvItem.state & LVIS_SELECTED) &&
3324 (lvItem.iSubItem == 0 ||
3325 (uView == LVS_REPORT && (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT))))
3327 if (infoPtr->bFocus)
3329 nmlvcd.clrTextBk = comctl32_color.clrHighlight;
3330 nmlvcd.clrText = comctl32_color.clrHighlightText;
3332 else if (infoPtr->dwStyle & LVS_SHOWSELALWAYS)
3334 nmlvcd.clrTextBk = comctl32_color.clr3dFace;
3335 nmlvcd.clrText = comctl32_color.clrBtnText;
3339 if (cdmode & CDRF_NOTIFYITEMDRAW)
3340 cditemmode = notify_customdraw (infoPtr, CDDS_ITEMPREPAINT, &nmlvcd);
3341 if (cditemmode & CDRF_SKIPDEFAULT) goto postpaint;
3343 /* state icons */
3344 if (infoPtr->himlState && !IsRectEmpty(&rcState))
3346 UINT uStateImage = (lvItem.state & LVIS_STATEIMAGEMASK) >> 12;
3347 if (uStateImage)
3348 ImageList_Draw(infoPtr->himlState, uStateImage - 1, hdc, rcState.left, rcState.top, ILD_NORMAL);
3351 /* small icons */
3352 himl = (uView == LVS_ICON ? infoPtr->himlNormal : infoPtr->himlSmall);
3353 if (himl && lvItem.iImage >= 0 && !IsRectEmpty(&rcIcon))
3354 ImageList_Draw(himl, lvItem.iImage, hdc, rcIcon.left, rcIcon.top,
3355 (lvItem.state & LVIS_SELECTED) && (infoPtr->bFocus) ? ILD_SELECTED : ILD_NORMAL);
3357 /* Don't bother painting item being edited */
3358 if (infoPtr->hwndEdit && lprcFocus && nSubItem == 0) goto postpaint;
3360 /* Set the text attributes */
3361 SetBkMode(hdc, nmlvcd.clrTextBk == CLR_NONE ? TRANSPARENT : OPAQUE);
3362 if (nmlvcd.clrTextBk != CLR_NONE)
3363 SetBkColor(hdc, nmlvcd.clrTextBk == CLR_DEFAULT ? infoPtr->clrTextBkDefault : nmlvcd.clrTextBk);
3364 SetTextColor(hdc, nmlvcd.clrText);
3366 /* draw the selection background, if we're drawing the main item */
3367 if (nSubItem == 0)
3369 rcSelect = rcLabel;
3370 if (uView == LVS_REPORT && (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT))
3371 rcSelect.right = rcBox.right;
3373 if (lvItem.state & LVIS_SELECTED)
3374 ExtTextOutW(hdc, rcSelect.left, rcSelect.top, ETO_OPAQUE, &rcSelect, 0, 0, 0);
3375 if(lprcFocus) *lprcFocus = rcSelect;
3378 /* figure out the text drawing flags */
3379 uFormat = (uView == LVS_ICON ? (lprcFocus ? LV_FL_DT_FLAGS : LV_ML_DT_FLAGS) : LV_SL_DT_FLAGS);
3380 if (uView == LVS_ICON)
3381 uFormat = (lprcFocus ? LV_FL_DT_FLAGS : LV_ML_DT_FLAGS);
3382 else
3384 INT align = DT_LEFT;
3386 if (nSubItem)
3388 LVCOLUMNW lvColumn;
3389 lvColumn.mask = LVCF_FMT;
3390 LISTVIEW_GetColumnT(infoPtr, nSubItem, &lvColumn, TRUE);
3391 TRACE("lvColumn=%s\n", debuglvcolumn_t(&lvColumn, TRUE));
3392 if (lvColumn.fmt & LVCFMT_RIGHT) align = DT_RIGHT;
3393 else if (lvColumn.fmt & LVCFMT_CENTER) align = DT_CENTER;
3395 uFormat |= align;
3397 if (!(uFormat & (DT_RIGHT | DT_CENTER))) rcLabel.left += 2;
3398 DrawTextW(hdc, lvItem.pszText, -1, &rcLabel, uFormat);
3400 postpaint:
3401 if (cditemmode & CDRF_NOTIFYPOSTPAINT)
3402 notify_customdraw(infoPtr, CDDS_ITEMPOSTPAINT, &nmlvcd);
3403 return TRUE;
3406 /***
3407 * DESCRIPTION:
3408 * Draws listview items when in owner draw mode.
3410 * PARAMETER(S):
3411 * [I] infoPtr : valid pointer to the listview structure
3412 * [I] hdc : device context handle
3414 * RETURN:
3415 * None
3417 static void LISTVIEW_RefreshOwnerDraw(LISTVIEW_INFO *infoPtr, HDC hdc)
3419 UINT uID = GetWindowLongW(infoPtr->hwndSelf, GWL_ID);
3420 HWND hwndParent = GetParent(infoPtr->hwndSelf);
3421 POINT Origin, Position;
3422 DRAWITEMSTRUCT dis;
3423 LVITEMW item;
3424 ITERATOR i;
3426 TRACE("()\n");
3428 ZeroMemory(&dis, sizeof(dis));
3430 /* Get scroll info once before loop */
3431 if (!LISTVIEW_GetOrigin(infoPtr, &Origin)) return;
3433 /* figure out what we need to draw */
3434 iterator_visibleitems(&i, infoPtr, hdc);
3436 /* send cache hint notification */
3437 if (infoPtr->dwStyle & LVS_OWNERDATA)
3439 RANGE range = iterator_range(&i);
3440 NMLVCACHEHINT nmlv;
3442 ZeroMemory(&nmlv, sizeof(NMLVCACHEHINT));
3443 nmlv.iFrom = range.lower;
3444 nmlv.iTo = range.upper - 1;
3445 notify_hdr(infoPtr, LVN_ODCACHEHINT, &nmlv.hdr);
3448 /* iterate through the invalidated rows */
3449 while(iterator_prev(&i))
3451 item.iItem = i.nItem;
3452 item.iSubItem = 0;
3453 item.mask = LVIF_PARAM | LVIF_STATE;
3454 item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
3455 if (!LISTVIEW_GetItemW(infoPtr, &item)) continue;
3457 dis.CtlType = ODT_LISTVIEW;
3458 dis.CtlID = uID;
3459 dis.itemID = item.iItem;
3460 dis.itemAction = ODA_DRAWENTIRE;
3461 dis.itemState = 0;
3462 if (item.state & LVIS_SELECTED) dis.itemState |= ODS_SELECTED;
3463 if (infoPtr->bFocus && (item.state & LVIS_FOCUSED)) dis.itemState |= ODS_FOCUS;
3464 dis.hwndItem = infoPtr->hwndSelf;
3465 dis.hDC = hdc;
3466 if (!LISTVIEW_GetItemOrigin(infoPtr, dis.itemID, &Position)) continue;
3467 dis.rcItem.left = Position.x + Origin.x;
3468 dis.rcItem.right = dis.rcItem.left + infoPtr->nItemWidth;
3469 dis.rcItem.top = Position.y + Origin.y;
3470 dis.rcItem.bottom = dis.rcItem.top + infoPtr->nItemHeight;
3471 dis.itemData = item.lParam;
3473 TRACE("item=%s, rcItem=%s\n", debuglvitem_t(&item, TRUE), debugrect(&dis.rcItem));
3474 SendMessageW(hwndParent, WM_DRAWITEM, dis.CtlID, (LPARAM)&dis);
3476 iterator_destroy(&i);
3479 /***
3480 * DESCRIPTION:
3481 * Draws listview items when in report display mode.
3483 * PARAMETER(S):
3484 * [I] infoPtr : valid pointer to the listview structure
3485 * [I] hdc : device context handle
3486 * [I] cdmode : custom draw mode
3488 * RETURN:
3489 * None
3491 static void LISTVIEW_RefreshReport(LISTVIEW_INFO *infoPtr, HDC hdc, DWORD cdmode)
3493 INT rgntype, nFirstCol, nLastCol, nCol;
3494 RECT rcClip, rcItem;
3495 POINT Origin, Position;
3496 ITERATOR i;
3498 TRACE("()\n");
3500 /* figure out what to draw */
3501 rgntype = GetClipBox(hdc, &rcClip);
3502 if (rgntype == NULLREGION) return;
3504 /* Get scroll info once before loop */
3505 if (!LISTVIEW_GetOrigin(infoPtr, &Origin)) return;
3507 /* narrow down the columns we need to paint */
3508 for(nFirstCol = 0; nFirstCol < infoPtr->hdpaColumns->nItemCount; nFirstCol++)
3509 if (LISTVIEW_GetHeaderRect(infoPtr, nFirstCol, &rcItem) &&
3510 rcItem.right + Origin.x >= rcClip.left) break;
3511 for(nLastCol = infoPtr->hdpaColumns->nItemCount - 1; nLastCol >= 0; nLastCol--)
3512 if (LISTVIEW_GetHeaderRect(infoPtr, nLastCol, &rcItem) &&
3513 rcItem.left + Origin.x < rcClip.right) break;
3515 /* figure out what we need to draw */
3516 iterator_visibleitems(&i, infoPtr, hdc);
3518 /* a last few bits before we start drawing */
3519 TRACE("Colums=(%di - %d)\n", nFirstCol, nLastCol);
3521 /* iterate through the invalidated rows */
3522 while(iterator_prev(&i))
3524 /* iterate through the invalidated columns */
3525 for (nCol = nFirstCol; nCol <= nLastCol; nCol++)
3527 if (!LISTVIEW_GetItemOrigin(infoPtr, i.nItem, &Position)) continue;
3528 Position.x += Origin.x;
3529 Position.y += Origin.y;
3531 if (rgntype == COMPLEXREGION && LISTVIEW_GetHeaderRect(infoPtr, nCol, &rcItem))
3533 rcItem.top = 0;
3534 rcItem.bottom = infoPtr->nItemHeight;
3535 OffsetRect(&rcItem, Position.x, Position.y);
3536 if (!RectVisible(hdc, &rcItem)) continue;
3539 LISTVIEW_DrawItem(infoPtr, hdc, i.nItem, nCol, Position, cdmode);
3542 iterator_destroy(&i);
3545 /***
3546 * DESCRIPTION:
3547 * Draws listview items when in list display mode.
3549 * PARAMETER(S):
3550 * [I] infoPtr : valid pointer to the listview structure
3551 * [I] hdc : device context handle
3552 * [I] cdmode : custom draw mode
3554 * RETURN:
3555 * None
3557 static void LISTVIEW_RefreshList(LISTVIEW_INFO *infoPtr, HDC hdc, DWORD cdmode)
3559 POINT Origin, Position;
3560 ITERATOR i;
3562 /* Get scroll info once before loop */
3563 if (!LISTVIEW_GetOrigin(infoPtr, &Origin)) return;
3565 /* figure out what we need to draw */
3566 iterator_visibleitems(&i, infoPtr, hdc);
3568 while(iterator_prev(&i))
3570 if (!LISTVIEW_GetItemOrigin(infoPtr, i.nItem, &Position)) continue;
3571 Position.x += Origin.x;
3572 Position.y += Origin.y;
3574 LISTVIEW_DrawItem(infoPtr, hdc, i.nItem, 0, Position, cdmode);
3576 iterator_destroy(&i);
3580 /***
3581 * DESCRIPTION:
3582 * Draws listview items.
3584 * PARAMETER(S):
3585 * [I] infoPtr : valid pointer to the listview structure
3586 * [I] HDC : device context handle
3588 * RETURN:
3589 * NoneX
3591 static void LISTVIEW_Refresh(LISTVIEW_INFO *infoPtr, HDC hdc)
3593 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
3594 COLORREF oldTextColor, oldClrTextBk, oldClrText;
3595 NMLVCUSTOMDRAW nmlvcd;
3596 HFONT hOldFont;
3597 DWORD cdmode;
3598 INT oldBkMode;
3599 RECT rcClient;
3601 LISTVIEW_DUMP(infoPtr);
3603 infoPtr->bIsDrawing = TRUE;
3605 /* save dc values we're gonna trash while drawing */
3606 hOldFont = SelectObject(hdc, infoPtr->hFont);
3607 oldBkMode = GetBkMode(hdc);
3608 infoPtr->clrTextBkDefault = GetBkColor(hdc);
3609 oldTextColor = GetTextColor(hdc);
3611 oldClrTextBk = infoPtr->clrTextBk;
3612 oldClrText = infoPtr->clrText;
3614 GetClientRect(infoPtr->hwndSelf, &rcClient);
3615 customdraw_fill(&nmlvcd, infoPtr, hdc, &rcClient);
3616 cdmode = notify_customdraw(infoPtr, CDDS_PREPAINT, &nmlvcd);
3617 if (cdmode & CDRF_SKIPDEFAULT) goto enddraw;
3619 /* Use these colors to draw the items */
3620 infoPtr->clrTextBk = nmlvcd.clrTextBk;
3621 infoPtr->clrText = nmlvcd.clrText;
3623 /* nothing to draw */
3624 if(infoPtr->nItemCount == 0) goto enddraw;
3626 if ((infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && (uView == LVS_REPORT))
3627 LISTVIEW_RefreshOwnerDraw(infoPtr, hdc);
3628 else
3630 if (uView == LVS_REPORT)
3631 LISTVIEW_RefreshReport(infoPtr, hdc, cdmode);
3632 else /* LVS_LIST, LVS_ICON or LVS_SMALLICON */
3633 LISTVIEW_RefreshList(infoPtr, hdc, cdmode);
3635 /* if we have a focus rect, draw it */
3636 if (infoPtr->bFocus)
3637 DrawFocusRect(hdc, &infoPtr->rcFocus);
3640 enddraw:
3641 if (cdmode & CDRF_NOTIFYPOSTPAINT)
3642 notify_customdraw(infoPtr, CDDS_POSTPAINT, &nmlvcd);
3644 infoPtr->clrTextBk = oldClrTextBk;
3645 infoPtr->clrText = oldClrText;
3647 SelectObject(hdc, hOldFont);
3648 SetBkMode(hdc, oldBkMode);
3649 SetBkColor(hdc, infoPtr->clrTextBkDefault);
3650 SetTextColor(hdc, oldTextColor);
3651 infoPtr->bIsDrawing = FALSE;
3655 /***
3656 * DESCRIPTION:
3657 * Calculates the approximate width and height of a given number of items.
3659 * PARAMETER(S):
3660 * [I] infoPtr : valid pointer to the listview structure
3661 * [I] INT : number of items
3662 * [I] INT : width
3663 * [I] INT : height
3665 * RETURN:
3666 * Returns a DWORD. The width in the low word and the height in high word.
3668 static LRESULT LISTVIEW_ApproximateViewRect(LISTVIEW_INFO *infoPtr, INT nItemCount,
3669 WORD wWidth, WORD wHeight)
3671 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
3672 INT nItemCountPerColumn = 1;
3673 INT nColumnCount = 0;
3674 DWORD dwViewRect = 0;
3676 if (nItemCount == -1)
3677 nItemCount = infoPtr->nItemCount;
3679 if (uView == LVS_LIST)
3681 if (wHeight == 0xFFFF)
3683 /* use current height */
3684 wHeight = infoPtr->rcList.bottom - infoPtr->rcList.top;
3687 if (wHeight < infoPtr->nItemHeight)
3688 wHeight = infoPtr->nItemHeight;
3690 if (nItemCount > 0)
3692 if (infoPtr->nItemHeight > 0)
3694 nItemCountPerColumn = wHeight / infoPtr->nItemHeight;
3695 if (nItemCountPerColumn == 0)
3696 nItemCountPerColumn = 1;
3698 if (nItemCount % nItemCountPerColumn != 0)
3699 nColumnCount = nItemCount / nItemCountPerColumn;
3700 else
3701 nColumnCount = nItemCount / nItemCountPerColumn + 1;
3705 /* Microsoft padding magic */
3706 wHeight = nItemCountPerColumn * infoPtr->nItemHeight + 2;
3707 wWidth = nColumnCount * infoPtr->nItemWidth + 2;
3709 dwViewRect = MAKELONG(wWidth, wHeight);
3711 else if (uView == LVS_REPORT)
3712 FIXME("uView == LVS_REPORT: not implemented\n");
3713 else if (uView == LVS_SMALLICON)
3714 FIXME("uView == LVS_SMALLICON: not implemented\n");
3715 else if (uView == LVS_ICON)
3716 FIXME("uView == LVS_ICON: not implemented\n");
3718 return dwViewRect;
3721 /***
3722 * DESCRIPTION:
3723 * Arranges listview items in icon display mode.
3725 * PARAMETER(S):
3726 * [I] infoPtr : valid pointer to the listview structure
3727 * [I] INT : alignment code
3729 * RETURN:
3730 * SUCCESS : TRUE
3731 * FAILURE : FALSE
3733 static LRESULT LISTVIEW_Arrange(LISTVIEW_INFO *infoPtr, INT nAlignCode)
3735 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
3736 BOOL bResult = FALSE;
3738 if ((uView == LVS_ICON) || (uView == LVS_SMALLICON))
3740 switch (nAlignCode)
3742 case LVA_ALIGNLEFT:
3743 FIXME("nAlignCode=LVA_ALIGNLEFT: not implemented\n");
3744 break;
3745 case LVA_ALIGNTOP:
3746 FIXME("nAlignCode=LVA_ALIGNTOP: not implemented\n");
3747 break;
3748 case LVA_DEFAULT:
3749 FIXME("nAlignCode=LVA_DEFAULT: not implemented\n");
3750 break;
3751 case LVA_SNAPTOGRID:
3752 FIXME("nAlignCode=LVA_SNAPTOGRID: not implemented\n");
3753 break;
3757 return bResult;
3760 /* << LISTVIEW_CreateDragImage >> */
3763 /***
3764 * DESCRIPTION:
3765 * Removes all listview items and subitems.
3767 * PARAMETER(S):
3768 * [I] infoPtr : valid pointer to the listview structure
3770 * RETURN:
3771 * SUCCESS : TRUE
3772 * FAILURE : FALSE
3774 static LRESULT LISTVIEW_DeleteAllItems(LISTVIEW_INFO *infoPtr)
3776 LONG lStyle = infoPtr->dwStyle;
3777 UINT uView = lStyle & LVS_TYPEMASK;
3778 LISTVIEW_ITEM *lpItem;
3779 LISTVIEW_SUBITEM *lpSubItem;
3780 NMLISTVIEW nmlv;
3781 BOOL bSuppress;
3782 BOOL bResult = FALSE;
3783 HDPA hdpaSubItems;
3785 TRACE("()\n");
3787 LISTVIEW_DeselectAll(infoPtr);
3788 infoPtr->nSelectionMark=-1;
3789 infoPtr->nFocusedItem=-1;
3790 SetRectEmpty(&infoPtr->rcFocus);
3791 /* But we are supposed to leave nHotItem as is! */
3793 if (lStyle & LVS_OWNERDATA)
3795 infoPtr->nItemCount = 0;
3796 LISTVIEW_InvalidateList(infoPtr);
3797 return TRUE;
3800 if (infoPtr->nItemCount > 0)
3802 INT i, j;
3804 /* send LVN_DELETEALLITEMS notification */
3805 /* verify if subsequent LVN_DELETEITEM notifications should be
3806 suppressed */
3807 ZeroMemory(&nmlv, sizeof(NMLISTVIEW));
3808 nmlv.iItem = -1;
3809 bSuppress = notify_listview(infoPtr, LVN_DELETEALLITEMS, &nmlv);
3811 for (i = 0; i < infoPtr->nItemCount; i++)
3813 hdpaSubItems = (HDPA)DPA_GetPtr(infoPtr->hdpaItems, i);
3814 if (hdpaSubItems != NULL)
3816 for (j = 1; j < hdpaSubItems->nItemCount; j++)
3818 lpSubItem = (LISTVIEW_SUBITEM *)DPA_GetPtr(hdpaSubItems, j);
3819 if (lpSubItem != NULL)
3821 /* free subitem string */
3822 if (is_textW(lpSubItem->hdr.pszText))
3823 COMCTL32_Free(lpSubItem->hdr.pszText);
3825 /* free subitem */
3826 COMCTL32_Free(lpSubItem);
3830 lpItem = (LISTVIEW_ITEM *)DPA_GetPtr(hdpaSubItems, 0);
3831 if (lpItem != NULL)
3833 if (!bSuppress)
3835 /* send LVN_DELETEITEM notification */
3836 nmlv.iItem = i;
3837 nmlv.lParam = lpItem->lParam;
3838 notify_listview(infoPtr, LVN_DELETEITEM, &nmlv);
3841 /* free item string */
3842 if (is_textW(lpItem->hdr.pszText))
3843 COMCTL32_Free(lpItem->hdr.pszText);
3845 /* free item */
3846 COMCTL32_Free(lpItem);
3849 DPA_Destroy(hdpaSubItems);
3853 /* reinitialize listview memory */
3854 bResult = DPA_DeleteAllPtrs(infoPtr->hdpaItems);
3855 infoPtr->nItemCount = 0;
3856 DPA_DeleteAllPtrs(infoPtr->hdpaPosX);
3857 DPA_DeleteAllPtrs(infoPtr->hdpaPosY);
3859 /* align items (set position of each item) */
3860 if ((uView == LVS_ICON) || (uView == LVS_SMALLICON))
3862 if (lStyle & LVS_ALIGNLEFT)
3864 LISTVIEW_AlignLeft(infoPtr);
3866 else
3868 LISTVIEW_AlignTop(infoPtr);
3872 LISTVIEW_UpdateScroll(infoPtr);
3874 LISTVIEW_InvalidateList(infoPtr);
3877 return bResult;
3880 /***
3881 * DESCRIPTION:
3882 * Scrolls, and updates the columns, when a column is changing width.
3884 * PARAMETER(S):
3885 * [I] infoPtr : valid pointer to the listview structure
3886 * [I] nColumn : column to scroll
3887 * [I] dx : amount of scroll, in pixels
3889 * RETURN:
3890 * SUCCESS : TRUE
3891 * FAILURE : FALSE
3893 static BOOL LISTVIEW_ScrollColumns(LISTVIEW_INFO *infoPtr, INT nColumn, INT dx)
3895 COLUMN_INFO *lpColumnInfo;
3896 RECT rcOld, rcCol;
3897 INT nCol;
3899 if ((lpColumnInfo = DPA_GetPtr(infoPtr->hdpaColumns, nColumn)))
3900 rcCol = lpColumnInfo->rcHeader;
3902 /* ajust the other columns */
3903 for (nCol = nColumn; (lpColumnInfo = DPA_GetPtr(infoPtr->hdpaColumns, nCol)); nCol++)
3905 lpColumnInfo->rcHeader.left += dx;
3906 lpColumnInfo->rcHeader.right += dx;
3909 /* do not update screen if not in report mode */
3910 if ((infoPtr->dwStyle & LVS_TYPEMASK) != LVS_REPORT) return TRUE;
3912 /* if we have a focus, must first erase the focus rect */
3913 if (infoPtr->bFocus) LISTVIEW_ShowFocusRect(infoPtr, FALSE);
3915 /* Need to reset the item width when inserting a new column */
3916 infoPtr->nItemWidth += dx;
3918 LISTVIEW_UpdateScroll(infoPtr);
3920 /* scroll to cover the deleted column, and invalidate for redraw */
3921 rcOld = infoPtr->rcList;
3922 rcOld.left = rcCol.left;
3923 ScrollWindowEx(infoPtr->hwndSelf, dx, 0, &rcOld, &rcOld, 0, 0, SW_ERASE | SW_INVALIDATE);
3925 /* we can restore focus now */
3926 if (infoPtr->bFocus) LISTVIEW_ShowFocusRect(infoPtr, TRUE);
3928 return TRUE;
3931 /***
3932 * DESCRIPTION:
3933 * Removes a column from the listview control.
3935 * PARAMETER(S):
3936 * [I] infoPtr : valid pointer to the listview structure
3937 * [I] INT : column index
3939 * RETURN:
3940 * SUCCESS : TRUE
3941 * FAILURE : FALSE
3943 static BOOL LISTVIEW_DeleteColumn(LISTVIEW_INFO *infoPtr, INT nColumn)
3945 RECT rcCol;
3947 TRACE("nColumn=%d\n", nColumn);
3949 if (nColumn <= 0) return FALSE;
3951 if (!LISTVIEW_GetHeaderRect(infoPtr, nColumn, &rcCol))
3952 return FALSE;
3954 if (!Header_DeleteItem(infoPtr->hwndHeader, nColumn))
3955 return FALSE;
3957 COMCTL32_Free(DPA_GetPtr(infoPtr->hdpaColumns, nColumn));
3958 DPA_DeletePtr(infoPtr->hdpaColumns, nColumn);
3960 if (!(infoPtr->dwStyle & LVS_OWNERDATA))
3962 LISTVIEW_SUBITEM *lpSubItem, *lpDelItem;
3963 HDPA hdpaSubItems;
3964 INT nItem, nSubItem, i;
3966 for (nItem = 0; nItem < infoPtr->nItemCount; nItem++)
3968 hdpaSubItems = (HDPA)DPA_GetPtr(infoPtr->hdpaItems, nItem);
3969 if (!hdpaSubItems) continue;
3970 nSubItem = 0;
3971 lpDelItem = 0;
3972 for (i = 1; i < hdpaSubItems->nItemCount; i++)
3974 lpSubItem = (LISTVIEW_SUBITEM *)DPA_GetPtr(hdpaSubItems, i);
3975 if (!lpSubItem) break;
3976 if (lpSubItem->iSubItem == nColumn)
3978 nSubItem = i;
3979 lpDelItem = lpSubItem;
3981 else if (lpSubItem->iSubItem > nColumn)
3983 lpSubItem->iSubItem--;
3987 /* if we found our subitem, zapp it */
3988 if (nSubItem > 0)
3990 /* free string */
3991 if (is_textW(lpDelItem->hdr.pszText))
3992 COMCTL32_Free(lpDelItem->hdr.pszText);
3994 /* free item */
3995 COMCTL32_Free(lpDelItem);
3997 /* free dpa memory */
3998 DPA_DeletePtr(hdpaSubItems, nSubItem);
4003 /* update the other column info */
4004 LISTVIEW_ScrollColumns(infoPtr, nColumn, -(rcCol.right - rcCol.left));
4006 return TRUE;
4009 /***
4010 * DESCRIPTION:
4011 * Removes an item from the listview control.
4013 * PARAMETER(S):
4014 * [I] infoPtr : valid pointer to the listview structure
4015 * [I] INT : item index
4017 * RETURN:
4018 * SUCCESS : TRUE
4019 * FAILURE : FALSE
4021 static LRESULT LISTVIEW_DeleteItem(LISTVIEW_INFO *infoPtr, INT nItem)
4023 LONG lStyle = infoPtr->dwStyle;
4024 UINT uView = lStyle & LVS_TYPEMASK;
4025 NMLISTVIEW nmlv;
4026 BOOL bResult = FALSE;
4027 HDPA hdpaSubItems;
4028 LISTVIEW_ITEM *lpItem;
4029 LISTVIEW_SUBITEM *lpSubItem;
4030 LVITEMW item;
4031 INT i;
4033 TRACE("(nItem=%d)\n", nItem);
4036 /* First, send LVN_DELETEITEM notification. */
4037 ZeroMemory(&nmlv, sizeof (NMLISTVIEW));
4038 nmlv.iItem = nItem;
4039 notify_listview(infoPtr, LVN_DELETEITEM, &nmlv);
4041 if (nItem == infoPtr->nFocusedItem)
4043 infoPtr->nFocusedItem = -1;
4044 SetRectEmpty(&infoPtr->rcFocus);
4047 /* remove it from the selection range */
4048 item.state = LVIS_SELECTED;
4049 item.stateMask = LVIS_SELECTED;
4050 LISTVIEW_SetItemState(infoPtr,nItem,&item);
4052 if (lStyle & LVS_OWNERDATA)
4054 infoPtr->nItemCount--;
4055 LISTVIEW_InvalidateList(infoPtr); /*FIXME: optimize */
4056 return TRUE;
4059 if ((nItem >= 0) && (nItem < infoPtr->nItemCount))
4061 /* initialize memory */
4062 ZeroMemory(&nmlv, sizeof(NMLISTVIEW));
4064 hdpaSubItems = (HDPA)DPA_DeletePtr(infoPtr->hdpaItems, nItem);
4065 if (hdpaSubItems != NULL)
4067 infoPtr->nItemCount--;
4068 for (i = 1; i < hdpaSubItems->nItemCount; i++)
4070 lpSubItem = (LISTVIEW_SUBITEM *)DPA_GetPtr(hdpaSubItems, i);
4071 if (lpSubItem != NULL)
4073 /* free item string */
4074 if (is_textW(lpSubItem->hdr.pszText))
4075 COMCTL32_Free(lpSubItem->hdr.pszText);
4077 /* free item */
4078 COMCTL32_Free(lpSubItem);
4082 lpItem = (LISTVIEW_ITEM *)DPA_GetPtr(hdpaSubItems, 0);
4083 if (lpItem != NULL)
4085 /* free item string */
4086 if (is_textW(lpItem->hdr.pszText))
4087 COMCTL32_Free(lpItem->hdr.pszText);
4089 /* free item */
4090 COMCTL32_Free(lpItem);
4093 bResult = DPA_Destroy(hdpaSubItems);
4094 DPA_DeletePtr(infoPtr->hdpaPosX, nItem);
4095 DPA_DeletePtr(infoPtr->hdpaPosY, nItem);
4098 LISTVIEW_ShiftIndices(infoPtr, nItem, -1);
4100 /* align items (set position of each item) */
4101 if ((uView == LVS_SMALLICON) || (uView == LVS_ICON))
4103 if (lStyle & LVS_ALIGNLEFT)
4104 LISTVIEW_AlignLeft(infoPtr);
4105 else
4106 LISTVIEW_AlignTop(infoPtr);
4109 LISTVIEW_UpdateScroll(infoPtr);
4111 LISTVIEW_InvalidateList(infoPtr); /* FIXME: optimize */
4114 return bResult;
4118 /***
4119 * DESCRIPTION:
4120 * Callback implementation for editlabel control
4122 * PARAMETER(S):
4123 * [I] infoPtr : valid pointer to the listview structure
4124 * [I] pszText : modified text
4125 * [I] isW : TRUE if psxText is Unicode, FALSE if it's ANSI
4127 * RETURN:
4128 * SUCCESS : TRUE
4129 * FAILURE : FALSE
4131 static BOOL LISTVIEW_EndEditLabelT(LISTVIEW_INFO *infoPtr, LPWSTR pszText, BOOL isW)
4133 NMLVDISPINFOW dispInfo;
4135 TRACE("(pszText=%s, isW=%d)\n", debugtext_t(pszText, isW), isW);
4137 ZeroMemory(&dispInfo, sizeof(dispInfo));
4138 dispInfo.item.mask = LVIF_PARAM | LVIF_STATE;
4139 dispInfo.item.iItem = infoPtr->nEditLabelItem;
4140 dispInfo.item.iSubItem = 0;
4141 dispInfo.item.stateMask = ~0;
4142 if (!LISTVIEW_GetItemW(infoPtr, &dispInfo.item)) return FALSE;
4143 /* add the text from the edit in */
4144 dispInfo.item.mask |= LVIF_TEXT;
4145 dispInfo.item.pszText = pszText;
4146 dispInfo.item.cchTextMax = textlenT(pszText, isW);
4148 /* Do we need to update the Item Text */
4149 if (!notify_dispinfoT(infoPtr, LVN_ENDLABELEDITW, &dispInfo, isW)) return FALSE;
4150 if (!pszText) return TRUE;
4152 ZeroMemory(&dispInfo, sizeof(dispInfo));
4153 dispInfo.item.mask = LVIF_TEXT;
4154 dispInfo.item.iItem = infoPtr->nEditLabelItem;
4155 dispInfo.item.iSubItem = 0;
4156 dispInfo.item.pszText = pszText;
4157 dispInfo.item.cchTextMax = textlenT(pszText, isW);
4158 return LISTVIEW_SetItemT(infoPtr, &dispInfo.item, isW);
4161 /***
4162 * DESCRIPTION:
4163 * Begin in place editing of specified list view item
4165 * PARAMETER(S):
4166 * [I] infoPtr : valid pointer to the listview structure
4167 * [I] INT : item index
4168 * [I] isW : TRUE if it's a Unicode req, FALSE if ASCII
4170 * RETURN:
4171 * SUCCESS : TRUE
4172 * FAILURE : FALSE
4174 static HWND LISTVIEW_EditLabelT(LISTVIEW_INFO *infoPtr, INT nItem, BOOL isW)
4176 WCHAR szDispText[DISP_TEXT_SIZE] = { 0 };
4177 NMLVDISPINFOW dispInfo;
4178 RECT rect;
4180 TRACE("(nItem=%d, isW=%d)\n", nItem, isW);
4182 if (~infoPtr->dwStyle & LVS_EDITLABELS) return 0;
4183 if (nItem < 0 || nItem >= infoPtr->nItemCount) return 0;
4185 infoPtr->nEditLabelItem = nItem;
4187 /* Is the EditBox still there, if so remove it */
4188 if(infoPtr->hwndEdit != 0)
4190 SetFocus(infoPtr->hwndSelf);
4191 infoPtr->hwndEdit = 0;
4194 LISTVIEW_SetSelection(infoPtr, nItem);
4195 LISTVIEW_SetItemFocus(infoPtr, nItem);
4197 rect.left = LVIR_LABEL;
4198 if (!LISTVIEW_GetItemRect(infoPtr, nItem, &rect)) return 0;
4200 ZeroMemory(&dispInfo, sizeof(dispInfo));
4201 dispInfo.item.mask = LVIF_PARAM | LVIF_STATE | LVIF_TEXT;
4202 dispInfo.item.iItem = nItem;
4203 dispInfo.item.iSubItem = 0;
4204 dispInfo.item.stateMask = ~0;
4205 dispInfo.item.pszText = szDispText;
4206 dispInfo.item.cchTextMax = DISP_TEXT_SIZE;
4207 if (!LISTVIEW_GetItemT(infoPtr, &dispInfo.item, isW)) return 0;
4209 infoPtr->hwndEdit = CreateEditLabelT(infoPtr, dispInfo.item.pszText, WS_VISIBLE,
4210 rect.left-2, rect.top-1, 0, rect.bottom - rect.top+2, isW);
4211 if (!infoPtr->hwndEdit) return 0;
4213 if (notify_dispinfoT(infoPtr, LVN_BEGINLABELEDITW, &dispInfo, isW))
4215 SendMessageW(infoPtr->hwndEdit, WM_CLOSE, 0, 0);
4216 infoPtr->hwndEdit = 0;
4217 return 0;
4220 ShowWindow(infoPtr->hwndEdit, SW_NORMAL);
4221 SetFocus(infoPtr->hwndEdit);
4222 SendMessageW(infoPtr->hwndEdit, EM_SETSEL, 0, -1);
4223 return infoPtr->hwndEdit;
4227 /***
4228 * DESCRIPTION:
4229 * Ensures the specified item is visible, scrolling into view if necessary.
4231 * PARAMETER(S):
4232 * [I] infoPtr : valid pointer to the listview structure
4233 * [I] nItem : item index
4234 * [I] bPartial : partially or entirely visible
4236 * RETURN:
4237 * SUCCESS : TRUE
4238 * FAILURE : FALSE
4240 static BOOL LISTVIEW_EnsureVisible(LISTVIEW_INFO *infoPtr, INT nItem, BOOL bPartial)
4242 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
4243 INT nScrollPosHeight = 0;
4244 INT nScrollPosWidth = 0;
4245 INT nHorzAdjust = 0;
4246 INT nVertAdjust = 0;
4247 INT nHorzDiff = 0;
4248 INT nVertDiff = 0;
4249 RECT rcItem, rcTemp;
4251 rcItem.left = LVIR_BOUNDS;
4252 if (!LISTVIEW_GetItemRect(infoPtr, nItem, &rcItem)) return FALSE;
4254 if (bPartial && IntersectRect(&rcTemp, &infoPtr->rcList, &rcItem)) return TRUE;
4256 if (rcItem.left < infoPtr->rcList.left || rcItem.right > infoPtr->rcList.right)
4258 /* scroll left/right, but in LVS_REPORT mode */
4259 if (uView == LVS_LIST)
4260 nScrollPosWidth = infoPtr->nItemWidth;
4261 else if ((uView == LVS_SMALLICON) || (uView == LVS_ICON))
4262 nScrollPosWidth = 1;
4264 if (rcItem.left < infoPtr->rcList.left)
4266 nHorzAdjust = -1;
4267 if (uView != LVS_REPORT) nHorzDiff = rcItem.left - infoPtr->rcList.left;
4269 else
4271 nHorzAdjust = 1;
4272 if (uView != LVS_REPORT) nHorzDiff = rcItem.right - infoPtr->rcList.right;
4276 if (rcItem.top < infoPtr->rcList.top || rcItem.bottom > infoPtr->rcList.bottom)
4278 /* scroll up/down, but not in LVS_LIST mode */
4279 if (uView == LVS_REPORT)
4280 nScrollPosHeight = infoPtr->nItemHeight;
4281 else if ((uView == LVS_ICON) || (uView == LVS_SMALLICON))
4282 nScrollPosHeight = 1;
4284 if (rcItem.top < infoPtr->rcList.top)
4286 nVertAdjust = -1;
4287 if (uView != LVS_LIST) nVertDiff = rcItem.top - infoPtr->rcList.top;
4289 else
4291 nVertAdjust = 1;
4292 if (uView != LVS_LIST) nVertDiff = rcItem.bottom - infoPtr->rcList.bottom;
4296 if (!nScrollPosWidth && !nScrollPosHeight) return TRUE;
4298 if (nScrollPosWidth)
4300 INT diff = nHorzDiff / nScrollPosWidth;
4301 if (nHorzDiff % nScrollPosWidth) diff += nHorzAdjust;
4302 LISTVIEW_HScroll(infoPtr, SB_INTERNAL, diff, 0);
4305 if (nScrollPosHeight)
4307 INT diff = nVertDiff / nScrollPosHeight;
4308 if (nVertDiff % nScrollPosHeight) diff += nVertAdjust;
4309 LISTVIEW_VScroll(infoPtr, SB_INTERNAL, diff, 0);
4312 return TRUE;
4315 /***
4316 * DESCRIPTION:
4317 * Searches for an item with specific characteristics.
4319 * PARAMETER(S):
4320 * [I] hwnd : window handle
4321 * [I] nStart : base item index
4322 * [I] lpFindInfo : item information to look for
4324 * RETURN:
4325 * SUCCESS : index of item
4326 * FAILURE : -1
4328 static LRESULT LISTVIEW_FindItemW(LISTVIEW_INFO *infoPtr, INT nStart,
4329 LPLVFINDINFOW lpFindInfo)
4331 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
4332 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
4333 BOOL bWrap = FALSE, bNearest = FALSE;
4334 INT nItem = nStart + 1, nLast = infoPtr->nItemCount, nNearestItem = -1;
4335 ULONG xdist, ydist, dist, mindist = 0x7fffffff;
4336 POINT Position, Destination;
4337 LVITEMW lvItem;
4339 if (!lpFindInfo || nItem < 0) return -1;
4341 lvItem.mask = 0;
4342 if (lpFindInfo->flags & (LVFI_STRING | LVFI_PARTIAL))
4344 lvItem.mask |= LVIF_TEXT;
4345 lvItem.pszText = szDispText;
4346 lvItem.cchTextMax = DISP_TEXT_SIZE;
4349 if (lpFindInfo->flags & LVFI_WRAP)
4350 bWrap = TRUE;
4352 if ((lpFindInfo->flags & LVFI_NEARESTXY) &&
4353 (uView == LVS_ICON || uView ==LVS_SMALLICON))
4355 POINT Origin;
4357 FIXME("LVFI_NEARESTXY is slow.\n");
4358 if (!LISTVIEW_GetOrigin(infoPtr, &Origin)) return -1;
4359 Destination.x = lpFindInfo->pt.x - Origin.x;
4360 Destination.y = lpFindInfo->pt.y - Origin.y;
4361 switch(lpFindInfo->vkDirection)
4363 case VK_DOWN: Destination.y += infoPtr->nItemHeight; break;
4364 case VK_UP: Destination.y -= infoPtr->nItemHeight; break;
4365 case VK_RIGHT: Destination.x += infoPtr->nItemWidth; break;
4366 case VK_LEFT: Destination.x -= infoPtr->nItemWidth; break;
4367 case VK_HOME: Destination.x = Destination.y = 0; break;
4368 case VK_END: Destination.x = infoPtr->rcView.right; Destination.y = infoPtr->rcView.bottom; break;
4369 case VK_NEXT: Destination.y += infoPtr->rcList.bottom - infoPtr->rcList.top; break;
4370 case VK_PRIOR: Destination.y -= infoPtr->rcList.bottom - infoPtr->rcList.top; break;
4371 default: FIXME("Unknown vkDirection=%d\n", lpFindInfo->vkDirection);
4373 bNearest = TRUE;
4376 /* if LVFI_PARAM is specified, all other flags are ignored */
4377 if (lpFindInfo->flags & LVFI_PARAM)
4379 lvItem.mask |= LVIF_PARAM;
4380 bNearest = FALSE;
4381 lvItem.mask &= ~LVIF_TEXT;
4384 again:
4385 for (; nItem < nLast; nItem++)
4387 lvItem.iItem = nItem;
4388 lvItem.iSubItem = 0;
4389 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) continue;
4391 if (lvItem.mask & LVIF_PARAM && lpFindInfo->lParam == lvItem.lParam)
4392 return nItem;
4394 if (lvItem.mask & LVIF_TEXT)
4396 if (lpFindInfo->flags & LVFI_PARTIAL)
4398 if (strstrW(lvItem.pszText, lpFindInfo->psz) == NULL) continue;
4400 else
4402 if (lstrcmpW(lvItem.pszText, lpFindInfo->psz) != 0) continue;
4406 if (!bNearest) return nItem;
4408 /* This is very inefficient. To do a good job here,
4409 * we need a sorted array of (x,y) item positions */
4410 if (!LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position)) continue;
4412 /* compute the distance^2 to the destination */
4413 xdist = Destination.x - Position.x;
4414 ydist = Destination.y - Position.y;
4415 dist = xdist * xdist + ydist * ydist;
4417 /* remember the distance, and item if it's closer */
4418 if (dist < mindist)
4420 mindist = dist;
4421 nNearestItem = nItem;
4425 if (bWrap)
4427 nItem = 0;
4428 nLast = min(nStart + 1, infoPtr->nItemCount);
4429 bWrap = FALSE;
4430 goto again;
4433 return nNearestItem;
4436 /***
4437 * DESCRIPTION:
4438 * Searches for an item with specific characteristics.
4440 * PARAMETER(S):
4441 * [I] hwnd : window handle
4442 * [I] nStart : base item index
4443 * [I] lpFindInfo : item information to look for
4445 * RETURN:
4446 * SUCCESS : index of item
4447 * FAILURE : -1
4449 static LRESULT LISTVIEW_FindItemA(LISTVIEW_INFO *infoPtr, INT nStart,
4450 LPLVFINDINFOA lpFindInfo)
4452 BOOL hasText = lpFindInfo->flags & (LVFI_STRING | LVFI_PARTIAL);
4453 LVFINDINFOW fiw;
4454 LRESULT res;
4456 memcpy(&fiw, lpFindInfo, sizeof(fiw));
4457 if (hasText) fiw.psz = textdupTtoW((LPCWSTR)lpFindInfo->psz, FALSE);
4458 res = LISTVIEW_FindItemW(infoPtr, nStart, &fiw);
4459 if (hasText) textfreeT((LPWSTR)fiw.psz, FALSE);
4460 return res;
4463 /***
4464 * DESCRIPTION:
4465 * Retrieves the background image of the listview control.
4467 * PARAMETER(S):
4468 * [I] infoPtr : valid pointer to the listview structure
4469 * [O] LPLVMKBIMAGE : background image attributes
4471 * RETURN:
4472 * SUCCESS : TRUE
4473 * FAILURE : FALSE
4475 /* static LRESULT LISTVIEW_GetBkImage(LISTVIEW_INFO *infoPtr, LPLVBKIMAGE lpBkImage) */
4476 /* { */
4477 /* FIXME (listview, "empty stub!\n"); */
4478 /* return FALSE; */
4479 /* } */
4481 /***
4482 * DESCRIPTION:
4483 * Retrieves column attributes.
4485 * PARAMETER(S):
4486 * [I] infoPtr : valid pointer to the listview structure
4487 * [I] INT : column index
4488 * [IO] LPLVCOLUMNW : column information
4489 * [I] isW : if TRUE, then lpColumn is a LPLVCOLUMNW
4490 * otherwise it is in fact a LPLVCOLUMNA
4492 * RETURN:
4493 * SUCCESS : TRUE
4494 * FAILURE : FALSE
4496 static LRESULT LISTVIEW_GetColumnT(LISTVIEW_INFO *infoPtr, INT nItem, LPLVCOLUMNW lpColumn, BOOL isW)
4498 HDITEMW hdi;
4499 BOOL bResult = FALSE;
4501 if (lpColumn != NULL)
4504 /* initialize memory */
4505 ZeroMemory(&hdi, sizeof(hdi));
4507 if (lpColumn->mask & LVCF_FMT)
4508 hdi.mask |= HDI_FORMAT;
4510 if (lpColumn->mask & LVCF_WIDTH)
4511 hdi.mask |= HDI_WIDTH;
4513 if (lpColumn->mask & LVCF_TEXT)
4515 hdi.mask |= HDI_TEXT;
4516 hdi.cchTextMax = lpColumn->cchTextMax;
4517 hdi.pszText = lpColumn->pszText;
4520 if (lpColumn->mask & LVCF_IMAGE)
4521 hdi.mask |= HDI_IMAGE;
4523 if (lpColumn->mask & LVCF_ORDER)
4524 hdi.mask |= HDI_ORDER;
4526 if (isW)
4527 bResult = Header_GetItemW(infoPtr->hwndHeader, nItem, &hdi);
4528 else
4529 bResult = Header_GetItemA(infoPtr->hwndHeader, nItem, &hdi);
4531 if (bResult)
4533 if (lpColumn->mask & LVCF_FMT)
4535 lpColumn->fmt = 0;
4537 if (hdi.fmt & HDF_LEFT)
4538 lpColumn->fmt |= LVCFMT_LEFT;
4539 else if (hdi.fmt & HDF_RIGHT)
4540 lpColumn->fmt |= LVCFMT_RIGHT;
4541 else if (hdi.fmt & HDF_CENTER)
4542 lpColumn->fmt |= LVCFMT_CENTER;
4544 if (hdi.fmt & HDF_IMAGE)
4545 lpColumn->fmt |= LVCFMT_COL_HAS_IMAGES;
4547 if (hdi.fmt & HDF_BITMAP_ON_RIGHT)
4548 lpColumn->fmt |= LVCFMT_BITMAP_ON_RIGHT;
4551 if (lpColumn->mask & LVCF_WIDTH)
4552 lpColumn->cx = hdi.cxy;
4554 if (lpColumn->mask & LVCF_IMAGE)
4555 lpColumn->iImage = hdi.iImage;
4557 if (lpColumn->mask & LVCF_ORDER)
4558 lpColumn->iOrder = hdi.iOrder;
4560 TRACE("(col=%d, lpColumn=%s, isW=%d)\n",
4561 nItem, debuglvcolumn_t(lpColumn, isW), isW);
4566 return bResult;
4570 static LRESULT LISTVIEW_GetColumnOrderArray(LISTVIEW_INFO *infoPtr, INT iCount, LPINT lpiArray)
4572 INT i;
4574 if (!lpiArray)
4575 return FALSE;
4577 /* FIXME: little hack */
4578 for (i = 0; i < iCount; i++)
4579 lpiArray[i] = i;
4581 return TRUE;
4584 /***
4585 * DESCRIPTION:
4586 * Retrieves the column width.
4588 * PARAMETER(S):
4589 * [I] infoPtr : valid pointer to the listview structure
4590 * [I] int : column index
4592 * RETURN:
4593 * SUCCESS : column width
4594 * FAILURE : zero
4596 static INT LISTVIEW_GetColumnWidth(LISTVIEW_INFO *infoPtr, INT nColumn)
4598 INT nColumnWidth = 0;
4599 RECT rcHeader;
4601 TRACE("nColumn=%d\n", nColumn);
4603 /* we have a 'column' in LIST and REPORT mode only */
4604 switch(infoPtr->dwStyle & LVS_TYPEMASK)
4606 case LVS_LIST:
4607 nColumnWidth = infoPtr->nItemWidth;
4608 break;
4609 case LVS_REPORT:
4610 if (LISTVIEW_GetHeaderRect(infoPtr, nColumn, &rcHeader))
4611 nColumnWidth = rcHeader.right - rcHeader.left;
4612 break;
4615 TRACE("nColumnWidth=%d\n", nColumnWidth);
4616 return nColumnWidth;
4619 /***
4620 * DESCRIPTION:
4621 * In list or report display mode, retrieves the number of items that can fit
4622 * vertically in the visible area. In icon or small icon display mode,
4623 * retrieves the total number of visible items.
4625 * PARAMETER(S):
4626 * [I] infoPtr : valid pointer to the listview structure
4628 * RETURN:
4629 * Number of fully visible items.
4631 static LRESULT LISTVIEW_GetCountPerPage(LISTVIEW_INFO *infoPtr)
4633 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
4634 INT nItemCount = 0;
4636 if (uView == LVS_LIST)
4638 if (infoPtr->rcList.right > infoPtr->nItemWidth)
4640 nItemCount = LISTVIEW_GetCountPerRow(infoPtr) *
4641 LISTVIEW_GetCountPerColumn(infoPtr);
4644 else if (uView == LVS_REPORT)
4646 nItemCount = LISTVIEW_GetCountPerColumn(infoPtr);
4648 else
4650 nItemCount = infoPtr->nItemCount;
4653 return nItemCount;
4657 /***
4658 * DESCRIPTION:
4659 * Retrieves an image list handle.
4661 * PARAMETER(S):
4662 * [I] infoPtr : valid pointer to the listview structure
4663 * [I] nImageList : image list identifier
4665 * RETURN:
4666 * SUCCESS : image list handle
4667 * FAILURE : NULL
4669 static LRESULT LISTVIEW_GetImageList(LISTVIEW_INFO *infoPtr, INT nImageList)
4671 HIMAGELIST himl = NULL;
4673 switch (nImageList)
4675 case LVSIL_NORMAL:
4676 himl = infoPtr->himlNormal;
4677 break;
4678 case LVSIL_SMALL:
4679 himl = infoPtr->himlSmall;
4680 break;
4681 case LVSIL_STATE:
4682 himl = infoPtr->himlState;
4683 break;
4686 return (LRESULT)himl;
4689 /* LISTVIEW_GetISearchString */
4691 /***
4692 * DESCRIPTION:
4693 * Retrieves item attributes.
4695 * PARAMETER(S):
4696 * [I] hwnd : window handle
4697 * [IO] lpLVItem : item info
4698 * [I] isW : if TRUE, then lpLVItem is a LPLVITEMW,
4699 * if FALSE, the lpLVItem is a LPLVITEMA.
4701 * NOTE:
4702 * This is the internal 'GetItem' interface -- it tries to
4703 * be smart, and avoids text copies, if possible, by modifing
4704 * lpLVItem->pszText to point to the text string. Please note
4705 * that this is not always possible (e.g. OWNERDATA), so on
4706 * entry you *must* supply valid values for pszText, and cchTextMax.
4707 * The only difference to the documented interface is that upon
4708 * return, you should use *only* the lpLVItem->pszText, rather than
4709 * the buffer pointer you provided on input. Most code already does
4710 * that, so it's not a problem.
4711 * For the two cases when the text must be copied (that is,
4712 * for LVM_GETITEM, and LVMGETITEMTEXT), use LISTVIEW_GetItemExtT.
4714 * RETURN:
4715 * SUCCESS : TRUE
4716 * FAILURE : FALSE
4718 static BOOL LISTVIEW_GetItemT(LISTVIEW_INFO *infoPtr, LPLVITEMW lpLVItem, BOOL isW)
4720 NMLVDISPINFOW dispInfo;
4721 LISTVIEW_ITEM *lpItem;
4722 ITEMHDR* pItemHdr;
4723 HDPA hdpaSubItems;
4725 TRACE("(lpLVItem=%s, isW=%d)\n", debuglvitem_t(lpLVItem, isW), isW);
4727 if (!lpLVItem || lpLVItem->iItem < 0 || lpLVItem->iItem >= infoPtr->nItemCount)
4728 return FALSE;
4730 if (lpLVItem->mask == 0) return TRUE;
4732 /* a quick optimization if all we're asked is the focus state
4733 * these queries are worth optimising since they are common,
4734 * and can be answered in constant time, without the heavy accesses */
4735 if ( (lpLVItem->mask == LVIF_STATE) && (lpLVItem->stateMask == LVIS_FOCUSED) &&
4736 !(infoPtr->uCallbackMask & LVIS_FOCUSED) )
4738 lpLVItem->state = 0;
4739 if (infoPtr->nFocusedItem == lpLVItem->iItem)
4740 lpLVItem->state |= LVIS_FOCUSED;
4741 return TRUE;
4744 ZeroMemory(&dispInfo, sizeof(dispInfo));
4746 /* if the app stores all the data, handle it separately */
4747 if (infoPtr->dwStyle & LVS_OWNERDATA)
4749 dispInfo.item.state = 0;
4751 /* if we need to callback, do it now */
4752 if ((lpLVItem->mask & ~LVIF_STATE) || infoPtr->uCallbackMask)
4754 /* NOTE: copy only fields which we _know_ are initialized, some apps
4755 * depend on the uninitialized fields being 0 */
4756 dispInfo.item.mask = lpLVItem->mask;
4757 dispInfo.item.iItem = lpLVItem->iItem;
4758 dispInfo.item.iSubItem = lpLVItem->iSubItem;
4759 if (lpLVItem->mask & LVIF_TEXT)
4761 dispInfo.item.pszText = lpLVItem->pszText;
4762 dispInfo.item.cchTextMax = lpLVItem->cchTextMax;
4764 if (lpLVItem->mask & LVIF_STATE)
4765 dispInfo.item.stateMask = lpLVItem->stateMask & infoPtr->uCallbackMask;
4766 notify_dispinfoT(infoPtr, LVN_GETDISPINFOW, &dispInfo, isW);
4767 dispInfo.item.stateMask = lpLVItem->stateMask;
4768 *lpLVItem = dispInfo.item;
4769 TRACE(" getdispinfo(1):lpLVItem=%s\n", debuglvitem_t(lpLVItem, isW));
4772 /* we store only a little state, so if we're not asked, we're done */
4773 if (!(lpLVItem->mask & LVIF_STATE) || lpLVItem->iSubItem) return TRUE;
4775 /* if focus is handled by us, report it */
4776 if ( lpLVItem->stateMask & ~infoPtr->uCallbackMask & LVIS_FOCUSED )
4778 lpLVItem->state &= ~LVIS_FOCUSED;
4779 if (infoPtr->nFocusedItem == lpLVItem->iItem)
4780 lpLVItem->state |= LVIS_FOCUSED;
4783 /* and do the same for selection, if we handle it */
4784 if ( lpLVItem->stateMask & ~infoPtr->uCallbackMask & LVIS_SELECTED )
4786 lpLVItem->state &= ~LVIS_SELECTED;
4787 if (ranges_contain(infoPtr->selectionRanges, lpLVItem->iItem))
4788 lpLVItem->state |= LVIS_SELECTED;
4791 return TRUE;
4794 /* find the item and subitem structures before we proceed */
4795 hdpaSubItems = (HDPA)DPA_GetPtr(infoPtr->hdpaItems, lpLVItem->iItem);
4796 if (hdpaSubItems == NULL) return FALSE;
4798 if ( !(lpItem = (LISTVIEW_ITEM *)DPA_GetPtr(hdpaSubItems, 0)) )
4799 return FALSE;
4801 if (lpLVItem->iSubItem)
4803 LISTVIEW_SUBITEM *lpSubItem = LISTVIEW_GetSubItemPtr(hdpaSubItems, lpLVItem->iSubItem);
4804 if(!lpSubItem) return FALSE;
4805 pItemHdr = &lpSubItem->hdr;
4807 else
4808 pItemHdr = &lpItem->hdr;
4810 /* Do we need to query the state from the app? */
4811 if ((lpLVItem->mask & LVIF_STATE) && infoPtr->uCallbackMask && lpLVItem->iSubItem == 0)
4813 dispInfo.item.mask |= LVIF_STATE;
4814 dispInfo.item.stateMask = infoPtr->uCallbackMask;
4817 /* Do we need to enquire about the image? */
4818 if ((lpLVItem->mask & LVIF_IMAGE) && pItemHdr->iImage == I_IMAGECALLBACK)
4819 dispInfo.item.mask |= LVIF_IMAGE;
4821 /* Apps depend on calling back for text if it is NULL or LPSTR_TEXTCALLBACKW */
4822 if ((lpLVItem->mask & LVIF_TEXT) && !is_textW(pItemHdr->pszText))
4824 dispInfo.item.mask |= LVIF_TEXT;
4825 dispInfo.item.pszText = lpLVItem->pszText;
4826 dispInfo.item.cchTextMax = lpLVItem->cchTextMax;
4827 if (dispInfo.item.pszText && dispInfo.item.cchTextMax > 0)
4828 *dispInfo.item.pszText = '\0';
4831 /* If we don't have all the requested info, query the application */
4832 if (dispInfo.item.mask != 0)
4834 dispInfo.item.iItem = lpLVItem->iItem;
4835 dispInfo.item.iSubItem = lpLVItem->iSubItem;
4836 dispInfo.item.lParam = lpItem->lParam;
4837 notify_dispinfoT(infoPtr, LVN_GETDISPINFOW, &dispInfo, isW);
4838 TRACE(" getdispinfo(2):item=%s\n", debuglvitem_t(&dispInfo.item, isW));
4841 /* Now, handle the iImage field */
4842 if (dispInfo.item.mask & LVIF_IMAGE)
4844 lpLVItem->iImage = dispInfo.item.iImage;
4845 if ((dispInfo.item.mask & LVIF_DI_SETITEM) && pItemHdr->iImage == I_IMAGECALLBACK)
4846 pItemHdr->iImage = dispInfo.item.iImage;
4848 else if (lpLVItem->mask & LVIF_IMAGE)
4849 lpLVItem->iImage = pItemHdr->iImage;
4851 /* The pszText field */
4852 if (dispInfo.item.mask & LVIF_TEXT)
4854 if ((dispInfo.item.mask & LVIF_DI_SETITEM) && pItemHdr->pszText)
4855 textsetptrT(&pItemHdr->pszText, dispInfo.item.pszText, isW);
4857 lpLVItem->pszText = dispInfo.item.pszText;
4859 else if (lpLVItem->mask & LVIF_TEXT)
4861 if (isW) lpLVItem->pszText = pItemHdr->pszText;
4862 else textcpynT(lpLVItem->pszText, isW, pItemHdr->pszText, TRUE, lpLVItem->cchTextMax);
4865 /* if this is a subitem, we're done */
4866 if (lpLVItem->iSubItem) return TRUE;
4868 /* Next is the lParam field */
4869 if (dispInfo.item.mask & LVIF_PARAM)
4871 lpLVItem->lParam = dispInfo.item.lParam;
4872 if ((dispInfo.item.mask & LVIF_DI_SETITEM))
4873 lpItem->lParam = dispInfo.item.lParam;
4875 else if (lpLVItem->mask & LVIF_PARAM)
4876 lpLVItem->lParam = lpItem->lParam;
4878 /* ... the state field (this one is different due to uCallbackmask) */
4879 if (lpLVItem->mask & LVIF_STATE)
4881 lpLVItem->state = lpItem->state;
4882 if (dispInfo.item.mask & LVIF_STATE)
4884 lpLVItem->state &= ~dispInfo.item.stateMask;
4885 lpLVItem->state |= (dispInfo.item.state & dispInfo.item.stateMask);
4887 if ( lpLVItem->stateMask & ~infoPtr->uCallbackMask & LVIS_FOCUSED )
4889 lpLVItem->state &= ~LVIS_FOCUSED;
4890 if (infoPtr->nFocusedItem == lpLVItem->iItem)
4891 lpLVItem->state |= LVIS_FOCUSED;
4893 if ( lpLVItem->stateMask & ~infoPtr->uCallbackMask & LVIS_SELECTED )
4895 lpLVItem->state &= ~LVIS_SELECTED;
4896 if (ranges_contain(infoPtr->selectionRanges, lpLVItem->iItem))
4897 lpLVItem->state |= LVIS_SELECTED;
4901 /* and last, but not least, the indent field */
4902 if (lpLVItem->mask & LVIF_INDENT)
4903 lpLVItem->iIndent = lpItem->iIndent;
4905 return TRUE;
4908 /***
4909 * DESCRIPTION:
4910 * Retrieves item attributes.
4912 * PARAMETER(S):
4913 * [I] hwnd : window handle
4914 * [IO] lpLVItem : item info
4915 * [I] isW : if TRUE, then lpLVItem is a LPLVITEMW,
4916 * if FALSE, the lpLVItem is a LPLVITEMA.
4918 * NOTE:
4919 * This is the external 'GetItem' interface -- it properly copies
4920 * the text in the provided buffer.
4922 * RETURN:
4923 * SUCCESS : TRUE
4924 * FAILURE : FALSE
4926 static BOOL LISTVIEW_GetItemExtT(LISTVIEW_INFO *infoPtr, LPLVITEMW lpLVItem, BOOL isW)
4928 LPWSTR pszText;
4929 BOOL bResult;
4931 if (!lpLVItem || lpLVItem->iItem < 0 || lpLVItem->iItem >= infoPtr->nItemCount)
4932 return FALSE;
4934 pszText = lpLVItem->pszText;
4935 bResult = LISTVIEW_GetItemT(infoPtr, lpLVItem, isW);
4936 if (bResult && lpLVItem->pszText != pszText)
4937 textcpynT(pszText, isW, lpLVItem->pszText, isW, lpLVItem->cchTextMax);
4938 lpLVItem->pszText = pszText;
4940 return bResult;
4944 /***
4945 * DESCRIPTION:
4946 * Retrieves the position (upper-left) of the listview control item.
4947 * Note that for LVS_ICON style, the upper-left is that of the icon
4948 * and not the bounding box.
4950 * PARAMETER(S):
4951 * [I] infoPtr : valid pointer to the listview structure
4952 * [I] nItem : item index
4953 * [O] lpptPosition : coordinate information
4955 * RETURN:
4956 * SUCCESS : TRUE
4957 * FAILURE : FALSE
4959 static BOOL LISTVIEW_GetItemPosition(LISTVIEW_INFO *infoPtr, INT nItem, LPPOINT lpptPosition)
4961 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
4962 POINT Origin;
4964 TRACE("(nItem=%d, lpptPosition=%p)\n", nItem, lpptPosition);
4966 if (!lpptPosition || nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
4967 if (!LISTVIEW_GetItemOrigin(infoPtr, nItem, lpptPosition)) return FALSE;
4968 if (!LISTVIEW_GetOrigin(infoPtr, &Origin)) return FALSE;
4970 if (uView == LVS_ICON)
4972 lpptPosition->x += (infoPtr->nItemWidth - infoPtr->iconSize.cx) / 2;
4973 lpptPosition->y += ICON_TOP_PADDING;
4975 lpptPosition->x += Origin.x;
4976 lpptPosition->y += Origin.y;
4978 TRACE (" lpptPosition=%s\n", debugpoint(lpptPosition));
4979 return TRUE;
4983 /***
4984 * DESCRIPTION:
4985 * Retrieves the bounding rectangle for a listview control item.
4987 * PARAMETER(S):
4988 * [I] infoPtr : valid pointer to the listview structure
4989 * [I] nItem : item index
4990 * [IO] lprc : bounding rectangle coordinates
4991 * lprc->left specifies the portion of the item for which the bounding
4992 * rectangle will be retrieved.
4994 * LVIR_BOUNDS Returns the bounding rectangle of the entire item,
4995 * including the icon and label.
4997 * * For LVS_ICON
4998 * * Experiment shows that native control returns:
4999 * * width = min (48, length of text line)
5000 * * .left = position.x - (width - iconsize.cx)/2
5001 * * .right = .left + width
5002 * * height = #lines of text * ntmHeight + icon height + 8
5003 * * .top = position.y - 2
5004 * * .bottom = .top + height
5005 * * separation between items .y = itemSpacing.cy - height
5006 * * .x = itemSpacing.cx - width
5007 * LVIR_ICON Returns the bounding rectangle of the icon or small icon.
5009 * * For LVS_ICON
5010 * * Experiment shows that native control returns:
5011 * * width = iconSize.cx + 16
5012 * * .left = position.x - (width - iconsize.cx)/2
5013 * * .right = .left + width
5014 * * height = iconSize.cy + 4
5015 * * .top = position.y - 2
5016 * * .bottom = .top + height
5017 * * separation between items .y = itemSpacing.cy - height
5018 * * .x = itemSpacing.cx - width
5019 * LVIR_LABEL Returns the bounding rectangle of the item text.
5021 * * For LVS_ICON
5022 * * Experiment shows that native control returns:
5023 * * width = text length
5024 * * .left = position.x - width/2
5025 * * .right = .left + width
5026 * * height = ntmH * linecount + 2
5027 * * .top = position.y + iconSize.cy + 6
5028 * * .bottom = .top + height
5029 * * separation between items .y = itemSpacing.cy - height
5030 * * .x = itemSpacing.cx - width
5031 * LVIR_SELECTBOUNDS Returns the union of the LVIR_ICON and LVIR_LABEL
5032 * rectangles, but excludes columns in report view.
5034 * RETURN:
5035 * SUCCESS : TRUE
5036 * FAILURE : FALSE
5038 * NOTES
5039 * Note that the bounding rectangle of the label in the LVS_ICON view depends
5040 * upon whether the window has the focus currently and on whether the item
5041 * is the one with the focus. Ensure that the control's record of which
5042 * item has the focus agrees with the items' records.
5044 static BOOL LISTVIEW_GetItemRect(LISTVIEW_INFO *infoPtr, INT nItem, LPRECT lprc)
5046 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
5047 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
5048 BOOL doLabel = TRUE, oversizedBox = FALSE;
5049 POINT Position, Origin;
5050 LVITEMW lvItem;
5051 RECT label_rect;
5053 TRACE("(hwnd=%x, nItem=%d, lprc=%p)\n", infoPtr->hwndSelf, nItem, lprc);
5055 if (!lprc || nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
5056 if (!LISTVIEW_GetOrigin(infoPtr, &Origin)) return FALSE;
5057 if (!LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position)) return FALSE;
5059 /* Be smart and try to figure out the minimum we have to do */
5060 if (lprc->left == LVIR_ICON) doLabel = FALSE;
5061 if (uView == LVS_REPORT && lprc->left == LVIR_BOUNDS) doLabel = FALSE;
5062 if (uView == LVS_ICON && lprc->left != LVIR_ICON &&
5063 infoPtr->bFocus && LISTVIEW_GetItemState(infoPtr, nItem, LVIS_FOCUSED))
5064 oversizedBox = TRUE;
5066 /* get what we need from the item before hand, so we make
5067 * only one request. This can speed up things, if data
5068 * is stored on the app side */
5069 lvItem.mask = 0;
5070 if (uView == LVS_REPORT) lvItem.mask |= LVIF_INDENT;
5071 if (doLabel) lvItem.mask |= LVIF_TEXT;
5072 lvItem.iItem = nItem;
5073 lvItem.iSubItem = 0;
5074 lvItem.pszText = szDispText;
5075 lvItem.cchTextMax = DISP_TEXT_SIZE;
5076 if (lvItem.mask && !LISTVIEW_GetItemW(infoPtr, &lvItem)) return FALSE;
5077 /* we got the state already up, simulate it here, to avoid a reget */
5078 if (uView == LVS_ICON && (lprc->left != LVIR_ICON))
5080 lvItem.mask |= LVIF_STATE;
5081 lvItem.stateMask = LVIS_FOCUSED;
5082 lvItem.state = (oversizedBox ? LVIS_FOCUSED : 0);
5085 if (uView == LVS_REPORT && (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT) && lprc->left == LVIR_SELECTBOUNDS)
5086 lprc->left = LVIR_BOUNDS;
5087 switch(lprc->left)
5089 case LVIR_ICON:
5090 if (!LISTVIEW_GetItemMetrics(infoPtr, &lvItem, NULL, NULL, lprc, NULL)) return FALSE;
5091 break;
5093 case LVIR_LABEL:
5094 if (!LISTVIEW_GetItemMetrics(infoPtr, &lvItem, NULL, NULL, NULL, lprc)) return FALSE;
5095 break;
5097 case LVIR_BOUNDS:
5098 if (!LISTVIEW_GetItemMetrics(infoPtr, &lvItem, lprc, NULL, NULL, NULL)) return FALSE;
5099 break;
5101 case LVIR_SELECTBOUNDS:
5102 if (!LISTVIEW_GetItemMetrics(infoPtr, &lvItem, NULL, NULL, lprc, &label_rect)) return FALSE;
5103 UnionRect(lprc, lprc, &label_rect);
5104 break;
5106 default:
5107 WARN("Unknown value: %d\n", lprc->left);
5108 return FALSE;
5111 OffsetRect(lprc, Position.x + Origin.x, Position.y + Origin.y);
5113 TRACE(" rect=%s\n", debugrect(lprc));
5115 return TRUE;
5118 /***
5119 * DESCRIPTION:
5120 * Retrieves the spacing between listview control items.
5122 * PARAMETER(S):
5123 * [I] infoPtr : valid pointer to the listview structure
5124 * [IO] lprc : rectangle to receive the output
5125 * on input, lprc->top = nSubItem
5126 * lprc->left = LVIR_ICON | LVIR_BOUNDS | LVIR_LABEL
5128 * NOTE: for subItem = 0, we should return the bounds of the _entire_ item,
5129 * not only those of the first column.
5130 * Fortunately, LISTVIEW_GetItemMetrics does the right thing.
5132 * RETURN:
5133 * TRUE: success
5134 * FALSE: failure
5136 static BOOL LISTVIEW_GetSubItemRect(LISTVIEW_INFO *infoPtr, INT nItem, LPRECT lprc)
5138 POINT Position, Origin;
5139 LVITEMW lvItem;
5141 if (!lprc || (infoPtr->dwStyle & LVS_TYPEMASK) != LVS_REPORT) return FALSE;
5143 TRACE("(nItem=%d, nSubItem=%d)\n", nItem, lprc->top);
5145 if (!LISTVIEW_GetOrigin(infoPtr, &Origin)) return FALSE;
5146 if (!LISTVIEW_GetItemPosition(infoPtr, nItem, &Position)) return FALSE;
5148 lvItem.mask = lprc->top == 0 ? LVIF_INDENT : 0;
5149 lvItem.iItem = nItem;
5150 lvItem.iSubItem = lprc->top;
5152 if (lvItem.mask && !LISTVIEW_GetItemW(infoPtr, &lvItem)) return FALSE;
5153 switch(lprc->left)
5155 case LVIR_ICON:
5156 if (!LISTVIEW_GetItemMetrics(infoPtr, &lvItem, NULL, NULL, lprc, NULL)) return FALSE;
5157 break;
5159 case LVIR_LABEL:
5160 case LVIR_BOUNDS:
5161 if (!LISTVIEW_GetItemMetrics(infoPtr, &lvItem, lprc, NULL, NULL, NULL)) return FALSE;
5162 break;
5164 default:
5165 ERR("Unknown bounds=%d\n", lprc->left);
5166 return FALSE;
5169 OffsetRect(lprc, Position.x + Origin.x, Position.y + Origin.y);
5170 return TRUE;
5174 /***
5175 * DESCRIPTION:
5176 * Retrieves the width of a label.
5178 * PARAMETER(S):
5179 * [I] infoPtr : valid pointer to the listview structure
5181 * RETURN:
5182 * SUCCESS : string width (in pixels)
5183 * FAILURE : zero
5185 static INT LISTVIEW_GetLabelWidth(LISTVIEW_INFO *infoPtr, INT nItem)
5187 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
5188 LVITEMW lvItem;
5190 TRACE("(nItem=%d)\n", nItem);
5192 lvItem.mask = LVIF_TEXT;
5193 lvItem.iItem = nItem;
5194 lvItem.iSubItem = 0;
5195 lvItem.pszText = szDispText;
5196 lvItem.cchTextMax = DISP_TEXT_SIZE;
5197 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) return 0;
5199 return LISTVIEW_GetStringWidthT(infoPtr, lvItem.pszText, TRUE);
5202 /***
5203 * DESCRIPTION:
5204 * Retrieves the spacing between listview control items.
5206 * PARAMETER(S):
5207 * [I] infoPtr : valid pointer to the listview structure
5208 * [I] BOOL : flag for small or large icon
5210 * RETURN:
5211 * Horizontal + vertical spacing
5213 static LRESULT LISTVIEW_GetItemSpacing(LISTVIEW_INFO *infoPtr, BOOL bSmall)
5215 LONG lResult;
5217 if (!bSmall)
5219 lResult = MAKELONG(infoPtr->iconSpacing.cx, infoPtr->iconSpacing.cy);
5221 else
5223 if ((infoPtr->dwStyle & LVS_TYPEMASK) == LVS_ICON)
5224 lResult = MAKELONG(DEFAULT_COLUMN_WIDTH, GetSystemMetrics(SM_CXSMICON)+HEIGHT_PADDING);
5225 else
5226 lResult = MAKELONG(infoPtr->nItemWidth, infoPtr->nItemHeight);
5228 return lResult;
5231 /***
5232 * DESCRIPTION:
5233 * Retrieves the state of a listview control item.
5235 * PARAMETER(S):
5236 * [I] infoPtr : valid pointer to the listview structure
5237 * [I] nItem : item index
5238 * [I] uMask : state mask
5240 * RETURN:
5241 * State specified by the mask.
5243 static LRESULT LISTVIEW_GetItemState(LISTVIEW_INFO *infoPtr, INT nItem, UINT uMask)
5245 LVITEMW lvItem;
5247 if (nItem < 0 || nItem >= infoPtr->nItemCount) return 0;
5249 lvItem.iItem = nItem;
5250 lvItem.iSubItem = 0;
5251 lvItem.mask = LVIF_STATE;
5252 lvItem.stateMask = uMask;
5253 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) return 0;
5255 return lvItem.state & uMask;
5258 /***
5259 * DESCRIPTION:
5260 * Retrieves the text of a listview control item or subitem.
5262 * PARAMETER(S):
5263 * [I] hwnd : window handle
5264 * [I] nItem : item index
5265 * [IO] lpLVItem : item information
5266 * [I] isW : TRUE if lpLVItem is Unicode
5268 * RETURN:
5269 * SUCCESS : string length
5270 * FAILURE : 0
5272 static LRESULT LISTVIEW_GetItemTextT(LISTVIEW_INFO *infoPtr, INT nItem, LPLVITEMW lpLVItem, BOOL isW)
5274 if (!lpLVItem || nItem < 0 || nItem >= infoPtr->nItemCount) return 0;
5276 lpLVItem->mask = LVIF_TEXT;
5277 lpLVItem->iItem = nItem;
5278 if (!LISTVIEW_GetItemExtT(infoPtr, lpLVItem, isW)) return 0;
5280 return textlenT(lpLVItem->pszText, isW);
5283 /***
5284 * DESCRIPTION:
5285 * Searches for an item based on properties + relationships.
5287 * PARAMETER(S):
5288 * [I] infoPtr : valid pointer to the listview structure
5289 * [I] nItem : item index
5290 * [I] uFlags : relationship flag
5292 * FIXME:
5293 * This function is very, very inefficient! Needs work.
5295 * RETURN:
5296 * SUCCESS : item index
5297 * FAILURE : -1
5299 static LRESULT LISTVIEW_GetNextItem(LISTVIEW_INFO *infoPtr, INT nItem, UINT uFlags)
5301 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
5302 UINT uMask = 0;
5303 LVFINDINFOW lvFindInfo;
5304 INT nCountPerColumn;
5305 INT i;
5307 TRACE("nItem=%d, uFlags=%x, nItemCount=%d\n", nItem, uFlags, infoPtr->nItemCount);
5308 if (nItem < -1 || nItem >= infoPtr->nItemCount) return -1;
5310 ZeroMemory(&lvFindInfo, sizeof(lvFindInfo));
5312 if (uFlags & LVNI_CUT)
5313 uMask |= LVIS_CUT;
5315 if (uFlags & LVNI_DROPHILITED)
5316 uMask |= LVIS_DROPHILITED;
5318 if (uFlags & LVNI_FOCUSED)
5319 uMask |= LVIS_FOCUSED;
5321 if (uFlags & LVNI_SELECTED)
5322 uMask |= LVIS_SELECTED;
5324 /* if we're asked for the focused item, that's only one,
5325 * so it's worth optimizing */
5326 if (uFlags & LVNI_FOCUSED)
5328 if (!(LISTVIEW_GetItemState(infoPtr, infoPtr->nFocusedItem, uMask) & uMask) == uMask) return -1;
5329 return (infoPtr->nFocusedItem == nItem) ? -1 : infoPtr->nFocusedItem;
5332 if (uFlags & LVNI_ABOVE)
5334 if ((uView == LVS_LIST) || (uView == LVS_REPORT))
5336 while (nItem >= 0)
5338 nItem--;
5339 if ((ListView_GetItemState(infoPtr->hwndSelf, nItem, uMask) & uMask) == uMask)
5340 return nItem;
5343 else
5345 lvFindInfo.flags = LVFI_NEARESTXY;
5346 lvFindInfo.vkDirection = VK_UP;
5347 ListView_GetItemPosition(infoPtr->hwndSelf, nItem, &lvFindInfo.pt);
5348 while ((nItem = ListView_FindItemW(infoPtr->hwndSelf, nItem, &lvFindInfo)) != -1)
5350 if ((ListView_GetItemState(infoPtr->hwndSelf, nItem, uMask) & uMask) == uMask)
5351 return nItem;
5355 else if (uFlags & LVNI_BELOW)
5357 if ((uView == LVS_LIST) || (uView == LVS_REPORT))
5359 while (nItem < infoPtr->nItemCount)
5361 nItem++;
5362 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
5363 return nItem;
5366 else
5368 lvFindInfo.flags = LVFI_NEARESTXY;
5369 lvFindInfo.vkDirection = VK_DOWN;
5370 ListView_GetItemPosition(infoPtr->hwndSelf, nItem, &lvFindInfo.pt);
5371 while ((nItem = ListView_FindItemW(infoPtr->hwndSelf, nItem, &lvFindInfo)) != -1)
5373 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
5374 return nItem;
5378 else if (uFlags & LVNI_TOLEFT)
5380 if (uView == LVS_LIST)
5382 nCountPerColumn = LISTVIEW_GetCountPerColumn(infoPtr);
5383 while (nItem - nCountPerColumn >= 0)
5385 nItem -= nCountPerColumn;
5386 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
5387 return nItem;
5390 else if ((uView == LVS_SMALLICON) || (uView == LVS_ICON))
5392 lvFindInfo.flags = LVFI_NEARESTXY;
5393 lvFindInfo.vkDirection = VK_LEFT;
5394 ListView_GetItemPosition(infoPtr->hwndSelf, nItem, &lvFindInfo.pt);
5395 while ((nItem = ListView_FindItemW(infoPtr->hwndSelf, nItem, &lvFindInfo)) != -1)
5397 if ((ListView_GetItemState(infoPtr->hwndSelf, nItem, uMask) & uMask) == uMask)
5398 return nItem;
5402 else if (uFlags & LVNI_TORIGHT)
5404 if (uView == LVS_LIST)
5406 nCountPerColumn = LISTVIEW_GetCountPerColumn(infoPtr);
5407 while (nItem + nCountPerColumn < infoPtr->nItemCount)
5409 nItem += nCountPerColumn;
5410 if ((ListView_GetItemState(infoPtr->hwndSelf, nItem, uMask) & uMask) == uMask)
5411 return nItem;
5414 else if ((uView == LVS_SMALLICON) || (uView == LVS_ICON))
5416 lvFindInfo.flags = LVFI_NEARESTXY;
5417 lvFindInfo.vkDirection = VK_RIGHT;
5418 ListView_GetItemPosition(infoPtr->hwndSelf, nItem, &lvFindInfo.pt);
5419 while ((nItem = ListView_FindItemW(infoPtr->hwndSelf, nItem, &lvFindInfo)) != -1)
5421 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
5422 return nItem;
5426 else
5428 nItem++;
5430 /* search by index */
5431 for (i = nItem; i < infoPtr->nItemCount; i++)
5433 if ((LISTVIEW_GetItemState(infoPtr, i, uMask) & uMask) == uMask)
5434 return i;
5438 return -1;
5441 /* LISTVIEW_GetNumberOfWorkAreas */
5443 /***
5444 * DESCRIPTION:
5445 * Retrieves the origin coordinates when in icon or small icon display mode.
5447 * PARAMETER(S):
5448 * [I] infoPtr : valid pointer to the listview structure
5449 * [O] lpptOrigin : coordinate information
5451 * RETURN:
5452 * SUCCESS : TRUE
5453 * FAILURE : FALSE
5455 static BOOL LISTVIEW_GetOrigin(LISTVIEW_INFO *infoPtr, LPPOINT lpptOrigin)
5457 DWORD lStyle = infoPtr->dwStyle;
5458 UINT uView = lStyle & LVS_TYPEMASK;
5459 INT nHorzPos = 0, nVertPos = 0;
5460 SCROLLINFO scrollInfo;
5462 if (!lpptOrigin) return FALSE;
5464 scrollInfo.cbSize = sizeof(SCROLLINFO);
5465 scrollInfo.fMask = SIF_POS;
5467 if ((lStyle & WS_HSCROLL) && GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo))
5468 nHorzPos = scrollInfo.nPos;
5469 if ((lStyle & WS_VSCROLL) && GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo))
5470 nVertPos = scrollInfo.nPos;
5472 TRACE("nHorzPos=%d, nVertPos=%d\n", nHorzPos, nVertPos);
5474 lpptOrigin->x = infoPtr->rcList.left;
5475 lpptOrigin->y = infoPtr->rcList.top;
5476 if (uView == LVS_LIST)
5477 nHorzPos *= infoPtr->nItemWidth;
5478 else if (uView == LVS_REPORT)
5479 nVertPos *= infoPtr->nItemHeight;
5481 lpptOrigin->x -= nHorzPos;
5482 lpptOrigin->y -= nVertPos;
5484 TRACE(" origin=%s\n", debugpoint(lpptOrigin));
5486 return TRUE;
5489 /***
5490 * DESCRIPTION:
5491 * Retrieves the width of a string.
5493 * PARAMETER(S):
5494 * [I] hwnd : window handle
5495 * [I] lpszText : text string to process
5496 * [I] isW : TRUE if lpszText is Unicode, FALSE otherwise
5498 * RETURN:
5499 * SUCCESS : string width (in pixels)
5500 * FAILURE : zero
5502 static LRESULT LISTVIEW_GetStringWidthT(LISTVIEW_INFO *infoPtr, LPCWSTR lpszText, BOOL isW)
5504 SIZE stringSize;
5506 stringSize.cx = 0;
5507 if (is_textT(lpszText, isW))
5509 HFONT hFont = infoPtr->hFont ? infoPtr->hFont : infoPtr->hDefaultFont;
5510 HDC hdc = GetDC(infoPtr->hwndSelf);
5511 HFONT hOldFont = SelectObject(hdc, hFont);
5513 if (isW)
5514 GetTextExtentPointW(hdc, lpszText, lstrlenW(lpszText), &stringSize);
5515 else
5516 GetTextExtentPointA(hdc, (LPCSTR)lpszText, lstrlenA((LPCSTR)lpszText), &stringSize);
5517 SelectObject(hdc, hOldFont);
5518 ReleaseDC(infoPtr->hwndSelf, hdc);
5520 return stringSize.cx;
5523 /***
5524 * DESCRIPTION:
5525 * Determines which listview item is located at the specified position.
5527 * PARAMETER(S):
5528 * [I] infoPtr : valid pointer to the listview structure
5529 * [IO] lpht : hit test information
5530 * [I] subitem : fill out iSubItem.
5531 * [I] select : return the index only if the hit selects the item
5533 * NOTE:
5534 * (mm 20001022): We must not allow iSubItem to be touched, for
5535 * an app might pass only a structure with space up to iItem!
5536 * (MS Office 97 does that for instance in the file open dialog)
5538 * RETURN:
5539 * SUCCESS : item index
5540 * FAILURE : -1
5542 static LRESULT LISTVIEW_HitTest(LISTVIEW_INFO *infoPtr, LPLVHITTESTINFO lpht, BOOL subitem, BOOL select)
5544 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
5545 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
5546 RECT rcBox, rcBounds, rcState, rcIcon, rcLabel, rcSearch;
5547 POINT Origin, Position, opt;
5548 LVITEMW lvItem;
5549 ITERATOR i;
5551 TRACE("(pt=%s, subitem=%d, select=%d)\n", debugpoint(&lpht->pt), subitem, select);
5553 lpht->flags = 0;
5554 lpht->iItem = -1;
5555 if (subitem) lpht->iSubItem = 0;
5557 if (infoPtr->rcList.left > lpht->pt.x)
5558 lpht->flags |= LVHT_TOLEFT;
5559 else if (infoPtr->rcList.right < lpht->pt.x)
5560 lpht->flags |= LVHT_TORIGHT;
5562 if (infoPtr->rcList.top > lpht->pt.y)
5563 lpht->flags |= LVHT_ABOVE;
5564 else if (infoPtr->rcList.bottom < lpht->pt.y)
5565 lpht->flags |= LVHT_BELOW;
5567 TRACE("lpht->flags=0x%x\n", lpht->flags);
5568 if (lpht->flags) return -1;
5570 lpht->flags |= LVHT_NOWHERE;
5572 if (!LISTVIEW_GetOrigin(infoPtr, &Origin)) return -1;
5574 /* first deal with the large items */
5575 rcSearch.left = lpht->pt.x;
5576 rcSearch.top = lpht->pt.y;
5577 rcSearch.right = rcSearch.left + 1;
5578 rcSearch.bottom = rcSearch.top + 1;
5580 iterator_frameditems(&i, infoPtr, &rcSearch);
5581 iterator_next(&i); /* go to first item in the sequence */
5582 lpht->iItem = i.nItem;
5583 iterator_destroy(&i);
5585 TRACE("lpht->iItem=%d\n", lpht->iItem);
5586 if (lpht->iItem == -1) return -1;
5588 lvItem.mask = LVIF_STATE | LVIF_TEXT;
5589 if (uView == LVS_REPORT) lvItem.mask |= LVIF_INDENT;
5590 lvItem.stateMask = LVIS_STATEIMAGEMASK;
5591 if (uView == LVS_ICON) lvItem.stateMask |= LVIS_FOCUSED;
5592 lvItem.iItem = lpht->iItem;
5593 lvItem.iSubItem = 0;
5594 lvItem.pszText = szDispText;
5595 lvItem.cchTextMax = DISP_TEXT_SIZE;
5596 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) return -1;
5597 if (!infoPtr->bFocus) lvItem.state &= ~LVIS_FOCUSED;
5599 if (!LISTVIEW_GetItemMetrics(infoPtr, &lvItem, &rcBox, &rcState, &rcIcon, &rcLabel)) return -1;
5600 if (!LISTVIEW_GetItemOrigin(infoPtr, lpht->iItem, &Position)) return -1;
5601 opt.x = lpht->pt.x - Position.x - Origin.x;
5602 opt.y = lpht->pt.y - Position.y - Origin.y;
5604 if (uView == LVS_REPORT && (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT))
5605 rcBounds = rcBox;
5606 else
5607 UnionRect(&rcBounds, &rcIcon, &rcLabel);
5608 TRACE("rcBounds=%s\n", debugrect(&rcBounds));
5609 if (!PtInRect(&rcBounds, opt)) return -1;
5611 if (PtInRect(&rcIcon, opt))
5612 lpht->flags |= LVHT_ONITEMICON;
5613 else if (PtInRect(&rcLabel, opt))
5614 lpht->flags |= LVHT_ONITEMLABEL;
5615 else if (infoPtr->himlState && ((lvItem.state & LVIS_STATEIMAGEMASK) >> 12) && PtInRect(&rcState, opt))
5616 lpht->flags |= LVHT_ONITEMSTATEICON;
5617 if (lpht->flags & LVHT_ONITEM)
5618 lpht->flags &= ~LVHT_NOWHERE;
5620 TRACE("lpht->flags=0x%x\n", lpht->flags);
5621 if (uView == LVS_REPORT && lpht->iItem != -1 && subitem)
5623 INT j;
5625 rcBounds.right = rcBounds.left;
5626 for (j = 0; j < infoPtr->hdpaColumns->nItemCount; j++)
5628 rcBounds.left = rcBounds.right;
5629 rcBounds.right += LISTVIEW_GetColumnWidth(infoPtr, j);
5630 if (PtInRect(&rcBounds, opt))
5632 lpht->iSubItem = j;
5633 break;
5638 if (!select || lpht->iItem == -1) return lpht->iItem;
5640 if (uView == LVS_REPORT && (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT)) return lpht->iItem;
5642 if (uView == LVS_REPORT) UnionRect(&rcBounds, &rcIcon, &rcLabel);
5643 return PtInRect(&rcBounds, opt) ? lpht->iItem : -1;
5647 /***
5648 * DESCRIPTION:
5649 * Inserts a new column.
5651 * PARAMETER(S):
5652 * [I] infoPtr : valid pointer to the listview structure
5653 * [I] INT : column index
5654 * [I] LPLVCOLUMNW : column information
5656 * RETURN:
5657 * SUCCESS : new column index
5658 * FAILURE : -1
5660 static LRESULT LISTVIEW_InsertColumnT(LISTVIEW_INFO *infoPtr, INT nColumn,
5661 LPLVCOLUMNW lpColumn, BOOL isW)
5663 COLUMN_INFO *lpColumnInfo = NULL;
5664 RECT rcCol;
5665 INT nNewColumn;
5666 HDITEMW hdi;
5668 TRACE("(nColumn=%d, lpColumn=%s, isW=%d)\n", nColumn, debuglvcolumn_t(lpColumn, isW), isW);
5670 if (!lpColumn) return -1;
5672 hdi.mask = hdi.fmt = 0;
5673 if (lpColumn->mask & LVCF_FMT)
5675 /* format member is valid */
5676 hdi.mask |= HDI_FORMAT;
5678 /* set text alignment (leftmost column must be left-aligned) */
5679 if (nColumn == 0 || lpColumn->fmt & LVCFMT_LEFT)
5680 hdi.fmt |= HDF_LEFT;
5681 else if (lpColumn->fmt & LVCFMT_RIGHT)
5682 hdi.fmt |= HDF_RIGHT;
5683 else if (lpColumn->fmt & LVCFMT_CENTER)
5684 hdi.fmt |= HDF_CENTER;
5686 if (lpColumn->fmt & LVCFMT_BITMAP_ON_RIGHT)
5687 hdi.fmt |= HDF_BITMAP_ON_RIGHT;
5689 if (lpColumn->fmt & LVCFMT_COL_HAS_IMAGES)
5691 hdi.fmt |= HDF_IMAGE;
5692 hdi.iImage = I_IMAGECALLBACK;
5696 if (lpColumn->mask & LVCF_WIDTH)
5698 hdi.mask |= HDI_WIDTH;
5699 if(lpColumn->cx == LVSCW_AUTOSIZE_USEHEADER)
5701 /* make it fill the remainder of the controls width */
5702 RECT rcHeader;
5703 INT item_index;
5705 for(item_index = 0; item_index < (nColumn - 1); item_index++)
5706 if (LISTVIEW_GetHeaderRect(infoPtr, item_index, &rcHeader))
5707 hdi.cxy += rcHeader.right - rcHeader.left;
5709 /* retrieve the layout of the header */
5710 GetClientRect(infoPtr->hwndSelf, &rcHeader);
5711 TRACE("start cxy=%d rcHeader=%s\n", hdi.cxy, debugrect(&rcHeader));
5713 hdi.cxy = (rcHeader.right - rcHeader.left) - hdi.cxy;
5715 else
5716 hdi.cxy = lpColumn->cx;
5719 if (lpColumn->mask & LVCF_TEXT)
5721 hdi.mask |= HDI_TEXT | HDI_FORMAT;
5722 hdi.fmt |= HDF_STRING;
5723 hdi.pszText = lpColumn->pszText;
5724 hdi.cchTextMax = textlenT(lpColumn->pszText, isW);
5727 if (lpColumn->mask & LVCF_IMAGE)
5729 hdi.mask |= HDI_IMAGE;
5730 hdi.iImage = lpColumn->iImage;
5733 if (lpColumn->mask & LVCF_ORDER)
5735 hdi.mask |= HDI_ORDER;
5736 hdi.iOrder = lpColumn->iOrder;
5739 /* insert item in header control */
5740 nNewColumn = SendMessageW(infoPtr->hwndHeader,
5741 isW ? HDM_INSERTITEMW : HDM_INSERTITEMA,
5742 (WPARAM)nColumn, (LPARAM)&hdi);
5743 if (nNewColumn == -1) return -1;
5745 /* create our own column info */
5746 if (!(lpColumnInfo = COMCTL32_Alloc(sizeof(COLUMN_INFO)))) goto fail;
5747 if (DPA_InsertPtr(infoPtr->hdpaColumns, nNewColumn, lpColumnInfo) == -1) goto fail;
5748 if (!Header_GetItemRect(infoPtr->hwndHeader, nNewColumn, &rcCol)) goto fail;
5749 lpColumnInfo->rcHeader = rcCol;
5750 if (lpColumn->mask & LVCF_FMT)
5752 if (nColumn == 0 || lpColumn->fmt & LVCFMT_LEFT) lpColumnInfo->align = DT_LEFT;
5753 else if (lpColumn->fmt & LVCFMT_RIGHT) lpColumnInfo->align = DT_RIGHT;
5754 else if (lpColumn->fmt & LVCFMT_CENTER) lpColumnInfo->align = DT_CENTER;
5756 if (lpColumn->fmt & LVCFMT_IMAGE) lpColumnInfo->hasImage = TRUE;
5758 else
5760 lpColumnInfo->align = DT_LEFT;
5761 lpColumnInfo->hasImage = (nColumn == 0);
5764 /* now we have to actually adjust the data */
5765 if (!(infoPtr->dwStyle & LVS_OWNERDATA) && infoPtr->nItemCount > 0)
5767 LISTVIEW_SUBITEM *lpSubItem, *lpMainItem, **lpNewItems = 0;
5768 HDPA hdpaSubItems;
5769 INT nItem, i;
5771 /* preallocate memory, so we can fail gracefully */
5772 if (nNewColumn == 0)
5774 lpNewItems = COMCTL32_Alloc(sizeof(LISTVIEW_SUBITEM *) * infoPtr->nItemCount);
5775 if (!lpNewItems) goto fail;
5776 for (i = 0; i < infoPtr->nItemCount; i++)
5777 if (!(lpNewItems[i] = COMCTL32_Alloc(sizeof(LISTVIEW_SUBITEM)))) break;
5778 if (i != infoPtr->nItemCount)
5780 for(; i >=0; i--) COMCTL32_Free(lpNewItems[i]);
5781 COMCTL32_Free(lpNewItems);
5782 goto fail;
5786 for (nItem = 0; nItem < infoPtr->nItemCount; nItem++)
5788 hdpaSubItems = (HDPA)DPA_GetPtr(infoPtr->hdpaItems, nItem);
5789 if (!hdpaSubItems) continue;
5790 for (i = 1; i < hdpaSubItems->nItemCount; i++)
5792 lpSubItem = (LISTVIEW_SUBITEM *)DPA_GetPtr(hdpaSubItems, i);
5793 if (!lpSubItem) break;
5794 if (lpSubItem->iSubItem >= nNewColumn)
5795 lpSubItem->iSubItem++;
5798 /* if we found our subitem, zapp it */
5799 if (nNewColumn == 0)
5801 lpMainItem = (LISTVIEW_SUBITEM *)DPA_GetPtr(hdpaSubItems, 0);
5802 lpSubItem = lpNewItems[nItem];
5803 lpSubItem->hdr = lpMainItem->hdr;
5804 lpSubItem->iSubItem = 1;
5805 ZeroMemory(&lpMainItem->hdr, sizeof(lpMainItem->hdr));
5806 lpMainItem->iSubItem = 0;
5807 DPA_InsertPtr(hdpaSubItems, 1, lpSubItem);
5811 COMCTL32_Free(lpNewItems);
5814 /* make space for the new column */
5815 LISTVIEW_ScrollColumns(infoPtr, nNewColumn + 1, rcCol.right - rcCol.left);
5817 return nNewColumn;
5819 fail:
5820 if (nNewColumn != -1) SendMessageW(infoPtr->hwndHeader, HDM_DELETEITEM, nNewColumn, 0);
5821 if (lpColumnInfo)
5823 DPA_DeletePtr(infoPtr->hdpaColumns, nNewColumn);
5824 COMCTL32_Free(lpColumnInfo);
5826 return -1;
5829 /* LISTVIEW_InsertCompare: callback routine for comparing pszText members of the LV_ITEMS
5830 in a LISTVIEW on insert. Passed to DPA_Sort in LISTVIEW_InsertItem.
5831 This function should only be used for inserting items into a sorted list (LVM_INSERTITEM)
5832 and not during the processing of a LVM_SORTITEMS message. Applications should provide
5833 their own sort proc. when sending LVM_SORTITEMS.
5835 /* Platform SDK:
5836 (remarks on LVITEM: LVM_INSERTITEM will insert the new item in the proper sort postion...
5838 LVS_SORTXXX must be specified,
5839 LVS_OWNERDRAW is not set,
5840 <item>.pszText is not LPSTR_TEXTCALLBACK.
5842 (LVS_SORT* flags): "For the LVS_SORTASCENDING... styles, item indices
5843 are sorted based on item text..."
5845 static INT WINAPI LISTVIEW_InsertCompare( LPVOID first, LPVOID second, LPARAM lParam)
5847 LISTVIEW_ITEM* lv_first = (LISTVIEW_ITEM*) DPA_GetPtr( (HDPA)first, 0 );
5848 LISTVIEW_ITEM* lv_second = (LISTVIEW_ITEM*) DPA_GetPtr( (HDPA)second, 0 );
5849 INT cmpv = textcmpWT(lv_first->hdr.pszText, lv_second->hdr.pszText, TRUE);
5851 /* if we're sorting descending, negate the return value */
5852 return (((LISTVIEW_INFO *)lParam)->dwStyle & LVS_SORTDESCENDING) ? -cmpv : cmpv;
5855 /***
5856 * nESCRIPTION:
5857 * Inserts a new item in the listview control.
5859 * PARAMETER(S):
5860 * [I] infoPtr : valid pointer to the listview structure
5861 * [I] lpLVItem : item information
5862 * [I] isW : TRUE if lpLVItem is Unicode, FALSE if it's ANSI
5864 * RETURN:
5865 * SUCCESS : new item index
5866 * FAILURE : -1
5868 static LRESULT LISTVIEW_InsertItemT(LISTVIEW_INFO *infoPtr, LPLVITEMW lpLVItem, BOOL isW)
5870 LONG lStyle = infoPtr->dwStyle;
5871 UINT uView = lStyle & LVS_TYPEMASK;
5872 INT nItem = -1;
5873 HDPA hdpaSubItems;
5874 NMLISTVIEW nmlv;
5875 LISTVIEW_ITEM *lpItem;
5876 BOOL is_sorted, has_changed;
5878 TRACE("(lpLVItem=%s, isW=%d)\n", debuglvitem_t(lpLVItem, isW), isW);
5880 if (lStyle & LVS_OWNERDATA)
5882 nItem = infoPtr->nItemCount;
5883 infoPtr->nItemCount++;
5884 return nItem;
5887 /* make sure it's an item, and not a subitem; cannot insert a subitem */
5888 if (!lpLVItem || lpLVItem->iItem < 0 || lpLVItem->iSubItem) return -1;
5890 if (!is_assignable_item(lpLVItem, lStyle)) return -1;
5892 if ( !(lpItem = (LISTVIEW_ITEM *)COMCTL32_Alloc(sizeof(LISTVIEW_ITEM))) )
5893 return -1;
5895 /* insert item in listview control data structure */
5896 if ( (hdpaSubItems = DPA_Create(8)) )
5897 nItem = DPA_InsertPtr(hdpaSubItems, 0, lpItem);
5898 if (nItem == -1) goto fail;
5900 /* FIXME: is the handling of this LVS_OWNERDRAWFIXED correct? */
5901 is_sorted = (lStyle & (LVS_SORTASCENDING | LVS_SORTDESCENDING)) &&
5902 !(lStyle & LVS_OWNERDRAWFIXED) && (LPSTR_TEXTCALLBACKW != lpLVItem->pszText);
5904 nItem = DPA_InsertPtr( infoPtr->hdpaItems,
5905 is_sorted ? infoPtr->nItemCount + 1 : lpLVItem->iItem,
5906 hdpaSubItems );
5907 if (nItem == -1) goto fail;
5908 /* the array may be sparsly populated, we can't just increment the count here */
5909 infoPtr->nItemCount = infoPtr->hdpaItems->nItemCount;
5911 /* set the item attributes */
5912 if (!set_main_item(infoPtr, lpLVItem, TRUE, isW, &has_changed)) goto undo;
5914 /* if we're sorted, sort the list, and update the index */
5915 if (is_sorted)
5917 DPA_Sort( infoPtr->hdpaItems, LISTVIEW_InsertCompare, (LPARAM)infoPtr );
5918 nItem = DPA_GetPtrIndex( infoPtr->hdpaItems, hdpaSubItems );
5919 assert(nItem != -1);
5922 /* make room for the position, if we are in the right mode */
5923 if ((uView == LVS_SMALLICON) || (uView == LVS_ICON))
5925 if (DPA_InsertPtr(infoPtr->hdpaPosX, nItem, 0) == -1)
5926 goto undo;
5927 if (DPA_InsertPtr(infoPtr->hdpaPosY, nItem, 0) == -1)
5929 DPA_DeletePtr(infoPtr->hdpaPosX, nItem);
5930 goto undo;
5934 /* Add the subitem list to the items array. Do this last in case we go to
5935 * fail during the above.
5937 LISTVIEW_ShiftIndices(infoPtr, nItem, 1);
5939 /* send LVN_INSERTITEM notification */
5940 ZeroMemory(&nmlv, sizeof(NMLISTVIEW));
5941 nmlv.iItem = nItem;
5942 nmlv.lParam = lpItem->lParam;
5943 notify_listview(infoPtr, LVN_INSERTITEM, &nmlv);
5945 /* align items (set position of each item) */
5946 if ((uView == LVS_SMALLICON) || (uView == LVS_ICON))
5948 if (lStyle & LVS_ALIGNLEFT) LISTVIEW_AlignLeft(infoPtr);
5949 else LISTVIEW_AlignTop(infoPtr);
5952 LISTVIEW_UpdateScroll(infoPtr);
5954 LISTVIEW_InvalidateList(infoPtr); /* FIXME: optimize */
5956 TRACE(" <- %d\n", nItem);
5957 return nItem;
5959 undo:
5960 DPA_DeletePtr(infoPtr->hdpaItems, nItem);
5961 infoPtr->nItemCount--;
5962 fail:
5963 DPA_DeletePtr(hdpaSubItems, 0);
5964 DPA_Destroy (hdpaSubItems);
5965 COMCTL32_Free (lpItem);
5966 return -1;
5969 /***
5970 * DESCRIPTION:
5971 * Redraws a range of items.
5973 * PARAMETER(S):
5974 * [I] infoPtr : valid pointer to the listview structure
5975 * [I] INT : first item
5976 * [I] INT : last item
5978 * RETURN:
5979 * SUCCESS : TRUE
5980 * FAILURE : FALSE
5982 static LRESULT LISTVIEW_RedrawItems(LISTVIEW_INFO *infoPtr, INT nFirst, INT nLast)
5984 INT i;
5986 if (nLast < nFirst || min(nFirst, nLast) < 0 ||
5987 max(nFirst, nLast) >= infoPtr->nItemCount)
5988 return FALSE;
5990 for (i = nFirst; i <= nLast; i++)
5991 LISTVIEW_InvalidateItem(infoPtr, i);
5993 return TRUE;
5996 /***
5997 * DESCRIPTION:
5998 * Scroll the content of a listview.
6000 * PARAMETER(S):
6001 * [I] infoPtr : valid pointer to the listview structure
6002 * [I] INT : horizontal scroll amount in pixels
6003 * [I] INT : vertical scroll amount in pixels
6005 * RETURN:
6006 * SUCCESS : TRUE
6007 * FAILURE : FALSE
6009 * COMMENTS:
6010 * If the control is in report mode (LVS_REPORT) the control can
6011 * be scrolled only in line increments. "dy" will be rounded to the
6012 * nearest number of pixels that are a whole line. Ex: if line height
6013 * is 16 and an 8 is passed, the list will be scrolled by 16. If a 7
6014 * is passed the the scroll will be 0. (per MSDN 7/2002)
6016 * For: (per experimentaion with native control and CSpy ListView)
6017 * LVS_ICON dy=1 = 1 pixel (vertical only)
6018 * dx ignored
6019 * LVS_SMALLICON dy=1 = 1 pixel (vertical only)
6020 * dx ignored
6021 * LVS_LIST dx=1 = 1 column (horizontal only)
6022 * but will only scroll 1 column per message
6023 * no matter what the value.
6024 * dy must be 0 or FALSE returned.
6025 * LVS_REPORT dx=1 = 1 pixel
6026 * dy= see above
6029 static LRESULT LISTVIEW_Scroll(LISTVIEW_INFO *infoPtr, INT dx, INT dy)
6031 switch(infoPtr->dwStyle & LVS_TYPEMASK) {
6032 case LVS_REPORT:
6033 dy += (dy < 0 ? -1 : 1) * infoPtr->nItemHeight/2;
6034 dy /= infoPtr->nItemHeight;
6035 break;
6036 case LVS_LIST:
6037 if (dy != 0) return FALSE;
6038 break;
6039 default: /* icon */
6040 dx = 0;
6041 break;
6044 if (dx != 0) LISTVIEW_HScroll(infoPtr, SB_INTERNAL, dx, 0);
6045 if (dy != 0) LISTVIEW_VScroll(infoPtr, SB_INTERNAL, dy, 0);
6047 return TRUE;
6050 /***
6051 * DESCRIPTION:
6052 * Sets the background color.
6054 * PARAMETER(S):
6055 * [I] infoPtr : valid pointer to the listview structure
6056 * [I] COLORREF : background color
6058 * RETURN:
6059 * SUCCESS : TRUE
6060 * FAILURE : FALSE
6062 static LRESULT LISTVIEW_SetBkColor(LISTVIEW_INFO *infoPtr, COLORREF clrBk)
6064 TRACE("(clrBk=%lx)\n", clrBk);
6066 if(infoPtr->clrBk != clrBk) {
6067 if (infoPtr->clrBk != CLR_NONE) DeleteObject(infoPtr->hBkBrush);
6068 infoPtr->clrBk = clrBk;
6069 if (clrBk == CLR_NONE)
6070 infoPtr->hBkBrush = GetClassLongW(infoPtr->hwndSelf, GCL_HBRBACKGROUND);
6071 else
6072 infoPtr->hBkBrush = CreateSolidBrush(clrBk);
6073 LISTVIEW_InvalidateList(infoPtr);
6076 return TRUE;
6079 /* LISTVIEW_SetBkImage */
6081 /***
6082 * DESCRIPTION:
6083 * Sets the attributes of a header item.
6085 * PARAMETER(S):
6086 * [I] infoPtr : valid pointer to the listview structure
6087 * [I] INT : column index
6088 * [I] LPLVCOLUMNW : column attributes
6089 * [I] isW: if TRUE, the lpColumn is a LPLVCOLUMNW,
6090 * otherwise it is in fact a LPLVCOLUMNA
6092 * RETURN:
6093 * SUCCESS : TRUE
6094 * FAILURE : FALSE
6096 static LRESULT LISTVIEW_SetColumnT(LISTVIEW_INFO *infoPtr, INT nColumn,
6097 LPLVCOLUMNW lpColumn, BOOL isW)
6099 BOOL bResult = FALSE;
6100 HDITEMW hdi, hdiget;
6102 if ((lpColumn != NULL) && (nColumn >= 0) && (nColumn < infoPtr->hdpaColumns->nItemCount))
6104 /* initialize memory */
6105 ZeroMemory(&hdi, sizeof(hdi));
6107 if (lpColumn->mask & LVCF_FMT)
6109 /* format member is valid */
6110 hdi.mask |= HDI_FORMAT;
6112 /* get current format first */
6113 hdiget.mask = HDI_FORMAT;
6114 if (Header_GetItemW(infoPtr->hwndHeader, nColumn, &hdiget))
6115 /* preserve HDF_STRING if present */
6116 hdi.fmt = hdiget.fmt & HDF_STRING;
6118 /* set text alignment (leftmost column must be left-aligned) */
6119 if (nColumn == 0)
6121 hdi.fmt |= HDF_LEFT;
6123 else
6125 if (lpColumn->fmt & LVCFMT_LEFT)
6126 hdi.fmt |= HDF_LEFT;
6127 else if (lpColumn->fmt & LVCFMT_RIGHT)
6128 hdi.fmt |= HDF_RIGHT;
6129 else if (lpColumn->fmt & LVCFMT_CENTER)
6130 hdi.fmt |= HDF_CENTER;
6133 if (lpColumn->fmt & LVCFMT_BITMAP_ON_RIGHT)
6134 hdi.fmt |= HDF_BITMAP_ON_RIGHT;
6136 if (lpColumn->fmt & LVCFMT_COL_HAS_IMAGES)
6137 hdi.fmt |= HDF_IMAGE;
6139 if (lpColumn->fmt & LVCFMT_IMAGE)
6141 hdi.fmt |= HDF_IMAGE;
6142 hdi.iImage = I_IMAGECALLBACK;
6146 if (lpColumn->mask & LVCF_WIDTH)
6148 hdi.mask |= HDI_WIDTH;
6149 hdi.cxy = lpColumn->cx;
6152 if (lpColumn->mask & LVCF_TEXT)
6154 hdi.mask |= HDI_TEXT | HDI_FORMAT;
6155 hdi.pszText = lpColumn->pszText;
6156 hdi.cchTextMax = textlenT(lpColumn->pszText, isW);
6157 hdi.fmt |= HDF_STRING;
6160 if (lpColumn->mask & LVCF_IMAGE)
6162 hdi.mask |= HDI_IMAGE;
6163 hdi.iImage = lpColumn->iImage;
6166 if (lpColumn->mask & LVCF_ORDER)
6168 hdi.mask |= HDI_ORDER;
6169 hdi.iOrder = lpColumn->iOrder;
6172 /* set header item attributes */
6173 if (isW)
6174 bResult = Header_SetItemW(infoPtr->hwndHeader, nColumn, &hdi);
6175 else
6176 bResult = Header_SetItemA(infoPtr->hwndHeader, nColumn, &hdi);
6179 return bResult;
6182 /***
6183 * DESCRIPTION:
6184 * Sets the column order array
6186 * PARAMETERS:
6187 * [I] infoPtr : valid pointer to the listview structure
6188 * [I] INT : number of elements in column order array
6189 * [I] INT : pointer to column order array
6191 * RETURN:
6192 * SUCCESS : TRUE
6193 * FAILURE : FALSE
6195 static LRESULT LISTVIEW_SetColumnOrderArray(LISTVIEW_INFO *infoPtr, INT iCount, LPINT lpiArray)
6197 FIXME("iCount %d lpiArray %p\n", iCount, lpiArray);
6199 if (!lpiArray)
6200 return FALSE;
6202 return TRUE;
6206 /***
6207 * DESCRIPTION:
6208 * Sets the width of a column
6210 * PARAMETERS:
6211 * [I] infoPtr : valid pointer to the listview structure
6212 * [I] INT : column index
6213 * [I] INT : column width
6215 * RETURN:
6216 * SUCCESS : TRUE
6217 * FAILURE : FALSE
6219 static LRESULT LISTVIEW_SetColumnWidth(LISTVIEW_INFO *infoPtr, INT iCol, INT cx)
6221 HDITEMW hdi;
6222 LRESULT lret;
6223 LONG lStyle = infoPtr->dwStyle;
6224 UINT uView = lStyle & LVS_TYPEMASK;
6225 HDC hdc;
6226 HFONT header_font;
6227 HFONT old_font;
6228 SIZE size;
6229 WCHAR text_buffer[DISP_TEXT_SIZE];
6230 INT header_item_count;
6231 INT item_index;
6232 INT nLabelWidth;
6233 RECT rcHeader;
6234 LVITEMW lvItem;
6235 WCHAR szDispText[DISP_TEXT_SIZE] = { 0 };
6237 TRACE("(iCol=%d, cx=%d\n", iCol, cx);
6239 /* set column width only if in report or list mode */
6240 if (uView != LVS_REPORT && uView != LVS_LIST) return FALSE;
6242 /* take care of invalid cx values */
6243 if(uView == LVS_REPORT && cx < -2) cx = LVSCW_AUTOSIZE;
6244 else if (uView == LVS_LIST && cx < 1) return FALSE;
6246 /* resize all columns if in LVS_LIST mode */
6247 if(uView == LVS_LIST)
6249 infoPtr->nItemWidth = cx;
6250 LISTVIEW_InvalidateList(infoPtr);
6251 return TRUE;
6254 /* autosize based on listview items width */
6255 if(cx == LVSCW_AUTOSIZE)
6257 /* set the width of the column to the width of the widest item */
6258 if (iCol == 0 || uView == LVS_LIST)
6260 cx = 0;
6261 for(item_index = 0; item_index < infoPtr->nItemCount; item_index++)
6263 nLabelWidth = LISTVIEW_GetLabelWidth(infoPtr, item_index);
6264 cx = (nLabelWidth>cx)?nLabelWidth:cx;
6266 if (infoPtr->himlSmall)
6267 cx += infoPtr->iconSize.cx + IMAGE_PADDING;
6269 else
6271 lvItem.iSubItem = iCol;
6272 lvItem.mask = LVIF_TEXT;
6273 lvItem.pszText = szDispText;
6274 lvItem.cchTextMax = DISP_TEXT_SIZE;
6275 cx = 0;
6276 for(item_index = 0; item_index < infoPtr->nItemCount; item_index++)
6278 lvItem.iItem = item_index;
6279 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) continue;
6280 nLabelWidth = LISTVIEW_GetStringWidthT(infoPtr, lvItem.pszText, TRUE);
6281 cx = (nLabelWidth>cx)?nLabelWidth:cx;
6284 cx += TRAILING_PADDING;
6285 } /* autosize based on listview header width */
6286 else if(cx == LVSCW_AUTOSIZE_USEHEADER)
6288 header_item_count = infoPtr->hdpaColumns->nItemCount;
6290 /* if iCol is the last column make it fill the remainder of the controls width */
6291 if(iCol == (header_item_count - 1)) {
6292 cx = 0;
6294 for(item_index = 0; item_index < (header_item_count - 1); item_index++)
6296 if (LISTVIEW_GetHeaderRect(infoPtr, item_index, &rcHeader))
6297 cx += rcHeader.right - rcHeader.left;
6300 /* retrieve the layout of the header */
6301 GetWindowRect(infoPtr->hwndHeader, &rcHeader);
6303 cx = (rcHeader.right - rcHeader.left) - cx;
6305 else
6307 /* Despite what the MS docs say, if this is not the last
6308 column, then MS resizes the column to the width of the
6309 largest text string in the column, including headers
6310 and items. This is different from LVSCW_AUTOSIZE in that
6311 LVSCW_AUTOSIZE ignores the header string length.
6314 /* retrieve header font */
6315 header_font = SendMessageW(infoPtr->hwndHeader, WM_GETFONT, 0L, 0L);
6317 /* retrieve header text */
6318 hdi.mask = HDI_TEXT;
6319 hdi.cchTextMax = sizeof(text_buffer)/sizeof(text_buffer[0]);
6320 hdi.pszText = text_buffer;
6322 Header_GetItemW(infoPtr->hwndHeader, iCol, (LPARAM)(&hdi));
6324 /* determine the width of the text in the header */
6325 hdc = GetDC(infoPtr->hwndSelf);
6326 old_font = SelectObject(hdc, header_font); /* select the font into hdc */
6328 GetTextExtentPoint32W(hdc, text_buffer, lstrlenW(text_buffer), &size);
6330 SelectObject(hdc, old_font); /* restore the old font */
6331 ReleaseDC(infoPtr->hwndSelf, hdc);
6333 lvItem.iSubItem = iCol;
6334 lvItem.mask = LVIF_TEXT;
6335 lvItem.pszText = szDispText;
6336 lvItem.cchTextMax = DISP_TEXT_SIZE;
6337 cx = size.cx;
6338 for(item_index = 0; item_index < infoPtr->nItemCount; item_index++)
6340 lvItem.iItem = item_index;
6341 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) continue;
6342 nLabelWidth = LISTVIEW_GetStringWidthT(infoPtr, lvItem.pszText, TRUE);
6343 nLabelWidth += TRAILING_PADDING;
6344 /* While it is possible for subitems to have icons, even MS messes
6345 up the positioning, so I suspect no applications actually use
6346 them. */
6347 if (item_index == 0 && infoPtr->himlSmall)
6348 nLabelWidth += infoPtr->iconSize.cx + IMAGE_PADDING;
6349 cx = (nLabelWidth>cx)?nLabelWidth:cx;
6354 /* call header to update the column change */
6355 hdi.mask = HDI_WIDTH;
6357 hdi.cxy = cx;
6358 lret = Header_SetItemW(infoPtr->hwndHeader, (WPARAM)iCol, (LPARAM)&hdi);
6360 LISTVIEW_InvalidateList(infoPtr); /* FIXME: optimize */
6362 return lret;
6365 /***
6366 * DESCRIPTION:
6367 * Sets the extended listview style.
6369 * PARAMETERS:
6370 * [I] infoPtr : valid pointer to the listview structure
6371 * [I] DWORD : mask
6372 * [I] DWORD : style
6374 * RETURN:
6375 * SUCCESS : previous style
6376 * FAILURE : 0
6378 static LRESULT LISTVIEW_SetExtendedListViewStyle(LISTVIEW_INFO *infoPtr, DWORD dwMask, DWORD dwStyle)
6380 DWORD dwOldStyle = infoPtr->dwLvExStyle;
6382 /* set new style */
6383 if (dwMask)
6384 infoPtr->dwLvExStyle = (dwOldStyle & ~dwMask) | (dwStyle & dwMask);
6385 else
6386 infoPtr->dwLvExStyle = dwStyle;
6388 return dwOldStyle;
6391 /***
6392 * DESCRIPTION:
6393 * Sets the new hot cursor used during hot tracking and hover selection.
6395 * PARAMETER(S):
6396 * [I] infoPtr : valid pointer to the listview structure
6397 * [I} hCurosr : the new hot cursor handle
6399 * RETURN:
6400 * Returns the previous hot cursor
6402 static HCURSOR LISTVIEW_SetHotCursor(LISTVIEW_INFO *infoPtr, HCURSOR hCursor)
6404 HCURSOR oldCursor = infoPtr->hHotCursor;
6405 infoPtr->hHotCursor = hCursor;
6406 return oldCursor;
6410 /***
6411 * DESCRIPTION:
6412 * Sets the hot item index.
6414 * PARAMETERS:
6415 * [I] infoPtr : valid pointer to the listview structure
6416 * [I] INT : index
6418 * RETURN:
6419 * SUCCESS : previous hot item index
6420 * FAILURE : -1 (no hot item)
6422 static LRESULT LISTVIEW_SetHotItem(LISTVIEW_INFO *infoPtr, INT iIndex)
6424 INT iOldIndex = infoPtr->nHotItem;
6425 infoPtr->nHotItem = iIndex;
6426 return iOldIndex;
6430 /***
6431 * DESCRIPTION:
6432 * Sets the amount of time the cursor must hover over an item before it is selected.
6434 * PARAMETER(S):
6435 * [I] infoPtr : valid pointer to the listview structure
6436 * [I] DWORD : dwHoverTime, if -1 the hover time is set to the default
6438 * RETURN:
6439 * Returns the previous hover time
6441 static LRESULT LISTVIEW_SetHoverTime(LISTVIEW_INFO *infoPtr, DWORD dwHoverTime)
6443 DWORD oldHoverTime = infoPtr->dwHoverTime;
6444 infoPtr->dwHoverTime = dwHoverTime;
6445 return oldHoverTime;
6448 /***
6449 * DESCRIPTION:
6450 * Sets spacing for icons of LVS_ICON style.
6452 * PARAMETER(S):
6453 * [I] infoPtr : valid pointer to the listview structure
6454 * [I] DWORD : MAKELONG(cx, cy)
6456 * RETURN:
6457 * MAKELONG(oldcx, oldcy)
6459 static LRESULT LISTVIEW_SetIconSpacing(LISTVIEW_INFO *infoPtr, DWORD spacing)
6461 INT cy = HIWORD(spacing), cx = LOWORD(spacing);
6462 DWORD oldspacing = MAKELONG(infoPtr->iconSpacing.cx, infoPtr->iconSpacing.cy);
6463 LONG lStyle = infoPtr->dwStyle;
6464 UINT uView = lStyle & LVS_TYPEMASK;
6466 TRACE("requested=(%d,%d)\n", cx, cy);
6468 /* this is supported only for LVS_ICON style */
6469 if (uView != LVS_ICON) return oldspacing;
6471 /* set to defaults, if instructed to */
6472 if (cx == -1) cx = GetSystemMetrics(SM_CXICONSPACING);
6473 if (cy == -1) cy = GetSystemMetrics(SM_CYICONSPACING);
6475 /* if 0 then compute width
6476 * FIXME: Should scan each item and determine max width of
6477 * icon or label, then make that the width */
6478 if (cx == 0)
6479 cx = infoPtr->iconSpacing.cx;
6481 /* if 0 then compute height */
6482 if (cy == 0)
6483 cy = infoPtr->iconSize.cy + 2 * infoPtr->ntmHeight +
6484 ICON_BOTTOM_PADDING + ICON_TOP_PADDING + LABEL_VERT_PADDING;
6487 infoPtr->iconSpacing.cx = cx;
6488 infoPtr->iconSpacing.cy = cy;
6490 TRACE("old=(%d,%d), new=(%d,%d), iconSize=(%ld,%ld), ntmH=%d\n",
6491 LOWORD(oldspacing), HIWORD(oldspacing), cx, cy,
6492 infoPtr->iconSize.cx, infoPtr->iconSize.cy,
6493 infoPtr->ntmHeight);
6495 /* these depend on the iconSpacing */
6496 infoPtr->nItemWidth = LISTVIEW_CalculateMaxWidth(infoPtr);
6497 infoPtr->nItemHeight = LISTVIEW_CalculateMaxHeight(infoPtr);
6499 return oldspacing;
6502 inline void update_icon_size(HIMAGELIST himl, BOOL small, SIZE *size)
6504 INT cx, cy;
6506 if (himl && ImageList_GetIconSize(himl, &cx, &cy))
6508 size->cx = cx;
6509 size->cy = cy;
6511 else
6513 size->cx = GetSystemMetrics(small ? SM_CXSMICON : SM_CXICON);
6514 size->cy = GetSystemMetrics(small ? SM_CYSMICON : SM_CYICON);
6518 /***
6519 * DESCRIPTION:
6520 * Sets image lists.
6522 * PARAMETER(S):
6523 * [I] infoPtr : valid pointer to the listview structure
6524 * [I] INT : image list type
6525 * [I] HIMAGELIST : image list handle
6527 * RETURN:
6528 * SUCCESS : old image list
6529 * FAILURE : NULL
6531 static HIMAGELIST LISTVIEW_SetImageList(LISTVIEW_INFO *infoPtr, INT nType, HIMAGELIST himl)
6533 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
6534 INT oldHeight = infoPtr->nItemHeight;
6535 HIMAGELIST himlOld = 0;
6537 switch (nType)
6539 case LVSIL_NORMAL:
6540 himlOld = infoPtr->himlNormal;
6541 infoPtr->himlNormal = himl;
6542 if (uView == LVS_ICON) update_icon_size(himl, FALSE, &infoPtr->iconSize);
6543 LISTVIEW_SetIconSpacing(infoPtr, 0);
6544 break;
6546 case LVSIL_SMALL:
6547 himlOld = infoPtr->himlSmall;
6548 infoPtr->himlSmall = himl;
6549 if (uView != LVS_ICON) update_icon_size(himl, TRUE, &infoPtr->iconSize);
6550 break;
6552 case LVSIL_STATE:
6553 himlOld = infoPtr->himlState;
6554 infoPtr->himlState = himl;
6555 update_icon_size(himl, TRUE, &infoPtr->iconStateSize);
6556 ImageList_SetBkColor(infoPtr->himlState, CLR_NONE);
6557 break;
6559 default:
6560 ERR("Unknown icon type=%d\n", nType);
6561 return NULL;
6564 infoPtr->nItemHeight = LISTVIEW_CalculateMaxHeight(infoPtr);
6565 if (infoPtr->nItemHeight != oldHeight)
6566 LISTVIEW_UpdateScroll(infoPtr);
6568 return himlOld;
6571 /***
6572 * DESCRIPTION:
6573 * Preallocates memory (does *not* set the actual count of items !)
6575 * PARAMETER(S):
6576 * [I] infoPtr : valid pointer to the listview structure
6577 * [I] INT : item count (projected number of items to allocate)
6578 * [I] DWORD : update flags
6580 * RETURN:
6581 * SUCCESS : TRUE
6582 * FAILURE : FALSE
6584 static BOOL LISTVIEW_SetItemCount(LISTVIEW_INFO *infoPtr, INT nItems, DWORD dwFlags)
6586 TRACE("(nItems=%d, dwFlags=%lx)\n", nItems, dwFlags);
6588 if (infoPtr->dwStyle & LVS_OWNERDATA)
6590 int precount,topvisible;
6592 TRACE("LVS_OWNERDATA is set!\n");
6593 if (dwFlags & (LVSICF_NOINVALIDATEALL | LVSICF_NOSCROLL))
6594 FIXME("flags %s %s not implemented\n",
6595 (dwFlags & LVSICF_NOINVALIDATEALL) ? "LVSICF_NOINVALIDATEALL"
6596 : "",
6597 (dwFlags & LVSICF_NOSCROLL) ? "LVSICF_NOSCROLL" : "");
6599 LISTVIEW_DeselectAll(infoPtr);
6601 precount = infoPtr->nItemCount;
6602 topvisible = LISTVIEW_GetTopIndex(infoPtr) +
6603 LISTVIEW_GetCountPerColumn(infoPtr) + 1;
6605 infoPtr->nItemCount = nItems;
6606 infoPtr->nItemWidth = max(LISTVIEW_CalculateMaxWidth(infoPtr),
6607 DEFAULT_COLUMN_WIDTH);
6609 LISTVIEW_UpdateSize(infoPtr);
6610 LISTVIEW_UpdateScroll(infoPtr);
6612 if (min(precount,infoPtr->nItemCount) < topvisible)
6613 LISTVIEW_InvalidateList(infoPtr); /* FIXME: optimize */
6615 else
6617 /* According to MSDN for non-LVS_OWNERDATA this is just
6618 * a performance issue. The control allocates its internal
6619 * data structures for the number of items specified. It
6620 * cuts down on the number of memory allocations. Therefore
6621 * we will just issue a WARN here
6623 WARN("for non-ownerdata performance option not implemented.\n");
6626 return TRUE;
6629 /***
6630 * DESCRIPTION:
6631 * Sets the position of an item.
6633 * PARAMETER(S):
6634 * [I] infoPtr : valid pointer to the listview structure
6635 * [I] nItem : item index
6636 * [I] pt : coordinate
6638 * RETURN:
6639 * SUCCESS : TRUE
6640 * FAILURE : FALSE
6642 static BOOL LISTVIEW_SetItemPosition(LISTVIEW_INFO *infoPtr, INT nItem, POINT pt)
6644 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
6645 POINT old;
6647 TRACE("(nItem=%d, &pt=%s\n", nItem, debugpoint(&pt));
6649 if (nItem < 0 || nItem >= infoPtr->nItemCount ||
6650 !(uView == LVS_ICON || uView == LVS_SMALLICON)) return FALSE;
6652 /* This point value seems to be an undocumented feature.
6653 * The best guess is that it means either at the origin,
6654 * or at true beginning of the list. I will assume the origin. */
6655 if ((pt.x == -1) && (pt.y == -1))
6656 LISTVIEW_GetOrigin(infoPtr, &pt);
6657 else if (uView == LVS_ICON)
6659 pt.x -= (infoPtr->nItemWidth - infoPtr->iconSize.cx) / 2;
6660 pt.y -= ICON_TOP_PADDING;
6663 /* save the old position */
6664 old.x = (LONG)DPA_GetPtr(infoPtr->hdpaPosX, nItem);
6665 old.y = (LONG)DPA_GetPtr(infoPtr->hdpaPosY, nItem);
6667 /* Is the position changing? */
6668 if (pt.x == old.x && pt.y == old.y) return TRUE;
6670 /* FIXME: shouldn't we invalidate, as the item moved? */
6672 /* Allocating a POINTER for every item is too resource intensive,
6673 * so we'll keep the (x,y) in different arrays */
6674 if (DPA_SetPtr(infoPtr->hdpaPosX, nItem, (void *)pt.x) &&
6675 DPA_SetPtr(infoPtr->hdpaPosY, nItem, (void *)pt.y) )
6676 return TRUE;
6678 ERR("We should never fail here (nItem=%d, pt=%s), please report.\n",
6679 nItem, debugpoint(&pt));
6680 return FALSE;
6683 /***
6684 * DESCRIPTION:
6685 * Sets the state of one or many items.
6687 * PARAMETER(S):
6688 * [I] infoPtr : valid pointer to the listview structure
6689 * [I]INT : item index
6690 * [I] LPLVITEM : item or subitem info
6692 * RETURN:
6693 * SUCCESS : TRUE
6694 * FAILURE : FALSE
6696 static LRESULT LISTVIEW_SetItemState(LISTVIEW_INFO *infoPtr, INT nItem, LPLVITEMW lpLVItem)
6698 BOOL bResult = TRUE;
6699 LVITEMW lvItem;
6701 lvItem.iItem = nItem;
6702 lvItem.iSubItem = 0;
6703 lvItem.mask = LVIF_STATE;
6704 lvItem.state = lpLVItem->state;
6705 lvItem.stateMask = lpLVItem->stateMask;
6706 TRACE("lvItem=%s\n", debuglvitem_t(&lvItem, TRUE));
6708 if (nItem == -1)
6710 /* apply to all items */
6711 for (lvItem.iItem = 0; lvItem.iItem < infoPtr->nItemCount; lvItem.iItem++)
6712 if (!LISTVIEW_SetItemT(infoPtr, &lvItem, TRUE)) bResult = FALSE;
6714 else
6715 bResult = LISTVIEW_SetItemT(infoPtr, &lvItem, TRUE);
6717 return bResult;
6720 /***
6721 * DESCRIPTION:
6722 * Sets the text of an item or subitem.
6724 * PARAMETER(S):
6725 * [I] hwnd : window handle
6726 * [I] nItem : item index
6727 * [I] lpLVItem : item or subitem info
6728 * [I] isW : TRUE if input is Unicode
6730 * RETURN:
6731 * SUCCESS : TRUE
6732 * FAILURE : FALSE
6734 static BOOL LISTVIEW_SetItemTextT(LISTVIEW_INFO *infoPtr, INT nItem, LPLVITEMW lpLVItem, BOOL isW)
6736 LVITEMW lvItem;
6738 if (nItem < 0 && nItem >= infoPtr->nItemCount) return FALSE;
6740 lvItem.iItem = nItem;
6741 lvItem.iSubItem = lpLVItem->iSubItem;
6742 lvItem.mask = LVIF_TEXT;
6743 lvItem.pszText = lpLVItem->pszText;
6744 lvItem.cchTextMax = lpLVItem->cchTextMax;
6746 TRACE("(nItem=%d, lpLVItem=%s, isW=%d)\n", nItem, debuglvitem_t(&lvItem, isW), isW);
6748 return LISTVIEW_SetItemT(infoPtr, &lvItem, isW);
6751 /***
6752 * DESCRIPTION:
6753 * Set item index that marks the start of a multiple selection.
6755 * PARAMETER(S):
6756 * [I] infoPtr : valid pointer to the listview structure
6757 * [I] INT : index
6759 * RETURN:
6760 * Index number or -1 if there is no selection mark.
6762 static LRESULT LISTVIEW_SetSelectionMark(LISTVIEW_INFO *infoPtr, INT nIndex)
6764 INT nOldIndex = infoPtr->nSelectionMark;
6766 TRACE("(nIndex=%d)\n", nIndex);
6768 infoPtr->nSelectionMark = nIndex;
6770 return nOldIndex;
6773 /***
6774 * DESCRIPTION:
6775 * Sets the text background color.
6777 * PARAMETER(S):
6778 * [I] infoPtr : valid pointer to the listview structure
6779 * [I] COLORREF : text background color
6781 * RETURN:
6782 * SUCCESS : TRUE
6783 * FAILURE : FALSE
6785 static LRESULT LISTVIEW_SetTextBkColor(LISTVIEW_INFO *infoPtr, COLORREF clrTextBk)
6787 TRACE("(clrTextBk=%lx)\n", clrTextBk);
6789 if (infoPtr->clrTextBk != clrTextBk)
6791 infoPtr->clrTextBk = clrTextBk;
6792 LISTVIEW_InvalidateList(infoPtr);
6795 return TRUE;
6798 /***
6799 * DESCRIPTION:
6800 * Sets the text foreground color.
6802 * PARAMETER(S):
6803 * [I] infoPtr : valid pointer to the listview structure
6804 * [I] COLORREF : text color
6806 * RETURN:
6807 * SUCCESS : TRUE
6808 * FAILURE : FALSE
6810 static LRESULT LISTVIEW_SetTextColor (LISTVIEW_INFO *infoPtr, COLORREF clrText)
6812 TRACE("(clrText=%lx)\n", clrText);
6814 if (infoPtr->clrText != clrText)
6816 infoPtr->clrText = clrText;
6817 LISTVIEW_InvalidateList(infoPtr);
6820 return TRUE;
6823 /* LISTVIEW_SetToolTips */
6824 /* LISTVIEW_SetUnicodeFormat */
6825 /* LISTVIEW_SetWorkAreas */
6827 /***
6828 * DESCRIPTION:
6829 * Callback internally used by LISTVIEW_SortItems()
6831 * PARAMETER(S):
6832 * [I] LPVOID : first LISTVIEW_ITEM to compare
6833 * [I] LPVOID : second LISTVIEW_ITEM to compare
6834 * [I] LPARAM : HWND of control
6836 * RETURN:
6837 * if first comes before second : negative
6838 * if first comes after second : positive
6839 * if first and second are equivalent : zero
6841 static INT WINAPI LISTVIEW_CallBackCompare(LPVOID first, LPVOID second, LPARAM lParam)
6843 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)GetWindowLongW((HWND)lParam, 0);
6844 LISTVIEW_ITEM* lv_first = (LISTVIEW_ITEM*) DPA_GetPtr( (HDPA)first, 0 );
6845 LISTVIEW_ITEM* lv_second = (LISTVIEW_ITEM*) DPA_GetPtr( (HDPA)second, 0 );
6847 /* Forward the call to the client defined callback */
6848 return (infoPtr->pfnCompare)( lv_first->lParam , lv_second->lParam, infoPtr->lParamSort );
6851 /***
6852 * DESCRIPTION:
6853 * Sorts the listview items.
6855 * PARAMETER(S):
6856 * [I] infoPtr : valid pointer to the listview structure
6857 * [I] WPARAM : application-defined value
6858 * [I] LPARAM : pointer to comparision callback
6860 * RETURN:
6861 * SUCCESS : TRUE
6862 * FAILURE : FALSE
6864 static LRESULT LISTVIEW_SortItems(LISTVIEW_INFO *infoPtr, PFNLVCOMPARE pfnCompare, LPARAM lParamSort)
6866 UINT lStyle = infoPtr->dwStyle;
6867 HDPA hdpaSubItems;
6868 LISTVIEW_ITEM *lpItem;
6869 LPVOID selectionMarkItem;
6870 LVITEMW item;
6871 int i;
6873 TRACE("(pfnCompare=%p, lParamSort=%lx)\n", pfnCompare, lParamSort);
6875 if (lStyle & LVS_OWNERDATA) return FALSE;
6877 if (!infoPtr->hdpaItems) return FALSE;
6879 /* if there are 0 or 1 items, there is no need to sort */
6880 if (infoPtr->nItemCount < 2) return TRUE;
6882 if (infoPtr->nFocusedItem >= 0)
6884 hdpaSubItems = (HDPA)DPA_GetPtr(infoPtr->hdpaItems, infoPtr->nFocusedItem);
6885 lpItem = (LISTVIEW_ITEM *)DPA_GetPtr(hdpaSubItems, 0);
6886 if (lpItem) lpItem->state |= LVIS_FOCUSED;
6888 /* FIXME: go thorugh selected items and mark them so in lpItem->state */
6889 /* clear the lpItem->state for non-selected ones */
6890 /* remove the selection ranges */
6892 infoPtr->pfnCompare = pfnCompare;
6893 infoPtr->lParamSort = lParamSort;
6894 DPA_Sort(infoPtr->hdpaItems, LISTVIEW_CallBackCompare, (LPARAM)infoPtr->hwndSelf);
6896 /* Adjust selections and indices so that they are the way they should
6897 * be after the sort (otherwise, the list items move around, but
6898 * whatever is at the item's previous original position will be
6899 * selected instead)
6901 selectionMarkItem=(infoPtr->nSelectionMark>=0)?DPA_GetPtr(infoPtr->hdpaItems, infoPtr->nSelectionMark):NULL;
6902 for (i=0; i < infoPtr->nItemCount; i++)
6904 hdpaSubItems = (HDPA)DPA_GetPtr(infoPtr->hdpaItems, i);
6905 lpItem = (LISTVIEW_ITEM *)DPA_GetPtr(hdpaSubItems, 0);
6907 if (lpItem->state & LVIS_SELECTED)
6909 item.state = LVIS_SELECTED;
6910 item.stateMask = LVIS_SELECTED;
6911 LISTVIEW_SetItemState(infoPtr, i, &item);
6913 if (lpItem->state & LVIS_FOCUSED)
6915 infoPtr->nFocusedItem = i;
6916 lpItem->state &= ~LVIS_FOCUSED;
6919 if (selectionMarkItem != NULL)
6920 infoPtr->nSelectionMark = DPA_GetPtrIndex(infoPtr->hdpaItems, selectionMarkItem);
6921 /* I believe nHotItem should be left alone, see LISTVIEW_ShiftIndices */
6923 /* align the items */
6924 LISTVIEW_AlignTop(infoPtr);
6926 /* refresh the display */
6927 LISTVIEW_InvalidateList(infoPtr); /* FIXME: display should not change for [SMALL]ICON view */
6929 return TRUE;
6932 /***
6933 * DESCRIPTION:
6934 * Updates an items or rearranges the listview control.
6936 * PARAMETER(S):
6937 * [I] infoPtr : valid pointer to the listview structure
6938 * [I] INT : item index
6940 * RETURN:
6941 * SUCCESS : TRUE
6942 * FAILURE : FALSE
6944 static BOOL LISTVIEW_Update(LISTVIEW_INFO *infoPtr, INT nItem)
6946 LONG lStyle = infoPtr->dwStyle;
6947 UINT uView = lStyle & LVS_TYPEMASK;
6949 TRACE("(nItem=%d)\n", nItem);
6951 if (nItem < 0 && nItem >= infoPtr->nItemCount) return FALSE;
6953 /* rearrange with default alignment style */
6954 if ((lStyle & LVS_AUTOARRANGE) && ((uView == LVS_ICON) ||(uView == LVS_SMALLICON)))
6955 LISTVIEW_Arrange(infoPtr, 0);
6956 else
6957 LISTVIEW_InvalidateItem(infoPtr, nItem);
6959 return TRUE;
6963 /***
6964 * DESCRIPTION:
6965 * Creates the listview control.
6967 * PARAMETER(S):
6968 * [I] hwnd : window handle
6969 * [I] lpcs : the create parameters
6971 * RETURN:
6972 * Success: 0
6973 * Failure: -1
6975 static LRESULT LISTVIEW_Create(HWND hwnd, LPCREATESTRUCTW lpcs)
6977 LISTVIEW_INFO *infoPtr;
6978 UINT uView = lpcs->style & LVS_TYPEMASK;
6979 LOGFONTW logFont;
6981 TRACE("(lpcs=%p)\n", lpcs);
6983 /* initialize info pointer */
6984 infoPtr = (LISTVIEW_INFO *)COMCTL32_Alloc(sizeof(LISTVIEW_INFO));
6985 if (!infoPtr) return -1;
6987 SetWindowLongW(hwnd, 0, (LONG)infoPtr);
6989 infoPtr->hwndSelf = hwnd;
6990 infoPtr->dwStyle = lpcs->style;
6991 /* determine the type of structures to use */
6992 infoPtr->notifyFormat = SendMessageW(GetParent(infoPtr->hwndSelf), WM_NOTIFYFORMAT,
6993 (WPARAM)infoPtr->hwndSelf, (LPARAM)NF_QUERY);
6995 /* initialize color information */
6996 infoPtr->clrBk = CLR_NONE;
6997 infoPtr->clrText = comctl32_color.clrWindowText;
6998 infoPtr->clrTextBk = CLR_DEFAULT;
6999 LISTVIEW_SetBkColor(infoPtr, comctl32_color.clrWindow);
7001 /* set default values */
7002 infoPtr->nFocusedItem = -1;
7003 infoPtr->nSelectionMark = -1;
7004 infoPtr->nHotItem = -1;
7005 infoPtr->bRedraw = TRUE;
7006 infoPtr->iconSpacing.cx = GetSystemMetrics(SM_CXICONSPACING);
7007 infoPtr->iconSpacing.cy = GetSystemMetrics(SM_CYICONSPACING);
7008 infoPtr->nEditLabelItem = -1;
7010 /* get default font (icon title) */
7011 SystemParametersInfoW(SPI_GETICONTITLELOGFONT, 0, &logFont, 0);
7012 infoPtr->hDefaultFont = CreateFontIndirectW(&logFont);
7013 infoPtr->hFont = infoPtr->hDefaultFont;
7014 LISTVIEW_SaveTextMetrics(infoPtr);
7016 /* create header */
7017 infoPtr->hwndHeader = CreateWindowW(WC_HEADERW, (LPCWSTR)NULL,
7018 WS_CHILD | HDS_HORZ | (DWORD)((LVS_NOSORTHEADER & lpcs->style)?0:HDS_BUTTONS),
7019 0, 0, 0, 0, hwnd, (HMENU)0,
7020 lpcs->hInstance, NULL);
7022 /* set header unicode format */
7023 SendMessageW(infoPtr->hwndHeader, HDM_SETUNICODEFORMAT, (WPARAM)TRUE, (LPARAM)NULL);
7025 /* set header font */
7026 SendMessageW(infoPtr->hwndHeader, WM_SETFONT, (WPARAM)infoPtr->hFont, (LPARAM)TRUE);
7028 infoPtr->hdpaColumns = DPA_Create(10);
7030 if (uView == LVS_ICON)
7032 infoPtr->iconSize.cx = GetSystemMetrics(SM_CXICON);
7033 infoPtr->iconSize.cy = GetSystemMetrics(SM_CYICON);
7035 else if (uView == LVS_REPORT)
7037 if (!(LVS_NOCOLUMNHEADER & lpcs->style))
7039 ShowWindow(infoPtr->hwndHeader, SW_SHOWNORMAL);
7041 else
7043 /* set HDS_HIDDEN flag to hide the header bar */
7044 SetWindowLongW(infoPtr->hwndHeader, GWL_STYLE,
7045 GetWindowLongW(infoPtr->hwndHeader, GWL_STYLE) | HDS_HIDDEN);
7049 infoPtr->iconSize.cx = GetSystemMetrics(SM_CXSMICON);
7050 infoPtr->iconSize.cy = GetSystemMetrics(SM_CYSMICON);
7052 else
7054 infoPtr->iconSize.cx = GetSystemMetrics(SM_CXSMICON);
7055 infoPtr->iconSize.cy = GetSystemMetrics(SM_CYSMICON);
7058 infoPtr->iconStateSize.cx = GetSystemMetrics(SM_CXSMICON);
7059 infoPtr->iconStateSize.cy = GetSystemMetrics(SM_CYSMICON);
7061 /* display unsupported listview window styles */
7062 LISTVIEW_UnsupportedStyles(lpcs->style);
7064 /* allocate memory for the data structure */
7065 infoPtr->hdpaItems = DPA_Create(10);
7066 infoPtr->hdpaPosX = DPA_Create(10);
7067 infoPtr->hdpaPosY = DPA_Create(10);
7069 /* allocate memory for the selection ranges */
7070 infoPtr->selectionRanges = ranges_create(10);
7072 /* initialize size of items */
7073 infoPtr->nItemWidth = LISTVIEW_CalculateMaxWidth(infoPtr);
7074 infoPtr->nItemHeight = LISTVIEW_CalculateMaxHeight(infoPtr);
7076 /* initialize the hover time to -1(indicating the default system hover time) */
7077 infoPtr->dwHoverTime = -1;
7079 return 0;
7082 /***
7083 * DESCRIPTION:
7084 * Erases the background of the listview control.
7086 * PARAMETER(S):
7087 * [I] infoPtr : valid pointer to the listview structure
7088 * [I] hdc : device context handle
7090 * RETURN:
7091 * SUCCESS : TRUE
7092 * FAILURE : FALSE
7094 static inline BOOL LISTVIEW_EraseBkgnd(LISTVIEW_INFO *infoPtr, HDC hdc)
7096 RECT rc;
7098 TRACE("(hdc=%x)\n", hdc);
7100 if (!GetClipBox(hdc, &rc)) return FALSE;
7102 return LISTVIEW_FillBkgnd(infoPtr, hdc, &rc);
7106 /***
7107 * DESCRIPTION:
7108 * Helper function for LISTVIEW_[HV]Scroll *only*.
7109 * Performs vertical/horizontal scrolling by a give amount.
7111 * PARAMETER(S):
7112 * [I] infoPtr : valid pointer to the listview structure
7113 * [I] dx : amount of horizontal scroll
7114 * [I] dy : amount of vertical scroll
7116 static void scroll_list(LISTVIEW_INFO *infoPtr, INT dx, INT dy)
7118 /* now we can scroll the list */
7119 ScrollWindowEx(infoPtr->hwndSelf, dx, dy, &infoPtr->rcList,
7120 &infoPtr->rcList, 0, 0, SW_ERASE | SW_INVALIDATE);
7121 /* if we have focus, adjust rect */
7122 OffsetRect(&infoPtr->rcFocus, dx, dy);
7123 UpdateWindow(infoPtr->hwndSelf);
7126 /***
7127 * DESCRIPTION:
7128 * Performs vertical scrolling.
7130 * PARAMETER(S):
7131 * [I] infoPtr : valid pointer to the listview structure
7132 * [I] nScrollCode : scroll code
7133 * [I] nScrollDiff : units to scroll in SB_INTERNAL mode, 0 otherwise
7134 * [I] hScrollWnd : scrollbar control window handle
7136 * RETURN:
7137 * Zero
7139 * NOTES:
7140 * SB_LINEUP/SB_LINEDOWN:
7141 * for LVS_ICON, LVS_SMALLICON is 37 by experiment
7142 * for LVS_REPORT is 1 line
7143 * for LVS_LIST cannot occur
7146 static LRESULT LISTVIEW_VScroll(LISTVIEW_INFO *infoPtr, INT nScrollCode,
7147 INT nScrollDiff, HWND hScrollWnd)
7149 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
7150 INT nOldScrollPos, nNewScrollPos;
7151 SCROLLINFO scrollInfo;
7152 BOOL is_an_icon;
7154 TRACE("(nScrollCode=%d, nScrollDiff=%d)\n", nScrollCode, nScrollDiff);
7156 if (infoPtr->hwndEdit) SendMessageW(infoPtr->hwndEdit, WM_KILLFOCUS, 0, 0);
7158 scrollInfo.cbSize = sizeof(SCROLLINFO);
7159 scrollInfo.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_TRACKPOS;
7161 is_an_icon = ((uView == LVS_ICON) || (uView == LVS_SMALLICON));
7163 if (!GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo)) return 1;
7165 nOldScrollPos = scrollInfo.nPos;
7166 switch (nScrollCode)
7168 case SB_INTERNAL:
7169 break;
7171 case SB_LINEUP:
7172 nScrollDiff = (is_an_icon) ? -LISTVIEW_SCROLL_ICON_LINE_SIZE : -1;
7173 break;
7175 case SB_LINEDOWN:
7176 nScrollDiff = (is_an_icon) ? LISTVIEW_SCROLL_ICON_LINE_SIZE : 1;
7177 break;
7179 case SB_PAGEUP:
7180 nScrollDiff = -scrollInfo.nPage;
7181 break;
7183 case SB_PAGEDOWN:
7184 nScrollDiff = scrollInfo.nPage;
7185 break;
7187 case SB_THUMBPOSITION:
7188 case SB_THUMBTRACK:
7189 nScrollDiff = scrollInfo.nTrackPos - scrollInfo.nPos;
7190 break;
7192 default:
7193 nScrollDiff = 0;
7196 /* quit right away if pos isn't changing */
7197 if (nScrollDiff == 0) return 0;
7199 /* calculate new position, and handle overflows */
7200 nNewScrollPos = scrollInfo.nPos + nScrollDiff;
7201 if (nScrollDiff > 0) {
7202 if (nNewScrollPos < nOldScrollPos ||
7203 nNewScrollPos > scrollInfo.nMax)
7204 nNewScrollPos = scrollInfo.nMax;
7205 } else {
7206 if (nNewScrollPos > nOldScrollPos ||
7207 nNewScrollPos < scrollInfo.nMin)
7208 nNewScrollPos = scrollInfo.nMin;
7211 /* set the new position, and reread in case it changed */
7212 scrollInfo.fMask = SIF_POS;
7213 scrollInfo.nPos = nNewScrollPos;
7214 nNewScrollPos = SetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo, TRUE);
7216 /* carry on only if it really changed */
7217 if (nNewScrollPos == nOldScrollPos) return 0;
7219 /* now adjust to client coordinates */
7220 nScrollDiff = nOldScrollPos - nNewScrollPos;
7221 if (uView == LVS_REPORT) nScrollDiff *= infoPtr->nItemHeight;
7223 /* and scroll the window */
7224 scroll_list(infoPtr, 0, nScrollDiff);
7226 return 0;
7229 /***
7230 * DESCRIPTION:
7231 * Performs horizontal scrolling.
7233 * PARAMETER(S):
7234 * [I] infoPtr : valid pointer to the listview structure
7235 * [I] nScrollCode : scroll code
7236 * [I] nScrollDiff : units to scroll in SB_INTERNAL mode, 0 otherwise
7237 * [I] hScrollWnd : scrollbar control window handle
7239 * RETURN:
7240 * Zero
7242 * NOTES:
7243 * SB_LINELEFT/SB_LINERIGHT:
7244 * for LVS_ICON, LVS_SMALLICON 1 pixel
7245 * for LVS_REPORT is 1 pixel
7246 * for LVS_LIST is 1 column --> which is a 1 because the
7247 * scroll is based on columns not pixels
7250 static LRESULT LISTVIEW_HScroll(LISTVIEW_INFO *infoPtr, INT nScrollCode,
7251 INT nScrollDiff, HWND hScrollWnd)
7253 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
7254 INT nOldScrollPos, nNewScrollPos;
7255 SCROLLINFO scrollInfo;
7257 TRACE("(nScrollCode=%d, nScrollDiff=%d)\n", nScrollCode, nScrollDiff);
7259 if (infoPtr->hwndEdit) SendMessageW(infoPtr->hwndEdit, WM_KILLFOCUS, 0, 0);
7261 scrollInfo.cbSize = sizeof(SCROLLINFO);
7262 scrollInfo.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_TRACKPOS;
7264 if (!GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo)) return 1;
7266 nOldScrollPos = scrollInfo.nPos;
7268 switch (nScrollCode)
7270 case SB_INTERNAL:
7271 break;
7273 case SB_LINELEFT:
7274 nScrollDiff = -1;
7275 break;
7277 case SB_LINERIGHT:
7278 nScrollDiff = 1;
7279 break;
7281 case SB_PAGELEFT:
7282 nScrollDiff = -scrollInfo.nPage;
7283 break;
7285 case SB_PAGERIGHT:
7286 nScrollDiff = scrollInfo.nPage;
7287 break;
7289 case SB_THUMBPOSITION:
7290 case SB_THUMBTRACK:
7291 nScrollDiff = scrollInfo.nTrackPos - scrollInfo.nPos;
7292 break;
7294 default:
7295 nScrollDiff = 0;
7298 /* quit right away if pos isn't changing */
7299 if (nScrollDiff == 0) return 0;
7301 /* calculate new position, and handle overflows */
7302 nNewScrollPos = scrollInfo.nPos + nScrollDiff;
7303 if (nScrollDiff > 0) {
7304 if (nNewScrollPos < nOldScrollPos ||
7305 nNewScrollPos > scrollInfo.nMax)
7306 nNewScrollPos = scrollInfo.nMax;
7307 } else {
7308 if (nNewScrollPos > nOldScrollPos ||
7309 nNewScrollPos < scrollInfo.nMin)
7310 nNewScrollPos = scrollInfo.nMin;
7313 /* set the new position, and reread in case it changed */
7314 scrollInfo.fMask = SIF_POS;
7315 scrollInfo.nPos = nNewScrollPos;
7316 nNewScrollPos = SetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo, TRUE);
7318 /* carry on only if it really changed */
7319 if (nNewScrollPos == nOldScrollPos) return 0;
7321 if(uView == LVS_REPORT)
7322 LISTVIEW_UpdateHeaderSize(infoPtr, nNewScrollPos);
7324 /* now adjust to client coordinates */
7325 nScrollDiff = nOldScrollPos - nNewScrollPos;
7326 if (uView == LVS_LIST) nScrollDiff *= infoPtr->nItemWidth;
7328 /* and scroll the window */
7329 scroll_list(infoPtr, nScrollDiff, 0);
7331 return 0;
7334 static LRESULT LISTVIEW_MouseWheel(LISTVIEW_INFO *infoPtr, INT wheelDelta)
7336 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
7337 INT gcWheelDelta = 0;
7338 UINT pulScrollLines = 3;
7339 SCROLLINFO scrollInfo;
7341 TRACE("(wheelDelta=%d)\n", wheelDelta);
7343 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES,0, &pulScrollLines, 0);
7344 gcWheelDelta -= wheelDelta;
7346 scrollInfo.cbSize = sizeof(SCROLLINFO);
7347 scrollInfo.fMask = SIF_POS;
7349 switch(uView)
7351 case LVS_ICON:
7352 case LVS_SMALLICON:
7354 * listview should be scrolled by a multiple of 37 dependently on its dimension or its visible item number
7355 * should be fixed in the future.
7357 if (GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo))
7358 LISTVIEW_VScroll(infoPtr, SB_THUMBPOSITION,
7359 scrollInfo.nPos + (gcWheelDelta < 0) ?
7360 LISTVIEW_SCROLL_ICON_LINE_SIZE :
7361 -LISTVIEW_SCROLL_ICON_LINE_SIZE, 0);
7362 break;
7364 case LVS_REPORT:
7365 if (abs(gcWheelDelta) >= WHEEL_DELTA && pulScrollLines)
7367 if (GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo))
7369 int cLineScroll = min(LISTVIEW_GetCountPerColumn(infoPtr), pulScrollLines);
7370 cLineScroll *= (gcWheelDelta / WHEEL_DELTA);
7371 LISTVIEW_VScroll(infoPtr, SB_THUMBPOSITION, scrollInfo.nPos + cLineScroll, 0);
7374 break;
7376 case LVS_LIST:
7377 LISTVIEW_HScroll(infoPtr, (gcWheelDelta < 0) ? SB_LINELEFT : SB_LINERIGHT, 0, 0);
7378 break;
7380 return 0;
7383 /***
7384 * DESCRIPTION:
7385 * ???
7387 * PARAMETER(S):
7388 * [I] infoPtr : valid pointer to the listview structure
7389 * [I] INT : virtual key
7390 * [I] LONG : key data
7392 * RETURN:
7393 * Zero
7395 static LRESULT LISTVIEW_KeyDown(LISTVIEW_INFO *infoPtr, INT nVirtualKey, LONG lKeyData)
7397 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
7398 INT nItem = -1;
7399 NMLVKEYDOWN nmKeyDown;
7401 TRACE("(nVirtualKey=%d, lKeyData=%ld)\n", nVirtualKey, lKeyData);
7403 /* send LVN_KEYDOWN notification */
7404 nmKeyDown.wVKey = nVirtualKey;
7405 nmKeyDown.flags = 0;
7406 notify_hdr(infoPtr, LVN_KEYDOWN, &nmKeyDown.hdr);
7408 switch (nVirtualKey)
7410 case VK_RETURN:
7411 if ((infoPtr->nItemCount > 0) && (infoPtr->nFocusedItem != -1))
7413 notify(infoPtr, NM_RETURN);
7414 notify(infoPtr, LVN_ITEMACTIVATE);
7416 break;
7418 case VK_HOME:
7419 if (infoPtr->nItemCount > 0)
7420 nItem = 0;
7421 break;
7423 case VK_END:
7424 if (infoPtr->nItemCount > 0)
7425 nItem = infoPtr->nItemCount - 1;
7426 break;
7428 case VK_LEFT:
7429 nItem = ListView_GetNextItem(infoPtr->hwndSelf, infoPtr->nFocusedItem, LVNI_TOLEFT);
7430 break;
7432 case VK_UP:
7433 nItem = ListView_GetNextItem(infoPtr->hwndSelf, infoPtr->nFocusedItem, LVNI_ABOVE);
7434 break;
7436 case VK_RIGHT:
7437 nItem = ListView_GetNextItem(infoPtr->hwndSelf, infoPtr->nFocusedItem, LVNI_TORIGHT);
7438 break;
7440 case VK_DOWN:
7441 nItem = ListView_GetNextItem(infoPtr->hwndSelf, infoPtr->nFocusedItem, LVNI_BELOW);
7442 break;
7444 case VK_PRIOR:
7445 if (uView == LVS_REPORT)
7446 nItem = infoPtr->nFocusedItem - LISTVIEW_GetCountPerColumn(infoPtr);
7447 else
7448 nItem = infoPtr->nFocusedItem - LISTVIEW_GetCountPerColumn(infoPtr)
7449 * LISTVIEW_GetCountPerRow(infoPtr);
7450 if(nItem < 0) nItem = 0;
7451 break;
7453 case VK_NEXT:
7454 if (uView == LVS_REPORT)
7455 nItem = infoPtr->nFocusedItem + LISTVIEW_GetCountPerColumn(infoPtr);
7456 else
7457 nItem = infoPtr->nFocusedItem + LISTVIEW_GetCountPerColumn(infoPtr)
7458 * LISTVIEW_GetCountPerRow(infoPtr);
7459 if(nItem >= infoPtr->nItemCount) nItem = infoPtr->nItemCount - 1;
7460 break;
7463 if ((nItem != -1) && (nItem != infoPtr->nFocusedItem))
7464 LISTVIEW_KeySelection(infoPtr, nItem);
7466 return 0;
7469 /***
7470 * DESCRIPTION:
7471 * Kills the focus.
7473 * PARAMETER(S):
7474 * [I] infoPtr : valid pointer to the listview structure
7476 * RETURN:
7477 * Zero
7479 static LRESULT LISTVIEW_KillFocus(LISTVIEW_INFO *infoPtr)
7481 TRACE("()\n");
7483 /* if we did not have the focus, there's nothing to do */
7484 if (!infoPtr->bFocus) return 0;
7486 /* send NM_KILLFOCUS notification */
7487 notify(infoPtr, NM_KILLFOCUS);
7489 /* if we have a focus rectagle, get rid of it */
7490 LISTVIEW_ShowFocusRect(infoPtr, FALSE);
7492 /* set window focus flag */
7493 infoPtr->bFocus = FALSE;
7495 /* invalidate the selected items before reseting focus flag */
7496 LISTVIEW_InvalidateSelectedItems(infoPtr);
7498 return 0;
7501 /***
7502 * DESCRIPTION:
7503 * Processes double click messages (left mouse button).
7505 * PARAMETER(S):
7506 * [I] infoPtr : valid pointer to the listview structure
7507 * [I] wKey : key flag
7508 * [I] pts : mouse coordinate
7510 * RETURN:
7511 * Zero
7513 static LRESULT LISTVIEW_LButtonDblClk(LISTVIEW_INFO *infoPtr, WORD wKey, POINTS pts)
7515 LVHITTESTINFO htInfo;
7517 TRACE("(key=%hu, X=%hu, Y=%hu)\n", wKey, pts.x, pts.y);
7519 /* send NM_RELEASEDCAPTURE notification */
7520 notify(infoPtr, NM_RELEASEDCAPTURE);
7522 htInfo.pt.x = pts.x;
7523 htInfo.pt.y = pts.y;
7525 /* send NM_DBLCLK notification */
7526 LISTVIEW_HitTest(infoPtr, &htInfo, TRUE, FALSE);
7527 notify_click(infoPtr, NM_DBLCLK, &htInfo);
7529 /* To send the LVN_ITEMACTIVATE, it must be on an Item */
7530 if(htInfo.iItem != -1) notify(infoPtr, LVN_ITEMACTIVATE);
7532 return 0;
7535 /***
7536 * DESCRIPTION:
7537 * Processes mouse down messages (left mouse button).
7539 * PARAMETER(S):
7540 * [I] infoPtr : valid pointer to the listview structure
7541 * [I] wKey : key flag
7542 * [I] pts : mouse coordinate
7544 * RETURN:
7545 * Zero
7547 static LRESULT LISTVIEW_LButtonDown(LISTVIEW_INFO *infoPtr, WORD wKey, POINTS pts)
7549 LVHITTESTINFO lvHitTestInfo;
7550 LONG lStyle = infoPtr->dwStyle;
7551 static BOOL bGroupSelect = TRUE;
7552 POINT pt = { pts.x, pts.y };
7553 INT nItem;
7555 TRACE("(key=%hu, X=%hu, Y=%hu)\n", wKey, pts.x, pts.y);
7557 /* send NM_RELEASEDCAPTURE notification */
7558 notify(infoPtr, NM_RELEASEDCAPTURE);
7560 if (!infoPtr->bFocus) SetFocus(infoPtr->hwndSelf);
7562 /* set left button down flag */
7563 infoPtr->bLButtonDown = TRUE;
7565 lvHitTestInfo.pt.x = pts.x;
7566 lvHitTestInfo.pt.y = pts.y;
7568 nItem = LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, TRUE);
7569 TRACE("at %s, nItem=%d\n", debugpoint(&pt), nItem);
7570 infoPtr->nEditLabelItem = -1;
7571 if ((nItem >= 0) && (nItem < infoPtr->nItemCount))
7573 if (lStyle & LVS_SINGLESEL)
7575 if (LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED))
7576 infoPtr->nEditLabelItem = nItem;
7577 else
7578 LISTVIEW_SetSelection(infoPtr, nItem);
7580 else
7582 if ((wKey & MK_CONTROL) && (wKey & MK_SHIFT))
7584 if (bGroupSelect)
7585 LISTVIEW_AddGroupSelection(infoPtr, nItem);
7586 else
7588 LVITEMW item;
7590 item.state = LVIS_SELECTED | LVIS_FOCUSED;
7591 item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
7593 LISTVIEW_SetItemState(infoPtr,nItem,&item);
7594 infoPtr->nSelectionMark = nItem;
7597 else if (wKey & MK_CONTROL)
7599 LVITEMW item;
7601 bGroupSelect = (LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED) == 0);
7603 item.state = (bGroupSelect ? LVIS_SELECTED : 0) | LVIS_FOCUSED;
7604 item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
7605 LISTVIEW_SetItemState(infoPtr, nItem, &item);
7606 infoPtr->nSelectionMark = nItem;
7608 else if (wKey & MK_SHIFT)
7610 LISTVIEW_SetGroupSelection(infoPtr, nItem);
7612 else
7614 if (LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED))
7615 infoPtr->nEditLabelItem = nItem;
7617 /* set selection (clears other pre-existing selections) */
7618 LISTVIEW_SetSelection(infoPtr, nItem);
7622 else
7624 /* remove all selections */
7625 LISTVIEW_DeselectAll(infoPtr);
7628 return 0;
7631 /***
7632 * DESCRIPTION:
7633 * Processes mouse up messages (left mouse button).
7635 * PARAMETER(S):
7636 * [I] infoPtr : valid pointer to the listview structure
7637 * [I] wKey : key flag
7638 * [I] pts : mouse coordinate
7640 * RETURN:
7641 * Zero
7643 static LRESULT LISTVIEW_LButtonUp(LISTVIEW_INFO *infoPtr, WORD wKey, POINTS pts)
7645 LVHITTESTINFO lvHitTestInfo;
7647 TRACE("(key=%hu, X=%hu, Y=%hu)\n", wKey, pts.x, pts.y);
7649 if (!infoPtr->bLButtonDown) return 0;
7651 lvHitTestInfo.pt.x = pts.x;
7652 lvHitTestInfo.pt.y = pts.y;
7654 /* send NM_CLICK notification */
7655 LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, FALSE);
7656 notify_click(infoPtr, NM_CLICK, &lvHitTestInfo);
7658 /* set left button flag */
7659 infoPtr->bLButtonDown = FALSE;
7661 /* if we clicked on a selected item, edit the label */
7662 if(lvHitTestInfo.iItem == infoPtr->nEditLabelItem && (lvHitTestInfo.flags & LVHT_ONITEMLABEL))
7663 LISTVIEW_EditLabelT(infoPtr, lvHitTestInfo.iItem, TRUE);
7665 return 0;
7668 /***
7669 * DESCRIPTION:
7670 * Destroys the listview control (called after WM_DESTROY).
7672 * PARAMETER(S):
7673 * [I] infoPtr : valid pointer to the listview structure
7675 * RETURN:
7676 * Zero
7678 static LRESULT LISTVIEW_NCDestroy(LISTVIEW_INFO *infoPtr)
7680 LONG lStyle = infoPtr->dwStyle;
7682 TRACE("()\n");
7684 /* delete all items */
7685 LISTVIEW_DeleteAllItems(infoPtr);
7687 /* destroy data structure */
7688 DPA_Destroy(infoPtr->hdpaItems);
7689 ranges_destroy(infoPtr->selectionRanges);
7691 /* destroy image lists */
7692 if (!(lStyle & LVS_SHAREIMAGELISTS))
7694 if (infoPtr->himlNormal)
7695 ImageList_Destroy(infoPtr->himlNormal);
7696 if (infoPtr->himlSmall)
7697 ImageList_Destroy(infoPtr->himlSmall);
7698 if (infoPtr->himlState)
7699 ImageList_Destroy(infoPtr->himlState);
7702 /* destroy font, bkgnd brush */
7703 infoPtr->hFont = 0;
7704 if (infoPtr->hDefaultFont) DeleteObject(infoPtr->hDefaultFont);
7705 if (infoPtr->clrBk != CLR_NONE) DeleteObject(infoPtr->hBkBrush);
7707 /* free listview info pointer*/
7708 COMCTL32_Free(infoPtr);
7710 SetWindowLongW(infoPtr->hwndSelf, 0, 0);
7711 return 0;
7714 /***
7715 * DESCRIPTION:
7716 * Handles notifications from children.
7718 * PARAMETER(S):
7719 * [I] infoPtr : valid pointer to the listview structure
7720 * [I] INT : control identifier
7721 * [I] LPNMHDR : notification information
7723 * RETURN:
7724 * Zero
7726 static LRESULT LISTVIEW_Notify(LISTVIEW_INFO *infoPtr, INT nCtrlId, LPNMHDR lpnmh)
7728 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
7730 TRACE("(nCtrlId=%d, lpnmh=%p)\n", nCtrlId, lpnmh);
7732 /* handle notification from header control */
7733 if (lpnmh->hwndFrom == infoPtr->hwndHeader)
7735 LPNMHEADERW lphnm = (LPNMHEADERW)lpnmh;
7737 if (lpnmh->code == HDN_TRACKW || lpnmh->code == HDN_TRACKA)
7739 COLUMN_INFO *lpColumnInfo;
7740 RECT rcCol;
7741 INT dx;
7743 if (!(lpColumnInfo = DPA_GetPtr(infoPtr->hdpaColumns, lphnm->iItem))) return 0;
7744 if (!(lphnm->pitem->mask & HDI_WIDTH)) return 0;
7746 /* determine how much we change since the last know position */
7747 dx = lphnm->pitem->cxy - (lpColumnInfo->rcHeader.right - lpColumnInfo->rcHeader.left);
7749 /* ajust the column being tracked */
7750 lpColumnInfo->rcHeader.right += dx;
7752 /* compute the rectangle for the tracked column */
7753 rcCol.left = lpColumnInfo->rcHeader.left;
7754 rcCol.top = infoPtr->rcList.top;
7755 rcCol.right = lpColumnInfo->rcHeader.right;
7756 rcCol.bottom = infoPtr->rcList.bottom;
7758 LISTVIEW_ScrollColumns(infoPtr, lphnm->iItem + 1, dx);
7759 if (uView == LVS_REPORT) LISTVIEW_InvalidateRect(infoPtr, &rcCol);
7761 else if(lpnmh->code == HDN_ITEMCLICKW || lpnmh->code == HDN_ITEMCLICKA)
7763 /* Handle sorting by Header Column */
7764 NMLISTVIEW nmlv;
7766 ZeroMemory(&nmlv, sizeof(NMLISTVIEW));
7767 nmlv.iItem = -1;
7768 nmlv.iSubItem = lphnm->iItem;
7769 notify_listview(infoPtr, LVN_COLUMNCLICK, &nmlv);
7773 return 0;
7776 /***
7777 * DESCRIPTION:
7778 * Determines the type of structure to use.
7780 * PARAMETER(S):
7781 * [I] infoPtr : valid pointer to the listview structureof the sender
7782 * [I] hwndFrom : listview window handle
7783 * [I] nCommand : command specifying the nature of the WM_NOTIFYFORMAT
7785 * RETURN:
7786 * Zero
7788 static LRESULT LISTVIEW_NotifyFormat(LISTVIEW_INFO *infoPtr, HWND hwndFrom, INT nCommand)
7790 TRACE("(hwndFrom=%x, nCommand=%d)\n", hwndFrom, nCommand);
7792 if (nCommand == NF_REQUERY)
7793 infoPtr->notifyFormat = SendMessageW(hwndFrom, WM_NOTIFYFORMAT,
7794 (WPARAM)infoPtr->hwndSelf, (LPARAM)NF_QUERY);
7795 return 0;
7798 /***
7799 * DESCRIPTION:
7800 * Paints/Repaints the listview control.
7802 * PARAMETER(S):
7803 * [I] infoPtr : valid pointer to the listview structure
7804 * [I] hdc : device context handle
7806 * RETURN:
7807 * Zero
7809 static LRESULT LISTVIEW_Paint(LISTVIEW_INFO *infoPtr, HDC hdc)
7811 TRACE("(hdc=%x)\n", hdc);
7813 if (hdc)
7814 LISTVIEW_Refresh(infoPtr, hdc);
7815 else
7817 PAINTSTRUCT ps;
7819 hdc = BeginPaint(infoPtr->hwndSelf, &ps);
7820 if (!hdc) return 1;
7821 if (ps.fErase) LISTVIEW_FillBkgnd(infoPtr, hdc, &ps.rcPaint);
7822 LISTVIEW_Refresh(infoPtr, hdc);
7823 EndPaint(infoPtr->hwndSelf, &ps);
7826 return 0;
7829 /***
7830 * DESCRIPTION:
7831 * Processes double click messages (right mouse button).
7833 * PARAMETER(S):
7834 * [I] infoPtr : valid pointer to the listview structure
7835 * [I] wKey : key flag
7836 * [I] pts : mouse coordinate
7838 * RETURN:
7839 * Zero
7841 static LRESULT LISTVIEW_RButtonDblClk(LISTVIEW_INFO *infoPtr, WORD wKey, POINTS pts)
7843 LVHITTESTINFO lvHitTestInfo;
7845 TRACE("(key=%hu,X=%hu,Y=%hu)\n", wKey, pts.x, pts.y);
7847 /* send NM_RELEASEDCAPTURE notification */
7848 notify(infoPtr, NM_RELEASEDCAPTURE);
7850 /* send NM_RDBLCLK notification */
7851 lvHitTestInfo.pt.x = pts.x;
7852 lvHitTestInfo.pt.y = pts.y;
7853 LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, FALSE);
7854 notify_click(infoPtr, NM_RDBLCLK, &lvHitTestInfo);
7856 return 0;
7859 /***
7860 * DESCRIPTION:
7861 * Processes mouse down messages (right mouse button).
7863 * PARAMETER(S):
7864 * [I] infoPtr : valid pointer to the listview structure
7865 * [I] wKey : key flag
7866 * [I] pts : mouse coordinate
7868 * RETURN:
7869 * Zero
7871 static LRESULT LISTVIEW_RButtonDown(LISTVIEW_INFO *infoPtr, WORD wKey, POINTS pts)
7873 LVHITTESTINFO lvHitTestInfo;
7874 INT nItem;
7876 TRACE("(key=%hu,X=%hu,Y=%hu)\n", wKey, pts.x, pts.y);
7878 /* send NM_RELEASEDCAPTURE notification */
7879 notify(infoPtr, NM_RELEASEDCAPTURE);
7881 /* make sure the listview control window has the focus */
7882 if (!infoPtr->bFocus) SetFocus(infoPtr->hwndSelf);
7884 /* set right button down flag */
7885 infoPtr->bRButtonDown = TRUE;
7887 /* determine the index of the selected item */
7888 lvHitTestInfo.pt.x = pts.x;
7889 lvHitTestInfo.pt.y = pts.y;
7890 nItem = LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, TRUE);
7892 if ((nItem >= 0) && (nItem < infoPtr->nItemCount))
7894 LISTVIEW_SetItemFocus(infoPtr, nItem);
7895 if (!((wKey & MK_SHIFT) || (wKey & MK_CONTROL)) &&
7896 !LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED))
7897 LISTVIEW_SetSelection(infoPtr, nItem);
7899 else
7901 LISTVIEW_DeselectAll(infoPtr);
7904 return 0;
7907 /***
7908 * DESCRIPTION:
7909 * Processes mouse up messages (right mouse button).
7911 * PARAMETER(S):
7912 * [I] infoPtr : valid pointer to the listview structure
7913 * [I] wKey : key flag
7914 * [I] pts : mouse coordinate
7916 * RETURN:
7917 * Zero
7919 static LRESULT LISTVIEW_RButtonUp(LISTVIEW_INFO *infoPtr, WORD wKey, POINTS pts)
7921 LVHITTESTINFO lvHitTestInfo;
7922 POINT pt;
7924 TRACE("(key=%hu,X=%hu,Y=%hu)\n", wKey, pts.x, pts.y);
7926 if (!infoPtr->bRButtonDown) return 0;
7928 /* set button flag */
7929 infoPtr->bRButtonDown = FALSE;
7931 /* Send NM_RClICK notification */
7932 lvHitTestInfo.pt.x = pts.x;
7933 lvHitTestInfo.pt.y = pts.y;
7934 LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, FALSE);
7935 notify_click(infoPtr, NM_RCLICK, &lvHitTestInfo);
7937 /* Change to screen coordinate for WM_CONTEXTMENU */
7938 pt = lvHitTestInfo.pt;
7939 ClientToScreen(infoPtr->hwndSelf, &pt);
7941 /* Send a WM_CONTEXTMENU message in response to the RBUTTONUP */
7942 SendMessageW(infoPtr->hwndSelf, WM_CONTEXTMENU,
7943 (WPARAM)infoPtr->hwndSelf, MAKELPARAM(pt.x, pt.y));
7945 return 0;
7949 /***
7950 * DESCRIPTION:
7951 * Sets the cursor.
7953 * PARAMETER(S):
7954 * [I] infoPtr : valid pointer to the listview structure
7955 * [I] hwnd : window handle of window containing the cursor
7956 * [I] nHittest : hit-test code
7957 * [I] wMouseMsg : ideintifier of the mouse message
7959 * RETURN:
7960 * TRUE if cursor is set
7961 * FALSE otherwise
7963 static BOOL LISTVIEW_SetCursor(LISTVIEW_INFO *infoPtr, HWND hwnd, UINT nHittest, UINT wMouseMsg)
7965 LVHITTESTINFO lvHitTestInfo;
7967 if(!(infoPtr->dwLvExStyle & LVS_EX_TRACKSELECT)) return FALSE;
7969 if(!infoPtr->hHotCursor) return FALSE;
7971 GetCursorPos(&lvHitTestInfo.pt);
7972 if (LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, FALSE, FALSE) < 0) return FALSE;
7974 SetCursor(infoPtr->hHotCursor);
7976 return TRUE;
7979 /***
7980 * DESCRIPTION:
7981 * Sets the focus.
7983 * PARAMETER(S):
7984 * [I] infoPtr : valid pointer to the listview structure
7985 * [I] hwndLoseFocus : handle of previously focused window
7987 * RETURN:
7988 * Zero
7990 static LRESULT LISTVIEW_SetFocus(LISTVIEW_INFO *infoPtr, HWND hwndLoseFocus)
7992 TRACE("(hwndLoseFocus=%x)\n", hwndLoseFocus);
7994 /* if we have the focus already, there's nothing to do */
7995 if (infoPtr->bFocus) return 0;
7997 /* send NM_SETFOCUS notification */
7998 notify(infoPtr, NM_SETFOCUS);
8000 /* set window focus flag */
8001 infoPtr->bFocus = TRUE;
8003 /* put the focus rect back on */
8004 LISTVIEW_ShowFocusRect(infoPtr, TRUE);
8006 /* redraw all visible selected items */
8007 LISTVIEW_InvalidateSelectedItems(infoPtr);
8009 return 0;
8012 /***
8013 * DESCRIPTION:
8014 * Sets the font.
8016 * PARAMETER(S):
8017 * [I] infoPtr : valid pointer to the listview structure
8018 * [I] fRedraw : font handle
8019 * [I] fRedraw : redraw flag
8021 * RETURN:
8022 * Zero
8024 static LRESULT LISTVIEW_SetFont(LISTVIEW_INFO *infoPtr, HFONT hFont, WORD fRedraw)
8026 HFONT oldFont = infoPtr->hFont;
8028 TRACE("(hfont=%x,redraw=%hu)\n", hFont, fRedraw);
8030 infoPtr->hFont = hFont ? hFont : infoPtr->hDefaultFont;
8031 if (infoPtr->hFont == oldFont) return 0;
8033 LISTVIEW_SaveTextMetrics(infoPtr);
8035 if ((infoPtr->dwStyle & LVS_TYPEMASK) == LVS_REPORT)
8036 SendMessageW(infoPtr->hwndHeader, WM_SETFONT, (WPARAM)hFont, MAKELPARAM(fRedraw, 0));
8038 if (fRedraw) LISTVIEW_InvalidateList(infoPtr);
8040 return 0;
8043 /***
8044 * DESCRIPTION:
8045 * Message handling for WM_SETREDRAW.
8046 * For the Listview, it invalidates the entire window (the doc specifies otherwise)
8048 * PARAMETER(S):
8049 * [I] infoPtr : valid pointer to the listview structure
8050 * [I] bRedraw: state of redraw flag
8052 * RETURN:
8053 * DefWinProc return value
8055 static LRESULT LISTVIEW_SetRedraw(LISTVIEW_INFO *infoPtr, BOOL bRedraw)
8057 infoPtr->bRedraw = bRedraw;
8058 if(bRedraw)
8059 RedrawWindow(infoPtr->hwndSelf, NULL, 0,
8060 RDW_INVALIDATE | RDW_FRAME | RDW_ERASE | RDW_ALLCHILDREN | RDW_ERASENOW);
8061 return 0;
8064 /***
8065 * DESCRIPTION:
8066 * Resizes the listview control. This function processes WM_SIZE
8067 * messages. At this time, the width and height are not used.
8069 * PARAMETER(S):
8070 * [I] infoPtr : valid pointer to the listview structure
8071 * [I] Width : new width
8072 * [I] Height : new height
8074 * RETURN:
8075 * Zero
8077 static LRESULT LISTVIEW_Size(LISTVIEW_INFO *infoPtr, int Width, int Height)
8079 LONG lStyle = infoPtr->dwStyle;
8080 UINT uView = lStyle & LVS_TYPEMASK;
8082 TRACE("(width=%d, height=%d)\n", Width, Height);
8084 if (LISTVIEW_UpdateSize(infoPtr))
8086 if ((uView == LVS_SMALLICON) || (uView == LVS_ICON))
8088 if (lStyle & LVS_ALIGNLEFT)
8089 LISTVIEW_AlignLeft(infoPtr);
8090 else
8091 LISTVIEW_AlignTop(infoPtr);
8094 LISTVIEW_UpdateScroll(infoPtr);
8096 LISTVIEW_InvalidateList(infoPtr); /* FIXME: optimize */
8099 return 0;
8102 /***
8103 * DESCRIPTION:
8104 * Sets the size information.
8106 * PARAMETER(S):
8107 * [I] infoPtr : valid pointer to the listview structure
8109 * RETURN:
8110 * Zero if no size change
8111 * 1 of size changed
8113 static BOOL LISTVIEW_UpdateSize(LISTVIEW_INFO *infoPtr)
8115 LONG lStyle = infoPtr->dwStyle;
8116 UINT uView = lStyle & LVS_TYPEMASK;
8117 RECT rcList;
8118 RECT rcOld;
8120 GetClientRect(infoPtr->hwndSelf, &rcList);
8121 CopyRect(&rcOld,&(infoPtr->rcList));
8122 infoPtr->rcList.left = 0;
8123 infoPtr->rcList.right = max(rcList.right - rcList.left, 1);
8124 infoPtr->rcList.top = 0;
8125 infoPtr->rcList.bottom = max(rcList.bottom - rcList.top, 1);
8127 if (uView == LVS_LIST)
8129 /* Apparently the "LIST" style is supposed to have the same
8130 * number of items in a column even if there is no scroll bar.
8131 * Since if a scroll bar already exists then the bottom is already
8132 * reduced, only reduce if the scroll bar does not currently exist.
8133 * The "2" is there to mimic the native control. I think it may be
8134 * related to either padding or edges. (GLA 7/2002)
8136 if (!(lStyle & WS_HSCROLL))
8138 INT nHScrollHeight = GetSystemMetrics(SM_CYHSCROLL);
8139 if (infoPtr->rcList.bottom > nHScrollHeight)
8140 infoPtr->rcList.bottom -= (nHScrollHeight + 2);
8142 else
8144 if (infoPtr->rcList.bottom > 2)
8145 infoPtr->rcList.bottom -= 2;
8148 else if (uView == LVS_REPORT)
8150 HDLAYOUT hl;
8151 WINDOWPOS wp;
8153 hl.prc = &rcList;
8154 hl.pwpos = &wp;
8155 Header_Layout(infoPtr->hwndHeader, &hl);
8157 SetWindowPos(wp.hwnd, wp.hwndInsertAfter, wp.x, wp.y, wp.cx, wp.cy, wp.flags);
8159 if (!(LVS_NOCOLUMNHEADER & lStyle))
8160 infoPtr->rcList.top = max(wp.cy, 0);
8162 return (EqualRect(&rcOld,&(infoPtr->rcList)));
8165 /***
8166 * DESCRIPTION:
8167 * Processes WM_STYLECHANGED messages.
8169 * PARAMETER(S):
8170 * [I] infoPtr : valid pointer to the listview structure
8171 * [I] wStyleType : window style type (normal or extended)
8172 * [I] lpss : window style information
8174 * RETURN:
8175 * Zero
8177 static INT LISTVIEW_StyleChanged(LISTVIEW_INFO *infoPtr, WPARAM wStyleType,
8178 LPSTYLESTRUCT lpss)
8180 UINT uNewView = lpss->styleNew & LVS_TYPEMASK;
8181 UINT uOldView = lpss->styleOld & LVS_TYPEMASK;
8183 TRACE("(styletype=%x, styleOld=0x%08lx, styleNew=0x%08lx)\n",
8184 wStyleType, lpss->styleOld, lpss->styleNew);
8186 if (wStyleType != GWL_STYLE) return 0;
8188 /* FIXME: if LVS_NOSORTHEADER changed, update header */
8189 /* what if LVS_OWNERDATA changed? */
8190 /* or LVS_SINGLESEL */
8191 /* or LVS_SORT{AS,DES}CENDING */
8193 infoPtr->dwStyle = lpss->styleNew;
8195 if (((lpss->styleOld & WS_HSCROLL) != 0)&&
8196 ((lpss->styleNew & WS_HSCROLL) == 0))
8197 ShowScrollBar(infoPtr->hwndSelf, SB_HORZ, FALSE);
8199 if (((lpss->styleOld & WS_VSCROLL) != 0)&&
8200 ((lpss->styleNew & WS_VSCROLL) == 0))
8201 ShowScrollBar(infoPtr->hwndSelf, SB_VERT, FALSE);
8203 if (uNewView != uOldView)
8205 SIZE oldIconSize = infoPtr->iconSize;
8206 HIMAGELIST himl;
8208 SendMessageW(infoPtr->hwndEdit, WM_KILLFOCUS, 0, 0);
8209 ShowWindow(infoPtr->hwndHeader, SW_HIDE);
8211 ShowScrollBar(infoPtr->hwndSelf, SB_BOTH, FALSE);
8212 SetRectEmpty(&infoPtr->rcFocus);
8214 himl = (uNewView == LVS_ICON ? infoPtr->himlNormal : infoPtr->himlSmall);
8215 update_icon_size(himl, uNewView != LVS_ICON, &infoPtr->iconSize);
8217 if (uNewView == LVS_ICON)
8219 if ((infoPtr->iconSize.cx != oldIconSize.cx) || (infoPtr->iconSize.cy != oldIconSize.cy))
8221 TRACE("icon old size=(%ld,%ld), new size=(%ld,%ld)\n",
8222 oldIconSize.cx, oldIconSize.cy, infoPtr->iconSize.cx, infoPtr->iconSize.cy);
8223 LISTVIEW_SetIconSpacing(infoPtr, 0);
8226 else if (uNewView == LVS_REPORT)
8228 HDLAYOUT hl;
8229 WINDOWPOS wp;
8231 hl.prc = &infoPtr->rcList;
8232 hl.pwpos = &wp;
8233 Header_Layout(infoPtr->hwndHeader, &hl);
8234 SetWindowPos(infoPtr->hwndHeader, infoPtr->hwndSelf, wp.x, wp.y, wp.cx, wp.cy, wp.flags);
8237 infoPtr->nItemWidth = LISTVIEW_CalculateMaxWidth(infoPtr);
8238 infoPtr->nItemHeight = LISTVIEW_CalculateMaxHeight(infoPtr);
8241 if (uNewView == LVS_REPORT)
8242 ShowWindow(infoPtr->hwndHeader, (LVS_NOCOLUMNHEADER & lpss->styleNew) ? SW_HIDE : SW_SHOWNORMAL);
8244 if ( (uNewView == LVS_ICON || uNewView == LVS_SMALLICON) &&
8245 (uNewView != uOldView || ((lpss->styleNew ^ lpss->styleOld) & LVS_ALIGNMASK)) )
8247 if (infoPtr->dwStyle & LVS_ALIGNLEFT)
8248 LISTVIEW_AlignLeft(infoPtr);
8249 else
8250 LISTVIEW_AlignTop(infoPtr);
8253 /* update the size of the client area */
8254 LISTVIEW_UpdateSize(infoPtr);
8256 /* add scrollbars if needed */
8257 LISTVIEW_UpdateScroll(infoPtr);
8259 /* invalidate client area + erase background */
8260 LISTVIEW_InvalidateList(infoPtr);
8262 /* print the list of unsupported window styles */
8263 LISTVIEW_UnsupportedStyles(lpss->styleNew);
8265 return 0;
8268 /***
8269 * DESCRIPTION:
8270 * Window procedure of the listview control.
8273 static LRESULT WINAPI
8274 LISTVIEW_WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
8276 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)GetWindowLongW(hwnd, 0);
8278 TRACE("(uMsg=%x wParam=%x lParam=%lx)\n", uMsg, wParam, lParam);
8280 if (!infoPtr && (uMsg != WM_CREATE))
8281 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
8283 if (infoPtr)
8285 infoPtr->dwStyle = GetWindowLongW(hwnd, GWL_STYLE);
8288 switch (uMsg)
8290 case LVM_APPROXIMATEVIEWRECT:
8291 return LISTVIEW_ApproximateViewRect(infoPtr, (INT)wParam,
8292 LOWORD(lParam), HIWORD(lParam));
8293 case LVM_ARRANGE:
8294 return LISTVIEW_Arrange(infoPtr, (INT)wParam);
8296 /* case LVN_CANCELEDITLABEL */
8298 /* case LVM_CREATEDRAGIMAGE: */
8300 case LVM_DELETEALLITEMS:
8301 return LISTVIEW_DeleteAllItems(infoPtr);
8303 case LVM_DELETECOLUMN:
8304 return LISTVIEW_DeleteColumn(infoPtr, (INT)wParam);
8306 case LVM_DELETEITEM:
8307 return LISTVIEW_DeleteItem(infoPtr, (INT)wParam);
8309 case LVM_EDITLABELW:
8310 return (LRESULT)LISTVIEW_EditLabelT(infoPtr, (INT)wParam, TRUE);
8312 case LVM_EDITLABELA:
8313 return (LRESULT)LISTVIEW_EditLabelT(infoPtr, (INT)wParam, FALSE);
8315 /* case LVN_ENABLEGROUPVIEW: */
8317 case LVM_ENSUREVISIBLE:
8318 return LISTVIEW_EnsureVisible(infoPtr, (INT)wParam, (BOOL)lParam);
8320 case LVM_FINDITEMW:
8321 return LISTVIEW_FindItemW(infoPtr, (INT)wParam, (LPLVFINDINFOW)lParam);
8323 case LVM_FINDITEMA:
8324 return LISTVIEW_FindItemA(infoPtr, (INT)wParam, (LPLVFINDINFOA)lParam);
8326 case LVM_GETBKCOLOR:
8327 return infoPtr->clrBk;
8329 /* case LVM_GETBKIMAGE: */
8331 case LVM_GETCALLBACKMASK:
8332 return infoPtr->uCallbackMask;
8334 case LVM_GETCOLUMNA:
8335 return LISTVIEW_GetColumnT(infoPtr, (INT)wParam, (LPLVCOLUMNW)lParam, FALSE);
8337 case LVM_GETCOLUMNW:
8338 return LISTVIEW_GetColumnT(infoPtr, (INT)wParam, (LPLVCOLUMNW)lParam, TRUE);
8340 case LVM_GETCOLUMNORDERARRAY:
8341 return LISTVIEW_GetColumnOrderArray(infoPtr, (INT)wParam, (LPINT)lParam);
8343 case LVM_GETCOLUMNWIDTH:
8344 return LISTVIEW_GetColumnWidth(infoPtr, (INT)wParam);
8346 case LVM_GETCOUNTPERPAGE:
8347 return LISTVIEW_GetCountPerPage(infoPtr);
8349 case LVM_GETEDITCONTROL:
8350 return (LRESULT)infoPtr->hwndEdit;
8352 case LVM_GETEXTENDEDLISTVIEWSTYLE:
8353 return infoPtr->dwLvExStyle;
8355 case LVM_GETHEADER:
8356 return (LRESULT)infoPtr->hwndHeader;
8358 case LVM_GETHOTCURSOR:
8359 return (LRESULT)infoPtr->hHotCursor;
8361 case LVM_GETHOTITEM:
8362 return infoPtr->nHotItem;
8364 case LVM_GETHOVERTIME:
8365 return infoPtr->dwHoverTime;
8367 case LVM_GETIMAGELIST:
8368 return LISTVIEW_GetImageList(infoPtr, (INT)wParam);
8370 /* case LVN_GETINSERTMARK: */
8372 /* case LVN_GETINSERTMARKCOLOR: */
8374 /* case LVN_GETINSERTMARKRECT: */
8376 case LVM_GETISEARCHSTRINGA:
8377 case LVM_GETISEARCHSTRINGW:
8378 FIXME("LVM_GETISEARCHSTRING: unimplemented\n");
8379 return FALSE;
8381 case LVM_GETITEMA:
8382 return LISTVIEW_GetItemExtT(infoPtr, (LPLVITEMW)lParam, FALSE);
8384 case LVM_GETITEMW:
8385 return LISTVIEW_GetItemExtT(infoPtr, (LPLVITEMW)lParam, TRUE);
8387 case LVM_GETITEMCOUNT:
8388 return infoPtr->nItemCount;
8390 case LVM_GETITEMPOSITION:
8391 return LISTVIEW_GetItemPosition(infoPtr, (INT)wParam, (LPPOINT)lParam);
8393 case LVM_GETITEMRECT:
8394 return LISTVIEW_GetItemRect(infoPtr, (INT)wParam, (LPRECT)lParam);
8396 case LVM_GETITEMSPACING:
8397 return LISTVIEW_GetItemSpacing(infoPtr, (BOOL)wParam);
8399 case LVM_GETITEMSTATE:
8400 return LISTVIEW_GetItemState(infoPtr, (INT)wParam, (UINT)lParam);
8402 case LVM_GETITEMTEXTA:
8403 return LISTVIEW_GetItemTextT(infoPtr, (INT)wParam, (LPLVITEMW)lParam, FALSE);
8405 case LVM_GETITEMTEXTW:
8406 return LISTVIEW_GetItemTextT(infoPtr, (INT)wParam, (LPLVITEMW)lParam, TRUE);
8408 case LVM_GETNEXTITEM:
8409 return LISTVIEW_GetNextItem(infoPtr, (INT)wParam, LOWORD(lParam));
8411 case LVM_GETNUMBEROFWORKAREAS:
8412 FIXME("LVM_GETNUMBEROFWORKAREAS: unimplemented\n");
8413 return 1;
8415 case LVM_GETORIGIN:
8416 return LISTVIEW_GetOrigin(infoPtr, (LPPOINT)lParam);
8418 /* case LVN_GETOUTLINECOLOR: */
8420 /* case LVM_GETSELECTEDCOLUMN: */
8422 case LVM_GETSELECTEDCOUNT:
8423 return LISTVIEW_GetSelectedCount(infoPtr);
8425 case LVM_GETSELECTIONMARK:
8426 return infoPtr->nSelectionMark;
8428 case LVM_GETSTRINGWIDTHA:
8429 return LISTVIEW_GetStringWidthT(infoPtr, (LPCWSTR)lParam, FALSE);
8431 case LVM_GETSTRINGWIDTHW:
8432 return LISTVIEW_GetStringWidthT(infoPtr, (LPCWSTR)lParam, TRUE);
8434 case LVM_GETSUBITEMRECT:
8435 return LISTVIEW_GetSubItemRect(infoPtr, (UINT)wParam, (LPRECT)lParam);
8437 case LVM_GETTEXTBKCOLOR:
8438 return infoPtr->clrTextBk;
8440 case LVM_GETTEXTCOLOR:
8441 return infoPtr->clrText;
8443 /* case LVN_GETTILEINFO: */
8445 /* case LVN_GETTILEVIEWINFO: */
8447 case LVM_GETTOOLTIPS:
8448 FIXME("LVM_GETTOOLTIPS: unimplemented\n");
8449 return FALSE;
8451 case LVM_GETTOPINDEX:
8452 return LISTVIEW_GetTopIndex(infoPtr);
8454 /*case LVM_GETUNICODEFORMAT:
8455 FIXME("LVM_GETUNICODEFORMAT: unimplemented\n");
8456 return FALSE;*/
8458 case LVM_GETVIEWRECT:
8459 return LISTVIEW_GetViewRect(infoPtr, (LPRECT)lParam);
8461 case LVM_GETWORKAREAS:
8462 FIXME("LVM_GETWORKAREAS: unimplemented\n");
8463 return FALSE;
8465 /* case LVN_HASGROUP: */
8467 case LVM_HITTEST:
8468 return LISTVIEW_HitTest(infoPtr, (LPLVHITTESTINFO)lParam, FALSE, FALSE);
8470 case LVM_INSERTCOLUMNA:
8471 return LISTVIEW_InsertColumnT(infoPtr, (INT)wParam, (LPLVCOLUMNW)lParam, FALSE);
8473 case LVM_INSERTCOLUMNW:
8474 return LISTVIEW_InsertColumnT(infoPtr, (INT)wParam, (LPLVCOLUMNW)lParam, TRUE);
8476 /* case LVN_INSERTGROUP: */
8478 /* case LVN_INSERTGROUPSORTED: */
8480 case LVM_INSERTITEMA:
8481 return LISTVIEW_InsertItemT(infoPtr, (LPLVITEMW)lParam, FALSE);
8483 case LVM_INSERTITEMW:
8484 return LISTVIEW_InsertItemT(infoPtr, (LPLVITEMW)lParam, TRUE);
8486 /* case LVN_INSERTMARKHITTEST: */
8488 /* case LVN_ISGROUPVIEWENABLED: */
8490 /* case LVN_MAPIDTOINDEX: */
8492 /* case LVN_INEDXTOID: */
8494 /* case LVN_MOVEGROUP: */
8496 /* case LVN_MOVEITEMTOGROUP: */
8498 case LVM_REDRAWITEMS:
8499 return LISTVIEW_RedrawItems(infoPtr, (INT)wParam, (INT)lParam);
8501 /* case LVN_REMOVEALLGROUPS: */
8503 /* case LVN_REMOVEGROUP: */
8505 case LVM_SCROLL:
8506 return LISTVIEW_Scroll(infoPtr, (INT)wParam, (INT)lParam);
8508 case LVM_SETBKCOLOR:
8509 return LISTVIEW_SetBkColor(infoPtr, (COLORREF)lParam);
8511 /* case LVM_SETBKIMAGE: */
8513 case LVM_SETCALLBACKMASK:
8514 infoPtr->uCallbackMask = (UINT)wParam;
8515 return TRUE;
8517 case LVM_SETCOLUMNA:
8518 return LISTVIEW_SetColumnT(infoPtr, (INT)wParam, (LPLVCOLUMNW)lParam, FALSE);
8520 case LVM_SETCOLUMNW:
8521 return LISTVIEW_SetColumnT(infoPtr, (INT)wParam, (LPLVCOLUMNW)lParam, TRUE);
8523 case LVM_SETCOLUMNORDERARRAY:
8524 return LISTVIEW_SetColumnOrderArray(infoPtr, (INT)wParam, (LPINT)lParam);
8526 case LVM_SETCOLUMNWIDTH:
8527 return LISTVIEW_SetColumnWidth(infoPtr, (INT)wParam, SLOWORD(lParam));
8529 case LVM_SETEXTENDEDLISTVIEWSTYLE:
8530 return LISTVIEW_SetExtendedListViewStyle(infoPtr, (DWORD)wParam, (DWORD)lParam);
8532 /* case LVN_SETGROUPINFO: */
8534 /* case LVN_SETGROUPMETRICS: */
8536 case LVM_SETHOTCURSOR:
8537 return (LRESULT)LISTVIEW_SetHotCursor(infoPtr, (HCURSOR)lParam);
8539 case LVM_SETHOTITEM:
8540 return LISTVIEW_SetHotItem(infoPtr, (INT)wParam);
8542 case LVM_SETHOVERTIME:
8543 return LISTVIEW_SetHoverTime(infoPtr, (DWORD)wParam);
8545 case LVM_SETICONSPACING:
8546 return LISTVIEW_SetIconSpacing(infoPtr, (DWORD)lParam);
8548 case LVM_SETIMAGELIST:
8549 return (LRESULT)LISTVIEW_SetImageList(infoPtr, (INT)wParam, (HIMAGELIST)lParam);
8551 /* case LVN_SETINFOTIP: */
8553 /* case LVN_SETINSERTMARK: */
8555 /* case LVN_SETINSERTMARKCOLOR: */
8557 case LVM_SETITEMA:
8558 return LISTVIEW_SetItemT(infoPtr, (LPLVITEMW)lParam, FALSE);
8560 case LVM_SETITEMW:
8561 return LISTVIEW_SetItemT(infoPtr, (LPLVITEMW)lParam, TRUE);
8563 case LVM_SETITEMCOUNT:
8564 return LISTVIEW_SetItemCount(infoPtr, (INT)wParam, (DWORD)lParam);
8566 case LVM_SETITEMPOSITION:
8568 POINT pt = { SLOWORD(lParam), SHIWORD(lParam) };
8569 return LISTVIEW_SetItemPosition(infoPtr, (INT)wParam, pt);
8572 case LVM_SETITEMPOSITION32:
8573 if (lParam == 0) return FALSE;
8574 return LISTVIEW_SetItemPosition(infoPtr, (INT)wParam, *((POINT*)lParam));
8576 case LVM_SETITEMSTATE:
8577 return LISTVIEW_SetItemState(infoPtr, (INT)wParam, (LPLVITEMW)lParam);
8579 case LVM_SETITEMTEXTA:
8580 return LISTVIEW_SetItemTextT(infoPtr, (INT)wParam, (LPLVITEMW)lParam, FALSE);
8582 case LVM_SETITEMTEXTW:
8583 return LISTVIEW_SetItemTextT(infoPtr, (INT)wParam, (LPLVITEMW)lParam, TRUE);
8585 /* case LVN_SETOUTLINECOLOR: */
8587 /* case LVN_SETSELECTEDCOLUMN: */
8589 case LVM_SETSELECTIONMARK:
8590 return LISTVIEW_SetSelectionMark(infoPtr, (INT)lParam);
8592 case LVM_SETTEXTBKCOLOR:
8593 return LISTVIEW_SetTextBkColor(infoPtr, (COLORREF)lParam);
8595 case LVM_SETTEXTCOLOR:
8596 return LISTVIEW_SetTextColor(infoPtr, (COLORREF)lParam);
8598 /* case LVN_SETTILEINFO: */
8600 /* case LVN_SETTILEVIEWINFO: */
8602 /* case LVN_SETTILEWIDTH: */
8604 /* case LVM_SETTOOLTIPS: */
8606 /* case LVM_SETUNICODEFORMAT: */
8608 /* case LVN_SETVIEW: */
8610 /* case LVM_SETWORKAREAS: */
8612 /* case LVN_SORTGROUPS: */
8614 case LVM_SORTITEMS:
8615 return LISTVIEW_SortItems(infoPtr, (PFNLVCOMPARE)lParam, (LPARAM)wParam);
8617 case LVM_SUBITEMHITTEST:
8618 return LISTVIEW_HitTest(infoPtr, (LPLVHITTESTINFO)lParam, TRUE, FALSE);
8620 case LVM_UPDATE:
8621 return LISTVIEW_Update(infoPtr, (INT)wParam);
8623 case WM_CHAR:
8624 return LISTVIEW_ProcessLetterKeys( infoPtr, wParam, lParam );
8626 case WM_COMMAND:
8627 return LISTVIEW_Command(infoPtr, wParam, lParam);
8629 case WM_CREATE:
8630 return LISTVIEW_Create(hwnd, (LPCREATESTRUCTW)lParam);
8632 case WM_ERASEBKGND:
8633 return LISTVIEW_EraseBkgnd(infoPtr, (HDC)wParam);
8635 case WM_GETDLGCODE:
8636 return DLGC_WANTCHARS | DLGC_WANTARROWS;
8638 case WM_GETFONT:
8639 return infoPtr->hFont;
8641 case WM_HSCROLL:
8642 return LISTVIEW_HScroll(infoPtr, (INT)LOWORD(wParam), 0, (HWND)lParam);
8644 case WM_KEYDOWN:
8645 return LISTVIEW_KeyDown(infoPtr, (INT)wParam, (LONG)lParam);
8647 case WM_KILLFOCUS:
8648 return LISTVIEW_KillFocus(infoPtr);
8650 case WM_LBUTTONDBLCLK:
8651 return LISTVIEW_LButtonDblClk(infoPtr, (WORD)wParam, MAKEPOINTS(lParam));
8653 case WM_LBUTTONDOWN:
8654 return LISTVIEW_LButtonDown(infoPtr, (WORD)wParam, MAKEPOINTS(lParam));
8656 case WM_LBUTTONUP:
8657 return LISTVIEW_LButtonUp(infoPtr, (WORD)wParam, MAKEPOINTS(lParam));
8659 case WM_MOUSEMOVE:
8660 return LISTVIEW_MouseMove (infoPtr, (WORD)wParam, MAKEPOINTS(lParam));
8662 case WM_MOUSEHOVER:
8663 return LISTVIEW_MouseHover(infoPtr, (WORD)wParam, MAKEPOINTS(lParam));
8665 case WM_NCDESTROY:
8666 return LISTVIEW_NCDestroy(infoPtr);
8668 case WM_NOTIFY:
8669 return LISTVIEW_Notify(infoPtr, (INT)wParam, (LPNMHDR)lParam);
8671 case WM_NOTIFYFORMAT:
8672 return LISTVIEW_NotifyFormat(infoPtr, (HWND)wParam, (INT)lParam);
8674 case WM_PAINT:
8675 return LISTVIEW_Paint(infoPtr, (HDC)wParam);
8677 case WM_RBUTTONDBLCLK:
8678 return LISTVIEW_RButtonDblClk(infoPtr, (WORD)wParam, MAKEPOINTS(lParam));
8680 case WM_RBUTTONDOWN:
8681 return LISTVIEW_RButtonDown(infoPtr, (WORD)wParam, MAKEPOINTS(lParam));
8683 case WM_RBUTTONUP:
8684 return LISTVIEW_RButtonUp(infoPtr, (WORD)wParam, MAKEPOINTS(lParam));
8686 case WM_SETCURSOR:
8687 if(LISTVIEW_SetCursor(infoPtr, (HWND)wParam, LOWORD(lParam), HIWORD(lParam)))
8688 return TRUE;
8689 goto fwd_msg;
8691 case WM_SETFOCUS:
8692 return LISTVIEW_SetFocus(infoPtr, (HWND)wParam);
8694 case WM_SETFONT:
8695 return LISTVIEW_SetFont(infoPtr, (HFONT)wParam, (WORD)lParam);
8697 case WM_SETREDRAW:
8698 return LISTVIEW_SetRedraw(infoPtr, (BOOL)wParam);
8700 case WM_SIZE:
8701 return LISTVIEW_Size(infoPtr, (int)SLOWORD(lParam), (int)SHIWORD(lParam));
8703 case WM_STYLECHANGED:
8704 return LISTVIEW_StyleChanged(infoPtr, wParam, (LPSTYLESTRUCT)lParam);
8706 case WM_SYSCOLORCHANGE:
8707 COMCTL32_RefreshSysColors();
8708 return 0;
8710 /* case WM_TIMER: */
8712 case WM_VSCROLL:
8713 return LISTVIEW_VScroll(infoPtr, (INT)LOWORD(wParam), 0, (HWND)lParam);
8715 case WM_MOUSEWHEEL:
8716 if (wParam & (MK_SHIFT | MK_CONTROL))
8717 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
8718 return LISTVIEW_MouseWheel(infoPtr, (short int)HIWORD(wParam));
8720 case WM_WINDOWPOSCHANGED:
8721 if (!(((WINDOWPOS *)lParam)->flags & SWP_NOSIZE)) {
8722 SetWindowPos(infoPtr->hwndSelf, 0, 0, 0, 0, 0, SWP_FRAMECHANGED | SWP_NOACTIVATE |
8723 SWP_NOZORDER | SWP_NOMOVE | SWP_NOSIZE);
8724 LISTVIEW_UpdateSize(infoPtr);
8725 LISTVIEW_UpdateScroll(infoPtr);
8727 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
8729 /* case WM_WININICHANGE: */
8731 default:
8732 if ((uMsg >= WM_USER) && (uMsg < WM_APP))
8733 ERR("unknown msg %04x wp=%08x lp=%08lx\n", uMsg, wParam, lParam);
8735 fwd_msg:
8736 /* call default window procedure */
8737 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
8740 return 0;
8743 /***
8744 * DESCRIPTION:
8745 * Registers the window class.
8747 * PARAMETER(S):
8748 * None
8750 * RETURN:
8751 * None
8753 void LISTVIEW_Register(void)
8755 WNDCLASSW wndClass;
8757 ZeroMemory(&wndClass, sizeof(WNDCLASSW));
8758 wndClass.style = CS_GLOBALCLASS | CS_DBLCLKS;
8759 wndClass.lpfnWndProc = (WNDPROC)LISTVIEW_WindowProc;
8760 wndClass.cbClsExtra = 0;
8761 wndClass.cbWndExtra = sizeof(LISTVIEW_INFO *);
8762 wndClass.hCursor = LoadCursorW(0, IDC_ARROWW);
8763 wndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
8764 wndClass.lpszClassName = WC_LISTVIEWW;
8765 RegisterClassW(&wndClass);
8768 /***
8769 * DESCRIPTION:
8770 * Unregisters the window class.
8772 * PARAMETER(S):
8773 * None
8775 * RETURN:
8776 * None
8778 void LISTVIEW_Unregister(void)
8780 UnregisterClassW(WC_LISTVIEWW, (HINSTANCE)NULL);
8783 /***
8784 * DESCRIPTION:
8785 * Handle any WM_COMMAND messages
8787 * PARAMETER(S):
8789 * RETURN:
8791 static LRESULT LISTVIEW_Command(LISTVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
8793 switch (HIWORD(wParam))
8795 case EN_UPDATE:
8798 * Adjust the edit window size
8800 WCHAR buffer[1024];
8801 HDC hdc = GetDC(infoPtr->hwndEdit);
8802 HFONT hFont, hOldFont = 0;
8803 RECT rect;
8804 SIZE sz;
8805 int len;
8807 if (!infoPtr->hwndEdit || !hdc) return 0;
8808 len = GetWindowTextW(infoPtr->hwndEdit, buffer, sizeof(buffer)/sizeof(buffer[0]));
8809 GetWindowRect(infoPtr->hwndEdit, &rect);
8811 /* Select font to get the right dimension of the string */
8812 hFont = SendMessageW(infoPtr->hwndEdit, WM_GETFONT, 0, 0);
8813 if(hFont != 0)
8815 hOldFont = SelectObject(hdc, hFont);
8818 if (GetTextExtentPoint32W(hdc, buffer, lstrlenW(buffer), &sz))
8820 TEXTMETRICW textMetric;
8822 /* Add Extra spacing for the next character */
8823 GetTextMetricsW(hdc, &textMetric);
8824 sz.cx += (textMetric.tmMaxCharWidth * 2);
8826 SetWindowPos (
8827 infoPtr->hwndEdit,
8828 HWND_TOP,
8831 sz.cx,
8832 rect.bottom - rect.top,
8833 SWP_DRAWFRAME|SWP_NOMOVE);
8835 if(hFont != 0)
8836 SelectObject(hdc, hOldFont);
8838 ReleaseDC(infoPtr->hwndSelf, hdc);
8840 break;
8843 default:
8844 return SendMessageW (GetParent (infoPtr->hwndSelf), WM_COMMAND, wParam, lParam);
8847 return 0;
8851 /***
8852 * DESCRIPTION:
8853 * Subclassed edit control windproc function
8855 * PARAMETER(S):
8857 * RETURN:
8859 static LRESULT EditLblWndProcT(HWND hwnd, UINT uMsg,
8860 WPARAM wParam, LPARAM lParam, BOOL isW)
8862 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)GetWindowLongW(GetParent(hwnd), 0);
8863 BOOL cancel = FALSE;
8865 TRACE("(hwnd=%x, uMsg=%x, wParam=%x, lParam=%lx, isW=%d)\n",
8866 hwnd, uMsg, wParam, lParam, isW);
8868 switch (uMsg)
8870 case WM_GETDLGCODE:
8871 return DLGC_WANTARROWS | DLGC_WANTALLKEYS;
8873 case WM_KILLFOCUS:
8874 break;
8876 case WM_DESTROY:
8878 WNDPROC editProc = infoPtr->EditWndProc;
8879 infoPtr->EditWndProc = 0;
8880 SetWindowLongW(hwnd, GWL_WNDPROC, (LONG)editProc);
8881 return CallWindowProcT(editProc, hwnd, uMsg, wParam, lParam, isW);
8884 case WM_KEYDOWN:
8885 if (VK_ESCAPE == (INT)wParam)
8887 cancel = TRUE;
8888 break;
8890 else if (VK_RETURN == (INT)wParam)
8891 break;
8893 default:
8894 return CallWindowProcT(infoPtr->EditWndProc, hwnd, uMsg, wParam, lParam, isW);
8897 /* kill the edit */
8898 if (infoPtr->hwndEdit)
8900 LPWSTR buffer = NULL;
8902 infoPtr->hwndEdit = 0;
8903 if (!cancel)
8905 DWORD len = isW ? GetWindowTextLengthW(hwnd) : GetWindowTextLengthA(hwnd);
8907 if (len)
8909 if ( (buffer = COMCTL32_Alloc((len+1) * (isW ? sizeof(WCHAR) : sizeof(CHAR)))) )
8911 if (isW) GetWindowTextW(hwnd, buffer, len+1);
8912 else GetWindowTextA(hwnd, (CHAR*)buffer, len+1);
8916 LISTVIEW_EndEditLabelT(infoPtr, buffer, isW);
8918 if (buffer) COMCTL32_Free(buffer);
8922 SendMessageW(hwnd, WM_CLOSE, 0, 0);
8923 return TRUE;
8926 /***
8927 * DESCRIPTION:
8928 * Subclassed edit control windproc function
8930 * PARAMETER(S):
8932 * RETURN:
8934 LRESULT CALLBACK EditLblWndProcW(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
8936 return EditLblWndProcT(hwnd, uMsg, wParam, lParam, TRUE);
8939 /***
8940 * DESCRIPTION:
8941 * Subclassed edit control windproc function
8943 * PARAMETER(S):
8945 * RETURN:
8947 LRESULT CALLBACK EditLblWndProcA(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
8949 return EditLblWndProcT(hwnd, uMsg, wParam, lParam, FALSE);
8952 /***
8953 * DESCRIPTION:
8954 * Creates a subclassed edit cotrol
8956 * PARAMETER(S):
8958 * RETURN:
8960 static HWND CreateEditLabelT(LISTVIEW_INFO *infoPtr, LPCWSTR text, DWORD style,
8961 INT x, INT y, INT width, INT height, BOOL isW)
8963 WCHAR editName[5] = { 'E', 'd', 'i', 't', '\0' };
8964 HWND hedit;
8965 SIZE sz;
8966 HDC hdc;
8967 HDC hOldFont=0;
8968 TEXTMETRICW textMetric;
8969 HINSTANCE hinst = GetWindowLongW(infoPtr->hwndSelf, GWL_HINSTANCE);
8971 TRACE("(text=%s, ..., isW=%d)\n", debugtext_t(text, isW), isW);
8973 style |= WS_CHILDWINDOW|WS_CLIPSIBLINGS|ES_LEFT|WS_BORDER;
8974 hdc = GetDC(infoPtr->hwndSelf);
8976 /* Select the font to get appropriate metric dimensions */
8977 if(infoPtr->hFont != 0)
8978 hOldFont = SelectObject(hdc, infoPtr->hFont);
8980 /*Get String Lenght in pixels */
8981 GetTextExtentPoint32W(hdc, text, lstrlenW(text), &sz);
8983 /*Add Extra spacing for the next character */
8984 GetTextMetricsW(hdc, &textMetric);
8985 sz.cx += (textMetric.tmMaxCharWidth * 2);
8987 if(infoPtr->hFont != 0)
8988 SelectObject(hdc, hOldFont);
8990 ReleaseDC(infoPtr->hwndSelf, hdc);
8991 if (isW)
8992 hedit = CreateWindowW(editName, text, style, x, y, sz.cx, height, infoPtr->hwndSelf, 0, hinst, 0);
8993 else
8994 hedit = CreateWindowA("Edit", (LPCSTR)text, style, x, y, sz.cx, height, infoPtr->hwndSelf, 0, hinst, 0);
8996 if (!hedit) return 0;
8998 infoPtr->EditWndProc = (WNDPROC)
8999 (isW ? SetWindowLongW(hedit, GWL_WNDPROC, (LONG)EditLblWndProcW) :
9000 SetWindowLongA(hedit, GWL_WNDPROC, (LONG)EditLblWndProcA) );
9002 SendMessageW(hedit, WM_SETFONT, infoPtr->hFont, FALSE);
9004 return hedit;