gphoto2.ds: Use standard dlopen() instead of the libwine wrappers.
[wine/zf.git] / programs / regedit / listview.c
blob0132cfb8db8163b7bd5ad17b735b9f7c0eaf7589
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>
27 #include "main.h"
28 #include "wine/heap.h"
30 static INT Image_String;
31 static INT Image_Binary;
33 /*******************************************************************************
34 * Global and Local Variables:
37 DWORD g_columnToSort = ~0U;
38 BOOL g_invertSort = FALSE;
39 WCHAR *g_currentPath;
40 HKEY g_currentRootKey;
41 static WCHAR g_szValueNotSet[64];
43 #define MAX_LIST_COLUMNS (IDS_LIST_COLUMN_LAST - IDS_LIST_COLUMN_FIRST + 1)
44 static int default_column_widths[MAX_LIST_COLUMNS] = { 200, 175, 400 };
45 static int column_alignment[MAX_LIST_COLUMNS] = { LVCFMT_LEFT, LVCFMT_LEFT, LVCFMT_LEFT };
47 LPWSTR GetItemText(HWND hwndLV, UINT item)
49 WCHAR *curStr;
50 unsigned int maxLen = 128;
51 if (item == 0) return NULL; /* first item is ALWAYS a default */
53 curStr = heap_xalloc(maxLen * sizeof(WCHAR));
54 do {
55 ListView_GetItemTextW(hwndLV, item, 0, curStr, maxLen);
56 if (lstrlenW(curStr) < maxLen - 1) return curStr;
57 maxLen *= 2;
58 curStr = heap_xrealloc(curStr, maxLen * sizeof(WCHAR));
59 } while (TRUE);
60 heap_free(curStr);
61 return NULL;
64 WCHAR *GetValueName(HWND hwndLV)
66 INT item;
68 item = SendMessageW(hwndLV, LVM_GETNEXTITEM, -1, MAKELPARAM(LVNI_FOCUSED, 0));
69 if (item == -1) return NULL;
71 return GetItemText(hwndLV, item);
74 BOOL update_listview_path(const WCHAR *path)
76 heap_free(g_currentPath);
78 g_currentPath = heap_xalloc((lstrlenW(path) + 1) * sizeof(WCHAR));
79 lstrcpyW(g_currentPath, path);
81 return TRUE;
84 /* convert '\0' separated string list into ',' separated string list */
85 static void MakeMULTISZDisplayable(LPWSTR multi)
89 for (; *multi; multi++)
91 if (*(multi+1))
93 *multi = ',';
94 multi++;
96 } while (*multi);
99 /*******************************************************************************
100 * Local module support methods
102 void format_value_data(HWND hwndLV, int index, DWORD type, void *data, DWORD size)
104 switch (type)
106 case REG_SZ:
107 case REG_EXPAND_SZ:
108 ListView_SetItemTextW(hwndLV, index, 2, data ? data : g_szValueNotSet);
109 break;
110 case REG_DWORD:
111 case REG_DWORD_BIG_ENDIAN:
113 DWORD value = *(DWORD *)data;
114 WCHAR buf[64];
115 WCHAR format[] = {'0','x','%','0','8','x',' ','(','%','u',')',0};
116 if (type == REG_DWORD_BIG_ENDIAN)
117 value = RtlUlongByteSwap(value);
118 wsprintfW(buf, format, value, value);
119 ListView_SetItemTextW(hwndLV, index, 2, buf);
120 break;
122 case REG_MULTI_SZ:
123 MakeMULTISZDisplayable(data);
124 ListView_SetItemTextW(hwndLV, index, 2, data);
125 break;
126 case REG_BINARY:
127 case REG_NONE:
128 default:
130 unsigned int i;
131 BYTE *pData = data;
132 WCHAR *strBinary = heap_xalloc(size * sizeof(WCHAR) * 3 + sizeof(WCHAR));
133 WCHAR format[] = {'%','0','2','X',' ',0};
134 for (i = 0; i < size; i++)
135 wsprintfW( strBinary + i*3, format, pData[i] );
136 strBinary[size * 3] = 0;
137 ListView_SetItemTextW(hwndLV, index, 2, strBinary);
138 heap_free(strBinary);
139 break;
144 int AddEntryToList(HWND hwndLV, WCHAR *Name, DWORD dwValType, void *ValBuf, DWORD dwCount, int pos)
146 LINE_INFO *linfo;
147 LVITEMW item = { 0 };
148 int index;
150 linfo = heap_xalloc(sizeof(LINE_INFO));
151 linfo->dwValType = dwValType;
152 linfo->val_len = dwCount;
154 if (Name)
156 linfo->name = heap_xalloc((lstrlenW(Name) + 1) * sizeof(WCHAR));
157 lstrcpyW(linfo->name, Name);
159 else linfo->name = NULL;
161 if (ValBuf && dwCount)
163 linfo->val = heap_xalloc(dwCount);
164 memcpy(linfo->val, ValBuf, dwCount);
166 else linfo->val = NULL;
168 item.mask = LVIF_TEXT | LVIF_PARAM | LVIF_STATE | LVIF_IMAGE;
169 item.iItem = (pos == -1) ? SendMessageW(hwndLV, LVM_GETITEMCOUNT, 0, 0) : pos;
170 item.stateMask = LVIS_FOCUSED | LVIS_SELECTED;
171 item.pszText = Name ? Name : LPSTR_TEXTCALLBACKW;
172 item.cchTextMax = Name ? lstrlenW(item.pszText) : 0;
174 switch (dwValType)
176 case REG_SZ:
177 case REG_EXPAND_SZ:
178 case REG_MULTI_SZ:
179 item.iImage = Image_String;
180 break;
181 default:
182 item.iImage = Image_Binary;
183 break;
185 item.lParam = (LPARAM)linfo;
187 if ((index = ListView_InsertItemW(hwndLV, &item)) != -1)
188 format_value_data(hwndLV, index, dwValType, ValBuf, dwCount);
189 return index;
192 static BOOL InitListViewImageList(HWND hWndListView)
194 HIMAGELIST himl;
195 HICON hicon;
196 INT cx = GetSystemMetrics(SM_CXSMICON);
197 INT cy = GetSystemMetrics(SM_CYSMICON);
199 himl = ImageList_Create(cx, cy, ILC_MASK, 0, 2);
200 if (!himl)
201 return FALSE;
203 hicon = LoadImageW(hInst, MAKEINTRESOURCEW(IDI_STRING),
204 IMAGE_ICON, cx, cy, LR_DEFAULTCOLOR);
205 Image_String = ImageList_AddIcon(himl, hicon);
207 hicon = LoadImageW(hInst, MAKEINTRESOURCEW(IDI_BIN),
208 IMAGE_ICON, cx, cy, LR_DEFAULTCOLOR);
209 Image_Binary = ImageList_AddIcon(himl, hicon);
211 SendMessageW( hWndListView, LVM_SETIMAGELIST, LVSIL_SMALL, (LPARAM) himl );
213 /* fail if some of the icons failed to load */
214 if (ImageList_GetImageCount(himl) < 2)
215 return FALSE;
217 return TRUE;
220 static BOOL CreateListColumns(HWND hWndListView)
222 WCHAR szText[50];
223 int index;
224 LVCOLUMNW lvC;
226 /* Create columns. */
227 lvC.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM;
228 lvC.pszText = szText;
230 /* Load the column labels from the resource file. */
231 for (index = 0; index < MAX_LIST_COLUMNS; index++) {
232 lvC.iSubItem = index;
233 lvC.cx = default_column_widths[index];
234 lvC.fmt = column_alignment[index];
235 LoadStringW(hInst, IDS_LIST_COLUMN_FIRST + index, szText, ARRAY_SIZE(szText));
236 if (ListView_InsertColumnW(hWndListView, index, &lvC) == -1) return FALSE;
238 return TRUE;
241 /* OnGetDispInfo - processes the LVN_GETDISPINFO notification message. */
242 void OnGetDispInfo(NMLVDISPINFOW *plvdi)
244 static WCHAR buffer[200];
245 static WCHAR reg_szT[] = {'R','E','G','_','S','Z',0},
246 reg_expand_szT[] = {'R','E','G','_','E','X','P','A','N','D','_','S','Z',0},
247 reg_binaryT[] = {'R','E','G','_','B','I','N','A','R','Y',0},
248 reg_dwordT[] = {'R','E','G','_','D','W','O','R','D',0},
249 reg_dword_big_endianT[] = {'R','E','G','_','D','W','O','R','D','_',
250 'B','I','G','_','E','N','D','I','A','N',0},
251 reg_multi_szT[] = {'R','E','G','_','M','U','L','T','I','_','S','Z',0},
252 reg_linkT[] = {'R','E','G','_','L','I','N','K',0},
253 reg_resource_listT[] = {'R','E','G','_','R','E','S','O','U','R','C','E','_','L','I','S','T',0},
254 reg_noneT[] = {'R','E','G','_','N','O','N','E',0},
255 emptyT[] = {0};
257 plvdi->item.pszText = NULL;
258 plvdi->item.cchTextMax = 0;
260 switch (plvdi->item.iSubItem) {
261 case 0:
262 plvdi->item.pszText = g_pszDefaultValueName;
263 break;
264 case 1:
266 DWORD data_type = ((LINE_INFO *)plvdi->item.lParam)->dwValType;
268 switch (data_type) {
269 case REG_SZ:
270 plvdi->item.pszText = reg_szT;
271 break;
272 case REG_EXPAND_SZ:
273 plvdi->item.pszText = reg_expand_szT;
274 break;
275 case REG_BINARY:
276 plvdi->item.pszText = reg_binaryT;
277 break;
278 case REG_DWORD:
279 plvdi->item.pszText = reg_dwordT;
280 break;
281 case REG_DWORD_BIG_ENDIAN:
282 plvdi->item.pszText = reg_dword_big_endianT;
283 break;
284 case REG_MULTI_SZ:
285 plvdi->item.pszText = reg_multi_szT;
286 break;
287 case REG_LINK:
288 plvdi->item.pszText = reg_linkT;
289 break;
290 case REG_RESOURCE_LIST:
291 plvdi->item.pszText = reg_resource_listT;
292 break;
293 case REG_NONE:
294 plvdi->item.pszText = reg_noneT;
295 break;
296 default:
298 WCHAR fmt[] = {'0','x','%','x',0};
299 wsprintfW(buffer, fmt, data_type);
300 plvdi->item.pszText = buffer;
301 break;
304 break;
306 case 2:
307 plvdi->item.pszText = g_szValueNotSet;
308 break;
309 case 3:
310 plvdi->item.pszText = emptyT;
311 break;
315 int CALLBACK CompareFunc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
317 LINE_INFO*l, *r;
318 l = (LINE_INFO*)lParam1;
319 r = (LINE_INFO*)lParam2;
320 if (!l->name) return -1;
321 if (!r->name) return +1;
323 if (g_columnToSort == ~0U)
324 g_columnToSort = 0;
326 if (g_columnToSort == 1)
327 return g_invertSort ? (int)r->dwValType - (int)l->dwValType : (int)l->dwValType - (int)r->dwValType;
328 if (g_columnToSort == 2) {
329 /* FIXME: Sort on value */
330 return 0;
332 return g_invertSort ? lstrcmpiW(r->name, l->name) : lstrcmpiW(l->name, r->name);
335 HWND StartValueRename(HWND hwndLV)
337 int item;
339 item = SendMessageW(hwndLV, LVM_GETNEXTITEM, -1, MAKELPARAM(LVNI_FOCUSED | LVNI_SELECTED, 0));
340 if (item < 1) { /* cannot rename default key */
341 MessageBeep(MB_ICONHAND);
342 return 0;
344 return (HWND)SendMessageW(hwndLV, LVM_EDITLABELW, item, 0);
347 HWND CreateListView(HWND hwndParent, UINT id)
349 RECT rcClient;
350 HWND hwndLV;
351 WCHAR ListView[] = {'L','i','s','t',' ','V','i','e','w',0};
353 /* prepare strings */
354 LoadStringW(hInst, IDS_REGISTRY_VALUE_NOT_SET, g_szValueNotSet, ARRAY_SIZE(g_szValueNotSet));
356 /* Get the dimensions of the parent window's client area, and create the list view control. */
357 GetClientRect(hwndParent, &rcClient);
358 hwndLV = CreateWindowExW(WS_EX_CLIENTEDGE, WC_LISTVIEWW, ListView,
359 WS_VISIBLE | WS_CHILD | WS_TABSTOP | LVS_REPORT | LVS_EDITLABELS,
360 0, 0, rcClient.right, rcClient.bottom,
361 hwndParent, ULongToHandle(id), hInst, NULL);
362 if (!hwndLV) return NULL;
363 SendMessageW(hwndLV, LVM_SETUNICODEFORMAT, TRUE, 0);
364 SendMessageW(hwndLV, LVM_SETEXTENDEDLISTVIEWSTYLE, 0, LVS_EX_FULLROWSELECT);
366 /* Initialize the image list */
367 if (!InitListViewImageList(hwndLV)) goto fail;
368 if (!CreateListColumns(hwndLV)) goto fail;
369 return hwndLV;
370 fail:
371 DestroyWindow(hwndLV);
372 return NULL;
375 BOOL RefreshListView(HWND hwndLV, HKEY hKeyRoot, LPCWSTR keyPath, LPCWSTR highlightValue)
377 BOOL result = FALSE;
378 DWORD max_sub_key_len;
379 DWORD max_val_name_len, valNameLen;
380 DWORD max_val_size, valSize;
381 DWORD val_count, index, valType;
382 WCHAR* valName = 0;
383 BYTE* valBuf = 0;
384 HKEY hKey = 0;
385 LONG errCode;
386 LVITEMW item;
388 if (!hwndLV) return FALSE;
390 SendMessageW(hwndLV, WM_SETREDRAW, FALSE, 0);
392 errCode = RegOpenKeyExW(hKeyRoot, keyPath, 0, KEY_READ, &hKey);
393 if (errCode != ERROR_SUCCESS) goto done;
395 g_columnToSort = ~0U;
396 SendMessageW(hwndLV, LVM_DELETEALLITEMS, 0, 0);
398 /* get size information and resize the buffers if necessary */
399 errCode = RegQueryInfoKeyW(hKey, NULL, NULL, NULL, NULL, &max_sub_key_len, NULL,
400 &val_count, &max_val_name_len, &max_val_size, NULL, NULL);
401 if (errCode != ERROR_SUCCESS) goto done;
403 /* account for the terminator char */
404 max_val_name_len++;
405 max_val_size++;
407 valName = heap_xalloc(max_val_name_len * sizeof(WCHAR));
408 valBuf = heap_xalloc(max_val_size);
410 valSize = max_val_size;
411 if (RegQueryValueExW(hKey, NULL, NULL, &valType, valBuf, &valSize) == ERROR_FILE_NOT_FOUND) {
412 AddEntryToList(hwndLV, NULL, REG_SZ, NULL, 0, -1);
414 for(index = 0; index < val_count; index++) {
415 valNameLen = max_val_name_len;
416 valSize = max_val_size;
417 valType = 0;
418 errCode = RegEnumValueW(hKey, index, valName, &valNameLen, NULL, &valType, valBuf, &valSize);
419 if (errCode != ERROR_SUCCESS) goto done;
420 valBuf[valSize] = 0;
421 AddEntryToList(hwndLV, valName[0] ? valName : NULL, valType, valBuf, valSize, -1);
424 memset(&item, 0, sizeof(item));
425 if (!highlightValue)
427 item.state = item.stateMask = LVIS_FOCUSED;
428 SendMessageW(hwndLV, LVM_SETITEMSTATE, 0, (LPARAM)&item);
431 SendMessageW(hwndLV, LVM_SORTITEMS, (WPARAM)hwndLV, (LPARAM)CompareFunc);
433 g_currentRootKey = hKeyRoot;
434 if (keyPath != g_currentPath && !update_listview_path(keyPath))
435 goto done;
437 result = TRUE;
439 done:
440 heap_free(valBuf);
441 heap_free(valName);
442 SendMessageW(hwndLV, WM_SETREDRAW, TRUE, 0);
443 if (hKey) RegCloseKey(hKey);
445 return result;