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
30 #include "wine/debug.h"
31 #include "wine/unicode.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(regedit
);
35 static INT Image_String
;
36 static INT Image_Binary
;
38 typedef struct tagLINE_INFO
46 /*******************************************************************************
47 * Global and Local Variables:
50 static WNDPROC g_orgListWndProc
;
51 static DWORD g_columnToSort
= ~0UL;
52 static BOOL g_invertSort
= FALSE
;
53 static LPTSTR g_valueName
;
54 static LPTSTR g_currentPath
;
55 static HKEY g_currentRootKey
;
57 #define MAX_LIST_COLUMNS (IDS_LIST_COLUMN_LAST - IDS_LIST_COLUMN_FIRST + 1)
58 static int default_column_widths
[MAX_LIST_COLUMNS
] = { 200, 175, 400 };
59 static int column_alignment
[MAX_LIST_COLUMNS
] = { LVCFMT_LEFT
, LVCFMT_LEFT
, LVCFMT_LEFT
};
61 static LPTSTR
get_item_text(HWND hwndLV
, int item
)
63 LPTSTR newStr
, curStr
;
64 unsigned int maxLen
= 128;
66 curStr
= HeapAlloc(GetProcessHeap(), 0, maxLen
);
67 if (!curStr
) return NULL
;
68 if (item
== 0) return NULL
; /* first item is ALWAYS a default */
70 ListView_GetItemText(hwndLV
, item
, 0, curStr
, maxLen
);
71 if (_tcslen(curStr
) < maxLen
- 1) return curStr
;
72 newStr
= HeapReAlloc(GetProcessHeap(), 0, curStr
, maxLen
* 2);
77 HeapFree(GetProcessHeap(), 0, curStr
);
81 LPCTSTR
GetValueName(HWND hwndLV
)
85 if (g_valueName
&& g_valueName
!= LPSTR_TEXTCALLBACK
)
86 HeapFree(GetProcessHeap(), 0, g_valueName
);
89 item
= ListView_GetNextItem(hwndLV
, -1, LVNI_FOCUSED
);
90 if (item
== -1) return NULL
;
92 g_valueName
= get_item_text(hwndLV
, item
);
97 /* convert '\0' separated string list into ',' separated string list */
98 static void MakeMULTISZDisplayable(LPTSTR multi
)
102 for (; *multi
; multi
++)
112 /*******************************************************************************
113 * Local module support methods
115 static void AddEntryToList(HWND hwndLV
, LPTSTR Name
, DWORD dwValType
,
116 void* ValBuf
, DWORD dwCount
, BOOL bHighlight
)
122 linfo
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(LINE_INFO
) + dwCount
);
123 linfo
->dwValType
= dwValType
;
124 linfo
->val_len
= dwCount
;
125 memcpy(&linfo
[1], ValBuf
, dwCount
);
128 linfo
->name
= _tcsdup(Name
);
132 item
.mask
= LVIF_TEXT
| LVIF_PARAM
| LVIF_STATE
| LVIF_IMAGE
;
133 item
.iItem
= ListView_GetItemCount(hwndLV
);/*idx; */
136 item
.stateMask
= LVIS_FOCUSED
| LVIS_SELECTED
;
137 item
.pszText
= Name
? Name
: LPSTR_TEXTCALLBACK
;
138 item
.cchTextMax
= Name
? _tcslen(item
.pszText
) : 0;
140 item
.stateMask
= item
.state
= LVIS_FOCUSED
| LVIS_SELECTED
;
147 item
.iImage
= Image_String
;
150 item
.iImage
= Image_Binary
;
153 item
.lParam
= (LPARAM
)linfo
;
155 #if (_WIN32_IE >= 0x0300)
159 index
= ListView_InsertItem(hwndLV
, &item
);
161 /* LPTSTR pszText = NULL; */
162 LPTSTR pszText
= _T("(cannot display value)");
167 ListView_SetItemText(hwndLV
, index
, 2, ValBuf
);
169 ListView_SetItemText(hwndLV
, index
, 2, "(not set)");
174 wsprintf(buf
, _T("0x%08X (%d)"), *(DWORD
*)ValBuf
, *(DWORD
*)ValBuf
);
175 ListView_SetItemText(hwndLV
, index
, 2, buf
);
177 /* lpsRes = convertHexToDWORDStr(lpbData, dwLen); */
181 LPBYTE pData
= (LPBYTE
)ValBuf
;
182 LPTSTR strBinary
= HeapAlloc(GetProcessHeap(), 0, dwCount
* sizeof(TCHAR
) * 3 + 1);
183 for (i
= 0; i
< dwCount
; i
++)
184 wsprintf( strBinary
+ i
*3, _T("%02X "), pData
[i
] );
185 strBinary
[dwCount
* 3] = 0;
186 ListView_SetItemText(hwndLV
, index
, 2, strBinary
);
187 HeapFree(GetProcessHeap(), 0, strBinary
);
191 MakeMULTISZDisplayable(ValBuf
);
192 ListView_SetItemText(hwndLV
, index
, 2, ValBuf
);
195 /* lpsRes = convertHexToHexCSV(lpbData, dwLen); */
196 ListView_SetItemText(hwndLV
, index
, 2, pszText
);
202 static BOOL
InitListViewImageList(HWND hWndListView
)
206 INT cx
= GetSystemMetrics(SM_CXSMICON
);
207 INT cy
= GetSystemMetrics(SM_CYSMICON
);
209 himl
= ImageList_Create(cx
, cy
, ILC_MASK
, 0, 2);
213 hicon
= LoadImage(hInst
, MAKEINTRESOURCE(IDI_STRING
),
214 IMAGE_ICON
, cx
, cy
, LR_DEFAULTCOLOR
);
215 Image_String
= ImageList_AddIcon(himl
, hicon
);
217 hicon
= LoadImage(hInst
, MAKEINTRESOURCE(IDI_BIN
),
218 IMAGE_ICON
, cx
, cy
, LR_DEFAULTCOLOR
);
219 Image_Binary
= ImageList_AddIcon(himl
, hicon
);
221 ListView_SetImageList(hWndListView
, himl
, LVSIL_SMALL
);
223 /* fail if some of the icons failed to load */
224 if (ImageList_GetImageCount(himl
) < 2)
230 static BOOL
CreateListColumns(HWND hWndListView
)
236 /* Create columns. */
237 lvC
.mask
= LVCF_FMT
| LVCF_WIDTH
| LVCF_TEXT
| LVCF_SUBITEM
;
238 lvC
.pszText
= szText
;
240 /* Load the column labels from the resource file. */
241 for (index
= 0; index
< MAX_LIST_COLUMNS
; index
++) {
242 lvC
.iSubItem
= index
;
243 lvC
.cx
= default_column_widths
[index
];
244 lvC
.fmt
= column_alignment
[index
];
245 LoadString(hInst
, IDS_LIST_COLUMN_FIRST
+ index
, szText
, sizeof(szText
)/sizeof(TCHAR
));
246 if (ListView_InsertColumn(hWndListView
, index
, &lvC
) == -1) return FALSE
;
251 /* OnGetDispInfo - processes the LVN_GETDISPINFO notification message. */
253 static void OnGetDispInfo(NMLVDISPINFO
* plvdi
)
255 static TCHAR buffer
[200];
257 plvdi
->item
.pszText
= NULL
;
258 plvdi
->item
.cchTextMax
= 0;
260 switch (plvdi
->item
.iSubItem
) {
262 plvdi
->item
.pszText
= (LPSTR
)g_pszDefaultValueName
;
265 switch (((LINE_INFO
*)plvdi
->item
.lParam
)->dwValType
) {
267 plvdi
->item
.pszText
= _T("REG_SZ");
270 plvdi
->item
.pszText
= _T("REG_EXPAND_SZ");
273 plvdi
->item
.pszText
= _T("REG_BINARY");
276 plvdi
->item
.pszText
= _T("REG_DWORD");
278 case REG_DWORD_BIG_ENDIAN
:
279 plvdi
->item
.pszText
= _T("REG_DWORD_BIG_ENDIAN");
282 plvdi
->item
.pszText
= _T("REG_MULTI_SZ");
285 plvdi
->item
.pszText
= _T("REG_LINK");
287 case REG_RESOURCE_LIST
:
288 plvdi
->item
.pszText
= _T("REG_RESOURCE_LIST");
291 plvdi
->item
.pszText
= _T("REG_NONE");
294 wsprintf(buffer
, _T("unknown(%d)"), plvdi
->item
.lParam
);
295 plvdi
->item
.pszText
= buffer
;
300 plvdi
->item
.pszText
= _T("(value not set)");
303 plvdi
->item
.pszText
= _T("");
308 static int CALLBACK
CompareFunc(LPARAM lParam1
, LPARAM lParam2
, LPARAM lParamSort
)
311 l
= (LINE_INFO
*)lParam1
;
312 r
= (LINE_INFO
*)lParam2
;
313 if (!l
->name
) return -1;
314 if (!r
->name
) return +1;
316 if (g_columnToSort
== ~0UL)
319 if (g_columnToSort
== 1 && l
->dwValType
!= r
->dwValType
)
320 return g_invertSort
? (int)r
->dwValType
- (int)l
->dwValType
: (int)l
->dwValType
- (int)r
->dwValType
;
321 if (g_columnToSort
== 2) {
322 /* FIXME: Sort on value */
324 return g_invertSort
? _tcscmp(r
->name
, l
->name
) : _tcscmp(l
->name
, r
->name
);
327 HWND
StartValueRename(HWND hwndLV
)
331 item
= ListView_GetNextItem(hwndLV
, -1, LVNI_FOCUSED
| LVNI_SELECTED
);
332 if (item
< 1) { /* cannot rename default key */
333 MessageBeep(MB_ICONHAND
);
336 return ListView_EditLabel(hwndLV
, item
);
339 static BOOL
_CmdWndProc(HWND hWnd
, UINT message
, WPARAM wParam
, LPARAM lParam
)
341 switch (LOWORD(wParam
)) {
342 /* case ID_FILE_OPEN: */
350 static LRESULT CALLBACK
ListWndProc(HWND hWnd
, UINT message
, WPARAM wParam
, LPARAM lParam
)
354 if (!_CmdWndProc(hWnd
, message
, wParam
, lParam
)) {
355 return CallWindowProc(g_orgListWndProc
, hWnd
, message
, wParam
, lParam
);
358 case WM_NOTIFY_REFLECT
:
359 switch (((LPNMHDR
)lParam
)->code
) {
361 case LVN_BEGINLABELEDIT
:
362 if (!((NMLVDISPINFO
*)lParam
)->item
.iItem
)
365 case LVN_GETDISPINFO
:
366 OnGetDispInfo((NMLVDISPINFO
*)lParam
);
368 case LVN_COLUMNCLICK
:
369 if (g_columnToSort
== ((LPNMLISTVIEW
)lParam
)->iSubItem
)
370 g_invertSort
= !g_invertSort
;
372 g_columnToSort
= ((LPNMLISTVIEW
)lParam
)->iSubItem
;
373 g_invertSort
= FALSE
;
376 ListView_SortItems(hWnd
, CompareFunc
, hWnd
);
378 case LVN_ENDLABELEDIT
: {
379 LPNMLVDISPINFO dispInfo
= (LPNMLVDISPINFO
)lParam
;
380 LPTSTR valueName
= get_item_text(hWnd
, dispInfo
->item
.iItem
);
382 if (!valueName
) return -1; /* cannot rename a default value */
383 ret
= RenameValue(hWnd
, g_currentRootKey
, g_currentPath
, valueName
, dispInfo
->item
.pszText
);
385 RefreshListView(hWnd
, g_currentRootKey
, g_currentPath
, dispInfo
->item
.pszText
);
386 HeapFree(GetProcessHeap(), 0, valueName
);
390 int cnt
= ListView_GetNextItem(hWnd
, -1, LVNI_FOCUSED
| LVNI_SELECTED
);
392 SendMessage(hFrameWnd
, WM_COMMAND
, ID_EDIT_MODIFY
, 0);
396 NMITEMACTIVATE
* nmitem
= (LPNMITEMACTIVATE
)lParam
;
399 /* if (nmitem->hdr.hwndFrom != hWnd) break; unnecessary because of WM_NOTIFY_REFLECT */
400 /* if (nmitem->hdr.idFrom != IDW_LISTVIEW) break; */
401 /* if (nmitem->hdr.code != ???) break; */
403 switch (nmitem
->uKeyFlags
) {
404 case LVKF_ALT
: /* The ALT key is pressed. */
405 /* properties dialog box ? */
407 case LVKF_CONTROL
: /* The CTRL key is pressed. */
408 /* run dialog box for providing parameters... */
410 case LVKF_SHIFT
: /* The SHIFT key is pressed. */
414 info
.pt
.x
= nmitem
->ptAction
.x
;
415 info
.pt
.y
= nmitem
->ptAction
.y
;
416 if (ListView_HitTest(hWnd
, &info
) != -1) {
417 ListView_SetItemState(hWnd
, -1, 0, LVIS_FOCUSED
|LVIS_SELECTED
);
418 ListView_SetItemState(hWnd
, info
.iItem
, LVIS_FOCUSED
|LVIS_SELECTED
,
419 LVIS_FOCUSED
|LVIS_SELECTED
);
420 SendMessage(hFrameWnd
, WM_COMMAND
, ID_EDIT_MODIFY
, 0);
426 return 0; /* shouldn't call default ! */
429 case WM_CONTEXTMENU
: {
430 int cnt
= ListView_GetNextItem(hWnd
, -1, LVNI_SELECTED
);
431 TrackPopupMenu(GetSubMenu(hPopupMenus
, cnt
== -1 ? PM_NEW
: PM_MODIFYVALUE
),
432 TPM_RIGHTBUTTON
, (short)LOWORD(lParam
), (short)HIWORD(lParam
),
437 return CallWindowProc(g_orgListWndProc
, hWnd
, message
, wParam
, lParam
);
444 HWND
CreateListView(HWND hwndParent
, int id
)
449 /* Get the dimensions of the parent window's client area, and create the list view control. */
450 GetClientRect(hwndParent
, &rcClient
);
451 hwndLV
= CreateWindowEx(WS_EX_CLIENTEDGE
, WC_LISTVIEW
, _T("List View"),
452 WS_VISIBLE
| WS_CHILD
| WS_TABSTOP
| LVS_REPORT
| LVS_EDITLABELS
,
453 0, 0, rcClient
.right
, rcClient
.bottom
,
454 hwndParent
, (HMENU
)id
, hInst
, NULL
);
455 if (!hwndLV
) return NULL
;
456 ListView_SetExtendedListViewStyle(hwndLV
, LVS_EX_FULLROWSELECT
);
458 /* Initialize the image list */
459 if (!InitListViewImageList(hwndLV
)) goto fail
;
460 if (!CreateListColumns(hwndLV
)) goto fail
;
461 g_orgListWndProc
= SubclassWindow(hwndLV
, ListWndProc
);
464 DestroyWindow(hwndLV
);
468 BOOL
RefreshListView(HWND hwndLV
, HKEY hKeyRoot
, LPCTSTR keyPath
, LPCTSTR highlightValue
)
471 DWORD max_sub_key_len
;
472 DWORD max_val_name_len
, valNameLen
;
473 DWORD max_val_size
, valSize
;
474 DWORD val_count
, index
, valType
;
482 if (!hwndLV
) return FALSE
;
484 SendMessage(hwndLV
, WM_SETREDRAW
, FALSE
, 0);
486 errCode
= RegOpenKeyEx(hKeyRoot
, keyPath
, 0, KEY_READ
, &hKey
);
487 if (errCode
!= ERROR_SUCCESS
) goto done
;
489 count
= ListView_GetItemCount(hwndLV
);
490 for (i
= 0; i
< count
; i
++) {
491 item
.mask
= LVIF_PARAM
;
493 ListView_GetItem(hwndLV
, &item
);
494 free(((LINE_INFO
*)item
.lParam
)->name
);
495 HeapFree(GetProcessHeap(), 0, (void*)item
.lParam
);
497 g_columnToSort
= ~0UL;
498 ListView_DeleteAllItems(hwndLV
);
500 /* get size information and resize the buffers if necessary */
501 errCode
= RegQueryInfoKey(hKey
, NULL
, NULL
, NULL
, NULL
, &max_sub_key_len
, NULL
,
502 &val_count
, &max_val_name_len
, &max_val_size
, NULL
, NULL
);
503 if (errCode
!= ERROR_SUCCESS
) goto done
;
505 /* account for the terminator char */
509 valName
= HeapAlloc(GetProcessHeap(), 0, max_val_name_len
* sizeof(TCHAR
));
510 valBuf
= HeapAlloc(GetProcessHeap(), 0, max_val_size
);
511 if (RegQueryValueEx(hKey
, NULL
, NULL
, &valType
, valBuf
, &valSize
) == ERROR_FILE_NOT_FOUND
) {
512 AddEntryToList(hwndLV
, NULL
, REG_SZ
, NULL
, 0, !highlightValue
);
514 /*dwValSize = max_val_size; */
515 for(index
= 0; index
< val_count
; index
++) {
516 BOOL bSelected
= (valName
== highlightValue
); /* NOT a bug, we check for double NULL here */
517 valNameLen
= max_val_name_len
;
518 valSize
= max_val_size
;
520 errCode
= RegEnumValue(hKey
, index
, valName
, &valNameLen
, NULL
, &valType
, valBuf
, &valSize
);
521 if (errCode
!= ERROR_SUCCESS
) goto done
;
523 if (valName
&& highlightValue
&& !_tcscmp(valName
, highlightValue
))
525 AddEntryToList(hwndLV
, valName
[0] ? valName
: NULL
, valType
, valBuf
, valSize
, bSelected
);
527 ListView_SortItems(hwndLV
, CompareFunc
, hwndLV
);
529 g_currentRootKey
= hKeyRoot
;
530 if (keyPath
!= g_currentPath
) {
531 HeapFree(GetProcessHeap(), 0, g_currentPath
);
532 g_currentPath
= HeapAlloc(GetProcessHeap(), 0, (lstrlen(keyPath
) + 1) * sizeof(TCHAR
));
533 if (!g_currentPath
) goto done
;
534 lstrcpy(g_currentPath
, keyPath
);
540 HeapFree(GetProcessHeap(), 0, valBuf
);
541 HeapFree(GetProcessHeap(), 0, valName
);
542 SendMessage(hwndLV
, WM_SETREDRAW
, TRUE
, 0);
543 if (hKey
) RegCloseKey(hKey
);