home *** CD-ROM | disk | FTP | other *** search
/ SuperHack / SuperHack CD.bin / CODING / CPP / METAKIT.ZIP / EXAMPLES / CATRECV / TREEDLG.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1996-12-09  |  5.8 KB  |  207 lines

  1. //    treedlg.cpp  -  catalog tree display sample code
  2. //
  3. //    This is a part of the MetaKit library.
  4. //    Copyright (c) 1996 Meta Four Software.
  5. //    All rights reserved.
  6. /////////////////////////////////////////////////////////////////////////////
  7.  
  8. #include "stdafx.h"
  9. #include "catrecv.h"
  10. #include "TreeDlg.h"
  11.  
  12. #ifdef _DEBUG
  13. //#define new DEBUG_NEW
  14. #undef THIS_FILE
  15. static char THIS_FILE[] = __FILE__;
  16. #endif
  17.  
  18. /////////////////////////////////////////////////////////////////////////////
  19. // These are the properties used in the directory catalog
  20.  
  21.     static c4_ViewProp        pDirs ("dirs"),
  22.                             pFiles ("files");
  23.     static c4_IntProp        pParent ("parent"),
  24.                             pSize ("size"),
  25.                             pDate ("date");
  26.     static c4_StringProp    pName ("name");
  27.  
  28. /////////////////////////////////////////////////////////////////////////////
  29. // CTreeDialog dialog
  30.  
  31. CTreeDialog::CTreeDialog (CFile& file_)
  32. {
  33.     _storage.LoadFromStream(&file_);    // load the data
  34.     _dirs = pDirs (_storage.Contents());
  35.  
  36.     //{{AFX_DATA_INIT(CTreeDialog)
  37.     //}}AFX_DATA_INIT
  38.  
  39.     Create(CTreeDialog::IDD);            // modeless stuff
  40. }
  41.  
  42. void CTreeDialog::OnCancel()
  43. {
  44.     DestroyWindow();                    // modeless stuff
  45. }
  46.  
  47. void CTreeDialog::PostNcDestroy()
  48. {
  49.     CDialog::PostNcDestroy();            // modeless stuff
  50.     delete this;
  51. }
  52.  
  53. void CTreeDialog::DoDataExchange(CDataExchange* pDX)
  54. {
  55.     CDialog::DoDataExchange(pDX);
  56.     //{{AFX_DATA_MAP(CTreeDialog)
  57.     DDX_Control(pDX, IDC_FILE_LIST, m_fileList);
  58.     DDX_Control(pDX, IDC_DIR_TREE, m_dirTree);
  59.     //}}AFX_DATA_MAP
  60. }
  61.  
  62. BEGIN_MESSAGE_MAP(CTreeDialog, CDialog)
  63.     //{{AFX_MSG_MAP(CTreeDialog)
  64.     ON_WM_SIZE()
  65.     ON_NOTIFY(TVN_SELCHANGED, IDC_DIR_TREE, OnSelchangedDirTree)
  66.     //}}AFX_MSG_MAP
  67. END_MESSAGE_MAP()
  68.  
  69. /////////////////////////////////////////////////////////////////////////////
  70. // [JCW,960119]  This code added for MetaKit CATRECV
  71.  
  72. BOOL CTreeDialog::OnInitDialog() 
  73. {
  74.     CDialog::OnInitDialog();
  75.  
  76.         // find out how wide the '0' character is in pixels
  77.     CDC* pDC = GetDC();
  78.     ASSERT(pDC);
  79.     int w = pDC->GetOutputTextExtent("0").cx;
  80.     ReleaseDC(pDC);
  81.  
  82.         // set up three columns with reasonable starting widths
  83.     VERIFY(m_fileList.InsertColumn(0, "Name", LVCFMT_LEFT, 12 * w) == 0);
  84.     VERIFY(m_fileList.InsertColumn(1, "Size", LVCFMT_RIGHT, 7 * w, 1) == 1);
  85.     VERIFY(m_fileList.InsertColumn(2, "Date", LVCFMT_LEFT, 6 * w, 2) == 2);
  86.   
  87.     RecalcLayout();
  88.  
  89.         // display the catalog contents
  90.     if (_dirs.GetSize() > 0)
  91.     {
  92.         CString path = pName (_dirs[0]);
  93.         SetWindowText("CATRECV - " + path);
  94.  
  95.         SetupDirTree();
  96.     }
  97.  
  98.     return TRUE;  // return TRUE unless you set the focus to a control
  99. }
  100.  
  101. // track changes in the main window size
  102. void CTreeDialog::OnSize(UINT nType, int cx, int cy) 
  103. {
  104.     CDialog::OnSize(nType, cx, cy);
  105.     
  106.     if (m_dirTree.m_hWnd && m_fileList.m_hWnd)
  107.         RecalcLayout();
  108. }
  109.  
  110. // share the client area between the tree control and the list control
  111. void CTreeDialog::RecalcLayout()
  112. {
  113.     CRect rect;
  114.     GetClientRect(&rect);
  115.     
  116.     ASSERT(rect.left == 0 && rect.top == 0);
  117.     int cx = rect.right;
  118.     int cy = rect.bottom;
  119.  
  120.     m_dirTree.MoveWindow(0, 0, cx/2, cy);
  121.     m_fileList.MoveWindow(cx/2, 0, cx - cx/2, cy); // careful with roundoff
  122. }
  123.  
  124. // Initialize the contents of the tree control with the directory structure
  125. // in the catalog that was just received. This version stores a copy of all
  126. // directory names in the tree control, could be optimized to use a callback
  127. // function to grab the name from the catalog whenever the control needs it.
  128. void CTreeDialog::SetupDirTree()
  129. {
  130.     HTREEITEM root, parent, item;
  131.  
  132.         // the root needs to be treated a little differently
  133.     root = m_dirTree.InsertItem(TVIF_TEXT | TVIF_PARAM, "(root)",
  134.                                         0, 0, 0, 0, 0, 0, TVI_SORT);
  135.  
  136.         // remember all tree control item handles, one per directory
  137.     _treeHandles.SetAtGrow(0, root);
  138.  
  139.         // now enter all remaining directories in the tree control
  140.     for (int i = 1; i < _dirs.GetSize(); ++i)
  141.     {
  142.         c4_RowRef thisDir = _dirs[i];
  143.  
  144.             // extract the relevant information from the catalog
  145.         CString dirName = pName (thisDir);
  146.         parent = (HTREEITEM) _treeHandles[pParent (thisDir)];
  147.  
  148.             // insert an item in the tree and remember its handle
  149.         item = m_dirTree.InsertItem(TVIF_TEXT | TVIF_PARAM, dirName,
  150.                                         0, 0, 0, 0, i, parent, TVI_SORT);
  151.         _treeHandles.SetAtGrow(i, item);
  152.     }
  153.  
  154.         // start off with a tree with an expanded root level
  155.     m_dirTree.Expand(root, TVE_EXPAND);
  156.  
  157.         // simulate a selection change (args are not really used here)
  158.     OnSelchangedDirTree(0, 0);
  159. }
  160.  
  161. // Update the file list when the directory selection changes. Again, a copy
  162. // of all strings is stored in the list control instead of using callbacks.
  163. void CTreeDialog::OnSelchangedDirTree(NMHDR* /* pNMHDR */, LRESULT* pResult) 
  164. {
  165.     VERIFY(m_fileList.DeleteAllItems());
  166.  
  167.     HTREEITEM item = m_dirTree.GetSelectedItem();
  168.     if (item)
  169.     {
  170.             // locate the corresponding list of files in the catalog
  171.         int dirIndex = m_dirTree.GetItemData(item);
  172.         c4_View files = pFiles (_dirs[dirIndex]);
  173.         
  174.         for (int i = 0; i < files.GetSize(); ++i)
  175.         {
  176.                 // extract the information from the catalog
  177.             CString name = pName (files[i]);
  178.             long size = pSize (files[i]);
  179.             long date = pDate (files[i]);
  180.  
  181.                 // create the main file entry
  182.             int n = m_fileList.InsertItem(i, name);
  183.             VERIFY(n >= 0);
  184.  
  185.             char buf [15];
  186.  
  187.                 // set subitem 1 to the file size
  188.             wsprintf(buf, "%ld", size);
  189.             m_fileList.SetItemText(n, 1, buf);
  190.  
  191.                 // set subitem 2 to the file date
  192.             if (date)
  193.             {
  194.                 wsprintf(buf, "%02d%02d%02d", 80 + (date >> 9),
  195.                                         (date >> 5) & 0x0F, date & 0x1F);
  196.                 m_fileList.SetItemText(n, 2, buf);
  197.             }
  198.         }
  199.     }
  200.     
  201.     if (pResult) // careful, this is null when called from SetupDirTree
  202.         *pResult = 0;
  203. }
  204.  
  205. /////////////////////////////////////////////////////////////////////////////
  206. // $Id: treedlg.cpp,v 1.2 1996/12/04 14:49:52 jcw Exp $
  207.