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
29 #include <wine/debug.h>
32 #include "wine/heap.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(regedit
);
37 /* Global variables and constants */
38 /* Image_Open, Image_Closed, and Image_Root - integer variables for indexes of the images. */
39 /* CX_BITMAP and CY_BITMAP - width and height of an icon. */
40 /* NUM_BITMAPS - number of bitmaps to add to the image list. */
47 static BOOL
UpdateExpandingTree(HWND hwndTV
, HTREEITEM hItem
, int state
);
49 static BOOL
get_item_path(HWND hwndTV
, HTREEITEM hItem
, HKEY
* phKey
, LPWSTR
* pKeyPath
, int* pPathLen
, int* pMaxChars
)
55 item
.mask
= TVIF_PARAM
;
57 if (!TreeView_GetItemW(hwndTV
, &item
)) return FALSE
;
60 /* found root key with valid key value */
61 *phKey
= (HKEY
)item
.lParam
;
65 hParent
= (HTREEITEM
)SendMessageW(hwndTV
, TVM_GETNEXTITEM
, TVGN_PARENT
, (LPARAM
)hItem
);
66 if(!get_item_path(hwndTV
, hParent
, phKey
, pKeyPath
, pPathLen
, pMaxChars
)) return FALSE
;
68 (*pKeyPath
)[*pPathLen
] = '\\';
73 item
.mask
= TVIF_TEXT
;
75 item
.pszText
= *pKeyPath
+ *pPathLen
;
76 item
.cchTextMax
= maxChars
= *pMaxChars
- *pPathLen
;
77 if (!TreeView_GetItemW(hwndTV
, &item
)) return FALSE
;
78 chars
= lstrlenW(item
.pszText
);
79 if (chars
< maxChars
- 1) {
85 *pKeyPath
= heap_xrealloc(*pKeyPath
, *pMaxChars
);
91 LPWSTR
GetItemPath(HWND hwndTV
, HTREEITEM hItem
, HKEY
* phRootKey
)
93 int pathLen
= 0, maxLen
= 1024;
97 hItem
= (HTREEITEM
)SendMessageW(hwndTV
, TVM_GETNEXTITEM
, TVGN_CARET
, 0);
98 if (!hItem
) return NULL
;
101 pathBuffer
= heap_xalloc(maxLen
* sizeof(WCHAR
));
102 if (!pathBuffer
) return NULL
;
104 if (!get_item_path(hwndTV
, hItem
, phRootKey
, &pathBuffer
, &pathLen
, &maxLen
)) return NULL
;
108 static LPWSTR
get_path_component(LPCWSTR
*lplpKeyName
) {
109 LPCWSTR lpPos
= *lplpKeyName
;
110 LPWSTR lpResult
= NULL
;
114 while(*lpPos
&& *lpPos
!= '\\')
116 if (*lpPos
&& lpPos
== *lplpKeyName
)
119 len
= lpPos
+1-(*lplpKeyName
);
120 lpResult
= heap_xalloc(len
* sizeof(WCHAR
));
122 lstrcpynW(lpResult
, *lplpKeyName
, len
);
123 *lplpKeyName
= *lpPos
? lpPos
+1 : NULL
;
127 HTREEITEM
FindPathInTree(HWND hwndTV
, LPCWSTR lpKeyName
) {
129 WCHAR buf
[261]; /* tree view has 260 character limitation on item name */
130 HTREEITEM hRoot
, hItem
, hOldItem
;
134 hRoot
= (HTREEITEM
)SendMessageW(hwndTV
, TVM_GETNEXTITEM
, TVGN_ROOT
, 0);
136 SendMessageW(hwndTV
, TVM_EXPAND
, TVE_EXPAND
, (LPARAM
)hItem
);
137 hItem
= (HTREEITEM
)SendMessageW(hwndTV
, TVM_GETNEXTITEM
, TVGN_CHILD
, (LPARAM
)hItem
);
141 LPWSTR lpItemName
= get_path_component(&lpKeyName
);
145 tvi
.mask
= TVIF_TEXT
| TVIF_HANDLE
;
148 tvi
.cchTextMax
= 260;
149 SendMessageW(hwndTV
, TVM_GETITEMW
, 0, (LPARAM
) &tvi
);
150 if (!lstrcmpiW(tvi
.pszText
, lpItemName
)) {
152 SendMessageW(hwndTV
, TVM_EXPAND
, TVE_EXPAND
, (LPARAM
)hItem
);
155 heap_free(lpItemName
);
159 hItem
= (HTREEITEM
)SendMessageW(hwndTV
, TVM_GETNEXTITEM
, TVGN_CHILD
, (LPARAM
)hItem
);
162 hItem
= (HTREEITEM
)SendMessageW(hwndTV
, TVM_GETNEXTITEM
, TVGN_NEXT
, (LPARAM
)hItem
);
164 heap_free(lpItemName
);
166 return valid_path
? hOldItem
: hRoot
;
169 return valid_path
? hItem
: hRoot
;
173 BOOL
DeleteNode(HWND hwndTV
, HTREEITEM hItem
)
175 if (!hItem
) hItem
= (HTREEITEM
)SendMessageW(hwndTV
, TVM_GETNEXTITEM
, TVGN_CARET
, 0);
176 if (!hItem
) return FALSE
;
177 return (BOOL
)SendMessageW(hwndTV
, TVM_DELETEITEM
, 0, (LPARAM
)hItem
);
180 /* Add an entry to the tree. Only give hKey for root nodes (HKEY_ constants) */
181 static HTREEITEM
AddEntryToTree(HWND hwndTV
, HTREEITEM hParent
, LPWSTR label
, HKEY hKey
, DWORD dwChildren
)
183 TVINSERTSTRUCTW tvins
;
186 if (RegQueryInfoKeyW(hKey
, 0, 0, 0, &dwChildren
, 0, 0, 0, 0, 0, 0, 0) != ERROR_SUCCESS
) {
191 tvins
.u
.item
.mask
= TVIF_TEXT
| TVIF_IMAGE
| TVIF_SELECTEDIMAGE
| TVIF_CHILDREN
| TVIF_PARAM
;
192 tvins
.u
.item
.pszText
= label
;
193 tvins
.u
.item
.cchTextMax
= lstrlenW(label
);
194 tvins
.u
.item
.iImage
= Image_Closed
;
195 tvins
.u
.item
.iSelectedImage
= Image_Open
;
196 tvins
.u
.item
.cChildren
= dwChildren
;
197 tvins
.u
.item
.lParam
= (LPARAM
)hKey
;
198 tvins
.hInsertAfter
= hKey
? TVI_LAST
: TVI_SORT
;
199 tvins
.hParent
= hParent
;
201 return TreeView_InsertItemW(hwndTV
, &tvins
);
204 static BOOL
match_string(LPCWSTR sstring1
, LPCWSTR sstring2
, int mode
)
206 if (mode
& SEARCH_WHOLE
)
207 return !lstrcmpiW(sstring1
, sstring2
);
209 return NULL
!= StrStrIW(sstring1
, sstring2
);
212 static BOOL
match_item(HWND hwndTV
, HTREEITEM hItem
, LPCWSTR sstring
, int mode
, int *row
)
215 WCHAR keyname
[KEY_MAX_LEN
];
216 item
.mask
= TVIF_TEXT
;
218 item
.pszText
= keyname
;
219 item
.cchTextMax
= KEY_MAX_LEN
;
220 if (!TreeView_GetItemW(hwndTV
, &item
)) return FALSE
;
221 if ((mode
& SEARCH_KEYS
) && match_string(keyname
, sstring
, mode
)) {
226 if (mode
& (SEARCH_VALUES
| SEARCH_CONTENT
)) {
228 WCHAR
*valName
, *KeyPath
;
230 DWORD lenName
, lenNameMax
, lenValueMax
;
231 WCHAR
*buffer
= NULL
;
233 KeyPath
= GetItemPath(hwndTV
, hItem
, &hRoot
);
235 if (!KeyPath
|| !hRoot
)
238 if (RegOpenKeyExW(hRoot
, KeyPath
, 0, KEY_READ
, &hKey
) != ERROR_SUCCESS
) {
245 if (ERROR_SUCCESS
!= RegQueryInfoKeyW(hKey
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, &lenNameMax
, &lenValueMax
, NULL
, NULL
))
248 lenName
= ++lenNameMax
;
249 valName
= heap_xalloc(lenName
* sizeof(WCHAR
));
253 /* RegEnumValue won't return empty default value, so fake it when dealing with *row,
254 which corresponds to list view rows, not value ids */
255 if (ERROR_SUCCESS
== RegEnumValueW(hKey
, 0, valName
, &lenName
, NULL
, NULL
, NULL
, NULL
) && *valName
)
261 DWORD lenValue
= 0, type
= 0;
262 lenName
= lenNameMax
;
264 if (ERROR_SUCCESS
!= RegEnumValueW(hKey
,
265 i
, valName
, &lenName
, NULL
, &type
, NULL
, NULL
))
268 if (mode
& SEARCH_VALUES
) {
269 if (match_string(valName
, sstring
, mode
)) {
278 if ((mode
& SEARCH_CONTENT
) && (type
== REG_EXPAND_SZ
|| type
== REG_SZ
)) {
280 buffer
= heap_xalloc(lenValueMax
);
282 lenName
= lenNameMax
;
283 lenValue
= lenValueMax
;
284 if (ERROR_SUCCESS
!= RegEnumValueW(hKey
, i
, valName
, &lenName
, NULL
, &type
, (LPBYTE
)buffer
, &lenValue
))
286 if (match_string(buffer
, sstring
, mode
)) {
304 HTREEITEM
FindNext(HWND hwndTV
, HTREEITEM hItem
, LPCWSTR sstring
, int mode
, int *row
)
306 HTREEITEM hTry
, hLast
;
310 if (match_item(hwndTV
, hLast
, sstring
, mode
& ~SEARCH_KEYS
, row
)) {
316 /* first look in subtree */
317 /* no children? maybe we haven't loaded them yet? */
318 if (!SendMessageW(hwndTV
, TVM_GETNEXTITEM
, TVGN_CHILD
, (LPARAM
)hLast
)) {
319 UINT state
= (UINT
)SendMessageW(hwndTV
, TVM_GETITEMSTATE
, (WPARAM
)hLast
, -1);
320 UpdateExpandingTree(hwndTV
, hLast
, state
);
322 hTry
= (HTREEITEM
)SendMessageW(hwndTV
, TVM_GETNEXTITEM
, TVGN_CHILD
, (LPARAM
)hLast
);
324 if (match_item(hwndTV
, hTry
, sstring
, mode
, row
))
329 /* no more children, maybe there are any siblings? */
330 hTry
= (HTREEITEM
)SendMessageW(hwndTV
, TVM_GETNEXTITEM
, TVGN_NEXT
, (LPARAM
)hLast
);
332 if (match_item(hwndTV
, hTry
, sstring
, mode
, row
))
337 /* no more siblings, look at the next siblings in parent(s) */
338 hLast
= (HTREEITEM
)SendMessageW(hwndTV
, TVM_GETNEXTITEM
, TVGN_PARENT
, (LPARAM
)hLast
);
341 while (hLast
&& (hTry
= (HTREEITEM
)SendMessageW(hwndTV
, TVM_GETNEXTITEM
, TVGN_NEXT
, (LPARAM
)hLast
)) == NULL
) {
342 hLast
= (HTREEITEM
)SendMessageW(hwndTV
, TVM_GETNEXTITEM
, TVGN_PARENT
, (LPARAM
)hLast
);
344 if (match_item(hwndTV
, hTry
, sstring
, mode
, row
))
351 static BOOL
RefreshTreeItem(HWND hwndTV
, HTREEITEM hItem
)
353 HKEY hRoot
, hKey
, hSubKey
;
356 DWORD dwCount
, dwIndex
, dwMaxSubKeyLen
;
361 KeyPath
= GetItemPath(hwndTV
, hItem
, &hRoot
);
363 if (!KeyPath
|| !hRoot
)
367 if (RegOpenKeyExW(hRoot
, KeyPath
, 0, KEY_READ
, &hKey
) != ERROR_SUCCESS
) {
368 WINE_TRACE("RegOpenKeyEx failed, %s was probably removed.\n", wine_dbgstr_w(KeyPath
));
376 if (RegQueryInfoKeyW(hKey
, 0, 0, 0, &dwCount
, &dwMaxSubKeyLen
, 0, 0, 0, 0, 0, 0) != ERROR_SUCCESS
) {
380 /* Set the number of children again */
381 tvItem
.mask
= TVIF_CHILDREN
;
382 tvItem
.hItem
= hItem
;
383 tvItem
.cChildren
= dwCount
;
384 if (!TreeView_SetItemW(hwndTV
, &tvItem
)) {
388 /* We don't have to bother with the rest if it's not expanded. */
389 if (SendMessageW(hwndTV
, TVM_GETITEMSTATE
, (WPARAM
)hItem
, TVIS_EXPANDED
) == 0) {
394 dwMaxSubKeyLen
++; /* account for the \0 terminator */
395 Name
= heap_xalloc(dwMaxSubKeyLen
* sizeof(WCHAR
));
397 tvItem
.cchTextMax
= dwMaxSubKeyLen
;
398 tvItem
.pszText
= heap_xalloc(dwMaxSubKeyLen
* sizeof(WCHAR
));
400 /* Now go through all the children in the registry, and check if any have to be added. */
401 for (dwIndex
= 0; dwIndex
< dwCount
; dwIndex
++) {
402 DWORD cName
= dwMaxSubKeyLen
, dwSubCount
;
406 if (RegEnumKeyExW(hKey
, dwIndex
, Name
, &cName
, 0, 0, 0, NULL
) != ERROR_SUCCESS
) {
410 /* Find the number of children of the node. */
412 if (RegOpenKeyExW(hKey
, Name
, 0, KEY_QUERY_VALUE
, &hSubKey
) == ERROR_SUCCESS
) {
413 if (RegQueryInfoKeyW(hSubKey
, 0, 0, 0, &dwSubCount
, 0, 0, 0, 0, 0, 0, 0) != ERROR_SUCCESS
) {
416 RegCloseKey(hSubKey
);
419 /* Check if the node is already in there. */
420 for (childItem
= (HTREEITEM
)SendMessageW(hwndTV
, TVM_GETNEXTITEM
, TVGN_CHILD
, (LPARAM
)hItem
); childItem
;
421 childItem
= (HTREEITEM
)SendMessageW(hwndTV
, TVM_GETNEXTITEM
, TVGN_NEXT
, (LPARAM
)childItem
)) {
422 tvItem
.mask
= TVIF_TEXT
;
423 tvItem
.hItem
= childItem
;
424 if (!TreeView_GetItemW(hwndTV
, &tvItem
)) {
426 heap_free(tvItem
.pszText
);
430 if (!lstrcmpiW(tvItem
.pszText
, Name
)) {
436 if (found
== FALSE
) {
437 WINE_TRACE("New subkey %s\n", wine_dbgstr_w(Name
));
438 AddEntryToTree(hwndTV
, hItem
, Name
, NULL
, dwSubCount
);
442 heap_free(tvItem
.pszText
);
445 /* Now go through all the children in the tree, and check if any have to be removed. */
446 childItem
= (HTREEITEM
)SendMessageW(hwndTV
, TVM_GETNEXTITEM
, TVGN_CHILD
, (LPARAM
)hItem
);
448 HTREEITEM nextItem
= (HTREEITEM
)SendMessageW(hwndTV
, TVM_GETNEXTITEM
, TVGN_NEXT
, (LPARAM
)childItem
);
449 if (RefreshTreeItem(hwndTV
, childItem
) == FALSE
) {
450 SendMessageW(hwndTV
, TVM_DELETEITEM
, 0, (LPARAM
)childItem
);
452 childItem
= nextItem
;
458 static void treeview_sort_item(HWND hWnd
, HTREEITEM item
)
460 HTREEITEM child
= (HTREEITEM
)SendMessageW(hWnd
, TVM_GETNEXTITEM
, TVGN_CHILD
, (LPARAM
)item
);
462 while (child
!= NULL
) {
463 treeview_sort_item(hWnd
, child
);
464 child
= (HTREEITEM
)SendMessageW(hWnd
, TVM_GETNEXTITEM
, TVGN_NEXT
, (LPARAM
)child
);
466 SendMessageW(hWnd
, TVM_SORTCHILDREN
, 0, (LPARAM
)item
);
469 BOOL
RefreshTreeView(HWND hwndTV
)
472 HTREEITEM hSelectedItem
;
477 hSelectedItem
= (HTREEITEM
)SendMessageW(hwndTV
, TVM_GETNEXTITEM
, TVGN_CARET
, 0);
478 hcursorOld
= SetCursor(LoadCursorW(NULL
, (LPCWSTR
)IDC_WAIT
));
479 SendMessageW(hwndTV
, WM_SETREDRAW
, FALSE
, 0);
481 hRoot
= (HTREEITEM
)SendMessageW(hwndTV
, TVM_GETNEXTITEM
, TVGN_ROOT
, 0);
482 hItem
= (HTREEITEM
)SendMessageW(hwndTV
, TVM_GETNEXTITEM
, TVGN_CHILD
, (LPARAM
)hRoot
);
484 RefreshTreeItem(hwndTV
, hItem
);
485 treeview_sort_item(hwndTV
, hItem
);
486 hItem
= (HTREEITEM
)SendMessageW(hwndTV
, TVM_GETNEXTITEM
, TVGN_NEXT
, (LPARAM
)hItem
);
489 SendMessageW(hwndTV
, WM_SETREDRAW
, TRUE
, 0);
490 InvalidateRect(hwndTV
, NULL
, FALSE
);
491 SetCursor(hcursorOld
);
493 /* We reselect the currently selected node, this will prompt a refresh of the listview. */
494 SendMessageW(hwndTV
, TVM_SELECTITEM
, TVGN_CARET
, (LPARAM
)hSelectedItem
);
498 HTREEITEM
InsertNode(HWND hwndTV
, HTREEITEM hItem
, LPWSTR name
)
500 WCHAR buf
[MAX_NEW_KEY_LEN
];
501 HTREEITEM hNewItem
= 0;
504 if (!hItem
) hItem
= (HTREEITEM
)SendMessageW(hwndTV
, TVM_GETNEXTITEM
, TVGN_CARET
, 0);
505 if (!hItem
) return FALSE
;
506 if (SendMessageW(hwndTV
, TVM_GETITEMSTATE
, (WPARAM
)hItem
, TVIS_EXPANDEDONCE
) & TVIS_EXPANDEDONCE
) {
507 hNewItem
= AddEntryToTree(hwndTV
, hItem
, name
, 0, 0);
509 item
.mask
= TVIF_CHILDREN
| TVIF_HANDLE
;
511 if (!TreeView_GetItemW(hwndTV
, &item
)) return FALSE
;
513 if (!TreeView_SetItemW(hwndTV
, &item
)) return FALSE
;
515 SendMessageW(hwndTV
, TVM_EXPAND
, TVE_EXPAND
, (LPARAM
)hItem
);
517 for(hNewItem
= (HTREEITEM
)SendMessageW(hwndTV
, TVM_GETNEXTITEM
, TVGN_CHILD
, (LPARAM
)hItem
); hNewItem
;
518 hNewItem
= (HTREEITEM
)SendMessageW(hwndTV
, TVM_GETNEXTITEM
, TVGN_NEXT
, (LPARAM
)hNewItem
)) {
519 item
.mask
= TVIF_HANDLE
| TVIF_TEXT
;
520 item
.hItem
= hNewItem
;
522 item
.cchTextMax
= ARRAY_SIZE(buf
);
523 if (!TreeView_GetItemW(hwndTV
, &item
)) continue;
524 if (lstrcmpW(name
, item
.pszText
) == 0) break;
528 SendMessageW(hwndTV
, TVM_SELECTITEM
, TVGN_CARET
, (LPARAM
)hNewItem
);
533 HWND
StartKeyRename(HWND hwndTV
)
537 if(!(hItem
= (HTREEITEM
)SendMessageW(hwndTV
, TVM_GETNEXTITEM
, TVGN_CARET
, 0))) return 0;
538 SetWindowLongPtrW(hwndTV
, GWLP_USERDATA
, 1);
539 return (HWND
)SendMessageW(hwndTV
, TVM_EDITLABELW
, 0, (LPARAM
)hItem
);
542 static BOOL
InitTreeViewItems(HWND hwndTV
, LPWSTR pHostName
)
544 TVINSERTSTRUCTW tvins
;
546 static WCHAR hkcr
[] = L
"HKEY_CLASSES_ROOT",
547 hkcu
[] = L
"HKEY_CURRENT_USER",
548 hklm
[] = L
"HKEY_LOCAL_MACHINE",
549 hku
[] = L
"HKEY_USERS",
550 hkcc
[] = L
"HKEY_CURRENT_CONFIG",
551 hkdd
[] = L
"HKEY_DYN_DATA";
553 tvins
.u
.item
.mask
= TVIF_TEXT
| TVIF_IMAGE
| TVIF_SELECTEDIMAGE
| TVIF_CHILDREN
| TVIF_PARAM
;
554 /* Set the text of the item. */
555 tvins
.u
.item
.pszText
= pHostName
;
556 tvins
.u
.item
.cchTextMax
= lstrlenW(pHostName
);
557 /* Assume the item is not a parent item, so give it an image. */
558 tvins
.u
.item
.iImage
= Image_Root
;
559 tvins
.u
.item
.iSelectedImage
= Image_Root
;
560 tvins
.u
.item
.cChildren
= 5;
561 /* Save the heading level in the item's application-defined data area. */
562 tvins
.u
.item
.lParam
= 0;
563 tvins
.hInsertAfter
= TVI_FIRST
;
564 tvins
.hParent
= TVI_ROOT
;
565 /* Add the item to the tree view control. */
566 if (!(hRoot
= TreeView_InsertItemW(hwndTV
, &tvins
))) return FALSE
;
568 if (!AddEntryToTree(hwndTV
, hRoot
, hkcr
, HKEY_CLASSES_ROOT
, 1)) return FALSE
;
569 if (!AddEntryToTree(hwndTV
, hRoot
, hkcu
, HKEY_CURRENT_USER
, 1)) return FALSE
;
570 if (!AddEntryToTree(hwndTV
, hRoot
, hklm
, HKEY_LOCAL_MACHINE
, 1)) return FALSE
;
571 if (!AddEntryToTree(hwndTV
, hRoot
, hku
, HKEY_USERS
, 1)) return FALSE
;
572 if (!AddEntryToTree(hwndTV
, hRoot
, hkcc
, HKEY_CURRENT_CONFIG
, 1)) return FALSE
;
573 if (!AddEntryToTree(hwndTV
, hRoot
, hkdd
, HKEY_DYN_DATA
, 1)) return FALSE
;
580 * InitTreeViewImageLists - creates an image list, adds three bitmaps
581 * to it, and associates the image list with a tree view control.
582 * Returns TRUE if successful, or FALSE otherwise.
583 * hwndTV - handle to the tree view control.
585 static BOOL
InitTreeViewImageLists(HWND hwndTV
)
587 HIMAGELIST himl
; /* handle to image list */
588 HICON hico
; /* handle to icon */
589 INT cx
= GetSystemMetrics(SM_CXSMICON
);
590 INT cy
= GetSystemMetrics(SM_CYSMICON
);
592 /* Create the image list. */
593 if ((himl
= ImageList_Create(cx
, cy
, ILC_MASK
, 0, NUM_ICONS
)) == NULL
)
596 /* Add the open file, closed file, and document bitmaps. */
597 hico
= LoadIconW(hInst
, MAKEINTRESOURCEW(IDI_OPEN_FILE
));
598 Image_Open
= ImageList_AddIcon(himl
, hico
);
600 hico
= LoadIconW(hInst
, MAKEINTRESOURCEW(IDI_CLOSED_FILE
));
601 Image_Closed
= ImageList_AddIcon(himl
, hico
);
603 hico
= LoadIconW(hInst
, MAKEINTRESOURCEW(IDI_ROOT
));
604 Image_Root
= ImageList_AddIcon(himl
, hico
);
606 /* Fail if not all of the images were added. */
607 if (ImageList_GetImageCount(himl
) < NUM_ICONS
)
612 /* Associate the image list with the tree view control. */
613 SendMessageW(hwndTV
, TVM_SETIMAGELIST
, TVSIL_NORMAL
, (LPARAM
)himl
);
618 BOOL
UpdateExpandingTree(HWND hwndTV
, HTREEITEM hItem
, int state
)
620 DWORD dwCount
, dwIndex
, dwMaxSubKeyLen
;
621 HKEY hRoot
, hNewKey
, hKey
;
628 static int expanding
;
629 if (expanding
) return FALSE
;
630 if (state
& TVIS_EXPANDEDONCE
) {
634 hcursorOld
= SetCursor(LoadCursorW(NULL
, (LPCWSTR
)IDC_WAIT
));
635 SendMessageW(hwndTV
, WM_SETREDRAW
, FALSE
, 0);
637 keyPath
= GetItemPath(hwndTV
, hItem
, &hRoot
);
638 if (!keyPath
) goto done
;
641 errCode
= RegOpenKeyExW(hRoot
, keyPath
, 0, KEY_READ
, &hNewKey
);
642 if (errCode
!= ERROR_SUCCESS
) goto done
;
647 errCode
= RegQueryInfoKeyW(hNewKey
, 0, 0, 0, &dwCount
, &dwMaxSubKeyLen
, 0, 0, 0, 0, 0, 0);
648 if (errCode
!= ERROR_SUCCESS
) goto done
;
649 dwMaxSubKeyLen
++; /* account for the \0 terminator */
650 Name
= heap_xalloc(dwMaxSubKeyLen
* sizeof(WCHAR
));
652 for (dwIndex
= 0; dwIndex
< dwCount
; dwIndex
++) {
653 DWORD cName
= dwMaxSubKeyLen
, dwSubCount
;
655 errCode
= RegEnumKeyExW(hNewKey
, dwIndex
, Name
, &cName
, 0, 0, 0, 0);
656 if (errCode
!= ERROR_SUCCESS
) continue;
657 errCode
= RegOpenKeyExW(hNewKey
, Name
, 0, KEY_QUERY_VALUE
, &hKey
);
658 if (errCode
== ERROR_SUCCESS
) {
659 errCode
= RegQueryInfoKeyW(hKey
, 0, 0, 0, &dwSubCount
, 0, 0, 0, 0, 0, 0, 0);
662 if (errCode
!= ERROR_SUCCESS
) dwSubCount
= 0;
663 AddEntryToTree(hwndTV
, hItem
, Name
, NULL
, dwSubCount
);
665 RegCloseKey(hNewKey
);
669 item
.mask
= TVIF_STATE
;
671 item
.stateMask
= TVIS_EXPANDEDONCE
;
672 item
.state
= TVIS_EXPANDEDONCE
;
673 SendMessageW(hwndTV
, TVM_SETITEMW
, 0, (LPARAM
)&item
);
674 SendMessageW(hwndTV
, WM_SETREDRAW
, TRUE
, 0);
675 SetCursor(hcursorOld
);
682 BOOL
OnTreeExpanding(HWND hwndTV
, NMTREEVIEWW
* pnmtv
)
684 return UpdateExpandingTree(hwndTV
, pnmtv
->itemNew
.hItem
, pnmtv
->itemNew
.state
);
689 * CreateTreeView - creates a tree view control.
690 * Returns the handle to the new control if successful, or NULL otherwise.
691 * hwndParent - handle to the control's parent window.
693 HWND
CreateTreeView(HWND hwndParent
, LPWSTR pHostName
, UINT id
)
698 /* Get the dimensions of the parent window's client area, and create the tree view control. */
699 GetClientRect(hwndParent
, &rcClient
);
700 hwndTV
= CreateWindowExW(WS_EX_CLIENTEDGE
, WC_TREEVIEWW
, L
"Tree View",
701 WS_VISIBLE
| WS_CHILD
| WS_TABSTOP
| 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
);