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