Bump version to 6.4-15
[LibreOffice.git] / embedserv / source / inprocserv / inprocembobj.cxx
blob23a1df21a5790942cb92a9ae995756c85f531a80
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <sal/types.h>
22 #include <embservconst.h>
23 #include "inprocembobj.h"
25 namespace inprocserv
28 namespace {
30 void SetName( LPCOLESTR pszNameFromOutside, wchar_t*& pOwnName )
32 if ( !pszNameFromOutside )
33 return;
35 // copy the string
36 size_t nLen = 0;
37 while( pszNameFromOutside[nLen] != 0 )
38 nLen++;
40 if ( pOwnName )
42 delete[] pOwnName;
43 pOwnName = nullptr;
46 pOwnName = new wchar_t[nLen+1];
47 for ( size_t nInd = 0; nInd < nLen; nInd++ )
48 pOwnName[nInd] = pszNameFromOutside[nInd];
49 pOwnName[nLen] = 0;
52 DWORD InsertAdviseLinkToList( const ComSmart<OleWrapperAdviseSink>& pOwnAdvise, ComSmart< OleWrapperAdviseSink > pAdvises[] )
54 // the result should start from 1 in case of success, the element 0 can be used for own needs
55 DWORD nResult = 0;
57 if ( pOwnAdvise )
59 for ( DWORD nInd = 1; nInd < DEFAULT_ARRAY_LEN && nResult == 0; nInd++ )
61 if ( pAdvises[nInd] == pOwnAdvise )
63 nResult = nInd;
65 else if ( pAdvises[nInd] == nullptr )
67 pAdvises[nInd] = pOwnAdvise;
68 nResult = nInd;
73 return nResult;
78 BOOL InprocEmbedDocument_Impl::CheckDefHandler()
80 // set the own listener
81 if ( m_pOleAdvises[0] == nullptr )
83 m_pOleAdvises[0] = new OleWrapperAdviseSink();
85 else
87 if ( m_pOleAdvises[0]->IsClosed() )
89 if ( m_pDefHandler )
91 // deregister all the listeners
93 ComSmart< IOleObject > pOleObject;
94 HRESULT hr = m_pDefHandler->QueryInterface( IID_IOleObject, reinterpret_cast<void**>(&pOleObject) );
95 if ( SUCCEEDED( hr ) && pOleObject )
97 for ( DWORD nInd = 0; nInd < DEFAULT_ARRAY_LEN; nInd++ )
98 if ( m_pOleAdvises[nInd] )
100 DWORD nID = m_pOleAdvises[nInd]->GetRegID();
101 pOleObject->Unadvise( nID );
102 m_pOleAdvises[nInd]->SetRegID( 0 );
105 pOleObject->SetClientSite( nullptr );
108 ComSmart< IDataObject > pIDataObject;
109 hr = m_pDefHandler->QueryInterface( IID_IDataObject, reinterpret_cast<void**>(&pIDataObject) );
110 if ( SUCCEEDED( hr ) && pIDataObject )
112 for ( DWORD nInd = 0; nInd < DEFAULT_ARRAY_LEN; nInd++ )
113 if ( m_pDataAdvises[nInd] )
115 DWORD nID = m_pDataAdvises[nInd]->GetRegID();
116 pIDataObject->DUnadvise( nID );
117 m_pDataAdvises[nInd]->SetRegID( 0 );
121 ComSmart< IViewObject > pIViewObject;
122 hr = m_pDefHandler->QueryInterface( IID_IViewObject, reinterpret_cast<void**>(&pIViewObject) );
123 if ( SUCCEEDED( hr ) && pIViewObject )
125 if ( m_pViewAdvise )
126 pIViewObject->SetAdvise( m_pViewAdvise->GetAspect(), m_pViewAdvise->GetViewAdviseFlag(), nullptr );
129 ComSmart< IPersistStorage > pPersist;
130 hr = m_pDefHandler->QueryInterface( IID_IPersistStorage, reinterpret_cast<void**>(&pPersist) );
131 if ( SUCCEEDED( hr ) && pPersist )
133 // disconnect the old wrapper from the storage
134 pPersist->HandsOffStorage();
137 m_pDefHandler = nullptr;
140 m_pOleAdvises[0]->UnsetClosed();
144 if ( m_nCallsOnStack )
145 return FALSE;
147 if ( !m_pDefHandler )
149 // create a new default inprocess handler
150 HRESULT hr = OleCreateDefaultHandler( m_guid, nullptr, IID_IUnknown, reinterpret_cast<void**>(&m_pDefHandler) );
151 if ( SUCCEEDED( hr ) )
153 if ( m_nInitMode == INIT_FROM_STORAGE )
155 ComSmart< IPersistStorage > pPersist;
156 hr = m_pDefHandler->QueryInterface( IID_IPersistStorage, reinterpret_cast<void**>(&pPersist) );
158 ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
159 if ( SUCCEEDED( hr ) && pPersist && m_pStorage )
160 hr = pPersist->InitNew( m_pStorage.get() );
162 else if ( m_nInitMode == LOAD_FROM_STORAGE )
164 ComSmart< IPersistStorage > pPersist;
165 hr = m_pDefHandler->QueryInterface( IID_IPersistStorage, reinterpret_cast<void**>(&pPersist) );
167 ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
168 if ( SUCCEEDED( hr ) && pPersist && m_pStorage )
169 hr = pPersist->Load( m_pStorage.get() );
171 else if ( m_nInitMode == LOAD_FROM_FILE )
173 ComSmart< IPersistFile > pPersistFile;
174 hr = m_pDefHandler->QueryInterface( IID_IPersistFile, reinterpret_cast<void**>(&pPersistFile) );
176 ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
177 if ( SUCCEEDED( hr ) && pPersistFile && m_pFileName )
178 hr = pPersistFile->Load( m_pFileName, m_nFileOpenMode );
182 if ( !SUCCEEDED( hr ) || !m_pDefHandler )
184 m_pDefHandler = nullptr;
185 return FALSE;
188 // register all the listeners new
190 ComSmart< IOleObject > pOleObject;
191 hr = m_pDefHandler->QueryInterface( IID_IOleObject, reinterpret_cast<void**>(&pOleObject) );
192 if ( SUCCEEDED( hr ) && pOleObject )
194 if ( m_pClientSite )
195 pOleObject->SetClientSite( m_pClientSite.get() );
197 for ( DWORD nInd = 0; nInd < DEFAULT_ARRAY_LEN; nInd++ )
198 if ( m_pOleAdvises[nInd] )
200 DWORD nRegID = 0;
201 if ( SUCCEEDED( pOleObject->Advise( m_pOleAdvises[nInd].get(), &nRegID ) ) && nRegID > 0 )
202 m_pOleAdvises[nInd]->SetRegID( nRegID );
206 ComSmart< IDataObject > pIDataObject;
207 hr = m_pDefHandler->QueryInterface( IID_IDataObject, reinterpret_cast<void**>(&pIDataObject) );
208 if ( SUCCEEDED( hr ) && pIDataObject )
210 for ( DWORD nInd = 0; nInd < DEFAULT_ARRAY_LEN; nInd++ )
211 if ( m_pDataAdvises[nInd] )
213 DWORD nRegID = 0;
214 if ( SUCCEEDED( pIDataObject->DAdvise( m_pDataAdvises[nInd]->GetFormatEtc(), m_pDataAdvises[nInd]->GetDataAdviseFlag(), m_pDataAdvises[nInd].get(), &nRegID ) ) && nRegID > 0 )
215 m_pDataAdvises[nInd]->SetRegID( nRegID );
219 ComSmart< IViewObject > pIViewObject;
220 hr = m_pDefHandler->QueryInterface( IID_IViewObject, reinterpret_cast<void**>(&pIViewObject) );
221 if ( SUCCEEDED( hr ) && pIViewObject )
223 if ( m_pViewAdvise )
224 pIViewObject->SetAdvise( m_pViewAdvise->GetAspect(), m_pViewAdvise->GetViewAdviseFlag(), m_pViewAdvise.get() );
229 return TRUE;
232 void InprocEmbedDocument_Impl::Clean()
234 m_pDefHandler = nullptr;
236 // no DisconnectOrigAdvise() call here, since it is no explicit disconnection
237 for ( DWORD nInd = 0; nInd < DEFAULT_ARRAY_LEN; nInd++ )
239 if ( m_pOleAdvises[nInd] )
241 ComSmart< OleWrapperAdviseSink > pAdvise = m_pOleAdvises[nInd];
242 m_pOleAdvises[nInd] = nullptr;
245 if ( m_pDataAdvises[nInd] )
247 ComSmart< OleWrapperAdviseSink > pAdvise = m_pDataAdvises[nInd];
248 m_pDataAdvises[nInd] = nullptr;
252 m_pViewAdvise = nullptr;
254 m_nInitMode = NOINIT;
255 m_pStorage = nullptr;
257 if ( m_pOleContainer )
259 m_pOleContainer->LockContainer( FALSE );
260 m_pOleContainer = nullptr;
263 m_pClientSite = nullptr;
265 m_nFileOpenMode = 0;
266 if ( m_pFileName )
268 delete m_pFileName;
269 m_pFileName = nullptr;
273 // IUnknown
275 COM_DECLSPEC_NOTHROW STDMETHODIMP InprocEmbedDocument_Impl::QueryInterface( REFIID riid, void FAR* FAR* ppv )
277 if(IsEqualIID(riid, IID_IUnknown))
279 AddRef();
280 *ppv = static_cast<IUnknown*>(static_cast<IPersistStorage*>(this));
281 return S_OK;
283 else if (IsEqualIID(riid, IID_IPersist))
285 AddRef();
286 *ppv = static_cast<IPersist*>(static_cast<IPersistStorage*>(this));
287 return S_OK;
289 else if (IsEqualIID(riid, IID_IPersistStorage))
291 AddRef();
292 *ppv = static_cast<IPersistStorage*>(this);
293 return S_OK;
295 else if (IsEqualIID(riid, IID_IDataObject))
297 AddRef();
298 *ppv = static_cast<IDataObject*>(this);
299 return S_OK;
301 else if (IsEqualIID(riid, IID_IOleObject))
303 AddRef();
304 *ppv = static_cast<IOleObject*>(this);
305 return S_OK;
307 else if (IsEqualIID(riid, IID_IPersistFile))
309 AddRef();
310 *ppv = static_cast<IPersistFile*>(this);
311 return S_OK;
313 else if (IsEqualIID(riid, IID_IRunnableObject))
315 AddRef();
316 *ppv = static_cast<IRunnableObject*>(this);
317 return S_OK;
319 else if (IsEqualIID(riid, IID_IViewObject))
321 AddRef();
322 *ppv = static_cast<IViewObject*>(this);
323 return S_OK;
325 else if (IsEqualIID(riid, IID_IViewObject2))
327 AddRef();
328 *ppv = static_cast<IViewObject2*>(this);
329 return S_OK;
331 else if (IsEqualIID(riid, IID_IOleCache))
333 AddRef();
334 *ppv = static_cast<IOleCache*>(&m_aInternalCache);
335 return S_OK;
337 else if (IsEqualIID(riid, IID_IOleCache2))
339 AddRef();
340 *ppv = static_cast<IOleCache2*>(&m_aInternalCache);
341 return S_OK;
343 else if (IsEqualIID(riid, IID_IOleWindow))
345 AddRef();
346 *ppv = static_cast<IOleWindow*>(this);
347 return S_OK;
349 else if (IsEqualIID(riid, IID_IOleInPlaceObject))
351 AddRef();
352 *ppv = static_cast<IOleInPlaceObject*>(this);
353 return S_OK;
355 else if (IsEqualIID(riid, IID_IDispatch))
357 AddRef();
358 *ppv = static_cast<IDispatch*>(this);
359 return S_OK;
362 *ppv = nullptr;
363 return ResultFromScode(E_NOINTERFACE);
367 COM_DECLSPEC_NOTHROW STDMETHODIMP_(ULONG) InprocEmbedDocument_Impl::AddRef()
369 return ++m_refCount;
373 COM_DECLSPEC_NOTHROW STDMETHODIMP_(ULONG) InprocEmbedDocument_Impl::Release()
375 // unfortunately there are reentrance problems in mfc that have to be workarounded
376 sal_Int32 nCount = m_refCount > 0 ? --m_refCount : 0;
377 if ( nCount == 0 && !m_bDeleted )
379 // deleting of this object can trigger deleting of mfc objects that will try to delete this object one more time
380 m_bDeleted = TRUE;
382 Clean();
383 delete this;
385 return nCount;
388 // IPersist
390 COM_DECLSPEC_NOTHROW STDMETHODIMP InprocEmbedDocument_Impl::GetClassID( CLSID* pClassId )
392 *pClassId = m_guid;
393 return S_OK;
396 // IPersistStorage
398 COM_DECLSPEC_NOTHROW STDMETHODIMP InprocEmbedDocument_Impl::IsDirty()
400 if ( m_pDefHandler == nullptr || m_pOleAdvises[0] == nullptr || m_pOleAdvises[0]->IsClosed() )
401 return S_FALSE;
403 if ( CheckDefHandler() )
405 ComSmart< IPersistStorage > pPersist;
406 HRESULT hr = m_pDefHandler->QueryInterface( IID_IPersistStorage, reinterpret_cast<void**>(&pPersist) );
408 ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
409 if ( SUCCEEDED( hr ) && pPersist )
410 return pPersist->IsDirty();
413 return E_FAIL;
417 COM_DECLSPEC_NOTHROW STDMETHODIMP InprocEmbedDocument_Impl::InitNew( IStorage *pStg )
419 if ( CheckDefHandler() )
421 ComSmart< IPersistStorage > pPersist;
422 HRESULT hr = m_pDefHandler->QueryInterface( IID_IPersistStorage, reinterpret_cast<void**>(&pPersist) );
424 ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
425 if ( SUCCEEDED( hr ) && pPersist )
427 hr = pPersist->InitNew( pStg );
428 if ( SUCCEEDED( hr ) )
430 m_nInitMode = INIT_FROM_STORAGE;
431 m_pStorage = pStg;
433 m_nFileOpenMode = 0;
434 if ( m_pFileName )
436 delete[] m_pFileName;
437 m_pFileName = nullptr;
441 return hr;
445 return E_FAIL;
449 COM_DECLSPEC_NOTHROW STDMETHODIMP InprocEmbedDocument_Impl::Load( IStorage *pStg )
451 if ( CheckDefHandler() )
453 ComSmart< IPersistStorage > pPersist;
454 HRESULT hr = m_pDefHandler->QueryInterface( IID_IPersistStorage, reinterpret_cast<void**>(&pPersist) );
456 ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
457 if ( SUCCEEDED( hr ) && pPersist )
459 hr = pPersist->Load( pStg );
460 if ( SUCCEEDED( hr ) )
462 m_nInitMode = LOAD_FROM_STORAGE;
463 m_pStorage = pStg;
465 m_nFileOpenMode = 0;
466 if ( m_pFileName )
468 delete[] m_pFileName;
469 m_pFileName = nullptr;
473 return hr;
477 return E_FAIL;
481 COM_DECLSPEC_NOTHROW STDMETHODIMP InprocEmbedDocument_Impl::Save( IStorage *pStgSave, BOOL fSameAsLoad )
483 if ( fSameAsLoad && ( m_pDefHandler == nullptr || m_pOleAdvises[0] == nullptr || m_pOleAdvises[0]->IsClosed() ) )
484 return S_OK;
486 if ( CheckDefHandler() )
488 ComSmart< IPersistStorage > pPersist;
489 HRESULT hr = m_pDefHandler->QueryInterface( IID_IPersistStorage, reinterpret_cast<void**>(&pPersist) );
491 ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
492 if ( SUCCEEDED( hr ) && pPersist )
493 return pPersist->Save( pStgSave, fSameAsLoad );
496 return E_FAIL;
500 COM_DECLSPEC_NOTHROW STDMETHODIMP InprocEmbedDocument_Impl::SaveCompleted( IStorage *pStgNew )
502 if ( m_pDefHandler == nullptr || m_pOleAdvises[0] == nullptr || m_pOleAdvises[0]->IsClosed() )
504 if ( pStgNew )
505 m_pStorage = pStgNew;
507 return S_OK;
510 if ( CheckDefHandler() )
512 ComSmart< IPersistStorage > pPersist;
513 HRESULT hr = m_pDefHandler->QueryInterface( IID_IPersistStorage, reinterpret_cast<void**>(&pPersist) );
515 ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
516 if ( SUCCEEDED( hr ) && pPersist )
518 hr = pPersist->SaveCompleted( pStgNew );
519 if ( SUCCEEDED( hr ) )
521 m_nInitMode = LOAD_FROM_STORAGE;
522 if ( pStgNew )
524 m_pStorage = pStgNew;
527 m_nFileOpenMode = 0;
528 if ( m_pFileName )
530 delete[] m_pFileName;
531 m_pFileName = nullptr;
535 return hr;
539 return E_FAIL;
543 COM_DECLSPEC_NOTHROW STDMETHODIMP InprocEmbedDocument_Impl::HandsOffStorage()
545 if ( CheckDefHandler() )
547 ComSmart< IPersistStorage > pPersist;
548 HRESULT hr = m_pDefHandler->QueryInterface( IID_IPersistStorage, reinterpret_cast<void**>(&pPersist) );
550 ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
551 if ( SUCCEEDED( hr ) && pPersist )
553 hr = pPersist->HandsOffStorage();
554 if ( SUCCEEDED( hr ) )
556 m_pStorage = nullptr;
559 return hr;
563 return E_FAIL;
566 // IPersistFile
568 COM_DECLSPEC_NOTHROW STDMETHODIMP InprocEmbedDocument_Impl::Load( LPCOLESTR pszFileName, DWORD dwMode )
570 if ( CheckDefHandler() && pszFileName )
572 ComSmart< IPersistFile > pPersist;
573 HRESULT hr = m_pDefHandler->QueryInterface( IID_IPersistFile, reinterpret_cast<void**>(&pPersist) );
575 ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
576 if ( SUCCEEDED( hr ) && pPersist )
578 hr = pPersist->Load( pszFileName, dwMode );
579 if ( SUCCEEDED( hr ) )
581 m_nInitMode = LOAD_FROM_FILE;
582 if ( m_pStorage )
583 m_pStorage = nullptr;
585 m_nFileOpenMode = dwMode;
586 // copy the string
587 SetName( pszFileName, m_pFileName );
590 return hr;
594 return E_FAIL;
598 COM_DECLSPEC_NOTHROW STDMETHODIMP InprocEmbedDocument_Impl::Save( LPCOLESTR pszFileName, BOOL fRemember )
600 if ( CheckDefHandler() )
602 ComSmart< IPersistFile > pPersist;
603 HRESULT hr = m_pDefHandler->QueryInterface( IID_IPersistFile, reinterpret_cast<void**>(&pPersist) );
605 ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
606 if ( SUCCEEDED( hr ) && pPersist )
607 return pPersist->Save( pszFileName, fRemember );
610 return E_FAIL;
614 COM_DECLSPEC_NOTHROW STDMETHODIMP InprocEmbedDocument_Impl::SaveCompleted( LPCOLESTR pszFileName )
616 if ( CheckDefHandler() )
618 ComSmart< IPersistFile > pPersist;
619 HRESULT hr = m_pDefHandler->QueryInterface( IID_IPersistFile, reinterpret_cast<void**>(&pPersist) );
621 ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
622 if ( SUCCEEDED( hr ) && pPersist )
624 hr = pPersist->SaveCompleted( pszFileName );
625 if ( SUCCEEDED( hr ) )
627 m_nInitMode = LOAD_FROM_STORAGE;
628 if ( m_pStorage )
629 m_pStorage = nullptr;
631 m_nFileOpenMode = STGM_READWRITE; // was just written
632 // copy the string
633 SetName( pszFileName, m_pFileName );
639 return E_FAIL;
643 COM_DECLSPEC_NOTHROW STDMETHODIMP InprocEmbedDocument_Impl::GetCurFile( LPOLESTR *ppszFileName )
645 if ( CheckDefHandler() )
647 ComSmart< IPersistFile > pPersist;
648 HRESULT hr = m_pDefHandler->QueryInterface( IID_IPersistFile, reinterpret_cast<void**>(&pPersist) );
650 ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
651 if ( SUCCEEDED( hr ) && pPersist )
652 return pPersist->GetCurFile( ppszFileName );
655 return E_FAIL;
658 // IOleObject
660 COM_DECLSPEC_NOTHROW STDMETHODIMP InprocEmbedDocument_Impl::SetClientSite( IOleClientSite* pSite )
662 if ( pSite == m_pClientSite.get() )
663 return S_OK;
665 if ( !pSite )
667 m_pClientSite = nullptr;
668 if ( m_pOleContainer )
670 m_pOleContainer->LockContainer( FALSE );
671 m_pOleContainer = nullptr;
675 if ( CheckDefHandler() )
677 ComSmart< IOleObject > pOleObject;
678 HRESULT hr = m_pDefHandler->QueryInterface( IID_IOleObject, reinterpret_cast<void**>(&pOleObject) );
680 ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
681 if ( SUCCEEDED( hr ) && pOleObject )
683 HRESULT hr2 = pOleObject->SetClientSite( pSite );
684 if ( SUCCEEDED( hr2 ) )
686 m_pClientSite = pSite;
688 if ( m_pOleContainer )
690 m_pOleContainer->LockContainer( FALSE );
691 m_pOleContainer = nullptr;
694 m_pClientSite->GetContainer( &m_pOleContainer );
695 if ( m_pOleContainer )
696 m_pOleContainer->LockContainer( TRUE );
699 return hr2;
703 return E_FAIL;
707 COM_DECLSPEC_NOTHROW STDMETHODIMP InprocEmbedDocument_Impl::GetClientSite( IOleClientSite** pSite )
709 if ( CheckDefHandler() )
711 ComSmart< IOleObject > pOleObject;
712 HRESULT hr = m_pDefHandler->QueryInterface( IID_IOleObject, reinterpret_cast<void**>(&pOleObject) );
714 ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
715 if ( SUCCEEDED( hr ) && pOleObject )
716 return pOleObject->GetClientSite( pSite );
719 return E_FAIL;
723 COM_DECLSPEC_NOTHROW STDMETHODIMP InprocEmbedDocument_Impl::SetHostNames( LPCOLESTR szContainerApp, LPCOLESTR szContainerObj )
726 if ( CheckDefHandler() )
728 ComSmart< IOleObject > pOleObject;
729 HRESULT hr = m_pDefHandler->QueryInterface( IID_IOleObject, reinterpret_cast<void**>(&pOleObject) );
731 ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
732 if ( SUCCEEDED( hr ) && pOleObject )
734 hr = pOleObject->SetHostNames( szContainerApp, szContainerObj );
738 return S_OK;
742 COM_DECLSPEC_NOTHROW STDMETHODIMP InprocEmbedDocument_Impl::Close( DWORD dwSaveOption )
744 HRESULT ret = S_OK;
745 if ( m_pDefHandler && CheckDefHandler() )
747 // no need to close if there is no default handler.
748 ComSmart< IOleObject > pOleObject;
749 HRESULT hr = m_pDefHandler->QueryInterface( IID_IOleObject, reinterpret_cast<void**>(&pOleObject) );
751 ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
752 if ( SUCCEEDED( hr ) && pOleObject )
754 hr = pOleObject->Close( dwSaveOption );
755 if (!SUCCEEDED(hr))
756 ret = hr;
757 hr = CoDisconnectObject( static_cast<IUnknown*>(static_cast<IPersistStorage*>(this)), 0 );
758 if (!SUCCEEDED(hr) && SUCCEEDED(ret))
759 ret = hr;
763 // if the object is closed from outside that means that it should go to uninitialized state
764 Clean();
766 return ret;
770 COM_DECLSPEC_NOTHROW STDMETHODIMP InprocEmbedDocument_Impl::SetMoniker( DWORD dwWhichMoniker, IMoniker * pmk )
772 if ( CheckDefHandler() )
774 ComSmart< IOleObject > pOleObject;
775 HRESULT hr = m_pDefHandler->QueryInterface( IID_IOleObject, reinterpret_cast<void**>(&pOleObject) );
777 ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
778 if ( SUCCEEDED( hr ) && pOleObject )
779 return pOleObject->SetMoniker( dwWhichMoniker, pmk );
782 return E_FAIL;
786 COM_DECLSPEC_NOTHROW STDMETHODIMP InprocEmbedDocument_Impl::GetMoniker( DWORD dwAssign, DWORD dwWhichMoniker, IMoniker ** ppmk )
788 if ( CheckDefHandler() )
790 ComSmart< IOleObject > pOleObject;
791 HRESULT hr = m_pDefHandler->QueryInterface( IID_IOleObject, reinterpret_cast<void**>(&pOleObject) );
793 ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
794 if ( SUCCEEDED( hr ) && pOleObject )
795 return pOleObject->GetMoniker( dwAssign, dwWhichMoniker, ppmk );
798 return E_FAIL;
802 COM_DECLSPEC_NOTHROW STDMETHODIMP InprocEmbedDocument_Impl::InitFromData( IDataObject * pDataObject, BOOL fCreation, DWORD dwReserved )
804 if ( CheckDefHandler() )
806 ComSmart< IOleObject > pOleObject;
807 HRESULT hr = m_pDefHandler->QueryInterface( IID_IOleObject, reinterpret_cast<void**>(&pOleObject) );
809 ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
810 if ( SUCCEEDED( hr ) && pOleObject )
811 return pOleObject->InitFromData( pDataObject, fCreation, dwReserved );
814 return E_FAIL;
818 COM_DECLSPEC_NOTHROW STDMETHODIMP InprocEmbedDocument_Impl::GetClipboardData( DWORD dwReserved, IDataObject ** ppDataObject )
820 if ( CheckDefHandler() )
822 ComSmart< IOleObject > pOleObject;
823 HRESULT hr = m_pDefHandler->QueryInterface( IID_IOleObject, reinterpret_cast<void**>(&pOleObject) );
825 ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
826 if ( SUCCEEDED( hr ) && pOleObject )
827 return pOleObject->GetClipboardData( dwReserved, ppDataObject );
830 return E_FAIL;
834 COM_DECLSPEC_NOTHROW STDMETHODIMP InprocEmbedDocument_Impl::DoVerb(
835 LONG iVerb,
836 LPMSG pMsg,
837 IOleClientSite *pActiveSite,
838 LONG nLong,
839 HWND hWin,
840 LPCRECT pRect )
842 if ( CheckDefHandler() )
844 ComSmart< IOleObject > pOleObject;
845 HRESULT hr = m_pDefHandler->QueryInterface( IID_IOleObject, reinterpret_cast<void**>(&pOleObject) );
847 ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
848 if ( SUCCEEDED( hr ) && pOleObject )
850 hr = pOleObject->DoVerb( iVerb, pMsg, pActiveSite, nLong, hWin, pRect );
851 return hr;
856 return E_FAIL;
860 COM_DECLSPEC_NOTHROW STDMETHODIMP InprocEmbedDocument_Impl::EnumVerbs( IEnumOLEVERB ** ppEnumOleVerb )
862 if ( CheckDefHandler() )
864 ComSmart< IOleObject > pOleObject;
865 HRESULT hr = m_pDefHandler->QueryInterface( IID_IOleObject, reinterpret_cast<void**>(&pOleObject) );
867 ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
868 if ( SUCCEEDED( hr ) && pOleObject )
869 return pOleObject->EnumVerbs( ppEnumOleVerb );
872 return E_FAIL;
876 COM_DECLSPEC_NOTHROW STDMETHODIMP InprocEmbedDocument_Impl::Update()
879 if ( m_pDefHandler && CheckDefHandler() )
881 ComSmart< IOleObject > pOleObject;
882 HRESULT hr = m_pDefHandler->QueryInterface( IID_IOleObject, reinterpret_cast<void**>(&pOleObject) );
884 ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
885 if ( SUCCEEDED( hr ) && pOleObject )
886 return pOleObject->Update();
889 return S_OK;
893 COM_DECLSPEC_NOTHROW STDMETHODIMP InprocEmbedDocument_Impl::IsUpToDate()
895 if ( CheckDefHandler() )
897 ComSmart< IOleObject > pOleObject;
898 HRESULT hr = m_pDefHandler->QueryInterface( IID_IOleObject, reinterpret_cast<void**>(&pOleObject) );
900 ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
901 if ( SUCCEEDED( hr ) && pOleObject )
902 return pOleObject->IsUpToDate();
905 return E_FAIL;
909 COM_DECLSPEC_NOTHROW STDMETHODIMP InprocEmbedDocument_Impl::GetUserClassID( CLSID *pClsid )
911 if ( pClsid )
912 *pClsid = m_guid;
914 return S_OK;
918 COM_DECLSPEC_NOTHROW STDMETHODIMP InprocEmbedDocument_Impl::GetUserType( DWORD dwFormOfType, LPOLESTR * pszUserType )
920 if ( CheckDefHandler() )
922 ComSmart< IOleObject > pOleObject;
923 HRESULT hr = m_pDefHandler->QueryInterface( IID_IOleObject, reinterpret_cast<void**>(&pOleObject) );
925 ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
926 if ( SUCCEEDED( hr ) && pOleObject )
927 return pOleObject->GetUserType( dwFormOfType, pszUserType );
930 return E_FAIL;
934 COM_DECLSPEC_NOTHROW STDMETHODIMP InprocEmbedDocument_Impl::SetExtent( DWORD dwDrawAspect, SIZEL *psizel )
936 if ( CheckDefHandler() )
938 ComSmart< IOleObject > pOleObject;
939 HRESULT hr = m_pDefHandler->QueryInterface( IID_IOleObject, reinterpret_cast<void**>(&pOleObject) );
941 ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
942 if ( SUCCEEDED( hr ) && pOleObject )
943 return pOleObject->SetExtent( dwDrawAspect, psizel );
946 return E_FAIL;
950 COM_DECLSPEC_NOTHROW STDMETHODIMP InprocEmbedDocument_Impl::GetExtent( DWORD dwDrawAspect, SIZEL * psizel )
952 if ( CheckDefHandler() )
954 ComSmart< IOleObject > pOleObject;
955 HRESULT hr = m_pDefHandler->QueryInterface( IID_IOleObject, reinterpret_cast<void**>(&pOleObject) );
957 ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
958 if ( SUCCEEDED( hr ) && pOleObject )
959 return pOleObject->GetExtent( dwDrawAspect, psizel );
962 return E_FAIL;
966 COM_DECLSPEC_NOTHROW STDMETHODIMP InprocEmbedDocument_Impl::Advise( IAdviseSink *pAdvSink, DWORD *pdwConnection )
969 if ( !pdwConnection )
970 return E_FAIL;
972 // CheckDefHandler will set the listener, avoid reusing of old listener
973 if ( DEFAULT_ARRAY_LEN > *pdwConnection && *pdwConnection > 0 && m_pOleAdvises[*pdwConnection] )
975 m_pOleAdvises[*pdwConnection]->DisconnectOrigAdvise();
976 m_pOleAdvises[*pdwConnection] = nullptr;
979 if ( pAdvSink && CheckDefHandler() )
981 ComSmart< IOleObject > pOleObject;
982 HRESULT hr = m_pDefHandler->QueryInterface( IID_IOleObject, reinterpret_cast<void**>(&pOleObject) );
984 ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
985 if ( SUCCEEDED( hr ) && pOleObject )
987 ComSmart<IAdviseSink> aListener(pAdvSink);
988 ComSmart<OleWrapperAdviseSink> pOwnAdvise(new OleWrapperAdviseSink(aListener));
989 DWORD nRegID = 0;
991 if ( SUCCEEDED( pOleObject->Advise( pOwnAdvise.get(), &nRegID ) ) && nRegID > 0 )
993 pOwnAdvise->SetRegID( nRegID );
994 *pdwConnection = InsertAdviseLinkToList( pOwnAdvise, m_pOleAdvises );
995 if ( *pdwConnection )
996 return S_OK;
997 else
998 pOleObject->Unadvise( nRegID );
1003 // return success always for now
1004 return S_OK;
1008 COM_DECLSPEC_NOTHROW STDMETHODIMP InprocEmbedDocument_Impl::Unadvise( DWORD dwConnection )
1010 if ( DEFAULT_ARRAY_LEN > dwConnection && dwConnection > 0 && m_pOleAdvises[dwConnection] )
1012 if ( m_pDefHandler )
1014 ComSmart< IOleObject > pOleObject;
1015 HRESULT hr = m_pDefHandler->QueryInterface( IID_IOleObject, reinterpret_cast<void**>(&pOleObject) );
1017 ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
1018 if ( SUCCEEDED( hr ) && pOleObject )
1020 DWORD nID = m_pOleAdvises[dwConnection]->GetRegID();
1021 pOleObject->Unadvise( nID );
1025 m_pOleAdvises[dwConnection]->DisconnectOrigAdvise();
1026 m_pOleAdvises[dwConnection] = nullptr;
1028 return S_OK;
1031 return E_FAIL;
1035 COM_DECLSPEC_NOTHROW STDMETHODIMP InprocEmbedDocument_Impl::EnumAdvise( IEnumSTATDATA ** /*ppenumAdvise*/ )
1037 return E_NOTIMPL;
1041 COM_DECLSPEC_NOTHROW STDMETHODIMP InprocEmbedDocument_Impl::GetMiscStatus( DWORD dwAspect, DWORD * pdwStatus )
1043 if ( CheckDefHandler() )
1045 ComSmart< IOleObject > pOleObject;
1046 HRESULT hr = m_pDefHandler->QueryInterface( IID_IOleObject, reinterpret_cast<void**>(&pOleObject) );
1048 ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
1049 if ( SUCCEEDED( hr ) && pOleObject )
1050 return pOleObject->GetMiscStatus( dwAspect, pdwStatus );
1053 return E_FAIL;
1057 COM_DECLSPEC_NOTHROW STDMETHODIMP InprocEmbedDocument_Impl::SetColorScheme( LOGPALETTE * pLogpal )
1059 if ( CheckDefHandler() )
1061 ComSmart< IOleObject > pOleObject;
1062 HRESULT hr = m_pDefHandler->QueryInterface( IID_IOleObject, reinterpret_cast<void**>(&pOleObject) );
1064 ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
1065 if ( SUCCEEDED( hr ) && pOleObject )
1066 return pOleObject->SetColorScheme( pLogpal );
1069 return E_FAIL;
1072 //IDataObject
1074 COM_DECLSPEC_NOTHROW STDMETHODIMP InprocEmbedDocument_Impl::GetData( FORMATETC * pFormatetc, STGMEDIUM * pMedium )
1076 if ( CheckDefHandler() )
1078 ComSmart< IDataObject > pIDataObject;
1079 HRESULT hr = m_pDefHandler->QueryInterface( IID_IDataObject, reinterpret_cast<void**>(&pIDataObject) );
1081 ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
1082 if ( SUCCEEDED( hr ) && pIDataObject )
1083 return pIDataObject->GetData( pFormatetc, pMedium );
1086 return E_FAIL;
1090 COM_DECLSPEC_NOTHROW STDMETHODIMP InprocEmbedDocument_Impl::GetDataHere( FORMATETC * pFormatetc, STGMEDIUM * pMedium )
1092 if ( CheckDefHandler() )
1094 ComSmart< IDataObject > pIDataObject;
1095 HRESULT hr = m_pDefHandler->QueryInterface( IID_IDataObject, reinterpret_cast<void**>(&pIDataObject) );
1097 ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
1098 if ( SUCCEEDED( hr ) && pIDataObject )
1099 return pIDataObject->GetDataHere( pFormatetc, pMedium );
1102 return E_FAIL;
1106 COM_DECLSPEC_NOTHROW STDMETHODIMP InprocEmbedDocument_Impl::QueryGetData( FORMATETC * pFormatetc )
1108 if ( CheckDefHandler() )
1110 ComSmart< IDataObject > pIDataObject;
1111 HRESULT hr = m_pDefHandler->QueryInterface( IID_IDataObject, reinterpret_cast<void**>(&pIDataObject) );
1113 ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
1114 if ( SUCCEEDED( hr ) && pIDataObject )
1115 return pIDataObject->QueryGetData( pFormatetc );
1118 return E_FAIL;
1122 COM_DECLSPEC_NOTHROW STDMETHODIMP InprocEmbedDocument_Impl::GetCanonicalFormatEtc( FORMATETC * pFormatetcIn, FORMATETC * pFormatetcOut )
1124 if ( CheckDefHandler() )
1126 ComSmart< IDataObject > pIDataObject;
1127 HRESULT hr = m_pDefHandler->QueryInterface( IID_IDataObject, reinterpret_cast<void**>(&pIDataObject) );
1129 ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
1130 if ( SUCCEEDED( hr ) && pIDataObject )
1131 return pIDataObject->GetCanonicalFormatEtc( pFormatetcIn, pFormatetcOut );
1134 return E_FAIL;
1138 COM_DECLSPEC_NOTHROW STDMETHODIMP InprocEmbedDocument_Impl::SetData( FORMATETC * pFormatetc, STGMEDIUM * pMedium, BOOL fRelease )
1140 if ( CheckDefHandler() )
1142 ComSmart< IDataObject > pIDataObject;
1143 HRESULT hr = m_pDefHandler->QueryInterface( IID_IDataObject, reinterpret_cast<void**>(&pIDataObject) );
1145 ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
1146 if ( SUCCEEDED( hr ) && pIDataObject )
1147 return pIDataObject->SetData( pFormatetc, pMedium, fRelease );
1150 return E_FAIL;
1154 COM_DECLSPEC_NOTHROW STDMETHODIMP InprocEmbedDocument_Impl::EnumFormatEtc( DWORD dwDirection, IEnumFORMATETC ** ppFormatetc )
1156 if ( CheckDefHandler() )
1158 ComSmart< IDataObject > pIDataObject;
1159 HRESULT hr = m_pDefHandler->QueryInterface( IID_IDataObject, reinterpret_cast<void**>(&pIDataObject) );
1161 ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
1162 if ( SUCCEEDED( hr ) && pIDataObject )
1163 return pIDataObject->EnumFormatEtc( dwDirection, ppFormatetc );
1166 return E_FAIL;
1170 COM_DECLSPEC_NOTHROW STDMETHODIMP InprocEmbedDocument_Impl::DAdvise( FORMATETC * pFormatetc, DWORD advf, IAdviseSink * pAdvSink, DWORD * pdwConnection )
1173 if ( !pdwConnection )
1174 return E_FAIL;
1176 // avoid reusing of the old listener
1177 if ( m_pDefHandler && DEFAULT_ARRAY_LEN > *pdwConnection && *pdwConnection > 0 && m_pDataAdvises[*pdwConnection] )
1179 m_pDataAdvises[*pdwConnection]->DisconnectOrigAdvise();
1180 m_pDataAdvises[*pdwConnection] = nullptr;
1183 if ( pAdvSink && CheckDefHandler() )
1185 ComSmart< IDataObject > pIDataObject;
1186 HRESULT hr = m_pDefHandler->QueryInterface( IID_IDataObject, reinterpret_cast<void**>(&pIDataObject) );
1188 ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
1189 if ( SUCCEEDED( hr ) && pIDataObject )
1191 ComSmart< OleWrapperAdviseSink > pOwnAdvise( new OleWrapperAdviseSink( ComSmart<IAdviseSink>( pAdvSink ), pFormatetc, advf ) );
1192 DWORD nRegID = 0;
1194 if ( SUCCEEDED( pIDataObject->DAdvise( pFormatetc, advf, pOwnAdvise.get(), &nRegID ) ) && nRegID > 0 )
1196 pOwnAdvise->SetRegID( nRegID );
1197 *pdwConnection = InsertAdviseLinkToList( pOwnAdvise, m_pDataAdvises );
1198 if ( *pdwConnection )
1199 return S_OK;
1200 else
1201 pIDataObject->DUnadvise( nRegID );
1206 // return success always for now
1207 return S_OK;
1211 COM_DECLSPEC_NOTHROW STDMETHODIMP InprocEmbedDocument_Impl::DUnadvise( DWORD dwConnection )
1213 if ( m_pDefHandler && DEFAULT_ARRAY_LEN > dwConnection && dwConnection > 0 && m_pDataAdvises[dwConnection] )
1215 if ( CheckDefHandler() )
1217 ComSmart< IDataObject > pIDataObject;
1218 HRESULT hr = m_pDefHandler->QueryInterface( IID_IDataObject, reinterpret_cast<void**>(&pIDataObject) );
1220 ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
1221 if ( SUCCEEDED( hr ) && pIDataObject )
1223 DWORD nID = m_pDataAdvises[dwConnection]->GetRegID();
1224 pIDataObject->DUnadvise( nID );
1228 m_pDataAdvises[dwConnection]->DisconnectOrigAdvise();
1229 m_pDataAdvises[dwConnection] = nullptr;
1231 return S_OK;
1234 return E_FAIL;
1238 COM_DECLSPEC_NOTHROW STDMETHODIMP InprocEmbedDocument_Impl::EnumDAdvise( IEnumSTATDATA ** ppenumAdvise )
1240 if ( CheckDefHandler() )
1242 ComSmart< IDataObject > pIDataObject;
1243 HRESULT hr = m_pDefHandler->QueryInterface( IID_IDataObject, reinterpret_cast<void**>(&pIDataObject) );
1245 ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
1246 if ( SUCCEEDED( hr ) && pIDataObject )
1247 return pIDataObject->EnumDAdvise( ppenumAdvise );
1250 return E_FAIL;
1253 // IRunnableObject
1255 COM_DECLSPEC_NOTHROW STDMETHODIMP InprocEmbedDocument_Impl::GetRunningClass( LPCLSID lpClsid )
1257 if ( CheckDefHandler() )
1259 ComSmart< IRunnableObject > pIRunObj;
1260 HRESULT hr = m_pDefHandler->QueryInterface( IID_IRunnableObject, reinterpret_cast<void**>(&pIRunObj) );
1262 ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
1263 if ( SUCCEEDED( hr ) && pIRunObj )
1264 return pIRunObj->GetRunningClass( lpClsid );
1267 return E_FAIL;
1271 COM_DECLSPEC_NOTHROW STDMETHODIMP InprocEmbedDocument_Impl::Run( LPBINDCTX pbc )
1273 if ( CheckDefHandler() )
1275 ComSmart< IRunnableObject > pIRunObj;
1276 HRESULT hr = m_pDefHandler->QueryInterface( IID_IRunnableObject, reinterpret_cast<void**>(&pIRunObj) );
1278 ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
1279 if ( SUCCEEDED( hr ) && pIRunObj )
1280 return pIRunObj->Run( pbc );
1283 return E_FAIL;
1286 BOOL STDMETHODCALLTYPE InprocEmbedDocument_Impl::IsRunning()
1288 if (CheckDefHandler())
1290 ComSmart< IRunnableObject > pIRunObj;
1291 HRESULT hr = m_pDefHandler->QueryInterface( IID_IRunnableObject, reinterpret_cast<void**>(&pIRunObj) );
1293 ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
1294 if ( SUCCEEDED( hr ) && pIRunObj )
1295 return pIRunObj->IsRunning();
1298 return FALSE;
1301 COM_DECLSPEC_NOTHROW STDMETHODIMP InprocEmbedDocument_Impl::LockRunning( BOOL fLock, BOOL fLastUnlockCloses )
1303 if ( CheckDefHandler() )
1305 ComSmart< IRunnableObject > pIRunObj;
1306 HRESULT hr = m_pDefHandler->QueryInterface( IID_IRunnableObject, reinterpret_cast<void**>(&pIRunObj) );
1308 ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
1309 if ( SUCCEEDED( hr ) && pIRunObj )
1310 return pIRunObj->LockRunning( fLock, fLastUnlockCloses );
1313 return E_FAIL;
1317 COM_DECLSPEC_NOTHROW STDMETHODIMP InprocEmbedDocument_Impl::SetContainedObject( BOOL fContained)
1319 if ( CheckDefHandler() )
1321 ComSmart< IRunnableObject > pIRunObj;
1322 HRESULT hr = m_pDefHandler->QueryInterface( IID_IRunnableObject, reinterpret_cast<void**>(&pIRunObj) );
1324 ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
1325 if ( SUCCEEDED( hr ) && pIRunObj )
1326 return pIRunObj->SetContainedObject( fContained );
1329 return E_FAIL;
1333 // IViewObject methods
1335 COM_DECLSPEC_NOTHROW STDMETHODIMP InprocEmbedDocument_Impl::Draw( DWORD dwDrawAspect, LONG lindex, void *pvAspect, DVTARGETDEVICE *ptd, HDC hdcTargetDev, HDC hdcDraw, LPCRECTL lprcBounds, LPCRECTL lprcWBounds, BOOL ( STDMETHODCALLTYPE *pfnContinue )( ULONG_PTR dwContinue ), ULONG_PTR dwContinue )
1337 if ( CheckDefHandler() )
1339 ComSmart< IViewObject > pIViewObject;
1340 HRESULT hr = m_pDefHandler->QueryInterface( IID_IViewObject, reinterpret_cast<void**>(&pIViewObject) );
1342 ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
1343 if ( SUCCEEDED( hr ) && pIViewObject )
1344 return pIViewObject->Draw( dwDrawAspect, lindex, pvAspect, ptd, hdcTargetDev, hdcDraw, lprcBounds, lprcWBounds, pfnContinue, dwContinue );
1347 return E_FAIL;
1351 COM_DECLSPEC_NOTHROW STDMETHODIMP InprocEmbedDocument_Impl::GetColorSet( DWORD dwDrawAspect, LONG lindex, void *pvAspect, DVTARGETDEVICE *ptd, HDC hicTargetDev, LOGPALETTE **ppColorSet )
1353 if ( CheckDefHandler() )
1355 ComSmart< IViewObject > pIViewObject;
1356 HRESULT hr = m_pDefHandler->QueryInterface( IID_IViewObject, reinterpret_cast<void**>(&pIViewObject) );
1358 ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
1359 if ( SUCCEEDED( hr ) && pIViewObject )
1360 return pIViewObject->GetColorSet( dwDrawAspect, lindex, pvAspect, ptd, hicTargetDev, ppColorSet );
1363 return E_FAIL;
1367 COM_DECLSPEC_NOTHROW STDMETHODIMP InprocEmbedDocument_Impl::Freeze( DWORD dwDrawAspect, LONG lindex, void *pvAspect, DWORD *pdwFreeze )
1369 if ( CheckDefHandler() )
1371 ComSmart< IViewObject > pIViewObject;
1372 HRESULT hr = m_pDefHandler->QueryInterface( IID_IViewObject, reinterpret_cast<void**>(&pIViewObject) );
1374 ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
1375 if ( SUCCEEDED( hr ) && pIViewObject )
1376 return pIViewObject->Freeze( dwDrawAspect, lindex, pvAspect, pdwFreeze );
1379 return E_FAIL;
1383 COM_DECLSPEC_NOTHROW STDMETHODIMP InprocEmbedDocument_Impl::Unfreeze( DWORD dwFreeze )
1385 if ( CheckDefHandler() )
1387 ComSmart< IViewObject > pIViewObject;
1388 HRESULT hr = m_pDefHandler->QueryInterface( IID_IViewObject, reinterpret_cast<void**>(&pIViewObject) );
1390 ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
1391 if ( SUCCEEDED( hr ) && pIViewObject )
1392 return pIViewObject->Unfreeze( dwFreeze );
1395 return E_FAIL;
1399 COM_DECLSPEC_NOTHROW STDMETHODIMP InprocEmbedDocument_Impl::SetAdvise( DWORD aspects, DWORD advf, IAdviseSink *pAdvSink )
1402 // CheckDefHandler will set the listener, avoid reusing of old listener
1403 if ( m_pViewAdvise )
1405 m_pViewAdvise->DisconnectOrigAdvise();
1406 m_pViewAdvise = nullptr;
1409 if ( pAdvSink && CheckDefHandler() )
1411 ComSmart< IViewObject > pIViewObject;
1412 HRESULT hr = m_pDefHandler->QueryInterface( IID_IViewObject, reinterpret_cast<void**>(&pIViewObject) );
1414 ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
1415 if ( SUCCEEDED( hr ) && pIViewObject )
1417 ComSmart<IAdviseSink> aListener(pAdvSink);
1418 ComSmart<OleWrapperAdviseSink> pOwnAdvise(new OleWrapperAdviseSink(aListener, aspects, advf));
1420 if ( SUCCEEDED( pIViewObject->SetAdvise( aspects, advf, pOwnAdvise.get() ) ) )
1422 m_pViewAdvise = pOwnAdvise;
1423 return S_OK;
1428 return S_OK;
1432 COM_DECLSPEC_NOTHROW STDMETHODIMP InprocEmbedDocument_Impl::GetAdvise( DWORD *pAspects, DWORD *pAdvf, IAdviseSink **ppAdvSink )
1434 if ( !ppAdvSink )
1435 return E_INVALIDARG;
1437 if ( m_pViewAdvise )
1439 if ( pAspects )
1440 *pAspects = m_pViewAdvise->GetAspect();
1442 if ( pAdvf )
1443 *pAdvf = m_pViewAdvise->GetViewAdviseFlag();
1445 *ppAdvSink = m_pViewAdvise->GetOrigAdvise().get();
1446 if ( *ppAdvSink )
1447 (*ppAdvSink)->AddRef();
1449 else
1450 *ppAdvSink = nullptr;
1452 return S_OK;
1455 // IViewObject2 methods
1457 COM_DECLSPEC_NOTHROW STDMETHODIMP InprocEmbedDocument_Impl::GetExtent( DWORD dwDrawAspect, LONG lindex, DVTARGETDEVICE *ptd, LPSIZEL lpsizel )
1459 if ( CheckDefHandler() )
1461 ComSmart< IViewObject2 > pIViewObject2;
1462 HRESULT hr = m_pDefHandler->QueryInterface( IID_IViewObject2, reinterpret_cast<void**>(&pIViewObject2) );
1464 ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
1465 if ( SUCCEEDED( hr ) && pIViewObject2 )
1466 return pIViewObject2->GetExtent( dwDrawAspect, lindex, ptd, lpsizel );
1469 return E_FAIL;
1473 // IOleWindow methods
1475 COM_DECLSPEC_NOTHROW STDMETHODIMP InprocEmbedDocument_Impl::GetWindow( HWND *phwnd )
1477 if ( CheckDefHandler() )
1479 ComSmart< IOleWindow > pIOleWindow;
1480 HRESULT hr = m_pDefHandler->QueryInterface( IID_IOleWindow, reinterpret_cast<void**>(&pIOleWindow) );
1482 ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
1483 if ( SUCCEEDED( hr ) && pIOleWindow )
1484 return pIOleWindow->GetWindow( phwnd );
1487 return E_FAIL;
1491 COM_DECLSPEC_NOTHROW STDMETHODIMP InprocEmbedDocument_Impl::ContextSensitiveHelp( BOOL fEnterMode )
1493 if ( CheckDefHandler() )
1495 ComSmart< IOleWindow > pIOleWindow;
1496 HRESULT hr = m_pDefHandler->QueryInterface( IID_IOleWindow, reinterpret_cast<void**>(&pIOleWindow) );
1498 ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
1499 if ( SUCCEEDED( hr ) && pIOleWindow )
1500 return pIOleWindow->ContextSensitiveHelp( fEnterMode );
1503 return E_FAIL;
1507 // IOleInPlaceObject methods
1509 COM_DECLSPEC_NOTHROW STDMETHODIMP InprocEmbedDocument_Impl::InPlaceDeactivate()
1511 if ( CheckDefHandler() )
1513 ComSmart< IOleInPlaceObject > pIOleInPlaceObject;
1514 HRESULT hr = m_pDefHandler->QueryInterface( IID_IOleInPlaceObject, reinterpret_cast<void**>(&pIOleInPlaceObject) );
1516 ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
1517 if ( SUCCEEDED( hr ) && pIOleInPlaceObject )
1518 return pIOleInPlaceObject->InPlaceDeactivate();
1521 return E_FAIL;
1525 COM_DECLSPEC_NOTHROW STDMETHODIMP InprocEmbedDocument_Impl::UIDeactivate()
1527 if ( CheckDefHandler() )
1529 ComSmart< IOleInPlaceObject > pIOleInPlaceObject;
1530 HRESULT hr = m_pDefHandler->QueryInterface( IID_IOleInPlaceObject, reinterpret_cast<void**>(&pIOleInPlaceObject) );
1532 ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
1533 if ( SUCCEEDED( hr ) && pIOleInPlaceObject )
1534 return pIOleInPlaceObject->UIDeactivate();
1537 return E_FAIL;
1541 COM_DECLSPEC_NOTHROW STDMETHODIMP InprocEmbedDocument_Impl::SetObjectRects( LPCRECT lprcPosRect, LPCRECT lprcClipRect )
1543 if ( CheckDefHandler() )
1545 ComSmart< IOleInPlaceObject > pIOleInPlaceObject;
1546 HRESULT hr = m_pDefHandler->QueryInterface( IID_IOleInPlaceObject, reinterpret_cast<void**>(&pIOleInPlaceObject) );
1548 ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
1549 if ( SUCCEEDED( hr ) && pIOleInPlaceObject )
1550 return pIOleInPlaceObject->SetObjectRects( lprcPosRect, lprcClipRect );
1553 return E_FAIL;
1557 COM_DECLSPEC_NOTHROW STDMETHODIMP InprocEmbedDocument_Impl::ReactivateAndUndo()
1559 if ( CheckDefHandler() )
1561 ComSmart< IOleInPlaceObject > pIOleInPlaceObject;
1562 HRESULT hr = m_pDefHandler->QueryInterface( IID_IOleInPlaceObject, reinterpret_cast<void**>(&pIOleInPlaceObject) );
1564 ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
1565 if ( SUCCEEDED( hr ) && pIOleInPlaceObject )
1566 return pIOleInPlaceObject->ReactivateAndUndo();
1569 return E_FAIL;
1573 // IDispatch methods
1575 COM_DECLSPEC_NOTHROW STDMETHODIMP InprocEmbedDocument_Impl::GetTypeInfoCount( UINT *pctinfo )
1577 if ( CheckDefHandler() )
1579 ComSmart< IDispatch > pIDispatch;
1580 HRESULT hr = m_pDefHandler->QueryInterface( IID_IDispatch, reinterpret_cast<void**>(&pIDispatch) );
1582 ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
1583 if ( SUCCEEDED( hr ) && pIDispatch )
1584 return pIDispatch->GetTypeInfoCount( pctinfo );
1587 return E_FAIL;
1591 COM_DECLSPEC_NOTHROW STDMETHODIMP InprocEmbedDocument_Impl::GetTypeInfo( UINT iTInfo, LCID lcid, ITypeInfo **ppTInfo )
1593 if ( CheckDefHandler() )
1595 ComSmart< IDispatch > pIDispatch;
1596 HRESULT hr = m_pDefHandler->QueryInterface( IID_IDispatch, reinterpret_cast<void**>(&pIDispatch) );
1598 ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
1599 if ( SUCCEEDED( hr ) && pIDispatch )
1600 return pIDispatch->GetTypeInfo( iTInfo, lcid, ppTInfo );
1603 return E_FAIL;
1607 COM_DECLSPEC_NOTHROW STDMETHODIMP InprocEmbedDocument_Impl::GetIDsOfNames( REFIID riid, LPOLESTR *rgszNames, UINT cNames, LCID lcid, DISPID *rgDispId )
1609 if ( CheckDefHandler() )
1611 ComSmart< IDispatch > pIDispatch;
1612 HRESULT hr = m_pDefHandler->QueryInterface( IID_IDispatch, reinterpret_cast<void**>(&pIDispatch) );
1614 ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
1615 if ( SUCCEEDED( hr ) && pIDispatch )
1616 return pIDispatch->GetIDsOfNames( riid, rgszNames, cNames, lcid, rgDispId );
1619 return E_FAIL;
1623 COM_DECLSPEC_NOTHROW STDMETHODIMP InprocEmbedDocument_Impl::Invoke( DISPID dispIdMember, REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams, VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr )
1625 if ( CheckDefHandler() )
1627 ComSmart< IDispatch > pIDispatch;
1628 HRESULT hr = m_pDefHandler->QueryInterface( IID_IDispatch, reinterpret_cast<void**>(&pIDispatch) );
1630 ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
1631 if ( SUCCEEDED( hr ) && pIDispatch )
1632 return pIDispatch->Invoke( dispIdMember, riid, lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr );
1635 return E_FAIL;
1639 // InternalCacheWrapper
1642 // IUnknown
1644 COM_DECLSPEC_NOTHROW STDMETHODIMP InprocEmbedDocument_Impl::InternalCacheWrapper::QueryInterface( REFIID riid, void FAR* FAR* ppv )
1646 return m_rOwnDocument.QueryInterface( riid, ppv );
1650 COM_DECLSPEC_NOTHROW STDMETHODIMP_(ULONG) InprocEmbedDocument_Impl::InternalCacheWrapper::AddRef()
1652 return m_rOwnDocument.AddRef();
1656 COM_DECLSPEC_NOTHROW STDMETHODIMP_(ULONG) InprocEmbedDocument_Impl::InternalCacheWrapper::Release()
1658 return m_rOwnDocument.Release();
1661 // IOleCache methods
1663 COM_DECLSPEC_NOTHROW STDMETHODIMP InprocEmbedDocument_Impl::InternalCacheWrapper::Cache( FORMATETC *pformatetc, DWORD advf, DWORD *pdwConnection )
1665 if ( m_rOwnDocument.CheckDefHandler() )
1667 ComSmart< IOleCache > pIOleCache;
1668 HRESULT hr = m_rOwnDocument.GetDefHandler()->QueryInterface( IID_IOleCache, reinterpret_cast<void**>(&pIOleCache) );
1670 ULONGGuard aGuard( &m_rOwnDocument.m_nCallsOnStack ); // avoid reentrance problem
1671 if ( SUCCEEDED( hr ) && pIOleCache )
1672 return pIOleCache->Cache( pformatetc, advf, pdwConnection );
1675 return E_FAIL;
1679 COM_DECLSPEC_NOTHROW STDMETHODIMP InprocEmbedDocument_Impl::InternalCacheWrapper::Uncache( DWORD dwConnection )
1681 if ( m_rOwnDocument.CheckDefHandler() )
1683 ComSmart< IOleCache > pIOleCache;
1684 HRESULT hr = m_rOwnDocument.GetDefHandler()->QueryInterface( IID_IOleCache, reinterpret_cast<void**>(&pIOleCache) );
1686 ULONGGuard aGuard( &m_rOwnDocument.m_nCallsOnStack ); // avoid reentrance problem
1687 if ( SUCCEEDED( hr ) && pIOleCache )
1688 return pIOleCache->Uncache( dwConnection );
1691 return E_FAIL;
1695 COM_DECLSPEC_NOTHROW STDMETHODIMP InprocEmbedDocument_Impl::InternalCacheWrapper::EnumCache( IEnumSTATDATA **ppenumSTATDATA )
1697 if ( m_rOwnDocument.CheckDefHandler() )
1699 ComSmart< IOleCache > pIOleCache;
1700 HRESULT hr = m_rOwnDocument.GetDefHandler()->QueryInterface( IID_IOleCache, reinterpret_cast<void**>(&pIOleCache) );
1702 ULONGGuard aGuard( &m_rOwnDocument.m_nCallsOnStack ); // avoid reentrance problem
1703 if ( SUCCEEDED( hr ) && pIOleCache )
1704 return pIOleCache->EnumCache( ppenumSTATDATA );
1707 return E_FAIL;
1711 COM_DECLSPEC_NOTHROW STDMETHODIMP InprocEmbedDocument_Impl::InternalCacheWrapper::InitCache( IDataObject *pDataObject )
1713 if ( m_rOwnDocument.CheckDefHandler() )
1715 ComSmart< IOleCache > pIOleCache;
1716 HRESULT hr = m_rOwnDocument.GetDefHandler()->QueryInterface( IID_IOleCache, reinterpret_cast<void**>(&pIOleCache) );
1718 ULONGGuard aGuard( &m_rOwnDocument.m_nCallsOnStack ); // avoid reentrance problem
1719 if ( SUCCEEDED( hr ) && pIOleCache )
1720 return pIOleCache->InitCache( pDataObject );
1723 return E_FAIL;
1727 COM_DECLSPEC_NOTHROW STDMETHODIMP InprocEmbedDocument_Impl::InternalCacheWrapper::SetData( FORMATETC *pformatetc, STGMEDIUM *pmedium, BOOL fRelease )
1729 if ( m_rOwnDocument.CheckDefHandler() )
1731 ComSmart< IOleCache > pIOleCache;
1732 HRESULT hr = m_rOwnDocument.GetDefHandler()->QueryInterface( IID_IOleCache, reinterpret_cast<void**>(&pIOleCache) );
1734 ULONGGuard aGuard( &m_rOwnDocument.m_nCallsOnStack ); // avoid reentrance problem
1735 if ( SUCCEEDED( hr ) && pIOleCache )
1736 return pIOleCache->SetData( pformatetc, pmedium, fRelease );
1739 return E_FAIL;
1742 // IOleCache2 methods
1744 COM_DECLSPEC_NOTHROW STDMETHODIMP InprocEmbedDocument_Impl::InternalCacheWrapper::UpdateCache( LPDATAOBJECT pDataObject, DWORD grfUpdf, LPVOID pReserved )
1746 if ( m_rOwnDocument.CheckDefHandler() )
1748 ComSmart< IOleCache2 > pIOleCache2;
1749 HRESULT hr = m_rOwnDocument.GetDefHandler()->QueryInterface( IID_IOleCache2, reinterpret_cast<void**>(&pIOleCache2) );
1751 ULONGGuard aGuard( &m_rOwnDocument.m_nCallsOnStack ); // avoid reentrance problem
1752 if ( SUCCEEDED( hr ) && pIOleCache2 )
1753 return pIOleCache2->UpdateCache( pDataObject, grfUpdf, pReserved );
1756 return E_FAIL;
1760 COM_DECLSPEC_NOTHROW STDMETHODIMP InprocEmbedDocument_Impl::InternalCacheWrapper::DiscardCache( DWORD dwDiscardOptions )
1762 if ( m_rOwnDocument.CheckDefHandler() )
1764 ComSmart< IOleCache2 > pIOleCache2;
1765 HRESULT hr = m_rOwnDocument.GetDefHandler()->QueryInterface( IID_IOleCache2, reinterpret_cast<void**>(&pIOleCache2) );
1767 ULONGGuard aGuard( &m_rOwnDocument.m_nCallsOnStack ); // avoid reentrance problem
1768 if ( SUCCEEDED( hr ) && pIOleCache2 )
1769 return pIOleCache2->DiscardCache( dwDiscardOptions );
1772 return E_FAIL;
1775 }; // namespace inprocserv
1777 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */