comctl32/tests: Add tests for LoadIconMetric function.
[wine/multimedia.git] / programs / regedit / treeview.c
blob008e6a13ddf1fe46722d101b62b227a68ba3bcf4
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 CX_ICON 16
47 #define CY_ICON 16
48 #define NUM_ICONS 3
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)
54 TVITEMW item;
55 int maxChars, chars;
56 LPWSTR newStr;
57 HTREEITEM hParent;
59 item.mask = TVIF_PARAM;
60 item.hItem = hItem;
61 if (!TreeView_GetItemW(hwndTV, &item)) return FALSE;
63 if (item.lParam) {
64 /* found root key with valid key value */
65 *phKey = (HKEY)item.lParam;
66 return TRUE;
69 hParent = (HTREEITEM)SendMessageW(hwndTV, TVM_GETNEXTITEM, TVGN_PARENT, (LPARAM)hItem);
70 if(!get_item_path(hwndTV, hParent, phKey, pKeyPath, pPathLen, pMaxChars)) return FALSE;
71 if (*pPathLen) {
72 (*pKeyPath)[*pPathLen] = '\\';
73 ++(*pPathLen);
76 do {
77 item.mask = TVIF_TEXT;
78 item.hItem = hItem;
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) {
84 *pPathLen += chars;
85 break;
87 newStr = HeapReAlloc(GetProcessHeap(), 0, *pKeyPath, *pMaxChars * 2);
88 if (!newStr) return FALSE;
89 *pKeyPath = newStr;
90 *pMaxChars *= 2;
91 } while(TRUE);
93 return TRUE;
96 LPWSTR GetItemPath(HWND hwndTV, HTREEITEM hItem, HKEY* phRootKey)
98 int pathLen = 0, maxLen;
99 WCHAR *pathBuffer;
101 pathBuffer = HeapAlloc(GetProcessHeap(), 0, 1024*sizeof(WCHAR));
102 if (!pathBuffer) return NULL;
103 *pathBuffer = 0;
104 maxLen = HeapSize(GetProcessHeap(), 0, pathBuffer);
105 if (maxLen == (SIZE_T) - 1) return NULL;
106 maxLen = maxLen / sizeof(WCHAR);
107 if (!hItem) hItem = (HTREEITEM)SendMessageW(hwndTV, TVM_GETNEXTITEM, TVGN_CARET, 0);
108 if (!hItem) return NULL;
109 if (!get_item_path(hwndTV, hItem, phRootKey, &pathBuffer, &pathLen, &maxLen)) return NULL;
110 return pathBuffer;
113 static LPWSTR get_path_component(LPCWSTR *lplpKeyName) {
114 LPCWSTR lpPos = *lplpKeyName;
115 LPWSTR lpResult = NULL;
116 int len;
117 if (!lpPos)
118 return NULL;
119 while(*lpPos && *lpPos != '\\')
120 lpPos++;
121 if (*lpPos && lpPos == *lplpKeyName)
122 return NULL;
123 len = lpPos+1-(*lplpKeyName);
124 lpResult = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
125 if (!lpResult) /* that would be very odd */
126 return NULL;
127 lstrcpynW(lpResult, *lplpKeyName, len);
128 *lplpKeyName = *lpPos ? lpPos+1 : NULL;
129 return lpResult;
132 HTREEITEM FindPathInTree(HWND hwndTV, LPCWSTR lpKeyName) {
133 TVITEMEXW tvi;
134 WCHAR buf[261]; /* tree view has 260 character limitation on item name */
135 HTREEITEM hItem, hOldItem;
137 buf[260] = '\0';
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);
141 hOldItem = hItem;
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 SendMessageW(hwndTV, TVM_EXPAND, TVE_EXPAND, (LPARAM)hItem );
154 if (!lpKeyName)
156 HeapFree(GetProcessHeap(), 0, 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 HeapFree(GetProcessHeap(), 0, lpItemName);
166 if (!hItem)
167 return hOldItem;
169 else
170 return hItem;
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 HeapFree(GetProcessHeap(), 0, KeyPath);
241 return FALSE;
244 HeapFree(GetProcessHeap(), 0, 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 if (!(valName = HeapAlloc(GetProcessHeap(), 0, lenName * sizeof(valName[0]) )))
251 return FALSE;
253 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 HeapFree(GetProcessHeap(), 0, valName);
272 HeapFree(GetProcessHeap(), 0, 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 = HeapAlloc(GetProcessHeap(), 0, lenValueMax);
282 if (!buffer)
283 break;
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 HeapFree(GetProcessHeap(), 0, valName);
290 HeapFree(GetProcessHeap(), 0, buffer);
291 RegCloseKey(hKey);
292 *row = i+adjust;
293 return TRUE;
297 i++;
299 HeapFree(GetProcessHeap(), 0, valName);
300 HeapFree(GetProcessHeap(), 0, 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 HeapFree(GetProcessHeap(), 0, 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 if (!(Name = HeapAlloc(GetProcessHeap(), 0, dwMaxSubKeyLen * sizeof(WCHAR)))) {
398 return FALSE;
400 tvItem.cchTextMax = dwMaxSubKeyLen;
401 if (!(tvItem.pszText = HeapAlloc(GetProcessHeap(), 0, dwMaxSubKeyLen * sizeof(WCHAR)))) {
402 HeapFree(GetProcessHeap(), 0, Name);
403 return FALSE;
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;
409 BOOL found;
411 found = FALSE;
412 if (RegEnumKeyExW(hKey, dwIndex, Name, &cName, 0, 0, 0, NULL) != ERROR_SUCCESS) {
413 continue;
416 /* Find the number of children of the node. */
417 dwSubCount = 0;
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) {
420 dwSubCount = 0;
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);
433 return FALSE;
436 if (!lstrcmpiW(tvItem.pszText, Name)) {
437 found = TRUE;
438 break;
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);
449 RegCloseKey(hKey);
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);
453 while (childItem) {
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;
461 return TRUE;
464 BOOL RefreshTreeView(HWND hwndTV)
466 HTREEITEM hItem;
467 HTREEITEM hSelectedItem;
468 HCURSOR hcursorOld;
469 HTREEITEM hRoot;
471 WINE_TRACE("\n");
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);
478 while (hItem) {
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);
489 return TRUE;
492 HTREEITEM InsertNode(HWND hwndTV, HTREEITEM hItem, LPWSTR name)
494 WCHAR buf[MAX_NEW_KEY_LEN];
495 HTREEITEM hNewItem = 0;
496 TVITEMEXW item;
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);
502 } else {
503 item.mask = TVIF_CHILDREN | TVIF_HANDLE;
504 item.hItem = hItem;
505 if (!TreeView_GetItemW(hwndTV, &item)) return FALSE;
506 item.cChildren = 1;
507 if (!TreeView_SetItemW(hwndTV, &item)) return FALSE;
509 SendMessageW(hwndTV, TVM_EXPAND, TVE_EXPAND, (LPARAM)hItem );
510 if (!hNewItem) {
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;
515 item.pszText = buf;
516 item.cchTextMax = COUNT_OF(buf);
517 if (!TreeView_GetItemW(hwndTV, &item)) continue;
518 if (lstrcmpW(name, item.pszText) == 0) break;
521 if (hNewItem)
522 SendMessageW(hwndTV, TVM_SELECTITEM, TVGN_CARET, (LPARAM)hNewItem);
524 return hNewItem;
527 HWND StartKeyRename(HWND hwndTV)
529 HTREEITEM hItem;
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;
538 HTREEITEM hRoot;
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);
571 return TRUE;
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)
589 return FALSE;
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)
604 return FALSE;
607 /* Associate the image list with the tree view control. */
608 SendMessageW(hwndTV, TVM_SETIMAGELIST, TVSIL_NORMAL, (LPARAM)himl);
610 return TRUE;
613 BOOL UpdateExpandingTree(HWND hwndTV, HTREEITEM hItem, int state)
615 DWORD dwCount, dwIndex, dwMaxSubKeyLen;
616 HKEY hRoot, hNewKey, hKey;
617 LPWSTR keyPath;
618 LPWSTR Name;
619 LONG errCode;
620 HCURSOR hcursorOld;
621 TVITEMW item;
623 static int expanding;
624 if (expanding) return FALSE;
625 if (state & TVIS_EXPANDEDONCE ) {
626 return TRUE;
628 expanding = TRUE;
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;
635 if (*keyPath) {
636 errCode = RegOpenKeyExW(hRoot, keyPath, 0, KEY_READ, &hNewKey);
637 if (errCode != ERROR_SUCCESS) goto done;
638 } else {
639 hNewKey = hRoot;
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);
656 RegCloseKey(hKey);
658 if (errCode != ERROR_SUCCESS) dwSubCount = 0;
659 AddEntryToTree(hwndTV, hItem, Name, NULL, dwSubCount);
661 RegCloseKey(hNewKey);
662 HeapFree(GetProcessHeap(), 0, Name);
664 done:
665 item.mask = TVIF_STATE;
666 item.hItem = hItem;
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);
672 expanding = FALSE;
673 HeapFree(GetProcessHeap(), 0, keyPath);
675 return TRUE;
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)
691 RECT rcClient;
692 HWND hwndTV;
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);
705 return NULL;
707 return hwndTV;