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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
29 #include "wine/debug.h"
30 #include "wine/unicode.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(regedit
);
34 static INT Image_String
;
35 static INT Image_Binary
;
37 typedef struct tagLINE_INFO
45 /*******************************************************************************
46 * Global and Local Variables:
49 static WNDPROC g_orgListWndProc
;
50 static DWORD g_columnToSort
= ~0UL;
51 static BOOL g_invertSort
= FALSE
;
52 static LPTSTR g_valueName
;
53 static LPTSTR g_currentPath
;
54 static HKEY g_currentRootKey
;
56 #define MAX_LIST_COLUMNS (IDS_LIST_COLUMN_LAST - IDS_LIST_COLUMN_FIRST + 1)
57 static int default_column_widths
[MAX_LIST_COLUMNS
] = { 200, 175, 400 };
58 static int column_alignment
[MAX_LIST_COLUMNS
] = { LVCFMT_LEFT
, LVCFMT_LEFT
, LVCFMT_LEFT
};
60 static LPTSTR
get_item_text(HWND hwndLV
, int item
)
62 LPTSTR newStr
, curStr
;
63 unsigned int maxLen
= 128;
65 curStr
= HeapAlloc(GetProcessHeap(), 0, maxLen
);
66 if (!curStr
) return NULL
;
67 if (item
== 0) return NULL
; /* first item is ALWAYS a default */
69 ListView_GetItemText(hwndLV
, item
, 0, curStr
, maxLen
);
70 if (_tcslen(curStr
) < maxLen
- 1) return curStr
;
71 newStr
= HeapReAlloc(GetProcessHeap(), 0, curStr
, maxLen
* 2);
76 HeapFree(GetProcessHeap(), 0, curStr
);
80 LPCTSTR
GetValueName(HWND hwndLV
)
84 if (g_valueName
&& g_valueName
!= LPSTR_TEXTCALLBACK
)
85 HeapFree(GetProcessHeap(), 0, g_valueName
);
88 item
= ListView_GetNextItem(hwndLV
, -1, LVNI_FOCUSED
);
89 if (item
== -1) return NULL
;
91 g_valueName
= get_item_text(hwndLV
, item
);
96 /* convert '\0' separated string list into ',' separated string list */
97 static void MakeMULTISZDisplayable(LPTSTR multi
)
101 for (; *multi
; multi
++)
111 /*******************************************************************************
112 * Local module support methods
114 static void AddEntryToList(HWND hwndLV
, LPTSTR Name
, DWORD dwValType
,
115 void* ValBuf
, DWORD dwCount
, BOOL bHighlight
)
121 linfo
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(LINE_INFO
) + dwCount
);
122 linfo
->dwValType
= dwValType
;
123 linfo
->val_len
= dwCount
;
124 memcpy(&linfo
[1], ValBuf
, dwCount
);
127 linfo
->name
= _tcsdup(Name
);
131 item
.mask
= LVIF_TEXT
| LVIF_PARAM
| LVIF_STATE
| LVIF_IMAGE
;
132 item
.iItem
= ListView_GetItemCount(hwndLV
);/*idx; */
135 item
.stateMask
= LVIS_FOCUSED
| LVIS_SELECTED
;
136 item
.pszText
= Name
? Name
: LPSTR_TEXTCALLBACK
;
137 item
.cchTextMax
= Name
? _tcslen(item
.pszText
) : 0;
139 item
.stateMask
= item
.state
= LVIS_FOCUSED
| LVIS_SELECTED
;
146 item
.iImage
= Image_String
;
149 item
.iImage
= Image_Binary
;
152 item
.lParam
= (LPARAM
)linfo
;
154 #if (_WIN32_IE >= 0x0300)
158 index
= ListView_InsertItem(hwndLV
, &item
);
160 /* LPTSTR pszText = NULL; */
161 LPTSTR pszText
= _T("(cannot display value)");
166 ListView_SetItemText(hwndLV
, index
, 2, ValBuf
);
168 ListView_SetItemText(hwndLV
, index
, 2, "(not set)");
173 wsprintf(buf
, _T("0x%08X (%d)"), *(DWORD
*)ValBuf
, *(DWORD
*)ValBuf
);
174 ListView_SetItemText(hwndLV
, index
, 2, buf
);
176 /* lpsRes = convertHexToDWORDStr(lpbData, dwLen); */
180 LPBYTE pData
= (LPBYTE
)ValBuf
;
181 LPTSTR strBinary
= HeapAlloc(GetProcessHeap(), 0, dwCount
* sizeof(TCHAR
) * 3 + 1);
182 for (i
= 0; i
< dwCount
; i
++)
183 wsprintf( strBinary
+ i
*3, _T("%02X "), pData
[i
] );
184 strBinary
[dwCount
* 3] = 0;
185 ListView_SetItemText(hwndLV
, index
, 2, strBinary
);
186 HeapFree(GetProcessHeap(), 0, strBinary
);
190 MakeMULTISZDisplayable(ValBuf
);
191 ListView_SetItemText(hwndLV
, index
, 2, ValBuf
);
194 /* lpsRes = convertHexToHexCSV(lpbData, dwLen); */
195 ListView_SetItemText(hwndLV
, index
, 2, pszText
);
201 static BOOL
InitListViewImageList(HWND hWndListView
)
205 INT cx
= GetSystemMetrics(SM_CXSMICON
);
206 INT cy
= GetSystemMetrics(SM_CYSMICON
);
208 himl
= ImageList_Create(cx
, cy
, ILC_MASK
, 0, 2);
212 hicon
= LoadImage(hInst
, MAKEINTRESOURCE(IDI_STRING
),
213 IMAGE_ICON
, cx
, cy
, LR_DEFAULTCOLOR
);
214 Image_String
= ImageList_AddIcon(himl
, hicon
);
216 hicon
= LoadImage(hInst
, MAKEINTRESOURCE(IDI_BIN
),
217 IMAGE_ICON
, cx
, cy
, LR_DEFAULTCOLOR
);
218 Image_Binary
= ImageList_AddIcon(himl
, hicon
);
220 SendMessage( hWndListView
, LVM_SETIMAGELIST
, LVSIL_SMALL
, (LPARAM
) himl
);
222 /* fail if some of the icons failed to load */
223 if (ImageList_GetImageCount(himl
) < 2)
229 static BOOL
CreateListColumns(HWND hWndListView
)
235 /* Create columns. */
236 lvC
.mask
= LVCF_FMT
| LVCF_WIDTH
| LVCF_TEXT
| LVCF_SUBITEM
;
237 lvC
.pszText
= szText
;
239 /* Load the column labels from the resource file. */
240 for (index
= 0; index
< MAX_LIST_COLUMNS
; index
++) {
241 lvC
.iSubItem
= index
;
242 lvC
.cx
= default_column_widths
[index
];
243 lvC
.fmt
= column_alignment
[index
];
244 LoadString(hInst
, IDS_LIST_COLUMN_FIRST
+ index
, szText
, sizeof(szText
)/sizeof(TCHAR
));
245 if (ListView_InsertColumn(hWndListView
, index
, &lvC
) == -1) return FALSE
;
250 /* OnGetDispInfo - processes the LVN_GETDISPINFO notification message. */
252 static void OnGetDispInfo(NMLVDISPINFO
* plvdi
)
254 static TCHAR buffer
[200];
256 plvdi
->item
.pszText
= NULL
;
257 plvdi
->item
.cchTextMax
= 0;
259 switch (plvdi
->item
.iSubItem
) {
261 plvdi
->item
.pszText
= (LPSTR
)g_pszDefaultValueName
;
264 switch (((LINE_INFO
*)plvdi
->item
.lParam
)->dwValType
) {
266 plvdi
->item
.pszText
= _T("REG_SZ");
269 plvdi
->item
.pszText
= _T("REG_EXPAND_SZ");
272 plvdi
->item
.pszText
= _T("REG_BINARY");
275 plvdi
->item
.pszText
= _T("REG_DWORD");
277 case REG_DWORD_BIG_ENDIAN
:
278 plvdi
->item
.pszText
= _T("REG_DWORD_BIG_ENDIAN");
281 plvdi
->item
.pszText
= _T("REG_MULTI_SZ");
284 plvdi
->item
.pszText
= _T("REG_LINK");
286 case REG_RESOURCE_LIST
:
287 plvdi
->item
.pszText
= _T("REG_RESOURCE_LIST");
290 plvdi
->item
.pszText
= _T("REG_NONE");
293 wsprintf(buffer
, _T("unknown(%d)"), plvdi
->item
.lParam
);
294 plvdi
->item
.pszText
= buffer
;
299 plvdi
->item
.pszText
= _T("(value not set)");
302 plvdi
->item
.pszText
= _T("");
307 static int CALLBACK
CompareFunc(LPARAM lParam1
, LPARAM lParam2
, LPARAM lParamSort
)
310 l
= (LINE_INFO
*)lParam1
;
311 r
= (LINE_INFO
*)lParam2
;
312 if (!l
->name
) return -1;
313 if (!r
->name
) return +1;
315 if (g_columnToSort
== ~0UL)
318 if (g_columnToSort
== 1 && l
->dwValType
!= r
->dwValType
)
319 return g_invertSort
? (int)r
->dwValType
- (int)l
->dwValType
: (int)l
->dwValType
- (int)r
->dwValType
;
320 if (g_columnToSort
== 2) {
321 /* FIXME: Sort on value */
323 return g_invertSort
? _tcscmp(r
->name
, l
->name
) : _tcscmp(l
->name
, r
->name
);
326 HWND
StartValueRename(HWND hwndLV
)
330 item
= ListView_GetNextItem(hwndLV
, -1, LVNI_FOCUSED
| LVNI_SELECTED
);
331 if (item
< 1) { /* cannot rename default key */
332 MessageBeep(MB_ICONHAND
);
335 return ListView_EditLabel(hwndLV
, item
);
338 static BOOL
_CmdWndProc(HWND hWnd
, UINT message
, WPARAM wParam
, LPARAM lParam
)
340 switch (LOWORD(wParam
)) {
341 /* case ID_FILE_OPEN: */
349 static LRESULT CALLBACK
ListWndProc(HWND hWnd
, UINT message
, WPARAM wParam
, LPARAM lParam
)
353 if (!_CmdWndProc(hWnd
, message
, wParam
, lParam
)) {
354 return CallWindowProc(g_orgListWndProc
, hWnd
, message
, wParam
, lParam
);
357 case WM_NOTIFY_REFLECT
:
358 switch (((LPNMHDR
)lParam
)->code
) {
360 case LVN_BEGINLABELEDIT
:
361 if (!((NMLVDISPINFO
*)lParam
)->item
.iItem
)
364 case LVN_GETDISPINFO
:
365 OnGetDispInfo((NMLVDISPINFO
*)lParam
);
367 case LVN_COLUMNCLICK
:
368 if (g_columnToSort
== ((LPNMLISTVIEW
)lParam
)->iSubItem
)
369 g_invertSort
= !g_invertSort
;
371 g_columnToSort
= ((LPNMLISTVIEW
)lParam
)->iSubItem
;
372 g_invertSort
= FALSE
;
375 SendMessage(hWnd
, LVM_SORTITEMS
, (WPARAM
)hWnd
, (LPARAM
)CompareFunc
);
377 case LVN_ENDLABELEDIT
: {
378 LPNMLVDISPINFO dispInfo
= (LPNMLVDISPINFO
)lParam
;
379 LPTSTR valueName
= get_item_text(hWnd
, dispInfo
->item
.iItem
);
381 if (!valueName
) return -1; /* cannot rename a default value */
382 ret
= RenameValue(hWnd
, g_currentRootKey
, g_currentPath
, valueName
, dispInfo
->item
.pszText
);
384 RefreshListView(hWnd
, g_currentRootKey
, g_currentPath
, dispInfo
->item
.pszText
);
385 HeapFree(GetProcessHeap(), 0, valueName
);
389 int cnt
= ListView_GetNextItem(hWnd
, -1, LVNI_FOCUSED
| LVNI_SELECTED
);
391 SendMessage(hFrameWnd
, WM_COMMAND
, ID_EDIT_MODIFY
, 0);
395 NMITEMACTIVATE
* nmitem
= (LPNMITEMACTIVATE
)lParam
;
398 /* if (nmitem->hdr.hwndFrom != hWnd) break; unnecessary because of WM_NOTIFY_REFLECT */
399 /* if (nmitem->hdr.idFrom != IDW_LISTVIEW) break; */
400 /* if (nmitem->hdr.code != ???) break; */
402 switch (nmitem
->uKeyFlags
) {
403 case LVKF_ALT
: /* The ALT key is pressed. */
404 /* properties dialog box ? */
406 case LVKF_CONTROL
: /* The CTRL key is pressed. */
407 /* run dialog box for providing parameters... */
409 case LVKF_SHIFT
: /* The SHIFT key is pressed. */
413 info
.pt
.x
= nmitem
->ptAction
.x
;
414 info
.pt
.y
= nmitem
->ptAction
.y
;
415 if (ListView_HitTest(hWnd
, &info
) != -1) {
416 ListView_SetItemState(hWnd
, -1, 0, LVIS_FOCUSED
|LVIS_SELECTED
);
417 ListView_SetItemState(hWnd
, info
.iItem
, LVIS_FOCUSED
|LVIS_SELECTED
,
418 LVIS_FOCUSED
|LVIS_SELECTED
);
419 SendMessage(hFrameWnd
, WM_COMMAND
, ID_EDIT_MODIFY
, 0);
425 return 0; /* shouldn't call default ! */
428 case WM_CONTEXTMENU
: {
429 int cnt
= ListView_GetNextItem(hWnd
, -1, LVNI_SELECTED
);
430 TrackPopupMenu(GetSubMenu(hPopupMenus
, cnt
== -1 ? PM_NEW
: PM_MODIFYVALUE
),
431 TPM_RIGHTBUTTON
, (short)LOWORD(lParam
), (short)HIWORD(lParam
),
436 return CallWindowProc(g_orgListWndProc
, hWnd
, message
, wParam
, lParam
);
443 HWND
CreateListView(HWND hwndParent
, int id
)
448 /* Get the dimensions of the parent window's client area, and create the list view control. */
449 GetClientRect(hwndParent
, &rcClient
);
450 hwndLV
= CreateWindowEx(WS_EX_CLIENTEDGE
, WC_LISTVIEW
, _T("List View"),
451 WS_VISIBLE
| WS_CHILD
| WS_TABSTOP
| LVS_REPORT
| LVS_EDITLABELS
,
452 0, 0, rcClient
.right
, rcClient
.bottom
,
453 hwndParent
, (HMENU
)id
, hInst
, NULL
);
454 if (!hwndLV
) return NULL
;
455 SendMessage(hwndLV
, LVM_SETEXTENDEDLISTVIEWSTYLE
, 0, LVS_EX_FULLROWSELECT
);
457 /* Initialize the image list */
458 if (!InitListViewImageList(hwndLV
)) goto fail
;
459 if (!CreateListColumns(hwndLV
)) goto fail
;
460 g_orgListWndProc
= (WNDPROC
) SetWindowLongPtr(hwndLV
, GWLP_WNDPROC
, (LPARAM
)ListWndProc
);
463 DestroyWindow(hwndLV
);
467 BOOL
RefreshListView(HWND hwndLV
, HKEY hKeyRoot
, LPCTSTR keyPath
, LPCTSTR highlightValue
)
470 DWORD max_sub_key_len
;
471 DWORD max_val_name_len
, valNameLen
;
472 DWORD max_val_size
, valSize
;
473 DWORD val_count
, index
, valType
;
481 if (!hwndLV
) return FALSE
;
483 SendMessage(hwndLV
, WM_SETREDRAW
, FALSE
, 0);
485 errCode
= RegOpenKeyEx(hKeyRoot
, keyPath
, 0, KEY_READ
, &hKey
);
486 if (errCode
!= ERROR_SUCCESS
) goto done
;
488 count
= ListView_GetItemCount(hwndLV
);
489 for (i
= 0; i
< count
; i
++) {
490 item
.mask
= LVIF_PARAM
;
492 SendMessage( hwndLV
, LVM_GETITEM
, 0, (LPARAM
)&item
);
493 free(((LINE_INFO
*)item
.lParam
)->name
);
494 HeapFree(GetProcessHeap(), 0, (void*)item
.lParam
);
496 g_columnToSort
= ~0UL;
497 SendMessage( hwndLV
, LVM_DELETEALLITEMS
, 0, 0L );
499 /* get size information and resize the buffers if necessary */
500 errCode
= RegQueryInfoKey(hKey
, NULL
, NULL
, NULL
, NULL
, &max_sub_key_len
, NULL
,
501 &val_count
, &max_val_name_len
, &max_val_size
, NULL
, NULL
);
502 if (errCode
!= ERROR_SUCCESS
) goto done
;
504 /* account for the terminator char */
508 valName
= HeapAlloc(GetProcessHeap(), 0, max_val_name_len
* sizeof(TCHAR
));
509 valBuf
= HeapAlloc(GetProcessHeap(), 0, max_val_size
);
510 if (RegQueryValueEx(hKey
, NULL
, NULL
, &valType
, valBuf
, &valSize
) == ERROR_FILE_NOT_FOUND
) {
511 AddEntryToList(hwndLV
, NULL
, REG_SZ
, NULL
, 0, !highlightValue
);
513 /*dwValSize = max_val_size; */
514 for(index
= 0; index
< val_count
; index
++) {
515 BOOL bSelected
= (valName
== highlightValue
); /* NOT a bug, we check for double NULL here */
516 valNameLen
= max_val_name_len
;
517 valSize
= max_val_size
;
519 errCode
= RegEnumValue(hKey
, index
, valName
, &valNameLen
, NULL
, &valType
, valBuf
, &valSize
);
520 if (errCode
!= ERROR_SUCCESS
) goto done
;
522 if (valName
&& highlightValue
&& !_tcscmp(valName
, highlightValue
))
524 AddEntryToList(hwndLV
, valName
[0] ? valName
: NULL
, valType
, valBuf
, valSize
, bSelected
);
526 SendMessage(hwndLV
, LVM_SORTITEMS
, (WPARAM
)hwndLV
, (LPARAM
)CompareFunc
);
528 g_currentRootKey
= hKeyRoot
;
529 if (keyPath
!= g_currentPath
) {
530 HeapFree(GetProcessHeap(), 0, g_currentPath
);
531 g_currentPath
= HeapAlloc(GetProcessHeap(), 0, (lstrlen(keyPath
) + 1) * sizeof(TCHAR
));
532 if (!g_currentPath
) goto done
;
533 lstrcpy(g_currentPath
, keyPath
);
539 HeapFree(GetProcessHeap(), 0, valBuf
);
540 HeapFree(GetProcessHeap(), 0, valName
);
541 SendMessage(hwndLV
, WM_SETREDRAW
, TRUE
, 0);
542 if (hKey
) RegCloseKey(hKey
);