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/unicode.h"
30 static INT Image_String
;
31 static INT Image_Binary
;
33 typedef struct tagLINE_INFO
41 /*******************************************************************************
42 * Global and Local Variables:
45 static WNDPROC g_orgListWndProc
;
46 static DWORD g_columnToSort
= ~0U;
47 static BOOL g_invertSort
= FALSE
;
48 static LPTSTR g_valueName
;
49 static LPTSTR g_currentPath
;
50 static HKEY g_currentRootKey
;
51 static TCHAR g_szValueNotSet
[64];
53 #define MAX_LIST_COLUMNS (IDS_LIST_COLUMN_LAST - IDS_LIST_COLUMN_FIRST + 1)
54 static int default_column_widths
[MAX_LIST_COLUMNS
] = { 200, 175, 400 };
55 static int column_alignment
[MAX_LIST_COLUMNS
] = { LVCFMT_LEFT
, LVCFMT_LEFT
, LVCFMT_LEFT
};
57 static LPTSTR
get_item_text(HWND hwndLV
, int item
)
59 LPTSTR newStr
, curStr
;
60 unsigned int maxLen
= 128;
62 curStr
= HeapAlloc(GetProcessHeap(), 0, maxLen
);
63 if (!curStr
) return NULL
;
64 if (item
== 0) { /* first item is ALWAYS a default */
65 HeapFree(GetProcessHeap(), 0, curStr
);
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
!= 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
);
164 ListView_SetItemText(hwndLV
, index
, 2, ValBuf
);
166 ListView_SetItemText(hwndLV
, index
, 2, g_szValueNotSet
);
171 wsprintf(buf
, _T("0x%08x (%u)"), *(DWORD
*)ValBuf
, *(DWORD
*)ValBuf
);
172 ListView_SetItemText(hwndLV
, index
, 2, buf
);
177 LPBYTE pData
= (LPBYTE
)ValBuf
;
178 LPTSTR strBinary
= HeapAlloc(GetProcessHeap(), 0, dwCount
* sizeof(TCHAR
) * 3 + 1);
179 for (i
= 0; i
< dwCount
; i
++)
180 wsprintf( strBinary
+ i
*3, _T("%02X "), pData
[i
] );
181 strBinary
[dwCount
* 3] = 0;
182 ListView_SetItemText(hwndLV
, index
, 2, strBinary
);
183 HeapFree(GetProcessHeap(), 0, strBinary
);
187 MakeMULTISZDisplayable(ValBuf
);
188 ListView_SetItemText(hwndLV
, index
, 2, ValBuf
);
193 LoadString(hInst
, IDS_REGISTRY_VALUE_CANT_DISPLAY
, szText
, COUNT_OF(szText
));
194 ListView_SetItemText(hwndLV
, index
, 2, szText
);
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];
255 static TCHAR reg_szT
[] = {'R','E','G','_','S','Z',0},
256 reg_expand_szT
[] = {'R','E','G','_','E','X','P','A','N','D','_','S','Z',0},
257 reg_binaryT
[] = {'R','E','G','_','B','I','N','A','R','Y',0},
258 reg_dwordT
[] = {'R','E','G','_','D','W','O','R','D',0},
259 reg_dword_big_endianT
[] = {'R','E','G','_','D','W','O','R','D','_',
260 'B','I','G','_','E','N','D','I','A','N',0},
261 reg_multi_szT
[] = {'R','E','G','_','M','U','L','T','I','_','S','Z',0},
262 reg_linkT
[] = {'R','E','G','_','L','I','N','K',0},
263 reg_resource_listT
[] = {'R','E','G','_','R','E','S','O','U','R','C','E','_','L','I','S','T',0},
264 reg_noneT
[] = {'R','E','G','_','N','O','N','E',0},
267 plvdi
->item
.pszText
= NULL
;
268 plvdi
->item
.cchTextMax
= 0;
270 switch (plvdi
->item
.iSubItem
) {
272 plvdi
->item
.pszText
= (LPSTR
)g_pszDefaultValueName
;
275 switch (((LINE_INFO
*)plvdi
->item
.lParam
)->dwValType
) {
277 plvdi
->item
.pszText
= reg_szT
;
280 plvdi
->item
.pszText
= reg_expand_szT
;
283 plvdi
->item
.pszText
= reg_binaryT
;
286 plvdi
->item
.pszText
= reg_dwordT
;
288 case REG_DWORD_BIG_ENDIAN
:
289 plvdi
->item
.pszText
= reg_dword_big_endianT
;
292 plvdi
->item
.pszText
= reg_multi_szT
;
295 plvdi
->item
.pszText
= reg_linkT
;
297 case REG_RESOURCE_LIST
:
298 plvdi
->item
.pszText
= reg_resource_listT
;
301 plvdi
->item
.pszText
= reg_noneT
;
305 TCHAR szUnknownFmt
[64];
306 LoadString(hInst
, IDS_REGISTRY_UNKNOWN_TYPE
, szUnknownFmt
, COUNT_OF(szUnknownFmt
));
307 wsprintf(buffer
, szUnknownFmt
, plvdi
->item
.lParam
);
308 plvdi
->item
.pszText
= buffer
;
314 plvdi
->item
.pszText
= g_szValueNotSet
;
317 plvdi
->item
.pszText
= emptyT
;
322 static int CALLBACK
CompareFunc(LPARAM lParam1
, LPARAM lParam2
, LPARAM lParamSort
)
325 l
= (LINE_INFO
*)lParam1
;
326 r
= (LINE_INFO
*)lParam2
;
327 if (!l
->name
) return -1;
328 if (!r
->name
) return +1;
330 if (g_columnToSort
== ~0U)
333 if (g_columnToSort
== 1 && l
->dwValType
!= r
->dwValType
)
334 return g_invertSort
? (int)r
->dwValType
- (int)l
->dwValType
: (int)l
->dwValType
- (int)r
->dwValType
;
335 if (g_columnToSort
== 2) {
336 /* FIXME: Sort on value */
338 return g_invertSort
? _tcscmp(r
->name
, l
->name
) : _tcscmp(l
->name
, r
->name
);
341 HWND
StartValueRename(HWND hwndLV
)
345 item
= ListView_GetNextItem(hwndLV
, -1, LVNI_FOCUSED
| LVNI_SELECTED
);
346 if (item
< 1) { /* cannot rename default key */
347 MessageBeep(MB_ICONHAND
);
350 return ListView_EditLabel(hwndLV
, item
);
353 static BOOL
_CmdWndProc(HWND hWnd
, UINT message
, WPARAM wParam
, LPARAM lParam
)
355 switch (LOWORD(wParam
)) {
356 /* case ID_FILE_OPEN: */
364 static LRESULT CALLBACK
ListWndProc(HWND hWnd
, UINT message
, WPARAM wParam
, LPARAM lParam
)
368 if (!_CmdWndProc(hWnd
, message
, wParam
, lParam
)) {
369 return CallWindowProc(g_orgListWndProc
, hWnd
, message
, wParam
, lParam
);
372 case WM_NOTIFY_REFLECT
:
373 switch (((LPNMHDR
)lParam
)->code
) {
375 case LVN_BEGINLABELEDIT
:
376 if (!((NMLVDISPINFO
*)lParam
)->item
.iItem
)
379 case LVN_GETDISPINFO
:
380 OnGetDispInfo((NMLVDISPINFO
*)lParam
);
382 case LVN_COLUMNCLICK
:
383 if (g_columnToSort
== ((LPNMLISTVIEW
)lParam
)->iSubItem
)
384 g_invertSort
= !g_invertSort
;
386 g_columnToSort
= ((LPNMLISTVIEW
)lParam
)->iSubItem
;
387 g_invertSort
= FALSE
;
390 SendMessage(hWnd
, LVM_SORTITEMS
, (WPARAM
)hWnd
, (LPARAM
)CompareFunc
);
392 case LVN_ENDLABELEDIT
: {
393 LPNMLVDISPINFO dispInfo
= (LPNMLVDISPINFO
)lParam
;
394 LPTSTR valueName
= get_item_text(hWnd
, dispInfo
->item
.iItem
);
396 if (!valueName
) return -1; /* cannot rename a default value */
397 ret
= RenameValue(hWnd
, g_currentRootKey
, g_currentPath
, valueName
, dispInfo
->item
.pszText
);
399 RefreshListView(hWnd
, g_currentRootKey
, g_currentPath
, dispInfo
->item
.pszText
);
400 HeapFree(GetProcessHeap(), 0, valueName
);
404 int cnt
= ListView_GetNextItem(hWnd
, -1, LVNI_FOCUSED
| LVNI_SELECTED
);
406 SendMessage(hFrameWnd
, WM_COMMAND
, ID_EDIT_MODIFY
, 0);
410 NMITEMACTIVATE
* nmitem
= (LPNMITEMACTIVATE
)lParam
;
413 /* if (nmitem->hdr.hwndFrom != hWnd) break; unnecessary because of WM_NOTIFY_REFLECT */
414 /* if (nmitem->hdr.idFrom != IDW_LISTVIEW) break; */
415 /* if (nmitem->hdr.code != ???) break; */
417 switch (nmitem
->uKeyFlags
) {
418 case LVKF_ALT
: /* The ALT key is pressed. */
419 /* properties dialog box ? */
421 case LVKF_CONTROL
: /* The CTRL key is pressed. */
422 /* run dialog box for providing parameters... */
424 case LVKF_SHIFT
: /* The SHIFT key is pressed. */
428 info
.pt
.x
= nmitem
->ptAction
.x
;
429 info
.pt
.y
= nmitem
->ptAction
.y
;
430 if (ListView_HitTest(hWnd
, &info
) != -1) {
431 ListView_SetItemState(hWnd
, -1, 0, LVIS_FOCUSED
|LVIS_SELECTED
);
432 ListView_SetItemState(hWnd
, info
.iItem
, LVIS_FOCUSED
|LVIS_SELECTED
,
433 LVIS_FOCUSED
|LVIS_SELECTED
);
434 SendMessage(hFrameWnd
, WM_COMMAND
, ID_EDIT_MODIFY
, 0);
440 return 0; /* shouldn't call default ! */
443 case WM_CONTEXTMENU
: {
444 int cnt
= ListView_GetNextItem(hWnd
, -1, LVNI_SELECTED
);
445 TrackPopupMenu(GetSubMenu(hPopupMenus
, cnt
== -1 ? PM_NEW
: PM_MODIFYVALUE
),
446 TPM_RIGHTBUTTON
, (short)LOWORD(lParam
), (short)HIWORD(lParam
),
451 return CallWindowProc(g_orgListWndProc
, hWnd
, message
, wParam
, lParam
);
457 HWND
CreateListView(HWND hwndParent
, UINT id
)
462 /* prepare strings */
463 LoadString(hInst
, IDS_REGISTRY_VALUE_NOT_SET
, g_szValueNotSet
, COUNT_OF(g_szValueNotSet
));
465 /* Get the dimensions of the parent window's client area, and create the list view control. */
466 GetClientRect(hwndParent
, &rcClient
);
467 hwndLV
= CreateWindowEx(WS_EX_CLIENTEDGE
, WC_LISTVIEW
, _T("List View"),
468 WS_VISIBLE
| WS_CHILD
| WS_TABSTOP
| LVS_REPORT
| LVS_EDITLABELS
,
469 0, 0, rcClient
.right
, rcClient
.bottom
,
470 hwndParent
, (HMENU
)ULongToHandle(id
), hInst
, NULL
);
471 if (!hwndLV
) return NULL
;
472 SendMessage(hwndLV
, LVM_SETEXTENDEDLISTVIEWSTYLE
, 0, LVS_EX_FULLROWSELECT
);
474 /* Initialize the image list */
475 if (!InitListViewImageList(hwndLV
)) goto fail
;
476 if (!CreateListColumns(hwndLV
)) goto fail
;
477 g_orgListWndProc
= (WNDPROC
) SetWindowLongPtr(hwndLV
, GWLP_WNDPROC
, (LPARAM
)ListWndProc
);
480 DestroyWindow(hwndLV
);
484 BOOL
RefreshListView(HWND hwndLV
, HKEY hKeyRoot
, LPCTSTR keyPath
, LPCTSTR highlightValue
)
487 DWORD max_sub_key_len
;
488 DWORD max_val_name_len
, valNameLen
;
489 DWORD max_val_size
, valSize
;
490 DWORD val_count
, index
, valType
;
498 if (!hwndLV
) return FALSE
;
500 SendMessage(hwndLV
, WM_SETREDRAW
, FALSE
, 0);
502 errCode
= RegOpenKeyEx(hKeyRoot
, keyPath
, 0, KEY_READ
, &hKey
);
503 if (errCode
!= ERROR_SUCCESS
) goto done
;
505 count
= ListView_GetItemCount(hwndLV
);
506 for (i
= 0; i
< count
; i
++) {
507 item
.mask
= LVIF_PARAM
;
509 SendMessage( hwndLV
, LVM_GETITEM
, 0, (LPARAM
)&item
);
510 free(((LINE_INFO
*)item
.lParam
)->name
);
511 HeapFree(GetProcessHeap(), 0, (void*)item
.lParam
);
513 g_columnToSort
= ~0U;
514 SendMessage( hwndLV
, LVM_DELETEALLITEMS
, 0, 0L );
516 /* get size information and resize the buffers if necessary */
517 errCode
= RegQueryInfoKey(hKey
, NULL
, NULL
, NULL
, NULL
, &max_sub_key_len
, NULL
,
518 &val_count
, &max_val_name_len
, &max_val_size
, NULL
, NULL
);
519 if (errCode
!= ERROR_SUCCESS
) goto done
;
521 /* account for the terminator char */
525 valName
= HeapAlloc(GetProcessHeap(), 0, max_val_name_len
* sizeof(TCHAR
));
526 valBuf
= HeapAlloc(GetProcessHeap(), 0, max_val_size
);
527 if (RegQueryValueEx(hKey
, NULL
, NULL
, &valType
, valBuf
, &valSize
) == ERROR_FILE_NOT_FOUND
) {
528 AddEntryToList(hwndLV
, NULL
, REG_SZ
, NULL
, 0, !highlightValue
);
530 for(index
= 0; index
< val_count
; index
++) {
531 BOOL bSelected
= (valName
== highlightValue
); /* NOT a bug, we check for double NULL here */
532 valNameLen
= max_val_name_len
;
533 valSize
= max_val_size
;
535 errCode
= RegEnumValue(hKey
, index
, valName
, &valNameLen
, NULL
, &valType
, valBuf
, &valSize
);
536 if (errCode
!= ERROR_SUCCESS
) goto done
;
538 if (valName
&& highlightValue
&& !_tcscmp(valName
, highlightValue
))
540 AddEntryToList(hwndLV
, valName
[0] ? valName
: NULL
, valType
, valBuf
, valSize
, bSelected
);
542 SendMessage(hwndLV
, LVM_SORTITEMS
, (WPARAM
)hwndLV
, (LPARAM
)CompareFunc
);
544 g_currentRootKey
= hKeyRoot
;
545 if (keyPath
!= g_currentPath
) {
546 HeapFree(GetProcessHeap(), 0, g_currentPath
);
547 g_currentPath
= HeapAlloc(GetProcessHeap(), 0, (lstrlen(keyPath
) + 1) * sizeof(TCHAR
));
548 if (!g_currentPath
) goto done
;
549 lstrcpy(g_currentPath
, keyPath
);
555 HeapFree(GetProcessHeap(), 0, valBuf
);
556 HeapFree(GetProcessHeap(), 0, valName
);
557 SendMessage(hwndLV
, WM_SETREDRAW
, TRUE
, 0);
558 if (hKey
) RegCloseKey(hKey
);