remove whitespace from end of lines
[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, int iStartAt)
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, iStartAt, 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 || !_tcscmp(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, BOOL ValExists)
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 switch (dwValType) {
163 case REG_SZ:
164 case REG_EXPAND_SZ:
165 if(dwCount > 0)
166 {
167 ListView_SetItemText(hwndLV, index, 2, ValBuf);
168 }
169 else if(!ValExists)
170 {
171 TCHAR buffer[255];
172 /* load (value not set) string */
173 LoadString(hInst, IDS_VALUE_NOT_SET, buffer, sizeof(buffer)/sizeof(TCHAR));
174 ListView_SetItemText(hwndLV, index, 2, buffer);
175 }
176 break;
177 case REG_MULTI_SZ:
178 {
179 LPTSTR src, str, cursrc;
180 if(dwCount >= 2)
181 {
182 src = (LPTSTR)ValBuf;
183 str = HeapAlloc(GetProcessHeap(), 0, dwCount);
184 if(str != NULL)
185 {
186 *str = _T('\0');
187 /* concatenate all srings */
188 while(*src != _T('\0'))
189 {
190 _tcscat(str, src);
191 _tcscat(str, _T(" "));
192 src += _tcslen(src) + 1;
193 }
194 ListView_SetItemText(hwndLV, index, 2, str);
195 HeapFree(GetProcessHeap(), 0, str);
196 }
197 else
198 ListView_SetItemText(hwndLV, index, 2, _T(""));
199 }
200 else
201 ListView_SetItemText(hwndLV, index, 2, _T(""));
202 }
203 break;
204 case REG_DWORD: {
205 TCHAR buf[200];
206 if(dwCount == sizeof(DWORD))
207 {
208 wsprintf(buf, _T("0x%08X (%d)"), *(DWORD*)ValBuf, *(DWORD*)ValBuf);
209 }
210 else
211 {
212 LoadString(hInst, IDS_INVALID_DWORD, buf, sizeof(buf)/sizeof(TCHAR));
213 }
214 ListView_SetItemText(hwndLV, index, 2, buf);
215 }
216 /* lpsRes = convertHexToDWORDStr(lpbData, dwLen); */
217 break;
218 default:
219 {
220 unsigned int i;
221 LPBYTE pData = (LPBYTE)ValBuf;
222 LPTSTR strBinary;
223 if(dwCount > 0)
224 {
225 strBinary = HeapAlloc(GetProcessHeap(), 0, (dwCount * sizeof(TCHAR) * 3) + 1);
226 for (i = 0; i < dwCount; i++)
227 {
228 wsprintf( strBinary + i*3, _T("%02X "), pData[i] );
229 }
230 strBinary[dwCount * 3] = 0;
231 ListView_SetItemText(hwndLV, index, 2, strBinary);
232 HeapFree(GetProcessHeap(), 0, strBinary);
233 }
234 else
235 {
236 TCHAR szText[128];
237 LoadString(hInst, IDS_BINARY_EMPTY, szText, sizeof(szText)/sizeof(TCHAR));
238 ListView_SetItemText(hwndLV, index, 2, szText);
239 }
240 }
241 break;
242 }
243 }
244 }
245
246 static BOOL CreateListColumns(HWND hWndListView)
247 {
248 TCHAR szText[50];
249 int index;
250 LV_COLUMN lvC;
251
252 /* Create columns. */
253 lvC.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM;
254 lvC.pszText = szText;
255
256 /* Load the column labels from the resource file. */
257 for (index = 0; index < MAX_LIST_COLUMNS; index++) {
258 lvC.iSubItem = index;
259 lvC.cx = default_column_widths[index];
260 lvC.fmt = column_alignment[index];
261 LoadString(hInst, IDS_LIST_COLUMN_FIRST + index, szText, sizeof(szText)/sizeof(TCHAR));
262 if (ListView_InsertColumn(hWndListView, index, &lvC) == -1) return FALSE;
263 }
264 return TRUE;
265 }
266
267 static BOOL InitListViewImageLists(HWND hwndLV)
268 {
269 HIMAGELIST himl; /* handle to image list */
270 HICON hico; /* handle to icon */
271
272 /* Create the image list. */
273 if ((himl = ImageList_Create(CX_ICON, CY_ICON,
274 ILC_MASK, 0, NUM_ICONS)) == NULL)
275 return FALSE;
276
277 hico = LoadIcon(hInst, MAKEINTRESOURCE(IDI_BIN));
278 Image_Bin = ImageList_AddIcon(himl, hico);
279
280 hico = LoadIcon(hInst, MAKEINTRESOURCE(IDI_STRING));
281 Image_String = ImageList_AddIcon(himl, hico);
282
283
284 /* Fail if not all of the images were added. */
285 if (ImageList_GetImageCount(himl) < NUM_ICONS)
286 {
287 return FALSE;
288 }
289
290 /* Associate the image list with the tree view control. */
291 ListView_SetImageList(hwndLV, himl, LVSIL_SMALL);
292
293 return TRUE;
294 }
295
296 /* OnGetDispInfo - processes the LVN_GETDISPINFO notification message. */
297
298 static void OnGetDispInfo(NMLVDISPINFO* plvdi)
299 {
300 static TCHAR buffer[200];
301
302 plvdi->item.pszText = NULL;
303 plvdi->item.cchTextMax = 0;
304
305 switch (plvdi->item.iSubItem) {
306 case 0:
307 LoadString(hInst, IDS_DEFAULT_VALUE_NAME, buffer, sizeof(buffer)/sizeof(TCHAR));
308 plvdi->item.pszText = buffer;
309 break;
310 case 1:
311 switch (((LINE_INFO*)plvdi->item.lParam)->dwValType) {
312 case REG_SZ:
313 plvdi->item.pszText = _T("REG_SZ");
314 break;
315 case REG_EXPAND_SZ:
316 plvdi->item.pszText = _T("REG_EXPAND_SZ");
317 break;
318 case REG_BINARY:
319 plvdi->item.pszText = _T("REG_BINARY");
320 break;
321 case REG_DWORD:
322 plvdi->item.pszText = _T("REG_DWORD");
323 break;
324 case REG_DWORD_BIG_ENDIAN:
325 plvdi->item.pszText = _T("REG_DWORD_BIG_ENDIAN");
326 break;
327 case REG_MULTI_SZ:
328 plvdi->item.pszText = _T("REG_MULTI_SZ");
329 break;
330 case REG_LINK:
331 plvdi->item.pszText = _T("REG_LINK");
332 break;
333 case REG_RESOURCE_LIST:
334 plvdi->item.pszText = _T("REG_RESOURCE_LIST");
335 break;
336 case REG_FULL_RESOURCE_DESCRIPTOR:
337 plvdi->item.pszText = _T("REG_FULL_RESOURCE_DESCRIPTOR");
338 break;
339 case REG_RESOURCE_REQUIREMENTS_LIST:
340 plvdi->item.pszText = _T("REG_RESOURCE_REQUIREMENTS_LIST");
341 break;
342 case REG_NONE:
343 plvdi->item.pszText = _T("REG_NONE");
344 break;
345 default: {
346 TCHAR buf2[200];
347 LoadString(hInst, IDS_UNKNOWN_TYPE, buf2, sizeof(buf2)/sizeof(TCHAR));
348 wsprintf(buffer, buf2, ((LINE_INFO*)plvdi->item.lParam)->dwValType);
349 plvdi->item.pszText = buffer;
350 break;
351 }
352 }
353 break;
354 case 3:
355 plvdi->item.pszText = _T("");
356 break;
357 }
358 }
359
360 static int CALLBACK CompareFunc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
361 {
362 LINE_INFO*l, *r;
363 l = (LINE_INFO*)lParam1;
364 r = (LINE_INFO*)lParam2;
365
366 if (g_columnToSort == ~0UL)
367 g_columnToSort = 0;
368
369 if (g_columnToSort == 1 && l->dwValType != r->dwValType)
370 return g_invertSort ? (int)r->dwValType - (int)l->dwValType : (int)l->dwValType - (int)r->dwValType;
371 if (g_columnToSort == 2) {
372 /* FIXME: Sort on value */
373 }
374 return g_invertSort ? _tcscmp(r->name, l->name) : _tcscmp(l->name, r->name);
375 }
376
377 static BOOL _CmdWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
378 {
379 switch (LOWORD(wParam)) {
380 /* case ID_FILE_OPEN: */
381 /* break; */
382 default:
383 return FALSE;
384 }
385 return TRUE;
386 }
387
388 BOOL ListWndNotifyProc(HWND hWnd, WPARAM wParam, LPARAM lParam, BOOL *Result)
389 {
390 NMLVDISPINFO* Info;
391 *Result = TRUE;
392 switch (((LPNMHDR)lParam)->code) {
393 case LVN_GETDISPINFO:
394 OnGetDispInfo((NMLVDISPINFO*)lParam);
395 return TRUE;
396 case LVN_COLUMNCLICK:
397 if (g_columnToSort == ((LPNMLISTVIEW)lParam)->iSubItem)
398 g_invertSort = !g_invertSort;
399 else {
400 g_columnToSort = ((LPNMLISTVIEW)lParam)->iSubItem;
401 g_invertSort = FALSE;
402 }
403
404 ListView_SortItems(hWnd, CompareFunc, (WPARAM)hWnd);
405 return TRUE;
406 case NM_DBLCLK:
407 case NM_RETURN:
408 {
409 SendMessage(hFrameWnd, WM_COMMAND, MAKEWPARAM(ID_EDIT_MODIFY, 0), 0);
410 }
411 return TRUE;
412 case NM_SETFOCUS:
413 g_pChildWnd->nFocusPanel = 0;
414 break;
415 case LVN_BEGINLABELEDIT:
416 {
417 PLINE_INFO lineinfo;
418 Info = (NMLVDISPINFO*)lParam;
419 if(Info)
420 {
421 lineinfo = (PLINE_INFO)Info->item.lParam;
422 if(!lineinfo->name || !_tcscmp(lineinfo->name, _T("")))
423 {
424 *Result = TRUE;
425 }
426 else
427 {
428 *Result = FALSE;
429 }
430 }
431 else
432 *Result = TRUE;
433 return TRUE;
434 }
435 case LVN_ENDLABELEDIT:
436 {
437 PLINE_INFO lineinfo;
438 Info = (NMLVDISPINFO*)lParam;
439 if(Info && Info->item.pszText)
440 {
441 lineinfo = (PLINE_INFO)Info->item.lParam;
442 if(!lineinfo->name || !_tcscmp(lineinfo->name, _T("")))
443 {
444 *Result = FALSE;
445 }
446 else
447 {
448 LONG ret;
449 //if((ret = RenameValue(lineinfo->name, Info->item.pszText)) != ERROR_SUCCESS)
450 {
451 TCHAR msg[128], caption[128];
452
453 LoadString(hInst, IDS_ERR_RENVAL_CAPTION, caption, sizeof(caption)/sizeof(TCHAR));
454 if(_tcslen(Info->item.pszText) == 0)
455 {
456 LoadString(hInst, IDS_ERR_RENVAL_TOEMPTY, msg, sizeof(msg)/sizeof(TCHAR));
457 }
458 else
459 _stprintf(msg, _T("rename from %s to %s"), lineinfo->name, Info->item.pszText);
460 MessageBox(0, msg, NULL, 0);
461 *Result = TRUE;
462 }
463 }
464 }
465 else
466 *Result = TRUE;
467 return TRUE;
468 }
469 }
470 return FALSE;
471 }
472
473
474 HWND CreateListView(HWND hwndParent, int id)
475 {
476 RECT rcClient;
477 HWND hwndLV;
478
479 /* Get the dimensions of the parent window's client area, and create the list view control. */
480 GetClientRect(hwndParent, &rcClient);
481 hwndLV = CreateWindowEx(WS_EX_CLIENTEDGE, WC_LISTVIEW, _T("List View"),
482 WS_VISIBLE | WS_CHILD | WS_TABSTOP | LVS_REPORT | LVS_EDITLABELS,
483 0, 0, rcClient.right, rcClient.bottom,
484 hwndParent, (HMENU)id, hInst, NULL);
485 if (!hwndLV) return NULL;
486
487 /* Initialize the image list, and add items to the control. */
488 if (!CreateListColumns(hwndLV)) goto fail;
489 if (!InitListViewImageLists(hwndLV)) goto fail;
490
491 return hwndLV;
492 fail:
493 DestroyWindow(hwndLV);
494 return NULL;
495 }
496
497 BOOL RefreshListView(HWND hwndLV, HKEY hKey, LPCTSTR keyPath)
498 {
499 DWORD max_sub_key_len;
500 DWORD max_val_name_len;
501 DWORD max_val_size;
502 DWORD val_count;
503 HKEY hNewKey;
504 LONG errCode;
505 INT count, i;
506 LVITEM item;
507 BOOL AddedDefault = FALSE;
508
509 if (!hwndLV) return FALSE;
510
511 ListView_EditLabel(hwndLV, -1);
512
513 SendMessage(hwndLV, WM_SETREDRAW, FALSE, 0);
514 count = ListView_GetItemCount(hwndLV);
515 for (i = 0; i < count; i++) {
516 item.mask = LVIF_PARAM;
517 item.iItem = i;
518 ListView_GetItem(hwndLV, &item);
519 free(((LINE_INFO*)item.lParam)->name);
520 HeapFree(GetProcessHeap(), 0, (void*)item.lParam);
521 }
522 g_columnToSort = ~0UL;
523 ListView_DeleteAllItems(hwndLV);
524
525 if(!hKey) return FALSE;
526
527 errCode = RegOpenKeyEx(hKey, keyPath, 0, KEY_READ, &hNewKey);
528 if (errCode != ERROR_SUCCESS) return FALSE;
529
530 /* get size information and resize the buffers if necessary */
531 errCode = RegQueryInfoKey(hNewKey, NULL, NULL, NULL, NULL, &max_sub_key_len, NULL,
532 &val_count, &max_val_name_len, &max_val_size, NULL, NULL);
533
534 #define BUF_HEAD_SPACE 2 /* FIXME: check why this is required with ROS ??? */
535
536 if (errCode == ERROR_SUCCESS) {
537 TCHAR* ValName = HeapAlloc(GetProcessHeap(), 0, ++max_val_name_len * sizeof(TCHAR) + BUF_HEAD_SPACE);
538 DWORD dwValNameLen = max_val_name_len;
539 BYTE* ValBuf = HeapAlloc(GetProcessHeap(), 0, ++max_val_size/* + BUF_HEAD_SPACE*/);
540 DWORD dwValSize = max_val_size;
541 DWORD dwIndex = 0L;
542 DWORD dwValType;
543 /* if (RegQueryValueEx(hNewKey, NULL, NULL, &dwValType, ValBuf, &dwValSize) == ERROR_SUCCESS) { */
544 /* AddEntryToList(hwndLV, _T("(Default)"), dwValType, ValBuf, dwValSize); */
545 /* } */
546 /* dwValSize = max_val_size; */
547 while (RegEnumValue(hNewKey, dwIndex, ValName, &dwValNameLen, NULL, &dwValType, ValBuf, &dwValSize) == ERROR_SUCCESS) {
548 ValBuf[dwValSize] = 0;
549 AddEntryToList(hwndLV, ValName, dwValType, ValBuf, dwValSize, -1, TRUE);
550 dwValNameLen = max_val_name_len;
551 dwValSize = max_val_size;
552 dwValType = 0L;
553 ++dwIndex;
554 if(!_tcscmp(ValName, _T("")))
555 {
556 AddedDefault = TRUE;
557 }
558 }
559 HeapFree(GetProcessHeap(), 0, ValBuf);
560 HeapFree(GetProcessHeap(), 0, ValName);
561 }
562 if(!AddedDefault)
563 {
564 AddEntryToList(hwndLV, _T(""), REG_SZ, NULL, 0, 0, FALSE);
565 }
566 ListView_SortItems(hwndLV, CompareFunc, (WPARAM)hwndLV);
567 RegCloseKey(hNewKey);
568 SendMessage(hwndLV, WM_SETREDRAW, TRUE, 0);
569
570 return TRUE;
571 }