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