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

  1. // chkbook.cpp : Defines the class behaviors for the application.
  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. /////////////////////////////////////////////////////////////////////////////
  23. // CChkBookApp
  24.  
  25. BEGIN_MESSAGE_MAP(CChkBookApp, CWinApp)
  26.     //{{AFX_MSG_MAP(CChkBookApp)
  27.     ON_COMMAND(ID_FILE_NEW, OnFileNew)
  28.     ON_COMMAND(ID_APP_ABOUT, OnAppAbout)
  29.     //}}AFX_MSG_MAP
  30.     // Standard file based document commands
  31.     ON_COMMAND(ID_FILE_OPEN, CWinApp::OnFileOpen)
  32.     // Standard print setup command
  33.     ON_COMMAND(ID_FILE_PRINT_SETUP, CWinApp::OnFilePrintSetup)
  34. END_MESSAGE_MAP()
  35.  
  36. /////////////////////////////////////////////////////////////////////////////
  37. // CChkBookApp construction
  38. // Place all significant initialization in InitInstance
  39.  
  40. CChkBookApp::CChkBookApp()
  41. {
  42. }
  43.  
  44. /////////////////////////////////////////////////////////////////////////////
  45. // The one and only CChkBookApp object
  46.  
  47. CChkBookApp theApp;
  48.  
  49. /////////////////////////////////////////////////////////////////////////////
  50. // CChkBookApp initialization
  51.  
  52. BOOL CChkBookApp::InitInstance()
  53. {
  54.     // Standard initialization
  55.     SetDialogBkColor();        // set dialog background color to gray
  56.  
  57.     // We create two doc template objects to orchestrate the creation of two
  58.     // distinct MDI children-hosted views of the document: (1) the check
  59.     // check view and (2) the book view.
  60.     //
  61.     // We register both doc templates with the CWinApp object, by calling
  62.     // AddDocTemplate.  However, if we were to do this and nothing else, then
  63.     // the framework would mistakenly believe that the application supports
  64.     // two document types.  The framework would display a File New dialog
  65.     // that lists two document types, both which would be "Check Book".
  66.     // We avoid this problem by removing the third string from 
  67.     // the document template for the check frame/view.  Specifically,
  68.     // the strings for documents IDR_BOOKFRAME and IDR_CHECKFRAME are,
  69.     // respectively:
  70.     //
  71.     //       "Book\nchecks\nCheck Book\n
  72.     //        Check Book File (*.chb)\n.chb\n
  73.     //        ChkBookFileType\nCheck Book File Type"
  74.     // and
  75.     //       "Check\nchecks\n\n
  76.     //        Check Book File (*.chb)\n.chb\n
  77.     //        ChkBookFileType\nCheck Book File Type"
  78.     //
  79.     // Finding no GetDocString(CDocTemplate::fileNewName) for the
  80.     // second document template, CWinApp concludes that there is only
  81.     // one document type supported by the application (the "Check Book"
  82.     // document type specified in the first document template; and
  83.     // therefore does not represent the user with a File New dialog.
  84.     
  85.     m_pBookViewTemplate = new CMultiDocTemplate(IDR_BOOKFRAME,
  86.             RUNTIME_CLASS(CChkBookDoc),
  87.             RUNTIME_CLASS(CCheckBookFrame),
  88.             RUNTIME_CLASS(CBookView));
  89.     AddDocTemplate(m_pBookViewTemplate);
  90.  
  91.     m_pCheckViewTemplate = new CMultiDocTemplate(IDR_CHECKFRAME,
  92.             RUNTIME_CLASS(CChkBookDoc),
  93.             RUNTIME_CLASS(CCheckBookFrame),
  94.             RUNTIME_CLASS(CCheckView));
  95.     AddDocTemplate(m_pCheckViewTemplate);
  96.  
  97.     // Create the main MDI frame window.
  98.  
  99.     CMainFrame* pMainFrame = new CMainFrame;
  100.     if (!pMainFrame->LoadFrame(IDR_MAINFRAME))
  101.         return FALSE;
  102.     
  103.     // The following reflects an optional user interface design
  104.     // decision to automatically maximize the main application window
  105.     // upon start-up.
  106.     pMainFrame->ShowWindow(SW_SHOWMAXIMIZED);
  107.  
  108.     pMainFrame->UpdateWindow();
  109.     m_pMainWnd = pMainFrame;
  110.  
  111.     if (m_lpCmdLine[0] == '\0')
  112.     {
  113.         // Open file name saved in private INI file.
  114.         CString strDocPath = GetDocPathFromIniFile();
  115.         if (!strDocPath.IsEmpty())
  116.             OpenDocumentFile(strDocPath);
  117.     }
  118.     else
  119.     {
  120.         OpenDocumentFile(m_lpCmdLine);
  121.     }
  122.  
  123.     return TRUE;
  124. }
  125.  
  126. /////////////////////////////////////////////////////////////////////////////
  127. // Overrides
  128.  
  129. void CChkBookApp::OnFileNew()
  130. {
  131.     // By default, the framework does not create an actual file for
  132.     // a new document until the user does a File Save As.  But ChkBook
  133.     // requires a file as soon as the user does a File New, because
  134.     // ChkBook updates the file on a per transaction basis.  Upon File
  135.     // New, we prompt the user with a File Open dialog, in which the
  136.     // user specifies a new file (or if she changes her mind, she
  137.     // can specify an existing check book file).  We call the same
  138.     // CWinApp::DoPromptFileName that CWinApp::OnFileOpen calls.
  139.     // But we replace the OFN_FILEMUSTEXIST flag with
  140.     // OFN_CREATEPROMPT.
  141.  
  142.     CString strNewFileName;
  143.  
  144.     if (!(DoPromptFileName(strNewFileName, IDS_NEW_CHECKBOOK,
  145.             OFN_HIDEREADONLY | OFN_CREATEPROMPT, TRUE, NULL)))
  146.         return;
  147.  
  148.     // If file doesn't already exist, then create it.
  149.     CFile file;
  150.     CFileStatus status;
  151.     if (!file.GetStatus(strNewFileName, status))
  152.     {
  153.         file.Open(strNewFileName, CFile::modeCreate);
  154.         file.Close();
  155.     }
  156.  
  157.     // Open the file now that it has been created.
  158.     OpenDocumentFile(strNewFileName);
  159. }
  160.  
  161. CDocument* CChkBookApp::OpenDocumentFile(LPCSTR lpszFileName)
  162. {
  163.     // CWinApp::OpenDocmentFile creates the first MDI child window
  164.     // for the book view.  This override creates the second MDI child
  165.     // window for the check view.  Then it tiles the two MDI children
  166.     // windows.
  167.  
  168.     CChkBookDoc* pDoc = (CChkBookDoc*)CWinApp::OpenDocumentFile(lpszFileName);
  169.     if (pDoc == NULL)
  170.     {
  171.         CString strMessage;
  172.         AfxFormatString1(strMessage, IDS_CANNOT_OPEN_CHECKBOOK, 
  173.             lpszFileName);
  174.         AfxMessageBox(strMessage);
  175.         return NULL;
  176.     }
  177.  
  178.     CFrameWnd* pNewFrame = m_pCheckViewTemplate->CreateNewFrame(pDoc, NULL);
  179.     if (pNewFrame == NULL)
  180.         return pDoc;
  181.     m_pCheckViewTemplate->InitialUpdateFrame(pNewFrame, pDoc);
  182.  
  183.     // Tile the two MDI children windows within the MDI frame window.
  184.  
  185.     ASSERT(pNewFrame->IsKindOf(RUNTIME_CLASS(CMDIChildWnd)));
  186.     CMDIFrameWnd* pMDIFrameWnd = ((CMDIChildWnd*)pNewFrame)->GetMDIFrame();
  187.     ASSERT(pMDIFrameWnd != NULL);
  188.     pMDIFrameWnd->MDITile(MDITILE_HORIZONTAL);
  189.  
  190.     return pDoc;
  191. }
  192.  
  193. /////////////////////////////////////////////////////////////////////////////
  194. // INI file implementation
  195.  
  196. static char BASED_CODE szIniFileSection[] = "Most Recent Check Book File";
  197. static char BASED_CODE szIniFileEntry[] = "File";
  198.  
  199. void CChkBookApp::UpdateIniFileWithDocPath(const char* pszPathName)
  200. {
  201.     WriteProfileString(szIniFileSection, szIniFileEntry, pszPathName);
  202. }
  203.  
  204. CString CChkBookApp::GetDocPathFromIniFile()
  205. {
  206.     return GetProfileString(szIniFileSection, szIniFileEntry, NULL);
  207. }
  208.  
  209. /////////////////////////////////////////////////////////////////////////////
  210. // CChkBookApp commands
  211.  
  212. void CChkBookApp::OnAppAbout()
  213. {
  214.     CDialog dlg(IDD_ABOUTBOX);
  215.     dlg.DoModal();
  216. }
  217.  
  218.