Update the address of the Free Software Foundation.
[wine/testsucceed.git] / programs / regedit / listview.c
blobcddeea0bb6dca9a218daa342d1b927c784e0212e
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"
29 #include "wine/debug.h"
30 #include "wine/unicode.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(regedit);
34 static INT Image_String;
35 static INT Image_Binary;
37 typedef struct tagLINE_INFO
39 DWORD dwValType;
40 LPTSTR name;
41 void* val;
42 size_t val_len;
43 } LINE_INFO;
45 /*******************************************************************************
46 * Global and Local Variables:
49 static WNDPROC g_orgListWndProc;
50 static DWORD g_columnToSort = ~0UL;
51 static BOOL g_invertSort = FALSE;
52 static LPTSTR g_valueName;
53 static LPTSTR g_currentPath;
54 static HKEY g_currentRootKey;
56 #define MAX_LIST_COLUMNS (IDS_LIST_COLUMN_LAST - IDS_LIST_COLUMN_FIRST + 1)
57 static int default_column_widths[MAX_LIST_COLUMNS] = { 200, 175, 400 };
58 static int column_alignment[MAX_LIST_COLUMNS] = { LVCFMT_LEFT, LVCFMT_LEFT, LVCFMT_LEFT };
60 static LPTSTR get_item_text(HWND hwndLV, int item)
62 LPTSTR newStr, curStr;
63 unsigned int maxLen = 128;
65 curStr = HeapAlloc(GetProcessHeap(), 0, maxLen);
66 if (!curStr) return NULL;
67 if (item == 0) return NULL; /* first item is ALWAYS a default */
68 do {
69 ListView_GetItemText(hwndLV, item, 0, curStr, maxLen);
70 if (_tcslen(curStr) < maxLen - 1) return curStr;
71 newStr = HeapReAlloc(GetProcessHeap(), 0, curStr, maxLen * 2);
72 if (!newStr) break;
73 curStr = newStr;
74 maxLen *= 2;
75 } while (TRUE);
76 HeapFree(GetProcessHeap(), 0, curStr);
77 return NULL;
80 LPCTSTR GetValueName(HWND hwndLV)
82 INT item;
84 if (g_valueName && g_valueName != LPSTR_TEXTCALLBACK)
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 = get_item_text(hwndLV, item);
93 return g_valueName;
96 /* convert '\0' separated string list into ',' separated string list */
97 static void MakeMULTISZDisplayable(LPTSTR 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, LPTSTR Name, DWORD dwValType,
115 void* ValBuf, DWORD dwCount, BOOL bHighlight)
117 LINE_INFO* linfo;
118 LVITEM 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 memcpy(&linfo[1], ValBuf, dwCount);
126 if (Name)
127 linfo->name = _tcsdup(Name);
128 else
129 linfo->name = NULL;
131 item.mask = LVIF_TEXT | LVIF_PARAM | LVIF_STATE | LVIF_IMAGE;
132 item.iItem = ListView_GetItemCount(hwndLV);/*idx; */
133 item.iSubItem = 0;
134 item.state = 0;
135 item.stateMask = LVIS_FOCUSED | LVIS_SELECTED;
136 item.pszText = Name ? Name : LPSTR_TEXTCALLBACK;
137 item.cchTextMax = Name ? _tcslen(item.pszText) : 0;
138 if (bHighlight) {
139 item.stateMask = item.state = LVIS_FOCUSED | LVIS_SELECTED;
141 switch (dwValType)
143 case REG_SZ:
144 case REG_EXPAND_SZ:
145 case REG_MULTI_SZ:
146 item.iImage = Image_String;
147 break;
148 default:
149 item.iImage = Image_Binary;
150 break;
152 item.lParam = (LPARAM)linfo;
154 #if (_WIN32_IE >= 0x0300)
155 item.iIndent = 0;
156 #endif
158 index = ListView_InsertItem(hwndLV, &item);
159 if (index != -1) {
160 /* LPTSTR pszText = NULL; */
161 LPTSTR pszText = _T("(cannot display value)");
162 switch (dwValType) {
163 case REG_SZ:
164 case REG_EXPAND_SZ:
165 if (ValBuf) {
166 ListView_SetItemText(hwndLV, index, 2, ValBuf);
167 } else {
168 ListView_SetItemText(hwndLV, index, 2, "(not set)");
170 break;
171 case REG_DWORD: {
172 TCHAR buf[64];
173 wsprintf(buf, _T("0x%08X (%d)"), *(DWORD*)ValBuf, *(DWORD*)ValBuf);
174 ListView_SetItemText(hwndLV, index, 2, buf);
176 /* lpsRes = convertHexToDWORDStr(lpbData, dwLen); */
177 break;
178 case REG_BINARY: {
179 unsigned int i;
180 LPBYTE pData = (LPBYTE)ValBuf;
181 LPTSTR strBinary = HeapAlloc(GetProcessHeap(), 0, dwCount * sizeof(TCHAR) * 3 + 1);
182 for (i = 0; i < dwCount; i++)
183 wsprintf( strBinary + i*3, _T("%02X "), pData[i] );
184 strBinary[dwCount * 3] = 0;
185 ListView_SetItemText(hwndLV, index, 2, strBinary);
186 HeapFree(GetProcessHeap(), 0, strBinary);
188 break;
189 case REG_MULTI_SZ:
190 MakeMULTISZDisplayable(ValBuf);
191 ListView_SetItemText(hwndLV, index, 2, ValBuf);
192 break;
193 default:
194 /* lpsRes = convertHexToHexCSV(lpbData, dwLen); */
195 ListView_SetItemText(hwndLV, index, 2, pszText);
196 break;
201 static BOOL InitListViewImageList(HWND hWndListView)
203 HIMAGELIST himl;
204 HICON hicon;
205 INT cx = GetSystemMetrics(SM_CXSMICON);
206 INT cy = GetSystemMetrics(SM_CYSMICON);
208 himl = ImageList_Create(cx, cy, ILC_MASK, 0, 2);
209 if (!himl)
210 return FALSE;
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)
224 return FALSE;
226 return TRUE;
229 static BOOL CreateListColumns(HWND hWndListView)
231 TCHAR szText[50];
232 int index;
233 LV_COLUMN lvC;
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;
247 return TRUE;
250 /* OnGetDispInfo - processes the LVN_GETDISPINFO notification message. */
252 static void OnGetDispInfo(NMLVDISPINFO* plvdi)
254 static TCHAR buffer[200];
256 plvdi->item.pszText = NULL;
257 plvdi->item.cchTextMax = 0;
259 switch (plvdi->item.iSubItem) {
260 case 0:
261 plvdi->item.pszText = (LPSTR)g_pszDefaultValueName;
262 break;
263 case 1:
264 switch (((LINE_INFO*)plvdi->item.lParam)->dwValType) {
265 case REG_SZ:
266 plvdi->item.pszText = _T("REG_SZ");
267 break;
268 case REG_EXPAND_SZ:
269 plvdi->item.pszText = _T("REG_EXPAND_SZ");
270 break;
271 case REG_BINARY:
272 plvdi->item.pszText = _T("REG_BINARY");
273 break;
274 case REG_DWORD:
275 plvdi->item.pszText = _T("REG_DWORD");
276 break;
277 case REG_DWORD_BIG_ENDIAN:
278 plvdi->item.pszText = _T("REG_DWORD_BIG_ENDIAN");
279 break;
280 case REG_MULTI_SZ:
281 plvdi->item.pszText = _T("REG_MULTI_SZ");
282 break;
283 case REG_LINK:
284 plvdi->item.pszText = _T("REG_LINK");
285 break;
286 case REG_RESOURCE_LIST:
287 plvdi->item.pszText = _T("REG_RESOURCE_LIST");
288 break;
289 case REG_NONE:
290 plvdi->item.pszText = _T("REG_NONE");
291 break;
292 default:
293 wsprintf(buffer, _T("unknown(%d)"), plvdi->item.lParam);
294 plvdi->item.pszText = buffer;
295 break;
297 break;
298 case 2:
299 plvdi->item.pszText = _T("(value not set)");
300 break;
301 case 3:
302 plvdi->item.pszText = _T("");
303 break;
307 static int CALLBACK CompareFunc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
309 LINE_INFO*l, *r;
310 l = (LINE_INFO*)lParam1;
311 r = (LINE_INFO*)lParam2;
312 if (!l->name) return -1;
313 if (!r->name) return +1;
315 if (g_columnToSort == ~0UL)
316 g_columnToSort = 0;
318 if (g_columnToSort == 1 && l->dwValType != r->dwValType)
319 return g_invertSort ? (int)r->dwValType - (int)l->dwValType : (int)l->dwValType - (int)r->dwValType;
320 if (g_columnToSort == 2) {
321 /* FIXME: Sort on value */
323 return g_invertSort ? _tcscmp(r->name, l->name) : _tcscmp(l->name, r->name);
326 HWND StartValueRename(HWND hwndLV)
328 int item;
330 item = ListView_GetNextItem(hwndLV, -1, LVNI_FOCUSED | LVNI_SELECTED);
331 if (item < 1) { /* cannot rename default key */
332 MessageBeep(MB_ICONHAND);
333 return 0;
335 return ListView_EditLabel(hwndLV, item);
338 static BOOL _CmdWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
340 switch (LOWORD(wParam)) {
341 /* case ID_FILE_OPEN: */
342 /* break; */
343 default:
344 return FALSE;
346 return TRUE;
349 static LRESULT CALLBACK ListWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
351 switch (message) {
352 case WM_COMMAND:
353 if (!_CmdWndProc(hWnd, message, wParam, lParam)) {
354 return CallWindowProc(g_orgListWndProc, hWnd, message, wParam, lParam);
356 break;
357 case WM_NOTIFY_REFLECT:
358 switch (((LPNMHDR)lParam)->code) {
360 case LVN_BEGINLABELEDIT:
361 if (!((NMLVDISPINFO *)lParam)->item.iItem)
362 return 1;
363 return 0;
364 case LVN_GETDISPINFO:
365 OnGetDispInfo((NMLVDISPINFO*)lParam);
366 break;
367 case LVN_COLUMNCLICK:
368 if (g_columnToSort == ((LPNMLISTVIEW)lParam)->iSubItem)
369 g_invertSort = !g_invertSort;
370 else {
371 g_columnToSort = ((LPNMLISTVIEW)lParam)->iSubItem;
372 g_invertSort = FALSE;
375 SendMessage(hWnd, LVM_SORTITEMS, (WPARAM)hWnd, (LPARAM)CompareFunc);
376 break;
377 case LVN_ENDLABELEDIT: {
378 LPNMLVDISPINFO dispInfo = (LPNMLVDISPINFO)lParam;
379 LPTSTR valueName = get_item_text(hWnd, dispInfo->item.iItem);
380 LONG ret;
381 if (!valueName) return -1; /* cannot rename a default value */
382 ret = RenameValue(hWnd, g_currentRootKey, g_currentPath, valueName, dispInfo->item.pszText);
383 if (ret)
384 RefreshListView(hWnd, g_currentRootKey, g_currentPath, dispInfo->item.pszText);
385 HeapFree(GetProcessHeap(), 0, valueName);
386 return 0;
388 case NM_RETURN: {
389 int cnt = ListView_GetNextItem(hWnd, -1, LVNI_FOCUSED | LVNI_SELECTED);
390 if (cnt != -1)
391 SendMessage(hFrameWnd, WM_COMMAND, ID_EDIT_MODIFY, 0);
393 break;
394 case NM_DBLCLK: {
395 NMITEMACTIVATE* nmitem = (LPNMITEMACTIVATE)lParam;
396 LVHITTESTINFO info;
398 /* if (nmitem->hdr.hwndFrom != hWnd) break; unnecessary because of WM_NOTIFY_REFLECT */
399 /* if (nmitem->hdr.idFrom != IDW_LISTVIEW) break; */
400 /* if (nmitem->hdr.code != ???) break; */
401 #ifdef _MSC_VER
402 switch (nmitem->uKeyFlags) {
403 case LVKF_ALT: /* The ALT key is pressed. */
404 /* properties dialog box ? */
405 break;
406 case LVKF_CONTROL: /* The CTRL key is pressed. */
407 /* run dialog box for providing parameters... */
408 break;
409 case LVKF_SHIFT: /* The SHIFT key is pressed. */
410 break;
412 #endif
413 info.pt.x = nmitem->ptAction.x;
414 info.pt.y = nmitem->ptAction.y;
415 if (ListView_HitTest(hWnd, &info) != -1) {
416 ListView_SetItemState(hWnd, -1, 0, LVIS_FOCUSED|LVIS_SELECTED);
417 ListView_SetItemState(hWnd, info.iItem, LVIS_FOCUSED|LVIS_SELECTED,
418 LVIS_FOCUSED|LVIS_SELECTED);
419 SendMessage(hFrameWnd, WM_COMMAND, ID_EDIT_MODIFY, 0);
422 break;
424 default:
425 return 0; /* shouldn't call default ! */
427 break;
428 case WM_CONTEXTMENU: {
429 int cnt = ListView_GetNextItem(hWnd, -1, LVNI_SELECTED);
430 TrackPopupMenu(GetSubMenu(hPopupMenus, cnt == -1 ? PM_NEW : PM_MODIFYVALUE),
431 TPM_RIGHTBUTTON, (short)LOWORD(lParam), (short)HIWORD(lParam),
432 0, hFrameWnd, NULL);
433 break;
435 default:
436 return CallWindowProc(g_orgListWndProc, hWnd, message, wParam, lParam);
437 break;
439 return 0;
443 HWND CreateListView(HWND hwndParent, int id)
445 RECT rcClient;
446 HWND hwndLV;
448 /* Get the dimensions of the parent window's client area, and create the list view control. */
449 GetClientRect(hwndParent, &rcClient);
450 hwndLV = CreateWindowEx(WS_EX_CLIENTEDGE, WC_LISTVIEW, _T("List View"),
451 WS_VISIBLE | WS_CHILD | WS_TABSTOP | LVS_REPORT | LVS_EDITLABELS,
452 0, 0, rcClient.right, rcClient.bottom,
453 hwndParent, (HMENU)id, hInst, NULL);
454 if (!hwndLV) return NULL;
455 SendMessage(hwndLV, LVM_SETEXTENDEDLISTVIEWSTYLE, 0, LVS_EX_FULLROWSELECT);
457 /* Initialize the image list */
458 if (!InitListViewImageList(hwndLV)) goto fail;
459 if (!CreateListColumns(hwndLV)) goto fail;
460 g_orgListWndProc = (WNDPROC) SetWindowLongPtr(hwndLV, GWLP_WNDPROC, (LPARAM)ListWndProc);
461 return hwndLV;
462 fail:
463 DestroyWindow(hwndLV);
464 return NULL;
467 BOOL RefreshListView(HWND hwndLV, HKEY hKeyRoot, LPCTSTR keyPath, LPCTSTR highlightValue)
469 BOOL result = FALSE;
470 DWORD max_sub_key_len;
471 DWORD max_val_name_len, valNameLen;
472 DWORD max_val_size, valSize;
473 DWORD val_count, index, valType;
474 TCHAR* valName = 0;
475 BYTE* valBuf = 0;
476 HKEY hKey = 0;
477 LONG errCode;
478 INT count, i;
479 LVITEM item;
481 if (!hwndLV) return FALSE;
483 SendMessage(hwndLV, WM_SETREDRAW, FALSE, 0);
485 errCode = RegOpenKeyEx(hKeyRoot, keyPath, 0, KEY_READ, &hKey);
486 if (errCode != ERROR_SUCCESS) goto done;
488 count = ListView_GetItemCount(hwndLV);
489 for (i = 0; i < count; i++) {
490 item.mask = LVIF_PARAM;
491 item.iItem = i;
492 SendMessage( hwndLV, LVM_GETITEM, 0, (LPARAM)&item );
493 free(((LINE_INFO*)item.lParam)->name);
494 HeapFree(GetProcessHeap(), 0, (void*)item.lParam);
496 g_columnToSort = ~0UL;
497 SendMessage( hwndLV, LVM_DELETEALLITEMS, 0, 0L );
499 /* get size information and resize the buffers if necessary */
500 errCode = RegQueryInfoKey(hKey, NULL, NULL, NULL, NULL, &max_sub_key_len, NULL,
501 &val_count, &max_val_name_len, &max_val_size, NULL, NULL);
502 if (errCode != ERROR_SUCCESS) goto done;
504 /* account for the terminator char */
505 max_val_name_len++;
506 max_val_size++;
508 valName = HeapAlloc(GetProcessHeap(), 0, max_val_name_len * sizeof(TCHAR));
509 valBuf = HeapAlloc(GetProcessHeap(), 0, max_val_size);
510 if (RegQueryValueEx(hKey, NULL, NULL, &valType, valBuf, &valSize) == ERROR_FILE_NOT_FOUND) {
511 AddEntryToList(hwndLV, NULL, REG_SZ, NULL, 0, !highlightValue);
513 /*dwValSize = max_val_size; */
514 for(index = 0; index < val_count; index++) {
515 BOOL bSelected = (valName == highlightValue); /* NOT a bug, we check for double NULL here */
516 valNameLen = max_val_name_len;
517 valSize = max_val_size;
518 valType = 0;
519 errCode = RegEnumValue(hKey, index, valName, &valNameLen, NULL, &valType, valBuf, &valSize);
520 if (errCode != ERROR_SUCCESS) goto done;
521 valBuf[valSize] = 0;
522 if (valName && highlightValue && !_tcscmp(valName, highlightValue))
523 bSelected = TRUE;
524 AddEntryToList(hwndLV, valName[0] ? valName : NULL, valType, valBuf, valSize, bSelected);
526 SendMessage(hwndLV, LVM_SORTITEMS, (WPARAM)hwndLV, (LPARAM)CompareFunc);
528 g_currentRootKey = hKeyRoot;
529 if (keyPath != g_currentPath) {
530 HeapFree(GetProcessHeap(), 0, g_currentPath);
531 g_currentPath = HeapAlloc(GetProcessHeap(), 0, (lstrlen(keyPath) + 1) * sizeof(TCHAR));
532 if (!g_currentPath) goto done;
533 lstrcpy(g_currentPath, keyPath);
536 result = TRUE;
538 done:
539 HeapFree(GetProcessHeap(), 0, valBuf);
540 HeapFree(GetProcessHeap(), 0, valName);
541 SendMessage(hwndLV, WM_SETREDRAW, TRUE, 0);
542 if (hKey) RegCloseKey(hKey);
544 return result;