Source code of Windows XP (NT5)
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.

225 lines
5.7 KiB

  1. // implements the exported CTreeItem
  2. #include "stdafx.h"
  3. #include "KeyObjs.h"
  4. #include "machine.h"
  5. #include "KeyRing.h"
  6. #include "KRDoc.h"
  7. #include "KRView.h"
  8. IMPLEMENT_DYNCREATE(CTreeItem, CObject);
  9. extern CKeyRingView* g_pTreeView;
  10. //-------------------------------------------------
  11. // constructor. it is NOT added to the parent immediately
  12. //-------------------------------------------------
  13. CTreeItem::CTreeItem():
  14. m_iImage(0),
  15. m_hTreeItem(NULL),
  16. m_fDirty( FALSE )
  17. {
  18. m_szItemName.Empty();
  19. }
  20. //-------------------------------------------------
  21. // remove the item from the ctreectrl
  22. //-------------------------------------------------
  23. BOOL CTreeItem::FRemoveFromTree()
  24. {
  25. CTreeCtrl *pTree = PGetTreeCtrl();
  26. ASSERT(pTree);
  27. ASSERT(m_hTreeItem);
  28. return pTree->DeleteItem( m_hTreeItem );
  29. }
  30. //-------------------------------------------------
  31. // add the item to the ctreectrl
  32. //-------------------------------------------------
  33. BOOL CTreeItem::FAddToTree( CTreeItem* pParent )
  34. {
  35. HTREEITEM hParent = TVI_ROOT;
  36. // this item should NOT have already been added
  37. ASSERT( !m_hTreeItem );
  38. // get the tree control
  39. CTreeCtrl* pTree = PGetTreeCtrl();
  40. ASSERT( pTree );
  41. if ( !pTree ) return FALSE;
  42. // get the parent's htree item
  43. if ( pParent )
  44. {
  45. hParent = pParent->HGetTreeItem();
  46. ASSERT( hParent );
  47. }
  48. // add this item to the tree
  49. HTREEITEM hTreeItem = pTree->InsertItem( m_szItemName, hParent );
  50. ASSERT( hTreeItem );
  51. if ( !hTreeItem ) return FALSE;
  52. // set the item's image to be the correct icon
  53. BOOL f = pTree->SetItemImage( hTreeItem, m_iImage, m_iImage );
  54. // set the item's private data to point back to the host object
  55. f = pTree->SetItemData( hTreeItem, (ULONG)this );
  56. // now set the tree item back into this so we can backtrack later
  57. m_hTreeItem = hTreeItem;
  58. // tell the item to update its caption
  59. UpdateCaption();
  60. // we always expand the parent after adding something to it
  61. // and we also alphabetize it
  62. if ( pParent )
  63. {
  64. pTree->SortChildren( hParent );
  65. pTree->Expand( hParent, TVE_EXPAND );
  66. }
  67. else
  68. {
  69. // this item is at the root level
  70. pTree->SortChildren( TVI_ROOT );
  71. }
  72. // looks successful to me
  73. return TRUE;
  74. }
  75. //-------------------------------------------------
  76. // get the parent item to this tree item
  77. //-------------------------------------------------
  78. CTreeItem* CTreeItem::PGetParent()
  79. {
  80. if ( !m_hTreeItem ) return NULL;
  81. CTreeCtrl *pTree = PGetTreeCtrl();
  82. ASSERT(pTree);
  83. ASSERT(m_hTreeItem);
  84. // get the parent tree item
  85. HTREEITEM hParent = pTree->GetParentItem( m_hTreeItem );
  86. ASSERT( hParent );
  87. if ( !hParent ) return NULL;
  88. // return the parent
  89. return (CTreeItem*)pTree->GetItemData( hParent );
  90. }
  91. //-------------------------------------------------
  92. // access the name shown in the tree view
  93. //-------------------------------------------------
  94. BOOL CTreeItem::FSetCaption( CString& szCaption )
  95. {
  96. CTreeCtrl *pTree = PGetTreeCtrl();
  97. ASSERT( szCaption );
  98. ASSERT( pTree );
  99. if ( m_hTreeItem )
  100. return pTree->SetItemText( m_hTreeItem, szCaption );
  101. else
  102. return FALSE;
  103. }
  104. //-------------------------------------------------
  105. // set the image shown in the tree view
  106. //-------------------------------------------------
  107. BOOL CTreeItem::FSetImage( WORD i )
  108. {
  109. if ( !m_hTreeItem )
  110. return FALSE;
  111. // store away the image index
  112. m_iImage = i;
  113. CTreeCtrl *pTree = PGetTreeCtrl();
  114. ASSERT( i );
  115. ASSERT( pTree );
  116. ASSERT( m_hTreeItem );
  117. return pTree->SetItemImage( m_hTreeItem, m_iImage, m_iImage );
  118. }
  119. //-------------------------------------------------
  120. // get the grandparental ctreectrl object
  121. //-------------------------------------------------
  122. CTreeCtrl* CTreeItem::PGetTreeCtrl( void )
  123. {
  124. return (CTreeCtrl*)g_pTreeView;
  125. }
  126. //-------------------------------------------------
  127. WORD CTreeItem::GetChildCount()
  128. {
  129. WORD cChildren = 0;
  130. // loop through the children and count them
  131. CTreeItem* pChild = GetFirstChild();
  132. while( pChild )
  133. {
  134. // as the great Count Von Count would say.....
  135. cChildren++;
  136. // get the next child
  137. pChild = GetNextChild( pChild );
  138. }
  139. // how many where there?
  140. return cChildren;
  141. }
  142. //-------------------------------------------------
  143. CTreeItem* CTreeItem::GetFirstChild()
  144. {
  145. ASSERT(HGetTreeItem());
  146. if ( !HGetTreeItem() ) return NULL;
  147. // get the tree control
  148. CTreeCtrl* pTree = PGetTreeCtrl();
  149. // get the first child
  150. HTREEITEM hTree = pTree->GetChildItem( HGetTreeItem() );
  151. // if there is no item, don't return anything
  152. if ( !hTree )
  153. return NULL;
  154. // return the object
  155. return (CTreeItem*)pTree->GetItemData( hTree );
  156. }
  157. //-------------------------------------------------
  158. CTreeItem* CTreeItem::GetNextChild( CTreeItem* pKid )
  159. {
  160. ASSERT(pKid->HGetTreeItem());
  161. if ( !pKid->HGetTreeItem() ) return NULL;
  162. // get the tree control
  163. CTreeCtrl* pTree = PGetTreeCtrl();
  164. // get the first child
  165. HTREEITEM hTree = pTree->GetNextSiblingItem( pKid->HGetTreeItem() );
  166. // if there is no item, don't return anything
  167. if ( !hTree )
  168. return NULL;
  169. // return the object
  170. return (CTreeItem*)pTree->GetItemData( hTree );
  171. }
  172. //-------------------------------------------------
  173. void CTreeItem::SetDirty( BOOL fDirty )
  174. {
  175. // get the parent item
  176. CTreeItem* pParent = PGetParent();
  177. // set the parent dirty
  178. if ( pParent && fDirty )
  179. pParent->SetDirty( fDirty );
  180. // set the dirty flag
  181. m_fDirty = fDirty;
  182. }