user32: Update the window DPI awareness in SetParent().
[wine.git] / programs / regedit / treeview.c
blob4bbec40df8dda1d770945dd05d84e82f2c2c8431
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 "wine/heap.h"
34 #include "main.h"
35 #include "regproc.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. */
43 int Image_Open;
44 int Image_Closed;
45 int Image_Root;
47 #define NUM_ICONS 3
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)
53 TVITEMW item;
54 int maxChars, chars;
55 HTREEITEM hParent;
57 item.mask = TVIF_PARAM;
58 item.hItem = hItem;
59 if (!TreeView_GetItemW(hwndTV, &item)) return FALSE;
61 if (item.lParam) {
62 /* found root key with valid key value */
63 *phKey = (HKEY)item.lParam;
64 return TRUE;
67 hParent = (HTREEITEM)SendMessageW(hwndTV, TVM_GETNEXTITEM, TVGN_PARENT, (LPARAM)hItem);
68 if(!get_item_path(hwndTV, hParent, phKey, pKeyPath, pPathLen, pMaxChars)) return FALSE;
69 if (*pPathLen) {
70 (*pKeyPath)[*pPathLen] = '\\';
71 ++(*pPathLen);
74 do {
75 item.mask = TVIF_TEXT;
76 item.hItem = hItem;
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) {
82 *pPathLen += chars;
83 break;
86 *pMaxChars *= 2;
87 *pKeyPath = heap_xrealloc(*pKeyPath, *pMaxChars);
88 } while(TRUE);
90 return TRUE;
93 LPWSTR GetItemPath(HWND hwndTV, HTREEITEM hItem, HKEY* phRootKey)
95 int pathLen = 0, maxLen = 1024;
96 WCHAR *pathBuffer;
98 if (!hItem) {
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;
105 *pathBuffer = 0;
106 if (!get_item_path(hwndTV, hItem, phRootKey, &pathBuffer, &pathLen, &maxLen)) return NULL;
107 return pathBuffer;
110 static LPWSTR get_path_component(LPCWSTR *lplpKeyName) {
111 LPCWSTR lpPos = *lplpKeyName;
112 LPWSTR lpResult = NULL;
113 int len;
114 if (!lpPos)
115 return NULL;
116 while(*lpPos && *lpPos != '\\')
117 lpPos++;
118 if (*lpPos && lpPos == *lplpKeyName)
119 return NULL;
121 len = lpPos+1-(*lplpKeyName);
122 lpResult = heap_xalloc(len * sizeof(WCHAR));
124 lstrcpynW(lpResult, *lplpKeyName, len);
125 *lplpKeyName = *lpPos ? lpPos+1 : NULL;
126 return lpResult;
129 HTREEITEM FindPathInTree(HWND hwndTV, LPCWSTR lpKeyName) {
130 TVITEMEXW tvi;
131 WCHAR buf[261]; /* tree view has 260 character limitation on item name */
132 HTREEITEM hRoot, hItem, hOldItem;
133 BOOL valid_path;
135 buf[260] = '\0';
136 hRoot = (HTREEITEM)SendMessageW(hwndTV, TVM_GETNEXTITEM, TVGN_ROOT, 0);
137 hItem = hRoot;
138 SendMessageW(hwndTV, TVM_EXPAND, TVE_EXPAND, (LPARAM)hItem );
139 hItem = (HTREEITEM)SendMessageW(hwndTV, TVM_GETNEXTITEM, TVGN_CHILD, (LPARAM)hItem);
140 hOldItem = hItem;
141 valid_path = FALSE;
142 while(1) {
143 LPWSTR lpItemName = get_path_component(&lpKeyName);
145 if (lpItemName) {
146 while(hItem) {
147 tvi.mask = TVIF_TEXT | TVIF_HANDLE;
148 tvi.hItem = hItem;
149 tvi.pszText = buf;
150 tvi.cchTextMax = 260;
151 SendMessageW(hwndTV, TVM_GETITEMW, 0, (LPARAM) &tvi);
152 if (!lstrcmpiW(tvi.pszText, lpItemName)) {
153 valid_path = TRUE;
154 SendMessageW(hwndTV, TVM_EXPAND, TVE_EXPAND, (LPARAM)hItem );
155 if (!lpKeyName)
157 heap_free(lpItemName);
158 return hItem;
160 hOldItem = hItem;
161 hItem = (HTREEITEM)SendMessageW(hwndTV, TVM_GETNEXTITEM, TVGN_CHILD, (LPARAM)hItem);
162 break;
164 hItem = (HTREEITEM)SendMessageW(hwndTV, TVM_GETNEXTITEM, TVGN_NEXT, (LPARAM)hItem);
166 heap_free(lpItemName);
167 if (!hItem)
168 return valid_path ? hOldItem : hRoot;
170 else
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;
187 if (hKey) {
188 if (RegQueryInfoKeyW(hKey, 0, 0, 0, &dwChildren, 0, 0, 0, 0, 0, 0, 0) != ERROR_SUCCESS) {
189 dwChildren = 0;
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);
210 else
211 return NULL != StrStrIW(sstring1, sstring2);
214 static BOOL match_item(HWND hwndTV, HTREEITEM hItem, LPCWSTR sstring, int mode, int *row)
216 TVITEMW item;
217 WCHAR keyname[KEY_MAX_LEN];
218 item.mask = TVIF_TEXT;
219 item.hItem = hItem;
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)) {
224 *row = -1;
225 return TRUE;
228 if (mode & (SEARCH_VALUES | SEARCH_CONTENT)) {
229 int i, adjust;
230 WCHAR *valName, *KeyPath;
231 HKEY hKey, hRoot;
232 DWORD lenName, lenNameMax, lenValueMax;
233 WCHAR *buffer = NULL;
235 KeyPath = GetItemPath(hwndTV, hItem, &hRoot);
237 if (!KeyPath || !hRoot)
238 return FALSE;
240 if (RegOpenKeyExW(hRoot, KeyPath, 0, KEY_READ, &hKey) != ERROR_SUCCESS) {
241 heap_free(KeyPath);
242 return FALSE;
245 heap_free(KeyPath);
247 if (ERROR_SUCCESS != RegQueryInfoKeyW(hKey, NULL, NULL, NULL, NULL, NULL, NULL, NULL, &lenNameMax, &lenValueMax, NULL, NULL))
248 return FALSE;
250 lenName = ++lenNameMax;
251 valName = heap_xalloc(lenName * sizeof(WCHAR));
253 adjust = 0;
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)
258 adjust = 1;
260 i = (*row)-adjust;
261 if (i < 0) i = 0;
262 while(1) {
263 DWORD lenValue = 0, type = 0;
264 lenName = lenNameMax;
266 if (ERROR_SUCCESS != RegEnumValueW(hKey,
267 i, valName, &lenName, NULL, &type, NULL, NULL))
268 break;
270 if (mode & SEARCH_VALUES) {
271 if (match_string(valName, sstring, mode)) {
272 heap_free(valName);
273 heap_free(buffer);
274 RegCloseKey(hKey);
275 *row = i+adjust;
276 return TRUE;
280 if ((mode & SEARCH_CONTENT) && (type == REG_EXPAND_SZ || type == REG_SZ)) {
281 if (!buffer)
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))
287 break;
288 if (match_string(buffer, sstring, mode)) {
289 heap_free(valName);
290 heap_free(buffer);
291 RegCloseKey(hKey);
292 *row = i+adjust;
293 return TRUE;
297 i++;
299 heap_free(valName);
300 heap_free(buffer);
301 RegCloseKey(hKey);
303 return FALSE;
306 HTREEITEM FindNext(HWND hwndTV, HTREEITEM hItem, LPCWSTR sstring, int mode, int *row)
308 HTREEITEM hTry, hLast;
310 hLast = hItem;
311 (*row)++;
312 if (match_item(hwndTV, hLast, sstring, mode & ~SEARCH_KEYS, row)) {
313 return hLast;
315 *row = 0;
317 while(hLast) {
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);
325 if (hTry) {
326 if (match_item(hwndTV, hTry, sstring, mode, row))
327 return hTry;
328 hLast = hTry;
329 continue;
331 /* no more children, maybe there are any siblings? */
332 hTry = (HTREEITEM)SendMessageW(hwndTV, TVM_GETNEXTITEM, TVGN_NEXT, (LPARAM)hLast);
333 if (hTry) {
334 if (match_item(hwndTV, hTry, sstring, mode, row))
335 return hTry;
336 hLast = hTry;
337 continue;
339 /* no more siblings, look at the next siblings in parent(s) */
340 hLast = (HTREEITEM)SendMessageW(hwndTV, TVM_GETNEXTITEM, TVGN_PARENT, (LPARAM)hLast);
341 if (!hLast)
342 return NULL;
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))
347 return hTry;
348 hLast = hTry;
350 return NULL;
353 static BOOL RefreshTreeItem(HWND hwndTV, HTREEITEM hItem)
355 HKEY hRoot, hKey, hSubKey;
356 HTREEITEM childItem;
357 LPWSTR KeyPath;
358 DWORD dwCount, dwIndex, dwMaxSubKeyLen;
359 LPWSTR Name;
360 TVITEMW tvItem;
362 hRoot = NULL;
363 KeyPath = GetItemPath(hwndTV, hItem, &hRoot);
365 if (!KeyPath || !hRoot)
366 return FALSE;
368 if (*KeyPath) {
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));
371 return FALSE;
373 } else {
374 hKey = hRoot;
376 heap_free(KeyPath);
378 if (RegQueryInfoKeyW(hKey, 0, 0, 0, &dwCount, &dwMaxSubKeyLen, 0, 0, 0, 0, 0, 0) != ERROR_SUCCESS) {
379 return FALSE;
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)) {
387 return FALSE;
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) {
392 RegCloseKey(hKey);
393 return TRUE;
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;
405 BOOL found;
407 found = FALSE;
408 if (RegEnumKeyExW(hKey, dwIndex, Name, &cName, 0, 0, 0, NULL) != ERROR_SUCCESS) {
409 continue;
412 /* Find the number of children of the node. */
413 dwSubCount = 0;
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) {
416 dwSubCount = 0;
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)) {
427 heap_free(Name);
428 heap_free(tvItem.pszText);
429 return FALSE;
432 if (!lstrcmpiW(tvItem.pszText, Name)) {
433 found = TRUE;
434 break;
438 if (found == FALSE) {
439 WINE_TRACE("New subkey %s\n", wine_dbgstr_w(Name));
440 AddEntryToTree(hwndTV, hItem, Name, NULL, dwSubCount);
443 heap_free(Name);
444 heap_free(tvItem.pszText);
445 RegCloseKey(hKey);
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);
449 while (childItem) {
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;
457 return TRUE;
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)
473 HTREEITEM hItem;
474 HTREEITEM hSelectedItem;
475 HCURSOR hcursorOld;
476 HTREEITEM hRoot;
478 WINE_TRACE("\n");
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);
485 while (hItem) {
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);
497 return TRUE;
500 HTREEITEM InsertNode(HWND hwndTV, HTREEITEM hItem, LPWSTR name)
502 WCHAR buf[MAX_NEW_KEY_LEN];
503 HTREEITEM hNewItem = 0;
504 TVITEMEXW item;
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);
510 } else {
511 item.mask = TVIF_CHILDREN | TVIF_HANDLE;
512 item.hItem = hItem;
513 if (!TreeView_GetItemW(hwndTV, &item)) return FALSE;
514 item.cChildren = 1;
515 if (!TreeView_SetItemW(hwndTV, &item)) return FALSE;
517 SendMessageW(hwndTV, TVM_EXPAND, TVE_EXPAND, (LPARAM)hItem );
518 if (!hNewItem) {
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;
523 item.pszText = buf;
524 item.cchTextMax = COUNT_OF(buf);
525 if (!TreeView_GetItemW(hwndTV, &item)) continue;
526 if (lstrcmpW(name, item.pszText) == 0) break;
529 if (hNewItem)
530 SendMessageW(hwndTV, TVM_SELECTITEM, TVGN_CARET, (LPARAM)hNewItem);
532 return hNewItem;
535 HWND StartKeyRename(HWND hwndTV)
537 HTREEITEM hItem;
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;
547 HTREEITEM hRoot;
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);
580 return TRUE;
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)
599 return FALSE;
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)
614 return FALSE;
617 /* Associate the image list with the tree view control. */
618 SendMessageW(hwndTV, TVM_SETIMAGELIST, TVSIL_NORMAL, (LPARAM)himl);
620 return TRUE;
623 BOOL UpdateExpandingTree(HWND hwndTV, HTREEITEM hItem, int state)
625 DWORD dwCount, dwIndex, dwMaxSubKeyLen;
626 HKEY hRoot, hNewKey, hKey;
627 LPWSTR keyPath;
628 LPWSTR Name;
629 LONG errCode;
630 HCURSOR hcursorOld;
631 TVITEMW item;
633 static int expanding;
634 if (expanding) return FALSE;
635 if (state & TVIS_EXPANDEDONCE ) {
636 return TRUE;
638 expanding = TRUE;
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;
645 if (*keyPath) {
646 errCode = RegOpenKeyExW(hRoot, keyPath, 0, KEY_READ, &hNewKey);
647 if (errCode != ERROR_SUCCESS) goto done;
648 } else {
649 hNewKey = hRoot;
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);
665 RegCloseKey(hKey);
667 if (errCode != ERROR_SUCCESS) dwSubCount = 0;
668 AddEntryToTree(hwndTV, hItem, Name, NULL, dwSubCount);
670 RegCloseKey(hNewKey);
671 heap_free(Name);
673 done:
674 item.mask = TVIF_STATE;
675 item.hItem = hItem;
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);
681 expanding = FALSE;
682 heap_free(keyPath);
684 return TRUE;
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)
700 RECT rcClient;
701 HWND hwndTV;
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);
715 return NULL;
717 return hwndTV;