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.

386 lines
10 KiB

  1. // KRView.cpp : implementation of the CKeyRingView class
  2. //
  3. #include "stdafx.h"
  4. #include "KeyRing.h"
  5. #include "MainFrm.h"
  6. #include "KeyObjs.h"
  7. #include "machine.h"
  8. #include "KRDoc.h"
  9. #include "KeyDView.h"
  10. #include "KRView.h"
  11. #ifdef _DEBUG
  12. #define new DEBUG_NEW
  13. #undef THIS_FILE
  14. static char THIS_FILE[] = __FILE__;
  15. #endif
  16. extern CKeyDataView* g_pDataView;
  17. /////////////////////////////////////////////////////////////////////////////
  18. // CKeyRingView
  19. IMPLEMENT_DYNCREATE(CKeyRingView, CTreeView)
  20. BEGIN_MESSAGE_MAP(CKeyRingView, CTreeView)
  21. //{{AFX_MSG_MAP(CKeyRingView)
  22. ON_WM_RBUTTONDOWN()
  23. ON_NOTIFY_REFLECT(TVN_SELCHANGED, OnSelchanged)
  24. ON_UPDATE_COMMAND_UI(ID_SERVER_DISCONNECT, OnUpdateServerDisconnect)
  25. ON_COMMAND(ID_SERVER_DISCONNECT, OnServerDisconnect)
  26. ON_NOTIFY_REFLECT(NM_DBLCLK, OnDblclk)
  27. ON_WM_CHAR()
  28. //}}AFX_MSG_MAP
  29. // Standard printing commands
  30. ON_COMMAND(ID_FILE_PRINT, CTreeView::OnFilePrint)
  31. ON_COMMAND(ID_FILE_PRINT_DIRECT, CTreeView::OnFilePrint)
  32. ON_COMMAND(ID_FILE_PRINT_PREVIEW, CTreeView::OnFilePrintPreview)
  33. END_MESSAGE_MAP()
  34. /////////////////////////////////////////////////////////////////////////////
  35. // CKeyRingView construction/destruction
  36. CKeyRingView::CKeyRingView()
  37. {
  38. // TODO: add construction code here
  39. }
  40. CKeyRingView::~CKeyRingView()
  41. {
  42. }
  43. BOOL CKeyRingView::PreCreateWindow(CREATESTRUCT& cs)
  44. {
  45. cs.style |= (TVS_HASLINES | TVS_HASBUTTONS | TVS_LINESATROOT);
  46. return CTreeView::PreCreateWindow(cs);
  47. }
  48. /////////////////////////////////////////////////////////////////////////////
  49. // CKeyRingView drawing
  50. void CKeyRingView::OnDraw(CDC* pDC)
  51. {
  52. CKeyRingDoc* pDoc = GetDocument();
  53. ASSERT_VALID(pDoc);
  54. // TODO: add draw code for native data here
  55. }
  56. /////////////////////////////////////////////////////////////////////////////
  57. // CKeyRingView printing
  58. BOOL CKeyRingView::OnPreparePrinting(CPrintInfo* pInfo)
  59. {
  60. // default preparation
  61. return DoPreparePrinting(pInfo);
  62. }
  63. void CKeyRingView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  64. {
  65. // TODO: add extra initialization before printing
  66. }
  67. void CKeyRingView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  68. {
  69. // TODO: add cleanup after printing
  70. }
  71. /////////////////////////////////////////////////////////////////////////////
  72. // CKeyRingView diagnostics
  73. #ifdef _DEBUG
  74. void CKeyRingView::AssertValid() const
  75. {
  76. CTreeView::AssertValid();
  77. }
  78. void CKeyRingView::Dump(CDumpContext& dc) const
  79. {
  80. CTreeView::Dump(dc);
  81. }
  82. CKeyRingDoc* CKeyRingView::GetDocument() // non-debug version is inline
  83. {
  84. ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CKeyRingDoc)));
  85. return (CKeyRingDoc*)m_pDocument;
  86. }
  87. #endif //_DEBUG
  88. /////////////////////////////////////////////////////////////////////////////
  89. // CKeyRingView message handlers
  90. //-----------------------------------------------------------------------
  91. void CKeyRingView::DestroyItems()
  92. {
  93. HTREEITEM hRoot;
  94. CTreeCtrl* pTree = (CTreeCtrl*)this;
  95. // make sure the items in the tree are deleted as well
  96. while ( hRoot = pTree->GetRootItem() )
  97. {
  98. CMachine* pMachine = (CMachine*)pTree->GetItemData( hRoot );
  99. ASSERT( pMachine->IsKindOf( RUNTIME_CLASS(CMachine) ) );
  100. DisconnectMachine( pMachine );
  101. delete pMachine;
  102. }
  103. }
  104. //-----------------------------------------------------------------------
  105. void CKeyRingView::DisconnectMachine( CMachine* pMachine )
  106. {
  107. // loop through the services and disconnect them all
  108. CService* pService;
  109. while( pService = (CService*)pMachine->GetFirstChild() )
  110. {
  111. ASSERT( pService->IsKindOf( RUNTIME_CLASS(CService) ) );
  112. pService->CloseConnection();
  113. pService->FRemoveFromTree();
  114. delete pService;
  115. }
  116. // now remove the machine itself
  117. pMachine->FRemoveFromTree();
  118. }
  119. //-----------------------------------------------------------------------
  120. BOOL CKeyRingView::FCommitMachinesNow()
  121. {
  122. CTreeCtrl* pTree = (CTreeCtrl*)this;
  123. // the success flag
  124. BOOL fSuccess = TRUE;
  125. // make sure the items in the tree are deleted as well
  126. HTREEITEM hItem = pTree->GetRootItem();
  127. while ( hItem )
  128. {
  129. CInternalMachine* pMachine = (CInternalMachine*)pTree->GetItemData( hItem );
  130. ASSERT( pMachine->IsKindOf( RUNTIME_CLASS(CMachine) ) );
  131. fSuccess &= pMachine->FCommitNow();
  132. // get the next item
  133. hItem = pTree->GetNextSiblingItem( hItem );
  134. }
  135. // return whether or not it succeeded
  136. return fSuccess;
  137. }
  138. //-----------------------------------------------------------------------
  139. BOOL CKeyRingView::Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext)
  140. {
  141. BOOL f;
  142. // create the main object
  143. f = CTreeView::Create(lpszClassName, lpszWindowName, dwStyle, rect, pParentWnd, nID, pContext);
  144. // assuming that worked, create the image list and set it into the tree view control
  145. if ( f )
  146. {
  147. CTreeCtrl *pTree = (CTreeCtrl*)this;;
  148. // create the image list
  149. f = m_imageList.Create( IDB_TREEIMAGES, 16, 3, 0x00FF00FF );
  150. // set the image list into the tree control
  151. pTree->SetImageList( &m_imageList, TVSIL_NORMAL );
  152. }
  153. // return the answer
  154. return f;
  155. }
  156. //------------------------------------------------------------------------------
  157. void CKeyRingView::OnContextMenu(CWnd*, CPoint point)
  158. {
  159. // lets see, what is that point in client coordinates.....
  160. CPoint ptClient = point;
  161. ScreenToClient( &ptClient );
  162. // we need to start this by selecting the correct item that
  163. // is being clicked on. First, find the item
  164. CTreeCtrl* pTree = (CTreeCtrl*)this;
  165. UINT flagsHit;
  166. HTREEITEM hHit = pTree->HitTest( ptClient, &flagsHit );
  167. // if nothing was hit, then don't bother with a context menu
  168. if ( !hHit ) return;
  169. // select the item that was hit
  170. pTree->SelectItem( hHit );
  171. // double check that we have the right selection
  172. // now get that item's CTreeItem
  173. CTreeItem* pItem = PGetSelectedItem();
  174. if ( !pItem ) return;
  175. // get on with the context menu...
  176. CMenu menu;
  177. VERIFY(menu.LoadMenu(IDR_KEYPROP));
  178. // determine which sub menu to display
  179. WORD iSubMenu;
  180. if ( pItem->IsKindOf(RUNTIME_CLASS(CKey)) )
  181. iSubMenu = 0; // key menu
  182. else if ( pItem->IsKindOf(RUNTIME_CLASS(CMachine)) )
  183. iSubMenu = 1; // machine menu
  184. else if ( pItem->IsKindOf(RUNTIME_CLASS(CService)) )
  185. iSubMenu = 2; // server menu
  186. else
  187. {
  188. ASSERT( FALSE );
  189. return; // we can't handle it
  190. }
  191. CMenu* pPopup = menu.GetSubMenu(iSubMenu);
  192. ASSERT(pPopup != NULL);
  193. CWnd* pWndPopupOwner = this;
  194. while (pWndPopupOwner->GetStyle() & WS_CHILD)
  195. pWndPopupOwner = pWndPopupOwner->GetParent();
  196. pPopup->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, point.x, point.y,
  197. pWndPopupOwner);
  198. }
  199. //-----------------------------------------------------------------------
  200. BOOL CKeyRingView::PreTranslateMessage(MSG* pMsg)
  201. {
  202. short a;
  203. if ( pMsg->message == WM_KEYDOWN )
  204. a = 0;
  205. // CG: This block was added by the Pop-up Menu component
  206. {
  207. // Shift+F10: show pop-up menu.
  208. if ((((pMsg->message == WM_KEYDOWN || pMsg->message == WM_SYSKEYDOWN) && // If we hit a key and
  209. (pMsg->wParam == VK_F10) && (GetKeyState(VK_SHIFT) & ~1)) != 0) || // it's Shift+F10 OR
  210. (pMsg->message == WM_CONTEXTMENU)) // Natural keyboard key
  211. {
  212. // get the rect of the selected item and base the position of the menu
  213. // off of that.
  214. CRect rect;
  215. CTreeCtrl* pTree = (CTreeCtrl*)this;
  216. HTREEITEM hSelItem = pTree->GetSelectedItem();
  217. // if no item is selected, bail
  218. if ( !hSelItem )
  219. return TRUE;
  220. // now get that rect...
  221. if ( !pTree->GetItemRect( hSelItem, &rect, TRUE ) )
  222. return TRUE;
  223. // finish prepping and call the menu
  224. ClientToScreen(rect);
  225. CPoint point = rect.BottomRight();
  226. point.Offset(-10, -10);
  227. OnContextMenu(NULL, point);
  228. return TRUE;
  229. }
  230. }
  231. return CTreeView::PreTranslateMessage(pMsg);
  232. }
  233. //-----------------------------------------------------------------------
  234. void CKeyRingView::OnRButtonDown(UINT nFlags, CPoint point)
  235. {
  236. // let the sub class do its thing
  237. CTreeView::OnRButtonDown(nFlags, point);
  238. // convert the point to screen coordinates
  239. ClientToScreen( &point );
  240. // run the context menu
  241. OnContextMenu(NULL, point);
  242. }
  243. //-----------------------------------------------------------------------
  244. CTreeItem* CKeyRingView::PGetSelectedItem()
  245. {
  246. CTreeCtrl* pTree = (CTreeCtrl*)this;
  247. HTREEITEM hHit = pTree->GetSelectedItem();
  248. if ( !hHit ) return NULL;
  249. // now get that item's CTreeItem
  250. return (CTreeItem*)pTree->GetItemData( hHit );
  251. }
  252. //-----------------------------------------------------------------------
  253. void CKeyRingView::OnSelchanged(NMHDR* pNMHDR, LRESULT* pResult)
  254. {
  255. NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
  256. CKeyRingDoc* pDoc = GetDocument();
  257. // make sure the key data view updates
  258. pDoc->UpdateAllViews( this, HINT_ChangeSelection, NULL );
  259. *pResult = 0;
  260. }
  261. //-----------------------------------------------------------------------
  262. void CKeyRingView::OnUpdateServerDisconnect(CCmdUI* pCmdUI)
  263. {
  264. CTreeItem* pItem = PGetSelectedItem();
  265. if ( pItem )
  266. pCmdUI->Enable( pItem->IsKindOf(RUNTIME_CLASS(CRemoteMachine)) );
  267. else
  268. pCmdUI->Enable( FALSE );
  269. }
  270. //-----------------------------------------------------------------------
  271. void CKeyRingView::OnServerDisconnect()
  272. {
  273. CRemoteMachine* pMachine = (CRemoteMachine*)PGetSelectedItem();
  274. ASSERT( pMachine->IsKindOf(RUNTIME_CLASS(CRemoteMachine)) );
  275. // if the machine is dirty, try to commit it
  276. if ( pMachine->FGetDirty() )
  277. {
  278. // ask the user if they want to commit
  279. switch( AfxMessageBox(IDS_SERVER_COMMIT, MB_YESNOCANCEL|MB_ICONQUESTION) )
  280. {
  281. case IDYES: // yes, they do want to commit
  282. pMachine->FCommitNow();
  283. break;
  284. case IDNO: // no, they don't want to commit
  285. break;
  286. case IDCANCEL: // whoa nellie! Stop this
  287. return;
  288. }
  289. }
  290. DisconnectMachine( pMachine );
  291. delete pMachine;
  292. }
  293. //-----------------------------------------------------------------------
  294. // A double click on a key should bring up the properties dialog
  295. void CKeyRingView::OnDblclk(NMHDR* pNMHDR, LRESULT* pResult)
  296. {
  297. CKey* pKey = (CKey*)PGetSelectedItem();
  298. if ( pKey && pKey->IsKindOf(RUNTIME_CLASS(CKey)) )
  299. pKey->OnProperties();
  300. *pResult = 0;
  301. }
  302. //-----------------------------------------------------------------------
  303. // allow the user to tab to the data view
  304. void CKeyRingView::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
  305. {
  306. if ( nChar == _T(' ') )
  307. {
  308. // get the parental frame window
  309. CMainFrame* pFrame = (CMainFrame*)GetParentFrame();
  310. // give the data view the focus
  311. pFrame->SetActiveView( g_pDataView );
  312. }
  313. else
  314. CTreeView::OnChar(nChar, nRepCnt, nFlags);
  315. }