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

  1. // DynaListView.cpp : implementation of the CDynaListView class
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "DynaList.h"
  6. #include "TbSelDlg.h"
  7. #include "DLDoc.h"
  8. #include "DLView.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. /////////////////////////////////////////////////////////////////////////////
  18. // CDynaListView
  19.  
  20. IMPLEMENT_DYNCREATE(CDynaListView, CListView)
  21.  
  22. BEGIN_MESSAGE_MAP(CDynaListView, CListView)
  23.     //{{AFX_MSG_MAP(CDynaListView)
  24.     ON_COMMAND(ID_FILE_OPEN, OnFileOpen)
  25.     ON_COMMAND(ID_FILE_CLOSE, OnFileClose)
  26.     ON_UPDATE_COMMAND_UI(ID_FILE_CLOSE, OnUpdateFileClose)
  27.     ON_WM_CREATE()
  28.     //}}AFX_MSG_MAP
  29. END_MESSAGE_MAP()
  30.  
  31. /////////////////////////////////////////////////////////////////////////////
  32. // CDynaListView construction/destruction
  33.  
  34. CDynaListView::CDynaListView()
  35. {
  36.     // TODO: add construction code here
  37.     m_pDatabase = new CDaoDatabase;
  38.     m_pSet = NULL;
  39. }
  40.  
  41. CDynaListView::~CDynaListView()
  42. {
  43.     // Cleanup memory
  44.     if(m_pSet && m_pSet->IsOpen())
  45.         m_pSet->Close();
  46.     if(m_pSet)
  47.         delete m_pSet;
  48.  
  49.     delete m_pDatabase;
  50. }
  51.  
  52. BOOL CDynaListView::PreCreateWindow(CREATESTRUCT& cs)
  53. {
  54.     // TODO: Modify the Window class or styles here by modifying
  55.     //  the CREATESTRUCT cs
  56.  
  57.     return CListView::PreCreateWindow(cs);
  58. }
  59.  
  60. /////////////////////////////////////////////////////////////////////////////
  61. // CDynaListView drawing
  62.  
  63. void CDynaListView::OnDraw(CDC* pDC)
  64. {
  65.     CDynaListDoc* pDoc = GetDocument();
  66.     ASSERT_VALID(pDoc);
  67.  
  68.     // TODO: add draw code for native data here
  69. }
  70.  
  71. void CDynaListView::OnInitialUpdate()
  72. {
  73.     CListView::OnInitialUpdate();
  74.  
  75.     CRect rc;
  76.     GetClientRect(rc);
  77.     CListCtrl& ctlList = (CListCtrl&) GetListCtrl();
  78.  
  79.     OnFileOpen();
  80. }
  81.  
  82. /////////////////////////////////////////////////////////////////////////////
  83. // CDynaListView diagnostics
  84.  
  85. #ifdef _DEBUG
  86. void CDynaListView::AssertValid() const
  87. {
  88.     CListView::AssertValid();
  89. }
  90.  
  91. void CDynaListView::Dump(CDumpContext& dc) const
  92. {
  93.     CListView::Dump(dc);
  94. }
  95.  
  96. CDynaListDoc* CDynaListView::GetDocument() // non-debug version is inline
  97. {
  98.     ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CDynaListDoc)));
  99.     return (CDynaListDoc*)m_pDocument;
  100. }
  101. #endif //_DEBUG
  102.  
  103. /////////////////////////////////////////////////////////////////////////////
  104. // CDynaListView message handlers
  105. BOOL CDynaListView::FillListViewHeaders(int cx)
  106. {
  107.     ASSERT(m_pSet);
  108.     CListCtrl& ctlList = (CListCtrl&) GetListCtrl();
  109.  
  110.     int nFields = m_pSet->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_pSet->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.         ctlList.InsertColumn(i, &lvc);
  125.     }
  126.     return TRUE;
  127. }
  128.  
  129. BOOL CDynaListView::FillListViewEntries(int nRecords)
  130. {
  131.     ASSERT(m_pSet);
  132.     CListCtrl& ctlList = (CListCtrl&) GetListCtrl();
  133.  
  134.     BOOL bEOF = FALSE;
  135.     LV_ITEM lvi;
  136.     CString strVal;
  137.     int count = 0;
  138.     int index = ctlList.GetItemCount();
  139.     int nFields = m_pSet->GetFieldCount();
  140.  
  141.     lvi.mask = LVIF_TEXT;
  142.     if (-1 == nRecords) 
  143.         nRecords = 0xFFFF;
  144.  
  145.     while (!(bEOF = m_pSet->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_pSet->GetFieldValue(i));
  153.             lvi.pszText = (LPSTR)LPCTSTR(strVal);
  154.             lvi.cchTextMax = strVal.GetLength();
  155.             if (i == 0)
  156.                 ctlList.InsertItem(&lvi);
  157.             else
  158.                 ctlList.SetItem(&lvi);
  159.         }
  160.         index++;
  161.         count++;
  162.         m_pSet->Move(1);
  163.     }
  164.     TRACE2("Filled ListView with %d new entries. Total %d Entries.\n", count, index);
  165.     return bEOF;
  166. }
  167.  
  168. void CDynaListView::OnFileOpen() 
  169. {
  170.     // Get new database name
  171.     CString strFilter = "Access Database (*.MDB)|*.MDB|All Files (*.*)|*.*|";
  172.     CFileDialog dlg(TRUE, "MDB", NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
  173.         strFilter, this);
  174.  
  175.     if(m_pDatabase->IsOpen())
  176.         strcpy(dlg.m_ofn.lpstrFile, m_pDatabase->GetName());
  177.  
  178.     if(IDOK == dlg.DoModal())
  179.     {
  180.         CDaoDatabase* pDatabase = new CDaoDatabase;
  181.         pDatabase->Open(dlg.GetPathName());
  182.  
  183.         CDaoTableSelectDlg tableDlg;
  184.         tableDlg.m_pDatabase = pDatabase;
  185.  
  186.         if(IDOK == tableDlg.DoModal())
  187.         {
  188.             if(m_pSet)
  189.                 if(m_pSet->IsOpen())
  190.                     m_pSet->Close();
  191.                 delete m_pSet;
  192.             if(m_pDatabase->IsOpen())
  193.             {
  194.                 m_pDatabase->Close();
  195.                 m_pDatabase = pDatabase;
  196.             }
  197.  
  198.             m_pSet = new CDaoRecordset(pDatabase);
  199.             // Construct Query
  200.             CString strSQL = "SELECT * FROM " + tableDlg.m_strTables;
  201.             m_pSet->Open(AFX_DAO_USE_DEFAULT_TYPE, strSQL);
  202.  
  203.             // Erase any ListView information
  204.             DeleteListViewData();
  205.  
  206.             // Add field titles to listview headers
  207.             CRect rc;
  208.             GetClientRect(rc);
  209.             FillListViewHeaders(rc.Width());
  210.  
  211.             // Fill ListView with record entries
  212.             FillListViewEntries(-1);
  213.         }
  214.     }
  215.         
  216. }
  217.  
  218. void CDynaListView::DeleteListViewData() 
  219. {
  220.     CListCtrl& ctlList = (CListCtrl&) GetListCtrl();
  221.     ctlList.DeleteAllItems();
  222.     while(ctlList.DeleteColumn(0))
  223.         ;
  224. }
  225.  
  226. void CDynaListView::OnFileClose() 
  227. {
  228.     // TODO: Add your command handler code here
  229.     
  230. }
  231.  
  232. void CDynaListView::OnUpdateFileClose(CCmdUI* pCmdUI) 
  233. {
  234.     // TODO: Add your command update UI handler code here
  235.     
  236. }
  237.  
  238. int CDynaListView::OnCreate(LPCREATESTRUCT lpCreateStruct) 
  239. {
  240.     // Create this in report view
  241.     lpCreateStruct->style |= LVS_REPORT;
  242.  
  243.     if (CListView::OnCreate(lpCreateStruct) == -1)
  244.         return -1;
  245.     
  246.     return 0;
  247. }
  248.