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