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
29 #include "wine/unicode.h"
30 static INT Image_String;
31 static INT Image_Binary;
33 typedef struct tagLINE_INFO
41 /*******************************************************************************
42 * Global and Local Variables:
45 static WNDPROC g_orgListWndProc;
46 static DWORD g_columnToSort = ~0UL;
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 static LPTSTR get_item_text(HWND hwndLV, int 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) return NULL; /* first item is ALWAYS a default */
66 ListView_GetItemText(hwndLV, item, 0, curStr, maxLen);
67 if (_tcslen(curStr) < maxLen - 1) return curStr;
68 newStr = HeapReAlloc(GetProcessHeap(), 0, curStr, maxLen * 2);
73 HeapFree(GetProcessHeap(), 0, curStr);
77 LPCTSTR GetValueName(HWND hwndLV)
81 if (g_valueName != LPSTR_TEXTCALLBACK)
82 HeapFree(GetProcessHeap(), 0, g_valueName);
85 item = ListView_GetNextItem(hwndLV, -1, LVNI_FOCUSED);
86 if (item == -1) return NULL;
88 g_valueName = get_item_text(hwndLV, item);
93 /* convert '\0' separated string list into ',' separated string list */
94 static void MakeMULTISZDisplayable(LPTSTR multi)
98 for (; *multi; multi++)
108 /*******************************************************************************
109 * Local module support methods
111 static void AddEntryToList(HWND hwndLV, LPTSTR Name, DWORD dwValType,
112 void* ValBuf, DWORD dwCount, BOOL bHighlight)
118 linfo = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(LINE_INFO) + dwCount);
119 linfo->dwValType = dwValType;
120 linfo->val_len = dwCount;
121 memcpy(&linfo[1], ValBuf, dwCount);
124 linfo->name = _tcsdup(Name);
128 item.mask = LVIF_TEXT | LVIF_PARAM | LVIF_STATE | LVIF_IMAGE;
129 item.iItem = ListView_GetItemCount(hwndLV);/*idx; */
132 item.stateMask = LVIS_FOCUSED | LVIS_SELECTED;
133 item.pszText = Name ? Name : LPSTR_TEXTCALLBACK;
134 item.cchTextMax = Name ? _tcslen(item.pszText) : 0;
136 item.stateMask = item.state = LVIS_FOCUSED | LVIS_SELECTED;
143 item.iImage = Image_String;
146 item.iImage = Image_Binary;
149 item.lParam = (LPARAM)linfo;
151 #if (_WIN32_IE >= 0x0300)
155 index = ListView_InsertItem(hwndLV, &item);
157 /* LPTSTR pszText = NULL; */
162 ListView_SetItemText(hwndLV, index, 2, ValBuf);
164 ListView_SetItemText(hwndLV, index, 2, g_szValueNotSet);
169 wsprintf(buf, _T("0x%08X (%d)"), *(DWORD*)ValBuf, *(DWORD*)ValBuf);
170 ListView_SetItemText(hwndLV, index, 2, buf);
172 /* lpsRes = convertHexToDWORDStr(lpbData, dwLen); */
176 LPBYTE pData = (LPBYTE)ValBuf;
177 LPTSTR strBinary = HeapAlloc(GetProcessHeap(), 0, dwCount * sizeof(TCHAR) * 3 + 1);
178 for (i = 0; i < dwCount; i++)
179 wsprintf( strBinary + i*3, _T("%02X "), pData[i] );
180 strBinary[dwCount * 3] = 0;
181 ListView_SetItemText(hwndLV, index, 2, strBinary);
182 HeapFree(GetProcessHeap(), 0, strBinary);
186 MakeMULTISZDisplayable(ValBuf);
187 ListView_SetItemText(hwndLV, index, 2, ValBuf);
191 /* lpsRes = convertHexToHexCSV(lpbData, dwLen); */
193 LoadString(hInst, IDS_REGISTRY_VALUE_CANT_DISPLAY, szText, COUNT_OF(szText));
194 ListView_SetItemText(hwndLV, index, 2, szText);
201 static BOOL InitListViewImageList(HWND hWndListView)
205 INT cx = GetSystemMetrics(SM_CXSMICON);
206 INT cy = GetSystemMetrics(SM_CYSMICON);
208 himl = ImageList_Create(cx, cy, ILC_MASK, 0, 2);
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)
229 static BOOL CreateListColumns(HWND hWndListView)
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;
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},
267 plvdi->item.pszText = NULL;
268 plvdi->item.cchTextMax = 0;
270 switch (plvdi->item.iSubItem) {
272 plvdi->item.pszText = (LPSTR)g_pszDefaultValueName;
275 switch (((LINE_INFO*)plvdi->item.lParam)->dwValType) {
277 plvdi->item.pszText = reg_szT;
280 plvdi->item.pszText = reg_expand_szT;
283 plvdi->item.pszText = reg_binaryT;
286 plvdi->item.pszText = reg_dwordT;
288 case REG_DWORD_BIG_ENDIAN:
289 plvdi->item.pszText = reg_dword_big_endianT;
292 plvdi->item.pszText = reg_multi_szT;
295 plvdi->item.pszText = reg_linkT;
297 case REG_RESOURCE_LIST:
298 plvdi->item.pszText = reg_resource_listT;
301 plvdi->item.pszText = reg_noneT;
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;
314 plvdi->item.pszText = g_szValueNotSet;
317 plvdi->item.pszText = emptyT;
322 static int CALLBACK CompareFunc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
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 == ~0UL)
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)
345 item = ListView_GetNextItem(hwndLV, -1, LVNI_FOCUSED | LVNI_SELECTED);
346 if (item < 1) { /* cannot rename default key */
347 MessageBeep(MB_ICONHAND);
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: */
364 static LRESULT CALLBACK ListWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
368 if (!_CmdWndProc(hWnd, message, wParam, lParam)) {
369 return CallWindowProc(g_orgListWndProc, hWnd, message, wParam, lParam);
372 case WM_NOTIFY_REFLECT:
373 switch (((LPNMHDR)lParam)->code) {
375 case LVN_BEGINLABELEDIT:
376 if (!((NMLVDISPINFO *)lParam)->item.iItem)
379 case LVN_GETDISPINFO:
380 OnGetDispInfo((NMLVDISPINFO*)lParam);
382 case LVN_COLUMNCLICK:
383 if (g_columnToSort == ((LPNMLISTVIEW)lParam)->iSubItem)
384 g_invertSort = !g_invertSort;
386 g_columnToSort = ((LPNMLISTVIEW)lParam)->iSubItem;
387 g_invertSort = FALSE;
390 SendMessage(hWnd, LVM_SORTITEMS, (WPARAM)hWnd, (LPARAM)CompareFunc);
392 case LVN_ENDLABELEDIT: {
393 LPNMLVDISPINFO dispInfo = (LPNMLVDISPINFO)lParam;
394 LPTSTR valueName = get_item_text(hWnd, dispInfo->item.iItem);
396 if (!valueName) return -1; /* cannot rename a default value */
397 ret = RenameValue(hWnd, g_currentRootKey, g_currentPath, valueName, dispInfo->item.pszText);
399 RefreshListView(hWnd, g_currentRootKey, g_currentPath, dispInfo->item.pszText);
400 HeapFree(GetProcessHeap(), 0, valueName);
404 int cnt = ListView_GetNextItem(hWnd, -1, LVNI_FOCUSED | LVNI_SELECTED);
406 SendMessage(hFrameWnd, WM_COMMAND, ID_EDIT_MODIFY, 0);
410 NMITEMACTIVATE* nmitem = (LPNMITEMACTIVATE)lParam;
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; */
417 switch (nmitem->uKeyFlags) {
418 case LVKF_ALT: /* The ALT key is pressed. */
419 /* properties dialog box ? */
421 case LVKF_CONTROL: /* The CTRL key is pressed. */
422 /* run dialog box for providing parameters... */
424 case LVKF_SHIFT: /* The SHIFT key is pressed. */
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);
440 return 0; /* shouldn't call default ! */
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),
451 return CallWindowProc(g_orgListWndProc, hWnd, message, wParam, lParam);
458 HWND CreateListView(HWND hwndParent, int id)
463 /* prepare strings */
464 LoadString(hInst, IDS_REGISTRY_VALUE_NOT_SET, g_szValueNotSet, COUNT_OF(g_szValueNotSet));
466 /* Get the dimensions of the parent window's client area, and create the list view control. */
467 GetClientRect(hwndParent, &rcClient);
468 hwndLV = CreateWindowEx(WS_EX_CLIENTEDGE, WC_LISTVIEW, _T("List View"),
469 WS_VISIBLE | WS_CHILD | WS_TABSTOP | LVS_REPORT | LVS_EDITLABELS,
470 0, 0, rcClient.right, rcClient.bottom,
471 hwndParent, (HMENU)id, hInst, NULL);
472 if (!hwndLV) return NULL;
473 SendMessage(hwndLV, LVM_SETEXTENDEDLISTVIEWSTYLE, 0, LVS_EX_FULLROWSELECT);
475 /* Initialize the image list */
476 if (!InitListViewImageList(hwndLV)) goto fail;
477 if (!CreateListColumns(hwndLV)) goto fail;
478 g_orgListWndProc = (WNDPROC) SetWindowLongPtr(hwndLV, GWLP_WNDPROC, (LPARAM)ListWndProc);
481 DestroyWindow(hwndLV);
485 BOOL RefreshListView(HWND hwndLV, HKEY hKeyRoot, LPCTSTR keyPath, LPCTSTR highlightValue)
488 DWORD max_sub_key_len;
489 DWORD max_val_name_len, valNameLen;
490 DWORD max_val_size, valSize;
491 DWORD val_count, index, valType;
499 if (!hwndLV) return FALSE;
501 SendMessage(hwndLV, WM_SETREDRAW, FALSE, 0);
503 errCode = RegOpenKeyEx(hKeyRoot, keyPath, 0, KEY_READ, &hKey);
504 if (errCode != ERROR_SUCCESS) goto done;
506 count = ListView_GetItemCount(hwndLV);
507 for (i = 0; i < count; i++) {
508 item.mask = LVIF_PARAM;
510 SendMessage( hwndLV, LVM_GETITEM, 0, (LPARAM)&item );
511 free(((LINE_INFO*)item.lParam)->name);
512 HeapFree(GetProcessHeap(), 0, (void*)item.lParam);
514 g_columnToSort = ~0UL;
515 SendMessage( hwndLV, LVM_DELETEALLITEMS, 0, 0L );
517 /* get size information and resize the buffers if necessary */
518 errCode = RegQueryInfoKey(hKey, NULL, NULL, NULL, NULL, &max_sub_key_len, NULL,
519 &val_count, &max_val_name_len, &max_val_size, NULL, NULL);
520 if (errCode != ERROR_SUCCESS) goto done;
522 /* account for the terminator char */
526 valName = HeapAlloc(GetProcessHeap(), 0, max_val_name_len * sizeof(TCHAR));
527 valBuf = HeapAlloc(GetProcessHeap(), 0, max_val_size);
528 if (RegQueryValueEx(hKey, NULL, NULL, &valType, valBuf, &valSize) == ERROR_FILE_NOT_FOUND) {
529 AddEntryToList(hwndLV, NULL, REG_SZ, NULL, 0, !highlightValue);
531 for(index = 0; index < val_count; index++) {
532 BOOL bSelected = (valName == highlightValue); /* NOT a bug, we check for double NULL here */
533 valNameLen = max_val_name_len;
534 valSize = max_val_size;
536 errCode = RegEnumValue(hKey, index, valName, &valNameLen, NULL, &valType, valBuf, &valSize);
537 if (errCode != ERROR_SUCCESS) goto done;
539 if (valName && highlightValue && !_tcscmp(valName, highlightValue))
541 AddEntryToList(hwndLV, valName[0] ? valName : NULL, valType, valBuf, valSize, bSelected);
543 SendMessage(hwndLV, LVM_SORTITEMS, (WPARAM)hwndLV, (LPARAM)CompareFunc);
545 g_currentRootKey = hKeyRoot;
546 if (keyPath != g_currentPath) {
547 HeapFree(GetProcessHeap(), 0, g_currentPath);
548 g_currentPath = HeapAlloc(GetProcessHeap(), 0, (lstrlen(keyPath) + 1) * sizeof(TCHAR));
549 if (!g_currentPath) goto done;
550 lstrcpy(g_currentPath, keyPath);
556 HeapFree(GetProcessHeap(), 0, valBuf);
557 HeapFree(GetProcessHeap(), 0, valName);
558 SendMessage(hwndLV, WM_SETREDRAW, TRUE, 0);
559 if (hKey) RegCloseKey(hKey);