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
28 static INT Image_String
;
29 static INT Image_Binary
;
31 /*******************************************************************************
32 * Global and Local Variables:
35 DWORD g_columnToSort
= ~0U;
36 BOOL g_invertSort
= FALSE
;
38 HKEY g_currentRootKey
;
39 static WCHAR g_szValueNotSet
[64];
41 #define MAX_LIST_COLUMNS (IDS_LIST_COLUMN_LAST - IDS_LIST_COLUMN_FIRST + 1)
42 static int default_column_widths
[MAX_LIST_COLUMNS
] = { 200, 175, 400 };
43 static int column_alignment
[MAX_LIST_COLUMNS
] = { LVCFMT_LEFT
, LVCFMT_LEFT
, LVCFMT_LEFT
};
45 LPWSTR
GetItemText(HWND hwndLV
, UINT item
)
48 unsigned int maxLen
= 128;
49 if (item
== 0) return NULL
; /* first item is ALWAYS a default */
51 curStr
= malloc(maxLen
* sizeof(WCHAR
));
53 ListView_GetItemTextW(hwndLV
, item
, 0, curStr
, maxLen
);
54 if (lstrlenW(curStr
) < maxLen
- 1) return curStr
;
56 curStr
= realloc(curStr
, maxLen
* sizeof(WCHAR
));
62 WCHAR
*GetValueName(HWND hwndLV
)
66 item
= SendMessageW(hwndLV
, LVM_GETNEXTITEM
, -1, MAKELPARAM(LVNI_FOCUSED
, 0));
67 if (item
== -1) return NULL
;
69 return GetItemText(hwndLV
, item
);
72 BOOL
update_listview_path(const WCHAR
*path
)
76 g_currentPath
= malloc((lstrlenW(path
) + 1) * sizeof(WCHAR
));
77 lstrcpyW(g_currentPath
, path
);
82 /* convert '\0' separated string list into ',' separated string list */
83 static void MakeMULTISZDisplayable(LPWSTR multi
)
87 for (; *multi
; multi
++)
97 /*******************************************************************************
98 * Local module support methods
100 void format_value_data(HWND hwndLV
, int index
, DWORD type
, void *data
, DWORD size
)
106 ListView_SetItemTextW(hwndLV
, index
, 2, data
? data
: g_szValueNotSet
);
109 case REG_DWORD_BIG_ENDIAN
:
111 DWORD value
= *(DWORD
*)data
;
113 if (type
== REG_DWORD_BIG_ENDIAN
)
114 value
= RtlUlongByteSwap(value
);
115 wsprintfW(buf
, L
"0x%08x (%u)", value
, value
);
116 ListView_SetItemTextW(hwndLV
, index
, 2, buf
);
121 UINT64 value
= *(UINT64
*)data
;
123 swprintf(buf
, ARRAY_SIZE(buf
), L
"0x%08I64x (%I64u)", value
, value
);
124 ListView_SetItemTextW(hwndLV
, index
, 2, buf
);
128 MakeMULTISZDisplayable(data
);
129 ListView_SetItemTextW(hwndLV
, index
, 2, data
);
137 WCHAR
*strBinary
= malloc(size
* sizeof(WCHAR
) * 3 + sizeof(WCHAR
));
138 for (i
= 0; i
< size
; i
++)
139 wsprintfW( strBinary
+ i
*3, L
"%02X ", pData
[i
] );
140 strBinary
[size
* 3] = 0;
141 ListView_SetItemTextW(hwndLV
, index
, 2, strBinary
);
148 int AddEntryToList(HWND hwndLV
, WCHAR
*Name
, DWORD dwValType
, void *ValBuf
, DWORD dwCount
, int pos
)
151 LVITEMW item
= { 0 };
154 linfo
= malloc(sizeof(LINE_INFO
));
155 linfo
->dwValType
= dwValType
;
156 linfo
->val_len
= dwCount
;
160 linfo
->name
= malloc((lstrlenW(Name
) + 1) * sizeof(WCHAR
));
161 lstrcpyW(linfo
->name
, Name
);
163 else linfo
->name
= NULL
;
165 if (ValBuf
&& dwCount
)
167 linfo
->val
= malloc(dwCount
);
168 memcpy(linfo
->val
, ValBuf
, dwCount
);
170 else linfo
->val
= NULL
;
172 item
.mask
= LVIF_TEXT
| LVIF_PARAM
| LVIF_STATE
| LVIF_IMAGE
;
173 item
.iItem
= (pos
== -1) ? SendMessageW(hwndLV
, LVM_GETITEMCOUNT
, 0, 0) : pos
;
174 item
.stateMask
= LVIS_FOCUSED
| LVIS_SELECTED
;
175 item
.pszText
= Name
? Name
: LPSTR_TEXTCALLBACKW
;
176 item
.cchTextMax
= Name
? lstrlenW(item
.pszText
) : 0;
183 item
.iImage
= Image_String
;
186 item
.iImage
= Image_Binary
;
189 item
.lParam
= (LPARAM
)linfo
;
191 if ((index
= ListView_InsertItemW(hwndLV
, &item
)) != -1)
192 format_value_data(hwndLV
, index
, dwValType
, ValBuf
, dwCount
);
196 static BOOL
InitListViewImageList(HWND hWndListView
)
200 INT cx
= GetSystemMetrics(SM_CXSMICON
);
201 INT cy
= GetSystemMetrics(SM_CYSMICON
);
203 himl
= ImageList_Create(cx
, cy
, ILC_MASK
, 0, 2);
207 hicon
= LoadImageW(hInst
, MAKEINTRESOURCEW(IDI_STRING
),
208 IMAGE_ICON
, cx
, cy
, LR_DEFAULTCOLOR
);
209 Image_String
= ImageList_AddIcon(himl
, hicon
);
211 hicon
= LoadImageW(hInst
, MAKEINTRESOURCEW(IDI_BIN
),
212 IMAGE_ICON
, cx
, cy
, LR_DEFAULTCOLOR
);
213 Image_Binary
= ImageList_AddIcon(himl
, hicon
);
215 SendMessageW( hWndListView
, LVM_SETIMAGELIST
, LVSIL_SMALL
, (LPARAM
) himl
);
217 /* fail if some of the icons failed to load */
218 if (ImageList_GetImageCount(himl
) < 2)
224 static BOOL
CreateListColumns(HWND hWndListView
)
230 /* Create columns. */
231 lvC
.mask
= LVCF_FMT
| LVCF_WIDTH
| LVCF_TEXT
| LVCF_SUBITEM
;
232 lvC
.pszText
= szText
;
234 /* Load the column labels from the resource file. */
235 for (index
= 0; index
< MAX_LIST_COLUMNS
; index
++) {
236 lvC
.iSubItem
= index
;
237 lvC
.cx
= default_column_widths
[index
];
238 lvC
.fmt
= column_alignment
[index
];
239 LoadStringW(hInst
, IDS_LIST_COLUMN_FIRST
+ index
, szText
, ARRAY_SIZE(szText
));
240 if (ListView_InsertColumnW(hWndListView
, index
, &lvC
) == -1) return FALSE
;
245 /* OnGetDispInfo - processes the LVN_GETDISPINFO notification message. */
246 void OnGetDispInfo(NMLVDISPINFOW
*plvdi
)
248 static WCHAR buffer
[200];
249 static WCHAR reg_szT
[] = L
"REG_SZ",
250 reg_expand_szT
[] = L
"REG_EXPAND_SZ",
251 reg_binaryT
[] = L
"REG_BINARY",
252 reg_dwordT
[] = L
"REG_DWORD",
253 reg_dword_big_endianT
[] = L
"REG_DWORD_BIG_ENDIAN",
254 reg_qwordT
[] = L
"REG_QWORD",
255 reg_multi_szT
[] = L
"REG_MULTI_SZ",
256 reg_linkT
[] = L
"REG_LINK",
257 reg_resource_listT
[] = L
"REG_RESOURCE_LIST",
258 reg_noneT
[] = L
"REG_NONE",
261 plvdi
->item
.pszText
= NULL
;
262 plvdi
->item
.cchTextMax
= 0;
264 switch (plvdi
->item
.iSubItem
) {
266 plvdi
->item
.pszText
= g_pszDefaultValueName
;
270 DWORD data_type
= ((LINE_INFO
*)plvdi
->item
.lParam
)->dwValType
;
274 plvdi
->item
.pszText
= reg_szT
;
277 plvdi
->item
.pszText
= reg_expand_szT
;
280 plvdi
->item
.pszText
= reg_binaryT
;
283 plvdi
->item
.pszText
= reg_dwordT
;
286 plvdi
->item
.pszText
= reg_qwordT
;
288 case REG_DWORD_BIG_ENDIAN
:
289 plvdi
->item
.pszText
= reg_dword_big_endianT
;
292 plvdi
->item
.pszText
= reg_multi_szT
;
295 plvdi
->item
.pszText
= reg_linkT
;
297 case REG_RESOURCE_LIST
:
298 plvdi
->item
.pszText
= reg_resource_listT
;
301 plvdi
->item
.pszText
= reg_noneT
;
305 wsprintfW(buffer
, L
"0x%x", data_type
);
306 plvdi
->item
.pszText
= buffer
;
313 plvdi
->item
.pszText
= g_szValueNotSet
;
316 plvdi
->item
.pszText
= emptyT
;
321 int CALLBACK
CompareFunc(LPARAM lParam1
, LPARAM lParam2
, LPARAM lParamSort
)
324 l
= (LINE_INFO
*)lParam1
;
325 r
= (LINE_INFO
*)lParam2
;
326 if (!l
->name
) return -1;
327 if (!r
->name
) return +1;
329 if (g_columnToSort
== ~0U)
332 if (g_columnToSort
== 1)
333 return g_invertSort
? (int)r
->dwValType
- (int)l
->dwValType
: (int)l
->dwValType
- (int)r
->dwValType
;
334 if (g_columnToSort
== 2) {
335 /* FIXME: Sort on value */
338 return g_invertSort
? lstrcmpiW(r
->name
, l
->name
) : lstrcmpiW(l
->name
, r
->name
);
341 HWND
StartValueRename(HWND hwndLV
)
345 item
= SendMessageW(hwndLV
, LVM_GETNEXTITEM
, -1, MAKELPARAM(LVNI_FOCUSED
| LVNI_SELECTED
, 0));
346 if (item
< 1) { /* cannot rename default key */
347 MessageBeep(MB_ICONHAND
);
350 return (HWND
)SendMessageW(hwndLV
, LVM_EDITLABELW
, item
, 0);
353 HWND
CreateListView(HWND hwndParent
, UINT id
)
358 /* prepare strings */
359 LoadStringW(hInst
, IDS_REGISTRY_VALUE_NOT_SET
, g_szValueNotSet
, ARRAY_SIZE(g_szValueNotSet
));
361 /* Get the dimensions of the parent window's client area, and create the list view control. */
362 GetClientRect(hwndParent
, &rcClient
);
363 hwndLV
= CreateWindowExW(WS_EX_CLIENTEDGE
, WC_LISTVIEWW
, L
"List View",
364 WS_VISIBLE
| WS_CHILD
| WS_TABSTOP
| WS_CLIPSIBLINGS
|
365 LVS_REPORT
| LVS_EDITLABELS
,
366 0, 0, rcClient
.right
, rcClient
.bottom
,
367 hwndParent
, ULongToHandle(id
), hInst
, NULL
);
368 if (!hwndLV
) return NULL
;
369 SendMessageW(hwndLV
, LVM_SETUNICODEFORMAT
, TRUE
, 0);
370 SendMessageW(hwndLV
, LVM_SETEXTENDEDLISTVIEWSTYLE
, 0, LVS_EX_FULLROWSELECT
);
372 /* Initialize the image list */
373 if (!InitListViewImageList(hwndLV
)) goto fail
;
374 if (!CreateListColumns(hwndLV
)) goto fail
;
377 DestroyWindow(hwndLV
);
381 BOOL
RefreshListView(HWND hwndLV
, HKEY hKeyRoot
, LPCWSTR keyPath
, LPCWSTR highlightValue
)
384 DWORD max_sub_key_len
;
385 DWORD max_val_name_len
, valNameLen
;
386 DWORD max_val_size
, valSize
;
387 DWORD val_count
, index
, valType
;
394 if (!hwndLV
) return FALSE
;
396 SendMessageW(hwndLV
, WM_SETREDRAW
, FALSE
, 0);
398 errCode
= RegOpenKeyExW(hKeyRoot
, keyPath
, 0, KEY_READ
, &hKey
);
399 if (errCode
!= ERROR_SUCCESS
) goto done
;
401 g_columnToSort
= ~0U;
402 SendMessageW(hwndLV
, LVM_DELETEALLITEMS
, 0, 0);
404 /* get size information and resize the buffers if necessary */
405 errCode
= RegQueryInfoKeyW(hKey
, NULL
, NULL
, NULL
, NULL
, &max_sub_key_len
, NULL
,
406 &val_count
, &max_val_name_len
, &max_val_size
, NULL
, NULL
);
407 if (errCode
!= ERROR_SUCCESS
) goto done
;
409 /* account for the terminator char */
413 valName
= malloc(max_val_name_len
* sizeof(WCHAR
));
414 valBuf
= malloc(max_val_size
);
416 valSize
= max_val_size
;
417 if (RegQueryValueExW(hKey
, NULL
, NULL
, &valType
, valBuf
, &valSize
) == ERROR_FILE_NOT_FOUND
) {
418 AddEntryToList(hwndLV
, NULL
, REG_SZ
, NULL
, 0, -1);
420 for(index
= 0; index
< val_count
; index
++) {
421 valNameLen
= max_val_name_len
;
422 valSize
= max_val_size
;
424 errCode
= RegEnumValueW(hKey
, index
, valName
, &valNameLen
, NULL
, &valType
, valBuf
, &valSize
);
425 if (errCode
!= ERROR_SUCCESS
) goto done
;
427 AddEntryToList(hwndLV
, valName
[0] ? valName
: NULL
, valType
, valBuf
, valSize
, -1);
430 memset(&item
, 0, sizeof(item
));
433 item
.state
= item
.stateMask
= LVIS_FOCUSED
;
434 SendMessageW(hwndLV
, LVM_SETITEMSTATE
, 0, (LPARAM
)&item
);
437 SendMessageW(hwndLV
, LVM_SORTITEMS
, (WPARAM
)hwndLV
, (LPARAM
)CompareFunc
);
439 g_currentRootKey
= hKeyRoot
;
440 if (keyPath
!= g_currentPath
&& !update_listview_path(keyPath
))
448 SendMessageW(hwndLV
, WM_SETREDRAW
, TRUE
, 0);
449 if (hKey
) RegCloseKey(hKey
);