[COMCTL32]
[reactos.git] / reactos / dll / win32 / comctl32 / listview.c
1 /*
2 * Listview control
3 *
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-2013 Nikolay Sivov
10 * Copyright 2009 Owen Rudge for CodeWeavers
11 * Copyright 2012-2013 Daniel Jelinski
12 *
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.
17 *
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.
22 *
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
26 *
27 * NOTES
28 *
29 * This code was audited for completeness against the documented features
30 * of Comctl32.dll version 6.0 on May. 20, 2005, by James Hawkins.
31 *
32 * Unless otherwise noted, we believe this code to be complete, as per
33 * the specification mentioned above.
34 * If you discover missing features, or bugs, please note them below.
35 *
36 * TODO:
37 *
38 * Default Message Processing
39 * -- WM_CREATE: create the icon and small icon image lists at this point only if
40 * the LVS_SHAREIMAGELISTS style is not specified.
41 * -- WM_WINDOWPOSCHANGED: arrange the list items if the current view is icon
42 * or small icon and the LVS_AUTOARRANGE style is specified.
43 * -- WM_TIMER
44 * -- WM_WININICHANGE
45 *
46 * Features
47 * -- Hot item handling, mouse hovering
48 * -- Workareas support
49 * -- Tilemode support
50 * -- Groups support
51 *
52 * Bugs
53 * -- Expand large item in ICON mode when the cursor is flying over the icon or text.
54 * -- Support CustomDraw options for _WIN32_IE >= 0x560 (see NMLVCUSTOMDRAW docs).
55 * -- LVA_SNAPTOGRID not implemented
56 * -- LISTVIEW_ApproximateViewRect partially implemented
57 * -- LISTVIEW_SetColumnWidth ignores header images & bitmap
58 * -- LISTVIEW_StyleChanged doesn't handle some changes too well
59 *
60 * Speedups
61 * -- LISTVIEW_GetNextItem needs to be rewritten. It is currently
62 * linear in the number of items in the list, and this is
63 * unacceptable for large lists.
64 * -- if list is sorted by item text LISTVIEW_InsertItemT could use
65 * binary search to calculate item index (e.g. DPA_Search()).
66 * This requires sorted state to be reliably tracked in item modifiers.
67 * -- we should keep an ordered array of coordinates in iconic mode
68 * this would allow to frame items (iterator_frameditems),
69 * and find nearest item (LVFI_NEARESTXY) a lot more efficiently
70 *
71 * Flags
72 * -- LVIF_COLUMNS
73 * -- LVIF_GROUPID
74 *
75 * States
76 * -- LVIS_ACTIVATING (not currently supported by comctl32.dll version 6.0)
77 * -- LVIS_DROPHILITED
78 *
79 * Styles
80 * -- LVS_NOLABELWRAP
81 * -- LVS_NOSCROLL (see Q137520)
82 * -- LVS_ALIGNTOP
83 *
84 * Extended Styles
85 * -- LVS_EX_BORDERSELECT
86 * -- LVS_EX_FLATSB
87 * -- LVS_EX_INFOTIP
88 * -- LVS_EX_LABELTIP
89 * -- LVS_EX_MULTIWORKAREAS
90 * -- LVS_EX_REGIONAL
91 * -- LVS_EX_SIMPLESELECT
92 * -- LVS_EX_TWOCLICKACTIVATE
93 * -- LVS_EX_UNDERLINECOLD
94 * -- LVS_EX_UNDERLINEHOT
95 *
96 * Notifications:
97 * -- LVN_BEGINSCROLL, LVN_ENDSCROLL
98 * -- LVN_GETINFOTIP
99 * -- LVN_HOTTRACK
100 * -- LVN_SETDISPINFO
101 *
102 * Messages:
103 * -- LVM_ENABLEGROUPVIEW
104 * -- LVM_GETBKIMAGE, LVM_SETBKIMAGE
105 * -- LVM_GETGROUPINFO, LVM_SETGROUPINFO
106 * -- LVM_GETGROUPMETRICS, LVM_SETGROUPMETRICS
107 * -- LVM_GETINSERTMARK, LVM_SETINSERTMARK
108 * -- LVM_GETINSERTMARKCOLOR, LVM_SETINSERTMARKCOLOR
109 * -- LVM_GETINSERTMARKRECT
110 * -- LVM_GETNUMBEROFWORKAREAS
111 * -- LVM_GETOUTLINECOLOR, LVM_SETOUTLINECOLOR
112 * -- LVM_GETSELECTEDCOLUMN, LVM_SETSELECTEDCOLUMN
113 * -- LVM_GETISEARCHSTRINGW, LVM_GETISEARCHSTRINGA
114 * -- LVM_GETTILEINFO, LVM_SETTILEINFO
115 * -- LVM_GETTILEVIEWINFO, LVM_SETTILEVIEWINFO
116 * -- LVM_GETWORKAREAS, LVM_SETWORKAREAS
117 * -- LVM_HASGROUP, LVM_INSERTGROUP, LVM_REMOVEGROUP, LVM_REMOVEALLGROUPS
118 * -- LVM_INSERTGROUPSORTED
119 * -- LVM_INSERTMARKHITTEST
120 * -- LVM_ISGROUPVIEWENABLED
121 * -- LVM_MOVEGROUP
122 * -- LVM_MOVEITEMTOGROUP
123 * -- LVM_SETINFOTIP
124 * -- LVM_SETTILEWIDTH
125 * -- LVM_SORTGROUPS
126 *
127 * Macros:
128 * -- ListView_GetHoverTime, ListView_SetHoverTime
129 * -- ListView_GetISearchString
130 * -- ListView_GetNumberOfWorkAreas
131 * -- ListView_GetWorkAreas, ListView_SetWorkAreas
132 *
133 * Functions:
134 * -- LVGroupComparE
135 */
136
137 #include <stdio.h>
138
139 #include "comctl32.h"
140
141 WINE_DEFAULT_DEBUG_CHANNEL(listview);
142
143 typedef struct tagCOLUMN_INFO
144 {
145 RECT rcHeader; /* tracks the header's rectangle */
146 INT fmt; /* same as LVCOLUMN.fmt */
147 INT cxMin;
148 } COLUMN_INFO;
149
150 typedef struct tagITEMHDR
151 {
152 LPWSTR pszText;
153 INT iImage;
154 } ITEMHDR, *LPITEMHDR;
155
156 typedef struct tagSUBITEM_INFO
157 {
158 ITEMHDR hdr;
159 INT iSubItem;
160 } SUBITEM_INFO;
161
162 typedef struct tagITEM_ID ITEM_ID;
163
164 typedef struct tagITEM_INFO
165 {
166 ITEMHDR hdr;
167 UINT state;
168 LPARAM lParam;
169 INT iIndent;
170 ITEM_ID *id;
171 } ITEM_INFO;
172
173 struct tagITEM_ID
174 {
175 UINT id; /* item id */
176 HDPA item; /* link to item data */
177 };
178
179 typedef struct tagRANGE
180 {
181 INT lower;
182 INT upper;
183 } RANGE;
184
185 typedef struct tagRANGES
186 {
187 HDPA hdpa;
188 } *RANGES;
189
190 typedef struct tagITERATOR
191 {
192 INT nItem;
193 INT nSpecial;
194 RANGE range;
195 RANGES ranges;
196 INT index;
197 } ITERATOR;
198
199 typedef struct tagDELAYED_ITEM_EDIT
200 {
201 BOOL fEnabled;
202 INT iItem;
203 } DELAYED_ITEM_EDIT;
204
205 typedef struct tagLISTVIEW_INFO
206 {
207 /* control window */
208 HWND hwndSelf;
209 RECT rcList; /* This rectangle is really the window
210 * client rectangle possibly reduced by the
211 * horizontal scroll bar and/or header - see
212 * LISTVIEW_UpdateSize. This rectangle offset
213 * by the LISTVIEW_GetOrigin value is in
214 * client coordinates */
215
216 /* notification window */
217 SHORT notifyFormat;
218 HWND hwndNotify;
219 BOOL bDoChangeNotify; /* send change notification messages? */
220 UINT uCallbackMask;
221
222 /* tooltips */
223 HWND hwndToolTip;
224
225 /* items */
226 INT nItemCount; /* the number of items in the list */
227 HDPA hdpaItems; /* array ITEM_INFO pointers */
228 HDPA hdpaItemIds; /* array of ITEM_ID pointers */
229 HDPA hdpaPosX; /* maintains the (X, Y) coordinates of the */
230 HDPA hdpaPosY; /* items in LVS_ICON, and LVS_SMALLICON modes */
231 RANGES selectionRanges;
232 INT nSelectionMark; /* item to start next multiselection from */
233 INT nHotItem;
234 BOOL bAutoarrange; /* Autoarrange flag when NOT in LVS_AUTOARRANGE */
235
236 /* columns */
237 HDPA hdpaColumns; /* array of COLUMN_INFO pointers */
238 BOOL colRectsDirty; /* trigger column rectangles requery from header */
239
240 /* item metrics */
241 BOOL bNoItemMetrics; /* flags if item metrics are not yet computed */
242 INT nItemHeight;
243 INT nItemWidth;
244
245 /* sorting */
246 PFNLVCOMPARE pfnCompare; /* sorting callback pointer */
247 LPARAM lParamSort;
248
249 /* style */
250 DWORD dwStyle; /* the cached window GWL_STYLE */
251 DWORD dwLvExStyle; /* extended listview style */
252 DWORD uView; /* current view available through LVM_[G,S]ETVIEW */
253
254 /* edit item */
255 HWND hwndEdit;
256 WNDPROC EditWndProc;
257 INT nEditLabelItem;
258 DELAYED_ITEM_EDIT itemEdit; /* Pointer to this structure will be the timer ID */
259
260 /* icons */
261 HIMAGELIST himlNormal;
262 HIMAGELIST himlSmall;
263 HIMAGELIST himlState;
264 SIZE iconSize;
265 BOOL autoSpacing;
266 SIZE iconSpacing;
267 SIZE iconStateSize;
268 POINT currIconPos; /* this is the position next icon will be placed */
269
270 /* header */
271 HWND hwndHeader;
272 INT xTrackLine; /* The x coefficient of the track line or -1 if none */
273
274 /* marquee selection */
275 BOOL bMarqueeSelect; /* marquee selection/highlight underway */
276 BOOL bScrolling;
277 RECT marqueeRect; /* absolute coordinates of marquee selection */
278 RECT marqueeDrawRect; /* relative coordinates for drawing marquee */
279 POINT marqueeOrigin; /* absolute coordinates of marquee click origin */
280
281 /* focus drawing */
282 BOOL bFocus; /* control has focus */
283 INT nFocusedItem;
284 RECT rcFocus; /* focus bounds */
285
286 /* colors */
287 HBRUSH hBkBrush;
288 COLORREF clrBk;
289 COLORREF clrText;
290 COLORREF clrTextBk;
291 BOOL bDefaultBkColor;
292
293 /* font */
294 HFONT hDefaultFont;
295 HFONT hFont;
296 INT ntmHeight; /* Some cached metrics of the font used */
297 INT ntmMaxCharWidth; /* by the listview to draw items */
298 INT nEllipsisWidth;
299
300 /* mouse operation */
301 BOOL bLButtonDown;
302 BOOL bDragging;
303 POINT ptClickPos; /* point where the user clicked */
304 INT nLButtonDownItem; /* tracks item to reset multiselection on WM_LBUTTONUP */
305 DWORD dwHoverTime;
306 HCURSOR hHotCursor;
307 INT cWheelRemainder;
308
309 /* keyboard operation */
310 DWORD lastKeyPressTimestamp;
311 WPARAM charCode;
312 INT nSearchParamLength;
313 WCHAR szSearchParam[ MAX_PATH ];
314
315 /* painting */
316 DWORD cditemmode; /* Keep the custom draw flags for an item/row */
317 BOOL bIsDrawing; /* Drawing in progress */
318 INT nMeasureItemHeight; /* WM_MEASUREITEM result */
319 BOOL bRedraw; /* WM_SETREDRAW switch */
320
321 /* misc */
322 DWORD iVersion; /* CCM_[G,S]ETVERSION */
323 } LISTVIEW_INFO;
324
325 /*
326 * constants
327 */
328 /* How many we debug buffer to allocate */
329 #define DEBUG_BUFFERS 20
330 /* The size of a single debug buffer */
331 #define DEBUG_BUFFER_SIZE 256
332
333 /* Internal interface to LISTVIEW_HScroll and LISTVIEW_VScroll */
334 #define SB_INTERNAL -1
335
336 /* maximum size of a label */
337 #define DISP_TEXT_SIZE 260
338
339 /* padding for items in list and small icon display modes */
340 #define WIDTH_PADDING 12
341
342 /* padding for items in list, report and small icon display modes */
343 #define HEIGHT_PADDING 1
344
345 /* offset of items in report display mode */
346 #define REPORT_MARGINX 2
347
348 /* padding for icon in large icon display mode
349 * ICON_TOP_PADDING_NOTHITABLE - space between top of box and area
350 * that HITTEST will see.
351 * ICON_TOP_PADDING_HITABLE - spacing between above and icon.
352 * ICON_TOP_PADDING - sum of the two above.
353 * ICON_BOTTOM_PADDING - between bottom of icon and top of text
354 * LABEL_HOR_PADDING - between text and sides of box
355 * LABEL_VERT_PADDING - between bottom of text and end of box
356 *
357 * ICON_LR_PADDING - additional width above icon size.
358 * ICON_LR_HALF - half of the above value
359 */
360 #define ICON_TOP_PADDING_NOTHITABLE 2
361 #define ICON_TOP_PADDING_HITABLE 2
362 #define ICON_TOP_PADDING (ICON_TOP_PADDING_NOTHITABLE + ICON_TOP_PADDING_HITABLE)
363 #define ICON_BOTTOM_PADDING 4
364 #define LABEL_HOR_PADDING 5
365 #define LABEL_VERT_PADDING 7
366 #define ICON_LR_PADDING 16
367 #define ICON_LR_HALF (ICON_LR_PADDING/2)
368
369 /* default label width for items in list and small icon display modes */
370 #define DEFAULT_LABEL_WIDTH 40
371 /* maximum select rectangle width for empty text item in LV_VIEW_DETAILS */
372 #define MAX_EMPTYTEXT_SELECT_WIDTH 80
373
374 /* default column width for items in list display mode */
375 #define DEFAULT_COLUMN_WIDTH 128
376
377 /* Size of "line" scroll for V & H scrolls */
378 #define LISTVIEW_SCROLL_ICON_LINE_SIZE 37
379
380 /* Padding between image and label */
381 #define IMAGE_PADDING 2
382
383 /* Padding behind the label */
384 #define TRAILING_LABEL_PADDING 12
385 #define TRAILING_HEADER_PADDING 11
386
387 /* Border for the icon caption */
388 #define CAPTION_BORDER 2
389
390 /* Standard DrawText flags */
391 #define LV_ML_DT_FLAGS (DT_TOP | DT_NOPREFIX | DT_EDITCONTROL | DT_CENTER | DT_WORDBREAK | DT_WORD_ELLIPSIS | DT_END_ELLIPSIS)
392 #define LV_FL_DT_FLAGS (DT_TOP | DT_NOPREFIX | DT_EDITCONTROL | DT_CENTER | DT_WORDBREAK | DT_NOCLIP)
393 #define LV_SL_DT_FLAGS (DT_VCENTER | DT_NOPREFIX | DT_EDITCONTROL | DT_SINGLELINE | DT_WORD_ELLIPSIS | DT_END_ELLIPSIS)
394
395 /* Image index from state */
396 #define STATEIMAGEINDEX(x) (((x) & LVIS_STATEIMAGEMASK) >> 12)
397
398 /* The time in milliseconds to reset the search in the list */
399 #define KEY_DELAY 450
400
401 /* Dump the LISTVIEW_INFO structure to the debug channel */
402 #define LISTVIEW_DUMP(iP) do { \
403 TRACE("hwndSelf=%p, clrBk=0x%06x, clrText=0x%06x, clrTextBk=0x%06x, ItemHeight=%d, ItemWidth=%d, Style=0x%08x\n", \
404 iP->hwndSelf, iP->clrBk, iP->clrText, iP->clrTextBk, \
405 iP->nItemHeight, iP->nItemWidth, iP->dwStyle); \
406 TRACE("hwndSelf=%p, himlNor=%p, himlSml=%p, himlState=%p, Focused=%d, Hot=%d, exStyle=0x%08x, Focus=%d\n", \
407 iP->hwndSelf, iP->himlNormal, iP->himlSmall, iP->himlState, \
408 iP->nFocusedItem, iP->nHotItem, iP->dwLvExStyle, iP->bFocus ); \
409 TRACE("hwndSelf=%p, ntmH=%d, icSz.cx=%d, icSz.cy=%d, icSp.cx=%d, icSp.cy=%d, notifyFmt=%d\n", \
410 iP->hwndSelf, iP->ntmHeight, iP->iconSize.cx, iP->iconSize.cy, \
411 iP->iconSpacing.cx, iP->iconSpacing.cy, iP->notifyFormat); \
412 TRACE("hwndSelf=%p, rcList=%s\n", iP->hwndSelf, wine_dbgstr_rect(&iP->rcList)); \
413 } while(0)
414
415 static const WCHAR themeClass[] = {'L','i','s','t','V','i','e','w',0};
416
417 /*
418 * forward declarations
419 */
420 static BOOL LISTVIEW_GetItemT(const LISTVIEW_INFO *, LPLVITEMW, BOOL);
421 static void LISTVIEW_GetItemBox(const LISTVIEW_INFO *, INT, LPRECT);
422 static void LISTVIEW_GetItemOrigin(const LISTVIEW_INFO *, INT, LPPOINT);
423 static BOOL LISTVIEW_GetItemPosition(const LISTVIEW_INFO *, INT, LPPOINT);
424 static BOOL LISTVIEW_GetItemRect(const LISTVIEW_INFO *, INT, LPRECT);
425 static void LISTVIEW_GetOrigin(const LISTVIEW_INFO *, LPPOINT);
426 static BOOL LISTVIEW_GetViewRect(const LISTVIEW_INFO *, LPRECT);
427 static void LISTVIEW_UpdateSize(LISTVIEW_INFO *);
428 static LRESULT LISTVIEW_Command(LISTVIEW_INFO *, WPARAM, LPARAM);
429 static INT LISTVIEW_GetStringWidthT(const LISTVIEW_INFO *, LPCWSTR, BOOL);
430 static BOOL LISTVIEW_KeySelection(LISTVIEW_INFO *, INT, BOOL);
431 static UINT LISTVIEW_GetItemState(const LISTVIEW_INFO *, INT, UINT);
432 static BOOL LISTVIEW_SetItemState(LISTVIEW_INFO *, INT, const LVITEMW *);
433 static LRESULT LISTVIEW_VScroll(LISTVIEW_INFO *, INT, INT);
434 static LRESULT LISTVIEW_HScroll(LISTVIEW_INFO *, INT, INT);
435 static BOOL LISTVIEW_EnsureVisible(LISTVIEW_INFO *, INT, BOOL);
436 static HIMAGELIST LISTVIEW_SetImageList(LISTVIEW_INFO *, INT, HIMAGELIST);
437 static INT LISTVIEW_HitTest(const LISTVIEW_INFO *, LPLVHITTESTINFO, BOOL, BOOL);
438 static BOOL LISTVIEW_EndEditLabelT(LISTVIEW_INFO *, BOOL, BOOL);
439 static BOOL LISTVIEW_Scroll(LISTVIEW_INFO *, INT, INT);
440
441 /******** Text handling functions *************************************/
442
443 /* A text pointer is either NULL, LPSTR_TEXTCALLBACK, or points to a
444 * text string. The string may be ANSI or Unicode, in which case
445 * the boolean isW tells us the type of the string.
446 *
447 * The name of the function tell what type of strings it expects:
448 * W: Unicode, T: ANSI/Unicode - function of isW
449 */
450
451 static inline BOOL is_text(LPCWSTR text)
452 {
453 return text != NULL && text != LPSTR_TEXTCALLBACKW;
454 }
455
456 static inline int textlenT(LPCWSTR text, BOOL isW)
457 {
458 return !is_text(text) ? 0 :
459 isW ? lstrlenW(text) : lstrlenA((LPCSTR)text);
460 }
461
462 static inline void textcpynT(LPWSTR dest, BOOL isDestW, LPCWSTR src, BOOL isSrcW, INT max)
463 {
464 if (isDestW)
465 if (isSrcW) lstrcpynW(dest, src, max);
466 else MultiByteToWideChar(CP_ACP, 0, (LPCSTR)src, -1, dest, max);
467 else
468 if (isSrcW) WideCharToMultiByte(CP_ACP, 0, src, -1, (LPSTR)dest, max, NULL, NULL);
469 else lstrcpynA((LPSTR)dest, (LPCSTR)src, max);
470 }
471
472 static inline LPWSTR textdupTtoW(LPCWSTR text, BOOL isW)
473 {
474 LPWSTR wstr = (LPWSTR)text;
475
476 if (!isW && is_text(text))
477 {
478 INT len = MultiByteToWideChar(CP_ACP, 0, (LPCSTR)text, -1, NULL, 0);
479 wstr = Alloc(len * sizeof(WCHAR));
480 if (wstr) MultiByteToWideChar(CP_ACP, 0, (LPCSTR)text, -1, wstr, len);
481 }
482 TRACE(" wstr=%s\n", text == LPSTR_TEXTCALLBACKW ? "(callback)" : debugstr_w(wstr));
483 return wstr;
484 }
485
486 static inline void textfreeT(LPWSTR wstr, BOOL isW)
487 {
488 if (!isW && is_text(wstr)) Free (wstr);
489 }
490
491 /*
492 * dest is a pointer to a Unicode string
493 * src is a pointer to a string (Unicode if isW, ANSI if !isW)
494 */
495 static BOOL textsetptrT(LPWSTR *dest, LPCWSTR src, BOOL isW)
496 {
497 BOOL bResult = TRUE;
498
499 if (src == LPSTR_TEXTCALLBACKW)
500 {
501 if (is_text(*dest)) Free(*dest);
502 *dest = LPSTR_TEXTCALLBACKW;
503 }
504 else
505 {
506 LPWSTR pszText = textdupTtoW(src, isW);
507 if (*dest == LPSTR_TEXTCALLBACKW) *dest = NULL;
508 bResult = Str_SetPtrW(dest, pszText);
509 textfreeT(pszText, isW);
510 }
511 return bResult;
512 }
513
514 /*
515 * compares a Unicode to a Unicode/ANSI text string
516 */
517 static inline int textcmpWT(LPCWSTR aw, LPCWSTR bt, BOOL isW)
518 {
519 if (!aw) return bt ? -1 : 0;
520 if (!bt) return 1;
521 if (aw == LPSTR_TEXTCALLBACKW)
522 return bt == LPSTR_TEXTCALLBACKW ? 1 : -1;
523 if (bt != LPSTR_TEXTCALLBACKW)
524 {
525 LPWSTR bw = textdupTtoW(bt, isW);
526 int r = bw ? lstrcmpW(aw, bw) : 1;
527 textfreeT(bw, isW);
528 return r;
529 }
530
531 return 1;
532 }
533
534 static inline int lstrncmpiW(LPCWSTR s1, LPCWSTR s2, int n)
535 {
536 n = min(min(n, lstrlenW(s1)), lstrlenW(s2));
537 return CompareStringW(LOCALE_USER_DEFAULT, NORM_IGNORECASE, s1, n, s2, n) - CSTR_EQUAL;
538 }
539
540 /******** Debugging functions *****************************************/
541
542 static inline LPCSTR debugtext_t(LPCWSTR text, BOOL isW)
543 {
544 if (text == LPSTR_TEXTCALLBACKW) return "(callback)";
545 return isW ? debugstr_w(text) : debugstr_a((LPCSTR)text);
546 }
547
548 static inline LPCSTR debugtext_tn(LPCWSTR text, BOOL isW, INT n)
549 {
550 if (text == LPSTR_TEXTCALLBACKW) return "(callback)";
551 n = min(textlenT(text, isW), n);
552 return isW ? debugstr_wn(text, n) : debugstr_an((LPCSTR)text, n);
553 }
554
555 static char* debug_getbuf(void)
556 {
557 static int index = 0;
558 static char buffers[DEBUG_BUFFERS][DEBUG_BUFFER_SIZE];
559 return buffers[index++ % DEBUG_BUFFERS];
560 }
561
562 static inline const char* debugrange(const RANGE *lprng)
563 {
564 if (!lprng) return "(null)";
565 return wine_dbg_sprintf("[%d, %d]", lprng->lower, lprng->upper);
566 }
567
568 static const char* debugscrollinfo(const SCROLLINFO *pScrollInfo)
569 {
570 char* buf = debug_getbuf(), *text = buf;
571 int len, size = DEBUG_BUFFER_SIZE;
572
573 if (pScrollInfo == NULL) return "(null)";
574 len = snprintf(buf, size, "{cbSize=%d, ", pScrollInfo->cbSize);
575 if (len == -1) goto end; buf += len; size -= len;
576 if (pScrollInfo->fMask & SIF_RANGE)
577 len = snprintf(buf, size, "nMin=%d, nMax=%d, ", pScrollInfo->nMin, pScrollInfo->nMax);
578 else len = 0;
579 if (len == -1) goto end; buf += len; size -= len;
580 if (pScrollInfo->fMask & SIF_PAGE)
581 len = snprintf(buf, size, "nPage=%u, ", pScrollInfo->nPage);
582 else len = 0;
583 if (len == -1) goto end; buf += len; size -= len;
584 if (pScrollInfo->fMask & SIF_POS)
585 len = snprintf(buf, size, "nPos=%d, ", pScrollInfo->nPos);
586 else len = 0;
587 if (len == -1) goto end; buf += len; size -= len;
588 if (pScrollInfo->fMask & SIF_TRACKPOS)
589 len = snprintf(buf, size, "nTrackPos=%d, ", pScrollInfo->nTrackPos);
590 else len = 0;
591 if (len == -1) goto end; buf += len;
592 goto undo;
593 end:
594 buf = text + strlen(text);
595 undo:
596 if (buf - text > 2) { buf[-2] = '}'; buf[-1] = 0; }
597 return text;
598 }
599
600 static const char* debugnmlistview(const NMLISTVIEW *plvnm)
601 {
602 if (!plvnm) return "(null)";
603 return wine_dbg_sprintf("iItem=%d, iSubItem=%d, uNewState=0x%x,"
604 " uOldState=0x%x, uChanged=0x%x, ptAction=%s, lParam=%ld",
605 plvnm->iItem, plvnm->iSubItem, plvnm->uNewState, plvnm->uOldState,
606 plvnm->uChanged, wine_dbgstr_point(&plvnm->ptAction), plvnm->lParam);
607 }
608
609 static const char* debuglvitem_t(const LVITEMW *lpLVItem, BOOL isW)
610 {
611 char* buf = debug_getbuf(), *text = buf;
612 int len, size = DEBUG_BUFFER_SIZE;
613
614 if (lpLVItem == NULL) return "(null)";
615 len = snprintf(buf, size, "{iItem=%d, iSubItem=%d, ", lpLVItem->iItem, lpLVItem->iSubItem);
616 if (len == -1) goto end; buf += len; size -= len;
617 if (lpLVItem->mask & LVIF_STATE)
618 len = snprintf(buf, size, "state=%x, stateMask=%x, ", lpLVItem->state, lpLVItem->stateMask);
619 else len = 0;
620 if (len == -1) goto end; buf += len; size -= len;
621 if (lpLVItem->mask & LVIF_TEXT)
622 len = snprintf(buf, size, "pszText=%s, cchTextMax=%d, ", debugtext_tn(lpLVItem->pszText, isW, 80), lpLVItem->cchTextMax);
623 else len = 0;
624 if (len == -1) goto end; buf += len; size -= len;
625 if (lpLVItem->mask & LVIF_IMAGE)
626 len = snprintf(buf, size, "iImage=%d, ", lpLVItem->iImage);
627 else len = 0;
628 if (len == -1) goto end; buf += len; size -= len;
629 if (lpLVItem->mask & LVIF_PARAM)
630 len = snprintf(buf, size, "lParam=%lx, ", lpLVItem->lParam);
631 else len = 0;
632 if (len == -1) goto end; buf += len; size -= len;
633 if (lpLVItem->mask & LVIF_INDENT)
634 len = snprintf(buf, size, "iIndent=%d, ", lpLVItem->iIndent);
635 else len = 0;
636 if (len == -1) goto end; buf += len;
637 goto undo;
638 end:
639 buf = text + strlen(text);
640 undo:
641 if (buf - text > 2) { buf[-2] = '}'; buf[-1] = 0; }
642 return text;
643 }
644
645 static const char* debuglvcolumn_t(const LVCOLUMNW *lpColumn, BOOL isW)
646 {
647 char* buf = debug_getbuf(), *text = buf;
648 int len, size = DEBUG_BUFFER_SIZE;
649
650 if (lpColumn == NULL) return "(null)";
651 len = snprintf(buf, size, "{");
652 if (len == -1) goto end; buf += len; size -= len;
653 if (lpColumn->mask & LVCF_SUBITEM)
654 len = snprintf(buf, size, "iSubItem=%d, ", lpColumn->iSubItem);
655 else len = 0;
656 if (len == -1) goto end; buf += len; size -= len;
657 if (lpColumn->mask & LVCF_FMT)
658 len = snprintf(buf, size, "fmt=%x, ", lpColumn->fmt);
659 else len = 0;
660 if (len == -1) goto end; buf += len; size -= len;
661 if (lpColumn->mask & LVCF_WIDTH)
662 len = snprintf(buf, size, "cx=%d, ", lpColumn->cx);
663 else len = 0;
664 if (len == -1) goto end; buf += len; size -= len;
665 if (lpColumn->mask & LVCF_TEXT)
666 len = snprintf(buf, size, "pszText=%s, cchTextMax=%d, ", debugtext_tn(lpColumn->pszText, isW, 80), lpColumn->cchTextMax);
667 else len = 0;
668 if (len == -1) goto end; buf += len; size -= len;
669 if (lpColumn->mask & LVCF_IMAGE)
670 len = snprintf(buf, size, "iImage=%d, ", lpColumn->iImage);
671 else len = 0;
672 if (len == -1) goto end; buf += len; size -= len;
673 if (lpColumn->mask & LVCF_ORDER)
674 len = snprintf(buf, size, "iOrder=%d, ", lpColumn->iOrder);
675 else len = 0;
676 if (len == -1) goto end; buf += len;
677 goto undo;
678 end:
679 buf = text + strlen(text);
680 undo:
681 if (buf - text > 2) { buf[-2] = '}'; buf[-1] = 0; }
682 return text;
683 }
684
685 static const char* debuglvhittestinfo(const LVHITTESTINFO *lpht)
686 {
687 if (!lpht) return "(null)";
688
689 return wine_dbg_sprintf("{pt=%s, flags=0x%x, iItem=%d, iSubItem=%d}",
690 wine_dbgstr_point(&lpht->pt), lpht->flags, lpht->iItem, lpht->iSubItem);
691 }
692
693 /* Return the corresponding text for a given scroll value */
694 static inline LPCSTR debugscrollcode(int nScrollCode)
695 {
696 switch(nScrollCode)
697 {
698 case SB_LINELEFT: return "SB_LINELEFT";
699 case SB_LINERIGHT: return "SB_LINERIGHT";
700 case SB_PAGELEFT: return "SB_PAGELEFT";
701 case SB_PAGERIGHT: return "SB_PAGERIGHT";
702 case SB_THUMBPOSITION: return "SB_THUMBPOSITION";
703 case SB_THUMBTRACK: return "SB_THUMBTRACK";
704 case SB_ENDSCROLL: return "SB_ENDSCROLL";
705 case SB_INTERNAL: return "SB_INTERNAL";
706 default: return "unknown";
707 }
708 }
709
710
711 /******** Notification functions ************************************/
712
713 static int get_ansi_notification(UINT unicodeNotificationCode)
714 {
715 switch (unicodeNotificationCode)
716 {
717 case LVN_BEGINLABELEDITA:
718 case LVN_BEGINLABELEDITW: return LVN_BEGINLABELEDITA;
719 case LVN_ENDLABELEDITA:
720 case LVN_ENDLABELEDITW: return LVN_ENDLABELEDITA;
721 case LVN_GETDISPINFOA:
722 case LVN_GETDISPINFOW: return LVN_GETDISPINFOA;
723 case LVN_SETDISPINFOA:
724 case LVN_SETDISPINFOW: return LVN_SETDISPINFOA;
725 case LVN_ODFINDITEMA:
726 case LVN_ODFINDITEMW: return LVN_ODFINDITEMA;
727 case LVN_GETINFOTIPA:
728 case LVN_GETINFOTIPW: return LVN_GETINFOTIPA;
729 /* header forwards */
730 case HDN_TRACKA:
731 case HDN_TRACKW: return HDN_TRACKA;
732 case HDN_ENDTRACKA:
733 case HDN_ENDTRACKW: return HDN_ENDTRACKA;
734 case HDN_BEGINDRAG: return HDN_BEGINDRAG;
735 case HDN_ENDDRAG: return HDN_ENDDRAG;
736 case HDN_ITEMCHANGINGA:
737 case HDN_ITEMCHANGINGW: return HDN_ITEMCHANGINGA;
738 case HDN_ITEMCHANGEDA:
739 case HDN_ITEMCHANGEDW: return HDN_ITEMCHANGEDA;
740 case HDN_ITEMCLICKA:
741 case HDN_ITEMCLICKW: return HDN_ITEMCLICKA;
742 case HDN_DIVIDERDBLCLICKA:
743 case HDN_DIVIDERDBLCLICKW: return HDN_DIVIDERDBLCLICKA;
744 default: break;
745 }
746 FIXME("unknown notification %x\n", unicodeNotificationCode);
747 return unicodeNotificationCode;
748 }
749
750 /* forwards header notifications to listview parent */
751 static LRESULT notify_forward_header(const LISTVIEW_INFO *infoPtr, NMHEADERW *lpnmhW)
752 {
753 LPCWSTR text = NULL, filter = NULL;
754 LRESULT ret;
755 NMHEADERA *lpnmh = (NMHEADERA*) lpnmhW;
756
757 /* on unicode format exit earlier */
758 if (infoPtr->notifyFormat == NFR_UNICODE)
759 return SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, lpnmh->hdr.idFrom,
760 (LPARAM)lpnmh);
761
762 /* header always supplies unicode notifications,
763 all we have to do is to convert strings to ANSI */
764 if (lpnmh->pitem)
765 {
766 /* convert item text */
767 if (lpnmh->pitem->mask & HDI_TEXT)
768 {
769 text = (LPCWSTR)lpnmh->pitem->pszText;
770 Str_SetPtrWtoA(&lpnmh->pitem->pszText, text);
771 }
772 /* convert filter text */
773 if ((lpnmh->pitem->mask & HDI_FILTER) && (lpnmh->pitem->type == HDFT_ISSTRING) &&
774 lpnmh->pitem->pvFilter)
775 {
776 filter = (LPCWSTR)((HD_TEXTFILTERA*)lpnmh->pitem->pvFilter)->pszText;
777 Str_SetPtrWtoA(&((HD_TEXTFILTERA*)lpnmh->pitem->pvFilter)->pszText, filter);
778 }
779 }
780 lpnmh->hdr.code = get_ansi_notification(lpnmh->hdr.code);
781
782 ret = SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, lpnmh->hdr.idFrom,
783 (LPARAM)lpnmh);
784
785 /* cleanup */
786 if(text)
787 {
788 Free(lpnmh->pitem->pszText);
789 lpnmh->pitem->pszText = (LPSTR)text;
790 }
791 if(filter)
792 {
793 Free(((HD_TEXTFILTERA*)lpnmh->pitem->pvFilter)->pszText);
794 ((HD_TEXTFILTERA*)lpnmh->pitem->pvFilter)->pszText = (LPSTR)filter;
795 }
796
797 return ret;
798 }
799
800 static LRESULT notify_hdr(const LISTVIEW_INFO *infoPtr, INT code, LPNMHDR pnmh)
801 {
802 LRESULT result;
803
804 TRACE("(code=%d)\n", code);
805
806 pnmh->hwndFrom = infoPtr->hwndSelf;
807 pnmh->idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
808 pnmh->code = code;
809 result = SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, pnmh->idFrom, (LPARAM)pnmh);
810
811 TRACE(" <= %ld\n", result);
812
813 return result;
814 }
815
816 static inline BOOL notify(const LISTVIEW_INFO *infoPtr, INT code)
817 {
818 NMHDR nmh;
819 HWND hwnd = infoPtr->hwndSelf;
820 notify_hdr(infoPtr, code, &nmh);
821 return IsWindow(hwnd);
822 }
823
824 static inline void notify_itemactivate(const LISTVIEW_INFO *infoPtr, const LVHITTESTINFO *htInfo)
825 {
826 NMITEMACTIVATE nmia;
827 LVITEMW item;
828
829 if (htInfo) {
830 nmia.uNewState = 0;
831 nmia.uOldState = 0;
832 nmia.uChanged = 0;
833 nmia.uKeyFlags = 0;
834
835 item.mask = LVIF_PARAM|LVIF_STATE;
836 item.iItem = htInfo->iItem;
837 item.iSubItem = 0;
838 item.stateMask = (UINT)-1;
839 if (LISTVIEW_GetItemT(infoPtr, &item, TRUE)) {
840 nmia.lParam = item.lParam;
841 nmia.uOldState = item.state;
842 nmia.uNewState = item.state | LVIS_ACTIVATING;
843 nmia.uChanged = LVIF_STATE;
844 }
845
846 nmia.iItem = htInfo->iItem;
847 nmia.iSubItem = htInfo->iSubItem;
848 nmia.ptAction = htInfo->pt;
849
850 if (GetKeyState(VK_SHIFT) & 0x8000) nmia.uKeyFlags |= LVKF_SHIFT;
851 if (GetKeyState(VK_CONTROL) & 0x8000) nmia.uKeyFlags |= LVKF_CONTROL;
852 if (GetKeyState(VK_MENU) & 0x8000) nmia.uKeyFlags |= LVKF_ALT;
853 }
854 notify_hdr(infoPtr, LVN_ITEMACTIVATE, (LPNMHDR)&nmia);
855 }
856
857 static inline LRESULT notify_listview(const LISTVIEW_INFO *infoPtr, INT code, LPNMLISTVIEW plvnm)
858 {
859 TRACE("(code=%d, plvnm=%s)\n", code, debugnmlistview(plvnm));
860 return notify_hdr(infoPtr, code, (LPNMHDR)plvnm);
861 }
862
863 /* Handles NM_DBLCLK, NM_CLICK, NM_RDBLCLK, NM_RCLICK. Only NM_RCLICK return value is used. */
864 static BOOL notify_click(const LISTVIEW_INFO *infoPtr, INT code, const LVHITTESTINFO *lvht)
865 {
866 NMITEMACTIVATE nmia;
867 LVITEMW item;
868 HWND hwnd = infoPtr->hwndSelf;
869 LRESULT ret;
870
871 TRACE("code=%d, lvht=%s\n", code, debuglvhittestinfo(lvht));
872 ZeroMemory(&nmia, sizeof(nmia));
873 nmia.iItem = lvht->iItem;
874 nmia.iSubItem = lvht->iSubItem;
875 nmia.ptAction = lvht->pt;
876 item.mask = LVIF_PARAM;
877 item.iItem = lvht->iItem;
878 item.iSubItem = 0;
879 if (LISTVIEW_GetItemT(infoPtr, &item, TRUE)) nmia.lParam = item.lParam;
880 ret = notify_hdr(infoPtr, code, (NMHDR*)&nmia);
881 return IsWindow(hwnd) && (code == NM_RCLICK ? !ret : TRUE);
882 }
883
884 static BOOL notify_deleteitem(const LISTVIEW_INFO *infoPtr, INT nItem)
885 {
886 NMLISTVIEW nmlv;
887 LVITEMW item;
888 HWND hwnd = infoPtr->hwndSelf;
889
890 ZeroMemory(&nmlv, sizeof (NMLISTVIEW));
891 nmlv.iItem = nItem;
892 item.mask = LVIF_PARAM;
893 item.iItem = nItem;
894 item.iSubItem = 0;
895 if (LISTVIEW_GetItemT(infoPtr, &item, TRUE)) nmlv.lParam = item.lParam;
896 notify_listview(infoPtr, LVN_DELETEITEM, &nmlv);
897 return IsWindow(hwnd);
898 }
899
900 /*
901 Send notification. depends on dispinfoW having same
902 structure as dispinfoA.
903 infoPtr : listview struct
904 code : *Unicode* notification code
905 pdi : dispinfo structure (can be unicode or ansi)
906 isW : TRUE if dispinfo is Unicode
907 */
908 static BOOL notify_dispinfoT(const LISTVIEW_INFO *infoPtr, UINT code, LPNMLVDISPINFOW pdi, BOOL isW)
909 {
910 INT length = 0, ret_length;
911 LPWSTR buffer = NULL, ret_text;
912 BOOL return_ansi = FALSE;
913 BOOL return_unicode = FALSE;
914 BOOL ret;
915
916 if ((pdi->item.mask & LVIF_TEXT) && is_text(pdi->item.pszText))
917 {
918 return_unicode = ( isW && infoPtr->notifyFormat == NFR_ANSI);
919 return_ansi = (!isW && infoPtr->notifyFormat == NFR_UNICODE);
920 }
921
922 ret_length = pdi->item.cchTextMax;
923 ret_text = pdi->item.pszText;
924
925 if (return_unicode || return_ansi)
926 {
927 if (code != LVN_GETDISPINFOW)
928 {
929 length = return_ansi ?
930 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pdi->item.pszText, -1, NULL, 0):
931 WideCharToMultiByte(CP_ACP, 0, pdi->item.pszText, -1, NULL, 0, NULL, NULL);
932 }
933 else
934 {
935 length = pdi->item.cchTextMax;
936 *pdi->item.pszText = 0; /* make sure we don't process garbage */
937 }
938
939 buffer = Alloc( (return_ansi ? sizeof(WCHAR) : sizeof(CHAR)) * length);
940 if (!buffer) return FALSE;
941
942 if (return_ansi)
943 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pdi->item.pszText, -1,
944 buffer, length);
945 else
946 WideCharToMultiByte(CP_ACP, 0, pdi->item.pszText, -1, (LPSTR) buffer,
947 length, NULL, NULL);
948
949 pdi->item.pszText = buffer;
950 pdi->item.cchTextMax = length;
951 }
952
953 if (infoPtr->notifyFormat == NFR_ANSI)
954 code = get_ansi_notification(code);
955
956 TRACE(" pdi->item=%s\n", debuglvitem_t(&pdi->item, infoPtr->notifyFormat != NFR_ANSI));
957 ret = notify_hdr(infoPtr, code, &pdi->hdr);
958 TRACE(" resulting code=%d\n", pdi->hdr.code);
959
960 if (return_ansi || return_unicode)
961 {
962 if (return_ansi && (pdi->hdr.code == LVN_GETDISPINFOA))
963 {
964 strcpy((char*)ret_text, (char*)pdi->item.pszText);
965 }
966 else if (return_unicode && (pdi->hdr.code == LVN_GETDISPINFOW))
967 {
968 strcpyW(ret_text, pdi->item.pszText);
969 }
970 else if (return_ansi) /* note : pointer can be changed by app ! */
971 {
972 WideCharToMultiByte(CP_ACP, 0, pdi->item.pszText, -1, (LPSTR) ret_text,
973 ret_length, NULL, NULL);
974 }
975 else
976 MultiByteToWideChar(CP_ACP, 0, (LPSTR) pdi->item.pszText, -1,
977 ret_text, ret_length);
978
979 pdi->item.pszText = ret_text; /* restores our buffer */
980 pdi->item.cchTextMax = ret_length;
981
982 Free(buffer);
983 return ret;
984 }
985
986 /* if dipsinfo holder changed notification code then convert */
987 if (!isW && (pdi->hdr.code == LVN_GETDISPINFOW) && (pdi->item.mask & LVIF_TEXT))
988 {
989 length = WideCharToMultiByte(CP_ACP, 0, pdi->item.pszText, -1, NULL, 0, NULL, NULL);
990
991 buffer = Alloc(length * sizeof(CHAR));
992 if (!buffer) return FALSE;
993
994 WideCharToMultiByte(CP_ACP, 0, pdi->item.pszText, -1, (LPSTR) buffer,
995 ret_length, NULL, NULL);
996
997 strcpy((LPSTR)pdi->item.pszText, (LPSTR)buffer);
998 Free(buffer);
999 }
1000
1001 return ret;
1002 }
1003
1004 static void customdraw_fill(NMLVCUSTOMDRAW *lpnmlvcd, const LISTVIEW_INFO *infoPtr, HDC hdc,
1005 const RECT *rcBounds, const LVITEMW *lplvItem)
1006 {
1007 ZeroMemory(lpnmlvcd, sizeof(NMLVCUSTOMDRAW));
1008 lpnmlvcd->nmcd.hdc = hdc;
1009 lpnmlvcd->nmcd.rc = *rcBounds;
1010 lpnmlvcd->clrTextBk = infoPtr->clrTextBk;
1011 lpnmlvcd->clrText = infoPtr->clrText;
1012 if (!lplvItem) return;
1013 lpnmlvcd->nmcd.dwItemSpec = lplvItem->iItem + 1;
1014 lpnmlvcd->iSubItem = lplvItem->iSubItem;
1015 if (lplvItem->state & LVIS_SELECTED) lpnmlvcd->nmcd.uItemState |= CDIS_SELECTED;
1016 if (lplvItem->state & LVIS_FOCUSED) lpnmlvcd->nmcd.uItemState |= CDIS_FOCUS;
1017 if (lplvItem->iItem == infoPtr->nHotItem) lpnmlvcd->nmcd.uItemState |= CDIS_HOT;
1018 lpnmlvcd->nmcd.lItemlParam = lplvItem->lParam;
1019 }
1020
1021 static inline DWORD notify_customdraw (const LISTVIEW_INFO *infoPtr, DWORD dwDrawStage, NMLVCUSTOMDRAW *lpnmlvcd)
1022 {
1023 BOOL isForItem = (lpnmlvcd->nmcd.dwItemSpec != 0);
1024 DWORD result;
1025
1026 lpnmlvcd->nmcd.dwDrawStage = dwDrawStage;
1027 if (isForItem) lpnmlvcd->nmcd.dwDrawStage |= CDDS_ITEM;
1028 if (lpnmlvcd->iSubItem) lpnmlvcd->nmcd.dwDrawStage |= CDDS_SUBITEM;
1029 if (isForItem) lpnmlvcd->nmcd.dwItemSpec--;
1030 result = notify_hdr(infoPtr, NM_CUSTOMDRAW, &lpnmlvcd->nmcd.hdr);
1031 if (isForItem) lpnmlvcd->nmcd.dwItemSpec++;
1032 return result;
1033 }
1034
1035 static void prepaint_setup (const LISTVIEW_INFO *infoPtr, HDC hdc, NMLVCUSTOMDRAW *lpnmlvcd, BOOL SubItem)
1036 {
1037 if (lpnmlvcd->clrTextBk == CLR_DEFAULT)
1038 lpnmlvcd->clrTextBk = comctl32_color.clrWindow;
1039 if (lpnmlvcd->clrText == CLR_DEFAULT)
1040 lpnmlvcd->clrText = comctl32_color.clrWindowText;
1041
1042 /* apparently, for selected items, we have to override the returned values */
1043 if (!SubItem)
1044 {
1045 if (lpnmlvcd->nmcd.uItemState & CDIS_SELECTED)
1046 {
1047 if (infoPtr->bFocus)
1048 {
1049 lpnmlvcd->clrTextBk = comctl32_color.clrHighlight;
1050 lpnmlvcd->clrText = comctl32_color.clrHighlightText;
1051 }
1052 else if (infoPtr->dwStyle & LVS_SHOWSELALWAYS)
1053 {
1054 lpnmlvcd->clrTextBk = comctl32_color.clr3dFace;
1055 lpnmlvcd->clrText = comctl32_color.clrBtnText;
1056 }
1057 }
1058 }
1059
1060 /* Set the text attributes */
1061 if (lpnmlvcd->clrTextBk != CLR_NONE)
1062 {
1063 SetBkMode(hdc, OPAQUE);
1064 SetBkColor(hdc,lpnmlvcd->clrTextBk);
1065 }
1066 else
1067 SetBkMode(hdc, TRANSPARENT);
1068 SetTextColor(hdc, lpnmlvcd->clrText);
1069 }
1070
1071 static inline DWORD notify_postpaint (const LISTVIEW_INFO *infoPtr, NMLVCUSTOMDRAW *lpnmlvcd)
1072 {
1073 return notify_customdraw(infoPtr, CDDS_POSTPAINT, lpnmlvcd);
1074 }
1075
1076 /* returns TRUE when repaint needed, FALSE otherwise */
1077 static BOOL notify_measureitem(LISTVIEW_INFO *infoPtr)
1078 {
1079 MEASUREITEMSTRUCT mis;
1080 mis.CtlType = ODT_LISTVIEW;
1081 mis.CtlID = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
1082 mis.itemID = -1;
1083 mis.itemWidth = 0;
1084 mis.itemData = 0;
1085 mis.itemHeight= infoPtr->nItemHeight;
1086 SendMessageW(infoPtr->hwndNotify, WM_MEASUREITEM, mis.CtlID, (LPARAM)&mis);
1087 if (infoPtr->nItemHeight != max(mis.itemHeight, 1))
1088 {
1089 infoPtr->nMeasureItemHeight = infoPtr->nItemHeight = max(mis.itemHeight, 1);
1090 return TRUE;
1091 }
1092 return FALSE;
1093 }
1094
1095 /******** Item iterator functions **********************************/
1096
1097 static RANGES ranges_create(int count);
1098 static void ranges_destroy(RANGES ranges);
1099 static BOOL ranges_add(RANGES ranges, RANGE range);
1100 static BOOL ranges_del(RANGES ranges, RANGE range);
1101 static void ranges_dump(RANGES ranges);
1102
1103 static inline BOOL ranges_additem(RANGES ranges, INT nItem)
1104 {
1105 RANGE range = { nItem, nItem + 1 };
1106
1107 return ranges_add(ranges, range);
1108 }
1109
1110 static inline BOOL ranges_delitem(RANGES ranges, INT nItem)
1111 {
1112 RANGE range = { nItem, nItem + 1 };
1113
1114 return ranges_del(ranges, range);
1115 }
1116
1117 /***
1118 * ITERATOR DOCUMENTATION
1119 *
1120 * The iterator functions allow for easy, and convenient iteration
1121 * over items of interest in the list. Typically, you create a
1122 * iterator, use it, and destroy it, as such:
1123 * ITERATOR i;
1124 *
1125 * iterator_xxxitems(&i, ...);
1126 * while (iterator_{prev,next}(&i)
1127 * {
1128 * //code which uses i.nItem
1129 * }
1130 * iterator_destroy(&i);
1131 *
1132 * where xxx is either: framed, or visible.
1133 * Note that it is important that the code destroys the iterator
1134 * after it's done with it, as the creation of the iterator may
1135 * allocate memory, which thus needs to be freed.
1136 *
1137 * You can iterate both forwards, and backwards through the list,
1138 * by using iterator_next or iterator_prev respectively.
1139 *
1140 * Lower numbered items are draw on top of higher number items in
1141 * LVS_ICON, and LVS_SMALLICON (which are the only modes where
1142 * items may overlap). So, to test items, you should use
1143 * iterator_next
1144 * which lists the items top to bottom (in Z-order).
1145 * For drawing items, you should use
1146 * iterator_prev
1147 * which lists the items bottom to top (in Z-order).
1148 * If you keep iterating over the items after the end-of-items
1149 * marker (-1) is returned, the iterator will start from the
1150 * beginning. Typically, you don't need to test for -1,
1151 * because iterator_{next,prev} will return TRUE if more items
1152 * are to be iterated over, or FALSE otherwise.
1153 *
1154 * Note: the iterator is defined to be bidirectional. That is,
1155 * any number of prev followed by any number of next, or
1156 * five versa, should leave the iterator at the same item:
1157 * prev * n, next * n = next * n, prev * n
1158 *
1159 * The iterator has a notion of an out-of-order, special item,
1160 * which sits at the start of the list. This is used in
1161 * LVS_ICON, and LVS_SMALLICON mode to handle the focused item,
1162 * which needs to be first, as it may overlap other items.
1163 *
1164 * The code is a bit messy because we have:
1165 * - a special item to deal with
1166 * - simple range, or composite range
1167 * - empty range.
1168 * If you find bugs, or want to add features, please make sure you
1169 * always check/modify *both* iterator_prev, and iterator_next.
1170 */
1171
1172 /****
1173 * This function iterates through the items in increasing order,
1174 * but prefixed by the special item, then -1. That is:
1175 * special, 1, 2, 3, ..., n, -1.
1176 * Each item is listed only once.
1177 */
1178 static inline BOOL iterator_next(ITERATOR* i)
1179 {
1180 if (i->nItem == -1)
1181 {
1182 i->nItem = i->nSpecial;
1183 if (i->nItem != -1) return TRUE;
1184 }
1185 if (i->nItem == i->nSpecial)
1186 {
1187 if (i->ranges) i->index = 0;
1188 goto pickarange;
1189 }
1190
1191 i->nItem++;
1192 testitem:
1193 if (i->nItem == i->nSpecial) i->nItem++;
1194 if (i->nItem < i->range.upper) return TRUE;
1195
1196 pickarange:
1197 if (i->ranges)
1198 {
1199 if (i->index < DPA_GetPtrCount(i->ranges->hdpa))
1200 i->range = *(RANGE*)DPA_GetPtr(i->ranges->hdpa, i->index++);
1201 else goto end;
1202 }
1203 else if (i->nItem >= i->range.upper) goto end;
1204
1205 i->nItem = i->range.lower;
1206 if (i->nItem >= 0) goto testitem;
1207 end:
1208 i->nItem = -1;
1209 return FALSE;
1210 }
1211
1212 /****
1213 * This function iterates through the items in decreasing order,
1214 * followed by the special item, then -1. That is:
1215 * n, n-1, ..., 3, 2, 1, special, -1.
1216 * Each item is listed only once.
1217 */
1218 static inline BOOL iterator_prev(ITERATOR* i)
1219 {
1220 BOOL start = FALSE;
1221
1222 if (i->nItem == -1)
1223 {
1224 start = TRUE;
1225 if (i->ranges) i->index = DPA_GetPtrCount(i->ranges->hdpa);
1226 goto pickarange;
1227 }
1228 if (i->nItem == i->nSpecial)
1229 {
1230 i->nItem = -1;
1231 return FALSE;
1232 }
1233
1234 testitem:
1235 i->nItem--;
1236 if (i->nItem == i->nSpecial) i->nItem--;
1237 if (i->nItem >= i->range.lower) return TRUE;
1238
1239 pickarange:
1240 if (i->ranges)
1241 {
1242 if (i->index > 0)
1243 i->range = *(RANGE*)DPA_GetPtr(i->ranges->hdpa, --i->index);
1244 else goto end;
1245 }
1246 else if (!start && i->nItem < i->range.lower) goto end;
1247
1248 i->nItem = i->range.upper;
1249 if (i->nItem > 0) goto testitem;
1250 end:
1251 return (i->nItem = i->nSpecial) != -1;
1252 }
1253
1254 static RANGE iterator_range(const ITERATOR *i)
1255 {
1256 RANGE range;
1257
1258 if (!i->ranges) return i->range;
1259
1260 if (DPA_GetPtrCount(i->ranges->hdpa) > 0)
1261 {
1262 range.lower = (*(RANGE*)DPA_GetPtr(i->ranges->hdpa, 0)).lower;
1263 range.upper = (*(RANGE*)DPA_GetPtr(i->ranges->hdpa, DPA_GetPtrCount(i->ranges->hdpa) - 1)).upper;
1264 }
1265 else range.lower = range.upper = 0;
1266
1267 return range;
1268 }
1269
1270 /***
1271 * Releases resources associated with this iterator.
1272 */
1273 static inline void iterator_destroy(const ITERATOR *i)
1274 {
1275 ranges_destroy(i->ranges);
1276 }
1277
1278 /***
1279 * Create an empty iterator.
1280 */
1281 static inline BOOL iterator_empty(ITERATOR* i)
1282 {
1283 ZeroMemory(i, sizeof(*i));
1284 i->nItem = i->nSpecial = i->range.lower = i->range.upper = -1;
1285 return TRUE;
1286 }
1287
1288 /***
1289 * Create an iterator over a range.
1290 */
1291 static inline BOOL iterator_rangeitems(ITERATOR* i, RANGE range)
1292 {
1293 iterator_empty(i);
1294 i->range = range;
1295 return TRUE;
1296 }
1297
1298 /***
1299 * Create an iterator over a bunch of ranges.
1300 * Please note that the iterator will take ownership of the ranges,
1301 * and will free them upon destruction.
1302 */
1303 static inline BOOL iterator_rangesitems(ITERATOR* i, RANGES ranges)
1304 {
1305 iterator_empty(i);
1306 i->ranges = ranges;
1307 return TRUE;
1308 }
1309
1310 /***
1311 * Creates an iterator over the items which intersect frame.
1312 * Uses absolute coordinates rather than compensating for the current offset.
1313 */
1314 static BOOL iterator_frameditems_absolute(ITERATOR* i, const LISTVIEW_INFO* infoPtr, const RECT *frame)
1315 {
1316 RECT rcItem, rcTemp;
1317
1318 /* in case we fail, we want to return an empty iterator */
1319 if (!iterator_empty(i)) return FALSE;
1320
1321 TRACE("(frame=%s)\n", wine_dbgstr_rect(frame));
1322
1323 if (infoPtr->uView == LV_VIEW_ICON || infoPtr->uView == LV_VIEW_SMALLICON)
1324 {
1325 INT nItem;
1326
1327 if (infoPtr->uView == LV_VIEW_ICON && infoPtr->nFocusedItem != -1)
1328 {
1329 LISTVIEW_GetItemBox(infoPtr, infoPtr->nFocusedItem, &rcItem);
1330 if (IntersectRect(&rcTemp, &rcItem, frame))
1331 i->nSpecial = infoPtr->nFocusedItem;
1332 }
1333 if (!(iterator_rangesitems(i, ranges_create(50)))) return FALSE;
1334 /* to do better here, we need to have PosX, and PosY sorted */
1335 TRACE("building icon ranges:\n");
1336 for (nItem = 0; nItem < infoPtr->nItemCount; nItem++)
1337 {
1338 rcItem.left = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosX, nItem);
1339 rcItem.top = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosY, nItem);
1340 rcItem.right = rcItem.left + infoPtr->nItemWidth;
1341 rcItem.bottom = rcItem.top + infoPtr->nItemHeight;
1342 if (IntersectRect(&rcTemp, &rcItem, frame))
1343 ranges_additem(i->ranges, nItem);
1344 }
1345 return TRUE;
1346 }
1347 else if (infoPtr->uView == LV_VIEW_DETAILS)
1348 {
1349 RANGE range;
1350
1351 if (frame->left >= infoPtr->nItemWidth) return TRUE;
1352 if (frame->top >= infoPtr->nItemHeight * infoPtr->nItemCount) return TRUE;
1353
1354 range.lower = max(frame->top / infoPtr->nItemHeight, 0);
1355 range.upper = min((frame->bottom - 1) / infoPtr->nItemHeight, infoPtr->nItemCount - 1) + 1;
1356 if (range.upper <= range.lower) return TRUE;
1357 if (!iterator_rangeitems(i, range)) return FALSE;
1358 TRACE(" report=%s\n", debugrange(&i->range));
1359 }
1360 else
1361 {
1362 INT nPerCol = max((infoPtr->rcList.bottom - infoPtr->rcList.top) / infoPtr->nItemHeight, 1);
1363 INT nFirstRow = max(frame->top / infoPtr->nItemHeight, 0);
1364 INT nLastRow = min((frame->bottom - 1) / infoPtr->nItemHeight, nPerCol - 1);
1365 INT nFirstCol;
1366 INT nLastCol;
1367 INT lower;
1368 RANGE item_range;
1369 INT nCol;
1370
1371 if (infoPtr->nItemWidth)
1372 {
1373 nFirstCol = max(frame->left / infoPtr->nItemWidth, 0);
1374 nLastCol = min((frame->right - 1) / infoPtr->nItemWidth, (infoPtr->nItemCount + nPerCol - 1) / nPerCol);
1375 }
1376 else
1377 {
1378 nFirstCol = max(frame->left, 0);
1379 nLastCol = min(frame->right - 1, (infoPtr->nItemCount + nPerCol - 1) / nPerCol);
1380 }
1381
1382 lower = nFirstCol * nPerCol + nFirstRow;
1383
1384 TRACE("nPerCol=%d, nFirstRow=%d, nLastRow=%d, nFirstCol=%d, nLastCol=%d, lower=%d\n",
1385 nPerCol, nFirstRow, nLastRow, nFirstCol, nLastCol, lower);
1386
1387 if (nLastCol < nFirstCol || nLastRow < nFirstRow) return TRUE;
1388
1389 if (!(iterator_rangesitems(i, ranges_create(nLastCol - nFirstCol + 1)))) return FALSE;
1390 TRACE("building list ranges:\n");
1391 for (nCol = nFirstCol; nCol <= nLastCol; nCol++)
1392 {
1393 item_range.lower = nCol * nPerCol + nFirstRow;
1394 if(item_range.lower >= infoPtr->nItemCount) break;
1395 item_range.upper = min(nCol * nPerCol + nLastRow + 1, infoPtr->nItemCount);
1396 TRACE(" list=%s\n", debugrange(&item_range));
1397 ranges_add(i->ranges, item_range);
1398 }
1399 }
1400
1401 return TRUE;
1402 }
1403
1404 /***
1405 * Creates an iterator over the items which intersect lprc.
1406 */
1407 static BOOL iterator_frameditems(ITERATOR* i, const LISTVIEW_INFO* infoPtr, const RECT *lprc)
1408 {
1409 RECT frame = *lprc;
1410 POINT Origin;
1411
1412 TRACE("(lprc=%s)\n", wine_dbgstr_rect(lprc));
1413
1414 LISTVIEW_GetOrigin(infoPtr, &Origin);
1415 OffsetRect(&frame, -Origin.x, -Origin.y);
1416
1417 return iterator_frameditems_absolute(i, infoPtr, &frame);
1418 }
1419
1420 /***
1421 * Creates an iterator over the items which intersect the visible region of hdc.
1422 */
1423 static BOOL iterator_visibleitems(ITERATOR *i, const LISTVIEW_INFO *infoPtr, HDC hdc)
1424 {
1425 POINT Origin, Position;
1426 RECT rcItem, rcClip;
1427 INT rgntype;
1428
1429 rgntype = GetClipBox(hdc, &rcClip);
1430 if (rgntype == NULLREGION) return iterator_empty(i);
1431 if (!iterator_frameditems(i, infoPtr, &rcClip)) return FALSE;
1432 if (rgntype == SIMPLEREGION) return TRUE;
1433
1434 /* first deal with the special item */
1435 if (i->nSpecial != -1)
1436 {
1437 LISTVIEW_GetItemBox(infoPtr, i->nSpecial, &rcItem);
1438 if (!RectVisible(hdc, &rcItem)) i->nSpecial = -1;
1439 }
1440
1441 /* if we can't deal with the region, we'll just go with the simple range */
1442 LISTVIEW_GetOrigin(infoPtr, &Origin);
1443 TRACE("building visible range:\n");
1444 if (!i->ranges && i->range.lower < i->range.upper)
1445 {
1446 if (!(i->ranges = ranges_create(50))) return TRUE;
1447 if (!ranges_add(i->ranges, i->range))
1448 {
1449 ranges_destroy(i->ranges);
1450 i->ranges = 0;
1451 return TRUE;
1452 }
1453 }
1454
1455 /* now delete the invisible items from the list */
1456 while(iterator_next(i))
1457 {
1458 LISTVIEW_GetItemOrigin(infoPtr, i->nItem, &Position);
1459 rcItem.left = (infoPtr->uView == LV_VIEW_DETAILS) ? Origin.x : Position.x + Origin.x;
1460 rcItem.top = Position.y + Origin.y;
1461 rcItem.right = rcItem.left + infoPtr->nItemWidth;
1462 rcItem.bottom = rcItem.top + infoPtr->nItemHeight;
1463 if (!RectVisible(hdc, &rcItem))
1464 ranges_delitem(i->ranges, i->nItem);
1465 }
1466 /* the iterator should restart on the next iterator_next */
1467 TRACE("done\n");
1468
1469 return TRUE;
1470 }
1471
1472 /* Remove common elements from two iterators */
1473 /* Passed iterators have to point on the first elements */
1474 static BOOL iterator_remove_common_items(ITERATOR *iter1, ITERATOR *iter2)
1475 {
1476 if(!iter1->ranges || !iter2->ranges) {
1477 int lower, upper;
1478
1479 if(iter1->ranges || iter2->ranges ||
1480 (iter1->range.lower<iter2->range.lower && iter1->range.upper>iter2->range.upper) ||
1481 (iter1->range.lower>iter2->range.lower && iter1->range.upper<iter2->range.upper)) {
1482 ERR("result is not a one range iterator\n");
1483 return FALSE;
1484 }
1485
1486 if(iter1->range.lower==-1 || iter2->range.lower==-1)
1487 return TRUE;
1488
1489 lower = iter1->range.lower;
1490 upper = iter1->range.upper;
1491
1492 if(lower < iter2->range.lower)
1493 iter1->range.upper = iter2->range.lower;
1494 else if(upper > iter2->range.upper)
1495 iter1->range.lower = iter2->range.upper;
1496 else
1497 iter1->range.lower = iter1->range.upper = -1;
1498
1499 if(iter2->range.lower < lower)
1500 iter2->range.upper = lower;
1501 else if(iter2->range.upper > upper)
1502 iter2->range.lower = upper;
1503 else
1504 iter2->range.lower = iter2->range.upper = -1;
1505
1506 return TRUE;
1507 }
1508
1509 iterator_next(iter1);
1510 iterator_next(iter2);
1511
1512 while(1) {
1513 if(iter1->nItem==-1 || iter2->nItem==-1)
1514 break;
1515
1516 if(iter1->nItem == iter2->nItem) {
1517 int delete = iter1->nItem;
1518
1519 iterator_prev(iter1);
1520 iterator_prev(iter2);
1521 ranges_delitem(iter1->ranges, delete);
1522 ranges_delitem(iter2->ranges, delete);
1523 iterator_next(iter1);
1524 iterator_next(iter2);
1525 } else if(iter1->nItem > iter2->nItem)
1526 iterator_next(iter2);
1527 else
1528 iterator_next(iter1);
1529 }
1530
1531 iter1->nItem = iter1->range.lower = iter1->range.upper = -1;
1532 iter2->nItem = iter2->range.lower = iter2->range.upper = -1;
1533 return TRUE;
1534 }
1535
1536 /******** Misc helper functions ************************************/
1537
1538 static inline LRESULT CallWindowProcT(WNDPROC proc, HWND hwnd, UINT uMsg,
1539 WPARAM wParam, LPARAM lParam, BOOL isW)
1540 {
1541 if (isW) return CallWindowProcW(proc, hwnd, uMsg, wParam, lParam);
1542 else return CallWindowProcA(proc, hwnd, uMsg, wParam, lParam);
1543 }
1544
1545 static inline BOOL is_autoarrange(const LISTVIEW_INFO *infoPtr)
1546 {
1547 return ((infoPtr->dwStyle & LVS_AUTOARRANGE) || infoPtr->bAutoarrange) &&
1548 (infoPtr->uView == LV_VIEW_ICON || infoPtr->uView == LV_VIEW_SMALLICON);
1549 }
1550
1551 static void toggle_checkbox_state(LISTVIEW_INFO *infoPtr, INT nItem)
1552 {
1553 DWORD state = STATEIMAGEINDEX(LISTVIEW_GetItemState(infoPtr, nItem, LVIS_STATEIMAGEMASK));
1554 if(state == 1 || state == 2)
1555 {
1556 LVITEMW lvitem;
1557 state ^= 3;
1558 lvitem.state = INDEXTOSTATEIMAGEMASK(state);
1559 lvitem.stateMask = LVIS_STATEIMAGEMASK;
1560 LISTVIEW_SetItemState(infoPtr, nItem, &lvitem);
1561 }
1562 }
1563
1564 /* this should be called after window style got updated,
1565 it used to reset view state to match current window style */
1566 static inline void map_style_view(LISTVIEW_INFO *infoPtr)
1567 {
1568 switch (infoPtr->dwStyle & LVS_TYPEMASK)
1569 {
1570 case LVS_ICON:
1571 infoPtr->uView = LV_VIEW_ICON;
1572 break;
1573 case LVS_REPORT:
1574 infoPtr->uView = LV_VIEW_DETAILS;
1575 break;
1576 case LVS_SMALLICON:
1577 infoPtr->uView = LV_VIEW_SMALLICON;
1578 break;
1579 case LVS_LIST:
1580 infoPtr->uView = LV_VIEW_LIST;
1581 }
1582 }
1583
1584 /* computes next item id value */
1585 static DWORD get_next_itemid(const LISTVIEW_INFO *infoPtr)
1586 {
1587 INT count = DPA_GetPtrCount(infoPtr->hdpaItemIds);
1588
1589 if (count > 0)
1590 {
1591 ITEM_ID *lpID = DPA_GetPtr(infoPtr->hdpaItemIds, count - 1);
1592 return lpID->id + 1;
1593 }
1594 return 0;
1595 }
1596
1597 /******** Internal API functions ************************************/
1598
1599 static inline COLUMN_INFO * LISTVIEW_GetColumnInfo(const LISTVIEW_INFO *infoPtr, INT nSubItem)
1600 {
1601 static COLUMN_INFO mainItem;
1602
1603 if (nSubItem == 0 && DPA_GetPtrCount(infoPtr->hdpaColumns) == 0) return &mainItem;
1604 assert (nSubItem >= 0 && nSubItem < DPA_GetPtrCount(infoPtr->hdpaColumns));
1605
1606 /* update cached column rectangles */
1607 if (infoPtr->colRectsDirty)
1608 {
1609 COLUMN_INFO *info;
1610 LISTVIEW_INFO *Ptr = (LISTVIEW_INFO*)infoPtr;
1611 INT i;
1612
1613 for (i = 0; i < DPA_GetPtrCount(infoPtr->hdpaColumns); i++) {
1614 info = DPA_GetPtr(infoPtr->hdpaColumns, i);
1615 SendMessageW(infoPtr->hwndHeader, HDM_GETITEMRECT, i, (LPARAM)&info->rcHeader);
1616 }
1617 Ptr->colRectsDirty = FALSE;
1618 }
1619
1620 return DPA_GetPtr(infoPtr->hdpaColumns, nSubItem);
1621 }
1622
1623 static INT LISTVIEW_CreateHeader(LISTVIEW_INFO *infoPtr)
1624 {
1625 DWORD dFlags = WS_CHILD | HDS_HORZ | HDS_FULLDRAG | HDS_DRAGDROP;
1626 HINSTANCE hInst;
1627
1628 if (infoPtr->hwndHeader) return 0;
1629
1630 TRACE("Creating header for list %p\n", infoPtr->hwndSelf);
1631
1632 /* setup creation flags */
1633 dFlags |= (LVS_NOSORTHEADER & infoPtr->dwStyle) ? 0 : HDS_BUTTONS;
1634 dFlags |= (LVS_NOCOLUMNHEADER & infoPtr->dwStyle) ? HDS_HIDDEN : 0;
1635
1636 hInst = (HINSTANCE)GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_HINSTANCE);
1637
1638 /* create header */
1639 infoPtr->hwndHeader = CreateWindowW(WC_HEADERW, NULL, dFlags,
1640 0, 0, 0, 0, infoPtr->hwndSelf, NULL, hInst, NULL);
1641 if (!infoPtr->hwndHeader) return -1;
1642
1643 /* set header unicode format */
1644 SendMessageW(infoPtr->hwndHeader, HDM_SETUNICODEFORMAT, TRUE, 0);
1645
1646 /* set header font */
1647 SendMessageW(infoPtr->hwndHeader, WM_SETFONT, (WPARAM)infoPtr->hFont, TRUE);
1648
1649 /* set header image list */
1650 if (infoPtr->himlSmall)
1651 SendMessageW(infoPtr->hwndHeader, HDM_SETIMAGELIST, 0, (LPARAM)infoPtr->himlSmall);
1652
1653 LISTVIEW_UpdateSize(infoPtr);
1654
1655 return 0;
1656 }
1657
1658 static inline void LISTVIEW_GetHeaderRect(const LISTVIEW_INFO *infoPtr, INT nSubItem, LPRECT lprc)
1659 {
1660 *lprc = LISTVIEW_GetColumnInfo(infoPtr, nSubItem)->rcHeader;
1661 }
1662
1663 static inline BOOL LISTVIEW_IsHeaderEnabled(const LISTVIEW_INFO *infoPtr)
1664 {
1665 return (infoPtr->uView == LV_VIEW_DETAILS ||
1666 infoPtr->dwLvExStyle & LVS_EX_HEADERINALLVIEWS) &&
1667 !(infoPtr->dwStyle & LVS_NOCOLUMNHEADER);
1668 }
1669
1670 static inline BOOL LISTVIEW_GetItemW(const LISTVIEW_INFO *infoPtr, LPLVITEMW lpLVItem)
1671 {
1672 return LISTVIEW_GetItemT(infoPtr, lpLVItem, TRUE);
1673 }
1674
1675 /* used to handle collapse main item column case */
1676 static inline BOOL LISTVIEW_DrawFocusRect(const LISTVIEW_INFO *infoPtr, HDC hdc)
1677 {
1678 BOOL Ret = FALSE;
1679
1680 if (infoPtr->rcFocus.left < infoPtr->rcFocus.right)
1681 {
1682 DWORD dwOldBkColor, dwOldTextColor;
1683
1684 dwOldBkColor = SetBkColor(hdc, RGB(255, 255, 255));
1685 dwOldTextColor = SetBkColor(hdc, RGB(0, 0, 0));
1686 Ret = DrawFocusRect(hdc, &infoPtr->rcFocus);
1687 SetBkColor(hdc, dwOldBkColor);
1688 SetBkColor(hdc, dwOldTextColor);
1689 }
1690 return Ret;
1691 }
1692
1693 /* Listview invalidation functions: use _only_ these functions to invalidate */
1694
1695 static inline BOOL is_redrawing(const LISTVIEW_INFO *infoPtr)
1696 {
1697 return infoPtr->bRedraw;
1698 }
1699
1700 static inline void LISTVIEW_InvalidateRect(const LISTVIEW_INFO *infoPtr, const RECT* rect)
1701 {
1702 if(!is_redrawing(infoPtr)) return;
1703 TRACE(" invalidating rect=%s\n", wine_dbgstr_rect(rect));
1704 InvalidateRect(infoPtr->hwndSelf, rect, TRUE);
1705 }
1706
1707 static inline void LISTVIEW_InvalidateItem(const LISTVIEW_INFO *infoPtr, INT nItem)
1708 {
1709 RECT rcBox;
1710
1711 if(!is_redrawing(infoPtr)) return;
1712 LISTVIEW_GetItemBox(infoPtr, nItem, &rcBox);
1713 LISTVIEW_InvalidateRect(infoPtr, &rcBox);
1714 }
1715
1716 static inline void LISTVIEW_InvalidateSubItem(const LISTVIEW_INFO *infoPtr, INT nItem, INT nSubItem)
1717 {
1718 POINT Origin, Position;
1719 RECT rcBox;
1720
1721 if(!is_redrawing(infoPtr)) return;
1722 assert (infoPtr->uView == LV_VIEW_DETAILS);
1723 LISTVIEW_GetOrigin(infoPtr, &Origin);
1724 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position);
1725 LISTVIEW_GetHeaderRect(infoPtr, nSubItem, &rcBox);
1726 rcBox.top = 0;
1727 rcBox.bottom = infoPtr->nItemHeight;
1728 OffsetRect(&rcBox, Origin.x + Position.x, Origin.y + Position.y);
1729 LISTVIEW_InvalidateRect(infoPtr, &rcBox);
1730 }
1731
1732 static inline void LISTVIEW_InvalidateList(const LISTVIEW_INFO *infoPtr)
1733 {
1734 LISTVIEW_InvalidateRect(infoPtr, NULL);
1735 }
1736
1737 static inline void LISTVIEW_InvalidateColumn(const LISTVIEW_INFO *infoPtr, INT nColumn)
1738 {
1739 RECT rcCol;
1740
1741 if(!is_redrawing(infoPtr)) return;
1742 LISTVIEW_GetHeaderRect(infoPtr, nColumn, &rcCol);
1743 rcCol.top = infoPtr->rcList.top;
1744 rcCol.bottom = infoPtr->rcList.bottom;
1745 LISTVIEW_InvalidateRect(infoPtr, &rcCol);
1746 }
1747
1748 /***
1749 * DESCRIPTION:
1750 * Retrieves the number of items that can fit vertically in the client area.
1751 *
1752 * PARAMETER(S):
1753 * [I] infoPtr : valid pointer to the listview structure
1754 *
1755 * RETURN:
1756 * Number of items per row.
1757 */
1758 static inline INT LISTVIEW_GetCountPerRow(const LISTVIEW_INFO *infoPtr)
1759 {
1760 INT nListWidth = infoPtr->rcList.right - infoPtr->rcList.left;
1761
1762 return max(nListWidth/(infoPtr->nItemWidth ? infoPtr->nItemWidth : 1), 1);
1763 }
1764
1765 /***
1766 * DESCRIPTION:
1767 * Retrieves the number of items that can fit horizontally in the client
1768 * area.
1769 *
1770 * PARAMETER(S):
1771 * [I] infoPtr : valid pointer to the listview structure
1772 *
1773 * RETURN:
1774 * Number of items per column.
1775 */
1776 static inline INT LISTVIEW_GetCountPerColumn(const LISTVIEW_INFO *infoPtr)
1777 {
1778 INT nListHeight = infoPtr->rcList.bottom - infoPtr->rcList.top;
1779
1780 return max(nListHeight / infoPtr->nItemHeight, 1);
1781 }
1782
1783
1784 /*************************************************************************
1785 * LISTVIEW_ProcessLetterKeys
1786 *
1787 * Processes keyboard messages generated by pressing the letter keys
1788 * on the keyboard.
1789 * What this does is perform a case insensitive search from the
1790 * current position with the following quirks:
1791 * - If two chars or more are pressed in quick succession we search
1792 * for the corresponding string (e.g. 'abc').
1793 * - If there is a delay we wipe away the current search string and
1794 * restart with just that char.
1795 * - If the user keeps pressing the same character, whether slowly or
1796 * fast, so that the search string is entirely composed of this
1797 * character ('aaaaa' for instance), then we search for first item
1798 * that starting with that character.
1799 * - If the user types the above character in quick succession, then
1800 * we must also search for the corresponding string ('aaaaa'), and
1801 * go to that string if there is a match.
1802 *
1803 * PARAMETERS
1804 * [I] hwnd : handle to the window
1805 * [I] charCode : the character code, the actual character
1806 * [I] keyData : key data
1807 *
1808 * RETURNS
1809 *
1810 * Zero.
1811 *
1812 * BUGS
1813 *
1814 * - The current implementation has a list of characters it will
1815 * accept and it ignores everything else. In particular it will
1816 * ignore accentuated characters which seems to match what
1817 * Windows does. But I'm not sure it makes sense to follow
1818 * Windows there.
1819 * - We don't sound a beep when the search fails.
1820 *
1821 * SEE ALSO
1822 *
1823 * TREEVIEW_ProcessLetterKeys
1824 */
1825 static INT LISTVIEW_ProcessLetterKeys(LISTVIEW_INFO *infoPtr, WPARAM charCode, LPARAM keyData)
1826 {
1827 WCHAR buffer[MAX_PATH];
1828 DWORD prevTime;
1829 LVITEMW item;
1830 int startidx;
1831 INT nItem;
1832 INT diff;
1833
1834 /* simple parameter checking */
1835 if (!charCode || !keyData || infoPtr->nItemCount == 0) return 0;
1836
1837 /* only allow the valid WM_CHARs through */
1838 if (!isalnumW(charCode) &&
1839 charCode != '.' && charCode != '`' && charCode != '!' &&
1840 charCode != '@' && charCode != '#' && charCode != '$' &&
1841 charCode != '%' && charCode != '^' && charCode != '&' &&
1842 charCode != '*' && charCode != '(' && charCode != ')' &&
1843 charCode != '-' && charCode != '_' && charCode != '+' &&
1844 charCode != '=' && charCode != '\\'&& charCode != ']' &&
1845 charCode != '}' && charCode != '[' && charCode != '{' &&
1846 charCode != '/' && charCode != '?' && charCode != '>' &&
1847 charCode != '<' && charCode != ',' && charCode != '~')
1848 return 0;
1849
1850 /* update the search parameters */
1851 prevTime = infoPtr->lastKeyPressTimestamp;
1852 infoPtr->lastKeyPressTimestamp = GetTickCount();
1853 diff = infoPtr->lastKeyPressTimestamp - prevTime;
1854
1855 if (diff >= 0 && diff < KEY_DELAY)
1856 {
1857 if (infoPtr->nSearchParamLength < MAX_PATH - 1)
1858 infoPtr->szSearchParam[infoPtr->nSearchParamLength++] = charCode;
1859
1860 if (infoPtr->charCode != charCode)
1861 infoPtr->charCode = charCode = 0;
1862 }
1863 else
1864 {
1865 infoPtr->charCode = charCode;
1866 infoPtr->szSearchParam[0] = charCode;
1867 infoPtr->nSearchParamLength = 1;
1868 }
1869
1870 /* should start from next after focused item, so next item that matches
1871 will be selected, if there isn't any and focused matches it will be selected
1872 on second search stage from beginning of the list */
1873 if (infoPtr->nFocusedItem >= 0 && infoPtr->nItemCount > 1)
1874 {
1875 /* with some accumulated search data available start with current focus, otherwise
1876 it's excluded from search */
1877 startidx = infoPtr->nSearchParamLength > 1 ? infoPtr->nFocusedItem : infoPtr->nFocusedItem + 1;
1878 if (startidx == infoPtr->nItemCount) startidx = 0;
1879 }
1880 else
1881 startidx = 0;
1882
1883 /* let application handle this for virtual listview */
1884 if (infoPtr->dwStyle & LVS_OWNERDATA)
1885 {
1886 NMLVFINDITEMW nmlv;
1887
1888 memset(&nmlv.lvfi, 0, sizeof(nmlv.lvfi));
1889 nmlv.lvfi.flags = (LVFI_WRAP | LVFI_PARTIAL);
1890 nmlv.lvfi.psz = infoPtr->szSearchParam;
1891 nmlv.iStart = startidx;
1892
1893 infoPtr->szSearchParam[infoPtr->nSearchParamLength] = 0;
1894
1895 nItem = notify_hdr(infoPtr, LVN_ODFINDITEMW, (LPNMHDR)&nmlv.hdr);
1896 }
1897 else
1898 {
1899 int i = startidx, endidx;
1900
1901 /* and search from the current position */
1902 nItem = -1;
1903 endidx = infoPtr->nItemCount;
1904
1905 /* first search in [startidx, endidx), on failure continue in [0, startidx) */
1906 while (1)
1907 {
1908 /* start from first item if not found with >= startidx */
1909 if (i == infoPtr->nItemCount && startidx > 0)
1910 {
1911 endidx = startidx;
1912 startidx = 0;
1913 }
1914
1915 for (i = startidx; i < endidx; i++)
1916 {
1917 /* retrieve text */
1918 item.mask = LVIF_TEXT;
1919 item.iItem = i;
1920 item.iSubItem = 0;
1921 item.pszText = buffer;
1922 item.cchTextMax = MAX_PATH;
1923 if (!LISTVIEW_GetItemW(infoPtr, &item)) return 0;
1924
1925 if (!lstrncmpiW(item.pszText, infoPtr->szSearchParam, infoPtr->nSearchParamLength))
1926 {
1927 nItem = i;
1928 break;
1929 }
1930 /* this is used to find first char match when search string is not available yet,
1931 otherwise every WM_CHAR will search to next item by first char, ignoring that we're
1932 already waiting for user to complete a string */
1933 else if (nItem == -1 && infoPtr->nSearchParamLength == 1 && !lstrncmpiW(item.pszText, infoPtr->szSearchParam, 1))
1934 {
1935 /* this would work but we must keep looking for a longer match */
1936 nItem = i;
1937 }
1938 }
1939
1940 if ( nItem != -1 || /* found something */
1941 endidx != infoPtr->nItemCount || /* second search done */
1942 (startidx == 0 && endidx == infoPtr->nItemCount) /* full range for first search */ )
1943 break;
1944 };
1945 }
1946
1947 if (nItem != -1)
1948 LISTVIEW_KeySelection(infoPtr, nItem, FALSE);
1949
1950 return 0;
1951 }
1952
1953 /*************************************************************************
1954 * LISTVIEW_UpdateHeaderSize [Internal]
1955 *
1956 * Function to resize the header control
1957 *
1958 * PARAMS
1959 * [I] hwnd : handle to a window
1960 * [I] nNewScrollPos : scroll pos to set
1961 *
1962 * RETURNS
1963 * None.
1964 */
1965 static void LISTVIEW_UpdateHeaderSize(const LISTVIEW_INFO *infoPtr, INT nNewScrollPos)
1966 {
1967 RECT winRect;
1968 POINT point[2];
1969
1970 TRACE("nNewScrollPos=%d\n", nNewScrollPos);
1971
1972 if (!infoPtr->hwndHeader) return;
1973
1974 GetWindowRect(infoPtr->hwndHeader, &winRect);
1975 point[0].x = winRect.left;
1976 point[0].y = winRect.top;
1977 point[1].x = winRect.right;
1978 point[1].y = winRect.bottom;
1979
1980 MapWindowPoints(HWND_DESKTOP, infoPtr->hwndSelf, point, 2);
1981 point[0].x = -nNewScrollPos;
1982 point[1].x += nNewScrollPos;
1983
1984 SetWindowPos(infoPtr->hwndHeader,0,
1985 point[0].x,point[0].y,point[1].x,point[1].y,
1986 (infoPtr->dwStyle & LVS_NOCOLUMNHEADER) ? SWP_HIDEWINDOW : SWP_SHOWWINDOW |
1987 SWP_NOZORDER | SWP_NOACTIVATE);
1988 }
1989
1990 /***
1991 * DESCRIPTION:
1992 * Update the scrollbars. This functions should be called whenever
1993 * the content, size or view changes.
1994 *
1995 * PARAMETER(S):
1996 * [I] infoPtr : valid pointer to the listview structure
1997 *
1998 * RETURN:
1999 * None
2000 */
2001 static void LISTVIEW_UpdateScroll(const LISTVIEW_INFO *infoPtr)
2002 {
2003 SCROLLINFO horzInfo, vertInfo;
2004 INT dx, dy;
2005
2006 if ((infoPtr->dwStyle & LVS_NOSCROLL) || !is_redrawing(infoPtr)) return;
2007
2008 ZeroMemory(&horzInfo, sizeof(SCROLLINFO));
2009 horzInfo.cbSize = sizeof(SCROLLINFO);
2010 horzInfo.nPage = infoPtr->rcList.right - infoPtr->rcList.left;
2011
2012 /* for now, we'll set info.nMax to the _count_, and adjust it later */
2013 if (infoPtr->uView == LV_VIEW_LIST)
2014 {
2015 INT nPerCol = LISTVIEW_GetCountPerColumn(infoPtr);
2016 horzInfo.nMax = (infoPtr->nItemCount + nPerCol - 1) / nPerCol;
2017
2018 /* scroll by at least one column per page */
2019 if(horzInfo.nPage < infoPtr->nItemWidth)
2020 horzInfo.nPage = infoPtr->nItemWidth;
2021
2022 if (infoPtr->nItemWidth)
2023 horzInfo.nPage /= infoPtr->nItemWidth;
2024 }
2025 else if (infoPtr->uView == LV_VIEW_DETAILS)
2026 {
2027 horzInfo.nMax = infoPtr->nItemWidth;
2028 }
2029 else /* LV_VIEW_ICON, or LV_VIEW_SMALLICON */
2030 {
2031 RECT rcView;
2032
2033 if (LISTVIEW_GetViewRect(infoPtr, &rcView)) horzInfo.nMax = rcView.right - rcView.left;
2034 }
2035
2036 if (LISTVIEW_IsHeaderEnabled(infoPtr))
2037 {
2038 if (DPA_GetPtrCount(infoPtr->hdpaColumns))
2039 {
2040 RECT rcHeader;
2041 INT index;
2042
2043 index = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX,
2044 DPA_GetPtrCount(infoPtr->hdpaColumns) - 1, 0);
2045
2046 LISTVIEW_GetHeaderRect(infoPtr, index, &rcHeader);
2047 horzInfo.nMax = rcHeader.right;
2048 TRACE("horzInfo.nMax=%d\n", horzInfo.nMax);
2049 }
2050 }
2051
2052 horzInfo.fMask = SIF_RANGE | SIF_PAGE;
2053 horzInfo.nMax = max(horzInfo.nMax - 1, 0);
2054 dx = GetScrollPos(infoPtr->hwndSelf, SB_HORZ);
2055 dx -= SetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &horzInfo, TRUE);
2056 TRACE("horzInfo=%s\n", debugscrollinfo(&horzInfo));
2057
2058 /* Setting the horizontal scroll can change the listview size
2059 * (and potentially everything else) so we need to recompute
2060 * everything again for the vertical scroll
2061 */
2062
2063 ZeroMemory(&vertInfo, sizeof(SCROLLINFO));
2064 vertInfo.cbSize = sizeof(SCROLLINFO);
2065 vertInfo.nPage = infoPtr->rcList.bottom - infoPtr->rcList.top;
2066
2067 if (infoPtr->uView == LV_VIEW_DETAILS)
2068 {
2069 vertInfo.nMax = infoPtr->nItemCount;
2070
2071 /* scroll by at least one page */
2072 if(vertInfo.nPage < infoPtr->nItemHeight)
2073 vertInfo.nPage = infoPtr->nItemHeight;
2074
2075 if (infoPtr->nItemHeight > 0)
2076 vertInfo.nPage /= infoPtr->nItemHeight;
2077 }
2078 else if (infoPtr->uView != LV_VIEW_LIST) /* LV_VIEW_ICON, or LV_VIEW_SMALLICON */
2079 {
2080 RECT rcView;
2081
2082 if (LISTVIEW_GetViewRect(infoPtr, &rcView)) vertInfo.nMax = rcView.bottom - rcView.top;
2083 }
2084
2085 vertInfo.fMask = SIF_RANGE | SIF_PAGE;
2086 vertInfo.nMax = max(vertInfo.nMax - 1, 0);
2087 dy = GetScrollPos(infoPtr->hwndSelf, SB_VERT);
2088 dy -= SetScrollInfo(infoPtr->hwndSelf, SB_VERT, &vertInfo, TRUE);
2089 TRACE("vertInfo=%s\n", debugscrollinfo(&vertInfo));
2090
2091 /* Change of the range may have changed the scroll pos. If so move the content */
2092 if (dx != 0 || dy != 0)
2093 {
2094 RECT listRect;
2095 listRect = infoPtr->rcList;
2096 ScrollWindowEx(infoPtr->hwndSelf, dx, dy, &listRect, &listRect, 0, 0,
2097 SW_ERASE | SW_INVALIDATE);
2098 }
2099
2100 /* Update the Header Control */
2101 if (infoPtr->hwndHeader)
2102 {
2103 horzInfo.fMask = SIF_POS;
2104 GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &horzInfo);
2105 LISTVIEW_UpdateHeaderSize(infoPtr, horzInfo.nPos);
2106 }
2107 }
2108
2109
2110 /***
2111 * DESCRIPTION:
2112 * Shows/hides the focus rectangle.
2113 *
2114 * PARAMETER(S):
2115 * [I] infoPtr : valid pointer to the listview structure
2116 * [I] fShow : TRUE to show the focus, FALSE to hide it.
2117 *
2118 * RETURN:
2119 * None
2120 */
2121 static void LISTVIEW_ShowFocusRect(const LISTVIEW_INFO *infoPtr, BOOL fShow)
2122 {
2123 HDC hdc;
2124
2125 TRACE("fShow=%d, nItem=%d\n", fShow, infoPtr->nFocusedItem);
2126
2127 if (infoPtr->nFocusedItem < 0) return;
2128
2129 /* we need some gymnastics in ICON mode to handle large items */
2130 if (infoPtr->uView == LV_VIEW_ICON)
2131 {
2132 RECT rcBox;
2133
2134 LISTVIEW_GetItemBox(infoPtr, infoPtr->nFocusedItem, &rcBox);
2135 if ((rcBox.bottom - rcBox.top) > infoPtr->nItemHeight)
2136 {
2137 LISTVIEW_InvalidateRect(infoPtr, &rcBox);
2138 return;
2139 }
2140 }
2141
2142 if (!(hdc = GetDC(infoPtr->hwndSelf))) return;
2143
2144 /* for some reason, owner draw should work only in report mode */
2145 if ((infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && (infoPtr->uView == LV_VIEW_DETAILS))
2146 {
2147 DRAWITEMSTRUCT dis;
2148 LVITEMW item;
2149
2150 HFONT hFont = infoPtr->hFont ? infoPtr->hFont : infoPtr->hDefaultFont;
2151 HFONT hOldFont = SelectObject(hdc, hFont);
2152
2153 item.iItem = infoPtr->nFocusedItem;
2154 item.iSubItem = 0;
2155 item.mask = LVIF_PARAM;
2156 if (!LISTVIEW_GetItemW(infoPtr, &item)) goto done;
2157
2158 ZeroMemory(&dis, sizeof(dis));
2159 dis.CtlType = ODT_LISTVIEW;
2160 dis.CtlID = (UINT)GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
2161 dis.itemID = item.iItem;
2162 dis.itemAction = ODA_FOCUS;
2163 if (fShow) dis.itemState |= ODS_FOCUS;
2164 dis.hwndItem = infoPtr->hwndSelf;
2165 dis.hDC = hdc;
2166 LISTVIEW_GetItemBox(infoPtr, dis.itemID, &dis.rcItem);
2167 dis.itemData = item.lParam;
2168
2169 SendMessageW(infoPtr->hwndNotify, WM_DRAWITEM, dis.CtlID, (LPARAM)&dis);
2170
2171 SelectObject(hdc, hOldFont);
2172 }
2173 else
2174 {
2175 LISTVIEW_DrawFocusRect(infoPtr, hdc);
2176 }
2177 done:
2178 ReleaseDC(infoPtr->hwndSelf, hdc);
2179 }
2180
2181 /***
2182 * Invalidates all visible selected items.
2183 */
2184 static void LISTVIEW_InvalidateSelectedItems(const LISTVIEW_INFO *infoPtr)
2185 {
2186 ITERATOR i;
2187
2188 iterator_frameditems(&i, infoPtr, &infoPtr->rcList);
2189 while(iterator_next(&i))
2190 {
2191 if (LISTVIEW_GetItemState(infoPtr, i.nItem, LVIS_SELECTED))
2192 LISTVIEW_InvalidateItem(infoPtr, i.nItem);
2193 }
2194 iterator_destroy(&i);
2195 }
2196
2197
2198 /***
2199 * DESCRIPTION: [INTERNAL]
2200 * Computes an item's (left,top) corner, relative to rcView.
2201 * That is, the position has NOT been made relative to the Origin.
2202 * This is deliberate, to avoid computing the Origin over, and
2203 * over again, when this function is called in a loop. Instead,
2204 * one can factor the computation of the Origin before the loop,
2205 * and offset the value returned by this function, on every iteration.
2206 *
2207 * PARAMETER(S):
2208 * [I] infoPtr : valid pointer to the listview structure
2209 * [I] nItem : item number
2210 * [O] lpptOrig : item top, left corner
2211 *
2212 * RETURN:
2213 * None.
2214 */
2215 static void LISTVIEW_GetItemOrigin(const LISTVIEW_INFO *infoPtr, INT nItem, LPPOINT lpptPosition)
2216 {
2217 assert(nItem >= 0 && nItem < infoPtr->nItemCount);
2218
2219 if ((infoPtr->uView == LV_VIEW_SMALLICON) || (infoPtr->uView == LV_VIEW_ICON))
2220 {
2221 lpptPosition->x = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosX, nItem);
2222 lpptPosition->y = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosY, nItem);
2223 }
2224 else if (infoPtr->uView == LV_VIEW_LIST)
2225 {
2226 INT nCountPerColumn = LISTVIEW_GetCountPerColumn(infoPtr);
2227 lpptPosition->x = nItem / nCountPerColumn * infoPtr->nItemWidth;
2228 lpptPosition->y = nItem % nCountPerColumn * infoPtr->nItemHeight;
2229 }
2230 else /* LV_VIEW_DETAILS */
2231 {
2232 lpptPosition->x = REPORT_MARGINX;
2233 /* item is always at zero indexed column */
2234 if (DPA_GetPtrCount(infoPtr->hdpaColumns) > 0)
2235 lpptPosition->x += LISTVIEW_GetColumnInfo(infoPtr, 0)->rcHeader.left;
2236 lpptPosition->y = nItem * infoPtr->nItemHeight;
2237 }
2238 }
2239
2240 /***
2241 * DESCRIPTION: [INTERNAL]
2242 * Compute the rectangles of an item. This is to localize all
2243 * the computations in one place. If you are not interested in some
2244 * of these values, simply pass in a NULL -- the function is smart
2245 * enough to compute only what's necessary. The function computes
2246 * the standard rectangles (BOUNDS, ICON, LABEL) plus a non-standard
2247 * one, the BOX rectangle. This rectangle is very cheap to compute,
2248 * and is guaranteed to contain all the other rectangles. Computing
2249 * the ICON rect is also cheap, but all the others are potentially
2250 * expensive. This gives an easy and effective optimization when
2251 * searching (like point inclusion, or rectangle intersection):
2252 * first test against the BOX, and if TRUE, test against the desired
2253 * rectangle.
2254 * If the function does not have all the necessary information
2255 * to computed the requested rectangles, will crash with a
2256 * failed assertion. This is done so we catch all programming
2257 * errors, given that the function is called only from our code.
2258 *
2259 * We have the following 'special' meanings for a few fields:
2260 * * If LVIS_FOCUSED is set, we assume the item has the focus
2261 * This is important in ICON mode, where it might get a larger
2262 * then usual rectangle
2263 *
2264 * Please note that subitem support works only in REPORT mode.
2265 *
2266 * PARAMETER(S):
2267 * [I] infoPtr : valid pointer to the listview structure
2268 * [I] lpLVItem : item to compute the measures for
2269 * [O] lprcBox : ptr to Box rectangle
2270 * Same as LVM_GETITEMRECT with LVIR_BOUNDS
2271 * [0] lprcSelectBox : ptr to select box rectangle
2272 * Same as LVM_GETITEMRECT with LVIR_SELECTEDBOUNDS
2273 * [O] lprcIcon : ptr to Icon rectangle
2274 * Same as LVM_GETITEMRECT with LVIR_ICON
2275 * [O] lprcStateIcon: ptr to State Icon rectangle
2276 * [O] lprcLabel : ptr to Label rectangle
2277 * Same as LVM_GETITEMRECT with LVIR_LABEL
2278 *
2279 * RETURN:
2280 * None.
2281 */
2282 static void LISTVIEW_GetItemMetrics(const LISTVIEW_INFO *infoPtr, const LVITEMW *lpLVItem,
2283 LPRECT lprcBox, LPRECT lprcSelectBox,
2284 LPRECT lprcIcon, LPRECT lprcStateIcon, LPRECT lprcLabel)
2285 {
2286 BOOL doSelectBox = FALSE, doIcon = FALSE, doLabel = FALSE, oversizedBox = FALSE;
2287 RECT Box, SelectBox, Icon, Label;
2288 COLUMN_INFO *lpColumnInfo = NULL;
2289 SIZE labelSize = { 0, 0 };
2290
2291 TRACE("(lpLVItem=%s)\n", debuglvitem_t(lpLVItem, TRUE));
2292
2293 /* Be smart and try to figure out the minimum we have to do */
2294 if (lpLVItem->iSubItem) assert(infoPtr->uView == LV_VIEW_DETAILS);
2295 if (infoPtr->uView == LV_VIEW_ICON && (lprcBox || lprcLabel))
2296 {
2297 assert((lpLVItem->mask & LVIF_STATE) && (lpLVItem->stateMask & LVIS_FOCUSED));
2298 if (lpLVItem->state & LVIS_FOCUSED) oversizedBox = doLabel = TRUE;
2299 }
2300 if (lprcSelectBox) doSelectBox = TRUE;
2301 if (lprcLabel) doLabel = TRUE;
2302 if (doLabel || lprcIcon || lprcStateIcon) doIcon = TRUE;
2303 if (doSelectBox)
2304 {
2305 doIcon = TRUE;
2306 doLabel = TRUE;
2307 }
2308
2309 /************************************************************/
2310 /* compute the box rectangle (it should be cheap to do) */
2311 /************************************************************/
2312 if (lpLVItem->iSubItem || infoPtr->uView == LV_VIEW_DETAILS)
2313 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, lpLVItem->iSubItem);
2314
2315 if (lpLVItem->iSubItem)
2316 {
2317 Box = lpColumnInfo->rcHeader;
2318 }
2319 else
2320 {
2321 Box.left = 0;
2322 Box.right = infoPtr->nItemWidth;
2323 }
2324 Box.top = 0;
2325 Box.bottom = infoPtr->nItemHeight;
2326
2327 /******************************************************************/
2328 /* compute ICON bounding box (ala LVM_GETITEMRECT) and STATEICON */
2329 /******************************************************************/
2330 if (doIcon)
2331 {
2332 LONG state_width = 0;
2333
2334 if (infoPtr->himlState && lpLVItem->iSubItem == 0)
2335 state_width = infoPtr->iconStateSize.cx;
2336
2337 if (infoPtr->uView == LV_VIEW_ICON)
2338 {
2339 Icon.left = Box.left + state_width;
2340 if (infoPtr->himlNormal)
2341 Icon.left += (infoPtr->nItemWidth - infoPtr->iconSize.cx - state_width) / 2;
2342 Icon.top = Box.top + ICON_TOP_PADDING;
2343 Icon.right = Icon.left;
2344 Icon.bottom = Icon.top;
2345 if (infoPtr->himlNormal)
2346 {
2347 Icon.right += infoPtr->iconSize.cx;
2348 Icon.bottom += infoPtr->iconSize.cy;
2349 }
2350 }
2351 else /* LV_VIEW_SMALLICON, LV_VIEW_LIST or LV_VIEW_DETAILS */
2352 {
2353 Icon.left = Box.left + state_width;
2354
2355 if (infoPtr->uView == LV_VIEW_DETAILS && lpLVItem->iSubItem == 0)
2356 {
2357 /* we need the indent in report mode */
2358 assert(lpLVItem->mask & LVIF_INDENT);
2359 Icon.left += infoPtr->iconSize.cx * lpLVItem->iIndent + REPORT_MARGINX;
2360 }
2361
2362 Icon.top = Box.top;
2363 Icon.right = Icon.left;
2364 if (infoPtr->himlSmall &&
2365 (!lpColumnInfo || lpLVItem->iSubItem == 0 ||
2366 ((infoPtr->dwLvExStyle & LVS_EX_SUBITEMIMAGES) && lpLVItem->iImage != I_IMAGECALLBACK)))
2367 Icon.right += infoPtr->iconSize.cx;
2368 Icon.bottom = Icon.top + infoPtr->iconSize.cy;
2369 }
2370 if(lprcIcon) *lprcIcon = Icon;
2371 TRACE(" - icon=%s\n", wine_dbgstr_rect(&Icon));
2372
2373 /* TODO: is this correct? */
2374 if (lprcStateIcon)
2375 {
2376 lprcStateIcon->left = Icon.left - state_width;
2377 lprcStateIcon->right = Icon.left;
2378 lprcStateIcon->top = Icon.top;
2379 lprcStateIcon->bottom = lprcStateIcon->top + infoPtr->iconSize.cy;
2380 TRACE(" - state icon=%s\n", wine_dbgstr_rect(lprcStateIcon));
2381 }
2382 }
2383 else Icon.right = 0;
2384
2385 /************************************************************/
2386 /* compute LABEL bounding box (ala LVM_GETITEMRECT) */
2387 /************************************************************/
2388 if (doLabel)
2389 {
2390 /* calculate how far to the right can the label stretch */
2391 Label.right = Box.right;
2392 if (infoPtr->uView == LV_VIEW_DETAILS)
2393 {
2394 if (lpLVItem->iSubItem == 0)
2395 {
2396 /* we need a zero based rect here */
2397 Label = lpColumnInfo->rcHeader;
2398 OffsetRect(&Label, -Label.left, 0);
2399 }
2400 }
2401
2402 if (lpLVItem->iSubItem || ((infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && infoPtr->uView == LV_VIEW_DETAILS))
2403 {
2404 labelSize.cx = infoPtr->nItemWidth;
2405 labelSize.cy = infoPtr->nItemHeight;
2406 goto calc_label;
2407 }
2408
2409 /* we need the text in non owner draw mode */
2410 assert(lpLVItem->mask & LVIF_TEXT);
2411 if (is_text(lpLVItem->pszText))
2412 {
2413 HFONT hFont = infoPtr->hFont ? infoPtr->hFont : infoPtr->hDefaultFont;
2414 HDC hdc = GetDC(infoPtr->hwndSelf);
2415 HFONT hOldFont = SelectObject(hdc, hFont);
2416 UINT uFormat;
2417 RECT rcText;
2418
2419 /* compute rough rectangle where the label will go */
2420 SetRectEmpty(&rcText);
2421 rcText.right = infoPtr->nItemWidth - TRAILING_LABEL_PADDING;
2422 rcText.bottom = infoPtr->nItemHeight;
2423 if (infoPtr->uView == LV_VIEW_ICON)
2424 rcText.bottom -= ICON_TOP_PADDING + infoPtr->iconSize.cy + ICON_BOTTOM_PADDING;
2425
2426 /* now figure out the flags */
2427 if (infoPtr->uView == LV_VIEW_ICON)
2428 uFormat = oversizedBox ? LV_FL_DT_FLAGS : LV_ML_DT_FLAGS;
2429 else
2430 uFormat = LV_SL_DT_FLAGS;
2431
2432 DrawTextW (hdc, lpLVItem->pszText, -1, &rcText, uFormat | DT_CALCRECT);
2433
2434 if (rcText.right != rcText.left)
2435 labelSize.cx = min(rcText.right - rcText.left + TRAILING_LABEL_PADDING, infoPtr->nItemWidth);
2436
2437 labelSize.cy = rcText.bottom - rcText.top;
2438
2439 SelectObject(hdc, hOldFont);
2440 ReleaseDC(infoPtr->hwndSelf, hdc);
2441 }
2442
2443 calc_label:
2444 if (infoPtr->uView == LV_VIEW_ICON)
2445 {
2446 Label.left = Box.left + (infoPtr->nItemWidth - labelSize.cx) / 2;
2447 Label.top = Box.top + ICON_TOP_PADDING_HITABLE +
2448 infoPtr->iconSize.cy + ICON_BOTTOM_PADDING;
2449 Label.right = Label.left + labelSize.cx;
2450 Label.bottom = Label.top + infoPtr->nItemHeight;
2451 if (!oversizedBox && labelSize.cy > infoPtr->ntmHeight)
2452 {
2453 labelSize.cy = min(Box.bottom - Label.top, labelSize.cy);
2454 labelSize.cy /= infoPtr->ntmHeight;
2455 labelSize.cy = max(labelSize.cy, 1);
2456 labelSize.cy *= infoPtr->ntmHeight;
2457 }
2458 Label.bottom = Label.top + labelSize.cy + HEIGHT_PADDING;
2459 }
2460 else if (infoPtr->uView == LV_VIEW_DETAILS)
2461 {
2462 Label.left = Icon.right;
2463 Label.top = Box.top;
2464 Label.right = lpLVItem->iSubItem ? lpColumnInfo->rcHeader.right :
2465 lpColumnInfo->rcHeader.right - lpColumnInfo->rcHeader.left;
2466 Label.bottom = Label.top + infoPtr->nItemHeight;
2467 }
2468 else /* LV_VIEW_SMALLICON or LV_VIEW_LIST */
2469 {
2470 Label.left = Icon.right;
2471 Label.top = Box.top;
2472 Label.right = min(Label.left + labelSize.cx, Label.right);
2473 Label.bottom = Label.top + infoPtr->nItemHeight;
2474 }
2475
2476 if (lprcLabel) *lprcLabel = Label;
2477 TRACE(" - label=%s\n", wine_dbgstr_rect(&Label));
2478 }
2479
2480 /************************************************************/
2481 /* compute SELECT bounding box */
2482 /************************************************************/
2483 if (doSelectBox)
2484 {
2485 if (infoPtr->uView == LV_VIEW_DETAILS)
2486 {
2487 SelectBox.left = Icon.left;
2488 SelectBox.top = Box.top;
2489 SelectBox.bottom = Box.bottom;
2490
2491 if (labelSize.cx)
2492 SelectBox.right = min(Label.left + labelSize.cx, Label.right);
2493 else
2494 SelectBox.right = min(Label.left + MAX_EMPTYTEXT_SELECT_WIDTH, Label.right);
2495 }
2496 else
2497 {
2498 UnionRect(&SelectBox, &Icon, &Label);
2499 }
2500 if (lprcSelectBox) *lprcSelectBox = SelectBox;
2501 TRACE(" - select box=%s\n", wine_dbgstr_rect(&SelectBox));
2502 }
2503
2504 /* Fix the Box if necessary */
2505 if (lprcBox)
2506 {
2507 if (oversizedBox) UnionRect(lprcBox, &Box, &Label);
2508 else *lprcBox = Box;
2509 }
2510 TRACE(" - box=%s\n", wine_dbgstr_rect(&Box));
2511 }
2512
2513 /***
2514 * DESCRIPTION: [INTERNAL]
2515 *
2516 * PARAMETER(S):
2517 * [I] infoPtr : valid pointer to the listview structure
2518 * [I] nItem : item number
2519 * [O] lprcBox : ptr to Box rectangle
2520 *
2521 * RETURN:
2522 * None.
2523 */
2524 static void LISTVIEW_GetItemBox(const LISTVIEW_INFO *infoPtr, INT nItem, LPRECT lprcBox)
2525 {
2526 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
2527 POINT Position, Origin;
2528 LVITEMW lvItem;
2529
2530 LISTVIEW_GetOrigin(infoPtr, &Origin);
2531 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position);
2532
2533 /* Be smart and try to figure out the minimum we have to do */
2534 lvItem.mask = 0;
2535 if (infoPtr->uView == LV_VIEW_ICON && infoPtr->bFocus && LISTVIEW_GetItemState(infoPtr, nItem, LVIS_FOCUSED))
2536 lvItem.mask |= LVIF_TEXT;
2537 lvItem.iItem = nItem;
2538 lvItem.iSubItem = 0;
2539 lvItem.pszText = szDispText;
2540 lvItem.cchTextMax = DISP_TEXT_SIZE;
2541 if (lvItem.mask) LISTVIEW_GetItemW(infoPtr, &lvItem);
2542 if (infoPtr->uView == LV_VIEW_ICON)
2543 {
2544 lvItem.mask |= LVIF_STATE;
2545 lvItem.stateMask = LVIS_FOCUSED;
2546 lvItem.state = (lvItem.mask & LVIF_TEXT ? LVIS_FOCUSED : 0);
2547 }
2548 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, lprcBox, 0, 0, 0, 0);
2549
2550 if (infoPtr->uView == LV_VIEW_DETAILS && infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT &&
2551 SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX, 0, 0))
2552 {
2553 OffsetRect(lprcBox, Origin.x, Position.y + Origin.y);
2554 }
2555 else
2556 OffsetRect(lprcBox, Position.x + Origin.x, Position.y + Origin.y);
2557 }
2558
2559 /* LISTVIEW_MapIdToIndex helper */
2560 static INT CALLBACK MapIdSearchCompare(LPVOID p1, LPVOID p2, LPARAM lParam)
2561 {
2562 ITEM_ID *id1 = (ITEM_ID*)p1;
2563 ITEM_ID *id2 = (ITEM_ID*)p2;
2564
2565 if (id1->id == id2->id) return 0;
2566
2567 return (id1->id < id2->id) ? -1 : 1;
2568 }
2569
2570 /***
2571 * DESCRIPTION:
2572 * Returns the item index for id specified.
2573 *
2574 * PARAMETER(S):
2575 * [I] infoPtr : valid pointer to the listview structure
2576 * [I] iID : item id to get index for
2577 *
2578 * RETURN:
2579 * Item index, or -1 on failure.
2580 */
2581 static INT LISTVIEW_MapIdToIndex(const LISTVIEW_INFO *infoPtr, UINT iID)
2582 {
2583 ITEM_ID ID;
2584 INT index;
2585
2586 TRACE("iID=%d\n", iID);
2587
2588 if (infoPtr->dwStyle & LVS_OWNERDATA) return -1;
2589 if (infoPtr->nItemCount == 0) return -1;
2590
2591 ID.id = iID;
2592 index = DPA_Search(infoPtr->hdpaItemIds, &ID, -1, MapIdSearchCompare, 0, DPAS_SORTED);
2593
2594 if (index != -1)
2595 {
2596 ITEM_ID *lpID = DPA_GetPtr(infoPtr->hdpaItemIds, index);
2597 return DPA_GetPtrIndex(infoPtr->hdpaItems, lpID->item);
2598 }
2599
2600 return -1;
2601 }
2602
2603 /***
2604 * DESCRIPTION:
2605 * Returns the item id for index given.
2606 *
2607 * PARAMETER(S):
2608 * [I] infoPtr : valid pointer to the listview structure
2609 * [I] iItem : item index to get id for
2610 *
2611 * RETURN:
2612 * Item id.
2613 */
2614 static DWORD LISTVIEW_MapIndexToId(const LISTVIEW_INFO *infoPtr, INT iItem)
2615 {
2616 ITEM_INFO *lpItem;
2617 HDPA hdpaSubItems;
2618
2619 TRACE("iItem=%d\n", iItem);
2620
2621 if (infoPtr->dwStyle & LVS_OWNERDATA) return -1;
2622 if (iItem < 0 || iItem >= infoPtr->nItemCount) return -1;
2623
2624 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, iItem);
2625 lpItem = DPA_GetPtr(hdpaSubItems, 0);
2626
2627 return lpItem->id->id;
2628 }
2629
2630 /***
2631 * DESCRIPTION:
2632 * Returns the current icon position, and advances it along the top.
2633 * The returned position is not offset by Origin.
2634 *
2635 * PARAMETER(S):
2636 * [I] infoPtr : valid pointer to the listview structure
2637 * [O] lpPos : will get the current icon position
2638 *
2639 * RETURN:
2640 * None
2641 */
2642 static void LISTVIEW_NextIconPosTop(LISTVIEW_INFO *infoPtr, LPPOINT lpPos)
2643 {
2644 INT nListWidth = infoPtr->rcList.right - infoPtr->rcList.left;
2645
2646 *lpPos = infoPtr->currIconPos;
2647
2648 infoPtr->currIconPos.x += infoPtr->nItemWidth;
2649 if (infoPtr->currIconPos.x + infoPtr->nItemWidth <= nListWidth) return;
2650
2651 infoPtr->currIconPos.x = 0;
2652 infoPtr->currIconPos.y += infoPtr->nItemHeight;
2653 }
2654
2655
2656 /***
2657 * DESCRIPTION:
2658 * Returns the current icon position, and advances it down the left edge.
2659 * The returned position is not offset by Origin.
2660 *
2661 * PARAMETER(S):
2662 * [I] infoPtr : valid pointer to the listview structure
2663 * [O] lpPos : will get the current icon position
2664 *
2665 * RETURN:
2666 * None
2667 */
2668 static void LISTVIEW_NextIconPosLeft(LISTVIEW_INFO *infoPtr, LPPOINT lpPos)
2669 {
2670 INT nListHeight = infoPtr->rcList.bottom - infoPtr->rcList.top;
2671
2672 *lpPos = infoPtr->currIconPos;
2673
2674 infoPtr->currIconPos.y += infoPtr->nItemHeight;
2675 if (infoPtr->currIconPos.y + infoPtr->nItemHeight <= nListHeight) return;
2676
2677 infoPtr->currIconPos.x += infoPtr->nItemWidth;
2678 infoPtr->currIconPos.y = 0;
2679 }
2680
2681
2682 /***
2683 * DESCRIPTION:
2684 * Moves an icon to the specified position.
2685 * It takes care of invalidating the item, etc.
2686 *
2687 * PARAMETER(S):
2688 * [I] infoPtr : valid pointer to the listview structure
2689 * [I] nItem : the item to move
2690 * [I] lpPos : the new icon position
2691 * [I] isNew : flags the item as being new
2692 *
2693 * RETURN:
2694 * Success: TRUE
2695 * Failure: FALSE
2696 */
2697 static BOOL LISTVIEW_MoveIconTo(const LISTVIEW_INFO *infoPtr, INT nItem, const POINT *lppt, BOOL isNew)
2698 {
2699 POINT old;
2700
2701 if (!isNew)
2702 {
2703 old.x = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosX, nItem);
2704 old.y = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosY, nItem);
2705
2706 if (lppt->x == old.x && lppt->y == old.y) return TRUE;
2707 LISTVIEW_InvalidateItem(infoPtr, nItem);
2708 }
2709
2710 /* Allocating a POINTER for every item is too resource intensive,
2711 * so we'll keep the (x,y) in different arrays */
2712 if (!DPA_SetPtr(infoPtr->hdpaPosX, nItem, (void *)(LONG_PTR)lppt->x)) return FALSE;
2713 if (!DPA_SetPtr(infoPtr->hdpaPosY, nItem, (void *)(LONG_PTR)lppt->y)) return FALSE;
2714
2715 LISTVIEW_InvalidateItem(infoPtr, nItem);
2716
2717 return TRUE;
2718 }
2719
2720 /***
2721 * DESCRIPTION:
2722 * Arranges listview items in icon display mode.
2723 *
2724 * PARAMETER(S):
2725 * [I] infoPtr : valid pointer to the listview structure
2726 * [I] nAlignCode : alignment code
2727 *
2728 * RETURN:
2729 * SUCCESS : TRUE
2730 * FAILURE : FALSE
2731 */
2732 static BOOL LISTVIEW_Arrange(LISTVIEW_INFO *infoPtr, INT nAlignCode)
2733 {
2734 void (*next_pos)(LISTVIEW_INFO *, LPPOINT);
2735 POINT pos;
2736 INT i;
2737
2738 if (infoPtr->uView != LV_VIEW_ICON && infoPtr->uView != LV_VIEW_SMALLICON) return FALSE;
2739
2740 TRACE("nAlignCode=%d\n", nAlignCode);
2741
2742 if (nAlignCode == LVA_DEFAULT)
2743 {
2744 if (infoPtr->dwStyle & LVS_ALIGNLEFT) nAlignCode = LVA_ALIGNLEFT;
2745 else nAlignCode = LVA_ALIGNTOP;
2746 }
2747
2748 switch (nAlignCode)
2749 {
2750 case LVA_ALIGNLEFT: next_pos = LISTVIEW_NextIconPosLeft; break;
2751 case LVA_ALIGNTOP: next_pos = LISTVIEW_NextIconPosTop; break;
2752 case LVA_SNAPTOGRID: next_pos = LISTVIEW_NextIconPosTop; break; /* FIXME */
2753 default: return FALSE;
2754 }
2755
2756 infoPtr->bAutoarrange = TRUE;
2757 infoPtr->currIconPos.x = infoPtr->currIconPos.y = 0;
2758 for (i = 0; i < infoPtr->nItemCount; i++)
2759 {
2760 next_pos(infoPtr, &pos);
2761 LISTVIEW_MoveIconTo(infoPtr, i, &pos, FALSE);
2762 }
2763
2764 return TRUE;
2765 }
2766
2767 /***
2768 * DESCRIPTION:
2769 * Retrieves the bounding rectangle of all the items, not offset by Origin.
2770 * For LVS_REPORT always returns empty rectangle.
2771 *
2772 * PARAMETER(S):
2773 * [I] infoPtr : valid pointer to the listview structure
2774 * [O] lprcView : bounding rectangle
2775 *
2776 * RETURN:
2777 * SUCCESS : TRUE
2778 * FAILURE : FALSE
2779 */
2780 static void LISTVIEW_GetAreaRect(const LISTVIEW_INFO *infoPtr, LPRECT lprcView)
2781 {
2782 INT i, x, y;
2783
2784 SetRectEmpty(lprcView);
2785
2786 switch (infoPtr->uView)
2787 {
2788 case LV_VIEW_ICON:
2789 case LV_VIEW_SMALLICON:
2790 for (i = 0; i < infoPtr->nItemCount; i++)
2791 {
2792 x = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosX, i);
2793 y = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosY, i);
2794 lprcView->right = max(lprcView->right, x);
2795 lprcView->bottom = max(lprcView->bottom, y);
2796 }
2797 if (infoPtr->nItemCount > 0)
2798 {
2799 lprcView->right += infoPtr->nItemWidth;
2800 lprcView->bottom += infoPtr->nItemHeight;
2801 }
2802 break;
2803
2804 case LV_VIEW_LIST:
2805 y = LISTVIEW_GetCountPerColumn(infoPtr);
2806 x = infoPtr->nItemCount / y;
2807 if (infoPtr->nItemCount % y) x++;
2808 lprcView->right = x * infoPtr->nItemWidth;
2809 lprcView->bottom = y * infoPtr->nItemHeight;
2810 break;
2811 }
2812 }
2813
2814 /***
2815 * DESCRIPTION:
2816 * Retrieves the bounding rectangle of all the items.
2817 *
2818 * PARAMETER(S):
2819 * [I] infoPtr : valid pointer to the listview structure
2820 * [O] lprcView : bounding rectangle
2821 *
2822 * RETURN:
2823 * SUCCESS : TRUE
2824 * FAILURE : FALSE
2825 */
2826 static BOOL LISTVIEW_GetViewRect(const LISTVIEW_INFO *infoPtr, LPRECT lprcView)
2827 {
2828 POINT ptOrigin;
2829
2830 TRACE("(lprcView=%p)\n", lprcView);
2831
2832 if (!lprcView) return FALSE;
2833
2834 LISTVIEW_GetAreaRect(infoPtr, lprcView);
2835
2836 if (infoPtr->uView != LV_VIEW_DETAILS)
2837 {
2838 LISTVIEW_GetOrigin(infoPtr, &ptOrigin);
2839 OffsetRect(lprcView, ptOrigin.x, ptOrigin.y);
2840 }
2841
2842 TRACE("lprcView=%s\n", wine_dbgstr_rect(lprcView));
2843
2844 return TRUE;
2845 }
2846
2847 /***
2848 * DESCRIPTION:
2849 * Retrieves the subitem pointer associated with the subitem index.
2850 *
2851 * PARAMETER(S):
2852 * [I] hdpaSubItems : DPA handle for a specific item
2853 * [I] nSubItem : index of subitem
2854 *
2855 * RETURN:
2856 * SUCCESS : subitem pointer
2857 * FAILURE : NULL
2858 */
2859 static SUBITEM_INFO* LISTVIEW_GetSubItemPtr(HDPA hdpaSubItems, INT nSubItem)
2860 {
2861 SUBITEM_INFO *lpSubItem;
2862 INT i;
2863
2864 /* we should binary search here if need be */
2865 for (i = 1; i < DPA_GetPtrCount(hdpaSubItems); i++)
2866 {
2867 lpSubItem = DPA_GetPtr(hdpaSubItems, i);
2868 if (lpSubItem->iSubItem == nSubItem)
2869 return lpSubItem;
2870 }
2871
2872 return NULL;
2873 }
2874
2875
2876 /***
2877 * DESCRIPTION:
2878 * Calculates the desired item width.
2879 *
2880 * PARAMETER(S):
2881 * [I] infoPtr : valid pointer to the listview structure
2882 *
2883 * RETURN:
2884 * The desired item width.
2885 */
2886 static INT LISTVIEW_CalculateItemWidth(const LISTVIEW_INFO *infoPtr)
2887 {
2888 INT nItemWidth = 0;
2889
2890 TRACE("uView=%d\n", infoPtr->uView);
2891
2892 if (infoPtr->uView == LV_VIEW_ICON)
2893 nItemWidth = infoPtr->iconSpacing.cx;
2894 else if (infoPtr->uView == LV_VIEW_DETAILS)
2895 {
2896 if (DPA_GetPtrCount(infoPtr->hdpaColumns) > 0)
2897 {
2898 RECT rcHeader;
2899 INT index;
2900
2901 index = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX,
2902 DPA_GetPtrCount(infoPtr->hdpaColumns) - 1, 0);
2903
2904 LISTVIEW_GetHeaderRect(infoPtr, index, &rcHeader);
2905 nItemWidth = rcHeader.right;
2906 }
2907 }
2908 else /* LV_VIEW_SMALLICON, or LV_VIEW_LIST */
2909 {
2910 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
2911 LVITEMW lvItem;
2912 INT i;
2913
2914 lvItem.mask = LVIF_TEXT;
2915 lvItem.iSubItem = 0;
2916
2917 for (i = 0; i < infoPtr->nItemCount; i++)
2918 {
2919 lvItem.iItem = i;
2920 lvItem.pszText = szDispText;
2921 lvItem.cchTextMax = DISP_TEXT_SIZE;
2922 if (LISTVIEW_GetItemW(infoPtr, &lvItem))
2923 nItemWidth = max(LISTVIEW_GetStringWidthT(infoPtr, lvItem.pszText, TRUE),
2924 nItemWidth);
2925 }
2926
2927 if (infoPtr->himlSmall) nItemWidth += infoPtr->iconSize.cx;
2928 if (infoPtr->himlState) nItemWidth += infoPtr->iconStateSize.cx;
2929
2930 nItemWidth = max(DEFAULT_COLUMN_WIDTH, nItemWidth + WIDTH_PADDING);
2931 }
2932
2933 return nItemWidth;
2934 }
2935
2936 /***
2937 * DESCRIPTION:
2938 * Calculates the desired item height.
2939 *
2940 * PARAMETER(S):
2941 * [I] infoPtr : valid pointer to the listview structure
2942 *
2943 * RETURN:
2944 * The desired item height.
2945 */
2946 static INT LISTVIEW_CalculateItemHeight(const LISTVIEW_INFO *infoPtr)
2947 {
2948 INT nItemHeight;
2949
2950 TRACE("uView=%d\n", infoPtr->uView);
2951
2952 if (infoPtr->uView == LV_VIEW_ICON)
2953 nItemHeight = infoPtr->iconSpacing.cy;
2954 else
2955 {
2956 nItemHeight = infoPtr->ntmHeight;
2957 if (infoPtr->himlState)
2958 nItemHeight = max(nItemHeight, infoPtr->iconStateSize.cy);
2959 if (infoPtr->himlSmall)
2960 nItemHeight = max(nItemHeight, infoPtr->iconSize.cy);
2961 nItemHeight += HEIGHT_PADDING;
2962 if (infoPtr->nMeasureItemHeight > 0)
2963 nItemHeight = infoPtr->nMeasureItemHeight;
2964 }
2965
2966 return max(nItemHeight, 1);
2967 }
2968
2969 /***
2970 * DESCRIPTION:
2971 * Updates the width, and height of an item.
2972 *
2973 * PARAMETER(S):
2974 * [I] infoPtr : valid pointer to the listview structure
2975 *
2976 * RETURN:
2977 * None.
2978 */
2979 static inline void LISTVIEW_UpdateItemSize(LISTVIEW_INFO *infoPtr)
2980 {
2981 infoPtr->nItemWidth = LISTVIEW_CalculateItemWidth(infoPtr);
2982 infoPtr->nItemHeight = LISTVIEW_CalculateItemHeight(infoPtr);
2983 }
2984
2985
2986 /***
2987 * DESCRIPTION:
2988 * Retrieves and saves important text metrics info for the current
2989 * Listview font.
2990 *
2991 * PARAMETER(S):
2992 * [I] infoPtr : valid pointer to the listview structure
2993 *
2994 */
2995 static void LISTVIEW_SaveTextMetrics(LISTVIEW_INFO *infoPtr)
2996 {
2997 HDC hdc = GetDC(infoPtr->hwndSelf);
2998 HFONT hFont = infoPtr->hFont ? infoPtr->hFont : infoPtr->hDefaultFont;
2999 HFONT hOldFont = SelectObject(hdc, hFont);
3000 TEXTMETRICW tm;
3001 SIZE sz;
3002
3003 if (GetTextMetricsW(hdc, &tm))
3004 {
3005 infoPtr->ntmHeight = tm.tmHeight;
3006 infoPtr->ntmMaxCharWidth = tm.tmMaxCharWidth;
3007 }
3008
3009 if (GetTextExtentPoint32A(hdc, "...", 3, &sz))
3010 infoPtr->nEllipsisWidth = sz.cx;
3011
3012 SelectObject(hdc, hOldFont);
3013 ReleaseDC(infoPtr->hwndSelf, hdc);
3014
3015 TRACE("tmHeight=%d\n", infoPtr->ntmHeight);
3016 }
3017
3018 /***
3019 * DESCRIPTION:
3020 * A compare function for ranges
3021 *
3022 * PARAMETER(S)
3023 * [I] range1 : pointer to range 1;
3024 * [I] range2 : pointer to range 2;
3025 * [I] flags : flags
3026 *
3027 * RETURNS:
3028 * > 0 : if range 1 > range 2
3029 * < 0 : if range 2 > range 1
3030 * = 0 : if range intersects range 2
3031 */
3032 static INT CALLBACK ranges_cmp(LPVOID range1, LPVOID range2, LPARAM flags)
3033 {
3034 INT cmp;
3035
3036 if (((RANGE*)range1)->upper <= ((RANGE*)range2)->lower)
3037 cmp = -1;
3038 else if (((RANGE*)range2)->upper <= ((RANGE*)range1)->lower)
3039 cmp = 1;
3040 else
3041 cmp = 0;
3042
3043 TRACE("range1=%s, range2=%s, cmp=%d\n", debugrange(range1), debugrange(range2), cmp);
3044
3045 return cmp;
3046 }
3047
3048 #define ranges_check(ranges, desc) if (TRACE_ON(listview)) ranges_assert(ranges, desc, __FILE__, __LINE__)
3049
3050 static void ranges_assert(RANGES ranges, LPCSTR desc, const char *file, int line)
3051 {
3052 INT i;
3053 RANGE *prev, *curr;
3054
3055 TRACE("*** Checking %s:%d:%s ***\n", file, line, desc);
3056 assert (ranges);
3057 assert (DPA_GetPtrCount(ranges->hdpa) >= 0);
3058 ranges_dump(ranges);
3059 if (DPA_GetPtrCount(ranges->hdpa) > 0)
3060 {
3061 prev = DPA_GetPtr(ranges->hdpa, 0);
3062 assert (prev->lower >= 0 && prev->lower < prev->upper);
3063 for (i = 1; i < DPA_GetPtrCount(ranges->hdpa); i++)
3064 {
3065 curr = DPA_GetPtr(ranges->hdpa, i);
3066 assert (prev->upper <= curr->lower);
3067 assert (curr->lower < curr->upper);
3068 prev = curr;
3069 }
3070 }
3071 TRACE("--- Done checking---\n");
3072 }
3073
3074 static RANGES ranges_create(int count)
3075 {
3076 RANGES ranges = Alloc(sizeof(struct tagRANGES));
3077 if (!ranges) return NULL;
3078 ranges->hdpa = DPA_Create(count);
3079 if (ranges->hdpa) return ranges;
3080 Free(ranges);
3081 return NULL;
3082 }
3083
3084 static void ranges_clear(RANGES ranges)
3085 {
3086 INT i;
3087
3088 for(i = 0; i < DPA_GetPtrCount(ranges->hdpa); i++)
3089 Free(DPA_GetPtr(ranges->hdpa, i));
3090 DPA_DeleteAllPtrs(ranges->hdpa);
3091 }
3092
3093
3094 static void ranges_destroy(RANGES ranges)
3095 {
3096 if (!ranges) return;
3097 ranges_clear(ranges);
3098 DPA_Destroy(ranges->hdpa);
3099 Free(ranges);
3100 }
3101
3102 static RANGES ranges_clone(RANGES ranges)
3103 {
3104 RANGES clone;
3105 INT i;
3106
3107 if (!(clone = ranges_create(DPA_GetPtrCount(ranges->hdpa)))) goto fail;
3108
3109 for (i = 0; i < DPA_GetPtrCount(ranges->hdpa); i++)
3110 {
3111 RANGE *newrng = Alloc(sizeof(RANGE));
3112 if (!newrng) goto fail;
3113 *newrng = *((RANGE*)DPA_GetPtr(ranges->hdpa, i));
3114 DPA_SetPtr(clone->hdpa, i, newrng);
3115 }
3116 return clone;
3117
3118 fail:
3119 TRACE ("clone failed\n");
3120 ranges_destroy(clone);
3121 return NULL;
3122 }
3123
3124 static RANGES ranges_diff(RANGES ranges, RANGES sub)
3125 {
3126 INT i;
3127
3128 for (i = 0; i < DPA_GetPtrCount(sub->hdpa); i++)
3129 ranges_del(ranges, *((RANGE *)DPA_GetPtr(sub->hdpa, i)));
3130
3131 return ranges;
3132 }
3133
3134 static void ranges_dump(RANGES ranges)
3135 {
3136 INT i;
3137
3138 for (i = 0; i < DPA_GetPtrCount(ranges->hdpa); i++)
3139 TRACE(" %s\n", debugrange(DPA_GetPtr(ranges->hdpa, i)));
3140 }
3141
3142 static inline BOOL ranges_contain(RANGES ranges, INT nItem)
3143 {
3144 RANGE srchrng = { nItem, nItem + 1 };
3145
3146 TRACE("(nItem=%d)\n", nItem);
3147 ranges_check(ranges, "before contain");
3148 return DPA_Search(ranges->hdpa, &srchrng, 0, ranges_cmp, 0, DPAS_SORTED) != -1;
3149 }
3150
3151 static INT ranges_itemcount(RANGES ranges)
3152 {
3153 INT i, count = 0;
3154
3155 for (i = 0; i < DPA_GetPtrCount(ranges->hdpa); i++)
3156 {
3157 RANGE *sel = DPA_GetPtr(ranges->hdpa, i);
3158 count += sel->upper - sel->lower;
3159 }
3160
3161 return count;
3162 }
3163
3164 static BOOL ranges_shift(RANGES ranges, INT nItem, INT delta, INT nUpper)
3165 {
3166 RANGE srchrng = { nItem, nItem + 1 }, *chkrng;
3167 INT index;
3168
3169 index = DPA_Search(ranges->hdpa, &srchrng, 0, ranges_cmp, 0, DPAS_SORTED | DPAS_INSERTAFTER);
3170 if (index == -1) return TRUE;
3171
3172 for (; index < DPA_GetPtrCount(ranges->hdpa); index++)
3173 {
3174 chkrng = DPA_GetPtr(ranges->hdpa, index);
3175 if (chkrng->lower >= nItem)
3176 chkrng->lower = max(min(chkrng->lower + delta, nUpper - 1), 0);
3177 if (chkrng->upper > nItem)
3178 chkrng->upper = max(min(chkrng->upper + delta, nUpper), 0);
3179 }
3180 return TRUE;
3181 }
3182
3183 static BOOL ranges_add(RANGES ranges, RANGE range)
3184 {
3185 RANGE srchrgn;
3186 INT index;
3187
3188 TRACE("(%s)\n", debugrange(&range));
3189 ranges_check(ranges, "before add");
3190
3191 /* try find overlapping regions first */
3192 srchrgn.lower = range.lower - 1;
3193 srchrgn.upper = range.upper + 1;
3194 index = DPA_Search(ranges->hdpa, &srchrgn, 0, ranges_cmp, 0, DPAS_SORTED);
3195
3196 if (index == -1)
3197 {
3198 RANGE *newrgn;
3199
3200 TRACE("Adding new range\n");
3201
3202 /* create the brand new range to insert */
3203 newrgn = Alloc(sizeof(RANGE));
3204 if(!newrgn) goto fail;
3205 *newrgn = range;
3206
3207 /* figure out where to insert it */
3208 index = DPA_Search(ranges->hdpa, newrgn, 0, ranges_cmp, 0, DPAS_SORTED | DPAS_INSERTAFTER);
3209 TRACE("index=%d\n", index);
3210 if (index == -1) index = 0;
3211
3212 /* and get it over with */
3213 if (DPA_InsertPtr(ranges->hdpa, index, newrgn) == -1)
3214 {
3215 Free(newrgn);
3216 goto fail;
3217 }
3218 }
3219 else
3220 {
3221 RANGE *chkrgn, *mrgrgn;
3222 INT fromindex, mergeindex;
3223
3224 chkrgn = DPA_GetPtr(ranges->hdpa, index);
3225 TRACE("Merge with %s @%d\n", debugrange(chkrgn), index);
3226
3227 chkrgn->lower = min(range.lower, chkrgn->lower);
3228 chkrgn->upper = max(range.upper, chkrgn->upper);
3229
3230 TRACE("New range %s @%d\n", debugrange(chkrgn), index);
3231
3232 /* merge now common ranges */
3233 fromindex = 0;
3234 srchrgn.lower = chkrgn->lower - 1;
3235 srchrgn.upper = chkrgn->upper + 1;
3236
3237 do
3238 {
3239 mergeindex = DPA_Search(ranges->hdpa, &srchrgn, fromindex, ranges_cmp, 0, 0);
3240 if (mergeindex == -1) break;
3241 if (mergeindex == index)
3242 {
3243 fromindex = index + 1;
3244 continue;
3245 }
3246
3247 TRACE("Merge with index %i\n", mergeindex);
3248
3249 mrgrgn = DPA_GetPtr(ranges->hdpa, mergeindex);
3250 chkrgn->lower = min(chkrgn->lower, mrgrgn->lower);
3251 chkrgn->upper = max(chkrgn->upper, mrgrgn->upper);
3252 Free(mrgrgn);
3253 DPA_DeletePtr(ranges->hdpa, mergeindex);
3254 if (mergeindex < index) index --;
3255 } while(1);
3256 }
3257
3258 ranges_check(ranges, "after add");
3259 return TRUE;
3260
3261 fail:
3262 ranges_check(ranges, "failed add");
3263 return FALSE;
3264 }
3265
3266 static BOOL ranges_del(RANGES ranges, RANGE range)
3267 {
3268 RANGE *chkrgn;
3269 INT index;
3270
3271 TRACE("(%s)\n", debugrange(&range));
3272 ranges_check(ranges, "before del");
3273
3274 /* we don't use DPAS_SORTED here, since we need *
3275 * to find the first overlapping range */
3276 index = DPA_Search(ranges->hdpa, &range, 0, ranges_cmp, 0, 0);
3277 while(index != -1)
3278 {
3279 chkrgn = DPA_GetPtr(ranges->hdpa, index);
3280
3281 TRACE("Matches range %s @%d\n", debugrange(chkrgn), index);
3282
3283 /* case 1: Same range */
3284 if ( (chkrgn->upper == range.upper) &&
3285 (chkrgn->lower == range.lower) )
3286 {
3287 DPA_DeletePtr(ranges->hdpa, index);
3288 Free(chkrgn);
3289 break;
3290 }
3291 /* case 2: engulf */
3292 else if ( (chkrgn->upper <= range.upper) &&
3293 (chkrgn->lower >= range.lower) )
3294 {
3295 DPA_DeletePtr(ranges->hdpa, index);
3296 Free(chkrgn);
3297 }
3298 /* case 3: overlap upper */
3299 else if ( (chkrgn->upper <= range.upper) &&
3300 (chkrgn->lower < range.lower) )
3301 {
3302 chkrgn->upper = range.lower;
3303 }
3304 /* case 4: overlap lower */
3305 else if ( (chkrgn->upper > range.upper) &&
3306 (chkrgn->lower >= range.lower) )
3307 {
3308 chkrgn->lower = range.upper;
3309 break;
3310 }
3311 /* case 5: fully internal */
3312 else
3313 {
3314 RANGE *newrgn;
3315
3316 if (!(newrgn = Alloc(sizeof(RANGE)))) goto fail;
3317 newrgn->lower = chkrgn->lower;
3318 newrgn->upper = range.lower;
3319 chkrgn->lower = range.upper;
3320 if (DPA_InsertPtr(ranges->hdpa, index, newrgn) == -1)
3321 {
3322 Free(newrgn);
3323 goto fail;
3324 }
3325 break;
3326 }
3327
3328 index = DPA_Search(ranges->hdpa, &range, index, ranges_cmp, 0, 0);
3329 }
3330
3331 ranges_check(ranges, "after del");
3332 return TRUE;
3333
3334 fail:
3335 ranges_check(ranges, "failed del");
3336 return FALSE;
3337 }
3338
3339 /***
3340 * DESCRIPTION:
3341 * Removes all selection ranges
3342 *
3343 * Parameters(s):
3344 * [I] infoPtr : valid pointer to the listview structure
3345 * [I] toSkip : item range to skip removing the selection
3346 *
3347 * RETURNS:
3348 * SUCCESS : TRUE
3349 * FAILURE : FALSE
3350 */
3351 static BOOL LISTVIEW_DeselectAllSkipItems(LISTVIEW_INFO *infoPtr, RANGES toSkip)
3352 {
3353 LVITEMW lvItem;
3354 ITERATOR i;
3355 RANGES clone;
3356
3357 TRACE("()\n");
3358
3359 lvItem.state = 0;
3360 lvItem.stateMask = LVIS_SELECTED;
3361
3362 /* need to clone the DPA because callbacks can change it */
3363 if (!(clone = ranges_clone(infoPtr->selectionRanges))) return FALSE;
3364 iterator_rangesitems(&i, ranges_diff(clone, toSkip));
3365 while(iterator_next(&i))
3366 LISTVIEW_SetItemState(infoPtr, i.nItem, &lvItem);
3367 /* note that the iterator destructor will free the cloned range */
3368 iterator_destroy(&i);
3369
3370 return TRUE;
3371 }
3372
3373 static inline BOOL LISTVIEW_DeselectAllSkipItem(LISTVIEW_INFO *infoPtr, INT nItem)
3374 {
3375 RANGES toSkip;
3376
3377 if (!(toSkip = ranges_create(1))) return FALSE;
3378 if (nItem != -1) ranges_additem(toSkip, nItem);
3379 LISTVIEW_DeselectAllSkipItems(infoPtr, toSkip);
3380 ranges_destroy(toSkip);
3381 return TRUE;
3382 }
3383
3384 static inline BOOL LISTVIEW_DeselectAll(LISTVIEW_INFO *infoPtr)
3385 {
3386 return LISTVIEW_DeselectAllSkipItem(infoPtr, -1);
3387 }
3388
3389 /***
3390 * DESCRIPTION:
3391 * Retrieves the number of items that are marked as selected.
3392 *
3393 * PARAMETER(S):
3394 * [I] infoPtr : valid pointer to the listview structure
3395 *
3396 * RETURN:
3397 * Number of items selected.
3398 */
3399 static INT LISTVIEW_GetSelectedCount(const LISTVIEW_INFO *infoPtr)
3400 {
3401 INT nSelectedCount = 0;
3402
3403 if (infoPtr->uCallbackMask & LVIS_SELECTED)
3404 {
3405 INT i;
3406 for (i = 0; i < infoPtr->nItemCount; i++)
3407 {
3408 if (LISTVIEW_GetItemState(infoPtr, i, LVIS_SELECTED))
3409 nSelectedCount++;
3410 }
3411 }
3412 else
3413 nSelectedCount = ranges_itemcount(infoPtr->selectionRanges);
3414
3415 TRACE("nSelectedCount=%d\n", nSelectedCount);
3416 return nSelectedCount;
3417 }
3418
3419 /***
3420 * DESCRIPTION:
3421 * Manages the item focus.
3422 *
3423 * PARAMETER(S):
3424 * [I] infoPtr : valid pointer to the listview structure
3425 * [I] nItem : item index
3426 *
3427 * RETURN:
3428 * TRUE : focused item changed
3429 * FALSE : focused item has NOT changed
3430 */
3431 static inline BOOL LISTVIEW_SetItemFocus(LISTVIEW_INFO *infoPtr, INT nItem)
3432 {
3433 INT oldFocus = infoPtr->nFocusedItem;
3434 LVITEMW lvItem;
3435
3436 if (nItem == infoPtr->nFocusedItem) return FALSE;
3437
3438 lvItem.state = nItem == -1 ? 0 : LVIS_FOCUSED;
3439 lvItem.stateMask = LVIS_FOCUSED;
3440 LISTVIEW_SetItemState(infoPtr, nItem == -1 ? infoPtr->nFocusedItem : nItem, &lvItem);
3441
3442 return oldFocus != infoPtr->nFocusedItem;
3443 }
3444
3445 static INT shift_item(const LISTVIEW_INFO *infoPtr, INT nShiftItem, INT nItem, INT direction)
3446 {
3447 if (nShiftItem < nItem) return nShiftItem;
3448
3449 if (nShiftItem > nItem) return nShiftItem + direction;
3450
3451 if (direction > 0) return nShiftItem + direction;
3452
3453 return min(nShiftItem, infoPtr->nItemCount - 1);
3454 }
3455
3456 /* This function updates focus index.
3457
3458 Parameters:
3459 focus : current focus index
3460 item : index of item to be added/removed
3461 direction : add/remove flag
3462 */
3463 static void LISTVIEW_ShiftFocus(LISTVIEW_INFO *infoPtr, INT focus, INT item, INT direction)
3464 {
3465 BOOL old_change = infoPtr->bDoChangeNotify;
3466
3467 infoPtr->bDoChangeNotify = FALSE;
3468 focus = shift_item(infoPtr, focus, item, direction);
3469 if (focus != infoPtr->nFocusedItem)
3470 LISTVIEW_SetItemFocus(infoPtr, focus);
3471 infoPtr->bDoChangeNotify = old_change;
3472 }
3473
3474 /**
3475 * DESCRIPTION:
3476 * Updates the various indices after an item has been inserted or deleted.
3477 *
3478 * PARAMETER(S):
3479 * [I] infoPtr : valid pointer to the listview structure
3480 * [I] nItem : item index
3481 * [I] direction : Direction of shift, +1 or -1.
3482 *
3483 * RETURN:
3484 * None
3485 */
3486 static void LISTVIEW_ShiftIndices(LISTVIEW_INFO *infoPtr, INT nItem, INT direction)
3487 {
3488 TRACE("Shifting %i, %i steps\n", nItem, direction);
3489
3490 ranges_shift(infoPtr->selectionRanges, nItem, direction, infoPtr->nItemCount);
3491 assert(abs(direction) == 1);
3492 infoPtr->nSelectionMark = shift_item(infoPtr, infoPtr->nSelectionMark, nItem, direction);
3493
3494 /* But we are not supposed to modify nHotItem! */
3495 }
3496
3497 /**
3498 * DESCRIPTION:
3499 * Adds a block of selections.
3500 *
3501 * PARAMETER(S):
3502 * [I] infoPtr : valid pointer to the listview structure
3503 * [I] nItem : item index
3504 *
3505 * RETURN:
3506 * Whether the window is still valid.
3507 */
3508 static BOOL LISTVIEW_AddGroupSelection(LISTVIEW_INFO *infoPtr, INT nItem)
3509 {
3510 INT nFirst = min(infoPtr->nSelectionMark, nItem);
3511 INT nLast = max(infoPtr->nSelectionMark, nItem);
3512 HWND hwndSelf = infoPtr->hwndSelf;
3513 NMLVODSTATECHANGE nmlv;
3514 LVITEMW item;
3515 BOOL bOldChange;
3516 INT i;
3517
3518 /* Temporarily disable change notification
3519 * If the control is LVS_OWNERDATA, we need to send
3520 * only one LVN_ODSTATECHANGED notification.
3521 * See MSDN documentation for LVN_ITEMCHANGED.
3522 */
3523 bOldChange = infoPtr->bDoChangeNotify;
3524 if (infoPtr->dwStyle & LVS_OWNERDATA) infoPtr->bDoChangeNotify = FALSE;
3525
3526 if (nFirst == -1) nFirst = nItem;
3527
3528 item.state = LVIS_SELECTED;
3529 item.stateMask = LVIS_SELECTED;
3530
3531 for (i = nFirst; i <= nLast; i++)
3532 LISTVIEW_SetItemState(infoPtr,i,&item);
3533
3534 ZeroMemory(&nmlv, sizeof(nmlv));
3535 nmlv.iFrom = nFirst;
3536 nmlv.iTo = nLast;
3537 nmlv.uOldState = 0;
3538 nmlv.uNewState = item.state;
3539
3540 notify_hdr(infoPtr, LVN_ODSTATECHANGED, (LPNMHDR)&nmlv);
3541 if (!IsWindow(hwndSelf))
3542 return FALSE;
3543 infoPtr->bDoChangeNotify = bOldChange;
3544 return TRUE;
3545 }
3546
3547
3548 /***
3549 * DESCRIPTION:
3550 * Sets a single group selection.
3551 *
3552 * PARAMETER(S):
3553 * [I] infoPtr : valid pointer to the listview structure
3554 * [I] nItem : item index
3555 *
3556 * RETURN:
3557 * None
3558 */
3559 static void LISTVIEW_SetGroupSelection(LISTVIEW_INFO *infoPtr, INT nItem)
3560 {
3561 RANGES selection;
3562 LVITEMW item;
3563 ITERATOR i;
3564 BOOL bOldChange;
3565
3566 if (!(selection = ranges_create(100))) return;
3567
3568 item.state = LVIS_SELECTED;
3569 item.stateMask = LVIS_SELECTED;
3570
3571 if ((infoPtr->uView == LV_VIEW_LIST) || (infoPtr->uView == LV_VIEW_DETAILS))
3572 {
3573 if (infoPtr->nSelectionMark == -1)
3574 {
3575 infoPtr->nSelectionMark = nItem;
3576 ranges_additem(selection, nItem);
3577 }
3578 else
3579 {
3580 RANGE sel;
3581
3582 sel.lower = min(infoPtr->nSelectionMark, nItem);
3583 sel.upper = max(infoPtr->nSelectionMark, nItem) + 1;
3584 ranges_add(selection, sel);
3585 }
3586 }
3587 else
3588 {
3589 RECT rcItem, rcSel, rcSelMark;
3590 POINT ptItem;
3591
3592 rcItem.left = LVIR_BOUNDS;
3593 if (!LISTVIEW_GetItemRect(infoPtr, nItem, &rcItem)) {
3594 ranges_destroy (selection);
3595 return;
3596 }
3597 rcSelMark.left = LVIR_BOUNDS;
3598 if (!LISTVIEW_GetItemRect(infoPtr, infoPtr->nSelectionMark, &rcSelMark)) {
3599 ranges_destroy (selection);
3600 return;
3601 }
3602 UnionRect(&rcSel, &rcItem, &rcSelMark);
3603 iterator_frameditems(&i, infoPtr, &rcSel);
3604 while(iterator_next(&i))
3605 {
3606 LISTVIEW_GetItemPosition(infoPtr, i.nItem, &ptItem);
3607 if (PtInRect(&rcSel, ptItem)) ranges_additem(selection, i.nItem);
3608 }
3609 iterator_destroy(&i);
3610 }
3611
3612 /* disable per item notifications on LVS_OWNERDATA style
3613 FIXME: single LVN_ODSTATECHANGED should be used */
3614 bOldChange = infoPtr->bDoChangeNotify;
3615 if (infoPtr->dwStyle & LVS_OWNERDATA) infoPtr->bDoChangeNotify = FALSE;
3616
3617 LISTVIEW_DeselectAllSkipItems(infoPtr, selection);
3618
3619
3620 iterator_rangesitems(&i, selection);
3621 while(iterator_next(&i))
3622 LISTVIEW_SetItemState(infoPtr, i.nItem, &item);
3623 /* this will also destroy the selection */
3624 iterator_destroy(&i);
3625
3626 infoPtr->bDoChangeNotify = bOldChange;
3627
3628 LISTVIEW_SetItemFocus(infoPtr, nItem);
3629 }
3630
3631 /***
3632 * DESCRIPTION:
3633 * Sets a single selection.
3634 *
3635 * PARAMETER(S):
3636 * [I] infoPtr : valid pointer to the listview structure
3637 * [I] nItem : item index
3638 *
3639 * RETURN:
3640 * None
3641 */
3642 static void LISTVIEW_SetSelection(LISTVIEW_INFO *infoPtr, INT nItem)
3643 {
3644 LVITEMW lvItem;
3645
3646 TRACE("nItem=%d\n", nItem);
3647
3648 LISTVIEW_DeselectAllSkipItem(infoPtr, nItem);
3649
3650 lvItem.state = LVIS_FOCUSED | LVIS_SELECTED;
3651 lvItem.stateMask = LVIS_FOCUSED | LVIS_SELECTED;
3652 LISTVIEW_SetItemState(infoPtr, nItem, &lvItem);
3653
3654 infoPtr->nSelectionMark = nItem;
3655 }
3656
3657 /***
3658 * DESCRIPTION:
3659 * Set selection(s) with keyboard.
3660 *
3661 * PARAMETER(S):
3662 * [I] infoPtr : valid pointer to the listview structure
3663 * [I] nItem : item index
3664 * [I] space : VK_SPACE code sent
3665 *
3666 * RETURN:
3667 * SUCCESS : TRUE (needs to be repainted)
3668 * FAILURE : FALSE (nothing has changed)
3669 */
3670 static BOOL LISTVIEW_KeySelection(LISTVIEW_INFO *infoPtr, INT nItem, BOOL space)
3671 {
3672 /* FIXME: pass in the state */
3673 WORD wShift = HIWORD(GetKeyState(VK_SHIFT));
3674 WORD wCtrl = HIWORD(GetKeyState(VK_CONTROL));
3675 BOOL bResult = FALSE;
3676
3677 TRACE("nItem=%d, wShift=%d, wCtrl=%d\n", nItem, wShift, wCtrl);
3678 if ((nItem >= 0) && (nItem < infoPtr->nItemCount))
3679 {
3680 bResult = TRUE;
3681
3682 if (infoPtr->dwStyle & LVS_SINGLESEL || (wShift == 0 && wCtrl == 0))
3683 LISTVIEW_SetSelection(infoPtr, nItem);
3684 else
3685 {
3686 if (wShift)
3687 LISTVIEW_SetGroupSelection(infoPtr, nItem);
3688 else if (wCtrl)
3689 {
3690 LVITEMW lvItem;
3691 lvItem.state = ~LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED);
3692 lvItem.stateMask = LVIS_SELECTED;
3693 if (space)
3694 {
3695 LISTVIEW_SetItemState(infoPtr, nItem, &lvItem);
3696 if (lvItem.state & LVIS_SELECTED)
3697 infoPtr->nSelectionMark = nItem;
3698 }
3699 bResult = LISTVIEW_SetItemFocus(infoPtr, nItem);
3700 }
3701 }
3702 LISTVIEW_EnsureVisible(infoPtr, nItem, FALSE);
3703 }
3704
3705 UpdateWindow(infoPtr->hwndSelf); /* update client area */
3706 return bResult;
3707 }
3708
3709 static BOOL LISTVIEW_GetItemAtPt(const LISTVIEW_INFO *infoPtr, LPLVITEMW lpLVItem, POINT pt)
3710 {
3711 LVHITTESTINFO lvHitTestInfo;
3712
3713 ZeroMemory(&lvHitTestInfo, sizeof(lvHitTestInfo));
3714 lvHitTestInfo.pt.x = pt.x;
3715 lvHitTestInfo.pt.y = pt.y;
3716
3717 LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, FALSE);
3718
3719 lpLVItem->mask = LVIF_PARAM;
3720 lpLVItem->iItem = lvHitTestInfo.iItem;
3721 lpLVItem->iSubItem = 0;
3722
3723 return LISTVIEW_GetItemT(infoPtr, lpLVItem, TRUE);
3724 }
3725
3726 static inline BOOL LISTVIEW_IsHotTracking(const LISTVIEW_INFO *infoPtr)
3727 {
3728 return ((infoPtr->dwLvExStyle & LVS_EX_TRACKSELECT) ||
3729 (infoPtr->dwLvExStyle & LVS_EX_ONECLICKACTIVATE) ||
3730 (infoPtr->dwLvExStyle & LVS_EX_TWOCLICKACTIVATE));
3731 }
3732
3733 /***
3734 * DESCRIPTION:
3735 * Called when the mouse is being actively tracked and has hovered for a specified
3736 * amount of time
3737 *
3738 * PARAMETER(S):
3739 * [I] infoPtr : valid pointer to the listview structure
3740 * [I] fwKeys : key indicator
3741 * [I] x,y : mouse position
3742 *
3743 * RETURN:
3744 * 0 if the message was processed, non-zero if there was an error
3745 *
3746 * INFO:
3747 * LVS_EX_TRACKSELECT: An item is automatically selected when the cursor remains
3748 * over the item for a certain period of time.
3749 *
3750 */
3751 static LRESULT LISTVIEW_MouseHover(LISTVIEW_INFO *infoPtr, INT x, INT y)
3752 {
3753 NMHDR hdr;
3754
3755 if (notify_hdr(infoPtr, NM_HOVER, &hdr)) return 0;
3756
3757 if (LISTVIEW_IsHotTracking(infoPtr))
3758 {
3759 LVITEMW item;
3760 POINT pt;
3761
3762 pt.x = x;
3763 pt.y = y;
3764
3765 if (LISTVIEW_GetItemAtPt(infoPtr, &item, pt))
3766 LISTVIEW_SetSelection(infoPtr, item.iItem);
3767
3768 SetFocus(infoPtr->hwndSelf);
3769 }
3770
3771 return 0;
3772 }
3773
3774 #define SCROLL_LEFT 0x1
3775 #define SCROLL_RIGHT 0x2
3776 #define SCROLL_UP 0x4
3777 #define SCROLL_DOWN 0x8
3778
3779 /***
3780 * DESCRIPTION:
3781 * Utility routine to draw and highlight items within a marquee selection rectangle.
3782 *
3783 * PARAMETER(S):
3784 * [I] infoPtr : valid pointer to the listview structure
3785 * [I] coords_orig : original co-ordinates of the cursor
3786 * [I] coords_offs : offsetted coordinates of the cursor
3787 * [I] offset : offset amount
3788 * [I] scroll : Bitmask of which directions we should scroll, if at all
3789 *
3790 * RETURN:
3791 * None.
3792 */
3793 static void LISTVIEW_MarqueeHighlight(LISTVIEW_INFO *infoPtr, const POINT *coords_orig,
3794 const POINT *coords_offs, const POINT *offset,
3795 INT scroll)
3796 {
3797 BOOL controlDown = FALSE;
3798 LVITEMW item;
3799 ITERATOR old_elems, new_elems;
3800 RECT rect;
3801
3802 if (coords_offs->x > infoPtr->marqueeOrigin.x)
3803 {
3804 rect.left = infoPtr->marqueeOrigin.x;
3805 rect.right = coords_offs->x;
3806 }
3807 else
3808 {
3809 rect.left = coords_offs->x;
3810 rect.right = infoPtr->marqueeOrigin.x;
3811 }
3812
3813 if (coords_offs->y > infoPtr->marqueeOrigin.y)
3814 {
3815 rect.top = infoPtr->marqueeOrigin.y;
3816 rect.bottom = coords_offs->y;
3817 }
3818 else
3819 {
3820 rect.top = coords_offs->y;
3821 rect.bottom = infoPtr->marqueeOrigin.y;
3822 }
3823
3824 /* Cancel out the old marquee rectangle and draw the new one */
3825 LISTVIEW_InvalidateRect(infoPtr, &infoPtr->marqueeDrawRect);
3826
3827 /* Scroll by the appropriate distance if applicable - speed up scrolling as
3828 the cursor is further away */
3829
3830 if ((scroll & SCROLL_LEFT) && (coords_orig->x <= 0))
3831 LISTVIEW_Scroll(infoPtr, coords_orig->x, 0);
3832
3833 if ((scroll & SCROLL_RIGHT) && (coords_orig->x >= infoPtr->rcList.right))
3834 LISTVIEW_Scroll(infoPtr, (coords_orig->x - infoPtr->rcList.right), 0);
3835
3836 if ((scroll & SCROLL_UP) && (coords_orig->y <= 0))
3837 LISTVIEW_Scroll(infoPtr, 0, coords_orig->y);
3838
3839 if ((scroll & SCROLL_DOWN) && (coords_orig->y >= infoPtr->rcList.bottom))
3840 LISTVIEW_Scroll(infoPtr, 0, (coords_orig->y - infoPtr->rcList.bottom));
3841
3842 iterator_frameditems_absolute(&old_elems, infoPtr, &infoPtr->marqueeRect);
3843
3844 CopyRect(&infoPtr->marqueeRect, &rect);
3845
3846 CopyRect(&infoPtr->marqueeDrawRect, &rect);
3847 OffsetRect(&infoPtr->marqueeDrawRect, offset->x, offset->y);
3848
3849 iterator_frameditems_absolute(&new_elems, infoPtr, &infoPtr->marqueeRect);
3850 iterator_remove_common_items(&old_elems, &new_elems);
3851
3852 /* Iterate over no longer selected items */
3853 while (iterator_next(&old_elems))
3854 {
3855 if (old_elems.nItem > -1)
3856 {
3857 if (LISTVIEW_GetItemState(infoPtr, old_elems.nItem, LVIS_SELECTED) == LVIS_SELECTED)
3858 item.state = 0;
3859 else
3860 item.state = LVIS_SELECTED;
3861
3862 item.stateMask = LVIS_SELECTED;
3863
3864 LISTVIEW_SetItemState(infoPtr, old_elems.nItem, &item);
3865 }
3866 }
3867 iterator_destroy(&old_elems);
3868
3869
3870 /* Iterate over newly selected items */
3871 if (GetKeyState(VK_CONTROL) & 0x8000)
3872 controlDown = TRUE;
3873
3874 while (iterator_next(&new_elems))
3875 {
3876 if (new_elems.nItem > -1)
3877 {
3878 /* If CTRL is pressed, invert. If not, always select the item. */
3879 if ((controlDown) && (LISTVIEW_GetItemState(infoPtr, new_elems.nItem, LVIS_SELECTED)))
3880 item.state = 0;
3881 else
3882 item.state = LVIS_SELECTED;
3883
3884 item.stateMask = LVIS_SELECTED;
3885
3886 LISTVIEW_SetItemState(infoPtr, new_elems.nItem, &item);
3887 }
3888 }
3889 iterator_destroy(&new_elems);
3890
3891 LISTVIEW_InvalidateRect(infoPtr, &infoPtr->marqueeDrawRect);
3892 }
3893
3894 /***
3895 * DESCRIPTION:
3896 * Called when we are in a marquee selection that involves scrolling the listview (ie,
3897 * the cursor is outside the bounds of the client area). This is a TIMERPROC.
3898 *
3899 * PARAMETER(S):
3900 * [I] hwnd : Handle to the listview
3901 * [I] uMsg : WM_TIMER (ignored)
3902 * [I] idEvent : The timer ID interpreted as a pointer to a LISTVIEW_INFO struct
3903 * [I] dwTimer : The elapsed time (ignored)
3904 *
3905 * RETURN:
3906 * None.
3907 */
3908 static VOID CALLBACK LISTVIEW_ScrollTimer(HWND hWnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime)
3909 {
3910 LISTVIEW_INFO *infoPtr;
3911 SCROLLINFO scrollInfo;
3912 POINT coords_orig;
3913 POINT coords_offs;
3914 POINT offset;
3915 INT scroll = 0;
3916
3917 infoPtr = (LISTVIEW_INFO *) idEvent;
3918
3919 if (!infoPtr)
3920 return;
3921
3922 /* Get the current cursor position and convert to client coordinates */
3923 GetCursorPos(&coords_orig);
3924 ScreenToClient(hWnd, &coords_orig);
3925
3926 /* Ensure coordinates are within client bounds */
3927 coords_offs.x = max(min(coords_orig.x, infoPtr->rcList.right), 0);
3928 coords_offs.y = max(min(coords_orig.y, infoPtr->rcList.bottom), 0);
3929
3930 /* Get offset */
3931 LISTVIEW_GetOrigin(infoPtr, &offset);
3932
3933 /* Offset coordinates by the appropriate amount */
3934 coords_offs.x -= offset.x;
3935 coords_offs.y -= offset.y;
3936
3937 scrollInfo.cbSize = sizeof(SCROLLINFO);
3938 scrollInfo.fMask = SIF_ALL;
3939
3940 /* Work out in which directions we can scroll */
3941 if (GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo))
3942 {
3943 if (scrollInfo.nPos != scrollInfo.nMin)
3944 scroll |= SCROLL_UP;
3945
3946 if (((scrollInfo.nPage + scrollInfo.nPos) - 1) != scrollInfo.nMax)
3947 scroll |= SCROLL_DOWN;
3948 }
3949
3950 if (GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo))
3951 {
3952 if (scrollInfo.nPos != scrollInfo.nMin)
3953 scroll |= SCROLL_LEFT;
3954
3955 if (((scrollInfo.nPage + scrollInfo.nPos) - 1) != scrollInfo.nMax)
3956 scroll |= SCROLL_RIGHT;
3957 }
3958
3959 if (((coords_orig.x <= 0) && (scroll & SCROLL_LEFT)) ||
3960 ((coords_orig.y <= 0) && (scroll & SCROLL_UP)) ||
3961 ((coords_orig.x >= infoPtr->rcList.right) && (scroll & SCROLL_RIGHT)) ||
3962 ((coords_orig.y >= infoPtr->rcList.bottom) && (scroll & SCROLL_DOWN)))
3963 {
3964 LISTVIEW_MarqueeHighlight(infoPtr, &coords_orig, &coords_offs, &offset, scroll);
3965 }
3966 }
3967
3968 /***
3969 * DESCRIPTION:
3970 * Called whenever WM_MOUSEMOVE is received.
3971 *
3972 * PARAMETER(S):
3973 * [I] infoPtr : valid pointer to the listview structure
3974 * [I] fwKeys : key indicator
3975 * [I] x,y : mouse position
3976 *
3977 * RETURN:
3978 * 0 if the message is processed, non-zero if there was an error
3979 */
3980 static LRESULT LISTVIEW_MouseMove(LISTVIEW_INFO *infoPtr, WORD fwKeys, INT x, INT y)
3981 {
3982 LVHITTESTINFO ht;
3983 RECT rect;
3984 POINT pt;
3985
3986 if (!(fwKeys & MK_LBUTTON))
3987 infoPtr->bLButtonDown = FALSE;
3988
3989 if (infoPtr->bLButtonDown)
3990 {
3991 rect.left = rect.right = infoPtr->ptClickPos.x;
3992 rect.top = rect.bottom = infoPtr->ptClickPos.y;
3993
3994 InflateRect(&rect, GetSystemMetrics(SM_CXDRAG), GetSystemMetrics(SM_CYDRAG));
3995 }
3996
3997 if (infoPtr->bLButtonDown)
3998 {
3999 if (infoPtr->bMarqueeSelect)
4000 {
4001 POINT coords_orig;
4002 POINT coords_offs;
4003 POINT offset;
4004
4005 coords_orig.x = x;
4006 coords_orig.y = y;
4007
4008 /* Get offset */
4009 LISTVIEW_GetOrigin(infoPtr, &offset);
4010
4011 /* Ensure coordinates are within client bounds */
4012 coords_offs.x = max(min(x, infoPtr->rcList.right), 0);
4013 coords_offs.y = max(min(y, infoPtr->rcList.bottom), 0);
4014
4015 /* Offset coordinates by the appropriate amount */
4016 coords_offs.x -= offset.x;
4017 coords_offs.y -= offset.y;
4018
4019 /* Enable the timer if we're going outside our bounds, in case the user doesn't
4020 move the mouse again */
4021
4022 if ((x <= 0) || (y <= 0) || (x >= infoPtr->rcList.right) ||
4023 (y >= infoPtr->rcList.bottom))
4024 {
4025 if (!infoPtr->bScrolling)
4026 {
4027 infoPtr->bScrolling = TRUE;
4028 SetTimer(infoPtr->hwndSelf, (UINT_PTR) infoPtr, 1, LISTVIEW_ScrollTimer);
4029 }
4030 }
4031 else
4032 {
4033 infoPtr->bScrolling = FALSE;
4034 KillTimer(infoPtr->hwndSelf, (UINT_PTR) infoPtr);
4035 }
4036
4037 LISTVIEW_MarqueeHighlight(infoPtr, &coords_orig, &coords_offs, &offset, 0);
4038 return 0;
4039 }
4040
4041 pt.x = x;
4042 pt.y = y;
4043
4044 ht.pt = pt;
4045 LISTVIEW_HitTest(infoPtr, &ht, TRUE, TRUE);
4046
4047 /* reset item marker */
4048 if (infoPtr->nLButtonDownItem != ht.iItem)
4049 infoPtr->nLButtonDownItem = -1;
4050
4051 if (!PtInRect(&rect, pt))
4052 {
4053 /* this path covers the following:
4054 1. WM_LBUTTONDOWN over selected item (sets focus on it)
4055 2. change focus with keys
4056 3. move mouse over item from step 1 selects it and moves focus on it */
4057 if (infoPtr->nLButtonDownItem != -1 &&
4058 !LISTVIEW_GetItemState(infoPtr, infoPtr->nLButtonDownItem, LVIS_SELECTED))
4059 {
4060 LVITEMW lvItem;
4061
4062 lvItem.state = LVIS_FOCUSED | LVIS_SELECTED;
4063 lvItem.stateMask = LVIS_FOCUSED | LVIS_SELECTED;
4064
4065 LISTVIEW_SetItemState(infoPtr, infoPtr->nLButtonDownItem, &lvItem);
4066 infoPtr->nLButtonDownItem = -1;
4067 }
4068
4069 if (!infoPtr->bDragging)
4070 {
4071 ht.pt = infoPtr->ptClickPos;
4072 LISTVIEW_HitTest(infoPtr, &ht, TRUE, TRUE);
4073
4074 /* If the click is outside the range of an item, begin a
4075 highlight. If not, begin an item drag. */
4076 if (ht.iItem == -1)
4077 {
4078 NMHDR hdr;
4079
4080 /* If we're allowing multiple selections, send notification.
4081 If return value is non-zero, cancel. */
4082 if (!(infoPtr->dwStyle & LVS_SINGLESEL) && (notify_hdr(infoPtr, LVN_MARQUEEBEGIN, &hdr) == 0))
4083 {
4084 /* Store the absolute coordinates of the click */
4085 POINT offset;
4086 LISTVIEW_GetOrigin(infoPtr, &offset);
4087
4088 infoPtr->marqueeOrigin.x = infoPtr->ptClickPos.x - offset.x;
4089 infoPtr->marqueeOrigin.y = infoPtr->ptClickPos.y - offset.y;
4090
4091 /* Begin selection and capture mouse */
4092 infoPtr->bMarqueeSelect = TRUE;
4093 SetCapture(infoPtr->hwndSelf);
4094 }
4095 }
4096 else
4097 {
4098 NMLISTVIEW nmlv;
4099
4100 ZeroMemory(&nmlv, sizeof(nmlv));
4101 nmlv.iItem = ht.iItem;
4102 nmlv.ptAction = infoPtr->ptClickPos;
4103
4104 notify_listview(infoPtr, LVN_BEGINDRAG, &nmlv);
4105 infoPtr->bDragging = TRUE;
4106 }
4107 }
4108
4109 return 0;
4110 }
4111 }
4112
4113 /* see if we are supposed to be tracking mouse hovering */
4114 if (LISTVIEW_IsHotTracking(infoPtr)) {
4115 TRACKMOUSEEVENT trackinfo;
4116
4117 trackinfo.cbSize = sizeof(TRACKMOUSEEVENT);
4118 trackinfo.dwFlags = TME_QUERY;
4119
4120 /* see if we are already tracking this hwnd */
4121 _TrackMouseEvent(&trackinfo);
4122
4123 if(!(trackinfo.dwFlags & TME_HOVER) || trackinfo.hwndTrack != infoPtr->hwndSelf) {
4124 trackinfo.dwFlags = TME_HOVER;
4125 trackinfo.dwHoverTime = infoPtr->dwHoverTime;
4126 trackinfo.hwndTrack = infoPtr->hwndSelf;
4127
4128 /* call TRACKMOUSEEVENT so we receive WM_MOUSEHOVER messages */
4129 _TrackMouseEvent(&trackinfo);
4130 }
4131 }
4132
4133 return 0;
4134 }
4135
4136
4137 /***
4138 * Tests whether the item is assignable to a list with style lStyle
4139 */
4140 static inline BOOL is_assignable_item(const LVITEMW *lpLVItem, LONG lStyle)
4141 {
4142 if ( (lpLVItem->mask & LVIF_TEXT) &&
4143 (lpLVItem->pszText == LPSTR_TEXTCALLBACKW) &&
4144 (lStyle & (LVS_SORTASCENDING | LVS_SORTDESCENDING)) ) return FALSE;
4145
4146 return TRUE;
4147 }
4148
4149
4150 /***
4151 * DESCRIPTION:
4152 * Helper for LISTVIEW_SetItemT and LISTVIEW_InsertItemT: sets item attributes.
4153 *
4154 * PARAMETER(S):
4155 * [I] infoPtr : valid pointer to the listview structure
4156 * [I] lpLVItem : valid pointer to new item attributes
4157 * [I] isNew : the item being set is being inserted
4158 * [I] isW : TRUE if lpLVItem is Unicode, FALSE if it's ANSI
4159 * [O] bChanged : will be set to TRUE if the item really changed
4160 *
4161 * RETURN:
4162 * SUCCESS : TRUE
4163 * FAILURE : FALSE
4164 */
4165 static BOOL set_main_item(LISTVIEW_INFO *infoPtr, const LVITEMW *lpLVItem, BOOL isNew, BOOL isW, BOOL *bChanged)
4166 {
4167 ITEM_INFO *lpItem;
4168 NMLISTVIEW nmlv;
4169 UINT uChanged = 0;
4170 LVITEMW item;
4171 /* stateMask is ignored for LVM_INSERTITEM */
4172 UINT stateMask = isNew ? ~0 : lpLVItem->stateMask;
4173
4174 TRACE("()\n");
4175
4176 assert(lpLVItem->iItem >= 0 && lpLVItem->iItem < infoPtr->nItemCount);
4177
4178 if (lpLVItem->mask == 0) return TRUE;
4179
4180 if (infoPtr->dwStyle & LVS_OWNERDATA)
4181 {
4182 /* a virtual listview only stores selection and focus */
4183 if (lpLVItem->mask & ~LVIF_STATE)
4184 return FALSE;
4185 lpItem = NULL;
4186 }
4187 else
4188 {
4189 HDPA hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, lpLVItem->iItem);
4190 lpItem = DPA_GetPtr(hdpaSubItems, 0);
4191 assert (lpItem);
4192 }
4193
4194 /* we need to get the lParam and state of the item */
4195 item.iItem = lpLVItem->iItem;
4196 item.iSubItem = lpLVItem->iSubItem;
4197 item.mask = LVIF_STATE | LVIF_PARAM;
4198 item.stateMask = (infoPtr->dwStyle & LVS_OWNERDATA) ? LVIS_FOCUSED | LVIS_SELECTED : ~0;
4199
4200 item.state = 0;
4201 item.lParam = 0;
4202 if (!isNew && !LISTVIEW_GetItemW(infoPtr, &item)) return FALSE;
4203
4204 TRACE("oldState=%x, newState=%x\n", item.state, lpLVItem->state);
4205 /* determine what fields will change */
4206 if ((lpLVItem->mask & LVIF_STATE) && ((item.state ^ lpLVItem->state) & stateMask & ~infoPtr->uCallbackMask))
4207 uChanged |= LVIF_STATE;
4208
4209 if ((lpLVItem->mask & LVIF_IMAGE) && (lpItem->hdr.iImage != lpLVItem->iImage))
4210 uChanged |= LVIF_IMAGE;
4211
4212 if ((lpLVItem->mask & LVIF_PARAM) && (lpItem->lParam != lpLVItem->lParam))
4213 uChanged |= LVIF_PARAM;
4214
4215 if ((lpLVItem->mask & LVIF_INDENT) && (lpItem->iIndent != lpLVItem->iIndent))
4216 uChanged |= LVIF_INDENT;
4217
4218 if ((lpLVItem->mask & LVIF_TEXT) && textcmpWT(lpItem->hdr.pszText, lpLVItem->pszText, isW))
4219 uChanged |= LVIF_TEXT;
4220
4221 TRACE("change mask=0x%x\n", uChanged);
4222
4223 memset(&nmlv, 0, sizeof(NMLISTVIEW));
4224 nmlv.iItem = lpLVItem->iItem;
4225 nmlv.uNewState = (item.state & ~stateMask) | (lpLVItem->state & stateMask);
4226 nmlv.uOldState = item.state;
4227 nmlv.uChanged = uChanged ? uChanged : lpLVItem->mask;
4228 nmlv.lParam = item.lParam;
4229
4230 /* Send LVN_ITEMCHANGING notification, if the item is not being inserted
4231 and we are _NOT_ virtual (LVS_OWNERDATA), and change notifications
4232 are enabled. Even nothing really changed we still need to send this,
4233 in this case uChanged mask is just set to passed item mask. */
4234 if(lpItem && !isNew && infoPtr->bDoChangeNotify)
4235 {
4236 HWND hwndSelf = infoPtr->hwndSelf;
4237
4238 if (notify_listview(infoPtr, LVN_ITEMCHANGING, &nmlv))
4239 return FALSE;
4240 if (!IsWindow(hwndSelf))
4241 return FALSE;
4242 }
4243
4244 /* When item is inserted we need to shift existing focus index if new item has lower index. */
4245 if (isNew && (stateMask & ~infoPtr->uCallbackMask & LVIS_FOCUSED) &&
4246 /* this means we won't hit a focus change path later */
4247 ((uChanged & LVIF_STATE) == 0 || (!(lpLVItem->state & LVIS_FOCUSED) && (infoPtr->nFocusedItem != lpLVItem->iItem))))
4248 {
4249 if (infoPtr->nFocusedItem != -1 && (lpLVItem->iItem <= infoPtr->nFocusedItem))
4250 infoPtr->nFocusedItem++;
4251 }
4252
4253 if (!uChanged) return TRUE;
4254 *bChanged = TRUE;
4255
4256 /* copy information */
4257 if (lpLVItem->mask & LVIF_TEXT)
4258 textsetptrT(&lpItem->hdr.pszText, lpLVItem->pszText, isW);
4259
4260 if (lpLVItem->mask & LVIF_IMAGE)
4261 lpItem->hdr.iImage = lpLVItem->iImage;
4262
4263 if (lpLVItem->mask & LVIF_PARAM)
4264 lpItem->lParam = lpLVItem->lParam;
4265
4266 if (lpLVItem->mask & LVIF_INDENT)
4267 lpItem->iIndent = lpLVItem->iIndent;
4268
4269 if (uChanged & LVIF_STATE)
4270 {
4271 if (lpItem && (stateMask & ~infoPtr->uCallbackMask))
4272 {
4273 lpItem->state &= ~stateMask;
4274 lpItem->state |= (lpLVItem->state & stateMask);
4275 }
4276 if (lpLVItem->state & stateMask & ~infoPtr->uCallbackMask & LVIS_SELECTED)
4277 {
4278 if (infoPtr->dwStyle & LVS_SINGLESEL) LISTVIEW_DeselectAllSkipItem(infoPtr, lpLVItem->iItem);
4279 ranges_additem(infoPtr->selectionRanges, lpLVItem->iItem);
4280 }
4281 else if (stateMask & LVIS_SELECTED)
4282 {
4283 ranges_delitem(infoPtr->selectionRanges, lpLVItem->iItem);
4284 }
4285 /* If we are asked to change focus, and we manage it, do it.
4286 It's important to have all new item data stored at this point,
4287 because changing existing focus could result in a redrawing operation,
4288 which in turn could ask for disp data, application should see all data
4289 for inserted item when processing LVN_GETDISPINFO.
4290
4291 The way this works application will see nested item change notifications -
4292 changed item notifications interrupted by ones from item losing focus. */
4293 if (stateMask & ~infoPtr->uCallbackMask & LVIS_FOCUSED)
4294 {
4295 if (lpLVItem->state & LVIS_FOCUSED)
4296 {
4297 /* update selection mark */
4298 if (infoPtr->nFocusedItem == -1 && infoPtr->nSelectionMark == -1)
4299 infoPtr->nSelectionMark = lpLVItem->iItem;
4300
4301 if (infoPtr->nFocusedItem != -1)
4302 {
4303 /* remove current focus */
4304 item.mask = LVIF_STATE;
4305 item.state = 0;
4306 item.stateMask = LVIS_FOCUSED;
4307
4308 /* recurse with redrawing an item */
4309 LISTVIEW_SetItemState(infoPtr, infoPtr->nFocusedItem, &item);
4310 }
4311
4312 infoPtr->nFocusedItem = lpLVItem->iItem;
4313 LISTVIEW_EnsureVisible(infoPtr, lpLVItem->iItem, infoPtr->uView == LV_VIEW_LIST);
4314 }
4315 else if (infoPtr->nFocusedItem == lpLVItem->iItem)
4316 {
4317 infoPtr->nFocusedItem = -1;
4318 }
4319 }
4320 }
4321
4322 /* if we're inserting the item, we're done */
4323 if (isNew) return TRUE;
4324
4325 /* send LVN_ITEMCHANGED notification */
4326 if (lpLVItem->mask & LVIF_PARAM) nmlv.lParam = lpLVItem->lParam;
4327 if (infoPtr->bDoChangeNotify) notify_listview(infoPtr, LVN_ITEMCHANGED, &nmlv);
4328
4329 return TRUE;
4330 }
4331
4332 /***
4333 * DESCRIPTION:
4334 * Helper for LISTVIEW_{Set,Insert}ItemT *only*: sets subitem attributes.
4335 *
4336 * PARAMETER(S):
4337 * [I] infoPtr : valid pointer to the listview structure
4338 * [I] lpLVItem : valid pointer to new subitem attributes
4339 * [I] isW : TRUE if lpLVItem is Unicode, FALSE if it's ANSI
4340 * [O] bChanged : will be set to TRUE if the item really changed
4341 *
4342 * RETURN:
4343 * SUCCESS : TRUE
4344 * FAILURE : FALSE
4345 */
4346 static BOOL set_sub_item(const LISTVIEW_INFO *infoPtr, const LVITEMW *lpLVItem, BOOL isW, BOOL *bChanged)
4347 {
4348 HDPA hdpaSubItems;
4349 SUBITEM_INFO *lpSubItem;
4350
4351 /* we do not support subitems for virtual listviews */
4352 if (infoPtr->dwStyle & LVS_OWNERDATA) return FALSE;
4353
4354 /* set subitem only if column is present */
4355 if (lpLVItem->iSubItem >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return FALSE;
4356
4357 /* First do some sanity checks */
4358 /* The LVIF_STATE flag is valid for subitems, but does not appear to be
4359 particularly useful. We currently do not actually do anything with
4360 the flag on subitems.
4361 */
4362 if (lpLVItem->mask & ~(LVIF_TEXT | LVIF_IMAGE | LVIF_STATE | LVIF_DI_SETITEM)) return FALSE;
4363 if (!(lpLVItem->mask & (LVIF_TEXT | LVIF_IMAGE | LVIF_STATE))) return TRUE;
4364
4365 /* get the subitem structure, and create it if not there */
4366 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, lpLVItem->iItem);
4367 assert (hdpaSubItems);
4368
4369 lpSubItem = LISTVIEW_GetSubItemPtr(hdpaSubItems, lpLVItem->iSubItem);
4370 if (!lpSubItem)
4371 {
4372 SUBITEM_INFO *tmpSubItem;
4373 INT i;
4374
4375 lpSubItem = Alloc(sizeof(SUBITEM_INFO));
4376 if (!lpSubItem) return FALSE;
4377 /* we could binary search here, if need be...*/
4378 for (i = 1; i < DPA_GetPtrCount(hdpaSubItems); i++)
4379 {
4380 tmpSubItem = DPA_GetPtr(hdpaSubItems, i);
4381 if (tmpSubItem->iSubItem > lpLVItem->iSubItem) break;
4382 }
4383 if (DPA_InsertPtr(hdpaSubItems, i, lpSubItem) == -1)
4384 {
4385 Free(lpSubItem);
4386 return FALSE;
4387 }
4388 lpSubItem->iSubItem = lpLVItem->iSubItem;
4389 lpSubItem->hdr.iImage = I_IMAGECALLBACK;
4390 *bChanged = TRUE;
4391 }
4392
4393 if ((lpLVItem->mask & LVIF_IMAGE) && (lpSubItem->hdr.iImage != lpLVItem->iImage))
4394 {
4395 lpSubItem->hdr.iImage = lpLVItem->iImage;
4396 *bChanged = TRUE;
4397 }
4398
4399 if ((lpLVItem->mask & LVIF_TEXT) && textcmpWT(lpSubItem->hdr.pszText, lpLVItem->pszText, isW))
4400 {
4401 textsetptrT(&lpSubItem->hdr.pszText, lpLVItem->pszText, isW);
4402 *bChanged = TRUE;
4403 }
4404
4405 return TRUE;
4406 }
4407
4408 /***
4409 * DESCRIPTION:
4410 * Sets item attributes.
4411 *
4412 * PARAMETER(S):
4413 * [I] infoPtr : valid pointer to the listview structure
4414 * [I] lpLVItem : new item attributes
4415 * [I] isW : TRUE if lpLVItem is Unicode, FALSE if it's ANSI
4416 *
4417 * RETURN:
4418 * SUCCESS : TRUE
4419 * FAILURE : FALSE
4420 */
4421 static BOOL LISTVIEW_SetItemT(LISTVIEW_INFO *infoPtr, LVITEMW *lpLVItem, BOOL isW)
4422 {
4423 HWND hwndSelf = infoPtr->hwndSelf;
4424 LPWSTR pszText = NULL;
4425 BOOL bResult, bChanged = FALSE;
4426 RECT oldItemArea;
4427
4428 TRACE("(lpLVItem=%s, isW=%d)\n", debuglvitem_t(lpLVItem, isW), isW);
4429
4430 if (!lpLVItem || lpLVItem->iItem < 0 || lpLVItem->iItem >= infoPtr->nItemCount)
4431 return FALSE;
4432
4433 /* Store old item area */
4434 LISTVIEW_GetItemBox(infoPtr, lpLVItem->iItem, &oldItemArea);
4435
4436 /* For efficiency, we transform the lpLVItem->pszText to Unicode here */
4437 if ((lpLVItem->mask & LVIF_TEXT) && is_text(lpLVItem->pszText))
4438 {
4439 pszText = lpLVItem->pszText;
4440 lpLVItem->pszText = textdupTtoW(lpLVItem->pszText, isW);
4441 }
4442
4443 /* actually set the fields */
4444 if (!is_assignable_item(lpLVItem, infoPtr->dwStyle)) return FALSE;
4445
4446 if (lpLVItem->iSubItem)
4447 bResult = set_sub_item(infoPtr, lpLVItem, TRUE, &bChanged);
4448 else
4449 bResult = set_main_item(infoPtr, lpLVItem, FALSE, TRUE, &bChanged);
4450 if (!IsWindow(hwndSelf))
4451 return FALSE;
4452
4453 /* redraw item, if necessary */
4454 if (bChanged && !infoPtr->bIsDrawing)
4455 {
4456 /* this little optimization eliminates some nasty flicker */
4457 if ( infoPtr->uView == LV_VIEW_DETAILS && !(infoPtr->dwStyle & LVS_OWNERDRAWFIXED) &&
4458 !(infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT) &&
4459 lpLVItem->iSubItem > 0 && lpLVItem->iSubItem <= DPA_GetPtrCount(infoPtr->hdpaColumns) )
4460 LISTVIEW_InvalidateSubItem(infoPtr, lpLVItem->iItem, lpLVItem->iSubItem);
4461 else
4462 {
4463 LISTVIEW_InvalidateRect(infoPtr, &oldItemArea);
4464 LISTVIEW_InvalidateItem(infoPtr, lpLVItem->iItem);
4465 }
4466 }
4467 /* restore text */
4468 if (pszText)
4469 {
4470 textfreeT(lpLVItem->pszText, isW);
4471 lpLVItem->pszText = pszText;
4472 }
4473
4474 return bResult;
4475 }
4476
4477 /***
4478 * DESCRIPTION:
4479 * Retrieves the index of the item at coordinate (0, 0) of the client area.
4480 *
4481 * PARAMETER(S):
4482 * [I] infoPtr : valid pointer to the listview structure
4483 *
4484 * RETURN:
4485 * item index
4486 */
4487 static INT LISTVIEW_GetTopIndex(const LISTVIEW_INFO *infoPtr)
4488 {
4489 INT nItem = 0;
4490 SCROLLINFO scrollInfo;
4491
4492 scrollInfo.cbSize = sizeof(SCROLLINFO);
4493 scrollInfo.fMask = SIF_POS;
4494
4495 if (infoPtr->uView == LV_VIEW_LIST)
4496 {
4497 if (GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo))
4498 nItem = scrollInfo.nPos * LISTVIEW_GetCountPerColumn(infoPtr);
4499 }
4500 else if (infoPtr->uView == LV_VIEW_DETAILS)
4501 {
4502 if (GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo))
4503 nItem = scrollInfo.nPos;
4504 }
4505 else
4506 {
4507 if (GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo))
4508 nItem = LISTVIEW_GetCountPerRow(infoPtr) * (scrollInfo.nPos / infoPtr->nItemHeight);
4509 }
4510
4511 TRACE("nItem=%d\n", nItem);
4512
4513 return nItem;
4514 }
4515
4516
4517 /***
4518 * DESCRIPTION:
4519 * Erases the background of the given rectangle
4520 *
4521 * PARAMETER(S):
4522 * [I] infoPtr : valid pointer to the listview structure
4523 * [I] hdc : device context handle
4524 * [I] lprcBox : clipping rectangle
4525 *
4526 * RETURN:
4527 * Success: TRUE
4528 * Failure: FALSE
4529 */
4530 static inline BOOL LISTVIEW_FillBkgnd(const LISTVIEW_INFO *infoPtr, HDC hdc, const RECT *lprcBox)
4531 {
4532 if (!infoPtr->hBkBrush) return FALSE;
4533
4534 TRACE("(hdc=%p, lprcBox=%s, hBkBrush=%p)\n", hdc, wine_dbgstr_rect(lprcBox), infoPtr->hBkBrush);
4535
4536 return FillRect(hdc, lprcBox, infoPtr->hBkBrush);
4537 }
4538
4539 /***
4540 * DESCRIPTION:
4541 * Draws an item.
4542 *
4543 * PARAMETER(S):
4544 * [I] infoPtr : valid pointer to the listview structure
4545 * [I] hdc : device context handle
4546 * [I] nItem : item index
4547 * [I] nSubItem : subitem index
4548 * [I] pos : item position in client coordinates
4549 * [I] cdmode : custom draw mode
4550 *
4551 * RETURN:
4552 * Success: TRUE
4553 * Failure: FALSE
4554 */
4555 static BOOL LISTVIEW_DrawItem(LISTVIEW_INFO *infoPtr, HDC hdc, INT nItem, INT nSubItem, POINT pos, DWORD cdmode)
4556 {
4557 UINT uFormat;
4558 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
4559 static WCHAR szCallback[] = { '(', 'c', 'a', 'l', 'l', 'b', 'a', 'c', 'k', ')', 0 };
4560 DWORD cdsubitemmode = CDRF_DODEFAULT;
4561 LPRECT lprcFocus;
4562 RECT rcSelect, rcBox, rcIcon, rcLabel, rcStateIcon;
4563 NMLVCUSTOMDRAW nmlvcd;
4564 HIMAGELIST himl;
4565 LVITEMW lvItem;
4566
4567 TRACE("(hdc=%p, nItem=%d, nSubItem=%d, pos=%s)\n", hdc, nItem, nSubItem, wine_dbgstr_point(&pos));
4568
4569 /* get information needed for drawing the item */
4570 lvItem.mask = LVIF_TEXT | LVIF_IMAGE | LVIF_PARAM;
4571 if (nSubItem == 0) lvItem.mask |= LVIF_STATE;
4572 if (infoPtr->uView == LV_VIEW_DETAILS) lvItem.mask |= LVIF_INDENT;
4573 lvItem.stateMask = LVIS_SELECTED | LVIS_FOCUSED | LVIS_STATEIMAGEMASK | LVIS_CUT | LVIS_OVERLAYMASK;
4574 lvItem.iItem = nItem;
4575 lvItem.iSubItem = nSubItem;
4576 lvItem.state = 0;
4577 lvItem.lParam = 0;
4578 lvItem.cchTextMax = DISP_TEXT_SIZE;
4579 lvItem.pszText = szDispText;
4580 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) return FALSE;
4581 if (nSubItem > 0 && (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT))
4582 lvItem.state = LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED);
4583 if (lvItem.pszText == LPSTR_TEXTCALLBACKW) lvItem.pszText = szCallback;
4584 TRACE(" lvItem=%s\n", debuglvitem_t(&lvItem, TRUE));
4585
4586 /* now check if we need to update the focus rectangle */
4587 lprcFocus = infoPtr->bFocus && (lvItem.state & LVIS_FOCUSED) ? &infoPtr->rcFocus : 0;
4588
4589 if (!lprcFocus) lvItem.state &= ~LVIS_FOCUSED;
4590 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, &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(" rcBox=%s, rcSelect=%s, rcIcon=%s. rcLabel=%s\n",
4597 wine_dbgstr_rect(&rcBox), wine_dbgstr_rect(&rcSelect),
4598 wine_dbgstr_rect(&rcIcon), wine_dbgstr_rect(&rcLabel));
4599
4600 /* fill in the custom draw structure */
4601 customdraw_fill(&nmlvcd, infoPtr, hdc, &rcBox, &lvItem);
4602
4603 if (nSubItem > 0) cdmode = infoPtr->cditemmode;
4604 if (cdmode & CDRF_SKIPDEFAULT) goto postpaint;
4605 if (cdmode & CDRF_NOTIFYITEMDRAW)
4606 cdsubitemmode = notify_customdraw(infoPtr, CDDS_PREPAINT, &nmlvcd);
4607 if (nSubItem == 0) infoPtr->cditemmode = cdsubitemmode;
4608 if (cdsubitemmode & CDRF_SKIPDEFAULT) goto postpaint;
4609 /* we have to send a CDDS_SUBITEM customdraw explicitly for subitem 0 */
4610 if (nSubItem == 0 && (cdsubitemmode & CDRF_NOTIFYITEMDRAW) != 0)
4611 {
4612 cdsubitemmode = notify_customdraw(infoPtr, CDDS_SUBITEM | CDDS_ITEMPREPAINT, &nmlvcd);
4613 if (cdsubitemmode & CDRF_SKIPDEFAULT) goto postpaint;
4614 }
4615 if (nSubItem == 0 || (cdmode & CDRF_NOTIFYITEMDRAW))
4616 prepaint_setup(infoPtr, hdc, &nmlvcd, FALSE);
4617 else if ((infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT) == FALSE)
4618 prepaint_setup(infoPtr, hdc, &nmlvcd, TRUE);
4619
4620 /* FIXME: temporary hack */
4621 rcSelect.left = rcLabel.left;
4622
4623 /* in icon mode, the label rect is really what we want to draw the
4624 * background for */
4625 /* in detail mode, we want to paint background for label rect when
4626 * item is not selected or listview has full row select; otherwise paint
4627 * background for text only */
4628 if (infoPtr->uView == LV_VIEW_ICON ||
4629 (infoPtr->uView == LV_VIEW_DETAILS &&
4630 (!(lvItem.state & LVIS_SELECTED) ||
4631 (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT) != 0)))
4632 rcSelect = rcLabel;
4633
4634 if (nmlvcd.clrTextBk != CLR_NONE)
4635 ExtTextOutW(hdc, rcSelect.left, rcSelect.top, ETO_OPAQUE, &rcSelect, NULL, 0, NULL);
4636
4637 if(nSubItem == 0 && infoPtr->nFocusedItem == nItem)
4638 {
4639 if (infoPtr->uView == LV_VIEW_DETAILS && (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT))
4640 {
4641 /* we have to update left focus bound too if item isn't in leftmost column
4642 and reduce right box bound */
4643 if (DPA_GetPtrCount(infoPtr->hdpaColumns) > 0)
4644 {
4645 INT leftmost;
4646
4647 if ((leftmost = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX, 0, 0)))
4648 {
4649 INT Originx = pos.x - LISTVIEW_GetColumnInfo(infoPtr, 0)->rcHeader.left;
4650 INT index = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX,
4651 DPA_GetPtrCount(infoPtr->hdpaColumns) - 1, 0);
4652
4653 rcBox.right = LISTVIEW_GetColumnInfo(infoPtr, index)->rcHeader.right + Originx;
4654 rcSelect.left = LISTVIEW_GetColumnInfo(infoPtr, leftmost)->rcHeader.left + Originx;
4655 }
4656 }
4657
4658 rcSelect.right = rcBox.right;
4659 }
4660
4661 /* store new focus rectangle */
4662 infoPtr->rcFocus = rcSelect;
4663 }
4664
4665 /* state icons */
4666 if (infoPtr->himlState && STATEIMAGEINDEX(lvItem.state) && (nSubItem == 0))
4667 {
4668 UINT uStateImage = STATEIMAGEINDEX(lvItem.state);
4669 if (uStateImage)
4670 {
4671 TRACE("uStateImage=%d\n", uStateImage);
4672 ImageList_Draw(infoPtr->himlState, uStateImage - 1, hdc,
4673 rcStateIcon.left, rcStateIcon.top, ILD_NORMAL);
4674 }
4675 }
4676
4677 /* item icons */
4678 himl = (infoPtr->uView == LV_VIEW_ICON ? infoPtr->himlNormal : infoPtr->himlSmall);
4679 if (himl && lvItem.iImage >= 0 && !IsRectEmpty(&rcIcon))
4680 {
4681 UINT style;
4682
4683 TRACE("iImage=%d\n", lvItem.iImage);
4684
4685 if (lvItem.state & (LVIS_SELECTED | LVIS_CUT) && infoPtr->bFocus)
4686 style = ILD_SELECTED;
4687 else
4688 style = ILD_NORMAL;
4689
4690 ImageList_DrawEx(himl, lvItem.iImage, hdc, rcIcon.left, rcIcon.top,
4691 rcIcon.right - rcIcon.left, rcIcon.bottom - rcIcon.top, infoPtr->clrBk,
4692 lvItem.state & LVIS_CUT ? RGB(255, 255, 255) : CLR_DEFAULT,
4693 style | (lvItem.state & LVIS_OVERLAYMASK));
4694 }
4695
4696 /* Don't bother painting item being edited */
4697 if (infoPtr->hwndEdit && nItem == infoPtr->nEditLabelItem && nSubItem == 0) goto postpaint;
4698
4699 /* figure out the text drawing flags */
4700 uFormat = (infoPtr->uView == LV_VIEW_ICON ? (lprcFocus ? LV_FL_DT_FLAGS : LV_ML_DT_FLAGS) : LV_SL_DT_FLAGS);
4701 if (infoPtr->uView == LV_VIEW_ICON)
4702 uFormat = (lprcFocus ? LV_FL_DT_FLAGS : LV_ML_DT_FLAGS);
4703 else if (nSubItem)
4704 {
4705 switch (LISTVIEW_GetColumnInfo(infoPtr, nSubItem)->fmt & LVCFMT_JUSTIFYMASK)
4706 {
4707 case LVCFMT_RIGHT: uFormat |= DT_RIGHT; break;
4708 case LVCFMT_CENTER: uFormat |= DT_CENTER; break;
4709 default: uFormat |= DT_LEFT;
4710 }
4711 }
4712 if (!(uFormat & (DT_RIGHT | DT_CENTER)))
4713 {
4714 if (himl && lvItem.iImage >= 0 && !IsRectEmpty(&rcIcon)) rcLabel.left += IMAGE_PADDING;
4715 else rcLabel.left += LABEL_HOR_PADDING;
4716 }
4717 else if (uFormat & DT_RIGHT) rcLabel.right -= LABEL_HOR_PADDING;
4718
4719 /* for GRIDLINES reduce the bottom so the text formats correctly */
4720 if (infoPtr->uView == LV_VIEW_DETAILS && infoPtr->dwLvExStyle & LVS_EX_GRIDLINES)
4721 rcLabel.bottom--;
4722
4723 if ((!(lvItem.state & LVIS_SELECTED) || !infoPtr->bFocus) && (infoPtr->dwLvExStyle & LVS_EX_TRANSPARENTSHADOWTEXT))
4724 DrawShadowText(hdc, lvItem.pszText, -1, &rcLabel, uFormat, RGB(255, 255, 255), RGB(0, 0, 0), 2, 2);
4725 else
4726 DrawTextW(hdc, lvItem.pszText, -1, &rcLabel, uFormat);
4727
4728 postpaint:
4729 if (cdsubitemmode & CDRF_NOTIFYPOSTPAINT)
4730 notify_postpaint(infoPtr, &nmlvcd);
4731 return TRUE;
4732 }
4733
4734 /***
4735 * DESCRIPTION:
4736 * Draws listview items when in owner draw mode.
4737 *
4738 * PARAMETER(S):
4739 * [I] infoPtr : valid pointer to the listview structure
4740 * [I] hdc : device context handle
4741 *
4742 * RETURN:
4743 * None
4744 */
4745 static void LISTVIEW_RefreshOwnerDraw(const LISTVIEW_INFO *infoPtr, ITERATOR *i, HDC hdc, DWORD cdmode)
4746 {
4747 UINT uID = (UINT)GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
4748 DWORD cditemmode = CDRF_DODEFAULT;
4749 NMLVCUSTOMDRAW nmlvcd;
4750 POINT Origin, Position;
4751 DRAWITEMSTRUCT dis;
4752 LVITEMW item;
4753
4754 TRACE("()\n");
4755
4756 ZeroMemory(&dis, sizeof(dis));
4757
4758 /* Get scroll info once before loop */
4759 LISTVIEW_GetOrigin(infoPtr, &Origin);
4760
4761 /* iterate through the invalidated rows */
4762 while(iterator_next(i))
4763 {
4764 item.iItem = i->nItem;
4765 item.iSubItem = 0;
4766 item.mask = LVIF_PARAM | LVIF_STATE;
4767 item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
4768 if (!LISTVIEW_GetItemW(infoPtr, &item)) continue;
4769
4770 dis.CtlType = ODT_LISTVIEW;
4771 dis.CtlID = uID;
4772 dis.itemID = item.iItem;
4773 dis.itemAction = ODA_DRAWENTIRE;
4774 dis.itemState = 0;
4775 if (item.state & LVIS_SELECTED) dis.itemState |= ODS_SELECTED;
4776 if (infoPtr->bFocus && (item.state & LVIS_FOCUSED)) dis.itemState |= ODS_FOCUS;
4777 dis.hwndItem = infoPtr->hwndSelf;
4778 dis.hDC = hdc;
4779 LISTVIEW_GetItemOrigin(infoPtr, dis.itemID, &Position);
4780 dis.rcItem.left = Position.x + Origin.x;
4781 dis.rcItem.right = dis.rcItem.left + infoPtr->nItemWidth;
4782 dis.rcItem.top = Position.y + Origin.y;
4783 dis.rcItem.bottom = dis.rcItem.top + infoPtr->nItemHeight;
4784 dis.itemData = item.lParam;
4785
4786 TRACE("item=%s, rcItem=%s\n", debuglvitem_t(&item, TRUE), wine_dbgstr_rect(&dis.rcItem));
4787
4788 /*
4789 * Even if we do not send the CDRF_NOTIFYITEMDRAW we need to fill the nmlvcd
4790 * structure for the rest. of the paint cycle
4791 */
4792 customdraw_fill(&nmlvcd, infoPtr, hdc, &dis.rcItem, &item);
4793 if (cdmode & CDRF_NOTIFYITEMDRAW)
4794 cditemmode = notify_customdraw(infoPtr, CDDS_PREPAINT, &nmlvcd);
4795
4796 if (!(cditemmode & CDRF_SKIPDEFAULT))
4797 {
4798 prepaint_setup (infoPtr, hdc, &nmlvcd, FALSE);
4799 SendMessageW(infoPtr->hwndNotify, WM_DRAWITEM, dis.CtlID, (LPARAM)&dis);
4800 }
4801
4802 if (cditemmode & CDRF_NOTIFYPOSTPAINT)
4803 notify_postpaint(infoPtr, &nmlvcd);
4804 }
4805 }
4806
4807 /***
4808 * DESCRIPTION:
4809 * Draws listview items when in report display mode.
4810 *
4811 * PARAMETER(S):
4812 * [I] infoPtr : valid pointer to the listview structure
4813 * [I] hdc : device context handle
4814 * [I] cdmode : custom draw mode
4815 *
4816 * RETURN:
4817 * None
4818 */
4819 static void LISTVIEW_RefreshReport(LISTVIEW_INFO *infoPtr, ITERATOR *i, HDC hdc, DWORD cdmode)
4820 {
4821 INT rgntype;
4822 RECT rcClip, rcItem;
4823 POINT Origin, Position;
4824 RANGES colRanges;
4825 INT col, index;
4826 ITERATOR j;
4827
4828 TRACE("()\n");
4829
4830 /* figure out what to draw */
4831 rgntype = GetClipBox(hdc, &rcClip);
4832 if (rgntype == NULLREGION) return;
4833
4834 /* Get scroll info once before loop */
4835 LISTVIEW_GetOrigin(infoPtr, &Origin);
4836
4837 colRanges = ranges_create(DPA_GetPtrCount(infoPtr->hdpaColumns));
4838
4839 /* narrow down the columns we need to paint */
4840 for(col = 0; col < DPA_GetPtrCount(infoPtr->hdpaColumns); col++)
4841 {
4842 index = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX, col, 0);
4843
4844 LISTVIEW_GetHeaderRect(infoPtr, index, &rcItem);
4845 if ((rcItem.right + Origin.x >= rcClip.left) && (rcItem.left + Origin.x < rcClip.right))
4846 ranges_additem(colRanges, index);
4847 }
4848 iterator_rangesitems(&j, colRanges);
4849
4850 /* in full row select, we _have_ to draw the main item */
4851 if (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT)
4852 j.nSpecial = 0;
4853
4854 /* iterate through the invalidated rows */
4855 while(iterator_next(i))
4856 {
4857 SelectObject(hdc, infoPtr->hFont);
4858 LISTVIEW_GetItemOrigin(infoPtr, i->nItem, &Position);
4859 Position.y += Origin.y;
4860
4861 /* iterate through the invalidated columns */
4862 while(iterator_next(&j))
4863 {
4864 LISTVIEW_GetHeaderRect(infoPtr, j.nItem, &rcItem);
4865 Position.x = (j.nItem == 0) ? rcItem.left + Origin.x : Origin.x;
4866
4867 if (rgntype == COMPLEXREGION && !((infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT) && j.nItem == 0))
4868 {
4869 rcItem.top = 0;
4870 rcItem.bottom = infoPtr->nItemHeight;
4871 OffsetRect(&rcItem, Origin.x, Position.y);
4872 if (!RectVisible(hdc, &rcItem)) continue;
4873 }
4874
4875 LISTVIEW_DrawItem(infoPtr, hdc, i->nItem, j.nItem, Position, cdmode);
4876 }
4877 }
4878 iterator_destroy(&j);
4879 }
4880
4881 /***
4882 * DESCRIPTION:
4883 * Draws the gridlines if necessary when in report display mode.
4884 *
4885 * PARAMETER(S):
4886 * [I] infoPtr : valid pointer to the listview structure
4887 * [I] hdc : device context handle
4888 *
4889 * RETURN:
4890 * None
4891 */
4892 static void LISTVIEW_RefreshReportGrid(LISTVIEW_INFO *infoPtr, HDC hdc)
4893 {
4894 INT rgntype;
4895 INT y, itemheight;
4896 INT col, index;
4897 HPEN hPen, hOldPen;
4898 RECT rcClip, rcItem = {0};
4899 POINT Origin;
4900 RANGES colRanges;
4901 ITERATOR j;
4902 BOOL rmost = FALSE;
4903
4904 TRACE("()\n");
4905
4906 /* figure out what to draw */
4907 rgntype = GetClipBox(hdc, &rcClip);
4908 if (rgntype == NULLREGION) return;
4909
4910 /* Get scroll info once before loop */
4911 LISTVIEW_GetOrigin(infoPtr, &Origin);
4912
4913 colRanges = ranges_create(DPA_GetPtrCount(infoPtr->hdpaColumns));
4914
4915 /* narrow down the columns we need to paint */
4916 for(col = 0; col < DPA_GetPtrCount(infoPtr->hdpaColumns); col++)
4917 {
4918 index = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX, col, 0);
4919
4920 LISTVIEW_GetHeaderRect(infoPtr, index, &rcItem);
4921 if ((rcItem.right + Origin.x >= rcClip.left) && (rcItem.left + Origin.x < rcClip.right))
4922 ranges_additem(colRanges, index);
4923 }
4924
4925 /* is right most vertical line visible? */
4926 if (DPA_GetPtrCount(infoPtr->hdpaColumns) > 0)
4927 {
4928 index = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX, DPA_GetPtrCount(infoPtr->hdpaColumns) - 1, 0);
4929 LISTVIEW_GetHeaderRect(infoPtr, index, &rcItem);
4930 rmost = (rcItem.right + Origin.x < rcClip.right);
4931 }
4932
4933 if ((hPen = CreatePen( PS_SOLID, 1, comctl32_color.clr3dFace )))
4934 {
4935 hOldPen = SelectObject ( hdc, hPen );
4936
4937 /* draw the vertical lines for the columns */
4938 iterator_rangesitems(&j, colRanges);
4939 while(iterator_next(&j))
4940 {
4941 LISTVIEW_GetHeaderRect(infoPtr, j.nItem, &rcItem);
4942 if (rcItem.left == 0) continue; /* skip leftmost column */
4943 rcItem.left += Origin.x;
4944 rcItem.right += Origin.x;
4945 rcItem.top = infoPtr->rcList.top;
4946 rcItem.bottom = infoPtr->rcList.bottom;
4947 TRACE("vert col=%d, rcItem=%s\n", j.nItem, wine_dbgstr_rect(&rcItem));
4948 MoveToEx (hdc, rcItem.left, rcItem.top, NULL);
4949 LineTo (hdc, rcItem.left, rcItem.bottom);
4950 }
4951 iterator_destroy(&j);
4952 /* draw rightmost grid line if visible */
4953 if (rmost)
4954 {
4955 index = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX,
4956 DPA_GetPtrCount(infoPtr->hdpaColumns) - 1, 0);
4957 LISTVIEW_GetHeaderRect(infoPtr, index, &rcItem);
4958
4959 rcItem.right += Origin.x;
4960
4961 MoveToEx (hdc, rcItem.right, infoPtr->rcList.top, NULL);
4962 LineTo (hdc, rcItem.right, infoPtr->rcList.bottom);
4963 }
4964
4965 /* draw the horizontal lines for the rows */
4966 itemheight = LISTVIEW_CalculateItemHeight(infoPtr);
4967 rcItem.left = infoPtr->rcList.left;
4968 rcItem.right = infoPtr->rcList.right;
4969 for(y = Origin.y > 1 ? Origin.y - 1 : itemheight - 1 + Origin.y % itemheight; y<=infoPtr->rcList.bottom; y+=itemheight)
4970 {
4971 rcItem.bottom = rcItem.top = y;
4972 TRACE("horz rcItem=%s\n", wine_dbgstr_rect(&rcItem));
4973 MoveToEx (hdc, rcItem.left, rcItem.top, NULL);
4974 LineTo (hdc, rcItem.right, rcItem.top);
4975 }
4976
4977 SelectObject( hdc, hOldPen );
4978 DeleteObject( hPen );
4979 }
4980 else
4981 ranges_destroy(colRanges);
4982 }
4983
4984 /***
4985 * DESCRIPTION:
4986 * Draws listview items when in list display mode.
4987 *
4988 * PARAMETER(S):
4989 * [I] infoPtr : valid pointer to the listview structure
4990 * [I] hdc : device context handle
4991 * [I] cdmode : custom draw mode
4992 *
4993 * RETURN:
4994 * None
4995 */
4996 static void LISTVIEW_RefreshList(LISTVIEW_INFO *infoPtr, ITERATOR *i, HDC hdc, DWORD cdmode)
4997 {
4998 POINT Origin, Position;
4999
5000 /* Get scroll info once before loop */
5001 LISTVIEW_GetOrigin(infoPtr, &Origin);
5002
5003 while(iterator_prev(i))
5004 {
5005 SelectObject(hdc, infoPtr->hFont);
5006 LISTVIEW_GetItemOrigin(infoPtr, i->nItem, &Position);
5007 Position.x += Origin.x;
5008 Position.y += Origin.y;
5009
5010 LISTVIEW_DrawItem(infoPtr, hdc, i->nItem, 0, Position, cdmode);
5011 }
5012 }
5013
5014
5015 /***
5016 * DESCRIPTION:
5017 * Draws listview items.
5018 *
5019 * PARAMETER(S):
5020 * [I] infoPtr : valid pointer to the listview structure
5021 * [I] hdc : device context handle
5022 * [I] prcErase : rect to be erased before refresh (may be NULL)
5023 *
5024 * RETURN:
5025 * NoneX
5026 */
5027 static void LISTVIEW_Refresh(LISTVIEW_INFO *infoPtr, HDC hdc, const RECT *prcErase)
5028 {
5029 COLORREF oldTextColor = 0, oldBkColor = 0;
5030 NMLVCUSTOMDRAW nmlvcd;
5031 HFONT hOldFont = 0;
5032 DWORD cdmode;
5033 INT oldBkMode = 0;
5034 RECT rcClient;
5035 ITERATOR i;
5036 HDC hdcOrig = hdc;
5037 HBITMAP hbmp = NULL;
5038 RANGE range;
5039
5040 LISTVIEW_DUMP(infoPtr);
5041
5042 if (infoPtr->dwLvExStyle & LVS_EX_DOUBLEBUFFER) {
5043 TRACE("double buffering\n");
5044
5045 hdc = CreateCompatibleDC(hdcOrig);
5046 if (!hdc) {
5047 ERR("Failed to create DC for backbuffer\n");
5048 return;
5049 }
5050 hbmp = CreateCompatibleBitmap(hdcOrig, infoPtr->rcList.right,
5051 infoPtr->rcList.bottom);
5052 if (!hbmp) {
5053 ERR("Failed to create bitmap for backbuffer\n");
5054 DeleteDC(hdc);
5055 return;
5056 }
5057
5058 SelectObject(hdc, hbmp);
5059 SelectObject(hdc, infoPtr->hFont);
5060
5061 if(GetClipBox(hdcOrig, &rcClient))
5062 IntersectClipRect(hdc, rcClient.left, rcClient.top, rcClient.right, rcClient.bottom);
5063 } else {
5064 /* Save dc values we're gonna trash while drawing
5065 * FIXME: Should be done in LISTVIEW_DrawItem() */
5066 hOldFont = SelectObject(hdc, infoPtr->hFont);
5067 oldBkMode = GetBkMode(hdc);
5068 oldBkColor = GetBkColor(hdc);
5069 oldTextColor = GetTextColor(hdc);
5070 }
5071
5072 infoPtr->bIsDrawing = TRUE;
5073
5074 if (prcErase) {
5075 LISTVIEW_FillBkgnd(infoPtr, hdc, prcErase);
5076 } else if (infoPtr->dwLvExStyle & LVS_EX_DOUBLEBUFFER) {
5077 /* If no erasing was done (usually because RedrawWindow was called
5078 * with RDW_INVALIDATE only) we need to copy the old contents into
5079 * the backbuffer before continuing. */
5080 BitBlt(hdc, infoPtr->rcList.left, infoPtr->rcList.top,
5081 infoPtr->rcList.right - infoPtr->rcList.left,
5082 infoPtr->rcList.bottom - infoPtr->rcList.top,
5083 hdcOrig, infoPtr->rcList.left, infoPtr->rcList.top, SRCCOPY);
5084 }
5085
5086 infoPtr->cditemmode = CDRF_DODEFAULT;
5087
5088 GetClientRect(infoPtr->hwndSelf, &rcClient);
5089 customdraw_fill(&nmlvcd, infoPtr, hdc, &rcClient, 0);
5090 cdmode = notify_customdraw(infoPtr, CDDS_PREPAINT, &nmlvcd);
5091 if (cdmode & CDRF_SKIPDEFAULT) goto enddraw;
5092
5093 /* nothing to draw */
5094 if(infoPtr->nItemCount == 0) goto enddraw;
5095
5096 /* figure out what we need to draw */
5097 iterator_visibleitems(&i, infoPtr, hdc);
5098 range = iterator_range(&i);
5099
5100 /* send cache hint notification */
5101 if (infoPtr->dwStyle & LVS_OWNERDATA)
5102 {
5103 NMLVCACHEHINT nmlv;
5104
5105 ZeroMemory(&nmlv, sizeof(NMLVCACHEHINT));
5106 nmlv.iFrom = range.lower;
5107 nmlv.iTo = range.upper - 1;
5108 notify_hdr(infoPtr, LVN_ODCACHEHINT, &nmlv.hdr);
5109 }
5110
5111 if ((infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && (infoPtr->uView == LV_VIEW_DETAILS))
5112 LISTVIEW_RefreshOwnerDraw(infoPtr, &i, hdc, cdmode);
5113 else
5114 {
5115 if (infoPtr->uView == LV_VIEW_DETAILS)
5116 LISTVIEW_RefreshReport(infoPtr, &i, hdc, cdmode);
5117 else /* LV_VIEW_LIST, LV_VIEW_ICON or LV_VIEW_SMALLICON */
5118 LISTVIEW_RefreshList(infoPtr, &i, hdc, cdmode);
5119
5120 /* if we have a focus rect and it's visible, draw it */
5121 if (infoPtr->bFocus && range.lower <= infoPtr->nFocusedItem &&
5122 (range.upper - 1) >= infoPtr->nFocusedItem)
5123 LISTVIEW_DrawFocusRect(infoPtr, hdc);
5124 }
5125 iterator_destroy(&i);
5126
5127 enddraw:
5128 /* For LVS_EX_GRIDLINES go and draw lines */
5129 /* This includes the case where there were *no* items */
5130 if ((infoPtr->uView == LV_VIEW_DETAILS) && infoPtr->dwLvExStyle & LVS_EX_GRIDLINES)
5131 LISTVIEW_RefreshReportGrid(infoPtr, hdc);
5132
5133 /* Draw marquee rectangle if appropriate */
5134 if (infoPtr->bMarqueeSelect)
5135 {
5136 SetBkColor(hdc, RGB(255, 255, 255));
5137 SetTextColor(hdc, RGB(0, 0, 0));
5138 DrawFocusRect(hdc, &infoPtr->marqueeDrawRect);
5139 }
5140
5141 if (cdmode & CDRF_NOTIFYPOSTPAINT)
5142 notify_postpaint(infoPtr, &nmlvcd);
5143
5144 if(hbmp) {
5145 BitBlt(hdcOrig, infoPtr->rcList.left, infoPtr->rcList.top,
5146 infoPtr->rcList.right - infoPtr->rcList.left,
5147 infoPtr->rcList.bottom - infoPtr->rcList.top,
5148 hdc, infoPtr->rcList.left, infoPtr->rcList.top, SRCCOPY);
5149
5150 DeleteObject(hbmp);
5151 DeleteDC(hdc);
5152 } else {
5153 SelectObject(hdc, hOldFont);
5154 SetBkMode(hdc, oldBkMode);
5155 SetBkColor(hdc, oldBkColor);
5156 SetTextColor(hdc, oldTextColor);
5157 }
5158
5159 infoPtr->bIsDrawing = FALSE;
5160 }
5161
5162
5163 /***
5164 * DESCRIPTION:
5165 * Calculates the approximate width and height of a given number of items.
5166 *
5167 * PARAMETER(S):
5168 * [I] infoPtr : valid pointer to the listview structure
5169 * [I] nItemCount : number of items
5170 * [I] wWidth : width
5171 * [I] wHeight : height
5172 *
5173 * RETURN:
5174 * Returns a DWORD. The width in the low word and the height in high word.
5175 */
5176 static DWORD LISTVIEW_ApproximateViewRect(const LISTVIEW_INFO *infoPtr, INT nItemCount,
5177 WORD wWidth, WORD wHeight)
5178 {
5179 DWORD dwViewRect = 0;
5180
5181 if (nItemCount == -1)
5182 nItemCount = infoPtr->nItemCount;
5183
5184 if (infoPtr->uView == LV_VIEW_LIST)
5185 {
5186 INT nItemCountPerColumn = 1;
5187 INT nColumnCount = 0;
5188
5189 if (wHeight == 0xFFFF)
5190 {
5191 /* use current height */
5192 wHeight = infoPtr->rcList.bottom - infoPtr->rcList.top;
5193 }
5194
5195 if (wHeight < infoPtr->nItemHeight)
5196 wHeight = infoPtr->nItemHeight;
5197
5198 if (nItemCount > 0)
5199 {
5200 if (infoPtr->nItemHeight > 0)
5201 {
5202 nItemCountPerColumn = wHeight / infoPtr->nItemHeight;
5203 if (nItemCountPerColumn == 0)
5204 nItemCountPerColumn = 1;
5205
5206 if (nItemCount % nItemCountPerColumn != 0)
5207 nColumnCount = nItemCount / nItemCountPerColumn;
5208 else
5209 nColumnCount = nItemCount / nItemCountPerColumn + 1;
5210 }
5211 }
5212
5213 /* Microsoft padding magic */
5214 wHeight = nItemCountPerColumn * infoPtr->nItemHeight + 2;
5215 wWidth = nColumnCount * infoPtr->nItemWidth + 2;
5216
5217 dwViewRect = MAKELONG(wWidth, wHeight);
5218 }
5219 else if (infoPtr->uView == LV_VIEW_DETAILS)
5220 {
5221 RECT rcBox;
5222
5223 if (infoPtr->nItemCount > 0)
5224 {
5225 LISTVIEW_GetItemBox(infoPtr, 0, &rcBox);
5226 wWidth = rcBox.right - rcBox.left;
5227 wHeight = (rcBox.bottom - rcBox.top) * nItemCount;
5228 }
5229 else
5230 {
5231 /* use current height and width */
5232 if (wHeight == 0xffff)
5233 wHeight = infoPtr->rcList.bottom - infoPtr->rcList.top;
5234 if (wWidth == 0xffff)
5235 wWidth = infoPtr->rcList.right - infoPtr->rcList.left;
5236 }
5237
5238 dwViewRect = MAKELONG(wWidth, wHeight);
5239 }
5240 else if (infoPtr->uView == LV_VIEW_ICON)
5241 {
5242 UINT rows,cols;
5243 UINT nItemWidth;
5244 UINT nItemHeight;
5245
5246 nItemWidth = infoPtr->iconSpacing.cx;
5247 nItemHeight = infoPtr->iconSpacing.cy;
5248
5249 if (wWidth == 0xffff)
5250 wWidth = infoPtr->rcList.right - infoPtr->rcList.left;
5251
5252 if (wWidth < nItemWidth)
5253 wWidth = nItemWidth;
5254
5255 cols = wWidth / nItemWidth;
5256 if (cols > nItemCount)
5257 cols = nItemCount;
5258 if (cols < 1)
5259 cols = 1;
5260
5261 if (nItemCount)
5262 {
5263 rows = nItemCount / cols;
5264 if (nItemCount % cols)
5265 rows++;
5266 }
5267 else
5268 rows = 0;
5269
5270 wHeight = (nItemHeight * rows)+2;
5271 wWidth = (nItemWidth * cols)+2;
5272
5273 dwViewRect = MAKELONG(wWidth, wHeight);
5274 }
5275 else if (infoPtr->uView == LV_VIEW_SMALLICON)
5276 FIXME("uView == LV_VIEW_SMALLICON: not implemented\n");
5277
5278 return dwViewRect;
5279 }
5280
5281 /***
5282 * DESCRIPTION:
5283 * Cancel edit label with saving item text.
5284 *
5285 * PARAMETER(S):
5286 * [I] infoPtr : valid pointer to the listview structure
5287 *
5288 * RETURN:
5289 * Always returns TRUE.
5290 */
5291 static LRESULT LISTVIEW_CancelEditLabel(LISTVIEW_INFO *infoPtr)
5292 {
5293 if (infoPtr->hwndEdit)
5294 {
5295 /* handle value will be lost after LISTVIEW_EndEditLabelT */
5296 HWND edit = infoPtr->hwndEdit;
5297
5298 LISTVIEW_EndEditLabelT(infoPtr, TRUE, IsWindowUnicode(infoPtr->hwndEdit));
5299 SendMessageW(edit, WM_CLOSE, 0, 0);
5300 }
5301
5302 return TRUE;
5303 }
5304
5305 /***
5306 * DESCRIPTION:
5307 * Create a drag image list for the specified item.
5308 *
5309 * PARAMETER(S):
5310 * [I] infoPtr : valid pointer to the listview structure
5311 * [I] iItem : index of item
5312 * [O] lppt : Upper-left corner of the image
5313 *
5314 * RETURN:
5315 * Returns a handle to the image list if successful, NULL otherwise.
5316 */
5317 static HIMAGELIST LISTVIEW_CreateDragImage(LISTVIEW_INFO *infoPtr, INT iItem, LPPOINT lppt)
5318 {
5319 RECT rcItem;
5320 SIZE size;
5321 POINT pos;
5322 HDC hdc, hdcOrig;
5323 HBITMAP hbmp, hOldbmp;
5324 HFONT hOldFont;
5325 HIMAGELIST dragList = 0;
5326 TRACE("iItem=%d Count=%d\n", iItem, infoPtr->nItemCount);
5327
5328 if (iItem < 0 || iItem >= infoPtr->nItemCount || !lppt)
5329 return 0;
5330
5331 rcItem.left = LVIR_BOUNDS;
5332 if (!LISTVIEW_GetItemRect(infoPtr, iItem, &rcItem))
5333 return 0;
5334
5335 lppt->x = rcItem.left;
5336 lppt->y = rcItem.top;
5337
5338 size.cx = rcItem.right - rcItem.left;
5339 size.cy = rcItem.bottom - rcItem.top;
5340
5341 hdcOrig = GetDC(infoPtr->hwndSelf);
5342 hdc = CreateCompatibleDC(hdcOrig);
5343 hbmp = CreateCompatibleBitmap(hdcOrig, size.cx, size.cy);
5344 hOldbmp = SelectObject(hdc, hbmp);
5345 hOldFont = SelectObject(hdc, infoPtr->hFont);
5346
5347 rcItem.left = rcItem.top = 0;
5348 rcItem.right = size.cx;
5349 rcItem.bottom = size.cy;
5350 FillRect(hdc, &rcItem, infoPtr->hBkBrush);
5351
5352 pos.x = pos.y = 0;
5353 if (LISTVIEW_DrawItem(infoPtr, hdc, iItem, 0, pos, infoPtr->cditemmode))
5354 {
5355 dragList = ImageList_Create(size.cx, size.cy, ILC_COLOR, 10, 10);
5356 SelectObject(hdc, hOldbmp);
5357 ImageList_Add(dragList, hbmp, 0);
5358 }
5359 else
5360 SelectObject(hdc, hOldbmp);
5361
5362 SelectObject(hdc, hOldFont);
5363 DeleteObject(hbmp);
5364 DeleteDC(hdc);
5365 ReleaseDC(infoPtr->hwndSelf, hdcOrig);
5366
5367 TRACE("ret=%p\n", dragList);
5368
5369 return dragList;
5370 }
5371
5372
5373 /***
5374 * DESCRIPTION:
5375 * Removes all listview items and subitems.
5376 *
5377 * PARAMETER(S):
5378 * [I] infoPtr : valid pointer to the listview structure
5379 *
5380 * RETURN:
5381 * SUCCESS : TRUE
5382 * FAILURE : FALSE
5383 */
5384 static BOOL LISTVIEW_DeleteAllItems(LISTVIEW_INFO *infoPtr, BOOL destroy)
5385 {
5386 NMLISTVIEW nmlv;
5387 HDPA hdpaSubItems = NULL;
5388 BOOL bSuppress;
5389 ITEMHDR *hdrItem;
5390 ITEM_INFO *lpItem;
5391 ITEM_ID *lpID;
5392 INT i, j;
5393
5394 TRACE("()\n");
5395
5396 /* we do it directly, to avoid notifications */
5397 ranges_clear(infoPtr->selectionRanges);
5398 infoPtr->nSelectionMark = -1;
5399 infoPtr->nFocusedItem = -1;
5400 SetRectEmpty(&infoPtr->rcFocus);
5401 /* But we are supposed to leave nHotItem as is! */
5402
5403
5404 /* send LVN_DELETEALLITEMS notification */
5405 ZeroMemory(&nmlv, sizeof(NMLISTVIEW));
5406 nmlv.iItem = -1;
5407 bSuppress = notify_listview(infoPtr, LVN_DELETEALLITEMS, &nmlv);
5408
5409 for (i = infoPtr->nItemCount - 1; i >= 0; i--)
5410 {
5411 if (!(infoPtr->dwStyle & LVS_OWNERDATA))
5412 {
5413 /* send LVN_DELETEITEM notification, if not suppressed
5414 and if it is not a virtual listview */
5415 if (!bSuppress) notify_deleteitem(infoPtr, i);
5416 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, i);
5417 lpItem = DPA_GetPtr(hdpaSubItems, 0);
5418 /* free id struct */
5419 j = DPA_GetPtrIndex(infoPtr->hdpaItemIds, lpItem->id);
5420 lpID = DPA_GetPtr(infoPtr->hdpaItemIds, j);
5421 DPA_DeletePtr(infoPtr->hdpaItemIds, j);
5422 Free(lpID);
5423 /* both item and subitem start with ITEMHDR header */
5424 for (j = 0; j < DPA_GetPtrCount(hdpaSubItems); j++)
5425 {
5426 hdrItem = DPA_GetPtr(hdpaSubItems, j);
5427 if (is_text(hdrItem->pszText)) Free(hdrItem->pszText);
5428 Free(hdrItem);
5429 }
5430 DPA_Destroy(hdpaSubItems);
5431 DPA_DeletePtr(infoPtr->hdpaItems, i);
5432 }
5433 DPA_DeletePtr(infoPtr->hdpaPosX, i);
5434 DPA_DeletePtr(infoPtr->hdpaPosY, i);
5435 infoPtr->nItemCount --;
5436 }
5437
5438 if (!destroy)
5439 {
5440 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
5441 LISTVIEW_UpdateScroll(infoPtr);
5442 }
5443 LISTVIEW_InvalidateList(infoPtr);
5444
5445 return TRUE;
5446 }
5447
5448 /***
5449 * DESCRIPTION:
5450 * Scrolls, and updates the columns, when a column is changing width.
5451 *
5452 * PARAMETER(S):
5453 * [I] infoPtr : valid pointer to the listview structure
5454 * [I] nColumn : column to scroll
5455 * [I] dx : amount of scroll, in pixels
5456 *
5457 * RETURN:
5458 * None.
5459 */
5460 static void LISTVIEW_ScrollColumns(LISTVIEW_INFO *infoPtr, INT nColumn, INT dx)
5461 {
5462 COLUMN_INFO *lpColumnInfo;
5463 RECT rcOld, rcCol;
5464 POINT ptOrigin;
5465 INT nCol;
5466 HDITEMW hdi;
5467
5468 if (nColumn < 0 || DPA_GetPtrCount(infoPtr->hdpaColumns) < 1) return;
5469 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, min(nColumn, DPA_GetPtrCount(infoPtr->hdpaColumns) - 1));
5470 rcCol = lpColumnInfo->rcHeader;
5471 if (nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns))
5472 rcCol.left = rcCol.right;
5473
5474 /* adjust the other columns */
5475 hdi.mask = HDI_ORDER;
5476 if (SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, nColumn, (LPARAM)&hdi))
5477 {
5478 INT nOrder = hdi.iOrder;
5479 for (nCol = 0; nCol < DPA_GetPtrCount(infoPtr->hdpaColumns); nCol++)
5480 {
5481 hdi.mask = HDI_ORDER;
5482 SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, nCol, (LPARAM)&hdi);
5483 if (hdi.iOrder >= nOrder) {
5484 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, nCol);
5485 lpColumnInfo->rcHeader.left += dx;
5486 lpColumnInfo->rcHeader.right += dx;
5487 }
5488 }
5489 }
5490
5491 /* do not update screen if not in report mode */
5492 if (!is_redrawing(infoPtr) || infoPtr->uView != LV_VIEW_DETAILS) return;
5493
5494 /* Need to reset the item width when inserting a new column */
5495 infoPtr->nItemWidth += dx;
5496
5497 LISTVIEW_UpdateScroll(infoPtr);
5498 LISTVIEW_GetOrigin(infoPtr, &ptOrigin);
5499
5500 /* scroll to cover the deleted column, and invalidate for redraw */
5501 rcOld = infoPtr->rcList;
5502 rcOld.left = ptOrigin.x + rcCol.left + dx;
5503 ScrollWindowEx(infoPtr->hwndSelf, dx, 0, &rcOld, &rcOld, 0, 0, SW_ERASE | SW_INVALIDATE);
5504 }
5505
5506 /***
5507 * DESCRIPTION:
5508 * Removes a column from the listview control.
5509 *
5510 * PARAMETER(S):
5511 * [I] infoPtr : valid pointer to the listview structure
5512 * [I] nColumn : column index
5513 *
5514 * RETURN:
5515 * SUCCESS : TRUE
5516 * FAILURE : FALSE
5517 */
5518 static BOOL LISTVIEW_DeleteColumn(LISTVIEW_INFO *infoPtr, INT nColumn)
5519 {
5520 RECT rcCol;
5521
5522 TRACE("nColumn=%d\n", nColumn);
5523
5524 if (nColumn < 0 || DPA_GetPtrCount(infoPtr->hdpaColumns) == 0
5525 || nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return FALSE;
5526
5527 /* While the MSDN specifically says that column zero should not be deleted,
5528 what actually happens is that the column itself is deleted but no items or subitems
5529 are removed.
5530 */
5531
5532 LISTVIEW_GetHeaderRect(infoPtr, nColumn, &rcCol);
5533
5534 if (!SendMessageW(infoPtr->hwndHeader, HDM_DELETEITEM, nColumn, 0))
5535 return FALSE;
5536
5537 Free(DPA_GetPtr(infoPtr->hdpaColumns, nColumn));
5538 DPA_DeletePtr(infoPtr->hdpaColumns, nColumn);
5539
5540 if (!(infoPtr->dwStyle & LVS_OWNERDATA) && nColumn)
5541 {
5542 SUBITEM_INFO *lpSubItem, *lpDelItem;
5543 HDPA hdpaSubItems;
5544 INT nItem, nSubItem, i;
5545
5546 for (nItem = 0; nItem < infoPtr->nItemCount; nItem++)
5547 {
5548 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, nItem);
5549 nSubItem = 0;
5550 lpDelItem = 0;
5551 for (i = 1; i < DPA_GetPtrCount(hdpaSubItems); i++)
5552 {
5553 lpSubItem = DPA_GetPtr(hdpaSubItems, i);
5554 if (lpSubItem->iSubItem == nColumn)
5555 {
5556 nSubItem = i;
5557 lpDelItem = lpSubItem;
5558 }
5559 else if (lpSubItem->iSubItem > nColumn)
5560 {
5561 lpSubItem->iSubItem--;
5562 }
5563 }
5564
5565 /* if we found our subitem, zap it */
5566 if (nSubItem > 0)
5567 {
5568 /* free string */
5569 if (is_text(lpDelItem->hdr.pszText))
5570 Free(lpDelItem->hdr.pszText);
5571
5572 /* free item */
5573 Free(lpDelItem);
5574
5575 /* free dpa memory */
5576 DPA_DeletePtr(hdpaSubItems, nSubItem);
5577 }
5578 }
5579 }
5580
5581 /* update the other column info */
5582 LISTVIEW_UpdateItemSize(infoPtr);
5583 if(DPA_GetPtrCount(infoPtr->hdpaColumns) == 0)
5584 LISTVIEW_InvalidateList(infoPtr);
5585 else
5586 LISTVIEW_ScrollColumns(infoPtr, nColumn, -(rcCol.right - rcCol.left));
5587
5588 return TRUE;
5589 }
5590
5591 /***
5592 * DESCRIPTION:
5593 * Invalidates the listview after an item's insertion or deletion.
5594 *
5595 * PARAMETER(S):
5596 * [I] infoPtr : valid pointer to the listview structure
5597 * [I] nItem : item index
5598 * [I] dir : -1 if deleting, 1 if inserting
5599 *
5600 * RETURN:
5601 * None
5602 */
5603 static void LISTVIEW_ScrollOnInsert(LISTVIEW_INFO *infoPtr, INT nItem, INT dir)
5604 {
5605 INT nPerCol, nItemCol, nItemRow;
5606 RECT rcScroll;
5607 POINT Origin;
5608
5609 /* if we don't refresh, what's the point of scrolling? */
5610 if (!is_redrawing(infoPtr)) return;
5611
5612 assert (abs(dir) == 1);
5613
5614 /* arrange icons if autoarrange is on */
5615 if (is_autoarrange(infoPtr))
5616 {
5617 BOOL arrange = TRUE;
5618 if (dir < 0 && nItem >= infoPtr->nItemCount) arrange = FALSE;
5619 if (dir > 0 && nItem == infoPtr->nItemCount - 1) arrange = FALSE;
5620 if (arrange) LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
5621 }
5622
5623 /* scrollbars need updating */
5624 LISTVIEW_UpdateScroll(infoPtr);
5625
5626 /* figure out the item's position */
5627 if (infoPtr->uView == LV_VIEW_DETAILS)
5628 nPerCol = infoPtr->nItemCount + 1;
5629 else if (infoPtr->uView == LV_VIEW_LIST)
5630 nPerCol = LISTVIEW_GetCountPerColumn(infoPtr);
5631 else /* LV_VIEW_ICON, or LV_VIEW_SMALLICON */
5632 return;
5633
5634 nItemCol = nItem / nPerCol;
5635 nItemRow = nItem % nPerCol;
5636 LISTVIEW_GetOrigin(infoPtr, &Origin);
5637
5638 /* move the items below up a slot */
5639 rcScroll.left = nItemCol * infoPtr->nItemWidth;
5640 rcScroll.top = nItemRow * infoPtr->nItemHeight;
5641 rcScroll.right = rcScroll.left + infoPtr->nItemWidth;
5642 rcScroll.bottom = nPerCol * infoPtr->nItemHeight;
5643 OffsetRect(&rcScroll, Origin.x, Origin.y);
5644 TRACE("rcScroll=%s, dx=%d\n", wine_dbgstr_rect(&rcScroll), dir * infoPtr->nItemHeight);
5645 if (IntersectRect(&rcScroll, &rcScroll, &infoPtr->rcList))
5646 {
5647 TRACE("Invalidating rcScroll=%s, rcList=%s\n", wine_dbgstr_rect(&rcScroll), wine_dbgstr_rect(&infoPtr->rcList));
5648 InvalidateRect(infoPtr->hwndSelf, &rcScroll, TRUE);
5649 }
5650
5651 /* report has only that column, so we're done */
5652 if (infoPtr->uView == LV_VIEW_DETAILS) return;
5653
5654 /* now for LISTs, we have to deal with the columns to the right */
5655 rcScroll.left = (nItemCol + 1) * infoPtr->nItemWidth;
5656 rcScroll.top = 0;
5657 rcScroll.right = (infoPtr->nItemCount / nPerCol + 1) * infoPtr->nItemWidth;
5658 rcScroll.bottom = nPerCol * infoPtr->nItemHeight;
5659 OffsetRect(&rcScroll, Origin.x, Origin.y);
5660 if (IntersectRect(&rcScroll, &rcScroll, &infoPtr->rcList))
5661 InvalidateRect(infoPtr->hwndSelf, &rcScroll, TRUE);
5662 }
5663
5664 /***
5665 * DESCRIPTION:
5666 * Removes an item from the listview control.
5667 *
5668 * PARAMETER(S):
5669 * [I] infoPtr : valid pointer to the listview structure
5670 * [I] nItem : item index
5671 *
5672 * RETURN:
5673 * SUCCESS : TRUE
5674 * FAILURE : FALSE
5675 */
5676 static BOOL LISTVIEW_DeleteItem(LISTVIEW_INFO *infoPtr, INT nItem)
5677 {
5678 LVITEMW item;
5679 const BOOL is_icon = (infoPtr->uView == LV_VIEW_SMALLICON || infoPtr->uView == LV_VIEW_ICON);
5680 INT focus = infoPtr->nFocusedItem;
5681
5682 TRACE("(nItem=%d)\n", nItem);
5683
5684 if (nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
5685
5686 /* remove selection, and focus */
5687 item.state = 0;
5688 item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
5689 LISTVIEW_SetItemState(infoPtr, nItem, &item);
5690
5691 /* send LVN_DELETEITEM notification. */
5692 if (!notify_deleteitem(infoPtr, nItem)) return FALSE;
5693
5694 /* we need to do this here, because we'll be deleting stuff */
5695 if (is_icon)
5696 LISTVIEW_InvalidateItem(infoPtr, nItem);
5697
5698 if (!(infoPtr->dwStyle & LVS_OWNERDATA))
5699 {
5700 HDPA hdpaSubItems;
5701 ITEMHDR *hdrItem;
5702 ITEM_INFO *lpItem;
5703 ITEM_ID *lpID;
5704 INT i;
5705
5706 hdpaSubItems = DPA_DeletePtr(infoPtr->hdpaItems, nItem);
5707 lpItem = DPA_GetPtr(hdpaSubItems, 0);
5708
5709 /* free id struct */
5710 i = DPA_GetPtrIndex(infoPtr->hdpaItemIds, lpItem->id);
5711 lpID = DPA_GetPtr(infoPtr->hdpaItemIds, i);
5712 DPA_DeletePtr(infoPtr->hdpaItemIds, i);
5713 Free(lpID);
5714 for (i = 0; i < DPA_GetPtrCount(hdpaSubItems); i++)
5715 {
5716 hdrItem = DPA_GetPtr(hdpaSubItems, i);
5717 if (is_text(hdrItem->pszText)) Free(hdrItem->pszText);
5718 Free(hdrItem);
5719 }
5720 DPA_Destroy(hdpaSubItems);
5721 }
5722
5723 if (is_icon)
5724 {
5725 DPA_DeletePtr(infoPtr->hdpaPosX, nItem);
5726 DPA_DeletePtr(infoPtr->hdpaPosY, nItem);
5727 }
5728
5729 infoPtr->nItemCount--;
5730 LISTVIEW_ShiftIndices(infoPtr, nItem, -1);
5731 LISTVIEW_ShiftFocus(infoPtr, focus, nItem, -1);
5732
5733 /* now is the invalidation fun */
5734 if (!is_icon)
5735 LISTVIEW_ScrollOnInsert(infoPtr, nItem, -1);
5736 return TRUE;
5737 }
5738
5739
5740 /***
5741 * DESCRIPTION:
5742 * Callback implementation for editlabel control
5743 *
5744 * PARAMETER(S):
5745 * [I] infoPtr : valid pointer to the listview structure
5746 * [I] storeText : store edit box text as item text
5747 * [I] isW : TRUE if psxText is Unicode, FALSE if it's ANSI
5748 *
5749 * RETURN:
5750 * SUCCESS : TRUE
5751 * FAILURE : FALSE
5752 */
5753 static BOOL LISTVIEW_EndEditLabelT(LISTVIEW_INFO *infoPtr, BOOL storeText, BOOL isW)
5754 {
5755 HWND hwndSelf = infoPtr->hwndSelf;
5756 WCHAR szDispText[DISP_TEXT_SIZE] = { 0 };
5757 NMLVDISPINFOW dispInfo;
5758 INT editedItem = infoPtr->nEditLabelItem;
5759 BOOL same;
5760 WCHAR *pszText = NULL;
5761 BOOL res;
5762
5763 if (storeText)
5764 {
5765 DWORD len = isW ? GetWindowTextLengthW(infoPtr->hwndEdit) : GetWindowTextLengthA(infoPtr->hwndEdit);
5766
5767 if (len)
5768 {
5769 if ((pszText = Alloc((len+1) * (isW ? sizeof(WCHAR) : sizeof(CHAR)))))
5770 {
5771 if (isW) GetWindowTextW(infoPtr->hwndEdit, pszText, len+1);
5772 else GetWindowTextA(infoPtr->hwndEdit, (CHAR*)pszText, len+1);
5773 }
5774 }
5775 }
5776
5777 TRACE("(pszText=%s, isW=%d)\n", debugtext_t(pszText, isW), isW);
5778
5779 ZeroMemory(&dispInfo, sizeof(dispInfo));
5780 dispInfo.item.mask = LVIF_PARAM | LVIF_STATE | LVIF_TEXT;
5781 dispInfo.item.iItem = editedItem;
5782 dispInfo.item.iSubItem = 0;
5783 dispInfo.item.stateMask = ~0;
5784 dispInfo.item.pszText = szDispText;
5785 dispInfo.item.cchTextMax = DISP_TEXT_SIZE;
5786 if (!LISTVIEW_GetItemT(infoPtr, &dispInfo.item, isW))
5787 {
5788 res = FALSE;
5789 goto cleanup;
5790 }
5791
5792 if (isW)
5793 same = (lstrcmpW(dispInfo.item.pszText, pszText) == 0);
5794 else
5795 {
5796 LPWSTR tmp = textdupTtoW(pszText, FALSE);
5797 same = (lstrcmpW(dispInfo.item.pszText, tmp) == 0);
5798 textfreeT(tmp, FALSE);
5799 }
5800
5801 /* add the text from the edit in */
5802 dispInfo.item.mask |= LVIF_TEXT;
5803 dispInfo.item.pszText = same ? NULL : pszText;
5804 dispInfo.item.cchTextMax = textlenT(dispInfo.item.pszText, isW);
5805
5806 /* Do we need to update the Item Text */
5807 res = notify_dispinfoT(infoPtr, LVN_ENDLABELEDITW, &dispInfo, isW);
5808
5809 infoPtr->nEditLabelItem = -1;
5810 infoPtr->hwndEdit = 0;
5811
5812 if (!res) goto cleanup;
5813
5814 if (!IsWindow(hwndSelf))
5815 {
5816 res = FALSE;
5817 goto cleanup;
5818 }
5819 if (!pszText) return TRUE;
5820 if (same)
5821 {
5822 res = TRUE;
5823 goto cleanup;
5824 }
5825
5826 if (!(infoPtr->dwStyle & LVS_OWNERDATA))
5827 {
5828 HDPA hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, editedItem);
5829 ITEM_INFO* lpItem = DPA_GetPtr(hdpaSubItems, 0);
5830 if (lpItem && lpItem->hdr.pszText == LPSTR_TEXTCALLBACKW)
5831 {
5832 LISTVIEW_InvalidateItem(infoPtr, editedItem);
5833 res = TRUE;
5834 goto cleanup;
5835 }
5836 }
5837
5838 ZeroMemory(&dispInfo, sizeof(dispInfo));
5839 dispInfo.item.mask = LVIF_TEXT;
5840 dispInfo.item.iItem = editedItem;
5841 dispInfo.item.iSubItem = 0;
5842 dispInfo.item.pszText = pszText;
5843 dispInfo.item.cchTextMax = textlenT(pszText, isW);
5844 res = LISTVIEW_SetItemT(infoPtr, &dispInfo.item, isW);
5845
5846 cleanup:
5847 Free(pszText);
5848
5849 return res;
5850 }
5851
5852 /***
5853 * DESCRIPTION:
5854 * Subclassed edit control windproc function
5855 *
5856 * PARAMETER(S):
5857 * [I] hwnd : the edit window handle
5858 * [I] uMsg : the message that is to be processed
5859 * [I] wParam : first message parameter
5860 * [I] lParam : second message parameter
5861 * [I] isW : TRUE if input is Unicode
5862 *
5863 * RETURN:
5864 * Zero.
5865 */
5866 static LRESULT EditLblWndProcT(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL isW)
5867 {
5868 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)GetWindowLongPtrW(GetParent(hwnd), 0);
5869 BOOL save = TRUE;
5870
5871 TRACE("(hwnd=%p, uMsg=%x, wParam=%lx, lParam=%lx, isW=%d)\n",
5872 hwnd, uMsg, wParam, lParam, isW);
5873
5874 switch (uMsg)
5875 {
5876 case WM_GETDLGCODE:
5877 return DLGC_WANTARROWS | DLGC_WANTALLKEYS;
5878
5879 case WM_DESTROY:
5880 {
5881 WNDPROC editProc = infoPtr->EditWndProc;
5882 infoPtr->EditWndProc = 0;
5883 SetWindowLongPtrW(hwnd, GWLP_WNDPROC, (DWORD_PTR)editProc);
5884 return CallWindowProcT(editProc, hwnd, uMsg, wParam, lParam, isW);
5885 }
5886
5887 case WM_KEYDOWN:
5888 if (VK_ESCAPE == (INT)wParam)
5889 {
5890 save = FALSE;
5891 break;
5892 }
5893 else if (VK_RETURN == (INT)wParam)
5894 break;
5895
5896 default:
5897 return CallWindowProcT(infoPtr->EditWndProc, hwnd, uMsg, wParam, lParam, isW);
5898 }
5899
5900 /* kill the edit */
5901 if (infoPtr->hwndEdit)
5902 LISTVIEW_EndEditLabelT(infoPtr, save, isW);
5903
5904 SendMessageW(hwnd, WM_CLOSE, 0, 0);
5905 return 0;
5906 }
5907
5908 /***
5909 * DESCRIPTION:
5910 * Subclassed edit control Unicode windproc function
5911 *
5912 * PARAMETER(S):
5913 * [I] hwnd : the edit window handle
5914 * [I] uMsg : the message that is to be processed
5915 * [I] wParam : first message parameter
5916 * [I] lParam : second message parameter
5917 *
5918 * RETURN:
5919 */
5920 static LRESULT CALLBACK EditLblWndProcW(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
5921 {
5922 return EditLblWndProcT(hwnd, uMsg, wParam, lParam, TRUE);
5923 }
5924
5925 /***
5926 * DESCRIPTION:
5927 * Subclassed edit control ANSI windproc function
5928 *
5929 * PARAMETER(S):
5930 * [I] hwnd : the edit window handle
5931 * [I] uMsg : the message that is to be processed
5932 * [I] wParam : first message parameter
5933 * [I] lParam : second message parameter
5934 *
5935 * RETURN:
5936 */
5937 static LRESULT CALLBACK EditLblWndProcA(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
5938 {
5939 return EditLblWndProcT(hwnd, uMsg, wParam, lParam, FALSE);
5940 }
5941
5942 /***
5943 * DESCRIPTION:
5944 * Creates a subclassed edit control
5945 *
5946 * PARAMETER(S):
5947 * [I] infoPtr : valid pointer to the listview structure
5948 * [I] text : initial text for the edit
5949 * [I] style : the window style
5950 * [I] isW : TRUE if input is Unicode
5951 *
5952 * RETURN:
5953 */
5954 static HWND CreateEditLabelT(LISTVIEW_INFO *infoPtr, LPCWSTR text, BOOL isW)
5955 {
5956 static const DWORD style = WS_CHILDWINDOW|WS_CLIPSIBLINGS|ES_LEFT|ES_AUTOHSCROLL|WS_BORDER|WS_VISIBLE;
5957 HINSTANCE hinst = (HINSTANCE)GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_HINSTANCE);
5958 HWND hedit;
5959
5960 TRACE("(%p, text=%s, isW=%d)\n", infoPtr, debugtext_t(text, isW), isW);
5961
5962 /* window will be resized and positioned after LVN_BEGINLABELEDIT */
5963 if (isW)
5964 hedit = CreateWindowW(WC_EDITW, text, style, 0, 0, 0, 0, infoPtr->hwndSelf, 0, hinst, 0);
5965 else
5966 hedit = CreateWindowA(WC_EDITA, (LPCSTR)text, style, 0, 0, 0, 0, infoPtr->hwndSelf, 0, hinst, 0);
5967
5968 if (!hedit) return 0;
5969
5970 infoPtr->EditWndProc = (WNDPROC)
5971 (isW ? SetWindowLongPtrW(hedit, GWLP_WNDPROC, (DWORD_PTR)EditLblWndProcW) :
5972 SetWindowLongPtrA(hedit, GWLP_WNDPROC, (DWORD_PTR)EditLblWndProcA) );
5973
5974 SendMessageW(hedit, WM_SETFONT, (WPARAM)infoPtr->hFont, FALSE);
5975 SendMessageW(hedit, EM_SETLIMITTEXT, DISP_TEXT_SIZE-1, 0);
5976
5977 return hedit;
5978 }
5979
5980 /***
5981 * DESCRIPTION:
5982 * Begin in place editing of specified list view item
5983 *
5984 * PARAMETER(S):
5985 * [I] infoPtr : valid pointer to the listview structure
5986 * [I] nItem : item index
5987 * [I] isW : TRUE if it's a Unicode req, FALSE if ASCII
5988 *
5989 * RETURN:
5990 * SUCCESS : TRUE
5991 * FAILURE : FALSE
5992 */
5993 static HWND LISTVIEW_EditLabelT(LISTVIEW_INFO *infoPtr, INT nItem, BOOL isW)
5994 {
5995 WCHAR disptextW[DISP_TEXT_SIZE] = { 0 };
5996 HWND hwndSelf = infoPtr->hwndSelf;
5997 NMLVDISPINFOW dispInfo;
5998 HFONT hOldFont = NULL;
5999 TEXTMETRICW tm;
6000 RECT rect;
6001 SIZE sz;
6002 HDC hdc;
6003
6004 TRACE("(nItem=%d, isW=%d)\n", nItem, isW);
6005
6006 if (~infoPtr->dwStyle & LVS_EDITLABELS) return 0;
6007
6008 /* remove existing edit box */
6009 if (infoPtr->hwndEdit)
6010 {
6011 SetFocus(infoPtr->hwndSelf);
6012 infoPtr->hwndEdit = 0;
6013 }
6014
6015 if (nItem < 0 || nItem >= infoPtr->nItemCount) return 0;
6016
6017 infoPtr->nEditLabelItem = nItem;
6018
6019 LISTVIEW_SetSelection(infoPtr, nItem);
6020 LISTVIEW_SetItemFocus(infoPtr, nItem);
6021 LISTVIEW_InvalidateItem(infoPtr, nItem);
6022
6023 rect.left = LVIR_LABEL;
6024 if (!LISTVIEW_GetItemRect(infoPtr, nItem, &rect)) return 0;
6025
6026 ZeroMemory(&dispInfo, sizeof(dispInfo));
6027 dispInfo.item.mask = LVIF_PARAM | LVIF_STATE | LVIF_TEXT;
6028 dispInfo.item.iItem = nItem;
6029 dispInfo.item.iSubItem = 0;
6030 dispInfo.item.stateMask = ~0;
6031 dispInfo.item.pszText = disptextW;
6032 dispInfo.item.cchTextMax = DISP_TEXT_SIZE;
6033 if (!LISTVIEW_GetItemT(infoPtr, &dispInfo.item, isW)) return 0;
6034
6035 infoPtr->hwndEdit = CreateEditLabelT(infoPtr, dispInfo.item.pszText, isW);
6036 if (!infoPtr->hwndEdit) return 0;
6037
6038 if (notify_dispinfoT(infoPtr, LVN_BEGINLABELEDITW, &dispInfo, isW))
6039 {
6040 if (!IsWindow(hwndSelf))
6041 return 0;
6042 SendMessageW(infoPtr->hwndEdit, WM_CLOSE, 0, 0);
6043 infoPtr->hwndEdit = 0;
6044 return 0;
6045 }
6046
6047 TRACE("disp text=%s\n", debugtext_t(dispInfo.item.pszText, isW));
6048
6049 /* position and display edit box */
6050 hdc = GetDC(infoPtr->hwndSelf);
6051
6052 /* select the font to get appropriate metric dimensions */
6053 if (infoPtr->hFont)
6054 hOldFont = SelectObject(hdc, infoPtr->hFont);
6055
6056 /* use real edit box content, it could be altered during LVN_BEGINLABELEDIT notification */
6057 GetWindowTextW(infoPtr->hwndEdit, disptextW, DISP_TEXT_SIZE);
6058 TRACE("edit box text=%s\n", debugstr_w(disptextW));
6059
6060 /* get string length in pixels */
6061 GetTextExtentPoint32W(hdc, disptextW, lstrlenW(disptextW), &sz);
6062
6063 /* add extra spacing for the next character */
6064 GetTextMetricsW(hdc, &tm);
6065 sz.cx += tm.tmMaxCharWidth * 2;
6066
6067 if (infoPtr->hFont)
6068 SelectObject(hdc, hOldFont);
6069
6070 ReleaseDC(infoPtr->hwndSelf, hdc);
6071
6072 sz.cy = rect.bottom - rect.top + 2;
6073 rect.left -= 2;
6074 rect.top -= 1;
6075 TRACE("moving edit=(%d,%d)-(%d,%d)\n", rect.left, rect.top, sz.cx, sz.cy);
6076 MoveWindow(infoPtr->hwndEdit, rect.left, rect.top, sz.cx, sz.cy, FALSE);
6077 ShowWindow(infoPtr->hwndEdit, SW_NORMAL);
6078 SetFocus(infoPtr->hwndEdit);
6079 SendMessageW(infoPtr->hwndEdit, EM_SETSEL, 0, -1);
6080 return infoPtr->hwndEdit;
6081 }
6082
6083
6084 /***
6085 * DESCRIPTION:
6086 * Ensures the specified item is visible, scrolling into view if necessary.
6087 *
6088 * PARAMETER(S):
6089 * [I] infoPtr : valid pointer to the listview structure
6090 * [I] nItem : item index
6091 * [I] bPartial : partially or entirely visible
6092 *
6093 * RETURN:
6094 * SUCCESS : TRUE
6095 * FAILURE : FALSE
6096 */
6097 static BOOL LISTVIEW_EnsureVisible(LISTVIEW_INFO *infoPtr, INT nItem, BOOL bPartial)
6098 {
6099 INT nScrollPosHeight = 0;
6100 INT nScrollPosWidth = 0;
6101 INT nHorzAdjust = 0;
6102 INT nVertAdjust = 0;
6103 INT nHorzDiff = 0;
6104 INT nVertDiff = 0;
6105 RECT rcItem, rcTemp;
6106
6107 rcItem.left = LVIR_BOUNDS;
6108 if (!LISTVIEW_GetItemRect(infoPtr, nItem, &rcItem)) return FALSE;
6109
6110 if (bPartial && IntersectRect(&rcTemp, &infoPtr->rcList, &rcItem)) return TRUE;
6111
6112 if (rcItem.left < infoPtr->rcList.left || rcItem.right > infoPtr->rcList.right)
6113 {
6114 /* scroll left/right, but in LV_VIEW_DETAILS mode */
6115 if (infoPtr->uView == LV_VIEW_LIST)
6116 nScrollPosWidth = infoPtr->nItemWidth;
6117 else if ((infoPtr->uView == LV_VIEW_SMALLICON) || (infoPtr->uView == LV_VIEW_ICON))
6118 nScrollPosWidth = 1;
6119
6120 if (rcItem.left < infoPtr->rcList.left)
6121 {
6122 nHorzAdjust = -1;
6123 if (infoPtr->uView != LV_VIEW_DETAILS) nHorzDiff = rcItem.left - infoPtr->rcList.left;
6124 }
6125 else
6126 {
6127 nHorzAdjust = 1;
6128 if (infoPtr->uView != LV_VIEW_DETAILS) nHorzDiff = rcItem.right - infoPtr->rcList.right;
6129 }
6130 }
6131
6132 if (rcItem.top < infoPtr->rcList.top || rcItem.bottom > infoPtr->rcList.bottom)
6133 {
6134 /* scroll up/down, but not in LVS_LIST mode */
6135 if (infoPtr->uView == LV_VIEW_DETAILS)
6136 nScrollPosHeight = infoPtr->nItemHeight;
6137 else if ((infoPtr->uView == LV_VIEW_ICON) || (infoPtr->uView == LV_VIEW_SMALLICON))
6138 nScrollPosHeight = 1;
6139
6140 if (rcItem.top < infoPtr->rcList.top)
6141 {
6142 nVertAdjust = -1;
6143 if (infoPtr->uView != LV_VIEW_LIST) nVertDiff = rcItem.top - infoPtr->rcList.top;
6144 }
6145 else
6146 {
6147 nVertAdjust = 1;
6148 if (infoPtr->uView != LV_VIEW_LIST) nVertDiff = rcItem.bottom - infoPtr->rcList.bottom;
6149 }
6150 }
6151
6152 if (!nScrollPosWidth && !nScrollPosHeight) return TRUE;
6153
6154 if (nScrollPosWidth)
6155 {
6156 INT diff = nHorzDiff / nScrollPosWidth;
6157 if (nHorzDiff % nScrollPosWidth) diff += nHorzAdjust;
6158 LISTVIEW_HScroll(infoPtr, SB_INTERNAL, diff);
6159 }
6160
6161 if (nScrollPosHeight)
6162 {
6163 INT diff = nVertDiff / nScrollPosHeight;
6164 if (nVertDiff % nScrollPosHeight) diff += nVertAdjust;
6165 LISTVIEW_VScroll(infoPtr, SB_INTERNAL, diff);
6166 }
6167
6168 return TRUE;
6169 }
6170
6171 /***
6172 * DESCRIPTION:
6173 * Searches for an item with specific characteristics.
6174 *
6175 * PARAMETER(S):
6176 * [I] hwnd : window handle
6177 * [I] nStart : base item index
6178 * [I] lpFindInfo : item information to look for
6179 *
6180 * RETURN:
6181 * SUCCESS : index of item
6182 * FAILURE : -1
6183 */
6184 static INT LISTVIEW_FindItemW(const LISTVIEW_INFO *infoPtr, INT nStart,
6185 const LVFINDINFOW *lpFindInfo)
6186 {
6187 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
6188 BOOL bWrap = FALSE, bNearest = FALSE;
6189 INT nItem = nStart + 1, nLast = infoPtr->nItemCount, nNearestItem = -1;
6190 ULONG xdist, ydist, dist, mindist = 0x7fffffff;
6191 POINT Position, Destination;
6192 LVITEMW lvItem;
6193
6194 /* Search in virtual listviews should be done by application, not by
6195 listview control, so we just send LVN_ODFINDITEMW and return the result */
6196 if (infoPtr->dwStyle & LVS_OWNERDATA)
6197 {
6198 NMLVFINDITEMW nmlv;
6199
6200 nmlv.iStart = nStart;
6201 nmlv.lvfi = *lpFindInfo;
6202 return notify_hdr(infoPtr, LVN_ODFINDITEMW, (LPNMHDR)&nmlv.hdr);
6203 }
6204
6205 if (!lpFindInfo || nItem < 0) return -1;
6206
6207 lvItem.mask = 0;
6208 if (lpFindInfo->flags & (LVFI_STRING | LVFI_PARTIAL) ||
6209 lpFindInfo->flags & LVFI_SUBSTRING)
6210 {
6211 lvItem.mask |= LVIF_TEXT;
6212 lvItem.pszText = szDispText;
6213 lvItem.cchTextMax = DISP_TEXT_SIZE;
6214 }
6215
6216 if (lpFindInfo->flags & LVFI_WRAP)
6217 bWrap = TRUE;
6218
6219 if ((lpFindInfo->flags & LVFI_NEARESTXY) &&
6220 (infoPtr->uView == LV_VIEW_ICON || infoPtr->uView == LV_VIEW_SMALLICON))
6221 {
6222 POINT Origin;
6223 RECT rcArea;
6224
6225 LISTVIEW_GetOrigin(infoPtr, &Origin);
6226 Destination.x = lpFindInfo->pt.x - Origin.x;
6227 Destination.y = lpFindInfo->pt.y - Origin.y;
6228 switch(lpFindInfo->vkDirection)
6229 {
6230 case VK_DOWN: Destination.y += infoPtr->nItemHeight; break;
6231 case VK_UP: Destination.y -= infoPtr->nItemHeight; break;
6232 case VK_RIGHT: Destination.x += infoPtr->nItemWidth; break;
6233 case VK_LEFT: Destination.x -= infoPtr->nItemWidth; break;
6234 case VK_HOME: Destination.x = Destination.y = 0; break;
6235 case VK_NEXT: Destination.y += infoPtr->rcList.bottom - infoPtr->rcList.top; break;
6236 case VK_PRIOR: Destination.y -= infoPtr->rcList.bottom - infoPtr->rcList.top; break;
6237 case VK_END:
6238 LISTVIEW_GetAreaRect(infoPtr, &rcArea);
6239 Destination.x = rcArea.right;
6240 Destination.y = rcArea.bottom;
6241 break;
6242 default: ERR("Unknown vkDirection=%d\n", lpFindInfo->vkDirection);
6243 }
6244 bNearest = TRUE;
6245 }
6246 else Destination.x = Destination.y = 0;
6247
6248 /* if LVFI_PARAM is specified, all other flags are ignored */
6249 if (lpFindInfo->flags & LVFI_PARAM)
6250 {
6251 lvItem.mask |= LVIF_PARAM;
6252 bNearest = FALSE;
6253 lvItem.mask &= ~LVIF_TEXT;
6254 }
6255
6256 again:
6257 for (; nItem < nLast; nItem++)
6258 {
6259 lvItem.iItem = nItem;
6260 lvItem.iSubItem = 0;
6261 lvItem.pszText = szDispText;
6262 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) continue;
6263
6264 if (lvItem.mask & LVIF_PARAM)
6265 {
6266 if (lpFindInfo->lParam == lvItem.lParam)
6267 return nItem;
6268 else
6269 continue;
6270 }
6271
6272 if (lvItem.mask & LVIF_TEXT)
6273 {
6274 if (lpFindInfo->flags & (LVFI_PARTIAL | LVFI_SUBSTRING))
6275 {
6276 WCHAR *p = strstrW(lvItem.pszText, lpFindInfo->psz);
6277 if (!p || p != lvItem.pszText) continue;
6278 }
6279 else
6280 {
6281 if (lstrcmpW(lvItem.pszText, lpFindInfo->psz) != 0) continue;
6282 }
6283 }
6284
6285 if (!bNearest) return nItem;
6286
6287 /* This is very inefficient. To do a good job here,
6288 * we need a sorted array of (x,y) item positions */
6289 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position);
6290
6291 /* compute the distance^2 to the destination */
6292 xdist = Destination.x - Position.x;
6293 ydist = Destination.y - Position.y;
6294 dist = xdist * xdist + ydist * ydist;
6295
6296 /* remember the distance, and item if it's closer */
6297 if (dist < mindist)
6298 {
6299 mindist = dist;
6300 nNearestItem = nItem;
6301 }
6302 }
6303
6304 if (bWrap)
6305 {
6306 nItem = 0;
6307 nLast = min(nStart + 1, infoPtr->nItemCount);
6308 bWrap = FALSE;
6309 goto again;
6310 }
6311
6312 return nNearestItem;
6313 }
6314
6315 /***
6316 * DESCRIPTION:
6317 * Searches for an item with specific characteristics.
6318 *
6319 * PARAMETER(S):
6320 * [I] hwnd : window handle
6321 * [I] nStart : base item index
6322 * [I] lpFindInfo : item information to look for
6323 *
6324 * RETURN:
6325 * SUCCESS : index of item
6326 * FAILURE : -1
6327 */
6328 static INT LISTVIEW_FindItemA(const LISTVIEW_INFO *infoPtr, INT nStart,
6329 const LVFINDINFOA *lpFindInfo)
6330 {
6331 BOOL hasText = lpFindInfo->flags & (LVFI_STRING | LVFI_PARTIAL) ||
6332 lpFindInfo->flags & LVFI_SUBSTRING;
6333 LVFINDINFOW fiw;
6334 INT res;
6335 LPWSTR strW = NULL;
6336
6337 memcpy(&fiw, lpFindInfo, sizeof(fiw));
6338 if (hasText) fiw.psz = strW = textdupTtoW((LPCWSTR)lpFindInfo->psz, FALSE);
6339 res = LISTVIEW_FindItemW(infoPtr, nStart, &fiw);
6340 textfreeT(strW, FALSE);
6341 return res;
6342 }
6343
6344 /***
6345 * DESCRIPTION:
6346 * Retrieves column attributes.
6347 *
6348 * PARAMETER(S):
6349 * [I] infoPtr : valid pointer to the listview structure
6350 * [I] nColumn : column index
6351 * [IO] lpColumn : column information
6352 * [I] isW : if TRUE, then lpColumn is a LPLVCOLUMNW
6353 * otherwise it is in fact a LPLVCOLUMNA
6354 *
6355 * RETURN:
6356 * SUCCESS : TRUE
6357 * FAILURE : FALSE
6358 */
6359 static BOOL LISTVIEW_GetColumnT(const LISTVIEW_INFO *infoPtr, INT nColumn, LPLVCOLUMNW lpColumn, BOOL isW)
6360 {
6361 COLUMN_INFO *lpColumnInfo;
6362 HDITEMW hdi;
6363
6364 if (!lpColumn || nColumn < 0 || nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return FALSE;
6365 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, nColumn);
6366
6367 /* initialize memory */
6368 ZeroMemory(&hdi, sizeof(hdi));
6369
6370 if (lpColumn->mask & LVCF_TEXT)
6371 {
6372 hdi.mask |= HDI_TEXT;
6373 hdi.pszText = lpColumn->pszText;
6374 hdi.cchTextMax = lpColumn->cchTextMax;
6375 }
6376
6377 if (lpColumn->mask & LVCF_IMAGE)
6378 hdi.mask |= HDI_IMAGE;
6379
6380 if (lpColumn->mask & LVCF_ORDER)
6381 hdi.mask |= HDI_ORDER;
6382
6383 if (lpColumn->mask & LVCF_SUBITEM)
6384 hdi.mask |= HDI_LPARAM;
6385
6386 if (!SendMessageW(infoPtr->hwndHeader, isW ? HDM_GETITEMW : HDM_GETITEMA, nColumn, (LPARAM)&hdi)) return FALSE;
6387
6388 if (lpColumn->mask & LVCF_FMT)
6389 lpColumn->fmt = lpColumnInfo->fmt;
6390
6391 if (lpColumn->mask & LVCF_WIDTH)
6392 lpColumn->cx = lpColumnInfo->rcHeader.right - lpColumnInfo->rcHeader.left;
6393
6394 if (lpColumn->mask & LVCF_IMAGE)
6395 lpColumn->iImage = hdi.iImage;
6396
6397 if (lpColumn->mask & LVCF_ORDER)
6398 lpColumn->iOrder = hdi.iOrder;
6399
6400 if (lpColumn->mask & LVCF_SUBITEM)
6401 lpColumn->iSubItem = hdi.lParam;
6402
6403 if (lpColumn->mask & LVCF_MINWIDTH)
6404 lpColumn->cxMin = lpColumnInfo->cxMin;
6405
6406 return TRUE;
6407 }
6408
6409
6410 static BOOL LISTVIEW_GetColumnOrderArray(const LISTVIEW_INFO *infoPtr, INT iCount, LPINT lpiArray)
6411 {
6412 TRACE("iCount=%d, lpiArray=%p\n", iCount, lpiArray);
6413
6414 if (!lpiArray)
6415 return FALSE;
6416
6417 return SendMessageW(infoPtr->hwndHeader, HDM_GETORDERARRAY, iCount, (LPARAM)lpiArray);
6418 }
6419
6420 /***
6421 * DESCRIPTION:
6422 * Retrieves the column width.
6423 *
6424 * PARAMETER(S):
6425 * [I] infoPtr : valid pointer to the listview structure
6426 * [I] int : column index
6427 *
6428 * RETURN:
6429 * SUCCESS : column width
6430 * FAILURE : zero
6431 */
6432 static INT LISTVIEW_GetColumnWidth(const LISTVIEW_INFO *infoPtr, INT nColumn)
6433 {
6434 INT nColumnWidth = 0;
6435 HDITEMW hdItem;
6436
6437 TRACE("nColumn=%d\n", nColumn);
6438
6439 /* we have a 'column' in LIST and REPORT mode only */
6440 switch(infoPtr->uView)
6441 {
6442 case LV_VIEW_LIST:
6443 nColumnWidth = infoPtr->nItemWidth;
6444 break;
6445 case LV_VIEW_DETAILS:
6446 /* We are not using LISTVIEW_GetHeaderRect as this data is updated only after a HDN_ITEMCHANGED.
6447 * There is an application that subclasses the listview, calls LVM_GETCOLUMNWIDTH in the
6448 * HDN_ITEMCHANGED handler and goes into infinite recursion if it receives old data.
6449 *
6450 * TODO: should we do the same in LVM_GETCOLUMN?
6451 */
6452 hdItem.mask = HDI_WIDTH;
6453 if (!SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, nColumn, (LPARAM)&hdItem))
6454 {
6455 WARN("(%p): HDM_GETITEMW failed for item %d\n", infoPtr->hwndSelf, nColumn);
6456 return 0;
6457 }
6458 nColumnWidth = hdItem.cxy;
6459 break;
6460 }
6461
6462 TRACE("nColumnWidth=%d\n", nColumnWidth);
6463 return nColumnWidth;
6464 }
6465
6466 /***
6467 * DESCRIPTION:
6468 * In list or report display mode, retrieves the number of items that can fit
6469 * vertically in the visible area. In icon or small icon display mode,
6470 * retrieves the total number of visible items.
6471 *
6472 * PARAMETER(S):
6473 * [I] infoPtr : valid pointer to the listview structure
6474 *
6475 * RETURN:
6476 * Number of fully visible items.
6477 */
6478 static INT LISTVIEW_GetCountPerPage(const LISTVIEW_INFO *infoPtr)
6479 {
6480 switch (infoPtr->uView)
6481 {
6482 case LV_VIEW_ICON:
6483 case LV_VIEW_SMALLICON:
6484 return infoPtr->nItemCount;
6485 case LV_VIEW_DETAILS:
6486 return LISTVIEW_GetCountPerColumn(infoPtr);
6487 case LV_VIEW_LIST:
6488 return LISTVIEW_GetCountPerRow(infoPtr) * LISTVIEW_GetCountPerColumn(infoPtr);
6489 }
6490 assert(FALSE);
6491 return 0;
6492 }
6493
6494 /***
6495 * DESCRIPTION:
6496 * Retrieves an image list handle.
6497 *
6498 * PARAMETER(S):
6499 * [I] infoPtr : valid pointer to the listview structure
6500 * [I] nImageList : image list identifier
6501 *
6502 * RETURN:
6503 * SUCCESS : image list handle
6504 * FAILURE : NULL
6505 */
6506 static HIMAGELIST LISTVIEW_GetImageList(const LISTVIEW_INFO *infoPtr, INT nImageList)
6507 {
6508 switch (nImageList)
6509 {
6510 case LVSIL_NORMAL: return infoPtr->himlNormal;
6511 case LVSIL_SMALL: return infoPtr->himlSmall;
6512 case LVSIL_STATE: return infoPtr->himlState;
6513 case LVSIL_GROUPHEADER:
6514 FIXME("LVSIL_GROUPHEADER not supported\n");
6515 break;
6516 default:
6517 WARN("got unknown imagelist index - %d\n", nImageList);
6518 }
6519 return NULL;
6520 }
6521
6522 /* LISTVIEW_GetISearchString */
6523
6524 /***
6525 * DESCRIPTION:
6526 * Retrieves item attributes.
6527 *
6528 * PARAMETER(S):
6529 * [I] hwnd : window handle
6530 * [IO] lpLVItem : item info
6531 * [I] isW : if TRUE, then lpLVItem is a LPLVITEMW,
6532 * if FALSE, then lpLVItem is a LPLVITEMA.
6533 *
6534 * NOTE:
6535 * This is the internal 'GetItem' interface -- it tries to
6536 * be smart and avoid text copies, if possible, by modifying
6537 * lpLVItem->pszText to point to the text string. Please note
6538 * that this is not always possible (e.g. OWNERDATA), so on
6539 * entry you *must* supply valid values for pszText, and cchTextMax.
6540 * The only difference to the documented interface is that upon
6541 * return, you should use *only* the lpLVItem->pszText, rather than
6542 * the buffer pointer you provided on input. Most code already does
6543 * that, so it's not a problem.
6544 * For the two cases when the text must be copied (that is,
6545 * for LVM_GETITEM, and LVM_GETITEMTEXT), use LISTVIEW_GetItemExtT.
6546 *
6547 * RETURN:
6548 * SUCCESS : TRUE
6549 * FAILURE : FALSE
6550 */
6551 static BOOL LISTVIEW_GetItemT(const LISTVIEW_INFO *infoPtr, LPLVITEMW lpLVItem, BOOL isW)
6552 {
6553 ITEMHDR callbackHdr = { LPSTR_TEXTCALLBACKW, I_IMAGECALLBACK };
6554 NMLVDISPINFOW dispInfo;
6555 ITEM_INFO *lpItem;
6556 ITEMHDR* pItemHdr;
6557 HDPA hdpaSubItems;
6558 INT isubitem;
6559
6560 TRACE("(item=%s, isW=%d)\n", debuglvitem_t(lpLVItem, isW), isW);
6561
6562 if (!lpLVItem || lpLVItem->iItem < 0 || lpLVItem->iItem >= infoPtr->nItemCount)
6563 return FALSE;
6564
6565 if (lpLVItem->mask == 0) return TRUE;
6566 TRACE("mask=%x\n", lpLVItem->mask);
6567
6568 /* make a local copy */
6569 isubitem = lpLVItem->iSubItem;
6570
6571 /* a quick optimization if all we're asked is the focus state
6572 * these queries are worth optimising since they are common,
6573 * and can be answered in constant time, without the heavy accesses */
6574 if ( (lpLVItem->mask == LVIF_STATE) && (lpLVItem->stateMask == LVIS_FOCUSED) &&
6575 !(infoPtr->uCallbackMask & LVIS_FOCUSED) )
6576 {
6577 lpLVItem->state = 0;
6578 if (infoPtr->nFocusedItem == lpLVItem->iItem)
6579 lpLVItem->state |= LVIS_FOCUSED;
6580 return TRUE;
6581 }
6582
6583 ZeroMemory(&dispInfo, sizeof(dispInfo));
6584
6585 /* if the app stores all the data, handle it separately */
6586 if (infoPtr->dwStyle & LVS_OWNERDATA)
6587 {
6588 dispInfo.item.state = 0;
6589
6590 /* apparently, we should not callback for lParam in LVS_OWNERDATA */
6591 if ((lpLVItem->mask & ~(LVIF_STATE | LVIF_PARAM)) ||
6592 ((lpLVItem->mask & LVIF_STATE) && (infoPtr->uCallbackMask & lpLVItem->stateMask)))
6593 {
6594 UINT mask = lpLVItem->mask;
6595
6596 /* NOTE: copy only fields which we _know_ are initialized, some apps
6597 * depend on the uninitialized fields being 0 */
6598 dispInfo.item.mask = lpLVItem->mask & ~LVIF_PARAM;
6599 dispInfo.item.iItem = lpLVItem->iItem;
6600 dispInfo.item.iSubItem = isubitem;
6601 if (lpLVItem->mask & LVIF_TEXT)
6602 {
6603 if (lpLVItem->mask & LVIF_NORECOMPUTE)
6604 /* reset mask */
6605 dispInfo.item.mask &= ~(LVIF_TEXT | LVIF_NORECOMPUTE);
6606 else
6607 {
6608 dispInfo.item.pszText = lpLVItem->pszText;
6609 dispInfo.item.cchTextMax = lpLVItem->cchTextMax;
6610 }
6611 }
6612 if (lpLVItem->mask & LVIF_STATE)
6613 dispInfo.item.stateMask = lpLVItem->stateMask & infoPtr->uCallbackMask;
6614 /* could be zeroed on LVIF_NORECOMPUTE case */
6615 if (dispInfo.item.mask)
6616 {
6617 notify_dispinfoT(infoPtr, LVN_GETDISPINFOW, &dispInfo, isW);
6618 dispInfo.item.stateMask = lpLVItem->stateMask;
6619 if (lpLVItem->mask & (LVIF_GROUPID|LVIF_COLUMNS))
6620 {
6621 /* full size structure expected - _WIN32IE >= 0x560 */
6622 *lpLVItem = dispInfo.item;
6623 }
6624 else if (lpLVItem->mask & LVIF_INDENT)
6625 {
6626 /* indent member expected - _WIN32IE >= 0x300 */
6627 memcpy(lpLVItem, &dispInfo.item, offsetof( LVITEMW, iGroupId ));
6628 }
6629 else
6630 {
6631 /* minimal structure expected */
6632 memcpy(lpLVItem, &dispInfo.item, offsetof( LVITEMW, iIndent ));
6633 }
6634 lpLVItem->mask = mask;
6635 TRACE(" getdispinfo(1):lpLVItem=%s\n", debuglvitem_t(lpLVItem, isW));
6636 }
6637 }
6638
6639 /* make sure lParam is zeroed out */
6640 if (lpLVItem->mask & LVIF_PARAM) lpLVItem->lParam = 0;
6641
6642 /* callback marked pointer required here */
6643 if ((lpLVItem->mask & LVIF_TEXT) && (lpLVItem->mask & LVIF_NORECOMPUTE))
6644 lpLVItem->pszText = LPSTR_TEXTCALLBACKW;
6645
6646 /* we store only a little state, so if we're not asked, we're done */
6647 if (!(lpLVItem->mask & LVIF_STATE) || isubitem) return TRUE;
6648
6649 /* if focus is handled by us, report it */
6650 if ( lpLVItem->stateMask & ~infoPtr->uCallbackMask & LVIS_FOCUSED )
6651 {
6652 lpLVItem->state &= ~LVIS_FOCUSED;
6653 if (infoPtr->nFocusedItem == lpLVItem->iItem)
6654 lpLVItem->state |= LVIS_FOCUSED;
6655 }
6656
6657 /* and do the same for selection, if we handle it */
6658 if ( lpLVItem->stateMask & ~infoPtr->uCallbackMask & LVIS_SELECTED )
6659 {
6660 lpLVItem->state &= ~LVIS_SELECTED;
6661 if (ranges_contain(infoPtr->selectionRanges, lpLVItem->iItem))
6662 lpLVItem->state |= LVIS_SELECTED;
6663 }
6664
6665 return TRUE;
6666 }
6667
6668 /* find the item and subitem structures before we proceed */
6669 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, lpLVItem->iItem);
6670 lpItem = DPA_GetPtr(hdpaSubItems, 0);
6671 assert (lpItem);
6672
6673 if (isubitem)
6674 {
6675 SUBITEM_INFO *lpSubItem = LISTVIEW_GetSubItemPtr(hdpaSubItems, isubitem);
6676 pItemHdr = lpSubItem ? &lpSubItem->hdr : &callbackHdr;
6677 if (!lpSubItem)
6678 {
6679 WARN(" iSubItem invalid (%08x), ignored.\n", isubitem);
6680 isubitem = 0;
6681 }
6682 }
6683 else
6684 pItemHdr = &lpItem->hdr;
6685
6686 /* Do we need to query the state from the app? */
6687 if ((lpLVItem->mask & LVIF_STATE) && infoPtr->uCallbackMask && isubitem == 0)
6688 {
6689 dispInfo.item.mask |= LVIF_STATE;
6690 dispInfo.item.stateMask = infoPtr->uCallbackMask;
6691 }
6692
6693 /* Do we need to enquire about the image? */
6694 if ((lpLVItem->mask & LVIF_IMAGE) && pItemHdr->iImage == I_IMAGECALLBACK &&
6695 (isubitem == 0 || (infoPtr->dwLvExStyle & LVS_EX_SUBITEMIMAGES)))
6696 {
6697 dispInfo.item.mask |= LVIF_IMAGE;
6698 dispInfo.item.iImage = I_IMAGECALLBACK;
6699 }
6700
6701 /* Only items support indentation */
6702 if ((lpLVItem->mask & LVIF_INDENT) && lpItem->iIndent == I_INDENTCALLBACK &&
6703 (isubitem == 0))
6704 {
6705 dispInfo.item.mask |= LVIF_INDENT;
6706 dispInfo.item.iIndent = I_INDENTCALLBACK;
6707 }
6708
6709 /* Apps depend on calling back for text if it is NULL or LPSTR_TEXTCALLBACKW */
6710 if ((lpLVItem->mask & LVIF_TEXT) && !(lpLVItem->mask & LVIF_NORECOMPUTE) &&
6711 !is_text(pItemHdr->pszText))
6712 {
6713 dispInfo.item.mask |= LVIF_TEXT;
6714 dispInfo.item.pszText = lpLVItem->pszText;
6715 dispInfo.item.cchTextMax = lpLVItem->cchTextMax;
6716 if (dispInfo.item.pszText && dispInfo.item.cchTextMax > 0)
6717 *dispInfo.item.pszText = '\0';
6718 }
6719
6720 /* If we don't have all the requested info, query the application */
6721 if (dispInfo.item.mask)
6722 {
6723 dispInfo.item.iItem = lpLVItem->iItem;
6724 dispInfo.item.iSubItem = lpLVItem->iSubItem; /* yes: the original subitem */
6725 dispInfo.item.lParam = lpItem->lParam;
6726 notify_dispinfoT(infoPtr, LVN_GETDISPINFOW, &dispInfo, isW);
6727 TRACE(" getdispinfo(2):item=%s\n", debuglvitem_t(&dispInfo.item, isW));
6728 }
6729
6730 /* we should not store values for subitems */
6731 if (isubitem) dispInfo.item.mask &= ~LVIF_DI_SETITEM;
6732
6733 /* Now, handle the iImage field */
6734 if (dispInfo.item.mask & LVIF_IMAGE)
6735 {
6736 lpLVItem->iImage = dispInfo.item.iImage;
6737 if ((dispInfo.item.mask & LVIF_DI_SETITEM) && pItemHdr->iImage == I_IMAGECALLBACK)
6738 pItemHdr->iImage = dispInfo.item.iImage;
6739 }
6740 else if (lpLVItem->mask & LVIF_IMAGE)
6741 {
6742 if(isubitem == 0 || (infoPtr->dwLvExStyle & LVS_EX_SUBITEMIMAGES))
6743 lpLVItem->iImage = pItemHdr->iImage;
6744 else
6745 lpLVItem->iImage = 0;
6746 }
6747
6748 /* The pszText field */
6749 if (dispInfo.item.mask & LVIF_TEXT)
6750 {
6751 if ((dispInfo.item.mask & LVIF_DI_SETITEM) && pItemHdr->pszText)
6752 textsetptrT(&pItemHdr->pszText, dispInfo.item.pszText, isW);
6753
6754 lpLVItem->pszText = dispInfo.item.pszText;
6755 }
6756 else if (lpLVItem->mask & LVIF_TEXT)
6757 {
6758 /* if LVN_GETDISPINFO's disabled with LVIF_NORECOMPUTE return callback placeholder */
6759 if (isW || !is_text(pItemHdr->pszText)) lpLVItem->pszText = pItemHdr->pszText;
6760 else textcpynT(lpLVItem->pszText, isW, pItemHdr->pszText, TRUE, lpLVItem->cchTextMax);
6761 }
6762
6763 /* Next is the lParam field */
6764 if (dispInfo.item.mask & LVIF_PARAM)
6765 {
6766 lpLVItem->lParam = dispInfo.item.lParam;
6767 if ((dispInfo.item.mask & LVIF_DI_SETITEM))
6768 lpItem->lParam = dispInfo.item.lParam;
6769 }
6770 else if (lpLVItem->mask & LVIF_PARAM)
6771 lpLVItem->lParam = lpItem->lParam;
6772
6773 /* if this is a subitem, we're done */
6774 if (isubitem) return TRUE;
6775
6776 /* ... the state field (this one is different due to uCallbackmask) */
6777 if (lpLVItem->mask & LVIF_STATE)
6778 {
6779 lpLVItem->state = lpItem->state & lpLVItem->stateMask;
6780 if (dispInfo.item.mask & LVIF_STATE)
6781 {
6782 lpLVItem->state &= ~dispInfo.item.stateMask;
6783 lpLVItem->state |= (dispInfo.item.state & dispInfo.item.stateMask);
6784 }
6785 if ( lpLVItem->stateMask & ~infoPtr->uCallbackMask & LVIS_FOCUSED )
6786 {
6787 lpLVItem->state &= ~LVIS_FOCUSED;
6788 if (infoPtr->nFocusedItem == lpLVItem->iItem)
6789 lpLVItem->state |= LVIS_FOCUSED;
6790 }
6791 if ( lpLVItem->stateMask & ~infoPtr->uCallbackMask & LVIS_SELECTED )
6792 {
6793 lpLVItem->state &= ~LVIS_SELECTED;
6794 if (ranges_contain(infoPtr->selectionRanges, lpLVItem->iItem))
6795 lpLVItem->state |= LVIS_SELECTED;
6796 }
6797 }
6798
6799 /* and last, but not least, the indent field */
6800 if (dispInfo.item.mask & LVIF_INDENT)
6801 {
6802 lpLVItem->iIndent = dispInfo.item.iIndent;
6803 if ((dispInfo.item.mask & LVIF_DI_SETITEM) && lpItem->iIndent == I_INDENTCALLBACK)
6804 lpItem->iIndent = dispInfo.item.iIndent;
6805 }
6806 else if (lpLVItem->mask & LVIF_INDENT)
6807 {
6808 lpLVItem->iIndent = lpItem->iIndent;
6809 }
6810
6811 return TRUE;
6812 }
6813
6814 /***
6815 * DESCRIPTION:
6816 * Retrieves item attributes.
6817 *
6818 * PARAMETER(S):
6819 * [I] hwnd : window handle
6820 * [IO] lpLVItem : item info
6821 * [I] isW : if TRUE, then lpLVItem is a LPLVITEMW,
6822 * if FALSE, then lpLVItem is a LPLVITEMA.
6823 *
6824 * NOTE:
6825 * This is the external 'GetItem' interface -- it properly copies
6826 * the text in the provided buffer.
6827 *
6828 * RETURN:
6829 * SUCCESS : TRUE
6830 * FAILURE : FALSE
6831 */
6832 static BOOL LISTVIEW_GetItemExtT(const LISTVIEW_INFO *infoPtr, LPLVITEMW lpLVItem, BOOL isW)
6833 {
6834 LPWSTR pszText;
6835 BOOL bResult;
6836
6837 if (!lpLVItem || lpLVItem->iItem < 0 || lpLVItem->iItem >= infoPtr->nItemCount)
6838 return FALSE;
6839
6840 pszText = lpLVItem->pszText;
6841 bResult = LISTVIEW_GetItemT(infoPtr, lpLVItem, isW);
6842 if (bResult && (lpLVItem->mask & LVIF_TEXT) && lpLVItem->pszText != pszText)
6843 {
6844 if (lpLVItem->pszText != LPSTR_TEXTCALLBACKW)
6845 textcpynT(pszText, isW, lpLVItem->pszText, isW, lpLVItem->cchTextMax);
6846 else
6847 pszText = LPSTR_TEXTCALLBACKW;
6848 }
6849 lpLVItem->pszText = pszText;
6850
6851 return bResult;
6852 }
6853
6854
6855 /***
6856 * DESCRIPTION:
6857 * Retrieves the position (upper-left) of the listview control item.
6858 * Note that for LVS_ICON style, the upper-left is that of the icon
6859 * and not the bounding box.
6860 *
6861 * PARAMETER(S):
6862 * [I] infoPtr : valid pointer to the listview structure
6863 * [I] nItem : item index
6864 * [O] lpptPosition : coordinate information
6865 *
6866 * RETURN:
6867 * SUCCESS : TRUE
6868 * FAILURE : FALSE
6869 */
6870 static BOOL LISTVIEW_GetItemPosition(const LISTVIEW_INFO *infoPtr, INT nItem, LPPOINT lpptPosition)
6871 {
6872 POINT Origin;
6873
6874 TRACE("(nItem=%d, lpptPosition=%p)\n", nItem, lpptPosition);
6875
6876 if (!lpptPosition || nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
6877
6878 LISTVIEW_GetOrigin(infoPtr, &Origin);
6879 LISTVIEW_GetItemOrigin(infoPtr, nItem, lpptPosition);
6880
6881 if (infoPtr->uView == LV_VIEW_ICON)
6882 {
6883 lpptPosition->x += (infoPtr->nItemWidth - infoPtr->iconSize.cx) / 2;
6884 lpptPosition->y += ICON_TOP_PADDING;
6885 }
6886 lpptPosition->x += Origin.x;
6887 lpptPosition->y += Origin.y;
6888
6889 TRACE (" lpptPosition=%s\n", wine_dbgstr_point(lpptPosition));
6890 return TRUE;
6891 }
6892
6893
6894 /***
6895 * DESCRIPTION:
6896 * Retrieves the bounding rectangle for a listview control item.
6897 *
6898 * PARAMETER(S):
6899 * [I] infoPtr : valid pointer to the listview structure
6900 * [I] nItem : item index
6901 * [IO] lprc : bounding rectangle coordinates
6902 * lprc->left specifies the portion of the item for which the bounding
6903 * rectangle will be retrieved.
6904 *
6905 * LVIR_BOUNDS Returns the bounding rectangle of the entire item,
6906 * including the icon and label.
6907 * *
6908 * * For LVS_ICON
6909 * * Experiment shows that native control returns:
6910 * * width = min (48, length of text line)
6911 * * .left = position.x - (width - iconsize.cx)/2
6912 * * .right = .left + width
6913 * * height = #lines of text * ntmHeight + icon height + 8
6914 * * .top = position.y - 2
6915 * * .bottom = .top + height
6916 * * separation between items .y = itemSpacing.cy - height
6917 * * .x = itemSpacing.cx - width
6918 * LVIR_ICON Returns the bounding rectangle of the icon or small icon.
6919 * *
6920 * * For LVS_ICON
6921 * * Experiment shows that native control returns:
6922 * * width = iconSize.cx + 16
6923 * * .left = position.x - (width - iconsize.cx)/2
6924 * * .right = .left + width
6925 * * height = iconSize.cy + 4
6926 * * .top = position.y - 2
6927 * * .bottom = .top + height
6928 * * separation between items .y = itemSpacing.cy - height
6929 * * .x = itemSpacing.cx - width
6930 * LVIR_LABEL Returns the bounding rectangle of the item text.
6931 * *
6932 * * For LVS_ICON
6933 * * Experiment shows that native control returns:
6934 * * width = text length
6935 * * .left = position.x - width/2
6936 * * .right = .left + width
6937 * * height = ntmH * linecount + 2
6938 * * .top = position.y + iconSize.cy + 6
6939 * * .bottom = .top + height
6940 * * separation between items .y = itemSpacing.cy - height
6941 * * .x = itemSpacing.cx - width
6942 * LVIR_SELECTBOUNDS Returns the union of the LVIR_ICON and LVIR_LABEL
6943 * rectangles, but excludes columns in report view.
6944 *
6945 * RETURN:
6946 * SUCCESS : TRUE
6947 * FAILURE : FALSE
6948 *
6949 * NOTES
6950 * Note that the bounding rectangle of the label in the LVS_ICON view depends
6951 * upon whether the window has the focus currently and on whether the item
6952 * is the one with the focus. Ensure that the control's record of which
6953 * item has the focus agrees with the items' records.
6954 */
6955 static BOOL LISTVIEW_GetItemRect(const LISTVIEW_INFO *infoPtr, INT nItem, LPRECT lprc)
6956 {
6957 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
6958 BOOL doLabel = TRUE, oversizedBox = FALSE;
6959 POINT Position, Origin;
6960 LVITEMW lvItem;
6961 LONG mode;
6962
6963 TRACE("(hwnd=%p, nItem=%d, lprc=%p)\n", infoPtr->hwndSelf, nItem, lprc);
6964
6965 if (!lprc || nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
6966
6967 LISTVIEW_GetOrigin(infoPtr, &Origin);
6968 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position);
6969
6970 /* Be smart and try to figure out the minimum we have to do */
6971 if (lprc->left == LVIR_ICON) doLabel = FALSE;
6972 if (infoPtr->uView == LV_VIEW_DETAILS && lprc->left == LVIR_BOUNDS) doLabel = FALSE;
6973 if (infoPtr->uView == LV_VIEW_ICON && lprc->left != LVIR_ICON &&
6974 infoPtr->bFocus && LISTVIEW_GetItemState(infoPtr, nItem, LVIS_FOCUSED))
6975 oversizedBox = TRUE;
6976
6977 /* get what we need from the item before hand, so we make
6978 * only one request. This can speed up things, if data
6979 * is stored on the app side */
6980 lvItem.mask = 0;
6981 if (infoPtr->uView == LV_VIEW_DETAILS) lvItem.mask |= LVIF_INDENT;
6982 if (doLabel) lvItem.mask |= LVIF_TEXT;
6983 lvItem.iItem = nItem;
6984 lvItem.iSubItem = 0;
6985 lvItem.pszText = szDispText;
6986 lvItem.cchTextMax = DISP_TEXT_SIZE;
6987 if (lvItem.mask && !LISTVIEW_GetItemW(infoPtr, &lvItem)) return FALSE;
6988 /* we got the state already up, simulate it here, to avoid a reget */
6989 if (infoPtr->uView == LV_VIEW_ICON && (lprc->left != LVIR_ICON))
6990 {
6991 lvItem.mask |= LVIF_STATE;
6992 lvItem.stateMask = LVIS_FOCUSED;
6993 lvItem.state = (oversizedBox ? LVIS_FOCUSED : 0);
6994 }
6995
6996 if (infoPtr->uView == LV_VIEW_DETAILS && (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT) && lprc->left == LVIR_SELECTBOUNDS)
6997 lprc->left = LVIR_BOUNDS;
6998
6999 mode = lprc->left;
7000 switch(lprc->left)
7001 {
7002 case LVIR_ICON:
7003 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, NULL, NULL, lprc, NULL, NULL);
7004 break;
7005
7006 case LVIR_LABEL:
7007 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, NULL, NULL, NULL, NULL, lprc);
7008 break;
7009
7010 case LVIR_BOUNDS:
7011 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, lprc, NULL, NULL, NULL, NULL);
7012 break;
7013
7014 case LVIR_SELECTBOUNDS:
7015 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, NULL, lprc, NULL, NULL, NULL);
7016 break;
7017
7018 default:
7019 WARN("Unknown value: %d\n", lprc->left);
7020 return FALSE;
7021 }
7022
7023 if (infoPtr->uView == LV_VIEW_DETAILS)
7024 {
7025 if (mode != LVIR_BOUNDS)
7026 OffsetRect(lprc, Origin.x + LISTVIEW_GetColumnInfo(infoPtr, 0)->rcHeader.left,
7027 Position.y + Origin.y);
7028 else
7029 OffsetRect(lprc, Origin.x, Position.y + Origin.y);
7030 }
7031 else
7032 OffsetRect(lprc, Position.x + Origin.x, Position.y + Origin.y);
7033
7034 TRACE(" rect=%s\n", wine_dbgstr_rect(lprc));
7035
7036 return TRUE;
7037 }
7038
7039 /***
7040 * DESCRIPTION:
7041 * Retrieves the spacing between listview control items.
7042 *
7043 * PARAMETER(S):
7044 * [I] infoPtr : valid pointer to the listview structure
7045 * [IO] lprc : rectangle to receive the output
7046 * on input, lprc->top = nSubItem
7047 * lprc->left = LVIR_ICON | LVIR_BOUNDS | LVIR_LABEL
7048 *
7049 * NOTE: for subItem = 0, we should return the bounds of the _entire_ item,
7050 * not only those of the first column.
7051 *
7052 * RETURN:
7053 * TRUE: success
7054 * FALSE: failure
7055 */
7056 static BOOL LISTVIEW_GetSubItemRect(const LISTVIEW_INFO *infoPtr, INT item, LPRECT lprc)
7057 {
7058 RECT rect = { 0, 0, 0, 0 };
7059 POINT origin;
7060 INT y;
7061
7062 if (!lprc) return FALSE;
7063
7064 TRACE("(item=%d, subitem=%d, type=%d)\n", item, lprc->top, lprc->left);
7065 /* Subitem of '0' means item itself, and this works for all control view modes */
7066 if (lprc->top == 0)
7067 return LISTVIEW_GetItemRect(infoPtr, item, lprc);
7068
7069 if (infoPtr->uView != LV_VIEW_DETAILS) return FALSE;
7070
7071 LISTVIEW_GetOrigin(infoPtr, &origin);
7072 /* this works for any item index, no matter if it exists or not */
7073 y = item * infoPtr->nItemHeight + origin.y;
7074
7075 if (infoPtr->hwndHeader && SendMessageW(infoPtr->hwndHeader, HDM_GETITEMRECT, lprc->top, (LPARAM)&rect))
7076 {
7077 rect.top = 0;
7078 rect.bottom = infoPtr->nItemHeight;
7079 }
7080 else
7081 {
7082 /* Native implementation is broken for this case and garbage is left for left and right fields,
7083 we zero them to get predictable output */
7084 lprc->left = lprc->right = lprc->top = 0;
7085 lprc->bottom = infoPtr->nItemHeight;
7086 OffsetRect(lprc, origin.x, y);
7087 TRACE("return rect %s\n", wine_dbgstr_rect(lprc));
7088 return TRUE;
7089 }
7090
7091 switch (lprc->left)
7092 {
7093 case LVIR_ICON:
7094 {
7095 /* it doesn't matter if main item actually has an icon, if imagelist is set icon width is returned */
7096 if (infoPtr->himlSmall)
7097 rect.right = rect.left + infoPtr->iconSize.cx;
7098 else
7099 rect.right = rect.left;
7100
7101 rect.bottom = rect.top + infoPtr->iconSize.cy;
7102 break;
7103 }
7104 case LVIR_LABEL:
7105 case LVIR_BOUNDS:
7106 break;
7107
7108 default:
7109 ERR("Unknown bounds=%d\n", lprc->left);
7110 return FALSE;
7111 }
7112
7113 OffsetRect(&rect, origin.x, y);
7114 *lprc = rect;
7115 TRACE("return rect %s\n", wine_dbgstr_rect(lprc));
7116
7117 return TRUE;
7118 }
7119
7120 /***
7121 * DESCRIPTION:
7122 * Retrieves the spacing between listview control items.
7123 *
7124 * PARAMETER(S):
7125 * [I] infoPtr : valid pointer to the listview structure
7126 * [I] bSmall : flag for small or large icon
7127 *
7128 * RETURN:
7129 * Horizontal + vertical spacing
7130 */
7131 static LONG LISTVIEW_GetItemSpacing(const LISTVIEW_INFO *infoPtr, BOOL bSmall)
7132 {
7133 LONG lResult;
7134
7135 if (!bSmall)
7136 {
7137 lResult = MAKELONG(infoPtr->iconSpacing.cx, infoPtr->iconSpacing.cy);
7138 }
7139 else
7140 {
7141 if (infoPtr->uView == LV_VIEW_ICON)
7142 lResult = MAKELONG(DEFAULT_COLUMN_WIDTH, GetSystemMetrics(SM_CXSMICON)+HEIGHT_PADDING);
7143 else
7144 lResult = MAKELONG(infoPtr->nItemWidth, infoPtr->nItemHeight);
7145 }
7146 return lResult;
7147 }
7148
7149 /***
7150 * DESCRIPTION:
7151 * Retrieves the state of a listview control item.
7152 *
7153 * PARAMETER(S):
7154 * [I] infoPtr : valid pointer to the listview structure
7155 * [I] nItem : item index
7156 * [I] uMask : state mask
7157 *
7158 * RETURN:
7159 * State specified by the mask.
7160 */
7161 static UINT LISTVIEW_GetItemState(const LISTVIEW_INFO *infoPtr, INT nItem, UINT uMask)
7162 {
7163 LVITEMW lvItem;
7164
7165 if (nItem < 0 || nItem >= infoPtr->nItemCount) return 0;
7166
7167 lvItem.iItem = nItem;
7168 lvItem.iSubItem = 0;
7169 lvItem.mask = LVIF_STATE;
7170 lvItem.stateMask = uMask;
7171 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) return 0;
7172
7173 return lvItem.state & uMask;
7174 }
7175
7176 /***
7177 * DESCRIPTION:
7178 * Retrieves the text of a listview control item or subitem.
7179 *
7180 * PARAMETER(S):
7181 * [I] hwnd : window handle
7182 * [I] nItem : item index
7183 * [IO] lpLVItem : item information
7184 * [I] isW : TRUE if lpLVItem is Unicode
7185 *
7186 * RETURN:
7187 * SUCCESS : string length
7188 * FAILURE : 0
7189 */
7190 static INT LISTVIEW_GetItemTextT(const LISTVIEW_INFO *infoPtr, INT nItem, LPLVITEMW lpLVItem, BOOL isW)
7191 {
7192 if (!lpLVItem || nItem < 0 || nItem >= infoPtr->nItemCount) return 0;
7193
7194 lpLVItem->mask = LVIF_TEXT;
7195 lpLVItem->iItem = nItem;
7196 if (!LISTVIEW_GetItemExtT(infoPtr, lpLVItem, isW)) return 0;
7197
7198 return textlenT(lpLVItem->pszText, isW);
7199 }
7200
7201 /***
7202 * DESCRIPTION:
7203 * Searches for an item based on properties + relationships.
7204 *
7205 * PARAMETER(S):
7206 * [I] infoPtr : valid pointer to the listview structure
7207 * [I] nItem : item index
7208 * [I] uFlags : relationship flag
7209 *
7210 * RETURN:
7211 * SUCCESS : item index
7212 * FAILURE : -1
7213 */
7214 static INT LISTVIEW_GetNextItem(const LISTVIEW_INFO *infoPtr, INT nItem, UINT uFlags)
7215 {
7216 UINT uMask = 0;
7217 LVFINDINFOW lvFindInfo;
7218 INT nCountPerColumn;
7219 INT nCountPerRow;
7220 INT i;
7221
7222 TRACE("nItem=%d, uFlags=%x, nItemCount=%d\n", nItem, uFlags, infoPtr->nItemCount);
7223 if (nItem < -1 || nItem >= infoPtr->nItemCount) return -1;
7224
7225 ZeroMemory(&lvFindInfo, sizeof(lvFindInfo));
7226
7227 if (uFlags & LVNI_CUT)
7228 uMask |= LVIS_CUT;
7229
7230 if (uFlags & LVNI_DROPHILITED)
7231 uMask |= LVIS_DROPHILITED;
7232
7233 if (uFlags & LVNI_FOCUSED)
7234 uMask |= LVIS_FOCUSED;
7235
7236 if (uFlags & LVNI_SELECTED)
7237 uMask |= LVIS_SELECTED;
7238
7239 /* if we're asked for the focused item, that's only one,
7240 * so it's worth optimizing */
7241 if (uFlags & LVNI_FOCUSED)
7242 {
7243 if ((LISTVIEW_GetItemState(infoPtr, infoPtr->nFocusedItem, uMask) & uMask) != uMask) return -1;
7244 return (infoPtr->nFocusedItem == nItem) ? -1 : infoPtr->nFocusedItem;
7245 }
7246
7247 if (uFlags & LVNI_ABOVE)
7248 {
7249 if ((infoPtr->uView == LV_VIEW_LIST) || (infoPtr->uView == LV_VIEW_DETAILS))
7250 {
7251 while (nItem >= 0)
7252 {
7253 nItem--;
7254 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7255 return nItem;
7256 }
7257 }
7258 else
7259 {
7260 /* Special case for autoarrange - move 'til the top of a list */
7261 if (is_autoarrange(infoPtr))
7262 {
7263 nCountPerRow = LISTVIEW_GetCountPerRow(infoPtr);
7264 while (nItem - nCountPerRow >= 0)
7265 {
7266 nItem -= nCountPerRow;
7267 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7268 return nItem;
7269 }
7270 return -1;
7271 }
7272 lvFindInfo.flags = LVFI_NEARESTXY;
7273 lvFindInfo.vkDirection = VK_UP;
7274 LISTVIEW_GetItemPosition(infoPtr, nItem, &lvFindInfo.pt);
7275 while ((nItem = LISTVIEW_FindItemW(infoPtr, nItem, &lvFindInfo)) != -1)
7276 {
7277 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7278 return nItem;
7279 }
7280 }
7281 }
7282 else if (uFlags & LVNI_BELOW)
7283 {
7284 if ((infoPtr->uView == LV_VIEW_LIST) || (infoPtr->uView == LV_VIEW_DETAILS))
7285 {
7286 while (nItem < infoPtr->nItemCount)
7287 {
7288 nItem++;
7289 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7290 return nItem;
7291 }
7292 }
7293 else
7294 {
7295 /* Special case for autoarrange - move 'til the bottom of a list */
7296 if (is_autoarrange(infoPtr))
7297 {
7298 nCountPerRow = LISTVIEW_GetCountPerRow(infoPtr);
7299 while (nItem + nCountPerRow < infoPtr->nItemCount )
7300 {
7301 nItem += nCountPerRow;
7302 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7303 return nItem;
7304 }
7305 return -1;
7306 }
7307 lvFindInfo.flags = LVFI_NEARESTXY;
7308 lvFindInfo.vkDirection = VK_DOWN;
7309 LISTVIEW_GetItemPosition(infoPtr, nItem, &lvFindInfo.pt);
7310 while ((nItem = LISTVIEW_FindItemW(infoPtr, nItem, &lvFindInfo)) != -1)
7311 {
7312 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7313 return nItem;
7314 }
7315 }
7316 }
7317 else if (uFlags & LVNI_TOLEFT)
7318 {
7319 if (infoPtr->uView == LV_VIEW_LIST)
7320 {
7321 nCountPerColumn = LISTVIEW_GetCountPerColumn(infoPtr);
7322 while (nItem - nCountPerColumn >= 0)
7323 {
7324 nItem -= nCountPerColumn;
7325 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7326 return nItem;
7327 }
7328 }
7329 else if ((infoPtr->uView == LV_VIEW_SMALLICON) || (infoPtr->uView == LV_VIEW_ICON))
7330 {
7331 /* Special case for autoarrange - move 'til the beginning of a row */
7332 if (is_autoarrange(infoPtr))
7333 {
7334 nCountPerRow = LISTVIEW_GetCountPerRow(infoPtr);
7335 while (nItem % nCountPerRow > 0)
7336 {
7337 nItem --;
7338 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7339 return nItem;
7340 }
7341 return -1;
7342 }
7343 lvFindInfo.flags = LVFI_NEARESTXY;
7344 lvFindInfo.vkDirection = VK_LEFT;
7345 LISTVIEW_GetItemPosition(infoPtr, nItem, &lvFindInfo.pt);
7346 while ((nItem = LISTVIEW_FindItemW(infoPtr, nItem, &lvFindInfo)) != -1)
7347 {
7348 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7349 return nItem;
7350 }
7351 }
7352 }
7353 else if (uFlags & LVNI_TORIGHT)
7354 {
7355 if (infoPtr->uView == LV_VIEW_LIST)
7356 {
7357 nCountPerColumn = LISTVIEW_GetCountPerColumn(infoPtr);
7358 while (nItem + nCountPerColumn < infoPtr->nItemCount)
7359 {
7360 nItem += nCountPerColumn;
7361 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7362 return nItem;
7363 }
7364 }
7365 else if ((infoPtr->uView == LV_VIEW_SMALLICON) || (infoPtr->uView == LV_VIEW_ICON))
7366 {
7367 /* Special case for autoarrange - move 'til the end of a row */
7368 if (is_autoarrange(infoPtr))
7369 {
7370 nCountPerRow = LISTVIEW_GetCountPerRow(infoPtr);
7371 while (nItem % nCountPerRow < nCountPerRow - 1 )
7372 {
7373 nItem ++;
7374 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7375 return nItem;
7376 }
7377 return -1;
7378 }
7379 lvFindInfo.flags = LVFI_NEARESTXY;
7380 lvFindInfo.vkDirection = VK_RIGHT;
7381 LISTVIEW_GetItemPosition(infoPtr, nItem, &lvFindInfo.pt);
7382 while ((nItem = LISTVIEW_FindItemW(infoPtr, nItem, &lvFindInfo)) != -1)
7383 {
7384 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7385 return nItem;
7386 }
7387 }
7388 }
7389 else
7390 {
7391 nItem++;
7392
7393 /* search by index */
7394 for (i = nItem; i < infoPtr->nItemCount; i++)
7395 {
7396 if ((LISTVIEW_GetItemState(infoPtr, i, uMask) & uMask) == uMask)
7397 return i;
7398 }
7399 }
7400
7401 return -1;
7402 }
7403
7404 /* LISTVIEW_GetNumberOfWorkAreas */
7405
7406 /***
7407 * DESCRIPTION:
7408 * Retrieves the origin coordinates when in icon or small icon display mode.
7409 *
7410 * PARAMETER(S):
7411 * [I] infoPtr : valid pointer to the listview structure
7412 * [O] lpptOrigin : coordinate information
7413 *
7414 * RETURN:
7415 * None.
7416 */
7417 static void LISTVIEW_GetOrigin(const LISTVIEW_INFO *infoPtr, LPPOINT lpptOrigin)
7418 {
7419 INT nHorzPos = 0, nVertPos = 0;
7420 SCROLLINFO scrollInfo;
7421
7422 scrollInfo.cbSize = sizeof(SCROLLINFO);
7423 scrollInfo.fMask = SIF_POS;
7424
7425 if (GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo))
7426 nHorzPos = scrollInfo.nPos;
7427 if (GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo))
7428 nVertPos = scrollInfo.nPos;
7429
7430 TRACE("nHorzPos=%d, nVertPos=%d\n", nHorzPos, nVertPos);
7431
7432 lpptOrigin->x = infoPtr->rcList.left;
7433 lpptOrigin->y = infoPtr->rcList.top;
7434 if (infoPtr->uView == LV_VIEW_LIST)
7435 nHorzPos *= infoPtr->nItemWidth;
7436 else if (infoPtr->uView == LV_VIEW_DETAILS)
7437 nVertPos *= infoPtr->nItemHeight;
7438
7439 lpptOrigin->x -= nHorzPos;
7440 lpptOrigin->y -= nVertPos;
7441
7442 TRACE(" origin=%s\n", wine_dbgstr_point(lpptOrigin));
7443 }
7444
7445 /***
7446 * DESCRIPTION:
7447 * Retrieves the width of a string.
7448 *
7449 * PARAMETER(S):
7450 * [I] hwnd : window handle
7451 * [I] lpszText : text string to process
7452 * [I] isW : TRUE if lpszText is Unicode, FALSE otherwise
7453 *
7454 * RETURN:
7455 * SUCCESS : string width (in pixels)
7456 * FAILURE : zero
7457 */
7458 static INT LISTVIEW_GetStringWidthT(const LISTVIEW_INFO *infoPtr, LPCWSTR lpszText, BOOL isW)
7459 {
7460 SIZE stringSize;
7461
7462 stringSize.cx = 0;
7463 if (is_text(lpszText))
7464 {
7465 HFONT hFont = infoPtr->hFont ? infoPtr->hFont : infoPtr->hDefaultFont;
7466 HDC hdc = GetDC(infoPtr->hwndSelf);
7467 HFONT hOldFont = SelectObject(hdc, hFont);
7468
7469 if (isW)
7470 GetTextExtentPointW(hdc, lpszText, lstrlenW(lpszText), &stringSize);
7471 else
7472 GetTextExtentPointA(hdc, (LPCSTR)lpszText, lstrlenA((LPCSTR)lpszText), &stringSize);
7473 SelectObject(hdc, hOldFont);
7474 ReleaseDC(infoPtr->hwndSelf, hdc);
7475 }
7476 return stringSize.cx;
7477 }
7478
7479 /***
7480 * DESCRIPTION:
7481 * Determines which listview item is located at the specified position.
7482 *
7483 * PARAMETER(S):
7484 * [I] infoPtr : valid pointer to the listview structure
7485 * [IO] lpht : hit test information
7486 * [I] subitem : fill out iSubItem.
7487 * [I] select : return the index only if the hit selects the item
7488 *
7489 * NOTE:
7490 * (mm 20001022): We must not allow iSubItem to be touched, for
7491 * an app might pass only a structure with space up to iItem!
7492 * (MS Office 97 does that for instance in the file open dialog)
7493 *
7494 * RETURN:
7495 * SUCCESS : item index
7496 * FAILURE : -1
7497 */
7498 static INT LISTVIEW_HitTest(const LISTVIEW_INFO *infoPtr, LPLVHITTESTINFO lpht, BOOL subitem, BOOL select)
7499 {
7500 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
7501 RECT rcBox, rcBounds, rcState, rcIcon, rcLabel, rcSearch;
7502 POINT Origin, Position, opt;
7503 LVITEMW lvItem;
7504 ITERATOR i;
7505 INT iItem;
7506
7507 TRACE("(pt=%s, subitem=%d, select=%d)\n", wine_dbgstr_point(&lpht->pt), subitem, select);
7508
7509 lpht->flags = 0;
7510 lpht->iItem = -1;
7511 if (subitem) lpht->iSubItem = 0;
7512
7513 LISTVIEW_GetOrigin(infoPtr, &Origin);
7514
7515 /* set whole list relation flags */
7516 if (subitem && infoPtr->uView == LV_VIEW_DETAILS)
7517 {
7518 /* LVM_SUBITEMHITTEST checks left bound of possible client area */
7519 if (infoPtr->rcList.left > lpht->pt.x && Origin.x < lpht->pt.x)
7520 lpht->flags |= LVHT_TOLEFT;
7521
7522 if (lpht->pt.y < infoPtr->rcList.top && lpht->pt.y >= 0)
7523 opt.y = lpht->pt.y + infoPtr->rcList.top;
7524 else
7525 opt.y = lpht->pt.y;
7526
7527 if (infoPtr->rcList.bottom < opt.y)
7528 lpht->flags |= LVHT_BELOW;
7529 }
7530 else
7531 {
7532 if (infoPtr->rcList.left > lpht->pt.x)
7533 lpht->flags |= LVHT_TOLEFT;
7534 else if (infoPtr->rcList.right < lpht->pt.x)
7535 lpht->flags |= LVHT_TORIGHT;
7536
7537 if (infoPtr->rcList.top > lpht->pt.y)
7538 lpht->flags |= LVHT_ABOVE;
7539 else if (infoPtr->rcList.bottom < lpht->pt.y)
7540 lpht->flags |= LVHT_BELOW;
7541 }
7542
7543 /* even if item is invalid try to find subitem */
7544 if (infoPtr->uView == LV_VIEW_DETAILS && subitem)
7545 {
7546 RECT *pRect;
7547 INT j;
7548
7549 opt.x = lpht->pt.x - Origin.x;
7550
7551 lpht->iSubItem = -1;
7552 for (j = 0; j < DPA_GetPtrCount(infoPtr->hdpaColumns); j++)
7553 {
7554 pRect = &LISTVIEW_GetColumnInfo(infoPtr, j)->rcHeader;
7555
7556 if ((opt.x >= pRect->left) && (opt.x < pRect->right))
7557 {
7558 lpht->iSubItem = j;
7559 break;
7560 }
7561 }
7562 TRACE("lpht->iSubItem=%d\n", lpht->iSubItem);
7563
7564 /* if we're outside horizontal columns bounds there's nothing to test further */
7565 if (lpht->iSubItem == -1)
7566 {
7567 lpht->iItem = -1;
7568 lpht->flags = LVHT_NOWHERE;
7569 return -1;
7570 }
7571 }
7572
7573 TRACE("lpht->flags=0x%x\n", lpht->flags);
7574 if (lpht->flags) return -1;
7575
7576 lpht->flags |= LVHT_NOWHERE;
7577
7578 /* first deal with the large items */
7579 rcSearch.left = lpht->pt.x;
7580 rcSearch.top = lpht->pt.y;
7581 rcSearch.right = rcSearch.left + 1;
7582 rcSearch.bottom = rcSearch.top + 1;
7583
7584 iterator_frameditems(&i, infoPtr, &rcSearch);
7585 iterator_next(&i); /* go to first item in the sequence */
7586 iItem = i.nItem;
7587 iterator_destroy(&i);
7588
7589 TRACE("lpht->iItem=%d\n", iItem);
7590 if (iItem == -1) return -1;
7591
7592 lvItem.mask = LVIF_STATE | LVIF_TEXT;
7593 if (infoPtr->uView == LV_VIEW_DETAILS) lvItem.mask |= LVIF_INDENT;
7594 lvItem.stateMask = LVIS_STATEIMAGEMASK;
7595 if (infoPtr->uView == LV_VIEW_ICON) lvItem.stateMask |= LVIS_FOCUSED;
7596 lvItem.iItem = iItem;
7597 lvItem.iSubItem = subitem ? lpht->iSubItem : 0;
7598 lvItem.pszText = szDispText;
7599 lvItem.cchTextMax = DISP_TEXT_SIZE;
7600 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) return -1;
7601 if (!infoPtr->bFocus) lvItem.state &= ~LVIS_FOCUSED;
7602
7603 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, &rcBox, NULL, &rcIcon, &rcState, &rcLabel);
7604 LISTVIEW_GetItemOrigin(infoPtr, iItem, &Position);
7605 opt.x = lpht->pt.x - Position.x - Origin.x;
7606
7607 if (lpht->pt.y < infoPtr->rcList.top && lpht->pt.y >= 0)
7608 opt.y = lpht->pt.y - Position.y - Origin.y + infoPtr->rcList.top;
7609 else
7610 opt.y = lpht->pt.y - Position.y - Origin.y;
7611
7612 if (infoPtr->uView == LV_VIEW_DETAILS)
7613 {
7614 rcBounds = rcBox;
7615 if (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT)
7616 opt.x = lpht->pt.x - Origin.x;
7617 }
7618 else
7619 {
7620 UnionRect(&rcBounds, &rcIcon, &rcLabel);
7621 UnionRect(&rcBounds, &rcBounds, &rcState);
7622 }
7623 TRACE("rcBounds=%s\n", wine_dbgstr_rect(&rcBounds));
7624 if (!PtInRect(&rcBounds, opt)) return -1;
7625
7626 if (PtInRect(&rcIcon, opt))
7627 lpht->flags |= LVHT_ONITEMICON;
7628 else if (PtInRect(&rcLabel, opt))
7629 lpht->flags |= LVHT_ONITEMLABEL;
7630 else if (infoPtr->himlState && PtInRect(&rcState, opt))
7631 lpht->flags |= LVHT_ONITEMSTATEICON;
7632 /* special case for LVS_EX_FULLROWSELECT */
7633 if (infoPtr->uView == LV_VIEW_DETAILS && infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT &&
7634 !(lpht->flags & LVHT_ONITEM))
7635 {
7636 lpht->flags = LVHT_ONITEM | LVHT_ABOVE;
7637 }
7638 if (lpht->flags & LVHT_ONITEM)
7639 lpht->flags &= ~LVHT_NOWHERE;
7640 TRACE("lpht->flags=0x%x\n", lpht->flags);
7641
7642 if (select && !(infoPtr->uView == LV_VIEW_DETAILS &&
7643 ((infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT) ||
7644 (infoPtr->dwStyle & LVS_OWNERDRAWFIXED))))
7645 {
7646 if (infoPtr->uView == LV_VIEW_DETAILS)
7647 {
7648 /* get main item bounds */
7649 lvItem.iSubItem = 0;
7650 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, &rcBox, NULL, &rcIcon, &rcState, &rcLabel);
7651 UnionRect(&rcBounds, &rcIcon, &rcLabel);
7652 UnionRect(&rcBounds, &rcBounds, &rcState);
7653 }
7654 if (!PtInRect(&rcBounds, opt)) iItem = -1;
7655 }
7656 return lpht->iItem = iItem;
7657 }
7658
7659 /***
7660 * DESCRIPTION:
7661 * Inserts a new item in the listview control.
7662 *
7663 * PARAMETER(S):
7664 * [I] infoPtr : valid pointer to the listview structure
7665 * [I] lpLVItem : item information
7666 * [I] isW : TRUE if lpLVItem is Unicode, FALSE if it's ANSI
7667 *
7668 * RETURN:
7669 * SUCCESS : new item index
7670 * FAILURE : -1
7671 */
7672 static INT LISTVIEW_InsertItemT(LISTVIEW_INFO *infoPtr, const LVITEMW *lpLVItem, BOOL isW)
7673 {
7674 INT nItem;
7675 HDPA hdpaSubItems;
7676 NMLISTVIEW nmlv;
7677 ITEM_INFO *lpItem;
7678 ITEM_ID *lpID;
7679 BOOL is_sorted, has_changed;
7680 LVITEMW item;
7681 HWND hwndSelf = infoPtr->hwndSelf;
7682
7683 TRACE("(item=%s, isW=%d)\n", debuglvitem_t(lpLVItem, isW), isW);
7684
7685 if (infoPtr->dwStyle & LVS_OWNERDATA) return infoPtr->nItemCount++;
7686
7687 /* make sure it's an item, and not a subitem; cannot insert a subitem */
7688 if (!lpLVItem || lpLVItem->iSubItem) return -1;
7689
7690 if (!is_assignable_item(lpLVItem, infoPtr->dwStyle)) return -1;
7691
7692 if (!(lpItem = Alloc(sizeof(ITEM_INFO)))) return -1;
7693
7694 /* insert item in listview control data structure */
7695 if ( !(hdpaSubItems = DPA_Create(8)) ) goto fail;
7696 if ( !DPA_SetPtr(hdpaSubItems, 0, lpItem) ) assert (FALSE);
7697
7698 /* link with id struct */
7699 if (!(lpID = Alloc(sizeof(ITEM_ID)))) goto fail;
7700 lpItem->id = lpID;
7701 lpID->item = hdpaSubItems;
7702 lpID->id = get_next_itemid(infoPtr);
7703 if ( DPA_InsertPtr(infoPtr->hdpaItemIds, infoPtr->nItemCount, lpID) == -1) goto fail;
7704
7705 is_sorted = (infoPtr->dwStyle & (LVS_SORTASCENDING | LVS_SORTDESCENDING)) &&
7706 !(infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && (LPSTR_TEXTCALLBACKW != lpLVItem->pszText);
7707
7708 if (lpLVItem->iItem < 0 && !is_sorted) return -1;
7709
7710 /* calculate new item index */
7711 if (is_sorted)
7712 {
7713 HDPA hItem;
7714 ITEM_INFO *item_s;
7715 INT i = 0, cmpv;
7716
7717 while (i < infoPtr->nItemCount)
7718 {
7719 hItem = DPA_GetPtr( infoPtr->hdpaItems, i);
7720 item_s = DPA_GetPtr(hItem, 0);
7721
7722 cmpv = textcmpWT(item_s->hdr.pszText, lpLVItem->pszText, isW);
7723 if (infoPtr->dwStyle & LVS_SORTDESCENDING) cmpv *= -1;
7724
7725 if (cmpv >= 0) break;
7726 i++;
7727 }
7728 nItem = i;
7729 }
7730 else
7731 nItem = min(lpLVItem->iItem, infoPtr->nItemCount);
7732
7733 TRACE("inserting at %d, sorted=%d, count=%d, iItem=%d\n", nItem, is_sorted, infoPtr->nItemCount, lpLVItem->iItem);
7734 nItem = DPA_InsertPtr( infoPtr->hdpaItems, nItem, hdpaSubItems );
7735 if (nItem == -1) goto fail;
7736 infoPtr->nItemCount++;
7737
7738 /* shift indices first so they don't get tangled */
7739 LISTVIEW_ShiftIndices(infoPtr, nItem, 1);
7740
7741 /* set the item attributes */
7742 if (lpLVItem->mask & (LVIF_GROUPID|LVIF_COLUMNS))
7743 {
7744 /* full size structure expected - _WIN32IE >= 0x560 */
7745 item = *lpLVItem;
7746 }
7747 else if (lpLVItem->mask & LVIF_INDENT)
7748 {
7749 /* indent member expected - _WIN32IE >= 0x300 */
7750 memcpy(&item, lpLVItem, offsetof( LVITEMW, iGroupId ));
7751 }
7752 else
7753 {
7754 /* minimal structure expected */
7755 memcpy(&item, lpLVItem, offsetof( LVITEMW, iIndent ));
7756 }
7757 item.iItem = nItem;
7758 if (infoPtr->dwLvExStyle & LVS_EX_CHECKBOXES)
7759 {
7760 item.mask |= LVIF_STATE;
7761 item.stateMask |= LVIS_STATEIMAGEMASK;
7762 item.state &= ~LVIS_STATEIMAGEMASK;
7763 item.state |= INDEXTOSTATEIMAGEMASK(1);
7764 }
7765
7766 if (!set_main_item(infoPtr, &item, TRUE, isW, &has_changed)) goto undo;
7767
7768 /* make room for the position, if we are in the right mode */
7769 if ((infoPtr->uView == LV_VIEW_SMALLICON) || (infoPtr->uView == LV_VIEW_ICON))
7770 {
7771 if (DPA_InsertPtr(infoPtr->hdpaPosX, nItem, 0) == -1)
7772 goto undo;
7773 if (DPA_InsertPtr(infoPtr->hdpaPosY, nItem, 0) == -1)
7774 {
7775 DPA_DeletePtr(infoPtr->hdpaPosX, nItem);
7776 goto undo;
7777 }
7778 }
7779
7780 /* send LVN_INSERTITEM notification */
7781 memset(&nmlv, 0, sizeof(NMLISTVIEW));
7782 nmlv.iItem = nItem;
7783 nmlv.lParam = lpItem->lParam;
7784 notify_listview(infoPtr, LVN_INSERTITEM, &nmlv);
7785 if (!IsWindow(hwndSelf))
7786 return -1;
7787
7788 /* align items (set position of each item) */
7789 if (infoPtr->uView == LV_VIEW_SMALLICON || infoPtr->uView == LV_VIEW_ICON)
7790 {
7791 POINT pt;
7792
7793 if (infoPtr->dwStyle & LVS_ALIGNLEFT)
7794 LISTVIEW_NextIconPosLeft(infoPtr, &pt);
7795 else
7796 LISTVIEW_NextIconPosTop(infoPtr, &pt);
7797
7798 LISTVIEW_MoveIconTo(infoPtr, nItem, &pt, TRUE);
7799 }
7800
7801 /* now is the invalidation fun */
7802 LISTVIEW_ScrollOnInsert(infoPtr, nItem, 1);
7803 return nItem;
7804
7805 undo:
7806 LISTVIEW_ShiftIndices(infoPtr, nItem, -1);
7807 LISTVIEW_ShiftFocus(infoPtr, infoPtr->nFocusedItem, nItem, -1);
7808 DPA_DeletePtr(infoPtr->hdpaItems, nItem);
7809 infoPtr->nItemCount--;
7810 fail:
7811 DPA_DeletePtr(hdpaSubItems, 0);
7812 DPA_Destroy (hdpaSubItems);
7813 Free (lpItem);
7814 return -1;
7815 }
7816
7817 /***
7818 * DESCRIPTION:
7819 * Checks item visibility.
7820 *
7821 * PARAMETER(S):
7822 * [I] infoPtr : valid pointer to the listview structure
7823 * [I] nFirst : item index to check for
7824 *
7825 * RETURN:
7826 * Item visible : TRUE
7827 * Item invisible or failure : FALSE
7828 */
7829 static BOOL LISTVIEW_IsItemVisible(const LISTVIEW_INFO *infoPtr, INT nItem)
7830 {
7831 POINT Origin, Position;
7832 RECT rcItem;
7833 HDC hdc;
7834 BOOL ret;
7835
7836 TRACE("nItem=%d\n", nItem);
7837
7838 if (nItem < 0 || nItem >= DPA_GetPtrCount(infoPtr->hdpaItems)) return FALSE;
7839
7840 LISTVIEW_GetOrigin(infoPtr, &Origin);
7841 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position);
7842 rcItem.left = Position.x + Origin.x;
7843 rcItem.top = Position.y + Origin.y;
7844 rcItem.right = rcItem.left + infoPtr->nItemWidth;
7845 rcItem.bottom = rcItem.top + infoPtr->nItemHeight;
7846
7847 hdc = GetDC(infoPtr->hwndSelf);
7848 if (!hdc) return FALSE;
7849 ret = RectVisible(hdc, &rcItem);
7850 ReleaseDC(infoPtr->hwndSelf, hdc);
7851
7852 return ret;
7853 }
7854
7855 /***
7856 * DESCRIPTION:
7857 * Redraws a range of items.
7858 *
7859 * PARAMETER(S):
7860 * [I] infoPtr : valid pointer to the listview structure
7861 * [I] nFirst : first item
7862 * [I] nLast : last item
7863 *
7864 * RETURN:
7865 * SUCCESS : TRUE
7866 * FAILURE : FALSE
7867 */
7868 static BOOL LISTVIEW_RedrawItems(const LISTVIEW_INFO *infoPtr, INT nFirst, INT nLast)
7869 {
7870 INT i;
7871
7872 if (nLast < nFirst || min(nFirst, nLast) < 0 ||
7873 max(nFirst, nLast) >= infoPtr->nItemCount)
7874 return FALSE;
7875
7876 for (i = nFirst; i <= nLast; i++)
7877 LISTVIEW_InvalidateItem(infoPtr, i);
7878
7879 return TRUE;
7880 }
7881
7882 /***
7883 * DESCRIPTION:
7884 * Scroll the content of a listview.
7885 *
7886 * PARAMETER(S):
7887 * [I] infoPtr : valid pointer to the listview structure
7888 * [I] dx : horizontal scroll amount in pixels
7889 * [I] dy : vertical scroll amount in pixels
7890 *
7891 * RETURN:
7892 * SUCCESS : TRUE
7893 * FAILURE : FALSE
7894 *
7895 * COMMENTS:
7896 * If the control is in report view (LV_VIEW_DETAILS) the control can
7897 * be scrolled only in line increments. "dy" will be rounded to the
7898 * nearest number of pixels that are a whole line. Ex: if line height
7899 * is 16 and an 8 is passed, the list will be scrolled by 16. If a 7
7900 * is passed, then the scroll will be 0. (per MSDN 7/2002)
7901 */
7902 static BOOL LISTVIEW_Scroll(LISTVIEW_INFO *infoPtr, INT dx, INT dy)
7903 {
7904 switch(infoPtr->uView) {
7905 case LV_VIEW_DETAILS:
7906 dy += (dy < 0 ? -1 : 1) * infoPtr->nItemHeight/2;
7907 dy /= infoPtr->nItemHeight;
7908 break;
7909 case LV_VIEW_LIST:
7910 if (dy != 0) return FALSE;
7911 break;
7912 default: /* icon */
7913 break;
7914 }
7915
7916 if (dx != 0) LISTVIEW_HScroll(infoPtr, SB_INTERNAL, dx);
7917 if (dy != 0) LISTVIEW_VScroll(infoPtr, SB_INTERNAL, dy);
7918
7919 return TRUE;
7920 }
7921
7922 /***
7923 * DESCRIPTION:
7924 * Sets the background color.
7925 *
7926 * PARAMETER(S):
7927 * [I] infoPtr : valid pointer to the listview structure
7928 * [I] color : background color
7929 *
7930 * RETURN:
7931 * SUCCESS : TRUE
7932 * FAILURE : FALSE
7933 */
7934 static BOOL LISTVIEW_SetBkColor(LISTVIEW_INFO *infoPtr, COLORREF color)
7935 {
7936 TRACE("(color=%x)\n", color);
7937
7938 infoPtr->bDefaultBkColor = FALSE;
7939 if(infoPtr->clrBk != color) {
7940 if (infoPtr->clrBk != CLR_NONE) DeleteObject(infoPtr->hBkBrush);
7941 infoPtr->clrBk = color;
7942 if (color == CLR_NONE)
7943 infoPtr->hBkBrush = (HBRUSH)GetClassLongPtrW(infoPtr->hwndSelf, GCLP_HBRBACKGROUND);
7944 else
7945 {
7946 infoPtr->hBkBrush = CreateSolidBrush(color);
7947 infoPtr->dwLvExStyle &= ~LVS_EX_TRANSPARENTBKGND;
7948 }
7949 }
7950
7951 return TRUE;
7952 }
7953
7954 /* LISTVIEW_SetBkImage */
7955
7956 /*** Helper for {Insert,Set}ColumnT *only* */
7957 static void column_fill_hditem(const LISTVIEW_INFO *infoPtr, HDITEMW *lphdi, INT nColumn,
7958 const LVCOLUMNW *lpColumn, BOOL isW)
7959 {
7960 if (lpColumn->mask & LVCF_FMT)
7961 {
7962 /* format member is valid */
7963 lphdi->mask |= HDI_FORMAT;
7964
7965 /* set text alignment (leftmost column must be left-aligned) */
7966 if (nColumn == 0 || (lpColumn->fmt & LVCFMT_JUSTIFYMASK) == LVCFMT_LEFT)
7967 lphdi->fmt |= HDF_LEFT;
7968 else if ((lpColumn->fmt & LVCFMT_JUSTIFYMASK) == LVCFMT_RIGHT)
7969 lphdi->fmt |= HDF_RIGHT;
7970 else if ((lpColumn->fmt & LVCFMT_JUSTIFYMASK) == LVCFMT_CENTER)
7971 lphdi->fmt |= HDF_CENTER;
7972
7973 if (lpColumn->fmt & LVCFMT_BITMAP_ON_RIGHT)
7974 lphdi->fmt |= HDF_BITMAP_ON_RIGHT;
7975
7976 if (lpColumn->fmt & LVCFMT_COL_HAS_IMAGES)
7977 {
7978 lphdi->fmt |= HDF_IMAGE;
7979 lphdi->iImage = I_IMAGECALLBACK;
7980 }
7981
7982 if (lpColumn->fmt & LVCFMT_FIXED_WIDTH)
7983 lphdi->fmt |= HDF_FIXEDWIDTH;
7984 }
7985
7986 if (lpColumn->mask & LVCF_WIDTH)
7987 {
7988 lphdi->mask |= HDI_WIDTH;
7989 if(lpColumn->cx == LVSCW_AUTOSIZE_USEHEADER)
7990 {
7991 /* make it fill the remainder of the controls width */
7992 RECT rcHeader;
7993 INT item_index;
7994
7995 for(item_index = 0; item_index < (nColumn - 1); item_index++)
7996 {
7997 LISTVIEW_GetHeaderRect(infoPtr, item_index, &rcHeader);
7998 lphdi->cxy += rcHeader.right - rcHeader.left;
7999 }
8000
8001 /* retrieve the layout of the header */
8002 GetClientRect(infoPtr->hwndSelf, &rcHeader);
8003 TRACE("start cxy=%d rcHeader=%s\n", lphdi->cxy, wine_dbgstr_rect(&rcHeader));
8004
8005 lphdi->cxy = (rcHeader.right - rcHeader.left) - lphdi->cxy;
8006 }
8007 else
8008 lphdi->cxy = lpColumn->cx;
8009 }
8010
8011 if (lpColumn->mask & LVCF_TEXT)
8012 {
8013 lphdi->mask |= HDI_TEXT | HDI_FORMAT;
8014 lphdi->fmt |= HDF_STRING;
8015 lphdi->pszText = lpColumn->pszText;
8016 lphdi->cchTextMax = textlenT(lpColumn->pszText, isW);
8017 }
8018
8019 if (lpColumn->mask & LVCF_IMAGE)
8020 {
8021 lphdi->mask |= HDI_IMAGE;
8022 lphdi->iImage = lpColumn->iImage;
8023 }
8024
8025 if (lpColumn->mask & LVCF_ORDER)
8026 {
8027 lphdi->mask |= HDI_ORDER;
8028 lphdi->iOrder = lpColumn->iOrder;
8029 }
8030 }
8031
8032
8033 /***
8034 * DESCRIPTION:
8035 * Inserts a new column.
8036 *
8037 * PARAMETER(S):
8038 * [I] infoPtr : valid pointer to the listview structure
8039 * [I] nColumn : column index
8040 * [I] lpColumn : column information
8041 * [I] isW : TRUE if lpColumn is Unicode, FALSE otherwise
8042 *
8043 * RETURN:
8044 * SUCCESS : new column index
8045 * FAILURE : -1
8046 */
8047 static INT LISTVIEW_InsertColumnT(LISTVIEW_INFO *infoPtr, INT nColumn,
8048 const LVCOLUMNW *lpColumn, BOOL isW)
8049 {
8050 COLUMN_INFO *lpColumnInfo;
8051 INT nNewColumn;
8052 HDITEMW hdi;
8053
8054 TRACE("(nColumn=%d, lpColumn=%s, isW=%d)\n", nColumn, debuglvcolumn_t(lpColumn, isW), isW);
8055
8056 if (!lpColumn || nColumn < 0) return -1;
8057 nColumn = min(nColumn, DPA_GetPtrCount(infoPtr->hdpaColumns));
8058
8059 ZeroMemory(&hdi, sizeof(HDITEMW));
8060 column_fill_hditem(infoPtr, &hdi, nColumn, lpColumn, isW);
8061
8062 /*
8063 * A mask not including LVCF_WIDTH turns into a mask of width, width 10
8064 * (can be seen in SPY) otherwise column never gets added.
8065 */
8066 if (!(lpColumn->mask & LVCF_WIDTH)) {
8067 hdi.mask |= HDI_WIDTH;
8068 hdi.cxy = 10;
8069 }
8070
8071 /*
8072 * when the iSubItem is available Windows copies it to the header lParam. It seems
8073 * to happen only in LVM_INSERTCOLUMN - not in LVM_SETCOLUMN
8074 */
8075 if (lpColumn->mask & LVCF_SUBITEM)
8076 {
8077 hdi.mask |= HDI_LPARAM;
8078 hdi.lParam = lpColumn->iSubItem;
8079 }
8080
8081 /* create header if not present */
8082 LISTVIEW_CreateHeader(infoPtr);
8083 if (!(LVS_NOCOLUMNHEADER & infoPtr->dwStyle) &&
8084 (infoPtr->uView == LV_VIEW_DETAILS) && (WS_VISIBLE & infoPtr->dwStyle))
8085 {
8086 ShowWindow(infoPtr->hwndHeader, SW_SHOWNORMAL);
8087 }
8088
8089 /* insert item in header control */
8090 nNewColumn = SendMessageW(infoPtr->hwndHeader,
8091 isW ? HDM_INSERTITEMW : HDM_INSERTITEMA,
8092 nColumn, (LPARAM)&hdi);
8093 if (nNewColumn == -1) return -1;
8094 if (nNewColumn != nColumn) ERR("nColumn=%d, nNewColumn=%d\n", nColumn, nNewColumn);
8095
8096 /* create our own column info */
8097 if (!(lpColumnInfo = Alloc(sizeof(COLUMN_INFO)))) goto fail;
8098 if (DPA_InsertPtr(infoPtr->hdpaColumns, nNewColumn, lpColumnInfo) == -1) goto fail;
8099
8100 if (lpColumn->mask & LVCF_FMT) lpColumnInfo->fmt = lpColumn->fmt;
8101 if (lpColumn->mask & LVCF_MINWIDTH) lpColumnInfo->cxMin = lpColumn->cxMin;
8102 if (!SendMessageW(infoPtr->hwndHeader, HDM_GETITEMRECT, nNewColumn, (LPARAM)&lpColumnInfo->rcHeader))
8103 goto fail;
8104
8105 /* now we have to actually adjust the data */
8106 if (!(infoPtr->dwStyle & LVS_OWNERDATA) && infoPtr->nItemCount > 0)
8107 {
8108 SUBITEM_INFO *lpSubItem;
8109 HDPA hdpaSubItems;
8110 INT nItem, i;
8111 LVITEMW item;
8112 BOOL changed;
8113
8114 item.iSubItem = nNewColumn;
8115 item.mask = LVIF_TEXT | LVIF_IMAGE;
8116 item.iImage = I_IMAGECALLBACK;
8117 item.pszText = LPSTR_TEXTCALLBACKW;
8118
8119 for (nItem = 0; nItem < infoPtr->nItemCount; nItem++)
8120 {
8121 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, nItem);
8122 for (i = 1; i < DPA_GetPtrCount(hdpaSubItems); i++)
8123 {
8124 lpSubItem = DPA_GetPtr(hdpaSubItems, i);
8125 if (lpSubItem->iSubItem >= nNewColumn)
8126 lpSubItem->iSubItem++;
8127 }
8128
8129 /* add new subitem for each item */
8130 item.iItem = nItem;
8131 set_sub_item(infoPtr, &item, isW, &changed);
8132 }
8133 }
8134
8135 /* make space for the new column */
8136 LISTVIEW_ScrollColumns(infoPtr, nNewColumn + 1, lpColumnInfo->rcHeader.right - lpColumnInfo->rcHeader.left);
8137 LISTVIEW_UpdateItemSize(infoPtr);
8138
8139 return nNewColumn;
8140
8141 fail:
8142 if (nNewColumn != -1) SendMessageW(infoPtr->hwndHeader, HDM_DELETEITEM, nNewColumn, 0);
8143 if (lpColumnInfo)
8144 {
8145 DPA_DeletePtr(infoPtr->hdpaColumns, nNewColumn);
8146 Free(lpColumnInfo);
8147 }
8148 return -1;
8149 }
8150
8151 /***
8152 * DESCRIPTION:
8153 * Sets the attributes of a header item.
8154 *
8155 * PARAMETER(S):
8156 * [I] infoPtr : valid pointer to the listview structure
8157 * [I] nColumn : column index
8158 * [I] lpColumn : column attributes
8159 * [I] isW: if TRUE, then lpColumn is a LPLVCOLUMNW, else it is a LPLVCOLUMNA
8160 *
8161 * RETURN:
8162 * SUCCESS : TRUE
8163 * FAILURE : FALSE
8164 */
8165 static BOOL LISTVIEW_SetColumnT(const LISTVIEW_INFO *infoPtr, INT nColumn,
8166 const LVCOLUMNW *lpColumn, BOOL isW)
8167 {
8168 HDITEMW hdi, hdiget;
8169 BOOL bResult;
8170
8171 TRACE("(nColumn=%d, lpColumn=%s, isW=%d)\n", nColumn, debuglvcolumn_t(lpColumn, isW), isW);
8172
8173 if (!lpColumn || nColumn < 0 || nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return FALSE;
8174
8175 ZeroMemory(&hdi, sizeof(HDITEMW));
8176 if (lpColumn->mask & LVCF_FMT)
8177 {
8178 hdi.mask |= HDI_FORMAT;
8179 hdiget.mask = HDI_FORMAT;
8180 if (SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, nColumn, (LPARAM)&hdiget))
8181 hdi.fmt = hdiget.fmt & HDF_STRING;
8182 }
8183 column_fill_hditem(infoPtr, &hdi, nColumn, lpColumn, isW);
8184
8185 /* set header item attributes */
8186 bResult = SendMessageW(infoPtr->hwndHeader, isW ? HDM_SETITEMW : HDM_SETITEMA, nColumn, (LPARAM)&hdi);
8187 if (!bResult) return FALSE;
8188
8189 if (lpColumn->mask & LVCF_FMT)
8190 {
8191 COLUMN_INFO *lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, nColumn);
8192 INT oldFmt = lpColumnInfo->fmt;
8193
8194 lpColumnInfo->fmt = lpColumn->fmt;
8195 if ((oldFmt ^ lpColumn->fmt) & (LVCFMT_JUSTIFYMASK | LVCFMT_IMAGE))
8196 {
8197 if (infoPtr->uView == LV_VIEW_DETAILS) LISTVIEW_InvalidateColumn(infoPtr, nColumn);
8198 }
8199 }
8200
8201 if (lpColumn->mask & LVCF_MINWIDTH)
8202 LISTVIEW_GetColumnInfo(infoPtr, nColumn)->cxMin = lpColumn->cxMin;
8203
8204 return TRUE;
8205 }
8206
8207 /***
8208 * DESCRIPTION:
8209 * Sets the column order array
8210 *
8211 * PARAMETERS:
8212 * [I] infoPtr : valid pointer to the listview structure
8213 * [I] iCount : number of elements in column order array
8214 * [I] lpiArray : pointer to column order array
8215 *
8216 * RETURN:
8217 * SUCCESS : TRUE
8218 * FAILURE : FALSE
8219 */
8220 static BOOL LISTVIEW_SetColumnOrderArray(LISTVIEW_INFO *infoPtr, INT iCount, const INT *lpiArray)
8221 {
8222 TRACE("iCount %d lpiArray %p\n", iCount, lpiArray);
8223
8224 if (!lpiArray || !IsWindow(infoPtr->hwndHeader)) return FALSE;
8225
8226 infoPtr->colRectsDirty = TRUE;
8227
8228 return SendMessageW(infoPtr->hwndHeader, HDM_SETORDERARRAY, iCount, (LPARAM)lpiArray);
8229 }
8230
8231 /***
8232 * DESCRIPTION:
8233 * Sets the width of a column
8234 *
8235 * PARAMETERS:
8236 * [I] infoPtr : valid pointer to the listview structure
8237 * [I] nColumn : column index
8238 * [I] cx : column width
8239 *
8240 * RETURN:
8241 * SUCCESS : TRUE
8242 * FAILURE : FALSE
8243 */
8244 static BOOL LISTVIEW_SetColumnWidth(LISTVIEW_INFO *infoPtr, INT nColumn, INT cx)
8245 {
8246 WCHAR szDispText[DISP_TEXT_SIZE] = { 0 };
8247 INT max_cx = 0;
8248 HDITEMW hdi;
8249
8250 TRACE("(nColumn=%d, cx=%d\n", nColumn, cx);
8251
8252 /* set column width only if in report or list mode */
8253 if (infoPtr->uView != LV_VIEW_DETAILS && infoPtr->uView != LV_VIEW_LIST) return FALSE;
8254
8255 /* take care of invalid cx values */
8256 if(infoPtr->uView == LV_VIEW_DETAILS && cx < -2) cx = LVSCW_AUTOSIZE;
8257 else if (infoPtr->uView == LV_VIEW_LIST && cx < 1) return FALSE;
8258
8259 /* resize all columns if in LV_VIEW_LIST mode */
8260 if(infoPtr->uView == LV_VIEW_LIST)
8261 {
8262 infoPtr->nItemWidth = cx;
8263 LISTVIEW_InvalidateList(infoPtr);
8264 return TRUE;
8265 }
8266
8267 if (nColumn < 0 || nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return FALSE;
8268
8269 if (cx == LVSCW_AUTOSIZE || (cx == LVSCW_AUTOSIZE_USEHEADER && nColumn < DPA_GetPtrCount(infoPtr->hdpaColumns) -1))
8270 {
8271 INT nLabelWidth;
8272 LVITEMW lvItem;
8273
8274 lvItem.mask = LVIF_TEXT;
8275 lvItem.iItem = 0;
8276 lvItem.iSubItem = nColumn;
8277 lvItem.cchTextMax = DISP_TEXT_SIZE;
8278 for (; lvItem.iItem < infoPtr->nItemCount; lvItem.iItem++)
8279 {
8280 lvItem.pszText = szDispText;
8281 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) continue;
8282 nLabelWidth = LISTVIEW_GetStringWidthT(infoPtr, lvItem.pszText, TRUE);
8283 if (max_cx < nLabelWidth) max_cx = nLabelWidth;
8284 }
8285 if (infoPtr->himlSmall && (nColumn == 0 || (LISTVIEW_GetColumnInfo(infoPtr, nColumn)->fmt & LVCFMT_IMAGE)))
8286 max_cx += infoPtr->iconSize.cx;
8287 max_cx += TRAILING_LABEL_PADDING;
8288 }
8289
8290 /* autosize based on listview items width */
8291 if(cx == LVSCW_AUTOSIZE)
8292 cx = max_cx;
8293 else if(cx == LVSCW_AUTOSIZE_USEHEADER)
8294 {
8295 /* if iCol is the last column make it fill the remainder of the controls width */
8296 if(nColumn == DPA_GetPtrCount(infoPtr->hdpaColumns) - 1)
8297 {
8298 RECT rcHeader;
8299 POINT Origin;
8300
8301 LISTVIEW_GetOrigin(infoPtr, &Origin);
8302 LISTVIEW_GetHeaderRect(infoPtr, nColumn, &rcHeader);
8303
8304 cx = infoPtr->rcList.right - Origin.x - rcHeader.left;
8305 }
8306 else
8307 {
8308 /* Despite what the MS docs say, if this is not the last
8309 column, then MS resizes the column to the width of the
8310 largest text string in the column, including headers
8311 and items. This is different from LVSCW_AUTOSIZE in that
8312 LVSCW_AUTOSIZE ignores the header string length. */
8313 cx = 0;
8314
8315 /* retrieve header text */
8316 hdi.mask = HDI_TEXT;
8317 hdi.cchTextMax = DISP_TEXT_SIZE;
8318 hdi.pszText = szDispText;
8319 if (SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, nColumn, (LPARAM)&hdi))
8320 {
8321 HDC hdc = GetDC(infoPtr->hwndSelf);
8322 HFONT old_font = SelectObject(hdc, (HFONT)SendMessageW(infoPtr->hwndHeader, WM_GETFONT, 0, 0));
8323 SIZE size;
8324
8325 if (GetTextExtentPoint32W(hdc, hdi.pszText, lstrlenW(hdi.pszText), &size))
8326 cx = size.cx + TRAILING_HEADER_PADDING;
8327 /* FIXME: Take into account the header image, if one is present */
8328 SelectObject(hdc, old_font);
8329 ReleaseDC(infoPtr->hwndSelf, hdc);
8330 }
8331 cx = max (cx, max_cx);
8332 }
8333 }
8334
8335 if (cx < 0) return FALSE;
8336
8337 /* call header to update the column change */
8338 hdi.mask = HDI_WIDTH;
8339 hdi.cxy = max(cx, LISTVIEW_GetColumnInfo(infoPtr, nColumn)->cxMin);
8340 TRACE("hdi.cxy=%d\n", hdi.cxy);
8341 return SendMessageW(infoPtr->hwndHeader, HDM_SETITEMW, nColumn, (LPARAM)&hdi);
8342 }
8343
8344 /***
8345 * Creates the checkbox imagelist. Helper for LISTVIEW_SetExtendedListViewStyle
8346 *
8347 */
8348 static HIMAGELIST LISTVIEW_CreateCheckBoxIL(const LISTVIEW_INFO *infoPtr)
8349 {
8350 HDC hdc_wnd, hdc;
8351 HBITMAP hbm_im, hbm_mask, hbm_orig;
8352 RECT rc;
8353 HBRUSH hbr_white = GetStockObject(WHITE_BRUSH);
8354 HBRUSH hbr_black = GetStockObject(BLACK_BRUSH);
8355 HIMAGELIST himl;
8356
8357 himl = ImageList_Create(GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON),
8358 ILC_COLOR | ILC_MASK, 2, 2);
8359 hdc_wnd = GetDC(infoPtr->hwndSelf);
8360 hdc = CreateCompatibleDC(hdc_wnd);
8361 hbm_im = CreateCompatibleBitmap(hdc_wnd, GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON));
8362 hbm_mask = CreateBitmap(GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), 1, 1, NULL);
8363 ReleaseDC(infoPtr->hwndSelf, hdc_wnd);
8364
8365 rc.left = rc.top = 0;
8366 rc.right = GetSystemMetrics(SM_CXSMICON);
8367 rc.bottom = GetSystemMetrics(SM_CYSMICON);
8368
8369 hbm_orig = SelectObject(hdc, hbm_mask);
8370 FillRect(hdc, &rc, hbr_white);
8371 InflateRect(&rc, -2, -2);
8372 FillRect(hdc, &rc, hbr_black);
8373
8374 SelectObject(hdc, hbm_im);
8375 DrawFrameControl(hdc, &rc, DFC_BUTTON, DFCS_BUTTONCHECK | DFCS_MONO);
8376 SelectObject(hdc, hbm_orig);
8377 ImageList_Add(himl, hbm_im, hbm_mask);
8378
8379 SelectObject(hdc, hbm_im);
8380 DrawFrameControl(hdc, &rc, DFC_BUTTON, DFCS_BUTTONCHECK | DFCS_MONO | DFCS_CHECKED);
8381 SelectObject(hdc, hbm_orig);
8382 ImageList_Add(himl, hbm_im, hbm_mask);
8383
8384 DeleteObject(hbm_mask);
8385 DeleteObject(hbm_im);
8386 DeleteDC(hdc);
8387
8388 return himl;
8389 }
8390
8391 /***
8392 * DESCRIPTION:
8393 * Sets the extended listview style.
8394 *
8395 * PARAMETERS:
8396 * [I] infoPtr : valid pointer to the listview structure
8397 * [I] dwMask : mask
8398 * [I] dwStyle : style
8399 *
8400 * RETURN:
8401 * SUCCESS : previous style
8402 * FAILURE : 0
8403 */
8404 static DWORD LISTVIEW_SetExtendedListViewStyle(LISTVIEW_INFO *infoPtr, DWORD mask, DWORD ex_style)
8405 {
8406 DWORD old_ex_style = infoPtr->dwLvExStyle;
8407
8408 TRACE("mask=0x%08x, ex_style=0x%08x\n", mask, ex_style);
8409
8410 /* set new style */
8411 if (mask)
8412 infoPtr->dwLvExStyle = (old_ex_style & ~mask) | (ex_style & mask);
8413 else
8414 infoPtr->dwLvExStyle = ex_style;
8415
8416 if((infoPtr->dwLvExStyle ^ old_ex_style) & LVS_EX_CHECKBOXES)
8417 {
8418 HIMAGELIST himl = 0;
8419 if(infoPtr->dwLvExStyle & LVS_EX_CHECKBOXES)
8420 {
8421 LVITEMW item;
8422 item.mask = LVIF_STATE;
8423 item.stateMask = LVIS_STATEIMAGEMASK;
8424 item.state = INDEXTOSTATEIMAGEMASK(1);
8425 LISTVIEW_SetItemState(infoPtr, -1, &item);
8426
8427 himl = LISTVIEW_CreateCheckBoxIL(infoPtr);
8428 if(!(infoPtr->dwStyle & LVS_SHAREIMAGELISTS))
8429 ImageList_Destroy(infoPtr->himlState);
8430 }
8431 himl = LISTVIEW_SetImageList(infoPtr, LVSIL_STATE, himl);
8432 /* checkbox list replaces previous custom list or... */
8433 if(((infoPtr->dwLvExStyle & LVS_EX_CHECKBOXES) &&
8434 !(infoPtr->dwStyle & LVS_SHAREIMAGELISTS)) ||
8435 /* ...previous was checkbox list */
8436 (old_ex_style & LVS_EX_CHECKBOXES))
8437 ImageList_Destroy(himl);
8438 }
8439
8440 if((infoPtr->dwLvExStyle ^ old_ex_style) & LVS_EX_HEADERDRAGDROP)
8441 {
8442 DWORD style;
8443
8444 /* if not already created */
8445 LISTVIEW_CreateHeader(infoPtr);
8446
8447 style = GetWindowLongW(infoPtr->hwndHeader, GWL_STYLE);
8448 if (infoPtr->dwLvExStyle & LVS_EX_HEADERDRAGDROP)
8449 style |= HDS_DRAGDROP;
8450 else
8451 style &= ~HDS_DRAGDROP;
8452 SetWindowLongW(infoPtr->hwndHeader, GWL_STYLE, style);
8453 }
8454
8455 /* GRIDLINES adds decoration at top so changes sizes */
8456 if((infoPtr->dwLvExStyle ^ old_ex_style) & LVS_EX_GRIDLINES)
8457 {
8458 LISTVIEW_CreateHeader(infoPtr);
8459 LISTVIEW_UpdateSize(infoPtr);
8460 }
8461
8462 if((infoPtr->dwLvExStyle ^ old_ex_style) & LVS_EX_FULLROWSELECT)
8463 {
8464 LISTVIEW_CreateHeader(infoPtr);
8465 }
8466
8467 if((infoPtr->dwLvExStyle ^ old_ex_style) & LVS_EX_TRANSPARENTBKGND)
8468 {
8469 if (infoPtr->dwLvExStyle & LVS_EX_TRANSPARENTBKGND)
8470 LISTVIEW_SetBkColor(infoPtr, CLR_NONE);
8471 }
8472
8473 if((infoPtr->dwLvExStyle ^ old_ex_style) & LVS_EX_HEADERINALLVIEWS)
8474 {
8475 if (infoPtr->dwLvExStyle & LVS_EX_HEADERINALLVIEWS)
8476 LISTVIEW_CreateHeader(infoPtr);
8477 else
8478 ShowWindow(infoPtr->hwndHeader, SW_HIDE);
8479 LISTVIEW_UpdateSize(infoPtr);
8480 LISTVIEW_UpdateScroll(infoPtr);
8481 }
8482
8483 LISTVIEW_InvalidateList(infoPtr);
8484 return old_ex_style;
8485 }
8486
8487 /***
8488 * DESCRIPTION:
8489 * Sets the new hot cursor used during hot tracking and hover selection.
8490 *
8491 * PARAMETER(S):
8492 * [I] infoPtr : valid pointer to the listview structure
8493 * [I] hCursor : the new hot cursor handle
8494 *
8495 * RETURN:
8496 * Returns the previous hot cursor
8497 */
8498 static HCURSOR LISTVIEW_SetHotCursor(LISTVIEW_INFO *infoPtr, HCURSOR hCursor)
8499 {
8500 HCURSOR oldCursor = infoPtr->hHotCursor;
8501
8502 infoPtr->hHotCursor = hCursor;
8503
8504 return oldCursor;
8505 }
8506
8507
8508 /***
8509 * DESCRIPTION:
8510 * Sets the hot item index.
8511 *
8512 * PARAMETERS:
8513 * [I] infoPtr : valid pointer to the listview structure
8514 * [I] iIndex : index
8515 *
8516 * RETURN:
8517 * SUCCESS : previous hot item index
8518 * FAILURE : -1 (no hot item)
8519 */
8520 static INT LISTVIEW_SetHotItem(LISTVIEW_INFO *infoPtr, INT iIndex)
8521 {
8522 INT iOldIndex = infoPtr->nHotItem;
8523
8524 infoPtr->nHotItem = iIndex;
8525
8526 return iOldIndex;
8527 }
8528
8529
8530 /***
8531 * DESCRIPTION:
8532 * Sets the amount of time the cursor must hover over an item before it is selected.
8533 *
8534 * PARAMETER(S):
8535 * [I] infoPtr : valid pointer to the listview structure
8536 * [I] dwHoverTime : hover time, if -1 the hover time is set to the default
8537 *
8538 * RETURN:
8539 * Returns the previous hover time
8540 */
8541 static DWORD LISTVIEW_SetHoverTime(LISTVIEW_INFO *infoPtr, DWORD dwHoverTime)
8542 {
8543 DWORD oldHoverTime = infoPtr->dwHoverTime;
8544
8545 infoPtr->dwHoverTime = dwHoverTime;
8546
8547 return oldHoverTime;
8548 }
8549
8550 /***
8551 * DESCRIPTION:
8552 * Sets spacing for icons of LVS_ICON style.
8553 *
8554 * PARAMETER(S):
8555 * [I] infoPtr : valid pointer to the listview structure
8556 * [I] cx : horizontal spacing (-1 = system spacing, 0 = autosize)
8557 * [I] cy : vertical spacing (-1 = system spacing, 0 = autosize)
8558 *
8559 * RETURN:
8560 * MAKELONG(oldcx, oldcy)
8561 */
8562 static DWORD LISTVIEW_SetIconSpacing(LISTVIEW_INFO *infoPtr, INT cx, INT cy)
8563 {
8564 INT iconWidth = 0, iconHeight = 0;
8565 DWORD oldspacing = MAKELONG(infoPtr->iconSpacing.cx, infoPtr->iconSpacing.cy);
8566
8567 TRACE("requested=(%d,%d)\n", cx, cy);
8568
8569 /* set to defaults, if instructed to */
8570 if (cx == -1 && cy == -1)
8571 {
8572 infoPtr->autoSpacing = TRUE;
8573 if (infoPtr->himlNormal)
8574 ImageList_GetIconSize(infoPtr->himlNormal, &iconWidth, &iconHeight);
8575 cx = GetSystemMetrics(SM_CXICONSPACING) - GetSystemMetrics(SM_CXICON) + iconWidth;
8576 cy = GetSystemMetrics(SM_CYICONSPACING) - GetSystemMetrics(SM_CYICON) + iconHeight;
8577 }
8578 else
8579 infoPtr->autoSpacing = FALSE;
8580
8581 /* if 0 then keep width */
8582 if (cx != 0)
8583 infoPtr->iconSpacing.cx = cx;
8584
8585 /* if 0 then keep height */
8586 if (cy != 0)
8587 infoPtr->iconSpacing.cy = cy;
8588
8589 TRACE("old=(%d,%d), new=(%d,%d), iconSize=(%d,%d), ntmH=%d\n",
8590 LOWORD(oldspacing), HIWORD(oldspacing), infoPtr->iconSpacing.cx, infoPtr->iconSpacing.cy,
8591 infoPtr->iconSize.cx, infoPtr->iconSize.cy,
8592 infoPtr->ntmHeight);
8593
8594 /* these depend on the iconSpacing */
8595 LISTVIEW_UpdateItemSize(infoPtr);
8596
8597 return oldspacing;
8598 }
8599
8600 static inline void set_icon_size(SIZE *size, HIMAGELIST himl, BOOL small)
8601 {
8602 INT cx, cy;
8603
8604 if (himl && ImageList_GetIconSize(himl, &cx, &cy))
8605 {
8606 size->cx = cx;
8607 size->cy = cy;
8608 }
8609 else
8610 {
8611 size->cx = GetSystemMetrics(small ? SM_CXSMICON : SM_CXICON);
8612 size->cy = GetSystemMetrics(small ? SM_CYSMICON : SM_CYICON);
8613 }
8614 }
8615
8616 /***
8617 * DESCRIPTION:
8618 * Sets image lists.
8619 *
8620 * PARAMETER(S):
8621 * [I] infoPtr : valid pointer to the listview structure
8622 * [I] nType : image list type
8623 * [I] himl : image list handle
8624 *
8625 * RETURN:
8626 * SUCCESS : old image list
8627 * FAILURE : NULL
8628 */
8629 static HIMAGELIST LISTVIEW_SetImageList(LISTVIEW_INFO *infoPtr, INT nType, HIMAGELIST himl)
8630 {
8631 INT oldHeight = infoPtr->nItemHeight;
8632 HIMAGELIST himlOld = 0;
8633
8634 TRACE("(nType=%d, himl=%p\n", nType, himl);
8635
8636 switch (nType)
8637 {
8638 case LVSIL_NORMAL:
8639 himlOld = infoPtr->himlNormal;
8640 infoPtr->himlNormal = himl;
8641 if (infoPtr->uView == LV_VIEW_ICON) set_icon_size(&infoPtr->iconSize, himl, FALSE);
8642 if (infoPtr->autoSpacing)
8643 LISTVIEW_SetIconSpacing(infoPtr, -1, -1);
8644 break;
8645
8646 case LVSIL_SMALL:
8647 himlOld = infoPtr->himlSmall;
8648 infoPtr->himlSmall = himl;
8649 if (infoPtr->uView != LV_VIEW_ICON) set_icon_size(&infoPtr->iconSize, himl, TRUE);
8650 if (infoPtr->hwndHeader)
8651 SendMessageW(infoPtr->hwndHeader, HDM_SETIMAGELIST, 0, (LPARAM)himl);
8652 break;
8653
8654 case LVSIL_STATE:
8655 himlOld = infoPtr->himlState;
8656 infoPtr->himlState = himl;
8657 set_icon_size(&infoPtr->iconStateSize, himl, TRUE);
8658 ImageList_SetBkColor(infoPtr->himlState, CLR_NONE);
8659 break;
8660
8661 default:
8662 ERR("Unknown icon type=%d\n", nType);
8663 return NULL;
8664 }
8665
8666 infoPtr->nItemHeight = LISTVIEW_CalculateItemHeight(infoPtr);
8667 if (infoPtr->nItemHeight != oldHeight)
8668 LISTVIEW_UpdateScroll(infoPtr);
8669
8670 return himlOld;
8671 }
8672
8673 /***
8674 * DESCRIPTION:
8675 * Preallocates memory (does *not* set the actual count of items !)
8676 *
8677 * PARAMETER(S):
8678 * [I] infoPtr : valid pointer to the listview structure
8679 * [I] nItems : item count (projected number of items to allocate)
8680 * [I] dwFlags : update flags
8681 *
8682 * RETURN:
8683 * SUCCESS : TRUE
8684 * FAILURE : FALSE
8685 */
8686 static BOOL LISTVIEW_SetItemCount(LISTVIEW_INFO *infoPtr, INT nItems, DWORD dwFlags)
8687 {
8688 TRACE("(nItems=%d, dwFlags=%x)\n", nItems, dwFlags);
8689
8690 if (infoPtr->dwStyle & LVS_OWNERDATA)
8691 {
8692 INT nOldCount = infoPtr->nItemCount;
8693
8694 if (nItems < nOldCount)
8695 {
8696 RANGE range = { nItems, nOldCount };
8697 ranges_del(infoPtr->selectionRanges, range);
8698 if (infoPtr->nFocusedItem >= nItems)
8699 {
8700 LISTVIEW_SetItemFocus(infoPtr, -1);
8701 SetRectEmpty(&infoPtr->rcFocus);
8702 }
8703 }
8704
8705 infoPtr->nItemCount = nItems;
8706 LISTVIEW_UpdateScroll(infoPtr);
8707
8708 /* the flags are valid only in ownerdata report and list modes */
8709 if (infoPtr->uView == LV_VIEW_ICON || infoPtr->uView == LV_VIEW_SMALLICON) dwFlags = 0;
8710
8711 if (!(dwFlags & LVSICF_NOSCROLL) && infoPtr->nFocusedItem != -1)
8712 LISTVIEW_EnsureVisible(infoPtr, infoPtr->nFocusedItem, FALSE);
8713
8714 if (!(dwFlags & LVSICF_NOINVALIDATEALL))
8715 LISTVIEW_InvalidateList(infoPtr);
8716 else
8717 {
8718 INT nFrom, nTo;
8719 POINT Origin;
8720 RECT rcErase;
8721
8722 LISTVIEW_GetOrigin(infoPtr, &Origin);
8723 nFrom = min(nOldCount, nItems);
8724 nTo = max(nOldCount, nItems);
8725
8726 if (infoPtr->uView == LV_VIEW_DETAILS)
8727 {
8728 rcErase.left = 0;
8729 rcErase.top = nFrom * infoPtr->nItemHeight;
8730 rcErase.right = infoPtr->nItemWidth;
8731 rcErase.bottom = nTo * infoPtr->nItemHeight;
8732 OffsetRect(&rcErase, Origin.x, Origin.y);
8733 if (IntersectRect(&rcErase, &rcErase, &infoPtr->rcList))
8734 LISTVIEW_InvalidateRect(infoPtr, &rcErase);
8735 }
8736 else /* LV_VIEW_LIST */
8737 {
8738 INT nPerCol = LISTVIEW_GetCountPerColumn(infoPtr);
8739
8740 rcErase.left = (nFrom / nPerCol) * infoPtr->nItemWidth;
8741 rcErase.top = (nFrom % nPerCol) * infoPtr->nItemHeight;
8742 rcErase.right = rcErase.left + infoPtr->nItemWidth;
8743 rcErase.bottom = nPerCol * infoPtr->nItemHeight;
8744 OffsetRect(&rcErase, Origin.x, Origin.y);
8745 if (IntersectRect(&rcErase, &rcErase, &infoPtr->rcList))
8746 LISTVIEW_InvalidateRect(infoPtr, &rcErase);
8747
8748 rcErase.left = (nFrom / nPerCol + 1) * infoPtr->nItemWidth;
8749 rcErase.top = 0;
8750 rcErase.right = (nTo / nPerCol + 1) * infoPtr->nItemWidth;
8751 rcErase.bottom = nPerCol * infoPtr->nItemHeight;
8752 OffsetRect(&rcErase, Origin.x, Origin.y);
8753 if (IntersectRect(&rcErase, &rcErase, &infoPtr->rcList))
8754 LISTVIEW_InvalidateRect(infoPtr, &rcErase);
8755 }
8756 }
8757 }
8758 else
8759 {
8760 /* According to MSDN for non-LVS_OWNERDATA this is just
8761 * a performance issue. The control allocates its internal
8762 * data structures for the number of items specified. It
8763 * cuts down on the number of memory allocations. Therefore
8764 * we will just issue a WARN here
8765 */
8766 WARN("for non-ownerdata performance option not implemented.\n");
8767 }
8768
8769 return TRUE;
8770 }
8771
8772 /***
8773 * DESCRIPTION:
8774 * Sets the position of an item.
8775 *
8776 * PARAMETER(S):
8777 * [I] infoPtr : valid pointer to the listview structure
8778 * [I] nItem : item index
8779 * [I] pt : coordinate
8780 *
8781 * RETURN:
8782 * SUCCESS : TRUE
8783 * FAILURE : FALSE
8784 */
8785 static BOOL LISTVIEW_SetItemPosition(LISTVIEW_INFO *infoPtr, INT nItem, const POINT *pt)
8786 {
8787 POINT Origin, Pt;
8788
8789 TRACE("(nItem=%d, pt=%s\n", nItem, wine_dbgstr_point(pt));
8790
8791 if (!pt || nItem < 0 || nItem >= infoPtr->nItemCount ||
8792 !(infoPtr->uView == LV_VIEW_ICON || infoPtr->uView == LV_VIEW_SMALLICON)) return FALSE;
8793
8794 Pt = *pt;
8795 LISTVIEW_GetOrigin(infoPtr, &Origin);
8796
8797 /* This point value seems to be an undocumented feature.
8798 * The best guess is that it means either at the origin,
8799 * or at true beginning of the list. I will assume the origin. */
8800 if ((Pt.x == -1) && (Pt.y == -1))
8801 Pt = Origin;
8802
8803 if (infoPtr->uView == LV_VIEW_ICON)
8804 {
8805 Pt.x -= (infoPtr->nItemWidth - infoPtr->iconSize.cx) / 2;
8806 Pt.y -= ICON_TOP_PADDING;
8807 }
8808 Pt.x -= Origin.x;
8809 Pt.y -= Origin.y;
8810
8811 infoPtr->bAutoarrange = FALSE;
8812
8813 return LISTVIEW_MoveIconTo(infoPtr, nItem, &Pt, FALSE);
8814 }
8815
8816 /***
8817 * DESCRIPTION:
8818 * Sets the state of one or many items.
8819 *
8820 * PARAMETER(S):
8821 * [I] infoPtr : valid pointer to the listview structure
8822 * [I] nItem : item index
8823 * [I] item : item or subitem info
8824 *
8825 * RETURN:
8826 * SUCCESS : TRUE
8827 * FAILURE : FALSE
8828 */
8829 static BOOL LISTVIEW_SetItemState(LISTVIEW_INFO *infoPtr, INT nItem, const LVITEMW *item)
8830 {
8831 BOOL ret = TRUE;
8832 LVITEMW lvItem;
8833
8834 if (!item) return FALSE;
8835
8836 lvItem.iItem = nItem;
8837 lvItem.iSubItem = 0;
8838 lvItem.mask = LVIF_STATE;
8839 lvItem.state = item->state;
8840 lvItem.stateMask = item->stateMask;
8841 TRACE("item=%s\n", debuglvitem_t(&lvItem, TRUE));
8842
8843 if (nItem == -1)
8844 {
8845 UINT oldstate = 0;
8846 BOOL notify;
8847
8848 /* special case optimization for recurring attempt to deselect all */
8849 if (lvItem.state == 0 && lvItem.stateMask == LVIS_SELECTED && !LISTVIEW_GetSelectedCount(infoPtr))
8850 return TRUE;
8851
8852 /* select all isn't allowed in LVS_SINGLESEL */
8853 if ((lvItem.state & lvItem.stateMask & LVIS_SELECTED) && (infoPtr->dwStyle & LVS_SINGLESEL))
8854 return FALSE;
8855
8856 /* focus all isn't allowed */
8857 if (lvItem.state & lvItem.stateMask & LVIS_FOCUSED) return FALSE;
8858
8859 notify = infoPtr->bDoChangeNotify;
8860 if (infoPtr->dwStyle & LVS_OWNERDATA)
8861 {
8862 infoPtr->bDoChangeNotify = FALSE;
8863 if (!(lvItem.state & LVIS_SELECTED) && LISTVIEW_GetSelectedCount(infoPtr))
8864 oldstate |= LVIS_SELECTED;
8865 if (infoPtr->nFocusedItem != -1) oldstate |= LVIS_FOCUSED;
8866 }
8867
8868 /* apply to all items */
8869 for (lvItem.iItem = 0; lvItem.iItem < infoPtr->nItemCount; lvItem.iItem++)
8870 if (!LISTVIEW_SetItemT(infoPtr, &lvItem, TRUE)) ret = FALSE;
8871
8872 if (infoPtr->dwStyle & LVS_OWNERDATA)
8873 {
8874 NMLISTVIEW nmlv;
8875
8876 infoPtr->bDoChangeNotify = notify;
8877
8878 nmlv.iItem = -1;
8879 nmlv.iSubItem = 0;
8880 nmlv.uNewState = lvItem.state & lvItem.stateMask;
8881 nmlv.uOldState = oldstate & lvItem.stateMask;
8882 nmlv.uChanged = LVIF_STATE;
8883 nmlv.ptAction.x = nmlv.ptAction.y = 0;
8884 nmlv.lParam = 0;
8885
8886 notify_listview(infoPtr, LVN_ITEMCHANGED, &nmlv);
8887 }
8888 }
8889 else
8890 ret = LISTVIEW_SetItemT(infoPtr, &lvItem, TRUE);
8891
8892 return ret;
8893 }
8894
8895 /***
8896 * DESCRIPTION:
8897 * Sets the text of an item or subitem.
8898 *
8899 * PARAMETER(S):
8900 * [I] hwnd : window handle
8901 * [I] nItem : item index
8902 * [I] lpLVItem : item or subitem info
8903 * [I] isW : TRUE if input is Unicode
8904 *
8905 * RETURN:
8906 * SUCCESS : TRUE
8907 * FAILURE : FALSE
8908 */
8909 static BOOL LISTVIEW_SetItemTextT(LISTVIEW_INFO *infoPtr, INT nItem, const LVITEMW *lpLVItem, BOOL isW)
8910 {
8911 LVITEMW lvItem;
8912
8913 if (!lpLVItem || nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
8914 if (infoPtr->dwStyle & LVS_OWNERDATA) return FALSE;
8915
8916 lvItem.iItem = nItem;
8917 lvItem.iSubItem = lpLVItem->iSubItem;
8918 lvItem.mask = LVIF_TEXT;
8919 lvItem.pszText = lpLVItem->pszText;
8920 lvItem.cchTextMax = lpLVItem->cchTextMax;
8921
8922 TRACE("(nItem=%d, lpLVItem=%s, isW=%d)\n", nItem, debuglvitem_t(&lvItem, isW), isW);
8923
8924 return LISTVIEW_SetItemT(infoPtr, &lvItem, isW);
8925 }
8926
8927 /***
8928 * DESCRIPTION:
8929 * Set item index that marks the start of a multiple selection.
8930 *
8931 * PARAMETER(S):
8932 * [I] infoPtr : valid pointer to the listview structure
8933 * [I] nIndex : index
8934 *
8935 * RETURN:
8936 * Index number or -1 if there is no selection mark.
8937 */
8938 static INT LISTVIEW_SetSelectionMark(LISTVIEW_INFO *infoPtr, INT nIndex)
8939 {
8940 INT nOldIndex = infoPtr->nSelectionMark;
8941
8942 TRACE("(nIndex=%d)\n", nIndex);
8943
8944 infoPtr->nSelectionMark = nIndex;
8945
8946 return nOldIndex;
8947 }
8948
8949 /***
8950 * DESCRIPTION:
8951 * Sets the text background color.
8952 *
8953 * PARAMETER(S):
8954 * [I] infoPtr : valid pointer to the listview structure
8955 * [I] color : text background color
8956 *
8957 * RETURN:
8958 * SUCCESS : TRUE
8959 * FAILURE : FALSE
8960 */
8961 static BOOL LISTVIEW_SetTextBkColor(LISTVIEW_INFO *infoPtr, COLORREF color)
8962 {
8963 TRACE("(color=%x)\n", color);
8964
8965 infoPtr->clrTextBk = color;
8966 return TRUE;
8967 }
8968
8969 /***
8970 * DESCRIPTION:
8971 * Sets the text foreground color.
8972 *
8973 * PARAMETER(S):
8974 * [I] infoPtr : valid pointer to the listview structure
8975 * [I] color : text color
8976 *
8977 * RETURN:
8978 * SUCCESS : TRUE
8979 * FAILURE : FALSE
8980 */
8981 static BOOL LISTVIEW_SetTextColor (LISTVIEW_INFO *infoPtr, COLORREF color)
8982 {
8983 TRACE("(color=%x)\n", color);
8984
8985 infoPtr->clrText = color;
8986 return TRUE;
8987 }
8988
8989 /***
8990 * DESCRIPTION:
8991 * Sets new ToolTip window to ListView control.
8992 *
8993 * PARAMETER(S):
8994 * [I] infoPtr : valid pointer to the listview structure
8995 * [I] hwndNewToolTip : handle to new ToolTip
8996 *
8997 * RETURN:
8998 * old tool tip
8999 */
9000 static HWND LISTVIEW_SetToolTips( LISTVIEW_INFO *infoPtr, HWND hwndNewToolTip)
9001 {
9002 HWND hwndOldToolTip = infoPtr->hwndToolTip;
9003 infoPtr->hwndToolTip = hwndNewToolTip;
9004 return hwndOldToolTip;
9005 }
9006
9007 /*
9008 * DESCRIPTION:
9009 * sets the Unicode character format flag for the control
9010 * PARAMETER(S):
9011 * [I] infoPtr :valid pointer to the listview structure
9012 * [I] fUnicode :true to switch to UNICODE false to switch to ANSI
9013 *
9014 * RETURN:
9015 * Old Unicode Format
9016 */
9017 static BOOL LISTVIEW_SetUnicodeFormat( LISTVIEW_INFO *infoPtr, BOOL unicode)
9018 {
9019 SHORT rc = infoPtr->notifyFormat;
9020 infoPtr->notifyFormat = (unicode) ? NFR_UNICODE : NFR_ANSI;
9021 return rc == NFR_UNICODE;
9022 }
9023
9024 /*
9025 * DESCRIPTION:
9026 * sets the control view mode
9027 * PARAMETER(S):
9028 * [I] infoPtr :valid pointer to the listview structure
9029 * [I] nView :new view mode value
9030 *
9031 * RETURN:
9032 * SUCCESS: 1
9033 * FAILURE: -1
9034 */
9035 static INT LISTVIEW_SetView(LISTVIEW_INFO *infoPtr, DWORD nView)
9036 {
9037 HIMAGELIST himl;
9038
9039 if (infoPtr->uView == nView) return 1;
9040
9041 if ((INT)nView < 0 || nView > LV_VIEW_MAX) return -1;
9042 if (nView == LV_VIEW_TILE)
9043 {
9044 FIXME("View LV_VIEW_TILE unimplemented\n");
9045 return -1;
9046 }
9047
9048 infoPtr->uView = nView;
9049
9050 SendMessageW(infoPtr->hwndEdit, WM_KILLFOCUS, 0, 0);
9051 ShowWindow(infoPtr->hwndHeader, SW_HIDE);
9052
9053 ShowScrollBar(infoPtr->hwndSelf, SB_BOTH, FALSE);
9054 SetRectEmpty(&infoPtr->rcFocus);
9055
9056 himl = (nView == LV_VIEW_ICON ? infoPtr->himlNormal : infoPtr->himlSmall);
9057 set_icon_size(&infoPtr->iconSize, himl, nView != LV_VIEW_ICON);
9058
9059 switch (nView)
9060 {
9061 case LV_VIEW_ICON:
9062 case LV_VIEW_SMALLICON:
9063 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
9064 break;
9065 case LV_VIEW_DETAILS:
9066 {
9067 HDLAYOUT hl;
9068 WINDOWPOS wp;
9069
9070 LISTVIEW_CreateHeader( infoPtr );
9071
9072 hl.prc = &infoPtr->rcList;
9073 hl.pwpos = &wp;
9074 SendMessageW(infoPtr->hwndHeader, HDM_LAYOUT, 0, (LPARAM)&hl);
9075 SetWindowPos(infoPtr->hwndHeader, infoPtr->hwndSelf, wp.x, wp.y, wp.cx, wp.cy,
9076 wp.flags | ((infoPtr->dwStyle & LVS_NOCOLUMNHEADER) ? SWP_HIDEWINDOW : SWP_SHOWWINDOW));
9077 break;
9078 }
9079 case LV_VIEW_LIST:
9080 break;
9081 }
9082
9083 LISTVIEW_UpdateItemSize(infoPtr);
9084 LISTVIEW_UpdateSize(infoPtr);
9085 LISTVIEW_UpdateScroll(infoPtr);
9086 LISTVIEW_InvalidateList(infoPtr);
9087
9088 TRACE("nView=%d\n", nView);
9089
9090 return 1;
9091 }
9092
9093 /* LISTVIEW_SetWorkAreas */
9094
9095 /***
9096 * DESCRIPTION:
9097 * Callback internally used by LISTVIEW_SortItems() in response of LVM_SORTITEMS
9098 *
9099 * PARAMETER(S):
9100 * [I] first : pointer to first ITEM_INFO to compare
9101 * [I] second : pointer to second ITEM_INFO to compare
9102 * [I] lParam : HWND of control
9103 *
9104 * RETURN:
9105 * if first comes before second : negative
9106 * if first comes after second : positive
9107 * if first and second are equivalent : zero
9108 */
9109 static INT WINAPI LISTVIEW_CallBackCompare(LPVOID first, LPVOID second, LPARAM lParam)
9110 {
9111 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)lParam;
9112 ITEM_INFO* lv_first = DPA_GetPtr( first, 0 );
9113 ITEM_INFO* lv_second = DPA_GetPtr( second, 0 );
9114
9115 /* Forward the call to the client defined callback */
9116 return (infoPtr->pfnCompare)( lv_first->lParam , lv_second->lParam, infoPtr->lParamSort );
9117 }
9118
9119 /***
9120 * DESCRIPTION:
9121 * Callback internally used by LISTVIEW_SortItems() in response of LVM_SORTITEMSEX
9122 *
9123 * PARAMETER(S):
9124 * [I] first : pointer to first ITEM_INFO to compare
9125 * [I] second : pointer to second ITEM_INFO to compare
9126 * [I] lParam : HWND of control
9127 *
9128 * RETURN:
9129 * if first comes before second : negative
9130 * if first comes after second : positive
9131 * if first and second are equivalent : zero
9132 */
9133 static INT WINAPI LISTVIEW_CallBackCompareEx(LPVOID first, LPVOID second, LPARAM lParam)
9134 {
9135 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)lParam;
9136 INT first_idx = DPA_GetPtrIndex( infoPtr->hdpaItems, first );
9137 INT second_idx = DPA_GetPtrIndex( infoPtr->hdpaItems, second );
9138
9139 /* Forward the call to the client defined callback */
9140 return (infoPtr->pfnCompare)( first_idx, second_idx, infoPtr->lParamSort );
9141 }
9142
9143 /***
9144 * DESCRIPTION:
9145 * Sorts the listview items.
9146 *
9147 * PARAMETER(S):
9148 * [I] infoPtr : valid pointer to the listview structure
9149 * [I] pfnCompare : application-defined value
9150 * [I] lParamSort : pointer to comparison callback
9151 * [I] IsEx : TRUE when LVM_SORTITEMSEX used
9152 *
9153 * RETURN:
9154 * SUCCESS : TRUE
9155 * FAILURE : FALSE
9156 */
9157 static BOOL LISTVIEW_SortItems(LISTVIEW_INFO *infoPtr, PFNLVCOMPARE pfnCompare,
9158 LPARAM lParamSort, BOOL IsEx)
9159 {
9160 HDPA hdpaSubItems;
9161 ITEM_INFO *lpItem;
9162 LPVOID selectionMarkItem = NULL;
9163 LPVOID focusedItem = NULL;
9164 int i;
9165
9166 TRACE("(pfnCompare=%p, lParamSort=%lx)\n", pfnCompare, lParamSort);
9167
9168 if (infoPtr->dwStyle & LVS_OWNERDATA) return FALSE;
9169
9170 if (!pfnCompare) return FALSE;
9171 if (!infoPtr->hdpaItems) return FALSE;
9172
9173 /* if there are 0 or 1 items, there is no need to sort */
9174 if (infoPtr->nItemCount < 2) return TRUE;
9175
9176 /* clear selection */
9177 ranges_clear(infoPtr->selectionRanges);
9178
9179 /* save selection mark and focused item */
9180 if (infoPtr->nSelectionMark >= 0)
9181 selectionMarkItem = DPA_GetPtr(infoPtr->hdpaItems, infoPtr->nSelectionMark);
9182 if (infoPtr->nFocusedItem >= 0)
9183 focusedItem = DPA_GetPtr(infoPtr->hdpaItems, infoPtr->nFocusedItem);
9184
9185 infoPtr->pfnCompare = pfnCompare;
9186 infoPtr->lParamSort = lParamSort;
9187 if (IsEx)
9188 DPA_Sort(infoPtr->hdpaItems, LISTVIEW_CallBackCompareEx, (LPARAM)infoPtr);
9189 else
9190 DPA_Sort(infoPtr->hdpaItems, LISTVIEW_CallBackCompare, (LPARAM)infoPtr);
9191
9192 /* restore selection ranges */
9193 for (i=0; i < infoPtr->nItemCount; i++)
9194 {
9195 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, i);
9196 lpItem = DPA_GetPtr(hdpaSubItems, 0);
9197
9198 if (lpItem->state & LVIS_SELECTED)
9199 ranges_additem(infoPtr->selectionRanges, i);
9200 }
9201 /* restore selection mark and focused item */
9202 infoPtr->nSelectionMark = DPA_GetPtrIndex(infoPtr->hdpaItems, selectionMarkItem);
9203 infoPtr->nFocusedItem = DPA_GetPtrIndex(infoPtr->hdpaItems, focusedItem);
9204
9205 /* I believe nHotItem should be left alone, see LISTVIEW_ShiftIndices */
9206
9207 /* refresh the display */
9208 LISTVIEW_InvalidateList(infoPtr);
9209 return TRUE;
9210 }
9211
9212 /***
9213 * DESCRIPTION:
9214 * Update theme handle after a theme change.
9215 *
9216 * PARAMETER(S):
9217 * [I] infoPtr : valid pointer to the listview structure
9218 *
9219 * RETURN:
9220 * SUCCESS : 0
9221 * FAILURE : something else
9222 */
9223 static LRESULT LISTVIEW_ThemeChanged(const LISTVIEW_INFO *infoPtr)
9224 {
9225 HTHEME theme = GetWindowTheme(infoPtr->hwndSelf);
9226 CloseThemeData(theme);
9227 OpenThemeData(infoPtr->hwndSelf, themeClass);
9228 return 0;
9229 }
9230
9231 /***
9232 * DESCRIPTION:
9233 * Updates an items or rearranges the listview control.
9234 *
9235 * PARAMETER(S):
9236 * [I] infoPtr : valid pointer to the listview structure
9237 * [I] nItem : item index
9238 *
9239 * RETURN:
9240 * SUCCESS : TRUE
9241 * FAILURE : FALSE
9242 */
9243 static BOOL LISTVIEW_Update(LISTVIEW_INFO *infoPtr, INT nItem)
9244 {
9245 TRACE("(nItem=%d)\n", nItem);
9246
9247 if (nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
9248
9249 /* rearrange with default alignment style */
9250 if (is_autoarrange(infoPtr))
9251 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
9252 else
9253 LISTVIEW_InvalidateItem(infoPtr, nItem);
9254
9255 return TRUE;
9256 }
9257
9258 /***
9259 * DESCRIPTION:
9260 * Draw the track line at the place defined in the infoPtr structure.
9261 * The line is drawn with a XOR pen so drawing the line for the second time
9262 * in the same place erases the line.
9263 *
9264 * PARAMETER(S):
9265 * [I] infoPtr : valid pointer to the listview structure
9266 *
9267 * RETURN:
9268 * SUCCESS : TRUE
9269 * FAILURE : FALSE
9270 */
9271 static BOOL LISTVIEW_DrawTrackLine(const LISTVIEW_INFO *infoPtr)
9272 {
9273 HDC hdc;
9274
9275 if (infoPtr->xTrackLine == -1)
9276 return FALSE;
9277
9278 if (!(hdc = GetDC(infoPtr->hwndSelf)))
9279 return FALSE;
9280 PatBlt( hdc, infoPtr->xTrackLine, infoPtr->rcList.top,
9281 1, infoPtr->rcList.bottom - infoPtr->rcList.top, DSTINVERT );
9282 ReleaseDC(infoPtr->hwndSelf, hdc);
9283 return TRUE;
9284 }
9285
9286 /***
9287 * DESCRIPTION:
9288 * Called when an edit control should be displayed. This function is called after
9289 * we are sure that there was a single click - not a double click (this is a TIMERPROC).
9290 *
9291 * PARAMETER(S):
9292 * [I] hwnd : Handle to the listview
9293 * [I] uMsg : WM_TIMER (ignored)
9294 * [I] idEvent : The timer ID interpreted as a pointer to a DELAYED_EDIT_ITEM struct
9295 * [I] dwTimer : The elapsed time (ignored)
9296 *
9297 * RETURN:
9298 * None.
9299 */
9300 static VOID CALLBACK LISTVIEW_DelayedEditItem(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime)
9301 {
9302 DELAYED_ITEM_EDIT *editItem = (DELAYED_ITEM_EDIT *)idEvent;
9303 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)GetWindowLongPtrW(hwnd, 0);
9304
9305 KillTimer(hwnd, idEvent);
9306 editItem->fEnabled = FALSE;
9307 /* check if the item is still selected */
9308 if (infoPtr->bFocus && LISTVIEW_GetItemState(infoPtr, editItem->iItem, LVIS_SELECTED))
9309 LISTVIEW_EditLabelT(infoPtr, editItem->iItem, TRUE);
9310 }
9311
9312 /***
9313 * DESCRIPTION:
9314 * Creates the listview control - the WM_NCCREATE phase.
9315 *
9316 * PARAMETER(S):
9317 * [I] hwnd : window handle
9318 * [I] lpcs : the create parameters
9319 *
9320 * RETURN:
9321 * Success: TRUE
9322 * Failure: FALSE
9323 */
9324 static LRESULT LISTVIEW_NCCreate(HWND hwnd, const CREATESTRUCTW *lpcs)
9325 {
9326 LISTVIEW_INFO *infoPtr;
9327 LOGFONTW logFont;
9328
9329 TRACE("(lpcs=%p)\n", lpcs);
9330
9331 /* initialize info pointer */
9332 infoPtr = Alloc(sizeof(LISTVIEW_INFO));
9333 if (!infoPtr) return FALSE;
9334
9335 SetWindowLongPtrW(hwnd, 0, (DWORD_PTR)infoPtr);
9336
9337 infoPtr->hwndSelf = hwnd;
9338 infoPtr->dwStyle = lpcs->style; /* Note: may be changed in WM_CREATE */
9339 map_style_view(infoPtr);
9340 /* determine the type of structures to use */
9341 infoPtr->hwndNotify = lpcs->hwndParent;
9342 /* infoPtr->notifyFormat will be filled in WM_CREATE */
9343
9344 /* initialize color information */
9345 infoPtr->clrBk = CLR_NONE;
9346 infoPtr->clrText = CLR_DEFAULT;
9347 infoPtr->clrTextBk = CLR_DEFAULT;
9348 LISTVIEW_SetBkColor(infoPtr, comctl32_color.clrWindow);
9349 infoPtr->bDefaultBkColor = TRUE;
9350
9351 /* set default values */
9352 infoPtr->nFocusedItem = -1;
9353 infoPtr->nSelectionMark = -1;
9354 infoPtr->nHotItem = -1;
9355 infoPtr->bRedraw = TRUE;
9356 infoPtr->bNoItemMetrics = TRUE;
9357 infoPtr->bDoChangeNotify = TRUE;
9358 infoPtr->autoSpacing = TRUE;
9359 infoPtr->iconSpacing.cx = GetSystemMetrics(SM_CXICONSPACING) - GetSystemMetrics(SM_CXICON);
9360 infoPtr->iconSpacing.cy = GetSystemMetrics(SM_CYICONSPACING) - GetSystemMetrics(SM_CYICON);
9361 infoPtr->nEditLabelItem = -1;
9362 infoPtr->nLButtonDownItem = -1;
9363 infoPtr->dwHoverTime = HOVER_DEFAULT; /* default system hover time */
9364 infoPtr->cWheelRemainder = 0;
9365 infoPtr->nMeasureItemHeight = 0;
9366 infoPtr->xTrackLine = -1; /* no track line */
9367 infoPtr->itemEdit.fEnabled = FALSE;
9368 infoPtr->iVersion = COMCTL32_VERSION;
9369 infoPtr->colRectsDirty = FALSE;
9370
9371 /* get default font (icon title) */
9372 SystemParametersInfoW(SPI_GETICONTITLELOGFONT, 0, &logFont, 0);
9373 infoPtr->hDefaultFont = CreateFontIndirectW(&logFont);
9374 infoPtr->hFont = infoPtr->hDefaultFont;
9375 LISTVIEW_SaveTextMetrics(infoPtr);
9376
9377 /* allocate memory for the data structure */
9378 if (!(infoPtr->selectionRanges = ranges_create(10))) goto fail;
9379 if (!(infoPtr->hdpaItems = DPA_Create(10))) goto fail;
9380 if (!(infoPtr->hdpaItemIds = DPA_Create(10))) goto fail;
9381 if (!(infoPtr->hdpaPosX = DPA_Create(10))) goto fail;
9382 if (!(infoPtr->hdpaPosY = DPA_Create(10))) goto fail;
9383 if (!(infoPtr->hdpaColumns = DPA_Create(10))) goto fail;
9384 return TRUE;
9385
9386 fail:
9387 DestroyWindow(infoPtr->hwndHeader);
9388 ranges_destroy(infoPtr->selectionRanges);
9389 DPA_Destroy(infoPtr->hdpaItems);
9390 DPA_Destroy(infoPtr->hdpaItemIds);
9391 DPA_Destroy(infoPtr->hdpaPosX);
9392 DPA_Destroy(infoPtr->hdpaPosY);
9393 DPA_Destroy(infoPtr->hdpaColumns);
9394 Free(infoPtr);
9395 return FALSE;
9396 }
9397
9398 /***
9399 * DESCRIPTION:
9400 * Creates the listview control - the WM_CREATE phase. Most of the data is
9401 * already set up in LISTVIEW_NCCreate
9402 *
9403 * PARAMETER(S):
9404 * [I] hwnd : window handle
9405 * [I] lpcs : the create parameters
9406 *
9407 * RETURN:
9408 * Success: 0
9409 * Failure: -1
9410 */
9411 static LRESULT LISTVIEW_Create(HWND hwnd, const CREATESTRUCTW *lpcs)
9412 {
9413 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)GetWindowLongPtrW(hwnd, 0);
9414
9415 TRACE("(lpcs=%p, style=0x%08x)\n", lpcs, lpcs->style);
9416
9417 infoPtr->dwStyle = lpcs->style;
9418 map_style_view(infoPtr);
9419
9420 infoPtr->notifyFormat = SendMessageW(infoPtr->hwndNotify, WM_NOTIFYFORMAT,
9421 (WPARAM)infoPtr->hwndSelf, NF_QUERY);
9422 /* on error defaulting to ANSI notifications */
9423 if (infoPtr->notifyFormat == 0) infoPtr->notifyFormat = NFR_ANSI;
9424 TRACE("notify format=%d\n", infoPtr->notifyFormat);
9425
9426 if ((infoPtr->uView == LV_VIEW_DETAILS) && (lpcs->style & WS_VISIBLE))
9427 {
9428 if (LISTVIEW_CreateHeader(infoPtr) < 0) return -1;
9429 }
9430 else
9431 infoPtr->hwndHeader = 0;
9432
9433 /* init item size to avoid division by 0 */
9434 LISTVIEW_UpdateItemSize (infoPtr);
9435 LISTVIEW_UpdateSize (infoPtr);
9436
9437 if (infoPtr->uView == LV_VIEW_DETAILS)
9438 {
9439 if (!(LVS_NOCOLUMNHEADER & lpcs->style) && (WS_VISIBLE & lpcs->style))
9440 {
9441 ShowWindow(infoPtr->hwndHeader, SW_SHOWNORMAL);
9442 }
9443 LISTVIEW_UpdateScroll(infoPtr);
9444 /* send WM_MEASUREITEM notification */
9445 if (infoPtr->dwStyle & LVS_OWNERDRAWFIXED) notify_measureitem(infoPtr);
9446 }
9447
9448 OpenThemeData(hwnd, themeClass);
9449
9450 /* initialize the icon sizes */
9451 set_icon_size(&infoPtr->iconSize, infoPtr->himlNormal, infoPtr->uView != LV_VIEW_ICON);
9452 set_icon_size(&infoPtr->iconStateSize, infoPtr->himlState, TRUE);
9453 return 0;
9454 }
9455
9456 /***
9457 * DESCRIPTION:
9458 * Destroys the listview control.
9459 *
9460 * PARAMETER(S):
9461 * [I] infoPtr : valid pointer to the listview structure
9462 *
9463 * RETURN:
9464 * Success: 0
9465 * Failure: -1
9466 */
9467 static LRESULT LISTVIEW_Destroy(LISTVIEW_INFO *infoPtr)
9468 {
9469 HTHEME theme = GetWindowTheme(infoPtr->hwndSelf);
9470 CloseThemeData(theme);
9471
9472 /* delete all items */
9473 LISTVIEW_DeleteAllItems(infoPtr, TRUE);
9474
9475 return 0;
9476 }
9477
9478 /***
9479 * DESCRIPTION:
9480 * Enables the listview control.
9481 *
9482 * PARAMETER(S):
9483 * [I] infoPtr : valid pointer to the listview structure
9484 * [I] bEnable : specifies whether to enable or disable the window
9485 *
9486 * RETURN:
9487 * SUCCESS : TRUE
9488 * FAILURE : FALSE
9489 */
9490 static BOOL LISTVIEW_Enable(const LISTVIEW_INFO *infoPtr)
9491 {
9492 if (infoPtr->dwStyle & LVS_OWNERDRAWFIXED)
9493 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
9494 return TRUE;
9495 }
9496
9497 /***
9498 * DESCRIPTION:
9499 * Erases the background of the listview control.
9500 *
9501 * PARAMETER(S):
9502 * [I] infoPtr : valid pointer to the listview structure
9503 * [I] hdc : device context handle
9504 *
9505 * RETURN:
9506 * SUCCESS : TRUE
9507 * FAILURE : FALSE
9508 */
9509 static inline BOOL LISTVIEW_EraseBkgnd(const LISTVIEW_INFO *infoPtr, HDC hdc)
9510 {
9511 RECT rc;
9512
9513 TRACE("(hdc=%p)\n", hdc);
9514
9515 if (!GetClipBox(hdc, &rc)) return FALSE;
9516
9517 if (infoPtr->clrBk == CLR_NONE)
9518 {
9519 if (infoPtr->dwLvExStyle & LVS_EX_TRANSPARENTBKGND)
9520 return SendMessageW(infoPtr->hwndNotify, WM_PRINTCLIENT,
9521 (WPARAM)hdc, PRF_ERASEBKGND);
9522 else
9523 return SendMessageW(infoPtr->hwndNotify, WM_ERASEBKGND, (WPARAM)hdc, 0);
9524 }
9525
9526 /* for double buffered controls we need to do this during refresh */
9527 if (infoPtr->dwLvExStyle & LVS_EX_DOUBLEBUFFER) return FALSE;
9528
9529 return LISTVIEW_FillBkgnd(infoPtr, hdc, &rc);
9530 }
9531
9532
9533 /***
9534 * DESCRIPTION:
9535 * Helper function for LISTVIEW_[HV]Scroll *only*.
9536 * Performs vertical/horizontal scrolling by a give amount.
9537 *
9538 * PARAMETER(S):
9539 * [I] infoPtr : valid pointer to the listview structure
9540 * [I] dx : amount of horizontal scroll
9541 * [I] dy : amount of vertical scroll
9542 */
9543 static void scroll_list(LISTVIEW_INFO *infoPtr, INT dx, INT dy)
9544 {
9545 /* now we can scroll the list */
9546 ScrollWindowEx(infoPtr->hwndSelf, dx, dy, &infoPtr->rcList,
9547 &infoPtr->rcList, 0, 0, SW_ERASE | SW_INVALIDATE);
9548 /* if we have focus, adjust rect */
9549 OffsetRect(&infoPtr->rcFocus, dx, dy);
9550 UpdateWindow(infoPtr->hwndSelf);
9551 }
9552
9553 /***
9554 * DESCRIPTION:
9555 * Performs vertical scrolling.
9556 *
9557 * PARAMETER(S):
9558 * [I] infoPtr : valid pointer to the listview structure
9559 * [I] nScrollCode : scroll code
9560 * [I] nScrollDiff : units to scroll in SB_INTERNAL mode, 0 otherwise
9561 * [I] hScrollWnd : scrollbar control window handle
9562 *
9563 * RETURN:
9564 * Zero
9565 *
9566 * NOTES:
9567 * SB_LINEUP/SB_LINEDOWN:
9568 * for LVS_ICON, LVS_SMALLICON is 37 by experiment
9569 * for LVS_REPORT is 1 line
9570 * for LVS_LIST cannot occur
9571 *
9572 */
9573 static LRESULT LISTVIEW_VScroll(LISTVIEW_INFO *infoPtr, INT nScrollCode,
9574 INT nScrollDiff)
9575 {
9576 INT nOldScrollPos, nNewScrollPos;
9577 SCROLLINFO scrollInfo;
9578 BOOL is_an_icon;
9579
9580 TRACE("(nScrollCode=%d(%s), nScrollDiff=%d)\n", nScrollCode,
9581 debugscrollcode(nScrollCode), nScrollDiff);
9582
9583 if (infoPtr->hwndEdit) SendMessageW(infoPtr->hwndEdit, WM_KILLFOCUS, 0, 0);
9584
9585 scrollInfo.cbSize = sizeof(SCROLLINFO);
9586 scrollInfo.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_TRACKPOS;
9587
9588 is_an_icon = ((infoPtr->uView == LV_VIEW_ICON) || (infoPtr->uView == LV_VIEW_SMALLICON));
9589
9590 if (!GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo)) return 1;
9591
9592 nOldScrollPos = scrollInfo.nPos;
9593 switch (nScrollCode)
9594 {
9595 case SB_INTERNAL:
9596 break;
9597
9598 case SB_LINEUP:
9599 nScrollDiff = (is_an_icon) ? -LISTVIEW_SCROLL_ICON_LINE_SIZE : -1;
9600 break;
9601
9602 case SB_LINEDOWN:
9603 nScrollDiff = (is_an_icon) ? LISTVIEW_SCROLL_ICON_LINE_SIZE : 1;
9604 break;
9605
9606 case SB_PAGEUP:
9607 nScrollDiff = -scrollInfo.nPage;
9608 break;
9609
9610 case SB_PAGEDOWN:
9611 nScrollDiff = scrollInfo.nPage;
9612 break;
9613
9614 case SB_THUMBPOSITION:
9615 case SB_THUMBTRACK:
9616 nScrollDiff = scrollInfo.nTrackPos - scrollInfo.nPos;
9617 break;
9618
9619 default:
9620 nScrollDiff = 0;
9621 }
9622
9623 /* quit right away if pos isn't changing */
9624 if (nScrollDiff == 0) return 0;
9625
9626 /* calculate new position, and handle overflows */
9627 nNewScrollPos = scrollInfo.nPos + nScrollDiff;
9628 if (nScrollDiff > 0) {
9629 if (nNewScrollPos < nOldScrollPos ||
9630 nNewScrollPos > scrollInfo.nMax)
9631 nNewScrollPos = scrollInfo.nMax;
9632 } else {
9633 if (nNewScrollPos > nOldScrollPos ||
9634 nNewScrollPos < scrollInfo.nMin)
9635 nNewScrollPos = scrollInfo.nMin;
9636 }
9637
9638 /* set the new position, and reread in case it changed */
9639 scrollInfo.fMask = SIF_POS;
9640 scrollInfo.nPos = nNewScrollPos;
9641 nNewScrollPos = SetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo, TRUE);
9642
9643 /* carry on only if it really changed */
9644 if (nNewScrollPos == nOldScrollPos) return 0;
9645
9646 /* now adjust to client coordinates */
9647 nScrollDiff = nOldScrollPos - nNewScrollPos;
9648 if (infoPtr->uView == LV_VIEW_DETAILS) nScrollDiff *= infoPtr->nItemHeight;
9649
9650 /* and scroll the window */
9651 scroll_list(infoPtr, 0, nScrollDiff);
9652
9653 return 0;
9654 }
9655
9656 /***
9657 * DESCRIPTION:
9658 * Performs horizontal scrolling.
9659 *
9660 * PARAMETER(S):
9661 * [I] infoPtr : valid pointer to the listview structure
9662 * [I] nScrollCode : scroll code
9663 * [I] nScrollDiff : units to scroll in SB_INTERNAL mode, 0 otherwise
9664 * [I] hScrollWnd : scrollbar control window handle
9665 *
9666 * RETURN:
9667 * Zero
9668 *
9669 * NOTES:
9670 * SB_LINELEFT/SB_LINERIGHT:
9671 * for LVS_ICON, LVS_SMALLICON 1 pixel
9672 * for LVS_REPORT is 1 pixel
9673 * for LVS_LIST is 1 column --> which is a 1 because the
9674 * scroll is based on columns not pixels
9675 *
9676 */
9677 static LRESULT LISTVIEW_HScroll(LISTVIEW_INFO *infoPtr, INT nScrollCode,
9678 INT nScrollDiff)
9679 {
9680 INT nOldScrollPos, nNewScrollPos;
9681 SCROLLINFO scrollInfo;
9682 BOOL is_an_icon;
9683
9684 TRACE("(nScrollCode=%d(%s), nScrollDiff=%d)\n", nScrollCode,
9685 debugscrollcode(nScrollCode), nScrollDiff);
9686
9687 if (infoPtr->hwndEdit) SendMessageW(infoPtr->hwndEdit, WM_KILLFOCUS, 0, 0);
9688
9689 scrollInfo.cbSize = sizeof(SCROLLINFO);
9690 scrollInfo.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_TRACKPOS;
9691
9692 is_an_icon = ((infoPtr->uView == LV_VIEW_ICON) || (infoPtr->uView == LV_VIEW_SMALLICON));
9693
9694 if (!GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo)) return 1;
9695
9696 nOldScrollPos = scrollInfo.nPos;
9697
9698 switch (nScrollCode)
9699 {
9700 case SB_INTERNAL:
9701 break;
9702
9703 case SB_LINELEFT:
9704 nScrollDiff = (is_an_icon) ? -LISTVIEW_SCROLL_ICON_LINE_SIZE : -1;
9705 break;
9706
9707 case SB_LINERIGHT:
9708 nScrollDiff = (is_an_icon) ? LISTVIEW_SCROLL_ICON_LINE_SIZE : 1;
9709 break;
9710
9711 case SB_PAGELEFT:
9712 nScrollDiff = -scrollInfo.nPage;
9713 break;
9714
9715 case SB_PAGERIGHT:
9716 nScrollDiff = scrollInfo.nPage;
9717 break;
9718
9719 case SB_THUMBPOSITION:
9720 case SB_THUMBTRACK:
9721 nScrollDiff = scrollInfo.nTrackPos - scrollInfo.nPos;
9722 break;
9723
9724 default:
9725 nScrollDiff = 0;
9726 }
9727
9728 /* quit right away if pos isn't changing */
9729 if (nScrollDiff == 0) return 0;
9730
9731 /* calculate new position, and handle overflows */
9732 nNewScrollPos = scrollInfo.nPos + nScrollDiff;
9733 if (nScrollDiff > 0) {
9734 if (nNewScrollPos < nOldScrollPos ||
9735 nNewScrollPos > scrollInfo.nMax)
9736 nNewScrollPos = scrollInfo.nMax;
9737 } else {
9738 if (nNewScrollPos > nOldScrollPos ||
9739 nNewScrollPos < scrollInfo.nMin)
9740 nNewScrollPos = scrollInfo.nMin;
9741 }
9742
9743 /* set the new position, and reread in case it changed */
9744 scrollInfo.fMask = SIF_POS;
9745 scrollInfo.nPos = nNewScrollPos;
9746 nNewScrollPos = SetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo, TRUE);
9747
9748 /* carry on only if it really changed */
9749 if (nNewScrollPos == nOldScrollPos) return 0;
9750
9751 if (infoPtr->hwndHeader) LISTVIEW_UpdateHeaderSize(infoPtr, nNewScrollPos);
9752
9753 /* now adjust to client coordinates */
9754 nScrollDiff = nOldScrollPos - nNewScrollPos;
9755 if (infoPtr->uView == LV_VIEW_LIST) nScrollDiff *= infoPtr->nItemWidth;
9756
9757 /* and scroll the window */
9758 scroll_list(infoPtr, nScrollDiff, 0);
9759
9760 return 0;
9761 }
9762
9763 static LRESULT LISTVIEW_MouseWheel(LISTVIEW_INFO *infoPtr, INT wheelDelta)
9764 {
9765 UINT pulScrollLines = 3;
9766
9767 TRACE("(wheelDelta=%d)\n", wheelDelta);
9768
9769 switch(infoPtr->uView)
9770 {
9771 case LV_VIEW_ICON:
9772 case LV_VIEW_SMALLICON:
9773 /*
9774 * listview should be scrolled by a multiple of 37 dependently on its dimension or its visible item number
9775 * should be fixed in the future.
9776 */
9777 LISTVIEW_VScroll(infoPtr, SB_INTERNAL, (wheelDelta > 0) ?
9778 -LISTVIEW_SCROLL_ICON_LINE_SIZE : LISTVIEW_SCROLL_ICON_LINE_SIZE);
9779 break;
9780
9781 case LV_VIEW_DETAILS:
9782 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES,0, &pulScrollLines, 0);
9783
9784 /* if scrolling changes direction, ignore left overs */
9785 if ((wheelDelta < 0 && infoPtr->cWheelRemainder < 0) ||
9786 (wheelDelta > 0 && infoPtr->cWheelRemainder > 0))
9787 infoPtr->cWheelRemainder += wheelDelta;
9788 else
9789 infoPtr->cWheelRemainder = wheelDelta;
9790 if (infoPtr->cWheelRemainder && pulScrollLines)
9791 {
9792 int cLineScroll;
9793 pulScrollLines = min((UINT)LISTVIEW_GetCountPerColumn(infoPtr), pulScrollLines);
9794 cLineScroll = pulScrollLines * (float)infoPtr->cWheelRemainder / WHEEL_DELTA;
9795 infoPtr->cWheelRemainder -= WHEEL_DELTA * cLineScroll / (int)pulScrollLines;
9796 LISTVIEW_VScroll(infoPtr, SB_INTERNAL, -cLineScroll);
9797 }
9798 break;
9799
9800 case LV_VIEW_LIST:
9801 LISTVIEW_HScroll(infoPtr, (wheelDelta > 0) ? SB_LINELEFT : SB_LINERIGHT, 0);
9802 break;
9803 }
9804 return 0;
9805 }
9806
9807 /***
9808 * DESCRIPTION:
9809 * ???
9810 *
9811 * PARAMETER(S):
9812 * [I] infoPtr : valid pointer to the listview structure
9813 * [I] nVirtualKey : virtual key
9814 * [I] lKeyData : key data
9815 *
9816 * RETURN:
9817 * Zero
9818 */
9819 static LRESULT LISTVIEW_KeyDown(LISTVIEW_INFO *infoPtr, INT nVirtualKey, LONG lKeyData)
9820 {
9821 HWND hwndSelf = infoPtr->hwndSelf;
9822 INT nItem = -1;
9823 NMLVKEYDOWN nmKeyDown;
9824
9825 TRACE("(nVirtualKey=%d, lKeyData=%d)\n", nVirtualKey, lKeyData);
9826
9827 /* send LVN_KEYDOWN notification */
9828 nmKeyDown.wVKey = nVirtualKey;
9829 nmKeyDown.flags = 0;
9830 notify_hdr(infoPtr, LVN_KEYDOWN, &nmKeyDown.hdr);
9831 if (!IsWindow(hwndSelf))
9832 return 0;
9833
9834 switch (nVirtualKey)
9835 {
9836 case VK_SPACE:
9837 nItem = infoPtr->nFocusedItem;
9838 if (infoPtr->dwLvExStyle & LVS_EX_CHECKBOXES)
9839 toggle_checkbox_state(infoPtr, infoPtr->nFocusedItem);
9840 break;
9841
9842 case VK_RETURN:
9843 if ((infoPtr->nItemCount > 0) && (infoPtr->nFocusedItem != -1))
9844 {
9845 if (!notify(infoPtr, NM_RETURN)) return 0;
9846 if (!notify(infoPtr, LVN_ITEMACTIVATE)) return 0;
9847 }
9848 break;
9849
9850 case VK_HOME:
9851 if (infoPtr->nItemCount > 0)
9852 nItem = 0;
9853 break;
9854
9855 case VK_END:
9856 if (infoPtr->nItemCount > 0)
9857 nItem = infoPtr->nItemCount - 1;
9858 break;
9859
9860 case VK_LEFT:
9861 nItem = LISTVIEW_GetNextItem(infoPtr, infoPtr->nFocusedItem, LVNI_TOLEFT);
9862 break;
9863
9864 case VK_UP:
9865 nItem = LISTVIEW_GetNextItem(infoPtr, infoPtr->nFocusedItem, LVNI_ABOVE);
9866 break;
9867
9868 case VK_RIGHT:
9869 nItem = LISTVIEW_GetNextItem(infoPtr, infoPtr->nFocusedItem, LVNI_TORIGHT);
9870 break;
9871
9872 case VK_DOWN:
9873 nItem = LISTVIEW_GetNextItem(infoPtr, infoPtr->nFocusedItem, LVNI_BELOW);
9874 break;
9875
9876 case VK_PRIOR:
9877 if (infoPtr->uView == LV_VIEW_DETAILS)
9878 {
9879 INT topidx = LISTVIEW_GetTopIndex(infoPtr);
9880 if (infoPtr->nFocusedItem == topidx)
9881 nItem = topidx - LISTVIEW_GetCountPerColumn(infoPtr) + 1;
9882 else
9883 nItem = topidx;
9884 }
9885 else
9886 nItem = infoPtr->nFocusedItem - LISTVIEW_GetCountPerColumn(infoPtr)
9887 * LISTVIEW_GetCountPerRow(infoPtr);
9888 if(nItem < 0) nItem = 0;
9889 break;
9890
9891 case VK_NEXT:
9892 if (infoPtr->uView == LV_VIEW_DETAILS)
9893 {
9894 INT topidx = LISTVIEW_GetTopIndex(infoPtr);
9895 INT cnt = LISTVIEW_GetCountPerColumn(infoPtr);
9896 if (infoPtr->nFocusedItem == topidx + cnt - 1)
9897 nItem = infoPtr->nFocusedItem + cnt - 1;
9898 else
9899 nItem = topidx + cnt - 1;
9900 }
9901 else
9902 nItem = infoPtr->nFocusedItem + LISTVIEW_GetCountPerColumn(infoPtr)
9903 * LISTVIEW_GetCountPerRow(infoPtr);
9904 if(nItem >= infoPtr->nItemCount) nItem = infoPtr->nItemCount - 1;
9905 break;
9906 }
9907
9908 if ((nItem != -1) && (nItem != infoPtr->nFocusedItem || nVirtualKey == VK_SPACE))
9909 LISTVIEW_KeySelection(infoPtr, nItem, nVirtualKey == VK_SPACE);
9910
9911 return 0;
9912 }
9913
9914 /***
9915 * DESCRIPTION:
9916 * Kills the focus.
9917 *
9918 * PARAMETER(S):
9919 * [I] infoPtr : valid pointer to the listview structure
9920 *
9921 * RETURN:
9922 * Zero
9923 */
9924 static LRESULT LISTVIEW_KillFocus(LISTVIEW_INFO *infoPtr)
9925 {
9926 TRACE("()\n");
9927
9928 /* drop any left over scroll amount */
9929 infoPtr->cWheelRemainder = 0;
9930
9931 /* if we did not have the focus, there's nothing more to do */
9932 if (!infoPtr->bFocus) return 0;
9933
9934 /* send NM_KILLFOCUS notification */
9935 if (!notify(infoPtr, NM_KILLFOCUS)) return 0;
9936
9937 /* if we have a focus rectangle, get rid of it */
9938 LISTVIEW_ShowFocusRect(infoPtr, FALSE);
9939
9940 /* if have a marquee selection, stop it */
9941 if (infoPtr->bMarqueeSelect)
9942 {
9943 /* Remove the marquee rectangle and release our mouse capture */
9944 LISTVIEW_InvalidateRect(infoPtr, &infoPtr->marqueeRect);
9945 ReleaseCapture();
9946
9947 SetRect(&infoPtr->marqueeRect, 0, 0, 0, 0);
9948
9949 infoPtr->bMarqueeSelect = FALSE;
9950 infoPtr->bScrolling = FALSE;
9951 KillTimer(infoPtr->hwndSelf, (UINT_PTR) infoPtr);
9952 }
9953
9954 /* set window focus flag */
9955 infoPtr->bFocus = FALSE;
9956
9957 /* invalidate the selected items before resetting focus flag */
9958 LISTVIEW_InvalidateSelectedItems(infoPtr);
9959
9960 return 0;
9961 }
9962
9963 /***
9964 * DESCRIPTION:
9965 * Processes double click messages (left mouse button).
9966 *
9967 * PARAMETER(S):
9968 * [I] infoPtr : valid pointer to the listview structure
9969 * [I] wKey : key flag
9970 * [I] x,y : mouse coordinate
9971 *
9972 * RETURN:
9973 * Zero
9974 */
9975 static LRESULT LISTVIEW_LButtonDblClk(LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
9976 {
9977 LVHITTESTINFO htInfo;
9978
9979 TRACE("(key=%hu, X=%u, Y=%u)\n", wKey, x, y);
9980
9981 /* Cancel the item edition if any */
9982 if (infoPtr->itemEdit.fEnabled)
9983 {
9984 KillTimer(infoPtr->hwndSelf, (UINT_PTR)&infoPtr->itemEdit);
9985 infoPtr->itemEdit.fEnabled = FALSE;
9986 }
9987
9988 /* send NM_RELEASEDCAPTURE notification */
9989 if (!notify(infoPtr, NM_RELEASEDCAPTURE)) return 0;
9990
9991 htInfo.pt.x = x;
9992 htInfo.pt.y = y;
9993
9994 /* send NM_DBLCLK notification */
9995 LISTVIEW_HitTest(infoPtr, &htInfo, TRUE, FALSE);
9996 if (!notify_click(infoPtr, NM_DBLCLK, &htInfo)) return 0;
9997
9998 /* To send the LVN_ITEMACTIVATE, it must be on an Item */
9999 if(htInfo.iItem != -1) notify_itemactivate(infoPtr,&htInfo);
10000
10001 return 0;
10002 }
10003
10004 static LRESULT LISTVIEW_TrackMouse(const LISTVIEW_INFO *infoPtr, POINT pt)
10005 {
10006 MSG msg;
10007 RECT r;
10008
10009 r.top = r.bottom = pt.y;
10010 r.left = r.right = pt.x;
10011
10012 InflateRect(&r, GetSystemMetrics(SM_CXDRAG), GetSystemMetrics(SM_CYDRAG));
10013
10014 SetCapture(infoPtr->hwndSelf);
10015
10016 while (1)
10017 {
10018 if (PeekMessageW(&msg, 0, 0, 0, PM_REMOVE | PM_NOYIELD))
10019 {
10020 if (msg.message == WM_MOUSEMOVE)
10021 {
10022 pt.x = (short)LOWORD(msg.lParam);
10023 pt.y = (short)HIWORD(msg.lParam);
10024 if (PtInRect(&r, pt))
10025 continue;
10026 else
10027 {
10028 ReleaseCapture();
10029 return 1;
10030 }
10031 }
10032 else if (msg.message >= WM_LBUTTONDOWN &&
10033 msg.message <= WM_RBUTTONDBLCLK)
10034 {
10035 break;
10036 }
10037
10038 DispatchMessageW(&msg);
10039 }
10040
10041 if (GetCapture() != infoPtr->hwndSelf)
10042 return 0;
10043 }
10044
10045 ReleaseCapture();
10046 return 0;
10047 }
10048
10049
10050 /***
10051 * DESCRIPTION:
10052 * Processes mouse down messages (left mouse button).
10053 *
10054 * PARAMETERS:
10055 * infoPtr [I ] valid pointer to the listview structure
10056 * wKey [I ] key flag
10057 * x,y [I ] mouse coordinate
10058 *
10059 * RETURN:
10060 * Zero
10061 */
10062 static LRESULT LISTVIEW_LButtonDown(LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
10063 {
10064 LVHITTESTINFO lvHitTestInfo;
10065 static BOOL bGroupSelect = TRUE;
10066 POINT pt = { x, y };
10067 INT nItem;
10068
10069 TRACE("(key=%hu, X=%u, Y=%u)\n", wKey, x, y);
10070
10071 /* send NM_RELEASEDCAPTURE notification */
10072 if (!notify(infoPtr, NM_RELEASEDCAPTURE)) return 0;
10073
10074 /* set left button down flag and record the click position */
10075 infoPtr->bLButtonDown = TRUE;
10076 infoPtr->ptClickPos = pt;
10077 infoPtr->bDragging = FALSE;
10078 infoPtr->bMarqueeSelect = FALSE;
10079 infoPtr->bScrolling = FALSE;
10080
10081 lvHitTestInfo.pt.x = x;
10082 lvHitTestInfo.pt.y = y;
10083
10084 nItem = LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, TRUE);
10085 TRACE("at %s, nItem=%d\n", wine_dbgstr_point(&pt), nItem);
10086 if ((nItem >= 0) && (nItem < infoPtr->nItemCount))
10087 {
10088 if ((infoPtr->dwLvExStyle & LVS_EX_CHECKBOXES) && (lvHitTestInfo.flags & LVHT_ONITEMSTATEICON))
10089 {
10090 toggle_checkbox_state(infoPtr, nItem);
10091 return 0;
10092 }
10093
10094 if (infoPtr->dwStyle & LVS_SINGLESEL)
10095 {
10096 if (LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED))
10097 infoPtr->nEditLabelItem = nItem;
10098 else
10099 LISTVIEW_SetSelection(infoPtr, nItem);
10100 }
10101 else
10102 {
10103 if ((wKey & MK_CONTROL) && (wKey & MK_SHIFT))
10104 {
10105 if (bGroupSelect)
10106 {
10107 if (!LISTVIEW_AddGroupSelection(infoPtr, nItem)) return 0;
10108 LISTVIEW_SetItemFocus(infoPtr, nItem);
10109 infoPtr->nSelectionMark = nItem;
10110 }
10111 else
10112 {
10113 LVITEMW item;
10114
10115 item.state = LVIS_SELECTED | LVIS_FOCUSED;
10116 item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
10117
10118 LISTVIEW_SetItemState(infoPtr,nItem,&item);
10119 infoPtr->nSelectionMark = nItem;
10120 }
10121 }
10122 else if (wKey & MK_CONTROL)
10123 {
10124 LVITEMW item;
10125
10126 bGroupSelect = (LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED) == 0);
10127
10128 item.state = (bGroupSelect ? LVIS_SELECTED : 0) | LVIS_FOCUSED;
10129 item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
10130 LISTVIEW_SetItemState(infoPtr, nItem, &item);
10131 infoPtr->nSelectionMark = nItem;
10132 }
10133 else if (wKey & MK_SHIFT)
10134 {
10135 LISTVIEW_SetGroupSelection(infoPtr, nItem);
10136 }
10137 else
10138 {
10139 if (LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED))
10140 {
10141 infoPtr->nEditLabelItem = nItem;
10142 infoPtr->nLButtonDownItem = nItem;
10143
10144 LISTVIEW_SetItemFocus(infoPtr, nItem);
10145 }
10146 else
10147 /* set selection (clears other pre-existing selections) */
10148 LISTVIEW_SetSelection(infoPtr, nItem);
10149 }
10150 }
10151
10152 if (infoPtr->dwLvExStyle & LVS_EX_ONECLICKACTIVATE)
10153 if(lvHitTestInfo.iItem != -1) notify_itemactivate(infoPtr,&lvHitTestInfo);
10154 }
10155 else
10156 {
10157 if (!infoPtr->bFocus)
10158 SetFocus(infoPtr->hwndSelf);
10159
10160 /* remove all selections */
10161 if (!(wKey & MK_CONTROL) && !(wKey & MK_SHIFT))
10162 LISTVIEW_DeselectAll(infoPtr);
10163 ReleaseCapture();
10164 }
10165
10166 return 0;
10167 }
10168
10169 /***
10170 * DESCRIPTION:
10171 * Processes mouse up messages (left mouse button).
10172 *
10173 * PARAMETERS:
10174 * infoPtr [I ] valid pointer to the listview structure
10175 * wKey [I ] key flag
10176 * x,y [I ] mouse coordinate
10177 *
10178 * RETURN:
10179 * Zero
10180 */
10181 static LRESULT LISTVIEW_LButtonUp(LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
10182 {
10183 LVHITTESTINFO lvHitTestInfo;
10184
10185 TRACE("(key=%hu, X=%u, Y=%u)\n", wKey, x, y);
10186
10187 if (!infoPtr->bLButtonDown) return 0;
10188
10189 lvHitTestInfo.pt.x = x;
10190 lvHitTestInfo.pt.y = y;
10191
10192 /* send NM_CLICK notification */
10193 LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, FALSE);
10194 if (!notify_click(infoPtr, NM_CLICK, &lvHitTestInfo)) return 0;
10195
10196 /* set left button flag */
10197 infoPtr->bLButtonDown = FALSE;
10198
10199 /* set a single selection, reset others */
10200 if(lvHitTestInfo.iItem == infoPtr->nLButtonDownItem && lvHitTestInfo.iItem != -1)
10201 LISTVIEW_SetSelection(infoPtr, infoPtr->nLButtonDownItem);
10202 infoPtr->nLButtonDownItem = -1;
10203
10204 if (infoPtr->bDragging || infoPtr->bMarqueeSelect)
10205 {
10206 /* Remove the marquee rectangle and release our mouse capture */
10207 if (infoPtr->bMarqueeSelect)
10208 {
10209 LISTVIEW_InvalidateRect(infoPtr, &infoPtr->marqueeDrawRect);
10210 ReleaseCapture();
10211 }
10212
10213 SetRect(&infoPtr->marqueeRect, 0, 0, 0, 0);
10214 SetRect(&infoPtr->marqueeDrawRect, 0, 0, 0, 0);
10215
10216 infoPtr->bDragging = FALSE;
10217 infoPtr->bMarqueeSelect = FALSE;
10218 infoPtr->bScrolling = FALSE;
10219
10220 KillTimer(infoPtr->hwndSelf, (UINT_PTR) infoPtr);
10221 return 0;
10222 }
10223
10224 /* if we clicked on a selected item, edit the label */
10225 if(lvHitTestInfo.iItem == infoPtr->nEditLabelItem && (lvHitTestInfo.flags & LVHT_ONITEMLABEL))
10226 {
10227 /* we want to make sure the user doesn't want to do a double click. So we will
10228 * delay the edit. WM_LBUTTONDBLCLICK will cancel the timer
10229 */
10230 infoPtr->itemEdit.fEnabled = TRUE;
10231 infoPtr->itemEdit.iItem = lvHitTestInfo.iItem;
10232 SetTimer(infoPtr->hwndSelf,
10233 (UINT_PTR)&infoPtr->itemEdit,
10234 GetDoubleClickTime(),
10235 LISTVIEW_DelayedEditItem);
10236 }
10237
10238 if (!infoPtr->bFocus)
10239 SetFocus(infoPtr->hwndSelf);
10240
10241 return 0;
10242 }
10243
10244 /***
10245 * DESCRIPTION:
10246 * Destroys the listview control (called after WM_DESTROY).
10247 *
10248 * PARAMETER(S):
10249 * [I] infoPtr : valid pointer to the listview structure
10250 *
10251 * RETURN:
10252 * Zero
10253 */
10254 static LRESULT LISTVIEW_NCDestroy(LISTVIEW_INFO *infoPtr)
10255 {
10256 INT i;
10257
10258 TRACE("()\n");
10259
10260 /* destroy data structure */
10261 DPA_Destroy(infoPtr->hdpaItems);
10262 DPA_Destroy(infoPtr->hdpaItemIds);
10263 DPA_Destroy(infoPtr->hdpaPosX);
10264 DPA_Destroy(infoPtr->hdpaPosY);
10265 /* columns */
10266 for (i = 0; i < DPA_GetPtrCount(infoPtr->hdpaColumns); i++)
10267 Free(DPA_GetPtr(infoPtr->hdpaColumns, i));
10268 DPA_Destroy(infoPtr->hdpaColumns);
10269 ranges_destroy(infoPtr->selectionRanges);
10270
10271 /* destroy image lists */
10272 if (!(infoPtr->dwStyle & LVS_SHAREIMAGELISTS))
10273 {
10274 ImageList_Destroy(infoPtr->himlNormal);
10275 ImageList_Destroy(infoPtr->himlSmall);
10276 ImageList_Destroy(infoPtr->himlState);
10277 }
10278
10279 /* destroy font, bkgnd brush */
10280 infoPtr->hFont = 0;
10281 if (infoPtr->hDefaultFont) DeleteObject(infoPtr->hDefaultFont);
10282 if (infoPtr->clrBk != CLR_NONE) DeleteObject(infoPtr->hBkBrush);
10283
10284 SetWindowLongPtrW(infoPtr->hwndSelf, 0, 0);
10285
10286 /* free listview info pointer*/
10287 Free(infoPtr);
10288
10289 return 0;
10290 }
10291
10292 /***
10293 * DESCRIPTION:
10294 * Handles notifications.
10295 *
10296 * PARAMETER(S):
10297 * [I] infoPtr : valid pointer to the listview structure
10298 * [I] lpnmhdr : notification information
10299 *
10300 * RETURN:
10301 * Zero
10302 */
10303 static LRESULT LISTVIEW_Notify(LISTVIEW_INFO *infoPtr, NMHDR *lpnmhdr)
10304 {
10305 NMHEADERW *lpnmh;
10306
10307 TRACE("(lpnmhdr=%p)\n", lpnmhdr);
10308
10309 if (!lpnmhdr || lpnmhdr->hwndFrom != infoPtr->hwndHeader) return 0;
10310
10311 /* remember: HDN_LAST < HDN_FIRST */
10312 if (lpnmhdr->code > HDN_FIRST || lpnmhdr->code < HDN_LAST) return 0;
10313 lpnmh = (NMHEADERW *)lpnmhdr;
10314
10315 if (lpnmh->iItem < 0 || lpnmh->iItem >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return 0;
10316
10317 switch (lpnmhdr->code)
10318 {
10319 case HDN_TRACKW:
10320 case HDN_TRACKA:
10321 {
10322 COLUMN_INFO *lpColumnInfo;
10323 POINT ptOrigin;
10324 INT x;
10325
10326 if (!lpnmh->pitem || !(lpnmh->pitem->mask & HDI_WIDTH))
10327 break;
10328
10329 /* remove the old line (if any) */
10330 LISTVIEW_DrawTrackLine(infoPtr);
10331
10332 /* compute & draw the new line */
10333 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, lpnmh->iItem);
10334 x = lpColumnInfo->rcHeader.left + lpnmh->pitem->cxy;
10335 LISTVIEW_GetOrigin(infoPtr, &ptOrigin);
10336 infoPtr->xTrackLine = x + ptOrigin.x;
10337 LISTVIEW_DrawTrackLine(infoPtr);
10338 return notify_forward_header(infoPtr, lpnmh);
10339 }
10340
10341 case HDN_ENDTRACKA:
10342 case HDN_ENDTRACKW:
10343 /* remove the track line (if any) */
10344 LISTVIEW_DrawTrackLine(infoPtr);
10345 infoPtr->xTrackLine = -1;
10346 return notify_forward_header(infoPtr, lpnmh);
10347
10348 case HDN_BEGINDRAG:
10349 if ((infoPtr->dwLvExStyle & LVS_EX_HEADERDRAGDROP) == 0) return 1;
10350 return notify_forward_header(infoPtr, lpnmh);
10351
10352 case HDN_ENDDRAG:
10353 infoPtr->colRectsDirty = TRUE;
10354 LISTVIEW_InvalidateList(infoPtr);
10355 return notify_forward_header(infoPtr, lpnmh);
10356
10357 case HDN_ITEMCHANGEDW:
10358 case HDN_ITEMCHANGEDA:
10359 {
10360 COLUMN_INFO *lpColumnInfo;
10361 HDITEMW hdi;
10362 INT dx, cxy;
10363
10364 if (!lpnmh->pitem || !(lpnmh->pitem->mask & HDI_WIDTH))
10365 {
10366 hdi.mask = HDI_WIDTH;
10367 if (!SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, lpnmh->iItem, (LPARAM)&hdi)) return 0;
10368 cxy = hdi.cxy;
10369 }
10370 else
10371 cxy = lpnmh->pitem->cxy;
10372
10373 /* determine how much we change since the last know position */
10374 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, lpnmh->iItem);
10375 dx = cxy - (lpColumnInfo->rcHeader.right - lpColumnInfo->rcHeader.left);
10376 if (dx != 0)
10377 {
10378 lpColumnInfo->rcHeader.right += dx;
10379
10380 hdi.mask = HDI_ORDER;
10381 SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, lpnmh->iItem, (LPARAM)&hdi);
10382
10383 /* not the rightmost one */
10384 if (hdi.iOrder + 1 < DPA_GetPtrCount(infoPtr->hdpaColumns))
10385 {
10386 INT nIndex = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX,
10387 hdi.iOrder + 1, 0);
10388 LISTVIEW_ScrollColumns(infoPtr, nIndex, dx);
10389 }
10390 else
10391 {
10392 /* only needs to update the scrolls */
10393 infoPtr->nItemWidth += dx;
10394 LISTVIEW_UpdateScroll(infoPtr);
10395 }
10396 LISTVIEW_UpdateItemSize(infoPtr);
10397 if (infoPtr->uView == LV_VIEW_DETAILS && is_redrawing(infoPtr))
10398 {
10399 POINT ptOrigin;
10400 RECT rcCol = lpColumnInfo->rcHeader;
10401
10402 LISTVIEW_GetOrigin(infoPtr, &ptOrigin);
10403 OffsetRect(&rcCol, ptOrigin.x, 0);
10404
10405 rcCol.top = infoPtr->rcList.top;
10406 rcCol.bottom = infoPtr->rcList.bottom;
10407
10408 /* resizing left-aligned columns leaves most of the left side untouched */
10409 if ((lpColumnInfo->fmt & LVCFMT_JUSTIFYMASK) == LVCFMT_LEFT)
10410 {
10411 INT nMaxDirty = infoPtr->nEllipsisWidth + infoPtr->ntmMaxCharWidth;
10412 if (dx > 0)
10413 nMaxDirty += dx;
10414 rcCol.left = max (rcCol.left, rcCol.right - nMaxDirty);
10415 }
10416
10417 /* when shrinking the last column clear the now unused field */
10418 if (hdi.iOrder == DPA_GetPtrCount(infoPtr->hdpaColumns) - 1)
10419 {
10420 RECT right;
10421
10422 rcCol.right -= dx;
10423
10424 /* deal with right from rightmost column area */
10425 right.left = rcCol.right;
10426 right.top = rcCol.top;
10427 right.bottom = rcCol.bottom;
10428 right.right = infoPtr->rcList.right;
10429
10430 LISTVIEW_InvalidateRect(infoPtr, &right);
10431 }
10432
10433 LISTVIEW_InvalidateRect(infoPtr, &rcCol);
10434 }
10435 }
10436 break;
10437 }
10438
10439 case HDN_ITEMCLICKW:
10440 case HDN_ITEMCLICKA:
10441 {
10442 /* Handle sorting by Header Column */
10443 NMLISTVIEW nmlv;
10444
10445 ZeroMemory(&nmlv, sizeof(NMLISTVIEW));
10446 nmlv.iItem = -1;
10447 nmlv.iSubItem = lpnmh->iItem;
10448 notify_listview(infoPtr, LVN_COLUMNCLICK, &nmlv);
10449 return notify_forward_header(infoPtr, lpnmh);
10450 }
10451
10452 case HDN_DIVIDERDBLCLICKW:
10453 case HDN_DIVIDERDBLCLICKA:
10454 /* FIXME: for LVS_EX_HEADERINALLVIEWS and not LV_VIEW_DETAILS
10455 we should use LVSCW_AUTOSIZE_USEHEADER, helper rework or
10456 split needed for that */
10457 LISTVIEW_SetColumnWidth(infoPtr, lpnmh->iItem, LVSCW_AUTOSIZE);
10458 return notify_forward_header(infoPtr, lpnmh);
10459 }
10460 return 0;
10461 }
10462
10463 /***
10464 * DESCRIPTION:
10465 * Paint non-client area of control.
10466 *
10467 * PARAMETER(S):
10468 * [I] infoPtr : valid pointer to the listview structureof the sender
10469 * [I] region : update region
10470 *
10471 * RETURN:
10472 * TRUE - frame was painted
10473 * FALSE - call default window proc
10474 */
10475 static BOOL LISTVIEW_NCPaint(const LISTVIEW_INFO *infoPtr, HRGN region)
10476 {
10477 HTHEME theme = GetWindowTheme (infoPtr->hwndSelf);
10478 HDC dc;
10479 RECT r;
10480 HRGN cliprgn;
10481 int cxEdge = GetSystemMetrics (SM_CXEDGE),
10482 cyEdge = GetSystemMetrics (SM_CYEDGE);
10483
10484 if (!theme)
10485 return DefWindowProcW (infoPtr->hwndSelf, WM_NCPAINT, (WPARAM)region, 0);
10486
10487 GetWindowRect(infoPtr->hwndSelf, &r);
10488
10489 cliprgn = CreateRectRgn (r.left + cxEdge, r.top + cyEdge,
10490 r.right - cxEdge, r.bottom - cyEdge);
10491 if (region != (HRGN)1)
10492 CombineRgn (cliprgn, cliprgn, region, RGN_AND);
10493 OffsetRect(&r, -r.left, -r.top);
10494
10495 dc = GetDCEx(infoPtr->hwndSelf, region, DCX_WINDOW|DCX_INTERSECTRGN);
10496 OffsetRect(&r, -r.left, -r.top);
10497
10498 if (IsThemeBackgroundPartiallyTransparent (theme, 0, 0))
10499 DrawThemeParentBackground(infoPtr->hwndSelf, dc, &r);
10500 DrawThemeBackground (theme, dc, 0, 0, &r, 0);
10501 ReleaseDC(infoPtr->hwndSelf, dc);
10502
10503 /* Call default proc to get the scrollbars etc. painted */
10504 DefWindowProcW (infoPtr->hwndSelf, WM_NCPAINT, (WPARAM)cliprgn, 0);
10505
10506 return FALSE;
10507 }
10508
10509 /***
10510 * DESCRIPTION:
10511 * Determines the type of structure to use.
10512 *
10513 * PARAMETER(S):
10514 * [I] infoPtr : valid pointer to the listview structureof the sender
10515 * [I] hwndFrom : listview window handle
10516 * [I] nCommand : command specifying the nature of the WM_NOTIFYFORMAT
10517 *
10518 * RETURN:
10519 * Zero
10520 */
10521 static LRESULT LISTVIEW_NotifyFormat(LISTVIEW_INFO *infoPtr, HWND hwndFrom, INT nCommand)
10522 {
10523 TRACE("(hwndFrom=%p, nCommand=%d)\n", hwndFrom, nCommand);
10524
10525 if (nCommand == NF_REQUERY)
10526 infoPtr->notifyFormat = SendMessageW(infoPtr->hwndNotify, WM_NOTIFYFORMAT, (WPARAM)infoPtr->hwndSelf, NF_QUERY);
10527
10528 return infoPtr->notifyFormat;
10529 }
10530
10531 /***
10532 * DESCRIPTION:
10533 * Paints/Repaints the listview control. Internal use.
10534 *
10535 * PARAMETER(S):
10536 * [I] infoPtr : valid pointer to the listview structure
10537 * [I] hdc : device context handle
10538 *
10539 * RETURN:
10540 * Zero
10541 */
10542 static LRESULT LISTVIEW_Paint(LISTVIEW_INFO *infoPtr, HDC hdc)
10543 {
10544 TRACE("(hdc=%p)\n", hdc);
10545
10546 if (infoPtr->bNoItemMetrics && infoPtr->nItemCount)
10547 {
10548 infoPtr->bNoItemMetrics = FALSE;
10549 LISTVIEW_UpdateItemSize(infoPtr);
10550 if (infoPtr->uView == LV_VIEW_ICON || infoPtr->uView == LV_VIEW_SMALLICON)
10551 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
10552 LISTVIEW_UpdateScroll(infoPtr);
10553 }
10554
10555 if (infoPtr->hwndHeader) UpdateWindow(infoPtr->hwndHeader);
10556
10557 if (hdc)
10558 LISTVIEW_Refresh(infoPtr, hdc, NULL);
10559 else
10560 {
10561 PAINTSTRUCT ps;
10562
10563 hdc = BeginPaint(infoPtr->hwndSelf, &ps);
10564 if (!hdc) return 1;
10565 LISTVIEW_Refresh(infoPtr, hdc, ps.fErase ? &ps.rcPaint : NULL);
10566 EndPaint(infoPtr->hwndSelf, &ps);
10567 }
10568
10569 return 0;
10570 }
10571
10572 /***
10573 * DESCRIPTION:
10574 * Paints/Repaints the listview control, WM_PAINT handler.
10575 *
10576 * PARAMETER(S):
10577 * [I] infoPtr : valid pointer to the listview structure
10578 * [I] hdc : device context handle
10579 *
10580 * RETURN:
10581 * Zero
10582 */
10583 static inline LRESULT LISTVIEW_WMPaint(LISTVIEW_INFO *infoPtr, HDC hdc)
10584 {
10585 TRACE("(hdc=%p)\n", hdc);
10586
10587 if (!is_redrawing(infoPtr))
10588 return DefWindowProcW (infoPtr->hwndSelf, WM_PAINT, (WPARAM)hdc, 0);
10589
10590 return LISTVIEW_Paint(infoPtr, hdc);
10591 }
10592
10593 /***
10594 * DESCRIPTION:
10595 * Paints/Repaints the listview control.
10596 *
10597 * PARAMETER(S):
10598 * [I] infoPtr : valid pointer to the listview structure
10599 * [I] hdc : device context handle
10600 * [I] options : drawing options
10601 *
10602 * RETURN:
10603 * Zero
10604 */
10605 static LRESULT LISTVIEW_PrintClient(LISTVIEW_INFO *infoPtr, HDC hdc, DWORD options)
10606 {
10607 FIXME("Partial Stub: (hdc=%p options=0x%08x)\n", hdc, options);
10608
10609 if ((options & PRF_CHECKVISIBLE) && !IsWindowVisible(infoPtr->hwndSelf))
10610 return 0;
10611
10612 if (options & PRF_ERASEBKGND)
10613 LISTVIEW_EraseBkgnd(infoPtr, hdc);
10614
10615 if (options & PRF_CLIENT)
10616 LISTVIEW_Paint(infoPtr, hdc);
10617
10618 return 0;
10619 }
10620
10621
10622 /***
10623 * DESCRIPTION:
10624 * Processes double click messages (right mouse button).
10625 *
10626 * PARAMETER(S):
10627 * [I] infoPtr : valid pointer to the listview structure
10628 * [I] wKey : key flag
10629 * [I] x,y : mouse coordinate
10630 *
10631 * RETURN:
10632 * Zero
10633 */
10634 static LRESULT LISTVIEW_RButtonDblClk(const LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
10635 {
10636 LVHITTESTINFO lvHitTestInfo;
10637
10638 TRACE("(key=%hu,X=%u,Y=%u)\n", wKey, x, y);
10639
10640 /* send NM_RELEASEDCAPTURE notification */
10641 if (!notify(infoPtr, NM_RELEASEDCAPTURE)) return 0;
10642
10643 /* send NM_RDBLCLK notification */
10644 lvHitTestInfo.pt.x = x;
10645 lvHitTestInfo.pt.y = y;
10646 LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, FALSE);
10647 notify_click(infoPtr, NM_RDBLCLK, &lvHitTestInfo);
10648
10649 return 0;
10650 }
10651
10652 /***
10653 * DESCRIPTION:
10654 * Processes WM_RBUTTONDOWN message and corresponding drag operation.
10655 *
10656 * PARAMETER(S):
10657 * [I] infoPtr : valid pointer to the listview structure
10658 * [I] wKey : key flag
10659 * [I] x, y : mouse coordinate
10660 *
10661 * RETURN:
10662 * Zero
10663 */
10664 static LRESULT LISTVIEW_RButtonDown(LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
10665 {
10666 LVHITTESTINFO ht;
10667 INT item;
10668
10669 TRACE("(key=%hu, x=%d, y=%d)\n", wKey, x, y);
10670
10671 /* send NM_RELEASEDCAPTURE notification */
10672 if (!notify(infoPtr, NM_RELEASEDCAPTURE)) return 0;
10673
10674 /* determine the index of the selected item */
10675 ht.pt.x = x;
10676 ht.pt.y = y;
10677 item = LISTVIEW_HitTest(infoPtr, &ht, TRUE, TRUE);
10678
10679 /* make sure the listview control window has the focus */
10680 if (!infoPtr->bFocus) SetFocus(infoPtr->hwndSelf);
10681
10682 if ((item >= 0) && (item < infoPtr->nItemCount))
10683 {
10684 LISTVIEW_SetItemFocus(infoPtr, item);
10685 if (!((wKey & MK_SHIFT) || (wKey & MK_CONTROL)) &&
10686 !LISTVIEW_GetItemState(infoPtr, item, LVIS_SELECTED))
10687 LISTVIEW_SetSelection(infoPtr, item);
10688 }
10689 else
10690 LISTVIEW_DeselectAll(infoPtr);
10691
10692 if (LISTVIEW_TrackMouse(infoPtr, ht.pt))
10693 {
10694 if (ht.iItem != -1)
10695 {
10696 NMLISTVIEW nmlv;
10697
10698 memset(&nmlv, 0, sizeof(nmlv));
10699 nmlv.iItem = ht.iItem;
10700 nmlv.ptAction = ht.pt;
10701
10702 notify_listview(infoPtr, LVN_BEGINRDRAG, &nmlv);
10703 }
10704 }
10705 else
10706 {
10707 SetFocus(infoPtr->hwndSelf);
10708
10709 ht.pt.x = x;
10710 ht.pt.y = y;
10711 LISTVIEW_HitTest(infoPtr, &ht, TRUE, FALSE);
10712
10713 if (notify_click(infoPtr, NM_RCLICK, &ht))
10714 {
10715 /* Send a WM_CONTEXTMENU message in response to the WM_RBUTTONUP */
10716 SendMessageW(infoPtr->hwndSelf, WM_CONTEXTMENU,
10717 (WPARAM)infoPtr->hwndSelf, (LPARAM)GetMessagePos());
10718 }
10719 }
10720
10721 return 0;
10722 }
10723
10724 /***
10725 * DESCRIPTION:
10726 * Sets the cursor.
10727 *
10728 * PARAMETER(S):
10729 * [I] infoPtr : valid pointer to the listview structure
10730 * [I] hwnd : window handle of window containing the cursor
10731 * [I] nHittest : hit-test code
10732 * [I] wMouseMsg : ideintifier of the mouse message
10733 *
10734 * RETURN:
10735 * TRUE if cursor is set
10736 * FALSE otherwise
10737 */
10738 static BOOL LISTVIEW_SetCursor(const LISTVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
10739 {
10740 LVHITTESTINFO lvHitTestInfo;
10741
10742 if (!LISTVIEW_IsHotTracking(infoPtr)) goto forward;
10743
10744 if (!infoPtr->hHotCursor) goto forward;
10745
10746 GetCursorPos(&lvHitTestInfo.pt);
10747 if (LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, FALSE, FALSE) < 0) goto forward;
10748
10749 SetCursor(infoPtr->hHotCursor);
10750
10751 return TRUE;
10752
10753 forward:
10754
10755 return DefWindowProcW(infoPtr->hwndSelf, WM_SETCURSOR, wParam, lParam);
10756 }
10757
10758 /***
10759 * DESCRIPTION:
10760 * Sets the focus.
10761 *
10762 * PARAMETER(S):
10763 * [I] infoPtr : valid pointer to the listview structure
10764 * [I] hwndLoseFocus : handle of previously focused window
10765 *
10766 * RETURN:
10767 * Zero
10768 */
10769 static LRESULT LISTVIEW_SetFocus(LISTVIEW_INFO *infoPtr, HWND hwndLoseFocus)
10770 {
10771 TRACE("(hwndLoseFocus=%p)\n", hwndLoseFocus);
10772
10773 /* if we have the focus already, there's nothing to do */
10774 if (infoPtr->bFocus) return 0;
10775
10776 /* send NM_SETFOCUS notification */
10777 if (!notify(infoPtr, NM_SETFOCUS)) return 0;
10778
10779 /* set window focus flag */
10780 infoPtr->bFocus = TRUE;
10781
10782 /* put the focus rect back on */
10783 LISTVIEW_ShowFocusRect(infoPtr, TRUE);
10784
10785 /* redraw all visible selected items */
10786 LISTVIEW_InvalidateSelectedItems(infoPtr);
10787
10788 return 0;
10789 }
10790
10791 /***
10792 * DESCRIPTION:
10793 * Sets the font.
10794 *
10795 * PARAMETER(S):
10796 * [I] infoPtr : valid pointer to the listview structure
10797 * [I] fRedraw : font handle
10798 * [I] fRedraw : redraw flag
10799 *
10800 * RETURN:
10801 * Zero
10802 */
10803 static LRESULT LISTVIEW_SetFont(LISTVIEW_INFO *infoPtr, HFONT hFont, WORD fRedraw)
10804 {
10805 HFONT oldFont = infoPtr->hFont;
10806 INT oldHeight = infoPtr->nItemHeight;
10807
10808 TRACE("(hfont=%p,redraw=%hu)\n", hFont, fRedraw);
10809
10810 infoPtr->hFont = hFont ? hFont : infoPtr->hDefaultFont;
10811 if (infoPtr->hFont == oldFont) return 0;
10812
10813 LISTVIEW_SaveTextMetrics(infoPtr);
10814
10815 infoPtr->nItemHeight = LISTVIEW_CalculateItemHeight(infoPtr);
10816
10817 if (infoPtr->uView == LV_VIEW_DETAILS)
10818 {
10819 SendMessageW(infoPtr->hwndHeader, WM_SETFONT, (WPARAM)hFont, MAKELPARAM(fRedraw, 0));
10820 LISTVIEW_UpdateSize(infoPtr);
10821 LISTVIEW_UpdateScroll(infoPtr);
10822 }
10823 else if (infoPtr->nItemHeight != oldHeight)
10824 LISTVIEW_UpdateScroll(infoPtr);
10825
10826 if (fRedraw) LISTVIEW_InvalidateList(infoPtr);
10827
10828 return 0;
10829 }
10830
10831 /***
10832 * DESCRIPTION:
10833 * Message handling for WM_SETREDRAW.
10834 * For the Listview, it invalidates the entire window (the doc specifies otherwise)
10835 *
10836 * PARAMETER(S):
10837 * [I] infoPtr : valid pointer to the listview structure
10838 * [I] bRedraw: state of redraw flag
10839 *
10840 * RETURN:
10841 * DefWinProc return value
10842 */
10843 static LRESULT LISTVIEW_SetRedraw(LISTVIEW_INFO *infoPtr, BOOL bRedraw)
10844 {
10845 TRACE("infoPtr->bRedraw=%d, bRedraw=%d\n", infoPtr->bRedraw, bRedraw);
10846
10847 /* we cannot use straight equality here because _any_ non-zero value is TRUE */
10848 if ((infoPtr->bRedraw && bRedraw) || (!infoPtr->bRedraw && !bRedraw)) return 0;
10849
10850 infoPtr->bRedraw = bRedraw;
10851
10852 if(!bRedraw) return 0;
10853
10854 if (is_autoarrange(infoPtr))
10855 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
10856 LISTVIEW_UpdateScroll(infoPtr);
10857
10858 /* despite what the WM_SETREDRAW docs says, apps expect us
10859 * to invalidate the listview here... stupid! */
10860 LISTVIEW_InvalidateList(infoPtr);
10861
10862 return 0;
10863 }
10864
10865 /***
10866 * DESCRIPTION:
10867 * Resizes the listview control. This function processes WM_SIZE
10868 * messages. At this time, the width and height are not used.
10869 *
10870 * PARAMETER(S):
10871 * [I] infoPtr : valid pointer to the listview structure
10872 * [I] Width : new width
10873 * [I] Height : new height
10874 *
10875 * RETURN:
10876 * Zero
10877 */
10878 static LRESULT LISTVIEW_Size(LISTVIEW_INFO *infoPtr, int Width, int Height)
10879 {
10880 RECT rcOld = infoPtr->rcList;
10881
10882 TRACE("(width=%d, height=%d)\n", Width, Height);
10883
10884 LISTVIEW_UpdateSize(infoPtr);
10885 if (EqualRect(&rcOld, &infoPtr->rcList)) return 0;
10886
10887 /* do not bother with display related stuff if we're not redrawing */
10888 if (!is_redrawing(infoPtr)) return 0;
10889
10890 if (is_autoarrange(infoPtr))
10891 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
10892
10893 LISTVIEW_UpdateScroll(infoPtr);
10894
10895 /* refresh all only for lists whose height changed significantly */
10896 if ((infoPtr->uView == LV_VIEW_LIST) &&
10897 (rcOld.bottom - rcOld.top) / infoPtr->nItemHeight !=
10898 (infoPtr->rcList.bottom - infoPtr->rcList.top) / infoPtr->nItemHeight)
10899 LISTVIEW_InvalidateList(infoPtr);
10900
10901 return 0;
10902 }
10903
10904 /***
10905 * DESCRIPTION:
10906 * Sets the size information.
10907 *
10908 * PARAMETER(S):
10909 * [I] infoPtr : valid pointer to the listview structure
10910 *
10911 * RETURN:
10912 * None
10913 */
10914 static void LISTVIEW_UpdateSize(LISTVIEW_INFO *infoPtr)
10915 {
10916 TRACE("uView=%d, rcList(old)=%s\n", infoPtr->uView, wine_dbgstr_rect(&infoPtr->rcList));
10917
10918 GetClientRect(infoPtr->hwndSelf, &infoPtr->rcList);
10919
10920 if (infoPtr->uView == LV_VIEW_LIST)
10921 {
10922 /* Apparently the "LIST" style is supposed to have the same
10923 * number of items in a column even if there is no scroll bar.
10924 * Since if a scroll bar already exists then the bottom is already
10925 * reduced, only reduce if the scroll bar does not currently exist.
10926 * The "2" is there to mimic the native control. I think it may be
10927 * related to either padding or edges. (GLA 7/2002)
10928 */
10929 if (!(GetWindowLongW(infoPtr->hwndSelf, GWL_STYLE) & WS_HSCROLL))
10930 infoPtr->rcList.bottom -= GetSystemMetrics(SM_CYHSCROLL);
10931 infoPtr->rcList.bottom = max (infoPtr->rcList.bottom - 2, 0);
10932 }
10933
10934 /* if control created invisible header isn't created */
10935 if (infoPtr->hwndHeader)
10936 {
10937 HDLAYOUT hl;
10938 WINDOWPOS wp;
10939
10940 hl.prc = &infoPtr->rcList;
10941 hl.pwpos = &wp;
10942 SendMessageW( infoPtr->hwndHeader, HDM_LAYOUT, 0, (LPARAM)&hl );
10943 TRACE(" wp.flags=0x%08x, wp=%d,%d (%dx%d)\n", wp.flags, wp.x, wp.y, wp.cx, wp.cy);
10944
10945 if (LISTVIEW_IsHeaderEnabled(infoPtr))
10946 wp.flags |= SWP_SHOWWINDOW;
10947 else
10948 {
10949 wp.flags |= SWP_HIDEWINDOW;
10950 wp.cy = 0;
10951 }
10952
10953 SetWindowPos(wp.hwnd, wp.hwndInsertAfter, wp.x, wp.y, wp.cx, wp.cy, wp.flags);
10954 TRACE(" after SWP wp=%d,%d (%dx%d)\n", wp.x, wp.y, wp.cx, wp.cy);
10955
10956 infoPtr->rcList.top = max(wp.cy, 0);
10957 }
10958 /* extra padding for grid */
10959 if (infoPtr->uView == LV_VIEW_DETAILS && infoPtr->dwLvExStyle & LVS_EX_GRIDLINES)
10960 infoPtr->rcList.top += 2;
10961
10962 TRACE(" rcList=%s\n", wine_dbgstr_rect(&infoPtr->rcList));
10963 }
10964
10965 /***
10966 * DESCRIPTION:
10967 * Processes WM_STYLECHANGED messages.
10968 *
10969 * PARAMETER(S):
10970 * [I] infoPtr : valid pointer to the listview structure
10971 * [I] wStyleType : window style type (normal or extended)
10972 * [I] lpss : window style information
10973 *
10974 * RETURN:
10975 * Zero
10976 */
10977 static INT LISTVIEW_StyleChanged(LISTVIEW_INFO *infoPtr, WPARAM wStyleType,
10978 const STYLESTRUCT *lpss)
10979 {
10980 UINT uNewView = lpss->styleNew & LVS_TYPEMASK;
10981 UINT uOldView = lpss->styleOld & LVS_TYPEMASK;
10982 UINT style;
10983
10984 TRACE("(styletype=%lx, styleOld=0x%08x, styleNew=0x%08x)\n",
10985 wStyleType, lpss->styleOld, lpss->styleNew);
10986
10987 if (wStyleType != GWL_STYLE) return 0;
10988
10989 infoPtr->dwStyle = lpss->styleNew;
10990 map_style_view(infoPtr);
10991
10992 if (((lpss->styleOld & WS_HSCROLL) != 0)&&
10993 ((lpss->styleNew & WS_HSCROLL) == 0))
10994 ShowScrollBar(infoPtr->hwndSelf, SB_HORZ, FALSE);
10995
10996 if (((lpss->styleOld & WS_VSCROLL) != 0)&&
10997 ((lpss->styleNew & WS_VSCROLL) == 0))
10998 ShowScrollBar(infoPtr->hwndSelf, SB_VERT, FALSE);
10999
11000 if (uNewView != uOldView)
11001 {
11002 HIMAGELIST himl;
11003
11004 SendMessageW(infoPtr->hwndEdit, WM_KILLFOCUS, 0, 0);
11005 ShowWindow(infoPtr->hwndHeader, SW_HIDE);
11006
11007 ShowScrollBar(infoPtr->hwndSelf, SB_BOTH, FALSE);
11008 SetRectEmpty(&infoPtr->rcFocus);
11009
11010 himl = (uNewView == LVS_ICON ? infoPtr->himlNormal : infoPtr->himlSmall);
11011 set_icon_size(&infoPtr->iconSize, himl, uNewView != LVS_ICON);
11012
11013 if (uNewView == LVS_REPORT)
11014 {
11015 HDLAYOUT hl;
11016 WINDOWPOS wp;
11017
11018 LISTVIEW_CreateHeader( infoPtr );
11019
11020 hl.prc = &infoPtr->rcList;
11021 hl.pwpos = &wp;
11022 SendMessageW( infoPtr->hwndHeader, HDM_LAYOUT, 0, (LPARAM)&hl );
11023 SetWindowPos(infoPtr->hwndHeader, infoPtr->hwndSelf, wp.x, wp.y, wp.cx, wp.cy,
11024 wp.flags | ((infoPtr->dwStyle & LVS_NOCOLUMNHEADER)
11025 ? SWP_HIDEWINDOW : SWP_SHOWWINDOW));
11026 }
11027
11028 LISTVIEW_UpdateItemSize(infoPtr);
11029 }
11030
11031 if (uNewView == LVS_REPORT || infoPtr->dwLvExStyle & LVS_EX_HEADERINALLVIEWS)
11032 {
11033 if ((lpss->styleOld ^ lpss->styleNew) & LVS_NOCOLUMNHEADER)
11034 {
11035 if (lpss->styleNew & LVS_NOCOLUMNHEADER)
11036 {
11037 /* Turn off the header control */
11038 style = GetWindowLongW(infoPtr->hwndHeader, GWL_STYLE);
11039 TRACE("Hide header control, was 0x%08x\n", style);
11040 SetWindowLongW(infoPtr->hwndHeader, GWL_STYLE, style | HDS_HIDDEN);
11041 } else {
11042 /* Turn on the header control */
11043 if ((style = GetWindowLongW(infoPtr->hwndHeader, GWL_STYLE)) & HDS_HIDDEN)
11044 {
11045 TRACE("Show header control, was 0x%08x\n", style);
11046 SetWindowLongW(infoPtr->hwndHeader, GWL_STYLE, (style & ~HDS_HIDDEN) | WS_VISIBLE);
11047 }
11048 }
11049 }
11050 }
11051
11052 if ( (uNewView == LVS_ICON || uNewView == LVS_SMALLICON) &&
11053 (uNewView != uOldView || ((lpss->styleNew ^ lpss->styleOld) & LVS_ALIGNMASK)) )
11054 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
11055
11056 /* update the size of the client area */
11057 LISTVIEW_UpdateSize(infoPtr);
11058
11059 /* add scrollbars if needed */
11060 LISTVIEW_UpdateScroll(infoPtr);
11061
11062 /* invalidate client area + erase background */
11063 LISTVIEW_InvalidateList(infoPtr);
11064
11065 return 0;
11066 }
11067
11068 /***
11069 * DESCRIPTION:
11070 * Processes WM_STYLECHANGING messages.
11071 *
11072 * PARAMETER(S):
11073 * [I] wStyleType : window style type (normal or extended)
11074 * [I0] lpss : window style information
11075 *
11076 * RETURN:
11077 * Zero
11078 */
11079 static INT LISTVIEW_StyleChanging(WPARAM wStyleType,
11080 STYLESTRUCT *lpss)
11081 {
11082 TRACE("(styletype=%lx, styleOld=0x%08x, styleNew=0x%08x)\n",
11083 wStyleType, lpss->styleOld, lpss->styleNew);
11084
11085 /* don't forward LVS_OWNERDATA only if not already set to */
11086 if ((lpss->styleNew ^ lpss->styleOld) & LVS_OWNERDATA)
11087 {
11088 if (lpss->styleOld & LVS_OWNERDATA)
11089 lpss->styleNew |= LVS_OWNERDATA;
11090 else
11091 lpss->styleNew &= ~LVS_OWNERDATA;
11092 }
11093
11094 return 0;
11095 }
11096
11097 /***
11098 * DESCRIPTION:
11099 * Processes WM_SHOWWINDOW messages.
11100 *
11101 * PARAMETER(S):
11102 * [I] infoPtr : valid pointer to the listview structure
11103 * [I] bShown : window is being shown (FALSE when hidden)
11104 * [I] iStatus : window show status
11105 *
11106 * RETURN:
11107 * Zero
11108 */
11109 static LRESULT LISTVIEW_ShowWindow(LISTVIEW_INFO *infoPtr, WPARAM bShown, LPARAM iStatus)
11110 {
11111 /* header delayed creation */
11112 if ((infoPtr->uView == LV_VIEW_DETAILS) && bShown)
11113 {
11114 LISTVIEW_CreateHeader(infoPtr);
11115
11116 if (!(LVS_NOCOLUMNHEADER & infoPtr->dwStyle))
11117 ShowWindow(infoPtr->hwndHeader, SW_SHOWNORMAL);
11118 }
11119
11120 return DefWindowProcW(infoPtr->hwndSelf, WM_SHOWWINDOW, bShown, iStatus);
11121 }
11122
11123 /***
11124 * DESCRIPTION:
11125 * Processes CCM_GETVERSION messages.
11126 *
11127 * PARAMETER(S):
11128 * [I] infoPtr : valid pointer to the listview structure
11129 *
11130 * RETURN:
11131 * Current version
11132 */
11133 static inline LRESULT LISTVIEW_GetVersion(const LISTVIEW_INFO *infoPtr)
11134 {
11135 return infoPtr->iVersion;
11136 }
11137
11138 /***
11139 * DESCRIPTION:
11140 * Processes CCM_SETVERSION messages.
11141 *
11142 * PARAMETER(S):
11143 * [I] infoPtr : valid pointer to the listview structure
11144 * [I] iVersion : version to be set
11145 *
11146 * RETURN:
11147 * -1 when requested version is greater than DLL version;
11148 * previous version otherwise
11149 */
11150 static LRESULT LISTVIEW_SetVersion(LISTVIEW_INFO *infoPtr, DWORD iVersion)
11151 {
11152 INT iOldVersion = infoPtr->iVersion;
11153
11154 if (iVersion > COMCTL32_VERSION)
11155 return -1;
11156
11157 infoPtr->iVersion = iVersion;
11158
11159 TRACE("new version %d\n", iVersion);
11160
11161 return iOldVersion;
11162 }
11163
11164 /***
11165 * DESCRIPTION:
11166 * Window procedure of the listview control.
11167 *
11168 */
11169 static LRESULT WINAPI
11170 LISTVIEW_WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
11171 {
11172 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)GetWindowLongPtrW(hwnd, 0);
11173
11174 TRACE("(hwnd=%p uMsg=%x wParam=%lx lParam=%lx)\n", hwnd, uMsg, wParam, lParam);
11175
11176 if (!infoPtr && (uMsg != WM_NCCREATE))
11177 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
11178
11179 switch (uMsg)
11180 {
11181 case LVM_APPROXIMATEVIEWRECT:
11182 return LISTVIEW_ApproximateViewRect(infoPtr, (INT)wParam,
11183 LOWORD(lParam), HIWORD(lParam));
11184 case LVM_ARRANGE:
11185 return LISTVIEW_Arrange(infoPtr, (INT)wParam);
11186
11187 case LVM_CANCELEDITLABEL:
11188 return LISTVIEW_CancelEditLabel(infoPtr);
11189
11190 case LVM_CREATEDRAGIMAGE:
11191 return (LRESULT)LISTVIEW_CreateDragImage(infoPtr, (INT)wParam, (LPPOINT)lParam);
11192
11193 case LVM_DELETEALLITEMS:
11194 return LISTVIEW_DeleteAllItems(infoPtr, FALSE);
11195
11196 case LVM_DELETECOLUMN:
11197 return LISTVIEW_DeleteColumn(infoPtr, (INT)wParam);
11198
11199 case LVM_DELETEITEM:
11200 return LISTVIEW_DeleteItem(infoPtr, (INT)wParam);
11201
11202 case LVM_EDITLABELA:
11203 case LVM_EDITLABELW:
11204 return (LRESULT)LISTVIEW_EditLabelT(infoPtr, (INT)wParam,
11205 uMsg == LVM_EDITLABELW);
11206 /* case LVM_ENABLEGROUPVIEW: */
11207
11208 case LVM_ENSUREVISIBLE:
11209 return LISTVIEW_EnsureVisible(infoPtr, (INT)wParam, (BOOL)lParam);
11210
11211 case LVM_FINDITEMW:
11212 return LISTVIEW_FindItemW(infoPtr, (INT)wParam, (LPLVFINDINFOW)lParam);
11213
11214 case LVM_FINDITEMA:
11215 return LISTVIEW_FindItemA(infoPtr, (INT)wParam, (LPLVFINDINFOA)lParam);
11216
11217 case LVM_GETBKCOLOR:
11218 return infoPtr->clrBk;
11219
11220 /* case LVM_GETBKIMAGE: */
11221
11222 case LVM_GETCALLBACKMASK:
11223 return infoPtr->uCallbackMask;
11224
11225 case LVM_GETCOLUMNA:
11226 case LVM_GETCOLUMNW:
11227 return LISTVIEW_GetColumnT(infoPtr, (INT)wParam, (LPLVCOLUMNW)lParam,
11228 uMsg == LVM_GETCOLUMNW);
11229
11230 case LVM_GETCOLUMNORDERARRAY:
11231 return LISTVIEW_GetColumnOrderArray(infoPtr, (INT)wParam, (LPINT)lParam);
11232
11233 case LVM_GETCOLUMNWIDTH:
11234 return LISTVIEW_GetColumnWidth(infoPtr, (INT)wParam);
11235
11236 case LVM_GETCOUNTPERPAGE:
11237 return LISTVIEW_GetCountPerPage(infoPtr);
11238
11239 case LVM_GETEDITCONTROL:
11240 return (LRESULT)infoPtr->hwndEdit;
11241
11242 case LVM_GETEXTENDEDLISTVIEWSTYLE:
11243 return infoPtr->dwLvExStyle;
11244
11245 /* case LVM_GETGROUPINFO: */
11246
11247 /* case LVM_GETGROUPMETRICS: */
11248
11249 case LVM_GETHEADER:
11250 return (LRESULT)infoPtr->hwndHeader;
11251
11252 case LVM_GETHOTCURSOR:
11253 return (LRESULT)infoPtr->hHotCursor;
11254
11255 case LVM_GETHOTITEM:
11256 return infoPtr->nHotItem;
11257
11258 case LVM_GETHOVERTIME:
11259 return infoPtr->dwHoverTime;
11260
11261 case LVM_GETIMAGELIST:
11262 return (LRESULT)LISTVIEW_GetImageList(infoPtr, (INT)wParam);
11263
11264 /* case LVM_GETINSERTMARK: */
11265
11266 /* case LVM_GETINSERTMARKCOLOR: */
11267
11268 /* case LVM_GETINSERTMARKRECT: */
11269
11270 case LVM_GETISEARCHSTRINGA:
11271 case LVM_GETISEARCHSTRINGW:
11272 FIXME("LVM_GETISEARCHSTRING: unimplemented\n");
11273 return FALSE;
11274
11275 case LVM_GETITEMA:
11276 case LVM_GETITEMW:
11277 return LISTVIEW_GetItemExtT(infoPtr, (LPLVITEMW)lParam, uMsg == LVM_GETITEMW);
11278
11279 case LVM_GETITEMCOUNT:
11280 return infoPtr->nItemCount;
11281
11282 case LVM_GETITEMPOSITION:
11283 return LISTVIEW_GetItemPosition(infoPtr, (INT)wParam, (LPPOINT)lParam);
11284
11285 case LVM_GETITEMRECT:
11286 return LISTVIEW_GetItemRect(infoPtr, (INT)wParam, (LPRECT)lParam);
11287
11288 case LVM_GETITEMSPACING:
11289 return LISTVIEW_GetItemSpacing(infoPtr, (BOOL)wParam);
11290
11291 case LVM_GETITEMSTATE:
11292 return LISTVIEW_GetItemState(infoPtr, (INT)wParam, (UINT)lParam);
11293
11294 case LVM_GETITEMTEXTA:
11295 case LVM_GETITEMTEXTW:
11296 return LISTVIEW_GetItemTextT(infoPtr, (INT)wParam, (LPLVITEMW)lParam,
11297 uMsg == LVM_GETITEMTEXTW);
11298
11299 case LVM_GETNEXTITEM:
11300 return LISTVIEW_GetNextItem(infoPtr, (INT)wParam, LOWORD(lParam));
11301
11302 case LVM_GETNUMBEROFWORKAREAS:
11303 FIXME("LVM_GETNUMBEROFWORKAREAS: unimplemented\n");
11304 return 1;
11305
11306 case LVM_GETORIGIN:
11307 if (!lParam) return FALSE;
11308 if (infoPtr->uView == LV_VIEW_DETAILS ||
11309 infoPtr->uView == LV_VIEW_LIST) return FALSE;
11310 LISTVIEW_GetOrigin(infoPtr, (LPPOINT)lParam);
11311 return TRUE;
11312
11313 /* case LVM_GETOUTLINECOLOR: */
11314
11315 /* case LVM_GETSELECTEDCOLUMN: */
11316
11317 case LVM_GETSELECTEDCOUNT:
11318 return LISTVIEW_GetSelectedCount(infoPtr);
11319
11320 case LVM_GETSELECTIONMARK:
11321 return infoPtr->nSelectionMark;
11322
11323 case LVM_GETSTRINGWIDTHA:
11324 case LVM_GETSTRINGWIDTHW:
11325 return LISTVIEW_GetStringWidthT(infoPtr, (LPCWSTR)lParam,
11326 uMsg == LVM_GETSTRINGWIDTHW);
11327
11328 case LVM_GETSUBITEMRECT:
11329 return LISTVIEW_GetSubItemRect(infoPtr, (UINT)wParam, (LPRECT)lParam);
11330
11331 case LVM_GETTEXTBKCOLOR:
11332 return infoPtr->clrTextBk;
11333
11334 case LVM_GETTEXTCOLOR:
11335 return infoPtr->clrText;
11336
11337 /* case LVM_GETTILEINFO: */
11338
11339 /* case LVM_GETTILEVIEWINFO: */
11340
11341 case LVM_GETTOOLTIPS:
11342 if( !infoPtr->hwndToolTip )
11343 infoPtr->hwndToolTip = COMCTL32_CreateToolTip( hwnd );
11344 return (LRESULT)infoPtr->hwndToolTip;
11345
11346 case LVM_GETTOPINDEX:
11347 return LISTVIEW_GetTopIndex(infoPtr);
11348
11349 case LVM_GETUNICODEFORMAT:
11350 return (infoPtr->notifyFormat == NFR_UNICODE);
11351
11352 case LVM_GETVIEW:
11353 return infoPtr->uView;
11354
11355 case LVM_GETVIEWRECT:
11356 return LISTVIEW_GetViewRect(infoPtr, (LPRECT)lParam);
11357
11358 case LVM_GETWORKAREAS:
11359 FIXME("LVM_GETWORKAREAS: unimplemented\n");
11360 return FALSE;
11361
11362 /* case LVM_HASGROUP: */
11363
11364 case LVM_HITTEST:
11365 return LISTVIEW_HitTest(infoPtr, (LPLVHITTESTINFO)lParam, FALSE, TRUE);
11366
11367 case LVM_INSERTCOLUMNA:
11368 case LVM_INSERTCOLUMNW:
11369 return LISTVIEW_InsertColumnT(infoPtr, (INT)wParam, (LPLVCOLUMNW)lParam,
11370 uMsg == LVM_INSERTCOLUMNW);
11371
11372 /* case LVM_INSERTGROUP: */
11373
11374 /* case LVM_INSERTGROUPSORTED: */
11375
11376 case LVM_INSERTITEMA:
11377 case LVM_INSERTITEMW:
11378 return LISTVIEW_InsertItemT(infoPtr, (LPLVITEMW)lParam, uMsg == LVM_INSERTITEMW);
11379
11380 /* case LVM_INSERTMARKHITTEST: */
11381
11382 /* case LVM_ISGROUPVIEWENABLED: */
11383
11384 case LVM_ISITEMVISIBLE:
11385 return LISTVIEW_IsItemVisible(infoPtr, (INT)wParam);
11386
11387 case LVM_MAPIDTOINDEX:
11388 return LISTVIEW_MapIdToIndex(infoPtr, (UINT)wParam);
11389
11390 case LVM_MAPINDEXTOID:
11391 return LISTVIEW_MapIndexToId(infoPtr, (INT)wParam);
11392
11393 /* case LVM_MOVEGROUP: */
11394
11395 /* case LVM_MOVEITEMTOGROUP: */
11396
11397 case LVM_REDRAWITEMS:
11398 return LISTVIEW_RedrawItems(infoPtr, (INT)wParam, (INT)lParam);
11399
11400 /* case LVM_REMOVEALLGROUPS: */
11401
11402 /* case LVM_REMOVEGROUP: */
11403
11404 case LVM_SCROLL:
11405 return LISTVIEW_Scroll(infoPtr, (INT)wParam, (INT)lParam);
11406
11407 case LVM_SETBKCOLOR:
11408 return LISTVIEW_SetBkColor(infoPtr, (COLORREF)lParam);
11409
11410 /* case LVM_SETBKIMAGE: */
11411
11412 case LVM_SETCALLBACKMASK:
11413 infoPtr->uCallbackMask = (UINT)wParam;
11414 return TRUE;
11415
11416 case LVM_SETCOLUMNA:
11417 case LVM_SETCOLUMNW:
11418 return LISTVIEW_SetColumnT(infoPtr, (INT)wParam, (LPLVCOLUMNW)lParam,
11419 uMsg == LVM_SETCOLUMNW);
11420
11421 case LVM_SETCOLUMNORDERARRAY:
11422 return LISTVIEW_SetColumnOrderArray(infoPtr, (INT)wParam, (LPINT)lParam);
11423
11424 case LVM_SETCOLUMNWIDTH:
11425 return LISTVIEW_SetColumnWidth(infoPtr, (INT)wParam, (short)LOWORD(lParam));
11426
11427 case LVM_SETEXTENDEDLISTVIEWSTYLE:
11428 return LISTVIEW_SetExtendedListViewStyle(infoPtr, (DWORD)wParam, (DWORD)lParam);
11429
11430 /* case LVM_SETGROUPINFO: */
11431
11432 /* case LVM_SETGROUPMETRICS: */
11433
11434 case LVM_SETHOTCURSOR:
11435 return (LRESULT)LISTVIEW_SetHotCursor(infoPtr, (HCURSOR)lParam);
11436
11437 case LVM_SETHOTITEM:
11438 return LISTVIEW_SetHotItem(infoPtr, (INT)wParam);
11439
11440 case LVM_SETHOVERTIME:
11441 return LISTVIEW_SetHoverTime(infoPtr, (DWORD)lParam);
11442
11443 case LVM_SETICONSPACING:
11444 if(lParam == -1)
11445 return LISTVIEW_SetIconSpacing(infoPtr, -1, -1);
11446 return LISTVIEW_SetIconSpacing(infoPtr, LOWORD(lParam), HIWORD(lParam));
11447
11448 case LVM_SETIMAGELIST:
11449 return (LRESULT)LISTVIEW_SetImageList(infoPtr, (INT)wParam, (HIMAGELIST)lParam);
11450
11451 /* case LVM_SETINFOTIP: */
11452
11453 /* case LVM_SETINSERTMARK: */
11454
11455 /* case LVM_SETINSERTMARKCOLOR: */
11456
11457 case LVM_SETITEMA:
11458 case LVM_SETITEMW:
11459 {
11460 if (infoPtr->dwStyle & LVS_OWNERDATA) return FALSE;
11461 return LISTVIEW_SetItemT(infoPtr, (LPLVITEMW)lParam, (uMsg == LVM_SETITEMW));
11462 }
11463
11464 case LVM_SETITEMCOUNT:
11465 return LISTVIEW_SetItemCount(infoPtr, (INT)wParam, (DWORD)lParam);
11466
11467 case LVM_SETITEMPOSITION:
11468 {
11469 POINT pt;
11470 pt.x = (short)LOWORD(lParam);
11471 pt.y = (short)HIWORD(lParam);
11472 return LISTVIEW_SetItemPosition(infoPtr, (INT)wParam, &pt);
11473 }
11474
11475 case LVM_SETITEMPOSITION32:
11476 return LISTVIEW_SetItemPosition(infoPtr, (INT)wParam, (POINT*)lParam);
11477
11478 case LVM_SETITEMSTATE:
11479 return LISTVIEW_SetItemState(infoPtr, (INT)wParam, (LPLVITEMW)lParam);
11480
11481 case LVM_SETITEMTEXTA:
11482 case LVM_SETITEMTEXTW:
11483 return LISTVIEW_SetItemTextT(infoPtr, (INT)wParam, (LPLVITEMW)lParam,
11484 uMsg == LVM_SETITEMTEXTW);
11485
11486 /* case LVM_SETOUTLINECOLOR: */
11487
11488 /* case LVM_SETSELECTEDCOLUMN: */
11489
11490 case LVM_SETSELECTIONMARK:
11491 return LISTVIEW_SetSelectionMark(infoPtr, (INT)lParam);
11492
11493 case LVM_SETTEXTBKCOLOR:
11494 return LISTVIEW_SetTextBkColor(infoPtr, (COLORREF)lParam);
11495
11496 case LVM_SETTEXTCOLOR:
11497 return LISTVIEW_SetTextColor(infoPtr, (COLORREF)lParam);
11498
11499 /* case LVM_SETTILEINFO: */
11500
11501 /* case LVM_SETTILEVIEWINFO: */
11502
11503 /* case LVM_SETTILEWIDTH: */
11504
11505 case LVM_SETTOOLTIPS:
11506 return (LRESULT)LISTVIEW_SetToolTips(infoPtr, (HWND)lParam);
11507
11508 case LVM_SETUNICODEFORMAT:
11509 return LISTVIEW_SetUnicodeFormat(infoPtr, wParam);
11510
11511 case LVM_SETVIEW:
11512 return LISTVIEW_SetView(infoPtr, wParam);
11513
11514 /* case LVM_SETWORKAREAS: */
11515
11516 /* case LVM_SORTGROUPS: */
11517
11518 case LVM_SORTITEMS:
11519 case LVM_SORTITEMSEX:
11520 return LISTVIEW_SortItems(infoPtr, (PFNLVCOMPARE)lParam, wParam,
11521 uMsg == LVM_SORTITEMSEX);
11522 case LVM_SUBITEMHITTEST:
11523 return LISTVIEW_HitTest(infoPtr, (LPLVHITTESTINFO)lParam, TRUE, FALSE);
11524
11525 case LVM_UPDATE:
11526 return LISTVIEW_Update(infoPtr, (INT)wParam);
11527
11528 case CCM_GETVERSION:
11529 return LISTVIEW_GetVersion(infoPtr);
11530
11531 case CCM_SETVERSION:
11532 return LISTVIEW_SetVersion(infoPtr, wParam);
11533
11534 case WM_CHAR:
11535 return LISTVIEW_ProcessLetterKeys( infoPtr, wParam, lParam );
11536
11537 case WM_COMMAND:
11538 return LISTVIEW_Command(infoPtr, wParam, lParam);
11539
11540 case WM_NCCREATE:
11541 return LISTVIEW_NCCreate(hwnd, (LPCREATESTRUCTW)lParam);
11542
11543 case WM_CREATE:
11544 return LISTVIEW_Create(hwnd, (LPCREATESTRUCTW)lParam);
11545
11546 case WM_DESTROY:
11547 return LISTVIEW_Destroy(infoPtr);
11548
11549 case WM_ENABLE:
11550 return LISTVIEW_Enable(infoPtr);
11551
11552 case WM_ERASEBKGND:
11553 return LISTVIEW_EraseBkgnd(infoPtr, (HDC)wParam);
11554
11555 case WM_GETDLGCODE:
11556 return DLGC_WANTCHARS | DLGC_WANTARROWS;
11557
11558 case WM_GETFONT:
11559 return (LRESULT)infoPtr->hFont;
11560
11561 case WM_HSCROLL:
11562 return LISTVIEW_HScroll(infoPtr, (INT)LOWORD(wParam), 0);
11563
11564 case WM_KEYDOWN:
11565 return LISTVIEW_KeyDown(infoPtr, (INT)wParam, (LONG)lParam);
11566
11567 case WM_KILLFOCUS:
11568 return LISTVIEW_KillFocus(infoPtr);
11569
11570 case WM_LBUTTONDBLCLK:
11571 return LISTVIEW_LButtonDblClk(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11572
11573 case WM_LBUTTONDOWN:
11574 return LISTVIEW_LButtonDown(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11575
11576 case WM_LBUTTONUP:
11577 return LISTVIEW_LButtonUp(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11578
11579 case WM_MOUSEMOVE:
11580 return LISTVIEW_MouseMove (infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11581
11582 case WM_MOUSEHOVER:
11583 return LISTVIEW_MouseHover(infoPtr, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11584
11585 case WM_NCDESTROY:
11586 return LISTVIEW_NCDestroy(infoPtr);
11587
11588 case WM_NCPAINT:
11589 return LISTVIEW_NCPaint(infoPtr, (HRGN)wParam);
11590
11591 case WM_NOTIFY:
11592 return LISTVIEW_Notify(infoPtr, (LPNMHDR)lParam);
11593
11594 case WM_NOTIFYFORMAT:
11595 return LISTVIEW_NotifyFormat(infoPtr, (HWND)wParam, (INT)lParam);
11596
11597 case WM_PRINTCLIENT:
11598 return LISTVIEW_PrintClient(infoPtr, (HDC)wParam, (DWORD)lParam);
11599
11600 case WM_PAINT:
11601 return LISTVIEW_WMPaint(infoPtr, (HDC)wParam);
11602
11603 case WM_RBUTTONDBLCLK:
11604 return LISTVIEW_RButtonDblClk(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11605
11606 case WM_RBUTTONDOWN:
11607 return LISTVIEW_RButtonDown(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11608
11609 case WM_SETCURSOR:
11610 return LISTVIEW_SetCursor(infoPtr, wParam, lParam);
11611
11612 case WM_SETFOCUS:
11613 return LISTVIEW_SetFocus(infoPtr, (HWND)wParam);
11614
11615 case WM_SETFONT:
11616 return LISTVIEW_SetFont(infoPtr, (HFONT)wParam, (WORD)lParam);
11617
11618 case WM_SETREDRAW:
11619 return LISTVIEW_SetRedraw(infoPtr, (BOOL)wParam);
11620
11621 case WM_SHOWWINDOW:
11622 return LISTVIEW_ShowWindow(infoPtr, wParam, lParam);
11623
11624 case WM_STYLECHANGED:
11625 return LISTVIEW_StyleChanged(infoPtr, wParam, (LPSTYLESTRUCT)lParam);
11626
11627 case WM_STYLECHANGING:
11628 return LISTVIEW_StyleChanging(wParam, (LPSTYLESTRUCT)lParam);
11629
11630 case WM_SYSCOLORCHANGE:
11631 COMCTL32_RefreshSysColors();
11632 if (infoPtr->bDefaultBkColor)
11633 {
11634 LISTVIEW_SetBkColor(infoPtr, comctl32_color.clrWindow);
11635 infoPtr->bDefaultBkColor = TRUE;
11636 LISTVIEW_InvalidateList(infoPtr);
11637 }
11638 return 0;
11639
11640 /* case WM_TIMER: */
11641 case WM_THEMECHANGED:
11642 return LISTVIEW_ThemeChanged(infoPtr);
11643
11644 case WM_VSCROLL:
11645 return LISTVIEW_VScroll(infoPtr, (INT)LOWORD(wParam), 0);
11646
11647 case WM_MOUSEWHEEL:
11648 if (wParam & (MK_SHIFT | MK_CONTROL))
11649 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
11650 return LISTVIEW_MouseWheel(infoPtr, (short int)HIWORD(wParam));
11651
11652 case WM_WINDOWPOSCHANGED:
11653 if (!(((WINDOWPOS *)lParam)->flags & SWP_NOSIZE))
11654 {
11655 SetWindowPos(infoPtr->hwndSelf, 0, 0, 0, 0, 0, SWP_FRAMECHANGED | SWP_NOACTIVATE |
11656 SWP_NOZORDER | SWP_NOMOVE | SWP_NOSIZE);
11657
11658 if ((infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && (infoPtr->uView == LV_VIEW_DETAILS))
11659 {
11660 if (notify_measureitem(infoPtr)) LISTVIEW_InvalidateList(infoPtr);
11661 }
11662 LISTVIEW_Size(infoPtr, ((WINDOWPOS *)lParam)->cx, ((WINDOWPOS *)lParam)->cy);
11663 }
11664 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
11665
11666 /* case WM_WININICHANGE: */
11667
11668 default:
11669 if ((uMsg >= WM_USER) && (uMsg < WM_APP) && !COMCTL32_IsReflectedMessage(uMsg))
11670 ERR("unknown msg %04x wp=%08lx lp=%08lx\n", uMsg, wParam, lParam);
11671
11672 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
11673 }
11674
11675 }
11676
11677 /***
11678 * DESCRIPTION:
11679 * Registers the window class.
11680 *
11681 * PARAMETER(S):
11682 * None
11683 *
11684 * RETURN:
11685 * None
11686 */
11687 void LISTVIEW_Register(void)
11688 {
11689 WNDCLASSW wndClass;
11690
11691 ZeroMemory(&wndClass, sizeof(WNDCLASSW));
11692 wndClass.style = CS_GLOBALCLASS | CS_DBLCLKS;
11693 wndClass.lpfnWndProc = LISTVIEW_WindowProc;
11694 wndClass.cbClsExtra = 0;
11695 wndClass.cbWndExtra = sizeof(LISTVIEW_INFO *);
11696 wndClass.hCursor = LoadCursorW(0, (LPWSTR)IDC_ARROW);
11697 wndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
11698 wndClass.lpszClassName = WC_LISTVIEWW;
11699 RegisterClassW(&wndClass);
11700 }
11701
11702 /***
11703 * DESCRIPTION:
11704 * Unregisters the window class.
11705 *
11706 * PARAMETER(S):
11707 * None
11708 *
11709 * RETURN:
11710 * None
11711 */
11712 void LISTVIEW_Unregister(void)
11713 {
11714 UnregisterClassW(WC_LISTVIEWW, NULL);
11715 }
11716
11717 /***
11718 * DESCRIPTION:
11719 * Handle any WM_COMMAND messages
11720 *
11721 * PARAMETER(S):
11722 * [I] infoPtr : valid pointer to the listview structure
11723 * [I] wParam : the first message parameter
11724 * [I] lParam : the second message parameter
11725 *
11726 * RETURN:
11727 * Zero.
11728 */
11729 static LRESULT LISTVIEW_Command(LISTVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
11730 {
11731
11732 TRACE("(%p %x %x %lx)\n", infoPtr, HIWORD(wParam), LOWORD(wParam), lParam);
11733
11734 if (!infoPtr->hwndEdit) return 0;
11735
11736 switch (HIWORD(wParam))
11737 {
11738 case EN_UPDATE:
11739 {
11740 /*
11741 * Adjust the edit window size
11742 */
11743 WCHAR buffer[1024];
11744 HDC hdc = GetDC(infoPtr->hwndEdit);
11745 HFONT hFont, hOldFont = 0;
11746 RECT rect;
11747 SIZE sz;
11748
11749 if (!infoPtr->hwndEdit || !hdc) return 0;
11750 GetWindowTextW(infoPtr->hwndEdit, buffer, sizeof(buffer)/sizeof(buffer[0]));
11751 GetWindowRect(infoPtr->hwndEdit, &rect);
11752
11753 /* Select font to get the right dimension of the string */
11754 hFont = (HFONT)SendMessageW(infoPtr->hwndEdit, WM_GETFONT, 0, 0);
11755 if (hFont)
11756 {
11757 hOldFont = SelectObject(hdc, hFont);
11758 }
11759
11760 if (GetTextExtentPoint32W(hdc, buffer, lstrlenW(buffer), &sz))
11761 {
11762 TEXTMETRICW textMetric;
11763
11764 /* Add Extra spacing for the next character */
11765 GetTextMetricsW(hdc, &textMetric);
11766 sz.cx += (textMetric.tmMaxCharWidth * 2);
11767
11768 SetWindowPos(infoPtr->hwndEdit, NULL, 0, 0, sz.cx,
11769 rect.bottom - rect.top, SWP_DRAWFRAME | SWP_NOMOVE | SWP_NOZORDER);
11770 }
11771 if (hFont)
11772 SelectObject(hdc, hOldFont);
11773
11774 ReleaseDC(infoPtr->hwndEdit, hdc);
11775
11776 break;
11777 }
11778 case EN_KILLFOCUS:
11779 {
11780 LISTVIEW_CancelEditLabel(infoPtr);
11781 break;
11782 }
11783
11784 default:
11785 return SendMessageW (infoPtr->hwndNotify, WM_COMMAND, wParam, lParam);
11786 }
11787
11788 return 0;
11789 }