ntdll: Move the plaform-independent thread data to the GdiTebBatch TEB field.
[wine.git] / programs / regedit / listview.c
blob72ab464646402190dd7d19d7063b1faa8f9f74d6
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 "wine/unicode.h"
31 static INT Image_String;
32 static INT Image_Binary;
34 /*******************************************************************************
35 * Global and Local Variables:
38 DWORD g_columnToSort = ~0U;
39 BOOL g_invertSort = FALSE;
40 WCHAR *g_currentPath;
41 HKEY g_currentRootKey;
42 static WCHAR *g_valueName;
43 static WCHAR g_szValueNotSet[64];
45 #define MAX_LIST_COLUMNS (IDS_LIST_COLUMN_LAST - IDS_LIST_COLUMN_FIRST + 1)
46 static int default_column_widths[MAX_LIST_COLUMNS] = { 200, 175, 400 };
47 static int column_alignment[MAX_LIST_COLUMNS] = { LVCFMT_LEFT, LVCFMT_LEFT, LVCFMT_LEFT };
49 LPWSTR GetItemText(HWND hwndLV, UINT item)
51 LPWSTR newStr, curStr;
52 unsigned int maxLen = 128;
53 if (item == 0) return NULL; /* first item is ALWAYS a default */
55 curStr = HeapAlloc(GetProcessHeap(), 0, maxLen * sizeof(WCHAR));
56 if (!curStr) return NULL;
57 do {
58 ListView_GetItemTextW(hwndLV, item, 0, curStr, maxLen);
59 if (lstrlenW(curStr) < maxLen - 1) return curStr;
60 maxLen *= 2;
61 newStr = HeapReAlloc(GetProcessHeap(), 0, curStr, maxLen * sizeof(WCHAR));
62 if (!newStr) break;
63 curStr = newStr;
64 } while (TRUE);
65 HeapFree(GetProcessHeap(), 0, curStr);
66 return NULL;
69 LPCWSTR GetValueName(HWND hwndLV)
71 INT item;
73 if (g_valueName != LPSTR_TEXTCALLBACKW)
74 HeapFree(GetProcessHeap(), 0, g_valueName);
75 g_valueName = NULL;
77 item = SendMessageW(hwndLV, LVM_GETNEXTITEM, -1, MAKELPARAM(LVNI_FOCUSED, 0));
78 if (item == -1) return NULL;
80 g_valueName = GetItemText(hwndLV, item);
82 return g_valueName;
85 BOOL update_listview_path(const WCHAR *path)
87 HeapFree(GetProcessHeap(), 0, g_currentPath);
89 g_currentPath = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(path) + 1) * sizeof(WCHAR));
90 if (!g_currentPath) return FALSE;
91 lstrcpyW(g_currentPath, path);
93 return TRUE;
96 /* convert '\0' separated string list into ',' separated string list */
97 static void MakeMULTISZDisplayable(LPWSTR multi)
101 for (; *multi; multi++)
103 if (*(multi+1))
105 *multi = ',';
106 multi++;
108 } while (*multi);
111 /*******************************************************************************
112 * Local module support methods
114 void format_value_data(HWND hwndLV, int index, DWORD type, void *data, DWORD size)
116 switch (type)
118 case REG_SZ:
119 case REG_EXPAND_SZ:
120 ListView_SetItemTextW(hwndLV, index, 2, data ? data : g_szValueNotSet);
121 break;
122 case REG_DWORD:
123 case REG_DWORD_BIG_ENDIAN:
125 DWORD value = *(DWORD *)data;
126 WCHAR buf[64];
127 WCHAR format[] = {'0','x','%','0','8','x',' ','(','%','u',')',0};
128 if (type == REG_DWORD_BIG_ENDIAN)
129 value = RtlUlongByteSwap(value);
130 wsprintfW(buf, format, value, value);
131 ListView_SetItemTextW(hwndLV, index, 2, buf);
132 break;
134 case REG_MULTI_SZ:
135 MakeMULTISZDisplayable(data);
136 ListView_SetItemTextW(hwndLV, index, 2, data);
137 break;
138 case REG_BINARY:
139 case REG_NONE:
140 default:
142 unsigned int i;
143 BYTE *pData = data;
144 WCHAR *strBinary = HeapAlloc(GetProcessHeap(), 0, size * sizeof(WCHAR) * 3 + sizeof(WCHAR));
145 WCHAR format[] = {'%','0','2','X',' ',0};
146 for (i = 0; i < size; i++)
147 wsprintfW( strBinary + i*3, format, pData[i] );
148 strBinary[size * 3] = 0;
149 ListView_SetItemTextW(hwndLV, index, 2, strBinary);
150 HeapFree(GetProcessHeap(), 0, strBinary);
151 break;
156 int AddEntryToList(HWND hwndLV, WCHAR *Name, DWORD dwValType, void *ValBuf, DWORD dwCount, int pos)
158 LVITEMW item = { 0 };
159 LINE_INFO* linfo;
160 int index;
162 linfo = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(LINE_INFO) + dwCount);
163 linfo->dwValType = dwValType;
164 linfo->val_len = dwCount;
165 CopyMemory(&linfo[1], ValBuf, dwCount);
167 if (Name)
169 linfo->name = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(Name) + 1) * sizeof(WCHAR));
170 lstrcpyW(linfo->name, Name);
171 } else
173 linfo->name = NULL;
176 item.mask = LVIF_TEXT | LVIF_PARAM | LVIF_STATE | LVIF_IMAGE;
177 item.iItem = (pos == -1) ? SendMessageW(hwndLV, LVM_GETITEMCOUNT, 0, 0) : pos;
178 item.stateMask = LVIS_FOCUSED | LVIS_SELECTED;
179 item.pszText = Name ? Name : LPSTR_TEXTCALLBACKW;
180 item.cchTextMax = Name ? lstrlenW(item.pszText) : 0;
182 switch (dwValType)
184 case REG_SZ:
185 case REG_EXPAND_SZ:
186 case REG_MULTI_SZ:
187 item.iImage = Image_String;
188 break;
189 default:
190 item.iImage = Image_Binary;
191 break;
193 item.lParam = (LPARAM)linfo;
195 if ((index = ListView_InsertItemW(hwndLV, &item)) != -1)
196 format_value_data(hwndLV, index, dwValType, ValBuf, dwCount);
197 return index;
200 static BOOL InitListViewImageList(HWND hWndListView)
202 HIMAGELIST himl;
203 HICON hicon;
204 INT cx = GetSystemMetrics(SM_CXSMICON);
205 INT cy = GetSystemMetrics(SM_CYSMICON);
207 himl = ImageList_Create(cx, cy, ILC_MASK, 0, 2);
208 if (!himl)
209 return FALSE;
211 hicon = LoadImageW(hInst, MAKEINTRESOURCEW(IDI_STRING),
212 IMAGE_ICON, cx, cy, LR_DEFAULTCOLOR);
213 Image_String = ImageList_AddIcon(himl, hicon);
215 hicon = LoadImageW(hInst, MAKEINTRESOURCEW(IDI_BIN),
216 IMAGE_ICON, cx, cy, LR_DEFAULTCOLOR);
217 Image_Binary = ImageList_AddIcon(himl, hicon);
219 SendMessageW( hWndListView, LVM_SETIMAGELIST, LVSIL_SMALL, (LPARAM) himl );
221 /* fail if some of the icons failed to load */
222 if (ImageList_GetImageCount(himl) < 2)
223 return FALSE;
225 return TRUE;
228 static BOOL CreateListColumns(HWND hWndListView)
230 WCHAR szText[50];
231 int index;
232 LVCOLUMNW lvC;
234 /* Create columns. */
235 lvC.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM;
236 lvC.pszText = szText;
238 /* Load the column labels from the resource file. */
239 for (index = 0; index < MAX_LIST_COLUMNS; index++) {
240 lvC.iSubItem = index;
241 lvC.cx = default_column_widths[index];
242 lvC.fmt = column_alignment[index];
243 LoadStringW(hInst, IDS_LIST_COLUMN_FIRST + index, szText, sizeof(szText)/sizeof(WCHAR));
244 if (ListView_InsertColumnW(hWndListView, index, &lvC) == -1) return FALSE;
246 return TRUE;
249 /* OnGetDispInfo - processes the LVN_GETDISPINFO notification message. */
250 void OnGetDispInfo(NMLVDISPINFOW *plvdi)
252 static WCHAR buffer[200];
253 static WCHAR reg_szT[] = {'R','E','G','_','S','Z',0},
254 reg_expand_szT[] = {'R','E','G','_','E','X','P','A','N','D','_','S','Z',0},
255 reg_binaryT[] = {'R','E','G','_','B','I','N','A','R','Y',0},
256 reg_dwordT[] = {'R','E','G','_','D','W','O','R','D',0},
257 reg_dword_big_endianT[] = {'R','E','G','_','D','W','O','R','D','_',
258 'B','I','G','_','E','N','D','I','A','N',0},
259 reg_multi_szT[] = {'R','E','G','_','M','U','L','T','I','_','S','Z',0},
260 reg_linkT[] = {'R','E','G','_','L','I','N','K',0},
261 reg_resource_listT[] = {'R','E','G','_','R','E','S','O','U','R','C','E','_','L','I','S','T',0},
262 reg_noneT[] = {'R','E','G','_','N','O','N','E',0},
263 emptyT[] = {0};
265 plvdi->item.pszText = NULL;
266 plvdi->item.cchTextMax = 0;
268 switch (plvdi->item.iSubItem) {
269 case 0:
270 plvdi->item.pszText = g_pszDefaultValueName;
271 break;
272 case 1:
274 DWORD data_type = ((LINE_INFO *)plvdi->item.lParam)->dwValType;
276 switch (data_type) {
277 case REG_SZ:
278 plvdi->item.pszText = reg_szT;
279 break;
280 case REG_EXPAND_SZ:
281 plvdi->item.pszText = reg_expand_szT;
282 break;
283 case REG_BINARY:
284 plvdi->item.pszText = reg_binaryT;
285 break;
286 case REG_DWORD:
287 plvdi->item.pszText = reg_dwordT;
288 break;
289 case REG_DWORD_BIG_ENDIAN:
290 plvdi->item.pszText = reg_dword_big_endianT;
291 break;
292 case REG_MULTI_SZ:
293 plvdi->item.pszText = reg_multi_szT;
294 break;
295 case REG_LINK:
296 plvdi->item.pszText = reg_linkT;
297 break;
298 case REG_RESOURCE_LIST:
299 plvdi->item.pszText = reg_resource_listT;
300 break;
301 case REG_NONE:
302 plvdi->item.pszText = reg_noneT;
303 break;
304 default:
306 WCHAR fmt[] = {'0','x','%','x',0};
307 wsprintfW(buffer, fmt, data_type);
308 plvdi->item.pszText = buffer;
309 break;
312 break;
314 case 2:
315 plvdi->item.pszText = g_szValueNotSet;
316 break;
317 case 3:
318 plvdi->item.pszText = emptyT;
319 break;
323 int CALLBACK CompareFunc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
325 LINE_INFO*l, *r;
326 l = (LINE_INFO*)lParam1;
327 r = (LINE_INFO*)lParam2;
328 if (!l->name) return -1;
329 if (!r->name) return +1;
331 if (g_columnToSort == ~0U)
332 g_columnToSort = 0;
334 if (g_columnToSort == 1)
335 return g_invertSort ? (int)r->dwValType - (int)l->dwValType : (int)l->dwValType - (int)r->dwValType;
336 if (g_columnToSort == 2) {
337 /* FIXME: Sort on value */
338 return 0;
340 return g_invertSort ? lstrcmpiW(r->name, l->name) : lstrcmpiW(l->name, r->name);
343 HWND StartValueRename(HWND hwndLV)
345 int item;
347 item = SendMessageW(hwndLV, LVM_GETNEXTITEM, -1, MAKELPARAM(LVNI_FOCUSED | LVNI_SELECTED, 0));
348 if (item < 1) { /* cannot rename default key */
349 MessageBeep(MB_ICONHAND);
350 return 0;
352 return (HWND)SendMessageW(hwndLV, LVM_EDITLABELW, item, 0);
355 HWND CreateListView(HWND hwndParent, UINT id)
357 RECT rcClient;
358 HWND hwndLV;
359 WCHAR ListView[] = {'L','i','s','t',' ','V','i','e','w',0};
361 /* prepare strings */
362 LoadStringW(hInst, IDS_REGISTRY_VALUE_NOT_SET, g_szValueNotSet, COUNT_OF(g_szValueNotSet));
364 /* Get the dimensions of the parent window's client area, and create the list view control. */
365 GetClientRect(hwndParent, &rcClient);
366 hwndLV = CreateWindowExW(WS_EX_CLIENTEDGE, WC_LISTVIEWW, ListView,
367 WS_VISIBLE | WS_CHILD | WS_TABSTOP | LVS_REPORT | LVS_EDITLABELS,
368 0, 0, rcClient.right, rcClient.bottom,
369 hwndParent, ULongToHandle(id), hInst, NULL);
370 if (!hwndLV) return NULL;
371 SendMessageW(hwndLV, LVM_SETUNICODEFORMAT, TRUE, 0);
372 SendMessageW(hwndLV, LVM_SETEXTENDEDLISTVIEWSTYLE, 0, LVS_EX_FULLROWSELECT);
374 /* Initialize the image list */
375 if (!InitListViewImageList(hwndLV)) goto fail;
376 if (!CreateListColumns(hwndLV)) goto fail;
377 return hwndLV;
378 fail:
379 DestroyWindow(hwndLV);
380 return NULL;
383 BOOL RefreshListView(HWND hwndLV, HKEY hKeyRoot, LPCWSTR keyPath, LPCWSTR highlightValue)
385 BOOL result = FALSE;
386 DWORD max_sub_key_len;
387 DWORD max_val_name_len, valNameLen;
388 DWORD max_val_size, valSize;
389 DWORD val_count, index, valType;
390 WCHAR* valName = 0;
391 BYTE* valBuf = 0;
392 HKEY hKey = 0;
393 LONG errCode;
394 LVITEMW item;
396 if (!hwndLV) return FALSE;
398 SendMessageW(hwndLV, WM_SETREDRAW, FALSE, 0);
400 errCode = RegOpenKeyExW(hKeyRoot, keyPath, 0, KEY_READ, &hKey);
401 if (errCode != ERROR_SUCCESS) goto done;
403 g_columnToSort = ~0U;
404 SendMessageW(hwndLV, LVM_DELETEALLITEMS, 0, 0);
406 /* get size information and resize the buffers if necessary */
407 errCode = RegQueryInfoKeyW(hKey, NULL, NULL, NULL, NULL, &max_sub_key_len, NULL,
408 &val_count, &max_val_name_len, &max_val_size, NULL, NULL);
409 if (errCode != ERROR_SUCCESS) goto done;
411 /* account for the terminator char */
412 max_val_name_len++;
413 max_val_size++;
415 valName = HeapAlloc(GetProcessHeap(), 0, max_val_name_len * sizeof(WCHAR));
416 if (!valName)
417 goto done;
418 valBuf = HeapAlloc(GetProcessHeap(), 0, max_val_size);
419 if (!valBuf)
420 goto done;
422 valSize = max_val_size;
423 if (RegQueryValueExW(hKey, NULL, NULL, &valType, valBuf, &valSize) == ERROR_FILE_NOT_FOUND) {
424 AddEntryToList(hwndLV, NULL, REG_SZ, NULL, 0, -1);
426 for(index = 0; index < val_count; index++) {
427 valNameLen = max_val_name_len;
428 valSize = max_val_size;
429 valType = 0;
430 errCode = RegEnumValueW(hKey, index, valName, &valNameLen, NULL, &valType, valBuf, &valSize);
431 if (errCode != ERROR_SUCCESS) goto done;
432 valBuf[valSize] = 0;
433 AddEntryToList(hwndLV, valName[0] ? valName : NULL, valType, valBuf, valSize, -1);
436 memset(&item, 0, sizeof(item));
437 if (!highlightValue)
439 item.state = item.stateMask = LVIS_FOCUSED;
440 SendMessageW(hwndLV, LVM_SETITEMSTATE, 0, (LPARAM)&item);
443 SendMessageW(hwndLV, LVM_SORTITEMS, (WPARAM)hwndLV, (LPARAM)CompareFunc);
445 g_currentRootKey = hKeyRoot;
446 if (keyPath != g_currentPath && !update_listview_path(keyPath))
447 goto done;
449 result = TRUE;
451 done:
452 HeapFree(GetProcessHeap(), 0, valBuf);
453 HeapFree(GetProcessHeap(), 0, valName);
454 SendMessageW(hwndLV, WM_SETREDRAW, TRUE, 0);
455 if (hKey) RegCloseKey(hKey);
457 return result;