summaryrefslogtreecommitdiff
path: root/plugins/HistoryStats/src/dlgoption_subglobal.cpp
blob: f7dd52c6232b67af65f8b3b148a94bdb3b82de8f (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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
#include "stdafx.h"
#include "dlgoption.h"

#include "main.h"

/*
 * DlgOption::SubGlobal
 */

INT_PTR CALLBACK DlgOption::SubGlobal::staticInfoProc(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam)
{
	switch (msg) {
	case WM_INITDIALOG:
		TranslateDialogDefault(hDlg);
		{

			SendMessage(hDlg, WM_SETICON, ICON_BIG, reinterpret_cast<LPARAM>(LoadIcon(g_hInst, MAKEINTRESOURCE(IDI_HISTORYSTATS))));

			utils::centerDialog(hDlg);

			// fill with info
			const SupportInfo& info = *reinterpret_cast<const SupportInfo*>(lParam);

			SetDlgItemText(hDlg, IDC_PLUGIN, info.szPlugin);
			SetDlgItemText(hDlg, IDC_FEATURES, TranslateW(info.szTeaser));
			SetDlgItemText(hDlg, IDC_DESCRIPTION, TranslateW(info.szDescription));

			static const WORD LinkIDs[] = { IDC_LINK2, IDC_LINK1 };

			ext::string linkTexts = TranslateW(info.szLinkTexts);
			ext::string linkURLs = info.szLinkURLs;
			int nCurLink = 0;

			if (!linkTexts.empty()) {
				while (!linkTexts.empty() && nCurLink < _countof(LinkIDs)) {
					size_t posTexts = linkTexts.rfind('|');
					size_t posURLs = linkURLs.rfind('|');

					if (posTexts == ext::string::npos || posURLs == ext::string::npos) {
						posTexts = posURLs = -1;
					}

					ext::string linkLabel = linkURLs.substr(posURLs + 1);

					linkLabel += L" [";
					linkLabel += linkTexts.substr(posTexts + 1);
					linkLabel += L"]";

					SetDlgItemText(hDlg, LinkIDs[nCurLink], linkLabel.c_str());

					linkTexts.erase((posTexts != -1) ? posTexts : 0);
					linkURLs.erase((posURLs != -1) ? posURLs : 0);

					++nCurLink;
				}
			}

			int nHeightAdd = 0;

			while (nCurLink < _countof(LinkIDs)) {
				HWND hLink = GetDlgItem(hDlg, LinkIDs[nCurLink]);
				RECT rLink;

				GetWindowRect(hLink, &rLink);
				nHeightAdd += rLink.bottom - rLink.top;

				ShowWindow(hLink, SW_HIDE);
				EnableWindow(hLink, FALSE);

				++nCurLink;
			}

			if (nHeightAdd > 0) {
				RECT rDetails;

				GetWindowRect(GetDlgItem(hDlg, IDC_DESCRIPTION), &rDetails);
				SetWindowPos(GetDlgItem(hDlg, IDC_DESCRIPTION), nullptr, 0, 0, rDetails.right - rDetails.left, rDetails.bottom - rDetails.top + nHeightAdd, SWP_NOMOVE | SWP_NOZORDER);
			}
		}
		return TRUE;

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

		case IDCANCEL:
			EndDialog(hDlg, -1);
			return TRUE;

		case IDC_LINK1:
		case IDC_LINK2:
			if (HIWORD(wParam) == STN_CLICKED) {
				HWND hLink = reinterpret_cast<HWND>(lParam);
				int nLen = GetWindowTextLength(hLink);
				wchar_t* szTitle = new wchar_t[nLen + 1];

				if (GetWindowText(hLink, szTitle, nLen + 1)) {
					wchar_t* szEndOfURL = (wchar_t*)ext::strfunc::str(szTitle, L" [");
					if (szEndOfURL) {
						*szEndOfURL = '\0';
						g_pSettings->openURL(szTitle);
					}
				}

				delete[] szTitle;
			}
			return TRUE;
		}
		break;

	case WM_CTLCOLORSTATIC:
		HWND hStatic = reinterpret_cast<HWND>(lParam);
		wchar_t szClassName[64];

		if (GetClassName(hStatic, szClassName, _countof(szClassName)) && ext::strfunc::cmp(szClassName, WC_EDIT) == 0) {
			HDC hDC = reinterpret_cast<HDC>(wParam);

			SetBkColor(hDC, GetSysColor(COLOR_WINDOW));
			SetTextColor(hDC, GetSysColor(COLOR_WINDOWTEXT));

			return reinterpret_cast<INT_PTR>(GetSysColorBrush(COLOR_WINDOW));
		}
		break;
	}

	return FALSE;
}

