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 typedef struct tagSORT_INFO
44 } SORT_INFO
, *PSORT_INFO
;
46 /*******************************************************************************
47 * Global and Local Variables:
50 static INT g_iSortedColumn
= 0;
52 #define MAX_LIST_COLUMNS (IDS_LIST_COLUMN_LAST - IDS_LIST_COLUMN_FIRST + 1)
53 static const int default_column_widths
[MAX_LIST_COLUMNS
] = { 200, 175, 400 };
54 static const int column_alignment
[MAX_LIST_COLUMNS
] = { LVCFMT_LEFT
, LVCFMT_LEFT
, LVCFMT_LEFT
};
56 LPCWSTR
GetValueName(HWND hwndLV
, int iStartAt
)
63 if a new item is inserted, then no allocation,
64 otherwise the heap block will be lost!
66 item
= ListView_GetNextItem(hwndLV
, iStartAt
, LVNI_SELECTED
);
67 if (item
== -1) return NULL
;
70 Should be always TRUE anyways
74 LVItem
.mask
= LVIF_PARAM
;
75 if (ListView_GetItem(hwndLV
, &LVItem
) == FALSE
)
78 lineinfo
= (PLINE_INFO
)LVItem
.lParam
;
82 return lineinfo
->name
;
85 VOID
SetValueName(HWND hwndLV
, LPCWSTR pszValueName
)
90 c
= ListView_GetItemCount(hwndLV
);
91 for(i
= 0; i
< c
; i
++)
93 ListView_SetItemState(hwndLV
, i
, 0, LVIS_FOCUSED
| LVIS_SELECTED
);
95 if (pszValueName
== NULL
)
99 fi
.flags
= LVFI_STRING
;
100 fi
.psz
= pszValueName
;
101 i
= ListView_FindItem(hwndLV
, -1, &fi
);
103 ListView_SetItemState(hwndLV
, i
, LVIS_FOCUSED
| LVIS_SELECTED
,
104 LVIS_FOCUSED
| LVIS_SELECTED
);
108 BOOL
IsDefaultValue(HWND hwndLV
, int i
)
113 Item
.mask
= LVIF_PARAM
;
115 if(ListView_GetItem(hwndLV
, &Item
))
117 lineinfo
= (PLINE_INFO
)Item
.lParam
;
118 return lineinfo
&& (!lineinfo
->name
|| !wcscmp(lineinfo
->name
, L
""));
123 /*******************************************************************************
124 * Local module support methods
126 static void AddEntryToList(HWND hwndLV
, LPWSTR Name
, DWORD dwValType
, void* ValBuf
, DWORD dwCount
, int Position
, BOOL ValExists
)
132 linfo
= (PLINE_INFO
)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(LINE_INFO
) + dwCount
);
133 linfo
->dwValType
= dwValType
;
134 linfo
->val_len
= dwCount
;
137 memcpy(&linfo
[1], ValBuf
, dwCount
);
138 linfo
->val
= &linfo
[1];
140 linfo
->name
= _wcsdup(Name
);
142 item
.mask
= LVIF_TEXT
| LVIF_PARAM
| LVIF_IMAGE
;
143 item
.iItem
= (Position
== -1 ? 0: Position
);
148 item
.cchTextMax
= (int)wcslen(item
.pszText
);
149 if (item
.cchTextMax
== 0)
150 item
.pszText
= LPSTR_TEXTCALLBACK
;
152 item
.lParam
= (LPARAM
)linfo
;
158 item
.iImage
= Image_String
;
161 item
.iImage
= Image_Bin
;
165 /* item.lParam = (LPARAM)ValBuf; */
166 #if (_WIN32_IE >= 0x0300)
170 index
= ListView_InsertItem(hwndLV
, &item
);
179 ListView_SetItemText(hwndLV
, index
, 2, ValBuf
);
184 /* load (value not set) string */
185 LoadStringW(hInst
, IDS_VALUE_NOT_SET
, buffer
, COUNT_OF(buffer
));
186 ListView_SetItemText(hwndLV
, index
, 2, buffer
);
194 src
= (LPWSTR
)ValBuf
;
195 str
= HeapAlloc(GetProcessHeap(), 0, dwCount
+ sizeof(WCHAR
));
199 /* concatenate all srings */
204 src
+= wcslen(src
) + 1;
206 ListView_SetItemText(hwndLV
, index
, 2, str
);
207 HeapFree(GetProcessHeap(), 0, str
);
210 ListView_SetItemText(hwndLV
, index
, 2, L
"");
213 ListView_SetItemText(hwndLV
, index
, 2, L
"");
219 if(dwCount
== sizeof(DWORD
))
221 wsprintf(buf
, L
"0x%08x (%u)", *(DWORD
*)ValBuf
, *(DWORD
*)ValBuf
);
225 LoadStringW(hInst
, IDS_INVALID_DWORD
, buf
, COUNT_OF(buf
));
227 ListView_SetItemText(hwndLV
, index
, 2, buf
);
229 /* lpsRes = convertHexToDWORDStr(lpbData, dwLen); */
234 LPBYTE pData
= (LPBYTE
)ValBuf
;
238 strBinary
= HeapAlloc(GetProcessHeap(), 0, (dwCount
* sizeof(WCHAR
) * 3) + sizeof(WCHAR
));
239 for (i
= 0; i
< dwCount
; i
++)
241 wsprintf( strBinary
+ i
*3, L
"%02X ", pData
[i
] );
243 strBinary
[dwCount
* 3] = 0;
244 ListView_SetItemText(hwndLV
, index
, 2, strBinary
);
245 HeapFree(GetProcessHeap(), 0, strBinary
);
250 LoadStringW(hInst
, IDS_BINARY_EMPTY
, szText
, COUNT_OF(szText
));
251 ListView_SetItemText(hwndLV
, index
, 2, szText
);
259 static BOOL
CreateListColumns(HWND hWndListView
)
265 /* Create columns. */
266 lvC
.mask
= LVCF_FMT
| LVCF_WIDTH
| LVCF_TEXT
| LVCF_SUBITEM
;
267 lvC
.pszText
= szText
;
269 /* Load the column labels from the resource file. */
270 for (index
= 0; index
< MAX_LIST_COLUMNS
; index
++)
272 lvC
.iSubItem
= index
;
273 lvC
.cx
= default_column_widths
[index
];
274 lvC
.fmt
= column_alignment
[index
];
275 LoadStringW(hInst
, IDS_LIST_COLUMN_FIRST
+ index
, szText
, COUNT_OF(szText
));
276 if (ListView_InsertColumn(hWndListView
, index
, &lvC
) == -1) return FALSE
;
281 static BOOL
InitListViewImageLists(HWND hwndLV
)
283 HIMAGELIST himl
; /* handle to image list */
284 HICON hico
; /* handle to icon */
286 /* Create the image list. */
287 if ((himl
= ImageList_Create(CX_ICON
, CY_ICON
,
288 ILC_MASK
, 0, NUM_ICONS
)) == NULL
)
293 hico
= LoadIconW(hInst
, MAKEINTRESOURCEW(IDI_BIN
));
294 Image_Bin
= ImageList_AddIcon(himl
, hico
);
296 hico
= LoadIconW(hInst
, MAKEINTRESOURCEW(IDI_STRING
));
297 Image_String
= ImageList_AddIcon(himl
, hico
);
299 /* Fail if not all of the images were added. */
300 if (ImageList_GetImageCount(himl
) < NUM_ICONS
)
305 /* Associate the image list with the tree view control. */
306 (void)ListView_SetImageList(hwndLV
, himl
, LVSIL_SMALL
);
311 /* OnGetDispInfo - processes the LVN_GETDISPINFO notification message. */
313 static void OnGetDispInfo(NMLVDISPINFO
* plvdi
)
315 static WCHAR buffer
[200];
317 plvdi
->item
.pszText
= NULL
;
318 plvdi
->item
.cchTextMax
= 0;
320 switch (plvdi
->item
.iSubItem
)
323 LoadStringW(hInst
, IDS_DEFAULT_VALUE_NAME
, buffer
, COUNT_OF(buffer
));
324 plvdi
->item
.pszText
= buffer
;
327 switch (((LINE_INFO
*)plvdi
->item
.lParam
)->dwValType
)
330 plvdi
->item
.pszText
= L
"REG_NONE";
333 plvdi
->item
.pszText
= L
"REG_SZ";
336 plvdi
->item
.pszText
= L
"REG_EXPAND_SZ";
339 plvdi
->item
.pszText
= L
"REG_BINARY";
341 case REG_DWORD
: /* REG_DWORD_LITTLE_ENDIAN */
342 plvdi
->item
.pszText
= L
"REG_DWORD";
344 case REG_DWORD_BIG_ENDIAN
:
345 plvdi
->item
.pszText
= L
"REG_DWORD_BIG_ENDIAN";
348 plvdi
->item
.pszText
= L
"REG_LINK";
351 plvdi
->item
.pszText
= L
"REG_MULTI_SZ";
353 case REG_RESOURCE_LIST
:
354 plvdi
->item
.pszText
= L
"REG_RESOURCE_LIST";
356 case REG_FULL_RESOURCE_DESCRIPTOR
:
357 plvdi
->item
.pszText
= L
"REG_FULL_RESOURCE_DESCRIPTOR";
359 case REG_RESOURCE_REQUIREMENTS_LIST
:
360 plvdi
->item
.pszText
= L
"REG_RESOURCE_REQUIREMENTS_LIST";
362 case REG_QWORD
: /* REG_QWORD_LITTLE_ENDIAN */
363 plvdi
->item
.pszText
= L
"REG_QWORD";
368 LoadStringW(hInst
, IDS_UNKNOWN_TYPE
, buf2
, COUNT_OF(buf2
));
369 wsprintf(buffer
, buf2
, ((LINE_INFO
*)plvdi
->item
.lParam
)->dwValType
);
370 plvdi
->item
.pszText
= buffer
;
376 plvdi
->item
.pszText
= L
"";
381 static int CALLBACK
CompareFunc(LPARAM lParam1
, LPARAM lParam2
, LPARAM lParamSort
)
383 PSORT_INFO pSortInfo
= (PSORT_INFO
)lParamSort
;
388 l
= (LINE_INFO
*)lParam1
;
389 r
= (LINE_INFO
*)lParam2
;
391 if (pSortInfo
->iSortingColumn
== 1 && l
->dwValType
!= r
->dwValType
)
394 if (pSortInfo
->bSortAscending
)
395 return ((int)l
->dwValType
- (int)r
->dwValType
);
397 return ((int)r
->dwValType
- (int)l
->dwValType
);
399 if (pSortInfo
->iSortingColumn
== 2)
402 if (l
->dwValType
!= r
->dwValType
)
404 if (pSortInfo
->bSortAscending
)
405 return ((int)l
->dwValType
- (int)r
->dwValType
);
407 return ((int)r
->dwValType
- (int)l
->dwValType
);
410 if (l
->val
== NULL
&& r
->val
== NULL
)
413 if (pSortInfo
->bSortAscending
)
432 dw1
= *(DWORD
*)l
->val
;
433 dw2
= *(DWORD
*)r
->val
;
434 if (pSortInfo
->bSortAscending
)
435 // return (dw1 > dw2 ? 1 : -1);
436 return ((int)dw1
- (int)dw2
);
438 // return (dw1 > dw2 ? -1 : 1);
439 return ((int)dw2
- (int)dw1
);
444 qw1
= *(DWORDLONG
*)l
->val
;
445 qw2
= *(DWORDLONG
*)r
->val
;
446 if (pSortInfo
->bSortAscending
)
447 // return (qw1 > qw2 ? 1 : -1);
448 return ((int)qw1
- (int)qw2
);
450 // return (qw1 > qw2 ? -1 : 1);
451 return ((int)qw2
- (int)qw1
);
458 if (pSortInfo
->bSortAscending
)
460 nCompare
= memcmp(l
->val
, r
->val
, min(l
->val_len
, r
->val_len
));
462 nCompare
= l
->val_len
- r
->val_len
;
466 nCompare
= memcmp(r
->val
, l
->val
, min(r
->val_len
, l
->val_len
));
468 nCompare
= r
->val_len
- l
->val_len
;
477 return (pSortInfo
->bSortAscending
? StrCmpLogicalW(l
->name
, r
->name
) : StrCmpLogicalW(r
->name
, l
->name
));
480 static BOOL
ListView_Sort(HWND hListView
, int iSortingColumn
, int iSortedColumn
)
482 if ( (GetWindowLongPtr(hListView
, GWL_STYLE
) & ~LVS_NOSORTHEADER
) &&
483 (iSortingColumn
>= 0) )
488 HWND hHeader
= ListView_GetHeader(hListView
);
489 HDITEM hColumn
= {0};
491 /* If we are sorting according to another column, uninitialize the old one */
492 if ( (iSortedColumn
>= 0) && (iSortingColumn
!= iSortedColumn
) )
494 hColumn
.mask
= HDI_FORMAT
;
495 Header_GetItem(hHeader
, iSortedColumn
, &hColumn
);
496 hColumn
.fmt
&= ~(HDF_SORTUP
| HDF_SORTDOWN
);
497 Header_SetItem(hHeader
, iSortedColumn
, &hColumn
);
500 /* Get the sorting state of the new column */
501 hColumn
.mask
= HDI_FORMAT
;
502 Header_GetItem(hHeader
, iSortingColumn
, &hColumn
);
505 * Check whether we are sorting the list because the user clicked
506 * on a column, or because we are refreshing the list:
508 * iSortedColumn >= 0 - User clicked on a column; holds the
509 * old sorting column index.
510 * iSortedColumn < 0 - List being refreshed.
512 if (iSortedColumn
>= 0)
514 /* Invert the sorting direction */
515 bSortAscending
= ((hColumn
.fmt
& HDF_SORTUP
) == 0);
520 * If the sorting state of the column is uninitialized,
521 * initialize it by default to ascending sorting.
523 if ((hColumn
.fmt
& (HDF_SORTUP
| HDF_SORTDOWN
)) == 0)
524 hColumn
.fmt
|= HDF_SORTUP
;
526 /* Keep the same sorting direction */
527 bSortAscending
= ((hColumn
.fmt
& HDF_SORTUP
) != 0);
530 /* Set the new column sorting state */
531 hColumn
.fmt
&= ~(bSortAscending
? HDF_SORTDOWN
: HDF_SORTUP
);
532 hColumn
.fmt
|= (bSortAscending
? HDF_SORTUP
: HDF_SORTDOWN
);
533 Header_SetItem(hHeader
, iSortingColumn
, &hColumn
);
536 SortInfo
.iSortingColumn
= iSortingColumn
;
537 SortInfo
.bSortAscending
= bSortAscending
;
538 return ListView_SortItems(hListView
, CompareFunc
, (LPARAM
)&SortInfo
);
544 BOOL
ListWndNotifyProc(HWND hWnd
, WPARAM wParam
, LPARAM lParam
, BOOL
*Result
)
548 UNREFERENCED_PARAMETER(wParam
);
550 switch (((LPNMHDR
)lParam
)->code
)
552 case LVN_GETDISPINFO
:
553 OnGetDispInfo((NMLVDISPINFO
*)lParam
);
555 case LVN_COLUMNCLICK
:
556 iSortingColumn
= ((LPNMLISTVIEW
)lParam
)->iSubItem
;
557 (void)ListView_Sort(hWnd
, iSortingColumn
, g_iSortedColumn
);
558 g_iSortedColumn
= iSortingColumn
;
563 SendMessageW(hFrameWnd
, WM_COMMAND
, MAKEWPARAM(ID_EDIT_MODIFY
, 0), 0);
567 g_pChildWnd
->nFocusPanel
= 0;
569 case LVN_BEGINLABELEDIT
:
570 Info
= (NMLVDISPINFO
*)lParam
;
573 PLINE_INFO lineinfo
= (PLINE_INFO
)Info
->item
.lParam
;
574 if(!lineinfo
->name
|| !wcscmp(lineinfo
->name
, L
""))
586 case LVN_ENDLABELEDIT
:
587 Info
= (NMLVDISPINFO
*)lParam
;
588 if(Info
&& Info
->item
.pszText
)
590 PLINE_INFO lineinfo
= (PLINE_INFO
)Info
->item
.lParam
;
591 if(!lineinfo
->name
|| !wcscmp(lineinfo
->name
, L
""))
597 if(wcslen(Info
->item
.pszText
) == 0)
599 WCHAR msg
[128], caption
[128];
601 LoadStringW(hInst
, IDS_ERR_RENVAL_TOEMPTY
, msg
, COUNT_OF(msg
));
602 LoadStringW(hInst
, IDS_ERR_RENVAL_CAPTION
, caption
, COUNT_OF(caption
));
603 MessageBoxW(0, msg
, caption
, 0);
612 keyPath
= GetItemPath(g_pChildWnd
->hTreeWnd
, 0, &hKeyRoot
);
613 lResult
= RenameValue(hKeyRoot
, keyPath
, Info
->item
.pszText
, lineinfo
->name
);
614 lineinfo
->name
= realloc(lineinfo
->name
, (wcslen(Info
->item
.pszText
)+1)*sizeof(WCHAR
));
615 if (lineinfo
->name
!= NULL
)
616 wcscpy(lineinfo
->name
, Info
->item
.pszText
);
619 return (lResult
== ERROR_SUCCESS
);
631 HWND
CreateListView(HWND hwndParent
, HMENU id
)
636 /* Get the dimensions of the parent window's client area, and create the list view control. */
637 GetClientRect(hwndParent
, &rcClient
);
638 hwndLV
= CreateWindowExW(WS_EX_CLIENTEDGE
, WC_LISTVIEW
, L
"List View",
639 WS_VISIBLE
| WS_CHILD
| WS_TABSTOP
| LVS_REPORT
| LVS_EDITLABELS
| LVS_SHOWSELALWAYS
,
640 0, 0, rcClient
.right
, rcClient
.bottom
,
641 hwndParent
, id
, hInst
, NULL
);
642 if (!hwndLV
) return NULL
;
644 /* Initialize the image list, and add items to the control. */
645 if (!CreateListColumns(hwndLV
)) goto fail
;
646 if (!InitListViewImageLists(hwndLV
)) goto fail
;
650 DestroyWindow(hwndLV
);
654 void DestroyListView(HWND hwndLV
)
659 count
= ListView_GetItemCount(hwndLV
);
660 for (i
= 0; i
< count
; i
++)
662 item
.mask
= LVIF_PARAM
;
664 (void)ListView_GetItem(hwndLV
, &item
);
665 free(((LINE_INFO
*)item
.lParam
)->name
);
666 HeapFree(GetProcessHeap(), 0, (void*)item
.lParam
);
671 BOOL
RefreshListView(HWND hwndLV
, HKEY hKey
, LPCWSTR keyPath
)
673 DWORD max_sub_key_len
;
674 DWORD max_val_name_len
;
680 BOOL AddedDefault
= FALSE
;
682 if (!hwndLV
) return FALSE
;
684 (void)ListView_EditLabel(hwndLV
, -1);
686 SendMessageW(hwndLV
, WM_SETREDRAW
, FALSE
, 0);
687 DestroyListView(hwndLV
);
689 (void)ListView_DeleteAllItems(hwndLV
);
691 if(!hKey
) return FALSE
;
693 errCode
= RegOpenKeyExW(hKey
, keyPath
, 0, KEY_READ
, &hNewKey
);
694 if (errCode
!= ERROR_SUCCESS
) return FALSE
;
696 /* get size information and resize the buffers if necessary */
697 errCode
= RegQueryInfoKeyW(hNewKey
, NULL
, NULL
, NULL
, NULL
, &max_sub_key_len
, NULL
,
698 &val_count
, &max_val_name_len
, &max_val_size
, NULL
, NULL
);
700 if (errCode
== ERROR_SUCCESS
)
702 WCHAR
* ValName
= HeapAlloc(GetProcessHeap(), 0, ++max_val_name_len
* sizeof(WCHAR
));
703 DWORD dwValNameLen
= max_val_name_len
;
704 BYTE
* ValBuf
= HeapAlloc(GetProcessHeap(), 0, max_val_size
+ sizeof(WCHAR
));
705 DWORD dwValSize
= max_val_size
;
708 /* if (RegQueryValueExW(hNewKey, NULL, NULL, &dwValType, ValBuf, &dwValSize) == ERROR_SUCCESS) { */
709 /* AddEntryToList(hwndLV, L"(Default)", dwValType, ValBuf, dwValSize); */
711 /* dwValSize = max_val_size; */
712 while (RegEnumValueW(hNewKey
, dwIndex
, ValName
, &dwValNameLen
, NULL
, &dwValType
, ValBuf
, &dwValSize
) == ERROR_SUCCESS
)
714 /* Add a terminating 0 character. Usually this is only necessary for strings. */
715 ValBuf
[dwValSize
] = ValBuf
[dwValSize
+ 1] = 0;
717 AddEntryToList(hwndLV
, ValName
, dwValType
, ValBuf
, dwValSize
, -1, TRUE
);
718 dwValNameLen
= max_val_name_len
;
719 dwValSize
= max_val_size
;
722 if(!wcscmp(ValName
, L
""))
727 HeapFree(GetProcessHeap(), 0, ValBuf
);
728 HeapFree(GetProcessHeap(), 0, ValName
);
730 RegCloseKey(hNewKey
);
734 AddEntryToList(hwndLV
, L
"", REG_SZ
, NULL
, 0, 0, FALSE
);
736 c
= ListView_GetItemCount(hwndLV
);
737 for(i
= 0; i
< c
; i
++)
739 ListView_SetItemState(hwndLV
, i
, 0, LVIS_FOCUSED
| LVIS_SELECTED
);
741 ListView_SetItemState(hwndLV
, iListViewSelect
,
742 LVIS_FOCUSED
| LVIS_SELECTED
,
743 LVIS_FOCUSED
| LVIS_SELECTED
);
744 (void)ListView_Sort(hwndLV
, g_iSortedColumn
, -1);
745 SendMessageW(hwndLV
, WM_SETREDRAW
, TRUE
, 0);