d2d1: Update "p0" after inserting a vertex in d2d_geometry_resolve_beziers().
[wine.git] / programs / regedit / listview.c
blobb9366e57e7cec967bc67964c4367cf37dafe40c2
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 <winternl.h>
24 #include <commctrl.h>
25 #include <stdlib.h>
26 #include <stdio.h>
28 #include "main.h"
30 #include "wine/unicode.h"
31 static INT Image_String;
32 static INT Image_Binary;
34 typedef struct tagLINE_INFO
36 DWORD dwValType;
37 LPWSTR name;
38 void* val;
39 size_t val_len;
40 } LINE_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_szValueNotSet[64];
54 #define MAX_LIST_COLUMNS (IDS_LIST_COLUMN_LAST - IDS_LIST_COLUMN_FIRST + 1)
55 static int default_column_widths[MAX_LIST_COLUMNS] = { 200, 175, 400 };
56 static int column_alignment[MAX_LIST_COLUMNS] = { LVCFMT_LEFT, LVCFMT_LEFT, LVCFMT_LEFT };
58 LPWSTR GetItemText(HWND hwndLV, UINT item)
60 LPWSTR newStr, curStr;
61 unsigned int maxLen = 128;
62 if (item == 0) return NULL; /* first item is ALWAYS a default */
64 curStr = HeapAlloc(GetProcessHeap(), 0, maxLen * sizeof(WCHAR));
65 if (!curStr) return NULL;
66 do {
67 ListView_GetItemTextW(hwndLV, item, 0, curStr, maxLen);
68 if (lstrlenW(curStr) < maxLen - 1) return curStr;
69 maxLen *= 2;
70 newStr = HeapReAlloc(GetProcessHeap(), 0, curStr, maxLen * sizeof(WCHAR));
71 if (!newStr) break;
72 curStr = newStr;
73 } while (TRUE);
74 HeapFree(GetProcessHeap(), 0, curStr);
75 return NULL;
78 LPCWSTR GetValueName(HWND hwndLV)
80 INT item;
82 if (g_valueName != LPSTR_TEXTCALLBACKW)
83 HeapFree(GetProcessHeap(), 0, g_valueName);
84 g_valueName = NULL;
86 item = SendMessageW(hwndLV, LVM_GETNEXTITEM, -1, MAKELPARAM(LVNI_FOCUSED, 0));
87 if (item == -1) return NULL;
89 g_valueName = GetItemText(hwndLV, item);
91 return g_valueName;
94 /* convert '\0' separated string list into ',' separated string list */
95 static void MakeMULTISZDisplayable(LPWSTR multi)
99 for (; *multi; multi++)
101 if (*(multi+1))
103 *multi = ',';
104 multi++;
106 } while (*multi);
109 /*******************************************************************************
110 * Local module support methods
112 static void AddEntryToList(HWND hwndLV, LPWSTR Name, DWORD dwValType,
113 void* ValBuf, DWORD dwCount, BOOL bHighlight)
115 LINE_INFO* linfo;
116 LVITEMW item;
117 int index;
119 linfo = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(LINE_INFO) + dwCount);
120 linfo->dwValType = dwValType;
121 linfo->val_len = dwCount;
122 CopyMemory(&linfo[1], ValBuf, dwCount);
124 if (Name)
126 linfo->name = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(Name) + 1) * sizeof(WCHAR));
127 lstrcpyW(linfo->name, Name);
128 } else
130 linfo->name = NULL;
133 item.mask = LVIF_TEXT | LVIF_PARAM | LVIF_STATE | LVIF_IMAGE;
134 item.iItem = SendMessageW(hwndLV, LVM_GETITEMCOUNT, 0, 0);
135 item.iSubItem = 0;
136 item.state = 0;
137 item.stateMask = LVIS_FOCUSED | LVIS_SELECTED;
138 item.pszText = Name ? Name : LPSTR_TEXTCALLBACKW;
139 item.cchTextMax = Name ? lstrlenW(item.pszText) : 0;
140 if (bHighlight) {
141 item.stateMask = item.state = LVIS_FOCUSED | LVIS_SELECTED;
143 switch (dwValType)
145 case REG_SZ:
146 case REG_EXPAND_SZ:
147 case REG_MULTI_SZ:
148 item.iImage = Image_String;
149 break;
150 default:
151 item.iImage = Image_Binary;
152 break;
154 item.lParam = (LPARAM)linfo;
156 #if (_WIN32_IE >= 0x0300)
157 item.iIndent = 0;
158 #endif
160 index = ListView_InsertItemW(hwndLV, &item);
161 if (index != -1) {
162 switch (dwValType) {
163 case REG_SZ:
164 case REG_EXPAND_SZ:
165 if (ValBuf) {
166 ListView_SetItemTextW(hwndLV, index, 2, ValBuf);
167 } else {
168 ListView_SetItemTextW(hwndLV, index, 2, g_szValueNotSet);
170 break;
171 case REG_DWORD:
172 case REG_DWORD_BIG_ENDIAN: {
173 DWORD value = *(DWORD*)ValBuf;
174 WCHAR buf[64];
175 WCHAR format[] = {'0','x','%','0','8','x',' ','(','%','u',')',0};
176 if (dwValType == REG_DWORD_BIG_ENDIAN)
177 value = RtlUlongByteSwap(value);
178 wsprintfW(buf, format, value, value);
179 ListView_SetItemTextW(hwndLV, index, 2, buf);
181 break;
182 case REG_BINARY:
183 case REG_NONE: {
184 unsigned int i;
185 LPBYTE pData = ValBuf;
186 LPWSTR strBinary = HeapAlloc(GetProcessHeap(), 0, dwCount * sizeof(WCHAR) * 3 + sizeof(WCHAR));
187 WCHAR format[] = {'%','0','2','X',' ',0};
188 for (i = 0; i < dwCount; i++)
189 wsprintfW( strBinary + i*3, format, pData[i] );
190 strBinary[dwCount * 3] = 0;
191 ListView_SetItemTextW(hwndLV, index, 2, strBinary);
192 HeapFree(GetProcessHeap(), 0, strBinary);
194 break;
195 case REG_MULTI_SZ:
196 MakeMULTISZDisplayable(ValBuf);
197 ListView_SetItemTextW(hwndLV, index, 2, ValBuf);
198 break;
199 default:
201 WCHAR szText[128];
202 LoadStringW(hInst, IDS_REGISTRY_VALUE_CANT_DISPLAY, szText, COUNT_OF(szText));
203 ListView_SetItemTextW(hwndLV, index, 2, szText);
204 break;
210 static BOOL InitListViewImageList(HWND hWndListView)
212 HIMAGELIST himl;
213 HICON hicon;
214 INT cx = GetSystemMetrics(SM_CXSMICON);
215 INT cy = GetSystemMetrics(SM_CYSMICON);
217 himl = ImageList_Create(cx, cy, ILC_MASK, 0, 2);
218 if (!himl)
219 return FALSE;
221 hicon = LoadImageW(hInst, MAKEINTRESOURCEW(IDI_STRING),
222 IMAGE_ICON, cx, cy, LR_DEFAULTCOLOR);
223 Image_String = ImageList_AddIcon(himl, hicon);
225 hicon = LoadImageW(hInst, MAKEINTRESOURCEW(IDI_BIN),
226 IMAGE_ICON, cx, cy, LR_DEFAULTCOLOR);
227 Image_Binary = ImageList_AddIcon(himl, hicon);
229 SendMessageW( hWndListView, LVM_SETIMAGELIST, LVSIL_SMALL, (LPARAM) himl );
231 /* fail if some of the icons failed to load */
232 if (ImageList_GetImageCount(himl) < 2)
233 return FALSE;
235 return TRUE;
238 static BOOL CreateListColumns(HWND hWndListView)
240 WCHAR szText[50];
241 int index;
242 LVCOLUMNW lvC;
244 /* Create columns. */
245 lvC.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM;
246 lvC.pszText = szText;
248 /* Load the column labels from the resource file. */
249 for (index = 0; index < MAX_LIST_COLUMNS; index++) {
250 lvC.iSubItem = index;
251 lvC.cx = default_column_widths[index];
252 lvC.fmt = column_alignment[index];
253 LoadStringW(hInst, IDS_LIST_COLUMN_FIRST + index, szText, sizeof(szText)/sizeof(WCHAR));
254 if (ListView_InsertColumnW(hWndListView, index, &lvC) == -1) return FALSE;
256 return TRUE;
259 /* OnGetDispInfo - processes the LVN_GETDISPINFO notification message. */
261 static void OnGetDispInfo(NMLVDISPINFOW* plvdi)
263 static WCHAR buffer[200];
264 static WCHAR reg_szT[] = {'R','E','G','_','S','Z',0},
265 reg_expand_szT[] = {'R','E','G','_','E','X','P','A','N','D','_','S','Z',0},
266 reg_binaryT[] = {'R','E','G','_','B','I','N','A','R','Y',0},
267 reg_dwordT[] = {'R','E','G','_','D','W','O','R','D',0},
268 reg_dword_big_endianT[] = {'R','E','G','_','D','W','O','R','D','_',
269 'B','I','G','_','E','N','D','I','A','N',0},
270 reg_multi_szT[] = {'R','E','G','_','M','U','L','T','I','_','S','Z',0},
271 reg_linkT[] = {'R','E','G','_','L','I','N','K',0},
272 reg_resource_listT[] = {'R','E','G','_','R','E','S','O','U','R','C','E','_','L','I','S','T',0},
273 reg_noneT[] = {'R','E','G','_','N','O','N','E',0},
274 emptyT[] = {0};
276 plvdi->item.pszText = NULL;
277 plvdi->item.cchTextMax = 0;
279 switch (plvdi->item.iSubItem) {
280 case 0:
281 plvdi->item.pszText = g_pszDefaultValueName;
282 break;
283 case 1:
284 switch (((LINE_INFO*)plvdi->item.lParam)->dwValType) {
285 case REG_SZ:
286 plvdi->item.pszText = reg_szT;
287 break;
288 case REG_EXPAND_SZ:
289 plvdi->item.pszText = reg_expand_szT;
290 break;
291 case REG_BINARY:
292 plvdi->item.pszText = reg_binaryT;
293 break;
294 case REG_DWORD:
295 plvdi->item.pszText = reg_dwordT;
296 break;
297 case REG_DWORD_BIG_ENDIAN:
298 plvdi->item.pszText = reg_dword_big_endianT;
299 break;
300 case REG_MULTI_SZ:
301 plvdi->item.pszText = reg_multi_szT;
302 break;
303 case REG_LINK:
304 plvdi->item.pszText = reg_linkT;
305 break;
306 case REG_RESOURCE_LIST:
307 plvdi->item.pszText = reg_resource_listT;
308 break;
309 case REG_NONE:
310 plvdi->item.pszText = reg_noneT;
311 break;
312 default:
314 WCHAR szUnknownFmt[64];
315 LoadStringW(hInst, IDS_REGISTRY_UNKNOWN_TYPE, szUnknownFmt, COUNT_OF(szUnknownFmt));
316 wsprintfW(buffer, szUnknownFmt, plvdi->item.lParam);
317 plvdi->item.pszText = buffer;
318 break;
321 break;
322 case 2:
323 plvdi->item.pszText = g_szValueNotSet;
324 break;
325 case 3:
326 plvdi->item.pszText = emptyT;
327 break;
331 static int CALLBACK CompareFunc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
333 LINE_INFO*l, *r;
334 l = (LINE_INFO*)lParam1;
335 r = (LINE_INFO*)lParam2;
336 if (!l->name) return -1;
337 if (!r->name) return +1;
339 if (g_columnToSort == ~0U)
340 g_columnToSort = 0;
342 if (g_columnToSort == 1)
343 return g_invertSort ? (int)r->dwValType - (int)l->dwValType : (int)l->dwValType - (int)r->dwValType;
344 if (g_columnToSort == 2) {
345 /* FIXME: Sort on value */
346 return 0;
348 return g_invertSort ? lstrcmpiW(r->name, l->name) : lstrcmpiW(l->name, r->name);
351 HWND StartValueRename(HWND hwndLV)
353 int item;
355 item = SendMessageW(hwndLV, LVM_GETNEXTITEM, -1, MAKELPARAM(LVNI_FOCUSED | LVNI_SELECTED, 0));
356 if (item < 1) { /* cannot rename default key */
357 MessageBeep(MB_ICONHAND);
358 return 0;
360 return (HWND)SendMessageW(hwndLV, LVM_EDITLABELW, item, 0);
363 static BOOL _CmdWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
365 switch (LOWORD(wParam)) {
366 /* case ID_FILE_OPEN: */
367 /* break; */
368 default:
369 return FALSE;
371 return TRUE;
374 static LRESULT CALLBACK ListWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
376 switch (message) {
377 case WM_COMMAND:
378 if (!_CmdWndProc(hWnd, message, wParam, lParam)) {
379 return CallWindowProcW(g_orgListWndProc, hWnd, message, wParam, lParam);
381 break;
382 case WM_NOTIFY_REFLECT:
383 switch (((LPNMHDR)lParam)->code) {
385 case LVN_BEGINLABELEDITW:
386 if (!((NMLVDISPINFOW *)lParam)->item.iItem)
387 return 1;
388 return 0;
389 case LVN_GETDISPINFOW:
390 OnGetDispInfo((NMLVDISPINFOW*)lParam);
391 break;
392 case LVN_COLUMNCLICK:
393 if (g_columnToSort == ((LPNMLISTVIEW)lParam)->iSubItem)
394 g_invertSort = !g_invertSort;
395 else {
396 g_columnToSort = ((LPNMLISTVIEW)lParam)->iSubItem;
397 g_invertSort = FALSE;
400 SendMessageW(hWnd, LVM_SORTITEMS, (WPARAM)hWnd, (LPARAM)CompareFunc);
401 break;
402 case LVN_ENDLABELEDITW: {
403 LPNMLVDISPINFOW dispInfo = (LPNMLVDISPINFOW)lParam;
404 LPWSTR oldName = GetItemText(hWnd, dispInfo->item.iItem);
405 LONG ret;
406 if (!oldName) return -1; /* cannot rename a default value */
407 ret = RenameValue(hWnd, g_currentRootKey, g_currentPath, oldName, dispInfo->item.pszText);
408 if (ret)
410 RefreshListView(hWnd, g_currentRootKey, g_currentPath, dispInfo->item.pszText);
412 HeapFree(GetProcessHeap(), 0, oldName);
413 return 0;
415 case NM_RETURN: {
416 int cnt = SendMessageW(hWnd, LVM_GETNEXTITEM, -1, MAKELPARAM(LVNI_FOCUSED | LVNI_SELECTED, 0));
417 if (cnt != -1)
418 SendMessageW(hFrameWnd, WM_COMMAND, ID_EDIT_MODIFY, 0);
420 break;
421 case NM_DBLCLK: {
422 NMITEMACTIVATE* nmitem = (LPNMITEMACTIVATE)lParam;
423 LVHITTESTINFO info;
425 /* if (nmitem->hdr.hwndFrom != hWnd) break; unnecessary because of WM_NOTIFY_REFLECT */
426 /* if (nmitem->hdr.idFrom != IDW_LISTVIEW) break; */
427 /* if (nmitem->hdr.code != ???) break; */
428 #ifdef _MSC_VER
429 switch (nmitem->uKeyFlags) {
430 case LVKF_ALT: /* The ALT key is pressed. */
431 /* properties dialog box ? */
432 break;
433 case LVKF_CONTROL: /* The CTRL key is pressed. */
434 /* run dialog box for providing parameters... */
435 break;
436 case LVKF_SHIFT: /* The SHIFT key is pressed. */
437 break;
439 #endif
440 info.pt.x = nmitem->ptAction.x;
441 info.pt.y = nmitem->ptAction.y;
442 if (SendMessageW(hWnd, LVM_HITTEST, 0, (LPARAM)&info) != -1) {
443 LVITEMW item;
445 item.state = 0;
446 item.stateMask = LVIS_FOCUSED | LVIS_SELECTED;
447 SendMessageW(hWnd, LVM_SETITEMSTATE, (UINT)-1, (LPARAM)&item);
449 item.state = LVIS_FOCUSED | LVIS_SELECTED;
450 item.stateMask = LVIS_FOCUSED | LVIS_SELECTED;
451 SendMessageW(hWnd, LVM_SETITEMSTATE, info.iItem, (LPARAM)&item);
453 SendMessageW(hFrameWnd, WM_COMMAND, ID_EDIT_MODIFY, 0);
456 break;
458 default:
459 return 0; /* shouldn't call default ! */
461 break;
462 case WM_CONTEXTMENU: {
463 int cnt = SendMessageW(hWnd, LVM_GETNEXTITEM, -1, MAKELPARAM(LVNI_SELECTED, 0));
464 TrackPopupMenu(GetSubMenu(hPopupMenus, cnt == -1 ? PM_NEW : PM_MODIFYVALUE),
465 TPM_RIGHTBUTTON, (short)LOWORD(lParam), (short)HIWORD(lParam),
466 0, hFrameWnd, NULL);
467 break;
469 default:
470 return CallWindowProcW(g_orgListWndProc, hWnd, message, wParam, lParam);
472 return 0;
476 HWND CreateListView(HWND hwndParent, UINT id)
478 RECT rcClient;
479 HWND hwndLV;
480 WCHAR ListView[] = {'L','i','s','t',' ','V','i','e','w',0};
482 /* prepare strings */
483 LoadStringW(hInst, IDS_REGISTRY_VALUE_NOT_SET, g_szValueNotSet, COUNT_OF(g_szValueNotSet));
485 /* Get the dimensions of the parent window's client area, and create the list view control. */
486 GetClientRect(hwndParent, &rcClient);
487 hwndLV = CreateWindowExW(WS_EX_CLIENTEDGE, WC_LISTVIEWW, ListView,
488 WS_VISIBLE | WS_CHILD | WS_TABSTOP | LVS_REPORT | LVS_EDITLABELS,
489 0, 0, rcClient.right, rcClient.bottom,
490 hwndParent, ULongToHandle(id), hInst, NULL);
491 if (!hwndLV) return NULL;
492 SendMessageW(hwndLV, LVM_SETUNICODEFORMAT, TRUE, 0);
493 SendMessageW(hwndLV, LVM_SETEXTENDEDLISTVIEWSTYLE, 0, LVS_EX_FULLROWSELECT);
495 /* Initialize the image list */
496 if (!InitListViewImageList(hwndLV)) goto fail;
497 if (!CreateListColumns(hwndLV)) goto fail;
498 g_orgListWndProc = (WNDPROC) SetWindowLongPtrW(hwndLV, GWLP_WNDPROC, (LPARAM)ListWndProc);
499 return hwndLV;
500 fail:
501 DestroyWindow(hwndLV);
502 return NULL;
505 BOOL RefreshListView(HWND hwndLV, HKEY hKeyRoot, LPCWSTR keyPath, LPCWSTR highlightValue)
507 BOOL result = FALSE;
508 DWORD max_sub_key_len;
509 DWORD max_val_name_len, valNameLen;
510 DWORD max_val_size, valSize;
511 DWORD val_count, index, valType;
512 WCHAR* valName = 0;
513 BYTE* valBuf = 0;
514 HKEY hKey = 0;
515 LONG errCode;
516 INT count, i;
517 LVITEMW item;
519 if (!hwndLV) return FALSE;
521 SendMessageW(hwndLV, WM_SETREDRAW, FALSE, 0);
523 errCode = RegOpenKeyExW(hKeyRoot, keyPath, 0, KEY_READ, &hKey);
524 if (errCode != ERROR_SUCCESS) goto done;
526 count = SendMessageW(hwndLV, LVM_GETITEMCOUNT, 0, 0);
527 for (i = 0; i < count; i++) {
528 item.mask = LVIF_PARAM;
529 item.iItem = i;
530 SendMessageW( hwndLV, LVM_GETITEMW, 0, (LPARAM)&item );
531 HeapFree(GetProcessHeap(), 0, ((LINE_INFO*)item.lParam)->name);
532 HeapFree(GetProcessHeap(), 0, (void*)item.lParam);
534 g_columnToSort = ~0U;
535 SendMessageW( hwndLV, LVM_DELETEALLITEMS, 0, 0L );
537 /* get size information and resize the buffers if necessary */
538 errCode = RegQueryInfoKeyW(hKey, NULL, NULL, NULL, NULL, &max_sub_key_len, NULL,
539 &val_count, &max_val_name_len, &max_val_size, NULL, NULL);
540 if (errCode != ERROR_SUCCESS) goto done;
542 /* account for the terminator char */
543 max_val_name_len++;
544 max_val_size++;
546 valName = HeapAlloc(GetProcessHeap(), 0, max_val_name_len * sizeof(WCHAR));
547 if (!valName)
548 goto done;
549 valBuf = HeapAlloc(GetProcessHeap(), 0, max_val_size);
550 if (!valBuf)
551 goto done;
553 if (RegQueryValueExW(hKey, NULL, NULL, &valType, valBuf, &valSize) == ERROR_FILE_NOT_FOUND) {
554 AddEntryToList(hwndLV, NULL, REG_SZ, NULL, 0, !highlightValue);
556 for(index = 0; index < val_count; index++) {
557 BOOL bSelected = (valName == highlightValue); /* NOT a bug, we check for double NULL here */
558 valNameLen = max_val_name_len;
559 valSize = max_val_size;
560 valType = 0;
561 errCode = RegEnumValueW(hKey, index, valName, &valNameLen, NULL, &valType, valBuf, &valSize);
562 if (errCode != ERROR_SUCCESS) goto done;
563 valBuf[valSize] = 0;
564 if (highlightValue && !lstrcmpW(valName, highlightValue))
565 bSelected = TRUE;
566 AddEntryToList(hwndLV, valName[0] ? valName : NULL, valType, valBuf, valSize, bSelected);
568 SendMessageW(hwndLV, LVM_SORTITEMS, (WPARAM)hwndLV, (LPARAM)CompareFunc);
570 g_currentRootKey = hKeyRoot;
571 if (keyPath != g_currentPath) {
572 HeapFree(GetProcessHeap(), 0, g_currentPath);
573 g_currentPath = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(keyPath) + 1) * sizeof(WCHAR));
574 if (!g_currentPath) goto done;
575 lstrcpyW(g_currentPath, keyPath);
578 result = TRUE;
580 done:
581 HeapFree(GetProcessHeap(), 0, valBuf);
582 HeapFree(GetProcessHeap(), 0, valName);
583 SendMessageW(hwndLV, WM_SETREDRAW, TRUE, 0);
584 if (hKey) RegCloseKey(hKey);
586 return result;