home *** CD-ROM | disk | FTP | other *** search
- // DynaListView.cpp : implementation of the CDynaListView class
- //
-
- #include "stdafx.h"
- #include "DynaList.h"
- #include "TbSelDlg.h"
- #include "DLDoc.h"
- #include "DLView.h"
- #include "crack.h"
-
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
-
- /////////////////////////////////////////////////////////////////////////////
- // CDynaListView
-
- IMPLEMENT_DYNCREATE(CDynaListView, CListView)
-
- BEGIN_MESSAGE_MAP(CDynaListView, CListView)
- //{{AFX_MSG_MAP(CDynaListView)
- ON_COMMAND(ID_FILE_OPEN, OnFileOpen)
- ON_COMMAND(ID_FILE_CLOSE, OnFileClose)
- ON_UPDATE_COMMAND_UI(ID_FILE_CLOSE, OnUpdateFileClose)
- ON_WM_CREATE()
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
-
- /////////////////////////////////////////////////////////////////////////////
- // CDynaListView construction/destruction
-
- CDynaListView::CDynaListView()
- {
- // TODO: add construction code here
- m_pDatabase = new CDaoDatabase;
- m_pSet = NULL;
- }
-
- CDynaListView::~CDynaListView()
- {
- // Cleanup memory
- if(m_pSet && m_pSet->IsOpen())
- m_pSet->Close();
- if(m_pSet)
- delete m_pSet;
-
- delete m_pDatabase;
- }
-
- BOOL CDynaListView::PreCreateWindow(CREATESTRUCT& cs)
- {
- // TODO: Modify the Window class or styles here by modifying
- // the CREATESTRUCT cs
-
- return CListView::PreCreateWindow(cs);
- }
-
- /////////////////////////////////////////////////////////////////////////////
- // CDynaListView drawing
-
- void CDynaListView::OnDraw(CDC* pDC)
- {
- CDynaListDoc* pDoc = GetDocument();
- ASSERT_VALID(pDoc);
-
- // TODO: add draw code for native data here
- }
-
- void CDynaListView::OnInitialUpdate()
- {
- CListView::OnInitialUpdate();
-
- CRect rc;
- GetClientRect(rc);
- CListCtrl& ctlList = (CListCtrl&) GetListCtrl();
-
- OnFileOpen();
- }
-
- /////////////////////////////////////////////////////////////////////////////
- // CDynaListView diagnostics
-
- #ifdef _DEBUG
- void CDynaListView::AssertValid() const
- {
- CListView::AssertValid();
- }
-
- void CDynaListView::Dump(CDumpContext& dc) const
- {
- CListView::Dump(dc);
- }
-
- CDynaListDoc* CDynaListView::GetDocument() // non-debug version is inline
- {
- ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CDynaListDoc)));
- return (CDynaListDoc*)m_pDocument;
- }
- #endif //_DEBUG
-
- /////////////////////////////////////////////////////////////////////////////
- // CDynaListView message handlers
- BOOL CDynaListView::FillListViewHeaders(int cx)
- {
- ASSERT(m_pSet);
- CListCtrl& ctlList = (CListCtrl&) GetListCtrl();
-
- int nFields = m_pSet->GetFieldCount();
-
- LV_COLUMN lvc;
- lvc.mask = LVCF_FMT | LVCF_SUBITEM | LVCF_TEXT | LVCF_WIDTH;
- lvc.fmt = LVCFMT_LEFT;
- lvc.cx = cx / nFields;
-
- for(int i = 0; i < nFields; i++)
- {
- CDaoFieldInfo fi;
- lvc.iSubItem = i;
- m_pSet->GetFieldInfo(i, fi, AFX_DAO_PRIMARY_INFO);
- lvc.pszText = (LPTSTR)LPCTSTR(fi.m_strName);
- TRACE2("Field %d Name: %s\n", i, lvc.pszText);
- ctlList.InsertColumn(i, &lvc);
- }
- return TRUE;
- }
-
- BOOL CDynaListView::FillListViewEntries(int nRecords)
- {
- ASSERT(m_pSet);
- CListCtrl& ctlList = (CListCtrl&) GetListCtrl();
-
- BOOL bEOF = FALSE;
- LV_ITEM lvi;
- CString strVal;
- int count = 0;
- int index = ctlList.GetItemCount();
- int nFields = m_pSet->GetFieldCount();
-
- lvi.mask = LVIF_TEXT;
- if (-1 == nRecords)
- nRecords = 0xFFFF;
-
- while (!(bEOF = m_pSet->IsEOF()) && count < nRecords)
- {
- lvi.iItem = index;
-
- for(int i = 0; i < nFields; i++)
- {
- lvi.iSubItem = i;
- strVal = CCrack::strVARIANT(m_pSet->GetFieldValue(i));
- lvi.pszText = (LPSTR)LPCTSTR(strVal);
- lvi.cchTextMax = strVal.GetLength();
- if (i == 0)
- ctlList.InsertItem(&lvi);
- else
- ctlList.SetItem(&lvi);
- }
- index++;
- count++;
- m_pSet->Move(1);
- }
- TRACE2("Filled ListView with %d new entries. Total %d Entries.\n", count, index);
- return bEOF;
- }
-
- void CDynaListView::OnFileOpen()
- {
- // Get new database name
- CString strFilter = "Access Database (*.MDB)|*.MDB|All Files (*.*)|*.*|";
- CFileDialog dlg(TRUE, "MDB", NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
- strFilter, this);
-
- if(m_pDatabase->IsOpen())
- strcpy(dlg.m_ofn.lpstrFile, m_pDatabase->GetName());
-
- if(IDOK == dlg.DoModal())
- {
- CDaoDatabase* pDatabase = new CDaoDatabase;
- pDatabase->Open(dlg.GetPathName());
-
- CDaoTableSelectDlg tableDlg;
- tableDlg.m_pDatabase = pDatabase;
-
- if(IDOK == tableDlg.DoModal())
- {
- if(m_pSet)
- if(m_pSet->IsOpen())
- m_pSet->Close();
- delete m_pSet;
- if(m_pDatabase->IsOpen())
- {
- m_pDatabase->Close();
- m_pDatabase = pDatabase;
- }
-
- m_pSet = new CDaoRecordset(pDatabase);
- // Construct Query
- CString strSQL = "SELECT * FROM " + tableDlg.m_strTables;
- m_pSet->Open(AFX_DAO_USE_DEFAULT_TYPE, strSQL);
-
- // Erase any ListView information
- DeleteListViewData();
-
- // Add field titles to listview headers
- CRect rc;
- GetClientRect(rc);
- FillListViewHeaders(rc.Width());
-
- // Fill ListView with record entries
- FillListViewEntries(-1);
- }
- }
-
- }
-
- void CDynaListView::DeleteListViewData()
- {
- CListCtrl& ctlList = (CListCtrl&) GetListCtrl();
- ctlList.DeleteAllItems();
- while(ctlList.DeleteColumn(0))
- ;
- }
-
- void CDynaListView::OnFileClose()
- {
- // TODO: Add your command handler code here
-
- }
-
- void CDynaListView::OnUpdateFileClose(CCmdUI* pCmdUI)
- {
- // TODO: Add your command update UI handler code here
-
- }
-
- int CDynaListView::OnCreate(LPCREATESTRUCT lpCreateStruct)
- {
- // Create this in report view
- lpCreateStruct->style |= LVS_REPORT;
-
- if (CListView::OnCreate(lpCreateStruct) == -1)
- return -1;
-
- return 0;
- }
-