summaryrefslogtreecommitdiff
path: root/protocols/Weather/src/weather_popup.cpp
blob: b9f938e41d7468adecab0c4d4135cb3c5be1e199 (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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
/*
Weather Protocol plugin for Miranda IM
Copyright (c) 2012 Miranda NG team
Copyright (c) 2005-2009 Boris Krasnovskiy All Rights Reserved
Copyright (c) 2002-2005 Calvin Che

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; version 2
of the License.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

/* This file contain the source related to weather popups, including popup
	options, popup display, and the code for popup process.
*/

#include "stdafx.h"

// variables for weather_popup.c
static MCONTACT hPopupContact;

/////////////////////////////////////////////////////////////////////////////////////////
// wrapper function for displaying weather warning popup by triggering an event (threaded)
// lpzText = error text
// kind = display type (see m_popup.h)

int CWeatherProto::WPShowMessage(const wchar_t *lpzText, int kind)
{
	if (!m_bPopups)
		return 0;

	if (kind == SM_WARNING)
		PUShowMessageW(lpzText, SM_WARNING);
	else if (kind == SM_NOTIFY)
		PUShowMessageW(lpzText, SM_NOTIFY);
	else if (kind == SM_WEATHERALERT) {
		POPUPDATAW ppd;
		wchar_t str1[512], str2[512];

		// get the 2 strings
		wcsncpy(str1, lpzText, _countof(str1) - 1);
		wcsncpy(str2, lpzText, _countof(str2) - 1);
		wchar_t *chop = wcschr(str1, 255);
		if (chop != nullptr)
			*chop = '\0';
		else
			str1[0] = 0;

		chop = wcschr(str2, 255);
		if (chop != nullptr)
			wcsncpy(str2, chop + 1, _countof(str2) - 1);
		else
			str2[0] = 0;

		// setup the popup
		ppd.lchIcon = (HICON)LoadImage(nullptr, MAKEINTRESOURCE(OIC_BANG), IMAGE_ICON,
			GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), LR_SHARED);
		mir_wstrcpy(ppd.lpwzContactName, str1);
		mir_wstrcpy(ppd.lpwzText, str2);
		ppd.colorBack = (opt.UseWinColors) ? GetSysColor(COLOR_BTNFACE) : opt.BGColour;
		ppd.colorText = (opt.UseWinColors) ? GetSysColor(COLOR_WINDOWTEXT) : opt.TextColour;
		ppd.iSeconds = opt.pDelay;
		PUAddPopupW(&ppd);
	}
	return 0;
}

/////////////////////////////////////////////////////////////////////////////////////////
// popup dialog pocess
// for selecting actions when click on the popup window
// use for displaying contact menu

static LRESULT CALLBACK PopupDlgProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	uint32_t ID;
	MCONTACT hContact = PUGetContact(hWnd);
	auto *ppro = (CWeatherProto*)Proto_GetContactInstance(hContact);

	switch (message) {
	case WM_COMMAND:
		ID = (ppro) ? ppro->opt.LeftClickAction : 0;
		if (ID != IDM_M7)  PUDeletePopup(hWnd);
		SendMessage(hPopupWindow, ID, hContact, LPARAM(ppro));
		return TRUE;

	case WM_CONTEXTMENU:
		ID = (ppro) ? ppro->opt.RightClickAction : IDM_M7;
		if (ID != IDM_M7)  PUDeletePopup(hWnd);
		SendMessage(hPopupWindow, ID, hContact, LPARAM(ppro));
		return TRUE;

	case UM_FREEPLUGINDATA:
		DestroyIcon((HICON)PUGetPluginData(hWnd));
		return FALSE;
	}

	return DefWindowProc(hWnd, message, wParam, lParam);
}

/////////////////////////////////////////////////////////////////////////////////////////
// display weather popups
// wParam = the contact to display popup
// lParam = whether the weather data is changed or not

