199626b98d50984b35ec7eb15fc0ba2aeb7c571a
[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 "comctl32.h"
138
139 #include <stdio.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 an
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 = GetKeyState(VK_SHIFT) & 0x8000;
3674 WORD wCtrl = GetKeyState(VK_CONTROL) & 0x8000;
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 #ifdef __REACTOS__
4724 if ((!(lvItem.state & LVIS_SELECTED) || !infoPtr->bFocus) && (infoPtr->dwLvExStyle & LVS_EX_TRANSPARENTSHADOWTEXT))
4725 DrawShadowText(hdc, lvItem.pszText, -1, &rcLabel, uFormat, RGB(255, 255, 255), RGB(0, 0, 0), 2, 2);
4726 else
4727 #endif
4728 DrawTextW(hdc, lvItem.pszText, -1, &rcLabel, uFormat);
4729
4730 postpaint:
4731 if (cdsubitemmode & CDRF_NOTIFYPOSTPAINT)
4732 notify_postpaint(infoPtr, &nmlvcd);
4733 return TRUE;
4734 }
4735
4736 /***
4737 * DESCRIPTION:
4738 * Draws listview items when in owner draw mode.
4739 *
4740 * PARAMETER(S):
4741 * [I] infoPtr : valid pointer to the listview structure
4742 * [I] hdc : device context handle
4743 *
4744 * RETURN:
4745 * None
4746 */
4747 static void LISTVIEW_RefreshOwnerDraw(const LISTVIEW_INFO *infoPtr, ITERATOR *i, HDC hdc, DWORD cdmode)
4748 {
4749 UINT uID = (UINT)GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
4750 DWORD cditemmode = CDRF_DODEFAULT;
4751 NMLVCUSTOMDRAW nmlvcd;
4752 POINT Origin, Position;
4753 DRAWITEMSTRUCT dis;
4754 LVITEMW item;
4755
4756 TRACE("()\n");
4757
4758 ZeroMemory(&dis, sizeof(dis));
4759
4760 /* Get scroll info once before loop */
4761 LISTVIEW_GetOrigin(infoPtr, &Origin);
4762
4763 /* iterate through the invalidated rows */
4764 while(iterator_next(i))
4765 {
4766 item.iItem = i->nItem;
4767 item.iSubItem = 0;
4768 item.mask = LVIF_PARAM | LVIF_STATE;
4769 item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
4770 if (!LISTVIEW_GetItemW(infoPtr, &item)) continue;
4771
4772 dis.CtlType = ODT_LISTVIEW;
4773 dis.CtlID = uID;
4774 dis.itemID = item.iItem;
4775 dis.itemAction = ODA_DRAWENTIRE;
4776 dis.itemState = 0;
4777 if (item.state & LVIS_SELECTED) dis.itemState |= ODS_SELECTED;
4778 if (infoPtr->bFocus && (item.state & LVIS_FOCUSED)) dis.itemState |= ODS_FOCUS;
4779 dis.hwndItem = infoPtr->hwndSelf;
4780 dis.hDC = hdc;
4781 LISTVIEW_GetItemOrigin(infoPtr, dis.itemID, &Position);
4782 dis.rcItem.left = Position.x + Origin.x;
4783 dis.rcItem.right = dis.rcItem.left + infoPtr->nItemWidth;
4784 dis.rcItem.top = Position.y + Origin.y;
4785 dis.rcItem.bottom = dis.rcItem.top + infoPtr->nItemHeight;
4786 dis.itemData = item.lParam;
4787
4788 TRACE("item=%s, rcItem=%s\n", debuglvitem_t(&item, TRUE), wine_dbgstr_rect(&dis.rcItem));
4789
4790 /*
4791 * Even if we do not send the CDRF_NOTIFYITEMDRAW we need to fill the nmlvcd
4792 * structure for the rest. of the paint cycle
4793 */
4794 customdraw_fill(&nmlvcd, infoPtr, hdc, &dis.rcItem, &item);
4795 if (cdmode & CDRF_NOTIFYITEMDRAW)
4796 cditemmode = notify_customdraw(infoPtr, CDDS_PREPAINT, &nmlvcd);
4797
4798 if (!(cditemmode & CDRF_SKIPDEFAULT))
4799 {
4800 prepaint_setup (infoPtr, hdc, &nmlvcd, FALSE);
4801 SendMessageW(infoPtr->hwndNotify, WM_DRAWITEM, dis.CtlID, (LPARAM)&dis);
4802 }
4803
4804 if (cditemmode & CDRF_NOTIFYPOSTPAINT)
4805 notify_postpaint(infoPtr, &nmlvcd);
4806 }
4807 }
4808
4809 /***
4810 * DESCRIPTION:
4811 * Draws listview items when in report display mode.
4812 *
4813 * PARAMETER(S):
4814 * [I] infoPtr : valid pointer to the listview structure
4815 * [I] hdc : device context handle
4816 * [I] cdmode : custom draw mode
4817 *
4818 * RETURN:
4819 * None
4820 */
4821 static void LISTVIEW_RefreshReport(LISTVIEW_INFO *infoPtr, ITERATOR *i, HDC hdc, DWORD cdmode)
4822 {
4823 INT rgntype;
4824 RECT rcClip, rcItem;
4825 POINT Origin, Position;
4826 RANGES colRanges;
4827 INT col, index;
4828 ITERATOR j;
4829
4830 TRACE("()\n");
4831
4832 /* figure out what to draw */
4833 rgntype = GetClipBox(hdc, &rcClip);
4834 if (rgntype == NULLREGION) return;
4835
4836 /* Get scroll info once before loop */
4837 LISTVIEW_GetOrigin(infoPtr, &Origin);
4838
4839 colRanges = ranges_create(DPA_GetPtrCount(infoPtr->hdpaColumns));
4840
4841 /* narrow down the columns we need to paint */
4842 for(col = 0; col < DPA_GetPtrCount(infoPtr->hdpaColumns); col++)
4843 {
4844 index = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX, col, 0);
4845
4846 LISTVIEW_GetHeaderRect(infoPtr, index, &rcItem);
4847 if ((rcItem.right + Origin.x >= rcClip.left) && (rcItem.left + Origin.x < rcClip.right))
4848 ranges_additem(colRanges, index);
4849 }
4850 iterator_rangesitems(&j, colRanges);
4851
4852 /* in full row select, we _have_ to draw the main item */
4853 if (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT)
4854 j.nSpecial = 0;
4855
4856 /* iterate through the invalidated rows */
4857 while(iterator_next(i))
4858 {
4859 SelectObject(hdc, infoPtr->hFont);
4860 LISTVIEW_GetItemOrigin(infoPtr, i->nItem, &Position);
4861 Position.y += Origin.y;
4862
4863 /* iterate through the invalidated columns */
4864 while(iterator_next(&j))
4865 {
4866 LISTVIEW_GetHeaderRect(infoPtr, j.nItem, &rcItem);
4867 Position.x = (j.nItem == 0) ? rcItem.left + Origin.x : Origin.x;
4868
4869 if (rgntype == COMPLEXREGION && !((infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT) && j.nItem == 0))
4870 {
4871 rcItem.top = 0;
4872 rcItem.bottom = infoPtr->nItemHeight;
4873 OffsetRect(&rcItem, Origin.x, Position.y);
4874 if (!RectVisible(hdc, &rcItem)) continue;
4875 }
4876
4877 LISTVIEW_DrawItem(infoPtr, hdc, i->nItem, j.nItem, Position, cdmode);
4878 }
4879 }
4880 iterator_destroy(&j);
4881 }
4882
4883 /***
4884 * DESCRIPTION:
4885 * Draws the gridlines if necessary when in report display mode.
4886 *
4887 * PARAMETER(S):
4888 * [I] infoPtr : valid pointer to the listview structure
4889 * [I] hdc : device context handle
4890 *
4891 * RETURN:
4892 * None
4893 */
4894 static void LISTVIEW_RefreshReportGrid(LISTVIEW_INFO *infoPtr, HDC hdc)
4895 {
4896 INT rgntype;
4897 INT y, itemheight;
4898 INT col, index;
4899 HPEN hPen, hOldPen;
4900 RECT rcClip, rcItem = {0};
4901 POINT Origin;
4902 RANGES colRanges;
4903 ITERATOR j;
4904 BOOL rmost = FALSE;
4905
4906 TRACE("()\n");
4907
4908 /* figure out what to draw */
4909 rgntype = GetClipBox(hdc, &rcClip);
4910 if (rgntype == NULLREGION) return;
4911
4912 /* Get scroll info once before loop */
4913 LISTVIEW_GetOrigin(infoPtr, &Origin);
4914
4915 colRanges = ranges_create(DPA_GetPtrCount(infoPtr->hdpaColumns));
4916
4917 /* narrow down the columns we need to paint */
4918 for(col = 0; col < DPA_GetPtrCount(infoPtr->hdpaColumns); col++)
4919 {
4920 index = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX, col, 0);
4921
4922 LISTVIEW_GetHeaderRect(infoPtr, index, &rcItem);
4923 if ((rcItem.right + Origin.x >= rcClip.left) && (rcItem.left + Origin.x < rcClip.right))
4924 ranges_additem(colRanges, index);
4925 }
4926
4927 /* is right most vertical line visible? */
4928 if (DPA_GetPtrCount(infoPtr->hdpaColumns) > 0)
4929 {
4930 index = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX, DPA_GetPtrCount(infoPtr->hdpaColumns) - 1, 0);
4931 LISTVIEW_GetHeaderRect(infoPtr, index, &rcItem);
4932 rmost = (rcItem.right + Origin.x < rcClip.right);
4933 }
4934
4935 if ((hPen = CreatePen( PS_SOLID, 1, comctl32_color.clr3dFace )))
4936 {
4937 hOldPen = SelectObject ( hdc, hPen );
4938
4939 /* draw the vertical lines for the columns */
4940 iterator_rangesitems(&j, colRanges);
4941 while(iterator_next(&j))
4942 {
4943 LISTVIEW_GetHeaderRect(infoPtr, j.nItem, &rcItem);
4944 if (rcItem.left == 0) continue; /* skip leftmost column */
4945 rcItem.left += Origin.x;
4946 rcItem.right += Origin.x;
4947 rcItem.top = infoPtr->rcList.top;
4948 rcItem.bottom = infoPtr->rcList.bottom;
4949 TRACE("vert col=%d, rcItem=%s\n", j.nItem, wine_dbgstr_rect(&rcItem));
4950 MoveToEx (hdc, rcItem.left, rcItem.top, NULL);
4951 LineTo (hdc, rcItem.left, rcItem.bottom);
4952 }
4953 iterator_destroy(&j);
4954 /* draw rightmost grid line if visible */
4955 if (rmost)
4956 {
4957 index = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX,
4958 DPA_GetPtrCount(infoPtr->hdpaColumns) - 1, 0);
4959 LISTVIEW_GetHeaderRect(infoPtr, index, &rcItem);
4960
4961 rcItem.right += Origin.x;
4962
4963 MoveToEx (hdc, rcItem.right, infoPtr->rcList.top, NULL);
4964 LineTo (hdc, rcItem.right, infoPtr->rcList.bottom);
4965 }
4966
4967 /* draw the horizontal lines for the rows */
4968 itemheight = LISTVIEW_CalculateItemHeight(infoPtr);
4969 rcItem.left = infoPtr->rcList.left;
4970 rcItem.right = infoPtr->rcList.right;
4971 for(y = Origin.y > 1 ? Origin.y - 1 : itemheight - 1 + Origin.y % itemheight; y<=infoPtr->rcList.bottom; y+=itemheight)
4972 {
4973 rcItem.bottom = rcItem.top = y;
4974 TRACE("horz rcItem=%s\n", wine_dbgstr_rect(&rcItem));
4975 MoveToEx (hdc, rcItem.left, rcItem.top, NULL);
4976 LineTo (hdc, rcItem.right, rcItem.top);
4977 }
4978
4979 SelectObject( hdc, hOldPen );
4980 DeleteObject( hPen );
4981 }
4982 else
4983 ranges_destroy(colRanges);
4984 }
4985
4986 /***
4987 * DESCRIPTION:
4988 * Draws listview items when in list display mode.
4989 *
4990 * PARAMETER(S):
4991 * [I] infoPtr : valid pointer to the listview structure
4992 * [I] hdc : device context handle
4993 * [I] cdmode : custom draw mode
4994 *
4995 * RETURN:
4996 * None
4997 */
4998 static void LISTVIEW_RefreshList(LISTVIEW_INFO *infoPtr, ITERATOR *i, HDC hdc, DWORD cdmode)
4999 {
5000 POINT Origin, Position;
5001
5002 /* Get scroll info once before loop */
5003 LISTVIEW_GetOrigin(infoPtr, &Origin);
5004
5005 while(iterator_prev(i))
5006 {
5007 SelectObject(hdc, infoPtr->hFont);
5008 LISTVIEW_GetItemOrigin(infoPtr, i->nItem, &Position);
5009 Position.x += Origin.x;
5010 Position.y += Origin.y;
5011
5012 LISTVIEW_DrawItem(infoPtr, hdc, i->nItem, 0, Position, cdmode);
5013 }
5014 }
5015
5016
5017 /***
5018 * DESCRIPTION:
5019 * Draws listview items.
5020 *
5021 * PARAMETER(S):
5022 * [I] infoPtr : valid pointer to the listview structure
5023 * [I] hdc : device context handle
5024 * [I] prcErase : rect to be erased before refresh (may be NULL)
5025 *
5026 * RETURN:
5027 * NoneX
5028 */
5029 static void LISTVIEW_Refresh(LISTVIEW_INFO *infoPtr, HDC hdc, const RECT *prcErase)
5030 {
5031 COLORREF oldTextColor = 0, oldBkColor = 0;
5032 NMLVCUSTOMDRAW nmlvcd;
5033 HFONT hOldFont = 0;
5034 DWORD cdmode;
5035 INT oldBkMode = 0;
5036 RECT rcClient;
5037 ITERATOR i;
5038 HDC hdcOrig = hdc;
5039 HBITMAP hbmp = NULL;
5040 RANGE range;
5041
5042 LISTVIEW_DUMP(infoPtr);
5043
5044 if (infoPtr->dwLvExStyle & LVS_EX_DOUBLEBUFFER) {
5045 TRACE("double buffering\n");
5046
5047 hdc = CreateCompatibleDC(hdcOrig);
5048 if (!hdc) {
5049 ERR("Failed to create DC for backbuffer\n");
5050 return;
5051 }
5052 hbmp = CreateCompatibleBitmap(hdcOrig, infoPtr->rcList.right,
5053 infoPtr->rcList.bottom);
5054 if (!hbmp) {
5055 ERR("Failed to create bitmap for backbuffer\n");
5056 DeleteDC(hdc);
5057 return;
5058 }
5059
5060 SelectObject(hdc, hbmp);
5061 SelectObject(hdc, infoPtr->hFont);
5062
5063 if(GetClipBox(hdcOrig, &rcClient))
5064 IntersectClipRect(hdc, rcClient.left, rcClient.top, rcClient.right, rcClient.bottom);
5065 } else {
5066 /* Save dc values we're gonna trash while drawing
5067 * FIXME: Should be done in LISTVIEW_DrawItem() */
5068 hOldFont = SelectObject(hdc, infoPtr->hFont);
5069 oldBkMode = GetBkMode(hdc);
5070 oldBkColor = GetBkColor(hdc);
5071 oldTextColor = GetTextColor(hdc);
5072 }
5073
5074 infoPtr->bIsDrawing = TRUE;
5075
5076 if (prcErase) {
5077 LISTVIEW_FillBkgnd(infoPtr, hdc, prcErase);
5078 } else if (infoPtr->dwLvExStyle & LVS_EX_DOUBLEBUFFER) {
5079 /* If no erasing was done (usually because RedrawWindow was called
5080 * with RDW_INVALIDATE only) we need to copy the old contents into
5081 * the backbuffer before continuing. */
5082 BitBlt(hdc, infoPtr->rcList.left, infoPtr->rcList.top,
5083 infoPtr->rcList.right - infoPtr->rcList.left,
5084 infoPtr->rcList.bottom - infoPtr->rcList.top,
5085 hdcOrig, infoPtr->rcList.left, infoPtr->rcList.top, SRCCOPY);
5086 }
5087
5088 infoPtr->cditemmode = CDRF_DODEFAULT;
5089
5090 GetClientRect(infoPtr->hwndSelf, &rcClient);
5091 customdraw_fill(&nmlvcd, infoPtr, hdc, &rcClient, 0);
5092 cdmode = notify_customdraw(infoPtr, CDDS_PREPAINT, &nmlvcd);
5093 if (cdmode & CDRF_SKIPDEFAULT) goto enddraw;
5094
5095 /* nothing to draw */
5096 if(infoPtr->nItemCount == 0) goto enddraw;
5097
5098 /* figure out what we need to draw */
5099 iterator_visibleitems(&i, infoPtr, hdc);
5100 range = iterator_range(&i);
5101
5102 /* send cache hint notification */
5103 if (infoPtr->dwStyle & LVS_OWNERDATA)
5104 {
5105 NMLVCACHEHINT nmlv;
5106
5107 ZeroMemory(&nmlv, sizeof(NMLVCACHEHINT));
5108 nmlv.iFrom = range.lower;
5109 nmlv.iTo = range.upper - 1;
5110 notify_hdr(infoPtr, LVN_ODCACHEHINT, &nmlv.hdr);
5111 }
5112
5113 if ((infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && (infoPtr->uView == LV_VIEW_DETAILS))
5114 LISTVIEW_RefreshOwnerDraw(infoPtr, &i, hdc, cdmode);
5115 else
5116 {
5117 if (infoPtr->uView == LV_VIEW_DETAILS)
5118 LISTVIEW_RefreshReport(infoPtr, &i, hdc, cdmode);
5119 else /* LV_VIEW_LIST, LV_VIEW_ICON or LV_VIEW_SMALLICON */
5120 LISTVIEW_RefreshList(infoPtr, &i, hdc, cdmode);
5121
5122 /* if we have a focus rect and it's visible, draw it */
5123 if (infoPtr->bFocus && range.lower <= infoPtr->nFocusedItem &&
5124 (range.upper - 1) >= infoPtr->nFocusedItem)
5125 LISTVIEW_DrawFocusRect(infoPtr, hdc);
5126 }
5127 iterator_destroy(&i);
5128
5129 enddraw:
5130 /* For LVS_EX_GRIDLINES go and draw lines */
5131 /* This includes the case where there were *no* items */
5132 if ((infoPtr->uView == LV_VIEW_DETAILS) && infoPtr->dwLvExStyle & LVS_EX_GRIDLINES)
5133 LISTVIEW_RefreshReportGrid(infoPtr, hdc);
5134
5135 /* Draw marquee rectangle if appropriate */
5136 if (infoPtr->bMarqueeSelect)
5137 {
5138 SetBkColor(hdc, RGB(255, 255, 255));
5139 SetTextColor(hdc, RGB(0, 0, 0));
5140 DrawFocusRect(hdc, &infoPtr->marqueeDrawRect);
5141 }
5142
5143 if (cdmode & CDRF_NOTIFYPOSTPAINT)
5144 notify_postpaint(infoPtr, &nmlvcd);
5145
5146 if(hbmp) {
5147 BitBlt(hdcOrig, infoPtr->rcList.left, infoPtr->rcList.top,
5148 infoPtr->rcList.right - infoPtr->rcList.left,
5149 infoPtr->rcList.bottom - infoPtr->rcList.top,
5150 hdc, infoPtr->rcList.left, infoPtr->rcList.top, SRCCOPY);
5151
5152 DeleteObject(hbmp);
5153 DeleteDC(hdc);
5154 } else {
5155 SelectObject(hdc, hOldFont);
5156 SetBkMode(hdc, oldBkMode);
5157 SetBkColor(hdc, oldBkColor);
5158 SetTextColor(hdc, oldTextColor);
5159 }
5160
5161 infoPtr->bIsDrawing = FALSE;
5162 }
5163
5164
5165 /***
5166 * DESCRIPTION:
5167 * Calculates the approximate width and height of a given number of items.
5168 *
5169 * PARAMETER(S):
5170 * [I] infoPtr : valid pointer to the listview structure
5171 * [I] nItemCount : number of items
5172 * [I] wWidth : width
5173 * [I] wHeight : height
5174 *
5175 * RETURN:
5176 * Returns a DWORD. The width in the low word and the height in high word.
5177 */
5178 static DWORD LISTVIEW_ApproximateViewRect(const LISTVIEW_INFO *infoPtr, INT nItemCount,
5179 WORD wWidth, WORD wHeight)
5180 {
5181 DWORD dwViewRect = 0;
5182
5183 if (nItemCount == -1)
5184 nItemCount = infoPtr->nItemCount;
5185
5186 if (infoPtr->uView == LV_VIEW_LIST)
5187 {
5188 INT nItemCountPerColumn = 1;
5189 INT nColumnCount = 0;
5190
5191 if (wHeight == 0xFFFF)
5192 {
5193 /* use current height */
5194 wHeight = infoPtr->rcList.bottom - infoPtr->rcList.top;
5195 }
5196
5197 if (wHeight < infoPtr->nItemHeight)
5198 wHeight = infoPtr->nItemHeight;
5199
5200 if (nItemCount > 0)
5201 {
5202 if (infoPtr->nItemHeight > 0)
5203 {
5204 nItemCountPerColumn = wHeight / infoPtr->nItemHeight;
5205 if (nItemCountPerColumn == 0)
5206 nItemCountPerColumn = 1;
5207
5208 if (nItemCount % nItemCountPerColumn != 0)
5209 nColumnCount = nItemCount / nItemCountPerColumn;
5210 else
5211 nColumnCount = nItemCount / nItemCountPerColumn + 1;
5212 }
5213 }
5214
5215 /* Microsoft padding magic */
5216 wHeight = nItemCountPerColumn * infoPtr->nItemHeight + 2;
5217 wWidth = nColumnCount * infoPtr->nItemWidth + 2;
5218
5219 dwViewRect = MAKELONG(wWidth, wHeight);
5220 }
5221 else if (infoPtr->uView == LV_VIEW_DETAILS)
5222 {
5223 RECT rcBox;
5224
5225 if (infoPtr->nItemCount > 0)
5226 {
5227 LISTVIEW_GetItemBox(infoPtr, 0, &rcBox);
5228 wWidth = rcBox.right - rcBox.left;
5229 wHeight = (rcBox.bottom - rcBox.top) * nItemCount;
5230 }
5231 else
5232 {
5233 /* use current height and width */
5234 if (wHeight == 0xffff)
5235 wHeight = infoPtr->rcList.bottom - infoPtr->rcList.top;
5236 if (wWidth == 0xffff)
5237 wWidth = infoPtr->rcList.right - infoPtr->rcList.left;
5238 }
5239
5240 dwViewRect = MAKELONG(wWidth, wHeight);
5241 }
5242 else if (infoPtr->uView == LV_VIEW_ICON)
5243 {
5244 UINT rows,cols;
5245 UINT nItemWidth;
5246 UINT nItemHeight;
5247
5248 nItemWidth = infoPtr->iconSpacing.cx;
5249 nItemHeight = infoPtr->iconSpacing.cy;
5250
5251 if (wWidth == 0xffff)
5252 wWidth = infoPtr->rcList.right - infoPtr->rcList.left;
5253
5254 if (wWidth < nItemWidth)
5255 wWidth = nItemWidth;
5256
5257 cols = wWidth / nItemWidth;
5258 if (cols > nItemCount)
5259 cols = nItemCount;
5260 if (cols < 1)
5261 cols = 1;
5262
5263 if (nItemCount)
5264 {
5265 rows = nItemCount / cols;
5266 if (nItemCount % cols)
5267 rows++;
5268 }
5269 else
5270 rows = 0;
5271
5272 wHeight = (nItemHeight * rows)+2;
5273 wWidth = (nItemWidth * cols)+2;
5274
5275 dwViewRect = MAKELONG(wWidth, wHeight);
5276 }
5277 else if (infoPtr->uView == LV_VIEW_SMALLICON)
5278 FIXME("uView == LV_VIEW_SMALLICON: not implemented\n");
5279
5280 return dwViewRect;
5281 }
5282
5283 /***
5284 * DESCRIPTION:
5285 * Cancel edit label with saving item text.
5286 *
5287 * PARAMETER(S):
5288 * [I] infoPtr : valid pointer to the listview structure
5289 *
5290 * RETURN:
5291 * Always returns TRUE.
5292 */
5293 static LRESULT LISTVIEW_CancelEditLabel(LISTVIEW_INFO *infoPtr)
5294 {
5295 if (infoPtr->hwndEdit)
5296 {
5297 /* handle value will be lost after LISTVIEW_EndEditLabelT */
5298 HWND edit = infoPtr->hwndEdit;
5299
5300 LISTVIEW_EndEditLabelT(infoPtr, TRUE, IsWindowUnicode(infoPtr->hwndEdit));
5301 SendMessageW(edit, WM_CLOSE, 0, 0);
5302 }
5303
5304 return TRUE;
5305 }
5306
5307 /***
5308 * DESCRIPTION:
5309 * Create a drag image list for the specified item.
5310 *
5311 * PARAMETER(S):
5312 * [I] infoPtr : valid pointer to the listview structure
5313 * [I] iItem : index of item
5314 * [O] lppt : Upper-left corner of the image
5315 *
5316 * RETURN:
5317 * Returns a handle to the image list if successful, NULL otherwise.
5318 */
5319 static HIMAGELIST LISTVIEW_CreateDragImage(LISTVIEW_INFO *infoPtr, INT iItem, LPPOINT lppt)
5320 {
5321 RECT rcItem;
5322 SIZE size;
5323 POINT pos;
5324 HDC hdc, hdcOrig;
5325 HBITMAP hbmp, hOldbmp;
5326 HFONT hOldFont;
5327 HIMAGELIST dragList = 0;
5328 TRACE("iItem=%d Count=%d\n", iItem, infoPtr->nItemCount);
5329
5330 if (iItem < 0 || iItem >= infoPtr->nItemCount || !lppt)
5331 return 0;
5332
5333 rcItem.left = LVIR_BOUNDS;
5334 if (!LISTVIEW_GetItemRect(infoPtr, iItem, &rcItem))
5335 return 0;
5336
5337 lppt->x = rcItem.left;
5338 lppt->y = rcItem.top;
5339
5340 size.cx = rcItem.right - rcItem.left;
5341 size.cy = rcItem.bottom - rcItem.top;
5342
5343 hdcOrig = GetDC(infoPtr->hwndSelf);
5344 hdc = CreateCompatibleDC(hdcOrig);
5345 hbmp = CreateCompatibleBitmap(hdcOrig, size.cx, size.cy);
5346 hOldbmp = SelectObject(hdc, hbmp);
5347 hOldFont = SelectObject(hdc, infoPtr->hFont);
5348
5349 rcItem.left = rcItem.top = 0;
5350 rcItem.right = size.cx;
5351 rcItem.bottom = size.cy;
5352 FillRect(hdc, &rcItem, infoPtr->hBkBrush);
5353
5354 pos.x = pos.y = 0;
5355 if (LISTVIEW_DrawItem(infoPtr, hdc, iItem, 0, pos, infoPtr->cditemmode))
5356 {
5357 dragList = ImageList_Create(size.cx, size.cy, ILC_COLOR, 10, 10);
5358 SelectObject(hdc, hOldbmp);
5359 ImageList_Add(dragList, hbmp, 0);
5360 }
5361 else
5362 SelectObject(hdc, hOldbmp);
5363
5364 SelectObject(hdc, hOldFont);
5365 DeleteObject(hbmp);
5366 DeleteDC(hdc);
5367 ReleaseDC(infoPtr->hwndSelf, hdcOrig);
5368
5369 TRACE("ret=%p\n", dragList);
5370
5371 return dragList;
5372 }
5373
5374
5375 /***
5376 * DESCRIPTION:
5377 * Removes all listview items and subitems.
5378 *
5379 * PARAMETER(S):
5380 * [I] infoPtr : valid pointer to the listview structure
5381 *
5382 * RETURN:
5383 * SUCCESS : TRUE
5384 * FAILURE : FALSE
5385 */
5386 static BOOL LISTVIEW_DeleteAllItems(LISTVIEW_INFO *infoPtr, BOOL destroy)
5387 {
5388 HDPA hdpaSubItems = NULL;
5389 BOOL suppress = FALSE;
5390 ITEMHDR *hdrItem;
5391 ITEM_INFO *lpItem;
5392 ITEM_ID *lpID;
5393 INT i, j;
5394
5395 TRACE("()\n");
5396
5397 /* we do it directly, to avoid notifications */
5398 ranges_clear(infoPtr->selectionRanges);
5399 infoPtr->nSelectionMark = -1;
5400 infoPtr->nFocusedItem = -1;
5401 SetRectEmpty(&infoPtr->rcFocus);
5402 /* But we are supposed to leave nHotItem as is! */
5403
5404 /* send LVN_DELETEALLITEMS notification */
5405 if (!(infoPtr->dwStyle & LVS_OWNERDATA) || !destroy)
5406 {
5407 NMLISTVIEW nmlv;
5408
5409 memset(&nmlv, 0, sizeof(NMLISTVIEW));
5410 nmlv.iItem = -1;
5411 suppress = notify_listview(infoPtr, LVN_DELETEALLITEMS, &nmlv);
5412 }
5413
5414 for (i = infoPtr->nItemCount - 1; i >= 0; i--)
5415 {
5416 if (!(infoPtr->dwStyle & LVS_OWNERDATA))
5417 {
5418 /* send LVN_DELETEITEM notification, if not suppressed
5419 and if it is not a virtual listview */
5420 if (!suppress) notify_deleteitem(infoPtr, i);
5421 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, i);
5422 lpItem = DPA_GetPtr(hdpaSubItems, 0);
5423 /* free id struct */
5424 j = DPA_GetPtrIndex(infoPtr->hdpaItemIds, lpItem->id);
5425 lpID = DPA_GetPtr(infoPtr->hdpaItemIds, j);
5426 DPA_DeletePtr(infoPtr->hdpaItemIds, j);
5427 Free(lpID);
5428 /* both item and subitem start with ITEMHDR header */
5429 for (j = 0; j < DPA_GetPtrCount(hdpaSubItems); j++)
5430 {
5431 hdrItem = DPA_GetPtr(hdpaSubItems, j);
5432 if (is_text(hdrItem->pszText)) Free(hdrItem->pszText);
5433 Free(hdrItem);
5434 }
5435 DPA_Destroy(hdpaSubItems);
5436 DPA_DeletePtr(infoPtr->hdpaItems, i);
5437 }
5438 DPA_DeletePtr(infoPtr->hdpaPosX, i);
5439 DPA_DeletePtr(infoPtr->hdpaPosY, i);
5440 infoPtr->nItemCount --;
5441 }
5442
5443 if (!destroy)
5444 {
5445 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
5446 LISTVIEW_UpdateScroll(infoPtr);
5447 }
5448 LISTVIEW_InvalidateList(infoPtr);
5449
5450 return TRUE;
5451 }
5452
5453 /***
5454 * DESCRIPTION:
5455 * Scrolls, and updates the columns, when a column is changing width.
5456 *
5457 * PARAMETER(S):
5458 * [I] infoPtr : valid pointer to the listview structure
5459 * [I] nColumn : column to scroll
5460 * [I] dx : amount of scroll, in pixels
5461 *
5462 * RETURN:
5463 * None.
5464 */
5465 static void LISTVIEW_ScrollColumns(LISTVIEW_INFO *infoPtr, INT nColumn, INT dx)
5466 {
5467 COLUMN_INFO *lpColumnInfo;
5468 RECT rcOld, rcCol;
5469 POINT ptOrigin;
5470 INT nCol;
5471 HDITEMW hdi;
5472
5473 if (nColumn < 0 || DPA_GetPtrCount(infoPtr->hdpaColumns) < 1) return;
5474 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, min(nColumn, DPA_GetPtrCount(infoPtr->hdpaColumns) - 1));
5475 rcCol = lpColumnInfo->rcHeader;
5476 if (nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns))
5477 rcCol.left = rcCol.right;
5478
5479 /* adjust the other columns */
5480 hdi.mask = HDI_ORDER;
5481 if (SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, nColumn, (LPARAM)&hdi))
5482 {
5483 INT nOrder = hdi.iOrder;
5484 for (nCol = 0; nCol < DPA_GetPtrCount(infoPtr->hdpaColumns); nCol++)
5485 {
5486 hdi.mask = HDI_ORDER;
5487 SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, nCol, (LPARAM)&hdi);
5488 if (hdi.iOrder >= nOrder) {
5489 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, nCol);
5490 lpColumnInfo->rcHeader.left += dx;
5491 lpColumnInfo->rcHeader.right += dx;
5492 }
5493 }
5494 }
5495
5496 /* do not update screen if not in report mode */
5497 if (!is_redrawing(infoPtr) || infoPtr->uView != LV_VIEW_DETAILS) return;
5498
5499 /* Need to reset the item width when inserting a new column */
5500 infoPtr->nItemWidth += dx;
5501
5502 LISTVIEW_UpdateScroll(infoPtr);
5503 LISTVIEW_GetOrigin(infoPtr, &ptOrigin);
5504
5505 /* scroll to cover the deleted column, and invalidate for redraw */
5506 rcOld = infoPtr->rcList;
5507 rcOld.left = ptOrigin.x + rcCol.left + dx;
5508 ScrollWindowEx(infoPtr->hwndSelf, dx, 0, &rcOld, &rcOld, 0, 0, SW_ERASE | SW_INVALIDATE);
5509 }
5510
5511 /***
5512 * DESCRIPTION:
5513 * Removes a column from the listview control.
5514 *
5515 * PARAMETER(S):
5516 * [I] infoPtr : valid pointer to the listview structure
5517 * [I] nColumn : column index
5518 *
5519 * RETURN:
5520 * SUCCESS : TRUE
5521 * FAILURE : FALSE
5522 */
5523 static BOOL LISTVIEW_DeleteColumn(LISTVIEW_INFO *infoPtr, INT nColumn)
5524 {
5525 RECT rcCol;
5526
5527 TRACE("nColumn=%d\n", nColumn);
5528
5529 if (nColumn < 0 || DPA_GetPtrCount(infoPtr->hdpaColumns) == 0
5530 || nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return FALSE;
5531
5532 /* While the MSDN specifically says that column zero should not be deleted,
5533 what actually happens is that the column itself is deleted but no items or subitems
5534 are removed.
5535 */
5536
5537 LISTVIEW_GetHeaderRect(infoPtr, nColumn, &rcCol);
5538
5539 if (!SendMessageW(infoPtr->hwndHeader, HDM_DELETEITEM, nColumn, 0))
5540 return FALSE;
5541
5542 Free(DPA_GetPtr(infoPtr->hdpaColumns, nColumn));
5543 DPA_DeletePtr(infoPtr->hdpaColumns, nColumn);
5544
5545 if (!(infoPtr->dwStyle & LVS_OWNERDATA) && nColumn)
5546 {
5547 SUBITEM_INFO *lpSubItem, *lpDelItem;
5548 HDPA hdpaSubItems;
5549 INT nItem, nSubItem, i;
5550
5551 for (nItem = 0; nItem < infoPtr->nItemCount; nItem++)
5552 {
5553 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, nItem);
5554 nSubItem = 0;
5555 lpDelItem = 0;
5556 for (i = 1; i < DPA_GetPtrCount(hdpaSubItems); i++)
5557 {
5558 lpSubItem = DPA_GetPtr(hdpaSubItems, i);
5559 if (lpSubItem->iSubItem == nColumn)
5560 {
5561 nSubItem = i;
5562 lpDelItem = lpSubItem;
5563 }
5564 else if (lpSubItem->iSubItem > nColumn)
5565 {
5566 lpSubItem->iSubItem--;
5567 }
5568 }
5569
5570 /* if we found our subitem, zap it */
5571 if (nSubItem > 0)
5572 {
5573 /* free string */
5574 if (is_text(lpDelItem->hdr.pszText))
5575 Free(lpDelItem->hdr.pszText);
5576
5577 /* free item */
5578 Free(lpDelItem);
5579
5580 /* free dpa memory */
5581 DPA_DeletePtr(hdpaSubItems, nSubItem);
5582 }
5583 }
5584 }
5585
5586 /* update the other column info */
5587 LISTVIEW_UpdateItemSize(infoPtr);
5588 if(DPA_GetPtrCount(infoPtr->hdpaColumns) == 0)
5589 LISTVIEW_InvalidateList(infoPtr);
5590 else
5591 LISTVIEW_ScrollColumns(infoPtr, nColumn, -(rcCol.right - rcCol.left));
5592
5593 return TRUE;
5594 }
5595
5596 /***
5597 * DESCRIPTION:
5598 * Invalidates the listview after an item's insertion or deletion.
5599 *
5600 * PARAMETER(S):
5601 * [I] infoPtr : valid pointer to the listview structure
5602 * [I] nItem : item index
5603 * [I] dir : -1 if deleting, 1 if inserting
5604 *
5605 * RETURN:
5606 * None
5607 */
5608 static void LISTVIEW_ScrollOnInsert(LISTVIEW_INFO *infoPtr, INT nItem, INT dir)
5609 {
5610 INT nPerCol, nItemCol, nItemRow;
5611 RECT rcScroll;
5612 POINT Origin;
5613
5614 /* if we don't refresh, what's the point of scrolling? */
5615 if (!is_redrawing(infoPtr)) return;
5616
5617 assert (abs(dir) == 1);
5618
5619 /* arrange icons if autoarrange is on */
5620 if (is_autoarrange(infoPtr))
5621 {
5622 BOOL arrange = TRUE;
5623 if (dir < 0 && nItem >= infoPtr->nItemCount) arrange = FALSE;
5624 if (dir > 0 && nItem == infoPtr->nItemCount - 1) arrange = FALSE;
5625 if (arrange) LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
5626 }
5627
5628 /* scrollbars need updating */
5629 LISTVIEW_UpdateScroll(infoPtr);
5630
5631 /* figure out the item's position */
5632 if (infoPtr->uView == LV_VIEW_DETAILS)
5633 nPerCol = infoPtr->nItemCount + 1;
5634 else if (infoPtr->uView == LV_VIEW_LIST)
5635 nPerCol = LISTVIEW_GetCountPerColumn(infoPtr);
5636 else /* LV_VIEW_ICON, or LV_VIEW_SMALLICON */
5637 return;
5638
5639 nItemCol = nItem / nPerCol;
5640 nItemRow = nItem % nPerCol;
5641 LISTVIEW_GetOrigin(infoPtr, &Origin);
5642
5643 /* move the items below up a slot */
5644 rcScroll.left = nItemCol * infoPtr->nItemWidth;
5645 rcScroll.top = nItemRow * infoPtr->nItemHeight;
5646 rcScroll.right = rcScroll.left + infoPtr->nItemWidth;
5647 rcScroll.bottom = nPerCol * infoPtr->nItemHeight;
5648 OffsetRect(&rcScroll, Origin.x, Origin.y);
5649 TRACE("rcScroll=%s, dx=%d\n", wine_dbgstr_rect(&rcScroll), dir * infoPtr->nItemHeight);
5650 if (IntersectRect(&rcScroll, &rcScroll, &infoPtr->rcList))
5651 {
5652 TRACE("Invalidating rcScroll=%s, rcList=%s\n", wine_dbgstr_rect(&rcScroll), wine_dbgstr_rect(&infoPtr->rcList));
5653 InvalidateRect(infoPtr->hwndSelf, &rcScroll, TRUE);
5654 }
5655
5656 /* report has only that column, so we're done */
5657 if (infoPtr->uView == LV_VIEW_DETAILS) return;
5658
5659 /* now for LISTs, we have to deal with the columns to the right */
5660 rcScroll.left = (nItemCol + 1) * infoPtr->nItemWidth;
5661 rcScroll.top = 0;
5662 rcScroll.right = (infoPtr->nItemCount / nPerCol + 1) * infoPtr->nItemWidth;
5663 rcScroll.bottom = nPerCol * infoPtr->nItemHeight;
5664 OffsetRect(&rcScroll, Origin.x, Origin.y);
5665 if (IntersectRect(&rcScroll, &rcScroll, &infoPtr->rcList))
5666 InvalidateRect(infoPtr->hwndSelf, &rcScroll, TRUE);
5667 }
5668
5669 /***
5670 * DESCRIPTION:
5671 * Removes an item from the listview control.
5672 *
5673 * PARAMETER(S):
5674 * [I] infoPtr : valid pointer to the listview structure
5675 * [I] nItem : item index
5676 *
5677 * RETURN:
5678 * SUCCESS : TRUE
5679 * FAILURE : FALSE
5680 */
5681 static BOOL LISTVIEW_DeleteItem(LISTVIEW_INFO *infoPtr, INT nItem)
5682 {
5683 LVITEMW item;
5684 const BOOL is_icon = (infoPtr->uView == LV_VIEW_SMALLICON || infoPtr->uView == LV_VIEW_ICON);
5685 INT focus = infoPtr->nFocusedItem;
5686
5687 TRACE("(nItem=%d)\n", nItem);
5688
5689 if (nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
5690
5691 /* remove selection, and focus */
5692 item.state = 0;
5693 item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
5694 LISTVIEW_SetItemState(infoPtr, nItem, &item);
5695
5696 /* send LVN_DELETEITEM notification. */
5697 if (!notify_deleteitem(infoPtr, nItem)) return FALSE;
5698
5699 /* we need to do this here, because we'll be deleting stuff */
5700 if (is_icon)
5701 LISTVIEW_InvalidateItem(infoPtr, nItem);
5702
5703 if (!(infoPtr->dwStyle & LVS_OWNERDATA))
5704 {
5705 HDPA hdpaSubItems;
5706 ITEMHDR *hdrItem;
5707 ITEM_INFO *lpItem;
5708 ITEM_ID *lpID;
5709 INT i;
5710
5711 hdpaSubItems = DPA_DeletePtr(infoPtr->hdpaItems, nItem);
5712 lpItem = DPA_GetPtr(hdpaSubItems, 0);
5713
5714 /* free id struct */
5715 i = DPA_GetPtrIndex(infoPtr->hdpaItemIds, lpItem->id);
5716 lpID = DPA_GetPtr(infoPtr->hdpaItemIds, i);
5717 DPA_DeletePtr(infoPtr->hdpaItemIds, i);
5718 Free(lpID);
5719 for (i = 0; i < DPA_GetPtrCount(hdpaSubItems); i++)
5720 {
5721 hdrItem = DPA_GetPtr(hdpaSubItems, i);
5722 if (is_text(hdrItem->pszText)) Free(hdrItem->pszText);
5723 Free(hdrItem);
5724 }
5725 DPA_Destroy(hdpaSubItems);
5726 }
5727
5728 if (is_icon)
5729 {
5730 DPA_DeletePtr(infoPtr->hdpaPosX, nItem);
5731 DPA_DeletePtr(infoPtr->hdpaPosY, nItem);
5732 }
5733
5734 infoPtr->nItemCount--;
5735 LISTVIEW_ShiftIndices(infoPtr, nItem, -1);
5736 LISTVIEW_ShiftFocus(infoPtr, focus, nItem, -1);
5737
5738 /* now is the invalidation fun */
5739 if (!is_icon)
5740 LISTVIEW_ScrollOnInsert(infoPtr, nItem, -1);
5741 return TRUE;
5742 }
5743
5744
5745 /***
5746 * DESCRIPTION:
5747 * Callback implementation for editlabel control
5748 *
5749 * PARAMETER(S):
5750 * [I] infoPtr : valid pointer to the listview structure
5751 * [I] storeText : store edit box text as item text
5752 * [I] isW : TRUE if psxText is Unicode, FALSE if it's ANSI
5753 *
5754 * RETURN:
5755 * SUCCESS : TRUE
5756 * FAILURE : FALSE
5757 */
5758 static BOOL LISTVIEW_EndEditLabelT(LISTVIEW_INFO *infoPtr, BOOL storeText, BOOL isW)
5759 {
5760 HWND hwndSelf = infoPtr->hwndSelf;
5761 WCHAR szDispText[DISP_TEXT_SIZE] = { 0 };
5762 NMLVDISPINFOW dispInfo;
5763 INT editedItem = infoPtr->nEditLabelItem;
5764 BOOL same;
5765 WCHAR *pszText = NULL;
5766 BOOL res;
5767
5768 if (storeText)
5769 {
5770 DWORD len = isW ? GetWindowTextLengthW(infoPtr->hwndEdit) : GetWindowTextLengthA(infoPtr->hwndEdit);
5771
5772 if (len)
5773 {
5774 if ((pszText = Alloc((len+1) * (isW ? sizeof(WCHAR) : sizeof(CHAR)))))
5775 {
5776 if (isW) GetWindowTextW(infoPtr->hwndEdit, pszText, len+1);
5777 else GetWindowTextA(infoPtr->hwndEdit, (CHAR*)pszText, len+1);
5778 }
5779 }
5780 }
5781
5782 TRACE("(pszText=%s, isW=%d)\n", debugtext_t(pszText, isW), isW);
5783
5784 ZeroMemory(&dispInfo, sizeof(dispInfo));
5785 dispInfo.item.mask = LVIF_PARAM | LVIF_STATE | LVIF_TEXT;
5786 dispInfo.item.iItem = editedItem;
5787 dispInfo.item.iSubItem = 0;
5788 dispInfo.item.stateMask = ~0;
5789 dispInfo.item.pszText = szDispText;
5790 dispInfo.item.cchTextMax = DISP_TEXT_SIZE;
5791 if (!LISTVIEW_GetItemT(infoPtr, &dispInfo.item, isW))
5792 {
5793 res = FALSE;
5794 goto cleanup;
5795 }
5796
5797 if (isW)
5798 same = (lstrcmpW(dispInfo.item.pszText, pszText) == 0);
5799 else
5800 {
5801 LPWSTR tmp = textdupTtoW(pszText, FALSE);
5802 same = (lstrcmpW(dispInfo.item.pszText, tmp) == 0);
5803 textfreeT(tmp, FALSE);
5804 }
5805
5806 /* add the text from the edit in */
5807 dispInfo.item.mask |= LVIF_TEXT;
5808 dispInfo.item.pszText = same ? NULL : pszText;
5809 dispInfo.item.cchTextMax = textlenT(dispInfo.item.pszText, isW);
5810
5811 /* Do we need to update the Item Text */
5812 res = notify_dispinfoT(infoPtr, LVN_ENDLABELEDITW, &dispInfo, isW);
5813
5814 infoPtr->nEditLabelItem = -1;
5815 infoPtr->hwndEdit = 0;
5816
5817 if (!res) goto cleanup;
5818
5819 if (!IsWindow(hwndSelf))
5820 {
5821 res = FALSE;
5822 goto cleanup;
5823 }
5824 if (!pszText) return TRUE;
5825 if (same)
5826 {
5827 res = TRUE;
5828 goto cleanup;
5829 }
5830
5831 if (!(infoPtr->dwStyle & LVS_OWNERDATA))
5832 {
5833 HDPA hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, editedItem);
5834 ITEM_INFO* lpItem = DPA_GetPtr(hdpaSubItems, 0);
5835 if (lpItem && lpItem->hdr.pszText == LPSTR_TEXTCALLBACKW)
5836 {
5837 LISTVIEW_InvalidateItem(infoPtr, editedItem);
5838 res = TRUE;
5839 goto cleanup;
5840 }
5841 }
5842
5843 ZeroMemory(&dispInfo, sizeof(dispInfo));
5844 dispInfo.item.mask = LVIF_TEXT;
5845 dispInfo.item.iItem = editedItem;
5846 dispInfo.item.iSubItem = 0;
5847 dispInfo.item.pszText = pszText;
5848 dispInfo.item.cchTextMax = textlenT(pszText, isW);
5849 res = LISTVIEW_SetItemT(infoPtr, &dispInfo.item, isW);
5850
5851 cleanup:
5852 Free(pszText);
5853
5854 return res;
5855 }
5856
5857 /***
5858 * DESCRIPTION:
5859 * Subclassed edit control windproc function
5860 *
5861 * PARAMETER(S):
5862 * [I] hwnd : the edit window handle
5863 * [I] uMsg : the message that is to be processed
5864 * [I] wParam : first message parameter
5865 * [I] lParam : second message parameter
5866 * [I] isW : TRUE if input is Unicode
5867 *
5868 * RETURN:
5869 * Zero.
5870 */
5871 static LRESULT EditLblWndProcT(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL isW)
5872 {
5873 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)GetWindowLongPtrW(GetParent(hwnd), 0);
5874 BOOL save = TRUE;
5875
5876 TRACE("(hwnd=%p, uMsg=%x, wParam=%lx, lParam=%lx, isW=%d)\n",
5877 hwnd, uMsg, wParam, lParam, isW);
5878
5879 switch (uMsg)
5880 {
5881 case WM_GETDLGCODE:
5882 return DLGC_WANTARROWS | DLGC_WANTALLKEYS;
5883
5884 case WM_DESTROY:
5885 {
5886 WNDPROC editProc = infoPtr->EditWndProc;
5887 infoPtr->EditWndProc = 0;
5888 SetWindowLongPtrW(hwnd, GWLP_WNDPROC, (DWORD_PTR)editProc);
5889 return CallWindowProcT(editProc, hwnd, uMsg, wParam, lParam, isW);
5890 }
5891
5892 case WM_KEYDOWN:
5893 if (VK_ESCAPE == (INT)wParam)
5894 {
5895 save = FALSE;
5896 break;
5897 }
5898 else if (VK_RETURN == (INT)wParam)
5899 break;
5900
5901 default:
5902 return CallWindowProcT(infoPtr->EditWndProc, hwnd, uMsg, wParam, lParam, isW);
5903 }
5904
5905 /* kill the edit */
5906 if (infoPtr->hwndEdit)
5907 LISTVIEW_EndEditLabelT(infoPtr, save, isW);
5908
5909 SendMessageW(hwnd, WM_CLOSE, 0, 0);
5910 return 0;
5911 }
5912
5913 /***
5914 * DESCRIPTION:
5915 * Subclassed edit control Unicode windproc function
5916 *
5917 * PARAMETER(S):
5918 * [I] hwnd : the edit window handle
5919 * [I] uMsg : the message that is to be processed
5920 * [I] wParam : first message parameter
5921 * [I] lParam : second message parameter
5922 *
5923 * RETURN:
5924 */
5925 static LRESULT CALLBACK EditLblWndProcW(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
5926 {
5927 return EditLblWndProcT(hwnd, uMsg, wParam, lParam, TRUE);
5928 }
5929
5930 /***
5931 * DESCRIPTION:
5932 * Subclassed edit control ANSI windproc function
5933 *
5934 * PARAMETER(S):
5935 * [I] hwnd : the edit window handle
5936 * [I] uMsg : the message that is to be processed
5937 * [I] wParam : first message parameter
5938 * [I] lParam : second message parameter
5939 *
5940 * RETURN:
5941 */
5942 static LRESULT CALLBACK EditLblWndProcA(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
5943 {
5944 return EditLblWndProcT(hwnd, uMsg, wParam, lParam, FALSE);
5945 }
5946
5947 /***
5948 * DESCRIPTION:
5949 * Creates a subclassed edit control
5950 *
5951 * PARAMETER(S):
5952 * [I] infoPtr : valid pointer to the listview structure
5953 * [I] text : initial text for the edit
5954 * [I] style : the window style
5955 * [I] isW : TRUE if input is Unicode
5956 *
5957 * RETURN:
5958 */
5959 static HWND CreateEditLabelT(LISTVIEW_INFO *infoPtr, LPCWSTR text, BOOL isW)
5960 {
5961 static const DWORD style = WS_CHILDWINDOW|WS_CLIPSIBLINGS|ES_LEFT|ES_AUTOHSCROLL|WS_BORDER|WS_VISIBLE;
5962 HINSTANCE hinst = (HINSTANCE)GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_HINSTANCE);
5963 HWND hedit;
5964
5965 TRACE("(%p, text=%s, isW=%d)\n", infoPtr, debugtext_t(text, isW), isW);
5966
5967 /* window will be resized and positioned after LVN_BEGINLABELEDIT */
5968 if (isW)
5969 hedit = CreateWindowW(WC_EDITW, text, style, 0, 0, 0, 0, infoPtr->hwndSelf, 0, hinst, 0);
5970 else
5971 hedit = CreateWindowA(WC_EDITA, (LPCSTR)text, style, 0, 0, 0, 0, infoPtr->hwndSelf, 0, hinst, 0);
5972
5973 if (!hedit) return 0;
5974
5975 infoPtr->EditWndProc = (WNDPROC)
5976 (isW ? SetWindowLongPtrW(hedit, GWLP_WNDPROC, (DWORD_PTR)EditLblWndProcW) :
5977 SetWindowLongPtrA(hedit, GWLP_WNDPROC, (DWORD_PTR)EditLblWndProcA) );
5978
5979 SendMessageW(hedit, WM_SETFONT, (WPARAM)infoPtr->hFont, FALSE);
5980 SendMessageW(hedit, EM_SETLIMITTEXT, DISP_TEXT_SIZE-1, 0);
5981
5982 return hedit;
5983 }
5984
5985 /***
5986 * DESCRIPTION:
5987 * Begin in place editing of specified list view item
5988 *
5989 * PARAMETER(S):
5990 * [I] infoPtr : valid pointer to the listview structure
5991 * [I] nItem : item index
5992 * [I] isW : TRUE if it's a Unicode req, FALSE if ASCII
5993 *
5994 * RETURN:
5995 * SUCCESS : TRUE
5996 * FAILURE : FALSE
5997 */
5998 static HWND LISTVIEW_EditLabelT(LISTVIEW_INFO *infoPtr, INT nItem, BOOL isW)
5999 {
6000 WCHAR disptextW[DISP_TEXT_SIZE] = { 0 };
6001 HWND hwndSelf = infoPtr->hwndSelf;
6002 NMLVDISPINFOW dispInfo;
6003 HFONT hOldFont = NULL;
6004 TEXTMETRICW tm;
6005 RECT rect;
6006 SIZE sz;
6007 HDC hdc;
6008
6009 TRACE("(nItem=%d, isW=%d)\n", nItem, isW);
6010
6011 if (~infoPtr->dwStyle & LVS_EDITLABELS) return 0;
6012
6013 /* remove existing edit box */
6014 if (infoPtr->hwndEdit)
6015 {
6016 SetFocus(infoPtr->hwndSelf);
6017 infoPtr->hwndEdit = 0;
6018 }
6019
6020 if (nItem < 0 || nItem >= infoPtr->nItemCount) return 0;
6021
6022 infoPtr->nEditLabelItem = nItem;
6023
6024 LISTVIEW_SetSelection(infoPtr, nItem);
6025 LISTVIEW_SetItemFocus(infoPtr, nItem);
6026 LISTVIEW_InvalidateItem(infoPtr, nItem);
6027
6028 rect.left = LVIR_LABEL;
6029 if (!LISTVIEW_GetItemRect(infoPtr, nItem, &rect)) return 0;
6030
6031 ZeroMemory(&dispInfo, sizeof(dispInfo));
6032 dispInfo.item.mask = LVIF_PARAM | LVIF_STATE | LVIF_TEXT;
6033 dispInfo.item.iItem = nItem;
6034 dispInfo.item.iSubItem = 0;
6035 dispInfo.item.stateMask = ~0;
6036 dispInfo.item.pszText = disptextW;
6037 dispInfo.item.cchTextMax = DISP_TEXT_SIZE;
6038 if (!LISTVIEW_GetItemT(infoPtr, &dispInfo.item, isW)) return 0;
6039
6040 infoPtr->hwndEdit = CreateEditLabelT(infoPtr, dispInfo.item.pszText, isW);
6041 if (!infoPtr->hwndEdit) return 0;
6042
6043 if (notify_dispinfoT(infoPtr, LVN_BEGINLABELEDITW, &dispInfo, isW))
6044 {
6045 if (!IsWindow(hwndSelf))
6046 return 0;
6047 SendMessageW(infoPtr->hwndEdit, WM_CLOSE, 0, 0);
6048 infoPtr->hwndEdit = 0;
6049 return 0;
6050 }
6051
6052 TRACE("disp text=%s\n", debugtext_t(dispInfo.item.pszText, isW));
6053
6054 /* position and display edit box */
6055 hdc = GetDC(infoPtr->hwndSelf);
6056
6057 /* select the font to get appropriate metric dimensions */
6058 if (infoPtr->hFont)
6059 hOldFont = SelectObject(hdc, infoPtr->hFont);
6060
6061 /* use real edit box content, it could be altered during LVN_BEGINLABELEDIT notification */
6062 GetWindowTextW(infoPtr->hwndEdit, disptextW, DISP_TEXT_SIZE);
6063 TRACE("edit box text=%s\n", debugstr_w(disptextW));
6064
6065 /* get string length in pixels */
6066 GetTextExtentPoint32W(hdc, disptextW, lstrlenW(disptextW), &sz);
6067
6068 /* add extra spacing for the next character */
6069 GetTextMetricsW(hdc, &tm);
6070 sz.cx += tm.tmMaxCharWidth * 2;
6071
6072 if (infoPtr->hFont)
6073 SelectObject(hdc, hOldFont);
6074
6075 ReleaseDC(infoPtr->hwndSelf, hdc);
6076
6077 sz.cy = rect.bottom - rect.top + 2;
6078 rect.left -= 2;
6079 rect.top -= 1;
6080 TRACE("moving edit=(%d,%d)-(%d,%d)\n", rect.left, rect.top, sz.cx, sz.cy);
6081 MoveWindow(infoPtr->hwndEdit, rect.left, rect.top, sz.cx, sz.cy, FALSE);
6082 ShowWindow(infoPtr->hwndEdit, SW_NORMAL);
6083 SetFocus(infoPtr->hwndEdit);
6084 SendMessageW(infoPtr->hwndEdit, EM_SETSEL, 0, -1);
6085 return infoPtr->hwndEdit;
6086 }
6087
6088
6089 /***
6090 * DESCRIPTION:
6091 * Ensures the specified item is visible, scrolling into view if necessary.
6092 *
6093 * PARAMETER(S):
6094 * [I] infoPtr : valid pointer to the listview structure
6095 * [I] nItem : item index
6096 * [I] bPartial : partially or entirely visible
6097 *
6098 * RETURN:
6099 * SUCCESS : TRUE
6100 * FAILURE : FALSE
6101 */
6102 static BOOL LISTVIEW_EnsureVisible(LISTVIEW_INFO *infoPtr, INT nItem, BOOL bPartial)
6103 {
6104 INT nScrollPosHeight = 0;
6105 INT nScrollPosWidth = 0;
6106 INT nHorzAdjust = 0;
6107 INT nVertAdjust = 0;
6108 INT nHorzDiff = 0;
6109 INT nVertDiff = 0;
6110 RECT rcItem, rcTemp;
6111
6112 rcItem.left = LVIR_BOUNDS;
6113 if (!LISTVIEW_GetItemRect(infoPtr, nItem, &rcItem)) return FALSE;
6114
6115 if (bPartial && IntersectRect(&rcTemp, &infoPtr->rcList, &rcItem)) return TRUE;
6116
6117 if (rcItem.left < infoPtr->rcList.left || rcItem.right > infoPtr->rcList.right)
6118 {
6119 /* scroll left/right, but in LV_VIEW_DETAILS mode */
6120 if (infoPtr->uView == LV_VIEW_LIST)
6121 nScrollPosWidth = infoPtr->nItemWidth;
6122 else if ((infoPtr->uView == LV_VIEW_SMALLICON) || (infoPtr->uView == LV_VIEW_ICON))
6123 nScrollPosWidth = 1;
6124
6125 if (rcItem.left < infoPtr->rcList.left)
6126 {
6127 nHorzAdjust = -1;
6128 if (infoPtr->uView != LV_VIEW_DETAILS) nHorzDiff = rcItem.left - infoPtr->rcList.left;
6129 }
6130 else
6131 {
6132 nHorzAdjust = 1;
6133 if (infoPtr->uView != LV_VIEW_DETAILS) nHorzDiff = rcItem.right - infoPtr->rcList.right;
6134 }
6135 }
6136
6137 if (rcItem.top < infoPtr->rcList.top || rcItem.bottom > infoPtr->rcList.bottom)
6138 {
6139 /* scroll up/down, but not in LVS_LIST mode */
6140 if (infoPtr->uView == LV_VIEW_DETAILS)
6141 nScrollPosHeight = infoPtr->nItemHeight;
6142 else if ((infoPtr->uView == LV_VIEW_ICON) || (infoPtr->uView == LV_VIEW_SMALLICON))
6143 nScrollPosHeight = 1;
6144
6145 if (rcItem.top < infoPtr->rcList.top)
6146 {
6147 nVertAdjust = -1;
6148 if (infoPtr->uView != LV_VIEW_LIST) nVertDiff = rcItem.top - infoPtr->rcList.top;
6149 }
6150 else
6151 {
6152 nVertAdjust = 1;
6153 if (infoPtr->uView != LV_VIEW_LIST) nVertDiff = rcItem.bottom - infoPtr->rcList.bottom;
6154 }
6155 }
6156
6157 if (!nScrollPosWidth && !nScrollPosHeight) return TRUE;
6158
6159 if (nScrollPosWidth)
6160 {
6161 INT diff = nHorzDiff / nScrollPosWidth;
6162 if (nHorzDiff % nScrollPosWidth) diff += nHorzAdjust;
6163 LISTVIEW_HScroll(infoPtr, SB_INTERNAL, diff);
6164 }
6165
6166 if (nScrollPosHeight)
6167 {
6168 INT diff = nVertDiff / nScrollPosHeight;
6169 if (nVertDiff % nScrollPosHeight) diff += nVertAdjust;
6170 LISTVIEW_VScroll(infoPtr, SB_INTERNAL, diff);
6171 }
6172
6173 return TRUE;
6174 }
6175
6176 /***
6177 * DESCRIPTION:
6178 * Searches for an item with specific characteristics.
6179 *
6180 * PARAMETER(S):
6181 * [I] hwnd : window handle
6182 * [I] nStart : base item index
6183 * [I] lpFindInfo : item information to look for
6184 *
6185 * RETURN:
6186 * SUCCESS : index of item
6187 * FAILURE : -1
6188 */
6189 static INT LISTVIEW_FindItemW(const LISTVIEW_INFO *infoPtr, INT nStart,
6190 const LVFINDINFOW *lpFindInfo)
6191 {
6192 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
6193 BOOL bWrap = FALSE, bNearest = FALSE;
6194 INT nItem = nStart + 1, nLast = infoPtr->nItemCount, nNearestItem = -1;
6195 ULONG xdist, ydist, dist, mindist = 0x7fffffff;
6196 POINT Position, Destination;
6197 LVITEMW lvItem;
6198
6199 /* Search in virtual listviews should be done by application, not by
6200 listview control, so we just send LVN_ODFINDITEMW and return the result */
6201 if (infoPtr->dwStyle & LVS_OWNERDATA)
6202 {
6203 NMLVFINDITEMW nmlv;
6204
6205 nmlv.iStart = nStart;
6206 nmlv.lvfi = *lpFindInfo;
6207 return notify_hdr(infoPtr, LVN_ODFINDITEMW, (LPNMHDR)&nmlv.hdr);
6208 }
6209
6210 if (!lpFindInfo || nItem < 0) return -1;
6211
6212 lvItem.mask = 0;
6213 if (lpFindInfo->flags & (LVFI_STRING | LVFI_PARTIAL) ||
6214 lpFindInfo->flags & LVFI_SUBSTRING)
6215 {
6216 lvItem.mask |= LVIF_TEXT;
6217 lvItem.pszText = szDispText;
6218 lvItem.cchTextMax = DISP_TEXT_SIZE;
6219 }
6220
6221 if (lpFindInfo->flags & LVFI_WRAP)
6222 bWrap = TRUE;
6223
6224 if ((lpFindInfo->flags & LVFI_NEARESTXY) &&
6225 (infoPtr->uView == LV_VIEW_ICON || infoPtr->uView == LV_VIEW_SMALLICON))
6226 {
6227 POINT Origin;
6228 RECT rcArea;
6229
6230 LISTVIEW_GetOrigin(infoPtr, &Origin);
6231 Destination.x = lpFindInfo->pt.x - Origin.x;
6232 Destination.y = lpFindInfo->pt.y - Origin.y;
6233 switch(lpFindInfo->vkDirection)
6234 {
6235 case VK_DOWN: Destination.y += infoPtr->nItemHeight; break;
6236 case VK_UP: Destination.y -= infoPtr->nItemHeight; break;
6237 case VK_RIGHT: Destination.x += infoPtr->nItemWidth; break;
6238 case VK_LEFT: Destination.x -= infoPtr->nItemWidth; break;
6239 case VK_HOME: Destination.x = Destination.y = 0; break;
6240 case VK_NEXT: Destination.y += infoPtr->rcList.bottom - infoPtr->rcList.top; break;
6241 case VK_PRIOR: Destination.y -= infoPtr->rcList.bottom - infoPtr->rcList.top; break;
6242 case VK_END:
6243 LISTVIEW_GetAreaRect(infoPtr, &rcArea);
6244 Destination.x = rcArea.right;
6245 Destination.y = rcArea.bottom;
6246 break;
6247 default: ERR("Unknown vkDirection=%d\n", lpFindInfo->vkDirection);
6248 }
6249 bNearest = TRUE;
6250 }
6251 else Destination.x = Destination.y = 0;
6252
6253 /* if LVFI_PARAM is specified, all other flags are ignored */
6254 if (lpFindInfo->flags & LVFI_PARAM)
6255 {
6256 lvItem.mask |= LVIF_PARAM;
6257 bNearest = FALSE;
6258 lvItem.mask &= ~LVIF_TEXT;
6259 }
6260
6261 again:
6262 for (; nItem < nLast; nItem++)
6263 {
6264 lvItem.iItem = nItem;
6265 lvItem.iSubItem = 0;
6266 lvItem.pszText = szDispText;
6267 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) continue;
6268
6269 if (lvItem.mask & LVIF_PARAM)
6270 {
6271 if (lpFindInfo->lParam == lvItem.lParam)
6272 return nItem;
6273 else
6274 continue;
6275 }
6276
6277 if (lvItem.mask & LVIF_TEXT)
6278 {
6279 if (lpFindInfo->flags & (LVFI_PARTIAL | LVFI_SUBSTRING))
6280 {
6281 WCHAR *p = strstrW(lvItem.pszText, lpFindInfo->psz);
6282 if (!p || p != lvItem.pszText) continue;
6283 }
6284 else
6285 {
6286 if (lstrcmpW(lvItem.pszText, lpFindInfo->psz) != 0) continue;
6287 }
6288 }
6289
6290 if (!bNearest) return nItem;
6291
6292 /* This is very inefficient. To do a good job here,
6293 * we need a sorted array of (x,y) item positions */
6294 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position);
6295
6296 /* compute the distance^2 to the destination */
6297 xdist = Destination.x - Position.x;
6298 ydist = Destination.y - Position.y;
6299 dist = xdist * xdist + ydist * ydist;
6300
6301 /* remember the distance, and item if it's closer */
6302 if (dist < mindist)
6303 {
6304 mindist = dist;
6305 nNearestItem = nItem;
6306 }
6307 }
6308
6309 if (bWrap)
6310 {
6311 nItem = 0;
6312 nLast = min(nStart + 1, infoPtr->nItemCount);
6313 bWrap = FALSE;
6314 goto again;
6315 }
6316
6317 return nNearestItem;
6318 }
6319
6320 /***
6321 * DESCRIPTION:
6322 * Searches for an item with specific characteristics.
6323 *
6324 * PARAMETER(S):
6325 * [I] hwnd : window handle
6326 * [I] nStart : base item index
6327 * [I] lpFindInfo : item information to look for
6328 *
6329 * RETURN:
6330 * SUCCESS : index of item
6331 * FAILURE : -1
6332 */
6333 static INT LISTVIEW_FindItemA(const LISTVIEW_INFO *infoPtr, INT nStart,
6334 const LVFINDINFOA *lpFindInfo)
6335 {
6336 BOOL hasText = lpFindInfo->flags & (LVFI_STRING | LVFI_PARTIAL) ||
6337 lpFindInfo->flags & LVFI_SUBSTRING;
6338 LVFINDINFOW fiw;
6339 INT res;
6340 LPWSTR strW = NULL;
6341
6342 memcpy(&fiw, lpFindInfo, sizeof(fiw));
6343 if (hasText) fiw.psz = strW = textdupTtoW((LPCWSTR)lpFindInfo->psz, FALSE);
6344 res = LISTVIEW_FindItemW(infoPtr, nStart, &fiw);
6345 textfreeT(strW, FALSE);
6346 return res;
6347 }
6348
6349 /***
6350 * DESCRIPTION:
6351 * Retrieves column attributes.
6352 *
6353 * PARAMETER(S):
6354 * [I] infoPtr : valid pointer to the listview structure
6355 * [I] nColumn : column index
6356 * [IO] lpColumn : column information
6357 * [I] isW : if TRUE, then lpColumn is a LPLVCOLUMNW
6358 * otherwise it is in fact a LPLVCOLUMNA
6359 *
6360 * RETURN:
6361 * SUCCESS : TRUE
6362 * FAILURE : FALSE
6363 */
6364 static BOOL LISTVIEW_GetColumnT(const LISTVIEW_INFO *infoPtr, INT nColumn, LPLVCOLUMNW lpColumn, BOOL isW)
6365 {
6366 COLUMN_INFO *lpColumnInfo;
6367 HDITEMW hdi;
6368
6369 if (!lpColumn || nColumn < 0 || nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return FALSE;
6370 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, nColumn);
6371
6372 /* initialize memory */
6373 ZeroMemory(&hdi, sizeof(hdi));
6374
6375 if (lpColumn->mask & LVCF_TEXT)
6376 {
6377 hdi.mask |= HDI_TEXT;
6378 hdi.pszText = lpColumn->pszText;
6379 hdi.cchTextMax = lpColumn->cchTextMax;
6380 }
6381
6382 if (lpColumn->mask & LVCF_IMAGE)
6383 hdi.mask |= HDI_IMAGE;
6384
6385 if (lpColumn->mask & LVCF_ORDER)
6386 hdi.mask |= HDI_ORDER;
6387
6388 if (lpColumn->mask & LVCF_SUBITEM)
6389 hdi.mask |= HDI_LPARAM;
6390
6391 if (!SendMessageW(infoPtr->hwndHeader, isW ? HDM_GETITEMW : HDM_GETITEMA, nColumn, (LPARAM)&hdi)) return FALSE;
6392
6393 if (lpColumn->mask & LVCF_FMT)
6394 lpColumn->fmt = lpColumnInfo->fmt;
6395
6396 if (lpColumn->mask & LVCF_WIDTH)
6397 lpColumn->cx = lpColumnInfo->rcHeader.right - lpColumnInfo->rcHeader.left;
6398
6399 if (lpColumn->mask & LVCF_IMAGE)
6400 lpColumn->iImage = hdi.iImage;
6401
6402 if (lpColumn->mask & LVCF_ORDER)
6403 lpColumn->iOrder = hdi.iOrder;
6404
6405 if (lpColumn->mask & LVCF_SUBITEM)
6406 lpColumn->iSubItem = hdi.lParam;
6407
6408 if (lpColumn->mask & LVCF_MINWIDTH)
6409 lpColumn->cxMin = lpColumnInfo->cxMin;
6410
6411 return TRUE;
6412 }
6413
6414
6415 static BOOL LISTVIEW_GetColumnOrderArray(const LISTVIEW_INFO *infoPtr, INT iCount, LPINT lpiArray)
6416 {
6417 TRACE("iCount=%d, lpiArray=%p\n", iCount, lpiArray);
6418
6419 if (!lpiArray)
6420 return FALSE;
6421
6422 return SendMessageW(infoPtr->hwndHeader, HDM_GETORDERARRAY, iCount, (LPARAM)lpiArray);
6423 }
6424
6425 /***
6426 * DESCRIPTION:
6427 * Retrieves the column width.
6428 *
6429 * PARAMETER(S):
6430 * [I] infoPtr : valid pointer to the listview structure
6431 * [I] int : column index
6432 *
6433 * RETURN:
6434 * SUCCESS : column width
6435 * FAILURE : zero
6436 */
6437 static INT LISTVIEW_GetColumnWidth(const LISTVIEW_INFO *infoPtr, INT nColumn)
6438 {
6439 INT nColumnWidth = 0;
6440 HDITEMW hdItem;
6441
6442 TRACE("nColumn=%d\n", nColumn);
6443
6444 /* we have a 'column' in LIST and REPORT mode only */
6445 switch(infoPtr->uView)
6446 {
6447 case LV_VIEW_LIST:
6448 nColumnWidth = infoPtr->nItemWidth;
6449 break;
6450 case LV_VIEW_DETAILS:
6451 /* We are not using LISTVIEW_GetHeaderRect as this data is updated only after a HDN_ITEMCHANGED.
6452 * There is an application that subclasses the listview, calls LVM_GETCOLUMNWIDTH in the
6453 * HDN_ITEMCHANGED handler and goes into infinite recursion if it receives old data.
6454 *
6455 * TODO: should we do the same in LVM_GETCOLUMN?
6456 */
6457 hdItem.mask = HDI_WIDTH;
6458 if (!SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, nColumn, (LPARAM)&hdItem))
6459 {
6460 WARN("(%p): HDM_GETITEMW failed for item %d\n", infoPtr->hwndSelf, nColumn);
6461 return 0;
6462 }
6463 nColumnWidth = hdItem.cxy;
6464 break;
6465 }
6466
6467 TRACE("nColumnWidth=%d\n", nColumnWidth);
6468 return nColumnWidth;
6469 }
6470
6471 /***
6472 * DESCRIPTION:
6473 * In list or report display mode, retrieves the number of items that can fit
6474 * vertically in the visible area. In icon or small icon display mode,
6475 * retrieves the total number of visible items.
6476 *
6477 * PARAMETER(S):
6478 * [I] infoPtr : valid pointer to the listview structure
6479 *
6480 * RETURN:
6481 * Number of fully visible items.
6482 */
6483 static INT LISTVIEW_GetCountPerPage(const LISTVIEW_INFO *infoPtr)
6484 {
6485 switch (infoPtr->uView)
6486 {
6487 case LV_VIEW_ICON:
6488 case LV_VIEW_SMALLICON:
6489 return infoPtr->nItemCount;
6490 case LV_VIEW_DETAILS:
6491 return LISTVIEW_GetCountPerColumn(infoPtr);
6492 case LV_VIEW_LIST:
6493 return LISTVIEW_GetCountPerRow(infoPtr) * LISTVIEW_GetCountPerColumn(infoPtr);
6494 }
6495 assert(FALSE);
6496 return 0;
6497 }
6498
6499 /***
6500 * DESCRIPTION:
6501 * Retrieves an image list handle.
6502 *
6503 * PARAMETER(S):
6504 * [I] infoPtr : valid pointer to the listview structure
6505 * [I] nImageList : image list identifier
6506 *
6507 * RETURN:
6508 * SUCCESS : image list handle
6509 * FAILURE : NULL
6510 */
6511 static HIMAGELIST LISTVIEW_GetImageList(const LISTVIEW_INFO *infoPtr, INT nImageList)
6512 {
6513 switch (nImageList)
6514 {
6515 case LVSIL_NORMAL: return infoPtr->himlNormal;
6516 case LVSIL_SMALL: return infoPtr->himlSmall;
6517 case LVSIL_STATE: return infoPtr->himlState;
6518 case LVSIL_GROUPHEADER:
6519 FIXME("LVSIL_GROUPHEADER not supported\n");
6520 break;
6521 default:
6522 WARN("got unknown imagelist index - %d\n", nImageList);
6523 }
6524 return NULL;
6525 }
6526
6527 /* LISTVIEW_GetISearchString */
6528
6529 /***
6530 * DESCRIPTION:
6531 * Retrieves item attributes.
6532 *
6533 * PARAMETER(S):
6534 * [I] hwnd : window handle
6535 * [IO] lpLVItem : item info
6536 * [I] isW : if TRUE, then lpLVItem is a LPLVITEMW,
6537 * if FALSE, then lpLVItem is a LPLVITEMA.
6538 *
6539 * NOTE:
6540 * This is the internal 'GetItem' interface -- it tries to
6541 * be smart and avoid text copies, if possible, by modifying
6542 * lpLVItem->pszText to point to the text string. Please note
6543 * that this is not always possible (e.g. OWNERDATA), so on
6544 * entry you *must* supply valid values for pszText, and cchTextMax.
6545 * The only difference to the documented interface is that upon
6546 * return, you should use *only* the lpLVItem->pszText, rather than
6547 * the buffer pointer you provided on input. Most code already does
6548 * that, so it's not a problem.
6549 * For the two cases when the text must be copied (that is,
6550 * for LVM_GETITEM, and LVM_GETITEMTEXT), use LISTVIEW_GetItemExtT.
6551 *
6552 * RETURN:
6553 * SUCCESS : TRUE
6554 * FAILURE : FALSE
6555 */
6556 static BOOL LISTVIEW_GetItemT(const LISTVIEW_INFO *infoPtr, LPLVITEMW lpLVItem, BOOL isW)
6557 {
6558 ITEMHDR callbackHdr = { LPSTR_TEXTCALLBACKW, I_IMAGECALLBACK };
6559 NMLVDISPINFOW dispInfo;
6560 ITEM_INFO *lpItem;
6561 ITEMHDR* pItemHdr;
6562 HDPA hdpaSubItems;
6563 INT isubitem;
6564
6565 TRACE("(item=%s, isW=%d)\n", debuglvitem_t(lpLVItem, isW), isW);
6566
6567 if (!lpLVItem || lpLVItem->iItem < 0 || lpLVItem->iItem >= infoPtr->nItemCount)
6568 return FALSE;
6569
6570 if (lpLVItem->mask == 0) return TRUE;
6571 TRACE("mask=%x\n", lpLVItem->mask);
6572
6573 /* make a local copy */
6574 isubitem = lpLVItem->iSubItem;
6575
6576 /* a quick optimization if all we're asked is the focus state
6577 * these queries are worth optimising since they are common,
6578 * and can be answered in constant time, without the heavy accesses */
6579 if ( (lpLVItem->mask == LVIF_STATE) && (lpLVItem->stateMask == LVIS_FOCUSED) &&
6580 !(infoPtr->uCallbackMask & LVIS_FOCUSED) )
6581 {
6582 lpLVItem->state = 0;
6583 if (infoPtr->nFocusedItem == lpLVItem->iItem)
6584 lpLVItem->state |= LVIS_FOCUSED;
6585 return TRUE;
6586 }
6587
6588 ZeroMemory(&dispInfo, sizeof(dispInfo));
6589
6590 /* if the app stores all the data, handle it separately */
6591 if (infoPtr->dwStyle & LVS_OWNERDATA)
6592 {
6593 dispInfo.item.state = 0;
6594
6595 /* apparently, we should not callback for lParam in LVS_OWNERDATA */
6596 if ((lpLVItem->mask & ~(LVIF_STATE | LVIF_PARAM)) ||
6597 ((lpLVItem->mask & LVIF_STATE) && (infoPtr->uCallbackMask & lpLVItem->stateMask)))
6598 {
6599 UINT mask = lpLVItem->mask;
6600
6601 /* NOTE: copy only fields which we _know_ are initialized, some apps
6602 * depend on the uninitialized fields being 0 */
6603 dispInfo.item.mask = lpLVItem->mask & ~LVIF_PARAM;
6604 dispInfo.item.iItem = lpLVItem->iItem;
6605 dispInfo.item.iSubItem = isubitem;
6606 if (lpLVItem->mask & LVIF_TEXT)
6607 {
6608 if (lpLVItem->mask & LVIF_NORECOMPUTE)
6609 /* reset mask */
6610 dispInfo.item.mask &= ~(LVIF_TEXT | LVIF_NORECOMPUTE);
6611 else
6612 {
6613 dispInfo.item.pszText = lpLVItem->pszText;
6614 dispInfo.item.cchTextMax = lpLVItem->cchTextMax;
6615 }
6616 }
6617 if (lpLVItem->mask & LVIF_STATE)
6618 dispInfo.item.stateMask = lpLVItem->stateMask & infoPtr->uCallbackMask;
6619 /* could be zeroed on LVIF_NORECOMPUTE case */
6620 if (dispInfo.item.mask)
6621 {
6622 notify_dispinfoT(infoPtr, LVN_GETDISPINFOW, &dispInfo, isW);
6623 dispInfo.item.stateMask = lpLVItem->stateMask;
6624 if (lpLVItem->mask & (LVIF_GROUPID|LVIF_COLUMNS))
6625 {
6626 /* full size structure expected - _WIN32IE >= 0x560 */
6627 *lpLVItem = dispInfo.item;
6628 }
6629 else if (lpLVItem->mask & LVIF_INDENT)
6630 {
6631 /* indent member expected - _WIN32IE >= 0x300 */
6632 memcpy(lpLVItem, &dispInfo.item, offsetof( LVITEMW, iGroupId ));
6633 }
6634 else
6635 {
6636 /* minimal structure expected */
6637 memcpy(lpLVItem, &dispInfo.item, offsetof( LVITEMW, iIndent ));
6638 }
6639 lpLVItem->mask = mask;
6640 TRACE(" getdispinfo(1):lpLVItem=%s\n", debuglvitem_t(lpLVItem, isW));
6641 }
6642 }
6643
6644 /* make sure lParam is zeroed out */
6645 if (lpLVItem->mask & LVIF_PARAM) lpLVItem->lParam = 0;
6646
6647 /* callback marked pointer required here */
6648 if ((lpLVItem->mask & LVIF_TEXT) && (lpLVItem->mask & LVIF_NORECOMPUTE))
6649 lpLVItem->pszText = LPSTR_TEXTCALLBACKW;
6650
6651 /* we store only a little state, so if we're not asked, we're done */
6652 if (!(lpLVItem->mask & LVIF_STATE) || isubitem) return TRUE;
6653
6654 /* if focus is handled by us, report it */
6655 if ( lpLVItem->stateMask & ~infoPtr->uCallbackMask & LVIS_FOCUSED )
6656 {
6657 lpLVItem->state &= ~LVIS_FOCUSED;
6658 if (infoPtr->nFocusedItem == lpLVItem->iItem)
6659 lpLVItem->state |= LVIS_FOCUSED;
6660 }
6661
6662 /* and do the same for selection, if we handle it */
6663 if ( lpLVItem->stateMask & ~infoPtr->uCallbackMask & LVIS_SELECTED )
6664 {
6665 lpLVItem->state &= ~LVIS_SELECTED;
6666 if (ranges_contain(infoPtr->selectionRanges, lpLVItem->iItem))
6667 lpLVItem->state |= LVIS_SELECTED;
6668 }
6669
6670 return TRUE;
6671 }
6672
6673 /* find the item and subitem structures before we proceed */
6674 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, lpLVItem->iItem);
6675 lpItem = DPA_GetPtr(hdpaSubItems, 0);
6676 assert (lpItem);
6677
6678 if (isubitem)
6679 {
6680 SUBITEM_INFO *lpSubItem = LISTVIEW_GetSubItemPtr(hdpaSubItems, isubitem);
6681 pItemHdr = lpSubItem ? &lpSubItem->hdr : &callbackHdr;
6682 if (!lpSubItem)
6683 {
6684 WARN(" iSubItem invalid (%08x), ignored.\n", isubitem);
6685 isubitem = 0;
6686 }
6687 }
6688 else
6689 pItemHdr = &lpItem->hdr;
6690
6691 /* Do we need to query the state from the app? */
6692 if ((lpLVItem->mask & LVIF_STATE) && infoPtr->uCallbackMask && isubitem == 0)
6693 {
6694 dispInfo.item.mask |= LVIF_STATE;
6695 dispInfo.item.stateMask = infoPtr->uCallbackMask;
6696 }
6697
6698 /* Do we need to enquire about the image? */
6699 if ((lpLVItem->mask & LVIF_IMAGE) && pItemHdr->iImage == I_IMAGECALLBACK &&
6700 (isubitem == 0 || (infoPtr->dwLvExStyle & LVS_EX_SUBITEMIMAGES)))
6701 {
6702 dispInfo.item.mask |= LVIF_IMAGE;
6703 dispInfo.item.iImage = I_IMAGECALLBACK;
6704 }
6705
6706 /* Only items support indentation */
6707 if ((lpLVItem->mask & LVIF_INDENT) && lpItem->iIndent == I_INDENTCALLBACK &&
6708 (isubitem == 0))
6709 {
6710 dispInfo.item.mask |= LVIF_INDENT;
6711 dispInfo.item.iIndent = I_INDENTCALLBACK;
6712 }
6713
6714 /* Apps depend on calling back for text if it is NULL or LPSTR_TEXTCALLBACKW */
6715 if ((lpLVItem->mask & LVIF_TEXT) && !(lpLVItem->mask & LVIF_NORECOMPUTE) &&
6716 !is_text(pItemHdr->pszText))
6717 {
6718 dispInfo.item.mask |= LVIF_TEXT;
6719 dispInfo.item.pszText = lpLVItem->pszText;
6720 dispInfo.item.cchTextMax = lpLVItem->cchTextMax;
6721 if (dispInfo.item.pszText && dispInfo.item.cchTextMax > 0)
6722 *dispInfo.item.pszText = '\0';
6723 }
6724
6725 /* If we don't have all the requested info, query the application */
6726 if (dispInfo.item.mask)
6727 {
6728 dispInfo.item.iItem = lpLVItem->iItem;
6729 dispInfo.item.iSubItem = lpLVItem->iSubItem; /* yes: the original subitem */
6730 dispInfo.item.lParam = lpItem->lParam;
6731 notify_dispinfoT(infoPtr, LVN_GETDISPINFOW, &dispInfo, isW);
6732 TRACE(" getdispinfo(2):item=%s\n", debuglvitem_t(&dispInfo.item, isW));
6733 }
6734
6735 /* we should not store values for subitems */
6736 if (isubitem) dispInfo.item.mask &= ~LVIF_DI_SETITEM;
6737
6738 /* Now, handle the iImage field */
6739 if (dispInfo.item.mask & LVIF_IMAGE)
6740 {
6741 lpLVItem->iImage = dispInfo.item.iImage;
6742 if ((dispInfo.item.mask & LVIF_DI_SETITEM) && pItemHdr->iImage == I_IMAGECALLBACK)
6743 pItemHdr->iImage = dispInfo.item.iImage;
6744 }
6745 else if (lpLVItem->mask & LVIF_IMAGE)
6746 {
6747 if(isubitem == 0 || (infoPtr->dwLvExStyle & LVS_EX_SUBITEMIMAGES))
6748 lpLVItem->iImage = pItemHdr->iImage;
6749 else
6750 lpLVItem->iImage = 0;
6751 }
6752
6753 /* The pszText field */
6754 if (dispInfo.item.mask & LVIF_TEXT)
6755 {
6756 if ((dispInfo.item.mask & LVIF_DI_SETITEM) && pItemHdr->pszText)
6757 textsetptrT(&pItemHdr->pszText, dispInfo.item.pszText, isW);
6758
6759 lpLVItem->pszText = dispInfo.item.pszText;
6760 }
6761 else if (lpLVItem->mask & LVIF_TEXT)
6762 {
6763 /* if LVN_GETDISPINFO's disabled with LVIF_NORECOMPUTE return callback placeholder */
6764 if (isW || !is_text(pItemHdr->pszText)) lpLVItem->pszText = pItemHdr->pszText;
6765 else textcpynT(lpLVItem->pszText, isW, pItemHdr->pszText, TRUE, lpLVItem->cchTextMax);
6766 }
6767
6768 /* Next is the lParam field */
6769 if (dispInfo.item.mask & LVIF_PARAM)
6770 {
6771 lpLVItem->lParam = dispInfo.item.lParam;
6772 if ((dispInfo.item.mask & LVIF_DI_SETITEM))
6773 lpItem->lParam = dispInfo.item.lParam;
6774 }
6775 else if (lpLVItem->mask & LVIF_PARAM)
6776 lpLVItem->lParam = lpItem->lParam;
6777
6778 /* if this is a subitem, we're done */
6779 if (isubitem) return TRUE;
6780
6781 /* ... the state field (this one is different due to uCallbackmask) */
6782 if (lpLVItem->mask & LVIF_STATE)
6783 {
6784 lpLVItem->state = lpItem->state & lpLVItem->stateMask;
6785 if (dispInfo.item.mask & LVIF_STATE)
6786 {
6787 lpLVItem->state &= ~dispInfo.item.stateMask;
6788 lpLVItem->state |= (dispInfo.item.state & dispInfo.item.stateMask);
6789 }
6790 if ( lpLVItem->stateMask & ~infoPtr->uCallbackMask & LVIS_FOCUSED )
6791 {
6792 lpLVItem->state &= ~LVIS_FOCUSED;
6793 if (infoPtr->nFocusedItem == lpLVItem->iItem)
6794 lpLVItem->state |= LVIS_FOCUSED;
6795 }
6796 if ( lpLVItem->stateMask & ~infoPtr->uCallbackMask & LVIS_SELECTED )
6797 {
6798 lpLVItem->state &= ~LVIS_SELECTED;
6799 if (ranges_contain(infoPtr->selectionRanges, lpLVItem->iItem))
6800 lpLVItem->state |= LVIS_SELECTED;
6801 }
6802 }
6803
6804 /* and last, but not least, the indent field */
6805 if (dispInfo.item.mask & LVIF_INDENT)
6806 {
6807 lpLVItem->iIndent = dispInfo.item.iIndent;
6808 if ((dispInfo.item.mask & LVIF_DI_SETITEM) && lpItem->iIndent == I_INDENTCALLBACK)
6809 lpItem->iIndent = dispInfo.item.iIndent;
6810 }
6811 else if (lpLVItem->mask & LVIF_INDENT)
6812 {
6813 lpLVItem->iIndent = lpItem->iIndent;
6814 }
6815
6816 return TRUE;
6817 }
6818
6819 /***
6820 * DESCRIPTION:
6821 * Retrieves item attributes.
6822 *
6823 * PARAMETER(S):
6824 * [I] hwnd : window handle
6825 * [IO] lpLVItem : item info
6826 * [I] isW : if TRUE, then lpLVItem is a LPLVITEMW,
6827 * if FALSE, then lpLVItem is a LPLVITEMA.
6828 *
6829 * NOTE:
6830 * This is the external 'GetItem' interface -- it properly copies
6831 * the text in the provided buffer.
6832 *
6833 * RETURN:
6834 * SUCCESS : TRUE
6835 * FAILURE : FALSE
6836 */
6837 static BOOL LISTVIEW_GetItemExtT(const LISTVIEW_INFO *infoPtr, LPLVITEMW lpLVItem, BOOL isW)
6838 {
6839 LPWSTR pszText;
6840 BOOL bResult;
6841
6842 if (!lpLVItem || lpLVItem->iItem < 0 || lpLVItem->iItem >= infoPtr->nItemCount)
6843 return FALSE;
6844
6845 pszText = lpLVItem->pszText;
6846 bResult = LISTVIEW_GetItemT(infoPtr, lpLVItem, isW);
6847 if (bResult && (lpLVItem->mask & LVIF_TEXT) && lpLVItem->pszText != pszText)
6848 {
6849 if (lpLVItem->pszText != LPSTR_TEXTCALLBACKW)
6850 textcpynT(pszText, isW, lpLVItem->pszText, isW, lpLVItem->cchTextMax);
6851 else
6852 pszText = LPSTR_TEXTCALLBACKW;
6853 }
6854 lpLVItem->pszText = pszText;
6855
6856 return bResult;
6857 }
6858
6859
6860 /***
6861 * DESCRIPTION:
6862 * Retrieves the position (upper-left) of the listview control item.
6863 * Note that for LVS_ICON style, the upper-left is that of the icon
6864 * and not the bounding box.
6865 *
6866 * PARAMETER(S):
6867 * [I] infoPtr : valid pointer to the listview structure
6868 * [I] nItem : item index
6869 * [O] lpptPosition : coordinate information
6870 *
6871 * RETURN:
6872 * SUCCESS : TRUE
6873 * FAILURE : FALSE
6874 */
6875 static BOOL LISTVIEW_GetItemPosition(const LISTVIEW_INFO *infoPtr, INT nItem, LPPOINT lpptPosition)
6876 {
6877 POINT Origin;
6878
6879 TRACE("(nItem=%d, lpptPosition=%p)\n", nItem, lpptPosition);
6880
6881 if (!lpptPosition || nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
6882
6883 LISTVIEW_GetOrigin(infoPtr, &Origin);
6884 LISTVIEW_GetItemOrigin(infoPtr, nItem, lpptPosition);
6885
6886 if (infoPtr->uView == LV_VIEW_ICON)
6887 {
6888 lpptPosition->x += (infoPtr->nItemWidth - infoPtr->iconSize.cx) / 2;
6889 lpptPosition->y += ICON_TOP_PADDING;
6890 }
6891 lpptPosition->x += Origin.x;
6892 lpptPosition->y += Origin.y;
6893
6894 TRACE (" lpptPosition=%s\n", wine_dbgstr_point(lpptPosition));
6895 return TRUE;
6896 }
6897
6898
6899 /***
6900 * DESCRIPTION:
6901 * Retrieves the bounding rectangle for a listview control item.
6902 *
6903 * PARAMETER(S):
6904 * [I] infoPtr : valid pointer to the listview structure
6905 * [I] nItem : item index
6906 * [IO] lprc : bounding rectangle coordinates
6907 * lprc->left specifies the portion of the item for which the bounding
6908 * rectangle will be retrieved.
6909 *
6910 * LVIR_BOUNDS Returns the bounding rectangle of the entire item,
6911 * including the icon and label.
6912 * *
6913 * * For LVS_ICON
6914 * * Experiment shows that native control returns:
6915 * * width = min (48, length of text line)
6916 * * .left = position.x - (width - iconsize.cx)/2
6917 * * .right = .left + width
6918 * * height = #lines of text * ntmHeight + icon height + 8
6919 * * .top = position.y - 2
6920 * * .bottom = .top + height
6921 * * separation between items .y = itemSpacing.cy - height
6922 * * .x = itemSpacing.cx - width
6923 * LVIR_ICON Returns the bounding rectangle of the icon or small icon.
6924 * *
6925 * * For LVS_ICON
6926 * * Experiment shows that native control returns:
6927 * * width = iconSize.cx + 16
6928 * * .left = position.x - (width - iconsize.cx)/2
6929 * * .right = .left + width
6930 * * height = iconSize.cy + 4
6931 * * .top = position.y - 2
6932 * * .bottom = .top + height
6933 * * separation between items .y = itemSpacing.cy - height
6934 * * .x = itemSpacing.cx - width
6935 * LVIR_LABEL Returns the bounding rectangle of the item text.
6936 * *
6937 * * For LVS_ICON
6938 * * Experiment shows that native control returns:
6939 * * width = text length
6940 * * .left = position.x - width/2
6941 * * .right = .left + width
6942 * * height = ntmH * linecount + 2
6943 * * .top = position.y + iconSize.cy + 6
6944 * * .bottom = .top + height
6945 * * separation between items .y = itemSpacing.cy - height
6946 * * .x = itemSpacing.cx - width
6947 * LVIR_SELECTBOUNDS Returns the union of the LVIR_ICON and LVIR_LABEL
6948 * rectangles, but excludes columns in report view.
6949 *
6950 * RETURN:
6951 * SUCCESS : TRUE
6952 * FAILURE : FALSE
6953 *
6954 * NOTES
6955 * Note that the bounding rectangle of the label in the LVS_ICON view depends
6956 * upon whether the window has the focus currently and on whether the item
6957 * is the one with the focus. Ensure that the control's record of which
6958 * item has the focus agrees with the items' records.
6959 */
6960 static BOOL LISTVIEW_GetItemRect(const LISTVIEW_INFO *infoPtr, INT nItem, LPRECT lprc)
6961 {
6962 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
6963 BOOL doLabel = TRUE, oversizedBox = FALSE;
6964 POINT Position, Origin;
6965 LVITEMW lvItem;
6966 LONG mode;
6967
6968 TRACE("(hwnd=%p, nItem=%d, lprc=%p)\n", infoPtr->hwndSelf, nItem, lprc);
6969
6970 if (!lprc || nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
6971
6972 LISTVIEW_GetOrigin(infoPtr, &Origin);
6973 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position);
6974
6975 /* Be smart and try to figure out the minimum we have to do */
6976 if (lprc->left == LVIR_ICON) doLabel = FALSE;
6977 if (infoPtr->uView == LV_VIEW_DETAILS && lprc->left == LVIR_BOUNDS) doLabel = FALSE;
6978 if (infoPtr->uView == LV_VIEW_ICON && lprc->left != LVIR_ICON &&
6979 infoPtr->bFocus && LISTVIEW_GetItemState(infoPtr, nItem, LVIS_FOCUSED))
6980 oversizedBox = TRUE;
6981
6982 /* get what we need from the item before hand, so we make
6983 * only one request. This can speed up things, if data
6984 * is stored on the app side */
6985 lvItem.mask = 0;
6986 if (infoPtr->uView == LV_VIEW_DETAILS) lvItem.mask |= LVIF_INDENT;
6987 if (doLabel) lvItem.mask |= LVIF_TEXT;
6988 lvItem.iItem = nItem;
6989 lvItem.iSubItem = 0;
6990 lvItem.pszText = szDispText;
6991 lvItem.cchTextMax = DISP_TEXT_SIZE;
6992 if (lvItem.mask && !LISTVIEW_GetItemW(infoPtr, &lvItem)) return FALSE;
6993 /* we got the state already up, simulate it here, to avoid a reget */
6994 if (infoPtr->uView == LV_VIEW_ICON && (lprc->left != LVIR_ICON))
6995 {
6996 lvItem.mask |= LVIF_STATE;
6997 lvItem.stateMask = LVIS_FOCUSED;
6998 lvItem.state = (oversizedBox ? LVIS_FOCUSED : 0);
6999 }
7000
7001 if (infoPtr->uView == LV_VIEW_DETAILS && (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT) && lprc->left == LVIR_SELECTBOUNDS)
7002 lprc->left = LVIR_BOUNDS;
7003
7004 mode = lprc->left;
7005 switch(lprc->left)
7006 {
7007 case LVIR_ICON:
7008 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, NULL, NULL, lprc, NULL, NULL);
7009 break;
7010
7011 case LVIR_LABEL:
7012 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, NULL, NULL, NULL, NULL, lprc);
7013 break;
7014
7015 case LVIR_BOUNDS:
7016 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, lprc, NULL, NULL, NULL, NULL);
7017 break;
7018
7019 case LVIR_SELECTBOUNDS:
7020 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, NULL, lprc, NULL, NULL, NULL);
7021 break;
7022
7023 default:
7024 WARN("Unknown value: %d\n", lprc->left);
7025 return FALSE;
7026 }
7027
7028 if (infoPtr->uView == LV_VIEW_DETAILS)
7029 {
7030 if (mode != LVIR_BOUNDS)
7031 OffsetRect(lprc, Origin.x + LISTVIEW_GetColumnInfo(infoPtr, 0)->rcHeader.left,
7032 Position.y + Origin.y);
7033 else
7034 OffsetRect(lprc, Origin.x, Position.y + Origin.y);
7035 }
7036 else
7037 OffsetRect(lprc, Position.x + Origin.x, Position.y + Origin.y);
7038
7039 TRACE(" rect=%s\n", wine_dbgstr_rect(lprc));
7040
7041 return TRUE;
7042 }
7043
7044 /***
7045 * DESCRIPTION:
7046 * Retrieves the spacing between listview control items.
7047 *
7048 * PARAMETER(S):
7049 * [I] infoPtr : valid pointer to the listview structure
7050 * [IO] lprc : rectangle to receive the output
7051 * on input, lprc->top = nSubItem
7052 * lprc->left = LVIR_ICON | LVIR_BOUNDS | LVIR_LABEL
7053 *
7054 * NOTE: for subItem = 0, we should return the bounds of the _entire_ item,
7055 * not only those of the first column.
7056 *
7057 * RETURN:
7058 * TRUE: success
7059 * FALSE: failure
7060 */
7061 static BOOL LISTVIEW_GetSubItemRect(const LISTVIEW_INFO *infoPtr, INT item, LPRECT lprc)
7062 {
7063 RECT rect = { 0, 0, 0, 0 };
7064 POINT origin;
7065 INT y;
7066
7067 if (!lprc) return FALSE;
7068
7069 TRACE("(item=%d, subitem=%d, type=%d)\n", item, lprc->top, lprc->left);
7070 /* Subitem of '0' means item itself, and this works for all control view modes */
7071 if (lprc->top == 0)
7072 return LISTVIEW_GetItemRect(infoPtr, item, lprc);
7073
7074 if (infoPtr->uView != LV_VIEW_DETAILS) return FALSE;
7075
7076 LISTVIEW_GetOrigin(infoPtr, &origin);
7077 /* this works for any item index, no matter if it exists or not */
7078 y = item * infoPtr->nItemHeight + origin.y;
7079
7080 if (infoPtr->hwndHeader && SendMessageW(infoPtr->hwndHeader, HDM_GETITEMRECT, lprc->top, (LPARAM)&rect))
7081 {
7082 rect.top = 0;
7083 rect.bottom = infoPtr->nItemHeight;
7084 }
7085 else
7086 {
7087 /* Native implementation is broken for this case and garbage is left for left and right fields,
7088 we zero them to get predictable output */
7089 lprc->left = lprc->right = lprc->top = 0;
7090 lprc->bottom = infoPtr->nItemHeight;
7091 OffsetRect(lprc, origin.x, y);
7092 TRACE("return rect %s\n", wine_dbgstr_rect(lprc));
7093 return TRUE;
7094 }
7095
7096 switch (lprc->left)
7097 {
7098 case LVIR_ICON:
7099 {
7100 /* it doesn't matter if main item actually has an icon, if imagelist is set icon width is returned */
7101 if (infoPtr->himlSmall)
7102 rect.right = rect.left + infoPtr->iconSize.cx;
7103 else
7104 rect.right = rect.left;
7105
7106 rect.bottom = rect.top + infoPtr->iconSize.cy;
7107 break;
7108 }
7109 case LVIR_LABEL:
7110 case LVIR_BOUNDS:
7111 break;
7112
7113 default:
7114 ERR("Unknown bounds=%d\n", lprc->left);
7115 return FALSE;
7116 }
7117
7118 OffsetRect(&rect, origin.x, y);
7119 *lprc = rect;
7120 TRACE("return rect %s\n", wine_dbgstr_rect(lprc));
7121
7122 return TRUE;
7123 }
7124
7125 /***
7126 * DESCRIPTION:
7127 * Retrieves the spacing between listview control items.
7128 *
7129 * PARAMETER(S):
7130 * [I] infoPtr : valid pointer to the listview structure
7131 * [I] bSmall : flag for small or large icon
7132 *
7133 * RETURN:
7134 * Horizontal + vertical spacing
7135 */
7136 static LONG LISTVIEW_GetItemSpacing(const LISTVIEW_INFO *infoPtr, BOOL bSmall)
7137 {
7138 LONG lResult;
7139
7140 if (!bSmall)
7141 {
7142 lResult = MAKELONG(infoPtr->iconSpacing.cx, infoPtr->iconSpacing.cy);
7143 }
7144 else
7145 {
7146 if (infoPtr->uView == LV_VIEW_ICON)
7147 lResult = MAKELONG(DEFAULT_COLUMN_WIDTH, GetSystemMetrics(SM_CXSMICON)+HEIGHT_PADDING);
7148 else
7149 lResult = MAKELONG(infoPtr->nItemWidth, infoPtr->nItemHeight);
7150 }
7151 return lResult;
7152 }
7153
7154 /***
7155 * DESCRIPTION:
7156 * Retrieves the state of a listview control item.
7157 *
7158 * PARAMETER(S):
7159 * [I] infoPtr : valid pointer to the listview structure
7160 * [I] nItem : item index
7161 * [I] uMask : state mask
7162 *
7163 * RETURN:
7164 * State specified by the mask.
7165 */
7166 static UINT LISTVIEW_GetItemState(const LISTVIEW_INFO *infoPtr, INT nItem, UINT uMask)
7167 {
7168 LVITEMW lvItem;
7169
7170 if (nItem < 0 || nItem >= infoPtr->nItemCount) return 0;
7171
7172 lvItem.iItem = nItem;
7173 lvItem.iSubItem = 0;
7174 lvItem.mask = LVIF_STATE;
7175 lvItem.stateMask = uMask;
7176 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) return 0;
7177
7178 return lvItem.state & uMask;
7179 }
7180
7181 /***
7182 * DESCRIPTION:
7183 * Retrieves the text of a listview control item or subitem.
7184 *
7185 * PARAMETER(S):
7186 * [I] hwnd : window handle
7187 * [I] nItem : item index
7188 * [IO] lpLVItem : item information
7189 * [I] isW : TRUE if lpLVItem is Unicode
7190 *
7191 * RETURN:
7192 * SUCCESS : string length
7193 * FAILURE : 0
7194 */
7195 static INT LISTVIEW_GetItemTextT(const LISTVIEW_INFO *infoPtr, INT nItem, LPLVITEMW lpLVItem, BOOL isW)
7196 {
7197 if (!lpLVItem || nItem < 0 || nItem >= infoPtr->nItemCount) return 0;
7198
7199 lpLVItem->mask = LVIF_TEXT;
7200 lpLVItem->iItem = nItem;
7201 if (!LISTVIEW_GetItemExtT(infoPtr, lpLVItem, isW)) return 0;
7202
7203 return textlenT(lpLVItem->pszText, isW);
7204 }
7205
7206 /***
7207 * DESCRIPTION:
7208 * Searches for an item based on properties + relationships.
7209 *
7210 * PARAMETER(S):
7211 * [I] infoPtr : valid pointer to the listview structure
7212 * [I] nItem : item index
7213 * [I] uFlags : relationship flag
7214 *
7215 * RETURN:
7216 * SUCCESS : item index
7217 * FAILURE : -1
7218 */
7219 static INT LISTVIEW_GetNextItem(const LISTVIEW_INFO *infoPtr, INT nItem, UINT uFlags)
7220 {
7221 UINT uMask = 0;
7222 LVFINDINFOW lvFindInfo;
7223 INT nCountPerColumn;
7224 INT nCountPerRow;
7225 INT i;
7226
7227 TRACE("nItem=%d, uFlags=%x, nItemCount=%d\n", nItem, uFlags, infoPtr->nItemCount);
7228 if (nItem < -1 || nItem >= infoPtr->nItemCount) return -1;
7229
7230 ZeroMemory(&lvFindInfo, sizeof(lvFindInfo));
7231
7232 if (uFlags & LVNI_CUT)
7233 uMask |= LVIS_CUT;
7234
7235 if (uFlags & LVNI_DROPHILITED)
7236 uMask |= LVIS_DROPHILITED;
7237
7238 if (uFlags & LVNI_FOCUSED)
7239 uMask |= LVIS_FOCUSED;
7240
7241 if (uFlags & LVNI_SELECTED)
7242 uMask |= LVIS_SELECTED;
7243
7244 /* if we're asked for the focused item, that's only one,
7245 * so it's worth optimizing */
7246 if (uFlags & LVNI_FOCUSED)
7247 {
7248 if ((LISTVIEW_GetItemState(infoPtr, infoPtr->nFocusedItem, uMask) & uMask) != uMask) return -1;
7249 return (infoPtr->nFocusedItem == nItem) ? -1 : infoPtr->nFocusedItem;
7250 }
7251
7252 if (uFlags & LVNI_ABOVE)
7253 {
7254 if ((infoPtr->uView == LV_VIEW_LIST) || (infoPtr->uView == LV_VIEW_DETAILS))
7255 {
7256 while (nItem >= 0)
7257 {
7258 nItem--;
7259 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7260 return nItem;
7261 }
7262 }
7263 else
7264 {
7265 /* Special case for autoarrange - move 'til the top of a list */
7266 if (is_autoarrange(infoPtr))
7267 {
7268 nCountPerRow = LISTVIEW_GetCountPerRow(infoPtr);
7269 while (nItem - nCountPerRow >= 0)
7270 {
7271 nItem -= nCountPerRow;
7272 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7273 return nItem;
7274 }
7275 return -1;
7276 }
7277 lvFindInfo.flags = LVFI_NEARESTXY;
7278 lvFindInfo.vkDirection = VK_UP;
7279 LISTVIEW_GetItemPosition(infoPtr, nItem, &lvFindInfo.pt);
7280 while ((nItem = LISTVIEW_FindItemW(infoPtr, nItem, &lvFindInfo)) != -1)
7281 {
7282 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7283 return nItem;
7284 }
7285 }
7286 }
7287 else if (uFlags & LVNI_BELOW)
7288 {
7289 if ((infoPtr->uView == LV_VIEW_LIST) || (infoPtr->uView == LV_VIEW_DETAILS))
7290 {
7291 while (nItem < infoPtr->nItemCount)
7292 {
7293 nItem++;
7294 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7295 return nItem;
7296 }
7297 }
7298 else
7299 {
7300 /* Special case for autoarrange - move 'til the bottom of a list */
7301 if (is_autoarrange(infoPtr))
7302 {
7303 nCountPerRow = LISTVIEW_GetCountPerRow(infoPtr);
7304 while (nItem + nCountPerRow < infoPtr->nItemCount )
7305 {
7306 nItem += nCountPerRow;
7307 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7308 return nItem;
7309 }
7310 return -1;
7311 }
7312 lvFindInfo.flags = LVFI_NEARESTXY;
7313 lvFindInfo.vkDirection = VK_DOWN;
7314 LISTVIEW_GetItemPosition(infoPtr, nItem, &lvFindInfo.pt);
7315 while ((nItem = LISTVIEW_FindItemW(infoPtr, nItem, &lvFindInfo)) != -1)
7316 {
7317 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7318 return nItem;
7319 }
7320 }
7321 }
7322 else if (uFlags & LVNI_TOLEFT)
7323 {
7324 if (infoPtr->uView == LV_VIEW_LIST)
7325 {
7326 nCountPerColumn = LISTVIEW_GetCountPerColumn(infoPtr);
7327 while (nItem - nCountPerColumn >= 0)
7328 {
7329 nItem -= nCountPerColumn;
7330 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7331 return nItem;
7332 }
7333 }
7334 else if ((infoPtr->uView == LV_VIEW_SMALLICON) || (infoPtr->uView == LV_VIEW_ICON))
7335 {
7336 /* Special case for autoarrange - move 'til the beginning of a row */
7337 if (is_autoarrange(infoPtr))
7338 {
7339 nCountPerRow = LISTVIEW_GetCountPerRow(infoPtr);
7340 while (nItem % nCountPerRow > 0)
7341 {
7342 nItem --;
7343 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7344 return nItem;
7345 }
7346 return -1;
7347 }
7348 lvFindInfo.flags = LVFI_NEARESTXY;
7349 lvFindInfo.vkDirection = VK_LEFT;
7350 LISTVIEW_GetItemPosition(infoPtr, nItem, &lvFindInfo.pt);
7351 while ((nItem = LISTVIEW_FindItemW(infoPtr, nItem, &lvFindInfo)) != -1)
7352 {
7353 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7354 return nItem;
7355 }
7356 }
7357 }
7358 else if (uFlags & LVNI_TORIGHT)
7359 {
7360 if (infoPtr->uView == LV_VIEW_LIST)
7361 {
7362 nCountPerColumn = LISTVIEW_GetCountPerColumn(infoPtr);
7363 while (nItem + nCountPerColumn < infoPtr->nItemCount)
7364 {
7365 nItem += nCountPerColumn;
7366 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7367 return nItem;
7368 }
7369 }
7370 else if ((infoPtr->uView == LV_VIEW_SMALLICON) || (infoPtr->uView == LV_VIEW_ICON))
7371 {
7372 /* Special case for autoarrange - move 'til the end of a row */
7373 if (is_autoarrange(infoPtr))
7374 {
7375 nCountPerRow = LISTVIEW_GetCountPerRow(infoPtr);
7376 while (nItem % nCountPerRow < nCountPerRow - 1 )
7377 {
7378 nItem ++;
7379 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7380 return nItem;
7381 }
7382 return -1;
7383 }
7384 lvFindInfo.flags = LVFI_NEARESTXY;
7385 lvFindInfo.vkDirection = VK_RIGHT;
7386 LISTVIEW_GetItemPosition(infoPtr, nItem, &lvFindInfo.pt);
7387 while ((nItem = LISTVIEW_FindItemW(infoPtr, nItem, &lvFindInfo)) != -1)
7388 {
7389 if ((LISTVIEW_GetItemState(infoPtr, nItem, uMask) & uMask) == uMask)
7390 return nItem;
7391 }
7392 }
7393 }
7394 else
7395 {
7396 nItem++;
7397
7398 /* search by index */
7399 for (i = nItem; i < infoPtr->nItemCount; i++)
7400 {
7401 if ((LISTVIEW_GetItemState(infoPtr, i, uMask) & uMask) == uMask)
7402 return i;
7403 }
7404 }
7405
7406 return -1;
7407 }
7408
7409 /* LISTVIEW_GetNumberOfWorkAreas */
7410
7411 /***
7412 * DESCRIPTION:
7413 * Retrieves the origin coordinates when in icon or small icon display mode.
7414 *
7415 * PARAMETER(S):
7416 * [I] infoPtr : valid pointer to the listview structure
7417 * [O] lpptOrigin : coordinate information
7418 *
7419 * RETURN:
7420 * None.
7421 */
7422 static void LISTVIEW_GetOrigin(const LISTVIEW_INFO *infoPtr, LPPOINT lpptOrigin)
7423 {
7424 INT nHorzPos = 0, nVertPos = 0;
7425 SCROLLINFO scrollInfo;
7426
7427 scrollInfo.cbSize = sizeof(SCROLLINFO);
7428 scrollInfo.fMask = SIF_POS;
7429
7430 if (GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo))
7431 nHorzPos = scrollInfo.nPos;
7432 if (GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo))
7433 nVertPos = scrollInfo.nPos;
7434
7435 TRACE("nHorzPos=%d, nVertPos=%d\n", nHorzPos, nVertPos);
7436
7437 lpptOrigin->x = infoPtr->rcList.left;
7438 lpptOrigin->y = infoPtr->rcList.top;
7439 if (infoPtr->uView == LV_VIEW_LIST)
7440 nHorzPos *= infoPtr->nItemWidth;
7441 else if (infoPtr->uView == LV_VIEW_DETAILS)
7442 nVertPos *= infoPtr->nItemHeight;
7443
7444 lpptOrigin->x -= nHorzPos;
7445 lpptOrigin->y -= nVertPos;
7446
7447 TRACE(" origin=%s\n", wine_dbgstr_point(lpptOrigin));
7448 }
7449
7450 /***
7451 * DESCRIPTION:
7452 * Retrieves the width of a string.
7453 *
7454 * PARAMETER(S):
7455 * [I] hwnd : window handle
7456 * [I] lpszText : text string to process
7457 * [I] isW : TRUE if lpszText is Unicode, FALSE otherwise
7458 *
7459 * RETURN:
7460 * SUCCESS : string width (in pixels)
7461 * FAILURE : zero
7462 */
7463 static INT LISTVIEW_GetStringWidthT(const LISTVIEW_INFO *infoPtr, LPCWSTR lpszText, BOOL isW)
7464 {
7465 SIZE stringSize;
7466
7467 stringSize.cx = 0;
7468 if (is_text(lpszText))
7469 {
7470 HFONT hFont = infoPtr->hFont ? infoPtr->hFont : infoPtr->hDefaultFont;
7471 HDC hdc = GetDC(infoPtr->hwndSelf);
7472 HFONT hOldFont = SelectObject(hdc, hFont);
7473
7474 if (isW)
7475 GetTextExtentPointW(hdc, lpszText, lstrlenW(lpszText), &stringSize);
7476 else
7477 GetTextExtentPointA(hdc, (LPCSTR)lpszText, lstrlenA((LPCSTR)lpszText), &stringSize);
7478 SelectObject(hdc, hOldFont);
7479 ReleaseDC(infoPtr->hwndSelf, hdc);
7480 }
7481 return stringSize.cx;
7482 }
7483
7484 /***
7485 * DESCRIPTION:
7486 * Determines which listview item is located at the specified position.
7487 *
7488 * PARAMETER(S):
7489 * [I] infoPtr : valid pointer to the listview structure
7490 * [IO] lpht : hit test information
7491 * [I] subitem : fill out iSubItem.
7492 * [I] select : return the index only if the hit selects the item
7493 *
7494 * NOTE:
7495 * (mm 20001022): We must not allow iSubItem to be touched, for
7496 * an app might pass only a structure with space up to iItem!
7497 * (MS Office 97 does that for instance in the file open dialog)
7498 *
7499 * RETURN:
7500 * SUCCESS : item index
7501 * FAILURE : -1
7502 */
7503 static INT LISTVIEW_HitTest(const LISTVIEW_INFO *infoPtr, LPLVHITTESTINFO lpht, BOOL subitem, BOOL select)
7504 {
7505 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
7506 RECT rcBox, rcBounds, rcState, rcIcon, rcLabel, rcSearch;
7507 POINT Origin, Position, opt;
7508 BOOL is_fullrow;
7509 LVITEMW lvItem;
7510 ITERATOR i;
7511 INT iItem;
7512
7513 TRACE("(pt=%s, subitem=%d, select=%d)\n", wine_dbgstr_point(&lpht->pt), subitem, select);
7514
7515 lpht->flags = 0;
7516 lpht->iItem = -1;
7517 if (subitem) lpht->iSubItem = 0;
7518
7519 LISTVIEW_GetOrigin(infoPtr, &Origin);
7520
7521 /* set whole list relation flags */
7522 if (subitem && infoPtr->uView == LV_VIEW_DETAILS)
7523 {
7524 /* LVM_SUBITEMHITTEST checks left bound of possible client area */
7525 if (infoPtr->rcList.left > lpht->pt.x && Origin.x < lpht->pt.x)
7526 lpht->flags |= LVHT_TOLEFT;
7527
7528 if (lpht->pt.y < infoPtr->rcList.top && lpht->pt.y >= 0)
7529 opt.y = lpht->pt.y + infoPtr->rcList.top;
7530 else
7531 opt.y = lpht->pt.y;
7532
7533 if (infoPtr->rcList.bottom < opt.y)
7534 lpht->flags |= LVHT_BELOW;
7535 }
7536 else
7537 {
7538 if (infoPtr->rcList.left > lpht->pt.x)
7539 lpht->flags |= LVHT_TOLEFT;
7540 else if (infoPtr->rcList.right < lpht->pt.x)
7541 lpht->flags |= LVHT_TORIGHT;
7542
7543 if (infoPtr->rcList.top > lpht->pt.y)
7544 lpht->flags |= LVHT_ABOVE;
7545 else if (infoPtr->rcList.bottom < lpht->pt.y)
7546 lpht->flags |= LVHT_BELOW;
7547 }
7548
7549 /* even if item is invalid try to find subitem */
7550 if (infoPtr->uView == LV_VIEW_DETAILS && subitem)
7551 {
7552 RECT *pRect;
7553 INT j;
7554
7555 opt.x = lpht->pt.x - Origin.x;
7556
7557 lpht->iSubItem = -1;
7558 for (j = 0; j < DPA_GetPtrCount(infoPtr->hdpaColumns); j++)
7559 {
7560 pRect = &LISTVIEW_GetColumnInfo(infoPtr, j)->rcHeader;
7561
7562 if ((opt.x >= pRect->left) && (opt.x < pRect->right))
7563 {
7564 lpht->iSubItem = j;
7565 break;
7566 }
7567 }
7568 TRACE("lpht->iSubItem=%d\n", lpht->iSubItem);
7569
7570 /* if we're outside horizontal columns bounds there's nothing to test further */
7571 if (lpht->iSubItem == -1)
7572 {
7573 lpht->iItem = -1;
7574 lpht->flags = LVHT_NOWHERE;
7575 return -1;
7576 }
7577 }
7578
7579 TRACE("lpht->flags=0x%x\n", lpht->flags);
7580 if (lpht->flags) return -1;
7581
7582 lpht->flags |= LVHT_NOWHERE;
7583
7584 /* first deal with the large items */
7585 rcSearch.left = lpht->pt.x;
7586 rcSearch.top = lpht->pt.y;
7587 rcSearch.right = rcSearch.left + 1;
7588 rcSearch.bottom = rcSearch.top + 1;
7589
7590 iterator_frameditems(&i, infoPtr, &rcSearch);
7591 iterator_next(&i); /* go to first item in the sequence */
7592 iItem = i.nItem;
7593 iterator_destroy(&i);
7594
7595 TRACE("lpht->iItem=%d\n", iItem);
7596 if (iItem == -1) return -1;
7597
7598 lvItem.mask = LVIF_STATE | LVIF_TEXT;
7599 if (infoPtr->uView == LV_VIEW_DETAILS) lvItem.mask |= LVIF_INDENT;
7600 lvItem.stateMask = LVIS_STATEIMAGEMASK;
7601 if (infoPtr->uView == LV_VIEW_ICON) lvItem.stateMask |= LVIS_FOCUSED;
7602 lvItem.iItem = iItem;
7603 lvItem.iSubItem = subitem ? lpht->iSubItem : 0;
7604 lvItem.pszText = szDispText;
7605 lvItem.cchTextMax = DISP_TEXT_SIZE;
7606 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) return -1;
7607 if (!infoPtr->bFocus) lvItem.state &= ~LVIS_FOCUSED;
7608
7609 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, &rcBox, NULL, &rcIcon, &rcState, &rcLabel);
7610 LISTVIEW_GetItemOrigin(infoPtr, iItem, &Position);
7611 opt.x = lpht->pt.x - Position.x - Origin.x;
7612
7613 if (lpht->pt.y < infoPtr->rcList.top && lpht->pt.y >= 0)
7614 opt.y = lpht->pt.y - Position.y - Origin.y + infoPtr->rcList.top;
7615 else
7616 opt.y = lpht->pt.y - Position.y - Origin.y;
7617
7618 if (infoPtr->uView == LV_VIEW_DETAILS)
7619 {
7620 rcBounds = rcBox;
7621 if (infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT)
7622 opt.x = lpht->pt.x - Origin.x;
7623 }
7624 else
7625 {
7626 UnionRect(&rcBounds, &rcIcon, &rcLabel);
7627 UnionRect(&rcBounds, &rcBounds, &rcState);
7628 }
7629 TRACE("rcBounds=%s\n", wine_dbgstr_rect(&rcBounds));
7630 if (!PtInRect(&rcBounds, opt)) return -1;
7631
7632 /* That's a special case - row rectangle is used as item rectangle and
7633 returned flags contain all item parts. */
7634 is_fullrow = (infoPtr->uView == LV_VIEW_DETAILS) && ((infoPtr->dwLvExStyle & LVS_EX_FULLROWSELECT) || (infoPtr->dwStyle & LVS_OWNERDRAWFIXED));
7635
7636 if (PtInRect(&rcIcon, opt))
7637 lpht->flags |= LVHT_ONITEMICON;
7638 else if (PtInRect(&rcLabel, opt))
7639 lpht->flags |= LVHT_ONITEMLABEL;
7640 else if (infoPtr->himlState && PtInRect(&rcState, opt))
7641 lpht->flags |= LVHT_ONITEMSTATEICON;
7642 if (is_fullrow && !(lpht->flags & LVHT_ONITEM))
7643 {
7644 lpht->flags = LVHT_ONITEM | LVHT_ABOVE;
7645 }
7646 if (lpht->flags & LVHT_ONITEM)
7647 lpht->flags &= ~LVHT_NOWHERE;
7648 TRACE("lpht->flags=0x%x\n", lpht->flags);
7649
7650 if (select && !is_fullrow)
7651 {
7652 if (infoPtr->uView == LV_VIEW_DETAILS)
7653 {
7654 /* get main item bounds */
7655 lvItem.iSubItem = 0;
7656 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, &rcBox, NULL, &rcIcon, &rcState, &rcLabel);
7657 UnionRect(&rcBounds, &rcIcon, &rcLabel);
7658 UnionRect(&rcBounds, &rcBounds, &rcState);
7659 }
7660 if (!PtInRect(&rcBounds, opt)) iItem = -1;
7661 }
7662 return lpht->iItem = iItem;
7663 }
7664
7665 /***
7666 * DESCRIPTION:
7667 * Inserts a new item in the listview control.
7668 *
7669 * PARAMETER(S):
7670 * [I] infoPtr : valid pointer to the listview structure
7671 * [I] lpLVItem : item information
7672 * [I] isW : TRUE if lpLVItem is Unicode, FALSE if it's ANSI
7673 *
7674 * RETURN:
7675 * SUCCESS : new item index
7676 * FAILURE : -1
7677 */
7678 static INT LISTVIEW_InsertItemT(LISTVIEW_INFO *infoPtr, const LVITEMW *lpLVItem, BOOL isW)
7679 {
7680 INT nItem;
7681 HDPA hdpaSubItems;
7682 NMLISTVIEW nmlv;
7683 ITEM_INFO *lpItem;
7684 ITEM_ID *lpID;
7685 BOOL is_sorted, has_changed;
7686 LVITEMW item;
7687 HWND hwndSelf = infoPtr->hwndSelf;
7688
7689 TRACE("(item=%s, isW=%d)\n", debuglvitem_t(lpLVItem, isW), isW);
7690
7691 if (infoPtr->dwStyle & LVS_OWNERDATA) return infoPtr->nItemCount++;
7692
7693 /* make sure it's an item, and not a subitem; cannot insert a subitem */
7694 if (!lpLVItem || lpLVItem->iSubItem) return -1;
7695
7696 if (!is_assignable_item(lpLVItem, infoPtr->dwStyle)) return -1;
7697
7698 if (!(lpItem = Alloc(sizeof(ITEM_INFO)))) return -1;
7699
7700 /* insert item in listview control data structure */
7701 if ( !(hdpaSubItems = DPA_Create(8)) ) goto fail;
7702 if ( !DPA_SetPtr(hdpaSubItems, 0, lpItem) ) assert (FALSE);
7703
7704 /* link with id struct */
7705 if (!(lpID = Alloc(sizeof(ITEM_ID)))) goto fail;
7706 lpItem->id = lpID;
7707 lpID->item = hdpaSubItems;
7708 lpID->id = get_next_itemid(infoPtr);
7709 if ( DPA_InsertPtr(infoPtr->hdpaItemIds, infoPtr->nItemCount, lpID) == -1) goto fail;
7710
7711 is_sorted = (infoPtr->dwStyle & (LVS_SORTASCENDING | LVS_SORTDESCENDING)) &&
7712 !(infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && (LPSTR_TEXTCALLBACKW != lpLVItem->pszText);
7713
7714 if (lpLVItem->iItem < 0 && !is_sorted) return -1;
7715
7716 /* calculate new item index */
7717 if (is_sorted)
7718 {
7719 HDPA hItem;
7720 ITEM_INFO *item_s;
7721 INT i = 0, cmpv;
7722
7723 while (i < infoPtr->nItemCount)
7724 {
7725 hItem = DPA_GetPtr( infoPtr->hdpaItems, i);
7726 item_s = DPA_GetPtr(hItem, 0);
7727
7728 cmpv = textcmpWT(item_s->hdr.pszText, lpLVItem->pszText, isW);
7729 if (infoPtr->dwStyle & LVS_SORTDESCENDING) cmpv *= -1;
7730
7731 if (cmpv >= 0) break;
7732 i++;
7733 }
7734 nItem = i;
7735 }
7736 else
7737 nItem = min(lpLVItem->iItem, infoPtr->nItemCount);
7738
7739 TRACE("inserting at %d, sorted=%d, count=%d, iItem=%d\n", nItem, is_sorted, infoPtr->nItemCount, lpLVItem->iItem);
7740 nItem = DPA_InsertPtr( infoPtr->hdpaItems, nItem, hdpaSubItems );
7741 if (nItem == -1) goto fail;
7742 infoPtr->nItemCount++;
7743
7744 /* shift indices first so they don't get tangled */
7745 LISTVIEW_ShiftIndices(infoPtr, nItem, 1);
7746
7747 /* set the item attributes */
7748 if (lpLVItem->mask & (LVIF_GROUPID|LVIF_COLUMNS))
7749 {
7750 /* full size structure expected - _WIN32IE >= 0x560 */
7751 item = *lpLVItem;
7752 }
7753 else if (lpLVItem->mask & LVIF_INDENT)
7754 {
7755 /* indent member expected - _WIN32IE >= 0x300 */
7756 memcpy(&item, lpLVItem, offsetof( LVITEMW, iGroupId ));
7757 }
7758 else
7759 {
7760 /* minimal structure expected */
7761 memcpy(&item, lpLVItem, offsetof( LVITEMW, iIndent ));
7762 }
7763 item.iItem = nItem;
7764 if (infoPtr->dwLvExStyle & LVS_EX_CHECKBOXES)
7765 {
7766 item.mask |= LVIF_STATE;
7767 item.stateMask |= LVIS_STATEIMAGEMASK;
7768 item.state &= ~LVIS_STATEIMAGEMASK;
7769 item.state |= INDEXTOSTATEIMAGEMASK(1);
7770 }
7771
7772 if (!set_main_item(infoPtr, &item, TRUE, isW, &has_changed)) goto undo;
7773
7774 /* make room for the position, if we are in the right mode */
7775 if ((infoPtr->uView == LV_VIEW_SMALLICON) || (infoPtr->uView == LV_VIEW_ICON))
7776 {
7777 if (DPA_InsertPtr(infoPtr->hdpaPosX, nItem, 0) == -1)
7778 goto undo;
7779 if (DPA_InsertPtr(infoPtr->hdpaPosY, nItem, 0) == -1)
7780 {
7781 DPA_DeletePtr(infoPtr->hdpaPosX, nItem);
7782 goto undo;
7783 }
7784 }
7785
7786 /* send LVN_INSERTITEM notification */
7787 memset(&nmlv, 0, sizeof(NMLISTVIEW));
7788 nmlv.iItem = nItem;
7789 nmlv.lParam = lpItem->lParam;
7790 notify_listview(infoPtr, LVN_INSERTITEM, &nmlv);
7791 if (!IsWindow(hwndSelf))
7792 return -1;
7793
7794 /* align items (set position of each item) */
7795 if (infoPtr->uView == LV_VIEW_SMALLICON || infoPtr->uView == LV_VIEW_ICON)
7796 {
7797 POINT pt;
7798
7799 if (infoPtr->dwStyle & LVS_ALIGNLEFT)
7800 LISTVIEW_NextIconPosLeft(infoPtr, &pt);
7801 else
7802 LISTVIEW_NextIconPosTop(infoPtr, &pt);
7803
7804 LISTVIEW_MoveIconTo(infoPtr, nItem, &pt, TRUE);
7805 }
7806
7807 /* now is the invalidation fun */
7808 LISTVIEW_ScrollOnInsert(infoPtr, nItem, 1);
7809 return nItem;
7810
7811 undo:
7812 LISTVIEW_ShiftIndices(infoPtr, nItem, -1);
7813 LISTVIEW_ShiftFocus(infoPtr, infoPtr->nFocusedItem, nItem, -1);
7814 DPA_DeletePtr(infoPtr->hdpaItems, nItem);
7815 infoPtr->nItemCount--;
7816 fail:
7817 DPA_DeletePtr(hdpaSubItems, 0);
7818 DPA_Destroy (hdpaSubItems);
7819 Free (lpItem);
7820 return -1;
7821 }
7822
7823 /***
7824 * DESCRIPTION:
7825 * Checks item visibility.
7826 *
7827 * PARAMETER(S):
7828 * [I] infoPtr : valid pointer to the listview structure
7829 * [I] nFirst : item index to check for
7830 *
7831 * RETURN:
7832 * Item visible : TRUE
7833 * Item invisible or failure : FALSE
7834 */
7835 static BOOL LISTVIEW_IsItemVisible(const LISTVIEW_INFO *infoPtr, INT nItem)
7836 {
7837 POINT Origin, Position;
7838 RECT rcItem;
7839 HDC hdc;
7840 BOOL ret;
7841
7842 TRACE("nItem=%d\n", nItem);
7843
7844 if (nItem < 0 || nItem >= DPA_GetPtrCount(infoPtr->hdpaItems)) return FALSE;
7845
7846 LISTVIEW_GetOrigin(infoPtr, &Origin);
7847 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position);
7848 rcItem.left = Position.x + Origin.x;
7849 rcItem.top = Position.y + Origin.y;
7850 rcItem.right = rcItem.left + infoPtr->nItemWidth;
7851 rcItem.bottom = rcItem.top + infoPtr->nItemHeight;
7852
7853 hdc = GetDC(infoPtr->hwndSelf);
7854 if (!hdc) return FALSE;
7855 ret = RectVisible(hdc, &rcItem);
7856 ReleaseDC(infoPtr->hwndSelf, hdc);
7857
7858 return ret;
7859 }
7860
7861 /***
7862 * DESCRIPTION:
7863 * Redraws a range of items.
7864 *
7865 * PARAMETER(S):
7866 * [I] infoPtr : valid pointer to the listview structure
7867 * [I] nFirst : first item
7868 * [I] nLast : last item
7869 *
7870 * RETURN:
7871 * SUCCESS : TRUE
7872 * FAILURE : FALSE
7873 */
7874 static BOOL LISTVIEW_RedrawItems(const LISTVIEW_INFO *infoPtr, INT nFirst, INT nLast)
7875 {
7876 INT i;
7877
7878 if (nLast < nFirst || min(nFirst, nLast) < 0 ||
7879 max(nFirst, nLast) >= infoPtr->nItemCount)
7880 return FALSE;
7881
7882 for (i = nFirst; i <= nLast; i++)
7883 LISTVIEW_InvalidateItem(infoPtr, i);
7884
7885 return TRUE;
7886 }
7887
7888 /***
7889 * DESCRIPTION:
7890 * Scroll the content of a listview.
7891 *
7892 * PARAMETER(S):
7893 * [I] infoPtr : valid pointer to the listview structure
7894 * [I] dx : horizontal scroll amount in pixels
7895 * [I] dy : vertical scroll amount in pixels
7896 *
7897 * RETURN:
7898 * SUCCESS : TRUE
7899 * FAILURE : FALSE
7900 *
7901 * COMMENTS:
7902 * If the control is in report view (LV_VIEW_DETAILS) the control can
7903 * be scrolled only in line increments. "dy" will be rounded to the
7904 * nearest number of pixels that are a whole line. Ex: if line height
7905 * is 16 and an 8 is passed, the list will be scrolled by 16. If a 7
7906 * is passed, then the scroll will be 0. (per MSDN 7/2002)
7907 */
7908 static BOOL LISTVIEW_Scroll(LISTVIEW_INFO *infoPtr, INT dx, INT dy)
7909 {
7910 switch(infoPtr->uView) {
7911 case LV_VIEW_DETAILS:
7912 dy += (dy < 0 ? -1 : 1) * infoPtr->nItemHeight/2;
7913 dy /= infoPtr->nItemHeight;
7914 break;
7915 case LV_VIEW_LIST:
7916 if (dy != 0) return FALSE;
7917 break;
7918 default: /* icon */
7919 break;
7920 }
7921
7922 if (dx != 0) LISTVIEW_HScroll(infoPtr, SB_INTERNAL, dx);
7923 if (dy != 0) LISTVIEW_VScroll(infoPtr, SB_INTERNAL, dy);
7924
7925 return TRUE;
7926 }
7927
7928 /***
7929 * DESCRIPTION:
7930 * Sets the background color.
7931 *
7932 * PARAMETER(S):
7933 * [I] infoPtr : valid pointer to the listview structure
7934 * [I] color : background color
7935 *
7936 * RETURN:
7937 * SUCCESS : TRUE
7938 * FAILURE : FALSE
7939 */
7940 static BOOL LISTVIEW_SetBkColor(LISTVIEW_INFO *infoPtr, COLORREF color)
7941 {
7942 TRACE("(color=%x)\n", color);
7943
7944 infoPtr->bDefaultBkColor = FALSE;
7945 if(infoPtr->clrBk != color) {
7946 if (infoPtr->clrBk != CLR_NONE) DeleteObject(infoPtr->hBkBrush);
7947 infoPtr->clrBk = color;
7948 if (color == CLR_NONE)
7949 infoPtr->hBkBrush = (HBRUSH)GetClassLongPtrW(infoPtr->hwndSelf, GCLP_HBRBACKGROUND);
7950 else
7951 {
7952 infoPtr->hBkBrush = CreateSolidBrush(color);
7953 infoPtr->dwLvExStyle &= ~LVS_EX_TRANSPARENTBKGND;
7954 }
7955 }
7956
7957 return TRUE;
7958 }
7959
7960 /* LISTVIEW_SetBkImage */
7961
7962 /*** Helper for {Insert,Set}ColumnT *only* */
7963 static void column_fill_hditem(const LISTVIEW_INFO *infoPtr, HDITEMW *lphdi, INT nColumn,
7964 const LVCOLUMNW *lpColumn, BOOL isW)
7965 {
7966 if (lpColumn->mask & LVCF_FMT)
7967 {
7968 /* format member is valid */
7969 lphdi->mask |= HDI_FORMAT;
7970
7971 /* set text alignment (leftmost column must be left-aligned) */
7972 if (nColumn == 0 || (lpColumn->fmt & LVCFMT_JUSTIFYMASK) == LVCFMT_LEFT)
7973 lphdi->fmt |= HDF_LEFT;
7974 else if ((lpColumn->fmt & LVCFMT_JUSTIFYMASK) == LVCFMT_RIGHT)
7975 lphdi->fmt |= HDF_RIGHT;
7976 else if ((lpColumn->fmt & LVCFMT_JUSTIFYMASK) == LVCFMT_CENTER)
7977 lphdi->fmt |= HDF_CENTER;
7978
7979 if (lpColumn->fmt & LVCFMT_BITMAP_ON_RIGHT)
7980 lphdi->fmt |= HDF_BITMAP_ON_RIGHT;
7981
7982 if (lpColumn->fmt & LVCFMT_COL_HAS_IMAGES)
7983 {
7984 lphdi->fmt |= HDF_IMAGE;
7985 lphdi->iImage = I_IMAGECALLBACK;
7986 }
7987
7988 if (lpColumn->fmt & LVCFMT_FIXED_WIDTH)
7989 lphdi->fmt |= HDF_FIXEDWIDTH;
7990 }
7991
7992 if (lpColumn->mask & LVCF_WIDTH)
7993 {
7994 lphdi->mask |= HDI_WIDTH;
7995 if(lpColumn->cx == LVSCW_AUTOSIZE_USEHEADER)
7996 {
7997 /* make it fill the remainder of the controls width */
7998 RECT rcHeader;
7999 INT item_index;
8000
8001 for(item_index = 0; item_index < (nColumn - 1); item_index++)
8002 {
8003 LISTVIEW_GetHeaderRect(infoPtr, item_index, &rcHeader);
8004 lphdi->cxy += rcHeader.right - rcHeader.left;
8005 }
8006
8007 /* retrieve the layout of the header */
8008 GetClientRect(infoPtr->hwndSelf, &rcHeader);
8009 TRACE("start cxy=%d rcHeader=%s\n", lphdi->cxy, wine_dbgstr_rect(&rcHeader));
8010
8011 lphdi->cxy = (rcHeader.right - rcHeader.left) - lphdi->cxy;
8012 }
8013 else
8014 lphdi->cxy = lpColumn->cx;
8015 }
8016
8017 if (lpColumn->mask & LVCF_TEXT)
8018 {
8019 lphdi->mask |= HDI_TEXT | HDI_FORMAT;
8020 lphdi->fmt |= HDF_STRING;
8021 lphdi->pszText = lpColumn->pszText;
8022 lphdi->cchTextMax = textlenT(lpColumn->pszText, isW);
8023 }
8024
8025 if (lpColumn->mask & LVCF_IMAGE)
8026 {
8027 lphdi->mask |= HDI_IMAGE;
8028 lphdi->iImage = lpColumn->iImage;
8029 }
8030
8031 if (lpColumn->mask & LVCF_ORDER)
8032 {
8033 lphdi->mask |= HDI_ORDER;
8034 lphdi->iOrder = lpColumn->iOrder;
8035 }
8036 }
8037
8038
8039 /***
8040 * DESCRIPTION:
8041 * Inserts a new column.
8042 *
8043 * PARAMETER(S):
8044 * [I] infoPtr : valid pointer to the listview structure
8045 * [I] nColumn : column index
8046 * [I] lpColumn : column information
8047 * [I] isW : TRUE if lpColumn is Unicode, FALSE otherwise
8048 *
8049 * RETURN:
8050 * SUCCESS : new column index
8051 * FAILURE : -1
8052 */
8053 static INT LISTVIEW_InsertColumnT(LISTVIEW_INFO *infoPtr, INT nColumn,
8054 const LVCOLUMNW *lpColumn, BOOL isW)
8055 {
8056 COLUMN_INFO *lpColumnInfo;
8057 INT nNewColumn;
8058 HDITEMW hdi;
8059
8060 TRACE("(nColumn=%d, lpColumn=%s, isW=%d)\n", nColumn, debuglvcolumn_t(lpColumn, isW), isW);
8061
8062 if (!lpColumn || nColumn < 0) return -1;
8063 nColumn = min(nColumn, DPA_GetPtrCount(infoPtr->hdpaColumns));
8064
8065 ZeroMemory(&hdi, sizeof(HDITEMW));
8066 column_fill_hditem(infoPtr, &hdi, nColumn, lpColumn, isW);
8067
8068 /*
8069 * A mask not including LVCF_WIDTH turns into a mask of width, width 10
8070 * (can be seen in SPY) otherwise column never gets added.
8071 */
8072 if (!(lpColumn->mask & LVCF_WIDTH)) {
8073 hdi.mask |= HDI_WIDTH;
8074 hdi.cxy = 10;
8075 }
8076
8077 /*
8078 * when the iSubItem is available Windows copies it to the header lParam. It seems
8079 * to happen only in LVM_INSERTCOLUMN - not in LVM_SETCOLUMN
8080 */
8081 if (lpColumn->mask & LVCF_SUBITEM)
8082 {
8083 hdi.mask |= HDI_LPARAM;
8084 hdi.lParam = lpColumn->iSubItem;
8085 }
8086
8087 /* create header if not present */
8088 LISTVIEW_CreateHeader(infoPtr);
8089 if (!(LVS_NOCOLUMNHEADER & infoPtr->dwStyle) &&
8090 (infoPtr->uView == LV_VIEW_DETAILS) && (WS_VISIBLE & infoPtr->dwStyle))
8091 {
8092 ShowWindow(infoPtr->hwndHeader, SW_SHOWNORMAL);
8093 }
8094
8095 /* insert item in header control */
8096 nNewColumn = SendMessageW(infoPtr->hwndHeader,
8097 isW ? HDM_INSERTITEMW : HDM_INSERTITEMA,
8098 nColumn, (LPARAM)&hdi);
8099 if (nNewColumn == -1) return -1;
8100 if (nNewColumn != nColumn) ERR("nColumn=%d, nNewColumn=%d\n", nColumn, nNewColumn);
8101
8102 /* create our own column info */
8103 if (!(lpColumnInfo = Alloc(sizeof(COLUMN_INFO)))) goto fail;
8104 if (DPA_InsertPtr(infoPtr->hdpaColumns, nNewColumn, lpColumnInfo) == -1) goto fail;
8105
8106 if (lpColumn->mask & LVCF_FMT) lpColumnInfo->fmt = lpColumn->fmt;
8107 if (lpColumn->mask & LVCF_MINWIDTH) lpColumnInfo->cxMin = lpColumn->cxMin;
8108 if (!SendMessageW(infoPtr->hwndHeader, HDM_GETITEMRECT, nNewColumn, (LPARAM)&lpColumnInfo->rcHeader))
8109 goto fail;
8110
8111 /* now we have to actually adjust the data */
8112 if (!(infoPtr->dwStyle & LVS_OWNERDATA) && infoPtr->nItemCount > 0)
8113 {
8114 SUBITEM_INFO *lpSubItem;
8115 HDPA hdpaSubItems;
8116 INT nItem, i;
8117 LVITEMW item;
8118 BOOL changed;
8119
8120 item.iSubItem = nNewColumn;
8121 item.mask = LVIF_TEXT | LVIF_IMAGE;
8122 item.iImage = I_IMAGECALLBACK;
8123 item.pszText = LPSTR_TEXTCALLBACKW;
8124
8125 for (nItem = 0; nItem < infoPtr->nItemCount; nItem++)
8126 {
8127 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, nItem);
8128 for (i = 1; i < DPA_GetPtrCount(hdpaSubItems); i++)
8129 {
8130 lpSubItem = DPA_GetPtr(hdpaSubItems, i);
8131 if (lpSubItem->iSubItem >= nNewColumn)
8132 lpSubItem->iSubItem++;
8133 }
8134
8135 /* add new subitem for each item */
8136 item.iItem = nItem;
8137 set_sub_item(infoPtr, &item, isW, &changed);
8138 }
8139 }
8140
8141 /* make space for the new column */
8142 LISTVIEW_ScrollColumns(infoPtr, nNewColumn + 1, lpColumnInfo->rcHeader.right - lpColumnInfo->rcHeader.left);
8143 LISTVIEW_UpdateItemSize(infoPtr);
8144
8145 return nNewColumn;
8146
8147 fail:
8148 if (nNewColumn != -1) SendMessageW(infoPtr->hwndHeader, HDM_DELETEITEM, nNewColumn, 0);
8149 if (lpColumnInfo)
8150 {
8151 DPA_DeletePtr(infoPtr->hdpaColumns, nNewColumn);
8152 Free(lpColumnInfo);
8153 }
8154 return -1;
8155 }
8156
8157 /***
8158 * DESCRIPTION:
8159 * Sets the attributes of a header item.
8160 *
8161 * PARAMETER(S):
8162 * [I] infoPtr : valid pointer to the listview structure
8163 * [I] nColumn : column index
8164 * [I] lpColumn : column attributes
8165 * [I] isW: if TRUE, then lpColumn is a LPLVCOLUMNW, else it is a LPLVCOLUMNA
8166 *
8167 * RETURN:
8168 * SUCCESS : TRUE
8169 * FAILURE : FALSE
8170 */
8171 static BOOL LISTVIEW_SetColumnT(const LISTVIEW_INFO *infoPtr, INT nColumn,
8172 const LVCOLUMNW *lpColumn, BOOL isW)
8173 {
8174 HDITEMW hdi, hdiget;
8175 BOOL bResult;
8176
8177 TRACE("(nColumn=%d, lpColumn=%s, isW=%d)\n", nColumn, debuglvcolumn_t(lpColumn, isW), isW);
8178
8179 if (!lpColumn || nColumn < 0 || nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return FALSE;
8180
8181 ZeroMemory(&hdi, sizeof(HDITEMW));
8182 if (lpColumn->mask & LVCF_FMT)
8183 {
8184 hdi.mask |= HDI_FORMAT;
8185 hdiget.mask = HDI_FORMAT;
8186 if (SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, nColumn, (LPARAM)&hdiget))
8187 hdi.fmt = hdiget.fmt & HDF_STRING;
8188 }
8189 column_fill_hditem(infoPtr, &hdi, nColumn, lpColumn, isW);
8190
8191 /* set header item attributes */
8192 bResult = SendMessageW(infoPtr->hwndHeader, isW ? HDM_SETITEMW : HDM_SETITEMA, nColumn, (LPARAM)&hdi);
8193 if (!bResult) return FALSE;
8194
8195 if (lpColumn->mask & LVCF_FMT)
8196 {
8197 COLUMN_INFO *lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, nColumn);
8198 INT oldFmt = lpColumnInfo->fmt;
8199
8200 lpColumnInfo->fmt = lpColumn->fmt;
8201 if ((oldFmt ^ lpColumn->fmt) & (LVCFMT_JUSTIFYMASK | LVCFMT_IMAGE))
8202 {
8203 if (infoPtr->uView == LV_VIEW_DETAILS) LISTVIEW_InvalidateColumn(infoPtr, nColumn);
8204 }
8205 }
8206
8207 if (lpColumn->mask & LVCF_MINWIDTH)
8208 LISTVIEW_GetColumnInfo(infoPtr, nColumn)->cxMin = lpColumn->cxMin;
8209
8210 return TRUE;
8211 }
8212
8213 /***
8214 * DESCRIPTION:
8215 * Sets the column order array
8216 *
8217 * PARAMETERS:
8218 * [I] infoPtr : valid pointer to the listview structure
8219 * [I] iCount : number of elements in column order array
8220 * [I] lpiArray : pointer to column order array
8221 *
8222 * RETURN:
8223 * SUCCESS : TRUE
8224 * FAILURE : FALSE
8225 */
8226 static BOOL LISTVIEW_SetColumnOrderArray(LISTVIEW_INFO *infoPtr, INT iCount, const INT *lpiArray)
8227 {
8228 TRACE("iCount %d lpiArray %p\n", iCount, lpiArray);
8229
8230 if (!lpiArray || !IsWindow(infoPtr->hwndHeader)) return FALSE;
8231
8232 infoPtr->colRectsDirty = TRUE;
8233
8234 return SendMessageW(infoPtr->hwndHeader, HDM_SETORDERARRAY, iCount, (LPARAM)lpiArray);
8235 }
8236
8237 /***
8238 * DESCRIPTION:
8239 * Sets the width of a column
8240 *
8241 * PARAMETERS:
8242 * [I] infoPtr : valid pointer to the listview structure
8243 * [I] nColumn : column index
8244 * [I] cx : column width
8245 *
8246 * RETURN:
8247 * SUCCESS : TRUE
8248 * FAILURE : FALSE
8249 */
8250 static BOOL LISTVIEW_SetColumnWidth(LISTVIEW_INFO *infoPtr, INT nColumn, INT cx)
8251 {
8252 WCHAR szDispText[DISP_TEXT_SIZE] = { 0 };
8253 INT max_cx = 0;
8254 HDITEMW hdi;
8255
8256 TRACE("(nColumn=%d, cx=%d)\n", nColumn, cx);
8257
8258 /* set column width only if in report or list mode */
8259 if (infoPtr->uView != LV_VIEW_DETAILS && infoPtr->uView != LV_VIEW_LIST) return FALSE;
8260
8261 /* take care of invalid cx values */
8262 if(infoPtr->uView == LV_VIEW_DETAILS && cx < -2) cx = LVSCW_AUTOSIZE;
8263 else if (infoPtr->uView == LV_VIEW_LIST && cx < 1) return FALSE;
8264
8265 /* resize all columns if in LV_VIEW_LIST mode */
8266 if(infoPtr->uView == LV_VIEW_LIST)
8267 {
8268 infoPtr->nItemWidth = cx;
8269 LISTVIEW_InvalidateList(infoPtr);
8270 return TRUE;
8271 }
8272
8273 if (nColumn < 0 || nColumn >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return FALSE;
8274
8275 if (cx == LVSCW_AUTOSIZE || (cx == LVSCW_AUTOSIZE_USEHEADER && nColumn < DPA_GetPtrCount(infoPtr->hdpaColumns) -1))
8276 {
8277 INT nLabelWidth;
8278 LVITEMW lvItem;
8279
8280 lvItem.mask = LVIF_TEXT;
8281 lvItem.iItem = 0;
8282 lvItem.iSubItem = nColumn;
8283 lvItem.cchTextMax = DISP_TEXT_SIZE;
8284 for (; lvItem.iItem < infoPtr->nItemCount; lvItem.iItem++)
8285 {
8286 lvItem.pszText = szDispText;
8287 if (!LISTVIEW_GetItemW(infoPtr, &lvItem)) continue;
8288 nLabelWidth = LISTVIEW_GetStringWidthT(infoPtr, lvItem.pszText, TRUE);
8289 if (max_cx < nLabelWidth) max_cx = nLabelWidth;
8290 }
8291 if (infoPtr->himlSmall && (nColumn == 0 || (LISTVIEW_GetColumnInfo(infoPtr, nColumn)->fmt & LVCFMT_IMAGE)))
8292 max_cx += infoPtr->iconSize.cx;
8293 max_cx += TRAILING_LABEL_PADDING;
8294 }
8295
8296 /* autosize based on listview items width */
8297 if(cx == LVSCW_AUTOSIZE)
8298 cx = max_cx;
8299 else if(cx == LVSCW_AUTOSIZE_USEHEADER)
8300 {
8301 /* if iCol is the last column make it fill the remainder of the controls width */
8302 if(nColumn == DPA_GetPtrCount(infoPtr->hdpaColumns) - 1)
8303 {
8304 RECT rcHeader;
8305 POINT Origin;
8306
8307 LISTVIEW_GetOrigin(infoPtr, &Origin);
8308 LISTVIEW_GetHeaderRect(infoPtr, nColumn, &rcHeader);
8309
8310 cx = infoPtr->rcList.right - Origin.x - rcHeader.left;
8311 }
8312 else
8313 {
8314 /* Despite what the MS docs say, if this is not the last
8315 column, then MS resizes the column to the width of the
8316 largest text string in the column, including headers
8317 and items. This is different from LVSCW_AUTOSIZE in that
8318 LVSCW_AUTOSIZE ignores the header string length. */
8319 cx = 0;
8320
8321 /* retrieve header text */
8322 hdi.mask = HDI_TEXT;
8323 hdi.cchTextMax = DISP_TEXT_SIZE;
8324 hdi.pszText = szDispText;
8325 if (SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, nColumn, (LPARAM)&hdi))
8326 {
8327 HDC hdc = GetDC(infoPtr->hwndSelf);
8328 HFONT old_font = SelectObject(hdc, (HFONT)SendMessageW(infoPtr->hwndHeader, WM_GETFONT, 0, 0));
8329 SIZE size;
8330
8331 if (GetTextExtentPoint32W(hdc, hdi.pszText, lstrlenW(hdi.pszText), &size))
8332 cx = size.cx + TRAILING_HEADER_PADDING;
8333 /* FIXME: Take into account the header image, if one is present */
8334 SelectObject(hdc, old_font);
8335 ReleaseDC(infoPtr->hwndSelf, hdc);
8336 }
8337 cx = max (cx, max_cx);
8338 }
8339 }
8340
8341 if (cx < 0) return FALSE;
8342
8343 /* call header to update the column change */
8344 hdi.mask = HDI_WIDTH;
8345 hdi.cxy = max(cx, LISTVIEW_GetColumnInfo(infoPtr, nColumn)->cxMin);
8346 TRACE("hdi.cxy=%d\n", hdi.cxy);
8347 return SendMessageW(infoPtr->hwndHeader, HDM_SETITEMW, nColumn, (LPARAM)&hdi);
8348 }
8349
8350 /***
8351 * Creates the checkbox imagelist. Helper for LISTVIEW_SetExtendedListViewStyle
8352 *
8353 */
8354 static HIMAGELIST LISTVIEW_CreateCheckBoxIL(const LISTVIEW_INFO *infoPtr)
8355 {
8356 HDC hdc_wnd, hdc;
8357 HBITMAP hbm_im, hbm_mask, hbm_orig;
8358 RECT rc;
8359 HBRUSH hbr_white = GetStockObject(WHITE_BRUSH);
8360 HBRUSH hbr_black = GetStockObject(BLACK_BRUSH);
8361 HIMAGELIST himl;
8362
8363 himl = ImageList_Create(GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON),
8364 ILC_COLOR | ILC_MASK, 2, 2);
8365 hdc_wnd = GetDC(infoPtr->hwndSelf);
8366 hdc = CreateCompatibleDC(hdc_wnd);
8367 hbm_im = CreateCompatibleBitmap(hdc_wnd, GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON));
8368 hbm_mask = CreateBitmap(GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), 1, 1, NULL);
8369 ReleaseDC(infoPtr->hwndSelf, hdc_wnd);
8370
8371 rc.left = rc.top = 0;
8372 rc.right = GetSystemMetrics(SM_CXSMICON);
8373 rc.bottom = GetSystemMetrics(SM_CYSMICON);
8374
8375 hbm_orig = SelectObject(hdc, hbm_mask);
8376 FillRect(hdc, &rc, hbr_white);
8377 InflateRect(&rc, -2, -2);
8378 FillRect(hdc, &rc, hbr_black);
8379
8380 SelectObject(hdc, hbm_im);
8381 DrawFrameControl(hdc, &rc, DFC_BUTTON, DFCS_BUTTONCHECK | DFCS_MONO);
8382 SelectObject(hdc, hbm_orig);
8383 ImageList_Add(himl, hbm_im, hbm_mask);
8384
8385 SelectObject(hdc, hbm_im);
8386 DrawFrameControl(hdc, &rc, DFC_BUTTON, DFCS_BUTTONCHECK | DFCS_MONO | DFCS_CHECKED);
8387 SelectObject(hdc, hbm_orig);
8388 ImageList_Add(himl, hbm_im, hbm_mask);
8389
8390 DeleteObject(hbm_mask);
8391 DeleteObject(hbm_im);
8392 DeleteDC(hdc);
8393
8394 return himl;
8395 }
8396
8397 /***
8398 * DESCRIPTION:
8399 * Sets the extended listview style.
8400 *
8401 * PARAMETERS:
8402 * [I] infoPtr : valid pointer to the listview structure
8403 * [I] dwMask : mask
8404 * [I] dwStyle : style
8405 *
8406 * RETURN:
8407 * SUCCESS : previous style
8408 * FAILURE : 0
8409 */
8410 static DWORD LISTVIEW_SetExtendedListViewStyle(LISTVIEW_INFO *infoPtr, DWORD mask, DWORD ex_style)
8411 {
8412 DWORD old_ex_style = infoPtr->dwLvExStyle;
8413
8414 TRACE("mask=0x%08x, ex_style=0x%08x\n", mask, ex_style);
8415
8416 /* set new style */
8417 if (mask)
8418 infoPtr->dwLvExStyle = (old_ex_style & ~mask) | (ex_style & mask);
8419 else
8420 infoPtr->dwLvExStyle = ex_style;
8421
8422 if((infoPtr->dwLvExStyle ^ old_ex_style) & LVS_EX_CHECKBOXES)
8423 {
8424 HIMAGELIST himl = 0;
8425 if(infoPtr->dwLvExStyle & LVS_EX_CHECKBOXES)
8426 {
8427 LVITEMW item;
8428 item.mask = LVIF_STATE;
8429 item.stateMask = LVIS_STATEIMAGEMASK;
8430 item.state = INDEXTOSTATEIMAGEMASK(1);
8431 LISTVIEW_SetItemState(infoPtr, -1, &item);
8432
8433 himl = LISTVIEW_CreateCheckBoxIL(infoPtr);
8434 if(!(infoPtr->dwStyle & LVS_SHAREIMAGELISTS))
8435 ImageList_Destroy(infoPtr->himlState);
8436 }
8437 himl = LISTVIEW_SetImageList(infoPtr, LVSIL_STATE, himl);
8438 /* checkbox list replaces previous custom list or... */
8439 if(((infoPtr->dwLvExStyle & LVS_EX_CHECKBOXES) &&
8440 !(infoPtr->dwStyle & LVS_SHAREIMAGELISTS)) ||
8441 /* ...previous was checkbox list */
8442 (old_ex_style & LVS_EX_CHECKBOXES))
8443 ImageList_Destroy(himl);
8444 }
8445
8446 if((infoPtr->dwLvExStyle ^ old_ex_style) & LVS_EX_HEADERDRAGDROP)
8447 {
8448 DWORD style;
8449
8450 /* if not already created */
8451 LISTVIEW_CreateHeader(infoPtr);
8452
8453 style = GetWindowLongW(infoPtr->hwndHeader, GWL_STYLE);
8454 if (infoPtr->dwLvExStyle & LVS_EX_HEADERDRAGDROP)
8455 style |= HDS_DRAGDROP;
8456 else
8457 style &= ~HDS_DRAGDROP;
8458 SetWindowLongW(infoPtr->hwndHeader, GWL_STYLE, style);
8459 }
8460
8461 /* GRIDLINES adds decoration at top so changes sizes */
8462 if((infoPtr->dwLvExStyle ^ old_ex_style) & LVS_EX_GRIDLINES)
8463 {
8464 LISTVIEW_CreateHeader(infoPtr);
8465 LISTVIEW_UpdateSize(infoPtr);
8466 }
8467
8468 if((infoPtr->dwLvExStyle ^ old_ex_style) & LVS_EX_FULLROWSELECT)
8469 {
8470 LISTVIEW_CreateHeader(infoPtr);
8471 }
8472
8473 if((infoPtr->dwLvExStyle ^ old_ex_style) & LVS_EX_TRANSPARENTBKGND)
8474 {
8475 if (infoPtr->dwLvExStyle & LVS_EX_TRANSPARENTBKGND)
8476 LISTVIEW_SetBkColor(infoPtr, CLR_NONE);
8477 }
8478
8479 if((infoPtr->dwLvExStyle ^ old_ex_style) & LVS_EX_HEADERINALLVIEWS)
8480 {
8481 if (infoPtr->dwLvExStyle & LVS_EX_HEADERINALLVIEWS)
8482 LISTVIEW_CreateHeader(infoPtr);
8483 else
8484 ShowWindow(infoPtr->hwndHeader, SW_HIDE);
8485 LISTVIEW_UpdateSize(infoPtr);
8486 LISTVIEW_UpdateScroll(infoPtr);
8487 }
8488
8489 LISTVIEW_InvalidateList(infoPtr);
8490 return old_ex_style;
8491 }
8492
8493 /***
8494 * DESCRIPTION:
8495 * Sets the new hot cursor used during hot tracking and hover selection.
8496 *
8497 * PARAMETER(S):
8498 * [I] infoPtr : valid pointer to the listview structure
8499 * [I] hCursor : the new hot cursor handle
8500 *
8501 * RETURN:
8502 * Returns the previous hot cursor
8503 */
8504 static HCURSOR LISTVIEW_SetHotCursor(LISTVIEW_INFO *infoPtr, HCURSOR hCursor)
8505 {
8506 HCURSOR oldCursor = infoPtr->hHotCursor;
8507
8508 infoPtr->hHotCursor = hCursor;
8509
8510 return oldCursor;
8511 }
8512
8513
8514 /***
8515 * DESCRIPTION:
8516 * Sets the hot item index.
8517 *
8518 * PARAMETERS:
8519 * [I] infoPtr : valid pointer to the listview structure
8520 * [I] iIndex : index
8521 *
8522 * RETURN:
8523 * SUCCESS : previous hot item index
8524 * FAILURE : -1 (no hot item)
8525 */
8526 static INT LISTVIEW_SetHotItem(LISTVIEW_INFO *infoPtr, INT iIndex)
8527 {
8528 INT iOldIndex = infoPtr->nHotItem;
8529
8530 infoPtr->nHotItem = iIndex;
8531
8532 return iOldIndex;
8533 }
8534
8535
8536 /***
8537 * DESCRIPTION:
8538 * Sets the amount of time the cursor must hover over an item before it is selected.
8539 *
8540 * PARAMETER(S):
8541 * [I] infoPtr : valid pointer to the listview structure
8542 * [I] dwHoverTime : hover time, if -1 the hover time is set to the default
8543 *
8544 * RETURN:
8545 * Returns the previous hover time
8546 */
8547 static DWORD LISTVIEW_SetHoverTime(LISTVIEW_INFO *infoPtr, DWORD dwHoverTime)
8548 {
8549 DWORD oldHoverTime = infoPtr->dwHoverTime;
8550
8551 infoPtr->dwHoverTime = dwHoverTime;
8552
8553 return oldHoverTime;
8554 }
8555
8556 /***
8557 * DESCRIPTION:
8558 * Sets spacing for icons of LVS_ICON style.
8559 *
8560 * PARAMETER(S):
8561 * [I] infoPtr : valid pointer to the listview structure
8562 * [I] cx : horizontal spacing (-1 = system spacing, 0 = autosize)
8563 * [I] cy : vertical spacing (-1 = system spacing, 0 = autosize)
8564 *
8565 * RETURN:
8566 * MAKELONG(oldcx, oldcy)
8567 */
8568 static DWORD LISTVIEW_SetIconSpacing(LISTVIEW_INFO *infoPtr, INT cx, INT cy)
8569 {
8570 INT iconWidth = 0, iconHeight = 0;
8571 DWORD oldspacing = MAKELONG(infoPtr->iconSpacing.cx, infoPtr->iconSpacing.cy);
8572
8573 TRACE("requested=(%d,%d)\n", cx, cy);
8574
8575 /* set to defaults, if instructed to */
8576 if (cx == -1 && cy == -1)
8577 {
8578 infoPtr->autoSpacing = TRUE;
8579 if (infoPtr->himlNormal)
8580 ImageList_GetIconSize(infoPtr->himlNormal, &iconWidth, &iconHeight);
8581 cx = GetSystemMetrics(SM_CXICONSPACING) - GetSystemMetrics(SM_CXICON) + iconWidth;
8582 cy = GetSystemMetrics(SM_CYICONSPACING) - GetSystemMetrics(SM_CYICON) + iconHeight;
8583 }
8584 else
8585 infoPtr->autoSpacing = FALSE;
8586
8587 /* if 0 then keep width */
8588 if (cx != 0)
8589 infoPtr->iconSpacing.cx = cx;
8590
8591 /* if 0 then keep height */
8592 if (cy != 0)
8593 infoPtr->iconSpacing.cy = cy;
8594
8595 TRACE("old=(%d,%d), new=(%d,%d), iconSize=(%d,%d), ntmH=%d\n",
8596 LOWORD(oldspacing), HIWORD(oldspacing), infoPtr->iconSpacing.cx, infoPtr->iconSpacing.cy,
8597 infoPtr->iconSize.cx, infoPtr->iconSize.cy,
8598 infoPtr->ntmHeight);
8599
8600 /* these depend on the iconSpacing */
8601 LISTVIEW_UpdateItemSize(infoPtr);
8602
8603 return oldspacing;
8604 }
8605
8606 static inline void set_icon_size(SIZE *size, HIMAGELIST himl, BOOL is_small)
8607 {
8608 INT cx, cy;
8609
8610 if (himl && ImageList_GetIconSize(himl, &cx, &cy))
8611 {
8612 size->cx = cx;
8613 size->cy = cy;
8614 }
8615 else
8616 {
8617 size->cx = GetSystemMetrics(is_small ? SM_CXSMICON : SM_CXICON);
8618 size->cy = GetSystemMetrics(is_small ? SM_CYSMICON : SM_CYICON);
8619 }
8620 }
8621
8622 /***
8623 * DESCRIPTION:
8624 * Sets image lists.
8625 *
8626 * PARAMETER(S):
8627 * [I] infoPtr : valid pointer to the listview structure
8628 * [I] nType : image list type
8629 * [I] himl : image list handle
8630 *
8631 * RETURN:
8632 * SUCCESS : old image list
8633 * FAILURE : NULL
8634 */
8635 static HIMAGELIST LISTVIEW_SetImageList(LISTVIEW_INFO *infoPtr, INT nType, HIMAGELIST himl)
8636 {
8637 INT oldHeight = infoPtr->nItemHeight;
8638 HIMAGELIST himlOld = 0;
8639
8640 TRACE("(nType=%d, himl=%p)\n", nType, himl);
8641
8642 switch (nType)
8643 {
8644 case LVSIL_NORMAL:
8645 himlOld = infoPtr->himlNormal;
8646 infoPtr->himlNormal = himl;
8647 if (infoPtr->uView == LV_VIEW_ICON) set_icon_size(&infoPtr->iconSize, himl, FALSE);
8648 if (infoPtr->autoSpacing)
8649 LISTVIEW_SetIconSpacing(infoPtr, -1, -1);
8650 break;
8651
8652 case LVSIL_SMALL:
8653 himlOld = infoPtr->himlSmall;
8654 infoPtr->himlSmall = himl;
8655 if (infoPtr->uView != LV_VIEW_ICON) set_icon_size(&infoPtr->iconSize, himl, TRUE);
8656 if (infoPtr->hwndHeader)
8657 SendMessageW(infoPtr->hwndHeader, HDM_SETIMAGELIST, 0, (LPARAM)himl);
8658 break;
8659
8660 case LVSIL_STATE:
8661 himlOld = infoPtr->himlState;
8662 infoPtr->himlState = himl;
8663 set_icon_size(&infoPtr->iconStateSize, himl, TRUE);
8664 ImageList_SetBkColor(infoPtr->himlState, CLR_NONE);
8665 break;
8666
8667 default:
8668 ERR("Unknown icon type=%d\n", nType);
8669 return NULL;
8670 }
8671
8672 infoPtr->nItemHeight = LISTVIEW_CalculateItemHeight(infoPtr);
8673 if (infoPtr->nItemHeight != oldHeight)
8674 LISTVIEW_UpdateScroll(infoPtr);
8675
8676 return himlOld;
8677 }
8678
8679 /***
8680 * DESCRIPTION:
8681 * Preallocates memory (does *not* set the actual count of items !)
8682 *
8683 * PARAMETER(S):
8684 * [I] infoPtr : valid pointer to the listview structure
8685 * [I] nItems : item count (projected number of items to allocate)
8686 * [I] dwFlags : update flags
8687 *
8688 * RETURN:
8689 * SUCCESS : TRUE
8690 * FAILURE : FALSE
8691 */
8692 static BOOL LISTVIEW_SetItemCount(LISTVIEW_INFO *infoPtr, INT nItems, DWORD dwFlags)
8693 {
8694 TRACE("(nItems=%d, dwFlags=%x)\n", nItems, dwFlags);
8695
8696 if (infoPtr->dwStyle & LVS_OWNERDATA)
8697 {
8698 INT nOldCount = infoPtr->nItemCount;
8699
8700 if (nItems < nOldCount)
8701 {
8702 RANGE range = { nItems, nOldCount };
8703 ranges_del(infoPtr->selectionRanges, range);
8704 if (infoPtr->nFocusedItem >= nItems)
8705 {
8706 LISTVIEW_SetItemFocus(infoPtr, -1);
8707 SetRectEmpty(&infoPtr->rcFocus);
8708 }
8709 }
8710
8711 infoPtr->nItemCount = nItems;
8712 LISTVIEW_UpdateScroll(infoPtr);
8713
8714 /* the flags are valid only in ownerdata report and list modes */
8715 if (infoPtr->uView == LV_VIEW_ICON || infoPtr->uView == LV_VIEW_SMALLICON) dwFlags = 0;
8716
8717 if (!(dwFlags & LVSICF_NOSCROLL) && infoPtr->nFocusedItem != -1)
8718 LISTVIEW_EnsureVisible(infoPtr, infoPtr->nFocusedItem, FALSE);
8719
8720 if (!(dwFlags & LVSICF_NOINVALIDATEALL))
8721 LISTVIEW_InvalidateList(infoPtr);
8722 else
8723 {
8724 INT nFrom, nTo;
8725 POINT Origin;
8726 RECT rcErase;
8727
8728 LISTVIEW_GetOrigin(infoPtr, &Origin);
8729 nFrom = min(nOldCount, nItems);
8730 nTo = max(nOldCount, nItems);
8731
8732 if (infoPtr->uView == LV_VIEW_DETAILS)
8733 {
8734 rcErase.left = 0;
8735 rcErase.top = nFrom * infoPtr->nItemHeight;
8736 rcErase.right = infoPtr->nItemWidth;
8737 rcErase.bottom = nTo * infoPtr->nItemHeight;
8738 OffsetRect(&rcErase, Origin.x, Origin.y);
8739 if (IntersectRect(&rcErase, &rcErase, &infoPtr->rcList))
8740 LISTVIEW_InvalidateRect(infoPtr, &rcErase);
8741 }
8742 else /* LV_VIEW_LIST */
8743 {
8744 INT nPerCol = LISTVIEW_GetCountPerColumn(infoPtr);
8745
8746 rcErase.left = (nFrom / nPerCol) * infoPtr->nItemWidth;
8747 rcErase.top = (nFrom % nPerCol) * infoPtr->nItemHeight;
8748 rcErase.right = rcErase.left + infoPtr->nItemWidth;
8749 rcErase.bottom = nPerCol * infoPtr->nItemHeight;
8750 OffsetRect(&rcErase, Origin.x, Origin.y);
8751 if (IntersectRect(&rcErase, &rcErase, &infoPtr->rcList))
8752 LISTVIEW_InvalidateRect(infoPtr, &rcErase);
8753
8754 rcErase.left = (nFrom / nPerCol + 1) * infoPtr->nItemWidth;
8755 rcErase.top = 0;
8756 rcErase.right = (nTo / nPerCol + 1) * infoPtr->nItemWidth;
8757 rcErase.bottom = nPerCol * infoPtr->nItemHeight;
8758 OffsetRect(&rcErase, Origin.x, Origin.y);
8759 if (IntersectRect(&rcErase, &rcErase, &infoPtr->rcList))
8760 LISTVIEW_InvalidateRect(infoPtr, &rcErase);
8761 }
8762 }
8763 }
8764 else
8765 {
8766 /* According to MSDN for non-LVS_OWNERDATA this is just
8767 * a performance issue. The control allocates its internal
8768 * data structures for the number of items specified. It
8769 * cuts down on the number of memory allocations. Therefore
8770 * we will just issue a WARN here
8771 */
8772 WARN("for non-ownerdata performance option not implemented.\n");
8773 }
8774
8775 return TRUE;
8776 }
8777
8778 /***
8779 * DESCRIPTION:
8780 * Sets the position of an item.
8781 *
8782 * PARAMETER(S):
8783 * [I] infoPtr : valid pointer to the listview structure
8784 * [I] nItem : item index
8785 * [I] pt : coordinate
8786 *
8787 * RETURN:
8788 * SUCCESS : TRUE
8789 * FAILURE : FALSE
8790 */
8791 static BOOL LISTVIEW_SetItemPosition(LISTVIEW_INFO *infoPtr, INT nItem, const POINT *pt)
8792 {
8793 POINT Origin, Pt;
8794
8795 TRACE("(nItem=%d, pt=%s)\n", nItem, wine_dbgstr_point(pt));
8796
8797 if (!pt || nItem < 0 || nItem >= infoPtr->nItemCount ||
8798 !(infoPtr->uView == LV_VIEW_ICON || infoPtr->uView == LV_VIEW_SMALLICON)) return FALSE;
8799
8800 Pt = *pt;
8801 LISTVIEW_GetOrigin(infoPtr, &Origin);
8802
8803 /* This point value seems to be an undocumented feature.
8804 * The best guess is that it means either at the origin,
8805 * or at true beginning of the list. I will assume the origin. */
8806 if ((Pt.x == -1) && (Pt.y == -1))
8807 Pt = Origin;
8808
8809 if (infoPtr->uView == LV_VIEW_ICON)
8810 {
8811 Pt.x -= (infoPtr->nItemWidth - infoPtr->iconSize.cx) / 2;
8812 Pt.y -= ICON_TOP_PADDING;
8813 }
8814 Pt.x -= Origin.x;
8815 Pt.y -= Origin.y;
8816
8817 infoPtr->bAutoarrange = FALSE;
8818
8819 return LISTVIEW_MoveIconTo(infoPtr, nItem, &Pt, FALSE);
8820 }
8821
8822 /***
8823 * DESCRIPTION:
8824 * Sets the state of one or many items.
8825 *
8826 * PARAMETER(S):
8827 * [I] infoPtr : valid pointer to the listview structure
8828 * [I] nItem : item index
8829 * [I] item : item or subitem info
8830 *
8831 * RETURN:
8832 * SUCCESS : TRUE
8833 * FAILURE : FALSE
8834 */
8835 static BOOL LISTVIEW_SetItemState(LISTVIEW_INFO *infoPtr, INT nItem, const LVITEMW *item)
8836 {
8837 BOOL ret = TRUE;
8838 LVITEMW lvItem;
8839
8840 if (!item) return FALSE;
8841
8842 lvItem.iItem = nItem;
8843 lvItem.iSubItem = 0;
8844 lvItem.mask = LVIF_STATE;
8845 lvItem.state = item->state;
8846 lvItem.stateMask = item->stateMask;
8847 TRACE("item=%s\n", debuglvitem_t(&lvItem, TRUE));
8848
8849 if (nItem == -1)
8850 {
8851 UINT oldstate = 0;
8852 BOOL notify;
8853
8854 /* special case optimization for recurring attempt to deselect all */
8855 if (lvItem.state == 0 && lvItem.stateMask == LVIS_SELECTED && !LISTVIEW_GetSelectedCount(infoPtr))
8856 return TRUE;
8857
8858 /* select all isn't allowed in LVS_SINGLESEL */
8859 if ((lvItem.state & lvItem.stateMask & LVIS_SELECTED) && (infoPtr->dwStyle & LVS_SINGLESEL))
8860 return FALSE;
8861
8862 /* focus all isn't allowed */
8863 if (lvItem.state & lvItem.stateMask & LVIS_FOCUSED) return FALSE;
8864
8865 notify = infoPtr->bDoChangeNotify;
8866 if (infoPtr->dwStyle & LVS_OWNERDATA)
8867 {
8868 infoPtr->bDoChangeNotify = FALSE;
8869 if (!(lvItem.state & LVIS_SELECTED) && LISTVIEW_GetSelectedCount(infoPtr))
8870 oldstate |= LVIS_SELECTED;
8871 if (infoPtr->nFocusedItem != -1) oldstate |= LVIS_FOCUSED;
8872 }
8873
8874 /* apply to all items */
8875 for (lvItem.iItem = 0; lvItem.iItem < infoPtr->nItemCount; lvItem.iItem++)
8876 if (!LISTVIEW_SetItemT(infoPtr, &lvItem, TRUE)) ret = FALSE;
8877
8878 if (infoPtr->dwStyle & LVS_OWNERDATA)
8879 {
8880 NMLISTVIEW nmlv;
8881
8882 infoPtr->bDoChangeNotify = notify;
8883
8884 nmlv.iItem = -1;
8885 nmlv.iSubItem = 0;
8886 nmlv.uNewState = lvItem.state & lvItem.stateMask;
8887 nmlv.uOldState = oldstate & lvItem.stateMask;
8888 nmlv.uChanged = LVIF_STATE;
8889 nmlv.ptAction.x = nmlv.ptAction.y = 0;
8890 nmlv.lParam = 0;
8891
8892 notify_listview(infoPtr, LVN_ITEMCHANGED, &nmlv);
8893 }
8894 }
8895 else
8896 ret = LISTVIEW_SetItemT(infoPtr, &lvItem, TRUE);
8897
8898 return ret;
8899 }
8900
8901 /***
8902 * DESCRIPTION:
8903 * Sets the text of an item or subitem.
8904 *
8905 * PARAMETER(S):
8906 * [I] hwnd : window handle
8907 * [I] nItem : item index
8908 * [I] lpLVItem : item or subitem info
8909 * [I] isW : TRUE if input is Unicode
8910 *
8911 * RETURN:
8912 * SUCCESS : TRUE
8913 * FAILURE : FALSE
8914 */
8915 static BOOL LISTVIEW_SetItemTextT(LISTVIEW_INFO *infoPtr, INT nItem, const LVITEMW *lpLVItem, BOOL isW)
8916 {
8917 LVITEMW lvItem;
8918
8919 if (!lpLVItem || nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
8920 if (infoPtr->dwStyle & LVS_OWNERDATA) return FALSE;
8921
8922 lvItem.iItem = nItem;
8923 lvItem.iSubItem = lpLVItem->iSubItem;
8924 lvItem.mask = LVIF_TEXT;
8925 lvItem.pszText = lpLVItem->pszText;
8926 lvItem.cchTextMax = lpLVItem->cchTextMax;
8927
8928 TRACE("(nItem=%d, lpLVItem=%s, isW=%d)\n", nItem, debuglvitem_t(&lvItem, isW), isW);
8929
8930 return LISTVIEW_SetItemT(infoPtr, &lvItem, isW);
8931 }
8932
8933 /***
8934 * DESCRIPTION:
8935 * Set item index that marks the start of a multiple selection.
8936 *
8937 * PARAMETER(S):
8938 * [I] infoPtr : valid pointer to the listview structure
8939 * [I] nIndex : index
8940 *
8941 * RETURN:
8942 * Index number or -1 if there is no selection mark.
8943 */
8944 static INT LISTVIEW_SetSelectionMark(LISTVIEW_INFO *infoPtr, INT nIndex)
8945 {
8946 INT nOldIndex = infoPtr->nSelectionMark;
8947
8948 TRACE("(nIndex=%d)\n", nIndex);
8949
8950 infoPtr->nSelectionMark = nIndex;
8951
8952 return nOldIndex;
8953 }
8954
8955 /***
8956 * DESCRIPTION:
8957 * Sets the text background color.
8958 *
8959 * PARAMETER(S):
8960 * [I] infoPtr : valid pointer to the listview structure
8961 * [I] color : text background color
8962 *
8963 * RETURN:
8964 * SUCCESS : TRUE
8965 * FAILURE : FALSE
8966 */
8967 static BOOL LISTVIEW_SetTextBkColor(LISTVIEW_INFO *infoPtr, COLORREF color)
8968 {
8969 TRACE("(color=%x)\n", color);
8970
8971 infoPtr->clrTextBk = color;
8972 return TRUE;
8973 }
8974
8975 /***
8976 * DESCRIPTION:
8977 * Sets the text foreground color.
8978 *
8979 * PARAMETER(S):
8980 * [I] infoPtr : valid pointer to the listview structure
8981 * [I] color : text color
8982 *
8983 * RETURN:
8984 * SUCCESS : TRUE
8985 * FAILURE : FALSE
8986 */
8987 static BOOL LISTVIEW_SetTextColor (LISTVIEW_INFO *infoPtr, COLORREF color)
8988 {
8989 TRACE("(color=%x)\n", color);
8990
8991 infoPtr->clrText = color;
8992 return TRUE;
8993 }
8994
8995 /***
8996 * DESCRIPTION:
8997 * Sets new ToolTip window to ListView control.
8998 *
8999 * PARAMETER(S):
9000 * [I] infoPtr : valid pointer to the listview structure
9001 * [I] hwndNewToolTip : handle to new ToolTip
9002 *
9003 * RETURN:
9004 * old tool tip
9005 */
9006 static HWND LISTVIEW_SetToolTips( LISTVIEW_INFO *infoPtr, HWND hwndNewToolTip)
9007 {
9008 HWND hwndOldToolTip = infoPtr->hwndToolTip;
9009 infoPtr->hwndToolTip = hwndNewToolTip;
9010 return hwndOldToolTip;
9011 }
9012
9013 /*
9014 * DESCRIPTION:
9015 * sets the Unicode character format flag for the control
9016 * PARAMETER(S):
9017 * [I] infoPtr :valid pointer to the listview structure
9018 * [I] fUnicode :true to switch to UNICODE false to switch to ANSI
9019 *
9020 * RETURN:
9021 * Old Unicode Format
9022 */
9023 static BOOL LISTVIEW_SetUnicodeFormat( LISTVIEW_INFO *infoPtr, BOOL unicode)
9024 {
9025 SHORT rc = infoPtr->notifyFormat;
9026 infoPtr->notifyFormat = (unicode) ? NFR_UNICODE : NFR_ANSI;
9027 return rc == NFR_UNICODE;
9028 }
9029
9030 /*
9031 * DESCRIPTION:
9032 * sets the control view mode
9033 * PARAMETER(S):
9034 * [I] infoPtr :valid pointer to the listview structure
9035 * [I] nView :new view mode value
9036 *
9037 * RETURN:
9038 * SUCCESS: 1
9039 * FAILURE: -1
9040 */
9041 static INT LISTVIEW_SetView(LISTVIEW_INFO *infoPtr, DWORD nView)
9042 {
9043 HIMAGELIST himl;
9044
9045 if (infoPtr->uView == nView) return 1;
9046
9047 if ((INT)nView < 0 || nView > LV_VIEW_MAX) return -1;
9048 if (nView == LV_VIEW_TILE)
9049 {
9050 FIXME("View LV_VIEW_TILE unimplemented\n");
9051 return -1;
9052 }
9053
9054 infoPtr->uView = nView;
9055
9056 SendMessageW(infoPtr->hwndEdit, WM_KILLFOCUS, 0, 0);
9057 ShowWindow(infoPtr->hwndHeader, SW_HIDE);
9058
9059 ShowScrollBar(infoPtr->hwndSelf, SB_BOTH, FALSE);
9060 SetRectEmpty(&infoPtr->rcFocus);
9061
9062 himl = (nView == LV_VIEW_ICON ? infoPtr->himlNormal : infoPtr->himlSmall);
9063 set_icon_size(&infoPtr->iconSize, himl, nView != LV_VIEW_ICON);
9064
9065 switch (nView)
9066 {
9067 case LV_VIEW_ICON:
9068 case LV_VIEW_SMALLICON:
9069 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
9070 break;
9071 case LV_VIEW_DETAILS:
9072 {
9073 HDLAYOUT hl;
9074 WINDOWPOS wp;
9075
9076 LISTVIEW_CreateHeader( infoPtr );
9077
9078 hl.prc = &infoPtr->rcList;
9079 hl.pwpos = &wp;
9080 SendMessageW(infoPtr->hwndHeader, HDM_LAYOUT, 0, (LPARAM)&hl);
9081 SetWindowPos(infoPtr->hwndHeader, infoPtr->hwndSelf, wp.x, wp.y, wp.cx, wp.cy,
9082 wp.flags | ((infoPtr->dwStyle & LVS_NOCOLUMNHEADER) ? SWP_HIDEWINDOW : SWP_SHOWWINDOW));
9083 break;
9084 }
9085 case LV_VIEW_LIST:
9086 break;
9087 }
9088
9089 LISTVIEW_UpdateItemSize(infoPtr);
9090 LISTVIEW_UpdateSize(infoPtr);
9091 LISTVIEW_UpdateScroll(infoPtr);
9092 LISTVIEW_InvalidateList(infoPtr);
9093
9094 TRACE("nView=%d\n", nView);
9095
9096 return 1;
9097 }
9098
9099 /* LISTVIEW_SetWorkAreas */
9100
9101 /***
9102 * DESCRIPTION:
9103 * Callback internally used by LISTVIEW_SortItems() in response of LVM_SORTITEMS
9104 *
9105 * PARAMETER(S):
9106 * [I] first : pointer to first ITEM_INFO to compare
9107 * [I] second : pointer to second ITEM_INFO to compare
9108 * [I] lParam : HWND of control
9109 *
9110 * RETURN:
9111 * if first comes before second : negative
9112 * if first comes after second : positive
9113 * if first and second are equivalent : zero
9114 */
9115 static INT WINAPI LISTVIEW_CallBackCompare(LPVOID first, LPVOID second, LPARAM lParam)
9116 {
9117 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)lParam;
9118 ITEM_INFO* lv_first = DPA_GetPtr( first, 0 );
9119 ITEM_INFO* lv_second = DPA_GetPtr( second, 0 );
9120
9121 /* Forward the call to the client defined callback */
9122 return (infoPtr->pfnCompare)( lv_first->lParam , lv_second->lParam, infoPtr->lParamSort );
9123 }
9124
9125 /***
9126 * DESCRIPTION:
9127 * Callback internally used by LISTVIEW_SortItems() in response of LVM_SORTITEMSEX
9128 *
9129 * PARAMETER(S):
9130 * [I] first : pointer to first ITEM_INFO to compare
9131 * [I] second : pointer to second ITEM_INFO to compare
9132 * [I] lParam : HWND of control
9133 *
9134 * RETURN:
9135 * if first comes before second : negative
9136 * if first comes after second : positive
9137 * if first and second are equivalent : zero
9138 */
9139 static INT WINAPI LISTVIEW_CallBackCompareEx(LPVOID first, LPVOID second, LPARAM lParam)
9140 {
9141 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)lParam;
9142 INT first_idx = DPA_GetPtrIndex( infoPtr->hdpaItems, first );
9143 INT second_idx = DPA_GetPtrIndex( infoPtr->hdpaItems, second );
9144
9145 /* Forward the call to the client defined callback */
9146 return (infoPtr->pfnCompare)( first_idx, second_idx, infoPtr->lParamSort );
9147 }
9148
9149 /***
9150 * DESCRIPTION:
9151 * Sorts the listview items.
9152 *
9153 * PARAMETER(S):
9154 * [I] infoPtr : valid pointer to the listview structure
9155 * [I] pfnCompare : application-defined value
9156 * [I] lParamSort : pointer to comparison callback
9157 * [I] IsEx : TRUE when LVM_SORTITEMSEX used
9158 *
9159 * RETURN:
9160 * SUCCESS : TRUE
9161 * FAILURE : FALSE
9162 */
9163 static BOOL LISTVIEW_SortItems(LISTVIEW_INFO *infoPtr, PFNLVCOMPARE pfnCompare,
9164 LPARAM lParamSort, BOOL IsEx)
9165 {
9166 HDPA hdpaSubItems;
9167 ITEM_INFO *lpItem;
9168 LPVOID selectionMarkItem = NULL;
9169 LPVOID focusedItem = NULL;
9170 int i;
9171
9172 TRACE("(pfnCompare=%p, lParamSort=%lx)\n", pfnCompare, lParamSort);
9173
9174 if (infoPtr->dwStyle & LVS_OWNERDATA) return FALSE;
9175
9176 if (!pfnCompare) return FALSE;
9177 if (!infoPtr->hdpaItems) return FALSE;
9178
9179 /* if there are 0 or 1 items, there is no need to sort */
9180 if (infoPtr->nItemCount < 2) return TRUE;
9181
9182 /* clear selection */
9183 ranges_clear(infoPtr->selectionRanges);
9184
9185 /* save selection mark and focused item */
9186 if (infoPtr->nSelectionMark >= 0)
9187 selectionMarkItem = DPA_GetPtr(infoPtr->hdpaItems, infoPtr->nSelectionMark);
9188 if (infoPtr->nFocusedItem >= 0)
9189 focusedItem = DPA_GetPtr(infoPtr->hdpaItems, infoPtr->nFocusedItem);
9190
9191 infoPtr->pfnCompare = pfnCompare;
9192 infoPtr->lParamSort = lParamSort;
9193 if (IsEx)
9194 DPA_Sort(infoPtr->hdpaItems, LISTVIEW_CallBackCompareEx, (LPARAM)infoPtr);
9195 else
9196 DPA_Sort(infoPtr->hdpaItems, LISTVIEW_CallBackCompare, (LPARAM)infoPtr);
9197
9198 /* restore selection ranges */
9199 for (i=0; i < infoPtr->nItemCount; i++)
9200 {
9201 hdpaSubItems = DPA_GetPtr(infoPtr->hdpaItems, i);
9202 lpItem = DPA_GetPtr(hdpaSubItems, 0);
9203
9204 if (lpItem->state & LVIS_SELECTED)
9205 ranges_additem(infoPtr->selectionRanges, i);
9206 }
9207 /* restore selection mark and focused item */
9208 infoPtr->nSelectionMark = DPA_GetPtrIndex(infoPtr->hdpaItems, selectionMarkItem);
9209 infoPtr->nFocusedItem = DPA_GetPtrIndex(infoPtr->hdpaItems, focusedItem);
9210
9211 /* I believe nHotItem should be left alone, see LISTVIEW_ShiftIndices */
9212
9213 /* refresh the display */
9214 LISTVIEW_InvalidateList(infoPtr);
9215 return TRUE;
9216 }
9217
9218 /***
9219 * DESCRIPTION:
9220 * Update theme handle after a theme change.
9221 *
9222 * PARAMETER(S):
9223 * [I] infoPtr : valid pointer to the listview structure
9224 *
9225 * RETURN:
9226 * SUCCESS : 0
9227 * FAILURE : something else
9228 */
9229 static LRESULT LISTVIEW_ThemeChanged(const LISTVIEW_INFO *infoPtr)
9230 {
9231 HTHEME theme = GetWindowTheme(infoPtr->hwndSelf);
9232 CloseThemeData(theme);
9233 OpenThemeData(infoPtr->hwndSelf, themeClass);
9234 return 0;
9235 }
9236
9237 /***
9238 * DESCRIPTION:
9239 * Updates an items or rearranges the listview control.
9240 *
9241 * PARAMETER(S):
9242 * [I] infoPtr : valid pointer to the listview structure
9243 * [I] nItem : item index
9244 *
9245 * RETURN:
9246 * SUCCESS : TRUE
9247 * FAILURE : FALSE
9248 */
9249 static BOOL LISTVIEW_Update(LISTVIEW_INFO *infoPtr, INT nItem)
9250 {
9251 TRACE("(nItem=%d)\n", nItem);
9252
9253 if (nItem < 0 || nItem >= infoPtr->nItemCount) return FALSE;
9254
9255 /* rearrange with default alignment style */
9256 if (is_autoarrange(infoPtr))
9257 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
9258 else
9259 LISTVIEW_InvalidateItem(infoPtr, nItem);
9260
9261 return TRUE;
9262 }
9263
9264 /***
9265 * DESCRIPTION:
9266 * Draw the track line at the place defined in the infoPtr structure.
9267 * The line is drawn with a XOR pen so drawing the line for the second time
9268 * in the same place erases the line.
9269 *
9270 * PARAMETER(S):
9271 * [I] infoPtr : valid pointer to the listview structure
9272 *
9273 * RETURN:
9274 * SUCCESS : TRUE
9275 * FAILURE : FALSE
9276 */
9277 static BOOL LISTVIEW_DrawTrackLine(const LISTVIEW_INFO *infoPtr)
9278 {
9279 HDC hdc;
9280
9281 if (infoPtr->xTrackLine == -1)
9282 return FALSE;
9283
9284 if (!(hdc = GetDC(infoPtr->hwndSelf)))
9285 return FALSE;
9286 PatBlt( hdc, infoPtr->xTrackLine, infoPtr->rcList.top,
9287 1, infoPtr->rcList.bottom - infoPtr->rcList.top, DSTINVERT );
9288 ReleaseDC(infoPtr->hwndSelf, hdc);
9289 return TRUE;
9290 }
9291
9292 /***
9293 * DESCRIPTION:
9294 * Called when an edit control should be displayed. This function is called after
9295 * we are sure that there was a single click - not a double click (this is a TIMERPROC).
9296 *
9297 * PARAMETER(S):
9298 * [I] hwnd : Handle to the listview
9299 * [I] uMsg : WM_TIMER (ignored)
9300 * [I] idEvent : The timer ID interpreted as a pointer to a DELAYED_EDIT_ITEM struct
9301 * [I] dwTimer : The elapsed time (ignored)
9302 *
9303 * RETURN:
9304 * None.
9305 */
9306 static VOID CALLBACK LISTVIEW_DelayedEditItem(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime)
9307 {
9308 DELAYED_ITEM_EDIT *editItem = (DELAYED_ITEM_EDIT *)idEvent;
9309 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)GetWindowLongPtrW(hwnd, 0);
9310
9311 KillTimer(hwnd, idEvent);
9312 editItem->fEnabled = FALSE;
9313 /* check if the item is still selected */
9314 if (infoPtr->bFocus && LISTVIEW_GetItemState(infoPtr, editItem->iItem, LVIS_SELECTED))
9315 LISTVIEW_EditLabelT(infoPtr, editItem->iItem, TRUE);
9316 }
9317
9318 /***
9319 * DESCRIPTION:
9320 * Creates the listview control - the WM_NCCREATE phase.
9321 *
9322 * PARAMETER(S):
9323 * [I] hwnd : window handle
9324 * [I] lpcs : the create parameters
9325 *
9326 * RETURN:
9327 * Success: TRUE
9328 * Failure: FALSE
9329 */
9330 static LRESULT LISTVIEW_NCCreate(HWND hwnd, const CREATESTRUCTW *lpcs)
9331 {
9332 LISTVIEW_INFO *infoPtr;
9333 LOGFONTW logFont;
9334
9335 TRACE("(lpcs=%p)\n", lpcs);
9336
9337 /* initialize info pointer */
9338 infoPtr = Alloc(sizeof(LISTVIEW_INFO));
9339 if (!infoPtr) return FALSE;
9340
9341 SetWindowLongPtrW(hwnd, 0, (DWORD_PTR)infoPtr);
9342
9343 infoPtr->hwndSelf = hwnd;
9344 infoPtr->dwStyle = lpcs->style; /* Note: may be changed in WM_CREATE */
9345 map_style_view(infoPtr);
9346 /* determine the type of structures to use */
9347 infoPtr->hwndNotify = lpcs->hwndParent;
9348 /* infoPtr->notifyFormat will be filled in WM_CREATE */
9349
9350 /* initialize color information */
9351 infoPtr->clrBk = CLR_NONE;
9352 infoPtr->clrText = CLR_DEFAULT;
9353 infoPtr->clrTextBk = CLR_DEFAULT;
9354 LISTVIEW_SetBkColor(infoPtr, comctl32_color.clrWindow);
9355 infoPtr->bDefaultBkColor = TRUE;
9356
9357 /* set default values */
9358 infoPtr->nFocusedItem = -1;
9359 infoPtr->nSelectionMark = -1;
9360 infoPtr->nHotItem = -1;
9361 infoPtr->bRedraw = TRUE;
9362 infoPtr->bNoItemMetrics = TRUE;
9363 infoPtr->bDoChangeNotify = TRUE;
9364 infoPtr->autoSpacing = TRUE;
9365 infoPtr->iconSpacing.cx = GetSystemMetrics(SM_CXICONSPACING) - GetSystemMetrics(SM_CXICON);
9366 infoPtr->iconSpacing.cy = GetSystemMetrics(SM_CYICONSPACING) - GetSystemMetrics(SM_CYICON);
9367 infoPtr->nEditLabelItem = -1;
9368 infoPtr->nLButtonDownItem = -1;
9369 infoPtr->dwHoverTime = HOVER_DEFAULT; /* default system hover time */
9370 infoPtr->cWheelRemainder = 0;
9371 infoPtr->nMeasureItemHeight = 0;
9372 infoPtr->xTrackLine = -1; /* no track line */
9373 infoPtr->itemEdit.fEnabled = FALSE;
9374 infoPtr->iVersion = COMCTL32_VERSION;
9375 infoPtr->colRectsDirty = FALSE;
9376
9377 /* get default font (icon title) */
9378 SystemParametersInfoW(SPI_GETICONTITLELOGFONT, 0, &logFont, 0);
9379 infoPtr->hDefaultFont = CreateFontIndirectW(&logFont);
9380 infoPtr->hFont = infoPtr->hDefaultFont;
9381 LISTVIEW_SaveTextMetrics(infoPtr);
9382
9383 /* allocate memory for the data structure */
9384 if (!(infoPtr->selectionRanges = ranges_create(10))) goto fail;
9385 if (!(infoPtr->hdpaItems = DPA_Create(10))) goto fail;
9386 if (!(infoPtr->hdpaItemIds = DPA_Create(10))) goto fail;
9387 if (!(infoPtr->hdpaPosX = DPA_Create(10))) goto fail;
9388 if (!(infoPtr->hdpaPosY = DPA_Create(10))) goto fail;
9389 if (!(infoPtr->hdpaColumns = DPA_Create(10))) goto fail;
9390 return TRUE;
9391
9392 fail:
9393 DestroyWindow(infoPtr->hwndHeader);
9394 ranges_destroy(infoPtr->selectionRanges);
9395 DPA_Destroy(infoPtr->hdpaItems);
9396 DPA_Destroy(infoPtr->hdpaItemIds);
9397 DPA_Destroy(infoPtr->hdpaPosX);
9398 DPA_Destroy(infoPtr->hdpaPosY);
9399 DPA_Destroy(infoPtr->hdpaColumns);
9400 Free(infoPtr);
9401 return FALSE;
9402 }
9403
9404 /***
9405 * DESCRIPTION:
9406 * Creates the listview control - the WM_CREATE phase. Most of the data is
9407 * already set up in LISTVIEW_NCCreate
9408 *
9409 * PARAMETER(S):
9410 * [I] hwnd : window handle
9411 * [I] lpcs : the create parameters
9412 *
9413 * RETURN:
9414 * Success: 0
9415 * Failure: -1
9416 */
9417 static LRESULT LISTVIEW_Create(HWND hwnd, const CREATESTRUCTW *lpcs)
9418 {
9419 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)GetWindowLongPtrW(hwnd, 0);
9420
9421 TRACE("(lpcs=%p, style=0x%08x)\n", lpcs, lpcs->style);
9422
9423 infoPtr->dwStyle = lpcs->style;
9424 map_style_view(infoPtr);
9425
9426 infoPtr->notifyFormat = SendMessageW(infoPtr->hwndNotify, WM_NOTIFYFORMAT,
9427 (WPARAM)infoPtr->hwndSelf, NF_QUERY);
9428 /* on error defaulting to ANSI notifications */
9429 if (infoPtr->notifyFormat == 0) infoPtr->notifyFormat = NFR_ANSI;
9430 TRACE("notify format=%d\n", infoPtr->notifyFormat);
9431
9432 if ((infoPtr->uView == LV_VIEW_DETAILS) && (lpcs->style & WS_VISIBLE))
9433 {
9434 if (LISTVIEW_CreateHeader(infoPtr) < 0) return -1;
9435 }
9436 else
9437 infoPtr->hwndHeader = 0;
9438
9439 /* init item size to avoid division by 0 */
9440 LISTVIEW_UpdateItemSize (infoPtr);
9441 LISTVIEW_UpdateSize (infoPtr);
9442
9443 if (infoPtr->uView == LV_VIEW_DETAILS)
9444 {
9445 if (!(LVS_NOCOLUMNHEADER & lpcs->style) && (WS_VISIBLE & lpcs->style))
9446 {
9447 ShowWindow(infoPtr->hwndHeader, SW_SHOWNORMAL);
9448 }
9449 LISTVIEW_UpdateScroll(infoPtr);
9450 /* send WM_MEASUREITEM notification */
9451 if (infoPtr->dwStyle & LVS_OWNERDRAWFIXED) notify_measureitem(infoPtr);
9452 }
9453
9454 OpenThemeData(hwnd, themeClass);
9455
9456 /* initialize the icon sizes */
9457 set_icon_size(&infoPtr->iconSize, infoPtr->himlNormal, infoPtr->uView != LV_VIEW_ICON);
9458 set_icon_size(&infoPtr->iconStateSize, infoPtr->himlState, TRUE);
9459 return 0;
9460 }
9461
9462 /***
9463 * DESCRIPTION:
9464 * Destroys the listview control.
9465 *
9466 * PARAMETER(S):
9467 * [I] infoPtr : valid pointer to the listview structure
9468 *
9469 * RETURN:
9470 * Success: 0
9471 * Failure: -1
9472 */
9473 static LRESULT LISTVIEW_Destroy(LISTVIEW_INFO *infoPtr)
9474 {
9475 HTHEME theme = GetWindowTheme(infoPtr->hwndSelf);
9476 CloseThemeData(theme);
9477
9478 /* delete all items */
9479 LISTVIEW_DeleteAllItems(infoPtr, TRUE);
9480
9481 return 0;
9482 }
9483
9484 /***
9485 * DESCRIPTION:
9486 * Enables the listview control.
9487 *
9488 * PARAMETER(S):
9489 * [I] infoPtr : valid pointer to the listview structure
9490 * [I] bEnable : specifies whether to enable or disable the window
9491 *
9492 * RETURN:
9493 * SUCCESS : TRUE
9494 * FAILURE : FALSE
9495 */
9496 static BOOL LISTVIEW_Enable(const LISTVIEW_INFO *infoPtr)
9497 {
9498 if (infoPtr->dwStyle & LVS_OWNERDRAWFIXED)
9499 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
9500 return TRUE;
9501 }
9502
9503 /***
9504 * DESCRIPTION:
9505 * Erases the background of the listview control.
9506 *
9507 * PARAMETER(S):
9508 * [I] infoPtr : valid pointer to the listview structure
9509 * [I] hdc : device context handle
9510 *
9511 * RETURN:
9512 * SUCCESS : TRUE
9513 * FAILURE : FALSE
9514 */
9515 static inline BOOL LISTVIEW_EraseBkgnd(const LISTVIEW_INFO *infoPtr, HDC hdc)
9516 {
9517 RECT rc;
9518
9519 TRACE("(hdc=%p)\n", hdc);
9520
9521 if (!GetClipBox(hdc, &rc)) return FALSE;
9522
9523 if (infoPtr->clrBk == CLR_NONE)
9524 {
9525 if (infoPtr->dwLvExStyle & LVS_EX_TRANSPARENTBKGND)
9526 return SendMessageW(infoPtr->hwndNotify, WM_PRINTCLIENT,
9527 (WPARAM)hdc, PRF_ERASEBKGND);
9528 else
9529 return SendMessageW(infoPtr->hwndNotify, WM_ERASEBKGND, (WPARAM)hdc, 0);
9530 }
9531
9532 /* for double buffered controls we need to do this during refresh */
9533 if (infoPtr->dwLvExStyle & LVS_EX_DOUBLEBUFFER) return FALSE;
9534
9535 return LISTVIEW_FillBkgnd(infoPtr, hdc, &rc);
9536 }
9537
9538
9539 /***
9540 * DESCRIPTION:
9541 * Helper function for LISTVIEW_[HV]Scroll *only*.
9542 * Performs vertical/horizontal scrolling by a give amount.
9543 *
9544 * PARAMETER(S):
9545 * [I] infoPtr : valid pointer to the listview structure
9546 * [I] dx : amount of horizontal scroll
9547 * [I] dy : amount of vertical scroll
9548 */
9549 static void scroll_list(LISTVIEW_INFO *infoPtr, INT dx, INT dy)
9550 {
9551 /* now we can scroll the list */
9552 ScrollWindowEx(infoPtr->hwndSelf, dx, dy, &infoPtr->rcList,
9553 &infoPtr->rcList, 0, 0, SW_ERASE | SW_INVALIDATE);
9554 /* if we have focus, adjust rect */
9555 OffsetRect(&infoPtr->rcFocus, dx, dy);
9556 UpdateWindow(infoPtr->hwndSelf);
9557 }
9558
9559 /***
9560 * DESCRIPTION:
9561 * Performs vertical scrolling.
9562 *
9563 * PARAMETER(S):
9564 * [I] infoPtr : valid pointer to the listview structure
9565 * [I] nScrollCode : scroll code
9566 * [I] nScrollDiff : units to scroll in SB_INTERNAL mode, 0 otherwise
9567 * [I] hScrollWnd : scrollbar control window handle
9568 *
9569 * RETURN:
9570 * Zero
9571 *
9572 * NOTES:
9573 * SB_LINEUP/SB_LINEDOWN:
9574 * for LVS_ICON, LVS_SMALLICON is 37 by experiment
9575 * for LVS_REPORT is 1 line
9576 * for LVS_LIST cannot occur
9577 *
9578 */
9579 static LRESULT LISTVIEW_VScroll(LISTVIEW_INFO *infoPtr, INT nScrollCode,
9580 INT nScrollDiff)
9581 {
9582 INT nOldScrollPos, nNewScrollPos;
9583 SCROLLINFO scrollInfo;
9584 BOOL is_an_icon;
9585
9586 TRACE("(nScrollCode=%d(%s), nScrollDiff=%d)\n", nScrollCode,
9587 debugscrollcode(nScrollCode), nScrollDiff);
9588
9589 if (infoPtr->hwndEdit) SendMessageW(infoPtr->hwndEdit, WM_KILLFOCUS, 0, 0);
9590
9591 scrollInfo.cbSize = sizeof(SCROLLINFO);
9592 scrollInfo.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_TRACKPOS;
9593
9594 is_an_icon = ((infoPtr->uView == LV_VIEW_ICON) || (infoPtr->uView == LV_VIEW_SMALLICON));
9595
9596 if (!GetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo)) return 1;
9597
9598 nOldScrollPos = scrollInfo.nPos;
9599 switch (nScrollCode)
9600 {
9601 case SB_INTERNAL:
9602 break;
9603
9604 case SB_LINEUP:
9605 nScrollDiff = (is_an_icon) ? -LISTVIEW_SCROLL_ICON_LINE_SIZE : -1;
9606 break;
9607
9608 case SB_LINEDOWN:
9609 nScrollDiff = (is_an_icon) ? LISTVIEW_SCROLL_ICON_LINE_SIZE : 1;
9610 break;
9611
9612 case SB_PAGEUP:
9613 nScrollDiff = -scrollInfo.nPage;
9614 break;
9615
9616 case SB_PAGEDOWN:
9617 nScrollDiff = scrollInfo.nPage;
9618 break;
9619
9620 case SB_THUMBPOSITION:
9621 case SB_THUMBTRACK:
9622 nScrollDiff = scrollInfo.nTrackPos - scrollInfo.nPos;
9623 break;
9624
9625 default:
9626 nScrollDiff = 0;
9627 }
9628
9629 /* quit right away if pos isn't changing */
9630 if (nScrollDiff == 0) return 0;
9631
9632 /* calculate new position, and handle overflows */
9633 nNewScrollPos = scrollInfo.nPos + nScrollDiff;
9634 if (nScrollDiff > 0) {
9635 if (nNewScrollPos < nOldScrollPos ||
9636 nNewScrollPos > scrollInfo.nMax)
9637 nNewScrollPos = scrollInfo.nMax;
9638 } else {
9639 if (nNewScrollPos > nOldScrollPos ||
9640 nNewScrollPos < scrollInfo.nMin)
9641 nNewScrollPos = scrollInfo.nMin;
9642 }
9643
9644 /* set the new position, and reread in case it changed */
9645 scrollInfo.fMask = SIF_POS;
9646 scrollInfo.nPos = nNewScrollPos;
9647 nNewScrollPos = SetScrollInfo(infoPtr->hwndSelf, SB_VERT, &scrollInfo, TRUE);
9648
9649 /* carry on only if it really changed */
9650 if (nNewScrollPos == nOldScrollPos) return 0;
9651
9652 /* now adjust to client coordinates */
9653 nScrollDiff = nOldScrollPos - nNewScrollPos;
9654 if (infoPtr->uView == LV_VIEW_DETAILS) nScrollDiff *= infoPtr->nItemHeight;
9655
9656 /* and scroll the window */
9657 scroll_list(infoPtr, 0, nScrollDiff);
9658
9659 return 0;
9660 }
9661
9662 /***
9663 * DESCRIPTION:
9664 * Performs horizontal scrolling.
9665 *
9666 * PARAMETER(S):
9667 * [I] infoPtr : valid pointer to the listview structure
9668 * [I] nScrollCode : scroll code
9669 * [I] nScrollDiff : units to scroll in SB_INTERNAL mode, 0 otherwise
9670 * [I] hScrollWnd : scrollbar control window handle
9671 *
9672 * RETURN:
9673 * Zero
9674 *
9675 * NOTES:
9676 * SB_LINELEFT/SB_LINERIGHT:
9677 * for LVS_ICON, LVS_SMALLICON 1 pixel
9678 * for LVS_REPORT is 1 pixel
9679 * for LVS_LIST is 1 column --> which is a 1 because the
9680 * scroll is based on columns not pixels
9681 *
9682 */
9683 static LRESULT LISTVIEW_HScroll(LISTVIEW_INFO *infoPtr, INT nScrollCode,
9684 INT nScrollDiff)
9685 {
9686 INT nOldScrollPos, nNewScrollPos;
9687 SCROLLINFO scrollInfo;
9688 BOOL is_an_icon;
9689
9690 TRACE("(nScrollCode=%d(%s), nScrollDiff=%d)\n", nScrollCode,
9691 debugscrollcode(nScrollCode), nScrollDiff);
9692
9693 if (infoPtr->hwndEdit) SendMessageW(infoPtr->hwndEdit, WM_KILLFOCUS, 0, 0);
9694
9695 scrollInfo.cbSize = sizeof(SCROLLINFO);
9696 scrollInfo.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_TRACKPOS;
9697
9698 is_an_icon = ((infoPtr->uView == LV_VIEW_ICON) || (infoPtr->uView == LV_VIEW_SMALLICON));
9699
9700 if (!GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo)) return 1;
9701
9702 nOldScrollPos = scrollInfo.nPos;
9703
9704 switch (nScrollCode)
9705 {
9706 case SB_INTERNAL:
9707 break;
9708
9709 case SB_LINELEFT:
9710 nScrollDiff = (is_an_icon) ? -LISTVIEW_SCROLL_ICON_LINE_SIZE : -1;
9711 break;
9712
9713 case SB_LINERIGHT:
9714 nScrollDiff = (is_an_icon) ? LISTVIEW_SCROLL_ICON_LINE_SIZE : 1;
9715 break;
9716
9717 case SB_PAGELEFT:
9718 nScrollDiff = -scrollInfo.nPage;
9719 break;
9720
9721 case SB_PAGERIGHT:
9722 nScrollDiff = scrollInfo.nPage;
9723 break;
9724
9725 case SB_THUMBPOSITION:
9726 case SB_THUMBTRACK:
9727 nScrollDiff = scrollInfo.nTrackPos - scrollInfo.nPos;
9728 break;
9729
9730 default:
9731 nScrollDiff = 0;
9732 }
9733
9734 /* quit right away if pos isn't changing */
9735 if (nScrollDiff == 0) return 0;
9736
9737 /* calculate new position, and handle overflows */
9738 nNewScrollPos = scrollInfo.nPos + nScrollDiff;
9739 if (nScrollDiff > 0) {
9740 if (nNewScrollPos < nOldScrollPos ||
9741 nNewScrollPos > scrollInfo.nMax)
9742 nNewScrollPos = scrollInfo.nMax;
9743 } else {
9744 if (nNewScrollPos > nOldScrollPos ||
9745 nNewScrollPos < scrollInfo.nMin)
9746 nNewScrollPos = scrollInfo.nMin;
9747 }
9748
9749 /* set the new position, and reread in case it changed */
9750 scrollInfo.fMask = SIF_POS;
9751 scrollInfo.nPos = nNewScrollPos;
9752 nNewScrollPos = SetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &scrollInfo, TRUE);
9753
9754 /* carry on only if it really changed */
9755 if (nNewScrollPos == nOldScrollPos) return 0;
9756
9757 if (infoPtr->hwndHeader) LISTVIEW_UpdateHeaderSize(infoPtr, nNewScrollPos);
9758
9759 /* now adjust to client coordinates */
9760 nScrollDiff = nOldScrollPos - nNewScrollPos;
9761 if (infoPtr->uView == LV_VIEW_LIST) nScrollDiff *= infoPtr->nItemWidth;
9762
9763 /* and scroll the window */
9764 scroll_list(infoPtr, nScrollDiff, 0);
9765
9766 return 0;
9767 }
9768
9769 static LRESULT LISTVIEW_MouseWheel(LISTVIEW_INFO *infoPtr, INT wheelDelta)
9770 {
9771 UINT pulScrollLines = 3;
9772
9773 TRACE("(wheelDelta=%d)\n", wheelDelta);
9774
9775 switch(infoPtr->uView)
9776 {
9777 case LV_VIEW_ICON:
9778 case LV_VIEW_SMALLICON:
9779 /*
9780 * listview should be scrolled by a multiple of 37 dependently on its dimension or its visible item number
9781 * should be fixed in the future.
9782 */
9783 LISTVIEW_VScroll(infoPtr, SB_INTERNAL, (wheelDelta > 0) ?
9784 -LISTVIEW_SCROLL_ICON_LINE_SIZE : LISTVIEW_SCROLL_ICON_LINE_SIZE);
9785 break;
9786
9787 case LV_VIEW_DETAILS:
9788 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES,0, &pulScrollLines, 0);
9789
9790 /* if scrolling changes direction, ignore left overs */
9791 if ((wheelDelta < 0 && infoPtr->cWheelRemainder < 0) ||
9792 (wheelDelta > 0 && infoPtr->cWheelRemainder > 0))
9793 infoPtr->cWheelRemainder += wheelDelta;
9794 else
9795 infoPtr->cWheelRemainder = wheelDelta;
9796 if (infoPtr->cWheelRemainder && pulScrollLines)
9797 {
9798 int cLineScroll;
9799 pulScrollLines = min((UINT)LISTVIEW_GetCountPerColumn(infoPtr), pulScrollLines);
9800 cLineScroll = pulScrollLines * (float)infoPtr->cWheelRemainder / WHEEL_DELTA;
9801 infoPtr->cWheelRemainder -= WHEEL_DELTA * cLineScroll / (int)pulScrollLines;
9802 LISTVIEW_VScroll(infoPtr, SB_INTERNAL, -cLineScroll);
9803 }
9804 break;
9805
9806 case LV_VIEW_LIST:
9807 LISTVIEW_HScroll(infoPtr, (wheelDelta > 0) ? SB_LINELEFT : SB_LINERIGHT, 0);
9808 break;
9809 }
9810 return 0;
9811 }
9812
9813 /***
9814 * DESCRIPTION:
9815 * ???
9816 *
9817 * PARAMETER(S):
9818 * [I] infoPtr : valid pointer to the listview structure
9819 * [I] nVirtualKey : virtual key
9820 * [I] lKeyData : key data
9821 *
9822 * RETURN:
9823 * Zero
9824 */
9825 static LRESULT LISTVIEW_KeyDown(LISTVIEW_INFO *infoPtr, INT nVirtualKey, LONG lKeyData)
9826 {
9827 HWND hwndSelf = infoPtr->hwndSelf;
9828 INT nItem = -1;
9829 NMLVKEYDOWN nmKeyDown;
9830
9831 TRACE("(nVirtualKey=%d, lKeyData=%d)\n", nVirtualKey, lKeyData);
9832
9833 /* send LVN_KEYDOWN notification */
9834 nmKeyDown.wVKey = nVirtualKey;
9835 nmKeyDown.flags = 0;
9836 notify_hdr(infoPtr, LVN_KEYDOWN, &nmKeyDown.hdr);
9837 if (!IsWindow(hwndSelf))
9838 return 0;
9839
9840 switch (nVirtualKey)
9841 {
9842 case VK_SPACE:
9843 nItem = infoPtr->nFocusedItem;
9844 if (infoPtr->dwLvExStyle & LVS_EX_CHECKBOXES)
9845 toggle_checkbox_state(infoPtr, infoPtr->nFocusedItem);
9846 break;
9847
9848 case VK_RETURN:
9849 if ((infoPtr->nItemCount > 0) && (infoPtr->nFocusedItem != -1))
9850 {
9851 if (!notify(infoPtr, NM_RETURN)) return 0;
9852 if (!notify(infoPtr, LVN_ITEMACTIVATE)) return 0;
9853 }
9854 break;
9855
9856 case VK_HOME:
9857 if (infoPtr->nItemCount > 0)
9858 nItem = 0;
9859 break;
9860
9861 case VK_END:
9862 if (infoPtr->nItemCount > 0)
9863 nItem = infoPtr->nItemCount - 1;
9864 break;
9865
9866 case VK_LEFT:
9867 nItem = LISTVIEW_GetNextItem(infoPtr, infoPtr->nFocusedItem, LVNI_TOLEFT);
9868 break;
9869
9870 case VK_UP:
9871 nItem = LISTVIEW_GetNextItem(infoPtr, infoPtr->nFocusedItem, LVNI_ABOVE);
9872 break;
9873
9874 case VK_RIGHT:
9875 nItem = LISTVIEW_GetNextItem(infoPtr, infoPtr->nFocusedItem, LVNI_TORIGHT);
9876 break;
9877
9878 case VK_DOWN:
9879 nItem = LISTVIEW_GetNextItem(infoPtr, infoPtr->nFocusedItem, LVNI_BELOW);
9880 break;
9881
9882 case VK_PRIOR:
9883 if (infoPtr->uView == LV_VIEW_DETAILS)
9884 {
9885 INT topidx = LISTVIEW_GetTopIndex(infoPtr);
9886 if (infoPtr->nFocusedItem == topidx)
9887 nItem = topidx - LISTVIEW_GetCountPerColumn(infoPtr) + 1;
9888 else
9889 nItem = topidx;
9890 }
9891 else
9892 nItem = infoPtr->nFocusedItem - LISTVIEW_GetCountPerColumn(infoPtr)
9893 * LISTVIEW_GetCountPerRow(infoPtr);
9894 if(nItem < 0) nItem = 0;
9895 break;
9896
9897 case VK_NEXT:
9898 if (infoPtr->uView == LV_VIEW_DETAILS)
9899 {
9900 INT topidx = LISTVIEW_GetTopIndex(infoPtr);
9901 INT cnt = LISTVIEW_GetCountPerColumn(infoPtr);
9902 if (infoPtr->nFocusedItem == topidx + cnt - 1)
9903 nItem = infoPtr->nFocusedItem + cnt - 1;
9904 else
9905 nItem = topidx + cnt - 1;
9906 }
9907 else
9908 nItem = infoPtr->nFocusedItem + LISTVIEW_GetCountPerColumn(infoPtr)
9909 * LISTVIEW_GetCountPerRow(infoPtr);
9910 if(nItem >= infoPtr->nItemCount) nItem = infoPtr->nItemCount - 1;
9911 break;
9912 }
9913
9914 if ((nItem != -1) && (nItem != infoPtr->nFocusedItem || nVirtualKey == VK_SPACE))
9915 LISTVIEW_KeySelection(infoPtr, nItem, nVirtualKey == VK_SPACE);
9916
9917 return 0;
9918 }
9919
9920 /***
9921 * DESCRIPTION:
9922 * Kills the focus.
9923 *
9924 * PARAMETER(S):
9925 * [I] infoPtr : valid pointer to the listview structure
9926 *
9927 * RETURN:
9928 * Zero
9929 */
9930 static LRESULT LISTVIEW_KillFocus(LISTVIEW_INFO *infoPtr)
9931 {
9932 TRACE("()\n");
9933
9934 /* drop any left over scroll amount */
9935 infoPtr->cWheelRemainder = 0;
9936
9937 /* if we did not have the focus, there's nothing more to do */
9938 if (!infoPtr->bFocus) return 0;
9939
9940 /* send NM_KILLFOCUS notification */
9941 if (!notify(infoPtr, NM_KILLFOCUS)) return 0;
9942
9943 /* if we have a focus rectangle, get rid of it */
9944 LISTVIEW_ShowFocusRect(infoPtr, FALSE);
9945
9946 /* if have a marquee selection, stop it */
9947 if (infoPtr->bMarqueeSelect)
9948 {
9949 /* Remove the marquee rectangle and release our mouse capture */
9950 LISTVIEW_InvalidateRect(infoPtr, &infoPtr->marqueeRect);
9951 ReleaseCapture();
9952
9953 SetRect(&infoPtr->marqueeRect, 0, 0, 0, 0);
9954
9955 infoPtr->bMarqueeSelect = FALSE;
9956 infoPtr->bScrolling = FALSE;
9957 KillTimer(infoPtr->hwndSelf, (UINT_PTR) infoPtr);
9958 }
9959
9960 /* set window focus flag */
9961 infoPtr->bFocus = FALSE;
9962
9963 /* invalidate the selected items before resetting focus flag */
9964 LISTVIEW_InvalidateSelectedItems(infoPtr);
9965
9966 return 0;
9967 }
9968
9969 /***
9970 * DESCRIPTION:
9971 * Processes double click messages (left mouse button).
9972 *
9973 * PARAMETER(S):
9974 * [I] infoPtr : valid pointer to the listview structure
9975 * [I] wKey : key flag
9976 * [I] x,y : mouse coordinate
9977 *
9978 * RETURN:
9979 * Zero
9980 */
9981 static LRESULT LISTVIEW_LButtonDblClk(LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
9982 {
9983 LVHITTESTINFO htInfo;
9984
9985 TRACE("(key=%hu, X=%u, Y=%u)\n", wKey, x, y);
9986
9987 /* Cancel the item edition if any */
9988 if (infoPtr->itemEdit.fEnabled)
9989 {
9990 KillTimer(infoPtr->hwndSelf, (UINT_PTR)&infoPtr->itemEdit);
9991 infoPtr->itemEdit.fEnabled = FALSE;
9992 }
9993
9994 /* send NM_RELEASEDCAPTURE notification */
9995 if (!notify(infoPtr, NM_RELEASEDCAPTURE)) return 0;
9996
9997 htInfo.pt.x = x;
9998 htInfo.pt.y = y;
9999
10000 /* send NM_DBLCLK notification */
10001 LISTVIEW_HitTest(infoPtr, &htInfo, TRUE, FALSE);
10002 if (!notify_click(infoPtr, NM_DBLCLK, &htInfo)) return 0;
10003
10004 /* To send the LVN_ITEMACTIVATE, it must be on an Item */
10005 if(htInfo.iItem != -1) notify_itemactivate(infoPtr,&htInfo);
10006
10007 return 0;
10008 }
10009
10010 static LRESULT LISTVIEW_TrackMouse(const LISTVIEW_INFO *infoPtr, POINT pt)
10011 {
10012 MSG msg;
10013 RECT r;
10014
10015 r.top = r.bottom = pt.y;
10016 r.left = r.right = pt.x;
10017
10018 InflateRect(&r, GetSystemMetrics(SM_CXDRAG), GetSystemMetrics(SM_CYDRAG));
10019
10020 SetCapture(infoPtr->hwndSelf);
10021
10022 while (1)
10023 {
10024 if (PeekMessageW(&msg, 0, 0, 0, PM_REMOVE | PM_NOYIELD))
10025 {
10026 if (msg.message == WM_MOUSEMOVE)
10027 {
10028 pt.x = (short)LOWORD(msg.lParam);
10029 pt.y = (short)HIWORD(msg.lParam);
10030 if (PtInRect(&r, pt))
10031 continue;
10032 else
10033 {
10034 ReleaseCapture();
10035 return 1;
10036 }
10037 }
10038 else if (msg.message >= WM_LBUTTONDOWN &&
10039 msg.message <= WM_RBUTTONDBLCLK)
10040 {
10041 break;
10042 }
10043
10044 DispatchMessageW(&msg);
10045 }
10046
10047 if (GetCapture() != infoPtr->hwndSelf)
10048 return 0;
10049 }
10050
10051 ReleaseCapture();
10052 return 0;
10053 }
10054
10055
10056 /***
10057 * DESCRIPTION:
10058 * Processes mouse down messages (left mouse button).
10059 *
10060 * PARAMETERS:
10061 * infoPtr [I ] valid pointer to the listview structure
10062 * wKey [I ] key flag
10063 * x,y [I ] mouse coordinate
10064 *
10065 * RETURN:
10066 * Zero
10067 */
10068 static LRESULT LISTVIEW_LButtonDown(LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
10069 {
10070 LVHITTESTINFO lvHitTestInfo;
10071 static BOOL bGroupSelect = TRUE;
10072 POINT pt = { x, y };
10073 INT nItem;
10074
10075 TRACE("(key=%hu, X=%u, Y=%u)\n", wKey, x, y);
10076
10077 /* send NM_RELEASEDCAPTURE notification */
10078 if (!notify(infoPtr, NM_RELEASEDCAPTURE)) return 0;
10079
10080 /* set left button down flag and record the click position */
10081 infoPtr->bLButtonDown = TRUE;
10082 infoPtr->ptClickPos = pt;
10083 infoPtr->bDragging = FALSE;
10084 infoPtr->bMarqueeSelect = FALSE;
10085 infoPtr->bScrolling = FALSE;
10086
10087 lvHitTestInfo.pt.x = x;
10088 lvHitTestInfo.pt.y = y;
10089
10090 nItem = LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, TRUE);
10091 TRACE("at %s, nItem=%d\n", wine_dbgstr_point(&pt), nItem);
10092 if ((nItem >= 0) && (nItem < infoPtr->nItemCount))
10093 {
10094 if ((infoPtr->dwLvExStyle & LVS_EX_CHECKBOXES) && (lvHitTestInfo.flags & LVHT_ONITEMSTATEICON))
10095 {
10096 toggle_checkbox_state(infoPtr, nItem);
10097 return 0;
10098 }
10099
10100 if (infoPtr->dwStyle & LVS_SINGLESEL)
10101 {
10102 if (LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED))
10103 infoPtr->nEditLabelItem = nItem;
10104 else
10105 LISTVIEW_SetSelection(infoPtr, nItem);
10106 }
10107 else
10108 {
10109 if ((wKey & MK_CONTROL) && (wKey & MK_SHIFT))
10110 {
10111 if (bGroupSelect)
10112 {
10113 if (!LISTVIEW_AddGroupSelection(infoPtr, nItem)) return 0;
10114 LISTVIEW_SetItemFocus(infoPtr, nItem);
10115 infoPtr->nSelectionMark = nItem;
10116 }
10117 else
10118 {
10119 LVITEMW item;
10120
10121 item.state = LVIS_SELECTED | LVIS_FOCUSED;
10122 item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
10123
10124 LISTVIEW_SetItemState(infoPtr,nItem,&item);
10125 infoPtr->nSelectionMark = nItem;
10126 }
10127 }
10128 else if (wKey & MK_CONTROL)
10129 {
10130 LVITEMW item;
10131
10132 bGroupSelect = (LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED) == 0);
10133
10134 item.state = (bGroupSelect ? LVIS_SELECTED : 0) | LVIS_FOCUSED;
10135 item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
10136 LISTVIEW_SetItemState(infoPtr, nItem, &item);
10137 infoPtr->nSelectionMark = nItem;
10138 }
10139 else if (wKey & MK_SHIFT)
10140 {
10141 LISTVIEW_SetGroupSelection(infoPtr, nItem);
10142 }
10143 else
10144 {
10145 if (LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED))
10146 {
10147 infoPtr->nEditLabelItem = nItem;
10148 infoPtr->nLButtonDownItem = nItem;
10149
10150 LISTVIEW_SetItemFocus(infoPtr, nItem);
10151 }
10152 else
10153 /* set selection (clears other pre-existing selections) */
10154 LISTVIEW_SetSelection(infoPtr, nItem);
10155 }
10156 }
10157
10158 if (infoPtr->dwLvExStyle & LVS_EX_ONECLICKACTIVATE)
10159 if(lvHitTestInfo.iItem != -1) notify_itemactivate(infoPtr,&lvHitTestInfo);
10160 }
10161 else
10162 {
10163 if (!infoPtr->bFocus)
10164 SetFocus(infoPtr->hwndSelf);
10165
10166 /* remove all selections */
10167 if (!(wKey & MK_CONTROL) && !(wKey & MK_SHIFT))
10168 LISTVIEW_DeselectAll(infoPtr);
10169 ReleaseCapture();
10170 }
10171
10172 return 0;
10173 }
10174
10175 /***
10176 * DESCRIPTION:
10177 * Processes mouse up messages (left mouse button).
10178 *
10179 * PARAMETERS:
10180 * infoPtr [I ] valid pointer to the listview structure
10181 * wKey [I ] key flag
10182 * x,y [I ] mouse coordinate
10183 *
10184 * RETURN:
10185 * Zero
10186 */
10187 static LRESULT LISTVIEW_LButtonUp(LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
10188 {
10189 LVHITTESTINFO lvHitTestInfo;
10190
10191 TRACE("(key=%hu, X=%u, Y=%u)\n", wKey, x, y);
10192
10193 if (!infoPtr->bLButtonDown) return 0;
10194
10195 lvHitTestInfo.pt.x = x;
10196 lvHitTestInfo.pt.y = y;
10197
10198 /* send NM_CLICK notification */
10199 LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, FALSE);
10200 if (!notify_click(infoPtr, NM_CLICK, &lvHitTestInfo)) return 0;
10201
10202 /* set left button flag */
10203 infoPtr->bLButtonDown = FALSE;
10204
10205 /* set a single selection, reset others */
10206 if(lvHitTestInfo.iItem == infoPtr->nLButtonDownItem && lvHitTestInfo.iItem != -1)
10207 LISTVIEW_SetSelection(infoPtr, infoPtr->nLButtonDownItem);
10208 infoPtr->nLButtonDownItem = -1;
10209
10210 if (infoPtr->bDragging || infoPtr->bMarqueeSelect)
10211 {
10212 /* Remove the marquee rectangle and release our mouse capture */
10213 if (infoPtr->bMarqueeSelect)
10214 {
10215 LISTVIEW_InvalidateRect(infoPtr, &infoPtr->marqueeDrawRect);
10216 ReleaseCapture();
10217 }
10218
10219 SetRect(&infoPtr->marqueeRect, 0, 0, 0, 0);
10220 SetRect(&infoPtr->marqueeDrawRect, 0, 0, 0, 0);
10221
10222 infoPtr->bDragging = FALSE;
10223 infoPtr->bMarqueeSelect = FALSE;
10224 infoPtr->bScrolling = FALSE;
10225
10226 KillTimer(infoPtr->hwndSelf, (UINT_PTR) infoPtr);
10227 return 0;
10228 }
10229
10230 /* if we clicked on a selected item, edit the label */
10231 if(lvHitTestInfo.iItem == infoPtr->nEditLabelItem && (lvHitTestInfo.flags & LVHT_ONITEMLABEL))
10232 {
10233 /* we want to make sure the user doesn't want to do a double click. So we will
10234 * delay the edit. WM_LBUTTONDBLCLICK will cancel the timer
10235 */
10236 infoPtr->itemEdit.fEnabled = TRUE;
10237 infoPtr->itemEdit.iItem = lvHitTestInfo.iItem;
10238 SetTimer(infoPtr->hwndSelf,
10239 (UINT_PTR)&infoPtr->itemEdit,
10240 GetDoubleClickTime(),
10241 LISTVIEW_DelayedEditItem);
10242 }
10243
10244 if (!infoPtr->bFocus)
10245 SetFocus(infoPtr->hwndSelf);
10246
10247 return 0;
10248 }
10249
10250 /***
10251 * DESCRIPTION:
10252 * Destroys the listview control (called after WM_DESTROY).
10253 *
10254 * PARAMETER(S):
10255 * [I] infoPtr : valid pointer to the listview structure
10256 *
10257 * RETURN:
10258 * Zero
10259 */
10260 static LRESULT LISTVIEW_NCDestroy(LISTVIEW_INFO *infoPtr)
10261 {
10262 INT i;
10263
10264 TRACE("()\n");
10265
10266 /* destroy data structure */
10267 DPA_Destroy(infoPtr->hdpaItems);
10268 DPA_Destroy(infoPtr->hdpaItemIds);
10269 DPA_Destroy(infoPtr->hdpaPosX);
10270 DPA_Destroy(infoPtr->hdpaPosY);
10271 /* columns */
10272 for (i = 0; i < DPA_GetPtrCount(infoPtr->hdpaColumns); i++)
10273 Free(DPA_GetPtr(infoPtr->hdpaColumns, i));
10274 DPA_Destroy(infoPtr->hdpaColumns);
10275 ranges_destroy(infoPtr->selectionRanges);
10276
10277 /* destroy image lists */
10278 if (!(infoPtr->dwStyle & LVS_SHAREIMAGELISTS))
10279 {
10280 ImageList_Destroy(infoPtr->himlNormal);
10281 ImageList_Destroy(infoPtr->himlSmall);
10282 ImageList_Destroy(infoPtr->himlState);
10283 }
10284
10285 /* destroy font, bkgnd brush */
10286 infoPtr->hFont = 0;
10287 if (infoPtr->hDefaultFont) DeleteObject(infoPtr->hDefaultFont);
10288 if (infoPtr->clrBk != CLR_NONE) DeleteObject(infoPtr->hBkBrush);
10289
10290 SetWindowLongPtrW(infoPtr->hwndSelf, 0, 0);
10291
10292 /* free listview info pointer*/
10293 Free(infoPtr);
10294
10295 return 0;
10296 }
10297
10298 /***
10299 * DESCRIPTION:
10300 * Handles notifications.
10301 *
10302 * PARAMETER(S):
10303 * [I] infoPtr : valid pointer to the listview structure
10304 * [I] lpnmhdr : notification information
10305 *
10306 * RETURN:
10307 * Zero
10308 */
10309 static LRESULT LISTVIEW_Notify(LISTVIEW_INFO *infoPtr, NMHDR *lpnmhdr)
10310 {
10311 NMHEADERW *lpnmh;
10312
10313 TRACE("(lpnmhdr=%p)\n", lpnmhdr);
10314
10315 if (!lpnmhdr || lpnmhdr->hwndFrom != infoPtr->hwndHeader) return 0;
10316
10317 /* remember: HDN_LAST < HDN_FIRST */
10318 if (lpnmhdr->code > HDN_FIRST || lpnmhdr->code < HDN_LAST) return 0;
10319 lpnmh = (NMHEADERW *)lpnmhdr;
10320
10321 if (lpnmh->iItem < 0 || lpnmh->iItem >= DPA_GetPtrCount(infoPtr->hdpaColumns)) return 0;
10322
10323 switch (lpnmhdr->code)
10324 {
10325 case HDN_TRACKW:
10326 case HDN_TRACKA:
10327 {
10328 COLUMN_INFO *lpColumnInfo;
10329 POINT ptOrigin;
10330 INT x;
10331
10332 if (!lpnmh->pitem || !(lpnmh->pitem->mask & HDI_WIDTH))
10333 break;
10334
10335 /* remove the old line (if any) */
10336 LISTVIEW_DrawTrackLine(infoPtr);
10337
10338 /* compute & draw the new line */
10339 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, lpnmh->iItem);
10340 x = lpColumnInfo->rcHeader.left + lpnmh->pitem->cxy;
10341 LISTVIEW_GetOrigin(infoPtr, &ptOrigin);
10342 infoPtr->xTrackLine = x + ptOrigin.x;
10343 LISTVIEW_DrawTrackLine(infoPtr);
10344 return notify_forward_header(infoPtr, lpnmh);
10345 }
10346
10347 case HDN_ENDTRACKA:
10348 case HDN_ENDTRACKW:
10349 /* remove the track line (if any) */
10350 LISTVIEW_DrawTrackLine(infoPtr);
10351 infoPtr->xTrackLine = -1;
10352 return notify_forward_header(infoPtr, lpnmh);
10353
10354 case HDN_BEGINDRAG:
10355 if ((infoPtr->dwLvExStyle & LVS_EX_HEADERDRAGDROP) == 0) return 1;
10356 return notify_forward_header(infoPtr, lpnmh);
10357
10358 case HDN_ENDDRAG:
10359 infoPtr->colRectsDirty = TRUE;
10360 LISTVIEW_InvalidateList(infoPtr);
10361 return notify_forward_header(infoPtr, lpnmh);
10362
10363 case HDN_ITEMCHANGEDW:
10364 case HDN_ITEMCHANGEDA:
10365 {
10366 COLUMN_INFO *lpColumnInfo;
10367 HDITEMW hdi;
10368 INT dx, cxy;
10369
10370 if (!lpnmh->pitem || !(lpnmh->pitem->mask & HDI_WIDTH))
10371 {
10372 hdi.mask = HDI_WIDTH;
10373 if (!SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, lpnmh->iItem, (LPARAM)&hdi)) return 0;
10374 cxy = hdi.cxy;
10375 }
10376 else
10377 cxy = lpnmh->pitem->cxy;
10378
10379 /* determine how much we change since the last know position */
10380 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, lpnmh->iItem);
10381 dx = cxy - (lpColumnInfo->rcHeader.right - lpColumnInfo->rcHeader.left);
10382 if (dx != 0)
10383 {
10384 lpColumnInfo->rcHeader.right += dx;
10385
10386 hdi.mask = HDI_ORDER;
10387 SendMessageW(infoPtr->hwndHeader, HDM_GETITEMW, lpnmh->iItem, (LPARAM)&hdi);
10388
10389 /* not the rightmost one */
10390 if (hdi.iOrder + 1 < DPA_GetPtrCount(infoPtr->hdpaColumns))
10391 {
10392 INT nIndex = SendMessageW(infoPtr->hwndHeader, HDM_ORDERTOINDEX,
10393 hdi.iOrder + 1, 0);
10394 LISTVIEW_ScrollColumns(infoPtr, nIndex, dx);
10395 }
10396 else
10397 {
10398 /* only needs to update the scrolls */
10399 infoPtr->nItemWidth += dx;
10400 LISTVIEW_UpdateScroll(infoPtr);
10401 }
10402 LISTVIEW_UpdateItemSize(infoPtr);
10403 if (infoPtr->uView == LV_VIEW_DETAILS && is_redrawing(infoPtr))
10404 {
10405 POINT ptOrigin;
10406 RECT rcCol = lpColumnInfo->rcHeader;
10407
10408 LISTVIEW_GetOrigin(infoPtr, &ptOrigin);
10409 OffsetRect(&rcCol, ptOrigin.x, 0);
10410
10411 rcCol.top = infoPtr->rcList.top;
10412 rcCol.bottom = infoPtr->rcList.bottom;
10413
10414 /* resizing left-aligned columns leaves most of the left side untouched */
10415 if ((lpColumnInfo->fmt & LVCFMT_JUSTIFYMASK) == LVCFMT_LEFT)
10416 {
10417 INT nMaxDirty = infoPtr->nEllipsisWidth + infoPtr->ntmMaxCharWidth;
10418 if (dx > 0)
10419 nMaxDirty += dx;
10420 rcCol.left = max (rcCol.left, rcCol.right - nMaxDirty);
10421 }
10422
10423 /* when shrinking the last column clear the now unused field */
10424 if (hdi.iOrder == DPA_GetPtrCount(infoPtr->hdpaColumns) - 1)
10425 {
10426 RECT right;
10427
10428 rcCol.right -= dx;
10429
10430 /* deal with right from rightmost column area */
10431 right.left = rcCol.right;
10432 right.top = rcCol.top;
10433 right.bottom = rcCol.bottom;
10434 right.right = infoPtr->rcList.right;
10435
10436 LISTVIEW_InvalidateRect(infoPtr, &right);
10437 }
10438
10439 LISTVIEW_InvalidateRect(infoPtr, &rcCol);
10440 }
10441 }
10442 break;
10443 }
10444
10445 case HDN_ITEMCLICKW:
10446 case HDN_ITEMCLICKA:
10447 {
10448 /* Handle sorting by Header Column */
10449 NMLISTVIEW nmlv;
10450
10451 ZeroMemory(&nmlv, sizeof(NMLISTVIEW));
10452 nmlv.iItem = -1;
10453 nmlv.iSubItem = lpnmh->iItem;
10454 notify_listview(infoPtr, LVN_COLUMNCLICK, &nmlv);
10455 return notify_forward_header(infoPtr, lpnmh);
10456 }
10457
10458 case HDN_DIVIDERDBLCLICKW:
10459 case HDN_DIVIDERDBLCLICKA:
10460 /* FIXME: for LVS_EX_HEADERINALLVIEWS and not LV_VIEW_DETAILS
10461 we should use LVSCW_AUTOSIZE_USEHEADER, helper rework or
10462 split needed for that */
10463 LISTVIEW_SetColumnWidth(infoPtr, lpnmh->iItem, LVSCW_AUTOSIZE);
10464 return notify_forward_header(infoPtr, lpnmh);
10465 }
10466 return 0;
10467 }
10468
10469 /***
10470 * DESCRIPTION:
10471 * Paint non-client area of control.
10472 *
10473 * PARAMETER(S):
10474 * [I] infoPtr : valid pointer to the listview structureof the sender
10475 * [I] region : update region
10476 *
10477 * RETURN:
10478 * TRUE - frame was painted
10479 * FALSE - call default window proc
10480 */
10481 static BOOL LISTVIEW_NCPaint(const LISTVIEW_INFO *infoPtr, HRGN region)
10482 {
10483 HTHEME theme = GetWindowTheme (infoPtr->hwndSelf);
10484 HDC dc;
10485 RECT r;
10486 HRGN cliprgn;
10487 int cxEdge = GetSystemMetrics (SM_CXEDGE),
10488 cyEdge = GetSystemMetrics (SM_CYEDGE);
10489
10490 if (!theme)
10491 return DefWindowProcW (infoPtr->hwndSelf, WM_NCPAINT, (WPARAM)region, 0);
10492
10493 GetWindowRect(infoPtr->hwndSelf, &r);
10494
10495 cliprgn = CreateRectRgn (r.left + cxEdge, r.top + cyEdge,
10496 r.right - cxEdge, r.bottom - cyEdge);
10497 if (region != (HRGN)1)
10498 CombineRgn (cliprgn, cliprgn, region, RGN_AND);
10499 OffsetRect(&r, -r.left, -r.top);
10500
10501 dc = GetDCEx(infoPtr->hwndSelf, region, DCX_WINDOW|DCX_INTERSECTRGN);
10502 OffsetRect(&r, -r.left, -r.top);
10503
10504 if (IsThemeBackgroundPartiallyTransparent (theme, 0, 0))
10505 DrawThemeParentBackground(infoPtr->hwndSelf, dc, &r);
10506 DrawThemeBackground (theme, dc, 0, 0, &r, 0);
10507 ReleaseDC(infoPtr->hwndSelf, dc);
10508
10509 /* Call default proc to get the scrollbars etc. painted */
10510 DefWindowProcW (infoPtr->hwndSelf, WM_NCPAINT, (WPARAM)cliprgn, 0);
10511
10512 return FALSE;
10513 }
10514
10515 /***
10516 * DESCRIPTION:
10517 * Determines the type of structure to use.
10518 *
10519 * PARAMETER(S):
10520 * [I] infoPtr : valid pointer to the listview structureof the sender
10521 * [I] hwndFrom : listview window handle
10522 * [I] nCommand : command specifying the nature of the WM_NOTIFYFORMAT
10523 *
10524 * RETURN:
10525 * Zero
10526 */
10527 static LRESULT LISTVIEW_NotifyFormat(LISTVIEW_INFO *infoPtr, HWND hwndFrom, INT nCommand)
10528 {
10529 TRACE("(hwndFrom=%p, nCommand=%d)\n", hwndFrom, nCommand);
10530
10531 if (nCommand == NF_REQUERY)
10532 infoPtr->notifyFormat = SendMessageW(infoPtr->hwndNotify, WM_NOTIFYFORMAT, (WPARAM)infoPtr->hwndSelf, NF_QUERY);
10533
10534 return infoPtr->notifyFormat;
10535 }
10536
10537 /***
10538 * DESCRIPTION:
10539 * Paints/Repaints the listview control. Internal use.
10540 *
10541 * PARAMETER(S):
10542 * [I] infoPtr : valid pointer to the listview structure
10543 * [I] hdc : device context handle
10544 *
10545 * RETURN:
10546 * Zero
10547 */
10548 static LRESULT LISTVIEW_Paint(LISTVIEW_INFO *infoPtr, HDC hdc)
10549 {
10550 TRACE("(hdc=%p)\n", hdc);
10551
10552 if (infoPtr->bNoItemMetrics && infoPtr->nItemCount)
10553 {
10554 infoPtr->bNoItemMetrics = FALSE;
10555 LISTVIEW_UpdateItemSize(infoPtr);
10556 if (infoPtr->uView == LV_VIEW_ICON || infoPtr->uView == LV_VIEW_SMALLICON)
10557 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
10558 LISTVIEW_UpdateScroll(infoPtr);
10559 }
10560
10561 if (infoPtr->hwndHeader) UpdateWindow(infoPtr->hwndHeader);
10562
10563 if (hdc)
10564 LISTVIEW_Refresh(infoPtr, hdc, NULL);
10565 else
10566 {
10567 PAINTSTRUCT ps;
10568
10569 hdc = BeginPaint(infoPtr->hwndSelf, &ps);
10570 if (!hdc) return 1;
10571 LISTVIEW_Refresh(infoPtr, hdc, ps.fErase ? &ps.rcPaint : NULL);
10572 EndPaint(infoPtr->hwndSelf, &ps);
10573 }
10574
10575 return 0;
10576 }
10577
10578 /***
10579 * DESCRIPTION:
10580 * Paints/Repaints the listview control, WM_PAINT handler.
10581 *
10582 * PARAMETER(S):
10583 * [I] infoPtr : valid pointer to the listview structure
10584 * [I] hdc : device context handle
10585 *
10586 * RETURN:
10587 * Zero
10588 */
10589 static inline LRESULT LISTVIEW_WMPaint(LISTVIEW_INFO *infoPtr, HDC hdc)
10590 {
10591 TRACE("(hdc=%p)\n", hdc);
10592
10593 if (!is_redrawing(infoPtr))
10594 return DefWindowProcW (infoPtr->hwndSelf, WM_PAINT, (WPARAM)hdc, 0);
10595
10596 return LISTVIEW_Paint(infoPtr, hdc);
10597 }
10598
10599 /***
10600 * DESCRIPTION:
10601 * Paints/Repaints the listview control.
10602 *
10603 * PARAMETER(S):
10604 * [I] infoPtr : valid pointer to the listview structure
10605 * [I] hdc : device context handle
10606 * [I] options : drawing options
10607 *
10608 * RETURN:
10609 * Zero
10610 */
10611 static LRESULT LISTVIEW_PrintClient(LISTVIEW_INFO *infoPtr, HDC hdc, DWORD options)
10612 {
10613 FIXME("Partial Stub: (hdc=%p options=0x%08x)\n", hdc, options);
10614
10615 if ((options & PRF_CHECKVISIBLE) && !IsWindowVisible(infoPtr->hwndSelf))
10616 return 0;
10617
10618 if (options & PRF_ERASEBKGND)
10619 LISTVIEW_EraseBkgnd(infoPtr, hdc);
10620
10621 if (options & PRF_CLIENT)
10622 LISTVIEW_Paint(infoPtr, hdc);
10623
10624 return 0;
10625 }
10626
10627
10628 /***
10629 * DESCRIPTION:
10630 * Processes double click messages (right mouse button).
10631 *
10632 * PARAMETER(S):
10633 * [I] infoPtr : valid pointer to the listview structure
10634 * [I] wKey : key flag
10635 * [I] x,y : mouse coordinate
10636 *
10637 * RETURN:
10638 * Zero
10639 */
10640 static LRESULT LISTVIEW_RButtonDblClk(const LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
10641 {
10642 LVHITTESTINFO lvHitTestInfo;
10643
10644 TRACE("(key=%hu,X=%u,Y=%u)\n", wKey, x, y);
10645
10646 /* send NM_RELEASEDCAPTURE notification */
10647 if (!notify(infoPtr, NM_RELEASEDCAPTURE)) return 0;
10648
10649 /* send NM_RDBLCLK notification */
10650 lvHitTestInfo.pt.x = x;
10651 lvHitTestInfo.pt.y = y;
10652 LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, FALSE);
10653 notify_click(infoPtr, NM_RDBLCLK, &lvHitTestInfo);
10654
10655 return 0;
10656 }
10657
10658 /***
10659 * DESCRIPTION:
10660 * Processes WM_RBUTTONDOWN message and corresponding drag operation.
10661 *
10662 * PARAMETER(S):
10663 * [I] infoPtr : valid pointer to the listview structure
10664 * [I] wKey : key flag
10665 * [I] x, y : mouse coordinate
10666 *
10667 * RETURN:
10668 * Zero
10669 */
10670 static LRESULT LISTVIEW_RButtonDown(LISTVIEW_INFO *infoPtr, WORD wKey, INT x, INT y)
10671 {
10672 LVHITTESTINFO ht;
10673 INT item;
10674
10675 TRACE("(key=%hu, x=%d, y=%d)\n", wKey, x, y);
10676
10677 /* send NM_RELEASEDCAPTURE notification */
10678 if (!notify(infoPtr, NM_RELEASEDCAPTURE)) return 0;
10679
10680 /* determine the index of the selected item */
10681 ht.pt.x = x;
10682 ht.pt.y = y;
10683 item = LISTVIEW_HitTest(infoPtr, &ht, TRUE, TRUE);
10684
10685 /* make sure the listview control window has the focus */
10686 if (!infoPtr->bFocus) SetFocus(infoPtr->hwndSelf);
10687
10688 if ((item >= 0) && (item < infoPtr->nItemCount))
10689 {
10690 LISTVIEW_SetItemFocus(infoPtr, item);
10691 if (!((wKey & MK_SHIFT) || (wKey & MK_CONTROL)) &&
10692 !LISTVIEW_GetItemState(infoPtr, item, LVIS_SELECTED))
10693 LISTVIEW_SetSelection(infoPtr, item);
10694 }
10695 else
10696 LISTVIEW_DeselectAll(infoPtr);
10697
10698 if (LISTVIEW_TrackMouse(infoPtr, ht.pt))
10699 {
10700 if (ht.iItem != -1)
10701 {
10702 NMLISTVIEW nmlv;
10703
10704 memset(&nmlv, 0, sizeof(nmlv));
10705 nmlv.iItem = ht.iItem;
10706 nmlv.ptAction = ht.pt;
10707
10708 notify_listview(infoPtr, LVN_BEGINRDRAG, &nmlv);
10709 }
10710 }
10711 else
10712 {
10713 SetFocus(infoPtr->hwndSelf);
10714
10715 ht.pt.x = x;
10716 ht.pt.y = y;
10717 LISTVIEW_HitTest(infoPtr, &ht, TRUE, FALSE);
10718
10719 if (notify_click(infoPtr, NM_RCLICK, &ht))
10720 {
10721 /* Send a WM_CONTEXTMENU message in response to the WM_RBUTTONUP */
10722 SendMessageW(infoPtr->hwndSelf, WM_CONTEXTMENU,
10723 (WPARAM)infoPtr->hwndSelf, (LPARAM)GetMessagePos());
10724 }
10725 }
10726
10727 return 0;
10728 }
10729
10730 /***
10731 * DESCRIPTION:
10732 * Sets the cursor.
10733 *
10734 * PARAMETER(S):
10735 * [I] infoPtr : valid pointer to the listview structure
10736 * [I] hwnd : window handle of window containing the cursor
10737 * [I] nHittest : hit-test code
10738 * [I] wMouseMsg : ideintifier of the mouse message
10739 *
10740 * RETURN:
10741 * TRUE if cursor is set
10742 * FALSE otherwise
10743 */
10744 static BOOL LISTVIEW_SetCursor(const LISTVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
10745 {
10746 LVHITTESTINFO lvHitTestInfo;
10747
10748 if (!LISTVIEW_IsHotTracking(infoPtr)) goto forward;
10749
10750 if (!infoPtr->hHotCursor) goto forward;
10751
10752 GetCursorPos(&lvHitTestInfo.pt);
10753 if (LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, FALSE, FALSE) < 0) goto forward;
10754
10755 SetCursor(infoPtr->hHotCursor);
10756
10757 return TRUE;
10758
10759 forward:
10760
10761 return DefWindowProcW(infoPtr->hwndSelf, WM_SETCURSOR, wParam, lParam);
10762 }
10763
10764 /***
10765 * DESCRIPTION:
10766 * Sets the focus.
10767 *
10768 * PARAMETER(S):
10769 * [I] infoPtr : valid pointer to the listview structure
10770 * [I] hwndLoseFocus : handle of previously focused window
10771 *
10772 * RETURN:
10773 * Zero
10774 */
10775 static LRESULT LISTVIEW_SetFocus(LISTVIEW_INFO *infoPtr, HWND hwndLoseFocus)
10776 {
10777 TRACE("(hwndLoseFocus=%p)\n", hwndLoseFocus);
10778
10779 /* if we have the focus already, there's nothing to do */
10780 if (infoPtr->bFocus) return 0;
10781
10782 /* send NM_SETFOCUS notification */
10783 if (!notify(infoPtr, NM_SETFOCUS)) return 0;
10784
10785 /* set window focus flag */
10786 infoPtr->bFocus = TRUE;
10787
10788 /* put the focus rect back on */
10789 LISTVIEW_ShowFocusRect(infoPtr, TRUE);
10790
10791 /* redraw all visible selected items */
10792 LISTVIEW_InvalidateSelectedItems(infoPtr);
10793
10794 return 0;
10795 }
10796
10797 /***
10798 * DESCRIPTION:
10799 * Sets the font.
10800 *
10801 * PARAMETER(S):
10802 * [I] infoPtr : valid pointer to the listview structure
10803 * [I] fRedraw : font handle
10804 * [I] fRedraw : redraw flag
10805 *
10806 * RETURN:
10807 * Zero
10808 */
10809 static LRESULT LISTVIEW_SetFont(LISTVIEW_INFO *infoPtr, HFONT hFont, WORD fRedraw)
10810 {
10811 HFONT oldFont = infoPtr->hFont;
10812 INT oldHeight = infoPtr->nItemHeight;
10813
10814 TRACE("(hfont=%p,redraw=%hu)\n", hFont, fRedraw);
10815
10816 infoPtr->hFont = hFont ? hFont : infoPtr->hDefaultFont;
10817 if (infoPtr->hFont == oldFont) return 0;
10818
10819 LISTVIEW_SaveTextMetrics(infoPtr);
10820
10821 infoPtr->nItemHeight = LISTVIEW_CalculateItemHeight(infoPtr);
10822
10823 if (infoPtr->uView == LV_VIEW_DETAILS)
10824 {
10825 SendMessageW(infoPtr->hwndHeader, WM_SETFONT, (WPARAM)hFont, MAKELPARAM(fRedraw, 0));
10826 LISTVIEW_UpdateSize(infoPtr);
10827 LISTVIEW_UpdateScroll(infoPtr);
10828 }
10829 else if (infoPtr->nItemHeight != oldHeight)
10830 LISTVIEW_UpdateScroll(infoPtr);
10831
10832 if (fRedraw) LISTVIEW_InvalidateList(infoPtr);
10833
10834 return 0;
10835 }
10836
10837 /***
10838 * DESCRIPTION:
10839 * Message handling for WM_SETREDRAW.
10840 * For the Listview, it invalidates the entire window (the doc specifies otherwise)
10841 *
10842 * PARAMETER(S):
10843 * [I] infoPtr : valid pointer to the listview structure
10844 * [I] bRedraw: state of redraw flag
10845 *
10846 * RETURN:
10847 * DefWinProc return value
10848 */
10849 static LRESULT LISTVIEW_SetRedraw(LISTVIEW_INFO *infoPtr, BOOL bRedraw)
10850 {
10851 TRACE("infoPtr->bRedraw=%d, bRedraw=%d\n", infoPtr->bRedraw, bRedraw);
10852
10853 /* we cannot use straight equality here because _any_ non-zero value is TRUE */
10854 if ((infoPtr->bRedraw && bRedraw) || (!infoPtr->bRedraw && !bRedraw)) return 0;
10855
10856 infoPtr->bRedraw = bRedraw;
10857
10858 if(!bRedraw) return 0;
10859
10860 if (is_autoarrange(infoPtr))
10861 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
10862 LISTVIEW_UpdateScroll(infoPtr);
10863
10864 /* despite what the WM_SETREDRAW docs says, apps expect us
10865 * to invalidate the listview here... stupid! */
10866 LISTVIEW_InvalidateList(infoPtr);
10867
10868 return 0;
10869 }
10870
10871 /***
10872 * DESCRIPTION:
10873 * Resizes the listview control. This function processes WM_SIZE
10874 * messages. At this time, the width and height are not used.
10875 *
10876 * PARAMETER(S):
10877 * [I] infoPtr : valid pointer to the listview structure
10878 * [I] Width : new width
10879 * [I] Height : new height
10880 *
10881 * RETURN:
10882 * Zero
10883 */
10884 static LRESULT LISTVIEW_Size(LISTVIEW_INFO *infoPtr, int Width, int Height)
10885 {
10886 RECT rcOld = infoPtr->rcList;
10887
10888 TRACE("(width=%d, height=%d)\n", Width, Height);
10889
10890 LISTVIEW_UpdateSize(infoPtr);
10891 if (EqualRect(&rcOld, &infoPtr->rcList)) return 0;
10892
10893 /* do not bother with display related stuff if we're not redrawing */
10894 if (!is_redrawing(infoPtr)) return 0;
10895
10896 if (is_autoarrange(infoPtr))
10897 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
10898
10899 LISTVIEW_UpdateScroll(infoPtr);
10900
10901 /* refresh all only for lists whose height changed significantly */
10902 if ((infoPtr->uView == LV_VIEW_LIST) &&
10903 (rcOld.bottom - rcOld.top) / infoPtr->nItemHeight !=
10904 (infoPtr->rcList.bottom - infoPtr->rcList.top) / infoPtr->nItemHeight)
10905 LISTVIEW_InvalidateList(infoPtr);
10906
10907 return 0;
10908 }
10909
10910 /***
10911 * DESCRIPTION:
10912 * Sets the size information.
10913 *
10914 * PARAMETER(S):
10915 * [I] infoPtr : valid pointer to the listview structure
10916 *
10917 * RETURN:
10918 * None
10919 */
10920 static void LISTVIEW_UpdateSize(LISTVIEW_INFO *infoPtr)
10921 {
10922 TRACE("uView=%d, rcList(old)=%s\n", infoPtr->uView, wine_dbgstr_rect(&infoPtr->rcList));
10923
10924 GetClientRect(infoPtr->hwndSelf, &infoPtr->rcList);
10925
10926 if (infoPtr->uView == LV_VIEW_LIST)
10927 {
10928 /* Apparently the "LIST" style is supposed to have the same
10929 * number of items in a column even if there is no scroll bar.
10930 * Since if a scroll bar already exists then the bottom is already
10931 * reduced, only reduce if the scroll bar does not currently exist.
10932 * The "2" is there to mimic the native control. I think it may be
10933 * related to either padding or edges. (GLA 7/2002)
10934 */
10935 if (!(GetWindowLongW(infoPtr->hwndSelf, GWL_STYLE) & WS_HSCROLL))
10936 infoPtr->rcList.bottom -= GetSystemMetrics(SM_CYHSCROLL);
10937 infoPtr->rcList.bottom = max (infoPtr->rcList.bottom - 2, 0);
10938 }
10939
10940 /* if control created invisible header isn't created */
10941 if (infoPtr->hwndHeader)
10942 {
10943 HDLAYOUT hl;
10944 WINDOWPOS wp;
10945
10946 hl.prc = &infoPtr->rcList;
10947 hl.pwpos = &wp;
10948 SendMessageW( infoPtr->hwndHeader, HDM_LAYOUT, 0, (LPARAM)&hl );
10949 TRACE(" wp.flags=0x%08x, wp=%d,%d (%dx%d)\n", wp.flags, wp.x, wp.y, wp.cx, wp.cy);
10950
10951 if (LISTVIEW_IsHeaderEnabled(infoPtr))
10952 wp.flags |= SWP_SHOWWINDOW;
10953 else
10954 {
10955 wp.flags |= SWP_HIDEWINDOW;
10956 wp.cy = 0;
10957 }
10958
10959 SetWindowPos(wp.hwnd, wp.hwndInsertAfter, wp.x, wp.y, wp.cx, wp.cy, wp.flags);
10960 TRACE(" after SWP wp=%d,%d (%dx%d)\n", wp.x, wp.y, wp.cx, wp.cy);
10961
10962 infoPtr->rcList.top = max(wp.cy, 0);
10963 }
10964 /* extra padding for grid */
10965 if (infoPtr->uView == LV_VIEW_DETAILS && infoPtr->dwLvExStyle & LVS_EX_GRIDLINES)
10966 infoPtr->rcList.top += 2;
10967
10968 TRACE(" rcList=%s\n", wine_dbgstr_rect(&infoPtr->rcList));
10969 }
10970
10971 /***
10972 * DESCRIPTION:
10973 * Processes WM_STYLECHANGED messages.
10974 *
10975 * PARAMETER(S):
10976 * [I] infoPtr : valid pointer to the listview structure
10977 * [I] wStyleType : window style type (normal or extended)
10978 * [I] lpss : window style information
10979 *
10980 * RETURN:
10981 * Zero
10982 */
10983 static INT LISTVIEW_StyleChanged(LISTVIEW_INFO *infoPtr, WPARAM wStyleType,
10984 const STYLESTRUCT *lpss)
10985 {
10986 UINT uNewView = lpss->styleNew & LVS_TYPEMASK;
10987 UINT uOldView = lpss->styleOld & LVS_TYPEMASK;
10988 UINT style;
10989
10990 TRACE("(styletype=%lx, styleOld=0x%08x, styleNew=0x%08x)\n",
10991 wStyleType, lpss->styleOld, lpss->styleNew);
10992
10993 if (wStyleType != GWL_STYLE) return 0;
10994
10995 infoPtr->dwStyle = lpss->styleNew;
10996 map_style_view(infoPtr);
10997
10998 if (((lpss->styleOld & WS_HSCROLL) != 0)&&
10999 ((lpss->styleNew & WS_HSCROLL) == 0))
11000 ShowScrollBar(infoPtr->hwndSelf, SB_HORZ, FALSE);
11001
11002 if (((lpss->styleOld & WS_VSCROLL) != 0)&&
11003 ((lpss->styleNew & WS_VSCROLL) == 0))
11004 ShowScrollBar(infoPtr->hwndSelf, SB_VERT, FALSE);
11005
11006 if (uNewView != uOldView)
11007 {
11008 HIMAGELIST himl;
11009
11010 SendMessageW(infoPtr->hwndEdit, WM_KILLFOCUS, 0, 0);
11011 ShowWindow(infoPtr->hwndHeader, SW_HIDE);
11012
11013 ShowScrollBar(infoPtr->hwndSelf, SB_BOTH, FALSE);
11014 SetRectEmpty(&infoPtr->rcFocus);
11015
11016 himl = (uNewView == LVS_ICON ? infoPtr->himlNormal : infoPtr->himlSmall);
11017 set_icon_size(&infoPtr->iconSize, himl, uNewView != LVS_ICON);
11018
11019 if (uNewView == LVS_REPORT)
11020 {
11021 HDLAYOUT hl;
11022 WINDOWPOS wp;
11023
11024 LISTVIEW_CreateHeader( infoPtr );
11025
11026 hl.prc = &infoPtr->rcList;
11027 hl.pwpos = &wp;
11028 SendMessageW( infoPtr->hwndHeader, HDM_LAYOUT, 0, (LPARAM)&hl );
11029 SetWindowPos(infoPtr->hwndHeader, infoPtr->hwndSelf, wp.x, wp.y, wp.cx, wp.cy,
11030 wp.flags | ((infoPtr->dwStyle & LVS_NOCOLUMNHEADER)
11031 ? SWP_HIDEWINDOW : SWP_SHOWWINDOW));
11032 }
11033
11034 LISTVIEW_UpdateItemSize(infoPtr);
11035 }
11036
11037 if (uNewView == LVS_REPORT || infoPtr->dwLvExStyle & LVS_EX_HEADERINALLVIEWS)
11038 {
11039 if ((lpss->styleOld ^ lpss->styleNew) & LVS_NOCOLUMNHEADER)
11040 {
11041 if (lpss->styleNew & LVS_NOCOLUMNHEADER)
11042 {
11043 /* Turn off the header control */
11044 style = GetWindowLongW(infoPtr->hwndHeader, GWL_STYLE);
11045 TRACE("Hide header control, was 0x%08x\n", style);
11046 SetWindowLongW(infoPtr->hwndHeader, GWL_STYLE, style | HDS_HIDDEN);
11047 } else {
11048 /* Turn on the header control */
11049 if ((style = GetWindowLongW(infoPtr->hwndHeader, GWL_STYLE)) & HDS_HIDDEN)
11050 {
11051 TRACE("Show header control, was 0x%08x\n", style);
11052 SetWindowLongW(infoPtr->hwndHeader, GWL_STYLE, (style & ~HDS_HIDDEN) | WS_VISIBLE);
11053 }
11054 }
11055 }
11056 }
11057
11058 if ( (uNewView == LVS_ICON || uNewView == LVS_SMALLICON) &&
11059 (uNewView != uOldView || ((lpss->styleNew ^ lpss->styleOld) & LVS_ALIGNMASK)) )
11060 LISTVIEW_Arrange(infoPtr, LVA_DEFAULT);
11061
11062 /* update the size of the client area */
11063 LISTVIEW_UpdateSize(infoPtr);
11064
11065 /* add scrollbars if needed */
11066 LISTVIEW_UpdateScroll(infoPtr);
11067
11068 /* invalidate client area + erase background */
11069 LISTVIEW_InvalidateList(infoPtr);
11070
11071 return 0;
11072 }
11073
11074 /***
11075 * DESCRIPTION:
11076 * Processes WM_STYLECHANGING messages.
11077 *
11078 * PARAMETER(S):
11079 * [I] wStyleType : window style type (normal or extended)
11080 * [I0] lpss : window style information
11081 *
11082 * RETURN:
11083 * Zero
11084 */
11085 static INT LISTVIEW_StyleChanging(WPARAM wStyleType,
11086 STYLESTRUCT *lpss)
11087 {
11088 TRACE("(styletype=%lx, styleOld=0x%08x, styleNew=0x%08x)\n",
11089 wStyleType, lpss->styleOld, lpss->styleNew);
11090
11091 /* don't forward LVS_OWNERDATA only if not already set to */
11092 if ((lpss->styleNew ^ lpss->styleOld) & LVS_OWNERDATA)
11093 {
11094 if (lpss->styleOld & LVS_OWNERDATA)
11095 lpss->styleNew |= LVS_OWNERDATA;
11096 else
11097 lpss->styleNew &= ~LVS_OWNERDATA;
11098 }
11099
11100 return 0;
11101 }
11102
11103 /***
11104 * DESCRIPTION:
11105 * Processes WM_SHOWWINDOW messages.
11106 *
11107 * PARAMETER(S):
11108 * [I] infoPtr : valid pointer to the listview structure
11109 * [I] bShown : window is being shown (FALSE when hidden)
11110 * [I] iStatus : window show status
11111 *
11112 * RETURN:
11113 * Zero
11114 */
11115 static LRESULT LISTVIEW_ShowWindow(LISTVIEW_INFO *infoPtr, WPARAM bShown, LPARAM iStatus)
11116 {
11117 /* header delayed creation */
11118 if ((infoPtr->uView == LV_VIEW_DETAILS) && bShown)
11119 {
11120 LISTVIEW_CreateHeader(infoPtr);
11121
11122 if (!(LVS_NOCOLUMNHEADER & infoPtr->dwStyle))
11123 ShowWindow(infoPtr->hwndHeader, SW_SHOWNORMAL);
11124 }
11125
11126 return DefWindowProcW(infoPtr->hwndSelf, WM_SHOWWINDOW, bShown, iStatus);
11127 }
11128
11129 /***
11130 * DESCRIPTION:
11131 * Processes CCM_GETVERSION messages.
11132 *
11133 * PARAMETER(S):
11134 * [I] infoPtr : valid pointer to the listview structure
11135 *
11136 * RETURN:
11137 * Current version
11138 */
11139 static inline LRESULT LISTVIEW_GetVersion(const LISTVIEW_INFO *infoPtr)
11140 {
11141 return infoPtr->iVersion;
11142 }
11143
11144 /***
11145 * DESCRIPTION:
11146 * Processes CCM_SETVERSION messages.
11147 *
11148 * PARAMETER(S):
11149 * [I] infoPtr : valid pointer to the listview structure
11150 * [I] iVersion : version to be set
11151 *
11152 * RETURN:
11153 * -1 when requested version is greater than DLL version;
11154 * previous version otherwise
11155 */
11156 static LRESULT LISTVIEW_SetVersion(LISTVIEW_INFO *infoPtr, DWORD iVersion)
11157 {
11158 INT iOldVersion = infoPtr->iVersion;
11159
11160 if (iVersion > COMCTL32_VERSION)
11161 return -1;
11162
11163 infoPtr->iVersion = iVersion;
11164
11165 TRACE("new version %d\n", iVersion);
11166
11167 return iOldVersion;
11168 }
11169
11170 /***
11171 * DESCRIPTION:
11172 * Window procedure of the listview control.
11173 *
11174 */
11175 static LRESULT WINAPI
11176 LISTVIEW_WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
11177 {
11178 LISTVIEW_INFO *infoPtr = (LISTVIEW_INFO *)GetWindowLongPtrW(hwnd, 0);
11179
11180 TRACE("(hwnd=%p uMsg=%x wParam=%lx lParam=%lx)\n", hwnd, uMsg, wParam, lParam);
11181
11182 if (!infoPtr && (uMsg != WM_NCCREATE))
11183 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
11184
11185 switch (uMsg)
11186 {
11187 case LVM_APPROXIMATEVIEWRECT:
11188 return LISTVIEW_ApproximateViewRect(infoPtr, (INT)wParam,
11189 LOWORD(lParam), HIWORD(lParam));
11190 case LVM_ARRANGE:
11191 return LISTVIEW_Arrange(infoPtr, (INT)wParam);
11192
11193 case LVM_CANCELEDITLABEL:
11194 return LISTVIEW_CancelEditLabel(infoPtr);
11195
11196 case LVM_CREATEDRAGIMAGE:
11197 return (LRESULT)LISTVIEW_CreateDragImage(infoPtr, (INT)wParam, (LPPOINT)lParam);
11198
11199 case LVM_DELETEALLITEMS:
11200 return LISTVIEW_DeleteAllItems(infoPtr, FALSE);
11201
11202 case LVM_DELETECOLUMN:
11203 return LISTVIEW_DeleteColumn(infoPtr, (INT)wParam);
11204
11205 case LVM_DELETEITEM:
11206 return LISTVIEW_DeleteItem(infoPtr, (INT)wParam);
11207
11208 case LVM_EDITLABELA:
11209 case LVM_EDITLABELW:
11210 return (LRESULT)LISTVIEW_EditLabelT(infoPtr, (INT)wParam,
11211 uMsg == LVM_EDITLABELW);
11212 /* case LVM_ENABLEGROUPVIEW: */
11213
11214 case LVM_ENSUREVISIBLE:
11215 return LISTVIEW_EnsureVisible(infoPtr, (INT)wParam, (BOOL)lParam);
11216
11217 case LVM_FINDITEMW:
11218 return LISTVIEW_FindItemW(infoPtr, (INT)wParam, (LPLVFINDINFOW)lParam);
11219
11220 case LVM_FINDITEMA:
11221 return LISTVIEW_FindItemA(infoPtr, (INT)wParam, (LPLVFINDINFOA)lParam);
11222
11223 case LVM_GETBKCOLOR:
11224 return infoPtr->clrBk;
11225
11226 /* case LVM_GETBKIMAGE: */
11227
11228 case LVM_GETCALLBACKMASK:
11229 return infoPtr->uCallbackMask;
11230
11231 case LVM_GETCOLUMNA:
11232 case LVM_GETCOLUMNW:
11233 return LISTVIEW_GetColumnT(infoPtr, (INT)wParam, (LPLVCOLUMNW)lParam,
11234 uMsg == LVM_GETCOLUMNW);
11235
11236 case LVM_GETCOLUMNORDERARRAY:
11237 return LISTVIEW_GetColumnOrderArray(infoPtr, (INT)wParam, (LPINT)lParam);
11238
11239 case LVM_GETCOLUMNWIDTH:
11240 return LISTVIEW_GetColumnWidth(infoPtr, (INT)wParam);
11241
11242 case LVM_GETCOUNTPERPAGE:
11243 return LISTVIEW_GetCountPerPage(infoPtr);
11244
11245 case LVM_GETEDITCONTROL:
11246 return (LRESULT)infoPtr->hwndEdit;
11247
11248 case LVM_GETEXTENDEDLISTVIEWSTYLE:
11249 return infoPtr->dwLvExStyle;
11250
11251 /* case LVM_GETGROUPINFO: */
11252
11253 /* case LVM_GETGROUPMETRICS: */
11254
11255 case LVM_GETHEADER:
11256 return (LRESULT)infoPtr->hwndHeader;
11257
11258 case LVM_GETHOTCURSOR:
11259 return (LRESULT)infoPtr->hHotCursor;
11260
11261 case LVM_GETHOTITEM:
11262 return infoPtr->nHotItem;
11263
11264 case LVM_GETHOVERTIME:
11265 return infoPtr->dwHoverTime;
11266
11267 case LVM_GETIMAGELIST:
11268 return (LRESULT)LISTVIEW_GetImageList(infoPtr, (INT)wParam);
11269
11270 /* case LVM_GETINSERTMARK: */
11271
11272 /* case LVM_GETINSERTMARKCOLOR: */
11273
11274 /* case LVM_GETINSERTMARKRECT: */
11275
11276 case LVM_GETISEARCHSTRINGA:
11277 case LVM_GETISEARCHSTRINGW:
11278 FIXME("LVM_GETISEARCHSTRING: unimplemented\n");
11279 return FALSE;
11280
11281 case LVM_GETITEMA:
11282 case LVM_GETITEMW:
11283 return LISTVIEW_GetItemExtT(infoPtr, (LPLVITEMW)lParam, uMsg == LVM_GETITEMW);
11284
11285 case LVM_GETITEMCOUNT:
11286 return infoPtr->nItemCount;
11287
11288 case LVM_GETITEMPOSITION:
11289 return LISTVIEW_GetItemPosition(infoPtr, (INT)wParam, (LPPOINT)lParam);
11290
11291 case LVM_GETITEMRECT:
11292 return LISTVIEW_GetItemRect(infoPtr, (INT)wParam, (LPRECT)lParam);
11293
11294 case LVM_GETITEMSPACING:
11295 return LISTVIEW_GetItemSpacing(infoPtr, (BOOL)wParam);
11296
11297 case LVM_GETITEMSTATE:
11298 return LISTVIEW_GetItemState(infoPtr, (INT)wParam, (UINT)lParam);
11299
11300 case LVM_GETITEMTEXTA:
11301 case LVM_GETITEMTEXTW:
11302 return LISTVIEW_GetItemTextT(infoPtr, (INT)wParam, (LPLVITEMW)lParam,
11303 uMsg == LVM_GETITEMTEXTW);
11304
11305 case LVM_GETNEXTITEM:
11306 return LISTVIEW_GetNextItem(infoPtr, (INT)wParam, LOWORD(lParam));
11307
11308 case LVM_GETNUMBEROFWORKAREAS:
11309 FIXME("LVM_GETNUMBEROFWORKAREAS: unimplemented\n");
11310 return 1;
11311
11312 case LVM_GETORIGIN:
11313 if (!lParam) return FALSE;
11314 if (infoPtr->uView == LV_VIEW_DETAILS ||
11315 infoPtr->uView == LV_VIEW_LIST) return FALSE;
11316 LISTVIEW_GetOrigin(infoPtr, (LPPOINT)lParam);
11317 return TRUE;
11318
11319 /* case LVM_GETOUTLINECOLOR: */
11320
11321 /* case LVM_GETSELECTEDCOLUMN: */
11322
11323 case LVM_GETSELECTEDCOUNT:
11324 return LISTVIEW_GetSelectedCount(infoPtr);
11325
11326 case LVM_GETSELECTIONMARK:
11327 return infoPtr->nSelectionMark;
11328
11329 case LVM_GETSTRINGWIDTHA:
11330 case LVM_GETSTRINGWIDTHW:
11331 return LISTVIEW_GetStringWidthT(infoPtr, (LPCWSTR)lParam,
11332 uMsg == LVM_GETSTRINGWIDTHW);
11333
11334 case LVM_GETSUBITEMRECT:
11335 return LISTVIEW_GetSubItemRect(infoPtr, (UINT)wParam, (LPRECT)lParam);
11336
11337 case LVM_GETTEXTBKCOLOR:
11338 return infoPtr->clrTextBk;
11339
11340 case LVM_GETTEXTCOLOR:
11341 return infoPtr->clrText;
11342
11343 /* case LVM_GETTILEINFO: */
11344
11345 /* case LVM_GETTILEVIEWINFO: */
11346
11347 case LVM_GETTOOLTIPS:
11348 if( !infoPtr->hwndToolTip )
11349 infoPtr->hwndToolTip = COMCTL32_CreateToolTip( hwnd );
11350 return (LRESULT)infoPtr->hwndToolTip;
11351
11352 case LVM_GETTOPINDEX:
11353 return LISTVIEW_GetTopIndex(infoPtr);
11354
11355 case LVM_GETUNICODEFORMAT:
11356 return (infoPtr->notifyFormat == NFR_UNICODE);
11357
11358 case LVM_GETVIEW:
11359 return infoPtr->uView;
11360
11361 case LVM_GETVIEWRECT:
11362 return LISTVIEW_GetViewRect(infoPtr, (LPRECT)lParam);
11363
11364 case LVM_GETWORKAREAS:
11365 FIXME("LVM_GETWORKAREAS: unimplemented\n");
11366 return FALSE;
11367
11368 /* case LVM_HASGROUP: */
11369
11370 case LVM_HITTEST:
11371 return LISTVIEW_HitTest(infoPtr, (LPLVHITTESTINFO)lParam, FALSE, TRUE);
11372
11373 case LVM_INSERTCOLUMNA:
11374 case LVM_INSERTCOLUMNW:
11375 return LISTVIEW_InsertColumnT(infoPtr, (INT)wParam, (LPLVCOLUMNW)lParam,
11376 uMsg == LVM_INSERTCOLUMNW);
11377
11378 /* case LVM_INSERTGROUP: */
11379
11380 /* case LVM_INSERTGROUPSORTED: */
11381
11382 case LVM_INSERTITEMA:
11383 case LVM_INSERTITEMW:
11384 return LISTVIEW_InsertItemT(infoPtr, (LPLVITEMW)lParam, uMsg == LVM_INSERTITEMW);
11385
11386 /* case LVM_INSERTMARKHITTEST: */
11387
11388 /* case LVM_ISGROUPVIEWENABLED: */
11389
11390 case LVM_ISITEMVISIBLE:
11391 return LISTVIEW_IsItemVisible(infoPtr, (INT)wParam);
11392
11393 case LVM_MAPIDTOINDEX:
11394 return LISTVIEW_MapIdToIndex(infoPtr, (UINT)wParam);
11395
11396 case LVM_MAPINDEXTOID:
11397 return LISTVIEW_MapIndexToId(infoPtr, (INT)wParam);
11398
11399 /* case LVM_MOVEGROUP: */
11400
11401 /* case LVM_MOVEITEMTOGROUP: */
11402
11403 case LVM_REDRAWITEMS:
11404 return LISTVIEW_RedrawItems(infoPtr, (INT)wParam, (INT)lParam);
11405
11406 /* case LVM_REMOVEALLGROUPS: */
11407
11408 /* case LVM_REMOVEGROUP: */
11409
11410 case LVM_SCROLL:
11411 return LISTVIEW_Scroll(infoPtr, (INT)wParam, (INT)lParam);
11412
11413 case LVM_SETBKCOLOR:
11414 return LISTVIEW_SetBkColor(infoPtr, (COLORREF)lParam);
11415
11416 /* case LVM_SETBKIMAGE: */
11417
11418 case LVM_SETCALLBACKMASK:
11419 infoPtr->uCallbackMask = (UINT)wParam;
11420 return TRUE;
11421
11422 case LVM_SETCOLUMNA:
11423 case LVM_SETCOLUMNW:
11424 return LISTVIEW_SetColumnT(infoPtr, (INT)wParam, (LPLVCOLUMNW)lParam,
11425 uMsg == LVM_SETCOLUMNW);
11426
11427 case LVM_SETCOLUMNORDERARRAY:
11428 return LISTVIEW_SetColumnOrderArray(infoPtr, (INT)wParam, (LPINT)lParam);
11429
11430 case LVM_SETCOLUMNWIDTH:
11431 return LISTVIEW_SetColumnWidth(infoPtr, (INT)wParam, (short)LOWORD(lParam));
11432
11433 case LVM_SETEXTENDEDLISTVIEWSTYLE:
11434 return LISTVIEW_SetExtendedListViewStyle(infoPtr, (DWORD)wParam, (DWORD)lParam);
11435
11436 /* case LVM_SETGROUPINFO: */
11437
11438 /* case LVM_SETGROUPMETRICS: */
11439
11440 case LVM_SETHOTCURSOR:
11441 return (LRESULT)LISTVIEW_SetHotCursor(infoPtr, (HCURSOR)lParam);
11442
11443 case LVM_SETHOTITEM:
11444 return LISTVIEW_SetHotItem(infoPtr, (INT)wParam);
11445
11446 case LVM_SETHOVERTIME:
11447 return LISTVIEW_SetHoverTime(infoPtr, (DWORD)lParam);
11448
11449 case LVM_SETICONSPACING:
11450 if(lParam == -1)
11451 return LISTVIEW_SetIconSpacing(infoPtr, -1, -1);
11452 return LISTVIEW_SetIconSpacing(infoPtr, LOWORD(lParam), HIWORD(lParam));
11453
11454 case LVM_SETIMAGELIST:
11455 return (LRESULT)LISTVIEW_SetImageList(infoPtr, (INT)wParam, (HIMAGELIST)lParam);
11456
11457 /* case LVM_SETINFOTIP: */
11458
11459 /* case LVM_SETINSERTMARK: */
11460
11461 /* case LVM_SETINSERTMARKCOLOR: */
11462
11463 case LVM_SETITEMA:
11464 case LVM_SETITEMW:
11465 {
11466 if (infoPtr->dwStyle & LVS_OWNERDATA) return FALSE;
11467 return LISTVIEW_SetItemT(infoPtr, (LPLVITEMW)lParam, (uMsg == LVM_SETITEMW));
11468 }
11469
11470 case LVM_SETITEMCOUNT:
11471 return LISTVIEW_SetItemCount(infoPtr, (INT)wParam, (DWORD)lParam);
11472
11473 case LVM_SETITEMPOSITION:
11474 {
11475 POINT pt;
11476 pt.x = (short)LOWORD(lParam);
11477 pt.y = (short)HIWORD(lParam);
11478 return LISTVIEW_SetItemPosition(infoPtr, (INT)wParam, &pt);
11479 }
11480
11481 case LVM_SETITEMPOSITION32:
11482 return LISTVIEW_SetItemPosition(infoPtr, (INT)wParam, (POINT*)lParam);
11483
11484 case LVM_SETITEMSTATE:
11485 return LISTVIEW_SetItemState(infoPtr, (INT)wParam, (LPLVITEMW)lParam);
11486
11487 case LVM_SETITEMTEXTA:
11488 case LVM_SETITEMTEXTW:
11489 return LISTVIEW_SetItemTextT(infoPtr, (INT)wParam, (LPLVITEMW)lParam,
11490 uMsg == LVM_SETITEMTEXTW);
11491
11492 /* case LVM_SETOUTLINECOLOR: */
11493
11494 /* case LVM_SETSELECTEDCOLUMN: */
11495
11496 case LVM_SETSELECTIONMARK:
11497 return LISTVIEW_SetSelectionMark(infoPtr, (INT)lParam);
11498
11499 case LVM_SETTEXTBKCOLOR:
11500 return LISTVIEW_SetTextBkColor(infoPtr, (COLORREF)lParam);
11501
11502 case LVM_SETTEXTCOLOR:
11503 return LISTVIEW_SetTextColor(infoPtr, (COLORREF)lParam);
11504
11505 /* case LVM_SETTILEINFO: */
11506
11507 /* case LVM_SETTILEVIEWINFO: */
11508
11509 /* case LVM_SETTILEWIDTH: */
11510
11511 case LVM_SETTOOLTIPS:
11512 return (LRESULT)LISTVIEW_SetToolTips(infoPtr, (HWND)lParam);
11513
11514 case LVM_SETUNICODEFORMAT:
11515 return LISTVIEW_SetUnicodeFormat(infoPtr, wParam);
11516
11517 case LVM_SETVIEW:
11518 return LISTVIEW_SetView(infoPtr, wParam);
11519
11520 /* case LVM_SETWORKAREAS: */
11521
11522 /* case LVM_SORTGROUPS: */
11523
11524 case LVM_SORTITEMS:
11525 case LVM_SORTITEMSEX:
11526 return LISTVIEW_SortItems(infoPtr, (PFNLVCOMPARE)lParam, wParam,
11527 uMsg == LVM_SORTITEMSEX);
11528 case LVM_SUBITEMHITTEST:
11529 return LISTVIEW_HitTest(infoPtr, (LPLVHITTESTINFO)lParam, TRUE, FALSE);
11530
11531 case LVM_UPDATE:
11532 return LISTVIEW_Update(infoPtr, (INT)wParam);
11533
11534 case CCM_GETVERSION:
11535 return LISTVIEW_GetVersion(infoPtr);
11536
11537 case CCM_SETVERSION:
11538 return LISTVIEW_SetVersion(infoPtr, wParam);
11539
11540 case WM_CHAR:
11541 return LISTVIEW_ProcessLetterKeys( infoPtr, wParam, lParam );
11542
11543 case WM_COMMAND:
11544 return LISTVIEW_Command(infoPtr, wParam, lParam);
11545
11546 case WM_NCCREATE:
11547 return LISTVIEW_NCCreate(hwnd, (LPCREATESTRUCTW)lParam);
11548
11549 case WM_CREATE:
11550 return LISTVIEW_Create(hwnd, (LPCREATESTRUCTW)lParam);
11551
11552 case WM_DESTROY:
11553 return LISTVIEW_Destroy(infoPtr);
11554
11555 case WM_ENABLE:
11556 return LISTVIEW_Enable(infoPtr);
11557
11558 case WM_ERASEBKGND:
11559 return LISTVIEW_EraseBkgnd(infoPtr, (HDC)wParam);
11560
11561 case WM_GETDLGCODE:
11562 return DLGC_WANTCHARS | DLGC_WANTARROWS;
11563
11564 case WM_GETFONT:
11565 return (LRESULT)infoPtr->hFont;
11566
11567 case WM_HSCROLL:
11568 return LISTVIEW_HScroll(infoPtr, (INT)LOWORD(wParam), 0);
11569
11570 case WM_KEYDOWN:
11571 return LISTVIEW_KeyDown(infoPtr, (INT)wParam, (LONG)lParam);
11572
11573 case WM_KILLFOCUS:
11574 return LISTVIEW_KillFocus(infoPtr);
11575
11576 case WM_LBUTTONDBLCLK:
11577 return LISTVIEW_LButtonDblClk(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11578
11579 case WM_LBUTTONDOWN:
11580 return LISTVIEW_LButtonDown(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11581
11582 case WM_LBUTTONUP:
11583 return LISTVIEW_LButtonUp(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11584
11585 case WM_MOUSEMOVE:
11586 return LISTVIEW_MouseMove (infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11587
11588 case WM_MOUSEHOVER:
11589 return LISTVIEW_MouseHover(infoPtr, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11590
11591 case WM_NCDESTROY:
11592 return LISTVIEW_NCDestroy(infoPtr);
11593
11594 case WM_NCPAINT:
11595 return LISTVIEW_NCPaint(infoPtr, (HRGN)wParam);
11596
11597 case WM_NOTIFY:
11598 return LISTVIEW_Notify(infoPtr, (LPNMHDR)lParam);
11599
11600 case WM_NOTIFYFORMAT:
11601 return LISTVIEW_NotifyFormat(infoPtr, (HWND)wParam, (INT)lParam);
11602
11603 case WM_PRINTCLIENT:
11604 return LISTVIEW_PrintClient(infoPtr, (HDC)wParam, (DWORD)lParam);
11605
11606 case WM_PAINT:
11607 return LISTVIEW_WMPaint(infoPtr, (HDC)wParam);
11608
11609 case WM_RBUTTONDBLCLK:
11610 return LISTVIEW_RButtonDblClk(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11611
11612 case WM_RBUTTONDOWN:
11613 return LISTVIEW_RButtonDown(infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
11614
11615 case WM_SETCURSOR:
11616 return LISTVIEW_SetCursor(infoPtr, wParam, lParam);
11617
11618 case WM_SETFOCUS:
11619 return LISTVIEW_SetFocus(infoPtr, (HWND)wParam);
11620
11621 case WM_SETFONT:
11622 return LISTVIEW_SetFont(infoPtr, (HFONT)wParam, (WORD)lParam);
11623
11624 case WM_SETREDRAW:
11625 return LISTVIEW_SetRedraw(infoPtr, (BOOL)wParam);
11626
11627 case WM_SHOWWINDOW:
11628 return LISTVIEW_ShowWindow(infoPtr, wParam, lParam);
11629
11630 case WM_STYLECHANGED:
11631 return LISTVIEW_StyleChanged(infoPtr, wParam, (LPSTYLESTRUCT)lParam);
11632
11633 case WM_STYLECHANGING:
11634 return LISTVIEW_StyleChanging(wParam, (LPSTYLESTRUCT)lParam);
11635
11636 case WM_SYSCOLORCHANGE:
11637 COMCTL32_RefreshSysColors();
11638 if (infoPtr->bDefaultBkColor)
11639 {
11640 LISTVIEW_SetBkColor(infoPtr, comctl32_color.clrWindow);
11641 infoPtr->bDefaultBkColor = TRUE;
11642 LISTVIEW_InvalidateList(infoPtr);
11643 }
11644 return 0;
11645
11646 /* case WM_TIMER: */
11647 case WM_THEMECHANGED:
11648 return LISTVIEW_ThemeChanged(infoPtr);
11649
11650 case WM_VSCROLL:
11651 return LISTVIEW_VScroll(infoPtr, (INT)LOWORD(wParam), 0);
11652
11653 case WM_MOUSEWHEEL:
11654 if (wParam & (MK_SHIFT | MK_CONTROL))
11655 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
11656 return LISTVIEW_MouseWheel(infoPtr, (short int)HIWORD(wParam));
11657
11658 case WM_WINDOWPOSCHANGED:
11659 if (!(((WINDOWPOS *)lParam)->flags & SWP_NOSIZE))
11660 {
11661 SetWindowPos(infoPtr->hwndSelf, 0, 0, 0, 0, 0, SWP_FRAMECHANGED | SWP_NOACTIVATE |
11662 SWP_NOZORDER | SWP_NOMOVE | SWP_NOSIZE);
11663
11664 if ((infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && (infoPtr->uView == LV_VIEW_DETAILS))
11665 {
11666 if (notify_measureitem(infoPtr)) LISTVIEW_InvalidateList(infoPtr);
11667 }
11668 LISTVIEW_Size(infoPtr, ((WINDOWPOS *)lParam)->cx, ((WINDOWPOS *)lParam)->cy);
11669 }
11670 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
11671
11672 /* case WM_WININICHANGE: */
11673
11674 default:
11675 if ((uMsg >= WM_USER) && (uMsg < WM_APP) && !COMCTL32_IsReflectedMessage(uMsg))
11676 ERR("unknown msg %04x wp=%08lx lp=%08lx\n", uMsg, wParam, lParam);
11677
11678 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
11679 }
11680
11681 }
11682
11683 /***
11684 * DESCRIPTION:
11685 * Registers the window class.
11686 *
11687 * PARAMETER(S):
11688 * None
11689 *
11690 * RETURN:
11691 * None
11692 */
11693 void LISTVIEW_Register(void)
11694 {
11695 WNDCLASSW wndClass;
11696
11697 ZeroMemory(&wndClass, sizeof(WNDCLASSW));
11698 wndClass.style = CS_GLOBALCLASS | CS_DBLCLKS;
11699 wndClass.lpfnWndProc = LISTVIEW_WindowProc;
11700 wndClass.cbClsExtra = 0;
11701 wndClass.cbWndExtra = sizeof(LISTVIEW_INFO *);
11702 wndClass.hCursor = LoadCursorW(0, (LPWSTR)IDC_ARROW);
11703 wndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
11704 wndClass.lpszClassName = WC_LISTVIEWW;
11705 RegisterClassW(&wndClass);
11706 }
11707
11708 /***
11709 * DESCRIPTION:
11710 * Unregisters the window class.
11711 *
11712 * PARAMETER(S):
11713 * None
11714 *
11715 * RETURN:
11716 * None
11717 */
11718 void LISTVIEW_Unregister(void)
11719 {
11720 UnregisterClassW(WC_LISTVIEWW, NULL);
11721 }
11722
11723 /***
11724 * DESCRIPTION:
11725 * Handle any WM_COMMAND messages
11726 *
11727 * PARAMETER(S):
11728 * [I] infoPtr : valid pointer to the listview structure
11729 * [I] wParam : the first message parameter
11730 * [I] lParam : the second message parameter
11731 *
11732 * RETURN:
11733 * Zero.
11734 */
11735 static LRESULT LISTVIEW_Command(LISTVIEW_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
11736 {
11737
11738 TRACE("(%p %x %x %lx)\n", infoPtr, HIWORD(wParam), LOWORD(wParam), lParam);
11739
11740 if (!infoPtr->hwndEdit) return 0;
11741
11742 switch (HIWORD(wParam))
11743 {
11744 case EN_UPDATE:
11745 {
11746 /*
11747 * Adjust the edit window size
11748 */
11749 WCHAR buffer[1024];
11750 HDC hdc = GetDC(infoPtr->hwndEdit);
11751 HFONT hFont, hOldFont = 0;
11752 RECT rect;
11753 SIZE sz;
11754
11755 if (!infoPtr->hwndEdit || !hdc) return 0;
11756 GetWindowTextW(infoPtr->hwndEdit, buffer, sizeof(buffer)/sizeof(buffer[0]));
11757 GetWindowRect(infoPtr->hwndEdit, &rect);
11758
11759 /* Select font to get the right dimension of the string */
11760 hFont = (HFONT)SendMessageW(infoPtr->hwndEdit, WM_GETFONT, 0, 0);
11761 if (hFont)
11762 {
11763 hOldFont = SelectObject(hdc, hFont);
11764 }
11765
11766 if (GetTextExtentPoint32W(hdc, buffer, lstrlenW(buffer), &sz))
11767 {
11768 TEXTMETRICW textMetric;
11769
11770 /* Add Extra spacing for the next character */
11771 GetTextMetricsW(hdc, &textMetric);
11772 sz.cx += (textMetric.tmMaxCharWidth * 2);
11773
11774 SetWindowPos(infoPtr->hwndEdit, NULL, 0, 0, sz.cx,
11775 rect.bottom - rect.top, SWP_DRAWFRAME | SWP_NOMOVE | SWP_NOZORDER);
11776 }
11777 if (hFont)
11778 SelectObject(hdc, hOldFont);
11779
11780 ReleaseDC(infoPtr->hwndEdit, hdc);
11781
11782 break;
11783 }
11784 case EN_KILLFOCUS:
11785 {
11786 LISTVIEW_CancelEditLabel(infoPtr);
11787 break;
11788 }
11789
11790 default:
11791 return SendMessageW (infoPtr->hwndNotify, WM_COMMAND, wParam, lParam);
11792 }
11793
11794 return 0;
11795 }