summaryrefslogtreecommitdiff
path: root/plugins/SmileyAdd/src/SmileyBase.cpp
blob: badb6d43e8738e6abeb306ba560e5d912bee085f (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
/*
Miranda SmileyAdd Plugin
Copyright (C) 2008 - 2011 Boris Krasnovskiy All Rights Reserved

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 "general.h"

static void HiMetricToPixel(const SIZEL * lpSizeInHiMetric, LPSIZEL lpSizeInPix)
{
	HDC hDCScreen = GetDC(NULL);
	const int nPixelsPerInchX = GetDeviceCaps(hDCScreen, LOGPIXELSX);
	const int nPixelsPerInchY = GetDeviceCaps(hDCScreen, LOGPIXELSY);
	ReleaseDC(NULL, hDCScreen);

	lpSizeInPix->cx = (lpSizeInHiMetric->cx * nPixelsPerInchX + (2540/2)) / 2540;
	lpSizeInPix->cy = (lpSizeInHiMetric->cy * nPixelsPerInchY + (2540/2)) / 2540;
}

static int CompareISmileyBase(const ISmileyBase* p1, const ISmileyBase* p2)
{
	return (int)((char*)p2 - (char*)p1);
}

static LIST<ISmileyBase> regSmileys(10, CompareISmileyBase);

// {105C56DF-6455-4705-A501-51F1CCFCF688}
const GUID IID_ISmileyAddSmiley = 
{ 0x105c56df, 0x6455, 0x4705, { 0xa5, 0x1, 0x51, 0xf1, 0xcc, 0xfc, 0xf6, 0x88 } };

// {58B32D03-1BD2-4840-992E-9AE799FD4ADE}
const GUID IID_ITooltipData = 
{ 0x58b32d03, 0x1bd2, 0x4840, { 0x99, 0x2e, 0x9a, 0xe7, 0x99, 0xfd, 0x4a, 0xde } };

ISmileyBase::ISmileyBase(void)
{
	m_spAdviseSink = NULL;
	m_spClientSite = NULL;
	m_spAdviseHolder = NULL;
	m_lRefCount = 1;
	m_advf = 0;
	m_smltxt = NULL;
	m_hwnd = NULL;
	m_visible = false;
	m_dirAniAllow = false;

	memset(&m_sizeExtent, 0, sizeof(m_sizeExtent));
	memset(&m_sizeExtentHiM, 0, sizeof(m_sizeExtentHiM));
	memset(&m_orect, 0, sizeof(m_orect));

	regSmileys.insert(this);
}

ISmileyBase::~ISmileyBase(void)
{
	free(m_smltxt);

	Close(OLECLOSE_NOSAVE);

	if (m_spClientSite) 
	{
		m_spClientSite->Release();
		m_spClientSite = NULL;
	}
	if (m_spAdviseHolder)
	{
		m_spAdviseHolder->Release();
		m_spAdviseHolder = NULL;
	}
}

void ISmileyBase::OnClose(void)
{
	if (m_spAdviseHolder) m_spAdviseHolder->SendOnClose();
}

void ISmileyBase::SendOnViewChange(void) 
{
	if (m_spAdviseSink) m_spAdviseSink->OnViewChange(DVASPECT_CONTENT, -1);
	if (m_advf & ADVF_ONLYONCE) 
	{
		m_spAdviseSink->Release();
		m_spAdviseSink = NULL;
		m_advf = 0;
	}
}

bool ISmileyBase::QueryHitPointSpecial(int x, int y, HWND hwnd, TCHAR** smltxt)
{
	bool result = m_visible && m_hwnd == hwnd;
	if (result)
	{
		result = x >= m_orect.left && x <= m_orect.right &&
			y >= m_orect.top && y <= m_orect.bottom;
	}
	if (result) *smltxt = m_smltxt;
	return result;
}

void ISmileyBase::SetHint(TCHAR* smltxt)
{
	m_smltxt = _tcsdup(smltxt);
}


void ISmileyBase::SetPosition(HWND hwnd, LPCRECT lpRect)
{
	m_hwnd = hwnd;
	if (lpRect == NULL || (lpRect->top == -1 && lpRect->bottom == -1))
	{
		m_visible = false;
	} 
	else
	{
		m_visible = true;
		m_dirAniAllow = true;
		m_orect.left = lpRect->left;
		m_orect.right = lpRect->left + m_sizeExtent.cx;
		if (lpRect->top == -1)
		{
			m_orect.top = lpRect->bottom - m_sizeExtent.cy;
			m_orect.bottom = lpRect->bottom;
		}
		else if (lpRect->bottom == -1)
		{
			m_orect.top = lpRect->top;
			m_orect.bottom = lpRect->top + m_sizeExtent.cy;;
		}
		else
		{
			m_orect.top = lpRect->bottom - m_sizeExtent.cy;
			m_orect.bottom = lpRect->bottom;
		}
	}
}


//
// IUnknown members
//
ULONG ISmileyBase::AddRef(void) 
{ return InterlockedIncrement(&m_lRefCount); }

ULONG ISmileyBase::Release(void)
{
	LONG count = InterlockedDecrement(&m_lRefCount);
	if(count == 0) 
		delete this;
	return count;
}


HRESULT ISmileyBase::QueryInterface(REFIID iid, void ** ppvObject)
{
	// check to see what interface has been requested
	if (ppvObject == NULL) return E_POINTER;
	if (iid == IID_ISmileyAddSmiley)
		*ppvObject = this;
	else if (iid == IID_ITooltipData)
		*ppvObject = static_cast<ITooltipData*>(this);
	else if (iid == IID_IViewObject)
		*ppvObject = static_cast<IViewObject2*>(this);
	else if (iid == IID_IOleObject)
		*ppvObject = static_cast<IOleObject*>(this);
	else if (iid == IID_IUnknown) 
		*ppvObject = this;
	else if (iid == IID_IViewObject2)
		*ppvObject = static_cast<IViewObject2*>(this);
	else 
	{
		*ppvObject = NULL;
		return E_NOINTERFACE;
	}
	AddRef();
	return S_OK;
}

//
// IOleObject members
//
HRESULT ISmileyBase::SetClientSite(IOleClientSite *pClientSite)
{
	if (m_spClientSite != NULL) m_spClientSite->Release();
	m_spClientSite = pClientSite;
	if (m_spClientSite != NULL) m_spClientSite->AddRef();
	return S_OK;
}

HRESULT ISmileyBase::GetClientSite(IOleClientSite **ppClientSite)
{
	if (ppClientSite == NULL) return E_POINTER;
	*ppClientSite = m_spClientSite;
	if (m_spClientSite != NULL) m_spClientSite->AddRef();
	return S_OK;
}

HRESULT ISmileyBase::SetHostNames(LPCOLESTR /* szContainerApp */, LPCOLESTR /* szContainerObj */)
{ return S_OK; }

