fix baseline build (old cairo) - 'cairo_rectangle_int_t' does not name a type
[LibreOffice.git] / embedserv / source / inprocserv / inprocembobj.cxx
blob912efc4dc160df6b05cce40407d9d7f31bf02006
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
29 BOOL StringsEqual( LPCOLESTR pszNameFromOutside, wchar_t* pOwnName )
31 BOOL bResult = TRUE;
33 if ( pszNameFromOutside && pOwnName )
35 for ( int nInd = 0; pszNameFromOutside[nInd] != 0 || pOwnName[nInd] != 0; nInd++ )
37 if ( pszNameFromOutside[nInd] != pOwnName[nInd] )
39 bResult = FALSE;
40 break;
44 else if ( pszNameFromOutside || pOwnName )
45 bResult = FALSE;
47 return bResult;
51 HRESULT InprocEmbedDocument_Impl::Init()
53 return S_OK;
57 void InprocEmbedDocument_Impl::SetName( LPCOLESTR pszNameFromOutside, wchar_t*& pOwnName )
59 if ( !pszNameFromOutside )
60 return;
62 // copy the string
63 size_t nLen = 0;
64 while( pszNameFromOutside[nLen] != 0 )
65 nLen++;
67 if ( pOwnName )
69 delete[] pOwnName;
70 pOwnName = NULL;
73 pOwnName = new wchar_t[nLen+1];
74 for ( size_t nInd = 0; nInd < nLen; nInd++ )
75 pOwnName[nInd] = pszNameFromOutside[nInd];
76 pOwnName[nLen] = 0;
80 BOOL InprocEmbedDocument_Impl::CheckDefHandler()
82 // set the own listener
83 if ( m_pOleAdvises[0] == NULL )
85 m_pOleAdvises[0] = new OleWrapperAdviseSink();
87 else
89 if ( m_pOleAdvises[0]->IsClosed() )
91 if ( m_pDefHandler )
93 // deregister all the listeners
95 ComSmart< IOleObject > pOleObject;
96 HRESULT hr = m_pDefHandler->QueryInterface( IID_IOleObject, (void**)&pOleObject );
97 if ( SUCCEEDED( hr ) && pOleObject )
99 for ( DWORD nInd = 0; nInd < DEFAULT_ARRAY_LEN; nInd++ )
100 if ( m_pOleAdvises[nInd] )
102 DWORD nID = m_pOleAdvises[nInd]->GetRegID();
103 pOleObject->Unadvise( nID );
104 m_pOleAdvises[nInd]->SetRegID( 0 );
107 pOleObject->SetClientSite( NULL );
110 ComSmart< IDataObject > pIDataObject;
111 hr = m_pDefHandler->QueryInterface( IID_IDataObject, (void**)&pIDataObject );
112 if ( SUCCEEDED( hr ) && pIDataObject )
114 for ( DWORD nInd = 0; nInd < DEFAULT_ARRAY_LEN; nInd++ )
115 if ( m_pDataAdvises[nInd] )
117 DWORD nID = m_pDataAdvises[nInd]->GetRegID();
118 pIDataObject->DUnadvise( nID );
119 m_pDataAdvises[nInd]->SetRegID( 0 );
123 ComSmart< IViewObject > pIViewObject;
124 hr = m_pDefHandler->QueryInterface( IID_IViewObject, (void**)&pIViewObject );
125 if ( SUCCEEDED( hr ) && pIViewObject )
127 if ( m_pViewAdvise )
128 pIViewObject->SetAdvise( m_pViewAdvise->GetAspect(), m_pViewAdvise->GetViewAdviseFlag(), NULL );
131 ComSmart< IPersistStorage > pPersist;
132 hr = m_pDefHandler->QueryInterface( IID_IPersistStorage, (void**)&pPersist );
133 if ( SUCCEEDED( hr ) && pPersist )
135 // disconnect the old wrapper from the storage
136 pPersist->HandsOffStorage();
139 m_pDefHandler = NULL;
142 m_pOleAdvises[0]->UnsetClosed();
146 if ( m_nCallsOnStack )
147 return FALSE;
149 if ( !m_pDefHandler )
151 // create a new default inprocess handler
152 HRESULT hr = OleCreateDefaultHandler( m_guid, NULL, IID_IUnknown, (void**)&m_pDefHandler );
153 if ( SUCCEEDED( hr ) )
156 if ( m_nInitMode == INIT_FROM_STORAGE )
158 ComSmart< IPersistStorage > pPersist;
159 hr = m_pDefHandler->QueryInterface( IID_IPersistStorage, (void**)&pPersist );
161 ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
162 if ( SUCCEEDED( hr ) && pPersist && m_pStorage )
163 hr = pPersist->InitNew( m_pStorage );
165 else if ( m_nInitMode == LOAD_FROM_STORAGE )
167 ComSmart< IPersistStorage > pPersist;
168 hr = m_pDefHandler->QueryInterface( IID_IPersistStorage, (void**)&pPersist );
170 ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
171 if ( SUCCEEDED( hr ) && pPersist && m_pStorage )
172 hr = pPersist->Load( m_pStorage );
174 else if ( m_nInitMode == LOAD_FROM_FILE )
176 ComSmart< IPersistFile > pPersistFile;
177 hr = m_pDefHandler->QueryInterface( IID_IPersistFile, (void**)&pPersistFile );
179 ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
180 if ( SUCCEEDED( hr ) && pPersistFile && m_pFileName )
181 hr = pPersistFile->Load( m_pFileName, m_nFileOpenMode );
186 if ( !SUCCEEDED( hr ) || !m_pDefHandler )
188 m_pDefHandler = NULL;
189 return FALSE;
192 // register all the listeners new
194 ComSmart< IOleObject > pOleObject;
195 hr = m_pDefHandler->QueryInterface( IID_IOleObject, (void**)&pOleObject );
196 if ( SUCCEEDED( hr ) && pOleObject )
198 if ( m_pClientSite )
199 pOleObject->SetClientSite( m_pClientSite );
201 for ( DWORD nInd = 0; nInd < DEFAULT_ARRAY_LEN; nInd++ )
202 if ( m_pOleAdvises[nInd] )
204 DWORD nRegID = 0;
205 if ( SUCCEEDED( pOleObject->Advise( m_pOleAdvises[nInd], &nRegID ) ) && nRegID > 0 )
206 m_pOleAdvises[nInd]->SetRegID( nRegID );
210 ComSmart< IDataObject > pIDataObject;
211 hr = m_pDefHandler->QueryInterface( IID_IDataObject, (void**)&pIDataObject );
212 if ( SUCCEEDED( hr ) && pIDataObject )
214 for ( DWORD nInd = 0; nInd < DEFAULT_ARRAY_LEN; nInd++ )
215 if ( m_pDataAdvises[nInd] )
217 DWORD nRegID = 0;
218 if ( SUCCEEDED( pIDataObject->DAdvise( m_pDataAdvises[nInd]->GetFormatEtc(), m_pDataAdvises[nInd]->GetDataAdviseFlag(), m_pDataAdvises[nInd], &nRegID ) ) && nRegID > 0 )
219 m_pDataAdvises[nInd]->SetRegID( nRegID );
223 ComSmart< IViewObject > pIViewObject;
224 hr = m_pDefHandler->QueryInterface( IID_IViewObject, (void**)&pIViewObject );
225 if ( SUCCEEDED( hr ) && pIViewObject )
227 if ( m_pViewAdvise )
228 pIViewObject->SetAdvise( m_pViewAdvise->GetAspect(), m_pViewAdvise->GetViewAdviseFlag(), m_pViewAdvise );
233 return TRUE;
237 DWORD InprocEmbedDocument_Impl::InsertAdviseLinkToList( const ComSmart<OleWrapperAdviseSink>& pOwnAdvise, ComSmart< OleWrapperAdviseSink > pAdvises[] )
239 // the result should start from 1 in case of success, the element 0 can be used for own needs
240 DWORD nResult = 0;
242 if ( pOwnAdvise )
244 for ( DWORD nInd = 1; nInd < DEFAULT_ARRAY_LEN && nResult == 0; nInd++ )
246 if ( pAdvises[nInd] == pOwnAdvise )
248 nResult = nInd;
250 else if ( pAdvises[nInd] == NULL )
252 pAdvises[nInd] = pOwnAdvise;
253 nResult = nInd;
258 return nResult;
262 void InprocEmbedDocument_Impl::Clean()
264 m_pDefHandler = (IUnknown*)NULL;
266 // no DisconnectOrigAdvise() call here, since it is no explicit disconnection
267 for ( DWORD nInd = 0; nInd < DEFAULT_ARRAY_LEN; nInd++ )
269 if ( m_pOleAdvises[nInd] )
271 ComSmart< OleWrapperAdviseSink > pAdvise = m_pOleAdvises[nInd];
272 m_pOleAdvises[nInd] = NULL;
275 if ( m_pDataAdvises[nInd] )
277 ComSmart< OleWrapperAdviseSink > pAdvise = m_pDataAdvises[nInd];
278 m_pDataAdvises[nInd] = NULL;
282 m_pViewAdvise = NULL;
284 m_nInitMode = NOINIT;
285 m_pStorage = NULL;
287 if ( m_pOleContainer )
289 m_pOleContainer->LockContainer( FALSE );
290 m_pOleContainer = NULL;
293 m_pClientSite = NULL;
295 m_nFileOpenMode = 0;
296 if ( m_pFileName )
298 delete m_pFileName;
299 m_pFileName = NULL;
303 // IUnknown
305 STDMETHODIMP InprocEmbedDocument_Impl::QueryInterface( REFIID riid, void FAR* FAR* ppv )
307 if(IsEqualIID(riid, IID_IUnknown))
309 AddRef();
310 *ppv = (IUnknown*) (IPersistStorage*) this;
311 return S_OK;
313 else if (IsEqualIID(riid, IID_IPersist))
315 AddRef();
316 *ppv = (IPersist*) (IPersistStorage*) this;
317 return S_OK;
319 else if (IsEqualIID(riid, IID_IPersistStorage))
321 AddRef();
322 *ppv = (IPersistStorage*) this;
323 return S_OK;
325 else if (IsEqualIID(riid, IID_IDataObject))
327 AddRef();
328 *ppv = (IDataObject*) this;
329 return S_OK;
331 else if (IsEqualIID(riid, IID_IOleObject))
333 AddRef();
334 *ppv = (IOleObject*) this;
335 return S_OK;
337 else if (IsEqualIID(riid, IID_IPersistFile))
339 AddRef();
340 *ppv = (IPersistFile*) this;
341 return S_OK;
343 else if (IsEqualIID(riid, IID_IRunnableObject))
345 AddRef();
346 *ppv = (IRunnableObject*) this;
347 return S_OK;
349 else if (IsEqualIID(riid, IID_IViewObject))
351 AddRef();
352 *ppv = (IViewObject*) this;
353 return S_OK;
355 else if (IsEqualIID(riid, IID_IViewObject2))
357 AddRef();
358 *ppv = (IViewObject2*) this;
359 return S_OK;
361 else if (IsEqualIID(riid, IID_IOleCache))
363 AddRef();
364 *ppv = (IOleCache*) &m_aInternalCache;
365 return S_OK;
367 else if (IsEqualIID(riid, IID_IOleCache2))
369 AddRef();
370 *ppv = (IOleCache2*) &m_aInternalCache;
371 return S_OK;
373 else if (IsEqualIID(riid, IID_IOleWindow))
375 AddRef();
376 *ppv = (IOleWindow*) this;
377 return S_OK;
379 else if (IsEqualIID(riid, IID_IOleInPlaceObject))
381 AddRef();
382 *ppv = (IOleInPlaceObject*) this;
383 return S_OK;
385 else if (IsEqualIID(riid, IID_IDispatch))
387 AddRef();
388 *ppv = (IDispatch*) this;
389 return S_OK;
392 *ppv = NULL;
393 return ResultFromScode(E_NOINTERFACE);
397 STDMETHODIMP_(ULONG) InprocEmbedDocument_Impl::AddRef()
399 return ++m_refCount;
403 STDMETHODIMP_(ULONG) InprocEmbedDocument_Impl::Release()
405 // unfortunately there are reentrance problems in mfc that have to be workarounded
406 sal_Int32 nCount = m_refCount > 0 ? --m_refCount : 0;
407 if ( nCount == 0 && !m_bDeleted )
409 // deleting of this object can trigger deleting of mfc objects that will try to delete this object one more time
410 m_bDeleted = TRUE;
412 Clean();
413 delete this;
415 return nCount;
418 // IPersist
420 STDMETHODIMP InprocEmbedDocument_Impl::GetClassID( CLSID* pClassId )
422 *pClassId = *&m_guid;
423 return S_OK;
426 // IPersistStorage
428 STDMETHODIMP InprocEmbedDocument_Impl::IsDirty()
430 if ( m_pDefHandler == NULL || m_pOleAdvises[0] == NULL || m_pOleAdvises[0]->IsClosed() )
431 return S_FALSE;
433 if ( CheckDefHandler() )
435 ComSmart< IPersistStorage > pPersist;
436 HRESULT hr = m_pDefHandler->QueryInterface( IID_IPersistStorage, (void**)&pPersist );
438 ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
439 if ( SUCCEEDED( hr ) && pPersist )
440 return pPersist->IsDirty();
443 return E_FAIL;
447 STDMETHODIMP InprocEmbedDocument_Impl::InitNew( IStorage *pStg )
449 if ( CheckDefHandler() )
451 ComSmart< IPersistStorage > pPersist;
452 HRESULT hr = m_pDefHandler->QueryInterface( IID_IPersistStorage, (void**)&pPersist );
454 ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
455 if ( SUCCEEDED( hr ) && pPersist )
457 hr = pPersist->InitNew( pStg );
458 if ( SUCCEEDED( hr ) )
460 m_nInitMode = INIT_FROM_STORAGE;
461 m_pStorage = pStg;
463 m_nFileOpenMode = 0;
464 if ( m_pFileName )
466 delete[] m_pFileName;
467 m_pFileName = NULL;
471 return hr;
475 return E_FAIL;
479 STDMETHODIMP InprocEmbedDocument_Impl::Load( IStorage *pStg )
481 if ( CheckDefHandler() )
483 ComSmart< IPersistStorage > pPersist;
484 HRESULT hr = m_pDefHandler->QueryInterface( IID_IPersistStorage, (void**)&pPersist );
486 ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
487 if ( SUCCEEDED( hr ) && pPersist )
489 hr = pPersist->Load( pStg );
490 if ( SUCCEEDED( hr ) )
492 m_nInitMode = LOAD_FROM_STORAGE;
493 m_pStorage = pStg;
495 m_nFileOpenMode = 0;
496 if ( m_pFileName )
498 delete[] m_pFileName;
499 m_pFileName = NULL;
503 return hr;
507 return E_FAIL;
511 STDMETHODIMP InprocEmbedDocument_Impl::Save( IStorage *pStgSave, BOOL fSameAsLoad )
513 if ( fSameAsLoad && ( m_pDefHandler == NULL || m_pOleAdvises[0] == NULL || m_pOleAdvises[0]->IsClosed() ) )
514 return S_OK;
516 if ( CheckDefHandler() )
518 ComSmart< IPersistStorage > pPersist;
519 HRESULT hr = m_pDefHandler->QueryInterface( IID_IPersistStorage, (void**)&pPersist );
521 ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
522 if ( SUCCEEDED( hr ) && pPersist )
523 return pPersist->Save( pStgSave, fSameAsLoad );
526 return E_FAIL;
530 STDMETHODIMP InprocEmbedDocument_Impl::SaveCompleted( IStorage *pStgNew )
532 if ( m_pDefHandler == NULL || m_pOleAdvises[0] == NULL || m_pOleAdvises[0]->IsClosed() )
534 if ( pStgNew )
535 m_pStorage = pStgNew;
537 return S_OK;
540 if ( CheckDefHandler() )
542 ComSmart< IPersistStorage > pPersist;
543 HRESULT hr = m_pDefHandler->QueryInterface( IID_IPersistStorage, (void**)&pPersist );
545 ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
546 if ( SUCCEEDED( hr ) && pPersist )
548 hr = pPersist->SaveCompleted( pStgNew );
549 if ( SUCCEEDED( hr ) )
551 m_nInitMode = LOAD_FROM_STORAGE;
552 if ( pStgNew )
554 m_pStorage = pStgNew;
557 m_nFileOpenMode = 0;
558 if ( m_pFileName )
560 delete[] m_pFileName;
561 m_pFileName = NULL;
565 return hr;
569 return E_FAIL;
573 STDMETHODIMP InprocEmbedDocument_Impl::HandsOffStorage()
575 if ( CheckDefHandler() )
577 ComSmart< IPersistStorage > pPersist;
578 HRESULT hr = m_pDefHandler->QueryInterface( IID_IPersistStorage, (void**)&pPersist );
580 ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
581 if ( SUCCEEDED( hr ) && pPersist )
583 hr = pPersist->HandsOffStorage();
584 if ( SUCCEEDED( hr ) )
586 m_pStorage = NULL;
589 return hr;
593 return E_FAIL;
596 // IPersistFile
598 STDMETHODIMP InprocEmbedDocument_Impl::Load( LPCOLESTR pszFileName, DWORD dwMode )
600 if ( CheckDefHandler() && pszFileName )
602 ComSmart< IPersistFile > pPersist;
603 HRESULT hr = m_pDefHandler->QueryInterface( IID_IPersistFile, (void**)&pPersist );
605 ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
606 if ( SUCCEEDED( hr ) && pPersist )
608 hr = pPersist->Load( pszFileName, dwMode );
609 if ( SUCCEEDED( hr ) )
611 m_nInitMode = LOAD_FROM_FILE;
612 if ( m_pStorage )
613 m_pStorage = NULL;
615 m_nFileOpenMode = dwMode;
616 // copy the string
617 SetName( pszFileName, m_pFileName );
620 return hr;
624 return E_FAIL;
628 STDMETHODIMP InprocEmbedDocument_Impl::Save( LPCOLESTR pszFileName, BOOL fRemember )
630 if ( CheckDefHandler() )
632 ComSmart< IPersistFile > pPersist;
633 HRESULT hr = m_pDefHandler->QueryInterface( IID_IPersistFile, (void**)&pPersist );
635 ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
636 if ( SUCCEEDED( hr ) && pPersist )
637 return pPersist->Save( pszFileName, fRemember );
640 return E_FAIL;
644 STDMETHODIMP InprocEmbedDocument_Impl::SaveCompleted( LPCOLESTR pszFileName )
646 if ( CheckDefHandler() )
648 ComSmart< IPersistFile > pPersist;
649 HRESULT hr = m_pDefHandler->QueryInterface( IID_IPersistFile, (void**)&pPersist );
651 ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
652 if ( SUCCEEDED( hr ) && pPersist )
654 hr = pPersist->SaveCompleted( pszFileName );
655 if ( SUCCEEDED( hr ) )
657 m_nInitMode = LOAD_FROM_STORAGE;
658 if ( m_pStorage )
659 m_pStorage = NULL;
661 m_nFileOpenMode = STGM_READWRITE; // was just written
662 // copy the string
663 SetName( pszFileName, m_pFileName );
669 return E_FAIL;
673 STDMETHODIMP InprocEmbedDocument_Impl::GetCurFile( LPOLESTR *ppszFileName )
675 if ( CheckDefHandler() )
677 ComSmart< IPersistFile > pPersist;
678 HRESULT hr = m_pDefHandler->QueryInterface( IID_IPersistFile, (void**)&pPersist );
680 ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
681 if ( SUCCEEDED( hr ) && pPersist )
682 return pPersist->GetCurFile( ppszFileName );
685 return E_FAIL;
688 // IOleObject
690 STDMETHODIMP InprocEmbedDocument_Impl::SetClientSite( IOleClientSite* pSite )
692 if ( pSite == m_pClientSite )
693 return S_OK;
695 if ( !pSite )
697 m_pClientSite = NULL;
698 if ( m_pOleContainer )
700 m_pOleContainer->LockContainer( FALSE );
701 m_pOleContainer = NULL;
705 if ( CheckDefHandler() )
707 ComSmart< IOleObject > pOleObject;
708 HRESULT hr = m_pDefHandler->QueryInterface( IID_IOleObject, (void**)&pOleObject );
710 ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
711 if ( SUCCEEDED( hr ) && pOleObject )
713 HRESULT hr2 = pOleObject->SetClientSite( pSite );
714 if ( SUCCEEDED( hr2 ) )
716 m_pClientSite = pSite;
718 if ( m_pOleContainer )
720 m_pOleContainer->LockContainer( FALSE );
721 m_pOleContainer = NULL;
724 m_pClientSite->GetContainer( &m_pOleContainer );
725 if ( m_pOleContainer )
726 m_pOleContainer->LockContainer( TRUE );
729 return hr2;
733 return E_FAIL;
737 STDMETHODIMP InprocEmbedDocument_Impl::GetClientSite( IOleClientSite** pSite )
739 if ( CheckDefHandler() )
741 ComSmart< IOleObject > pOleObject;
742 HRESULT hr = m_pDefHandler->QueryInterface( IID_IOleObject, (void**)&pOleObject );
744 ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
745 if ( SUCCEEDED( hr ) && pOleObject )
746 return pOleObject->GetClientSite( pSite );
749 return E_FAIL;
753 STDMETHODIMP InprocEmbedDocument_Impl::SetHostNames( LPCOLESTR szContainerApp, LPCOLESTR szContainerObj )
756 if ( CheckDefHandler() )
758 ComSmart< IOleObject > pOleObject;
759 HRESULT hr = m_pDefHandler->QueryInterface( IID_IOleObject, (void**)&pOleObject );
761 ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
762 if ( SUCCEEDED( hr ) && pOleObject )
764 hr = pOleObject->SetHostNames( szContainerApp, szContainerObj );
768 return S_OK;
772 STDMETHODIMP InprocEmbedDocument_Impl::Close( DWORD dwSaveOption )
774 HRESULT ret = S_OK;
775 if ( m_pDefHandler && CheckDefHandler() )
777 // no need to close if there is no default handler.
778 ComSmart< IOleObject > pOleObject;
779 HRESULT hr = m_pDefHandler->QueryInterface( IID_IOleObject, (void**)&pOleObject );
781 ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
782 if ( SUCCEEDED( hr ) && pOleObject )
784 hr = pOleObject->Close( dwSaveOption );
785 if (!SUCCEEDED(hr))
786 ret = hr;
787 hr = CoDisconnectObject( (IUnknown*)(IPersistStorage*)this, 0 );
788 if (!SUCCEEDED(hr) && SUCCEEDED(ret))
789 ret = hr;
793 // if the object is closed from outside that means that it should go to uninitialized state
794 Clean();
796 return ret;
800 STDMETHODIMP InprocEmbedDocument_Impl::SetMoniker( DWORD dwWhichMoniker, IMoniker * pmk )
802 if ( CheckDefHandler() )
804 ComSmart< IOleObject > pOleObject;
805 HRESULT hr = m_pDefHandler->QueryInterface( IID_IOleObject, (void**)&pOleObject );
807 ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
808 if ( SUCCEEDED( hr ) && pOleObject )
809 return pOleObject->SetMoniker( dwWhichMoniker, pmk );
812 return E_FAIL;
816 STDMETHODIMP InprocEmbedDocument_Impl::GetMoniker( DWORD dwAssign, DWORD dwWhichMoniker, IMoniker ** ppmk )
818 if ( CheckDefHandler() )
820 ComSmart< IOleObject > pOleObject;
821 HRESULT hr = m_pDefHandler->QueryInterface( IID_IOleObject, (void**)&pOleObject );
823 ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
824 if ( SUCCEEDED( hr ) && pOleObject )
825 return pOleObject->GetMoniker( dwAssign, dwWhichMoniker, ppmk );
828 return E_FAIL;
832 STDMETHODIMP InprocEmbedDocument_Impl::InitFromData( IDataObject * pDataObject, BOOL fCreation, DWORD dwReserved )
834 if ( CheckDefHandler() )
836 ComSmart< IOleObject > pOleObject;
837 HRESULT hr = m_pDefHandler->QueryInterface( IID_IOleObject, (void**)&pOleObject );
839 ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
840 if ( SUCCEEDED( hr ) && pOleObject )
841 return pOleObject->InitFromData( pDataObject, fCreation, dwReserved );
844 return E_FAIL;
848 STDMETHODIMP InprocEmbedDocument_Impl::GetClipboardData( DWORD dwReserved, IDataObject ** ppDataObject )
850 if ( CheckDefHandler() )
852 ComSmart< IOleObject > pOleObject;
853 HRESULT hr = m_pDefHandler->QueryInterface( IID_IOleObject, (void**)&pOleObject );
855 ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
856 if ( SUCCEEDED( hr ) && pOleObject )
857 return pOleObject->GetClipboardData( dwReserved, ppDataObject );
860 return E_FAIL;
864 STDMETHODIMP InprocEmbedDocument_Impl::DoVerb(
865 LONG iVerb,
866 LPMSG pMsg,
867 IOleClientSite *pActiveSite,
868 LONG nLong,
869 HWND hWin,
870 LPCRECT pRect )
872 if ( CheckDefHandler() )
874 ComSmart< IOleObject > pOleObject;
875 HRESULT hr = m_pDefHandler->QueryInterface( IID_IOleObject, (void**)&pOleObject );
877 ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
878 if ( SUCCEEDED( hr ) && pOleObject )
880 hr = pOleObject->DoVerb( iVerb, pMsg, pActiveSite, nLong, hWin, pRect );
881 if ( SUCCEEDED( hr ) )
885 return hr;
890 return E_FAIL;
894 STDMETHODIMP InprocEmbedDocument_Impl::EnumVerbs( IEnumOLEVERB ** ppEnumOleVerb )
896 if ( CheckDefHandler() )
898 ComSmart< IOleObject > pOleObject;
899 HRESULT hr = m_pDefHandler->QueryInterface( IID_IOleObject, (void**)&pOleObject );
901 ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
902 if ( SUCCEEDED( hr ) && pOleObject )
903 return pOleObject->EnumVerbs( ppEnumOleVerb );
906 return E_FAIL;
910 STDMETHODIMP InprocEmbedDocument_Impl::Update()
913 if ( m_pDefHandler && CheckDefHandler() )
915 ComSmart< IOleObject > pOleObject;
916 HRESULT hr = m_pDefHandler->QueryInterface( IID_IOleObject, (void**)&pOleObject );
918 ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
919 if ( SUCCEEDED( hr ) && pOleObject )
920 return pOleObject->Update();
923 return S_OK;
927 STDMETHODIMP InprocEmbedDocument_Impl::IsUpToDate()
929 if ( CheckDefHandler() )
931 ComSmart< IOleObject > pOleObject;
932 HRESULT hr = m_pDefHandler->QueryInterface( IID_IOleObject, (void**)&pOleObject );
934 ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
935 if ( SUCCEEDED( hr ) && pOleObject )
936 return pOleObject->IsUpToDate();
939 return E_FAIL;
943 STDMETHODIMP InprocEmbedDocument_Impl::GetUserClassID( CLSID *pClsid )
945 if ( pClsid )
946 *pClsid = m_guid;
948 return S_OK;
952 STDMETHODIMP InprocEmbedDocument_Impl::GetUserType( DWORD dwFormOfType, LPOLESTR * pszUserType )
954 if ( CheckDefHandler() )
956 ComSmart< IOleObject > pOleObject;
957 HRESULT hr = m_pDefHandler->QueryInterface( IID_IOleObject, (void**)&pOleObject );
959 ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
960 if ( SUCCEEDED( hr ) && pOleObject )
961 return pOleObject->GetUserType( dwFormOfType, pszUserType );
964 return E_FAIL;
968 STDMETHODIMP InprocEmbedDocument_Impl::SetExtent( DWORD dwDrawAspect, SIZEL *psizel )
970 if ( CheckDefHandler() )
972 ComSmart< IOleObject > pOleObject;
973 HRESULT hr = m_pDefHandler->QueryInterface( IID_IOleObject, (void**)&pOleObject );
975 ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
976 if ( SUCCEEDED( hr ) && pOleObject )
977 return pOleObject->SetExtent( dwDrawAspect, psizel );
980 return E_FAIL;
984 STDMETHODIMP InprocEmbedDocument_Impl::GetExtent( DWORD dwDrawAspect, SIZEL * psizel )
986 if ( CheckDefHandler() )
988 ComSmart< IOleObject > pOleObject;
989 HRESULT hr = m_pDefHandler->QueryInterface( IID_IOleObject, (void**)&pOleObject );
991 ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
992 if ( SUCCEEDED( hr ) && pOleObject )
993 return pOleObject->GetExtent( dwDrawAspect, psizel );
996 return E_FAIL;
1000 STDMETHODIMP InprocEmbedDocument_Impl::Advise( IAdviseSink *pAdvSink, DWORD *pdwConnection )
1003 if ( !pdwConnection )
1004 return E_FAIL;
1006 // CheckDefHandler will set the listener, avoid reusing of old listener
1007 if ( DEFAULT_ARRAY_LEN > *pdwConnection && *pdwConnection > 0 && m_pOleAdvises[*pdwConnection] )
1009 m_pOleAdvises[*pdwConnection]->DisconnectOrigAdvise();
1010 m_pOleAdvises[*pdwConnection] = NULL;
1013 if ( pAdvSink && CheckDefHandler() )
1015 ComSmart< IOleObject > pOleObject;
1016 HRESULT hr = m_pDefHandler->QueryInterface( IID_IOleObject, (void**)&pOleObject );
1018 ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
1019 if ( SUCCEEDED( hr ) && pOleObject )
1021 ComSmart< OleWrapperAdviseSink > pOwnAdvise( new OleWrapperAdviseSink( pAdvSink ) );
1022 DWORD nRegID = 0;
1024 if ( SUCCEEDED( pOleObject->Advise( pOwnAdvise, &nRegID ) ) && nRegID > 0 )
1026 pOwnAdvise->SetRegID( nRegID );
1027 *pdwConnection = InsertAdviseLinkToList( pOwnAdvise, m_pOleAdvises );
1028 if ( *pdwConnection )
1029 return S_OK;
1030 else
1031 pOleObject->Unadvise( nRegID );
1036 // return success always for now
1037 return S_OK;
1041 STDMETHODIMP InprocEmbedDocument_Impl::Unadvise( DWORD dwConnection )
1043 if ( DEFAULT_ARRAY_LEN > dwConnection && dwConnection > 0 && m_pOleAdvises[dwConnection] )
1045 if ( m_pDefHandler )
1047 ComSmart< IOleObject > pOleObject;
1048 HRESULT hr = m_pDefHandler->QueryInterface( IID_IOleObject, (void**)&pOleObject );
1050 ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
1051 if ( SUCCEEDED( hr ) && pOleObject )
1053 DWORD nID = m_pOleAdvises[dwConnection]->GetRegID();
1054 pOleObject->Unadvise( nID );
1058 m_pOleAdvises[dwConnection]->DisconnectOrigAdvise();
1059 m_pOleAdvises[dwConnection] = NULL;
1061 return S_OK;
1064 return E_FAIL;
1068 STDMETHODIMP InprocEmbedDocument_Impl::EnumAdvise( IEnumSTATDATA ** /*ppenumAdvise*/ )
1070 return E_NOTIMPL;
1074 STDMETHODIMP InprocEmbedDocument_Impl::GetMiscStatus( DWORD dwAspect, DWORD * pdwStatus )
1076 if ( CheckDefHandler() )
1078 ComSmart< IOleObject > pOleObject;
1079 HRESULT hr = m_pDefHandler->QueryInterface( IID_IOleObject, (void**)&pOleObject );
1081 ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
1082 if ( SUCCEEDED( hr ) && pOleObject )
1083 return pOleObject->GetMiscStatus( dwAspect, pdwStatus );
1086 return E_FAIL;
1090 STDMETHODIMP InprocEmbedDocument_Impl::SetColorScheme( LOGPALETTE * pLogpal )
1092 if ( CheckDefHandler() )
1094 ComSmart< IOleObject > pOleObject;
1095 HRESULT hr = m_pDefHandler->QueryInterface( IID_IOleObject, (void**)&pOleObject );
1097 ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
1098 if ( SUCCEEDED( hr ) && pOleObject )
1099 return pOleObject->SetColorScheme( pLogpal );
1102 return E_FAIL;
1105 //IDataObject
1107 STDMETHODIMP InprocEmbedDocument_Impl::GetData( FORMATETC * pFormatetc, STGMEDIUM * pMedium )
1109 if ( CheckDefHandler() )
1111 ComSmart< IDataObject > pIDataObject;
1112 HRESULT hr = m_pDefHandler->QueryInterface( IID_IDataObject, (void**)&pIDataObject );
1114 ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
1115 if ( SUCCEEDED( hr ) && pIDataObject )
1116 return pIDataObject->GetData( pFormatetc, pMedium );
1119 return E_FAIL;
1123 STDMETHODIMP InprocEmbedDocument_Impl::GetDataHere( FORMATETC * pFormatetc, STGMEDIUM * pMedium )
1125 if ( CheckDefHandler() )
1127 ComSmart< IDataObject > pIDataObject;
1128 HRESULT hr = m_pDefHandler->QueryInterface( IID_IDataObject, (void**)&pIDataObject );
1130 ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
1131 if ( SUCCEEDED( hr ) && pIDataObject )
1132 return pIDataObject->GetDataHere( pFormatetc, pMedium );
1135 return E_FAIL;
1139 STDMETHODIMP InprocEmbedDocument_Impl::QueryGetData( FORMATETC * pFormatetc )
1141 if ( CheckDefHandler() )
1143 ComSmart< IDataObject > pIDataObject;
1144 HRESULT hr = m_pDefHandler->QueryInterface( IID_IDataObject, (void**)&pIDataObject );
1146 ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
1147 if ( SUCCEEDED( hr ) && pIDataObject )
1148 return pIDataObject->QueryGetData( pFormatetc );
1151 return E_FAIL;
1155 STDMETHODIMP InprocEmbedDocument_Impl::GetCanonicalFormatEtc( FORMATETC * pFormatetcIn, FORMATETC * pFormatetcOut )
1157 if ( CheckDefHandler() )
1159 ComSmart< IDataObject > pIDataObject;
1160 HRESULT hr = m_pDefHandler->QueryInterface( IID_IDataObject, (void**)&pIDataObject );
1162 ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
1163 if ( SUCCEEDED( hr ) && pIDataObject )
1164 return pIDataObject->GetCanonicalFormatEtc( pFormatetcIn, pFormatetcOut );
1167 return E_FAIL;
1171 STDMETHODIMP InprocEmbedDocument_Impl::SetData( FORMATETC * pFormatetc, STGMEDIUM * pMedium, BOOL fRelease )
1173 if ( CheckDefHandler() )
1175 ComSmart< IDataObject > pIDataObject;
1176 HRESULT hr = m_pDefHandler->QueryInterface( IID_IDataObject, (void**)&pIDataObject );
1178 ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
1179 if ( SUCCEEDED( hr ) && pIDataObject )
1180 return pIDataObject->SetData( pFormatetc, pMedium, fRelease );
1183 return E_FAIL;
1187 STDMETHODIMP InprocEmbedDocument_Impl::EnumFormatEtc( DWORD dwDirection, IEnumFORMATETC ** ppFormatetc )
1189 if ( CheckDefHandler() )
1191 ComSmart< IDataObject > pIDataObject;
1192 HRESULT hr = m_pDefHandler->QueryInterface( IID_IDataObject, (void**)&pIDataObject );
1194 ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
1195 if ( SUCCEEDED( hr ) && pIDataObject )
1196 return pIDataObject->EnumFormatEtc( dwDirection, ppFormatetc );
1199 return E_FAIL;
1203 STDMETHODIMP InprocEmbedDocument_Impl::DAdvise( FORMATETC * pFormatetc, DWORD advf, IAdviseSink * pAdvSink, DWORD * pdwConnection )
1206 if ( !pdwConnection )
1207 return E_FAIL;
1209 // avoid reusing of the old listener
1210 if ( m_pDefHandler && DEFAULT_ARRAY_LEN > *pdwConnection && *pdwConnection > 0 && m_pDataAdvises[*pdwConnection] )
1212 m_pDataAdvises[*pdwConnection]->DisconnectOrigAdvise();
1213 m_pDataAdvises[*pdwConnection] = NULL;
1216 if ( pAdvSink && CheckDefHandler() )
1218 ComSmart< IDataObject > pIDataObject;
1219 HRESULT hr = m_pDefHandler->QueryInterface( IID_IDataObject, (void**)&pIDataObject );
1221 ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
1222 if ( SUCCEEDED( hr ) && pIDataObject )
1224 ComSmart< OleWrapperAdviseSink > pOwnAdvise( new OleWrapperAdviseSink( ComSmart<IAdviseSink>( pAdvSink ), pFormatetc, advf ) );
1225 DWORD nRegID = 0;
1227 if ( SUCCEEDED( pIDataObject->DAdvise( pFormatetc, advf, pOwnAdvise, &nRegID ) ) && nRegID > 0 )
1229 pOwnAdvise->SetRegID( nRegID );
1230 *pdwConnection = InsertAdviseLinkToList( pOwnAdvise, m_pDataAdvises );
1231 if ( *pdwConnection )
1232 return S_OK;
1233 else
1234 pIDataObject->DUnadvise( nRegID );
1239 // return success always for now
1240 return S_OK;
1244 STDMETHODIMP InprocEmbedDocument_Impl::DUnadvise( DWORD dwConnection )
1246 if ( m_pDefHandler && DEFAULT_ARRAY_LEN > dwConnection && dwConnection > 0 && m_pDataAdvises[dwConnection] )
1248 if ( CheckDefHandler() )
1250 ComSmart< IDataObject > pIDataObject;
1251 HRESULT hr = m_pDefHandler->QueryInterface( IID_IDataObject, (void**)&pIDataObject );
1253 ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
1254 if ( SUCCEEDED( hr ) && pIDataObject )
1256 DWORD nID = m_pDataAdvises[dwConnection]->GetRegID();
1257 pIDataObject->DUnadvise( nID );
1261 m_pDataAdvises[dwConnection]->DisconnectOrigAdvise();
1262 m_pDataAdvises[dwConnection] = NULL;
1264 return S_OK;
1267 return E_FAIL;
1271 STDMETHODIMP InprocEmbedDocument_Impl::EnumDAdvise( IEnumSTATDATA ** ppenumAdvise )
1273 if ( CheckDefHandler() )
1275 ComSmart< IDataObject > pIDataObject;
1276 HRESULT hr = m_pDefHandler->QueryInterface( IID_IDataObject, (void**)&pIDataObject );
1278 ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
1279 if ( SUCCEEDED( hr ) && pIDataObject )
1280 return pIDataObject->EnumDAdvise( ppenumAdvise );
1283 return E_FAIL;
1286 // IRunnableObject
1288 STDMETHODIMP InprocEmbedDocument_Impl::GetRunningClass( LPCLSID lpClsid )
1290 if ( CheckDefHandler() )
1292 ComSmart< IRunnableObject > pIRunObj;
1293 HRESULT hr = m_pDefHandler->QueryInterface( IID_IRunnableObject, (void**)&pIRunObj );
1295 ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
1296 if ( SUCCEEDED( hr ) && pIRunObj )
1297 return pIRunObj->GetRunningClass( lpClsid );
1300 return E_FAIL;
1304 STDMETHODIMP InprocEmbedDocument_Impl::Run( LPBINDCTX pbc )
1306 if ( CheckDefHandler() )
1308 ComSmart< IRunnableObject > pIRunObj;
1309 HRESULT hr = m_pDefHandler->QueryInterface( IID_IRunnableObject, (void**)&pIRunObj );
1311 ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
1312 if ( SUCCEEDED( hr ) && pIRunObj )
1313 return pIRunObj->Run( pbc );
1316 return E_FAIL;
1320 BOOL STDMETHODCALLTYPE InprocEmbedDocument_Impl::IsRunning()
1322 if ( CheckDefHandler() )
1324 ComSmart< IRunnableObject > pIRunObj;
1325 HRESULT hr = m_pDefHandler->QueryInterface( IID_IRunnableObject, (void**)&pIRunObj );
1327 ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
1328 if ( SUCCEEDED( hr ) && pIRunObj )
1329 return pIRunObj->IsRunning();
1332 return E_FAIL;
1337 STDMETHODIMP InprocEmbedDocument_Impl::LockRunning( BOOL fLock, BOOL fLastUnlockCloses )
1339 if ( CheckDefHandler() )
1341 ComSmart< IRunnableObject > pIRunObj;
1342 HRESULT hr = m_pDefHandler->QueryInterface( IID_IRunnableObject, (void**)&pIRunObj );
1344 ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
1345 if ( SUCCEEDED( hr ) && pIRunObj )
1346 return pIRunObj->LockRunning( fLock, fLastUnlockCloses );
1349 return E_FAIL;
1353 STDMETHODIMP InprocEmbedDocument_Impl::SetContainedObject( BOOL fContained)
1355 if ( CheckDefHandler() )
1357 ComSmart< IRunnableObject > pIRunObj;
1358 HRESULT hr = m_pDefHandler->QueryInterface( IID_IRunnableObject, (void**)&pIRunObj );
1360 ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
1361 if ( SUCCEEDED( hr ) && pIRunObj )
1362 return pIRunObj->SetContainedObject( fContained );
1365 return E_FAIL;
1369 // IViewObject methods
1371 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 )
1373 if ( CheckDefHandler() )
1375 ComSmart< IViewObject > pIViewObject;
1376 HRESULT hr = m_pDefHandler->QueryInterface( IID_IViewObject, (void**)&pIViewObject );
1378 ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
1379 if ( SUCCEEDED( hr ) && pIViewObject )
1380 return pIViewObject->Draw( dwDrawAspect, lindex, pvAspect, ptd, hdcTargetDev, hdcDraw, lprcBounds, lprcWBounds, pfnContinue, dwContinue );
1383 return E_FAIL;
1387 STDMETHODIMP InprocEmbedDocument_Impl::GetColorSet( DWORD dwDrawAspect, LONG lindex, void *pvAspect, DVTARGETDEVICE *ptd, HDC hicTargetDev, LOGPALETTE **ppColorSet )
1389 if ( CheckDefHandler() )
1391 ComSmart< IViewObject > pIViewObject;
1392 HRESULT hr = m_pDefHandler->QueryInterface( IID_IViewObject, (void**)&pIViewObject );
1394 ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
1395 if ( SUCCEEDED( hr ) && pIViewObject )
1396 return pIViewObject->GetColorSet( dwDrawAspect, lindex, pvAspect, ptd, hicTargetDev, ppColorSet );
1399 return E_FAIL;
1403 STDMETHODIMP InprocEmbedDocument_Impl::Freeze( DWORD dwDrawAspect, LONG lindex, void *pvAspect, DWORD *pdwFreeze )
1405 if ( CheckDefHandler() )
1407 ComSmart< IViewObject > pIViewObject;
1408 HRESULT hr = m_pDefHandler->QueryInterface( IID_IViewObject, (void**)&pIViewObject );
1410 ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
1411 if ( SUCCEEDED( hr ) && pIViewObject )
1412 return pIViewObject->Freeze( dwDrawAspect, lindex, pvAspect, pdwFreeze );
1415 return E_FAIL;
1419 STDMETHODIMP InprocEmbedDocument_Impl::Unfreeze( DWORD dwFreeze )
1421 if ( CheckDefHandler() )
1423 ComSmart< IViewObject > pIViewObject;
1424 HRESULT hr = m_pDefHandler->QueryInterface( IID_IViewObject, (void**)&pIViewObject );
1426 ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
1427 if ( SUCCEEDED( hr ) && pIViewObject )
1428 return pIViewObject->Unfreeze( dwFreeze );
1431 return E_FAIL;
1435 STDMETHODIMP InprocEmbedDocument_Impl::SetAdvise( DWORD aspects, DWORD advf, IAdviseSink *pAdvSink )
1438 // CheckDefHandler will set the listener, avoid reusing of old listener
1439 if ( m_pViewAdvise )
1441 m_pViewAdvise->DisconnectOrigAdvise();
1442 m_pViewAdvise = NULL;
1445 if ( pAdvSink && CheckDefHandler() )
1447 ComSmart< IViewObject > pIViewObject;
1448 HRESULT hr = m_pDefHandler->QueryInterface( IID_IViewObject, (void**)&pIViewObject );
1450 ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
1451 if ( SUCCEEDED( hr ) && pIViewObject )
1453 ComSmart< OleWrapperAdviseSink > pOwnAdvise( new OleWrapperAdviseSink( pAdvSink, aspects, advf ) );
1455 if ( SUCCEEDED( pIViewObject->SetAdvise( aspects, advf, pOwnAdvise ) ) )
1457 m_pViewAdvise = pOwnAdvise;
1458 return S_OK;
1463 return S_OK;
1467 STDMETHODIMP InprocEmbedDocument_Impl::GetAdvise( DWORD *pAspects, DWORD *pAdvf, IAdviseSink **ppAdvSink )
1469 if ( !ppAdvSink )
1470 return E_INVALIDARG;
1472 if ( m_pViewAdvise )
1474 if ( pAspects )
1475 *pAspects = m_pViewAdvise->GetAspect();
1477 if ( pAdvf )
1478 *pAdvf = m_pViewAdvise->GetViewAdviseFlag();
1480 *ppAdvSink = m_pViewAdvise->GetOrigAdvise();
1481 if ( *ppAdvSink )
1482 (*ppAdvSink)->AddRef();
1484 else
1485 *ppAdvSink = NULL;
1487 return S_OK;
1490 // IViewObject2 methods
1492 STDMETHODIMP InprocEmbedDocument_Impl::GetExtent( DWORD dwDrawAspect, LONG lindex, DVTARGETDEVICE *ptd, LPSIZEL lpsizel )
1494 if ( CheckDefHandler() )
1496 ComSmart< IViewObject2 > pIViewObject2;
1497 HRESULT hr = m_pDefHandler->QueryInterface( IID_IViewObject2, (void**)&pIViewObject2 );
1499 ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
1500 if ( SUCCEEDED( hr ) && pIViewObject2 )
1501 return pIViewObject2->GetExtent( dwDrawAspect, lindex, ptd, lpsizel );
1504 return E_FAIL;
1509 // IOleWindow methods
1511 STDMETHODIMP InprocEmbedDocument_Impl::GetWindow( HWND *phwnd )
1513 if ( CheckDefHandler() )
1515 ComSmart< IOleWindow > pIOleWindow;
1516 HRESULT hr = m_pDefHandler->QueryInterface( IID_IOleWindow, (void**)&pIOleWindow );
1518 ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
1519 if ( SUCCEEDED( hr ) && pIOleWindow )
1520 return pIOleWindow->GetWindow( phwnd );
1523 return E_FAIL;
1527 STDMETHODIMP InprocEmbedDocument_Impl::ContextSensitiveHelp( BOOL fEnterMode )
1529 if ( CheckDefHandler() )
1531 ComSmart< IOleWindow > pIOleWindow;
1532 HRESULT hr = m_pDefHandler->QueryInterface( IID_IOleWindow, (void**)&pIOleWindow );
1534 ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
1535 if ( SUCCEEDED( hr ) && pIOleWindow )
1536 return pIOleWindow->ContextSensitiveHelp( fEnterMode );
1539 return E_FAIL;
1543 // IOleInPlaceObject methods
1545 STDMETHODIMP InprocEmbedDocument_Impl::InPlaceDeactivate()
1547 if ( CheckDefHandler() )
1549 ComSmart< IOleInPlaceObject > pIOleInPlaceObject;
1550 HRESULT hr = m_pDefHandler->QueryInterface( IID_IOleInPlaceObject, (void**)&pIOleInPlaceObject );
1552 ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
1553 if ( SUCCEEDED( hr ) && pIOleInPlaceObject )
1554 return pIOleInPlaceObject->InPlaceDeactivate();
1557 return E_FAIL;
1561 STDMETHODIMP InprocEmbedDocument_Impl::UIDeactivate()
1563 if ( CheckDefHandler() )
1565 ComSmart< IOleInPlaceObject > pIOleInPlaceObject;
1566 HRESULT hr = m_pDefHandler->QueryInterface( IID_IOleInPlaceObject, (void**)&pIOleInPlaceObject );
1568 ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
1569 if ( SUCCEEDED( hr ) && pIOleInPlaceObject )
1570 return pIOleInPlaceObject->UIDeactivate();
1573 return E_FAIL;
1577 STDMETHODIMP InprocEmbedDocument_Impl::SetObjectRects( LPCRECT lprcPosRect, LPCRECT lprcClipRect )
1579 if ( CheckDefHandler() )
1581 ComSmart< IOleInPlaceObject > pIOleInPlaceObject;
1582 HRESULT hr = m_pDefHandler->QueryInterface( IID_IOleInPlaceObject, (void**)&pIOleInPlaceObject );
1584 ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
1585 if ( SUCCEEDED( hr ) && pIOleInPlaceObject )
1586 return pIOleInPlaceObject->SetObjectRects( lprcPosRect, lprcClipRect );
1589 return E_FAIL;
1593 STDMETHODIMP InprocEmbedDocument_Impl::ReactivateAndUndo()
1595 if ( CheckDefHandler() )
1597 ComSmart< IOleInPlaceObject > pIOleInPlaceObject;
1598 HRESULT hr = m_pDefHandler->QueryInterface( IID_IOleInPlaceObject, (void**)&pIOleInPlaceObject );
1600 ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
1601 if ( SUCCEEDED( hr ) && pIOleInPlaceObject )
1602 return pIOleInPlaceObject->ReactivateAndUndo();
1605 return E_FAIL;
1609 // IDispatch methods
1611 STDMETHODIMP InprocEmbedDocument_Impl::GetTypeInfoCount( UINT *pctinfo )
1613 if ( CheckDefHandler() )
1615 ComSmart< IDispatch > pIDispatch;
1616 HRESULT hr = m_pDefHandler->QueryInterface( IID_IDispatch, (void**)&pIDispatch );
1618 ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
1619 if ( SUCCEEDED( hr ) && pIDispatch )
1620 return pIDispatch->GetTypeInfoCount( pctinfo );
1623 return E_FAIL;
1627 STDMETHODIMP InprocEmbedDocument_Impl::GetTypeInfo( UINT iTInfo, LCID lcid, ITypeInfo **ppTInfo )
1629 if ( CheckDefHandler() )
1631 ComSmart< IDispatch > pIDispatch;
1632 HRESULT hr = m_pDefHandler->QueryInterface( IID_IDispatch, (void**)&pIDispatch );
1634 ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
1635 if ( SUCCEEDED( hr ) && pIDispatch )
1636 return pIDispatch->GetTypeInfo( iTInfo, lcid, ppTInfo );
1639 return E_FAIL;
1643 STDMETHODIMP InprocEmbedDocument_Impl::GetIDsOfNames( REFIID riid, LPOLESTR *rgszNames, UINT cNames, LCID lcid, DISPID *rgDispId )
1645 if ( CheckDefHandler() )
1647 ComSmart< IDispatch > pIDispatch;
1648 HRESULT hr = m_pDefHandler->QueryInterface( IID_IDispatch, (void**)&pIDispatch );
1650 ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
1651 if ( SUCCEEDED( hr ) && pIDispatch )
1652 return pIDispatch->GetIDsOfNames( riid, rgszNames, cNames, lcid, rgDispId );
1655 return E_FAIL;
1659 STDMETHODIMP InprocEmbedDocument_Impl::Invoke( DISPID dispIdMember, REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams, VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr )
1661 if ( CheckDefHandler() )
1663 ComSmart< IDispatch > pIDispatch;
1664 HRESULT hr = m_pDefHandler->QueryInterface( IID_IDispatch, (void**)&pIDispatch );
1666 ULONGGuard aGuard( &m_nCallsOnStack ); // avoid reentrance problem
1667 if ( SUCCEEDED( hr ) && pIDispatch )
1668 return pIDispatch->Invoke( dispIdMember, riid, lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr );
1671 return E_FAIL;
1676 // InternalCacheWrapper
1679 // IUnknown
1681 STDMETHODIMP InprocEmbedDocument_Impl::InternalCacheWrapper::QueryInterface( REFIID riid, void FAR* FAR* ppv )
1683 return m_rOwnDocument.QueryInterface( riid, ppv );
1687 STDMETHODIMP_(ULONG) InprocEmbedDocument_Impl::InternalCacheWrapper::AddRef()
1689 return m_rOwnDocument.AddRef();
1693 STDMETHODIMP_(ULONG) InprocEmbedDocument_Impl::InternalCacheWrapper::Release()
1695 return m_rOwnDocument.Release();
1698 // IOleCache methods
1700 STDMETHODIMP InprocEmbedDocument_Impl::InternalCacheWrapper::Cache( FORMATETC *pformatetc, DWORD advf, DWORD *pdwConnection )
1702 if ( m_rOwnDocument.CheckDefHandler() )
1704 ComSmart< IOleCache > pIOleCache;
1705 HRESULT hr = m_rOwnDocument.GetDefHandler()->QueryInterface( IID_IOleCache, (void**)&pIOleCache );
1707 ULONGGuard aGuard( &m_rOwnDocument.m_nCallsOnStack ); // avoid reentrance problem
1708 if ( SUCCEEDED( hr ) && pIOleCache )
1709 return pIOleCache->Cache( pformatetc, advf, pdwConnection );
1712 return E_FAIL;
1716 STDMETHODIMP InprocEmbedDocument_Impl::InternalCacheWrapper::Uncache( DWORD dwConnection )
1718 if ( m_rOwnDocument.CheckDefHandler() )
1720 ComSmart< IOleCache > pIOleCache;
1721 HRESULT hr = m_rOwnDocument.GetDefHandler()->QueryInterface( IID_IOleCache, (void**)&pIOleCache );
1723 ULONGGuard aGuard( &m_rOwnDocument.m_nCallsOnStack ); // avoid reentrance problem
1724 if ( SUCCEEDED( hr ) && pIOleCache )
1725 return pIOleCache->Uncache( dwConnection );
1728 return E_FAIL;
1732 STDMETHODIMP InprocEmbedDocument_Impl::InternalCacheWrapper::EnumCache( IEnumSTATDATA **ppenumSTATDATA )
1734 if ( m_rOwnDocument.CheckDefHandler() )
1736 ComSmart< IOleCache > pIOleCache;
1737 HRESULT hr = m_rOwnDocument.GetDefHandler()->QueryInterface( IID_IOleCache, (void**)&pIOleCache );
1739 ULONGGuard aGuard( &m_rOwnDocument.m_nCallsOnStack ); // avoid reentrance problem
1740 if ( SUCCEEDED( hr ) && pIOleCache )
1741 return pIOleCache->EnumCache( ppenumSTATDATA );
1744 return E_FAIL;
1748 STDMETHODIMP InprocEmbedDocument_Impl::InternalCacheWrapper::InitCache( IDataObject *pDataObject )
1750 if ( m_rOwnDocument.CheckDefHandler() )
1752 ComSmart< IOleCache > pIOleCache;
1753 HRESULT hr = m_rOwnDocument.GetDefHandler()->QueryInterface( IID_IOleCache, (void**)&pIOleCache );
1755 ULONGGuard aGuard( &m_rOwnDocument.m_nCallsOnStack ); // avoid reentrance problem
1756 if ( SUCCEEDED( hr ) && pIOleCache )
1757 return pIOleCache->InitCache( pDataObject );
1760 return E_FAIL;
1764 STDMETHODIMP InprocEmbedDocument_Impl::InternalCacheWrapper::SetData( FORMATETC *pformatetc, STGMEDIUM *pmedium, BOOL fRelease )
1766 if ( m_rOwnDocument.CheckDefHandler() )
1768 ComSmart< IOleCache > pIOleCache;
1769 HRESULT hr = m_rOwnDocument.GetDefHandler()->QueryInterface( IID_IOleCache, (void**)&pIOleCache );
1771 ULONGGuard aGuard( &m_rOwnDocument.m_nCallsOnStack ); // avoid reentrance problem
1772 if ( SUCCEEDED( hr ) && pIOleCache )
1773 return pIOleCache->SetData( pformatetc, pmedium, fRelease );
1776 return E_FAIL;
1779 // IOleCache2 methods
1781 STDMETHODIMP InprocEmbedDocument_Impl::InternalCacheWrapper::UpdateCache( LPDATAOBJECT pDataObject, DWORD grfUpdf, LPVOID pReserved )
1783 if ( m_rOwnDocument.CheckDefHandler() )
1785 ComSmart< IOleCache2 > pIOleCache2;
1786 HRESULT hr = m_rOwnDocument.GetDefHandler()->QueryInterface( IID_IOleCache2, (void**)&pIOleCache2 );
1788 ULONGGuard aGuard( &m_rOwnDocument.m_nCallsOnStack ); // avoid reentrance problem
1789 if ( SUCCEEDED( hr ) && pIOleCache2 )
1790 return pIOleCache2->UpdateCache( pDataObject, grfUpdf, pReserved );
1793 return E_FAIL;
1797 STDMETHODIMP InprocEmbedDocument_Impl::InternalCacheWrapper::DiscardCache( DWORD dwDiscardOptions )
1799 if ( m_rOwnDocument.CheckDefHandler() )
1801 ComSmart< IOleCache2 > pIOleCache2;
1802 HRESULT hr = m_rOwnDocument.GetDefHandler()->QueryInterface( IID_IOleCache2, (void**)&pIOleCache2 );
1804 ULONGGuard aGuard( &m_rOwnDocument.m_nCallsOnStack ); // avoid reentrance problem
1805 if ( SUCCEEDED( hr ) && pIOleCache2 )
1806 return pIOleCache2->DiscardCache( dwDiscardOptions );
1809 return E_FAIL;
1812 }; // namespace inprocserv
1814 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */