push b0b97fcd59eff07f047585692fa36859b459324f
[wine/hacks.git] / programs / regedit / listview.c
blob0a919a0fd80f1e79a844eca9af0a8f8f41830920
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"
28 #include "regproc.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_szValueNotSetW[64];
53 static TCHAR g_szValueNotSet[64];
55 #define MAX_LIST_COLUMNS (IDS_LIST_COLUMN_LAST - IDS_LIST_COLUMN_FIRST + 1)
56 static int default_column_widths[MAX_LIST_COLUMNS] = { 200, 175, 400 };
57 static int column_alignment[MAX_LIST_COLUMNS] = { LVCFMT_LEFT, LVCFMT_LEFT, LVCFMT_LEFT };
59 LPTSTR GetItemText(HWND hwndLV, UINT item)
61 LPTSTR newStr, curStr;
62 unsigned int maxLen = 128;
64 curStr = HeapAlloc(GetProcessHeap(), 0, maxLen);
65 if (!curStr) return NULL;
66 if (item == 0) { /* first item is ALWAYS a default */
67 HeapFree(GetProcessHeap(), 0, curStr);
68 return NULL;
70 do {
71 ListView_GetItemText(hwndLV, item, 0, curStr, maxLen);
72 if (_tcslen(curStr) < maxLen - 1) return curStr;
73 newStr = HeapReAlloc(GetProcessHeap(), 0, curStr, maxLen * 2);
74 if (!newStr) break;
75 curStr = newStr;
76 maxLen *= 2;
77 } while (TRUE);
78 HeapFree(GetProcessHeap(), 0, curStr);
79 return NULL;
82 LPWSTR GetItemTextW(HWND hwndLV, UINT item)
84 LPWSTR newStr, curStr;
85 unsigned int maxLen = 128;
87 curStr = HeapAlloc(GetProcessHeap(), 0, maxLen * sizeof(WCHAR));
88 if (!curStr) return NULL;
89 if (item == 0) { /* first item is ALWAYS a default */
90 HeapFree(GetProcessHeap(), 0, curStr);
91 return NULL;
93 do {
94 ListView_GetItemTextW(hwndLV, item, 0, curStr, maxLen * sizeof(WCHAR));
95 if (lstrlenW(curStr) < maxLen - 1) return curStr;
96 newStr = HeapReAlloc(GetProcessHeap(), 0, curStr, maxLen * 2 * sizeof(WCHAR));
97 if (!newStr) break;
98 curStr = newStr;
99 maxLen *= 2;
100 } while (TRUE);
101 HeapFree(GetProcessHeap(), 0, curStr);
102 return NULL;
105 LPCWSTR GetValueName(HWND hwndLV)
107 INT item;
109 if (g_valueName != LPSTR_TEXTCALLBACKW)
110 HeapFree(GetProcessHeap(), 0, g_valueName);
111 g_valueName = NULL;
113 item = ListView_GetNextItem(hwndLV, -1, LVNI_FOCUSED);
114 if (item == -1) return NULL;
116 g_valueName = GetItemTextW(hwndLV, item);
118 return g_valueName;
121 /* convert '\0' separated string list into ',' separated string list */
122 static void MakeMULTISZDisplayable(LPWSTR multi)
126 for (; *multi; multi++)
128 if (*(multi+1))
130 *multi = ',';
131 multi++;
133 } while (*multi);
136 /*******************************************************************************
137 * Local module support methods
139 static void AddEntryToList(HWND hwndLV, LPWSTR Name, DWORD dwValType,
140 void* ValBuf, DWORD dwCount, BOOL bHighlight)
142 LINE_INFO* linfo;
143 LVITEMW item;
144 int index;
146 linfo = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(LINE_INFO) + dwCount);
147 linfo->dwValType = dwValType;
148 linfo->val_len = dwCount;
149 CopyMemory(&linfo[1], ValBuf, dwCount);
151 if (Name)
153 linfo->name = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(Name) + 1) * sizeof(WCHAR));
154 lstrcpyW(linfo->name, Name);
155 } else
157 linfo->name = NULL;
160 item.mask = LVIF_TEXT | LVIF_PARAM | LVIF_STATE | LVIF_IMAGE;
161 item.iItem = ListView_GetItemCount(hwndLV);/*idx; */
162 item.iSubItem = 0;
163 item.state = 0;
164 item.stateMask = LVIS_FOCUSED | LVIS_SELECTED;
165 item.pszText = Name ? Name : LPSTR_TEXTCALLBACKW;
166 item.cchTextMax = Name ? lstrlenW(item.pszText) : 0;
167 if (bHighlight) {
168 item.stateMask = item.state = LVIS_FOCUSED | LVIS_SELECTED;
170 switch (dwValType)
172 case REG_SZ:
173 case REG_EXPAND_SZ:
174 case REG_MULTI_SZ:
175 item.iImage = Image_String;
176 break;
177 default:
178 item.iImage = Image_Binary;
179 break;
181 item.lParam = (LPARAM)linfo;
183 #if (_WIN32_IE >= 0x0300)
184 item.iIndent = 0;
185 #endif
187 index = ListView_InsertItemW(hwndLV, &item);
188 if (index != -1) {
189 switch (dwValType) {
190 case REG_SZ:
191 case REG_EXPAND_SZ:
192 if (ValBuf) {
193 ListView_SetItemTextW(hwndLV, index, 2, ValBuf);
194 } else {
195 ListView_SetItemTextW(hwndLV, index, 2, g_szValueNotSetW);
197 break;
198 case REG_DWORD: {
199 WCHAR buf[64];
200 WCHAR format[] = {'0','x','%','0','8','x',' ','(','%','u',')',0};
201 wsprintfW(buf, format, *(DWORD*)ValBuf, *(DWORD*)ValBuf);
202 ListView_SetItemTextW(hwndLV, index, 2, buf);
204 break;
205 case REG_BINARY: {
206 unsigned int i;
207 LPBYTE pData = (LPBYTE)ValBuf;
208 LPWSTR strBinary = HeapAlloc(GetProcessHeap(), 0, dwCount * sizeof(WCHAR) * 3 + sizeof(WCHAR));
209 WCHAR format[] = {'%','0','2','X',' ',0};
210 for (i = 0; i < dwCount; i++)
211 wsprintfW( strBinary + i*3, format, pData[i] );
212 strBinary[dwCount * 3] = 0;
213 ListView_SetItemTextW(hwndLV, index, 2, strBinary);
214 HeapFree(GetProcessHeap(), 0, strBinary);
216 break;
217 case REG_MULTI_SZ:
218 MakeMULTISZDisplayable(ValBuf);
219 ListView_SetItemTextW(hwndLV, index, 2, ValBuf);
220 break;
221 default:
223 WCHAR szText[128];
224 LoadStringW(hInst, IDS_REGISTRY_VALUE_CANT_DISPLAY, szText, COUNT_OF(szText));
225 ListView_SetItemTextW(hwndLV, index, 2, szText);
226 break;
232 static BOOL InitListViewImageList(HWND hWndListView)
234 HIMAGELIST himl;
235 HICON hicon;
236 INT cx = GetSystemMetrics(SM_CXSMICON);
237 INT cy = GetSystemMetrics(SM_CYSMICON);
239 himl = ImageList_Create(cx, cy, ILC_MASK, 0, 2);
240 if (!himl)
241 return FALSE;
243 hicon = LoadImage(hInst, MAKEINTRESOURCE(IDI_STRING),
244 IMAGE_ICON, cx, cy, LR_DEFAULTCOLOR);
245 Image_String = ImageList_AddIcon(himl, hicon);
247 hicon = LoadImage(hInst, MAKEINTRESOURCE(IDI_BIN),
248 IMAGE_ICON, cx, cy, LR_DEFAULTCOLOR);
249 Image_Binary = ImageList_AddIcon(himl, hicon);
251 SendMessage( hWndListView, LVM_SETIMAGELIST, LVSIL_SMALL, (LPARAM) himl );
253 /* fail if some of the icons failed to load */
254 if (ImageList_GetImageCount(himl) < 2)
255 return FALSE;
257 return TRUE;
260 static BOOL CreateListColumns(HWND hWndListView)
262 TCHAR szText[50];
263 int index;
264 LV_COLUMN lvC;
266 /* Create columns. */
267 lvC.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM;
268 lvC.pszText = szText;
270 /* Load the column labels from the resource file. */
271 for (index = 0; index < MAX_LIST_COLUMNS; index++) {
272 lvC.iSubItem = index;
273 lvC.cx = default_column_widths[index];
274 lvC.fmt = column_alignment[index];
275 LoadString(hInst, IDS_LIST_COLUMN_FIRST + index, szText, sizeof(szText)/sizeof(TCHAR));
276 if (ListView_InsertColumn(hWndListView, index, &lvC) == -1) return FALSE;
278 return TRUE;
281 /* OnGetDispInfo - processes the LVN_GETDISPINFO notification message. */
283 static void OnGetDispInfo(NMLVDISPINFO* plvdi)
285 static TCHAR buffer[200];
286 static TCHAR reg_szT[] = {'R','E','G','_','S','Z',0},
287 reg_expand_szT[] = {'R','E','G','_','E','X','P','A','N','D','_','S','Z',0},
288 reg_binaryT[] = {'R','E','G','_','B','I','N','A','R','Y',0},
289 reg_dwordT[] = {'R','E','G','_','D','W','O','R','D',0},
290 reg_dword_big_endianT[] = {'R','E','G','_','D','W','O','R','D','_',
291 'B','I','G','_','E','N','D','I','A','N',0},
292 reg_multi_szT[] = {'R','E','G','_','M','U','L','T','I','_','S','Z',0},
293 reg_linkT[] = {'R','E','G','_','L','I','N','K',0},
294 reg_resource_listT[] = {'R','E','G','_','R','E','S','O','U','R','C','E','_','L','I','S','T',0},
295 reg_noneT[] = {'R','E','G','_','N','O','N','E',0},
296 emptyT[] = {0};
298 plvdi->item.pszText = NULL;
299 plvdi->item.cchTextMax = 0;
301 switch (plvdi->item.iSubItem) {
302 case 0:
303 plvdi->item.pszText = (LPSTR)g_pszDefaultValueName;
304 break;
305 case 1:
306 switch (((LINE_INFO*)plvdi->item.lParam)->dwValType) {
307 case REG_SZ:
308 plvdi->item.pszText = reg_szT;
309 break;
310 case REG_EXPAND_SZ:
311 plvdi->item.pszText = reg_expand_szT;
312 break;
313 case REG_BINARY:
314 plvdi->item.pszText = reg_binaryT;
315 break;
316 case REG_DWORD:
317 plvdi->item.pszText = reg_dwordT;
318 break;
319 case REG_DWORD_BIG_ENDIAN:
320 plvdi->item.pszText = reg_dword_big_endianT;
321 break;
322 case REG_MULTI_SZ:
323 plvdi->item.pszText = reg_multi_szT;
324 break;
325 case REG_LINK:
326 plvdi->item.pszText = reg_linkT;
327 break;
328 case REG_RESOURCE_LIST:
329 plvdi->item.pszText = reg_resource_listT;
330 break;
331 case REG_NONE:
332 plvdi->item.pszText = reg_noneT;
333 break;
334 default:
336 TCHAR szUnknownFmt[64];
337 LoadString(hInst, IDS_REGISTRY_UNKNOWN_TYPE, szUnknownFmt, COUNT_OF(szUnknownFmt));
338 wsprintf(buffer, szUnknownFmt, plvdi->item.lParam);
339 plvdi->item.pszText = buffer;
340 break;
343 break;
344 case 2:
345 plvdi->item.pszText = g_szValueNotSet;
346 break;
347 case 3:
348 plvdi->item.pszText = emptyT;
349 break;
353 static int CALLBACK CompareFunc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
355 LINE_INFO*l, *r;
356 l = (LINE_INFO*)lParam1;
357 r = (LINE_INFO*)lParam2;
358 if (!l->name) return -1;
359 if (!r->name) return +1;
361 if (g_columnToSort == ~0U)
362 g_columnToSort = 0;
364 if (g_columnToSort == 1 && l->dwValType != r->dwValType)
365 return g_invertSort ? (int)r->dwValType - (int)l->dwValType : (int)l->dwValType - (int)r->dwValType;
366 if (g_columnToSort == 2) {
367 /* FIXME: Sort on value */
369 return g_invertSort ? lstrcmpiW(r->name, l->name) : lstrcmpiW(l->name, r->name);
372 HWND StartValueRename(HWND hwndLV)
374 int item;
376 item = ListView_GetNextItem(hwndLV, -1, LVNI_FOCUSED | LVNI_SELECTED);
377 if (item < 1) { /* cannot rename default key */
378 MessageBeep(MB_ICONHAND);
379 return 0;
381 return ListView_EditLabel(hwndLV, item);
384 static BOOL _CmdWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
386 switch (LOWORD(wParam)) {
387 /* case ID_FILE_OPEN: */
388 /* break; */
389 default:
390 return FALSE;
392 return TRUE;
395 static LRESULT CALLBACK ListWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
397 switch (message) {
398 case WM_COMMAND:
399 if (!_CmdWndProc(hWnd, message, wParam, lParam)) {
400 return CallWindowProc(g_orgListWndProc, hWnd, message, wParam, lParam);
402 break;
403 case WM_NOTIFY_REFLECT:
404 switch (((LPNMHDR)lParam)->code) {
406 case LVN_BEGINLABELEDIT:
407 if (!((NMLVDISPINFO *)lParam)->item.iItem)
408 return 1;
409 return 0;
410 case LVN_GETDISPINFO:
411 OnGetDispInfo((NMLVDISPINFO*)lParam);
412 break;
413 case LVN_COLUMNCLICK:
414 if (g_columnToSort == ((LPNMLISTVIEW)lParam)->iSubItem)
415 g_invertSort = !g_invertSort;
416 else {
417 g_columnToSort = ((LPNMLISTVIEW)lParam)->iSubItem;
418 g_invertSort = FALSE;
421 SendMessage(hWnd, LVM_SORTITEMS, (WPARAM)hWnd, (LPARAM)CompareFunc);
422 break;
423 case LVN_ENDLABELEDIT: {
424 LPNMLVDISPINFO dispInfo = (LPNMLVDISPINFO)lParam;
425 LPTSTR valueName = GetItemText(hWnd, dispInfo->item.iItem);
426 LPSTR pathA;
427 LONG ret;
428 if (!valueName) return -1; /* cannot rename a default value */
429 pathA = GetMultiByteString(g_currentPath);
430 ret = RenameValue(hWnd, g_currentRootKey, pathA, valueName, dispInfo->item.pszText);
431 HeapFree(GetProcessHeap(), 0, pathA);
432 if (ret)
434 WCHAR* itemTextW = GetWideString(dispInfo->item.pszText);
435 RefreshListView(hWnd, g_currentRootKey, g_currentPath, itemTextW);
436 HeapFree(GetProcessHeap(), 0, itemTextW);
438 HeapFree(GetProcessHeap(), 0, valueName);
439 return 0;
441 case NM_RETURN: {
442 int cnt = ListView_GetNextItem(hWnd, -1, LVNI_FOCUSED | LVNI_SELECTED);
443 if (cnt != -1)
444 SendMessage(hFrameWnd, WM_COMMAND, ID_EDIT_MODIFY, 0);
446 break;
447 case NM_DBLCLK: {
448 NMITEMACTIVATE* nmitem = (LPNMITEMACTIVATE)lParam;
449 LVHITTESTINFO info;
451 /* if (nmitem->hdr.hwndFrom != hWnd) break; unnecessary because of WM_NOTIFY_REFLECT */
452 /* if (nmitem->hdr.idFrom != IDW_LISTVIEW) break; */
453 /* if (nmitem->hdr.code != ???) break; */
454 #ifdef _MSC_VER
455 switch (nmitem->uKeyFlags) {
456 case LVKF_ALT: /* The ALT key is pressed. */
457 /* properties dialog box ? */
458 break;
459 case LVKF_CONTROL: /* The CTRL key is pressed. */
460 /* run dialog box for providing parameters... */
461 break;
462 case LVKF_SHIFT: /* The SHIFT key is pressed. */
463 break;
465 #endif
466 info.pt.x = nmitem->ptAction.x;
467 info.pt.y = nmitem->ptAction.y;
468 if (ListView_HitTest(hWnd, &info) != -1) {
469 ListView_SetItemState(hWnd, -1, 0, LVIS_FOCUSED|LVIS_SELECTED);
470 ListView_SetItemState(hWnd, info.iItem, LVIS_FOCUSED|LVIS_SELECTED,
471 LVIS_FOCUSED|LVIS_SELECTED);
472 SendMessage(hFrameWnd, WM_COMMAND, ID_EDIT_MODIFY, 0);
475 break;
477 default:
478 return 0; /* shouldn't call default ! */
480 break;
481 case WM_CONTEXTMENU: {
482 int cnt = ListView_GetNextItem(hWnd, -1, LVNI_SELECTED);
483 TrackPopupMenu(GetSubMenu(hPopupMenus, cnt == -1 ? PM_NEW : PM_MODIFYVALUE),
484 TPM_RIGHTBUTTON, (short)LOWORD(lParam), (short)HIWORD(lParam),
485 0, hFrameWnd, NULL);
486 break;
488 default:
489 return CallWindowProc(g_orgListWndProc, hWnd, message, wParam, lParam);
491 return 0;
495 HWND CreateListView(HWND hwndParent, UINT id)
497 RECT rcClient;
498 HWND hwndLV;
500 /* prepare strings */
501 LoadString(hInst, IDS_REGISTRY_VALUE_NOT_SET, g_szValueNotSet, COUNT_OF(g_szValueNotSet));
502 LoadStringW(hInst, IDS_REGISTRY_VALUE_NOT_SET, g_szValueNotSetW, COUNT_OF(g_szValueNotSetW));
504 /* Get the dimensions of the parent window's client area, and create the list view control. */
505 GetClientRect(hwndParent, &rcClient);
506 hwndLV = CreateWindowEx(WS_EX_CLIENTEDGE, WC_LISTVIEW, _T("List View"),
507 WS_VISIBLE | WS_CHILD | WS_TABSTOP | LVS_REPORT | LVS_EDITLABELS,
508 0, 0, rcClient.right, rcClient.bottom,
509 hwndParent, (HMENU)ULongToHandle(id), hInst, NULL);
510 if (!hwndLV) return NULL;
511 SendMessage(hwndLV, LVM_SETEXTENDEDLISTVIEWSTYLE, 0, LVS_EX_FULLROWSELECT);
513 /* Initialize the image list */
514 if (!InitListViewImageList(hwndLV)) goto fail;
515 if (!CreateListColumns(hwndLV)) goto fail;
516 g_orgListWndProc = (WNDPROC) SetWindowLongPtr(hwndLV, GWLP_WNDPROC, (LPARAM)ListWndProc);
517 return hwndLV;
518 fail:
519 DestroyWindow(hwndLV);
520 return NULL;
523 BOOL RefreshListView(HWND hwndLV, HKEY hKeyRoot, LPCWSTR keyPath, LPCWSTR highlightValue)
525 BOOL result = FALSE;
526 DWORD max_sub_key_len;
527 DWORD max_val_name_len, valNameLen;
528 DWORD max_val_size, valSize;
529 DWORD val_count, index, valType;
530 WCHAR* valName = 0;
531 BYTE* valBuf = 0;
532 HKEY hKey = 0;
533 LONG errCode;
534 INT count, i;
535 LVITEMW item;
537 if (!hwndLV) return FALSE;
539 SendMessageW(hwndLV, WM_SETREDRAW, FALSE, 0);
541 errCode = RegOpenKeyExW(hKeyRoot, keyPath, 0, KEY_READ, &hKey);
542 if (errCode != ERROR_SUCCESS) goto done;
544 count = ListView_GetItemCount(hwndLV);
545 for (i = 0; i < count; i++) {
546 item.mask = LVIF_PARAM;
547 item.iItem = i;
548 SendMessageW( hwndLV, LVM_GETITEM, 0, (LPARAM)&item );
549 HeapFree(GetProcessHeap(), 0, ((LINE_INFO*)item.lParam)->name);
550 HeapFree(GetProcessHeap(), 0, (void*)item.lParam);
552 g_columnToSort = ~0U;
553 SendMessageW( hwndLV, LVM_DELETEALLITEMS, 0, 0L );
555 /* get size information and resize the buffers if necessary */
556 errCode = RegQueryInfoKeyW(hKey, NULL, NULL, NULL, NULL, &max_sub_key_len, NULL,
557 &val_count, &max_val_name_len, &max_val_size, NULL, NULL);
558 if (errCode != ERROR_SUCCESS) goto done;
560 /* account for the terminator char */
561 max_val_name_len++;
562 max_val_size++;
564 valName = HeapAlloc(GetProcessHeap(), 0, max_val_name_len * sizeof(WCHAR));
565 valBuf = HeapAlloc(GetProcessHeap(), 0, max_val_size);
566 if (RegQueryValueExW(hKey, NULL, NULL, &valType, valBuf, &valSize) == ERROR_FILE_NOT_FOUND) {
567 AddEntryToList(hwndLV, NULL, REG_SZ, NULL, 0, !highlightValue);
569 for(index = 0; index < val_count; index++) {
570 BOOL bSelected = (valName == highlightValue); /* NOT a bug, we check for double NULL here */
571 valNameLen = max_val_name_len;
572 valSize = max_val_size;
573 valType = 0;
574 errCode = RegEnumValueW(hKey, index, valName, &valNameLen, NULL, &valType, valBuf, &valSize);
575 if (errCode != ERROR_SUCCESS) goto done;
576 valBuf[valSize] = 0;
577 if (valName && highlightValue && !lstrcmpW(valName, highlightValue))
578 bSelected = TRUE;
579 AddEntryToList(hwndLV, valName[0] ? valName : NULL, valType, valBuf, valSize, bSelected);
581 SendMessageW(hwndLV, LVM_SORTITEMS, (WPARAM)hwndLV, (LPARAM)CompareFunc);
583 g_currentRootKey = hKeyRoot;
584 if (keyPath != g_currentPath) {
585 HeapFree(GetProcessHeap(), 0, g_currentPath);
586 g_currentPath = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(keyPath) + 1) * sizeof(WCHAR));
587 if (!g_currentPath) goto done;
588 lstrcpyW(g_currentPath, keyPath);
591 result = TRUE;
593 done:
594 HeapFree(GetProcessHeap(), 0, valBuf);
595 HeapFree(GetProcessHeap(), 0, valName);
596 SendMessageW(hwndLV, WM_SETREDRAW, TRUE, 0);
597 if (hKey) RegCloseKey(hKey);
599 return result;