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