ntdll: Implement RtlIsEcCode().
[wine.git] / programs / regedit / listview.c
blobc7a301929c2471d2411676ceb1f6bfa77139fa2e
1 /*
2 * Regedit listviews
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
22 #include <windows.h>
23 #include <winternl.h>
24 #include <commctrl.h>
26 #include "main.h"
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;
37 WCHAR *g_currentPath;
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)
47 WCHAR *curStr;
48 unsigned int maxLen = 128;
49 if (item == 0) return NULL; /* first item is ALWAYS a default */
51 curStr = malloc(maxLen * sizeof(WCHAR));
52 do {
53 ListView_GetItemTextW(hwndLV, item, 0, curStr, maxLen);
54 if (lstrlenW(curStr) < maxLen - 1) return curStr;
55 maxLen *= 2;
56 curStr = realloc(curStr, maxLen * sizeof(WCHAR));
57 } while (TRUE);
58 free(curStr);
59 return NULL;
62 WCHAR *GetValueName(HWND hwndLV)
64 INT item;
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)
74 free(g_currentPath);
76 g_currentPath = malloc((lstrlenW(path) + 1) * sizeof(WCHAR));
77 lstrcpyW(g_currentPath, path);
79 return TRUE;
82 /* convert '\0' separated string list into ',' separated string list */
83 static void MakeMULTISZDisplayable(LPWSTR multi)
87 for (; *multi; multi++)
89 if (*(multi+1))
91 *multi = ',';
92 multi++;
94 } while (*multi);
97 /*******************************************************************************
98 * Local module support methods
100 void format_value_data(HWND hwndLV, int index, DWORD type, void *data, DWORD size)
102 switch (type)
104 case REG_SZ:
105 case REG_EXPAND_SZ:
106 ListView_SetItemTextW(hwndLV, index, 2, data ? data : g_szValueNotSet);
107 break;
108 case REG_DWORD:
109 case REG_DWORD_BIG_ENDIAN:
111 DWORD value = *(DWORD *)data;
112 WCHAR buf[64];
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);
117 break;
119 case REG_QWORD:
121 UINT64 value = *(UINT64 *)data;
122 WCHAR buf[64];
123 swprintf(buf, ARRAY_SIZE(buf), L"0x%08I64x (%I64u)", value, value);
124 ListView_SetItemTextW(hwndLV, index, 2, buf);
125 break;
127 case REG_MULTI_SZ:
128 MakeMULTISZDisplayable(data);
129 ListView_SetItemTextW(hwndLV, index, 2, data);
130 break;
131 case REG_BINARY:
132 case REG_NONE:
133 default:
135 unsigned int i;
136 BYTE *pData = 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);
142 free(strBinary);
143 break;
148 int AddEntryToList(HWND hwndLV, WCHAR *Name, DWORD dwValType, void *ValBuf, DWORD dwCount, int pos)
150 LINE_INFO *linfo;
151 LVITEMW item = { 0 };
152 int index;
154 linfo = malloc(sizeof(LINE_INFO));
155 linfo->dwValType = dwValType;
156 linfo->val_len = dwCount;
158 if (Name)
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;
178 switch (dwValType)
180 case REG_SZ:
181 case REG_EXPAND_SZ:
182 case REG_MULTI_SZ:
183 item.iImage = Image_String;
184 break;
185 default:
186 item.iImage = Image_Binary;
187 break;
189 item.lParam = (LPARAM)linfo;
191 if ((index = ListView_InsertItemW(hwndLV, &item)) != -1)
192 format_value_data(hwndLV, index, dwValType, ValBuf, dwCount);
193 return index;
196 static BOOL InitListViewImageList(HWND hWndListView)
198 HIMAGELIST himl;
199 HICON hicon;
200 INT cx = GetSystemMetrics(SM_CXSMICON);
201 INT cy = GetSystemMetrics(SM_CYSMICON);
203 himl = ImageList_Create(cx, cy, ILC_MASK, 0, 2);
204 if (!himl)
205 return FALSE;
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)
219 return FALSE;
221 return TRUE;
224 static BOOL CreateListColumns(HWND hWndListView)
226 WCHAR szText[50];
227 int index;
228 LVCOLUMNW lvC;
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;
242 return TRUE;
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",
259 emptyT[] = L"";
261 plvdi->item.pszText = NULL;
262 plvdi->item.cchTextMax = 0;
264 switch (plvdi->item.iSubItem) {
265 case 0:
266 plvdi->item.pszText = g_pszDefaultValueName;
267 break;
268 case 1:
270 DWORD data_type = ((LINE_INFO *)plvdi->item.lParam)->dwValType;
272 switch (data_type) {
273 case REG_SZ:
274 plvdi->item.pszText = reg_szT;
275 break;
276 case REG_EXPAND_SZ:
277 plvdi->item.pszText = reg_expand_szT;
278 break;
279 case REG_BINARY:
280 plvdi->item.pszText = reg_binaryT;
281 break;
282 case REG_DWORD:
283 plvdi->item.pszText = reg_dwordT;
284 break;
285 case REG_QWORD:
286 plvdi->item.pszText = reg_qwordT;
287 break;
288 case REG_DWORD_BIG_ENDIAN:
289 plvdi->item.pszText = reg_dword_big_endianT;
290 break;
291 case REG_MULTI_SZ:
292 plvdi->item.pszText = reg_multi_szT;
293 break;
294 case REG_LINK:
295 plvdi->item.pszText = reg_linkT;
296 break;
297 case REG_RESOURCE_LIST:
298 plvdi->item.pszText = reg_resource_listT;
299 break;
300 case REG_NONE:
301 plvdi->item.pszText = reg_noneT;
302 break;
303 default:
305 wsprintfW(buffer, L"0x%x", data_type);
306 plvdi->item.pszText = buffer;
307 break;
310 break;
312 case 2:
313 plvdi->item.pszText = g_szValueNotSet;
314 break;
315 case 3:
316 plvdi->item.pszText = emptyT;
317 break;
321 int CALLBACK CompareFunc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
323 LINE_INFO*l, *r;
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)
330 g_columnToSort = 0;
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 */
336 return 0;
338 return g_invertSort ? lstrcmpiW(r->name, l->name) : lstrcmpiW(l->name, r->name);
341 HWND StartValueRename(HWND hwndLV)
343 int item;
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);
348 return 0;
350 return (HWND)SendMessageW(hwndLV, LVM_EDITLABELW, item, 0);
353 HWND CreateListView(HWND hwndParent, UINT id)
355 RECT rcClient;
356 HWND hwndLV;
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;
375 return hwndLV;
376 fail:
377 DestroyWindow(hwndLV);
378 return NULL;
381 BOOL RefreshListView(HWND hwndLV, HKEY hKeyRoot, LPCWSTR keyPath, LPCWSTR highlightValue)
383 BOOL result = FALSE;
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;
388 WCHAR* valName = 0;
389 BYTE* valBuf = 0;
390 HKEY hKey = 0;
391 LONG errCode;
392 LVITEMW item;
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 */
410 max_val_name_len++;
411 max_val_size++;
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;
423 valType = 0;
424 errCode = RegEnumValueW(hKey, index, valName, &valNameLen, NULL, &valType, valBuf, &valSize);
425 if (errCode != ERROR_SUCCESS) goto done;
426 valBuf[valSize] = 0;
427 AddEntryToList(hwndLV, valName[0] ? valName : NULL, valType, valBuf, valSize, -1);
430 memset(&item, 0, sizeof(item));
431 if (!highlightValue)
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))
441 goto done;
443 result = TRUE;
445 done:
446 free(valBuf);
447 free(valName);
448 SendMessageW(hwndLV, WM_SETREDRAW, TRUE, 0);
449 if (hKey) RegCloseKey(hKey);
451 return result;