summaryrefslogtreecommitdiff
path: root/plugins/wbOSD/src/wbOSD.cpp
blob: 7b227db5cb6a812cbd5394c4e6926a0f9e981746 (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
/*
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"

wchar_t szClassName[] = L"wbOSD";
const static osdmsg defstr = { L"", 0, RGB(0, 0, 0), nullptr, 0 };

int DrawMe(HWND hwnd, wchar_t *string, COLORREF color)
{
	logmsg("DrawMe");
	if (!string) string = L"bullshit";

	plgsettings plgs;
	loadDBSettings(&plgs);
	HFONT fh = CreateFontIndirect(&(plgs.lf));

	PAINTSTRUCT	ps;
	HDC hdc = BeginPaint(hwnd, &ps);
	SetBkMode(hdc, TRANSPARENT);

	RECT rect;
	GetClientRect(hwnd, &rect);
	HBRUSH bkb = CreateSolidBrush(plgs.bkclr);
	FillRect(hdc, &rect, bkb);

	DeleteObject(bkb);

	HGDIOBJ oo = SelectObject(hdc, fh);

	UINT talign = 0;
	RECT rect2 = rect;
	DrawText(hdc, string, -1, &rect2, DT_WORDBREAK | DT_CALCRECT);

	if (plgs.align <= 3)
		rect.top = 0;
	else if (plgs.align <= 6)
		rect.top = (rect.bottom - rect2.bottom) / 2;
	else if (plgs.align <= 9)
		rect.top = rect.bottom - rect2.bottom;

	if (((plgs.align - 1) % 3) == 0)
		rect.left = 0;
	else if (((plgs.align - 2) % 3) == 0)
		rect.left = (rect.right - rect2.right) / 2;
	else if (((plgs.align - 3) % 3) == 0)
		rect.left = rect.right - rect2.right;

	rect.bottom = rect.top + rect2.bottom;
	rect.right = rect.left + rect2.right;

	//draw shadow
	if (plgs.showShadow) {
		int sxo, syo;
		logmsg("DrawMe::showShadow");
		if (plgs.salign <= 3)
			syo = -plgs.distance;
		else if (plgs.salign <= 6)
			syo = 0;
		else if (plgs.salign <= 9)
			syo = plgs.distance;
		else
			syo = 0;

		if (((plgs.salign - 1) % 3) == 0)
			sxo = -plgs.distance;
		else if (((plgs.salign - 2) % 3) == 0)
			sxo = 0;
		else if (((plgs.salign - 3) % 3) == 0)
			sxo = plgs.distance;
		else
			sxo = 0;

		SetTextColor(hdc, plgs.clr_shadow);
		if (plgs.altShadow == 0) {
			rect2 = rect;
			OffsetRect(&rect, sxo, syo);

			DrawText(hdc, string, -1, &rect2, DT_WORDBREAK | talign);
		}
		else {
			rect2 = rect;
			rect2.left += plgs.distance;
			DrawText(hdc, string, -1, &rect2, DT_WORDBREAK | talign);

			rect2 = rect;
			rect2.left -= plgs.distance;
			DrawText(hdc, string, -1, &rect2, DT_WORDBREAK | talign);

			rect2 = rect;
			rect2.top -= plgs.distance;
			DrawText(hdc, string, -1, &rect2, DT_WORDBREAK | talign);

			rect2 = rect;
			rect2.top += plgs.distance;
			DrawText(hdc, string, -1, &rect2, DT_WORDBREAK | talign);

			OffsetRect(&rect, sxo / 2, syo / 2);
		}
	}

	//draw text
	SetTextColor(hdc, color);
	DrawText(hdc, string, -1, &rect, DT_WORDBREAK);

	SelectObject(hdc, oo);
	DeleteObject(fh);
	EndPaint(hwnd, &ps);

	return 0;
}

LRESULT CALLBACK WindowProcedure(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	osdmsg *ms;

	switch (message) {
	case WM_CREATE:
		logmsg("WindowProcedure::CREATE");
		SetWindowLongPtr(hwnd, GWLP_USERDATA, (LONG_PTR)&defstr);
		return 0;

	case WM_DESTROY:
		logmsg("WindowProcedure::DESTROY");
		return 0;

	case WM_PAINT:
		logmsg("WindowProcedure::PAINT");

		ms = (osdmsg*)GetWindowLongPtr(hwnd, GWLP_USERDATA);
		if (ms)
			return DrawMe(hwnd, ms->text, ms->color);

		PAINTSTRUCT ps;
		BeginPaint(hwnd, &ps);
		EndPaint(hwnd, &ps);
		return 0;

	case WM_NCRBUTTONDOWN:
		logmsg("WindowProcedure::NCRBUTTONDOWN");

		ms = (osdmsg*)GetWindowLongPtr(hwnd, GWLP_USERDATA);
		if (ms) {
			if (ms->callback) ms->callback(ms->param);
			SendMessage(hwnd, WM_USER + 3, 0, 0);
		}
		return 0;

	case WM_TIMER:
		logmsg("WindowProcedure::TIMER");
		SendMessage(hwnd, WM_USER + 3, wParam, 0);
		return 0;

	case WM_USER + 1: //draw text ((char *)string, (int) timeout
		logmsg("WindowProcedure::USER+1");

		ms = (osdmsg*)mir_alloc(sizeof(osdmsg));
		ms->text = mir_wstrdup((wchar_t *)wParam);
		if (lParam == 0)
			lParam = db_get_dw(NULL, THIS_MODULE, "timeout", DEFAULT_TIMEOUT);
		ms->timeout = lParam;
		ms->callback = nullptr;
		ms->color = db_get_dw(NULL, THIS_MODULE, "clr_msg", DEFAULT_CLRMSG);
		ms->param = 0;
		SendMessage(hwnd, WM_USER + 4, (WPARAM)ms, 0);
		mir_free(ms->text);
		mir_free(ms);
		return 0;

	case WM_USER + 2: //show
		logmsg("WindowProcedure::USER+2");
		SetWindowPos(hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW | SWP_NOACTIVATE);
		return 0;

	case WM_USER + 3: //hide
		ms = (osdmsg*)GetWindowLongPtr(hwnd, GWLP_USERDATA);
		logmsg("WindowProcedure::USER+3");
		if (!ms)
			return 0;

		logmsg("WindowProcedure::USER+3/om");
		KillTimer(hwnd, (UINT_PTR)ms);
		mir_free(ms->text);
		mir_free(ms);
		SetWindowLongPtr(hwnd, GWLP_USERDATA, 0);
		ShowWindow(hwnd, SW_HIDE);
		return 0;

	case WM_USER + 4:
		logmsg("WindowProcedure::USER+4");

		ms = (osdmsg*)GetWindowLongPtr(hwnd, GWLP_USERDATA);
		if (ms != nullptr) {
			logmsg("WindowProcedure::USER+4/old");
			KillTimer(hwnd, (UINT_PTR)ms);
			mir_free(ms->text);
			mir_free(ms);
			SetWindowLongPtr(hwnd, GWLP_USERDATA, 0);
		}

		ms = (osdmsg*)mir_alloc(sizeof(osdmsg));
		memcpy(ms, (osdmsg*)wParam, sizeof(osdmsg));
		ms->text = mir_wstrdup(ms->text);

		SetWindowLongPtr(hwnd, GWLP_USERDATA, (LONG_PTR)ms);
		SetTimer(hwnd, (UINT_PTR)ms, (UINT)ms->timeout, nullptr);
		InvalidateRect(hwnd, nullptr, TRUE);
		SendMessage(hwnd, WM_USER + 2, 0, 0);
		return 0;

	case WM_NCHITTEST:
		{
			RECT rect;
			GetWindowRect(hwnd, &rect);

			logmsg("WindowProcedure::NCHITTEST");

			if (LOWORD(lParam) >= (rect.left + 5) && LOWORD(lParam) <= (rect.right - 5) &&
				HIWORD(lParam) >= (rect.top + 5) && HIWORD(lParam) <= (rect.bottom - 5))
				return HTCAPTION;
			return DefWindowProc(hwnd, message, wParam, lParam);
		}
		//here will be the doubleclick => open-message-window solution ;-)
		//case WM_NCLBUTTONDBLCLK:
		//	CallService(MS_MSG_SENDMESSAGE, wparam,(LPARAM)&odp);
		//	return 0;
	default:
		return DefWindowProc(hwnd, message, wParam, lParam);
	}
}

INT_PTR OSDAnnounce(WPARAM wParam, LPARAM lParam)
{
	logmsg("OSDAnnounce");
	return SendMessage(g_hWnd, WM_USER + 1, wParam, lParam);
}

int pluginShutDown(WPARAM, LPARAM)
{
	logmsg("pluginShutDown");
	if (g_hWnd) {
		logmsg("pluginShutDown/hwnd");
		SendMessage(g_hWnd, WM_USER + 3, 0, 0);
		DestroyWindow(g_hWnd);
		g_hWnd = nullptr;
	}
	return 0;
}

int MainInit(WPARAM, LPARAM)
{
	logmsg("MainInit");
	HookEvent(ME_OPT_INITIALISE, OptionsInit);

	WNDCLASSEX wincl;
	wincl.hInstance = g_plugin.getInst();
	wincl.lpszClassName = szClassName;
	wincl.lpfnWndProc = WindowProcedure;
	wincl.style = CS_DBLCLKS;
	wincl.cbSize = sizeof(WNDCLASSEX);

	wincl.hIcon = LoadIcon(nullptr, IDI_APPLICATION);
	wincl.hIconSm = LoadIcon(nullptr, IDI_APPLICATION);
	wincl.hCursor = LoadCursor(nullptr, IDC_ARROW);
	wincl.lpszMenuName = nullptr;
	wincl.cbClsExtra = 0;
	wincl.cbWndExtra = 0;
	wincl.hbrBackground = nullptr;

	if (!RegisterClassEx(&wincl))
		return 0;

	g_hWnd = CreateWindowEx(WS_EX_LAYERED | WS_EX_TOOLWINDOW, szClassName, L"WannaBeOSD",
		WS_POPUP,
		db_get_dw(NULL, THIS_MODULE, "winxpos", DEFAULT_WINXPOS),
		db_get_dw(NULL, THIS_MODULE, "winypos", DEFAULT_WINYPOS),
		db_get_dw(NULL, THIS_MODULE, "winx", DEFAULT_WINX),
		db_get_dw(NULL, THIS_MODULE, "winy", DEFAULT_WINY),
		HWND_DESKTOP, nullptr, g_plugin.getInst(), nullptr);

	SetWindowLongPtr(g_hWnd, GWLP_USERDATA, 0);

	SetLayeredWindowAttributes(g_hWnd, db_get_dw(NULL, THIS_MODULE, "bkclr", DEFAULT_BKCLR), db_get_b(NULL, THIS_MODULE, "alpha", DEFAULT_ALPHA), (db_get_b(NULL, THIS_MODULE, "transparent", DEFAULT_TRANPARENT) ? LWA_COLORKEY : 0) | LWA_ALPHA);

	CreateServiceFunction("OSD/Announce", OSDAnnounce);

	HookEvent(ME_DB_EVENT_ADDED, HookedNewEvent);

	// try to create ME_STATUSCHANGE_CONTACTSTATUSCHANGED event... I hope it fails when newstatusnotify or equal creates it before ;-)
	HANDLE hContactStatusChanged = HookEvent(ME_STATUSCHANGE_CONTACTSTATUSCHANGED, ContactStatusChanged);
	if (!hContactStatusChanged) {
		hHookContactStatusChanged = CreateHookableEvent(ME_STATUSCHANGE_CONTACTSTATUSCHANGED);
		HookEvent(ME_DB_CONTACT_SETTINGCHANGED, ContactSettingChanged);
		HookEvent(ME_STATUSCHANGE_CONTACTSTATUSCHANGED, ContactStatusChanged);
	}
	
	HookEvent(ME_PROTO_ACK, ProtoAck);
	HookEvent(ME_SYSTEM_SHUTDOWN, pluginShutDown);
	return 0;
}