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