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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
39 typedef struct tagLINE_INFO
45 } LINE_INFO
, *PLINE_INFO
;
47 /*******************************************************************************
48 * Global and Local Variables:
51 static DWORD g_columnToSort
= ~0UL;
52 static BOOL g_invertSort
= FALSE
;
53 static LPTSTR g_valueName
;
55 #define MAX_LIST_COLUMNS (IDS_LIST_COLUMN_LAST - IDS_LIST_COLUMN_FIRST + 1)
56 static int default_column_widths
[MAX_LIST_COLUMNS
] = { 200, 175, 400 };
57 static int column_alignment
[MAX_LIST_COLUMNS
] = { LVCFMT_LEFT
, LVCFMT_LEFT
, LVCFMT_LEFT
};
59 LPCTSTR
GetValueName(HWND hwndLV
, int iStartAt
)
61 int item
, len
, maxLen
;
66 if (!g_valueName
) g_valueName
= HeapAlloc(GetProcessHeap(), 0, 1024);
67 if (!g_valueName
) return NULL
;
69 maxLen
= HeapSize(GetProcessHeap(), 0, g_valueName
);
70 if (maxLen
== -1) return NULL
;
72 item
= ListView_GetNextItem(hwndLV
, iStartAt
, LVNI_SELECTED
);
73 if (item
== -1) return NULL
;
74 LVItem
.mask
= LVIF_PARAM
;
78 if(ListView_GetItem(hwndLV
, &LVItem
))
80 lineinfo
= (PLINE_INFO
)LVItem
.lParam
;
86 len
= _tcslen(lineinfo
->name
);
87 if (len
< maxLen
- 1) break;
88 newStr
= HeapReAlloc(GetProcessHeap(), 0, g_valueName
, maxLen
* 2);
89 if (!newStr
) return NULL
;
96 memcpy(g_valueName
, lineinfo
->name
, sizeof(TCHAR
) * (len
+ 1));
100 BOOL
IsDefaultValue(HWND hwndLV
, int i
)
105 Item
.mask
= LVIF_PARAM
;
107 if(ListView_GetItem(hwndLV
, &Item
))
109 lineinfo
= (PLINE_INFO
)Item
.lParam
;
110 return lineinfo
&& (!lineinfo
->name
|| !_tcscmp(lineinfo
->name
, _T("")));
115 /*******************************************************************************
116 * Local module support methods
118 static void AddEntryToList(HWND hwndLV
, LPTSTR Name
, DWORD dwValType
, void* ValBuf
, DWORD dwCount
, int Position
, BOOL ValExists
)
124 linfo
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(LINE_INFO
) + dwCount
);
125 linfo
->dwValType
= dwValType
;
126 linfo
->val_len
= dwCount
;
129 memcpy(&linfo
[1], ValBuf
, dwCount
);
131 linfo
->name
= _tcsdup(Name
);
133 item
.mask
= LVIF_TEXT
| LVIF_PARAM
| LVIF_IMAGE
;
134 item
.iItem
= (Position
== -1 ? 0: Position
);
139 item
.cchTextMax
= _tcslen(item
.pszText
);
140 if (item
.cchTextMax
== 0)
141 item
.pszText
= LPSTR_TEXTCALLBACK
;
143 item
.lParam
= (LPARAM
)linfo
;
149 item
.iImage
= Image_String
;
152 item
.iImage
= Image_Bin
;
156 /* item.lParam = (LPARAM)ValBuf; */
157 #if (_WIN32_IE >= 0x0300)
161 index
= ListView_InsertItem(hwndLV
, &item
);
168 ListView_SetItemText(hwndLV
, index
, 2, ValBuf
);
173 /* load (value not set) string */
174 LoadString(hInst
, IDS_VALUE_NOT_SET
, buffer
, sizeof(buffer
)/sizeof(TCHAR
));
175 ListView_SetItemText(hwndLV
, index
, 2, buffer
);
183 src
= (LPTSTR
)ValBuf
;
184 str
= HeapAlloc(GetProcessHeap(), 0, dwCount
);
188 /* concatenate all srings */
189 while(*src
!= _T('\0'))
192 _tcscat(str
, _T(" "));
193 src
+= _tcslen(src
) + 1;
195 ListView_SetItemText(hwndLV
, index
, 2, str
);
196 HeapFree(GetProcessHeap(), 0, str
);
199 ListView_SetItemText(hwndLV
, index
, 2, _T(""));
202 ListView_SetItemText(hwndLV
, index
, 2, _T(""));
207 if(dwCount
== sizeof(DWORD
))
209 wsprintf(buf
, _T("0x%08X (%d)"), *(DWORD
*)ValBuf
, *(DWORD
*)ValBuf
);
213 LoadString(hInst
, IDS_INVALID_DWORD
, buf
, sizeof(buf
)/sizeof(TCHAR
));
215 ListView_SetItemText(hwndLV
, index
, 2, buf
);
217 /* lpsRes = convertHexToDWORDStr(lpbData, dwLen); */
222 LPBYTE pData
= (LPBYTE
)ValBuf
;
226 strBinary
= HeapAlloc(GetProcessHeap(), 0, (dwCount
* sizeof(TCHAR
) * 3) + 1);
227 for (i
= 0; i
< dwCount
; i
++)
229 wsprintf( strBinary
+ i
*3, _T("%02X "), pData
[i
] );
231 strBinary
[dwCount
* 3] = 0;
232 ListView_SetItemText(hwndLV
, index
, 2, strBinary
);
233 HeapFree(GetProcessHeap(), 0, strBinary
);
238 LoadString(hInst
, IDS_BINARY_EMPTY
, szText
, sizeof(szText
)/sizeof(TCHAR
));
239 ListView_SetItemText(hwndLV
, index
, 2, szText
);
247 static BOOL
CreateListColumns(HWND hWndListView
)
253 /* Create columns. */
254 lvC
.mask
= LVCF_FMT
| LVCF_WIDTH
| LVCF_TEXT
| LVCF_SUBITEM
;
255 lvC
.pszText
= szText
;
257 /* Load the column labels from the resource file. */
258 for (index
= 0; index
< MAX_LIST_COLUMNS
; index
++) {
259 lvC
.iSubItem
= index
;
260 lvC
.cx
= default_column_widths
[index
];
261 lvC
.fmt
= column_alignment
[index
];
262 LoadString(hInst
, IDS_LIST_COLUMN_FIRST
+ index
, szText
, sizeof(szText
)/sizeof(TCHAR
));
263 if (ListView_InsertColumn(hWndListView
, index
, &lvC
) == -1) return FALSE
;
268 static BOOL
InitListViewImageLists(HWND hwndLV
)
270 HIMAGELIST himl
; /* handle to image list */
271 HICON hico
; /* handle to icon */
273 /* Create the image list. */
274 if ((himl
= ImageList_Create(CX_ICON
, CY_ICON
,
275 ILC_MASK
, 0, NUM_ICONS
)) == NULL
)
278 hico
= LoadIcon(hInst
, MAKEINTRESOURCE(IDI_BIN
));
279 Image_Bin
= ImageList_AddIcon(himl
, hico
);
281 hico
= LoadIcon(hInst
, MAKEINTRESOURCE(IDI_STRING
));
282 Image_String
= ImageList_AddIcon(himl
, hico
);
285 /* Fail if not all of the images were added. */
286 if (ImageList_GetImageCount(himl
) < NUM_ICONS
)
291 /* Associate the image list with the tree view control. */
292 ListView_SetImageList(hwndLV
, himl
, LVSIL_SMALL
);
297 /* OnGetDispInfo - processes the LVN_GETDISPINFO notification message. */
299 static void OnGetDispInfo(NMLVDISPINFO
* plvdi
)
301 static TCHAR buffer
[200];
303 plvdi
->item
.pszText
= NULL
;
304 plvdi
->item
.cchTextMax
= 0;
306 switch (plvdi
->item
.iSubItem
) {
308 LoadString(hInst
, IDS_DEFAULT_VALUE_NAME
, buffer
, sizeof(buffer
)/sizeof(TCHAR
));
309 plvdi
->item
.pszText
= buffer
;
312 switch (((LINE_INFO
*)plvdi
->item
.lParam
)->dwValType
) {
314 plvdi
->item
.pszText
= _T("REG_NONE");
317 plvdi
->item
.pszText
= _T("REG_SZ");
320 plvdi
->item
.pszText
= _T("REG_EXPAND_SZ");
323 plvdi
->item
.pszText
= _T("REG_BINARY");
325 case REG_DWORD
: /* REG_DWORD_LITTLE_ENDIAN */
326 plvdi
->item
.pszText
= _T("REG_DWORD");
328 case REG_DWORD_BIG_ENDIAN
:
329 plvdi
->item
.pszText
= _T("REG_DWORD_BIG_ENDIAN");
332 plvdi
->item
.pszText
= _T("REG_LINK");
335 plvdi
->item
.pszText
= _T("REG_MULTI_SZ");
337 case REG_RESOURCE_LIST
:
338 plvdi
->item
.pszText
= _T("REG_RESOURCE_LIST");
340 case REG_FULL_RESOURCE_DESCRIPTOR
:
341 plvdi
->item
.pszText
= _T("REG_FULL_RESOURCE_DESCRIPTOR");
343 case REG_RESOURCE_REQUIREMENTS_LIST
:
344 plvdi
->item
.pszText
= _T("REG_RESOURCE_REQUIREMENTS_LIST");
346 case REG_QWORD
: /* REG_QWORD_LITTLE_ENDIAN */
347 plvdi
->item
.pszText
= _T("REG_QWORD");
351 LoadString(hInst
, IDS_UNKNOWN_TYPE
, buf2
, sizeof(buf2
)/sizeof(TCHAR
));
352 wsprintf(buffer
, buf2
, ((LINE_INFO
*)plvdi
->item
.lParam
)->dwValType
);
353 plvdi
->item
.pszText
= buffer
;
359 plvdi
->item
.pszText
= _T("");
364 static int CALLBACK
CompareFunc(LPARAM lParam1
, LPARAM lParam2
, LPARAM lParamSort
)
367 l
= (LINE_INFO
*)lParam1
;
368 r
= (LINE_INFO
*)lParam2
;
370 if (g_columnToSort
== ~0UL)
373 if (g_columnToSort
== 1 && l
->dwValType
!= r
->dwValType
)
374 return g_invertSort
? (int)r
->dwValType
- (int)l
->dwValType
: (int)l
->dwValType
- (int)r
->dwValType
;
375 if (g_columnToSort
== 2) {
376 /* FIXME: Sort on value */
378 return g_invertSort
? _tcscmp(r
->name
, l
->name
) : _tcscmp(l
->name
, r
->name
);
381 BOOL
ListWndNotifyProc(HWND hWnd
, WPARAM wParam
, LPARAM lParam
, BOOL
*Result
)
385 switch (((LPNMHDR
)lParam
)->code
) {
386 case LVN_GETDISPINFO
:
387 OnGetDispInfo((NMLVDISPINFO
*)lParam
);
389 case LVN_COLUMNCLICK
:
390 if (g_columnToSort
== (DWORD
)((LPNMLISTVIEW
)lParam
)->iSubItem
)
391 g_invertSort
= !g_invertSort
;
393 g_columnToSort
= ((LPNMLISTVIEW
)lParam
)->iSubItem
;
394 g_invertSort
= FALSE
;
397 ListView_SortItems(hWnd
, CompareFunc
, (WPARAM
)hWnd
);
402 SendMessage(hFrameWnd
, WM_COMMAND
, MAKEWPARAM(ID_EDIT_MODIFY
, 0), 0);
406 g_pChildWnd
->nFocusPanel
= 0;
408 case LVN_BEGINLABELEDIT
:
411 Info
= (NMLVDISPINFO
*)lParam
;
414 lineinfo
= (PLINE_INFO
)Info
->item
.lParam
;
415 if(!lineinfo
->name
|| !_tcscmp(lineinfo
->name
, _T("")))
428 case LVN_ENDLABELEDIT
:
431 Info
= (NMLVDISPINFO
*)lParam
;
432 if(Info
&& Info
->item
.pszText
)
434 lineinfo
= (PLINE_INFO
)Info
->item
.lParam
;
435 if(!lineinfo
->name
|| !_tcscmp(lineinfo
->name
, _T("")))
441 //if((ret = RenameValue(lineinfo->name, Info->item.pszText)) != ERROR_SUCCESS)
443 TCHAR msg
[128], caption
[128];
445 LoadString(hInst
, IDS_ERR_RENVAL_CAPTION
, caption
, sizeof(caption
)/sizeof(TCHAR
));
446 if(_tcslen(Info
->item
.pszText
) == 0)
448 LoadString(hInst
, IDS_ERR_RENVAL_TOEMPTY
, msg
, sizeof(msg
)/sizeof(TCHAR
));
449 MessageBox(0, msg
, NULL
, 0);
457 keyPath
= GetItemPath(g_pChildWnd
->hTreeWnd
, 0, &hKeyRoot
);
458 lResult
= RegRenameValue(hKeyRoot
, keyPath
, Info
->item
.pszText
, lineinfo
->name
);
460 return (lResult
== ERROR_SUCCESS
);
474 HWND
CreateListView(HWND hwndParent
, int id
)
479 /* Get the dimensions of the parent window's client area, and create the list view control. */
480 GetClientRect(hwndParent
, &rcClient
);
481 hwndLV
= CreateWindowEx(WS_EX_CLIENTEDGE
, WC_LISTVIEW
, _T("List View"),
482 WS_VISIBLE
| WS_CHILD
| WS_TABSTOP
| LVS_REPORT
| LVS_EDITLABELS
,
483 0, 0, rcClient
.right
, rcClient
.bottom
,
484 hwndParent
, (HMENU
)id
, hInst
, NULL
);
485 if (!hwndLV
) return NULL
;
487 /* Initialize the image list, and add items to the control. */
488 if (!CreateListColumns(hwndLV
)) goto fail
;
489 if (!InitListViewImageLists(hwndLV
)) goto fail
;
493 DestroyWindow(hwndLV
);
497 BOOL
RefreshListView(HWND hwndLV
, HKEY hKey
, LPCTSTR keyPath
)
499 DWORD max_sub_key_len
;
500 DWORD max_val_name_len
;
507 BOOL AddedDefault
= FALSE
;
509 if (!hwndLV
) return FALSE
;
511 ListView_EditLabel(hwndLV
, -1);
513 SendMessage(hwndLV
, WM_SETREDRAW
, FALSE
, 0);
514 count
= ListView_GetItemCount(hwndLV
);
515 for (i
= 0; i
< count
; i
++) {
516 item
.mask
= LVIF_PARAM
;
518 ListView_GetItem(hwndLV
, &item
);
519 free(((LINE_INFO
*)item
.lParam
)->name
);
520 HeapFree(GetProcessHeap(), 0, (void*)item
.lParam
);
522 g_columnToSort
= ~0UL;
523 ListView_DeleteAllItems(hwndLV
);
525 if(!hKey
) return FALSE
;
527 errCode
= RegOpenKeyEx(hKey
, keyPath
, 0, KEY_READ
, &hNewKey
);
528 if (errCode
!= ERROR_SUCCESS
) return FALSE
;
530 /* get size information and resize the buffers if necessary */
531 errCode
= RegQueryInfoKey(hNewKey
, NULL
, NULL
, NULL
, NULL
, &max_sub_key_len
, NULL
,
532 &val_count
, &max_val_name_len
, &max_val_size
, NULL
, NULL
);
534 #define BUF_HEAD_SPACE 2 /* FIXME: check why this is required with ROS ??? */
536 if (errCode
== ERROR_SUCCESS
) {
537 TCHAR
* ValName
= HeapAlloc(GetProcessHeap(), 0, ++max_val_name_len
* sizeof(TCHAR
) + BUF_HEAD_SPACE
);
538 DWORD dwValNameLen
= max_val_name_len
;
539 BYTE
* ValBuf
= HeapAlloc(GetProcessHeap(), 0, ++max_val_size
/* + BUF_HEAD_SPACE*/);
540 DWORD dwValSize
= max_val_size
;
543 /* if (RegQueryValueEx(hNewKey, NULL, NULL, &dwValType, ValBuf, &dwValSize) == ERROR_SUCCESS) { */
544 /* AddEntryToList(hwndLV, _T("(Default)"), dwValType, ValBuf, dwValSize); */
546 /* dwValSize = max_val_size; */
547 while (RegEnumValue(hNewKey
, dwIndex
, ValName
, &dwValNameLen
, NULL
, &dwValType
, ValBuf
, &dwValSize
) == ERROR_SUCCESS
) {
548 ValBuf
[dwValSize
] = 0;
549 AddEntryToList(hwndLV
, ValName
, dwValType
, ValBuf
, dwValSize
, -1, TRUE
);
550 dwValNameLen
= max_val_name_len
;
551 dwValSize
= max_val_size
;
554 if(!_tcscmp(ValName
, _T("")))
559 HeapFree(GetProcessHeap(), 0, ValBuf
);
560 HeapFree(GetProcessHeap(), 0, ValName
);
564 AddEntryToList(hwndLV
, _T(""), REG_SZ
, NULL
, 0, 0, FALSE
);
566 ListView_SortItems(hwndLV
, CompareFunc
, (WPARAM
)hwndLV
);
567 RegCloseKey(hNewKey
);
568 SendMessage(hwndLV
, WM_SETREDRAW
, TRUE
, 0);