DlgOption::SubGlobal::SubGlobal() :
	m_hOnStartup(nullptr),
	m_hShowMainMenu(nullptr),
	m_hShowMainMenuSub(nullptr),
	m_hShowContactMenu(nullptr),
	m_hShowContactMenuPseudo(nullptr),
	m_hGraphicsMode(nullptr),
	m_hGraphicsModePNG(nullptr),
	m_hPNGMode(nullptr),
	m_hThreadLowPriority(nullptr),
	m_hPathToBrowser(nullptr),
	m_bShowInfo(false),
	m_nInfoHeight(0)
{
}

DlgOption::SubGlobal::~SubGlobal()
{
	g_pSettings->setShowSupportInfo(m_bShowInfo);
}

BOOL DlgOption::SubGlobal::handleMsg(UINT msg, WPARAM wParam, LPARAM lParam)
{
	switch (msg) {
	case WM_WINDOWPOSCHANGED:
		rearrangeControls();
		return TRUE;

	case WM_COMMAND:
		if (LOWORD(wParam) == IDC_INFOLABEL && HIWORD(wParam) == STN_CLICKED) {
			m_bShowInfo = !m_bShowInfo;
			toggleInfo();
			return TRUE;
		}
		break;

	case WM_NOTIFY:
		NMHDR* p = reinterpret_cast<NMHDR*>(lParam);
		if (p->idFrom == IDC_INFO) {
			NMTREEVIEW* pNM = reinterpret_cast<NMTREEVIEW*>(lParam);

			if (p->code == NM_DBLCLK) {
				DWORD dwPoint = GetMessagePos();
				POINTS pts = MAKEPOINTS(dwPoint);
				TVHITTESTINFO tvhti = { { pts.x, pts.y } };

				ScreenToClient(pNM->hdr.hwndFrom, &tvhti.pt);

				if (TreeView_HitTest(pNM->hdr.hwndFrom, &tvhti) && tvhti.flags & TVHT_ONITEM) {
					TVITEM tvi;

					tvi.mask = TVIF_HANDLE | TVIF_PARAM;
					tvi.hItem = tvhti.hItem;

					if (TreeView_GetItem(pNM->hdr.hwndFrom, &tvi) && tvi.lParam) {
						onShowSupportInfo(*reinterpret_cast<SupportInfo*>(tvi.lParam));
						return TRUE;
					}
				}
			}
			else if (p->code == TVN_ITEMEXPANDING) {
				if (pNM->action == TVE_COLLAPSE || pNM->action == TVE_COLLAPSERESET ||
					 (pNM->action == TVE_TOGGLE && pNM->itemNew.state & TVIS_EXPANDED)) {
					SetWindowLongPtr(getHWnd(), DWLP_MSGRESULT, TRUE);
					return TRUE;
				}
			}
		}
		else if (p->idFrom == IDC_OPTIONS) {
			if (p->code == OptionsCtrl::OCN_MODIFIED) {
				getParent()->settingsChanged();
				getParent()->updateProblemInfo();
				return TRUE;
			}
		}
		break;
	}

	return FALSE;
}

