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
31 typedef struct tagLINE_INFO
39 /*******************************************************************************
40 * Global and Local Variables:
43 static WNDPROC g_orgListWndProc
;
44 static DWORD g_columnToSort
= ~0UL;
45 static BOOL g_invertSort
= FALSE
;
46 static LPTSTR g_valueName
;
47 static LPTSTR g_currentPath
;
48 static HKEY g_currentRootKey
;
50 #define MAX_LIST_COLUMNS (IDS_LIST_COLUMN_LAST - IDS_LIST_COLUMN_FIRST + 1)
51 static int default_column_widths
[MAX_LIST_COLUMNS
] = { 200, 175, 400 };
52 static int column_alignment
[MAX_LIST_COLUMNS
] = { LVCFMT_LEFT
, LVCFMT_LEFT
, LVCFMT_LEFT
};
54 LPTSTR
get_item_text(HWND hwndLV
, int item
)
56 LPTSTR newStr
, curStr
;
59 curStr
= HeapAlloc(GetProcessHeap(), 0, maxLen
);
60 if (!curStr
) return NULL
;
62 ListView_GetItemText(hwndLV
, item
, 0, curStr
, maxLen
);
63 if (_tcslen(curStr
) < maxLen
- 1) return curStr
;
64 newStr
= HeapReAlloc(GetProcessHeap(), 0, curStr
, maxLen
* 2);
69 HeapFree(GetProcessHeap(), 0, curStr
);
73 LPCTSTR
GetValueName(HWND hwndLV
)
77 if (g_valueName
) HeapFree(GetProcessHeap(), 0, g_valueName
);
80 item
= ListView_GetNextItem(hwndLV
, -1, LVNI_FOCUSED
);
81 if (item
== -1) return NULL
;
83 g_valueName
= get_item_text(hwndLV
, item
);
88 /*******************************************************************************
89 * Local module support methods
91 static void AddEntryToList(HWND hwndLV
, LPTSTR Name
, DWORD dwValType
, void* ValBuf
, DWORD dwCount
)
97 linfo
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(LINE_INFO
) + dwCount
);
98 linfo
->dwValType
= dwValType
;
99 linfo
->val_len
= dwCount
;
100 memcpy(&linfo
[1], ValBuf
, dwCount
);
101 linfo
->name
= _tcsdup(Name
);
103 item
.mask
= LVIF_TEXT
| LVIF_PARAM
;
104 item
.iItem
= 0;/*idx; */
109 item
.cchTextMax
= _tcslen(item
.pszText
);
110 if (item
.cchTextMax
== 0)
111 item
.pszText
= LPSTR_TEXTCALLBACK
;
113 item
.lParam
= (LPARAM
)linfo
;
115 /* item.lParam = (LPARAM)ValBuf; */
116 #if (_WIN32_IE >= 0x0300)
120 index
= ListView_InsertItem(hwndLV
, &item
);
122 /* LPTSTR pszText = NULL; */
123 LPTSTR pszText
= _T("value");
127 ListView_SetItemText(hwndLV
, index
, 2, ValBuf
);
131 wsprintf(buf
, _T("0x%08X (%d)"), *(DWORD
*)ValBuf
, *(DWORD
*)ValBuf
);
132 ListView_SetItemText(hwndLV
, index
, 2, buf
);
134 /* lpsRes = convertHexToDWORDStr(lpbData, dwLen); */
138 LPBYTE pData
= (LPBYTE
)ValBuf
;
139 LPTSTR strBinary
= HeapAlloc(GetProcessHeap(), 0, dwCount
* sizeof(TCHAR
) * 3 + 1);
140 for (i
= 0; i
< dwCount
; i
++)
141 wsprintf( strBinary
+ i
*3, _T("%02X "), pData
[i
] );
142 strBinary
[dwCount
* 3] = 0;
143 ListView_SetItemText(hwndLV
, index
, 2, strBinary
);
144 HeapFree(GetProcessHeap(), 0, strBinary
);
148 /* lpsRes = convertHexToHexCSV(lpbData, dwLen); */
149 ListView_SetItemText(hwndLV
, index
, 2, pszText
);
155 static BOOL
CreateListColumns(HWND hWndListView
)
161 /* Create columns. */
162 lvC
.mask
= LVCF_FMT
| LVCF_WIDTH
| LVCF_TEXT
| LVCF_SUBITEM
;
163 lvC
.pszText
= szText
;
165 /* Load the column labels from the resource file. */
166 for (index
= 0; index
< MAX_LIST_COLUMNS
; index
++) {
167 lvC
.iSubItem
= index
;
168 lvC
.cx
= default_column_widths
[index
];
169 lvC
.fmt
= column_alignment
[index
];
170 LoadString(hInst
, IDS_LIST_COLUMN_FIRST
+ index
, szText
, sizeof(szText
)/sizeof(TCHAR
));
171 if (ListView_InsertColumn(hWndListView
, index
, &lvC
) == -1) return FALSE
;
176 /* OnGetDispInfo - processes the LVN_GETDISPINFO notification message. */
178 static void OnGetDispInfo(NMLVDISPINFO
* plvdi
)
180 static TCHAR buffer
[200];
182 plvdi
->item
.pszText
= NULL
;
183 plvdi
->item
.cchTextMax
= 0;
185 switch (plvdi
->item
.iSubItem
) {
187 plvdi
->item
.pszText
= _T("(Default)");
190 switch (((LINE_INFO
*)plvdi
->item
.lParam
)->dwValType
) {
192 plvdi
->item
.pszText
= _T("REG_SZ");
195 plvdi
->item
.pszText
= _T("REG_EXPAND_SZ");
198 plvdi
->item
.pszText
= _T("REG_BINARY");
201 plvdi
->item
.pszText
= _T("REG_DWORD");
203 case REG_DWORD_BIG_ENDIAN
:
204 plvdi
->item
.pszText
= _T("REG_DWORD_BIG_ENDIAN");
207 plvdi
->item
.pszText
= _T("REG_MULTI_SZ");
210 plvdi
->item
.pszText
= _T("REG_LINK");
212 case REG_RESOURCE_LIST
:
213 plvdi
->item
.pszText
= _T("REG_RESOURCE_LIST");
216 plvdi
->item
.pszText
= _T("REG_NONE");
219 wsprintf(buffer
, _T("unknown(%d)"), plvdi
->item
.lParam
);
220 plvdi
->item
.pszText
= buffer
;
225 plvdi
->item
.pszText
= _T("(value not set)");
228 plvdi
->item
.pszText
= _T("");
233 static int CALLBACK
CompareFunc(LPARAM lParam1
, LPARAM lParam2
, LPARAM lParamSort
)
236 l
= (LINE_INFO
*)lParam1
;
237 r
= (LINE_INFO
*)lParam2
;
239 if (g_columnToSort
== ~0UL)
242 if (g_columnToSort
== 1 && l
->dwValType
!= r
->dwValType
)
243 return g_invertSort
? (int)r
->dwValType
- (int)l
->dwValType
: (int)l
->dwValType
- (int)r
->dwValType
;
244 if (g_columnToSort
== 2) {
245 /* FIXME: Sort on value */
247 return g_invertSort
? _tcscmp(r
->name
, l
->name
) : _tcscmp(l
->name
, r
->name
);
250 static void ListViewPopUpMenu(HWND hWnd
, POINT pt
)
254 BOOL
StartValueRename(HWND hwndLV
)
258 item
= ListView_GetNextItem(hwndLV
, -1, LVNI_FOCUSED
| LVNI_SELECTED
);
259 if (item
< 0) return FALSE
;
260 if (!ListView_EditLabel(hwndLV
, item
)) return FALSE
;
264 static BOOL
_CmdWndProc(HWND hWnd
, UINT message
, WPARAM wParam
, LPARAM lParam
)
266 switch (LOWORD(wParam
)) {
267 /* case ID_FILE_OPEN: */
275 static LRESULT CALLBACK
ListWndProc(HWND hWnd
, UINT message
, WPARAM wParam
, LPARAM lParam
)
279 if (!_CmdWndProc(hWnd
, message
, wParam
, lParam
)) {
280 return CallWindowProc(g_orgListWndProc
, hWnd
, message
, wParam
, lParam
);
284 switch (((LPNMHDR
)lParam
)->code
) {
285 case LVN_GETDISPINFO
:
286 OnGetDispInfo((NMLVDISPINFO
*)lParam
);
288 case LVN_COLUMNCLICK
:
289 if (g_columnToSort
== ((LPNMLISTVIEW
)lParam
)->iSubItem
)
290 g_invertSort
= !g_invertSort
;
292 g_columnToSort
= ((LPNMLISTVIEW
)lParam
)->iSubItem
;
293 g_invertSort
= FALSE
;
296 ListView_SortItems(hWnd
, CompareFunc
, hWnd
);
298 case LVN_ENDLABELEDIT
: {
299 LPNMLVDISPINFO dispInfo
= (LPNMLVDISPINFO
)lParam
;
300 LPTSTR valueName
= get_item_text(hWnd
, dispInfo
->item
.iItem
);
301 BOOL res
= RenameValue(hWnd
, g_currentRootKey
, g_currentPath
, valueName
, dispInfo
->item
.pszText
);
302 if (res
) RefreshListView(hWnd
, g_currentRootKey
, g_currentPath
);
303 HeapFree(GetProcessHeap(), 0, valueName
);
307 NMITEMACTIVATE
* nmitem
= (LPNMITEMACTIVATE
)lParam
;
310 if (nmitem
->hdr
.hwndFrom
!= hWnd
) break;
311 /* if (nmitem->hdr.idFrom != IDW_LISTVIEW) break; */
312 /* if (nmitem->hdr.code != ???) break; */
314 switch (nmitem
->uKeyFlags
) {
315 case LVKF_ALT
: /* The ALT key is pressed. */
316 /* properties dialog box ? */
318 case LVKF_CONTROL
: /* The CTRL key is pressed. */
319 /* run dialog box for providing parameters... */
321 case LVKF_SHIFT
: /* The SHIFT key is pressed. */
325 info
.pt
.x
= nmitem
->ptAction
.x
;
326 info
.pt
.y
= nmitem
->ptAction
.y
;
327 if (ListView_HitTest(hWnd
, &info
) != -1) {
329 item
.mask
= LVIF_PARAM
;
330 item
.iItem
= info
.iItem
;
331 if (ListView_GetItem(hWnd
, &item
)) {}
339 NM_LISTVIEW
* pNm
= (NM_LISTVIEW
*)lParam
;
340 lvH
.pt
.x
= pNm
->ptAction
.x
;
341 lvH
.pt
.y
= pNm
->ptAction
.y
;
342 idx
= ListView_HitTest(hWnd
, &lvH
);
346 ListViewPopUpMenu(hWnd
, pt
);
353 return CallWindowProc(g_orgListWndProc
, hWnd
, message
, wParam
, lParam
);
356 case WM_CONTEXTMENU
: {
357 POINTS pt
= MAKEPOINTS(lParam
);
358 int cnt
= ListView_GetNextItem(hWnd
, -1, LVNI_FOCUSED
| LVNI_SELECTED
);
359 TrackPopupMenu(GetSubMenu(hPopupMenus
, cnt
== -1 ? PM_NEW
: PM_MODIFYVALUE
),
360 TPM_RIGHTBUTTON
, pt
.x
, pt
.y
, 0, hFrameWnd
, NULL
);
364 if (wParam
== VK_TAB
) {
365 /*TODO: SetFocus(Globals.hDriveBar) */
366 /*SetFocus(child->nFocusPanel? child->left.hWnd: child->right.hWnd); */
370 return CallWindowProc(g_orgListWndProc
, hWnd
, message
, wParam
, lParam
);
377 HWND
CreateListView(HWND hwndParent
, int id
)
382 /* Get the dimensions of the parent window's client area, and create the list view control. */
383 GetClientRect(hwndParent
, &rcClient
);
384 hwndLV
= CreateWindowEx(WS_EX_CLIENTEDGE
, WC_LISTVIEW
, _T("List View"),
385 WS_VISIBLE
| WS_CHILD
| WS_TABSTOP
| LVS_REPORT
| LVS_EDITLABELS
,
386 0, 0, rcClient
.right
, rcClient
.bottom
,
387 hwndParent
, (HMENU
)id
, hInst
, NULL
);
388 if (!hwndLV
) return NULL
;
389 ListView_SetExtendedListViewStyle(hwndLV
, LVS_EX_FULLROWSELECT
);
391 /* Initialize the image list, and add items to the control. */
393 if (!InitListViewImageLists(hwndLV)) goto fail;
394 if (!InitListViewItems(hwndLV, szName)) goto fail;
396 if (!CreateListColumns(hwndLV
)) goto fail
;
397 g_orgListWndProc
= SubclassWindow(hwndLV
, ListWndProc
);
400 DestroyWindow(hwndLV
);
404 BOOL
RefreshListView(HWND hwndLV
, HKEY hKeyRoot
, LPCTSTR keyPath
)
407 DWORD max_sub_key_len
;
408 DWORD max_val_name_len
, valNameLen
;
409 DWORD max_val_size
, valSize
;
410 DWORD val_count
, index
, valType
;
418 if (!hwndLV
) return FALSE
;
420 SendMessage(hwndLV
, WM_SETREDRAW
, FALSE
, 0);
422 errCode
= RegOpenKeyEx(hKeyRoot
, keyPath
, 0, KEY_READ
, &hKey
);
423 if (errCode
!= ERROR_SUCCESS
) goto done
;
425 count
= ListView_GetItemCount(hwndLV
);
426 for (i
= 0; i
< count
; i
++) {
427 item
.mask
= LVIF_PARAM
;
429 ListView_GetItem(hwndLV
, &item
);
430 free(((LINE_INFO
*)item
.lParam
)->name
);
431 HeapFree(GetProcessHeap(), 0, (void*)item
.lParam
);
433 g_columnToSort
= ~0UL;
434 ListView_DeleteAllItems(hwndLV
);
436 /* get size information and resize the buffers if necessary */
437 errCode
= RegQueryInfoKey(hKey
, NULL
, NULL
, NULL
, NULL
, &max_sub_key_len
, NULL
,
438 &val_count
, &max_val_name_len
, &max_val_size
, NULL
, NULL
);
439 if (errCode
!= ERROR_SUCCESS
) goto done
;
441 /* account for the terminator char */
445 valName
= HeapAlloc(GetProcessHeap(), 0, max_val_name_len
* sizeof(TCHAR
));
446 valBuf
= HeapAlloc(GetProcessHeap(), 0, max_val_size
);
447 /*if (RegQueryValueEx(hKey, NULL, NULL, &dwValType, ValBuf, &dwValSize) == ERROR_SUCCESS) { */
448 /* AddEntryToList(hwndLV, _T("(Default)"), dwValType, ValBuf, dwValSize); */
450 /*dwValSize = max_val_size; */
451 for(index
= 0; index
< val_count
; index
++) {
452 valNameLen
= max_val_name_len
;
453 valSize
= max_val_size
;
455 errCode
= RegEnumValue(hKey
, index
, valName
, &valNameLen
, NULL
, &valType
, valBuf
, &valSize
);
456 if (errCode
!= ERROR_SUCCESS
) goto done
;
458 AddEntryToList(hwndLV
, valName
, valType
, valBuf
, valSize
);
460 ListView_SortItems(hwndLV
, CompareFunc
, hwndLV
);
462 g_currentRootKey
= hKeyRoot
;
463 if (keyPath
!= g_currentPath
) {
464 HeapFree(GetProcessHeap(), 0, g_currentPath
);
465 g_currentPath
= HeapAlloc(GetProcessHeap(), 0, (lstrlen(keyPath
) + 1) * sizeof(TCHAR
));
466 if (!g_currentPath
) goto done
;
467 lstrcpy(g_currentPath
, keyPath
);
473 HeapFree(GetProcessHeap(), 0, valBuf
);
474 HeapFree(GetProcessHeap(), 0, valName
);
475 SendMessage(hwndLV
, WM_SETREDRAW
, TRUE
, 0);
476 if (hKey
) RegCloseKey(hKey
);