push 378fe7a60681a28e8b22f62dcfe122d585b92570
[wine/hacks.git] / programs / regedit / listview.c
blob333b2a45c234687f2af14d0255f396734934f715
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 <commctrl.h>
24 #include <stdlib.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 LPWSTR 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 LPWSTR g_valueName;
49 static LPWSTR g_currentPath;
50 static HKEY g_currentRootKey;
51 static WCHAR 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 LPWSTR GetItemText(HWND hwndLV, UINT item)
59 LPWSTR newStr, curStr;
60 unsigned int maxLen = 128;
62 curStr = HeapAlloc(GetProcessHeap(), 0, maxLen * sizeof(WCHAR));
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_GetItemTextW(hwndLV, item, 0, curStr, maxLen * sizeof(WCHAR));
70 if (lstrlenW(curStr) < maxLen - 1) return curStr;
71 newStr = HeapReAlloc(GetProcessHeap(), 0, curStr, maxLen * 2 * sizeof(WCHAR));
72 if (!newStr) break;
73 curStr = newStr;
74 maxLen *= 2;
75 } while (TRUE);
76 HeapFree(GetProcessHeap(), 0, curStr);
77 return NULL;
80 LPCWSTR GetValueName(HWND hwndLV)
82 INT item;
84 if (g_valueName != LPSTR_TEXTCALLBACKW)
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(LPWSTR 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, LPWSTR Name, DWORD dwValType,
115 void* ValBuf, DWORD dwCount, BOOL bHighlight)
117 LINE_INFO* linfo;
118 LVITEMW 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 CopyMemory(&linfo[1], ValBuf, dwCount);
126 if (Name)
128 linfo->name = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(Name) + 1) * sizeof(WCHAR));
129 lstrcpyW(linfo->name, Name);
130 } else
132 linfo->name = NULL;
135 item.mask = LVIF_TEXT | LVIF_PARAM | LVIF_STATE | LVIF_IMAGE;
136 item.iItem = ListView_GetItemCount(hwndLV);/*idx; */
137 item.iSubItem = 0;
138 item.state = 0;
139 item.stateMask = LVIS_FOCUSED | LVIS_SELECTED;
140 item.pszText = Name ? Name : LPSTR_TEXTCALLBACKW;
141 item.cchTextMax = Name ? lstrlenW(item.pszText) : 0;
142 if (bHighlight) {
143 item.stateMask = item.state = LVIS_FOCUSED | LVIS_SELECTED;
145 switch (dwValType)
147 case REG_SZ:
148 case REG_EXPAND_SZ:
149 case REG_MULTI_SZ:
150 item.iImage = Image_String;
151 break;
152 default:
153 item.iImage = Image_Binary;
154 break;
156 item.lParam = (LPARAM)linfo;
158 #if (_WIN32_IE >= 0x0300)
159 item.iIndent = 0;
160 #endif
162 index = ListView_InsertItemW(hwndLV, &item);
163 if (index != -1) {
164 switch (dwValType) {
165 case REG_SZ:
166 case REG_EXPAND_SZ:
167 if (ValBuf) {
168 ListView_SetItemTextW(hwndLV, index, 2, ValBuf);
169 } else {
170 ListView_SetItemTextW(hwndLV, index, 2, g_szValueNotSet);
172 break;
173 case REG_DWORD: {
174 WCHAR buf[64];
175 WCHAR format[] = {'0','x','%','0','8','x',' ','(','%','u',')',0};
176 wsprintfW(buf, format, *(DWORD*)ValBuf, *(DWORD*)ValBuf);
177 ListView_SetItemTextW(hwndLV, index, 2, buf);
179 break;
180 case REG_BINARY: {
181 unsigned int i;
182 LPBYTE pData = ValBuf;
183 LPWSTR strBinary = HeapAlloc(GetProcessHeap(), 0, dwCount * sizeof(WCHAR) * 3 + sizeof(WCHAR));
184 WCHAR format[] = {'%','0','2','X',' ',0};
185 for (i = 0; i < dwCount; i++)
186 wsprintfW( strBinary + i*3, format, pData[i] );
187 strBinary[dwCount * 3] = 0;
188 ListView_SetItemTextW(hwndLV, index, 2, strBinary);
189 HeapFree(GetProcessHeap(), 0, strBinary);
191 break;
192 case REG_MULTI_SZ:
193 MakeMULTISZDisplayable(ValBuf);
194 ListView_SetItemTextW(hwndLV, index, 2, ValBuf);
195 break;
196 default:
198 WCHAR szText[128];
199 LoadStringW(hInst, IDS_REGISTRY_VALUE_CANT_DISPLAY, szText, COUNT_OF(szText));
200 ListView_SetItemTextW(hwndLV, index, 2, szText);
201 break;
207 static BOOL InitListViewImageList(HWND hWndListView)
209 HIMAGELIST himl;
210 HICON hicon;
211 INT cx = GetSystemMetrics(SM_CXSMICON);
212 INT cy = GetSystemMetrics(SM_CYSMICON);
214 himl = ImageList_Create(cx, cy, ILC_MASK, 0, 2);
215 if (!himl)
216 return FALSE;
218 hicon = LoadImageW(hInst, MAKEINTRESOURCEW(IDI_STRING),
219 IMAGE_ICON, cx, cy, LR_DEFAULTCOLOR);
220 Image_String = ImageList_AddIcon(himl, hicon);
222 hicon = LoadImageW(hInst, MAKEINTRESOURCEW(IDI_BIN),
223 IMAGE_ICON, cx, cy, LR_DEFAULTCOLOR);
224 Image_Binary = ImageList_AddIcon(himl, hicon);
226 SendMessageW( hWndListView, LVM_SETIMAGELIST, LVSIL_SMALL, (LPARAM) himl );
228 /* fail if some of the icons failed to load */
229 if (ImageList_GetImageCount(himl) < 2)
230 return FALSE;
232 return TRUE;
235 static BOOL CreateListColumns(HWND hWndListView)
237 WCHAR szText[50];
238 int index;
239 LVCOLUMNW lvC;
241 /* Create columns. */
242 lvC.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM;
243 lvC.pszText = szText;
245 /* Load the column labels from the resource file. */
246 for (index = 0; index < MAX_LIST_COLUMNS; index++) {
247 lvC.iSubItem = index;
248 lvC.cx = default_column_widths[index];
249 lvC.fmt = column_alignment[index];
250 LoadStringW(hInst, IDS_LIST_COLUMN_FIRST + index, szText, sizeof(szText)/sizeof(WCHAR));
251 if (ListView_InsertColumnW(hWndListView, index, &lvC) == -1) return FALSE;
253 return TRUE;
256 /* OnGetDispInfo - processes the LVN_GETDISPINFO notification message. */
258 static void OnGetDispInfo(NMLVDISPINFOW* plvdi)
260 static WCHAR buffer[200];
261 static WCHAR reg_szT[] = {'R','E','G','_','S','Z',0},
262 reg_expand_szT[] = {'R','E','G','_','E','X','P','A','N','D','_','S','Z',0},
263 reg_binaryT[] = {'R','E','G','_','B','I','N','A','R','Y',0},
264 reg_dwordT[] = {'R','E','G','_','D','W','O','R','D',0},
265 reg_dword_big_endianT[] = {'R','E','G','_','D','W','O','R','D','_',
266 'B','I','G','_','E','N','D','I','A','N',0},
267 reg_multi_szT[] = {'R','E','G','_','M','U','L','T','I','_','S','Z',0},
268 reg_linkT[] = {'R','E','G','_','L','I','N','K',0},
269 reg_resource_listT[] = {'R','E','G','_','R','E','S','O','U','R','C','E','_','L','I','S','T',0},
270 reg_noneT[] = {'R','E','G','_','N','O','N','E',0},
271 emptyT[] = {0};
273 plvdi->item.pszText = NULL;
274 plvdi->item.cchTextMax = 0;
276 switch (plvdi->item.iSubItem) {
277 case 0:
278 plvdi->item.pszText = g_pszDefaultValueName;
279 break;
280 case 1:
281 switch (((LINE_INFO*)plvdi->item.lParam)->dwValType) {
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 szUnknownFmt[64];
312 LoadStringW(hInst, IDS_REGISTRY_UNKNOWN_TYPE, szUnknownFmt, COUNT_OF(szUnknownFmt));
313 wsprintfW(buffer, szUnknownFmt, plvdi->item.lParam);
314 plvdi->item.pszText = buffer;
315 break;
318 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 && l->dwValType != r->dwValType)
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 */
344 return g_invertSort ? lstrcmpiW(r->name, l->name) : lstrcmpiW(l->name, r->name);
347 HWND StartValueRename(HWND hwndLV)
349 int item;
351 item = ListView_GetNextItem(hwndLV, -1, LVNI_FOCUSED | LVNI_SELECTED);
352 if (item < 1) { /* cannot rename default key */
353 MessageBeep(MB_ICONHAND);
354 return 0;
356 return ListView_EditLabel(hwndLV, item);
359 static BOOL _CmdWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
361 switch (LOWORD(wParam)) {
362 /* case ID_FILE_OPEN: */
363 /* break; */
364 default:
365 return FALSE;
367 return TRUE;
370 static LRESULT CALLBACK ListWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
372 switch (message) {
373 case WM_COMMAND:
374 if (!_CmdWndProc(hWnd, message, wParam, lParam)) {
375 return CallWindowProc(g_orgListWndProc, hWnd, message, wParam, lParam);
377 break;
378 case WM_NOTIFY_REFLECT:
379 switch (((LPNMHDR)lParam)->code) {
381 case LVN_BEGINLABELEDITW:
382 if (!((NMLVDISPINFOW *)lParam)->item.iItem)
383 return 1;
384 return 0;
385 case LVN_GETDISPINFOW:
386 OnGetDispInfo((NMLVDISPINFOW*)lParam);
387 break;
388 case LVN_COLUMNCLICK:
389 if (g_columnToSort == ((LPNMLISTVIEW)lParam)->iSubItem)
390 g_invertSort = !g_invertSort;
391 else {
392 g_columnToSort = ((LPNMLISTVIEW)lParam)->iSubItem;
393 g_invertSort = FALSE;
396 SendMessageW(hWnd, LVM_SORTITEMS, (WPARAM)hWnd, (LPARAM)CompareFunc);
397 break;
398 case LVN_ENDLABELEDITW: {
399 LPNMLVDISPINFOW dispInfo = (LPNMLVDISPINFOW)lParam;
400 LPWSTR oldName = GetItemText(hWnd, dispInfo->item.iItem);
401 LONG ret;
402 if (!oldName) return -1; /* cannot rename a default value */
403 ret = RenameValue(hWnd, g_currentRootKey, g_currentPath, oldName, dispInfo->item.pszText);
404 if (ret)
406 RefreshListView(hWnd, g_currentRootKey, g_currentPath, dispInfo->item.pszText);
408 HeapFree(GetProcessHeap(), 0, oldName);
409 return 0;
411 case NM_RETURN: {
412 int cnt = ListView_GetNextItem(hWnd, -1, LVNI_FOCUSED | LVNI_SELECTED);
413 if (cnt != -1)
414 SendMessageW(hFrameWnd, WM_COMMAND, ID_EDIT_MODIFY, 0);
416 break;
417 case NM_DBLCLK: {
418 NMITEMACTIVATE* nmitem = (LPNMITEMACTIVATE)lParam;
419 LVHITTESTINFO info;
421 /* if (nmitem->hdr.hwndFrom != hWnd) break; unnecessary because of WM_NOTIFY_REFLECT */
422 /* if (nmitem->hdr.idFrom != IDW_LISTVIEW) break; */
423 /* if (nmitem->hdr.code != ???) break; */
424 #ifdef _MSC_VER
425 switch (nmitem->uKeyFlags) {
426 case LVKF_ALT: /* The ALT key is pressed. */
427 /* properties dialog box ? */
428 break;
429 case LVKF_CONTROL: /* The CTRL key is pressed. */
430 /* run dialog box for providing parameters... */
431 break;
432 case LVKF_SHIFT: /* The SHIFT key is pressed. */
433 break;
435 #endif
436 info.pt.x = nmitem->ptAction.x;
437 info.pt.y = nmitem->ptAction.y;
438 if (ListView_HitTest(hWnd, &info) != -1) {
439 ListView_SetItemState(hWnd, -1, 0, LVIS_FOCUSED|LVIS_SELECTED);
440 ListView_SetItemState(hWnd, info.iItem, LVIS_FOCUSED|LVIS_SELECTED,
441 LVIS_FOCUSED|LVIS_SELECTED);
442 SendMessageW(hFrameWnd, WM_COMMAND, ID_EDIT_MODIFY, 0);
445 break;
447 default:
448 return 0; /* shouldn't call default ! */
450 break;
451 case WM_CONTEXTMENU: {
452 int cnt = ListView_GetNextItem(hWnd, -1, LVNI_SELECTED);
453 TrackPopupMenu(GetSubMenu(hPopupMenus, cnt == -1 ? PM_NEW : PM_MODIFYVALUE),
454 TPM_RIGHTBUTTON, (short)LOWORD(lParam), (short)HIWORD(lParam),
455 0, hFrameWnd, NULL);
456 break;
458 default:
459 return CallWindowProc(g_orgListWndProc, hWnd, message, wParam, lParam);
461 return 0;
465 HWND CreateListView(HWND hwndParent, UINT id)
467 RECT rcClient;
468 HWND hwndLV;
469 WCHAR ListView[] = {'L','i','s','t',' ','V','i','e','w',0};
471 /* prepare strings */
472 LoadStringW(hInst, IDS_REGISTRY_VALUE_NOT_SET, g_szValueNotSet, COUNT_OF(g_szValueNotSet));
474 /* Get the dimensions of the parent window's client area, and create the list view control. */
475 GetClientRect(hwndParent, &rcClient);
476 hwndLV = CreateWindowExW(WS_EX_CLIENTEDGE, WC_LISTVIEWW, ListView,
477 WS_VISIBLE | WS_CHILD | WS_TABSTOP | LVS_REPORT | LVS_EDITLABELS,
478 0, 0, rcClient.right, rcClient.bottom,
479 hwndParent, ULongToHandle(id), hInst, NULL);
480 if (!hwndLV) return NULL;
481 SendMessageW(hwndLV, LVM_SETUNICODEFORMAT, TRUE, 0);
482 SendMessageW(hwndLV, LVM_SETEXTENDEDLISTVIEWSTYLE, 0, LVS_EX_FULLROWSELECT);
484 /* Initialize the image list */
485 if (!InitListViewImageList(hwndLV)) goto fail;
486 if (!CreateListColumns(hwndLV)) goto fail;
487 g_orgListWndProc = (WNDPROC) SetWindowLongPtrW(hwndLV, GWLP_WNDPROC, (LPARAM)ListWndProc);
488 return hwndLV;
489 fail:
490 DestroyWindow(hwndLV);
491 return NULL;
494 BOOL RefreshListView(HWND hwndLV, HKEY hKeyRoot, LPCWSTR keyPath, LPCWSTR highlightValue)
496 BOOL result = FALSE;
497 DWORD max_sub_key_len;
498 DWORD max_val_name_len, valNameLen;
499 DWORD max_val_size, valSize;
500 DWORD val_count, index, valType;
501 WCHAR* valName = 0;
502 BYTE* valBuf = 0;
503 HKEY hKey = 0;
504 LONG errCode;
505 INT count, i;
506 LVITEMW item;
508 if (!hwndLV) return FALSE;
510 SendMessageW(hwndLV, WM_SETREDRAW, FALSE, 0);
512 errCode = RegOpenKeyExW(hKeyRoot, keyPath, 0, KEY_READ, &hKey);
513 if (errCode != ERROR_SUCCESS) goto done;
515 count = ListView_GetItemCount(hwndLV);
516 for (i = 0; i < count; i++) {
517 item.mask = LVIF_PARAM;
518 item.iItem = i;
519 SendMessageW( hwndLV, LVM_GETITEM, 0, (LPARAM)&item );
520 HeapFree(GetProcessHeap(), 0, ((LINE_INFO*)item.lParam)->name);
521 HeapFree(GetProcessHeap(), 0, (void*)item.lParam);
523 g_columnToSort = ~0U;
524 SendMessageW( hwndLV, LVM_DELETEALLITEMS, 0, 0L );
526 /* get size information and resize the buffers if necessary */
527 errCode = RegQueryInfoKeyW(hKey, NULL, NULL, NULL, NULL, &max_sub_key_len, NULL,
528 &val_count, &max_val_name_len, &max_val_size, NULL, NULL);
529 if (errCode != ERROR_SUCCESS) goto done;
531 /* account for the terminator char */
532 max_val_name_len++;
533 max_val_size++;
535 valName = HeapAlloc(GetProcessHeap(), 0, max_val_name_len * sizeof(WCHAR));
536 if (!valName)
537 goto done;
538 valBuf = HeapAlloc(GetProcessHeap(), 0, max_val_size);
539 if (!valBuf)
540 goto done;
542 if (RegQueryValueExW(hKey, NULL, NULL, &valType, valBuf, &valSize) == ERROR_FILE_NOT_FOUND) {
543 AddEntryToList(hwndLV, NULL, REG_SZ, NULL, 0, !highlightValue);
545 for(index = 0; index < val_count; index++) {
546 BOOL bSelected = (valName == highlightValue); /* NOT a bug, we check for double NULL here */
547 valNameLen = max_val_name_len;
548 valSize = max_val_size;
549 valType = 0;
550 errCode = RegEnumValueW(hKey, index, valName, &valNameLen, NULL, &valType, valBuf, &valSize);
551 if (errCode != ERROR_SUCCESS) goto done;
552 valBuf[valSize] = 0;
553 if (highlightValue && !lstrcmpW(valName, highlightValue))
554 bSelected = TRUE;
555 AddEntryToList(hwndLV, valName[0] ? valName : NULL, valType, valBuf, valSize, bSelected);
557 SendMessageW(hwndLV, LVM_SORTITEMS, (WPARAM)hwndLV, (LPARAM)CompareFunc);
559 g_currentRootKey = hKeyRoot;
560 if (keyPath != g_currentPath) {
561 HeapFree(GetProcessHeap(), 0, g_currentPath);
562 g_currentPath = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(keyPath) + 1) * sizeof(WCHAR));
563 if (!g_currentPath) goto done;
564 lstrcpyW(g_currentPath, keyPath);
567 result = TRUE;
569 done:
570 HeapFree(GetProcessHeap(), 0, valBuf);
571 HeapFree(GetProcessHeap(), 0, valName);
572 SendMessageW(hwndLV, WM_SETREDRAW, TRUE, 0);
573 if (hKey) RegCloseKey(hKey);
575 return result;