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>
33 #include "wine/heap.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(regedit
);
39 /* Global variables and constants */
40 /* Image_Open, Image_Closed, and Image_Root - integer variables for indexes of the images. */
41 /* CX_BITMAP and CY_BITMAP - width and height of an icon. */
42 /* NUM_BITMAPS - number of bitmaps to add to the image list. */
49 static BOOL
UpdateExpandingTree(HWND hwndTV
, HTREEITEM hItem
, int state
);
51 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) {
87 *pKeyPath
= heap_xrealloc(*pKeyPath
, *pMaxChars
);
93 LPWSTR
GetItemPath(HWND hwndTV
, HTREEITEM hItem
, HKEY
* phRootKey
)
95 int pathLen
= 0, maxLen
= 1024;
99 hItem
= (HTREEITEM
)SendMessageW(hwndTV
, TVM_GETNEXTITEM
, TVGN_CARET
, 0);
100 if (!hItem
) return NULL
;
103 pathBuffer
= heap_xalloc(maxLen
* sizeof(WCHAR
));
104 if (!pathBuffer
) return NULL
;
106 if (!get_item_path(hwndTV
, hItem
, phRootKey
, &pathBuffer
, &pathLen
, &maxLen
)) return NULL
;
110 static LPWSTR
get_path_component(LPCWSTR
*lplpKeyName
) {
111 LPCWSTR lpPos
= *lplpKeyName
;
112 LPWSTR lpResult
= NULL
;
116 while(*lpPos
&& *lpPos
!= '\\')
118 if (*lpPos
&& lpPos
== *lplpKeyName
)
121 len
= lpPos
+1-(*lplpKeyName
);
122 lpResult
= heap_xalloc(len
* sizeof(WCHAR
));
124 lstrcpynW(lpResult
, *lplpKeyName
, len
);
125 *lplpKeyName
= *lpPos
? lpPos
+1 : NULL
;
129 HTREEITEM
FindPathInTree(HWND hwndTV
, LPCWSTR lpKeyName
) {
131 WCHAR buf
[261]; /* tree view has 260 character limitation on item name */
132 HTREEITEM hRoot
, hItem
, hOldItem
;
136 hRoot
= (HTREEITEM
)SendMessageW(hwndTV
, TVM_GETNEXTITEM
, TVGN_ROOT
, 0);
138 SendMessageW(hwndTV
, TVM_EXPAND
, TVE_EXPAND
, (LPARAM
)hItem
);
139 hItem
= (HTREEITEM
)SendMessageW(hwndTV
, TVM_GETNEXTITEM
, TVGN_CHILD
, (LPARAM
)hItem
);
143 LPWSTR lpItemName
= get_path_component(&lpKeyName
);
147 tvi
.mask
= TVIF_TEXT
| TVIF_HANDLE
;
150 tvi
.cchTextMax
= 260;
151 SendMessageW(hwndTV
, TVM_GETITEMW
, 0, (LPARAM
) &tvi
);
152 if (!lstrcmpiW(tvi
.pszText
, lpItemName
)) {
154 SendMessageW(hwndTV
, TVM_EXPAND
, TVE_EXPAND
, (LPARAM
)hItem
);
157 heap_free(lpItemName
);
161 hItem
= (HTREEITEM
)SendMessageW(hwndTV
, TVM_GETNEXTITEM
, TVGN_CHILD
, (LPARAM
)hItem
);
164 hItem
= (HTREEITEM
)SendMessageW(hwndTV
, TVM_GETNEXTITEM
, TVGN_NEXT
, (LPARAM
)hItem
);
166 heap_free(lpItemName
);
168 return valid_path
? hOldItem
: hRoot
;
171 return valid_path
? hItem
: hRoot
;
175 BOOL
DeleteNode(HWND hwndTV
, HTREEITEM hItem
)
177 if (!hItem
) hItem
= (HTREEITEM
)SendMessageW(hwndTV
, TVM_GETNEXTITEM
, TVGN_CARET
, 0);
178 if (!hItem
) return FALSE
;
179 return (BOOL
)SendMessageW(hwndTV
, TVM_DELETEITEM
, 0, (LPARAM
)hItem
);
182 /* Add an entry to the tree. Only give hKey for root nodes (HKEY_ constants) */
183 static HTREEITEM
AddEntryToTree(HWND hwndTV
, HTREEITEM hParent
, LPWSTR label
, HKEY hKey
, DWORD dwChildren
)
185 TVINSERTSTRUCTW tvins
;
188 if (RegQueryInfoKeyW(hKey
, 0, 0, 0, &dwChildren
, 0, 0, 0, 0, 0, 0, 0) != ERROR_SUCCESS
) {
193 tvins
.u
.item
.mask
= TVIF_TEXT
| TVIF_IMAGE
| TVIF_SELECTEDIMAGE
| TVIF_CHILDREN
| TVIF_PARAM
;
194 tvins
.u
.item
.pszText
= label
;
195 tvins
.u
.item
.cchTextMax
= lstrlenW(label
);
196 tvins
.u
.item
.iImage
= Image_Closed
;
197 tvins
.u
.item
.iSelectedImage
= Image_Open
;
198 tvins
.u
.item
.cChildren
= dwChildren
;
199 tvins
.u
.item
.lParam
= (LPARAM
)hKey
;
200 tvins
.hInsertAfter
= hKey
? TVI_LAST
: TVI_SORT
;
201 tvins
.hParent
= hParent
;
203 return TreeView_InsertItemW(hwndTV
, &tvins
);
206 static BOOL
match_string(LPCWSTR sstring1
, LPCWSTR sstring2
, int mode
)
208 if (mode
& SEARCH_WHOLE
)
209 return !lstrcmpiW(sstring1
, sstring2
);
211 return NULL
!= StrStrIW(sstring1
, sstring2
);
214 static BOOL
match_item(HWND hwndTV
, HTREEITEM hItem
, LPCWSTR sstring
, int mode
, int *row
)
217 WCHAR keyname
[KEY_MAX_LEN
];
218 item
.mask
= TVIF_TEXT
;
220 item
.pszText
= keyname
;
221 item
.cchTextMax
= KEY_MAX_LEN
;
222 if (!TreeView_GetItemW(hwndTV
, &item
)) return FALSE
;
223 if ((mode
& SEARCH_KEYS
) && match_string(keyname
, sstring
, mode
)) {
228 if (mode
& (SEARCH_VALUES
| SEARCH_CONTENT
)) {
230 WCHAR
*valName
, *KeyPath
;
232 DWORD lenName
, lenNameMax
, lenValueMax
;
233 WCHAR
*buffer
= NULL
;
235 KeyPath
= GetItemPath(hwndTV
, hItem
, &hRoot
);
237 if (!KeyPath
|| !hRoot
)
240 if (RegOpenKeyExW(hRoot
, KeyPath
, 0, KEY_READ
, &hKey
) != ERROR_SUCCESS
) {
247 if (ERROR_SUCCESS
!= RegQueryInfoKeyW(hKey
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, &lenNameMax
, &lenValueMax
, NULL
, NULL
))
250 lenName
= ++lenNameMax
;
251 valName
= heap_xalloc(lenName
* sizeof(WCHAR
));
255 /* RegEnumValue won't return empty default value, so fake it when dealing with *row,
256 which corresponds to list view rows, not value ids */
257 if (ERROR_SUCCESS
== RegEnumValueW(hKey
, 0, valName
, &lenName
, NULL
, NULL
, NULL
, NULL
) && *valName
)
263 DWORD lenValue
= 0, type
= 0;
264 lenName
= lenNameMax
;
266 if (ERROR_SUCCESS
!= RegEnumValueW(hKey
,
267 i
, valName
, &lenName
, NULL
, &type
, NULL
, NULL
))
270 if (mode
& SEARCH_VALUES
) {
271 if (match_string(valName
, sstring
, mode
)) {
280 if ((mode
& SEARCH_CONTENT
) && (type
== REG_EXPAND_SZ
|| type
== REG_SZ
)) {
282 buffer
= heap_xalloc(lenValueMax
);
284 lenName
= lenNameMax
;
285 lenValue
= lenValueMax
;
286 if (ERROR_SUCCESS
!= RegEnumValueW(hKey
, i
, valName
, &lenName
, NULL
, &type
, (LPBYTE
)buffer
, &lenValue
))
288 if (match_string(buffer
, sstring
, mode
)) {
306 HTREEITEM
FindNext(HWND hwndTV
, HTREEITEM hItem
, LPCWSTR sstring
, int mode
, int *row
)
308 HTREEITEM hTry
, hLast
;
312 if (match_item(hwndTV
, hLast
, sstring
, mode
& ~SEARCH_KEYS
, row
)) {
318 /* first look in subtree */
319 /* no children? maybe we haven't loaded them yet? */
320 if (!SendMessageW(hwndTV
, TVM_GETNEXTITEM
, TVGN_CHILD
, (LPARAM
)hLast
)) {
321 UINT state
= (UINT
)SendMessageW(hwndTV
, TVM_GETITEMSTATE
, (WPARAM
)hLast
, -1);
322 UpdateExpandingTree(hwndTV
, hLast
, state
);
324 hTry
= (HTREEITEM
)SendMessageW(hwndTV
, TVM_GETNEXTITEM
, TVGN_CHILD
, (LPARAM
)hLast
);
326 if (match_item(hwndTV
, hTry
, sstring
, mode
, row
))
331 /* no more children, maybe there are any siblings? */
332 hTry
= (HTREEITEM
)SendMessageW(hwndTV
, TVM_GETNEXTITEM
, TVGN_NEXT
, (LPARAM
)hLast
);
334 if (match_item(hwndTV
, hTry
, sstring
, mode
, row
))
339 /* no more siblings, look at the next siblings in parent(s) */
340 hLast
= (HTREEITEM
)SendMessageW(hwndTV
, TVM_GETNEXTITEM
, TVGN_PARENT
, (LPARAM
)hLast
);
343 while (hLast
&& (hTry
= (HTREEITEM
)SendMessageW(hwndTV
, TVM_GETNEXTITEM
, TVGN_NEXT
, (LPARAM
)hLast
)) == NULL
) {
344 hLast
= (HTREEITEM
)SendMessageW(hwndTV
, TVM_GETNEXTITEM
, TVGN_PARENT
, (LPARAM
)hLast
);
346 if (match_item(hwndTV
, hTry
, sstring
, mode
, row
))
353 static BOOL
RefreshTreeItem(HWND hwndTV
, HTREEITEM hItem
)
355 HKEY hRoot
, hKey
, hSubKey
;
358 DWORD dwCount
, dwIndex
, dwMaxSubKeyLen
;
363 KeyPath
= GetItemPath(hwndTV
, hItem
, &hRoot
);
365 if (!KeyPath
|| !hRoot
)
369 if (RegOpenKeyExW(hRoot
, KeyPath
, 0, KEY_READ
, &hKey
) != ERROR_SUCCESS
) {
370 WINE_TRACE("RegOpenKeyEx failed, %s was probably removed.\n", wine_dbgstr_w(KeyPath
));
378 if (RegQueryInfoKeyW(hKey
, 0, 0, 0, &dwCount
, &dwMaxSubKeyLen
, 0, 0, 0, 0, 0, 0) != ERROR_SUCCESS
) {
382 /* Set the number of children again */
383 tvItem
.mask
= TVIF_CHILDREN
;
384 tvItem
.hItem
= hItem
;
385 tvItem
.cChildren
= dwCount
;
386 if (!TreeView_SetItemW(hwndTV
, &tvItem
)) {
390 /* We don't have to bother with the rest if it's not expanded. */
391 if (SendMessageW(hwndTV
, TVM_GETITEMSTATE
, (WPARAM
)hItem
, TVIS_EXPANDED
) == 0) {
396 dwMaxSubKeyLen
++; /* account for the \0 terminator */
397 Name
= heap_xalloc(dwMaxSubKeyLen
* sizeof(WCHAR
));
399 tvItem
.cchTextMax
= dwMaxSubKeyLen
;
400 tvItem
.pszText
= heap_xalloc(dwMaxSubKeyLen
* sizeof(WCHAR
));
402 /* Now go through all the children in the registry, and check if any have to be added. */
403 for (dwIndex
= 0; dwIndex
< dwCount
; dwIndex
++) {
404 DWORD cName
= dwMaxSubKeyLen
, dwSubCount
;
408 if (RegEnumKeyExW(hKey
, dwIndex
, Name
, &cName
, 0, 0, 0, NULL
) != ERROR_SUCCESS
) {
412 /* Find the number of children of the node. */
414 if (RegOpenKeyExW(hKey
, Name
, 0, KEY_QUERY_VALUE
, &hSubKey
) == ERROR_SUCCESS
) {
415 if (RegQueryInfoKeyW(hSubKey
, 0, 0, 0, &dwSubCount
, 0, 0, 0, 0, 0, 0, 0) != ERROR_SUCCESS
) {
418 RegCloseKey(hSubKey
);
421 /* Check if the node is already in there. */
422 for (childItem
= (HTREEITEM
)SendMessageW(hwndTV
, TVM_GETNEXTITEM
, TVGN_CHILD
, (LPARAM
)hItem
); childItem
;
423 childItem
= (HTREEITEM
)SendMessageW(hwndTV
, TVM_GETNEXTITEM
, TVGN_NEXT
, (LPARAM
)childItem
)) {
424 tvItem
.mask
= TVIF_TEXT
;
425 tvItem
.hItem
= childItem
;
426 if (!TreeView_GetItemW(hwndTV
, &tvItem
)) {
428 heap_free(tvItem
.pszText
);
432 if (!lstrcmpiW(tvItem
.pszText
, Name
)) {
438 if (found
== FALSE
) {
439 WINE_TRACE("New subkey %s\n", wine_dbgstr_w(Name
));
440 AddEntryToTree(hwndTV
, hItem
, Name
, NULL
, dwSubCount
);
444 heap_free(tvItem
.pszText
);
447 /* Now go through all the children in the tree, and check if any have to be removed. */
448 childItem
= (HTREEITEM
)SendMessageW(hwndTV
, TVM_GETNEXTITEM
, TVGN_CHILD
, (LPARAM
)hItem
);
450 HTREEITEM nextItem
= (HTREEITEM
)SendMessageW(hwndTV
, TVM_GETNEXTITEM
, TVGN_NEXT
, (LPARAM
)childItem
);
451 if (RefreshTreeItem(hwndTV
, childItem
) == FALSE
) {
452 SendMessageW(hwndTV
, TVM_DELETEITEM
, 0, (LPARAM
)childItem
);
454 childItem
= nextItem
;
460 static void treeview_sort_item(HWND hWnd
, HTREEITEM item
)
462 HTREEITEM child
= (HTREEITEM
)SendMessageW(hWnd
, TVM_GETNEXTITEM
, TVGN_CHILD
, (LPARAM
)item
);
464 while (child
!= NULL
) {
465 treeview_sort_item(hWnd
, child
);
466 child
= (HTREEITEM
)SendMessageW(hWnd
, TVM_GETNEXTITEM
, TVGN_NEXT
, (LPARAM
)child
);
468 SendMessageW(hWnd
, TVM_SORTCHILDREN
, 0, (LPARAM
)item
);
471 BOOL
RefreshTreeView(HWND hwndTV
)
474 HTREEITEM hSelectedItem
;
479 hSelectedItem
= (HTREEITEM
)SendMessageW(hwndTV
, TVM_GETNEXTITEM
, TVGN_CARET
, 0);
480 hcursorOld
= SetCursor(LoadCursorW(NULL
, (LPCWSTR
)IDC_WAIT
));
481 SendMessageW(hwndTV
, WM_SETREDRAW
, FALSE
, 0);
483 hRoot
= (HTREEITEM
)SendMessageW(hwndTV
, TVM_GETNEXTITEM
, TVGN_ROOT
, 0);
484 hItem
= (HTREEITEM
)SendMessageW(hwndTV
, TVM_GETNEXTITEM
, TVGN_CHILD
, (LPARAM
)hRoot
);
486 RefreshTreeItem(hwndTV
, hItem
);
487 treeview_sort_item(hwndTV
, hItem
);
488 hItem
= (HTREEITEM
)SendMessageW(hwndTV
, TVM_GETNEXTITEM
, TVGN_NEXT
, (LPARAM
)hItem
);
491 SendMessageW(hwndTV
, WM_SETREDRAW
, TRUE
, 0);
492 InvalidateRect(hwndTV
, NULL
, FALSE
);
493 SetCursor(hcursorOld
);
495 /* We reselect the currently selected node, this will prompt a refresh of the listview. */
496 SendMessageW(hwndTV
, TVM_SELECTITEM
, TVGN_CARET
, (LPARAM
)hSelectedItem
);
500 HTREEITEM
InsertNode(HWND hwndTV
, HTREEITEM hItem
, LPWSTR name
)
502 WCHAR buf
[MAX_NEW_KEY_LEN
];
503 HTREEITEM hNewItem
= 0;
506 if (!hItem
) hItem
= (HTREEITEM
)SendMessageW(hwndTV
, TVM_GETNEXTITEM
, TVGN_CARET
, 0);
507 if (!hItem
) return FALSE
;
508 if (SendMessageW(hwndTV
, TVM_GETITEMSTATE
, (WPARAM
)hItem
, TVIS_EXPANDEDONCE
) & TVIS_EXPANDEDONCE
) {
509 hNewItem
= AddEntryToTree(hwndTV
, hItem
, name
, 0, 0);
511 item
.mask
= TVIF_CHILDREN
| TVIF_HANDLE
;
513 if (!TreeView_GetItemW(hwndTV
, &item
)) return FALSE
;
515 if (!TreeView_SetItemW(hwndTV
, &item
)) return FALSE
;
517 SendMessageW(hwndTV
, TVM_EXPAND
, TVE_EXPAND
, (LPARAM
)hItem
);
519 for(hNewItem
= (HTREEITEM
)SendMessageW(hwndTV
, TVM_GETNEXTITEM
, TVGN_CHILD
, (LPARAM
)hItem
); hNewItem
;
520 hNewItem
= (HTREEITEM
)SendMessageW(hwndTV
, TVM_GETNEXTITEM
, TVGN_NEXT
, (LPARAM
)hNewItem
)) {
521 item
.mask
= TVIF_HANDLE
| TVIF_TEXT
;
522 item
.hItem
= hNewItem
;
524 item
.cchTextMax
= COUNT_OF(buf
);
525 if (!TreeView_GetItemW(hwndTV
, &item
)) continue;
526 if (lstrcmpW(name
, item
.pszText
) == 0) break;
530 SendMessageW(hwndTV
, TVM_SELECTITEM
, TVGN_CARET
, (LPARAM
)hNewItem
);
535 HWND
StartKeyRename(HWND hwndTV
)
539 if(!(hItem
= (HTREEITEM
)SendMessageW(hwndTV
, TVM_GETNEXTITEM
, TVGN_CARET
, 0))) return 0;
540 SetWindowLongPtrW(hwndTV
, GWLP_USERDATA
, 1);
541 return (HWND
)SendMessageW(hwndTV
, TVM_EDITLABELW
, 0, (LPARAM
)hItem
);
544 static BOOL
InitTreeViewItems(HWND hwndTV
, LPWSTR pHostName
)
546 TVINSERTSTRUCTW tvins
;
548 static WCHAR hkcr
[] = {'H','K','E','Y','_','C','L','A','S','S','E','S','_','R','O','O','T',0},
549 hkcu
[] = {'H','K','E','Y','_','C','U','R','R','E','N','T','_','U','S','E','R',0},
550 hklm
[] = {'H','K','E','Y','_','L','O','C','A','L','_','M','A','C','H','I','N','E',0},
551 hku
[] = {'H','K','E','Y','_','U','S','E','R','S',0},
552 hkcc
[] = {'H','K','E','Y','_','C','U','R','R','E','N','T','_','C','O','N','F','I','G',0},
553 hkdd
[] = {'H','K','E','Y','_','D','Y','N','_','D','A','T','A',0};
555 tvins
.u
.item
.mask
= TVIF_TEXT
| TVIF_IMAGE
| TVIF_SELECTEDIMAGE
| TVIF_CHILDREN
| TVIF_PARAM
;
556 /* Set the text of the item. */
557 tvins
.u
.item
.pszText
= pHostName
;
558 tvins
.u
.item
.cchTextMax
= lstrlenW(pHostName
);
559 /* Assume the item is not a parent item, so give it an image. */
560 tvins
.u
.item
.iImage
= Image_Root
;
561 tvins
.u
.item
.iSelectedImage
= Image_Root
;
562 tvins
.u
.item
.cChildren
= 5;
563 /* Save the heading level in the item's application-defined data area. */
564 tvins
.u
.item
.lParam
= 0;
565 tvins
.hInsertAfter
= TVI_FIRST
;
566 tvins
.hParent
= TVI_ROOT
;
567 /* Add the item to the tree view control. */
568 if (!(hRoot
= TreeView_InsertItemW(hwndTV
, &tvins
))) return FALSE
;
570 if (!AddEntryToTree(hwndTV
, hRoot
, hkcr
, HKEY_CLASSES_ROOT
, 1)) return FALSE
;
571 if (!AddEntryToTree(hwndTV
, hRoot
, hkcu
, HKEY_CURRENT_USER
, 1)) return FALSE
;
572 if (!AddEntryToTree(hwndTV
, hRoot
, hklm
, HKEY_LOCAL_MACHINE
, 1)) return FALSE
;
573 if (!AddEntryToTree(hwndTV
, hRoot
, hku
, HKEY_USERS
, 1)) return FALSE
;
574 if (!AddEntryToTree(hwndTV
, hRoot
, hkcc
, HKEY_CURRENT_CONFIG
, 1)) return FALSE
;
575 if (!AddEntryToTree(hwndTV
, hRoot
, hkdd
, HKEY_DYN_DATA
, 1)) return FALSE
;
577 /* expand and select host name */
578 SendMessageW(hwndTV
, TVM_EXPAND
, TVE_EXPAND
, (LPARAM
)hRoot
);
579 SendMessageW(hwndTV
, TVM_SELECTITEM
, TVGN_CARET
, (LPARAM
)hRoot
);
585 * InitTreeViewImageLists - creates an image list, adds three bitmaps
586 * to it, and associates the image list with a tree view control.
587 * Returns TRUE if successful, or FALSE otherwise.
588 * hwndTV - handle to the tree view control.
590 static BOOL
InitTreeViewImageLists(HWND hwndTV
)
592 HIMAGELIST himl
; /* handle to image list */
593 HICON hico
; /* handle to icon */
594 INT cx
= GetSystemMetrics(SM_CXSMICON
);
595 INT cy
= GetSystemMetrics(SM_CYSMICON
);
597 /* Create the image list. */
598 if ((himl
= ImageList_Create(cx
, cy
, ILC_MASK
, 0, NUM_ICONS
)) == NULL
)
601 /* Add the open file, closed file, and document bitmaps. */
602 hico
= LoadIconW(hInst
, MAKEINTRESOURCEW(IDI_OPEN_FILE
));
603 Image_Open
= ImageList_AddIcon(himl
, hico
);
605 hico
= LoadIconW(hInst
, MAKEINTRESOURCEW(IDI_CLOSED_FILE
));
606 Image_Closed
= ImageList_AddIcon(himl
, hico
);
608 hico
= LoadIconW(hInst
, MAKEINTRESOURCEW(IDI_ROOT
));
609 Image_Root
= ImageList_AddIcon(himl
, hico
);
611 /* Fail if not all of the images were added. */
612 if (ImageList_GetImageCount(himl
) < NUM_ICONS
)
617 /* Associate the image list with the tree view control. */
618 SendMessageW(hwndTV
, TVM_SETIMAGELIST
, TVSIL_NORMAL
, (LPARAM
)himl
);
623 BOOL
UpdateExpandingTree(HWND hwndTV
, HTREEITEM hItem
, int state
)
625 DWORD dwCount
, dwIndex
, dwMaxSubKeyLen
;
626 HKEY hRoot
, hNewKey
, hKey
;
633 static int expanding
;
634 if (expanding
) return FALSE
;
635 if (state
& TVIS_EXPANDEDONCE
) {
639 hcursorOld
= SetCursor(LoadCursorW(NULL
, (LPCWSTR
)IDC_WAIT
));
640 SendMessageW(hwndTV
, WM_SETREDRAW
, FALSE
, 0);
642 keyPath
= GetItemPath(hwndTV
, hItem
, &hRoot
);
643 if (!keyPath
) goto done
;
646 errCode
= RegOpenKeyExW(hRoot
, keyPath
, 0, KEY_READ
, &hNewKey
);
647 if (errCode
!= ERROR_SUCCESS
) goto done
;
652 errCode
= RegQueryInfoKeyW(hNewKey
, 0, 0, 0, &dwCount
, &dwMaxSubKeyLen
, 0, 0, 0, 0, 0, 0);
653 if (errCode
!= ERROR_SUCCESS
) goto done
;
654 dwMaxSubKeyLen
++; /* account for the \0 terminator */
655 Name
= heap_xalloc(dwMaxSubKeyLen
* sizeof(WCHAR
));
657 for (dwIndex
= 0; dwIndex
< dwCount
; dwIndex
++) {
658 DWORD cName
= dwMaxSubKeyLen
, dwSubCount
;
660 errCode
= RegEnumKeyExW(hNewKey
, dwIndex
, Name
, &cName
, 0, 0, 0, 0);
661 if (errCode
!= ERROR_SUCCESS
) continue;
662 errCode
= RegOpenKeyExW(hNewKey
, Name
, 0, KEY_QUERY_VALUE
, &hKey
);
663 if (errCode
== ERROR_SUCCESS
) {
664 errCode
= RegQueryInfoKeyW(hKey
, 0, 0, 0, &dwSubCount
, 0, 0, 0, 0, 0, 0, 0);
667 if (errCode
!= ERROR_SUCCESS
) dwSubCount
= 0;
668 AddEntryToTree(hwndTV
, hItem
, Name
, NULL
, dwSubCount
);
670 RegCloseKey(hNewKey
);
674 item
.mask
= TVIF_STATE
;
676 item
.stateMask
= TVIS_EXPANDEDONCE
;
677 item
.state
= TVIS_EXPANDEDONCE
;
678 SendMessageW(hwndTV
, TVM_SETITEMW
, 0, (LPARAM
)&item
);
679 SendMessageW(hwndTV
, WM_SETREDRAW
, TRUE
, 0);
680 SetCursor(hcursorOld
);
687 BOOL
OnTreeExpanding(HWND hwndTV
, NMTREEVIEWW
* pnmtv
)
689 return UpdateExpandingTree(hwndTV
, pnmtv
->itemNew
.hItem
, pnmtv
->itemNew
.state
);
694 * CreateTreeView - creates a tree view control.
695 * Returns the handle to the new control if successful, or NULL otherwise.
696 * hwndParent - handle to the control's parent window.
698 HWND
CreateTreeView(HWND hwndParent
, LPWSTR pHostName
, UINT id
)
702 WCHAR TreeView
[] = {'T','r','e','e',' ','V','i','e','w',0};
704 /* Get the dimensions of the parent window's client area, and create the tree view control. */
705 GetClientRect(hwndParent
, &rcClient
);
706 hwndTV
= CreateWindowExW(WS_EX_CLIENTEDGE
, WC_TREEVIEWW
, TreeView
,
707 WS_VISIBLE
| WS_CHILD
| WS_TABSTOP
| TVS_HASLINES
| TVS_HASBUTTONS
|
708 TVS_LINESATROOT
| TVS_EDITLABELS
| TVS_SHOWSELALWAYS
,
709 0, 0, rcClient
.right
, rcClient
.bottom
,
710 hwndParent
, ULongToHandle(id
), hInst
, NULL
);
711 SendMessageW(hwndTV
, TVM_SETUNICODEFORMAT
, TRUE
, 0);
712 /* Initialize the image list, and add items to the control. */
713 if (!InitTreeViewImageLists(hwndTV
) || !InitTreeViewItems(hwndTV
, pHostName
)) {
714 DestroyWindow(hwndTV
);