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