Reformat regedit in a consistent manner.
[wine.git] / programs / regedit / listview.c
blobf6760c00d226f38d64d66bb987f55ec241428bb2
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 <commctrl.h>
23 #include <stdlib.h>
24 #include <tchar.h>
25 #include <process.h>
26 #include <stdio.h>
28 #include "commctrl.h"
30 #include <windowsx.h>
31 #include "main.h"
34 /*******************************************************************************
35 * Global and Local Variables:
38 static WNDPROC g_orgListWndProc;
40 #define MAX_LIST_COLUMNS (IDS_LIST_COLUMN_LAST - IDS_LIST_COLUMN_FIRST + 1)
41 static int default_column_widths[MAX_LIST_COLUMNS] = { 200, 175, 400 };
42 static int column_alignment[MAX_LIST_COLUMNS] = { LVCFMT_LEFT, LVCFMT_LEFT, LVCFMT_LEFT };
45 /*******************************************************************************
46 * Local module support methods
48 static void AddEntryToList(HWND hwndLV, LPTSTR Name, DWORD dwValType, void* ValBuf, DWORD dwCount)
50 LVITEM item;
51 int index;
53 item.mask = LVIF_TEXT | LVIF_PARAM;
54 item.iItem = 0;/*idx; */
55 item.iSubItem = 0;
56 item.state = 0;
57 item.stateMask = 0;
58 item.pszText = Name;
59 item.cchTextMax = _tcslen(item.pszText);
60 if (item.cchTextMax == 0)
61 item.pszText = LPSTR_TEXTCALLBACK;
62 item.iImage = 0;
63 item.lParam = (LPARAM)dwValType;
64 /* item.lParam = (LPARAM)ValBuf; */
65 #if (_WIN32_IE >= 0x0300)
66 item.iIndent = 0;
67 #endif
69 index = ListView_InsertItem(hwndLV, &item);
70 if (index != -1) {
71 /* LPTSTR pszText = NULL; */
72 LPTSTR pszText = _T("value");
73 switch (dwValType) {
74 case REG_SZ:
75 case REG_EXPAND_SZ:
76 ListView_SetItemText(hwndLV, index, 2, ValBuf);
77 break;
78 case REG_DWORD: {
79 TCHAR buf[64];
80 wsprintf(buf, _T("0x%08X (%d)"), *(DWORD*)ValBuf, *(DWORD*)ValBuf);
81 ListView_SetItemText(hwndLV, index, 2, buf);
83 /* lpsRes = convertHexToDWORDStr(lpbData, dwLen); */
84 break;
85 case REG_BINARY: {
86 unsigned int i;
87 LPBYTE pData = (LPBYTE)ValBuf;
88 LPTSTR strBinary = HeapAlloc(GetProcessHeap(), 0, dwCount * sizeof(TCHAR) * 3 + 1);
89 for (i = 0; i < dwCount; i++)
90 wsprintf( strBinary + i*3, _T("%02X "), pData[i] );
91 strBinary[dwCount * 3] = 0;
92 ListView_SetItemText(hwndLV, index, 2, strBinary);
93 HeapFree(GetProcessHeap(), 0, strBinary);
95 break;
96 default:
97 /* lpsRes = convertHexToHexCSV(lpbData, dwLen); */
98 ListView_SetItemText(hwndLV, index, 2, pszText);
99 break;
104 static void CreateListColumns(HWND hWndListView)
106 TCHAR szText[50];
107 int index;
108 LV_COLUMN lvC;
110 /* Create columns. */
111 lvC.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM;
112 lvC.pszText = szText;
114 /* Load the column labels from the resource file. */
115 for (index = 0; index < MAX_LIST_COLUMNS; index++) {
116 lvC.iSubItem = index;
117 lvC.cx = default_column_widths[index];
118 lvC.fmt = column_alignment[index];
119 LoadString(hInst, IDS_LIST_COLUMN_FIRST + index, szText, sizeof(szText)/sizeof(TCHAR));
120 if (ListView_InsertColumn(hWndListView, index, &lvC) == -1) {
121 /* TODO: handle failure condition... */
122 break;
127 /* OnGetDispInfo - processes the LVN_GETDISPINFO notification message. */
129 static void OnGetDispInfo(NMLVDISPINFO* plvdi)
131 static TCHAR buffer[200];
133 plvdi->item.pszText = NULL;
134 plvdi->item.cchTextMax = 0;
136 switch (plvdi->item.iSubItem) {
137 case 0:
138 plvdi->item.pszText = _T("(Default)");
139 break;
140 case 1:
141 switch (plvdi->item.lParam) {
142 case REG_SZ:
143 plvdi->item.pszText = _T("REG_SZ");
144 break;
145 case REG_EXPAND_SZ:
146 plvdi->item.pszText = _T("REG_EXPAND_SZ");
147 break;
148 case REG_BINARY:
149 plvdi->item.pszText = _T("REG_BINARY");
150 break;
151 case REG_DWORD:
152 plvdi->item.pszText = _T("REG_DWORD");
153 break;
154 /* case REG_DWORD_LITTLE_ENDIAN: */
155 /* plvdi->item.pszText = _T("REG_DWORD_LITTLE_ENDIAN"); */
156 /* break; */
157 case REG_DWORD_BIG_ENDIAN:
158 plvdi->item.pszText = _T("REG_DWORD_BIG_ENDIAN");
159 break;
160 case REG_MULTI_SZ:
161 plvdi->item.pszText = _T("REG_MULTI_SZ");
162 break;
163 case REG_LINK:
164 plvdi->item.pszText = _T("REG_LINK");
165 break;
166 case REG_RESOURCE_LIST:
167 plvdi->item.pszText = _T("REG_RESOURCE_LIST");
168 break;
169 case REG_NONE:
170 plvdi->item.pszText = _T("REG_NONE");
171 break;
172 default:
173 wsprintf(buffer, _T("unknown(%d)"), plvdi->item.lParam);
174 plvdi->item.pszText = buffer;
175 break;
177 break;
178 case 2:
179 plvdi->item.pszText = _T("(value not set)");
180 break;
181 case 3:
182 plvdi->item.pszText = _T("");
183 break;
187 #if 0
188 static int CALLBACK CompareFunc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
190 TCHAR buf1[1000];
191 TCHAR buf2[1000];
193 ListView_GetItemText((HWND)lParamSort, lParam1, 0, buf1, sizeof(buf1));
194 ListView_GetItemText((HWND)lParamSort, lParam2, 0, buf2, sizeof(buf2));
195 return _tcscmp(buf1, buf2);
197 #endif
199 static void ListViewPopUpMenu(HWND hWnd, POINT pt)
202 static BOOL _CmdWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
204 switch (LOWORD(wParam)) {
205 /* case ID_FILE_OPEN: */
206 /* break; */
207 default:
208 return FALSE;
210 return TRUE;
213 static LRESULT CALLBACK ListWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
215 switch (message) {
216 case WM_COMMAND:
217 if (!_CmdWndProc(hWnd, message, wParam, lParam)) {
218 return CallWindowProc(g_orgListWndProc, hWnd, message, wParam, lParam);
220 break;
221 case WM_NOTIFY:
222 switch (((LPNMHDR)lParam)->code) {
223 case LVN_GETDISPINFO:
224 OnGetDispInfo((NMLVDISPINFO*)lParam);
225 break;
226 case NM_DBLCLK: {
227 NMITEMACTIVATE* nmitem = (LPNMITEMACTIVATE)lParam;
228 LVHITTESTINFO info;
230 if (nmitem->hdr.hwndFrom != hWnd) break;
231 /* if (nmitem->hdr.idFrom != IDW_LISTVIEW) break; */
232 /* if (nmitem->hdr.code != ???) break; */
233 #ifdef _MSC_VER
234 switch (nmitem->uKeyFlags) {
235 case LVKF_ALT: /* The ALT key is pressed. */
236 /* properties dialog box ? */
237 break;
238 case LVKF_CONTROL: /* The CTRL key is pressed. */
239 /* run dialog box for providing parameters... */
240 break;
241 case LVKF_SHIFT: /* The SHIFT key is pressed. */
242 break;
244 #endif
245 info.pt.x = nmitem->ptAction.x;
246 info.pt.y = nmitem->ptAction.y;
247 if (ListView_HitTest(hWnd, &info) != -1) {
248 LVITEM item;
249 item.mask = LVIF_PARAM;
250 item.iItem = info.iItem;
251 if (ListView_GetItem(hWnd, &item)) {}
254 break;
256 case NM_RCLICK: {
257 int idx;
258 LV_HITTESTINFO lvH;
259 NM_LISTVIEW* pNm = (NM_LISTVIEW*)lParam;
260 lvH.pt.x = pNm->ptAction.x;
261 lvH.pt.y = pNm->ptAction.y;
262 idx = ListView_HitTest(hWnd, &lvH);
263 if (idx != -1) {
264 POINT pt;
265 GetCursorPos(&pt);
266 ListViewPopUpMenu(hWnd, pt);
267 return idx;
270 break;
272 default:
273 return CallWindowProc(g_orgListWndProc, hWnd, message, wParam, lParam);
275 break;
276 case WM_KEYDOWN:
277 if (wParam == VK_TAB) {
278 /*TODO: SetFocus(Globals.hDriveBar) */
279 /*SetFocus(child->nFocusPanel? child->left.hWnd: child->right.hWnd); */
281 /* fall thru... */
282 default:
283 return CallWindowProc(g_orgListWndProc, hWnd, message, wParam, lParam);
284 break;
286 return 0;
290 HWND CreateListView(HWND hwndParent, int id)
292 RECT rcClient;
293 HWND hwndLV;
295 /* Get the dimensions of the parent window's client area, and create the list view control. */
296 GetClientRect(hwndParent, &rcClient);
297 hwndLV = CreateWindowEx(WS_EX_CLIENTEDGE, WC_LISTVIEW, _T("List View"),
298 WS_VISIBLE | WS_CHILD | LVS_REPORT,
299 0, 0, rcClient.right, rcClient.bottom,
300 hwndParent, (HMENU)id, hInst, NULL);
301 ListView_SetExtendedListViewStyle(hwndLV, LVS_EX_FULLROWSELECT);
303 /* Initialize the image list, and add items to the control. */
305 if (!InitListViewImageLists(hwndLV) ||
306 !InitListViewItems(hwndLV, szName)) {
307 DestroyWindow(hwndLV);
308 return FALSE;
311 CreateListColumns(hwndLV);
312 g_orgListWndProc = SubclassWindow(hwndLV, ListWndProc);
313 return hwndLV;
316 BOOL RefreshListView(HWND hwndLV, HKEY hKey, LPTSTR keyPath)
318 if (hwndLV != NULL) {
319 ListView_DeleteAllItems(hwndLV);
322 if (hKey != NULL) {
323 HKEY hNewKey;
324 LONG errCode = RegOpenKeyEx(hKey, keyPath, 0, KEY_READ, &hNewKey);
325 if (errCode == ERROR_SUCCESS) {
326 DWORD max_sub_key_len;
327 DWORD max_val_name_len;
328 DWORD max_val_size;
329 DWORD val_count;
330 ShowWindow(hwndLV, SW_HIDE);
331 /* get size information and resize the buffers if necessary */
332 errCode = RegQueryInfoKey(hNewKey, NULL, NULL, NULL, NULL,
333 &max_sub_key_len, NULL, &val_count, &max_val_name_len, &max_val_size, NULL, NULL);
335 #define BUF_HEAD_SPACE 2 /* TODO: check why this is required with ROS ??? */
337 if (errCode == ERROR_SUCCESS) {
338 TCHAR* ValName = HeapAlloc(GetProcessHeap(), 0, ++max_val_name_len * sizeof(TCHAR) + BUF_HEAD_SPACE);
339 DWORD dwValNameLen = max_val_name_len;
340 BYTE* ValBuf = HeapAlloc(GetProcessHeap(), 0, ++max_val_size/* + BUF_HEAD_SPACE*/);
341 DWORD dwValSize = max_val_size;
342 DWORD dwIndex = 0L;
343 DWORD dwValType;
344 /* if (RegQueryValueEx(hNewKey, NULL, NULL, &dwValType, ValBuf, &dwValSize) == ERROR_SUCCESS) { */
345 /* AddEntryToList(hwndLV, _T("(Default)"), dwValType, ValBuf, dwValSize); */
346 /* } */
347 /* dwValSize = max_val_size; */
348 while (RegEnumValue(hNewKey, dwIndex, ValName, &dwValNameLen, NULL, &dwValType, ValBuf, &dwValSize) == ERROR_SUCCESS) {
349 ValBuf[dwValSize] = 0;
350 AddEntryToList(hwndLV, ValName, dwValType, ValBuf, dwValSize);
351 dwValNameLen = max_val_name_len;
352 dwValSize = max_val_size;
353 dwValType = 0L;
354 ++dwIndex;
356 HeapFree(GetProcessHeap(), 0, ValBuf);
357 HeapFree(GetProcessHeap(), 0, ValName);
359 /*ListView_SortItemsEx(hwndLV, CompareFunc, hwndLV); */
360 /* SendMessage(hwndLV, LVM_SORTITEMSEX, (WPARAM)CompareFunc, (LPARAM)hwndLV); */
361 ShowWindow(hwndLV, SW_SHOW);
362 RegCloseKey(hNewKey);
365 return TRUE;