summaryrefslogtreecommitdiff
path: root/plugins/wbOSD/src/options.cpp
blob: 472411de62783bcd34ee15430ed24038def4ce76 (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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
/*
Wannabe OSD
This plugin tries to become miranda's standard OSD ;-)

(C) 2005 Andrej Krutak

Distributed under GNU's GPL 2 or later
*/

#include "stdafx.h"

COLORREF pencustcolors[16];

const static osdmsg defstr = { L"", 0, RGB(0, 0, 0), nullptr, 0 };

void FillCheckBoxTree(HWND hwndTree, DWORD style)
{
	logmsg("FillCheckBoxTree");

	TVINSERTSTRUCT tvis = {};
	tvis.hParent = nullptr;
	tvis.hInsertAfter = TVI_LAST;
	tvis.item.mask = TVIF_PARAM | TVIF_TEXT | TVIF_STATE;
	for (uint16_t status = ID_STATUS_OFFLINE; status <= ID_STATUS_MAX; status++) {
		tvis.item.lParam = status - ID_STATUS_OFFLINE;
		tvis.item.pszText = Clist_GetStatusModeDescription(status, 0);
		tvis.item.stateMask = TVIS_STATEIMAGEMASK;
		tvis.item.state = INDEXTOSTATEIMAGEMASK((style & (1 << tvis.item.lParam)) != 0 ? 2 : 1);
		TreeView_InsertItem(hwndTree, &tvis);
	}
}

DWORD MakeCheckBoxTreeFlags(HWND hwndTree)
{
	DWORD flags = 0;

	logmsg("MakeCheckBoxTreeFlags");

	TVITEM tvi = { 0 };
	tvi.mask = TVIF_HANDLE | TVIF_PARAM | TVIF_STATE;
	tvi.hItem = TreeView_GetRoot(hwndTree);
	while (tvi.hItem) {
		TreeView_GetItem(hwndTree, &tvi);
		if (((tvi.state&TVIS_STATEIMAGEMASK) >> 12 == 2)) flags |= 1 << tvi.lParam;
		tvi.hItem = TreeView_GetNextSibling(hwndTree, tvi.hItem);
	}
	return flags;
}

int selectColor(HWND hwnd, COLORREF *clr)
{
	logmsg("SelectColor");

	CHOOSECOLOR cc = { 0 };
	cc.lStructSize = sizeof(cc);
	cc.hwndOwner = hwnd;
	cc.hInstance = (HWND)g_plugin.getInst();
	cc.rgbResult = *clr;
	cc.lpCustColors = pencustcolors;
	cc.Flags = CC_FULLOPEN | CC_RGBINIT;
	if (!ChooseColor(&cc))
		return 1;

	*clr = cc.rgbResult;
	return 0;
}

int selectFont(HWND hDlg, LOGFONT *lf)
{
	COLORREF color = RGB(0, 0, 0);

	logmsg("SelectFont");

	HDC hDC = GetDC(hDlg);

	CHOOSEFONT cf;
	memset(&cf, 0, sizeof(CHOOSEFONT));
	cf.lStructSize = sizeof(cf);
	cf.hwndOwner = hDlg;
	cf.hDC = hDC;
	cf.lpLogFont = lf;
	cf.rgbColors = 0;
	cf.Flags = CF_INITTOLOGFONTSTRUCT | CF_EFFECTS | CF_BOTH | CF_FORCEFONTEXIST;
	cf.nFontType = 0;
	cf.rgbColors = color;

	if (!ChooseFont(&cf)) {
		if (cf.hDC)
			DeleteDC(cf.hDC);

		ReleaseDC(hDlg, hDC);
		return 1;
	}

	if (cf.hDC)
		DeleteDC(cf.hDC);

	ReleaseDC(hDlg, hDC);
	return 0;
}

void loadDBSettings(plgsettings *ps)
{
	logmsg("loadDBSettings");

	ps->align = g_plugin.getByte("align", DEFAULT_ALIGN);
	ps->salign = g_plugin.getByte("salign", DEFAULT_SALIGN);
	ps->altShadow = g_plugin.getByte("altShadow", DEFAULT_ALTSHADOW);
	ps->transparent = g_plugin.getByte("transparent", DEFAULT_TRANPARENT);
	ps->showShadow = g_plugin.getByte("showShadow", DEFAULT_SHOWSHADOW);
	ps->messages = g_plugin.getByte("messages", DEFAULT_ANNOUNCEMESSAGES);
	ps->a_user = g_plugin.getByte("a_user", DEFAULT_ANNOUNCESTATUS);
	ps->distance = g_plugin.getByte("distance", DEFAULT_DISTANCE);
	ps->winx = g_plugin.getDword("winx", DEFAULT_WINX);
	ps->winy = g_plugin.getDword("winy", DEFAULT_WINY);
	ps->winxpos = g_plugin.getDword("winxpos", DEFAULT_WINXPOS);
	ps->winypos = g_plugin.getDword("winypos", DEFAULT_WINYPOS);
	ps->alpha = g_plugin.getByte("alpha", DEFAULT_ALPHA);
	ps->showmystatus = g_plugin.getByte("showMyStatus", DEFAULT_SHOWMYSTATUS);
	ps->timeout = g_plugin.getDword("timeout", DEFAULT_TIMEOUT);
	ps->clr_msg = g_plugin.getDword("clr_msg", DEFAULT_CLRMSG);
	ps->clr_status = g_plugin.getDword("clr_status", DEFAULT_CLRSTATUS);
	ps->clr_shadow = g_plugin.getDword("clr_shadow", DEFAULT_CLRSHADOW);
	ps->bkclr = g_plugin.getDword("bkclr", DEFAULT_BKCLR);

	ps->showMsgWindow = g_plugin.getByte("showMessageWindow", DEFAULT_SHOWMSGWIN);
	ps->showWhen = g_plugin.getDword("showWhen", DEFAULT_SHOWWHEN);

	DBVARIANT dbv;
	if (!g_plugin.getWString("message_format", &dbv)) {
		mir_wstrcpy(ps->msgformat, dbv.pwszVal);
		db_free(&dbv);
	}
	else mir_wstrcpy(ps->msgformat, DEFAULT_MESSAGEFORMAT);

	ps->announce = g_plugin.getDword("announce", DEFAULT_ANNOUNCE);

	ps->lf.lfHeight = g_plugin.getDword("fntHeight", DEFAULT_FNT_HEIGHT);
	ps->lf.lfWidth = g_plugin.getDword("fntWidth", DEFAULT_FNT_WIDTH);
	ps->lf.lfEscapement = g_plugin.getDword("fntEscapement", DEFAULT_FNT_ESCAPEMENT);
	ps->lf.lfOrientation = g_plugin.getDword("fntOrientation", DEFAULT_FNT_ORIENTATION);
	ps->lf.lfWeight = g_plugin.getDword("fntWeight", DEFAULT_FNT_WEIGHT);
	ps->lf.lfItalic = g_plugin.getByte("fntItalic", DEFAULT_FNT_ITALIC);
	ps->lf.lfUnderline = g_plugin.getByte("fntUnderline", DEFAULT_FNT_UNDERLINE);
	ps->lf.lfStrikeOut = g_plugin.getByte("fntStrikeout", DEFAULT_FNT_STRIKEOUT);
	ps->lf.lfCharSet = g_plugin.getByte("fntCharSet", DEFAULT_FNT_CHARSET);
	ps->lf.lfOutPrecision = g_plugin.getByte("fntOutPrecision", DEFAULT_FNT_OUTPRECISION);
	ps->lf.lfClipPrecision = g_plugin.getByte("fntClipPrecision", DEFAULT_FNT_CLIPRECISION);
	ps->lf.lfQuality = g_plugin.getByte("fntQuality", DEFAULT_FNT_QUALITY);
	ps->lf.lfPitchAndFamily = g_plugin.getByte("fntPitchAndFamily", DEFAULT_FNT_PITCHANDFAM);

	if (!g_plugin.getWString("fntFaceName", &dbv)) {
		mir_wstrcpy(ps->lf.lfFaceName, dbv.pwszVal);
		db_free(&dbv);
	}
	else
		mir_wstrcpy(ps->lf.lfFaceName, DEFAULT_FNT_FACENAME);
}

void saveDBSettings(plgsettings *ps)
{
	logmsg("saveDBSettings");

	g_plugin.setByte("showShadow", ps->showShadow);
	g_plugin.setByte("altShadow", ps->altShadow);
	g_plugin.setByte("distance", ps->distance);

	g_plugin.setDword("winx", ps->winx);
	g_plugin.setDword("winy", ps->winy);
	g_plugin.setDword("winxpos", ps->winxpos);
	g_plugin.setDword("winypos", ps->winypos);

	g_plugin.setByte("alpha", ps->alpha);
	g_plugin.setDword("timeout", ps->timeout);

	g_plugin.setByte("transparent", ps->transparent);
	g_plugin.setByte("messages", ps->messages);
	g_plugin.setByte("a_user", ps->a_user);
	g_plugin.setWString("message_format", ps->msgformat);

	g_plugin.setByte("align", ps->align);
	g_plugin.setByte("salign", ps->salign);

	g_plugin.setByte("showMyStatus", ps->showmystatus);

	g_plugin.setDword("clr_msg", ps->clr_msg);
	g_plugin.setDword("clr_shadow", ps->clr_shadow);
	g_plugin.setDword("clr_status", ps->clr_status);
	g_plugin.setDword("bkclr", ps->bkclr);

	g_plugin.setDword("fntHeight", ps->lf.lfHeight);
	g_plugin.setDword("fntWidth", ps->lf.lfWidth);
	g_plugin.setDword("fntEscapement", ps->lf.lfEscapement);
	g_plugin.setDword("fntOrientation", ps->lf.lfOrientation);
	g_plugin.setDword("fntWeight", ps->lf.lfWeight);
	g_plugin.setByte("fntItalic", ps->lf.lfItalic);
	g_plugin.setByte("fntUnderline", ps->lf.lfUnderline);
	g_plugin.setByte("fntStrikeout", ps->lf.lfStrikeOut);
	g_plugin.setByte("fntCharSet", ps->lf.lfCharSet);
	g_plugin.setByte("fntOutPrecision", ps->lf.lfOutPrecision);
	g_plugin.setByte("fntClipPrecision", ps->lf.lfClipPrecision);
	g_plugin.setByte("fntQuality", ps->lf.lfQuality);
	g_plugin.setByte("fntPitchAndFamily", ps->lf.lfPitchAndFamily);
	g_plugin.setWString("fntFaceName", ps->lf.lfFaceName);

	g_plugin.setDword("announce", ps->announce);

	g_plugin.setByte("showMessageWindow", ps->showMsgWindow);
	g_plugin.setDword("showWhen", ps->showWhen);
}

INT_PTR CALLBACK OptDlgProc(HWND hDlg, UINT msg, WPARAM wparam, LPARAM lparam)
{
	RECT rect;
	plgsettings *ps; //0: current; 1: original

	logmsg("OptDlgProc");

	switch (msg) {
	case WM_INITDIALOG:
		logmsg("OptDlgProc::INITDIALOG");
		TranslateDialogDefault(hDlg);

		ps = (plgsettings*)malloc(sizeof(plgsettings) * 2);
		loadDBSettings(&ps[0]);
		ps[1] = ps[0];
		SetWindowLongPtr(hDlg, GWLP_USERDATA, (LONG_PTR)ps);
		SetWindowLongPtr(g_hWnd, GWL_STYLE, WS_POPUP | WS_SIZEBOX);
		SetWindowPos(g_hWnd, nullptr, 0, 0, 0, 0, SWP_NOZORDER | SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE | SWP_FRAMECHANGED);

		SetWindowLongPtr(GetDlgItem(hDlg, IDC_TREE1), GWL_STYLE, GetWindowLongPtr(GetDlgItem(hDlg, IDC_TREE1), GWL_STYLE) | TVS_NOHSCROLL | TVS_CHECKBOXES);
		SetWindowLongPtr(GetDlgItem(hDlg, IDC_TREE2), GWL_STYLE, GetWindowLongPtr(GetDlgItem(hDlg, IDC_TREE1), GWL_STYLE) | TVS_NOHSCROLL | TVS_CHECKBOXES);

		CheckDlgButton(hDlg, IDC_RADIO1 + ps->align - 1, BST_CHECKED);
		CheckDlgButton(hDlg, IDC_RADIO10 + 9 - ps->salign, BST_CHECKED);
		CheckDlgButton(hDlg, IDC_CHECK1, ps->altShadow ? BST_CHECKED : BST_UNCHECKED);
		CheckDlgButton(hDlg, IDC_CHECK2, ps->showMsgWindow ? BST_CHECKED : BST_UNCHECKED);
		CheckDlgButton(hDlg, IDC_CHECK3, ps->transparent ? BST_CHECKED : BST_UNCHECKED);
		CheckDlgButton(hDlg, IDC_CHECK4, ps->showShadow ? BST_CHECKED : BST_UNCHECKED);
		CheckDlgButton(hDlg, IDC_CHECK5, ps->messages ? BST_CHECKED : BST_UNCHECKED);

		SetDlgItemText(hDlg, IDC_EDIT2, ps->msgformat);

		CheckDlgButton(hDlg, IDC_CHECK6, ps->a_user ? BST_CHECKED : BST_UNCHECKED);
		CheckDlgButton(hDlg, IDC_CHECK7, ps->showmystatus ? BST_CHECKED : BST_UNCHECKED);
		SetDlgItemInt(hDlg, IDC_EDIT1, ps->distance, 0);

		SendDlgItemMessage(hDlg, IDC_SLIDER1, TBM_SETRANGE, 0, MAKELONG(0, 255));
		SendDlgItemMessage(hDlg, IDC_SLIDER1, TBM_SETPOS, TRUE, (LPARAM)ps->alpha);

		{
			wchar_t buf[20];
			mir_snwprintf(buf, L"%d %%", ps->alpha * 100 / 255);
			SetDlgItemText(hDlg, IDC_ALPHATXT, buf);
		}

		SetDlgItemInt(hDlg, IDC_EDIT5, ps->timeout, 0);
		FillCheckBoxTree(GetDlgItem(hDlg, IDC_TREE1), ps->announce);
		FillCheckBoxTree(GetDlgItem(hDlg, IDC_TREE2), ps->showWhen);
		return 0;

	case WM_HSCROLL:
		if (LOWORD(wparam) == SB_ENDSCROLL || LOWORD(wparam) == SB_THUMBPOSITION || LOWORD(wparam) == SB_ENDSCROLL)
			return 0;
		ps = (plgsettings*)GetWindowLongPtr(hDlg, GWLP_USERDATA);
		ps->alpha = SendDlgItemMessage(hDlg, IDC_SLIDER1, TBM_GETPOS, 0, 0);
		{
			wchar_t buf[20];
			mir_snwprintf(buf, L"%d %%", ps->alpha * 100 / 255);
			SetDlgItemText(hDlg, IDC_ALPHATXT, buf);
		}
		goto xxx;
	case WM_DESTROY:
		logmsg("OptDlgProc::DESTROY");
		ps = (plgsettings*)GetWindowLongPtr(hDlg, GWLP_USERDATA);
		ps[0] = ps[1];
		saveDBSettings(&ps[0]);

		SetWindowLongPtr(g_hWnd, GWL_STYLE, WS_POPUP);
		SetWindowPos(g_hWnd, nullptr, 0, 0, 0, 0, SWP_NOZORDER | SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE | SWP_FRAMECHANGED);

		SetWindowPos(g_hWnd, nullptr, ps->winxpos, ps->winypos, ps->winx, ps->winy, SWP_NOZORDER | SWP_NOACTIVATE);
		SetLayeredWindowAttributes(g_hWnd, ps->bkclr, ps->alpha, (ps->transparent ? LWA_COLORKEY : 0) | LWA_ALPHA);

		free((void*)GetWindowLongPtr(hDlg, GWLP_USERDATA));
		return 0;
	case WM_COMMAND:
		logmsg("OptDlgProc::COMMAND");
		ps = (plgsettings*)GetWindowLongPtr(hDlg, GWLP_USERDATA);
		switch (LOWORD(wparam)) {
		case IDC_BUTTON7:
			MessageBox(hDlg, TranslateT("Variables:\n  %n : Nick\n  %m : Message\n  %l : New line"), TranslateT("Help"), MB_ICONINFORMATION | MB_OK);
			return 0;
		case IDC_BUTTON5:
			SendMessage(g_hWnd, WM_USER + 1, (WPARAM)TranslateT("Miranda NG is great and this is a long message."), 0);
			break;
		case IDC_BUTTON1:
			selectFont(hDlg, &(ps->lf));
			break;
		case IDC_BUTTON2:
			selectColor(hDlg, &ps->clr_status);
			break;
		case IDC_BUTTON6:
			selectColor(hDlg, &ps->clr_msg);
			break;
		case IDC_BUTTON3:
			selectColor(hDlg, &ps->clr_shadow);
			break;
		case IDC_BUTTON4:
			selectColor(hDlg, &ps->bkclr);
			break;
		case IDC_CHECK4:
			ps->showShadow = IsDlgButtonChecked(hDlg, IDC_CHECK4);
			break;
		case IDC_CHECK1:
			ps->altShadow = IsDlgButtonChecked(hDlg, IDC_CHECK1);
			break;
		case IDC_CHECK2:
			ps->showMsgWindow = IsDlgButtonChecked(hDlg, IDC_CHECK2);
		case IDC_EDIT1:
			ps->distance = GetDlgItemInt(hDlg, IDC_EDIT1, nullptr, 0);
			break;
		case IDC_EDIT5:
			ps->timeout = GetDlgItemInt(hDlg, IDC_EDIT5, nullptr, 0);
			break;
		case IDC_CHECK3:
			ps->transparent = IsDlgButtonChecked(hDlg, IDC_CHECK3);
			break;
		case IDC_CHECK5:
			ps->messages = IsDlgButtonChecked(hDlg, IDC_CHECK5);
			break;
		case IDC_CHECK6:
			ps->a_user = IsDlgButtonChecked(hDlg, IDC_CHECK6);
			break;
		case IDC_CHECK7:
			ps->showmystatus = IsDlgButtonChecked(hDlg, IDC_CHECK7);
			break;
		case IDC_RADIO1:
		case IDC_RADIO2:
		case IDC_RADIO3:
		case IDC_RADIO4:
		case IDC_RADIO5:
		case IDC_RADIO6:
		case IDC_RADIO7:
		case IDC_RADIO8:
		case IDC_RADIO9:
			if (IsDlgButtonChecked(hDlg, LOWORD(wparam)))
				ps->align = LOWORD(wparam) - IDC_RADIO1 + 1;
			break;
		case IDC_RADIO10:
		case IDC_RADIO11:
		case IDC_RADIO12:
		case IDC_RADIO13:
		case IDC_RADIO14:
		case IDC_RADIO15:
		case IDC_RADIO16:
		case IDC_RADIO17:
		case IDC_RADIO18:
			if (IsDlgButtonChecked(hDlg, LOWORD(wparam)))
				ps->salign = 10 - (LOWORD(wparam) - IDC_RADIO10 + 1);
			break;
		}
xxx:
		saveDBSettings(ps);
		SetWindowPos(g_hWnd, nullptr, 0, 0, ps->winx, ps->winy, SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE);
		SetLayeredWindowAttributes(g_hWnd, 
			g_plugin.getDword("bkclr", DEFAULT_BKCLR), 
			g_plugin.getByte("alpha", DEFAULT_ALPHA), 
			(g_plugin.getByte("transparent", DEFAULT_TRANPARENT) ? LWA_COLORKEY : 0) | LWA_ALPHA);
		InvalidateRect(g_hWnd, nullptr, TRUE);
		SendMessage(GetParent(hDlg), PSM_CHANGED, 0, 0);

		return 0;

	case WM_NOTIFY:
		logmsg("OptDlgProc::NOTIFY");
		switch (((LPNMHDR)lparam)->code) {
		case TVN_SETDISPINFO:
		case NM_CLICK:
		case NM_RETURN:
		case TVN_SELCHANGED:
			if (((LPNMHDR)lparam)->idFrom == IDC_TREE1)
				SendMessage(GetParent(hDlg), PSM_CHANGED, 0, 0);
			break;
		
		case PSN_APPLY:
			ps = (plgsettings*)GetWindowLongPtr(hDlg, GWLP_USERDATA);

			GetWindowRect(g_hWnd, &rect);
			ps->winx = rect.right - rect.left;
			ps->winy = rect.bottom - rect.top;
			ps->winxpos = rect.left;
			ps->winypos = rect.top;
			ps->announce = MakeCheckBoxTreeFlags(GetDlgItem(hDlg, IDC_TREE1));
			ps->showWhen = MakeCheckBoxTreeFlags(GetDlgItem(hDlg, IDC_TREE2));
			GetDlgItemText(hDlg, IDC_EDIT2, ps->msgformat, 255);
			ps[1] = ps[0]; //apply current settings at closing

			saveDBSettings(ps);
			SetLayeredWindowAttributes(g_hWnd, g_plugin.getDword("bkclr", DEFAULT_BKCLR), g_plugin.getByte("alpha", DEFAULT_ALPHA), (g_plugin.getByte("transparent", DEFAULT_TRANPARENT) ? LWA_COLORKEY : 0) | LWA_ALPHA);
			InvalidateRect(g_hWnd, nullptr, TRUE);
			break;
		}
		break;
	}

	return 0;
}

int OptionsInit(WPARAM wparam, LPARAM)
{
	OPTIONSDIALOGPAGE odp = {};
	odp.position = 150000000;
	odp.pszTemplate = MAKEINTRESOURCEA(IDD_DIALOG1);
	odp.szGroup.w = LPGENW("Plugins");
	odp.szTitle.w = LPGENW("OSD");
	odp.pfnDlgProc = OptDlgProc;
	odp.flags = ODPF_BOLDGROUPS | ODPF_UNICODE;
	g_plugin.addOptions(wparam, &odp);
	return 0;
}