Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1524 lines
32 KiB

  1. /*++
  2. Copyright (c) 1994-95 Microsoft Corporation
  3. Module Name:
  4. utils.cpp
  5. Abstract:
  6. Utilities.
  7. Author:
  8. Don Ryan (donryan) 04-Jan-1995
  9. Environment:
  10. User Mode - Win32
  11. Revision History:
  12. Jeff Parham (jeffparh) 16-Jan-1996
  13. o Removed multitudinous SetRedraw()'s and made a fix to TvGetDomain()
  14. so that LLSMGR no longer AVs when it performs refreshes wherein the
  15. overall number of domains diminishes and one of the now superfluous
  16. entries was expanded before the refresh.
  17. --*/
  18. #include "stdafx.h"
  19. #include "llsmgr.h"
  20. #include <strsafe.h>
  21. //
  22. // List view utilities
  23. //
  24. void LvInitColumns(CListCtrl* pListCtrl, PLV_COLUMN_INFO plvColumnInfo)
  25. /*++
  26. Routine Description:
  27. Initializes list view columns.
  28. Arguments:
  29. pListCtrl - list control.
  30. plvColumnInfo - column information.
  31. Return Values:
  32. None.
  33. --*/
  34. {
  35. ASSERT(plvColumnInfo);
  36. VALIDATE_OBJECT(pListCtrl, CListCtrl);
  37. int nStringId;
  38. CString strText;
  39. LV_COLUMN lvColumn;
  40. int nColumns = plvColumnInfo->nColumns;
  41. PLV_COLUMN_ENTRY plvColumnEntry = plvColumnInfo->lvColumnEntry;
  42. lvColumn.mask = LVCF_FMT|
  43. LVCF_TEXT|
  44. LVCF_SUBITEM;
  45. lvColumn.fmt = LVCFMT_LEFT;
  46. pListCtrl->SetRedraw(FALSE); // turn off drawing...
  47. while (nColumns--)
  48. {
  49. lvColumn.iSubItem = plvColumnEntry->iSubItem;
  50. nStringId = plvColumnEntry->nStringId;
  51. if (0 < nStringId)
  52. {
  53. strText.LoadString(nStringId);
  54. lvColumn.pszText = MKSTR(strText);
  55. }
  56. else
  57. lvColumn.pszText = _T("");
  58. pListCtrl->InsertColumn(lvColumn.iSubItem, &lvColumn);
  59. plvColumnEntry++;
  60. }
  61. pListCtrl->SetImageList(&theApp.m_smallImages, LVSIL_SMALL);
  62. pListCtrl->SetImageList(&theApp.m_largeImages, LVSIL_NORMAL);
  63. SetDefaultFont(pListCtrl);
  64. LvResizeColumns(pListCtrl, plvColumnInfo);
  65. pListCtrl->SetRedraw(TRUE); // turn on drawing...
  66. }
  67. void LvResizeColumns(CListCtrl* pListCtrl, PLV_COLUMN_INFO plvColumnInfo)
  68. /*++
  69. Routine Description:
  70. Resizes list view columns.
  71. Arguments:
  72. pListCtrl - list control.
  73. plvColumnInfo - column information.
  74. Return Values:
  75. None.
  76. --*/
  77. {
  78. ASSERT(plvColumnInfo);
  79. VALIDATE_OBJECT(pListCtrl, CListCtrl);
  80. int nColumnWidth;
  81. int nRelativeWidth;
  82. int nEntireWidthSoFar = 0;
  83. int nColumns = plvColumnInfo->nColumns;
  84. PLV_COLUMN_ENTRY plvColumnEntry = plvColumnInfo->lvColumnEntry;
  85. CRect clientRect;
  86. pListCtrl->GetClientRect(clientRect);
  87. pListCtrl->SetRedraw(FALSE); // turn off drawing...
  88. while ((nRelativeWidth = plvColumnEntry->nRelativeWidth) != -1)
  89. {
  90. nColumnWidth = (nRelativeWidth * clientRect.Width()) / 100;
  91. pListCtrl->SetColumnWidth(plvColumnEntry->iSubItem, nColumnWidth);
  92. nEntireWidthSoFar += nColumnWidth;
  93. plvColumnEntry++;
  94. }
  95. nColumnWidth = clientRect.Width() - nEntireWidthSoFar;
  96. pListCtrl->SetColumnWidth(plvColumnEntry->iSubItem, nColumnWidth);
  97. pListCtrl->SetRedraw(TRUE); // turn on drawing...
  98. }
  99. void LvChangeFormat(CListCtrl* pListCtrl, UINT nFormatId)
  100. /*++
  101. Routine Description:
  102. Changes window style of list view.
  103. Arguments:
  104. pListCtrl - list control.
  105. nFormatId - format specification.
  106. Return Values:
  107. None.
  108. --*/
  109. {
  110. VALIDATE_OBJECT(pListCtrl, CListCtrl);
  111. DWORD dwStyle = ::GetWindowLong(pListCtrl->GetSafeHwnd(), GWL_STYLE);
  112. pListCtrl->BeginWaitCursor();
  113. pListCtrl->SetRedraw(FALSE); // turn off drawing...
  114. if ((dwStyle & LVS_TYPEMASK) != nFormatId)
  115. {
  116. ::SetWindowLong(
  117. pListCtrl->GetSafeHwnd(),
  118. GWL_STYLE,
  119. (dwStyle & ~LVS_TYPEMASK) | nFormatId
  120. );
  121. }
  122. pListCtrl->SetRedraw(TRUE); // turn on drawing...
  123. pListCtrl->EndWaitCursor();
  124. }
  125. LPVOID LvGetSelObj(CListCtrl* pListCtrl)
  126. /*++
  127. Routine Description:
  128. Retrieves the object selected (assumes one) from list view.
  129. Arguments:
  130. pListCtrl - list control.
  131. Return Values:
  132. Same as LvGetNextObj.
  133. --*/
  134. {
  135. int iItem = -1;
  136. return LvGetNextObj(pListCtrl, &iItem);
  137. }
  138. LPVOID LvGetNextObj(CListCtrl* pListCtrl, LPINT piItem, int nType)
  139. /*++
  140. Routine Description:
  141. Retrieves the next object selected from list view.
  142. Arguments:
  143. pListCtrl - list control.
  144. piItem - starting index (updated).
  145. nType - specifies search criteria.
  146. Return Values:
  147. Returns object pointer or null.
  148. --*/
  149. {
  150. ASSERT(piItem);
  151. VALIDATE_OBJECT(pListCtrl, CListCtrl);
  152. LV_ITEM lvItem;
  153. if ((lvItem.iItem = pListCtrl->GetNextItem(*piItem, nType)) != -1)
  154. {
  155. lvItem.mask = LVIF_PARAM;
  156. lvItem.iSubItem = 0;
  157. if (pListCtrl->GetItem(&lvItem))
  158. {
  159. *piItem = lvItem.iItem;
  160. return (LPVOID)lvItem.lParam;
  161. }
  162. }
  163. return NULL;
  164. }
  165. BOOL LvInsertObArray(CListCtrl* pListCtrl, PLV_COLUMN_INFO plvColumnInfo, CObArray* pObArray)
  166. /*++
  167. Routine Description:
  168. Insert object array into list view.
  169. Note list view must be unsorted and support LVN_GETDISPINFO.
  170. Arguments:
  171. pListCtrl - list control.
  172. plvColumnInfo - column info.
  173. pObArray - object array.
  174. Return Values:
  175. VT_BOOL.
  176. --*/
  177. {
  178. VALIDATE_OBJECT(pObArray, CObArray);
  179. VALIDATE_OBJECT(pListCtrl, CListCtrl);
  180. ASSERT(plvColumnInfo);
  181. ASSERT(pListCtrl->GetItemCount() == 0);
  182. BOOL bItemsInserted = FALSE;
  183. LV_ITEM lvItem;
  184. lvItem.mask = LVIF_TEXT|
  185. LVIF_PARAM|
  186. LVIF_IMAGE;
  187. lvItem.pszText = LPSTR_TEXTCALLBACK;
  188. lvItem.cchTextMax = LPSTR_TEXTCALLBACK_MAX;
  189. lvItem.iImage = I_IMAGECALLBACK;
  190. lvItem.iSubItem = 0;
  191. int iItem;
  192. int iSubItem;
  193. int nItems = (int)pObArray->GetSize();
  194. ASSERT(nItems != -1); // iItem is -1 if error...
  195. pListCtrl->SetRedraw(FALSE); // turn off drawing...
  196. pListCtrl->SetItemCount(nItems);
  197. CCmdTarget* pObject = NULL;
  198. for (iItem = 0; (iItem < nItems) && (NULL != (pObject = (CCmdTarget*)pObArray->GetAt(iItem))); iItem++)
  199. {
  200. VALIDATE_OBJECT(pObject, CCmdTarget);
  201. lvItem.iItem = iItem;
  202. lvItem.lParam = (LPARAM)(LPVOID)pObject;
  203. pObject->InternalAddRef(); // add ref...
  204. iItem = pListCtrl->InsertItem(&lvItem);
  205. ASSERT((iItem == lvItem.iItem) || (iItem == -1));
  206. for (iSubItem = 1; iSubItem < plvColumnInfo->nColumns; iSubItem++)
  207. {
  208. pListCtrl->SetItemText(iItem, iSubItem, LPSTR_TEXTCALLBACK);
  209. }
  210. }
  211. if (iItem == nItems)
  212. {
  213. bItemsInserted = TRUE;
  214. VERIFY(pListCtrl->SetItemState(
  215. 0,
  216. LVIS_FOCUSED|
  217. LVIS_SELECTED,
  218. LVIS_FOCUSED|
  219. LVIS_SELECTED
  220. ));
  221. }
  222. else
  223. {
  224. LlsSetLastStatus(STATUS_NO_MEMORY);
  225. VERIFY(pListCtrl->DeleteAllItems());
  226. }
  227. LvResizeColumns(pListCtrl, plvColumnInfo);
  228. pListCtrl->Invalidate(TRUE);
  229. pListCtrl->SetRedraw(TRUE); // turn on drawing...
  230. return bItemsInserted;
  231. }
  232. BOOL
  233. LvRefreshObArray(
  234. CListCtrl* pListCtrl,
  235. PLV_COLUMN_INFO plvColumnInfo,
  236. CObArray* pObArray
  237. )
  238. /*++
  239. Routine Description:
  240. Refresh object array in list view.
  241. Arguments:
  242. pListCtrl - list control.
  243. plvColumnInfo - column info.
  244. pObArray - object array.
  245. Return Values:
  246. VT_BOOL.
  247. --*/
  248. {
  249. ASSERT(plvColumnInfo);
  250. VALIDATE_OBJECT(pObArray, CObArray);
  251. VALIDATE_OBJECT(pListCtrl, CListCtrl);
  252. LONG_PTR nObjects = pObArray->GetSize();
  253. long nObjectsInList = pListCtrl->GetItemCount();
  254. if (!nObjects)
  255. {
  256. LvReleaseObArray(pListCtrl);
  257. return TRUE;
  258. }
  259. else if (!nObjectsInList)
  260. {
  261. return LvInsertObArray(
  262. pListCtrl,
  263. plvColumnInfo,
  264. pObArray
  265. );
  266. }
  267. CCmdTarget* pObject;
  268. int iObject = 0;
  269. int iObjectInList = 0;
  270. LV_ITEM lvItem;
  271. pListCtrl->SetRedraw(FALSE); // turn off drawing...
  272. while (nObjectsInList--)
  273. {
  274. lvItem.mask = LVIF_PARAM;
  275. lvItem.iItem = iObjectInList;
  276. lvItem.iSubItem = 0;
  277. VERIFY(pListCtrl->GetItem(&lvItem));
  278. pObject = (CCmdTarget*)lvItem.lParam;
  279. VALIDATE_OBJECT(pObject, CCmdTarget);
  280. if (iObject < nObjects)
  281. {
  282. pObject->InternalRelease(); // release before...
  283. pObject = (CCmdTarget*)pObArray->GetAt(iObject++);
  284. VALIDATE_OBJECT(pObject, CCmdTarget);
  285. pObject->InternalAddRef(); // add ref...
  286. lvItem.mask = LVIF_TEXT|LVIF_PARAM;
  287. lvItem.pszText = LPSTR_TEXTCALLBACK;
  288. lvItem.cchTextMax = LPSTR_TEXTCALLBACK_MAX;
  289. lvItem.lParam = (LPARAM)(LPVOID)pObject;
  290. VERIFY(pListCtrl->SetItem(&lvItem)); // overwrite...
  291. iObjectInList++; // increment count...
  292. }
  293. else
  294. {
  295. VERIFY(pListCtrl->DeleteItem(iObjectInList));
  296. pObject->InternalRelease(); // release after...
  297. }
  298. }
  299. lvItem.mask = LVIF_TEXT|
  300. LVIF_PARAM|
  301. LVIF_IMAGE;
  302. lvItem.pszText = LPSTR_TEXTCALLBACK;
  303. lvItem.cchTextMax = LPSTR_TEXTCALLBACK_MAX;
  304. lvItem.iImage = I_IMAGECALLBACK;
  305. lvItem.iSubItem = 0;
  306. int iItem;
  307. int iSubItem;
  308. while (iObject < nObjects)
  309. {
  310. lvItem.iItem = iObject;
  311. pObject = (CCmdTarget*)pObArray->GetAt(iObject++);
  312. VALIDATE_OBJECT(pObject, CCmdTarget);
  313. pObject->InternalAddRef(); // add ref...
  314. lvItem.lParam = (LPARAM)(LPVOID)pObject;
  315. iItem = pListCtrl->InsertItem(&lvItem);
  316. ASSERT((iItem == lvItem.iItem) && (iItem != -1));
  317. for (iSubItem = 1; iSubItem < plvColumnInfo->nColumns; iSubItem++)
  318. {
  319. VERIFY(pListCtrl->SetItemText(iItem, iSubItem, LPSTR_TEXTCALLBACK));
  320. }
  321. }
  322. LvResizeColumns(pListCtrl, plvColumnInfo);
  323. pListCtrl->Invalidate(TRUE);
  324. pListCtrl->SetRedraw(TRUE); // turn on drawing...
  325. return TRUE;
  326. }
  327. void LvReleaseObArray(CListCtrl* pListCtrl)
  328. /*++
  329. Routine Description:
  330. Release objects inserted into list view.
  331. Arguments:
  332. pListCtrl - list control.
  333. Return Values:
  334. None.
  335. --*/
  336. {
  337. VALIDATE_OBJECT(pListCtrl, CListCtrl);
  338. LV_ITEM lvItem;
  339. CCmdTarget* pObject;
  340. lvItem.mask = LVIF_PARAM;
  341. lvItem.iItem = 0;
  342. lvItem.iSubItem = 0;
  343. int nObjectsInList = pListCtrl->GetItemCount();
  344. pListCtrl->BeginWaitCursor();
  345. pListCtrl->SetRedraw(FALSE); // turn off drawing...
  346. while (nObjectsInList--)
  347. {
  348. VERIFY(pListCtrl->GetItem(&lvItem));
  349. pObject = (CCmdTarget*)lvItem.lParam;
  350. VALIDATE_OBJECT(pObject, CCmdTarget);
  351. VERIFY(pListCtrl->DeleteItem(lvItem.iItem));
  352. pObject->InternalRelease(); // release after...
  353. }
  354. pListCtrl->SetRedraw(TRUE); // turn on drawing...
  355. pListCtrl->EndWaitCursor();
  356. }
  357. void LvReleaseSelObjs(CListCtrl* pListCtrl)
  358. /*++
  359. Routine Description:
  360. Release selected objects in list view.
  361. Arguments:
  362. pListCtrl - list control.
  363. Return Values:
  364. None.
  365. --*/
  366. {
  367. VALIDATE_OBJECT(pListCtrl, CListCtrl);
  368. int iItem = -1;
  369. CCmdTarget* pObject;
  370. pListCtrl->SetRedraw(FALSE); // turn off drawing...
  371. while (NULL != (pObject = (CCmdTarget*)::LvGetNextObj(pListCtrl, &iItem)))
  372. {
  373. pObject->InternalRelease();
  374. pListCtrl->DeleteItem(iItem);
  375. iItem = -1;
  376. }
  377. LvSelObjIfNecessary(pListCtrl);
  378. pListCtrl->SetRedraw(TRUE); // turn on drawing...
  379. }
  380. void LvSelObjIfNecessary(CListCtrl* pListCtrl, BOOL bSetFocus)
  381. /*++
  382. Routine Description:
  383. Ensure that object selected.
  384. Arguments:
  385. pListCtrl - list control.
  386. bSetFocus - true if focus to be set focus as well.
  387. Return Values:
  388. None.
  389. --*/
  390. {
  391. VALIDATE_OBJECT(pListCtrl, CListCtrl);
  392. if (!IsItemSelectedInList(pListCtrl) && pListCtrl->GetItemCount())
  393. {
  394. pListCtrl->SendMessage(WM_KEYDOWN, VK_RIGHT); // HACKHACK...
  395. int iItem = pListCtrl->GetNextItem(-1, LVNI_FOCUSED|LVNI_ALL);
  396. int nState = bSetFocus ? (LVIS_SELECTED|LVIS_FOCUSED) : LVIS_SELECTED;
  397. VERIFY(pListCtrl->SetItemState((iItem == -1) ? 0 : iItem, nState, nState));
  398. }
  399. }
  400. #ifdef _DEBUG
  401. void LvDumpObArray(CListCtrl* pListCtrl)
  402. /*++
  403. Routine Description:
  404. Release objects inserted into list view.
  405. Arguments:
  406. pListCtrl - list control.
  407. Return Values:
  408. None.
  409. --*/
  410. {
  411. VALIDATE_OBJECT(pListCtrl, CListCtrl);
  412. LV_ITEM lvItem;
  413. CString strDump;
  414. CCmdTarget* pObject;
  415. lvItem.mask = LVIF_STATE|LVIF_PARAM;
  416. lvItem.stateMask = (DWORD)-1;
  417. lvItem.iSubItem = 0;
  418. int nObjectsInList = pListCtrl->GetItemCount();
  419. pListCtrl->SetRedraw(FALSE); // turn off drawing...
  420. while (nObjectsInList--)
  421. {
  422. lvItem.iItem = nObjectsInList;
  423. VERIFY(pListCtrl->GetItem(&lvItem));
  424. pObject = (CCmdTarget*)lvItem.lParam;
  425. VALIDATE_OBJECT(pObject, CCmdTarget);
  426. strDump.Format(_T("iItem %d"), lvItem.iItem);
  427. strDump += (lvItem.state & LVIS_CUT) ? _T(" LVIS_CUT ") : _T("");
  428. strDump += (lvItem.state & LVIS_FOCUSED) ? _T(" LVIS_FOCUSED ") : _T("");
  429. strDump += (lvItem.state & LVIS_SELECTED) ? _T(" LVIS_SELECTED ") : _T("");
  430. strDump += _T("\r\n");
  431. afxDump << strDump;
  432. }
  433. pListCtrl->SetRedraw(TRUE); // turn on drawing...
  434. }
  435. #endif
  436. //
  437. // Tree view utilities
  438. //
  439. LPVOID TvGetSelObj(CTreeCtrl* pTreeCtrl)
  440. /*++
  441. Routine Description:
  442. Retrieves the object selected from treeview.
  443. Arguments:
  444. pTreeCtrl - tree control.
  445. Return Values:
  446. Returns object pointer or null.
  447. --*/
  448. {
  449. VALIDATE_OBJECT(pTreeCtrl, CTreeCtrl);
  450. TV_ITEM tvItem;
  451. tvItem.hItem = pTreeCtrl->GetSelectedItem();
  452. if (NULL != tvItem.hItem)
  453. {
  454. tvItem.mask = TVIF_PARAM;
  455. VERIFY(pTreeCtrl->GetItem(&tvItem));
  456. return (LPVOID)tvItem.lParam;
  457. }
  458. return NULL;
  459. }
  460. BOOL
  461. TvInsertObArray(
  462. CTreeCtrl* pTreeCtrl,
  463. HTREEITEM hParent,
  464. CObArray* pObArray,
  465. BOOL bIsContainer
  466. )
  467. /*++
  468. Routine Description:
  469. Insert collection into tree view.
  470. Arguments:
  471. pTreeCtrl - tree control.
  472. hParent - parent tree item.
  473. pObArray - object array.
  474. bIsContainer - container object.
  475. Return Values:
  476. VT_BOOL.
  477. --*/
  478. {
  479. VALIDATE_OBJECT(pObArray, CObArray);
  480. VALIDATE_OBJECT(pTreeCtrl, CTreeCtrl);
  481. BOOL bItemsInserted = FALSE;
  482. TV_ITEM tvItem;
  483. TV_INSERTSTRUCT tvInsert;
  484. tvItem.mask = TVIF_TEXT|
  485. TVIF_PARAM|
  486. TVIF_IMAGE|
  487. TVIF_CHILDREN|
  488. TVIF_SELECTEDIMAGE;
  489. tvItem.pszText = LPSTR_TEXTCALLBACK;
  490. tvItem.cchTextMax = LPSTR_TEXTCALLBACK_MAX;
  491. tvItem.iImage = I_IMAGECALLBACK;
  492. tvItem.iSelectedImage = I_IMAGECALLBACK;
  493. tvItem.cChildren = bIsContainer;
  494. tvInsert.hInsertAfter = (HTREEITEM)TVI_LAST;
  495. tvInsert.hParent = (HTREEITEM)hParent;
  496. int iItem;
  497. INT_PTR nItems = pObArray->GetSize();
  498. HTREEITEM hNewItem = (HTREEITEM)-1; // init for loop...
  499. pTreeCtrl->SetRedraw(FALSE); // turn off drawing...
  500. CCmdTarget* pObject = NULL;
  501. for (iItem = 0; hNewItem && (iItem < nItems); iItem++)
  502. {
  503. pObject = (CCmdTarget*)pObArray->GetAt(iItem);
  504. VALIDATE_OBJECT(pObject, CCmdTarget);
  505. pObject->InternalAddRef(); // add ref...
  506. tvItem.lParam = (LPARAM)(LPVOID)pObject;
  507. tvInsert.item = tvItem;
  508. hNewItem = pTreeCtrl->InsertItem(&tvInsert);
  509. }
  510. if (hNewItem && (iItem == nItems))
  511. {
  512. bItemsInserted = TRUE;
  513. }
  514. else
  515. {
  516. LlsSetLastStatus(STATUS_NO_MEMORY);
  517. }
  518. pTreeCtrl->SetRedraw(TRUE); // turn on drawing...
  519. return bItemsInserted;
  520. }
  521. BOOL
  522. TvRefreshObArray(
  523. CTreeCtrl* pTreeCtrl,
  524. HTREEITEM hParent,
  525. CObArray* pObArray,
  526. TV_EXPANDED_INFO* pExpandedInfo,
  527. BOOL bIsContainer
  528. )
  529. /*++
  530. Routine Description:
  531. Refresh objects in tree view.
  532. Arguments:
  533. pTreeCtrl - tree control.
  534. hParent - parent tree item.
  535. pObArray - object array.
  536. pExpandedInfo - refresh information.
  537. bIsContainer - container object.
  538. Return Values:
  539. Returns true if successful.
  540. --*/
  541. {
  542. ASSERT(pExpandedInfo);
  543. VALIDATE_OBJECT(pObArray, CObArray);
  544. VALIDATE_OBJECT(pTreeCtrl, CTreeCtrl);
  545. pExpandedInfo->nExpandedItems = 0; // initialize...
  546. pExpandedInfo->pExpandedItems = NULL;
  547. LONG_PTR nObjects = pObArray->GetSize();
  548. long nObjectsInTree = TvSizeObArray(pTreeCtrl, hParent);
  549. if (!nObjects) // tree no longer exists...
  550. {
  551. TvReleaseObArray(pTreeCtrl, hParent);
  552. return TRUE;
  553. }
  554. else if (!nObjectsInTree) // tree is currently empty...
  555. {
  556. return TvInsertObArray(
  557. pTreeCtrl,
  558. hParent,
  559. pObArray,
  560. bIsContainer
  561. );
  562. }
  563. TV_EXPANDED_ITEM* pExpandedItem = new TV_EXPANDED_ITEM[nObjectsInTree];
  564. if (!pExpandedItem)
  565. {
  566. LlsSetLastStatus(STATUS_NO_MEMORY);
  567. return FALSE;
  568. }
  569. pExpandedInfo->pExpandedItems = pExpandedItem;
  570. TV_ITEM tvItem;
  571. HTREEITEM hItem;
  572. BOOL bIsItemExpanded;
  573. CCmdTarget* pObject;
  574. int iObject = 0;
  575. hItem = pTreeCtrl->GetChildItem(hParent);
  576. while (hItem)
  577. {
  578. tvItem.hItem = hItem;
  579. tvItem.mask = TVIF_STATE|TVIF_PARAM;
  580. VERIFY(pTreeCtrl->GetItem(&tvItem));
  581. pObject = (CCmdTarget*)tvItem.lParam;
  582. VALIDATE_OBJECT(pObject, CCmdTarget);
  583. bIsItemExpanded = tvItem.state & TVIS_EXPANDEDONCE; //this bit and oper to bool, it seems very strange
  584. if (FALSE == bIsItemExpanded)
  585. {
  586. pObject->InternalRelease(); // release now...
  587. }
  588. else
  589. {
  590. pExpandedItem->hItem = tvItem.hItem;
  591. pExpandedItem->pObject = pObject; // don't release yet...
  592. pExpandedItem++;
  593. pExpandedInfo->nExpandedItems++;
  594. ASSERT(pExpandedInfo->nExpandedItems <= nObjectsInTree);
  595. }
  596. hItem = pTreeCtrl->GetNextSiblingItem(tvItem.hItem);
  597. if (iObject < nObjects)
  598. {
  599. pObject = (CCmdTarget*)pObArray->GetAt(iObject++);
  600. VALIDATE_OBJECT(pObject, CCmdTarget);
  601. pObject->InternalAddRef(); // add ref...
  602. tvItem.mask = TVIF_PARAM;
  603. tvItem.lParam = (LPARAM)(LPVOID)pObject;
  604. VERIFY(pTreeCtrl->SetItem(&tvItem)); // overwrite...
  605. }
  606. else if (bIsItemExpanded)
  607. {
  608. tvItem.mask = TVIF_PARAM;
  609. tvItem.lParam = (LPARAM)(LPVOID)NULL; // place holder...
  610. VERIFY(pTreeCtrl->SetItem(&tvItem)); // remove later...
  611. }
  612. else
  613. {
  614. VERIFY(pTreeCtrl->DeleteItem(tvItem.hItem)); // trim excess...
  615. }
  616. }
  617. if (iObject < nObjects)
  618. {
  619. TV_INSERTSTRUCT tvInsert;
  620. tvItem.mask = TVIF_TEXT|
  621. TVIF_PARAM|
  622. TVIF_IMAGE|
  623. TVIF_CHILDREN|
  624. TVIF_SELECTEDIMAGE;
  625. tvItem.pszText = LPSTR_TEXTCALLBACK;
  626. tvItem.cchTextMax = LPSTR_TEXTCALLBACK_MAX;
  627. tvItem.iImage = I_IMAGECALLBACK;
  628. tvItem.iSelectedImage = I_IMAGECALLBACK;
  629. tvItem.cChildren = bIsContainer;
  630. tvInsert.hInsertAfter = (HTREEITEM)TVI_LAST;
  631. tvInsert.hParent = (HTREEITEM)hParent;
  632. hItem = (HTREEITEM)-1; // init for loop...
  633. for (; hItem && (iObject < nObjects); iObject++)
  634. {
  635. pObject = (CCmdTarget*)pObArray->GetAt(iObject);
  636. VALIDATE_OBJECT(pObject, CCmdTarget);
  637. pObject->InternalAddRef(); // AddRef each...
  638. tvItem.lParam = (LPARAM)(LPVOID)pObject;
  639. tvInsert.item = tvItem;
  640. hItem = pTreeCtrl->InsertItem(&tvInsert);
  641. }
  642. if (!(hItem && (iObject == nObjects)))
  643. {
  644. pExpandedItem = pExpandedInfo->pExpandedItems;
  645. while (pExpandedInfo->nExpandedItems--)
  646. (pExpandedItem++)->pObject->InternalRelease();
  647. delete [] pExpandedInfo->pExpandedItems;
  648. pExpandedInfo->pExpandedItems = NULL;
  649. LlsSetLastStatus(STATUS_NO_MEMORY);
  650. return FALSE;
  651. }
  652. }
  653. if (!pExpandedInfo->nExpandedItems)
  654. {
  655. delete [] pExpandedInfo->pExpandedItems;
  656. pExpandedInfo->pExpandedItems = NULL;
  657. }
  658. return TRUE;
  659. }
  660. void TvReleaseObArray(CTreeCtrl* pTreeCtrl, HTREEITEM hParent)
  661. /*++
  662. Routine Description:
  663. Release objects inserted into tree view.
  664. Arguments:
  665. pTreeCtrl - tree control.
  666. hParent - parent tree item.
  667. Return Values:
  668. None.
  669. --*/
  670. {
  671. if (!hParent)
  672. return; // nothing to release...
  673. VALIDATE_OBJECT(pTreeCtrl, CTreeCtrl);
  674. TV_ITEM tvItem;
  675. HTREEITEM hItem;
  676. CCmdTarget* pObject;
  677. tvItem.mask = TVIF_STATE|TVIF_PARAM;
  678. hItem = pTreeCtrl->GetChildItem(hParent);
  679. while (hItem)
  680. {
  681. tvItem.hItem = hItem;
  682. VERIFY(pTreeCtrl->GetItem(&tvItem));
  683. if (tvItem.state & TVIS_EXPANDEDONCE)
  684. {
  685. TvReleaseObArray(pTreeCtrl, tvItem.hItem);
  686. }
  687. hItem = pTreeCtrl->GetNextSiblingItem(tvItem.hItem);
  688. pObject = (CCmdTarget*)tvItem.lParam;
  689. VALIDATE_OBJECT(pObject, CCmdTarget);
  690. pObject->InternalRelease(); // release now...
  691. pTreeCtrl->DeleteItem(tvItem.hItem);
  692. }
  693. tvItem.hItem = hParent;
  694. tvItem.mask = TVIF_STATE|TVIF_PARAM;
  695. VERIFY(pTreeCtrl->GetItem(&tvItem));
  696. if (!tvItem.lParam)
  697. {
  698. pTreeCtrl->DeleteItem(hParent); // delete placeholder...
  699. }
  700. else if (tvItem.state & TVIS_EXPANDEDONCE)
  701. {
  702. tvItem.state = 0;
  703. tvItem.stateMask = TVIS_EXPANDED|TVIS_EXPANDEDONCE;
  704. VERIFY(pTreeCtrl->SetItem(&tvItem)); // no longer expanded...
  705. }
  706. }
  707. long TvSizeObArray(CTreeCtrl* pTreeCtrl, HTREEITEM hParent)
  708. /*++
  709. Routine Description:
  710. Count objects in tree view.
  711. Arguments:
  712. pTreeCtrl - tree control.
  713. hParent - parent tree item.
  714. Return Values:
  715. None.
  716. --*/
  717. {
  718. VALIDATE_OBJECT(pTreeCtrl, CTreeCtrl);
  719. long nObjects = 0;
  720. HTREEITEM hItem = pTreeCtrl->GetChildItem(hParent);
  721. while (hItem)
  722. {
  723. nObjects++;
  724. hItem = pTreeCtrl->GetNextSiblingItem(hItem);
  725. }
  726. return nObjects;
  727. }
  728. void
  729. TvSwitchItem(
  730. CTreeCtrl* pTreeCtrl,
  731. HTREEITEM hRandomItem,
  732. TV_EXPANDED_ITEM* pExpandedItem
  733. )
  734. /*++
  735. Routine Description:
  736. Move object from random node to previously expanded node. If there
  737. is an object in the previously expanded node we move it to the random
  738. node to be sorted later.
  739. Arguments:
  740. pTreeCtrl - tree control.
  741. hRandomItem - handle to random node with object of interest.
  742. pExpandedItem - state information.
  743. Return Values:
  744. None.
  745. --*/
  746. {
  747. ASSERT(pExpandedItem);
  748. VALIDATE_OBJECT(pTreeCtrl, CTreeCtrl);
  749. CCmdTarget* pRandomObject;
  750. CCmdTarget* pExpandedObject;
  751. TV_ITEM tvRandomItem;
  752. TV_ITEM tvExpandedItem;
  753. if (hRandomItem != pExpandedItem->hItem)
  754. {
  755. tvRandomItem.hItem = hRandomItem;
  756. tvRandomItem.mask = LVIF_PARAM;
  757. tvExpandedItem.hItem = pExpandedItem->hItem;
  758. tvExpandedItem.mask = LVIF_PARAM;
  759. VERIFY(pTreeCtrl->GetItem(&tvRandomItem));
  760. pExpandedObject = (CCmdTarget*)tvRandomItem.lParam;
  761. VALIDATE_OBJECT(pExpandedObject, CCmdTarget);
  762. VERIFY(pTreeCtrl->GetItem(&tvExpandedItem));
  763. pRandomObject = (CCmdTarget*)tvExpandedItem.lParam; // could be null...
  764. if (pRandomObject)
  765. {
  766. VALIDATE_OBJECT(pRandomObject, CCmdTarget);
  767. tvRandomItem.lParam = (LPARAM)(LPVOID)pRandomObject;
  768. VERIFY(pTreeCtrl->SetItem(&tvRandomItem)); // switch position...
  769. }
  770. else
  771. {
  772. VERIFY(pTreeCtrl->DeleteItem(tvRandomItem.hItem)); // delete placeholder...
  773. }
  774. tvExpandedItem.lParam = (LPARAM)(LPVOID)pExpandedObject;
  775. VERIFY(pTreeCtrl->SetItem(&tvExpandedItem));
  776. }
  777. }
  778. HTREEITEM TvGetDomain(CTreeCtrl* pTreeCtrl, HTREEITEM hParent, CCmdTarget* pObject)
  779. /*++
  780. Routine Description:
  781. Find domain in tree view.
  782. Arguments:
  783. pTreeCtrl - tree control.
  784. hParent - parent tree item.
  785. pObject - object to find.
  786. Return Values:
  787. Handle of located object.
  788. --*/
  789. {
  790. VALIDATE_OBJECT(pObject, CDomain);
  791. VALIDATE_OBJECT(pTreeCtrl, CTreeCtrl);
  792. CDomain* pDomain;
  793. TV_ITEM tvItem;
  794. tvItem.mask = TVIF_PARAM;
  795. tvItem.hItem = pTreeCtrl->GetChildItem(hParent);
  796. while (tvItem.hItem)
  797. {
  798. VERIFY(pTreeCtrl->GetItem(&tvItem));
  799. pDomain = (CDomain*)tvItem.lParam; // can be NULL if placeholder...
  800. if ( NULL != pDomain )
  801. {
  802. VALIDATE_OBJECT(pDomain, CDomain);
  803. if (!((CDomain*)pObject)->m_strName.CompareNoCase(pDomain->m_strName))
  804. {
  805. return tvItem.hItem; // found it...
  806. }
  807. }
  808. tvItem.hItem = pTreeCtrl->GetNextSiblingItem(tvItem.hItem);
  809. }
  810. return NULL;
  811. }
  812. HTREEITEM TvGetServer(CTreeCtrl* pTreeCtrl, HTREEITEM hParent, CCmdTarget* pObject)
  813. /*++
  814. Routine Description:
  815. Find server in tree view.
  816. Arguments:
  817. pTreeCtrl - tree control.
  818. hParent - parent tree item.
  819. pObject - object to find.
  820. Return Values:
  821. Handle of located object.
  822. --*/
  823. {
  824. VALIDATE_OBJECT(pObject, CServer);
  825. VALIDATE_OBJECT(pTreeCtrl, CTreeCtrl);
  826. CServer* pServer;
  827. TV_ITEM tvItem;
  828. tvItem.mask = TVIF_PARAM;
  829. tvItem.hItem = pTreeCtrl->GetChildItem(hParent);
  830. while (tvItem.hItem)
  831. {
  832. VERIFY(pTreeCtrl->GetItem(&tvItem));
  833. pServer = (CServer*)tvItem.lParam;
  834. VALIDATE_OBJECT(pServer, CServer);
  835. if (!((CServer*)pObject)->m_strName.CompareNoCase(pServer->m_strName))
  836. {
  837. return tvItem.hItem; // found it...
  838. }
  839. tvItem.hItem = pTreeCtrl->GetNextSiblingItem(tvItem.hItem);
  840. }
  841. return NULL;
  842. }
  843. HTREEITEM TvGetService(CTreeCtrl* pTreeCtrl, HTREEITEM hParent, CCmdTarget* pObject)
  844. /*++
  845. Routine Description:
  846. Find service in tree view.
  847. Arguments:
  848. pTreeCtrl - tree control.
  849. hParent - parent tree item.
  850. pObject - object to find.
  851. Return Values:
  852. Handle of located object.
  853. --*/
  854. {
  855. VALIDATE_OBJECT(pObject, CService);
  856. VALIDATE_OBJECT(pTreeCtrl, CTreeCtrl);
  857. CService* pService;
  858. TV_ITEM tvItem;
  859. tvItem.mask = TVIF_PARAM;
  860. tvItem.hItem = pTreeCtrl->GetChildItem(hParent);
  861. while (tvItem.hItem)
  862. {
  863. VERIFY(pTreeCtrl->GetItem(&tvItem));
  864. pService = (CService*)tvItem.lParam;
  865. VALIDATE_OBJECT(pService, CService);
  866. if (!((CService*)pObject)->m_strName.CompareNoCase(pService->m_strName))
  867. {
  868. return tvItem.hItem; // found it...
  869. }
  870. tvItem.hItem = pTreeCtrl->GetNextSiblingItem(tvItem.hItem);
  871. }
  872. return NULL;
  873. }
  874. //
  875. // Tab control utilities
  876. //
  877. void TcInitTabs(CTabCtrl* pTabCtrl, PTC_TAB_INFO ptcTabInfo)
  878. /*++
  879. Routine Description:
  880. Initializes tab items.
  881. Arguments:
  882. pTabCtrl - tab control.
  883. ptcTabInfo - tab information.
  884. Return Values:
  885. None.
  886. --*/
  887. {
  888. ASSERT(ptcTabInfo);
  889. VALIDATE_OBJECT(pTabCtrl, CTabCtrl);
  890. CString strText;
  891. TC_ITEM tcItem;
  892. int nTabs = ptcTabInfo->nTabs;
  893. PTC_TAB_ENTRY ptcTabEntry = ptcTabInfo->tcTabEntry;
  894. tcItem.mask = TCIF_TEXT;
  895. while (nTabs--)
  896. {
  897. strText.LoadString(ptcTabEntry->nStringId);
  898. tcItem.pszText = MKSTR(strText);
  899. pTabCtrl->InsertItem(ptcTabEntry->iItem, &tcItem);
  900. ptcTabEntry++;
  901. }
  902. SetDefaultFont(pTabCtrl);
  903. }
  904. //
  905. // Miscellaneous utilities
  906. //
  907. #define NUMBER_OF_SECONDS_IN_MINUTE (60)
  908. #define NUMBER_OF_SECONDS_IN_DAY (60 * 60 * 24)
  909. double SecondsSince1980ToDate(DWORD sysSeconds)
  910. /*++
  911. Routine Description:
  912. Converts time format to OLE-compliant.
  913. Arguments:
  914. hParent - parent item.
  915. pDomains - domain collection.
  916. Return Values:
  917. None.
  918. --*/
  919. {
  920. WORD dosDate = 0;
  921. WORD dosTime = 0;
  922. double dateTime = 0;
  923. FILETIME fileTime;
  924. LARGE_INTEGER locTime;
  925. DWORD locDays;
  926. DWORD locSeconds;
  927. TIME_ZONE_INFORMATION tzi;
  928. GetTimeZoneInformation(&tzi);
  929. locSeconds = sysSeconds - (tzi.Bias * NUMBER_OF_SECONDS_IN_MINUTE);
  930. locDays = locSeconds / NUMBER_OF_SECONDS_IN_DAY; // round off to days
  931. locSeconds = locDays * NUMBER_OF_SECONDS_IN_DAY; // for displaying time...
  932. RtlSecondsSince1980ToTime(locSeconds, &locTime);
  933. fileTime.dwLowDateTime = locTime.LowPart;
  934. fileTime.dwHighDateTime = locTime.HighPart;
  935. // JonN 5/15/00 PREFIX 112121
  936. // ignore error returns here
  937. (void)FileTimeToDosDateTime(&fileTime, &dosDate, &dosTime);
  938. (void)DosDateTimeToVariantTime(dosDate, dosTime, &dateTime);
  939. return dateTime;
  940. }
  941. void SetDefaultFont(CWnd* pWnd)
  942. /*++
  943. Routine Description:
  944. Set default font.
  945. Arguments:
  946. pWnd - window to change font.
  947. Return Values:
  948. None.
  949. --*/
  950. {
  951. HRESULT hr;
  952. VALIDATE_OBJECT(pWnd, CWnd);
  953. HFONT hFont;
  954. LOGFONT lFont;
  955. ZeroMemory(&lFont, sizeof(lFont)); // initialize
  956. lFont.lfHeight = -12;
  957. lFont.lfWeight = FW_NORMAL; // normal
  958. CHARSETINFO csi;
  959. DWORD dw = ::GetACP();
  960. if (!::TranslateCharsetInfo((DWORD*)UintToPtr(dw), &csi, TCI_SRCCODEPAGE))
  961. csi.ciCharset = ANSI_CHARSET;
  962. lFont.lfCharSet = (BYTE)csi.ciCharset;
  963. hr = StringCbCopy(lFont.lfFaceName, sizeof(lFont.lfFaceName), TEXT("MS Shell Dlg"));
  964. ASSERT(SUCCEEDED(hr));
  965. hFont = ::CreateFontIndirect(&lFont);
  966. pWnd->SetFont(CFont::FromHandle(hFont));
  967. }
  968. void SafeEnableWindow(CWnd* pEnableWnd, CWnd* pNewFocusWnd, CWnd* pOldFocusWnd, BOOL bEnableWnd)
  969. /*++
  970. Routine Description:
  971. Enable/disable window without losing focus.
  972. Arguments:
  973. pEnableWnd - window to enable/disable.
  974. bEnableWnd - true if window to be enabled.
  975. pOldFocusWnd - window with current focus.
  976. pNewFocusWnd - window to receive focus.
  977. Return Values:
  978. None.
  979. --*/
  980. {
  981. VALIDATE_OBJECT(pEnableWnd, CWnd);
  982. VALIDATE_OBJECT(pNewFocusWnd, CWnd);
  983. if (bEnableWnd)
  984. {
  985. pEnableWnd->EnableWindow(TRUE);
  986. }
  987. else if (pOldFocusWnd == pEnableWnd)
  988. {
  989. ASSERT(pNewFocusWnd->IsWindowEnabled());
  990. pNewFocusWnd->SetFocus();
  991. pEnableWnd->EnableWindow(FALSE);
  992. }
  993. else
  994. {
  995. pEnableWnd->EnableWindow(FALSE);
  996. }
  997. }