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