wined3d: Don't evict resources that are currently mapped in wined3d_device_evict_mana...
[wine/multimedia.git] / programs / regedit / listview.c
blob655911651871c1a8f3969d03815e246fb30edf92
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;
61 if (item == 0) return NULL; /* first item is ALWAYS a default */
63 curStr = HeapAlloc(GetProcessHeap(), 0, maxLen * sizeof(WCHAR));
64 if (!curStr) return NULL;
65 do {
66 ListView_GetItemTextW(hwndLV, item, 0, curStr, maxLen);
67 if (lstrlenW(curStr) < maxLen - 1) return curStr;
68 maxLen *= 2;
69 newStr = HeapReAlloc(GetProcessHeap(), 0, curStr, maxLen * sizeof(WCHAR));
70 if (!newStr) break;
71 curStr = newStr;
72 } while (TRUE);
73 HeapFree(GetProcessHeap(), 0, curStr);
74 return NULL;
77 LPCWSTR GetValueName(HWND hwndLV)
79 INT item;
81 if (g_valueName != LPSTR_TEXTCALLBACKW)
82 HeapFree(GetProcessHeap(), 0, g_valueName);
83 g_valueName = NULL;
85 item = SendMessageW(hwndLV, LVM_GETNEXTITEM, -1, MAKELPARAM(LVNI_FOCUSED, 0));
86 if (item == -1) return NULL;
88 g_valueName = GetItemText(hwndLV, item);
90 return g_valueName;
93 /* convert '\0' separated string list into ',' separated string list */
94 static void MakeMULTISZDisplayable(LPWSTR multi)
98 for (; *multi; multi++)
100 if (*(multi+1))
102 *multi = ',';
103 multi++;
105 } while (*multi);
108 /*******************************************************************************
109 * Local module support methods
111 static void AddEntryToList(HWND hwndLV, LPWSTR Name, DWORD dwValType,
112 void* ValBuf, DWORD dwCount, BOOL bHighlight)
114 LINE_INFO* linfo;
115 LVITEMW item;
116 int index;
118 linfo = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(LINE_INFO) + dwCount);
119 linfo->dwValType = dwValType;
120 linfo->val_len = dwCount;
121 CopyMemory(&linfo[1], ValBuf, dwCount);
123 if (Name)
125 linfo->name = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(Name) + 1) * sizeof(WCHAR));
126 lstrcpyW(linfo->name, Name);
127 } else
129 linfo->name = NULL;
132 item.mask = LVIF_TEXT | LVIF_PARAM | LVIF_STATE | LVIF_IMAGE;
133 item.iItem = SendMessageW(hwndLV, LVM_GETITEMCOUNT, 0, 0);
134 item.iSubItem = 0;
135 item.state = 0;
136 item.stateMask = LVIS_FOCUSED | LVIS_SELECTED;
137 item.pszText = Name ? Name : LPSTR_TEXTCALLBACKW;
138 item.cchTextMax = Name ? lstrlenW(item.pszText) : 0;
139 if (bHighlight) {
140 item.stateMask = item.state = LVIS_FOCUSED | LVIS_SELECTED;
142 switch (dwValType)
144 case REG_SZ:
145 case REG_EXPAND_SZ:
146 case REG_MULTI_SZ:
147 item.iImage = Image_String;
148 break;
149 default:
150 item.iImage = Image_Binary;
151 break;
153 item.lParam = (LPARAM)linfo;
155 #if (_WIN32_IE >= 0x0300)
156 item.iIndent = 0;
157 #endif
159 index = ListView_InsertItemW(hwndLV, &item);
160 if (index != -1) {
161 switch (dwValType) {
162 case REG_SZ:
163 case REG_EXPAND_SZ:
164 if (ValBuf) {
165 ListView_SetItemTextW(hwndLV, index, 2, ValBuf);
166 } else {
167 ListView_SetItemTextW(hwndLV, index, 2, g_szValueNotSet);
169 break;
170 case REG_DWORD: {
171 WCHAR buf[64];
172 WCHAR format[] = {'0','x','%','0','8','x',' ','(','%','u',')',0};
173 wsprintfW(buf, format, *(DWORD*)ValBuf, *(DWORD*)ValBuf);
174 ListView_SetItemTextW(hwndLV, index, 2, buf);
176 break;
177 case REG_BINARY: {
178 unsigned int i;
179 LPBYTE pData = ValBuf;
180 LPWSTR strBinary = HeapAlloc(GetProcessHeap(), 0, dwCount * sizeof(WCHAR) * 3 + sizeof(WCHAR));
181 WCHAR format[] = {'%','0','2','X',' ',0};
182 for (i = 0; i < dwCount; i++)
183 wsprintfW( strBinary + i*3, format, pData[i] );
184 strBinary[dwCount * 3] = 0;
185 ListView_SetItemTextW(hwndLV, index, 2, strBinary);
186 HeapFree(GetProcessHeap(), 0, strBinary);
188 break;
189 case REG_MULTI_SZ:
190 MakeMULTISZDisplayable(ValBuf);
191 ListView_SetItemTextW(hwndLV, index, 2, ValBuf);
192 break;
193 default:
195 WCHAR szText[128];
196 LoadStringW(hInst, IDS_REGISTRY_VALUE_CANT_DISPLAY, szText, COUNT_OF(szText));
197 ListView_SetItemTextW(hwndLV, index, 2, szText);
198 break;
204 static BOOL InitListViewImageList(HWND hWndListView)
206 HIMAGELIST himl;
207 HICON hicon;
208 INT cx = GetSystemMetrics(SM_CXSMICON);
209 INT cy = GetSystemMetrics(SM_CYSMICON);
211 himl = ImageList_Create(cx, cy, ILC_MASK, 0, 2);
212 if (!himl)
213 return FALSE;
215 hicon = LoadImageW(hInst, MAKEINTRESOURCEW(IDI_STRING),
216 IMAGE_ICON, cx, cy, LR_DEFAULTCOLOR);
217 Image_String = ImageList_AddIcon(himl, hicon);
219 hicon = LoadImageW(hInst, MAKEINTRESOURCEW(IDI_BIN),
220 IMAGE_ICON, cx, cy, LR_DEFAULTCOLOR);
221 Image_Binary = ImageList_AddIcon(himl, hicon);
223 SendMessageW( hWndListView, LVM_SETIMAGELIST, LVSIL_SMALL, (LPARAM) himl );
225 /* fail if some of the icons failed to load */
226 if (ImageList_GetImageCount(himl) < 2)
227 return FALSE;
229 return TRUE;
232 static BOOL CreateListColumns(HWND hWndListView)
234 WCHAR szText[50];
235 int index;
236 LVCOLUMNW lvC;
238 /* Create columns. */
239 lvC.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM;
240 lvC.pszText = szText;
242 /* Load the column labels from the resource file. */
243 for (index = 0; index < MAX_LIST_COLUMNS; index++) {
244 lvC.iSubItem = index;
245 lvC.cx = default_column_widths[index];
246 lvC.fmt = column_alignment[index];
247 LoadStringW(hInst, IDS_LIST_COLUMN_FIRST + index, szText, sizeof(szText)/sizeof(WCHAR));
248 if (ListView_InsertColumnW(hWndListView, index, &lvC) == -1) return FALSE;
250 return TRUE;
253 /* OnGetDispInfo - processes the LVN_GETDISPINFO notification message. */
255 static void OnGetDispInfo(NMLVDISPINFOW* plvdi)
257 static WCHAR buffer[200];
258 static WCHAR reg_szT[] = {'R','E','G','_','S','Z',0},
259 reg_expand_szT[] = {'R','E','G','_','E','X','P','A','N','D','_','S','Z',0},
260 reg_binaryT[] = {'R','E','G','_','B','I','N','A','R','Y',0},
261 reg_dwordT[] = {'R','E','G','_','D','W','O','R','D',0},
262 reg_dword_big_endianT[] = {'R','E','G','_','D','W','O','R','D','_',
263 'B','I','G','_','E','N','D','I','A','N',0},
264 reg_multi_szT[] = {'R','E','G','_','M','U','L','T','I','_','S','Z',0},
265 reg_linkT[] = {'R','E','G','_','L','I','N','K',0},
266 reg_resource_listT[] = {'R','E','G','_','R','E','S','O','U','R','C','E','_','L','I','S','T',0},
267 reg_noneT[] = {'R','E','G','_','N','O','N','E',0},
268 emptyT[] = {0};
270 plvdi->item.pszText = NULL;
271 plvdi->item.cchTextMax = 0;
273 switch (plvdi->item.iSubItem) {
274 case 0:
275 plvdi->item.pszText = g_pszDefaultValueName;
276 break;
277 case 1:
278 switch (((LINE_INFO*)plvdi->item.lParam)->dwValType) {
279 case REG_SZ:
280 plvdi->item.pszText = reg_szT;
281 break;
282 case REG_EXPAND_SZ:
283 plvdi->item.pszText = reg_expand_szT;
284 break;
285 case REG_BINARY:
286 plvdi->item.pszText = reg_binaryT;
287 break;
288 case REG_DWORD:
289 plvdi->item.pszText = reg_dwordT;
290 break;
291 case REG_DWORD_BIG_ENDIAN:
292 plvdi->item.pszText = reg_dword_big_endianT;
293 break;
294 case REG_MULTI_SZ:
295 plvdi->item.pszText = reg_multi_szT;
296 break;
297 case REG_LINK:
298 plvdi->item.pszText = reg_linkT;
299 break;
300 case REG_RESOURCE_LIST:
301 plvdi->item.pszText = reg_resource_listT;
302 break;
303 case REG_NONE:
304 plvdi->item.pszText = reg_noneT;
305 break;
306 default:
308 WCHAR szUnknownFmt[64];
309 LoadStringW(hInst, IDS_REGISTRY_UNKNOWN_TYPE, szUnknownFmt, COUNT_OF(szUnknownFmt));
310 wsprintfW(buffer, szUnknownFmt, plvdi->item.lParam);
311 plvdi->item.pszText = buffer;
312 break;
315 break;
316 case 2:
317 plvdi->item.pszText = g_szValueNotSet;
318 break;
319 case 3:
320 plvdi->item.pszText = emptyT;
321 break;
325 static int CALLBACK CompareFunc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
327 LINE_INFO*l, *r;
328 l = (LINE_INFO*)lParam1;
329 r = (LINE_INFO*)lParam2;
330 if (!l->name) return -1;
331 if (!r->name) return +1;
333 if (g_columnToSort == ~0U)
334 g_columnToSort = 0;
336 if (g_columnToSort == 1 && l->dwValType != r->dwValType)
337 return g_invertSort ? (int)r->dwValType - (int)l->dwValType : (int)l->dwValType - (int)r->dwValType;
338 if (g_columnToSort == 2) {
339 /* FIXME: Sort on value */
341 return g_invertSort ? lstrcmpiW(r->name, l->name) : lstrcmpiW(l->name, r->name);
344 HWND StartValueRename(HWND hwndLV)
346 int item;
348 item = SendMessageW(hwndLV, LVM_GETNEXTITEM, -1, MAKELPARAM(LVNI_FOCUSED | LVNI_SELECTED, 0));
349 if (item < 1) { /* cannot rename default key */
350 MessageBeep(MB_ICONHAND);
351 return 0;
353 return (HWND)SendMessageW(hwndLV, LVM_EDITLABELW, item, 0);
356 static BOOL _CmdWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
358 switch (LOWORD(wParam)) {
359 /* case ID_FILE_OPEN: */
360 /* break; */
361 default:
362 return FALSE;
364 return TRUE;
367 static LRESULT CALLBACK ListWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
369 switch (message) {
370 case WM_COMMAND:
371 if (!_CmdWndProc(hWnd, message, wParam, lParam)) {
372 return CallWindowProcW(g_orgListWndProc, hWnd, message, wParam, lParam);
374 break;
375 case WM_NOTIFY_REFLECT:
376 switch (((LPNMHDR)lParam)->code) {
378 case LVN_BEGINLABELEDITW:
379 if (!((NMLVDISPINFOW *)lParam)->item.iItem)
380 return 1;
381 return 0;
382 case LVN_GETDISPINFOW:
383 OnGetDispInfo((NMLVDISPINFOW*)lParam);
384 break;
385 case LVN_COLUMNCLICK:
386 if (g_columnToSort == ((LPNMLISTVIEW)lParam)->iSubItem)
387 g_invertSort = !g_invertSort;
388 else {
389 g_columnToSort = ((LPNMLISTVIEW)lParam)->iSubItem;
390 g_invertSort = FALSE;
393 SendMessageW(hWnd, LVM_SORTITEMS, (WPARAM)hWnd, (LPARAM)CompareFunc);
394 break;
395 case LVN_ENDLABELEDITW: {
396 LPNMLVDISPINFOW dispInfo = (LPNMLVDISPINFOW)lParam;
397 LPWSTR oldName = GetItemText(hWnd, dispInfo->item.iItem);
398 LONG ret;
399 if (!oldName) return -1; /* cannot rename a default value */
400 ret = RenameValue(hWnd, g_currentRootKey, g_currentPath, oldName, dispInfo->item.pszText);
401 if (ret)
403 RefreshListView(hWnd, g_currentRootKey, g_currentPath, dispInfo->item.pszText);
405 HeapFree(GetProcessHeap(), 0, oldName);
406 return 0;
408 case NM_RETURN: {
409 int cnt = SendMessageW(hWnd, LVM_GETNEXTITEM, -1, MAKELPARAM(LVNI_FOCUSED | LVNI_SELECTED, 0));
410 if (cnt != -1)
411 SendMessageW(hFrameWnd, WM_COMMAND, ID_EDIT_MODIFY, 0);
413 break;
414 case NM_DBLCLK: {
415 NMITEMACTIVATE* nmitem = (LPNMITEMACTIVATE)lParam;
416 LVHITTESTINFO info;
418 /* if (nmitem->hdr.hwndFrom != hWnd) break; unnecessary because of WM_NOTIFY_REFLECT */
419 /* if (nmitem->hdr.idFrom != IDW_LISTVIEW) break; */
420 /* if (nmitem->hdr.code != ???) break; */
421 #ifdef _MSC_VER
422 switch (nmitem->uKeyFlags) {
423 case LVKF_ALT: /* The ALT key is pressed. */
424 /* properties dialog box ? */
425 break;
426 case LVKF_CONTROL: /* The CTRL key is pressed. */
427 /* run dialog box for providing parameters... */
428 break;
429 case LVKF_SHIFT: /* The SHIFT key is pressed. */
430 break;
432 #endif
433 info.pt.x = nmitem->ptAction.x;
434 info.pt.y = nmitem->ptAction.y;
435 if (SendMessageW(hWnd, LVM_HITTEST, 0, (LPARAM)&info) != -1) {
436 LVITEMW item;
438 item.state = 0;
439 item.stateMask = LVIS_FOCUSED | LVIS_SELECTED;
440 SendMessageW(hWnd, LVM_SETITEMSTATE, (UINT)-1, (LPARAM)&item);
442 item.state = LVIS_FOCUSED | LVIS_SELECTED;
443 item.stateMask = LVIS_FOCUSED | LVIS_SELECTED;
444 SendMessageW(hWnd, LVM_SETITEMSTATE, info.iItem, (LPARAM)&item);
446 SendMessageW(hFrameWnd, WM_COMMAND, ID_EDIT_MODIFY, 0);
449 break;
451 default:
452 return 0; /* shouldn't call default ! */
454 break;
455 case WM_CONTEXTMENU: {
456 int cnt = SendMessageW(hWnd, LVM_GETNEXTITEM, -1, MAKELPARAM(LVNI_SELECTED, 0));
457 TrackPopupMenu(GetSubMenu(hPopupMenus, cnt == -1 ? PM_NEW : PM_MODIFYVALUE),
458 TPM_RIGHTBUTTON, (short)LOWORD(lParam), (short)HIWORD(lParam),
459 0, hFrameWnd, NULL);
460 break;
462 default:
463 return CallWindowProcW(g_orgListWndProc, hWnd, message, wParam, lParam);
465 return 0;
469 HWND CreateListView(HWND hwndParent, UINT id)
471 RECT rcClient;
472 HWND hwndLV;
473 WCHAR ListView[] = {'L','i','s','t',' ','V','i','e','w',0};
475 /* prepare strings */
476 LoadStringW(hInst, IDS_REGISTRY_VALUE_NOT_SET, g_szValueNotSet, COUNT_OF(g_szValueNotSet));
478 /* Get the dimensions of the parent window's client area, and create the list view control. */
479 GetClientRect(hwndParent, &rcClient);
480 hwndLV = CreateWindowExW(WS_EX_CLIENTEDGE, WC_LISTVIEWW, ListView,
481 WS_VISIBLE | WS_CHILD | WS_TABSTOP | LVS_REPORT | LVS_EDITLABELS,
482 0, 0, rcClient.right, rcClient.bottom,
483 hwndParent, ULongToHandle(id), hInst, NULL);
484 if (!hwndLV) return NULL;
485 SendMessageW(hwndLV, LVM_SETUNICODEFORMAT, TRUE, 0);
486 SendMessageW(hwndLV, LVM_SETEXTENDEDLISTVIEWSTYLE, 0, LVS_EX_FULLROWSELECT);
488 /* Initialize the image list */
489 if (!InitListViewImageList(hwndLV)) goto fail;
490 if (!CreateListColumns(hwndLV)) goto fail;
491 g_orgListWndProc = (WNDPROC) SetWindowLongPtrW(hwndLV, GWLP_WNDPROC, (LPARAM)ListWndProc);
492 return hwndLV;
493 fail:
494 DestroyWindow(hwndLV);
495 return NULL;
498 BOOL RefreshListView(HWND hwndLV, HKEY hKeyRoot, LPCWSTR keyPath, LPCWSTR highlightValue)
500 BOOL result = FALSE;
501 DWORD max_sub_key_len;
502 DWORD max_val_name_len, valNameLen;
503 DWORD max_val_size, valSize;
504 DWORD val_count, index, valType;
505 WCHAR* valName = 0;
506 BYTE* valBuf = 0;
507 HKEY hKey = 0;
508 LONG errCode;
509 INT count, i;
510 LVITEMW item;
512 if (!hwndLV) return FALSE;
514 SendMessageW(hwndLV, WM_SETREDRAW, FALSE, 0);
516 errCode = RegOpenKeyExW(hKeyRoot, keyPath, 0, KEY_READ, &hKey);
517 if (errCode != ERROR_SUCCESS) goto done;
519 count = SendMessageW(hwndLV, LVM_GETITEMCOUNT, 0, 0);
520 for (i = 0; i < count; i++) {
521 item.mask = LVIF_PARAM;
522 item.iItem = i;
523 SendMessageW( hwndLV, LVM_GETITEMW, 0, (LPARAM)&item );
524 HeapFree(GetProcessHeap(), 0, ((LINE_INFO*)item.lParam)->name);
525 HeapFree(GetProcessHeap(), 0, (void*)item.lParam);
527 g_columnToSort = ~0U;
528 SendMessageW( hwndLV, LVM_DELETEALLITEMS, 0, 0L );
530 /* get size information and resize the buffers if necessary */
531 errCode = RegQueryInfoKeyW(hKey, NULL, NULL, NULL, NULL, &max_sub_key_len, NULL,
532 &val_count, &max_val_name_len, &max_val_size, NULL, NULL);
533 if (errCode != ERROR_SUCCESS) goto done;
535 /* account for the terminator char */
536 max_val_name_len++;
537 max_val_size++;
539 valName = HeapAlloc(GetProcessHeap(), 0, max_val_name_len * sizeof(WCHAR));
540 if (!valName)
541 goto done;
542 valBuf = HeapAlloc(GetProcessHeap(), 0, max_val_size);
543 if (!valBuf)
544 goto done;
546 if (RegQueryValueExW(hKey, NULL, NULL, &valType, valBuf, &valSize) == ERROR_FILE_NOT_FOUND) {
547 AddEntryToList(hwndLV, NULL, REG_SZ, NULL, 0, !highlightValue);
549 for(index = 0; index < val_count; index++) {
550 BOOL bSelected = (valName == highlightValue); /* NOT a bug, we check for double NULL here */
551 valNameLen = max_val_name_len;
552 valSize = max_val_size;
553 valType = 0;
554 errCode = RegEnumValueW(hKey, index, valName, &valNameLen, NULL, &valType, valBuf, &valSize);
555 if (errCode != ERROR_SUCCESS) goto done;
556 valBuf[valSize] = 0;
557 if (highlightValue && !lstrcmpW(valName, highlightValue))
558 bSelected = TRUE;
559 AddEntryToList(hwndLV, valName[0] ? valName : NULL, valType, valBuf, valSize, bSelected);
561 SendMessageW(hwndLV, LVM_SORTITEMS, (WPARAM)hwndLV, (LPARAM)CompareFunc);
563 g_currentRootKey = hKeyRoot;
564 if (keyPath != g_currentPath) {
565 HeapFree(GetProcessHeap(), 0, g_currentPath);
566 g_currentPath = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(keyPath) + 1) * sizeof(WCHAR));
567 if (!g_currentPath) goto done;
568 lstrcpyW(g_currentPath, keyPath);
571 result = TRUE;
573 done:
574 HeapFree(GetProcessHeap(), 0, valBuf);
575 HeapFree(GetProcessHeap(), 0, valName);
576 SendMessageW(hwndLV, WM_SETREDRAW, TRUE, 0);
577 if (hKey) RegCloseKey(hKey);
579 return result;