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

  1. // ListView.cpp : implementation file
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "tree.h"
  6.  
  7. #include "AnmlData.h"
  8. #include "treedoc.h"
  9.  
  10. #include "ListView.h"
  11. #include "TreeView.h"
  12.  
  13. #ifdef _DEBUG
  14. #define new DEBUG_NEW
  15. #undef THIS_FILE
  16. static char THIS_FILE[] = __FILE__;
  17. #endif
  18.  
  19. /////////////////////////////////////////////////////////////////////////////
  20. // CSimpleListView
  21.  
  22. IMPLEMENT_DYNCREATE(CSimpleListView, CListView)
  23.  
  24. CSimpleListView::CSimpleListView()
  25. {
  26. }
  27.  
  28. CSimpleListView::~CSimpleListView()
  29. {
  30. }
  31.  
  32. BEGIN_MESSAGE_MAP(CSimpleListView, CListView)
  33.     //{{AFX_MSG_MAP(CSimpleListView)
  34.     ON_WM_CREATE()
  35.     //}}AFX_MSG_MAP
  36. END_MESSAGE_MAP()
  37.  
  38. /////////////////////////////////////////////////////////////////////////////
  39. // CSimpleListView message handlers
  40. int CSimpleListView::OnCreate(LPCREATESTRUCT lpCreateStruct) 
  41. {
  42.     if (CListView::OnCreate(lpCreateStruct) == -1)
  43.         return -1;
  44.     
  45.     // Initialize the document object's pointer
  46.     // to the list view object.
  47.     GetDocument()->m_pListView = this;
  48.  
  49.     return 0;
  50. }
  51.  
  52. void CSimpleListView::DisplayTypeInfo(const HTREEITEM ParentNode)
  53. {
  54.     static char * headers[] = { "Animal", NULL };
  55.  
  56.     DisplayChildren(headers, ParentNode);
  57. }
  58.  
  59. void CSimpleListView::DisplayClassInfo(const HTREEITEM ParentNode)
  60. {
  61.     static char * headers[] = { "Types", NULL };
  62.  
  63.     DisplayChildren(headers, ParentNode);
  64. }
  65.  
  66. void CSimpleListView::DisplayChildren(char **headers, const HTREEITEM ParentNode)
  67. {
  68.     CListCtrl & clc = GetListCtrl();
  69.     CTreeCtrl & ctc = GetDocument()->m_pTreeView->GetTreeCtrl();
  70.     int i = 0;
  71.  
  72.     LV_COLUMN lv;
  73.     lv.mask = LVCF_FMT | LVCF_TEXT | LVCF_WIDTH;
  74.     lv.fmt = LVCFMT_LEFT;
  75.  
  76.     EraseList();
  77.     
  78.     while (headers[i])
  79.     {
  80.         lv.cx = 15 * clc.GetStringWidth(headers[i]) / 10;
  81.         lv.pszText = headers[i];
  82.         clc.InsertColumn(i++, &lv);
  83.         lv.fmt = LVCFMT_CENTER;
  84.     }
  85.  
  86.     HTREEITEM node = ctc.GetChildItem(ParentNode);
  87.     i = 0;    
  88.     CString s;
  89.     while (node != NULL)
  90.     {
  91.         s = ctc.GetItemText(node);
  92.         clc.InsertItem(i++, s);
  93.         node = ctc.GetNextItem(node, TVGN_NEXT);
  94.     }
  95. }
  96.  
  97. void CSimpleListView::DisplayAnimalInfo(const DWORD itemData)
  98. {
  99.     static char * headers[] = { "Type", "Animal",
  100.                                 "Max. Weight (kgs)", NULL };
  101.     int i = 0;
  102.     LV_COLUMN lv;
  103.     lv.mask = LVCF_FMT | LVCF_TEXT | LVCF_WIDTH;
  104.     lv.fmt = LVCFMT_LEFT;
  105.     CListCtrl & clc = GetListCtrl();
  106.  
  107.     EraseList();
  108.  
  109.     while (headers[i])
  110.     {
  111.         lv.cx = 15 * clc.GetStringWidth(headers[i]) / 10;
  112.         lv.pszText = headers[i];
  113.         clc.InsertColumn(i++, &lv);
  114.         lv.fmt = LVCFMT_CENTER;
  115.     }
  116.  
  117.     CAnimalInfo & rAnimal = 
  118.         GetDocument()->m_AnimalList.GetAt((POSITION)itemData);
  119.         
  120.     // Insert the 3 pieces of information and expand
  121.     // column widths as necessary.
  122.     clc.InsertItem(0, rAnimal.m_type);
  123.     int w = clc.GetStringWidth(rAnimal.m_type);
  124.     if ( w > clc.GetColumnWidth(0) )
  125.         clc.SetColumnWidth(0, 15 * w / 10);
  126.     
  127.     clc.SetItemText(0, 1, rAnimal.m_animal);
  128.     w = clc.GetStringWidth(rAnimal.m_animal);
  129.     if ( clc.GetColumnWidth(1) - 10 < w )
  130.         clc.SetColumnWidth(1, 17 * w / 10);
  131.     
  132.     CString weight;
  133.     weight.Format("%d", rAnimal.m_weight);
  134.     clc.SetItemText(0, 2, weight);
  135.     w = clc.GetStringWidth(weight);
  136.     if ( clc.GetColumnWidth(1) - 10 < w )
  137.         clc.SetColumnWidth(1, 17 * w / 10);
  138. }
  139.  
  140. void CSimpleListView::OnInitialUpdate() 
  141. {
  142.     GetListCtrl().ModifyStyle(NULL, LVS_REPORT);
  143.  
  144.     CListView::OnInitialUpdate();
  145. }
  146.  
  147. void CSimpleListView::EraseList()
  148. {
  149.     CListCtrl& ctlList = GetListCtrl();
  150.     
  151.     // Remove all the list's data.
  152.     ctlList.DeleteAllItems();
  153.     // Remove all the columns and consequently their headers.
  154.     while(ctlList.DeleteColumn(0));
  155.     // Force a repaint.
  156.     UpdateWindow();
  157. }
  158.  
  159. /////////////////////////////////////////////////////////////////////////////
  160. // CSimpleListView diagnostics
  161. #ifdef _DEBUG
  162. void CSimpleListView::AssertValid() const
  163. {
  164.     CListView::AssertValid();
  165. }
  166.  
  167. void CSimpleListView::Dump(CDumpContext& dc) const
  168. {
  169.     CListView::Dump(dc);
  170. }
  171.  
  172. CTreeDoc* CSimpleListView::GetDocument() // non-debug version is inline
  173. {
  174.     ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CTreeDoc)));
  175.     return (CTreeDoc*)m_pDocument;
  176. }
  177.  
  178. #endif //_DEBUG
  179.  
  180.