summaryrefslogtreecommitdiff
path: root/plugins/HistoryStats/src/dlgfilterwords.cpp
blob: b48650fa79237b76c36c084b1bd25ce4ee3b9d6e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
#include "stdafx.h"
#include "dlgfilterwords.h"

#include "main.h"
#include "resource.h"

#include <algorithm>

INT_PTR CALLBACK DlgFilterWords::staticDlgProc(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam)
{
	DlgFilterWords* pDlg = reinterpret_cast<DlgFilterWords*>(GetWindowLongPtr(hDlg, DWLP_USER));

	switch (msg) {
	case WM_INITDIALOG:
		pDlg = reinterpret_cast<DlgFilterWords*>(lParam);
		SetWindowLongPtr(hDlg, DWLP_USER, reinterpret_cast<LONG_PTR>(pDlg));
		pDlg->m_hWnd = hDlg;
		pDlg->onWMInitDialog();
		return TRUE;

	case WM_DESTROY:
		pDlg->onWMDestroy();
		pDlg->m_hWnd = nullptr;
		SetWindowLongPtr(hDlg, DWLP_USER, 0);
		return TRUE;

	case WM_COMMAND:
		switch (LOWORD(wParam)) {
		case IDOK:
			EndDialog(hDlg, 1);
			return TRUE;

		case IDCANCEL:
			EndDialog(hDlg, 0);
			return TRUE;
		}
		break;

	case WM_NOTIFY:
		switch (reinterpret_cast<NMHDR*>(lParam)->idFrom) {
		case IDC_BAND:
		{
			BandCtrl::NMBANDCTRL* pNM = reinterpret_cast<BandCtrl::NMBANDCTRL*>(lParam);

			if (pNM->hdr.code == BandCtrl::BCN_CLICKED)
				pDlg->onBandClicked(pNM->hButton, pNM->dwData);
		}
		break;

		case IDC_SETS:
			OptionsCtrl::NMOPTIONSCTRL * pNM = reinterpret_cast<OptionsCtrl::NMOPTIONSCTRL*>(lParam);

			if (pNM->hdr.code == OptionsCtrl::OCN_MODIFIED)
				pDlg->onSetItemModified(pNM->hItem, pNM->dwData);
			else if (pNM->hdr.code == OptionsCtrl::OCN_SELCHANGED)
				pDlg->onSetSelChanged(pNM->hItem, pNM->dwData);
			else if (pNM->hdr.code == OptionsCtrl::OCN_SELCHANGING)
				pDlg->onSetSelChanging(pNM->hItem, pNM->dwData);
			break;
		}
		break;
	}

	return FALSE;
}

