home *** CD-ROM | disk | FTP | other *** search
- // DBListView.cpp : implementation file
- //
-
- #include "stdafx.h"
- #include "DBAccess.h"
- #include "BibSet.h"
- #include "DBADoc.h"
- #include "DBLView.h"
- #include "crack.h"
-
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
-
- const UINT WM_FILLLISTVIEW = WM_USER + 0;
-
- /////////////////////////////////////////////////////////////////////////////
- // CDBListView
-
- IMPLEMENT_DYNCREATE(CDBListView, CListView)
-
- CDBListView::CDBListView()
- {
- }
-
- CDBListView::~CDBListView()
- {
- }
-
-
- BEGIN_MESSAGE_MAP(CDBListView, CListView)
- //{{AFX_MSG_MAP(CDBListView)
- ON_WM_CREATE()
- ON_WM_DESTROY()
- //}}AFX_MSG_MAP
- ON_MESSAGE(WM_FILLLISTVIEW, OnFillListView)
- END_MESSAGE_MAP()
-
- /////////////////////////////////////////////////////////////////////////////
- // CDBListView drawing
-
- void CDBListView::OnDraw(CDC* pDC)
- {
- CDocument* pDoc = GetDocument();
- // TODO: add draw code here
- }
-
- /////////////////////////////////////////////////////////////////////////////
- // CDBListView diagnostics
-
- #ifdef _DEBUG
- void CDBListView::AssertValid() const
- {
- CListView::AssertValid();
- }
-
- void CDBListView::Dump(CDumpContext& dc) const
- {
- CListView::Dump(dc);
- }
-
- CDBAccessDoc* CDBListView::GetDocument() // non-debug version is inline
- {
- ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CDBAccessDoc)));
- return (CDBAccessDoc*)m_pDocument;
- }
- #endif //_DEBUG
-
- /////////////////////////////////////////////////////////////////////////////
- // CDBListView message handlers
-
- void CDBListView::OnInitialUpdate()
- {
- CListView::OnInitialUpdate();
-
- CRect rc;
- GetClientRect(rc);
- m_pListCtrl = &GetListCtrl();
-
- m_pRecordset = new CBiblioSet;
- m_pRecordset->Open();
-
- // Add field titles to listview headers
- FillListViewHeaders(rc.Width());
-
- // Fill ListView with initial record entries
- FillListViewEntries(50);
-
- // Fill ListView with remaining record entries
- PostMessage(WM_FILLLISTVIEW, (WPARAM)-1, 0);
- ShowWindow(SW_SHOW);
- }
-
- int CDBListView::OnCreate(LPCREATESTRUCT lpCreateStruct)
- {
- lpCreateStruct->style |= LVS_REPORT;
- if (CListView::OnCreate(lpCreateStruct) == -1)
- return -1;
-
- return 0;
- }
-
- BOOL CDBListView::FillListViewHeaders(int cx)
- {
- ASSERT(m_pRecordset);
- ASSERT(m_pListCtrl);
-
- int nFields = m_pRecordset->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_pRecordset->GetFieldInfo(i, fi, AFX_DAO_PRIMARY_INFO);
- lvc.pszText = (LPTSTR)LPCTSTR(fi.m_strName);
- TRACE2("Field %d Name: %s\n", i, lvc.pszText);
- m_pListCtrl->InsertColumn(i, &lvc);
- }
- return TRUE;
- }
-
- BOOL CDBListView::FillListViewEntries(int nRecords)
- {
- ASSERT(m_pRecordset);
- ASSERT(m_pListCtrl);
-
- BOOL bEOF = FALSE;
- LV_ITEM lvi;
- CString strVal;
- int count = 0;
- int index = m_pListCtrl->GetItemCount();
- int nFields = m_pRecordset->GetFieldCount();
-
- lvi.mask = LVIF_TEXT;
- if (-1 == nRecords)
- nRecords = 0xFFFF;
-
- while (!(bEOF = m_pRecordset->IsEOF()) && count < nRecords)
- {
- lvi.iItem = index;
-
- for(int i = 0; i < nFields; i++)
- {
- lvi.iSubItem = i;
- strVal = CCrack::strVARIANT(m_pRecordset->GetFieldValue(i));
- lvi.pszText = (LPSTR)LPCTSTR(strVal);
- lvi.cchTextMax = strVal.GetLength();
- if (i == 0)
- m_pListCtrl->InsertItem(&lvi);
- else
- m_pListCtrl->SetItem(&lvi);
- }
- index++;
- count++;
- m_pRecordset->Move(1);
- }
- TRACE2("Filled ListView with %d new entries. Total %d Entries.\n", count, index);
- return bEOF;
- }
-
- long CDBListView::OnFillListView(WPARAM nRecords, LPARAM)
- {
- return FillListViewEntries((int)nRecords);
- }
-
- void CDBListView::OnDestroy()
- {
- CListView::OnDestroy();
-
- m_pRecordset->Close();
- delete m_pRecordset;
- m_pRecordset = 0;
- }
-