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
38 typedef struct tagLINE_INFO
44 } LINE_INFO
, *PLINE_INFO
;
46 /*******************************************************************************
47 * Global and Local Variables:
50 static DWORD g_columnToSort
= ~0UL;
51 static BOOL g_invertSort
= FALSE
;
52 static LPTSTR g_valueName
;
54 #define MAX_LIST_COLUMNS (IDS_LIST_COLUMN_LAST - IDS_LIST_COLUMN_FIRST + 1)
55 static int default_column_widths
[MAX_LIST_COLUMNS
] = { 200, 175, 400 };
56 static int column_alignment
[MAX_LIST_COLUMNS
] = { LVCFMT_LEFT
, LVCFMT_LEFT
, LVCFMT_LEFT
};
58 LPCTSTR
GetValueName(HWND hwndLV
, int iStartAt
)
60 int item
, len
, maxLen
;
65 if (!g_valueName
) g_valueName
= HeapAlloc(GetProcessHeap(), 0, 1024);
66 if (!g_valueName
) return NULL
;
68 maxLen
= HeapSize(GetProcessHeap(), 0, g_valueName
);
69 if (maxLen
== (SIZE_T
) - 1) return NULL
;
71 item
= ListView_GetNextItem(hwndLV
, iStartAt
, LVNI_SELECTED
);
72 if (item
== -1) return NULL
;
73 LVItem
.mask
= LVIF_PARAM
;
77 if(ListView_GetItem(hwndLV
, &LVItem
))
79 lineinfo
= (PLINE_INFO
)LVItem
.lParam
;
85 len
= _tcslen(lineinfo
->name
);
86 if (len
< maxLen
- 1) break;
87 newStr
= HeapReAlloc(GetProcessHeap(), 0, g_valueName
, maxLen
* 2);
88 if (!newStr
) return NULL
;
95 memcpy(g_valueName
, lineinfo
->name
, sizeof(TCHAR
) * (len
+ 1));
99 BOOL
IsDefaultValue(HWND hwndLV
, int i
)
104 Item
.mask
= LVIF_PARAM
;
106 if(ListView_GetItem(hwndLV
, &Item
))
108 lineinfo
= (PLINE_INFO
)Item
.lParam
;
109 return lineinfo
&& (!lineinfo
->name
|| !_tcscmp(lineinfo
->name
, _T("")));
114 /*******************************************************************************
115 * Local module support methods
117 static void AddEntryToList(HWND hwndLV
, LPTSTR Name
, DWORD dwValType
, void* ValBuf
, DWORD dwCount
, int Position
, BOOL ValExists
)
123 linfo
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(LINE_INFO
) + dwCount
);
124 linfo
->dwValType
= dwValType
;
125 linfo
->val_len
= dwCount
;
128 memcpy(&linfo
[1], ValBuf
, dwCount
);
130 linfo
->name
= _tcsdup(Name
);
132 item
.mask
= LVIF_TEXT
| LVIF_PARAM
| LVIF_IMAGE
;
133 item
.iItem
= (Position
== -1 ? 0: Position
);
138 item
.cchTextMax
= _tcslen(item
.pszText
);
139 if (item
.cchTextMax
== 0)
140 item
.pszText
= LPSTR_TEXTCALLBACK
;
142 item
.lParam
= (LPARAM
)linfo
;
148 item
.iImage
= Image_String
;
151 item
.iImage
= Image_Bin
;
155 /* item.lParam = (LPARAM)ValBuf; */
156 #if (_WIN32_IE >= 0x0300)
160 index
= ListView_InsertItem(hwndLV
, &item
);
167 ListView_SetItemText(hwndLV
, index
, 2, ValBuf
);
172 /* load (value not set) string */
173 LoadString(hInst
, IDS_VALUE_NOT_SET
, buffer
, sizeof(buffer
)/sizeof(TCHAR
));
174 ListView_SetItemText(hwndLV
, index
, 2, buffer
);
182 src
= (LPTSTR
)ValBuf
;
183 str
= HeapAlloc(GetProcessHeap(), 0, dwCount
);
187 /* concatenate all srings */
188 while(*src
!= _T('\0'))
191 _tcscat(str
, _T(" "));
192 src
+= _tcslen(src
) + 1;
194 ListView_SetItemText(hwndLV
, index
, 2, str
);
195 HeapFree(GetProcessHeap(), 0, str
);
198 ListView_SetItemText(hwndLV
, index
, 2, _T(""));
201 ListView_SetItemText(hwndLV
, index
, 2, _T(""));
206 if(dwCount
== sizeof(DWORD
))
208 wsprintf(buf
, _T("0x%08X (%d)"), *(DWORD
*)ValBuf
, *(DWORD
*)ValBuf
);
212 LoadString(hInst
, IDS_INVALID_DWORD
, buf
, sizeof(buf
)/sizeof(TCHAR
));
214 ListView_SetItemText(hwndLV
, index
, 2, buf
);
216 /* lpsRes = convertHexToDWORDStr(lpbData, dwLen); */
221 LPBYTE pData
= (LPBYTE
)ValBuf
;
225 strBinary
= HeapAlloc(GetProcessHeap(), 0, (dwCount
* sizeof(TCHAR
) * 3) + 1);
226 for (i
= 0; i
< dwCount
; i
++)
228 wsprintf( strBinary
+ i
*3, _T("%02X "), pData
[i
] );
230 strBinary
[dwCount
* 3] = 0;
231 ListView_SetItemText(hwndLV
, index
, 2, strBinary
);
232 HeapFree(GetProcessHeap(), 0, strBinary
);
237 LoadString(hInst
, IDS_BINARY_EMPTY
, szText
, sizeof(szText
)/sizeof(TCHAR
));
238 ListView_SetItemText(hwndLV
, index
, 2, szText
);
246 static BOOL
CreateListColumns(HWND hWndListView
)
252 /* Create columns. */
253 lvC
.mask
= LVCF_FMT
| LVCF_WIDTH
| LVCF_TEXT
| LVCF_SUBITEM
;
254 lvC
.pszText
= szText
;
256 /* Load the column labels from the resource file. */
257 for (index
= 0; index
< MAX_LIST_COLUMNS
; index
++) {
258 lvC
.iSubItem
= index
;
259 lvC
.cx
= default_column_widths
[index
];
260 lvC
.fmt
= column_alignment
[index
];
261 LoadString(hInst
, IDS_LIST_COLUMN_FIRST
+ index
, szText
, sizeof(szText
)/sizeof(TCHAR
));
262 if (ListView_InsertColumn(hWndListView
, index
, &lvC
) == -1) return FALSE
;
267 static BOOL
InitListViewImageLists(HWND hwndLV
)
269 HIMAGELIST himl
; /* handle to image list */
270 HICON hico
; /* handle to icon */
272 /* Create the image list. */
273 if ((himl
= ImageList_Create(CX_ICON
, CY_ICON
,
274 ILC_MASK
, 0, NUM_ICONS
)) == NULL
)
277 hico
= LoadIcon(hInst
, MAKEINTRESOURCE(IDI_BIN
));
278 Image_Bin
= ImageList_AddIcon(himl
, hico
);
280 hico
= LoadIcon(hInst
, MAKEINTRESOURCE(IDI_STRING
));
281 Image_String
= ImageList_AddIcon(himl
, hico
);
284 /* Fail if not all of the images were added. */
285 if (ImageList_GetImageCount(himl
) < NUM_ICONS
)
290 /* Associate the image list with the tree view control. */
291 ListView_SetImageList(hwndLV
, himl
, LVSIL_SMALL
);
296 /* OnGetDispInfo - processes the LVN_GETDISPINFO notification message. */
298 static void OnGetDispInfo(NMLVDISPINFO
* plvdi
)
300 static TCHAR buffer
[200];
302 plvdi
->item
.pszText
= NULL
;
303 plvdi
->item
.cchTextMax
= 0;
305 switch (plvdi
->item
.iSubItem
) {
307 LoadString(hInst
, IDS_DEFAULT_VALUE_NAME
, buffer
, sizeof(buffer
)/sizeof(TCHAR
));
308 plvdi
->item
.pszText
= buffer
;
311 switch (((LINE_INFO
*)plvdi
->item
.lParam
)->dwValType
) {
313 plvdi
->item
.pszText
= _T("REG_SZ");
316 plvdi
->item
.pszText
= _T("REG_EXPAND_SZ");
319 plvdi
->item
.pszText
= _T("REG_BINARY");
322 plvdi
->item
.pszText
= _T("REG_DWORD");
324 case REG_DWORD_BIG_ENDIAN
:
325 plvdi
->item
.pszText
= _T("REG_DWORD_BIG_ENDIAN");
328 plvdi
->item
.pszText
= _T("REG_MULTI_SZ");
331 plvdi
->item
.pszText
= _T("REG_LINK");
333 case REG_RESOURCE_LIST
:
334 plvdi
->item
.pszText
= _T("REG_RESOURCE_LIST");
336 case REG_FULL_RESOURCE_DESCRIPTOR
:
337 plvdi
->item
.pszText
= _T("REG_FULL_RESOURCE_DESCRIPTOR");
339 case REG_RESOURCE_REQUIREMENTS_LIST
:
340 plvdi
->item
.pszText
= _T("REG_RESOURCE_REQUIREMENTS_LIST");
343 plvdi
->item
.pszText
= _T("REG_NONE");
347 LoadString(hInst
, IDS_UNKNOWN_TYPE
, buf2
, sizeof(buf2
)/sizeof(TCHAR
));
348 wsprintf(buffer
, buf2
, ((LINE_INFO
*)plvdi
->item
.lParam
)->dwValType
);
349 plvdi
->item
.pszText
= buffer
;
355 plvdi
->item
.pszText
= _T("");
360 static int CALLBACK
CompareFunc(LPARAM lParam1
, LPARAM lParam2
, LPARAM lParamSort
)
363 l
= (LINE_INFO
*)lParam1
;
364 r
= (LINE_INFO
*)lParam2
;
366 if (g_columnToSort
== ~0UL)
369 if (g_columnToSort
== 1 && l
->dwValType
!= r
->dwValType
)
370 return g_invertSort
? (int)r
->dwValType
- (int)l
->dwValType
: (int)l
->dwValType
- (int)r
->dwValType
;
371 if (g_columnToSort
== 2) {
372 /* FIXME: Sort on value */
374 return g_invertSort
? _tcscmp(r
->name
, l
->name
) : _tcscmp(l
->name
, r
->name
);
377 BOOL
ListWndNotifyProc(HWND hWnd
, WPARAM wParam
, LPARAM lParam
, BOOL
*Result
)
381 switch (((LPNMHDR
)lParam
)->code
) {
382 case LVN_GETDISPINFO
:
383 OnGetDispInfo((NMLVDISPINFO
*)lParam
);
385 case LVN_COLUMNCLICK
:
386 if (g_columnToSort
== ((LPNMLISTVIEW
)lParam
)->iSubItem
)
387 g_invertSort
= !g_invertSort
;
389 g_columnToSort
= ((LPNMLISTVIEW
)lParam
)->iSubItem
;
390 g_invertSort
= FALSE
;
393 ListView_SortItems(hWnd
, CompareFunc
, (WPARAM
)hWnd
);
398 SendMessage(hFrameWnd
, WM_COMMAND
, MAKEWPARAM(ID_EDIT_MODIFY
, 0), 0);
402 g_pChildWnd
->nFocusPanel
= 0;
404 case LVN_BEGINLABELEDIT
:
407 Info
= (NMLVDISPINFO
*)lParam
;
410 lineinfo
= (PLINE_INFO
)Info
->item
.lParam
;
411 if(!lineinfo
->name
|| !_tcscmp(lineinfo
->name
, _T("")))
424 case LVN_ENDLABELEDIT
:
427 Info
= (NMLVDISPINFO
*)lParam
;
428 if(Info
&& Info
->item
.pszText
)
430 lineinfo
= (PLINE_INFO
)Info
->item
.lParam
;
431 if(!lineinfo
->name
|| !_tcscmp(lineinfo
->name
, _T("")))
437 //if((ret = RenameValue(lineinfo->name, Info->item.pszText)) != ERROR_SUCCESS)
439 TCHAR msg
[128], caption
[128];
441 LoadString(hInst
, IDS_ERR_RENVAL_CAPTION
, caption
, sizeof(caption
)/sizeof(TCHAR
));
442 if(_tcslen(Info
->item
.pszText
) == 0)
444 LoadString(hInst
, IDS_ERR_RENVAL_TOEMPTY
, msg
, sizeof(msg
)/sizeof(TCHAR
));
447 _stprintf(msg
, _T("rename from %s to %s"), lineinfo
->name
, Info
->item
.pszText
);
448 MessageBox(0, msg
, NULL
, 0);
462 HWND
CreateListView(HWND hwndParent
, int id
)
467 /* Get the dimensions of the parent window's client area, and create the list view control. */
468 GetClientRect(hwndParent
, &rcClient
);
469 hwndLV
= CreateWindowEx(WS_EX_CLIENTEDGE
, WC_LISTVIEW
, _T("List View"),
470 WS_VISIBLE
| WS_CHILD
| WS_TABSTOP
| LVS_REPORT
| LVS_EDITLABELS
,
471 0, 0, rcClient
.right
, rcClient
.bottom
,
472 hwndParent
, (HMENU
)id
, hInst
, NULL
);
473 if (!hwndLV
) return NULL
;
475 /* Initialize the image list, and add items to the control. */
476 if (!CreateListColumns(hwndLV
)) goto fail
;
477 if (!InitListViewImageLists(hwndLV
)) goto fail
;
481 DestroyWindow(hwndLV
);
485 BOOL
RefreshListView(HWND hwndLV
, HKEY hKey
, LPCTSTR keyPath
)
487 DWORD max_sub_key_len
;
488 DWORD max_val_name_len
;
495 BOOL AddedDefault
= FALSE
;
497 if (!hwndLV
) return FALSE
;
499 ListView_EditLabel(hwndLV
, -1);
501 SendMessage(hwndLV
, WM_SETREDRAW
, FALSE
, 0);
502 count
= ListView_GetItemCount(hwndLV
);
503 for (i
= 0; i
< count
; i
++) {
504 item
.mask
= LVIF_PARAM
;
506 ListView_GetItem(hwndLV
, &item
);
507 free(((LINE_INFO
*)item
.lParam
)->name
);
508 HeapFree(GetProcessHeap(), 0, (void*)item
.lParam
);
510 g_columnToSort
= ~0UL;
511 ListView_DeleteAllItems(hwndLV
);
513 if(!hKey
) return FALSE
;
515 errCode
= RegOpenKeyEx(hKey
, keyPath
, 0, KEY_READ
, &hNewKey
);
516 if (errCode
!= ERROR_SUCCESS
) return FALSE
;
518 /* get size information and resize the buffers if necessary */
519 errCode
= RegQueryInfoKey(hNewKey
, NULL
, NULL
, NULL
, NULL
, &max_sub_key_len
, NULL
,
520 &val_count
, &max_val_name_len
, &max_val_size
, NULL
, NULL
);
522 #define BUF_HEAD_SPACE 2 /* FIXME: check why this is required with ROS ??? */
524 if (errCode
== ERROR_SUCCESS
) {
525 TCHAR
* ValName
= HeapAlloc(GetProcessHeap(), 0, ++max_val_name_len
* sizeof(TCHAR
) + BUF_HEAD_SPACE
);
526 DWORD dwValNameLen
= max_val_name_len
;
527 BYTE
* ValBuf
= HeapAlloc(GetProcessHeap(), 0, ++max_val_size
/* + BUF_HEAD_SPACE*/);
528 DWORD dwValSize
= max_val_size
;
531 /* if (RegQueryValueEx(hNewKey, NULL, NULL, &dwValType, ValBuf, &dwValSize) == ERROR_SUCCESS) { */
532 /* AddEntryToList(hwndLV, _T("(Default)"), dwValType, ValBuf, dwValSize); */
534 /* dwValSize = max_val_size; */
535 while (RegEnumValue(hNewKey
, dwIndex
, ValName
, &dwValNameLen
, NULL
, &dwValType
, ValBuf
, &dwValSize
) == ERROR_SUCCESS
) {
536 ValBuf
[dwValSize
] = 0;
537 AddEntryToList(hwndLV
, ValName
, dwValType
, ValBuf
, dwValSize
, -1, TRUE
);
538 dwValNameLen
= max_val_name_len
;
539 dwValSize
= max_val_size
;
542 if(!_tcscmp(ValName
, _T("")))
547 HeapFree(GetProcessHeap(), 0, ValBuf
);
548 HeapFree(GetProcessHeap(), 0, ValName
);
552 AddEntryToList(hwndLV
, _T(""), REG_SZ
, NULL
, 0, 0, FALSE
);
554 ListView_SortItems(hwndLV
, CompareFunc
, (WPARAM
)hwndLV
);
555 RegCloseKey(hNewKey
);
556 SendMessage(hwndLV
, WM_SETREDRAW
, TRUE
, 0);