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

  1. // MarkIt.cpp : Implementation of CMarkIt
  2. #include "stdafx.h"
  3. #include "Booknote.h"
  4. #include "MarkIt.h"
  5. #include "dlgnote.h"
  6.  
  7. CString strMainKey = _T("Software\\Microsoft\\devstudio\\6.0\\AddIns\\Booknote.MarkIt.1\\");
  8.  
  9. /////////////////////////////////////////////////////////////////////////////
  10. // CMarkIt
  11. CMarkIt::CMarkIt()
  12. {
  13.     CRegKey regKey;
  14.     long lRes;
  15.     unsigned long lSize;
  16.  
  17.     lRes = regKey.Create(HKEY_CURRENT_USER, strMainKey);
  18.     regKey.QueryValue(m_strFile.GetBuffer(_MAX_PATH), _T("LogFile"), &lSize);
  19.     m_strFile.ReleaseBuffer();
  20.     if (m_strFile.IsEmpty())
  21.     {
  22.         m_strFile = _T("c:\\booklog.txt");
  23.     }
  24. }
  25.  
  26. CMarkIt::~CMarkIt()
  27. {
  28.     CRegKey regKey;
  29.     long lRes;
  30.  
  31.     lRes = regKey.Create(HKEY_CURRENT_USER, strMainKey);
  32.     regKey.SetValue(m_strFile, _T("LogFile"));
  33. }
  34.  
  35.  
  36. HRESULT CMarkIt::OnConnection(IApplication* pApp, VARIANT_BOOL bFirstTime, long dwAddInID, VARIANT_BOOL* bOnConnection)
  37. {
  38.     HRESULT hr = S_OK;
  39.     m_spApplication = pApp;
  40.     m_dwAddInID = dwAddInID;
  41.  
  42.     hr = pApp->SetAddInInfo((long)_Module.GetModuleInstance(), 
  43.         static_cast<IMarkIt*>(this), IDB_TOOLBAR_MEDIUM_MARKIT, IDB_TOOLBAR_LARGE_MARKIT, dwAddInID);
  44.     LPCTSTR szCommand = _T("BookNote");
  45.     VARIANT_BOOL bRet;
  46.     if (SUCCEEDED(hr))
  47.     {
  48.         hr = pApp->AddCommand(CComBSTR(_T("BookNote\nBookNote\nBookNote\nBookNote")),CComBSTR(_T("BookNote")), 0, dwAddInID, &bRet);
  49.     }
  50.  
  51.     // Add toolbar buttons only if this is the first time the add-in
  52.     // is being loaded.  Toolbar buttons are automatically remembered
  53.     // by Developer Studio from session to session, so we should only
  54.     // add the toolbar buttons once.
  55.     if (bFirstTime)
  56.     {
  57.         if (SUCCEEDED(hr))
  58.         {
  59.             hr = pApp->AddCommandBarButton(dsGlyph, CComBSTR(_T("BookNote")), dwAddInID);
  60.         }
  61.     }
  62.  
  63.     *bOnConnection = SUCCEEDED(hr) ? VARIANT_TRUE :VARIANT_FALSE;
  64.     return hr;
  65. }
  66.  
  67. HRESULT CMarkIt::OnDisconnection(VARIANT_BOOL bLastTime)
  68. {
  69.     return S_OK;
  70. }
  71.  
  72. HRESULT CMarkIt::BookNote()
  73. {
  74.     AFX_MANAGE_STATE(AfxGetStaticModuleState());
  75.     m_spApplication->EnableModeless(VARIANT_FALSE);
  76.     CComQIPtr<ITextDocument, &IID_ITextDocument> pDocument;
  77.     CComPtr<IDispatch> pDispDocument;
  78.     CDlgNote dlgNote;
  79.     int iRes;
  80.     CTime timeNow;
  81.     CComBSTR bstrTime;
  82.  
  83.     m_spApplication->get_ActiveDocument(&pDispDocument);
  84.     pDocument = pDispDocument;
  85.     pDispDocument = NULL;
  86.     if (pDocument)
  87.     {
  88.         CComBSTR bstrName;
  89.         pDocument->get_FullName(&bstrName);
  90.         CComQIPtr<ITextSelection, &IID_ITextSelection> pSelection;
  91.         CComPtr<IDispatch> pDispSelection;
  92.         pDocument->get_Selection(&pDispSelection);
  93.         pSelection = pDispSelection;
  94.         pDispSelection = NULL;
  95.         if (pSelection)
  96.         {
  97.             long col, line;
  98.             CComBSTR bstr(2048,LPCSTR(" "));
  99.             CComBSTR bstrNote;
  100.             HRESULT hr;
  101.             hr = pSelection->get_CurrentColumn(&col);
  102.             hr =  pSelection->get_CurrentLine(&line);
  103.  
  104.             dlgNote.m_strLogFile = m_strFile;
  105.             dlgNote.SetMarkIt(this);
  106.             iRes = dlgNote.DoModal();
  107.             if (iRes == IDOK)
  108.             {
  109.                 m_strFile = dlgNote.m_strLogFile;
  110.                 // produce output like "c:\test\foo.cpp(37) : warning C4310: cast truncates constant value"
  111.                 bstrNote = dlgNote.m_strNote;
  112.                 timeNow = CTime::GetCurrentTime();
  113.                 bstrTime = timeNow.Format(_T("%I:%M:%S %p  %d/%m/%Y"));
  114.                 swprintf(bstr,L"%s(%d): (%s) %s", bstrName, line, bstrTime, bstrNote);
  115.                 if (dlgNote.m_fEcho)
  116.                 {
  117.                     m_spApplication->PrintToOutputWindow(bstr);
  118.                 }
  119.  
  120.                 CString strOut;
  121.                 strOut = bstr;
  122.                 CStdioFile log;
  123.                 try
  124.                 {
  125.                     if (!dlgNote.m_strLogFile.IsEmpty() && log.Open(dlgNote.m_strLogFile, CFile::modeNoTruncate | CFile::modeWrite | CFile::modeCreate))
  126.                     {
  127.                         log.SeekToEnd();
  128.                         strOut += _T("\n");
  129.                         log.WriteString(strOut);
  130.                         log.Close();
  131.                     }
  132.                 }
  133.                 catch (CFileException *eFile)
  134.                 {
  135.                     CString strMsg;
  136.                     CString strErr;
  137.                     eFile->GetErrorMessage(strErr.GetBuffer(_MAX_PATH), _MAX_PATH);
  138.                     strErr.ReleaseBuffer();
  139.                     strMsg.Format("%s \n    %s", strErr, dlgNote.m_strLogFile);
  140.                     MessageBox(NULL, strMsg, _T("BOOKNOTE"), MB_OK);
  141.                     eFile->Delete();
  142.                 }
  143.             }
  144.  
  145.         }
  146.     }
  147.     m_spApplication->EnableModeless((VARIANT_BOOL)VARIANT_TRUE);
  148.     return S_OK;
  149. }
  150.  
  151.  
  152. HRESULT CMarkIt::Dump(LPCTSTR szFile)
  153. {
  154.     CStdioFile file;
  155.     CString str;
  156.     HRESULT hr = E_FAIL;
  157.  
  158.     if (file.Open(szFile, CFile::modeRead))
  159.     { 
  160.         while (file.ReadString(str))
  161.         {
  162.             CComBSTR bstr = str;
  163.             hr = m_spApplication->PrintToOutputWindow(bstr);
  164.         }
  165.     }
  166.     return (hr);
  167. }
  168.