home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / mfc / general / dbvlist / empview.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-03-27  |  5.6 KB  |  255 lines

  1. // EmpView.cpp : implementation file
  2. //
  3. // This is a part of the Microsoft Foundation Classes C++ library.
  4. // Copyright (C) 1992-1998 Microsoft Corporation
  5. // All rights reserved.
  6. //
  7. // This source code is only intended as a supplement to the
  8. // Microsoft Foundation Classes Reference and related
  9. // electronic documentation provided with the library.
  10. // See these sources for detailed information regarding the
  11. // Microsoft Foundation Classes product.
  12.  
  13. #include "stdafx.h"
  14. #include "DBVList.h"
  15. #include "DBVListSet.h"
  16. #include "DBVListDoc.h"
  17.  
  18. #include "EmpView.h"
  19.  
  20. #ifdef _DEBUG
  21. #define new DEBUG_NEW
  22. #undef THIS_FILE
  23. static char THIS_FILE[] = __FILE__;
  24. #endif
  25.  
  26. /////////////////////////////////////////////////////////////////////////////
  27. // CEmpView
  28.  
  29. IMPLEMENT_DYNCREATE(CEmpView, CListView)
  30.  
  31. CEmpView::CEmpView()
  32. {
  33.     m_dwDefaultStyle |= ( LVS_REPORT | LVS_OWNERDATA );
  34. }
  35.  
  36. CEmpView::~CEmpView()
  37. {
  38. }
  39.  
  40.  
  41. BEGIN_MESSAGE_MAP(CEmpView, CListView)
  42.     //{{AFX_MSG_MAP(CEmpView)
  43.         // NOTE - the ClassWizard will add and remove mapping macros here.
  44.     //}}AFX_MSG_MAP
  45.     ON_NOTIFY_RANGE(LVN_COLUMNCLICK,0,0xffff,OnColumnClick)
  46. END_MESSAGE_MAP()
  47.  
  48. /////////////////////////////////////////////////////////////////////////////
  49. // CEmpView diagnostics
  50.  
  51. #ifdef _DEBUG
  52. void CEmpView::AssertValid() const
  53. {
  54.     CListView::AssertValid();
  55. }
  56.  
  57. void CEmpView::Dump(CDumpContext& dc) const
  58. {
  59.     CListView::Dump(dc);
  60. }
  61.  
  62. CDBVListDoc* CEmpView::GetDocument() // non-debug version is inline
  63. {
  64.     ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CDBVListDoc)));
  65.     return (CDBVListDoc*)m_pDocument;
  66. }
  67. #endif //_DEBUG
  68.  
  69. /////////////////////////////////////////////////////////////////////////////
  70. // CEmpView message handlers
  71.  
  72. void CEmpView::PrepareCache(int /*iFrom*/, int /*iTo*/)
  73. {
  74. }
  75.  
  76. void CEmpView::GetDispInfo(LVITEM* pItem)
  77. {
  78.     CDBVListSet* pSet = &GetDocument()->m_dBVListSet;
  79.  
  80.     // called when the listview needs to display data
  81.     if(pItem->mask & LVIF_TEXT)
  82.     {
  83.         // first, move to the appropriate row in the database
  84.         pSet->MoveFirst();
  85.         pSet->Move(pItem->iItem);
  86.  
  87.         if(!pSet->IsEOF())
  88.         {
  89.             // then display the appropriate column
  90.             switch(pItem->iSubItem)
  91.             {
  92.             case 0:
  93.                 lstrcpy(pItem->pszText, (LPCTSTR)pSet->m_Email);
  94.                 break;
  95.             case 1:
  96.                 lstrcpy(pItem->pszText, (LPCTSTR)pSet->m_Name);
  97.                 break;
  98.             case 2:
  99.                 lstrcpy(pItem->pszText, (LPCTSTR)pSet->m_Phone);
  100.                 break;
  101.             case 3:
  102.                 lstrcpy(pItem->pszText, (LPCTSTR)pSet->m_Location);
  103.                 break;
  104.             case 4:
  105.                 lstrcpy(pItem->pszText, (LPCTSTR)pSet->m_Title);
  106.                 break;
  107.             case 5:
  108.                 lstrcpy(pItem->pszText, (LPCTSTR)pSet->m_Department);
  109.                 break;
  110.             }
  111.         }
  112.     }
  113. }
  114.  
  115. int CEmpView::FindItem(int /*iStart*/, LVFINDINFO* /*plvfi*/)
  116. {
  117.     return -1;
  118. }
  119.  
  120. void CEmpView::UpdateFilter(CString strCurQuery)
  121. {
  122.     // convenience function to set the SQL filter for the query
  123.  
  124.     CDBVListSet* pSet = &GetDocument()->m_dBVListSet;
  125.  
  126.     pSet->SetFilter(strCurQuery);
  127.  
  128.     // set the item count to the new record count
  129.     GetListCtrl().SetItemCountEx(pSet->GetRecordCount());
  130.  
  131.     // Invalidate the listview so it will redraw itself
  132.     Invalidate();
  133. }
  134.  
  135. void CEmpView::OnInitialUpdate()
  136. {
  137.     //initialise columns in the listview
  138.  
  139.     CListView::OnInitialUpdate();
  140.  
  141.     LV_COLUMN lvc;
  142.  
  143.     lvc.mask = LVCF_TEXT | LVCF_SUBITEM | LVCF_WIDTH;
  144.  
  145.     lvc.iSubItem = 0;
  146.     lvc.pszText = _T("Email");
  147.     lvc.cx = 75;
  148.     GetListCtrl().InsertColumn(0,&lvc);
  149.     lvc.iSubItem = 1;
  150.     lvc.pszText = _T("Name (First, Last)");
  151.     lvc.cx = 125;
  152.     GetListCtrl().InsertColumn(1,&lvc);
  153.     lvc.iSubItem = 2;
  154.     lvc.pszText = _T("Phone");
  155.     lvc.cx = 75;
  156.     GetListCtrl().InsertColumn(2,&lvc);
  157.     lvc.iSubItem = 3;
  158.     lvc.pszText = _T("Location");
  159.     lvc.cx = 75;
  160.     GetListCtrl().InsertColumn(3,&lvc);
  161.     lvc.iSubItem = 4;
  162.     lvc.pszText = _T("Title");
  163.     lvc.cx = 150;
  164.     GetListCtrl().InsertColumn(4,&lvc);
  165.     lvc.iSubItem = 5;
  166.     lvc.pszText = _T("Dept");
  167.     lvc.cx = 150;
  168.     GetListCtrl().InsertColumn(5,&lvc);
  169. }
  170.  
  171. void CEmpView::OnColumnClick(UINT id, NMHDR* pNotifyStruct, LRESULT* pResult)
  172. {
  173.     // when a column is clicked on, sort by that column
  174.  
  175.     CDBVListSet* pSet = &GetDocument()->m_dBVListSet;
  176.  
  177.     switch(reinterpret_cast<NMLISTVIEW*>(pNotifyStruct)->iSubItem)
  178.     {
  179.     case 0:
  180.         pSet->SetSort(_T("[Email]"));
  181.         break;
  182.     case 1:
  183.         pSet->SetSort(_T("[Name]"));
  184.         break;
  185.     case 2:
  186.         pSet->SetSort(_T("[Phone]"));
  187.         break;
  188.     case 3:
  189.         pSet->SetSort(_T("[Location]"));
  190.         break;
  191.     case 4:
  192.         pSet->SetSort(_T("[Title]"));
  193.         break;
  194.     case 5:
  195.         pSet->SetSort(_T("[Department]"));
  196.         break;
  197.     case 6:
  198.         pSet->SetSort(_T("[Division]"));
  199.         break;
  200.     }
  201.     Invalidate();
  202. }
  203.  
  204. BOOL CEmpView::OnChildNotify(UINT message, WPARAM wParam, LPARAM lParam, LRESULT* pResult)
  205. {
  206.     if(message == WM_NOTIFY)
  207.     {
  208.         NMHDR* phdr = (NMHDR*)lParam;
  209.  
  210.         // these 3 notifications are only sent by virtual listviews
  211.         switch(phdr->code)
  212.         {
  213.         case LVN_GETDISPINFO:
  214.             {
  215.                 NMLVDISPINFO* pLvdi;
  216.  
  217.                 pLvdi = (NMLVDISPINFO*)lParam;
  218.                 GetDispInfo(&pLvdi->item);
  219.             }
  220.             if(pResult != NULL)
  221.             {
  222.                 *pResult = 0;
  223.             }
  224.             break;
  225.         case LVN_ODCACHEHINT:
  226.             {
  227.                 NMLVCACHEHINT* pHint = (NMLVCACHEHINT*)lParam;
  228.  
  229.                 PrepareCache(pHint->iFrom, pHint->iTo);
  230.             }
  231.             if(pResult != NULL)
  232.             {
  233.                 *pResult = 0;
  234.             }
  235.             break;
  236.         case LVN_ODFINDITEM:
  237.             {
  238.                 NMLVFINDITEM* pFindItem = (NMLVFINDITEM*)lParam;
  239.                 int i = FindItem(pFindItem->iStart, &pFindItem->lvfi);
  240.                 if(pResult != NULL)
  241.                 {
  242.                     *pResult = i;
  243.                 }
  244.             }
  245.             break;
  246.         default:
  247.             return CListView::OnChildNotify(message, wParam, lParam, pResult);
  248.         }
  249.     }
  250.     else
  251.         return CListView::OnChildNotify(message, wParam, lParam, pResult);
  252.  
  253.     return TRUE;
  254. }
  255.