home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / addins / cmdwnd / cmdstub / cmdwnd.cpp < prev    next >
C/C++ Source or Header  |  1998-04-03  |  4KB  |  179 lines

  1. // CmdWnd.cpp : Implementation of CCmdWnd
  2. #include "stdafx.h"
  3. #include "Cmdstub.h"
  4. #include "CmdWnd.h"
  5.  
  6. /////////////////////////////////////////////////////////////////////////////
  7. // CCmdWnd
  8. CCmdWnd::CCmdWnd()
  9. {
  10.     m_dwCookie = NULL;
  11. }
  12.  
  13. CCmdWnd::~CCmdWnd()
  14. {
  15.     if (m_pCommandWindow)
  16.     {
  17.         m_pCommandWindow.Release();
  18.         m_pCommandWindow = NULL;
  19.     }
  20.  
  21. }
  22.  
  23.  
  24. HRESULT CCmdWnd::OnConnection(IApplication* pApp, VARIANT_BOOL bFirstTime, long dwAddInID, VARIANT_BOOL* bOnConnection)
  25. {
  26.     HRESULT hr = S_OK;
  27.     m_spApplication = pApp;
  28.     m_dwAddInID = dwAddInID;
  29.  
  30.     // Connect up to application event sink
  31.     AtlAdvise(pApp, GetUnknown(), IID_IApplicationEvents, &m_dwAppEvents);
  32.  
  33.     // Connect up to debugger event sink
  34.     CComPtr<IDispatch> pDebugger;
  35.     hr = m_spApplication->get_Debugger(&pDebugger);
  36.     if (SUCCEEDED(hr))
  37.         AtlAdvise(pDebugger, GetUnknown(), IID_IDebuggerEvents, &m_dwDbgEvents);
  38.  
  39.     hr = pApp->SetAddInInfo((long)_Module.GetModuleInstance(), 
  40.         static_cast<ICmdWnd*>(this), IDB_TOOLBAR_MEDIUM_CMDWND, IDB_TOOLBAR_LARGE_CMDWND, dwAddInID);
  41.     LPCTSTR szCommand = _T("Command Window");
  42.     VARIANT_BOOL bRet;
  43.     if (SUCCEEDED(hr))
  44.     {
  45.         hr = pApp->AddCommand(CComBSTR(_T("Command Window\nCommand Window\nCommand Window\nCommand Window")),CComBSTR(_T("DoCmdWnd")), 0, dwAddInID, &bRet);
  46.     }
  47.  
  48.     // Add toolbar buttons only if this is the first time the add-in
  49.     // is being loaded.  Toolbar buttons are automatically remembered
  50.     // by Developer Studio from session to session, so we should only
  51.     // add the toolbar buttons once.
  52.     if (bFirstTime)
  53.     {
  54.         if (SUCCEEDED(hr))
  55.         {
  56.             hr = pApp->AddCommandBarButton(dsGlyph, CComBSTR(_T("Command Window")), dwAddInID);
  57.         }
  58.     }
  59.  
  60.     *bOnConnection = SUCCEEDED(hr) ? VARIANT_TRUE :VARIANT_FALSE;
  61.     return hr;
  62. }
  63.  
  64. HRESULT CCmdWnd::OnDisconnection(VARIANT_BOOL bLastTime)
  65. {
  66.     AtlUnadvise(m_spApplication, IID_IApplicationEvents, m_dwAppEvents);
  67.     AtlUnadvise(m_spApplication, IID_IDebuggerEvents, m_dwDbgEvents);
  68.     return S_OK;
  69. }
  70.  
  71. /////////////////////////////////////////////////////////////////////////////
  72. // Application events
  73. HRESULT CCmdWnd::BeforeBuildStart()
  74. {
  75.     return S_OK;
  76. }
  77. HRESULT CCmdWnd::BuildFinish(long nNumErrors, long nNumWarnings)
  78. {
  79.     return S_OK;
  80. }
  81. HRESULT CCmdWnd::BeforeApplicationShutDown()
  82. {
  83.     if (m_pCommandWindow)
  84.     {
  85.         m_pCommandWindow->DoClose();
  86.     }
  87.     return S_OK;
  88. }
  89. HRESULT CCmdWnd::DocumentOpen(IDispatch* theDocument)
  90. {
  91.     return S_OK;
  92. }
  93. HRESULT CCmdWnd::BeforeDocumentClose(IDispatch* theDocument)
  94. {
  95.     return S_OK;
  96. }
  97. HRESULT CCmdWnd::DocumentSave(IDispatch* theDocument)
  98. {
  99.     return S_OK;
  100. }
  101. HRESULT CCmdWnd::NewDocument(IDispatch* theDocument)
  102. {
  103.     return S_OK;
  104. }
  105. HRESULT CCmdWnd::WindowActivate(IDispatch* theWindow)
  106. {
  107.     return S_OK;
  108. }
  109. HRESULT CCmdWnd::WindowDeactivate(IDispatch* theWindow)
  110. {
  111.     return S_OK;
  112. }
  113. HRESULT CCmdWnd::WorkspaceOpen()
  114. {
  115.     return S_OK;
  116. }
  117. HRESULT CCmdWnd::WorkspaceClose()
  118. {
  119.     return S_OK;
  120. }
  121. HRESULT CCmdWnd::NewWorkspace()
  122. {
  123.     return S_OK;
  124. }
  125.  
  126. /////////////////////////////////////////////////////////////////////////////
  127. // Debugger event
  128. HRESULT CCmdWnd::BreakpointHit(IDispatch* pBreakpoint)
  129. {
  130.     return S_OK;
  131. }
  132.  
  133. HRESULT CCmdWnd::DoCmdWnd()
  134. {
  135.     HRESULT hr = E_FAIL;
  136.     if (m_pCommandWindow == NULL)
  137.     {
  138.         hr = CoCreateInstance(CLSID_CommandWindow, NULL, CLSCTX_ALL, IID_ICommandWindow, (LPVOID *)&m_pCommandWindow);
  139.         if (SUCCEEDED(hr) && m_pCommandWindow != NULL) // if m_pCommandWindow is null, proxy problem or interface. Try rebuild ALL
  140.         {
  141.             hr = m_pCommandWindow->Open(m_spApplication);
  142.             _ASSERTE(SUCCEEDED(hr));
  143.             if (SUCCEEDED(hr))
  144.             {
  145.                 CComQIPtr<_ICommandWindowEvents, &DIID__ICommandWindowEvents> pEvents;
  146.                 CComQIPtr<IUnknown, &IID_IUnknown> pUnkThis;
  147.                 CComQIPtr<IUnknown, &IID_IUnknown> pUnkCmdWnd = m_pCommandWindow;
  148.                 pUnkThis = (ICmdWnd *)this;
  149.                 pEvents = pUnkThis;
  150.                 _ASSERTE(pEvents);
  151.                 hr = AtlAdvise(pUnkCmdWnd, GetUnknown(), DIID__ICommandWindowEvents,  &m_dwCookie);
  152.             }
  153.  
  154.         }
  155.         if (FAILED(hr))
  156.         {
  157.             m_pCommandWindow.Release(); // don't hold onto this if we can't get told when it closes.
  158.             m_pCommandWindow = NULL;
  159.         }
  160.     }
  161.     else
  162.     {
  163.             m_pCommandWindow->SetFocus();
  164.     }
  165.     return S_OK;
  166. }
  167.  
  168.  
  169. HRESULT _stdcall CCmdWnd::OnClose()
  170. { // called when IShinc is closing down.
  171.     HRESULT hr = S_OK;
  172.  
  173.     m_pCommandWindow.Release();
  174.     m_pCommandWindow = NULL;
  175.     return(hr);
  176. }
  177.  
  178.  
  179.