4 * Copyright (C) 2002 Robert Dickenson <robd@reactos.org>
5 * Copyright (C) 2008 Alexander N. Sørnes <alex@thehandofagony.com>
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
30 #include "wine/unicode.h"
32 static INT Image_String
;
33 static INT Image_Binary
;
35 /*******************************************************************************
36 * Global and Local Variables:
39 DWORD g_columnToSort
= ~0U;
40 BOOL g_invertSort
= FALSE
;
42 HKEY g_currentRootKey
;
43 static WCHAR
*g_valueName
;
44 static WCHAR g_szValueNotSet
[64];
46 #define MAX_LIST_COLUMNS (IDS_LIST_COLUMN_LAST - IDS_LIST_COLUMN_FIRST + 1)
47 static int default_column_widths
[MAX_LIST_COLUMNS
] = { 200, 175, 400 };
48 static int column_alignment
[MAX_LIST_COLUMNS
] = { LVCFMT_LEFT
, LVCFMT_LEFT
, LVCFMT_LEFT
};
50 LPWSTR
GetItemText(HWND hwndLV
, UINT item
)
53 unsigned int maxLen
= 128;
54 if (item
== 0) return NULL
; /* first item is ALWAYS a default */
56 curStr
= heap_xalloc(maxLen
* sizeof(WCHAR
));
58 ListView_GetItemTextW(hwndLV
, item
, 0, curStr
, maxLen
);
59 if (lstrlenW(curStr
) < maxLen
- 1) return curStr
;
61 curStr
= heap_xrealloc(curStr
, maxLen
* sizeof(WCHAR
));
67 LPCWSTR
GetValueName(HWND hwndLV
)
71 if (g_valueName
!= LPSTR_TEXTCALLBACKW
)
72 heap_free(g_valueName
);
75 item
= SendMessageW(hwndLV
, LVM_GETNEXTITEM
, -1, MAKELPARAM(LVNI_FOCUSED
, 0));
76 if (item
== -1) return NULL
;
78 g_valueName
= GetItemText(hwndLV
, item
);
83 BOOL
update_listview_path(const WCHAR
*path
)
85 heap_free(g_currentPath
);
87 g_currentPath
= heap_xalloc((lstrlenW(path
) + 1) * sizeof(WCHAR
));
88 lstrcpyW(g_currentPath
, path
);
93 /* convert '\0' separated string list into ',' separated string list */
94 static void MakeMULTISZDisplayable(LPWSTR multi
)
98 for (; *multi
; multi
++)
108 /*******************************************************************************
109 * Local module support methods
111 void format_value_data(HWND hwndLV
, int index
, DWORD type
, void *data
, DWORD size
)
117 ListView_SetItemTextW(hwndLV
, index
, 2, data
? data
: g_szValueNotSet
);
120 case REG_DWORD_BIG_ENDIAN
:
122 DWORD value
= *(DWORD
*)data
;
124 WCHAR format
[] = {'0','x','%','0','8','x',' ','(','%','u',')',0};
125 if (type
== REG_DWORD_BIG_ENDIAN
)
126 value
= RtlUlongByteSwap(value
);
127 wsprintfW(buf
, format
, value
, value
);
128 ListView_SetItemTextW(hwndLV
, index
, 2, buf
);
132 MakeMULTISZDisplayable(data
);
133 ListView_SetItemTextW(hwndLV
, index
, 2, data
);
141 WCHAR
*strBinary
= heap_xalloc(size
* sizeof(WCHAR
) * 3 + sizeof(WCHAR
));
142 WCHAR format
[] = {'%','0','2','X',' ',0};
143 for (i
= 0; i
< size
; i
++)
144 wsprintfW( strBinary
+ i
*3, format
, pData
[i
] );
145 strBinary
[size
* 3] = 0;
146 ListView_SetItemTextW(hwndLV
, index
, 2, strBinary
);
147 heap_free(strBinary
);
153 int AddEntryToList(HWND hwndLV
, WCHAR
*Name
, DWORD dwValType
, void *ValBuf
, DWORD dwCount
, int pos
)
156 LVITEMW item
= { 0 };
159 linfo
= heap_xalloc(sizeof(LINE_INFO
));
160 linfo
->dwValType
= dwValType
;
161 linfo
->val_len
= dwCount
;
165 linfo
->name
= heap_xalloc((lstrlenW(Name
) + 1) * sizeof(WCHAR
));
166 lstrcpyW(linfo
->name
, Name
);
168 else linfo
->name
= NULL
;
170 if (ValBuf
&& dwCount
)
172 linfo
->val
= heap_xalloc(dwCount
);
173 memcpy(linfo
->val
, ValBuf
, dwCount
);
175 else linfo
->val
= NULL
;
177 item
.mask
= LVIF_TEXT
| LVIF_PARAM
| LVIF_STATE
| LVIF_IMAGE
;
178 item
.iItem
= (pos
== -1) ? SendMessageW(hwndLV
, LVM_GETITEMCOUNT
, 0, 0) : pos
;
179 item
.stateMask
= LVIS_FOCUSED
| LVIS_SELECTED
;
180 item
.pszText
= Name
? Name
: LPSTR_TEXTCALLBACKW
;
181 item
.cchTextMax
= Name
? lstrlenW(item
.pszText
) : 0;
188 item
.iImage
= Image_String
;
191 item
.iImage
= Image_Binary
;
194 item
.lParam
= (LPARAM
)linfo
;
196 if ((index
= ListView_InsertItemW(hwndLV
, &item
)) != -1)
197 format_value_data(hwndLV
, index
, dwValType
, ValBuf
, dwCount
);
201 static BOOL
InitListViewImageList(HWND hWndListView
)
205 INT cx
= GetSystemMetrics(SM_CXSMICON
);
206 INT cy
= GetSystemMetrics(SM_CYSMICON
);
208 himl
= ImageList_Create(cx
, cy
, ILC_MASK
, 0, 2);
212 hicon
= LoadImageW(hInst
, MAKEINTRESOURCEW(IDI_STRING
),
213 IMAGE_ICON
, cx
, cy
, LR_DEFAULTCOLOR
);
214 Image_String
= ImageList_AddIcon(himl
, hicon
);
216 hicon
= LoadImageW(hInst
, MAKEINTRESOURCEW(IDI_BIN
),
217 IMAGE_ICON
, cx
, cy
, LR_DEFAULTCOLOR
);
218 Image_Binary
= ImageList_AddIcon(himl
, hicon
);
220 SendMessageW( hWndListView
, LVM_SETIMAGELIST
, LVSIL_SMALL
, (LPARAM
) himl
);
222 /* fail if some of the icons failed to load */
223 if (ImageList_GetImageCount(himl
) < 2)
229 static BOOL
CreateListColumns(HWND hWndListView
)
235 /* Create columns. */
236 lvC
.mask
= LVCF_FMT
| LVCF_WIDTH
| LVCF_TEXT
| LVCF_SUBITEM
;
237 lvC
.pszText
= szText
;
239 /* Load the column labels from the resource file. */
240 for (index
= 0; index
< MAX_LIST_COLUMNS
; index
++) {
241 lvC
.iSubItem
= index
;
242 lvC
.cx
= default_column_widths
[index
];
243 lvC
.fmt
= column_alignment
[index
];
244 LoadStringW(hInst
, IDS_LIST_COLUMN_FIRST
+ index
, szText
, sizeof(szText
)/sizeof(WCHAR
));
245 if (ListView_InsertColumnW(hWndListView
, index
, &lvC
) == -1) return FALSE
;
250 /* OnGetDispInfo - processes the LVN_GETDISPINFO notification message. */
251 void OnGetDispInfo(NMLVDISPINFOW
*plvdi
)
253 static WCHAR buffer
[200];
254 static WCHAR reg_szT
[] = {'R','E','G','_','S','Z',0},
255 reg_expand_szT
[] = {'R','E','G','_','E','X','P','A','N','D','_','S','Z',0},
256 reg_binaryT
[] = {'R','E','G','_','B','I','N','A','R','Y',0},
257 reg_dwordT
[] = {'R','E','G','_','D','W','O','R','D',0},
258 reg_dword_big_endianT
[] = {'R','E','G','_','D','W','O','R','D','_',
259 'B','I','G','_','E','N','D','I','A','N',0},
260 reg_multi_szT
[] = {'R','E','G','_','M','U','L','T','I','_','S','Z',0},
261 reg_linkT
[] = {'R','E','G','_','L','I','N','K',0},
262 reg_resource_listT
[] = {'R','E','G','_','R','E','S','O','U','R','C','E','_','L','I','S','T',0},
263 reg_noneT
[] = {'R','E','G','_','N','O','N','E',0},
266 plvdi
->item
.pszText
= NULL
;
267 plvdi
->item
.cchTextMax
= 0;
269 switch (plvdi
->item
.iSubItem
) {
271 plvdi
->item
.pszText
= g_pszDefaultValueName
;
275 DWORD data_type
= ((LINE_INFO
*)plvdi
->item
.lParam
)->dwValType
;
279 plvdi
->item
.pszText
= reg_szT
;
282 plvdi
->item
.pszText
= reg_expand_szT
;
285 plvdi
->item
.pszText
= reg_binaryT
;
288 plvdi
->item
.pszText
= reg_dwordT
;
290 case REG_DWORD_BIG_ENDIAN
:
291 plvdi
->item
.pszText
= reg_dword_big_endianT
;
294 plvdi
->item
.pszText
= reg_multi_szT
;
297 plvdi
->item
.pszText
= reg_linkT
;
299 case REG_RESOURCE_LIST
:
300 plvdi
->item
.pszText
= reg_resource_listT
;
303 plvdi
->item
.pszText
= reg_noneT
;
307 WCHAR fmt
[] = {'0','x','%','x',0};
308 wsprintfW(buffer
, fmt
, data_type
);
309 plvdi
->item
.pszText
= buffer
;
316 plvdi
->item
.pszText
= g_szValueNotSet
;
319 plvdi
->item
.pszText
= emptyT
;
324 int CALLBACK
CompareFunc(LPARAM lParam1
, LPARAM lParam2
, LPARAM lParamSort
)
327 l
= (LINE_INFO
*)lParam1
;
328 r
= (LINE_INFO
*)lParam2
;
329 if (!l
->name
) return -1;
330 if (!r
->name
) return +1;
332 if (g_columnToSort
== ~0U)
335 if (g_columnToSort
== 1)
336 return g_invertSort
? (int)r
->dwValType
- (int)l
->dwValType
: (int)l
->dwValType
- (int)r
->dwValType
;
337 if (g_columnToSort
== 2) {
338 /* FIXME: Sort on value */
341 return g_invertSort
? lstrcmpiW(r
->name
, l
->name
) : lstrcmpiW(l
->name
, r
->name
);
344 HWND
StartValueRename(HWND hwndLV
)
348 item
= SendMessageW(hwndLV
, LVM_GETNEXTITEM
, -1, MAKELPARAM(LVNI_FOCUSED
| LVNI_SELECTED
, 0));
349 if (item
< 1) { /* cannot rename default key */
350 MessageBeep(MB_ICONHAND
);
353 return (HWND
)SendMessageW(hwndLV
, LVM_EDITLABELW
, item
, 0);
356 HWND
CreateListView(HWND hwndParent
, UINT id
)
360 WCHAR ListView
[] = {'L','i','s','t',' ','V','i','e','w',0};
362 /* prepare strings */
363 LoadStringW(hInst
, IDS_REGISTRY_VALUE_NOT_SET
, g_szValueNotSet
, COUNT_OF(g_szValueNotSet
));
365 /* Get the dimensions of the parent window's client area, and create the list view control. */
366 GetClientRect(hwndParent
, &rcClient
);
367 hwndLV
= CreateWindowExW(WS_EX_CLIENTEDGE
, WC_LISTVIEWW
, ListView
,
368 WS_VISIBLE
| WS_CHILD
| WS_TABSTOP
| LVS_REPORT
| LVS_EDITLABELS
,
369 0, 0, rcClient
.right
, rcClient
.bottom
,
370 hwndParent
, ULongToHandle(id
), hInst
, NULL
);
371 if (!hwndLV
) return NULL
;
372 SendMessageW(hwndLV
, LVM_SETUNICODEFORMAT
, TRUE
, 0);
373 SendMessageW(hwndLV
, LVM_SETEXTENDEDLISTVIEWSTYLE
, 0, LVS_EX_FULLROWSELECT
);
375 /* Initialize the image list */
376 if (!InitListViewImageList(hwndLV
)) goto fail
;
377 if (!CreateListColumns(hwndLV
)) goto fail
;
380 DestroyWindow(hwndLV
);
384 BOOL
RefreshListView(HWND hwndLV
, HKEY hKeyRoot
, LPCWSTR keyPath
, LPCWSTR highlightValue
)
387 DWORD max_sub_key_len
;
388 DWORD max_val_name_len
, valNameLen
;
389 DWORD max_val_size
, valSize
;
390 DWORD val_count
, index
, valType
;
397 if (!hwndLV
) return FALSE
;
399 SendMessageW(hwndLV
, WM_SETREDRAW
, FALSE
, 0);
401 errCode
= RegOpenKeyExW(hKeyRoot
, keyPath
, 0, KEY_READ
, &hKey
);
402 if (errCode
!= ERROR_SUCCESS
) goto done
;
404 g_columnToSort
= ~0U;
405 SendMessageW(hwndLV
, LVM_DELETEALLITEMS
, 0, 0);
407 /* get size information and resize the buffers if necessary */
408 errCode
= RegQueryInfoKeyW(hKey
, NULL
, NULL
, NULL
, NULL
, &max_sub_key_len
, NULL
,
409 &val_count
, &max_val_name_len
, &max_val_size
, NULL
, NULL
);
410 if (errCode
!= ERROR_SUCCESS
) goto done
;
412 /* account for the terminator char */
416 valName
= heap_xalloc(max_val_name_len
* sizeof(WCHAR
));
417 valBuf
= heap_xalloc(max_val_size
);
419 valSize
= max_val_size
;
420 if (RegQueryValueExW(hKey
, NULL
, NULL
, &valType
, valBuf
, &valSize
) == ERROR_FILE_NOT_FOUND
) {
421 AddEntryToList(hwndLV
, NULL
, REG_SZ
, NULL
, 0, -1);
423 for(index
= 0; index
< val_count
; index
++) {
424 valNameLen
= max_val_name_len
;
425 valSize
= max_val_size
;
427 errCode
= RegEnumValueW(hKey
, index
, valName
, &valNameLen
, NULL
, &valType
, valBuf
, &valSize
);
428 if (errCode
!= ERROR_SUCCESS
) goto done
;
430 AddEntryToList(hwndLV
, valName
[0] ? valName
: NULL
, valType
, valBuf
, valSize
, -1);
433 memset(&item
, 0, sizeof(item
));
436 item
.state
= item
.stateMask
= LVIS_FOCUSED
;
437 SendMessageW(hwndLV
, LVM_SETITEMSTATE
, 0, (LPARAM
)&item
);
440 SendMessageW(hwndLV
, LVM_SORTITEMS
, (WPARAM
)hwndLV
, (LPARAM
)CompareFunc
);
442 g_currentRootKey
= hKeyRoot
;
443 if (keyPath
!= g_currentPath
&& !update_listview_path(keyPath
))
451 SendMessageW(hwndLV
, WM_SETREDRAW
, TRUE
, 0);
452 if (hKey
) RegCloseKey(hKey
);