4 * Copyright (C) 2002 Robert Dickenson <robd@reactos.org>
5 * Copyright (C) 2008 Alexander N. Sørnes <alex@thehandofagony.com>
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #define WIN32_LEAN_AND_MEAN /* Exclude rarely-used stuff from Windows headers */
24 #define NONAMELESSUNION
30 #include <wine/debug.h>
36 WINE_DEFAULT_DEBUG_CHANNEL(regedit
);
38 /* Global variables and constants */
39 /* Image_Open, Image_Closed, and Image_Root - integer variables for indexes of the images. */
40 /* CX_BITMAP and CY_BITMAP - width and height of an icon. */
41 /* NUM_BITMAPS - number of bitmaps to add to the image list. */
48 static BOOL
UpdateExpandingTree(HWND hwndTV
, HTREEITEM hItem
, int state
);
50 static BOOL
get_item_path(HWND hwndTV
, HTREEITEM hItem
, HKEY
* phKey
, LPWSTR
* pKeyPath
, int* pPathLen
, int* pMaxChars
)
57 item
.mask
= TVIF_PARAM
;
59 if (!TreeView_GetItemW(hwndTV
, &item
)) return FALSE
;
62 /* found root key with valid key value */
63 *phKey
= (HKEY
)item
.lParam
;
67 hParent
= (HTREEITEM
)SendMessageW(hwndTV
, TVM_GETNEXTITEM
, TVGN_PARENT
, (LPARAM
)hItem
);
68 if(!get_item_path(hwndTV
, hParent
, phKey
, pKeyPath
, pPathLen
, pMaxChars
)) return FALSE
;
70 (*pKeyPath
)[*pPathLen
] = '\\';
75 item
.mask
= TVIF_TEXT
;
77 item
.pszText
= *pKeyPath
+ *pPathLen
;
78 item
.cchTextMax
= maxChars
= *pMaxChars
- *pPathLen
;
79 if (!TreeView_GetItemW(hwndTV
, &item
)) return FALSE
;
80 chars
= lstrlenW(item
.pszText
);
81 if (chars
< maxChars
- 1) {
85 newStr
= HeapReAlloc(GetProcessHeap(), 0, *pKeyPath
, *pMaxChars
* 2);
86 if (!newStr
) return FALSE
;
94 LPWSTR
GetItemPath(HWND hwndTV
, HTREEITEM hItem
, HKEY
* phRootKey
)
96 int pathLen
= 0, maxLen
= 1024;
100 hItem
= (HTREEITEM
)SendMessageW(hwndTV
, TVM_GETNEXTITEM
, TVGN_CARET
, 0);
101 if (!hItem
) return NULL
;
104 pathBuffer
= HeapAlloc(GetProcessHeap(), 0, maxLen
*sizeof(WCHAR
));
105 if (!pathBuffer
) return NULL
;
107 if (!get_item_path(hwndTV
, hItem
, phRootKey
, &pathBuffer
, &pathLen
, &maxLen
)) return NULL
;
111 static LPWSTR
get_path_component(LPCWSTR
*lplpKeyName
) {
112 LPCWSTR lpPos
= *lplpKeyName
;
113 LPWSTR lpResult
= NULL
;
117 while(*lpPos
&& *lpPos
!= '\\')
119 if (*lpPos
&& lpPos
== *lplpKeyName
)
121 len
= lpPos
+1-(*lplpKeyName
);
122 lpResult
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
123 if (!lpResult
) /* that would be very odd */
125 lstrcpynW(lpResult
, *lplpKeyName
, len
);
126 *lplpKeyName
= *lpPos
? lpPos
+1 : NULL
;
130 HTREEITEM
FindPathInTree(HWND hwndTV
, LPCWSTR lpKeyName
) {
132 WCHAR buf
[261]; /* tree view has 260 character limitation on item name */
133 HTREEITEM hRoot
, hItem
, hOldItem
;
137 hRoot
= (HTREEITEM
)SendMessageW(hwndTV
, TVM_GETNEXTITEM
, TVGN_ROOT
, 0);
139 SendMessageW(hwndTV
, TVM_EXPAND
, TVE_EXPAND
, (LPARAM
)hItem
);
140 hItem
= (HTREEITEM
)SendMessageW(hwndTV
, TVM_GETNEXTITEM
, TVGN_CHILD
, (LPARAM
)hItem
);
144 LPWSTR lpItemName
= get_path_component(&lpKeyName
);
148 tvi
.mask
= TVIF_TEXT
| TVIF_HANDLE
;
151 tvi
.cchTextMax
= 260;
152 SendMessageW(hwndTV
, TVM_GETITEMW
, 0, (LPARAM
) &tvi
);
153 if (!lstrcmpiW(tvi
.pszText
, lpItemName
)) {
155 SendMessageW(hwndTV
, TVM_EXPAND
, TVE_EXPAND
, (LPARAM
)hItem
);
158 HeapFree(GetProcessHeap(), 0, lpItemName
);
162 hItem
= (HTREEITEM
)SendMessageW(hwndTV
, TVM_GETNEXTITEM
, TVGN_CHILD
, (LPARAM
)hItem
);
165 hItem
= (HTREEITEM
)SendMessageW(hwndTV
, TVM_GETNEXTITEM
, TVGN_NEXT
, (LPARAM
)hItem
);
167 HeapFree(GetProcessHeap(), 0, lpItemName
);
169 return valid_path
? hOldItem
: hRoot
;
172 return valid_path
? hItem
: hRoot
;
176 BOOL
DeleteNode(HWND hwndTV
, HTREEITEM hItem
)
178 if (!hItem
) hItem
= (HTREEITEM
)SendMessageW(hwndTV
, TVM_GETNEXTITEM
, TVGN_CARET
, 0);
179 if (!hItem
) return FALSE
;
180 return (BOOL
)SendMessageW(hwndTV
, TVM_DELETEITEM
, 0, (LPARAM
)hItem
);
183 /* Add an entry to the tree. Only give hKey for root nodes (HKEY_ constants) */
184 static HTREEITEM
AddEntryToTree(HWND hwndTV
, HTREEITEM hParent
, LPWSTR label
, HKEY hKey
, DWORD dwChildren
)
186 TVINSERTSTRUCTW tvins
;
189 if (RegQueryInfoKeyW(hKey
, 0, 0, 0, &dwChildren
, 0, 0, 0, 0, 0, 0, 0) != ERROR_SUCCESS
) {
194 tvins
.u
.item
.mask
= TVIF_TEXT
| TVIF_IMAGE
| TVIF_SELECTEDIMAGE
| TVIF_CHILDREN
| TVIF_PARAM
;
195 tvins
.u
.item
.pszText
= label
;
196 tvins
.u
.item
.cchTextMax
= lstrlenW(label
);
197 tvins
.u
.item
.iImage
= Image_Closed
;
198 tvins
.u
.item
.iSelectedImage
= Image_Open
;
199 tvins
.u
.item
.cChildren
= dwChildren
;
200 tvins
.u
.item
.lParam
= (LPARAM
)hKey
;
201 tvins
.hInsertAfter
= hKey
? TVI_LAST
: TVI_SORT
;
202 tvins
.hParent
= hParent
;
204 return TreeView_InsertItemW(hwndTV
, &tvins
);
207 static BOOL
match_string(LPCWSTR sstring1
, LPCWSTR sstring2
, int mode
)
209 if (mode
& SEARCH_WHOLE
)
210 return !lstrcmpiW(sstring1
, sstring2
);
212 return NULL
!= StrStrIW(sstring1
, sstring2
);
215 static BOOL
match_item(HWND hwndTV
, HTREEITEM hItem
, LPCWSTR sstring
, int mode
, int *row
)
218 WCHAR keyname
[KEY_MAX_LEN
];
219 item
.mask
= TVIF_TEXT
;
221 item
.pszText
= keyname
;
222 item
.cchTextMax
= KEY_MAX_LEN
;
223 if (!TreeView_GetItemW(hwndTV
, &item
)) return FALSE
;
224 if ((mode
& SEARCH_KEYS
) && match_string(keyname
, sstring
, mode
)) {
229 if (mode
& (SEARCH_VALUES
| SEARCH_CONTENT
)) {
231 WCHAR
*valName
, *KeyPath
;
233 DWORD lenName
, lenNameMax
, lenValueMax
;
234 WCHAR
*buffer
= NULL
;
236 KeyPath
= GetItemPath(hwndTV
, hItem
, &hRoot
);
238 if (!KeyPath
|| !hRoot
)
241 if (RegOpenKeyExW(hRoot
, KeyPath
, 0, KEY_READ
, &hKey
) != ERROR_SUCCESS
) {
242 HeapFree(GetProcessHeap(), 0, KeyPath
);
246 HeapFree(GetProcessHeap(), 0, KeyPath
);
248 if (ERROR_SUCCESS
!= RegQueryInfoKeyW(hKey
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, &lenNameMax
, &lenValueMax
, NULL
, NULL
))
251 lenName
= ++lenNameMax
;
252 if (!(valName
= HeapAlloc(GetProcessHeap(), 0, lenName
* sizeof(valName
[0]) )))
256 /* RegEnumValue won't return empty default value, so fake it when dealing with *row,
257 which corresponds to list view rows, not value ids */
258 if (ERROR_SUCCESS
== RegEnumValueW(hKey
, 0, valName
, &lenName
, NULL
, NULL
, NULL
, NULL
) && *valName
)
264 DWORD lenValue
= 0, type
= 0;
265 lenName
= lenNameMax
;
267 if (ERROR_SUCCESS
!= RegEnumValueW(hKey
,
268 i
, valName
, &lenName
, NULL
, &type
, NULL
, NULL
))
271 if (mode
& SEARCH_VALUES
) {
272 if (match_string(valName
, sstring
, mode
)) {
273 HeapFree(GetProcessHeap(), 0, valName
);
274 HeapFree(GetProcessHeap(), 0, buffer
);
281 if ((mode
& SEARCH_CONTENT
) && (type
== REG_EXPAND_SZ
|| type
== REG_SZ
)) {
283 buffer
= HeapAlloc(GetProcessHeap(), 0, lenValueMax
);
286 lenName
= lenNameMax
;
287 lenValue
= lenValueMax
;
288 if (ERROR_SUCCESS
!= RegEnumValueW(hKey
, i
, valName
, &lenName
, NULL
, &type
, (LPBYTE
)buffer
, &lenValue
))
290 if (match_string(buffer
, sstring
, mode
)) {
291 HeapFree(GetProcessHeap(), 0, valName
);
292 HeapFree(GetProcessHeap(), 0, buffer
);
301 HeapFree(GetProcessHeap(), 0, valName
);
302 HeapFree(GetProcessHeap(), 0, buffer
);
308 HTREEITEM
FindNext(HWND hwndTV
, HTREEITEM hItem
, LPCWSTR sstring
, int mode
, int *row
)
310 HTREEITEM hTry
, hLast
;
314 if (match_item(hwndTV
, hLast
, sstring
, mode
& ~SEARCH_KEYS
, row
)) {
320 /* first look in subtree */
321 /* no children? maybe we haven't loaded them yet? */
322 if (!SendMessageW(hwndTV
, TVM_GETNEXTITEM
, TVGN_CHILD
, (LPARAM
)hLast
)) {
323 UINT state
= (UINT
)SendMessageW(hwndTV
, TVM_GETITEMSTATE
, (WPARAM
)hLast
, -1);
324 UpdateExpandingTree(hwndTV
, hLast
, state
);
326 hTry
= (HTREEITEM
)SendMessageW(hwndTV
, TVM_GETNEXTITEM
, TVGN_CHILD
, (LPARAM
)hLast
);
328 if (match_item(hwndTV
, hTry
, sstring
, mode
, row
))
333 /* no more children, maybe there are any siblings? */
334 hTry
= (HTREEITEM
)SendMessageW(hwndTV
, TVM_GETNEXTITEM
, TVGN_NEXT
, (LPARAM
)hLast
);
336 if (match_item(hwndTV
, hTry
, sstring
, mode
, row
))
341 /* no more siblings, look at the next siblings in parent(s) */
342 hLast
= (HTREEITEM
)SendMessageW(hwndTV
, TVM_GETNEXTITEM
, TVGN_PARENT
, (LPARAM
)hLast
);
345 while (hLast
&& (hTry
= (HTREEITEM
)SendMessageW(hwndTV
, TVM_GETNEXTITEM
, TVGN_NEXT
, (LPARAM
)hLast
)) == NULL
) {
346 hLast
= (HTREEITEM
)SendMessageW(hwndTV
, TVM_GETNEXTITEM
, TVGN_PARENT
, (LPARAM
)hLast
);
348 if (match_item(hwndTV
, hTry
, sstring
, mode
, row
))
355 static BOOL
RefreshTreeItem(HWND hwndTV
, HTREEITEM hItem
)
357 HKEY hRoot
, hKey
, hSubKey
;
360 DWORD dwCount
, dwIndex
, dwMaxSubKeyLen
;
365 KeyPath
= GetItemPath(hwndTV
, hItem
, &hRoot
);
367 if (!KeyPath
|| !hRoot
)
371 if (RegOpenKeyExW(hRoot
, KeyPath
, 0, KEY_READ
, &hKey
) != ERROR_SUCCESS
) {
372 WINE_TRACE("RegOpenKeyEx failed, %s was probably removed.\n", wine_dbgstr_w(KeyPath
));
378 HeapFree(GetProcessHeap(), 0, KeyPath
);
380 if (RegQueryInfoKeyW(hKey
, 0, 0, 0, &dwCount
, &dwMaxSubKeyLen
, 0, 0, 0, 0, 0, 0) != ERROR_SUCCESS
) {
384 /* Set the number of children again */
385 tvItem
.mask
= TVIF_CHILDREN
;
386 tvItem
.hItem
= hItem
;
387 tvItem
.cChildren
= dwCount
;
388 if (!TreeView_SetItemW(hwndTV
, &tvItem
)) {
392 /* We don't have to bother with the rest if it's not expanded. */
393 if (SendMessageW(hwndTV
, TVM_GETITEMSTATE
, (WPARAM
)hItem
, TVIS_EXPANDED
) == 0) {
398 dwMaxSubKeyLen
++; /* account for the \0 terminator */
399 if (!(Name
= HeapAlloc(GetProcessHeap(), 0, dwMaxSubKeyLen
* sizeof(WCHAR
)))) {
402 tvItem
.cchTextMax
= dwMaxSubKeyLen
;
403 if (!(tvItem
.pszText
= HeapAlloc(GetProcessHeap(), 0, dwMaxSubKeyLen
* sizeof(WCHAR
)))) {
404 HeapFree(GetProcessHeap(), 0, Name
);
408 /* Now go through all the children in the registry, and check if any have to be added. */
409 for (dwIndex
= 0; dwIndex
< dwCount
; dwIndex
++) {
410 DWORD cName
= dwMaxSubKeyLen
, dwSubCount
;
414 if (RegEnumKeyExW(hKey
, dwIndex
, Name
, &cName
, 0, 0, 0, NULL
) != ERROR_SUCCESS
) {
418 /* Find the number of children of the node. */
420 if (RegOpenKeyExW(hKey
, Name
, 0, KEY_QUERY_VALUE
, &hSubKey
) == ERROR_SUCCESS
) {
421 if (RegQueryInfoKeyW(hSubKey
, 0, 0, 0, &dwSubCount
, 0, 0, 0, 0, 0, 0, 0) != ERROR_SUCCESS
) {
424 RegCloseKey(hSubKey
);
427 /* Check if the node is already in there. */
428 for (childItem
= (HTREEITEM
)SendMessageW(hwndTV
, TVM_GETNEXTITEM
, TVGN_CHILD
, (LPARAM
)hItem
); childItem
;
429 childItem
= (HTREEITEM
)SendMessageW(hwndTV
, TVM_GETNEXTITEM
, TVGN_NEXT
, (LPARAM
)childItem
)) {
430 tvItem
.mask
= TVIF_TEXT
;
431 tvItem
.hItem
= childItem
;
432 if (!TreeView_GetItemW(hwndTV
, &tvItem
)) {
433 HeapFree(GetProcessHeap(), 0, Name
);
434 HeapFree(GetProcessHeap(), 0, tvItem
.pszText
);
438 if (!lstrcmpiW(tvItem
.pszText
, Name
)) {
444 if (found
== FALSE
) {
445 WINE_TRACE("New subkey %s\n", wine_dbgstr_w(Name
));
446 AddEntryToTree(hwndTV
, hItem
, Name
, NULL
, dwSubCount
);
449 HeapFree(GetProcessHeap(), 0, Name
);
450 HeapFree(GetProcessHeap(), 0, tvItem
.pszText
);
453 /* Now go through all the children in the tree, and check if any have to be removed. */
454 childItem
= (HTREEITEM
)SendMessageW(hwndTV
, TVM_GETNEXTITEM
, TVGN_CHILD
, (LPARAM
)hItem
);
456 HTREEITEM nextItem
= (HTREEITEM
)SendMessageW(hwndTV
, TVM_GETNEXTITEM
, TVGN_NEXT
, (LPARAM
)childItem
);
457 if (RefreshTreeItem(hwndTV
, childItem
) == FALSE
) {
458 SendMessageW(hwndTV
, TVM_DELETEITEM
, 0, (LPARAM
)childItem
);
460 childItem
= nextItem
;
466 static void treeview_sort_item(HWND hWnd
, HTREEITEM item
)
468 HTREEITEM child
= (HTREEITEM
)SendMessageW(hWnd
, TVM_GETNEXTITEM
, TVGN_CHILD
, (LPARAM
)item
);
470 while (child
!= NULL
) {
471 treeview_sort_item(hWnd
, child
);
472 child
= (HTREEITEM
)SendMessageW(hWnd
, TVM_GETNEXTITEM
, TVGN_NEXT
, (LPARAM
)child
);
474 SendMessageW(hWnd
, TVM_SORTCHILDREN
, 0, (LPARAM
)item
);
477 BOOL
RefreshTreeView(HWND hwndTV
)
480 HTREEITEM hSelectedItem
;
485 hSelectedItem
= (HTREEITEM
)SendMessageW(hwndTV
, TVM_GETNEXTITEM
, TVGN_CARET
, 0);
486 hcursorOld
= SetCursor(LoadCursorW(NULL
, (LPCWSTR
)IDC_WAIT
));
487 SendMessageW(hwndTV
, WM_SETREDRAW
, FALSE
, 0);
489 hRoot
= (HTREEITEM
)SendMessageW(hwndTV
, TVM_GETNEXTITEM
, TVGN_ROOT
, 0);
490 hItem
= (HTREEITEM
)SendMessageW(hwndTV
, TVM_GETNEXTITEM
, TVGN_CHILD
, (LPARAM
)hRoot
);
492 RefreshTreeItem(hwndTV
, hItem
);
493 treeview_sort_item(hwndTV
, hItem
);
494 hItem
= (HTREEITEM
)SendMessageW(hwndTV
, TVM_GETNEXTITEM
, TVGN_NEXT
, (LPARAM
)hItem
);
497 SendMessageW(hwndTV
, WM_SETREDRAW
, TRUE
, 0);
498 InvalidateRect(hwndTV
, NULL
, FALSE
);
499 SetCursor(hcursorOld
);
501 /* We reselect the currently selected node, this will prompt a refresh of the listview. */
502 SendMessageW(hwndTV
, TVM_SELECTITEM
, TVGN_CARET
, (LPARAM
)hSelectedItem
);
506 HTREEITEM
InsertNode(HWND hwndTV
, HTREEITEM hItem
, LPWSTR name
)
508 WCHAR buf
[MAX_NEW_KEY_LEN
];
509 HTREEITEM hNewItem
= 0;
512 if (!hItem
) hItem
= (HTREEITEM
)SendMessageW(hwndTV
, TVM_GETNEXTITEM
, TVGN_CARET
, 0);
513 if (!hItem
) return FALSE
;
514 if (SendMessageW(hwndTV
, TVM_GETITEMSTATE
, (WPARAM
)hItem
, TVIS_EXPANDEDONCE
) & TVIS_EXPANDEDONCE
) {
515 hNewItem
= AddEntryToTree(hwndTV
, hItem
, name
, 0, 0);
517 item
.mask
= TVIF_CHILDREN
| TVIF_HANDLE
;
519 if (!TreeView_GetItemW(hwndTV
, &item
)) return FALSE
;
521 if (!TreeView_SetItemW(hwndTV
, &item
)) return FALSE
;
523 SendMessageW(hwndTV
, TVM_EXPAND
, TVE_EXPAND
, (LPARAM
)hItem
);
525 for(hNewItem
= (HTREEITEM
)SendMessageW(hwndTV
, TVM_GETNEXTITEM
, TVGN_CHILD
, (LPARAM
)hItem
); hNewItem
;
526 hNewItem
= (HTREEITEM
)SendMessageW(hwndTV
, TVM_GETNEXTITEM
, TVGN_NEXT
, (LPARAM
)hNewItem
)) {
527 item
.mask
= TVIF_HANDLE
| TVIF_TEXT
;
528 item
.hItem
= hNewItem
;
530 item
.cchTextMax
= COUNT_OF(buf
);
531 if (!TreeView_GetItemW(hwndTV
, &item
)) continue;
532 if (lstrcmpW(name
, item
.pszText
) == 0) break;
536 SendMessageW(hwndTV
, TVM_SELECTITEM
, TVGN_CARET
, (LPARAM
)hNewItem
);
541 HWND
StartKeyRename(HWND hwndTV
)
545 if(!(hItem
= (HTREEITEM
)SendMessageW(hwndTV
, TVM_GETNEXTITEM
, TVGN_CARET
, 0))) return 0;
546 SetWindowLongPtrW(hwndTV
, GWLP_USERDATA
, 1);
547 return (HWND
)SendMessageW(hwndTV
, TVM_EDITLABELW
, 0, (LPARAM
)hItem
);
550 static BOOL
InitTreeViewItems(HWND hwndTV
, LPWSTR pHostName
)
552 TVINSERTSTRUCTW tvins
;
554 static WCHAR hkcr
[] = {'H','K','E','Y','_','C','L','A','S','S','E','S','_','R','O','O','T',0},
555 hkcu
[] = {'H','K','E','Y','_','C','U','R','R','E','N','T','_','U','S','E','R',0},
556 hklm
[] = {'H','K','E','Y','_','L','O','C','A','L','_','M','A','C','H','I','N','E',0},
557 hku
[] = {'H','K','E','Y','_','U','S','E','R','S',0},
558 hkcc
[] = {'H','K','E','Y','_','C','U','R','R','E','N','T','_','C','O','N','F','I','G',0},
559 hkdd
[] = {'H','K','E','Y','_','D','Y','N','_','D','A','T','A',0};
561 tvins
.u
.item
.mask
= TVIF_TEXT
| TVIF_IMAGE
| TVIF_SELECTEDIMAGE
| TVIF_CHILDREN
| TVIF_PARAM
;
562 /* Set the text of the item. */
563 tvins
.u
.item
.pszText
= pHostName
;
564 tvins
.u
.item
.cchTextMax
= lstrlenW(pHostName
);
565 /* Assume the item is not a parent item, so give it an image. */
566 tvins
.u
.item
.iImage
= Image_Root
;
567 tvins
.u
.item
.iSelectedImage
= Image_Root
;
568 tvins
.u
.item
.cChildren
= 5;
569 /* Save the heading level in the item's application-defined data area. */
570 tvins
.u
.item
.lParam
= 0;
571 tvins
.hInsertAfter
= TVI_FIRST
;
572 tvins
.hParent
= TVI_ROOT
;
573 /* Add the item to the tree view control. */
574 if (!(hRoot
= TreeView_InsertItemW(hwndTV
, &tvins
))) return FALSE
;
576 if (!AddEntryToTree(hwndTV
, hRoot
, hkcr
, HKEY_CLASSES_ROOT
, 1)) return FALSE
;
577 if (!AddEntryToTree(hwndTV
, hRoot
, hkcu
, HKEY_CURRENT_USER
, 1)) return FALSE
;
578 if (!AddEntryToTree(hwndTV
, hRoot
, hklm
, HKEY_LOCAL_MACHINE
, 1)) return FALSE
;
579 if (!AddEntryToTree(hwndTV
, hRoot
, hku
, HKEY_USERS
, 1)) return FALSE
;
580 if (!AddEntryToTree(hwndTV
, hRoot
, hkcc
, HKEY_CURRENT_CONFIG
, 1)) return FALSE
;
581 if (!AddEntryToTree(hwndTV
, hRoot
, hkdd
, HKEY_DYN_DATA
, 1)) return FALSE
;
583 /* expand and select host name */
584 SendMessageW(hwndTV
, TVM_EXPAND
, TVE_EXPAND
, (LPARAM
)hRoot
);
585 SendMessageW(hwndTV
, TVM_SELECTITEM
, TVGN_CARET
, (LPARAM
)hRoot
);
591 * InitTreeViewImageLists - creates an image list, adds three bitmaps
592 * to it, and associates the image list with a tree view control.
593 * Returns TRUE if successful, or FALSE otherwise.
594 * hwndTV - handle to the tree view control.
596 static BOOL
InitTreeViewImageLists(HWND hwndTV
)
598 HIMAGELIST himl
; /* handle to image list */
599 HICON hico
; /* handle to icon */
600 INT cx
= GetSystemMetrics(SM_CXSMICON
);
601 INT cy
= GetSystemMetrics(SM_CYSMICON
);
603 /* Create the image list. */
604 if ((himl
= ImageList_Create(cx
, cy
, ILC_MASK
, 0, NUM_ICONS
)) == NULL
)
607 /* Add the open file, closed file, and document bitmaps. */
608 hico
= LoadIconW(hInst
, MAKEINTRESOURCEW(IDI_OPEN_FILE
));
609 Image_Open
= ImageList_AddIcon(himl
, hico
);
611 hico
= LoadIconW(hInst
, MAKEINTRESOURCEW(IDI_CLOSED_FILE
));
612 Image_Closed
= ImageList_AddIcon(himl
, hico
);
614 hico
= LoadIconW(hInst
, MAKEINTRESOURCEW(IDI_ROOT
));
615 Image_Root
= ImageList_AddIcon(himl
, hico
);
617 /* Fail if not all of the images were added. */
618 if (ImageList_GetImageCount(himl
) < NUM_ICONS
)
623 /* Associate the image list with the tree view control. */
624 SendMessageW(hwndTV
, TVM_SETIMAGELIST
, TVSIL_NORMAL
, (LPARAM
)himl
);
629 BOOL
UpdateExpandingTree(HWND hwndTV
, HTREEITEM hItem
, int state
)
631 DWORD dwCount
, dwIndex
, dwMaxSubKeyLen
;
632 HKEY hRoot
, hNewKey
, hKey
;
639 static int expanding
;
640 if (expanding
) return FALSE
;
641 if (state
& TVIS_EXPANDEDONCE
) {
645 hcursorOld
= SetCursor(LoadCursorW(NULL
, (LPCWSTR
)IDC_WAIT
));
646 SendMessageW(hwndTV
, WM_SETREDRAW
, FALSE
, 0);
648 keyPath
= GetItemPath(hwndTV
, hItem
, &hRoot
);
649 if (!keyPath
) goto done
;
652 errCode
= RegOpenKeyExW(hRoot
, keyPath
, 0, KEY_READ
, &hNewKey
);
653 if (errCode
!= ERROR_SUCCESS
) goto done
;
658 errCode
= RegQueryInfoKeyW(hNewKey
, 0, 0, 0, &dwCount
, &dwMaxSubKeyLen
, 0, 0, 0, 0, 0, 0);
659 if (errCode
!= ERROR_SUCCESS
) goto done
;
660 dwMaxSubKeyLen
++; /* account for the \0 terminator */
661 Name
= HeapAlloc(GetProcessHeap(), 0, dwMaxSubKeyLen
* sizeof(WCHAR
));
662 if (!Name
) goto done
;
664 for (dwIndex
= 0; dwIndex
< dwCount
; dwIndex
++) {
665 DWORD cName
= dwMaxSubKeyLen
, dwSubCount
;
667 errCode
= RegEnumKeyExW(hNewKey
, dwIndex
, Name
, &cName
, 0, 0, 0, 0);
668 if (errCode
!= ERROR_SUCCESS
) continue;
669 errCode
= RegOpenKeyExW(hNewKey
, Name
, 0, KEY_QUERY_VALUE
, &hKey
);
670 if (errCode
== ERROR_SUCCESS
) {
671 errCode
= RegQueryInfoKeyW(hKey
, 0, 0, 0, &dwSubCount
, 0, 0, 0, 0, 0, 0, 0);
674 if (errCode
!= ERROR_SUCCESS
) dwSubCount
= 0;
675 AddEntryToTree(hwndTV
, hItem
, Name
, NULL
, dwSubCount
);
677 RegCloseKey(hNewKey
);
678 HeapFree(GetProcessHeap(), 0, Name
);
681 item
.mask
= TVIF_STATE
;
683 item
.stateMask
= TVIS_EXPANDEDONCE
;
684 item
.state
= TVIS_EXPANDEDONCE
;
685 SendMessageW(hwndTV
, TVM_SETITEMW
, 0, (LPARAM
)&item
);
686 SendMessageW(hwndTV
, WM_SETREDRAW
, TRUE
, 0);
687 SetCursor(hcursorOld
);
689 HeapFree(GetProcessHeap(), 0, keyPath
);
694 BOOL
OnTreeExpanding(HWND hwndTV
, NMTREEVIEWW
* pnmtv
)
696 return UpdateExpandingTree(hwndTV
, pnmtv
->itemNew
.hItem
, pnmtv
->itemNew
.state
);
701 * CreateTreeView - creates a tree view control.
702 * Returns the handle to the new control if successful, or NULL otherwise.
703 * hwndParent - handle to the control's parent window.
705 HWND
CreateTreeView(HWND hwndParent
, LPWSTR pHostName
, UINT id
)
709 WCHAR TreeView
[] = {'T','r','e','e',' ','V','i','e','w',0};
711 /* Get the dimensions of the parent window's client area, and create the tree view control. */
712 GetClientRect(hwndParent
, &rcClient
);
713 hwndTV
= CreateWindowExW(WS_EX_CLIENTEDGE
, WC_TREEVIEWW
, TreeView
,
714 WS_VISIBLE
| WS_CHILD
| WS_TABSTOP
| TVS_HASLINES
| TVS_HASBUTTONS
|
715 TVS_LINESATROOT
| TVS_EDITLABELS
| TVS_SHOWSELALWAYS
,
716 0, 0, rcClient
.right
, rcClient
.bottom
,
717 hwndParent
, ULongToHandle(id
), hInst
, NULL
);
718 SendMessageW(hwndTV
, TVM_SETUNICODEFORMAT
, TRUE
, 0);
719 /* Initialize the image list, and add items to the control. */
720 if (!InitTreeViewImageLists(hwndTV
) || !InitTreeViewItems(hwndTV
, pHostName
)) {
721 DestroyWindow(hwndTV
);