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 Street, Fifth Floor, Boston, MA 02110-1301 USA
31 typedef struct tagLINE_INFO
37 } LINE_INFO
, *PLINE_INFO
;
39 /*******************************************************************************
40 * Global and Local Variables:
43 static DWORD g_columnToSort
= ~0UL;
44 static BOOL g_invertSort
= FALSE
;
46 #define MAX_LIST_COLUMNS (IDS_LIST_COLUMN_LAST - IDS_LIST_COLUMN_FIRST + 1)
47 static const int default_column_widths
[MAX_LIST_COLUMNS
] = { 200, 175, 400 };
48 static const int column_alignment
[MAX_LIST_COLUMNS
] = { LVCFMT_LEFT
, LVCFMT_LEFT
, LVCFMT_LEFT
};
50 LPCTSTR
GetValueName(HWND hwndLV
, int iStartAt
)
57 if a new item is inserted, then no allocation,
58 otherwise the heap block will be lost!
60 item
= ListView_GetNextItem(hwndLV
, iStartAt
, LVNI_SELECTED
);
61 if (item
== -1) return NULL
;
64 Should be always TRUE anyways
68 LVItem
.mask
= LVIF_PARAM
;
69 if (ListView_GetItem(hwndLV
, &LVItem
) == FALSE
)
72 lineinfo
= (PLINE_INFO
)LVItem
.lParam
;
76 return lineinfo
->name
;
79 BOOL
IsDefaultValue(HWND hwndLV
, int i
)
84 Item
.mask
= LVIF_PARAM
;
86 if(ListView_GetItem(hwndLV
, &Item
))
88 lineinfo
= (PLINE_INFO
)Item
.lParam
;
89 return lineinfo
&& (!lineinfo
->name
|| !_tcscmp(lineinfo
->name
, _T("")));
94 /*******************************************************************************
95 * Local module support methods
97 static void AddEntryToList(HWND hwndLV
, LPTSTR Name
, DWORD dwValType
, void* ValBuf
, DWORD dwCount
, int Position
, BOOL ValExists
)
103 linfo
= (PLINE_INFO
)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(LINE_INFO
) + dwCount
);
104 linfo
->dwValType
= dwValType
;
105 linfo
->val_len
= dwCount
;
108 memcpy(&linfo
[1], ValBuf
, dwCount
);
110 linfo
->name
= _tcsdup(Name
);
112 item
.mask
= LVIF_TEXT
| LVIF_PARAM
| LVIF_IMAGE
;
113 item
.iItem
= (Position
== -1 ? 0: Position
);
118 item
.cchTextMax
= (int) _tcslen(item
.pszText
);
119 if (item
.cchTextMax
== 0)
120 item
.pszText
= LPSTR_TEXTCALLBACK
;
122 item
.lParam
= (LPARAM
)linfo
;
128 item
.iImage
= Image_String
;
131 item
.iImage
= Image_Bin
;
135 /* item.lParam = (LPARAM)ValBuf; */
136 #if (_WIN32_IE >= 0x0300)
140 index
= ListView_InsertItem(hwndLV
, &item
);
147 ListView_SetItemText(hwndLV
, index
, 2, ValBuf
);
152 /* load (value not set) string */
153 LoadString(hInst
, IDS_VALUE_NOT_SET
, buffer
, sizeof(buffer
)/sizeof(TCHAR
));
154 ListView_SetItemText(hwndLV
, index
, 2, buffer
);
162 src
= (LPTSTR
)ValBuf
;
163 str
= HeapAlloc(GetProcessHeap(), 0, dwCount
);
167 /* concatenate all srings */
168 while(*src
!= _T('\0'))
171 _tcscat(str
, _T(" "));
172 src
+= _tcslen(src
) + 1;
174 ListView_SetItemText(hwndLV
, index
, 2, str
);
175 HeapFree(GetProcessHeap(), 0, str
);
178 ListView_SetItemText(hwndLV
, index
, 2, _T(""));
181 ListView_SetItemText(hwndLV
, index
, 2, _T(""));
186 if(dwCount
== sizeof(DWORD
))
188 wsprintf(buf
, _T("0x%08x (%u)"), *(DWORD
*)ValBuf
, *(DWORD
*)ValBuf
);
192 LoadString(hInst
, IDS_INVALID_DWORD
, buf
, sizeof(buf
)/sizeof(TCHAR
));
194 ListView_SetItemText(hwndLV
, index
, 2, buf
);
196 /* lpsRes = convertHexToDWORDStr(lpbData, dwLen); */
201 LPBYTE pData
= (LPBYTE
)ValBuf
;
205 strBinary
= HeapAlloc(GetProcessHeap(), 0, (dwCount
* sizeof(TCHAR
) * 3) + sizeof(TCHAR
));
206 for (i
= 0; i
< dwCount
; i
++)
208 wsprintf( strBinary
+ i
*3, _T("%02X "), pData
[i
] );
210 strBinary
[dwCount
* 3] = 0;
211 ListView_SetItemText(hwndLV
, index
, 2, strBinary
);
212 HeapFree(GetProcessHeap(), 0, strBinary
);
217 LoadString(hInst
, IDS_BINARY_EMPTY
, szText
, sizeof(szText
)/sizeof(TCHAR
));
218 ListView_SetItemText(hwndLV
, index
, 2, szText
);
226 static BOOL
CreateListColumns(HWND hWndListView
)
232 /* Create columns. */
233 lvC
.mask
= LVCF_FMT
| LVCF_WIDTH
| LVCF_TEXT
| LVCF_SUBITEM
;
234 lvC
.pszText
= szText
;
236 /* Load the column labels from the resource file. */
237 for (index
= 0; index
< MAX_LIST_COLUMNS
; index
++) {
238 lvC
.iSubItem
= index
;
239 lvC
.cx
= default_column_widths
[index
];
240 lvC
.fmt
= column_alignment
[index
];
241 LoadString(hInst
, IDS_LIST_COLUMN_FIRST
+ index
, szText
, sizeof(szText
)/sizeof(TCHAR
));
242 if (ListView_InsertColumn(hWndListView
, index
, &lvC
) == -1) return FALSE
;
247 static BOOL
InitListViewImageLists(HWND hwndLV
)
249 HIMAGELIST himl
; /* handle to image list */
250 HICON hico
; /* handle to icon */
252 /* Create the image list. */
253 if ((himl
= ImageList_Create(CX_ICON
, CY_ICON
,
254 ILC_MASK
, 0, NUM_ICONS
)) == NULL
)
257 hico
= LoadIcon(hInst
, MAKEINTRESOURCE(IDI_BIN
));
258 Image_Bin
= ImageList_AddIcon(himl
, hico
);
260 hico
= LoadIcon(hInst
, MAKEINTRESOURCE(IDI_STRING
));
261 Image_String
= ImageList_AddIcon(himl
, hico
);
264 /* Fail if not all of the images were added. */
265 if (ImageList_GetImageCount(himl
) < NUM_ICONS
)
270 /* Associate the image list with the tree view control. */
271 (void)ListView_SetImageList(hwndLV
, himl
, LVSIL_SMALL
);
276 /* OnGetDispInfo - processes the LVN_GETDISPINFO notification message. */
278 static void OnGetDispInfo(NMLVDISPINFO
* plvdi
)
280 static TCHAR buffer
[200];
282 plvdi
->item
.pszText
= NULL
;
283 plvdi
->item
.cchTextMax
= 0;
285 switch (plvdi
->item
.iSubItem
) {
287 LoadString(hInst
, IDS_DEFAULT_VALUE_NAME
, buffer
, sizeof(buffer
)/sizeof(TCHAR
));
288 plvdi
->item
.pszText
= buffer
;
291 switch (((LINE_INFO
*)plvdi
->item
.lParam
)->dwValType
) {
293 plvdi
->item
.pszText
= _T("REG_NONE");
296 plvdi
->item
.pszText
= _T("REG_SZ");
299 plvdi
->item
.pszText
= _T("REG_EXPAND_SZ");
302 plvdi
->item
.pszText
= _T("REG_BINARY");
304 case REG_DWORD
: /* REG_DWORD_LITTLE_ENDIAN */
305 plvdi
->item
.pszText
= _T("REG_DWORD");
307 case REG_DWORD_BIG_ENDIAN
:
308 plvdi
->item
.pszText
= _T("REG_DWORD_BIG_ENDIAN");
311 plvdi
->item
.pszText
= _T("REG_LINK");
314 plvdi
->item
.pszText
= _T("REG_MULTI_SZ");
316 case REG_RESOURCE_LIST
:
317 plvdi
->item
.pszText
= _T("REG_RESOURCE_LIST");
319 case REG_FULL_RESOURCE_DESCRIPTOR
:
320 plvdi
->item
.pszText
= _T("REG_FULL_RESOURCE_DESCRIPTOR");
322 case REG_RESOURCE_REQUIREMENTS_LIST
:
323 plvdi
->item
.pszText
= _T("REG_RESOURCE_REQUIREMENTS_LIST");
325 case REG_QWORD
: /* REG_QWORD_LITTLE_ENDIAN */
326 plvdi
->item
.pszText
= _T("REG_QWORD");
330 LoadString(hInst
, IDS_UNKNOWN_TYPE
, buf2
, sizeof(buf2
)/sizeof(TCHAR
));
331 wsprintf(buffer
, buf2
, ((LINE_INFO
*)plvdi
->item
.lParam
)->dwValType
);
332 plvdi
->item
.pszText
= buffer
;
338 plvdi
->item
.pszText
= _T("");
343 static int CALLBACK
CompareFunc(LPARAM lParam1
, LPARAM lParam2
, LPARAM lParamSort
)
346 UNREFERENCED_PARAMETER(lParamSort
);
348 l
= (LINE_INFO
*)lParam1
;
349 r
= (LINE_INFO
*)lParam2
;
351 if (g_columnToSort
== ~0UL)
354 if (g_columnToSort
== 1 && l
->dwValType
!= r
->dwValType
)
355 return g_invertSort
? (int)r
->dwValType
- (int)l
->dwValType
: (int)l
->dwValType
- (int)r
->dwValType
;
356 if (g_columnToSort
== 2) {
357 /* FIXME: Sort on value */
359 return g_invertSort
? _tcscmp(r
->name
, l
->name
) : _tcscmp(l
->name
, r
->name
);
362 BOOL
ListWndNotifyProc(HWND hWnd
, WPARAM wParam
, LPARAM lParam
, BOOL
*Result
)
365 UNREFERENCED_PARAMETER(wParam
);
367 switch (((LPNMHDR
)lParam
)->code
) {
368 case LVN_GETDISPINFO
:
369 OnGetDispInfo((NMLVDISPINFO
*)lParam
);
371 case LVN_COLUMNCLICK
:
372 if (g_columnToSort
== (DWORD
)((LPNMLISTVIEW
)lParam
)->iSubItem
)
373 g_invertSort
= !g_invertSort
;
375 g_columnToSort
= ((LPNMLISTVIEW
)lParam
)->iSubItem
;
376 g_invertSort
= FALSE
;
379 (void)ListView_SortItems(hWnd
, CompareFunc
, (WPARAM
)hWnd
);
384 SendMessage(hFrameWnd
, WM_COMMAND
, MAKEWPARAM(ID_EDIT_MODIFY
, 0), 0);
388 g_pChildWnd
->nFocusPanel
= 0;
390 case LVN_BEGINLABELEDIT
:
391 Info
= (NMLVDISPINFO
*)lParam
;
394 PLINE_INFO lineinfo
= (PLINE_INFO
)Info
->item
.lParam
;
395 if(!lineinfo
->name
|| !_tcscmp(lineinfo
->name
, _T("")))
407 case LVN_ENDLABELEDIT
:
408 Info
= (NMLVDISPINFO
*)lParam
;
409 if(Info
&& Info
->item
.pszText
)
411 PLINE_INFO lineinfo
= (PLINE_INFO
)Info
->item
.lParam
;
412 if(!lineinfo
->name
|| !_tcscmp(lineinfo
->name
, _T("")))
418 if(_tcslen(Info
->item
.pszText
) == 0)
420 TCHAR msg
[128], caption
[128];
422 LoadString(hInst
, IDS_ERR_RENVAL_TOEMPTY
, msg
, sizeof(msg
)/sizeof(TCHAR
));
423 LoadString(hInst
, IDS_ERR_RENVAL_CAPTION
, caption
, sizeof(caption
)/sizeof(TCHAR
));
424 MessageBox(0, msg
, caption
, 0);
433 keyPath
= GetItemPath(g_pChildWnd
->hTreeWnd
, 0, &hKeyRoot
);
434 lResult
= RegRenameValue(hKeyRoot
, keyPath
, Info
->item
.pszText
, lineinfo
->name
);
435 lineinfo
->name
= realloc(lineinfo
->name
, (_tcslen(Info
->item
.pszText
)+1)*sizeof(TCHAR
));
436 if (lineinfo
->name
!= NULL
)
437 _tcscpy(lineinfo
->name
, Info
->item
.pszText
);
440 return (lResult
== ERROR_SUCCESS
);
453 HWND
CreateListView(HWND hwndParent
, HMENU id
)
458 /* Get the dimensions of the parent window's client area, and create the list view control. */
459 GetClientRect(hwndParent
, &rcClient
);
460 hwndLV
= CreateWindowEx(WS_EX_CLIENTEDGE
, WC_LISTVIEW
, _T("List View"),
461 WS_VISIBLE
| WS_CHILD
| WS_TABSTOP
| LVS_REPORT
| LVS_EDITLABELS
,
462 0, 0, rcClient
.right
, rcClient
.bottom
,
463 hwndParent
, id
, hInst
, NULL
);
464 if (!hwndLV
) return NULL
;
466 /* Initialize the image list, and add items to the control. */
467 if (!CreateListColumns(hwndLV
)) goto fail
;
468 if (!InitListViewImageLists(hwndLV
)) goto fail
;
472 DestroyWindow(hwndLV
);
476 void DestroyListView(HWND hwndLV
)
481 count
= ListView_GetItemCount(hwndLV
);
482 for (i
= 0; i
< count
; i
++) {
483 item
.mask
= LVIF_PARAM
;
485 (void)ListView_GetItem(hwndLV
, &item
);
486 free(((LINE_INFO
*)item
.lParam
)->name
);
487 HeapFree(GetProcessHeap(), 0, (void*)item
.lParam
);
492 BOOL
RefreshListView(HWND hwndLV
, HKEY hKey
, LPCTSTR keyPath
)
494 DWORD max_sub_key_len
;
495 DWORD max_val_name_len
;
500 BOOL AddedDefault
= FALSE
;
502 if (!hwndLV
) return FALSE
;
504 (void)ListView_EditLabel(hwndLV
, -1);
506 SendMessage(hwndLV
, WM_SETREDRAW
, FALSE
, 0);
507 DestroyListView(hwndLV
);
509 g_columnToSort
= ~0UL;
510 (void)ListView_DeleteAllItems(hwndLV
);
512 if(!hKey
) return FALSE
;
514 errCode
= RegOpenKeyEx(hKey
, keyPath
, 0, KEY_READ
, &hNewKey
);
515 if (errCode
!= ERROR_SUCCESS
) return FALSE
;
517 /* get size information and resize the buffers if necessary */
518 errCode
= RegQueryInfoKey(hNewKey
, NULL
, NULL
, NULL
, NULL
, &max_sub_key_len
, NULL
,
519 &val_count
, &max_val_name_len
, &max_val_size
, NULL
, NULL
);
521 if (errCode
== ERROR_SUCCESS
) {
522 TCHAR
* ValName
= HeapAlloc(GetProcessHeap(), 0, ++max_val_name_len
* sizeof(TCHAR
));
523 DWORD dwValNameLen
= max_val_name_len
;
524 BYTE
* ValBuf
= HeapAlloc(GetProcessHeap(), 0, max_val_size
+ sizeof(TCHAR
));
525 DWORD dwValSize
= max_val_size
;
528 /* if (RegQueryValueEx(hNewKey, NULL, NULL, &dwValType, ValBuf, &dwValSize) == ERROR_SUCCESS) { */
529 /* AddEntryToList(hwndLV, _T("(Default)"), dwValType, ValBuf, dwValSize); */
531 /* dwValSize = max_val_size; */
532 while (RegEnumValue(hNewKey
, dwIndex
, ValName
, &dwValNameLen
, NULL
, &dwValType
, ValBuf
, &dwValSize
) == ERROR_SUCCESS
) {
533 /* Remove unwanted path from key name */
534 TCHAR
*pLastBl
= _tcsrchr(ValName
, TEXT('\\'));
535 if (pLastBl
!= NULL
) ++pLastBl
; else pLastBl
= ValName
;
536 /* Add a terminating 0 character. Usually this is only necessary for strings. */
537 ((TCHAR
*)ValBuf
)[dwValSize
/sizeof(TCHAR
)] = 0;
538 AddEntryToList(hwndLV
, pLastBl
, dwValType
, ValBuf
, dwValSize
, -1, TRUE
);
539 dwValNameLen
= max_val_name_len
;
540 dwValSize
= max_val_size
;
543 if(!_tcscmp(ValName
, _T("")))
548 HeapFree(GetProcessHeap(), 0, ValBuf
);
549 HeapFree(GetProcessHeap(), 0, ValName
);
553 AddEntryToList(hwndLV
, _T(""), REG_SZ
, NULL
, 0, 0, FALSE
);
555 (void)ListView_SortItems(hwndLV
, CompareFunc
, (WPARAM
)hwndLV
);
556 RegCloseKey(hNewKey
);
557 SendMessage(hwndLV
, WM_SETREDRAW
, TRUE
, 0);