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
30 #include "wine/unicode.h"
31 static INT Image_String;
32 static INT Image_Binary;
34 typedef struct tagLINE_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);
71 ListView_GetItemText(hwndLV, item, 0, curStr, maxLen);
72 if (_tcslen(curStr) < maxLen - 1) return curStr;
73 newStr = HeapReAlloc(GetProcessHeap(), 0, curStr, maxLen * 2);
78 HeapFree(GetProcessHeap(), 0, curStr);
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);
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));
101 HeapFree(GetProcessHeap(), 0, curStr);
105 LPCWSTR GetValueName(HWND hwndLV)
109 if (g_valueName != LPSTR_TEXTCALLBACKW)
110 HeapFree(GetProcessHeap(), 0, g_valueName);
113 item = ListView_GetNextItem(hwndLV, -1, LVNI_FOCUSED);
114 if (item == -1) return NULL;
116 g_valueName = GetItemTextW(hwndLV, item);
121 /* convert '\0' separated string list into ',' separated string list */
122 static void MakeMULTISZDisplayable(LPWSTR multi)
126 for (; *multi; multi++)
136 /*******************************************************************************
137 * Local module support methods
139 static void AddEntryToList(HWND hwndLV, LPWSTR Name, DWORD dwValType,
140 void* ValBuf, DWORD dwCount, BOOL bHighlight)
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);
153 linfo->name = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(Name) + 1) * sizeof(WCHAR));
154 lstrcpyW(linfo->name, Name);
160 item.mask = LVIF_TEXT | LVIF_PARAM | LVIF_STATE | LVIF_IMAGE;
161 item.iItem = ListView_GetItemCount(hwndLV);/*idx; */
164 item.stateMask = LVIS_FOCUSED | LVIS_SELECTED;
165 item.pszText = Name ? Name : LPSTR_TEXTCALLBACKW;
166 item.cchTextMax = Name ? lstrlenW(item.pszText) : 0;
168 item.stateMask = item.state = LVIS_FOCUSED | LVIS_SELECTED;
175 item.iImage = Image_String;
178 item.iImage = Image_Binary;
181 item.lParam = (LPARAM)linfo;
183 #if (_WIN32_IE >= 0x0300)
187 index = ListView_InsertItemW(hwndLV, &item);
193 ListView_SetItemTextW(hwndLV, index, 2, ValBuf);
195 ListView_SetItemTextW(hwndLV, index, 2, g_szValueNotSetW);
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);
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);
218 MakeMULTISZDisplayable(ValBuf);
219 ListView_SetItemTextW(hwndLV, index, 2, ValBuf);
224 LoadStringW(hInst, IDS_REGISTRY_VALUE_CANT_DISPLAY, szText, COUNT_OF(szText));
225 ListView_SetItemTextW(hwndLV, index, 2, szText);
232 static BOOL InitListViewImageList(HWND hWndListView)
236 INT cx = GetSystemMetrics(SM_CXSMICON);
237 INT cy = GetSystemMetrics(SM_CYSMICON);
239 himl = ImageList_Create(cx, cy, ILC_MASK, 0, 2);
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)
260 static BOOL CreateListColumns(HWND hWndListView)
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;
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},
298 plvdi->item.pszText = NULL;
299 plvdi->item.cchTextMax = 0;
301 switch (plvdi->item.iSubItem) {
303 plvdi->item.pszText = (LPSTR)g_pszDefaultValueName;
306 switch (((LINE_INFO*)plvdi->item.lParam)->dwValType) {
308 plvdi->item.pszText = reg_szT;
311 plvdi->item.pszText = reg_expand_szT;
314 plvdi->item.pszText = reg_binaryT;
317 plvdi->item.pszText = reg_dwordT;
319 case REG_DWORD_BIG_ENDIAN:
320 plvdi->item.pszText = reg_dword_big_endianT;
323 plvdi->item.pszText = reg_multi_szT;
326 plvdi->item.pszText = reg_linkT;
328 case REG_RESOURCE_LIST:
329 plvdi->item.pszText = reg_resource_listT;
332 plvdi->item.pszText = reg_noneT;
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;
345 plvdi->item.pszText = g_szValueNotSet;
348 plvdi->item.pszText = emptyT;
353 static int CALLBACK CompareFunc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
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)
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)
376 item = ListView_GetNextItem(hwndLV, -1, LVNI_FOCUSED | LVNI_SELECTED);
377 if (item < 1) { /* cannot rename default key */
378 MessageBeep(MB_ICONHAND);
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: */
395 static LRESULT CALLBACK ListWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
399 if (!_CmdWndProc(hWnd, message, wParam, lParam)) {
400 return CallWindowProc(g_orgListWndProc, hWnd, message, wParam, lParam);
403 case WM_NOTIFY_REFLECT:
404 switch (((LPNMHDR)lParam)->code) {
406 case LVN_BEGINLABELEDIT:
407 if (!((NMLVDISPINFO *)lParam)->item.iItem)
410 case LVN_GETDISPINFO:
411 OnGetDispInfo((NMLVDISPINFO*)lParam);
413 case LVN_COLUMNCLICK:
414 if (g_columnToSort == ((LPNMLISTVIEW)lParam)->iSubItem)
415 g_invertSort = !g_invertSort;
417 g_columnToSort = ((LPNMLISTVIEW)lParam)->iSubItem;
418 g_invertSort = FALSE;
421 SendMessage(hWnd, LVM_SORTITEMS, (WPARAM)hWnd, (LPARAM)CompareFunc);
423 case LVN_ENDLABELEDIT: {
424 LPNMLVDISPINFO dispInfo = (LPNMLVDISPINFO)lParam;
425 LPWSTR oldName = GetItemTextW(hWnd, dispInfo->item.iItem);
426 WCHAR* newName = GetWideString(dispInfo->item.pszText);
428 if (!oldName) return -1; /* cannot rename a default value */
429 ret = RenameValue(hWnd, g_currentRootKey, g_currentPath, oldName, newName);
432 RefreshListView(hWnd, g_currentRootKey, g_currentPath, newName);
434 HeapFree(GetProcessHeap(), 0, newName);
435 HeapFree(GetProcessHeap(), 0, oldName);
439 int cnt = ListView_GetNextItem(hWnd, -1, LVNI_FOCUSED | LVNI_SELECTED);
441 SendMessage(hFrameWnd, WM_COMMAND, ID_EDIT_MODIFY, 0);
445 NMITEMACTIVATE* nmitem = (LPNMITEMACTIVATE)lParam;
448 /* if (nmitem->hdr.hwndFrom != hWnd) break; unnecessary because of WM_NOTIFY_REFLECT */
449 /* if (nmitem->hdr.idFrom != IDW_LISTVIEW) break; */
450 /* if (nmitem->hdr.code != ???) break; */
452 switch (nmitem->uKeyFlags) {
453 case LVKF_ALT: /* The ALT key is pressed. */
454 /* properties dialog box ? */
456 case LVKF_CONTROL: /* The CTRL key is pressed. */
457 /* run dialog box for providing parameters... */
459 case LVKF_SHIFT: /* The SHIFT key is pressed. */
463 info.pt.x = nmitem->ptAction.x;
464 info.pt.y = nmitem->ptAction.y;
465 if (ListView_HitTest(hWnd, &info) != -1) {
466 ListView_SetItemState(hWnd, -1, 0, LVIS_FOCUSED|LVIS_SELECTED);
467 ListView_SetItemState(hWnd, info.iItem, LVIS_FOCUSED|LVIS_SELECTED,
468 LVIS_FOCUSED|LVIS_SELECTED);
469 SendMessage(hFrameWnd, WM_COMMAND, ID_EDIT_MODIFY, 0);
475 return 0; /* shouldn't call default ! */
478 case WM_CONTEXTMENU: {
479 int cnt = ListView_GetNextItem(hWnd, -1, LVNI_SELECTED);
480 TrackPopupMenu(GetSubMenu(hPopupMenus, cnt == -1 ? PM_NEW : PM_MODIFYVALUE),
481 TPM_RIGHTBUTTON, (short)LOWORD(lParam), (short)HIWORD(lParam),
486 return CallWindowProc(g_orgListWndProc, hWnd, message, wParam, lParam);
492 HWND CreateListView(HWND hwndParent, UINT id)
497 /* prepare strings */
498 LoadString(hInst, IDS_REGISTRY_VALUE_NOT_SET, g_szValueNotSet, COUNT_OF(g_szValueNotSet));
499 LoadStringW(hInst, IDS_REGISTRY_VALUE_NOT_SET, g_szValueNotSetW, COUNT_OF(g_szValueNotSetW));
501 /* Get the dimensions of the parent window's client area, and create the list view control. */
502 GetClientRect(hwndParent, &rcClient);
503 hwndLV = CreateWindowEx(WS_EX_CLIENTEDGE, WC_LISTVIEW, _T("List View"),
504 WS_VISIBLE | WS_CHILD | WS_TABSTOP | LVS_REPORT | LVS_EDITLABELS,
505 0, 0, rcClient.right, rcClient.bottom,
506 hwndParent, (HMENU)ULongToHandle(id), hInst, NULL);
507 if (!hwndLV) return NULL;
508 SendMessage(hwndLV, LVM_SETEXTENDEDLISTVIEWSTYLE, 0, LVS_EX_FULLROWSELECT);
510 /* Initialize the image list */
511 if (!InitListViewImageList(hwndLV)) goto fail;
512 if (!CreateListColumns(hwndLV)) goto fail;
513 g_orgListWndProc = (WNDPROC) SetWindowLongPtr(hwndLV, GWLP_WNDPROC, (LPARAM)ListWndProc);
516 DestroyWindow(hwndLV);
520 BOOL RefreshListView(HWND hwndLV, HKEY hKeyRoot, LPCWSTR keyPath, LPCWSTR highlightValue)
523 DWORD max_sub_key_len;
524 DWORD max_val_name_len, valNameLen;
525 DWORD max_val_size, valSize;
526 DWORD val_count, index, valType;
534 if (!hwndLV) return FALSE;
536 SendMessageW(hwndLV, WM_SETREDRAW, FALSE, 0);
538 errCode = RegOpenKeyExW(hKeyRoot, keyPath, 0, KEY_READ, &hKey);
539 if (errCode != ERROR_SUCCESS) goto done;
541 count = ListView_GetItemCount(hwndLV);
542 for (i = 0; i < count; i++) {
543 item.mask = LVIF_PARAM;
545 SendMessageW( hwndLV, LVM_GETITEM, 0, (LPARAM)&item );
546 HeapFree(GetProcessHeap(), 0, ((LINE_INFO*)item.lParam)->name);
547 HeapFree(GetProcessHeap(), 0, (void*)item.lParam);
549 g_columnToSort = ~0U;
550 SendMessageW( hwndLV, LVM_DELETEALLITEMS, 0, 0L );
552 /* get size information and resize the buffers if necessary */
553 errCode = RegQueryInfoKeyW(hKey, NULL, NULL, NULL, NULL, &max_sub_key_len, NULL,
554 &val_count, &max_val_name_len, &max_val_size, NULL, NULL);
555 if (errCode != ERROR_SUCCESS) goto done;
557 /* account for the terminator char */
561 valName = HeapAlloc(GetProcessHeap(), 0, max_val_name_len * sizeof(WCHAR));
562 valBuf = HeapAlloc(GetProcessHeap(), 0, max_val_size);
563 if (RegQueryValueExW(hKey, NULL, NULL, &valType, valBuf, &valSize) == ERROR_FILE_NOT_FOUND) {
564 AddEntryToList(hwndLV, NULL, REG_SZ, NULL, 0, !highlightValue);
566 for(index = 0; index < val_count; index++) {
567 BOOL bSelected = (valName == highlightValue); /* NOT a bug, we check for double NULL here */
568 valNameLen = max_val_name_len;
569 valSize = max_val_size;
571 errCode = RegEnumValueW(hKey, index, valName, &valNameLen, NULL, &valType, valBuf, &valSize);
572 if (errCode != ERROR_SUCCESS) goto done;
574 if (valName && highlightValue && !lstrcmpW(valName, highlightValue))
576 AddEntryToList(hwndLV, valName[0] ? valName : NULL, valType, valBuf, valSize, bSelected);
578 SendMessageW(hwndLV, LVM_SORTITEMS, (WPARAM)hwndLV, (LPARAM)CompareFunc);
580 g_currentRootKey = hKeyRoot;
581 if (keyPath != g_currentPath) {
582 HeapFree(GetProcessHeap(), 0, g_currentPath);
583 g_currentPath = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(keyPath) + 1) * sizeof(WCHAR));
584 if (!g_currentPath) goto done;
585 lstrcpyW(g_currentPath, keyPath);
591 HeapFree(GetProcessHeap(), 0, valBuf);
592 HeapFree(GetProcessHeap(), 0, valName);
593 SendMessageW(hwndLV, WM_SETREDRAW, TRUE, 0);
594 if (hKey) RegCloseKey(hKey);