int CWeatherProto::WeatherPopup(MCONTACT hContact, bool bAlways)
{
	// determine if the popup should display or not
	if (m_bPopups && opt.UpdatePopup && (!opt.PopupOnChange || bAlways) && !getByte(hContact, "DPopUp")) {
		WEATHERINFO winfo = LoadWeatherInfo(hContact);

		// setup the popup
		POPUPDATAW ppd;
		ppd.lchContact = hContact;
		ppd.PluginData = ppd.lchIcon = GetStatusIcon(winfo.hContact);
		wcsncpy_s(ppd.lpwzContactName, GetDisplay(&winfo, GetTextValue('P')), _TRUNCATE);
		wcsncpy_s(ppd.lpwzText, GetDisplay(&winfo, GetTextValue('p')), _TRUNCATE);
		ppd.PluginWindowProc = PopupDlgProc;
		ppd.colorBack = (opt.UseWinColors) ? GetSysColor(COLOR_BTNFACE) : opt.BGColour;
		ppd.colorText = (opt.UseWinColors) ? GetSysColor(COLOR_WINDOWTEXT) : opt.TextColour;
		ppd.iSeconds = opt.pDelay;
		PUAddPopupW(&ppd);
	}
	return 0;
}

/////////////////////////////////////////////////////////////////////////////////////////
// process for the popup window
// containing the code for popup actions

LRESULT CALLBACK CWeatherProto::PopupWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	POINT pt;
	HMENU hMenu;
	auto *ppro = (CWeatherProto *)lParam;

	switch (uMsg) {
	case IDM_M2:	// brief info
		ppro->BriefInfo(wParam, 0);
		break;

	case IDM_M3:	// read complete forecast
		ppro->LoadForecast(wParam, 0);
		break;

	case IDM_M4:	// display weather map
		ppro->WeatherMap(wParam, 0);
		break;

	case IDM_M5:	// open history window
		CallService(MS_HISTORY_SHOWCONTACTHISTORY, wParam, 0);
		break;

	case IDM_M6:	// open external log
		ppro->ViewLog(wParam, 0);
		break;

	case IDM_M7:	// display contact menu
		hMenu = Menu_BuildContactMenu(wParam);
		GetCursorPos(&pt);
		hPopupContact = wParam;
		TrackPopupMenu(hMenu, TPM_LEFTALIGN, pt.x, pt.y, 0, hWnd, nullptr);
		DestroyMenu(hMenu);
		break;

	case IDM_M8:	// display contact detail
		CallService(MS_USERINFO_SHOWDIALOG, wParam, 0);

	case WM_COMMAND:	 //Needed by the contact's context menu
		if (Clist_MenuProcessCommand(LOWORD(wParam), MPCF_CONTACTMENU, hPopupContact))
			break;
		return FALSE;

	case WM_MEASUREITEM: //Needed by the contact's context menu
		return Menu_MeasureItem(lParam);

	case WM_DRAWITEM: //Needed by the contact's context menu
		return Menu_DrawItem(lParam);
	}

	return DefWindowProc(hWnd, uMsg, wParam, lParam);//FALSE;
}

/////////////////////////////////////////////////////////////////////////////////////////

struct
{
	wchar_t c;
	int  id;
	char *setting;
}
static controls[] =
{
	{ 'p', IDC_PText, "PopupText" },
	{ 'P', IDC_PTitle, "PopupTitle" },
};

// used to select the menu item for popup action menu
static void SelectMenuItem(HMENU hMenu, int Check)
{
	for (int i = 0; i <= GetMenuItemCount(hMenu) - 1; i++)
		CheckMenuItem(hMenu, i, MF_BYPOSITION | ((int)GetMenuItemID(hMenu, i) == Check) * 8);
}

class CPopupOptsDlg : public CWeatherDlgBase
{
	CCtrlEdit edtDelay;
	CCtrlCheck chkUseWin;
	CCtrlButton btnLeft, btnRight, btnPD1, btnPD2, btnPD3, btnPdef, btnVars, btnPreview;

