regedit: Display data for all values in the GUI.
[wine.git] / programs / regedit / listview.c
blobb7585645a3ae90350962de889596fdafd5f55b42
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>
25 #include <stdlib.h>
26 #include <stdio.h>
28 #include "main.h"
30 #include "wine/unicode.h"
31 static INT Image_String;
32 static INT Image_Binary;
34 typedef struct tagLINE_INFO
36 DWORD dwValType;
37 LPWSTR name;
38 void* val;
39 size_t val_len;
40 } LINE_INFO;
42 /*******************************************************************************
43 * Global and Local Variables:
46 static WNDPROC g_orgListWndProc;
47 static DWORD g_columnToSort = ~0U;
48 static BOOL g_invertSort = FALSE;
49 static LPWSTR g_valueName;
50 static LPWSTR g_currentPath;
51 static HKEY g_currentRootKey;
52 static WCHAR g_szValueNotSet[64];
54 #define MAX_LIST_COLUMNS (IDS_LIST_COLUMN_LAST - IDS_LIST_COLUMN_FIRST + 1)
55 static int default_column_widths[MAX_LIST_COLUMNS] = { 200, 175, 400 };
56 static int column_alignment[MAX_LIST_COLUMNS] = { LVCFMT_LEFT, LVCFMT_LEFT, LVCFMT_LEFT };
58 LPWSTR GetItemText(HWND hwndLV, UINT item)
60 LPWSTR newStr, curStr;
61 unsigned int maxLen = 128;
62 if (item == 0) return NULL; /* first item is ALWAYS a default */
64 curStr = HeapAlloc(GetProcessHeap(), 0, maxLen * sizeof(WCHAR));
65 if (!curStr) return NULL;
66 do {
67 ListView_GetItemTextW(hwndLV, item, 0, curStr, maxLen);
68 if (lstrlenW(curStr) < maxLen - 1) return curStr;
69 maxLen *= 2;
70 newStr = HeapReAlloc(GetProcessHeap(), 0, curStr, maxLen * sizeof(WCHAR));
71 if (!newStr) break;
72 curStr = newStr;
73 } while (TRUE);
74 HeapFree(GetProcessHeap(), 0, curStr);
75 return NULL;
78 LPCWSTR GetValueName(HWND hwndLV)
80 INT item;
82 if (g_valueName != LPSTR_TEXTCALLBACKW)
83 HeapFree(GetProcessHeap(), 0, g_valueName);
84 g_valueName = NULL;
86 item = SendMessageW(hwndLV, LVM_GETNEXTITEM, -1, MAKELPARAM(LVNI_FOCUSED, 0));
87 if (item == -1) return NULL;
89 g_valueName = GetItemText(hwndLV, item);
91 return g_valueName;
94 /* convert '\0' separated string list into ',' separated string list */
95 static void MakeMULTISZDisplayable(LPWSTR multi)
99 for (; *multi; multi++)
101 if (*(multi+1))
103 *multi = ',';
104 multi++;
106 } while (*multi);
109 /*******************************************************************************
110 * Local module support methods
112 void format_value_data(HWND hwndLV, int index, DWORD type, void *data, DWORD size)
114 switch (type)
116 case REG_SZ:
117 case REG_EXPAND_SZ:
118 ListView_SetItemTextW(hwndLV, index, 2, data ? data : g_szValueNotSet);
119 break;
120 case REG_DWORD:
121 case REG_DWORD_BIG_ENDIAN:
123 DWORD value = *(DWORD *)data;
124 WCHAR buf[64];
125 WCHAR format[] = {'0','x','%','0','8','x',' ','(','%','u',')',0};
126 if (type == REG_DWORD_BIG_ENDIAN)
127 value = RtlUlongByteSwap(value);
128 wsprintfW(buf, format, value, value);
129 ListView_SetItemTextW(hwndLV, index, 2, buf);
130 break;
132 case REG_MULTI_SZ:
133 MakeMULTISZDisplayable(data);
134 ListView_SetItemTextW(hwndLV, index, 2, data);
135 break;
136 case REG_BINARY:
137 case REG_NONE:
138 default:
140 unsigned int i;
141 BYTE *pData = data;
142 WCHAR *strBinary = HeapAlloc(GetProcessHeap(), 0, size * sizeof(WCHAR) * 3 + sizeof(WCHAR));
143 WCHAR format[] = {'%','0','2','X',' ',0};
144 for (i = 0; i < size; i++)
145 wsprintfW( strBinary + i*3, format, pData[i] );
146 strBinary[size * 3] = 0;
147 ListView_SetItemTextW(hwndLV, index, 2, strBinary);
148 HeapFree(GetProcessHeap(), 0, strBinary);
149 break;
154 int AddEntryToList(HWND hwndLV, WCHAR *Name, DWORD dwValType, void *ValBuf, DWORD dwCount, int pos)
156 LINE_INFO* linfo;
157 LVITEMW item;
158 int index;
160 linfo = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(LINE_INFO) + dwCount);
161 linfo->dwValType = dwValType;
162 linfo->val_len = dwCount;
163 CopyMemory(&linfo[1], ValBuf, dwCount);
165 if (Name)
167 linfo->name = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(Name) + 1) * sizeof(WCHAR));
168 lstrcpyW(linfo->name, Name);
169 } else
171 linfo->name = NULL;
174 item.mask = LVIF_TEXT | LVIF_PARAM | LVIF_STATE | LVIF_IMAGE;
175 item.iItem = (pos == -1) ? SendMessageW(hwndLV, LVM_GETITEMCOUNT, 0, 0) : pos;
176 item.iSubItem = 0;
177 item.state = 0;
178 item.stateMask = LVIS_FOCUSED | LVIS_SELECTED;
179 item.pszText = Name ? Name : LPSTR_TEXTCALLBACKW;
180 item.cchTextMax = Name ? lstrlenW(item.pszText) : 0;
182 switch (dwValType)
184 case REG_SZ:
185 case REG_EXPAND_SZ:
186 case REG_MULTI_SZ:
187 item.iImage = Image_String;
188 break;
189 default:
190 item.iImage = Image_Binary;
191 break;
193 item.lParam = (LPARAM)linfo;
195 #if (_WIN32_IE >= 0x0300)
196 item.iIndent = 0;
197 #endif
199 if ((index = ListView_InsertItemW(hwndLV, &item)) != -1)
200 format_value_data(hwndLV, index, dwValType, ValBuf, dwCount);
201 return index;
204 static BOOL InitListViewImageList(HWND hWndListView)
206 HIMAGELIST himl;
207 HICON hicon;
208 INT cx = GetSystemMetrics(SM_CXSMICON);
209 INT cy = GetSystemMetrics(SM_CYSMICON);
211 himl = ImageList_Create(cx, cy, ILC_MASK, 0, 2);
212 if (!himl)
213 return FALSE;
215 hicon = LoadImageW(hInst, MAKEINTRESOURCEW(IDI_STRING),
216 IMAGE_ICON, cx, cy, LR_DEFAULTCOLOR);
217 Image_String = ImageList_AddIcon(himl, hicon);
219 hicon = LoadImageW(hInst, MAKEINTRESOURCEW(IDI_BIN),
220 IMAGE_ICON, cx, cy, LR_DEFAULTCOLOR);
221 Image_Binary = ImageList_AddIcon(himl, hicon);
223 SendMessageW( hWndListView, LVM_SETIMAGELIST, LVSIL_SMALL, (LPARAM) himl );
225 /* fail if some of the icons failed to load */
226 if (ImageList_GetImageCount(himl) < 2)
227 return FALSE;
229 return TRUE;
232 static BOOL CreateListColumns(HWND hWndListView)
234 WCHAR szText[50];
235 int index;
236 LVCOLUMNW lvC;
238 /* Create columns. */
239 lvC.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM;
240 lvC.pszText = szText;
242 /* Load the column labels from the resource file. */
243 for (index = 0; index < MAX_LIST_COLUMNS; index++) {
244 lvC.iSubItem = index;
245 lvC.cx = default_column_widths[index];
246 lvC.fmt = column_alignment[index];
247 LoadStringW(hInst, IDS_LIST_COLUMN_FIRST + index, szText, sizeof(szText)/sizeof(WCHAR));
248 if (ListView_InsertColumnW(hWndListView, index, &lvC) == -1) return FALSE;
250 return TRUE;
253 /* OnGetDispInfo - processes the LVN_GETDISPINFO notification message. */
255 static void OnGetDispInfo(NMLVDISPINFOW* plvdi)
257 static WCHAR buffer[200];
258 static WCHAR reg_szT[] = {'R','E','G','_','S','Z',0},
259 reg_expand_szT[] = {'R','E','G','_','E','X','P','A','N','D','_','S','Z',0},
260 reg_binaryT[] = {'R','E','G','_','B','I','N','A','R','Y',0},
261 reg_dwordT[] = {'R','E','G','_','D','W','O','R','D',0},
262 reg_dword_big_endianT[] = {'R','E','G','_','D','W','O','R','D','_',
263 'B','I','G','_','E','N','D','I','A','N',0},
264 reg_multi_szT[] = {'R','E','G','_','M','U','L','T','I','_','S','Z',0},
265 reg_linkT[] = {'R','E','G','_','L','I','N','K',0},
266 reg_resource_listT[] = {'R','E','G','_','R','E','S','O','U','R','C','E','_','L','I','S','T',0},
267 reg_noneT[] = {'R','E','G','_','N','O','N','E',0},
268 emptyT[] = {0};
270 plvdi->item.pszText = NULL;
271 plvdi->item.cchTextMax = 0;
273 switch (plvdi->item.iSubItem) {
274 case 0:
275 plvdi->item.pszText = g_pszDefaultValueName;
276 break;
277 case 1:
279 DWORD data_type = ((LINE_INFO *)plvdi->item.lParam)->dwValType;
281 switch (data_type) {
282 case REG_SZ:
283 plvdi->item.pszText = reg_szT;
284 break;
285 case REG_EXPAND_SZ:
286 plvdi->item.pszText = reg_expand_szT;
287 break;
288 case REG_BINARY:
289 plvdi->item.pszText = reg_binaryT;
290 break;
291 case REG_DWORD:
292 plvdi->item.pszText = reg_dwordT;
293 break;
294 case REG_DWORD_BIG_ENDIAN:
295 plvdi->item.pszText = reg_dword_big_endianT;
296 break;
297 case REG_MULTI_SZ:
298 plvdi->item.pszText = reg_multi_szT;
299 break;
300 case REG_LINK:
301 plvdi->item.pszText = reg_linkT;
302 break;
303 case REG_RESOURCE_LIST:
304 plvdi->item.pszText = reg_resource_listT;
305 break;
306 case REG_NONE:
307 plvdi->item.pszText = reg_noneT;
308 break;
309 default:
311 WCHAR fmt[] = {'0','x','%','x',0};
312 wsprintfW(buffer, fmt, data_type);
313 plvdi->item.pszText = buffer;
314 break;
317 break;
319 case 2:
320 plvdi->item.pszText = g_szValueNotSet;
321 break;
322 case 3:
323 plvdi->item.pszText = emptyT;
324 break;
328 static int CALLBACK CompareFunc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
330 LINE_INFO*l, *r;
331 l = (LINE_INFO*)lParam1;
332 r = (LINE_INFO*)lParam2;
333 if (!l->name) return -1;
334 if (!r->name) return +1;
336 if (g_columnToSort == ~0U)
337 g_columnToSort = 0;
339 if (g_columnToSort == 1)
340 return g_invertSort ? (int)r->dwValType - (int)l->dwValType : (int)l->dwValType - (int)r->dwValType;
341 if (g_columnToSort == 2) {
342 /* FIXME: Sort on value */
343 return 0;
345 return g_invertSort ? lstrcmpiW(r->name, l->name) : lstrcmpiW(l->name, r->name);
348 HWND StartValueRename(HWND hwndLV)
350 int item;
352 item = SendMessageW(hwndLV, LVM_GETNEXTITEM, -1, MAKELPARAM(LVNI_FOCUSED | LVNI_SELECTED, 0));
353 if (item < 1) { /* cannot rename default key */
354 MessageBeep(MB_ICONHAND);
355 return 0;
357 return (HWND)SendMessageW(hwndLV, LVM_EDITLABELW, item, 0);
360 static BOOL _CmdWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
362 switch (LOWORD(wParam)) {
363 /* case ID_FILE_OPEN: */
364 /* break; */
365 default:
366 return FALSE;
368 return TRUE;
371 static LRESULT CALLBACK ListWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
373 switch (message) {
374 case WM_COMMAND:
375 if (!_CmdWndProc(hWnd, message, wParam, lParam)) {
376 return CallWindowProcW(g_orgListWndProc, hWnd, message, wParam, lParam);
378 break;
379 case WM_NOTIFY_REFLECT:
380 switch (((LPNMHDR)lParam)->code) {
382 case LVN_BEGINLABELEDITW:
383 if (!((NMLVDISPINFOW *)lParam)->item.iItem)
384 return 1;
385 return 0;
386 case LVN_GETDISPINFOW:
387 OnGetDispInfo((NMLVDISPINFOW*)lParam);
388 break;
389 case LVN_COLUMNCLICK:
390 if (g_columnToSort == ((LPNMLISTVIEW)lParam)->iSubItem)
391 g_invertSort = !g_invertSort;
392 else {
393 g_columnToSort = ((LPNMLISTVIEW)lParam)->iSubItem;
394 g_invertSort = FALSE;
397 SendMessageW(hWnd, LVM_SORTITEMS, (WPARAM)hWnd, (LPARAM)CompareFunc);
398 break;
399 case LVN_ENDLABELEDITW: {
400 LPNMLVDISPINFOW dispInfo = (LPNMLVDISPINFOW)lParam;
401 LPWSTR oldName = GetItemText(hWnd, dispInfo->item.iItem);
402 LONG ret;
404 if (!oldName) return -1; /* cannot rename a default value */
405 ret = RenameValue(hWnd, g_currentRootKey, g_currentPath, oldName, dispInfo->item.pszText);
406 if (ret)
408 dispInfo->item.iSubItem = 0;
409 SendMessageW(hWnd, LVM_SETITEMTEXTW, dispInfo->item.iItem, (LPARAM)&dispInfo->item);
411 HeapFree(GetProcessHeap(), 0, oldName);
412 return 0;
414 case NM_RETURN: {
415 int cnt = SendMessageW(hWnd, LVM_GETNEXTITEM, -1, MAKELPARAM(LVNI_FOCUSED | LVNI_SELECTED, 0));
416 if (cnt != -1)
417 SendMessageW(hFrameWnd, WM_COMMAND, ID_EDIT_MODIFY, 0);
419 break;
420 case NM_SETFOCUS:
421 g_pChildWnd->nFocusPanel = 1;
422 break;
423 case NM_DBLCLK: {
424 NMITEMACTIVATE* nmitem = (LPNMITEMACTIVATE)lParam;
425 LVHITTESTINFO info;
427 /* if (nmitem->hdr.hwndFrom != hWnd) break; unnecessary because of WM_NOTIFY_REFLECT */
428 /* if (nmitem->hdr.idFrom != IDW_LISTVIEW) break; */
429 /* if (nmitem->hdr.code != ???) break; */
430 #ifdef _MSC_VER
431 switch (nmitem->uKeyFlags) {
432 case LVKF_ALT: /* The ALT key is pressed. */
433 /* properties dialog box ? */
434 break;
435 case LVKF_CONTROL: /* The CTRL key is pressed. */
436 /* run dialog box for providing parameters... */
437 break;
438 case LVKF_SHIFT: /* The SHIFT key is pressed. */
439 break;
441 #endif
442 info.pt.x = nmitem->ptAction.x;
443 info.pt.y = nmitem->ptAction.y;
444 if (SendMessageW(hWnd, LVM_HITTEST, 0, (LPARAM)&info) != -1) {
445 LVITEMW item;
447 item.state = 0;
448 item.stateMask = LVIS_FOCUSED | LVIS_SELECTED;
449 SendMessageW(hWnd, LVM_SETITEMSTATE, (UINT)-1, (LPARAM)&item);
451 item.state = LVIS_FOCUSED | LVIS_SELECTED;
452 item.stateMask = LVIS_FOCUSED | LVIS_SELECTED;
453 SendMessageW(hWnd, LVM_SETITEMSTATE, info.iItem, (LPARAM)&item);
455 SendMessageW(hFrameWnd, WM_COMMAND, ID_EDIT_MODIFY, 0);
458 break;
460 default:
461 return 0; /* shouldn't call default ! */
463 break;
464 case WM_CONTEXTMENU: {
465 int cnt = SendMessageW(hWnd, LVM_GETNEXTITEM, -1, MAKELPARAM(LVNI_SELECTED, 0));
466 TrackPopupMenu(GetSubMenu(hPopupMenus, cnt == -1 ? PM_NEW_VALUE : PM_MODIFY_VALUE),
467 TPM_RIGHTBUTTON, (short)LOWORD(lParam), (short)HIWORD(lParam),
468 0, hFrameWnd, NULL);
469 break;
471 default:
472 return CallWindowProcW(g_orgListWndProc, hWnd, message, wParam, lParam);
474 return 0;
478 HWND CreateListView(HWND hwndParent, UINT id)
480 RECT rcClient;
481 HWND hwndLV;
482 WCHAR ListView[] = {'L','i','s','t',' ','V','i','e','w',0};
484 /* prepare strings */
485 LoadStringW(hInst, IDS_REGISTRY_VALUE_NOT_SET, g_szValueNotSet, COUNT_OF(g_szValueNotSet));
487 /* Get the dimensions of the parent window's client area, and create the list view control. */
488 GetClientRect(hwndParent, &rcClient);
489 hwndLV = CreateWindowExW(WS_EX_CLIENTEDGE, WC_LISTVIEWW, ListView,
490 WS_VISIBLE | WS_CHILD | WS_TABSTOP | LVS_REPORT | LVS_EDITLABELS,
491 0, 0, rcClient.right, rcClient.bottom,
492 hwndParent, ULongToHandle(id), hInst, NULL);
493 if (!hwndLV) return NULL;
494 SendMessageW(hwndLV, LVM_SETUNICODEFORMAT, TRUE, 0);
495 SendMessageW(hwndLV, LVM_SETEXTENDEDLISTVIEWSTYLE, 0, LVS_EX_FULLROWSELECT);
497 /* Initialize the image list */
498 if (!InitListViewImageList(hwndLV)) goto fail;
499 if (!CreateListColumns(hwndLV)) goto fail;
500 g_orgListWndProc = (WNDPROC) SetWindowLongPtrW(hwndLV, GWLP_WNDPROC, (LPARAM)ListWndProc);
501 return hwndLV;
502 fail:
503 DestroyWindow(hwndLV);
504 return NULL;
507 BOOL RefreshListView(HWND hwndLV, HKEY hKeyRoot, LPCWSTR keyPath, LPCWSTR highlightValue)
509 BOOL result = FALSE;
510 DWORD max_sub_key_len;
511 DWORD max_val_name_len, valNameLen;
512 DWORD max_val_size, valSize;
513 DWORD val_count, index, valType;
514 WCHAR* valName = 0;
515 BYTE* valBuf = 0;
516 HKEY hKey = 0;
517 LONG errCode;
518 INT count, i;
519 LVITEMW item;
521 if (!hwndLV) return FALSE;
523 SendMessageW(hwndLV, WM_SETREDRAW, FALSE, 0);
525 errCode = RegOpenKeyExW(hKeyRoot, keyPath, 0, KEY_READ, &hKey);
526 if (errCode != ERROR_SUCCESS) goto done;
528 count = SendMessageW(hwndLV, LVM_GETITEMCOUNT, 0, 0);
529 for (i = 0; i < count; i++) {
530 item.mask = LVIF_PARAM;
531 item.iItem = i;
532 SendMessageW( hwndLV, LVM_GETITEMW, 0, (LPARAM)&item );
533 HeapFree(GetProcessHeap(), 0, ((LINE_INFO*)item.lParam)->name);
534 HeapFree(GetProcessHeap(), 0, (void*)item.lParam);
536 g_columnToSort = ~0U;
537 SendMessageW( hwndLV, LVM_DELETEALLITEMS, 0, 0L );
539 /* get size information and resize the buffers if necessary */
540 errCode = RegQueryInfoKeyW(hKey, NULL, NULL, NULL, NULL, &max_sub_key_len, NULL,
541 &val_count, &max_val_name_len, &max_val_size, NULL, NULL);
542 if (errCode != ERROR_SUCCESS) goto done;
544 /* account for the terminator char */
545 max_val_name_len++;
546 max_val_size++;
548 valName = HeapAlloc(GetProcessHeap(), 0, max_val_name_len * sizeof(WCHAR));
549 if (!valName)
550 goto done;
551 valBuf = HeapAlloc(GetProcessHeap(), 0, max_val_size);
552 if (!valBuf)
553 goto done;
555 valSize = max_val_size;
556 if (RegQueryValueExW(hKey, NULL, NULL, &valType, valBuf, &valSize) == ERROR_FILE_NOT_FOUND) {
557 AddEntryToList(hwndLV, NULL, REG_SZ, NULL, 0, -1);
559 for(index = 0; index < val_count; index++) {
560 valNameLen = max_val_name_len;
561 valSize = max_val_size;
562 valType = 0;
563 errCode = RegEnumValueW(hKey, index, valName, &valNameLen, NULL, &valType, valBuf, &valSize);
564 if (errCode != ERROR_SUCCESS) goto done;
565 valBuf[valSize] = 0;
566 AddEntryToList(hwndLV, valName[0] ? valName : NULL, valType, valBuf, valSize, -1);
569 memset(&item, 0, sizeof(item));
570 if (!highlightValue)
572 item.state = item.stateMask = LVIS_FOCUSED;
573 SendMessageW(hwndLV, LVM_SETITEMSTATE, 0, (LPARAM)&item);
576 SendMessageW(hwndLV, LVM_SORTITEMS, (WPARAM)hwndLV, (LPARAM)CompareFunc);
578 g_currentRootKey = hKeyRoot;
579 if (keyPath != g_currentPath) {
580 HeapFree(GetProcessHeap(), 0, g_currentPath);
581 g_currentPath = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(keyPath) + 1) * sizeof(WCHAR));
582 if (!g_currentPath) goto done;
583 lstrcpyW(g_currentPath, keyPath);
586 result = TRUE;
588 done:
589 HeapFree(GetProcessHeap(), 0, valBuf);
590 HeapFree(GetProcessHeap(), 0, valName);
591 SendMessageW(hwndLV, WM_SETREDRAW, TRUE, 0);
592 if (hKey) RegCloseKey(hKey);
594 return result;