2 * @file SubstitutionFiltersDlg.cpp
4 * @brief Implementation of Substitution Filters dialog
8 #include "SubstitutionFiltersList.h"
10 #include "SubstitutionFiltersDlg.h"
11 #include <Poco/Exception.h>
17 /** @brief Location for file compare specific help to open. */
18 static TCHAR FilterHelpLocation[] = _T("::/htmlhelp/Filters.html");
20 /////////////////////////////////////////////////////////////////////////////
21 // CPropLineFilter property page
23 IMPLEMENT_DYNAMIC(SubstitutionFiltersDlg, CTrPropertyPage)
28 SubstitutionFiltersDlg::SubstitutionFiltersDlg()
29 : CTrPropertyPage(SubstitutionFiltersDlg::IDD)
30 , m_pSubstitutionFiltersList(nullptr)
32 //{{AFX_DATA_INIT(SubstitutionFiltersFiltersDlg)
35 m_strCaption = theApp.LoadDialogCaption(m_lpszTemplateName).c_str();
36 m_psp.pszTitle = m_strCaption;
37 m_psp.dwFlags |= PSP_USETITLE;
38 m_psp.hIcon = AfxGetApp()->LoadIcon(IDI_LINEFILTER);
39 m_psp.dwFlags |= PSP_USEHICON;
42 void SubstitutionFiltersDlg::DoDataExchange(CDataExchange* pDX)
44 CPropertyPage::DoDataExchange(pDX);
45 //{{AFX_DATA_MAP(SubstitutionFiltersFiltersDlg)
46 DDX_Check(pDX, IDC_IGNORED_SUSBSTITUTIONS_ENABLED, m_bEnabled);
47 DDX_Control(pDX, IDC_SUBSTITUTION_FILTERS, m_listFilters);
51 BEGIN_MESSAGE_MAP(SubstitutionFiltersDlg, CTrPropertyPage)
52 //{{AFX_MSG_MAP(SubstitutionFiltersFiltersDlg)
53 ON_COMMAND(ID_HELP, OnHelp)
54 ON_BN_CLICKED(IDC_LFILTER_ADDBTN, OnBnClickedAddBtn)
55 ON_BN_CLICKED(IDC_LFILTER_CLEARBTN, OnBnClickedClearBtn)
56 ON_BN_CLICKED(IDC_LFILTER_REMOVEBTN, OnBnClickedRemovebtn)
61 /////////////////////////////////////////////////////////////////////////////
62 // CPropLineFilter message handlers
65 * @brief Initialize the dialog.
67 BOOL SubstitutionFiltersDlg::OnInitDialog()
69 m_bEnabled = m_pSubstitutionFiltersList->GetEnabled();
70 CTrPropertyPage::OnInitDialog();
74 return TRUE; // return TRUE unless you set the focus to a control
75 // EXCEPTION: OCX Property Pages should return FALSE
78 static CString RemoveMnemonic(String text)
80 Poco::RegularExpression re("\\(&.\\)|&|:");
81 std::string textu8 = ucr::toUTF8(text);
82 re.subst(textu8, "", Poco::RegularExpression::RE_GLOBAL);
83 return ucr::toTString(textu8).c_str();
86 void SubstitutionFiltersDlg::InitList()
88 m_listFilters.SetExtendedStyle(LVS_EX_CHECKBOXES | LVS_EX_FULLROWSELECT | LVS_EX_INFOTIP);
90 const int lpx = CClientDC(this).GetDeviceCaps(LOGPIXELSX);
91 auto pointToPixel = [lpx](int point) { return MulDiv(point, lpx, 72); };
93 m_listFilters.InsertColumn(0, RemoveMnemonic(_("Fi&nd what:")), LVCFMT_LEFT, pointToPixel(150));
94 m_listFilters.InsertColumn(1, RemoveMnemonic(_("Re&place with:")), LVCFMT_LEFT, pointToPixel(150));
95 m_listFilters.InsertColumn(2, RemoveMnemonic(_("Regular &expression")), LVCFMT_LEFT, pointToPixel(120));
96 m_listFilters.InsertColumn(3, RemoveMnemonic(_("Match &case")), LVCFMT_LEFT, pointToPixel(120));
97 m_listFilters.InsertColumn(4, RemoveMnemonic(_("Match &whole word only")), LVCFMT_LEFT, pointToPixel(120));
98 m_listFilters.SetBooleanValueColumn(2);
99 m_listFilters.SetBooleanValueColumn(3);
100 m_listFilters.SetBooleanValueColumn(4);
102 if (m_pSubstitutionFiltersList)
104 for (int i = 0; i < (int)m_pSubstitutionFiltersList->GetCount(); i++)
106 const SubstitutionFilter& item = m_pSubstitutionFiltersList->GetAt(i);
107 m_listFilters.InsertItem(i, item.pattern.c_str());
108 m_listFilters.SetItemText(i, 1, item.replacement.c_str());
109 m_listFilters.SetItemBooleanValue(i, 2, item.useRegExp);
110 m_listFilters.SetItemBooleanValue(i, 3, item.caseSensitive);
111 m_listFilters.SetItemBooleanValue(i, 4, item.matchWholeWordOnly);
112 m_listFilters.SetCheck(i, item.enabled);
118 * @brief Open help from mainframe when user presses F1.
120 void SubstitutionFiltersDlg::OnHelp()
122 theApp.ShowHelp(FilterHelpLocation);
126 * @brief Called when Add-button is clicked.
128 void SubstitutionFiltersDlg::OnBnClickedAddBtn()
130 int num = m_listFilters.GetItemCount();
131 int ind = m_listFilters.InsertItem(num, _("<Edit here>").c_str());
132 m_listFilters.SetItemText(num, 1, _("<Edit here>").c_str());
133 m_listFilters.SetItemBooleanValue(num, 2, false);
134 m_listFilters.SetItemBooleanValue(num, 3, false);
135 m_listFilters.SetItemBooleanValue(num, 4, false);
136 m_listFilters.SetCheck(num);
140 m_listFilters.SetItemState(ind, LVIS_SELECTED, LVIS_SELECTED);
141 m_listFilters.EnsureVisible(ind, FALSE);
146 * @brief Called when Clear-button is clicked.
148 void SubstitutionFiltersDlg::OnBnClickedClearBtn()
150 m_listFilters.DeleteAllItems();
154 * @brief Save filters to list when exiting the dialog.
156 BOOL SubstitutionFiltersDlg::OnApply()
158 m_pSubstitutionFiltersList->Empty();
160 for (int i = 0; i < m_listFilters.GetItemCount(); i++)
162 String symbolBeforeRename = m_listFilters.GetItemText(i, 0);
163 String symbolAfterRename = m_listFilters.GetItemText(i, 1);
164 bool useRegExp = m_listFilters.GetItemBooleanValue(i, 2);
165 bool caseSensitive = m_listFilters.GetItemBooleanValue(i, 3);
166 bool matchWholeWordOnly = m_listFilters.GetItemBooleanValue(i, 4);
168 matchWholeWordOnly = false;
169 bool enabled = !!m_listFilters.GetCheck(i);
170 m_pSubstitutionFiltersList->Add(symbolBeforeRename, symbolAfterRename,
171 useRegExp, caseSensitive, matchWholeWordOnly, enabled);
177 m_pSubstitutionFiltersList->MakeSubstitutionList(true);
179 catch (Poco::RegularExpressionException& e)
181 AfxMessageBox(ucr::toTString(e.message()).c_str(), MB_OK | MB_ICONEXCLAMATION);
185 m_pSubstitutionFiltersList->SetEnabled(m_bEnabled);
186 AfxGetApp()->WriteProfileInt(_T("Settings"), _T("FilterStartPage"), GetParentSheet()->GetActiveIndex());
191 * @brief Sets external filter list.
192 * @param [in] list External filter list.
194 void SubstitutionFiltersDlg::SetList(SubstitutionFiltersList *list)
196 m_pSubstitutionFiltersList = list;
200 * @brief Called when Remove button is clicked.
202 void SubstitutionFiltersDlg::OnBnClickedRemovebtn()
204 int sel = m_listFilters.GetNextItem(-1, LVNI_SELECTED);
207 m_listFilters.DeleteItem(sel);
210 int newSel = min(m_listFilters.GetItemCount() - 1, sel);
213 m_listFilters.SetItemState(newSel, LVIS_SELECTED, LVIS_SELECTED);
214 bool bPartialOk = false;
215 m_listFilters.EnsureVisible(newSel, bPartialOk);