summaryrefslogtreecommitdiff
path: root/plugins/SplashScreen/src/splash.cpp
blob: d2ce106a7cfd57ddf46de4b572d1a192097407ec (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
/*
Splash Screen Plugin for Miranda NG (www.miranda-ng.org)
(c) 2004-2007 nullbie, (c) 2005-2007 Thief

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; either version 2 of the License, or
(at your option) any later version.

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, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

#include "headers.h"

BOOL bpreviewruns;
MyBitmap *SplashBmp, *tmpBmp;

LRESULT CALLBACK SplashWindowProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	switch (message)
	{
		case WM_LOADED:
		{
			#ifdef _DEBUG
				logMessage(_T("WM_LOADED"), _T("called"));
			#endif

			if (!options.showtime) SetTimer(hwnd, 7, 2000, 0);

			break;
		}

		case WM_LBUTTONDOWN:
			PostMessage(hwnd, WM_CLOSE, 0, 0);
			break;

		case WM_TIMER:

			#ifdef _DEBUG
				TCHAR b [40];
				mir_sntprintf(b, SIZEOF(b), _T("%d"), wParam);
				logMessage(_T("Timer ID"), b);
				mir_sntprintf(b, SIZEOF(b), _T("%d"), options.showtime);
				logMessage(_T("ShowTime value"), b);
			#endif

			if (options.showtime > 0) //TimeToShow enabled
			{
				if (wParam == 6)
				{
					PostMessage(hwnd, WM_CLOSE, 0, 0);
					#ifdef _DEBUG
						logMessage(_T("Showtime timer"), _T("triggered"));
					#endif
				}
			}
			else
			{
				PostMessage(hwnd, WM_CLOSE, 0, 0);
				if (wParam == 7)
				{
					#ifdef _DEBUG
						logMessage(_T("On Modules Loaded timer"), _T("triggered"));
					#endif
				}
				if (wParam == 8)
				{
					#ifdef _DEBUG
						logMessage(_T("On Modules Loaded workaround"), _T("triggered"));
					#endif
				}
			}

			break;

		case WM_RBUTTONDOWN:
		{
			ShowWindow(hwndSplash, SW_HIDE);
			DestroyWindow(hwndSplash);
			bpreviewruns = false; // preview is stopped.
			break;
		}

		case WM_CLOSE:
		{
			RECT rc; GetWindowRect(hwndSplash, &rc);
			POINT ptDst = { rc.left, rc.top };
			POINT ptSrc = { 0, 0 };
			SIZE sz = { rc.right - rc.left, rc.bottom - rc.top };

			BLENDFUNCTION blend;
			blend.BlendOp =             AC_SRC_OVER;
			blend.BlendFlags =          0;
			blend.SourceConstantAlpha = 255;
			blend.AlphaFormat =         AC_SRC_ALPHA;

			// Fade Out
			if (options.fadeout)
			{
				int i;
				for (i = 255; i>=0; i -= options.fosteps)
				{
					blend.SourceConstantAlpha = i;
					UpdateLayeredWindow(hwndSplash, NULL, &ptDst, &sz, SplashBmp->getDC(), &ptSrc, 0xffffffff, &blend, LWA_ALPHA);
					Sleep(1);
				}
			}
			if (bserviceinvoked) bserviceinvoked = false;
			if (bpreviewruns) bpreviewruns = false;

			DestroyWindow(hwndSplash);
		}

		case WM_MOUSEMOVE:
		{
			if (bserviceinvoked)
				PostMessage(hwnd, WM_CLOSE, 0, 0);
			break;
		}

		case WM_DESTROY:
		{
			PostQuitMessage(0);
			#ifdef _DEBUG
				logMessage(_T("WM_DESTROY"), _T("called"));
			#endif
			break;
		}

		ShowWindow(hwndSplash, SW_HIDE);
		DestroyWindow(hwndSplash);
		break;
	}

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

int SplashThread(void *arg)
{
    IGraphBuilder *pGraph = NULL;
    IMediaControl *pControl = NULL;

	if (options.playsnd)
	{
		// Initialize the COM library.
		CoInitialize(NULL);

		// Create the filter graph manager and query for interfaces.
		CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER, IID_IGraphBuilder, (void **)&pGraph);

		// Get MediaControl Interface
		pGraph->QueryInterface(IID_IMediaControl, (void **)&pControl);

		// Build the graph. IMPORTANT: Change this string to a file on your system.
		pGraph->RenderFile(szSoundFile, NULL);

		// Run the graph.
		pControl->Run();
	}

	WNDCLASSEX wcl;
	wcl.cbSize = sizeof(wcl);
	wcl.lpfnWndProc = SplashWindowProc;
	wcl.style = 0;
	wcl.cbClsExtra = 0;
	wcl.cbWndExtra = 0;
	wcl.hInstance = hInst;
	wcl.hIcon = NULL;
	wcl.hCursor = LoadCursor(NULL, IDC_ARROW);
	wcl.hbrBackground = (HBRUSH)GetStockObject(LTGRAY_BRUSH);
	wcl.lpszMenuName = NULL;
	wcl.lpszClassName = _T(SPLASH_CLASS);
	wcl.hIconSm = NULL;
	RegisterClassEx(&wcl);

	RECT DesktopRect;
	int w = GetSystemMetrics(SM_CXSCREEN);
	int h = GetSystemMetrics(SM_CYSCREEN);
	DesktopRect.left = 0;
	DesktopRect.top = 0;
	DesktopRect.right = w;
	DesktopRect.bottom = h;

	RECT WindowRect;
	WindowRect.left = (DesktopRect.left + DesktopRect.right - SplashBmp->getWidth()) / 2;
	WindowRect.top  = (DesktopRect.top + DesktopRect.bottom - SplashBmp->getHeight()) / 2;
	WindowRect.right = WindowRect.left + SplashBmp->getWidth();
	WindowRect.bottom = WindowRect.top + SplashBmp->getHeight();

	hwndSplash = CreateWindowEx(
		WS_EX_TOOLWINDOW | WS_EX_TOPMOST,//dwStyleEx
		_T(SPLASH_CLASS), //Class name
		NULL, //Title
		DS_SETFONT | DS_FIXEDSYS | WS_POPUP, //dwStyle
		WindowRect.left, // x
		WindowRect.top, // y
		SplashBmp->getWidth(), // Width
		SplashBmp->getHeight(), // Height
		HWND_DESKTOP, //Parent
		NULL, //menu handle
		hInst, //Instance
		NULL);

	RECT rc; GetWindowRect(hwndSplash, &rc);
	POINT ptDst = {rc.left, rc.top};
	POINT ptSrc = {0, 0};
	SIZE sz = {rc.right - rc.left, rc.bottom - rc.top};
	bool splashWithMarkers = false;

	BLENDFUNCTION blend;
	blend.BlendOp =             AC_SRC_OVER;
	blend.BlendFlags =          0;
	blend.SourceConstantAlpha = 0;
	blend.AlphaFormat =         AC_SRC_ALPHA;

	if (options.showversion)
	{
		// locate text markers:
		int i, x = -1, y = -1;

		int splashWidth = SplashBmp->getWidth();
		for (i = 0; i < splashWidth; ++i)
			if (SplashBmp->getRow(0)[i] & 0xFF000000)
		{
			if (x < 0)
			{
				x = i-1; // 1 pixel for marker line
				splashWithMarkers = true;
			} else
			{
				x = -1;
				splashWithMarkers = false;
				break;
			}
		}
		int splashHeight = SplashBmp->getHeight();
		for (i = 0; splashWithMarkers && (i < splashHeight); ++i)
			if(SplashBmp->getRow(i)[0] & 0xFF000000)
			{
				if (y < 0)
				{
					y = i-1; // 1 pixel for marker line
					splashWithMarkers = true;
				} else
				{
					y = -1;
					splashWithMarkers = false;
					break;
				}
			}

			TCHAR verString[256] = {0};
			TCHAR* mirandaVerString = mir_a2t(szVersion);
			mir_sntprintf(verString, SIZEOF(verString), _T("%s%s"), szPrefix, mirandaVerString);
			mir_free(mirandaVerString);
			LOGFONT lf = {0};
			lf.lfHeight = 14;
			_tcscpy_s(lf.lfFaceName, _T("Verdana"));
			SelectObject(SplashBmp->getDC(), CreateFontIndirect(&lf));
			if (!splashWithMarkers)
			{
				SIZE v_sz = {0,0};
				GetTextExtentPoint32(SplashBmp->getDC(), verString, (int)_tcslen(verString), &v_sz);
				x = SplashBmp->getWidth()/2-(v_sz.cx/2);
				y = SplashBmp->getHeight()-(SplashBmp->getHeight()*(100-90)/100);
			}

			SetTextColor(SplashBmp->getDC(), (0xFFFFFFFFUL-SplashBmp->getRow(y)[x])&0x00FFFFFFUL);
			SetBkMode(SplashBmp->getDC(), TRANSPARENT);
			SplashBmp->DrawText(verString, x, y);
	}

	SetWindowLongPtr(hwndSplash, GWL_EXSTYLE, GetWindowLongPtr(hwndSplash, GWL_EXSTYLE) | WS_EX_LAYERED);
	UpdateLayeredWindow(hwndSplash, NULL, &ptDst, &sz, SplashBmp->getDC(), &ptSrc, 0xffffffff, &blend, LWA_ALPHA);

	ShowWindow(hwndSplash, SW_SHOWNORMAL);

	if (options.fadein)
	{
		// Fade in
		int i;
		for (i = 0; i < 255; i += options.fisteps)
		{
			blend.SourceConstantAlpha = i;
			UpdateLayeredWindow(hwndSplash, NULL, &ptDst, &sz, SplashBmp->getDC(), &ptSrc, 0xffffffff, &blend, LWA_ALPHA);
			Sleep(1);
		}
	}
	blend.SourceConstantAlpha = 255;
	UpdateLayeredWindow(hwndSplash, NULL, &ptDst, &sz, SplashBmp->getDC(), &ptSrc, 0xffffffff, &blend, LWA_ALPHA);

	if (DWORD(arg) > 0)
	{
		if (SetTimer(hwndSplash, 6, DWORD(arg), 0))
		{
			#ifdef _DEBUG
				logMessage(_T("Timer TimeToShow"), _T("set"));
			#endif
		}
	}
	else
		if (bmodulesloaded)
		{
			if (SetTimer(hwndSplash, 8, 2000, 0))
			{
				#ifdef _DEBUG
					logMessage(_T("Timer Modules loaded"), _T("set"));
				#endif
			}
		}

	// The Message Pump
	MSG msg;
	while (GetMessage(&msg, NULL, 0, 0) == TRUE) //NULL means every window in the thread; == TRUE means a safe pump.
	{
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}

	if (options.playsnd)
	{
		pControl->Release();
		pGraph->Release();
		CoUninitialize();
	}

	ExitThread(0);
	return 1;
}

BOOL ShowSplash(BOOL bpreview)
{
	if (bpreview && bpreviewruns) return 0;

	if (bpreview) bpreviewruns = true;
	unsigned long timeout = 0;

	SplashBmp = new MyBitmap;

	#ifdef _DEBUG
		logMessage(_T("Loading splash file"), szSplashFile);
	#endif

	SplashBmp->loadFromFile(szSplashFile, NULL);

	DWORD threadID;

	#ifdef _DEBUG
		logMessage(_T("Thread"), _T("start"));
	#endif

	if (bpreview)
	{
		ShowWindow(hwndSplash, SW_HIDE);
		DestroyWindow(hwndSplash);

		timeout = 2000;
		#ifdef _DEBUG
			logMessage(_T("Preview"), _T("yes"));
		#endif
	}
	else
	{
		timeout = options.showtime;
		#ifdef _DEBUG
			TCHAR b [40];
			mir_sntprintf(b, SIZEOF(b), _T("%d"), options.showtime);
			logMessage(_T("Timeout"), b);
		#endif
	}

	hSplashThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)SplashThread, (LPVOID)timeout, 0, &threadID);

	#ifdef _DEBUG
		logMessage(_T("Thread"), _T("end"));
	#endif

	CloseHandle(hSplashThread);
	hSplashThread = NULL;

	return 1;
}