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