home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / mfc / ole / oleview / mainfrm.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-03-27  |  13.5 KB  |  522 lines

  1. // mainfrm.cpp : implementation of the CMainFrame class
  2. //
  3.  
  4. // This is a part of the Microsoft Foundation Classes C++ library.
  5. // Copyright (C) 1992-1998 Microsoft Corporation
  6. // All rights reserved.
  7. //
  8. // This source code is only intended as a supplement to the
  9. // Microsoft Foundation Classes Reference and related
  10. // electronic documentation provided with the library.
  11. // See these sources for detailed information regarding the
  12. // Microsoft Foundation Classes product.
  13.  
  14. #include "stdafx.h"
  15. #include "OleView.h"
  16. #include "mainfrm.h"
  17. #include "shadow.h"
  18. #include "obj_vw.h"
  19.  
  20. #ifdef _DEBUG
  21. #undef THIS_FILE
  22. static char BASED_CODE THIS_FILE[] = __FILE__;
  23. #endif
  24.  
  25.  
  26. /////////////////////////////////////////////////////////////////////////////
  27. // CMainFrame
  28. // Create a splitter window which splits an output text view and an input view
  29. //                               |
  30. //    OBJECT VIEW (CObjTreeView)  | INTERFACE VIEW (CRegistryView)
  31. //                               |
  32.  
  33. IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)
  34.  
  35. BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
  36.     //{{AFX_MSG_MAP(CMainFrame)
  37.     ON_WM_CREATE()
  38.     ON_WM_DESTROY()
  39.     ON_COMMAND(ID_FILE_RUNREGEDIT, OnFileRunREGEDIT)
  40.     ON_COMMAND(ID_VIEW_REFRESH, OnViewRefresh)
  41.     ON_UPDATE_COMMAND_UI(ID_VIEW_REFRESH, OnUpdateViewRefresh)
  42.     ON_WM_SYSCOLORCHANGE()
  43.     ON_COMMAND(ID_OBJECT_DELETE, OnObjectDelete)
  44.     ON_UPDATE_COMMAND_UI(ID_OBJECT_DELETE, OnUpdateObjectDelete)
  45.     ON_COMMAND(ID_OBJECT_VERIFY, OnObjectVerify)
  46.     ON_UPDATE_COMMAND_UI(ID_OBJECT_VERIFY, OnUpdateObjectVerify)
  47.     ON_COMMAND(ID_FILE_VIEWTYPELIB, OnFileViewTypeLib)
  48.     ON_COMMAND(ID_IFACES_USEINPROCSERVER, OnUseInProcServer)
  49.     ON_UPDATE_COMMAND_UI(ID_IFACES_USEINPROCSERVER, OnUpdateUseInProcServer)
  50.     ON_COMMAND(ID_IFACES_USEINPROCHANDLER, OnUseInProcHandler)
  51.     ON_UPDATE_COMMAND_UI(ID_IFACES_USEINPROCHANDLER, OnUpdateUseInProcHandler)
  52.     ON_COMMAND(ID_IFACES_USELOCALSERVER, OnUseLocalServer)
  53.     ON_UPDATE_COMMAND_UI(ID_IFACES_USELOCALSERVER, OnUpdateUseLocalServer)
  54.     //}}AFX_MSG_MAP
  55. END_MESSAGE_MAP()
  56.  
  57. /////////////////////////////////////////////////////////////////////////////
  58. // arrays of IDs used to initialize control bars
  59.  
  60. // toolbar buttons - IDs are command buttons
  61. static UINT BASED_CODE buttons[] =
  62. {
  63.     // same order as in the bitmap 'toolbar.bmp'
  64.     ID_FILE_BINDTOAFILE,
  65.     ID_FILE_VIEWTYPELIB,
  66.     ID_SEPARATOR,
  67.     ID_FILE_RUNREGEDIT,
  68.     ID_SEPARATOR,
  69.     ID_OBJECT_DELETE,
  70.     ID_OBJECT_VERIFY,
  71.     ID_SEPARATOR,
  72.     ID_IFACES_USEINPROCSERVER,
  73.     ID_IFACES_USEINPROCHANDLER,
  74.     ID_IFACES_USELOCALSERVER,
  75.     ID_SEPARATOR,
  76.     ID_APP_ABOUT
  77. };
  78.  
  79. static UINT BASED_CODE indicators[] =
  80. {
  81.       ID_SEPARATOR,             // status line indicator
  82. //    ID_INDICATOR_CAPS,
  83. //    ID_INDICATOR_NUM,
  84. //    ID_INDICATOR_SCRL,
  85. };
  86.  
  87. /////////////////////////////////////////////////////////////////////////////
  88. // CMainFrame construction/destruction
  89.  
  90. CMainFrame::CMainFrame()
  91. {
  92.     m_pObjTreeView = NULL ;
  93.     m_pObjectView = NULL ;
  94. }
  95.  
  96. CMainFrame::~CMainFrame()
  97. {
  98. }
  99.  
  100. BOOL CMainFrame::LoadFrame(UINT nIDResource, DWORD dwDefaultStyle,
  101.                 CWnd* pParentWnd, CCreateContext* pContext)
  102. {
  103.     // Turn off auto update of title bar
  104.     dwDefaultStyle &= ~((DWORD)FWS_ADDTOTITLE) ;
  105.     BOOL f = CFrameWnd::LoadFrame(nIDResource, dwDefaultStyle,
  106.                 pParentWnd, pContext);
  107.  
  108.     return f ;
  109. }
  110. /////////////////////////////////////////////////////////////////////////////
  111. // CMainFrame message handlers
  112.  
  113. int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
  114. {
  115.     if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
  116.         return -1;
  117.  
  118.     if (!m_wndToolBar.Create(this) ||
  119.         !m_wndToolBar.LoadBitmap(IDR_MAINFRAME) ||
  120.         !m_wndToolBar.SetButtons(buttons, sizeof(buttons)/sizeof(UINT)))
  121.     {
  122.         TRACE(_T("Failed to create toolbar\n"));
  123.         return -1;      // fail to create
  124.     }
  125.  
  126. //    m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
  127. //    EnableDocking(CBRS_ALIGN_ANY);
  128. //    DockControlBar(&m_wndToolBar);
  129.  
  130.     m_wndToolBar.SetBarStyle(m_wndToolBar.GetBarStyle() |
  131.         CBRS_TOOLTIPS | CBRS_FLYBY);
  132.  
  133.     if (!m_wndStatusBar.Create(this) ||
  134.         !m_wndStatusBar.SetIndicators(indicators, sizeof(indicators)/sizeof(UINT)) )
  135.     {
  136.         TRACE(_T("Failed to create status bar\n"));
  137.         return -1;      // fail to create
  138.     }
  139.  
  140.     UINT nID, nStyle ;
  141.     int cxWidth ;
  142.     m_wndStatusBar.GetPaneInfo( 0, nID, nStyle, cxWidth ) ;
  143.     m_wndStatusBar.SetPaneInfo( 0, ID_SEPARATOR, nStyle | SBPS_POPOUT, cxWidth ) ;
  144.  
  145.     // When CStatusBar is created, a font is created and stored
  146.     // internally in AFX.  But AFX will clean that font up during
  147.     // shutdown, so we don't need to delete it.
  148.     m_wndStatusBar.SetFont( theApp.m_pFont ) ;
  149.  
  150.     DragAcceptFiles( TRUE ) ;
  151.  
  152.     return 0;
  153. }
  154.  
  155. void CMainFrame::OnDestroy()
  156. {
  157.     CFrameWnd::OnDestroy();
  158.     SavePosition() ;
  159. }
  160.  
  161. BOOL CMainFrame::OnCreateClient(LPCREATESTRUCT /*lpcs*/,
  162.      CCreateContext* pContext)
  163. {
  164.     // create a splitter with 1 row, 2 columns
  165.     if (!m_wndSplitter.CreateStatic(this, 1, 2))
  166.     {
  167.         TRACE(_T("Failed to CreateStaticSplitter\n"));
  168.         return FALSE;
  169.     }
  170.  
  171.     // add the first splitter pane - the default view in column 0
  172.     if (!m_wndSplitter.CreateView(0, 0,
  173.         pContext->m_pNewViewClass, CSize(240, 0), pContext))
  174.     {
  175.         TRACE(_T("Failed to create first pane\n"));
  176.         return FALSE;
  177.     }
  178.  
  179.     if (!m_wndSplitter.CreateView(0, 1,
  180.         RUNTIME_CLASS(CRegistryView), CSize(0, 0), pContext))
  181.     {
  182.         TRACE(_T("Failed to create second pane\n"));
  183.         return FALSE;
  184.     }
  185.  
  186.     m_pObjTreeView = (CObjTreeView*)m_wndSplitter.GetPane(0, 0) ;
  187.     m_pObjectView = (CRegistryView*)m_wndSplitter.GetPane(0,1) ;
  188.  
  189.     // activate the input view
  190.     SetActiveView((CView*)m_wndSplitter.GetPane(0, 0));
  191.     m_wndSplitter.SetColumnInfo( 0, 240, 0 ) ;
  192.  
  193.     return TRUE;
  194. }
  195.  
  196. /////////////////////////////////////////////////////////////////////////////
  197. // CMainFrame diagnostics
  198.  
  199. #ifdef _DEBUG
  200. void CMainFrame::AssertValid() const
  201. {
  202.     CFrameWnd::AssertValid();
  203. }
  204.  
  205. void CMainFrame::Dump(CDumpContext& dc) const
  206. {
  207.     CFrameWnd::Dump(dc);
  208. }
  209.  
  210. #endif //_DEBUG
  211.  
  212.  
  213. BOOL CMainFrame::SavePosition()
  214. {
  215.     CString szSection ;
  216.     CString szKey ;
  217.  
  218.     szSection.LoadString( IDS_INI_CONFIG ) ;
  219.     szKey.LoadString( IDS_INI_WNDPOS ) ;
  220.  
  221.     WINDOWPLACEMENT wp;
  222.     CString szValue ;
  223.  
  224.     wp.length = sizeof( WINDOWPLACEMENT );
  225.     GetWindowPlacement( &wp );
  226.  
  227.     int nWidth, n ;
  228.     m_wndSplitter.GetColumnInfo( 0, nWidth, n ) ;
  229.  
  230.     LPTSTR p = szValue.GetBuffer( 255 ) ;
  231.     wsprintf( p, _T("%d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d"),
  232.         wp.showCmd, wp.ptMinPosition.x, wp.ptMinPosition.y,
  233.         wp.ptMaxPosition.x, wp.ptMaxPosition.y,
  234.         wp.rcNormalPosition.left, wp.rcNormalPosition.top,
  235.         wp.rcNormalPosition.right, wp.rcNormalPosition.bottom,
  236.         nWidth,
  237.         (m_wndToolBar.GetStyle() & WS_VISIBLE) ? TRUE : FALSE,
  238.         (m_wndStatusBar.GetStyle() & WS_VISIBLE) ? TRUE : FALSE);
  239.  
  240.     szValue.ReleaseBuffer() ;
  241.     theApp.WriteProfileString( szSection, szKey, szValue );
  242.     return TRUE ;
  243. }
  244.  
  245. BOOL CMainFrame::RestorePosition(int nCmdShow)
  246. {
  247.     CString sz ;
  248.     CString szSection ;
  249.     CString szKey ;
  250.     BOOL fToolBar = TRUE ;
  251.     BOOL fStatusBar = TRUE ;
  252.     int  nWidth ;
  253.  
  254.     szSection.LoadString( IDS_INI_CONFIG ) ;
  255.     szKey.LoadString( IDS_INI_WNDPOS ) ;
  256.  
  257.     WINDOWPLACEMENT wp;
  258.     int     nConv;
  259.  
  260.     wp.length = sizeof( WINDOWPLACEMENT );
  261.     wp.flags = 0 ;
  262.  
  263.     TRY
  264.     {
  265.         sz = theApp.GetProfileString(szSection, szKey, _T("") ) ;
  266.         if (sz.IsEmpty())
  267.             AfxThrowMemoryException();
  268.  
  269.         LPTSTR   lp = (LPTSTR)sz.GetBuffer( 255 );
  270.  
  271.         wp.showCmd = (WORD)ParseOffNumber( (LPTSTR FAR *)&lp, &nConv );
  272.         if (!nConv)
  273.             AfxThrowMemoryException();
  274.  
  275.         wp.ptMinPosition.x = (int)ParseOffNumber( (LPTSTR FAR *)&lp, &nConv );
  276.         if (!nConv)
  277.             AfxThrowMemoryException();
  278.  
  279.         wp.ptMinPosition.y = (int)ParseOffNumber( (LPTSTR FAR *)&lp, &nConv );
  280.         if (!nConv)
  281.             AfxThrowMemoryException();
  282.  
  283.         wp.ptMaxPosition.x = (int)ParseOffNumber( (LPTSTR FAR *)&lp, &nConv );
  284.         if (!nConv)
  285.             AfxThrowMemoryException();
  286.  
  287.         wp.ptMaxPosition.y = (int)ParseOffNumber( (LPTSTR FAR *)&lp, &nConv );
  288.         if (!nConv)
  289.             AfxThrowMemoryException();
  290.  
  291.         wp.rcNormalPosition.left = (int)ParseOffNumber( (LPTSTR FAR *)&lp, &nConv );
  292.         if (!nConv)
  293.             AfxThrowMemoryException();
  294.  
  295.         wp.rcNormalPosition.top = (int)ParseOffNumber( (LPTSTR FAR *)&lp, &nConv );
  296.         if (!nConv)
  297.             AfxThrowMemoryException();
  298.  
  299.         wp.rcNormalPosition.right = (int)ParseOffNumber( (LPTSTR FAR *)&lp, &nConv );
  300.         if (!nConv)
  301.             AfxThrowMemoryException();
  302.  
  303.         wp.rcNormalPosition.bottom = (int)ParseOffNumber( (LPTSTR FAR *)&lp, &nConv );
  304.         if (!nConv)
  305.             AfxThrowMemoryException();
  306.  
  307.         nWidth = (int)ParseOffNumber( (LPTSTR FAR *)&lp, &nConv );
  308.         if (!nConv)
  309.             AfxThrowMemoryException();
  310.  
  311.         fToolBar = (BOOL)ParseOffNumber( (LPTSTR FAR *)&lp, &nConv );
  312.         if (!nConv)
  313.             AfxThrowMemoryException();
  314.  
  315.         fStatusBar = (BOOL)ParseOffNumber( (LPTSTR FAR *)&lp, &nConv );
  316.         if (!nConv)
  317.             AfxThrowMemoryException();
  318.  
  319.         // Always strip off minimize.
  320.         //
  321.         if (wp.showCmd == SW_SHOWMINIMIZED)
  322.             wp.showCmd = SW_SHOWNORMAL ;
  323.  
  324.         if (nCmdShow != SW_SHOWNORMAL || nCmdShow != SW_NORMAL)
  325.             wp.showCmd = nCmdShow ;
  326.  
  327.         m_wndSplitter.SetColumnInfo( 0, nWidth, 0 ) ;
  328.     }
  329.     CATCH(CException, pException)
  330.     {
  331.         fToolBar = TRUE ;
  332.         fStatusBar = TRUE ;
  333.         ShowControlBar( &m_wndToolBar, fToolBar, TRUE ) ;
  334.         ShowControlBar( &m_wndStatusBar, fStatusBar, TRUE ) ;
  335.         ShowWindow( SW_SHOWNORMAL );
  336.         return FALSE ;
  337.     }
  338.     END_CATCH
  339.     ShowControlBar( &m_wndToolBar, fToolBar, TRUE ) ;
  340.     ShowControlBar( &m_wndStatusBar, fStatusBar, TRUE ) ;
  341.     return (BOOL)SetWindowPlacement( &wp ) ;
  342. }
  343.  
  344. void CMainFrame::OnFileRunREGEDIT()
  345. {
  346.     if (WinExec( "REGEDT32.EXE", SW_SHOWNORMAL ) < 32)
  347.         WinExec( "REGEDIT.EXE", SW_SHOWNORMAL ) ;
  348. }
  349.  
  350. void CMainFrame::OnViewRefresh()
  351. {
  352.     COle2ViewDoc*   pDoc = (COle2ViewDoc*)GetActiveDocument() ;
  353.     pDoc->UpdateAllViews( NULL, UPD_NORELOAD ) ;
  354. }
  355.  
  356. void CMainFrame::OnUpdateViewRefresh(CCmdUI* /*pCmdUI*/)
  357. {
  358. }
  359.  
  360. void CMainFrame::OnSysColorChange()
  361. {
  362. //    Ctl3dColorChange() ;
  363.     theApp.LoadBitmaps( TRUE ) ;
  364.     CFrameWnd::OnSysColorChange();
  365. }
  366.  
  367. void CMainFrame::OnObjectDelete()
  368. {
  369.     m_pObjTreeView->OnObjectDelete() ;
  370. }
  371.  
  372. void CMainFrame::OnUpdateObjectDelete(CCmdUI* pCmdUI)
  373. {
  374.     pCmdUI->Enable( m_pObjTreeView->IsValidSel() ) ;
  375. }
  376.  
  377. void CMainFrame::OnObjectVerify()
  378. {
  379.     m_pObjTreeView->OnObjectVerify() ;
  380.  
  381. }
  382.  
  383. void CMainFrame::OnUpdateObjectVerify(CCmdUI* pCmdUI)
  384. {
  385.     pCmdUI->Enable( m_pObjTreeView->IsValidSel() ) ;
  386. }
  387.  
  388. void CMainFrame::OnFileViewTypeLib()
  389. {
  390.     USES_CONVERSION;
  391.     static TCHAR szFilter[] = _T("TypeLib Files (*.tlb;*.olb;*.dll;*.ocx;*.exe)|*.tlb;*.olb;*.dll;*.ocx;*.exe|AllFiles(*.*)|*.*|") ;
  392.  
  393.     CFileDialog dlg(TRUE, _T("*.tlb"), NULL,
  394.                     OFN_FILEMUSTEXIST | OFN_HIDEREADONLY | OFN_PATHMUSTEXIST,
  395.                     szFilter, this);
  396.     if (IDOK != dlg.DoModal())
  397.         return ;
  398.  
  399.     // Call LoadTypeLib
  400.     LPTYPELIB lpTypeLib;
  401.     HRESULT hr = ::LoadTypeLib(T2COLE(dlg.GetPathName()), &lpTypeLib);
  402.     if (FAILED(hr))
  403.     {
  404.         CString szErrorMsg;
  405.         szErrorMsg.Format(_T("LoadTypeLib( %s ) failed."),(LPCTSTR)dlg.GetPathName());
  406.         ErrorMessage( szErrorMsg, hr );
  407.         return ;
  408.     }
  409.     // call the interface wiewer
  410.     ASSERT(lpTypeLib != NULL);
  411.     ViewInterface( GetSafeHwnd(), IID_ITypeLib, (IUnknown*)lpTypeLib);
  412.     VERIFY(0 == lpTypeLib->Release()) ;
  413. }
  414.  
  415. void WINAPI ViewInterface( HWND hwnd, REFIID riid, IUnknown *punk )
  416. {
  417.     IInterfaceViewer* piv = NULL ;
  418.     SCODE sc ;
  419.  
  420.     // Look in the registry for the "Ole2ViewIViewer=" key for this iid
  421.     //
  422.     TCHAR szKey[128] ;
  423.     TCHAR szValue[80] ;
  424.     TCHAR szInterface[80] ;
  425.  
  426.     USES_CONVERSION;
  427.     // get the string from CLSID in TCHAR format
  428.     LPOLESTR lpszOleIID = NULL;
  429.     ::StringFromCLSID(riid, &lpszOleIID);
  430.     ASSERT(lpszOleIID != NULL);
  431.  
  432.     LPTSTR lpszIID = OLE2T(lpszOleIID);
  433.     ASSERT(lpszIID != NULL);
  434.     IMalloc* pmal = NULL ;
  435.     ::CoGetMalloc( MEMCTX_TASK, &pmal ) ;
  436.     pmal->Free( lpszOleIID ) ;
  437.     pmal->Release() ;
  438.  
  439.     wsprintf(szKey, _T("Interface\\%s"), lpszIID) ;
  440.  
  441.     LONG cb = sizeof(szValue);
  442.     if (::RegQueryValue(HKEY_CLASSES_ROOT, szKey, szInterface, &cb) != ERROR_SUCCESS)
  443.     {
  444.         lstrcpy( szInterface, "<no name>" ) ;
  445.     }
  446.  
  447.     wsprintf( szKey, _T("Interface\\%s\\Ole2ViewIViewerCLSID"), lpszIID );
  448.  
  449.     cb = sizeof(szValue) ;
  450.     if (::RegQueryValue(HKEY_CLASSES_ROOT, szKey, szValue, &cb) != ERROR_SUCCESS)
  451.     {
  452.         CString str;
  453.         str.Format(_T("There is no interface viewer for %s installed."), szInterface);
  454.         ErrorMessage( str, E_FAIL ) ;
  455.         return ;
  456.     }
  457.  
  458.     CLSID clsid ;
  459.     sc = ::CLSIDFromString( T2OLE(szValue), &clsid ) ;
  460.     if (FAILED(sc))
  461.     {
  462.         CString str;
  463.         str.Format(_T("Could not convert the CLSID of the %s interface viewer."), szInterface);
  464.         ErrorMessage( str, sc ) ;
  465.         return ;
  466.     }
  467.  
  468.     sc = ::CoCreateInstance( clsid, NULL, CLSCTX_SERVER, IID_IInterfaceViewer, (void**)&piv ) ;
  469.     if (SUCCEEDED(sc))
  470.     {
  471.         IUnknown* ptemp = NULL ;
  472.         HRESULT hr = punk->QueryInterface( riid, (void**)&ptemp ) ;
  473.         if (SUCCEEDED(hr))
  474.         {
  475.             piv->View( hwnd, riid, ptemp ) ;
  476.             piv->Release() ;
  477.             ptemp->Release() ;
  478.         }
  479.     }
  480.     else
  481.     {
  482.         CString str;
  483.         str.Format(_T("The %s interface viewer failed to load."), szInterface);
  484.         ErrorMessage( str, sc ) ;
  485.     }
  486. }
  487.  
  488. void CMainFrame::OnUseInProcServer()
  489. {
  490.     m_pObjTreeView->OnUseInProcServer();
  491. }
  492.  
  493. void CMainFrame::OnUpdateUseInProcServer(CCmdUI* pCmdUI)
  494. {
  495.     m_pObjTreeView->OnUpdateUseInProcServer( pCmdUI );
  496. }
  497.  
  498. void CMainFrame::OnUseInProcHandler()
  499. {
  500.    m_pObjTreeView->OnUseInProcHandler();
  501. }
  502.  
  503. void CMainFrame::OnUpdateUseInProcHandler(CCmdUI* pCmdUI)
  504. {
  505.     m_pObjTreeView->OnUpdateUseInProcHandler( pCmdUI ) ;
  506. }
  507.  
  508. void CMainFrame::OnUseLocalServer()
  509. {
  510.     m_pObjTreeView->OnUseLocalServer() ;
  511. }
  512.  
  513. void CMainFrame::OnUpdateUseLocalServer(CCmdUI* pCmdUI)
  514. {
  515.     m_pObjTreeView->OnUpdateUseLocalServer( pCmdUI ) ;
  516. }
  517.  
  518. void CMainFrame::OnFileBind()
  519. {
  520.     m_pObjTreeView->OnFileBind() ;
  521. }
  522.