xref: /aoo41x/main/svtools/source/misc/embedhlp.cxx (revision 4fa9ac85)
15900e8ecSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
35900e8ecSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
45900e8ecSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
55900e8ecSAndrew Rist  * distributed with this work for additional information
65900e8ecSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
75900e8ecSAndrew Rist  * to you under the Apache License, Version 2.0 (the
85900e8ecSAndrew Rist  * "License"); you may not use this file except in compliance
95900e8ecSAndrew Rist  * with the License.  You may obtain a copy of the License at
105900e8ecSAndrew Rist  *
115900e8ecSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
125900e8ecSAndrew Rist  *
135900e8ecSAndrew Rist  * Unless required by applicable law or agreed to in writing,
145900e8ecSAndrew Rist  * software distributed under the License is distributed on an
155900e8ecSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
165900e8ecSAndrew Rist  * KIND, either express or implied.  See the License for the
175900e8ecSAndrew Rist  * specific language governing permissions and limitations
185900e8ecSAndrew Rist  * under the License.
195900e8ecSAndrew Rist  *
205900e8ecSAndrew Rist  *************************************************************/
215900e8ecSAndrew Rist 
225900e8ecSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_svtools.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include <svtools/embedhlp.hxx>
28cdf0e10cSrcweir #include <svtools/filter.hxx>
29cdf0e10cSrcweir #include <svtools/svtools.hrc>
30cdf0e10cSrcweir #include <svtools/svtdata.hxx>
31cdf0e10cSrcweir 
32cdf0e10cSrcweir #include <comphelper/embeddedobjectcontainer.hxx>
33cdf0e10cSrcweir #include <comphelper/seqstream.hxx>
34cdf0e10cSrcweir #include <toolkit/helper/vclunohelper.hxx>
35cdf0e10cSrcweir #include <unotools/ucbstreamhelper.hxx>
36cdf0e10cSrcweir #include <unotools/streamwrap.hxx>
37cdf0e10cSrcweir 
38cdf0e10cSrcweir #include <tools/globname.hxx>
39cdf0e10cSrcweir #include <sot/clsids.hxx>
40cdf0e10cSrcweir #include <com/sun/star/util/XModifyListener.hpp>
41cdf0e10cSrcweir #ifndef _COM_SUN_STAR_UTIL_XMODIFYiBLE_HPP_
42cdf0e10cSrcweir #include <com/sun/star/util/XModifiable.hpp>
43cdf0e10cSrcweir #endif
44cdf0e10cSrcweir #include <com/sun/star/embed/EmbedStates.hpp>
45cdf0e10cSrcweir #include <com/sun/star/embed/EmbedMisc.hpp>
46cdf0e10cSrcweir #include <com/sun/star/embed/XStateChangeListener.hpp>
47cdf0e10cSrcweir #include <com/sun/star/embed/NoVisualAreaSizeException.hpp>
48cdf0e10cSrcweir #include <com/sun/star/util/XModifiable.hpp>
49cdf0e10cSrcweir #include <com/sun/star/datatransfer/XTransferable.hpp>
50cdf0e10cSrcweir #include <com/sun/star/chart2/XDefaultSizeTransmitter.hpp>
51cdf0e10cSrcweir #include <cppuhelper/implbase4.hxx>
52cdf0e10cSrcweir #include "vcl/svapp.hxx"
53cdf0e10cSrcweir #include <rtl/logfile.hxx>
54cdf0e10cSrcweir #include <vos/mutex.hxx>
55cdf0e10cSrcweir 
56cdf0e10cSrcweir using namespace com::sun::star;
57cdf0e10cSrcweir 
58cdf0e10cSrcweir namespace svt
59cdf0e10cSrcweir {
60cdf0e10cSrcweir 
61cdf0e10cSrcweir class EmbedEventListener_Impl : public ::cppu::WeakImplHelper4 < embed::XStateChangeListener,
62cdf0e10cSrcweir 																 document::XEventListener,
63cdf0e10cSrcweir                                                                  util::XModifyListener,
64cdf0e10cSrcweir 																 util::XCloseListener >
65cdf0e10cSrcweir {
66cdf0e10cSrcweir public:
67cdf0e10cSrcweir     EmbeddedObjectRef*          pObject;
68cdf0e10cSrcweir     sal_Int32                   nState;
69cdf0e10cSrcweir 
70cdf0e10cSrcweir                                 EmbedEventListener_Impl( EmbeddedObjectRef* p ) :
71cdf0e10cSrcweir                                     pObject(p)
72cdf0e10cSrcweir                                     , nState(-1)
73cdf0e10cSrcweir                                 {}
74cdf0e10cSrcweir 
75cdf0e10cSrcweir     static EmbedEventListener_Impl* Create( EmbeddedObjectRef* );
76cdf0e10cSrcweir 
77cdf0e10cSrcweir     virtual void SAL_CALL changingState( const lang::EventObject& aEvent, ::sal_Int32 nOldState, ::sal_Int32 nNewState )
78cdf0e10cSrcweir 									throw (embed::WrongStateException, uno::RuntimeException);
79cdf0e10cSrcweir     virtual void SAL_CALL stateChanged( const lang::EventObject& aEvent, ::sal_Int32 nOldState, ::sal_Int32 nNewState )
80cdf0e10cSrcweir 									throw (uno::RuntimeException);
81cdf0e10cSrcweir     virtual void SAL_CALL queryClosing( const lang::EventObject& Source, ::sal_Bool GetsOwnership )
82cdf0e10cSrcweir                                     throw (util::CloseVetoException, uno::RuntimeException);
83cdf0e10cSrcweir     virtual void SAL_CALL notifyClosing( const lang::EventObject& Source ) throw (uno::RuntimeException);
84cdf0e10cSrcweir     virtual void SAL_CALL notifyEvent( const document::EventObject& aEvent ) throw( uno::RuntimeException );
85cdf0e10cSrcweir     virtual void SAL_CALL disposing( const lang::EventObject& aEvent ) throw( uno::RuntimeException );
86cdf0e10cSrcweir     virtual void SAL_CALL modified( const ::com::sun::star::lang::EventObject& aEvent ) throw (::com::sun::star::uno::RuntimeException);
87cdf0e10cSrcweir };
88cdf0e10cSrcweir 
89cdf0e10cSrcweir EmbedEventListener_Impl* EmbedEventListener_Impl::Create( EmbeddedObjectRef* p )
90cdf0e10cSrcweir {
91cdf0e10cSrcweir     EmbedEventListener_Impl* xRet = new EmbedEventListener_Impl( p );
92cdf0e10cSrcweir     xRet->acquire();
93cdf0e10cSrcweir 
94cdf0e10cSrcweir 	if ( p->GetObject().is() )
95cdf0e10cSrcweir 	{
96cdf0e10cSrcweir         p->GetObject()->addStateChangeListener( xRet );
97cdf0e10cSrcweir 
98cdf0e10cSrcweir     	uno::Reference < util::XCloseable > xClose( p->GetObject(), uno::UNO_QUERY );
99cdf0e10cSrcweir     	DBG_ASSERT( xClose.is(), "Object does not support XCloseable!" );
100cdf0e10cSrcweir     	if ( xClose.is() )
101cdf0e10cSrcweir             xClose->addCloseListener( xRet );
102cdf0e10cSrcweir 
103cdf0e10cSrcweir     	uno::Reference < document::XEventBroadcaster > xBrd( p->GetObject(), uno::UNO_QUERY );
104cdf0e10cSrcweir     	if ( xBrd.is() )
105cdf0e10cSrcweir         	xBrd->addEventListener( xRet );
106cdf0e10cSrcweir 
107cdf0e10cSrcweir         xRet->nState = p->GetObject()->getCurrentState();
108cdf0e10cSrcweir         if ( xRet->nState == embed::EmbedStates::RUNNING )
109cdf0e10cSrcweir         {
110cdf0e10cSrcweir             uno::Reference < util::XModifiable > xMod( p->GetObject()->getComponent(), uno::UNO_QUERY );
111cdf0e10cSrcweir             if ( xMod.is() )
112cdf0e10cSrcweir                 // listen for changes in running state (update replacements in case of changes)
113cdf0e10cSrcweir                 xMod->addModifyListener( xRet );
114cdf0e10cSrcweir         }
115cdf0e10cSrcweir 	}
116cdf0e10cSrcweir 
117cdf0e10cSrcweir     return xRet;
118cdf0e10cSrcweir }
119cdf0e10cSrcweir 
120cdf0e10cSrcweir void SAL_CALL EmbedEventListener_Impl::changingState( const lang::EventObject&,
121cdf0e10cSrcweir 													::sal_Int32,
122cdf0e10cSrcweir 													::sal_Int32 )
123cdf0e10cSrcweir 	throw ( embed::WrongStateException,
124cdf0e10cSrcweir 			uno::RuntimeException )
125cdf0e10cSrcweir {
126cdf0e10cSrcweir }
127cdf0e10cSrcweir 
128cdf0e10cSrcweir void SAL_CALL EmbedEventListener_Impl::stateChanged( const lang::EventObject&,
129cdf0e10cSrcweir 													::sal_Int32 nOldState,
130cdf0e10cSrcweir 													::sal_Int32 nNewState )
131cdf0e10cSrcweir 	throw ( uno::RuntimeException )
132cdf0e10cSrcweir {
133cdf0e10cSrcweir 	::vos::OGuard aGuard( Application::GetSolarMutex() );
134cdf0e10cSrcweir     nState = nNewState;
135cdf0e10cSrcweir     if ( !pObject )
136cdf0e10cSrcweir         return;
137cdf0e10cSrcweir 
138cdf0e10cSrcweir     uno::Reference < util::XModifiable > xMod( pObject->GetObject()->getComponent(), uno::UNO_QUERY );
139cdf0e10cSrcweir     if ( nNewState == embed::EmbedStates::RUNNING )
140cdf0e10cSrcweir     {
141cdf0e10cSrcweir         // TODO/LATER: container must be set before!
142cdf0e10cSrcweir         // When is this event created? Who sets the new container when it changed?
143cdf0e10cSrcweir         if( ( pObject->GetViewAspect() != embed::Aspects::MSOLE_ICON ) && nOldState != embed::EmbedStates::LOADED && !pObject->IsChart() )
144cdf0e10cSrcweir             // get new replacement after deactivation
145cdf0e10cSrcweir             pObject->UpdateReplacement();
146cdf0e10cSrcweir 
147cdf0e10cSrcweir         if( pObject->IsChart() && nOldState == embed::EmbedStates::UI_ACTIVE )
148cdf0e10cSrcweir         {
149cdf0e10cSrcweir             //create a new metafile replacement when leaving the edit mode
150cdf0e10cSrcweir             //for buggy documents where the old image looks different from the correct one
151cdf0e10cSrcweir             if( xMod.is() && !xMod->isModified() )//in case of modification a new replacement will be requested anyhow
152cdf0e10cSrcweir                 pObject->UpdateReplacementOnDemand();
153cdf0e10cSrcweir         }
154cdf0e10cSrcweir 
155cdf0e10cSrcweir         if ( xMod.is() && nOldState == embed::EmbedStates::LOADED )
156cdf0e10cSrcweir             // listen for changes (update replacements in case of changes)
157cdf0e10cSrcweir             xMod->addModifyListener( this );
158cdf0e10cSrcweir     }
159cdf0e10cSrcweir     else if ( nNewState == embed::EmbedStates::LOADED )
160cdf0e10cSrcweir     {
161cdf0e10cSrcweir         // in loaded state we can't listen
162cdf0e10cSrcweir         if ( xMod.is() )
163cdf0e10cSrcweir             xMod->removeModifyListener( this );
164cdf0e10cSrcweir     }
165cdf0e10cSrcweir }
166cdf0e10cSrcweir 
167cdf0e10cSrcweir void SAL_CALL EmbedEventListener_Impl::modified( const lang::EventObject& ) throw (uno::RuntimeException)
168cdf0e10cSrcweir {
169cdf0e10cSrcweir 	::vos::OGuard aGuard( Application::GetSolarMutex() );
170cdf0e10cSrcweir     if ( pObject && pObject->GetViewAspect() != embed::Aspects::MSOLE_ICON )
171cdf0e10cSrcweir     {
172cdf0e10cSrcweir         if ( nState == embed::EmbedStates::RUNNING )
173cdf0e10cSrcweir         {
174cdf0e10cSrcweir             // updates only necessary in non-active states
175cdf0e10cSrcweir             if( pObject->IsChart() )
176cdf0e10cSrcweir                 pObject->UpdateReplacementOnDemand();
177cdf0e10cSrcweir             else
178cdf0e10cSrcweir                 pObject->UpdateReplacement();
179cdf0e10cSrcweir         }
180cdf0e10cSrcweir         else if ( nState == embed::EmbedStates::UI_ACTIVE || nState == embed::EmbedStates::INPLACE_ACTIVE )
181cdf0e10cSrcweir         {
182cdf0e10cSrcweir             // in case the object is inplace or UI active the replacement image should be updated on demand
183cdf0e10cSrcweir             pObject->UpdateReplacementOnDemand();
184cdf0e10cSrcweir         }
185cdf0e10cSrcweir     }
186cdf0e10cSrcweir }
187cdf0e10cSrcweir 
188cdf0e10cSrcweir void SAL_CALL EmbedEventListener_Impl::notifyEvent( const document::EventObject& aEvent ) throw( uno::RuntimeException )
189cdf0e10cSrcweir {
190cdf0e10cSrcweir 	::vos::OGuard aGuard( Application::GetSolarMutex() );
191cdf0e10cSrcweir 
192cdf0e10cSrcweir #if 0
193cdf0e10cSrcweir     if ( pObject && aEvent.EventName.equalsAscii("OnSaveDone") || aEvent.EventName.equalsAscii("OnSaveAsDone") )
194cdf0e10cSrcweir     {
195cdf0e10cSrcweir         // TODO/LATER: container must be set before!
196cdf0e10cSrcweir         // When is this event created? Who sets the new container when it changed?
197cdf0e10cSrcweir         pObject->UpdateReplacement();
198cdf0e10cSrcweir     }
199cdf0e10cSrcweir     else
200cdf0e10cSrcweir #endif
201cdf0e10cSrcweir     if ( pObject && aEvent.EventName.equalsAscii("OnVisAreaChanged") && pObject->GetViewAspect() != embed::Aspects::MSOLE_ICON && !pObject->IsChart() )
202cdf0e10cSrcweir     {
203cdf0e10cSrcweir         pObject->UpdateReplacement();
204cdf0e10cSrcweir     }
205cdf0e10cSrcweir }
206cdf0e10cSrcweir 
207cdf0e10cSrcweir void SAL_CALL EmbedEventListener_Impl::queryClosing( const lang::EventObject& Source, ::sal_Bool )
208cdf0e10cSrcweir         throw ( util::CloseVetoException, uno::RuntimeException)
209cdf0e10cSrcweir {
210cdf0e10cSrcweir     // An embedded object can be shared between several objects (f.e. for undo purposes)
211cdf0e10cSrcweir     // the object will not be closed before the last "customer" is destroyed
212cdf0e10cSrcweir     // Now the EmbeddedObjectRef helper class works like a "lock" on the object
213cdf0e10cSrcweir     if ( pObject && pObject->IsLocked() && Source.Source == pObject->GetObject() )
214cdf0e10cSrcweir         throw util::CloseVetoException();
215cdf0e10cSrcweir }
216cdf0e10cSrcweir 
217cdf0e10cSrcweir void SAL_CALL EmbedEventListener_Impl::notifyClosing( const lang::EventObject& Source ) throw (::com::sun::star::uno::RuntimeException)
218cdf0e10cSrcweir {
219cdf0e10cSrcweir     if ( pObject && Source.Source == pObject->GetObject() )
220cdf0e10cSrcweir     {
221cdf0e10cSrcweir         pObject->Clear();
222cdf0e10cSrcweir         pObject = 0;
223cdf0e10cSrcweir     }
224cdf0e10cSrcweir }
225cdf0e10cSrcweir 
226cdf0e10cSrcweir void SAL_CALL EmbedEventListener_Impl::disposing( const lang::EventObject& aEvent ) throw( uno::RuntimeException )
227cdf0e10cSrcweir {
228cdf0e10cSrcweir     if ( pObject && aEvent.Source == pObject->GetObject() )
229cdf0e10cSrcweir     {
230cdf0e10cSrcweir         pObject->Clear();
231cdf0e10cSrcweir         pObject = 0;
232cdf0e10cSrcweir     }
233cdf0e10cSrcweir }
234cdf0e10cSrcweir 
235cdf0e10cSrcweir struct EmbeddedObjectRef_Impl
236cdf0e10cSrcweir {
237cdf0e10cSrcweir     EmbedEventListener_Impl*                    xListener;
238cdf0e10cSrcweir     ::rtl::OUString                             aPersistName;
239cdf0e10cSrcweir     ::rtl::OUString                             aMediaType;
240cdf0e10cSrcweir     comphelper::EmbeddedObjectContainer*        pContainer;
241cdf0e10cSrcweir     Graphic*                                    pGraphic;
242cdf0e10cSrcweir     Graphic*                                    pHCGraphic;
243cdf0e10cSrcweir     sal_Int64                                   nViewAspect;
244cdf0e10cSrcweir     sal_Bool                                        bIsLocked;
245cdf0e10cSrcweir     sal_Bool                                    bNeedUpdate;
246cdf0e10cSrcweir 
247cdf0e10cSrcweir     // #i104867#
248cdf0e10cSrcweir     sal_uInt32                                  mnGraphicVersion;
249cdf0e10cSrcweir     awt::Size                                   aDefaultSizeForChart_In_100TH_MM;//#i103460# charts do not necessaryly have an own size within ODF files, in this case they need to use the size settings from the surrounding frame, which is made available with this member
250cdf0e10cSrcweir };
251cdf0e10cSrcweir 
252cdf0e10cSrcweir void EmbeddedObjectRef::Construct_Impl()
253cdf0e10cSrcweir {
254cdf0e10cSrcweir     mpImp = new EmbeddedObjectRef_Impl;
255cdf0e10cSrcweir     mpImp->pContainer = 0;
256cdf0e10cSrcweir     mpImp->pGraphic = 0;
257cdf0e10cSrcweir     mpImp->pHCGraphic = 0;
258cdf0e10cSrcweir     mpImp->nViewAspect = embed::Aspects::MSOLE_CONTENT;
259cdf0e10cSrcweir     mpImp->bIsLocked = sal_False;
260cdf0e10cSrcweir     mpImp->bNeedUpdate = sal_False;
261cdf0e10cSrcweir     mpImp->mnGraphicVersion = 0;
262cdf0e10cSrcweir     mpImp->aDefaultSizeForChart_In_100TH_MM = awt::Size(8000,7000);
263cdf0e10cSrcweir }
264cdf0e10cSrcweir 
265cdf0e10cSrcweir EmbeddedObjectRef::EmbeddedObjectRef()
266cdf0e10cSrcweir {
267cdf0e10cSrcweir     Construct_Impl();
268cdf0e10cSrcweir }
269cdf0e10cSrcweir 
270cdf0e10cSrcweir EmbeddedObjectRef::EmbeddedObjectRef( const NS_UNO::Reference < NS_EMBED::XEmbeddedObject >& xObj, sal_Int64 nAspect )
271cdf0e10cSrcweir {
272cdf0e10cSrcweir     Construct_Impl();
273cdf0e10cSrcweir     mpImp->nViewAspect = nAspect;
274cdf0e10cSrcweir     mxObj = xObj;
275cdf0e10cSrcweir     mpImp->xListener = EmbedEventListener_Impl::Create( this );
276cdf0e10cSrcweir }
277cdf0e10cSrcweir 
278cdf0e10cSrcweir EmbeddedObjectRef::EmbeddedObjectRef( const EmbeddedObjectRef& rObj )
279cdf0e10cSrcweir {
280cdf0e10cSrcweir     mpImp = new EmbeddedObjectRef_Impl;
281cdf0e10cSrcweir     mpImp->pContainer = rObj.mpImp->pContainer;
282cdf0e10cSrcweir     mpImp->nViewAspect = rObj.mpImp->nViewAspect;
283cdf0e10cSrcweir     mpImp->bIsLocked = rObj.mpImp->bIsLocked;
284cdf0e10cSrcweir     mxObj = rObj.mxObj;
285cdf0e10cSrcweir     mpImp->xListener = EmbedEventListener_Impl::Create( this );
286cdf0e10cSrcweir     mpImp->aPersistName = rObj.mpImp->aPersistName;
287cdf0e10cSrcweir     mpImp->aMediaType = rObj.mpImp->aMediaType;
288cdf0e10cSrcweir     mpImp->bNeedUpdate = rObj.mpImp->bNeedUpdate;
289cdf0e10cSrcweir     mpImp->aDefaultSizeForChart_In_100TH_MM = rObj.mpImp->aDefaultSizeForChart_In_100TH_MM;
290cdf0e10cSrcweir 
291cdf0e10cSrcweir     if ( rObj.mpImp->pGraphic && !rObj.mpImp->bNeedUpdate )
292cdf0e10cSrcweir         mpImp->pGraphic = new Graphic( *rObj.mpImp->pGraphic );
293cdf0e10cSrcweir     else
294cdf0e10cSrcweir         mpImp->pGraphic = 0;
295cdf0e10cSrcweir 
296cdf0e10cSrcweir     mpImp->pHCGraphic = 0;
297cdf0e10cSrcweir     mpImp->mnGraphicVersion = 0;
298cdf0e10cSrcweir }
299cdf0e10cSrcweir 
300cdf0e10cSrcweir EmbeddedObjectRef::~EmbeddedObjectRef()
301cdf0e10cSrcweir {
302cdf0e10cSrcweir     delete mpImp->pGraphic;
303cdf0e10cSrcweir 	if ( mpImp->pHCGraphic )
304cdf0e10cSrcweir         DELETEZ( mpImp->pHCGraphic );
305cdf0e10cSrcweir     Clear();
306*4fa9ac85SHerbert Dürr     delete mpImp;
307cdf0e10cSrcweir }
308cdf0e10cSrcweir /*
309cdf0e10cSrcweir EmbeddedObjectRef& EmbeddedObjectRef::operator = ( const EmbeddedObjectRef& rObj )
310cdf0e10cSrcweir {
311cdf0e10cSrcweir     DBG_ASSERT( !mxObj.is(), "Never assign an already assigned object!" );
312cdf0e10cSrcweir 
313cdf0e10cSrcweir     delete mpImp->pGraphic;
314cdf0e10cSrcweir 	if ( mpImp->pHCGraphic ) DELETEZ( mpImp->pHCGraphic );
315cdf0e10cSrcweir     Clear();
316cdf0e10cSrcweir 
317cdf0e10cSrcweir     mpImp->nViewAspect = rObj.mpImp->nViewAspect;
318cdf0e10cSrcweir     mpImp->bIsLocked = rObj.mpImp->bIsLocked;
319cdf0e10cSrcweir     mxObj = rObj.mxObj;
320cdf0e10cSrcweir     mpImp->xListener = EmbedEventListener_Impl::Create( this );
321cdf0e10cSrcweir     mpImp->pContainer = rObj.mpImp->pContainer;
322cdf0e10cSrcweir     mpImp->aPersistName = rObj.mpImp->aPersistName;
323cdf0e10cSrcweir     mpImp->aMediaType = rObj.mpImp->aMediaType;
324cdf0e10cSrcweir     mpImp->bNeedUpdate = rObj.mpImp->bNeedUpdate;
325cdf0e10cSrcweir 
326cdf0e10cSrcweir     if ( rObj.mpImp->pGraphic && !rObj.mpImp->bNeedUpdate )
327cdf0e10cSrcweir         mpImp->pGraphic = new Graphic( *rObj.mpImp->pGraphic );
328cdf0e10cSrcweir     else
329cdf0e10cSrcweir         mpImp->pGraphic = 0;
330cdf0e10cSrcweir     return *this;
331cdf0e10cSrcweir }
332cdf0e10cSrcweir */
333cdf0e10cSrcweir void EmbeddedObjectRef::Assign( const NS_UNO::Reference < NS_EMBED::XEmbeddedObject >& xObj, sal_Int64 nAspect )
334cdf0e10cSrcweir {
335cdf0e10cSrcweir     DBG_ASSERT( !mxObj.is(), "Never assign an already assigned object!" );
336cdf0e10cSrcweir 
337cdf0e10cSrcweir     Clear();
338cdf0e10cSrcweir     mpImp->nViewAspect = nAspect;
339cdf0e10cSrcweir     mxObj = xObj;
340cdf0e10cSrcweir     mpImp->xListener = EmbedEventListener_Impl::Create( this );
341cdf0e10cSrcweir 
342cdf0e10cSrcweir     //#i103460#
343cdf0e10cSrcweir     if ( IsChart() )
344cdf0e10cSrcweir     {
345cdf0e10cSrcweir         ::com::sun::star::uno::Reference < ::com::sun::star::chart2::XDefaultSizeTransmitter > xSizeTransmitter( xObj, uno::UNO_QUERY );
346cdf0e10cSrcweir         DBG_ASSERT( xSizeTransmitter.is(), "Object does not support XDefaultSizeTransmitter -> will cause #i103460#!" );
347cdf0e10cSrcweir         if( xSizeTransmitter.is() )
348cdf0e10cSrcweir             xSizeTransmitter->setDefaultSize( mpImp->aDefaultSizeForChart_In_100TH_MM );
349cdf0e10cSrcweir     }
350cdf0e10cSrcweir }
351cdf0e10cSrcweir 
352cdf0e10cSrcweir void EmbeddedObjectRef::Clear()
353cdf0e10cSrcweir {
354cdf0e10cSrcweir     if ( mxObj.is() && mpImp->xListener )
355cdf0e10cSrcweir     {
356cdf0e10cSrcweir         mxObj->removeStateChangeListener( mpImp->xListener );
357cdf0e10cSrcweir 
358cdf0e10cSrcweir         uno::Reference < util::XCloseable > xClose( mxObj, uno::UNO_QUERY );
359cdf0e10cSrcweir         if ( xClose.is() )
360cdf0e10cSrcweir             xClose->removeCloseListener( mpImp->xListener );
361cdf0e10cSrcweir 
362cdf0e10cSrcweir         uno::Reference < document::XEventBroadcaster > xBrd( mxObj, uno::UNO_QUERY );
363cdf0e10cSrcweir         if ( xBrd.is() )
364cdf0e10cSrcweir             xBrd->removeEventListener( mpImp->xListener );
365cdf0e10cSrcweir 
366cdf0e10cSrcweir         if ( mpImp->bIsLocked )
367cdf0e10cSrcweir         {
368cdf0e10cSrcweir             if ( xClose.is() )
369cdf0e10cSrcweir             {
370cdf0e10cSrcweir                 try
371cdf0e10cSrcweir                 {
372cdf0e10cSrcweir                     mxObj->changeState( embed::EmbedStates::LOADED );
373cdf0e10cSrcweir                     xClose->close( sal_True );
374cdf0e10cSrcweir                 }
375cdf0e10cSrcweir                 catch ( util::CloseVetoException& )
376cdf0e10cSrcweir                 {
377cdf0e10cSrcweir                     // there's still someone who needs the object!
378cdf0e10cSrcweir                 }
379cdf0e10cSrcweir 				catch ( uno::Exception& )
380cdf0e10cSrcweir 				{
381cdf0e10cSrcweir 					OSL_ENSURE( sal_False, "Error on switching of the object to loaded state and closing!\n" );
382cdf0e10cSrcweir 				}
383cdf0e10cSrcweir             }
384cdf0e10cSrcweir         }
385cdf0e10cSrcweir 
386cdf0e10cSrcweir         if ( mpImp->xListener )
387cdf0e10cSrcweir         {
388cdf0e10cSrcweir             mpImp->xListener->pObject = 0;
389cdf0e10cSrcweir             mpImp->xListener->release();
390cdf0e10cSrcweir             mpImp->xListener = 0;
391cdf0e10cSrcweir         }
392cdf0e10cSrcweir 
393cdf0e10cSrcweir         mxObj = 0;
394cdf0e10cSrcweir         mpImp->bNeedUpdate = sal_False;
395cdf0e10cSrcweir     }
396cdf0e10cSrcweir 
397cdf0e10cSrcweir     mpImp->pContainer = 0;
398cdf0e10cSrcweir     mpImp->bIsLocked = sal_False;
399cdf0e10cSrcweir     mpImp->bNeedUpdate = sal_False;
400cdf0e10cSrcweir }
401cdf0e10cSrcweir 
402cdf0e10cSrcweir void EmbeddedObjectRef::AssignToContainer( comphelper::EmbeddedObjectContainer* pContainer, const ::rtl::OUString& rPersistName )
403cdf0e10cSrcweir {
404cdf0e10cSrcweir     mpImp->pContainer = pContainer;
405cdf0e10cSrcweir     mpImp->aPersistName = rPersistName;
406cdf0e10cSrcweir 
407cdf0e10cSrcweir     if ( mpImp->pGraphic && !mpImp->bNeedUpdate && pContainer )
408cdf0e10cSrcweir 		SetGraphicToContainer( *mpImp->pGraphic, *pContainer, mpImp->aPersistName, ::rtl::OUString() );
409cdf0e10cSrcweir }
410cdf0e10cSrcweir 
411cdf0e10cSrcweir comphelper::EmbeddedObjectContainer* EmbeddedObjectRef::GetContainer() const
412cdf0e10cSrcweir {
413cdf0e10cSrcweir 	return mpImp->pContainer;
414cdf0e10cSrcweir }
415cdf0e10cSrcweir 
416cdf0e10cSrcweir ::rtl::OUString EmbeddedObjectRef::GetPersistName() const
417cdf0e10cSrcweir {
418cdf0e10cSrcweir 	return mpImp->aPersistName;
419cdf0e10cSrcweir }
420cdf0e10cSrcweir 
421cdf0e10cSrcweir MapUnit EmbeddedObjectRef::GetMapUnit() const
422cdf0e10cSrcweir {
423cdf0e10cSrcweir 	if ( mpImp->nViewAspect == embed::Aspects::MSOLE_CONTENT )
424cdf0e10cSrcweir 		return VCLUnoHelper::UnoEmbed2VCLMapUnit( mxObj->getMapUnit( mpImp->nViewAspect ) );
425cdf0e10cSrcweir 	else
426cdf0e10cSrcweir 		// TODO/LATER: currently only CONTENT aspect requires communication with the object
427cdf0e10cSrcweir 		return MAP_100TH_MM;
428cdf0e10cSrcweir }
429cdf0e10cSrcweir 
430cdf0e10cSrcweir sal_Int64 EmbeddedObjectRef::GetViewAspect() const
431cdf0e10cSrcweir {
432cdf0e10cSrcweir     return mpImp->nViewAspect;
433cdf0e10cSrcweir }
434cdf0e10cSrcweir 
435cdf0e10cSrcweir void EmbeddedObjectRef::SetViewAspect( sal_Int64 nAspect )
436cdf0e10cSrcweir {
437cdf0e10cSrcweir     mpImp->nViewAspect = nAspect;
438cdf0e10cSrcweir }
439cdf0e10cSrcweir 
440cdf0e10cSrcweir void EmbeddedObjectRef::Lock( sal_Bool bLock )
441cdf0e10cSrcweir {
442cdf0e10cSrcweir     mpImp->bIsLocked = bLock;
443cdf0e10cSrcweir }
444cdf0e10cSrcweir 
445cdf0e10cSrcweir sal_Bool EmbeddedObjectRef::IsLocked() const
446cdf0e10cSrcweir {
447cdf0e10cSrcweir     return mpImp->bIsLocked;
448cdf0e10cSrcweir }
449cdf0e10cSrcweir 
450cdf0e10cSrcweir void EmbeddedObjectRef::GetReplacement( sal_Bool bUpdate )
451cdf0e10cSrcweir {
452cdf0e10cSrcweir     if ( bUpdate )
453cdf0e10cSrcweir     {
454cdf0e10cSrcweir         DELETEZ( mpImp->pGraphic );
455cdf0e10cSrcweir         mpImp->aMediaType = ::rtl::OUString();
456cdf0e10cSrcweir         mpImp->pGraphic = new Graphic;
457cdf0e10cSrcweir 		if ( mpImp->pHCGraphic )
458cdf0e10cSrcweir             DELETEZ( mpImp->pHCGraphic );
459cdf0e10cSrcweir         mpImp->mnGraphicVersion++;
460cdf0e10cSrcweir     }
461cdf0e10cSrcweir     else if ( !mpImp->pGraphic )
462cdf0e10cSrcweir     {
463cdf0e10cSrcweir         mpImp->pGraphic = new Graphic;
464cdf0e10cSrcweir         mpImp->mnGraphicVersion++;
465cdf0e10cSrcweir     }
466cdf0e10cSrcweir     else
467cdf0e10cSrcweir     {
468cdf0e10cSrcweir         DBG_ERROR("No update, but replacement exists already!");
469cdf0e10cSrcweir         return;
470cdf0e10cSrcweir     }
471cdf0e10cSrcweir 
472cdf0e10cSrcweir     SvStream* pGraphicStream = GetGraphicStream( bUpdate );
473cdf0e10cSrcweir     if ( pGraphicStream )
474cdf0e10cSrcweir     {
475cdf0e10cSrcweir         GraphicFilter* pGF = GraphicFilter::GetGraphicFilter();
476cdf0e10cSrcweir         if( mpImp->pGraphic )
477cdf0e10cSrcweir             pGF->ImportGraphic( *mpImp->pGraphic, String(), *pGraphicStream, GRFILTER_FORMAT_DONTKNOW );
478cdf0e10cSrcweir         mpImp->mnGraphicVersion++;
479cdf0e10cSrcweir         delete pGraphicStream;
480cdf0e10cSrcweir     }
481cdf0e10cSrcweir }
482cdf0e10cSrcweir 
483cdf0e10cSrcweir Graphic* EmbeddedObjectRef::GetGraphic( ::rtl::OUString* pMediaType ) const
484cdf0e10cSrcweir {
485cdf0e10cSrcweir     if ( mpImp->bNeedUpdate )
486cdf0e10cSrcweir         // bNeedUpdate will be set to false while retrieving new replacement
487cdf0e10cSrcweir         const_cast < EmbeddedObjectRef* >(this)->GetReplacement( sal_True );
488cdf0e10cSrcweir     else if ( !mpImp->pGraphic )
489cdf0e10cSrcweir         const_cast < EmbeddedObjectRef* >(this)->GetReplacement( sal_False );
490cdf0e10cSrcweir 
491cdf0e10cSrcweir     if ( mpImp->pGraphic && pMediaType )
492cdf0e10cSrcweir         *pMediaType = mpImp->aMediaType;
493cdf0e10cSrcweir     return mpImp->pGraphic;
494cdf0e10cSrcweir }
495cdf0e10cSrcweir 
496cdf0e10cSrcweir Size EmbeddedObjectRef::GetSize( MapMode* pTargetMapMode ) const
497cdf0e10cSrcweir {
498cdf0e10cSrcweir 	MapMode aSourceMapMode( MAP_100TH_MM );
499cdf0e10cSrcweir 	Size aResult;
500cdf0e10cSrcweir 
501cdf0e10cSrcweir 	if ( mpImp->nViewAspect == embed::Aspects::MSOLE_ICON )
502cdf0e10cSrcweir 	{
503cdf0e10cSrcweir 		Graphic* pGraphic = GetGraphic();
504cdf0e10cSrcweir 		if ( pGraphic )
505cdf0e10cSrcweir 		{
506cdf0e10cSrcweir 			aSourceMapMode = pGraphic->GetPrefMapMode();
507cdf0e10cSrcweir 			aResult = pGraphic->GetPrefSize();
508cdf0e10cSrcweir 		}
509cdf0e10cSrcweir 		else
510cdf0e10cSrcweir 			aResult = Size( 2500, 2500 );
511cdf0e10cSrcweir 	}
512cdf0e10cSrcweir 	else
513cdf0e10cSrcweir 	{
514cdf0e10cSrcweir 		awt::Size aSize;
515cdf0e10cSrcweir 
516cdf0e10cSrcweir 		if ( mxObj.is() )
517cdf0e10cSrcweir 		{
518cdf0e10cSrcweir 			try
519cdf0e10cSrcweir 			{
520cdf0e10cSrcweir 				aSize = mxObj->getVisualAreaSize( mpImp->nViewAspect );
521cdf0e10cSrcweir 			}
522cdf0e10cSrcweir 			catch( embed::NoVisualAreaSizeException& )
523cdf0e10cSrcweir 			{
524cdf0e10cSrcweir 			}
525cdf0e10cSrcweir 			catch( uno::Exception& )
526cdf0e10cSrcweir 			{
527cdf0e10cSrcweir 				OSL_ENSURE( sal_False, "Something went wrong on getting of the size of the object!" );
528cdf0e10cSrcweir 			}
529cdf0e10cSrcweir 
530cdf0e10cSrcweir 			try
531cdf0e10cSrcweir 			{
532cdf0e10cSrcweir 				aSourceMapMode = VCLUnoHelper::UnoEmbed2VCLMapUnit( mxObj->getMapUnit( mpImp->nViewAspect ) );
533cdf0e10cSrcweir 			}
534cdf0e10cSrcweir 			catch( uno::Exception )
535cdf0e10cSrcweir 			{
536cdf0e10cSrcweir 				OSL_ENSURE( sal_False, "Can not get the map mode!" );
537cdf0e10cSrcweir 			}
538cdf0e10cSrcweir 		}
539cdf0e10cSrcweir 
540cdf0e10cSrcweir 		if ( !aSize.Height && !aSize.Width )
541cdf0e10cSrcweir 		{
542cdf0e10cSrcweir 			aSize.Width = 5000;
543cdf0e10cSrcweir 			aSize.Height = 5000;
544cdf0e10cSrcweir 		}
545cdf0e10cSrcweir 
546cdf0e10cSrcweir 		aResult = Size( aSize.Width, aSize.Height );
547cdf0e10cSrcweir 	}
548cdf0e10cSrcweir 
549cdf0e10cSrcweir 	if ( pTargetMapMode )
550cdf0e10cSrcweir 		aResult = OutputDevice::LogicToLogic( aResult, aSourceMapMode, *pTargetMapMode );
551cdf0e10cSrcweir 
552cdf0e10cSrcweir 	return aResult;
553cdf0e10cSrcweir }
554cdf0e10cSrcweir 
555cdf0e10cSrcweir Graphic* EmbeddedObjectRef::GetHCGraphic() const
556cdf0e10cSrcweir {
557cdf0e10cSrcweir 	if ( !mpImp->pHCGraphic )
558cdf0e10cSrcweir 	{
559cdf0e10cSrcweir 		uno::Reference< io::XInputStream > xInStream;
560cdf0e10cSrcweir 		try
561cdf0e10cSrcweir 		{
562cdf0e10cSrcweir 			// if the object needs size on load, that means that it is not our object
563cdf0e10cSrcweir 			// currently the HC mode is supported only for OOo own objects so the following
564cdf0e10cSrcweir 			// check is used as an optimization
565cdf0e10cSrcweir 			// TODO/LATER: shouldn't there be a special status flag to detect alien implementation?
566cdf0e10cSrcweir 			if ( mpImp->nViewAspect == embed::Aspects::MSOLE_CONTENT
567cdf0e10cSrcweir 			  && mxObj.is() && !( mxObj->getStatus( mpImp->nViewAspect ) & embed::EmbedMisc::EMBED_NEEDSSIZEONLOAD ) )
568cdf0e10cSrcweir 			{
569cdf0e10cSrcweir 				// TODO/LATER: optimization, it makes no sence to do it for OLE objects
570cdf0e10cSrcweir 				if ( mxObj->getCurrentState() == embed::EmbedStates::LOADED )
571cdf0e10cSrcweir 					mxObj->changeState( embed::EmbedStates::RUNNING );
572cdf0e10cSrcweir 
573cdf0e10cSrcweir 				// TODO: return for the aspect of the document
574cdf0e10cSrcweir 				embed::VisualRepresentation aVisualRepresentation;
575cdf0e10cSrcweir     			uno::Reference< datatransfer::XTransferable > xTransferable( mxObj->getComponent(), uno::UNO_QUERY );
576cdf0e10cSrcweir 				if ( !xTransferable.is() )
577cdf0e10cSrcweir 					throw uno::RuntimeException();
578cdf0e10cSrcweir 
579cdf0e10cSrcweir 				datatransfer::DataFlavor aDataFlavor(
580cdf0e10cSrcweir             			::rtl::OUString::createFromAscii(
581cdf0e10cSrcweir 								"application/x-openoffice-highcontrast-gdimetafile;windows_formatname=\"GDIMetaFile\"" ),
582cdf0e10cSrcweir 						::rtl::OUString::createFromAscii( "GDIMetaFile" ),
583cdf0e10cSrcweir 						::getCppuType( (const uno::Sequence< sal_Int8 >*) NULL ) );
584cdf0e10cSrcweir 
585cdf0e10cSrcweir 				uno::Sequence < sal_Int8 > aSeq;
586cdf0e10cSrcweir 				if ( ( xTransferable->getTransferData( aDataFlavor ) >>= aSeq ) && aSeq.getLength() )
587cdf0e10cSrcweir 					xInStream = new ::comphelper::SequenceInputStream( aSeq );
588cdf0e10cSrcweir 			}
589cdf0e10cSrcweir 		}
590cdf0e10cSrcweir 		catch ( uno::Exception& )
591cdf0e10cSrcweir 		{
592cdf0e10cSrcweir 		}
593cdf0e10cSrcweir 
594cdf0e10cSrcweir 		if ( xInStream.is() )
595cdf0e10cSrcweir 		{
596cdf0e10cSrcweir 			SvStream* pStream = NULL;
597cdf0e10cSrcweir 			pStream = ::utl::UcbStreamHelper::CreateStream( xInStream );
598cdf0e10cSrcweir 			if ( pStream )
599cdf0e10cSrcweir 			{
600cdf0e10cSrcweir 				if ( !pStream->GetError() )
601cdf0e10cSrcweir 				{
602cdf0e10cSrcweir         			GraphicFilter* pGF = GraphicFilter::GetGraphicFilter();
603cdf0e10cSrcweir 					Graphic* pGraphic = new Graphic();
604cdf0e10cSrcweir         			if ( pGF->ImportGraphic( *pGraphic, String(), *pStream, GRFILTER_FORMAT_DONTKNOW ) == 0 )
605cdf0e10cSrcweir 						mpImp->pHCGraphic = pGraphic;
606cdf0e10cSrcweir 					else
607cdf0e10cSrcweir 						delete pGraphic;
608cdf0e10cSrcweir                     mpImp->mnGraphicVersion++;
609cdf0e10cSrcweir 				}
610cdf0e10cSrcweir 
611cdf0e10cSrcweir         		delete pStream;
612cdf0e10cSrcweir 			}
613cdf0e10cSrcweir 		}
614cdf0e10cSrcweir 	}
615cdf0e10cSrcweir 
616cdf0e10cSrcweir 	return mpImp->pHCGraphic;
617cdf0e10cSrcweir }
618cdf0e10cSrcweir 
619cdf0e10cSrcweir void EmbeddedObjectRef::SetGraphicStream( const uno::Reference< io::XInputStream >& xInGrStream,
620cdf0e10cSrcweir 											const ::rtl::OUString& rMediaType )
621cdf0e10cSrcweir {
622cdf0e10cSrcweir     if ( mpImp->pGraphic )
623cdf0e10cSrcweir         delete mpImp->pGraphic;
624cdf0e10cSrcweir     mpImp->pGraphic = new Graphic();
625cdf0e10cSrcweir     mpImp->aMediaType = rMediaType;
626cdf0e10cSrcweir 	if ( mpImp->pHCGraphic )
627cdf0e10cSrcweir         DELETEZ( mpImp->pHCGraphic );
628cdf0e10cSrcweir     mpImp->mnGraphicVersion++;
629cdf0e10cSrcweir 
630cdf0e10cSrcweir     SvStream* pGraphicStream = ::utl::UcbStreamHelper::CreateStream( xInGrStream );
631cdf0e10cSrcweir 
632cdf0e10cSrcweir     if ( pGraphicStream )
633cdf0e10cSrcweir     {
634cdf0e10cSrcweir         GraphicFilter* pGF = GraphicFilter::GetGraphicFilter();
635cdf0e10cSrcweir         pGF->ImportGraphic( *mpImp->pGraphic, String(), *pGraphicStream, GRFILTER_FORMAT_DONTKNOW );
636cdf0e10cSrcweir         mpImp->mnGraphicVersion++;
637cdf0e10cSrcweir 
638cdf0e10cSrcweir 		if ( mpImp->pContainer )
639cdf0e10cSrcweir 		{
640cdf0e10cSrcweir 			pGraphicStream->Seek( 0 );
641cdf0e10cSrcweir 			uno::Reference< io::XInputStream > xInSeekGrStream = new ::utl::OSeekableInputStreamWrapper( pGraphicStream );
642cdf0e10cSrcweir 
643cdf0e10cSrcweir     		mpImp->pContainer->InsertGraphicStream( xInSeekGrStream, mpImp->aPersistName, rMediaType );
644cdf0e10cSrcweir 		}
645cdf0e10cSrcweir 
646cdf0e10cSrcweir         delete pGraphicStream;
647cdf0e10cSrcweir 	}
648cdf0e10cSrcweir 
649cdf0e10cSrcweir     mpImp->bNeedUpdate = sal_False;
650cdf0e10cSrcweir 
651cdf0e10cSrcweir }
652cdf0e10cSrcweir 
653cdf0e10cSrcweir void EmbeddedObjectRef::SetGraphic( const Graphic& rGraphic, const ::rtl::OUString& rMediaType )
654cdf0e10cSrcweir {
655cdf0e10cSrcweir     if ( mpImp->pGraphic )
656cdf0e10cSrcweir         delete mpImp->pGraphic;
657cdf0e10cSrcweir     mpImp->pGraphic = new Graphic( rGraphic );
658cdf0e10cSrcweir     mpImp->aMediaType = rMediaType;
659cdf0e10cSrcweir 	if ( mpImp->pHCGraphic )
660cdf0e10cSrcweir         DELETEZ( mpImp->pHCGraphic );
661cdf0e10cSrcweir     mpImp->mnGraphicVersion++;
662cdf0e10cSrcweir 
663cdf0e10cSrcweir     if ( mpImp->pContainer )
664cdf0e10cSrcweir 		SetGraphicToContainer( rGraphic, *mpImp->pContainer, mpImp->aPersistName, rMediaType );
665cdf0e10cSrcweir 
666cdf0e10cSrcweir     mpImp->bNeedUpdate = sal_False;
667cdf0e10cSrcweir }
668cdf0e10cSrcweir 
669cdf0e10cSrcweir SvStream* EmbeddedObjectRef::GetGraphicStream( sal_Bool bUpdate ) const
670cdf0e10cSrcweir {
671cdf0e10cSrcweir 	RTL_LOGFILE_CONTEXT( aLog, "svtools (mv76033) svt::EmbeddedObjectRef::GetGraphicStream" );
672cdf0e10cSrcweir     DBG_ASSERT( bUpdate || mpImp->pContainer, "Can't retrieve current graphic!" );
673cdf0e10cSrcweir     uno::Reference < io::XInputStream > xStream;
674cdf0e10cSrcweir     if ( mpImp->pContainer && !bUpdate )
675cdf0e10cSrcweir     {
676cdf0e10cSrcweir 		RTL_LOGFILE_CONTEXT_TRACE( aLog, "getting stream from container" );
677cdf0e10cSrcweir         // try to get graphic stream from container storage
678cdf0e10cSrcweir         xStream = mpImp->pContainer->GetGraphicStream( mxObj, &mpImp->aMediaType );
679cdf0e10cSrcweir         if ( xStream.is() )
680cdf0e10cSrcweir         {
681cdf0e10cSrcweir             const sal_Int32 nConstBufferSize = 32000;
682cdf0e10cSrcweir             SvStream *pStream = new SvMemoryStream( 32000, 32000 );
683cdf0e10cSrcweir             sal_Int32 nRead=0;
684cdf0e10cSrcweir             uno::Sequence < sal_Int8 > aSequence ( nConstBufferSize );
685cdf0e10cSrcweir             do
686cdf0e10cSrcweir             {
687cdf0e10cSrcweir                 nRead = xStream->readBytes ( aSequence, nConstBufferSize );
688cdf0e10cSrcweir                 pStream->Write( aSequence.getConstArray(), nRead );
689cdf0e10cSrcweir             }
690cdf0e10cSrcweir             while ( nRead == nConstBufferSize );
691cdf0e10cSrcweir             pStream->Seek(0);
692cdf0e10cSrcweir             return pStream;
693cdf0e10cSrcweir         }
694cdf0e10cSrcweir     }
695cdf0e10cSrcweir 
696cdf0e10cSrcweir     if ( !xStream.is() )
697cdf0e10cSrcweir     {
698cdf0e10cSrcweir 		RTL_LOGFILE_CONTEXT_TRACE( aLog, "getting stream from object" );
699cdf0e10cSrcweir         // update wanted or no stream in container storage available
700cdf0e10cSrcweir 		xStream = GetGraphicReplacementStream( mpImp->nViewAspect, mxObj, &mpImp->aMediaType );
701cdf0e10cSrcweir 
702cdf0e10cSrcweir 		if ( xStream.is() )
703cdf0e10cSrcweir 		{
704cdf0e10cSrcweir 			if ( mpImp->pContainer )
705cdf0e10cSrcweir             	mpImp->pContainer->InsertGraphicStream( xStream, mpImp->aPersistName, mpImp->aMediaType );
706cdf0e10cSrcweir 
707cdf0e10cSrcweir             SvStream* pResult = ::utl::UcbStreamHelper::CreateStream( xStream );
708cdf0e10cSrcweir             if ( pResult && bUpdate )
709cdf0e10cSrcweir                 mpImp->bNeedUpdate = sal_False;
710cdf0e10cSrcweir 
711cdf0e10cSrcweir             return pResult;
712cdf0e10cSrcweir         }
713cdf0e10cSrcweir     }
714cdf0e10cSrcweir 
715cdf0e10cSrcweir     return NULL;
716cdf0e10cSrcweir }
717cdf0e10cSrcweir 
718cdf0e10cSrcweir void EmbeddedObjectRef::DrawPaintReplacement( const Rectangle &rRect, const String &rText, OutputDevice *pOut )
719cdf0e10cSrcweir {
720cdf0e10cSrcweir 	MapMode aMM( MAP_APPFONT );
721cdf0e10cSrcweir 	Size aAppFontSz = pOut->LogicToLogic( Size( 0, 8 ), &aMM, NULL );
722cdf0e10cSrcweir 	Font aFnt( String::CreateFromAscii( RTL_CONSTASCII_STRINGPARAM( "Helvetica" ) ), aAppFontSz );
723cdf0e10cSrcweir 	aFnt.SetTransparent( sal_True );
724cdf0e10cSrcweir 	aFnt.SetColor( Color( COL_LIGHTRED ) );
725cdf0e10cSrcweir 	aFnt.SetWeight( WEIGHT_BOLD );
726cdf0e10cSrcweir 	aFnt.SetFamily( FAMILY_SWISS );
727cdf0e10cSrcweir 
728cdf0e10cSrcweir 	pOut->Push();
729cdf0e10cSrcweir 	pOut->SetBackground();
730cdf0e10cSrcweir 	pOut->SetFont( aFnt );
731cdf0e10cSrcweir 
732cdf0e10cSrcweir 	Point aPt;
733cdf0e10cSrcweir 	// Nun den Text so skalieren, dass er in das Rect passt.
734cdf0e10cSrcweir 	// Wir fangen mit der Defaultsize an und gehen 1-AppFont runter
735cdf0e10cSrcweir 	for( sal_uInt16 i = 8; i > 2; i-- )
736cdf0e10cSrcweir 	{
737cdf0e10cSrcweir 		aPt.X() = (rRect.GetWidth()  - pOut->GetTextWidth( rText )) / 2;
738cdf0e10cSrcweir 		aPt.Y() = (rRect.GetHeight() - pOut->GetTextHeight()) / 2;
739cdf0e10cSrcweir 
740cdf0e10cSrcweir 		sal_Bool bTiny = sal_False;
741cdf0e10cSrcweir 		if( aPt.X() < 0 ) bTiny = sal_True, aPt.X() = 0;
742cdf0e10cSrcweir 		if( aPt.Y() < 0 ) bTiny = sal_True, aPt.Y() = 0;
743cdf0e10cSrcweir 		if( bTiny )
744cdf0e10cSrcweir 		{
745cdf0e10cSrcweir 			// heruntergehen bei kleinen Bildern
746cdf0e10cSrcweir 			aFnt.SetSize( Size( 0, aAppFontSz.Height() * i / 8 ) );
747cdf0e10cSrcweir 			pOut->SetFont( aFnt );
748cdf0e10cSrcweir 		}
749cdf0e10cSrcweir 		else
750cdf0e10cSrcweir 			break;
751cdf0e10cSrcweir 	}
752cdf0e10cSrcweir 
753cdf0e10cSrcweir     Bitmap aBmp( SvtResId( BMP_PLUGIN ) );
754cdf0e10cSrcweir 	long nHeight = rRect.GetHeight() - pOut->GetTextHeight();
755cdf0e10cSrcweir 	long nWidth = rRect.GetWidth();
756cdf0e10cSrcweir 	if( nHeight > 0 )
757cdf0e10cSrcweir 	{
758cdf0e10cSrcweir 		aPt.Y() = nHeight;
759cdf0e10cSrcweir 		Point	aP = rRect.TopLeft();
760cdf0e10cSrcweir 		Size	aBmpSize = aBmp.GetSizePixel();
761cdf0e10cSrcweir 		// Bitmap einpassen
762cdf0e10cSrcweir 		if( nHeight * 10 / nWidth
763cdf0e10cSrcweir 		  > aBmpSize.Height() * 10 / aBmpSize.Width() )
764cdf0e10cSrcweir 		{
765cdf0e10cSrcweir 			// nach der Breite ausrichten
766cdf0e10cSrcweir 			// Proportion beibehalten
767cdf0e10cSrcweir 			long nH = nWidth * aBmpSize.Height() / aBmpSize.Width();
768cdf0e10cSrcweir 			// zentrieren
769cdf0e10cSrcweir 			aP.Y() += (nHeight - nH) / 2;
770cdf0e10cSrcweir 			nHeight = nH;
771cdf0e10cSrcweir 		}
772cdf0e10cSrcweir 		else
773cdf0e10cSrcweir 		{
774cdf0e10cSrcweir 			// nach der H"ohe ausrichten
775cdf0e10cSrcweir 			// Proportion beibehalten
776cdf0e10cSrcweir 			long nW = nHeight * aBmpSize.Width() / aBmpSize.Height();
777cdf0e10cSrcweir 			// zentrieren
778cdf0e10cSrcweir 			aP.X() += (nWidth - nW) / 2;
779cdf0e10cSrcweir 			nWidth = nW;
780cdf0e10cSrcweir 		}
781cdf0e10cSrcweir 
782cdf0e10cSrcweir 		pOut->DrawBitmap( aP, Size( nWidth, nHeight ), aBmp );
783cdf0e10cSrcweir 	}
784cdf0e10cSrcweir 
785cdf0e10cSrcweir 	pOut->IntersectClipRegion( rRect );
786cdf0e10cSrcweir 	aPt += rRect.TopLeft();
787cdf0e10cSrcweir 	pOut->DrawText( aPt, rText );
788cdf0e10cSrcweir 	pOut->Pop();
789cdf0e10cSrcweir }
790cdf0e10cSrcweir 
791cdf0e10cSrcweir void EmbeddedObjectRef::DrawShading( const Rectangle &rRect, OutputDevice *pOut )
792cdf0e10cSrcweir {
793cdf0e10cSrcweir 	GDIMetaFile * pMtf = pOut->GetConnectMetaFile();
794cdf0e10cSrcweir 	if( pMtf && pMtf->IsRecord() )
795cdf0e10cSrcweir 		return;
796cdf0e10cSrcweir 
797cdf0e10cSrcweir 	pOut->Push();
798cdf0e10cSrcweir 	pOut->SetLineColor( Color( COL_BLACK ) );
799cdf0e10cSrcweir 
800cdf0e10cSrcweir 	Size aPixSize = pOut->LogicToPixel( rRect.GetSize() );
801cdf0e10cSrcweir 	aPixSize.Width() -= 1;
802cdf0e10cSrcweir 	aPixSize.Height() -= 1;
803cdf0e10cSrcweir 	Point aPixViewPos = pOut->LogicToPixel( rRect.TopLeft() );
804cdf0e10cSrcweir 	sal_Int32 nMax = aPixSize.Width() + aPixSize.Height();
805cdf0e10cSrcweir 	for( sal_Int32 i = 5; i < nMax; i += 5 )
806cdf0e10cSrcweir 	{
807cdf0e10cSrcweir 		Point a1( aPixViewPos ), a2( aPixViewPos );
808cdf0e10cSrcweir 		if( i > aPixSize.Width() )
809cdf0e10cSrcweir 			a1 += Point( aPixSize.Width(), i - aPixSize.Width() );
810cdf0e10cSrcweir 		else
811cdf0e10cSrcweir 			a1 += Point( i, 0 );
812cdf0e10cSrcweir 		if( i > aPixSize.Height() )
813cdf0e10cSrcweir 			a2 += Point( i - aPixSize.Height(), aPixSize.Height() );
814cdf0e10cSrcweir 		else
815cdf0e10cSrcweir 			a2 += Point( 0, i );
816cdf0e10cSrcweir 
817cdf0e10cSrcweir 		pOut->DrawLine( pOut->PixelToLogic( a1 ), pOut->PixelToLogic( a2 ) );
818cdf0e10cSrcweir 	}
819cdf0e10cSrcweir 
820cdf0e10cSrcweir 	pOut->Pop();
821cdf0e10cSrcweir 
822cdf0e10cSrcweir }
823cdf0e10cSrcweir 
824cdf0e10cSrcweir sal_Bool EmbeddedObjectRef::TryRunningState()
825cdf0e10cSrcweir {
826cdf0e10cSrcweir     return TryRunningState( mxObj );
827cdf0e10cSrcweir }
828cdf0e10cSrcweir 
829cdf0e10cSrcweir sal_Bool EmbeddedObjectRef::TryRunningState( const uno::Reference < embed::XEmbeddedObject >& xEmbObj )
830cdf0e10cSrcweir {
831cdf0e10cSrcweir 	if ( !xEmbObj.is() )
832cdf0e10cSrcweir 		return sal_False;
833cdf0e10cSrcweir 
834cdf0e10cSrcweir     try
835cdf0e10cSrcweir     {
836cdf0e10cSrcweir         if ( xEmbObj->getCurrentState() == embed::EmbedStates::LOADED )
837cdf0e10cSrcweir             xEmbObj->changeState( embed::EmbedStates::RUNNING );
838cdf0e10cSrcweir     }
839cdf0e10cSrcweir     catch ( uno::Exception& )
840cdf0e10cSrcweir     {
841cdf0e10cSrcweir         return sal_False;
842cdf0e10cSrcweir     }
843cdf0e10cSrcweir 
844cdf0e10cSrcweir     return sal_True;
845cdf0e10cSrcweir }
846cdf0e10cSrcweir 
847cdf0e10cSrcweir void EmbeddedObjectRef::SetGraphicToContainer( const Graphic& rGraphic,
848cdf0e10cSrcweir                                                 comphelper::EmbeddedObjectContainer& aContainer,
849cdf0e10cSrcweir                                                 const ::rtl::OUString& aName,
850cdf0e10cSrcweir 												const ::rtl::OUString& aMediaType )
851cdf0e10cSrcweir {
852cdf0e10cSrcweir     SvMemoryStream aStream;
853cdf0e10cSrcweir     aStream.SetVersion( SOFFICE_FILEFORMAT_CURRENT );
854cdf0e10cSrcweir     if ( rGraphic.ExportNative( aStream ) )
855cdf0e10cSrcweir 	{
856cdf0e10cSrcweir 		aStream.Seek( 0 );
857cdf0e10cSrcweir 
858cdf0e10cSrcweir        	uno::Reference < io::XInputStream > xStream = new ::utl::OSeekableInputStreamWrapper( aStream );
859cdf0e10cSrcweir        	aContainer.InsertGraphicStream( xStream, aName, aMediaType );
860cdf0e10cSrcweir 	}
861cdf0e10cSrcweir     else
862cdf0e10cSrcweir         OSL_ENSURE( sal_False, "Export of graphic is failed!\n" );
863cdf0e10cSrcweir }
864cdf0e10cSrcweir 
865cdf0e10cSrcweir sal_Bool EmbeddedObjectRef::ObjectIsModified( const uno::Reference< embed::XEmbeddedObject >& xObj )
866cdf0e10cSrcweir 	throw( uno::Exception )
867cdf0e10cSrcweir {
868cdf0e10cSrcweir 	sal_Bool bResult = sal_False;
869cdf0e10cSrcweir 
870cdf0e10cSrcweir 	sal_Int32 nState = xObj->getCurrentState();
871cdf0e10cSrcweir 	if ( nState != embed::EmbedStates::LOADED && nState != embed::EmbedStates::RUNNING )
872cdf0e10cSrcweir 	{
873cdf0e10cSrcweir 		// the object is active so if the model is modified the replacement
874cdf0e10cSrcweir 		// should be retrieved from the object
875cdf0e10cSrcweir 		uno::Reference< util::XModifiable > xModifiable( xObj->getComponent(), uno::UNO_QUERY );
876cdf0e10cSrcweir 		if ( xModifiable.is() )
877cdf0e10cSrcweir 			bResult = xModifiable->isModified();
878cdf0e10cSrcweir 	}
879cdf0e10cSrcweir 
880cdf0e10cSrcweir 	return bResult;
881cdf0e10cSrcweir }
882cdf0e10cSrcweir 
883cdf0e10cSrcweir uno::Reference< io::XInputStream > EmbeddedObjectRef::GetGraphicReplacementStream(
884cdf0e10cSrcweir 																sal_Int64 nViewAspect,
885cdf0e10cSrcweir 																const uno::Reference< embed::XEmbeddedObject >& xObj,
886cdf0e10cSrcweir 																::rtl::OUString* pMediaType )
887cdf0e10cSrcweir 	throw()
888cdf0e10cSrcweir {
889cdf0e10cSrcweir     return ::comphelper::EmbeddedObjectContainer::GetGraphicReplacementStream(nViewAspect,xObj,pMediaType);
890cdf0e10cSrcweir }
891cdf0e10cSrcweir 
892cdf0e10cSrcweir void EmbeddedObjectRef::UpdateReplacementOnDemand()
893cdf0e10cSrcweir {
894cdf0e10cSrcweir     DELETEZ( mpImp->pGraphic );
895cdf0e10cSrcweir     mpImp->bNeedUpdate = sal_True;
896cdf0e10cSrcweir 	if ( mpImp->pHCGraphic )
897cdf0e10cSrcweir         DELETEZ( mpImp->pHCGraphic );
898cdf0e10cSrcweir     mpImp->mnGraphicVersion++;
899cdf0e10cSrcweir 
900cdf0e10cSrcweir     if( mpImp->pContainer )
901cdf0e10cSrcweir     {
902cdf0e10cSrcweir         //remove graphic from container thus a new up to date one is requested on save
903cdf0e10cSrcweir         mpImp->pContainer->RemoveGraphicStream( mpImp->aPersistName );
904cdf0e10cSrcweir     }
905cdf0e10cSrcweir }
906cdf0e10cSrcweir 
907cdf0e10cSrcweir sal_Bool EmbeddedObjectRef::IsChart() const
908cdf0e10cSrcweir {
909cdf0e10cSrcweir     //todo maybe for 3.0:
910cdf0e10cSrcweir     //if the changes work good for chart
911cdf0e10cSrcweir     //we should apply them for all own ole objects
912cdf0e10cSrcweir 
913cdf0e10cSrcweir     //#i83708# #i81857# #i79578# request an ole replacement image only if really necessary
914cdf0e10cSrcweir     //as this call can be very expensive and does block the user interface as long at it takes
915cdf0e10cSrcweir 
916cdf0e10cSrcweir     if ( !mxObj.is() )
917cdf0e10cSrcweir         return false;
918cdf0e10cSrcweir 
919cdf0e10cSrcweir     SvGlobalName aObjClsId( mxObj->getClassID() );
920cdf0e10cSrcweir     if(
921cdf0e10cSrcweir         SvGlobalName(SO3_SCH_CLASSID_30) == aObjClsId
922cdf0e10cSrcweir         || SvGlobalName(SO3_SCH_CLASSID_40) == aObjClsId
923cdf0e10cSrcweir         || SvGlobalName(SO3_SCH_CLASSID_50) == aObjClsId
924cdf0e10cSrcweir         || SvGlobalName(SO3_SCH_CLASSID_60) == aObjClsId)
925cdf0e10cSrcweir     {
926cdf0e10cSrcweir         return sal_True;
927cdf0e10cSrcweir     }
928cdf0e10cSrcweir 
929cdf0e10cSrcweir     return sal_False;
930cdf0e10cSrcweir }
931cdf0e10cSrcweir 
932cdf0e10cSrcweir // #i104867#
933cdf0e10cSrcweir sal_uInt32 EmbeddedObjectRef::getGraphicVersion() const
934cdf0e10cSrcweir {
935cdf0e10cSrcweir     return mpImp->mnGraphicVersion;
936cdf0e10cSrcweir }
937cdf0e10cSrcweir 
938cdf0e10cSrcweir void EmbeddedObjectRef::SetDefaultSizeForChart( const Size& rSizeIn_100TH_MM )
939cdf0e10cSrcweir {
940cdf0e10cSrcweir     //#i103460# charts do not necessaryly have an own size within ODF files,
941cdf0e10cSrcweir     //for this case they need to use the size settings from the surrounding frame,
942cdf0e10cSrcweir     //which is made available with this method
943cdf0e10cSrcweir 
944cdf0e10cSrcweir     mpImp->aDefaultSizeForChart_In_100TH_MM = awt::Size( rSizeIn_100TH_MM.getWidth(), rSizeIn_100TH_MM.getHeight() );
945cdf0e10cSrcweir 
946cdf0e10cSrcweir     ::com::sun::star::uno::Reference < ::com::sun::star::chart2::XDefaultSizeTransmitter > xSizeTransmitter( mxObj, uno::UNO_QUERY );
947cdf0e10cSrcweir     DBG_ASSERT( xSizeTransmitter.is(), "Object does not support XDefaultSizeTransmitter -> will cause #i103460#!" );
948cdf0e10cSrcweir     if( xSizeTransmitter.is() )
949cdf0e10cSrcweir         xSizeTransmitter->setDefaultSize( mpImp->aDefaultSizeForChart_In_100TH_MM );
950cdf0e10cSrcweir }
951cdf0e10cSrcweir 
952cdf0e10cSrcweir } // namespace svt
953cdf0e10cSrcweir 
954