summaryrefslogtreecommitdiff
path: root/plugins/TabSRMM/src/ImageDataObject.cpp
blob: 095552739852d7b0d6c8b60ee7a57c92f3129b70 (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
/////////////////////////////////////////////////////////////////////////////////////////
// Miranda NG: the free IM client for Microsoft* Windows*
//
// Copyright (c) 2012-14 Miranda NG project,
// Copyright (c) 2000-09 Miranda ICQ/IM project,
// all portions of this codebase are copyrighted to the people
// listed in contributors.txt.
//
// 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.
//
// part of tabSRMM messaging plugin for Miranda.
//
// (C) 2005-2010 by silvercircle _at_ gmail _dot_ com and contributors
//
// inserts a bitmap into a rich edit control using OLE
// code partially taken from public example on the internet, source unknown.
//
// originally part of the smileyadd plugin for Miranda NG

#include "commonheaders.h"

extern void ReleaseRichEditOle(IRichEditOle *ole)
{
	ole->Release();
}

extern void ImageDataInsertBitmap(IRichEditOle *ole, HBITMAP hBm)
{
	CImageDataObject::InsertBitmap(ole, hBm);
}

int CacheIconToBMP(TLogIcon *theIcon, HICON hIcon, COLORREF backgroundColor, int sizeX, int sizeY)
{
	int IconSizeX = sizeX;
	int IconSizeY = sizeY;

	if ((IconSizeX == 0) || (IconSizeY == 0)) {
		Utils::getIconSize(hIcon, IconSizeX, IconSizeY);
		if (sizeX != 0)
			IconSizeX = sizeX;
		if (sizeY != 0)
			IconSizeY = sizeY;
	}
	RECT rc;
	BITMAPINFOHEADER bih = {0};
	int widthBytes;
	theIcon->hBkgBrush = CreateSolidBrush(backgroundColor);
	bih.biSize = sizeof(bih);
	bih.biBitCount = 24;
	bih.biPlanes = 1;
	bih.biCompression = BI_RGB;
	bih.biHeight = IconSizeY;
	bih.biWidth = IconSizeX;
	widthBytes = ((bih.biWidth * bih.biBitCount + 31) >> 5) * 4;
	rc.top = rc.left = 0;
	rc.right = bih.biWidth;
	rc.bottom = bih.biHeight;
	theIcon->hdc = GetDC(0);
	theIcon->hBmp = CreateCompatibleBitmap(theIcon->hdc, bih.biWidth, bih.biHeight);
	theIcon->hdcMem = CreateCompatibleDC(theIcon->hdc);
	theIcon->hoBmp = (HBITMAP)SelectObject(theIcon->hdcMem, theIcon->hBmp);
	FillRect(theIcon->hdcMem, &rc, theIcon->hBkgBrush);
	DrawIconEx(theIcon->hdcMem, 0, 0, hIcon, bih.biWidth, bih.biHeight, 0, NULL, DI_NORMAL);
	SelectObject(theIcon->hdcMem, theIcon->hoBmp);
	return TRUE;
}

void DeleteCachedIcon(TLogIcon *theIcon)
{
	DeleteDC(theIcon->hdcMem);
	DeleteObject(theIcon->hBmp);
	ReleaseDC(NULL, theIcon->hdc);
	DeleteObject(theIcon->hBkgBrush);
}

// returns true on success, false on failure
bool CImageDataObject::InsertBitmap(IRichEditOle* pRichEditOle, HBITMAP hBitmap)
{
	SCODE sc;
	BITMAP bminfo;

	// Get the image data object
	//
	CImageDataObject *pods = new CImageDataObject;
	LPDATAOBJECT lpDataObject;
	pods->QueryInterface(IID_IDataObject, (void **)&lpDataObject);

	GetObject(hBitmap, sizeof(bminfo), &bminfo);
	pods->SetBitmap(hBitmap);

	// Get the RichEdit container site
	//
	IOleClientSite *pOleClientSite;
	pRichEditOle->GetClientSite(&pOleClientSite);

	// Initialize a Storage Object
	//
	IStorage *pStorage;

	LPLOCKBYTES lpLockBytes = NULL;
	sc = ::CreateILockBytesOnHGlobal(NULL, TRUE, &lpLockBytes);
	if (sc != S_OK) {
		pOleClientSite->Release();
		return false;
	}
	sc = ::StgCreateDocfileOnILockBytes(lpLockBytes,
										STGM_SHARE_EXCLUSIVE | STGM_CREATE | STGM_READWRITE, 0, &pStorage);
	if (sc != S_OK) {
		lpLockBytes = NULL;
		pOleClientSite->Release();
		return false;
	}
	// The final ole object which will be inserted in the richedit control
	//
	IOleObject *pOleObject;
	pOleObject = pods->GetOleObject(pOleClientSite, pStorage);
	if (pOleObject == NULL) {
		pStorage->Release();
		pOleClientSite->Release();
		return false;
	}

	// all items are "contained" -- this makes our reference to this object
	//  weak -- which is needed for links to embedding silent update.
	OleSetContainedObject(pOleObject, TRUE);

	// Now Add the object to the RichEdit
	//
	REOBJECT reobject;
	memset(&reobject, 0, sizeof(REOBJECT));
	reobject.cbStruct = sizeof(REOBJECT);

	CLSID clsid;
	sc = pOleObject->GetUserClassID(&clsid);
	if (sc != S_OK) {
		pOleObject->Release();
		pStorage->Release();
		pOleClientSite->Release();
		return false;
	}

	reobject.clsid = clsid;
	reobject.cp = REO_CP_SELECTION ;
	reobject.dvaspect = DVASPECT_CONTENT;
	reobject.poleobj = pOleObject;
	reobject.polesite = pOleClientSite;
	reobject.pstg = pStorage;
	reobject.dwFlags = bminfo.bmHeight <= 12 ? 0 : REO_BELOWBASELINE;

	// Insert the bitmap at the current location in the richedit control
	//
	sc = pRichEditOle->InsertObject(&reobject);

	// Release all unnecessary interfaces
	//
	pOleObject->Release();
	pOleClientSite->Release();
	lpLockBytes->Release();
	pStorage->Release();
	lpDataObject->Release();
	if (sc != S_OK)
		return false;
	else
		return true;
}


void CImageDataObject::SetBitmap(HBITMAP hBitmap)
{
	STGMEDIUM stgm;
	stgm.tymed = TYMED_GDI;                 // Storage medium = HBITMAP handle
	stgm.hBitmap = hBitmap;
	stgm.pUnkForRelease = NULL;       // Use ReleaseStgMedium

	FORMATETC fm;
	fm.cfFormat = CF_BITMAP;                // Clipboard format = CF_BITMAP
	fm.ptd = NULL;                              // Target Device = Screen
	fm.dwAspect = DVASPECT_CONTENT; // Level of detail = Full content
	fm.lindex = -1;                             // Index = Not applicaple
	fm.tymed = TYMED_GDI;                     // Storage medium = HBITMAP handle

	this->SetData(&fm, &stgm, TRUE);
}


IOleObject *CImageDataObject::GetOleObject(IOleClientSite *pOleClientSite, IStorage *pStorage)
{
	SCODE sc;
	IOleObject *pOleObject;
	sc = ::OleCreateStaticFromData(this, IID_IOleObject, OLERENDER_FORMAT,
								   &m_format, pOleClientSite, pStorage, (void **) & pOleObject);
	if (sc != S_OK)
		pOleObject = NULL;
	return pOleObject;
}