void DlgFilterWords::onWMInitDialog()
{
	TranslateDialogDefault(m_hWnd);

	SendMessage(m_hWnd, WM_SETICON, ICON_BIG, reinterpret_cast<LPARAM>(LoadIcon(g_plugin.getInst(), MAKEINTRESOURCE(IDI_HISTORYSTATS))));

	utils::centerDialog(m_hWnd);

	// init band control
	m_Band << GetDlgItem(m_hWnd, IDC_BAND);

	static const struct
	{
		uint16_t iconId;
		wchar_t* szTooltip;
		bool bDisabled;
	} columnBand[] = {
		{ IDI_COL_ADD, LPGENW("Add set"), false },
		{ IDI_COL_DEL, LPGENW("Delete set"), true },
	};

	array_each_(i, columnBand)
	{
		HICON hIcon = reinterpret_cast<HICON>(LoadImage(g_plugin.getInst(), MAKEINTRESOURCE(columnBand[i].iconId), IMAGE_ICON, OS::smIconCX(), OS::smIconCY(), 0));
		uint32_t dwFlags = (columnBand[i].bDisabled ? BandCtrl::BCF_DISABLED : 0);

		m_hActionButtons[i] = m_Band.addButton(dwFlags, hIcon, i, TranslateW(columnBand[i].szTooltip));

		DestroyIcon(hIcon);
	}

	// init options control (sets)
	m_Sets << GetDlgItem(m_hWnd, IDC_SETS);

	citer_each_(FilterVec, i, m_Filters)
	{
		const Filter* pFilter = *i;

		HANDLE hCheck = m_Sets.insertCheck(nullptr, pFilter->getName().c_str(), 0, reinterpret_cast<INT_PTR>(pFilter));

		if (m_bColProvided && m_ColFilters.find(pFilter->getID()) != m_ColFilters.end())
			m_Sets.checkItem(hCheck, true);
	}

	// init other controls
	static const wchar_t* szWordFilterModes[] = {
		LPGENW("Filter words matching"),
		LPGENW("Filter words containing"),
		LPGENW("Filter words starting with"),
		LPGENW("Filter words ending with"),
		LPGENW("Filter messages matching"),
		LPGENW("Filter messages containing"),
		LPGENW("Filter messages starting with"),
		LPGENW("Filter messages ending with"),
	};

	array_each_(i, szWordFilterModes)
	{
		SendDlgItemMessage(m_hWnd, IDC_MODE, CB_ADDSTRING, 0, reinterpret_cast<LPARAM>(TranslateW(szWordFilterModes[i])));
	}

	SendDlgItemMessage(m_hWnd, IDC_MODE, CB_SETCURSEL, 0, 0);

	onSetSelChanged(nullptr, 0);
}

void DlgFilterWords::onWMDestroy()
{
	// avoid OCN_SELCHANGING messages when dialog object already destroyed
	m_Sets.deleteAllItems();
}

void DlgFilterWords::onBandClicked(HANDLE, INT_PTR dwData)
{
	switch (dwData) {
	case saAdd:
		onSetAdd();
		break;

	case saDel:
		onSetDel();
		break;
	}
}

void DlgFilterWords::onSetAdd()
{
	Filter* pFilter = new Filter(utils::getGUID());

	m_Filters.push_back(pFilter);

	HANDLE hAdded = m_Sets.insertCheck(nullptr, pFilter->getName().c_str(), 0, reinterpret_cast<UINT_PTR>(pFilter));

	m_Sets.selectItem(hAdded);
}

void DlgFilterWords::onSetDel()
{
	HANDLE hSel = m_Sets.getSelection();

	if (hSel) {
		Filter* pFilter = reinterpret_cast<Filter*>(m_Sets.getItemData(hSel));

		if (pFilter->getRef() > 0) {
			if (IDYES != MessageBox(m_hWnd,
				TranslateT("The selected set is in use by at least one other column. If you remove it, it won't be available to all other columns that use it. Are you sure you want to remove the set?"),
				TranslateT("HistoryStats - Warning"), MB_ICONWARNING | MB_YESNO)) {
				return;
			}
		}

		FilterVec::iterator i = std::find(m_Filters.begin(), m_Filters.end(), pFilter);
		if (i != m_Filters.end()) {
			m_Sets.deleteItem(hSel);
			m_Filters.erase(i);

			delete pFilter;
		}
	}
}

void DlgFilterWords::onSetItemModified(HANDLE hItem, INT_PTR dwData)
{
	if (m_bColProvided) {
		Filter* pFilter = reinterpret_cast<Filter*>(dwData);

		if (m_Sets.isItemChecked(hItem))
			m_ColFilters.insert(pFilter->getID());
		else
			m_ColFilters.erase(pFilter->getID());
	}
	else {
		if (m_Sets.isItemChecked(hItem))
			m_Sets.checkItem(hItem, false);
	}
}

