treeview.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714
  1. /*
  2. * Regedit treeview
  3. *
  4. * Copyright (C) 2002 Robert Dickenson <robd@reactos.org>
  5. * Copyright (C) 2008 Alexander N. Sørnes <alex@thehandofagony.com>
  6. *
  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.
  11. *
  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.
  16. *
  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
  20. */
  21. #define WIN32_LEAN_AND_MEAN /* Exclude rarely-used stuff from Windows headers */
  22. #define NONAMELESSUNION
  23. #include <windows.h>
  24. #include <commctrl.h>
  25. #include <stdlib.h>
  26. #include <wine/debug.h>
  27. #include <shlwapi.h>
  28. #include "wine/heap.h"
  29. #include "main.h"
  30. WINE_DEFAULT_DEBUG_CHANNEL(regedit);
  31. /* Global variables and constants */
  32. /* Image_Open, Image_Closed, and Image_Root - integer variables for indexes of the images. */
  33. /* CX_BITMAP and CY_BITMAP - width and height of an icon. */
  34. /* NUM_BITMAPS - number of bitmaps to add to the image list. */
  35. int Image_Open;
  36. int Image_Closed;
  37. int Image_Root;
  38. #define NUM_ICONS 3
  39. static BOOL UpdateExpandingTree(HWND hwndTV, HTREEITEM hItem, int state);
  40. static BOOL get_item_path(HWND hwndTV, HTREEITEM hItem, HKEY* phKey, LPWSTR* pKeyPath, int* pPathLen, int* pMaxChars)
  41. {
  42. TVITEMW item;
  43. int maxChars, chars;
  44. HTREEITEM hParent;
  45. item.mask = TVIF_PARAM;
  46. item.hItem = hItem;
  47. if (!TreeView_GetItemW(hwndTV, &item)) return FALSE;
  48. if (item.lParam) {
  49. /* found root key with valid key value */
  50. *phKey = (HKEY)item.lParam;
  51. return TRUE;
  52. }
  53. hParent = (HTREEITEM)SendMessageW(hwndTV, TVM_GETNEXTITEM, TVGN_PARENT, (LPARAM)hItem);
  54. if(!get_item_path(hwndTV, hParent, phKey, pKeyPath, pPathLen, pMaxChars)) return FALSE;
  55. if (*pPathLen) {
  56. (*pKeyPath)[*pPathLen] = '\\';
  57. ++(*pPathLen);
  58. }
  59. do {
  60. item.mask = TVIF_TEXT;
  61. item.hItem = hItem;
  62. item.pszText = *pKeyPath + *pPathLen;
  63. item.cchTextMax = maxChars = *pMaxChars - *pPathLen;
  64. if (!TreeView_GetItemW(hwndTV, &item)) return FALSE;
  65. chars = lstrlenW(item.pszText);
  66. if (chars < maxChars - 1) {
  67. *pPathLen += chars;
  68. break;
  69. }
  70. *pMaxChars *= 2;
  71. *pKeyPath = heap_xrealloc(*pKeyPath, *pMaxChars);
  72. } while(TRUE);
  73. return TRUE;
  74. }
  75. LPWSTR GetItemPath(HWND hwndTV, HTREEITEM hItem, HKEY* phRootKey)
  76. {
  77. int pathLen = 0, maxLen = 1024;
  78. WCHAR *pathBuffer;
  79. if (!hItem) {
  80. hItem = (HTREEITEM)SendMessageW(hwndTV, TVM_GETNEXTITEM, TVGN_CARET, 0);
  81. if (!hItem) return NULL;
  82. }
  83. pathBuffer = heap_xalloc(maxLen * sizeof(WCHAR));
  84. if (!pathBuffer) return NULL;
  85. *pathBuffer = 0;
  86. if (!get_item_path(hwndTV, hItem, phRootKey, &pathBuffer, &pathLen, &maxLen)) return NULL;
  87. return pathBuffer;
  88. }
  89. static LPWSTR get_path_component(LPCWSTR *lplpKeyName) {
  90. LPCWSTR lpPos = *lplpKeyName;
  91. LPWSTR lpResult = NULL;
  92. int len;
  93. if (!lpPos)
  94. return NULL;
  95. while(*lpPos && *lpPos != '\\')
  96. lpPos++;
  97. if (*lpPos && lpPos == *lplpKeyName)
  98. return NULL;
  99. len = lpPos+1-(*lplpKeyName);
  100. lpResult = heap_xalloc(len * sizeof(WCHAR));
  101. lstrcpynW(lpResult, *lplpKeyName, len);
  102. *lplpKeyName = *lpPos ? lpPos+1 : NULL;
  103. return lpResult;
  104. }
  105. HTREEITEM FindPathInTree(HWND hwndTV, LPCWSTR lpKeyName) {
  106. TVITEMEXW tvi;
  107. WCHAR buf[261]; /* tree view has 260 character limitation on item name */
  108. HTREEITEM hRoot, hItem, hOldItem;
  109. BOOL valid_path;
  110. buf[260] = '\0';
  111. hRoot = (HTREEITEM)SendMessageW(hwndTV, TVM_GETNEXTITEM, TVGN_ROOT, 0);
  112. hItem = hRoot;
  113. SendMessageW(hwndTV, TVM_EXPAND, TVE_EXPAND, (LPARAM)hItem );
  114. hItem = (HTREEITEM)SendMessageW(hwndTV, TVM_GETNEXTITEM, TVGN_CHILD, (LPARAM)hItem);
  115. hOldItem = hItem;
  116. valid_path = FALSE;
  117. while(1) {
  118. LPWSTR lpItemName = get_path_component(&lpKeyName);
  119. if (lpItemName) {
  120. while(hItem) {
  121. tvi.mask = TVIF_TEXT | TVIF_HANDLE;
  122. tvi.hItem = hItem;
  123. tvi.pszText = buf;
  124. tvi.cchTextMax = 260;
  125. SendMessageW(hwndTV, TVM_GETITEMW, 0, (LPARAM) &tvi);
  126. if (!lstrcmpiW(tvi.pszText, lpItemName)) {
  127. valid_path = TRUE;
  128. SendMessageW(hwndTV, TVM_EXPAND, TVE_EXPAND, (LPARAM)hItem );
  129. if (!lpKeyName)
  130. {
  131. heap_free(lpItemName);
  132. return hItem;
  133. }
  134. hOldItem = hItem;
  135. hItem = (HTREEITEM)SendMessageW(hwndTV, TVM_GETNEXTITEM, TVGN_CHILD, (LPARAM)hItem);
  136. break;
  137. }
  138. hItem = (HTREEITEM)SendMessageW(hwndTV, TVM_GETNEXTITEM, TVGN_NEXT, (LPARAM)hItem);
  139. }
  140. heap_free(lpItemName);
  141. if (!hItem)
  142. return valid_path ? hOldItem : hRoot;
  143. }
  144. else
  145. return valid_path ? hItem : hRoot;
  146. }
  147. }
  148. BOOL DeleteNode(HWND hwndTV, HTREEITEM hItem)
  149. {
  150. if (!hItem) hItem = (HTREEITEM)SendMessageW(hwndTV, TVM_GETNEXTITEM, TVGN_CARET, 0);
  151. if (!hItem) return FALSE;
  152. return (BOOL)SendMessageW(hwndTV, TVM_DELETEITEM, 0, (LPARAM)hItem);
  153. }
  154. /* Add an entry to the tree. Only give hKey for root nodes (HKEY_ constants) */
  155. static HTREEITEM AddEntryToTree(HWND hwndTV, HTREEITEM hParent, LPWSTR label, HKEY hKey, DWORD dwChildren)
  156. {
  157. TVINSERTSTRUCTW tvins;
  158. if (hKey) {
  159. if (RegQueryInfoKeyW(hKey, 0, 0, 0, &dwChildren, 0, 0, 0, 0, 0, 0, 0) != ERROR_SUCCESS) {
  160. dwChildren = 0;
  161. }
  162. }
  163. tvins.u.item.mask = TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_CHILDREN | TVIF_PARAM;
  164. tvins.u.item.pszText = label;
  165. tvins.u.item.cchTextMax = lstrlenW(label);
  166. tvins.u.item.iImage = Image_Closed;
  167. tvins.u.item.iSelectedImage = Image_Open;
  168. tvins.u.item.cChildren = dwChildren;
  169. tvins.u.item.lParam = (LPARAM)hKey;
  170. tvins.hInsertAfter = hKey ? TVI_LAST : TVI_SORT;
  171. tvins.hParent = hParent;
  172. return TreeView_InsertItemW(hwndTV, &tvins);
  173. }
  174. static BOOL match_string(LPCWSTR sstring1, LPCWSTR sstring2, int mode)
  175. {
  176. if (mode & SEARCH_WHOLE)
  177. return !lstrcmpiW(sstring1, sstring2);
  178. else
  179. return NULL != StrStrIW(sstring1, sstring2);
  180. }
  181. static BOOL match_item(HWND hwndTV, HTREEITEM hItem, LPCWSTR sstring, int mode, int *row)
  182. {
  183. TVITEMW item;
  184. WCHAR keyname[KEY_MAX_LEN];
  185. item.mask = TVIF_TEXT;
  186. item.hItem = hItem;
  187. item.pszText = keyname;
  188. item.cchTextMax = KEY_MAX_LEN;
  189. if (!TreeView_GetItemW(hwndTV, &item)) return FALSE;
  190. if ((mode & SEARCH_KEYS) && match_string(keyname, sstring, mode)) {
  191. *row = -1;
  192. return TRUE;
  193. }
  194. if (mode & (SEARCH_VALUES | SEARCH_CONTENT)) {
  195. int i, adjust;
  196. WCHAR *valName, *KeyPath;
  197. HKEY hKey, hRoot;
  198. DWORD lenName, lenNameMax, lenValueMax;
  199. WCHAR *buffer = NULL;
  200. KeyPath = GetItemPath(hwndTV, hItem, &hRoot);
  201. if (!KeyPath || !hRoot)
  202. return FALSE;
  203. if (RegOpenKeyExW(hRoot, KeyPath, 0, KEY_READ, &hKey) != ERROR_SUCCESS) {
  204. heap_free(KeyPath);
  205. return FALSE;
  206. }
  207. heap_free(KeyPath);
  208. if (ERROR_SUCCESS != RegQueryInfoKeyW(hKey, NULL, NULL, NULL, NULL, NULL, NULL, NULL, &lenNameMax, &lenValueMax, NULL, NULL))
  209. return FALSE;
  210. lenName = ++lenNameMax;
  211. valName = heap_xalloc(lenName * sizeof(WCHAR));
  212. adjust = 0;
  213. /* RegEnumValue won't return empty default value, so fake it when dealing with *row,
  214. which corresponds to list view rows, not value ids */
  215. if (ERROR_SUCCESS == RegEnumValueW(hKey, 0, valName, &lenName, NULL, NULL, NULL, NULL) && *valName)
  216. adjust = 1;
  217. i = (*row)-adjust;
  218. if (i < 0) i = 0;
  219. while(1) {
  220. DWORD lenValue = 0, type = 0;
  221. lenName = lenNameMax;
  222. if (ERROR_SUCCESS != RegEnumValueW(hKey,
  223. i, valName, &lenName, NULL, &type, NULL, NULL))
  224. break;
  225. if (mode & SEARCH_VALUES) {
  226. if (match_string(valName, sstring, mode)) {
  227. heap_free(valName);
  228. heap_free(buffer);
  229. RegCloseKey(hKey);
  230. *row = i+adjust;
  231. return TRUE;
  232. }
  233. }
  234. if ((mode & SEARCH_CONTENT) && (type == REG_EXPAND_SZ || type == REG_SZ)) {
  235. if (!buffer)
  236. buffer = heap_xalloc(lenValueMax);
  237. lenName = lenNameMax;
  238. lenValue = lenValueMax;
  239. if (ERROR_SUCCESS != RegEnumValueW(hKey, i, valName, &lenName, NULL, &type, (LPBYTE)buffer, &lenValue))
  240. break;
  241. if (match_string(buffer, sstring, mode)) {
  242. heap_free(valName);
  243. heap_free(buffer);
  244. RegCloseKey(hKey);
  245. *row = i+adjust;
  246. return TRUE;
  247. }
  248. }
  249. i++;
  250. }
  251. heap_free(valName);
  252. heap_free(buffer);
  253. RegCloseKey(hKey);
  254. }
  255. return FALSE;
  256. }
  257. HTREEITEM FindNext(HWND hwndTV, HTREEITEM hItem, LPCWSTR sstring, int mode, int *row)
  258. {
  259. HTREEITEM hTry, hLast;
  260. hLast = hItem;
  261. (*row)++;
  262. if (match_item(hwndTV, hLast, sstring, mode & ~SEARCH_KEYS, row)) {
  263. return hLast;
  264. }
  265. *row = 0;
  266. while(hLast) {
  267. /* first look in subtree */
  268. /* no children? maybe we haven't loaded them yet? */
  269. if (!SendMessageW(hwndTV, TVM_GETNEXTITEM, TVGN_CHILD, (LPARAM)hLast)) {
  270. UINT state = (UINT)SendMessageW(hwndTV, TVM_GETITEMSTATE, (WPARAM)hLast, -1);
  271. UpdateExpandingTree(hwndTV, hLast, state);
  272. }
  273. hTry = (HTREEITEM)SendMessageW(hwndTV, TVM_GETNEXTITEM, TVGN_CHILD, (LPARAM)hLast);
  274. if (hTry) {
  275. if (match_item(hwndTV, hTry, sstring, mode, row))
  276. return hTry;
  277. hLast = hTry;
  278. continue;
  279. }
  280. /* no more children, maybe there are any siblings? */
  281. hTry = (HTREEITEM)SendMessageW(hwndTV, TVM_GETNEXTITEM, TVGN_NEXT, (LPARAM)hLast);
  282. if (hTry) {
  283. if (match_item(hwndTV, hTry, sstring, mode, row))
  284. return hTry;
  285. hLast = hTry;
  286. continue;
  287. }
  288. /* no more siblings, look at the next siblings in parent(s) */
  289. hLast = (HTREEITEM)SendMessageW(hwndTV, TVM_GETNEXTITEM, TVGN_PARENT, (LPARAM)hLast);
  290. if (!hLast)
  291. return NULL;
  292. while (hLast && (hTry = (HTREEITEM)SendMessageW(hwndTV, TVM_GETNEXTITEM, TVGN_NEXT, (LPARAM)hLast)) == NULL) {
  293. hLast = (HTREEITEM)SendMessageW(hwndTV, TVM_GETNEXTITEM, TVGN_PARENT, (LPARAM)hLast);
  294. }
  295. if (match_item(hwndTV, hTry, sstring, mode, row))
  296. return hTry;
  297. hLast = hTry;
  298. }
  299. return NULL;
  300. }
  301. static BOOL RefreshTreeItem(HWND hwndTV, HTREEITEM hItem)
  302. {
  303. HKEY hRoot, hKey, hSubKey;
  304. HTREEITEM childItem;
  305. LPWSTR KeyPath;
  306. DWORD dwCount, dwIndex, dwMaxSubKeyLen;
  307. LPWSTR Name;
  308. TVITEMW tvItem;
  309. hRoot = NULL;
  310. KeyPath = GetItemPath(hwndTV, hItem, &hRoot);
  311. if (!KeyPath || !hRoot)
  312. return FALSE;
  313. if (*KeyPath) {
  314. if (RegOpenKeyExW(hRoot, KeyPath, 0, KEY_READ, &hKey) != ERROR_SUCCESS) {
  315. WINE_TRACE("RegOpenKeyEx failed, %s was probably removed.\n", wine_dbgstr_w(KeyPath));
  316. return FALSE;
  317. }
  318. } else {
  319. hKey = hRoot;
  320. }
  321. heap_free(KeyPath);
  322. if (RegQueryInfoKeyW(hKey, 0, 0, 0, &dwCount, &dwMaxSubKeyLen, 0, 0, 0, 0, 0, 0) != ERROR_SUCCESS) {
  323. return FALSE;
  324. }
  325. /* Set the number of children again */
  326. tvItem.mask = TVIF_CHILDREN;
  327. tvItem.hItem = hItem;
  328. tvItem.cChildren = dwCount;
  329. if (!TreeView_SetItemW(hwndTV, &tvItem)) {
  330. return FALSE;
  331. }
  332. /* We don't have to bother with the rest if it's not expanded. */
  333. if (SendMessageW(hwndTV, TVM_GETITEMSTATE, (WPARAM)hItem, TVIS_EXPANDED) == 0) {
  334. RegCloseKey(hKey);
  335. return TRUE;
  336. }
  337. dwMaxSubKeyLen++; /* account for the \0 terminator */
  338. Name = heap_xalloc(dwMaxSubKeyLen * sizeof(WCHAR));
  339. tvItem.cchTextMax = dwMaxSubKeyLen;
  340. tvItem.pszText = heap_xalloc(dwMaxSubKeyLen * sizeof(WCHAR));
  341. /* Now go through all the children in the registry, and check if any have to be added. */
  342. for (dwIndex = 0; dwIndex < dwCount; dwIndex++) {
  343. DWORD cName = dwMaxSubKeyLen, dwSubCount;
  344. BOOL found;
  345. found = FALSE;
  346. if (RegEnumKeyExW(hKey, dwIndex, Name, &cName, 0, 0, 0, NULL) != ERROR_SUCCESS) {
  347. continue;
  348. }
  349. /* Find the number of children of the node. */
  350. dwSubCount = 0;
  351. if (RegOpenKeyExW(hKey, Name, 0, KEY_QUERY_VALUE, &hSubKey) == ERROR_SUCCESS) {
  352. if (RegQueryInfoKeyW(hSubKey, 0, 0, 0, &dwSubCount, 0, 0, 0, 0, 0, 0, 0) != ERROR_SUCCESS) {
  353. dwSubCount = 0;
  354. }
  355. RegCloseKey(hSubKey);
  356. }
  357. /* Check if the node is already in there. */
  358. for (childItem = (HTREEITEM)SendMessageW(hwndTV, TVM_GETNEXTITEM, TVGN_CHILD, (LPARAM)hItem); childItem;
  359. childItem = (HTREEITEM)SendMessageW(hwndTV, TVM_GETNEXTITEM, TVGN_NEXT, (LPARAM)childItem)) {
  360. tvItem.mask = TVIF_TEXT;
  361. tvItem.hItem = childItem;
  362. if (!TreeView_GetItemW(hwndTV, &tvItem)) {
  363. heap_free(Name);
  364. heap_free(tvItem.pszText);
  365. return FALSE;
  366. }
  367. if (!lstrcmpiW(tvItem.pszText, Name)) {
  368. found = TRUE;
  369. break;
  370. }
  371. }
  372. if (found == FALSE) {
  373. WINE_TRACE("New subkey %s\n", wine_dbgstr_w(Name));
  374. AddEntryToTree(hwndTV, hItem, Name, NULL, dwSubCount);
  375. }
  376. }
  377. heap_free(Name);
  378. heap_free(tvItem.pszText);
  379. RegCloseKey(hKey);
  380. /* Now go through all the children in the tree, and check if any have to be removed. */
  381. childItem = (HTREEITEM)SendMessageW(hwndTV, TVM_GETNEXTITEM, TVGN_CHILD, (LPARAM)hItem);
  382. while (childItem) {
  383. HTREEITEM nextItem = (HTREEITEM)SendMessageW(hwndTV, TVM_GETNEXTITEM, TVGN_NEXT, (LPARAM)childItem);
  384. if (RefreshTreeItem(hwndTV, childItem) == FALSE) {
  385. SendMessageW(hwndTV, TVM_DELETEITEM, 0, (LPARAM)childItem);
  386. }
  387. childItem = nextItem;
  388. }
  389. return TRUE;
  390. }
  391. static void treeview_sort_item(HWND hWnd, HTREEITEM item)
  392. {
  393. HTREEITEM child = (HTREEITEM)SendMessageW(hWnd, TVM_GETNEXTITEM, TVGN_CHILD, (LPARAM)item);
  394. while (child != NULL) {
  395. treeview_sort_item(hWnd, child);
  396. child = (HTREEITEM)SendMessageW(hWnd, TVM_GETNEXTITEM, TVGN_NEXT, (LPARAM)child);
  397. }
  398. SendMessageW(hWnd, TVM_SORTCHILDREN, 0, (LPARAM)item);
  399. }
  400. BOOL RefreshTreeView(HWND hwndTV)
  401. {
  402. HTREEITEM hItem;
  403. HTREEITEM hSelectedItem;
  404. HCURSOR hcursorOld;
  405. HTREEITEM hRoot;
  406. WINE_TRACE("\n");
  407. hSelectedItem = (HTREEITEM)SendMessageW(hwndTV, TVM_GETNEXTITEM, TVGN_CARET, 0);
  408. hcursorOld = SetCursor(LoadCursorW(NULL, (LPCWSTR)IDC_WAIT));
  409. SendMessageW(hwndTV, WM_SETREDRAW, FALSE, 0);
  410. hRoot = (HTREEITEM)SendMessageW(hwndTV, TVM_GETNEXTITEM, TVGN_ROOT, 0);
  411. hItem = (HTREEITEM)SendMessageW(hwndTV, TVM_GETNEXTITEM, TVGN_CHILD, (LPARAM)hRoot);
  412. while (hItem) {
  413. RefreshTreeItem(hwndTV, hItem);
  414. treeview_sort_item(hwndTV, hItem);
  415. hItem = (HTREEITEM)SendMessageW(hwndTV, TVM_GETNEXTITEM, TVGN_NEXT, (LPARAM)hItem);
  416. }
  417. SendMessageW(hwndTV, WM_SETREDRAW, TRUE, 0);
  418. InvalidateRect(hwndTV, NULL, FALSE);
  419. SetCursor(hcursorOld);
  420. /* We reselect the currently selected node, this will prompt a refresh of the listview. */
  421. SendMessageW(hwndTV, TVM_SELECTITEM, TVGN_CARET, (LPARAM)hSelectedItem);
  422. return TRUE;
  423. }
  424. HTREEITEM InsertNode(HWND hwndTV, HTREEITEM hItem, LPWSTR name)
  425. {
  426. WCHAR buf[MAX_NEW_KEY_LEN];
  427. HTREEITEM hNewItem = 0;
  428. TVITEMEXW item;
  429. if (!hItem) hItem = (HTREEITEM)SendMessageW(hwndTV, TVM_GETNEXTITEM, TVGN_CARET, 0);
  430. if (!hItem) return FALSE;
  431. if (SendMessageW(hwndTV, TVM_GETITEMSTATE, (WPARAM)hItem, TVIS_EXPANDEDONCE) & TVIS_EXPANDEDONCE) {
  432. hNewItem = AddEntryToTree(hwndTV, hItem, name, 0, 0);
  433. } else {
  434. item.mask = TVIF_CHILDREN | TVIF_HANDLE;
  435. item.hItem = hItem;
  436. if (!TreeView_GetItemW(hwndTV, &item)) return FALSE;
  437. item.cChildren = 1;
  438. if (!TreeView_SetItemW(hwndTV, &item)) return FALSE;
  439. }
  440. SendMessageW(hwndTV, TVM_EXPAND, TVE_EXPAND, (LPARAM)hItem );
  441. if (!hNewItem) {
  442. for(hNewItem = (HTREEITEM)SendMessageW(hwndTV, TVM_GETNEXTITEM, TVGN_CHILD, (LPARAM)hItem); hNewItem;
  443. hNewItem = (HTREEITEM)SendMessageW(hwndTV, TVM_GETNEXTITEM, TVGN_NEXT, (LPARAM)hNewItem)) {
  444. item.mask = TVIF_HANDLE | TVIF_TEXT;
  445. item.hItem = hNewItem;
  446. item.pszText = buf;
  447. item.cchTextMax = ARRAY_SIZE(buf);
  448. if (!TreeView_GetItemW(hwndTV, &item)) continue;
  449. if (lstrcmpW(name, item.pszText) == 0) break;
  450. }
  451. }
  452. if (hNewItem)
  453. SendMessageW(hwndTV, TVM_SELECTITEM, TVGN_CARET, (LPARAM)hNewItem);
  454. return hNewItem;
  455. }
  456. HWND StartKeyRename(HWND hwndTV)
  457. {
  458. HTREEITEM hItem;
  459. if(!(hItem = (HTREEITEM)SendMessageW(hwndTV, TVM_GETNEXTITEM, TVGN_CARET, 0))) return 0;
  460. SetWindowLongPtrW(hwndTV, GWLP_USERDATA, 1);
  461. return (HWND)SendMessageW(hwndTV, TVM_EDITLABELW, 0, (LPARAM)hItem);
  462. }
  463. static BOOL InitTreeViewItems(HWND hwndTV, LPWSTR pHostName)
  464. {
  465. TVINSERTSTRUCTW tvins;
  466. HTREEITEM hRoot;
  467. static WCHAR hkcr[] = {'H','K','E','Y','_','C','L','A','S','S','E','S','_','R','O','O','T',0},
  468. hkcu[] = {'H','K','E','Y','_','C','U','R','R','E','N','T','_','U','S','E','R',0},
  469. hklm[] = {'H','K','E','Y','_','L','O','C','A','L','_','M','A','C','H','I','N','E',0},
  470. hku[] = {'H','K','E','Y','_','U','S','E','R','S',0},
  471. hkcc[] = {'H','K','E','Y','_','C','U','R','R','E','N','T','_','C','O','N','F','I','G',0},
  472. hkdd[] = {'H','K','E','Y','_','D','Y','N','_','D','A','T','A',0};
  473. tvins.u.item.mask = TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_CHILDREN | TVIF_PARAM;
  474. /* Set the text of the item. */
  475. tvins.u.item.pszText = pHostName;
  476. tvins.u.item.cchTextMax = lstrlenW(pHostName);
  477. /* Assume the item is not a parent item, so give it an image. */
  478. tvins.u.item.iImage = Image_Root;
  479. tvins.u.item.iSelectedImage = Image_Root;
  480. tvins.u.item.cChildren = 5;
  481. /* Save the heading level in the item's application-defined data area. */
  482. tvins.u.item.lParam = 0;
  483. tvins.hInsertAfter = TVI_FIRST;
  484. tvins.hParent = TVI_ROOT;
  485. /* Add the item to the tree view control. */
  486. if (!(hRoot = TreeView_InsertItemW(hwndTV, &tvins))) return FALSE;
  487. if (!AddEntryToTree(hwndTV, hRoot, hkcr, HKEY_CLASSES_ROOT, 1)) return FALSE;
  488. if (!AddEntryToTree(hwndTV, hRoot, hkcu, HKEY_CURRENT_USER, 1)) return FALSE;
  489. if (!AddEntryToTree(hwndTV, hRoot, hklm, HKEY_LOCAL_MACHINE, 1)) return FALSE;
  490. if (!AddEntryToTree(hwndTV, hRoot, hku, HKEY_USERS, 1)) return FALSE;
  491. if (!AddEntryToTree(hwndTV, hRoot, hkcc, HKEY_CURRENT_CONFIG, 1)) return FALSE;
  492. if (!AddEntryToTree(hwndTV, hRoot, hkdd, HKEY_DYN_DATA, 1)) return FALSE;
  493. return TRUE;
  494. }
  495. /*
  496. * InitTreeViewImageLists - creates an image list, adds three bitmaps
  497. * to it, and associates the image list with a tree view control.
  498. * Returns TRUE if successful, or FALSE otherwise.
  499. * hwndTV - handle to the tree view control.
  500. */
  501. static BOOL InitTreeViewImageLists(HWND hwndTV)
  502. {
  503. HIMAGELIST himl; /* handle to image list */
  504. HICON hico; /* handle to icon */
  505. INT cx = GetSystemMetrics(SM_CXSMICON);
  506. INT cy = GetSystemMetrics(SM_CYSMICON);
  507. /* Create the image list. */
  508. if ((himl = ImageList_Create(cx, cy, ILC_MASK, 0, NUM_ICONS)) == NULL)
  509. return FALSE;
  510. /* Add the open file, closed file, and document bitmaps. */
  511. hico = LoadIconW(hInst, MAKEINTRESOURCEW(IDI_OPEN_FILE));
  512. Image_Open = ImageList_AddIcon(himl, hico);
  513. hico = LoadIconW(hInst, MAKEINTRESOURCEW(IDI_CLOSED_FILE));
  514. Image_Closed = ImageList_AddIcon(himl, hico);
  515. hico = LoadIconW(hInst, MAKEINTRESOURCEW(IDI_ROOT));
  516. Image_Root = ImageList_AddIcon(himl, hico);
  517. /* Fail if not all of the images were added. */
  518. if (ImageList_GetImageCount(himl) < NUM_ICONS)
  519. {
  520. return FALSE;
  521. }
  522. /* Associate the image list with the tree view control. */
  523. SendMessageW(hwndTV, TVM_SETIMAGELIST, TVSIL_NORMAL, (LPARAM)himl);
  524. return TRUE;
  525. }
  526. BOOL UpdateExpandingTree(HWND hwndTV, HTREEITEM hItem, int state)
  527. {
  528. DWORD dwCount, dwIndex, dwMaxSubKeyLen;
  529. HKEY hRoot, hNewKey, hKey;
  530. LPWSTR keyPath;
  531. LPWSTR Name;
  532. LONG errCode;
  533. HCURSOR hcursorOld;
  534. TVITEMW item;
  535. static int expanding;
  536. if (expanding) return FALSE;
  537. if (state & TVIS_EXPANDEDONCE ) {
  538. return TRUE;
  539. }
  540. expanding = TRUE;
  541. hcursorOld = SetCursor(LoadCursorW(NULL, (LPCWSTR)IDC_WAIT));
  542. SendMessageW(hwndTV, WM_SETREDRAW, FALSE, 0);
  543. keyPath = GetItemPath(hwndTV, hItem, &hRoot);
  544. if (!keyPath) goto done;
  545. if (*keyPath) {
  546. errCode = RegOpenKeyExW(hRoot, keyPath, 0, KEY_READ, &hNewKey);
  547. if (errCode != ERROR_SUCCESS) goto done;
  548. } else {
  549. hNewKey = hRoot;
  550. }
  551. errCode = RegQueryInfoKeyW(hNewKey, 0, 0, 0, &dwCount, &dwMaxSubKeyLen, 0, 0, 0, 0, 0, 0);
  552. if (errCode != ERROR_SUCCESS) goto done;
  553. dwMaxSubKeyLen++; /* account for the \0 terminator */
  554. Name = heap_xalloc(dwMaxSubKeyLen * sizeof(WCHAR));
  555. for (dwIndex = 0; dwIndex < dwCount; dwIndex++) {
  556. DWORD cName = dwMaxSubKeyLen, dwSubCount;
  557. errCode = RegEnumKeyExW(hNewKey, dwIndex, Name, &cName, 0, 0, 0, 0);
  558. if (errCode != ERROR_SUCCESS) continue;
  559. errCode = RegOpenKeyExW(hNewKey, Name, 0, KEY_QUERY_VALUE, &hKey);
  560. if (errCode == ERROR_SUCCESS) {
  561. errCode = RegQueryInfoKeyW(hKey, 0, 0, 0, &dwSubCount, 0, 0, 0, 0, 0, 0, 0);
  562. RegCloseKey(hKey);
  563. }
  564. if (errCode != ERROR_SUCCESS) dwSubCount = 0;
  565. AddEntryToTree(hwndTV, hItem, Name, NULL, dwSubCount);
  566. }
  567. RegCloseKey(hNewKey);
  568. heap_free(Name);
  569. done:
  570. item.mask = TVIF_STATE;
  571. item.hItem = hItem;
  572. item.stateMask = TVIS_EXPANDEDONCE;
  573. item.state = TVIS_EXPANDEDONCE;
  574. SendMessageW(hwndTV, TVM_SETITEMW, 0, (LPARAM)&item);
  575. SendMessageW(hwndTV, WM_SETREDRAW, TRUE, 0);
  576. SetCursor(hcursorOld);
  577. expanding = FALSE;
  578. heap_free(keyPath);
  579. return TRUE;
  580. }
  581. BOOL OnTreeExpanding(HWND hwndTV, NMTREEVIEWW* pnmtv)
  582. {
  583. return UpdateExpandingTree(hwndTV, pnmtv->itemNew.hItem, pnmtv->itemNew.state);
  584. }
  585. /*
  586. * CreateTreeView - creates a tree view control.
  587. * Returns the handle to the new control if successful, or NULL otherwise.
  588. * hwndParent - handle to the control's parent window.
  589. */
  590. HWND CreateTreeView(HWND hwndParent, LPWSTR pHostName, UINT id)
  591. {
  592. RECT rcClient;
  593. HWND hwndTV;
  594. WCHAR TreeView[] = {'T','r','e','e',' ','V','i','e','w',0};
  595. /* Get the dimensions of the parent window's client area, and create the tree view control. */
  596. GetClientRect(hwndParent, &rcClient);
  597. hwndTV = CreateWindowExW(WS_EX_CLIENTEDGE, WC_TREEVIEWW, TreeView,
  598. WS_VISIBLE | WS_CHILD | WS_TABSTOP | TVS_HASLINES | TVS_HASBUTTONS |
  599. TVS_LINESATROOT | TVS_EDITLABELS | TVS_SHOWSELALWAYS,
  600. 0, 0, rcClient.right, rcClient.bottom,
  601. hwndParent, ULongToHandle(id), hInst, NULL);
  602. SendMessageW(hwndTV, TVM_SETUNICODEFORMAT, TRUE, 0);
  603. /* Initialize the image list, and add items to the control. */
  604. if (!InitTreeViewImageLists(hwndTV) || !InitTreeViewItems(hwndTV, pHostName)) {
  605. DestroyWindow(hwndTV);
  606. return NULL;
  607. }
  608. return hwndTV;
  609. }