summaryrefslogtreecommitdiff
path: root/plugins/SmileyAdd/src/general.cpp
blob: c0a73d55f75a429bed4cc7fe6f788ddb7ff1bc7f (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
/*
Miranda SmileyAdd Plugin
Copyright (C) 2005 - 2012 Boris Krasnovskiy All Rights Reserved
Copyright (C) 2003 - 2004 Rein-Peter de Boer

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/>.
*/

#include "stdafx.h"

static ULONG_PTR g_gdiplusToken = 0;
static bool gdiPlusFail = false;

//
// General functions
//
int CalculateTextHeight(HDC hdc, CHARFORMAT2 *chf)
{
	HDC hcdc = CreateCompatibleDC(hdc);

	int logPixelsY = GetDeviceCaps(hdc, LOGPIXELSY);
	HFONT hFont = CreateFont(-(chf->yHeight * logPixelsY / 1440), 0, 0, 0,
		chf->wWeight, chf->dwEffects & CFE_ITALIC ? 1 : 0, 0, 0,
		chf->bCharSet, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
		DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE,
		chf->szFaceName);
	SelectObject(hcdc, hFont);

	SIZE fontSize;
	GetTextExtentPoint32(hcdc, L")", 1, &fontSize);

	DeleteObject(hFont);
	DeleteDC(hcdc);

	return fontSize.cy;
}

HICON GetDefaultIcon(bool copy)
{
	HICON resIco = IcoLib_GetIcon("SmileyAdd_ButtonSmiley");
	if (resIco == nullptr)
		resIco = (HICON)LoadImage(g_plugin.getInst(), MAKEINTRESOURCE(IDI_SMILINGICON), IMAGE_ICON, 0, 0, copy ? 0 : LR_SHARED);
	else if (copy) {
		resIco = (HICON)CopyImage(resIco, IMAGE_ICON, 0, 0, 0);
		IcoLib_Release("SmileyAdd_ButtonSmiley");
	}

	return resIco;
}

HICON ImageList_GetIconFixed(HIMAGELIST himl, INT i, UINT fStyle)
{
	ICONINFO ii;
	HICON hIcon;
	HBITMAP hOldDstBitmap;
	HDC hdcDst;

	int cx, cy;
	ImageList_GetIconSize(himl, &cx, &cy);

	hdcDst = CreateCompatibleDC(nullptr);

	ii.fIcon = TRUE;
	ii.xHotspot = 0;
	ii.yHotspot = 0;

	// draw mask
	ii.hbmMask = CreateBitmap(cx, cy, 1, 1, nullptr);
	hOldDstBitmap = (HBITMAP)SelectObject(hdcDst, ii.hbmMask);
	PatBlt(hdcDst, 0, 0, cx, cy, WHITENESS);
	ImageList_Draw(himl, i, hdcDst, 0, 0, fStyle | ILD_MASK);

	// draw image
	ii.hbmColor = CreateBitmap(cx, cy, 1, 32, nullptr);
	SelectObject(hdcDst, ii.hbmColor);
	PatBlt(hdcDst, 0, 0, cx, cy, BLACKNESS);
	ImageList_Draw(himl, i, hdcDst, 0, 0, fStyle | ILD_TRANSPARENT);

	// CreateIconIndirect requires us to deselect the bitmaps from
	// the DCs before calling
	SelectObject(hdcDst, hOldDstBitmap);

	hIcon = CreateIconIndirect(&ii);

	DeleteObject(ii.hbmMask);
	DeleteObject(ii.hbmColor);
	DeleteDC(hdcDst);

	return hIcon;
}

bool InitGdiPlus(void)
{
	Gdiplus::GdiplusStartupInput gdiplusStartupInput;

	static const wchar_t errmsg[] = L"GDI+ not installed.\n"
		L"GDI+ can be downloaded here: http://www.microsoft.com/downloads";

	__try {
		if (g_gdiplusToken == 0 && !gdiPlusFail)
			Gdiplus::GdiplusStartup(&g_gdiplusToken, &gdiplusStartupInput, nullptr);
	}
	__except (EXCEPTION_EXECUTE_HANDLER)
	{
		gdiPlusFail = true;
		ReportError(errmsg);
	}

	return !gdiPlusFail;
}

void DestroyGdiPlus(void)
{
	if (g_gdiplusToken != 0) {
		Gdiplus::GdiplusShutdown(g_gdiplusToken);
		g_gdiplusToken = 0;
	}
}

MCONTACT DecodeMetaContact(MCONTACT hContact)
{
	if (hContact == 0)
		return 0;

	MCONTACT hReal = db_mc_getMostOnline(hContact);
	return (hReal == 0) ? hContact : hReal;
}

bool IsSmileyProto(char *proto)
{
	return proto && mir_strcmp(proto, META_PROTO) && (CallProtoService(proto, PS_GETCAPS, PFLAGNUM_1, 0) & (PF1_IM | PF1_CHAT));
}

void ReportError(const wchar_t *errmsg)
{
	POPUPDATAW ppd = {};
	mir_wstrcpy(ppd.lpwzContactName, L"Miranda SmileyAdd");
	mir_wstrcpy(ppd.lpwzText, errmsg);
	PUAddPopupW(&ppd);
}