	// temporary read the current option to memory
	// but does not write to the database
	void ReadPopupOpt(HWND hdlg)
	{
		wchar_t str[512];
		auto &opt = m_proto->opt;

		// popup colour
		opt.TextColour = SendDlgItemMessage(hdlg, IDC_TEXTCOLOUR, CPM_GETCOLOUR, 0, 0);
		opt.BGColour = SendDlgItemMessage(hdlg, IDC_BGCOLOUR, CPM_GETCOLOUR, 0, 0);

		// get delay time
		GetDlgItemText(hdlg, IDC_DELAY, str, _countof(str));
		int num = _wtoi(str);
		opt.pDelay = num;

		// other options
		opt.UseWinColors = chkUseWin.IsChecked();
		opt.UpdatePopup = (uint8_t)IsDlgButtonChecked(hdlg, IDC_POP1);
		opt.AlertPopup = (uint8_t)IsDlgButtonChecked(hdlg, IDC_POP2);
		opt.PopupOnChange = (uint8_t)IsDlgButtonChecked(hdlg, IDC_CH);
		opt.ShowWarnings = (uint8_t)IsDlgButtonChecked(hdlg, IDC_W);
	}

public:
	CPopupOptsDlg(CWeatherProto *ppro) :
		CWeatherDlgBase(ppro, IDD_POPUP),
		btnPD1(this, IDC_PD1),
		btnPD2(this, IDC_PD2),
		btnPD3(this, IDC_PD3),
		btnPdef(this, IDC_PDEF),
		btnVars(this, IDC_VAR3),
		btnLeft(this, IDC_LeftClick),
		btnRight(this, IDC_RightClick),
		btnPreview(this, IDC_PREVIEW),
		edtDelay(this, IDC_DELAY),
		chkUseWin(this, IDC_USEWINCOLORS)
	{
		edtDelay.OnChange = Callback(this, &CPopupOptsDlg::onChanged_Delay);
		chkUseWin.OnChange = Callback(this, &CPopupOptsDlg::onChanged_UseWin);

		btnPD1.OnClick = Callback(this, &CPopupOptsDlg::onClick_PD1);
		btnPD2.OnClick = Callback(this, &CPopupOptsDlg::onClick_PD2);
		btnPD3.OnClick = Callback(this, &CPopupOptsDlg::onChanged_Delay);
		btnPdef.OnClick = Callback(this, &CPopupOptsDlg::onClick_Pdef);
		btnVars.OnClick = Callback(this, &CPopupOptsDlg::onClick_Vars);
		btnLeft.OnClick = Callback(this, &CPopupOptsDlg::onClick_Left);
		btnRight.OnClick = Callback(this, &CPopupOptsDlg::onClick_Right);
		btnPreview.OnClick = Callback(this, &CPopupOptsDlg::onClick_Preview);
	}

