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 St, Fifth Floor, Boston, MA 02110-1301, USA
21 #define WIN32_LEAN_AND_MEAN /* Exclude rarely-used stuff from Windows headers */
23 #define NONAMELESSUNION
24 #define NONAMELESSSTRUCT
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. */
50 static BOOL
UpdateExpandingTree(HWND hwndTV
, HTREEITEM hItem
, int state
);
52 static BOOL
get_item_path(HWND hwndTV
, HTREEITEM hItem
, HKEY
* phKey
, LPTSTR
* pKeyPath
, int* pPathLen
, int* pMaxLen
)
58 item
.mask
= TVIF_PARAM
;
60 if (!TreeView_GetItem(hwndTV
, &item
)) return FALSE
;
63 /* found root key with valid key value */
64 *phKey
= (HKEY
)item
.lParam
;
68 if(!get_item_path(hwndTV
, TreeView_GetParent(hwndTV
, hItem
), phKey
, pKeyPath
, pPathLen
, pMaxLen
)) return FALSE
;
70 (*pKeyPath
)[*pPathLen
] = _T('\\');
75 item
.mask
= TVIF_TEXT
;
77 item
.pszText
= *pKeyPath
+ *pPathLen
;
78 item
.cchTextMax
= maxLen
= *pMaxLen
- *pPathLen
;
79 if (!TreeView_GetItem(hwndTV
, &item
)) return FALSE
;
80 len
= _tcslen(item
.pszText
);
81 if (len
< maxLen
- 1) {
85 newStr
= HeapReAlloc(GetProcessHeap(), 0, *pKeyPath
, *pMaxLen
* 2);
86 if (!newStr
) return FALSE
;
94 static BOOL
get_item_pathW(HWND hwndTV
, HTREEITEM hItem
, HKEY
* phKey
, LPWSTR
* pKeyPath
, int* pPathLen
, int* pMaxChars
)
100 item
.mask
= TVIF_PARAM
;
102 if (!TreeView_GetItem(hwndTV
, &item
)) return FALSE
;
105 /* found root key with valid key value */
106 *phKey
= (HKEY
)item
.lParam
;
110 if(!get_item_pathW(hwndTV
, TreeView_GetParent(hwndTV
, hItem
), phKey
, pKeyPath
, pPathLen
, pMaxChars
)) return FALSE
;
112 (*pKeyPath
)[*pPathLen
] = '\\';
117 item
.mask
= TVIF_TEXT
;
119 item
.pszText
= *pKeyPath
+ *pPathLen
;
120 item
.cchTextMax
= maxChars
= *pMaxChars
- *pPathLen
;
121 if (!TreeView_GetItemW(hwndTV
, &item
)) return FALSE
;
122 chars
= lstrlenW(item
.pszText
);
123 if (chars
< maxChars
- 1) {
127 newStr
= HeapReAlloc(GetProcessHeap(), 0, *pKeyPath
, *pMaxChars
* 2);
128 if (!newStr
) return FALSE
;
136 LPTSTR
GetItemPath(HWND hwndTV
, HTREEITEM hItem
, HKEY
* phRootKey
)
138 int pathLen
= 0, maxLen
;
141 pathBuffer
= HeapAlloc(GetProcessHeap(), 0, 1024);
142 if (!pathBuffer
) return NULL
;
144 maxLen
= HeapSize(GetProcessHeap(), 0, pathBuffer
);
145 if (maxLen
== (SIZE_T
) - 1) return NULL
;
146 if (!hItem
) hItem
= TreeView_GetSelection(hwndTV
);
147 if (!hItem
) return NULL
;
148 if (!get_item_path(hwndTV
, hItem
, phRootKey
, &pathBuffer
, &pathLen
, &maxLen
)) return NULL
;
152 LPWSTR
GetItemPathW(HWND hwndTV
, HTREEITEM hItem
, HKEY
* phRootKey
)
154 int pathLen
= 0, maxLen
;
157 pathBuffer
= HeapAlloc(GetProcessHeap(), 0, 1024*sizeof(WCHAR
));
158 if (!pathBuffer
) return NULL
;
160 maxLen
= HeapSize(GetProcessHeap(), 0, pathBuffer
);
161 if (maxLen
== (SIZE_T
) - 1) return NULL
;
162 maxLen
= maxLen
/ sizeof(WCHAR
);
163 if (!hItem
) hItem
= TreeView_GetSelection(hwndTV
);
164 if (!hItem
) return NULL
;
165 if (!get_item_pathW(hwndTV
, hItem
, phRootKey
, &pathBuffer
, &pathLen
, &maxLen
)) return NULL
;
169 static LPTSTR
get_path_component(LPCTSTR
*lplpKeyName
) {
170 LPCTSTR lpPos
= *lplpKeyName
;
171 LPTSTR lpResult
= NULL
;
175 while(*lpPos
&& *lpPos
!= '\\')
177 if (*lpPos
&& lpPos
== *lplpKeyName
)
179 len
= (lpPos
+1-(*lplpKeyName
)) * sizeof(TCHAR
);
180 lpResult
= HeapAlloc(GetProcessHeap(), 0, len
);
181 if (!lpResult
) /* that would be very odd */
183 memcpy(lpResult
, *lplpKeyName
, len
-1);
184 lpResult
[len
-1] = '\0';
185 *lplpKeyName
= *lpPos
? lpPos
+1 : NULL
;
189 HTREEITEM
FindPathInTree(HWND hwndTV
, LPCTSTR lpKeyName
) {
191 TCHAR buf
[261]; /* tree view has 260 character limitation on item name */
192 HTREEITEM hItem
, hOldItem
;
195 hItem
= TreeView_GetRoot(hwndTV
);
196 SendMessage(hwndTV
, TVM_EXPAND
, TVE_EXPAND
, (LPARAM
)hItem
);
197 hItem
= TreeView_GetChild(hwndTV
, hItem
);
200 LPTSTR lpItemName
= get_path_component(&lpKeyName
);
203 tvi
.mask
= TVIF_TEXT
| TVIF_HANDLE
;
206 tvi
.cchTextMax
= 260;
207 SendMessage(hwndTV
, TVM_GETITEM
, 0, (LPARAM
) &tvi
);
208 if (!_tcsicmp(tvi
.pszText
, lpItemName
)) {
209 SendMessage(hwndTV
, TVM_EXPAND
, TVE_EXPAND
, (LPARAM
)hItem
);
213 hItem
= TreeView_GetChild(hwndTV
, hItem
);
216 hItem
= TreeView_GetNextSibling(hwndTV
, hItem
);
218 HeapFree(GetProcessHeap(), 0, lpItemName
);
227 BOOL
DeleteNode(HWND hwndTV
, HTREEITEM hItem
)
229 if (!hItem
) hItem
= TreeView_GetSelection(hwndTV
);
230 if (!hItem
) return FALSE
;
231 return TreeView_DeleteItem(hwndTV
, hItem
);
234 /* Add an entry to the tree. Only give hKey for root nodes (HKEY_ constants) */
235 static HTREEITEM
AddEntryToTree(HWND hwndTV
, HTREEITEM hParent
, LPTSTR label
, HKEY hKey
, DWORD dwChildren
)
237 TVINSERTSTRUCT tvins
;
240 if (RegQueryInfoKey(hKey
, 0, 0, 0, &dwChildren
, 0, 0, 0, 0, 0, 0, 0) != ERROR_SUCCESS
) {
245 tvins
.u
.item
.mask
= TVIF_TEXT
| TVIF_IMAGE
| TVIF_SELECTEDIMAGE
| TVIF_CHILDREN
| TVIF_PARAM
;
246 tvins
.u
.item
.pszText
= label
;
247 tvins
.u
.item
.cchTextMax
= lstrlen(label
);
248 tvins
.u
.item
.iImage
= Image_Closed
;
249 tvins
.u
.item
.iSelectedImage
= Image_Open
;
250 tvins
.u
.item
.cChildren
= dwChildren
;
251 tvins
.u
.item
.lParam
= (LPARAM
)hKey
;
252 tvins
.hInsertAfter
= (HTREEITEM
)(hKey
? TVI_LAST
: TVI_SORT
);
253 tvins
.hParent
= hParent
;
254 return TreeView_InsertItem(hwndTV
, &tvins
);
257 static BOOL
match_string(LPCTSTR sstring1
, LPCTSTR sstring2
, int mode
)
259 if (mode
& SEARCH_WHOLE
)
260 return !stricmp(sstring1
, sstring2
);
262 return NULL
!= StrStrI(sstring1
, sstring2
);
265 static BOOL
match_item(HWND hwndTV
, HTREEITEM hItem
, LPCTSTR sstring
, int mode
, int *row
)
268 TCHAR keyname
[KEY_MAX_LEN
];
269 item
.mask
= TVIF_TEXT
;
271 item
.pszText
= keyname
;
272 item
.cchTextMax
= KEY_MAX_LEN
;
273 if (!TreeView_GetItem(hwndTV
, &item
)) return FALSE
;
274 if ((mode
& SEARCH_KEYS
) && match_string(keyname
, sstring
, mode
)) {
279 if (mode
& (SEARCH_VALUES
| SEARCH_CONTENT
)) {
281 TCHAR valName
[KEY_MAX_LEN
], *KeyPath
;
285 KeyPath
= GetItemPath(hwndTV
, hItem
, &hRoot
);
287 if (!KeyPath
|| !hRoot
)
290 if (RegOpenKeyEx(hRoot
, KeyPath
, 0, KEY_READ
, &hKey
) != ERROR_SUCCESS
) {
291 HeapFree(GetProcessHeap(), 0, KeyPath
);
295 HeapFree(GetProcessHeap(), 0, KeyPath
);
296 lenName
= KEY_MAX_LEN
;
298 /* RegEnumValue won't return empty default value, so fake it when dealing with *row,
299 which corresponds to list view rows, not value ids */
300 if (ERROR_SUCCESS
== RegEnumValue(hKey
, 0, valName
, &lenName
, NULL
, NULL
, NULL
, NULL
) && *valName
)
306 DWORD lenValue
= 0, type
= 0;
307 lenName
= KEY_MAX_LEN
;
309 if (ERROR_SUCCESS
!= RegEnumValue(hKey
,
310 i
, valName
, &lenName
, NULL
, &type
, NULL
, &lenValue
))
313 if (mode
& SEARCH_VALUES
) {
314 if (match_string(valName
, sstring
, mode
)) {
321 if ((mode
& SEARCH_CONTENT
) && (type
== REG_EXPAND_SZ
|| type
== REG_SZ
)) {
323 buffer
= HeapAlloc(GetProcessHeap(), 0, lenValue
);
324 RegEnumValue(hKey
, i
, valName
, &lenName
, NULL
, &type
, (LPBYTE
)buffer
, &lenValue
);
325 if (match_string(buffer
, sstring
, mode
)) {
326 HeapFree(GetProcessHeap(), 0, buffer
);
331 HeapFree(GetProcessHeap(), 0, buffer
);
341 HTREEITEM
FindNext(HWND hwndTV
, HTREEITEM hItem
, LPCTSTR sstring
, int mode
, int *row
)
343 HTREEITEM hTry
, hLast
;
347 if (match_item(hwndTV
, hLast
, sstring
, mode
& ~SEARCH_KEYS
, row
)) {
353 /* first look in subtree */
354 /* no children? maybe we haven't loaded them yet? */
355 if (!TreeView_GetChild(hwndTV
, hLast
)) {
356 UpdateExpandingTree(hwndTV
, hLast
, TreeView_GetItemState(hwndTV
, hLast
, -1));
358 hTry
= TreeView_GetChild(hwndTV
, hLast
);
360 if (match_item(hwndTV
, hTry
, sstring
, mode
, row
))
365 /* no more children, maybe there are any siblings? */
366 hTry
= TreeView_GetNextSibling(hwndTV
, hLast
);
368 if (match_item(hwndTV
, hTry
, sstring
, mode
, row
))
373 /* no more siblings, look at the next siblings in parent(s) */
374 hLast
= TreeView_GetParent(hwndTV
, hLast
);
377 while (hLast
&& (hTry
= TreeView_GetNextSibling(hwndTV
, hLast
)) == NULL
) {
378 hLast
= TreeView_GetParent(hwndTV
, hLast
);
380 if (match_item(hwndTV
, hTry
, sstring
, mode
, row
))
387 static BOOL
RefreshTreeItem(HWND hwndTV
, HTREEITEM hItem
)
389 HKEY hRoot
, hKey
, hSubKey
;
392 DWORD dwCount
, dwIndex
, dwMaxSubKeyLen
;
397 KeyPath
= GetItemPath(hwndTV
, hItem
, &hRoot
);
399 if (!KeyPath
|| !hRoot
)
403 if (RegOpenKeyEx(hRoot
, KeyPath
, 0, KEY_READ
, &hKey
) != ERROR_SUCCESS
) {
404 WINE_TRACE("RegOpenKeyEx failed, \"%s\" was probably removed.\n", KeyPath
);
410 HeapFree(GetProcessHeap(), 0, KeyPath
);
412 if (RegQueryInfoKey(hKey
, 0, 0, 0, &dwCount
, &dwMaxSubKeyLen
, 0, 0, 0, 0, 0, 0) != ERROR_SUCCESS
) {
416 /* Set the number of children again */
417 tvItem
.mask
= TVIF_CHILDREN
;
418 tvItem
.hItem
= hItem
;
419 tvItem
.cChildren
= dwCount
;
420 if (!TreeView_SetItem(hwndTV
, &tvItem
)) {
424 /* We don't have to bother with the rest if it's not expanded. */
425 if (TreeView_GetItemState(hwndTV
, hItem
, TVIS_EXPANDED
) == 0) {
430 dwMaxSubKeyLen
++; /* account for the \0 terminator */
431 if (!(Name
= HeapAlloc(GetProcessHeap(), 0, dwMaxSubKeyLen
* sizeof(TCHAR
)))) {
434 tvItem
.cchTextMax
= dwMaxSubKeyLen
;
435 if (!(tvItem
.pszText
= HeapAlloc(GetProcessHeap(), 0, dwMaxSubKeyLen
* sizeof(TCHAR
)))) {
439 /* Now go through all the children in the registry, and check if any have to be added. */
440 for (dwIndex
= 0; dwIndex
< dwCount
; dwIndex
++) {
441 DWORD cName
= dwMaxSubKeyLen
, dwSubCount
;
445 if (RegEnumKeyEx(hKey
, dwIndex
, Name
, &cName
, 0, 0, 0, NULL
) != ERROR_SUCCESS
) {
449 /* Find the number of children of the node. */
451 if (RegOpenKeyEx(hKey
, Name
, 0, KEY_QUERY_VALUE
, &hSubKey
) == ERROR_SUCCESS
) {
452 if (RegQueryInfoKey(hSubKey
, 0, 0, 0, &dwSubCount
, 0, 0, 0, 0, 0, 0, 0) != ERROR_SUCCESS
) {
455 RegCloseKey(hSubKey
);
458 /* Check if the node is already in there. */
459 for (childItem
= TreeView_GetChild(hwndTV
, hItem
); childItem
;
460 childItem
= TreeView_GetNextSibling(hwndTV
, childItem
)) {
461 tvItem
.mask
= TVIF_TEXT
;
462 tvItem
.hItem
= childItem
;
463 if (!TreeView_GetItem(hwndTV
, &tvItem
)) {
467 if (!stricmp(tvItem
.pszText
, Name
)) {
473 if (found
== FALSE
) {
474 WINE_TRACE("New subkey %s\n", Name
);
475 AddEntryToTree(hwndTV
, hItem
, Name
, NULL
, dwSubCount
);
478 HeapFree(GetProcessHeap(), 0, Name
);
479 HeapFree(GetProcessHeap(), 0, tvItem
.pszText
);
482 /* Now go through all the children in the tree, and check if any have to be removed. */
483 childItem
= TreeView_GetChild(hwndTV
, hItem
);
485 HTREEITEM nextItem
= TreeView_GetNextSibling(hwndTV
, childItem
);
486 if (RefreshTreeItem(hwndTV
, childItem
) == FALSE
) {
487 SendMessage(hwndTV
, TVM_DELETEITEM
, 0, (LPARAM
)childItem
);
489 childItem
= nextItem
;
495 BOOL
RefreshTreeView(HWND hwndTV
)
498 HTREEITEM hSelectedItem
;
502 hSelectedItem
= TreeView_GetSelection(hwndTV
);
503 hcursorOld
= SetCursor(LoadCursor(NULL
, IDC_WAIT
));
504 SendMessage(hwndTV
, WM_SETREDRAW
, FALSE
, 0);
506 hItem
= TreeView_GetChild(hwndTV
, TreeView_GetRoot(hwndTV
));
508 RefreshTreeItem(hwndTV
, hItem
);
509 hItem
= TreeView_GetNextSibling(hwndTV
, hItem
);
512 SendMessage(hwndTV
, WM_SETREDRAW
, TRUE
, 0);
513 InvalidateRect(hwndTV
, NULL
, FALSE
);
514 SetCursor(hcursorOld
);
516 /* We reselect the currently selected node, this will prompt a refresh of the listview. */
517 SendMessage(hwndTV
, TVM_SELECTITEM
, TVGN_CARET
, (LPARAM
)hSelectedItem
);
521 HTREEITEM
InsertNode(HWND hwndTV
, HTREEITEM hItem
, LPTSTR name
)
523 TCHAR buf
[MAX_NEW_KEY_LEN
];
524 HTREEITEM hNewItem
= 0;
527 if (!hItem
) hItem
= TreeView_GetSelection(hwndTV
);
528 if (!hItem
) return FALSE
;
529 if (TreeView_GetItemState(hwndTV
, hItem
, TVIS_EXPANDEDONCE
)) {
530 hNewItem
= AddEntryToTree(hwndTV
, hItem
, name
, 0, 0);
532 item
.mask
= TVIF_CHILDREN
| TVIF_HANDLE
;
534 if (!TreeView_GetItem(hwndTV
, &item
)) return FALSE
;
536 if (!TreeView_SetItem(hwndTV
, &item
)) return FALSE
;
538 SendMessage(hwndTV
, TVM_EXPAND
, TVE_EXPAND
, (LPARAM
)hItem
);
540 for(hNewItem
= TreeView_GetChild(hwndTV
, hItem
); hNewItem
; hNewItem
= TreeView_GetNextSibling(hwndTV
, hNewItem
)) {
541 item
.mask
= TVIF_HANDLE
| TVIF_TEXT
;
542 item
.hItem
= hNewItem
;
544 item
.cchTextMax
= COUNT_OF(buf
);
545 if (!TreeView_GetItem(hwndTV
, &item
)) continue;
546 if (lstrcmp(name
, item
.pszText
) == 0) break;
550 SendMessage(hwndTV
, TVM_SELECTITEM
, TVGN_CARET
, (LPARAM
)hNewItem
);
555 HWND
StartKeyRename(HWND hwndTV
)
559 if(!(hItem
= TreeView_GetSelection(hwndTV
))) return 0;
560 return TreeView_EditLabel(hwndTV
, hItem
);
563 static BOOL
InitTreeViewItems(HWND hwndTV
, LPTSTR pHostName
)
565 TVINSERTSTRUCT tvins
;
567 static TCHAR hkcr
[] = {'H','K','E','Y','_','C','L','A','S','S','E','S','_','R','O','O','T',0},
568 hkcu
[] = {'H','K','E','Y','_','C','U','R','R','E','N','T','_','U','S','E','R',0},
569 hklm
[] = {'H','K','E','Y','_','L','O','C','A','L','_','M','A','C','H','I','N','E',0},
570 hku
[] = {'H','K','E','Y','_','U','S','E','R','S',0},
571 hkcc
[] = {'H','K','E','Y','_','C','U','R','R','E','N','T','_','C','O','N','F','I','G',0},
572 hkdd
[] = {'H','K','E','Y','_','D','Y','N','_','D','A','T','A',0};
574 tvins
.u
.item
.mask
= TVIF_TEXT
| TVIF_IMAGE
| TVIF_SELECTEDIMAGE
| TVIF_CHILDREN
| TVIF_PARAM
;
575 /* Set the text of the item. */
576 tvins
.u
.item
.pszText
= pHostName
;
577 tvins
.u
.item
.cchTextMax
= lstrlen(pHostName
);
578 /* Assume the item is not a parent item, so give it an image. */
579 tvins
.u
.item
.iImage
= Image_Root
;
580 tvins
.u
.item
.iSelectedImage
= Image_Root
;
581 tvins
.u
.item
.cChildren
= 5;
582 /* Save the heading level in the item's application-defined data area. */
583 tvins
.u
.item
.lParam
= (LPARAM
)NULL
;
584 tvins
.hInsertAfter
= (HTREEITEM
)TVI_FIRST
;
585 tvins
.hParent
= TVI_ROOT
;
586 /* Add the item to the tree view control. */
587 if (!(hRoot
= TreeView_InsertItem(hwndTV
, &tvins
))) return FALSE
;
589 if (!AddEntryToTree(hwndTV
, hRoot
, hkcr
, HKEY_CLASSES_ROOT
, 1)) return FALSE
;
590 if (!AddEntryToTree(hwndTV
, hRoot
, hkcu
, HKEY_CURRENT_USER
, 1)) return FALSE
;
591 if (!AddEntryToTree(hwndTV
, hRoot
, hklm
, HKEY_LOCAL_MACHINE
, 1)) return FALSE
;
592 if (!AddEntryToTree(hwndTV
, hRoot
, hku
, HKEY_USERS
, 1)) return FALSE
;
593 if (!AddEntryToTree(hwndTV
, hRoot
, hkcc
, HKEY_CURRENT_CONFIG
, 1)) return FALSE
;
594 if (!AddEntryToTree(hwndTV
, hRoot
, hkdd
, HKEY_DYN_DATA
, 1)) return FALSE
;
596 /* expand and select host name */
597 SendMessage(hwndTV
, TVM_EXPAND
, TVE_EXPAND
, (LPARAM
)hRoot
);
598 SendMessage(hwndTV
, TVM_SELECTITEM
, TVGN_CARET
, (LPARAM
)hRoot
);
604 * InitTreeViewImageLists - creates an image list, adds three bitmaps
605 * to it, and associates the image list with a tree view control.
606 * Returns TRUE if successful, or FALSE otherwise.
607 * hwndTV - handle to the tree view control.
609 static BOOL
InitTreeViewImageLists(HWND hwndTV
)
611 HIMAGELIST himl
; /* handle to image list */
612 HICON hico
; /* handle to icon */
614 /* Create the image list. */
615 if ((himl
= ImageList_Create(CX_ICON
, CY_ICON
,
616 ILC_MASK
, 0, NUM_ICONS
)) == NULL
)
619 /* Add the open file, closed file, and document bitmaps. */
620 hico
= LoadIcon(hInst
, MAKEINTRESOURCE(IDI_OPEN_FILE
));
621 Image_Open
= ImageList_AddIcon(himl
, hico
);
623 hico
= LoadIcon(hInst
, MAKEINTRESOURCE(IDI_CLOSED_FILE
));
624 Image_Closed
= ImageList_AddIcon(himl
, hico
);
626 hico
= LoadIcon(hInst
, MAKEINTRESOURCE(IDI_ROOT
));
627 Image_Root
= ImageList_AddIcon(himl
, hico
);
629 /* Fail if not all of the images were added. */
630 if (ImageList_GetImageCount(himl
) < NUM_ICONS
)
635 /* Associate the image list with the tree view control. */
636 SendMessage(hwndTV
, TVM_SETIMAGELIST
, TVSIL_NORMAL
, (LPARAM
)himl
);
641 BOOL
UpdateExpandingTree(HWND hwndTV
, HTREEITEM hItem
, int state
)
643 DWORD dwCount
, dwIndex
, dwMaxSubKeyLen
;
644 HKEY hRoot
, hNewKey
, hKey
;
650 static int expanding
;
651 if (expanding
) return FALSE
;
652 if (state
& TVIS_EXPANDEDONCE
) {
656 hcursorOld
= SetCursor(LoadCursor(NULL
, IDC_WAIT
));
657 SendMessage(hwndTV
, WM_SETREDRAW
, FALSE
, 0);
659 keyPath
= GetItemPath(hwndTV
, hItem
, &hRoot
);
660 if (!keyPath
) goto done
;
663 errCode
= RegOpenKeyEx(hRoot
, keyPath
, 0, KEY_READ
, &hNewKey
);
664 if (errCode
!= ERROR_SUCCESS
) goto done
;
669 errCode
= RegQueryInfoKey(hNewKey
, 0, 0, 0, &dwCount
, &dwMaxSubKeyLen
, 0, 0, 0, 0, 0, 0);
670 if (errCode
!= ERROR_SUCCESS
) goto done
;
671 dwMaxSubKeyLen
++; /* account for the \0 terminator */
672 Name
= HeapAlloc(GetProcessHeap(), 0, dwMaxSubKeyLen
* sizeof(TCHAR
));
673 if (!Name
) goto done
;
675 for (dwIndex
= 0; dwIndex
< dwCount
; dwIndex
++) {
676 DWORD cName
= dwMaxSubKeyLen
, dwSubCount
;
678 errCode
= RegEnumKeyEx(hNewKey
, dwIndex
, Name
, &cName
, 0, 0, 0, 0);
679 if (errCode
!= ERROR_SUCCESS
) continue;
680 errCode
= RegOpenKeyEx(hNewKey
, Name
, 0, KEY_QUERY_VALUE
, &hKey
);
681 if (errCode
== ERROR_SUCCESS
) {
682 errCode
= RegQueryInfoKey(hKey
, 0, 0, 0, &dwSubCount
, 0, 0, 0, 0, 0, 0, 0);
685 if (errCode
!= ERROR_SUCCESS
) dwSubCount
= 0;
686 AddEntryToTree(hwndTV
, hItem
, Name
, NULL
, dwSubCount
);
688 RegCloseKey(hNewKey
);
689 HeapFree(GetProcessHeap(), 0, Name
);
692 TreeView_SetItemState(hwndTV
, hItem
, TVIS_EXPANDEDONCE
, TVIS_EXPANDEDONCE
);
693 SendMessage(hwndTV
, WM_SETREDRAW
, TRUE
, 0);
694 SetCursor(hcursorOld
);
696 HeapFree(GetProcessHeap(), 0, keyPath
);
701 BOOL
OnTreeExpanding(HWND hwndTV
, NMTREEVIEW
* pnmtv
)
703 return UpdateExpandingTree(hwndTV
, pnmtv
->itemNew
.hItem
, pnmtv
->itemNew
.state
);
708 * CreateTreeView - creates a tree view control.
709 * Returns the handle to the new control if successful, or NULL otherwise.
710 * hwndParent - handle to the control's parent window.
712 HWND
CreateTreeView(HWND hwndParent
, LPTSTR pHostName
, UINT id
)
717 /* Get the dimensions of the parent window's client area, and create the tree view control. */
718 GetClientRect(hwndParent
, &rcClient
);
719 hwndTV
= CreateWindowEx(WS_EX_CLIENTEDGE
, WC_TREEVIEW
, _T("Tree View"),
720 WS_VISIBLE
| WS_CHILD
| WS_TABSTOP
| TVS_HASLINES
| TVS_HASBUTTONS
| TVS_LINESATROOT
,
721 0, 0, rcClient
.right
, rcClient
.bottom
,
722 hwndParent
, (HMENU
)ULongToHandle(id
), hInst
, NULL
);
723 /* Initialize the image list, and add items to the control. */
724 if (!InitTreeViewImageLists(hwndTV
) || !InitTreeViewItems(hwndTV
, pHostName
)) {
725 DestroyWindow(hwndTV
);