home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / mfc / ole / drawcli / summinfo.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-03-27  |  9.3 KB  |  405 lines

  1. // summinfo.cpp : implementation of the CDrawDoc class
  2. //
  3. // This is a part of the Microsoft Foundation Classes C++ library.
  4. // Copyright (C) 1992-1998 Microsoft Corporation
  5. // All rights reserved.
  6. //
  7. // This source code is only intended as a supplement to the
  8. // Microsoft Foundation Classes Reference and related
  9. // electronic documentation provided with the library.
  10. // See these sources for detailed information regarding the
  11. // Microsoft Foundation Classes product.
  12.  
  13. #include "stdafx.h"
  14. #include <winnls.h>
  15.  
  16. #include "drawcli.h"
  17.  
  18. #ifdef _DEBUG
  19. #undef THIS_FILE
  20. static char BASED_CODE THIS_FILE[] = __FILE__;
  21. #endif
  22.  
  23. #include <objbase.h>
  24.  
  25. // the following header redefines
  26. // the DEFINE_GUID macro to actually allocate data
  27.  
  28. #include <initguid.h>
  29. #ifndef INITGUID
  30. #define INITGUID
  31. #endif
  32.  
  33. // the DEFINE_GUID macro in the following header now allocates data
  34. #include "summinfo.h"
  35.  
  36. const OLECHAR szSummInfo[] = OLESTR("\005SummaryInformation");
  37.  
  38. #if defined(_UNICODE)
  39. LPCSTR tcstocs(LPCTSTR lpctStr) {                       // typed char (WCHAR) to CHAR
  40.     static CHAR strTemp[1024];
  41.     wcstombs(strTemp, lpctStr, 1024);
  42.     return strTemp;
  43. }
  44. #else // !defined(_UNICODE)
  45. #define tcstocs(lpctStr) (LPCSTR)(lpctStr)
  46. #endif
  47.  
  48. CSummInfo::CSummInfo()
  49. {
  50.     m_propSet.SetFormatVersion(0);
  51.     DWORD dwOSVer;
  52.     dwOSVer = (DWORD)MAKELONG(LOWORD(GetVersion()), 2);
  53.     m_propSet.SetOSVersion(dwOSVer);
  54.     m_propSet.SetClassID(FMTID_SummaryInformation);
  55.     m_pSection = m_propSet.AddSection(FMTID_SummaryInformation);
  56.     UINT cp = GetACP();
  57.     m_pSection->Set(PID_CODEPAGE, (void*)&cp, VT_I2);
  58.     SetTitle(_T(""));
  59.     SetSubject(_T(""));
  60.     SetAuthor(_T(""));
  61.     SetKeywords(_T(""));
  62.     SetComments(_T(""));
  63.     SetTemplate(_T(""));
  64.     SetLastAuthor(_T(""));
  65.     m_pSection->Set(PIDSI_REVNUMBER, (void*)_T("0"), VT_LPSTR);
  66.     FILETIME zeroTime = {0L, 0L};
  67.     m_pSection->Set(PIDSI_EDITTIME, (void*)&zeroTime, VT_FILETIME);
  68.     m_pSection->Set(PIDSI_LASTPRINTED, (void*)&zeroTime, VT_FILETIME);
  69.     m_pSection->Set(PIDSI_LASTSAVE_DTM, (void*)&zeroTime, VT_FILETIME);
  70.     m_pSection->Set(PIDSI_CREATE_DTM, (void*)&zeroTime, VT_FILETIME);
  71.     SetNumPages(0);
  72.     SetNumWords(0);
  73.     SetNumChars(0);
  74.     SetAppname(_T(""));
  75.     SetSecurity(0);
  76. }
  77.  
  78. BOOL CSummInfo::SetTitle(LPCTSTR szTitle)
  79. {
  80.     return m_pSection->Set(PIDSI_TITLE, (void*)tcstocs(szTitle), VT_LPSTR);
  81. }
  82.  
  83. CString CSummInfo::GetTitle()
  84. {
  85.     return CString((LPCSTR)m_pSection->Get(PIDSI_TITLE));
  86. }
  87.  
  88. BOOL CSummInfo::SetSubject(LPCTSTR szSubject)
  89. {
  90.     return m_pSection->Set(PIDSI_SUBJECT,
  91.         (void*)tcstocs(szSubject), VT_LPSTR);
  92. }
  93.  
  94. CString CSummInfo::GetSubject()
  95. {
  96.     return CString((LPCSTR)m_pSection->Get(PIDSI_SUBJECT));
  97. }
  98.  
  99. BOOL CSummInfo::SetAuthor(LPCTSTR szAuthor)
  100. {
  101.     return m_pSection->Set(PIDSI_AUTHOR,
  102.         (void*)tcstocs(szAuthor), VT_LPSTR);
  103. }
  104.  
  105. CString CSummInfo::GetAuthor()
  106. {
  107.     return CString((LPCSTR)m_pSection->Get(PIDSI_AUTHOR));
  108. }
  109.  
  110. BOOL CSummInfo::SetKeywords(LPCTSTR szKeywords)
  111. {
  112.     return m_pSection->Set(PIDSI_KEYWORDS,
  113.         (void*)tcstocs(szKeywords), VT_LPSTR);
  114. }
  115.  
  116. CString CSummInfo::GetKeywords()
  117. {
  118.     return CString((LPCSTR)m_pSection->Get(PIDSI_KEYWORDS));
  119. }
  120.  
  121. BOOL CSummInfo::SetComments(LPCTSTR szComments)
  122. {
  123.     return m_pSection->Set(PIDSI_COMMENTS,
  124.         (void*)tcstocs(szComments), VT_LPSTR);
  125. }
  126.  
  127. CString CSummInfo::GetComments()
  128. {
  129.     return CString((LPCSTR)m_pSection->Get(PIDSI_COMMENTS));
  130. }
  131.  
  132. BOOL CSummInfo::SetTemplate(LPCTSTR szTemplate)
  133. {
  134.     return m_pSection->Set(PIDSI_TEMPLATE,
  135.         (void*)tcstocs(szTemplate), VT_LPSTR);
  136. }
  137.  
  138. CString CSummInfo::GetTemplate()
  139. {
  140.     return CString((LPCSTR)m_pSection->Get(PIDSI_TEMPLATE));
  141. }
  142.  
  143. BOOL CSummInfo::SetLastAuthor(LPCTSTR szLastAuthor)
  144. {
  145.     return m_pSection->Set(PIDSI_LASTAUTHOR,
  146.         (void*)tcstocs(szLastAuthor), VT_LPSTR);
  147. }
  148.  
  149. CString CSummInfo::GetLastAuthor()
  150. {
  151.     return CString((LPCSTR)m_pSection->Get(PIDSI_LASTAUTHOR));
  152. }
  153.  
  154. BOOL CSummInfo::IncrRevNum()
  155. {
  156.     ULONG count;
  157.     _stscanf((LPCTSTR)GetRevNum(), _T("%lu"), &count);
  158.     count++;
  159.     TCHAR buff[20];
  160.     _stprintf(buff, _T("%lu"), count);
  161.     return m_pSection->Set(PIDSI_REVNUMBER, (void*)buff, VT_LPSTR);
  162. }
  163.  
  164. CString CSummInfo::GetRevNum()
  165. {
  166.     return CString((LPCSTR)m_pSection->Get(PIDSI_REVNUMBER));
  167. }
  168.  
  169. void CSummInfo::StartEditTimeCount()
  170. {
  171.     FILETIME now;
  172.     CoFileTimeNow(&now);
  173.     startEdit = *(__int64*)&now;
  174. }
  175.  
  176. BOOL CSummInfo::AddCountToEditTime()
  177. {
  178.     FILETIME now;
  179.     CoFileTimeNow(&now);
  180.     __int64 currTime = *(__int64*)&now;
  181.     __int64 thisSession = currTime - startEdit;
  182.     __int64 lastTotal = *(__int64*)m_pSection->Get(PIDSI_EDITTIME);
  183.     __int64 newTotal = lastTotal + thisSession;
  184.     return m_pSection->Set(PIDSI_EDITTIME, (void*)&newTotal, VT_FILETIME);
  185. }
  186.  
  187. CString CSummInfo::GetEditTime()
  188. {
  189.     FILETIME now;
  190.     CoFileTimeNow(&now);
  191.     __int64 currTime = *(__int64*)&now;
  192.     __int64 thisSession = currTime - startEdit;
  193.     __int64 lastTotal = *(__int64*)m_pSection->Get(PIDSI_EDITTIME);
  194.     __int64 newTotal = lastTotal + thisSession;
  195.     ULONG editMinutes = (ULONG)(newTotal/600000000);
  196.  
  197.     TCHAR buff[20];
  198.     _stprintf(buff, _T("%lu min"), editMinutes);
  199.     return CString(buff);
  200. }
  201.  
  202. BOOL CSummInfo::RecordPrintDate()
  203. {
  204.     FILETIME printDate;
  205.     CoFileTimeNow(&printDate);
  206.     return m_pSection->Set(PIDSI_LASTPRINTED,
  207.         (void*)&printDate, VT_FILETIME);
  208. }
  209.  
  210. CString CSummInfo::GetLastPrintDate()
  211. {
  212.     FILETIME* pPrintDate = (FILETIME*)m_pSection->Get(PIDSI_LASTPRINTED);
  213.     if ((pPrintDate == NULL) ||
  214.         ((pPrintDate->dwLowDateTime == 0L) &&
  215.          (pPrintDate->dwHighDateTime == 0L)  ))
  216.         return CString(_T(""));
  217.     else
  218.     {
  219.         COleDateTime tempDate = *pPrintDate;
  220.         return tempDate.Format();
  221.     }
  222. }
  223.  
  224. BOOL CSummInfo::RecordCreateDate()
  225. {
  226.     FILETIME createDate;
  227.     CoFileTimeNow(&createDate);
  228.     return m_pSection->Set(PIDSI_CREATE_DTM,
  229.         (void*)&createDate, VT_FILETIME);
  230. }
  231.  
  232. CString CSummInfo::GetCreateDate()
  233. {
  234.     FILETIME* pCreateDate = (FILETIME*)m_pSection->Get(PIDSI_CREATE_DTM);
  235.     if ((pCreateDate == NULL) ||
  236.         ((pCreateDate->dwLowDateTime == 0L) &&
  237.          (pCreateDate->dwHighDateTime == 0L)  ))
  238.         return CString(_T(""));
  239.     else
  240.     {
  241.         COleDateTime tempDate = *pCreateDate;
  242.         return tempDate.Format();
  243.     }
  244. }
  245.  
  246. BOOL CSummInfo::RecordSaveDate()
  247. {
  248.     FILETIME saveDate;
  249.     CoFileTimeNow(&saveDate);
  250.     return m_pSection->Set(PIDSI_LASTSAVE_DTM,
  251.         (void*)&saveDate, VT_FILETIME);
  252. }
  253.  
  254. CString CSummInfo::GetLastSaveDate()
  255. {
  256.     FILETIME *pSaveDate = (FILETIME*)m_pSection->Get(PIDSI_LASTSAVE_DTM);
  257.     if ((pSaveDate == NULL) ||
  258.         ((pSaveDate->dwLowDateTime == 0L) &&
  259.          (pSaveDate->dwHighDateTime == 0L)  ))
  260.         return CString(_T(""));
  261.     else
  262.     {
  263.         COleDateTime tempDate = *pSaveDate;
  264.         return tempDate.Format();
  265.     }
  266. }
  267.  
  268. BOOL CSummInfo::SetNumPages(ULONG nPages)
  269. {
  270.     return m_pSection->Set(PIDSI_PAGECOUNT, (void*)&nPages, VT_I4);
  271. }
  272.  
  273. CString CSummInfo::GetNumPages()
  274. {
  275.     TCHAR buff[20];
  276.     ULONG* pNumPages = (ULONG*)m_pSection->Get(PIDSI_PAGECOUNT);
  277.     if (pNumPages != NULL)
  278.     {
  279.         _stprintf(buff, _T("%lu"), *pNumPages);
  280.         return CString(buff);
  281.     }
  282.     else
  283.         return CString(_T(""));
  284. }
  285.  
  286. BOOL CSummInfo::SetNumWords(ULONG nWords)
  287. {
  288.     return m_pSection->Set(PIDSI_WORDCOUNT, (void*)&nWords, VT_I4);
  289. }
  290.  
  291. CString CSummInfo::GetNumWords()
  292. {
  293.     TCHAR buff[20];
  294.     ULONG* pNumWords = (ULONG*)m_pSection->Get(PIDSI_WORDCOUNT);
  295.     if (pNumWords != NULL)
  296.     {
  297.         _stprintf(buff, _T("%lu"), *pNumWords);
  298.         return CString(buff);
  299.     }
  300.     else
  301.         return CString(_T(""));
  302. }
  303.  
  304. BOOL CSummInfo::SetNumChars(ULONG nChars)
  305. {
  306.     return m_pSection->Set(PIDSI_CHARCOUNT, (void*)&nChars, VT_I4);
  307. }
  308.  
  309. CString CSummInfo::GetNumChars()
  310. {
  311.     TCHAR buff[20];
  312.     ULONG* pNumChars = (ULONG*)m_pSection->Get(PIDSI_CHARCOUNT);
  313.     if (pNumChars != NULL)
  314.     {
  315.         _stprintf(buff, _T("%lu"), *pNumChars);
  316.         return CString(buff);
  317.     }
  318.     else
  319.         return CString(_T(""));
  320. }
  321.  
  322. BOOL CSummInfo::SetAppname(LPCTSTR szAppname)
  323. {
  324.     return m_pSection->Set(PIDSI_APPNAME,
  325.         (void*)tcstocs(szAppname), VT_LPSTR);
  326. }
  327.  
  328. CString CSummInfo::GetAppname()
  329. {
  330.     return CString((LPCSTR)m_pSection->Get(PIDSI_APPNAME));
  331. }
  332.  
  333. BOOL CSummInfo::SetSecurity(ULONG nLevel)
  334. {
  335.     return m_pSection->Set(PID_SECURITY, (void*)&nLevel, VT_I4);
  336. }
  337.  
  338. CString CSummInfo::GetSecurity()
  339. {
  340.     TCHAR buff[20];
  341.     ULONG* pSecurity = (ULONG*)m_pSection->Get(PID_SECURITY);
  342.     if (pSecurity != NULL)
  343.     {
  344.         _stprintf(buff, _T("%lu"), *pSecurity);
  345.         return CString(buff);
  346.     }
  347.     else
  348.         return CString(_T(""));
  349. }
  350.  
  351. BOOL CSummInfo::WriteToStorage(LPSTORAGE lpRootStg)
  352. {
  353.     if (lpRootStg != NULL)
  354.     {
  355.         LPSTREAM lpStream = NULL;
  356.         if (FAILED(lpRootStg->CreateStream(szSummInfo,
  357.                    STGM_SHARE_EXCLUSIVE|STGM_CREATE|STGM_READWRITE,
  358.                    0, 0, &lpStream)))
  359.         {
  360.             TRACE(_T("CreateStream failed\n"));
  361.             return FALSE;
  362.         }
  363.         else
  364.         {
  365.             if(!m_propSet.WriteToStream(lpStream))
  366.             {
  367.                 TRACE(_T("WriteToStream failed\n"));
  368.                 return FALSE;
  369.             }
  370.             lpRootStg->Commit(STGC_DEFAULT);
  371.             lpStream->Release();
  372.             return TRUE;
  373.         }
  374.     }
  375.     return FALSE;
  376. }
  377.  
  378. BOOL CSummInfo::ReadFromStorage(LPSTORAGE lpRootStg)
  379. {
  380.     if (lpRootStg != NULL)
  381.     {
  382.         LPSTREAM lpStream = NULL;
  383.  
  384.         if (FAILED(lpRootStg->OpenStream(szSummInfo,
  385.                    NULL, STGM_SHARE_EXCLUSIVE|STGM_READ,
  386.                    0, &lpStream)))
  387.         {
  388.             TRACE(_T("OpenStream failed\n"));
  389.             return FALSE;
  390.         }
  391.         else
  392.         {
  393.             if (!m_propSet.ReadFromStream(lpStream))
  394.             {
  395.                 TRACE(_T("ReadFromStream failed\n"));
  396.                 return FALSE;
  397.             }
  398.             m_pSection = m_propSet.GetSection(FMTID_SummaryInformation);
  399.             lpStream->Release();
  400.             return TRUE;
  401.         }
  402.     }
  403.     return FALSE;
  404. }
  405.