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

  1. // TimgView.cpp : implementation of the CTreeWithImagesView class
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "TreeWimg.h"
  6.  
  7. #include "TimgDoc.h"
  8. #include "TimgView.h"
  9.  
  10. #ifdef _DEBUG
  11. #define new DEBUG_NEW
  12. #undef THIS_FILE
  13. static char THIS_FILE[] = __FILE__;
  14. #endif
  15.  
  16. /////////////////////////////////////////////////////////////////////////////
  17. // CTreeWithImagesView
  18.  
  19. IMPLEMENT_DYNCREATE(CTreeWithImagesView, CTreeView)
  20.  
  21. BEGIN_MESSAGE_MAP(CTreeWithImagesView, CTreeView)
  22.     //{{AFX_MSG_MAP(CTreeWithImagesView)
  23.         // NOTE - the ClassWizard will add and remove mapping macros here.
  24.         //    DO NOT EDIT what you see in these blocks of generated code!
  25.     //}}AFX_MSG_MAP
  26.     // Standard printing commands
  27.     ON_COMMAND(ID_FILE_PRINT, CTreeView::OnFilePrint)
  28.     ON_COMMAND(ID_FILE_PRINT_DIRECT, CTreeView::OnFilePrint)
  29.     ON_COMMAND(ID_FILE_PRINT_PREVIEW, CTreeView::OnFilePrintPreview)
  30. END_MESSAGE_MAP()
  31.  
  32. /////////////////////////////////////////////////////////////////////////////
  33. // CTreeWithImagesView construction/destruction
  34.  
  35. CTreeWithImagesView::CTreeWithImagesView()
  36. {
  37.     cim = 0;
  38. }
  39.  
  40. CTreeWithImagesView::~CTreeWithImagesView()
  41. {
  42.     if (cim)
  43.     {
  44.         delete cim;
  45.         cim = 0;
  46.     }
  47. }
  48.  
  49. BOOL CTreeWithImagesView::PreCreateWindow(CREATESTRUCT& cs)
  50. {
  51.     return CTreeView::PreCreateWindow(cs);
  52. }
  53.  
  54. /////////////////////////////////////////////////////////////////////////////
  55. // CTreeWithImagesView drawing
  56.  
  57. void CTreeWithImagesView::OnDraw(CDC* pDC)
  58. {
  59.     CTreeWithImagesDoc* pDoc = GetDocument();
  60.     ASSERT_VALID(pDoc);
  61. }
  62.  
  63. void CTreeWithImagesView::OnInitialUpdate()
  64. {
  65.     CTreeView::OnInitialUpdate();
  66.  
  67.     GetTreeCtrl().ModifyStyle(NULL,
  68.         TVS_HASBUTTONS | TVS_HASLINES | TVS_LINESATROOT);
  69.     
  70.     InsertBitmaps();
  71. }
  72.  
  73. void CTreeWithImagesView::OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint) 
  74. {
  75.     GetTreeCtrl().DeleteAllItems();
  76.     InsertAnimals();
  77. }
  78.  
  79. /////////////////////////////////////////////////////////////////////////////
  80. // CTreeWithImagesView printing
  81.  
  82. BOOL CTreeWithImagesView::OnPreparePrinting(CPrintInfo* pInfo)
  83. {
  84.     // default preparation
  85.     return DoPreparePrinting(pInfo);
  86. }
  87.  
  88. void CTreeWithImagesView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  89. {
  90.     // TODO: add extra initialization before printing
  91. }
  92.  
  93. void CTreeWithImagesView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  94. {
  95.     // TODO: add cleanup after printing
  96. }
  97.  
  98. /////////////////////////////////////////////////////////////////////////////
  99. // CTreeWithImagesView diagnostics
  100.  
  101. #ifdef _DEBUG
  102. void CTreeWithImagesView::AssertValid() const
  103. {
  104.     CTreeView::AssertValid();
  105. }
  106.  
  107. void CTreeWithImagesView::Dump(CDumpContext& dc) const
  108. {
  109.     CTreeView::Dump(dc);
  110. }
  111.  
  112. CTreeWithImagesDoc* CTreeWithImagesView::GetDocument() // non-debug version is inline
  113. {
  114.     ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CTreeWithImagesDoc)));
  115.     return (CTreeWithImagesDoc*)m_pDocument;
  116. }
  117. #endif //_DEBUG
  118.  
  119. /////////////////////////////////////////////////////////////////////////////
  120. // CTreeWithImagesView message handlers
  121. void CTreeWithImagesView::InsertBitmaps()
  122. {
  123.     // Create the CImageList. It's destroyed in the destructor.
  124.     cim = new CImageList();
  125.     cim->Create(BITMAP_WIDTH, BITMAP_HEIGHT, TRUE, NUM_BITMAPS, 0);
  126.  
  127.     CBitmap bitmap;
  128.  
  129.     // Load the bitmaps and add them to the image list.
  130.     for (int i = IDB_AMPHIBIAN; i <= IDB_REPTILE_SELECTED; i++)
  131.     {
  132.         bitmap.LoadBitmap(i);
  133.         cim->Add(&bitmap, (COLORREF)0xFFFFFF);
  134.         bitmap.DeleteObject();
  135.     }
  136.  
  137.     // Associate the image list with the tree control.
  138.     GetTreeCtrl().SetImageList(cim, TVSIL_NORMAL);
  139. }
  140.  
  141. void CTreeWithImagesView::InsertAnimals()
  142. {
  143.     static char * classes[] = {  "amphibians", "birds", "fishes",
  144.                                 "mammals", "reptiles", NULL };
  145.     static char * types[][4] = { "frogs", "toads", "salamanders", NULL,
  146.                                 "eagles", "owls", "falcons", NULL,
  147.                                 "trout", "perch", "bass", NULL,
  148.                                 "bears", "whales", "rodents", NULL,
  149.                                 "snakes", "turtles", "lizards", NULL };
  150.  
  151.     HTREEITEM hSubTree;
  152.     int ImageListIndex = 0;
  153.  
  154.     CTreeCtrl & ctc = GetTreeCtrl();
  155.  
  156.     for (int r = 0; r < 5; r++)
  157.     {
  158.         hSubTree = ctc.InsertItem(classes[r], TVI_ROOT, TVI_SORT);
  159.         ctc.SetItemImage(hSubTree, ImageListIndex, ImageListIndex + 1);
  160.         // The string from the classes array is placed at the root...
  161.         // and the newly-inserted node serves as parent to the types.
  162.         InsertNodes(hSubTree, types[r], ImageListIndex, ImageListIndex + 1);
  163.         ImageListIndex += 2;
  164.     }
  165.  
  166.     static char * beartypes[] = { "grizzly", "polar", "black", NULL };
  167.     HTREEITEM hSubTree2;
  168.  
  169.     // Now that the tree is built, the bears node will have children
  170.     // added to it. This requires 2 lookup operations. 
  171.     hSubTree = FindNode(ctc.GetRootItem(), classes[3]);
  172.     if (NULL != hSubTree)
  173.     {
  174.         hSubTree2 = FindNode(hSubTree, types[3][0]);
  175.         if (NULL != hSubTree2)
  176.             InsertNodes(hSubTree2, beartypes, MAMMAL, MAMMAL + 1);
  177.     }
  178. }
  179.  
  180. // The function iterates over the children of ParentNode looking for a match.
  181. // If ParentNode is NULL, the search starts in the root, otherwise it starts
  182. // in the designated node. Returns NULL if not found, the HTREEITEM if found.
  183. HTREEITEM CTreeWithImagesView::FindNode(const HTREEITEM ParentNode,
  184.                                         const CString &str) const
  185. {
  186.     CTreeCtrl & ctc = GetTreeCtrl();
  187.     HTREEITEM node;
  188.     CString s;
  189.  
  190.     if (NULL == ParentNode)
  191.         node = ctc.GetRootItem();
  192.     else
  193.         if (ctc.GetRootItem() == ParentNode)
  194.             node = ParentNode;
  195.         else
  196.             node = ctc.GetChildItem(ParentNode);
  197.     
  198.     while (node != NULL)
  199.     {
  200.         s = ctc.GetItemText(node);
  201.         // Halt the search when we find what we're looking for.
  202.         if (0 == s.CompareNoCase(str))
  203.             return node;
  204.         node = ctc.GetNextItem(node, TVGN_NEXT);
  205.     }
  206.     // If we get to here, string was never found, and node == NULL.
  207.     return node;
  208. }
  209.  
  210. // This function inserts the array of strings pointed to by the 2nd
  211. // argument into the tree at the node represented by the first
  212. // argument. All nodes have the same 2 image IDs.
  213. void CTreeWithImagesView::InsertNodes(const HTREEITEM hSubTree, char ** AnimalClass,
  214.                             const int ImageID, const int SelectedImageID)
  215. {
  216.     HTREEITEM node;
  217.     CTreeCtrl & ctc = GetTreeCtrl();
  218.     
  219.     int i = 0;
  220.     while (AnimalClass[i])
  221.     {
  222.         // Insert a node as a child of hSubTree and set its images.
  223.         node = ctc.InsertItem(AnimalClass[i++], hSubTree, TVI_SORT);
  224.         ctc.SetItemImage(node, ImageID, SelectedImageID);
  225.     }
  226. }
  227.