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

  1. // Commands.cpp : implementation file
  2. //
  3. // Copyright (C) 1992-1998 Microsoft Corporation
  4. // All rights reserved.
  5.  
  6.  
  7. #include "stdafx.h"
  8. #include "API2Help.h"
  9. #include "Commands.h"
  10. #include "Welcome.h"
  11.  
  12. #ifdef _DEBUG
  13. #define new DEBUG_NEW
  14. #undef THIS_FILE
  15. static char THIS_FILE[] = __FILE__;
  16. #endif
  17.  
  18. /////////////////////////////////////////////////////////////////////////////
  19. // CCommands
  20.  
  21. CCommands::CCommands()
  22. {
  23.     m_pApplication = NULL;
  24. }
  25.  
  26. CCommands::~CCommands()
  27. {
  28.     ASSERT (m_pApplication != NULL);
  29.     m_pApplication->Release();
  30. }
  31.  
  32. void CCommands::SetApplicationObject(IApplication* pApplication)
  33. {
  34.     // This function assumes pApplication has already been AddRef'd
  35.     //  for us, which CDSAddIn did in its QueryInterface call
  36.     //  just before it called us.
  37.     m_pApplication = pApplication;
  38. }
  39.  
  40.  
  41. /////////////////////////////////////////////////////////////////////////////
  42. // CCommands methods
  43.  
  44. STDMETHODIMP CCommands::API2HelpCommandMethod()
  45. {
  46.     AFX_MANAGE_STATE(AfxGetStaticModuleState());
  47.  
  48.     VERIFY_OK(m_pApplication->EnableModeless(VARIANT_FALSE));
  49.     CComBSTR bStr;
  50.     CComPtr<IDispatch> lpDispActDoc, lpDispSel;
  51.     VERIFY_OK(m_pApplication->get_ActiveDocument(&lpDispActDoc));
  52.     if (lpDispActDoc == NULL)
  53.         AfxMessageBox(IDS_NODOC_WARN);
  54.     else
  55.     {
  56.         CComQIPtr<ITextDocument, &IID_ITextDocument> lpActDoc(lpDispActDoc);
  57.         if (FAILED(lpActDoc->get_Selection(&lpDispSel)))
  58.             AfxMessageBox(IDS_NOTEXTSEL);
  59.         else
  60.         {
  61.             CComQIPtr<ITextSelection, &IID_ITextSelection> lpSel(lpDispSel);
  62.             if (FAILED(lpSel->get_Text(&bStr)))
  63.                 AfxMessageBox(IDS_COULDNOTRETRIEVE);
  64.             else
  65.             {
  66.                 CString cStr = bStr;
  67.                 if (cStr == "")
  68.                     AfxMessageBox(IDS_NODOC_WARN);
  69.                 else
  70.                 {
  71.                     CWelcome WelcomeDlg(cStr, NULL);
  72.                     WelcomeDlg.DoModal();
  73.                 }
  74.             }
  75.         }
  76.     }
  77.     VERIFY_OK(m_pApplication->EnableModeless(VARIANT_TRUE));
  78.     return S_OK;
  79. }
  80.