ia2comproxy: Introduce new proxy stub DLL for IAccessible2.
[wine.git] / dlls / comctl32 / listview.c
blob730bf4aaddd5a8cf92f9852c5a7bfe0bb0176d69
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
9 * Copyright 2009-2015 Nikolay Sivov
10 * Copyright 2009 Owen Rudge for CodeWeavers
11 * Copyright 2012-2013 Daniel Jelinski
13 * This library is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU Lesser General Public
15 * License as published by the Free Software Foundation; either
16 * version 2.1 of the License, or (at your option) any later version.
18 * This library is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 * Lesser General Public License for more details.
23 * You should have received a copy of the GNU Lesser General Public
24 * License along with this library; if not, write to the Free Software
25 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
27 * TODO:
29 * Default Message Processing
30 * -- WM_CREATE: create the icon and small icon image lists at this point only if
31 * the LVS_SHAREIMAGELISTS style is not specified.
32 * -- WM_WINDOWPOSCHANGED: arrange the list items if the current view is icon
33 * or small icon and the LVS_AUTOARRANGE style is specified.
34 * -- WM_TIMER
35 * -- WM_WININICHANGE
37 * Features
38 * -- Hot item handling, mouse hovering
39 * -- Workareas support
40 * -- Tilemode support
41 * -- Groups support
43 * Bugs
44 * -- Expand large item in ICON mode when the cursor is flying over the icon or text.
45 * -- Support CustomDraw options for _WIN32_IE >= 0x560 (see NMLVCUSTOMDRAW docs).
46 * -- LVA_SNAPTOGRID not implemented
47 * -- LISTVIEW_ApproximateViewRect partially implemented
48 * -- LISTVIEW_StyleChanged doesn't handle some changes too well
50 * Speedups
51 * -- LISTVIEW_GetNextItem needs to be rewritten. It is currently
52 * linear in the number of items in the list, and this is
53 * unacceptable for large lists.
54 * -- if list is sorted by item text LISTVIEW_InsertItemT could use
55 * binary search to calculate item index (e.g. DPA_Search()).
56 * This requires sorted state to be reliably tracked in item modifiers.
57 * -- we should keep an ordered array of coordinates in iconic mode.
58 * This would allow framing items (iterator_frameditems),
59 * and finding the nearest item (LVFI_NEARESTXY) a lot more efficiently.
61 * Flags
62 * -- LVIF_COLUMNS
63 * -- LVIF_GROUPID
65 * States
66 * -- LVIS_ACTIVATING (not currently supported by comctl32.dll version 6.0)
67 * -- LVIS_DROPHILITED
69 * Styles
70 * -- LVS_NOLABELWRAP
71 * -- LVS_NOSCROLL (see Q137520)
72 * -- LVS_ALIGNTOP
74 * Extended Styles
75 * -- LVS_EX_BORDERSELECT
76 * -- LVS_EX_FLATSB
77 * -- LVS_EX_INFOTIP
78 * -- LVS_EX_LABELTIP
79 * -- LVS_EX_MULTIWORKAREAS
80 * -- LVS_EX_REGIONAL
81 * -- LVS_EX_SIMPLESELECT
82 * -- LVS_EX_TWOCLICKACTIVATE
83 * -- LVS_EX_UNDERLINECOLD
84 * -- LVS_EX_UNDERLINEHOT
86 * Notifications:
87 * -- LVN_BEGINSCROLL, LVN_ENDSCROLL
88 * -- LVN_GETINFOTIP
89 * -- LVN_HOTTRACK
90 * -- LVN_SETDISPINFO
92 * Messages:
93 * -- LVM_ENABLEGROUPVIEW
94 * -- LVM_GETBKIMAGE, LVM_SETBKIMAGE
95 * -- LVM_GETGROUPINFO, LVM_SETGROUPINFO
96 * -- LVM_GETGROUPMETRICS, LVM_SETGROUPMETRICS
97 * -- LVM_GETINSERTMARK, LVM_SETINSERTMARK
98 * -- LVM_GETINSERTMARKCOLOR, LVM_SETINSERTMARKCOLOR
99 * -- LVM_GETINSERTMARKRECT
100 * -- LVM_GETNUMBEROFWORKAREAS
101 * -- LVM_GETOUTLINECOLOR, LVM_SETOUTLINECOLOR
102 * -- LVM_GETISEARCHSTRINGW, LVM_GETISEARCHSTRINGA
103 * -- LVM_GETTILEINFO, LVM_SETTILEINFO
104 * -- LVM_GETTILEVIEWINFO, LVM_SETTILEVIEWINFO
105 * -- LVM_GETWORKAREAS, LVM_SETWORKAREAS
106 * -- LVM_HASGROUP, LVM_INSERTGROUP, LVM_REMOVEGROUP, LVM_REMOVEALLGROUPS
107 * -- LVM_INSERTGROUPSORTED
108 * -- LVM_INSERTMARKHITTEST
109 * -- LVM_ISGROUPVIEWENABLED
110 * -- LVM_MOVEGROUP
111 * -- LVM_MOVEITEMTOGROUP
112 * -- LVM_SETINFOTIP
113 * -- LVM_SETTILEWIDTH
114 * -- LVM_SORTGROUPS
116 * Macros:
117 * -- ListView_GetHoverTime, ListView_SetHoverTime
118 * -- ListView_GetISearchString
119 * -- ListView_GetNumberOfWorkAreas
120 * -- ListView_GetWorkAreas, ListView_SetWorkAreas
122 * Functions:
123 * -- LVGroupComparE
126 #include <assert.h>
127 #include <ctype.h>
128 #include <string.h>
129 #include <stdlib.h>
130 #include <stdarg.h>
131 #include <stdio.h>
133 #include "windef.h"
134 #include "winbase.h"
135 #include "winnt.h"
136 #include "wingdi.h"
137 #include "winuser.h"
138 #include "winnls.h"
139 #include "commctrl.h"
140 #include "comctl32.h"
141 #include "uxtheme.h"
142 #include "vsstyle.h"
143 #include "shlwapi.h"
145 #include "wine/debug.h"
147 WINE_DEFAULT_DEBUG_CHANNEL(listview);
149 typedef struct tagCOLUMN_INFO
151 RECT rcHeader; /* tracks the header's rectangle */
152 INT fmt; /* same as LVCOLUMN.fmt */
153 INT cxMin;
154 } COLUMN_INFO;
156 typedef struct tagITEMHDR
158 LPWSTR pszText;
159 INT iImage;
160 } ITEMHDR, *LPITEMHDR;
162 typedef struct tagSUBITEM_INFO
164 ITEMHDR hdr;
165 INT iSubItem;
166 } SUBITEM_INFO;
168 typedef struct tagITEM_ID ITEM_ID;
170 typedef struct tagITEM_INFO
172 ITEMHDR hdr;
173 UINT state;
174 LPARAM lParam;
175 INT iIndent;
176 ITEM_ID *id;
177 } ITEM_INFO;
179 struct tagITEM_ID
181 UINT id; /* item id */
182 HDPA item; /* link to item data */
185 typedef struct tagRANGE
187 INT lower;
188 INT upper;
189 } RANGE;
191 typedef struct tagRANGES
193 HDPA hdpa;
194 } *RANGES;
196 typedef struct tagITERATOR
198 INT nItem;
199 INT nSpecial;
200 RANGE range;
201 RANGES ranges;
202 INT index;
203 } ITERATOR;
205 typedef struct tagDELAYED_ITEM_EDIT
207 BOOL fEnabled;
208 INT iItem;
209 } DELAYED_ITEM_EDIT;
211 enum notification_mask
213 NOTIFY_MASK_ITEM_CHANGE = 0x1,
214 NOTIFY_MASK_END_LABEL_EDIT = 0x2,
215 NOTIFY_MASK_UNMASK_ALL = 0xffffffff
218 typedef struct tagLISTVIEW_INFO
220 /* control window */
221 HWND hwndSelf;
222 RECT rcList; /* This rectangle is really the window
223 * client rectangle possibly reduced by the
224 * horizontal scroll bar and/or header - see
225 * LISTVIEW_UpdateSize. This rectangle offset
226 * by the LISTVIEW_GetOrigin value is in
227 * client coordinates */
229 /* notification window */
230 SHORT notifyFormat;
231 HWND hwndNotify;
232 DWORD notify_mask;
233 UINT uCallbackMask;
235 /* tooltips */
236 HWND hwndToolTip;
238 /* items */
239 INT nItemCount; /* the number of items in the list */
240 HDPA hdpaItems; /* array ITEM_INFO pointers */
241 HDPA hdpaItemIds; /* array of ITEM_ID pointers */
242 HDPA hdpaPosX; /* maintains the (X, Y) coordinates of the */
243 HDPA hdpaPosY; /* items in LVS_ICON, and LVS_SMALLICON modes */
244 RANGES selectionRanges;
245 INT nSelectionMark; /* item to start next multiselection from */
246 INT nHotItem;
248 /* columns */
249 HDPA hdpaColumns; /* array of COLUMN_INFO pointers */
250 BOOL colRectsDirty; /* trigger column rectangles requery from header */
251 INT selected_column; /* index for LVM_SETSELECTEDCOLUMN/LVM_GETSELECTEDCOLUMN */
253 /* item metrics */
254 BOOL bNoItemMetrics; /* flags if item metrics are not yet computed */
255 INT nItemHeight;
256 INT nItemWidth;
258 /* style */
259 DWORD dwStyle; /* the cached window GWL_STYLE */
260 DWORD dwLvExStyle; /* extended listview style */
261 DWORD uView; /* current view available through LVM_[G,S]ETVIEW */
263 /* edit item */
264 HWND hwndEdit;
265 WNDPROC EditWndProc;
266 INT nEditLabelItem;
267 DELAYED_ITEM_EDIT itemEdit; /* Pointer to this structure will be the timer ID */
269 /* icons */
270 HIMAGELIST himlNormal;
271 HIMAGELIST himlSmall;
272 HIMAGELIST himlState;
273 SIZE iconSize;
274 BOOL autoSpacing;
275 SIZE iconSpacing;
276 SIZE iconStateSize;
277 POINT currIconPos; /* this is the position next icon will be placed */
279 /* header */
280 HWND hwndHeader;
281 INT xTrackLine; /* The x coefficient of the track line or -1 if none */
283 /* marquee selection */
284 BOOL bMarqueeSelect; /* marquee selection/highlight underway */
285 BOOL bScrolling;
286 RECT marqueeRect; /* absolute coordinates of marquee selection */
287 RECT marqueeDrawRect; /* relative coordinates for drawing marquee */
288 POINT marqueeOrigin; /* absolute coordinates of marquee click origin */
290 /* focus drawing */
291 BOOL bFocus; /* control has focus */
292 INT nFocusedItem;
293 RECT rcFocus; /* focus bounds */
295 /* colors */
296 HBRUSH hBkBrush;
297 COLORREF clrBk;
298 COLORREF clrText;
299 COLORREF clrTextBk;
301 /* font */
302 HFONT hDefaultFont;
303 HFONT hFont;
304 INT ntmHeight; /* Some cached metrics of the font used */
305 INT ntmMaxCharWidth; /* by the listview to draw items */
306 INT nEllipsisWidth;
308 /* mouse operation */
309 BOOL bLButtonDown;
310 BOOL bDragging;
311 POINT ptClickPos; /* point where the user clicked */
312 INT nLButtonDownItem; /* tracks item to reset multiselection on WM_LBUTTONUP */
313 DWORD dwHoverTime;
314 HCURSOR hHotCursor;
315 INT cWheelRemainder;
317 /* keyboard operation */
318 DWORD lastKeyPressTimestamp;
319 WPARAM charCode;
320 INT nSearchParamLength;
321 WCHAR szSearchParam[ MAX_PATH ];
323 /* painting */
324 BOOL bIsDrawing; /* Drawing in progress */
325 INT nMeasureItemHeight; /* WM_MEASUREITEM result */
326 BOOL redraw; /* WM_SETREDRAW switch */
328 /* misc */
329 DWORD iVersion; /* CCM_[G,S]ETVERSION */
330 } LISTVIEW_INFO;
333 * constants
335 /* How many we debug buffer to allocate */
336 #define DEBUG_BUFFERS 20
337 /* The size of a single debug buffer */
338 #define DEBUG_BUFFER_SIZE 256
340 /* Internal interface to LISTVIEW_HScroll and LISTVIEW_VScroll */
341 #define SB_INTERNAL -1
343 /* maximum size of a label */
344 #define DISP_TEXT_SIZE 260
346 /* padding for items in list and small icon display modes */
347 #define WIDTH_PADDING 12
349 /* padding for items in list, report and small icon display modes */
350 #define HEIGHT_PADDING 1
352 /* offset of items in report display mode */
353 #define REPORT_MARGINX 2
355 /* padding for icon in large icon display mode
356 * ICON_TOP_PADDING_NOTHITABLE - space between top of box and area
357 * that HITTEST will see.
358 * ICON_TOP_PADDING_HITABLE - spacing between above and icon.
359 * ICON_TOP_PADDING - sum of the two above.
360 * ICON_BOTTOM_PADDING - between bottom of icon and top of text
361 * LABEL_HOR_PADDING - between text and sides of box
362 * LABEL_VERT_PADDING - between bottom of text and end of box
364 * ICON_LR_PADDING - additional width above icon size.
365 * ICON_LR_HALF - half of the above value
367 #define ICON_TOP_PADDING_NOTHITABLE 2
368 #define ICON_TOP_PADDING_HITABLE 2
369 #define ICON_TOP_PADDING (ICON_TOP_PADDING_NOTHITABLE + ICON_TOP_PADDING_HITABLE)
370 #define ICON_BOTTOM_PADDING 4
371 #define LABEL_HOR_PADDING 5
372 #define LABEL_VERT_PADDING 7
373 #define ICON_LR_PADDING 16
374 #define ICON_LR_HALF (ICON_LR_PADDING/2)
376 /* default label width for items in list and small icon display modes */
377 #define DEFAULT_LABEL_WIDTH 40
378 /* maximum select rectangle width for empty text item in LV_VIEW_DETAILS */
379 #define MAX_EMPTYTEXT_SELECT_WIDTH 80
381 /* default column width for items in list display mode */
382 #define DEFAULT_COLUMN_WIDTH 128
384 /* Size of "line" scroll for V & H scrolls */
385 #define LISTVIEW_SCROLL_ICON_LINE_SIZE 37
387 /* Padding between image and label */
388 #define IMAGE_PADDING 2
390 /* Padding behind the label */
391 #define TRAILING_LABEL_PADDING 12
392 #define TRAILING_HEADER_PADDING 11
394 /* Border for the icon caption */
395 #define CAPTION_BORDER 2
397 /* Standard DrawText flags */
398 #define LV_ML_DT_FLAGS (DT_TOP | DT_NOPREFIX | DT_EDITCONTROL | DT_CENTER | DT_WORDBREAK | DT_WORD_ELLIPSIS | DT_END_ELLIPSIS)
399 #define LV_FL_DT_FLAGS (DT_TOP | DT_NOPREFIX | DT_EDITCONTROL | DT_CENTER | DT_WORDBREAK | DT_NOCLIP)
400 #define LV_SL_DT_FLAGS (DT_VCENTER | DT_NOPREFIX | DT_EDITCONTROL | DT_SINGLELINE | DT_WORD_ELLIPSIS | DT_END_ELLIPSIS)
402 /* Image index from state */
403 #define STATEIMAGEINDEX(x) (((x) & LVIS_STATEIMAGEMASK) >> 12)
405 /* The time in milliseconds to reset the search in the list */
406 #define KEY_DELAY 450
408 /* Dump the LISTVIEW_INFO structure to the debug channel */
409 #define LISTVIEW_DUMP(iP) do { \
410 TRACE("hwndSelf=%p, clrBk=%#lx, clrText=%#lx, clrTextBk=%#lx, ItemHeight=%d, ItemWidth=%d, Style=%#lx\n", \
411 iP->hwndSelf, iP->clrBk, iP->clrText, iP->clrTextBk, \
412 iP->nItemHeight, iP->nItemWidth, iP->dwStyle); \
413 TRACE("hwndSelf=%p, himlNor=%p, himlSml=%p, himlState=%p, Focused=%d, Hot=%d, exStyle=%#lx, Focus=%d\n", \
414 iP->hwndSelf, iP->himlNormal, iP->himlSmall, iP->himlState, \
415 iP->nFocusedItem, iP->nHotItem, iP->dwLvExStyle, iP->bFocus ); \
416 TRACE("hwndSelf=%p, ntmH=%d, icSz.cx=%ld, icSz.cy=%ld, icSp.cx=%ld, icSp.cy=%ld, notifyFmt=%d\n", \
417 iP->hwndSelf, iP->ntmHeight, iP->iconSize.cx, iP->iconSize.cy, \
418 iP->iconSpacing.cx, iP->iconSpacing.cy, iP->notifyFormat); \
419 TRACE("hwndSelf=%p, rcList=%s\n", iP->hwndSelf, wine_dbgstr_rect(&iP->rcList)); \
420 } while(0)
422 static const WCHAR themeClass[] = L"ListView";
425 * forward declarations
427 static BOOL LISTVIEW_GetItemT(const LISTVIEW_INFO *, LPLVITEMW, BOOL);
428 static void LISTVIEW_GetItemBox(const LISTVIEW_INFO *, INT, LPRECT);
429 static void LISTVIEW_GetItemOrigin(const LISTVIEW_INFO *, INT, LPPOINT);
430 static BOOL LISTVIEW_GetItemPosition(const LISTVIEW_INFO *, INT, LPPOINT);
431 static BOOL LISTVIEW_GetItemRect(const LISTVIEW_INFO *, INT, LPRECT);
432 static void LISTVIEW_GetOrigin(const LISTVIEW_INFO *, LPPOINT);
433 static BOOL LISTVIEW_GetViewRect(const LISTVIEW_INFO *, LPRECT);
434 static void LISTVIEW_UpdateSize(LISTVIEW_INFO *);
435 static LRESULT LISTVIEW_Command(LISTVIEW_INFO *, WPARAM, LPARAM);
436 static INT LISTVIEW_GetStringWidthT(const LISTVIEW_INFO *, LPCWSTR, BOOL);
437 static BOOL LISTVIEW_KeySelection(LISTVIEW_INFO *, INT, BOOL);
438 static UINT LISTVIEW_GetItemState(const LISTVIEW_INFO *, INT, UINT);
439 static BOOL LISTVIEW_SetItemState(LISTVIEW_INFO *, INT, const LVITEMW *);
440 static LRESULT LISTVIEW_VScroll(LISTVIEW_INFO *, INT, INT);
441 static LRESULT LISTVIEW_HScroll(LISTVIEW_INFO *, INT, INT);
442 static BOOL LISTVIEW_EnsureVisible(LISTVIEW_INFO *, INT, BOOL);
443 static HIMAGELIST LISTVIEW_SetImageList(LISTVIEW_INFO *, INT, HIMAGELIST);
444 static INT LISTVIEW_HitTest(const LISTVIEW_INFO *, LPLVHITTESTINFO, BOOL, BOOL);
445 static BOOL LISTVIEW_EndEditLabelT(LISTVIEW_INFO *, BOOL, BOOL);
446 static BOOL LISTVIEW_Scroll(LISTVIEW_INFO *, INT, INT);
448 /******** Text handling functions *************************************/
450 /* A text pointer is either NULL, LPSTR_TEXTCALLBACK, or points to a
451 * text string. The string may be ANSI or Unicode, in which case
452 * the boolean isW tells us the type of the string.
454 * The name of the function tell what type of strings it expects:
455 * W: Unicode, T: ANSI/Unicode - function of isW
458 static inline BOOL is_text(LPCWSTR text)
460 return text != NULL && text != LPSTR_TEXTCALLBACKW;
463 static inline int textlenT(LPCWSTR text, BOOL isW)
465 return !is_text(text) ? 0 :
466 isW ? lstrlenW(text) : lstrlenA((LPCSTR)text);
469 static inline void textcpynT(LPWSTR dest, BOOL isDestW, LPCWSTR src, BOOL isSrcW, INT max)
471 if (isDestW)
472 if (isSrcW) lstrcpynW(dest, src, max);
473 else MultiByteToWideChar(CP_ACP, 0, (LPCSTR)src, -1, dest, max);
474 else
475 if (isSrcW) WideCharToMultiByte(CP_ACP, 0, src, -1, (LPSTR)dest, max, NULL, NULL);
476 else lstrcpynA((LPSTR)dest, (LPCSTR)src, max);
479 static inline LPWSTR textdupTtoW(LPCWSTR text, BOOL isW)
481 LPWSTR wstr = (LPWSTR)text;
483 if (!isW && is_text(text))
485 INT len = MultiByteToWideChar(CP_ACP, 0, (LPCSTR)text, -1, NULL, 0);
486 wstr = Alloc(len * sizeof(WCHAR));
487 if (wstr) MultiByteToWideChar(CP_ACP, 0, (LPCSTR)text, -1, wstr, len);
489 TRACE(" wstr=%s\n", text == LPSTR_TEXTCALLBACKW ? "(callback)" : debugstr_w(wstr));
490 return wstr;
493 static inline void textfreeT(LPWSTR wstr, BOOL isW)
495 if (!isW && is_text(wstr)) Free (wstr);
499 * dest is a pointer to a Unicode string
500 * src is a pointer to a string (Unicode if isW, ANSI if !isW)
502 static BOOL textsetptrT(LPWSTR *dest, LPCWSTR src, BOOL isW)
504 BOOL bResult = TRUE;
506 if (src == LPSTR_TEXTCALLBACKW)
508 if (is_text(*dest)) Free(*dest);
509 *dest = LPSTR_TEXTCALLBACKW;
511 else
513 LPWSTR pszText = textdupTtoW(src, isW);
514 if (*dest == LPSTR_TEXTCALLBACKW) *dest = NULL;
515 bResult = Str_SetPtrW(dest, pszText);
516 textfreeT(pszText, isW);
518 return bResult;
522 * compares a Unicode to a Unicode/ANSI text string
524 static inline int textcmpWT(LPCWSTR aw, LPCWSTR bt, BOOL isW)
526 if (!aw) return bt ? -1 : 0;
527 if (!bt) return 1;
528 if (aw == LPSTR_TEXTCALLBACKW)
529 return bt == LPSTR_TEXTCALLBACKW ? 1 : -1;
530 if (bt != LPSTR_TEXTCALLBACKW)
532 LPWSTR bw = textdupTtoW(bt, isW);
533 int r = bw ? lstrcmpW(aw, bw) : 1;
534 textfreeT(bw, isW);
535 return r;
538 return 1;
541 /******** Debugging functions *****************************************/
543 static inline LPCSTR debugtext_t(LPCWSTR text, BOOL isW)
545 if (text == LPSTR_TEXTCALLBACKW) return "(callback)";
546 return isW ? debugstr_w(text) : debugstr_a((LPCSTR)text);
549 static inline LPCSTR debugtext_tn(LPCWSTR text, BOOL isW, INT n)
551 if (text == LPSTR_TEXTCALLBACKW) return "(callback)";
552 n = min(textlenT(text, isW), n);
553 return isW ? debugstr_wn(text, n) : debugstr_an((LPCSTR)text, n);
556 static char* debug_getbuf(void)
558 static int index = 0;
559 static char buffers[DEBUG_BUFFERS][DEBUG_BUFFER_SIZE];
560 return buffers[index++ % DEBUG_BUFFERS];
563 static inline const char* debugrange(const RANGE *lprng)
565 if (!lprng) return "(null)";
566 return wine_dbg_sprintf("[%d, %d]", lprng->lower, lprng->upper);
569 static const char* debugscrollinfo(const SCROLLINFO *pScrollInfo)
571 char* buf = debug_getbuf(), *text = buf;
572 int len, size = DEBUG_BUFFER_SIZE;
574 if (pScrollInfo == NULL) return "(null)";
575 len = snprintf(buf, size, "{cbSize=%u, ", pScrollInfo->cbSize);
576 if (len == -1) goto end;
577 buf += len; size -= len;
578 if (pScrollInfo->fMask & SIF_RANGE)
579 len = snprintf(buf, size, "nMin=%d, nMax=%d, ", pScrollInfo->nMin, pScrollInfo->nMax);
580 else len = 0;
581 if (len == -1) goto end;
582 buf += len; size -= len;
583 if (pScrollInfo->fMask & SIF_PAGE)
584 len = snprintf(buf, size, "nPage=%u, ", pScrollInfo->nPage);
585 else len = 0;
586 if (len == -1) goto end;
587 buf += len; size -= len;
588 if (pScrollInfo->fMask & SIF_POS)
589 len = snprintf(buf, size, "nPos=%d, ", pScrollInfo->nPos);
590 else len = 0;
591 if (len == -1) goto end;
592 buf += len; size -= len;
593 if (pScrollInfo->fMask & SIF_TRACKPOS)
594 len = snprintf(buf, size, "nTrackPos=%d, ", pScrollInfo->nTrackPos);
595 else len = 0;
596 if (len == -1) goto end;
597 buf += len;
598 goto undo;
599 end:
600 buf = text + strlen(text);
601 undo:
602 if (buf - text > 2) { buf[-2] = '}'; buf[-1] = 0; }
603 return text;
606 static const char* debugnmlistview(const NMLISTVIEW *plvnm)
608 if (!plvnm) return "(null)";
609 return wine_dbg_sprintf("iItem=%d, iSubItem=%d, uNewState=0x%x,"
610 " uOldState=0x%x, uChanged=0x%x, ptAction=%s, lParam=%Id",
611 plvnm->iItem, plvnm->iSubItem, plvnm->uNewState, plvnm->uOldState,
612 plvnm->uChanged, wine_dbgstr_point(&plvnm->ptAction), plvnm->lParam);
615 static const char* debuglvitem_t(const LVITEMW *lpLVItem, BOOL isW)
617 char* buf = debug_getbuf(), *text = buf;
618 int len, size = DEBUG_BUFFER_SIZE;
620 if (lpLVItem == NULL) return "(null)";
621 len = snprintf(buf, size, "{iItem=%d, iSubItem=%d, ", lpLVItem->iItem, lpLVItem->iSubItem);
622 if (len == -1) goto end;
623 buf += len; size -= len;
624 if (lpLVItem->mask & LVIF_STATE)
625 len = snprintf(buf, size, "state=%x, stateMask=%x, ", lpLVItem->state, lpLVItem->stateMask);
626 else len = 0;
627 if (len == -1) goto end;
628 buf += len; size -= len;
629 if (lpLVItem->mask & LVIF_TEXT)
630 len = snprintf(buf, size, "pszText=%s, cchTextMax=%d, ", debugtext_tn(lpLVItem->pszText, isW, 80), lpLVItem->cchTextMax);
631 else len = 0;
632 if (len == -1) goto end;
633 buf += len; size -= len;
634 if (lpLVItem->mask & LVIF_IMAGE)
635 len = snprintf(buf, size, "iImage=%d, ", lpLVItem->iImage);
636 else len = 0;
637 if (len == -1) goto end;
638 buf += len; size -= len;
639 if (lpLVItem->mask & LVIF_PARAM)
640 len = snprintf(buf, size, "lParam=%Ix, ", lpLVItem->lParam);
641 else len = 0;
642 if (len == -1) goto end;
643 buf += len; size -= len;
644 if (lpLVItem->mask & LVIF_INDENT)
645 len = snprintf(buf, size, "iIndent=%d, ", lpLVItem->iIndent);
646 else len = 0;
647 if (len == -1) goto end;
648 buf += len;
649 goto undo;
650 end:
651 buf = text + strlen(text);
652 undo:
653 if (buf - text > 2) { buf[-2] = '}'; buf[-1] = 0; }
654 return text;
657 static const char* debuglvcolumn_t(const LVCOLUMNW *lpColumn, BOOL isW)
659 char* buf = debug_getbuf(), *text = buf;
660 int len, size = DEBUG_BUFFER_SIZE;
662 if (lpColumn == NULL) return "(null)";
663 len = snprintf(buf, size, "{");
664 if (len == -1) goto end;
665 buf += len; size -= len;
666 if (lpColumn->mask & LVCF_SUBITEM)
667 len = snprintf(buf, size, "iSubItem=%d, ", lpColumn->iSubItem);
668 else len = 0;
669 if (len == -1) goto end;
670 buf += len; size -= len;
671 if (lpColumn->mask & LVCF_FMT)
672 len = snprintf(buf, size, "fmt=%x, ", lpColumn->fmt);
673 else len = 0;
674 if (len == -1) goto end;
675 buf += len; size -= len;
676 if (lpColumn->mask & LVCF_WIDTH)
677 len = snprintf(buf, size, "cx=%d, ", lpColumn->cx);
678 else len = 0;
679 if (len == -1) goto end;
680 buf += len; size -= len;
681 if (lpColumn->mask & LVCF_TEXT)
682 len = snprintf(buf, size, "pszText=%s, cchTextMax=%d, ", debugtext_tn(lpColumn->pszText, isW, 80), lpColumn->cchTextMax);
683 else len = 0;
684 if (len == -1) goto end;
685 buf += len; size -= len;
686 if (lpColumn->mask & LVCF_IMAGE)
687 len = snprintf(buf, size, "iImage=%d, ", lpColumn->iImage);
688 else len = 0;
689 if (len == -1) goto end;
690 buf += len; size -= len;
691 if (lpColumn->mask & LVCF_ORDER)
692 len = snprintf(buf, size, "iOrder=%d, ", lpColumn->iOrder);
693 else len = 0;
694 if (len == -1) goto end;
695 buf += len;
696 goto undo;
697 end:
698 buf = text + strlen(text);
699 undo:
700 if (buf - text > 2) { buf[-2] = '}'; buf[-1] = 0; }
701 return text;
704 static const char* debuglvhittestinfo(const LVHITTESTINFO *lpht)
706 if (!lpht) return "(null)";
708 return wine_dbg_sprintf("{pt=%s, flags=0x%x, iItem=%d, iSubItem=%d}",
709 wine_dbgstr_point(&lpht->pt), lpht->flags, lpht->iItem, lpht->iSubItem);
712 /* Return the corresponding text for a given scroll value */
713 static inline LPCSTR debugscrollcode(int nScrollCode)
715 switch(nScrollCode)
717 case SB_LINELEFT: return "SB_LINELEFT";
718 case SB_LINERIGHT: return "SB_LINERIGHT";
719 case SB_PAGELEFT: return "SB_PAGELEFT";
720 case SB_PAGERIGHT: return "SB_PAGERIGHT";
721 case SB_THUMBPOSITION: return "SB_THUMBPOSITION";
722 case SB_THUMBTRACK: return "SB_THUMBTRACK";
723 case SB_ENDSCROLL: return "SB_ENDSCROLL";
724 case SB_INTERNAL: return "SB_INTERNAL";
725 default: return "unknown";
730 /******** Notification functions ************************************/
732 static int get_ansi_notification(UINT unicodeNotificationCode)
734 switch (unicodeNotificationCode)
736 case LVN_BEGINLABELEDITA:
737 case LVN_BEGINLABELEDITW: return LVN_BEGINLABELEDITA;
738 case LVN_ENDLABELEDITA:
739 case LVN_ENDLABELEDITW: return LVN_ENDLABELEDITA;
740 case LVN_GETDISPINFOA:
741 case LVN_GETDISPINFOW: return LVN_GETDISPINFOA;
742 case LVN_SETDISPINFOA:
743 case LVN_SETDISPINFOW: return LVN_SETDISPINFOA;
744 case LVN_ODFINDITEMA:
745 case LVN_ODFINDITEMW: return LVN_ODFINDITEMA;
746 case LVN_GETINFOTIPA:
747 case LVN_GETINFOTIPW: return LVN_GETINFOTIPA;
748 /* header forwards */
749 case HDN_TRACKA:
750 case HDN_TRACKW: return HDN_TRACKA;
751 case HDN_ENDTRACKA:
752 case HDN_ENDTRACKW: return HDN_ENDTRACKA;
753 case HDN_BEGINDRAG: return HDN_BEGINDRAG;
754 case HDN_ENDDRAG: return HDN_ENDDRAG;
755 case HDN_ITEMCHANGINGA:
756 case HDN_ITEMCHANGINGW: return HDN_ITEMCHANGINGA;
757 case HDN_ITEMCHANGEDA:
758 case HDN_ITEMCHANGEDW: return HDN_ITEMCHANGEDA;
759 case HDN_ITEMCLICKA:
760 case HDN_ITEMCLICKW: return HDN_ITEMCLICKA;
761 case HDN_DIVIDERDBLCLICKA:
762 case HDN_DIVIDERDBLCLICKW: return HDN_DIVIDERDBLCLICKA;
763 default: break;
765 FIXME("unknown notification %x\n", unicodeNotificationCode);
766 return unicodeNotificationCode;
769 /* forwards header notifications to listview parent */
770 static LRESULT notify_forward_header(const LISTVIEW_INFO *infoPtr, NMHEADERW *lpnmhW)
772 LPCWSTR text = NULL, filter = NULL;
773 LRESULT ret;
774 NMHEADERA *lpnmh = (NMHEADERA*) lpnmhW;
776 /* on unicode format exit earlier */
777 if (infoPtr->notifyFormat == NFR_UNICODE)
778 return SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, lpnmh->hdr.idFrom,
779 (LPARAM)lpnmh);
781 /* header always supplies unicode notifications,
782 all we have to do is to convert strings to ANSI */
783 if (lpnmh->pitem)
785 /* convert item text */
786 if (lpnmh->pitem->mask & HDI_TEXT)
788 text = (LPCWSTR)lpnmh->pitem->pszText;
789 lpnmh->pitem->pszText = NULL;
790 Str_SetPtrWtoA(&lpnmh->pitem->pszText, text);
792 /* convert filter text */
793 if ((lpnmh->pitem->mask & HDI_FILTER) && (lpnmh->pitem->type == HDFT_ISSTRING) &&
794 lpnmh->pitem->pvFilter)
796 filter = (LPCWSTR)((HD_TEXTFILTERA*)lpnmh->pitem->pvFilter)->pszText;
797 ((HD_TEXTFILTERA*)lpnmh->pitem->pvFilter)->pszText = NULL;
798 Str_SetPtrWtoA(&((HD_TEXTFILTERA*)lpnmh->pitem->pvFilter)->pszText, filter);
801 lpnmh->hdr.code = get_ansi_notification(lpnmh->hdr.code);
803 ret = SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, lpnmh->hdr.idFrom,
804 (LPARAM)lpnmh);
806 /* cleanup */
807 if(text)
809 Free(lpnmh->pitem->pszText);
810 lpnmh->pitem->pszText = (LPSTR)text;
812 if(filter)
814 Free(((HD_TEXTFILTERA*)lpnmh->pitem->pvFilter)->pszText);
815 ((HD_TEXTFILTERA*)lpnmh->pitem->pvFilter)->pszText = (LPSTR)filter;
818 return ret;
821 static LRESULT notify_hdr(const LISTVIEW_INFO *infoPtr, INT code, LPNMHDR pnmh)
823 LRESULT result;
825 TRACE("(code=%d)\n", code);
827 pnmh->hwndFrom = infoPtr->hwndSelf;
828 pnmh->idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
829 pnmh->code = code;
830 result = SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, pnmh->idFrom, (LPARAM)pnmh);
832 TRACE(" <= %Id\n", result);
834 return result;
837 static inline BOOL notify(const LISTVIEW_INFO *infoPtr, INT code)
839 NMHDR nmh;
840 HWND hwnd = infoPtr->hwndSelf;
841 notify_hdr(infoPtr, code, &nmh);
842 return IsWindow(hwnd);
845 static inline void notify_itemactivate(const LISTVIEW_INFO *infoPtr, const LVHITTESTINFO *htInfo)
847 NMITEMACTIVATE nmia;
848 LVITEMW item;
850 nmia.uNewState = 0;
851 nmia.uOldState = 0;
852 nmia.uChanged = 0;
853 nmia.uKeyFlags = 0;
855 item.mask = LVIF_PARAM|LVIF_STATE;
856 item.iItem = htInfo->iItem;
857 item.iSubItem = 0;
858 item.stateMask = (UINT)-1;
859 if (LISTVIEW_GetItemT(infoPtr, &item, TRUE)) {
860 nmia.lParam = item.lParam;
861 nmia.uOldState = item.state;
862 nmia.uNewState = item.state | LVIS_ACTIVATING;
863 nmia.uChanged = LVIF_STATE;
866 nmia.iItem = htInfo->iItem;
867 nmia.iSubItem = htInfo->iSubItem;
868 nmia.ptAction = htInfo->pt;
870 if (GetKeyState(VK_SHIFT) & 0x8000) nmia.uKeyFlags |= LVKF_SHIFT;
871 if (GetKeyState(VK_CONTROL) & 0x8000) nmia.uKeyFlags |= LVKF_CONTROL;
872 if (GetKeyState(VK_MENU) & 0x8000) nmia.uKeyFlags |= LVKF_ALT;
874 notify_hdr(infoPtr, LVN_ITEMACTIVATE, (LPNMHDR)&nmia);
877 static inline LRESULT notify_listview(const LISTVIEW_INFO *infoPtr, INT code, LPNMLISTVIEW plvnm)
879 TRACE("(code=%d, plvnm=%s)\n", code, debugnmlistview(plvnm));
880 return notify_hdr(infoPtr, code, (LPNMHDR)plvnm);
883 /* Handles NM_DBLCLK, NM_CLICK, NM_RDBLCLK, NM_RCLICK. Only NM_RCLICK return value is used. */
884 static BOOL notify_click(const LISTVIEW_INFO *infoPtr, INT code, const LVHITTESTINFO *lvht)
886 NMITEMACTIVATE nmia;
887 LVITEMW item;
888 HWND hwnd = infoPtr->hwndSelf;
889 LRESULT ret;
891 TRACE("code=%d, lvht=%s\n", code, debuglvhittestinfo(lvht));
892 ZeroMemory(&nmia, sizeof(nmia));
893 nmia.iItem = lvht->iItem;
894 nmia.iSubItem = lvht->iSubItem;
895 nmia.ptAction = lvht->pt;
896 item.mask = LVIF_PARAM;
897 item.iItem = lvht->iItem;
898 item.iSubItem = 0;
899 if (LISTVIEW_GetItemT(infoPtr, &item, TRUE)) nmia.lParam = item.lParam;
900 ret = notify_hdr(infoPtr, code, (NMHDR*)&nmia);
901 return IsWindow(hwnd) && (code == NM_RCLICK ? !ret : TRUE);
904 static BOOL notify_deleteitem(const LISTVIEW_INFO *infoPtr, INT nItem)
906 NMLISTVIEW nmlv;
907 LVITEMW item;
908 HWND hwnd = infoPtr->hwndSelf;
910 ZeroMemory(&nmlv, sizeof (NMLISTVIEW));
911 nmlv.iItem = nItem;
912 item.mask = LVIF_PARAM;
913 item.iItem = nItem;
914 item.iSubItem = 0;
915 if (LISTVIEW_GetItemT(infoPtr, &item, TRUE)) nmlv.lParam = item.lParam;
916 notify_listview(infoPtr, LVN_DELETEITEM, &nmlv);
917 return IsWindow(hwnd);
921 Send notification. depends on dispinfoW having same
922 structure as dispinfoA.
923 infoPtr : listview struct
924 code : *Unicode* notification code
925 pdi : dispinfo structure (can be unicode or ansi)
926 isW : TRUE if dispinfo is Unicode
928 static BOOL notify_dispinfoT(const LISTVIEW_INFO *infoPtr, UINT code, LPNMLVDISPINFOW pdi, BOOL isW)
930 INT length = 0, ret_length;
931 LPWSTR buffer = NULL, ret_text;
932 BOOL return_ansi = FALSE;
933 BOOL return_unicode = FALSE;
934 BOOL ret;
936 if ((pdi->item.mask & LVIF_TEXT) && is_text(pdi->item.pszText))
938 return_unicode = ( isW && infoPtr->notifyFormat == NFR_ANSI);
939 return_ansi = (!isW && infoPtr->notifyFormat == NFR_UNICODE);
942 ret_length = pdi->item.cchTextMax;
943 ret_text = pdi->item.pszText;
945 if (return_unicode || return_ansi)
947 if (code != LVN_GETDISPINFOW)
949 length = return_ansi ?
950 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pdi->item.pszText, -1, NULL, 0):
951 WideCharToMultiByte(CP_ACP, 0, pdi->item.pszText, -1, NULL, 0, NULL, NULL);
953 else
955 length = pdi->item.cchTextMax;
956 *pdi->item.pszText = 0; /* make sure we don't process garbage */
959 buffer = Alloc( (return_ansi ? sizeof(WCHAR) : sizeof(CHAR)) * length);
960 if (!buffer) return FALSE;
962 if (return_ansi)
963 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pdi->item.pszText, -1,
964 buffer, length);
965 else
966 WideCharToMultiByte(CP_ACP, 0, pdi->item.pszText, -1, (LPSTR) buffer,
967 length, NULL, NULL);
969 pdi->item.pszText = buffer;
970 pdi->item.cchTextMax = length;
973 if (infoPtr->notifyFormat == NFR_ANSI)
974 code = get_ansi_notification(code);
976 TRACE(" pdi->item=%s\n", debuglvitem_t(&pdi->item, infoPtr->notifyFormat != NFR_ANSI));
977 ret = notify_hdr(infoPtr, code, &pdi->hdr);
978 TRACE(" resulting code=%d\n", pdi->hdr.code);
980 if (return_ansi || return_unicode)
982 if (return_ansi && (pdi->hdr.code == LVN_GETDISPINFOA))
984 strcpy((char*)ret_text, (char*)pdi->item.pszText);
986 else if (return_unicode && (pdi->hdr.code == LVN_GETDISPINFOW))
988 lstrcpyW(ret_text, pdi->item.pszText);
990 else if (return_ansi) /* note : pointer can be changed by app ! */
992 WideCharToMultiByte(CP_ACP, 0, pdi->item.pszText, -1, (LPSTR) ret_text,
993 ret_length, NULL, NULL);
995 else
996 MultiByteToWideChar(CP_ACP, 0, (LPSTR) pdi->item.pszText, -1,
997 ret_text, ret_length);
999 pdi->item.pszText = ret_text; /* restores our buffer */
1000 pdi->item.cchTextMax = ret_length;
1002 Free(buffer);
1003 return ret;
1006 /* if dispinfo holder changed notification code then convert */
1007 if (!isW && (pdi->hdr.code == LVN_GETDISPINFOW) && (pdi->item.mask & LVIF_TEXT))
1009 length = WideCharToMultiByte(CP_ACP, 0, pdi->item.pszText, -1, NULL, 0, NULL, NULL);
1011 buffer = Alloc(length * sizeof(CHAR));
1012 if (!buffer) return FALSE;
1014 WideCharToMultiByte(CP_ACP, 0, pdi->item.pszText, -1, (LPSTR) buffer,
1015 ret_length, NULL, NULL);
1017 strcpy((LPSTR)pdi->item.pszText, (LPSTR)buffer);
1018 Free(buffer);
1021 return ret;
1024 static void customdraw_fill(NMLVCUSTOMDRAW *lpnmlvcd, const LISTVIEW_INFO *infoPtr, HDC hdc,
1025 const RECT *rcBounds, const LVITEMW *lplvItem)
1027 ZeroMemory(lpnmlvcd, sizeof(NMLVCUSTOMDRAW));
1028 lpnmlvcd->nmcd.hdc = hdc;
1029 lpnmlvcd->nmcd.rc = *rcBounds;
1030 lpnmlvcd->clrTextBk = infoPtr->clrTextBk;
1031 lpnmlvcd->clrText = infoPtr->clrText;
1032 if (!lplvItem) return;
1033 lpnmlvcd->nmcd.dwItemSpec = lplvItem->iItem + 1;
1034 lpnmlvcd->iSubItem = lplvItem->iSubItem;
1035 if (lplvItem->state & LVIS_SELECTED) lpnmlvcd->nmcd.uItemState |= CDIS_SELECTED;
1036 if (lplvItem->state & LVIS_FOCUSED) lpnmlvcd->nmcd.uItemState |= CDIS_FOCUS;
1037 if (lplvItem->iItem == infoPtr->nHotItem) lpnmlvcd->nmcd.uItemState |= CDIS_HOT;
1038 lpnmlvcd->nmcd.lItemlParam = lplvItem->lParam;
1041 static inline DWORD notify_customdraw (const LISTVIEW_INFO *infoPtr, DWORD dwDrawStage, NMLVCUSTOMDRAW *lpnmlvcd)
1043 BOOL isForItem = (lpnmlvcd->nmcd.dwItemSpec != 0);
1044 DWORD result;
1046 lpnmlvcd->nmcd.dwDrawStage = dwDrawStage;
1047 if (isForItem) lpnmlvcd->nmcd.dwDrawStage |= CDDS_ITEM;
1048 if (lpnmlvcd->iSubItem) lpnmlvcd->nmcd.dwDrawStage |= CDDS_SUBITEM;
1049 if (isForItem) lpnmlvcd->nmcd.dwItemSpec--;
1050 result = notify_hdr(infoPtr, NM_CUSTOMDRAW, &lpnmlvcd->nmcd.hdr);
1051 if (isForItem) lpnmlvcd->nmcd.dwItemSpec++;
1052 return result;
1055 static void prepaint_setup (const LISTVIEW_INFO *infoPtr, HDC hdc, const NMLVCUSTOMDRAW *cd, BOOL SubItem)
1057 COLORREF backcolor, textcolor;
1059 backcolor = cd->clrTextBk;
1060 textcolor = cd->clrText;
1062 /* apparently, for selected items, we have to override the returned values */
1063 if (!SubItem)
1065 if (cd->nmcd.uItemState & CDIS_SELECTED)
1067 if (infoPtr->bFocus)
1069 backcolor = comctl32_color.clrHighlight;
1070 textcolor = comctl32_color.clrHighlightText;
1072 else if (infoPtr->dwStyle & LVS_SHOWSELALWAYS)
1074 backcolor = comctl32_color.clr3dFace;
1075 textcolor = comctl32_color.clrBtnText;
1080 if (backcolor == CLR_DEFAULT)
1081 backcolor = comctl32_color.clrWindow;
1082 if (textcolor == CLR_DEFAULT)
1083 textcolor = comctl32_color.clrWindowText;
1085 /* Set the text attributes */
1086 if (backcolor != CLR_NONE)
1088 SetBkMode(hdc, OPAQUE);
1089 SetBkColor(hdc, backcolor);
1091 else
1092 SetBkMode(hdc, TRANSPARENT);
1093 SetTextColor(hdc, textcolor);
1096 static inline DWORD notify_postpaint (const LISTVIEW_INFO *infoPtr, NMLVCUSTOMDRAW *lpnmlvcd)
1098 return notify_customdraw(infoPtr, CDDS_POSTPAINT, lpnmlvcd);
1101 /* returns TRUE when repaint needed, FALSE otherwise */
1102 static BOOL notify_measureitem(LISTVIEW_INFO *infoPtr)
1104 MEASUREITEMSTRUCT mis;
1105 mis.CtlType = ODT_LISTVIEW;
1106 mis.CtlID = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
1107 mis.itemID = -1;
1108 mis.itemWidth = 0;
1109 mis.itemData = 0;
1110 mis.itemHeight= infoPtr->nItemHeight;
1111 SendMessageW(infoPtr->hwndNotify, WM_MEASUREITEM, mis.CtlID, (LPARAM)&mis);
1112 if (infoPtr->nItemHeight != max(mis.itemHeight, 1))
1114 infoPtr->nMeasureItemHeight = infoPtr->nItemHeight = max(mis.itemHeight, 1);
1115 return TRUE;
1117 return FALSE;
1120 /******** Item iterator functions **********************************/
1122 static RANGES ranges_create(int count);
1123 static void ranges_destroy(RANGES ranges);
1124 static BOOL ranges_add(RANGES ranges, RANGE range);
1125 static BOOL ranges_del(RANGES ranges, RANGE range);
1126 static void ranges_dump(RANGES ranges);
1128 static inline BOOL ranges_additem(RANGES ranges, INT nItem)
1130 RANGE range = { nItem, nItem + 1 };
1132 return ranges_add(ranges, range);
1135 static inline BOOL ranges_delitem(RANGES ranges, INT nItem)
1137 RANGE range = { nItem, nItem + 1 };
1139 return ranges_del(ranges, range);
1142 /***
1143 * ITERATOR DOCUMENTATION
1145 * The iterator functions allow for easy, and convenient iteration
1146 * over items of interest in the list. Typically, you create an
1147 * iterator, use it, and destroy it, as such:
1148 * ITERATOR i;
1150 * iterator_xxxitems(&i, ...);
1151 * while (iterator_{prev,next}(&i)
1153 * //code which uses i.nItem
1155 * iterator_destroy(&i);
1157 * where xxx is either: framed, or visible.
1158 * Note that it is important that the code destroys the iterator
1159 * after it's done with it, as the creation of the iterator may
1160 * allocate memory, which thus needs to be freed.
1162 * You can iterate both forwards, and backwards through the list,
1163 * by using iterator_next or iterator_prev respectively.
1165 * Lower numbered items are draw on top of higher number items in
1166 * LVS_ICON, and LVS_SMALLICON (which are the only modes where
1167 * items may overlap). So, to test items, you should use
1168 * iterator_next
1169 * which lists the items top to bottom (in Z-order).
1170 * For drawing items, you should use
1171 * iterator_prev
1172 * which lists the items bottom to top (in Z-order).
1173 * If you keep iterating over the items after the end-of-items
1174 * marker (-1) is returned, the iterator will start from the
1175 * beginning. Typically, you don't need to test for -1,
1176 * because iterator_{next,prev} will return TRUE if more items
1177 * are to be iterated over, or FALSE otherwise.
1179 * Note: the iterator is defined to be bidirectional. That is,
1180 * any number of prev followed by any number of next, or
1181 * five versa, should leave the iterator at the same item:
1182 * prev * n, next * n = next * n, prev * n
1184 * The iterator has a notion of an out-of-order, special item,
1185 * which sits at the start of the list. This is used in
1186 * LVS_ICON, and LVS_SMALLICON mode to handle the focused item,
1187 * which needs to be first, as it may overlap other items.
1189 * The code is a bit messy because we have:
1190 * - a special item to deal with
1191 * - simple range, or composite range
1192 * - empty range.
1193 * If you find bugs, or want to add features, please make sure you
1194 * always check/modify *both* iterator_prev, and iterator_next.
1197 /****
1198 * This function iterates through the items in increasing order,
1199 * but prefixed by the special item, then -1. That is:
1200 * special, 1, 2, 3, ..., n, -1.
1201 * Each item is listed only once.
1203 static inline BOOL iterator_next(ITERATOR* i)
1205 if (i->nItem == -1)
1207 i->nItem = i->nSpecial;
1208 if (i->nItem != -1) return TRUE;
1210 if (i->nItem == i->nSpecial)
1212 if (i->ranges) i->index = 0;
1213 goto pickarange;
1216 i->nItem++;
1217 testitem:
1218 if (i->nItem == i->nSpecial) i->nItem++;
1219 if (i->nItem < i->range.upper) return TRUE;
1221 pickarange:
1222 if (i->ranges)
1224 if (i->index < DPA_GetPtrCount(i->ranges->hdpa))
1225 i->range = *(RANGE*)DPA_GetPtr(i->ranges->hdpa, i->index++);
1226 else goto end;
1228 else if (i->nItem >= i->range.upper) goto end;
1230 i->nItem = i->range.lower;
1231 if (i->nItem >= 0) goto testitem;
1232 end:
1233 i->nItem = -1;
1234 return FALSE;
1237 /****
1238 * This function iterates through the items in decreasing order,
1239 * followed by the special item, then -1. That is:
1240 * n, n-1, ..., 3, 2, 1, special, -1.
1241 * Each item is listed only once.
1243 static inline BOOL iterator_prev(ITERATOR* i)
1245 BOOL start = FALSE;
1247 if (i->nItem == -1)
1249 start = TRUE;
1250 if (i->ranges) i->index = DPA_GetPtrCount(i->ranges->hdpa);
1251 goto pickarange;
1253 if (i->nItem == i->nSpecial)
1255 i->nItem = -1;
1256 return FALSE;
1259 testitem:
1260 i->nItem--;
1261 if (i->nItem == i->nSpecial) i->nItem--;
1262 if (i->nItem >= i->range.lower) return TRUE;
1264 pickarange:
1265 if (i->ranges)
1267 if (i->index > 0)
1268 i->range = *(RANGE*)DPA_GetPtr(i->ranges->hdpa, --i->index);
1269 else goto end;
1271 else if (!start && i->nItem < i->range.lower) goto end;
1273 i->nItem = i->range.upper;
1274 if (i->nItem > 0) goto testitem;
1275 end:
1276 return (i->nItem = i->nSpecial) != -1;
1279 static RANGE iterator_range(const ITERATOR *i)
1281 RANGE range;
1283 if (!i->ranges) return i->range;
1285 if (DPA_GetPtrCount(i->ranges->hdpa) > 0)
1287 range.lower = (*(RANGE*)DPA_GetPtr(i->ranges->hdpa, 0)).lower;
1288 range.upper = (*(RANGE*)DPA_GetPtr(i->ranges->hdpa, DPA_GetPtrCount(i->ranges->hdpa) - 1)).upper;
1290 else range.lower = range.upper = 0;
1292 return range;
1295 /***
1296 * Releases resources associated with this iterator.
1298 static inline void iterator_destroy(const ITERATOR *i)
1300 ranges_destroy(i->ranges);
1303 /***
1304 * Create an empty iterator.
1306 static inline void iterator_empty(ITERATOR* i)
1308 ZeroMemory(i, sizeof(*i));
1309 i->nItem = i->nSpecial = i->range.lower = i->range.upper = -1;
1312 /***
1313 * Create an iterator over a range.
1315 static inline void iterator_rangeitems(ITERATOR* i, RANGE range)
1317 iterator_empty(i);
1318 i->range = range;
1321 /***
1322 * Create an iterator over a bunch of ranges.
1323 * Please note that the iterator will take ownership of the ranges,
1324 * and will free them upon destruction.
1326 static inline void iterator_rangesitems(ITERATOR* i, RANGES ranges)
1328 iterator_empty(i);
1329 i->ranges = ranges;
1332 /***
1333 * Creates an iterator over the items which intersect frame.
1334 * Uses absolute coordinates rather than compensating for the current offset.
1336 static BOOL iterator_frameditems_absolute(ITERATOR* i, const LISTVIEW_INFO* infoPtr, const RECT *frame)
1338 RECT rcItem, rcTemp;
1339 RANGES ranges;
1341 TRACE("(frame=%s)\n", wine_dbgstr_rect(frame));
1343 /* in case we fail, we want to return an empty iterator */
1344 iterator_empty(i);
1346 if (infoPtr->nItemCount == 0)
1347 return TRUE;
1349 if (infoPtr->uView == LV_VIEW_ICON || infoPtr->uView == LV_VIEW_SMALLICON)
1351 INT nItem;
1353 if (infoPtr->uView == LV_VIEW_ICON && infoPtr->nFocusedItem != -1)
1355 LISTVIEW_GetItemBox(infoPtr, infoPtr->nFocusedItem, &rcItem);
1356 if (IntersectRect(&rcTemp, &rcItem, frame))
1357 i->nSpecial = infoPtr->nFocusedItem;
1359 if (!(ranges = ranges_create(50))) return FALSE;
1360 iterator_rangesitems(i, ranges);
1361 /* to do better here, we need to have PosX, and PosY sorted */
1362 TRACE("building icon ranges:\n");
1363 for (nItem = 0; nItem < infoPtr->nItemCount; nItem++)
1365 rcItem.left = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosX, nItem);
1366 rcItem.top = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosY, nItem);
1367 rcItem.right = rcItem.left + infoPtr->nItemWidth;
1368 rcItem.bottom = rcItem.top + infoPtr->nItemHeight;
1369 if (IntersectRect(&rcTemp, &rcItem, frame))
1370 ranges_additem(i->ranges, nItem);
1372 return TRUE;
1374 else if (infoPtr->uView == LV_VIEW_DETAILS)
1376 RANGE range;
1378 if (frame->left >= infoPtr->nItemWidth) return TRUE;
1379 if (frame->top >= infoPtr->nItemHeight * infoPtr->nItemCount) return TRUE;
1381 range.lower = max(frame->top / infoPtr->nItemHeight, 0);
1382 range.upper = min((frame->bottom - 1) / infoPtr->nItemHeight, infoPtr->nItemCount - 1) + 1;
1383 if (range.upper <= range.lower) return TRUE;
1384 iterator_rangeitems(i, range);
1385 TRACE(" report=%s\n", debugrange(&i->range));
1387 else
1389 INT nPerCol = max((infoPtr->rcList.bottom - infoPtr->rcList.top) / infoPtr->nItemHeight, 1);
1390 INT nFirstRow = max(frame->top / infoPtr->nItemHeight, 0);
1391 INT nLastRow = min((frame->bottom - 1) / infoPtr->nItemHeight, nPerCol - 1);
1392 INT nFirstCol;
1393 INT nLastCol;
1394 INT lower;
1395 RANGE item_range;
1396 INT nCol;
1398 if (infoPtr->nItemWidth)
1400 nFirstCol = max(frame->left / infoPtr->nItemWidth, 0);
1401 nLastCol = min((frame->right - 1) / infoPtr->nItemWidth, (infoPtr->nItemCount + nPerCol - 1) / nPerCol);
1403 else
1405 nFirstCol = max(frame->left, 0);
1406 nLastCol = min(frame->right - 1, (infoPtr->nItemCount + nPerCol - 1) / nPerCol);
1409 lower = nFirstCol * nPerCol + nFirstRow;
1411 TRACE("nPerCol=%d, nFirstRow=%d, nLastRow=%d, nFirstCol=%d, nLastCol=%d, lower=%d\n",
1412 nPerCol, nFirstRow, nLastRow, nFirstCol, nLastCol, lower);
1414 if (nLastCol < nFirstCol || nLastRow < nFirstRow) return TRUE;
1416 if (!(ranges = ranges_create(nLastCol - nFirstCol + 1))) return FALSE;
1417 iterator_rangesitems(i, ranges);
1418 TRACE("building list ranges:\n");
1419 for (nCol = nFirstCol; nCol <= nLastCol; nCol++)
1421 item_range.lower = nCol * nPerCol + nFirstRow;
1422 if(item_range.lower >= infoPtr->nItemCount) break;
1423 item_range.upper = min(nCol * nPerCol + nLastRow + 1, infoPtr->nItemCount);
1424 TRACE(" list=%s\n", debugrange(&item_range));
1425 ranges_add(i->ranges, item_range);
1429 return TRUE;
1432 /***
1433 * Creates an iterator over the items which intersect lprc.
1435 static BOOL iterator_frameditems(ITERATOR* i, const LISTVIEW_INFO* infoPtr, const RECT *lprc)
1437 RECT frame = *lprc;
1438 POINT Origin;
1440 TRACE("(lprc=%s)\n", wine_dbgstr_rect(lprc));
1442 LISTVIEW_GetOrigin(infoPtr, &Origin);
1443 OffsetRect(&frame, -Origin.x, -Origin.y);
1445 return iterator_frameditems_absolute(i, infoPtr, &frame);
1448 /***
1449 * Creates an iterator over the items which intersect the visible region of hdc.
1451 static BOOL iterator_visibleitems(ITERATOR *i, const LISTVIEW_INFO *infoPtr, HDC hdc)
1453 POINT Origin, Position;
1454 RECT rcItem, rcClip;
1455 INT rgntype;
1457 rgntype = GetClipBox(hdc, &rcClip);
1458 if (rgntype == NULLREGION)
1460 iterator_empty(i);
1461 return TRUE;
1463 if (!iterator_frameditems(i, infoPtr, &rcClip)) return FALSE;
1464 if (rgntype == SIMPLEREGION) return TRUE;
1466 /* first deal with the special item */
1467 if (i->nSpecial != -1)
1469 LISTVIEW_GetItemBox(infoPtr, i->nSpecial, &rcItem);
1470 if (!RectVisible(hdc, &rcItem)) i->nSpecial = -1;
1473 /* if we can't deal with the region, we'll just go with the simple range */
1474 LISTVIEW_GetOrigin(infoPtr, &Origin);
1475 TRACE("building visible range:\n");
1476 if (!i->ranges && i->range.lower < i->range.upper)
1478 if (!(i->ranges = ranges_create(50))) return TRUE;
1479 if (!ranges_add(i->ranges, i->range))
1481 ranges_destroy(i->ranges);
1482 i->ranges = 0;
1483 return TRUE;
1487 /* now delete the invisible items from the list */
1488 while(iterator_next(i))
1490 LISTVIEW_GetItemOrigin(infoPtr, i->nItem, &Position);
1491 rcItem.left = (infoPtr->uView == LV_VIEW_DETAILS) ? Origin.x : Position.x + Origin.x;
1492 rcItem.top = Position.y + Origin.y;
1493 rcItem.right = rcItem.left + infoPtr->nItemWidth;
1494 rcItem.bottom = rcItem.top + infoPtr->nItemHeight;
1495 if (!RectVisible(hdc, &rcItem))
1496 ranges_delitem(i->ranges, i->nItem);
1498 /* the iterator should restart on the next iterator_next */
1499 TRACE("done\n");
1501 return TRUE;
1504 /* Remove common elements from two iterators */
1505 /* Passed iterators have to point on the first elements */
1506 static BOOL iterator_remove_common_items(ITERATOR *iter1, ITERATOR *iter2)
1508 if(!iter1->ranges || !iter2->ranges) {
1509 int lower, upper;
1511 if(iter1->ranges || iter2->ranges ||
1512 (iter1->range.lower<iter2->range.lower && iter1->range.upper>iter2->range.upper) ||
1513 (iter1->range.lower>iter2->range.lower && iter1->range.upper<iter2->range.upper)) {
1514 ERR("result is not a one range iterator\n");
1515 return FALSE;
1518 if(iter1->range.lower==-1 || iter2->range.lower==-1)
1519 return TRUE;
1521 lower = iter1->range.lower;
1522 upper = iter1->range.upper;
1524 if(lower < iter2->range.lower)
1525 iter1->range.upper = iter2->range.lower;
1526 else if(upper > iter2->range.upper)
1527 iter1->range.lower = iter2->range.upper;
1528 else
1529 iter1->range.lower = iter1->range.upper = -1;
1531 if(iter2->range.lower < lower)
1532 iter2->range.upper = lower;
1533 else if(iter2->range.upper > upper)
1534 iter2->range.lower = upper;
1535 else
1536 iter2->range.lower = iter2->range.upper = -1;
1538 return TRUE;
1541 iterator_next(iter1);
1542 iterator_next(iter2);
1544 while(1) {
1545 if(iter1->nItem==-1 || iter2->nItem==-1)
1546 break;
1548 if(iter1->nItem == iter2->nItem) {
1549 int delete = iter1->nItem;
1551 iterator_prev(iter1);
1552 iterator_prev(iter2);
1553 ranges_delitem(iter1->ranges, delete);
1554 ranges_delitem(iter2->ranges, delete);
1555 iterator_next(iter1);
1556 iterator_next(iter2);
1557 } else if(iter1->nItem > iter2->nItem)
1558 iterator_next(iter2);
1559 else
1560 iterator_next(iter1);
1563 iter1->nItem = iter1->range.lower = iter1->range.upper = -1;
1564 iter2->nItem = iter2->range.lower = iter2->range.upper = -1;
1565 return TRUE;
1568 /******** Misc helper functions ************************************/
1570 static inline LRESULT CallWindowProcT(WNDPROC proc, HWND hwnd, UINT uMsg,
1571 WPARAM wParam, LPARAM lParam, BOOL isW)
1573 if (isW) return CallWindowProcW(proc, hwnd, uMsg, wParam, lParam);
1574 else return CallWindowProcA(proc, hwnd, uMsg, wParam, lParam);
1577 static inline BOOL is_autoarrange(const LISTVIEW_INFO *infoPtr)
1579 return (infoPtr->dwStyle & LVS_AUTOARRANGE) &&
1580 (infoPtr->uView == LV_VIEW_ICON || infoPtr->uView == LV_VIEW_SMALLICON);
1583 static void toggle_checkbox_state(LISTVIEW_INFO *infoPtr, INT nItem)
1585 DWORD state = STATEIMAGEINDEX(LISTVIEW_GetItemState(infoPtr, nItem, LVIS_STATEIMAGEMASK));
1586 if(state == 1 || state == 2)
1588 LVITEMW lvitem;
1589 state ^= 3;
1590 lvitem.state = INDEXTOSTATEIMAGEMASK(state);
1591 lvitem.stateMask = LVIS_STATEIMAGEMASK;
1592 LISTVIEW_SetItemState(infoPtr, nItem, &lvitem);
1596 /* this should be called after window style got updated,
1597 it used to reset view state to match current window style */
1598 static inline void map_style_view(LISTVIEW_INFO *infoPtr)
1600 switch (infoPtr->dwStyle & LVS_TYPEMASK)
1602 case LVS_ICON:
1603 infoPtr->uView = LV_VIEW_ICON;
1604 break;
1605 case LVS_REPORT:
1606 infoPtr->uView = LV_VIEW_DETAILS;
1607 break;
1608 case LVS_SMALLICON:
1609 infoPtr->uView = LV_VIEW_SMALLICON;
1610 break;
1611 case LVS_LIST:
1612 infoPtr->uView = LV_VIEW_LIST;
1616 /* computes next item id value */
1617 static DWORD get_next_itemid(const LISTVIEW_INFO *infoPtr)
1619 INT count = DPA_GetPtrCount(infoPtr->hdpaItemIds);
1621 if (count > 0)
1623 ITEM_ID *lpID = DPA_GetPtr(infoPtr->hdpaItemIds, count - 1);
1624 return lpID->id + 1;
1626 return 0;
1629 /******** Internal API functions ************************************/
1631 static inline COLUMN_INFO * LISTVIEW_GetColumnInfo(const LISTVIEW_INFO *infoPtr, INT nSubItem)
1633 static COLUMN_INFO mainItem;
1635 if (nSubItem == 0 && DPA_GetPtrCount(infoPtr->hdpaColumns) == 0) return &mainItem;
1636 assert (nSubItem >= 0 && nSubItem < DPA_GetPtrCount(infoPtr->hdpaColumns));
1638 /* update cached column rectangles */
1639 if (infoPtr->colRectsDirty)
1641 COLUMN_INFO *info;
1642 LISTVIEW_INFO *Ptr = (LISTVIEW_INFO*)infoPtr;
1643 INT i;
1645 for (i = 0; i < DPA_GetPtrCount(infoPtr->hdpaColumns); i++) {
1646 info = DPA_GetPtr(infoPtr->hdpaColumns, i);
1647 SendMessageW(infoPtr->hwndHeader, HDM_GETITEMRECT, i, (LPARAM)&info->rcHeader);
1649 Ptr->colRectsDirty = FALSE;
1652 return DPA_GetPtr(infoPtr->hdpaColumns, nSubItem);
1655 static INT LISTVIEW_CreateHeader(LISTVIEW_INFO *infoPtr)
1657 DWORD dFlags = WS_CHILD | HDS_HORZ | HDS_FULLDRAG | HDS_DRAGDROP;
1658 HINSTANCE hInst;
1660 if (infoPtr->hwndHeader) return 0;
1662 TRACE("Creating header for list %p\n", infoPtr->hwndSelf);
1664 /* setup creation flags */
1665 dFlags |= (LVS_NOSORTHEADER & infoPtr->dwStyle) ? 0 : HDS_BUTTONS;
1666 dFlags |= (LVS_NOCOLUMNHEADER & infoPtr->dwStyle) ? HDS_HIDDEN : 0;
1668 hInst = (HINSTANCE)GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_HINSTANCE);
1670 /* create header */
1671 infoPtr->hwndHeader = CreateWindowW(WC_HEADERW, NULL, dFlags,
1672 0, 0, 0, 0, infoPtr->hwndSelf, NULL, hInst, NULL);
1673 if (!infoPtr->hwndHeader) return -1;
1675 /* set header unicode format */
1676 SendMessageW(infoPtr->hwndHeader, HDM_SETUNICODEFORMAT, TRUE, 0);
1678 /* set header font */
1679 SendMessageW(infoPtr->hwndHeader, WM_SETFONT, (WPARAM)infoPtr->hFont, TRUE);
1681 /* set header image list */
1682 if (infoPtr->himlSmall)
1683 SendMessageW(infoPtr->hwndHeader, HDM_SETIMAGELIST, 0, (LPARAM)infoPtr->himlSmall);
1685 LISTVIEW_UpdateSize(infoPtr);
1687 return 0;
1690 static inline void LISTVIEW_GetHeaderRect(const LISTVIEW_INFO *infoPtr, INT nSubItem, LPRECT lprc)
1692 *lprc = LISTVIEW_GetColumnInfo(infoPtr, nSubItem)->rcHeader;
1695 static inline BOOL LISTVIEW_IsHeaderEnabled(const LISTVIEW_INFO *infoPtr)
1697 return (infoPtr->uView == LV_VIEW_DETAILS ||
1698 infoPtr->dwLvExStyle & LVS_EX_HEADERINALLVIEWS) &&
1699 !(infoPtr->dwStyle & LVS_NOCOLUMNHEADER);
1702 static inline BOOL LISTVIEW_GetItemW(const LISTVIEW_INFO *infoPtr, LPLVITEMW lpLVItem)
1704 return LISTVIEW_GetItemT(infoPtr, lpLVItem, TRUE);
1707 /* used to handle collapse main item column case */
1708 static inline BOOL LISTVIEW_DrawFocusRect(const LISTVIEW_INFO *infoPtr, HDC hdc)
1710 return (infoPtr->rcFocus.left < infoPtr->rcFocus.right) ?
1711 DrawFocusRect(hdc, &infoPtr->rcFocus) : FALSE;
1714 /* Listview invalidation functions: use _only_ these functions to invalidate */
1716 static inline BOOL is_redrawing(const LISTVIEW_INFO *infoPtr)
1718 return infoPtr->redraw;
1721 static inline void LISTVIEW_InvalidateRect(const LISTVIEW_INFO *infoPtr, const RECT* rect)
1723 if(!is_redrawing(infoPtr)) return;
1724 TRACE(" invalidating rect=%s\n", wine_dbgstr_rect(rect));
1725 InvalidateRect(infoPtr->hwndSelf, rect, TRUE);
1728 static inline void LISTVIEW_InvalidateItem(const LISTVIEW_INFO *infoPtr, INT nItem)
1730 RECT rcBox;
1732 if (!is_redrawing(infoPtr) || nItem < 0 || nItem >= infoPtr->nItemCount)
1733 return;
1735 LISTVIEW_GetItemBox(infoPtr, nItem, &rcBox);
1736 LISTVIEW_InvalidateRect(infoPtr, &rcBox);
1739 static inline void LISTVIEW_InvalidateSubItem(const LISTVIEW_INFO *infoPtr, INT nItem, INT nSubItem)
1741 POINT Origin, Position;
1742 RECT rcBox;
1744 if(!is_redrawing(infoPtr)) return;
1745 assert (infoPtr->uView == LV_VIEW_DETAILS);
1746 LISTVIEW_GetOrigin(infoPtr, &Origin);
1747 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position);
1748 LISTVIEW_GetHeaderRect(infoPtr, nSubItem, &rcBox);
1749 rcBox.top = 0;
1750 rcBox.bottom = infoPtr->nItemHeight;
1751 OffsetRect(&rcBox, Origin.x, Origin.y + Position.y);
1752 LISTVIEW_InvalidateRect(infoPtr, &rcBox);
1755 static inline void LISTVIEW_InvalidateList(const LISTVIEW_INFO *infoPtr)
1757 LISTVIEW_InvalidateRect(infoPtr, NULL);
1760 static inline void LISTVIEW_InvalidateColumn(const LISTVIEW_INFO *infoPtr, INT nColumn)
1762 RECT rcCol;
1764 if(!is_redrawing(infoPtr)) return;
1765 LISTVIEW_GetHeaderRect(infoPtr, nColumn, &rcCol);
1766 rcCol.top = infoPtr->rcList.top;
1767 rcCol.bottom = infoPtr->rcList.bottom;
1768 LISTVIEW_InvalidateRect(infoPtr, &rcCol);
1771 /***
1772 * DESCRIPTION:
1773 * Retrieves the number of items that can fit vertically in the client area.
1775 * PARAMETER(S):
1776 * [I] infoPtr : valid pointer to the listview structure
1778 * RETURN:
1779 * Number of items per row.
1781 static inline INT LISTVIEW_GetCountPerRow(const LISTVIEW_INFO *infoPtr)
1783 INT nListWidth = infoPtr->rcList.right - infoPtr->rcList.left;
1785 return max(nListWidth/(infoPtr->nItemWidth ? infoPtr->nItemWidth : 1), 1);
1788 /***
1789 * DESCRIPTION:
1790 * Retrieves the number of items that can fit horizontally in the client
1791 * area.
1793 * PARAMETER(S):
1794 * [I] infoPtr : valid pointer to the listview structure
1796 * RETURN:
1797 * Number of items per column.
1799 static inline INT LISTVIEW_GetCountPerColumn(const LISTVIEW_INFO *infoPtr)
1801 INT nListHeight = infoPtr->rcList.bottom - infoPtr->rcList.top;
1803 return infoPtr->nItemHeight ? max(nListHeight / infoPtr->nItemHeight, 1) : 0;
1807 /*************************************************************************
1808 * LISTVIEW_ProcessLetterKeys
1810 * Processes keyboard messages generated by pressing the letter keys
1811 * on the keyboard.
1812 * What this does is perform a case insensitive search from the
1813 * current position with the following quirks:
1814 * - If two chars or more are pressed in quick succession we search
1815 * for the corresponding string (e.g. 'abc').
1816 * - If there is a delay we wipe away the current search string and
1817 * restart with just that char.
1818 * - If the user keeps pressing the same character, whether slowly or
1819 * fast, so that the search string is entirely composed of this
1820 * character ('aaaaa' for instance), then we search for first item
1821 * that starting with that character.
1822 * - If the user types the above character in quick succession, then
1823 * we must also search for the corresponding string ('aaaaa'), and
1824 * go to that string if there is a match.
1826 * PARAMETERS
1827 * [I] hwnd : handle to the window
1828 * [I] charCode : the character code, the actual character
1829 * [I] keyData : key data
1831 * RETURNS
1833 * Zero.
1835 * BUGS
1837 * - The current implementation has a list of characters it will
1838 * accept and it ignores everything else. In particular it will
1839 * ignore accentuated characters which seems to match what
1840 * Windows does. But I'm not sure it makes sense to follow
1841 * Windows there.
1842 * - We don't sound a beep when the search fails.
1844 * SEE ALSO
1846 * TREEVIEW_ProcessLetterKeys
1848 static INT LISTVIEW_ProcessLetterKeys(LISTVIEW_INFO *infoPtr, WPARAM charCode, LPARAM keyData)
1850 WCHAR buffer[MAX_PATH];
1851 DWORD prevTime;
1852 LVITEMW item;
1853 int startidx;
1854 INT nItem;
1855 INT diff;
1857 /* simple parameter checking */
1858 if (!charCode || !keyData || infoPtr->nItemCount == 0) return 0;
1860 /* only allow the valid WM_CHARs through */
1861 if (!iswalnum(charCode) &&
1862 charCode != '.' && charCode != '`' && charCode != '!' &&
1863 charCode != '@' && charCode != '#' && charCode != '$' &&
1864 charCode != '%' && charCode != '^' && charCode != '&' &&
1865 charCode != '*' && charCode != '(' && charCode != ')' &&
1866 charCode != '-' && charCode != '_' && charCode != '+' &&
1867 charCode != '=' && charCode != '\\'&& charCode != ']' &&
1868 charCode != '}' && charCode != '[' && charCode != '{' &&
1869 charCode != '/' && charCode != '?' && charCode != '>' &&
1870 charCode != '<' && charCode != ',' && charCode != '~')
1871 return 0;
1873 /* update the search parameters */
1874 prevTime = infoPtr->lastKeyPressTimestamp;
1875 infoPtr->lastKeyPressTimestamp = GetTickCount();
1876 diff = infoPtr->lastKeyPressTimestamp - prevTime;
1878 if (diff >= 0 && diff < KEY_DELAY)
1880 if (infoPtr->nSearchParamLength < MAX_PATH - 1)
1881 infoPtr->szSearchParam[infoPtr->nSearchParamLength++] = charCode;
1883 if (infoPtr->charCode != charCode)
1884 infoPtr->charCode = charCode = 0;
1886 else
1888 infoPtr->charCode = charCode;
1889 infoPtr->szSearchParam[0] = charCode;
1890 infoPtr->nSearchParamLength = 1;
1893 /* should start from next after focused item, so next item that matches
1894 will be selected, if there isn't any and focused matches it will be selected
1895 on second search stage from beginning of the list */
1896 if (infoPtr->nFocusedItem >= 0 && infoPtr->nItemCount > 1)
1898 /* with some accumulated search data available start with current focus, otherwise
1899 it's excluded from search */
1900 startidx = infoPtr->nSearchParamLength > 1 ? infoPtr->nFocusedItem : infoPtr->nFocusedItem + 1;
1901 if (startidx == infoPtr->nItemCount) startidx = 0;
1903 else
1904 startidx = 0;
1906 /* let application handle this for virtual listview */
1907 if (infoPtr->dwStyle & LVS_OWNERDATA)
1909 NMLVFINDITEMW nmlv;
1911 memset(&nmlv.lvfi, 0, sizeof(nmlv.lvfi));
1912 nmlv.lvfi.flags = (LVFI_WRAP | LVFI_PARTIAL);
1913 nmlv.lvfi.psz = infoPtr->szSearchParam;
1914 nmlv.iStart = startidx;
1916 infoPtr->szSearchParam[infoPtr->nSearchParamLength] = 0;
1918 nItem = notify_hdr(infoPtr, LVN_ODFINDITEMW, (LPNMHDR)&nmlv.hdr);
1920 else
1922 int i = startidx, endidx;
1924 /* and search from the current position */
1925 nItem = -1;
1926 endidx = infoPtr->nItemCount;
1928 /* first search in [startidx, endidx), on failure continue in [0, startidx) */
1929 while (1)
1931 /* start from first item if not found with >= startidx */
1932 if (i == infoPtr->nItemCount && startidx > 0)
1934 endidx = startidx;
1935 startidx = 0;
1938 for (i = startidx; i < endidx; i++)
1940 /* retrieve text */
1941 item.mask = LVIF_TEXT;
1942 item.iItem = i;
1943 item.iSubItem = 0;
1944 item.pszText = buffer;
1945 item.cchTextMax = MAX_PATH;
1946 if (!LISTVIEW_GetItemW(infoPtr, &item)) return 0;
1948 if (!wcsnicmp(item.pszText, infoPtr->szSearchParam, infoPtr->nSearchParamLength))
1950 nItem = i;
1951 break;
1953 /* this is used to find first char match when search string is not available yet,
1954 otherwise every WM_CHAR will search to next item by first char, ignoring that we're
1955 already waiting for user to complete a string */
1956 else if (nItem == -1 && infoPtr->nSearchParamLength == 1 && !wcsnicmp(item.pszText, infoPtr->szSearchParam, 1))
1958 /* this would work but we must keep looking for a longer match */
1959 nItem = i;
1963 if ( nItem != -1 || /* found something */
1964 endidx != infoPtr->nItemCount || /* second search done */
1965 (startidx == 0 && endidx == infoPtr->nItemCount) /* full range for first search */ )
1966 break;
1970 if (nItem != -1)
1971 LISTVIEW_KeySelection(infoPtr, nItem, FALSE);
1973 return 0;
1976 /*************************************************************************
1977 * LISTVIEW_UpdateHeaderSize [Internal]
1979 * Function to resize the header control
1981 * PARAMS
1982 * [I] hwnd : handle to a window
1983 * [I] nNewScrollPos : scroll pos to set
1985 * RETURNS
1986 * None.
1988 static void LISTVIEW_UpdateHeaderSize(const LISTVIEW_INFO *infoPtr, INT nNewScrollPos)
1990 RECT winRect;
1991 POINT point[2];
1993 TRACE("nNewScrollPos=%d\n", nNewScrollPos);
1995 if (!infoPtr->hwndHeader) return;
1997 GetWindowRect(infoPtr->hwndHeader, &winRect);
1998 point[0].x = winRect.left;
1999 point[0].y = winRect.top;
2000 point[1].x = winRect.right;
2001 point[1].y = winRect.bottom;
2003 MapWindowPoints(HWND_DESKTOP, infoPtr->hwndSelf, point, 2);
2004 point[0].x = -nNewScrollPos;
2005 point[1].x += nNewScrollPos;
2007 SetWindowPos(infoPtr->hwndHeader,0,
2008 point[0].x,point[0].y,point[1].x,point[1].y,
2009 (infoPtr->dwStyle & LVS_NOCOLUMNHEADER) ? SWP_HIDEWINDOW : SWP_SHOWWINDOW |
2010 SWP_NOZORDER | SWP_NOACTIVATE);
2013 static INT LISTVIEW_UpdateHScroll(LISTVIEW_INFO *infoPtr)
2015 SCROLLINFO horzInfo;
2016 INT dx;
2018 ZeroMemory(&horzInfo, sizeof(SCROLLINFO));
2019 horzInfo.cbSize = sizeof(SCROLLINFO);
2020 horzInfo.nPage = infoPtr->rcList.right - infoPtr->rcList.left;
2022 /* for now, we'll set info.nMax to the _count_, and adjust it later */
2023 if (infoPtr->uView == LV_VIEW_LIST)
2025 INT nPerCol = LISTVIEW_GetCountPerColumn(infoPtr);
2026 horzInfo.nMax = (infoPtr->nItemCount + nPerCol - 1) / nPerCol;
2028 /* scroll by at least one column per page */
2029 if(horzInfo.nPage < infoPtr->nItemWidth)
2030 horzInfo.nPage = infoPtr->nItemWidth;
2032 if (infoPtr->nItemWidth)
2033 horzInfo.nPage /= infoPtr->nItemWidth;
2035 else if (infoPtr->uView == LV_VIEW_DETAILS)
2037 horzInfo.nMax = infoPtr->nItemWidth;
2039 else /* LV_VIEW_ICON, or LV_VIEW_SMALLICON */
2041 RECT rcView;
2043 if (LISTVIEW_GetViewRect(infoPtr, &rcView)) horzInfo.nMax = rcView.right - rcView.left;
2046 if (LISTVIEW_IsHeaderEnabled(infoPtr))
2048 if (DPA_GetPtrCount(infoPtr->hdpaColumns))
2050 RECT rcHeader;
2051 INT index;
2053 index = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX,
2054 DPA_GetPtrCount(infoPtr->hdpaColumns) - 1, 0);
2056 LISTVIEW_GetHeaderRect(infoPtr, index, &rcHeader);
2057 horzInfo.nMax = rcHeader.right;
2058 TRACE("horzInfo.nMax=%d\n", horzInfo.nMax);
2062 horzInfo.fMask = SIF_RANGE | SIF_PAGE;
2063 horzInfo.nMax = max(horzInfo.nMax - 1, 0);
2064 dx = GetScrollPos(infoPtr->hwndSelf, SB_HORZ);
2065 dx -= SetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &horzInfo, TRUE);
2066 TRACE("horzInfo=%s\n", debugscrollinfo(&horzInfo));
2068 /* Update the Header Control */
2069 if (infoPtr->hwndHeader)
2071 horzInfo.fMask = SIF_POS;
2072 GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &horzInfo);
2073 LISTVIEW_UpdateHeaderSize(infoPtr, horzInfo.nPos);
2076 LISTVIEW_UpdateSize(infoPtr);
2077 return dx;
2080 static INT LISTVIEW_UpdateVScroll(LISTVIEW_INFO *infoPtr)
2082 SCROLLINFO vertInfo;
2083 INT dy;
2085 ZeroMemory(&vertInfo, sizeof(SCROLLINFO));
2086 vertInfo.cbSize = sizeof(SCROLLINFO);
2087 vertInfo.nPage = infoPtr->rcList.bottom - infoPtr->rcList.top;
2089 if (infoPtr->uView == LV_VIEW_DETAILS)
2091 vertInfo.nMax = infoPtr->nItemCount;
2093 /* scroll by at least one page */
2094 if(vertInfo.nPage < infoPtr->nItemHeight)
2095 vertInfo.nPage = infoPtr->nItemHeight;
2097 if (infoPtr->nItemHeight > 0)
2098 vertInfo.nPage /= infoPtr->nItemHeight;
2100 else if (infoPtr->uView != LV_VIEW_LIST) /* LV_VIEW_ICON, or LV_VIEW_SMALLICON */
2102 RECT rcView;
2104 if (LISTVIEW_GetViewRect(infoPtr, &rcView)) vertInfo.nMax = rcView.bottom - rcView.top;
2107 vertInfo.fMask = SIF_RANGE | SIF_PAGE;
2108 vertInfo.nMax = max(vertInfo.nMax - 1, 0);
2109 dy = GetScrollPos(infoPtr->hwndSelf, SB_VERT);
2110 dy -= SetScrollInfo(infoPtr->hwndSelf, SB_VERT, &vertInfo, TRUE);
2111 TRACE("vertInfo=%s\n", debugscrollinfo(&vertInfo));
2113 LISTVIEW_UpdateSize(infoPtr);
2114 return dy;
2117 /***
2118 * DESCRIPTION:
2119 * Update the scrollbars. This function should be called whenever
2120 * the content, size or view changes.
2122 * PARAMETER(S):
2123 * [I] infoPtr : valid pointer to the listview structure
2125 * RETURN:
2126 * None
2128 static void LISTVIEW_UpdateScroll(LISTVIEW_INFO *infoPtr)
2130 INT dx, dy, pass;
2132 if ((infoPtr->dwStyle & LVS_NOSCROLL) || !is_redrawing(infoPtr)) return;
2134 /* Setting the horizontal scroll can change the listview size
2135 * (and potentially everything else) so we need to recompute
2136 * everything again for the vertical scroll and vice-versa
2138 for (dx = 0, dy = 0, pass = 0; pass <= 1; pass++)
2140 dx += LISTVIEW_UpdateHScroll(infoPtr);
2141 dy += LISTVIEW_UpdateVScroll(infoPtr);
2144 /* Change of the range may have changed the scroll pos. If so move the content */
2145 if (dx != 0 || dy != 0)
2147 RECT listRect;
2148 listRect = infoPtr->rcList;
2149 ScrollWindowEx(infoPtr->hwndSelf, dx, dy, &listRect, &listRect, 0, 0,
2150 SW_ERASE | SW_INVALIDATE);
2155 /***
2156 * DESCRIPTION:
2157 * Shows/hides the focus rectangle.
2159 * PARAMETER(S):
2160 * [I] infoPtr : valid pointer to the listview structure
2161 * [I] fShow : TRUE to show the focus, FALSE to hide it.
2163 * RETURN:
2164 * None
2166 static void LISTVIEW_ShowFocusRect(const LISTVIEW_INFO *infoPtr, BOOL fShow)
2168 HDC hdc;
2170 TRACE("fShow=%d, nItem=%d\n", fShow, infoPtr->nFocusedItem);
2172 if (infoPtr->nFocusedItem < 0) return;
2174 /* we need some gymnastics in ICON mode to handle large items */
2175 if (infoPtr->uView == LV_VIEW_ICON)
2177 RECT rcBox;
2179 LISTVIEW_GetItemBox(infoPtr, infoPtr->nFocusedItem, &rcBox);
2180 if ((rcBox.bottom - rcBox.top) > infoPtr->nItemHeight)
2182 LISTVIEW_InvalidateRect(infoPtr, &rcBox);
2183 return;
2187 if (!(hdc = GetDC(infoPtr->hwndSelf))) return;
2189 /* for some reason, owner draw should work only in report mode */
2190 if ((infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && (infoPtr->uView == LV_VIEW_DETAILS))
2192 DRAWITEMSTRUCT dis;
2193 LVITEMW item;
2195 HFONT hFont = infoPtr->hFont ? infoPtr->hFont : infoPtr->hDefaultFont;
2196 HFONT hOldFont = SelectObject(hdc, hFont);
2198 item.iItem = infoPtr->nFocusedItem;
2199 item.iSubItem = 0;
2200 item.mask = LVIF_PARAM;
2201 if (!LISTVIEW_GetItemW(infoPtr, &item)) goto done;
2203 ZeroMemory(&dis, sizeof(dis));
2204 dis.CtlType = ODT_LISTVIEW;
2205 dis.CtlID = (UINT)GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
2206 dis.itemID = item.iItem;
2207 dis.itemAction = ODA_FOCUS;
2208 if (fShow) dis.itemState |= ODS_FOCUS;
2209 dis.hwndItem = infoPtr->hwndSelf;
2210 dis.hDC = hdc;
2211 LISTVIEW_GetItemBox(infoPtr, dis.itemID, &dis.rcItem);
2212 dis.itemData = item.lParam;
2214 SendMessageW(infoPtr->hwndNotify, WM_DRAWITEM, dis.CtlID, (LPARAM)&dis);
2216 SelectObject(hdc, hOldFont);
2218 else
2219 LISTVIEW_InvalidateItem(infoPtr, infoPtr->nFocusedItem);
2221 done:
2222 ReleaseDC(infoPtr->hwndSelf, hdc);
2225 /***
2226 * Invalidates all visible selected items.
2228 static void LISTVIEW_InvalidateSelectedItems(const LISTVIEW_INFO *infoPtr)
2230 ITERATOR i;
2232 iterator_frameditems(&i, infoPtr, &infoPtr->rcList);
2233 while(iterator_next(&i))
2235 if (LISTVIEW_GetItemState(infoPtr, i.nItem, LVIS_SELECTED))
2236 LISTVIEW_InvalidateItem(infoPtr, i.nItem);
2238 iterator_destroy(&i);
2242 /***
2243 * DESCRIPTION: [INTERNAL]
2244 * Computes an item's (left,top) corner, relative to rcView.
2245 * That is, the position has NOT been made relative to the Origin.
2246 * This is deliberate, to avoid computing the Origin over, and
2247 * over again, when this function is called in a loop. Instead,
2248 * one can factor the computation of the Origin before the loop,
2249 * and offset the value returned by this function, on every iteration.
2251 * PARAMETER(S):
2252 * [I] infoPtr : valid pointer to the listview structure
2253 * [I] nItem : item number
2254 * [O] lpptOrig : item top, left corner
2256 * RETURN:
2257 * None.
2259 static void LISTVIEW_GetItemOrigin(const LISTVIEW_INFO *infoPtr, INT nItem, LPPOINT lpptPosition)
2261 assert(nItem >= 0 && nItem < infoPtr->nItemCount);
2263 if ((infoPtr->uView == LV_VIEW_SMALLICON) || (infoPtr->uView == LV_VIEW_ICON))
2265 lpptPosition->x = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosX, nItem);
2266 lpptPosition->y = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosY, nItem);
2268 else if (infoPtr->uView == LV_VIEW_LIST)
2270 INT nCountPerColumn = LISTVIEW_GetCountPerColumn(infoPtr);
2271 lpptPosition->x = nItem / nCountPerColumn * infoPtr->nItemWidth;
2272 lpptPosition->y = nItem % nCountPerColumn * infoPtr->nItemHeight;
2274 else /* LV_VIEW_DETAILS */
2276 lpptPosition->x = REPORT_MARGINX;
2277 /* item is always at zero indexed column */
2278 if (DPA_GetPtrCount(infoPtr->hdpaColumns) > 0)
2279 lpptPosition->x += LISTVIEW_GetColumnInfo(infoPtr, 0)->rcHeader.left;
2280 lpptPosition->y = nItem * infoPtr->nItemHeight;
2284 /***
2285 * DESCRIPTION: [INTERNAL]
2286 * Compute the rectangles of an item. This is to localize all
2287 * the computations in one place. If you are not interested in some
2288 * of these values, simply pass in a NULL -- the function is smart
2289 * enough to compute only what's necessary. The function computes
2290 * the standard rectangles (BOUNDS, ICON, LABEL) plus a non-standard
2291 * one, the BOX rectangle. This rectangle is very cheap to compute,
2292 * and is guaranteed to contain all the other rectangles. Computing
2293 * the ICON rect is also cheap, but all the others are potentially
2294 * expensive. This gives an easy and effective optimization when
2295 * searching (like point inclusion, or rectangle intersection):
2296 * first test against the BOX, and if TRUE, test against the desired
2297 * rectangle.
2298 * If the function does not have all the necessary information
2299 * to computed the requested rectangles, will crash with a
2300 * failed assertion. This is done so we catch all programming
2301 * errors, given that the function is called only from our code.
2303 * We have the following 'special' meanings for a few fields:
2304 * * If LVIS_FOCUSED is set, we assume the item has the focus
2305 * This is important in ICON mode, where it might get a larger
2306 * then usual rectangle
2308 * Please note that subitem support works only in REPORT mode.
2310 * PARAMETER(S):
2311 * [I] infoPtr : valid pointer to the listview structure
2312 * [I] lpLVItem : item to compute the measures for
2313 * [O] lprcBox : ptr to Box rectangle
2314 * Same as LVM_GETITEMRECT with LVIR_BOUNDS
2315 * [0] lprcSelectBox : ptr to select box rectangle
2316 * Same as LVM_GETITEMRECT with LVIR_SELECTEDBOUNDS
2317 * [O] lprcIcon : ptr to Icon rectangle
2318 * Same as LVM_GETITEMRECT with LVIR_ICON
2319 * [O] lprcStateIcon: ptr to State Icon rectangle
2320 * [O] lprcLabel : ptr to Label rectangle
2321 * Same as LVM_GETITEMRECT with LVIR_LABEL
2323 * RETURN:
2324 * None.
2326 static void LISTVIEW_GetItemMetrics(const LISTVIEW_INFO *infoPtr, const LVITEMW *lpLVItem,
2327 LPRECT lprcBox, LPRECT lprcSelectBox,
2328 LPRECT lprcIcon, LPRECT lprcStateIcon, LPRECT lprcLabel)
2330 BOOL doSelectBox = FALSE, doIcon = FALSE, doLabel = FALSE, oversizedBox = FALSE;
2331 RECT Box, SelectBox, Icon, Label;
2332 COLUMN_INFO *lpColumnInfo = NULL;
2333 SIZE labelSize = { 0, 0 };
2335 TRACE("(lpLVItem=%s)\n", debuglvitem_t(lpLVItem, TRUE));
2337 /* Be smart and try to figure out the minimum we have to do */
2338 if (lpLVItem->iSubItem) assert(infoPtr->uView == LV_VIEW_DETAILS);
2339 if (infoPtr->uView == LV_VIEW_ICON && (lprcBox || lprcLabel))
2341 assert((lpLVItem->mask & LVIF_STATE) && (lpLVItem->stateMask & LVIS_FOCUSED));
2342 if (lpLVItem->state & LVIS_FOCUSED) oversizedBox = doLabel = TRUE;
2344 if (lprcSelectBox) doSelectBox = TRUE;
2345 if (lprcLabel) doLabel = TRUE;
2346 if (doLabel || lprcIcon || lprcStateIcon) doIcon = TRUE;
2347 if (doSelectBox)
2349 doIcon = TRUE;
2350 doLabel = TRUE;
2353 /************************************************************/
2354 /* compute the box rectangle (it should be cheap to do) */
2355 /************************************************************/
2356 if (lpLVItem->iSubItem || infoPtr->uView == LV_VIEW_DETAILS)
2357 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, lpLVItem->iSubItem);
2359 if (lpLVItem->iSubItem)
2361 Box = lpColumnInfo->rcHeader;
2363 else
2365 Box.left = 0;
2366 Box.right = infoPtr->nItemWidth;
2368 Box.top = 0;
2369 Box.bottom = infoPtr->nItemHeight;
2371 /******************************************************************/
2372 /* compute ICON bounding box (ala LVM_GETITEMRECT) and STATEICON */
2373 /******************************************************************/
2374 if (doIcon)
2376 LONG state_width = 0;
2378 if (infoPtr->himlState && lpLVItem->iSubItem == 0)
2379 state_width = infoPtr->iconStateSize.cx;
2381 if (infoPtr->uView == LV_VIEW_ICON)
2383 Icon.left = Box.left + state_width;
2384 if (infoPtr->himlNormal)
2385 Icon.left += (infoPtr->nItemWidth - infoPtr->iconSize.cx - state_width) / 2;
2386 Icon.top = Box.top + ICON_TOP_PADDING;
2387 Icon.right = Icon.left;
2388 Icon.bottom = Icon.top;
2389 if (infoPtr->himlNormal)
2391 Icon.right += infoPtr->iconSize.cx;
2392 Icon.bottom += infoPtr->iconSize.cy;
2395 else /* LV_VIEW_SMALLICON, LV_VIEW_LIST or LV_VIEW_DETAILS */
2397 Icon.left = Box.left + state_width;
2399 if (infoPtr->uView == LV_VIEW_DETAILS && lpLVItem->iSubItem == 0)
2401 /* we need the indent in report mode */
2402 assert(lpLVItem->mask & LVIF_INDENT);
2403 Icon.left += infoPtr->iconSize.cx * lpLVItem->iIndent + REPORT_MARGINX;
2406 Icon.top = Box.top;
2407 Icon.right = Icon.left;
2408 if (infoPtr->himlSmall &&
2409 (!lpColumnInfo || lpLVItem->iSubItem == 0 ||
2410 ((infoPtr->dwLvExStyle & LVS_EX_SUBITEMIMAGES) && lpLVItem->iImage != I_IMAGECALLBACK)))
2411 Icon.right += infoPtr->iconSize.cx;
2412 Icon.bottom = Icon.top + infoPtr->iconSize.cy;
2414 if(lprcIcon) *lprcIcon = Icon;
2415 TRACE(" - icon=%s\n", wine_dbgstr_rect(&Icon));
2417 /* TODO: is this correct? */
2418 if (lprcStateIcon)
2420 lprcStateIcon->left = Icon.left - state_width;
2421 lprcStateIcon->right = Icon.left;
2422 lprcStateIcon->top = Icon.top;
2423 lprcStateIcon->bottom = lprcStateIcon->top + infoPtr->iconSize.cy;
2424 TRACE(" - state icon=%s\n", wine_dbgstr_rect(lprcStateIcon));
2427 else Icon.right = 0;
2429 /************************************************************/
2430 /* compute LABEL bounding box (ala LVM_GETITEMRECT) */
2431 /************************************************************/
2432 if (doLabel)
2434 /* calculate how far to the right can the label stretch */
2435 Label.right = Box.right;
2436 if (infoPtr->uView == LV_VIEW_DETAILS)
2438 if (lpLVItem->iSubItem == 0)
2440 /* we need a zero based rect here */
2441 Label = lpColumnInfo->rcHeader;
2442 OffsetRect(&Label, -Label.left, 0);
2446 if (lpLVItem->iSubItem || ((infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && infoPtr->uView == LV_VIEW_DETAILS))
2448 labelSize.cx = infoPtr->nItemWidth;
2449 labelSize.cy = infoPtr->nItemHeight;
2450 goto calc_label;
2453 /* we need the text in non owner draw mode */
2454 assert(lpLVItem->mask & LVIF_TEXT);
2455 if (is_text(lpLVItem->pszText))
2457 HFONT hFont = infoPtr->hFont ? infoPtr->hFont : infoPtr->hDefaultFont;
2458 HDC hdc = GetDC(infoPtr->hwndSelf);
2459 HFONT hOldFont = SelectObject(hdc, hFont);
2460 UINT uFormat;
2461 RECT rcText;
2463 /* compute rough rectangle where the label will go */
2464 SetRectEmpty(&rcText);
2465 rcText.right = infoPtr->nItemWidth - TRAILING_LABEL_PADDING;
2466 rcText.bottom = infoPtr->nItemHeight;
2467 if (infoPtr->uView == LV_VIEW_ICON)
2468 rcText.bottom -= ICON_TOP_PADDING + infoPtr->iconSize.cy + ICON_BOTTOM_PADDING;
2470 /* now figure out the flags */
2471 if (infoPtr->uView == LV_VIEW_ICON)
2472 uFormat = oversizedBox ? LV_FL_DT_FLAGS : LV_ML_DT_FLAGS;
2473 else
2474 uFormat = LV_SL_DT_FLAGS;
2476 DrawTextW (hdc, lpLVItem->pszText, -1, &rcText, uFormat | DT_CALCRECT);
2478 if (rcText.right != rcText.left)
2479 labelSize.cx = min(rcText.right - rcText.left + TRAILING_LABEL_PADDING, infoPtr->nItemWidth);
2481 labelSize.cy = rcText.bottom - rcText.top;
2483 SelectObject(hdc, hOldFont);
2484 ReleaseDC(infoPtr->hwndSelf, hdc);
2487 calc_label:
2488 if (infoPtr->uView == LV_VIEW_ICON)
2490 Label.left = Box.left + (infoPtr->nItemWidth - labelSize.cx) / 2;
2491 Label.top = Box.top + ICON_TOP_PADDING_HITABLE +
2492 infoPtr->iconSize.cy + ICON_BOTTOM_PADDING;
2493 Label.right = Label.left + labelSize.cx;
2494 Label.bottom = Label.top + infoPtr->nItemHeight;
2495 if (!oversizedBox && labelSize.cy > infoPtr->ntmHeight)
2497 labelSize.cy = min(Box.bottom - Label.top, labelSize.cy);
2498 labelSize.cy /= infoPtr->ntmHeight;
2499 labelSize.cy = max(labelSize.cy, 1);
2500 labelSize.cy *= infoPtr->ntmHeight;
2502 Label.bottom = Label.top + labelSize.cy + HEIGHT_PADDING;
2504 else if (infoPtr->uView == LV_VIEW_DETAILS)
2506 Label.left = Icon.right;
2507 Label.top = Box.top;
2508 Label.right = lpLVItem->iSubItem ? lpColumnInfo->rcHeader.right :
2509 lpColumnInfo->rcHeader.right - lpColumnInfo->rcHeader.left;
2510 Label.bottom = Label.top + infoPtr->nItemHeight;
2512 else /* LV_VIEW_SMALLICON or LV_VIEW_LIST */
2514 Label.left = Icon.right;
2515 Label.top = Box.top;
2516 Label.right = min(Label.left + labelSize.cx, Label.right);
2517 Label.bottom = Label.top + infoPtr->nItemHeight;
2520 if (lprcLabel) *lprcLabel = Label;
2521 TRACE(" - label=%s\n", wine_dbgstr_rect(&Label));
2524 /************************************************************/
2525 /* compute SELECT bounding box */
2526 /************************************************************/
2527 if (doSelectBox)
2529 if (infoPtr->uView == LV_VIEW_DETAILS)
2531 SelectBox.left = Icon.left;
2532 SelectBox.top = Box.top;
2533 SelectBox.bottom = Box.bottom;
2535 if (labelSize.cx)
2536 SelectBox.right = min(Label.left + labelSize.cx, Label.right);
2537 else
2538 SelectBox.right = min(Label.left + MAX_EMPTYTEXT_SELECT_WIDTH, Label.right);
2540 else
2542 UnionRect(&SelectBox, &Icon, &Label);
2544 if (lprcSelectBox) *lprcSelectBox = SelectBox;
2545 TRACE(" - select box=%s\n", wine_dbgstr_rect(&SelectBox));
2548 /* Fix the Box if necessary */
2549 if (lprcBox)
2551 if (oversizedBox) UnionRect(lprcBox, &Box, &Label);
2552 else *lprcBox = Box;
2554 TRACE(" - box=%s\n", wine_dbgstr_rect(&Box));
2557 /***
2558 * DESCRIPTION: [INTERNAL]
2560 * PARAMETER(S):
2561 * [I] infoPtr : valid pointer to the listview structure
2562 * [I] nItem : item number
2563 * [O] lprcBox : ptr to Box rectangle
2565 * RETURN:
2566 * None.
2568 static void LISTVIEW_GetItemBox(const LISTVIEW_INFO *infoPtr, INT nItem, LPRECT lprcBox)
2570 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
2571 POINT Position, Origin;
2572 LVITEMW lvItem;
2574 LISTVIEW_GetOrigin(infoPtr, &Origin);
2575 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position);
2577 /* Be smart and try to figure out the minimum we have to do */
2578 lvItem.mask = 0;
2579 if (infoPtr->uView == LV_VIEW_ICON && infoPtr->bFocus && LISTVIEW_GetItemState(infoPtr, nItem, LVIS_FOCUSED))
2580 lvItem.mask |= LVIF_TEXT;
2581 lvItem.iItem = nItem;
2582 lvItem.iSubItem = 0;
2583 lvItem.pszText = szDispText;
2584 lvItem.cchTextMax = DISP_TEXT_SIZE;
2585 if (lvItem.mask) LISTVIEW_GetItemW(infoPtr, &lvItem);
2586 if (infoPtr->uView == LV_VIEW_ICON)
2588 lvItem.mask |= LVIF_STATE;
2589 lvItem.stateMask = LVIS_FOCUSED;
2590 lvItem.state = (lvItem.mask & LVIF_TEXT ? LVIS_FOCUSED : 0);
2592 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, lprcBox, 0, 0, 0, 0);
2594 if (infoPtr->uView == LV_VIEW_DETAILS && infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT &&
2595 SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX, 0, 0))
2597 OffsetRect(lprcBox, Origin.x, Position.y + Origin.y);
2599 else
2600 OffsetRect(lprcBox, Position.x + Origin.x, Position.y + Origin.y);
2603 /* LISTVIEW_MapIdToIndex helper */
2604 static INT CALLBACK MapIdSearchCompare(LPVOID p1, LPVOID p2, LPARAM lParam)
2606 ITEM_ID *id1 = (ITEM_ID*)p1;
2607 ITEM_ID *id2 = (ITEM_ID*)p2;
2609 if (id1->id == id2->id) return 0;
2611 return (id1->id < id2->id) ? -1 : 1;
2614 /***
2615 * DESCRIPTION:
2616 * Returns the item index for id specified.
2618 * PARAMETER(S):
2619 * [I] infoPtr : valid pointer to the listview structure
2620 * [I] iID : item id to get index for
2622 * RETURN:
2623 * Item index, or -1 on failure.
2625 static INT LISTVIEW_MapIdToIndex(const LISTVIEW_INFO *infoPtr, UINT iID)
2627 ITEM_ID ID;
2628 INT index;
2630 TRACE("iID=%d\n", iID);
2632 if (infoPtr->dwStyle & LVS_OWNERDATA) return -1;
2633 if (infoPtr->nItemCount == 0) return -1;
2635 ID.id = iID;
2636 index = DPA_Search(infoPtr->hdpaItemIds, &ID, -1, MapIdSearchCompare, 0, DPAS_SORTED);
2638 if (index != -1)
2640 ITEM_ID *lpID = DPA_GetPtr(infoPtr->hdpaItemIds, index);
2641 return DPA_GetPtrIndex(infoPtr->hdpaItems, lpID->item);
2644 return -1;
2647 /***
2648 * DESCRIPTION:
2649 * Returns the item id for index given.
2651 * PARAMETER(S):
2652 * [I] infoPtr : valid pointer to the listview structure
2653 * [I] iItem : item index to get id for
2655 * RETURN:
2656 * Item id.
2658 static DWORD LISTVIEW_MapIndexToId(const LISTVIEW_INFO *infoPtr, INT iItem)
2660 ITEM_INFO *lpItem;
2661 HDPA hdpaSubItems;
2663 TRACE("iItem=%d\n", iItem);
2665 if (infoPtr->dwStyle & LVS_OWNERDATA) return -1;
2666 if (iItem < 0 || iItem >= infoPtr->nItemCount) return -1;
2668 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, iItem);
2669 lpItem = DPA_GetPtr(hdpaSubItems, 0);
2671 return lpItem->id->id;
2674 /***
2675 * DESCRIPTION:
2676 * Returns the current icon position, and advances it along the top.
2677 * The returned position is not offset by Origin.
2679 * PARAMETER(S):
2680 * [I] infoPtr : valid pointer to the listview structure
2681 * [O] lpPos : will get the current icon position
2683 * RETURN:
2684 * None
2686 static void LISTVIEW_NextIconPosTop(LISTVIEW_INFO *infoPtr, LPPOINT lpPos)
2688 INT nListWidth = infoPtr->rcList.right - infoPtr->rcList.left;
2690 *lpPos = infoPtr->currIconPos;
2692 infoPtr->currIconPos.x += infoPtr->nItemWidth;
2693 if (infoPtr->currIconPos.x + infoPtr->nItemWidth <= nListWidth) return;
2695 infoPtr->currIconPos.x = 0;
2696 infoPtr->currIconPos.y += infoPtr->nItemHeight;
2700 /***
2701 * DESCRIPTION:
2702 * Returns the current icon position, and advances it down the left edge.
2703 * The returned position is not offset by Origin.
2705 * PARAMETER(S):
2706 * [I] infoPtr : valid pointer to the listview structure
2707 * [O] lpPos : will get the current icon position
2709 * RETURN:
2710 * None
2712 static void LISTVIEW_NextIconPosLeft(LISTVIEW_INFO *infoPtr, LPPOINT lpPos)
2714 INT nListHeight = infoPtr->rcList.bottom - infoPtr->rcList.top;
2716 *lpPos = infoPtr->currIconPos;
2718 infoPtr->currIconPos.y += infoPtr->nItemHeight;
2719 if (infoPtr->currIconPos.y + infoPtr->nItemHeight <= nListHeight) return;
2721 infoPtr->currIconPos.x += infoPtr->nItemWidth;
2722 infoPtr->currIconPos.y = 0;
2726 /***
2727 * DESCRIPTION:
2728 * Moves an icon to the specified position.
2729 * It takes care of invalidating the item, etc.
2731 * PARAMETER(S):
2732 * [I] infoPtr : valid pointer to the listview structure
2733 * [I] nItem : the item to move
2734 * [I] lpPos : the new icon position
2735 * [I] isNew : flags the item as being new
2737 * RETURN:
2738 * Success: TRUE
2739 * Failure: FALSE
2741 static BOOL LISTVIEW_MoveIconTo(const LISTVIEW_INFO *infoPtr, INT nItem, const POINT *lppt, BOOL isNew)
2743 POINT old;
2745 if (!isNew)
2747 old.x = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosX, nItem);
2748 old.y = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosY, nItem);
2750 if (lppt->x == old.x && lppt->y == old.y) return TRUE;
2751 LISTVIEW_InvalidateItem(infoPtr, nItem);
2754 /* Allocating a POINTER for every item is too resource intensive,
2755 * so we'll keep the (x,y) in different arrays */
2756 if (!DPA_SetPtr(infoPtr->hdpaPosX, nItem, (void *)(LONG_PTR)lppt->x)) return FALSE;
2757 if (!DPA_SetPtr(infoPtr->hdpaPosY, nItem, (void *)(LONG_PTR)lppt->y)) return FALSE;
2759 LISTVIEW_InvalidateItem(infoPtr, nItem);
2761 return TRUE;
2764 /***
2765 * DESCRIPTION:
2766 * Arranges listview items in icon display mode.
2768 * PARAMETER(S):
2769 * [I] infoPtr : valid pointer to the listview structure
2770 * [I] nAlignCode : alignment code
2772 * RETURN:
2773 * SUCCESS : TRUE
2774 * FAILURE : FALSE
2776 static BOOL LISTVIEW_Arrange(LISTVIEW_INFO *infoPtr, INT nAlignCode)
2778 void (*next_pos)(LISTVIEW_INFO *, LPPOINT);
2779 POINT pos;
2780 INT i;
2782 if (infoPtr->uView != LV_VIEW_ICON && infoPtr->uView != LV_VIEW_SMALLICON) return FALSE;
2784 TRACE("nAlignCode=%d\n", nAlignCode);
2786 if (nAlignCode == LVA_DEFAULT)
2788 if (infoPtr->dwStyle & LVS_ALIGNLEFT) nAlignCode = LVA_ALIGNLEFT;
2789 else nAlignCode = LVA_ALIGNTOP;
2792 switch (nAlignCode)
2794 case LVA_ALIGNLEFT: next_pos = LISTVIEW_NextIconPosLeft; break;
2795 case LVA_ALIGNTOP: next_pos = LISTVIEW_NextIconPosTop; break;
2796 case LVA_SNAPTOGRID: next_pos = LISTVIEW_NextIconPosTop; break; /* FIXME */
2797 default: return FALSE;
2800 infoPtr->currIconPos.x = infoPtr->currIconPos.y = 0;
2801 for (i = 0; i < infoPtr->nItemCount; i++)
2803 next_pos(infoPtr, &pos);
2804 LISTVIEW_MoveIconTo(infoPtr, i, &pos, FALSE);
2807 return TRUE;
2810 /***
2811 * DESCRIPTION:
2812 * Retrieves the bounding rectangle of all the items, not offset by Origin.
2813 * For LVS_REPORT always returns empty rectangle.
2815 * PARAMETER(S):
2816 * [I] infoPtr : valid pointer to the listview structure
2817 * [O] lprcView : bounding rectangle
2819 * RETURN:
2820 * SUCCESS : TRUE
2821 * FAILURE : FALSE
2823 static void LISTVIEW_GetAreaRect(const LISTVIEW_INFO *infoPtr, LPRECT lprcView)
2825 INT i, x, y;
2827 SetRectEmpty(lprcView);
2829 switch (infoPtr->uView)
2831 case LV_VIEW_ICON:
2832 case LV_VIEW_SMALLICON:
2833 for (i = 0; i < infoPtr->nItemCount; i++)
2835 x = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosX, i);
2836 y = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosY, i);
2837 lprcView->right = max(lprcView->right, x);
2838 lprcView->bottom = max(lprcView->bottom, y);
2840 if (infoPtr->nItemCount > 0)
2842 lprcView->right += infoPtr->nItemWidth;
2843 lprcView->bottom += infoPtr->nItemHeight;
2845 break;
2847 case LV_VIEW_LIST:
2848 y = LISTVIEW_GetCountPerColumn(infoPtr);
2849 x = infoPtr->nItemCount / y;
2850 if (infoPtr->nItemCount % y) x++;
2851 lprcView->right = x * infoPtr->nItemWidth;
2852 lprcView->bottom = y * infoPtr->nItemHeight;
2853 break;
2857 /***
2858 * DESCRIPTION:
2859 * Retrieves the bounding rectangle of all the items.
2861 * PARAMETER(S):
2862 * [I] infoPtr : valid pointer to the listview structure
2863 * [O] lprcView : bounding rectangle
2865 * RETURN:
2866 * SUCCESS : TRUE
2867 * FAILURE : FALSE
2869 static BOOL LISTVIEW_GetViewRect(const LISTVIEW_INFO *infoPtr, LPRECT lprcView)
2871 POINT ptOrigin;
2873 TRACE("(lprcView=%p)\n", lprcView);
2875 if (!lprcView) return FALSE;
2877 LISTVIEW_GetAreaRect(infoPtr, lprcView);
2879 if (infoPtr->uView != LV_VIEW_DETAILS)
2881 LISTVIEW_GetOrigin(infoPtr, &ptOrigin);
2882 OffsetRect(lprcView, ptOrigin.x, ptOrigin.y);
2885 TRACE("lprcView=%s\n", wine_dbgstr_rect(lprcView));
2887 return TRUE;
2890 /***
2891 * DESCRIPTION:
2892 * Retrieves the subitem pointer associated with the subitem index.
2894 * PARAMETER(S):
2895 * [I] hdpaSubItems : DPA handle for a specific item
2896 * [I] nSubItem : index of subitem
2898 * RETURN:
2899 * SUCCESS : subitem pointer
2900 * FAILURE : NULL
2902 static SUBITEM_INFO* LISTVIEW_GetSubItemPtr(HDPA hdpaSubItems, INT nSubItem)
2904 SUBITEM_INFO *lpSubItem;
2905 INT i;
2907 /* we should binary search here if need be */
2908 for (i = 1; i < DPA_GetPtrCount(hdpaSubItems); i++)
2910 lpSubItem = DPA_GetPtr(hdpaSubItems, i);
2911 if (lpSubItem->iSubItem == nSubItem)
2912 return lpSubItem;
2915 return NULL;
2919 /***
2920 * DESCRIPTION:
2921 * Calculates the desired item width.
2923 * PARAMETER(S):
2924 * [I] infoPtr : valid pointer to the listview structure
2926 * RETURN:
2927 * The desired item width.
2929 static INT LISTVIEW_CalculateItemWidth(const LISTVIEW_INFO *infoPtr)
2931 INT nItemWidth = 0;
2933 TRACE("view %ld\n", infoPtr->uView);
2935 if (infoPtr->uView == LV_VIEW_ICON)
2936 nItemWidth = infoPtr->iconSpacing.cx;
2937 else if (infoPtr->uView == LV_VIEW_DETAILS)
2939 if (DPA_GetPtrCount(infoPtr->hdpaColumns) > 0)
2941 RECT rcHeader;
2942 INT index;
2944 index = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX,
2945 DPA_GetPtrCount(infoPtr->hdpaColumns) - 1, 0);
2947 LISTVIEW_GetHeaderRect(infoPtr, index, &rcHeader);
2948 nItemWidth = rcHeader.right;
2951 else /* LV_VIEW_SMALLICON, or LV_VIEW_LIST */
2953 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
2954 LVITEMW lvItem;
2955 INT i;
2957 lvItem.mask = LVIF_TEXT;
2958 lvItem.iSubItem = 0;
2960 for (i = 0; i < infoPtr->nItemCount; i++)
2962 lvItem.iItem = i;
2963 lvItem.pszText = szDispText;
2964 lvItem.cchTextMax = DISP_TEXT_SIZE;
2965 if (LISTVIEW_GetItemW(infoPtr, &lvItem))
2966 nItemWidth = max(LISTVIEW_GetStringWidthT(infoPtr, lvItem.pszText, TRUE),
2967 nItemWidth);
2970 if (infoPtr->himlSmall) nItemWidth += infoPtr->iconSize.cx;
2971 if (infoPtr->himlState) nItemWidth += infoPtr->iconStateSize.cx;
2973 nItemWidth = max(DEFAULT_COLUMN_WIDTH, nItemWidth + WIDTH_PADDING);
2976 return nItemWidth;
2979 /***
2980 * DESCRIPTION:
2981 * Calculates the desired item height.
2983 * PARAMETER(S):
2984 * [I] infoPtr : valid pointer to the listview structure
2986 * RETURN:
2987 * The desired item height.
2989 static INT LISTVIEW_CalculateItemHeight(const LISTVIEW_INFO *infoPtr)
2991 INT nItemHeight;
2993 TRACE("view %ld\n", infoPtr->uView);
2995 if (infoPtr->uView == LV_VIEW_ICON)
2996 nItemHeight = infoPtr->iconSpacing.cy;
2997 else
2999 nItemHeight = infoPtr->ntmHeight;
3000 if (infoPtr->himlState)
3001 nItemHeight = max(nItemHeight, infoPtr->iconStateSize.cy);
3002 if (infoPtr->himlSmall)
3003 nItemHeight = max(nItemHeight, infoPtr->iconSize.cy);
3004 nItemHeight += HEIGHT_PADDING;
3005 if (infoPtr->nMeasureItemHeight > 0)
3006 nItemHeight = infoPtr->nMeasureItemHeight;
3009 return max(nItemHeight, 1);
3012 /***
3013 * DESCRIPTION:
3014 * Updates the width, and height of an item.
3016 * PARAMETER(S):
3017 * [I] infoPtr : valid pointer to the listview structure
3019 * RETURN:
3020 * None.
3022 static inline void LISTVIEW_UpdateItemSize(LISTVIEW_INFO *infoPtr)
3024 infoPtr->nItemWidth = LISTVIEW_CalculateItemWidth(infoPtr);
3025 infoPtr->nItemHeight = LISTVIEW_CalculateItemHeight(infoPtr);
3029 /***
3030 * DESCRIPTION:
3031 * Retrieves and saves important text metrics info for the current
3032 * Listview font.
3034 * PARAMETER(S):
3035 * [I] infoPtr : valid pointer to the listview structure
3038 static void LISTVIEW_SaveTextMetrics(LISTVIEW_INFO *infoPtr)
3040 HDC hdc = GetDC(infoPtr->hwndSelf);
3041 HFONT hFont = infoPtr->hFont ? infoPtr->hFont : infoPtr->hDefaultFont;
3042 HFONT hOldFont = SelectObject(hdc, hFont);
3043 TEXTMETRICW tm;
3044 SIZE sz;
3046 if (GetTextMetricsW(hdc, &tm))
3048 infoPtr->ntmHeight = tm.tmHeight;
3049 infoPtr->ntmMaxCharWidth = tm.tmMaxCharWidth;
3052 if (GetTextExtentPoint32A(hdc, "...", 3, &sz))
3053 infoPtr->nEllipsisWidth = sz.cx;
3055 SelectObject(hdc, hOldFont);
3056 ReleaseDC(infoPtr->hwndSelf, hdc);
3058 TRACE("tmHeight=%d\n", infoPtr->ntmHeight);
3061 /***
3062 * DESCRIPTION:
3063 * A compare function for ranges
3065 * PARAMETER(S)
3066 * [I] range1 : pointer to range 1;
3067 * [I] range2 : pointer to range 2;
3068 * [I] flags : flags
3070 * RETURNS:
3071 * > 0 : if range 1 > range 2
3072 * < 0 : if range 2 > range 1
3073 * = 0 : if range intersects range 2
3075 static INT CALLBACK ranges_cmp(LPVOID range1, LPVOID range2, LPARAM flags)
3077 INT cmp;
3079 if (((RANGE*)range1)->upper <= ((RANGE*)range2)->lower)
3080 cmp = -1;
3081 else if (((RANGE*)range2)->upper <= ((RANGE*)range1)->lower)
3082 cmp = 1;
3083 else
3084 cmp = 0;
3086 TRACE("range1=%s, range2=%s, cmp=%d\n", debugrange(range1), debugrange(range2), cmp);
3088 return cmp;
3091 #define ranges_check(ranges, desc) if (TRACE_ON(listview)) ranges_assert(ranges, desc, __FILE__, __LINE__)
3093 static void ranges_assert(RANGES ranges, LPCSTR desc, const char *file, int line)
3095 INT i;
3096 RANGE *prev, *curr;
3098 TRACE("*** Checking %s:%d:%s ***\n", file, line, desc);
3099 assert (ranges);
3100 assert (DPA_GetPtrCount(ranges->hdpa) >= 0);
3101 ranges_dump(ranges);
3102 if (DPA_GetPtrCount(ranges->hdpa) > 0)
3104 prev = DPA_GetPtr(ranges->hdpa, 0);
3105 assert (prev->lower >= 0 && prev->lower < prev->upper);
3106 for (i = 1; i < DPA_GetPtrCount(ranges->hdpa); i++)
3108 curr = DPA_GetPtr(ranges->hdpa, i);
3109 assert (prev->upper <= curr->lower);
3110 assert (curr->lower < curr->upper);
3111 prev = curr;
3114 TRACE("--- Done checking---\n");
3117 static RANGES ranges_create(int count)
3119 RANGES ranges = Alloc(sizeof(struct tagRANGES));
3120 if (!ranges) return NULL;
3121 ranges->hdpa = DPA_Create(count);
3122 if (ranges->hdpa) return ranges;
3123 Free(ranges);
3124 return NULL;
3127 static void ranges_clear(RANGES ranges)
3129 INT i;
3131 for(i = 0; i < DPA_GetPtrCount(ranges->hdpa); i++)
3132 Free(DPA_GetPtr(ranges->hdpa, i));
3133 DPA_DeleteAllPtrs(ranges->hdpa);
3137 static void ranges_destroy(RANGES ranges)
3139 if (!ranges) return;
3140 ranges_clear(ranges);
3141 DPA_Destroy(ranges->hdpa);
3142 Free(ranges);
3145 static RANGES ranges_clone(RANGES ranges)
3147 RANGES clone;
3148 INT i;
3150 if (!(clone = ranges_create(DPA_GetPtrCount(ranges->hdpa)))) goto fail;
3152 for (i = 0; i < DPA_GetPtrCount(ranges->hdpa); i++)
3154 RANGE *newrng = Alloc(sizeof(RANGE));
3155 if (!newrng) goto fail;
3156 *newrng = *((RANGE*)DPA_GetPtr(ranges->hdpa, i));
3157 if (!DPA_SetPtr(clone->hdpa, i, newrng))
3159 Free(newrng);
3160 goto fail;
3163 return clone;
3165 fail:
3166 TRACE ("clone failed\n");
3167 ranges_destroy(clone);
3168 return NULL;
3171 static RANGES ranges_diff(RANGES ranges, RANGES sub)
3173 INT i;
3175 for (i = 0; i < DPA_GetPtrCount(sub->hdpa); i++)
3176 ranges_del(ranges, *((RANGE *)DPA_GetPtr(sub->hdpa, i)));
3178 return ranges;
3181 static void ranges_dump(RANGES ranges)
3183 INT i;
3185 for (i = 0; i < DPA_GetPtrCount(ranges->hdpa); i++)
3186 TRACE(" %s\n", debugrange(DPA_GetPtr(ranges->hdpa, i)));
3189 static inline BOOL ranges_contain(RANGES ranges, INT nItem)
3191 RANGE srchrng = { nItem, nItem + 1 };
3193 TRACE("(nItem=%d)\n", nItem);
3194 ranges_check(ranges, "before contain");
3195 return DPA_Search(ranges->hdpa, &srchrng, 0, ranges_cmp, 0, DPAS_SORTED) != -1;
3198 static INT ranges_itemcount(RANGES ranges)
3200 INT i, count = 0;
3202 for (i = 0; i < DPA_GetPtrCount(ranges->hdpa); i++)
3204 RANGE *sel = DPA_GetPtr(ranges->hdpa, i);
3205 count += sel->upper - sel->lower;
3208 return count;
3211 static BOOL ranges_shift(RANGES ranges, INT nItem, INT delta, INT nUpper)
3213 RANGE srchrng = { nItem, nItem + 1 }, *chkrng;
3214 INT index;
3216 index = DPA_Search(ranges->hdpa, &srchrng, 0, ranges_cmp, 0, DPAS_SORTED | DPAS_INSERTAFTER);
3217 if (index == -1) return TRUE;
3219 for (; index < DPA_GetPtrCount(ranges->hdpa); index++)
3221 chkrng = DPA_GetPtr(ranges->hdpa, index);
3222 if (chkrng->lower >= nItem)
3223 chkrng->lower = max(min(chkrng->lower + delta, nUpper - 1), 0);
3224 if (chkrng->upper > nItem)
3225 chkrng->upper = max(min(chkrng->upper + delta, nUpper), 0);
3227 return TRUE;
3230 static BOOL ranges_add(RANGES ranges, RANGE range)
3232 RANGE srchrgn;
3233 INT index;
3235 TRACE("(%s)\n", debugrange(&range));
3236 ranges_check(ranges, "before add");
3238 /* try find overlapping regions first */
3239 srchrgn.lower = range.lower - 1;
3240 srchrgn.upper = range.upper + 1;
3241 index = DPA_Search(ranges->hdpa, &srchrgn, 0, ranges_cmp, 0, DPAS_SORTED);
3243 if (index == -1)
3245 RANGE *newrgn;
3247 TRACE("Adding new range\n");
3249 /* create the brand new range to insert */
3250 newrgn = Alloc(sizeof(RANGE));
3251 if(!newrgn) goto fail;
3252 *newrgn = range;
3254 /* figure out where to insert it */
3255 index = DPA_Search(ranges->hdpa, newrgn, 0, ranges_cmp, 0, DPAS_SORTED | DPAS_INSERTAFTER);
3256 TRACE("index=%d\n", index);
3257 if (index == -1) index = 0;
3259 /* and get it over with */
3260 if (DPA_InsertPtr(ranges->hdpa, index, newrgn) == -1)
3262 Free(newrgn);
3263 goto fail;
3266 else
3268 RANGE *chkrgn, *mrgrgn;
3269 INT fromindex, mergeindex;
3271 chkrgn = DPA_GetPtr(ranges->hdpa, index);
3272 TRACE("Merge with %s @%d\n", debugrange(chkrgn), index);
3274 chkrgn->lower = min(range.lower, chkrgn->lower);
3275 chkrgn->upper = max(range.upper, chkrgn->upper);
3277 TRACE("New range %s @%d\n", debugrange(chkrgn), index);
3279 /* merge now common ranges */
3280 fromindex = 0;
3281 srchrgn.lower = chkrgn->lower - 1;
3282 srchrgn.upper = chkrgn->upper + 1;
3286 mergeindex = DPA_Search(ranges->hdpa, &srchrgn, fromindex, ranges_cmp, 0, 0);
3287 if (mergeindex == -1) break;
3288 if (mergeindex == index)
3290 fromindex = index + 1;
3291 continue;
3294 TRACE("Merge with index %i\n", mergeindex);
3296 mrgrgn = DPA_GetPtr(ranges->hdpa, mergeindex);
3297 chkrgn->lower = min(chkrgn->lower, mrgrgn->lower);
3298 chkrgn->upper = max(chkrgn->upper, mrgrgn->upper);
3299 Free(mrgrgn);
3300 DPA_DeletePtr(ranges->hdpa, mergeindex);
3301 if (mergeindex < index) index --;
3302 } while(1);
3305 ranges_check(ranges, "after add");
3306 return TRUE;
3308 fail:
3309 ranges_check(ranges, "failed add");
3310 return FALSE;
3313 static BOOL ranges_del(RANGES ranges, RANGE range)
3315 RANGE *chkrgn;
3316 INT index;
3318 TRACE("(%s)\n", debugrange(&range));
3319 ranges_check(ranges, "before del");
3321 /* we don't use DPAS_SORTED here, since we need *
3322 * to find the first overlapping range */
3323 index = DPA_Search(ranges->hdpa, &range, 0, ranges_cmp, 0, 0);
3324 while(index != -1)
3326 chkrgn = DPA_GetPtr(ranges->hdpa, index);
3328 TRACE("Matches range %s @%d\n", debugrange(chkrgn), index);
3330 /* case 1: Same range */
3331 if ( (chkrgn->upper == range.upper) &&
3332 (chkrgn->lower == range.lower) )
3334 DPA_DeletePtr(ranges->hdpa, index);
3335 Free(chkrgn);
3336 break;
3338 /* case 2: engulf */
3339 else if ( (chkrgn->upper <= range.upper) &&
3340 (chkrgn->lower >= range.lower) )
3342 DPA_DeletePtr(ranges->hdpa, index);
3343 Free(chkrgn);
3345 /* case 3: overlap upper */
3346 else if ( (chkrgn->upper <= range.upper) &&
3347 (chkrgn->lower < range.lower) )
3349 chkrgn->upper = range.lower;
3351 /* case 4: overlap lower */
3352 else if ( (chkrgn->upper > range.upper) &&
3353 (chkrgn->lower >= range.lower) )
3355 chkrgn->lower = range.upper;
3356 break;
3358 /* case 5: fully internal */
3359 else
3361 RANGE *newrgn;
3363 if (!(newrgn = Alloc(sizeof(RANGE)))) goto fail;
3364 newrgn->lower = chkrgn->lower;
3365 newrgn->upper = range.lower;
3366 chkrgn->lower = range.upper;
3367 if (DPA_InsertPtr(ranges->hdpa, index, newrgn) == -1)
3369 Free(newrgn);
3370 goto fail;
3372 break;
3375 index = DPA_Search(ranges->hdpa, &range, index, ranges_cmp, 0, 0);
3378 ranges_check(ranges, "after del");
3379 return TRUE;
3381 fail:
3382 ranges_check(ranges, "failed del");
3383 return FALSE;
3386 /***
3387 * DESCRIPTION:
3388 * Removes all selection ranges
3390 * Parameters(s):
3391 * [I] infoPtr : valid pointer to the listview structure
3392 * [I] toSkip : item range to skip removing the selection
3394 * RETURNS:
3395 * SUCCESS : TRUE
3396 * FAILURE : FALSE
3398 static BOOL LISTVIEW_DeselectAllSkipItems(LISTVIEW_INFO *infoPtr, RANGES toSkip)
3400 LVITEMW lvItem;
3401 ITERATOR i;
3402 RANGES clone;
3404 TRACE("()\n");
3406 lvItem.state = 0;
3407 lvItem.stateMask = LVIS_SELECTED;
3409 /* need to clone the DPA because callbacks can change it */
3410 if (!(clone = ranges_clone(infoPtr->selectionRanges))) return FALSE;
3411 iterator_rangesitems(&i, ranges_diff(clone, toSkip));
3412 while(iterator_next(&i))
3413 LISTVIEW_SetItemState(infoPtr, i.nItem, &lvItem);
3414 /* note that the iterator destructor will free the cloned range */
3415 iterator_destroy(&i);
3417 return TRUE;
3420 static inline BOOL LISTVIEW_DeselectAllSkipItem(LISTVIEW_INFO *infoPtr, INT nItem)
3422 RANGES toSkip;
3424 if (!(toSkip = ranges_create(1))) return FALSE;
3425 if (nItem != -1) ranges_additem(toSkip, nItem);
3426 LISTVIEW_DeselectAllSkipItems(infoPtr, toSkip);
3427 ranges_destroy(toSkip);
3428 return TRUE;
3431 static inline BOOL LISTVIEW_DeselectAll(LISTVIEW_INFO *infoPtr)
3433 return LISTVIEW_DeselectAllSkipItem(infoPtr, -1);
3436 /***
3437 * DESCRIPTION:
3438 * Retrieves the number of items that are marked as selected.
3440 * PARAMETER(S):
3441 * [I] infoPtr : valid pointer to the listview structure
3443 * RETURN:
3444 * Number of items selected.
3446 static INT LISTVIEW_GetSelectedCount(const LISTVIEW_INFO *infoPtr)
3448 INT nSelectedCount = 0;
3450 if (infoPtr->uCallbackMask & LVIS_SELECTED)
3452 INT i;
3453 for (i = 0; i < infoPtr->nItemCount; i++)
3455 if (LISTVIEW_GetItemState(infoPtr, i, LVIS_SELECTED))
3456 nSelectedCount++;
3459 else
3460 nSelectedCount = ranges_itemcount(infoPtr->selectionRanges);
3462 TRACE("nSelectedCount=%d\n", nSelectedCount);
3463 return nSelectedCount;
3466 /***
3467 * DESCRIPTION:
3468 * Manages the item focus.
3470 * PARAMETER(S):
3471 * [I] infoPtr : valid pointer to the listview structure
3472 * [I] nItem : item index
3474 * RETURN:
3475 * TRUE : focused item changed
3476 * FALSE : focused item has NOT changed
3478 static inline BOOL LISTVIEW_SetItemFocus(LISTVIEW_INFO *infoPtr, INT nItem)
3480 INT oldFocus = infoPtr->nFocusedItem;
3481 LVITEMW lvItem;
3483 if (nItem == infoPtr->nFocusedItem) return FALSE;
3485 lvItem.state = nItem == -1 ? 0 : LVIS_FOCUSED;
3486 lvItem.stateMask = LVIS_FOCUSED;
3487 LISTVIEW_SetItemState(infoPtr, nItem == -1 ? infoPtr->nFocusedItem : nItem, &lvItem);
3489 return oldFocus != infoPtr->nFocusedItem;
3492 static INT shift_item(const LISTVIEW_INFO *infoPtr, INT nShiftItem, INT nItem, INT direction)
3494 if (nShiftItem < nItem) return nShiftItem;
3496 if (nShiftItem > nItem) return nShiftItem + direction;
3498 if (direction > 0) return nShiftItem + direction;
3500 return min(nShiftItem, infoPtr->nItemCount - 1);
3503 /* This function updates focus index.
3505 Parameters:
3506 focus : current focus index
3507 item : index of item to be added/removed
3508 direction : add/remove flag
3510 static void LISTVIEW_ShiftFocus(LISTVIEW_INFO *infoPtr, INT focus, INT item, INT direction)
3512 DWORD old_mask = infoPtr->notify_mask & NOTIFY_MASK_ITEM_CHANGE;
3514 infoPtr->notify_mask &= ~NOTIFY_MASK_ITEM_CHANGE;
3515 focus = shift_item(infoPtr, focus, item, direction);
3516 if (focus != infoPtr->nFocusedItem)
3517 LISTVIEW_SetItemFocus(infoPtr, focus);
3518 infoPtr->notify_mask |= old_mask;
3522 * DESCRIPTION:
3523 * Updates the various indices after an item has been inserted or deleted.
3525 * PARAMETER(S):
3526 * [I] infoPtr : valid pointer to the listview structure
3527 * [I] nItem : item index
3528 * [I] direction : Direction of shift, +1 or -1.
3530 * RETURN:
3531 * None
3533 static void LISTVIEW_ShiftIndices(LISTVIEW_INFO *infoPtr, INT nItem, INT direction)
3535 TRACE("Shifting %i, %i steps\n", nItem, direction);
3537 ranges_shift(infoPtr->selectionRanges, nItem, direction, infoPtr->nItemCount);
3538 assert(abs(direction) == 1);
3539 infoPtr->nSelectionMark = shift_item(infoPtr, infoPtr->nSelectionMark, nItem, direction);
3541 /* But we are not supposed to modify nHotItem! */
3545 * DESCRIPTION:
3546 * Adds a block of selections.
3548 * PARAMETER(S):
3549 * [I] infoPtr : valid pointer to the listview structure
3550 * [I] nItem : item index
3552 * RETURN:
3553 * Whether the window is still valid.
3555 static BOOL LISTVIEW_AddGroupSelection(LISTVIEW_INFO *infoPtr, INT nItem)
3557 INT nFirst = min(infoPtr->nSelectionMark, nItem);
3558 INT nLast = max(infoPtr->nSelectionMark, nItem);
3559 HWND hwndSelf = infoPtr->hwndSelf;
3560 NMLVODSTATECHANGE nmlv;
3561 DWORD old_mask;
3562 LVITEMW item;
3563 INT i;
3565 /* Temporarily disable change notification
3566 * If the control is LVS_OWNERDATA, we need to send
3567 * only one LVN_ODSTATECHANGED notification.
3568 * See MSDN documentation for LVN_ITEMCHANGED.
3570 old_mask = infoPtr->notify_mask & NOTIFY_MASK_ITEM_CHANGE;
3571 if (infoPtr->dwStyle & LVS_OWNERDATA)
3572 infoPtr->notify_mask &= ~NOTIFY_MASK_ITEM_CHANGE;
3574 if (nFirst == -1) nFirst = nItem;
3576 item.state = LVIS_SELECTED;
3577 item.stateMask = LVIS_SELECTED;
3579 for (i = nFirst; i <= nLast; i++)
3580 LISTVIEW_SetItemState(infoPtr,i,&item);
3582 ZeroMemory(&nmlv, sizeof(nmlv));
3583 nmlv.iFrom = nFirst;
3584 nmlv.iTo = nLast;
3585 nmlv.uOldState = 0;
3586 nmlv.uNewState = item.state;
3588 notify_hdr(infoPtr, LVN_ODSTATECHANGED, (LPNMHDR)&nmlv);
3589 if (!IsWindow(hwndSelf))
3590 return FALSE;
3591 infoPtr->notify_mask |= old_mask;
3592 return TRUE;
3596 /***
3597 * DESCRIPTION:
3598 * Sets a single group selection.
3600 * PARAMETER(S):
3601 * [I] infoPtr : valid pointer to the listview structure
3602 * [I] nItem : item index
3604 * RETURN:
3605 * None
3607 static void LISTVIEW_SetGroupSelection(LISTVIEW_INFO *infoPtr, INT nItem)
3609 RANGES selection;
3610 DWORD old_mask;
3611 LVITEMW item;
3612 ITERATOR i;
3614 if (!(selection = ranges_create(100))) return;
3616 item.state = LVIS_SELECTED;
3617 item.stateMask = LVIS_SELECTED;
3619 if ((infoPtr->uView == LV_VIEW_LIST) || (infoPtr->uView == LV_VIEW_DETAILS))
3621 if (infoPtr->nSelectionMark == -1)
3623 infoPtr->nSelectionMark = nItem;
3624 ranges_additem(selection, nItem);
3626 else
3628 RANGE sel;
3630 sel.lower = min(infoPtr->nSelectionMark, nItem);
3631 sel.upper = max(infoPtr->nSelectionMark, nItem) + 1;
3632 ranges_add(selection, sel);
3635 else
3637 RECT rcItem, rcSel, rcSelMark;
3638 POINT ptItem;
3640 rcItem.left = LVIR_BOUNDS;
3641 if (!LISTVIEW_GetItemRect(infoPtr, nItem, &rcItem)) {
3642 ranges_destroy (selection);
3643 return;
3645 rcSelMark.left = LVIR_BOUNDS;
3646 if (!LISTVIEW_GetItemRect(infoPtr, infoPtr->nSelectionMark, &rcSelMark)) {
3647 ranges_destroy (selection);
3648 return;
3650 UnionRect(&rcSel, &rcItem, &rcSelMark);
3651 iterator_frameditems(&i, infoPtr, &rcSel);
3652 while(iterator_next(&i))
3654 LISTVIEW_GetItemPosition(infoPtr, i.nItem, &ptItem);
3655 if (PtInRect(&rcSel, ptItem)) ranges_additem(selection, i.nItem);
3657 iterator_destroy(&i);
3660 /* disable per item notifications on LVS_OWNERDATA style
3661 FIXME: single LVN_ODSTATECHANGED should be used */
3662 old_mask = infoPtr->notify_mask & NOTIFY_MASK_ITEM_CHANGE;
3663 if (infoPtr->dwStyle & LVS_OWNERDATA)
3664 infoPtr->notify_mask &= ~NOTIFY_MASK_ITEM_CHANGE;
3666 LISTVIEW_DeselectAllSkipItems(infoPtr, selection);
3669 iterator_rangesitems(&i, selection);
3670 while(iterator_next(&i))
3671 LISTVIEW_SetItemState(infoPtr, i.nItem, &item);
3672 /* this will also destroy the selection */
3673 iterator_destroy(&i);
3675 infoPtr->notify_mask |= old_mask;
3676 LISTVIEW_SetItemFocus(infoPtr, nItem);
3679 /***
3680 * DESCRIPTION:
3681 * Sets a single selection.
3683 * PARAMETER(S):
3684 * [I] infoPtr : valid pointer to the listview structure
3685 * [I] nItem : item index
3687 * RETURN:
3688 * None
3690 static void LISTVIEW_SetSelection(LISTVIEW_INFO *infoPtr, INT nItem)
3692 LVITEMW lvItem;
3694 TRACE("nItem=%d\n", nItem);
3696 LISTVIEW_DeselectAllSkipItem(infoPtr, nItem);
3698 lvItem.state = LVIS_FOCUSED | LVIS_SELECTED;
3699 lvItem.stateMask = LVIS_FOCUSED | LVIS_SELECTED;
3700 LISTVIEW_SetItemState(infoPtr, nItem, &lvItem);
3702 infoPtr->nSelectionMark = nItem;
3705 /***
3706 * DESCRIPTION:
3707 * Set selection(s) with keyboard.
3709 * PARAMETER(S):
3710 * [I] infoPtr : valid pointer to the listview structure
3711 * [I] nItem : item index
3712 * [I] space : VK_SPACE code sent
3714 * RETURN:
3715 * SUCCESS : TRUE (needs to be repainted)
3716 * FAILURE : FALSE (nothing has changed)
3718 static BOOL LISTVIEW_KeySelection(LISTVIEW_INFO *infoPtr, INT nItem, BOOL space)
3720 /* FIXME: pass in the state */
3721 WORD wShift = GetKeyState(VK_SHIFT) & 0x8000;
3722 WORD wCtrl = GetKeyState(VK_CONTROL) & 0x8000;
3723 BOOL bResult = FALSE;
3725 TRACE("nItem=%d, wShift=%d, wCtrl=%d\n", nItem, wShift, wCtrl);
3726 if ((nItem >= 0) && (nItem < infoPtr->nItemCount))
3728 bResult = TRUE;
3730 if (infoPtr->dwStyle & LVS_SINGLESEL || (wShift == 0 && wCtrl == 0))
3731 LISTVIEW_SetSelection(infoPtr, nItem);
3732 else
3734 if (wShift)
3735 LISTVIEW_SetGroupSelection(infoPtr, nItem);
3736 else if (wCtrl)
3738 LVITEMW lvItem;
3739 lvItem.state = ~LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED);
3740 lvItem.stateMask = LVIS_SELECTED;
3741 if (space)
3743 LISTVIEW_SetItemState(infoPtr, nItem, &lvItem);
3744 if (lvItem.state & LVIS_SELECTED)
3745 infoPtr->nSelectionMark = nItem;
3747 bResult = LISTVIEW_SetItemFocus(infoPtr, nItem);
3750 LISTVIEW_EnsureVisible(infoPtr, nItem, FALSE);
3753 UpdateWindow(infoPtr->hwndSelf); /* update client area */
3754 return bResult;
3757 static BOOL LISTVIEW_GetItemAtPt(const LISTVIEW_INFO *infoPtr, LPLVITEMW lpLVItem, POINT pt)
3759 LVHITTESTINFO lvHitTestInfo;
3761 ZeroMemory(&lvHitTestInfo, sizeof(lvHitTestInfo));
3762 lvHitTestInfo.pt.x = pt.x;
3763 lvHitTestInfo.pt.y = pt.y;
3765 LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, FALSE);
3767 lpLVItem->mask = LVIF_PARAM;
3768 lpLVItem->iItem = lvHitTestInfo.iItem;
3769 lpLVItem->iSubItem = 0;
3771 return LISTVIEW_GetItemT(infoPtr, lpLVItem, TRUE);
3774 static inline BOOL LISTVIEW_IsHotTracking(const LISTVIEW_INFO *infoPtr)
3776 return ((infoPtr->dwLvExStyle & LVS_EX_TRACKSELECT) ||
3777 (infoPtr->dwLvExStyle & LVS_EX_ONECLICKACTIVATE) ||
3778 (infoPtr->dwLvExStyle & LVS_EX_TWOCLICKACTIVATE));
3781 /***
3782 * DESCRIPTION:
3783 * Called when the mouse is being actively tracked and has hovered for a specified
3784 * amount of time
3786 * PARAMETER(S):
3787 * [I] infoPtr : valid pointer to the listview structure
3788 * [I] fwKeys : key indicator
3789 * [I] x,y : mouse position
3791 * RETURN:
3792 * 0 if the message was processed, non-zero if there was an error
3794 * INFO:
3795 * LVS_EX_TRACKSELECT: An item is automatically selected when the cursor remains
3796 * over the item for a certain period of time.
3799 static LRESULT LISTVIEW_MouseHover(LISTVIEW_INFO *infoPtr, INT x, INT y)
3801 NMHDR hdr;
3803 if (notify_hdr(infoPtr, NM_HOVER, &hdr)) return 0;
3805 if (LISTVIEW_IsHotTracking(infoPtr))
3807 LVITEMW item;
3808 POINT pt;
3810 pt.x = x;
3811 pt.y = y;
3813 if (LISTVIEW_GetItemAtPt(infoPtr, &item, pt))
3814 LISTVIEW_SetSelection(infoPtr, item.iItem);
3816 SetFocus(infoPtr->hwndSelf);
3819 return 0;
3822 #define SCROLL_LEFT 0x1
3823 #define SCROLL_RIGHT 0x2
3824 #define SCROLL_UP 0x4
3825 #define SCROLL_DOWN 0x8
3827 /***
3828 * DESCRIPTION:
3829 * Utility routine to draw and highlight items within a marquee selection rectangle.
3831 * PARAMETER(S):
3832 * [I] infoPtr : valid pointer to the listview structure
3833 * [I] coords_orig : original co-ordinates of the cursor
3834 * [I] coords_offs : offsetted coordinates of the cursor
3835 * [I] offset : offset amount
3836 * [I] scroll : Bitmask of which directions we should scroll, if at all
3838 * RETURN:
3839 * None.
3841 static void LISTVIEW_MarqueeHighlight(LISTVIEW_INFO *infoPtr, const POINT *coords_orig,
3842 INT scroll)
3844 BOOL controlDown = FALSE;
3845 LVITEMW item;
3846 ITERATOR old_elems, new_elems;
3847 RECT rect;
3848 POINT coords_offs, offset;
3850 /* Ensure coordinates are within client bounds */
3851 coords_offs.x = max(min(coords_orig->x, infoPtr->rcList.right), 0);
3852 coords_offs.y = max(min(coords_orig->y, infoPtr->rcList.bottom), 0);
3854 /* Get offset */
3855 LISTVIEW_GetOrigin(infoPtr, &offset);
3857 /* Offset coordinates by the appropriate amount */
3858 coords_offs.x -= offset.x;
3859 coords_offs.y -= offset.y;
3861 if (coords_offs.x > infoPtr->marqueeOrigin.x)
3863 rect.left = infoPtr->marqueeOrigin.x;
3864 rect.right = coords_offs.x;
3866 else
3868 rect.left = coords_offs.x;
3869 rect.right = infoPtr->marqueeOrigin.x;
3872 if (coords_offs.y > infoPtr->marqueeOrigin.y)
3874 rect.top = infoPtr->marqueeOrigin.y;
3875 rect.bottom = coords_offs.y;
3877 else
3879 rect.top = coords_offs.y;
3880 rect.bottom = infoPtr->marqueeOrigin.y;
3883 /* Cancel out the old marquee rectangle and draw the new one */
3884 LISTVIEW_InvalidateRect(infoPtr, &infoPtr->marqueeDrawRect);
3886 /* Scroll by the appropriate distance if applicable - speed up scrolling as
3887 the cursor is further away */
3889 if ((scroll & SCROLL_LEFT) && (coords_orig->x <= 0))
3890 LISTVIEW_Scroll(infoPtr, coords_orig->x, 0);
3892 if ((scroll & SCROLL_RIGHT) && (coords_orig->x >= infoPtr->rcList.right))
3893 LISTVIEW_Scroll(infoPtr, (coords_orig->x - infoPtr->rcList.right), 0);
3895 if ((scroll & SCROLL_UP) && (coords_orig->y <= 0))
3896 LISTVIEW_Scroll(infoPtr, 0, coords_orig->y);
3898 if ((scroll & SCROLL_DOWN) && (coords_orig->y >= infoPtr->rcList.bottom))
3899 LISTVIEW_Scroll(infoPtr, 0, (coords_orig->y - infoPtr->rcList.bottom));
3901 iterator_frameditems_absolute(&old_elems, infoPtr, &infoPtr->marqueeRect);
3903 infoPtr->marqueeRect = rect;
3904 infoPtr->marqueeDrawRect = rect;
3905 OffsetRect(&infoPtr->marqueeDrawRect, offset.x, offset.y);
3907 iterator_frameditems_absolute(&new_elems, infoPtr, &infoPtr->marqueeRect);
3908 iterator_remove_common_items(&old_elems, &new_elems);
3910 /* Iterate over no longer selected items */
3911 while (iterator_next(&old_elems))
3913 if (old_elems.nItem > -1)
3915 if (LISTVIEW_GetItemState(infoPtr, old_elems.nItem, LVIS_SELECTED) == LVIS_SELECTED)
3916 item.state = 0;
3917 else
3918 item.state = LVIS_SELECTED;
3920 item.stateMask = LVIS_SELECTED;
3922 LISTVIEW_SetItemState(infoPtr, old_elems.nItem, &item);
3925 iterator_destroy(&old_elems);
3928 /* Iterate over newly selected items */
3929 if (GetKeyState(VK_CONTROL) & 0x8000)
3930 controlDown = TRUE;
3932 while (iterator_next(&new_elems))
3934 if (new_elems.nItem > -1)
3936 /* If CTRL is pressed, invert. If not, always select the item. */
3937 if ((controlDown) && (LISTVIEW_GetItemState(infoPtr, new_elems.nItem, LVIS_SELECTED)))
3938 item.state = 0;
3939 else
3940 item.state = LVIS_SELECTED;
3942 item.stateMask = LVIS_SELECTED;
3944 LISTVIEW_SetItemState(infoPtr, new_elems.nItem, &item);
3947 iterator_destroy(&new_elems);
3949 LISTVIEW_InvalidateRect(infoPtr, &infoPtr->marqueeDrawRect);
3952 /***
3953 * DESCRIPTION:
3954 * Called when we are in a marquee selection that involves scrolling the listview (ie,
3955 * the cursor is outside the bounds of the client area). This is a TIMERPROC.
3957 * PARAMETER(S):
3958 * [I] hwnd : Handle to the listview
3959 * [I] uMsg : WM_TIMER (ignored)
3960 * [I] idEvent : The timer ID interpreted as a pointer to a LISTVIEW_INFO struct
3961 * [I] dwTimer : The elapsed time (ignored)
3963 * RETURN:
3964 * None.
3966 static VOID CALLBACK LISTVIEW_ScrollTimer(HWND hWnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime)
3968 LISTVIEW_INFO *infoPtr;
3969 SCROLLINFO scrollInfo;
3970 POINT coords;
3971 INT scroll = 0;
3973 infoPtr = (LISTVIEW_INFO *) idEvent;
3975 if (!infoPtr)
3976 return;
3978 /* Get the current cursor position and convert to client coordinates */
3979 GetCursorPos(&coords);
3980 ScreenToClient(hWnd, &coords);
3982 scrollInfo.cbSize = sizeof(SCROLLINFO);
3983 scrollInfo.fMask = SIF_ALL;
3985 /* Work out in which directions we can scroll */
3986 if (GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo))
3988 if (scrollInfo.nPos != scrollInfo.nMin)
3989 scroll |= SCROLL_UP;
3991 if (((scrollInfo.nPage + scrollInfo.nPos) - 1) != scrollInfo.nMax)
3992 scroll |= SCROLL_DOWN;
3995 if (GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo))
3997 if (scrollInfo.nPos != scrollInfo.nMin)
3998 scroll |= SCROLL_LEFT;
4000 if (((scrollInfo.nPage + scrollInfo.nPos) - 1) != scrollInfo.nMax)
4001 scroll |= SCROLL_RIGHT;
4004 if (((coords.x <= 0) && (scroll & SCROLL_LEFT)) ||
4005 ((coords.y <= 0) && (scroll & SCROLL_UP)) ||
4006 ((coords.x >= infoPtr->rcList.right) && (scroll & SCROLL_RIGHT)) ||
4007 ((coords.y >= infoPtr->rcList.bottom) && (scroll & SCROLL_DOWN)))
4009 LISTVIEW_MarqueeHighlight(infoPtr, &coords, scroll);
4013 /***
4014 * DESCRIPTION:
4015 * Called whenever WM_MOUSEMOVE is received.
4017 * PARAMETER(S):
4018 * [I] infoPtr : valid pointer to the listview structure
4019 * [I] fwKeys : key indicator
4020 * [I] x,y : mouse position
4022 * RETURN:
4023 * 0 if the message is processed, non-zero if there was an error
4025 static LRESULT LISTVIEW_MouseMove(LISTVIEW_INFO *infoPtr, WORD fwKeys, INT x, INT y)
4027 LVHITTESTINFO ht;
4028 RECT rect;
4029 POINT pt;
4031 pt.x = x;
4032 pt.y = y;
4034 if (!(fwKeys & MK_LBUTTON))
4035 infoPtr->bLButtonDown = FALSE;
4037 if (infoPtr->bLButtonDown)
4039 rect.left = rect.right = infoPtr->ptClickPos.x;
4040 rect.top = rect.bottom = infoPtr->ptClickPos.y;
4042 InflateRect(&rect, GetSystemMetrics(SM_CXDRAG), GetSystemMetrics(SM_CYDRAG));
4044 if (infoPtr->bMarqueeSelect)
4046 /* Enable the timer if we're going outside our bounds, in case the user doesn't
4047 move the mouse again */
4049 if ((x <= 0) || (y <= 0) || (x >= infoPtr->rcList.right) ||
4050 (y >= infoPtr->rcList.bottom))
4052 if (!infoPtr->bScrolling)
4054 infoPtr->bScrolling = TRUE;
4055 SetTimer(infoPtr->hwndSelf, (UINT_PTR) infoPtr, 1, LISTVIEW_ScrollTimer);
4058 else
4060 infoPtr->bScrolling = FALSE;
4061 KillTimer(infoPtr->hwndSelf, (UINT_PTR) infoPtr);
4064 LISTVIEW_MarqueeHighlight(infoPtr, &pt, 0);
4065 return 0;
4068 ht.pt = pt;
4069 LISTVIEW_HitTest(infoPtr, &ht, TRUE, TRUE);
4071 /* reset item marker */
4072 if (infoPtr->nLButtonDownItem != ht.iItem)
4073 infoPtr->nLButtonDownItem = -1;
4075 if (!PtInRect(&rect, pt))
4077 /* this path covers the following:
4078 1. WM_LBUTTONDOWN over selected item (sets focus on it)
4079 2. change focus with keys
4080 3. move mouse over item from step 1 selects it and moves focus on it */
4081 if (infoPtr->nLButtonDownItem != -1 &&
4082 !LISTVIEW_GetItemState(infoPtr, infoPtr->nLButtonDownItem, LVIS_SELECTED))
4084 LVITEMW lvItem;
4086 lvItem.state = LVIS_FOCUSED | LVIS_SELECTED;
4087 lvItem.stateMask = LVIS_FOCUSED | LVIS_SELECTED;
4089 LISTVIEW_SetItemState(infoPtr, infoPtr->nLButtonDownItem, &lvItem);
4090 infoPtr->nLButtonDownItem = -1;
4093 if (!infoPtr->bDragging)
4095 ht.pt = infoPtr->ptClickPos;
4096 LISTVIEW_HitTest(infoPtr, &ht, TRUE, TRUE);
4098 /* If the click is outside the range of an item, begin a
4099 highlight. If not, begin an item drag. */
4100 if (ht.iItem == -1)
4102 NMHDR hdr;
4104 /* If we're allowing multiple selections, send notification.
4105 If return value is non-zero, cancel. */
4106 if (!(infoPtr->dwStyle & LVS_SINGLESEL) && (notify_hdr(infoPtr, LVN_MARQUEEBEGIN, &hdr) == 0))
4108 /* Store the absolute coordinates of the click */
4109 POINT offset;
4110 LISTVIEW_GetOrigin(infoPtr, &offset);
4112 infoPtr->marqueeOrigin.x = infoPtr->ptClickPos.x - offset.x;
4113 infoPtr->marqueeOrigin.y = infoPtr->ptClickPos.y - offset.y;
4115 /* Begin selection and capture mouse */
4116 infoPtr->bMarqueeSelect = TRUE;
4117 infoPtr->marqueeRect = rect;
4118 SetCapture(infoPtr->hwndSelf);
4121 else
4123 NMLISTVIEW nmlv;
4125 ZeroMemory(&nmlv, sizeof(nmlv));
4126 nmlv.iItem = ht.iItem;
4127 nmlv.ptAction = infoPtr->ptClickPos;
4129 notify_listview(infoPtr, LVN_BEGINDRAG, &nmlv);
4130 infoPtr->bDragging = TRUE;
4134 return 0;
4138 /* see if we are supposed to be tracking mouse hovering */
4139 if (LISTVIEW_IsHotTracking(infoPtr)) {
4140 TRACKMOUSEEVENT trackinfo;
4141 DWORD flags;
4143 trackinfo.cbSize = sizeof(TRACKMOUSEEVENT);
4144 trackinfo.dwFlags = TME_QUERY;
4146 /* see if we are already tracking this hwnd */
4147 _TrackMouseEvent(&trackinfo);
4149 flags = TME_LEAVE;
4150 if(infoPtr->dwLvExStyle & LVS_EX_TRACKSELECT)
4151 flags |= TME_HOVER;
4153 if((trackinfo.dwFlags & flags) != flags || trackinfo.hwndTrack != infoPtr->hwndSelf) {
4154 trackinfo.dwFlags = flags;
4155 trackinfo.dwHoverTime = infoPtr->dwHoverTime;
4156 trackinfo.hwndTrack = infoPtr->hwndSelf;
4158 /* call TRACKMOUSEEVENT so we receive WM_MOUSEHOVER messages */
4159 _TrackMouseEvent(&trackinfo);
4163 return 0;
4167 /***
4168 * Tests whether the item is assignable to a list with style lStyle
4170 static inline BOOL is_assignable_item(const LVITEMW *lpLVItem, LONG lStyle)
4172 if ( (lpLVItem->mask & LVIF_TEXT) &&
4173 (lpLVItem->pszText == LPSTR_TEXTCALLBACKW) &&
4174 (lStyle & (LVS_SORTASCENDING | LVS_SORTDESCENDING)) ) return FALSE;
4176 return TRUE;
4180 /***
4181 * DESCRIPTION:
4182 * Helper for LISTVIEW_SetItemT and LISTVIEW_InsertItemT: sets item attributes.
4184 * PARAMETER(S):
4185 * [I] infoPtr : valid pointer to the listview structure
4186 * [I] lpLVItem : valid pointer to new item attributes
4187 * [I] isNew : the item being set is being inserted
4188 * [I] isW : TRUE if lpLVItem is Unicode, FALSE if it's ANSI
4189 * [O] bChanged : will be set to TRUE if the item really changed
4191 * RETURN:
4192 * SUCCESS : TRUE
4193 * FAILURE : FALSE
4195 static BOOL set_main_item(LISTVIEW_INFO *infoPtr, const LVITEMW *lpLVItem, BOOL isNew, BOOL isW, BOOL *bChanged)
4197 ITEM_INFO *lpItem;
4198 NMLISTVIEW nmlv;
4199 UINT uChanged = 0;
4200 LVITEMW item;
4201 /* stateMask is ignored for LVM_INSERTITEM */
4202 UINT stateMask = isNew ? ~0 : lpLVItem->stateMask;
4203 BOOL send_change_notification;
4205 TRACE("()\n");
4207 assert(lpLVItem->iItem >= 0 && lpLVItem->iItem < infoPtr->nItemCount);
4209 if (lpLVItem->mask == 0) return TRUE;
4211 if (infoPtr->dwStyle & LVS_OWNERDATA)
4213 /* a virtual listview only stores selection and focus */
4214 if (lpLVItem->mask & ~LVIF_STATE)
4215 return FALSE;
4216 lpItem = NULL;
4218 else
4220 HDPA hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, lpLVItem->iItem);
4221 lpItem = DPA_GetPtr(hdpaSubItems, 0);
4222 assert (lpItem);
4225 /* we need to get the lParam and state of the item */
4226 item.iItem = lpLVItem->iItem;
4227 item.iSubItem = lpLVItem->iSubItem;
4228 item.mask = LVIF_STATE | LVIF_PARAM;
4229 item.stateMask = (infoPtr->dwStyle & LVS_OWNERDATA) ? LVIS_FOCUSED | LVIS_SELECTED : ~0;
4231 item.state = 0;
4232 item.lParam = 0;
4233 if (!isNew && !LISTVIEW_GetItemW(infoPtr, &item)) return FALSE;
4235 TRACE("oldState=%x, newState=%x\n", item.state, lpLVItem->state);
4236 /* determine what fields will change */
4237 if ((lpLVItem->mask & LVIF_STATE) && ((item.state ^ lpLVItem->state) & stateMask & ~infoPtr->uCallbackMask))
4238 uChanged |= LVIF_STATE;
4240 if ((lpLVItem->mask & LVIF_IMAGE) && (lpItem->hdr.iImage != lpLVItem->iImage))
4241 uChanged |= LVIF_IMAGE;
4243 if ((lpLVItem->mask & LVIF_PARAM) && (lpItem->lParam != lpLVItem->lParam))
4244 uChanged |= LVIF_PARAM;
4246 if ((lpLVItem->mask & LVIF_INDENT) && (lpItem->iIndent != lpLVItem->iIndent))
4247 uChanged |= LVIF_INDENT;
4249 if ((lpLVItem->mask & LVIF_TEXT) && textcmpWT(lpItem->hdr.pszText, lpLVItem->pszText, isW))
4250 uChanged |= LVIF_TEXT;
4252 TRACE("change mask=0x%x\n", uChanged);
4254 memset(&nmlv, 0, sizeof(NMLISTVIEW));
4255 nmlv.iItem = lpLVItem->iItem;
4256 if (lpLVItem->mask & LVIF_STATE)
4258 nmlv.uNewState = (item.state & ~stateMask) | (lpLVItem->state & stateMask);
4259 nmlv.uOldState = item.state;
4261 nmlv.uChanged = isNew ? LVIF_STATE : (uChanged ? uChanged : lpLVItem->mask);
4262 nmlv.lParam = item.lParam;
4264 /* Send change notification if the item is not being inserted, or inserted (selected|focused),
4265 and we are _NOT_ virtual (LVS_OWNERDATA), and change notifications
4266 are enabled. Even if nothing really changed we still need to send this,
4267 in this case uChanged mask is just set to passed item mask. */
4269 send_change_notification = !isNew;
4270 send_change_notification |= (uChanged & LVIF_STATE) && (lpLVItem->state & (LVIS_FOCUSED | LVIS_SELECTED));
4271 send_change_notification &= !!(infoPtr->notify_mask & NOTIFY_MASK_ITEM_CHANGE);
4273 if (lpItem && send_change_notification)
4275 HWND hwndSelf = infoPtr->hwndSelf;
4277 if (notify_listview(infoPtr, LVN_ITEMCHANGING, &nmlv))
4278 return FALSE;
4279 if (!IsWindow(hwndSelf))
4280 return FALSE;
4283 /* When item is inserted we need to shift existing focus index if new item has lower index. */
4284 if (isNew && (stateMask & ~infoPtr->uCallbackMask & LVIS_FOCUSED) &&
4285 /* this means we won't hit a focus change path later */
4286 ((uChanged & LVIF_STATE) == 0 || (!(lpLVItem->state & LVIS_FOCUSED) && (infoPtr->nFocusedItem != lpLVItem->iItem))))
4288 if (infoPtr->nFocusedItem != -1 && (lpLVItem->iItem <= infoPtr->nFocusedItem))
4289 infoPtr->nFocusedItem++;
4292 if (!uChanged) return TRUE;
4293 *bChanged = TRUE;
4295 /* copy information */
4296 if (lpLVItem->mask & LVIF_TEXT)
4297 textsetptrT(&lpItem->hdr.pszText, lpLVItem->pszText, isW);
4299 if (lpLVItem->mask & LVIF_IMAGE)
4300 lpItem->hdr.iImage = lpLVItem->iImage;
4302 if (lpLVItem->mask & LVIF_PARAM)
4303 lpItem->lParam = lpLVItem->lParam;
4305 if (lpLVItem->mask & LVIF_INDENT)
4306 lpItem->iIndent = lpLVItem->iIndent;
4308 if (uChanged & LVIF_STATE)
4310 if (lpItem && (stateMask & ~infoPtr->uCallbackMask))
4312 lpItem->state &= ~stateMask;
4313 lpItem->state |= (lpLVItem->state & stateMask);
4315 if (lpLVItem->state & stateMask & ~infoPtr->uCallbackMask & LVIS_SELECTED)
4317 if (infoPtr->dwStyle & LVS_SINGLESEL) LISTVIEW_DeselectAllSkipItem(infoPtr, lpLVItem->iItem);
4318 ranges_additem(infoPtr->selectionRanges, lpLVItem->iItem);
4320 else if (stateMask & LVIS_SELECTED)
4322 ranges_delitem(infoPtr->selectionRanges, lpLVItem->iItem);
4324 /* If we are asked to change focus, and we manage it, do it.
4325 It's important to have all new item data stored at this point,
4326 because changing existing focus could result in a redrawing operation,
4327 which in turn could ask for disp data, application should see all data
4328 for inserted item when processing LVN_GETDISPINFO.
4330 The way this works application will see nested item change notifications -
4331 changed item notifications interrupted by ones from item losing focus. */
4332 if (stateMask & ~infoPtr->uCallbackMask & LVIS_FOCUSED)
4334 if (lpLVItem->state & LVIS_FOCUSED)
4336 /* update selection mark */
4337 if (infoPtr->nFocusedItem == -1 && infoPtr->nSelectionMark == -1)
4338 infoPtr->nSelectionMark = lpLVItem->iItem;
4340 if (infoPtr->nFocusedItem != -1)
4342 /* remove current focus */
4343 item.mask = LVIF_STATE;
4344 item.state = 0;
4345 item.stateMask = LVIS_FOCUSED;
4347 /* recurse with redrawing an item */
4348 LISTVIEW_SetItemState(infoPtr, infoPtr->nFocusedItem, &item);
4351 infoPtr->nFocusedItem = lpLVItem->iItem;
4352 LISTVIEW_EnsureVisible(infoPtr, lpLVItem->iItem, infoPtr->uView == LV_VIEW_LIST);
4354 else if (infoPtr->nFocusedItem == lpLVItem->iItem)
4356 infoPtr->nFocusedItem = -1;
4361 if (send_change_notification)
4363 if (lpLVItem->mask & LVIF_PARAM) nmlv.lParam = lpLVItem->lParam;
4364 notify_listview(infoPtr, LVN_ITEMCHANGED, &nmlv);
4367 return TRUE;
4370 /***
4371 * DESCRIPTION:
4372 * Helper for LISTVIEW_{Set,Insert}ItemT *only*: sets subitem attributes.
4374 * PARAMETER(S):
4375 * [I] infoPtr : valid pointer to the listview structure
4376 * [I] lpLVItem : valid pointer to new subitem attributes
4377 * [I] isW : TRUE if lpLVItem is Unicode, FALSE if it's ANSI
4378 * [O] bChanged : will be set to TRUE if the item really changed
4380 * RETURN:
4381 * SUCCESS : TRUE
4382 * FAILURE : FALSE
4384 static BOOL set_sub_item(const LISTVIEW_INFO *infoPtr, const LVITEMW *lpLVItem, BOOL isW, BOOL *bChanged)
4386 HDPA hdpaSubItems;
4387 SUBITEM_INFO *lpSubItem;
4389 /* we do not support subitems for virtual listviews */
4390 if (infoPtr->dwStyle & LVS_OWNERDATA) return FALSE;
4392 /* set subitem only if column is present */
4393 if (lpLVItem->iSubItem >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return FALSE;
4395 /* First do some sanity checks */
4396 /* The LVIF_STATE flag is valid for subitems, but does not appear to be
4397 particularly useful. We currently do not actually do anything with
4398 the flag on subitems.
4400 if (lpLVItem->mask & ~(LVIF_TEXT | LVIF_IMAGE | LVIF_STATE | LVIF_DI_SETITEM)) return FALSE;
4401 if (!(lpLVItem->mask & (LVIF_TEXT | LVIF_IMAGE | LVIF_STATE))) return TRUE;
4403 /* get the subitem structure, and create it if not there */
4404 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, lpLVItem->iItem);
4405 assert (hdpaSubItems);
4407 lpSubItem = LISTVIEW_GetSubItemPtr(hdpaSubItems, lpLVItem->iSubItem);
4408 if (!lpSubItem)
4410 SUBITEM_INFO *tmpSubItem;
4411 INT i;
4413 lpSubItem = Alloc(sizeof(SUBITEM_INFO));
4414 if (!lpSubItem) return FALSE;
4415 /* we could binary search here, if need be...*/
4416 for (i = 1; i < DPA_GetPtrCount(hdpaSubItems); i++)
4418 tmpSubItem = DPA_GetPtr(hdpaSubItems, i);
4419 if (tmpSubItem->iSubItem > lpLVItem->iSubItem) break;
4421 if (DPA_InsertPtr(hdpaSubItems, i, lpSubItem) == -1)
4423 Free(lpSubItem);
4424 return FALSE;
4426 lpSubItem->iSubItem = lpLVItem->iSubItem;
4427 lpSubItem->hdr.iImage = I_IMAGECALLBACK;
4428 *bChanged = TRUE;
4431 if ((lpLVItem->mask & LVIF_IMAGE) && (lpSubItem->hdr.iImage != lpLVItem->iImage))
4433 lpSubItem->hdr.iImage = lpLVItem->iImage;
4434 *bChanged = TRUE;
4437 if ((lpLVItem->mask & LVIF_TEXT) && textcmpWT(lpSubItem->hdr.pszText, lpLVItem->pszText, isW))
4439 textsetptrT(&lpSubItem->hdr.pszText, lpLVItem->pszText, isW);
4440 *bChanged = TRUE;
4443 return TRUE;
4446 /***
4447 * DESCRIPTION:
4448 * Sets item attributes.
4450 * PARAMETER(S):
4451 * [I] infoPtr : valid pointer to the listview structure
4452 * [I] lpLVItem : new item attributes
4453 * [I] isW : TRUE if lpLVItem is Unicode, FALSE if it's ANSI
4455 * RETURN:
4456 * SUCCESS : TRUE
4457 * FAILURE : FALSE
4459 static BOOL LISTVIEW_SetItemT(LISTVIEW_INFO *infoPtr, LVITEMW *lpLVItem, BOOL isW)
4461 HWND hwndSelf = infoPtr->hwndSelf;
4462 LPWSTR pszText = NULL;
4463 BOOL bResult, bChanged = FALSE;
4464 RECT oldItemArea;
4466 TRACE("(lpLVItem=%s, isW=%d)\n", debuglvitem_t(lpLVItem, isW), isW);
4468 if (!lpLVItem || lpLVItem->iItem < 0 || lpLVItem->iItem >= infoPtr->nItemCount)
4469 return FALSE;
4471 /* Store old item area */
4472 LISTVIEW_GetItemBox(infoPtr, lpLVItem->iItem, &oldItemArea);
4474 /* For efficiency, we transform the lpLVItem->pszText to Unicode here */
4475 if ((lpLVItem->mask & LVIF_TEXT) && is_text(lpLVItem->pszText))
4477 pszText = lpLVItem->pszText;
4478 lpLVItem->pszText = textdupTtoW(lpLVItem->pszText, isW);
4481 /* actually set the fields */
4482 if (!is_assignable_item(lpLVItem, infoPtr->dwStyle)) return FALSE;
4484 if (lpLVItem->iSubItem)
4485 bResult = set_sub_item(infoPtr, lpLVItem, TRUE, &bChanged);
4486 else
4487 bResult = set_main_item(infoPtr, lpLVItem, FALSE, TRUE, &bChanged);
4488 if (!IsWindow(hwndSelf))
4489 return FALSE;
4491 /* redraw item, if necessary */
4492 if (bChanged && !infoPtr->bIsDrawing)
4494 /* this little optimization eliminates some nasty flicker */
4495 if ( infoPtr->uView == LV_VIEW_DETAILS && !(infoPtr->dwStyle & LVS_OWNERDRAWFIXED) &&
4496 !(infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT) &&
4497 lpLVItem->iSubItem > 0 && lpLVItem->iSubItem <= DPA_GetPtrCount(infoPtr->hdpaColumns) )
4498 LISTVIEW_InvalidateSubItem(infoPtr, lpLVItem->iItem, lpLVItem->iSubItem);
4499 else
4501 LISTVIEW_InvalidateRect(infoPtr, &oldItemArea);
4502 LISTVIEW_InvalidateItem(infoPtr, lpLVItem->iItem);
4505 /* restore text */
4506 if (pszText)
4508 textfreeT(lpLVItem->pszText, isW);
4509 lpLVItem->pszText = pszText;
4512 return bResult;
4515 /***
4516 * DESCRIPTION:
4517 * Retrieves the index of the item at coordinate (0, 0) of the client area.
4519 * PARAMETER(S):
4520 * [I] infoPtr : valid pointer to the listview structure
4522 * RETURN:
4523 * item index
4525 static INT LISTVIEW_GetTopIndex(const LISTVIEW_INFO *infoPtr)
4527 INT nItem = 0;
4528 SCROLLINFO scrollInfo;
4530 scrollInfo.cbSize = sizeof(SCROLLINFO);
4531 scrollInfo.fMask = SIF_POS;
4533 if (infoPtr->uView == LV_VIEW_LIST)
4535 if (GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo))
4536 nItem = scrollInfo.nPos * LISTVIEW_GetCountPerColumn(infoPtr);
4538 else if (infoPtr->uView == LV_VIEW_DETAILS)
4540 if (GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo))
4541 nItem = scrollInfo.nPos;
4543 else
4545 if (GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo))
4546 nItem = LISTVIEW_GetCountPerRow(infoPtr) * (scrollInfo.nPos / infoPtr->nItemHeight);
4549 TRACE("nItem=%d\n", nItem);
4551 return nItem;
4555 /***
4556 * DESCRIPTION:
4557 * Erases the background of the given rectangle
4559 * PARAMETER(S):
4560 * [I] infoPtr : valid pointer to the listview structure
4561 * [I] hdc : device context handle
4562 * [I] lprcBox : clipping rectangle
4564 * RETURN:
4565 * Success: TRUE
4566 * Failure: FALSE
4568 static inline BOOL LISTVIEW_FillBkgnd(const LISTVIEW_INFO *infoPtr, HDC hdc, const RECT *lprcBox)
4570 if (!infoPtr->hBkBrush) return FALSE;
4572 TRACE("(hdc=%p, lprcBox=%s, hBkBrush=%p)\n", hdc, wine_dbgstr_rect(lprcBox), infoPtr->hBkBrush);
4574 return FillRect(hdc, lprcBox, infoPtr->hBkBrush);
4577 /* Draw main item or subitem */
4578 static void LISTVIEW_DrawItemPart(LISTVIEW_INFO *infoPtr, LVITEMW *item, const NMLVCUSTOMDRAW *nmlvcd, const POINT *pos)
4580 RECT rcSelect, rcLabel, rcBox, rcStateIcon, rcIcon;
4581 const RECT *background;
4582 HIMAGELIST himl;
4583 UINT format;
4584 RECT *focus;
4586 /* now check if we need to update the focus rectangle */
4587 focus = infoPtr->bFocus && (item->state & LVIS_FOCUSED) ? &infoPtr->rcFocus : 0;
4588 if (!focus) item->state &= ~LVIS_FOCUSED;
4590 LISTVIEW_GetItemMetrics(infoPtr, item, &rcBox, &rcSelect, &rcIcon, &rcStateIcon, &rcLabel);
4591 OffsetRect(&rcBox, pos->x, pos->y);
4592 OffsetRect(&rcSelect, pos->x, pos->y);
4593 OffsetRect(&rcIcon, pos->x, pos->y);
4594 OffsetRect(&rcStateIcon, pos->x, pos->y);
4595 OffsetRect(&rcLabel, pos->x, pos->y);
4596 TRACE("%d: box=%s, select=%s, icon=%s. label=%s\n", item->iSubItem,
4597 wine_dbgstr_rect(&rcBox), wine_dbgstr_rect(&rcSelect),
4598 wine_dbgstr_rect(&rcIcon), wine_dbgstr_rect(&rcLabel));
4600 /* FIXME: temporary hack */
4601 rcSelect.left = rcLabel.left;
4603 if (infoPtr->uView == LV_VIEW_DETAILS && item->iSubItem == 0)
4605 if (!(infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT))
4606 OffsetRect(&rcSelect, LISTVIEW_GetColumnInfo(infoPtr, 0)->rcHeader.left, 0);
4607 OffsetRect(&rcIcon, LISTVIEW_GetColumnInfo(infoPtr, 0)->rcHeader.left, 0);
4608 OffsetRect(&rcStateIcon, LISTVIEW_GetColumnInfo(infoPtr, 0)->rcHeader.left, 0);
4609 OffsetRect(&rcLabel, LISTVIEW_GetColumnInfo(infoPtr, 0)->rcHeader.left, 0);
4612 /* in icon mode, the label rect is really what we want to draw the
4613 * background for */
4614 /* in detail mode, we want to paint background for label rect when
4615 * item is not selected or listview has full row select; otherwise paint
4616 * background for text only */
4617 if ( infoPtr->uView == LV_VIEW_ICON ||
4618 (infoPtr->uView == LV_VIEW_DETAILS && (!(item->state & LVIS_SELECTED) ||
4619 (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT))))
4620 background = &rcLabel;
4621 else
4622 background = &rcSelect;
4624 if (nmlvcd->clrTextBk != CLR_NONE)
4625 ExtTextOutW(nmlvcd->nmcd.hdc, background->left, background->top, ETO_OPAQUE, background, NULL, 0, NULL);
4627 if (item->state & LVIS_FOCUSED)
4629 if (infoPtr->uView == LV_VIEW_DETAILS)
4631 if (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT)
4633 /* we have to update left focus bound too if item isn't in leftmost column
4634 and reduce right box bound */
4635 if (DPA_GetPtrCount(infoPtr->hdpaColumns) > 0)
4637 INT leftmost;
4639 if ((leftmost = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX, 0, 0)))
4641 INT Originx = pos->x - LISTVIEW_GetColumnInfo(infoPtr, leftmost)->rcHeader.left;
4642 INT rightmost = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX,
4643 DPA_GetPtrCount(infoPtr->hdpaColumns) - 1, 0);
4645 rcBox.right = LISTVIEW_GetColumnInfo(infoPtr, rightmost)->rcHeader.right + Originx;
4646 rcSelect.left = LISTVIEW_GetColumnInfo(infoPtr, leftmost)->rcHeader.left + Originx;
4649 rcSelect.right = rcBox.right;
4651 infoPtr->rcFocus = rcSelect;
4653 else
4654 infoPtr->rcFocus = rcLabel;
4657 /* state icons */
4658 if (infoPtr->himlState && STATEIMAGEINDEX(item->state) && (item->iSubItem == 0))
4660 UINT stateimage = STATEIMAGEINDEX(item->state);
4661 if (stateimage)
4663 TRACE("stateimage=%d\n", stateimage);
4664 ImageList_Draw(infoPtr->himlState, stateimage-1, nmlvcd->nmcd.hdc, rcStateIcon.left, rcStateIcon.top, ILD_NORMAL);
4668 /* item icons */
4669 himl = (infoPtr->uView == LV_VIEW_ICON ? infoPtr->himlNormal : infoPtr->himlSmall);
4670 if (himl && item->iImage >= 0 && !IsRectEmpty(&rcIcon))
4672 UINT style;
4674 TRACE("iImage=%d\n", item->iImage);
4676 if (item->state & (LVIS_SELECTED | LVIS_CUT) && infoPtr->bFocus)
4677 style = ILD_SELECTED;
4678 else
4679 style = ILD_NORMAL;
4681 ImageList_DrawEx(himl, item->iImage, nmlvcd->nmcd.hdc, rcIcon.left, rcIcon.top,
4682 rcIcon.right - rcIcon.left, rcIcon.bottom - rcIcon.top, infoPtr->clrBk,
4683 item->state & LVIS_CUT ? RGB(255, 255, 255) : CLR_DEFAULT,
4684 style | (item->state & LVIS_OVERLAYMASK));
4687 /* Don't bother painting item being edited */
4688 if (infoPtr->hwndEdit && item->iItem == infoPtr->nEditLabelItem && item->iSubItem == 0) return;
4690 /* figure out the text drawing flags */
4691 format = (infoPtr->uView == LV_VIEW_ICON ? (focus ? LV_FL_DT_FLAGS : LV_ML_DT_FLAGS) : LV_SL_DT_FLAGS);
4692 if (infoPtr->uView == LV_VIEW_ICON)
4693 format = (focus ? LV_FL_DT_FLAGS : LV_ML_DT_FLAGS);
4694 else if (item->iSubItem)
4696 switch (LISTVIEW_GetColumnInfo(infoPtr, item->iSubItem)->fmt & LVCFMT_JUSTIFYMASK)
4698 case LVCFMT_RIGHT: format |= DT_RIGHT; break;
4699 case LVCFMT_CENTER: format |= DT_CENTER; break;
4700 default: format |= DT_LEFT;
4703 if (!(format & (DT_RIGHT | DT_CENTER)))
4705 if (himl && item->iImage >= 0 && !IsRectEmpty(&rcIcon)) rcLabel.left += IMAGE_PADDING;
4706 else rcLabel.left += LABEL_HOR_PADDING;
4708 else if (format & DT_RIGHT) rcLabel.right -= LABEL_HOR_PADDING;
4710 /* for GRIDLINES reduce the bottom so the text formats correctly */
4711 if (infoPtr->uView == LV_VIEW_DETAILS && infoPtr->dwLvExStyle & LVS_EX_GRIDLINES)
4712 rcLabel.bottom--;
4714 DrawTextW(nmlvcd->nmcd.hdc, item->pszText, -1, &rcLabel, format);
4717 /***
4718 * DESCRIPTION:
4719 * Draws an item.
4721 * PARAMETER(S):
4722 * [I] infoPtr : valid pointer to the listview structure
4723 * [I] hdc : device context handle
4724 * [I] nItem : item index
4725 * [I] nSubItem : subitem index
4726 * [I] pos : item position in client coordinates
4727 * [I] cdmode : custom draw mode
4729 * RETURN:
4730 * Success: TRUE
4731 * Failure: FALSE
4733 static BOOL LISTVIEW_DrawItem(LISTVIEW_INFO *infoPtr, HDC hdc, INT nItem, ITERATOR *subitems, POINT pos, DWORD cdmode)
4735 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
4736 static WCHAR callbackW[] = L"(callback)";
4737 DWORD cdsubitemmode = CDRF_DODEFAULT;
4738 RECT *focus, rcBox;
4739 NMLVCUSTOMDRAW nmlvcd;
4740 LVITEMW lvItem;
4742 TRACE("(hdc=%p, nItem=%d, subitems=%p, pos=%s)\n", hdc, nItem, subitems, wine_dbgstr_point(&pos));
4744 /* get information needed for drawing the item */
4745 lvItem.mask = LVIF_TEXT | LVIF_IMAGE | LVIF_PARAM | LVIF_STATE;
4746 if (infoPtr->uView == LV_VIEW_DETAILS) lvItem.mask |= LVIF_INDENT;
4747 lvItem.stateMask = LVIS_SELECTED | LVIS_FOCUSED | LVIS_STATEIMAGEMASK | LVIS_CUT | LVIS_OVERLAYMASK;
4748 lvItem.iItem = nItem;
4749 lvItem.iSubItem = 0;
4750 lvItem.state = 0;
4751 lvItem.lParam = 0;
4752 lvItem.cchTextMax = DISP_TEXT_SIZE;
4753 lvItem.pszText = szDispText;
4754 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) return FALSE;
4755 if (lvItem.pszText == LPSTR_TEXTCALLBACKW) lvItem.pszText = callbackW;
4756 TRACE(" lvItem=%s\n", debuglvitem_t(&lvItem, TRUE));
4758 /* now check if we need to update the focus rectangle */
4759 focus = infoPtr->bFocus && (lvItem.state & LVIS_FOCUSED) ? &infoPtr->rcFocus : 0;
4760 if (!focus) lvItem.state &= ~LVIS_FOCUSED;
4762 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, &rcBox, NULL, NULL, NULL, NULL);
4763 OffsetRect(&rcBox, pos.x, pos.y);
4765 /* Full custom draw stage sequence looks like this:
4767 LV_VIEW_DETAILS:
4769 - CDDS_ITEMPREPAINT
4770 - CDDS_ITEMPREPAINT|CDDS_SUBITEM | => sent n times, where n is number of subitems,
4771 CDDS_ITEMPOSTPAINT|CDDS_SUBITEM | including item itself
4772 - CDDS_ITEMPOSTPAINT
4774 other styles:
4776 - CDDS_ITEMPREPAINT
4777 - CDDS_ITEMPOSTPAINT
4780 /* fill in the custom draw structure */
4781 customdraw_fill(&nmlvcd, infoPtr, hdc, &rcBox, &lvItem);
4782 if (cdmode & CDRF_NOTIFYITEMDRAW)
4783 cdsubitemmode = notify_customdraw(infoPtr, CDDS_ITEMPREPAINT, &nmlvcd);
4784 if (cdsubitemmode & CDRF_SKIPDEFAULT) goto postpaint;
4786 if (subitems)
4788 while (iterator_next(subitems))
4790 DWORD subitemstage = CDRF_DODEFAULT;
4792 /* We need to query for each subitem, item's data (subitem == 0) is already here at this point */
4793 if (subitems->nItem)
4795 lvItem.mask = LVIF_TEXT | LVIF_IMAGE | LVIF_PARAM | LVIF_INDENT;
4796 lvItem.stateMask = LVIS_SELECTED | LVIS_FOCUSED | LVIS_STATEIMAGEMASK | LVIS_CUT | LVIS_OVERLAYMASK;
4797 lvItem.iItem = nItem;
4798 lvItem.iSubItem = subitems->nItem;
4799 lvItem.state = 0;
4800 lvItem.lParam = 0;
4801 lvItem.cchTextMax = DISP_TEXT_SIZE;
4802 lvItem.pszText = szDispText;
4803 szDispText[0] = 0;
4804 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) return FALSE;
4805 if (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT)
4806 lvItem.state = LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED);
4807 if (lvItem.pszText == LPSTR_TEXTCALLBACKW) lvItem.pszText = callbackW;
4808 TRACE(" lvItem=%s\n", debuglvitem_t(&lvItem, TRUE));
4810 /* update custom draw data */
4811 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, &nmlvcd.nmcd.rc, NULL, NULL, NULL, NULL);
4812 OffsetRect(&nmlvcd.nmcd.rc, pos.x, pos.y);
4813 nmlvcd.iSubItem = subitems->nItem;
4816 if (cdsubitemmode & CDRF_NOTIFYSUBITEMDRAW)
4817 subitemstage = notify_customdraw(infoPtr, CDDS_SUBITEM | CDDS_ITEMPREPAINT, &nmlvcd);
4819 if (subitems->nItem == 0 || (cdmode & CDRF_NOTIFYITEMDRAW))
4820 prepaint_setup(infoPtr, hdc, &nmlvcd, FALSE);
4821 else if (!(infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT))
4822 prepaint_setup(infoPtr, hdc, &nmlvcd, TRUE);
4824 if (!(subitemstage & CDRF_SKIPDEFAULT))
4825 LISTVIEW_DrawItemPart(infoPtr, &lvItem, &nmlvcd, &pos);
4827 if (subitemstage & CDRF_NOTIFYPOSTPAINT)
4828 subitemstage = notify_customdraw(infoPtr, CDDS_SUBITEM | CDDS_ITEMPOSTPAINT, &nmlvcd);
4831 else
4833 prepaint_setup(infoPtr, hdc, &nmlvcd, FALSE);
4834 LISTVIEW_DrawItemPart(infoPtr, &lvItem, &nmlvcd, &pos);
4837 postpaint:
4838 if (cdsubitemmode & CDRF_NOTIFYPOSTPAINT)
4840 nmlvcd.iSubItem = 0;
4841 notify_customdraw(infoPtr, CDDS_ITEMPOSTPAINT, &nmlvcd);
4844 return TRUE;
4847 /***
4848 * DESCRIPTION:
4849 * Draws listview items when in owner draw mode.
4851 * PARAMETER(S):
4852 * [I] infoPtr : valid pointer to the listview structure
4853 * [I] hdc : device context handle
4855 * RETURN:
4856 * None
4858 static void LISTVIEW_RefreshOwnerDraw(const LISTVIEW_INFO *infoPtr, ITERATOR *i, HDC hdc, DWORD cdmode)
4860 UINT uID = (UINT)GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
4861 DWORD cditemmode = CDRF_DODEFAULT;
4862 NMLVCUSTOMDRAW nmlvcd;
4863 POINT Origin, Position;
4864 DRAWITEMSTRUCT dis;
4865 LVITEMW item;
4867 TRACE("()\n");
4869 ZeroMemory(&dis, sizeof(dis));
4871 /* Get scroll info once before loop */
4872 LISTVIEW_GetOrigin(infoPtr, &Origin);
4874 /* iterate through the invalidated rows */
4875 while(iterator_next(i))
4877 item.iItem = i->nItem;
4878 item.iSubItem = 0;
4879 item.mask = LVIF_PARAM | LVIF_STATE;
4880 item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
4881 if (!LISTVIEW_GetItemW(infoPtr, &item)) continue;
4883 dis.CtlType = ODT_LISTVIEW;
4884 dis.CtlID = uID;
4885 dis.itemID = item.iItem;
4886 dis.itemAction = ODA_DRAWENTIRE;
4887 dis.itemState = 0;
4888 if (item.state & LVIS_SELECTED) dis.itemState |= ODS_SELECTED;
4889 if (infoPtr->bFocus && (item.state & LVIS_FOCUSED)) dis.itemState |= ODS_FOCUS;
4890 dis.hwndItem = infoPtr->hwndSelf;
4891 dis.hDC = hdc;
4892 LISTVIEW_GetItemOrigin(infoPtr, dis.itemID, &Position);
4893 dis.rcItem.left = Position.x + Origin.x;
4894 dis.rcItem.right = dis.rcItem.left + infoPtr->nItemWidth;
4895 dis.rcItem.top = Position.y + Origin.y;
4896 dis.rcItem.bottom = dis.rcItem.top + infoPtr->nItemHeight;
4897 dis.itemData = item.lParam;
4899 TRACE("item=%s, rcItem=%s\n", debuglvitem_t(&item, TRUE), wine_dbgstr_rect(&dis.rcItem));
4902 * Even if we do not send the CDRF_NOTIFYITEMDRAW we need to fill the nmlvcd
4903 * structure for the rest. of the paint cycle
4905 customdraw_fill(&nmlvcd, infoPtr, hdc, &dis.rcItem, &item);
4906 if (cdmode & CDRF_NOTIFYITEMDRAW)
4907 cditemmode = notify_customdraw(infoPtr, CDDS_PREPAINT, &nmlvcd);
4909 if (!(cditemmode & CDRF_SKIPDEFAULT))
4911 prepaint_setup (infoPtr, hdc, &nmlvcd, FALSE);
4912 SendMessageW(infoPtr->hwndNotify, WM_DRAWITEM, dis.CtlID, (LPARAM)&dis);
4915 if (cditemmode & CDRF_NOTIFYPOSTPAINT)
4916 notify_postpaint(infoPtr, &nmlvcd);
4920 /***
4921 * DESCRIPTION:
4922 * Draws listview items when in report display mode.
4924 * PARAMETER(S):
4925 * [I] infoPtr : valid pointer to the listview structure
4926 * [I] hdc : device context handle
4927 * [I] cdmode : custom draw mode
4929 * RETURN:
4930 * None
4932 static void LISTVIEW_RefreshReport(LISTVIEW_INFO *infoPtr, ITERATOR *i, HDC hdc, DWORD cdmode)
4934 INT rgntype;
4935 RECT rcClip, rcItem;
4936 POINT Origin;
4937 RANGES colRanges;
4938 INT col;
4939 ITERATOR j;
4941 TRACE("()\n");
4943 /* figure out what to draw */
4944 rgntype = GetClipBox(hdc, &rcClip);
4945 if (rgntype == NULLREGION) return;
4947 /* Get scroll info once before loop */
4948 LISTVIEW_GetOrigin(infoPtr, &Origin);
4950 colRanges = ranges_create(DPA_GetPtrCount(infoPtr->hdpaColumns));
4952 /* narrow down the columns we need to paint */
4953 for(col = 0; col < DPA_GetPtrCount(infoPtr->hdpaColumns); col++)
4955 INT index = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX, col, 0);
4957 LISTVIEW_GetHeaderRect(infoPtr, index, &rcItem);
4958 if ((rcItem.right + Origin.x >= rcClip.left) && (rcItem.left + Origin.x < rcClip.right))
4959 ranges_additem(colRanges, index);
4961 iterator_rangesitems(&j, colRanges);
4963 /* in full row select, we _have_ to draw the main item */
4964 if (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT)
4965 j.nSpecial = 0;
4967 /* iterate through the invalidated rows */
4968 while(iterator_next(i))
4970 RANGES subitems;
4971 POINT Position;
4972 ITERATOR k;
4974 SelectObject(hdc, infoPtr->hFont);
4975 LISTVIEW_GetItemOrigin(infoPtr, i->nItem, &Position);
4976 Position.x = Origin.x;
4977 Position.y += Origin.y;
4979 subitems = ranges_create(DPA_GetPtrCount(infoPtr->hdpaColumns));
4981 /* iterate through the invalidated columns */
4982 while(iterator_next(&j))
4984 LISTVIEW_GetHeaderRect(infoPtr, j.nItem, &rcItem);
4986 if (rgntype == COMPLEXREGION && !((infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT) && j.nItem == 0))
4988 rcItem.top = 0;
4989 rcItem.bottom = infoPtr->nItemHeight;
4990 OffsetRect(&rcItem, Origin.x, Position.y);
4991 if (!RectVisible(hdc, &rcItem)) continue;
4994 ranges_additem(subitems, j.nItem);
4997 iterator_rangesitems(&k, subitems);
4998 LISTVIEW_DrawItem(infoPtr, hdc, i->nItem, &k, Position, cdmode);
4999 iterator_destroy(&k);
5001 iterator_destroy(&j);
5004 /***
5005 * DESCRIPTION:
5006 * Draws the gridlines if necessary when in report display mode.
5008 * PARAMETER(S):
5009 * [I] infoPtr : valid pointer to the listview structure
5010 * [I] hdc : device context handle
5012 * RETURN:
5013 * None
5015 static void LISTVIEW_RefreshReportGrid(LISTVIEW_INFO *infoPtr, HDC hdc)
5017 INT rgntype;
5018 INT y, itemheight;
5019 INT col, index;
5020 HPEN hPen, hOldPen;
5021 RECT rcClip, rcItem = {0};
5022 POINT Origin;
5023 RANGES colRanges;
5024 ITERATOR j;
5025 BOOL rmost = FALSE;
5027 TRACE("()\n");
5029 /* figure out what to draw */
5030 rgntype = GetClipBox(hdc, &rcClip);
5031 if (rgntype == NULLREGION) return;
5033 /* Get scroll info once before loop */
5034 LISTVIEW_GetOrigin(infoPtr, &Origin);
5036 colRanges = ranges_create(DPA_GetPtrCount(infoPtr->hdpaColumns));
5038 /* narrow down the columns we need to paint */
5039 for(col = 0; col < DPA_GetPtrCount(infoPtr->hdpaColumns); col++)
5041 index = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX, col, 0);
5043 LISTVIEW_GetHeaderRect(infoPtr, index, &rcItem);
5044 if ((rcItem.right + Origin.x >= rcClip.left) && (rcItem.left + Origin.x < rcClip.right))
5045 ranges_additem(colRanges, index);
5048 /* is right most vertical line visible? */
5049 if (DPA_GetPtrCount(infoPtr->hdpaColumns) > 0)
5051 index = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX, DPA_GetPtrCount(infoPtr->hdpaColumns) - 1, 0);
5052 LISTVIEW_GetHeaderRect(infoPtr, index, &rcItem);
5053 rmost = (rcItem.right + Origin.x < rcClip.right);
5056 if ((hPen = CreatePen( PS_SOLID, 1, comctl32_color.clr3dFace )))
5058 hOldPen = SelectObject ( hdc, hPen );
5060 /* draw the vertical lines for the columns */
5061 iterator_rangesitems(&j, colRanges);
5062 while(iterator_next(&j))
5064 LISTVIEW_GetHeaderRect(infoPtr, j.nItem, &rcItem);
5065 if (rcItem.left == 0) continue; /* skip leftmost column */
5066 rcItem.left += Origin.x;
5067 rcItem.right += Origin.x;
5068 rcItem.top = infoPtr->rcList.top;
5069 rcItem.bottom = infoPtr->rcList.bottom;
5070 TRACE("vert col=%d, rcItem=%s\n", j.nItem, wine_dbgstr_rect(&rcItem));
5071 MoveToEx (hdc, rcItem.left, rcItem.top, NULL);
5072 LineTo (hdc, rcItem.left, rcItem.bottom);
5074 iterator_destroy(&j);
5075 /* draw rightmost grid line if visible */
5076 if (rmost)
5078 index = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX,
5079 DPA_GetPtrCount(infoPtr->hdpaColumns) - 1, 0);
5080 LISTVIEW_GetHeaderRect(infoPtr, index, &rcItem);
5082 rcItem.right += Origin.x;
5084 MoveToEx (hdc, rcItem.right, infoPtr->rcList.top, NULL);
5085 LineTo (hdc, rcItem.right, infoPtr->rcList.bottom);
5088 /* draw the horizontal lines for the rows */
5089 itemheight = LISTVIEW_CalculateItemHeight(infoPtr);
5090 rcItem.left = infoPtr->rcList.left;
5091 rcItem.right = infoPtr->rcList.right;
5092 for(y = Origin.y > 1 ? Origin.y - 1 : itemheight - 1 + Origin.y % itemheight; y<=infoPtr->rcList.bottom; y+=itemheight)
5094 rcItem.bottom = rcItem.top = y;
5095 TRACE("horz rcItem=%s\n", wine_dbgstr_rect(&rcItem));
5096 MoveToEx (hdc, rcItem.left, rcItem.top, NULL);
5097 LineTo (hdc, rcItem.right, rcItem.top);
5100 SelectObject( hdc, hOldPen );
5101 DeleteObject( hPen );
5103 else
5104 ranges_destroy(colRanges);
5107 /***
5108 * DESCRIPTION:
5109 * Draws listview items when in list display mode.
5111 * PARAMETER(S):
5112 * [I] infoPtr : valid pointer to the listview structure
5113 * [I] hdc : device context handle
5114 * [I] cdmode : custom draw mode
5116 * RETURN:
5117 * None
5119 static void LISTVIEW_RefreshList(LISTVIEW_INFO *infoPtr, ITERATOR *i, HDC hdc, DWORD cdmode)
5121 POINT Origin, Position;
5123 /* Get scroll info once before loop */
5124 LISTVIEW_GetOrigin(infoPtr, &Origin);
5126 while(iterator_prev(i))
5128 SelectObject(hdc, infoPtr->hFont);
5129 LISTVIEW_GetItemOrigin(infoPtr, i->nItem, &Position);
5130 Position.x += Origin.x;
5131 Position.y += Origin.y;
5133 LISTVIEW_DrawItem(infoPtr, hdc, i->nItem, NULL, Position, cdmode);
5138 /***
5139 * DESCRIPTION:
5140 * Draws listview items.
5142 * PARAMETER(S):
5143 * [I] infoPtr : valid pointer to the listview structure
5144 * [I] hdc : device context handle
5145 * [I] prcErase : rect to be erased before refresh (may be NULL)
5147 * RETURN:
5148 * NoneX
5150 static void LISTVIEW_Refresh(LISTVIEW_INFO *infoPtr, HDC hdc, const RECT *prcErase)
5152 COLORREF oldTextColor = 0, oldBkColor = 0;
5153 NMLVCUSTOMDRAW nmlvcd;
5154 HFONT hOldFont = 0;
5155 DWORD cdmode;
5156 INT oldBkMode = 0;
5157 RECT rcClient;
5158 ITERATOR i;
5159 HDC hdcOrig = hdc;
5160 HBITMAP hbmp = NULL;
5161 RANGE range;
5163 LISTVIEW_DUMP(infoPtr);
5165 if (infoPtr->dwLvExStyle & LVS_EX_DOUBLEBUFFER) {
5166 TRACE("double buffering\n");
5168 hdc = CreateCompatibleDC(hdcOrig);
5169 if (!hdc) {
5170 ERR("Failed to create DC for backbuffer\n");
5171 return;
5173 hbmp = CreateCompatibleBitmap(hdcOrig, infoPtr->rcList.right,
5174 infoPtr->rcList.bottom);
5175 if (!hbmp) {
5176 ERR("Failed to create bitmap for backbuffer\n");
5177 DeleteDC(hdc);
5178 return;
5181 SelectObject(hdc, hbmp);
5182 SelectObject(hdc, infoPtr->hFont);
5184 if(GetClipBox(hdcOrig, &rcClient))
5185 IntersectClipRect(hdc, rcClient.left, rcClient.top, rcClient.right, rcClient.bottom);
5186 } else {
5187 /* Save dc values we're gonna trash while drawing
5188 * FIXME: Should be done in LISTVIEW_DrawItem() */
5189 hOldFont = SelectObject(hdc, infoPtr->hFont);
5190 oldBkMode = GetBkMode(hdc);
5191 oldBkColor = GetBkColor(hdc);
5192 oldTextColor = GetTextColor(hdc);
5195 infoPtr->bIsDrawing = TRUE;
5197 if (prcErase) {
5198 LISTVIEW_FillBkgnd(infoPtr, hdc, prcErase);
5199 } else if (infoPtr->dwLvExStyle & LVS_EX_DOUBLEBUFFER) {
5200 /* If no erasing was done (usually because RedrawWindow was called
5201 * with RDW_INVALIDATE only) we need to copy the old contents into
5202 * the backbuffer before continuing. */
5203 BitBlt(hdc, infoPtr->rcList.left, infoPtr->rcList.top,
5204 infoPtr->rcList.right - infoPtr->rcList.left,
5205 infoPtr->rcList.bottom - infoPtr->rcList.top,
5206 hdcOrig, infoPtr->rcList.left, infoPtr->rcList.top, SRCCOPY);
5209 GetClientRect(infoPtr->hwndSelf, &rcClient);
5210 customdraw_fill(&nmlvcd, infoPtr, hdc, &rcClient, 0);
5211 cdmode = notify_customdraw(infoPtr, CDDS_PREPAINT, &nmlvcd);
5212 if (cdmode & CDRF_SKIPDEFAULT) goto enddraw;
5214 /* nothing to draw */
5215 if(infoPtr->nItemCount == 0) goto enddraw;
5217 /* figure out what we need to draw */
5218 iterator_visibleitems(&i, infoPtr, hdc);
5219 range = iterator_range(&i);
5221 /* send cache hint notification */
5222 if (infoPtr->dwStyle & LVS_OWNERDATA)
5224 NMLVCACHEHINT nmlv;
5226 ZeroMemory(&nmlv, sizeof(NMLVCACHEHINT));
5227 nmlv.iFrom = range.lower;
5228 nmlv.iTo = range.upper - 1;
5229 notify_hdr(infoPtr, LVN_ODCACHEHINT, &nmlv.hdr);
5232 if ((infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && (infoPtr->uView == LV_VIEW_DETAILS))
5233 LISTVIEW_RefreshOwnerDraw(infoPtr, &i, hdc, cdmode);
5234 else
5236 if (infoPtr->uView == LV_VIEW_DETAILS)
5237 LISTVIEW_RefreshReport(infoPtr, &i, hdc, cdmode);
5238 else /* LV_VIEW_LIST, LV_VIEW_ICON or LV_VIEW_SMALLICON */
5239 LISTVIEW_RefreshList(infoPtr, &i, hdc, cdmode);
5241 /* if we have a focus rect and it's visible, draw it */
5242 if (infoPtr->bFocus && range.lower <= infoPtr->nFocusedItem &&
5243 (range.upper - 1) >= infoPtr->nFocusedItem)
5244 LISTVIEW_DrawFocusRect(infoPtr, hdc);
5246 iterator_destroy(&i);
5248 enddraw:
5249 /* For LVS_EX_GRIDLINES go and draw lines */
5250 /* This includes the case where there were *no* items */
5251 if ((infoPtr->uView == LV_VIEW_DETAILS) && infoPtr->dwLvExStyle & LVS_EX_GRIDLINES)
5252 LISTVIEW_RefreshReportGrid(infoPtr, hdc);
5254 /* Draw marquee rectangle if appropriate */
5255 if (infoPtr->bMarqueeSelect)
5256 DrawFocusRect(hdc, &infoPtr->marqueeDrawRect);
5258 if (cdmode & CDRF_NOTIFYPOSTPAINT)
5259 notify_postpaint(infoPtr, &nmlvcd);
5261 if(hbmp) {
5262 BitBlt(hdcOrig, infoPtr->rcList.left, infoPtr->rcList.top,
5263 infoPtr->rcList.right - infoPtr->rcList.left,
5264 infoPtr->rcList.bottom - infoPtr->rcList.top,
5265 hdc, infoPtr->rcList.left, infoPtr->rcList.top, SRCCOPY);
5267 DeleteObject(hbmp);
5268 DeleteDC(hdc);
5269 } else {
5270 SelectObject(hdc, hOldFont);
5271 SetBkMode(hdc, oldBkMode);
5272 SetBkColor(hdc, oldBkColor);
5273 SetTextColor(hdc, oldTextColor);
5276 infoPtr->bIsDrawing = FALSE;
5280 /***
5281 * DESCRIPTION:
5282 * Calculates the approximate width and height of a given number of items.
5284 * PARAMETER(S):
5285 * [I] infoPtr : valid pointer to the listview structure
5286 * [I] nItemCount : number of items
5287 * [I] wWidth : width
5288 * [I] wHeight : height
5290 * RETURN:
5291 * Returns a DWORD. The width in the low word and the height in high word.
5293 static DWORD LISTVIEW_ApproximateViewRect(const LISTVIEW_INFO *infoPtr, INT nItemCount,
5294 WORD wWidth, WORD wHeight)
5296 DWORD dwViewRect = 0;
5298 if (nItemCount == -1)
5299 nItemCount = infoPtr->nItemCount;
5301 if (infoPtr->uView == LV_VIEW_LIST)
5303 INT nItemCountPerColumn = 1;
5304 INT nColumnCount = 0;
5306 if (wHeight == 0xFFFF)
5308 /* use current height */
5309 wHeight = infoPtr->rcList.bottom - infoPtr->rcList.top;
5312 if (wHeight < infoPtr->nItemHeight)
5313 wHeight = infoPtr->nItemHeight;
5315 if (nItemCount > 0)
5317 if (infoPtr->nItemHeight > 0)
5319 nItemCountPerColumn = wHeight / infoPtr->nItemHeight;
5320 if (nItemCountPerColumn == 0)
5321 nItemCountPerColumn = 1;
5323 if (nItemCount % nItemCountPerColumn != 0)
5324 nColumnCount = nItemCount / nItemCountPerColumn;
5325 else
5326 nColumnCount = nItemCount / nItemCountPerColumn + 1;
5330 /* Microsoft padding magic */
5331 wHeight = nItemCountPerColumn * infoPtr->nItemHeight + 2;
5332 wWidth = nColumnCount * infoPtr->nItemWidth + 2;
5334 dwViewRect = MAKELONG(wWidth, wHeight);
5336 else if (infoPtr->uView == LV_VIEW_DETAILS)
5338 RECT rcBox;
5340 if (infoPtr->nItemCount > 0)
5342 LISTVIEW_GetItemBox(infoPtr, 0, &rcBox);
5343 wWidth = rcBox.right - rcBox.left;
5344 wHeight = (rcBox.bottom - rcBox.top) * nItemCount;
5346 else
5348 /* use current height and width */
5349 if (wHeight == 0xffff)
5350 wHeight = infoPtr->rcList.bottom - infoPtr->rcList.top;
5351 if (wWidth == 0xffff)
5352 wWidth = infoPtr->rcList.right - infoPtr->rcList.left;
5355 dwViewRect = MAKELONG(wWidth, wHeight);
5357 else if (infoPtr->uView == LV_VIEW_ICON)
5359 UINT rows,cols;
5360 UINT nItemWidth;
5361 UINT nItemHeight;
5363 nItemWidth = infoPtr->iconSpacing.cx;
5364 nItemHeight = infoPtr->iconSpacing.cy;
5366 if (wWidth == 0xffff)
5367 wWidth = infoPtr->rcList.right - infoPtr->rcList.left;
5369 if (wWidth < nItemWidth)
5370 wWidth = nItemWidth;
5372 cols = wWidth / nItemWidth;
5373 if (cols > nItemCount)
5374 cols = nItemCount;
5375 if (cols < 1)
5376 cols = 1;
5378 if (nItemCount)
5380 rows = nItemCount / cols;
5381 if (nItemCount % cols)
5382 rows++;
5384 else
5385 rows = 0;
5387 wHeight = (nItemHeight * rows)+2;
5388 wWidth = (nItemWidth * cols)+2;
5390 dwViewRect = MAKELONG(wWidth, wHeight);
5392 else if (infoPtr->uView == LV_VIEW_SMALLICON)
5393 FIXME("uView == LV_VIEW_SMALLICON: not implemented\n");
5395 return dwViewRect;
5398 /***
5399 * DESCRIPTION:
5400 * Cancel edit label with saving item text.
5402 * PARAMETER(S):
5403 * [I] infoPtr : valid pointer to the listview structure
5405 * RETURN:
5406 * Always returns TRUE.
5408 static LRESULT LISTVIEW_CancelEditLabel(LISTVIEW_INFO *infoPtr)
5410 if (infoPtr->hwndEdit)
5412 /* handle value will be lost after LISTVIEW_EndEditLabelT */
5413 HWND edit = infoPtr->hwndEdit;
5415 LISTVIEW_EndEditLabelT(infoPtr, TRUE, IsWindowUnicode(infoPtr->hwndEdit));
5416 SendMessageW(edit, WM_CLOSE, 0, 0);
5419 return TRUE;
5422 /***
5423 * DESCRIPTION:
5424 * Create a drag image list for the specified item.
5426 * PARAMETER(S):
5427 * [I] infoPtr : valid pointer to the listview structure
5428 * [I] iItem : index of item
5429 * [O] lppt : Upper-left corner of the image
5431 * RETURN:
5432 * Returns a handle to the image list if successful, NULL otherwise.
5434 static HIMAGELIST LISTVIEW_CreateDragImage(LISTVIEW_INFO *infoPtr, INT iItem, LPPOINT lppt)
5436 RECT rcItem;
5437 SIZE size;
5438 POINT pos;
5439 HDC hdc, hdcOrig;
5440 HBITMAP hbmp, hOldbmp;
5441 HFONT hOldFont;
5442 HIMAGELIST dragList = 0;
5443 TRACE("iItem=%d Count=%d\n", iItem, infoPtr->nItemCount);
5445 if (iItem < 0 || iItem >= infoPtr->nItemCount || !lppt)
5446 return 0;
5448 rcItem.left = LVIR_BOUNDS;
5449 if (!LISTVIEW_GetItemRect(infoPtr, iItem, &rcItem))
5450 return 0;
5452 lppt->x = rcItem.left;
5453 lppt->y = rcItem.top;
5455 size.cx = rcItem.right - rcItem.left;
5456 size.cy = rcItem.bottom - rcItem.top;
5458 hdcOrig = GetDC(infoPtr->hwndSelf);
5459 hdc = CreateCompatibleDC(hdcOrig);
5460 hbmp = CreateCompatibleBitmap(hdcOrig, size.cx, size.cy);
5461 hOldbmp = SelectObject(hdc, hbmp);
5462 hOldFont = SelectObject(hdc, infoPtr->hFont);
5464 SetRect(&rcItem, 0, 0, size.cx, size.cy);
5465 FillRect(hdc, &rcItem, infoPtr->hBkBrush);
5467 pos.x = pos.y = 0;
5468 if (LISTVIEW_DrawItem(infoPtr, hdc, iItem, NULL, pos, CDRF_DODEFAULT))
5470 dragList = ImageList_Create(size.cx, size.cy, ILC_COLOR, 10, 10);
5471 SelectObject(hdc, hOldbmp);
5472 ImageList_Add(dragList, hbmp, 0);
5474 else
5475 SelectObject(hdc, hOldbmp);
5477 SelectObject(hdc, hOldFont);
5478 DeleteObject(hbmp);
5479 DeleteDC(hdc);
5480 ReleaseDC(infoPtr->hwndSelf, hdcOrig);
5482 TRACE("ret=%p\n", dragList);
5484 return dragList;
5488 /***
5489 * DESCRIPTION:
5490 * Removes all listview items and subitems.
5492 * PARAMETER(S):
5493 * [I] infoPtr : valid pointer to the listview structure
5495 * RETURN:
5496 * SUCCESS : TRUE
5497 * FAILURE : FALSE
5499 static BOOL LISTVIEW_DeleteAllItems(LISTVIEW_INFO *infoPtr, BOOL destroy)
5501 HDPA hdpaSubItems = NULL;
5502 BOOL suppress = FALSE;
5503 ITEMHDR *hdrItem;
5504 ITEM_INFO *lpItem;
5505 ITEM_ID *lpID;
5506 INT i, j;
5508 TRACE("()\n");
5510 /* we do it directly, to avoid notifications */
5511 ranges_clear(infoPtr->selectionRanges);
5512 infoPtr->nSelectionMark = -1;
5513 infoPtr->nFocusedItem = -1;
5514 SetRectEmpty(&infoPtr->rcFocus);
5515 /* But we are supposed to leave nHotItem as is! */
5517 /* send LVN_DELETEALLITEMS notification */
5518 if (!(infoPtr->dwStyle & LVS_OWNERDATA) || !destroy)
5520 NMLISTVIEW nmlv;
5522 memset(&nmlv, 0, sizeof(NMLISTVIEW));
5523 nmlv.iItem = -1;
5524 suppress = notify_listview(infoPtr, LVN_DELETEALLITEMS, &nmlv);
5527 for (i = infoPtr->nItemCount - 1; i >= 0; i--)
5529 if (!(infoPtr->dwStyle & LVS_OWNERDATA))
5531 /* send LVN_DELETEITEM notification, if not suppressed
5532 and if it is not a virtual listview */
5533 if (!suppress) notify_deleteitem(infoPtr, i);
5534 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, i);
5535 lpItem = DPA_GetPtr(hdpaSubItems, 0);
5536 /* free id struct */
5537 j = DPA_GetPtrIndex(infoPtr->hdpaItemIds, lpItem->id);
5538 lpID = DPA_GetPtr(infoPtr->hdpaItemIds, j);
5539 DPA_DeletePtr(infoPtr->hdpaItemIds, j);
5540 Free(lpID);
5541 /* both item and subitem start with ITEMHDR header */
5542 for (j = 0; j < DPA_GetPtrCount(hdpaSubItems); j++)
5544 hdrItem = DPA_GetPtr(hdpaSubItems, j);
5545 if (is_text(hdrItem->pszText)) Free(hdrItem->pszText);
5546 Free(hdrItem);
5548 DPA_Destroy(hdpaSubItems);
5549 DPA_DeletePtr(infoPtr->hdpaItems, i);
5551 DPA_DeletePtr(infoPtr->hdpaPosX, i);
5552 DPA_DeletePtr(infoPtr->hdpaPosY, i);
5553 infoPtr->nItemCount --;
5556 if (!destroy)
5558 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
5559 LISTVIEW_UpdateScroll(infoPtr);
5561 LISTVIEW_InvalidateList(infoPtr);
5563 return TRUE;
5566 /***
5567 * DESCRIPTION:
5568 * Scrolls, and updates the columns, when a column is changing width.
5570 * PARAMETER(S):
5571 * [I] infoPtr : valid pointer to the listview structure
5572 * [I] nColumn : column to scroll
5573 * [I] dx : amount of scroll, in pixels
5575 * RETURN:
5576 * None.
5578 static void LISTVIEW_ScrollColumns(LISTVIEW_INFO *infoPtr, INT nColumn, INT dx)
5580 COLUMN_INFO *lpColumnInfo;
5581 RECT rcOld, rcCol;
5582 POINT ptOrigin;
5583 INT nCol;
5584 HDITEMW hdi;
5586 if (nColumn < 0 || DPA_GetPtrCount(infoPtr->hdpaColumns) < 1) return;
5587 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, min(nColumn, DPA_GetPtrCount(infoPtr->hdpaColumns) - 1));
5588 rcCol = lpColumnInfo->rcHeader;
5589 if (nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns))
5590 rcCol.left = rcCol.right;
5592 /* adjust the other columns */
5593 hdi.mask = HDI_ORDER;
5594 if (SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, nColumn, (LPARAM)&hdi))
5596 INT nOrder = hdi.iOrder;
5597 for (nCol = 0; nCol < DPA_GetPtrCount(infoPtr->hdpaColumns); nCol++)
5599 hdi.mask = HDI_ORDER;
5600 SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, nCol, (LPARAM)&hdi);
5601 if (hdi.iOrder >= nOrder) {
5602 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, nCol);
5603 lpColumnInfo->rcHeader.left += dx;
5604 lpColumnInfo->rcHeader.right += dx;
5609 /* do not update screen if not in report mode */
5610 if (!is_redrawing(infoPtr) || infoPtr->uView != LV_VIEW_DETAILS) return;
5612 /* Need to reset the item width when inserting a new column */
5613 infoPtr->nItemWidth += dx;
5615 LISTVIEW_UpdateScroll(infoPtr);
5616 LISTVIEW_GetOrigin(infoPtr, &ptOrigin);
5618 /* scroll to cover the deleted column, and invalidate for redraw */
5619 rcOld = infoPtr->rcList;
5620 rcOld.left = ptOrigin.x + rcCol.left + dx;
5621 ScrollWindowEx(infoPtr->hwndSelf, dx, 0, &rcOld, &rcOld, 0, 0, SW_ERASE | SW_INVALIDATE);
5624 /***
5625 * DESCRIPTION:
5626 * Removes a column from the listview control.
5628 * PARAMETER(S):
5629 * [I] infoPtr : valid pointer to the listview structure
5630 * [I] nColumn : column index
5632 * RETURN:
5633 * SUCCESS : TRUE
5634 * FAILURE : FALSE
5636 static BOOL LISTVIEW_DeleteColumn(LISTVIEW_INFO *infoPtr, INT nColumn)
5638 RECT rcCol;
5640 TRACE("nColumn=%d\n", nColumn);
5642 if (nColumn < 0 || nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns))
5643 return FALSE;
5645 /* While the MSDN specifically says that column zero should not be deleted,
5646 what actually happens is that the column itself is deleted but no items or subitems
5647 are removed.
5650 LISTVIEW_GetHeaderRect(infoPtr, nColumn, &rcCol);
5652 if (!SendMessageW(infoPtr->hwndHeader, HDM_DELETEITEM, nColumn, 0))
5653 return FALSE;
5655 Free(DPA_GetPtr(infoPtr->hdpaColumns, nColumn));
5656 DPA_DeletePtr(infoPtr->hdpaColumns, nColumn);
5658 if (!(infoPtr->dwStyle & LVS_OWNERDATA) && nColumn)
5660 SUBITEM_INFO *lpSubItem, *lpDelItem;
5661 HDPA hdpaSubItems;
5662 INT nItem, nSubItem, i;
5664 for (nItem = 0; nItem < infoPtr->nItemCount; nItem++)
5666 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, nItem);
5667 nSubItem = 0;
5668 lpDelItem = 0;
5669 for (i = 1; i < DPA_GetPtrCount(hdpaSubItems); i++)
5671 lpSubItem = DPA_GetPtr(hdpaSubItems, i);
5672 if (lpSubItem->iSubItem == nColumn)
5674 nSubItem = i;
5675 lpDelItem = lpSubItem;
5677 else if (lpSubItem->iSubItem > nColumn)
5679 lpSubItem->iSubItem--;
5683 /* if we found our subitem, zap it */
5684 if (nSubItem > 0)
5686 /* free string */
5687 if (is_text(lpDelItem->hdr.pszText))
5688 Free(lpDelItem->hdr.pszText);
5690 /* free item */
5691 Free(lpDelItem);
5693 /* free dpa memory */
5694 DPA_DeletePtr(hdpaSubItems, nSubItem);
5699 /* update the other column info */
5700 if(DPA_GetPtrCount(infoPtr->hdpaColumns) == 0)
5701 LISTVIEW_InvalidateList(infoPtr);
5702 else
5703 LISTVIEW_ScrollColumns(infoPtr, nColumn, -(rcCol.right - rcCol.left));
5704 LISTVIEW_UpdateItemSize(infoPtr);
5706 return TRUE;
5709 /***
5710 * DESCRIPTION:
5711 * Invalidates the listview after an item's insertion or deletion.
5713 * PARAMETER(S):
5714 * [I] infoPtr : valid pointer to the listview structure
5715 * [I] nItem : item index
5716 * [I] dir : -1 if deleting, 1 if inserting
5718 * RETURN:
5719 * None
5721 static void LISTVIEW_ScrollOnInsert(LISTVIEW_INFO *infoPtr, INT nItem, INT dir)
5723 INT nPerCol, nItemCol, nItemRow;
5724 RECT rcScroll;
5725 POINT Origin;
5727 /* if we don't refresh, what's the point of scrolling? */
5728 if (!is_redrawing(infoPtr)) return;
5730 assert (abs(dir) == 1);
5732 /* arrange icons if autoarrange is on */
5733 if (is_autoarrange(infoPtr))
5735 BOOL arrange = TRUE;
5736 if (dir < 0 && nItem >= infoPtr->nItemCount) arrange = FALSE;
5737 if (dir > 0 && nItem == infoPtr->nItemCount - 1) arrange = FALSE;
5738 if (arrange) LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
5741 /* scrollbars need updating */
5742 LISTVIEW_UpdateScroll(infoPtr);
5744 /* figure out the item's position */
5745 if (infoPtr->uView == LV_VIEW_DETAILS)
5746 nPerCol = infoPtr->nItemCount + 1;
5747 else if (infoPtr->uView == LV_VIEW_LIST)
5748 nPerCol = LISTVIEW_GetCountPerColumn(infoPtr);
5749 else /* LV_VIEW_ICON, or LV_VIEW_SMALLICON */
5750 return;
5752 nItemCol = nItem / nPerCol;
5753 nItemRow = nItem % nPerCol;
5754 LISTVIEW_GetOrigin(infoPtr, &Origin);
5756 /* move the items below up a slot */
5757 rcScroll.left = nItemCol * infoPtr->nItemWidth;
5758 rcScroll.top = nItemRow * infoPtr->nItemHeight;
5759 rcScroll.right = rcScroll.left + infoPtr->nItemWidth;
5760 rcScroll.bottom = nPerCol * infoPtr->nItemHeight;
5761 OffsetRect(&rcScroll, Origin.x, Origin.y);
5762 TRACE("rcScroll=%s, dx=%d\n", wine_dbgstr_rect(&rcScroll), dir * infoPtr->nItemHeight);
5763 if (IntersectRect(&rcScroll, &rcScroll, &infoPtr->rcList))
5765 TRACE("Invalidating rcScroll=%s, rcList=%s\n", wine_dbgstr_rect(&rcScroll), wine_dbgstr_rect(&infoPtr->rcList));
5766 InvalidateRect(infoPtr->hwndSelf, &rcScroll, TRUE);
5769 /* report has only that column, so we're done */
5770 if (infoPtr->uView == LV_VIEW_DETAILS) return;
5772 /* now for LISTs, we have to deal with the columns to the right */
5773 SetRect(&rcScroll, (nItemCol + 1) * infoPtr->nItemWidth, 0,
5774 (infoPtr->nItemCount / nPerCol + 1) * infoPtr->nItemWidth,
5775 nPerCol * infoPtr->nItemHeight);
5776 OffsetRect(&rcScroll, Origin.x, Origin.y);
5777 if (IntersectRect(&rcScroll, &rcScroll, &infoPtr->rcList))
5778 InvalidateRect(infoPtr->hwndSelf, &rcScroll, TRUE);
5781 /***
5782 * DESCRIPTION:
5783 * Removes an item from the listview control.
5785 * PARAMETER(S):
5786 * [I] infoPtr : valid pointer to the listview structure
5787 * [I] nItem : item index
5789 * RETURN:
5790 * SUCCESS : TRUE
5791 * FAILURE : FALSE
5793 static BOOL LISTVIEW_DeleteItem(LISTVIEW_INFO *infoPtr, INT nItem)
5795 LVITEMW item;
5796 const BOOL is_icon = (infoPtr->uView == LV_VIEW_SMALLICON || infoPtr->uView == LV_VIEW_ICON);
5797 INT focus = infoPtr->nFocusedItem;
5799 TRACE("(nItem=%d)\n", nItem);
5801 if (nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
5803 /* remove selection, and focus */
5804 item.state = 0;
5805 item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
5806 LISTVIEW_SetItemState(infoPtr, nItem, &item);
5808 /* send LVN_DELETEITEM notification. */
5809 if (!notify_deleteitem(infoPtr, nItem)) return FALSE;
5811 /* we need to do this here, because we'll be deleting stuff */
5812 if (is_icon)
5813 LISTVIEW_InvalidateItem(infoPtr, nItem);
5815 if (!(infoPtr->dwStyle & LVS_OWNERDATA))
5817 HDPA hdpaSubItems;
5818 ITEMHDR *hdrItem;
5819 ITEM_INFO *lpItem;
5820 ITEM_ID *lpID;
5821 INT i;
5823 hdpaSubItems = DPA_DeletePtr(infoPtr->hdpaItems, nItem);
5824 lpItem = DPA_GetPtr(hdpaSubItems, 0);
5826 /* free id struct */
5827 i = DPA_GetPtrIndex(infoPtr->hdpaItemIds, lpItem->id);
5828 lpID = DPA_GetPtr(infoPtr->hdpaItemIds, i);
5829 DPA_DeletePtr(infoPtr->hdpaItemIds, i);
5830 Free(lpID);
5831 for (i = 0; i < DPA_GetPtrCount(hdpaSubItems); i++)
5833 hdrItem = DPA_GetPtr(hdpaSubItems, i);
5834 if (is_text(hdrItem->pszText)) Free(hdrItem->pszText);
5835 Free(hdrItem);
5837 DPA_Destroy(hdpaSubItems);
5840 if (is_icon)
5842 DPA_DeletePtr(infoPtr->hdpaPosX, nItem);
5843 DPA_DeletePtr(infoPtr->hdpaPosY, nItem);
5846 infoPtr->nItemCount--;
5847 LISTVIEW_ShiftIndices(infoPtr, nItem, -1);
5848 LISTVIEW_ShiftFocus(infoPtr, focus, nItem, -1);
5850 /* now is the invalidation fun */
5851 if (!is_icon)
5852 LISTVIEW_ScrollOnInsert(infoPtr, nItem, -1);
5853 return TRUE;
5857 /***
5858 * DESCRIPTION:
5859 * Callback implementation for editlabel control
5861 * PARAMETER(S):
5862 * [I] infoPtr : valid pointer to the listview structure
5863 * [I] storeText : store edit box text as item text
5864 * [I] isW : TRUE if psxText is Unicode, FALSE if it's ANSI
5866 * RETURN:
5867 * SUCCESS : TRUE
5868 * FAILURE : FALSE
5870 static BOOL LISTVIEW_EndEditLabelT(LISTVIEW_INFO *infoPtr, BOOL storeText, BOOL isW)
5872 HWND hwndSelf = infoPtr->hwndSelf;
5873 WCHAR szDispText[DISP_TEXT_SIZE] = { 0 };
5874 NMLVDISPINFOW dispInfo;
5875 INT editedItem = infoPtr->nEditLabelItem;
5876 BOOL same;
5877 WCHAR *pszText = NULL;
5878 BOOL res;
5880 if (storeText)
5882 DWORD len = isW ? GetWindowTextLengthW(infoPtr->hwndEdit) : GetWindowTextLengthA(infoPtr->hwndEdit);
5884 if (len++)
5886 if (!(pszText = Alloc(len * (isW ? sizeof(WCHAR) : sizeof(CHAR)))))
5887 return FALSE;
5889 if (isW)
5890 GetWindowTextW(infoPtr->hwndEdit, pszText, len);
5891 else
5892 GetWindowTextA(infoPtr->hwndEdit, (CHAR*)pszText, len);
5896 TRACE("(pszText=%s, isW=%d)\n", debugtext_t(pszText, isW), isW);
5898 ZeroMemory(&dispInfo, sizeof(dispInfo));
5899 dispInfo.item.mask = LVIF_PARAM | LVIF_STATE | LVIF_TEXT;
5900 dispInfo.item.iItem = editedItem;
5901 dispInfo.item.iSubItem = 0;
5902 dispInfo.item.stateMask = ~0;
5903 dispInfo.item.pszText = szDispText;
5904 dispInfo.item.cchTextMax = DISP_TEXT_SIZE;
5905 if (!LISTVIEW_GetItemT(infoPtr, &dispInfo.item, isW))
5907 res = FALSE;
5908 goto cleanup;
5911 if (isW)
5912 same = (lstrcmpW(dispInfo.item.pszText, pszText) == 0);
5913 else
5915 LPWSTR tmp = textdupTtoW(pszText, FALSE);
5916 same = (lstrcmpW(dispInfo.item.pszText, tmp) == 0);
5917 textfreeT(tmp, FALSE);
5920 /* add the text from the edit in */
5921 dispInfo.item.mask |= LVIF_TEXT;
5922 dispInfo.item.pszText = same ? NULL : pszText;
5923 dispInfo.item.cchTextMax = textlenT(dispInfo.item.pszText, isW);
5925 infoPtr->notify_mask &= ~NOTIFY_MASK_END_LABEL_EDIT;
5927 /* Do we need to update the Item Text */
5928 res = notify_dispinfoT(infoPtr, LVN_ENDLABELEDITW, &dispInfo, isW);
5930 infoPtr->notify_mask |= NOTIFY_MASK_END_LABEL_EDIT;
5932 infoPtr->nEditLabelItem = -1;
5933 infoPtr->hwndEdit = 0;
5935 if (!res) goto cleanup;
5937 if (!IsWindow(hwndSelf))
5939 res = FALSE;
5940 goto cleanup;
5942 if (!pszText) return TRUE;
5943 if (same)
5945 res = TRUE;
5946 goto cleanup;
5949 if (!(infoPtr->dwStyle & LVS_OWNERDATA))
5951 HDPA hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, editedItem);
5952 ITEM_INFO* lpItem = DPA_GetPtr(hdpaSubItems, 0);
5953 if (lpItem && lpItem->hdr.pszText == LPSTR_TEXTCALLBACKW)
5955 LISTVIEW_InvalidateItem(infoPtr, editedItem);
5956 res = TRUE;
5957 goto cleanup;
5961 ZeroMemory(&dispInfo, sizeof(dispInfo));
5962 dispInfo.item.mask = LVIF_TEXT;
5963 dispInfo.item.iItem = editedItem;
5964 dispInfo.item.iSubItem = 0;
5965 dispInfo.item.pszText = pszText;
5966 dispInfo.item.cchTextMax = textlenT(pszText, isW);
5967 res = LISTVIEW_SetItemT(infoPtr, &dispInfo.item, isW);
5969 cleanup:
5970 Free(pszText);
5972 return res;
5975 static LRESULT EditLblWndProcT(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL isW)
5977 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)GetWindowLongPtrW(GetParent(hwnd), 0);
5978 BOOL save = TRUE;
5980 TRACE("hwnd %p, uMsg %x, wParam %Ix, lParam %Ix, isW %d\n", hwnd, uMsg, wParam, lParam, isW);
5982 switch (uMsg)
5984 case WM_GETDLGCODE:
5985 return DLGC_WANTARROWS | DLGC_WANTALLKEYS;
5987 case WM_DESTROY:
5989 WNDPROC editProc = infoPtr->EditWndProc;
5990 infoPtr->EditWndProc = 0;
5991 SetWindowLongPtrW(hwnd, GWLP_WNDPROC, (DWORD_PTR)editProc);
5992 return CallWindowProcT(editProc, hwnd, uMsg, wParam, lParam, isW);
5995 case WM_KEYDOWN:
5996 if (VK_ESCAPE == (INT)wParam)
5998 save = FALSE;
5999 break;
6001 else if (VK_RETURN == (INT)wParam)
6002 break;
6004 default:
6005 return CallWindowProcT(infoPtr->EditWndProc, hwnd, uMsg, wParam, lParam, isW);
6008 /* kill the edit */
6009 if (infoPtr->hwndEdit)
6010 LISTVIEW_EndEditLabelT(infoPtr, save, isW);
6012 SendMessageW(hwnd, WM_CLOSE, 0, 0);
6013 return 0;
6016 static LRESULT CALLBACK EditLblWndProcW(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
6018 return EditLblWndProcT(hwnd, uMsg, wParam, lParam, TRUE);
6021 static LRESULT CALLBACK EditLblWndProcA(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
6023 return EditLblWndProcT(hwnd, uMsg, wParam, lParam, FALSE);
6026 static HWND CreateEditLabelT(LISTVIEW_INFO *infoPtr, LPCWSTR text, BOOL isW)
6028 static const DWORD style = WS_CHILDWINDOW|WS_CLIPSIBLINGS|ES_LEFT|ES_AUTOHSCROLL|WS_BORDER|WS_VISIBLE;
6029 HINSTANCE hinst = (HINSTANCE)GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_HINSTANCE);
6030 HWND hedit;
6032 TRACE("(%p, text=%s, isW=%d)\n", infoPtr, debugtext_t(text, isW), isW);
6034 /* window will be resized and positioned after LVN_BEGINLABELEDIT */
6035 if (isW)
6036 hedit = CreateWindowW(WC_EDITW, text, style, 0, 0, 0, 0, infoPtr->hwndSelf, 0, hinst, 0);
6037 else
6038 hedit = CreateWindowA(WC_EDITA, (LPCSTR)text, style, 0, 0, 0, 0, infoPtr->hwndSelf, 0, hinst, 0);
6040 if (!hedit) return 0;
6042 infoPtr->EditWndProc = (WNDPROC)
6043 (isW ? SetWindowLongPtrW(hedit, GWLP_WNDPROC, (DWORD_PTR)EditLblWndProcW) :
6044 SetWindowLongPtrA(hedit, GWLP_WNDPROC, (DWORD_PTR)EditLblWndProcA) );
6046 SendMessageW(hedit, WM_SETFONT, (WPARAM)infoPtr->hFont, FALSE);
6047 SendMessageW(hedit, EM_SETLIMITTEXT, DISP_TEXT_SIZE-1, 0);
6049 return hedit;
6052 static HWND LISTVIEW_EditLabelT(LISTVIEW_INFO *infoPtr, INT nItem, BOOL isW)
6054 WCHAR disptextW[DISP_TEXT_SIZE] = { 0 };
6055 HWND hwndSelf = infoPtr->hwndSelf;
6056 NMLVDISPINFOW dispInfo;
6057 HFONT hOldFont = NULL;
6058 TEXTMETRICW tm;
6059 RECT rect;
6060 SIZE sz;
6061 HDC hdc;
6063 TRACE("(nItem=%d, isW=%d)\n", nItem, isW);
6065 if (~infoPtr->dwStyle & LVS_EDITLABELS) return 0;
6067 /* remove existing edit box */
6068 if (infoPtr->hwndEdit)
6070 SetFocus(infoPtr->hwndSelf);
6071 infoPtr->hwndEdit = 0;
6074 if (nItem < 0 || nItem >= infoPtr->nItemCount) return 0;
6076 infoPtr->nEditLabelItem = nItem;
6078 LISTVIEW_SetSelection(infoPtr, nItem);
6079 LISTVIEW_SetItemFocus(infoPtr, nItem);
6080 LISTVIEW_InvalidateItem(infoPtr, nItem);
6082 rect.left = LVIR_LABEL;
6083 if (!LISTVIEW_GetItemRect(infoPtr, nItem, &rect)) return 0;
6085 ZeroMemory(&dispInfo, sizeof(dispInfo));
6086 dispInfo.item.mask = LVIF_PARAM | LVIF_STATE | LVIF_TEXT;
6087 dispInfo.item.iItem = nItem;
6088 dispInfo.item.iSubItem = 0;
6089 dispInfo.item.stateMask = ~0;
6090 dispInfo.item.pszText = disptextW;
6091 dispInfo.item.cchTextMax = DISP_TEXT_SIZE;
6092 if (!LISTVIEW_GetItemT(infoPtr, &dispInfo.item, isW)) return 0;
6094 infoPtr->hwndEdit = CreateEditLabelT(infoPtr, dispInfo.item.pszText, isW);
6095 if (!infoPtr->hwndEdit) return 0;
6097 if (notify_dispinfoT(infoPtr, LVN_BEGINLABELEDITW, &dispInfo, isW))
6099 if (!IsWindow(hwndSelf))
6100 return 0;
6101 SendMessageW(infoPtr->hwndEdit, WM_CLOSE, 0, 0);
6102 infoPtr->hwndEdit = 0;
6103 return 0;
6106 TRACE("disp text=%s\n", debugtext_t(dispInfo.item.pszText, isW));
6108 /* position and display edit box */
6109 hdc = GetDC(infoPtr->hwndSelf);
6111 /* select the font to get appropriate metric dimensions */
6112 if (infoPtr->hFont)
6113 hOldFont = SelectObject(hdc, infoPtr->hFont);
6115 /* use real edit box content, it could be altered during LVN_BEGINLABELEDIT notification */
6116 GetWindowTextW(infoPtr->hwndEdit, disptextW, DISP_TEXT_SIZE);
6117 TRACE("edit box text=%s\n", debugstr_w(disptextW));
6119 /* get string length in pixels */
6120 GetTextExtentPoint32W(hdc, disptextW, lstrlenW(disptextW), &sz);
6122 /* add extra spacing for the next character */
6123 GetTextMetricsW(hdc, &tm);
6124 sz.cx += tm.tmMaxCharWidth * 2;
6126 if (infoPtr->hFont)
6127 SelectObject(hdc, hOldFont);
6129 ReleaseDC(infoPtr->hwndSelf, hdc);
6131 sz.cy = rect.bottom - rect.top + 2;
6132 rect.left -= 2;
6133 rect.top -= 1;
6134 TRACE("moving edit (%ld,%ld)-(%ld,%ld)\n", rect.left, rect.top, sz.cx, sz.cy);
6135 MoveWindow(infoPtr->hwndEdit, rect.left, rect.top, sz.cx, sz.cy, FALSE);
6136 ShowWindow(infoPtr->hwndEdit, SW_NORMAL);
6137 SetFocus(infoPtr->hwndEdit);
6138 SendMessageW(infoPtr->hwndEdit, EM_SETSEL, 0, -1);
6139 return infoPtr->hwndEdit;
6143 /***
6144 * DESCRIPTION:
6145 * Ensures the specified item is visible, scrolling into view if necessary.
6147 * PARAMETER(S):
6148 * [I] infoPtr : valid pointer to the listview structure
6149 * [I] nItem : item index
6150 * [I] bPartial : partially or entirely visible
6152 * RETURN:
6153 * SUCCESS : TRUE
6154 * FAILURE : FALSE
6156 static BOOL LISTVIEW_EnsureVisible(LISTVIEW_INFO *infoPtr, INT nItem, BOOL bPartial)
6158 INT nScrollPosHeight = 0;
6159 INT nScrollPosWidth = 0;
6160 INT nHorzAdjust = 0;
6161 INT nVertAdjust = 0;
6162 INT nHorzDiff = 0;
6163 INT nVertDiff = 0;
6164 RECT rcItem, rcTemp;
6166 rcItem.left = LVIR_BOUNDS;
6167 if (!LISTVIEW_GetItemRect(infoPtr, nItem, &rcItem)) return FALSE;
6169 if (bPartial && IntersectRect(&rcTemp, &infoPtr->rcList, &rcItem)) return TRUE;
6171 if (rcItem.left < infoPtr->rcList.left || rcItem.right > infoPtr->rcList.right)
6173 /* scroll left/right, but in LV_VIEW_DETAILS mode */
6174 if (infoPtr->uView == LV_VIEW_LIST)
6175 nScrollPosWidth = infoPtr->nItemWidth;
6176 else if ((infoPtr->uView == LV_VIEW_SMALLICON) || (infoPtr->uView == LV_VIEW_ICON))
6177 nScrollPosWidth = 1;
6179 if (rcItem.left < infoPtr->rcList.left)
6181 nHorzAdjust = -1;
6182 if (infoPtr->uView != LV_VIEW_DETAILS) nHorzDiff = rcItem.left - infoPtr->rcList.left;
6184 else
6186 nHorzAdjust = 1;
6187 if (infoPtr->uView != LV_VIEW_DETAILS) nHorzDiff = rcItem.right - infoPtr->rcList.right;
6191 if (rcItem.top < infoPtr->rcList.top || rcItem.bottom > infoPtr->rcList.bottom)
6193 /* scroll up/down, but not in LVS_LIST mode */
6194 if (infoPtr->uView == LV_VIEW_DETAILS)
6195 nScrollPosHeight = infoPtr->nItemHeight;
6196 else if ((infoPtr->uView == LV_VIEW_ICON) || (infoPtr->uView == LV_VIEW_SMALLICON))
6197 nScrollPosHeight = 1;
6199 if (rcItem.top < infoPtr->rcList.top)
6201 nVertAdjust = -1;
6202 if (infoPtr->uView != LV_VIEW_LIST) nVertDiff = rcItem.top - infoPtr->rcList.top;
6204 else
6206 nVertAdjust = 1;
6207 if (infoPtr->uView != LV_VIEW_LIST) nVertDiff = rcItem.bottom - infoPtr->rcList.bottom;
6211 if (!nScrollPosWidth && !nScrollPosHeight) return TRUE;
6213 if (nScrollPosWidth)
6215 INT diff = nHorzDiff / nScrollPosWidth;
6216 if (nHorzDiff % nScrollPosWidth) diff += nHorzAdjust;
6217 LISTVIEW_HScroll(infoPtr, SB_INTERNAL, diff);
6220 if (nScrollPosHeight)
6222 INT diff = nVertDiff / nScrollPosHeight;
6223 if (nVertDiff % nScrollPosHeight) diff += nVertAdjust;
6224 LISTVIEW_VScroll(infoPtr, SB_INTERNAL, diff);
6227 return TRUE;
6230 /***
6231 * DESCRIPTION:
6232 * Searches for an item with specific characteristics.
6234 * PARAMETER(S):
6235 * [I] hwnd : window handle
6236 * [I] nStart : base item index
6237 * [I] lpFindInfo : item information to look for
6239 * RETURN:
6240 * SUCCESS : index of item
6241 * FAILURE : -1
6243 static INT LISTVIEW_FindItemW(const LISTVIEW_INFO *infoPtr, INT nStart,
6244 const LVFINDINFOW *lpFindInfo)
6246 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
6247 BOOL bWrap = FALSE, bNearest = FALSE;
6248 INT nItem = nStart + 1, nLast = infoPtr->nItemCount, nNearestItem = -1;
6249 ULONG xdist, ydist, dist, mindist = 0x7fffffff;
6250 POINT Position, Destination;
6251 int search_len = 0;
6252 LVITEMW lvItem;
6254 /* Search in virtual listviews should be done by application, not by
6255 listview control, so we just send LVN_ODFINDITEMW and return the result */
6256 if (infoPtr->dwStyle & LVS_OWNERDATA)
6258 NMLVFINDITEMW nmlv;
6260 nmlv.iStart = nStart;
6261 nmlv.lvfi = *lpFindInfo;
6262 return notify_hdr(infoPtr, LVN_ODFINDITEMW, (LPNMHDR)&nmlv.hdr);
6265 if (!lpFindInfo || nItem < 0) return -1;
6267 lvItem.mask = 0;
6268 if (lpFindInfo->flags & (LVFI_STRING | LVFI_PARTIAL | LVFI_SUBSTRING))
6270 lvItem.mask |= LVIF_TEXT;
6271 lvItem.pszText = szDispText;
6272 lvItem.cchTextMax = DISP_TEXT_SIZE;
6275 if (lpFindInfo->flags & LVFI_WRAP)
6276 bWrap = TRUE;
6278 if ((lpFindInfo->flags & LVFI_NEARESTXY) &&
6279 (infoPtr->uView == LV_VIEW_ICON || infoPtr->uView == LV_VIEW_SMALLICON))
6281 POINT Origin;
6282 RECT rcArea;
6284 LISTVIEW_GetOrigin(infoPtr, &Origin);
6285 Destination.x = lpFindInfo->pt.x - Origin.x;
6286 Destination.y = lpFindInfo->pt.y - Origin.y;
6287 switch(lpFindInfo->vkDirection)
6289 case VK_DOWN: Destination.y += infoPtr->nItemHeight; break;
6290 case VK_UP: Destination.y -= infoPtr->nItemHeight; break;
6291 case VK_RIGHT: Destination.x += infoPtr->nItemWidth; break;
6292 case VK_LEFT: Destination.x -= infoPtr->nItemWidth; break;
6293 case VK_HOME: Destination.x = Destination.y = 0; break;
6294 case VK_NEXT: Destination.y += infoPtr->rcList.bottom - infoPtr->rcList.top; break;
6295 case VK_PRIOR: Destination.y -= infoPtr->rcList.bottom - infoPtr->rcList.top; break;
6296 case VK_END:
6297 LISTVIEW_GetAreaRect(infoPtr, &rcArea);
6298 Destination.x = rcArea.right;
6299 Destination.y = rcArea.bottom;
6300 break;
6301 default: ERR("Unknown vkDirection=%d\n", lpFindInfo->vkDirection);
6303 bNearest = TRUE;
6305 else Destination.x = Destination.y = 0;
6307 /* if LVFI_PARAM is specified, all other flags are ignored */
6308 if (lpFindInfo->flags & LVFI_PARAM)
6310 lvItem.mask |= LVIF_PARAM;
6311 bNearest = FALSE;
6312 lvItem.mask &= ~LVIF_TEXT;
6315 nItem = bNearest ? -1 : nStart + 1;
6317 if (lpFindInfo->flags & (LVFI_PARTIAL | LVFI_SUBSTRING))
6318 search_len = lstrlenW(lpFindInfo->psz);
6320 again:
6321 for (; nItem < nLast; nItem++)
6323 lvItem.iItem = nItem;
6324 lvItem.iSubItem = 0;
6325 lvItem.pszText = szDispText;
6326 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) continue;
6328 if (lvItem.mask & LVIF_PARAM)
6330 if (lpFindInfo->lParam == lvItem.lParam)
6331 return nItem;
6332 else
6333 continue;
6336 if (lvItem.mask & LVIF_TEXT)
6338 if (lpFindInfo->flags & (LVFI_PARTIAL | LVFI_SUBSTRING))
6340 if (StrCmpNIW(lvItem.pszText, lpFindInfo->psz, search_len)) continue;
6342 else
6344 if (StrCmpIW(lvItem.pszText, lpFindInfo->psz)) continue;
6348 if (!bNearest) return nItem;
6350 /* This is very inefficient. To do a good job here,
6351 * we need a sorted array of (x,y) item positions */
6352 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position);
6354 /* compute the distance^2 to the destination */
6355 xdist = Destination.x - Position.x;
6356 ydist = Destination.y - Position.y;
6357 dist = xdist * xdist + ydist * ydist;
6359 /* remember the distance, and item if it's closer */
6360 if (dist < mindist)
6362 mindist = dist;
6363 nNearestItem = nItem;
6367 if (bWrap)
6369 nItem = 0;
6370 nLast = min(nStart + 1, infoPtr->nItemCount);
6371 bWrap = FALSE;
6372 goto again;
6375 return nNearestItem;
6378 /***
6379 * DESCRIPTION:
6380 * Searches for an item with specific characteristics.
6382 * PARAMETER(S):
6383 * [I] hwnd : window handle
6384 * [I] nStart : base item index
6385 * [I] lpFindInfo : item information to look for
6387 * RETURN:
6388 * SUCCESS : index of item
6389 * FAILURE : -1
6391 static INT LISTVIEW_FindItemA(const LISTVIEW_INFO *infoPtr, INT nStart,
6392 const LVFINDINFOA *lpFindInfo)
6394 LVFINDINFOW fiw;
6395 INT res;
6396 LPWSTR strW = NULL;
6398 memcpy(&fiw, lpFindInfo, sizeof(fiw));
6399 if (lpFindInfo->flags & (LVFI_STRING | LVFI_PARTIAL | LVFI_SUBSTRING))
6400 fiw.psz = strW = textdupTtoW((LPCWSTR)lpFindInfo->psz, FALSE);
6401 res = LISTVIEW_FindItemW(infoPtr, nStart, &fiw);
6402 textfreeT(strW, FALSE);
6403 return res;
6406 /***
6407 * DESCRIPTION:
6408 * Retrieves column attributes.
6410 * PARAMETER(S):
6411 * [I] infoPtr : valid pointer to the listview structure
6412 * [I] nColumn : column index
6413 * [IO] lpColumn : column information
6414 * [I] isW : if TRUE, then lpColumn is a LPLVCOLUMNW
6415 * otherwise it is in fact a LPLVCOLUMNA
6417 * RETURN:
6418 * SUCCESS : TRUE
6419 * FAILURE : FALSE
6421 static BOOL LISTVIEW_GetColumnT(const LISTVIEW_INFO *infoPtr, INT nColumn, LPLVCOLUMNW lpColumn, BOOL isW)
6423 COLUMN_INFO *lpColumnInfo;
6424 HDITEMW hdi;
6426 if (!lpColumn || nColumn < 0 || nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return FALSE;
6427 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, nColumn);
6429 /* initialize memory */
6430 ZeroMemory(&hdi, sizeof(hdi));
6432 if (lpColumn->mask & LVCF_TEXT)
6434 hdi.mask |= HDI_TEXT;
6435 hdi.pszText = lpColumn->pszText;
6436 hdi.cchTextMax = lpColumn->cchTextMax;
6439 if (lpColumn->mask & LVCF_IMAGE)
6440 hdi.mask |= HDI_IMAGE;
6442 if (lpColumn->mask & LVCF_ORDER)
6443 hdi.mask |= HDI_ORDER;
6445 if (lpColumn->mask & LVCF_SUBITEM)
6446 hdi.mask |= HDI_LPARAM;
6448 if (!SendMessageW(infoPtr->hwndHeader, isW ? HDM_GETITEMW : HDM_GETITEMA, nColumn, (LPARAM)&hdi)) return FALSE;
6450 if (lpColumn->mask & LVCF_FMT)
6451 lpColumn->fmt = lpColumnInfo->fmt;
6453 if (lpColumn->mask & LVCF_WIDTH)
6454 lpColumn->cx = lpColumnInfo->rcHeader.right - lpColumnInfo->rcHeader.left;
6456 if (lpColumn->mask & LVCF_IMAGE)
6457 lpColumn->iImage = hdi.iImage;
6459 if (lpColumn->mask & LVCF_ORDER)
6460 lpColumn->iOrder = hdi.iOrder;
6462 if (lpColumn->mask & LVCF_SUBITEM)
6463 lpColumn->iSubItem = hdi.lParam;
6465 if (lpColumn->mask & LVCF_MINWIDTH)
6466 lpColumn->cxMin = lpColumnInfo->cxMin;
6468 return TRUE;
6471 static inline BOOL LISTVIEW_GetColumnOrderArray(const LISTVIEW_INFO *infoPtr, INT iCount, LPINT lpiArray)
6473 if (!infoPtr->hwndHeader) return FALSE;
6474 return SendMessageW(infoPtr->hwndHeader, HDM_GETORDERARRAY, iCount, (LPARAM)lpiArray);
6477 /***
6478 * DESCRIPTION:
6479 * Retrieves the column width.
6481 * PARAMETER(S):
6482 * [I] infoPtr : valid pointer to the listview structure
6483 * [I] int : column index
6485 * RETURN:
6486 * SUCCESS : column width
6487 * FAILURE : zero
6489 static INT LISTVIEW_GetColumnWidth(const LISTVIEW_INFO *infoPtr, INT nColumn)
6491 INT nColumnWidth = 0;
6492 HDITEMW hdItem;
6494 TRACE("nColumn=%d\n", nColumn);
6496 /* we have a 'column' in LIST and REPORT mode only */
6497 switch(infoPtr->uView)
6499 case LV_VIEW_LIST:
6500 nColumnWidth = infoPtr->nItemWidth;
6501 break;
6502 case LV_VIEW_DETAILS:
6503 /* We are not using LISTVIEW_GetHeaderRect as this data is updated only after a HDN_ITEMCHANGED.
6504 * There is an application that subclasses the listview, calls LVM_GETCOLUMNWIDTH in the
6505 * HDN_ITEMCHANGED handler and goes into infinite recursion if it receives old data.
6507 hdItem.mask = HDI_WIDTH;
6508 if (!SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, nColumn, (LPARAM)&hdItem))
6510 WARN("(%p): HDM_GETITEMW failed for item %d\n", infoPtr->hwndSelf, nColumn);
6511 return 0;
6513 nColumnWidth = hdItem.cxy;
6514 break;
6517 TRACE("nColumnWidth=%d\n", nColumnWidth);
6518 return nColumnWidth;
6521 /***
6522 * DESCRIPTION:
6523 * In list or report display mode, retrieves the number of items that can fit
6524 * vertically in the visible area. In icon or small icon display mode,
6525 * retrieves the total number of visible items.
6527 * PARAMETER(S):
6528 * [I] infoPtr : valid pointer to the listview structure
6530 * RETURN:
6531 * Number of fully visible items.
6533 static INT LISTVIEW_GetCountPerPage(const LISTVIEW_INFO *infoPtr)
6535 switch (infoPtr->uView)
6537 case LV_VIEW_ICON:
6538 case LV_VIEW_SMALLICON:
6539 return infoPtr->nItemCount;
6540 case LV_VIEW_DETAILS:
6541 return LISTVIEW_GetCountPerColumn(infoPtr);
6542 case LV_VIEW_LIST:
6543 return LISTVIEW_GetCountPerRow(infoPtr) * LISTVIEW_GetCountPerColumn(infoPtr);
6545 assert(FALSE);
6546 return 0;
6549 /***
6550 * DESCRIPTION:
6551 * Retrieves an image list handle.
6553 * PARAMETER(S):
6554 * [I] infoPtr : valid pointer to the listview structure
6555 * [I] nImageList : image list identifier
6557 * RETURN:
6558 * SUCCESS : image list handle
6559 * FAILURE : NULL
6561 static HIMAGELIST LISTVIEW_GetImageList(const LISTVIEW_INFO *infoPtr, INT nImageList)
6563 switch (nImageList)
6565 case LVSIL_NORMAL: return infoPtr->himlNormal;
6566 case LVSIL_SMALL: return infoPtr->himlSmall;
6567 case LVSIL_STATE: return infoPtr->himlState;
6568 case LVSIL_GROUPHEADER:
6569 FIXME("LVSIL_GROUPHEADER not supported\n");
6570 break;
6571 default:
6572 WARN("got unknown imagelist index - %d\n", nImageList);
6574 return NULL;
6577 /* LISTVIEW_GetISearchString */
6579 /***
6580 * DESCRIPTION:
6581 * Retrieves item attributes.
6583 * PARAMETER(S):
6584 * [I] hwnd : window handle
6585 * [IO] lpLVItem : item info
6586 * [I] isW : if TRUE, then lpLVItem is a LPLVITEMW,
6587 * if FALSE, then lpLVItem is a LPLVITEMA.
6589 * NOTE:
6590 * This is the internal 'GetItem' interface -- it tries to
6591 * be smart and avoid text copies, if possible, by modifying
6592 * lpLVItem->pszText to point to the text string. Please note
6593 * that this is not always possible (e.g. OWNERDATA), so on
6594 * entry you *must* supply valid values for pszText, and cchTextMax.
6595 * The only difference to the documented interface is that upon
6596 * return, you should use *only* the lpLVItem->pszText, rather than
6597 * the buffer pointer you provided on input. Most code already does
6598 * that, so it's not a problem.
6599 * For the two cases when the text must be copied (that is,
6600 * for LVM_GETITEM, and LVM_GETITEMTEXT), use LISTVIEW_GetItemExtT.
6602 * RETURN:
6603 * SUCCESS : TRUE
6604 * FAILURE : FALSE
6606 static BOOL LISTVIEW_GetItemT(const LISTVIEW_INFO *infoPtr, LPLVITEMW lpLVItem, BOOL isW)
6608 ITEMHDR callbackHdr = { LPSTR_TEXTCALLBACKW, I_IMAGECALLBACK };
6609 BOOL is_subitem_invalid = FALSE;
6610 NMLVDISPINFOW dispInfo;
6611 ITEM_INFO *lpItem;
6612 ITEMHDR* pItemHdr;
6613 HDPA hdpaSubItems;
6615 TRACE("(item=%s, isW=%d)\n", debuglvitem_t(lpLVItem, isW), isW);
6617 if (!lpLVItem || lpLVItem->iItem < 0 || lpLVItem->iItem >= infoPtr->nItemCount)
6618 return FALSE;
6620 if (lpLVItem->mask == 0) return TRUE;
6621 TRACE("mask=%x\n", lpLVItem->mask);
6623 if (lpLVItem->iSubItem && (lpLVItem->mask & LVIF_STATE))
6624 lpLVItem->state = 0;
6626 /* a quick optimization if all we're asked is the focus state
6627 * these queries are worth optimising since they are common,
6628 * and can be answered in constant time, without the heavy accesses */
6629 if ( (lpLVItem->mask == LVIF_STATE) && (lpLVItem->stateMask == LVIS_FOCUSED) &&
6630 !(infoPtr->uCallbackMask & LVIS_FOCUSED) )
6632 lpLVItem->state = 0;
6633 if (infoPtr->nFocusedItem == lpLVItem->iItem && !lpLVItem->iSubItem)
6634 lpLVItem->state |= LVIS_FOCUSED;
6635 return TRUE;
6638 ZeroMemory(&dispInfo, sizeof(dispInfo));
6640 /* if the app stores all the data, handle it separately */
6641 if (infoPtr->dwStyle & LVS_OWNERDATA)
6643 dispInfo.item.state = 0;
6645 /* apparently, we should not callback for lParam in LVS_OWNERDATA */
6646 if ((lpLVItem->mask & ~(LVIF_STATE | LVIF_PARAM)) ||
6647 ((lpLVItem->mask & LVIF_STATE) && (infoPtr->uCallbackMask & lpLVItem->stateMask)))
6649 UINT mask = lpLVItem->mask;
6651 /* NOTE: copy only fields which we _know_ are initialized, some apps
6652 * depend on the uninitialized fields being 0 */
6653 dispInfo.item.mask = lpLVItem->mask & ~LVIF_PARAM;
6654 dispInfo.item.iItem = lpLVItem->iItem;
6655 dispInfo.item.iSubItem = lpLVItem->iSubItem;
6656 if (lpLVItem->mask & LVIF_TEXT)
6658 if (lpLVItem->mask & LVIF_NORECOMPUTE)
6659 /* reset mask */
6660 dispInfo.item.mask &= ~(LVIF_TEXT | LVIF_NORECOMPUTE);
6661 else
6663 dispInfo.item.pszText = lpLVItem->pszText;
6664 dispInfo.item.cchTextMax = lpLVItem->cchTextMax;
6667 if (lpLVItem->mask & LVIF_STATE)
6668 dispInfo.item.stateMask = lpLVItem->stateMask & infoPtr->uCallbackMask;
6669 /* could be zeroed on LVIF_NORECOMPUTE case */
6670 if (dispInfo.item.mask)
6672 notify_dispinfoT(infoPtr, LVN_GETDISPINFOW, &dispInfo, isW);
6673 dispInfo.item.stateMask = lpLVItem->stateMask;
6674 if (lpLVItem->mask & (LVIF_GROUPID|LVIF_COLUMNS))
6676 /* full size structure expected - _WIN32IE >= 0x560 */
6677 *lpLVItem = dispInfo.item;
6679 else if (lpLVItem->mask & LVIF_INDENT)
6681 /* indent member expected - _WIN32IE >= 0x300 */
6682 memcpy(lpLVItem, &dispInfo.item, offsetof( LVITEMW, iGroupId ));
6684 else
6686 /* minimal structure expected */
6687 memcpy(lpLVItem, &dispInfo.item, offsetof( LVITEMW, iIndent ));
6689 lpLVItem->mask = mask;
6690 TRACE(" getdispinfo(1):lpLVItem=%s\n", debuglvitem_t(lpLVItem, isW));
6694 /* make sure lParam is zeroed out */
6695 if (lpLVItem->mask & LVIF_PARAM) lpLVItem->lParam = 0;
6697 /* callback marked pointer required here */
6698 if ((lpLVItem->mask & LVIF_TEXT) && (lpLVItem->mask & LVIF_NORECOMPUTE))
6699 lpLVItem->pszText = LPSTR_TEXTCALLBACKW;
6701 /* we store only a little state, so if we're not asked, we're done */
6702 if (!(lpLVItem->mask & LVIF_STATE) || lpLVItem->iSubItem) return TRUE;
6704 /* if focus is handled by us, report it */
6705 if ( lpLVItem->stateMask & ~infoPtr->uCallbackMask & LVIS_FOCUSED )
6707 lpLVItem->state &= ~LVIS_FOCUSED;
6708 if (infoPtr->nFocusedItem == lpLVItem->iItem)
6709 lpLVItem->state |= LVIS_FOCUSED;
6712 /* and do the same for selection, if we handle it */
6713 if ( lpLVItem->stateMask & ~infoPtr->uCallbackMask & LVIS_SELECTED )
6715 lpLVItem->state &= ~LVIS_SELECTED;
6716 if (ranges_contain(infoPtr->selectionRanges, lpLVItem->iItem))
6717 lpLVItem->state |= LVIS_SELECTED;
6720 return TRUE;
6723 /* find the item and subitem structures before we proceed */
6724 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, lpLVItem->iItem);
6725 lpItem = DPA_GetPtr(hdpaSubItems, 0);
6726 assert (lpItem);
6728 if (lpLVItem->iSubItem)
6730 SUBITEM_INFO *lpSubItem = LISTVIEW_GetSubItemPtr(hdpaSubItems, lpLVItem->iSubItem);
6731 if (lpSubItem)
6732 pItemHdr = &lpSubItem->hdr;
6733 else
6735 pItemHdr = &callbackHdr;
6736 is_subitem_invalid = TRUE;
6739 else
6740 pItemHdr = &lpItem->hdr;
6742 /* Do we need to query the state from the app? */
6743 if ((lpLVItem->mask & LVIF_STATE) && infoPtr->uCallbackMask && (!lpLVItem->iSubItem || is_subitem_invalid))
6745 dispInfo.item.mask |= LVIF_STATE;
6746 dispInfo.item.stateMask = infoPtr->uCallbackMask;
6749 /* Do we need to enquire about the image? */
6750 if ((lpLVItem->mask & LVIF_IMAGE) && pItemHdr->iImage == I_IMAGECALLBACK &&
6751 (!lpLVItem->iSubItem || (infoPtr->dwLvExStyle & LVS_EX_SUBITEMIMAGES)))
6753 dispInfo.item.mask |= LVIF_IMAGE;
6754 dispInfo.item.iImage = I_IMAGECALLBACK;
6757 /* Only items support indentation */
6758 if ((lpLVItem->mask & LVIF_INDENT) && lpItem->iIndent == I_INDENTCALLBACK && !lpLVItem->iSubItem)
6760 dispInfo.item.mask |= LVIF_INDENT;
6761 dispInfo.item.iIndent = I_INDENTCALLBACK;
6764 /* Apps depend on calling back for text if it is NULL or LPSTR_TEXTCALLBACKW */
6765 if ((lpLVItem->mask & LVIF_TEXT) && !(lpLVItem->mask & LVIF_NORECOMPUTE) &&
6766 !is_text(pItemHdr->pszText))
6768 dispInfo.item.mask |= LVIF_TEXT;
6769 dispInfo.item.pszText = lpLVItem->pszText;
6770 dispInfo.item.cchTextMax = lpLVItem->cchTextMax;
6771 if (dispInfo.item.pszText && dispInfo.item.cchTextMax > 0)
6772 *dispInfo.item.pszText = '\0';
6775 /* If we don't have all the requested info, query the application */
6776 if (dispInfo.item.mask)
6778 dispInfo.item.iItem = lpLVItem->iItem;
6779 dispInfo.item.iSubItem = lpLVItem->iSubItem;
6780 dispInfo.item.lParam = lpItem->lParam;
6781 notify_dispinfoT(infoPtr, LVN_GETDISPINFOW, &dispInfo, isW);
6782 TRACE(" getdispinfo(2):item=%s\n", debuglvitem_t(&dispInfo.item, isW));
6785 /* we should not store values for subitems */
6786 if (lpLVItem->iSubItem) dispInfo.item.mask &= ~LVIF_DI_SETITEM;
6788 /* Now, handle the iImage field */
6789 if (dispInfo.item.mask & LVIF_IMAGE)
6791 lpLVItem->iImage = dispInfo.item.iImage;
6792 if ((dispInfo.item.mask & LVIF_DI_SETITEM) && pItemHdr->iImage == I_IMAGECALLBACK)
6793 pItemHdr->iImage = dispInfo.item.iImage;
6795 else if (lpLVItem->mask & LVIF_IMAGE)
6797 if (!lpLVItem->iSubItem || (infoPtr->dwLvExStyle & LVS_EX_SUBITEMIMAGES))
6798 lpLVItem->iImage = pItemHdr->iImage;
6799 else
6800 lpLVItem->iImage = 0;
6803 /* The pszText field */
6804 if (dispInfo.item.mask & LVIF_TEXT)
6806 if ((dispInfo.item.mask & LVIF_DI_SETITEM) && pItemHdr->pszText)
6807 textsetptrT(&pItemHdr->pszText, dispInfo.item.pszText, isW);
6809 lpLVItem->pszText = dispInfo.item.pszText;
6811 else if (lpLVItem->mask & LVIF_TEXT)
6813 /* if LVN_GETDISPINFO's disabled with LVIF_NORECOMPUTE return callback placeholder */
6814 if (isW || !is_text(pItemHdr->pszText)) lpLVItem->pszText = pItemHdr->pszText;
6815 else textcpynT(lpLVItem->pszText, isW, pItemHdr->pszText, TRUE, lpLVItem->cchTextMax);
6818 /* Next is the lParam field */
6819 if (dispInfo.item.mask & LVIF_PARAM)
6821 lpLVItem->lParam = dispInfo.item.lParam;
6822 if ((dispInfo.item.mask & LVIF_DI_SETITEM))
6823 lpItem->lParam = dispInfo.item.lParam;
6825 else if (lpLVItem->mask & LVIF_PARAM)
6826 lpLVItem->lParam = lpItem->lParam;
6828 /* if this is a subitem, we're done */
6829 if (lpLVItem->iSubItem) return TRUE;
6831 /* ... the state field (this one is different due to uCallbackmask) */
6832 if (lpLVItem->mask & LVIF_STATE)
6834 lpLVItem->state = lpItem->state & lpLVItem->stateMask;
6835 if (dispInfo.item.mask & LVIF_STATE)
6837 lpLVItem->state &= ~dispInfo.item.stateMask;
6838 lpLVItem->state |= (dispInfo.item.state & dispInfo.item.stateMask);
6840 if ( lpLVItem->stateMask & ~infoPtr->uCallbackMask & LVIS_FOCUSED )
6842 lpLVItem->state &= ~LVIS_FOCUSED;
6843 if (infoPtr->nFocusedItem == lpLVItem->iItem)
6844 lpLVItem->state |= LVIS_FOCUSED;
6846 if ( lpLVItem->stateMask & ~infoPtr->uCallbackMask & LVIS_SELECTED )
6848 lpLVItem->state &= ~LVIS_SELECTED;
6849 if (ranges_contain(infoPtr->selectionRanges, lpLVItem->iItem))
6850 lpLVItem->state |= LVIS_SELECTED;
6854 /* and last, but not least, the indent field */
6855 if (dispInfo.item.mask & LVIF_INDENT)
6857 lpLVItem->iIndent = dispInfo.item.iIndent;
6858 if ((dispInfo.item.mask & LVIF_DI_SETITEM) && lpItem->iIndent == I_INDENTCALLBACK)
6859 lpItem->iIndent = dispInfo.item.iIndent;
6861 else if (lpLVItem->mask & LVIF_INDENT)
6863 lpLVItem->iIndent = lpItem->iIndent;
6866 return TRUE;
6869 /***
6870 * DESCRIPTION:
6871 * Retrieves item attributes.
6873 * PARAMETER(S):
6874 * [I] hwnd : window handle
6875 * [IO] lpLVItem : item info
6876 * [I] isW : if TRUE, then lpLVItem is a LPLVITEMW,
6877 * if FALSE, then lpLVItem is a LPLVITEMA.
6879 * NOTE:
6880 * This is the external 'GetItem' interface -- it properly copies
6881 * the text in the provided buffer.
6883 * RETURN:
6884 * SUCCESS : TRUE
6885 * FAILURE : FALSE
6887 static BOOL LISTVIEW_GetItemExtT(const LISTVIEW_INFO *infoPtr, LPLVITEMW lpLVItem, BOOL isW)
6889 LPWSTR pszText;
6890 BOOL bResult;
6892 if (!lpLVItem || lpLVItem->iItem < 0 || lpLVItem->iItem >= infoPtr->nItemCount)
6893 return FALSE;
6895 pszText = lpLVItem->pszText;
6896 bResult = LISTVIEW_GetItemT(infoPtr, lpLVItem, isW);
6897 if (bResult && (lpLVItem->mask & LVIF_TEXT) && lpLVItem->pszText != pszText)
6899 if (lpLVItem->pszText != LPSTR_TEXTCALLBACKW)
6900 textcpynT(pszText, isW, lpLVItem->pszText, isW, lpLVItem->cchTextMax);
6901 else
6902 pszText = LPSTR_TEXTCALLBACKW;
6904 lpLVItem->pszText = pszText;
6906 return bResult;
6910 /***
6911 * DESCRIPTION:
6912 * Retrieves the position (upper-left) of the listview control item.
6913 * Note that for LVS_ICON style, the upper-left is that of the icon
6914 * and not the bounding box.
6916 * PARAMETER(S):
6917 * [I] infoPtr : valid pointer to the listview structure
6918 * [I] nItem : item index
6919 * [O] lpptPosition : coordinate information
6921 * RETURN:
6922 * SUCCESS : TRUE
6923 * FAILURE : FALSE
6925 static BOOL LISTVIEW_GetItemPosition(const LISTVIEW_INFO *infoPtr, INT nItem, LPPOINT lpptPosition)
6927 POINT Origin;
6929 TRACE("(nItem=%d, lpptPosition=%p)\n", nItem, lpptPosition);
6931 if (!lpptPosition || nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
6933 LISTVIEW_GetOrigin(infoPtr, &Origin);
6934 LISTVIEW_GetItemOrigin(infoPtr, nItem, lpptPosition);
6936 if (infoPtr->uView == LV_VIEW_ICON)
6938 lpptPosition->x += (infoPtr->nItemWidth - infoPtr->iconSize.cx) / 2;
6939 lpptPosition->y += ICON_TOP_PADDING;
6941 lpptPosition->x += Origin.x;
6942 lpptPosition->y += Origin.y;
6944 TRACE (" lpptPosition=%s\n", wine_dbgstr_point(lpptPosition));
6945 return TRUE;
6949 /***
6950 * DESCRIPTION:
6951 * Retrieves the bounding rectangle for a listview control item.
6953 * PARAMETER(S):
6954 * [I] infoPtr : valid pointer to the listview structure
6955 * [I] nItem : item index
6956 * [IO] lprc : bounding rectangle coordinates
6957 * lprc->left specifies the portion of the item for which the bounding
6958 * rectangle will be retrieved.
6960 * LVIR_BOUNDS Returns the bounding rectangle of the entire item,
6961 * including the icon and label.
6963 * * For LVS_ICON
6964 * * Experiment shows that native control returns:
6965 * * width = min (48, length of text line)
6966 * * .left = position.x - (width - iconsize.cx)/2
6967 * * .right = .left + width
6968 * * height = #lines of text * ntmHeight + icon height + 8
6969 * * .top = position.y - 2
6970 * * .bottom = .top + height
6971 * * separation between items .y = itemSpacing.cy - height
6972 * * .x = itemSpacing.cx - width
6973 * LVIR_ICON Returns the bounding rectangle of the icon or small icon.
6975 * * For LVS_ICON
6976 * * Experiment shows that native control returns:
6977 * * width = iconSize.cx + 16
6978 * * .left = position.x - (width - iconsize.cx)/2
6979 * * .right = .left + width
6980 * * height = iconSize.cy + 4
6981 * * .top = position.y - 2
6982 * * .bottom = .top + height
6983 * * separation between items .y = itemSpacing.cy - height
6984 * * .x = itemSpacing.cx - width
6985 * LVIR_LABEL Returns the bounding rectangle of the item text.
6987 * * For LVS_ICON
6988 * * Experiment shows that native control returns:
6989 * * width = text length
6990 * * .left = position.x - width/2
6991 * * .right = .left + width
6992 * * height = ntmH * linecount + 2
6993 * * .top = position.y + iconSize.cy + 6
6994 * * .bottom = .top + height
6995 * * separation between items .y = itemSpacing.cy - height
6996 * * .x = itemSpacing.cx - width
6997 * LVIR_SELECTBOUNDS Returns the union of the LVIR_ICON and LVIR_LABEL
6998 * rectangles, but excludes columns in report view.
7000 * RETURN:
7001 * SUCCESS : TRUE
7002 * FAILURE : FALSE
7004 * NOTES
7005 * Note that the bounding rectangle of the label in the LVS_ICON view depends
7006 * upon whether the window has the focus currently and on whether the item
7007 * is the one with the focus. Ensure that the control's record of which
7008 * item has the focus agrees with the items' records.
7010 static BOOL LISTVIEW_GetItemRect(const LISTVIEW_INFO *infoPtr, INT nItem, LPRECT lprc)
7012 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
7013 BOOL doLabel = TRUE, oversizedBox = FALSE;
7014 POINT Position, Origin;
7015 LVITEMW lvItem;
7016 LONG mode;
7018 TRACE("(hwnd=%p, nItem=%d, lprc=%p)\n", infoPtr->hwndSelf, nItem, lprc);
7020 if (!lprc || nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
7022 LISTVIEW_GetOrigin(infoPtr, &Origin);
7023 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position);
7025 /* Be smart and try to figure out the minimum we have to do */
7026 if (lprc->left == LVIR_ICON) doLabel = FALSE;
7027 if (infoPtr->uView == LV_VIEW_DETAILS && lprc->left == LVIR_BOUNDS) doLabel = FALSE;
7028 if (infoPtr->uView == LV_VIEW_ICON && lprc->left != LVIR_ICON &&
7029 infoPtr->bFocus && LISTVIEW_GetItemState(infoPtr, nItem, LVIS_FOCUSED))
7030 oversizedBox = TRUE;
7032 /* get what we need from the item before hand, so we make
7033 * only one request. This can speed up things, if data
7034 * is stored on the app side */
7035 lvItem.mask = 0;
7036 if (infoPtr->uView == LV_VIEW_DETAILS) lvItem.mask |= LVIF_INDENT;
7037 if (doLabel) lvItem.mask |= LVIF_TEXT;
7038 lvItem.iItem = nItem;
7039 lvItem.iSubItem = 0;
7040 lvItem.pszText = szDispText;
7041 lvItem.cchTextMax = DISP_TEXT_SIZE;
7042 if (lvItem.mask && !LISTVIEW_GetItemW(infoPtr, &lvItem)) return FALSE;
7043 /* we got the state already up, simulate it here, to avoid a reget */
7044 if (infoPtr->uView == LV_VIEW_ICON && (lprc->left != LVIR_ICON))
7046 lvItem.mask |= LVIF_STATE;
7047 lvItem.stateMask = LVIS_FOCUSED;
7048 lvItem.state = (oversizedBox ? LVIS_FOCUSED : 0);
7051 if (infoPtr->uView == LV_VIEW_DETAILS && (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT) && lprc->left == LVIR_SELECTBOUNDS)
7052 lprc->left = LVIR_BOUNDS;
7054 mode = lprc->left;
7055 switch(lprc->left)
7057 case LVIR_ICON:
7058 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, NULL, NULL, lprc, NULL, NULL);
7059 break;
7061 case LVIR_LABEL:
7062 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, NULL, NULL, NULL, NULL, lprc);
7063 break;
7065 case LVIR_BOUNDS:
7066 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, lprc, NULL, NULL, NULL, NULL);
7067 break;
7069 case LVIR_SELECTBOUNDS:
7070 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, NULL, lprc, NULL, NULL, NULL);
7071 break;
7073 default:
7074 WARN("Unknown value: %ld\n", lprc->left);
7075 return FALSE;
7078 if (infoPtr->uView == LV_VIEW_DETAILS)
7080 if (mode != LVIR_BOUNDS)
7081 OffsetRect(lprc, Origin.x + LISTVIEW_GetColumnInfo(infoPtr, 0)->rcHeader.left,
7082 Position.y + Origin.y);
7083 else
7084 OffsetRect(lprc, Origin.x, Position.y + Origin.y);
7086 else
7087 OffsetRect(lprc, Position.x + Origin.x, Position.y + Origin.y);
7089 TRACE(" rect=%s\n", wine_dbgstr_rect(lprc));
7091 return TRUE;
7094 /***
7095 * DESCRIPTION:
7096 * Retrieves the spacing between listview control items.
7098 * PARAMETER(S):
7099 * [I] infoPtr : valid pointer to the listview structure
7100 * [IO] lprc : rectangle to receive the output
7101 * on input, lprc->top = nSubItem
7102 * lprc->left = LVIR_ICON | LVIR_BOUNDS | LVIR_LABEL
7104 * NOTE: for subItem = 0, we should return the bounds of the _entire_ item,
7105 * not only those of the first column.
7107 * RETURN:
7108 * TRUE: success
7109 * FALSE: failure
7111 static BOOL LISTVIEW_GetSubItemRect(const LISTVIEW_INFO *infoPtr, INT item, LPRECT lprc)
7113 RECT rect = { 0, 0, 0, 0 };
7114 POINT origin;
7115 INT y;
7117 if (!lprc) return FALSE;
7119 TRACE("item %d, subitem %ld, type %ld\n", item, lprc->top, lprc->left);
7121 /* Subitem of '0' means item itself, and this works for all control view modes */
7122 if (lprc->top == 0)
7123 return LISTVIEW_GetItemRect(infoPtr, item, lprc);
7125 if (infoPtr->uView != LV_VIEW_DETAILS) return FALSE;
7127 LISTVIEW_GetOrigin(infoPtr, &origin);
7128 /* this works for any item index, no matter if it exists or not */
7129 y = item * infoPtr->nItemHeight + origin.y;
7131 if (infoPtr->hwndHeader && SendMessageW(infoPtr->hwndHeader, HDM_GETITEMRECT, lprc->top, (LPARAM)&rect))
7133 rect.top = 0;
7134 rect.bottom = infoPtr->nItemHeight;
7136 else
7138 /* Native implementation is broken for this case and garbage is left for left and right fields,
7139 we zero them to get predictable output */
7140 lprc->left = lprc->right = lprc->top = 0;
7141 lprc->bottom = infoPtr->nItemHeight;
7142 OffsetRect(lprc, origin.x, y);
7143 TRACE("return rect %s\n", wine_dbgstr_rect(lprc));
7144 return TRUE;
7147 switch (lprc->left)
7149 case LVIR_ICON:
7151 /* it doesn't matter if main item actually has an icon, if imagelist is set icon width is returned */
7152 if (infoPtr->himlSmall)
7153 rect.right = rect.left + infoPtr->iconSize.cx;
7154 else
7155 rect.right = rect.left;
7157 rect.bottom = rect.top + infoPtr->iconSize.cy;
7158 break;
7160 case LVIR_LABEL:
7161 case LVIR_BOUNDS:
7162 break;
7164 default:
7165 ERR("Unknown bounds %ld\n", lprc->left);
7166 return FALSE;
7169 OffsetRect(&rect, origin.x, y);
7170 *lprc = rect;
7171 TRACE("return rect %s\n", wine_dbgstr_rect(lprc));
7173 return TRUE;
7176 /***
7177 * DESCRIPTION:
7178 * Retrieves the spacing between listview control items.
7180 * PARAMETER(S):
7181 * [I] infoPtr : valid pointer to the listview structure
7182 * [I] bSmall : flag for small or large icon
7184 * RETURN:
7185 * Horizontal + vertical spacing
7187 static LONG LISTVIEW_GetItemSpacing(const LISTVIEW_INFO *infoPtr, BOOL bSmall)
7189 LONG lResult;
7191 if (!bSmall)
7193 lResult = MAKELONG(infoPtr->iconSpacing.cx, infoPtr->iconSpacing.cy);
7195 else
7197 if (infoPtr->uView == LV_VIEW_ICON)
7198 lResult = MAKELONG(DEFAULT_COLUMN_WIDTH, GetSystemMetrics(SM_CXSMICON)+HEIGHT_PADDING);
7199 else
7200 lResult = MAKELONG(infoPtr->nItemWidth, infoPtr->nItemHeight);
7202 return lResult;
7205 /***
7206 * DESCRIPTION:
7207 * Retrieves the state of a listview control item.
7209 * PARAMETER(S):
7210 * [I] infoPtr : valid pointer to the listview structure
7211 * [I] nItem : item index
7212 * [I] uMask : state mask
7214 * RETURN:
7215 * State specified by the mask.
7217 static UINT LISTVIEW_GetItemState(const LISTVIEW_INFO *infoPtr, INT nItem, UINT uMask)
7219 LVITEMW lvItem;
7221 if (nItem < 0 || nItem >= infoPtr->nItemCount) return 0;
7223 lvItem.iItem = nItem;
7224 lvItem.iSubItem = 0;
7225 lvItem.mask = LVIF_STATE;
7226 lvItem.stateMask = uMask;
7227 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) return 0;
7229 return lvItem.state & uMask;
7232 /***
7233 * DESCRIPTION:
7234 * Retrieves the text of a listview control item or subitem.
7236 * PARAMETER(S):
7237 * [I] hwnd : window handle
7238 * [I] nItem : item index
7239 * [IO] lpLVItem : item information
7240 * [I] isW : TRUE if lpLVItem is Unicode
7242 * RETURN:
7243 * SUCCESS : string length
7244 * FAILURE : 0
7246 static INT LISTVIEW_GetItemTextT(const LISTVIEW_INFO *infoPtr, INT nItem, LPLVITEMW lpLVItem, BOOL isW)
7248 if (!lpLVItem || nItem < 0 || nItem >= infoPtr->nItemCount) return 0;
7250 lpLVItem->mask = LVIF_TEXT;
7251 lpLVItem->iItem = nItem;
7252 if (!LISTVIEW_GetItemExtT(infoPtr, lpLVItem, isW)) return 0;
7254 return textlenT(lpLVItem->pszText, isW);
7257 /***
7258 * DESCRIPTION:
7259 * Searches for an item based on properties + relationships.
7261 * PARAMETER(S):
7262 * [I] infoPtr : valid pointer to the listview structure
7263 * [I] nItem : item index
7264 * [I] uFlags : relationship flag
7266 * RETURN:
7267 * SUCCESS : item index
7268 * FAILURE : -1
7270 static INT LISTVIEW_GetNextItem(const LISTVIEW_INFO *infoPtr, INT nItem, UINT uFlags)
7272 UINT uMask = 0;
7273 LVFINDINFOW lvFindInfo;
7274 INT nCountPerColumn;
7275 INT nCountPerRow;
7276 INT i;
7278 TRACE("nItem=%d, uFlags=%x, nItemCount=%d\n", nItem, uFlags, infoPtr->nItemCount);
7279 if (nItem < -1 || nItem >= infoPtr->nItemCount) return -1;
7281 ZeroMemory(&lvFindInfo, sizeof(lvFindInfo));
7283 if (uFlags & LVNI_CUT)
7284 uMask |= LVIS_CUT;
7286 if (uFlags & LVNI_DROPHILITED)
7287 uMask |= LVIS_DROPHILITED;
7289 if (uFlags & LVNI_FOCUSED)
7290 uMask |= LVIS_FOCUSED;
7292 if (uFlags & LVNI_SELECTED)
7293 uMask |= LVIS_SELECTED;
7295 /* if we're asked for the focused item, that's only one,
7296 * so it's worth optimizing */
7297 if (uFlags & LVNI_FOCUSED)
7299 if ((LISTVIEW_GetItemState(infoPtr, infoPtr->nFocusedItem, uMask) & uMask) != uMask) return -1;
7300 return (infoPtr->nFocusedItem == nItem) ? -1 : infoPtr->nFocusedItem;
7303 if (uFlags & LVNI_ABOVE)
7305 if ((infoPtr->uView == LV_VIEW_LIST) || (infoPtr->uView == LV_VIEW_DETAILS))
7307 while (nItem >= 0)
7309 nItem--;
7310 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7311 return nItem;
7314 else
7316 /* Special case for autoarrange - move 'til the top of a list */
7317 if (is_autoarrange(infoPtr))
7319 nCountPerRow = LISTVIEW_GetCountPerRow(infoPtr);
7320 while (nItem - nCountPerRow >= 0)
7322 nItem -= nCountPerRow;
7323 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7324 return nItem;
7326 return -1;
7328 lvFindInfo.flags = LVFI_NEARESTXY;
7329 lvFindInfo.vkDirection = VK_UP;
7330 LISTVIEW_GetItemPosition(infoPtr, nItem, &lvFindInfo.pt);
7331 while ((nItem = LISTVIEW_FindItemW(infoPtr, nItem, &lvFindInfo)) != -1)
7333 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7334 return nItem;
7338 else if (uFlags & LVNI_BELOW)
7340 if ((infoPtr->uView == LV_VIEW_LIST) || (infoPtr->uView == LV_VIEW_DETAILS))
7342 while (nItem < infoPtr->nItemCount)
7344 nItem++;
7345 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7346 return nItem;
7349 else
7351 /* Special case for autoarrange - move 'til the bottom of a list */
7352 if (is_autoarrange(infoPtr))
7354 nCountPerRow = LISTVIEW_GetCountPerRow(infoPtr);
7355 while (nItem + nCountPerRow < infoPtr->nItemCount )
7357 nItem += nCountPerRow;
7358 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7359 return nItem;
7361 return -1;
7363 lvFindInfo.flags = LVFI_NEARESTXY;
7364 lvFindInfo.vkDirection = VK_DOWN;
7365 LISTVIEW_GetItemPosition(infoPtr, nItem, &lvFindInfo.pt);
7366 while ((nItem = LISTVIEW_FindItemW(infoPtr, nItem, &lvFindInfo)) != -1)
7368 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7369 return nItem;
7373 else if (uFlags & LVNI_TOLEFT)
7375 if (infoPtr->uView == LV_VIEW_LIST)
7377 nCountPerColumn = LISTVIEW_GetCountPerColumn(infoPtr);
7378 while (nItem - nCountPerColumn >= 0)
7380 nItem -= nCountPerColumn;
7381 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7382 return nItem;
7385 else if ((infoPtr->uView == LV_VIEW_SMALLICON) || (infoPtr->uView == LV_VIEW_ICON))
7387 /* Special case for autoarrange - move 'til the beginning of a row */
7388 if (is_autoarrange(infoPtr))
7390 nCountPerRow = LISTVIEW_GetCountPerRow(infoPtr);
7391 while (nItem % nCountPerRow > 0)
7393 nItem --;
7394 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7395 return nItem;
7397 return -1;
7399 lvFindInfo.flags = LVFI_NEARESTXY;
7400 lvFindInfo.vkDirection = VK_LEFT;
7401 LISTVIEW_GetItemPosition(infoPtr, nItem, &lvFindInfo.pt);
7402 while ((nItem = LISTVIEW_FindItemW(infoPtr, nItem, &lvFindInfo)) != -1)
7404 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7405 return nItem;
7409 else if (uFlags & LVNI_TORIGHT)
7411 if (infoPtr->uView == LV_VIEW_LIST)
7413 nCountPerColumn = LISTVIEW_GetCountPerColumn(infoPtr);
7414 while (nItem + nCountPerColumn < infoPtr->nItemCount)
7416 nItem += nCountPerColumn;
7417 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7418 return nItem;
7421 else if ((infoPtr->uView == LV_VIEW_SMALLICON) || (infoPtr->uView == LV_VIEW_ICON))
7423 /* Special case for autoarrange - move 'til the end of a row */
7424 if (is_autoarrange(infoPtr))
7426 nCountPerRow = LISTVIEW_GetCountPerRow(infoPtr);
7427 while (nItem % nCountPerRow < nCountPerRow - 1 )
7429 nItem ++;
7430 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7431 return nItem;
7433 return -1;
7435 lvFindInfo.flags = LVFI_NEARESTXY;
7436 lvFindInfo.vkDirection = VK_RIGHT;
7437 LISTVIEW_GetItemPosition(infoPtr, nItem, &lvFindInfo.pt);
7438 while ((nItem = LISTVIEW_FindItemW(infoPtr, nItem, &lvFindInfo)) != -1)
7440 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7441 return nItem;
7445 else
7447 nItem++;
7449 /* search by index */
7450 for (i = nItem; i < infoPtr->nItemCount; i++)
7452 if ((LISTVIEW_GetItemState(infoPtr, i, uMask) & uMask) == uMask)
7453 return i;
7457 return -1;
7460 static BOOL LISTVIEW_GetNextItemIndex(const LISTVIEW_INFO *infoPtr, LVITEMINDEX *index, UINT flags)
7462 /* FIXME: specified item group is ignored */
7464 if (!index)
7465 return FALSE;
7467 index->iItem = LISTVIEW_GetNextItem(infoPtr, index->iItem, flags);
7468 return index->iItem != -1;
7471 /* LISTVIEW_GetNumberOfWorkAreas */
7473 /***
7474 * DESCRIPTION:
7475 * Retrieves the origin coordinates when in icon or small icon display mode.
7477 * PARAMETER(S):
7478 * [I] infoPtr : valid pointer to the listview structure
7479 * [O] lpptOrigin : coordinate information
7481 * RETURN:
7482 * None.
7484 static void LISTVIEW_GetOrigin(const LISTVIEW_INFO *infoPtr, LPPOINT lpptOrigin)
7486 INT nHorzPos = 0, nVertPos = 0;
7487 SCROLLINFO scrollInfo;
7489 scrollInfo.cbSize = sizeof(SCROLLINFO);
7490 scrollInfo.fMask = SIF_POS;
7492 if (GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo))
7493 nHorzPos = scrollInfo.nPos;
7494 if (GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo))
7495 nVertPos = scrollInfo.nPos;
7497 TRACE("nHorzPos=%d, nVertPos=%d\n", nHorzPos, nVertPos);
7499 lpptOrigin->x = infoPtr->rcList.left;
7500 lpptOrigin->y = infoPtr->rcList.top;
7501 if (infoPtr->uView == LV_VIEW_LIST)
7502 nHorzPos *= infoPtr->nItemWidth;
7503 else if (infoPtr->uView == LV_VIEW_DETAILS)
7504 nVertPos *= infoPtr->nItemHeight;
7506 lpptOrigin->x -= nHorzPos;
7507 lpptOrigin->y -= nVertPos;
7509 TRACE(" origin=%s\n", wine_dbgstr_point(lpptOrigin));
7512 /***
7513 * DESCRIPTION:
7514 * Retrieves the width of a string.
7516 * PARAMETER(S):
7517 * [I] hwnd : window handle
7518 * [I] lpszText : text string to process
7519 * [I] isW : TRUE if lpszText is Unicode, FALSE otherwise
7521 * RETURN:
7522 * SUCCESS : string width (in pixels)
7523 * FAILURE : zero
7525 static INT LISTVIEW_GetStringWidthT(const LISTVIEW_INFO *infoPtr, LPCWSTR lpszText, BOOL isW)
7527 SIZE stringSize;
7529 stringSize.cx = 0;
7530 if (is_text(lpszText))
7532 HFONT hFont = infoPtr->hFont ? infoPtr->hFont : infoPtr->hDefaultFont;
7533 HDC hdc = GetDC(infoPtr->hwndSelf);
7534 HFONT hOldFont = SelectObject(hdc, hFont);
7536 if (isW)
7537 GetTextExtentPointW(hdc, lpszText, lstrlenW(lpszText), &stringSize);
7538 else
7539 GetTextExtentPointA(hdc, (LPCSTR)lpszText, lstrlenA((LPCSTR)lpszText), &stringSize);
7540 SelectObject(hdc, hOldFont);
7541 ReleaseDC(infoPtr->hwndSelf, hdc);
7543 return stringSize.cx;
7546 /***
7547 * DESCRIPTION:
7548 * Determines which listview item is located at the specified position.
7550 * PARAMETER(S):
7551 * [I] infoPtr : valid pointer to the listview structure
7552 * [IO] lpht : hit test information
7553 * [I] subitem : fill out iSubItem.
7554 * [I] select : return the index only if the hit selects the item
7556 * NOTE:
7557 * (mm 20001022): We must not allow iSubItem to be touched, for
7558 * an app might pass only a structure with space up to iItem!
7559 * (MS Office 97 does that for instance in the file open dialog)
7561 * RETURN:
7562 * SUCCESS : item index
7563 * FAILURE : -1
7565 static INT LISTVIEW_HitTest(const LISTVIEW_INFO *infoPtr, LPLVHITTESTINFO lpht, BOOL subitem, BOOL select)
7567 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
7568 RECT rcBox, rcBounds, rcState, rcIcon, rcLabel, rcSearch;
7569 POINT Origin, Position, opt;
7570 BOOL is_fullrow;
7571 LVITEMW lvItem;
7572 ITERATOR i;
7573 INT iItem;
7575 TRACE("(pt=%s, subitem=%d, select=%d)\n", wine_dbgstr_point(&lpht->pt), subitem, select);
7577 lpht->flags = 0;
7578 lpht->iItem = -1;
7579 if (subitem) lpht->iSubItem = 0;
7581 LISTVIEW_GetOrigin(infoPtr, &Origin);
7583 /* set whole list relation flags */
7584 if (subitem && infoPtr->uView == LV_VIEW_DETAILS)
7586 /* LVM_SUBITEMHITTEST checks left bound of possible client area */
7587 if (infoPtr->rcList.left > lpht->pt.x && Origin.x < lpht->pt.x)
7588 lpht->flags |= LVHT_TOLEFT;
7590 if (lpht->pt.y < infoPtr->rcList.top && lpht->pt.y >= 0)
7591 opt.y = lpht->pt.y + infoPtr->rcList.top;
7592 else
7593 opt.y = lpht->pt.y;
7595 if (infoPtr->rcList.bottom < opt.y)
7596 lpht->flags |= LVHT_BELOW;
7598 else
7600 if (infoPtr->rcList.left > lpht->pt.x)
7601 lpht->flags |= LVHT_TOLEFT;
7602 else if (infoPtr->rcList.right < lpht->pt.x)
7603 lpht->flags |= LVHT_TORIGHT;
7605 if (infoPtr->rcList.top > lpht->pt.y)
7606 lpht->flags |= LVHT_ABOVE;
7607 else if (infoPtr->rcList.bottom < lpht->pt.y)
7608 lpht->flags |= LVHT_BELOW;
7611 /* even if item is invalid try to find subitem */
7612 if (infoPtr->uView == LV_VIEW_DETAILS && subitem)
7614 RECT *pRect;
7615 INT j;
7617 opt.x = lpht->pt.x - Origin.x;
7619 lpht->iSubItem = -1;
7620 for (j = 0; j < DPA_GetPtrCount(infoPtr->hdpaColumns); j++)
7622 pRect = &LISTVIEW_GetColumnInfo(infoPtr, j)->rcHeader;
7624 if ((opt.x >= pRect->left) && (opt.x < pRect->right))
7626 lpht->iSubItem = j;
7627 break;
7630 TRACE("lpht->iSubItem=%d\n", lpht->iSubItem);
7632 /* if we're outside horizontal columns bounds there's nothing to test further */
7633 if (lpht->iSubItem == -1)
7635 lpht->iItem = -1;
7636 lpht->flags = LVHT_NOWHERE;
7637 return -1;
7641 TRACE("lpht->flags=0x%x\n", lpht->flags);
7642 if (lpht->flags) return -1;
7644 lpht->flags |= LVHT_NOWHERE;
7646 /* first deal with the large items */
7647 rcSearch.left = lpht->pt.x;
7648 rcSearch.top = lpht->pt.y;
7649 rcSearch.right = rcSearch.left + 1;
7650 rcSearch.bottom = rcSearch.top + 1;
7652 iterator_frameditems(&i, infoPtr, &rcSearch);
7653 iterator_next(&i); /* go to first item in the sequence */
7654 iItem = i.nItem;
7655 iterator_destroy(&i);
7657 TRACE("lpht->iItem=%d\n", iItem);
7658 if (iItem == -1) return -1;
7660 lvItem.mask = LVIF_STATE | LVIF_TEXT;
7661 if (infoPtr->uView == LV_VIEW_DETAILS) lvItem.mask |= LVIF_INDENT;
7662 lvItem.stateMask = LVIS_STATEIMAGEMASK;
7663 if (infoPtr->uView == LV_VIEW_ICON) lvItem.stateMask |= LVIS_FOCUSED;
7664 lvItem.iItem = iItem;
7665 lvItem.iSubItem = subitem ? lpht->iSubItem : 0;
7666 lvItem.pszText = szDispText;
7667 lvItem.cchTextMax = DISP_TEXT_SIZE;
7668 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) return -1;
7669 if (!infoPtr->bFocus) lvItem.state &= ~LVIS_FOCUSED;
7671 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, &rcBox, NULL, &rcIcon, &rcState, &rcLabel);
7672 LISTVIEW_GetItemOrigin(infoPtr, iItem, &Position);
7673 opt.x = lpht->pt.x - Position.x - Origin.x;
7675 if (lpht->pt.y < infoPtr->rcList.top && lpht->pt.y >= 0)
7676 opt.y = lpht->pt.y - Position.y - Origin.y + infoPtr->rcList.top;
7677 else
7678 opt.y = lpht->pt.y - Position.y - Origin.y;
7680 if (infoPtr->uView == LV_VIEW_DETAILS)
7682 rcBounds = rcBox;
7683 if (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT)
7684 opt.x = lpht->pt.x - Origin.x;
7686 else
7688 UnionRect(&rcBounds, &rcIcon, &rcLabel);
7689 UnionRect(&rcBounds, &rcBounds, &rcState);
7691 TRACE("rcBounds=%s\n", wine_dbgstr_rect(&rcBounds));
7692 if (!PtInRect(&rcBounds, opt)) return -1;
7694 /* That's a special case - row rectangle is used as item rectangle and
7695 returned flags contain all item parts. */
7696 is_fullrow = (infoPtr->uView == LV_VIEW_DETAILS) && ((infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT) || (infoPtr->dwStyle & LVS_OWNERDRAWFIXED));
7698 if (PtInRect(&rcIcon, opt))
7699 lpht->flags |= LVHT_ONITEMICON;
7700 else if (PtInRect(&rcLabel, opt))
7701 lpht->flags |= LVHT_ONITEMLABEL;
7702 else if (infoPtr->himlState && PtInRect(&rcState, opt))
7703 lpht->flags |= LVHT_ONITEMSTATEICON;
7704 if (is_fullrow && !(lpht->flags & LVHT_ONITEM))
7706 lpht->flags = LVHT_ONITEM | LVHT_ABOVE;
7708 if (lpht->flags & LVHT_ONITEM)
7709 lpht->flags &= ~LVHT_NOWHERE;
7710 TRACE("lpht->flags=0x%x\n", lpht->flags);
7712 if (select && !is_fullrow)
7714 if (infoPtr->uView == LV_VIEW_DETAILS)
7716 /* get main item bounds */
7717 lvItem.iSubItem = 0;
7718 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, &rcBox, NULL, &rcIcon, &rcState, &rcLabel);
7719 UnionRect(&rcBounds, &rcIcon, &rcLabel);
7720 UnionRect(&rcBounds, &rcBounds, &rcState);
7722 if (!PtInRect(&rcBounds, opt)) iItem = -1;
7724 return lpht->iItem = iItem;
7727 /***
7728 * DESCRIPTION:
7729 * Inserts a new item in the listview control.
7731 * PARAMETER(S):
7732 * [I] infoPtr : valid pointer to the listview structure
7733 * [I] lpLVItem : item information
7734 * [I] isW : TRUE if lpLVItem is Unicode, FALSE if it's ANSI
7736 * RETURN:
7737 * SUCCESS : new item index
7738 * FAILURE : -1
7740 static INT LISTVIEW_InsertItemT(LISTVIEW_INFO *infoPtr, const LVITEMW *lpLVItem, BOOL isW)
7742 INT nItem;
7743 HDPA hdpaSubItems;
7744 NMLISTVIEW nmlv;
7745 ITEM_INFO *lpItem;
7746 ITEM_ID *lpID;
7747 BOOL is_sorted, has_changed;
7748 LVITEMW item;
7749 HWND hwndSelf = infoPtr->hwndSelf;
7751 TRACE("(item=%s, isW=%d)\n", debuglvitem_t(lpLVItem, isW), isW);
7753 if (infoPtr->dwStyle & LVS_OWNERDATA) return infoPtr->nItemCount++;
7755 /* make sure it's an item, and not a subitem; cannot insert a subitem */
7756 if (!lpLVItem || lpLVItem->iSubItem) return -1;
7758 if (!is_assignable_item(lpLVItem, infoPtr->dwStyle)) return -1;
7760 if (!(lpItem = Alloc(sizeof(ITEM_INFO)))) return -1;
7762 /* insert item in listview control data structure */
7763 if ( !(hdpaSubItems = DPA_Create(8)) ) goto fail;
7764 if ( !DPA_SetPtr(hdpaSubItems, 0, lpItem) ) assert (FALSE);
7766 /* link with id struct */
7767 if (!(lpID = Alloc(sizeof(ITEM_ID)))) goto fail;
7768 lpItem->id = lpID;
7769 lpID->item = hdpaSubItems;
7770 lpID->id = get_next_itemid(infoPtr);
7771 if ( DPA_InsertPtr(infoPtr->hdpaItemIds, infoPtr->nItemCount, lpID) == -1) goto fail;
7773 is_sorted = (infoPtr->dwStyle & (LVS_SORTASCENDING | LVS_SORTDESCENDING)) &&
7774 !(infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && (LPSTR_TEXTCALLBACKW != lpLVItem->pszText);
7776 if (lpLVItem->iItem < 0 && !is_sorted) return -1;
7778 /* calculate new item index */
7779 if (is_sorted)
7781 HDPA hItem;
7782 ITEM_INFO *item_s;
7783 INT i = 0, cmpv;
7784 WCHAR *textW;
7786 textW = textdupTtoW(lpLVItem->pszText, isW);
7788 while (i < infoPtr->nItemCount)
7790 hItem = DPA_GetPtr( infoPtr->hdpaItems, i);
7791 item_s = DPA_GetPtr(hItem, 0);
7793 cmpv = textcmpWT(item_s->hdr.pszText, textW, TRUE);
7794 if (infoPtr->dwStyle & LVS_SORTDESCENDING) cmpv *= -1;
7796 if (cmpv >= 0) break;
7797 i++;
7800 textfreeT(textW, isW);
7802 nItem = i;
7804 else
7805 nItem = min(lpLVItem->iItem, infoPtr->nItemCount);
7807 TRACE("inserting at %d, sorted=%d, count=%d, iItem=%d\n", nItem, is_sorted, infoPtr->nItemCount, lpLVItem->iItem);
7808 nItem = DPA_InsertPtr( infoPtr->hdpaItems, nItem, hdpaSubItems );
7809 if (nItem == -1) goto fail;
7810 infoPtr->nItemCount++;
7812 /* shift indices first so they don't get tangled */
7813 LISTVIEW_ShiftIndices(infoPtr, nItem, 1);
7815 /* set the item attributes */
7816 if (lpLVItem->mask & (LVIF_GROUPID|LVIF_COLUMNS))
7818 /* full size structure expected - _WIN32IE >= 0x560 */
7819 item = *lpLVItem;
7821 else if (lpLVItem->mask & LVIF_INDENT)
7823 /* indent member expected - _WIN32IE >= 0x300 */
7824 memcpy(&item, lpLVItem, offsetof( LVITEMW, iGroupId ));
7826 else
7828 /* minimal structure expected */
7829 memcpy(&item, lpLVItem, offsetof( LVITEMW, iIndent ));
7831 item.iItem = nItem;
7832 if (infoPtr->dwLvExStyle & LVS_EX_CHECKBOXES)
7834 if (item.mask & LVIF_STATE)
7836 item.stateMask |= LVIS_STATEIMAGEMASK;
7837 item.state &= ~LVIS_STATEIMAGEMASK;
7838 item.state |= INDEXTOSTATEIMAGEMASK(1);
7840 else
7842 item.mask |= LVIF_STATE;
7843 item.stateMask = LVIS_STATEIMAGEMASK;
7844 item.state = INDEXTOSTATEIMAGEMASK(1);
7848 if (!set_main_item(infoPtr, &item, TRUE, isW, &has_changed)) goto undo;
7850 /* make room for the position, if we are in the right mode */
7851 if ((infoPtr->uView == LV_VIEW_SMALLICON) || (infoPtr->uView == LV_VIEW_ICON))
7853 if (DPA_InsertPtr(infoPtr->hdpaPosX, nItem, 0) == -1)
7854 goto undo;
7855 if (DPA_InsertPtr(infoPtr->hdpaPosY, nItem, 0) == -1)
7857 DPA_DeletePtr(infoPtr->hdpaPosX, nItem);
7858 goto undo;
7862 /* send LVN_INSERTITEM notification */
7863 memset(&nmlv, 0, sizeof(NMLISTVIEW));
7864 nmlv.iItem = nItem;
7865 nmlv.lParam = lpItem->lParam;
7866 notify_listview(infoPtr, LVN_INSERTITEM, &nmlv);
7867 if (!IsWindow(hwndSelf))
7868 return -1;
7870 /* align items (set position of each item) */
7871 if (infoPtr->uView == LV_VIEW_SMALLICON || infoPtr->uView == LV_VIEW_ICON)
7873 POINT pt;
7875 if (infoPtr->dwStyle & LVS_ALIGNLEFT)
7876 LISTVIEW_NextIconPosLeft(infoPtr, &pt);
7877 else
7878 LISTVIEW_NextIconPosTop(infoPtr, &pt);
7880 LISTVIEW_MoveIconTo(infoPtr, nItem, &pt, TRUE);
7883 /* now is the invalidation fun */
7884 LISTVIEW_ScrollOnInsert(infoPtr, nItem, 1);
7885 return nItem;
7887 undo:
7888 LISTVIEW_ShiftIndices(infoPtr, nItem, -1);
7889 LISTVIEW_ShiftFocus(infoPtr, infoPtr->nFocusedItem, nItem, -1);
7890 DPA_DeletePtr(infoPtr->hdpaItems, nItem);
7891 infoPtr->nItemCount--;
7892 fail:
7893 DPA_DeletePtr(hdpaSubItems, 0);
7894 DPA_Destroy (hdpaSubItems);
7895 Free (lpItem);
7896 return -1;
7899 /***
7900 * DESCRIPTION:
7901 * Checks item visibility.
7903 * PARAMETER(S):
7904 * [I] infoPtr : valid pointer to the listview structure
7905 * [I] nFirst : item index to check for
7907 * RETURN:
7908 * Item visible : TRUE
7909 * Item invisible or failure : FALSE
7911 static BOOL LISTVIEW_IsItemVisible(const LISTVIEW_INFO *infoPtr, INT nItem)
7913 POINT Origin, Position;
7914 RECT rcItem;
7915 HDC hdc;
7916 BOOL ret;
7918 TRACE("nItem=%d\n", nItem);
7920 if (nItem < 0 || nItem >= DPA_GetPtrCount(infoPtr->hdpaItems)) return FALSE;
7922 LISTVIEW_GetOrigin(infoPtr, &Origin);
7923 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position);
7924 rcItem.left = Position.x + Origin.x;
7925 rcItem.top = Position.y + Origin.y;
7926 rcItem.right = rcItem.left + infoPtr->nItemWidth;
7927 rcItem.bottom = rcItem.top + infoPtr->nItemHeight;
7929 hdc = GetDC(infoPtr->hwndSelf);
7930 if (!hdc) return FALSE;
7931 ret = RectVisible(hdc, &rcItem);
7932 ReleaseDC(infoPtr->hwndSelf, hdc);
7934 return ret;
7937 /***
7938 * DESCRIPTION:
7939 * Redraws a range of items.
7941 * PARAMETER(S):
7942 * [I] infoPtr : valid pointer to the listview structure
7943 * [I] nFirst : first item
7944 * [I] nLast : last item
7946 * RETURN:
7947 * SUCCESS : TRUE
7948 * FAILURE : FALSE
7950 static BOOL LISTVIEW_RedrawItems(const LISTVIEW_INFO *infoPtr, INT nFirst, INT nLast)
7952 INT i;
7954 for (i = max(nFirst, 0); i <= min(nLast, infoPtr->nItemCount - 1); i++)
7955 LISTVIEW_InvalidateItem(infoPtr, i);
7957 return TRUE;
7960 /***
7961 * DESCRIPTION:
7962 * Scroll the content of a listview.
7964 * PARAMETER(S):
7965 * [I] infoPtr : valid pointer to the listview structure
7966 * [I] dx : horizontal scroll amount in pixels
7967 * [I] dy : vertical scroll amount in pixels
7969 * RETURN:
7970 * SUCCESS : TRUE
7971 * FAILURE : FALSE
7973 * COMMENTS:
7974 * If the control is in report view (LV_VIEW_DETAILS) the control can
7975 * be scrolled only in line increments. "dy" will be rounded to the
7976 * nearest number of pixels that are a whole line. Ex: if line height
7977 * is 16 and an 8 is passed, the list will be scrolled by 16. If a 7
7978 * is passed, then the scroll will be 0. (per MSDN 7/2002)
7980 static BOOL LISTVIEW_Scroll(LISTVIEW_INFO *infoPtr, INT dx, INT dy)
7982 switch(infoPtr->uView) {
7983 case LV_VIEW_DETAILS:
7984 dy += (dy < 0 ? -1 : 1) * infoPtr->nItemHeight/2;
7985 dy /= infoPtr->nItemHeight;
7986 break;
7987 case LV_VIEW_LIST:
7988 if (dy != 0) return FALSE;
7989 break;
7990 default: /* icon */
7991 break;
7994 if (dx != 0) LISTVIEW_HScroll(infoPtr, SB_INTERNAL, dx);
7995 if (dy != 0) LISTVIEW_VScroll(infoPtr, SB_INTERNAL, dy);
7997 return TRUE;
8000 /***
8001 * DESCRIPTION:
8002 * Sets the background color.
8004 * PARAMETER(S):
8005 * [I] infoPtr : valid pointer to the listview structure
8006 * [I] color : background color
8008 * RETURN:
8009 * SUCCESS : TRUE
8010 * FAILURE : FALSE
8012 static BOOL LISTVIEW_SetBkColor(LISTVIEW_INFO *infoPtr, COLORREF color)
8014 TRACE("color %lx\n", color);
8016 if(infoPtr->clrBk != color) {
8017 if (infoPtr->clrBk != CLR_NONE) DeleteObject(infoPtr->hBkBrush);
8018 infoPtr->clrBk = color;
8019 if (color == CLR_NONE)
8020 infoPtr->hBkBrush = (HBRUSH)GetClassLongPtrW(infoPtr->hwndSelf, GCLP_HBRBACKGROUND);
8021 else
8023 infoPtr->hBkBrush = CreateSolidBrush(color);
8024 infoPtr->dwLvExStyle &= ~LVS_EX_TRANSPARENTBKGND;
8028 return TRUE;
8031 /* LISTVIEW_SetBkImage */
8033 /*** Helper for {Insert,Set}ColumnT *only* */
8034 static void column_fill_hditem(const LISTVIEW_INFO *infoPtr, HDITEMW *lphdi, INT nColumn,
8035 const LVCOLUMNW *lpColumn, BOOL isW)
8037 if (lpColumn->mask & LVCF_FMT)
8039 /* format member is valid */
8040 lphdi->mask |= HDI_FORMAT;
8042 /* set text alignment (leftmost column must be left-aligned) */
8043 if (nColumn == 0 || (lpColumn->fmt & LVCFMT_JUSTIFYMASK) == LVCFMT_LEFT)
8044 lphdi->fmt |= HDF_LEFT;
8045 else if ((lpColumn->fmt & LVCFMT_JUSTIFYMASK) == LVCFMT_RIGHT)
8046 lphdi->fmt |= HDF_RIGHT;
8047 else if ((lpColumn->fmt & LVCFMT_JUSTIFYMASK) == LVCFMT_CENTER)
8048 lphdi->fmt |= HDF_CENTER;
8050 if (lpColumn->fmt & LVCFMT_BITMAP_ON_RIGHT)
8051 lphdi->fmt |= HDF_BITMAP_ON_RIGHT;
8053 if (lpColumn->fmt & LVCFMT_COL_HAS_IMAGES)
8055 lphdi->fmt |= HDF_IMAGE;
8056 lphdi->iImage = I_IMAGECALLBACK;
8059 if (lpColumn->fmt & LVCFMT_FIXED_WIDTH)
8060 lphdi->fmt |= HDF_FIXEDWIDTH;
8063 if (lpColumn->mask & LVCF_WIDTH)
8065 lphdi->mask |= HDI_WIDTH;
8066 if(lpColumn->cx == LVSCW_AUTOSIZE_USEHEADER)
8068 /* make it fill the remainder of the controls width */
8069 RECT rcHeader;
8070 INT item_index;
8072 for(item_index = 0; item_index < (nColumn - 1); item_index++)
8074 LISTVIEW_GetHeaderRect(infoPtr, item_index, &rcHeader);
8075 lphdi->cxy += rcHeader.right - rcHeader.left;
8078 /* retrieve the layout of the header */
8079 GetClientRect(infoPtr->hwndSelf, &rcHeader);
8080 TRACE("start cxy=%d rcHeader=%s\n", lphdi->cxy, wine_dbgstr_rect(&rcHeader));
8082 lphdi->cxy = (rcHeader.right - rcHeader.left) - lphdi->cxy;
8084 else
8085 lphdi->cxy = lpColumn->cx;
8088 if (lpColumn->mask & LVCF_TEXT)
8090 lphdi->mask |= HDI_TEXT | HDI_FORMAT;
8091 lphdi->fmt |= HDF_STRING;
8092 lphdi->pszText = lpColumn->pszText;
8093 lphdi->cchTextMax = textlenT(lpColumn->pszText, isW);
8096 if (lpColumn->mask & LVCF_IMAGE)
8098 lphdi->mask |= HDI_IMAGE;
8099 lphdi->iImage = lpColumn->iImage;
8102 if (lpColumn->mask & LVCF_ORDER)
8104 lphdi->mask |= HDI_ORDER;
8105 lphdi->iOrder = lpColumn->iOrder;
8110 /***
8111 * DESCRIPTION:
8112 * Inserts a new column.
8114 * PARAMETER(S):
8115 * [I] infoPtr : valid pointer to the listview structure
8116 * [I] nColumn : column index
8117 * [I] lpColumn : column information
8118 * [I] isW : TRUE if lpColumn is Unicode, FALSE otherwise
8120 * RETURN:
8121 * SUCCESS : new column index
8122 * FAILURE : -1
8124 static INT LISTVIEW_InsertColumnT(LISTVIEW_INFO *infoPtr, INT nColumn,
8125 const LVCOLUMNW *lpColumn, BOOL isW)
8127 COLUMN_INFO *lpColumnInfo;
8128 INT nNewColumn;
8129 HDITEMW hdi;
8131 TRACE("(nColumn=%d, lpColumn=%s, isW=%d)\n", nColumn, debuglvcolumn_t(lpColumn, isW), isW);
8133 if (!lpColumn || nColumn < 0) return -1;
8134 nColumn = min(nColumn, DPA_GetPtrCount(infoPtr->hdpaColumns));
8136 ZeroMemory(&hdi, sizeof(HDITEMW));
8137 column_fill_hditem(infoPtr, &hdi, nColumn, lpColumn, isW);
8140 * A mask not including LVCF_WIDTH turns into a mask of width, width 10
8141 * (can be seen in SPY) otherwise column never gets added.
8143 if (!(lpColumn->mask & LVCF_WIDTH)) {
8144 hdi.mask |= HDI_WIDTH;
8145 hdi.cxy = 10;
8149 * when the iSubItem is available Windows copies it to the header lParam. It seems
8150 * to happen only in LVM_INSERTCOLUMN - not in LVM_SETCOLUMN
8152 if (lpColumn->mask & LVCF_SUBITEM)
8154 hdi.mask |= HDI_LPARAM;
8155 hdi.lParam = lpColumn->iSubItem;
8158 /* create header if not present */
8159 LISTVIEW_CreateHeader(infoPtr);
8160 if (!(LVS_NOCOLUMNHEADER & infoPtr->dwStyle) &&
8161 (infoPtr->uView == LV_VIEW_DETAILS) && (WS_VISIBLE & infoPtr->dwStyle))
8163 ShowWindow(infoPtr->hwndHeader, SW_SHOWNORMAL);
8166 /* insert item in header control */
8167 nNewColumn = SendMessageW(infoPtr->hwndHeader,
8168 isW ? HDM_INSERTITEMW : HDM_INSERTITEMA,
8169 nColumn, (LPARAM)&hdi);
8170 if (nNewColumn == -1) return -1;
8171 if (nNewColumn != nColumn) ERR("nColumn=%d, nNewColumn=%d\n", nColumn, nNewColumn);
8173 /* create our own column info */
8174 if (!(lpColumnInfo = Alloc(sizeof(COLUMN_INFO)))) goto fail;
8175 if (DPA_InsertPtr(infoPtr->hdpaColumns, nNewColumn, lpColumnInfo) == -1) goto fail;
8177 if (lpColumn->mask & LVCF_FMT) lpColumnInfo->fmt = lpColumn->fmt;
8178 if (lpColumn->mask & LVCF_MINWIDTH) lpColumnInfo->cxMin = lpColumn->cxMin;
8179 if (!SendMessageW(infoPtr->hwndHeader, HDM_GETITEMRECT, nNewColumn, (LPARAM)&lpColumnInfo->rcHeader))
8180 goto fail;
8182 /* now we have to actually adjust the data */
8183 if (!(infoPtr->dwStyle & LVS_OWNERDATA) && infoPtr->nItemCount > 0)
8185 SUBITEM_INFO *lpSubItem;
8186 HDPA hdpaSubItems;
8187 INT nItem, i;
8188 LVITEMW item;
8189 BOOL changed;
8191 item.iSubItem = nNewColumn;
8192 item.mask = LVIF_TEXT | LVIF_IMAGE;
8193 item.iImage = I_IMAGECALLBACK;
8194 item.pszText = LPSTR_TEXTCALLBACKW;
8196 for (nItem = 0; nItem < infoPtr->nItemCount; nItem++)
8198 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, nItem);
8199 for (i = 1; i < DPA_GetPtrCount(hdpaSubItems); i++)
8201 lpSubItem = DPA_GetPtr(hdpaSubItems, i);
8202 if (lpSubItem->iSubItem >= nNewColumn)
8203 lpSubItem->iSubItem++;
8206 /* add new subitem for each item */
8207 item.iItem = nItem;
8208 set_sub_item(infoPtr, &item, isW, &changed);
8212 /* make space for the new column */
8213 LISTVIEW_ScrollColumns(infoPtr, nNewColumn + 1, lpColumnInfo->rcHeader.right - lpColumnInfo->rcHeader.left);
8214 LISTVIEW_UpdateItemSize(infoPtr);
8216 return nNewColumn;
8218 fail:
8219 if (nNewColumn != -1) SendMessageW(infoPtr->hwndHeader, HDM_DELETEITEM, nNewColumn, 0);
8220 if (lpColumnInfo)
8222 DPA_DeletePtr(infoPtr->hdpaColumns, nNewColumn);
8223 Free(lpColumnInfo);
8225 return -1;
8228 /***
8229 * DESCRIPTION:
8230 * Sets the attributes of a header item.
8232 * PARAMETER(S):
8233 * [I] infoPtr : valid pointer to the listview structure
8234 * [I] nColumn : column index
8235 * [I] lpColumn : column attributes
8236 * [I] isW: if TRUE, then lpColumn is a LPLVCOLUMNW, else it is a LPLVCOLUMNA
8238 * RETURN:
8239 * SUCCESS : TRUE
8240 * FAILURE : FALSE
8242 static BOOL LISTVIEW_SetColumnT(const LISTVIEW_INFO *infoPtr, INT nColumn,
8243 const LVCOLUMNW *lpColumn, BOOL isW)
8245 HDITEMW hdi, hdiget;
8246 BOOL bResult;
8248 TRACE("(nColumn=%d, lpColumn=%s, isW=%d)\n", nColumn, debuglvcolumn_t(lpColumn, isW), isW);
8250 if (!lpColumn || nColumn < 0 || nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return FALSE;
8252 ZeroMemory(&hdi, sizeof(HDITEMW));
8253 if (lpColumn->mask & LVCF_FMT)
8255 hdi.mask |= HDI_FORMAT;
8256 hdiget.mask = HDI_FORMAT;
8257 if (SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, nColumn, (LPARAM)&hdiget))
8258 hdi.fmt = hdiget.fmt & HDF_STRING;
8260 column_fill_hditem(infoPtr, &hdi, nColumn, lpColumn, isW);
8262 /* set header item attributes */
8263 bResult = SendMessageW(infoPtr->hwndHeader, isW ? HDM_SETITEMW : HDM_SETITEMA, nColumn, (LPARAM)&hdi);
8264 if (!bResult) return FALSE;
8266 if (lpColumn->mask & LVCF_FMT)
8268 COLUMN_INFO *lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, nColumn);
8269 INT oldFmt = lpColumnInfo->fmt;
8271 lpColumnInfo->fmt = lpColumn->fmt;
8272 if ((oldFmt ^ lpColumn->fmt) & (LVCFMT_JUSTIFYMASK | LVCFMT_IMAGE))
8274 if (infoPtr->uView == LV_VIEW_DETAILS) LISTVIEW_InvalidateColumn(infoPtr, nColumn);
8278 if (lpColumn->mask & LVCF_MINWIDTH)
8279 LISTVIEW_GetColumnInfo(infoPtr, nColumn)->cxMin = lpColumn->cxMin;
8281 return TRUE;
8284 /***
8285 * DESCRIPTION:
8286 * Sets the column order array
8288 * PARAMETERS:
8289 * [I] infoPtr : valid pointer to the listview structure
8290 * [I] iCount : number of elements in column order array
8291 * [I] lpiArray : pointer to column order array
8293 * RETURN:
8294 * SUCCESS : TRUE
8295 * FAILURE : FALSE
8297 static BOOL LISTVIEW_SetColumnOrderArray(LISTVIEW_INFO *infoPtr, INT iCount, const INT *lpiArray)
8299 if (!infoPtr->hwndHeader) return FALSE;
8300 infoPtr->colRectsDirty = TRUE;
8301 return SendMessageW(infoPtr->hwndHeader, HDM_SETORDERARRAY, iCount, (LPARAM)lpiArray);
8304 /***
8305 * DESCRIPTION:
8306 * Sets the width of a column
8308 * PARAMETERS:
8309 * [I] infoPtr : valid pointer to the listview structure
8310 * [I] nColumn : column index
8311 * [I] cx : column width
8313 * RETURN:
8314 * SUCCESS : TRUE
8315 * FAILURE : FALSE
8317 static BOOL LISTVIEW_SetColumnWidth(LISTVIEW_INFO *infoPtr, INT nColumn, INT cx)
8319 WCHAR szDispText[DISP_TEXT_SIZE] = { 0 };
8320 INT max_cx = 0;
8321 HDITEMW hdi;
8323 TRACE("(nColumn=%d, cx=%d)\n", nColumn, cx);
8325 /* set column width only if in report or list mode */
8326 if (infoPtr->uView != LV_VIEW_DETAILS && infoPtr->uView != LV_VIEW_LIST) return FALSE;
8328 /* take care of invalid cx values - LVSCW_AUTOSIZE_* values are negative,
8329 with _USEHEADER being the lowest */
8330 if (infoPtr->uView == LV_VIEW_DETAILS && cx < LVSCW_AUTOSIZE_USEHEADER) cx = LVSCW_AUTOSIZE;
8331 else if (infoPtr->uView == LV_VIEW_LIST && cx <= 0) return FALSE;
8333 /* resize all columns if in LV_VIEW_LIST mode */
8334 if(infoPtr->uView == LV_VIEW_LIST)
8336 infoPtr->nItemWidth = cx;
8337 LISTVIEW_InvalidateList(infoPtr);
8338 return TRUE;
8341 if (nColumn < 0 || nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return FALSE;
8343 if (cx == LVSCW_AUTOSIZE || (cx == LVSCW_AUTOSIZE_USEHEADER && nColumn < DPA_GetPtrCount(infoPtr->hdpaColumns) -1))
8345 INT nLabelWidth;
8346 LVITEMW lvItem;
8348 lvItem.mask = LVIF_TEXT;
8349 lvItem.iItem = 0;
8350 lvItem.iSubItem = nColumn;
8351 lvItem.cchTextMax = DISP_TEXT_SIZE;
8352 for (; lvItem.iItem < infoPtr->nItemCount; lvItem.iItem++)
8354 lvItem.pszText = szDispText;
8355 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) continue;
8356 nLabelWidth = LISTVIEW_GetStringWidthT(infoPtr, lvItem.pszText, TRUE);
8357 if (max_cx < nLabelWidth) max_cx = nLabelWidth;
8359 if (infoPtr->himlSmall && (nColumn == 0 || (LISTVIEW_GetColumnInfo(infoPtr, nColumn)->fmt & LVCFMT_IMAGE)))
8360 max_cx += infoPtr->iconSize.cx;
8361 max_cx += TRAILING_LABEL_PADDING;
8362 if (nColumn == 0 && (infoPtr->dwLvExStyle & LVS_EX_CHECKBOXES))
8363 max_cx += GetSystemMetrics(SM_CXSMICON);
8366 /* autosize based on listview items width */
8367 if(cx == LVSCW_AUTOSIZE)
8368 cx = max_cx;
8369 else if(cx == LVSCW_AUTOSIZE_USEHEADER)
8371 /* if iCol is the last column make it fill the remainder of the controls width */
8372 if(nColumn == DPA_GetPtrCount(infoPtr->hdpaColumns) - 1)
8374 RECT rcHeader;
8375 POINT Origin;
8377 LISTVIEW_GetOrigin(infoPtr, &Origin);
8378 LISTVIEW_GetHeaderRect(infoPtr, nColumn, &rcHeader);
8380 cx = infoPtr->rcList.right - Origin.x - rcHeader.left;
8382 else
8384 /* Despite what the MS docs say, if this is not the last
8385 column, then MS resizes the column to the width of the
8386 largest text string in the column, including headers
8387 and items. This is different from LVSCW_AUTOSIZE in that
8388 LVSCW_AUTOSIZE ignores the header string length. */
8389 cx = 0;
8391 /* retrieve header text */
8392 hdi.mask = HDI_TEXT|HDI_FORMAT|HDI_IMAGE|HDI_BITMAP;
8393 hdi.cchTextMax = DISP_TEXT_SIZE;
8394 hdi.pszText = szDispText;
8395 if (SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, nColumn, (LPARAM)&hdi))
8397 HDC hdc = GetDC(infoPtr->hwndSelf);
8398 HFONT old_font = SelectObject(hdc, (HFONT)SendMessageW(infoPtr->hwndHeader, WM_GETFONT, 0, 0));
8399 HIMAGELIST himl = (HIMAGELIST)SendMessageW(infoPtr->hwndHeader, HDM_GETIMAGELIST, 0, 0);
8400 INT bitmap_margin = 0;
8401 SIZE size;
8403 if (GetTextExtentPoint32W(hdc, hdi.pszText, lstrlenW(hdi.pszText), &size))
8404 cx = size.cx + TRAILING_HEADER_PADDING;
8406 if (hdi.fmt & (HDF_IMAGE|HDF_BITMAP))
8407 bitmap_margin = SendMessageW(infoPtr->hwndHeader, HDM_GETBITMAPMARGIN, 0, 0);
8409 if ((hdi.fmt & HDF_IMAGE) && himl)
8411 INT icon_cx, icon_cy;
8413 if (!ImageList_GetIconSize(himl, &icon_cx, &icon_cy))
8414 cx += icon_cx + 2*bitmap_margin;
8416 else if (hdi.fmt & HDF_BITMAP)
8418 BITMAP bmp;
8420 GetObjectW(hdi.hbm, sizeof(BITMAP), &bmp);
8421 cx += bmp.bmWidth + 2*bitmap_margin;
8424 SelectObject(hdc, old_font);
8425 ReleaseDC(infoPtr->hwndSelf, hdc);
8427 cx = max (cx, max_cx);
8431 if (cx < 0) return FALSE;
8433 /* call header to update the column change */
8434 hdi.mask = HDI_WIDTH;
8435 hdi.cxy = max(cx, LISTVIEW_GetColumnInfo(infoPtr, nColumn)->cxMin);
8436 TRACE("hdi.cxy=%d\n", hdi.cxy);
8437 return SendMessageW(infoPtr->hwndHeader, HDM_SETITEMW, nColumn, (LPARAM)&hdi);
8440 /***
8441 * Creates the checkbox imagelist. Helper for LISTVIEW_SetExtendedListViewStyle
8444 static HIMAGELIST LISTVIEW_CreateThemedCheckBoxImageList(const LISTVIEW_INFO *info)
8446 HBITMAP bitmap, old_bitmap;
8447 HIMAGELIST image_list;
8448 HDC hdc, mem_hdc;
8449 HTHEME theme;
8450 RECT rect;
8451 SIZE size;
8453 if (!GetWindowTheme(info->hwndSelf))
8454 return NULL;
8456 theme = OpenThemeDataForDpi(NULL, L"Button", GetDpiForWindow(info->hwndSelf));
8457 if (!theme)
8458 return NULL;
8460 hdc = GetDC(info->hwndSelf);
8461 GetThemePartSize(theme, hdc, BP_CHECKBOX, 0, NULL, TS_DRAW, &size);
8462 SetRect(&rect, 0, 0, size.cx, size.cy);
8463 image_list = ImageList_Create(size.cx, size.cy, ILC_COLOR32, 2, 2);
8464 mem_hdc = CreateCompatibleDC(hdc);
8465 bitmap = CreateCompatibleBitmap(hdc, size.cx, size.cy);
8466 old_bitmap = SelectObject(mem_hdc, bitmap);
8467 ReleaseDC(info->hwndSelf, hdc);
8469 if (IsThemeBackgroundPartiallyTransparent(theme, BP_CHECKBOX, CBS_UNCHECKEDNORMAL))
8470 FillRect(mem_hdc, &rect, (HBRUSH)(COLOR_WINDOW + 1));
8471 DrawThemeBackground(theme, mem_hdc, BP_CHECKBOX, CBS_UNCHECKEDNORMAL, &rect, NULL);
8472 ImageList_Add(image_list, bitmap, NULL);
8474 if (IsThemeBackgroundPartiallyTransparent(theme, BP_CHECKBOX, CBS_CHECKEDNORMAL))
8475 FillRect(mem_hdc, &rect, (HBRUSH)(COLOR_WINDOW + 1));
8476 DrawThemeBackground(theme, mem_hdc, BP_CHECKBOX, CBS_CHECKEDNORMAL, &rect, NULL);
8477 ImageList_Add(image_list, bitmap, NULL);
8479 SelectObject(mem_hdc, old_bitmap);
8480 DeleteObject(bitmap);
8481 DeleteDC(mem_hdc);
8482 CloseThemeData(theme);
8483 return image_list;
8486 static HIMAGELIST LISTVIEW_CreateCheckBoxIL(const LISTVIEW_INFO *infoPtr)
8488 HDC hdc_wnd, hdc;
8489 HBITMAP hbm_im, hbm_mask, hbm_orig;
8490 RECT rc;
8491 HBRUSH hbr_white, hbr_black;
8492 HIMAGELIST himl;
8494 himl = LISTVIEW_CreateThemedCheckBoxImageList(infoPtr);
8495 if (himl)
8496 return himl;
8498 hbr_white = GetStockObject(WHITE_BRUSH);
8499 hbr_black = GetStockObject(BLACK_BRUSH);
8500 himl = ImageList_Create(GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON),
8501 ILC_COLOR | ILC_MASK, 2, 2);
8502 hdc_wnd = GetDC(infoPtr->hwndSelf);
8503 hdc = CreateCompatibleDC(hdc_wnd);
8504 hbm_im = CreateCompatibleBitmap(hdc_wnd, GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON));
8505 hbm_mask = CreateBitmap(GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), 1, 1, NULL);
8506 ReleaseDC(infoPtr->hwndSelf, hdc_wnd);
8508 SetRect(&rc, 0, 0, GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON));
8509 hbm_orig = SelectObject(hdc, hbm_mask);
8510 FillRect(hdc, &rc, hbr_white);
8511 InflateRect(&rc, -2, -2);
8512 FillRect(hdc, &rc, hbr_black);
8514 SelectObject(hdc, hbm_im);
8515 DrawFrameControl(hdc, &rc, DFC_BUTTON, DFCS_BUTTONCHECK | DFCS_MONO);
8516 SelectObject(hdc, hbm_orig);
8517 ImageList_Add(himl, hbm_im, hbm_mask);
8519 SelectObject(hdc, hbm_im);
8520 DrawFrameControl(hdc, &rc, DFC_BUTTON, DFCS_BUTTONCHECK | DFCS_MONO | DFCS_CHECKED);
8521 SelectObject(hdc, hbm_orig);
8522 ImageList_Add(himl, hbm_im, hbm_mask);
8524 DeleteObject(hbm_mask);
8525 DeleteObject(hbm_im);
8526 DeleteDC(hdc);
8528 return himl;
8531 /***
8532 * DESCRIPTION:
8533 * Sets the extended listview style.
8535 * PARAMETERS:
8536 * [I] infoPtr : valid pointer to the listview structure
8537 * [I] dwMask : mask
8538 * [I] dwStyle : style
8540 * RETURN:
8541 * SUCCESS : previous style
8542 * FAILURE : 0
8544 static DWORD LISTVIEW_SetExtendedListViewStyle(LISTVIEW_INFO *infoPtr, DWORD mask, DWORD ex_style)
8546 DWORD old_ex_style = infoPtr->dwLvExStyle;
8548 TRACE("mask %#lx, ex_style %#lx\n", mask, ex_style);
8550 /* set new style */
8551 if (mask)
8552 infoPtr->dwLvExStyle = (old_ex_style & ~mask) | (ex_style & mask);
8553 else
8554 infoPtr->dwLvExStyle = ex_style;
8556 if((infoPtr->dwLvExStyle ^ old_ex_style) & LVS_EX_CHECKBOXES)
8558 HIMAGELIST himl = 0;
8559 if(infoPtr->dwLvExStyle & LVS_EX_CHECKBOXES)
8561 LVITEMW item;
8562 item.mask = LVIF_STATE;
8563 item.stateMask = LVIS_STATEIMAGEMASK;
8564 item.state = INDEXTOSTATEIMAGEMASK(1);
8565 LISTVIEW_SetItemState(infoPtr, -1, &item);
8567 himl = LISTVIEW_CreateCheckBoxIL(infoPtr);
8568 if(!(infoPtr->dwStyle & LVS_SHAREIMAGELISTS))
8569 ImageList_Destroy(infoPtr->himlState);
8571 himl = LISTVIEW_SetImageList(infoPtr, LVSIL_STATE, himl);
8572 /* checkbox list replaces previous custom list or... */
8573 if(((infoPtr->dwLvExStyle & LVS_EX_CHECKBOXES) &&
8574 !(infoPtr->dwStyle & LVS_SHAREIMAGELISTS)) ||
8575 /* ...previous was checkbox list */
8576 (old_ex_style & LVS_EX_CHECKBOXES))
8577 ImageList_Destroy(himl);
8580 if((infoPtr->dwLvExStyle ^ old_ex_style) & LVS_EX_HEADERDRAGDROP)
8582 DWORD style;
8584 /* if not already created */
8585 LISTVIEW_CreateHeader(infoPtr);
8587 style = GetWindowLongW(infoPtr->hwndHeader, GWL_STYLE);
8588 if (infoPtr->dwLvExStyle & LVS_EX_HEADERDRAGDROP)
8589 style |= HDS_DRAGDROP;
8590 else
8591 style &= ~HDS_DRAGDROP;
8592 SetWindowLongW(infoPtr->hwndHeader, GWL_STYLE, style);
8595 /* GRIDLINES adds decoration at top so changes sizes */
8596 if((infoPtr->dwLvExStyle ^ old_ex_style) & LVS_EX_GRIDLINES)
8598 LISTVIEW_CreateHeader(infoPtr);
8599 LISTVIEW_UpdateSize(infoPtr);
8602 if((infoPtr->dwLvExStyle ^ old_ex_style) & LVS_EX_FULLROWSELECT)
8604 LISTVIEW_CreateHeader(infoPtr);
8607 if((infoPtr->dwLvExStyle ^ old_ex_style) & LVS_EX_TRANSPARENTBKGND)
8609 if (infoPtr->dwLvExStyle & LVS_EX_TRANSPARENTBKGND)
8610 LISTVIEW_SetBkColor(infoPtr, CLR_NONE);
8613 if((infoPtr->dwLvExStyle ^ old_ex_style) & LVS_EX_HEADERINALLVIEWS)
8615 if (infoPtr->dwLvExStyle & LVS_EX_HEADERINALLVIEWS)
8616 LISTVIEW_CreateHeader(infoPtr);
8617 else
8618 ShowWindow(infoPtr->hwndHeader, SW_HIDE);
8619 LISTVIEW_UpdateSize(infoPtr);
8620 LISTVIEW_UpdateScroll(infoPtr);
8623 LISTVIEW_InvalidateList(infoPtr);
8624 return old_ex_style;
8627 /***
8628 * DESCRIPTION:
8629 * Sets the new hot cursor used during hot tracking and hover selection.
8631 * PARAMETER(S):
8632 * [I] infoPtr : valid pointer to the listview structure
8633 * [I] hCursor : the new hot cursor handle
8635 * RETURN:
8636 * Returns the previous hot cursor
8638 static HCURSOR LISTVIEW_SetHotCursor(LISTVIEW_INFO *infoPtr, HCURSOR hCursor)
8640 HCURSOR oldCursor = infoPtr->hHotCursor;
8642 infoPtr->hHotCursor = hCursor;
8644 return oldCursor;
8648 /***
8649 * DESCRIPTION:
8650 * Sets the hot item index.
8652 * PARAMETERS:
8653 * [I] infoPtr : valid pointer to the listview structure
8654 * [I] iIndex : index
8656 * RETURN:
8657 * SUCCESS : previous hot item index
8658 * FAILURE : -1 (no hot item)
8660 static INT LISTVIEW_SetHotItem(LISTVIEW_INFO *infoPtr, INT iIndex)
8662 INT iOldIndex = infoPtr->nHotItem;
8664 infoPtr->nHotItem = iIndex;
8666 return iOldIndex;
8670 /***
8671 * DESCRIPTION:
8672 * Sets the amount of time the cursor must hover over an item before it is selected.
8674 * PARAMETER(S):
8675 * [I] infoPtr : valid pointer to the listview structure
8676 * [I] dwHoverTime : hover time, if -1 the hover time is set to the default
8678 * RETURN:
8679 * Returns the previous hover time
8681 static DWORD LISTVIEW_SetHoverTime(LISTVIEW_INFO *infoPtr, DWORD dwHoverTime)
8683 DWORD oldHoverTime = infoPtr->dwHoverTime;
8685 infoPtr->dwHoverTime = dwHoverTime;
8687 return oldHoverTime;
8690 /***
8691 * DESCRIPTION:
8692 * Sets spacing for icons of LVS_ICON style.
8694 * PARAMETER(S):
8695 * [I] infoPtr : valid pointer to the listview structure
8696 * [I] cx : horizontal spacing (-1 = system spacing, 0 = autosize)
8697 * [I] cy : vertical spacing (-1 = system spacing, 0 = autosize)
8699 * RETURN:
8700 * MAKELONG(oldcx, oldcy)
8702 static DWORD LISTVIEW_SetIconSpacing(LISTVIEW_INFO *infoPtr, INT cx, INT cy)
8704 INT iconWidth = 0, iconHeight = 0;
8705 DWORD oldspacing = MAKELONG(infoPtr->iconSpacing.cx, infoPtr->iconSpacing.cy);
8707 TRACE("requested=(%d,%d)\n", cx, cy);
8709 /* set to defaults, if instructed to */
8710 if (cx == -1 && cy == -1)
8712 infoPtr->autoSpacing = TRUE;
8713 if (infoPtr->himlNormal)
8714 ImageList_GetIconSize(infoPtr->himlNormal, &iconWidth, &iconHeight);
8715 cx = GetSystemMetrics(SM_CXICONSPACING) - GetSystemMetrics(SM_CXICON) + iconWidth;
8716 cy = GetSystemMetrics(SM_CYICONSPACING) - GetSystemMetrics(SM_CYICON) + iconHeight;
8718 else
8719 infoPtr->autoSpacing = FALSE;
8721 /* if 0 then keep width */
8722 if (cx != 0)
8723 infoPtr->iconSpacing.cx = cx;
8725 /* if 0 then keep height */
8726 if (cy != 0)
8727 infoPtr->iconSpacing.cy = cy;
8729 TRACE("old=(%d,%d), new=(%ld,%ld), iconSize=(%ld,%ld), ntmH=%d\n",
8730 LOWORD(oldspacing), HIWORD(oldspacing), infoPtr->iconSpacing.cx, infoPtr->iconSpacing.cy,
8731 infoPtr->iconSize.cx, infoPtr->iconSize.cy,
8732 infoPtr->ntmHeight);
8734 /* these depend on the iconSpacing */
8735 LISTVIEW_UpdateItemSize(infoPtr);
8737 return oldspacing;
8740 static inline void set_icon_size(SIZE *size, HIMAGELIST himl, BOOL is_small)
8742 INT cx, cy;
8744 if (himl && ImageList_GetIconSize(himl, &cx, &cy))
8746 size->cx = cx;
8747 size->cy = cy;
8749 else
8751 size->cx = GetSystemMetrics(is_small ? SM_CXSMICON : SM_CXICON);
8752 size->cy = GetSystemMetrics(is_small ? SM_CYSMICON : SM_CYICON);
8756 /***
8757 * DESCRIPTION:
8758 * Sets image lists.
8760 * PARAMETER(S):
8761 * [I] infoPtr : valid pointer to the listview structure
8762 * [I] nType : image list type
8763 * [I] himl : image list handle
8765 * RETURN:
8766 * SUCCESS : old image list
8767 * FAILURE : NULL
8769 static HIMAGELIST LISTVIEW_SetImageList(LISTVIEW_INFO *infoPtr, INT nType, HIMAGELIST himl)
8771 INT oldHeight = infoPtr->nItemHeight;
8772 HIMAGELIST himlOld = 0;
8774 TRACE("(nType=%d, himl=%p)\n", nType, himl);
8776 switch (nType)
8778 case LVSIL_NORMAL:
8779 himlOld = infoPtr->himlNormal;
8780 infoPtr->himlNormal = himl;
8781 if (infoPtr->uView == LV_VIEW_ICON) set_icon_size(&infoPtr->iconSize, himl, FALSE);
8782 if (infoPtr->autoSpacing)
8783 LISTVIEW_SetIconSpacing(infoPtr, -1, -1);
8784 break;
8786 case LVSIL_SMALL:
8787 himlOld = infoPtr->himlSmall;
8788 infoPtr->himlSmall = himl;
8789 if (infoPtr->uView != LV_VIEW_ICON) set_icon_size(&infoPtr->iconSize, himl, TRUE);
8790 if (infoPtr->hwndHeader)
8791 SendMessageW(infoPtr->hwndHeader, HDM_SETIMAGELIST, 0, (LPARAM)himl);
8792 break;
8794 case LVSIL_STATE:
8795 himlOld = infoPtr->himlState;
8796 infoPtr->himlState = himl;
8797 set_icon_size(&infoPtr->iconStateSize, himl, TRUE);
8798 ImageList_SetBkColor(infoPtr->himlState, CLR_NONE);
8799 break;
8801 default:
8802 ERR("Unknown icon type=%d\n", nType);
8803 return NULL;
8806 infoPtr->nItemHeight = LISTVIEW_CalculateItemHeight(infoPtr);
8807 if (infoPtr->nItemHeight != oldHeight)
8808 LISTVIEW_UpdateScroll(infoPtr);
8810 return himlOld;
8813 /***
8814 * DESCRIPTION:
8815 * Preallocates memory (does *not* set the actual count of items !)
8817 * PARAMETER(S):
8818 * [I] infoPtr : valid pointer to the listview structure
8819 * [I] nItems : item count (projected number of items to allocate)
8820 * [I] dwFlags : update flags
8822 * RETURN:
8823 * SUCCESS : TRUE
8824 * FAILURE : FALSE
8826 static BOOL LISTVIEW_SetItemCount(LISTVIEW_INFO *infoPtr, INT nItems, DWORD dwFlags)
8828 TRACE("nItems %d, flags %#lx\n", nItems, dwFlags);
8830 if (infoPtr->dwStyle & LVS_OWNERDATA)
8832 INT nOldCount = infoPtr->nItemCount;
8833 infoPtr->nItemCount = nItems;
8835 if (nItems < nOldCount)
8837 RANGE range = { nItems, nOldCount };
8838 ranges_del(infoPtr->selectionRanges, range);
8839 if (infoPtr->nFocusedItem >= nItems)
8841 LISTVIEW_SetItemFocus(infoPtr, -1);
8842 infoPtr->nFocusedItem = -1;
8843 SetRectEmpty(&infoPtr->rcFocus);
8847 LISTVIEW_UpdateScroll(infoPtr);
8849 /* the flags are valid only in ownerdata report and list modes */
8850 if (infoPtr->uView == LV_VIEW_ICON || infoPtr->uView == LV_VIEW_SMALLICON) dwFlags = 0;
8852 if (!(dwFlags & LVSICF_NOSCROLL) && infoPtr->nFocusedItem != -1)
8853 LISTVIEW_EnsureVisible(infoPtr, infoPtr->nFocusedItem, FALSE);
8855 if (!(dwFlags & LVSICF_NOINVALIDATEALL))
8856 LISTVIEW_InvalidateList(infoPtr);
8857 else
8859 INT nFrom, nTo;
8860 POINT Origin;
8861 RECT rcErase;
8863 LISTVIEW_GetOrigin(infoPtr, &Origin);
8864 nFrom = min(nOldCount, nItems);
8865 nTo = max(nOldCount, nItems);
8867 if (infoPtr->uView == LV_VIEW_DETAILS)
8869 SetRect(&rcErase, 0, nFrom * infoPtr->nItemHeight, infoPtr->nItemWidth,
8870 nTo * infoPtr->nItemHeight);
8871 OffsetRect(&rcErase, Origin.x, Origin.y);
8872 if (IntersectRect(&rcErase, &rcErase, &infoPtr->rcList))
8873 LISTVIEW_InvalidateRect(infoPtr, &rcErase);
8875 else /* LV_VIEW_LIST */
8877 INT nPerCol = LISTVIEW_GetCountPerColumn(infoPtr);
8879 rcErase.left = (nFrom / nPerCol) * infoPtr->nItemWidth;
8880 rcErase.top = (nFrom % nPerCol) * infoPtr->nItemHeight;
8881 rcErase.right = rcErase.left + infoPtr->nItemWidth;
8882 rcErase.bottom = nPerCol * infoPtr->nItemHeight;
8883 OffsetRect(&rcErase, Origin.x, Origin.y);
8884 if (IntersectRect(&rcErase, &rcErase, &infoPtr->rcList))
8885 LISTVIEW_InvalidateRect(infoPtr, &rcErase);
8887 rcErase.left = (nFrom / nPerCol + 1) * infoPtr->nItemWidth;
8888 rcErase.top = 0;
8889 rcErase.right = (nTo / nPerCol + 1) * infoPtr->nItemWidth;
8890 rcErase.bottom = nPerCol * infoPtr->nItemHeight;
8891 OffsetRect(&rcErase, Origin.x, Origin.y);
8892 if (IntersectRect(&rcErase, &rcErase, &infoPtr->rcList))
8893 LISTVIEW_InvalidateRect(infoPtr, &rcErase);
8897 else
8899 /* According to MSDN for non-LVS_OWNERDATA this is just
8900 * a performance issue. The control allocates its internal
8901 * data structures for the number of items specified. It
8902 * cuts down on the number of memory allocations. Therefore
8903 * we will just issue a WARN here
8905 WARN("for non-ownerdata performance option not implemented.\n");
8908 return TRUE;
8911 /***
8912 * DESCRIPTION:
8913 * Sets the position of an item.
8915 * PARAMETER(S):
8916 * [I] infoPtr : valid pointer to the listview structure
8917 * [I] nItem : item index
8918 * [I] pt : coordinate
8920 * RETURN:
8921 * SUCCESS : TRUE
8922 * FAILURE : FALSE
8924 static BOOL LISTVIEW_SetItemPosition(LISTVIEW_INFO *infoPtr, INT nItem, const POINT *pt)
8926 POINT Origin, Pt;
8928 TRACE("(nItem=%d, pt=%s)\n", nItem, wine_dbgstr_point(pt));
8930 if (!pt || nItem < 0 || nItem >= infoPtr->nItemCount ||
8931 !(infoPtr->uView == LV_VIEW_ICON || infoPtr->uView == LV_VIEW_SMALLICON)) return FALSE;
8933 Pt = *pt;
8934 LISTVIEW_GetOrigin(infoPtr, &Origin);
8936 /* This point value seems to be an undocumented feature.
8937 * The best guess is that it means either at the origin,
8938 * or at true beginning of the list. I will assume the origin. */
8939 if ((Pt.x == -1) && (Pt.y == -1))
8940 Pt = Origin;
8942 if (infoPtr->uView == LV_VIEW_ICON)
8944 Pt.x -= (infoPtr->nItemWidth - infoPtr->iconSize.cx) / 2;
8945 Pt.y -= ICON_TOP_PADDING;
8947 Pt.x -= Origin.x;
8948 Pt.y -= Origin.y;
8950 return LISTVIEW_MoveIconTo(infoPtr, nItem, &Pt, FALSE);
8953 /***
8954 * DESCRIPTION:
8955 * Sets the state of one or many items.
8957 * PARAMETER(S):
8958 * [I] infoPtr : valid pointer to the listview structure
8959 * [I] nItem : item index
8960 * [I] item : item or subitem info
8962 * RETURN:
8963 * SUCCESS : TRUE
8964 * FAILURE : FALSE
8966 static BOOL LISTVIEW_SetItemState(LISTVIEW_INFO *infoPtr, INT nItem, const LVITEMW *item)
8968 BOOL ret = TRUE;
8969 LVITEMW lvItem;
8971 if (!item) return FALSE;
8973 lvItem.iItem = nItem;
8974 lvItem.iSubItem = 0;
8975 lvItem.mask = LVIF_STATE;
8976 lvItem.state = item->state;
8977 lvItem.stateMask = item->stateMask;
8978 TRACE("item=%s\n", debuglvitem_t(&lvItem, TRUE));
8980 if (nItem == -1)
8982 UINT oldstate = 0;
8983 DWORD old_mask;
8985 /* special case optimization for recurring attempt to deselect all */
8986 if (lvItem.state == 0 && lvItem.stateMask == LVIS_SELECTED && !LISTVIEW_GetSelectedCount(infoPtr))
8987 return TRUE;
8989 /* select all isn't allowed in LVS_SINGLESEL */
8990 if ((lvItem.state & lvItem.stateMask & LVIS_SELECTED) && (infoPtr->dwStyle & LVS_SINGLESEL))
8991 return FALSE;
8993 /* focus all isn't allowed */
8994 if (lvItem.state & lvItem.stateMask & LVIS_FOCUSED) return FALSE;
8996 old_mask = infoPtr->notify_mask & NOTIFY_MASK_ITEM_CHANGE;
8997 if (infoPtr->dwStyle & LVS_OWNERDATA)
8999 infoPtr->notify_mask &= ~NOTIFY_MASK_ITEM_CHANGE;
9000 if (!(lvItem.state & LVIS_SELECTED) && LISTVIEW_GetSelectedCount(infoPtr))
9001 oldstate |= LVIS_SELECTED;
9002 if (infoPtr->nFocusedItem != -1) oldstate |= LVIS_FOCUSED;
9005 /* apply to all items */
9006 for (lvItem.iItem = 0; lvItem.iItem < infoPtr->nItemCount; lvItem.iItem++)
9007 if (!LISTVIEW_SetItemT(infoPtr, &lvItem, TRUE)) ret = FALSE;
9009 if (infoPtr->dwStyle & LVS_OWNERDATA)
9011 NMLISTVIEW nmlv;
9013 infoPtr->notify_mask |= old_mask;
9015 nmlv.iItem = -1;
9016 nmlv.iSubItem = 0;
9017 nmlv.uNewState = lvItem.state & lvItem.stateMask;
9018 nmlv.uOldState = oldstate & lvItem.stateMask;
9019 nmlv.uChanged = LVIF_STATE;
9020 nmlv.ptAction.x = nmlv.ptAction.y = 0;
9021 nmlv.lParam = 0;
9023 notify_listview(infoPtr, LVN_ITEMCHANGED, &nmlv);
9026 else
9027 ret = LISTVIEW_SetItemT(infoPtr, &lvItem, TRUE);
9029 return ret;
9032 /***
9033 * DESCRIPTION:
9034 * Sets the text of an item or subitem.
9036 * PARAMETER(S):
9037 * [I] hwnd : window handle
9038 * [I] nItem : item index
9039 * [I] lpLVItem : item or subitem info
9040 * [I] isW : TRUE if input is Unicode
9042 * RETURN:
9043 * SUCCESS : TRUE
9044 * FAILURE : FALSE
9046 static BOOL LISTVIEW_SetItemTextT(LISTVIEW_INFO *infoPtr, INT nItem, const LVITEMW *lpLVItem, BOOL isW)
9048 LVITEMW lvItem;
9050 if (!lpLVItem || nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
9051 if (infoPtr->dwStyle & LVS_OWNERDATA) return FALSE;
9053 lvItem.iItem = nItem;
9054 lvItem.iSubItem = lpLVItem->iSubItem;
9055 lvItem.mask = LVIF_TEXT;
9056 lvItem.pszText = lpLVItem->pszText;
9057 lvItem.cchTextMax = lpLVItem->cchTextMax;
9059 TRACE("(nItem=%d, lpLVItem=%s, isW=%d)\n", nItem, debuglvitem_t(&lvItem, isW), isW);
9061 return LISTVIEW_SetItemT(infoPtr, &lvItem, isW);
9064 /***
9065 * DESCRIPTION:
9066 * Set item index that marks the start of a multiple selection.
9068 * PARAMETER(S):
9069 * [I] infoPtr : valid pointer to the listview structure
9070 * [I] nIndex : index
9072 * RETURN:
9073 * Index number or -1 if there is no selection mark.
9075 static INT LISTVIEW_SetSelectionMark(LISTVIEW_INFO *infoPtr, INT nIndex)
9077 INT nOldIndex = infoPtr->nSelectionMark;
9079 TRACE("(nIndex=%d)\n", nIndex);
9081 if (nIndex >= -1 && nIndex < infoPtr->nItemCount)
9082 infoPtr->nSelectionMark = nIndex;
9084 return nOldIndex;
9087 /***
9088 * DESCRIPTION:
9089 * Sets the text background color.
9091 * PARAMETER(S):
9092 * [I] infoPtr : valid pointer to the listview structure
9093 * [I] color : text background color
9095 * RETURN:
9096 * SUCCESS : TRUE
9097 * FAILURE : FALSE
9099 static BOOL LISTVIEW_SetTextBkColor(LISTVIEW_INFO *infoPtr, COLORREF color)
9101 TRACE("color %#lx\n", color);
9103 infoPtr->clrTextBk = color;
9104 return TRUE;
9107 /***
9108 * DESCRIPTION:
9109 * Sets the text foreground color.
9111 * PARAMETER(S):
9112 * [I] infoPtr : valid pointer to the listview structure
9113 * [I] color : text color
9115 * RETURN:
9116 * SUCCESS : TRUE
9117 * FAILURE : FALSE
9119 static BOOL LISTVIEW_SetTextColor (LISTVIEW_INFO *infoPtr, COLORREF color)
9121 TRACE("color %#lx\n", color);
9123 infoPtr->clrText = color;
9124 return TRUE;
9127 /***
9128 * DESCRIPTION:
9129 * Sets new ToolTip window to ListView control.
9131 * PARAMETER(S):
9132 * [I] infoPtr : valid pointer to the listview structure
9133 * [I] hwndNewToolTip : handle to new ToolTip
9135 * RETURN:
9136 * old tool tip
9138 static HWND LISTVIEW_SetToolTips( LISTVIEW_INFO *infoPtr, HWND hwndNewToolTip)
9140 HWND hwndOldToolTip = infoPtr->hwndToolTip;
9141 infoPtr->hwndToolTip = hwndNewToolTip;
9142 return hwndOldToolTip;
9146 * DESCRIPTION:
9147 * sets the Unicode character format flag for the control
9148 * PARAMETER(S):
9149 * [I] infoPtr :valid pointer to the listview structure
9150 * [I] fUnicode :true to switch to UNICODE false to switch to ANSI
9152 * RETURN:
9153 * Old Unicode Format
9155 static BOOL LISTVIEW_SetUnicodeFormat( LISTVIEW_INFO *infoPtr, BOOL unicode)
9157 SHORT rc = infoPtr->notifyFormat;
9158 infoPtr->notifyFormat = (unicode) ? NFR_UNICODE : NFR_ANSI;
9159 return rc == NFR_UNICODE;
9163 * DESCRIPTION:
9164 * sets the control view mode
9165 * PARAMETER(S):
9166 * [I] infoPtr :valid pointer to the listview structure
9167 * [I] nView :new view mode value
9169 * RETURN:
9170 * SUCCESS: 1
9171 * FAILURE: -1
9173 static INT LISTVIEW_SetView(LISTVIEW_INFO *infoPtr, DWORD nView)
9175 HIMAGELIST himl;
9177 if (infoPtr->uView == nView) return 1;
9179 if ((INT)nView < 0 || nView > LV_VIEW_MAX) return -1;
9180 if (nView == LV_VIEW_TILE)
9182 FIXME("View LV_VIEW_TILE unimplemented\n");
9183 return -1;
9186 infoPtr->uView = nView;
9188 SendMessageW(infoPtr->hwndEdit, WM_KILLFOCUS, 0, 0);
9189 ShowWindow(infoPtr->hwndHeader, SW_HIDE);
9191 ShowScrollBar(infoPtr->hwndSelf, SB_BOTH, FALSE);
9192 SetRectEmpty(&infoPtr->rcFocus);
9194 himl = (nView == LV_VIEW_ICON ? infoPtr->himlNormal : infoPtr->himlSmall);
9195 set_icon_size(&infoPtr->iconSize, himl, nView != LV_VIEW_ICON);
9197 switch (nView)
9199 case LV_VIEW_ICON:
9200 case LV_VIEW_SMALLICON:
9201 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
9202 break;
9203 case LV_VIEW_DETAILS:
9205 HDLAYOUT hl;
9206 WINDOWPOS wp;
9208 LISTVIEW_CreateHeader( infoPtr );
9210 hl.prc = &infoPtr->rcList;
9211 hl.pwpos = &wp;
9212 SendMessageW(infoPtr->hwndHeader, HDM_LAYOUT, 0, (LPARAM)&hl);
9213 SetWindowPos(infoPtr->hwndHeader, infoPtr->hwndSelf, wp.x, wp.y, wp.cx, wp.cy,
9214 wp.flags | ((infoPtr->dwStyle & LVS_NOCOLUMNHEADER) ? SWP_HIDEWINDOW : SWP_SHOWWINDOW));
9215 break;
9217 case LV_VIEW_LIST:
9218 break;
9221 LISTVIEW_UpdateItemSize(infoPtr);
9222 LISTVIEW_UpdateSize(infoPtr);
9223 LISTVIEW_UpdateScroll(infoPtr);
9224 LISTVIEW_InvalidateList(infoPtr);
9226 TRACE("nView %ld\n", nView);
9228 return 1;
9231 /* LISTVIEW_SetWorkAreas */
9233 struct sorting_context
9235 HDPA items;
9236 PFNLVCOMPARE compare_func;
9237 LPARAM lParam;
9240 /* DPA_Sort() callback used for LVM_SORTITEMS */
9241 static INT WINAPI LISTVIEW_CallBackCompare(LPVOID first, LPVOID second, LPARAM lParam)
9243 struct sorting_context *context = (struct sorting_context *)lParam;
9244 ITEM_INFO* lv_first = DPA_GetPtr( first, 0 );
9245 ITEM_INFO* lv_second = DPA_GetPtr( second, 0 );
9247 return context->compare_func(lv_first->lParam, lv_second->lParam, context->lParam);
9250 /* DPA_Sort() callback used for LVM_SORTITEMSEX */
9251 static INT WINAPI LISTVIEW_CallBackCompareEx(LPVOID first, LPVOID second, LPARAM lParam)
9253 struct sorting_context *context = (struct sorting_context *)lParam;
9254 INT first_idx = DPA_GetPtrIndex( context->items, first );
9255 INT second_idx = DPA_GetPtrIndex( context->items, second );
9257 return context->compare_func(first_idx, second_idx, context->lParam);
9260 /***
9261 * DESCRIPTION:
9262 * Sorts the listview items.
9264 * PARAMETER(S):
9265 * [I] infoPtr : valid pointer to the listview structure
9266 * [I] pfnCompare : application-defined value
9267 * [I] lParamSort : pointer to comparison callback
9268 * [I] IsEx : TRUE when LVM_SORTITEMSEX used
9270 * RETURN:
9271 * SUCCESS : TRUE
9272 * FAILURE : FALSE
9274 static BOOL LISTVIEW_SortItems(LISTVIEW_INFO *infoPtr, PFNLVCOMPARE pfnCompare,
9275 LPARAM lParamSort, BOOL IsEx)
9277 HDPA hdpaSubItems, hdpaItems;
9278 ITEM_INFO *lpItem;
9279 LPVOID selectionMarkItem = NULL;
9280 LPVOID focusedItem = NULL;
9281 struct sorting_context context;
9282 int i;
9284 TRACE("pfnCompare %p, lParamSort %Ix\n", pfnCompare, lParamSort);
9286 if (infoPtr->dwStyle & LVS_OWNERDATA) return FALSE;
9288 if (!pfnCompare) return FALSE;
9289 if (!infoPtr->hdpaItems) return FALSE;
9291 /* if there are 0 or 1 items, there is no need to sort */
9292 if (infoPtr->nItemCount < 2) return TRUE;
9293 if (!(hdpaItems = DPA_Clone(infoPtr->hdpaItems, NULL))) return FALSE;
9295 /* clear selection */
9296 ranges_clear(infoPtr->selectionRanges);
9298 /* save selection mark and focused item */
9299 if (infoPtr->nSelectionMark >= 0)
9300 selectionMarkItem = DPA_GetPtr(infoPtr->hdpaItems, infoPtr->nSelectionMark);
9301 if (infoPtr->nFocusedItem >= 0)
9302 focusedItem = DPA_GetPtr(infoPtr->hdpaItems, infoPtr->nFocusedItem);
9304 context.items = hdpaItems;
9305 context.compare_func = pfnCompare;
9306 context.lParam = lParamSort;
9307 if (IsEx)
9308 DPA_Sort(hdpaItems, LISTVIEW_CallBackCompareEx, (LPARAM)&context);
9309 else
9310 DPA_Sort(hdpaItems, LISTVIEW_CallBackCompare, (LPARAM)&context);
9311 DPA_Destroy(infoPtr->hdpaItems);
9312 infoPtr->hdpaItems = hdpaItems;
9314 /* restore selection ranges */
9315 for (i=0; i < infoPtr->nItemCount; i++)
9317 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, i);
9318 lpItem = DPA_GetPtr(hdpaSubItems, 0);
9320 if (lpItem->state & LVIS_SELECTED)
9321 ranges_additem(infoPtr->selectionRanges, i);
9323 /* restore selection mark and focused item */
9324 infoPtr->nSelectionMark = DPA_GetPtrIndex(infoPtr->hdpaItems, selectionMarkItem);
9325 infoPtr->nFocusedItem = DPA_GetPtrIndex(infoPtr->hdpaItems, focusedItem);
9327 /* I believe nHotItem should be left alone, see LISTVIEW_ShiftIndices */
9329 /* refresh the display */
9330 LISTVIEW_InvalidateList(infoPtr);
9331 return TRUE;
9334 /***
9335 * DESCRIPTION:
9336 * Update theme handle after a theme change.
9338 * PARAMETER(S):
9339 * [I] infoPtr : valid pointer to the listview structure
9341 * RETURN:
9342 * SUCCESS : 0
9343 * FAILURE : something else
9345 static LRESULT LISTVIEW_ThemeChanged(const LISTVIEW_INFO *infoPtr)
9347 HTHEME theme = GetWindowTheme(infoPtr->hwndSelf);
9348 CloseThemeData(theme);
9349 OpenThemeData(infoPtr->hwndSelf, themeClass);
9350 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
9351 return 0;
9354 /***
9355 * DESCRIPTION:
9356 * Updates an items or rearranges the listview control.
9358 * PARAMETER(S):
9359 * [I] infoPtr : valid pointer to the listview structure
9360 * [I] nItem : item index
9362 * RETURN:
9363 * SUCCESS : TRUE
9364 * FAILURE : FALSE
9366 static BOOL LISTVIEW_Update(LISTVIEW_INFO *infoPtr, INT nItem)
9368 TRACE("(nItem=%d)\n", nItem);
9370 if (nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
9372 /* rearrange with default alignment style */
9373 if (is_autoarrange(infoPtr))
9374 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
9375 else
9376 LISTVIEW_InvalidateItem(infoPtr, nItem);
9378 return TRUE;
9381 /***
9382 * DESCRIPTION:
9383 * Draw the track line at the place defined in the infoPtr structure.
9384 * The line is drawn with a XOR pen so drawing the line for the second time
9385 * in the same place erases the line.
9387 * PARAMETER(S):
9388 * [I] infoPtr : valid pointer to the listview structure
9390 * RETURN:
9391 * SUCCESS : TRUE
9392 * FAILURE : FALSE
9394 static BOOL LISTVIEW_DrawTrackLine(const LISTVIEW_INFO *infoPtr)
9396 HDC hdc;
9398 if (infoPtr->xTrackLine == -1)
9399 return FALSE;
9401 if (!(hdc = GetDC(infoPtr->hwndSelf)))
9402 return FALSE;
9403 PatBlt( hdc, infoPtr->xTrackLine, infoPtr->rcList.top,
9404 1, infoPtr->rcList.bottom - infoPtr->rcList.top, DSTINVERT );
9405 ReleaseDC(infoPtr->hwndSelf, hdc);
9406 return TRUE;
9409 /***
9410 * DESCRIPTION:
9411 * Called when an edit control should be displayed. This function is called after
9412 * we are sure that there was a single click - not a double click (this is a TIMERPROC).
9414 * PARAMETER(S):
9415 * [I] hwnd : Handle to the listview
9416 * [I] uMsg : WM_TIMER (ignored)
9417 * [I] idEvent : The timer ID interpreted as a pointer to a DELAYED_EDIT_ITEM struct
9418 * [I] dwTimer : The elapsed time (ignored)
9420 * RETURN:
9421 * None.
9423 static VOID CALLBACK LISTVIEW_DelayedEditItem(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime)
9425 DELAYED_ITEM_EDIT *editItem = (DELAYED_ITEM_EDIT *)idEvent;
9426 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)GetWindowLongPtrW(hwnd, 0);
9428 KillTimer(hwnd, idEvent);
9429 editItem->fEnabled = FALSE;
9430 /* check if the item is still selected */
9431 if (infoPtr->bFocus && LISTVIEW_GetItemState(infoPtr, editItem->iItem, LVIS_SELECTED))
9432 LISTVIEW_EditLabelT(infoPtr, editItem->iItem, TRUE);
9435 /***
9436 * DESCRIPTION:
9437 * Creates the listview control - the WM_NCCREATE phase.
9439 * PARAMETER(S):
9440 * [I] hwnd : window handle
9441 * [I] lpcs : the create parameters
9443 * RETURN:
9444 * Success: TRUE
9445 * Failure: FALSE
9447 static LRESULT LISTVIEW_NCCreate(HWND hwnd, WPARAM wParam, const CREATESTRUCTW *lpcs)
9449 LISTVIEW_INFO *infoPtr;
9450 LOGFONTW logFont;
9452 TRACE("(lpcs=%p)\n", lpcs);
9454 /* initialize info pointer */
9455 infoPtr = Alloc(sizeof(LISTVIEW_INFO));
9456 if (!infoPtr) return FALSE;
9458 SetWindowLongPtrW(hwnd, 0, (DWORD_PTR)infoPtr);
9460 infoPtr->hwndSelf = hwnd;
9461 infoPtr->dwStyle = lpcs->style; /* Note: may be changed in WM_CREATE */
9462 map_style_view(infoPtr);
9463 /* determine the type of structures to use */
9464 infoPtr->hwndNotify = lpcs->hwndParent;
9465 /* infoPtr->notifyFormat will be filled in WM_CREATE */
9467 /* initialize color information */
9468 infoPtr->clrBk = CLR_NONE;
9469 infoPtr->clrText = CLR_DEFAULT;
9470 infoPtr->clrTextBk = CLR_DEFAULT;
9471 LISTVIEW_SetBkColor(infoPtr, comctl32_color.clrWindow);
9473 /* set default values */
9474 infoPtr->nFocusedItem = -1;
9475 infoPtr->nSelectionMark = -1;
9476 infoPtr->nHotItem = -1;
9477 infoPtr->redraw = TRUE;
9478 infoPtr->bNoItemMetrics = TRUE;
9479 infoPtr->notify_mask = NOTIFY_MASK_UNMASK_ALL;
9480 infoPtr->autoSpacing = TRUE;
9481 infoPtr->iconSpacing.cx = GetSystemMetrics(SM_CXICONSPACING) - GetSystemMetrics(SM_CXICON);
9482 infoPtr->iconSpacing.cy = GetSystemMetrics(SM_CYICONSPACING) - GetSystemMetrics(SM_CYICON);
9483 infoPtr->nEditLabelItem = -1;
9484 infoPtr->nLButtonDownItem = -1;
9485 infoPtr->dwHoverTime = HOVER_DEFAULT; /* default system hover time */
9486 infoPtr->cWheelRemainder = 0;
9487 infoPtr->nMeasureItemHeight = 0;
9488 infoPtr->xTrackLine = -1; /* no track line */
9489 infoPtr->itemEdit.fEnabled = FALSE;
9490 infoPtr->iVersion = COMCTL32_VERSION;
9491 infoPtr->colRectsDirty = FALSE;
9492 infoPtr->selected_column = -1;
9494 /* get default font (icon title) */
9495 SystemParametersInfoW(SPI_GETICONTITLELOGFONT, 0, &logFont, 0);
9496 infoPtr->hDefaultFont = CreateFontIndirectW(&logFont);
9497 infoPtr->hFont = infoPtr->hDefaultFont;
9498 LISTVIEW_SaveTextMetrics(infoPtr);
9500 /* allocate memory for the data structure */
9501 if (!(infoPtr->selectionRanges = ranges_create(10))) goto fail;
9502 if (!(infoPtr->hdpaItems = DPA_Create(10))) goto fail;
9503 if (!(infoPtr->hdpaItemIds = DPA_Create(10))) goto fail;
9504 if (!(infoPtr->hdpaPosX = DPA_Create(10))) goto fail;
9505 if (!(infoPtr->hdpaPosY = DPA_Create(10))) goto fail;
9506 if (!(infoPtr->hdpaColumns = DPA_Create(10))) goto fail;
9508 return DefWindowProcW(hwnd, WM_NCCREATE, wParam, (LPARAM)lpcs);
9510 fail:
9511 DestroyWindow(infoPtr->hwndHeader);
9512 ranges_destroy(infoPtr->selectionRanges);
9513 DPA_Destroy(infoPtr->hdpaItems);
9514 DPA_Destroy(infoPtr->hdpaItemIds);
9515 DPA_Destroy(infoPtr->hdpaPosX);
9516 DPA_Destroy(infoPtr->hdpaPosY);
9517 DPA_Destroy(infoPtr->hdpaColumns);
9518 Free(infoPtr);
9519 return FALSE;
9522 /***
9523 * DESCRIPTION:
9524 * Creates the listview control - the WM_CREATE phase. Most of the data is
9525 * already set up in LISTVIEW_NCCreate
9527 * PARAMETER(S):
9528 * [I] hwnd : window handle
9529 * [I] lpcs : the create parameters
9531 * RETURN:
9532 * Success: 0
9533 * Failure: -1
9535 static LRESULT LISTVIEW_Create(HWND hwnd, const CREATESTRUCTW *lpcs)
9537 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)GetWindowLongPtrW(hwnd, 0);
9539 TRACE("lpcs %p, style %#lx\n", lpcs, lpcs->style);
9541 infoPtr->dwStyle = lpcs->style;
9542 map_style_view(infoPtr);
9544 infoPtr->notifyFormat = SendMessageW(infoPtr->hwndNotify, WM_NOTIFYFORMAT,
9545 (WPARAM)infoPtr->hwndSelf, NF_QUERY);
9546 /* on error defaulting to ANSI notifications */
9547 if (infoPtr->notifyFormat == 0) infoPtr->notifyFormat = NFR_ANSI;
9548 TRACE("notify format=%d\n", infoPtr->notifyFormat);
9550 if ((infoPtr->uView == LV_VIEW_DETAILS) && (lpcs->style & WS_VISIBLE))
9552 if (LISTVIEW_CreateHeader(infoPtr) < 0) return -1;
9554 else
9555 infoPtr->hwndHeader = 0;
9557 /* init item size to avoid division by 0 */
9558 LISTVIEW_UpdateItemSize (infoPtr);
9559 LISTVIEW_UpdateSize (infoPtr);
9561 if (infoPtr->uView == LV_VIEW_DETAILS)
9563 if (!(LVS_NOCOLUMNHEADER & lpcs->style) && (WS_VISIBLE & lpcs->style))
9565 ShowWindow(infoPtr->hwndHeader, SW_SHOWNORMAL);
9567 LISTVIEW_UpdateScroll(infoPtr);
9568 /* send WM_MEASUREITEM notification */
9569 if (infoPtr->dwStyle & LVS_OWNERDRAWFIXED) notify_measureitem(infoPtr);
9572 OpenThemeData(hwnd, themeClass);
9574 /* initialize the icon sizes */
9575 set_icon_size(&infoPtr->iconSize, infoPtr->himlNormal, infoPtr->uView != LV_VIEW_ICON);
9576 set_icon_size(&infoPtr->iconStateSize, infoPtr->himlState, TRUE);
9577 return 0;
9580 /***
9581 * DESCRIPTION:
9582 * Destroys the listview control.
9584 * PARAMETER(S):
9585 * [I] infoPtr : valid pointer to the listview structure
9587 * RETURN:
9588 * Success: 0
9589 * Failure: -1
9591 static LRESULT LISTVIEW_Destroy(LISTVIEW_INFO *infoPtr)
9593 HTHEME theme = GetWindowTheme(infoPtr->hwndSelf);
9594 CloseThemeData(theme);
9596 /* delete all items */
9597 LISTVIEW_DeleteAllItems(infoPtr, TRUE);
9599 return 0;
9602 /***
9603 * DESCRIPTION:
9604 * Enables the listview control.
9606 * PARAMETER(S):
9607 * [I] infoPtr : valid pointer to the listview structure
9608 * [I] bEnable : specifies whether to enable or disable the window
9610 * RETURN:
9611 * SUCCESS : TRUE
9612 * FAILURE : FALSE
9614 static BOOL LISTVIEW_Enable(const LISTVIEW_INFO *infoPtr)
9616 if (infoPtr->dwStyle & LVS_OWNERDRAWFIXED)
9617 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
9618 return TRUE;
9621 /***
9622 * DESCRIPTION:
9623 * Erases the background of the listview control.
9625 * PARAMETER(S):
9626 * [I] infoPtr : valid pointer to the listview structure
9627 * [I] hdc : device context handle
9629 * RETURN:
9630 * SUCCESS : TRUE
9631 * FAILURE : FALSE
9633 static inline BOOL LISTVIEW_EraseBkgnd(const LISTVIEW_INFO *infoPtr, HDC hdc)
9635 RECT rc;
9637 TRACE("(hdc=%p)\n", hdc);
9639 if (!GetClipBox(hdc, &rc)) return FALSE;
9641 if (infoPtr->clrBk == CLR_NONE)
9643 if (infoPtr->dwLvExStyle & LVS_EX_TRANSPARENTBKGND)
9644 return SendMessageW(infoPtr->hwndNotify, WM_PRINTCLIENT,
9645 (WPARAM)hdc, PRF_ERASEBKGND);
9646 else
9647 return SendMessageW(infoPtr->hwndNotify, WM_ERASEBKGND, (WPARAM)hdc, 0);
9650 /* for double buffered controls we need to do this during refresh */
9651 if (infoPtr->dwLvExStyle & LVS_EX_DOUBLEBUFFER) return FALSE;
9653 return LISTVIEW_FillBkgnd(infoPtr, hdc, &rc);
9657 /***
9658 * DESCRIPTION:
9659 * Helper function for LISTVIEW_[HV]Scroll *only*.
9660 * Performs vertical/horizontal scrolling by a give amount.
9662 * PARAMETER(S):
9663 * [I] infoPtr : valid pointer to the listview structure
9664 * [I] dx : amount of horizontal scroll
9665 * [I] dy : amount of vertical scroll
9667 static void scroll_list(LISTVIEW_INFO *infoPtr, INT dx, INT dy)
9669 /* now we can scroll the list */
9670 ScrollWindowEx(infoPtr->hwndSelf, dx, dy, &infoPtr->rcList,
9671 &infoPtr->rcList, 0, 0, SW_ERASE | SW_INVALIDATE);
9672 /* if we have focus, adjust rect */
9673 OffsetRect(&infoPtr->rcFocus, dx, dy);
9674 UpdateWindow(infoPtr->hwndSelf);
9677 /***
9678 * DESCRIPTION:
9679 * Performs vertical scrolling.
9681 * PARAMETER(S):
9682 * [I] infoPtr : valid pointer to the listview structure
9683 * [I] nScrollCode : scroll code
9684 * [I] nScrollDiff : units to scroll in SB_INTERNAL mode, 0 otherwise
9685 * [I] hScrollWnd : scrollbar control window handle
9687 * RETURN:
9688 * Zero
9690 * NOTES:
9691 * SB_LINEUP/SB_LINEDOWN:
9692 * for LVS_ICON, LVS_SMALLICON is 37 by experiment
9693 * for LVS_REPORT is 1 line
9694 * for LVS_LIST cannot occur
9697 static LRESULT LISTVIEW_VScroll(LISTVIEW_INFO *infoPtr, INT nScrollCode,
9698 INT nScrollDiff)
9700 INT nOldScrollPos, nNewScrollPos;
9701 SCROLLINFO scrollInfo;
9702 BOOL is_an_icon;
9704 TRACE("(nScrollCode=%d(%s), nScrollDiff=%d)\n", nScrollCode,
9705 debugscrollcode(nScrollCode), nScrollDiff);
9707 if (infoPtr->hwndEdit) SendMessageW(infoPtr->hwndEdit, WM_KILLFOCUS, 0, 0);
9709 scrollInfo.cbSize = sizeof(SCROLLINFO);
9710 scrollInfo.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_TRACKPOS;
9712 is_an_icon = ((infoPtr->uView == LV_VIEW_ICON) || (infoPtr->uView == LV_VIEW_SMALLICON));
9714 if (!GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo)) return 1;
9716 nOldScrollPos = scrollInfo.nPos;
9717 switch (nScrollCode)
9719 case SB_INTERNAL:
9720 break;
9722 case SB_LINEUP:
9723 nScrollDiff = (is_an_icon) ? -LISTVIEW_SCROLL_ICON_LINE_SIZE : -1;
9724 break;
9726 case SB_LINEDOWN:
9727 nScrollDiff = (is_an_icon) ? LISTVIEW_SCROLL_ICON_LINE_SIZE : 1;
9728 break;
9730 case SB_PAGEUP:
9731 nScrollDiff = -scrollInfo.nPage;
9732 break;
9734 case SB_PAGEDOWN:
9735 nScrollDiff = scrollInfo.nPage;
9736 break;
9738 case SB_THUMBPOSITION:
9739 case SB_THUMBTRACK:
9740 nScrollDiff = scrollInfo.nTrackPos - scrollInfo.nPos;
9741 break;
9743 default:
9744 nScrollDiff = 0;
9747 /* quit right away if pos isn't changing */
9748 if (nScrollDiff == 0) return 0;
9750 /* calculate new position, and handle overflows */
9751 nNewScrollPos = scrollInfo.nPos + nScrollDiff;
9752 if (nScrollDiff > 0) {
9753 if (nNewScrollPos < nOldScrollPos ||
9754 nNewScrollPos > scrollInfo.nMax)
9755 nNewScrollPos = scrollInfo.nMax;
9756 } else {
9757 if (nNewScrollPos > nOldScrollPos ||
9758 nNewScrollPos < scrollInfo.nMin)
9759 nNewScrollPos = scrollInfo.nMin;
9762 /* set the new position, and reread in case it changed */
9763 scrollInfo.fMask = SIF_POS;
9764 scrollInfo.nPos = nNewScrollPos;
9765 nNewScrollPos = SetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo, TRUE);
9767 /* carry on only if it really changed */
9768 if (nNewScrollPos == nOldScrollPos) return 0;
9770 /* now adjust to client coordinates */
9771 nScrollDiff = nOldScrollPos - nNewScrollPos;
9772 if (infoPtr->uView == LV_VIEW_DETAILS) nScrollDiff *= infoPtr->nItemHeight;
9774 /* and scroll the window */
9775 scroll_list(infoPtr, 0, nScrollDiff);
9777 return 0;
9780 /***
9781 * DESCRIPTION:
9782 * Performs horizontal scrolling.
9784 * PARAMETER(S):
9785 * [I] infoPtr : valid pointer to the listview structure
9786 * [I] nScrollCode : scroll code
9787 * [I] nScrollDiff : units to scroll in SB_INTERNAL mode, 0 otherwise
9788 * [I] hScrollWnd : scrollbar control window handle
9790 * RETURN:
9791 * Zero
9793 * NOTES:
9794 * SB_LINELEFT/SB_LINERIGHT:
9795 * for LVS_ICON, LVS_SMALLICON 1 pixel
9796 * for LVS_REPORT is 1 pixel
9797 * for LVS_LIST is 1 column --> which is a 1 because the
9798 * scroll is based on columns not pixels
9801 static LRESULT LISTVIEW_HScroll(LISTVIEW_INFO *infoPtr, INT nScrollCode,
9802 INT nScrollDiff)
9804 INT nOldScrollPos, nNewScrollPos;
9805 SCROLLINFO scrollInfo;
9806 BOOL is_an_icon;
9808 TRACE("(nScrollCode=%d(%s), nScrollDiff=%d)\n", nScrollCode,
9809 debugscrollcode(nScrollCode), nScrollDiff);
9811 if (infoPtr->hwndEdit) SendMessageW(infoPtr->hwndEdit, WM_KILLFOCUS, 0, 0);
9813 scrollInfo.cbSize = sizeof(SCROLLINFO);
9814 scrollInfo.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_TRACKPOS;
9816 is_an_icon = ((infoPtr->uView == LV_VIEW_ICON) || (infoPtr->uView == LV_VIEW_SMALLICON));
9818 if (!GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo)) return 1;
9820 nOldScrollPos = scrollInfo.nPos;
9822 switch (nScrollCode)
9824 case SB_INTERNAL:
9825 break;
9827 case SB_LINELEFT:
9828 nScrollDiff = (is_an_icon) ? -LISTVIEW_SCROLL_ICON_LINE_SIZE : -1;
9829 break;
9831 case SB_LINERIGHT:
9832 nScrollDiff = (is_an_icon) ? LISTVIEW_SCROLL_ICON_LINE_SIZE : 1;
9833 break;
9835 case SB_PAGELEFT:
9836 nScrollDiff = -scrollInfo.nPage;
9837 break;
9839 case SB_PAGERIGHT:
9840 nScrollDiff = scrollInfo.nPage;
9841 break;
9843 case SB_THUMBPOSITION:
9844 case SB_THUMBTRACK:
9845 nScrollDiff = scrollInfo.nTrackPos - scrollInfo.nPos;
9846 break;
9848 default:
9849 nScrollDiff = 0;
9852 /* quit right away if pos isn't changing */
9853 if (nScrollDiff == 0) return 0;
9855 /* calculate new position, and handle overflows */
9856 nNewScrollPos = scrollInfo.nPos + nScrollDiff;
9857 if (nScrollDiff > 0) {
9858 if (nNewScrollPos < nOldScrollPos ||
9859 nNewScrollPos > scrollInfo.nMax)
9860 nNewScrollPos = scrollInfo.nMax;
9861 } else {
9862 if (nNewScrollPos > nOldScrollPos ||
9863 nNewScrollPos < scrollInfo.nMin)
9864 nNewScrollPos = scrollInfo.nMin;
9867 /* set the new position, and reread in case it changed */
9868 scrollInfo.fMask = SIF_POS;
9869 scrollInfo.nPos = nNewScrollPos;
9870 nNewScrollPos = SetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo, TRUE);
9872 /* carry on only if it really changed */
9873 if (nNewScrollPos == nOldScrollPos) return 0;
9875 LISTVIEW_UpdateHeaderSize(infoPtr, nNewScrollPos);
9877 /* now adjust to client coordinates */
9878 nScrollDiff = nOldScrollPos - nNewScrollPos;
9879 if (infoPtr->uView == LV_VIEW_LIST) nScrollDiff *= infoPtr->nItemWidth;
9881 /* and scroll the window */
9882 scroll_list(infoPtr, nScrollDiff, 0);
9884 return 0;
9887 static LRESULT LISTVIEW_MouseWheel(LISTVIEW_INFO *infoPtr, INT wheelDelta)
9889 INT pulScrollLines = 3;
9891 TRACE("(wheelDelta=%d)\n", wheelDelta);
9893 switch(infoPtr->uView)
9895 case LV_VIEW_ICON:
9896 case LV_VIEW_SMALLICON:
9898 * listview should be scrolled by a multiple of 37 dependently on its dimension or its visible item number
9899 * should be fixed in the future.
9901 LISTVIEW_VScroll(infoPtr, SB_INTERNAL, (wheelDelta > 0) ?
9902 -LISTVIEW_SCROLL_ICON_LINE_SIZE : LISTVIEW_SCROLL_ICON_LINE_SIZE);
9903 break;
9905 case LV_VIEW_DETAILS:
9906 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES,0, &pulScrollLines, 0);
9908 /* if scrolling changes direction, ignore left overs */
9909 if ((wheelDelta < 0 && infoPtr->cWheelRemainder < 0) ||
9910 (wheelDelta > 0 && infoPtr->cWheelRemainder > 0))
9911 infoPtr->cWheelRemainder += wheelDelta;
9912 else
9913 infoPtr->cWheelRemainder = wheelDelta;
9914 if (infoPtr->cWheelRemainder && pulScrollLines)
9916 int cLineScroll;
9917 pulScrollLines = min((UINT)LISTVIEW_GetCountPerColumn(infoPtr), pulScrollLines);
9918 cLineScroll = pulScrollLines * infoPtr->cWheelRemainder / WHEEL_DELTA;
9919 infoPtr->cWheelRemainder -= WHEEL_DELTA * cLineScroll / pulScrollLines;
9920 LISTVIEW_VScroll(infoPtr, SB_INTERNAL, -cLineScroll);
9922 break;
9924 case LV_VIEW_LIST:
9925 LISTVIEW_HScroll(infoPtr, (wheelDelta > 0) ? SB_LINELEFT : SB_LINERIGHT, 0);
9926 break;
9928 return 0;
9931 /***
9932 * DESCRIPTION:
9933 * ???
9935 * PARAMETER(S):
9936 * [I] infoPtr : valid pointer to the listview structure
9937 * [I] nVirtualKey : virtual key
9938 * [I] lKeyData : key data
9940 * RETURN:
9941 * Zero
9943 static LRESULT LISTVIEW_KeyDown(LISTVIEW_INFO *infoPtr, INT nVirtualKey, LONG lKeyData)
9945 HWND hwndSelf = infoPtr->hwndSelf;
9946 INT nItem = -1;
9947 NMLVKEYDOWN nmKeyDown;
9949 TRACE("(nVirtualKey=%d, lKeyData=%ld)\n", nVirtualKey, lKeyData);
9951 /* send LVN_KEYDOWN notification */
9952 nmKeyDown.wVKey = nVirtualKey;
9953 nmKeyDown.flags = 0;
9954 notify_hdr(infoPtr, LVN_KEYDOWN, &nmKeyDown.hdr);
9955 if (!IsWindow(hwndSelf))
9956 return 0;
9958 switch (nVirtualKey)
9960 case VK_SPACE:
9961 nItem = infoPtr->nFocusedItem;
9962 if (infoPtr->dwLvExStyle & LVS_EX_CHECKBOXES)
9963 toggle_checkbox_state(infoPtr, infoPtr->nFocusedItem);
9964 break;
9966 case VK_RETURN:
9967 if ((infoPtr->nItemCount > 0) && (infoPtr->nFocusedItem != -1))
9969 if (!notify(infoPtr, NM_RETURN)) return 0;
9970 if (!notify(infoPtr, LVN_ITEMACTIVATE)) return 0;
9972 break;
9974 case VK_HOME:
9975 if (infoPtr->nItemCount > 0)
9976 nItem = 0;
9977 break;
9979 case VK_END:
9980 if (infoPtr->nItemCount > 0)
9981 nItem = infoPtr->nItemCount - 1;
9982 break;
9984 case VK_LEFT:
9985 nItem = LISTVIEW_GetNextItem(infoPtr, infoPtr->nFocusedItem, LVNI_TOLEFT);
9986 break;
9988 case VK_UP:
9989 nItem = LISTVIEW_GetNextItem(infoPtr, infoPtr->nFocusedItem, LVNI_ABOVE);
9990 break;
9992 case VK_RIGHT:
9993 nItem = LISTVIEW_GetNextItem(infoPtr, infoPtr->nFocusedItem, LVNI_TORIGHT);
9994 break;
9996 case VK_DOWN:
9997 nItem = LISTVIEW_GetNextItem(infoPtr, infoPtr->nFocusedItem, LVNI_BELOW);
9998 break;
10000 case VK_PRIOR:
10001 if (infoPtr->uView == LV_VIEW_DETAILS)
10003 INT topidx = LISTVIEW_GetTopIndex(infoPtr);
10004 if (infoPtr->nFocusedItem == topidx)
10005 nItem = topidx - LISTVIEW_GetCountPerColumn(infoPtr) + 1;
10006 else
10007 nItem = topidx;
10009 else
10010 nItem = infoPtr->nFocusedItem - LISTVIEW_GetCountPerColumn(infoPtr)
10011 * LISTVIEW_GetCountPerRow(infoPtr);
10012 if(nItem < 0) nItem = 0;
10013 break;
10015 case VK_NEXT:
10016 if (infoPtr->uView == LV_VIEW_DETAILS)
10018 INT topidx = LISTVIEW_GetTopIndex(infoPtr);
10019 INT cnt = LISTVIEW_GetCountPerColumn(infoPtr);
10020 if (infoPtr->nFocusedItem == topidx + cnt - 1)
10021 nItem = infoPtr->nFocusedItem + cnt - 1;
10022 else
10023 nItem = topidx + cnt - 1;
10025 else
10026 nItem = infoPtr->nFocusedItem + LISTVIEW_GetCountPerColumn(infoPtr)
10027 * LISTVIEW_GetCountPerRow(infoPtr);
10028 if(nItem >= infoPtr->nItemCount) nItem = infoPtr->nItemCount - 1;
10029 break;
10032 if ((nItem != -1) && (nItem != infoPtr->nFocusedItem || nVirtualKey == VK_SPACE))
10033 LISTVIEW_KeySelection(infoPtr, nItem, nVirtualKey == VK_SPACE);
10035 return 0;
10038 /***
10039 * DESCRIPTION:
10040 * Kills the focus.
10042 * PARAMETER(S):
10043 * [I] infoPtr : valid pointer to the listview structure
10045 * RETURN:
10046 * Zero
10048 static LRESULT LISTVIEW_KillFocus(LISTVIEW_INFO *infoPtr)
10050 TRACE("()\n");
10052 /* drop any left over scroll amount */
10053 infoPtr->cWheelRemainder = 0;
10055 /* if we did not have the focus, there's nothing more to do */
10056 if (!infoPtr->bFocus) return 0;
10058 /* send NM_KILLFOCUS notification */
10059 if (!notify(infoPtr, NM_KILLFOCUS)) return 0;
10061 /* if we have a focus rectangle, get rid of it */
10062 LISTVIEW_ShowFocusRect(infoPtr, FALSE);
10064 /* if have a marquee selection, stop it */
10065 if (infoPtr->bMarqueeSelect)
10067 /* Remove the marquee rectangle and release our mouse capture */
10068 LISTVIEW_InvalidateRect(infoPtr, &infoPtr->marqueeRect);
10069 ReleaseCapture();
10071 SetRectEmpty(&infoPtr->marqueeRect);
10073 infoPtr->bMarqueeSelect = FALSE;
10074 infoPtr->bScrolling = FALSE;
10075 KillTimer(infoPtr->hwndSelf, (UINT_PTR) infoPtr);
10078 /* set window focus flag */
10079 infoPtr->bFocus = FALSE;
10081 /* invalidate the selected items before resetting focus flag */
10082 LISTVIEW_InvalidateSelectedItems(infoPtr);
10084 return 0;
10087 /***
10088 * DESCRIPTION:
10089 * Processes double click messages (left mouse button).
10091 * PARAMETER(S):
10092 * [I] infoPtr : valid pointer to the listview structure
10093 * [I] wKey : key flag
10094 * [I] x,y : mouse coordinate
10096 * RETURN:
10097 * Zero
10099 static LRESULT LISTVIEW_LButtonDblClk(LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
10101 LVHITTESTINFO htInfo;
10103 TRACE("(key=%hu, X=%u, Y=%u)\n", wKey, x, y);
10105 /* Cancel the item edition if any */
10106 if (infoPtr->itemEdit.fEnabled)
10108 KillTimer(infoPtr->hwndSelf, (UINT_PTR)&infoPtr->itemEdit);
10109 infoPtr->itemEdit.fEnabled = FALSE;
10112 /* send NM_RELEASEDCAPTURE notification */
10113 if (!notify(infoPtr, NM_RELEASEDCAPTURE)) return 0;
10115 htInfo.pt.x = x;
10116 htInfo.pt.y = y;
10118 /* send NM_DBLCLK notification */
10119 LISTVIEW_HitTest(infoPtr, &htInfo, TRUE, FALSE);
10120 if (!notify_click(infoPtr, NM_DBLCLK, &htInfo)) return 0;
10122 /* To send the LVN_ITEMACTIVATE, it must be on an Item */
10123 if(htInfo.iItem != -1) notify_itemactivate(infoPtr,&htInfo);
10125 return 0;
10128 static LRESULT LISTVIEW_TrackMouse(const LISTVIEW_INFO *infoPtr, POINT pt)
10130 MSG msg;
10131 RECT r;
10133 r.top = r.bottom = pt.y;
10134 r.left = r.right = pt.x;
10136 InflateRect(&r, GetSystemMetrics(SM_CXDRAG), GetSystemMetrics(SM_CYDRAG));
10138 SetCapture(infoPtr->hwndSelf);
10140 while (1)
10142 if (PeekMessageW(&msg, 0, 0, 0, PM_REMOVE | PM_NOYIELD))
10144 if (msg.message == WM_MOUSEMOVE)
10146 pt.x = (short)LOWORD(msg.lParam);
10147 pt.y = (short)HIWORD(msg.lParam);
10148 if (PtInRect(&r, pt))
10149 continue;
10150 else
10152 ReleaseCapture();
10153 return 1;
10156 else if (msg.message >= WM_LBUTTONDOWN &&
10157 msg.message <= WM_RBUTTONDBLCLK)
10159 break;
10162 DispatchMessageW(&msg);
10165 if (GetCapture() != infoPtr->hwndSelf)
10166 return 0;
10169 ReleaseCapture();
10170 return 0;
10174 /***
10175 * DESCRIPTION:
10176 * Processes mouse down messages (left mouse button).
10178 * PARAMETERS:
10179 * infoPtr [I ] valid pointer to the listview structure
10180 * wKey [I ] key flag
10181 * x,y [I ] mouse coordinate
10183 * RETURN:
10184 * Zero
10186 static LRESULT LISTVIEW_LButtonDown(LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
10188 LVHITTESTINFO lvHitTestInfo;
10189 static BOOL bGroupSelect = TRUE;
10190 POINT pt = { x, y };
10191 INT nItem;
10193 TRACE("(key=%hu, X=%u, Y=%u)\n", wKey, x, y);
10195 /* send NM_RELEASEDCAPTURE notification */
10196 if (!notify(infoPtr, NM_RELEASEDCAPTURE)) return 0;
10198 /* set left button down flag and record the click position */
10199 infoPtr->bLButtonDown = TRUE;
10200 infoPtr->ptClickPos = pt;
10201 infoPtr->bDragging = FALSE;
10202 infoPtr->bMarqueeSelect = FALSE;
10203 infoPtr->bScrolling = FALSE;
10205 lvHitTestInfo.pt.x = x;
10206 lvHitTestInfo.pt.y = y;
10208 nItem = LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, TRUE);
10209 TRACE("at %s, nItem=%d\n", wine_dbgstr_point(&pt), nItem);
10210 if ((nItem >= 0) && (nItem < infoPtr->nItemCount))
10212 if ((infoPtr->dwLvExStyle & LVS_EX_CHECKBOXES) && (lvHitTestInfo.flags & LVHT_ONITEMSTATEICON))
10214 notify_click(infoPtr, NM_CLICK, &lvHitTestInfo);
10215 toggle_checkbox_state(infoPtr, nItem);
10216 infoPtr->bLButtonDown = FALSE;
10217 return 0;
10220 if (infoPtr->dwStyle & LVS_SINGLESEL)
10222 if (LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED))
10223 infoPtr->nEditLabelItem = nItem;
10224 else
10225 LISTVIEW_SetSelection(infoPtr, nItem);
10227 else
10229 if ((wKey & MK_CONTROL) && (wKey & MK_SHIFT))
10231 if (bGroupSelect)
10233 if (!LISTVIEW_AddGroupSelection(infoPtr, nItem)) return 0;
10234 LISTVIEW_SetItemFocus(infoPtr, nItem);
10235 infoPtr->nSelectionMark = nItem;
10237 else
10239 LVITEMW item;
10241 item.state = LVIS_SELECTED | LVIS_FOCUSED;
10242 item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
10244 LISTVIEW_SetItemState(infoPtr,nItem,&item);
10245 infoPtr->nSelectionMark = nItem;
10248 else if (wKey & MK_CONTROL)
10250 LVITEMW item;
10252 bGroupSelect = (LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED) == 0);
10254 item.state = (bGroupSelect ? LVIS_SELECTED : 0) | LVIS_FOCUSED;
10255 item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
10256 LISTVIEW_SetItemState(infoPtr, nItem, &item);
10257 infoPtr->nSelectionMark = nItem;
10259 else if (wKey & MK_SHIFT)
10261 LISTVIEW_SetGroupSelection(infoPtr, nItem);
10263 else
10265 if (LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED))
10267 infoPtr->nEditLabelItem = nItem;
10268 infoPtr->nLButtonDownItem = nItem;
10270 LISTVIEW_SetItemFocus(infoPtr, nItem);
10272 else
10273 /* set selection (clears other pre-existing selections) */
10274 LISTVIEW_SetSelection(infoPtr, nItem);
10278 if (!infoPtr->bFocus)
10279 SetFocus(infoPtr->hwndSelf);
10281 if (infoPtr->dwLvExStyle & LVS_EX_ONECLICKACTIVATE)
10282 if(lvHitTestInfo.iItem != -1) notify_itemactivate(infoPtr,&lvHitTestInfo);
10284 else
10286 if (!infoPtr->bFocus)
10287 SetFocus(infoPtr->hwndSelf);
10289 /* remove all selections */
10290 if (!(wKey & MK_CONTROL) && !(wKey & MK_SHIFT))
10291 LISTVIEW_DeselectAll(infoPtr);
10292 ReleaseCapture();
10295 return 0;
10298 /***
10299 * DESCRIPTION:
10300 * Processes mouse up messages (left mouse button).
10302 * PARAMETERS:
10303 * infoPtr [I ] valid pointer to the listview structure
10304 * wKey [I ] key flag
10305 * x,y [I ] mouse coordinate
10307 * RETURN:
10308 * Zero
10310 static LRESULT LISTVIEW_LButtonUp(LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
10312 LVHITTESTINFO lvHitTestInfo;
10314 TRACE("(key=%hu, X=%u, Y=%u)\n", wKey, x, y);
10316 if (!infoPtr->bLButtonDown) return 0;
10318 lvHitTestInfo.pt.x = x;
10319 lvHitTestInfo.pt.y = y;
10321 /* send NM_CLICK notification */
10322 LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, FALSE);
10323 if (!notify_click(infoPtr, NM_CLICK, &lvHitTestInfo)) return 0;
10325 /* set left button flag */
10326 infoPtr->bLButtonDown = FALSE;
10328 /* set a single selection, reset others */
10329 if(lvHitTestInfo.iItem == infoPtr->nLButtonDownItem && lvHitTestInfo.iItem != -1)
10330 LISTVIEW_SetSelection(infoPtr, infoPtr->nLButtonDownItem);
10331 infoPtr->nLButtonDownItem = -1;
10333 if (infoPtr->bDragging || infoPtr->bMarqueeSelect)
10335 /* Remove the marquee rectangle and release our mouse capture */
10336 if (infoPtr->bMarqueeSelect)
10338 LISTVIEW_InvalidateRect(infoPtr, &infoPtr->marqueeDrawRect);
10339 ReleaseCapture();
10342 SetRectEmpty(&infoPtr->marqueeRect);
10343 SetRectEmpty(&infoPtr->marqueeDrawRect);
10345 infoPtr->bDragging = FALSE;
10346 infoPtr->bMarqueeSelect = FALSE;
10347 infoPtr->bScrolling = FALSE;
10349 KillTimer(infoPtr->hwndSelf, (UINT_PTR) infoPtr);
10350 return 0;
10353 /* if we clicked on a selected item, edit the label */
10354 if(lvHitTestInfo.iItem == infoPtr->nEditLabelItem && (lvHitTestInfo.flags & LVHT_ONITEMLABEL))
10356 /* we want to make sure the user doesn't want to do a double click. So we will
10357 * delay the edit. WM_LBUTTONDBLCLICK will cancel the timer
10359 infoPtr->itemEdit.fEnabled = TRUE;
10360 infoPtr->itemEdit.iItem = lvHitTestInfo.iItem;
10361 SetTimer(infoPtr->hwndSelf,
10362 (UINT_PTR)&infoPtr->itemEdit,
10363 GetDoubleClickTime(),
10364 LISTVIEW_DelayedEditItem);
10367 return 0;
10370 /***
10371 * DESCRIPTION:
10372 * Destroys the listview control (called after WM_DESTROY).
10374 * PARAMETER(S):
10375 * [I] infoPtr : valid pointer to the listview structure
10377 * RETURN:
10378 * Zero
10380 static LRESULT LISTVIEW_NCDestroy(LISTVIEW_INFO *infoPtr)
10382 INT i;
10384 TRACE("()\n");
10386 /* destroy data structure */
10387 DPA_Destroy(infoPtr->hdpaItems);
10388 DPA_Destroy(infoPtr->hdpaItemIds);
10389 DPA_Destroy(infoPtr->hdpaPosX);
10390 DPA_Destroy(infoPtr->hdpaPosY);
10391 /* columns */
10392 for (i = 0; i < DPA_GetPtrCount(infoPtr->hdpaColumns); i++)
10393 Free(DPA_GetPtr(infoPtr->hdpaColumns, i));
10394 DPA_Destroy(infoPtr->hdpaColumns);
10395 ranges_destroy(infoPtr->selectionRanges);
10397 /* destroy image lists */
10398 if (!(infoPtr->dwStyle & LVS_SHAREIMAGELISTS))
10400 ImageList_Destroy(infoPtr->himlNormal);
10401 ImageList_Destroy(infoPtr->himlSmall);
10402 ImageList_Destroy(infoPtr->himlState);
10405 /* destroy font, bkgnd brush */
10406 infoPtr->hFont = 0;
10407 if (infoPtr->hDefaultFont) DeleteObject(infoPtr->hDefaultFont);
10408 if (infoPtr->clrBk != CLR_NONE) DeleteObject(infoPtr->hBkBrush);
10410 SetWindowLongPtrW(infoPtr->hwndSelf, 0, 0);
10412 /* free listview info pointer*/
10413 Free(infoPtr);
10415 return 0;
10418 /***
10419 * DESCRIPTION:
10420 * Handles notifications.
10422 * PARAMETER(S):
10423 * [I] infoPtr : valid pointer to the listview structure
10424 * [I] lpnmhdr : notification information
10426 * RETURN:
10427 * Zero
10429 static LRESULT LISTVIEW_Notify(LISTVIEW_INFO *infoPtr, NMHDR *lpnmhdr)
10431 NMHEADERW *lpnmh;
10433 TRACE("(lpnmhdr=%p)\n", lpnmhdr);
10435 if (!lpnmhdr || lpnmhdr->hwndFrom != infoPtr->hwndHeader) return 0;
10437 /* remember: HDN_LAST < HDN_FIRST */
10438 if (lpnmhdr->code > HDN_FIRST || lpnmhdr->code < HDN_LAST) return 0;
10439 lpnmh = (NMHEADERW *)lpnmhdr;
10441 if (lpnmh->iItem < 0 || lpnmh->iItem >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return 0;
10443 switch (lpnmhdr->code)
10445 case HDN_TRACKW:
10446 case HDN_TRACKA:
10448 COLUMN_INFO *lpColumnInfo;
10449 POINT ptOrigin;
10450 INT x;
10452 if (!lpnmh->pitem || !(lpnmh->pitem->mask & HDI_WIDTH))
10453 break;
10455 /* remove the old line (if any) */
10456 LISTVIEW_DrawTrackLine(infoPtr);
10458 /* compute & draw the new line */
10459 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, lpnmh->iItem);
10460 x = lpColumnInfo->rcHeader.left + lpnmh->pitem->cxy;
10461 LISTVIEW_GetOrigin(infoPtr, &ptOrigin);
10462 infoPtr->xTrackLine = x + ptOrigin.x;
10463 LISTVIEW_DrawTrackLine(infoPtr);
10464 return notify_forward_header(infoPtr, lpnmh);
10467 case HDN_ENDTRACKA:
10468 case HDN_ENDTRACKW:
10469 /* remove the track line (if any) */
10470 LISTVIEW_DrawTrackLine(infoPtr);
10471 infoPtr->xTrackLine = -1;
10472 return notify_forward_header(infoPtr, lpnmh);
10474 case HDN_BEGINDRAG:
10475 if ((infoPtr->dwLvExStyle & LVS_EX_HEADERDRAGDROP) == 0) return 1;
10476 return notify_forward_header(infoPtr, lpnmh);
10478 case HDN_ENDDRAG:
10479 infoPtr->colRectsDirty = TRUE;
10480 LISTVIEW_InvalidateList(infoPtr);
10481 return notify_forward_header(infoPtr, lpnmh);
10483 case HDN_ITEMCHANGEDW:
10484 case HDN_ITEMCHANGEDA:
10486 COLUMN_INFO *lpColumnInfo;
10487 HDITEMW hdi;
10488 INT dx, cxy;
10490 if (!lpnmh->pitem || !(lpnmh->pitem->mask & HDI_WIDTH))
10492 hdi.mask = HDI_WIDTH;
10493 if (!SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, lpnmh->iItem, (LPARAM)&hdi)) return 0;
10494 cxy = hdi.cxy;
10496 else
10497 cxy = lpnmh->pitem->cxy;
10499 /* determine how much we change since the last know position */
10500 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, lpnmh->iItem);
10501 dx = cxy - (lpColumnInfo->rcHeader.right - lpColumnInfo->rcHeader.left);
10502 if (dx != 0)
10504 lpColumnInfo->rcHeader.right += dx;
10506 hdi.mask = HDI_ORDER;
10507 SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, lpnmh->iItem, (LPARAM)&hdi);
10509 /* not the rightmost one */
10510 if (hdi.iOrder + 1 < DPA_GetPtrCount(infoPtr->hdpaColumns))
10512 INT nIndex = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX,
10513 hdi.iOrder + 1, 0);
10514 LISTVIEW_ScrollColumns(infoPtr, nIndex, dx);
10516 else
10518 /* only needs to update the scrolls */
10519 infoPtr->nItemWidth += dx;
10520 LISTVIEW_UpdateScroll(infoPtr);
10522 LISTVIEW_UpdateItemSize(infoPtr);
10523 if (infoPtr->uView == LV_VIEW_DETAILS && is_redrawing(infoPtr))
10525 POINT ptOrigin;
10526 RECT rcCol = lpColumnInfo->rcHeader;
10528 LISTVIEW_GetOrigin(infoPtr, &ptOrigin);
10529 OffsetRect(&rcCol, ptOrigin.x, 0);
10531 rcCol.top = infoPtr->rcList.top;
10532 rcCol.bottom = infoPtr->rcList.bottom;
10534 /* resizing left-aligned columns leaves most of the left side untouched */
10535 if ((lpColumnInfo->fmt & LVCFMT_JUSTIFYMASK) == LVCFMT_LEFT)
10537 INT nMaxDirty = infoPtr->nEllipsisWidth + infoPtr->ntmMaxCharWidth;
10538 if (dx > 0)
10539 nMaxDirty += dx;
10540 rcCol.left = max (rcCol.left, rcCol.right - nMaxDirty);
10543 /* when shrinking the last column clear the now unused field */
10544 if (hdi.iOrder == DPA_GetPtrCount(infoPtr->hdpaColumns) - 1)
10546 RECT right;
10548 rcCol.right -= dx;
10550 /* deal with right from rightmost column area */
10551 right.left = rcCol.right;
10552 right.top = rcCol.top;
10553 right.bottom = rcCol.bottom;
10554 right.right = infoPtr->rcList.right;
10556 LISTVIEW_InvalidateRect(infoPtr, &right);
10559 LISTVIEW_InvalidateRect(infoPtr, &rcCol);
10562 break;
10565 case HDN_ITEMCLICKW:
10566 case HDN_ITEMCLICKA:
10568 /* Handle sorting by Header Column */
10569 NMLISTVIEW nmlv;
10571 ZeroMemory(&nmlv, sizeof(NMLISTVIEW));
10572 nmlv.iItem = -1;
10573 nmlv.iSubItem = lpnmh->iItem;
10574 notify_listview(infoPtr, LVN_COLUMNCLICK, &nmlv);
10575 return notify_forward_header(infoPtr, lpnmh);
10578 case HDN_DIVIDERDBLCLICKW:
10579 case HDN_DIVIDERDBLCLICKA:
10580 /* FIXME: for LVS_EX_HEADERINALLVIEWS and not LV_VIEW_DETAILS
10581 we should use LVSCW_AUTOSIZE_USEHEADER, helper rework or
10582 split needed for that */
10583 LISTVIEW_SetColumnWidth(infoPtr, lpnmh->iItem, LVSCW_AUTOSIZE);
10584 return notify_forward_header(infoPtr, lpnmh);
10586 return 0;
10589 /***
10590 * DESCRIPTION:
10591 * Paint non-client area of control.
10593 * PARAMETER(S):
10594 * [I] infoPtr : valid pointer to the listview structureof the sender
10595 * [I] region : update region
10597 * RETURN:
10598 * 0 - frame was painted
10600 static LRESULT LISTVIEW_NCPaint(const LISTVIEW_INFO *infoPtr, HRGN region)
10602 LONG exstyle = GetWindowLongW (infoPtr->hwndSelf, GWL_EXSTYLE);
10603 HTHEME theme = GetWindowTheme (infoPtr->hwndSelf);
10604 RECT r, window_rect;
10605 HDC dc;
10606 HRGN cliprgn;
10607 int cxEdge = GetSystemMetrics (SM_CXEDGE),
10608 cyEdge = GetSystemMetrics (SM_CYEDGE);
10610 if (!theme || !(exstyle & WS_EX_CLIENTEDGE))
10611 return DefWindowProcW (infoPtr->hwndSelf, WM_NCPAINT, (WPARAM)region, 0);
10613 GetWindowRect(infoPtr->hwndSelf, &r);
10615 cliprgn = CreateRectRgn (r.left + cxEdge, r.top + cyEdge,
10616 r.right - cxEdge, r.bottom - cyEdge);
10617 if (region != (HRGN)1)
10618 CombineRgn (cliprgn, cliprgn, region, RGN_AND);
10620 OffsetRect(&r, -r.left, -r.top);
10621 if (infoPtr->hwndHeader && LISTVIEW_IsHeaderEnabled(infoPtr))
10623 GetWindowRect(infoPtr->hwndHeader, &window_rect);
10624 r.top = min(r.bottom, r.top + window_rect.bottom - window_rect.top);
10627 dc = GetDCEx(infoPtr->hwndSelf, region, DCX_WINDOW|DCX_INTERSECTRGN);
10629 if (IsThemeBackgroundPartiallyTransparent (theme, 0, 0))
10630 DrawThemeParentBackground(infoPtr->hwndSelf, dc, &r);
10631 DrawThemeBackground (theme, dc, 0, 0, &r, 0);
10632 ReleaseDC(infoPtr->hwndSelf, dc);
10634 /* Call default proc to get the scrollbars etc. painted */
10635 DefWindowProcW (infoPtr->hwndSelf, WM_NCPAINT, (WPARAM)cliprgn, 0);
10636 DeleteObject(cliprgn);
10638 return 0;
10641 /***
10642 * DESCRIPTION:
10643 * Determines the type of structure to use.
10645 * PARAMETER(S):
10646 * [I] infoPtr : valid pointer to the listview structureof the sender
10647 * [I] hwndFrom : listview window handle
10648 * [I] nCommand : command specifying the nature of the WM_NOTIFYFORMAT
10650 * RETURN:
10651 * Zero
10653 static LRESULT LISTVIEW_NotifyFormat(LISTVIEW_INFO *infoPtr, HWND hwndFrom, INT nCommand)
10655 TRACE("(hwndFrom=%p, nCommand=%d)\n", hwndFrom, nCommand);
10657 if (nCommand == NF_REQUERY)
10658 infoPtr->notifyFormat = SendMessageW(infoPtr->hwndNotify, WM_NOTIFYFORMAT, (WPARAM)infoPtr->hwndSelf, NF_QUERY);
10660 return infoPtr->notifyFormat;
10663 /***
10664 * DESCRIPTION:
10665 * Paints/Repaints the listview control. Internal use.
10667 * PARAMETER(S):
10668 * [I] infoPtr : valid pointer to the listview structure
10669 * [I] hdc : device context handle
10671 * RETURN:
10672 * Zero
10674 static LRESULT LISTVIEW_Paint(LISTVIEW_INFO *infoPtr, HDC hdc)
10676 TRACE("(hdc=%p)\n", hdc);
10678 if (infoPtr->bNoItemMetrics && infoPtr->nItemCount)
10680 infoPtr->bNoItemMetrics = FALSE;
10681 LISTVIEW_UpdateItemSize(infoPtr);
10682 if (infoPtr->uView == LV_VIEW_ICON || infoPtr->uView == LV_VIEW_SMALLICON)
10683 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
10684 LISTVIEW_UpdateScroll(infoPtr);
10687 if (infoPtr->hwndHeader) UpdateWindow(infoPtr->hwndHeader);
10689 if (hdc)
10690 LISTVIEW_Refresh(infoPtr, hdc, NULL);
10691 else
10693 PAINTSTRUCT ps;
10695 hdc = BeginPaint(infoPtr->hwndSelf, &ps);
10696 if (!hdc) return 1;
10697 LISTVIEW_Refresh(infoPtr, hdc, ps.fErase ? &ps.rcPaint : NULL);
10698 EndPaint(infoPtr->hwndSelf, &ps);
10701 return 0;
10704 /***
10705 * DESCRIPTION:
10706 * Paints/Repaints the listview control, WM_PAINT handler.
10708 * PARAMETER(S):
10709 * [I] infoPtr : valid pointer to the listview structure
10710 * [I] hdc : device context handle
10712 * RETURN:
10713 * Zero
10715 static inline LRESULT LISTVIEW_WMPaint(LISTVIEW_INFO *infoPtr, HDC hdc)
10717 TRACE("(hdc=%p)\n", hdc);
10719 if (!is_redrawing(infoPtr))
10720 return DefWindowProcW (infoPtr->hwndSelf, WM_PAINT, (WPARAM)hdc, 0);
10722 return LISTVIEW_Paint(infoPtr, hdc);
10725 /***
10726 * DESCRIPTION:
10727 * Paints/Repaints the listview control.
10729 * PARAMETER(S):
10730 * [I] infoPtr : valid pointer to the listview structure
10731 * [I] hdc : device context handle
10732 * [I] options : drawing options
10734 * RETURN:
10735 * Zero
10737 static LRESULT LISTVIEW_PrintClient(LISTVIEW_INFO *infoPtr, HDC hdc, DWORD options)
10739 if ((options & PRF_CHECKVISIBLE) && !IsWindowVisible(infoPtr->hwndSelf))
10740 return 0;
10742 if (options & ~(PRF_ERASEBKGND|PRF_CLIENT))
10743 FIXME("(hdc=%p options %#lx) partial stub\n", hdc, options);
10745 if (options & PRF_ERASEBKGND)
10746 LISTVIEW_EraseBkgnd(infoPtr, hdc);
10748 if (options & PRF_CLIENT)
10749 LISTVIEW_Paint(infoPtr, hdc);
10751 return 0;
10755 /***
10756 * DESCRIPTION:
10757 * Processes double click messages (right mouse button).
10759 * PARAMETER(S):
10760 * [I] infoPtr : valid pointer to the listview structure
10761 * [I] wKey : key flag
10762 * [I] x,y : mouse coordinate
10764 * RETURN:
10765 * Zero
10767 static LRESULT LISTVIEW_RButtonDblClk(const LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
10769 LVHITTESTINFO lvHitTestInfo;
10771 TRACE("(key=%hu,X=%u,Y=%u)\n", wKey, x, y);
10773 /* send NM_RELEASEDCAPTURE notification */
10774 if (!notify(infoPtr, NM_RELEASEDCAPTURE)) return 0;
10776 /* send NM_RDBLCLK notification */
10777 lvHitTestInfo.pt.x = x;
10778 lvHitTestInfo.pt.y = y;
10779 LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, FALSE);
10780 notify_click(infoPtr, NM_RDBLCLK, &lvHitTestInfo);
10782 return 0;
10785 /***
10786 * DESCRIPTION:
10787 * Processes WM_RBUTTONDOWN message and corresponding drag operation.
10789 * PARAMETER(S):
10790 * [I] infoPtr : valid pointer to the listview structure
10791 * [I] wKey : key flag
10792 * [I] x, y : mouse coordinate
10794 * RETURN:
10795 * Zero
10797 static LRESULT LISTVIEW_RButtonDown(LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
10799 LVHITTESTINFO ht;
10800 INT item;
10802 TRACE("(key=%hu, x=%d, y=%d)\n", wKey, x, y);
10804 /* send NM_RELEASEDCAPTURE notification */
10805 if (!notify(infoPtr, NM_RELEASEDCAPTURE)) return 0;
10807 /* determine the index of the selected item */
10808 ht.pt.x = x;
10809 ht.pt.y = y;
10810 item = LISTVIEW_HitTest(infoPtr, &ht, TRUE, TRUE);
10812 /* make sure the listview control window has the focus */
10813 if (!infoPtr->bFocus) SetFocus(infoPtr->hwndSelf);
10815 if ((item >= 0) && (item < infoPtr->nItemCount))
10817 LISTVIEW_SetItemFocus(infoPtr, item);
10818 if (!((wKey & MK_SHIFT) || (wKey & MK_CONTROL)) &&
10819 !LISTVIEW_GetItemState(infoPtr, item, LVIS_SELECTED))
10820 LISTVIEW_SetSelection(infoPtr, item);
10822 else
10823 LISTVIEW_DeselectAll(infoPtr);
10825 if (LISTVIEW_TrackMouse(infoPtr, ht.pt))
10827 if (ht.iItem != -1)
10829 NMLISTVIEW nmlv;
10831 memset(&nmlv, 0, sizeof(nmlv));
10832 nmlv.iItem = ht.iItem;
10833 nmlv.ptAction = ht.pt;
10835 notify_listview(infoPtr, LVN_BEGINRDRAG, &nmlv);
10838 else
10840 SetFocus(infoPtr->hwndSelf);
10842 ht.pt.x = x;
10843 ht.pt.y = y;
10844 LISTVIEW_HitTest(infoPtr, &ht, TRUE, FALSE);
10846 if (notify_click(infoPtr, NM_RCLICK, &ht))
10848 /* Send a WM_CONTEXTMENU message in response to the WM_RBUTTONUP */
10849 SendMessageW(infoPtr->hwndSelf, WM_CONTEXTMENU,
10850 (WPARAM)infoPtr->hwndSelf, (LPARAM)GetMessagePos());
10854 return 0;
10857 /***
10858 * DESCRIPTION:
10859 * Sets the cursor.
10861 * PARAMETER(S):
10862 * [I] infoPtr : valid pointer to the listview structure
10863 * [I] hwnd : window handle of window containing the cursor
10864 * [I] nHittest : hit-test code
10865 * [I] wMouseMsg : ideintifier of the mouse message
10867 * RETURN:
10868 * TRUE if cursor is set
10869 * FALSE otherwise
10871 static BOOL LISTVIEW_SetCursor(const LISTVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
10873 LVHITTESTINFO lvHitTestInfo;
10875 if (!LISTVIEW_IsHotTracking(infoPtr)) goto forward;
10877 if (!infoPtr->hHotCursor) goto forward;
10879 GetCursorPos(&lvHitTestInfo.pt);
10880 if (LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, FALSE, FALSE) < 0) goto forward;
10882 SetCursor(infoPtr->hHotCursor);
10884 return TRUE;
10886 forward:
10888 return DefWindowProcW(infoPtr->hwndSelf, WM_SETCURSOR, wParam, lParam);
10891 /***
10892 * DESCRIPTION:
10893 * Sets the focus.
10895 * PARAMETER(S):
10896 * [I] infoPtr : valid pointer to the listview structure
10897 * [I] hwndLoseFocus : handle of previously focused window
10899 * RETURN:
10900 * Zero
10902 static LRESULT LISTVIEW_SetFocus(LISTVIEW_INFO *infoPtr, HWND hwndLoseFocus)
10904 TRACE("(hwndLoseFocus=%p)\n", hwndLoseFocus);
10906 /* if we have the focus already, there's nothing to do */
10907 if (infoPtr->bFocus) return 0;
10909 /* send NM_SETFOCUS notification */
10910 if (!notify(infoPtr, NM_SETFOCUS)) return 0;
10912 /* set window focus flag */
10913 infoPtr->bFocus = TRUE;
10915 /* put the focus rect back on */
10916 LISTVIEW_ShowFocusRect(infoPtr, TRUE);
10918 /* redraw all visible selected items */
10919 LISTVIEW_InvalidateSelectedItems(infoPtr);
10921 return 0;
10924 /***
10925 * DESCRIPTION:
10926 * Sets the font.
10928 * PARAMETER(S):
10929 * [I] infoPtr : valid pointer to the listview structure
10930 * [I] fRedraw : font handle
10931 * [I] fRedraw : redraw flag
10933 * RETURN:
10934 * Zero
10936 static LRESULT LISTVIEW_SetFont(LISTVIEW_INFO *infoPtr, HFONT hFont, WORD fRedraw)
10938 HFONT oldFont = infoPtr->hFont;
10939 INT oldHeight = infoPtr->nItemHeight;
10941 TRACE("(hfont=%p,redraw=%hu)\n", hFont, fRedraw);
10943 infoPtr->hFont = hFont ? hFont : infoPtr->hDefaultFont;
10944 if (infoPtr->hFont == oldFont) return 0;
10946 LISTVIEW_SaveTextMetrics(infoPtr);
10948 infoPtr->nItemHeight = LISTVIEW_CalculateItemHeight(infoPtr);
10950 if (infoPtr->uView == LV_VIEW_DETAILS)
10952 SendMessageW(infoPtr->hwndHeader, WM_SETFONT, (WPARAM)hFont, MAKELPARAM(fRedraw, 0));
10953 LISTVIEW_UpdateSize(infoPtr);
10954 LISTVIEW_UpdateScroll(infoPtr);
10956 else if (infoPtr->nItemHeight != oldHeight)
10957 LISTVIEW_UpdateScroll(infoPtr);
10959 if (fRedraw) LISTVIEW_InvalidateList(infoPtr);
10961 return 0;
10964 /***
10965 * DESCRIPTION:
10966 * Message handling for WM_SETREDRAW.
10967 * For the Listview, it invalidates the entire window (the doc specifies otherwise)
10969 * PARAMETER(S):
10970 * [I] infoPtr : valid pointer to the listview structure
10971 * [I] redraw: state of redraw flag
10973 * RETURN:
10974 * Zero.
10976 static LRESULT LISTVIEW_SetRedraw(LISTVIEW_INFO *infoPtr, BOOL redraw)
10978 TRACE("old=%d, new=%d\n", infoPtr->redraw, redraw);
10980 if (infoPtr->redraw == !!redraw)
10981 return 0;
10983 if (!(infoPtr->redraw = !!redraw))
10984 return 0;
10986 if (is_autoarrange(infoPtr))
10987 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
10988 LISTVIEW_UpdateScroll(infoPtr);
10990 /* despite what the WM_SETREDRAW docs says, apps expect us
10991 * to invalidate the listview here... stupid! */
10992 LISTVIEW_InvalidateList(infoPtr);
10994 return 0;
10997 /***
10998 * DESCRIPTION:
10999 * Resizes the listview control. This function processes WM_SIZE
11000 * messages. At this time, the width and height are not used.
11002 * PARAMETER(S):
11003 * [I] infoPtr : valid pointer to the listview structure
11004 * [I] Width : new width
11005 * [I] Height : new height
11007 * RETURN:
11008 * Zero
11010 static LRESULT LISTVIEW_Size(LISTVIEW_INFO *infoPtr, int Width, int Height)
11012 RECT rcOld = infoPtr->rcList;
11014 TRACE("(width=%d, height=%d)\n", Width, Height);
11016 LISTVIEW_UpdateSize(infoPtr);
11017 if (EqualRect(&rcOld, &infoPtr->rcList)) return 0;
11019 /* do not bother with display related stuff if we're not redrawing */
11020 if (!is_redrawing(infoPtr)) return 0;
11022 if (is_autoarrange(infoPtr))
11023 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
11025 LISTVIEW_UpdateScroll(infoPtr);
11027 /* refresh all only for lists whose height changed significantly */
11028 if ((infoPtr->uView == LV_VIEW_LIST) &&
11029 (rcOld.bottom - rcOld.top) / infoPtr->nItemHeight !=
11030 (infoPtr->rcList.bottom - infoPtr->rcList.top) / infoPtr->nItemHeight)
11031 LISTVIEW_InvalidateList(infoPtr);
11033 return 0;
11036 /***
11037 * DESCRIPTION:
11038 * Sets the size information.
11040 * PARAMETER(S):
11041 * [I] infoPtr : valid pointer to the listview structure
11043 * RETURN:
11044 * None
11046 static void LISTVIEW_UpdateSize(LISTVIEW_INFO *infoPtr)
11048 TRACE("uView %ld, rcList(old)=%s\n", infoPtr->uView, wine_dbgstr_rect(&infoPtr->rcList));
11050 GetClientRect(infoPtr->hwndSelf, &infoPtr->rcList);
11052 if (infoPtr->uView == LV_VIEW_LIST)
11054 /* Apparently the "LIST" style is supposed to have the same
11055 * number of items in a column even if there is no scroll bar.
11056 * Since if a scroll bar already exists then the bottom is already
11057 * reduced, only reduce if the scroll bar does not currently exist.
11058 * The "2" is there to mimic the native control. I think it may be
11059 * related to either padding or edges. (GLA 7/2002)
11061 if (!(GetWindowLongW(infoPtr->hwndSelf, GWL_STYLE) & WS_HSCROLL))
11062 infoPtr->rcList.bottom -= GetSystemMetrics(SM_CYHSCROLL);
11063 infoPtr->rcList.bottom = max (infoPtr->rcList.bottom - 2, 0);
11066 /* When ListView control is created invisible, header isn't created right away. */
11067 if (infoPtr->hwndHeader)
11069 POINT origin;
11070 WINDOWPOS wp;
11071 HDLAYOUT hl;
11072 RECT rect;
11074 LISTVIEW_GetOrigin(infoPtr, &origin);
11076 rect = infoPtr->rcList;
11077 rect.left += origin.x;
11079 hl.prc = &rect;
11080 hl.pwpos = &wp;
11081 SendMessageW( infoPtr->hwndHeader, HDM_LAYOUT, 0, (LPARAM)&hl );
11082 TRACE(" wp.flags=0x%08x, wp=%d,%d (%dx%d)\n", wp.flags, wp.x, wp.y, wp.cx, wp.cy);
11084 if (LISTVIEW_IsHeaderEnabled(infoPtr))
11085 wp.flags |= SWP_SHOWWINDOW;
11086 else
11088 wp.flags |= SWP_HIDEWINDOW;
11089 wp.cy = 0;
11092 SetWindowPos(wp.hwnd, wp.hwndInsertAfter, wp.x, wp.y, wp.cx, wp.cy, wp.flags);
11093 TRACE(" after SWP wp=%d,%d (%dx%d)\n", wp.x, wp.y, wp.cx, wp.cy);
11095 infoPtr->rcList.top = max(wp.cy, 0);
11097 /* extra padding for grid */
11098 if (infoPtr->uView == LV_VIEW_DETAILS && infoPtr->dwLvExStyle & LVS_EX_GRIDLINES)
11099 infoPtr->rcList.top += 2;
11101 TRACE(" rcList=%s\n", wine_dbgstr_rect(&infoPtr->rcList));
11104 /***
11105 * DESCRIPTION:
11106 * Processes WM_STYLECHANGED messages.
11108 * PARAMETER(S):
11109 * [I] infoPtr : valid pointer to the listview structure
11110 * [I] wStyleType : window style type (normal or extended)
11111 * [I] lpss : window style information
11113 * RETURN:
11114 * Zero
11116 static INT LISTVIEW_StyleChanged(LISTVIEW_INFO *infoPtr, WPARAM wStyleType,
11117 const STYLESTRUCT *lpss)
11119 UINT uNewView, uOldView;
11120 UINT style;
11122 TRACE("styletype %Ix, styleOld %#lx, styleNew %#lx\n",
11123 wStyleType, lpss->styleOld, lpss->styleNew);
11125 if (wStyleType != GWL_STYLE || lpss->styleNew == infoPtr->dwStyle) return 0;
11127 infoPtr->dwStyle = lpss->styleNew;
11129 if (((lpss->styleOld & WS_HSCROLL) != 0)&&
11130 ((lpss->styleNew & WS_HSCROLL) == 0))
11131 ShowScrollBar(infoPtr->hwndSelf, SB_HORZ, FALSE);
11133 if (((lpss->styleOld & WS_VSCROLL) != 0)&&
11134 ((lpss->styleNew & WS_VSCROLL) == 0))
11135 ShowScrollBar(infoPtr->hwndSelf, SB_VERT, FALSE);
11137 uNewView = lpss->styleNew & LVS_TYPEMASK;
11138 uOldView = lpss->styleOld & LVS_TYPEMASK;
11140 if (uNewView != uOldView)
11142 HIMAGELIST himl;
11144 /* LVM_SETVIEW doesn't change window style bits within LVS_TYPEMASK,
11145 changing style updates current view only when view bits change. */
11146 map_style_view(infoPtr);
11147 SendMessageW(infoPtr->hwndEdit, WM_KILLFOCUS, 0, 0);
11148 ShowWindow(infoPtr->hwndHeader, SW_HIDE);
11150 ShowScrollBar(infoPtr->hwndSelf, SB_BOTH, FALSE);
11151 SetRectEmpty(&infoPtr->rcFocus);
11153 himl = (uNewView == LVS_ICON ? infoPtr->himlNormal : infoPtr->himlSmall);
11154 set_icon_size(&infoPtr->iconSize, himl, uNewView != LVS_ICON);
11156 if (uNewView == LVS_REPORT)
11158 HDLAYOUT hl;
11159 WINDOWPOS wp;
11161 LISTVIEW_CreateHeader( infoPtr );
11163 hl.prc = &infoPtr->rcList;
11164 hl.pwpos = &wp;
11165 SendMessageW( infoPtr->hwndHeader, HDM_LAYOUT, 0, (LPARAM)&hl );
11166 SetWindowPos(infoPtr->hwndHeader, infoPtr->hwndSelf, wp.x, wp.y, wp.cx, wp.cy,
11167 wp.flags | ((infoPtr->dwStyle & LVS_NOCOLUMNHEADER)
11168 ? SWP_HIDEWINDOW : SWP_SHOWWINDOW));
11171 LISTVIEW_UpdateItemSize(infoPtr);
11174 if (uNewView == LVS_REPORT || infoPtr->dwLvExStyle & LVS_EX_HEADERINALLVIEWS)
11176 if ((lpss->styleOld ^ lpss->styleNew) & LVS_NOCOLUMNHEADER)
11178 if (lpss->styleNew & LVS_NOCOLUMNHEADER)
11180 /* Turn off the header control */
11181 style = GetWindowLongW(infoPtr->hwndHeader, GWL_STYLE);
11182 TRACE("Hide header control, was 0x%08x\n", style);
11183 SetWindowLongW(infoPtr->hwndHeader, GWL_STYLE, style | HDS_HIDDEN);
11184 } else {
11185 /* Turn on the header control */
11186 if ((style = GetWindowLongW(infoPtr->hwndHeader, GWL_STYLE)) & HDS_HIDDEN)
11188 TRACE("Show header control, was 0x%08x\n", style);
11189 SetWindowLongW(infoPtr->hwndHeader, GWL_STYLE, (style & ~HDS_HIDDEN) | WS_VISIBLE);
11195 if ( (uNewView == LVS_ICON || uNewView == LVS_SMALLICON) &&
11196 (uNewView != uOldView || ((lpss->styleNew ^ lpss->styleOld) & LVS_ALIGNMASK)) )
11197 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
11199 /* update the size of the client area */
11200 LISTVIEW_UpdateSize(infoPtr);
11202 /* add scrollbars if needed */
11203 LISTVIEW_UpdateScroll(infoPtr);
11205 /* invalidate client area + erase background */
11206 LISTVIEW_InvalidateList(infoPtr);
11208 return 0;
11211 /***
11212 * DESCRIPTION:
11213 * Processes WM_STYLECHANGING messages.
11215 * PARAMETER(S):
11216 * [I] wStyleType : window style type (normal or extended)
11217 * [I0] lpss : window style information
11219 * RETURN:
11220 * Zero
11222 static INT LISTVIEW_StyleChanging(WPARAM wStyleType,
11223 STYLESTRUCT *lpss)
11225 TRACE("styletype %Ix, styleOld %#lx, styleNew %#lx\n",
11226 wStyleType, lpss->styleOld, lpss->styleNew);
11228 /* don't forward LVS_OWNERDATA only if not already set to */
11229 if ((lpss->styleNew ^ lpss->styleOld) & LVS_OWNERDATA)
11231 if (lpss->styleOld & LVS_OWNERDATA)
11232 lpss->styleNew |= LVS_OWNERDATA;
11233 else
11234 lpss->styleNew &= ~LVS_OWNERDATA;
11237 return 0;
11240 /***
11241 * DESCRIPTION:
11242 * Processes WM_SHOWWINDOW messages.
11244 * PARAMETER(S):
11245 * [I] infoPtr : valid pointer to the listview structure
11246 * [I] bShown : window is being shown (FALSE when hidden)
11247 * [I] iStatus : window show status
11249 * RETURN:
11250 * Zero
11252 static LRESULT LISTVIEW_ShowWindow(LISTVIEW_INFO *infoPtr, WPARAM bShown, LPARAM iStatus)
11254 /* header delayed creation */
11255 if ((infoPtr->uView == LV_VIEW_DETAILS) && bShown)
11257 LISTVIEW_CreateHeader(infoPtr);
11259 if (!(LVS_NOCOLUMNHEADER & infoPtr->dwStyle))
11260 ShowWindow(infoPtr->hwndHeader, SW_SHOWNORMAL);
11263 return DefWindowProcW(infoPtr->hwndSelf, WM_SHOWWINDOW, bShown, iStatus);
11266 /***
11267 * DESCRIPTION:
11268 * Processes CCM_GETVERSION messages.
11270 * PARAMETER(S):
11271 * [I] infoPtr : valid pointer to the listview structure
11273 * RETURN:
11274 * Current version
11276 static inline LRESULT LISTVIEW_GetVersion(const LISTVIEW_INFO *infoPtr)
11278 return infoPtr->iVersion;
11281 /***
11282 * DESCRIPTION:
11283 * Processes CCM_SETVERSION messages.
11285 * PARAMETER(S):
11286 * [I] infoPtr : valid pointer to the listview structure
11287 * [I] iVersion : version to be set
11289 * RETURN:
11290 * -1 when requested version is greater than DLL version;
11291 * previous version otherwise
11293 static LRESULT LISTVIEW_SetVersion(LISTVIEW_INFO *infoPtr, DWORD iVersion)
11295 INT iOldVersion = infoPtr->iVersion;
11297 if (iVersion > COMCTL32_VERSION)
11298 return -1;
11300 infoPtr->iVersion = iVersion;
11302 TRACE("new version %ld\n", iVersion);
11304 return iOldVersion;
11307 /***
11308 * DESCRIPTION:
11309 * Window procedure of the listview control.
11312 static LRESULT WINAPI
11313 LISTVIEW_WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
11315 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)GetWindowLongPtrW(hwnd, 0);
11317 TRACE("hwnd %p, uMsg %x, wParam %Ix, lParam %Ix\n", hwnd, uMsg, wParam, lParam);
11319 if (!infoPtr && (uMsg != WM_NCCREATE))
11320 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
11322 switch (uMsg)
11324 case LVM_APPROXIMATEVIEWRECT:
11325 return LISTVIEW_ApproximateViewRect(infoPtr, (INT)wParam,
11326 LOWORD(lParam), HIWORD(lParam));
11327 case LVM_ARRANGE:
11328 return LISTVIEW_Arrange(infoPtr, (INT)wParam);
11330 case LVM_CANCELEDITLABEL:
11331 return LISTVIEW_CancelEditLabel(infoPtr);
11333 case LVM_CREATEDRAGIMAGE:
11334 return (LRESULT)LISTVIEW_CreateDragImage(infoPtr, (INT)wParam, (LPPOINT)lParam);
11336 case LVM_DELETEALLITEMS:
11337 return LISTVIEW_DeleteAllItems(infoPtr, FALSE);
11339 case LVM_DELETECOLUMN:
11340 return LISTVIEW_DeleteColumn(infoPtr, (INT)wParam);
11342 case LVM_DELETEITEM:
11343 return LISTVIEW_DeleteItem(infoPtr, (INT)wParam);
11345 case LVM_EDITLABELA:
11346 case LVM_EDITLABELW:
11347 return (LRESULT)LISTVIEW_EditLabelT(infoPtr, (INT)wParam,
11348 uMsg == LVM_EDITLABELW);
11349 /* case LVM_ENABLEGROUPVIEW: */
11351 case LVM_ENSUREVISIBLE:
11352 return LISTVIEW_EnsureVisible(infoPtr, (INT)wParam, (BOOL)lParam);
11354 case LVM_FINDITEMW:
11355 return LISTVIEW_FindItemW(infoPtr, (INT)wParam, (LPLVFINDINFOW)lParam);
11357 case LVM_FINDITEMA:
11358 return LISTVIEW_FindItemA(infoPtr, (INT)wParam, (LPLVFINDINFOA)lParam);
11360 case LVM_GETBKCOLOR:
11361 return infoPtr->clrBk;
11363 /* case LVM_GETBKIMAGE: */
11365 case LVM_GETCALLBACKMASK:
11366 return infoPtr->uCallbackMask;
11368 case LVM_GETCOLUMNA:
11369 case LVM_GETCOLUMNW:
11370 return LISTVIEW_GetColumnT(infoPtr, (INT)wParam, (LPLVCOLUMNW)lParam,
11371 uMsg == LVM_GETCOLUMNW);
11373 case LVM_GETCOLUMNORDERARRAY:
11374 return LISTVIEW_GetColumnOrderArray(infoPtr, (INT)wParam, (LPINT)lParam);
11376 case LVM_GETCOLUMNWIDTH:
11377 return LISTVIEW_GetColumnWidth(infoPtr, (INT)wParam);
11379 case LVM_GETCOUNTPERPAGE:
11380 return LISTVIEW_GetCountPerPage(infoPtr);
11382 case LVM_GETEDITCONTROL:
11383 return (LRESULT)infoPtr->hwndEdit;
11385 case LVM_GETEXTENDEDLISTVIEWSTYLE:
11386 return infoPtr->dwLvExStyle;
11388 /* case LVM_GETGROUPINFO: */
11390 /* case LVM_GETGROUPMETRICS: */
11392 case LVM_GETHEADER:
11393 return (LRESULT)infoPtr->hwndHeader;
11395 case LVM_GETHOTCURSOR:
11396 return (LRESULT)infoPtr->hHotCursor;
11398 case LVM_GETHOTITEM:
11399 return infoPtr->nHotItem;
11401 case LVM_GETHOVERTIME:
11402 return infoPtr->dwHoverTime;
11404 case LVM_GETIMAGELIST:
11405 return (LRESULT)LISTVIEW_GetImageList(infoPtr, (INT)wParam);
11407 /* case LVM_GETINSERTMARK: */
11409 /* case LVM_GETINSERTMARKCOLOR: */
11411 /* case LVM_GETINSERTMARKRECT: */
11413 case LVM_GETISEARCHSTRINGA:
11414 case LVM_GETISEARCHSTRINGW:
11415 FIXME("LVM_GETISEARCHSTRING: unimplemented\n");
11416 return FALSE;
11418 case LVM_GETITEMA:
11419 case LVM_GETITEMW:
11420 return LISTVIEW_GetItemExtT(infoPtr, (LPLVITEMW)lParam, uMsg == LVM_GETITEMW);
11422 case LVM_GETITEMCOUNT:
11423 return infoPtr->nItemCount;
11425 case LVM_GETITEMPOSITION:
11426 return LISTVIEW_GetItemPosition(infoPtr, (INT)wParam, (LPPOINT)lParam);
11428 case LVM_GETITEMRECT:
11429 return LISTVIEW_GetItemRect(infoPtr, (INT)wParam, (LPRECT)lParam);
11431 case LVM_GETITEMSPACING:
11432 return LISTVIEW_GetItemSpacing(infoPtr, (BOOL)wParam);
11434 case LVM_GETITEMSTATE:
11435 return LISTVIEW_GetItemState(infoPtr, (INT)wParam, (UINT)lParam);
11437 case LVM_GETITEMTEXTA:
11438 case LVM_GETITEMTEXTW:
11439 return LISTVIEW_GetItemTextT(infoPtr, (INT)wParam, (LPLVITEMW)lParam,
11440 uMsg == LVM_GETITEMTEXTW);
11442 case LVM_GETNEXTITEM:
11443 return LISTVIEW_GetNextItem(infoPtr, (INT)wParam, LOWORD(lParam));
11445 case LVM_GETNEXTITEMINDEX:
11446 return LISTVIEW_GetNextItemIndex(infoPtr, (LVITEMINDEX *)wParam, lParam);
11448 case LVM_GETNUMBEROFWORKAREAS:
11449 FIXME("LVM_GETNUMBEROFWORKAREAS: unimplemented\n");
11450 return 1;
11452 case LVM_GETORIGIN:
11453 if (!lParam) return FALSE;
11454 if (infoPtr->uView == LV_VIEW_DETAILS ||
11455 infoPtr->uView == LV_VIEW_LIST) return FALSE;
11456 LISTVIEW_GetOrigin(infoPtr, (LPPOINT)lParam);
11457 return TRUE;
11459 /* case LVM_GETOUTLINECOLOR: */
11461 case LVM_GETSELECTEDCOLUMN:
11462 return infoPtr->selected_column;
11464 case LVM_GETSELECTEDCOUNT:
11465 return LISTVIEW_GetSelectedCount(infoPtr);
11467 case LVM_GETSELECTIONMARK:
11468 return infoPtr->nSelectionMark;
11470 case LVM_GETSTRINGWIDTHA:
11471 case LVM_GETSTRINGWIDTHW:
11472 return LISTVIEW_GetStringWidthT(infoPtr, (LPCWSTR)lParam,
11473 uMsg == LVM_GETSTRINGWIDTHW);
11475 case LVM_GETSUBITEMRECT:
11476 return LISTVIEW_GetSubItemRect(infoPtr, (UINT)wParam, (LPRECT)lParam);
11478 case LVM_GETTEXTBKCOLOR:
11479 return infoPtr->clrTextBk;
11481 case LVM_GETTEXTCOLOR:
11482 return infoPtr->clrText;
11484 /* case LVM_GETTILEINFO: */
11486 /* case LVM_GETTILEVIEWINFO: */
11488 case LVM_GETTOOLTIPS:
11489 if( !infoPtr->hwndToolTip )
11490 infoPtr->hwndToolTip = COMCTL32_CreateToolTip( hwnd );
11491 return (LRESULT)infoPtr->hwndToolTip;
11493 case LVM_GETTOPINDEX:
11494 return LISTVIEW_GetTopIndex(infoPtr);
11496 case LVM_GETUNICODEFORMAT:
11497 return (infoPtr->notifyFormat == NFR_UNICODE);
11499 case LVM_GETVIEW:
11500 return infoPtr->uView;
11502 case LVM_GETVIEWRECT:
11503 return LISTVIEW_GetViewRect(infoPtr, (LPRECT)lParam);
11505 case LVM_GETWORKAREAS:
11506 FIXME("LVM_GETWORKAREAS: unimplemented\n");
11507 return FALSE;
11509 /* case LVM_HASGROUP: */
11511 case LVM_HITTEST:
11512 return LISTVIEW_HitTest(infoPtr, (LPLVHITTESTINFO)lParam, FALSE, TRUE);
11514 case LVM_INSERTCOLUMNA:
11515 case LVM_INSERTCOLUMNW:
11516 return LISTVIEW_InsertColumnT(infoPtr, (INT)wParam, (LPLVCOLUMNW)lParam,
11517 uMsg == LVM_INSERTCOLUMNW);
11519 /* case LVM_INSERTGROUP: */
11521 /* case LVM_INSERTGROUPSORTED: */
11523 case LVM_INSERTITEMA:
11524 case LVM_INSERTITEMW:
11525 return LISTVIEW_InsertItemT(infoPtr, (LPLVITEMW)lParam, uMsg == LVM_INSERTITEMW);
11527 /* case LVM_INSERTMARKHITTEST: */
11529 /* case LVM_ISGROUPVIEWENABLED: */
11531 case LVM_ISITEMVISIBLE:
11532 return LISTVIEW_IsItemVisible(infoPtr, (INT)wParam);
11534 case LVM_MAPIDTOINDEX:
11535 return LISTVIEW_MapIdToIndex(infoPtr, (UINT)wParam);
11537 case LVM_MAPINDEXTOID:
11538 return LISTVIEW_MapIndexToId(infoPtr, (INT)wParam);
11540 /* case LVM_MOVEGROUP: */
11542 /* case LVM_MOVEITEMTOGROUP: */
11544 case LVM_REDRAWITEMS:
11545 return LISTVIEW_RedrawItems(infoPtr, (INT)wParam, (INT)lParam);
11547 /* case LVM_REMOVEALLGROUPS: */
11549 /* case LVM_REMOVEGROUP: */
11551 case LVM_SCROLL:
11552 return LISTVIEW_Scroll(infoPtr, (INT)wParam, (INT)lParam);
11554 case LVM_SETBKCOLOR:
11555 return LISTVIEW_SetBkColor(infoPtr, (COLORREF)lParam);
11557 /* case LVM_SETBKIMAGE: */
11559 case LVM_SETCALLBACKMASK:
11560 infoPtr->uCallbackMask = (UINT)wParam;
11561 return TRUE;
11563 case LVM_SETCOLUMNA:
11564 case LVM_SETCOLUMNW:
11565 return LISTVIEW_SetColumnT(infoPtr, (INT)wParam, (LPLVCOLUMNW)lParam,
11566 uMsg == LVM_SETCOLUMNW);
11568 case LVM_SETCOLUMNORDERARRAY:
11569 return LISTVIEW_SetColumnOrderArray(infoPtr, (INT)wParam, (LPINT)lParam);
11571 case LVM_SETCOLUMNWIDTH:
11572 return LISTVIEW_SetColumnWidth(infoPtr, (INT)wParam, (short)LOWORD(lParam));
11574 case LVM_SETEXTENDEDLISTVIEWSTYLE:
11575 return LISTVIEW_SetExtendedListViewStyle(infoPtr, (DWORD)wParam, (DWORD)lParam);
11577 /* case LVM_SETGROUPINFO: */
11579 /* case LVM_SETGROUPMETRICS: */
11581 case LVM_SETHOTCURSOR:
11582 return (LRESULT)LISTVIEW_SetHotCursor(infoPtr, (HCURSOR)lParam);
11584 case LVM_SETHOTITEM:
11585 return LISTVIEW_SetHotItem(infoPtr, (INT)wParam);
11587 case LVM_SETHOVERTIME:
11588 return LISTVIEW_SetHoverTime(infoPtr, (DWORD)lParam);
11590 case LVM_SETICONSPACING:
11591 if(lParam == -1)
11592 return LISTVIEW_SetIconSpacing(infoPtr, -1, -1);
11593 return LISTVIEW_SetIconSpacing(infoPtr, LOWORD(lParam), HIWORD(lParam));
11595 case LVM_SETIMAGELIST:
11596 return (LRESULT)LISTVIEW_SetImageList(infoPtr, (INT)wParam, (HIMAGELIST)lParam);
11598 /* case LVM_SETINFOTIP: */
11600 /* case LVM_SETINSERTMARK: */
11602 /* case LVM_SETINSERTMARKCOLOR: */
11604 case LVM_SETITEMA:
11605 case LVM_SETITEMW:
11607 if (infoPtr->dwStyle & LVS_OWNERDATA) return FALSE;
11608 return LISTVIEW_SetItemT(infoPtr, (LPLVITEMW)lParam, (uMsg == LVM_SETITEMW));
11611 case LVM_SETITEMCOUNT:
11612 return LISTVIEW_SetItemCount(infoPtr, (INT)wParam, (DWORD)lParam);
11614 case LVM_SETITEMPOSITION:
11616 POINT pt;
11617 pt.x = (short)LOWORD(lParam);
11618 pt.y = (short)HIWORD(lParam);
11619 return LISTVIEW_SetItemPosition(infoPtr, (INT)wParam, &pt);
11622 case LVM_SETITEMPOSITION32:
11623 return LISTVIEW_SetItemPosition(infoPtr, (INT)wParam, (POINT*)lParam);
11625 case LVM_SETITEMSTATE:
11626 return LISTVIEW_SetItemState(infoPtr, (INT)wParam, (LPLVITEMW)lParam);
11628 case LVM_SETITEMTEXTA:
11629 case LVM_SETITEMTEXTW:
11630 return LISTVIEW_SetItemTextT(infoPtr, (INT)wParam, (LPLVITEMW)lParam,
11631 uMsg == LVM_SETITEMTEXTW);
11633 /* case LVM_SETOUTLINECOLOR: */
11635 case LVM_SETSELECTEDCOLUMN:
11636 infoPtr->selected_column = (INT)wParam;
11637 return TRUE;
11639 case LVM_SETSELECTIONMARK:
11640 return LISTVIEW_SetSelectionMark(infoPtr, (INT)lParam);
11642 case LVM_SETTEXTBKCOLOR:
11643 return LISTVIEW_SetTextBkColor(infoPtr, (COLORREF)lParam);
11645 case LVM_SETTEXTCOLOR:
11646 return LISTVIEW_SetTextColor(infoPtr, (COLORREF)lParam);
11648 /* case LVM_SETTILEINFO: */
11650 /* case LVM_SETTILEVIEWINFO: */
11652 /* case LVM_SETTILEWIDTH: */
11654 case LVM_SETTOOLTIPS:
11655 return (LRESULT)LISTVIEW_SetToolTips(infoPtr, (HWND)lParam);
11657 case LVM_SETUNICODEFORMAT:
11658 return LISTVIEW_SetUnicodeFormat(infoPtr, wParam);
11660 case LVM_SETVIEW:
11661 return LISTVIEW_SetView(infoPtr, wParam);
11663 /* case LVM_SETWORKAREAS: */
11665 /* case LVM_SORTGROUPS: */
11667 case LVM_SORTITEMS:
11668 case LVM_SORTITEMSEX:
11669 return LISTVIEW_SortItems(infoPtr, (PFNLVCOMPARE)lParam, wParam,
11670 uMsg == LVM_SORTITEMSEX);
11671 case LVM_SUBITEMHITTEST:
11672 return LISTVIEW_HitTest(infoPtr, (LPLVHITTESTINFO)lParam, TRUE, FALSE);
11674 case LVM_UPDATE:
11675 return LISTVIEW_Update(infoPtr, (INT)wParam);
11677 case CCM_GETVERSION:
11678 return LISTVIEW_GetVersion(infoPtr);
11680 case CCM_SETVERSION:
11681 return LISTVIEW_SetVersion(infoPtr, wParam);
11683 case WM_CHAR:
11684 return LISTVIEW_ProcessLetterKeys( infoPtr, wParam, lParam );
11686 case WM_COMMAND:
11687 return LISTVIEW_Command(infoPtr, wParam, lParam);
11689 case WM_NCCREATE:
11690 return LISTVIEW_NCCreate(hwnd, wParam, (LPCREATESTRUCTW)lParam);
11692 case WM_CREATE:
11693 return LISTVIEW_Create(hwnd, (LPCREATESTRUCTW)lParam);
11695 case WM_DESTROY:
11696 return LISTVIEW_Destroy(infoPtr);
11698 case WM_ENABLE:
11699 return LISTVIEW_Enable(infoPtr);
11701 case WM_ERASEBKGND:
11702 return LISTVIEW_EraseBkgnd(infoPtr, (HDC)wParam);
11704 case WM_GETDLGCODE:
11705 return DLGC_WANTCHARS | DLGC_WANTARROWS;
11707 case WM_GETFONT:
11708 return (LRESULT)infoPtr->hFont;
11710 case WM_HSCROLL:
11711 return LISTVIEW_HScroll(infoPtr, (INT)LOWORD(wParam), 0);
11713 case WM_KEYDOWN:
11714 return LISTVIEW_KeyDown(infoPtr, (INT)wParam, (LONG)lParam);
11716 case WM_KILLFOCUS:
11717 return LISTVIEW_KillFocus(infoPtr);
11719 case WM_LBUTTONDBLCLK:
11720 return LISTVIEW_LButtonDblClk(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11722 case WM_LBUTTONDOWN:
11723 return LISTVIEW_LButtonDown(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11725 case WM_LBUTTONUP:
11726 return LISTVIEW_LButtonUp(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11728 case WM_MOUSEMOVE:
11729 return LISTVIEW_MouseMove (infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11731 case WM_MOUSEHOVER:
11732 return LISTVIEW_MouseHover(infoPtr, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11734 case WM_NCDESTROY:
11735 return LISTVIEW_NCDestroy(infoPtr);
11737 case WM_NCPAINT:
11738 return LISTVIEW_NCPaint(infoPtr, (HRGN)wParam);
11740 case WM_NOTIFY:
11741 return LISTVIEW_Notify(infoPtr, (LPNMHDR)lParam);
11743 case WM_NOTIFYFORMAT:
11744 return LISTVIEW_NotifyFormat(infoPtr, (HWND)wParam, (INT)lParam);
11746 case WM_PRINTCLIENT:
11747 return LISTVIEW_PrintClient(infoPtr, (HDC)wParam, (DWORD)lParam);
11749 case WM_PAINT:
11750 return LISTVIEW_WMPaint(infoPtr, (HDC)wParam);
11752 case WM_RBUTTONDBLCLK:
11753 return LISTVIEW_RButtonDblClk(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11755 case WM_RBUTTONDOWN:
11756 return LISTVIEW_RButtonDown(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11758 case WM_SETCURSOR:
11759 return LISTVIEW_SetCursor(infoPtr, wParam, lParam);
11761 case WM_SETFOCUS:
11762 return LISTVIEW_SetFocus(infoPtr, (HWND)wParam);
11764 case WM_SETFONT:
11765 return LISTVIEW_SetFont(infoPtr, (HFONT)wParam, (WORD)lParam);
11767 case WM_SETREDRAW:
11768 return LISTVIEW_SetRedraw(infoPtr, (BOOL)wParam);
11770 case WM_SHOWWINDOW:
11771 return LISTVIEW_ShowWindow(infoPtr, wParam, lParam);
11773 case WM_STYLECHANGED:
11774 return LISTVIEW_StyleChanged(infoPtr, wParam, (LPSTYLESTRUCT)lParam);
11776 case WM_STYLECHANGING:
11777 return LISTVIEW_StyleChanging(wParam, (LPSTYLESTRUCT)lParam);
11779 case WM_SYSCOLORCHANGE:
11780 COMCTL32_RefreshSysColors();
11781 return 0;
11783 /* case WM_TIMER: */
11784 case WM_THEMECHANGED:
11785 return LISTVIEW_ThemeChanged(infoPtr);
11787 case WM_VSCROLL:
11788 return LISTVIEW_VScroll(infoPtr, (INT)LOWORD(wParam), 0);
11790 case WM_MOUSEWHEEL:
11791 if (wParam & (MK_SHIFT | MK_CONTROL))
11792 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
11793 return LISTVIEW_MouseWheel(infoPtr, (short int)HIWORD(wParam));
11795 case WM_WINDOWPOSCHANGED:
11796 if (!(((WINDOWPOS *)lParam)->flags & SWP_NOSIZE))
11798 SetWindowPos(infoPtr->hwndSelf, 0, 0, 0, 0, 0, SWP_FRAMECHANGED | SWP_NOACTIVATE |
11799 SWP_NOZORDER | SWP_NOMOVE | SWP_NOSIZE);
11801 if ((infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && (infoPtr->uView == LV_VIEW_DETAILS))
11803 if (notify_measureitem(infoPtr)) LISTVIEW_InvalidateList(infoPtr);
11805 LISTVIEW_Size(infoPtr, ((WINDOWPOS *)lParam)->cx, ((WINDOWPOS *)lParam)->cy);
11807 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
11809 /* case WM_WININICHANGE: */
11811 default:
11812 if ((uMsg >= WM_USER) && (uMsg < WM_APP) && !COMCTL32_IsReflectedMessage(uMsg))
11813 ERR("unknown msg %04x, wp %Ix, lp %Ix\n", uMsg, wParam, lParam);
11815 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
11820 /***
11821 * DESCRIPTION:
11822 * Registers the window class.
11824 * PARAMETER(S):
11825 * None
11827 * RETURN:
11828 * None
11830 void LISTVIEW_Register(void)
11832 WNDCLASSW wndClass;
11834 ZeroMemory(&wndClass, sizeof(WNDCLASSW));
11835 wndClass.style = CS_GLOBALCLASS | CS_DBLCLKS;
11836 wndClass.lpfnWndProc = LISTVIEW_WindowProc;
11837 wndClass.cbClsExtra = 0;
11838 wndClass.cbWndExtra = sizeof(LISTVIEW_INFO *);
11839 wndClass.hCursor = LoadCursorW(0, (LPWSTR)IDC_ARROW);
11840 wndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
11841 wndClass.lpszClassName = WC_LISTVIEWW;
11842 RegisterClassW(&wndClass);
11845 /***
11846 * DESCRIPTION:
11847 * Unregisters the window class.
11849 * PARAMETER(S):
11850 * None
11852 * RETURN:
11853 * None
11855 void LISTVIEW_Unregister(void)
11857 UnregisterClassW(WC_LISTVIEWW, NULL);
11860 /***
11861 * DESCRIPTION:
11862 * Handle any WM_COMMAND messages
11864 * PARAMETER(S):
11865 * [I] infoPtr : valid pointer to the listview structure
11866 * [I] wParam : the first message parameter
11867 * [I] lParam : the second message parameter
11869 * RETURN:
11870 * Zero.
11872 static LRESULT LISTVIEW_Command(LISTVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
11875 TRACE("%p, %x, %x, %Ix\n", infoPtr, HIWORD(wParam), LOWORD(wParam), lParam);
11877 if (!infoPtr->hwndEdit) return 0;
11879 switch (HIWORD(wParam))
11881 case EN_UPDATE:
11884 * Adjust the edit window size
11886 WCHAR buffer[1024];
11887 HDC hdc = GetDC(infoPtr->hwndEdit);
11888 HFONT hFont, hOldFont = 0;
11889 RECT rect;
11890 SIZE sz;
11892 if (!infoPtr->hwndEdit || !hdc) return 0;
11893 GetWindowTextW(infoPtr->hwndEdit, buffer, ARRAY_SIZE(buffer));
11894 GetWindowRect(infoPtr->hwndEdit, &rect);
11896 /* Select font to get the right dimension of the string */
11897 hFont = (HFONT)SendMessageW(infoPtr->hwndEdit, WM_GETFONT, 0, 0);
11898 if (hFont)
11900 hOldFont = SelectObject(hdc, hFont);
11903 if (GetTextExtentPoint32W(hdc, buffer, lstrlenW(buffer), &sz))
11905 TEXTMETRICW textMetric;
11907 /* Add Extra spacing for the next character */
11908 GetTextMetricsW(hdc, &textMetric);
11909 sz.cx += (textMetric.tmMaxCharWidth * 2);
11911 SetWindowPos(infoPtr->hwndEdit, NULL, 0, 0, sz.cx,
11912 rect.bottom - rect.top, SWP_DRAWFRAME | SWP_NOMOVE | SWP_NOZORDER);
11914 if (hFont)
11915 SelectObject(hdc, hOldFont);
11917 ReleaseDC(infoPtr->hwndEdit, hdc);
11919 break;
11921 case EN_KILLFOCUS:
11923 if (infoPtr->notify_mask & NOTIFY_MASK_END_LABEL_EDIT)
11924 LISTVIEW_CancelEditLabel(infoPtr);
11925 break;
11928 default:
11929 return SendMessageW (infoPtr->hwndNotify, WM_COMMAND, wParam, lParam);
11932 return 0;