Implement SetCPGlobal (an undocumented Win32 API).
[wine/multimedia.git] / programs / regedit / listview.c
blobe2aea8050666d18a9c619296d2d83e56022e0560
1 /*
2 * Regedit listviews
4 * Copyright (C) 2002 Robert Dickenson <robd@reactos.org>
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.
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.
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
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>
29 #include "main.h"
31 #include "wine/debug.h"
32 #include "wine/unicode.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(regedit);
36 static INT Image_String;
37 static INT Image_Binary;
39 typedef struct tagLINE_INFO
41 DWORD dwValType;
42 LPTSTR name;
43 void* val;
44 size_t val_len;
45 } LINE_INFO;
47 /*******************************************************************************
48 * Global and Local Variables:
51 static WNDPROC g_orgListWndProc;
52 static DWORD g_columnToSort = ~0UL;
53 static BOOL g_invertSort = FALSE;
54 static LPTSTR g_valueName;
55 static LPTSTR g_currentPath;
56 static HKEY g_currentRootKey;
58 #define MAX_LIST_COLUMNS (IDS_LIST_COLUMN_LAST - IDS_LIST_COLUMN_FIRST + 1)
59 static int default_column_widths[MAX_LIST_COLUMNS] = { 200, 175, 400 };
60 static int column_alignment[MAX_LIST_COLUMNS] = { LVCFMT_LEFT, LVCFMT_LEFT, LVCFMT_LEFT };
62 LPTSTR get_item_text(HWND hwndLV, int item)
64 LPTSTR newStr, curStr;
65 unsigned int maxLen = 128;
67 curStr = HeapAlloc(GetProcessHeap(), 0, maxLen);
68 if (!curStr) return NULL;
69 if (item == 0) return NULL; /* first item is ALWAYS a default */
70 do {
71 ListView_GetItemText(hwndLV, item, 0, curStr, maxLen);
72 if (_tcslen(curStr) < maxLen - 1) return curStr;
73 newStr = HeapReAlloc(GetProcessHeap(), 0, curStr, maxLen * 2);
74 if (!newStr) break;
75 curStr = newStr;
76 maxLen *= 2;
77 } while (TRUE);
78 HeapFree(GetProcessHeap(), 0, curStr);
79 return NULL;
82 LPCTSTR GetValueName(HWND hwndLV)
84 INT item;
86 if (g_valueName && g_valueName != LPSTR_TEXTCALLBACK)
87 HeapFree(GetProcessHeap(), 0, g_valueName);
88 g_valueName = NULL;
90 item = ListView_GetNextItem(hwndLV, -1, LVNI_FOCUSED);
91 if (item == -1) return NULL;
93 g_valueName = get_item_text(hwndLV, item);
95 return g_valueName;
98 /* convert '\0' separated string list into ',' separated string list */
99 static void MakeMULTISZDisplayable(LPTSTR multi)
103 for (; *multi; multi++)
105 if (*(multi+1))
107 *multi = ',';
108 multi++;
110 } while (*multi);
113 /*******************************************************************************
114 * Local module support methods
116 static void AddEntryToList(HWND hwndLV, LPTSTR Name, DWORD dwValType,
117 void* ValBuf, DWORD dwCount, BOOL bHighlight)
119 LINE_INFO* linfo;
120 LVITEM item;
121 int index;
123 linfo = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(LINE_INFO) + dwCount);
124 linfo->dwValType = dwValType;
125 linfo->val_len = dwCount;
126 memcpy(&linfo[1], ValBuf, dwCount);
128 if (Name)
129 linfo->name = _tcsdup(Name);
130 else
131 linfo->name = NULL;
133 item.mask = LVIF_TEXT | LVIF_PARAM | LVIF_STATE | LVIF_IMAGE;
134 item.iItem = ListView_GetItemCount(hwndLV);/*idx; */
135 item.iSubItem = 0;
136 item.state = 0;
137 item.stateMask = LVIS_FOCUSED | LVIS_SELECTED;
138 item.pszText = Name ? Name : LPSTR_TEXTCALLBACK;
139 item.cchTextMax = Name ? _tcslen(item.pszText) : 0;
140 if (bHighlight) {
141 item.stateMask = item.state = LVIS_FOCUSED | LVIS_SELECTED;
143 switch (dwValType)
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_Binary;
152 break;
154 item.lParam = (LPARAM)linfo;
156 #if (_WIN32_IE >= 0x0300)
157 item.iIndent = 0;
158 #endif
160 index = ListView_InsertItem(hwndLV, &item);
161 if (index != -1) {
162 /* LPTSTR pszText = NULL; */
163 LPTSTR pszText = _T("(cannot display value)");
164 switch (dwValType) {
165 case REG_SZ:
166 case REG_EXPAND_SZ:
167 if (ValBuf) {
168 ListView_SetItemText(hwndLV, index, 2, ValBuf);
169 } else {
170 ListView_SetItemText(hwndLV, index, 2, "(not set)");
172 break;
173 case REG_DWORD: {
174 TCHAR buf[64];
175 wsprintf(buf, _T("0x%08X (%d)"), *(DWORD*)ValBuf, *(DWORD*)ValBuf);
176 ListView_SetItemText(hwndLV, index, 2, buf);
178 /* lpsRes = convertHexToDWORDStr(lpbData, dwLen); */
179 break;
180 case REG_BINARY: {
181 unsigned int i;
182 LPBYTE pData = (LPBYTE)ValBuf;
183 LPTSTR strBinary = HeapAlloc(GetProcessHeap(), 0, dwCount * sizeof(TCHAR) * 3 + 1);
184 for (i = 0; i < dwCount; i++)
185 wsprintf( strBinary + i*3, _T("%02X "), pData[i] );
186 strBinary[dwCount * 3] = 0;
187 ListView_SetItemText(hwndLV, index, 2, strBinary);
188 HeapFree(GetProcessHeap(), 0, strBinary);
190 break;
191 case REG_MULTI_SZ:
192 MakeMULTISZDisplayable(ValBuf);
193 ListView_SetItemText(hwndLV, index, 2, ValBuf);
194 break;
195 default:
196 /* lpsRes = convertHexToHexCSV(lpbData, dwLen); */
197 ListView_SetItemText(hwndLV, index, 2, pszText);
198 break;
203 static BOOL InitListViewImageList(HWND hWndListView)
205 HIMAGELIST himl;
206 HICON hicon;
207 INT cx = GetSystemMetrics(SM_CXSMICON);
208 INT cy = GetSystemMetrics(SM_CYSMICON);
210 himl = ImageList_Create(cx, cy, ILC_MASK, 0, 2);
211 if (!himl)
212 return FALSE;
214 hicon = LoadImage(hInst, MAKEINTRESOURCE(IDI_STRING),
215 IMAGE_ICON, cx, cy, LR_DEFAULTCOLOR);
216 Image_String = ImageList_AddIcon(himl, hicon);
218 hicon = LoadImage(hInst, MAKEINTRESOURCE(IDI_BIN),
219 IMAGE_ICON, cx, cy, LR_DEFAULTCOLOR);
220 Image_Binary = ImageList_AddIcon(himl, hicon);
222 ListView_SetImageList(hWndListView, himl, LVSIL_SMALL);
224 /* fail if some of the icons failed to load */
225 if (ImageList_GetImageCount(himl) < 2)
226 return FALSE;
228 return TRUE;
231 static BOOL CreateListColumns(HWND hWndListView)
233 TCHAR szText[50];
234 int index;
235 LV_COLUMN lvC;
237 /* Create columns. */
238 lvC.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM;
239 lvC.pszText = szText;
241 /* Load the column labels from the resource file. */
242 for (index = 0; index < MAX_LIST_COLUMNS; index++) {
243 lvC.iSubItem = index;
244 lvC.cx = default_column_widths[index];
245 lvC.fmt = column_alignment[index];
246 LoadString(hInst, IDS_LIST_COLUMN_FIRST + index, szText, sizeof(szText)/sizeof(TCHAR));
247 if (ListView_InsertColumn(hWndListView, index, &lvC) == -1) return FALSE;
249 return TRUE;
252 /* OnGetDispInfo - processes the LVN_GETDISPINFO notification message. */
254 static void OnGetDispInfo(NMLVDISPINFO* plvdi)
256 static TCHAR buffer[200];
258 plvdi->item.pszText = NULL;
259 plvdi->item.cchTextMax = 0;
261 switch (plvdi->item.iSubItem) {
262 case 0:
263 plvdi->item.pszText = (LPSTR)g_pszDefaultValueName;
264 break;
265 case 1:
266 switch (((LINE_INFO*)plvdi->item.lParam)->dwValType) {
267 case REG_SZ:
268 plvdi->item.pszText = _T("REG_SZ");
269 break;
270 case REG_EXPAND_SZ:
271 plvdi->item.pszText = _T("REG_EXPAND_SZ");
272 break;
273 case REG_BINARY:
274 plvdi->item.pszText = _T("REG_BINARY");
275 break;
276 case REG_DWORD:
277 plvdi->item.pszText = _T("REG_DWORD");
278 break;
279 case REG_DWORD_BIG_ENDIAN:
280 plvdi->item.pszText = _T("REG_DWORD_BIG_ENDIAN");
281 break;
282 case REG_MULTI_SZ:
283 plvdi->item.pszText = _T("REG_MULTI_SZ");
284 break;
285 case REG_LINK:
286 plvdi->item.pszText = _T("REG_LINK");
287 break;
288 case REG_RESOURCE_LIST:
289 plvdi->item.pszText = _T("REG_RESOURCE_LIST");
290 break;
291 case REG_NONE:
292 plvdi->item.pszText = _T("REG_NONE");
293 break;
294 default:
295 wsprintf(buffer, _T("unknown(%d)"), plvdi->item.lParam);
296 plvdi->item.pszText = buffer;
297 break;
299 break;
300 case 2:
301 plvdi->item.pszText = _T("(value not set)");
302 break;
303 case 3:
304 plvdi->item.pszText = _T("");
305 break;
309 static int CALLBACK CompareFunc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
311 LINE_INFO*l, *r;
312 l = (LINE_INFO*)lParam1;
313 r = (LINE_INFO*)lParam2;
314 if (!l->name) return -1;
315 if (!r->name) return +1;
317 if (g_columnToSort == ~0UL)
318 g_columnToSort = 0;
320 if (g_columnToSort == 1 && l->dwValType != r->dwValType)
321 return g_invertSort ? (int)r->dwValType - (int)l->dwValType : (int)l->dwValType - (int)r->dwValType;
322 if (g_columnToSort == 2) {
323 /* FIXME: Sort on value */
325 return g_invertSort ? _tcscmp(r->name, l->name) : _tcscmp(l->name, r->name);
328 HWND StartValueRename(HWND hwndLV)
330 int item;
332 item = ListView_GetNextItem(hwndLV, -1, LVNI_FOCUSED | LVNI_SELECTED);
333 if (item < 1) { /* cannot rename default key */
334 MessageBeep(MB_ICONHAND);
335 return 0;
337 return ListView_EditLabel(hwndLV, item);
340 static BOOL _CmdWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
342 switch (LOWORD(wParam)) {
343 /* case ID_FILE_OPEN: */
344 /* break; */
345 default:
346 return FALSE;
348 return TRUE;
351 static LRESULT CALLBACK ListWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
353 switch (message) {
354 case WM_COMMAND:
355 if (!_CmdWndProc(hWnd, message, wParam, lParam)) {
356 return CallWindowProc(g_orgListWndProc, hWnd, message, wParam, lParam);
358 break;
359 case WM_NOTIFY_REFLECT:
360 switch (((LPNMHDR)lParam)->code) {
362 case LVN_BEGINLABELEDIT:
363 if (!((NMLVDISPINFO *)lParam)->item.iItem)
364 return 1;
365 return 0;
366 case LVN_GETDISPINFO:
367 OnGetDispInfo((NMLVDISPINFO*)lParam);
368 break;
369 case LVN_COLUMNCLICK:
370 if (g_columnToSort == ((LPNMLISTVIEW)lParam)->iSubItem)
371 g_invertSort = !g_invertSort;
372 else {
373 g_columnToSort = ((LPNMLISTVIEW)lParam)->iSubItem;
374 g_invertSort = FALSE;
377 ListView_SortItems(hWnd, CompareFunc, hWnd);
378 break;
379 case LVN_ENDLABELEDIT: {
380 LPNMLVDISPINFO dispInfo = (LPNMLVDISPINFO)lParam;
381 LPTSTR valueName = get_item_text(hWnd, dispInfo->item.iItem);
382 LONG ret;
383 if (!valueName) return -1; /* cannot rename a default value */
384 ret = RenameValue(hWnd, g_currentRootKey, g_currentPath, valueName, dispInfo->item.pszText);
385 if (ret)
386 RefreshListView(hWnd, g_currentRootKey, g_currentPath, dispInfo->item.pszText);
387 HeapFree(GetProcessHeap(), 0, valueName);
388 return 0;
390 case NM_RETURN: {
391 int cnt = ListView_GetNextItem(hWnd, -1, LVNI_FOCUSED | LVNI_SELECTED);
392 if (cnt != -1)
393 SendMessage(hFrameWnd, WM_COMMAND, ID_EDIT_MODIFY, 0);
395 break;
396 case NM_DBLCLK: {
397 NMITEMACTIVATE* nmitem = (LPNMITEMACTIVATE)lParam;
398 LVHITTESTINFO info;
400 /* if (nmitem->hdr.hwndFrom != hWnd) break; unnecessary because of WM_NOTIFY_REFLECT */
401 /* if (nmitem->hdr.idFrom != IDW_LISTVIEW) break; */
402 /* if (nmitem->hdr.code != ???) break; */
403 #ifdef _MSC_VER
404 switch (nmitem->uKeyFlags) {
405 case LVKF_ALT: /* The ALT key is pressed. */
406 /* properties dialog box ? */
407 break;
408 case LVKF_CONTROL: /* The CTRL key is pressed. */
409 /* run dialog box for providing parameters... */
410 break;
411 case LVKF_SHIFT: /* The SHIFT key is pressed. */
412 break;
414 #endif
415 info.pt.x = nmitem->ptAction.x;
416 info.pt.y = nmitem->ptAction.y;
417 if (ListView_HitTest(hWnd, &info) != -1) {
418 ListView_SetItemState(hWnd, -1, 0, LVIS_FOCUSED|LVIS_SELECTED);
419 ListView_SetItemState(hWnd, info.iItem, LVIS_FOCUSED|LVIS_SELECTED,
420 LVIS_FOCUSED|LVIS_SELECTED);
421 SendMessage(hFrameWnd, WM_COMMAND, ID_EDIT_MODIFY, 0);
424 break;
426 default:
427 return 0; /* shouldn't call default ! */
429 break;
430 case WM_CONTEXTMENU: {
431 int cnt = ListView_GetNextItem(hWnd, -1, LVNI_SELECTED);
432 TrackPopupMenu(GetSubMenu(hPopupMenus, cnt == -1 ? PM_NEW : PM_MODIFYVALUE),
433 TPM_RIGHTBUTTON, (short)LOWORD(lParam), (short)HIWORD(lParam),
434 0, hFrameWnd, NULL);
435 break;
437 default:
438 return CallWindowProc(g_orgListWndProc, hWnd, message, wParam, lParam);
439 break;
441 return 0;
445 HWND CreateListView(HWND hwndParent, int id)
447 RECT rcClient;
448 HWND hwndLV;
450 /* Get the dimensions of the parent window's client area, and create the list view control. */
451 GetClientRect(hwndParent, &rcClient);
452 hwndLV = CreateWindowEx(WS_EX_CLIENTEDGE, WC_LISTVIEW, _T("List View"),
453 WS_VISIBLE | WS_CHILD | WS_TABSTOP | LVS_REPORT | LVS_EDITLABELS,
454 0, 0, rcClient.right, rcClient.bottom,
455 hwndParent, (HMENU)id, hInst, NULL);
456 if (!hwndLV) return NULL;
457 ListView_SetExtendedListViewStyle(hwndLV, LVS_EX_FULLROWSELECT);
459 /* Initialize the image list */
460 if (!InitListViewImageList(hwndLV)) goto fail;
461 if (!CreateListColumns(hwndLV)) goto fail;
462 g_orgListWndProc = SubclassWindow(hwndLV, ListWndProc);
463 return hwndLV;
464 fail:
465 DestroyWindow(hwndLV);
466 return NULL;
469 BOOL RefreshListView(HWND hwndLV, HKEY hKeyRoot, LPCTSTR keyPath, LPCTSTR highlightValue)
471 BOOL result = FALSE;
472 DWORD max_sub_key_len;
473 DWORD max_val_name_len, valNameLen;
474 DWORD max_val_size, valSize;
475 DWORD val_count, index, valType;
476 TCHAR* valName = 0;
477 BYTE* valBuf = 0;
478 HKEY hKey = 0;
479 LONG errCode;
480 INT count, i;
481 LVITEM item;
483 if (!hwndLV) return FALSE;
485 SendMessage(hwndLV, WM_SETREDRAW, FALSE, 0);
487 errCode = RegOpenKeyEx(hKeyRoot, keyPath, 0, KEY_READ, &hKey);
488 if (errCode != ERROR_SUCCESS) goto done;
490 count = ListView_GetItemCount(hwndLV);
491 for (i = 0; i < count; i++) {
492 item.mask = LVIF_PARAM;
493 item.iItem = i;
494 ListView_GetItem(hwndLV, &item);
495 free(((LINE_INFO*)item.lParam)->name);
496 HeapFree(GetProcessHeap(), 0, (void*)item.lParam);
498 g_columnToSort = ~0UL;
499 ListView_DeleteAllItems(hwndLV);
501 /* get size information and resize the buffers if necessary */
502 errCode = RegQueryInfoKey(hKey, NULL, NULL, NULL, NULL, &max_sub_key_len, NULL,
503 &val_count, &max_val_name_len, &max_val_size, NULL, NULL);
504 if (errCode != ERROR_SUCCESS) goto done;
506 /* account for the terminator char */
507 max_val_name_len++;
508 max_val_size++;
510 valName = HeapAlloc(GetProcessHeap(), 0, max_val_name_len * sizeof(TCHAR));
511 valBuf = HeapAlloc(GetProcessHeap(), 0, max_val_size);
512 if (RegQueryValueEx(hKey, NULL, NULL, &valType, valBuf, &valSize) == ERROR_FILE_NOT_FOUND) {
513 AddEntryToList(hwndLV, NULL, REG_SZ, NULL, 0, !highlightValue);
515 /*dwValSize = max_val_size; */
516 for(index = 0; index < val_count; index++) {
517 BOOL bSelected = (valName == highlightValue); /* NOT a bug, we check for double NULL here */
518 valNameLen = max_val_name_len;
519 valSize = max_val_size;
520 valType = 0;
521 errCode = RegEnumValue(hKey, index, valName, &valNameLen, NULL, &valType, valBuf, &valSize);
522 if (errCode != ERROR_SUCCESS) goto done;
523 valBuf[valSize] = 0;
524 if (valName && highlightValue && !_tcscmp(valName, highlightValue))
525 bSelected = TRUE;
526 AddEntryToList(hwndLV, valName[0] ? valName : NULL, valType, valBuf, valSize, bSelected);
528 ListView_SortItems(hwndLV, CompareFunc, hwndLV);
530 g_currentRootKey = hKeyRoot;
531 if (keyPath != g_currentPath) {
532 HeapFree(GetProcessHeap(), 0, g_currentPath);
533 g_currentPath = HeapAlloc(GetProcessHeap(), 0, (lstrlen(keyPath) + 1) * sizeof(TCHAR));
534 if (!g_currentPath) goto done;
535 lstrcpy(g_currentPath, keyPath);
538 result = TRUE;
540 done:
541 HeapFree(GetProcessHeap(), 0, valBuf);
542 HeapFree(GetProcessHeap(), 0, valName);
543 SendMessage(hwndLV, WM_SETREDRAW, TRUE, 0);
544 if (hKey) RegCloseKey(hKey);
546 return result;