void DlgFilterWords::onSetSelChanging(HANDLE hItem, INT_PTR dwData)
{
	if (hItem) {
		Filter* pFilter = reinterpret_cast<Filter*>(dwData);

		// set name
		HWND hText = GetDlgItem(m_hWnd, IDC_SETNAME);
		int nLen = GetWindowTextLength(hText);
		wchar_t* szText = new wchar_t[nLen + 1];

		if (GetWindowText(hText, szText, nLen + 1)) {
			pFilter->setName(szText);
			m_Sets.setItemLabel(hItem, szText);
		}

		delete[] szText;

		// words
		hText = GetDlgItem(m_hWnd, IDC_WORDS);
		nLen = GetWindowTextLength(hText);
		szText = new wchar_t[nLen + 1];

		if (GetWindowText(hText, szText, nLen + 1)) {
			ext::string strText = szText;

			utils::replaceAllInPlace(strText, L"\r", L"");
			pFilter->clearWords();

			size_t nPos = strText.find('\n');

			while (nPos != ext::string::npos) {
				if (nPos > 0)
					pFilter->addWord(utils::toLowerCase(strText.substr(0, nPos)));

				strText.erase(0, nPos + 1);

				nPos = strText.find('\n');
			}

			if (!strText.empty())
				pFilter->addWord(utils::toLowerCase(strText));
		}

		delete[] szText;

		// mode
		pFilter->setMode(SendDlgItemMessage(m_hWnd, IDC_MODE, CB_GETCURSEL, 0, 0));
	}
}

void DlgFilterWords::onSetSelChanged(HANDLE hItem, INT_PTR dwData)
{
	BOOL bEnable = hItem ? TRUE : FALSE;

	EnableWindow(GetDlgItem(m_hWnd, IDC_SETNAME), bEnable);
	EnableWindow(GetDlgItem(m_hWnd, IDC_MODE), bEnable);
	EnableWindow(GetDlgItem(m_hWnd, IDC_WORDS), bEnable);

	if (hItem) {
		Filter* pFilter = reinterpret_cast<Filter*>(dwData);

		SetDlgItemText(m_hWnd, IDC_SETNAME, pFilter->getName().c_str());
		SendDlgItemMessage(m_hWnd, IDC_MODE, CB_SETCURSEL, pFilter->getMode(), 0);

		ext::string strWords;

		citer_each_(WordSet, i, pFilter->getWords())
		{
			strWords += *i;
			strWords += L"\r\n";
		}

		if (!strWords.empty())
			strWords.erase(strWords.length() - 2, 2);

		SetDlgItemText(m_hWnd, IDC_WORDS, strWords.c_str());
	}
	else {
		SetDlgItemText(m_hWnd, IDC_SETNAME, L"");
		SetDlgItemText(m_hWnd, IDC_WORDS, L"");
	}

	// (de)activate band controls
	m_Band.enableButton(m_hActionButtons[saDel], bool_(hItem));
}

void DlgFilterWords::clearFilters()
{
	citer_each_(FilterVec, i, m_Filters)
	{
		delete *i;
	}

	m_Filters.clear();
}

DlgFilterWords::DlgFilterWords() : m_hWnd(nullptr), m_bColProvided(false)
{
}

DlgFilterWords::~DlgFilterWords()
{
	clearFilters();
}

bool DlgFilterWords::showModal(HWND hParent)
{
	return (DialogBoxParam(g_plugin.getInst(), MAKEINTRESOURCE(IDD_FILTERWORDS), hParent, staticDlgProc, reinterpret_cast<LPARAM>(this)) == 1);
}

void DlgFilterWords::setFilters(const FilterSet& Filters)
{
	clearFilters();

	citer_each_(Settings::FilterSet, i, Filters)
	{
		m_Filters.push_back(new Filter(*i));
	}

	FilterCompare cmp;

	std::sort(m_Filters.begin(), m_Filters.end(), cmp);
}

void DlgFilterWords::setColFilters(const ColFilterSet& ColFilters)
{
	m_bColProvided = true;
	m_ColFilters = ColFilters;
}

void DlgFilterWords::updateFilters(FilterSet& Filters)
{
	Filters.clear();

	citer_each_(FilterVec, i, m_Filters)
	{
		Filters.insert(**i);
	}
}