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);
161 ListView_SetItemText(hwndLV, index, 2, ValBuf);
163 ListView_SetItemText(hwndLV, index, 2, g_szValueNotSet);
168 wsprintf(buf, _T("0x%08X (%d)"), *(DWORD*)ValBuf, *(DWORD*)ValBuf);
169 ListView_SetItemText(hwndLV, index, 2, buf);
174 LPBYTE pData = (LPBYTE)ValBuf;
175 LPTSTR strBinary = HeapAlloc(GetProcessHeap(), 0, dwCount * sizeof(TCHAR) * 3 + 1);
176 for (i = 0; i < dwCount; i++)
177 wsprintf( strBinary + i*3, _T("%02X "), pData[i] );
178 strBinary[dwCount * 3] = 0;
179 ListView_SetItemText(hwndLV, index, 2, strBinary);
180 HeapFree(GetProcessHeap(), 0, strBinary);
184 MakeMULTISZDisplayable(ValBuf);
185 ListView_SetItemText(hwndLV, index, 2, ValBuf);
190 LoadString(hInst, IDS_REGISTRY_VALUE_CANT_DISPLAY, szText, COUNT_OF(szText));
191 ListView_SetItemText(hwndLV, index, 2, szText);
198 static BOOL InitListViewImageList(HWND hWndListView)
202 INT cx = GetSystemMetrics(SM_CXSMICON);
203 INT cy = GetSystemMetrics(SM_CYSMICON);
205 himl = ImageList_Create(cx, cy, ILC_MASK, 0, 2);
209 hicon = LoadImage(hInst, MAKEINTRESOURCE(IDI_STRING),
210 IMAGE_ICON, cx, cy, LR_DEFAULTCOLOR);
211 Image_String = ImageList_AddIcon(himl, hicon);
213 hicon = LoadImage(hInst, MAKEINTRESOURCE(IDI_BIN),
214 IMAGE_ICON, cx, cy, LR_DEFAULTCOLOR);
215 Image_Binary = ImageList_AddIcon(himl, hicon);
217 SendMessage( hWndListView, LVM_SETIMAGELIST, LVSIL_SMALL, (LPARAM) himl );
219 /* fail if some of the icons failed to load */
220 if (ImageList_GetImageCount(himl) < 2)
226 static BOOL CreateListColumns(HWND hWndListView)
232 /* Create columns. */
233 lvC.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM;
234 lvC.pszText = szText;
236 /* Load the column labels from the resource file. */
237 for (index = 0; index < MAX_LIST_COLUMNS; index++) {
238 lvC.iSubItem = index;
239 lvC.cx = default_column_widths[index];
240 lvC.fmt = column_alignment[index];
241 LoadString(hInst, IDS_LIST_COLUMN_FIRST + index, szText, sizeof(szText)/sizeof(TCHAR));
242 if (ListView_InsertColumn(hWndListView, index, &lvC) == -1) return FALSE;
247 /* OnGetDispInfo - processes the LVN_GETDISPINFO notification message. */
249 static void OnGetDispInfo(NMLVDISPINFO* plvdi)
251 static TCHAR buffer[200];
252 static TCHAR reg_szT[] = {'R','E','G','_','S','Z',0},
253 reg_expand_szT[] = {'R','E','G','_','E','X','P','A','N','D','_','S','Z',0},
254 reg_binaryT[] = {'R','E','G','_','B','I','N','A','R','Y',0},
255 reg_dwordT[] = {'R','E','G','_','D','W','O','R','D',0},
256 reg_dword_big_endianT[] = {'R','E','G','_','D','W','O','R','D','_',
257 'B','I','G','_','E','N','D','I','A','N',0},
258 reg_multi_szT[] = {'R','E','G','_','M','U','L','T','I','_','S','Z',0},
259 reg_linkT[] = {'R','E','G','_','L','I','N','K',0},
260 reg_resource_listT[] = {'R','E','G','_','R','E','S','O','U','R','C','E','_','L','I','S','T',0},
261 reg_noneT[] = {'R','E','G','_','N','O','N','E',0},
264 plvdi->item.pszText = NULL;
265 plvdi->item.cchTextMax = 0;
267 switch (plvdi->item.iSubItem) {
269 plvdi->item.pszText = (LPSTR)g_pszDefaultValueName;
272 switch (((LINE_INFO*)plvdi->item.lParam)->dwValType) {
274 plvdi->item.pszText = reg_szT;
277 plvdi->item.pszText = reg_expand_szT;
280 plvdi->item.pszText = reg_binaryT;
283 plvdi->item.pszText = reg_dwordT;
285 case REG_DWORD_BIG_ENDIAN:
286 plvdi->item.pszText = reg_dword_big_endianT;
289 plvdi->item.pszText = reg_multi_szT;
292 plvdi->item.pszText = reg_linkT;
294 case REG_RESOURCE_LIST:
295 plvdi->item.pszText = reg_resource_listT;
298 plvdi->item.pszText = reg_noneT;
302 TCHAR szUnknownFmt[64];
303 LoadString(hInst, IDS_REGISTRY_UNKNOWN_TYPE, szUnknownFmt, COUNT_OF(szUnknownFmt));
304 wsprintf(buffer, szUnknownFmt, plvdi->item.lParam);
305 plvdi->item.pszText = buffer;
311 plvdi->item.pszText = g_szValueNotSet;
314 plvdi->item.pszText = emptyT;
319 static int CALLBACK CompareFunc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
322 l = (LINE_INFO*)lParam1;
323 r = (LINE_INFO*)lParam2;
324 if (!l->name) return -1;
325 if (!r->name) return +1;
327 if (g_columnToSort == ~0UL)
330 if (g_columnToSort == 1 && l->dwValType != r->dwValType)
331 return g_invertSort ? (int)r->dwValType - (int)l->dwValType : (int)l->dwValType - (int)r->dwValType;
332 if (g_columnToSort == 2) {
333 /* FIXME: Sort on value */
335 return g_invertSort ? _tcscmp(r->name, l->name) : _tcscmp(l->name, r->name);
338 HWND StartValueRename(HWND hwndLV)
342 item = ListView_GetNextItem(hwndLV, -1, LVNI_FOCUSED | LVNI_SELECTED);
343 if (item < 1) { /* cannot rename default key */
344 MessageBeep(MB_ICONHAND);
347 return ListView_EditLabel(hwndLV, item);
350 static BOOL _CmdWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
352 switch (LOWORD(wParam)) {
353 /* case ID_FILE_OPEN: */
361 static LRESULT CALLBACK ListWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
365 if (!_CmdWndProc(hWnd, message, wParam, lParam)) {
366 return CallWindowProc(g_orgListWndProc, hWnd, message, wParam, lParam);
369 case WM_NOTIFY_REFLECT:
370 switch (((LPNMHDR)lParam)->code) {
372 case LVN_BEGINLABELEDIT:
373 if (!((NMLVDISPINFO *)lParam)->item.iItem)
376 case LVN_GETDISPINFO:
377 OnGetDispInfo((NMLVDISPINFO*)lParam);
379 case LVN_COLUMNCLICK:
380 if (g_columnToSort == ((LPNMLISTVIEW)lParam)->iSubItem)
381 g_invertSort = !g_invertSort;
383 g_columnToSort = ((LPNMLISTVIEW)lParam)->iSubItem;
384 g_invertSort = FALSE;
387 SendMessage(hWnd, LVM_SORTITEMS, (WPARAM)hWnd, (LPARAM)CompareFunc);
389 case LVN_ENDLABELEDIT: {
390 LPNMLVDISPINFO dispInfo = (LPNMLVDISPINFO)lParam;
391 LPTSTR valueName = get_item_text(hWnd, dispInfo->item.iItem);
393 if (!valueName) return -1; /* cannot rename a default value */
394 ret = RenameValue(hWnd, g_currentRootKey, g_currentPath, valueName, dispInfo->item.pszText);
396 RefreshListView(hWnd, g_currentRootKey, g_currentPath, dispInfo->item.pszText);
397 HeapFree(GetProcessHeap(), 0, valueName);
401 int cnt = ListView_GetNextItem(hWnd, -1, LVNI_FOCUSED | LVNI_SELECTED);
403 SendMessage(hFrameWnd, WM_COMMAND, ID_EDIT_MODIFY, 0);
407 NMITEMACTIVATE* nmitem = (LPNMITEMACTIVATE)lParam;
410 /* if (nmitem->hdr.hwndFrom != hWnd) break; unnecessary because of WM_NOTIFY_REFLECT */
411 /* if (nmitem->hdr.idFrom != IDW_LISTVIEW) break; */
412 /* if (nmitem->hdr.code != ???) break; */
414 switch (nmitem->uKeyFlags) {
415 case LVKF_ALT: /* The ALT key is pressed. */
416 /* properties dialog box ? */
418 case LVKF_CONTROL: /* The CTRL key is pressed. */
419 /* run dialog box for providing parameters... */
421 case LVKF_SHIFT: /* The SHIFT key is pressed. */
425 info.pt.x = nmitem->ptAction.x;
426 info.pt.y = nmitem->ptAction.y;
427 if (ListView_HitTest(hWnd, &info) != -1) {
428 ListView_SetItemState(hWnd, -1, 0, LVIS_FOCUSED|LVIS_SELECTED);
429 ListView_SetItemState(hWnd, info.iItem, LVIS_FOCUSED|LVIS_SELECTED,
430 LVIS_FOCUSED|LVIS_SELECTED);
431 SendMessage(hFrameWnd, WM_COMMAND, ID_EDIT_MODIFY, 0);
437 return 0; /* shouldn't call default ! */
440 case WM_CONTEXTMENU: {
441 int cnt = ListView_GetNextItem(hWnd, -1, LVNI_SELECTED);
442 TrackPopupMenu(GetSubMenu(hPopupMenus, cnt == -1 ? PM_NEW : PM_MODIFYVALUE),
443 TPM_RIGHTBUTTON, (short)LOWORD(lParam), (short)HIWORD(lParam),
448 return CallWindowProc(g_orgListWndProc, hWnd, message, wParam, lParam);
454 HWND CreateListView(HWND hwndParent, int id)
459 /* prepare strings */
460 LoadString(hInst, IDS_REGISTRY_VALUE_NOT_SET, g_szValueNotSet, COUNT_OF(g_szValueNotSet));
462 /* Get the dimensions of the parent window's client area, and create the list view control. */
463 GetClientRect(hwndParent, &rcClient);
464 hwndLV = CreateWindowEx(WS_EX_CLIENTEDGE, WC_LISTVIEW, _T("List View"),
465 WS_VISIBLE | WS_CHILD | WS_TABSTOP | LVS_REPORT | LVS_EDITLABELS,
466 0, 0, rcClient.right, rcClient.bottom,
467 hwndParent, (HMENU)id, hInst, NULL);
468 if (!hwndLV) return NULL;
469 SendMessage(hwndLV, LVM_SETEXTENDEDLISTVIEWSTYLE, 0, LVS_EX_FULLROWSELECT);
471 /* Initialize the image list */
472 if (!InitListViewImageList(hwndLV)) goto fail;
473 if (!CreateListColumns(hwndLV)) goto fail;
474 g_orgListWndProc = (WNDPROC) SetWindowLongPtr(hwndLV, GWLP_WNDPROC, (LPARAM)ListWndProc);
477 DestroyWindow(hwndLV);
481 BOOL RefreshListView(HWND hwndLV, HKEY hKeyRoot, LPCTSTR keyPath, LPCTSTR highlightValue)
484 DWORD max_sub_key_len;
485 DWORD max_val_name_len, valNameLen;
486 DWORD max_val_size, valSize;
487 DWORD val_count, index, valType;
495 if (!hwndLV) return FALSE;
497 SendMessage(hwndLV, WM_SETREDRAW, FALSE, 0);
499 errCode = RegOpenKeyEx(hKeyRoot, keyPath, 0, KEY_READ, &hKey);
500 if (errCode != ERROR_SUCCESS) goto done;
502 count = ListView_GetItemCount(hwndLV);
503 for (i = 0; i < count; i++) {
504 item.mask = LVIF_PARAM;
506 SendMessage( hwndLV, LVM_GETITEM, 0, (LPARAM)&item );
507 free(((LINE_INFO*)item.lParam)->name);
508 HeapFree(GetProcessHeap(), 0, (void*)item.lParam);
510 g_columnToSort = ~0UL;
511 SendMessage( hwndLV, LVM_DELETEALLITEMS, 0, 0L );
513 /* get size information and resize the buffers if necessary */
514 errCode = RegQueryInfoKey(hKey, NULL, NULL, NULL, NULL, &max_sub_key_len, NULL,
515 &val_count, &max_val_name_len, &max_val_size, NULL, NULL);
516 if (errCode != ERROR_SUCCESS) goto done;
518 /* account for the terminator char */
522 valName = HeapAlloc(GetProcessHeap(), 0, max_val_name_len * sizeof(TCHAR));
523 valBuf = HeapAlloc(GetProcessHeap(), 0, max_val_size);
524 if (RegQueryValueEx(hKey, NULL, NULL, &valType, valBuf, &valSize) == ERROR_FILE_NOT_FOUND) {
525 AddEntryToList(hwndLV, NULL, REG_SZ, NULL, 0, !highlightValue);
527 for(index = 0; index < val_count; index++) {
528 BOOL bSelected = (valName == highlightValue); /* NOT a bug, we check for double NULL here */
529 valNameLen = max_val_name_len;
530 valSize = max_val_size;
532 errCode = RegEnumValue(hKey, index, valName, &valNameLen, NULL, &valType, valBuf, &valSize);
533 if (errCode != ERROR_SUCCESS) goto done;
535 if (valName && highlightValue && !_tcscmp(valName, highlightValue))
537 AddEntryToList(hwndLV, valName[0] ? valName : NULL, valType, valBuf, valSize, bSelected);
539 SendMessage(hwndLV, LVM_SORTITEMS, (WPARAM)hwndLV, (LPARAM)CompareFunc);
541 g_currentRootKey = hKeyRoot;
542 if (keyPath != g_currentPath) {
543 HeapFree(GetProcessHeap(), 0, g_currentPath);
544 g_currentPath = HeapAlloc(GetProcessHeap(), 0, (lstrlen(keyPath) + 1) * sizeof(TCHAR));
545 if (!g_currentPath) goto done;
546 lstrcpy(g_currentPath, keyPath);
552 HeapFree(GetProcessHeap(), 0, valBuf);
553 HeapFree(GetProcessHeap(), 0, valName);
554 SendMessage(hwndLV, WM_SETREDRAW, TRUE, 0);
555 if (hKey) RegCloseKey(hKey);