comctl32/tests: Enable more ListView tests on Comctl32 v6.
[wine.git] / programs / regedit / treeview.c
blob77befb52805f6f67db2d74be886f7f69d6bb1875
1 /*
2 * Regedit treeview
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
26 #include <windows.h>
27 #include <commctrl.h>
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <wine/debug.h>
31 #include <shlwapi.h>
33 #include "main.h"
34 #include "regproc.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. */
42 int Image_Open;
43 int Image_Closed;
44 int Image_Root;
46 #define NUM_ICONS 3
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)
52 TVITEMW item;
53 int maxChars, chars;
54 HTREEITEM hParent;
56 item.mask = TVIF_PARAM;
57 item.hItem = hItem;
58 if (!TreeView_GetItemW(hwndTV, &item)) return FALSE;
60 if (item.lParam) {
61 /* found root key with valid key value */
62 *phKey = (HKEY)item.lParam;
63 return TRUE;
66 hParent = (HTREEITEM)SendMessageW(hwndTV, TVM_GETNEXTITEM, TVGN_PARENT, (LPARAM)hItem);
67 if(!get_item_path(hwndTV, hParent, phKey, pKeyPath, pPathLen, pMaxChars)) return FALSE;
68 if (*pPathLen) {
69 (*pKeyPath)[*pPathLen] = '\\';
70 ++(*pPathLen);
73 do {
74 item.mask = TVIF_TEXT;
75 item.hItem = hItem;
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) {
81 *pPathLen += chars;
82 break;
85 *pMaxChars *= 2;
86 *pKeyPath = heap_xrealloc(*pKeyPath, *pMaxChars);
87 } while(TRUE);
89 return TRUE;
92 LPWSTR GetItemPath(HWND hwndTV, HTREEITEM hItem, HKEY* phRootKey)
94 int pathLen = 0, maxLen = 1024;
95 WCHAR *pathBuffer;
97 if (!hItem) {
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;
104 *pathBuffer = 0;
105 if (!get_item_path(hwndTV, hItem, phRootKey, &pathBuffer, &pathLen, &maxLen)) return NULL;
106 return pathBuffer;
109 static LPWSTR get_path_component(LPCWSTR *lplpKeyName) {
110 LPCWSTR lpPos = *lplpKeyName;
111 LPWSTR lpResult = NULL;
112 int len;
113 if (!lpPos)
114 return NULL;
115 while(*lpPos && *lpPos != '\\')
116 lpPos++;
117 if (*lpPos && lpPos == *lplpKeyName)
118 return NULL;
120 len = lpPos+1-(*lplpKeyName);
121 lpResult = heap_xalloc(len * sizeof(WCHAR));
123 lstrcpynW(lpResult, *lplpKeyName, len);
124 *lplpKeyName = *lpPos ? lpPos+1 : NULL;
125 return lpResult;
128 HTREEITEM FindPathInTree(HWND hwndTV, LPCWSTR lpKeyName) {
129 TVITEMEXW tvi;
130 WCHAR buf[261]; /* tree view has 260 character limitation on item name */
131 HTREEITEM hRoot, hItem, hOldItem;
132 BOOL valid_path;
134 buf[260] = '\0';
135 hRoot = (HTREEITEM)SendMessageW(hwndTV, TVM_GETNEXTITEM, TVGN_ROOT, 0);
136 hItem = hRoot;
137 SendMessageW(hwndTV, TVM_EXPAND, TVE_EXPAND, (LPARAM)hItem );
138 hItem = (HTREEITEM)SendMessageW(hwndTV, TVM_GETNEXTITEM, TVGN_CHILD, (LPARAM)hItem);
139 hOldItem = hItem;
140 valid_path = FALSE;
141 while(1) {
142 LPWSTR lpItemName = get_path_component(&lpKeyName);
144 if (lpItemName) {
145 while(hItem) {
146 tvi.mask = TVIF_TEXT | TVIF_HANDLE;
147 tvi.hItem = hItem;
148 tvi.pszText = buf;
149 tvi.cchTextMax = 260;
150 SendMessageW(hwndTV, TVM_GETITEMW, 0, (LPARAM) &tvi);
151 if (!lstrcmpiW(tvi.pszText, lpItemName)) {
152 valid_path = TRUE;
153 SendMessageW(hwndTV, TVM_EXPAND, TVE_EXPAND, (LPARAM)hItem );
154 if (!lpKeyName)
156 heap_free(lpItemName);
157 return hItem;
159 hOldItem = hItem;
160 hItem = (HTREEITEM)SendMessageW(hwndTV, TVM_GETNEXTITEM, TVGN_CHILD, (LPARAM)hItem);
161 break;
163 hItem = (HTREEITEM)SendMessageW(hwndTV, TVM_GETNEXTITEM, TVGN_NEXT, (LPARAM)hItem);
165 heap_free(lpItemName);
166 if (!hItem)
167 return valid_path ? hOldItem : hRoot;
169 else
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;
186 if (hKey) {
187 if (RegQueryInfoKeyW(hKey, 0, 0, 0, &dwChildren, 0, 0, 0, 0, 0, 0, 0) != ERROR_SUCCESS) {
188 dwChildren = 0;
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);
209 else
210 return NULL != StrStrIW(sstring1, sstring2);
213 static BOOL match_item(HWND hwndTV, HTREEITEM hItem, LPCWSTR sstring, int mode, int *row)
215 TVITEMW item;
216 WCHAR keyname[KEY_MAX_LEN];
217 item.mask = TVIF_TEXT;
218 item.hItem = hItem;
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)) {
223 *row = -1;
224 return TRUE;
227 if (mode & (SEARCH_VALUES | SEARCH_CONTENT)) {
228 int i, adjust;
229 WCHAR *valName, *KeyPath;
230 HKEY hKey, hRoot;
231 DWORD lenName, lenNameMax, lenValueMax;
232 WCHAR *buffer = NULL;
234 KeyPath = GetItemPath(hwndTV, hItem, &hRoot);
236 if (!KeyPath || !hRoot)
237 return FALSE;
239 if (RegOpenKeyExW(hRoot, KeyPath, 0, KEY_READ, &hKey) != ERROR_SUCCESS) {
240 heap_free(KeyPath);
241 return FALSE;
244 heap_free(KeyPath);
246 if (ERROR_SUCCESS != RegQueryInfoKeyW(hKey, NULL, NULL, NULL, NULL, NULL, NULL, NULL, &lenNameMax, &lenValueMax, NULL, NULL))
247 return FALSE;
249 lenName = ++lenNameMax;
250 valName = heap_xalloc(lenName * sizeof(WCHAR));
252 adjust = 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)
257 adjust = 1;
259 i = (*row)-adjust;
260 if (i < 0) i = 0;
261 while(1) {
262 DWORD lenValue = 0, type = 0;
263 lenName = lenNameMax;
265 if (ERROR_SUCCESS != RegEnumValueW(hKey,
266 i, valName, &lenName, NULL, &type, NULL, NULL))
267 break;
269 if (mode & SEARCH_VALUES) {
270 if (match_string(valName, sstring, mode)) {
271 heap_free(valName);
272 heap_free(buffer);
273 RegCloseKey(hKey);
274 *row = i+adjust;
275 return TRUE;
279 if ((mode & SEARCH_CONTENT) && (type == REG_EXPAND_SZ || type == REG_SZ)) {
280 if (!buffer)
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))
286 break;
287 if (match_string(buffer, sstring, mode)) {
288 heap_free(valName);
289 heap_free(buffer);
290 RegCloseKey(hKey);
291 *row = i+adjust;
292 return TRUE;
296 i++;
298 heap_free(valName);
299 heap_free(buffer);
300 RegCloseKey(hKey);
302 return FALSE;
305 HTREEITEM FindNext(HWND hwndTV, HTREEITEM hItem, LPCWSTR sstring, int mode, int *row)
307 HTREEITEM hTry, hLast;
309 hLast = hItem;
310 (*row)++;
311 if (match_item(hwndTV, hLast, sstring, mode & ~SEARCH_KEYS, row)) {
312 return hLast;
314 *row = 0;
316 while(hLast) {
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);
324 if (hTry) {
325 if (match_item(hwndTV, hTry, sstring, mode, row))
326 return hTry;
327 hLast = hTry;
328 continue;
330 /* no more children, maybe there are any siblings? */
331 hTry = (HTREEITEM)SendMessageW(hwndTV, TVM_GETNEXTITEM, TVGN_NEXT, (LPARAM)hLast);
332 if (hTry) {
333 if (match_item(hwndTV, hTry, sstring, mode, row))
334 return hTry;
335 hLast = hTry;
336 continue;
338 /* no more siblings, look at the next siblings in parent(s) */
339 hLast = (HTREEITEM)SendMessageW(hwndTV, TVM_GETNEXTITEM, TVGN_PARENT, (LPARAM)hLast);
340 if (!hLast)
341 return NULL;
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))
346 return hTry;
347 hLast = hTry;
349 return NULL;
352 static BOOL RefreshTreeItem(HWND hwndTV, HTREEITEM hItem)
354 HKEY hRoot, hKey, hSubKey;
355 HTREEITEM childItem;
356 LPWSTR KeyPath;
357 DWORD dwCount, dwIndex, dwMaxSubKeyLen;
358 LPWSTR Name;
359 TVITEMW tvItem;
361 hRoot = NULL;
362 KeyPath = GetItemPath(hwndTV, hItem, &hRoot);
364 if (!KeyPath || !hRoot)
365 return FALSE;
367 if (*KeyPath) {
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));
370 return FALSE;
372 } else {
373 hKey = hRoot;
375 heap_free(KeyPath);
377 if (RegQueryInfoKeyW(hKey, 0, 0, 0, &dwCount, &dwMaxSubKeyLen, 0, 0, 0, 0, 0, 0) != ERROR_SUCCESS) {
378 return FALSE;
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)) {
386 return FALSE;
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) {
391 RegCloseKey(hKey);
392 return TRUE;
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;
404 BOOL found;
406 found = FALSE;
407 if (RegEnumKeyExW(hKey, dwIndex, Name, &cName, 0, 0, 0, NULL) != ERROR_SUCCESS) {
408 continue;
411 /* Find the number of children of the node. */
412 dwSubCount = 0;
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) {
415 dwSubCount = 0;
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)) {
426 heap_free(Name);
427 heap_free(tvItem.pszText);
428 return FALSE;
431 if (!lstrcmpiW(tvItem.pszText, Name)) {
432 found = TRUE;
433 break;
437 if (found == FALSE) {
438 WINE_TRACE("New subkey %s\n", wine_dbgstr_w(Name));
439 AddEntryToTree(hwndTV, hItem, Name, NULL, dwSubCount);
442 heap_free(Name);
443 heap_free(tvItem.pszText);
444 RegCloseKey(hKey);
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);
448 while (childItem) {
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;
456 return TRUE;
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)
472 HTREEITEM hItem;
473 HTREEITEM hSelectedItem;
474 HCURSOR hcursorOld;
475 HTREEITEM hRoot;
477 WINE_TRACE("\n");
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);
484 while (hItem) {
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);
496 return TRUE;
499 HTREEITEM InsertNode(HWND hwndTV, HTREEITEM hItem, LPWSTR name)
501 WCHAR buf[MAX_NEW_KEY_LEN];
502 HTREEITEM hNewItem = 0;
503 TVITEMEXW item;
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);
509 } else {
510 item.mask = TVIF_CHILDREN | TVIF_HANDLE;
511 item.hItem = hItem;
512 if (!TreeView_GetItemW(hwndTV, &item)) return FALSE;
513 item.cChildren = 1;
514 if (!TreeView_SetItemW(hwndTV, &item)) return FALSE;
516 SendMessageW(hwndTV, TVM_EXPAND, TVE_EXPAND, (LPARAM)hItem );
517 if (!hNewItem) {
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;
522 item.pszText = buf;
523 item.cchTextMax = COUNT_OF(buf);
524 if (!TreeView_GetItemW(hwndTV, &item)) continue;
525 if (lstrcmpW(name, item.pszText) == 0) break;
528 if (hNewItem)
529 SendMessageW(hwndTV, TVM_SELECTITEM, TVGN_CARET, (LPARAM)hNewItem);
531 return hNewItem;
534 HWND StartKeyRename(HWND hwndTV)
536 HTREEITEM hItem;
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;
546 HTREEITEM hRoot;
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);
579 return TRUE;
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)
598 return FALSE;
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)
613 return FALSE;
616 /* Associate the image list with the tree view control. */
617 SendMessageW(hwndTV, TVM_SETIMAGELIST, TVSIL_NORMAL, (LPARAM)himl);
619 return TRUE;
622 BOOL UpdateExpandingTree(HWND hwndTV, HTREEITEM hItem, int state)
624 DWORD dwCount, dwIndex, dwMaxSubKeyLen;
625 HKEY hRoot, hNewKey, hKey;
626 LPWSTR keyPath;
627 LPWSTR Name;
628 LONG errCode;
629 HCURSOR hcursorOld;
630 TVITEMW item;
632 static int expanding;
633 if (expanding) return FALSE;
634 if (state & TVIS_EXPANDEDONCE ) {
635 return TRUE;
637 expanding = TRUE;
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;
644 if (*keyPath) {
645 errCode = RegOpenKeyExW(hRoot, keyPath, 0, KEY_READ, &hNewKey);
646 if (errCode != ERROR_SUCCESS) goto done;
647 } else {
648 hNewKey = hRoot;
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);
664 RegCloseKey(hKey);
666 if (errCode != ERROR_SUCCESS) dwSubCount = 0;
667 AddEntryToTree(hwndTV, hItem, Name, NULL, dwSubCount);
669 RegCloseKey(hNewKey);
670 heap_free(Name);
672 done:
673 item.mask = TVIF_STATE;
674 item.hItem = hItem;
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);
680 expanding = FALSE;
681 heap_free(keyPath);
683 return TRUE;
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)
699 RECT rcClient;
700 HWND hwndTV;
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);
714 return NULL;
716 return hwndTV;