home *** CD-ROM | disk | FTP | other *** search
/ Mastering MFC Development / MMD.ISO / labs / c06 / lab05 / ex03 / mainfrm.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1997-02-20  |  9.1 KB  |  390 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. #include "Pages.h"
  15.  
  16. #ifdef _DEBUG
  17. #undef THIS_FILE
  18. static char THIS_FILE[] = __FILE__;
  19. #endif
  20.  
  21. /////////////////////////////////////////////////////////////////////////////
  22. // CMainFrame
  23.  
  24. static const UINT nMsgFindDifference = 
  25.                     ::RegisterWindowMessage(FINDDIFF_MSGSTRING);
  26.  
  27. IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)
  28.  
  29. BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
  30.     //{{AFX_MSG_MAP(CMainFrame)
  31.     ON_WM_CREATE()
  32.     ON_WM_DROPFILES()
  33.     ON_COMMAND(ID_EDIT_FIND_DIFFERENCE, OnEditFindDiff)
  34.     ON_COMMAND(ID_VIEW_PREFERENCES, OnViewPreferences)
  35.     //}}AFX_MSG_MAP
  36.     //special registerd message for FindDifference Dialog
  37.     ON_REGISTERED_MESSAGE (nMsgFindDifference, OnFindDifferenceCmd)
  38. END_MESSAGE_MAP()
  39.  
  40. static UINT indicators[] =
  41. {
  42.     ID_SEPARATOR,           // status line indicator
  43.     ID_INDICATOR_CAPS,
  44.     ID_INDICATOR_NUM,
  45.     ID_INDICATOR_SCRL,
  46. };
  47.  
  48. /////////////////////////////////////////////////////////////////////////////
  49. // CMainFrame construction/destruction
  50.  
  51. CMainFrame::CMainFrame()
  52. {
  53.     m_pFindDiffDlg = NULL;
  54.     m_bIgnoreCase = FALSE;
  55.     m_bIgnoreWhiteSpace = FALSE;
  56.     m_uReadAheadOptLevel = 2;
  57. }
  58.  
  59. CMainFrame::~CMainFrame()
  60. {
  61. }
  62.  
  63. int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
  64. {
  65.     if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
  66.         return -1;
  67.     
  68.     if (!m_wndToolBar.Create(this) ||
  69.         !m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
  70.     {
  71.         TRACE0("Failed to create toolbar\n");
  72.         return -1;      // fail to create
  73.     }
  74.  
  75.     if (!m_wndStatusBar.Create(this) ||
  76.         !m_wndStatusBar.SetIndicators(indicators,
  77.           sizeof(indicators)/sizeof(UINT)))
  78.     {
  79.         TRACE0("Failed to create status bar\n");
  80.         return -1;      // fail to create
  81.     }
  82.  
  83.     // TODO: Delete these three lines if you don't want the toolbar to
  84.     //  be dockable
  85.     m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
  86.     EnableDocking(CBRS_ALIGN_ANY);
  87.     DockControlBar(&m_wndToolBar);
  88.  
  89.     // TODO: Remove this if you don't want tool tips
  90.     m_wndToolBar.SetBarStyle(m_wndToolBar.GetBarStyle() |
  91.         CBRS_TOOLTIPS | CBRS_FLYBY);
  92.  
  93.     return 0;
  94. }
  95.  
  96. BOOL CMainFrame::OnCreateClient( LPCREATESTRUCT /*lpcs*/,
  97.     CCreateContext* pContext)
  98. {
  99.  
  100.         //
  101.         //    For the difference application, we will create a
  102.         //    static splitter window    with 2 side by side panes.
  103.         //    
  104.     if(!m_wndSplitter.CreateStatic(this,1,2,WS_CHILD))
  105.     {
  106.         return FALSE;
  107.     }
  108.  
  109.     SIZE size;                                  
  110.     CRect rect;     
  111.     GetClientRect(&rect);   
  112.         
  113.         //
  114.         //    Calculate the size of the splitter panes
  115.         //
  116.     size.cx = (rect.right - m_wndSplitter.GetSplitterWidth())/2;
  117.     size.cy = rect.bottom;
  118.  
  119.         //
  120.         //    Provide each pane with its own view, the same in this case
  121.         //    but could be different    
  122.         //
  123.     m_wndSplitter.CreateView(0,0,RUNTIME_CLASS(CDiffView),size, pContext);
  124.     m_wndSplitter.CreateView(0,1,RUNTIME_CLASS(CDiffView),size, pContext);
  125.     //SetActiveView((CView *)m_wndSplitter.GetPane(0,1));
  126.   
  127.     m_wndSplitter.ShowWindow(SW_SHOWNORMAL);
  128.     m_wndSplitter.UpdateWindow();
  129.  
  130.     return TRUE;
  131. }
  132.  
  133. BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
  134. {
  135.     // TODO: Modify the Window class or styles here by modifying
  136.     //  the CREATESTRUCT cs
  137.  
  138.     return CFrameWnd::PreCreateWindow(cs);
  139. }
  140.  
  141. //ADDED HERE!!!!!
  142.  
  143. CDiffView *CMainFrame::GetView(int nColumn)
  144. {
  145.     ASSERT(nColumn == 0 || nColumn == 1);
  146.     return (CDiffView *)GetSplitter()->GetPane(0,nColumn);
  147. }
  148.  
  149. /////////////////////////////////////////////////////////////////////////////
  150. // CMainFrame diagnostics
  151.  
  152. #ifdef _DEBUG
  153. void CMainFrame::AssertValid() const
  154. {
  155.     CFrameWnd::AssertValid();
  156. }
  157.  
  158. void CMainFrame::Dump(CDumpContext& dc) const
  159. {
  160.     CFrameWnd::Dump(dc);
  161. }
  162.  
  163. #endif //_DEBUG
  164.  
  165. /////////////////////////////////////////////////////////////////////////////
  166. // CMainFrame message handlers
  167.  
  168. void CMainFrame::OnDropFiles(HDROP hDropInfo) 
  169. {
  170.     UINT nFileCount = ::DragQueryFile (hDropInfo,
  171.                                         0xFFFFFFFF,
  172.                                         NULL, 0 );
  173.     ASSERT (nFileCount !=0);
  174.  
  175.     // we must have at least two files for this function;
  176.     // we will grab only the first two
  177.  
  178.     if (nFileCount >= 2)
  179.     {
  180.         CString File1;
  181.         CString File2;
  182.  
  183.         ::DragQueryFile (hDropInfo,
  184.                         0,
  185.                         File1.GetBufferSetLength (_MAX_PATH),
  186.                         _MAX_PATH);
  187.         File1.ReleaseBuffer();
  188.  
  189.         ::DragQueryFile (hDropInfo,
  190.                         1,
  191.                         File2.GetBufferSetLength (_MAX_PATH),
  192.                         _MAX_PATH);
  193.         File2.ReleaseBuffer();
  194.  
  195.         ((CDiffDoc *)GetActiveDocument())->
  196.                             RunComparison (File1, File2);
  197.  
  198.         ::DragFinish(hDropInfo);
  199.     }
  200.  
  201. }
  202.  
  203. void CMainFrame::OnEditFindDiff() 
  204. {
  205.     //    Create the dialog if needed
  206.  
  207.     if(m_pFindDiffDlg == NULL)
  208.     {
  209.         
  210.         m_pFindDiffDlg = new CFindDifferenceDialog(this);
  211.         if(m_pFindDiffDlg)
  212.         {
  213.             m_pFindDiffDlg->Create();
  214.         }
  215.     }
  216.  
  217.     //    Show it
  218.         
  219.     if(m_pFindDiffDlg)
  220.     {
  221.         m_pFindDiffDlg->SetActiveWindow();
  222.         m_pFindDiffDlg->ShowWindow(SW_SHOW);
  223.     }
  224. }
  225.  
  226.  
  227.  
  228. //handler for the registered message from the modeless dialog
  229. //
  230. LRESULT CMainFrame::OnFindDifferenceCmd(WPARAM, LPARAM lParam)
  231. {
  232.  
  233.     CFindDifferenceDialog* pDialog = (CFindDifferenceDialog *)lParam;
  234.  
  235.     if (pDialog->IsTerminating())
  236.     {
  237.         //    The CFindDifferenceDialog is self deleting
  238.         //    This is its way of letting us know that
  239.         //    its no longer with us
  240.         //
  241.         m_pFindDiffDlg = NULL;
  242.     }
  243.     else if (pDialog->FindNext())
  244.     {
  245.         OnFindNextDifference(pDialog->SearchDown(), 
  246.                              pDialog->FindDifference());
  247.     }
  248.     
  249.     return 0;
  250. }
  251.  
  252.  
  253. void CMainFrame::OnFindNextDifference(BOOL bSearchDown, 
  254.                                       BOOL bNextDifference)
  255. {
  256.     //    This method, when implemented, will scroll the next set
  257.     //    of differences or the next equal sequence into view. This
  258.     //    method is called as a result of the user pressing Find Next
  259.     //  on the Find Differences modeless Dialog
  260.  
  261.  
  262.  
  263.     //    The following code is used only to display some feedback and
  264.     //    does not have anything to do with acually finding differences
  265.     CDiffView * pView = (CDiffView *)GetActiveView();
  266.     if(pView)
  267.     {
  268.         int nLineCnt = pView->GetRichEditCtrl().GetLineCount();
  269.         LONG lStart = 0;
  270.         LONG lEnd    = 0;
  271.         pView->GetRichEditCtrl().GetSel(lStart, lEnd);
  272.         LONG lCurLine = pView->GetRichEditCtrl().LineFromChar(lStart);
  273.  
  274.         //    Find a new line to select
  275.         int nNewLine;
  276.         if(bSearchDown)
  277.         {
  278.             nNewLine = lCurLine + (rand() % (nLineCnt-lCurLine)+1);
  279.         }
  280.         else
  281.         {
  282.             nNewLine = rand() % (lCurLine+1) + 1;
  283.         }
  284.  
  285.         lStart = pView->GetRichEditCtrl().LineIndex(nNewLine);
  286.         lEnd = lStart + pView->GetRichEditCtrl().LineLength(nNewLine);
  287.  
  288.         pView->GetRichEditCtrl().SetSel(lStart, lEnd);
  289.         if (bNextDifference)
  290.         {
  291.             m_wndStatusBar.SetWindowText(_T("Found next difference"));
  292.         }
  293.         else
  294.         {
  295.             m_wndStatusBar.SetWindowText(_T("Found next equal run"));
  296.         }
  297.     }
  298. }
  299.  
  300.  
  301. void CMainFrame::OnViewPreferences() 
  302. {
  303.     CPreferences Preferences;
  304.  
  305.  
  306.     //Initialize pages
  307.     //compare
  308.     Preferences.m_ComparePage.SetIgnoreCase(GetIgnoreCase());
  309.     Preferences.m_ComparePage.SetIgnoreWhiteSpace 
  310.                                     (GetIgnoreWhiteSpace());
  311.     Preferences.m_ComparePage.SetReadAheadOptLevel
  312.                                     (GetReadAheadOptLevel());
  313.  
  314.     //font page
  315.     // Get the left view as a data source
  316.     CDiffView* pViewLeft = GetView (0);
  317.     CHARFORMAT CharFmt;
  318.     memset(&CharFmt, 0, sizeof(CharFmt));
  319.     CharFmt.cbSize = sizeof(CharFmt);
  320.     pViewLeft->GetCharacterFormat(CharFmt);
  321.     Preferences.m_FontPage.SetCharacterFormat (CharFmt);
  322.  
  323.     if (Preferences.DoModal() == IDOK)
  324.     {
  325.  
  326.         //update Compare options
  327.         SetIgnoreCase(Preferences.m_ComparePage.GetIgnoreCase());
  328.         SetIgnoreWhiteSpace
  329.             (Preferences.m_ComparePage.GetIgnoreWhiteSpace());
  330.         SetReadAheadOptLevel
  331.             (Preferences.m_ComparePage.GetReadAheadOptLevel());
  332.  
  333.         //update Fonts
  334.         CDiffView* pViewRight = GetView (1);
  335.         Preferences.m_FontPage.GetCharacterFormat (CharFmt);
  336.         pViewLeft->SetCharacterFormat(CharFmt);
  337.         pViewRight->SetCharacterFormat(CharFmt);
  338.  
  339.         WriteRegistryPreferences();
  340.     }
  341. }
  342.  
  343. void CMainFrame::WriteRegistryPreferences()
  344. {
  345.     CWinApp* pApp = AfxGetApp();
  346.  
  347.     CString    Section;
  348.     Section.LoadString(IDS_REG_SECTION);
  349.  
  350.  
  351.     CString Entry;
  352.     Entry.LoadString(IDS_REG_IGNORECASE);
  353.     pApp->WriteProfileInt (Section, Entry, GetIgnoreCase());
  354.  
  355.     Entry.LoadString(IDS_REG_IGNOREWHITESPACE);
  356.     pApp->WriteProfileInt (Section, Entry, GetIgnoreWhiteSpace());
  357.  
  358.     Entry.LoadString(IDS_REG_READAHEAD);
  359.     pApp->WriteProfileInt (Section, Entry, GetReadAheadOptLevel());
  360.  
  361.     //you might look at CWinApp::WriteProfileBinary() and implement
  362.     //character preferences...
  363. }
  364.  
  365. void CMainFrame::ReadRegistryPreferences()
  366. {
  367.     CWinApp* pApp = AfxGetApp();
  368.  
  369.     CString    Section;
  370.     Section.LoadString(IDS_REG_SECTION);
  371.  
  372.  
  373.     CString Entry;
  374.     Entry.LoadString(IDS_REG_IGNORECASE);
  375.     SetIgnoreCase(pApp->GetProfileInt (Section, Entry,0));
  376.  
  377.     Entry.LoadString(IDS_REG_IGNOREWHITESPACE);
  378.     SetIgnoreWhiteSpace(pApp->GetProfileInt (Section, Entry, 0));
  379.  
  380.     Entry.LoadString(IDS_REG_READAHEAD);
  381.     SetReadAheadOptLevel(pApp->GetProfileInt (Section, Entry, 0));
  382.  
  383.     //you might look at CWinApp::GetProfileBinary() and implement
  384.     //character preferences...
  385. }
  386.  
  387.  
  388.  
  389.  
  390.