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. */
50 static BOOL
UpdateExpandingTree(HWND hwndTV
, HTREEITEM hItem
, int state
);
52 static BOOL
get_item_path(HWND hwndTV
, HTREEITEM hItem
, HKEY
* phKey
, LPWSTR
* pKeyPath
, int* pPathLen
, int* pMaxChars
)
59 item
.mask
= TVIF_PARAM
;
61 if (!TreeView_GetItemW(hwndTV
, &item
)) return FALSE
;
64 /* found root key with valid key value */
65 *phKey
= (HKEY
)item
.lParam
;
69 hParent
= (HTREEITEM
)SendMessageW(hwndTV
, TVM_GETNEXTITEM
, TVGN_PARENT
, (LPARAM
)hItem
);
70 if(!get_item_path(hwndTV
, hParent
, phKey
, pKeyPath
, pPathLen
, pMaxChars
)) return FALSE
;
72 (*pKeyPath
)[*pPathLen
] = '\\';
77 item
.mask
= TVIF_TEXT
;
79 item
.pszText
= *pKeyPath
+ *pPathLen
;
80 item
.cchTextMax
= maxChars
= *pMaxChars
- *pPathLen
;
81 if (!TreeView_GetItemW(hwndTV
, &item
)) return FALSE
;
82 chars
= lstrlenW(item
.pszText
);
83 if (chars
< maxChars
- 1) {
87 newStr
= HeapReAlloc(GetProcessHeap(), 0, *pKeyPath
, *pMaxChars
* 2);
88 if (!newStr
) return FALSE
;
96 LPWSTR
GetItemPath(HWND hwndTV
, HTREEITEM hItem
, HKEY
* phRootKey
)
98 int pathLen
= 0, maxLen
= 1024;
102 hItem
= (HTREEITEM
)SendMessageW(hwndTV
, TVM_GETNEXTITEM
, TVGN_CARET
, 0);
103 if (!hItem
) return NULL
;
106 pathBuffer
= HeapAlloc(GetProcessHeap(), 0, maxLen
*sizeof(WCHAR
));
107 if (!pathBuffer
) return NULL
;
109 if (!get_item_path(hwndTV
, hItem
, phRootKey
, &pathBuffer
, &pathLen
, &maxLen
)) return NULL
;
113 static LPWSTR
get_path_component(LPCWSTR
*lplpKeyName
) {
114 LPCWSTR lpPos
= *lplpKeyName
;
115 LPWSTR lpResult
= NULL
;
119 while(*lpPos
&& *lpPos
!= '\\')
121 if (*lpPos
&& lpPos
== *lplpKeyName
)
123 len
= lpPos
+1-(*lplpKeyName
);
124 lpResult
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
125 if (!lpResult
) /* that would be very odd */
127 lstrcpynW(lpResult
, *lplpKeyName
, len
);
128 *lplpKeyName
= *lpPos
? lpPos
+1 : NULL
;
132 HTREEITEM
FindPathInTree(HWND hwndTV
, LPCWSTR lpKeyName
) {
134 WCHAR buf
[261]; /* tree view has 260 character limitation on item name */
135 HTREEITEM hItem
, hOldItem
;
138 hItem
= (HTREEITEM
)SendMessageW(hwndTV
, TVM_GETNEXTITEM
, TVGN_ROOT
, 0);
139 SendMessageW(hwndTV
, TVM_EXPAND
, TVE_EXPAND
, (LPARAM
)hItem
);
140 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
)) {
153 SendMessageW(hwndTV
, TVM_EXPAND
, TVE_EXPAND
, (LPARAM
)hItem
);
156 HeapFree(GetProcessHeap(), 0, lpItemName
);
160 hItem
= (HTREEITEM
)SendMessageW(hwndTV
, TVM_GETNEXTITEM
, TVGN_CHILD
, (LPARAM
)hItem
);
163 hItem
= (HTREEITEM
)SendMessageW(hwndTV
, TVM_GETNEXTITEM
, TVGN_NEXT
, (LPARAM
)hItem
);
165 HeapFree(GetProcessHeap(), 0, lpItemName
);
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
) {
240 HeapFree(GetProcessHeap(), 0, KeyPath
);
244 HeapFree(GetProcessHeap(), 0, KeyPath
);
246 if (ERROR_SUCCESS
!= RegQueryInfoKeyW(hKey
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, &lenNameMax
, &lenValueMax
, NULL
, NULL
))
249 lenName
= ++lenNameMax
;
250 if (!(valName
= HeapAlloc(GetProcessHeap(), 0, lenName
* sizeof(valName
[0]) )))
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
)) {
271 HeapFree(GetProcessHeap(), 0, valName
);
272 HeapFree(GetProcessHeap(), 0, buffer
);
279 if ((mode
& SEARCH_CONTENT
) && (type
== REG_EXPAND_SZ
|| type
== REG_SZ
)) {
281 buffer
= HeapAlloc(GetProcessHeap(), 0, 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
)) {
289 HeapFree(GetProcessHeap(), 0, valName
);
290 HeapFree(GetProcessHeap(), 0, buffer
);
299 HeapFree(GetProcessHeap(), 0, valName
);
300 HeapFree(GetProcessHeap(), 0, buffer
);
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
));
376 HeapFree(GetProcessHeap(), 0, 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 if (!(Name
= HeapAlloc(GetProcessHeap(), 0, dwMaxSubKeyLen
* sizeof(WCHAR
)))) {
400 tvItem
.cchTextMax
= dwMaxSubKeyLen
;
401 if (!(tvItem
.pszText
= HeapAlloc(GetProcessHeap(), 0, dwMaxSubKeyLen
* sizeof(WCHAR
)))) {
402 HeapFree(GetProcessHeap(), 0, Name
);
406 /* Now go through all the children in the registry, and check if any have to be added. */
407 for (dwIndex
= 0; dwIndex
< dwCount
; dwIndex
++) {
408 DWORD cName
= dwMaxSubKeyLen
, dwSubCount
;
412 if (RegEnumKeyExW(hKey
, dwIndex
, Name
, &cName
, 0, 0, 0, NULL
) != ERROR_SUCCESS
) {
416 /* Find the number of children of the node. */
418 if (RegOpenKeyExW(hKey
, Name
, 0, KEY_QUERY_VALUE
, &hSubKey
) == ERROR_SUCCESS
) {
419 if (RegQueryInfoKeyW(hSubKey
, 0, 0, 0, &dwSubCount
, 0, 0, 0, 0, 0, 0, 0) != ERROR_SUCCESS
) {
422 RegCloseKey(hSubKey
);
425 /* Check if the node is already in there. */
426 for (childItem
= (HTREEITEM
)SendMessageW(hwndTV
, TVM_GETNEXTITEM
, TVGN_CHILD
, (LPARAM
)hItem
); childItem
;
427 childItem
= (HTREEITEM
)SendMessageW(hwndTV
, TVM_GETNEXTITEM
, TVGN_NEXT
, (LPARAM
)childItem
)) {
428 tvItem
.mask
= TVIF_TEXT
;
429 tvItem
.hItem
= childItem
;
430 if (!TreeView_GetItemW(hwndTV
, &tvItem
)) {
431 HeapFree(GetProcessHeap(), 0, Name
);
432 HeapFree(GetProcessHeap(), 0, tvItem
.pszText
);
436 if (!lstrcmpiW(tvItem
.pszText
, Name
)) {
442 if (found
== FALSE
) {
443 WINE_TRACE("New subkey %s\n", wine_dbgstr_w(Name
));
444 AddEntryToTree(hwndTV
, hItem
, Name
, NULL
, dwSubCount
);
447 HeapFree(GetProcessHeap(), 0, Name
);
448 HeapFree(GetProcessHeap(), 0, tvItem
.pszText
);
451 /* Now go through all the children in the tree, and check if any have to be removed. */
452 childItem
= (HTREEITEM
)SendMessageW(hwndTV
, TVM_GETNEXTITEM
, TVGN_CHILD
, (LPARAM
)hItem
);
454 HTREEITEM nextItem
= (HTREEITEM
)SendMessageW(hwndTV
, TVM_GETNEXTITEM
, TVGN_NEXT
, (LPARAM
)childItem
);
455 if (RefreshTreeItem(hwndTV
, childItem
) == FALSE
) {
456 SendMessageW(hwndTV
, TVM_DELETEITEM
, 0, (LPARAM
)childItem
);
458 childItem
= nextItem
;
464 BOOL
RefreshTreeView(HWND hwndTV
)
467 HTREEITEM hSelectedItem
;
472 hSelectedItem
= (HTREEITEM
)SendMessageW(hwndTV
, TVM_GETNEXTITEM
, TVGN_CARET
, 0);
473 hcursorOld
= SetCursor(LoadCursorW(NULL
, (LPCWSTR
)IDC_WAIT
));
474 SendMessageW(hwndTV
, WM_SETREDRAW
, FALSE
, 0);
476 hRoot
= (HTREEITEM
)SendMessageW(hwndTV
, TVM_GETNEXTITEM
, TVGN_ROOT
, 0);
477 hItem
= (HTREEITEM
)SendMessageW(hwndTV
, TVM_GETNEXTITEM
, TVGN_CHILD
, (LPARAM
)hRoot
);
479 RefreshTreeItem(hwndTV
, hItem
);
480 hItem
= (HTREEITEM
)SendMessageW(hwndTV
, TVM_GETNEXTITEM
, TVGN_NEXT
, (LPARAM
)hItem
);
483 SendMessageW(hwndTV
, WM_SETREDRAW
, TRUE
, 0);
484 InvalidateRect(hwndTV
, NULL
, FALSE
);
485 SetCursor(hcursorOld
);
487 /* We reselect the currently selected node, this will prompt a refresh of the listview. */
488 SendMessageW(hwndTV
, TVM_SELECTITEM
, TVGN_CARET
, (LPARAM
)hSelectedItem
);
492 HTREEITEM
InsertNode(HWND hwndTV
, HTREEITEM hItem
, LPWSTR name
)
494 WCHAR buf
[MAX_NEW_KEY_LEN
];
495 HTREEITEM hNewItem
= 0;
498 if (!hItem
) hItem
= (HTREEITEM
)SendMessageW(hwndTV
, TVM_GETNEXTITEM
, TVGN_CARET
, 0);
499 if (!hItem
) return FALSE
;
500 if (SendMessageW(hwndTV
, TVM_GETITEMSTATE
, (WPARAM
)hItem
, TVIS_EXPANDEDONCE
) & TVIS_EXPANDEDONCE
) {
501 hNewItem
= AddEntryToTree(hwndTV
, hItem
, name
, 0, 0);
503 item
.mask
= TVIF_CHILDREN
| TVIF_HANDLE
;
505 if (!TreeView_GetItemW(hwndTV
, &item
)) return FALSE
;
507 if (!TreeView_SetItemW(hwndTV
, &item
)) return FALSE
;
509 SendMessageW(hwndTV
, TVM_EXPAND
, TVE_EXPAND
, (LPARAM
)hItem
);
511 for(hNewItem
= (HTREEITEM
)SendMessageW(hwndTV
, TVM_GETNEXTITEM
, TVGN_CHILD
, (LPARAM
)hItem
); hNewItem
;
512 hNewItem
= (HTREEITEM
)SendMessageW(hwndTV
, TVM_GETNEXTITEM
, TVGN_NEXT
, (LPARAM
)hNewItem
)) {
513 item
.mask
= TVIF_HANDLE
| TVIF_TEXT
;
514 item
.hItem
= hNewItem
;
516 item
.cchTextMax
= COUNT_OF(buf
);
517 if (!TreeView_GetItemW(hwndTV
, &item
)) continue;
518 if (lstrcmpW(name
, item
.pszText
) == 0) break;
522 SendMessageW(hwndTV
, TVM_SELECTITEM
, TVGN_CARET
, (LPARAM
)hNewItem
);
527 HWND
StartKeyRename(HWND hwndTV
)
531 if(!(hItem
= (HTREEITEM
)SendMessageW(hwndTV
, TVM_GETNEXTITEM
, TVGN_CARET
, 0))) return 0;
532 return (HWND
)SendMessageW(hwndTV
, TVM_EDITLABELW
, 0, (LPARAM
)hItem
);
535 static BOOL
InitTreeViewItems(HWND hwndTV
, LPWSTR pHostName
)
537 TVINSERTSTRUCTW tvins
;
539 static WCHAR hkcr
[] = {'H','K','E','Y','_','C','L','A','S','S','E','S','_','R','O','O','T',0},
540 hkcu
[] = {'H','K','E','Y','_','C','U','R','R','E','N','T','_','U','S','E','R',0},
541 hklm
[] = {'H','K','E','Y','_','L','O','C','A','L','_','M','A','C','H','I','N','E',0},
542 hku
[] = {'H','K','E','Y','_','U','S','E','R','S',0},
543 hkcc
[] = {'H','K','E','Y','_','C','U','R','R','E','N','T','_','C','O','N','F','I','G',0},
544 hkdd
[] = {'H','K','E','Y','_','D','Y','N','_','D','A','T','A',0};
546 tvins
.u
.item
.mask
= TVIF_TEXT
| TVIF_IMAGE
| TVIF_SELECTEDIMAGE
| TVIF_CHILDREN
| TVIF_PARAM
;
547 /* Set the text of the item. */
548 tvins
.u
.item
.pszText
= pHostName
;
549 tvins
.u
.item
.cchTextMax
= lstrlenW(pHostName
);
550 /* Assume the item is not a parent item, so give it an image. */
551 tvins
.u
.item
.iImage
= Image_Root
;
552 tvins
.u
.item
.iSelectedImage
= Image_Root
;
553 tvins
.u
.item
.cChildren
= 5;
554 /* Save the heading level in the item's application-defined data area. */
555 tvins
.u
.item
.lParam
= 0;
556 tvins
.hInsertAfter
= TVI_FIRST
;
557 tvins
.hParent
= TVI_ROOT
;
558 /* Add the item to the tree view control. */
559 if (!(hRoot
= TreeView_InsertItemW(hwndTV
, &tvins
))) return FALSE
;
561 if (!AddEntryToTree(hwndTV
, hRoot
, hkcr
, HKEY_CLASSES_ROOT
, 1)) return FALSE
;
562 if (!AddEntryToTree(hwndTV
, hRoot
, hkcu
, HKEY_CURRENT_USER
, 1)) return FALSE
;
563 if (!AddEntryToTree(hwndTV
, hRoot
, hklm
, HKEY_LOCAL_MACHINE
, 1)) return FALSE
;
564 if (!AddEntryToTree(hwndTV
, hRoot
, hku
, HKEY_USERS
, 1)) return FALSE
;
565 if (!AddEntryToTree(hwndTV
, hRoot
, hkcc
, HKEY_CURRENT_CONFIG
, 1)) return FALSE
;
566 if (!AddEntryToTree(hwndTV
, hRoot
, hkdd
, HKEY_DYN_DATA
, 1)) return FALSE
;
568 /* expand and select host name */
569 SendMessageW(hwndTV
, TVM_EXPAND
, TVE_EXPAND
, (LPARAM
)hRoot
);
570 SendMessageW(hwndTV
, TVM_SELECTITEM
, TVGN_CARET
, (LPARAM
)hRoot
);
576 * InitTreeViewImageLists - creates an image list, adds three bitmaps
577 * to it, and associates the image list with a tree view control.
578 * Returns TRUE if successful, or FALSE otherwise.
579 * hwndTV - handle to the tree view control.
581 static BOOL
InitTreeViewImageLists(HWND hwndTV
)
583 HIMAGELIST himl
; /* handle to image list */
584 HICON hico
; /* handle to icon */
586 /* Create the image list. */
587 if ((himl
= ImageList_Create(CX_ICON
, CY_ICON
,
588 ILC_MASK
, 0, NUM_ICONS
)) == NULL
)
591 /* Add the open file, closed file, and document bitmaps. */
592 hico
= LoadIconW(hInst
, MAKEINTRESOURCEW(IDI_OPEN_FILE
));
593 Image_Open
= ImageList_AddIcon(himl
, hico
);
595 hico
= LoadIconW(hInst
, MAKEINTRESOURCEW(IDI_CLOSED_FILE
));
596 Image_Closed
= ImageList_AddIcon(himl
, hico
);
598 hico
= LoadIconW(hInst
, MAKEINTRESOURCEW(IDI_ROOT
));
599 Image_Root
= ImageList_AddIcon(himl
, hico
);
601 /* Fail if not all of the images were added. */
602 if (ImageList_GetImageCount(himl
) < NUM_ICONS
)
607 /* Associate the image list with the tree view control. */
608 SendMessageW(hwndTV
, TVM_SETIMAGELIST
, TVSIL_NORMAL
, (LPARAM
)himl
);
613 BOOL
UpdateExpandingTree(HWND hwndTV
, HTREEITEM hItem
, int state
)
615 DWORD dwCount
, dwIndex
, dwMaxSubKeyLen
;
616 HKEY hRoot
, hNewKey
, hKey
;
623 static int expanding
;
624 if (expanding
) return FALSE
;
625 if (state
& TVIS_EXPANDEDONCE
) {
629 hcursorOld
= SetCursor(LoadCursorW(NULL
, (LPCWSTR
)IDC_WAIT
));
630 SendMessageW(hwndTV
, WM_SETREDRAW
, FALSE
, 0);
632 keyPath
= GetItemPath(hwndTV
, hItem
, &hRoot
);
633 if (!keyPath
) goto done
;
636 errCode
= RegOpenKeyExW(hRoot
, keyPath
, 0, KEY_READ
, &hNewKey
);
637 if (errCode
!= ERROR_SUCCESS
) goto done
;
642 errCode
= RegQueryInfoKeyW(hNewKey
, 0, 0, 0, &dwCount
, &dwMaxSubKeyLen
, 0, 0, 0, 0, 0, 0);
643 if (errCode
!= ERROR_SUCCESS
) goto done
;
644 dwMaxSubKeyLen
++; /* account for the \0 terminator */
645 Name
= HeapAlloc(GetProcessHeap(), 0, dwMaxSubKeyLen
* sizeof(WCHAR
));
646 if (!Name
) goto done
;
648 for (dwIndex
= 0; dwIndex
< dwCount
; dwIndex
++) {
649 DWORD cName
= dwMaxSubKeyLen
, dwSubCount
;
651 errCode
= RegEnumKeyExW(hNewKey
, dwIndex
, Name
, &cName
, 0, 0, 0, 0);
652 if (errCode
!= ERROR_SUCCESS
) continue;
653 errCode
= RegOpenKeyExW(hNewKey
, Name
, 0, KEY_QUERY_VALUE
, &hKey
);
654 if (errCode
== ERROR_SUCCESS
) {
655 errCode
= RegQueryInfoKeyW(hKey
, 0, 0, 0, &dwSubCount
, 0, 0, 0, 0, 0, 0, 0);
658 if (errCode
!= ERROR_SUCCESS
) dwSubCount
= 0;
659 AddEntryToTree(hwndTV
, hItem
, Name
, NULL
, dwSubCount
);
661 RegCloseKey(hNewKey
);
662 HeapFree(GetProcessHeap(), 0, Name
);
665 item
.mask
= TVIF_STATE
;
667 item
.stateMask
= TVIS_EXPANDEDONCE
;
668 item
.state
= TVIS_EXPANDEDONCE
;
669 SendMessageW(hwndTV
, TVM_SETITEMW
, 0, (LPARAM
)&item
);
670 SendMessageW(hwndTV
, WM_SETREDRAW
, TRUE
, 0);
671 SetCursor(hcursorOld
);
673 HeapFree(GetProcessHeap(), 0, keyPath
);
678 BOOL
OnTreeExpanding(HWND hwndTV
, NMTREEVIEWW
* pnmtv
)
680 return UpdateExpandingTree(hwndTV
, pnmtv
->itemNew
.hItem
, pnmtv
->itemNew
.state
);
685 * CreateTreeView - creates a tree view control.
686 * Returns the handle to the new control if successful, or NULL otherwise.
687 * hwndParent - handle to the control's parent window.
689 HWND
CreateTreeView(HWND hwndParent
, LPWSTR pHostName
, UINT id
)
693 WCHAR TreeView
[] = {'T','r','e','e',' ','V','i','e','w',0};
695 /* Get the dimensions of the parent window's client area, and create the tree view control. */
696 GetClientRect(hwndParent
, &rcClient
);
697 hwndTV
= CreateWindowExW(WS_EX_CLIENTEDGE
, WC_TREEVIEWW
, TreeView
,
698 WS_VISIBLE
| WS_CHILD
| WS_TABSTOP
| TVS_HASLINES
| TVS_HASBUTTONS
| TVS_LINESATROOT
| TVS_EDITLABELS
,
699 0, 0, rcClient
.right
, rcClient
.bottom
,
700 hwndParent
, ULongToHandle(id
), hInst
, NULL
);
701 SendMessageW(hwndTV
, TVM_SETUNICODEFORMAT
, TRUE
, 0);
702 /* Initialize the image list, and add items to the control. */
703 if (!InitTreeViewImageLists(hwndTV
) || !InitTreeViewItems(hwndTV
, pHostName
)) {
704 DestroyWindow(hwndTV
);