more improvements
[reactos.git] / reactos / subsys / system / regedit / listview.c
1 /*
2 * Regedit listviews
3 *
4 * Copyright (C) 2002 Robert Dickenson <robd@reactos.org>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21 #include <windows.h>
22 #include <windowsx.h>
23 #include <commctrl.h>
24 #include <stdlib.h>
25 #include <tchar.h>
26 #include <process.h>
27 #include <stdio.h>
28
29 #include "main.h"
30
31 #define CX_ICON 16
32 #define CY_ICON 16
33 #define NUM_ICONS 2
34
35 int Image_String = 0;
36 int Image_Bin = 0;
37
38 typedef struct tagLINE_INFO
39 {
40 DWORD dwValType;
41 LPTSTR name;
42 void* val;
43 size_t val_len;
44 } LINE_INFO, *PLINE_INFO;
45
46 /*******************************************************************************
47 * Global and Local Variables:
48 */
49
50 static DWORD g_columnToSort = ~0UL;
51 static BOOL g_invertSort = FALSE;
52 static LPTSTR g_valueName;
53
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 };
57
58 LPCTSTR GetValueName(HWND hwndLV)
59 {
60 int item, len, maxLen;
61 LPTSTR newStr;
62 LVITEM LVItem;
63 PLINE_INFO lineinfo;
64
65 if (!g_valueName) g_valueName = HeapAlloc(GetProcessHeap(), 0, 1024);
66 if (!g_valueName) return NULL;
67 *g_valueName = 0;
68 maxLen = HeapSize(GetProcessHeap(), 0, g_valueName);
69 if (maxLen == (SIZE_T) - 1) return NULL;
70
71 item = ListView_GetNextItem(hwndLV, -1, LVNI_FOCUSED | LVNI_SELECTED);
72 if (item == -1) return NULL;
73 LVItem.mask = LVIF_PARAM;
74 LVItem.iItem = item;
75 for(;;)
76 {
77 if(ListView_GetItem(hwndLV, &LVItem))
78 {
79 lineinfo = (PLINE_INFO)LVItem.lParam;
80 if(!lineinfo->name)
81 {
82 *g_valueName = 0;
83 return g_valueName;
84 }
85 len = _tcslen(lineinfo->name);
86 if (len < maxLen - 1) break;
87 newStr = HeapReAlloc(GetProcessHeap(), 0, g_valueName, maxLen * 2);
88 if (!newStr) return NULL;
89 g_valueName = newStr;
90 maxLen *= 2;
91 }
92 else
93 return NULL;
94 }
95 memcpy(g_valueName, lineinfo->name, sizeof(TCHAR) * (len + 1));
96 return g_valueName;
97 }
98
99 BOOL IsDefaultValue(HWND hwndLV, int i)
100 {
101 PLINE_INFO lineinfo;
102 LVITEM Item;
103
104 Item.mask = LVIF_PARAM;
105 Item.iItem = i;
106 if(ListView_GetItem(hwndLV, &Item))
107 {
108 lineinfo = (PLINE_INFO)Item.lParam;
109 return lineinfo && (!lineinfo->name || !strcmp(lineinfo->name, _T("")));
110 }
111 return FALSE;
112 }
113
114 /*******************************************************************************
115 * Local module support methods
116 */
117 static void AddEntryToList(HWND hwndLV, LPTSTR Name, DWORD dwValType, void* ValBuf, DWORD dwCount, int Position)
118 {
119 LINE_INFO* linfo;
120 LVITEM item;
121 int index;
122
123 linfo = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(LINE_INFO) + dwCount);
124 linfo->dwValType = dwValType;
125 linfo->val_len = dwCount;
126 if(dwCount > 0)
127 {
128 memcpy(&linfo[1], ValBuf, dwCount);
129 }
130 linfo->name = _tcsdup(Name);
131
132 item.mask = LVIF_TEXT | LVIF_PARAM | LVIF_IMAGE;
133 item.iItem = (Position == -1 ? 0: Position);
134 item.iSubItem = 0;
135 item.state = 0;
136 item.stateMask = 0;
137 item.pszText = Name;
138 item.cchTextMax = _tcslen(item.pszText);
139 if (item.cchTextMax == 0)
140 item.pszText = LPSTR_TEXTCALLBACK;
141 item.iImage = 0;
142 item.lParam = (LPARAM)linfo;
143 switch(dwValType)
144 {
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_Bin;
152 break;
153 }
154
155 /* item.lParam = (LPARAM)ValBuf; */
156 #if (_WIN32_IE >= 0x0300)
157 item.iIndent = 0;
158 #endif
159
160 index = ListView_InsertItem(hwndLV, &item);
161 if (index != -1) {
162 /* LPTSTR pszText = NULL; */
163 LPTSTR pszText = _T("value");
164 switch (dwValType) {
165 case REG_SZ:
166 case REG_EXPAND_SZ:
167 if(dwCount)
168 {
169 ListView_SetItemText(hwndLV, index, 2, ValBuf);
170 }
171 break;
172 case REG_DWORD: {
173 TCHAR buf[64];
174 wsprintf(buf, _T("0x%08X (%d)"), *(DWORD*)ValBuf, *(DWORD*)ValBuf);
175 ListView_SetItemText(hwndLV, index, 2, buf);
176 }
177 /* lpsRes = convertHexToDWORDStr(lpbData, dwLen); */
178 break;
179 case REG_BINARY: {
180 unsigned int i;
181 LPBYTE pData = (LPBYTE)ValBuf;
182 LPTSTR strBinary = HeapAlloc(GetProcessHeap(), 0, dwCount * sizeof(TCHAR) * 3 + 1);
183 for (i = 0; i < dwCount; i++)
184 wsprintf( strBinary + i*3, _T("%02X "), pData[i] );
185 strBinary[dwCount * 3] = 0;
186 ListView_SetItemText(hwndLV, index, 2, strBinary);
187 HeapFree(GetProcessHeap(), 0, strBinary);
188 }
189 break;
190 default:
191 /* lpsRes = convertHexToHexCSV(lpbData, dwLen); */
192 ListView_SetItemText(hwndLV, index, 2, pszText);
193 break;
194 }
195 }
196 }
197
198 static BOOL CreateListColumns(HWND hWndListView)
199 {
200 TCHAR szText[50];
201 int index;
202 LV_COLUMN lvC;
203
204 /* Create columns. */
205 lvC.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM;
206 lvC.pszText = szText;
207
208 /* Load the column labels from the resource file. */
209 for (index = 0; index < MAX_LIST_COLUMNS; index++) {
210 lvC.iSubItem = index;
211 lvC.cx = default_column_widths[index];
212 lvC.fmt = column_alignment[index];
213 LoadString(hInst, IDS_LIST_COLUMN_FIRST + index, szText, sizeof(szText)/sizeof(TCHAR));
214 if (ListView_InsertColumn(hWndListView, index, &lvC) == -1) return FALSE;
215 }
216 return TRUE;
217 }
218
219 static BOOL InitListViewImageLists(HWND hwndLV)
220 {
221 HIMAGELIST himl; /* handle to image list */
222 HICON hico; /* handle to icon */
223
224 /* Create the image list. */
225 if ((himl = ImageList_Create(CX_ICON, CY_ICON,
226 ILC_MASK, 0, NUM_ICONS)) == NULL)
227 return FALSE;
228
229 hico = LoadIcon(hInst, MAKEINTRESOURCE(IDI_BIN));
230 Image_Bin = ImageList_AddIcon(himl, hico);
231
232 hico = LoadIcon(hInst, MAKEINTRESOURCE(IDI_STRING));
233 Image_String = ImageList_AddIcon(himl, hico);
234
235
236 /* Fail if not all of the images were added. */
237 if (ImageList_GetImageCount(himl) < NUM_ICONS)
238 {
239 return FALSE;
240 }
241
242 /* Associate the image list with the tree view control. */
243 ListView_SetImageList(hwndLV, himl, LVSIL_SMALL);
244
245 return TRUE;
246 }
247
248 /* OnGetDispInfo - processes the LVN_GETDISPINFO notification message. */
249
250 static void OnGetDispInfo(NMLVDISPINFO* plvdi)
251 {
252 static TCHAR buffer[200];
253
254 plvdi->item.pszText = NULL;
255 plvdi->item.cchTextMax = 0;
256
257 switch (plvdi->item.iSubItem) {
258 case 0:
259 plvdi->item.pszText = _T("(Default)");
260 break;
261 case 1:
262 switch (((LINE_INFO*)plvdi->item.lParam)->dwValType) {
263 case REG_SZ:
264 plvdi->item.pszText = _T("REG_SZ");
265 break;
266 case REG_EXPAND_SZ:
267 plvdi->item.pszText = _T("REG_EXPAND_SZ");
268 break;
269 case REG_BINARY:
270 plvdi->item.pszText = _T("REG_BINARY");
271 break;
272 case REG_DWORD:
273 plvdi->item.pszText = _T("REG_DWORD");
274 break;
275 case REG_DWORD_BIG_ENDIAN:
276 plvdi->item.pszText = _T("REG_DWORD_BIG_ENDIAN");
277 break;
278 case REG_MULTI_SZ:
279 plvdi->item.pszText = _T("REG_MULTI_SZ");
280 break;
281 case REG_LINK:
282 plvdi->item.pszText = _T("REG_LINK");
283 break;
284 case REG_RESOURCE_LIST:
285 plvdi->item.pszText = _T("REG_RESOURCE_LIST");
286 break;
287 case REG_NONE:
288 plvdi->item.pszText = _T("REG_NONE");
289 break;
290 default:
291 wsprintf(buffer, _T("unknown(%d)"), plvdi->item.lParam);
292 plvdi->item.pszText = buffer;
293 break;
294 }
295 break;
296 case 2:
297 plvdi->item.pszText = _T("(value not set)");
298 break;
299 case 3:
300 plvdi->item.pszText = _T("");
301 break;
302 }
303 }
304
305 static int CALLBACK CompareFunc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
306 {
307 LINE_INFO*l, *r;
308 l = (LINE_INFO*)lParam1;
309 r = (LINE_INFO*)lParam2;
310
311 if (g_columnToSort == ~0UL)
312 g_columnToSort = 0;
313
314 if (g_columnToSort == 1 && l->dwValType != r->dwValType)
315 return g_invertSort ? (int)r->dwValType - (int)l->dwValType : (int)l->dwValType - (int)r->dwValType;
316 if (g_columnToSort == 2) {
317 /* FIXME: Sort on value */
318 }
319 return g_invertSort ? _tcscmp(r->name, l->name) : _tcscmp(l->name, r->name);
320 }
321
322 static BOOL _CmdWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
323 {
324 switch (LOWORD(wParam)) {
325 /* case ID_FILE_OPEN: */
326 /* break; */
327 default:
328 return FALSE;
329 }
330 return TRUE;
331 }
332
333 BOOL ListWndNotifyProc(HWND hWnd, WPARAM wParam, LPARAM lParam, BOOL *Result)
334 {
335 NMLVDISPINFO* Info;
336 *Result = TRUE;
337 switch (((LPNMHDR)lParam)->code) {
338 case LVN_GETDISPINFO:
339 OnGetDispInfo((NMLVDISPINFO*)lParam);
340 return TRUE;
341 case LVN_COLUMNCLICK:
342 if (g_columnToSort == ((LPNMLISTVIEW)lParam)->iSubItem)
343 g_invertSort = !g_invertSort;
344 else {
345 g_columnToSort = ((LPNMLISTVIEW)lParam)->iSubItem;
346 g_invertSort = FALSE;
347 }
348
349 ListView_SortItems(hWnd, CompareFunc, (WPARAM)hWnd);
350 return TRUE;
351 case NM_DBLCLK:
352 {
353 SendMessage(hFrameWnd, WM_COMMAND, MAKEWPARAM(ID_EDIT_MODIFY, 0), 0);
354 }
355 return TRUE;
356
357 case LVN_BEGINLABELEDIT:
358 {
359 PLINE_INFO lineinfo;
360 Info = (NMLVDISPINFO*)lParam;
361 if(Info)
362 {
363 lineinfo = (PLINE_INFO)Info->item.lParam;
364 if(!lineinfo->name || !strcmp(lineinfo->name, _T("")))
365 {
366 *Result = TRUE;
367 }
368 else
369 {
370 *Result = FALSE;
371 }
372 }
373 else
374 *Result = TRUE;
375 return TRUE;
376 }
377 }
378 return FALSE;
379 }
380
381
382 HWND CreateListView(HWND hwndParent, int id)
383 {
384 RECT rcClient;
385 HWND hwndLV;
386
387 /* Get the dimensions of the parent window's client area, and create the list view control. */
388 GetClientRect(hwndParent, &rcClient);
389 hwndLV = CreateWindowEx(WS_EX_CLIENTEDGE, WC_LISTVIEW, _T("List View"),
390 WS_VISIBLE | WS_CHILD | LVS_REPORT | LVS_EDITLABELS,
391 0, 0, rcClient.right, rcClient.bottom,
392 hwndParent, (HMENU)id, hInst, NULL);
393 if (!hwndLV) return NULL;
394
395 /* Initialize the image list, and add items to the control. */
396 if (!CreateListColumns(hwndLV)) goto fail;
397 if (!InitListViewImageLists(hwndLV)) goto fail;
398
399 return hwndLV;
400 fail:
401 DestroyWindow(hwndLV);
402 return NULL;
403 }
404
405 BOOL RefreshListView(HWND hwndLV, HKEY hKey, LPCTSTR keyPath)
406 {
407 DWORD max_sub_key_len;
408 DWORD max_val_name_len;
409 DWORD max_val_size;
410 DWORD val_count;
411 HKEY hNewKey;
412 LONG errCode;
413 INT count, i;
414 LVITEM item;
415 BOOL AddedDefault = FALSE;
416
417 if (!hwndLV) return FALSE;
418
419 ListView_EditLabel(hwndLV, -1);
420
421 SendMessage(hwndLV, WM_SETREDRAW, FALSE, 0);
422 count = ListView_GetItemCount(hwndLV);
423 for (i = 0; i < count; i++) {
424 item.mask = LVIF_PARAM;
425 item.iItem = i;
426 ListView_GetItem(hwndLV, &item);
427 free(((LINE_INFO*)item.lParam)->name);
428 HeapFree(GetProcessHeap(), 0, (void*)item.lParam);
429 }
430 g_columnToSort = ~0UL;
431 ListView_DeleteAllItems(hwndLV);
432
433 if(!hKey) return FALSE;
434
435 errCode = RegOpenKeyEx(hKey, keyPath, 0, KEY_READ, &hNewKey);
436 if (errCode != ERROR_SUCCESS) return FALSE;
437
438 /* get size information and resize the buffers if necessary */
439 errCode = RegQueryInfoKey(hNewKey, NULL, NULL, NULL, NULL, &max_sub_key_len, NULL,
440 &val_count, &max_val_name_len, &max_val_size, NULL, NULL);
441
442 #define BUF_HEAD_SPACE 2 /* FIXME: check why this is required with ROS ??? */
443
444 if (errCode == ERROR_SUCCESS) {
445 TCHAR* ValName = HeapAlloc(GetProcessHeap(), 0, ++max_val_name_len * sizeof(TCHAR) + BUF_HEAD_SPACE);
446 DWORD dwValNameLen = max_val_name_len;
447 BYTE* ValBuf = HeapAlloc(GetProcessHeap(), 0, ++max_val_size/* + BUF_HEAD_SPACE*/);
448 DWORD dwValSize = max_val_size;
449 DWORD dwIndex = 0L;
450 DWORD dwValType;
451 /* if (RegQueryValueEx(hNewKey, NULL, NULL, &dwValType, ValBuf, &dwValSize) == ERROR_SUCCESS) { */
452 /* AddEntryToList(hwndLV, _T("(Default)"), dwValType, ValBuf, dwValSize); */
453 /* } */
454 /* dwValSize = max_val_size; */
455 while (RegEnumValue(hNewKey, dwIndex, ValName, &dwValNameLen, NULL, &dwValType, ValBuf, &dwValSize) == ERROR_SUCCESS) {
456 ValBuf[dwValSize] = 0;
457 AddEntryToList(hwndLV, ValName, dwValType, ValBuf, dwValSize, -1);
458 dwValNameLen = max_val_name_len;
459 dwValSize = max_val_size;
460 dwValType = 0L;
461 ++dwIndex;
462 if(!strcmp(ValName, _T("")))
463 {
464 AddedDefault = TRUE;
465 }
466 }
467 HeapFree(GetProcessHeap(), 0, ValBuf);
468 HeapFree(GetProcessHeap(), 0, ValName);
469 }
470 if(!AddedDefault)
471 {
472 AddEntryToList(hwndLV, _T(""), REG_SZ, NULL, 0, 0);
473 }
474 ListView_SortItems(hwndLV, CompareFunc, (WPARAM)hwndLV);
475 RegCloseKey(hNewKey);
476 SendMessage(hwndLV, WM_SETREDRAW, TRUE, 0);
477
478 return TRUE;
479 }