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
25 #define LISTVIEW_NUM_ICONS 2
29 INT iListViewSelect
= -1;
31 typedef struct tagLINE_INFO
37 } LINE_INFO
, *PLINE_INFO
;
39 typedef struct tagSORT_INFO
43 } SORT_INFO
, *PSORT_INFO
;
45 /*******************************************************************************
46 * Global and Local Variables:
49 static INT g_iSortedColumn
= 0;
51 #define MAX_LIST_COLUMNS (IDS_LIST_COLUMN_LAST - IDS_LIST_COLUMN_FIRST + 1)
52 static const int default_column_widths
[MAX_LIST_COLUMNS
] = { 35, 25, 40 }; /* in percents */
53 static const int column_alignment
[MAX_LIST_COLUMNS
] = { LVCFMT_LEFT
, LVCFMT_LEFT
, LVCFMT_LEFT
};
55 WCHAR
*GetValueName(HWND hwndLV
, int iStartAt
)
62 if a new item is inserted, then no allocation,
63 otherwise the heap block will be lost!
65 item
= ListView_GetNextItem(hwndLV
, iStartAt
, LVNI_SELECTED
);
66 if (item
== -1) return NULL
;
69 Should be always TRUE anyways
73 LVItem
.mask
= LVIF_PARAM
;
74 if (ListView_GetItem(hwndLV
, &LVItem
) == FALSE
)
77 lineinfo
= (PLINE_INFO
)LVItem
.lParam
;
81 return lineinfo
->name
;
84 VOID
SetValueName(HWND hwndLV
, LPCWSTR pszValueName
)
89 c
= ListView_GetItemCount(hwndLV
);
90 for(i
= 0; i
< c
; i
++)
92 ListView_SetItemState(hwndLV
, i
, 0, LVIS_FOCUSED
| LVIS_SELECTED
);
94 if (pszValueName
== NULL
|| pszValueName
[0] == 0)
98 fi
.flags
= LVFI_STRING
;
99 fi
.psz
= pszValueName
;
100 i
= ListView_FindItem(hwndLV
, -1, &fi
);
102 ListView_SetItemState(hwndLV
, i
, LVIS_FOCUSED
| LVIS_SELECTED
,
103 LVIS_FOCUSED
| LVIS_SELECTED
);
104 ListView_EnsureVisible(hwndLV
, i
, FALSE
);
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
, ARRAY_SIZE(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
"");
220 if(dwCount
== sizeof(DWORD
))
222 wsprintf(buf
, L
"0x%08x (%u)", *(DWORD
*)ValBuf
, *(DWORD
*)ValBuf
);
226 LoadStringW(hInst
, IDS_INVALID_DWORD
, buf
, ARRAY_SIZE(buf
));
228 ListView_SetItemText(hwndLV
, index
, 2, buf
);
230 /* lpsRes = convertHexToDWORDStr(lpbData, dwLen); */
235 LPBYTE pData
= (LPBYTE
)ValBuf
;
239 strBinary
= HeapAlloc(GetProcessHeap(), 0, (dwCount
* sizeof(WCHAR
) * 3) + sizeof(WCHAR
));
240 for (i
= 0; i
< dwCount
; i
++)
242 wsprintf( strBinary
+ i
*3, L
"%02X ", pData
[i
] );
244 strBinary
[dwCount
* 3] = 0;
245 ListView_SetItemText(hwndLV
, index
, 2, strBinary
);
246 HeapFree(GetProcessHeap(), 0, strBinary
);
251 LoadStringW(hInst
, IDS_BINARY_EMPTY
, szText
, ARRAY_SIZE(szText
));
252 ListView_SetItemText(hwndLV
, index
, 2, szText
);
260 static BOOL
CreateListColumns(HWND hWndListView
, INT cxTotal
)
266 /* Create columns. */
267 lvC
.mask
= LVCF_FMT
| LVCF_WIDTH
| LVCF_TEXT
| LVCF_SUBITEM
;
268 lvC
.pszText
= szText
;
270 /* Load the column labels from the resource file. */
271 for (index
= 0; index
< MAX_LIST_COLUMNS
; index
++)
273 lvC
.iSubItem
= index
;
274 lvC
.cx
= (cxTotal
* default_column_widths
[index
]) / 100;
275 lvC
.fmt
= column_alignment
[index
];
276 LoadStringW(hInst
, IDS_LIST_COLUMN_FIRST
+ index
, szText
, ARRAY_SIZE(szText
));
277 if (ListView_InsertColumn(hWndListView
, index
, &lvC
) == -1) return FALSE
;
282 static BOOL
InitListViewImageLists(HWND hwndLV
)
284 HIMAGELIST himl
; /* handle to image list */
285 HICON hico
; /* handle to icon */
287 /* Create the image list. */
288 if ((himl
= ImageList_Create(CX_ICON
, CY_ICON
,
289 ILC_MASK
, 0, LISTVIEW_NUM_ICONS
)) == NULL
)
294 hico
= LoadIconW(hInst
, MAKEINTRESOURCEW(IDI_BIN
));
295 Image_Bin
= ImageList_AddIcon(himl
, hico
);
297 hico
= LoadIconW(hInst
, MAKEINTRESOURCEW(IDI_STRING
));
298 Image_String
= ImageList_AddIcon(himl
, hico
);
300 /* Fail if not all of the images were added. */
301 if (ImageList_GetImageCount(himl
) < LISTVIEW_NUM_ICONS
)
306 /* Associate the image list with the tree view control. */
307 (void)ListView_SetImageList(hwndLV
, himl
, LVSIL_SMALL
);
312 /* OnGetDispInfo - processes the LVN_GETDISPINFO notification message. */
314 static void OnGetDispInfo(NMLVDISPINFO
* plvdi
)
316 static WCHAR buffer
[200];
318 plvdi
->item
.pszText
= NULL
;
319 plvdi
->item
.cchTextMax
= 0;
321 switch (plvdi
->item
.iSubItem
)
324 LoadStringW(hInst
, IDS_DEFAULT_VALUE_NAME
, buffer
, ARRAY_SIZE(buffer
));
325 plvdi
->item
.pszText
= buffer
;
328 switch (((LINE_INFO
*)plvdi
->item
.lParam
)->dwValType
)
331 plvdi
->item
.pszText
= L
"REG_NONE";
334 plvdi
->item
.pszText
= L
"REG_SZ";
337 plvdi
->item
.pszText
= L
"REG_EXPAND_SZ";
340 plvdi
->item
.pszText
= L
"REG_BINARY";
342 case REG_DWORD
: /* REG_DWORD_LITTLE_ENDIAN */
343 plvdi
->item
.pszText
= L
"REG_DWORD";
345 case REG_DWORD_BIG_ENDIAN
:
346 plvdi
->item
.pszText
= L
"REG_DWORD_BIG_ENDIAN";
349 plvdi
->item
.pszText
= L
"REG_LINK";
352 plvdi
->item
.pszText
= L
"REG_MULTI_SZ";
354 case REG_RESOURCE_LIST
:
355 plvdi
->item
.pszText
= L
"REG_RESOURCE_LIST";
357 case REG_FULL_RESOURCE_DESCRIPTOR
:
358 plvdi
->item
.pszText
= L
"REG_FULL_RESOURCE_DESCRIPTOR";
360 case REG_RESOURCE_REQUIREMENTS_LIST
:
361 plvdi
->item
.pszText
= L
"REG_RESOURCE_REQUIREMENTS_LIST";
363 case REG_QWORD
: /* REG_QWORD_LITTLE_ENDIAN */
364 plvdi
->item
.pszText
= L
"REG_QWORD";
369 LoadStringW(hInst
, IDS_UNKNOWN_TYPE
, buf2
, ARRAY_SIZE(buf2
));
370 wsprintf(buffer
, buf2
, ((LINE_INFO
*)plvdi
->item
.lParam
)->dwValType
);
371 plvdi
->item
.pszText
= buffer
;
377 plvdi
->item
.pszText
= L
"";
382 static int CALLBACK
CompareFunc(LPARAM lParam1
, LPARAM lParam2
, LPARAM lParamSort
)
384 PSORT_INFO pSortInfo
= (PSORT_INFO
)lParamSort
;
389 l
= (LINE_INFO
*)lParam1
;
390 r
= (LINE_INFO
*)lParam2
;
392 if (pSortInfo
->iSortingColumn
== 1 && l
->dwValType
!= r
->dwValType
)
395 if (pSortInfo
->bSortAscending
)
396 return ((int)l
->dwValType
- (int)r
->dwValType
);
398 return ((int)r
->dwValType
- (int)l
->dwValType
);
400 if (pSortInfo
->iSortingColumn
== 2)
403 if (l
->dwValType
!= r
->dwValType
)
405 if (pSortInfo
->bSortAscending
)
406 return ((int)l
->dwValType
- (int)r
->dwValType
);
408 return ((int)r
->dwValType
- (int)l
->dwValType
);
411 if (l
->val
== NULL
&& r
->val
== NULL
)
414 if (pSortInfo
->bSortAscending
)
433 dw1
= *(DWORD
*)l
->val
;
434 dw2
= *(DWORD
*)r
->val
;
435 if (pSortInfo
->bSortAscending
)
436 // return (dw1 > dw2 ? 1 : -1);
437 return ((int)dw1
- (int)dw2
);
439 // return (dw1 > dw2 ? -1 : 1);
440 return ((int)dw2
- (int)dw1
);
443 case REG_QWORD
: /* REG_QWORD_LITTLE_ENDIAN */
445 qw1
= *(DWORDLONG
*)l
->val
;
446 qw2
= *(DWORDLONG
*)r
->val
;
447 if (pSortInfo
->bSortAscending
)
448 // return (qw1 > qw2 ? 1 : -1);
449 return ((int)qw1
- (int)qw2
);
451 // return (qw1 > qw2 ? -1 : 1);
452 return ((int)qw2
- (int)qw1
);
459 if (pSortInfo
->bSortAscending
)
461 nCompare
= memcmp(l
->val
, r
->val
, min(l
->val_len
, r
->val_len
));
463 nCompare
= l
->val_len
- r
->val_len
;
467 nCompare
= memcmp(r
->val
, l
->val
, min(r
->val_len
, l
->val_len
));
469 nCompare
= r
->val_len
- l
->val_len
;
478 return (pSortInfo
->bSortAscending
? StrCmpLogicalW(l
->name
, r
->name
) : StrCmpLogicalW(r
->name
, l
->name
));
481 static BOOL
ListView_Sort(HWND hListView
, int iSortingColumn
, int iSortedColumn
)
483 if (!(GetWindowLongPtr(hListView
, GWL_STYLE
) & LVS_NOSORTHEADER
) &&
484 (iSortingColumn
>= 0) )
489 HWND hHeader
= ListView_GetHeader(hListView
);
490 HDITEM hColumn
= {0};
492 /* If we are sorting according to another column, uninitialize the old one */
493 if ( (iSortedColumn
>= 0) && (iSortingColumn
!= iSortedColumn
) )
495 hColumn
.mask
= HDI_FORMAT
;
496 Header_GetItem(hHeader
, iSortedColumn
, &hColumn
);
497 hColumn
.fmt
&= ~(HDF_SORTUP
| HDF_SORTDOWN
);
498 Header_SetItem(hHeader
, iSortedColumn
, &hColumn
);
501 /* Get the sorting state of the new column */
502 hColumn
.mask
= HDI_FORMAT
;
503 Header_GetItem(hHeader
, iSortingColumn
, &hColumn
);
506 * Check whether we are sorting the list because the user clicked
507 * on a column, or because we are refreshing the list:
509 * iSortedColumn >= 0 - User clicked on a column; holds the
510 * old sorting column index.
511 * iSortedColumn < 0 - List being refreshed.
513 if (iSortedColumn
>= 0)
515 /* Invert the sorting direction */
516 bSortAscending
= ((hColumn
.fmt
& HDF_SORTUP
) == 0);
521 * If the sorting state of the column is uninitialized,
522 * initialize it by default to ascending sorting.
524 if ((hColumn
.fmt
& (HDF_SORTUP
| HDF_SORTDOWN
)) == 0)
525 hColumn
.fmt
|= HDF_SORTUP
;
527 /* Keep the same sorting direction */
528 bSortAscending
= ((hColumn
.fmt
& HDF_SORTUP
) != 0);
531 /* Set the new column sorting state */
532 hColumn
.fmt
&= ~(bSortAscending
? HDF_SORTDOWN
: HDF_SORTUP
);
533 hColumn
.fmt
|= (bSortAscending
? HDF_SORTUP
: HDF_SORTDOWN
);
534 Header_SetItem(hHeader
, iSortingColumn
, &hColumn
);
537 SortInfo
.iSortingColumn
= iSortingColumn
;
538 SortInfo
.bSortAscending
= bSortAscending
;
539 return ListView_SortItems(hListView
, CompareFunc
, (LPARAM
)&SortInfo
);
545 BOOL
ListWndNotifyProc(HWND hWnd
, WPARAM wParam
, LPARAM lParam
, BOOL
*Result
)
549 UNREFERENCED_PARAMETER(wParam
);
551 switch (((LPNMHDR
)lParam
)->code
)
553 case LVN_GETDISPINFO
:
554 OnGetDispInfo((NMLVDISPINFO
*)lParam
);
556 case LVN_COLUMNCLICK
:
557 iSortingColumn
= ((LPNMLISTVIEW
)lParam
)->iSubItem
;
558 (void)ListView_Sort(hWnd
, iSortingColumn
, g_iSortedColumn
);
559 g_iSortedColumn
= iSortingColumn
;
564 SendMessageW(hFrameWnd
, WM_COMMAND
, MAKEWPARAM(ID_EDIT_MODIFY
, 0), 0);
568 g_pChildWnd
->nFocusPanel
= 1;
570 case LVN_BEGINLABELEDIT
:
571 Info
= (NMLVDISPINFO
*)lParam
;
574 PLINE_INFO lineinfo
= (PLINE_INFO
)Info
->item
.lParam
;
575 if(!lineinfo
->name
|| !wcscmp(lineinfo
->name
, L
""))
587 case LVN_ENDLABELEDIT
:
588 Info
= (NMLVDISPINFO
*)lParam
;
589 if(Info
&& Info
->item
.pszText
)
591 PLINE_INFO lineinfo
= (PLINE_INFO
)Info
->item
.lParam
;
592 if(!lineinfo
->name
|| !wcscmp(lineinfo
->name
, L
""))
598 if(wcslen(Info
->item
.pszText
) == 0)
600 WCHAR msg
[128], caption
[128];
602 LoadStringW(hInst
, IDS_ERR_RENVAL_TOEMPTY
, msg
, ARRAY_SIZE(msg
));
603 LoadStringW(hInst
, IDS_ERR_RENVAL_CAPTION
, caption
, ARRAY_SIZE(caption
));
604 MessageBoxW(0, msg
, caption
, 0);
613 keyPath
= GetItemPath(g_pChildWnd
->hTreeWnd
, 0, &hKeyRoot
);
614 lResult
= RenameValue(hKeyRoot
, keyPath
, Info
->item
.pszText
, lineinfo
->name
);
615 lineinfo
->name
= realloc(lineinfo
->name
, (wcslen(Info
->item
.pszText
)+1)*sizeof(WCHAR
));
616 if (lineinfo
->name
!= NULL
)
617 wcscpy(lineinfo
->name
, Info
->item
.pszText
);
620 return (lResult
== ERROR_SUCCESS
);
632 HWND
CreateListView(HWND hwndParent
, HMENU id
, INT cx
)
637 /* Get the dimensions of the parent window's client area, and create the list view control. */
638 GetClientRect(hwndParent
, &rcClient
);
639 hwndLV
= CreateWindowExW(WS_EX_CLIENTEDGE
, WC_LISTVIEW
, L
"List View",
640 WS_VISIBLE
| WS_CHILD
| WS_TABSTOP
| LVS_REPORT
| LVS_EDITLABELS
| LVS_SHOWSELALWAYS
,
641 0, 0, rcClient
.right
, rcClient
.bottom
,
642 hwndParent
, id
, hInst
, NULL
);
643 if (!hwndLV
) return NULL
;
645 /* Initialize the image list, and add items to the control. */
646 if (!CreateListColumns(hwndLV
, cx
)) goto fail
;
647 if (!InitListViewImageLists(hwndLV
)) goto fail
;
651 DestroyWindow(hwndLV
);
655 void DestroyListView(HWND hwndLV
)
660 count
= ListView_GetItemCount(hwndLV
);
661 for (i
= 0; i
< count
; i
++)
663 item
.mask
= LVIF_PARAM
;
665 (void)ListView_GetItem(hwndLV
, &item
);
666 free(((LINE_INFO
*)item
.lParam
)->name
);
667 HeapFree(GetProcessHeap(), 0, (void*)item
.lParam
);
672 BOOL
RefreshListView(HWND hwndLV
, HKEY hKey
, LPCWSTR keyPath
, BOOL bSelectNone
)
674 DWORD max_sub_key_len
;
675 DWORD max_val_name_len
;
681 BOOL AddedDefault
= FALSE
;
683 if (!hwndLV
) return FALSE
;
685 (void)ListView_EditLabel(hwndLV
, -1);
687 SendMessageW(hwndLV
, WM_SETREDRAW
, FALSE
, 0);
688 DestroyListView(hwndLV
);
690 (void)ListView_DeleteAllItems(hwndLV
);
692 if(!hKey
) return FALSE
;
694 errCode
= RegOpenKeyExW(hKey
, keyPath
, 0, KEY_READ
, &hNewKey
);
695 if (errCode
!= ERROR_SUCCESS
) return FALSE
;
697 /* get size information and resize the buffers if necessary */
698 errCode
= RegQueryInfoKeyW(hNewKey
, NULL
, NULL
, NULL
, NULL
, &max_sub_key_len
, NULL
,
699 &val_count
, &max_val_name_len
, &max_val_size
, NULL
, NULL
);
701 if (errCode
== ERROR_SUCCESS
)
703 WCHAR
* ValName
= HeapAlloc(GetProcessHeap(), 0, ++max_val_name_len
* sizeof(WCHAR
));
704 DWORD dwValNameLen
= max_val_name_len
;
705 BYTE
* ValBuf
= HeapAlloc(GetProcessHeap(), 0, max_val_size
+ sizeof(WCHAR
));
706 DWORD dwValSize
= max_val_size
;
709 /* if (RegQueryValueExW(hNewKey, NULL, NULL, &dwValType, ValBuf, &dwValSize) == ERROR_SUCCESS) { */
710 /* AddEntryToList(hwndLV, L"(Default)", dwValType, ValBuf, dwValSize); */
712 /* dwValSize = max_val_size; */
713 while (RegEnumValueW(hNewKey
, dwIndex
, ValName
, &dwValNameLen
, NULL
, &dwValType
, ValBuf
, &dwValSize
) == ERROR_SUCCESS
)
715 /* Add a terminating 0 character. Usually this is only necessary for strings. */
716 ValBuf
[dwValSize
] = ValBuf
[dwValSize
+ 1] = 0;
718 AddEntryToList(hwndLV
, ValName
, dwValType
, ValBuf
, dwValSize
, -1, TRUE
);
719 dwValNameLen
= max_val_name_len
;
720 dwValSize
= max_val_size
;
723 if(!wcscmp(ValName
, L
""))
728 HeapFree(GetProcessHeap(), 0, ValBuf
);
729 HeapFree(GetProcessHeap(), 0, ValName
);
731 RegCloseKey(hNewKey
);
735 AddEntryToList(hwndLV
, L
"", REG_SZ
, NULL
, 0, 0, FALSE
);
737 c
= ListView_GetItemCount(hwndLV
);
738 for(i
= 0; i
< c
; i
++)
740 ListView_SetItemState(hwndLV
, i
, 0, LVIS_FOCUSED
| LVIS_SELECTED
);
744 iListViewSelect
= -1;
745 ListView_SetItemState(hwndLV
, iListViewSelect
,
746 LVIS_FOCUSED
| LVIS_SELECTED
,
747 LVIS_FOCUSED
| LVIS_SELECTED
);
748 (void)ListView_Sort(hwndLV
, g_iSortedColumn
, -1);
749 SendMessageW(hwndLV
, WM_SETREDRAW
, TRUE
, 0);