home *** CD-ROM | disk | FTP | other *** search
/ BUG 15 / BUGCD1998_06.ISO / aplic / recent / recent.exe / Chooser.cpp next >
C/C++ Source or Header  |  1998-04-16  |  3KB  |  108 lines

  1. #include "stdafx.h"
  2. #include "Recent.h"
  3. #include "Chooser.h"
  4. #include "FilesDlg.h"
  5. #include <ObjModel\appguid.h>
  6.  
  7. HRESULT Chooser::OnConnection(IApplication* pApp, VARIANT_BOOL bFirstTime, long dwAddInID, VARIANT_BOOL* bOnConnection)
  8. {
  9.     HRESULT hr = S_OK;
  10.     m_spApplication = pApp;
  11.     m_dwAddInID = dwAddInID;
  12.  
  13.     hr = pApp->SetAddInInfo(reinterpret_cast<long>(_Module.GetModuleInstance()),
  14.         static_cast<IDispatch*>(this), IDB_TOOLBAR_MEDIUM_CHOOSER, IDB_TOOLBAR_LARGE_CHOOSER, dwAddInID);
  15.  
  16.     if(SUCCEEDED(hr))
  17.     {
  18.         VARIANT_BOOL bRet;
  19.         hr = pApp->AddCommand(CComBSTR(_T("ChooseRecentFile\nRecent File\nChoose recent file\nRecent File")),
  20.             CComBSTR(_T("ChooseRecentFile")), 0, dwAddInID, &bRet);
  21.         if(bFirstTime && SUCCEEDED(hr))
  22.             hr = pApp->AddCommandBarButton(dsGlyph, CComBSTR(_T("ChooseRecentFile")), dwAddInID);
  23.     }
  24.     if(SUCCEEDED(hr))
  25.     {
  26.         VARIANT_BOOL bRet;
  27.         hr = pApp->AddCommand(CComBSTR(_T("ChooseRecentWorkspace\nRecent Workspace\nChoose recent workspace\nRecent Workspace")),
  28.             CComBSTR(_T("ChooseRecentWorkspace")), 1, dwAddInID, &bRet);
  29.         if(bFirstTime && SUCCEEDED(hr))
  30.             hr = pApp->AddCommandBarButton(dsGlyph, CComBSTR(_T("ChooseRecentWorkspace")), dwAddInID);
  31.     }
  32.  
  33. #pragma warning(disable:4310)
  34.     *bOnConnection = SUCCEEDED(hr) ? VARIANT_TRUE : VARIANT_FALSE;
  35. #pragma warning(default:4310)
  36.     return hr;
  37. }
  38.  
  39. HRESULT Chooser::OnDisconnection(VARIANT_BOOL /*bLastTime*/)
  40. {
  41.     m_spApplication.Release();
  42.     return S_OK;
  43. }
  44.  
  45. HRESULT Chooser::ChooseRecentFile()
  46. {
  47.     return ChooseRecent(_T("Open Recent File"), _T("File"));
  48. }
  49.  
  50. HRESULT Chooser::ChooseRecentWorkspace()
  51. {
  52.     return ChooseRecent(_T("Open Recent Workspace"), _T("Project"), _T(".dsw"));
  53. }
  54.  
  55. HRESULT Chooser::ChooseRecent(const TCHAR* title, const TCHAR* prefix, const TCHAR* append)
  56. {
  57.     m_spApplication->EnableModeless(VARIANT_FALSE);
  58.  
  59.     FilesDlg dlg(title);
  60.     if(!FillList(dlg.files, prefix))
  61.         return E_FAIL;
  62.     HRESULT hr = S_OK;
  63.     if(dlg.DoModal() == IDOK)
  64.     {
  65.         CComPtr<IDispatch> dispDocs;
  66.         hr = m_spApplication->get_Documents(&dispDocs);
  67.         if(SUCCEEDED(hr))
  68.         {
  69.             _ASSERT(dispDocs != NULL);
  70.             CComQIPtr<IDocuments, &IID_IDocuments> docs(dispDocs);
  71.             _ASSERT(docs != NULL);
  72.             CComPtr<IDispatch> doc;
  73.             tstring name = dlg.selected;
  74.             if(append != NULL)
  75.                 name += append;
  76.             hr = docs->Open(CComBSTR(name.data()), CComVariant(_T("Auto")), CComVariant(false), &doc);
  77.         }
  78.     }
  79.  
  80. #pragma warning(disable:4310)
  81.     m_spApplication->EnableModeless(VARIANT_TRUE);
  82. #pragma warning(default:4310)
  83.     return hr;
  84. }
  85.  
  86. bool Chooser::FillList(list<tstring>& files, const tstring& prefix)
  87. {
  88.     files.clear();
  89.     CRegKey rk;
  90.     if(rk.Open(HKEY_CURRENT_USER, _T("Software\\Microsoft\\DevStudio\\5.0\\Recent File List"), KEY_QUERY_VALUE) != ERROR_SUCCESS)
  91.         return false;
  92.  
  93.     DWORD count;
  94.     if(rk.QueryValue(count, (prefix + _T("Count")).data()) != ERROR_SUCCESS)
  95.         return false;
  96.  
  97.     for(DWORD i = 1; i <= count; ++i)
  98.     {
  99.         TCHAR index[32];
  100.         _ultot(i, index, 10);
  101.         TCHAR value[1024];
  102.         DWORD size = sizeof(value)/sizeof(TCHAR);
  103.         if(rk.QueryValue(value, (prefix + index).data(), &size) == ERROR_SUCCESS)
  104.             files.push_back(value);
  105.     }
  106.     return true;
  107. }
  108.