	bool OnInitDialog() override
	{
		m_proto->SaveOptions();

		// click actions
		HMENU hMenu = LoadMenu(g_plugin.getInst(), MAKEINTRESOURCE(IDR_PMENU));
		HMENU hMenu1 = GetSubMenu(hMenu, 0);

		wchar_t str[512];
		auto &opt = m_proto->opt;
		GetMenuStringW(hMenu1, opt.LeftClickAction, str, _countof(str), MF_BYCOMMAND);
		SetDlgItemTextW(m_hwnd, IDC_LeftClick, TranslateW(str));
		GetMenuStringW(hMenu1, opt.RightClickAction, str, _countof(str), MF_BYCOMMAND);
		SetDlgItemTextW(m_hwnd, IDC_RightClick, TranslateW(str));
		DestroyMenu(hMenu);

		// other options
		CheckDlgButton(m_hwnd, IDC_POP2, opt.AlertPopup ? BST_CHECKED : BST_UNCHECKED);
		CheckDlgButton(m_hwnd, IDC_POP1, opt.UpdatePopup ? BST_CHECKED : BST_UNCHECKED);
		CheckDlgButton(m_hwnd, IDC_CH, opt.PopupOnChange ? BST_CHECKED : BST_UNCHECKED);
		CheckDlgButton(m_hwnd, IDC_W, opt.ShowWarnings ? BST_CHECKED : BST_UNCHECKED);

		for (auto &it : controls)
			SetDlgItemText(m_hwnd, it.id, m_proto->GetTextValue(it.c));

		// setting popup delay option
		_ltow(opt.pDelay, str, 10);
		SetDlgItemText(m_hwnd, IDC_DELAY, str);
		if (opt.pDelay == -1)
			CheckRadioButton(m_hwnd, IDC_PD1, IDC_PD3, IDC_PD2);
		else if (opt.pDelay == 0)
			CheckRadioButton(m_hwnd, IDC_PD1, IDC_PD3, IDC_PD1);
		else
			CheckRadioButton(m_hwnd, IDC_PD1, IDC_PD3, IDC_PD3);

		// Colours. First step is configuring the colours.
		SendDlgItemMessage(m_hwnd, IDC_BGCOLOUR, CPM_SETCOLOUR, 0, opt.BGColour);
		SendDlgItemMessage(m_hwnd, IDC_TEXTCOLOUR, CPM_SETCOLOUR, 0, opt.TextColour);

		// Second step is disabling them if we want to use default Windows ones.
		chkUseWin.SetState(opt.UseWinColors);

		// buttons
		SendDlgItemMessage(m_hwnd, IDC_PREVIEW, BUTTONSETASFLATBTN, TRUE, 0);
		SendDlgItemMessage(m_hwnd, IDC_PDEF, BUTTONSETASFLATBTN, TRUE, 0);
		SendDlgItemMessage(m_hwnd, IDC_LeftClick, BUTTONSETASFLATBTN, TRUE, 0);
		SendDlgItemMessage(m_hwnd, IDC_RightClick, BUTTONSETASFLATBTN, TRUE, 0);
		SendDlgItemMessage(m_hwnd, IDC_VAR3, BUTTONSETASFLATBTN, TRUE, 0);
		return true;
	}

	bool OnApply() override
	{
		ReadPopupOpt(m_hwnd);

		wchar_t textstr[MAX_TEXT_SIZE];
		for (auto &it : controls) {
			GetDlgItemText(m_hwnd, it.id, textstr, _countof(textstr));
			if (!mir_wstrcmpi(textstr, GetDefaultText(it.c)))
				m_proto->delSetting(it.setting);
			else
				m_proto->setWString(it.setting, textstr);
		}

		// save the options, and update main menu
		m_proto->SaveOptions();
		return true;
	}

	void onChanged_UseWin(CCtrlCheck *pCheck)
	{
		// use window color - enable/disable color selection controls
		bool bEnable = !pCheck->IsChecked();
		EnableWindow(GetDlgItem(m_hwnd, IDC_BGCOLOUR), bEnable);
		EnableWindow(GetDlgItem(m_hwnd, IDC_TEXTCOLOUR), bEnable);
	}

	void onClick_Right(CCtrlButton *)
	{
		// right click action selection menu
		RECT pos;
		GetWindowRect(GetDlgItem(m_hwnd, IDC_RightClick), &pos);

		HMENU hMenu = LoadMenu(g_plugin.getInst(), MAKEINTRESOURCE(IDR_PMENU));
		HMENU hMenu1 = GetSubMenu(hMenu, 0);
		TranslateMenu(hMenu1);
		SelectMenuItem(hMenu1, m_proto->opt.RightClickAction);
		int ID = TrackPopupMenu(hMenu1, TPM_LEFTBUTTON | TPM_RETURNCMD, pos.left, pos.bottom, 0, m_hwnd, nullptr);
		if (ID)
			m_proto->opt.RightClickAction = ID;
		DestroyMenu(hMenu);

		hMenu = LoadMenu(g_plugin.getInst(), MAKEINTRESOURCE(IDR_PMENU));
		hMenu1 = GetSubMenu(hMenu, 0);

		wchar_t str[512];
		GetMenuString(hMenu1, m_proto->opt.RightClickAction, str, _countof(str), MF_BYCOMMAND);
		SetDlgItemText(m_hwnd, IDC_RightClick, TranslateW(str));
		DestroyMenu(hMenu);
	}

