home *** CD-ROM | disk | FTP | other *** search
/ Mastering MFC Development / MMD.ISO / labs / c10 / lab01 / ex01 / browserview.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1997-02-20  |  7.9 KB  |  278 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.     //Exercise 1  Fill the list box with information from the database
  147.     //    nInfo will have enumerated value TABLE, QUERY, or RELATION
  148.     try
  149.     {                                              
  150.         //Call the appropriate member function to get definition count
  151.         int count = ( db.*fnCount[nInfo] )( );    
  152.  
  153.  
  154.         for ( int i = 0 ; i < count ; i ++ )
  155.         {
  156.             switch( nInfo )
  157.             {
  158.             case TABLE :    db.GetTableDefInfo( i, tInfo );
  159.                             if ( ! ( dbSystemObject      
  160.                                     && tInfo.m_lAttributes ) )    //Don't want system tables
  161.                             m_List.AddString( tInfo.m_strName );
  162.                 break;
  163.             case QUERY :     db.GetQueryDefInfo( i, qInfo );
  164.                             m_List.AddString( qInfo.m_strName );
  165.                 break;
  166.             case RELATION: db.GetRelationInfo( i, rInfo );
  167.                             str = rInfo.m_strName +
  168.                                 " :   " + rInfo.m_strTable + " -> " +
  169.                                 rInfo.m_strForeignTable;
  170.                             m_List.AddString( str );
  171.                 break;
  172.             }
  173.         }
  174.         CString name = pDoc->GetTitle( );
  175.         pDoc->SetTitle( name ); //Set document title
  176.     }
  177.     catch( CException * ex )
  178.     {
  179.         ex->ReportError( );
  180.         ex->Delete( );
  181.         m_Tab.SetCurSel( TABLE );
  182.         Invalidate(  );    //Reset everything
  183.     }
  184. }
  185.  
  186. void CBrowserView::OnSelchangeTab(NMHDR* pNMHDR, LRESULT* pResult) 
  187. {
  188.     // TODO: Add your control notification handler code here
  189.     //When the a tab is selected, reload the list box with
  190.     //the correct information
  191.     GetDBNames( m_Tab.GetCurSel( ) );
  192.  
  193.     *pResult = 0;
  194. }
  195.  
  196. void CBrowserView::OnUpdateViewTable(CCmdUI* pCmdUI) 
  197. {
  198.     // TODO: Add your command update UI handler code here
  199.     //Enable Table item if the database is open, and the
  200.     //Table tab is selected
  201.     BOOL bDbIsOpen = 
  202.         GetDocument( )->GetDatabase( )->IsOpen( );
  203.     pCmdUI->Enable( TABLE == m_Tab.GetCurSel( ) && bDbIsOpen );
  204. }
  205.  
  206. void CBrowserView::OnUpdateViewQuery(CCmdUI* pCmdUI) 
  207. {
  208.     // TODO: Add your command update UI handler code here
  209.     //Enable Query item if the database is open, and the
  210.     //Query tab is selected
  211.     BOOL bDbIsOpen = 
  212.         GetDocument( )->GetDatabase( )->IsOpen( );
  213.     pCmdUI->Enable( QUERY == m_Tab.GetCurSel( ) && bDbIsOpen );
  214. }
  215.  
  216. void CBrowserView::OnViewData( ) 
  217. {
  218.     // TODO: Add your command handler code here
  219.     CString str;
  220.     int num = m_List.GetCurSel( ); //Index of selected item
  221.  
  222.     if ( LB_ERR == num )        //Nothing selected
  223.         m_List.SetCurSel( 0 );    //Default to first item
  224.  
  225.     UpdateData( TRUE );     //m_ListSelect now has selected string
  226.     
  227.     //Convert the selected string into a view of the data:    
  228.     //First get our dataview template from the app
  229.     CDocTemplate * pTemplate = 
  230.         ( (CBrowserApp*) AfxGetApp( ) )->m_pDataTemplate;    
  231.     //Create a frame based on the template and current document
  232.     CBrowserDoc * pDoc = GetDocument( );
  233.  
  234.     CMDIChildWnd * pFrame = 
  235.         ( CMDIChildWnd * )     
  236.         pTemplate->CreateNewFrame( pDoc, NULL );
  237.     
  238.     if ( ! pFrame ) return;  //Oops, didn't get a frame
  239.     
  240.     pTemplate->InitialUpdateFrame( pFrame, pDoc );
  241.     CDataView * pDataView = ( CDataView * ) pFrame->GetActiveView( );
  242.     
  243.     //Tell the new view about the database, the table or query name,
  244.     //and whether it's a table or query
  245.     if ( ! pDataView->
  246.         SetData( pDoc->GetDatabase( ), m_ListSelect, m_Tab.GetCurSel( ) ) )
  247.             AfxMessageBox( "Could not create recordset" );
  248.     pDoc->UpdateAllViews( NULL );
  249. }
  250.  
  251. void CBrowserView::OnDblclkList() 
  252. {
  253.     // TODO: Add your control notification handler code here
  254.     //We don't open relationships
  255.     if ( RELATION == m_Tab.GetCurSel( ) )
  256.     {    
  257.         AfxMessageBox( "Relationships cannot be opened" );
  258.     }
  259.     else  //We do open Tables and Queries
  260.         OnViewData( );
  261. }
  262.  
  263. void CBrowserView::OnActivateView(BOOL bActivate, CView* pActivateView, CView* pDeactiveView) 
  264. {
  265.     // TODO: Add your specialized code here and/or call the base class
  266.     //When the Browser view is active, it provides a toolbar
  267.     CMainFrame * pFrame = ( CMainFrame * ) AfxGetMainWnd( );
  268.     
  269.     if ( bActivate )   //Fix up the toolbar    if view is activated
  270.         pFrame->m_wndToolBar.LoadToolBar(IDR_ACCESSTYPE);
  271.     else if ( pFrame == ( CMainFrame * ) pActivateView ) //Is the main frame
  272.         pFrame->m_wndToolBar.LoadToolBar(IDR_MAINFRAME); //the one activated?
  273.  
  274.     pFrame->RecalcLayout( );  //Determines space available for client area
  275.  
  276.     CFormView::OnActivateView(bActivate, pActivateView, pDeactiveView);
  277. }
  278.