HRESULT ISmileyBase::Close(DWORD /* dwSaveOption */)
{
	regSmileys.remove(this);

	if (m_spAdviseSink) m_spAdviseSink->Release();
	m_spAdviseSink = NULL;

	return S_OK;
}

HRESULT ISmileyBase::SetMoniker(DWORD /* dwWhichMoniker */, IMoniker* /* pmk */)
{ return E_NOTIMPL; }

HRESULT ISmileyBase::GetMoniker(DWORD /* dwAssign */, DWORD /* dwWhichMoniker */, IMoniker** /* ppmk */)
{ return E_NOTIMPL; }

HRESULT ISmileyBase::InitFromData(IDataObject* /* pDataObject */, BOOL /* fCreation */, DWORD /* dwReserved */)
{ return E_NOTIMPL; }

HRESULT ISmileyBase::GetClipboardData(DWORD /* dwReserved */, IDataObject** /* ppDataObject */)
{ return E_NOTIMPL; }

HRESULT ISmileyBase::DoVerb(LONG /* iVerb */, LPMSG /* pMsg */, IOleClientSite* /* pActiveSite */, LONG /* lindex */,
	HWND /* hwndParent */, LPCRECT /* lprcPosRect */)
{
	return E_NOTIMPL;
}

HRESULT ISmileyBase::EnumVerbs(IEnumOLEVERB** /*ppEnumOleVerb*/) { return E_NOTIMPL; }
HRESULT ISmileyBase::Update(void) { return S_OK; }
HRESULT ISmileyBase::IsUpToDate(void) { return S_OK; }

HRESULT ISmileyBase::GetUserClassID(CLSID *pClsid)
{
	if (!pClsid) return E_POINTER;
	*pClsid = CLSID_NULL;
	return S_OK;
}

HRESULT ISmileyBase::GetUserType(DWORD /*dwFormOfType*/, LPOLESTR* /*pszUserType*/)
{ return E_NOTIMPL; }

HRESULT ISmileyBase::SetExtent(DWORD dwDrawAspect, SIZEL* psizel)
{ 
	if (dwDrawAspect != DVASPECT_CONTENT) return E_FAIL;
	if (psizel == NULL) return E_POINTER;

	HiMetricToPixel(psizel, &m_sizeExtent);
	m_sizeExtentHiM = *psizel;
	return S_OK;
}

