home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c480 / 19.ddi / MFC / SAMPLES / CHKBOOK / CHECKDOC.CP_ / CHECKDOC.CP
Encoding:
Text File  |  1993-02-08  |  7.6 KB  |  292 lines

  1. // checkdoc.cpp : implementation of the CChkBookDoc class
  2. //
  3. // This is a part of the Microsoft Foundation Classes C++ library.
  4. // Copyright (C) 1992 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 Microsoft
  9. // QuickHelp and/or WinHelp documentation provided with the library.
  10. // See these sources for detailed information regarding the
  11. // Microsoft Foundation Classes product.
  12.  
  13.  
  14. #include "stdafx.h"
  15. #include "chkbook.h"
  16.  
  17. #ifdef _DEBUG
  18. #undef THIS_FILE
  19. static char BASED_CODE THIS_FILE[] = __FILE__;
  20. #endif
  21.  
  22. #define FIRST_CHECK_NO 101
  23. #define CHECK_BOOK_FILE_SIGNATURE 0x6f7e471d
  24.  
  25. /////////////////////////////////////////////////////////////////////////////
  26. // CChkBookDoc
  27. //
  28.  
  29. IMPLEMENT_DYNCREATE(CChkBookDoc, CDocument)
  30.  
  31. BEGIN_MESSAGE_MAP(CChkBookDoc, CDocument)
  32.     //{{AFX_MSG_MAP(CChkBookDoc)
  33.     ON_COMMAND(ID_EDIT_NEW_CHECK, NewCheck)
  34.     ON_COMMAND(ID_NEXT_CHECK, OnNextCheck)
  35.     ON_UPDATE_COMMAND_UI(ID_NEXT_CHECK, OnUpdateNextCheck)
  36.     ON_COMMAND(ID_PREV_CHECK, OnPrevCheck)
  37.     ON_UPDATE_COMMAND_UI(ID_PREV_CHECK, OnUpdatePrevCheck)
  38.     //}}AFX_MSG_MAP
  39. END_MESSAGE_MAP()
  40.  
  41. /////////////////////////////////////////////////////////////////////////////
  42. // CChkBookDoc construction/destruction
  43.  
  44. CChkBookDoc::CChkBookDoc()
  45. {
  46.     m_header.nRecordLength = sizeof(m_record);
  47.     m_header.nExtraHeaderLength = sizeof(m_extraHeader);
  48.     m_header.nRecordCount = 0;
  49.     m_nActiveRecord = 0;
  50.     m_extraHeader.nFirstCheckNo = FIRST_CHECK_NO;
  51.     m_extraHeader.dwFileSignature = CHECK_BOOK_FILE_SIGNATURE;
  52. }
  53.  
  54. CChkBookDoc::~CChkBookDoc()
  55. {
  56. }
  57.  
  58. /////////////////////////////////////////////////////////////////////////////
  59. // Overrides of CFixedLenRecDoc and CDocument
  60.  
  61. BOOL CChkBookDoc::OnOpenDocument(const char* pszPathName)
  62. {
  63.     // Upon opening the document, tell the application object
  64.     // to save the path name in the private INI file.
  65.  
  66.     if (!CFixedLenRecDoc::OnOpenDocument(pszPathName))
  67.         return FALSE;
  68.     UpdateIniFileWithDocPath(pszPathName);
  69.     m_nActiveRecord = 0;
  70.     return TRUE;
  71. }
  72.  
  73. BOOL CChkBookDoc::OnSaveDocument(const char* pszPathName)
  74. {
  75.     // Upon saving the document, tell the application object
  76.     // to save the path name in the private INI file.
  77.  
  78.     if (!CFixedLenRecDoc::OnSaveDocument(pszPathName))
  79.         return FALSE;
  80.     UpdateIniFileWithDocPath(pszPathName);
  81.     return TRUE;
  82. }
  83.  
  84. BOOL CChkBookDoc::SaveModified()
  85. {
  86.     // If the user has been editing a check in the check view
  87.     // but hasn't commited it yet, ask her whether she wants to
  88.     // commit the check.  She might response with 'Cancel', in
  89.     // which case MaybeCommitDirtyCheck() returns FALSE, which
  90.     // SaveModified() in turn returns to cancel the File Close.
  91.  
  92.     return MaybeCommitDirtyCheck();
  93. }
  94.  
  95. void CChkBookDoc::UpdateIniFileWithDocPath(const char* pszPathName)
  96. {
  97.     theApp.UpdateIniFileWithDocPath(pszPathName);
  98. }
  99.  
  100. void* CChkBookDoc::OnCreateNewRecord(int nNewRecordIndex)
  101. {
  102.     // The base class CFixedLenRecDoc calls this override to
  103.     // format a new record (in memory).
  104.  
  105.     DWORD dwCents = 0L;
  106.     char date[9];
  107.     _strdate(date);
  108.     CString strDate(date);
  109.     CString strPayTo("");
  110.     CString strMemo("");
  111.     PackRecord(dwCents, strPayTo, strDate, strMemo);
  112.     m_nActiveRecord = nNewRecordIndex;
  113.     return &m_record;
  114. }
  115.  
  116. BOOL CChkBookDoc::OnReadExtraHeader()
  117. {
  118.     // Read the ChkBook-specific portion of the file header,
  119.     // and verify the file signature to make sure we're not
  120.     // reading a non-ChkBook file.
  121.  
  122.     if (m_file.Read(&m_extraHeader, sizeof(m_extraHeader))
  123.         < sizeof(m_extraHeader))
  124.         return FALSE;
  125.     return (m_extraHeader.dwFileSignature == CHECK_BOOK_FILE_SIGNATURE);
  126. }
  127.  
  128. void CChkBookDoc::OnWriteExtraHeader(BOOL bNewHeader)
  129. {
  130.     m_file.Write(&m_extraHeader, sizeof(m_extraHeader));
  131.  
  132.     // If this is a new header (that is, if the first is first being
  133.     // created), then create the first record.
  134.  
  135.     if (bNewHeader)
  136.         CreateNewRecord();
  137.  
  138. }
  139.  
  140. /////////////////////////////////////////////////////////////////////////////
  141. // Operations, called by book view and check view
  142.  
  143. void CChkBookDoc::GetCheck(UINT nCheckNo, DWORD& dwCents, CString& strPayTo,
  144.         CString& strDate, CString& strMemo)
  145. {
  146.     UINT nRecord = CheckNoToRecordIndex(nCheckNo);
  147.     GetRecord(nRecord, &m_record);
  148.     ParseRecord(dwCents, strPayTo, strDate, strMemo);
  149. }
  150.  
  151.  
  152. void CChkBookDoc::UpdateCheck(CView* pSourceView, UINT nCheckNo,
  153.         DWORD dwCents, const char* szPayTo, const char* szDate, 
  154.         const char* szMemo)
  155. {
  156.     UINT nRecord = CheckNoToRecordIndex(nCheckNo);
  157.     PackRecord(dwCents, szPayTo, szDate, szMemo);
  158.     UpdateRecord(pSourceView, nRecord, &m_record);
  159. }
  160.  
  161. void CChkBookDoc::ChangeSelectionNextCheckNo(BOOL bNext)
  162. {
  163.     if (bNext)
  164.     {
  165.         if (m_nActiveRecord < (GetRecordCount() - 1))
  166.         {
  167.             if (!MaybeCommitDirtyCheck())
  168.                 return;
  169.             UpdateAllViewsWithRecord(NULL, ++m_nActiveRecord);
  170.         }
  171.     }
  172.     else
  173.     {
  174.         if (m_nActiveRecord > 0)
  175.         {
  176.             if (!MaybeCommitDirtyCheck())
  177.                 return;
  178.             UpdateAllViewsWithRecord(NULL, --m_nActiveRecord);
  179.         }
  180.     }
  181. }
  182.  
  183. void CChkBookDoc::ChangeSelectionToCheckNo(UINT nNewActiveCheckNo)
  184. {
  185.     if (!MaybeCommitDirtyCheck())
  186.         return;
  187.     m_nActiveRecord = CheckNoToRecordIndex(nNewActiveCheckNo);
  188.     UpdateAllViewsWithRecord(NULL, m_nActiveRecord);
  189. }
  190.  
  191. BOOL CChkBookDoc::MaybeCommitDirtyCheck()
  192. {
  193.     CView* pView;
  194.     POSITION pos = GetFirstViewPosition();
  195.     while (pos)
  196.     {
  197.         pView = GetNextView(pos);
  198.         if (pView->IsKindOf(RUNTIME_CLASS(CCheckView)))
  199.             return ((CCheckView*)pView)->MaybeCommitDirtyCheck();
  200.     }
  201.     return TRUE;
  202. }
  203.  
  204. /////////////////////////////////////////////////////////////////////////////
  205. // Implementation
  206.  
  207. void CChkBookDoc::PackRecord(DWORD dwCents, const char* szPayTo,
  208.             const char* szDate, const char* szMemo)
  209. {
  210.     m_record.dwCents = dwCents;
  211.     strncpy(m_record.szPayTo, szPayTo, sizeof(m_record.szPayTo) - 1);
  212.     m_record.szPayTo[sizeof(m_record.szPayTo) - 1] = 0;
  213.     strncpy(m_record.szDate, szDate, sizeof(m_record.szDate) - 1);
  214.     m_record.szDate[sizeof(m_record.szDate) - 1] = 0;
  215.     strncpy(m_record.szMemo, szMemo, sizeof(m_record.szMemo) - 1);
  216.     m_record.szMemo[sizeof(m_record.szMemo) - 1] = 0;
  217.  
  218. }
  219.  
  220. void CChkBookDoc::ParseRecord(DWORD& dwCents, CString& strPayTo,
  221.             CString& strDate, CString& strMemo)
  222. {
  223.     dwCents = m_record.dwCents;
  224.     strPayTo = m_record.szPayTo;
  225.     strDate = m_record.szDate;
  226.     strMemo = m_record.szMemo;
  227. }
  228.  
  229. UINT CChkBookDoc::CheckNoToRecordIndex(UINT nCheckNo)
  230. {
  231.     return (nCheckNo - m_extraHeader.nFirstCheckNo);
  232. }
  233.  
  234. UINT CChkBookDoc::RecordIndexToCheckNo(UINT nRecordIndex)
  235. {
  236.     return (nRecordIndex + m_extraHeader.nFirstCheckNo);
  237. }
  238.  
  239. UINT CChkBookDoc::GetActiveCheckNo()
  240. {
  241.     return (m_nActiveRecord + m_extraHeader.nFirstCheckNo);
  242. }
  243.  
  244. UINT CChkBookDoc::GetFirstCheckNo()
  245. {
  246.     return m_extraHeader.nFirstCheckNo;
  247. }
  248.  
  249. UINT CChkBookDoc::GetLastCheckNo()
  250. {
  251.     return (m_extraHeader.nFirstCheckNo + GetRecordCount() - 1);
  252. }
  253.  
  254. /////////////////////////////////////////////////////////////////////////////
  255. // CChkBookDoc commands
  256.  
  257. void CChkBookDoc::NewCheck()
  258. {
  259.     // Before creating a new record, which will become the new selection,
  260.     // ask the user whether she wants to commit data entered in the
  261.     // check view for the previously selected check.
  262.  
  263.     if (!MaybeCommitDirtyCheck())
  264.         return;
  265.  
  266.     m_nActiveRecord = CreateNewRecord();
  267. }
  268.  
  269. void CChkBookDoc::OnNextCheck()
  270. {
  271.     ChangeSelectionNextCheckNo(TRUE);
  272. }
  273.  
  274. void CChkBookDoc::OnUpdateNextCheck(CCmdUI* pCmdUI)
  275. {
  276.     pCmdUI->Enable(m_nActiveRecord < (GetRecordCount() - 1));
  277. }
  278.  
  279. void CChkBookDoc::OnPrevCheck()
  280. {
  281.     ChangeSelectionNextCheckNo(FALSE);
  282. }
  283.  
  284. void CChkBookDoc::OnUpdatePrevCheck(CCmdUI* pCmdUI)
  285. {
  286.     pCmdUI->Enable(m_nActiveRecord > 0);
  287.  
  288. }
  289.  
  290.  
  291.  
  292.