summaryrefslogtreecommitdiff
path: root/spamfilter/trunk/utilities.c
blob: 013c80657e3b704add9243e2ada65f100fd5b0b9 (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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
/*

"Spam Filter"-Plugin for Miranda IM

Copyright 2003-2006 Heiko Herkenrath

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 ("SpamFilter-License.txt"); if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/


// -- Includes
#include "common.h"


// -----------------------------------------

// -- Error Output --

static void ThreadMessageBox(void* arg)
{
	MSGBOXPARAMS* pmbp = (MSGBOXPARAMS*)arg;

	MessageBoxIndirect(pmbp);

	mir_free((WCHAR*)pmbp->lpszCaption);
	mir_free((WCHAR*)pmbp->lpszText);
	mir_free(pmbp);
}


void ShowInfoMessage(UINT uIconID, const WCHAR* pszCaption, const WCHAR* pszTextFmt, size_t cchExtraSize, ...)
{
	// Params:
	// uIconID    - NIIF_INFO, NIIF_WARNING or NIIF_ERROR
	// pszCaption - Only 64chars of it will be used.
	// pszTextFmt - Only 256chars of it will be used.

	va_list va;
	WCHAR* pszText;
	BOOL bErrorShown = FALSE;

	if (!pszCaption || !pszTextFmt || Miranda_Terminated()) return;
	pszText = mir_alloc((lstrlen(pszTextFmt)+((cchExtraSize>0)?cchExtraSize:0)+1)*sizeof(WCHAR));
	if (!pszText) return;

	if (cchExtraSize <= 0) {
		mir_sntprintf(pszText, lstrlen(pszTextFmt)+1, _T("%s"), pszTextFmt);
	} else {
		va_start(va, cchExtraSize);
		mir_vsntprintf(pszText, lstrlen(pszTextFmt)+((cchExtraSize>0)?cchExtraSize:0)+1, pszTextFmt, va);
		va_end(va);
	}

	if (ServiceExists(MS_CLIST_SYSTRAY_NOTIFY))
	{
		// Output balloon tip if supported
		MIRANDASYSTRAYNOTIFY msn;

		ZeroMemory(&msn, sizeof(msn)); // forward compatibility
		msn.cbSize = sizeof(msn);
		#if defined(UNICODE)
			// SystrayNotify does not support Unicode
			// We need to convert Unicode->Ansi
			mir_utf8decode(msn.szInfoTitle,&pszCaption);
			mir_utf8decode(msn.szInfo,&pszText);
		#else
			msn.szInfoTitle = (char*)pszCaption;
			msn.szInfo = (char*)pszText;
		#endif
		//msn.szProto = NULL;
		msn.uTimeout = 20000; // 30 sec is current max timeout that is possible
		if (uIconID == NIIF_ERROR)
			msn.dwInfoFlags = NIIF_ERROR;
		else if (uIconID == NIIF_WARNING)
			msn.dwInfoFlags = NIIF_WARNING;
		else
			msn.dwInfoFlags = NIIF_INFO;

		// I don't know if service is thread safe therefor using sync
		bErrorShown = (CallServiceSync(MS_CLIST_SYSTRAY_NOTIFY, 0, (LPARAM)&msn) == 0);
		
		#if defined(UNICODE)
			if (msn.szInfoTitle) mir_free(msn.szInfoTitle);
			if (msn.szInfo) mir_free(msn.szInfo);
		#endif


	#if defined(UNICODE)
	} else if (ServiceExists(MS_POPUP_ADDPOPUPW)) 
	{
		// Output a popup message as alternative
		POPUPDATAW ppd;

		ZeroMemory(&ppd, sizeof(ppd));
		mir_sntprintf(ppd.lpwzContactName, ARRAYSIZE(ppd.lpwzContactName), _T("%s"), pszCaption);
		mir_sntprintf(ppd.lpwzText, ARRAYSIZE(ppd.lpwzText), _T("%s"), pszText);
		//ppd.iSeconds = 0;
		//ppd.lchContact = NULL;
		//ppd.colorText = GetSysColor(COLOR_INFOTEXT);	// or GetSysColor(COLOR_WINDOWTEXT)
		//ppd.colorBack = GetSysColor(COLOR_INFOBK);	// or GetSysColor(COLOR_3DFACE)
		if (uIconID == NIIF_WARNING)
			ppd.lchIcon = (HICON)LoadImage(NULL, MAKEINTRESOURCE(IDI_WARNING), IMAGE_ICON, GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), LR_SHARED);
		else if (uIconID == NIIF_ERROR)
			ppd.lchIcon = (HICON)LoadImage(NULL, MAKEINTRESOURCE(IDI_ERROR), IMAGE_ICON, GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), LR_SHARED);
		else
			ppd.lchIcon = (HICON)LoadImage(NULL, MAKEINTRESOURCE(IDI_INFORMATION), IMAGE_ICON, GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), LR_SHARED);

		// I don't know if popup module author made it thread safe...
		bErrorShown = (CallServiceSync(MS_POPUP_ADDPOPUPW, (WPARAM)&ppd, 0) >= 0);
	}
	#else
	} else if (ServiceExists(MS_POPUP_ADDPOPUPEX)) 
	{
		// Output a popup message as alternative
		POPUPDATAEX ppd;

		ZeroMemory(&ppd, sizeof(ppd));
		mir_snprintf(ppd.lpzContactName, ARRAYSIZE(ppd.lpzContactName), "%s", pszCaption);
		mir_snprintf(ppd.lpzText, ARRAYSIZE(ppd.lpzText), "%s", pszText);
		//ppd.iSeconds = 0;
		//ppd.lchContact = NULL;
		//ppd.colorText = GetSysColor(COLOR_INFOTEXT);	// or GetSysColor(COLOR_WINDOWTEXT)
		//ppd.colorBack = GetSysColor(COLOR_INFOBK);	// or GetSysColor(COLOR_3DFACE)

		if (ServiceExists(MS_POPUP_ADDCLASS)) {
			//ppd.skinBack = ppd.colorBack;
			ppd.colorBack = POPUP_USE_SKINNED_BG;
		}

		if (uIconID == NIIF_WARNING) {
			ppd.lchIcon = (HICON)LoadImage(NULL, MAKEINTRESOURCE(IDI_WARNING), IMAGE_ICON, GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), LR_SHARED);

			if (ppd.colorBack == POPUP_USE_SKINNED_BG)
				ppd.lpzClass = (LPCTSTR)POPUP_CLASS_NOTIFY; // type "LPCTSTR" of lpzClass is wrong!
			
		} else if (uIconID == NIIF_ERROR) {
			ppd.lchIcon = (HICON)LoadImage(NULL, MAKEINTRESOURCE(IDI_ERROR), IMAGE_ICON, GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), LR_SHARED);
				
			if (ppd.colorBack == POPUP_USE_SKINNED_BG)
				ppd.lpzClass = (LPCTSTR)POPUP_CLASS_WARNING; // type "LPCTSTR" of lpzClass is wrong!
			
		} else {
			ppd.lchIcon = (HICON)LoadImage(NULL, MAKEINTRESOURCE(IDI_INFORMATION), IMAGE_ICON, GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), LR_SHARED);
				
			if (ppd.colorBack == POPUP_USE_SKINNED_BG)
				ppd.lpzClass = (LPCTSTR)POPUP_CLASS_DEFAULT; // type "LPCTSTR" of lpzClass is wrong!
		}

		bErrorShown = (CallServiceSync(MS_POPUP_ADDPOPUPEX, (WPARAM)&ppd, 0) >= 0); // I don't know if popup module author made it thread safe...
	}
	#endif

	if (!bErrorShown)
	{
		// Output normal message dialog when popup plugin is not installed
		// or when Windows doesn't support balloon tips
		// or if an popup error occured

		MSGBOXPARAMS* pmbp = (MSGBOXPARAMS*)mir_alloc(sizeof(MSGBOXPARAMS));
		if (pmbp)
		{
			ZeroMemory(pmbp, sizeof(MSGBOXPARAMS));
			pmbp->cbSize = sizeof(MSGBOXPARAMS);
			pmbp->lpszCaption = mir_wstrdup(pszCaption); // will be freed by thread
			pmbp->lpszText = pszText; // will be freed by thread
			pszText = NULL;
			pmbp->hwndOwner = NULL;
			pmbp->dwStyle = MB_OK|MB_SETFOREGROUND|MB_TASKMODAL;
			pmbp->dwLanguageId = LANGIDFROMLCID(GetThreadLocale());
			if (uIconID == NIIF_WARNING)
				pmbp->dwStyle |= MB_ICONWARNING;
			else if (uIconID == NIIF_ERROR)
				pmbp->dwStyle |= MB_ICONERROR;
			else
				pmbp->dwStyle |= MB_ICONINFORMATION;		

			forkthread(ThreadMessageBox, 0, pmbp);
		}
	}

	if (pszText) mir_free(pszText);
}


WCHAR* GetLastErrorDescription(DWORD dwLastError)
{
	WCHAR* pszReturn;

	#if defined(UNICODE)

		#if defined(MICROSOFT_LAYER_FOR_UNICODE)
			char* pszLastError = NULL;

			// FormatMessageW does not work with UnicoWS layer on Win9x/ME 
			if (FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM, NULL, dwLastError, 0, (LPSTR)&pszLastError, 0, NULL) != 0) {
				pszReturn = mir_utf8encodeW(pszLastError);
				LocalFree(pszLastError);
			} else {
				pszReturn = NULL;
			}

		#else
			WCHAR* pwszLastError;
			if (FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM, NULL, dwLastError, 0, (LPWSTR)&pwszLastError, 0, NULL) != 0) {
				pszReturn = mir_wstrdup(pwszLastError);
				LocalFree(pwszLastError);
			} else {
				pszReturn = NULL;
			}
		#endif

	#else
		WCHAR* pszLastError;
		if (FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM, NULL, dwLastError, 0, (LPTSTR)&pszLastError, 0, NULL) != 0) {
			pszReturn = mir_strdup(pszLastError);
			LocalFree(pszLastError);
		} else {
			pszReturn = NULL;
		}
	#endif

	return pszReturn;
}


// -- Sounds --

void SkinAddNewSoundBundled(const char* pszName, const char* pszSection, const char* pszDescription, const WCHAR* pszDefaultFile)
{
	SKINSOUNDDESCEX ssd;
	WCHAR szFileName[MAX_PATH];
	char szFileNameFin[MAX_PATH];

	ZeroMemory(&ssd, sizeof(ssd));
	ssd.cbSize = sizeof(ssd);
	ssd.pszSection = (char*)pszSection;
	ssd.pszName = (char*)pszName;
	ssd.pszDescription = (char*)pszDescription;

	PConstructLocalPath(szFileName, CSIDL_EXT_EXECUTABLE, SOUNDS_SUBDIRECTORY, (WCHAR*)pszDefaultFile, NULL);
	if (PathFileExists(szFileName))
	{
		#if defined(UNICODE)
		WCHAR* pszBuf;
			mir_utf8decode((char*)szFileName,&pszBuf);
			if (pszBuf) {
				if (CallService(MS_UTILS_PATHTORELATIVE, (WPARAM)pszBuf, (LPARAM)szFileNameFin) <= 0)
					mir_snprintf(szFileNameFin, ARRAYSIZE(szFileNameFin), "%s", pszBuf);
				ssd.pszDefaultFile = szFileNameFin;
				mir_free(pszBuf);
			}
		#else
			if (CallService(MS_UTILS_PATHTORELATIVE, (WPARAM)szFileName, (LPARAM)szFileNameFin) <= 0)
				ssd.pszDefaultFile = szFileName;
			else
				ssd.pszDefaultFile = szFileNameFin;
		#endif
	}

	CallService(MS_SKIN_ADDNEWSOUND, 0, (LPARAM)&ssd);
}


// -- Icons --

void SkinAddNewIcon(const char* pszName, const WCHAR* pszSection, const WCHAR* pszDescription, int iDefResID, BOOL bLarge)
{
	// Support for IcoLib
	if (ServiceExists(MS_SKIN2_ADDICON) && ServiceExists(MS_SKIN2_GETICON))
	{
		SKINICONDESC sid;

		ZeroMemory(&sid, sizeof(sid));
		sid.cbSize = sizeof(sid);
		sid.ptszSection = (WCHAR*)pszSection;
		sid.ptszDescription = (WCHAR*)pszDescription;
		sid.pszName = (char*)pszName;
		sid.hDefaultIcon = (HICON)LoadImage(hInstance, MAKEINTRESOURCE(iDefResID), IMAGE_ICON, bLarge?0:GetSystemMetrics(SM_CXSMICON), bLarge?0:GetSystemMetrics(SM_CYSMICON), (bLarge?LR_DEFAULTSIZE:0)|LR_SHARED);
		sid.cx = GetSystemMetrics(bLarge?SM_CXICON:SM_CXSMICON);
		sid.cy = GetSystemMetrics(bLarge?SM_CYICON:SM_CYSMICON);
		sid.flags = SIDF_UNICODE;

		CallService(MS_SKIN2_ADDICON, 0, (LPARAM)&sid);
	}
}


HICON SkinGetIcon(const char* pszName, int iDefResID, BOOL bLarge)
{
	// Support for IcoLib
	if (ServiceExists(MS_SKIN2_GETICON) && ServiceExists(MS_SKIN2_ADDICON)) {
		HICON hIcon = (HICON)CallService(MS_SKIN2_GETICON, 0, (LPARAM)pszName);
		if (hIcon) return hIcon;
	}
	if (iDefResID <= 0) return NULL;
	return (HICON)LoadImage(hInstance, MAKEINTRESOURCE(iDefResID), IMAGE_ICON, bLarge?0:GetSystemMetrics(SM_CXSMICON), bLarge?0:GetSystemMetrics(SM_CYSMICON), (bLarge?LR_DEFAULTSIZE:0)|LR_SHARED);
}


// -- Hotkeys --

/*
void SkinAddNewHotkey(const char* pszName, const char* pszSection, const char* pszDescription, const int iDefHotkey, const char* pszServiceName)
{
	// Support for CList_MW
	if (ServiceExists(MS_SKIN_ADDHOTKEY))
	{
		SKINHOTKEYDESCEX shd;

		ZeroMemory(&shd, sizeof(shd));
		shd.cbSize = sizeof(shd);
		shd.pszName = (char*)pszName;
		shd.pszDescription = (char*)pszDescription;
		shd.pszSection = (char*)pszSection;
		shd.pszService = (char*)pszServiceName;
		shd.DefHotKey = iDefHotkey;

		CallService(MS_SKIN_ADDHOTKEY, 0, (LPARAM)&shd);
	}
}
*/


// -- Fonts --

/*
void SkinAddNewFont(const char* pszName, const char* pszSection, const char* pszDescription, COLORREF clrColor, const char* pszFace, int iSize, DWORD dwStyleFlags)
{
	if (pszName && ServiceExists(MS_FONT_REGISTER))
	{
		FontID fid;

		ZeroMemory(&fid, sizeof(fid));
		fid.cbSize = sizeof(fid);
		mir_snprintf(fid.name, ARRAYSIZE(fid.name), "%s", pszName);
		mir_snprintf(fid.group, ARRAYSIZE(fid.group), "%s", pszSection ? pszSection : Translate("Other"));
		mir_snprintf(fid.dbSettingsGroup, ARRAYSIZE(fid.dbSettingsGroup), "%s", DB_MODULE_NAME);
		mir_snprintf(fid.prefix, ARRAYSIZE(fid.prefix), "%s", "Font_");
		//fid.order = 0;
		fid.flags = FIDF_DEFAULTVALID|FIDF_ALLOWEFFECTS;
		fid.deffontsettings.charset = DEFAULT_CHARSET;
		fid.deffontsettings.colour = clrColor;
		fid.deffontsettings.size = (iSize > 0)?iSize:12;
		fid.deffontsettings.style = dwStyleFlags;
		mir_snprintf(fid.deffontsettings.szFace, ARRAYSIZE(fid.deffontsettings.szFace), "%s", pszFace ? pszFace : "MS Sans Serif");

		CallService(MS_FONT_REGISTER, (WPARAM)&fid, 0);
	}
}


BOOL SkinAddNewFontSameAs(const char* pszDescription, const char* pszSection, const char* pszSameAsDescription)
{
	if (ServiceExists(MS_FONT_REGISTER))
	{
		FontID fid;
		LOGFONT lf;
		
		ZeroMemory(&fid, sizeof(fid));
 		fid.cbSize = sizeof(fid);
		mir_snprintf(fid.name, ARRAYSIZE(fid.name), "%s", pszDescription);
		mir_snprintf(fid.group, ARRAYSIZE(fid.group), "%s", pszSection ? pszSection : Translate("Other"));
		mir_snprintf(fid.dbSettingsGroup, ARRAYSIZE(fid.dbSettingsGroup), "%s", DB_MODULE_NAME);
		mir_snprintf(fid.prefix, ARRAYSIZE(fid.name), "%s", "Font_");
		//fid.order = 0;
		fid.flags = FIDF_ALLOWEFFECTS;
		if (pszSameAsDescription)
		{ 
			ZeroMemory(&lf, sizeof(lf)); // not good; SkinGetFont has no error indicating value (workaround)

			fid.deffontsettings.colour = SkinGetFont(pszSameAsDescription, &lf);
			fid.deffontsettings.charset = lf.lfCharSet;
			fid.deffontsettings.size = (char)lf.lfHeight;
			fid.deffontsettings.style = 0;
			
			mir_snprintf(fid.deffontsettings.szFace, ARRAYSIZE(fid.deffontsettings.szFace), "%s", lf.lfFaceName);

			if (lf.lfItalic) fid.deffontsettings.style = DBFONTF_ITALIC;
			if (lf.lfWeight) fid.deffontsettings.style = DBFONTF_BOLD;
			if (lf.lfUnderline) fid.deffontsettings.style = DBFONTF_UNDERLINE;
			if (lf.lfStrikeOut) fid.deffontsettings.style = DBFONTF_STRIKEOUT;

			fid.flags |= FIDF_DEFAULTVALID;

			CallService(MS_FONT_REGISTER, (WPARAM)&fid, 0);
			return TRUE;
		} else {
			CallService(MS_FONT_REGISTER, (WPARAM)&fid, 0);
			return FALSE;
		}
	}
}


BOOL SkinGetFont(const char* pszDescription, const char* pszSection, COLORREF* pclrColor, LOGFONT* plfFont)
{
	if (pszName && pclrColor && plfFont && ServiceExists(MS_FONT_GET))
	{
		FontID fid;
		
		mir_snprintf(fid.name, 64, "%s", pszDescription);
		mir_snprintf(fid.group, 64, "%s", pszSection ? pszSection : Translate("Other"));

	    *pclrColor = (COLORREF)CallService(MS_FONT_GET, (WPARAM)&fid, (LPARAM)plfFont);
		
		return TRUE;
	} else {
		return FALSE;
	}
}
*/


// -- Colors --

void SkinAddNewColor(const char* pszDbName, const char* pszSection, const char* pszDescription, COLORREF clrDefColor, BOOL bFontRelated)
{
	if (!pszSection || (pszDbName && !pszDescription)) return;

	// Support for FontService
	if (bFontRelated && ServiceExists(MS_COLOUR_REGISTER) && ServiceExists(MS_COLOUR_GET))
	{
		ColourID cid;
		
		ZeroMemory(&cid, sizeof(cid));
		cid.cbSize = sizeof(cid);
		cid.defcolour = clrDefColor; 
		mir_snprintf(cid.dbSettingsGroup, ARRAYSIZE(cid.dbSettingsGroup), "%s", DB_MODULE_NAME);
		mir_snprintf(cid.group, ARRAYSIZE(cid.group), "%s", pszSection);
		mir_snprintf(cid.setting, ARRAYSIZE(cid.setting), "%s", pszDbName?pszDbName:"BkgColour");
		mir_snprintf(cid.name, ARRAYSIZE(cid.name), "%s", pszDbName?pszDescription:Translate("Background"));
			
		CallService(MS_COLOUR_REGISTER, (WPARAM)&cid, 0);
		return;
	}
	
}


COLORREF SkinGetColor(const char* pszDbName, const char* pszDescription, const char* pszSection, COLORREF clrDefColor, BOOL bFontRelated)
{
	if (!pszSection || (pszDbName && !pszDescription)) return clrDefColor;

	// Support for FontService
	if (bFontRelated && ServiceExists(MS_COLOUR_REGISTER) && ServiceExists(MS_COLOUR_GET))
	{
		ColourID cid;

		ZeroMemory(&cid, sizeof(cid));
		cid.cbSize = sizeof(cid);
		mir_snprintf(cid.setting, ARRAYSIZE(cid.setting), "%s", pszDbName?pszDbName:"BkgColour");
		mir_snprintf(cid.group, ARRAYSIZE(cid.group), "%s", pszSection);
		mir_snprintf(cid.name, ARRAYSIZE(cid.name), "%s", pszDescription?(char*)pszDescription:Translate("Background"));
			
		return (COLORREF)CallService(MS_COLOUR_GET, (WPARAM)&cid, 0);
	}

	return clrDefColor;
}


// -- System Icons --

HIMAGELIST LoadSystemCheckboxImageList(void)
{
	HWND hwndTreeView;
	HIMAGELIST hImageList, hImgLstBuf;
	INITCOMMONCONTROLSEX icc;

	ZeroMemory(&icc, sizeof(icc));
	icc.dwSize = sizeof(icc);
	icc.dwICC = ICC_TREEVIEW_CLASSES;
	InitCommonControlsEx(&icc);

	// Create hidden treeview
    hwndTreeView = CreateWindowEx(0, WC_TREEVIEW, NULL, 0, 0, 0, 0, 0, NULL, 0, hInstance, NULL);
	if (!hwndTreeView) return NULL;
	SetWindowLongPtr(hwndTreeView, GWL_STYLE, (LONG_PTR)TVS_CHECKBOXES); // Style needs to be set after creating

	// Get imagelist
	hImgLstBuf = TreeView_GetImageList(hwndTreeView, TVSIL_STATE);
	hImageList = ImageList_Duplicate(hImgLstBuf);

	// Destroy treeview
	TreeView_SetImageList(hwndTreeView, NULL, TVSIL_STATE); // Avoiding Access Violation in CommonControls DLL
	ImageList_Destroy(hImgLstBuf);
	DestroyWindow(hwndTreeView);

	return hImageList;
}


HICON LoadSystemIcon(int iListID, int iIconIndex) // Returned icon needs to be destroyed using DestroyIcon()
{
/*
Index values for IDB_HIST_LARGE_COLOR and IDB_HIST_SMALL_COLOR:
(Microsoft Windows Explorer bitmaps)
HIST_ADDTOFAVORITES 
Add to favorites.
HIST_BACK 
Move back.
HIST_FAVORITES 
Open favorites folder.
HIST_FORWARD 
Move forward.
HIST_VIEWTREE 
View tree.
 
Index values for IDB_STD_LARGE_COLOR and IDB_STD_SMALL_COLOR:
(Standard bitmaps)
STD_COPY 
Copy operation.
STD_CUT 
Cut operation.
STD_DELETE 
Delete operation.
STD_FILENEW 
New file operation.
STD_FILEOPEN 
Open file operation.
STD_FILESAVE 
Save file operation.
STD_FIND 
Find operation.
STD_HELP 
Help operation.
STD_PASTE 
Paste operation.
STD_PRINT 
Print operation.
STD_PRINTPRE 
Print preview operation.
STD_PROPERTIES 
Properties operation.
STD_REDOW 
Redo operation. 
STD_REPLACE 
Replace operation.
STD_UNDO 
Undo operation.
 
Index values for IDB_VIEW_LARGE_COLOR and IDB_VIEW_SMALL_COLOR:
(View bitmaps)
VIEW_DETAILS 
Details view.
VIEW_LARGEICONS 
Large icons view.
VIEW_LIST 
List view.
VIEW_NETCONNECT 
Connect to network drive.
VIEW_NETDISCONNECT 
Disconnect from network drive.
VIEW_NEWFOLDER 
New folder.
VIEW_PARENTFOLDER 
Go to parent folder.
VIEW_SMALLICONS 
Small icon view.
VIEW_SORTDATE 
Sort by date.
VIEW_SORTNAME 
Sort by name.
VIEW_SORTSIZE 
Sort by size.
VIEW_SORTTYPE 
Sort by type.
*/

	HWND hwndToolbar;
	HICON hIcon;
	INITCOMMONCONTROLSEX icc;
		
	ZeroMemory(&icc, sizeof(icc));
	icc.dwSize = sizeof(icc);
	icc.dwICC = ICC_BAR_CLASSES;
	InitCommonControlsEx(&icc);
	
	// Create hidden toolbar
	hwndToolbar = CreateWindowEx(0, TOOLBARCLASSNAME, _T(""), 0, 0, 0, 0, 0, NULL, 0, hInstance, NULL);
	if (!hwndToolbar) return NULL;
	SendMessage(hwndToolbar, TB_BUTTONSTRUCTSIZE, sizeof(TBBUTTON), 0);  

	// Get icon
	SendMessage(hwndToolbar, TB_LOADIMAGES, (WPARAM)iListID, (LPARAM)HINST_COMMCTRL);
	hIcon = ImageList_GetIcon((HIMAGELIST)SendMessage(hwndToolbar, TB_GETIMAGELIST, 0, 0), iIconIndex, ILD_TRANSPARENT);

	// Destroy toolbar
	DestroyWindow(hwndToolbar);

	return hIcon;
}


// -- Icon Overlay --

HICON OverlayIcon(HICON hMainIcon, HICON hOverlayIcon, int cxWidth, int cyHeight)
{ 
	HICON hIconReturn;
    HIMAGELIST himlIconList, himlIconListBuf; // New image lists
	int iMainIcon, iOverlayIcon;
 
	if ((cxWidth < 0) || (cyHeight < 0) || !hMainIcon || !hOverlayIcon)
		return NULL;

    himlIconList = ImageList_Create((cxWidth==0)?GetSystemMetrics(SM_CXICON):cxWidth, (cyHeight==0)?GetSystemMetrics(SM_CYICON):cyHeight, (IsWinVerXPPlus()?ILC_COLOR32:ILC_COLOR16)|ILC_MASK,2, 0); 
    iMainIcon = ImageList_AddIcon(himlIconList, hMainIcon); 
    iOverlayIcon = ImageList_AddIcon(himlIconList, hOverlayIcon); 
	
    himlIconListBuf = ImageList_Merge(himlIconList, iMainIcon, himlIconList, iOverlayIcon, 0, 0);
	ImageList_Destroy(himlIconList);
	
	hIconReturn = ImageList_GetIcon(himlIconListBuf, iMainIcon, ILD_TRANSPARENT);
 	ImageList_Destroy(himlIconListBuf);

	return hIconReturn; // returned HICON needs to be destroyed (DestroyIcon()) 
}