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
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
] = { 200, 175, 400 };
53 static const int column_alignment
[MAX_LIST_COLUMNS
] = { LVCFMT_LEFT
, LVCFMT_LEFT
, LVCFMT_LEFT
};
55 LPCWSTR
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
)
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
);
107 BOOL
IsDefaultValue(HWND hwndLV
, int i
)
112 Item
.mask
= LVIF_PARAM
;
114 if(ListView_GetItem(hwndLV
, &Item
))
116 lineinfo
= (PLINE_INFO
)Item
.lParam
;
117 return lineinfo
&& (!lineinfo
->name
|| !wcscmp(lineinfo
->name
, L
""));
122 /*******************************************************************************
123 * Local module support methods
125 static void AddEntryToList(HWND hwndLV
, LPWSTR Name
, DWORD dwValType
, void* ValBuf
, DWORD dwCount
, int Position
, BOOL ValExists
)
131 linfo
= (PLINE_INFO
)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(LINE_INFO
) + dwCount
);
132 linfo
->dwValType
= dwValType
;
133 linfo
->val_len
= dwCount
;
136 memcpy(&linfo
[1], ValBuf
, dwCount
);
137 linfo
->val
= &linfo
[1];
139 linfo
->name
= _wcsdup(Name
);
141 item
.mask
= LVIF_TEXT
| LVIF_PARAM
| LVIF_IMAGE
;
142 item
.iItem
= (Position
== -1 ? 0: Position
);
147 item
.cchTextMax
= (int)wcslen(item
.pszText
);
148 if (item
.cchTextMax
== 0)
149 item
.pszText
= LPSTR_TEXTCALLBACK
;
151 item
.lParam
= (LPARAM
)linfo
;
157 item
.iImage
= Image_String
;
160 item
.iImage
= Image_Bin
;
164 /* item.lParam = (LPARAM)ValBuf; */
165 #if (_WIN32_IE >= 0x0300)
169 index
= ListView_InsertItem(hwndLV
, &item
);
178 ListView_SetItemText(hwndLV
, index
, 2, ValBuf
);
183 /* load (value not set) string */
184 LoadStringW(hInst
, IDS_VALUE_NOT_SET
, buffer
, COUNT_OF(buffer
));
185 ListView_SetItemText(hwndLV
, index
, 2, buffer
);
193 src
= (LPWSTR
)ValBuf
;
194 str
= HeapAlloc(GetProcessHeap(), 0, dwCount
+ sizeof(WCHAR
));
198 /* concatenate all srings */
203 src
+= wcslen(src
) + 1;
205 ListView_SetItemText(hwndLV
, index
, 2, str
);
206 HeapFree(GetProcessHeap(), 0, str
);
209 ListView_SetItemText(hwndLV
, index
, 2, L
"");
212 ListView_SetItemText(hwndLV
, index
, 2, L
"");
218 if(dwCount
== sizeof(DWORD
))
220 wsprintf(buf
, L
"0x%08x (%u)", *(DWORD
*)ValBuf
, *(DWORD
*)ValBuf
);
224 LoadStringW(hInst
, IDS_INVALID_DWORD
, buf
, COUNT_OF(buf
));
226 ListView_SetItemText(hwndLV
, index
, 2, buf
);
228 /* lpsRes = convertHexToDWORDStr(lpbData, dwLen); */
233 LPBYTE pData
= (LPBYTE
)ValBuf
;
237 strBinary
= HeapAlloc(GetProcessHeap(), 0, (dwCount
* sizeof(WCHAR
) * 3) + sizeof(WCHAR
));
238 for (i
= 0; i
< dwCount
; i
++)
240 wsprintf( strBinary
+ i
*3, L
"%02X ", pData
[i
] );
242 strBinary
[dwCount
* 3] = 0;
243 ListView_SetItemText(hwndLV
, index
, 2, strBinary
);
244 HeapFree(GetProcessHeap(), 0, strBinary
);
249 LoadStringW(hInst
, IDS_BINARY_EMPTY
, szText
, COUNT_OF(szText
));
250 ListView_SetItemText(hwndLV
, index
, 2, szText
);
258 static BOOL
CreateListColumns(HWND hWndListView
)
264 /* Create columns. */
265 lvC
.mask
= LVCF_FMT
| LVCF_WIDTH
| LVCF_TEXT
| LVCF_SUBITEM
;
266 lvC
.pszText
= szText
;
268 /* Load the column labels from the resource file. */
269 for (index
= 0; index
< MAX_LIST_COLUMNS
; index
++)
271 lvC
.iSubItem
= index
;
272 lvC
.cx
= default_column_widths
[index
];
273 lvC
.fmt
= column_alignment
[index
];
274 LoadStringW(hInst
, IDS_LIST_COLUMN_FIRST
+ index
, szText
, COUNT_OF(szText
));
275 if (ListView_InsertColumn(hWndListView
, index
, &lvC
) == -1) return FALSE
;
280 static BOOL
InitListViewImageLists(HWND hwndLV
)
282 HIMAGELIST himl
; /* handle to image list */
283 HICON hico
; /* handle to icon */
285 /* Create the image list. */
286 if ((himl
= ImageList_Create(CX_ICON
, CY_ICON
,
287 ILC_MASK
, 0, NUM_ICONS
)) == NULL
)
292 hico
= LoadIconW(hInst
, MAKEINTRESOURCEW(IDI_BIN
));
293 Image_Bin
= ImageList_AddIcon(himl
, hico
);
295 hico
= LoadIconW(hInst
, MAKEINTRESOURCEW(IDI_STRING
));
296 Image_String
= ImageList_AddIcon(himl
, hico
);
298 /* Fail if not all of the images were added. */
299 if (ImageList_GetImageCount(himl
) < NUM_ICONS
)
304 /* Associate the image list with the tree view control. */
305 (void)ListView_SetImageList(hwndLV
, himl
, LVSIL_SMALL
);
310 /* OnGetDispInfo - processes the LVN_GETDISPINFO notification message. */
312 static void OnGetDispInfo(NMLVDISPINFO
* plvdi
)
314 static WCHAR buffer
[200];
316 plvdi
->item
.pszText
= NULL
;
317 plvdi
->item
.cchTextMax
= 0;
319 switch (plvdi
->item
.iSubItem
)
322 LoadStringW(hInst
, IDS_DEFAULT_VALUE_NAME
, buffer
, COUNT_OF(buffer
));
323 plvdi
->item
.pszText
= buffer
;
326 switch (((LINE_INFO
*)plvdi
->item
.lParam
)->dwValType
)
329 plvdi
->item
.pszText
= L
"REG_NONE";
332 plvdi
->item
.pszText
= L
"REG_SZ";
335 plvdi
->item
.pszText
= L
"REG_EXPAND_SZ";
338 plvdi
->item
.pszText
= L
"REG_BINARY";
340 case REG_DWORD
: /* REG_DWORD_LITTLE_ENDIAN */
341 plvdi
->item
.pszText
= L
"REG_DWORD";
343 case REG_DWORD_BIG_ENDIAN
:
344 plvdi
->item
.pszText
= L
"REG_DWORD_BIG_ENDIAN";
347 plvdi
->item
.pszText
= L
"REG_LINK";
350 plvdi
->item
.pszText
= L
"REG_MULTI_SZ";
352 case REG_RESOURCE_LIST
:
353 plvdi
->item
.pszText
= L
"REG_RESOURCE_LIST";
355 case REG_FULL_RESOURCE_DESCRIPTOR
:
356 plvdi
->item
.pszText
= L
"REG_FULL_RESOURCE_DESCRIPTOR";
358 case REG_RESOURCE_REQUIREMENTS_LIST
:
359 plvdi
->item
.pszText
= L
"REG_RESOURCE_REQUIREMENTS_LIST";
361 case REG_QWORD
: /* REG_QWORD_LITTLE_ENDIAN */
362 plvdi
->item
.pszText
= L
"REG_QWORD";
367 LoadStringW(hInst
, IDS_UNKNOWN_TYPE
, buf2
, COUNT_OF(buf2
));
368 wsprintf(buffer
, buf2
, ((LINE_INFO
*)plvdi
->item
.lParam
)->dwValType
);
369 plvdi
->item
.pszText
= buffer
;
375 plvdi
->item
.pszText
= L
"";
380 static int CALLBACK
CompareFunc(LPARAM lParam1
, LPARAM lParam2
, LPARAM lParamSort
)
382 PSORT_INFO pSortInfo
= (PSORT_INFO
)lParamSort
;
387 l
= (LINE_INFO
*)lParam1
;
388 r
= (LINE_INFO
*)lParam2
;
390 if (pSortInfo
->iSortingColumn
== 1 && l
->dwValType
!= r
->dwValType
)
393 if (pSortInfo
->bSortAscending
)
394 return ((int)l
->dwValType
- (int)r
->dwValType
);
396 return ((int)r
->dwValType
- (int)l
->dwValType
);
398 if (pSortInfo
->iSortingColumn
== 2)
401 if (l
->dwValType
!= r
->dwValType
)
403 if (pSortInfo
->bSortAscending
)
404 return ((int)l
->dwValType
- (int)r
->dwValType
);
406 return ((int)r
->dwValType
- (int)l
->dwValType
);
409 if (l
->val
== NULL
&& r
->val
== NULL
)
412 if (pSortInfo
->bSortAscending
)
431 dw1
= *(DWORD
*)l
->val
;
432 dw2
= *(DWORD
*)r
->val
;
433 if (pSortInfo
->bSortAscending
)
434 // return (dw1 > dw2 ? 1 : -1);
435 return ((int)dw1
- (int)dw2
);
437 // return (dw1 > dw2 ? -1 : 1);
438 return ((int)dw2
- (int)dw1
);
441 case REG_QWORD
: /* REG_QWORD_LITTLE_ENDIAN */
443 qw1
= *(DWORDLONG
*)l
->val
;
444 qw2
= *(DWORDLONG
*)r
->val
;
445 if (pSortInfo
->bSortAscending
)
446 // return (qw1 > qw2 ? 1 : -1);
447 return ((int)qw1
- (int)qw2
);
449 // return (qw1 > qw2 ? -1 : 1);
450 return ((int)qw2
- (int)qw1
);
457 if (pSortInfo
->bSortAscending
)
459 nCompare
= memcmp(l
->val
, r
->val
, min(l
->val_len
, r
->val_len
));
461 nCompare
= l
->val_len
- r
->val_len
;
465 nCompare
= memcmp(r
->val
, l
->val
, min(r
->val_len
, l
->val_len
));
467 nCompare
= r
->val_len
- l
->val_len
;
476 return (pSortInfo
->bSortAscending
? StrCmpLogicalW(l
->name
, r
->name
) : StrCmpLogicalW(r
->name
, l
->name
));
479 static BOOL
ListView_Sort(HWND hListView
, int iSortingColumn
, int iSortedColumn
)
481 if ( (GetWindowLongPtr(hListView
, GWL_STYLE
) & ~LVS_NOSORTHEADER
) &&
482 (iSortingColumn
>= 0) )
487 HWND hHeader
= ListView_GetHeader(hListView
);
488 HDITEM hColumn
= {0};
490 /* If we are sorting according to another column, uninitialize the old one */
491 if ( (iSortedColumn
>= 0) && (iSortingColumn
!= iSortedColumn
) )
493 hColumn
.mask
= HDI_FORMAT
;
494 Header_GetItem(hHeader
, iSortedColumn
, &hColumn
);
495 hColumn
.fmt
&= ~(HDF_SORTUP
| HDF_SORTDOWN
);
496 Header_SetItem(hHeader
, iSortedColumn
, &hColumn
);
499 /* Get the sorting state of the new column */
500 hColumn
.mask
= HDI_FORMAT
;
501 Header_GetItem(hHeader
, iSortingColumn
, &hColumn
);
504 * Check whether we are sorting the list because the user clicked
505 * on a column, or because we are refreshing the list:
507 * iSortedColumn >= 0 - User clicked on a column; holds the
508 * old sorting column index.
509 * iSortedColumn < 0 - List being refreshed.
511 if (iSortedColumn
>= 0)
513 /* Invert the sorting direction */
514 bSortAscending
= ((hColumn
.fmt
& HDF_SORTUP
) == 0);
519 * If the sorting state of the column is uninitialized,
520 * initialize it by default to ascending sorting.
522 if ((hColumn
.fmt
& (HDF_SORTUP
| HDF_SORTDOWN
)) == 0)
523 hColumn
.fmt
|= HDF_SORTUP
;
525 /* Keep the same sorting direction */
526 bSortAscending
= ((hColumn
.fmt
& HDF_SORTUP
) != 0);
529 /* Set the new column sorting state */
530 hColumn
.fmt
&= ~(bSortAscending
? HDF_SORTDOWN
: HDF_SORTUP
);
531 hColumn
.fmt
|= (bSortAscending
? HDF_SORTUP
: HDF_SORTDOWN
);
532 Header_SetItem(hHeader
, iSortingColumn
, &hColumn
);
535 SortInfo
.iSortingColumn
= iSortingColumn
;
536 SortInfo
.bSortAscending
= bSortAscending
;
537 return ListView_SortItems(hListView
, CompareFunc
, (LPARAM
)&SortInfo
);
543 BOOL
ListWndNotifyProc(HWND hWnd
, WPARAM wParam
, LPARAM lParam
, BOOL
*Result
)
547 UNREFERENCED_PARAMETER(wParam
);
549 switch (((LPNMHDR
)lParam
)->code
)
551 case LVN_GETDISPINFO
:
552 OnGetDispInfo((NMLVDISPINFO
*)lParam
);
554 case LVN_COLUMNCLICK
:
555 iSortingColumn
= ((LPNMLISTVIEW
)lParam
)->iSubItem
;
556 (void)ListView_Sort(hWnd
, iSortingColumn
, g_iSortedColumn
);
557 g_iSortedColumn
= iSortingColumn
;
562 SendMessageW(hFrameWnd
, WM_COMMAND
, MAKEWPARAM(ID_EDIT_MODIFY
, 0), 0);
566 g_pChildWnd
->nFocusPanel
= 0;
568 case LVN_BEGINLABELEDIT
:
569 Info
= (NMLVDISPINFO
*)lParam
;
572 PLINE_INFO lineinfo
= (PLINE_INFO
)Info
->item
.lParam
;
573 if(!lineinfo
->name
|| !wcscmp(lineinfo
->name
, L
""))
585 case LVN_ENDLABELEDIT
:
586 Info
= (NMLVDISPINFO
*)lParam
;
587 if(Info
&& Info
->item
.pszText
)
589 PLINE_INFO lineinfo
= (PLINE_INFO
)Info
->item
.lParam
;
590 if(!lineinfo
->name
|| !wcscmp(lineinfo
->name
, L
""))
596 if(wcslen(Info
->item
.pszText
) == 0)
598 WCHAR msg
[128], caption
[128];
600 LoadStringW(hInst
, IDS_ERR_RENVAL_TOEMPTY
, msg
, COUNT_OF(msg
));
601 LoadStringW(hInst
, IDS_ERR_RENVAL_CAPTION
, caption
, COUNT_OF(caption
));
602 MessageBoxW(0, msg
, caption
, 0);
611 keyPath
= GetItemPath(g_pChildWnd
->hTreeWnd
, 0, &hKeyRoot
);
612 lResult
= RenameValue(hKeyRoot
, keyPath
, Info
->item
.pszText
, lineinfo
->name
);
613 lineinfo
->name
= realloc(lineinfo
->name
, (wcslen(Info
->item
.pszText
)+1)*sizeof(WCHAR
));
614 if (lineinfo
->name
!= NULL
)
615 wcscpy(lineinfo
->name
, Info
->item
.pszText
);
618 return (lResult
== ERROR_SUCCESS
);
630 HWND
CreateListView(HWND hwndParent
, HMENU id
)
635 /* Get the dimensions of the parent window's client area, and create the list view control. */
636 GetClientRect(hwndParent
, &rcClient
);
637 hwndLV
= CreateWindowExW(WS_EX_CLIENTEDGE
, WC_LISTVIEW
, L
"List View",
638 WS_VISIBLE
| WS_CHILD
| WS_TABSTOP
| LVS_REPORT
| LVS_EDITLABELS
| LVS_SHOWSELALWAYS
,
639 0, 0, rcClient
.right
, rcClient
.bottom
,
640 hwndParent
, id
, hInst
, NULL
);
641 if (!hwndLV
) return NULL
;
643 /* Initialize the image list, and add items to the control. */
644 if (!CreateListColumns(hwndLV
)) goto fail
;
645 if (!InitListViewImageLists(hwndLV
)) goto fail
;
649 DestroyWindow(hwndLV
);
653 void DestroyListView(HWND hwndLV
)
658 count
= ListView_GetItemCount(hwndLV
);
659 for (i
= 0; i
< count
; i
++)
661 item
.mask
= LVIF_PARAM
;
663 (void)ListView_GetItem(hwndLV
, &item
);
664 free(((LINE_INFO
*)item
.lParam
)->name
);
665 HeapFree(GetProcessHeap(), 0, (void*)item
.lParam
);
670 BOOL
RefreshListView(HWND hwndLV
, HKEY hKey
, LPCWSTR keyPath
)
672 DWORD max_sub_key_len
;
673 DWORD max_val_name_len
;
679 BOOL AddedDefault
= FALSE
;
681 if (!hwndLV
) return FALSE
;
683 (void)ListView_EditLabel(hwndLV
, -1);
685 SendMessageW(hwndLV
, WM_SETREDRAW
, FALSE
, 0);
686 DestroyListView(hwndLV
);
688 (void)ListView_DeleteAllItems(hwndLV
);
690 if(!hKey
) return FALSE
;
692 errCode
= RegOpenKeyExW(hKey
, keyPath
, 0, KEY_READ
, &hNewKey
);
693 if (errCode
!= ERROR_SUCCESS
) return FALSE
;
695 /* get size information and resize the buffers if necessary */
696 errCode
= RegQueryInfoKeyW(hNewKey
, NULL
, NULL
, NULL
, NULL
, &max_sub_key_len
, NULL
,
697 &val_count
, &max_val_name_len
, &max_val_size
, NULL
, NULL
);
699 if (errCode
== ERROR_SUCCESS
)
701 WCHAR
* ValName
= HeapAlloc(GetProcessHeap(), 0, ++max_val_name_len
* sizeof(WCHAR
));
702 DWORD dwValNameLen
= max_val_name_len
;
703 BYTE
* ValBuf
= HeapAlloc(GetProcessHeap(), 0, max_val_size
+ sizeof(WCHAR
));
704 DWORD dwValSize
= max_val_size
;
707 /* if (RegQueryValueExW(hNewKey, NULL, NULL, &dwValType, ValBuf, &dwValSize) == ERROR_SUCCESS) { */
708 /* AddEntryToList(hwndLV, L"(Default)", dwValType, ValBuf, dwValSize); */
710 /* dwValSize = max_val_size; */
711 while (RegEnumValueW(hNewKey
, dwIndex
, ValName
, &dwValNameLen
, NULL
, &dwValType
, ValBuf
, &dwValSize
) == ERROR_SUCCESS
)
713 /* Add a terminating 0 character. Usually this is only necessary for strings. */
714 ValBuf
[dwValSize
] = ValBuf
[dwValSize
+ 1] = 0;
716 AddEntryToList(hwndLV
, ValName
, dwValType
, ValBuf
, dwValSize
, -1, TRUE
);
717 dwValNameLen
= max_val_name_len
;
718 dwValSize
= max_val_size
;
721 if(!wcscmp(ValName
, L
""))
726 HeapFree(GetProcessHeap(), 0, ValBuf
);
727 HeapFree(GetProcessHeap(), 0, ValName
);
729 RegCloseKey(hNewKey
);
733 AddEntryToList(hwndLV
, L
"", REG_SZ
, NULL
, 0, 0, FALSE
);
735 c
= ListView_GetItemCount(hwndLV
);
736 for(i
= 0; i
< c
; i
++)
738 ListView_SetItemState(hwndLV
, i
, 0, LVIS_FOCUSED
| LVIS_SELECTED
);
740 ListView_SetItemState(hwndLV
, iListViewSelect
,
741 LVIS_FOCUSED
| LVIS_SELECTED
,
742 LVIS_FOCUSED
| LVIS_SELECTED
);
743 (void)ListView_Sort(hwndLV
, g_iSortedColumn
, -1);
744 SendMessageW(hwndLV
, WM_SETREDRAW
, TRUE
, 0);