home *** CD-ROM | disk | FTP | other *** search
/ Mastering MFC Development / MMD.ISO / labs / c05 / lab06 / ex04 / mainfrm.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1997-02-20  |  7.3 KB  |  294 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.     //Dialog Bar lab
  221.     
  222.     CComboBox * pCmb = 
  223.         ( CComboBox * ) m_wndDialogBar.GetDlgItem( LEFT );
  224.     CString str;
  225.     pCmb->GetWindowText( str );  //Get the selected text
  226.     AddItem( LEFT, str );
  227.     ResetFile( LEFT, str );
  228. }
  229.  
  230. void CMainFrame::SetList( int nID, const CString & strFile )
  231. {
  232.     //Dialog Bar lab
  233.     AddItem( nID, strFile );
  234.     CComboBox * pCmb = 
  235.         ( CComboBox * ) m_wndDialogBar.GetDlgItem( nID );
  236.     pCmb->SelectString( -1, strFile ); //Start search at top
  237. }
  238.  
  239. void CMainFrame::AddItem( int nID, const CString & str )
  240. {
  241.     //Dialog Bar lab
  242.     CComboBox * pCmb = 
  243.         ( CComboBox * ) m_wndDialogBar.GetDlgItem( nID );
  244.     if ( CB_ERR == pCmb->FindString( -1, str ) )//Search from top
  245.         pCmb->AddString( str );
  246. }
  247.  
  248. void CMainFrame::ResetFile( int nID, const CString & str )
  249. {
  250.     //Dialog Bar lab
  251.     if ( str.IsEmpty( ) )  //Nothing to do
  252.         return;
  253.     int pane = LEFT == nID ? 0 : 1; //pane 0 is left, pane 1 is right
  254.     
  255.     //Get the appropriate pane, Row 0, Column left or right
  256.     CDiffView * pView = ( CDiffView * ) m_wndSplitter.GetPane( 0, pane );
  257.     
  258.     //Load the pane from the drive using the view's Serialize function
  259.     if ( NULL != pView )
  260.     {
  261.         CFile file( str, CFile::modeRead );
  262.         CArchive ar( & file, CArchive::load );
  263.         pView->Serialize( ar );
  264.  
  265.         //Update the document data member (we're a friend)
  266.         CDiffDoc * pDoc = pView->GetDocument( );
  267.         if ( LEFT == nID )
  268.             pDoc->m_File1 = str;
  269.         else
  270.             pDoc->m_File2 = str;
  271.     }
  272. }
  273.  
  274. BOOL CMainFrame::OnCommand(WPARAM wParam, LPARAM lParam) 
  275. {
  276.     // TODO: Add your specialized code here and/or call the base class
  277.     //Dialog Bar lab
  278.     
  279.     int msg  = HIWORD( wParam );  //Get the message
  280.     int ctrl = LOWORD( wParam );  //Get the message source
  281.  
  282.     //Check for notification message from the right-side combo
  283.     if ( CBN_SELENDOK == msg && IDC_RIGHT == ctrl )        
  284.     {
  285.         CString str;
  286.         CComboBox * pCmb = 
  287.             ( CComboBox * ) m_wndDialogBar.GetDlgItem( ctrl );
  288.         pCmb->GetWindowText( str );  //Get selected text
  289.         ResetFile( ctrl, str );      //Change to the selected file
  290.     }
  291.     
  292.     return CFrameWnd::OnCommand(wParam, lParam);
  293. }
  294.