HRESULT ISmileyBase::GetExtent(DWORD dwDrawAspect, SIZEL *psizel)
{
	if (dwDrawAspect != DVASPECT_CONTENT) return E_FAIL;
	if (psizel == NULL) return E_POINTER;

	*psizel = m_sizeExtentHiM;
	return S_OK;
}

HRESULT ISmileyBase::Advise(IAdviseSink *pAdvSink, DWORD *pdwConnection)
{
	HRESULT hr = S_OK;
	if (m_spAdviseHolder == NULL)
		hr = CreateOleAdviseHolder(&m_spAdviseHolder);
	if (SUCCEEDED(hr))
		hr = m_spAdviseHolder->Advise(pAdvSink, pdwConnection);
	else
		m_spAdviseHolder = NULL;
	return hr;
}

HRESULT ISmileyBase::Unadvise(DWORD dwConnection)
{
	return m_spAdviseHolder ? m_spAdviseHolder->Unadvise(dwConnection) : E_FAIL;
}

HRESULT ISmileyBase::EnumAdvise(IEnumSTATDATA **ppEnumAdvise)
{
	if (ppEnumAdvise == NULL) return E_POINTER;
	return m_spAdviseHolder ? m_spAdviseHolder->EnumAdvise(ppEnumAdvise) : E_FAIL;
}

HRESULT ISmileyBase::GetMiscStatus(DWORD dwAspect, DWORD *pdwStatus)
{
	if (pdwStatus == NULL) return E_POINTER;
	if (dwAspect == DVASPECT_CONTENT)
	{
		*pdwStatus =  OLEMISC_STATIC | OLEMISC_INVISIBLEATRUNTIME | 
			OLEMISC_CANTLINKINSIDE | OLEMISC_NOUIACTIVATE; 	
		return S_OK;
	}
	else
	{
		*pdwStatus = 0;
		return E_FAIL;
	}
}

HRESULT ISmileyBase::SetColorScheme(LOGPALETTE* /* pLogpal */)
{ return E_NOTIMPL; }

//
// IViewObject members
//
HRESULT ISmileyBase::SetAdvise(DWORD aspect, DWORD advf, IAdviseSink* pAdvSink) 
{
	if (aspect != DVASPECT_CONTENT) return DV_E_DVASPECT;
	m_advf = advf;
	if (m_spAdviseSink) m_spAdviseSink->Release();
	m_spAdviseSink = pAdvSink;
	if (advf & ADVF_PRIMEFIRST) SendOnViewChange();
	return S_OK;
}
HRESULT ISmileyBase::GetAdvise(DWORD* /*pAspects*/, DWORD* /*pAdvf*/, IAdviseSink** ppAdvSink) 
{
	if (!ppAdvSink) return E_POINTER;
	*ppAdvSink = m_spAdviseSink;
	if (m_spAdviseSink) m_spAdviseSink->AddRef();
	return S_OK;
}
HRESULT ISmileyBase::Freeze(DWORD, long, void*, DWORD*) { return E_NOTIMPL; }
HRESULT ISmileyBase::Unfreeze(DWORD) { return E_NOTIMPL; }
HRESULT ISmileyBase::GetColorSet(DWORD, long, void*, DVTARGETDEVICE*, HDC, 
	LOGPALETTE**) { return E_NOTIMPL; }

//
// IViewObject2 members
//
HRESULT ISmileyBase::GetExtent(DWORD aspect, long, DVTARGETDEVICE*, SIZEL* pSize) 
{
	if (pSize == NULL) return E_POINTER;
	if (aspect != DVASPECT_CONTENT) return DV_E_DVASPECT;
	*pSize = m_sizeExtent;
	return S_OK;
}


//
// ITooltipData members
//
HRESULT ISmileyBase::SetTooltip(BSTR /* bstrHint */)
{
	return S_OK;
}

HRESULT ISmileyBase::GetTooltip(BSTR *bstrHint)
{
	if (bstrHint == NULL) return E_POINTER;
	*bstrHint = SysAllocString(T2W_SM(m_smltxt));
	return S_OK;
}


void CloseSmileys(void)
{
	for (int i=regSmileys.getCount()-1; i >= 0; i--) {
		regSmileys[i]->OnClose();
		regSmileys[i]->Close(OLECLOSE_NOSAVE);
	}
}

int CheckForTip(int x, int y, HWND hwnd, TCHAR** smltxt)
{
	for (int i=0; i < regSmileys.getCount(); i++)
		if (regSmileys[i]->QueryHitPointSpecial(x, y, hwnd, smltxt))
			return i;

	return -1;
}

void DestroySmileyBase(void)
{
	regSmileys.destroy();
}