	void onClick_Left(CCtrlButton *)
	{
		// left click action selection menu
		RECT pos;
		GetWindowRect(GetDlgItem(m_hwnd, IDC_LeftClick), &pos);

		HMENU hMenu = LoadMenu(g_plugin.getInst(), MAKEINTRESOURCE(IDR_PMENU));
		HMENU hMenu1 = GetSubMenu(hMenu, 0);
		TranslateMenu(hMenu1);
		SelectMenuItem(hMenu1, m_proto->opt.LeftClickAction);
		int ID = TrackPopupMenu(hMenu1, TPM_LEFTBUTTON | TPM_RETURNCMD, pos.left, pos.bottom, 0, m_hwnd, nullptr);
		if (ID)
			m_proto->opt.LeftClickAction = ID;
		DestroyMenu(hMenu);

		hMenu = LoadMenu(g_plugin.getInst(), MAKEINTRESOURCE(IDR_PMENU));
		hMenu1 = GetSubMenu(hMenu, 0);

		wchar_t str[512];
		GetMenuString(hMenu1, m_proto->opt.LeftClickAction, str, _countof(str), MF_BYCOMMAND);
		SetDlgItemText(m_hwnd, IDC_LeftClick, TranslateW(str));
		DestroyMenu(hMenu);
	}

	void onClick_PD1(CCtrlButton *)
	{
		// Popup delay setting from popup plugin
		SetDlgItemText(m_hwnd, IDC_DELAY, L"0");
		CheckRadioButton(m_hwnd, IDC_PD1, IDC_PD3, IDC_PD1);
	}

	void onClick_PD2(CCtrlButton *)
	{
		// Popup delay = permanent
		SetDlgItemText(m_hwnd, IDC_DELAY, L"-1");
		CheckRadioButton(m_hwnd, IDC_PD1, IDC_PD3, IDC_PD2);
	}

	void onChanged_Delay(CCtrlEdit *)
	{
		// if text is edited
		CheckRadioButton(m_hwnd, IDC_PD1, IDC_PD3, IDC_PD3);
	}

	void onClick_Pdef(CCtrlButton *)
	{
		// set the default value for popup texts
		for (auto &it : controls)
			SetDlgItemText(m_hwnd, it.id, GetDefaultText(it.c));
	}

	void onClick_Vars(CCtrlButton *)
	{
		// display variable list
		CMStringW wszText;
		wszText += L"                                                            \n"; // to make the message box wider
		wszText += TranslateT("%c\tcurrent condition\n%d\tcurrent date\n%e\tdewpoint\n%f\tfeel-like temperature\n%h\ttoday's high\n%i\twind direction\n%l\ttoday's low\n%m\thumidity\n%n\tstation name\n%p\tpressure\n%r\tsunrise time\n%s\tstation ID\n%t\ttemperature\n%u\tupdate time\n%v\tvisibility\n%w\twind speed\n%y\tsun set");
		wszText += L"\n";
		wszText += TranslateT("%[..]\tcustom variables");
		MessageBox(nullptr, wszText, TranslateT("Variable List"), MB_OK | MB_ICONASTERISK | MB_TOPMOST);
	}
	
	void onClick_Preview(CCtrlButton *)
	{
		// popup preview
		MCONTACT hContact = m_proto->opt.DefStn;
		ReadPopupOpt(m_hwnd);	// read new options to memory
		m_proto->WeatherPopup(m_proto->opt.DefStn, true);	// display popup using new opt
		m_proto->LoadOptions();		// restore old option in memory
		m_proto->opt.DefStn = hContact;
	}
};

void CWeatherProto::InitPopupOptions(WPARAM wParam)
{
	// if popup service exists, load the weather popup options
	OPTIONSDIALOGPAGE odp = {};
	odp.flags = ODPF_BOLDGROUPS | ODPF_UNICODE;
	odp.position = 100000000;
	odp.szGroup.w = LPGENW("Popups");
	odp.szTitle.w = m_tszUserName;
	odp.pDialog = new CPopupOptsDlg(this);
	g_plugin.addOptions(wParam, &odp);
}