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

  1. // BrowserView.cpp : implementation of the CBrowserView class
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "Browser.h"
  6.  
  7. #include "BrowserDoc.h"
  8. #include "BrowserView.h"
  9. #include "ChildFrm.h"
  10. #include "MainFrm.h"
  11. #include "DataView.h"
  12.  
  13. #ifdef _DEBUG
  14. #define new DEBUG_NEW
  15. #undef THIS_FILE
  16. static char THIS_FILE[] = __FILE__;
  17. #endif
  18.  
  19. /////////////////////////////////////////////////////////////////////////////
  20. // CBrowserView
  21.  
  22. IMPLEMENT_DYNCREATE(CBrowserView, CFormView)
  23.  
  24. BEGIN_MESSAGE_MAP(CBrowserView, CFormView)
  25.     //{{AFX_MSG_MAP(CBrowserView)
  26.     ON_NOTIFY(TCN_SELCHANGE, IDC_TAB, OnSelchangeTab)
  27.     ON_UPDATE_COMMAND_UI(ID_VIEW_TABLE, OnUpdateViewTable)
  28.     ON_UPDATE_COMMAND_UI(ID_VIEW_QUERY, OnUpdateViewQuery)
  29.     ON_COMMAND(ID_VIEW_TABLE, OnViewData)
  30.     ON_COMMAND(ID_VIEW_QUERY, OnViewData)
  31.     ON_LBN_DBLCLK(IDC_LIST, OnDblclkList)
  32.     //}}AFX_MSG_MAP
  33. END_MESSAGE_MAP()
  34.  
  35. /////////////////////////////////////////////////////////////////////////////
  36. // CBrowserView construction/destruction
  37.  
  38. CBrowserView::CBrowserView()
  39.     : CFormView(CBrowserView::IDD)
  40. {
  41.     //{{AFX_DATA_INIT(CBrowserView)
  42.     m_ListSelect = _T("");
  43.     //}}AFX_DATA_INIT
  44.     // TODO: add construction code here
  45.  
  46. }
  47.  
  48. CBrowserView::~CBrowserView()
  49. {
  50. }
  51.  
  52. void CBrowserView::DoDataExchange(CDataExchange* pDX)
  53. {
  54.     CFormView::DoDataExchange(pDX);
  55.     //{{AFX_DATA_MAP(CBrowserView)
  56.     DDX_Control(pDX, IDC_LIST, m_List);
  57.     DDX_Control(pDX, IDC_TAB, m_Tab);
  58.     DDX_LBString(pDX, IDC_LIST, m_ListSelect);
  59.     //}}AFX_DATA_MAP
  60. }
  61.                                                             
  62. BOOL CBrowserView::PreCreateWindow(CREATESTRUCT& cs)
  63. {
  64.     // TODO: Modify the Window class or styles here by modifying
  65.     //  the CREATESTRUCT cs
  66.  
  67.     return CFormView::PreCreateWindow(cs);
  68. }
  69.  
  70. /////////////////////////////////////////////////////////////////////////////
  71. // CBrowserView diagnostics
  72.  
  73. #ifdef _DEBUG
  74. void CBrowserView::AssertValid() const
  75. {
  76.     CFormView::AssertValid();
  77. }
  78.  
  79. void CBrowserView::Dump(CDumpContext& dc) const
  80. {
  81.     CFormView::Dump(dc);
  82. }
  83.  
  84. CBrowserDoc* CBrowserView::GetDocument() // non-debug version is inline
  85. {
  86.     ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CBrowserDoc)));
  87.     return (CBrowserDoc*)m_pDocument;
  88. }
  89. #endif //_DEBUG
  90.  
  91. /////////////////////////////////////////////////////////////////////////////
  92. // CBrowserView message handlers
  93.  
  94. void CBrowserView::OnInitialUpdate() 
  95. {
  96.     CFormView::OnInitialUpdate();
  97.     
  98.     // TODO: Add your specialized code here and/or call the base class
  99.     
  100.     //Code to set initial tab values
  101.     m_Tab.DeleteAllItems( );         //Clear existing tabs
  102.     TC_ITEM tc;
  103.     tc.mask = TCIF_TEXT;             //Inserting text values
  104.     tc.pszText = "Tables" ;             //Tab label
  105.     m_Tab.InsertItem( TABLE, & tc ); //Put tab in position
  106.     tc.pszText = "Queries" ;
  107.     m_Tab.InsertItem( QUERY, & tc );
  108.     tc.pszText = "Relations" ;
  109.     m_Tab.InsertItem( RELATION, & tc );
  110. }
  111.  
  112. void CBrowserView::OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint) 
  113. {
  114.     
  115.     // TODO: Add your specialized code here and/or call the base class
  116.     //Determine which tab is selected, pass to function GetDBNames
  117.     GetDBNames( m_Tab.GetCurSel( ) );
  118. }
  119.  
  120. void CBrowserView::GetDBNames( int nInfo )
  121. {
  122.     CBrowserDoc * pDoc = ( CBrowserDoc * ) GetDocument( );
  123.  
  124.     CDaoDatabase & db = * pDoc->GetDatabase( );
  125.     if ( ! db.IsOpen( ) )    //If the database is not open
  126.         return;                //there are no names to get
  127.     
  128.     //Array of member function pointers to eliminate 
  129.     //    if/switch testing.  Entries are order dependent
  130.     short ( CDaoDatabase::* fnCount[ ] )( ) =
  131.     {
  132.         CDaoDatabase::GetTableDefCount,        //TABLES = 0
  133.         CDaoDatabase::GetQueryDefCount,        //QUERIES
  134.         CDaoDatabase::GetRelationCount,        //RELATIONS
  135.     }; //
  136.  
  137.         CDaoTableDefInfo tInfo;    
  138.         CDaoQueryDefInfo qInfo;
  139.         CDaoRelationInfo rInfo;
  140.     
  141.         CString strTableName;
  142.         CString str;
  143.  
  144.         m_List.ResetContent( );        //It's okay to clear the list here
  145.  
  146.     //Call the appropriate member function to get definition count
  147.     int count = ( db.*fnCount[nInfo] )( );
  148.     
  149.     //Exercise 1  Fill the list box with information from the database
  150.     //    nInfo will have enumerated value TABLE, QUERY, or RELATION
  151.     try
  152.     {                                              
  153.         for ( int i = 0 ; i < count ; i ++ )
  154.         {
  155.             switch( nInfo )
  156.             {
  157.             case TABLE :    //Complete for exercise 1                        
  158.                 break;
  159.             case QUERY :    //Complete for exercise 1
  160.                 break;
  161.             case RELATION: db.GetRelationInfo( i, rInfo );
  162.                             str = rInfo.m_strName +
  163.                                 " :   " + rInfo.m_strTable + " -> " +
  164.                                 rInfo.m_strForeignTable;
  165.                             m_List.AddString( str );
  166.                 break;
  167.             }
  168.         }
  169.         CString name = pDoc->GetTitle( );
  170.         pDoc->SetTitle( name ); //Set document title
  171.     }
  172.     catch( CException * ex )
  173.     {
  174.         ex->ReportError( );
  175.         ex->Delete( );
  176.         m_Tab.SetCurSel( TABLE );
  177.         Invalidate(  );    //Reset everything
  178.     }
  179. }
  180.  
  181. void CBrowserView::OnSelchangeTab(NMHDR* pNMHDR, LRESULT* pResult) 
  182. {
  183.     // TODO: Add your control notification handler code here
  184.     //When the a tab is selected, reload the list box with
  185.     //the correct information
  186.     GetDBNames( m_Tab.GetCurSel( ) );
  187.  
  188.     *pResult = 0;
  189. }
  190.  
  191. void CBrowserView::OnUpdateViewTable(CCmdUI* pCmdUI) 
  192. {
  193.     // TODO: Add your command update UI handler code here
  194.     //Enable Table item if the database is open, and the
  195.     //Table tab is selected
  196.     BOOL bDbIsOpen = 
  197.         GetDocument( )->GetDatabase( )->IsOpen( );
  198.     pCmdUI->Enable( TABLE == m_Tab.GetCurSel( ) && bDbIsOpen );
  199. }
  200.  
  201. void CBrowserView::OnUpdateViewQuery(CCmdUI* pCmdUI) 
  202. {
  203.     // TODO: Add your command update UI handler code here
  204.     //Enable Query item if the database is open, and the
  205.     //Query tab is selected
  206.     BOOL bDbIsOpen = 
  207.         GetDocument( )->GetDatabase( )->IsOpen( );
  208.     pCmdUI->Enable( QUERY == m_Tab.GetCurSel( ) && bDbIsOpen );
  209. }
  210.  
  211. void CBrowserView::OnViewData( ) 
  212. {
  213.     // TODO: Add your command handler code here
  214.     CString str;
  215.     int num = m_List.GetCurSel( ); //Index of selected item
  216.  
  217.     if ( LB_ERR == num )        //Nothing selected
  218.         m_List.SetCurSel( 0 );    //Default to first item
  219.  
  220.     UpdateData( TRUE );     //m_ListSelect now has selected string
  221.     
  222.     //Convert the selected string into a view of the data:    
  223.     //First get our dataview template from the app
  224.     CDocTemplate * pTemplate = 
  225.         ( (CBrowserApp*) AfxGetApp( ) )->m_pDataTemplate;    
  226.     //Create a frame based on the template and current document
  227.     CBrowserDoc * pDoc = GetDocument( );
  228.  
  229.     CMDIChildWnd * pFrame = 
  230.         ( CMDIChildWnd * )     
  231.         pTemplate->CreateNewFrame( pDoc, NULL );
  232.     
  233.     if ( ! pFrame ) return;  //Oops, didn't get a frame
  234.     
  235.     pTemplate->InitialUpdateFrame( pFrame, pDoc );
  236.     CDataView * pDataView = ( CDataView * ) pFrame->GetActiveView( );
  237.     
  238.     //Tell the new view about the database, the table or query name,
  239.     //and whether it's a table or query
  240.     if ( ! pDataView->
  241.         SetData( pDoc->GetDatabase( ), m_ListSelect, m_Tab.GetCurSel( ) ) )
  242.             AfxMessageBox( "Could not create recordset" );
  243.     pDoc->UpdateAllViews( NULL );
  244. }
  245.  
  246. void CBrowserView::OnDblclkList() 
  247. {
  248.     // TODO: Add your control notification handler code here
  249.     //We don't open relationships
  250.     if ( RELATION == m_Tab.GetCurSel( ) )
  251.     {    
  252.         AfxMessageBox( "Relationships cannot be opened" );
  253.     }
  254.     else  //We do open Tables and Queries
  255.         OnViewData( );
  256. }
  257.  
  258. void CBrowserView::OnActivateView(BOOL bActivate, CView* pActivateView, CView* pDeactiveView) 
  259. {
  260.     // TODO: Add your specialized code here and/or call the base class
  261.     //When the Browser view is active, it provides a toolbar
  262.     CMainFrame * pFrame = ( CMainFrame * ) AfxGetMainWnd( );
  263.     
  264.     if ( bActivate )   //Fix up the toolbar    if view is activated
  265.         pFrame->m_wndToolBar.LoadToolBar(IDR_ACCESSTYPE);
  266.     else if ( pFrame == ( CMainFrame * ) pActivateView ) //Is the main frame
  267.         pFrame->m_wndToolBar.LoadToolBar(IDR_MAINFRAME); //the one activated?
  268.  
  269.     pFrame->RecalcLayout( );  //Determines space available for client area
  270.  
  271.     CFormView::OnActivateView(bActivate, pActivateView, pDeactiveView);
  272. }
  273.