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
30 INT iListViewSelect
= -1;
32 typedef struct tagLINE_INFO
38 } LINE_INFO
, *PLINE_INFO
;
40 /*******************************************************************************
41 * Global and Local Variables:
44 static DWORD g_columnToSort
= ~0UL;
45 static BOOL g_invertSort
= FALSE
;
47 #define MAX_LIST_COLUMNS (IDS_LIST_COLUMN_LAST - IDS_LIST_COLUMN_FIRST + 1)
48 static const int default_column_widths
[MAX_LIST_COLUMNS
] = { 200, 175, 400 };
49 static const int column_alignment
[MAX_LIST_COLUMNS
] = { LVCFMT_LEFT
, LVCFMT_LEFT
, LVCFMT_LEFT
};
51 LPCTSTR
GetValueName(HWND hwndLV
, int iStartAt
)
58 if a new item is inserted, then no allocation,
59 otherwise the heap block will be lost!
61 item
= ListView_GetNextItem(hwndLV
, iStartAt
, LVNI_SELECTED
);
62 if (item
== -1) return NULL
;
65 Should be always TRUE anyways
69 LVItem
.mask
= LVIF_PARAM
;
70 if (ListView_GetItem(hwndLV
, &LVItem
) == FALSE
)
73 lineinfo
= (PLINE_INFO
)LVItem
.lParam
;
77 return lineinfo
->name
;
80 VOID
SetValueName(HWND hwndLV
, LPCTSTR pszValueName
)
85 c
= ListView_GetItemCount(hwndLV
);
86 for(i
= 0; i
< c
; i
++)
88 ListView_SetItemState(hwndLV
, i
, 0, LVIS_FOCUSED
| LVIS_SELECTED
);
90 if (pszValueName
== NULL
)
94 fi
.flags
= LVFI_STRING
;
95 fi
.psz
= pszValueName
;
96 i
= ListView_FindItem(hwndLV
, -1, &fi
);
98 ListView_SetItemState(hwndLV
, i
, LVIS_FOCUSED
| LVIS_SELECTED
,
99 LVIS_FOCUSED
| LVIS_SELECTED
);
103 BOOL
IsDefaultValue(HWND hwndLV
, int i
)
108 Item
.mask
= LVIF_PARAM
;
110 if(ListView_GetItem(hwndLV
, &Item
))
112 lineinfo
= (PLINE_INFO
)Item
.lParam
;
113 return lineinfo
&& (!lineinfo
->name
|| !_tcscmp(lineinfo
->name
, _T("")));
118 /*******************************************************************************
119 * Local module support methods
121 static void AddEntryToList(HWND hwndLV
, LPTSTR Name
, DWORD dwValType
, void* ValBuf
, DWORD dwCount
, int Position
, BOOL ValExists
)
127 linfo
= (PLINE_INFO
)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(LINE_INFO
) + dwCount
);
128 linfo
->dwValType
= dwValType
;
129 linfo
->val_len
= dwCount
;
132 memcpy(&linfo
[1], ValBuf
, dwCount
);
134 linfo
->name
= _tcsdup(Name
);
136 item
.mask
= LVIF_TEXT
| LVIF_PARAM
| LVIF_IMAGE
;
137 item
.iItem
= (Position
== -1 ? 0: Position
);
142 item
.cchTextMax
= (int) _tcslen(item
.pszText
);
143 if (item
.cchTextMax
== 0)
144 item
.pszText
= LPSTR_TEXTCALLBACK
;
146 item
.lParam
= (LPARAM
)linfo
;
152 item
.iImage
= Image_String
;
155 item
.iImage
= Image_Bin
;
159 /* item.lParam = (LPARAM)ValBuf; */
160 #if (_WIN32_IE >= 0x0300)
164 index
= ListView_InsertItem(hwndLV
, &item
);
173 ListView_SetItemText(hwndLV
, index
, 2, ValBuf
);
178 /* load (value not set) string */
179 LoadString(hInst
, IDS_VALUE_NOT_SET
, buffer
, COUNT_OF(buffer
));
180 ListView_SetItemText(hwndLV
, index
, 2, buffer
);
188 src
= (LPTSTR
)ValBuf
;
189 str
= HeapAlloc(GetProcessHeap(), 0, dwCount
+ sizeof(TCHAR
));
193 /* concatenate all srings */
194 while(*src
!= _T('\0'))
197 _tcscat(str
, _T(" "));
198 src
+= _tcslen(src
) + 1;
200 ListView_SetItemText(hwndLV
, index
, 2, str
);
201 HeapFree(GetProcessHeap(), 0, str
);
204 ListView_SetItemText(hwndLV
, index
, 2, _T(""));
207 ListView_SetItemText(hwndLV
, index
, 2, _T(""));
213 if(dwCount
== sizeof(DWORD
))
215 wsprintf(buf
, _T("0x%08x (%u)"), *(DWORD
*)ValBuf
, *(DWORD
*)ValBuf
);
219 LoadString(hInst
, IDS_INVALID_DWORD
, buf
, COUNT_OF(buf
));
221 ListView_SetItemText(hwndLV
, index
, 2, buf
);
223 /* lpsRes = convertHexToDWORDStr(lpbData, dwLen); */
228 LPBYTE pData
= (LPBYTE
)ValBuf
;
232 strBinary
= HeapAlloc(GetProcessHeap(), 0, (dwCount
* sizeof(TCHAR
) * 3) + sizeof(TCHAR
));
233 for (i
= 0; i
< dwCount
; i
++)
235 wsprintf( strBinary
+ i
*3, _T("%02X "), pData
[i
] );
237 strBinary
[dwCount
* 3] = 0;
238 ListView_SetItemText(hwndLV
, index
, 2, strBinary
);
239 HeapFree(GetProcessHeap(), 0, strBinary
);
244 LoadString(hInst
, IDS_BINARY_EMPTY
, szText
, COUNT_OF(szText
));
245 ListView_SetItemText(hwndLV
, index
, 2, szText
);
253 static BOOL
CreateListColumns(HWND hWndListView
)
259 /* Create columns. */
260 lvC
.mask
= LVCF_FMT
| LVCF_WIDTH
| LVCF_TEXT
| LVCF_SUBITEM
;
261 lvC
.pszText
= szText
;
263 /* Load the column labels from the resource file. */
264 for (index
= 0; index
< MAX_LIST_COLUMNS
; index
++)
266 lvC
.iSubItem
= index
;
267 lvC
.cx
= default_column_widths
[index
];
268 lvC
.fmt
= column_alignment
[index
];
269 LoadString(hInst
, IDS_LIST_COLUMN_FIRST
+ index
, szText
, COUNT_OF(szText
));
270 if (ListView_InsertColumn(hWndListView
, index
, &lvC
) == -1) return FALSE
;
275 static BOOL
InitListViewImageLists(HWND hwndLV
)
277 HIMAGELIST himl
; /* handle to image list */
278 HICON hico
; /* handle to icon */
280 /* Create the image list. */
281 if ((himl
= ImageList_Create(CX_ICON
, CY_ICON
,
282 ILC_MASK
, 0, NUM_ICONS
)) == NULL
)
285 hico
= LoadIcon(hInst
, MAKEINTRESOURCE(IDI_BIN
));
286 Image_Bin
= ImageList_AddIcon(himl
, hico
);
288 hico
= LoadIcon(hInst
, MAKEINTRESOURCE(IDI_STRING
));
289 Image_String
= ImageList_AddIcon(himl
, hico
);
292 /* Fail if not all of the images were added. */
293 if (ImageList_GetImageCount(himl
) < NUM_ICONS
)
298 /* Associate the image list with the tree view control. */
299 (void)ListView_SetImageList(hwndLV
, himl
, LVSIL_SMALL
);
304 /* OnGetDispInfo - processes the LVN_GETDISPINFO notification message. */
306 static void OnGetDispInfo(NMLVDISPINFO
* plvdi
)
308 static TCHAR buffer
[200];
310 plvdi
->item
.pszText
= NULL
;
311 plvdi
->item
.cchTextMax
= 0;
313 switch (plvdi
->item
.iSubItem
)
316 LoadString(hInst
, IDS_DEFAULT_VALUE_NAME
, buffer
, COUNT_OF(buffer
));
317 plvdi
->item
.pszText
= buffer
;
320 switch (((LINE_INFO
*)plvdi
->item
.lParam
)->dwValType
)
323 plvdi
->item
.pszText
= _T("REG_NONE");
326 plvdi
->item
.pszText
= _T("REG_SZ");
329 plvdi
->item
.pszText
= _T("REG_EXPAND_SZ");
332 plvdi
->item
.pszText
= _T("REG_BINARY");
334 case REG_DWORD
: /* REG_DWORD_LITTLE_ENDIAN */
335 plvdi
->item
.pszText
= _T("REG_DWORD");
337 case REG_DWORD_BIG_ENDIAN
:
338 plvdi
->item
.pszText
= _T("REG_DWORD_BIG_ENDIAN");
341 plvdi
->item
.pszText
= _T("REG_LINK");
344 plvdi
->item
.pszText
= _T("REG_MULTI_SZ");
346 case REG_RESOURCE_LIST
:
347 plvdi
->item
.pszText
= _T("REG_RESOURCE_LIST");
349 case REG_FULL_RESOURCE_DESCRIPTOR
:
350 plvdi
->item
.pszText
= _T("REG_FULL_RESOURCE_DESCRIPTOR");
352 case REG_RESOURCE_REQUIREMENTS_LIST
:
353 plvdi
->item
.pszText
= _T("REG_RESOURCE_REQUIREMENTS_LIST");
355 case REG_QWORD
: /* REG_QWORD_LITTLE_ENDIAN */
356 plvdi
->item
.pszText
= _T("REG_QWORD");
361 LoadString(hInst
, IDS_UNKNOWN_TYPE
, buf2
, COUNT_OF(buf2
));
362 wsprintf(buffer
, buf2
, ((LINE_INFO
*)plvdi
->item
.lParam
)->dwValType
);
363 plvdi
->item
.pszText
= buffer
;
369 plvdi
->item
.pszText
= _T("");
374 static int CALLBACK
CompareFunc(LPARAM lParam1
, LPARAM lParam2
, LPARAM lParamSort
)
377 UNREFERENCED_PARAMETER(lParamSort
);
379 l
= (LINE_INFO
*)lParam1
;
380 r
= (LINE_INFO
*)lParam2
;
382 if (g_columnToSort
== ~0UL)
385 if (g_columnToSort
== 1 && l
->dwValType
!= r
->dwValType
)
386 return g_invertSort
? (int)r
->dwValType
- (int)l
->dwValType
: (int)l
->dwValType
- (int)r
->dwValType
;
387 if (g_columnToSort
== 2)
389 /* FIXME: Sort on value */
391 return g_invertSort
? _tcsicmp(r
->name
, l
->name
) : _tcsicmp(l
->name
, r
->name
);
394 BOOL
ListWndNotifyProc(HWND hWnd
, WPARAM wParam
, LPARAM lParam
, BOOL
*Result
)
397 UNREFERENCED_PARAMETER(wParam
);
399 switch (((LPNMHDR
)lParam
)->code
)
401 case LVN_GETDISPINFO
:
402 OnGetDispInfo((NMLVDISPINFO
*)lParam
);
404 case LVN_COLUMNCLICK
:
405 if (g_columnToSort
== (DWORD
)((LPNMLISTVIEW
)lParam
)->iSubItem
)
406 g_invertSort
= !g_invertSort
;
409 g_columnToSort
= ((LPNMLISTVIEW
)lParam
)->iSubItem
;
410 g_invertSort
= FALSE
;
413 (void)ListView_SortItems(hWnd
, CompareFunc
, (WPARAM
)hWnd
);
418 SendMessage(hFrameWnd
, WM_COMMAND
, MAKEWPARAM(ID_EDIT_MODIFY
, 0), 0);
422 g_pChildWnd
->nFocusPanel
= 0;
424 case LVN_BEGINLABELEDIT
:
425 Info
= (NMLVDISPINFO
*)lParam
;
428 PLINE_INFO lineinfo
= (PLINE_INFO
)Info
->item
.lParam
;
429 if(!lineinfo
->name
|| !_tcscmp(lineinfo
->name
, _T("")))
441 case LVN_ENDLABELEDIT
:
442 Info
= (NMLVDISPINFO
*)lParam
;
443 if(Info
&& Info
->item
.pszText
)
445 PLINE_INFO lineinfo
= (PLINE_INFO
)Info
->item
.lParam
;
446 if(!lineinfo
->name
|| !_tcscmp(lineinfo
->name
, _T("")))
452 if(_tcslen(Info
->item
.pszText
) == 0)
454 TCHAR msg
[128], caption
[128];
456 LoadString(hInst
, IDS_ERR_RENVAL_TOEMPTY
, msg
, COUNT_OF(msg
));
457 LoadString(hInst
, IDS_ERR_RENVAL_CAPTION
, caption
, COUNT_OF(caption
));
458 MessageBox(0, msg
, caption
, 0);
467 keyPath
= GetItemPath(g_pChildWnd
->hTreeWnd
, 0, &hKeyRoot
);
468 lResult
= RenameValue(hKeyRoot
, keyPath
, Info
->item
.pszText
, lineinfo
->name
);
469 lineinfo
->name
= realloc(lineinfo
->name
, (_tcslen(Info
->item
.pszText
)+1)*sizeof(TCHAR
));
470 if (lineinfo
->name
!= NULL
)
471 _tcscpy(lineinfo
->name
, Info
->item
.pszText
);
474 return (lResult
== ERROR_SUCCESS
);
487 HWND
CreateListView(HWND hwndParent
, HMENU id
)
492 /* Get the dimensions of the parent window's client area, and create the list view control. */
493 GetClientRect(hwndParent
, &rcClient
);
494 hwndLV
= CreateWindowEx(WS_EX_CLIENTEDGE
, WC_LISTVIEW
, _T("List View"),
495 WS_VISIBLE
| WS_CHILD
| WS_TABSTOP
| LVS_REPORT
| LVS_EDITLABELS
,
496 0, 0, rcClient
.right
, rcClient
.bottom
,
497 hwndParent
, id
, hInst
, NULL
);
498 if (!hwndLV
) return NULL
;
500 /* Initialize the image list, and add items to the control. */
501 if (!CreateListColumns(hwndLV
)) goto fail
;
502 if (!InitListViewImageLists(hwndLV
)) goto fail
;
506 DestroyWindow(hwndLV
);
510 void DestroyListView(HWND hwndLV
)
515 count
= ListView_GetItemCount(hwndLV
);
516 for (i
= 0; i
< count
; i
++)
518 item
.mask
= LVIF_PARAM
;
520 (void)ListView_GetItem(hwndLV
, &item
);
521 free(((LINE_INFO
*)item
.lParam
)->name
);
522 HeapFree(GetProcessHeap(), 0, (void*)item
.lParam
);
527 BOOL
RefreshListView(HWND hwndLV
, HKEY hKey
, LPCTSTR keyPath
)
529 DWORD max_sub_key_len
;
530 DWORD max_val_name_len
;
536 BOOL AddedDefault
= FALSE
;
538 if (!hwndLV
) return FALSE
;
540 (void)ListView_EditLabel(hwndLV
, -1);
542 SendMessage(hwndLV
, WM_SETREDRAW
, FALSE
, 0);
543 DestroyListView(hwndLV
);
545 g_columnToSort
= ~0UL;
546 (void)ListView_DeleteAllItems(hwndLV
);
548 if(!hKey
) return FALSE
;
550 errCode
= RegOpenKeyEx(hKey
, keyPath
, 0, KEY_READ
, &hNewKey
);
551 if (errCode
!= ERROR_SUCCESS
) return FALSE
;
553 /* get size information and resize the buffers if necessary */
554 errCode
= RegQueryInfoKey(hNewKey
, NULL
, NULL
, NULL
, NULL
, &max_sub_key_len
, NULL
,
555 &val_count
, &max_val_name_len
, &max_val_size
, NULL
, NULL
);
557 if (errCode
== ERROR_SUCCESS
)
559 TCHAR
* ValName
= HeapAlloc(GetProcessHeap(), 0, ++max_val_name_len
* sizeof(TCHAR
));
560 DWORD dwValNameLen
= max_val_name_len
;
561 BYTE
* ValBuf
= HeapAlloc(GetProcessHeap(), 0, max_val_size
+ sizeof(TCHAR
));
562 DWORD dwValSize
= max_val_size
;
565 /* if (RegQueryValueEx(hNewKey, NULL, NULL, &dwValType, ValBuf, &dwValSize) == ERROR_SUCCESS) { */
566 /* AddEntryToList(hwndLV, _T("(Default)"), dwValType, ValBuf, dwValSize); */
568 /* dwValSize = max_val_size; */
569 while (RegEnumValue(hNewKey
, dwIndex
, ValName
, &dwValNameLen
, NULL
, &dwValType
, ValBuf
, &dwValSize
) == ERROR_SUCCESS
)
571 /* Add a terminating 0 character. Usually this is only necessary for strings. */
572 ValBuf
[dwValSize
] = 0;
574 ValBuf
[dwValSize
+ 1] = 0;
576 AddEntryToList(hwndLV
, ValName
, dwValType
, ValBuf
, dwValSize
, -1, TRUE
);
577 dwValNameLen
= max_val_name_len
;
578 dwValSize
= max_val_size
;
581 if(!_tcscmp(ValName
, _T("")))
586 HeapFree(GetProcessHeap(), 0, ValBuf
);
587 HeapFree(GetProcessHeap(), 0, ValName
);
591 AddEntryToList(hwndLV
, _T(""), REG_SZ
, NULL
, 0, 0, FALSE
);
593 ListView_SortItems(hwndLV
, CompareFunc
, (WPARAM
)hwndLV
);
594 c
= ListView_GetItemCount(hwndLV
);
595 for(i
= 0; i
< c
; i
++)
597 ListView_SetItemState(hwndLV
, i
, 0, LVIS_FOCUSED
| LVIS_SELECTED
);
599 ListView_SetItemState(hwndLV
, iListViewSelect
,
600 LVIS_FOCUSED
| LVIS_SELECTED
,
601 LVIS_FOCUSED
| LVIS_SELECTED
);
602 RegCloseKey(hNewKey
);
603 SendMessage(hwndLV
, WM_SETREDRAW
, TRUE
, 0);