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 */
29 #include <wine/debug.h>
31 WINE_DEFAULT_DEBUG_CHANNEL(regedit
);
33 /* Global variables and constants */
34 /* Image_Open, Image_Closed, and Image_Root - integer variables for indexes of the images. */
35 /* CX_BITMAP and CY_BITMAP - width and height of an icon. */
36 /* NUM_BITMAPS - number of bitmaps to add to the image list. */
43 static BOOL
UpdateExpandingTree(HWND hwndTV
, HTREEITEM hItem
, int state
);
45 static BOOL
get_item_path(HWND hwndTV
, HTREEITEM hItem
, HKEY
* phKey
, LPWSTR
* pKeyPath
, int* pPathLen
, int* pMaxChars
)
51 item
.mask
= TVIF_PARAM
;
53 if (!TreeView_GetItemW(hwndTV
, &item
)) return FALSE
;
56 /* found root key with valid key value */
57 *phKey
= (HKEY
)item
.lParam
;
61 hParent
= (HTREEITEM
)SendMessageW(hwndTV
, TVM_GETNEXTITEM
, TVGN_PARENT
, (LPARAM
)hItem
);
62 if(!get_item_path(hwndTV
, hParent
, phKey
, pKeyPath
, pPathLen
, pMaxChars
)) return FALSE
;
64 (*pKeyPath
)[*pPathLen
] = '\\';
69 item
.mask
= TVIF_TEXT
;
71 item
.pszText
= *pKeyPath
+ *pPathLen
;
72 item
.cchTextMax
= maxChars
= *pMaxChars
- *pPathLen
;
73 if (!TreeView_GetItemW(hwndTV
, &item
)) return FALSE
;
74 chars
= lstrlenW(item
.pszText
);
75 if (chars
< maxChars
- 1) {
81 *pKeyPath
= realloc(*pKeyPath
, *pMaxChars
);
87 LPWSTR
GetItemPath(HWND hwndTV
, HTREEITEM hItem
, HKEY
* phRootKey
)
89 int pathLen
= 0, maxLen
= 1024;
93 hItem
= (HTREEITEM
)SendMessageW(hwndTV
, TVM_GETNEXTITEM
, TVGN_CARET
, 0);
94 if (!hItem
) return NULL
;
97 pathBuffer
= malloc(maxLen
* sizeof(WCHAR
));
98 if (!pathBuffer
) return NULL
;
100 if (!get_item_path(hwndTV
, hItem
, phRootKey
, &pathBuffer
, &pathLen
, &maxLen
)) {
107 static LPWSTR
get_path_component(LPCWSTR
*lplpKeyName
) {
108 LPCWSTR lpPos
= *lplpKeyName
;
109 LPWSTR lpResult
= NULL
;
113 while(*lpPos
&& *lpPos
!= '\\')
115 if (*lpPos
&& lpPos
== *lplpKeyName
)
118 len
= lpPos
+1-(*lplpKeyName
);
119 lpResult
= malloc(len
* sizeof(WCHAR
));
121 lstrcpynW(lpResult
, *lplpKeyName
, len
);
122 *lplpKeyName
= *lpPos
? lpPos
+1 : NULL
;
126 HTREEITEM
FindPathInTree(HWND hwndTV
, LPCWSTR lpKeyName
) {
128 WCHAR buf
[261]; /* tree view has 260 character limitation on item name */
129 HTREEITEM hRoot
, hItem
, hOldItem
;
133 hRoot
= (HTREEITEM
)SendMessageW(hwndTV
, TVM_GETNEXTITEM
, TVGN_ROOT
, 0);
135 SendMessageW(hwndTV
, TVM_EXPAND
, TVE_EXPAND
, (LPARAM
)hItem
);
136 hItem
= (HTREEITEM
)SendMessageW(hwndTV
, TVM_GETNEXTITEM
, TVGN_CHILD
, (LPARAM
)hItem
);
140 LPWSTR lpItemName
= get_path_component(&lpKeyName
);
144 tvi
.mask
= TVIF_TEXT
| TVIF_HANDLE
;
147 tvi
.cchTextMax
= 260;
148 SendMessageW(hwndTV
, TVM_GETITEMW
, 0, (LPARAM
) &tvi
);
149 if (!lstrcmpiW(tvi
.pszText
, lpItemName
)) {
151 SendMessageW(hwndTV
, TVM_EXPAND
, TVE_EXPAND
, (LPARAM
)hItem
);
158 hItem
= (HTREEITEM
)SendMessageW(hwndTV
, TVM_GETNEXTITEM
, TVGN_CHILD
, (LPARAM
)hItem
);
161 hItem
= (HTREEITEM
)SendMessageW(hwndTV
, TVM_GETNEXTITEM
, TVGN_NEXT
, (LPARAM
)hItem
);
165 return valid_path
? hOldItem
: hRoot
;
168 return valid_path
? hItem
: hRoot
;
172 BOOL
DeleteNode(HWND hwndTV
, HTREEITEM hItem
)
174 if (!hItem
) hItem
= (HTREEITEM
)SendMessageW(hwndTV
, TVM_GETNEXTITEM
, TVGN_CARET
, 0);
175 if (!hItem
) return FALSE
;
176 return (BOOL
)SendMessageW(hwndTV
, TVM_DELETEITEM
, 0, (LPARAM
)hItem
);
179 /* Add an entry to the tree. Only give hKey for root nodes (HKEY_ constants) */
180 static HTREEITEM
AddEntryToTree(HWND hwndTV
, HTREEITEM hParent
, LPWSTR label
, HKEY hKey
, DWORD dwChildren
)
182 TVINSERTSTRUCTW tvins
;
185 if (RegQueryInfoKeyW(hKey
, 0, 0, 0, &dwChildren
, 0, 0, 0, 0, 0, 0, 0) != ERROR_SUCCESS
) {
190 tvins
.item
.mask
= TVIF_TEXT
| TVIF_IMAGE
| TVIF_SELECTEDIMAGE
| TVIF_CHILDREN
| TVIF_PARAM
;
191 tvins
.item
.pszText
= label
;
192 tvins
.item
.cchTextMax
= lstrlenW(label
);
193 tvins
.item
.iImage
= Image_Closed
;
194 tvins
.item
.iSelectedImage
= Image_Open
;
195 tvins
.item
.cChildren
= dwChildren
;
196 tvins
.item
.lParam
= (LPARAM
)hKey
;
197 tvins
.hInsertAfter
= hKey
? TVI_LAST
: TVI_SORT
;
198 tvins
.hParent
= hParent
;
200 return TreeView_InsertItemW(hwndTV
, &tvins
);
203 static BOOL
match_string(LPCWSTR sstring1
, LPCWSTR sstring2
, int mode
)
205 if (mode
& SEARCH_WHOLE
)
206 return !lstrcmpiW(sstring1
, sstring2
);
208 return NULL
!= StrStrIW(sstring1
, sstring2
);
211 static BOOL
match_item(HWND hwndTV
, HTREEITEM hItem
, LPCWSTR sstring
, int mode
, int *row
)
214 WCHAR keyname
[KEY_MAX_LEN
];
215 item
.mask
= TVIF_TEXT
;
217 item
.pszText
= keyname
;
218 item
.cchTextMax
= KEY_MAX_LEN
;
219 if (!TreeView_GetItemW(hwndTV
, &item
)) return FALSE
;
220 if ((mode
& SEARCH_KEYS
) && match_string(keyname
, sstring
, mode
)) {
225 if (mode
& (SEARCH_VALUES
| SEARCH_CONTENT
)) {
227 WCHAR
*valName
, *KeyPath
;
229 DWORD lenName
, lenNameMax
, lenValueMax
;
230 WCHAR
*buffer
= NULL
;
232 KeyPath
= GetItemPath(hwndTV
, hItem
, &hRoot
);
234 if (!KeyPath
|| !hRoot
)
237 if (RegOpenKeyExW(hRoot
, KeyPath
, 0, KEY_READ
, &hKey
) != ERROR_SUCCESS
) {
244 if (ERROR_SUCCESS
!= RegQueryInfoKeyW(hKey
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, &lenNameMax
, &lenValueMax
, NULL
, NULL
))
247 lenName
= ++lenNameMax
;
248 valName
= malloc(lenName
* sizeof(WCHAR
));
252 /* RegEnumValue won't return empty default value, so fake it when dealing with *row,
253 which corresponds to list view rows, not value ids */
254 if (ERROR_SUCCESS
== RegEnumValueW(hKey
, 0, valName
, &lenName
, NULL
, NULL
, NULL
, NULL
) && *valName
)
260 DWORD lenValue
= 0, type
= 0;
261 lenName
= lenNameMax
;
263 if (ERROR_SUCCESS
!= RegEnumValueW(hKey
,
264 i
, valName
, &lenName
, NULL
, &type
, NULL
, NULL
))
267 if (mode
& SEARCH_VALUES
) {
268 if (match_string(valName
, sstring
, mode
)) {
277 if ((mode
& SEARCH_CONTENT
) && (type
== REG_EXPAND_SZ
|| type
== REG_SZ
)) {
279 buffer
= malloc(lenValueMax
);
281 lenName
= lenNameMax
;
282 lenValue
= lenValueMax
;
283 if (ERROR_SUCCESS
!= RegEnumValueW(hKey
, i
, valName
, &lenName
, NULL
, &type
, (LPBYTE
)buffer
, &lenValue
))
285 if (match_string(buffer
, sstring
, mode
)) {
303 HTREEITEM
FindNext(HWND hwndTV
, HTREEITEM hItem
, LPCWSTR sstring
, int mode
, int *row
)
305 HTREEITEM hTry
, hLast
;
309 if (match_item(hwndTV
, hLast
, sstring
, mode
& ~SEARCH_KEYS
, row
)) {
315 /* first look in subtree */
316 /* no children? maybe we haven't loaded them yet? */
317 if (!SendMessageW(hwndTV
, TVM_GETNEXTITEM
, TVGN_CHILD
, (LPARAM
)hLast
)) {
318 UINT state
= (UINT
)SendMessageW(hwndTV
, TVM_GETITEMSTATE
, (WPARAM
)hLast
, -1);
319 UpdateExpandingTree(hwndTV
, hLast
, state
);
321 hTry
= (HTREEITEM
)SendMessageW(hwndTV
, TVM_GETNEXTITEM
, TVGN_CHILD
, (LPARAM
)hLast
);
323 if (match_item(hwndTV
, hTry
, sstring
, mode
, row
))
328 /* no more children, maybe there are any siblings? */
329 hTry
= (HTREEITEM
)SendMessageW(hwndTV
, TVM_GETNEXTITEM
, TVGN_NEXT
, (LPARAM
)hLast
);
331 if (match_item(hwndTV
, hTry
, sstring
, mode
, row
))
336 /* no more siblings, look at the next siblings in parent(s) */
337 hLast
= (HTREEITEM
)SendMessageW(hwndTV
, TVM_GETNEXTITEM
, TVGN_PARENT
, (LPARAM
)hLast
);
340 while (hLast
&& (hTry
= (HTREEITEM
)SendMessageW(hwndTV
, TVM_GETNEXTITEM
, TVGN_NEXT
, (LPARAM
)hLast
)) == NULL
) {
341 hLast
= (HTREEITEM
)SendMessageW(hwndTV
, TVM_GETNEXTITEM
, TVGN_PARENT
, (LPARAM
)hLast
);
343 if (match_item(hwndTV
, hTry
, sstring
, mode
, row
))
350 static BOOL
RefreshTreeItem(HWND hwndTV
, HTREEITEM hItem
)
352 HKEY hRoot
, hKey
, hSubKey
;
355 DWORD dwCount
, dwIndex
, dwMaxSubKeyLen
;
360 KeyPath
= GetItemPath(hwndTV
, hItem
, &hRoot
);
362 if (!KeyPath
|| !hRoot
)
366 if (RegOpenKeyExW(hRoot
, KeyPath
, 0, KEY_READ
, &hKey
) != ERROR_SUCCESS
) {
367 WINE_TRACE("RegOpenKeyEx failed, %s was probably removed.\n", wine_dbgstr_w(KeyPath
));
375 if (RegQueryInfoKeyW(hKey
, 0, 0, 0, &dwCount
, &dwMaxSubKeyLen
, 0, 0, 0, 0, 0, 0) != ERROR_SUCCESS
) {
379 /* Set the number of children again */
380 tvItem
.mask
= TVIF_CHILDREN
;
381 tvItem
.hItem
= hItem
;
382 tvItem
.cChildren
= dwCount
;
383 if (!TreeView_SetItemW(hwndTV
, &tvItem
)) {
387 /* We don't have to bother with the rest if it's not expanded. */
388 if (SendMessageW(hwndTV
, TVM_GETITEMSTATE
, (WPARAM
)hItem
, TVIS_EXPANDED
) == 0) {
393 dwMaxSubKeyLen
++; /* account for the \0 terminator */
394 Name
= malloc(dwMaxSubKeyLen
* sizeof(WCHAR
));
396 tvItem
.cchTextMax
= dwMaxSubKeyLen
;
397 tvItem
.pszText
= malloc(dwMaxSubKeyLen
* sizeof(WCHAR
));
399 /* Now go through all the children in the registry, and check if any have to be added. */
400 for (dwIndex
= 0; dwIndex
< dwCount
; dwIndex
++) {
401 DWORD cName
= dwMaxSubKeyLen
, dwSubCount
;
405 if (RegEnumKeyExW(hKey
, dwIndex
, Name
, &cName
, 0, 0, 0, NULL
) != ERROR_SUCCESS
) {
409 /* Find the number of children of the node. */
411 if (RegOpenKeyExW(hKey
, Name
, 0, KEY_QUERY_VALUE
, &hSubKey
) == ERROR_SUCCESS
) {
412 if (RegQueryInfoKeyW(hSubKey
, 0, 0, 0, &dwSubCount
, 0, 0, 0, 0, 0, 0, 0) != ERROR_SUCCESS
) {
415 RegCloseKey(hSubKey
);
418 /* Check if the node is already in there. */
419 for (childItem
= (HTREEITEM
)SendMessageW(hwndTV
, TVM_GETNEXTITEM
, TVGN_CHILD
, (LPARAM
)hItem
); childItem
;
420 childItem
= (HTREEITEM
)SendMessageW(hwndTV
, TVM_GETNEXTITEM
, TVGN_NEXT
, (LPARAM
)childItem
)) {
421 tvItem
.mask
= TVIF_TEXT
;
422 tvItem
.hItem
= childItem
;
423 if (!TreeView_GetItemW(hwndTV
, &tvItem
)) {
425 free(tvItem
.pszText
);
429 if (!lstrcmpiW(tvItem
.pszText
, Name
)) {
435 if (found
== FALSE
) {
436 WINE_TRACE("New subkey %s\n", wine_dbgstr_w(Name
));
437 AddEntryToTree(hwndTV
, hItem
, Name
, NULL
, dwSubCount
);
441 free(tvItem
.pszText
);
444 /* Now go through all the children in the tree, and check if any have to be removed. */
445 childItem
= (HTREEITEM
)SendMessageW(hwndTV
, TVM_GETNEXTITEM
, TVGN_CHILD
, (LPARAM
)hItem
);
447 HTREEITEM nextItem
= (HTREEITEM
)SendMessageW(hwndTV
, TVM_GETNEXTITEM
, TVGN_NEXT
, (LPARAM
)childItem
);
448 if (RefreshTreeItem(hwndTV
, childItem
) == FALSE
) {
449 SendMessageW(hwndTV
, TVM_DELETEITEM
, 0, (LPARAM
)childItem
);
451 childItem
= nextItem
;
457 static void treeview_sort_item(HWND hWnd
, HTREEITEM item
)
459 HTREEITEM child
= (HTREEITEM
)SendMessageW(hWnd
, TVM_GETNEXTITEM
, TVGN_CHILD
, (LPARAM
)item
);
461 while (child
!= NULL
) {
462 treeview_sort_item(hWnd
, child
);
463 child
= (HTREEITEM
)SendMessageW(hWnd
, TVM_GETNEXTITEM
, TVGN_NEXT
, (LPARAM
)child
);
465 SendMessageW(hWnd
, TVM_SORTCHILDREN
, 0, (LPARAM
)item
);
468 BOOL
RefreshTreeView(HWND hwndTV
)
471 HTREEITEM hSelectedItem
;
476 hSelectedItem
= (HTREEITEM
)SendMessageW(hwndTV
, TVM_GETNEXTITEM
, TVGN_CARET
, 0);
477 hcursorOld
= SetCursor(LoadCursorW(NULL
, (LPCWSTR
)IDC_WAIT
));
478 SendMessageW(hwndTV
, WM_SETREDRAW
, FALSE
, 0);
480 hRoot
= (HTREEITEM
)SendMessageW(hwndTV
, TVM_GETNEXTITEM
, TVGN_ROOT
, 0);
481 hItem
= (HTREEITEM
)SendMessageW(hwndTV
, TVM_GETNEXTITEM
, TVGN_CHILD
, (LPARAM
)hRoot
);
483 RefreshTreeItem(hwndTV
, hItem
);
484 treeview_sort_item(hwndTV
, hItem
);
485 hItem
= (HTREEITEM
)SendMessageW(hwndTV
, TVM_GETNEXTITEM
, TVGN_NEXT
, (LPARAM
)hItem
);
488 SendMessageW(hwndTV
, WM_SETREDRAW
, TRUE
, 0);
489 InvalidateRect(hwndTV
, NULL
, FALSE
);
490 SetCursor(hcursorOld
);
492 /* We reselect the currently selected node, this will prompt a refresh of the listview. */
493 SendMessageW(hwndTV
, TVM_SELECTITEM
, TVGN_CARET
, (LPARAM
)hSelectedItem
);
497 HTREEITEM
InsertNode(HWND hwndTV
, HTREEITEM hItem
, LPWSTR name
)
499 WCHAR buf
[MAX_NEW_KEY_LEN
];
500 HTREEITEM hNewItem
= 0;
503 if (!hItem
) hItem
= (HTREEITEM
)SendMessageW(hwndTV
, TVM_GETNEXTITEM
, TVGN_CARET
, 0);
504 if (!hItem
) return FALSE
;
505 if (SendMessageW(hwndTV
, TVM_GETITEMSTATE
, (WPARAM
)hItem
, TVIS_EXPANDEDONCE
) & TVIS_EXPANDEDONCE
) {
506 hNewItem
= AddEntryToTree(hwndTV
, hItem
, name
, 0, 0);
508 item
.mask
= TVIF_CHILDREN
| TVIF_HANDLE
;
510 if (!TreeView_GetItemW(hwndTV
, &item
)) return FALSE
;
512 if (!TreeView_SetItemW(hwndTV
, &item
)) return FALSE
;
514 SendMessageW(hwndTV
, TVM_EXPAND
, TVE_EXPAND
, (LPARAM
)hItem
);
516 for(hNewItem
= (HTREEITEM
)SendMessageW(hwndTV
, TVM_GETNEXTITEM
, TVGN_CHILD
, (LPARAM
)hItem
); hNewItem
;
517 hNewItem
= (HTREEITEM
)SendMessageW(hwndTV
, TVM_GETNEXTITEM
, TVGN_NEXT
, (LPARAM
)hNewItem
)) {
518 item
.mask
= TVIF_HANDLE
| TVIF_TEXT
;
519 item
.hItem
= hNewItem
;
521 item
.cchTextMax
= ARRAY_SIZE(buf
);
522 if (!TreeView_GetItemW(hwndTV
, &item
)) continue;
523 if (lstrcmpW(name
, item
.pszText
) == 0) break;
527 SendMessageW(hwndTV
, TVM_SELECTITEM
, TVGN_CARET
, (LPARAM
)hNewItem
);
532 HWND
StartKeyRename(HWND hwndTV
)
536 if(!(hItem
= (HTREEITEM
)SendMessageW(hwndTV
, TVM_GETNEXTITEM
, TVGN_CARET
, 0))) return 0;
537 SetWindowLongPtrW(hwndTV
, GWLP_USERDATA
, 1);
538 return (HWND
)SendMessageW(hwndTV
, TVM_EDITLABELW
, 0, (LPARAM
)hItem
);
541 static BOOL
InitTreeViewItems(HWND hwndTV
, LPWSTR pHostName
)
543 TVINSERTSTRUCTW tvins
;
545 static WCHAR hkcr
[] = L
"HKEY_CLASSES_ROOT",
546 hkcu
[] = L
"HKEY_CURRENT_USER",
547 hklm
[] = L
"HKEY_LOCAL_MACHINE",
548 hku
[] = L
"HKEY_USERS",
549 hkcc
[] = L
"HKEY_CURRENT_CONFIG",
550 hkdd
[] = L
"HKEY_DYN_DATA";
552 tvins
.item
.mask
= TVIF_TEXT
| TVIF_IMAGE
| TVIF_SELECTEDIMAGE
| TVIF_CHILDREN
| TVIF_PARAM
;
553 /* Set the text of the item. */
554 tvins
.item
.pszText
= pHostName
;
555 tvins
.item
.cchTextMax
= lstrlenW(pHostName
);
556 /* Assume the item is not a parent item, so give it an image. */
557 tvins
.item
.iImage
= Image_Root
;
558 tvins
.item
.iSelectedImage
= Image_Root
;
559 tvins
.item
.cChildren
= 5;
560 /* Save the heading level in the item's application-defined data area. */
561 tvins
.item
.lParam
= 0;
562 tvins
.hInsertAfter
= TVI_FIRST
;
563 tvins
.hParent
= TVI_ROOT
;
564 /* Add the item to the tree view control. */
565 if (!(hRoot
= TreeView_InsertItemW(hwndTV
, &tvins
))) return FALSE
;
567 if (!AddEntryToTree(hwndTV
, hRoot
, hkcr
, HKEY_CLASSES_ROOT
, 1)) return FALSE
;
568 if (!AddEntryToTree(hwndTV
, hRoot
, hkcu
, HKEY_CURRENT_USER
, 1)) return FALSE
;
569 if (!AddEntryToTree(hwndTV
, hRoot
, hklm
, HKEY_LOCAL_MACHINE
, 1)) return FALSE
;
570 if (!AddEntryToTree(hwndTV
, hRoot
, hku
, HKEY_USERS
, 1)) return FALSE
;
571 if (!AddEntryToTree(hwndTV
, hRoot
, hkcc
, HKEY_CURRENT_CONFIG
, 1)) return FALSE
;
572 if (!AddEntryToTree(hwndTV
, hRoot
, hkdd
, HKEY_DYN_DATA
, 1)) return FALSE
;
579 * InitTreeViewImageLists - creates an image list, adds three bitmaps
580 * to it, and associates the image list with a tree view control.
581 * Returns TRUE if successful, or FALSE otherwise.
582 * hwndTV - handle to the tree view control.
584 static BOOL
InitTreeViewImageLists(HWND hwndTV
)
586 HIMAGELIST himl
; /* handle to image list */
587 HICON hico
; /* handle to icon */
588 INT cx
= GetSystemMetrics(SM_CXSMICON
);
589 INT cy
= GetSystemMetrics(SM_CYSMICON
);
591 /* Create the image list. */
592 if ((himl
= ImageList_Create(cx
, cy
, ILC_MASK
, 0, NUM_ICONS
)) == NULL
)
595 /* Add the open file, closed file, and document bitmaps. */
596 hico
= LoadIconW(hInst
, MAKEINTRESOURCEW(IDI_OPEN_FILE
));
597 Image_Open
= ImageList_AddIcon(himl
, hico
);
599 hico
= LoadIconW(hInst
, MAKEINTRESOURCEW(IDI_CLOSED_FILE
));
600 Image_Closed
= ImageList_AddIcon(himl
, hico
);
602 hico
= LoadIconW(hInst
, MAKEINTRESOURCEW(IDI_ROOT
));
603 Image_Root
= ImageList_AddIcon(himl
, hico
);
605 /* Fail if not all of the images were added. */
606 if (ImageList_GetImageCount(himl
) < NUM_ICONS
)
611 /* Associate the image list with the tree view control. */
612 SendMessageW(hwndTV
, TVM_SETIMAGELIST
, TVSIL_NORMAL
, (LPARAM
)himl
);
617 BOOL
UpdateExpandingTree(HWND hwndTV
, HTREEITEM hItem
, int state
)
619 DWORD dwCount
, dwIndex
, dwMaxSubKeyLen
;
620 HKEY hRoot
, hNewKey
, hKey
;
627 static int expanding
;
628 if (expanding
) return FALSE
;
629 if (state
& TVIS_EXPANDEDONCE
) {
633 hcursorOld
= SetCursor(LoadCursorW(NULL
, (LPCWSTR
)IDC_WAIT
));
634 SendMessageW(hwndTV
, WM_SETREDRAW
, FALSE
, 0);
636 keyPath
= GetItemPath(hwndTV
, hItem
, &hRoot
);
637 if (!keyPath
) goto done
;
640 errCode
= RegOpenKeyExW(hRoot
, keyPath
, 0, KEY_READ
, &hNewKey
);
641 if (errCode
!= ERROR_SUCCESS
) goto done
;
646 errCode
= RegQueryInfoKeyW(hNewKey
, 0, 0, 0, &dwCount
, &dwMaxSubKeyLen
, 0, 0, 0, 0, 0, 0);
647 if (errCode
!= ERROR_SUCCESS
) goto done
;
648 dwMaxSubKeyLen
++; /* account for the \0 terminator */
649 Name
= malloc(dwMaxSubKeyLen
* sizeof(WCHAR
));
651 for (dwIndex
= 0; dwIndex
< dwCount
; dwIndex
++) {
652 DWORD cName
= dwMaxSubKeyLen
, dwSubCount
;
654 errCode
= RegEnumKeyExW(hNewKey
, dwIndex
, Name
, &cName
, 0, 0, 0, 0);
655 if (errCode
!= ERROR_SUCCESS
) continue;
656 errCode
= RegOpenKeyExW(hNewKey
, Name
, 0, KEY_QUERY_VALUE
, &hKey
);
657 if (errCode
== ERROR_SUCCESS
) {
658 errCode
= RegQueryInfoKeyW(hKey
, 0, 0, 0, &dwSubCount
, 0, 0, 0, 0, 0, 0, 0);
661 if (errCode
!= ERROR_SUCCESS
) dwSubCount
= 0;
662 AddEntryToTree(hwndTV
, hItem
, Name
, NULL
, dwSubCount
);
664 RegCloseKey(hNewKey
);
668 item
.mask
= TVIF_STATE
;
670 item
.stateMask
= TVIS_EXPANDEDONCE
;
671 item
.state
= TVIS_EXPANDEDONCE
;
672 SendMessageW(hwndTV
, TVM_SETITEMW
, 0, (LPARAM
)&item
);
673 SendMessageW(hwndTV
, WM_SETREDRAW
, TRUE
, 0);
674 SetCursor(hcursorOld
);
681 BOOL
OnTreeExpanding(HWND hwndTV
, NMTREEVIEWW
* pnmtv
)
683 return UpdateExpandingTree(hwndTV
, pnmtv
->itemNew
.hItem
, pnmtv
->itemNew
.state
);
688 * CreateTreeView - creates a tree view control.
689 * Returns the handle to the new control if successful, or NULL otherwise.
690 * hwndParent - handle to the control's parent window.
692 HWND
CreateTreeView(HWND hwndParent
, LPWSTR pHostName
, UINT id
)
697 /* Get the dimensions of the parent window's client area, and create the tree view control. */
698 GetClientRect(hwndParent
, &rcClient
);
699 hwndTV
= CreateWindowExW(WS_EX_CLIENTEDGE
, WC_TREEVIEWW
, L
"Tree View",
700 WS_VISIBLE
| WS_CHILD
| WS_TABSTOP
| WS_CLIPSIBLINGS
|
701 TVS_HASLINES
| TVS_HASBUTTONS
|
702 TVS_LINESATROOT
| TVS_EDITLABELS
| TVS_SHOWSELALWAYS
,
703 0, 0, rcClient
.right
, rcClient
.bottom
,
704 hwndParent
, ULongToHandle(id
), hInst
, NULL
);
705 SendMessageW(hwndTV
, TVM_SETUNICODEFORMAT
, TRUE
, 0);
706 /* Initialize the image list, and add items to the control. */
707 if (!InitTreeViewImageLists(hwndTV
) || !InitTreeViewItems(hwndTV
, pHostName
)) {
708 DestroyWindow(hwndTV
);