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

  1. // Commands.cpp : implementation file
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "ReplAll.h"
  6. #include "Commands.h"
  7. #include "replacedlg.h"
  8.  
  9. #ifdef _DEBUG
  10. #define new DEBUG_NEW
  11. #undef THIS_FILE
  12. static char THIS_FILE[] = __FILE__;
  13. #endif
  14.  
  15. /////////////////////////////////////////////////////////////////////////////
  16. // CCommands
  17.  
  18. CCommands::CCommands()
  19. {
  20.     m_pApplication = NULL;
  21. }
  22.  
  23. CCommands::~CCommands()
  24. {
  25.     ASSERT (m_pApplication != NULL);
  26.     m_pApplication->Release();
  27. }
  28.  
  29. void CCommands::SetApplicationObject(IApplication* pApplication)
  30. {
  31.     // This function assumes pApplication has already been AddRef'd
  32.     //  for us, which CDSAddIn did in its QueryInterface call
  33.     //  just before it called us.
  34.     m_pApplication = pApplication;
  35.  
  36. }
  37.  
  38. /////////////////////////////////////////////////////////////////////////////
  39. // CCommands methods
  40.  
  41. STDMETHODIMP CCommands::ReplAllCommandMethod() 
  42. {
  43.     AFX_MANAGE_STATE(AfxGetStaticModuleState());
  44.  
  45.     VERIFY_OK(m_pApplication->EnableModeless(VARIANT_FALSE));
  46.     CReplaceDlg RepDlg;
  47.     CComPtr<IDispatch> pTextEditorDisp;
  48.     m_pApplication->get_TextEditor(&pTextEditorDisp);
  49.     CComQIPtr<ITextEditor, &IID_ITextEditor> pTextEditor(pTextEditorDisp);
  50.     pTextEditor->get_Emulation(&RepDlg.m_lEmulation);
  51.     CComPtr<IDispatch> pActiveDocumentDisp;
  52.     m_pApplication->get_ActiveDocument(&pActiveDocumentDisp);
  53.     CComQIPtr<ITextDocument, &IID_ITextDocument> 
  54.             pActiveDocument(pActiveDocumentDisp);
  55.     if (pActiveDocument)
  56.     {
  57.         CComPtr<IDispatch> pTextSelectionDisp;
  58.         pActiveDocument->get_Selection(&pTextSelectionDisp);
  59.         CComQIPtr<ITextSelection, &IID_ITextSelection> 
  60.                 pTextSelection(pTextSelectionDisp);
  61.         CComBSTR bstrText;
  62.         pTextSelection->get_Text(&bstrText);
  63.         RepDlg.m_strFindString = bstrText;
  64.     }
  65.     else
  66.         RepDlg.m_strFindString = _T("");
  67.     if (RepDlg.DoModal() == IDOK)
  68.     {
  69.         CComBSTR bstrFind = RepDlg.m_strFindString;
  70.         CComBSTR bstrReplace = RepDlg.m_strReplaceString;
  71.  
  72.         CComPtr<IDispatch> pDocsDisp;
  73.         m_pApplication->get_Documents(&pDocsDisp);
  74.         CComQIPtr<IDocuments, &IID_IDocuments> pDocs(pDocsDisp);
  75.  
  76.         short stmp = 0;
  77.          
  78.         if (RepDlg.m_bMatchCase == true)
  79.             stmp += dsMatchCase;
  80.         if (RepDlg.m_bRegularExpression == true)
  81.             stmp += dsMatchRegExpCur;
  82.         if (RepDlg.m_bMatchWholeWord == true)
  83.             stmp += dsMatchWord;
  84.  
  85.         CComVariant varFlags = stmp;
  86.  
  87.         long lDocCount;
  88.         pDocs->get_Count(&lDocCount);
  89.         for (long lIndex = 1 ; lIndex <= lDocCount ; lIndex++)
  90.         {
  91.             CComVariant varItemNum = lIndex;
  92.             CComPtr<IDispatch> pDocDisp;
  93.             pDocs->Item(varItemNum, &pDocDisp);
  94.             CComQIPtr<ITextDocument, &IID_ITextDocument> pTextDoc(pDocDisp);
  95.             VARIANT_BOOL varRetVal;
  96.             pTextDoc->ReplaceText(bstrFind, bstrReplace, varFlags, &varRetVal);
  97.         }
  98.     }
  99.  
  100.     VERIFY_OK(m_pApplication->EnableModeless(VARIANT_TRUE));
  101.     
  102.     return S_OK;
  103. }
  104.