home *** CD-ROM | disk | FTP | other *** search
/ Mastering MFC Development / MMD.ISO / samples / c08 / listview / dblview.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1997-02-20  |  3.8 KB  |  181 lines

  1. // DBListView.cpp : implementation file
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "DBAccess.h"
  6. #include "BibSet.h"
  7. #include "DBADoc.h"
  8. #include "DBLView.h"
  9. #include "crack.h"
  10.  
  11. #ifdef _DEBUG
  12. #define new DEBUG_NEW
  13. #undef THIS_FILE
  14. static char THIS_FILE[] = __FILE__;
  15. #endif
  16.  
  17. const UINT WM_FILLLISTVIEW = WM_USER + 0;
  18.  
  19. /////////////////////////////////////////////////////////////////////////////
  20. // CDBListView
  21.  
  22. IMPLEMENT_DYNCREATE(CDBListView, CListView)
  23.  
  24. CDBListView::CDBListView()
  25. {
  26. }
  27.  
  28. CDBListView::~CDBListView()
  29. {
  30. }
  31.  
  32.  
  33. BEGIN_MESSAGE_MAP(CDBListView, CListView)
  34.     //{{AFX_MSG_MAP(CDBListView)
  35.     ON_WM_CREATE()
  36.     ON_WM_DESTROY()
  37.     //}}AFX_MSG_MAP
  38.     ON_MESSAGE(WM_FILLLISTVIEW, OnFillListView)
  39. END_MESSAGE_MAP()
  40.  
  41. /////////////////////////////////////////////////////////////////////////////
  42. // CDBListView drawing
  43.  
  44. void CDBListView::OnDraw(CDC* pDC)
  45. {
  46.     CDocument* pDoc = GetDocument();
  47.     // TODO: add draw code here
  48. }
  49.  
  50. /////////////////////////////////////////////////////////////////////////////
  51. // CDBListView diagnostics
  52.  
  53. #ifdef _DEBUG
  54. void CDBListView::AssertValid() const
  55. {
  56.     CListView::AssertValid();
  57. }
  58.  
  59. void CDBListView::Dump(CDumpContext& dc) const
  60. {
  61.     CListView::Dump(dc);
  62. }
  63.  
  64. CDBAccessDoc* CDBListView::GetDocument() // non-debug version is inline
  65. {
  66.     ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CDBAccessDoc)));
  67.     return (CDBAccessDoc*)m_pDocument;
  68. }
  69. #endif //_DEBUG
  70.  
  71. /////////////////////////////////////////////////////////////////////////////
  72. // CDBListView message handlers
  73.  
  74. void CDBListView::OnInitialUpdate() 
  75. {
  76.     CListView::OnInitialUpdate();
  77.     
  78.     CRect rc;
  79.     GetClientRect(rc);
  80.     m_pListCtrl = &GetListCtrl();
  81.  
  82.     m_pRecordset = new CBiblioSet;
  83.     m_pRecordset->Open();
  84.  
  85.     // Add field titles to listview headers
  86.     FillListViewHeaders(rc.Width());
  87.  
  88.     // Fill ListView with initial record entries
  89.     FillListViewEntries(50);
  90.  
  91.     // Fill ListView with remaining record entries
  92.     PostMessage(WM_FILLLISTVIEW, (WPARAM)-1, 0);
  93.     ShowWindow(SW_SHOW);
  94. }
  95.  
  96. int CDBListView::OnCreate(LPCREATESTRUCT lpCreateStruct) 
  97. {
  98.     lpCreateStruct->style |= LVS_REPORT;
  99.     if (CListView::OnCreate(lpCreateStruct) == -1)
  100.         return -1;
  101.     
  102.     return 0;
  103. }
  104.  
  105. BOOL CDBListView::FillListViewHeaders(int cx)
  106. {
  107.     ASSERT(m_pRecordset);
  108.     ASSERT(m_pListCtrl);
  109.  
  110.     int nFields = m_pRecordset->GetFieldCount();
  111.  
  112.     LV_COLUMN lvc;
  113.     lvc.mask = LVCF_FMT | LVCF_SUBITEM | LVCF_TEXT | LVCF_WIDTH;
  114.     lvc.fmt = LVCFMT_LEFT;
  115.     lvc.cx = cx / nFields;
  116.  
  117.     for(int i = 0; i < nFields; i++)
  118.     {
  119.         CDaoFieldInfo fi;
  120.         lvc.iSubItem = i;
  121.         m_pRecordset->GetFieldInfo(i, fi, AFX_DAO_PRIMARY_INFO);
  122.         lvc.pszText = (LPTSTR)LPCTSTR(fi.m_strName);
  123.         TRACE2("Field %d Name: %s\n", i, lvc.pszText);
  124.         m_pListCtrl->InsertColumn(i, &lvc);
  125.     }
  126.     return TRUE;
  127. }
  128.  
  129. BOOL CDBListView::FillListViewEntries(int nRecords)
  130. {
  131.     ASSERT(m_pRecordset);
  132.     ASSERT(m_pListCtrl);
  133.  
  134.     BOOL bEOF = FALSE;
  135.     LV_ITEM lvi;
  136.     CString strVal;
  137.     int count = 0;
  138.     int index = m_pListCtrl->GetItemCount();
  139.     int nFields = m_pRecordset->GetFieldCount();
  140.  
  141.     lvi.mask = LVIF_TEXT;
  142.     if (-1 == nRecords) 
  143.         nRecords = 0xFFFF;
  144.  
  145.     while (!(bEOF = m_pRecordset->IsEOF()) && count < nRecords)
  146.     {
  147.         lvi.iItem = index;
  148.  
  149.         for(int i = 0; i < nFields; i++)
  150.         {
  151.             lvi.iSubItem = i;
  152.             strVal = CCrack::strVARIANT(m_pRecordset->GetFieldValue(i));
  153.             lvi.pszText = (LPSTR)LPCTSTR(strVal);
  154.             lvi.cchTextMax = strVal.GetLength();
  155.             if (i == 0)
  156.                 m_pListCtrl->InsertItem(&lvi);
  157.             else
  158.                 m_pListCtrl->SetItem(&lvi);
  159.         }
  160.         index++;
  161.         count++;
  162.         m_pRecordset->Move(1);
  163.     }
  164.     TRACE2("Filled ListView with %d new entries. Total %d Entries.\n", count, index);
  165.     return bEOF;
  166. }
  167.  
  168. long CDBListView::OnFillListView(WPARAM nRecords, LPARAM)
  169. {
  170.     return FillListViewEntries((int)nRecords);
  171. }
  172.  
  173. void CDBListView::OnDestroy() 
  174. {
  175.     CListView::OnDestroy();
  176.     
  177.     m_pRecordset->Close();
  178.     delete m_pRecordset;
  179.     m_pRecordset = 0;
  180. }
  181.