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
)
56 item
.mask
= TVIF_PARAM
;
58 if (!TreeView_GetItemW(hwndTV
, &item
)) return FALSE
;
61 /* found root key with valid key value */
62 *phKey
= (HKEY
)item
.lParam
;
66 hParent
= (HTREEITEM
)SendMessageW(hwndTV
, TVM_GETNEXTITEM
, TVGN_PARENT
, (LPARAM
)hItem
);
67 if(!get_item_path(hwndTV
, hParent
, phKey
, pKeyPath
, pPathLen
, pMaxChars
)) return FALSE
;
69 (*pKeyPath
)[*pPathLen
] = '\\';
74 item
.mask
= TVIF_TEXT
;
76 item
.pszText
= *pKeyPath
+ *pPathLen
;
77 item
.cchTextMax
= maxChars
= *pMaxChars
- *pPathLen
;
78 if (!TreeView_GetItemW(hwndTV
, &item
)) return FALSE
;
79 chars
= lstrlenW(item
.pszText
);
80 if (chars
< maxChars
- 1) {
86 *pKeyPath
= heap_xrealloc(*pKeyPath
, *pMaxChars
);
92 LPWSTR
GetItemPath(HWND hwndTV
, HTREEITEM hItem
, HKEY
* phRootKey
)
94 int pathLen
= 0, maxLen
= 1024;
98 hItem
= (HTREEITEM
)SendMessageW(hwndTV
, TVM_GETNEXTITEM
, TVGN_CARET
, 0);
99 if (!hItem
) return NULL
;
102 pathBuffer
= heap_xalloc(maxLen
* sizeof(WCHAR
));
103 if (!pathBuffer
) return NULL
;
105 if (!get_item_path(hwndTV
, hItem
, phRootKey
, &pathBuffer
, &pathLen
, &maxLen
)) return NULL
;
109 static LPWSTR
get_path_component(LPCWSTR
*lplpKeyName
) {
110 LPCWSTR lpPos
= *lplpKeyName
;
111 LPWSTR lpResult
= NULL
;
115 while(*lpPos
&& *lpPos
!= '\\')
117 if (*lpPos
&& lpPos
== *lplpKeyName
)
120 len
= lpPos
+1-(*lplpKeyName
);
121 lpResult
= heap_xalloc(len
* sizeof(WCHAR
));
123 lstrcpynW(lpResult
, *lplpKeyName
, len
);
124 *lplpKeyName
= *lpPos
? lpPos
+1 : NULL
;
128 HTREEITEM
FindPathInTree(HWND hwndTV
, LPCWSTR lpKeyName
) {
130 WCHAR buf
[261]; /* tree view has 260 character limitation on item name */
131 HTREEITEM hRoot
, hItem
, hOldItem
;
135 hRoot
= (HTREEITEM
)SendMessageW(hwndTV
, TVM_GETNEXTITEM
, TVGN_ROOT
, 0);
137 SendMessageW(hwndTV
, TVM_EXPAND
, TVE_EXPAND
, (LPARAM
)hItem
);
138 hItem
= (HTREEITEM
)SendMessageW(hwndTV
, TVM_GETNEXTITEM
, TVGN_CHILD
, (LPARAM
)hItem
);
142 LPWSTR lpItemName
= get_path_component(&lpKeyName
);
146 tvi
.mask
= TVIF_TEXT
| TVIF_HANDLE
;
149 tvi
.cchTextMax
= 260;
150 SendMessageW(hwndTV
, TVM_GETITEMW
, 0, (LPARAM
) &tvi
);
151 if (!lstrcmpiW(tvi
.pszText
, lpItemName
)) {
153 SendMessageW(hwndTV
, TVM_EXPAND
, TVE_EXPAND
, (LPARAM
)hItem
);
156 heap_free(lpItemName
);
160 hItem
= (HTREEITEM
)SendMessageW(hwndTV
, TVM_GETNEXTITEM
, TVGN_CHILD
, (LPARAM
)hItem
);
163 hItem
= (HTREEITEM
)SendMessageW(hwndTV
, TVM_GETNEXTITEM
, TVGN_NEXT
, (LPARAM
)hItem
);
165 heap_free(lpItemName
);
167 return valid_path
? hOldItem
: hRoot
;
170 return valid_path
? hItem
: hRoot
;
174 BOOL
DeleteNode(HWND hwndTV
, HTREEITEM hItem
)
176 if (!hItem
) hItem
= (HTREEITEM
)SendMessageW(hwndTV
, TVM_GETNEXTITEM
, TVGN_CARET
, 0);
177 if (!hItem
) return FALSE
;
178 return (BOOL
)SendMessageW(hwndTV
, TVM_DELETEITEM
, 0, (LPARAM
)hItem
);
181 /* Add an entry to the tree. Only give hKey for root nodes (HKEY_ constants) */
182 static HTREEITEM
AddEntryToTree(HWND hwndTV
, HTREEITEM hParent
, LPWSTR label
, HKEY hKey
, DWORD dwChildren
)
184 TVINSERTSTRUCTW tvins
;
187 if (RegQueryInfoKeyW(hKey
, 0, 0, 0, &dwChildren
, 0, 0, 0, 0, 0, 0, 0) != ERROR_SUCCESS
) {
192 tvins
.u
.item
.mask
= TVIF_TEXT
| TVIF_IMAGE
| TVIF_SELECTEDIMAGE
| TVIF_CHILDREN
| TVIF_PARAM
;
193 tvins
.u
.item
.pszText
= label
;
194 tvins
.u
.item
.cchTextMax
= lstrlenW(label
);
195 tvins
.u
.item
.iImage
= Image_Closed
;
196 tvins
.u
.item
.iSelectedImage
= Image_Open
;
197 tvins
.u
.item
.cChildren
= dwChildren
;
198 tvins
.u
.item
.lParam
= (LPARAM
)hKey
;
199 tvins
.hInsertAfter
= hKey
? TVI_LAST
: TVI_SORT
;
200 tvins
.hParent
= hParent
;
202 return TreeView_InsertItemW(hwndTV
, &tvins
);
205 static BOOL
match_string(LPCWSTR sstring1
, LPCWSTR sstring2
, int mode
)
207 if (mode
& SEARCH_WHOLE
)
208 return !lstrcmpiW(sstring1
, sstring2
);
210 return NULL
!= StrStrIW(sstring1
, sstring2
);
213 static BOOL
match_item(HWND hwndTV
, HTREEITEM hItem
, LPCWSTR sstring
, int mode
, int *row
)
216 WCHAR keyname
[KEY_MAX_LEN
];
217 item
.mask
= TVIF_TEXT
;
219 item
.pszText
= keyname
;
220 item
.cchTextMax
= KEY_MAX_LEN
;
221 if (!TreeView_GetItemW(hwndTV
, &item
)) return FALSE
;
222 if ((mode
& SEARCH_KEYS
) && match_string(keyname
, sstring
, mode
)) {
227 if (mode
& (SEARCH_VALUES
| SEARCH_CONTENT
)) {
229 WCHAR
*valName
, *KeyPath
;
231 DWORD lenName
, lenNameMax
, lenValueMax
;
232 WCHAR
*buffer
= NULL
;
234 KeyPath
= GetItemPath(hwndTV
, hItem
, &hRoot
);
236 if (!KeyPath
|| !hRoot
)
239 if (RegOpenKeyExW(hRoot
, KeyPath
, 0, KEY_READ
, &hKey
) != ERROR_SUCCESS
) {
246 if (ERROR_SUCCESS
!= RegQueryInfoKeyW(hKey
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, &lenNameMax
, &lenValueMax
, NULL
, NULL
))
249 lenName
= ++lenNameMax
;
250 valName
= heap_xalloc(lenName
* sizeof(WCHAR
));
254 /* RegEnumValue won't return empty default value, so fake it when dealing with *row,
255 which corresponds to list view rows, not value ids */
256 if (ERROR_SUCCESS
== RegEnumValueW(hKey
, 0, valName
, &lenName
, NULL
, NULL
, NULL
, NULL
) && *valName
)
262 DWORD lenValue
= 0, type
= 0;
263 lenName
= lenNameMax
;
265 if (ERROR_SUCCESS
!= RegEnumValueW(hKey
,
266 i
, valName
, &lenName
, NULL
, &type
, NULL
, NULL
))
269 if (mode
& SEARCH_VALUES
) {
270 if (match_string(valName
, sstring
, mode
)) {
279 if ((mode
& SEARCH_CONTENT
) && (type
== REG_EXPAND_SZ
|| type
== REG_SZ
)) {
281 buffer
= heap_xalloc(lenValueMax
);
283 lenName
= lenNameMax
;
284 lenValue
= lenValueMax
;
285 if (ERROR_SUCCESS
!= RegEnumValueW(hKey
, i
, valName
, &lenName
, NULL
, &type
, (LPBYTE
)buffer
, &lenValue
))
287 if (match_string(buffer
, sstring
, mode
)) {
305 HTREEITEM
FindNext(HWND hwndTV
, HTREEITEM hItem
, LPCWSTR sstring
, int mode
, int *row
)
307 HTREEITEM hTry
, hLast
;
311 if (match_item(hwndTV
, hLast
, sstring
, mode
& ~SEARCH_KEYS
, row
)) {
317 /* first look in subtree */
318 /* no children? maybe we haven't loaded them yet? */
319 if (!SendMessageW(hwndTV
, TVM_GETNEXTITEM
, TVGN_CHILD
, (LPARAM
)hLast
)) {
320 UINT state
= (UINT
)SendMessageW(hwndTV
, TVM_GETITEMSTATE
, (WPARAM
)hLast
, -1);
321 UpdateExpandingTree(hwndTV
, hLast
, state
);
323 hTry
= (HTREEITEM
)SendMessageW(hwndTV
, TVM_GETNEXTITEM
, TVGN_CHILD
, (LPARAM
)hLast
);
325 if (match_item(hwndTV
, hTry
, sstring
, mode
, row
))
330 /* no more children, maybe there are any siblings? */
331 hTry
= (HTREEITEM
)SendMessageW(hwndTV
, TVM_GETNEXTITEM
, TVGN_NEXT
, (LPARAM
)hLast
);
333 if (match_item(hwndTV
, hTry
, sstring
, mode
, row
))
338 /* no more siblings, look at the next siblings in parent(s) */
339 hLast
= (HTREEITEM
)SendMessageW(hwndTV
, TVM_GETNEXTITEM
, TVGN_PARENT
, (LPARAM
)hLast
);
342 while (hLast
&& (hTry
= (HTREEITEM
)SendMessageW(hwndTV
, TVM_GETNEXTITEM
, TVGN_NEXT
, (LPARAM
)hLast
)) == NULL
) {
343 hLast
= (HTREEITEM
)SendMessageW(hwndTV
, TVM_GETNEXTITEM
, TVGN_PARENT
, (LPARAM
)hLast
);
345 if (match_item(hwndTV
, hTry
, sstring
, mode
, row
))
352 static BOOL
RefreshTreeItem(HWND hwndTV
, HTREEITEM hItem
)
354 HKEY hRoot
, hKey
, hSubKey
;
357 DWORD dwCount
, dwIndex
, dwMaxSubKeyLen
;
362 KeyPath
= GetItemPath(hwndTV
, hItem
, &hRoot
);
364 if (!KeyPath
|| !hRoot
)
368 if (RegOpenKeyExW(hRoot
, KeyPath
, 0, KEY_READ
, &hKey
) != ERROR_SUCCESS
) {
369 WINE_TRACE("RegOpenKeyEx failed, %s was probably removed.\n", wine_dbgstr_w(KeyPath
));
377 if (RegQueryInfoKeyW(hKey
, 0, 0, 0, &dwCount
, &dwMaxSubKeyLen
, 0, 0, 0, 0, 0, 0) != ERROR_SUCCESS
) {
381 /* Set the number of children again */
382 tvItem
.mask
= TVIF_CHILDREN
;
383 tvItem
.hItem
= hItem
;
384 tvItem
.cChildren
= dwCount
;
385 if (!TreeView_SetItemW(hwndTV
, &tvItem
)) {
389 /* We don't have to bother with the rest if it's not expanded. */
390 if (SendMessageW(hwndTV
, TVM_GETITEMSTATE
, (WPARAM
)hItem
, TVIS_EXPANDED
) == 0) {
395 dwMaxSubKeyLen
++; /* account for the \0 terminator */
396 Name
= heap_xalloc(dwMaxSubKeyLen
* sizeof(WCHAR
));
398 tvItem
.cchTextMax
= dwMaxSubKeyLen
;
399 tvItem
.pszText
= heap_xalloc(dwMaxSubKeyLen
* sizeof(WCHAR
));
401 /* Now go through all the children in the registry, and check if any have to be added. */
402 for (dwIndex
= 0; dwIndex
< dwCount
; dwIndex
++) {
403 DWORD cName
= dwMaxSubKeyLen
, dwSubCount
;
407 if (RegEnumKeyExW(hKey
, dwIndex
, Name
, &cName
, 0, 0, 0, NULL
) != ERROR_SUCCESS
) {
411 /* Find the number of children of the node. */
413 if (RegOpenKeyExW(hKey
, Name
, 0, KEY_QUERY_VALUE
, &hSubKey
) == ERROR_SUCCESS
) {
414 if (RegQueryInfoKeyW(hSubKey
, 0, 0, 0, &dwSubCount
, 0, 0, 0, 0, 0, 0, 0) != ERROR_SUCCESS
) {
417 RegCloseKey(hSubKey
);
420 /* Check if the node is already in there. */
421 for (childItem
= (HTREEITEM
)SendMessageW(hwndTV
, TVM_GETNEXTITEM
, TVGN_CHILD
, (LPARAM
)hItem
); childItem
;
422 childItem
= (HTREEITEM
)SendMessageW(hwndTV
, TVM_GETNEXTITEM
, TVGN_NEXT
, (LPARAM
)childItem
)) {
423 tvItem
.mask
= TVIF_TEXT
;
424 tvItem
.hItem
= childItem
;
425 if (!TreeView_GetItemW(hwndTV
, &tvItem
)) {
427 heap_free(tvItem
.pszText
);
431 if (!lstrcmpiW(tvItem
.pszText
, Name
)) {
437 if (found
== FALSE
) {
438 WINE_TRACE("New subkey %s\n", wine_dbgstr_w(Name
));
439 AddEntryToTree(hwndTV
, hItem
, Name
, NULL
, dwSubCount
);
443 heap_free(tvItem
.pszText
);
446 /* Now go through all the children in the tree, and check if any have to be removed. */
447 childItem
= (HTREEITEM
)SendMessageW(hwndTV
, TVM_GETNEXTITEM
, TVGN_CHILD
, (LPARAM
)hItem
);
449 HTREEITEM nextItem
= (HTREEITEM
)SendMessageW(hwndTV
, TVM_GETNEXTITEM
, TVGN_NEXT
, (LPARAM
)childItem
);
450 if (RefreshTreeItem(hwndTV
, childItem
) == FALSE
) {
451 SendMessageW(hwndTV
, TVM_DELETEITEM
, 0, (LPARAM
)childItem
);
453 childItem
= nextItem
;
459 static void treeview_sort_item(HWND hWnd
, HTREEITEM item
)
461 HTREEITEM child
= (HTREEITEM
)SendMessageW(hWnd
, TVM_GETNEXTITEM
, TVGN_CHILD
, (LPARAM
)item
);
463 while (child
!= NULL
) {
464 treeview_sort_item(hWnd
, child
);
465 child
= (HTREEITEM
)SendMessageW(hWnd
, TVM_GETNEXTITEM
, TVGN_NEXT
, (LPARAM
)child
);
467 SendMessageW(hWnd
, TVM_SORTCHILDREN
, 0, (LPARAM
)item
);
470 BOOL
RefreshTreeView(HWND hwndTV
)
473 HTREEITEM hSelectedItem
;
478 hSelectedItem
= (HTREEITEM
)SendMessageW(hwndTV
, TVM_GETNEXTITEM
, TVGN_CARET
, 0);
479 hcursorOld
= SetCursor(LoadCursorW(NULL
, (LPCWSTR
)IDC_WAIT
));
480 SendMessageW(hwndTV
, WM_SETREDRAW
, FALSE
, 0);
482 hRoot
= (HTREEITEM
)SendMessageW(hwndTV
, TVM_GETNEXTITEM
, TVGN_ROOT
, 0);
483 hItem
= (HTREEITEM
)SendMessageW(hwndTV
, TVM_GETNEXTITEM
, TVGN_CHILD
, (LPARAM
)hRoot
);
485 RefreshTreeItem(hwndTV
, hItem
);
486 treeview_sort_item(hwndTV
, hItem
);
487 hItem
= (HTREEITEM
)SendMessageW(hwndTV
, TVM_GETNEXTITEM
, TVGN_NEXT
, (LPARAM
)hItem
);
490 SendMessageW(hwndTV
, WM_SETREDRAW
, TRUE
, 0);
491 InvalidateRect(hwndTV
, NULL
, FALSE
);
492 SetCursor(hcursorOld
);
494 /* We reselect the currently selected node, this will prompt a refresh of the listview. */
495 SendMessageW(hwndTV
, TVM_SELECTITEM
, TVGN_CARET
, (LPARAM
)hSelectedItem
);
499 HTREEITEM
InsertNode(HWND hwndTV
, HTREEITEM hItem
, LPWSTR name
)
501 WCHAR buf
[MAX_NEW_KEY_LEN
];
502 HTREEITEM hNewItem
= 0;
505 if (!hItem
) hItem
= (HTREEITEM
)SendMessageW(hwndTV
, TVM_GETNEXTITEM
, TVGN_CARET
, 0);
506 if (!hItem
) return FALSE
;
507 if (SendMessageW(hwndTV
, TVM_GETITEMSTATE
, (WPARAM
)hItem
, TVIS_EXPANDEDONCE
) & TVIS_EXPANDEDONCE
) {
508 hNewItem
= AddEntryToTree(hwndTV
, hItem
, name
, 0, 0);
510 item
.mask
= TVIF_CHILDREN
| TVIF_HANDLE
;
512 if (!TreeView_GetItemW(hwndTV
, &item
)) return FALSE
;
514 if (!TreeView_SetItemW(hwndTV
, &item
)) return FALSE
;
516 SendMessageW(hwndTV
, TVM_EXPAND
, TVE_EXPAND
, (LPARAM
)hItem
);
518 for(hNewItem
= (HTREEITEM
)SendMessageW(hwndTV
, TVM_GETNEXTITEM
, TVGN_CHILD
, (LPARAM
)hItem
); hNewItem
;
519 hNewItem
= (HTREEITEM
)SendMessageW(hwndTV
, TVM_GETNEXTITEM
, TVGN_NEXT
, (LPARAM
)hNewItem
)) {
520 item
.mask
= TVIF_HANDLE
| TVIF_TEXT
;
521 item
.hItem
= hNewItem
;
523 item
.cchTextMax
= COUNT_OF(buf
);
524 if (!TreeView_GetItemW(hwndTV
, &item
)) continue;
525 if (lstrcmpW(name
, item
.pszText
) == 0) break;
529 SendMessageW(hwndTV
, TVM_SELECTITEM
, TVGN_CARET
, (LPARAM
)hNewItem
);
534 HWND
StartKeyRename(HWND hwndTV
)
538 if(!(hItem
= (HTREEITEM
)SendMessageW(hwndTV
, TVM_GETNEXTITEM
, TVGN_CARET
, 0))) return 0;
539 SetWindowLongPtrW(hwndTV
, GWLP_USERDATA
, 1);
540 return (HWND
)SendMessageW(hwndTV
, TVM_EDITLABELW
, 0, (LPARAM
)hItem
);
543 static BOOL
InitTreeViewItems(HWND hwndTV
, LPWSTR pHostName
)
545 TVINSERTSTRUCTW tvins
;
547 static WCHAR hkcr
[] = {'H','K','E','Y','_','C','L','A','S','S','E','S','_','R','O','O','T',0},
548 hkcu
[] = {'H','K','E','Y','_','C','U','R','R','E','N','T','_','U','S','E','R',0},
549 hklm
[] = {'H','K','E','Y','_','L','O','C','A','L','_','M','A','C','H','I','N','E',0},
550 hku
[] = {'H','K','E','Y','_','U','S','E','R','S',0},
551 hkcc
[] = {'H','K','E','Y','_','C','U','R','R','E','N','T','_','C','O','N','F','I','G',0},
552 hkdd
[] = {'H','K','E','Y','_','D','Y','N','_','D','A','T','A',0};
554 tvins
.u
.item
.mask
= TVIF_TEXT
| TVIF_IMAGE
| TVIF_SELECTEDIMAGE
| TVIF_CHILDREN
| TVIF_PARAM
;
555 /* Set the text of the item. */
556 tvins
.u
.item
.pszText
= pHostName
;
557 tvins
.u
.item
.cchTextMax
= lstrlenW(pHostName
);
558 /* Assume the item is not a parent item, so give it an image. */
559 tvins
.u
.item
.iImage
= Image_Root
;
560 tvins
.u
.item
.iSelectedImage
= Image_Root
;
561 tvins
.u
.item
.cChildren
= 5;
562 /* Save the heading level in the item's application-defined data area. */
563 tvins
.u
.item
.lParam
= 0;
564 tvins
.hInsertAfter
= TVI_FIRST
;
565 tvins
.hParent
= TVI_ROOT
;
566 /* Add the item to the tree view control. */
567 if (!(hRoot
= TreeView_InsertItemW(hwndTV
, &tvins
))) return FALSE
;
569 if (!AddEntryToTree(hwndTV
, hRoot
, hkcr
, HKEY_CLASSES_ROOT
, 1)) return FALSE
;
570 if (!AddEntryToTree(hwndTV
, hRoot
, hkcu
, HKEY_CURRENT_USER
, 1)) return FALSE
;
571 if (!AddEntryToTree(hwndTV
, hRoot
, hklm
, HKEY_LOCAL_MACHINE
, 1)) return FALSE
;
572 if (!AddEntryToTree(hwndTV
, hRoot
, hku
, HKEY_USERS
, 1)) return FALSE
;
573 if (!AddEntryToTree(hwndTV
, hRoot
, hkcc
, HKEY_CURRENT_CONFIG
, 1)) return FALSE
;
574 if (!AddEntryToTree(hwndTV
, hRoot
, hkdd
, HKEY_DYN_DATA
, 1)) return FALSE
;
576 /* expand and select host name */
577 SendMessageW(hwndTV
, TVM_EXPAND
, TVE_EXPAND
, (LPARAM
)hRoot
);
578 SendMessageW(hwndTV
, TVM_SELECTITEM
, TVGN_CARET
, (LPARAM
)hRoot
);
584 * InitTreeViewImageLists - creates an image list, adds three bitmaps
585 * to it, and associates the image list with a tree view control.
586 * Returns TRUE if successful, or FALSE otherwise.
587 * hwndTV - handle to the tree view control.
589 static BOOL
InitTreeViewImageLists(HWND hwndTV
)
591 HIMAGELIST himl
; /* handle to image list */
592 HICON hico
; /* handle to icon */
593 INT cx
= GetSystemMetrics(SM_CXSMICON
);
594 INT cy
= GetSystemMetrics(SM_CYSMICON
);
596 /* Create the image list. */
597 if ((himl
= ImageList_Create(cx
, cy
, ILC_MASK
, 0, NUM_ICONS
)) == NULL
)
600 /* Add the open file, closed file, and document bitmaps. */
601 hico
= LoadIconW(hInst
, MAKEINTRESOURCEW(IDI_OPEN_FILE
));
602 Image_Open
= ImageList_AddIcon(himl
, hico
);
604 hico
= LoadIconW(hInst
, MAKEINTRESOURCEW(IDI_CLOSED_FILE
));
605 Image_Closed
= ImageList_AddIcon(himl
, hico
);
607 hico
= LoadIconW(hInst
, MAKEINTRESOURCEW(IDI_ROOT
));
608 Image_Root
= ImageList_AddIcon(himl
, hico
);
610 /* Fail if not all of the images were added. */
611 if (ImageList_GetImageCount(himl
) < NUM_ICONS
)
616 /* Associate the image list with the tree view control. */
617 SendMessageW(hwndTV
, TVM_SETIMAGELIST
, TVSIL_NORMAL
, (LPARAM
)himl
);
622 BOOL
UpdateExpandingTree(HWND hwndTV
, HTREEITEM hItem
, int state
)
624 DWORD dwCount
, dwIndex
, dwMaxSubKeyLen
;
625 HKEY hRoot
, hNewKey
, hKey
;
632 static int expanding
;
633 if (expanding
) return FALSE
;
634 if (state
& TVIS_EXPANDEDONCE
) {
638 hcursorOld
= SetCursor(LoadCursorW(NULL
, (LPCWSTR
)IDC_WAIT
));
639 SendMessageW(hwndTV
, WM_SETREDRAW
, FALSE
, 0);
641 keyPath
= GetItemPath(hwndTV
, hItem
, &hRoot
);
642 if (!keyPath
) goto done
;
645 errCode
= RegOpenKeyExW(hRoot
, keyPath
, 0, KEY_READ
, &hNewKey
);
646 if (errCode
!= ERROR_SUCCESS
) goto done
;
651 errCode
= RegQueryInfoKeyW(hNewKey
, 0, 0, 0, &dwCount
, &dwMaxSubKeyLen
, 0, 0, 0, 0, 0, 0);
652 if (errCode
!= ERROR_SUCCESS
) goto done
;
653 dwMaxSubKeyLen
++; /* account for the \0 terminator */
654 Name
= heap_xalloc(dwMaxSubKeyLen
* sizeof(WCHAR
));
656 for (dwIndex
= 0; dwIndex
< dwCount
; dwIndex
++) {
657 DWORD cName
= dwMaxSubKeyLen
, dwSubCount
;
659 errCode
= RegEnumKeyExW(hNewKey
, dwIndex
, Name
, &cName
, 0, 0, 0, 0);
660 if (errCode
!= ERROR_SUCCESS
) continue;
661 errCode
= RegOpenKeyExW(hNewKey
, Name
, 0, KEY_QUERY_VALUE
, &hKey
);
662 if (errCode
== ERROR_SUCCESS
) {
663 errCode
= RegQueryInfoKeyW(hKey
, 0, 0, 0, &dwSubCount
, 0, 0, 0, 0, 0, 0, 0);
666 if (errCode
!= ERROR_SUCCESS
) dwSubCount
= 0;
667 AddEntryToTree(hwndTV
, hItem
, Name
, NULL
, dwSubCount
);
669 RegCloseKey(hNewKey
);
673 item
.mask
= TVIF_STATE
;
675 item
.stateMask
= TVIS_EXPANDEDONCE
;
676 item
.state
= TVIS_EXPANDEDONCE
;
677 SendMessageW(hwndTV
, TVM_SETITEMW
, 0, (LPARAM
)&item
);
678 SendMessageW(hwndTV
, WM_SETREDRAW
, TRUE
, 0);
679 SetCursor(hcursorOld
);
686 BOOL
OnTreeExpanding(HWND hwndTV
, NMTREEVIEWW
* pnmtv
)
688 return UpdateExpandingTree(hwndTV
, pnmtv
->itemNew
.hItem
, pnmtv
->itemNew
.state
);
693 * CreateTreeView - creates a tree view control.
694 * Returns the handle to the new control if successful, or NULL otherwise.
695 * hwndParent - handle to the control's parent window.
697 HWND
CreateTreeView(HWND hwndParent
, LPWSTR pHostName
, UINT id
)
701 WCHAR TreeView
[] = {'T','r','e','e',' ','V','i','e','w',0};
703 /* Get the dimensions of the parent window's client area, and create the tree view control. */
704 GetClientRect(hwndParent
, &rcClient
);
705 hwndTV
= CreateWindowExW(WS_EX_CLIENTEDGE
, WC_TREEVIEWW
, TreeView
,
706 WS_VISIBLE
| WS_CHILD
| WS_TABSTOP
| TVS_HASLINES
| TVS_HASBUTTONS
|
707 TVS_LINESATROOT
| TVS_EDITLABELS
| TVS_SHOWSELALWAYS
,
708 0, 0, rcClient
.right
, rcClient
.bottom
,
709 hwndParent
, ULongToHandle(id
), hInst
, NULL
);
710 SendMessageW(hwndTV
, TVM_SETUNICODEFORMAT
, TRUE
, 0);
711 /* Initialize the image list, and add items to the control. */
712 if (!InitTreeViewImageLists(hwndTV
) || !InitTreeViewItems(hwndTV
, pHostName
)) {
713 DestroyWindow(hwndTV
);