void DlgOption::SubGlobal::onWMInitDialog()
{
	TranslateDialogDefault(getHWnd());

	// init options control
	m_Options << GetDlgItem(getHWnd(), IDC_OPTIONS);

	// settings
	OptionsCtrl::Item hTemp;

	hTemp = m_Options.insertGroup(nullptr, TranslateT("Integration"), OptionsCtrl::OCF_ROOTGROUP);
	m_hOnStartup = m_Options.insertCheck(hTemp, TranslateT("Create statistics on Miranda NG startup"));
	m_hShowMainMenu = m_Options.insertCheck(hTemp, TranslateT("Add menu items to main menu"), OptionsCtrl::OCF_DISABLECHILDSONUNCHECK);
	m_hShowMainMenuSub = m_Options.insertCheck(m_hShowMainMenu, TranslateT("Put menu items into submenu"));
	m_hShowContactMenu = m_Options.insertCheck(hTemp, TranslateT("Add menu items to contact menu"), OptionsCtrl::OCF_DISABLECHILDSONUNCHECK);
	m_hShowContactMenuPseudo = m_Options.insertCheck(m_hShowContactMenu, TranslateT("Don't hide menu items for pseudo protocols"));
	m_hProtocols = m_Options.insertGroup(m_hShowContactMenu, TranslateT("Hide menu items for protocol..."));
	hTemp = m_Options.insertGroup(nullptr, TranslateT("Graphics"), OptionsCtrl::OCF_ROOTGROUP | OptionsCtrl::OCF_NODISABLECHILDS);
	m_hGraphicsMode = m_Options.insertRadio(hTemp, nullptr, TranslateT("Only use HTML to simulate graphics"));
	m_hGraphicsModePNG = m_Options.insertRadio(hTemp, m_hGraphicsMode, TranslateT("Generate PNG files to represent graphics"), OptionsCtrl::OCF_DISABLECHILDSONUNCHECK);
	m_hPNGMode = m_Options.insertRadio(m_hGraphicsModePNG, nullptr, TranslateT("Fall back to HTML output if column options require HTML output"));
	m_Options.insertRadio(m_hGraphicsModePNG, m_hPNGMode, TranslateT("Enforce PNG output, possibly ignoring some column options"));
	m_Options.insertRadio(m_hGraphicsModePNG, m_hPNGMode, TranslateT("Prefer HTML output over PNG output if available"));
	hTemp = m_Options.insertGroup(nullptr, TranslateT("Miscellaneous"), OptionsCtrl::OCF_ROOTGROUP);
	m_hThreadLowPriority = m_Options.insertCheck(hTemp, TranslateT("Generate statistics in background thread with low priority"));
	m_hPathToBrowser = m_Options.insertEdit(hTemp, TranslateT("Path to browser (leave blank for system default)"));

	// insert known protocols
	m_hHideContactMenuProtos.clear();

	PROTOACCOUNT **protoList;
	int protoCount;
	Proto_EnumAccounts(&protoCount, &protoList);
	upto_each_(i, protoCount)
	{
		m_hHideContactMenuProtos.push_back(m_Options.insertCheck(
			m_hProtocols,
			protoList[i]->tszAccountName,
			0,
			reinterpret_cast<INT_PTR>(protoList[i]->szModuleName)));
	}

	m_Options.ensureVisible(nullptr);

	// init support info list
	initSupportInfo();

	m_Options.checkItem(m_hOnStartup, true);

	m_bShowInfo = g_pSettings->getShowSupportInfo();
	toggleInfo();
}

void DlgOption::SubGlobal::loadSettings()
{
	// read settings from local settings store
	Settings& localS = getParent()->getLocalSettings();

	m_Options.checkItem(m_hOnStartup, localS.m_OnStartup);
	m_Options.checkItem(m_hShowMainMenu, localS.m_ShowMainMenu);
	m_Options.checkItem(m_hShowMainMenuSub, localS.m_ShowMainMenuSub);
	m_Options.checkItem(m_hShowContactMenu, localS.m_ShowContactMenu);
	m_Options.checkItem(m_hShowContactMenuPseudo, localS.m_ShowContactMenuPseudo);
	m_Options.setRadioChecked(m_hGraphicsMode, localS.m_GraphicsMode);
	m_Options.setRadioChecked(m_hPNGMode, localS.m_PNGMode);
	m_Options.checkItem(m_hThreadLowPriority, localS.m_ThreadLowPriority);
	m_Options.setEditString(m_hPathToBrowser, localS.m_PathToBrowser.c_str());

	// 'set check' on hidden contact menu items protocols
	citer_each_(std::vector<OptionsCtrl::Check>, i, m_hHideContactMenuProtos)
	{
		ext::a::string protoName = reinterpret_cast<const char*>(m_Options.getItemData(*i));

		m_Options.checkItem(*i, localS.m_HideContactMenuProtos.find(protoName) != localS.m_HideContactMenuProtos.end());
	}
}

void DlgOption::SubGlobal::saveSettings()
{
	Settings& localS = getParent()->getLocalSettings();

	localS.m_OnStartup = m_Options.isItemChecked(m_hOnStartup);
	localS.m_ShowMainMenu = m_Options.isItemChecked(m_hShowMainMenu);
	localS.m_ShowMainMenuSub = m_Options.isItemChecked(m_hShowMainMenuSub);
	localS.m_ShowContactMenu = m_Options.isItemChecked(m_hShowContactMenu);
	localS.m_ShowContactMenuPseudo = m_Options.isItemChecked(m_hShowContactMenuPseudo);
	localS.m_GraphicsMode = m_Options.getRadioChecked(m_hGraphicsMode);
	localS.m_PNGMode = m_Options.getRadioChecked(m_hPNGMode);
	localS.m_ThreadLowPriority = m_Options.isItemChecked(m_hThreadLowPriority);
	localS.m_PathToBrowser = m_Options.getEditString(m_hPathToBrowser);

	localS.m_HideContactMenuProtos.clear();
	vector_each_(i, m_hHideContactMenuProtos)
	{
		if (m_Options.isItemChecked(m_hHideContactMenuProtos[i]))
			localS.m_HideContactMenuProtos.insert(reinterpret_cast<char*>(m_Options.getItemData(m_hHideContactMenuProtos[i])));
	}
}

