regedit: Use the system metrics to retrieve the treeview icon sizes.
[wine.git] / programs / regedit / treeview.c
blobd11793ca9d7b967f114e84db395ad9af48e32527
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 LPWSTR newStr;
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;
85 newStr = HeapReAlloc(GetProcessHeap(), 0, *pKeyPath, *pMaxChars * 2);
86 if (!newStr) return FALSE;
87 *pKeyPath = newStr;
88 *pMaxChars *= 2;
89 } while(TRUE);
91 return TRUE;
94 LPWSTR GetItemPath(HWND hwndTV, HTREEITEM hItem, HKEY* phRootKey)
96 int pathLen = 0, maxLen = 1024;
97 WCHAR *pathBuffer;
99 if (!hItem) {
100 hItem = (HTREEITEM)SendMessageW(hwndTV, TVM_GETNEXTITEM, TVGN_CARET, 0);
101 if (!hItem) return NULL;
104 pathBuffer = HeapAlloc(GetProcessHeap(), 0, maxLen*sizeof(WCHAR));
105 if (!pathBuffer) return NULL;
106 *pathBuffer = 0;
107 if (!get_item_path(hwndTV, hItem, phRootKey, &pathBuffer, &pathLen, &maxLen)) return NULL;
108 return pathBuffer;
111 static LPWSTR get_path_component(LPCWSTR *lplpKeyName) {
112 LPCWSTR lpPos = *lplpKeyName;
113 LPWSTR lpResult = NULL;
114 int len;
115 if (!lpPos)
116 return NULL;
117 while(*lpPos && *lpPos != '\\')
118 lpPos++;
119 if (*lpPos && lpPos == *lplpKeyName)
120 return NULL;
121 len = lpPos+1-(*lplpKeyName);
122 lpResult = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
123 if (!lpResult) /* that would be very odd */
124 return NULL;
125 lstrcpynW(lpResult, *lplpKeyName, len);
126 *lplpKeyName = *lpPos ? lpPos+1 : NULL;
127 return lpResult;
130 HTREEITEM FindPathInTree(HWND hwndTV, LPCWSTR lpKeyName) {
131 TVITEMEXW tvi;
132 WCHAR buf[261]; /* tree view has 260 character limitation on item name */
133 HTREEITEM hRoot, hItem, hOldItem;
134 BOOL valid_path;
136 buf[260] = '\0';
137 hRoot = (HTREEITEM)SendMessageW(hwndTV, TVM_GETNEXTITEM, TVGN_ROOT, 0);
138 hItem = hRoot;
139 SendMessageW(hwndTV, TVM_EXPAND, TVE_EXPAND, (LPARAM)hItem );
140 hItem = (HTREEITEM)SendMessageW(hwndTV, TVM_GETNEXTITEM, TVGN_CHILD, (LPARAM)hItem);
141 hOldItem = hItem;
142 valid_path = FALSE;
143 while(1) {
144 LPWSTR lpItemName = get_path_component(&lpKeyName);
146 if (lpItemName) {
147 while(hItem) {
148 tvi.mask = TVIF_TEXT | TVIF_HANDLE;
149 tvi.hItem = hItem;
150 tvi.pszText = buf;
151 tvi.cchTextMax = 260;
152 SendMessageW(hwndTV, TVM_GETITEMW, 0, (LPARAM) &tvi);
153 if (!lstrcmpiW(tvi.pszText, lpItemName)) {
154 valid_path = TRUE;
155 SendMessageW(hwndTV, TVM_EXPAND, TVE_EXPAND, (LPARAM)hItem );
156 if (!lpKeyName)
158 HeapFree(GetProcessHeap(), 0, lpItemName);
159 return hItem;
161 hOldItem = hItem;
162 hItem = (HTREEITEM)SendMessageW(hwndTV, TVM_GETNEXTITEM, TVGN_CHILD, (LPARAM)hItem);
163 break;
165 hItem = (HTREEITEM)SendMessageW(hwndTV, TVM_GETNEXTITEM, TVGN_NEXT, (LPARAM)hItem);
167 HeapFree(GetProcessHeap(), 0, lpItemName);
168 if (!hItem)
169 return valid_path ? hOldItem : hRoot;
171 else
172 return valid_path ? hItem : hRoot;
176 BOOL DeleteNode(HWND hwndTV, HTREEITEM hItem)
178 if (!hItem) hItem = (HTREEITEM)SendMessageW(hwndTV, TVM_GETNEXTITEM, TVGN_CARET, 0);
179 if (!hItem) return FALSE;
180 return (BOOL)SendMessageW(hwndTV, TVM_DELETEITEM, 0, (LPARAM)hItem);
183 /* Add an entry to the tree. Only give hKey for root nodes (HKEY_ constants) */
184 static HTREEITEM AddEntryToTree(HWND hwndTV, HTREEITEM hParent, LPWSTR label, HKEY hKey, DWORD dwChildren)
186 TVINSERTSTRUCTW tvins;
188 if (hKey) {
189 if (RegQueryInfoKeyW(hKey, 0, 0, 0, &dwChildren, 0, 0, 0, 0, 0, 0, 0) != ERROR_SUCCESS) {
190 dwChildren = 0;
194 tvins.u.item.mask = TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_CHILDREN | TVIF_PARAM;
195 tvins.u.item.pszText = label;
196 tvins.u.item.cchTextMax = lstrlenW(label);
197 tvins.u.item.iImage = Image_Closed;
198 tvins.u.item.iSelectedImage = Image_Open;
199 tvins.u.item.cChildren = dwChildren;
200 tvins.u.item.lParam = (LPARAM)hKey;
201 tvins.hInsertAfter = hKey ? TVI_LAST : TVI_SORT;
202 tvins.hParent = hParent;
204 return TreeView_InsertItemW(hwndTV, &tvins);
207 static BOOL match_string(LPCWSTR sstring1, LPCWSTR sstring2, int mode)
209 if (mode & SEARCH_WHOLE)
210 return !lstrcmpiW(sstring1, sstring2);
211 else
212 return NULL != StrStrIW(sstring1, sstring2);
215 static BOOL match_item(HWND hwndTV, HTREEITEM hItem, LPCWSTR sstring, int mode, int *row)
217 TVITEMW item;
218 WCHAR keyname[KEY_MAX_LEN];
219 item.mask = TVIF_TEXT;
220 item.hItem = hItem;
221 item.pszText = keyname;
222 item.cchTextMax = KEY_MAX_LEN;
223 if (!TreeView_GetItemW(hwndTV, &item)) return FALSE;
224 if ((mode & SEARCH_KEYS) && match_string(keyname, sstring, mode)) {
225 *row = -1;
226 return TRUE;
229 if (mode & (SEARCH_VALUES | SEARCH_CONTENT)) {
230 int i, adjust;
231 WCHAR *valName, *KeyPath;
232 HKEY hKey, hRoot;
233 DWORD lenName, lenNameMax, lenValueMax;
234 WCHAR *buffer = NULL;
236 KeyPath = GetItemPath(hwndTV, hItem, &hRoot);
238 if (!KeyPath || !hRoot)
239 return FALSE;
241 if (RegOpenKeyExW(hRoot, KeyPath, 0, KEY_READ, &hKey) != ERROR_SUCCESS) {
242 HeapFree(GetProcessHeap(), 0, KeyPath);
243 return FALSE;
246 HeapFree(GetProcessHeap(), 0, KeyPath);
248 if (ERROR_SUCCESS != RegQueryInfoKeyW(hKey, NULL, NULL, NULL, NULL, NULL, NULL, NULL, &lenNameMax, &lenValueMax, NULL, NULL))
249 return FALSE;
251 lenName = ++lenNameMax;
252 if (!(valName = HeapAlloc(GetProcessHeap(), 0, lenName * sizeof(valName[0]) )))
253 return FALSE;
255 adjust = 0;
256 /* RegEnumValue won't return empty default value, so fake it when dealing with *row,
257 which corresponds to list view rows, not value ids */
258 if (ERROR_SUCCESS == RegEnumValueW(hKey, 0, valName, &lenName, NULL, NULL, NULL, NULL) && *valName)
259 adjust = 1;
261 i = (*row)-adjust;
262 if (i < 0) i = 0;
263 while(1) {
264 DWORD lenValue = 0, type = 0;
265 lenName = lenNameMax;
267 if (ERROR_SUCCESS != RegEnumValueW(hKey,
268 i, valName, &lenName, NULL, &type, NULL, NULL))
269 break;
271 if (mode & SEARCH_VALUES) {
272 if (match_string(valName, sstring, mode)) {
273 HeapFree(GetProcessHeap(), 0, valName);
274 HeapFree(GetProcessHeap(), 0, buffer);
275 RegCloseKey(hKey);
276 *row = i+adjust;
277 return TRUE;
281 if ((mode & SEARCH_CONTENT) && (type == REG_EXPAND_SZ || type == REG_SZ)) {
282 if (!buffer)
283 buffer = HeapAlloc(GetProcessHeap(), 0, lenValueMax);
284 if (!buffer)
285 break;
286 lenName = lenNameMax;
287 lenValue = lenValueMax;
288 if (ERROR_SUCCESS != RegEnumValueW(hKey, i, valName, &lenName, NULL, &type, (LPBYTE)buffer, &lenValue))
289 break;
290 if (match_string(buffer, sstring, mode)) {
291 HeapFree(GetProcessHeap(), 0, valName);
292 HeapFree(GetProcessHeap(), 0, buffer);
293 RegCloseKey(hKey);
294 *row = i+adjust;
295 return TRUE;
299 i++;
301 HeapFree(GetProcessHeap(), 0, valName);
302 HeapFree(GetProcessHeap(), 0, buffer);
303 RegCloseKey(hKey);
305 return FALSE;
308 HTREEITEM FindNext(HWND hwndTV, HTREEITEM hItem, LPCWSTR sstring, int mode, int *row)
310 HTREEITEM hTry, hLast;
312 hLast = hItem;
313 (*row)++;
314 if (match_item(hwndTV, hLast, sstring, mode & ~SEARCH_KEYS, row)) {
315 return hLast;
317 *row = 0;
319 while(hLast) {
320 /* first look in subtree */
321 /* no children? maybe we haven't loaded them yet? */
322 if (!SendMessageW(hwndTV, TVM_GETNEXTITEM, TVGN_CHILD, (LPARAM)hLast)) {
323 UINT state = (UINT)SendMessageW(hwndTV, TVM_GETITEMSTATE, (WPARAM)hLast, -1);
324 UpdateExpandingTree(hwndTV, hLast, state);
326 hTry = (HTREEITEM)SendMessageW(hwndTV, TVM_GETNEXTITEM, TVGN_CHILD, (LPARAM)hLast);
327 if (hTry) {
328 if (match_item(hwndTV, hTry, sstring, mode, row))
329 return hTry;
330 hLast = hTry;
331 continue;
333 /* no more children, maybe there are any siblings? */
334 hTry = (HTREEITEM)SendMessageW(hwndTV, TVM_GETNEXTITEM, TVGN_NEXT, (LPARAM)hLast);
335 if (hTry) {
336 if (match_item(hwndTV, hTry, sstring, mode, row))
337 return hTry;
338 hLast = hTry;
339 continue;
341 /* no more siblings, look at the next siblings in parent(s) */
342 hLast = (HTREEITEM)SendMessageW(hwndTV, TVM_GETNEXTITEM, TVGN_PARENT, (LPARAM)hLast);
343 if (!hLast)
344 return NULL;
345 while (hLast && (hTry = (HTREEITEM)SendMessageW(hwndTV, TVM_GETNEXTITEM, TVGN_NEXT, (LPARAM)hLast)) == NULL) {
346 hLast = (HTREEITEM)SendMessageW(hwndTV, TVM_GETNEXTITEM, TVGN_PARENT, (LPARAM)hLast);
348 if (match_item(hwndTV, hTry, sstring, mode, row))
349 return hTry;
350 hLast = hTry;
352 return NULL;
355 static BOOL RefreshTreeItem(HWND hwndTV, HTREEITEM hItem)
357 HKEY hRoot, hKey, hSubKey;
358 HTREEITEM childItem;
359 LPWSTR KeyPath;
360 DWORD dwCount, dwIndex, dwMaxSubKeyLen;
361 LPWSTR Name;
362 TVITEMW tvItem;
364 hRoot = NULL;
365 KeyPath = GetItemPath(hwndTV, hItem, &hRoot);
367 if (!KeyPath || !hRoot)
368 return FALSE;
370 if (*KeyPath) {
371 if (RegOpenKeyExW(hRoot, KeyPath, 0, KEY_READ, &hKey) != ERROR_SUCCESS) {
372 WINE_TRACE("RegOpenKeyEx failed, %s was probably removed.\n", wine_dbgstr_w(KeyPath));
373 return FALSE;
375 } else {
376 hKey = hRoot;
378 HeapFree(GetProcessHeap(), 0, KeyPath);
380 if (RegQueryInfoKeyW(hKey, 0, 0, 0, &dwCount, &dwMaxSubKeyLen, 0, 0, 0, 0, 0, 0) != ERROR_SUCCESS) {
381 return FALSE;
384 /* Set the number of children again */
385 tvItem.mask = TVIF_CHILDREN;
386 tvItem.hItem = hItem;
387 tvItem.cChildren = dwCount;
388 if (!TreeView_SetItemW(hwndTV, &tvItem)) {
389 return FALSE;
392 /* We don't have to bother with the rest if it's not expanded. */
393 if (SendMessageW(hwndTV, TVM_GETITEMSTATE, (WPARAM)hItem, TVIS_EXPANDED) == 0) {
394 RegCloseKey(hKey);
395 return TRUE;
398 dwMaxSubKeyLen++; /* account for the \0 terminator */
399 if (!(Name = HeapAlloc(GetProcessHeap(), 0, dwMaxSubKeyLen * sizeof(WCHAR)))) {
400 return FALSE;
402 tvItem.cchTextMax = dwMaxSubKeyLen;
403 if (!(tvItem.pszText = HeapAlloc(GetProcessHeap(), 0, dwMaxSubKeyLen * sizeof(WCHAR)))) {
404 HeapFree(GetProcessHeap(), 0, Name);
405 return FALSE;
408 /* Now go through all the children in the registry, and check if any have to be added. */
409 for (dwIndex = 0; dwIndex < dwCount; dwIndex++) {
410 DWORD cName = dwMaxSubKeyLen, dwSubCount;
411 BOOL found;
413 found = FALSE;
414 if (RegEnumKeyExW(hKey, dwIndex, Name, &cName, 0, 0, 0, NULL) != ERROR_SUCCESS) {
415 continue;
418 /* Find the number of children of the node. */
419 dwSubCount = 0;
420 if (RegOpenKeyExW(hKey, Name, 0, KEY_QUERY_VALUE, &hSubKey) == ERROR_SUCCESS) {
421 if (RegQueryInfoKeyW(hSubKey, 0, 0, 0, &dwSubCount, 0, 0, 0, 0, 0, 0, 0) != ERROR_SUCCESS) {
422 dwSubCount = 0;
424 RegCloseKey(hSubKey);
427 /* Check if the node is already in there. */
428 for (childItem = (HTREEITEM)SendMessageW(hwndTV, TVM_GETNEXTITEM, TVGN_CHILD, (LPARAM)hItem); childItem;
429 childItem = (HTREEITEM)SendMessageW(hwndTV, TVM_GETNEXTITEM, TVGN_NEXT, (LPARAM)childItem)) {
430 tvItem.mask = TVIF_TEXT;
431 tvItem.hItem = childItem;
432 if (!TreeView_GetItemW(hwndTV, &tvItem)) {
433 HeapFree(GetProcessHeap(), 0, Name);
434 HeapFree(GetProcessHeap(), 0, tvItem.pszText);
435 return FALSE;
438 if (!lstrcmpiW(tvItem.pszText, Name)) {
439 found = TRUE;
440 break;
444 if (found == FALSE) {
445 WINE_TRACE("New subkey %s\n", wine_dbgstr_w(Name));
446 AddEntryToTree(hwndTV, hItem, Name, NULL, dwSubCount);
449 HeapFree(GetProcessHeap(), 0, Name);
450 HeapFree(GetProcessHeap(), 0, tvItem.pszText);
451 RegCloseKey(hKey);
453 /* Now go through all the children in the tree, and check if any have to be removed. */
454 childItem = (HTREEITEM)SendMessageW(hwndTV, TVM_GETNEXTITEM, TVGN_CHILD, (LPARAM)hItem);
455 while (childItem) {
456 HTREEITEM nextItem = (HTREEITEM)SendMessageW(hwndTV, TVM_GETNEXTITEM, TVGN_NEXT, (LPARAM)childItem);
457 if (RefreshTreeItem(hwndTV, childItem) == FALSE) {
458 SendMessageW(hwndTV, TVM_DELETEITEM, 0, (LPARAM)childItem);
460 childItem = nextItem;
463 return TRUE;
466 static void treeview_sort_item(HWND hWnd, HTREEITEM item)
468 HTREEITEM child = (HTREEITEM)SendMessageW(hWnd, TVM_GETNEXTITEM, TVGN_CHILD, (LPARAM)item);
470 while (child != NULL) {
471 treeview_sort_item(hWnd, child);
472 child = (HTREEITEM)SendMessageW(hWnd, TVM_GETNEXTITEM, TVGN_NEXT, (LPARAM)child);
474 SendMessageW(hWnd, TVM_SORTCHILDREN, 0, (LPARAM)item);
477 BOOL RefreshTreeView(HWND hwndTV)
479 HTREEITEM hItem;
480 HTREEITEM hSelectedItem;
481 HCURSOR hcursorOld;
482 HTREEITEM hRoot;
484 WINE_TRACE("\n");
485 hSelectedItem = (HTREEITEM)SendMessageW(hwndTV, TVM_GETNEXTITEM, TVGN_CARET, 0);
486 hcursorOld = SetCursor(LoadCursorW(NULL, (LPCWSTR)IDC_WAIT));
487 SendMessageW(hwndTV, WM_SETREDRAW, FALSE, 0);
489 hRoot = (HTREEITEM)SendMessageW(hwndTV, TVM_GETNEXTITEM, TVGN_ROOT, 0);
490 hItem = (HTREEITEM)SendMessageW(hwndTV, TVM_GETNEXTITEM, TVGN_CHILD, (LPARAM)hRoot);
491 while (hItem) {
492 RefreshTreeItem(hwndTV, hItem);
493 treeview_sort_item(hwndTV, hItem);
494 hItem = (HTREEITEM)SendMessageW(hwndTV, TVM_GETNEXTITEM, TVGN_NEXT, (LPARAM)hItem);
497 SendMessageW(hwndTV, WM_SETREDRAW, TRUE, 0);
498 InvalidateRect(hwndTV, NULL, FALSE);
499 SetCursor(hcursorOld);
501 /* We reselect the currently selected node, this will prompt a refresh of the listview. */
502 SendMessageW(hwndTV, TVM_SELECTITEM, TVGN_CARET, (LPARAM)hSelectedItem);
503 return TRUE;
506 HTREEITEM InsertNode(HWND hwndTV, HTREEITEM hItem, LPWSTR name)
508 WCHAR buf[MAX_NEW_KEY_LEN];
509 HTREEITEM hNewItem = 0;
510 TVITEMEXW item;
512 if (!hItem) hItem = (HTREEITEM)SendMessageW(hwndTV, TVM_GETNEXTITEM, TVGN_CARET, 0);
513 if (!hItem) return FALSE;
514 if (SendMessageW(hwndTV, TVM_GETITEMSTATE, (WPARAM)hItem, TVIS_EXPANDEDONCE) & TVIS_EXPANDEDONCE) {
515 hNewItem = AddEntryToTree(hwndTV, hItem, name, 0, 0);
516 } else {
517 item.mask = TVIF_CHILDREN | TVIF_HANDLE;
518 item.hItem = hItem;
519 if (!TreeView_GetItemW(hwndTV, &item)) return FALSE;
520 item.cChildren = 1;
521 if (!TreeView_SetItemW(hwndTV, &item)) return FALSE;
523 SendMessageW(hwndTV, TVM_EXPAND, TVE_EXPAND, (LPARAM)hItem );
524 if (!hNewItem) {
525 for(hNewItem = (HTREEITEM)SendMessageW(hwndTV, TVM_GETNEXTITEM, TVGN_CHILD, (LPARAM)hItem); hNewItem;
526 hNewItem = (HTREEITEM)SendMessageW(hwndTV, TVM_GETNEXTITEM, TVGN_NEXT, (LPARAM)hNewItem)) {
527 item.mask = TVIF_HANDLE | TVIF_TEXT;
528 item.hItem = hNewItem;
529 item.pszText = buf;
530 item.cchTextMax = COUNT_OF(buf);
531 if (!TreeView_GetItemW(hwndTV, &item)) continue;
532 if (lstrcmpW(name, item.pszText) == 0) break;
535 if (hNewItem)
536 SendMessageW(hwndTV, TVM_SELECTITEM, TVGN_CARET, (LPARAM)hNewItem);
538 return hNewItem;
541 HWND StartKeyRename(HWND hwndTV)
543 HTREEITEM hItem;
545 if(!(hItem = (HTREEITEM)SendMessageW(hwndTV, TVM_GETNEXTITEM, TVGN_CARET, 0))) return 0;
546 SetWindowLongPtrW(hwndTV, GWLP_USERDATA, 1);
547 return (HWND)SendMessageW(hwndTV, TVM_EDITLABELW, 0, (LPARAM)hItem);
550 static BOOL InitTreeViewItems(HWND hwndTV, LPWSTR pHostName)
552 TVINSERTSTRUCTW tvins;
553 HTREEITEM hRoot;
554 static WCHAR hkcr[] = {'H','K','E','Y','_','C','L','A','S','S','E','S','_','R','O','O','T',0},
555 hkcu[] = {'H','K','E','Y','_','C','U','R','R','E','N','T','_','U','S','E','R',0},
556 hklm[] = {'H','K','E','Y','_','L','O','C','A','L','_','M','A','C','H','I','N','E',0},
557 hku[] = {'H','K','E','Y','_','U','S','E','R','S',0},
558 hkcc[] = {'H','K','E','Y','_','C','U','R','R','E','N','T','_','C','O','N','F','I','G',0},
559 hkdd[] = {'H','K','E','Y','_','D','Y','N','_','D','A','T','A',0};
561 tvins.u.item.mask = TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_CHILDREN | TVIF_PARAM;
562 /* Set the text of the item. */
563 tvins.u.item.pszText = pHostName;
564 tvins.u.item.cchTextMax = lstrlenW(pHostName);
565 /* Assume the item is not a parent item, so give it an image. */
566 tvins.u.item.iImage = Image_Root;
567 tvins.u.item.iSelectedImage = Image_Root;
568 tvins.u.item.cChildren = 5;
569 /* Save the heading level in the item's application-defined data area. */
570 tvins.u.item.lParam = 0;
571 tvins.hInsertAfter = TVI_FIRST;
572 tvins.hParent = TVI_ROOT;
573 /* Add the item to the tree view control. */
574 if (!(hRoot = TreeView_InsertItemW(hwndTV, &tvins))) return FALSE;
576 if (!AddEntryToTree(hwndTV, hRoot, hkcr, HKEY_CLASSES_ROOT, 1)) return FALSE;
577 if (!AddEntryToTree(hwndTV, hRoot, hkcu, HKEY_CURRENT_USER, 1)) return FALSE;
578 if (!AddEntryToTree(hwndTV, hRoot, hklm, HKEY_LOCAL_MACHINE, 1)) return FALSE;
579 if (!AddEntryToTree(hwndTV, hRoot, hku, HKEY_USERS, 1)) return FALSE;
580 if (!AddEntryToTree(hwndTV, hRoot, hkcc, HKEY_CURRENT_CONFIG, 1)) return FALSE;
581 if (!AddEntryToTree(hwndTV, hRoot, hkdd, HKEY_DYN_DATA, 1)) return FALSE;
583 /* expand and select host name */
584 SendMessageW(hwndTV, TVM_EXPAND, TVE_EXPAND, (LPARAM)hRoot );
585 SendMessageW(hwndTV, TVM_SELECTITEM, TVGN_CARET, (LPARAM)hRoot);
586 return TRUE;
591 * InitTreeViewImageLists - creates an image list, adds three bitmaps
592 * to it, and associates the image list with a tree view control.
593 * Returns TRUE if successful, or FALSE otherwise.
594 * hwndTV - handle to the tree view control.
596 static BOOL InitTreeViewImageLists(HWND hwndTV)
598 HIMAGELIST himl; /* handle to image list */
599 HICON hico; /* handle to icon */
600 INT cx = GetSystemMetrics(SM_CXSMICON);
601 INT cy = GetSystemMetrics(SM_CYSMICON);
603 /* Create the image list. */
604 if ((himl = ImageList_Create(cx, cy, ILC_MASK, 0, NUM_ICONS)) == NULL)
605 return FALSE;
607 /* Add the open file, closed file, and document bitmaps. */
608 hico = LoadIconW(hInst, MAKEINTRESOURCEW(IDI_OPEN_FILE));
609 Image_Open = ImageList_AddIcon(himl, hico);
611 hico = LoadIconW(hInst, MAKEINTRESOURCEW(IDI_CLOSED_FILE));
612 Image_Closed = ImageList_AddIcon(himl, hico);
614 hico = LoadIconW(hInst, MAKEINTRESOURCEW(IDI_ROOT));
615 Image_Root = ImageList_AddIcon(himl, hico);
617 /* Fail if not all of the images were added. */
618 if (ImageList_GetImageCount(himl) < NUM_ICONS)
620 return FALSE;
623 /* Associate the image list with the tree view control. */
624 SendMessageW(hwndTV, TVM_SETIMAGELIST, TVSIL_NORMAL, (LPARAM)himl);
626 return TRUE;
629 BOOL UpdateExpandingTree(HWND hwndTV, HTREEITEM hItem, int state)
631 DWORD dwCount, dwIndex, dwMaxSubKeyLen;
632 HKEY hRoot, hNewKey, hKey;
633 LPWSTR keyPath;
634 LPWSTR Name;
635 LONG errCode;
636 HCURSOR hcursorOld;
637 TVITEMW item;
639 static int expanding;
640 if (expanding) return FALSE;
641 if (state & TVIS_EXPANDEDONCE ) {
642 return TRUE;
644 expanding = TRUE;
645 hcursorOld = SetCursor(LoadCursorW(NULL, (LPCWSTR)IDC_WAIT));
646 SendMessageW(hwndTV, WM_SETREDRAW, FALSE, 0);
648 keyPath = GetItemPath(hwndTV, hItem, &hRoot);
649 if (!keyPath) goto done;
651 if (*keyPath) {
652 errCode = RegOpenKeyExW(hRoot, keyPath, 0, KEY_READ, &hNewKey);
653 if (errCode != ERROR_SUCCESS) goto done;
654 } else {
655 hNewKey = hRoot;
658 errCode = RegQueryInfoKeyW(hNewKey, 0, 0, 0, &dwCount, &dwMaxSubKeyLen, 0, 0, 0, 0, 0, 0);
659 if (errCode != ERROR_SUCCESS) goto done;
660 dwMaxSubKeyLen++; /* account for the \0 terminator */
661 Name = HeapAlloc(GetProcessHeap(), 0, dwMaxSubKeyLen * sizeof(WCHAR));
662 if (!Name) goto done;
664 for (dwIndex = 0; dwIndex < dwCount; dwIndex++) {
665 DWORD cName = dwMaxSubKeyLen, dwSubCount;
667 errCode = RegEnumKeyExW(hNewKey, dwIndex, Name, &cName, 0, 0, 0, 0);
668 if (errCode != ERROR_SUCCESS) continue;
669 errCode = RegOpenKeyExW(hNewKey, Name, 0, KEY_QUERY_VALUE, &hKey);
670 if (errCode == ERROR_SUCCESS) {
671 errCode = RegQueryInfoKeyW(hKey, 0, 0, 0, &dwSubCount, 0, 0, 0, 0, 0, 0, 0);
672 RegCloseKey(hKey);
674 if (errCode != ERROR_SUCCESS) dwSubCount = 0;
675 AddEntryToTree(hwndTV, hItem, Name, NULL, dwSubCount);
677 RegCloseKey(hNewKey);
678 HeapFree(GetProcessHeap(), 0, Name);
680 done:
681 item.mask = TVIF_STATE;
682 item.hItem = hItem;
683 item.stateMask = TVIS_EXPANDEDONCE;
684 item.state = TVIS_EXPANDEDONCE;
685 SendMessageW(hwndTV, TVM_SETITEMW, 0, (LPARAM)&item);
686 SendMessageW(hwndTV, WM_SETREDRAW, TRUE, 0);
687 SetCursor(hcursorOld);
688 expanding = FALSE;
689 HeapFree(GetProcessHeap(), 0, keyPath);
691 return TRUE;
694 BOOL OnTreeExpanding(HWND hwndTV, NMTREEVIEWW* pnmtv)
696 return UpdateExpandingTree(hwndTV, pnmtv->itemNew.hItem, pnmtv->itemNew.state);
701 * CreateTreeView - creates a tree view control.
702 * Returns the handle to the new control if successful, or NULL otherwise.
703 * hwndParent - handle to the control's parent window.
705 HWND CreateTreeView(HWND hwndParent, LPWSTR pHostName, UINT id)
707 RECT rcClient;
708 HWND hwndTV;
709 WCHAR TreeView[] = {'T','r','e','e',' ','V','i','e','w',0};
711 /* Get the dimensions of the parent window's client area, and create the tree view control. */
712 GetClientRect(hwndParent, &rcClient);
713 hwndTV = CreateWindowExW(WS_EX_CLIENTEDGE, WC_TREEVIEWW, TreeView,
714 WS_VISIBLE | WS_CHILD | WS_TABSTOP | TVS_HASLINES | TVS_HASBUTTONS |
715 TVS_LINESATROOT | TVS_EDITLABELS | TVS_SHOWSELALWAYS,
716 0, 0, rcClient.right, rcClient.bottom,
717 hwndParent, ULongToHandle(id), hInst, NULL);
718 SendMessageW(hwndTV, TVM_SETUNICODEFORMAT, TRUE, 0);
719 /* Initialize the image list, and add items to the control. */
720 if (!InitTreeViewImageLists(hwndTV) || !InitTreeViewItems(hwndTV, pHostName)) {
721 DestroyWindow(hwndTV);
722 return NULL;
724 return hwndTV;