regedit: Implement deleting multiple values.
[wine/wine64.git] / programs / regedit / listview.c
blobebb456a1d1c178e04f101439bad22975741918eb
1 /*
2 * Regedit listviews
4 * Copyright (C) 2002 Robert Dickenson <robd@reactos.org>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include <windows.h>
22 #include <commctrl.h>
23 #include <stdlib.h>
24 #include <tchar.h>
25 #include <stdio.h>
27 #include "main.h"
29 #include "wine/unicode.h"
30 static INT Image_String;
31 static INT Image_Binary;
33 typedef struct tagLINE_INFO
35 DWORD dwValType;
36 LPTSTR name;
37 void* val;
38 size_t val_len;
39 } LINE_INFO;
41 /*******************************************************************************
42 * Global and Local Variables:
45 static WNDPROC g_orgListWndProc;
46 static DWORD g_columnToSort = ~0U;
47 static BOOL g_invertSort = FALSE;
48 static LPTSTR g_valueName;
49 static LPTSTR g_currentPath;
50 static HKEY g_currentRootKey;
51 static TCHAR g_szValueNotSet[64];
53 #define MAX_LIST_COLUMNS (IDS_LIST_COLUMN_LAST - IDS_LIST_COLUMN_FIRST + 1)
54 static int default_column_widths[MAX_LIST_COLUMNS] = { 200, 175, 400 };
55 static int column_alignment[MAX_LIST_COLUMNS] = { LVCFMT_LEFT, LVCFMT_LEFT, LVCFMT_LEFT };
57 LPTSTR GetItemText(HWND hwndLV, UINT item)
59 LPTSTR newStr, curStr;
60 unsigned int maxLen = 128;
62 curStr = HeapAlloc(GetProcessHeap(), 0, maxLen);
63 if (!curStr) return NULL;
64 if (item == 0) { /* first item is ALWAYS a default */
65 HeapFree(GetProcessHeap(), 0, curStr);
66 return NULL;
68 do {
69 ListView_GetItemText(hwndLV, item, 0, curStr, maxLen);
70 if (_tcslen(curStr) < maxLen - 1) return curStr;
71 newStr = HeapReAlloc(GetProcessHeap(), 0, curStr, maxLen * 2);
72 if (!newStr) break;
73 curStr = newStr;
74 maxLen *= 2;
75 } while (TRUE);
76 HeapFree(GetProcessHeap(), 0, curStr);
77 return NULL;
80 LPCTSTR GetValueName(HWND hwndLV)
82 INT item;
84 if (g_valueName != LPSTR_TEXTCALLBACK)
85 HeapFree(GetProcessHeap(), 0, g_valueName);
86 g_valueName = NULL;
88 item = ListView_GetNextItem(hwndLV, -1, LVNI_FOCUSED);
89 if (item == -1) return NULL;
91 g_valueName = GetItemText(hwndLV, item);
93 return g_valueName;
96 /* convert '\0' separated string list into ',' separated string list */
97 static void MakeMULTISZDisplayable(LPTSTR multi)
101 for (; *multi; multi++)
103 if (*(multi+1))
105 *multi = ',';
106 multi++;
108 } while (*multi);
111 /*******************************************************************************
112 * Local module support methods
114 static void AddEntryToList(HWND hwndLV, LPTSTR Name, DWORD dwValType,
115 void* ValBuf, DWORD dwCount, BOOL bHighlight)
117 LINE_INFO* linfo;
118 LVITEM item;
119 int index;
121 linfo = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(LINE_INFO) + dwCount);
122 linfo->dwValType = dwValType;
123 linfo->val_len = dwCount;
124 memcpy(&linfo[1], ValBuf, dwCount);
126 if (Name)
127 linfo->name = _tcsdup(Name);
128 else
129 linfo->name = NULL;
131 item.mask = LVIF_TEXT | LVIF_PARAM | LVIF_STATE | LVIF_IMAGE;
132 item.iItem = ListView_GetItemCount(hwndLV);/*idx; */
133 item.iSubItem = 0;
134 item.state = 0;
135 item.stateMask = LVIS_FOCUSED | LVIS_SELECTED;
136 item.pszText = Name ? Name : LPSTR_TEXTCALLBACK;
137 item.cchTextMax = Name ? _tcslen(item.pszText) : 0;
138 if (bHighlight) {
139 item.stateMask = item.state = LVIS_FOCUSED | LVIS_SELECTED;
141 switch (dwValType)
143 case REG_SZ:
144 case REG_EXPAND_SZ:
145 case REG_MULTI_SZ:
146 item.iImage = Image_String;
147 break;
148 default:
149 item.iImage = Image_Binary;
150 break;
152 item.lParam = (LPARAM)linfo;
154 #if (_WIN32_IE >= 0x0300)
155 item.iIndent = 0;
156 #endif
158 index = ListView_InsertItem(hwndLV, &item);
159 if (index != -1) {
160 switch (dwValType) {
161 case REG_SZ:
162 case REG_EXPAND_SZ:
163 if (ValBuf) {
164 ListView_SetItemText(hwndLV, index, 2, ValBuf);
165 } else {
166 ListView_SetItemText(hwndLV, index, 2, g_szValueNotSet);
168 break;
169 case REG_DWORD: {
170 TCHAR buf[64];
171 wsprintf(buf, _T("0x%08x (%u)"), *(DWORD*)ValBuf, *(DWORD*)ValBuf);
172 ListView_SetItemText(hwndLV, index, 2, buf);
174 break;
175 case REG_BINARY: {
176 unsigned int i;
177 LPBYTE pData = (LPBYTE)ValBuf;
178 LPTSTR strBinary = HeapAlloc(GetProcessHeap(), 0, dwCount * sizeof(TCHAR) * 3 + 1);
179 for (i = 0; i < dwCount; i++)
180 wsprintf( strBinary + i*3, _T("%02X "), pData[i] );
181 strBinary[dwCount * 3] = 0;
182 ListView_SetItemText(hwndLV, index, 2, strBinary);
183 HeapFree(GetProcessHeap(), 0, strBinary);
185 break;
186 case REG_MULTI_SZ:
187 MakeMULTISZDisplayable(ValBuf);
188 ListView_SetItemText(hwndLV, index, 2, ValBuf);
189 break;
190 default:
192 TCHAR szText[128];
193 LoadString(hInst, IDS_REGISTRY_VALUE_CANT_DISPLAY, szText, COUNT_OF(szText));
194 ListView_SetItemText(hwndLV, index, 2, szText);
195 break;
201 static BOOL InitListViewImageList(HWND hWndListView)
203 HIMAGELIST himl;
204 HICON hicon;
205 INT cx = GetSystemMetrics(SM_CXSMICON);
206 INT cy = GetSystemMetrics(SM_CYSMICON);
208 himl = ImageList_Create(cx, cy, ILC_MASK, 0, 2);
209 if (!himl)
210 return FALSE;
212 hicon = LoadImage(hInst, MAKEINTRESOURCE(IDI_STRING),
213 IMAGE_ICON, cx, cy, LR_DEFAULTCOLOR);
214 Image_String = ImageList_AddIcon(himl, hicon);
216 hicon = LoadImage(hInst, MAKEINTRESOURCE(IDI_BIN),
217 IMAGE_ICON, cx, cy, LR_DEFAULTCOLOR);
218 Image_Binary = ImageList_AddIcon(himl, hicon);
220 SendMessage( hWndListView, LVM_SETIMAGELIST, LVSIL_SMALL, (LPARAM) himl );
222 /* fail if some of the icons failed to load */
223 if (ImageList_GetImageCount(himl) < 2)
224 return FALSE;
226 return TRUE;
229 static BOOL CreateListColumns(HWND hWndListView)
231 TCHAR szText[50];
232 int index;
233 LV_COLUMN lvC;
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 LoadString(hInst, IDS_LIST_COLUMN_FIRST + index, szText, sizeof(szText)/sizeof(TCHAR));
245 if (ListView_InsertColumn(hWndListView, index, &lvC) == -1) return FALSE;
247 return TRUE;
250 /* OnGetDispInfo - processes the LVN_GETDISPINFO notification message. */
252 static void OnGetDispInfo(NMLVDISPINFO* plvdi)
254 static TCHAR buffer[200];
255 static TCHAR reg_szT[] = {'R','E','G','_','S','Z',0},
256 reg_expand_szT[] = {'R','E','G','_','E','X','P','A','N','D','_','S','Z',0},
257 reg_binaryT[] = {'R','E','G','_','B','I','N','A','R','Y',0},
258 reg_dwordT[] = {'R','E','G','_','D','W','O','R','D',0},
259 reg_dword_big_endianT[] = {'R','E','G','_','D','W','O','R','D','_',
260 'B','I','G','_','E','N','D','I','A','N',0},
261 reg_multi_szT[] = {'R','E','G','_','M','U','L','T','I','_','S','Z',0},
262 reg_linkT[] = {'R','E','G','_','L','I','N','K',0},
263 reg_resource_listT[] = {'R','E','G','_','R','E','S','O','U','R','C','E','_','L','I','S','T',0},
264 reg_noneT[] = {'R','E','G','_','N','O','N','E',0},
265 emptyT[] = {0};
267 plvdi->item.pszText = NULL;
268 plvdi->item.cchTextMax = 0;
270 switch (plvdi->item.iSubItem) {
271 case 0:
272 plvdi->item.pszText = (LPSTR)g_pszDefaultValueName;
273 break;
274 case 1:
275 switch (((LINE_INFO*)plvdi->item.lParam)->dwValType) {
276 case REG_SZ:
277 plvdi->item.pszText = reg_szT;
278 break;
279 case REG_EXPAND_SZ:
280 plvdi->item.pszText = reg_expand_szT;
281 break;
282 case REG_BINARY:
283 plvdi->item.pszText = reg_binaryT;
284 break;
285 case REG_DWORD:
286 plvdi->item.pszText = reg_dwordT;
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 TCHAR szUnknownFmt[64];
306 LoadString(hInst, IDS_REGISTRY_UNKNOWN_TYPE, szUnknownFmt, COUNT_OF(szUnknownFmt));
307 wsprintf(buffer, szUnknownFmt, plvdi->item.lParam);
308 plvdi->item.pszText = buffer;
309 break;
312 break;
313 case 2:
314 plvdi->item.pszText = g_szValueNotSet;
315 break;
316 case 3:
317 plvdi->item.pszText = emptyT;
318 break;
322 static int CALLBACK CompareFunc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
324 LINE_INFO*l, *r;
325 l = (LINE_INFO*)lParam1;
326 r = (LINE_INFO*)lParam2;
327 if (!l->name) return -1;
328 if (!r->name) return +1;
330 if (g_columnToSort == ~0U)
331 g_columnToSort = 0;
333 if (g_columnToSort == 1 && l->dwValType != r->dwValType)
334 return g_invertSort ? (int)r->dwValType - (int)l->dwValType : (int)l->dwValType - (int)r->dwValType;
335 if (g_columnToSort == 2) {
336 /* FIXME: Sort on value */
338 return g_invertSort ? _tcscmp(r->name, l->name) : _tcscmp(l->name, r->name);
341 HWND StartValueRename(HWND hwndLV)
343 int item;
345 item = ListView_GetNextItem(hwndLV, -1, LVNI_FOCUSED | LVNI_SELECTED);
346 if (item < 1) { /* cannot rename default key */
347 MessageBeep(MB_ICONHAND);
348 return 0;
350 return ListView_EditLabel(hwndLV, item);
353 static BOOL _CmdWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
355 switch (LOWORD(wParam)) {
356 /* case ID_FILE_OPEN: */
357 /* break; */
358 default:
359 return FALSE;
361 return TRUE;
364 static LRESULT CALLBACK ListWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
366 switch (message) {
367 case WM_COMMAND:
368 if (!_CmdWndProc(hWnd, message, wParam, lParam)) {
369 return CallWindowProc(g_orgListWndProc, hWnd, message, wParam, lParam);
371 break;
372 case WM_NOTIFY_REFLECT:
373 switch (((LPNMHDR)lParam)->code) {
375 case LVN_BEGINLABELEDIT:
376 if (!((NMLVDISPINFO *)lParam)->item.iItem)
377 return 1;
378 return 0;
379 case LVN_GETDISPINFO:
380 OnGetDispInfo((NMLVDISPINFO*)lParam);
381 break;
382 case LVN_COLUMNCLICK:
383 if (g_columnToSort == ((LPNMLISTVIEW)lParam)->iSubItem)
384 g_invertSort = !g_invertSort;
385 else {
386 g_columnToSort = ((LPNMLISTVIEW)lParam)->iSubItem;
387 g_invertSort = FALSE;
390 SendMessage(hWnd, LVM_SORTITEMS, (WPARAM)hWnd, (LPARAM)CompareFunc);
391 break;
392 case LVN_ENDLABELEDIT: {
393 LPNMLVDISPINFO dispInfo = (LPNMLVDISPINFO)lParam;
394 LPTSTR valueName = GetItemText(hWnd, dispInfo->item.iItem);
395 LONG ret;
396 if (!valueName) return -1; /* cannot rename a default value */
397 ret = RenameValue(hWnd, g_currentRootKey, g_currentPath, valueName, dispInfo->item.pszText);
398 if (ret)
399 RefreshListView(hWnd, g_currentRootKey, g_currentPath, dispInfo->item.pszText);
400 HeapFree(GetProcessHeap(), 0, valueName);
401 return 0;
403 case NM_RETURN: {
404 int cnt = ListView_GetNextItem(hWnd, -1, LVNI_FOCUSED | LVNI_SELECTED);
405 if (cnt != -1)
406 SendMessage(hFrameWnd, WM_COMMAND, ID_EDIT_MODIFY, 0);
408 break;
409 case NM_DBLCLK: {
410 NMITEMACTIVATE* nmitem = (LPNMITEMACTIVATE)lParam;
411 LVHITTESTINFO info;
413 /* if (nmitem->hdr.hwndFrom != hWnd) break; unnecessary because of WM_NOTIFY_REFLECT */
414 /* if (nmitem->hdr.idFrom != IDW_LISTVIEW) break; */
415 /* if (nmitem->hdr.code != ???) break; */
416 #ifdef _MSC_VER
417 switch (nmitem->uKeyFlags) {
418 case LVKF_ALT: /* The ALT key is pressed. */
419 /* properties dialog box ? */
420 break;
421 case LVKF_CONTROL: /* The CTRL key is pressed. */
422 /* run dialog box for providing parameters... */
423 break;
424 case LVKF_SHIFT: /* The SHIFT key is pressed. */
425 break;
427 #endif
428 info.pt.x = nmitem->ptAction.x;
429 info.pt.y = nmitem->ptAction.y;
430 if (ListView_HitTest(hWnd, &info) != -1) {
431 ListView_SetItemState(hWnd, -1, 0, LVIS_FOCUSED|LVIS_SELECTED);
432 ListView_SetItemState(hWnd, info.iItem, LVIS_FOCUSED|LVIS_SELECTED,
433 LVIS_FOCUSED|LVIS_SELECTED);
434 SendMessage(hFrameWnd, WM_COMMAND, ID_EDIT_MODIFY, 0);
437 break;
439 default:
440 return 0; /* shouldn't call default ! */
442 break;
443 case WM_CONTEXTMENU: {
444 int cnt = ListView_GetNextItem(hWnd, -1, LVNI_SELECTED);
445 TrackPopupMenu(GetSubMenu(hPopupMenus, cnt == -1 ? PM_NEW : PM_MODIFYVALUE),
446 TPM_RIGHTBUTTON, (short)LOWORD(lParam), (short)HIWORD(lParam),
447 0, hFrameWnd, NULL);
448 break;
450 default:
451 return CallWindowProc(g_orgListWndProc, hWnd, message, wParam, lParam);
453 return 0;
457 HWND CreateListView(HWND hwndParent, UINT id)
459 RECT rcClient;
460 HWND hwndLV;
462 /* prepare strings */
463 LoadString(hInst, IDS_REGISTRY_VALUE_NOT_SET, g_szValueNotSet, COUNT_OF(g_szValueNotSet));
465 /* Get the dimensions of the parent window's client area, and create the list view control. */
466 GetClientRect(hwndParent, &rcClient);
467 hwndLV = CreateWindowEx(WS_EX_CLIENTEDGE, WC_LISTVIEW, _T("List View"),
468 WS_VISIBLE | WS_CHILD | WS_TABSTOP | LVS_REPORT | LVS_EDITLABELS,
469 0, 0, rcClient.right, rcClient.bottom,
470 hwndParent, (HMENU)ULongToHandle(id), hInst, NULL);
471 if (!hwndLV) return NULL;
472 SendMessage(hwndLV, LVM_SETEXTENDEDLISTVIEWSTYLE, 0, LVS_EX_FULLROWSELECT);
474 /* Initialize the image list */
475 if (!InitListViewImageList(hwndLV)) goto fail;
476 if (!CreateListColumns(hwndLV)) goto fail;
477 g_orgListWndProc = (WNDPROC) SetWindowLongPtr(hwndLV, GWLP_WNDPROC, (LPARAM)ListWndProc);
478 return hwndLV;
479 fail:
480 DestroyWindow(hwndLV);
481 return NULL;
484 BOOL RefreshListView(HWND hwndLV, HKEY hKeyRoot, LPCTSTR keyPath, LPCTSTR highlightValue)
486 BOOL result = FALSE;
487 DWORD max_sub_key_len;
488 DWORD max_val_name_len, valNameLen;
489 DWORD max_val_size, valSize;
490 DWORD val_count, index, valType;
491 TCHAR* valName = 0;
492 BYTE* valBuf = 0;
493 HKEY hKey = 0;
494 LONG errCode;
495 INT count, i;
496 LVITEM item;
498 if (!hwndLV) return FALSE;
500 SendMessage(hwndLV, WM_SETREDRAW, FALSE, 0);
502 errCode = RegOpenKeyEx(hKeyRoot, keyPath, 0, KEY_READ, &hKey);
503 if (errCode != ERROR_SUCCESS) goto done;
505 count = ListView_GetItemCount(hwndLV);
506 for (i = 0; i < count; i++) {
507 item.mask = LVIF_PARAM;
508 item.iItem = i;
509 SendMessage( hwndLV, LVM_GETITEM, 0, (LPARAM)&item );
510 free(((LINE_INFO*)item.lParam)->name);
511 HeapFree(GetProcessHeap(), 0, (void*)item.lParam);
513 g_columnToSort = ~0U;
514 SendMessage( hwndLV, LVM_DELETEALLITEMS, 0, 0L );
516 /* get size information and resize the buffers if necessary */
517 errCode = RegQueryInfoKey(hKey, NULL, NULL, NULL, NULL, &max_sub_key_len, NULL,
518 &val_count, &max_val_name_len, &max_val_size, NULL, NULL);
519 if (errCode != ERROR_SUCCESS) goto done;
521 /* account for the terminator char */
522 max_val_name_len++;
523 max_val_size++;
525 valName = HeapAlloc(GetProcessHeap(), 0, max_val_name_len * sizeof(TCHAR));
526 valBuf = HeapAlloc(GetProcessHeap(), 0, max_val_size);
527 if (RegQueryValueEx(hKey, NULL, NULL, &valType, valBuf, &valSize) == ERROR_FILE_NOT_FOUND) {
528 AddEntryToList(hwndLV, NULL, REG_SZ, NULL, 0, !highlightValue);
530 for(index = 0; index < val_count; index++) {
531 BOOL bSelected = (valName == highlightValue); /* NOT a bug, we check for double NULL here */
532 valNameLen = max_val_name_len;
533 valSize = max_val_size;
534 valType = 0;
535 errCode = RegEnumValue(hKey, index, valName, &valNameLen, NULL, &valType, valBuf, &valSize);
536 if (errCode != ERROR_SUCCESS) goto done;
537 valBuf[valSize] = 0;
538 if (valName && highlightValue && !_tcscmp(valName, highlightValue))
539 bSelected = TRUE;
540 AddEntryToList(hwndLV, valName[0] ? valName : NULL, valType, valBuf, valSize, bSelected);
542 SendMessage(hwndLV, LVM_SORTITEMS, (WPARAM)hwndLV, (LPARAM)CompareFunc);
544 g_currentRootKey = hKeyRoot;
545 if (keyPath != g_currentPath) {
546 HeapFree(GetProcessHeap(), 0, g_currentPath);
547 g_currentPath = HeapAlloc(GetProcessHeap(), 0, (lstrlen(keyPath) + 1) * sizeof(TCHAR));
548 if (!g_currentPath) goto done;
549 lstrcpy(g_currentPath, keyPath);
552 result = TRUE;
554 done:
555 HeapFree(GetProcessHeap(), 0, valBuf);
556 HeapFree(GetProcessHeap(), 0, valName);
557 SendMessage(hwndLV, WM_SETREDRAW, TRUE, 0);
558 if (hKey) RegCloseKey(hKey);
560 return result;