winhlp32: Remove redundant comparison.
[wine.git] / programs / regedit / listview.c
blob4c2ec36bb00d4826bc31b60171bbd0cda62e2173
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/unicode.h"
32 static INT Image_String;
33 static INT Image_Binary;
35 /*******************************************************************************
36 * Global and Local Variables:
39 DWORD g_columnToSort = ~0U;
40 BOOL g_invertSort = FALSE;
41 WCHAR *g_currentPath;
42 HKEY g_currentRootKey;
43 static WCHAR *g_valueName;
44 static WCHAR g_szValueNotSet[64];
46 #define MAX_LIST_COLUMNS (IDS_LIST_COLUMN_LAST - IDS_LIST_COLUMN_FIRST + 1)
47 static int default_column_widths[MAX_LIST_COLUMNS] = { 200, 175, 400 };
48 static int column_alignment[MAX_LIST_COLUMNS] = { LVCFMT_LEFT, LVCFMT_LEFT, LVCFMT_LEFT };
50 LPWSTR GetItemText(HWND hwndLV, UINT item)
52 WCHAR *curStr;
53 unsigned int maxLen = 128;
54 if (item == 0) return NULL; /* first item is ALWAYS a default */
56 curStr = heap_xalloc(maxLen * sizeof(WCHAR));
57 do {
58 ListView_GetItemTextW(hwndLV, item, 0, curStr, maxLen);
59 if (lstrlenW(curStr) < maxLen - 1) return curStr;
60 maxLen *= 2;
61 curStr = heap_xrealloc(curStr, maxLen * sizeof(WCHAR));
62 } while (TRUE);
63 heap_free(curStr);
64 return NULL;
67 LPCWSTR GetValueName(HWND hwndLV)
69 INT item;
71 if (g_valueName != LPSTR_TEXTCALLBACKW)
72 heap_free(g_valueName);
73 g_valueName = NULL;
75 item = SendMessageW(hwndLV, LVM_GETNEXTITEM, -1, MAKELPARAM(LVNI_FOCUSED, 0));
76 if (item == -1) return NULL;
78 g_valueName = GetItemText(hwndLV, item);
80 return g_valueName;
83 BOOL update_listview_path(const WCHAR *path)
85 heap_free(g_currentPath);
87 g_currentPath = heap_xalloc((lstrlenW(path) + 1) * sizeof(WCHAR));
88 lstrcpyW(g_currentPath, path);
90 return TRUE;
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 void format_value_data(HWND hwndLV, int index, DWORD type, void *data, DWORD size)
113 switch (type)
115 case REG_SZ:
116 case REG_EXPAND_SZ:
117 ListView_SetItemTextW(hwndLV, index, 2, data ? data : g_szValueNotSet);
118 break;
119 case REG_DWORD:
120 case REG_DWORD_BIG_ENDIAN:
122 DWORD value = *(DWORD *)data;
123 WCHAR buf[64];
124 WCHAR format[] = {'0','x','%','0','8','x',' ','(','%','u',')',0};
125 if (type == REG_DWORD_BIG_ENDIAN)
126 value = RtlUlongByteSwap(value);
127 wsprintfW(buf, format, value, value);
128 ListView_SetItemTextW(hwndLV, index, 2, buf);
129 break;
131 case REG_MULTI_SZ:
132 MakeMULTISZDisplayable(data);
133 ListView_SetItemTextW(hwndLV, index, 2, data);
134 break;
135 case REG_BINARY:
136 case REG_NONE:
137 default:
139 unsigned int i;
140 BYTE *pData = data;
141 WCHAR *strBinary = heap_xalloc(size * sizeof(WCHAR) * 3 + sizeof(WCHAR));
142 WCHAR format[] = {'%','0','2','X',' ',0};
143 for (i = 0; i < size; i++)
144 wsprintfW( strBinary + i*3, format, pData[i] );
145 strBinary[size * 3] = 0;
146 ListView_SetItemTextW(hwndLV, index, 2, strBinary);
147 heap_free(strBinary);
148 break;
153 int AddEntryToList(HWND hwndLV, WCHAR *Name, DWORD dwValType, void *ValBuf, DWORD dwCount, int pos)
155 LINE_INFO *linfo;
156 LVITEMW item = { 0 };
157 int index;
159 linfo = heap_xalloc(sizeof(LINE_INFO));
160 linfo->dwValType = dwValType;
161 linfo->val_len = dwCount;
163 if (Name)
165 linfo->name = heap_xalloc((lstrlenW(Name) + 1) * sizeof(WCHAR));
166 lstrcpyW(linfo->name, Name);
168 else linfo->name = NULL;
170 if (ValBuf && dwCount)
172 linfo->val = heap_xalloc(dwCount);
173 memcpy(linfo->val, ValBuf, dwCount);
175 else linfo->val = NULL;
177 item.mask = LVIF_TEXT | LVIF_PARAM | LVIF_STATE | LVIF_IMAGE;
178 item.iItem = (pos == -1) ? SendMessageW(hwndLV, LVM_GETITEMCOUNT, 0, 0) : pos;
179 item.stateMask = LVIS_FOCUSED | LVIS_SELECTED;
180 item.pszText = Name ? Name : LPSTR_TEXTCALLBACKW;
181 item.cchTextMax = Name ? lstrlenW(item.pszText) : 0;
183 switch (dwValType)
185 case REG_SZ:
186 case REG_EXPAND_SZ:
187 case REG_MULTI_SZ:
188 item.iImage = Image_String;
189 break;
190 default:
191 item.iImage = Image_Binary;
192 break;
194 item.lParam = (LPARAM)linfo;
196 if ((index = ListView_InsertItemW(hwndLV, &item)) != -1)
197 format_value_data(hwndLV, index, dwValType, ValBuf, dwCount);
198 return index;
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 = LoadImageW(hInst, MAKEINTRESOURCEW(IDI_STRING),
213 IMAGE_ICON, cx, cy, LR_DEFAULTCOLOR);
214 Image_String = ImageList_AddIcon(himl, hicon);
216 hicon = LoadImageW(hInst, MAKEINTRESOURCEW(IDI_BIN),
217 IMAGE_ICON, cx, cy, LR_DEFAULTCOLOR);
218 Image_Binary = ImageList_AddIcon(himl, hicon);
220 SendMessageW( 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 WCHAR szText[50];
232 int index;
233 LVCOLUMNW 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 LoadStringW(hInst, IDS_LIST_COLUMN_FIRST + index, szText, sizeof(szText)/sizeof(WCHAR));
245 if (ListView_InsertColumnW(hWndListView, index, &lvC) == -1) return FALSE;
247 return TRUE;
250 /* OnGetDispInfo - processes the LVN_GETDISPINFO notification message. */
251 void OnGetDispInfo(NMLVDISPINFOW *plvdi)
253 static WCHAR buffer[200];
254 static WCHAR reg_szT[] = {'R','E','G','_','S','Z',0},
255 reg_expand_szT[] = {'R','E','G','_','E','X','P','A','N','D','_','S','Z',0},
256 reg_binaryT[] = {'R','E','G','_','B','I','N','A','R','Y',0},
257 reg_dwordT[] = {'R','E','G','_','D','W','O','R','D',0},
258 reg_dword_big_endianT[] = {'R','E','G','_','D','W','O','R','D','_',
259 'B','I','G','_','E','N','D','I','A','N',0},
260 reg_multi_szT[] = {'R','E','G','_','M','U','L','T','I','_','S','Z',0},
261 reg_linkT[] = {'R','E','G','_','L','I','N','K',0},
262 reg_resource_listT[] = {'R','E','G','_','R','E','S','O','U','R','C','E','_','L','I','S','T',0},
263 reg_noneT[] = {'R','E','G','_','N','O','N','E',0},
264 emptyT[] = {0};
266 plvdi->item.pszText = NULL;
267 plvdi->item.cchTextMax = 0;
269 switch (plvdi->item.iSubItem) {
270 case 0:
271 plvdi->item.pszText = g_pszDefaultValueName;
272 break;
273 case 1:
275 DWORD data_type = ((LINE_INFO *)plvdi->item.lParam)->dwValType;
277 switch (data_type) {
278 case REG_SZ:
279 plvdi->item.pszText = reg_szT;
280 break;
281 case REG_EXPAND_SZ:
282 plvdi->item.pszText = reg_expand_szT;
283 break;
284 case REG_BINARY:
285 plvdi->item.pszText = reg_binaryT;
286 break;
287 case REG_DWORD:
288 plvdi->item.pszText = reg_dwordT;
289 break;
290 case REG_DWORD_BIG_ENDIAN:
291 plvdi->item.pszText = reg_dword_big_endianT;
292 break;
293 case REG_MULTI_SZ:
294 plvdi->item.pszText = reg_multi_szT;
295 break;
296 case REG_LINK:
297 plvdi->item.pszText = reg_linkT;
298 break;
299 case REG_RESOURCE_LIST:
300 plvdi->item.pszText = reg_resource_listT;
301 break;
302 case REG_NONE:
303 plvdi->item.pszText = reg_noneT;
304 break;
305 default:
307 WCHAR fmt[] = {'0','x','%','x',0};
308 wsprintfW(buffer, fmt, data_type);
309 plvdi->item.pszText = buffer;
310 break;
313 break;
315 case 2:
316 plvdi->item.pszText = g_szValueNotSet;
317 break;
318 case 3:
319 plvdi->item.pszText = emptyT;
320 break;
324 int CALLBACK CompareFunc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
326 LINE_INFO*l, *r;
327 l = (LINE_INFO*)lParam1;
328 r = (LINE_INFO*)lParam2;
329 if (!l->name) return -1;
330 if (!r->name) return +1;
332 if (g_columnToSort == ~0U)
333 g_columnToSort = 0;
335 if (g_columnToSort == 1)
336 return g_invertSort ? (int)r->dwValType - (int)l->dwValType : (int)l->dwValType - (int)r->dwValType;
337 if (g_columnToSort == 2) {
338 /* FIXME: Sort on value */
339 return 0;
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 HWND CreateListView(HWND hwndParent, UINT id)
358 RECT rcClient;
359 HWND hwndLV;
360 WCHAR ListView[] = {'L','i','s','t',' ','V','i','e','w',0};
362 /* prepare strings */
363 LoadStringW(hInst, IDS_REGISTRY_VALUE_NOT_SET, g_szValueNotSet, COUNT_OF(g_szValueNotSet));
365 /* Get the dimensions of the parent window's client area, and create the list view control. */
366 GetClientRect(hwndParent, &rcClient);
367 hwndLV = CreateWindowExW(WS_EX_CLIENTEDGE, WC_LISTVIEWW, ListView,
368 WS_VISIBLE | WS_CHILD | WS_TABSTOP | LVS_REPORT | LVS_EDITLABELS,
369 0, 0, rcClient.right, rcClient.bottom,
370 hwndParent, ULongToHandle(id), hInst, NULL);
371 if (!hwndLV) return NULL;
372 SendMessageW(hwndLV, LVM_SETUNICODEFORMAT, TRUE, 0);
373 SendMessageW(hwndLV, LVM_SETEXTENDEDLISTVIEWSTYLE, 0, LVS_EX_FULLROWSELECT);
375 /* Initialize the image list */
376 if (!InitListViewImageList(hwndLV)) goto fail;
377 if (!CreateListColumns(hwndLV)) goto fail;
378 return hwndLV;
379 fail:
380 DestroyWindow(hwndLV);
381 return NULL;
384 BOOL RefreshListView(HWND hwndLV, HKEY hKeyRoot, LPCWSTR keyPath, LPCWSTR highlightValue)
386 BOOL result = FALSE;
387 DWORD max_sub_key_len;
388 DWORD max_val_name_len, valNameLen;
389 DWORD max_val_size, valSize;
390 DWORD val_count, index, valType;
391 WCHAR* valName = 0;
392 BYTE* valBuf = 0;
393 HKEY hKey = 0;
394 LONG errCode;
395 LVITEMW item;
397 if (!hwndLV) return FALSE;
399 SendMessageW(hwndLV, WM_SETREDRAW, FALSE, 0);
401 errCode = RegOpenKeyExW(hKeyRoot, keyPath, 0, KEY_READ, &hKey);
402 if (errCode != ERROR_SUCCESS) goto done;
404 g_columnToSort = ~0U;
405 SendMessageW(hwndLV, LVM_DELETEALLITEMS, 0, 0);
407 /* get size information and resize the buffers if necessary */
408 errCode = RegQueryInfoKeyW(hKey, NULL, NULL, NULL, NULL, &max_sub_key_len, NULL,
409 &val_count, &max_val_name_len, &max_val_size, NULL, NULL);
410 if (errCode != ERROR_SUCCESS) goto done;
412 /* account for the terminator char */
413 max_val_name_len++;
414 max_val_size++;
416 valName = heap_xalloc(max_val_name_len * sizeof(WCHAR));
417 valBuf = heap_xalloc(max_val_size);
419 valSize = max_val_size;
420 if (RegQueryValueExW(hKey, NULL, NULL, &valType, valBuf, &valSize) == ERROR_FILE_NOT_FOUND) {
421 AddEntryToList(hwndLV, NULL, REG_SZ, NULL, 0, -1);
423 for(index = 0; index < val_count; index++) {
424 valNameLen = max_val_name_len;
425 valSize = max_val_size;
426 valType = 0;
427 errCode = RegEnumValueW(hKey, index, valName, &valNameLen, NULL, &valType, valBuf, &valSize);
428 if (errCode != ERROR_SUCCESS) goto done;
429 valBuf[valSize] = 0;
430 AddEntryToList(hwndLV, valName[0] ? valName : NULL, valType, valBuf, valSize, -1);
433 memset(&item, 0, sizeof(item));
434 if (!highlightValue)
436 item.state = item.stateMask = LVIS_FOCUSED;
437 SendMessageW(hwndLV, LVM_SETITEMSTATE, 0, (LPARAM)&item);
440 SendMessageW(hwndLV, LVM_SORTITEMS, (WPARAM)hwndLV, (LPARAM)CompareFunc);
442 g_currentRootKey = hKeyRoot;
443 if (keyPath != g_currentPath && !update_listview_path(keyPath))
444 goto done;
446 result = TRUE;
448 done:
449 heap_free(valBuf);
450 heap_free(valName);
451 SendMessageW(hwndLV, WM_SETREDRAW, TRUE, 0);
452 if (hKey) RegCloseKey(hKey);
454 return result;