xref: /AOO41X/main/shell/source/win32/shlxthandler/thumbviewer/thumbviewer.cxx (revision 7e8066ab7ce05f6e5cc79dfc39ea68d520b5a9ad)
1f8e2c85aSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3f8e2c85aSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4f8e2c85aSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5f8e2c85aSAndrew Rist  * distributed with this work for additional information
6f8e2c85aSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7f8e2c85aSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8f8e2c85aSAndrew Rist  * "License"); you may not use this file except in compliance
9f8e2c85aSAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
11f8e2c85aSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
13f8e2c85aSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14f8e2c85aSAndrew Rist  * software distributed under the License is distributed on an
15f8e2c85aSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16f8e2c85aSAndrew Rist  * KIND, either express or implied.  See the License for the
17f8e2c85aSAndrew Rist  * specific language governing permissions and limitations
18f8e2c85aSAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
20f8e2c85aSAndrew Rist  *************************************************************/
21f8e2c85aSAndrew Rist 
22f8e2c85aSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_shell.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include "internal/global.hxx"
28cdf0e10cSrcweir 
29cdf0e10cSrcweir #ifndef INFOTIPS_HXX_INCLUDED
30cdf0e10cSrcweir #include "internal/thumbviewer.hxx"
31cdf0e10cSrcweir #endif
32cdf0e10cSrcweir #include "internal/shlxthdl.hxx"
33cdf0e10cSrcweir #include "internal/registry.hxx"
34cdf0e10cSrcweir #include "internal/fileextensions.hxx"
35cdf0e10cSrcweir #include "internal/config.hxx"
36cdf0e10cSrcweir #include "internal/zipfile.hxx"
37cdf0e10cSrcweir #include "internal/utilities.hxx"
38cdf0e10cSrcweir 
39cdf0e10cSrcweir #include "internal/resource.h"
40cdf0e10cSrcweir 
41cdf0e10cSrcweir #include <stdio.h>
42cdf0e10cSrcweir #include <utility>
43cdf0e10cSrcweir #include <stdlib.h>
44cdf0e10cSrcweir 
45cdf0e10cSrcweir #if defined _MSC_VER
46cdf0e10cSrcweir #pragma warning(push, 1)
47cdf0e10cSrcweir #endif
48cdf0e10cSrcweir #include <shellapi.h>
49cdf0e10cSrcweir #if defined _MSC_VER
50cdf0e10cSrcweir #pragma warning(pop)
51cdf0e10cSrcweir #endif
52cdf0e10cSrcweir #include <memory>
53cdf0e10cSrcweir 
54cdf0e10cSrcweir extern HINSTANCE g_hModule;
55cdf0e10cSrcweir 
56cdf0e10cSrcweir namespace internal
57cdf0e10cSrcweir {
58cdf0e10cSrcweir 	/* The signet.png used for thumbnails of signed documents
59cdf0e10cSrcweir 	   is contained as resource in this module, the resource
60cdf0e10cSrcweir 	   id is 2000 */
LoadSignetImageFromResource(ZipFile::ZipContentBuffer_t & buffer)61cdf0e10cSrcweir 	void LoadSignetImageFromResource(ZipFile::ZipContentBuffer_t& buffer)
62cdf0e10cSrcweir 	{
63cdf0e10cSrcweir 		HRSRC hrc = FindResource(g_hModule, TEXT("#2000"), RT_RCDATA);
64cdf0e10cSrcweir 		DWORD size = SizeofResource(g_hModule, hrc);
65cdf0e10cSrcweir 		HGLOBAL hglob = LoadResource(g_hModule, hrc);
66cdf0e10cSrcweir 		char* data = reinterpret_cast<char*>(LockResource(hglob));
67cdf0e10cSrcweir 		buffer = ZipFile::ZipContentBuffer_t(data, data + size);
68cdf0e10cSrcweir 	}
69cdf0e10cSrcweir 
IsSignedDocument(const ZipFile * zipfile)70cdf0e10cSrcweir 	bool IsSignedDocument(const ZipFile* zipfile)
71cdf0e10cSrcweir 	{
72cdf0e10cSrcweir 		return zipfile->HasContent("META-INF/documentsignatures.xml");
73cdf0e10cSrcweir 	}
74cdf0e10cSrcweir 
IsWindowsXP()75cdf0e10cSrcweir 	bool IsWindowsXP()
76cdf0e10cSrcweir 	{
77cdf0e10cSrcweir 		OSVERSIONINFO osvi;
78cdf0e10cSrcweir 		ZeroMemory(&osvi, sizeof(osvi));
79cdf0e10cSrcweir 		osvi.dwOSVersionInfoSize = sizeof(osvi);
80cdf0e10cSrcweir 		GetVersionEx(&osvi);
81cdf0e10cSrcweir 
82cdf0e10cSrcweir 		return ((osvi.dwPlatformId == VER_PLATFORM_WIN32_NT) &&
83cdf0e10cSrcweir 				((osvi.dwMajorVersion >= 5) && (osvi.dwMinorVersion >= 1)));
84cdf0e10cSrcweir 	}
85cdf0e10cSrcweir 
86cdf0e10cSrcweir 	/* Calculate where to position the signet image.
87cdf0e10cSrcweir 	   On Windows ME we need to shift the signet a
88cdf0e10cSrcweir 	   little bit to the left because Windows ME
89cdf0e10cSrcweir 	   puts an overlay icon to the lower right
90cdf0e10cSrcweir 	   corner of a thumbnail image so that our signet
917f610566Smseidel 	   will be hidden. */
CalcSignetPosition(const Gdiplus::Rect & canvas,const Gdiplus::Rect & thumbnail_border,const Gdiplus::Rect & signet)92cdf0e10cSrcweir 	Gdiplus::Point CalcSignetPosition(
93cdf0e10cSrcweir 		const Gdiplus::Rect& canvas, const Gdiplus::Rect& thumbnail_border, const Gdiplus::Rect& signet)
94cdf0e10cSrcweir 	{
95cdf0e10cSrcweir 		int x = 0;
96cdf0e10cSrcweir 		int y = 0;
97cdf0e10cSrcweir 		int hoffset = canvas.GetRight() - thumbnail_border.GetRight();
98cdf0e10cSrcweir 		int voffset = canvas.GetBottom() - thumbnail_border.GetBottom();
99cdf0e10cSrcweir 
100cdf0e10cSrcweir 		if (hoffset > voffset)
101cdf0e10cSrcweir 		{
102cdf0e10cSrcweir 			x = thumbnail_border.GetRight() - signet.GetRight() + min(signet.GetRight() / 2, hoffset);
103cdf0e10cSrcweir 			y = thumbnail_border.GetBottom() - signet.GetBottom();
104cdf0e10cSrcweir 		}
105cdf0e10cSrcweir 		else
106cdf0e10cSrcweir 		{
107cdf0e10cSrcweir 			x = thumbnail_border.GetRight() - signet.GetRight();
108cdf0e10cSrcweir 			y = thumbnail_border.GetBottom() - signet.GetBottom() + min(signet.GetBottom() / 2, voffset);
109cdf0e10cSrcweir 		}
110cdf0e10cSrcweir 
111cdf0e10cSrcweir 		if (!IsWindowsXP())
112cdf0e10cSrcweir 			x -= 15;
113cdf0e10cSrcweir 
114cdf0e10cSrcweir 		return Gdiplus::Point(x,y);
115cdf0e10cSrcweir 	}
116cdf0e10cSrcweir }
117cdf0e10cSrcweir 
118cdf0e10cSrcweir class StreamOnZipBuffer : public IStream
119cdf0e10cSrcweir {
120cdf0e10cSrcweir public:
121cdf0e10cSrcweir 	StreamOnZipBuffer(const ZipFile::ZipContentBuffer_t& zip_buffer);
122cdf0e10cSrcweir 
123cdf0e10cSrcweir 	// IUnknown
124cdf0e10cSrcweir 	virtual ULONG STDMETHODCALLTYPE AddRef();
125cdf0e10cSrcweir 	virtual ULONG STDMETHODCALLTYPE Release( void);
126cdf0e10cSrcweir 	virtual HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid, void __RPC_FAR *__RPC_FAR *ppvObject);
127cdf0e10cSrcweir 
128cdf0e10cSrcweir 	// IStream
129cdf0e10cSrcweir 	virtual HRESULT STDMETHODCALLTYPE Read(void *pv, ULONG cb, ULONG *pcbRead);
130cdf0e10cSrcweir 	virtual HRESULT STDMETHODCALLTYPE Write(void const *pv, ULONG cb, ULONG *pcbWritten);
131cdf0e10cSrcweir 	virtual HRESULT STDMETHODCALLTYPE Seek(LARGE_INTEGER dlibMove, DWORD dwOrigin, ULARGE_INTEGER *plibNewPosition);
132cdf0e10cSrcweir 	virtual HRESULT STDMETHODCALLTYPE SetSize(ULARGE_INTEGER libNewSize);
133cdf0e10cSrcweir 	virtual HRESULT STDMETHODCALLTYPE CopyTo(IStream *pstm, ULARGE_INTEGER cb, ULARGE_INTEGER *pcbRead, ULARGE_INTEGER *pcbWritten);
134cdf0e10cSrcweir 	virtual HRESULT STDMETHODCALLTYPE Commit(DWORD grfCommitFlags);
135cdf0e10cSrcweir 	virtual HRESULT STDMETHODCALLTYPE Revert(void);
136cdf0e10cSrcweir 	virtual HRESULT STDMETHODCALLTYPE LockRegion(ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType);
137cdf0e10cSrcweir 	virtual HRESULT STDMETHODCALLTYPE UnlockRegion(ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType);
138cdf0e10cSrcweir 	virtual HRESULT STDMETHODCALLTYPE Stat(STATSTG *pstatstg, DWORD grfStatFlag);
139cdf0e10cSrcweir 	virtual HRESULT STDMETHODCALLTYPE Clone(IStream **ppstm);
140cdf0e10cSrcweir 
141cdf0e10cSrcweir private:
142cdf0e10cSrcweir 	LONG ref_count_;
143cdf0e10cSrcweir 	const ZipFile::ZipContentBuffer_t& ref_zip_buffer_;
144cdf0e10cSrcweir 	size_t pos_;
145cdf0e10cSrcweir };
146cdf0e10cSrcweir 
StreamOnZipBuffer(const ZipFile::ZipContentBuffer_t & zip_buffer)147cdf0e10cSrcweir StreamOnZipBuffer::StreamOnZipBuffer(const ZipFile::ZipContentBuffer_t& zip_buffer) :
148cdf0e10cSrcweir 	ref_count_(1),
149cdf0e10cSrcweir 	ref_zip_buffer_(zip_buffer),
150cdf0e10cSrcweir 	pos_(0)
151cdf0e10cSrcweir {
152cdf0e10cSrcweir }
153cdf0e10cSrcweir 
154cdf0e10cSrcweir // IUnknown methods
155cdf0e10cSrcweir 
AddRef(void)156cdf0e10cSrcweir ULONG STDMETHODCALLTYPE StreamOnZipBuffer::AddRef(void)
157cdf0e10cSrcweir {
158cdf0e10cSrcweir 	return InterlockedIncrement(&ref_count_);
159cdf0e10cSrcweir }
160cdf0e10cSrcweir 
Release(void)161cdf0e10cSrcweir ULONG STDMETHODCALLTYPE StreamOnZipBuffer::Release( void)
162cdf0e10cSrcweir {
163cdf0e10cSrcweir 	long refcnt = InterlockedDecrement(&ref_count_);
164cdf0e10cSrcweir 
165cdf0e10cSrcweir 	if (0 == ref_count_)
166cdf0e10cSrcweir 		delete this;
167cdf0e10cSrcweir 
168cdf0e10cSrcweir 	return refcnt;
169cdf0e10cSrcweir }
170cdf0e10cSrcweir 
QueryInterface(REFIID riid,void __RPC_FAR * __RPC_FAR * ppvObject)171cdf0e10cSrcweir HRESULT STDMETHODCALLTYPE StreamOnZipBuffer::QueryInterface(REFIID riid, void __RPC_FAR *__RPC_FAR *ppvObject)
172cdf0e10cSrcweir {
173cdf0e10cSrcweir 	*ppvObject = 0;
174cdf0e10cSrcweir 	IUnknown* pUnk = 0;
175cdf0e10cSrcweir 
176cdf0e10cSrcweir 	if ((IID_IUnknown == riid) || (IID_IStream == riid))
177cdf0e10cSrcweir 	{
178cdf0e10cSrcweir 		pUnk = static_cast<IStream*>(this);
179cdf0e10cSrcweir 		pUnk->AddRef();
180cdf0e10cSrcweir 		*ppvObject = pUnk;
181cdf0e10cSrcweir 		return S_OK;
182cdf0e10cSrcweir 	}
183cdf0e10cSrcweir 	return E_NOINTERFACE;
184cdf0e10cSrcweir }
185cdf0e10cSrcweir 
Read(void * pv,ULONG cb,ULONG * pcbRead)186cdf0e10cSrcweir HRESULT STDMETHODCALLTYPE StreamOnZipBuffer::Read(void *pv, ULONG cb, ULONG *pcbRead)
187cdf0e10cSrcweir {
188cdf0e10cSrcweir 	if (pv == NULL)
189cdf0e10cSrcweir 		return STG_E_INVALIDPOINTER;
190cdf0e10cSrcweir 
191cdf0e10cSrcweir 	size_t size = ref_zip_buffer_.size();
192cdf0e10cSrcweir 
193cdf0e10cSrcweir 	if (pos_ > size)
194cdf0e10cSrcweir 		return S_FALSE;
195cdf0e10cSrcweir 
196cdf0e10cSrcweir 	char* p = reinterpret_cast<char*>(pv);
197cdf0e10cSrcweir 	ULONG read = 0;
198cdf0e10cSrcweir 
199cdf0e10cSrcweir 	for ( ;(pos_ < size) && (cb > 0); pos_++, cb--, read++)
200cdf0e10cSrcweir 		*p++ = ref_zip_buffer_[pos_];
201cdf0e10cSrcweir 
202cdf0e10cSrcweir 	if (pcbRead)
203cdf0e10cSrcweir 		*pcbRead = read;
204cdf0e10cSrcweir 
205cdf0e10cSrcweir 	return S_OK;
206cdf0e10cSrcweir }
207cdf0e10cSrcweir 
Seek(LARGE_INTEGER dlibMove,DWORD dwOrigin,ULARGE_INTEGER *)208cdf0e10cSrcweir HRESULT STDMETHODCALLTYPE StreamOnZipBuffer::Seek(LARGE_INTEGER dlibMove, DWORD dwOrigin, ULARGE_INTEGER *)
209cdf0e10cSrcweir {
210cdf0e10cSrcweir 	__int64 size = (__int64) ref_zip_buffer_.size();
211cdf0e10cSrcweir 	__int64 p = 0;
212cdf0e10cSrcweir 
213cdf0e10cSrcweir 	switch (dwOrigin)
214cdf0e10cSrcweir 	{
215cdf0e10cSrcweir 		case STREAM_SEEK_SET:
216cdf0e10cSrcweir 			break;
217cdf0e10cSrcweir 		case STREAM_SEEK_CUR:
218cdf0e10cSrcweir 			p = (__int64) pos_;
219cdf0e10cSrcweir 			break;
220cdf0e10cSrcweir 		case STREAM_SEEK_END:
221cdf0e10cSrcweir 			p = size - 1;
222cdf0e10cSrcweir 			break;
223cdf0e10cSrcweir 	}
224cdf0e10cSrcweir 
225cdf0e10cSrcweir 	HRESULT hr = STG_E_INVALIDFUNCTION;
226cdf0e10cSrcweir 
227cdf0e10cSrcweir 	p += dlibMove.QuadPart;
228cdf0e10cSrcweir 
229cdf0e10cSrcweir 	if ( ( p >= 0 ) && (p < size) )
230cdf0e10cSrcweir 	{
231cdf0e10cSrcweir 		pos_ = (size_t) p;
232cdf0e10cSrcweir 		hr = S_OK;
233cdf0e10cSrcweir 	}
234cdf0e10cSrcweir 	return hr;
235cdf0e10cSrcweir }
236cdf0e10cSrcweir 
Stat(STATSTG * pstatstg,DWORD grfStatFlag)237cdf0e10cSrcweir HRESULT STDMETHODCALLTYPE StreamOnZipBuffer::Stat(STATSTG *pstatstg, DWORD grfStatFlag)
238cdf0e10cSrcweir {
239cdf0e10cSrcweir 	if (pstatstg == NULL)
240cdf0e10cSrcweir 		return STG_E_INVALIDPOINTER;
241cdf0e10cSrcweir 
242cdf0e10cSrcweir 	ZeroMemory(pstatstg, sizeof(STATSTG));
243cdf0e10cSrcweir 
244cdf0e10cSrcweir 	if (grfStatFlag == STATFLAG_DEFAULT)
245cdf0e10cSrcweir 	{
246cdf0e10cSrcweir 		size_t sz = 4 * sizeof(wchar_t);
247cdf0e10cSrcweir 		wchar_t* name = reinterpret_cast<wchar_t*>(CoTaskMemAlloc(sz));
248cdf0e10cSrcweir 		ZeroMemory(name, sz);
249cdf0e10cSrcweir 		memcpy(name, L"png", 3 * sizeof(wchar_t));
250cdf0e10cSrcweir 		pstatstg->pwcsName = name;
251cdf0e10cSrcweir 	}
252cdf0e10cSrcweir 
253cdf0e10cSrcweir 	pstatstg->type = STGTY_LOCKBYTES;
254cdf0e10cSrcweir 
255cdf0e10cSrcweir 	ULARGE_INTEGER uli;
256cdf0e10cSrcweir 	uli.LowPart = ref_zip_buffer_.size();
257cdf0e10cSrcweir 	uli.HighPart = 0;
258cdf0e10cSrcweir 
259cdf0e10cSrcweir 	pstatstg->cbSize = uli;
260cdf0e10cSrcweir 
261cdf0e10cSrcweir 	return S_OK;
262cdf0e10cSrcweir }
263cdf0e10cSrcweir 
Write(void const *,ULONG,ULONG *)264cdf0e10cSrcweir HRESULT STDMETHODCALLTYPE StreamOnZipBuffer::Write(void const *, ULONG, ULONG *)
265cdf0e10cSrcweir { return E_NOTIMPL; }
266cdf0e10cSrcweir 
SetSize(ULARGE_INTEGER)267cdf0e10cSrcweir HRESULT STDMETHODCALLTYPE StreamOnZipBuffer::SetSize(ULARGE_INTEGER)
268cdf0e10cSrcweir { return E_NOTIMPL; }
269cdf0e10cSrcweir 
CopyTo(IStream *,ULARGE_INTEGER,ULARGE_INTEGER *,ULARGE_INTEGER *)270cdf0e10cSrcweir HRESULT STDMETHODCALLTYPE StreamOnZipBuffer::CopyTo(IStream *, ULARGE_INTEGER, ULARGE_INTEGER *, ULARGE_INTEGER *)
271cdf0e10cSrcweir { return E_NOTIMPL; }
272cdf0e10cSrcweir 
Commit(DWORD)273cdf0e10cSrcweir HRESULT STDMETHODCALLTYPE StreamOnZipBuffer::Commit(DWORD)
274cdf0e10cSrcweir { return E_NOTIMPL; }
275cdf0e10cSrcweir 
Revert(void)276cdf0e10cSrcweir HRESULT STDMETHODCALLTYPE StreamOnZipBuffer::Revert(void)
277cdf0e10cSrcweir { return E_NOTIMPL; }
278cdf0e10cSrcweir 
LockRegion(ULARGE_INTEGER,ULARGE_INTEGER,DWORD)279cdf0e10cSrcweir HRESULT STDMETHODCALLTYPE StreamOnZipBuffer::LockRegion(ULARGE_INTEGER, ULARGE_INTEGER, DWORD)
280cdf0e10cSrcweir { return E_NOTIMPL; }
281cdf0e10cSrcweir 
UnlockRegion(ULARGE_INTEGER,ULARGE_INTEGER,DWORD)282cdf0e10cSrcweir HRESULT STDMETHODCALLTYPE StreamOnZipBuffer::UnlockRegion(ULARGE_INTEGER, ULARGE_INTEGER, DWORD)
283cdf0e10cSrcweir { return E_NOTIMPL; }
284cdf0e10cSrcweir 
Clone(IStream **)285cdf0e10cSrcweir HRESULT STDMETHODCALLTYPE StreamOnZipBuffer::Clone(IStream **)
286cdf0e10cSrcweir { return E_NOTIMPL; }
287cdf0e10cSrcweir 
288cdf0e10cSrcweir 
289cdf0e10cSrcweir //#########################################
290cdf0e10cSrcweir 
291cdf0e10cSrcweir 
CThumbviewer(long RefCnt)292cdf0e10cSrcweir CThumbviewer::CThumbviewer(long RefCnt) :
293cdf0e10cSrcweir 	ref_count_(RefCnt)
294cdf0e10cSrcweir {
295cdf0e10cSrcweir 	InterlockedIncrement(&g_DllRefCnt);
296cdf0e10cSrcweir 
297cdf0e10cSrcweir 	thumbnail_size_.cx = 0;
298cdf0e10cSrcweir 	thumbnail_size_.cy = 0;
299cdf0e10cSrcweir 
300cdf0e10cSrcweir 	Gdiplus::GdiplusStartupInput gdiplusStartupInput;
301cdf0e10cSrcweir 	Gdiplus::GdiplusStartup(&gdiplus_token_, &gdiplusStartupInput, NULL);
302cdf0e10cSrcweir 
303cdf0e10cSrcweir 	ZipFile::ZipContentBuffer_t img_data;
304cdf0e10cSrcweir 	internal::LoadSignetImageFromResource(img_data);
305cdf0e10cSrcweir 	IStream* stream = new StreamOnZipBuffer(img_data);
306cdf0e10cSrcweir 	signet_ = new Gdiplus::Bitmap(stream, TRUE);
307cdf0e10cSrcweir 	stream->Release();
308cdf0e10cSrcweir }
309cdf0e10cSrcweir 
~CThumbviewer()310cdf0e10cSrcweir CThumbviewer::~CThumbviewer()
311cdf0e10cSrcweir {
312cdf0e10cSrcweir 	delete signet_;
313cdf0e10cSrcweir 	Gdiplus::GdiplusShutdown(gdiplus_token_);
314cdf0e10cSrcweir 	InterlockedDecrement(&g_DllRefCnt);
315cdf0e10cSrcweir }
316cdf0e10cSrcweir 
317cdf0e10cSrcweir // IUnknown methods
318cdf0e10cSrcweir 
QueryInterface(REFIID riid,void __RPC_FAR * __RPC_FAR * ppvObject)319cdf0e10cSrcweir HRESULT STDMETHODCALLTYPE CThumbviewer::QueryInterface(REFIID riid, void __RPC_FAR *__RPC_FAR *ppvObject)
320cdf0e10cSrcweir {
321cdf0e10cSrcweir 	*ppvObject = 0;
322cdf0e10cSrcweir 	IUnknown* pUnk = 0;
323cdf0e10cSrcweir 
324cdf0e10cSrcweir 	if ((IID_IUnknown == riid) || (IID_IPersistFile == riid))
325cdf0e10cSrcweir 	{
326cdf0e10cSrcweir 		pUnk = static_cast<IPersistFile*>(this);
327cdf0e10cSrcweir 		pUnk->AddRef();
328cdf0e10cSrcweir 		*ppvObject = pUnk;
329cdf0e10cSrcweir 		return S_OK;
330cdf0e10cSrcweir 	}
331cdf0e10cSrcweir 	else if (IID_IExtractImage == riid)
332cdf0e10cSrcweir 	{
333cdf0e10cSrcweir 		pUnk = static_cast<IExtractImage*>(this);
334cdf0e10cSrcweir 		pUnk->AddRef();
335cdf0e10cSrcweir 		*ppvObject = pUnk;
336cdf0e10cSrcweir 		return S_OK;
337cdf0e10cSrcweir 	}
338cdf0e10cSrcweir 	return E_NOINTERFACE;
339cdf0e10cSrcweir }
340cdf0e10cSrcweir 
AddRef(void)341cdf0e10cSrcweir ULONG STDMETHODCALLTYPE CThumbviewer::AddRef(void)
342cdf0e10cSrcweir {
343cdf0e10cSrcweir 	return InterlockedIncrement(&ref_count_);
344cdf0e10cSrcweir }
345cdf0e10cSrcweir 
Release(void)346cdf0e10cSrcweir ULONG STDMETHODCALLTYPE CThumbviewer::Release( void)
347cdf0e10cSrcweir {
348cdf0e10cSrcweir 	long refcnt = InterlockedDecrement(&ref_count_);
349cdf0e10cSrcweir 
350cdf0e10cSrcweir 	if (0 == ref_count_)
351cdf0e10cSrcweir 		delete this;
352cdf0e10cSrcweir 
353cdf0e10cSrcweir 	return refcnt;
354cdf0e10cSrcweir }
355cdf0e10cSrcweir 
356cdf0e10cSrcweir // IExtractImage2 methods
357cdf0e10cSrcweir 
358cdf0e10cSrcweir const std::string THUMBNAIL_CONTENT = "Thumbnails/thumbnail.png";
359cdf0e10cSrcweir 
Extract(HBITMAP * phBmpImage)360cdf0e10cSrcweir HRESULT STDMETHODCALLTYPE CThumbviewer::Extract(HBITMAP *phBmpImage)
361cdf0e10cSrcweir {
362cdf0e10cSrcweir 	HRESULT hr = E_FAIL;
363cdf0e10cSrcweir 
364cdf0e10cSrcweir 	try
365cdf0e10cSrcweir 	{
366cdf0e10cSrcweir 		std::wstring fname = getShortPathName( filename_ );
367cdf0e10cSrcweir 		std::auto_ptr<ZipFile> zipfile( new ZipFile( WStringToString( fname ) ) );
368cdf0e10cSrcweir 
369cdf0e10cSrcweir 		if (zipfile->HasContent(THUMBNAIL_CONTENT))
370cdf0e10cSrcweir 		{
371cdf0e10cSrcweir 			ZipFile::ZipContentBuffer_t thumbnail;
372cdf0e10cSrcweir 			zipfile->GetUncompressedContent(THUMBNAIL_CONTENT, thumbnail);
373cdf0e10cSrcweir 			IStream* stream = new StreamOnZipBuffer(thumbnail);
374cdf0e10cSrcweir 
375cdf0e10cSrcweir 			Gdiplus::Bitmap thumbnail_png(stream, TRUE);
376cdf0e10cSrcweir 
377cdf0e10cSrcweir 			if ((thumbnail_png.GetHeight() == 0) || (thumbnail_png.GetWidth() == 0))
378cdf0e10cSrcweir 			{
379cdf0e10cSrcweir 				stream->Release();
380cdf0e10cSrcweir 				return E_FAIL;
381cdf0e10cSrcweir 			}
382cdf0e10cSrcweir 
383cdf0e10cSrcweir 			HWND hwnd = GetDesktopWindow();
384cdf0e10cSrcweir 			HDC hdc = GetDC(hwnd);
385cdf0e10cSrcweir 			HDC memDC = CreateCompatibleDC(hdc);
386cdf0e10cSrcweir 
387cdf0e10cSrcweir 			if (memDC)
388cdf0e10cSrcweir 			{
3897f610566Smseidel 				UINT offset = 1; // reserve a little border space
390cdf0e10cSrcweir 
391cdf0e10cSrcweir 				Gdiplus::Rect canvas(0, 0, thumbnail_size_.cx, thumbnail_size_.cy);
392*7e8066abSmseidel 				Gdiplus::Rect canvas_thumbnail(offset, offset, thumbnail_size_.cx - (2 * offset) - 1, thumbnail_size_.cy - (2 * offset) - 1);
393cdf0e10cSrcweir 
394cdf0e10cSrcweir 				Gdiplus::Rect scaledRect = CalcScaledAspectRatio(
395cdf0e10cSrcweir 					Gdiplus::Rect(0, 0, thumbnail_png.GetWidth(), thumbnail_png.GetHeight()), canvas_thumbnail);
396cdf0e10cSrcweir 
397cdf0e10cSrcweir 				struct {
398cdf0e10cSrcweir 					BITMAPINFOHEADER bi;
399cdf0e10cSrcweir 					DWORD ct[256];
400cdf0e10cSrcweir 				} dib;
401cdf0e10cSrcweir 
402cdf0e10cSrcweir 				ZeroMemory(&dib, sizeof(dib));
403cdf0e10cSrcweir 
404cdf0e10cSrcweir 				dib.bi.biSize = sizeof(BITMAPINFOHEADER);
405cdf0e10cSrcweir 				dib.bi.biWidth = thumbnail_size_.cx;
406cdf0e10cSrcweir 				dib.bi.biHeight = thumbnail_size_.cy;
407cdf0e10cSrcweir 				dib.bi.biPlanes = 1;
408cdf0e10cSrcweir 				dib.bi.biBitCount = static_cast<WORD>(color_depth_);
409cdf0e10cSrcweir 				dib.bi.biCompression = BI_RGB;
410cdf0e10cSrcweir 
411cdf0e10cSrcweir 				LPVOID lpBits;
412cdf0e10cSrcweir 				HBITMAP hMemBmp = CreateDIBSection(memDC, (LPBITMAPINFO)&dib, DIB_RGB_COLORS, &lpBits, NULL, 0);
413cdf0e10cSrcweir 				HGDIOBJ hOldObj = SelectObject(memDC, hMemBmp);
414cdf0e10cSrcweir 
415cdf0e10cSrcweir 				Gdiplus::Graphics graphics(memDC);
416*7e8066abSmseidel 				Gdiplus::Pen grayPen(Gdiplus::Color(255, 127, 127, 127), 1);
417cdf0e10cSrcweir 
418cdf0e10cSrcweir 				Gdiplus::SolidBrush whiteBrush(Gdiplus::Color(255, 255, 255, 255));
419cdf0e10cSrcweir 				graphics.FillRectangle(&whiteBrush, canvas);
420cdf0e10cSrcweir 
421cdf0e10cSrcweir 				scaledRect.X = (canvas.Width - scaledRect.Width) / 2;
422cdf0e10cSrcweir 				scaledRect.Y = (canvas.Height - scaledRect.Height) / 2;
423cdf0e10cSrcweir 
424cdf0e10cSrcweir 				Gdiplus::Rect border_rect(scaledRect.X, scaledRect.Y, scaledRect.Width, scaledRect.Height);
425*7e8066abSmseidel 				graphics.DrawRectangle(&grayPen, border_rect);
426cdf0e10cSrcweir 
427cdf0e10cSrcweir 				scaledRect.X += 1;
428cdf0e10cSrcweir 				scaledRect.Y += 1;
429cdf0e10cSrcweir 				scaledRect.Width -= 1;
430cdf0e10cSrcweir 				scaledRect.Height -= 1;
431cdf0e10cSrcweir 
432cdf0e10cSrcweir 				graphics.SetInterpolationMode(Gdiplus::InterpolationModeHighQualityBicubic);
433cdf0e10cSrcweir 				Gdiplus::Status stat = graphics.DrawImage(
434cdf0e10cSrcweir 					&thumbnail_png, scaledRect, 0 , 0,
435cdf0e10cSrcweir 					thumbnail_png.GetWidth(), thumbnail_png.GetHeight(), Gdiplus::UnitPixel);
436cdf0e10cSrcweir 
437cdf0e10cSrcweir 				/* Add a signet sign to the thumbnail of signed documents */
438cdf0e10cSrcweir 				if (internal::IsSignedDocument(zipfile.get()))
439cdf0e10cSrcweir 				{
440cdf0e10cSrcweir 					double SCALING_FACTOR = 0.6;
441cdf0e10cSrcweir 					Gdiplus::Rect signet_scaled(
442cdf0e10cSrcweir 						0, 0, static_cast<INT>(signet_->GetWidth() * SCALING_FACTOR), static_cast<INT>(signet_->GetHeight() * SCALING_FACTOR));
443cdf0e10cSrcweir 					Gdiplus::Point pos_signet = internal::CalcSignetPosition(canvas_thumbnail, border_rect, signet_scaled);
444cdf0e10cSrcweir 					Gdiplus::Rect dest(pos_signet.X, pos_signet.Y, signet_scaled.GetRight(), signet_scaled.GetBottom());
445cdf0e10cSrcweir 
446cdf0e10cSrcweir 					stat = graphics.DrawImage(
447cdf0e10cSrcweir 						signet_, dest,
448cdf0e10cSrcweir 						0, 0, signet_->GetWidth(), signet_->GetHeight(),
449cdf0e10cSrcweir 						Gdiplus::UnitPixel);
450cdf0e10cSrcweir 				}
451cdf0e10cSrcweir 
452cdf0e10cSrcweir 				if (stat == Gdiplus::Ok)
453cdf0e10cSrcweir 				{
454cdf0e10cSrcweir 					*phBmpImage = hMemBmp;
455cdf0e10cSrcweir 					hr = NOERROR;
456cdf0e10cSrcweir 				}
457cdf0e10cSrcweir 
458cdf0e10cSrcweir 				SelectObject(memDC, hOldObj);
459cdf0e10cSrcweir 				DeleteDC(memDC);
460cdf0e10cSrcweir 			}
461cdf0e10cSrcweir 
462cdf0e10cSrcweir 			ReleaseDC(hwnd, hdc);
463cdf0e10cSrcweir 			stream->Release();
464cdf0e10cSrcweir 		}
465cdf0e10cSrcweir 	}
466cdf0e10cSrcweir 	catch(std::exception&)
467cdf0e10cSrcweir 	{
468cdf0e10cSrcweir 		OutputDebugStringFormat( "CThumbviewer Extract ERROR!\n" );
469cdf0e10cSrcweir 		hr = E_FAIL;
470cdf0e10cSrcweir 	}
471cdf0e10cSrcweir 	return hr;
472cdf0e10cSrcweir }
473cdf0e10cSrcweir 
GetLocation(LPWSTR pszPathBuffer,DWORD cchMax,DWORD * pdwPriority,const SIZE * prgSize,DWORD dwRecClrDepth,DWORD * pdwFlags)474cdf0e10cSrcweir HRESULT STDMETHODCALLTYPE CThumbviewer::GetLocation(
475cdf0e10cSrcweir 	LPWSTR pszPathBuffer, DWORD cchMax, DWORD *pdwPriority, const SIZE *prgSize, DWORD dwRecClrDepth, DWORD *pdwFlags)
476cdf0e10cSrcweir {
477cdf0e10cSrcweir 	if ((prgSize == NULL) || (pdwFlags == NULL) || ((*pdwFlags & IEIFLAG_ASYNC) && (pdwPriority == NULL)))
478cdf0e10cSrcweir 		return E_INVALIDARG;
479cdf0e10cSrcweir 
480cdf0e10cSrcweir 	thumbnail_size_ = *prgSize;
481cdf0e10cSrcweir 	color_depth_ = dwRecClrDepth;
482cdf0e10cSrcweir 
483cdf0e10cSrcweir 	*pdwFlags = IEIFLAG_CACHE; // we don't cache the image
484cdf0e10cSrcweir 
485cdf0e10cSrcweir 	wcsncpy(pszPathBuffer, filename_.c_str(), cchMax);
486cdf0e10cSrcweir 
487cdf0e10cSrcweir 	return NOERROR;
488cdf0e10cSrcweir }
489cdf0e10cSrcweir 
490cdf0e10cSrcweir // IPersist methods
491cdf0e10cSrcweir 
GetClassID(CLSID * pClassID)492cdf0e10cSrcweir HRESULT STDMETHODCALLTYPE CThumbviewer::GetClassID(CLSID* pClassID)
493cdf0e10cSrcweir {
494cdf0e10cSrcweir 	pClassID = const_cast<CLSID*>(&CLSID_THUMBVIEWER_HANDLER);
495cdf0e10cSrcweir 	return S_OK;
496cdf0e10cSrcweir }
497cdf0e10cSrcweir 
498cdf0e10cSrcweir // IPersistFile methods
499cdf0e10cSrcweir 
Load(LPCOLESTR pszFileName,DWORD)500cdf0e10cSrcweir HRESULT STDMETHODCALLTYPE CThumbviewer::Load(LPCOLESTR pszFileName, DWORD)
501cdf0e10cSrcweir {
502cdf0e10cSrcweir 	filename_ = pszFileName;
503cdf0e10cSrcweir 	return S_OK;
504cdf0e10cSrcweir }
505cdf0e10cSrcweir 
IsDirty()506cdf0e10cSrcweir HRESULT STDMETHODCALLTYPE CThumbviewer::IsDirty()
507cdf0e10cSrcweir { return E_NOTIMPL; }
508cdf0e10cSrcweir 
Save(LPCOLESTR,BOOL)509cdf0e10cSrcweir HRESULT STDMETHODCALLTYPE CThumbviewer::Save(LPCOLESTR, BOOL)
510cdf0e10cSrcweir { return E_NOTIMPL; }
511cdf0e10cSrcweir 
SaveCompleted(LPCOLESTR)512cdf0e10cSrcweir HRESULT STDMETHODCALLTYPE CThumbviewer::SaveCompleted(LPCOLESTR)
513cdf0e10cSrcweir { return E_NOTIMPL; }
514cdf0e10cSrcweir 
GetCurFile(LPOLESTR __RPC_FAR *)515cdf0e10cSrcweir HRESULT STDMETHODCALLTYPE CThumbviewer::GetCurFile(LPOLESTR __RPC_FAR*)
516cdf0e10cSrcweir { return E_NOTIMPL; }
517cdf0e10cSrcweir 
518cdf0e10cSrcweir 
CalcScaledAspectRatio(Gdiplus::Rect src,Gdiplus::Rect dest)519cdf0e10cSrcweir Gdiplus::Rect CThumbviewer::CalcScaledAspectRatio(Gdiplus::Rect src, Gdiplus::Rect dest)
520cdf0e10cSrcweir {
521cdf0e10cSrcweir 	Gdiplus::Rect result;
522cdf0e10cSrcweir 	if (src.Width >= src.Height)
523cdf0e10cSrcweir 		result = Gdiplus::Rect(0, 0, dest.Width, src.Height * dest.Width / src.Width);
524cdf0e10cSrcweir 	else
525cdf0e10cSrcweir 		result = Gdiplus::Rect(0, 0, src.Width * dest.Height / src.Height, dest.Height);
526cdf0e10cSrcweir 
527cdf0e10cSrcweir 	return result;
528cdf0e10cSrcweir }
529cdf0e10cSrcweir 
5307f610566Smseidel /* vim: set noet sw=4 ts=4: */
531