home *** CD-ROM | disk | FTP | other *** search
/ Mastering MFC Development / MMD.ISO / labs / c10 / lab01 / ex03 / browser.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1997-02-20  |  6.3 KB  |  238 lines

  1. // Browser.cpp : Defines the class behaviors for the application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "Browser.h"
  6.  
  7. #include "MainFrm.h"
  8. #include "ChildFrm.h"
  9. #include "BrowserDoc.h"
  10. #include "BrowserView.h"
  11. #include "DataView.h"
  12.  
  13.  
  14. #ifdef _DEBUG
  15. #define new DEBUG_NEW
  16. #undef THIS_FILE
  17. static char THIS_FILE[] = __FILE__;
  18. #endif
  19.  
  20. /////////////////////////////////////////////////////////////////////////////
  21. // CBrowserApp
  22.  
  23. BEGIN_MESSAGE_MAP(CBrowserApp, CWinApp)
  24.     //{{AFX_MSG_MAP(CBrowserApp)
  25.     ON_COMMAND(ID_APP_ABOUT, OnAppAbout)
  26.     ON_COMMAND(ID_FILE_OPEN, OnFileOpen)
  27.     //}}AFX_MSG_MAP
  28.     // Standard file based document commands
  29.     ON_COMMAND(ID_FILE_NEW, CWinApp::OnFileNew)
  30.     ON_COMMAND(ID_FILE_OPEN, CWinApp::OnFileOpen)
  31. END_MESSAGE_MAP()
  32.  
  33. /////////////////////////////////////////////////////////////////////////////
  34. // CBrowserApp construction
  35.  
  36. CBrowserApp::CBrowserApp()
  37. {
  38.     // TODO: add construction code here,
  39.     // Place all significant initialization in InitInstance
  40. }
  41.  
  42. /////////////////////////////////////////////////////////////////////////////
  43. // The one and only CBrowserApp object
  44.  
  45. CBrowserApp theApp;
  46.  
  47. /////////////////////////////////////////////////////////////////////////////
  48. // CBrowserApp initialization
  49.  
  50. BOOL CBrowserApp::InitInstance()
  51. {
  52.     AfxEnableControlContainer();
  53.  
  54.     // Standard initialization
  55.     // If you are not using these features and wish to reduce the size
  56.     //  of your final executable, you should remove from the following
  57.     //  the specific initialization routines you do not need.
  58.  
  59. #ifdef _AFXDLL
  60.     Enable3dControls();            // Call this when using MFC in a shared DLL
  61. #else
  62.     Enable3dControlsStatic();    // Call this when linking to MFC statically
  63. #endif
  64.  
  65.     LoadStdProfileSettings();  // Load standard INI file options (including MRU)
  66.  
  67.     // Register the application's document templates.  Document templates
  68.     //  serve as the connection between documents, frame windows and views.
  69.  
  70.     m_pTemplate = new CMultiDocTemplate(
  71.         IDR_ACCESSTYPE,
  72.         RUNTIME_CLASS(CBrowserDoc),
  73.         RUNTIME_CLASS(CChildFrame), // custom MDI child frame
  74.         RUNTIME_CLASS(CBrowserView));
  75.     AddDocTemplate(m_pTemplate);
  76.  
  77.     //Template to add another view to the document.  This view
  78.     //has its own set of resources.
  79.  
  80.     m_pDataTemplate = new CMultiDocTemplate(
  81.         IDR_DATATYPE,                  //Data view resources
  82.         RUNTIME_CLASS( CBrowserDoc ),  //Same document class
  83.         RUNTIME_CLASS( CChildFrame ), // custom MDI child frame
  84.         RUNTIME_CLASS( CDataView ) ); //Data view class      
  85.     //Don't add template because we will manage it.  We have to
  86.     //delete the template in the destructor to avoid memory leak.
  87.  
  88.     // create main MDI Frame window
  89.     CMainFrame * pMainFrame = new CMainFrame;
  90.     if (!pMainFrame->LoadFrame(IDR_MAINFRAME))
  91.         return FALSE;
  92.     m_pMainWnd = pMainFrame;
  93.  
  94.     // Enable drag/drop open
  95.     m_pMainWnd->DragAcceptFiles();
  96.  
  97.     // Enable DDE Execute open
  98.     EnableShellOpen();
  99.     RegisterShellFileTypes(TRUE);
  100.  
  101.     // Parse command line for standard shell commands, DDE, file open
  102.     CCommandLineInfo cmdInfo;
  103.     
  104.     //We want to open with an empty frame, so we modify the cmdInfo data
  105.     //structure to indicate no new file. See article Q141725
  106.  
  107.     if ( CCommandLineInfo::FileNew == cmdInfo.m_nShellCommand )
  108.         cmdInfo.m_nShellCommand = CCommandLineInfo::FileNothing;
  109.  
  110.     ParseCommandLine(cmdInfo);
  111.  
  112.     // Dispatch commands specified on the command line
  113.     if (!ProcessShellCommand(cmdInfo))
  114.         return FALSE;
  115.     
  116.     //Reset the toolbar for the mainframe
  117.     pMainFrame->m_wndToolBar.LoadToolBar(IDR_MAINFRAME);
  118.     ( ( CFrameWnd * ) pMainFrame )->RecalcLayout( );
  119.  
  120.     // The main window has been initialized, so update and show it.
  121.     pMainFrame->UpdateWindow();
  122.     pMainFrame->ShowWindow(m_nCmdShow);
  123.  
  124.     return TRUE;
  125. }
  126.  
  127. /////////////////////////////////////////////////////////////////////////////
  128. // CAboutDlg dialog used for App About
  129.  
  130. class CAboutDlg : public CDialog
  131. {
  132. public:
  133.     CAboutDlg();
  134.  
  135. // Dialog Data
  136.     //{{AFX_DATA(CAboutDlg)
  137.     enum { IDD = IDD_ABOUTBOX };
  138.     //}}AFX_DATA
  139.  
  140.     // ClassWizard generated virtual function overrides
  141.     //{{AFX_VIRTUAL(CAboutDlg)
  142.     protected:
  143.     virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
  144.     //}}AFX_VIRTUAL
  145.  
  146. // Implementation
  147. protected:
  148.     //{{AFX_MSG(CAboutDlg)
  149.         // No message handlers
  150.     //}}AFX_MSG
  151.     DECLARE_MESSAGE_MAP()
  152. };
  153.  
  154. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  155. {
  156.     //{{AFX_DATA_INIT(CAboutDlg)
  157.     //}}AFX_DATA_INIT
  158. }
  159.  
  160. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  161. {
  162.     CDialog::DoDataExchange(pDX);
  163.     //{{AFX_DATA_MAP(CAboutDlg)
  164.     //}}AFX_DATA_MAP
  165. }
  166.  
  167. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  168.     //{{AFX_MSG_MAP(CAboutDlg)
  169.         // No message handlers
  170.     //}}AFX_MSG_MAP
  171. END_MESSAGE_MAP()
  172.  
  173. // App command to run the dialog
  174. void CBrowserApp::OnAppAbout()
  175. {
  176.     CAboutDlg aboutDlg;
  177.     aboutDlg.DoModal();
  178. }
  179.  
  180. /////////////////////////////////////////////////////////////////////////////
  181. // CBrowserApp commands
  182.  
  183.  
  184. void CBrowserApp::OnFileOpen() 
  185. {
  186.     // TODO: Add your command handler code here
  187.         
  188.     CFileDialog    dlg( TRUE,                        //Open dialog
  189.         "mdb", "*.mdb",                            //Extension and filename
  190.         OFN_HIDEREADONLY | OFN_PATHMUSTEXIST
  191.         | OFN_FILEMUSTEXIST,        //No read-only box, only valid names
  192.         "Access files(*.mdb)|*.mdb|All files(*.*)|*.*||", //File filters
  193.         NULL );                                                //Parent
  194.     
  195.     if ( IDOK == dlg.DoModal( ) )     //Show the File Open dialog
  196.     {
  197.         CBrowserDoc * pDoc = NULL;
  198.     
  199.         CString path = dlg.GetPathName( );    //Get the complete path
  200.         CString title = dlg.GetFileTitle( );  //Get just the filename
  201.         
  202.         if ( NULL == pDoc )  //We need a document to open a database
  203.         {
  204.             pDoc =    
  205.             (CBrowserDoc *) m_pTemplate->OpenDocumentFile(NULL);
  206.         }
  207.  
  208.         CDaoDatabase * pDB = pDoc->GetDatabase( ); //From exercise 1
  209.         
  210.         if ( pDB->IsOpen( ) )            //Close the database if it's open
  211.             pDB->Close( );
  212.         
  213.         try
  214.         {    
  215.             pDoc->GetDatabase( )->Open( path );   //Open the database
  216.         }
  217.         catch( CException * ex )
  218.         {
  219.             ex->ReportError( );
  220.             ex->Delete( );
  221.         }
  222.     
  223.         CChildFrame * pF = ( CChildFrame * ) 
  224.             ( ( CMainFrame * ) AfxGetMainWnd( ) )->GetActiveFrame( );
  225.         pF->m_strCaption = title;
  226.         pDoc->SetTitle( title );
  227.         pDoc->UpdateAllViews( NULL );
  228.     }
  229. }
  230.  
  231.  
  232. CBrowserApp::~CBrowserApp( )
  233. {    
  234.     //We created the template, but didn't add it to the
  235.     //list maintained by the app, so we have to delete it
  236.     delete m_pDataTemplate;     
  237. }
  238.