Release 3.8.
[wine.git] / programs / regedit / listview.c
blob0934e03a39e7dfc1d032bb351fb7ffc14c7a5ff5
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"
29 #include "regproc.h"
30 #include "wine/heap.h"
31 #include "wine/unicode.h"
33 static INT Image_String;
34 static INT Image_Binary;
36 /*******************************************************************************
37 * Global and Local Variables:
40 DWORD g_columnToSort = ~0U;
41 BOOL g_invertSort = FALSE;
42 WCHAR *g_currentPath;
43 HKEY g_currentRootKey;
44 static WCHAR *g_valueName;
45 static WCHAR g_szValueNotSet[64];
47 #define MAX_LIST_COLUMNS (IDS_LIST_COLUMN_LAST - IDS_LIST_COLUMN_FIRST + 1)
48 static int default_column_widths[MAX_LIST_COLUMNS] = { 200, 175, 400 };
49 static int column_alignment[MAX_LIST_COLUMNS] = { LVCFMT_LEFT, LVCFMT_LEFT, LVCFMT_LEFT };
51 LPWSTR GetItemText(HWND hwndLV, UINT item)
53 WCHAR *curStr;
54 unsigned int maxLen = 128;
55 if (item == 0) return NULL; /* first item is ALWAYS a default */
57 curStr = heap_xalloc(maxLen * sizeof(WCHAR));
58 do {
59 ListView_GetItemTextW(hwndLV, item, 0, curStr, maxLen);
60 if (lstrlenW(curStr) < maxLen - 1) return curStr;
61 maxLen *= 2;
62 curStr = heap_xrealloc(curStr, maxLen * sizeof(WCHAR));
63 } while (TRUE);
64 heap_free(curStr);
65 return NULL;
68 LPCWSTR GetValueName(HWND hwndLV)
70 INT item;
72 if (g_valueName != LPSTR_TEXTCALLBACKW)
73 heap_free(g_valueName);
74 g_valueName = NULL;
76 item = SendMessageW(hwndLV, LVM_GETNEXTITEM, -1, MAKELPARAM(LVNI_FOCUSED, 0));
77 if (item == -1) return NULL;
79 g_valueName = GetItemText(hwndLV, item);
81 return g_valueName;
84 BOOL update_listview_path(const WCHAR *path)
86 heap_free(g_currentPath);
88 g_currentPath = heap_xalloc((lstrlenW(path) + 1) * sizeof(WCHAR));
89 lstrcpyW(g_currentPath, path);
91 return TRUE;
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 void format_value_data(HWND hwndLV, int index, DWORD type, void *data, DWORD size)
114 switch (type)
116 case REG_SZ:
117 case REG_EXPAND_SZ:
118 ListView_SetItemTextW(hwndLV, index, 2, data ? data : g_szValueNotSet);
119 break;
120 case REG_DWORD:
121 case REG_DWORD_BIG_ENDIAN:
123 DWORD value = *(DWORD *)data;
124 WCHAR buf[64];
125 WCHAR format[] = {'0','x','%','0','8','x',' ','(','%','u',')',0};
126 if (type == REG_DWORD_BIG_ENDIAN)
127 value = RtlUlongByteSwap(value);
128 wsprintfW(buf, format, value, value);
129 ListView_SetItemTextW(hwndLV, index, 2, buf);
130 break;
132 case REG_MULTI_SZ:
133 MakeMULTISZDisplayable(data);
134 ListView_SetItemTextW(hwndLV, index, 2, data);
135 break;
136 case REG_BINARY:
137 case REG_NONE:
138 default:
140 unsigned int i;
141 BYTE *pData = data;
142 WCHAR *strBinary = heap_xalloc(size * sizeof(WCHAR) * 3 + sizeof(WCHAR));
143 WCHAR format[] = {'%','0','2','X',' ',0};
144 for (i = 0; i < size; i++)
145 wsprintfW( strBinary + i*3, format, pData[i] );
146 strBinary[size * 3] = 0;
147 ListView_SetItemTextW(hwndLV, index, 2, strBinary);
148 heap_free(strBinary);
149 break;
154 int AddEntryToList(HWND hwndLV, WCHAR *Name, DWORD dwValType, void *ValBuf, DWORD dwCount, int pos)
156 LINE_INFO *linfo;
157 LVITEMW item = { 0 };
158 int index;
160 linfo = heap_xalloc(sizeof(LINE_INFO));
161 linfo->dwValType = dwValType;
162 linfo->val_len = dwCount;
164 if (Name)
166 linfo->name = heap_xalloc((lstrlenW(Name) + 1) * sizeof(WCHAR));
167 lstrcpyW(linfo->name, Name);
169 else linfo->name = NULL;
171 if (ValBuf && dwCount)
173 linfo->val = heap_xalloc(dwCount);
174 memcpy(linfo->val, ValBuf, dwCount);
176 else linfo->val = NULL;
178 item.mask = LVIF_TEXT | LVIF_PARAM | LVIF_STATE | LVIF_IMAGE;
179 item.iItem = (pos == -1) ? SendMessageW(hwndLV, LVM_GETITEMCOUNT, 0, 0) : pos;
180 item.stateMask = LVIS_FOCUSED | LVIS_SELECTED;
181 item.pszText = Name ? Name : LPSTR_TEXTCALLBACKW;
182 item.cchTextMax = Name ? lstrlenW(item.pszText) : 0;
184 switch (dwValType)
186 case REG_SZ:
187 case REG_EXPAND_SZ:
188 case REG_MULTI_SZ:
189 item.iImage = Image_String;
190 break;
191 default:
192 item.iImage = Image_Binary;
193 break;
195 item.lParam = (LPARAM)linfo;
197 if ((index = ListView_InsertItemW(hwndLV, &item)) != -1)
198 format_value_data(hwndLV, index, dwValType, ValBuf, dwCount);
199 return index;
202 static BOOL InitListViewImageList(HWND hWndListView)
204 HIMAGELIST himl;
205 HICON hicon;
206 INT cx = GetSystemMetrics(SM_CXSMICON);
207 INT cy = GetSystemMetrics(SM_CYSMICON);
209 himl = ImageList_Create(cx, cy, ILC_MASK, 0, 2);
210 if (!himl)
211 return FALSE;
213 hicon = LoadImageW(hInst, MAKEINTRESOURCEW(IDI_STRING),
214 IMAGE_ICON, cx, cy, LR_DEFAULTCOLOR);
215 Image_String = ImageList_AddIcon(himl, hicon);
217 hicon = LoadImageW(hInst, MAKEINTRESOURCEW(IDI_BIN),
218 IMAGE_ICON, cx, cy, LR_DEFAULTCOLOR);
219 Image_Binary = ImageList_AddIcon(himl, hicon);
221 SendMessageW( hWndListView, LVM_SETIMAGELIST, LVSIL_SMALL, (LPARAM) himl );
223 /* fail if some of the icons failed to load */
224 if (ImageList_GetImageCount(himl) < 2)
225 return FALSE;
227 return TRUE;
230 static BOOL CreateListColumns(HWND hWndListView)
232 WCHAR szText[50];
233 int index;
234 LVCOLUMNW lvC;
236 /* Create columns. */
237 lvC.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM;
238 lvC.pszText = szText;
240 /* Load the column labels from the resource file. */
241 for (index = 0; index < MAX_LIST_COLUMNS; index++) {
242 lvC.iSubItem = index;
243 lvC.cx = default_column_widths[index];
244 lvC.fmt = column_alignment[index];
245 LoadStringW(hInst, IDS_LIST_COLUMN_FIRST + index, szText, sizeof(szText)/sizeof(WCHAR));
246 if (ListView_InsertColumnW(hWndListView, index, &lvC) == -1) return FALSE;
248 return TRUE;
251 /* OnGetDispInfo - processes the LVN_GETDISPINFO notification message. */
252 void OnGetDispInfo(NMLVDISPINFOW *plvdi)
254 static WCHAR buffer[200];
255 static WCHAR reg_szT[] = {'R','E','G','_','S','Z',0},
256 reg_expand_szT[] = {'R','E','G','_','E','X','P','A','N','D','_','S','Z',0},
257 reg_binaryT[] = {'R','E','G','_','B','I','N','A','R','Y',0},
258 reg_dwordT[] = {'R','E','G','_','D','W','O','R','D',0},
259 reg_dword_big_endianT[] = {'R','E','G','_','D','W','O','R','D','_',
260 'B','I','G','_','E','N','D','I','A','N',0},
261 reg_multi_szT[] = {'R','E','G','_','M','U','L','T','I','_','S','Z',0},
262 reg_linkT[] = {'R','E','G','_','L','I','N','K',0},
263 reg_resource_listT[] = {'R','E','G','_','R','E','S','O','U','R','C','E','_','L','I','S','T',0},
264 reg_noneT[] = {'R','E','G','_','N','O','N','E',0},
265 emptyT[] = {0};
267 plvdi->item.pszText = NULL;
268 plvdi->item.cchTextMax = 0;
270 switch (plvdi->item.iSubItem) {
271 case 0:
272 plvdi->item.pszText = g_pszDefaultValueName;
273 break;
274 case 1:
276 DWORD data_type = ((LINE_INFO *)plvdi->item.lParam)->dwValType;
278 switch (data_type) {
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 fmt[] = {'0','x','%','x',0};
309 wsprintfW(buffer, fmt, data_type);
310 plvdi->item.pszText = buffer;
311 break;
314 break;
316 case 2:
317 plvdi->item.pszText = g_szValueNotSet;
318 break;
319 case 3:
320 plvdi->item.pszText = emptyT;
321 break;
325 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)
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 */
340 return 0;
342 return g_invertSort ? lstrcmpiW(r->name, l->name) : lstrcmpiW(l->name, r->name);
345 HWND StartValueRename(HWND hwndLV)
347 int item;
349 item = SendMessageW(hwndLV, LVM_GETNEXTITEM, -1, MAKELPARAM(LVNI_FOCUSED | LVNI_SELECTED, 0));
350 if (item < 1) { /* cannot rename default key */
351 MessageBeep(MB_ICONHAND);
352 return 0;
354 return (HWND)SendMessageW(hwndLV, LVM_EDITLABELW, item, 0);
357 HWND CreateListView(HWND hwndParent, UINT id)
359 RECT rcClient;
360 HWND hwndLV;
361 WCHAR ListView[] = {'L','i','s','t',' ','V','i','e','w',0};
363 /* prepare strings */
364 LoadStringW(hInst, IDS_REGISTRY_VALUE_NOT_SET, g_szValueNotSet, COUNT_OF(g_szValueNotSet));
366 /* Get the dimensions of the parent window's client area, and create the list view control. */
367 GetClientRect(hwndParent, &rcClient);
368 hwndLV = CreateWindowExW(WS_EX_CLIENTEDGE, WC_LISTVIEWW, ListView,
369 WS_VISIBLE | WS_CHILD | WS_TABSTOP | LVS_REPORT | LVS_EDITLABELS,
370 0, 0, rcClient.right, rcClient.bottom,
371 hwndParent, ULongToHandle(id), hInst, NULL);
372 if (!hwndLV) return NULL;
373 SendMessageW(hwndLV, LVM_SETUNICODEFORMAT, TRUE, 0);
374 SendMessageW(hwndLV, LVM_SETEXTENDEDLISTVIEWSTYLE, 0, LVS_EX_FULLROWSELECT);
376 /* Initialize the image list */
377 if (!InitListViewImageList(hwndLV)) goto fail;
378 if (!CreateListColumns(hwndLV)) goto fail;
379 return hwndLV;
380 fail:
381 DestroyWindow(hwndLV);
382 return NULL;
385 BOOL RefreshListView(HWND hwndLV, HKEY hKeyRoot, LPCWSTR keyPath, LPCWSTR highlightValue)
387 BOOL result = FALSE;
388 DWORD max_sub_key_len;
389 DWORD max_val_name_len, valNameLen;
390 DWORD max_val_size, valSize;
391 DWORD val_count, index, valType;
392 WCHAR* valName = 0;
393 BYTE* valBuf = 0;
394 HKEY hKey = 0;
395 LONG errCode;
396 LVITEMW item;
398 if (!hwndLV) return FALSE;
400 SendMessageW(hwndLV, WM_SETREDRAW, FALSE, 0);
402 errCode = RegOpenKeyExW(hKeyRoot, keyPath, 0, KEY_READ, &hKey);
403 if (errCode != ERROR_SUCCESS) goto done;
405 g_columnToSort = ~0U;
406 SendMessageW(hwndLV, LVM_DELETEALLITEMS, 0, 0);
408 /* get size information and resize the buffers if necessary */
409 errCode = RegQueryInfoKeyW(hKey, NULL, NULL, NULL, NULL, &max_sub_key_len, NULL,
410 &val_count, &max_val_name_len, &max_val_size, NULL, NULL);
411 if (errCode != ERROR_SUCCESS) goto done;
413 /* account for the terminator char */
414 max_val_name_len++;
415 max_val_size++;
417 valName = heap_xalloc(max_val_name_len * sizeof(WCHAR));
418 valBuf = heap_xalloc(max_val_size);
420 valSize = max_val_size;
421 if (RegQueryValueExW(hKey, NULL, NULL, &valType, valBuf, &valSize) == ERROR_FILE_NOT_FOUND) {
422 AddEntryToList(hwndLV, NULL, REG_SZ, NULL, 0, -1);
424 for(index = 0; index < val_count; index++) {
425 valNameLen = max_val_name_len;
426 valSize = max_val_size;
427 valType = 0;
428 errCode = RegEnumValueW(hKey, index, valName, &valNameLen, NULL, &valType, valBuf, &valSize);
429 if (errCode != ERROR_SUCCESS) goto done;
430 valBuf[valSize] = 0;
431 AddEntryToList(hwndLV, valName[0] ? valName : NULL, valType, valBuf, valSize, -1);
434 memset(&item, 0, sizeof(item));
435 if (!highlightValue)
437 item.state = item.stateMask = LVIS_FOCUSED;
438 SendMessageW(hwndLV, LVM_SETITEMSTATE, 0, (LPARAM)&item);
441 SendMessageW(hwndLV, LVM_SORTITEMS, (WPARAM)hwndLV, (LPARAM)CompareFunc);
443 g_currentRootKey = hKeyRoot;
444 if (keyPath != g_currentPath && !update_listview_path(keyPath))
445 goto done;
447 result = TRUE;
449 done:
450 heap_free(valBuf);
451 heap_free(valName);
452 SendMessageW(hwndLV, WM_SETREDRAW, TRUE, 0);
453 if (hKey) RegCloseKey(hKey);
455 return result;