home *** CD-ROM | disk | FTP | other *** search
/ Mastering MFC Development / MMD.ISO / labs / c06 / lab04 / ex03 / fileopenex.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1997-02-20  |  3.9 KB  |  175 lines

  1. // FileOpenEx.cpp : implementation file
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "diff.h"
  6. #include "FileOpenEx.h"
  7.  
  8. #ifdef _DEBUG
  9. #define new DEBUG_NEW
  10. #undef THIS_FILE
  11. static char THIS_FILE[] = __FILE__;
  12. #endif
  13.  
  14. /////////////////////////////////////////////////////////////////////////////
  15. // CFileOpenEx dialog
  16.  
  17. CFileOpenEx::CFileOpenEx(LPCTSTR lpszDefExt /*= NULL*/,
  18.                         LPCTSTR lpszFileName /*= NULL*/,
  19.                         DWORD dwFlags /*= OFN_HIDEREADONLY*/,
  20.                         LPCTSTR lpszFilter /*= NULL*/,
  21.                         CWnd* pParentWnd /*= NULL*/)
  22.             : CFileDialog(TRUE, lpszDefExt, lpszFileName, dwFlags, lpszFilter, pParentWnd)
  23. {
  24.     //    Always start out folded
  25.     m_bUnfolded = TRUE;
  26.     m_bFirstTime = TRUE;
  27.  
  28.     //{{AFX_DATA_INIT(CFileOpenEx)
  29.         // NOTE: the ClassWizard will add member initialization here
  30.     //}}AFX_DATA_INIT
  31. }
  32.  
  33. void CFileOpenEx::DoDataExchange(CDataExchange* pDX)
  34. {
  35.     CFileDialog::DoDataExchange(pDX);
  36.     //{{AFX_DATA_MAP(CFileOpenEx)
  37.         // NOTE: the ClassWizard will add DDX and DDV calls here
  38.     //}}AFX_DATA_MAP
  39.  
  40.     DDX_Control(pDX, IDC_EDIT_CONTENTS, m_RichEdit);
  41. }
  42.  
  43.  
  44. BEGIN_MESSAGE_MAP(CFileOpenEx, CFileDialog)
  45.     //{{AFX_MSG_MAP(CFileOpenEx)
  46.     ON_WM_SIZE()
  47.     ON_BN_CLICKED(IDC_BUTTON_OPTIONS, OnButtonOptions)
  48.     //}}AFX_MSG_MAP
  49. END_MESSAGE_MAP()
  50.  
  51. /////////////////////////////////////////////////////////////////////////////
  52. // CFileOpenEx message handlers
  53.  
  54. int CFileOpenEx::DoModal() 
  55. {
  56.     SetTemplate(NULL, MAKEINTRESOURCE(IDD_FILEOPEN_EX));    
  57.     return CFileDialog::DoModal();
  58. }
  59.  
  60. void CFileOpenEx::OnSize(UINT nType, int cx, int cy) 
  61. {
  62.     CFileDialog::OnSize(nType, cx, cy);
  63.     
  64.     if(m_bFirstTime)
  65.     {
  66.         m_bFirstTime = FALSE;
  67.         Expand(FALSE);
  68.     }    
  69. }
  70.  
  71. void CFileOpenEx::OnButtonOptions() 
  72. {
  73.     //    Toggle the expanded state
  74.     Expand(!m_bUnfolded);    
  75. }
  76.  
  77. void CFileOpenEx::OnFileNameChange()
  78. {
  79.     //    Called when the filename changes in the Explorer
  80.     //    portion of the Dialog. If unfolded, load the
  81.     //    contents of the file into the RichEdit control
  82.  
  83.     if(m_bUnfolded)
  84.     {
  85.         CString FileName = GetPathName();
  86.         if(FileName.GetLength())
  87.         {
  88.             OFSTRUCT of;
  89.             if(OpenFile(FileName, &of, OF_EXIST) != HFILE_ERROR)
  90.             {
  91.                 PreviewContents(FileName);
  92.             }
  93.         }
  94.     }
  95. }
  96.  
  97. void CFileOpenEx::Expand(const BOOL bExpand)
  98. {
  99.     if( (bExpand && !m_bUnfolded) ||
  100.         (!bExpand && m_bUnfolded) )
  101.     {
  102.         CRect    rectInitDlg, rectNewDlg, rectBasic;
  103.         GetParent()->GetWindowRect(&rectInitDlg);
  104.  
  105.         WORD wSizeCtrl = IDC_FOLDSIZE;
  106.         if(bExpand)
  107.         {
  108.             wSizeCtrl = IDC_UNFOLDSIZE;
  109.         }
  110.         
  111.         CWnd* pSizeMarker = GetDlgItem(wSizeCtrl);
  112.     
  113.         CRect rectMarker;
  114.         pSizeMarker->GetWindowRect(&rectMarker);
  115.         ScreenToClient(&rectMarker);
  116.         
  117.         rectNewDlg.SetRect(0, 0, rectMarker.right, rectMarker.bottom);
  118.         
  119.         ::AdjustWindowRectEx(&rectNewDlg, 
  120.                             GetParent()->GetStyle(),
  121.                             FALSE,
  122.                             GetParent()->GetExStyle());
  123.         
  124.         GetParent()->SetWindowPos(NULL, 
  125.                                 0, 0,
  126.                                 rectNewDlg.Width(), rectNewDlg.Height(),
  127.                                 SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOZORDER);
  128.         
  129.         rectBasic = bExpand?rectInitDlg:rectNewDlg;
  130.         ClientToScreen(&rectBasic);
  131.         EnableOptionalChildren(rectBasic, bExpand);
  132.         m_bUnfolded = bExpand;
  133.     }
  134. }
  135.  
  136. void CFileOpenEx::EnableOptionalChildren(const CRect & rectBasic, const BOOL bEnable)
  137. {
  138.     CRect rectChild;
  139.     
  140.     CWnd *pChild = GetTopWindow();
  141.     
  142.     while (pChild)
  143.     {
  144.         pChild->GetWindowRect(&rectChild);
  145.         
  146.         if(!rectBasic.PtInRect(rectChild.TopLeft()) 
  147.             ||!rectBasic.PtInRect(rectChild.BottomRight()) )
  148.         {
  149.             pChild->EnableWindow(bEnable);
  150.         }
  151.         pChild = pChild->GetNextWindow();
  152.     }
  153. }
  154.  
  155. DWORD CALLBACK OpenCallback(DWORD dwCookie, LPBYTE pbBuff, 
  156.                             LONG cb, LONG * pcb)
  157. {
  158.     CFile *pFile = (CFile *)dwCookie;
  159.     
  160.     *pcb = pFile->Read(pbBuff, cb);
  161.     return 0;
  162. }
  163.  
  164. void CFileOpenEx::PreviewContents(LPCSTR lpszFilespec)
  165. {
  166.     CFile File(lpszFilespec, CFile::modeRead);    
  167.     
  168.     EDITSTREAM es;
  169.     es.dwCookie    = (DWORD)&File;
  170.     es.dwError = 0;
  171.     es.pfnCallback = OpenCallback;
  172.     
  173.     m_RichEdit.StreamIn(SF_TEXT, es);
  174. }
  175.