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 #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
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 */
71 ListView_GetItemText(hwndLV
, item
, 0, curStr
, maxLen
);
72 if (_tcslen(curStr
) < maxLen
- 1) return curStr
;
73 newStr
= HeapReAlloc(GetProcessHeap(), 0, curStr
, maxLen
* 2);
78 HeapFree(GetProcessHeap(), 0, curStr
);
82 LPCTSTR
GetValueName(HWND hwndLV
)
86 if (g_valueName
&& g_valueName
!= LPSTR_TEXTCALLBACK
)
87 HeapFree(GetProcessHeap(), 0, g_valueName
);
90 item
= ListView_GetNextItem(hwndLV
, -1, LVNI_FOCUSED
);
91 if (item
== -1) return NULL
;
93 g_valueName
= get_item_text(hwndLV
, item
);
98 /* convert '\0' separated string list into ',' separated string list */
99 static void MakeMULTISZDisplayable(LPTSTR multi
)
103 for (; *multi
; multi
++)
113 /*******************************************************************************
114 * Local module support methods
116 static void AddEntryToList(HWND hwndLV
, LPTSTR Name
, DWORD dwValType
,
117 void* ValBuf
, DWORD dwCount
, BOOL bHighlight
)
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
);
129 linfo
->name
= _tcsdup(Name
);
133 item
.mask
= LVIF_TEXT
| LVIF_PARAM
| LVIF_STATE
| LVIF_IMAGE
;
134 item
.iItem
= ListView_GetItemCount(hwndLV
);/*idx; */
137 item
.stateMask
= LVIS_FOCUSED
| LVIS_SELECTED
;
138 item
.pszText
= Name
? Name
: LPSTR_TEXTCALLBACK
;
139 item
.cchTextMax
= Name
? _tcslen(item
.pszText
) : 0;
141 item
.stateMask
= item
.state
= LVIS_FOCUSED
| LVIS_SELECTED
;
148 item
.iImage
= Image_String
;
151 item
.iImage
= Image_Binary
;
154 item
.lParam
= (LPARAM
)linfo
;
156 #if (_WIN32_IE >= 0x0300)
160 index
= ListView_InsertItem(hwndLV
, &item
);
162 /* LPTSTR pszText = NULL; */
163 LPTSTR pszText
= _T("(cannot display value)");
168 ListView_SetItemText(hwndLV
, index
, 2, ValBuf
);
170 ListView_SetItemText(hwndLV
, index
, 2, "(not set)");
175 wsprintf(buf
, _T("0x%08X (%d)"), *(DWORD
*)ValBuf
, *(DWORD
*)ValBuf
);
176 ListView_SetItemText(hwndLV
, index
, 2, buf
);
178 /* lpsRes = convertHexToDWORDStr(lpbData, dwLen); */
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
);
192 MakeMULTISZDisplayable(ValBuf
);
193 ListView_SetItemText(hwndLV
, index
, 2, ValBuf
);
196 /* lpsRes = convertHexToHexCSV(lpbData, dwLen); */
197 ListView_SetItemText(hwndLV
, index
, 2, pszText
);
203 static BOOL
InitListViewImageList(HWND hWndListView
)
207 INT cx
= GetSystemMetrics(SM_CXSMICON
);
208 INT cy
= GetSystemMetrics(SM_CYSMICON
);
210 himl
= ImageList_Create(cx
, cy
, ILC_MASK
, 0, 2);
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)
231 static BOOL
CreateListColumns(HWND hWndListView
)
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
;
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
) {
263 plvdi
->item
.pszText
= (LPSTR
)g_pszDefaultValueName
;
266 switch (((LINE_INFO
*)plvdi
->item
.lParam
)->dwValType
) {
268 plvdi
->item
.pszText
= _T("REG_SZ");
271 plvdi
->item
.pszText
= _T("REG_EXPAND_SZ");
274 plvdi
->item
.pszText
= _T("REG_BINARY");
277 plvdi
->item
.pszText
= _T("REG_DWORD");
279 case REG_DWORD_BIG_ENDIAN
:
280 plvdi
->item
.pszText
= _T("REG_DWORD_BIG_ENDIAN");
283 plvdi
->item
.pszText
= _T("REG_MULTI_SZ");
286 plvdi
->item
.pszText
= _T("REG_LINK");
288 case REG_RESOURCE_LIST
:
289 plvdi
->item
.pszText
= _T("REG_RESOURCE_LIST");
292 plvdi
->item
.pszText
= _T("REG_NONE");
295 wsprintf(buffer
, _T("unknown(%d)"), plvdi
->item
.lParam
);
296 plvdi
->item
.pszText
= buffer
;
301 plvdi
->item
.pszText
= _T("(value not set)");
304 plvdi
->item
.pszText
= _T("");
309 static int CALLBACK
CompareFunc(LPARAM lParam1
, LPARAM lParam2
, LPARAM lParamSort
)
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)
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
)
332 item
= ListView_GetNextItem(hwndLV
, -1, LVNI_FOCUSED
| LVNI_SELECTED
);
333 if (item
< 1) { /* cannot rename default key */
334 MessageBeep(MB_ICONHAND
);
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: */
351 static LRESULT CALLBACK
ListWndProc(HWND hWnd
, UINT message
, WPARAM wParam
, LPARAM lParam
)
355 if (!_CmdWndProc(hWnd
, message
, wParam
, lParam
)) {
356 return CallWindowProc(g_orgListWndProc
, hWnd
, message
, wParam
, lParam
);
359 case WM_NOTIFY_REFLECT
:
360 switch (((LPNMHDR
)lParam
)->code
) {
362 case LVN_BEGINLABELEDIT
:
363 if (!((NMLVDISPINFO
*)lParam
)->item
.iItem
)
366 case LVN_GETDISPINFO
:
367 OnGetDispInfo((NMLVDISPINFO
*)lParam
);
369 case LVN_COLUMNCLICK
:
370 if (g_columnToSort
== ((LPNMLISTVIEW
)lParam
)->iSubItem
)
371 g_invertSort
= !g_invertSort
;
373 g_columnToSort
= ((LPNMLISTVIEW
)lParam
)->iSubItem
;
374 g_invertSort
= FALSE
;
377 ListView_SortItems(hWnd
, CompareFunc
, hWnd
);
379 case LVN_ENDLABELEDIT
: {
380 LPNMLVDISPINFO dispInfo
= (LPNMLVDISPINFO
)lParam
;
381 LPTSTR valueName
= get_item_text(hWnd
, dispInfo
->item
.iItem
);
383 if (!valueName
) return -1; /* cannot rename a default value */
384 ret
= RenameValue(hWnd
, g_currentRootKey
, g_currentPath
, valueName
, dispInfo
->item
.pszText
);
386 RefreshListView(hWnd
, g_currentRootKey
, g_currentPath
, dispInfo
->item
.pszText
);
387 HeapFree(GetProcessHeap(), 0, valueName
);
391 int cnt
= ListView_GetNextItem(hWnd
, -1, LVNI_FOCUSED
| LVNI_SELECTED
);
393 SendMessage(hFrameWnd
, WM_COMMAND
, ID_EDIT_MODIFY
, 0);
397 NMITEMACTIVATE
* nmitem
= (LPNMITEMACTIVATE
)lParam
;
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; */
404 switch (nmitem
->uKeyFlags
) {
405 case LVKF_ALT
: /* The ALT key is pressed. */
406 /* properties dialog box ? */
408 case LVKF_CONTROL
: /* The CTRL key is pressed. */
409 /* run dialog box for providing parameters... */
411 case LVKF_SHIFT
: /* The SHIFT key is pressed. */
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);
427 return 0; /* shouldn't call default ! */
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
),
438 return CallWindowProc(g_orgListWndProc
, hWnd
, message
, wParam
, lParam
);
445 HWND
CreateListView(HWND hwndParent
, int id
)
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
);
465 DestroyWindow(hwndLV
);
469 BOOL
RefreshListView(HWND hwndLV
, HKEY hKeyRoot
, LPCTSTR keyPath
, LPCTSTR highlightValue
)
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
;
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
;
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 */
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
;
521 errCode
= RegEnumValue(hKey
, index
, valName
, &valNameLen
, NULL
, &valType
, valBuf
, &valSize
);
522 if (errCode
!= ERROR_SUCCESS
) goto done
;
524 if (valName
&& highlightValue
&& !_tcscmp(valName
, highlightValue
))
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
);
541 HeapFree(GetProcessHeap(), 0, valBuf
);
542 HeapFree(GetProcessHeap(), 0, valName
);
543 SendMessage(hwndLV
, WM_SETREDRAW
, TRUE
, 0);
544 if (hKey
) RegCloseKey(hKey
);