OSDN Git Service

Merge with stable
[winmerge-jp/winmerge-jp.git] / Src / TestFilterDlg.cpp
1 /** 
2  * @file  TestFilterDlg.cpp
3  *
4  * @brief Dialog for testing file filters
5  */
6 // ID line follows -- this is updated by SVN
7 // $Id$
8
9 #include "stdafx.h"
10 #include "TestFilterDlg.h"
11 #include "Merge.h"
12 #include "resource.h"
13 #include "UnicodeString.h"
14 #include "FileFilterMgr.h"
15 #include "DDXHelper.h"
16
17 #ifdef _DEBUG
18 #define new DEBUG_NEW
19 #undef THIS_FILE
20 static char THIS_FILE[] = __FILE__;
21 #endif
22
23 /**
24  * @brief Constructor.
25  * @param [in] pParent Parent window.
26  * @param [in] pFileFilter File filter to test.
27  * @param [in] pFilterMgr File filter manager.
28  */
29 CTestFilterDlg::CTestFilterDlg(CWnd* pParent, FileFilter * pFileFilter, FileFilterMgr *pFilterMgr)
30 : CDialog(CTestFilterDlg::IDD, pParent)
31 , m_pFileFilter(pFileFilter)
32 , m_pFileFilterMgr(pFilterMgr)
33 {
34 }
35
36 void CTestFilterDlg::DoDataExchange(CDataExchange* pDX)
37 {
38         CDialog::DoDataExchange(pDX);
39         //{{AFX_DATA_MAP(CTestFilterDlg)
40                 // NOTE: the ClassWizard will add DDX and DDV calls here
41         //}}AFX_DATA_MAP
42 }
43
44
45 BEGIN_MESSAGE_MAP(CTestFilterDlg, CDialog)
46         //{{AFX_MSG_MAP(CTestFilterDlg)
47         ON_BN_CLICKED(IDC_TEST_BTN, OnTestBtn)
48         //}}AFX_MSG_MAP
49 END_MESSAGE_MAP()
50
51 /////////////////////////////////////////////////////////////////////////////
52 // CTestFilterDlg message handlers
53
54 /**
55  * @brief Initialize the dialog.
56  * @return FALSE always.
57  */
58 BOOL CTestFilterDlg::OnInitDialog()
59 {
60         theApp.TranslateDialog(m_hWnd);
61         CDialog::OnInitDialog();
62
63         GetDlgItem(IDC_TEST_TEXT)->SetFocus();
64
65         String name = m_pFileFilterMgr->GetFilterName(m_pFileFilter);
66         SetDlgItemText(IDC_HEADER_FILTER_NAME, name.c_str());
67         
68         return FALSE;  // return TRUE unless you set the focus to a control
69                       // EXCEPTION: OCX Property Pages should return FALSE
70 }
71
72 /** @brief User pressed Text button. */
73 void CTestFilterDlg::OnTestBtn() 
74 {
75         String text;
76         GetDlgItemText(IDC_TEST_TEXT, PopString(text));
77
78         bool passed = CheckText(text);
79
80         String result = (passed ? _T("passed") : _T("failed"));
81         text += _T(": ") + result;
82
83         AppendResult(text);
84 }
85
86 /** @brief user pressed <enter> key. */
87 void CTestFilterDlg::OnOK()
88 {
89    CWnd *pWnd = GetFocus(); 
90    ASSERT (pWnd); 
91    if (IDCANCEL == pWnd->GetDlgCtrlID()) 
92    { 
93        CDialog::OnCancel(); 
94    }
95    else
96    {
97            OnTestBtn();
98    }
99 }
100
101 /**
102  * @brief Test text against filter.
103  * @param [in] text Text to test.
104  * @return true if text passes the filter, FALSE otherwise.
105  */
106 bool CTestFilterDlg::CheckText(String text) const
107 {
108         CButton * IsDirButton = (CButton *)GetDlgItem(IDC_IS_DIRECTORY);
109         bool isDir = (IsDirButton->GetCheck() == BST_CHECKED);
110         if (isDir)
111         {
112                 // Convert any forward slashes to canonical Windows-style backslashes
113                 string_replace(text, _T("/"), _T("\\"));
114                 return m_pFileFilterMgr->TestDirNameAgainstFilter(m_pFileFilter, text);
115         }
116         else
117         {
118                 return m_pFileFilterMgr->TestFileNameAgainstFilter(m_pFileFilter, text);
119         }
120 }
121
122 /**
123  * @brief Add text to end of edit control.
124  * @param [in] edit Edit contror into which the text is added.
125  * @param [in] text Text to add to edit control.
126  */
127 void AppendToEditBox(CEdit & edit, const String& text)
128 {
129         int len = edit.GetWindowTextLength();
130         edit.SetSel(len, len);
131         edit.ReplaceSel(text.c_str());
132 }
133
134 /**
135  * @brief Add new result to end of result edit box.
136  * @param [in] result Result text to add.
137  */
138 void CTestFilterDlg::AppendResult(String result)
139 {
140         CEdit * edit = (CEdit *)GetDlgItem(IDC_RESULTS);
141         if (edit->GetWindowTextLength()>0)
142                 result = _T("\r\n") + result;
143         AppendToEditBox(*edit, result);
144 }