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