home *** CD-ROM | disk | FTP | other *** search
- // BrowserView.cpp : implementation of the CBrowserView class
- //
-
- #include "stdafx.h"
- #include "Browser.h"
-
- #include "BrowserDoc.h"
- #include "BrowserView.h"
- #include "ChildFrm.h"
- #include "MainFrm.h"
- #include "DataView.h"
-
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
-
- /////////////////////////////////////////////////////////////////////////////
- // CBrowserView
-
- IMPLEMENT_DYNCREATE(CBrowserView, CFormView)
-
- BEGIN_MESSAGE_MAP(CBrowserView, CFormView)
- //{{AFX_MSG_MAP(CBrowserView)
- ON_NOTIFY(TCN_SELCHANGE, IDC_TAB, OnSelchangeTab)
- ON_UPDATE_COMMAND_UI(ID_VIEW_TABLE, OnUpdateViewTable)
- ON_UPDATE_COMMAND_UI(ID_VIEW_QUERY, OnUpdateViewQuery)
- ON_COMMAND(ID_VIEW_TABLE, OnViewData)
- ON_COMMAND(ID_VIEW_QUERY, OnViewData)
- ON_LBN_DBLCLK(IDC_LIST, OnDblclkList)
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
-
- /////////////////////////////////////////////////////////////////////////////
- // CBrowserView construction/destruction
-
- CBrowserView::CBrowserView()
- : CFormView(CBrowserView::IDD)
- {
- //{{AFX_DATA_INIT(CBrowserView)
- m_ListSelect = _T("");
- //}}AFX_DATA_INIT
- // TODO: add construction code here
-
- }
-
- CBrowserView::~CBrowserView()
- {
- }
-
- void CBrowserView::DoDataExchange(CDataExchange* pDX)
- {
- CFormView::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CBrowserView)
- DDX_Control(pDX, IDC_LIST, m_List);
- DDX_Control(pDX, IDC_TAB, m_Tab);
- DDX_LBString(pDX, IDC_LIST, m_ListSelect);
- //}}AFX_DATA_MAP
- }
-
- BOOL CBrowserView::PreCreateWindow(CREATESTRUCT& cs)
- {
- // TODO: Modify the Window class or styles here by modifying
- // the CREATESTRUCT cs
-
- return CFormView::PreCreateWindow(cs);
- }
-
- /////////////////////////////////////////////////////////////////////////////
- // CBrowserView diagnostics
-
- #ifdef _DEBUG
- void CBrowserView::AssertValid() const
- {
- CFormView::AssertValid();
- }
-
- void CBrowserView::Dump(CDumpContext& dc) const
- {
- CFormView::Dump(dc);
- }
-
- CBrowserDoc* CBrowserView::GetDocument() // non-debug version is inline
- {
- ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CBrowserDoc)));
- return (CBrowserDoc*)m_pDocument;
- }
- #endif //_DEBUG
-
- /////////////////////////////////////////////////////////////////////////////
- // CBrowserView message handlers
-
- void CBrowserView::OnInitialUpdate()
- {
- CFormView::OnInitialUpdate();
-
- // TODO: Add your specialized code here and/or call the base class
-
- //Code to set initial tab values
- m_Tab.DeleteAllItems( ); //Clear existing tabs
- TC_ITEM tc;
- tc.mask = TCIF_TEXT; //Inserting text values
- tc.pszText = "Tables" ; //Tab label
- m_Tab.InsertItem( TABLE, & tc ); //Put tab in position
- tc.pszText = "Queries" ;
- m_Tab.InsertItem( QUERY, & tc );
- tc.pszText = "Relations" ;
- m_Tab.InsertItem( RELATION, & tc );
- }
-
- void CBrowserView::OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint)
- {
-
- // TODO: Add your specialized code here and/or call the base class
- //Determine which tab is selected, pass to function GetDBNames
- GetDBNames( m_Tab.GetCurSel( ) );
- }
-
- void CBrowserView::GetDBNames( int nInfo )
- {
- CBrowserDoc * pDoc = ( CBrowserDoc * ) GetDocument( );
-
- CDaoDatabase & db = * pDoc->GetDatabase( );
- if ( ! db.IsOpen( ) ) //If the database is not open
- return; //there are no names to get
-
- //Array of member function pointers to eliminate
- // if/switch testing. Entries are order dependent
- short ( CDaoDatabase::* fnCount[ ] )( ) =
- {
- CDaoDatabase::GetTableDefCount, //TABLES = 0
- CDaoDatabase::GetQueryDefCount, //QUERIES
- CDaoDatabase::GetRelationCount, //RELATIONS
- }; //
-
- CDaoTableDefInfo tInfo;
- CDaoQueryDefInfo qInfo;
- CDaoRelationInfo rInfo;
-
- CString strTableName;
- CString str;
-
- m_List.ResetContent( ); //It's okay to clear the list here
-
- //Exercise 1 Fill the list box with information from the database
- // nInfo will have enumerated value TABLE, QUERY, or RELATION
- try
- {
- //Call the appropriate member function to get definition count
- int count = ( db.*fnCount[nInfo] )( );
-
-
- for ( int i = 0 ; i < count ; i ++ )
- {
- switch( nInfo )
- {
- case TABLE : db.GetTableDefInfo( i, tInfo );
- if ( ! ( dbSystemObject
- && tInfo.m_lAttributes ) ) //Don't want system tables
- m_List.AddString( tInfo.m_strName );
- break;
- case QUERY : db.GetQueryDefInfo( i, qInfo );
- m_List.AddString( qInfo.m_strName );
- break;
- case RELATION: db.GetRelationInfo( i, rInfo );
- str = rInfo.m_strName +
- " : " + rInfo.m_strTable + " -> " +
- rInfo.m_strForeignTable;
- m_List.AddString( str );
- break;
- }
- }
- CString name = pDoc->GetTitle( );
- pDoc->SetTitle( name ); //Set document title
- }
- catch( CException * ex )
- {
- ex->ReportError( );
- ex->Delete( );
- m_Tab.SetCurSel( TABLE );
- Invalidate( ); //Reset everything
- }
- }
-
- void CBrowserView::OnSelchangeTab(NMHDR* pNMHDR, LRESULT* pResult)
- {
- // TODO: Add your control notification handler code here
- //When the a tab is selected, reload the list box with
- //the correct information
- GetDBNames( m_Tab.GetCurSel( ) );
-
- *pResult = 0;
- }
-
- void CBrowserView::OnUpdateViewTable(CCmdUI* pCmdUI)
- {
- // TODO: Add your command update UI handler code here
- //Enable Table item if the database is open, and the
- //Table tab is selected
- BOOL bDbIsOpen =
- GetDocument( )->GetDatabase( )->IsOpen( );
- pCmdUI->Enable( TABLE == m_Tab.GetCurSel( ) && bDbIsOpen );
- }
-
- void CBrowserView::OnUpdateViewQuery(CCmdUI* pCmdUI)
- {
- // TODO: Add your command update UI handler code here
- //Enable Query item if the database is open, and the
- //Query tab is selected
- BOOL bDbIsOpen =
- GetDocument( )->GetDatabase( )->IsOpen( );
- pCmdUI->Enable( QUERY == m_Tab.GetCurSel( ) && bDbIsOpen );
- }
-
- void CBrowserView::OnViewData( )
- {
- // TODO: Add your command handler code here
- CString str;
- int num = m_List.GetCurSel( ); //Index of selected item
-
- if ( LB_ERR == num ) //Nothing selected
- m_List.SetCurSel( 0 ); //Default to first item
-
- UpdateData( TRUE ); //m_ListSelect now has selected string
-
- //Convert the selected string into a view of the data:
- //First get our dataview template from the app
- CDocTemplate * pTemplate =
- ( (CBrowserApp*) AfxGetApp( ) )->m_pDataTemplate;
- //Create a frame based on the template and current document
- CBrowserDoc * pDoc = GetDocument( );
-
- CMDIChildWnd * pFrame =
- ( CMDIChildWnd * )
- pTemplate->CreateNewFrame( pDoc, NULL );
-
- if ( ! pFrame ) return; //Oops, didn't get a frame
-
- pTemplate->InitialUpdateFrame( pFrame, pDoc );
- CDataView * pDataView = ( CDataView * ) pFrame->GetActiveView( );
-
- //Tell the new view about the database, the table or query name,
- //and whether it's a table or query
- if ( ! pDataView->
- SetData( pDoc->GetDatabase( ), m_ListSelect, m_Tab.GetCurSel( ) ) )
- AfxMessageBox( "Could not create recordset" );
- pDoc->UpdateAllViews( NULL );
- }
-
- void CBrowserView::OnDblclkList()
- {
- // TODO: Add your control notification handler code here
- //We don't open relationships
- if ( RELATION == m_Tab.GetCurSel( ) )
- {
- AfxMessageBox( "Relationships cannot be opened" );
- }
- else //We do open Tables and Queries
- OnViewData( );
- }
-
- void CBrowserView::OnActivateView(BOOL bActivate, CView* pActivateView, CView* pDeactiveView)
- {
- // TODO: Add your specialized code here and/or call the base class
- //When the Browser view is active, it provides a toolbar
- CMainFrame * pFrame = ( CMainFrame * ) AfxGetMainWnd( );
-
- if ( bActivate ) //Fix up the toolbar if view is activated
- pFrame->m_wndToolBar.LoadToolBar(IDR_ACCESSTYPE);
- else if ( pFrame == ( CMainFrame * ) pActivateView ) //Is the main frame
- pFrame->m_wndToolBar.LoadToolBar(IDR_MAINFRAME); //the one activated?
-
- pFrame->RecalcLayout( ); //Determines space available for client area
-
- CFormView::OnActivateView(bActivate, pActivateView, pDeactiveView);
- }
-