home *** CD-ROM | disk | FTP | other *** search
/ Mastering MFC Development / MMD.ISO / labs / c06 / lab05 / baseline / mainfrm.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1997-02-20  |  6.6 KB  |  292 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 "diffdoc.h"
  10. #include "diffview.h"
  11. #include "finddiff.h"
  12. #include "MainFrm.h"
  13.  
  14. #ifdef _DEBUG
  15. #undef THIS_FILE
  16. static char THIS_FILE[] = __FILE__;
  17. #endif
  18.  
  19. /////////////////////////////////////////////////////////////////////////////
  20. // CMainFrame
  21.  
  22. static const UINT nMsgFindDifference = 
  23.                     ::RegisterWindowMessage(FINDDIFF_MSGSTRING);
  24.  
  25. IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)
  26.  
  27. BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
  28.     //{{AFX_MSG_MAP(CMainFrame)
  29.     ON_WM_CREATE()
  30.     ON_WM_DROPFILES()
  31.     ON_COMMAND(ID_EDIT_FIND_DIFFERENCE, OnEditFindDiff)
  32.     //}}AFX_MSG_MAP
  33.     //special registerd message for FindDifference Dialog
  34.     ON_REGISTERED_MESSAGE (nMsgFindDifference, OnFindDifferenceCmd)
  35. END_MESSAGE_MAP()
  36.  
  37. static UINT indicators[] =
  38. {
  39.     ID_SEPARATOR,           // status line indicator
  40.     ID_INDICATOR_CAPS,
  41.     ID_INDICATOR_NUM,
  42.     ID_INDICATOR_SCRL,
  43. };
  44.  
  45. /////////////////////////////////////////////////////////////////////////////
  46. // CMainFrame construction/destruction
  47.  
  48. CMainFrame::CMainFrame()
  49. {
  50.     m_pFindDiffDlg = NULL;    
  51. }
  52.  
  53. CMainFrame::~CMainFrame()
  54. {
  55. }
  56.  
  57. int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
  58. {
  59.     if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
  60.         return -1;
  61.     
  62.     if (!m_wndToolBar.Create(this) ||
  63.         !m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
  64.     {
  65.         TRACE0("Failed to create toolbar\n");
  66.         return -1;      // fail to create
  67.     }
  68.  
  69.     if (!m_wndStatusBar.Create(this) ||
  70.         !m_wndStatusBar.SetIndicators(indicators,
  71.           sizeof(indicators)/sizeof(UINT)))
  72.     {
  73.         TRACE0("Failed to create status bar\n");
  74.         return -1;      // fail to create
  75.     }
  76.  
  77.     // TODO: Delete these three lines if you don't want the toolbar to
  78.     //  be dockable
  79.     m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
  80.     EnableDocking(CBRS_ALIGN_ANY);
  81.     DockControlBar(&m_wndToolBar);
  82.  
  83.     // TODO: Remove this if you don't want tool tips
  84.     m_wndToolBar.SetBarStyle(m_wndToolBar.GetBarStyle() |
  85.         CBRS_TOOLTIPS | CBRS_FLYBY);
  86.  
  87.     return 0;
  88. }
  89.  
  90. BOOL CMainFrame::OnCreateClient( LPCREATESTRUCT /*lpcs*/,
  91.     CCreateContext* pContext)
  92. {
  93.         //
  94.         //    For the difference application, we will create a
  95.         //    static splitter window    with 2 side by side panes.
  96.         //    
  97.     if(!m_wndSplitter.CreateStatic(this,1,2,WS_CHILD))
  98.     {
  99.         return FALSE;
  100.     }
  101.  
  102.     SIZE size;                                  
  103.     CRect rect;     
  104.     GetClientRect(&rect);   
  105.         
  106.         //
  107.         //    Calculate the size of the splitter panes
  108.         //
  109.     size.cx = (rect.right - m_wndSplitter.GetSplitterWidth())/2;
  110.     size.cy = rect.bottom;
  111.  
  112.         //
  113.         //    Provide each pane with its own view, the same in this case
  114.         //    but could be different    
  115.         //
  116.     m_wndSplitter.CreateView(0,0,RUNTIME_CLASS(CDiffView),size, pContext);
  117.     m_wndSplitter.CreateView(0,1,RUNTIME_CLASS(CDiffView),size, pContext);
  118.     //SetActiveView((CView *)m_wndSplitter.GetPane(0,1));
  119.   
  120.     m_wndSplitter.ShowWindow(SW_SHOWNORMAL);
  121.     m_wndSplitter.UpdateWindow();
  122.  
  123.     return TRUE;
  124. }
  125.  
  126. BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
  127. {
  128.     // TODO: Modify the Window class or styles here by modifying
  129.     //  the CREATESTRUCT cs
  130.  
  131.     return CFrameWnd::PreCreateWindow(cs);
  132. }
  133.  
  134. //ADDED HERE!!!!!
  135.  
  136. CDiffView *CMainFrame::GetView(int nColumn)
  137. {
  138.     ASSERT(nColumn == 0 || nColumn == 1);
  139.     return (CDiffView *)GetSplitter()->GetPane(0,nColumn);
  140. }
  141.  
  142. /////////////////////////////////////////////////////////////////////////////
  143. // CMainFrame diagnostics
  144.  
  145. #ifdef _DEBUG
  146. void CMainFrame::AssertValid() const
  147. {
  148.     CFrameWnd::AssertValid();
  149. }
  150.  
  151. void CMainFrame::Dump(CDumpContext& dc) const
  152. {
  153.     CFrameWnd::Dump(dc);
  154. }
  155.  
  156. #endif //_DEBUG
  157.  
  158. /////////////////////////////////////////////////////////////////////////////
  159. // CMainFrame message handlers
  160.  
  161. void CMainFrame::OnDropFiles(HDROP hDropInfo) 
  162. {
  163.     UINT nFileCount = ::DragQueryFile (hDropInfo,
  164.                                         0xFFFFFFFF,
  165.                                         NULL, 0 );
  166.     ASSERT (nFileCount !=0);
  167.  
  168.     // we must have at least two files for this function;
  169.     // we will grab only the first two
  170.  
  171.     if (nFileCount >= 2)
  172.     {
  173.         CString File1;
  174.         CString File2;
  175.  
  176.         ::DragQueryFile (hDropInfo,
  177.                         0,
  178.                         File1.GetBufferSetLength (_MAX_PATH),
  179.                         _MAX_PATH);
  180.         File1.ReleaseBuffer();
  181.  
  182.         ::DragQueryFile (hDropInfo,
  183.                         1,
  184.                         File2.GetBufferSetLength (_MAX_PATH),
  185.                         _MAX_PATH);
  186.         File2.ReleaseBuffer();
  187.  
  188.         ((CDiffDoc *)GetActiveDocument())->
  189.                             RunComparison (File1, File2);
  190.  
  191.         ::DragFinish(hDropInfo);
  192.     }
  193.  
  194. }
  195.  
  196. void CMainFrame::OnEditFindDiff() 
  197. {
  198.     //    Create the dialog if needed
  199.  
  200.     if(m_pFindDiffDlg == NULL)
  201.     {
  202.         
  203.         m_pFindDiffDlg = new CFindDifferenceDialog(this);
  204.         if(m_pFindDiffDlg)
  205.         {
  206.             m_pFindDiffDlg->Create();
  207.         }
  208.     }
  209.  
  210.     //    Show it
  211.         
  212.     if(m_pFindDiffDlg)
  213.     {
  214.         m_pFindDiffDlg->SetActiveWindow();
  215.         m_pFindDiffDlg->ShowWindow(SW_SHOW);
  216.     }
  217. }
  218.  
  219.  
  220.  
  221. //handler for the registered message from the modeless dialog
  222. //
  223. LRESULT CMainFrame::OnFindDifferenceCmd(WPARAM, LPARAM lParam)
  224. {
  225.  
  226.     CFindDifferenceDialog* pDialog = (CFindDifferenceDialog *)lParam;
  227.  
  228.     if (pDialog->IsTerminating())
  229.     {
  230.         //    The CFindDifferenceDialog is self deleting
  231.         //    This is its way of letting us know that
  232.         //    its no longer with us
  233.         //
  234.         m_pFindDiffDlg = NULL;
  235.     }
  236.     else if (pDialog->FindNext())
  237.     {
  238.         OnFindNextDifference(pDialog->SearchDown(), 
  239.                              pDialog->FindDifference());
  240.     }
  241.     
  242.     return 0;
  243. }
  244.  
  245.  
  246. void CMainFrame::OnFindNextDifference(BOOL bSearchDown, 
  247.                                       BOOL bNextDifference)
  248. {
  249.     //    This method, when implemented, will scroll the next set
  250.     //    of differences or the next equal sequence into view. This
  251.     //    method is called as a result of the user pressing Find Next
  252.     //  on the Find Differences modeless Dialog
  253.  
  254.  
  255.  
  256.     //    The following code is used only to display some feedback and
  257.     //    does not have anything to do with acually finding differences
  258.     CDiffView * pView = (CDiffView *)GetActiveView();
  259.     if(pView)
  260.     {
  261.         int nLineCnt = pView->GetRichEditCtrl().GetLineCount();
  262.         LONG lStart = 0;
  263.         LONG lEnd    = 0;
  264.         pView->GetRichEditCtrl().GetSel(lStart, lEnd);
  265.         LONG lCurLine = pView->GetRichEditCtrl().LineFromChar(lStart);
  266.  
  267.         //    Find a new line to select
  268.         int nNewLine;
  269.         if(bSearchDown)
  270.         {
  271.             nNewLine = lCurLine + (rand() % (nLineCnt-lCurLine)+1);
  272.         }
  273.         else
  274.         {
  275.             nNewLine = rand() % (lCurLine+1) + 1;
  276.         }
  277.  
  278.         lStart = pView->GetRichEditCtrl().LineIndex(nNewLine);
  279.         lEnd = lStart + pView->GetRichEditCtrl().LineLength(nNewLine);
  280.  
  281.         pView->GetRichEditCtrl().SetSel(lStart, lEnd);
  282.         if (bNextDifference)
  283.         {
  284.             m_wndStatusBar.SetWindowText(_T("Found next difference"));
  285.         }
  286.         else
  287.         {
  288.             m_wndStatusBar.SetWindowText(_T("Found next equal run"));
  289.         }
  290.     }
  291. }
  292.