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