dpnet/tests: Add tests to show IDirectPlay8ThreadPool is a singleton object.
[wine.git] / programs / regedit / treeview.c
blobd07399debc07295b63350a2c737d66c859de9df3
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 = 1024;
99 WCHAR *pathBuffer;
101 if (!hItem) {
102 hItem = (HTREEITEM)SendMessageW(hwndTV, TVM_GETNEXTITEM, TVGN_CARET, 0);
103 if (!hItem) return NULL;
106 pathBuffer = HeapAlloc(GetProcessHeap(), 0, maxLen*sizeof(WCHAR));
107 if (!pathBuffer) return NULL;
108 *pathBuffer = 0;
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 SendMessageW(hwndTV, TVM_SORTCHILDREN, TRUE, (LPARAM)hSelectedItem);
486 SetCursor(hcursorOld);
488 /* We reselect the currently selected node, this will prompt a refresh of the listview. */
489 SendMessageW(hwndTV, TVM_SELECTITEM, TVGN_CARET, (LPARAM)hSelectedItem);
490 return TRUE;
493 HTREEITEM InsertNode(HWND hwndTV, HTREEITEM hItem, LPWSTR name)
495 WCHAR buf[MAX_NEW_KEY_LEN];
496 HTREEITEM hNewItem = 0;
497 TVITEMEXW item;
499 if (!hItem) hItem = (HTREEITEM)SendMessageW(hwndTV, TVM_GETNEXTITEM, TVGN_CARET, 0);
500 if (!hItem) return FALSE;
501 if (SendMessageW(hwndTV, TVM_GETITEMSTATE, (WPARAM)hItem, TVIS_EXPANDEDONCE) & TVIS_EXPANDEDONCE) {
502 hNewItem = AddEntryToTree(hwndTV, hItem, name, 0, 0);
503 } else {
504 item.mask = TVIF_CHILDREN | TVIF_HANDLE;
505 item.hItem = hItem;
506 if (!TreeView_GetItemW(hwndTV, &item)) return FALSE;
507 item.cChildren = 1;
508 if (!TreeView_SetItemW(hwndTV, &item)) return FALSE;
510 SendMessageW(hwndTV, TVM_EXPAND, TVE_EXPAND, (LPARAM)hItem );
511 if (!hNewItem) {
512 for(hNewItem = (HTREEITEM)SendMessageW(hwndTV, TVM_GETNEXTITEM, TVGN_CHILD, (LPARAM)hItem); hNewItem;
513 hNewItem = (HTREEITEM)SendMessageW(hwndTV, TVM_GETNEXTITEM, TVGN_NEXT, (LPARAM)hNewItem)) {
514 item.mask = TVIF_HANDLE | TVIF_TEXT;
515 item.hItem = hNewItem;
516 item.pszText = buf;
517 item.cchTextMax = COUNT_OF(buf);
518 if (!TreeView_GetItemW(hwndTV, &item)) continue;
519 if (lstrcmpW(name, item.pszText) == 0) break;
522 if (hNewItem)
523 SendMessageW(hwndTV, TVM_SELECTITEM, TVGN_CARET, (LPARAM)hNewItem);
525 return hNewItem;
528 HWND StartKeyRename(HWND hwndTV)
530 HTREEITEM hItem;
532 if(!(hItem = (HTREEITEM)SendMessageW(hwndTV, TVM_GETNEXTITEM, TVGN_CARET, 0))) return 0;
533 SetWindowLongPtrW(hwndTV, GWLP_USERDATA, 1);
534 return (HWND)SendMessageW(hwndTV, TVM_EDITLABELW, 0, (LPARAM)hItem);
537 static BOOL InitTreeViewItems(HWND hwndTV, LPWSTR pHostName)
539 TVINSERTSTRUCTW tvins;
540 HTREEITEM hRoot;
541 static WCHAR hkcr[] = {'H','K','E','Y','_','C','L','A','S','S','E','S','_','R','O','O','T',0},
542 hkcu[] = {'H','K','E','Y','_','C','U','R','R','E','N','T','_','U','S','E','R',0},
543 hklm[] = {'H','K','E','Y','_','L','O','C','A','L','_','M','A','C','H','I','N','E',0},
544 hku[] = {'H','K','E','Y','_','U','S','E','R','S',0},
545 hkcc[] = {'H','K','E','Y','_','C','U','R','R','E','N','T','_','C','O','N','F','I','G',0},
546 hkdd[] = {'H','K','E','Y','_','D','Y','N','_','D','A','T','A',0};
548 tvins.u.item.mask = TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_CHILDREN | TVIF_PARAM;
549 /* Set the text of the item. */
550 tvins.u.item.pszText = pHostName;
551 tvins.u.item.cchTextMax = lstrlenW(pHostName);
552 /* Assume the item is not a parent item, so give it an image. */
553 tvins.u.item.iImage = Image_Root;
554 tvins.u.item.iSelectedImage = Image_Root;
555 tvins.u.item.cChildren = 5;
556 /* Save the heading level in the item's application-defined data area. */
557 tvins.u.item.lParam = 0;
558 tvins.hInsertAfter = TVI_FIRST;
559 tvins.hParent = TVI_ROOT;
560 /* Add the item to the tree view control. */
561 if (!(hRoot = TreeView_InsertItemW(hwndTV, &tvins))) return FALSE;
563 if (!AddEntryToTree(hwndTV, hRoot, hkcr, HKEY_CLASSES_ROOT, 1)) return FALSE;
564 if (!AddEntryToTree(hwndTV, hRoot, hkcu, HKEY_CURRENT_USER, 1)) return FALSE;
565 if (!AddEntryToTree(hwndTV, hRoot, hklm, HKEY_LOCAL_MACHINE, 1)) return FALSE;
566 if (!AddEntryToTree(hwndTV, hRoot, hku, HKEY_USERS, 1)) return FALSE;
567 if (!AddEntryToTree(hwndTV, hRoot, hkcc, HKEY_CURRENT_CONFIG, 1)) return FALSE;
568 if (!AddEntryToTree(hwndTV, hRoot, hkdd, HKEY_DYN_DATA, 1)) return FALSE;
570 /* expand and select host name */
571 SendMessageW(hwndTV, TVM_EXPAND, TVE_EXPAND, (LPARAM)hRoot );
572 SendMessageW(hwndTV, TVM_SELECTITEM, TVGN_CARET, (LPARAM)hRoot);
573 return TRUE;
578 * InitTreeViewImageLists - creates an image list, adds three bitmaps
579 * to it, and associates the image list with a tree view control.
580 * Returns TRUE if successful, or FALSE otherwise.
581 * hwndTV - handle to the tree view control.
583 static BOOL InitTreeViewImageLists(HWND hwndTV)
585 HIMAGELIST himl; /* handle to image list */
586 HICON hico; /* handle to icon */
588 /* Create the image list. */
589 if ((himl = ImageList_Create(CX_ICON, CY_ICON,
590 ILC_MASK, 0, NUM_ICONS)) == NULL)
591 return FALSE;
593 /* Add the open file, closed file, and document bitmaps. */
594 hico = LoadIconW(hInst, MAKEINTRESOURCEW(IDI_OPEN_FILE));
595 Image_Open = ImageList_AddIcon(himl, hico);
597 hico = LoadIconW(hInst, MAKEINTRESOURCEW(IDI_CLOSED_FILE));
598 Image_Closed = ImageList_AddIcon(himl, hico);
600 hico = LoadIconW(hInst, MAKEINTRESOURCEW(IDI_ROOT));
601 Image_Root = ImageList_AddIcon(himl, hico);
603 /* Fail if not all of the images were added. */
604 if (ImageList_GetImageCount(himl) < NUM_ICONS)
606 return FALSE;
609 /* Associate the image list with the tree view control. */
610 SendMessageW(hwndTV, TVM_SETIMAGELIST, TVSIL_NORMAL, (LPARAM)himl);
612 return TRUE;
615 BOOL UpdateExpandingTree(HWND hwndTV, HTREEITEM hItem, int state)
617 DWORD dwCount, dwIndex, dwMaxSubKeyLen;
618 HKEY hRoot, hNewKey, hKey;
619 LPWSTR keyPath;
620 LPWSTR Name;
621 LONG errCode;
622 HCURSOR hcursorOld;
623 TVITEMW item;
625 static int expanding;
626 if (expanding) return FALSE;
627 if (state & TVIS_EXPANDEDONCE ) {
628 return TRUE;
630 expanding = TRUE;
631 hcursorOld = SetCursor(LoadCursorW(NULL, (LPCWSTR)IDC_WAIT));
632 SendMessageW(hwndTV, WM_SETREDRAW, FALSE, 0);
634 keyPath = GetItemPath(hwndTV, hItem, &hRoot);
635 if (!keyPath) goto done;
637 if (*keyPath) {
638 errCode = RegOpenKeyExW(hRoot, keyPath, 0, KEY_READ, &hNewKey);
639 if (errCode != ERROR_SUCCESS) goto done;
640 } else {
641 hNewKey = hRoot;
644 errCode = RegQueryInfoKeyW(hNewKey, 0, 0, 0, &dwCount, &dwMaxSubKeyLen, 0, 0, 0, 0, 0, 0);
645 if (errCode != ERROR_SUCCESS) goto done;
646 dwMaxSubKeyLen++; /* account for the \0 terminator */
647 Name = HeapAlloc(GetProcessHeap(), 0, dwMaxSubKeyLen * sizeof(WCHAR));
648 if (!Name) goto done;
650 for (dwIndex = 0; dwIndex < dwCount; dwIndex++) {
651 DWORD cName = dwMaxSubKeyLen, dwSubCount;
653 errCode = RegEnumKeyExW(hNewKey, dwIndex, Name, &cName, 0, 0, 0, 0);
654 if (errCode != ERROR_SUCCESS) continue;
655 errCode = RegOpenKeyExW(hNewKey, Name, 0, KEY_QUERY_VALUE, &hKey);
656 if (errCode == ERROR_SUCCESS) {
657 errCode = RegQueryInfoKeyW(hKey, 0, 0, 0, &dwSubCount, 0, 0, 0, 0, 0, 0, 0);
658 RegCloseKey(hKey);
660 if (errCode != ERROR_SUCCESS) dwSubCount = 0;
661 AddEntryToTree(hwndTV, hItem, Name, NULL, dwSubCount);
663 RegCloseKey(hNewKey);
664 HeapFree(GetProcessHeap(), 0, Name);
666 done:
667 item.mask = TVIF_STATE;
668 item.hItem = hItem;
669 item.stateMask = TVIS_EXPANDEDONCE;
670 item.state = TVIS_EXPANDEDONCE;
671 SendMessageW(hwndTV, TVM_SETITEMW, 0, (LPARAM)&item);
672 SendMessageW(hwndTV, WM_SETREDRAW, TRUE, 0);
673 SetCursor(hcursorOld);
674 expanding = FALSE;
675 HeapFree(GetProcessHeap(), 0, keyPath);
677 return TRUE;
680 BOOL OnTreeExpanding(HWND hwndTV, NMTREEVIEWW* pnmtv)
682 return UpdateExpandingTree(hwndTV, pnmtv->itemNew.hItem, pnmtv->itemNew.state);
687 * CreateTreeView - creates a tree view control.
688 * Returns the handle to the new control if successful, or NULL otherwise.
689 * hwndParent - handle to the control's parent window.
691 HWND CreateTreeView(HWND hwndParent, LPWSTR pHostName, UINT id)
693 RECT rcClient;
694 HWND hwndTV;
695 WCHAR TreeView[] = {'T','r','e','e',' ','V','i','e','w',0};
697 /* Get the dimensions of the parent window's client area, and create the tree view control. */
698 GetClientRect(hwndParent, &rcClient);
699 hwndTV = CreateWindowExW(WS_EX_CLIENTEDGE, WC_TREEVIEWW, TreeView,
700 WS_VISIBLE | WS_CHILD | WS_TABSTOP | TVS_HASLINES | TVS_HASBUTTONS |
701 TVS_LINESATROOT | TVS_EDITLABELS | TVS_SHOWSELALWAYS,
702 0, 0, rcClient.right, rcClient.bottom,
703 hwndParent, ULongToHandle(id), hInst, NULL);
704 SendMessageW(hwndTV, TVM_SETUNICODEFORMAT, TRUE, 0);
705 /* Initialize the image list, and add items to the control. */
706 if (!InitTreeViewImageLists(hwndTV) || !InitTreeViewItems(hwndTV, pHostName)) {
707 DestroyWindow(hwndTV);
708 return NULL;
710 return hwndTV;