wined3d: Remove unused parallel_point_count variable.
[wine.git] / programs / regedit / treeview.c
blobe81e05def12dba3d23d58e01c512c3eb6846364e
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 #include <windows.h>
25 #include <commctrl.h>
26 #include <shlwapi.h>
28 #include "main.h"
29 #include <wine/debug.h>
31 WINE_DEFAULT_DEBUG_CHANNEL(regedit);
33 /* Global variables and constants */
34 /* Image_Open, Image_Closed, and Image_Root - integer variables for indexes of the images. */
35 /* CX_BITMAP and CY_BITMAP - width and height of an icon. */
36 /* NUM_BITMAPS - number of bitmaps to add to the image list. */
37 int Image_Open;
38 int Image_Closed;
39 int Image_Root;
41 #define NUM_ICONS 3
43 static BOOL UpdateExpandingTree(HWND hwndTV, HTREEITEM hItem, int state);
45 static BOOL get_item_path(HWND hwndTV, HTREEITEM hItem, HKEY* phKey, LPWSTR* pKeyPath, int* pPathLen, int* pMaxChars)
47 TVITEMW item;
48 int maxChars, chars;
49 HTREEITEM hParent;
51 item.mask = TVIF_PARAM;
52 item.hItem = hItem;
53 if (!TreeView_GetItemW(hwndTV, &item)) return FALSE;
55 if (item.lParam) {
56 /* found root key with valid key value */
57 *phKey = (HKEY)item.lParam;
58 return TRUE;
61 hParent = (HTREEITEM)SendMessageW(hwndTV, TVM_GETNEXTITEM, TVGN_PARENT, (LPARAM)hItem);
62 if(!get_item_path(hwndTV, hParent, phKey, pKeyPath, pPathLen, pMaxChars)) return FALSE;
63 if (*pPathLen) {
64 (*pKeyPath)[*pPathLen] = '\\';
65 ++(*pPathLen);
68 do {
69 item.mask = TVIF_TEXT;
70 item.hItem = hItem;
71 item.pszText = *pKeyPath + *pPathLen;
72 item.cchTextMax = maxChars = *pMaxChars - *pPathLen;
73 if (!TreeView_GetItemW(hwndTV, &item)) return FALSE;
74 chars = lstrlenW(item.pszText);
75 if (chars < maxChars - 1) {
76 *pPathLen += chars;
77 break;
80 *pMaxChars *= 2;
81 *pKeyPath = realloc(*pKeyPath, *pMaxChars);
82 } while(TRUE);
84 return TRUE;
87 LPWSTR GetItemPath(HWND hwndTV, HTREEITEM hItem, HKEY* phRootKey)
89 int pathLen = 0, maxLen = 1024;
90 WCHAR *pathBuffer;
92 if (!hItem) {
93 hItem = (HTREEITEM)SendMessageW(hwndTV, TVM_GETNEXTITEM, TVGN_CARET, 0);
94 if (!hItem) return NULL;
97 pathBuffer = malloc(maxLen * sizeof(WCHAR));
98 if (!pathBuffer) return NULL;
99 *pathBuffer = 0;
100 if (!get_item_path(hwndTV, hItem, phRootKey, &pathBuffer, &pathLen, &maxLen)) {
101 free(pathBuffer);
102 return NULL;
104 return pathBuffer;
107 static LPWSTR get_path_component(LPCWSTR *lplpKeyName) {
108 LPCWSTR lpPos = *lplpKeyName;
109 LPWSTR lpResult = NULL;
110 int len;
111 if (!lpPos)
112 return NULL;
113 while(*lpPos && *lpPos != '\\')
114 lpPos++;
115 if (*lpPos && lpPos == *lplpKeyName)
116 return NULL;
118 len = lpPos+1-(*lplpKeyName);
119 lpResult = malloc(len * sizeof(WCHAR));
121 lstrcpynW(lpResult, *lplpKeyName, len);
122 *lplpKeyName = *lpPos ? lpPos+1 : NULL;
123 return lpResult;
126 HTREEITEM FindPathInTree(HWND hwndTV, LPCWSTR lpKeyName) {
127 TVITEMEXW tvi;
128 WCHAR buf[261]; /* tree view has 260 character limitation on item name */
129 HTREEITEM hRoot, hItem, hOldItem;
130 BOOL valid_path;
132 buf[260] = '\0';
133 hRoot = (HTREEITEM)SendMessageW(hwndTV, TVM_GETNEXTITEM, TVGN_ROOT, 0);
134 hItem = hRoot;
135 SendMessageW(hwndTV, TVM_EXPAND, TVE_EXPAND, (LPARAM)hItem );
136 hItem = (HTREEITEM)SendMessageW(hwndTV, TVM_GETNEXTITEM, TVGN_CHILD, (LPARAM)hItem);
137 hOldItem = hItem;
138 valid_path = FALSE;
139 while(1) {
140 LPWSTR lpItemName = get_path_component(&lpKeyName);
142 if (lpItemName) {
143 while(hItem) {
144 tvi.mask = TVIF_TEXT | TVIF_HANDLE;
145 tvi.hItem = hItem;
146 tvi.pszText = buf;
147 tvi.cchTextMax = 260;
148 SendMessageW(hwndTV, TVM_GETITEMW, 0, (LPARAM) &tvi);
149 if (!lstrcmpiW(tvi.pszText, lpItemName)) {
150 valid_path = TRUE;
151 SendMessageW(hwndTV, TVM_EXPAND, TVE_EXPAND, (LPARAM)hItem );
152 if (!lpKeyName)
154 free(lpItemName);
155 return hItem;
157 hOldItem = hItem;
158 hItem = (HTREEITEM)SendMessageW(hwndTV, TVM_GETNEXTITEM, TVGN_CHILD, (LPARAM)hItem);
159 break;
161 hItem = (HTREEITEM)SendMessageW(hwndTV, TVM_GETNEXTITEM, TVGN_NEXT, (LPARAM)hItem);
163 free(lpItemName);
164 if (!hItem)
165 return valid_path ? hOldItem : hRoot;
167 else
168 return valid_path ? hItem : hRoot;
172 BOOL DeleteNode(HWND hwndTV, HTREEITEM hItem)
174 if (!hItem) hItem = (HTREEITEM)SendMessageW(hwndTV, TVM_GETNEXTITEM, TVGN_CARET, 0);
175 if (!hItem) return FALSE;
176 return (BOOL)SendMessageW(hwndTV, TVM_DELETEITEM, 0, (LPARAM)hItem);
179 /* Add an entry to the tree. Only give hKey for root nodes (HKEY_ constants) */
180 static HTREEITEM AddEntryToTree(HWND hwndTV, HTREEITEM hParent, LPWSTR label, HKEY hKey, DWORD dwChildren)
182 TVINSERTSTRUCTW tvins;
184 if (hKey) {
185 if (RegQueryInfoKeyW(hKey, 0, 0, 0, &dwChildren, 0, 0, 0, 0, 0, 0, 0) != ERROR_SUCCESS) {
186 dwChildren = 0;
190 tvins.item.mask = TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_CHILDREN | TVIF_PARAM;
191 tvins.item.pszText = label;
192 tvins.item.cchTextMax = lstrlenW(label);
193 tvins.item.iImage = Image_Closed;
194 tvins.item.iSelectedImage = Image_Open;
195 tvins.item.cChildren = dwChildren;
196 tvins.item.lParam = (LPARAM)hKey;
197 tvins.hInsertAfter = hKey ? TVI_LAST : TVI_SORT;
198 tvins.hParent = hParent;
200 return TreeView_InsertItemW(hwndTV, &tvins);
203 static BOOL match_string(LPCWSTR sstring1, LPCWSTR sstring2, int mode)
205 if (mode & SEARCH_WHOLE)
206 return !lstrcmpiW(sstring1, sstring2);
207 else
208 return NULL != StrStrIW(sstring1, sstring2);
211 static BOOL match_item(HWND hwndTV, HTREEITEM hItem, LPCWSTR sstring, int mode, int *row)
213 TVITEMW item;
214 WCHAR keyname[KEY_MAX_LEN];
215 item.mask = TVIF_TEXT;
216 item.hItem = hItem;
217 item.pszText = keyname;
218 item.cchTextMax = KEY_MAX_LEN;
219 if (!TreeView_GetItemW(hwndTV, &item)) return FALSE;
220 if ((mode & SEARCH_KEYS) && match_string(keyname, sstring, mode)) {
221 *row = -1;
222 return TRUE;
225 if (mode & (SEARCH_VALUES | SEARCH_CONTENT)) {
226 int i, adjust;
227 WCHAR *valName, *KeyPath;
228 HKEY hKey, hRoot;
229 DWORD lenName, lenNameMax, lenValueMax;
230 WCHAR *buffer = NULL;
232 KeyPath = GetItemPath(hwndTV, hItem, &hRoot);
234 if (!KeyPath || !hRoot)
235 return FALSE;
237 if (RegOpenKeyExW(hRoot, KeyPath, 0, KEY_READ, &hKey) != ERROR_SUCCESS) {
238 free(KeyPath);
239 return FALSE;
242 free(KeyPath);
244 if (ERROR_SUCCESS != RegQueryInfoKeyW(hKey, NULL, NULL, NULL, NULL, NULL, NULL, NULL, &lenNameMax, &lenValueMax, NULL, NULL))
245 return FALSE;
247 lenName = ++lenNameMax;
248 valName = malloc(lenName * sizeof(WCHAR));
250 adjust = 0;
252 /* RegEnumValue won't return empty default value, so fake it when dealing with *row,
253 which corresponds to list view rows, not value ids */
254 if (ERROR_SUCCESS == RegEnumValueW(hKey, 0, valName, &lenName, NULL, NULL, NULL, NULL) && *valName)
255 adjust = 1;
257 i = (*row)-adjust;
258 if (i < 0) i = 0;
259 while(1) {
260 DWORD lenValue = 0, type = 0;
261 lenName = lenNameMax;
263 if (ERROR_SUCCESS != RegEnumValueW(hKey,
264 i, valName, &lenName, NULL, &type, NULL, NULL))
265 break;
267 if (mode & SEARCH_VALUES) {
268 if (match_string(valName, sstring, mode)) {
269 free(valName);
270 free(buffer);
271 RegCloseKey(hKey);
272 *row = i+adjust;
273 return TRUE;
277 if ((mode & SEARCH_CONTENT) && (type == REG_EXPAND_SZ || type == REG_SZ)) {
278 if (!buffer)
279 buffer = malloc(lenValueMax);
281 lenName = lenNameMax;
282 lenValue = lenValueMax;
283 if (ERROR_SUCCESS != RegEnumValueW(hKey, i, valName, &lenName, NULL, &type, (LPBYTE)buffer, &lenValue))
284 break;
285 if (match_string(buffer, sstring, mode)) {
286 free(valName);
287 free(buffer);
288 RegCloseKey(hKey);
289 *row = i+adjust;
290 return TRUE;
294 i++;
296 free(valName);
297 free(buffer);
298 RegCloseKey(hKey);
300 return FALSE;
303 HTREEITEM FindNext(HWND hwndTV, HTREEITEM hItem, LPCWSTR sstring, int mode, int *row)
305 HTREEITEM hTry, hLast;
307 hLast = hItem;
308 (*row)++;
309 if (match_item(hwndTV, hLast, sstring, mode & ~SEARCH_KEYS, row)) {
310 return hLast;
312 *row = 0;
314 while(hLast) {
315 /* first look in subtree */
316 /* no children? maybe we haven't loaded them yet? */
317 if (!SendMessageW(hwndTV, TVM_GETNEXTITEM, TVGN_CHILD, (LPARAM)hLast)) {
318 UINT state = (UINT)SendMessageW(hwndTV, TVM_GETITEMSTATE, (WPARAM)hLast, -1);
319 UpdateExpandingTree(hwndTV, hLast, state);
321 hTry = (HTREEITEM)SendMessageW(hwndTV, TVM_GETNEXTITEM, TVGN_CHILD, (LPARAM)hLast);
322 if (hTry) {
323 if (match_item(hwndTV, hTry, sstring, mode, row))
324 return hTry;
325 hLast = hTry;
326 continue;
328 /* no more children, maybe there are any siblings? */
329 hTry = (HTREEITEM)SendMessageW(hwndTV, TVM_GETNEXTITEM, TVGN_NEXT, (LPARAM)hLast);
330 if (hTry) {
331 if (match_item(hwndTV, hTry, sstring, mode, row))
332 return hTry;
333 hLast = hTry;
334 continue;
336 /* no more siblings, look at the next siblings in parent(s) */
337 hLast = (HTREEITEM)SendMessageW(hwndTV, TVM_GETNEXTITEM, TVGN_PARENT, (LPARAM)hLast);
338 if (!hLast)
339 return NULL;
340 while (hLast && (hTry = (HTREEITEM)SendMessageW(hwndTV, TVM_GETNEXTITEM, TVGN_NEXT, (LPARAM)hLast)) == NULL) {
341 hLast = (HTREEITEM)SendMessageW(hwndTV, TVM_GETNEXTITEM, TVGN_PARENT, (LPARAM)hLast);
343 if (match_item(hwndTV, hTry, sstring, mode, row))
344 return hTry;
345 hLast = hTry;
347 return NULL;
350 static BOOL RefreshTreeItem(HWND hwndTV, HTREEITEM hItem)
352 HKEY hRoot, hKey, hSubKey;
353 HTREEITEM childItem;
354 LPWSTR KeyPath;
355 DWORD dwCount, dwIndex, dwMaxSubKeyLen;
356 LPWSTR Name;
357 TVITEMW tvItem;
359 hRoot = NULL;
360 KeyPath = GetItemPath(hwndTV, hItem, &hRoot);
362 if (!KeyPath || !hRoot)
363 return FALSE;
365 if (*KeyPath) {
366 if (RegOpenKeyExW(hRoot, KeyPath, 0, KEY_READ, &hKey) != ERROR_SUCCESS) {
367 WINE_TRACE("RegOpenKeyEx failed, %s was probably removed.\n", wine_dbgstr_w(KeyPath));
368 return FALSE;
370 } else {
371 hKey = hRoot;
373 free(KeyPath);
375 if (RegQueryInfoKeyW(hKey, 0, 0, 0, &dwCount, &dwMaxSubKeyLen, 0, 0, 0, 0, 0, 0) != ERROR_SUCCESS) {
376 return FALSE;
379 /* Set the number of children again */
380 tvItem.mask = TVIF_CHILDREN;
381 tvItem.hItem = hItem;
382 tvItem.cChildren = dwCount;
383 if (!TreeView_SetItemW(hwndTV, &tvItem)) {
384 return FALSE;
387 /* We don't have to bother with the rest if it's not expanded. */
388 if (SendMessageW(hwndTV, TVM_GETITEMSTATE, (WPARAM)hItem, TVIS_EXPANDED) == 0) {
389 RegCloseKey(hKey);
390 return TRUE;
393 dwMaxSubKeyLen++; /* account for the \0 terminator */
394 Name = malloc(dwMaxSubKeyLen * sizeof(WCHAR));
396 tvItem.cchTextMax = dwMaxSubKeyLen;
397 tvItem.pszText = malloc(dwMaxSubKeyLen * sizeof(WCHAR));
399 /* Now go through all the children in the registry, and check if any have to be added. */
400 for (dwIndex = 0; dwIndex < dwCount; dwIndex++) {
401 DWORD cName = dwMaxSubKeyLen, dwSubCount;
402 BOOL found;
404 found = FALSE;
405 if (RegEnumKeyExW(hKey, dwIndex, Name, &cName, 0, 0, 0, NULL) != ERROR_SUCCESS) {
406 continue;
409 /* Find the number of children of the node. */
410 dwSubCount = 0;
411 if (RegOpenKeyExW(hKey, Name, 0, KEY_QUERY_VALUE, &hSubKey) == ERROR_SUCCESS) {
412 if (RegQueryInfoKeyW(hSubKey, 0, 0, 0, &dwSubCount, 0, 0, 0, 0, 0, 0, 0) != ERROR_SUCCESS) {
413 dwSubCount = 0;
415 RegCloseKey(hSubKey);
418 /* Check if the node is already in there. */
419 for (childItem = (HTREEITEM)SendMessageW(hwndTV, TVM_GETNEXTITEM, TVGN_CHILD, (LPARAM)hItem); childItem;
420 childItem = (HTREEITEM)SendMessageW(hwndTV, TVM_GETNEXTITEM, TVGN_NEXT, (LPARAM)childItem)) {
421 tvItem.mask = TVIF_TEXT;
422 tvItem.hItem = childItem;
423 if (!TreeView_GetItemW(hwndTV, &tvItem)) {
424 free(Name);
425 free(tvItem.pszText);
426 return FALSE;
429 if (!lstrcmpiW(tvItem.pszText, Name)) {
430 found = TRUE;
431 break;
435 if (found == FALSE) {
436 WINE_TRACE("New subkey %s\n", wine_dbgstr_w(Name));
437 AddEntryToTree(hwndTV, hItem, Name, NULL, dwSubCount);
440 free(Name);
441 free(tvItem.pszText);
442 RegCloseKey(hKey);
444 /* Now go through all the children in the tree, and check if any have to be removed. */
445 childItem = (HTREEITEM)SendMessageW(hwndTV, TVM_GETNEXTITEM, TVGN_CHILD, (LPARAM)hItem);
446 while (childItem) {
447 HTREEITEM nextItem = (HTREEITEM)SendMessageW(hwndTV, TVM_GETNEXTITEM, TVGN_NEXT, (LPARAM)childItem);
448 if (RefreshTreeItem(hwndTV, childItem) == FALSE) {
449 SendMessageW(hwndTV, TVM_DELETEITEM, 0, (LPARAM)childItem);
451 childItem = nextItem;
454 return TRUE;
457 static void treeview_sort_item(HWND hWnd, HTREEITEM item)
459 HTREEITEM child = (HTREEITEM)SendMessageW(hWnd, TVM_GETNEXTITEM, TVGN_CHILD, (LPARAM)item);
461 while (child != NULL) {
462 treeview_sort_item(hWnd, child);
463 child = (HTREEITEM)SendMessageW(hWnd, TVM_GETNEXTITEM, TVGN_NEXT, (LPARAM)child);
465 SendMessageW(hWnd, TVM_SORTCHILDREN, 0, (LPARAM)item);
468 BOOL RefreshTreeView(HWND hwndTV)
470 HTREEITEM hItem;
471 HTREEITEM hSelectedItem;
472 HCURSOR hcursorOld;
473 HTREEITEM hRoot;
475 WINE_TRACE("\n");
476 hSelectedItem = (HTREEITEM)SendMessageW(hwndTV, TVM_GETNEXTITEM, TVGN_CARET, 0);
477 hcursorOld = SetCursor(LoadCursorW(NULL, (LPCWSTR)IDC_WAIT));
478 SendMessageW(hwndTV, WM_SETREDRAW, FALSE, 0);
480 hRoot = (HTREEITEM)SendMessageW(hwndTV, TVM_GETNEXTITEM, TVGN_ROOT, 0);
481 hItem = (HTREEITEM)SendMessageW(hwndTV, TVM_GETNEXTITEM, TVGN_CHILD, (LPARAM)hRoot);
482 while (hItem) {
483 RefreshTreeItem(hwndTV, hItem);
484 treeview_sort_item(hwndTV, hItem);
485 hItem = (HTREEITEM)SendMessageW(hwndTV, TVM_GETNEXTITEM, TVGN_NEXT, (LPARAM)hItem);
488 SendMessageW(hwndTV, WM_SETREDRAW, TRUE, 0);
489 InvalidateRect(hwndTV, NULL, FALSE);
490 SetCursor(hcursorOld);
492 /* We reselect the currently selected node, this will prompt a refresh of the listview. */
493 SendMessageW(hwndTV, TVM_SELECTITEM, TVGN_CARET, (LPARAM)hSelectedItem);
494 return TRUE;
497 HTREEITEM InsertNode(HWND hwndTV, HTREEITEM hItem, LPWSTR name)
499 WCHAR buf[MAX_NEW_KEY_LEN];
500 HTREEITEM hNewItem = 0;
501 TVITEMEXW item;
503 if (!hItem) hItem = (HTREEITEM)SendMessageW(hwndTV, TVM_GETNEXTITEM, TVGN_CARET, 0);
504 if (!hItem) return FALSE;
505 if (SendMessageW(hwndTV, TVM_GETITEMSTATE, (WPARAM)hItem, TVIS_EXPANDEDONCE) & TVIS_EXPANDEDONCE) {
506 hNewItem = AddEntryToTree(hwndTV, hItem, name, 0, 0);
507 } else {
508 item.mask = TVIF_CHILDREN | TVIF_HANDLE;
509 item.hItem = hItem;
510 if (!TreeView_GetItemW(hwndTV, &item)) return FALSE;
511 item.cChildren = 1;
512 if (!TreeView_SetItemW(hwndTV, &item)) return FALSE;
514 SendMessageW(hwndTV, TVM_EXPAND, TVE_EXPAND, (LPARAM)hItem );
515 if (!hNewItem) {
516 for(hNewItem = (HTREEITEM)SendMessageW(hwndTV, TVM_GETNEXTITEM, TVGN_CHILD, (LPARAM)hItem); hNewItem;
517 hNewItem = (HTREEITEM)SendMessageW(hwndTV, TVM_GETNEXTITEM, TVGN_NEXT, (LPARAM)hNewItem)) {
518 item.mask = TVIF_HANDLE | TVIF_TEXT;
519 item.hItem = hNewItem;
520 item.pszText = buf;
521 item.cchTextMax = ARRAY_SIZE(buf);
522 if (!TreeView_GetItemW(hwndTV, &item)) continue;
523 if (lstrcmpW(name, item.pszText) == 0) break;
526 if (hNewItem)
527 SendMessageW(hwndTV, TVM_SELECTITEM, TVGN_CARET, (LPARAM)hNewItem);
529 return hNewItem;
532 HWND StartKeyRename(HWND hwndTV)
534 HTREEITEM hItem;
536 if(!(hItem = (HTREEITEM)SendMessageW(hwndTV, TVM_GETNEXTITEM, TVGN_CARET, 0))) return 0;
537 SetWindowLongPtrW(hwndTV, GWLP_USERDATA, 1);
538 return (HWND)SendMessageW(hwndTV, TVM_EDITLABELW, 0, (LPARAM)hItem);
541 static BOOL InitTreeViewItems(HWND hwndTV, LPWSTR pHostName)
543 TVINSERTSTRUCTW tvins;
544 HTREEITEM hRoot;
545 static WCHAR hkcr[] = L"HKEY_CLASSES_ROOT",
546 hkcu[] = L"HKEY_CURRENT_USER",
547 hklm[] = L"HKEY_LOCAL_MACHINE",
548 hku[] = L"HKEY_USERS",
549 hkcc[] = L"HKEY_CURRENT_CONFIG",
550 hkdd[] = L"HKEY_DYN_DATA";
552 tvins.item.mask = TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_CHILDREN | TVIF_PARAM;
553 /* Set the text of the item. */
554 tvins.item.pszText = pHostName;
555 tvins.item.cchTextMax = lstrlenW(pHostName);
556 /* Assume the item is not a parent item, so give it an image. */
557 tvins.item.iImage = Image_Root;
558 tvins.item.iSelectedImage = Image_Root;
559 tvins.item.cChildren = 5;
560 /* Save the heading level in the item's application-defined data area. */
561 tvins.item.lParam = 0;
562 tvins.hInsertAfter = TVI_FIRST;
563 tvins.hParent = TVI_ROOT;
564 /* Add the item to the tree view control. */
565 if (!(hRoot = TreeView_InsertItemW(hwndTV, &tvins))) return FALSE;
567 if (!AddEntryToTree(hwndTV, hRoot, hkcr, HKEY_CLASSES_ROOT, 1)) return FALSE;
568 if (!AddEntryToTree(hwndTV, hRoot, hkcu, HKEY_CURRENT_USER, 1)) return FALSE;
569 if (!AddEntryToTree(hwndTV, hRoot, hklm, HKEY_LOCAL_MACHINE, 1)) return FALSE;
570 if (!AddEntryToTree(hwndTV, hRoot, hku, HKEY_USERS, 1)) return FALSE;
571 if (!AddEntryToTree(hwndTV, hRoot, hkcc, HKEY_CURRENT_CONFIG, 1)) return FALSE;
572 if (!AddEntryToTree(hwndTV, hRoot, hkdd, HKEY_DYN_DATA, 1)) return FALSE;
574 return TRUE;
579 * InitTreeViewImageLists - creates an image list, adds three bitmaps
580 * to it, and associates the image list with a tree view control.
581 * Returns TRUE if successful, or FALSE otherwise.
582 * hwndTV - handle to the tree view control.
584 static BOOL InitTreeViewImageLists(HWND hwndTV)
586 HIMAGELIST himl; /* handle to image list */
587 HICON hico; /* handle to icon */
588 INT cx = GetSystemMetrics(SM_CXSMICON);
589 INT cy = GetSystemMetrics(SM_CYSMICON);
591 /* Create the image list. */
592 if ((himl = ImageList_Create(cx, cy, ILC_MASK, 0, NUM_ICONS)) == NULL)
593 return FALSE;
595 /* Add the open file, closed file, and document bitmaps. */
596 hico = LoadIconW(hInst, MAKEINTRESOURCEW(IDI_OPEN_FILE));
597 Image_Open = ImageList_AddIcon(himl, hico);
599 hico = LoadIconW(hInst, MAKEINTRESOURCEW(IDI_CLOSED_FILE));
600 Image_Closed = ImageList_AddIcon(himl, hico);
602 hico = LoadIconW(hInst, MAKEINTRESOURCEW(IDI_ROOT));
603 Image_Root = ImageList_AddIcon(himl, hico);
605 /* Fail if not all of the images were added. */
606 if (ImageList_GetImageCount(himl) < NUM_ICONS)
608 return FALSE;
611 /* Associate the image list with the tree view control. */
612 SendMessageW(hwndTV, TVM_SETIMAGELIST, TVSIL_NORMAL, (LPARAM)himl);
614 return TRUE;
617 BOOL UpdateExpandingTree(HWND hwndTV, HTREEITEM hItem, int state)
619 DWORD dwCount, dwIndex, dwMaxSubKeyLen;
620 HKEY hRoot, hNewKey, hKey;
621 LPWSTR keyPath;
622 LPWSTR Name;
623 LONG errCode;
624 HCURSOR hcursorOld;
625 TVITEMW item;
627 static int expanding;
628 if (expanding) return FALSE;
629 if (state & TVIS_EXPANDEDONCE ) {
630 return TRUE;
632 expanding = TRUE;
633 hcursorOld = SetCursor(LoadCursorW(NULL, (LPCWSTR)IDC_WAIT));
634 SendMessageW(hwndTV, WM_SETREDRAW, FALSE, 0);
636 keyPath = GetItemPath(hwndTV, hItem, &hRoot);
637 if (!keyPath) goto done;
639 if (*keyPath) {
640 errCode = RegOpenKeyExW(hRoot, keyPath, 0, KEY_READ, &hNewKey);
641 if (errCode != ERROR_SUCCESS) goto done;
642 } else {
643 hNewKey = hRoot;
646 errCode = RegQueryInfoKeyW(hNewKey, 0, 0, 0, &dwCount, &dwMaxSubKeyLen, 0, 0, 0, 0, 0, 0);
647 if (errCode != ERROR_SUCCESS) goto done;
648 dwMaxSubKeyLen++; /* account for the \0 terminator */
649 Name = malloc(dwMaxSubKeyLen * sizeof(WCHAR));
651 for (dwIndex = 0; dwIndex < dwCount; dwIndex++) {
652 DWORD cName = dwMaxSubKeyLen, dwSubCount;
654 errCode = RegEnumKeyExW(hNewKey, dwIndex, Name, &cName, 0, 0, 0, 0);
655 if (errCode != ERROR_SUCCESS) continue;
656 errCode = RegOpenKeyExW(hNewKey, Name, 0, KEY_QUERY_VALUE, &hKey);
657 if (errCode == ERROR_SUCCESS) {
658 errCode = RegQueryInfoKeyW(hKey, 0, 0, 0, &dwSubCount, 0, 0, 0, 0, 0, 0, 0);
659 RegCloseKey(hKey);
661 if (errCode != ERROR_SUCCESS) dwSubCount = 0;
662 AddEntryToTree(hwndTV, hItem, Name, NULL, dwSubCount);
664 RegCloseKey(hNewKey);
665 free(Name);
667 done:
668 item.mask = TVIF_STATE;
669 item.hItem = hItem;
670 item.stateMask = TVIS_EXPANDEDONCE;
671 item.state = TVIS_EXPANDEDONCE;
672 SendMessageW(hwndTV, TVM_SETITEMW, 0, (LPARAM)&item);
673 SendMessageW(hwndTV, WM_SETREDRAW, TRUE, 0);
674 SetCursor(hcursorOld);
675 expanding = FALSE;
676 free(keyPath);
678 return TRUE;
681 BOOL OnTreeExpanding(HWND hwndTV, NMTREEVIEWW* pnmtv)
683 return UpdateExpandingTree(hwndTV, pnmtv->itemNew.hItem, pnmtv->itemNew.state);
688 * CreateTreeView - creates a tree view control.
689 * Returns the handle to the new control if successful, or NULL otherwise.
690 * hwndParent - handle to the control's parent window.
692 HWND CreateTreeView(HWND hwndParent, LPWSTR pHostName, UINT id)
694 RECT rcClient;
695 HWND hwndTV;
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, L"Tree View",
700 WS_VISIBLE | WS_CHILD | WS_TABSTOP | WS_CLIPSIBLINGS |
701 TVS_HASLINES | TVS_HASBUTTONS |
702 TVS_LINESATROOT | TVS_EDITLABELS | TVS_SHOWSELALWAYS,
703 0, 0, rcClient.right, rcClient.bottom,
704 hwndParent, ULongToHandle(id), hInst, NULL);
705 SendMessageW(hwndTV, TVM_SETUNICODEFORMAT, TRUE, 0);
706 /* Initialize the image list, and add items to the control. */
707 if (!InitTreeViewImageLists(hwndTV) || !InitTreeViewItems(hwndTV, pHostName)) {
708 DestroyWindow(hwndTV);
709 return NULL;
711 return hwndTV;