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

  1. // MainFrm.cpp : implementation of the CMainFrame class
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "diff.h"
  6.  
  7. #include "progress.h"
  8. #include "splitter.h"
  9. #include "MainFrm.h"
  10. #include "diffdoc.h"
  11. #include "diffview.h"
  12.  
  13. #ifdef _DEBUG
  14. #undef THIS_FILE
  15. static char THIS_FILE[] = __FILE__;
  16. #endif
  17.  
  18. /////////////////////////////////////////////////////////////////////////////
  19. // CMainFrame
  20.  
  21. IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)
  22.  
  23. BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
  24.     //{{AFX_MSG_MAP(CMainFrame)
  25.     ON_WM_CREATE()
  26.     ON_WM_DROPFILES()
  27.     ON_COMMAND(ID_VIEW_DIALOGBAR, OnViewDialogbar)
  28.     ON_UPDATE_COMMAND_UI(ID_VIEW_DIALOGBAR, OnUpdateViewDialogbar)
  29.     //}}AFX_MSG_MAP
  30. END_MESSAGE_MAP()
  31.  
  32. static UINT indicators[] =
  33. {
  34.     ID_SEPARATOR,           // status line indicator
  35.     ID_INDICATOR_CAPS,
  36.     ID_INDICATOR_NUM,
  37.     ID_INDICATOR_SCRL,
  38. };
  39.  
  40. /////////////////////////////////////////////////////////////////////////////
  41. // CMainFrame construction/destruction
  42.  
  43. CMainFrame::CMainFrame()
  44. {
  45.     // TODO: add member initialization code here
  46.     
  47. }
  48.  
  49. CMainFrame::~CMainFrame()
  50. {
  51. }
  52.  
  53. int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
  54. {
  55.     if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
  56.         return -1;
  57.     
  58.     if (!m_wndToolBar.Create(this) ||
  59.         !m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
  60.     {
  61.         TRACE0("Failed to create toolbar\n");
  62.         return -1;      // fail to create
  63.     }
  64.  
  65.     if (!m_wndStatusBar.Create(this) ||
  66.         !m_wndStatusBar.SetIndicators(indicators,
  67.           sizeof(indicators)/sizeof(UINT)))
  68.     {
  69.         TRACE0("Failed to create status bar\n");
  70.         return -1;      // fail to create
  71.     }
  72.  
  73.     // TODO: Delete these three lines if you don't want the toolbar to
  74.     //  be dockable
  75.     m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
  76.     EnableDocking(CBRS_ALIGN_ANY);
  77.     DockControlBar(&m_wndToolBar);
  78.  
  79.     // TODO: Remove this if you don't want tool tips
  80.     m_wndToolBar.SetBarStyle(m_wndToolBar.GetBarStyle() |
  81.         CBRS_TOOLTIPS | CBRS_FLYBY);
  82.     
  83.     //Added for Dialog Bar Lab
  84.     //Create a dialog bar object as a child of the main frame
  85.     //This dialog bar is, in effect, a modeless dialog
  86.     //The main frame will handle the messages for the dialog bar
  87.  
  88.     m_wndDialogBar.Create( this,    //Parent window is the main frame
  89.                     IDD_DIALOGBAR,    //The graphic resource
  90.                     CBRS_TOP,        //Align to the top of the frame
  91.                     ID_VIEW_DIALOGBAR);    //Control's ID
  92.     
  93.     m_wndDialogBar.EnableDocking(CBRS_ALIGN_TOP | CBRS_ALIGN_BOTTOM);
  94.     EnableDocking(CBRS_ALIGN_ANY);
  95.     DockControlBar(&m_wndDialogBar);
  96.     m_wndDialogBar.SetBarStyle(m_wndDialogBar.GetBarStyle() |
  97.         CBRS_TOOLTIPS | CBRS_FLYBY);
  98.  
  99.     return 0;
  100. }
  101.  
  102. BOOL CMainFrame::OnCreateClient( LPCREATESTRUCT /*lpcs*/,
  103.     CCreateContext* pContext)
  104. {
  105.         //
  106.         //    For the difference application, we will create a
  107.         //    static splitter window    with 2 side by side panes.
  108.         //    
  109.     if(!m_wndSplitter.CreateStatic(this,1,2,WS_CHILD))
  110.     {
  111.         return FALSE;
  112.     }
  113.  
  114.     SIZE size;                                  
  115.     CRect rect;     
  116.     GetClientRect(&rect);   
  117.         
  118.         //
  119.         //    Calculate the size of the splitter panes
  120.         //
  121.     size.cx = (rect.right - m_wndSplitter.GetSplitterWidth())/2;
  122.     size.cy = rect.bottom;
  123.  
  124.         //
  125.         //    Provide each pane with its own view, the same in this case
  126.         //    but could be different    
  127.         //
  128.     m_wndSplitter.CreateView(0,0,RUNTIME_CLASS(CDiffView),size, pContext);
  129.     m_wndSplitter.CreateView(0,1,RUNTIME_CLASS(CDiffView),size, pContext);
  130.     //SetActiveView((CView *)m_wndSplitter.GetPane(0,1));
  131.   
  132.     m_wndSplitter.ShowWindow(SW_SHOWNORMAL);
  133.     m_wndSplitter.UpdateWindow();
  134.  
  135.     return TRUE;
  136. }
  137.  
  138. BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
  139. {
  140.     // TODO: Modify the Window class or styles here by modifying
  141.     //  the CREATESTRUCT cs
  142.  
  143.     return CFrameWnd::PreCreateWindow(cs);
  144. }
  145.  
  146. /////////////////////////////////////////////////////////////////////////////
  147. // CMainFrame diagnostics
  148.  
  149. #ifdef _DEBUG
  150. void CMainFrame::AssertValid() const
  151. {
  152.     CFrameWnd::AssertValid();
  153. }
  154.  
  155. void CMainFrame::Dump(CDumpContext& dc) const
  156. {
  157.     CFrameWnd::Dump(dc);
  158. }
  159.  
  160. #endif //_DEBUG
  161.  
  162. /////////////////////////////////////////////////////////////////////////////
  163. // CMainFrame message handlers
  164.  
  165. void CMainFrame::OnDropFiles(HDROP hDropInfo) 
  166. {
  167.     UINT nFileCount = ::DragQueryFile (hDropInfo,
  168.                                         0xFFFFFFFF,
  169.                                         NULL, 0 );
  170.     ASSERT (nFileCount !=0);
  171.  
  172.     // we must have at least two files for this function;
  173.     // we will grab only the first two
  174.  
  175.     if (nFileCount >= 2)
  176.     {
  177.         CString File1;
  178.         CString File2;
  179.  
  180.         ::DragQueryFile (hDropInfo,
  181.                         0,
  182.                         File1.GetBufferSetLength (_MAX_PATH),
  183.                         _MAX_PATH);
  184.         File1.ReleaseBuffer();
  185.  
  186.         ::DragQueryFile (hDropInfo,
  187.                         1,
  188.                         File2.GetBufferSetLength (_MAX_PATH),
  189.                         _MAX_PATH);
  190.         File2.ReleaseBuffer();
  191.  
  192.         ((CDiffDoc *)GetActiveDocument())->
  193.                             RunComparison (File1, File2);
  194.  
  195.         ::DragFinish(hDropInfo);
  196.     }
  197.  
  198. }
  199.  
  200. void CMainFrame::OnViewDialogbar() 
  201. {
  202.     // TODO: Add your command handler code here
  203.     //Dialog Bar lab
  204.     ShowControlBar( & m_wndDialogBar,
  205.         ! m_wndDialogBar.IsWindowVisible( ),FALSE );
  206. }
  207.  
  208. void CMainFrame::OnUpdateViewDialogbar(CCmdUI* pCmdUI) 
  209. {
  210.     // TODO: Add your command update UI handler code here
  211.     //Dialog Bar lab
  212.     pCmdUI->SetCheck( m_wndDialogBar.IsWindowVisible( ) );
  213. }
  214.  
  215. void CMainFrame::SetList( int nID, const CString & strFile )
  216. {
  217.     //Dialog Bar lab
  218.     AddItem( nID, strFile );
  219.     CComboBox * pCmb = 
  220.         ( CComboBox * ) m_wndDialogBar.GetDlgItem( nID );
  221.     pCmb->SelectString( -1, strFile ); //Start search at top
  222. }
  223.  
  224. void CMainFrame::AddItem( int nID, const CString & str )
  225. {
  226.     //Dialog Bar lab
  227.     CComboBox * pCmb = 
  228.         ( CComboBox * ) m_wndDialogBar.GetDlgItem( nID );
  229.     if ( CB_ERR == pCmb->FindString( -1, str ) )//Search from top
  230.         pCmb->AddString( str );
  231. }
  232.  
  233. void CMainFrame::ResetFile( int nID, const CString & str )
  234. {
  235.     //Dialog Bar lab
  236.     if ( str.IsEmpty( ) )  //Nothing to do
  237.         return;
  238.     int pane = LEFT == nID ? 0 : 1; //pane 0 is left, pane 1 is right
  239.     
  240.     //Get the appropriate pane, Row 0, Column left or right
  241.     CDiffView * pView = ( CDiffView * ) m_wndSplitter.GetPane( 0, pane );
  242.     
  243.     //Load the pane from the drive using the view's Serialize function
  244.     if ( NULL != pView )
  245.     {
  246.         CFile file( str, CFile::modeRead );
  247.         CArchive ar( & file, CArchive::load );
  248.         pView->Serialize( ar );
  249.  
  250.         //Update the document data member (we're a friend)
  251.         CDiffDoc * pDoc = pView->GetDocument( );
  252.         if ( LEFT == nID )
  253.             pDoc->m_File1 = str;
  254.         else
  255.             pDoc->m_File2 = str;
  256.     }
  257. }
  258.  
  259.  
  260.