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 *
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
14 *
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 *
24 * NOTES
25 *
26 * This code was audited for completeness against the documented features
27 * of Comctl32.dll version 6.0 on May. 20, 2005, by James Hawkins.
28 *
29 * Unless otherwise noted, we believe this code to be complete, as per
30 * the specification mentioned above.
31 * If you discover missing features, or bugs, please note them below.
32 *
33 * TODO:
34 *
35 * Default Message Processing
36 * -- EN_KILLFOCUS should be handled in WM_COMMAND
37 * -- WM_CREATE: create the icon and small icon image lists at this point only if
38 * the LVS_SHAREIMAGELISTS style is not specified.
39 * -- WM_ERASEBKGND: forward this message to the parent window if the bkgnd
40 * color is CLR_NONE.
41 * -- WM_WINDOWPOSCHANGED: arrange the list items if the current view is icon
42 * or small icon and the LVS_AUTOARRANGE style is specified.
43 * -- WM_TIMER
44 * -- WM_WININICHANGE
45 *
46 * Features
47 * -- Hot item handling, mouse hovering
48 * -- Workareas support
49 * -- Tilemode support
50 * -- Groups support
51 *
52 * Bugs
53 * -- Expand large item in ICON mode when the cursor is flying over the icon or text.
54 * -- Support CustomDraw options for _WIN32_IE >= 0x560 (see NMLVCUSTOMDRAW docs).
55 * -- LVA_SNAPTOGRID not implemented
56 * -- LISTVIEW_ApproximateViewRect partially implemented
57 * -- LISTVIEW_[GS]etColumnOrderArray stubs
58 * -- LISTVIEW_SetColumnWidth ignores header images & bitmap
59 * -- LISTVIEW_SetIconSpacing is incomplete
60 * -- LISTVIEW_SortItems is broken
61 * -- LISTVIEW_StyleChanged doesn't handle some changes too well
62 *
63 * Speedups
64 * -- LISTVIEW_GetNextItem needs to be rewritten. It is currently
65 * linear in the number of items in the list, and this is
66 * unacceptable for large lists.
67 * -- in sorted mode, LISTVIEW_InsertItemT sorts the array,
68 * instead of inserting in the right spot
69 * -- we should keep an ordered array of coordinates in iconic mode
70 * this would allow to frame items (iterator_frameditems),
71 * and find nearest item (LVFI_NEARESTXY) a lot more efficiently
72 *
73 * Flags
74 * -- LVIF_COLUMNS
75 * -- LVIF_GROUPID
76 * -- LVIF_NORECOMPUTE
77 *
78 * States
79 * -- LVIS_ACTIVATING (not currently supported by comctl32.dll version 6.0)
80 * -- LVIS_CUT
81 * -- LVIS_DROPHILITED
82 * -- LVIS_OVERLAYMASK
83 *
84 * Styles
85 * -- LVS_NOLABELWRAP
86 * -- LVS_NOSCROLL (see Q137520)
87 * -- LVS_SORTASCENDING, LVS_SORTDESCENDING
88 * -- LVS_ALIGNTOP
89 * -- LVS_TYPESTYLEMASK
90 *
91 * Extended Styles
92 * -- LVS_EX_BORDERSELECT
93 * -- LVS_EX_FLATSB
94 * -- LVS_EX_HEADERDRAGDROP
95 * -- LVS_EX_INFOTIP
96 * -- LVS_EX_LABELTIP
97 * -- LVS_EX_MULTIWORKAREAS
98 * -- LVS_EX_REGIONAL
99 * -- LVS_EX_SIMPLESELECT
100 * -- LVS_EX_TWOCLICKACTIVATE
101 * -- LVS_EX_UNDERLINECOLD
102 * -- LVS_EX_UNDERLINEHOT
103 *
104 * Notifications:
105 * -- LVN_BEGINSCROLL, LVN_ENDSCROLL
106 * -- LVN_GETINFOTIP
107 * -- LVN_HOTTRACK
108 * -- LVN_MARQUEEBEGIN
109 * -- LVN_ODFINDITEM
110 * -- LVN_SETDISPINFO
111 * -- NM_HOVER
112 * -- LVN_BEGINRDRAG
113 *
114 * Messages:
115 * -- LVM_CANCELEDITLABEL
116 * -- LVM_ENABLEGROUPVIEW
117 * -- LVM_GETBKIMAGE, LVM_SETBKIMAGE
118 * -- LVM_GETGROUPINFO, LVM_SETGROUPINFO
119 * -- LVM_GETGROUPMETRICS, LVM_SETGROUPMETRICS
120 * -- LVM_GETINSERTMARK, LVM_SETINSERTMARK
121 * -- LVM_GETINSERTMARKCOLOR, LVM_SETINSERTMARKCOLOR
122 * -- LVM_GETINSERTMARKRECT
123 * -- LVM_GETNUMBEROFWORKAREAS
124 * -- LVM_GETOUTLINECOLOR, LVM_SETOUTLINECOLOR
125 * -- LVM_GETSELECTEDCOLUMN, LVM_SETSELECTEDCOLUMN
126 * -- LVM_GETISEARCHSTRINGW, LVM_GETISEARCHSTRINGA
127 * -- LVM_GETTILEINFO, LVM_SETTILEINFO
128 * -- LVM_GETTILEVIEWINFO, LVM_SETTILEVIEWINFO
129 * -- LVM_GETUNICODEFORMAT, LVM_SETUNICODEFORMAT
130 * -- LVM_GETVIEW, LVM_SETVIEW
131 * -- LVM_GETWORKAREAS, LVM_SETWORKAREAS
132 * -- LVM_HASGROUP, LVM_INSERTGROUP, LVM_REMOVEGROUP, LVM_REMOVEALLGROUPS
133 * -- LVM_INSERTGROUPSORTED
134 * -- LVM_INSERTMARKHITTEST
135 * -- LVM_ISGROUPVIEWENABLED
136 * -- LVM_MAPIDTOINDEX, LVM_MAPINDEXTOID
137 * -- LVM_MOVEGROUP
138 * -- LVM_MOVEITEMTOGROUP
139 * -- LVM_SETINFOTIP
140 * -- LVM_SETTILEWIDTH
141 * -- LVM_SORTGROUPS
142 * -- LVM_SORTITEMSEX
143 *
144 * Macros:
145 * -- ListView_GetCheckSate, ListView_SetCheckState
146 * -- ListView_GetHoverTime, ListView_SetHoverTime
147 * -- ListView_GetISearchString
148 * -- ListView_GetNumberOfWorkAreas
149 * -- ListView_GetOrigin
150 * -- ListView_GetTextBkColor
151 * -- ListView_GetUnicodeFormat, ListView_SetUnicodeFormat
152 * -- ListView_GetWorkAreas, ListView_SetWorkAreas
153 * -- ListView_SortItemsEx
154 *
155 * Functions:
156 * -- LVGroupComparE
157 *
158 * Known differences in message stream from native control (not known if
159 * these differences cause problems):
160 * LVM_INSERTITEM issues LVM_SETITEMSTATE and LVM_SETITEM in certain cases.
161 * LVM_SETITEM does not always issue LVN_ITEMCHANGING/LVN_ITEMCHANGED.
162 * WM_CREATE does not issue WM_QUERYUISTATE and associated registry
163 * processing for "USEDOUBLECLICKTIME".
164 */
165
166 #include "config.h"
167 #include "wine/port.h"
168
169 #include <assert.h>
170 #include <ctype.h>
171 #include <string.h>
172 #include <stdlib.h>
173 #include <stdarg.h>
174 #include <stdio.h>
175
176 #include "windef.h"
177 #include "winbase.h"
178 #include "winnt.h"
179 #include "wingdi.h"
180 #include "winuser.h"
181 #include "winnls.h"
182 #include "commctrl.h"
183 #include "comctl32.h"
184 #include "uxtheme.h"
185
186 #include "wine/debug.h"
187 #include "wine/unicode.h"
188
189 WINE_DEFAULT_DEBUG_CHANNEL(listview);
190
191 /* make sure you set this to 0 for production use! */
192 #define DEBUG_RANGES 1
193
194 typedef struct tagCOLUMN_INFO
195 {
196 RECT rcHeader; /* tracks the header's rectangle */
197 int fmt; /* same as LVCOLUMN.fmt */
198 } COLUMN_INFO;
199
200 typedef struct tagITEMHDR
201 {
202 LPWSTR pszText;
203 INT iImage;
204 } ITEMHDR, *LPITEMHDR;
205
206 typedef struct tagSUBITEM_INFO
207 {
208 ITEMHDR hdr;
209 INT iSubItem;
210 } SUBITEM_INFO;
211
212 typedef struct tagITEM_INFO
213 {
214 ITEMHDR hdr;
215 UINT state;
216 LPARAM lParam;
217 INT iIndent;
218 } ITEM_INFO;
219
220 typedef struct tagRANGE
221 {
222 INT lower;
223 INT upper;
224 } RANGE;
225
226 typedef struct tagRANGES
227 {
228 HDPA hdpa;
229 } *RANGES;
230
231 typedef struct tagITERATOR
232 {
233 INT nItem;
234 INT nSpecial;
235 RANGE range;
236 RANGES ranges;
237 INT index;
238 } ITERATOR;
239
240 typedef struct tagDELAYED_ITEM_EDIT
241 {
242 BOOL fEnabled;
243 INT iItem;
244 } DELAYED_ITEM_EDIT;
245
246 typedef struct tagLISTVIEW_INFO
247 {
248 HWND hwndSelf;
249 HBRUSH hBkBrush;
250 COLORREF clrBk;
251 COLORREF clrText;
252 COLORREF clrTextBk;
253 HIMAGELIST himlNormal;
254 HIMAGELIST himlSmall;
255 HIMAGELIST himlState;
256 BOOL bLButtonDown;
257 BOOL bRButtonDown;
258 BOOL bDragging;
259 POINT ptClickPos; /* point where the user clicked */
260 BOOL bNoItemMetrics; /* flags if item metrics are not yet computed */
261 INT nItemHeight;
262 INT nItemWidth;
263 RANGES selectionRanges;
264 INT nSelectionMark;
265 INT nHotItem;
266 SHORT notifyFormat;
267 HWND hwndNotify;
268 RECT rcList; /* This rectangle is really the window
269 * client rectangle possibly reduced by the
270 * horizontal scroll bar and/or header - see
271 * LISTVIEW_UpdateSize. This rectangle offset
272 * by the LISTVIEW_GetOrigin value is in
273 * client coordinates */
274 SIZE iconSize;
275 SIZE iconSpacing;
276 SIZE iconStateSize;
277 UINT uCallbackMask;
278 HWND hwndHeader;
279 HCURSOR hHotCursor;
280 HFONT hDefaultFont;
281 HFONT hFont;
282 INT ntmHeight; /* Some cached metrics of the font used */
283 INT ntmMaxCharWidth; /* by the listview to draw items */
284 INT nEllipsisWidth;
285 BOOL bRedraw; /* Turns on/off repaints & invalidations */
286 BOOL bAutoarrange; /* Autoarrange flag when NOT in LVS_AUTOARRANGE */
287 BOOL bFocus;
288 BOOL bDoChangeNotify; /* send change notification messages? */
289 INT nFocusedItem;
290 RECT rcFocus;
291 DWORD dwStyle; /* the cached window GWL_STYLE */
292 DWORD dwLvExStyle; /* extended listview style */
293 INT nItemCount; /* the number of items in the list */
294 HDPA hdpaItems; /* array ITEM_INFO pointers */
295 HDPA hdpaPosX; /* maintains the (X, Y) coordinates of the */
296 HDPA hdpaPosY; /* items in LVS_ICON, and LVS_SMALLICON modes */
297 HDPA hdpaColumns; /* array of COLUMN_INFO pointers */
298 POINT currIconPos; /* this is the position next icon will be placed */
299 PFNLVCOMPARE pfnCompare;
300 LPARAM lParamSort;
301 HWND hwndEdit;
302 WNDPROC EditWndProc;
303 INT nEditLabelItem;
304 DWORD dwHoverTime;
305 HWND hwndToolTip;
306
307 DWORD cditemmode; /* Keep the custom draw flags for an item/row */
308
309 DWORD lastKeyPressTimestamp;
310 WPARAM charCode;
311 INT nSearchParamLength;
312 WCHAR szSearchParam[ MAX_PATH ];
313 BOOL bIsDrawing;
314 INT nMeasureItemHeight;
315 INT xTrackLine; /* The x coefficient of the track line or -1 if none */
316 DELAYED_ITEM_EDIT itemEdit; /* Pointer to this structure will be the timer ID */
317 } LISTVIEW_INFO;
318
319 /*
320 * constants
321 */
322 /* How many we debug buffer to allocate */
323 #define DEBUG_BUFFERS 20
324 /* The size of a single debug bbuffer */
325 #define DEBUG_BUFFER_SIZE 256
326
327 /* Internal interface to LISTVIEW_HScroll and LISTVIEW_VScroll */
328 #define SB_INTERNAL -1
329
330 /* maximum size of a label */
331 #define DISP_TEXT_SIZE 512
332
333 /* padding for items in list and small icon display modes */
334 #define WIDTH_PADDING 12
335
336 /* padding for items in list, report and small icon display modes */
337 #define HEIGHT_PADDING 1
338
339 /* offset of items in report display mode */
340 #define REPORT_MARGINX 2
341
342 /* padding for icon in large icon display mode
343 * ICON_TOP_PADDING_NOTHITABLE - space between top of box and area
344 * that HITTEST will see.
345 * ICON_TOP_PADDING_HITABLE - spacing between above and icon.
346 * ICON_TOP_PADDING - sum of the two above.
347 * ICON_BOTTOM_PADDING - between bottom of icon and top of text
348 * LABEL_HOR_PADDING - between text and sides of box
349 * LABEL_VERT_PADDING - between bottom of text and end of box
350 *
351 * ICON_LR_PADDING - additional width above icon size.
352 * ICON_LR_HALF - half of the above value
353 */
354 #define ICON_TOP_PADDING_NOTHITABLE 2
355 #define ICON_TOP_PADDING_HITABLE 2
356 #define ICON_TOP_PADDING (ICON_TOP_PADDING_NOTHITABLE + ICON_TOP_PADDING_HITABLE)
357 #define ICON_BOTTOM_PADDING 4
358 #define LABEL_HOR_PADDING 5
359 #define LABEL_VERT_PADDING 7
360 #define ICON_LR_PADDING 16
361 #define ICON_LR_HALF (ICON_LR_PADDING/2)
362
363 /* default label width for items in list and small icon display modes */
364 #define DEFAULT_LABEL_WIDTH 40
365
366 /* default column width for items in list display mode */
367 #define DEFAULT_COLUMN_WIDTH 128
368
369 /* Size of "line" scroll for V & H scrolls */
370 #define LISTVIEW_SCROLL_ICON_LINE_SIZE 37
371
372 /* Padding between image and label */
373 #define IMAGE_PADDING 2
374
375 /* Padding behind the label */
376 #define TRAILING_LABEL_PADDING 12
377 #define TRAILING_HEADER_PADDING 11
378
379 /* Border for the icon caption */
380 #define CAPTION_BORDER 2
381
382 /* Standard DrawText flags */
383 #define LV_ML_DT_FLAGS (DT_TOP | DT_NOPREFIX | DT_EDITCONTROL | DT_CENTER | DT_WORDBREAK | DT_WORD_ELLIPSIS | DT_END_ELLIPSIS)
384 #define LV_FL_DT_FLAGS (DT_TOP | DT_NOPREFIX | DT_EDITCONTROL | DT_CENTER | DT_WORDBREAK | DT_NOCLIP)
385 #define LV_SL_DT_FLAGS (DT_VCENTER | DT_NOPREFIX | DT_EDITCONTROL | DT_SINGLELINE | DT_WORD_ELLIPSIS | DT_END_ELLIPSIS)
386
387 /* Image index from state */
388 #define STATEIMAGEINDEX(x) (((x) & LVIS_STATEIMAGEMASK) >> 12)
389
390 /* The time in milliseconds to reset the search in the list */
391 #define KEY_DELAY 450
392
393 /* Dump the LISTVIEW_INFO structure to the debug channel */
394 #define LISTVIEW_DUMP(iP) do { \
395 TRACE("hwndSelf=%p, clrBk=0x%06x, clrText=0x%06x, clrTextBk=0x%06x, ItemHeight=%d, ItemWidth=%d, Style=0x%08x\n", \
396 iP->hwndSelf, iP->clrBk, iP->clrText, iP->clrTextBk, \
397 iP->nItemHeight, iP->nItemWidth, iP->dwStyle); \
398 TRACE("hwndSelf=%p, himlNor=%p, himlSml=%p, himlState=%p, Focused=%d, Hot=%d, exStyle=0x%08x, Focus=%d\n", \
399 iP->hwndSelf, iP->himlNormal, iP->himlSmall, iP->himlState, \
400 iP->nFocusedItem, iP->nHotItem, iP->dwLvExStyle, iP->bFocus ); \
401 TRACE("hwndSelf=%p, ntmH=%d, icSz.cx=%d, icSz.cy=%d, icSp.cx=%d, icSp.cy=%d, notifyFmt=%d\n", \
402 iP->hwndSelf, iP->ntmHeight, iP->iconSize.cx, iP->iconSize.cy, \
403 iP->iconSpacing.cx, iP->iconSpacing.cy, iP->notifyFormat); \
404 TRACE("hwndSelf=%p, rcList=%s\n", iP->hwndSelf, wine_dbgstr_rect(&iP->rcList)); \
405 } while(0)
406
407 static const WCHAR themeClass[] = {'L','i','s','t','V','i','e','w',0};
408
409 /*
410 * forward declarations
411 */
412 static BOOL LISTVIEW_GetItemT(const LISTVIEW_INFO *, LPLVITEMW, BOOL);
413 static void LISTVIEW_GetItemBox(const LISTVIEW_INFO *, INT, LPRECT);
414 static void LISTVIEW_GetItemOrigin(const LISTVIEW_INFO *, INT, LPPOINT);
415 static BOOL LISTVIEW_GetItemPosition(const LISTVIEW_INFO *, INT, LPPOINT);
416 static BOOL LISTVIEW_GetItemRect(const LISTVIEW_INFO *, INT, LPRECT);
417 static INT LISTVIEW_GetLabelWidth(const LISTVIEW_INFO *, INT);
418 static void LISTVIEW_GetOrigin(const LISTVIEW_INFO *, LPPOINT);
419 static BOOL LISTVIEW_GetViewRect(const LISTVIEW_INFO *, LPRECT);
420 static void LISTVIEW_SetGroupSelection(LISTVIEW_INFO *, INT);
421 static BOOL LISTVIEW_SetItemT(LISTVIEW_INFO *, LVITEMW *, BOOL);
422 static void LISTVIEW_UpdateScroll(const LISTVIEW_INFO *);
423 static void LISTVIEW_SetSelection(LISTVIEW_INFO *, INT);
424 static void LISTVIEW_UpdateSize(LISTVIEW_INFO *);
425 static HWND LISTVIEW_EditLabelT(LISTVIEW_INFO *, INT, BOOL);
426 static LRESULT LISTVIEW_Command(const LISTVIEW_INFO *, WPARAM, LPARAM);
427 static BOOL LISTVIEW_SortItems(LISTVIEW_INFO *, PFNLVCOMPARE, LPARAM);
428 static INT LISTVIEW_GetStringWidthT(const LISTVIEW_INFO *, LPCWSTR, BOOL);
429 static BOOL LISTVIEW_KeySelection(LISTVIEW_INFO *, INT);
430 static UINT LISTVIEW_GetItemState(const LISTVIEW_INFO *, INT, UINT);
431 static BOOL LISTVIEW_SetItemState(LISTVIEW_INFO *, INT, const LVITEMW *);
432 static LRESULT LISTVIEW_VScroll(LISTVIEW_INFO *, INT, INT, HWND);
433 static LRESULT LISTVIEW_HScroll(LISTVIEW_INFO *, INT, INT, HWND);
434 static INT LISTVIEW_GetTopIndex(const LISTVIEW_INFO *);
435 static BOOL LISTVIEW_EnsureVisible(LISTVIEW_INFO *, INT, BOOL);
436 static HWND CreateEditLabelT(LISTVIEW_INFO *, LPCWSTR, DWORD, INT, INT, INT, INT, BOOL);
437 static HIMAGELIST LISTVIEW_SetImageList(LISTVIEW_INFO *, INT, HIMAGELIST);
438 static INT LISTVIEW_HitTest(const LISTVIEW_INFO *, LPLVHITTESTINFO, BOOL, BOOL);
439
440 /******** Text handling functions *************************************/
441
442 /* A text pointer is either NULL, LPSTR_TEXTCALLBACK, or points to a
443 * text string. The string may be ANSI or Unicode, in which case
444 * the boolean isW tells us the type of the string.
445 *
446 * The name of the function tell what type of strings it expects:
447 * W: Unicode, T: ANSI/Unicode - function of isW
448 */
449
450 static inline BOOL is_textW(LPCWSTR text)
451 {
452 return text != NULL && text != LPSTR_TEXTCALLBACKW;
453 }
454
455 static inline BOOL is_textT(LPCWSTR text, BOOL isW)
456 {
457 /* we can ignore isW since LPSTR_TEXTCALLBACKW == LPSTR_TEXTCALLBACKA */
458 return is_textW(text);
459 }
460
461 static inline int textlenT(LPCWSTR text, BOOL isW)
462 {
463 return !is_textT(text, isW) ? 0 :
464 isW ? lstrlenW(text) : lstrlenA((LPCSTR)text);
465 }
466
467 static inline void textcpynT(LPWSTR dest, BOOL isDestW, LPCWSTR src, BOOL isSrcW, INT max)
468 {
469 if (isDestW)
470 if (isSrcW) lstrcpynW(dest, src, max);
471 else MultiByteToWideChar(CP_ACP, 0, (LPCSTR)src, -1, dest, max);
472 else
473 if (isSrcW) WideCharToMultiByte(CP_ACP, 0, src, -1, (LPSTR)dest, max, NULL, NULL);
474 else lstrcpynA((LPSTR)dest, (LPCSTR)src, max);
475 }
476
477 static inline LPWSTR textdupTtoW(LPCWSTR text, BOOL isW)
478 {
479 LPWSTR wstr = (LPWSTR)text;
480
481 if (!isW && is_textT(text, isW))
482 {
483 INT len = MultiByteToWideChar(CP_ACP, 0, (LPCSTR)text, -1, NULL, 0);
484 wstr = Alloc(len * sizeof(WCHAR));
485 if (wstr) MultiByteToWideChar(CP_ACP, 0, (LPCSTR)text, -1, wstr, len);
486 }
487 TRACE(" wstr=%s\n", text == LPSTR_TEXTCALLBACKW ? "(callback)" : debugstr_w(wstr));
488 return wstr;
489 }
490
491 static inline void textfreeT(LPWSTR wstr, BOOL isW)
492 {
493 if (!isW && is_textT(wstr, isW)) Free (wstr);
494 }
495
496 /*
497 * dest is a pointer to a Unicode string
498 * src is a pointer to a string (Unicode if isW, ANSI if !isW)
499 */
500 static BOOL textsetptrT(LPWSTR *dest, LPCWSTR src, BOOL isW)
501 {
502 BOOL bResult = TRUE;
503
504 if (src == LPSTR_TEXTCALLBACKW)
505 {
506 if (is_textW(*dest)) Free(*dest);
507 *dest = LPSTR_TEXTCALLBACKW;
508 }
509 else
510 {
511 LPWSTR pszText = textdupTtoW(src, isW);
512 if (*dest == LPSTR_TEXTCALLBACKW) *dest = NULL;
513 bResult = Str_SetPtrW(dest, pszText);
514 textfreeT(pszText, isW);
515 }
516 return bResult;
517 }
518
519 /*
520 * compares a Unicode to a Unicode/ANSI text string
521 */
522 static inline int textcmpWT(LPCWSTR aw, LPCWSTR bt, BOOL isW)
523 {
524 if (!aw) return bt ? -1 : 0;
525 if (!bt) return aw ? 1 : 0;
526 if (aw == LPSTR_TEXTCALLBACKW)
527 return bt == LPSTR_TEXTCALLBACKW ? 0 : -1;
528 if (bt != LPSTR_TEXTCALLBACKW)
529 {
530 LPWSTR bw = textdupTtoW(bt, isW);
531 int r = bw ? lstrcmpW(aw, bw) : 1;
532 textfreeT(bw, isW);
533 return r;
534 }
535
536 return 1;
537 }
538
539 static inline int lstrncmpiW(LPCWSTR s1, LPCWSTR s2, int n)
540 {
541 int res;
542
543 n = min(min(n, lstrlenW(s1)), lstrlenW(s2));
544 res = CompareStringW(LOCALE_USER_DEFAULT, NORM_IGNORECASE, s1, n, s2, n);
545 return res ? res - sizeof(WCHAR) : res;
546 }
547
548 /******** Debugging functions *****************************************/
549
550 static inline LPCSTR debugtext_t(LPCWSTR text, BOOL isW)
551 {
552 if (text == LPSTR_TEXTCALLBACKW) return "(callback)";
553 return isW ? debugstr_w(text) : debugstr_a((LPCSTR)text);
554 }
555
556 static inline LPCSTR debugtext_tn(LPCWSTR text, BOOL isW, INT n)
557 {
558 if (text == LPSTR_TEXTCALLBACKW) return "(callback)";
559 n = min(textlenT(text, isW), n);
560 return isW ? debugstr_wn(text, n) : debugstr_an((LPCSTR)text, n);
561 }
562
563 static char* debug_getbuf(void)
564 {
565 static int index = 0;
566 static char buffers[DEBUG_BUFFERS][DEBUG_BUFFER_SIZE];
567 return buffers[index++ % DEBUG_BUFFERS];
568 }
569
570 static inline const char* debugrange(const RANGE *lprng)
571 {
572 if (!lprng) return "(null)";
573 return wine_dbg_sprintf("[%d, %d]", lprng->lower, lprng->upper);
574 }
575
576 static const char* debugscrollinfo(const SCROLLINFO *pScrollInfo)
577 {
578 char* buf = debug_getbuf(), *text = buf;
579 int len, size = DEBUG_BUFFER_SIZE;
580
581 if (pScrollInfo == NULL) return "(null)";
582 len = snprintf(buf, size, "{cbSize=%d, ", pScrollInfo->cbSize);
583 if (len == -1) goto end; buf += len; size -= len;
584 if (pScrollInfo->fMask & SIF_RANGE)
585 len = snprintf(buf, size, "nMin=%d, nMax=%d, ", pScrollInfo->nMin, pScrollInfo->nMax);
586 else len = 0;
587 if (len == -1) goto end; buf += len; size -= len;
588 if (pScrollInfo->fMask & SIF_PAGE)
589 len = snprintf(buf, size, "nPage=%u, ", pScrollInfo->nPage);
590 else len = 0;
591 if (len == -1) goto end; buf += len; size -= len;
592 if (pScrollInfo->fMask & SIF_POS)
593 len = snprintf(buf, size, "nPos=%d, ", pScrollInfo->nPos);
594 else len = 0;
595 if (len == -1) goto end; buf += len; size -= len;
596 if (pScrollInfo->fMask & SIF_TRACKPOS)
597 len = snprintf(buf, size, "nTrackPos=%d, ", pScrollInfo->nTrackPos);
598 else len = 0;
599 if (len == -1) goto end; buf += len; size -= len;
600 goto undo;
601 end:
602 buf = text + strlen(text);
603 undo:
604 if (buf - text > 2) { buf[-2] = '}'; buf[-1] = 0; }
605 return text;
606 }
607
608 static const char* debugnmlistview(const NMLISTVIEW *plvnm)
609 {
610 if (!plvnm) return "(null)";
611 return wine_dbg_sprintf("iItem=%d, iSubItem=%d, uNewState=0x%x,"
612 " uOldState=0x%x, uChanged=0x%x, ptAction=%s, lParam=%ld",
613 plvnm->iItem, plvnm->iSubItem, plvnm->uNewState, plvnm->uOldState,
614 plvnm->uChanged, wine_dbgstr_point(&plvnm->ptAction), plvnm->lParam);
615 }
616
617 static const char* debuglvitem_t(const LVITEMW *lpLVItem, BOOL isW)
618 {
619 char* buf = debug_getbuf(), *text = buf;
620 int len, size = DEBUG_BUFFER_SIZE;
621
622 if (lpLVItem == NULL) return "(null)";
623 len = snprintf(buf, size, "{iItem=%d, iSubItem=%d, ", lpLVItem->iItem, lpLVItem->iSubItem);
624 if (len == -1) goto end; buf += len; size -= len;
625 if (lpLVItem->mask & LVIF_STATE)
626 len = snprintf(buf, size, "state=%x, stateMask=%x, ", lpLVItem->state, lpLVItem->stateMask);
627 else len = 0;
628 if (len == -1) goto end; buf += len; size -= len;
629 if (lpLVItem->mask & LVIF_TEXT)
630 len = snprintf(buf, size, "pszText=%s, cchTextMax=%d, ", debugtext_tn(lpLVItem->pszText, isW, 80), lpLVItem->cchTextMax);
631 else len = 0;
632 if (len == -1) goto end; buf += len; size -= len;
633 if (lpLVItem->mask & LVIF_IMAGE)
634 len = snprintf(buf, size, "iImage=%d, ", lpLVItem->iImage);
635 else len = 0;
636 if (len == -1) goto end; buf += len; size -= len;
637 if (lpLVItem->mask & LVIF_PARAM)
638 len = snprintf(buf, size, "lParam=%lx, ", lpLVItem->lParam);
639 else len = 0;
640 if (len == -1) goto end; buf += len; size -= len;
641 if (lpLVItem->mask & LVIF_INDENT)
642 len = snprintf(buf, size, "iIndent=%d, ", lpLVItem->iIndent);
643 else len = 0;
644 if (len == -1) goto end; buf += len; size -= len;
645 goto undo;
646 end:
647 buf = text + strlen(text);
648 undo:
649 if (buf - text > 2) { buf[-2] = '}'; buf[-1] = 0; }
650 return text;
651 }
652
653 static const char* debuglvcolumn_t(const LVCOLUMNW *lpColumn, BOOL isW)
654 {
655 char* buf = debug_getbuf(), *text = buf;
656 int len, size = DEBUG_BUFFER_SIZE;
657
658 if (lpColumn == NULL) return "(null)";
659 len = snprintf(buf, size, "{");
660 if (len == -1) goto end; buf += len; size -= len;
661 if (lpColumn->mask & LVCF_SUBITEM)
662 len = snprintf(buf, size, "iSubItem=%d, ", lpColumn->iSubItem);
663 else len = 0;
664 if (len == -1) goto end; buf += len; size -= len;
665 if (lpColumn->mask & LVCF_FMT)
666 len = snprintf(buf, size, "fmt=%x, ", lpColumn->fmt);
667 else len = 0;
668 if (len == -1) goto end; buf += len; size -= len;
669 if (lpColumn->mask & LVCF_WIDTH)
670 len = snprintf(buf, size, "cx=%d, ", lpColumn->cx);
671 else len = 0;
672 if (len == -1) goto end; buf += len; size -= len;
673 if (lpColumn->mask & LVCF_TEXT)
674 len = snprintf(buf, size, "pszText=%s, cchTextMax=%d, ", debugtext_tn(lpColumn->pszText, isW, 80), lpColumn->cchTextMax);
675 else len = 0;
676 if (len == -1) goto end; buf += len; size -= len;
677 if (lpColumn->mask & LVCF_IMAGE)
678 len = snprintf(buf, size, "iImage=%d, ", lpColumn->iImage);
679 else len = 0;
680 if (len == -1) goto end; buf += len; size -= len;
681 if (lpColumn->mask & LVCF_ORDER)
682 len = snprintf(buf, size, "iOrder=%d, ", lpColumn->iOrder);
683 else len = 0;
684 if (len == -1) goto end; buf += len; size -= len;
685 goto undo;
686 end:
687 buf = text + strlen(text);
688 undo:
689 if (buf - text > 2) { buf[-2] = '}'; buf[-1] = 0; }
690 return text;
691 }
692
693 static const char* debuglvhittestinfo(const LVHITTESTINFO *lpht)
694 {
695 if (!lpht) return "(null)";
696
697 return wine_dbg_sprintf("{pt=%s, flags=0x%x, iItem=%d, iSubItem=%d}",
698 wine_dbgstr_point(&lpht->pt), lpht->flags, lpht->iItem, lpht->iSubItem);
699 }
700
701 /* Return the corresponding text for a given scroll value */
702 static inline LPCSTR debugscrollcode(int nScrollCode)
703 {
704 switch(nScrollCode)
705 {
706 case SB_LINELEFT: return "SB_LINELEFT";
707 case SB_LINERIGHT: return "SB_LINERIGHT";
708 case SB_PAGELEFT: return "SB_PAGELEFT";
709 case SB_PAGERIGHT: return "SB_PAGERIGHT";
710 case SB_THUMBPOSITION: return "SB_THUMBPOSITION";
711 case SB_THUMBTRACK: return "SB_THUMBTRACK";
712 case SB_ENDSCROLL: return "SB_ENDSCROLL";
713 case SB_INTERNAL: return "SB_INTERNAL";
714 default: return "unknown";
715 }
716 }
717
718
719 /******** Notification functions i************************************/
720
721 static LRESULT notify_forward_header(const LISTVIEW_INFO *infoPtr, const NMHEADERW *lpnmh)
722 {
723 return SendMessageW(infoPtr->hwndNotify, WM_NOTIFY,
724 (WPARAM)lpnmh->hdr.idFrom, (LPARAM)lpnmh);
725 }
726
727 static LRESULT notify_hdr(const LISTVIEW_INFO *infoPtr, INT code, LPNMHDR pnmh)
728 {
729 LRESULT result;
730
731 TRACE("(code=%d)\n", code);
732
733 pnmh->hwndFrom = infoPtr->hwndSelf;
734 pnmh->idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
735 pnmh->code = code;
736 result = SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, pnmh->idFrom, (LPARAM)pnmh);
737
738 TRACE(" <= %ld\n", result);
739
740 return result;
741 }
742
743 static inline BOOL notify(const LISTVIEW_INFO *infoPtr, INT code)
744 {
745 NMHDR nmh;
746 HWND hwnd = infoPtr->hwndSelf;
747 notify_hdr(infoPtr, code, &nmh);
748 return IsWindow(hwnd);
749 }
750
751 static inline void notify_itemactivate(const LISTVIEW_INFO *infoPtr, const LVHITTESTINFO *htInfo)
752 {
753 NMITEMACTIVATE nmia;
754 LVITEMW item;
755
756 if (htInfo) {
757 nmia.uNewState = 0;
758 nmia.uOldState = 0;
759 nmia.uChanged = 0;
760 nmia.uKeyFlags = 0;
761
762 item.mask = LVIF_PARAM|LVIF_STATE;
763 item.iItem = htInfo->iItem;
764 item.iSubItem = 0;
765 if (LISTVIEW_GetItemT(infoPtr, &item, TRUE)) {
766 nmia.lParam = item.lParam;
767 nmia.uOldState = item.state;
768 nmia.uNewState = item.state | LVIS_ACTIVATING;
769 nmia.uChanged = LVIF_STATE;
770 }
771
772 nmia.iItem = htInfo->iItem;
773 nmia.iSubItem = htInfo->iSubItem;
774 nmia.ptAction = htInfo->pt;
775
776 if (GetKeyState(VK_SHIFT) & 0x8000) nmia.uKeyFlags |= LVKF_SHIFT;
777 if (GetKeyState(VK_CONTROL) & 0x8000) nmia.uKeyFlags |= LVKF_CONTROL;
778 if (GetKeyState(VK_MENU) & 0x8000) nmia.uKeyFlags |= LVKF_ALT;
779 }
780 notify_hdr(infoPtr, LVN_ITEMACTIVATE, (LPNMHDR)&nmia);
781 }
782
783 static inline LRESULT notify_listview(const LISTVIEW_INFO *infoPtr, INT code, LPNMLISTVIEW plvnm)
784 {
785 TRACE("(code=%d, plvnm=%s)\n", code, debugnmlistview(plvnm));
786 return notify_hdr(infoPtr, code, (LPNMHDR)plvnm);
787 }
788
789 static BOOL notify_click(const LISTVIEW_INFO *infoPtr, INT code, const LVHITTESTINFO *lvht)
790 {
791 NMLISTVIEW nmlv;
792 LVITEMW item;
793 HWND hwnd = infoPtr->hwndSelf;
794
795 TRACE("code=%d, lvht=%s\n", code, debuglvhittestinfo(lvht));
796 ZeroMemory(&nmlv, sizeof(nmlv));
797 nmlv.iItem = lvht->iItem;
798 nmlv.iSubItem = lvht->iSubItem;
799 nmlv.ptAction = lvht->pt;
800 item.mask = LVIF_PARAM;
801 item.iItem = lvht->iItem;
802 item.iSubItem = 0;
803 if (LISTVIEW_GetItemT(infoPtr, &item, TRUE)) nmlv.lParam = item.lParam;
804 notify_listview(infoPtr, code, &nmlv);
805 return IsWindow(hwnd);
806 }
807
808 static BOOL notify_deleteitem(const LISTVIEW_INFO *infoPtr, INT nItem)
809 {
810 NMLISTVIEW nmlv;
811 LVITEMW item;
812 HWND hwnd = infoPtr->hwndSelf;
813
814 ZeroMemory(&nmlv, sizeof (NMLISTVIEW));
815 nmlv.iItem = nItem;
816 item.mask = LVIF_PARAM;
817 item.iItem = nItem;
818 item.iSubItem = 0;
819 if (LISTVIEW_GetItemT(infoPtr, &item, TRUE)) nmlv.lParam = item.lParam;
820 notify_listview(infoPtr, LVN_DELETEITEM, &nmlv);
821 return IsWindow(hwnd);
822 }
823
824 static int get_ansi_notification(UINT unicodeNotificationCode)
825 {
826 switch (unicodeNotificationCode)
827 {
828 case LVN_BEGINLABELEDITW: return LVN_BEGINLABELEDITA;
829 case LVN_ENDLABELEDITW: return LVN_ENDLABELEDITA;
830 case LVN_GETDISPINFOW: return LVN_GETDISPINFOA;
831 case LVN_SETDISPINFOW: return LVN_SETDISPINFOA;
832 case LVN_ODFINDITEMW: return LVN_ODFINDITEMA;
833 case LVN_GETINFOTIPW: return LVN_GETINFOTIPA;
834 }
835 ERR("unknown notification %x\n", unicodeNotificationCode);
836 assert(FALSE);
837 return 0;
838 }
839
840 /*
841 Send notification. depends on dispinfoW having same
842 structure as dispinfoA.
843 infoPtr : listview struct
844 notificationCode : *Unicode* notification code
845 pdi : dispinfo structure (can be unicode or ansi)
846 isW : TRUE if dispinfo is Unicode
847 */
848 static BOOL notify_dispinfoT(const LISTVIEW_INFO *infoPtr, UINT notificationCode, LPNMLVDISPINFOW pdi, BOOL isW)
849 {
850 BOOL bResult = FALSE;
851 BOOL convertToAnsi = FALSE, convertToUnicode = FALSE;
852 INT cchTempBufMax = 0, savCchTextMax = 0;
853 UINT realNotifCode;
854 LPWSTR pszTempBuf = NULL, savPszText = NULL;
855
856 if ((pdi->item.mask & LVIF_TEXT) && is_textT(pdi->item.pszText, isW))
857 {
858 convertToAnsi = (isW && infoPtr->notifyFormat == NFR_ANSI);
859 convertToUnicode = (!isW && infoPtr->notifyFormat == NFR_UNICODE);
860 }
861
862 if (convertToAnsi || convertToUnicode)
863 {
864 if (notificationCode != LVN_GETDISPINFOW)
865 {
866 cchTempBufMax = convertToUnicode ?
867 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pdi->item.pszText, -1, NULL, 0):
868 WideCharToMultiByte(CP_ACP, 0, pdi->item.pszText, -1, NULL, 0, NULL, NULL);
869 }
870 else
871 {
872 cchTempBufMax = pdi->item.cchTextMax;
873 *pdi->item.pszText = 0; /* make sure we don't process garbage */
874 }
875
876 pszTempBuf = Alloc( (convertToUnicode ? sizeof(WCHAR) : sizeof(CHAR)) * cchTempBufMax);
877 if (!pszTempBuf) return FALSE;
878
879 if (convertToUnicode)
880 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pdi->item.pszText, -1,
881 pszTempBuf, cchTempBufMax);
882 else
883 WideCharToMultiByte(CP_ACP, 0, pdi->item.pszText, -1, (LPSTR) pszTempBuf,
884 cchTempBufMax, NULL, NULL);
885
886 savCchTextMax = pdi->item.cchTextMax;
887 savPszText = pdi->item.pszText;
888 pdi->item.pszText = pszTempBuf;
889 pdi->item.cchTextMax = cchTempBufMax;
890 }
891
892 if (infoPtr->notifyFormat == NFR_ANSI)
893 realNotifCode = get_ansi_notification(notificationCode);
894 else
895 realNotifCode = notificationCode;
896 TRACE(" pdi->item=%s\n", debuglvitem_t(&pdi->item, infoPtr->notifyFormat != NFR_ANSI));
897 bResult = notify_hdr(infoPtr, realNotifCode, &pdi->hdr);
898
899 if (convertToUnicode || convertToAnsi)
900 {
901 if (convertToUnicode) /* note : pointer can be changed by app ! */
902 WideCharToMultiByte(CP_ACP, 0, pdi->item.pszText, -1, (LPSTR) savPszText,
903 savCchTextMax, NULL, NULL);
904 else
905 MultiByteToWideChar(CP_ACP, 0, (LPSTR) pdi->item.pszText, -1,
906 savPszText, savCchTextMax);
907 pdi->item.pszText = savPszText; /* restores our buffer */
908 pdi->item.cchTextMax = savCchTextMax;
909 Free (pszTempBuf);
910 }
911 return bResult;
912 }
913
914 static void customdraw_fill(NMLVCUSTOMDRAW *lpnmlvcd, const LISTVIEW_INFO *infoPtr, HDC hdc,
915 const RECT *rcBounds, const LVITEMW *lplvItem)
916 {
917 ZeroMemory(lpnmlvcd, sizeof(NMLVCUSTOMDRAW));
918 lpnmlvcd->nmcd.hdc = hdc;
919 lpnmlvcd->nmcd.rc = *rcBounds;
920 lpnmlvcd->clrTextBk = infoPtr->clrTextBk;
921 lpnmlvcd->clrText = infoPtr->clrText;
922 if (!lplvItem) return;
923 lpnmlvcd->nmcd.dwItemSpec = lplvItem->iItem + 1;
924 lpnmlvcd->iSubItem = lplvItem->iSubItem;
925 if (lplvItem->state & LVIS_SELECTED) lpnmlvcd->nmcd.uItemState |= CDIS_SELECTED;
926 if (lplvItem->state & LVIS_FOCUSED) lpnmlvcd->nmcd.uItemState |= CDIS_FOCUS;
927 if (lplvItem->iItem == infoPtr->nHotItem) lpnmlvcd->nmcd.uItemState |= CDIS_HOT;
928 lpnmlvcd->nmcd.lItemlParam = lplvItem->lParam;
929 }
930
931 static inline DWORD notify_customdraw (const LISTVIEW_INFO *infoPtr, DWORD dwDrawStage, NMLVCUSTOMDRAW *lpnmlvcd)
932 {
933 BOOL isForItem = (lpnmlvcd->nmcd.dwItemSpec != 0);
934 DWORD result;
935
936 lpnmlvcd->nmcd.dwDrawStage = dwDrawStage;
937 if (isForItem) lpnmlvcd->nmcd.dwDrawStage |= CDDS_ITEM;
938 if (lpnmlvcd->iSubItem) lpnmlvcd->nmcd.dwDrawStage |= CDDS_SUBITEM;
939 if (isForItem) lpnmlvcd->nmcd.dwItemSpec--;
940 result = notify_hdr(infoPtr, NM_CUSTOMDRAW, &lpnmlvcd->nmcd.hdr);
941 if (isForItem) lpnmlvcd->nmcd.dwItemSpec++;
942 return result;
943 }
944
945 static void prepaint_setup (const LISTVIEW_INFO *infoPtr, HDC hdc, NMLVCUSTOMDRAW *lpnmlvcd, BOOL SubItem)
946 {
947 if (lpnmlvcd->clrTextBk == CLR_DEFAULT)
948 lpnmlvcd->clrTextBk = comctl32_color.clrWindow;
949 if (lpnmlvcd->clrText == CLR_DEFAULT)
950 lpnmlvcd->clrText = comctl32_color.clrWindowText;
951
952 /* apparently, for selected items, we have to override the returned values */
953 if (!SubItem)
954 {
955 if (lpnmlvcd->nmcd.uItemState & CDIS_SELECTED)
956 {
957 if (infoPtr->bFocus)
958 {
959 lpnmlvcd->clrTextBk = comctl32_color.clrHighlight;
960 lpnmlvcd->clrText = comctl32_color.clrHighlightText;
961 }
962 else if (infoPtr->dwStyle & LVS_SHOWSELALWAYS)
963 {
964 lpnmlvcd->clrTextBk = comctl32_color.clr3dFace;
965 lpnmlvcd->clrText = comctl32_color.clrBtnText;
966 }
967 }
968 }
969
970 /* Set the text attributes */
971 if (lpnmlvcd->clrTextBk != CLR_NONE)
972 {
973 SetBkMode(hdc, OPAQUE);
974 SetBkColor(hdc,lpnmlvcd->clrTextBk);
975 }
976 else
977 SetBkMode(hdc, TRANSPARENT);
978 SetTextColor(hdc, lpnmlvcd->clrText);
979 }
980
981 static inline DWORD notify_postpaint (const LISTVIEW_INFO *infoPtr, NMLVCUSTOMDRAW *lpnmlvcd)
982 {
983 return notify_customdraw(infoPtr, CDDS_POSTPAINT, lpnmlvcd);
984 }
985
986 /******** Item iterator functions **********************************/
987
988 static RANGES ranges_create(int count);
989 static void ranges_destroy(RANGES ranges);
990 static BOOL ranges_add(RANGES ranges, RANGE range);
991 static BOOL ranges_del(RANGES ranges, RANGE range);
992 static void ranges_dump(RANGES ranges);
993
994 static inline BOOL ranges_additem(RANGES ranges, INT nItem)
995 {
996 RANGE range = { nItem, nItem + 1 };
997
998 return ranges_add(ranges, range);
999 }
1000
1001 static inline BOOL ranges_delitem(RANGES ranges, INT nItem)
1002 {
1003 RANGE range = { nItem, nItem + 1 };
1004
1005 return ranges_del(ranges, range);
1006 }
1007
1008 /***
1009 * ITERATOR DOCUMENTATION
1010 *
1011 * The iterator functions allow for easy, and convenient iteration
1012 * over items of interest in the list. Typically, you create a
1013 * iterator, use it, and destroy it, as such:
1014 * ITERATOR i;
1015 *
1016 * iterator_xxxitems(&i, ...);
1017 * while (iterator_{prev,next}(&i)
1018 * {
1019 * //code which uses i.nItem
1020 * }
1021 * iterator_destroy(&i);
1022 *
1023 * where xxx is either: framed, or visible.
1024 * Note that it is important that the code destroys the iterator
1025 * after it's done with it, as the creation of the iterator may
1026 * allocate memory, which thus needs to be freed.
1027 *
1028 * You can iterate both forwards, and backwards through the list,
1029 * by using iterator_next or iterator_prev respectively.
1030 *
1031 * Lower numbered items are draw on top of higher number items in
1032 * LVS_ICON, and LVS_SMALLICON (which are the only modes where
1033 * items may overlap). So, to test items, you should use
1034 * iterator_next
1035 * which lists the items top to bottom (in Z-order).
1036 * For drawing items, you should use
1037 * iterator_prev
1038 * which lists the items bottom to top (in Z-order).
1039 * If you keep iterating over the items after the end-of-items
1040 * marker (-1) is returned, the iterator will start from the
1041 * beginning. Typically, you don't need to test for -1,
1042 * because iterator_{next,prev} will return TRUE if more items
1043 * are to be iterated over, or FALSE otherwise.
1044 *
1045 * Note: the iterator is defined to be bidirectional. That is,
1046 * any number of prev followed by any number of next, or
1047 * five versa, should leave the iterator at the same item:
1048 * prev * n, next * n = next * n, prev * n
1049 *
1050 * The iterator has a notion of an out-of-order, special item,
1051 * which sits at the start of the list. This is used in
1052 * LVS_ICON, and LVS_SMALLICON mode to handle the focused item,
1053 * which needs to be first, as it may overlap other items.
1054 *
1055 * The code is a bit messy because we have:
1056 * - a special item to deal with
1057 * - simple range, or composite range
1058 * - empty range.
1059 * If you find bugs, or want to add features, please make sure you
1060 * always check/modify *both* iterator_prev, and iterator_next.
1061 */
1062
1063 /****
1064 * This function iterates through the items in increasing order,
1065 * but prefixed by the special item, then -1. That is:
1066 * special, 1, 2, 3, ..., n, -1.
1067 * Each item is listed only once.
1068 */
1069 static inline BOOL iterator_next(ITERATOR* i)
1070 {
1071 if (i->nItem == -1)
1072 {
1073 i->nItem = i->nSpecial;
1074 if (i->nItem != -1) return TRUE;
1075 }
1076 if (i->nItem == i->nSpecial)
1077 {
1078 if (i->ranges) i->index = 0;
1079 goto pickarange;
1080 }
1081
1082 i->nItem++;
1083 testitem:
1084 if (i->nItem == i->nSpecial) i->nItem++;
1085 if (i->nItem < i->range.upper) return TRUE;
1086
1087 pickarange:
1088 if (i->ranges)
1089 {
1090 if (i->index < DPA_GetPtrCount(i->ranges->hdpa))
1091 i->range = *(RANGE*)DPA_GetPtr(i->ranges->hdpa, i->index++);
1092 else goto end;
1093 }
1094 else if (i->nItem >= i->range.upper) goto end;
1095
1096 i->nItem = i->range.lower;
1097 if (i->nItem >= 0) goto testitem;
1098 end:
1099 i->nItem = -1;
1100 return FALSE;
1101 }
1102
1103 /****
1104 * This function iterates through the items in decreasing order,
1105 * followed by the special item, then -1. That is:
1106 * n, n-1, ..., 3, 2, 1, special, -1.
1107 * Each item is listed only once.
1108 */
1109 static inline BOOL iterator_prev(ITERATOR* i)
1110 {
1111 BOOL start = FALSE;
1112
1113 if (i->nItem == -1)
1114 {
1115 start = TRUE;
1116 if (i->ranges) i->index = DPA_GetPtrCount(i->ranges->hdpa);
1117 goto pickarange;
1118 }
1119 if (i->nItem == i->nSpecial)
1120 {
1121 i->nItem = -1;
1122 return FALSE;
1123 }
1124
1125 testitem:
1126 i->nItem--;
1127 if (i->nItem == i->nSpecial) i->nItem--;
1128 if (i->nItem >= i->range.lower) return TRUE;
1129
1130 pickarange:
1131 if (i->ranges)
1132 {
1133 if (i->index > 0)
1134 i->range = *(RANGE*)DPA_GetPtr(i->ranges->hdpa, --i->index);
1135 else goto end;
1136 }
1137 else if (!start && i->nItem < i->range.lower) goto end;
1138
1139 i->nItem = i->range.upper;
1140 if (i->nItem > 0) goto testitem;
1141 end:
1142 return (i->nItem = i->nSpecial) != -1;
1143 }
1144
1145 static RANGE iterator_range(const ITERATOR *i)
1146 {
1147 RANGE range;
1148
1149 if (!i->ranges) return i->range;
1150
1151 if (DPA_GetPtrCount(i->ranges->hdpa) > 0)
1152 {
1153 range.lower = (*(RANGE*)DPA_GetPtr(i->ranges->hdpa, 0)).lower;
1154 range.upper = (*(RANGE*)DPA_GetPtr(i->ranges->hdpa, DPA_GetPtrCount(i->ranges->hdpa) - 1)).upper;
1155 }
1156 else range.lower = range.upper = 0;
1157
1158 return range;
1159 }
1160
1161 /***
1162 * Releases resources associated with this ierator.
1163 */
1164 static inline void iterator_destroy(const ITERATOR *i)
1165 {
1166 ranges_destroy(i->ranges);
1167 }
1168
1169 /***
1170 * Create an empty iterator.
1171 */
1172 static inline BOOL iterator_empty(ITERATOR* i)
1173 {
1174 ZeroMemory(i, sizeof(*i));
1175 i->nItem = i->nSpecial = i->range.lower = i->range.upper = -1;
1176 return TRUE;
1177 }
1178
1179 /***
1180 * Create an iterator over a range.
1181 */
1182 static inline BOOL iterator_rangeitems(ITERATOR* i, RANGE range)
1183 {
1184 iterator_empty(i);
1185 i->range = range;
1186 return TRUE;
1187 }
1188
1189 /***
1190 * Create an iterator over a bunch of ranges.
1191 * Please note that the iterator will take ownership of the ranges,
1192 * and will free them upon destruction.
1193 */
1194 static inline BOOL iterator_rangesitems(ITERATOR* i, RANGES ranges)
1195 {
1196 iterator_empty(i);
1197 i->ranges = ranges;
1198 return TRUE;
1199 }
1200
1201 /***
1202 * Creates an iterator over the items which intersect lprc.
1203 */
1204 static BOOL iterator_frameditems(ITERATOR* i, const LISTVIEW_INFO* infoPtr, const RECT *lprc)
1205 {
1206 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
1207 RECT frame = *lprc, rcItem, rcTemp;
1208 POINT Origin;
1209
1210 /* in case we fail, we want to return an empty iterator */
1211 if (!iterator_empty(i)) return FALSE;
1212
1213 LISTVIEW_GetOrigin(infoPtr, &Origin);
1214
1215 TRACE("(lprc=%s)\n", wine_dbgstr_rect(lprc));
1216 OffsetRect(&frame, -Origin.x, -Origin.y);
1217
1218 if (uView == LVS_ICON || uView == LVS_SMALLICON)
1219 {
1220 INT nItem;
1221
1222 if (uView == LVS_ICON && infoPtr->nFocusedItem != -1)
1223 {
1224 LISTVIEW_GetItemBox(infoPtr, infoPtr->nFocusedItem, &rcItem);
1225 if (IntersectRect(&rcTemp, &rcItem, lprc))
1226 i->nSpecial = infoPtr->nFocusedItem;
1227 }
1228 if (!(iterator_rangesitems(i, ranges_create(50)))) return FALSE;
1229 /* to do better here, we need to have PosX, and PosY sorted */
1230 TRACE("building icon ranges:\n");
1231 for (nItem = 0; nItem < infoPtr->nItemCount; nItem++)
1232 {
1233 rcItem.left = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosX, nItem);
1234 rcItem.top = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosY, nItem);
1235 rcItem.right = rcItem.left + infoPtr->nItemWidth;
1236 rcItem.bottom = rcItem.top + infoPtr->nItemHeight;
1237 if (IntersectRect(&rcTemp, &rcItem, &frame))
1238 ranges_additem(i->ranges, nItem);
1239 }
1240 return TRUE;
1241 }
1242 else if (uView == LVS_REPORT)
1243 {
1244 RANGE range;
1245
1246 if (frame.left >= infoPtr->nItemWidth) return TRUE;
1247 if (frame.top >= infoPtr->nItemHeight * infoPtr->nItemCount) return TRUE;
1248
1249 range.lower = max(frame.top / infoPtr->nItemHeight, 0);
1250 range.upper = min((frame.bottom - 1) / infoPtr->nItemHeight, infoPtr->nItemCount - 1) + 1;
1251 if (range.upper <= range.lower) return TRUE;
1252 if (!iterator_rangeitems(i, range)) return FALSE;
1253 TRACE(" report=%s\n", debugrange(&i->range));
1254 }
1255 else
1256 {
1257 INT nPerCol = max((infoPtr->rcList.bottom - infoPtr->rcList.top) / infoPtr->nItemHeight, 1);
1258 INT nFirstRow = max(frame.top / infoPtr->nItemHeight, 0);
1259 INT nLastRow = min((frame.bottom - 1) / infoPtr->nItemHeight, nPerCol - 1);
1260 INT nFirstCol = max(frame.left / infoPtr->nItemWidth, 0);
1261 INT nLastCol = min((frame.right - 1) / infoPtr->nItemWidth, (infoPtr->nItemCount + nPerCol - 1) / nPerCol);
1262 INT lower = nFirstCol * nPerCol + nFirstRow;
1263 RANGE item_range;
1264 INT nCol;
1265
1266 TRACE("nPerCol=%d, nFirstRow=%d, nLastRow=%d, nFirstCol=%d, nLastCol=%d, lower=%d\n",
1267 nPerCol, nFirstRow, nLastRow, nFirstCol, nLastCol, lower);
1268
1269 if (nLastCol < nFirstCol || nLastRow < nFirstRow) return TRUE;
1270
1271 if (!(iterator_rangesitems(i, ranges_create(nLastCol - nFirstCol + 1)))) return FALSE;
1272 TRACE("building list ranges:\n");
1273 for (nCol = nFirstCol; nCol <= nLastCol; nCol++)
1274 {
1275 item_range.lower = nCol * nPerCol + nFirstRow;
1276 if(item_range.lower >= infoPtr->nItemCount) break;
1277 item_range.upper = min(nCol * nPerCol + nLastRow + 1, infoPtr->nItemCount);
1278 TRACE(" list=%s\n", debugrange(&item_range));
1279 ranges_add(i->ranges, item_range);
1280 }
1281 }
1282
1283 return TRUE;
1284 }
1285
1286 /***
1287 * Creates an iterator over the items which intersect the visible region of hdc.
1288 */
1289 static BOOL iterator_visibleitems(ITERATOR *i, const LISTVIEW_INFO *infoPtr, HDC hdc)
1290 {
1291 POINT Origin, Position;
1292 RECT rcItem, rcClip;
1293 INT rgntype;
1294
1295 rgntype = GetClipBox(hdc, &rcClip);
1296 if (rgntype == NULLREGION) return iterator_empty(i);
1297 if (!iterator_frameditems(i, infoPtr, &rcClip)) return FALSE;
1298 if (rgntype == SIMPLEREGION) return TRUE;
1299
1300 /* first deal with the special item */
1301 if (i->nSpecial != -1)
1302 {
1303 LISTVIEW_GetItemBox(infoPtr, i->nSpecial, &rcItem);
1304 if (!RectVisible(hdc, &rcItem)) i->nSpecial = -1;
1305 }
1306
1307 /* if we can't deal with the region, we'll just go with the simple range */
1308 LISTVIEW_GetOrigin(infoPtr, &Origin);
1309 TRACE("building visible range:\n");
1310 if (!i->ranges && i->range.lower < i->range.upper)
1311 {
1312 if (!(i->ranges = ranges_create(50))) return TRUE;
1313 if (!ranges_add(i->ranges, i->range))
1314 {
1315 ranges_destroy(i->ranges);
1316 i->ranges = 0;
1317 return TRUE;
1318 }
1319 }
1320
1321 /* now delete the invisible items from the list */
1322 while(iterator_next(i))
1323 {
1324 LISTVIEW_GetItemOrigin(infoPtr, i->nItem, &Position);
1325 rcItem.left = Position.x + Origin.x;
1326 rcItem.top = Position.y + Origin.y;
1327 rcItem.right = rcItem.left + infoPtr->nItemWidth;
1328 rcItem.bottom = rcItem.top + infoPtr->nItemHeight;
1329 if (!RectVisible(hdc, &rcItem))
1330 ranges_delitem(i->ranges, i->nItem);
1331 }
1332 /* the iterator should restart on the next iterator_next */
1333 TRACE("done\n");
1334
1335 return TRUE;
1336 }
1337
1338 /******** Misc helper functions ************************************/
1339
1340 static inline LRESULT CallWindowProcT(WNDPROC proc, HWND hwnd, UINT uMsg,
1341 WPARAM wParam, LPARAM lParam, BOOL isW)
1342 {
1343 if (isW) return CallWindowProcW(proc, hwnd, uMsg, wParam, lParam);
1344 else return CallWindowProcA(proc, hwnd, uMsg, wParam, lParam);
1345 }
1346
1347 static inline BOOL is_autoarrange(const LISTVIEW_INFO *infoPtr)
1348 {
1349 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
1350
1351 return ((infoPtr->dwStyle & LVS_AUTOARRANGE) || infoPtr->bAutoarrange) &&
1352 (uView == LVS_ICON || uView == LVS_SMALLICON);
1353 }
1354
1355 static void toggle_checkbox_state(LISTVIEW_INFO *infoPtr, INT nItem)
1356 {
1357 DWORD state = STATEIMAGEINDEX(LISTVIEW_GetItemState(infoPtr, nItem, LVIS_STATEIMAGEMASK));
1358 if(state == 1 || state == 2)
1359 {
1360 LVITEMW lvitem;
1361 state ^= 3;
1362 lvitem.state = INDEXTOSTATEIMAGEMASK(state);
1363 lvitem.stateMask = LVIS_STATEIMAGEMASK;
1364 LISTVIEW_SetItemState(infoPtr, nItem, &lvitem);
1365 }
1366 }
1367
1368 /******** Internal API functions ************************************/
1369
1370 static inline COLUMN_INFO * LISTVIEW_GetColumnInfo(const LISTVIEW_INFO *infoPtr, INT nSubItem)
1371 {
1372 static COLUMN_INFO mainItem;
1373
1374 if (nSubItem == 0 && DPA_GetPtrCount(infoPtr->hdpaColumns) == 0) return &mainItem;
1375 assert (nSubItem >= 0 && nSubItem < DPA_GetPtrCount(infoPtr->hdpaColumns));
1376 return (COLUMN_INFO *)DPA_GetPtr(infoPtr->hdpaColumns, nSubItem);
1377 }
1378
1379 static inline void LISTVIEW_GetHeaderRect(const LISTVIEW_INFO *infoPtr, INT nSubItem, LPRECT lprc)
1380 {
1381 *lprc = LISTVIEW_GetColumnInfo(infoPtr, nSubItem)->rcHeader;
1382 }
1383
1384 static inline BOOL LISTVIEW_GetItemW(const LISTVIEW_INFO *infoPtr, LPLVITEMW lpLVItem)
1385 {
1386 return LISTVIEW_GetItemT(infoPtr, lpLVItem, TRUE);
1387 }
1388
1389 /* Listview invalidation functions: use _only_ these functions to invalidate */
1390
1391 static inline BOOL is_redrawing(const LISTVIEW_INFO *infoPtr)
1392 {
1393 return infoPtr->bRedraw;
1394 }
1395
1396 static inline void LISTVIEW_InvalidateRect(const LISTVIEW_INFO *infoPtr, const RECT* rect)
1397 {
1398 if(!is_redrawing(infoPtr)) return;
1399 TRACE(" invalidating rect=%s\n", wine_dbgstr_rect(rect));
1400 InvalidateRect(infoPtr->hwndSelf, rect, TRUE);
1401 }
1402
1403 static inline void LISTVIEW_InvalidateItem(const LISTVIEW_INFO *infoPtr, INT nItem)
1404 {
1405 RECT rcBox;
1406
1407 if(!is_redrawing(infoPtr)) return;
1408 LISTVIEW_GetItemBox(infoPtr, nItem, &rcBox);
1409 LISTVIEW_InvalidateRect(infoPtr, &rcBox);
1410 }
1411
1412 static inline void LISTVIEW_InvalidateSubItem(const LISTVIEW_INFO *infoPtr, INT nItem, INT nSubItem)
1413 {
1414 POINT Origin, Position;
1415 RECT rcBox;
1416
1417 if(!is_redrawing(infoPtr)) return;
1418 assert ((infoPtr->dwStyle & LVS_TYPEMASK) == LVS_REPORT);
1419 LISTVIEW_GetOrigin(infoPtr, &Origin);
1420 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position);
1421 LISTVIEW_GetHeaderRect(infoPtr, nSubItem, &rcBox);
1422 rcBox.top = 0;
1423 rcBox.bottom = infoPtr->nItemHeight;
1424 OffsetRect(&rcBox, Origin.x + Position.x, Origin.y + Position.y);
1425 LISTVIEW_InvalidateRect(infoPtr, &rcBox);
1426 }
1427
1428 static inline void LISTVIEW_InvalidateList(const LISTVIEW_INFO *infoPtr)
1429 {
1430 LISTVIEW_InvalidateRect(infoPtr, NULL);
1431 }
1432
1433 static inline void LISTVIEW_InvalidateColumn(const LISTVIEW_INFO *infoPtr, INT nColumn)
1434 {
1435 RECT rcCol;
1436
1437 if(!is_redrawing(infoPtr)) return;
1438 LISTVIEW_GetHeaderRect(infoPtr, nColumn, &rcCol);
1439 rcCol.top = infoPtr->rcList.top;
1440 rcCol.bottom = infoPtr->rcList.bottom;
1441 LISTVIEW_InvalidateRect(infoPtr, &rcCol);
1442 }
1443
1444 /***
1445 * DESCRIPTION:
1446 * Retrieves the number of items that can fit vertically in the client area.
1447 *
1448 * PARAMETER(S):
1449 * [I] infoPtr : valid pointer to the listview structure
1450 *
1451 * RETURN:
1452 * Number of items per row.
1453 */
1454 static inline INT LISTVIEW_GetCountPerRow(const LISTVIEW_INFO *infoPtr)
1455 {
1456 INT nListWidth = infoPtr->rcList.right - infoPtr->rcList.left;
1457
1458 return max(nListWidth/infoPtr->nItemWidth, 1);
1459 }
1460
1461 /***
1462 * DESCRIPTION:
1463 * Retrieves the number of items that can fit horizontally in the client
1464 * area.
1465 *
1466 * PARAMETER(S):
1467 * [I] infoPtr : valid pointer to the listview structure
1468 *
1469 * RETURN:
1470 * Number of items per column.
1471 */
1472 static inline INT LISTVIEW_GetCountPerColumn(const LISTVIEW_INFO *infoPtr)
1473 {
1474 INT nListHeight = infoPtr->rcList.bottom - infoPtr->rcList.top;
1475
1476 return max(nListHeight / infoPtr->nItemHeight, 1);
1477 }
1478
1479
1480 /*************************************************************************
1481 * LISTVIEW_ProcessLetterKeys
1482 *
1483 * Processes keyboard messages generated by pressing the letter keys
1484 * on the keyboard.
1485 * What this does is perform a case insensitive search from the
1486 * current position with the following quirks:
1487 * - If two chars or more are pressed in quick succession we search
1488 * for the corresponding string (e.g. 'abc').
1489 * - If there is a delay we wipe away the current search string and
1490 * restart with just that char.
1491 * - If the user keeps pressing the same character, whether slowly or
1492 * fast, so that the search string is entirely composed of this
1493 * character ('aaaaa' for instance), then we search for first item
1494 * that starting with that character.
1495 * - If the user types the above character in quick succession, then
1496 * we must also search for the corresponding string ('aaaaa'), and
1497 * go to that string if there is a match.
1498 *
1499 * PARAMETERS
1500 * [I] hwnd : handle to the window
1501 * [I] charCode : the character code, the actual character
1502 * [I] keyData : key data
1503 *
1504 * RETURNS
1505 *
1506 * Zero.
1507 *
1508 * BUGS
1509 *
1510 * - The current implementation has a list of characters it will
1511 * accept and it ignores everything else. In particular it will
1512 * ignore accentuated characters which seems to match what
1513 * Windows does. But I'm not sure it makes sense to follow
1514 * Windows there.
1515 * - We don't sound a beep when the search fails.
1516 *
1517 * SEE ALSO
1518 *
1519 * TREEVIEW_ProcessLetterKeys
1520 */
1521 static INT LISTVIEW_ProcessLetterKeys(LISTVIEW_INFO *infoPtr, WPARAM charCode, LPARAM keyData)
1522 {
1523 INT nItem;
1524 INT endidx,idx;
1525 LVITEMW item;
1526 WCHAR buffer[MAX_PATH];
1527 DWORD lastKeyPressTimestamp = infoPtr->lastKeyPressTimestamp;
1528
1529 /* simple parameter checking */
1530 if (!charCode || !keyData) return 0;
1531
1532 /* only allow the valid WM_CHARs through */
1533 if (!isalnum(charCode) &&
1534 charCode != '.' && charCode != '`' && charCode != '!' &&
1535 charCode != '@' && charCode != '#' && charCode != '$' &&
1536 charCode != '%' && charCode != '^' && charCode != '&' &&
1537 charCode != '*' && charCode != '(' && charCode != ')' &&
1538 charCode != '-' && charCode != '_' && charCode != '+' &&
1539 charCode != '=' && charCode != '\\'&& charCode != ']' &&
1540 charCode != '}' && charCode != '[' && charCode != '{' &&
1541 charCode != '/' && charCode != '?' && charCode != '>' &&
1542 charCode != '<' && charCode != ',' && charCode != '~')
1543 return 0;
1544
1545 /* if there's one item or less, there is no where to go */
1546 if (infoPtr->nItemCount <= 1) return 0;
1547
1548 /* update the search parameters */
1549 infoPtr->lastKeyPressTimestamp = GetTickCount();
1550 if (infoPtr->lastKeyPressTimestamp - lastKeyPressTimestamp < KEY_DELAY) {
1551 if (infoPtr->nSearchParamLength < MAX_PATH)
1552 infoPtr->szSearchParam[infoPtr->nSearchParamLength++]=charCode;
1553 if (infoPtr->charCode != charCode)
1554 infoPtr->charCode = charCode = 0;
1555 } else {
1556 infoPtr->charCode=charCode;
1557 infoPtr->szSearchParam[0]=charCode;
1558 infoPtr->nSearchParamLength=1;
1559 /* Redundant with the 1 char string */
1560 charCode=0;
1561 }
1562
1563 /* and search from the current position */
1564 nItem=-1;
1565 if (infoPtr->nFocusedItem >= 0) {
1566 endidx=infoPtr->nFocusedItem;
1567 idx=endidx;
1568 /* if looking for single character match,
1569 * then we must always move forward
1570 */
1571 if (infoPtr->nSearchParamLength == 1)
1572 idx++;
1573 } else {
1574 endidx=infoPtr->nItemCount;
1575 idx=0;
1576 }
1577 do {
1578 if (idx == infoPtr->nItemCount) {
1579 if (endidx == infoPtr->nItemCount || endidx == 0)
1580 break;
1581 idx=0;
1582 }
1583
1584 /* get item */
1585 item.mask = LVIF_TEXT;
1586 item.iItem = idx;
1587 item.iSubItem = 0;
1588 item.pszText = buffer;
1589 item.cchTextMax = MAX_PATH;
1590 if (!LISTVIEW_GetItemW(infoPtr, &item)) return 0;
1591
1592 /* check for a match */
1593 if (lstrncmpiW(item.pszText,infoPtr->szSearchParam,infoPtr->nSearchParamLength) == 0) {
1594 nItem=idx;
1595 break;
1596 } else if ( (charCode != 0) && (nItem == -1) && (nItem != infoPtr->nFocusedItem) &&
1597 (lstrncmpiW(item.pszText,infoPtr->szSearchParam,1) == 0) ) {
1598 /* This would work but we must keep looking for a longer match */
1599 nItem=idx;
1600 }
1601 idx++;
1602 } while (idx != endidx);
1603
1604 if (nItem != -1)
1605 LISTVIEW_KeySelection(infoPtr, nItem);
1606
1607 return 0;
1608 }
1609
1610 /*************************************************************************
1611 * LISTVIEW_UpdateHeaderSize [Internal]
1612 *
1613 * Function to resize the header control
1614 *
1615 * PARAMS
1616 * [I] hwnd : handle to a window
1617 * [I] nNewScrollPos : scroll pos to set
1618 *
1619 * RETURNS
1620 * None.
1621 */
1622 static void LISTVIEW_UpdateHeaderSize(const LISTVIEW_INFO *infoPtr, INT nNewScrollPos)
1623 {
1624 RECT winRect;
1625 POINT point[2];
1626
1627 TRACE("nNewScrollPos=%d\n", nNewScrollPos);
1628
1629 GetWindowRect(infoPtr->hwndHeader, &winRect);
1630 point[0].x = winRect.left;
1631 point[0].y = winRect.top;
1632 point[1].x = winRect.right;
1633 point[1].y = winRect.bottom;
1634
1635 MapWindowPoints(HWND_DESKTOP, infoPtr->hwndSelf, point, 2);
1636 point[0].x = -nNewScrollPos;
1637 point[1].x += nNewScrollPos;
1638
1639 SetWindowPos(infoPtr->hwndHeader,0,
1640 point[0].x,point[0].y,point[1].x,point[1].y,
1641 (infoPtr->dwStyle & LVS_NOCOLUMNHEADER) ? SWP_HIDEWINDOW : SWP_SHOWWINDOW |
1642 SWP_NOZORDER | SWP_NOACTIVATE);
1643 }
1644
1645 /***
1646 * DESCRIPTION:
1647 * Update the scrollbars. This functions should be called whenever
1648 * the content, size or view changes.
1649 *
1650 * PARAMETER(S):
1651 * [I] infoPtr : valid pointer to the listview structure
1652 *
1653 * RETURN:
1654 * None
1655 */
1656 static void LISTVIEW_UpdateScroll(const LISTVIEW_INFO *infoPtr)
1657 {
1658 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
1659 SCROLLINFO horzInfo, vertInfo;
1660 INT dx, dy;
1661
1662 if ((infoPtr->dwStyle & LVS_NOSCROLL) || !is_redrawing(infoPtr)) return;
1663
1664 ZeroMemory(&horzInfo, sizeof(SCROLLINFO));
1665 horzInfo.cbSize = sizeof(SCROLLINFO);
1666 horzInfo.nPage = infoPtr->rcList.right - infoPtr->rcList.left;
1667
1668 /* for now, we'll set info.nMax to the _count_, and adjust it later */
1669 if (uView == LVS_LIST)
1670 {
1671 INT nPerCol = LISTVIEW_GetCountPerColumn(infoPtr);
1672 horzInfo.nMax = (infoPtr->nItemCount + nPerCol - 1) / nPerCol;
1673
1674 /* scroll by at least one column per page */
1675 if(horzInfo.nPage < infoPtr->nItemWidth)
1676 horzInfo.nPage = infoPtr->nItemWidth;
1677
1678 horzInfo.nPage /= infoPtr->nItemWidth;
1679 }
1680 else if (uView == LVS_REPORT)
1681 {
1682 horzInfo.nMax = infoPtr->nItemWidth;
1683 }
1684 else /* LVS_ICON, or LVS_SMALLICON */
1685 {
1686 RECT rcView;
1687
1688 if (LISTVIEW_GetViewRect(infoPtr, &rcView)) horzInfo.nMax = rcView.right - rcView.left;
1689 }
1690
1691 horzInfo.fMask = SIF_RANGE | SIF_PAGE;
1692 horzInfo.nMax = max(horzInfo.nMax - 1, 0);
1693 dx = GetScrollPos(infoPtr->hwndSelf, SB_HORZ);
1694 dx -= SetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &horzInfo, TRUE);
1695 TRACE("horzInfo=%s\n", debugscrollinfo(&horzInfo));
1696
1697 /* Setting the horizontal scroll can change the listview size
1698 * (and potentially everything else) so we need to recompute
1699 * everything again for the vertical scroll
1700 */
1701
1702 ZeroMemory(&vertInfo, sizeof(SCROLLINFO));
1703 vertInfo.cbSize = sizeof(SCROLLINFO);
1704 vertInfo.nPage = infoPtr->rcList.bottom - infoPtr->rcList.top;
1705
1706 if (uView == LVS_REPORT)
1707 {
1708 vertInfo.nMax = infoPtr->nItemCount;
1709
1710 /* scroll by at least one page */
1711 if(vertInfo.nPage < infoPtr->nItemHeight)
1712 vertInfo.nPage = infoPtr->nItemHeight;
1713
1714 if (infoPtr->nItemHeight > 0)
1715 vertInfo.nPage /= infoPtr->nItemHeight;
1716 }
1717 else if (uView != LVS_LIST) /* LVS_ICON, or LVS_SMALLICON */
1718 {
1719 RECT rcView;
1720
1721 if (LISTVIEW_GetViewRect(infoPtr, &rcView)) vertInfo.nMax = rcView.bottom - rcView.top;
1722 }
1723
1724 vertInfo.fMask = SIF_RANGE | SIF_PAGE;
1725 vertInfo.nMax = max(vertInfo.nMax - 1, 0);
1726 dy = GetScrollPos(infoPtr->hwndSelf, SB_VERT);
1727 dy -= SetScrollInfo(infoPtr->hwndSelf, SB_VERT, &vertInfo, TRUE);
1728 TRACE("vertInfo=%s\n", debugscrollinfo(&vertInfo));
1729
1730 /* Change of the range may have changed the scroll pos. If so move the content */
1731 if (dx != 0 || dy != 0)
1732 {
1733 RECT listRect;
1734 listRect = infoPtr->rcList;
1735 ScrollWindowEx(infoPtr->hwndSelf, dx, dy, &listRect, &listRect, 0, 0,
1736 SW_ERASE | SW_INVALIDATE);
1737 }
1738
1739 /* Update the Header Control */
1740 if (uView == LVS_REPORT)
1741 {
1742 horzInfo.fMask = SIF_POS;
1743 GetScrollInfo(infoPtr->hwndSelf, SB_HORZ, &horzInfo);
1744 LISTVIEW_UpdateHeaderSize(infoPtr, horzInfo.nPos);
1745 }
1746 }
1747
1748
1749 /***
1750 * DESCRIPTION:
1751 * Shows/hides the focus rectangle.
1752 *
1753 * PARAMETER(S):
1754 * [I] infoPtr : valid pointer to the listview structure
1755 * [I] fShow : TRUE to show the focus, FALSE to hide it.
1756 *
1757 * RETURN:
1758 * None
1759 */
1760 static void LISTVIEW_ShowFocusRect(const LISTVIEW_INFO *infoPtr, BOOL fShow)
1761 {
1762 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
1763 HDC hdc;
1764
1765 TRACE("fShow=%d, nItem=%d\n", fShow, infoPtr->nFocusedItem);
1766
1767 if (infoPtr->nFocusedItem < 0) return;
1768
1769 /* we need some gymnastics in ICON mode to handle large items */
1770 if ( (infoPtr->dwStyle & LVS_TYPEMASK) == LVS_ICON )
1771 {
1772 RECT rcBox;
1773
1774 LISTVIEW_GetItemBox(infoPtr, infoPtr->nFocusedItem, &rcBox);
1775 if ((rcBox.bottom - rcBox.top) > infoPtr->nItemHeight)
1776 {
1777 LISTVIEW_InvalidateRect(infoPtr, &rcBox);
1778 return;
1779 }
1780 }
1781
1782 if (!(hdc = GetDC(infoPtr->hwndSelf))) return;
1783
1784 /* for some reason, owner draw should work only in report mode */
1785 if ((infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && (uView == LVS_REPORT))
1786 {
1787 DRAWITEMSTRUCT dis;
1788 LVITEMW item;
1789
1790 HFONT hFont = infoPtr->hFont ? infoPtr->hFont : infoPtr->hDefaultFont;
1791 HFONT hOldFont = SelectObject(hdc, hFont);
1792
1793 item.iItem = infoPtr->nFocusedItem;
1794 item.iSubItem = 0;
1795 item.mask = LVIF_PARAM;
1796 if (!LISTVIEW_GetItemW(infoPtr, &item)) goto done;
1797
1798 ZeroMemory(&dis, sizeof(dis));
1799 dis.CtlType = ODT_LISTVIEW;
1800 dis.CtlID = (UINT)GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
1801 dis.itemID = item.iItem;
1802 dis.itemAction = ODA_FOCUS;
1803 if (fShow) dis.itemState |= ODS_FOCUS;
1804 dis.hwndItem = infoPtr->hwndSelf;
1805 dis.hDC = hdc;
1806 LISTVIEW_GetItemBox(infoPtr, dis.itemID, &dis.rcItem);
1807 dis.itemData = item.lParam;
1808
1809 SendMessageW(infoPtr->hwndNotify, WM_DRAWITEM, dis.CtlID, (LPARAM)&dis);
1810
1811 SelectObject(hdc, hOldFont);
1812 }
1813 else
1814 {
1815 DrawFocusRect(hdc, &infoPtr->rcFocus);
1816 }
1817 done:
1818 ReleaseDC(infoPtr->hwndSelf, hdc);
1819 }
1820
1821 /***
1822 * Invalidates all visible selected items.
1823 */
1824 static void LISTVIEW_InvalidateSelectedItems(const LISTVIEW_INFO *infoPtr)
1825 {
1826 ITERATOR i;
1827
1828 iterator_frameditems(&i, infoPtr, &infoPtr->rcList);
1829 while(iterator_next(&i))
1830 {
1831 if (LISTVIEW_GetItemState(infoPtr, i.nItem, LVIS_SELECTED))
1832 LISTVIEW_InvalidateItem(infoPtr, i.nItem);
1833 }
1834 iterator_destroy(&i);
1835 }
1836
1837
1838 /***
1839 * DESCRIPTION: [INTERNAL]
1840 * Computes an item's (left,top) corner, relative to rcView.
1841 * That is, the position has NOT been made relative to the Origin.
1842 * This is deliberate, to avoid computing the Origin over, and
1843 * over again, when this function is called in a loop. Instead,
1844 * one can factor the computation of the Origin before the loop,
1845 * and offset the value returned by this function, on every iteration.
1846 *
1847 * PARAMETER(S):
1848 * [I] infoPtr : valid pointer to the listview structure
1849 * [I] nItem : item number
1850 * [O] lpptOrig : item top, left corner
1851 *
1852 * RETURN:
1853 * None.
1854 */
1855 static void LISTVIEW_GetItemOrigin(const LISTVIEW_INFO *infoPtr, INT nItem, LPPOINT lpptPosition)
1856 {
1857 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
1858
1859 assert(nItem >= 0 && nItem < infoPtr->nItemCount);
1860
1861 if ((uView == LVS_SMALLICON) || (uView == LVS_ICON))
1862 {
1863 lpptPosition->x = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosX, nItem);
1864 lpptPosition->y = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosY, nItem);
1865 }
1866 else if (uView == LVS_LIST)
1867 {
1868 INT nCountPerColumn = LISTVIEW_GetCountPerColumn(infoPtr);
1869 lpptPosition->x = nItem / nCountPerColumn * infoPtr->nItemWidth;
1870 lpptPosition->y = nItem % nCountPerColumn * infoPtr->nItemHeight;
1871 }
1872 else /* LVS_REPORT */
1873 {
1874 lpptPosition->x = 0;
1875 lpptPosition->y = nItem * infoPtr->nItemHeight;
1876 }
1877 }
1878
1879 /***
1880 * DESCRIPTION: [INTERNAL]
1881 * Compute the rectangles of an item. This is to localize all
1882 * the computations in one place. If you are not interested in some
1883 * of these values, simply pass in a NULL -- the function is smart
1884 * enough to compute only what's necessary. The function computes
1885 * the standard rectangles (BOUNDS, ICON, LABEL) plus a non-standard
1886 * one, the BOX rectangle. This rectangle is very cheap to compute,
1887 * and is guaranteed to contain all the other rectangles. Computing
1888 * the ICON rect is also cheap, but all the others are potentially
1889 * expensive. This gives an easy and effective optimization when
1890 * searching (like point inclusion, or rectangle intersection):
1891 * first test against the BOX, and if TRUE, test against the desired
1892 * rectangle.
1893 * If the function does not have all the necessary information
1894 * to computed the requested rectangles, will crash with a
1895 * failed assertion. This is done so we catch all programming
1896 * errors, given that the function is called only from our code.
1897 *
1898 * We have the following 'special' meanings for a few fields:
1899 * * If LVIS_FOCUSED is set, we assume the item has the focus
1900 * This is important in ICON mode, where it might get a larger
1901 * then usual rectangle
1902 *
1903 * Please note that subitem support works only in REPORT mode.
1904 *
1905 * PARAMETER(S):
1906 * [I] infoPtr : valid pointer to the listview structure
1907 * [I] lpLVItem : item to compute the measures for
1908 * [O] lprcBox : ptr to Box rectangle
1909 * Same as LVM_GETITEMRECT with LVIR_BOUNDS
1910 * [0] lprcSelectBox : ptr to select box rectangle
1911 * Same as LVM_GETITEMRECT with LVIR_SELECTEDBOUNDS
1912 * [O] lprcIcon : ptr to Icon rectangle
1913 * Same as LVM_GETITEMRECT with LVIR_ICON
1914 * [O] lprcStateIcon: ptr to State Icon rectangle
1915 * [O] lprcLabel : ptr to Label rectangle
1916 * Same as LVM_GETITEMRECT with LVIR_LABEL
1917 *
1918 * RETURN:
1919 * None.
1920 */
1921 static void LISTVIEW_GetItemMetrics(const LISTVIEW_INFO *infoPtr, const LVITEMW *lpLVItem,
1922 LPRECT lprcBox, LPRECT lprcSelectBox,
1923 LPRECT lprcIcon, LPRECT lprcStateIcon, LPRECT lprcLabel)
1924 {
1925 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
1926 BOOL doSelectBox = FALSE, doIcon = FALSE, doLabel = FALSE, oversizedBox = FALSE;
1927 RECT Box, SelectBox, Icon, Label;
1928 COLUMN_INFO *lpColumnInfo = NULL;
1929 SIZE labelSize = { 0, 0 };
1930
1931 TRACE("(lpLVItem=%s)\n", debuglvitem_t(lpLVItem, TRUE));
1932
1933 /* Be smart and try to figure out the minimum we have to do */
1934 if (lpLVItem->iSubItem) assert(uView == LVS_REPORT);
1935 if (uView == LVS_ICON && (lprcBox || lprcLabel))
1936 {
1937 assert((lpLVItem->mask & LVIF_STATE) && (lpLVItem->stateMask & LVIS_FOCUSED));
1938 if (lpLVItem->state & LVIS_FOCUSED) oversizedBox = doLabel = TRUE;
1939 }
1940 if (lprcSelectBox) doSelectBox = TRUE;
1941 if (lprcLabel) doLabel = TRUE;
1942 if (doLabel || lprcIcon || lprcStateIcon) doIcon = TRUE;
1943 if (doSelectBox)
1944 {
1945 doIcon = TRUE;
1946 doLabel = TRUE;
1947 }
1948
1949 /************************************************************/
1950 /* compute the box rectangle (it should be cheap to do) */
1951 /************************************************************/
1952 if (lpLVItem->iSubItem || uView == LVS_REPORT)
1953 lpColumnInfo = LISTVIEW_GetColumnInfo(infoPtr, lpLVItem->iSubItem);
1954
1955 if (lpLVItem->iSubItem)
1956 {
1957 Box = lpColumnInfo->rcHeader;
1958 }
1959 else
1960 {
1961 Box.left = 0;
1962 Box.right = infoPtr->nItemWidth;
1963 }
1964 Box.top = 0;
1965 Box.bottom = infoPtr->nItemHeight;
1966
1967 /******************************************************************/
1968 /* compute ICON bounding box (ala LVM_GETITEMRECT) and STATEICON */
1969 /******************************************************************/
1970 if (doIcon)
1971 {
1972 LONG state_width = 0;
1973
1974 if (infoPtr->himlState && lpLVItem->iSubItem == 0)
1975 state_width = infoPtr->iconStateSize.cx;
1976
1977 if (uView == LVS_ICON)
1978 {
1979 Icon.left = Box.left + state_width;
1980 if (infoPtr->himlNormal)
1981 Icon.left += (infoPtr->nItemWidth - infoPtr->iconSize.cx - state_width) / 2;
1982 Icon.top = Box.top + ICON_TOP_PADDING;
1983 Icon.right = Icon.left;
1984 Icon.bottom = Icon.top;
1985 if (infoPtr->himlNormal)
1986 {
1987 Icon.right += infoPtr->iconSize.cx;
1988 Icon.bottom += infoPtr->iconSize.cy;
1989 }
1990 }
1991 else /* LVS_SMALLICON, LVS_LIST or LVS_REPORT */
1992 {
1993 Icon.left = Box.left + state_width;
1994
1995 if (uView == LVS_REPORT)
1996 Icon.left += REPORT_MARGINX;
1997
1998 Icon.top = Box.top;
1999 Icon.right = Icon.left;
2000 if (infoPtr->himlSmall &&
2001 (!lpColumnInfo || lpLVItem->iSubItem == 0 || (lpColumnInfo->fmt & LVCFMT_IMAGE) ||
2002 ((infoPtr->dwLvExStyle & LVS_EX_SUBITEMIMAGES) && lpLVItem->iImage != I_IMAGECALLBACK)))
2003 Icon.right += infoPtr->iconSize.cx;
2004 Icon.bottom = Icon.top + infoPtr->iconSize.cy;
2005 }
2006 if(lprcIcon) *lprcIcon = Icon;
2007 TRACE(" - icon=%s\n", wine_dbgstr_rect(&Icon));
2008
2009 /* TODO: is this correct? */
2010 if (lprcStateIcon)
2011 {
2012 lprcStateIcon->left = Icon.left - state_width;
2013 lprcStateIcon->right = Icon.left;
2014 lprcStateIcon->top = Icon.top;
2015 lprcStateIcon->bottom = lprcStateIcon->top + infoPtr->iconSize.cy;
2016 TRACE(" - state icon=%s\n", wine_dbgstr_rect(lprcStateIcon));
2017 }
2018 }
2019 else Icon.right = 0;
2020
2021 /************************************************************/
2022 /* compute LABEL bounding box (ala LVM_GETITEMRECT) */
2023 /************************************************************/
2024 if (doLabel)
2025 {
2026 /* calculate how far to the right can the label stretch */
2027 Label.right = Box.right;
2028 if (uView == LVS_REPORT)
2029 {
2030 if (lpLVItem->iSubItem == 0) Label = lpColumnInfo->rcHeader;
2031 }
2032
2033 if (lpLVItem->iSubItem || ((infoPtr->dwStyle & LVS_OWNERDRAWFIXED) && uView == LVS_REPORT))
2034 {
2035 labelSize.cx = infoPtr->nItemWidth;
2036 labelSize.cy = infoPtr->nItemHeight;
2037 goto calc_label;
2038 }
2039
2040 /* we need the text in non owner draw mode */
2041 assert(lpLVItem->mask & LVIF_TEXT);
2042 if (is_textT(lpLVItem->pszText, TRUE))
2043 {
2044 HFONT hFont = infoPtr->hFont ? infoPtr->hFont : infoPtr->hDefaultFont;
2045 HDC hdc = GetDC(infoPtr->hwndSelf);
2046 HFONT hOldFont = SelectObject(hdc, hFont);
2047 UINT uFormat;
2048 RECT rcText;
2049
2050 /* compute rough rectangle where the label will go */
2051 SetRectEmpty(&rcText);
2052 rcText.right = infoPtr->nItemWidth - TRAILING_LABEL_PADDING;
2053 rcText.bottom = infoPtr->nItemHeight;
2054 if (uView == LVS_ICON)
2055 rcText.bottom -= ICON_TOP_PADDING + infoPtr->iconSize.cy + ICON_BOTTOM_PADDING;
2056
2057 /* now figure out the flags */
2058 if (uView == LVS_ICON)
2059 uFormat = oversizedBox ? LV_FL_DT_FLAGS : LV_ML_DT_FLAGS;
2060 else
2061 uFormat = LV_SL_DT_FLAGS;
2062
2063 DrawTextW (hdc, lpLVItem->pszText, -1, &rcText, uFormat | DT_CALCRECT);
2064
2065 labelSize.cx = min(rcText.right - rcText.left + TRAILING_LABEL_PADDING, infoPtr->nItemWidth);
2066 labelSize.cy = rcText.bottom - rcText.top;
2067
2068 SelectObject(hdc, hOldFont);
2069 ReleaseDC(infoPtr->hwndSelf, hdc);
2070 }
2071
2072 calc_label:
2073 if (uView == LVS_ICON)
2074 {
2075 Label.left = Box.left + (infoPtr->nItemWidth - labelSize.cx) / 2;
2076 Label.top = Box.top + ICON_TOP_PADDING_HITABLE +
2077 infoPtr->iconSize.cy + ICON_BOTTOM_PADDING;
2078 Label.right = Label.left + labelSize.cx;
2079 Label.bottom = Label.top + infoPtr->nItemHeight;
2080 if (!oversizedBox && labelSize.cy > infoPtr->ntmHeight)
2081 {
2082 labelSize.cy = min(Box.bottom - Label.top, labelSize.cy);
2083 labelSize.cy /= infoPtr->ntmHeight;
2084 labelSize.cy = max(labelSize.cy, 1);
2085 labelSize.cy *= infoPtr->ntmHeight;
2086 }
2087 Label.bottom = Label.top + labelSize.cy + HEIGHT_PADDING;
2088 }
2089 else if (uView == LVS_REPORT)
2090 {
2091 Label.left = Icon.right;
2092 Label.top = Box.top;
2093 Label.right = lpColumnInfo->rcHeader.right;
2094 Label.bottom = Label.top + infoPtr->nItemHeight;
2095 }
2096 else /* LVS_SMALLICON, LVS_LIST or LVS_REPORT */
2097 {
2098 Label.left = Icon.right;
2099 Label.top = Box.top;
2100 Label.right = min(Label.left + labelSize.cx, Label.right);
2101 Label.bottom = Label.top + infoPtr->nItemHeight;
2102 }
2103
2104 if (lprcLabel) *lprcLabel = Label;
2105 TRACE(" - label=%s\n", wine_dbgstr_rect(&Label));
2106 }
2107
2108 /************************************************************/
2109 /* compute STATEICON bounding box */
2110 /************************************************************/
2111 if (doSelectBox)
2112 {
2113 if (uView == LVS_REPORT)
2114 {
2115 SelectBox.left = Icon.right; /* FIXME: should be Icon.left */
2116 SelectBox.top = Box.top;
2117 SelectBox.bottom = Box.bottom;
2118 if (lpLVItem->iSubItem == 0)
2119 {
2120 /* we need the indent in report mode */
2121 assert(lpLVItem->mask & LVIF_INDENT);
2122 SelectBox.left += infoPtr->iconSize.cx * lpLVItem->iIndent;
2123 }
2124 SelectBox.right = min(SelectBox.left + labelSize.cx, Label.right);
2125 }
2126 else
2127 {
2128 UnionRect(&SelectBox, &Icon, &Label);
2129 }
2130 if (lprcSelectBox) *lprcSelectBox = SelectBox;
2131 TRACE(" - select box=%s\n", wine_dbgstr_rect(&SelectBox));
2132 }
2133
2134 /* Fix the Box if necessary */
2135 if (lprcBox)
2136 {
2137 if (oversizedBox) UnionRect(lprcBox, &Box, &Label);
2138 else *lprcBox = Box;
2139 }
2140 TRACE(" - box=%s\n", wine_dbgstr_rect(&Box));
2141 }
2142
2143 /***
2144 * DESCRIPTION: [INTERNAL]
2145 *
2146 * PARAMETER(S):
2147 * [I] infoPtr : valid pointer to the listview structure
2148 * [I] nItem : item number
2149 * [O] lprcBox : ptr to Box rectangle
2150 *
2151 * RETURN:
2152 * None.
2153 */
2154 static void LISTVIEW_GetItemBox(const LISTVIEW_INFO *infoPtr, INT nItem, LPRECT lprcBox)
2155 {
2156 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
2157 WCHAR szDispText[DISP_TEXT_SIZE] = { '\0' };
2158 POINT Position, Origin;
2159 LVITEMW lvItem;
2160
2161 LISTVIEW_GetOrigin(infoPtr, &Origin);
2162 LISTVIEW_GetItemOrigin(infoPtr, nItem, &Position);
2163
2164 /* Be smart and try to figure out the minimum we have to do */
2165 lvItem.mask = 0;
2166 if (uView == LVS_ICON && infoPtr->bFocus && LISTVIEW_GetItemState(infoPtr, nItem, LVIS_FOCUSED))
2167 lvItem.mask |= LVIF_TEXT;
2168 lvItem.iItem = nItem;
2169 lvItem.iSubItem = 0;
2170 lvItem.pszText = szDispText;
2171 lvItem.cchTextMax = DISP_TEXT_SIZE;
2172 if (lvItem.mask) LISTVIEW_GetItemW(infoPtr, &lvItem);
2173 if (uView == LVS_ICON)
2174 {
2175 lvItem.mask |= LVIF_STATE;
2176 lvItem.stateMask = LVIS_FOCUSED;
2177 lvItem.state = (lvItem.mask & LVIF_TEXT ? LVIS_FOCUSED : 0);
2178 }
2179 LISTVIEW_GetItemMetrics(infoPtr, &lvItem, lprcBox, 0, 0, 0, 0);
2180
2181 OffsetRect(lprcBox, Position.x + Origin.x, Position.y + Origin.y);
2182 }
2183
2184
2185 /***
2186 * DESCRIPTION:
2187 * Returns the current icon position, and advances it along the top.
2188 * The returned position is not offset by Origin.
2189 *
2190 * PARAMETER(S):
2191 * [I] infoPtr : valid pointer to the listview structure
2192 * [O] lpPos : will get the current icon position
2193 *
2194 * RETURN:
2195 * None
2196 */
2197 static void LISTVIEW_NextIconPosTop(LISTVIEW_INFO *infoPtr, LPPOINT lpPos)
2198 {
2199 INT nListWidth = infoPtr->rcList.right - infoPtr->rcList.left;
2200
2201 *lpPos = infoPtr->currIconPos;
2202
2203 infoPtr->currIconPos.x += infoPtr->nItemWidth;
2204 if (infoPtr->currIconPos.x + infoPtr->nItemWidth <= nListWidth) return;
2205
2206 infoPtr->currIconPos.x = 0;
2207 infoPtr->currIconPos.y += infoPtr->nItemHeight;
2208 }
2209
2210
2211 /***
2212 * DESCRIPTION:
2213 * Returns the current icon position, and advances it down the left edge.
2214 * The returned position is not offset by Origin.
2215 *
2216 * PARAMETER(S):
2217 * [I] infoPtr : valid pointer to the listview structure
2218 * [O] lpPos : will get the current icon position
2219 *
2220 * RETURN:
2221 * None
2222 */
2223 static void LISTVIEW_NextIconPosLeft(LISTVIEW_INFO *infoPtr, LPPOINT lpPos)
2224 {
2225 INT nListHeight = infoPtr->rcList.bottom - infoPtr->rcList.top;
2226
2227 *lpPos = infoPtr->currIconPos;
2228
2229 infoPtr->currIconPos.y += infoPtr->nItemHeight;
2230 if (infoPtr->currIconPos.y + infoPtr->nItemHeight <= nListHeight) return;
2231
2232 infoPtr->currIconPos.x += infoPtr->nItemWidth;
2233 infoPtr->currIconPos.y = 0;
2234 }
2235
2236
2237 /***
2238 * DESCRIPTION:
2239 * Moves an icon to the specified position.
2240 * It takes care of invalidating the item, etc.
2241 *
2242 * PARAMETER(S):
2243 * [I] infoPtr : valid pointer to the listview structure
2244 * [I] nItem : the item to move
2245 * [I] lpPos : the new icon position
2246 * [I] isNew : flags the item as being new
2247 *
2248 * RETURN:
2249 * Success: TRUE
2250 * Failure: FALSE
2251 */
2252 static BOOL LISTVIEW_MoveIconTo(const LISTVIEW_INFO *infoPtr, INT nItem, const POINT *lppt, BOOL isNew)
2253 {
2254 POINT old;
2255
2256 if (!isNew)
2257 {
2258 old.x = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosX, nItem);
2259 old.y = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosY, nItem);
2260
2261 if (lppt->x == old.x && lppt->y == old.y) return TRUE;
2262 LISTVIEW_InvalidateItem(infoPtr, nItem);
2263 }
2264
2265 /* Allocating a POINTER for every item is too resource intensive,
2266 * so we'll keep the (x,y) in different arrays */
2267 if (!DPA_SetPtr(infoPtr->hdpaPosX, nItem, (void *)(LONG_PTR)lppt->x)) return FALSE;
2268 if (!DPA_SetPtr(infoPtr->hdpaPosY, nItem, (void *)(LONG_PTR)lppt->y)) return FALSE;
2269
2270 LISTVIEW_InvalidateItem(infoPtr, nItem);
2271
2272 return TRUE;
2273 }
2274
2275 /***
2276 * DESCRIPTION:
2277 * Arranges listview items in icon display mode.
2278 *
2279 * PARAMETER(S):
2280 * [I] infoPtr : valid pointer to the listview structure
2281 * [I] nAlignCode : alignment code
2282 *
2283 * RETURN:
2284 * SUCCESS : TRUE
2285 * FAILURE : FALSE
2286 */
2287 static BOOL LISTVIEW_Arrange(LISTVIEW_INFO *infoPtr, INT nAlignCode)
2288 {
2289 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
2290 void (*next_pos)(LISTVIEW_INFO *, LPPOINT);
2291 POINT pos;
2292 INT i;
2293
2294 if (uView != LVS_ICON && uView != LVS_SMALLICON) return FALSE;
2295
2296 TRACE("nAlignCode=%d\n", nAlignCode);
2297
2298 if (nAlignCode == LVA_DEFAULT)
2299 {
2300 if (infoPtr->dwStyle & LVS_ALIGNLEFT) nAlignCode = LVA_ALIGNLEFT;
2301 else nAlignCode = LVA_ALIGNTOP;
2302 }
2303
2304 switch (nAlignCode)
2305 {
2306 case LVA_ALIGNLEFT: next_pos = LISTVIEW_NextIconPosLeft; break;
2307 case LVA_ALIGNTOP: next_pos = LISTVIEW_NextIconPosTop; break;
2308 case LVA_SNAPTOGRID: next_pos = LISTVIEW_NextIconPosTop; break; /* FIXME */
2309 default: return FALSE;
2310 }
2311
2312 infoPtr->bAutoarrange = TRUE;
2313 infoPtr->currIconPos.x = infoPtr->currIconPos.y = 0;
2314 for (i = 0; i < infoPtr->nItemCount; i++)
2315 {
2316 next_pos(infoPtr, &pos);
2317 LISTVIEW_MoveIconTo(infoPtr, i, &pos, FALSE);
2318 }
2319
2320 return TRUE;
2321 }
2322
2323 /***
2324 * DESCRIPTION:
2325 * Retrieves the bounding rectangle of all the items, not offset by Origin.
2326 *
2327 * PARAMETER(S):
2328 * [I] infoPtr : valid pointer to the listview structure
2329 * [O] lprcView : bounding rectangle
2330 *
2331 * RETURN:
2332 * SUCCESS : TRUE
2333 * FAILURE : FALSE
2334 */
2335 static void LISTVIEW_GetAreaRect(const LISTVIEW_INFO *infoPtr, LPRECT lprcView)
2336 {
2337 INT i, x, y;
2338
2339 SetRectEmpty(lprcView);
2340
2341 switch (infoPtr->dwStyle & LVS_TYPEMASK)
2342 {
2343 case LVS_ICON:
2344 case LVS_SMALLICON:
2345 for (i = 0; i < infoPtr->nItemCount; i++)
2346 {
2347 x = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosX, i);
2348 y = (LONG_PTR)DPA_GetPtr(infoPtr->hdpaPosY, i);
2349 lprcView->right = max(lprcView->right, x);
2350 lprcView->bottom = max(lprcView->bottom, y);
2351 }
2352 if (infoPtr->nItemCount > 0)
2353 {
2354 lprcView->right += infoPtr->nItemWidth;
2355 lprcView->bottom += infoPtr->nItemHeight;
2356 }
2357 break;
2358
2359 case LVS_LIST:
2360 y = LISTVIEW_GetCountPerColumn(infoPtr);
2361 x = infoPtr->nItemCount / y;
2362 if (infoPtr->nItemCount % y) x++;
2363 lprcView->right = x * infoPtr->nItemWidth;
2364 lprcView->bottom = y * infoPtr->nItemHeight;
2365 break;
2366
2367 case LVS_REPORT:
2368 lprcView->right = infoPtr->nItemWidth;
2369 lprcView->bottom = infoPtr->nItemCount * infoPtr->nItemHeight;
2370 break;
2371 }
2372 }
2373
2374 /***
2375 * DESCRIPTION:
2376 * Retrieves the bounding rectangle of all the items.
2377 *
2378 * PARAMETER(S):
2379 * [I] infoPtr : valid pointer to the listview structure
2380 * [O] lprcView : bounding rectangle
2381 *
2382 * RETURN:
2383 * SUCCESS : TRUE
2384 * FAILURE : FALSE
2385 */
2386 static BOOL LISTVIEW_GetViewRect(const LISTVIEW_INFO *infoPtr, LPRECT lprcView)
2387 {
2388 POINT ptOrigin;
2389
2390 TRACE("(lprcView=%p)\n", lprcView);
2391
2392 if (!lprcView) return FALSE;
2393
2394 LISTVIEW_GetOrigin(infoPtr, &ptOrigin);
2395 LISTVIEW_GetAreaRect(infoPtr, lprcView);
2396 OffsetRect(lprcView, ptOrigin.x, ptOrigin.y);
2397
2398 TRACE("lprcView=%s\n", wine_dbgstr_rect(lprcView));
2399
2400 return TRUE;
2401 }
2402
2403 /***
2404 * DESCRIPTION:
2405 * Retrieves the subitem pointer associated with the subitem index.
2406 *
2407 * PARAMETER(S):
2408 * [I] hdpaSubItems : DPA handle for a specific item
2409 * [I] nSubItem : index of subitem
2410 *
2411 * RETURN:
2412 * SUCCESS : subitem pointer
2413 * FAILURE : NULL
2414 */
2415 static SUBITEM_INFO* LISTVIEW_GetSubItemPtr(HDPA hdpaSubItems, INT nSubItem)
2416 {
2417 SUBITEM_INFO *lpSubItem;
2418 INT i;
2419
2420 /* we should binary search here if need be */
2421 for (i = 1; i < DPA_GetPtrCount(hdpaSubItems); i++)
2422 {
2423 lpSubItem = DPA_GetPtr(hdpaSubItems, i);
2424 if (lpSubItem->iSubItem == nSubItem)
2425 return lpSubItem;
2426 }
2427
2428 return NULL;
2429 }
2430
2431
2432 /***
2433 * DESCRIPTION:
2434 * Calculates the desired item width.
2435 *
2436 * PARAMETER(S):
2437 * [I] infoPtr : valid pointer to the listview structure
2438 *
2439 * RETURN:
2440 * The desired item width.
2441 */
2442 static INT LISTVIEW_CalculateItemWidth(const LISTVIEW_INFO *infoPtr)
2443 {
2444 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
2445 INT nItemWidth = 0;
2446
2447 TRACE("uView=%d\n", uView);
2448
2449 if (uView == LVS_ICON)
2450 nItemWidth = infoPtr->iconSpacing.cx;
2451 else if (uView == LVS_REPORT)
2452 {
2453 RECT rcHeader;
2454
2455 if (DPA_GetPtrCount(infoPtr->hdpaColumns) > 0)
2456 {
2457 LISTVIEW_GetHeaderRect(infoPtr, DPA_GetPtrCount(infoPtr->hdpaColumns) - 1, &rcHeader);
2458 nItemWidth = rcHeader.right;
2459 }
2460 }
2461 else /* LVS_SMALLICON, or LVS_LIST */
2462 {
2463 INT i;
2464
2465 for (i = 0; i < infoPtr->nItemCount; i++)
2466 nItemWidth = max(LISTVIEW_GetLabelWidth(infoPtr, i), nItemWidth);
2467
2468 if (infoPtr->himlSmall) nItemWidth += infoPtr->iconSize.cx;
2469 if (infoPtr->himlState) nItemWidth += infoPtr->iconStateSize.cx;
2470
2471 nItemWidth = max(DEFAULT_COLUMN_WIDTH, nItemWidth + WIDTH_PADDING);
2472 }
2473
2474 return max(nItemWidth, 1);
2475 }
2476
2477 /***
2478 * DESCRIPTION:
2479 * Calculates the desired item height.
2480 *
2481 * PARAMETER(S):
2482 * [I] infoPtr : valid pointer to the listview structure
2483 *
2484 * RETURN:
2485 * The desired item height.
2486 */
2487 static INT LISTVIEW_CalculateItemHeight(const LISTVIEW_INFO *infoPtr)
2488 {
2489 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
2490 INT nItemHeight;
2491
2492 TRACE("uView=%d\n", uView);
2493
2494 if (uView == LVS_ICON)
2495 nItemHeight = infoPtr->iconSpacing.cy;
2496 else
2497 {
2498 nItemHeight = infoPtr->ntmHeight;
2499 if (uView == LVS_REPORT && infoPtr->dwLvExStyle & LVS_EX_GRIDLINES)
2500 nItemHeight++;
2501 if (infoPtr->himlState)
2502 nItemHeight = max(nItemHeight, infoPtr->iconStateSize.cy);
2503 if (infoPtr->himlSmall)
2504 nItemHeight = max(nItemHeight, infoPtr->iconSize.cy);
2505 if (infoPtr->himlState || infoPtr->himlSmall)
2506 nItemHeight += HEIGHT_PADDING;
2507 if (infoPtr->nMeasureItemHeight > 0)
2508 nItemHeight = infoPtr->nMeasureItemHeight;
2509 }
2510
2511 return max(nItemHeight, 1);
2512 }
2513
2514 /***
2515 * DESCRIPTION:
2516 * Updates the width, and height of an item.
2517 *
2518 * PARAMETER(S):
2519 * [I] infoPtr : valid pointer to the listview structure
2520 *
2521 * RETURN:
2522 * None.
2523 */
2524 static inline void LISTVIEW_UpdateItemSize(LISTVIEW_INFO *infoPtr)
2525 {
2526 infoPtr->nItemWidth = LISTVIEW_CalculateItemWidth(infoPtr);
2527 infoPtr->nItemHeight = LISTVIEW_CalculateItemHeight(infoPtr);
2528 }
2529
2530
2531 /***
2532 * DESCRIPTION:
2533 * Retrieves and saves important text metrics info for the current
2534 * Listview font.
2535 *
2536 * PARAMETER(S):
2537 * [I] infoPtr : valid pointer to the listview structure
2538 *
2539 */
2540 static void LISTVIEW_SaveTextMetrics(LISTVIEW_INFO *infoPtr)
2541 {
2542 HDC hdc = GetDC(infoPtr->hwndSelf);
2543 HFONT hFont = infoPtr->hFont ? infoPtr->hFont : infoPtr->hDefaultFont;
2544 HFONT hOldFont = SelectObject(hdc, hFont);
2545 TEXTMETRICW tm;
2546 SIZE sz;
2547
2548 if (GetTextMetricsW(hdc, &tm))
2549 {
2550 infoPtr->ntmHeight = tm.tmHeight;
2551 infoPtr->ntmMaxCharWidth = tm.tmMaxCharWidth;
2552 }
2553
2554 if (GetTextExtentPoint32A(hdc, "...", 3, &sz))
2555 infoPtr->nEllipsisWidth = sz.cx;
2556
2557 SelectObject(hdc, hOldFont);
2558 ReleaseDC(infoPtr->hwndSelf, hdc);
2559
2560 TRACE("tmHeight=%d\n", infoPtr->ntmHeight);
2561 }
2562
2563 /***
2564 * DESCRIPTION:
2565 * A compare function for ranges
2566 *
2567 * PARAMETER(S)
2568 * [I] range1 : pointer to range 1;
2569 * [I] range2 : pointer to range 2;
2570 * [I] flags : flags
2571 *
2572 * RETURNS:
2573 * > 0 : if range 1 > range 2
2574 * < 0 : if range 2 > range 1
2575 * = 0 : if range intersects range 2
2576 */
2577 static INT CALLBACK ranges_cmp(LPVOID range1, LPVOID range2, LPARAM flags)
2578 {
2579 INT cmp;
2580
2581 if (((RANGE*)range1)->upper <= ((RANGE*)range2)->lower)
2582 cmp = -1;
2583 else if (((RANGE*)range2)->upper <= ((RANGE*)range1)->lower)
2584 cmp = 1;
2585 else
2586 cmp = 0;
2587
2588 TRACE("range1=%s, range2=%s, cmp=%d\n", debugrange((RANGE*)range1), debugrange((RANGE*)range2), cmp);
2589
2590 return cmp;
2591 }
2592
2593 #if DEBUG_RANGES
2594 #define ranges_check(ranges, desc) ranges_assert(ranges, desc, __FUNCTION__, __LINE__)
2595 #else
2596 #define ranges_check(ranges, desc) do { } while(0)
2597 #endif
2598
2599 static void ranges_assert(RANGES ranges, LPCSTR desc, const char *func, int line)
2600 {
2601 INT i;
2602 RANGE *prev, *curr;
2603
2604 TRACE("*** Checking %s:%d:%s ***\n", func, line, desc);
2605 assert (ranges);
2606 assert (DPA_GetPtrCount(ranges->hdpa) >= 0);
2607 ranges_dump(ranges);
2608 prev = DPA_GetPtr(ranges->hdpa, 0);
2609 if (DPA_GetPtrCount(ranges->hdpa) > 0)
2610 assert (prev->lower >= 0 && prev->lower < prev->upper);
2611 for (i = 1; i < DPA_GetPtrCount(ranges->hdpa); i++)
2612 {
2613 curr = DPA_GetPtr(ranges->hdpa, i);
2614 assert (prev->upper <= curr->lower);
2615 assert (curr->lower < curr->upper);
2616 prev = curr;
2617 }
2618 TRACE("--- Done checking---\n");
2619 }
2620
2621 static RANGES ranges_create(int count)
2622 {
2623 RANGES ranges = Alloc(sizeof(struct tagRANGES));
2624 if (!ranges) return NULL;
2625 ranges->hdpa = DPA_Create(count);
2626 if (ranges->hdpa) return ranges;
2627 Free(ranges);
2628 return NULL;
2629 }
2630
2631 static void ranges_clear(RANGES ranges)
2632 {
2633 INT i;
2634
2635 for(i = 0; i < DPA_GetPtrCount(ranges->hdpa); i++)
2636 Free(DPA_GetPtr(ranges->hdpa, i));
2637 DPA_DeleteAllPtrs(ranges->hdpa);
2638 }
2639
2640
2641 static void ranges_destroy(RANGES ranges)
2642 {
2643 if (!ranges) return;
2644 ranges_clear(ranges);
2645 DPA_Destroy(ranges->hdpa);
2646 Free(ranges);
2647 }
2648
2649 static RANGES ranges_clone(RANGES ranges)
2650 {
2651 RANGES clone;
2652 INT i;
2653
2654 if (!(clone = ranges_create(DPA_GetPtrCount(ranges->hdpa)))) goto fail;
2655
2656 for (i = 0; i < DPA_GetPtrCount(ranges->hdpa); i++)
2657 {
2658 RANGE *newrng = Alloc(sizeof(RANGE));
2659 if (!newrng) goto fail;
2660 *newrng = *((RANGE*)DPA_GetPtr(ranges->hdpa, i));
2661 DPA_SetPtr(clone->hdpa, i, newrng);
2662 }
2663 return clone;
2664
2665 fail:
2666 TRACE ("clone failed\n");
2667 ranges_destroy(clone);
2668 return NULL;
2669 }
2670
2671 static RANGES ranges_diff(RANGES ranges, RANGES sub)
2672 {
2673 INT i;
2674
2675 for (i = 0; i < DPA_GetPtrCount(sub->hdpa); i++)
2676 ranges_del(ranges, *((RANGE *)DPA_GetPtr(sub->hdpa, i)));
2677
2678 return ranges;
2679 }
2680
2681 static void ranges_dump(RANGES ranges)
2682 {
2683 INT i;
2684
2685 for (i = 0; i < DPA_GetPtrCount(ranges->hdpa); i++)
2686 TRACE(" %s\n", debugrange(DPA_GetPtr(ranges->hdpa, i)));
2687 }
2688
2689 static inline BOOL ranges_contain(RANGES ranges, INT nItem)
2690 {
2691 RANGE srchrng = { nItem, nItem + 1 };
2692
2693 TRACE("(nItem=%d)\n", nItem);
2694 ranges_check(ranges, "before contain");
2695 return DPA_Search(ranges->hdpa, &srchrng, 0, ranges_cmp, 0, DPAS_SORTED) != -1;
2696 }
2697
2698 static INT ranges_itemcount(RANGES ranges)
2699 {
2700 INT i, count = 0;
2701
2702 for (i = 0; i < DPA_GetPtrCount(ranges->hdpa); i++)
2703 {
2704 RANGE *sel = DPA_GetPtr(ranges->hdpa, i);
2705 count += sel->upper - sel->lower;
2706 }
2707
2708 return count;
2709 }
2710
2711 static BOOL ranges_shift(RANGES ranges, INT nItem, INT delta, INT nUpper)
2712 {
2713 RANGE srchrng = { nItem, nItem + 1 }, *chkrng;
2714 INT index;
2715
2716 index = DPA_Search(ranges->hdpa, &srchrng, 0, ranges_cmp, 0, DPAS_SORTED | DPAS_INSERTAFTER);
2717 if (index == -1) return TRUE;
2718
2719 for (; index < DPA_GetPtrCount(ranges->hdpa); index++)
2720 {
2721 chkrng = DPA_GetPtr(ranges->hdpa, index);
2722 if (chkrng->lower >= nItem)
2723 chkrng->lower = max(min(chkrng->lower + delta, nUpper - 1), 0);
2724 if (chkrng->upper > nItem)
2725 chkrng->upper = max(min(chkrng->upper + delta, nUpper), 0);
2726 }
2727 return TRUE;
2728 }
2729
2730 static BOOL ranges_add(RANGES ranges, RANGE range)
2731 {
2732 RANGE srchrgn;
2733 INT index;
2734
2735 TRACE("(%s)\n", debugrange(&range));
2736 ranges_check(ranges, "before add");
2737
2738 /* try find overlapping regions first */
2739 srchrgn.lower = range.lower - 1;
2740 srchrgn.upper = range.upper + 1;
2741 index = DPA_Search(ranges->hdpa, &srchrgn, 0, ranges_cmp, 0, DPAS_SORTED);
2742
2743 if (index == -1)
2744 {
2745 RANGE *newrgn;
2746
2747 TRACE("Adding new range\n");
2748
2749 /* create the brand new range to insert */
2750 newrgn = Alloc(sizeof(RANGE));
2751 if(!newrgn) goto fail;
2752 *newrgn = range;
2753
2754 /* figure out where to insert it */
2755 index = DPA_Search(ranges->hdpa, newrgn, 0, ranges_cmp, 0, DPAS_SORTED | DPAS_INSERTAFTER);
2756 TRACE("index=%d\n", index);
2757 if (index == -1) index = 0;
2758
2759 /* and get it over with */
2760 if (DPA_InsertPtr(ranges->hdpa, index, newrgn) == -1)
2761 {
2762 Free(newrgn);
2763 goto fail;
2764 }
2765 }
2766 else
2767 {
2768 RANGE *chkrgn, *mrgrgn;
2769 INT fromindex, mergeindex;
2770
2771 chkrgn = DPA_GetPtr(ranges->hdpa, index);
2772 TRACE("Merge with %s @%d\n", debugrange(chkrgn), index);
2773
2774 chkrgn->lower = min(range.lower, chkrgn->lower);
2775 chkrgn->upper = max(range.upper, chkrgn->upper);
2776
2777 TRACE("New range %s @%d\n", debugrange(chkrgn), index);
2778
2779 /* merge now common ranges */
2780 fromindex = 0;
2781 srchrgn.lower = chkrgn->lower - 1;
2782 srchrgn.upper = chkrgn->upper + 1;
2783
2784 do
2785 {
2786 mergeindex = DPA_Search(ranges->hdpa, &srchrgn, fromindex, ranges_cmp, 0, 0);
2787 if (mergeindex == -1) break;
2788 if (mergeindex == index)
2789 {
2790 fromindex = index + 1;
2791 continue;
2792 }
2793
2794 TRACE("Merge with index %i\n", mergeindex);
2795
2796 mrgrgn = DPA_GetPtr(ranges->hdpa, mergeindex);
2797 chkrgn->lower = min(chkrgn->lower, mrgrgn->lower);
2798 chkrgn->upper = max(chkrgn->upper, mrgrgn->upper);
2799 Free(mrgrgn);
2800 DPA_DeletePtr(ranges->hdpa, mergeindex);
2801 if (mergeindex < index) index --;
2802 } while(1);
2803 }
2804
2805 ranges_check(ranges, "after add");
2806 return TRUE;
2807
2808 fail:
2809 ranges_check(ranges, "failed add");
2810 return FALSE;
2811 }
2812
2813 static BOOL ranges_del(RANGES ranges, RANGE range)
2814 {
2815 RANGE *chkrgn;
2816 INT index;
2817
2818 TRACE("(%s)\n", debugrange(&range));
2819 ranges_check(ranges, "before del");
2820
2821 /* we don't use DPAS_SORTED here, since we need *
2822 * to find the first overlapping range */
2823 index = DPA_Search(ranges->hdpa, &range, 0, ranges_cmp, 0, 0);
2824 while(index != -1)
2825 {
2826 chkrgn = DPA_GetPtr(ranges->hdpa, index);
2827
2828 TRACE("Matches range %s @%d\n", debugrange(chkrgn), index);
2829
2830 /* case 1: Same range */
2831 if ( (chkrgn->upper == range.upper) &&
2832 (chkrgn->lower == range.lower) )
2833 {
2834 DPA_DeletePtr(ranges->hdpa, index);
2835 break;
2836 }
2837 /* case 2: engulf */
2838 else if ( (chkrgn->upper <= range.upper) &&
2839 (chkrgn->lower >= range.lower) )
2840 {
2841 DPA_DeletePtr(ranges->hdpa, index);
2842 }
2843 /* case 3: overlap upper */
2844 else if ( (chkrgn->upper <= range.upper) &&
2845 (chkrgn->lower < range.lower) )
2846 {
2847 chkrgn->upper = range.lower;
2848 }
2849 /* case 4: overlap lower */
2850 else if ( (chkrgn->upper > range.upper) &&
2851 (chkrgn->lower >= range.lower) )
2852 {
2853 chkrgn->lower = range.upper;
2854 break;
2855 }
2856 /* case 5: fully internal */
2857 else
2858 {
2859 RANGE tmprgn = *chkrgn, *newrgn;
2860
2861 if (!(newrgn = Alloc(sizeof(RANGE)))) goto fail;
2862 newrgn->lower = chkrgn->lower;
2863 newrgn->upper = range.lower;
2864 chkrgn->lower = range.upper;
2865 if (DPA_InsertPtr(ranges->hdpa, index, newrgn) == -1)
2866 {
2867 Free(newrgn);
2868 goto fail;
2869 }
2870 chkrgn = &tmprgn;
2871 break;
2872 }
2873
2874 index = DPA_Search(ranges->hdpa, &range, index, ranges_cmp, 0, 0);
2875 }
2876
2877 ranges_check(ranges, "after del");
2878 return TRUE;
2879
2880 fail:
2881 ranges_check(ranges, "failed del");
2882 return FALSE;
2883 }
2884
2885 /***
2886 * DESCRIPTION:
2887 * Removes all selection ranges
2888 *
2889 * Parameters(s):
2890 * [I] infoPtr : valid pointer to the listview structure
2891 * [I] toSkip : item range to skip removing the selection
2892 *
2893 * RETURNS:
2894 * SUCCESS : TRUE
2895 * FAILURE : TRUE
2896 */
2897 static BOOL LISTVIEW_DeselectAllSkipItems(LISTVIEW_INFO *infoPtr, RANGES toSkip)
2898 {
2899 LVITEMW lvItem;
2900 ITERATOR i;
2901 RANGES clone;
2902
2903 TRACE("()\n");
2904
2905 lvItem.state = 0;
2906 lvItem.stateMask = LVIS_SELECTED;
2907
2908 /* need to clone the DPA because callbacks can change it */
2909 if (!(clone = ranges_clone(infoPtr->selectionRanges))) return FALSE;
2910 iterator_rangesitems(&i, ranges_diff(clone, toSkip));
2911 while(iterator_next(&i))
2912 LISTVIEW_SetItemState(infoPtr, i.nItem, &lvItem);
2913 /* note that the iterator destructor will free the cloned range */
2914 iterator_destroy(&i);
2915
2916 return TRUE;
2917 }
2918
2919 static inline BOOL LISTVIEW_DeselectAllSkipItem(LISTVIEW_INFO *infoPtr, INT nItem)
2920 {
2921 RANGES toSkip;
2922
2923 if (!(toSkip = ranges_create(1))) return FALSE;
2924 if (nItem != -1) ranges_additem(toSkip, nItem);
2925 LISTVIEW_DeselectAllSkipItems(infoPtr, toSkip);
2926 ranges_destroy(toSkip);
2927 return TRUE;
2928 }
2929
2930 static inline BOOL LISTVIEW_DeselectAll(LISTVIEW_INFO *infoPtr)
2931 {
2932 return LISTVIEW_DeselectAllSkipItem(infoPtr, -1);
2933 }
2934
2935 /***
2936 * DESCRIPTION:
2937 * Retrieves the number of items that are marked as selected.
2938 *
2939 * PARAMETER(S):
2940 * [I] infoPtr : valid pointer to the listview structure
2941 *
2942 * RETURN:
2943 * Number of items selected.
2944 */
2945 static INT LISTVIEW_GetSelectedCount(const LISTVIEW_INFO *infoPtr)
2946 {
2947 INT nSelectedCount = 0;
2948
2949 if (infoPtr->uCallbackMask & LVIS_SELECTED)
2950 {
2951 INT i;
2952 for (i = 0; i < infoPtr->nItemCount; i++)
2953 {
2954 if (LISTVIEW_GetItemState(infoPtr, i, LVIS_SELECTED))
2955 nSelectedCount++;
2956 }
2957 }
2958 else
2959 nSelectedCount = ranges_itemcount(infoPtr->selectionRanges);
2960
2961 TRACE("nSelectedCount=%d\n", nSelectedCount);
2962 return nSelectedCount;
2963 }
2964
2965 /***
2966 * DESCRIPTION:
2967 * Manages the item focus.
2968 *
2969 * PARAMETER(S):
2970 * [I] infoPtr : valid pointer to the listview structure
2971 * [I] nItem : item index
2972 *
2973 * RETURN:
2974 * TRUE : focused item changed
2975 * FALSE : focused item has NOT changed
2976 */
2977 static inline BOOL LISTVIEW_SetItemFocus(LISTVIEW_INFO *infoPtr, INT nItem)
2978 {
2979 INT oldFocus = infoPtr->nFocusedItem;
2980 LVITEMW lvItem;
2981
2982 if (nItem == infoPtr->nFocusedItem) return FALSE;
2983
2984 lvItem.state = nItem == -1 ? 0 : LVIS_FOCUSED;
2985 lvItem.stateMask = LVIS_FOCUSED;
2986 LISTVIEW_SetItemState(infoPtr, nItem == -1 ? infoPtr->nFocusedItem : nItem, &lvItem);
2987
2988 return oldFocus != infoPtr->nFocusedItem;
2989 }
2990
2991 /* Helper function for LISTVIEW_ShiftIndices *only* */
2992 static INT shift_item(const LISTVIEW_INFO *infoPtr, INT nShiftItem, INT nItem, INT direction)
2993 {
2994 if (nShiftItem < nItem) return nShiftItem;
2995
2996 if (nShiftItem > nItem) return nShiftItem + direction;
2997
2998 if (direction > 0) return nShiftItem + direction;
2999
3000 return min(nShiftItem, infoPtr->nItemCount - 1);
3001 }
3002
3003 /**
3004 * DESCRIPTION:
3005 * Updates the various indices after an item has been inserted or deleted.
3006 *
3007 * PARAMETER(S):
3008 * [I] infoPtr : valid pointer to the listview structure
3009 * [I] nItem : item index
3010 * [I] direction : Direction of shift, +1 or -1.
3011 *
3012 * RETURN:
3013 * None
3014 */
3015 static void LISTVIEW_ShiftIndices(LISTVIEW_INFO *infoPtr, INT nItem, INT direction)
3016 {
3017 INT nNewFocus;
3018 BOOL bOldChange;
3019
3020 /* temporarily disable change notification while shifting items */
3021 bOldChange = infoPtr->bDoChangeNotify;
3022 infoPtr->bDoChangeNotify = FALSE;
3023
3024 TRACE("Shifting %iu, %i steps\n", nItem, direction);
3025
3026 ranges_shift(infoPtr->selectionRanges, nItem, direction, infoPtr->nItemCount);
3027
3028 assert(abs(direction) == 1);
3029
3030 infoPtr->nSelectionMark = shift_item(infoPtr, infoPtr->nSelectionMark, nItem, direction);
3031
3032 nNewFocus = shift_item(infoPtr, infoPtr->nFocusedItem, nItem, direction);
3033 if (nNewFocus != infoPtr->nFocusedItem)
3034 LISTVIEW_SetItemFocus(infoPtr, nNewFocus);
3035
3036 /* But we are not supposed to modify nHotItem! */
3037
3038 infoPtr->bDoChangeNotify = bOldChange;
3039 }
3040
3041
3042 /**
3043 * DESCRIPTION:
3044 * Adds a block of selections.
3045 *
3046 * PARAMETER(S):
3047 * [I] infoPtr : valid pointer to the listview structure
3048 * [I] nItem : item index
3049 *
3050 * RETURN:
3051 * Whether the window is still valid.
3052 */
3053 static BOOL LISTVIEW_AddGroupSelection(LISTVIEW_INFO *infoPtr, INT nItem)
3054 {
3055 INT nFirst = min(infoPtr->nSelectionMark, nItem);
3056 INT nLast = max(infoPtr->nSelectionMark, nItem);
3057 HWND hwndSelf = infoPtr->hwndSelf;
3058 NMLVODSTATECHANGE nmlv;
3059 LVITEMW item;
3060 BOOL bOldChange;
3061 INT i;
3062
3063 /* Temporarily disable change notification
3064 * If the control is LVS_OWNERDATA, we need to send
3065 * only one LVN_ODSTATECHANGED notification.
3066 * See MSDN documentation for LVN_ITEMCHANGED.
3067 */
3068 bOldChange = infoPtr->bDoChangeNotify;
3069 if (infoPtr->dwStyle & LVS_OWNERDATA) infoPtr->bDoChangeNotify = FALSE;
3070
3071 if (nFirst == -1) nFirst = nItem;
3072
3073 item.state = LVIS_SELECTED;
3074 item.stateMask = LVIS_SELECTED;
3075
3076 for (i = nFirst; i <= nLast; i++)
3077 LISTVIEW_SetItemState(infoPtr,i,&item);
3078
3079 ZeroMemory(&nmlv, sizeof(nmlv));
3080 nmlv.iFrom = nFirst;
3081 nmlv.iTo = nLast;
3082 nmlv.uNewState = 0;
3083 nmlv.uOldState = item.state;
3084
3085 notify_hdr(infoPtr, LVN_ODSTATECHANGED, (LPNMHDR)&nmlv);
3086 if (!IsWindow(hwndSelf))
3087 return FALSE;
3088 infoPtr->bDoChangeNotify = bOldChange;
3089 return TRUE;
3090 }
3091
3092
3093 /***
3094 * DESCRIPTION:
3095 * Sets a single group selection.
3096 *
3097 * PARAMETER(S):
3098 * [I] infoPtr : valid pointer to the listview structure
3099 * [I] nItem : item index
3100 *
3101 * RETURN:
3102 * None
3103 */
3104 static void LISTVIEW_SetGroupSelection(LISTVIEW_INFO *infoPtr, INT nItem)
3105 {
3106 UINT uView = infoPtr->dwStyle & LVS_TYPEMASK;
3107 RANGES selection;
3108 LVITEMW item;
3109 ITERATOR i;
3110 BOOL bOldChange;
3111
3112 if (!(selection = ranges_create(100))) return;
3113
3114 item.state = LVIS_SELECTED;
3115 item.stateMask = LVIS_SELECTED;
3116
3117 if ((uView == LVS_LIST) || (uView == LVS_REPORT))
3118 {
3119 if (infoPtr->nSelectionMark == -1)
3120 {
3121 infoPtr->nSelectionMark = nItem;
3122 ranges_additem(selection, nItem);
3123 }
3124 else
3125 {
3126 RANGE sel;
3127
3128 sel.lower = min(infoPtr->nSelectionMark, nItem);
3129 sel.upper = max(infoPtr->nSelectionMark, nItem) + 1;
3130 ranges_add(selection, sel);
3131 }
3132 }
3133 else
3134 {
3135 RECT rcItem, rcSel, rcSelMark;
3136 POINT ptItem;
3137
3138 rcItem.left = LVIR_BOUNDS;
3139 if (!LISTVIEW_GetItemRect(infoPtr, nItem, &rcItem)) return;
3140 rcSelMark.left = LVIR_BOUNDS;
3141 if (!LISTVIEW_GetItemRect(infoPtr, infoPtr->nSelectionMark, &rcSelMark)) return;
3142 UnionRect(&rcSel, &rcItem, &rcSelMark);
3143 iterator_frameditems(&i, infoPtr, &rcSel);
3144 while(iterator_next(&i))
3145 {
3146 LISTVIEW_GetItemPosition(infoPtr, i.nItem, &ptItem);
3147 if (PtInRect(&rcSel, ptItem)) ranges_additem(selection, i.nItem);
3148 }
3149 iterator_destroy(&i);
3150 }
3151
3152 bOldChange = infoPtr->bDoChangeNotify;
3153 infoPtr->bDoChangeNotify = FALSE;
3154
3155 LISTVIEW_DeselectAllSkipItems(infoPtr, selection);
3156
3157
3158 iterator_rangesitems(&i, selection);
3159 while(iterator_next(&i))
3160 LISTVIEW_SetItemState(infoPtr, i.nItem, &item);
3161 /* this will also destroy the selection */
3162 iterator_destroy(&i);
3163
3164 infoPtr->bDoChangeNotify = bOldChange;
3165
3166 LISTVIEW_SetItemFocus(infoPtr, nItem);
3167 }
3168
3169 /***
3170 * DESCRIPTION:
3171 * Sets a single selection.
3172 *
3173 * PARAMETER(S):
3174 * [I] infoPtr : valid pointer to the listview structure
3175 * [I] nItem : item index
3176 *
3177 * RETURN:
3178 * None
3179 */
3180 static void LISTVIEW_SetSelection(LISTVIEW_INFO *infoPtr, INT nItem)
3181 {
3182 LVITEMW lvItem;
3183
3184 TRACE("nItem=%d\n", nItem);
3185
3186 LISTVIEW_DeselectAllSkipItem(infoPtr, nItem);
3187
3188 lvItem.state = LVIS_FOCUSED | LVIS_SELECTED;
3189 lvItem.stateMask = LVIS_FOCUSED | LVIS_SELECTED;
3190 LISTVIEW_SetItemState(infoPtr, nItem, &lvItem);
3191
3192 infoPtr->nSelectionMark = nItem;
3193 }
3194
3195 /***
3196 * DESCRIPTION:
3197 * Set selection(s) with keyboard.
3198 *
3199 * PARAMETER(S):
3200 * [I] infoPtr : valid pointer to the listview structure
3201 * [I] nItem : item index
3202 *
3203 * RETURN:
3204 * SUCCESS : TRUE (needs to be repainted)
3205 * FAILURE : FALSE (nothing has changed)
3206 */
3207 static BOOL LISTVIEW_KeySelection(LISTVIEW_INFO *infoPtr, INT nItem)
3208 {
3209 /* FIXME: pass in the state */
3210 WORD wShift = HIWORD(GetKeyState(VK_SHIFT));
3211 WORD wCtrl = HIWORD(GetKeyState(VK_CONTROL));
3212 BOOL bResult = FALSE;
3213
3214 TRACE("nItem=%d, wShift=%d, wCtrl=%d\n", nItem, wShift, wCtrl);
3215 if ((nItem >= 0) && (nItem < infoPtr->nItemCount))
3216 {
3217 if (infoPtr->dwStyle & LVS_SINGLESEL)
3218 {
3219 bResult = TRUE;
3220 LISTVIEW_SetSelection(infoPtr, nItem);
3221 }
3222 else
3223 {
3224 if (wShift)
3225 {
3226 bResult = TRUE;
3227 LISTVIEW_SetGroupSelection(infoPtr, nItem);
3228 }
3229 else if (wCtrl)
3230 {
3231 LVITEMW lvItem;
3232 lvItem.state = ~LISTVIEW_GetItemState(infoPtr, nItem, LVIS_SELECTED);
3233 lvItem.stateMask = LVIS_SELECTED;
3234 LISTVIEW_SetItemState(infoPtr, nItem, &lvItem);
3235
3236 if (lvItem.state & LVIS_SELECTED)
3237 infoPtr->nSelectionMark = nItem;
3238
3239 bResult = LISTVIEW_SetItemFocus(infoPtr, nItem);
3240 }
3241 else
3242 {
3243 bResult = TRUE;
3244 LISTVIEW_SetSelection(infoPtr, nItem);
3245 }
3246 }
3247 LISTVIEW_EnsureVisible(infoPtr, nItem, FALSE);
3248 }
3249
3250 UpdateWindow(infoPtr->hwndSelf); /* update client area */
3251 return bResult;
3252 }
3253
3254 static BOOL LISTVIEW_GetItemAtPt(const LISTVIEW_INFO *infoPtr, LPLVITEMW lpLVItem, POINT pt)
3255 {
3256 LVHITTESTINFO lvHitTestInfo;
3257
3258 ZeroMemory(&lvHitTestInfo, sizeof(lvHitTestInfo));
3259 lvHitTestInfo.pt.x = pt.x;
3260 lvHitTestInfo.pt.y = pt.y;
3261
3262 LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, FALSE);
3263
3264 lpLVItem->mask = LVIF_PARAM;
3265 lpLVItem->iItem = lvHitTestInfo.iItem;
3266 lpLVItem->iSubItem = 0;
3267
3268 return LISTVIEW_GetItemT(infoPtr, lpLVItem, TRUE);
3269 }
3270
3271 static inline BOOL LISTVIEW_isHotTracking(const LISTVIEW_INFO *infoPtr)
3272 {
3273 return ((infoPtr->dwLvExStyle & LVS_EX_TRACKSELECT) ||
3274 (infoPtr->dwLvExStyle & LVS_EX_ONECLICKACTIVATE) ||
3275 (infoPtr->dwLvExStyle & LVS_EX_TWOCLICKACTIVATE));
3276 }
3277
3278 /***
3279 * DESCRIPTION:
3280 * Called when the mouse is being actively tracked and has hovered for a specified
3281 * amount of time
3282 *
3283 * PARAMETER(S):
3284 * [I] infoPtr : valid pointer to the listview structure
3285 * [I] fwKeys : key indicator
3286 * [I] x,y : mouse position
3287 *
3288 * RETURN:
3289 * 0 if the message was processed, non-zero if there was an error
3290 *
3291 * INFO:
3292 * LVS_EX_TRACKSELECT: An item is automatically selected when the cursor remains
3293 * over the item for a certain period of time.
3294 *
3295 */
3296 static LRESULT LISTVIEW_MouseHover(LISTVIEW_INFO *infoPtr, WORD fwKeys, INT x, INT y)
3297 {
3298 if (LISTVIEW_isHotTracking(infoPtr))
3299 {
3300 LVITEMW item;
3301 POINT pt;
3302
3303 pt.x = x;
3304 pt.y = y;
3305
3306 if (LISTVIEW_GetItemAtPt(infoPtr, &item, pt))
3307 LISTVIEW_SetSelection(infoPtr, item.iItem);
3308 }
3309
3310 return 0;
3311 }
3312
3313 /***
3314 * DESCRIPTION:
3315 * Called whenever WM_MOUSEMOVE is received.
3316 *
3317 * PARAMETER(S):
3318 * [I] infoPtr : valid pointer to the listview structure
3319 * [I] fwKeys : key indicator
3320 * [I] x,y : mouse position
3321 *
3322 * RETURN:
3323 * 0 if the message is processed, non-zero if there was an error
3324 */
3325 static LRESULT LISTVIEW_MouseMove(LISTVIEW_INFO *infoPtr, WORD fwKeys, INT x, INT y)
3326 {
3327 TRACKMOUSEEVENT trackinfo;
3328
3329 if (!(fwKeys & MK_LBUTTON))
3330 infoPtr->bLButtonDown = FALSE;
3331
3332 if (infoPtr->bLButtonDown)
3333 {
3334 POINT tmp;
3335 RECT rect;
3336 WORD wDragWidth = GetSystemMetrics(SM_CXDRAG);
3337 WORD wDragHeight= GetSystemMetrics(SM_CYDRAG);
3338
3339 rect.left = infoPtr->ptClickPos.x - wDragWidth;
3340 rect.right = infoPtr->ptClickPos.x + wDragWidth;
3341 rect.top = infoPtr->ptClickPos.y - wDragHeight;
3342 rect.bottom = infoPtr->ptClickPos.y + wDragHeight;
3343
3344 tmp.x = x;
3345 tmp.y = y;
3346
3347 if (!PtInRect(&rect, tmp))
3348 {
3349 LVHITTESTINFO lvHitTestInfo;
3350 NMLISTVIEW nmlv;
3351
3352 lvHitTestInfo.pt = infoPtr->ptClickPos;
3353 LISTVIEW_HitTest(infoPtr, &lvHitTestInfo, TRUE, TRUE);
3354
3355 ZeroMemory(&nmlv, sizeof(nmlv));
3356 nmlv.iItem = lvHitTestInfo.iItem;
3357 nmlv.ptAction = infoPtr->ptClickPos;
3358
3359 if (!infoPtr->bDragging)
3360 {
3361 notify_listview(infoPtr, LVN_BEGINDRAG, &nmlv);
3362 infoPtr->bDragging = TRUE;
3363 }
3364
3365 return 0;
3366 }
3367 }
3368 else
3369 infoPtr->bLButtonDown = FALSE;
3370
3371 /* see if we are supposed to be tracking mouse hovering */
3372 if (LISTVIEW_isHotTracking(infoPtr)) {
3373 /* fill in the trackinfo struct */
3374 trackinfo.cbSize = sizeof(TRACKMOUSEEVENT);
3375 trackinfo.dwFlags = TME_QUERY;
3376 trackinfo.hwndTrack = infoPtr->hwndSelf;
3377 trackinfo.dwHoverTime = infoPtr->dwHoverTime;
3378
3379 /* see if we are already tracking this hwnd */
3380 _TrackMouseEvent(&trackinfo);
3381
3382 if(!(trackinfo.dwFlags & TME_HOVER)) {
3383 trackinfo.dwFlags = TME_HOVER;
3384
3385 /* call TRACKMOUSEEVENT so we receive WM_MOUSEHOVER messages */
3386 _TrackMouseEvent(&trackinfo);
3387 }
3388 }
3389
3390 return 0;
3391 }
3392
3393
3394 /***
3395 * Tests whether the item is assignable to a list with style lStyle
3396 */
3397 static inline BOOL is_assignable_item(const LVITEMW *lpLVItem, LONG lStyle)
3398 {
3399 if ( (lpLVItem->mask & LVIF_TEXT) &&
3400 (lpLVItem->pszText == LPSTR_TEXTCALLBACKW) &&
3401 (lStyle & (LVS_SORTASCENDING | LVS_SORTDESCENDING)) ) return FALSE;
3402
3403 return TRUE;
3404 }