home *** CD-ROM | disk | FTP | other *** search
/ Mastering MFC Development / MMD.ISO / labs / c05 / lab06 / ex04 / dlgfilex.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1997-02-20  |  4.1 KB  |  199 lines

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