void DlgOption::SubGlobal::initSupportInfo()
{
	static const SupportInfo Infos[] = {
		{
			TranslateT("At this time there is no external plugin supported."),
			LPGENW(""),
			LPGENW(""),
			LPGENW(""),
			L""
		},
	};

	HWND hInfo = GetDlgItem(getHWnd(), IDC_INFO);

	SetWindowLongPtr(hInfo, GWL_STYLE, GetWindowLongPtr(hInfo, GWL_STYLE) | TVS_NOHSCROLL);

	SendMessage(hInfo, WM_SETREDRAW, FALSE, 0);

	// fill with data
	TVINSERTSTRUCT tvi;
	tvi.hParent = TVI_ROOT;
	tvi.hInsertAfter = TVI_LAST;
	tvi.item.mask = TVIF_TEXT | TVIF_PARAM | TVIF_STATE;
	tvi.item.lParam = 0;
	tvi.item.state = TVIS_EXPANDED | TVIS_BOLD;
	tvi.item.stateMask = TVIS_EXPANDED | TVIS_BOLD;

	tvi.item.pszText = const_cast<wchar_t*>(TranslateT("Supported plugins (double-click to learn more):"));
	tvi.hParent = TreeView_InsertItem(hInfo, &tvi);

	tvi.item.stateMask &= ~TVIS_BOLD;

	array_each_(i, Infos)
	{
		tvi.item.pszText = const_cast<wchar_t*>(Infos[i].szPlugin);
		tvi.item.lParam = reinterpret_cast<LPARAM>(&Infos[i]);
		TreeView_InsertItem(hInfo, &tvi);
	}

	SendMessage(hInfo, WM_SETREDRAW, TRUE, 0);
}

void DlgOption::SubGlobal::rearrangeControls()
{
	RECT rClient, rWnd;
	int offsetY;

	if (m_nInfoHeight == 0) {
		m_nInfoHeight = utils::getWindowRect(getHWnd(), IDC_INFO).bottom;
		m_nInfoHeight -= utils::getWindowRect(getHWnd(), IDC_INFOLABEL).bottom;
	}

	GetClientRect(getHWnd(), &rClient);

	// support info list
	rWnd = utils::getWindowRect(getHWnd(), IDC_INFO);
	offsetY = rClient.bottom + (m_bShowInfo ? 0 : m_nInfoHeight) - rWnd.bottom;
	OffsetRect(&rWnd, 0, offsetY);
	rWnd.right = rClient.right;
	utils::moveWindow(getHWnd(), IDC_INFO, rWnd);

	// support info list label
	rWnd = utils::getWindowRect(getHWnd(), IDC_INFOLABEL);
	OffsetRect(&rWnd, 0, offsetY);
	rWnd.right = rClient.right;
	utils::moveWindow(getHWnd(), IDC_INFOLABEL, rWnd);

	// options tree
	rWnd = utils::getWindowRect(getHWnd(), m_Options);
	rWnd.right = rClient.right;
	rWnd.bottom += offsetY;
	utils::moveWindow(m_Options, rWnd);
}

void DlgOption::SubGlobal::toggleInfo()
{
	HWND hInfo = GetDlgItem(getHWnd(), IDC_INFO);
	const wchar_t* szInfoLabelText = m_bShowInfo ? LPGENW("HistoryStats supports several plugins. Click to hide info...") : LPGENW("HistoryStats supports several plugins. Click to learn more...");

	SetDlgItemText(getHWnd(), IDC_INFOLABEL, TranslateW(szInfoLabelText));
	ShowWindow(hInfo, m_bShowInfo ? SW_SHOW : SW_HIDE);
	EnableWindow(hInfo, BOOL_(m_bShowInfo));

	rearrangeControls();
}

void DlgOption::SubGlobal::onShowSupportInfo(const SupportInfo& info)
{
	DialogBoxParam(g_hInst, MAKEINTRESOURCE(IDD_SUPPORTINFO), getHWnd(), staticInfoProc, reinterpret_cast<LPARAM>(&info));
}