xref: /aoo41x/main/svx/source/svdraw/svdoole2.cxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_svx.hxx"
30 
31 #include <svx/svdoole2.hxx>
32 #include <com/sun/star/util/XModifyBroadcaster.hpp>
33 #include <com/sun/star/util/XModifiable.hpp>
34 #include <com/sun/star/embed/EmbedStates.hpp>
35 #include <com/sun/star/embed/ElementModes.hpp>
36 #include <com/sun/star/embed/EmbedMisc.hpp>
37 #include <com/sun/star/embed/Aspects.hpp>
38 #include <com/sun/star/embed/XInplaceClient.hpp>
39 #include <com/sun/star/embed/XInplaceObject.hpp>
40 #include <com/sun/star/embed/XLinkageSupport.hpp>
41 #include <com/sun/star/embed/NoVisualAreaSizeException.hpp>
42 #include <com/sun/star/embed/XWindowSupplier.hpp>
43 #include <com/sun/star/document/XEventListener.hpp>
44 #include <com/sun/star/container/XChild.hpp>
45 #include "com/sun/star/document/XStorageBasedDocument.hpp"
46 
47 #include <comphelper/processfactory.hxx>
48 #include <cppuhelper/exc_hlp.hxx>
49 #include <unotools/ucbstreamhelper.hxx>
50 
51 #include <toolkit/helper/vclunohelper.hxx>
52 #include <toolkit/awt/vclxwindow.hxx>
53 #include <toolkit/helper/convert.hxx>
54 
55 #include <svtools/filter.hxx>
56 #include <svtools/embedhlp.hxx>
57 
58 #include <sfx2/objsh.hxx>
59 #include <sfx2/ipclient.hxx>
60 #include <sfx2/lnkbase.hxx>
61 #include <tools/stream.hxx>
62 #include <comphelper/anytostring.hxx>
63 #include <svx/svdpagv.hxx>
64 #include <tools/globname.hxx>
65 #include <vcl/jobset.hxx>
66 #include <sot/clsids.hxx>
67 
68 #include <sot/formats.hxx>
69 #include <sfx2/linkmgr.hxx>
70 #include <svtools/transfer.hxx>
71 #include <cppuhelper/implbase5.hxx>
72 
73 #include <svl/solar.hrc>
74 #include <svl/urihelper.hxx>
75 #include <vos/mutex.hxx>
76 #include <vcl/svapp.hxx>
77 
78 #include <svx/svdpagv.hxx>
79 #include <svx/svdmodel.hxx>
80 #include "svx/svdglob.hxx"  // Stringcache
81 #include "svx/svdstr.hrc"   // Objektname
82 #include <svx/svdetc.hxx>
83 #include <svx/svdview.hxx>
84 #include "unomlstr.hxx"
85 #include <svtools/chartprettypainter.hxx>
86 #include <svx/sdr/contact/viewcontactofsdrole2obj.hxx>
87 #include <svx/svdograf.hxx>
88 #include <svx/sdr/properties/oleproperties.hxx>
89 
90 // #i100710#
91 #include <svx/xlnclit.hxx>
92 #include <svx/xbtmpit.hxx>
93 #include <svx/xflbmtit.hxx>
94 #include <svx/xflbstit.hxx>
95 
96 using namespace ::rtl;
97 using namespace ::com::sun::star;
98 
99 uno::Reference < beans::XPropertySet > lcl_getFrame_throw(const SdrOle2Obj* _pObject)
100 {
101     uno::Reference < beans::XPropertySet > xFrame;
102     if ( _pObject )
103     {
104         uno::Reference< frame::XController> xController = _pObject->GetParentXModel()->getCurrentController();
105         if ( xController.is() )
106         {
107             xFrame.set( xController->getFrame(),uno::UNO_QUERY_THROW);
108         }
109     } // if ( _pObject )
110     return xFrame;
111 }
112 
113 class SdrLightEmbeddedClient_Impl : public ::cppu::WeakImplHelper5
114 															< embed::XStateChangeListener
115 															, document::XEventListener
116                                                             , embed::XInplaceClient
117 															, embed::XEmbeddedClient
118                                                             , embed::XWindowSupplier
119                                                             >
120 {
121     uno::Reference< awt::XWindow > m_xWindow;
122 	SdrOle2Obj* mpObj;
123 
124 	Fraction m_aScaleWidth;
125 	Fraction m_aScaleHeight;
126 
127 
128 public:
129     SdrLightEmbeddedClient_Impl( SdrOle2Obj* pObj );
130     void Release();
131 
132 	void SetSizeScale( const Fraction& aScaleWidth, const Fraction& aScaleHeight )
133 	{
134 		m_aScaleWidth = aScaleWidth;
135 		m_aScaleHeight = aScaleHeight;
136 	}
137 
138 	Fraction GetScaleWidth() const { return m_aScaleWidth; }
139 	Fraction GetScaleHeight() const { return m_aScaleHeight; }
140 
141     void setWindow(const uno::Reference< awt::XWindow >& _xWindow);
142 
143 private:
144     Rectangle impl_getScaledRect_nothrow() const;
145 	// XStateChangeListener
146     virtual void SAL_CALL changingState( const ::com::sun::star::lang::EventObject& aEvent, ::sal_Int32 nOldState, ::sal_Int32 nNewState ) throw (::com::sun::star::embed::WrongStateException, ::com::sun::star::uno::RuntimeException);
147     virtual void SAL_CALL stateChanged( const ::com::sun::star::lang::EventObject& aEvent, ::sal_Int32 nOldState, ::sal_Int32 nNewState ) throw (::com::sun::star::uno::RuntimeException);
148     virtual void SAL_CALL disposing( const ::com::sun::star::lang::EventObject& aEvent ) throw (::com::sun::star::uno::RuntimeException);
149 
150     // document::XEventListener
151     virtual void SAL_CALL       notifyEvent( const document::EventObject& aEvent ) throw( uno::RuntimeException );
152 
153 	// XEmbeddedClient
154     virtual void SAL_CALL saveObject() throw ( embed::ObjectSaveVetoException, uno::Exception, uno::RuntimeException );
155     virtual void SAL_CALL visibilityChanged( sal_Bool bVisible ) throw ( embed::WrongStateException, uno::RuntimeException );
156 
157 	// XComponentSupplier
158     virtual uno::Reference< util::XCloseable > SAL_CALL getComponent() throw ( uno::RuntimeException );
159 
160     // XInplaceClient
161     virtual sal_Bool SAL_CALL canInplaceActivate() throw ( uno::RuntimeException );
162     virtual void SAL_CALL activatingInplace() throw ( embed::WrongStateException, uno::RuntimeException );
163     virtual void SAL_CALL activatingUI() throw ( embed::WrongStateException, uno::RuntimeException );
164     virtual void SAL_CALL deactivatedInplace() throw ( embed::WrongStateException, uno::RuntimeException );
165     virtual void SAL_CALL deactivatedUI() throw ( embed::WrongStateException, uno::RuntimeException );
166     virtual uno::Reference< ::com::sun::star::frame::XLayoutManager > SAL_CALL getLayoutManager() throw ( embed::WrongStateException, uno::RuntimeException );
167     virtual uno::Reference< frame::XDispatchProvider > SAL_CALL getInplaceDispatchProvider() throw ( embed::WrongStateException, uno::RuntimeException );
168     virtual awt::Rectangle SAL_CALL getPlacement() throw ( embed::WrongStateException, uno::RuntimeException );
169     virtual awt::Rectangle SAL_CALL getClipRectangle() throw ( embed::WrongStateException, uno::RuntimeException );
170     virtual void SAL_CALL translateAccelerators( const uno::Sequence< awt::KeyEvent >& aKeys ) throw ( embed::WrongStateException, uno::RuntimeException );
171     virtual void SAL_CALL scrollObject( const awt::Size& aOffset ) throw ( embed::WrongStateException, uno::RuntimeException );
172     virtual void SAL_CALL changedPlacement( const awt::Rectangle& aPosRect ) throw ( embed::WrongStateException, uno::Exception, uno::RuntimeException );
173 
174     // XWindowSupplier
175     virtual uno::Reference< awt::XWindow > SAL_CALL getWindow() throw ( uno::RuntimeException );
176 };
177 
178 //--------------------------------------------------------------------
179 SdrLightEmbeddedClient_Impl::SdrLightEmbeddedClient_Impl( SdrOle2Obj* pObj )
180 : mpObj( pObj )
181 {
182 }
183 Rectangle SdrLightEmbeddedClient_Impl::impl_getScaledRect_nothrow() const
184 {
185     MapUnit aContainerMapUnit( MAP_100TH_MM );
186     uno::Reference< embed::XVisualObject > xParentVis( mpObj->GetParentXModel(), uno::UNO_QUERY );
187     if ( xParentVis.is() )
188 	    aContainerMapUnit = VCLUnoHelper::UnoEmbed2VCLMapUnit( xParentVis->getMapUnit( mpObj->GetAspect() ) );
189     Rectangle aLogicRect( mpObj->GetLogicRect() );
190     // apply scaling to object area and convert to pixels
191     aLogicRect.SetSize( Size( Fraction( aLogicRect.GetWidth() ) * m_aScaleWidth,
192                     			Fraction( aLogicRect.GetHeight() ) * m_aScaleHeight ) );
193     return aLogicRect;
194 }
195 //--------------------------------------------------------------------
196 void SAL_CALL SdrLightEmbeddedClient_Impl::changingState( const ::com::sun::star::lang::EventObject& /*aEvent*/, ::sal_Int32 /*nOldState*/, ::sal_Int32 /*nNewState*/ ) throw (::com::sun::star::embed::WrongStateException, ::com::sun::star::uno::RuntimeException)
197 {
198 }
199 
200 //--------------------------------------------------------------------
201 void SdrLightEmbeddedClient_Impl::Release()
202 {
203 	{
204 		::vos::OGuard aGuard( Application::GetSolarMutex() );
205     	mpObj = NULL;
206 	}
207 
208     release();
209 }
210 
211 //--------------------------------------------------------------------
212 void SAL_CALL SdrLightEmbeddedClient_Impl::stateChanged( const ::com::sun::star::lang::EventObject& /*aEvent*/, ::sal_Int32 nOldState, ::sal_Int32 nNewState ) throw (::com::sun::star::uno::RuntimeException)
213 {
214 	::vos::OGuard aGuard( Application::GetSolarMutex() );
215 
216     if ( mpObj && nOldState == embed::EmbedStates::LOADED && nNewState == embed::EmbedStates::RUNNING )
217     {
218         mpObj->ObjectLoaded();
219         GetSdrGlobalData().GetOLEObjCache().InsertObj(mpObj);
220     }
221     else if ( mpObj && nNewState == embed::EmbedStates::LOADED && nOldState == embed::EmbedStates::RUNNING )
222     {
223         GetSdrGlobalData().GetOLEObjCache().RemoveObj(mpObj);
224     }
225 }
226 
227 //--------------------------------------------------------------------
228 void SAL_CALL SdrLightEmbeddedClient_Impl::disposing( const ::com::sun::star::lang::EventObject& /*aEvent*/ ) throw (::com::sun::star::uno::RuntimeException)
229 {
230 	::vos::OGuard aGuard( Application::GetSolarMutex() );
231 
232     GetSdrGlobalData().GetOLEObjCache().RemoveObj(mpObj);
233 }
234 
235 //--------------------------------------------------------------------
236 void SAL_CALL SdrLightEmbeddedClient_Impl::notifyEvent( const document::EventObject& aEvent ) throw( uno::RuntimeException )
237 {
238 	// TODO/LATER: when writer uses this implementation the code could be shared with SfxInPlaceClient_Impl
239 
240 	::vos::OGuard aGuard( Application::GetSolarMutex() );
241 
242 	// the code currently makes sence only in case there is no other client
243 	if ( mpObj && mpObj->GetAspect() != embed::Aspects::MSOLE_ICON && aEvent.EventName.equalsAscii("OnVisAreaChanged")
244 	  && mpObj->GetObjRef().is() && mpObj->GetObjRef()->getClientSite() == uno::Reference< embed::XEmbeddedClient >( this ) )
245 	{
246 		try
247 		{
248             MapUnit aContainerMapUnit( MAP_100TH_MM );
249 		    uno::Reference< embed::XVisualObject > xParentVis( mpObj->GetParentXModel(), uno::UNO_QUERY );
250 		    if ( xParentVis.is() )
251 			    aContainerMapUnit = VCLUnoHelper::UnoEmbed2VCLMapUnit( xParentVis->getMapUnit( mpObj->GetAspect() ) );
252 
253 		    MapUnit aObjMapUnit = VCLUnoHelper::UnoEmbed2VCLMapUnit( mpObj->GetObjRef()->getMapUnit( mpObj->GetAspect() ) );
254 
255 		    Rectangle		   aVisArea;
256 		    awt::Size aSz;
257 		    try
258 		    {
259 			    aSz = mpObj->GetObjRef()->getVisualAreaSize( mpObj->GetAspect() );
260 		    }
261 		    catch( embed::NoVisualAreaSizeException& )
262 		    {
263 			    OSL_ENSURE( sal_False, "No visual area size!\n" );
264 			    aSz.Width = 5000;
265 			    aSz.Height = 5000;
266 		    }
267 		    catch( uno::Exception& )
268 		    {
269 			    OSL_ENSURE( sal_False, "Unexpected exception!\n" );
270 			    aSz.Width = 5000;
271 			    aSz.Height = 5000;
272 		    }
273 
274 		    aVisArea.SetSize( Size( aSz.Width, aSz.Height ) );
275 		    aVisArea = OutputDevice::LogicToLogic( aVisArea, aObjMapUnit, aContainerMapUnit );
276 		    Size aScaledSize( static_cast< long >( m_aScaleWidth * Fraction( aVisArea.GetWidth() ) ),
277 							    static_cast< long >( m_aScaleHeight * Fraction( aVisArea.GetHeight() ) ) );
278             Rectangle aLogicRect( mpObj->GetLogicRect() );
279 
280 			// react to the change if the difference is bigger than one pixel
281 			Size aPixelDiff =
282 				Application::GetDefaultDevice()->LogicToPixel(
283 					Size( aLogicRect.GetWidth() - aScaledSize.Width(),
284 						  aLogicRect.GetHeight() - aScaledSize.Height() ),
285 					aContainerMapUnit );
286 			if( aPixelDiff.Width() || aPixelDiff.Height() )
287 			{
288 				mpObj->SetLogicRect( Rectangle( aLogicRect.TopLeft(), aScaledSize ) );
289 				mpObj->BroadcastObjectChange();
290 			}
291 			else
292 				mpObj->ActionChanged();
293 		}
294 		catch( uno::Exception& )
295 		{
296 			OSL_ENSURE( sal_False, "Unexpected exception!\n" );
297 		}
298 	}
299 }
300 
301 //--------------------------------------------------------------------
302 void SAL_CALL SdrLightEmbeddedClient_Impl::saveObject()
303 	throw ( embed::ObjectSaveVetoException,
304 			uno::Exception,
305 			uno::RuntimeException )
306 {
307 	// TODO/LATER: when writer uses this implementation the code could be shared with SfxInPlaceClient_Impl
308 	uno::Reference< embed::XCommonEmbedPersist > xPersist;
309 	uno::Reference< util::XModifiable > xModifiable;
310 
311 	{
312 		::vos::OGuard aGuard( Application::GetSolarMutex() );
313 
314 		if ( !mpObj )
315 			throw embed::ObjectSaveVetoException();
316 
317 		// the common persistance is supported by objects and links
318 		xPersist = uno::Reference< embed::XCommonEmbedPersist >( mpObj->GetObjRef(), uno::UNO_QUERY_THROW );
319 		xModifiable = uno::Reference< util::XModifiable >( mpObj->GetParentXModel(), uno::UNO_QUERY );
320 	}
321 
322 	xPersist->storeOwn();
323 
324 	if ( xModifiable.is() )
325 		xModifiable->setModified( sal_True );
326 }
327 
328 //--------------------------------------------------------------------
329 void SAL_CALL SdrLightEmbeddedClient_Impl::visibilityChanged( sal_Bool /*bVisible*/ )
330 	throw ( embed::WrongStateException,
331 			uno::RuntimeException )
332 {
333 	// nothing to do currently
334 	// TODO/LATER: when writer uses this implementation the code could be shared with SfxInPlaceClient_Impl
335     if ( mpObj )
336     {
337         Rectangle aLogicRect( mpObj->GetLogicRect() );
338         Size aLogicSize( aLogicRect.GetWidth(), aLogicRect.GetHeight() );
339 
340         if( mpObj->IsChart() )
341         {
342             //charts never should be stretched see #i84323# for example
343             mpObj->SetLogicRect( Rectangle( aLogicRect.TopLeft(), aLogicSize ) );
344             mpObj->BroadcastObjectChange();
345         } // if( mpObj->IsChart() )
346     }
347 }
348 
349 //--------------------------------------------------------------------
350 uno::Reference< util::XCloseable > SAL_CALL SdrLightEmbeddedClient_Impl::getComponent()
351 	throw ( uno::RuntimeException )
352 {
353 	uno::Reference< util::XCloseable > xResult;
354 
355 	::vos::OGuard aGuard( Application::GetSolarMutex() );
356 	if ( mpObj )
357 		xResult = uno::Reference< util::XCloseable >( mpObj->GetParentXModel(), uno::UNO_QUERY );
358 
359 	return xResult;
360 }
361 // XInplaceClient
362 //--------------------------------------------------------------------
363 sal_Bool SAL_CALL SdrLightEmbeddedClient_Impl::canInplaceActivate()
364 	throw ( uno::RuntimeException )
365 {
366     sal_Bool bRet = sal_False;
367     ::vos::OGuard aGuard( Application::GetSolarMutex() );
368     if ( mpObj )
369     {
370         uno::Reference< embed::XEmbeddedObject > xObject = mpObj->GetObjRef();
371         if ( !xObject.is() )
372 		    throw uno::RuntimeException();
373         // we don't want to switch directly from outplace to inplace mode
374         bRet = !( xObject->getCurrentState() == embed::EmbedStates::ACTIVE || mpObj->GetAspect() == embed::Aspects::MSOLE_ICON );
375     } // if ( mpObj )
376     return bRet;
377 }
378 
379 //--------------------------------------------------------------------
380 void SAL_CALL SdrLightEmbeddedClient_Impl::activatingInplace()
381 	throw ( embed::WrongStateException,
382 			uno::RuntimeException )
383 {
384 }
385 
386 //--------------------------------------------------------------------
387 void SAL_CALL SdrLightEmbeddedClient_Impl::activatingUI()
388 	throw ( embed::WrongStateException,
389 			uno::RuntimeException )
390 {
391     ::vos::OGuard aGuard( Application::GetSolarMutex() );
392 
393     uno::Reference < beans::XPropertySet > xFrame( lcl_getFrame_throw(mpObj));
394     uno::Reference < frame::XFrame > xOwnFrame( xFrame,uno::UNO_QUERY);
395     uno::Reference < frame::XFramesSupplier > xParentFrame( xOwnFrame->getCreator(), uno::UNO_QUERY );
396     if ( xParentFrame.is() )
397         xParentFrame->setActiveFrame( xOwnFrame );
398 
399     OLEObjCache& rObjCache = GetSdrGlobalData().GetOLEObjCache();
400     const sal_uIntPtr nCount = rObjCache.Count();
401     for(sal_Int32 i = nCount-1 ; i >= 0;--i)
402     {
403         SdrOle2Obj* pObj = reinterpret_cast<SdrOle2Obj*>(rObjCache.GetObject(i));
404         if ( pObj != mpObj )
405         {
406             // only deactivate ole objects which belongs to the same frame
407             if ( xFrame == lcl_getFrame_throw(pObj) )
408             {
409                 uno::Reference< embed::XEmbeddedObject > xObject = pObj->GetObjRef();
410                 try
411                 {
412                     if ( xObject->getStatus( pObj->GetAspect() ) & embed::EmbedMisc::MS_EMBED_ACTIVATEWHENVISIBLE )
413                         xObject->changeState( embed::EmbedStates::INPLACE_ACTIVE );
414                     else
415                     {
416                         // the links should not stay in running state for long time because of locking
417                         uno::Reference< embed::XLinkageSupport > xLink( xObject, uno::UNO_QUERY );
418                         if ( xLink.is() && xLink->isLink() )
419                             xObject->changeState( embed::EmbedStates::LOADED );
420                         else
421                             xObject->changeState( embed::EmbedStates::RUNNING );
422                     }
423                 }
424                 catch (com::sun::star::uno::Exception& )
425                 {}
426             }
427         }
428     } // for(sal_Int32 i = nCount-1 ; i >= 0;--i)
429 
430     //m_pClient->GetViewShell()->UIActivating( m_pClient );
431 }
432 
433 //--------------------------------------------------------------------
434 void SAL_CALL SdrLightEmbeddedClient_Impl::deactivatedInplace()
435 	throw ( embed::WrongStateException,
436 			uno::RuntimeException )
437 {
438 }
439 
440 //--------------------------------------------------------------------
441 void SAL_CALL SdrLightEmbeddedClient_Impl::deactivatedUI()
442 	throw ( embed::WrongStateException,
443 			uno::RuntimeException )
444 {
445     ::vos::OGuard aGuard( Application::GetSolarMutex() );
446     com::sun::star::uno::Reference< ::com::sun::star::frame::XLayoutManager > xLayoutManager(getLayoutManager());
447     if ( xLayoutManager.is() )
448     {
449         const static rtl::OUString aMenuBarURL( RTL_CONSTASCII_USTRINGPARAM( "private:resource/menubar/menubar" ));
450 		if ( !xLayoutManager->isElementVisible( aMenuBarURL ) )
451         	xLayoutManager->createElement( aMenuBarURL );
452     }
453 }
454 
455 //--------------------------------------------------------------------
456 uno::Reference< ::com::sun::star::frame::XLayoutManager > SAL_CALL SdrLightEmbeddedClient_Impl::getLayoutManager()
457 	throw ( embed::WrongStateException,
458 			uno::RuntimeException )
459 {
460     uno::Reference< ::com::sun::star::frame::XLayoutManager > xMan;
461     ::vos::OGuard aGuard( Application::GetSolarMutex() );
462     uno::Reference < beans::XPropertySet > xFrame( lcl_getFrame_throw(mpObj));
463     try
464     {
465         xMan.set(xFrame->getPropertyValue( ::rtl::OUString::createFromAscii("LayoutManager") ),uno::UNO_QUERY);
466     }
467     catch ( uno::Exception& )
468     {
469         throw uno::RuntimeException();
470     }
471 
472     return xMan;
473 }
474 
475 //--------------------------------------------------------------------
476 uno::Reference< frame::XDispatchProvider > SAL_CALL SdrLightEmbeddedClient_Impl::getInplaceDispatchProvider()
477 	throw ( embed::WrongStateException,
478 			uno::RuntimeException )
479 {
480     ::vos::OGuard aGuard( Application::GetSolarMutex() );
481 	return uno::Reference < frame::XDispatchProvider >( lcl_getFrame_throw(mpObj), uno::UNO_QUERY_THROW );
482 }
483 
484 //--------------------------------------------------------------------
485 awt::Rectangle SAL_CALL SdrLightEmbeddedClient_Impl::getPlacement()
486 	throw ( embed::WrongStateException,
487 			uno::RuntimeException )
488 {
489     ::vos::OGuard aGuard( Application::GetSolarMutex() );
490     if ( !mpObj )
491         throw uno::RuntimeException();
492 
493     Rectangle aLogicRect = impl_getScaledRect_nothrow();
494     MapUnit aContainerMapUnit( MAP_100TH_MM );
495     uno::Reference< embed::XVisualObject > xParentVis( mpObj->GetParentXModel(), uno::UNO_QUERY );
496     if ( xParentVis.is() )
497 	    aContainerMapUnit = VCLUnoHelper::UnoEmbed2VCLMapUnit( xParentVis->getMapUnit( mpObj->GetAspect() ) );
498 
499     aLogicRect = Application::GetDefaultDevice()->LogicToPixel(aLogicRect,aContainerMapUnit);
500 	return AWTRectangle( aLogicRect );
501 }
502 
503 //--------------------------------------------------------------------
504 awt::Rectangle SAL_CALL SdrLightEmbeddedClient_Impl::getClipRectangle()
505 	throw ( embed::WrongStateException,
506 			uno::RuntimeException )
507 {
508 	return getPlacement();
509 }
510 
511 //--------------------------------------------------------------------
512 void SAL_CALL SdrLightEmbeddedClient_Impl::translateAccelerators( const uno::Sequence< awt::KeyEvent >& /*aKeys*/ )
513 	throw ( embed::WrongStateException,
514 			uno::RuntimeException )
515 {
516 }
517 
518 //--------------------------------------------------------------------
519 void SAL_CALL SdrLightEmbeddedClient_Impl::scrollObject( const awt::Size& /*aOffset*/ )
520 	throw ( embed::WrongStateException,
521 			uno::RuntimeException )
522 {
523 }
524 
525 //--------------------------------------------------------------------
526 void SAL_CALL SdrLightEmbeddedClient_Impl::changedPlacement( const awt::Rectangle& aPosRect )
527 	throw ( embed::WrongStateException,
528 			uno::Exception,
529 			uno::RuntimeException )
530 {
531     ::vos::OGuard aGuard( Application::GetSolarMutex() );
532     if ( !mpObj )
533         throw uno::RuntimeException();
534 
535 	uno::Reference< embed::XInplaceObject > xInplace( mpObj->GetObjRef(), uno::UNO_QUERY );
536     if ( !xInplace.is() )
537 		throw uno::RuntimeException();
538 
539     // check if the change is at least one pixel in size
540     awt::Rectangle aOldRect = getPlacement();
541     Rectangle aNewPixelRect = VCLRectangle( aPosRect );
542     Rectangle aOldPixelRect = VCLRectangle( aOldRect );
543     if ( aOldPixelRect == aNewPixelRect )
544         // nothing has changed
545         return;
546 
547     // new scaled object area
548     MapUnit aContainerMapUnit( MAP_100TH_MM );
549     uno::Reference< embed::XVisualObject > xParentVis( mpObj->GetParentXModel(), uno::UNO_QUERY );
550     if ( xParentVis.is() )
551 	    aContainerMapUnit = VCLUnoHelper::UnoEmbed2VCLMapUnit( xParentVis->getMapUnit( mpObj->GetAspect() ) );
552 
553     Rectangle aNewLogicRect = Application::GetDefaultDevice()->PixelToLogic(aNewPixelRect,aContainerMapUnit);
554     Rectangle aLogicRect = impl_getScaledRect_nothrow();
555 
556     if ( aNewLogicRect != aLogicRect )
557 	{
558 		// the calculation of the object area has not changed the object size
559 		// it should be done here then
560 		//SfxBooleanFlagGuard aGuard( m_bResizeNoScale, sal_True );
561 
562     	// new size of the object area without scaling
563     	Size aNewObjSize( Fraction( aNewLogicRect.GetWidth() ) / m_aScaleWidth,
564                           Fraction( aNewLogicRect.GetHeight() ) / m_aScaleHeight );
565 
566     	// now remove scaling from new placement and keep this a the new object area
567     	aNewLogicRect.SetSize( aNewObjSize );
568         // react to the change if the difference is bigger than one pixel
569 		Size aPixelDiff =
570 			Application::GetDefaultDevice()->LogicToPixel(
571 				Size( aLogicRect.GetWidth() - aNewObjSize.Width(),
572 					  aLogicRect.GetHeight() - aNewObjSize.Height() ),
573 				aContainerMapUnit );
574 		if( aPixelDiff.Width() || aPixelDiff.Height() )
575 		{
576 			mpObj->SetLogicRect( Rectangle( aLogicRect.TopLeft(), aNewObjSize ) );
577 			mpObj->BroadcastObjectChange();
578 		}
579 		else
580 			mpObj->ActionChanged();
581 
582 		// let the window size be recalculated
583 		//SizeHasChanged(); // TODO: OJ
584 	}
585 }
586 // XWindowSupplier
587 //--------------------------------------------------------------------
588 uno::Reference< awt::XWindow > SAL_CALL SdrLightEmbeddedClient_Impl::getWindow()
589 	throw ( uno::RuntimeException )
590 {
591 	::vos::OGuard aGuard( Application::GetSolarMutex() );
592     uno::Reference< awt::XWindow > xCurrent = m_xWindow;
593     if ( !xCurrent.is() )
594     {
595         if ( !mpObj )
596             throw uno::RuntimeException();
597         uno::Reference< frame::XFrame> xFrame(lcl_getFrame_throw(mpObj),uno::UNO_QUERY_THROW);
598         xCurrent = xFrame->getComponentWindow();
599     } // if ( !xCurrent.is() )
600     return xCurrent;
601 }
602 void SdrLightEmbeddedClient_Impl::setWindow(const uno::Reference< awt::XWindow >& _xWindow)
603 {
604     m_xWindow = _xWindow;
605 }
606 
607 ////////////////////////////////////////////////////////////////////////////////////////////////////
608 
609 class SdrEmbedObjectLink : public sfx2::SvBaseLink
610 {
611 	SdrOle2Obj*			pObj;
612 
613 public:
614 						SdrEmbedObjectLink(SdrOle2Obj* pObj);
615 	virtual				~SdrEmbedObjectLink();
616 
617 	virtual void		Closed();
618 	virtual void		DataChanged( const String& rMimeType,
619 								const ::com::sun::star::uno::Any & rValue );
620 
621 	sal_Bool			Connect() { return GetRealObject() != NULL; }
622 };
623 
624 // -----------------------------------------------------------------------------
625 
626 SdrEmbedObjectLink::SdrEmbedObjectLink(SdrOle2Obj* pObject):
627 	::sfx2::SvBaseLink( ::sfx2::LINKUPDATE_ONCALL, SOT_FORMATSTR_ID_SVXB ),
628 	pObj(pObject)
629 {
630 	SetSynchron( sal_False );
631 }
632 
633 // -----------------------------------------------------------------------------
634 
635 SdrEmbedObjectLink::~SdrEmbedObjectLink()
636 {
637 }
638 
639 // -----------------------------------------------------------------------------
640 
641 void SdrEmbedObjectLink::DataChanged( const String& /*rMimeType*/,
642 								const ::com::sun::star::uno::Any & /*rValue*/ )
643 {
644 	if ( !pObj->UpdateLinkURL_Impl() )
645 	{
646 		// the link URL was not changed
647 		uno::Reference< embed::XEmbeddedObject > xObject = pObj->GetObjRef();
648 		OSL_ENSURE( xObject.is(), "The object must exist always!\n" );
649 		if ( xObject.is() )
650 		{
651 			// let the object reload the link
652 			// TODO/LATER: reload call could be used for this case
653 
654 			try
655 			{
656 				sal_Int32 nState = xObject->getCurrentState();
657 				if ( nState != embed::EmbedStates::LOADED )
658 				{
659 					// in some cases the linked file probably is not locked so it could be changed
660 					xObject->changeState( embed::EmbedStates::LOADED );
661 					xObject->changeState( nState );
662 				}
663 			}
664 			catch ( uno::Exception& )
665 			{
666 			}
667 		}
668 	}
669 
670 	pObj->GetNewReplacement();
671 	pObj->SetChanged();
672 }
673 
674 // -----------------------------------------------------------------------------
675 
676 void SdrEmbedObjectLink::Closed()
677 {
678 	pObj->BreakFileLink_Impl();
679 	SvBaseLink::Closed();
680 }
681 
682 ////////////////////////////////////////////////////////////////////////////////////////////////////
683 
684 class SdrOle2ObjImpl
685 {
686 public:
687     // TODO/LATER: do we really need this pointer?
688 	GraphicObject*	pGraphicObject;
689 	String          aPersistName;       // name of object in persist
690     SdrLightEmbeddedClient_Impl* pLightClient; // must be registered as client only using AddOwnLightClient() call
691 
692 	// #107645#
693 	// New local var to avoid repeated loading if load of OLE2 fails
694 	sal_Bool		mbLoadingOLEObjectFailed;
695     sal_Bool        mbConnected;
696 
697 	SdrEmbedObjectLink*	mpObjectLink;
698 	String maLinkURL;
699 
700 	SdrOle2ObjImpl()
701 	: pGraphicObject( NULL )
702 	// #107645#
703 	// init to start situation, loading did not fail
704 	, mbLoadingOLEObjectFailed( sal_False )
705 	, mbConnected( sal_False )
706 	, mpObjectLink( NULL )
707 	{
708 	}
709 };
710 
711 ////////////////////////////////////////////////////////////////////////////////////////////////////
712 
713 // Predicate determining whether the given OLE is an internal math
714 // object
715 static bool ImplIsMathObj( const uno::Reference < embed::XEmbeddedObject >& rObjRef )
716 {
717     if ( !rObjRef.is() )
718         return false;
719 
720     SvGlobalName aClassName( rObjRef->getClassID() );
721     if( aClassName == SvGlobalName(SO3_SM_CLASSID_30) ||
722         aClassName == SvGlobalName(SO3_SM_CLASSID_40) ||
723         aClassName == SvGlobalName(SO3_SM_CLASSID_50) ||
724         aClassName == SvGlobalName(SO3_SM_CLASSID_60) ||
725         aClassName == SvGlobalName(SO3_SM_CLASSID) 		)
726     {
727         return true;
728     }
729     else
730     {
731         return false;
732     }
733 }
734 
735 //////////////////////////////////////////////////////////////////////////////
736 // BaseProperties section
737 
738 sdr::properties::BaseProperties* SdrOle2Obj::CreateObjectSpecificProperties()
739 {
740 	return new sdr::properties::OleProperties(*this);
741 }
742 
743 //////////////////////////////////////////////////////////////////////////////
744 // DrawContact section
745 
746 sdr::contact::ViewContact* SdrOle2Obj::CreateObjectSpecificViewContact()
747 {
748 	return new sdr::contact::ViewContactOfSdrOle2Obj(*this);
749 }
750 
751 // -----------------------------------------------------------------------------
752 
753 TYPEINIT1(SdrOle2Obj,SdrRectObj);
754 DBG_NAME(SdrOle2Obj)
755 SdrOle2Obj::SdrOle2Obj(FASTBOOL bFrame_) : m_bTypeAsked(false)
756 ,m_bChart(false)
757 {
758     DBG_CTOR( SdrOle2Obj,NULL);
759 	bInDestruction = sal_False;
760 	Init();
761 	bFrame=bFrame_;
762 }
763 
764 // -----------------------------------------------------------------------------
765 SdrOle2Obj::SdrOle2Obj( const svt::EmbeddedObjectRef& rNewObjRef, FASTBOOL bFrame_)
766     : xObjRef( rNewObjRef )
767     , m_bTypeAsked(false)
768     , m_bChart(false)
769 {
770     DBG_CTOR( SdrOle2Obj,NULL);
771 	bInDestruction = sal_False;
772 	Init();
773 
774 	bFrame=bFrame_;
775 
776     if ( xObjRef.is() && (xObjRef->getStatus( GetAspect() ) & embed::EmbedMisc::EMBED_NEVERRESIZE ) )
777         SetResizeProtect(sal_True);
778 
779     // #108759# For math objects, set closed state to transparent
780     if( ImplIsMathObj( xObjRef.GetObject() ) )
781         SetClosedObj( false );
782 }
783 
784 // -----------------------------------------------------------------------------
785 
786 SdrOle2Obj::SdrOle2Obj( const svt::EmbeddedObjectRef& rNewObjRef, const XubString& rNewObjName, FASTBOOL bFrame_)
787     : xObjRef( rNewObjRef )
788     , m_bTypeAsked(false)
789     , m_bChart(false)
790 {
791     DBG_CTOR( SdrOle2Obj,NULL);
792 	bInDestruction = sal_False;
793 	Init();
794 
795 	mpImpl->aPersistName = rNewObjName;
796 	bFrame=bFrame_;
797 
798     if ( xObjRef.is() && (xObjRef->getStatus( GetAspect() ) & embed::EmbedMisc::EMBED_NEVERRESIZE ) )
799         SetResizeProtect(sal_True);
800 
801     // #108759# For math objects, set closed state to transparent
802     if( ImplIsMathObj( xObjRef.GetObject() ) )
803         SetClosedObj( false );
804 }
805 
806 // -----------------------------------------------------------------------------
807 
808 SdrOle2Obj::SdrOle2Obj( const svt::EmbeddedObjectRef&  rNewObjRef, const XubString& rNewObjName, const Rectangle& rNewRect, FASTBOOL bFrame_)
809     : SdrRectObj(rNewRect)
810     , xObjRef( rNewObjRef )
811     , m_bTypeAsked(false)
812     , m_bChart(false)
813 {
814     DBG_CTOR( SdrOle2Obj,NULL);
815 	bInDestruction = sal_False;
816 	Init();
817 
818 	mpImpl->aPersistName = rNewObjName;
819 	bFrame=bFrame_;
820 
821     if ( xObjRef.is() && (xObjRef->getStatus( GetAspect() ) & embed::EmbedMisc::EMBED_NEVERRESIZE ) )
822         SetResizeProtect(sal_True);
823 
824     // #108759# For math objects, set closed state to transparent
825     if( ImplIsMathObj( xObjRef.GetObject() ) )
826         SetClosedObj( false );
827 }
828 
829 // -----------------------------------------------------------------------------
830 
831 void SdrOle2Obj::Init()
832 {
833 	mpImpl = new SdrOle2ObjImpl;
834 	pModifyListener = NULL;
835 	pGraphic=NULL;
836 	mpImpl->pGraphicObject=NULL;
837     mpImpl->pLightClient = 0;
838 
839     xObjRef.Lock( sal_True );
840 }
841 
842 // -----------------------------------------------------------------------------
843 
844 SdrOle2Obj::~SdrOle2Obj()
845 {
846     DBG_DTOR( SdrOle2Obj,NULL);
847 	bInDestruction = sal_True;
848 
849     if ( mpImpl->mbConnected )
850         Disconnect();
851 
852     if( pGraphic!=NULL )
853 		delete pGraphic;
854 
855 	if(mpImpl->pGraphicObject!=NULL)
856 		delete mpImpl->pGraphicObject;
857 
858 	if(pModifyListener)
859 	{
860 		pModifyListener->invalidate();
861 		pModifyListener->release();
862 	}
863 
864 	DisconnectFileLink_Impl();
865 
866     if ( mpImpl->pLightClient )
867 	{
868         mpImpl->pLightClient->Release();
869 		mpImpl->pLightClient = NULL;
870 	}
871 
872 	delete mpImpl;
873 }
874 
875 // -----------------------------------------------------------------------------
876 void SdrOle2Obj::SetAspect( sal_Int64 nAspect )
877 {
878 	xObjRef.SetViewAspect( nAspect );
879 }
880 
881 // -----------------------------------------------------------------------------
882 
883 void SdrOle2Obj::SetGraphic_Impl(const Graphic* pGrf)
884 {
885 	if ( pGraphic )
886 	{
887 		delete pGraphic;
888 		pGraphic = NULL;
889 		delete mpImpl->pGraphicObject;
890 		mpImpl->pGraphicObject = NULL;
891 	}
892 
893 	if (pGrf!=NULL)
894 	{
895 		pGraphic = new Graphic(*pGrf);
896 		mpImpl->pGraphicObject = new GraphicObject( *pGraphic );
897 	}
898 
899 	SetChanged();
900 	BroadcastObjectChange();
901 
902 	//if ( ppObjRef->Is() && pGrf )
903 	//	BroadcastObjectChange();
904 }
905 
906 void SdrOle2Obj::SetGraphic(const Graphic* pGrf)
907 {
908     // only for setting a preview graphic
909     SetGraphic_Impl( pGrf );
910 }
911 
912 // -----------------------------------------------------------------------------
913 
914 FASTBOOL SdrOle2Obj::IsEmpty() const
915 {
916     return !(xObjRef.is());
917 }
918 
919 // -----------------------------------------------------------------------------
920 
921 void SdrOle2Obj::Connect()
922 {
923 	if( IsEmptyPresObj() )
924 		return;
925 
926     if( mpImpl->mbConnected )
927     {
928         // mba: currently there are situations where it seems to be unavoidable to have multiple connects
929         // changing this would need a larger code rewrite, so for now I remove the assertion
930         // DBG_ERROR("Connect() called on connected object!");
931 		return;
932     }
933 
934     Connect_Impl();
935     AddListeners_Impl();
936 }
937 
938 // -----------------------------------------------------------------------------
939 
940 sal_Bool SdrOle2Obj::UpdateLinkURL_Impl()
941 {
942 	sal_Bool bResult = sal_False;
943 
944 	if ( mpImpl->mpObjectLink )
945 	{
946 		sfx2::LinkManager* pLinkManager = pModel ? pModel->GetLinkManager() : NULL;
947 		if ( pLinkManager )
948 		{
949 			String aNewLinkURL;
950 			pLinkManager->GetDisplayNames( mpImpl->mpObjectLink, 0, &aNewLinkURL, 0, 0 );
951 			if ( !aNewLinkURL.EqualsIgnoreCaseAscii( mpImpl->maLinkURL ) )
952 			{
953     			const_cast<SdrOle2Obj*>(this)->GetObjRef_Impl();
954 				uno::Reference< embed::XCommonEmbedPersist > xPersObj( xObjRef.GetObject(), uno::UNO_QUERY );
955 				OSL_ENSURE( xPersObj.is(), "The object must exist!\n" );
956     			if ( xPersObj.is() )
957 				{
958 					try
959 					{
960 						sal_Int32 nCurState = xObjRef->getCurrentState();
961 						if ( nCurState != embed::EmbedStates::LOADED )
962 							xObjRef->changeState( embed::EmbedStates::LOADED );
963 
964 						// TODO/LATER: there should be possible to get current mediadescriptor settings from the object
965 						uno::Sequence< beans::PropertyValue > aArgs( 1 );
966 						aArgs[0].Name = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "URL" ) );
967 						aArgs[0].Value <<= ::rtl::OUString( aNewLinkURL );
968 						xPersObj->reload( aArgs, uno::Sequence< beans::PropertyValue >() );
969 
970 						mpImpl->maLinkURL = aNewLinkURL;
971 						bResult = sal_True;
972 
973 						if ( nCurState != embed::EmbedStates::LOADED )
974 							xObjRef->changeState( nCurState );
975 					}
976 					catch( ::com::sun::star::uno::Exception& e )
977 					{
978 						(void)e;
979 						DBG_ERROR(
980 							(OString("SdrOle2Obj::UpdateLinkURL_Impl(), "
981 									"exception caught: ") +
982 							rtl::OUStringToOString(
983 								comphelper::anyToString( cppu::getCaughtException() ),
984 								RTL_TEXTENCODING_UTF8 )).getStr() );
985 					}
986 				}
987 
988 				if ( !bResult )
989 				{
990 					// TODO/LATER: return the old name to the link manager, is it possible?
991 				}
992 			}
993 		}
994 	}
995 
996 	return bResult;
997 }
998 
999 // -----------------------------------------------------------------------------
1000 
1001 void SdrOle2Obj::BreakFileLink_Impl()
1002 {
1003     uno::Reference<document::XStorageBasedDocument> xDoc;
1004 	if ( pModel )
1005 		xDoc.set( pModel->getUnoModel(),uno::UNO_QUERY);
1006 
1007 	if ( xDoc.is() )
1008 	{
1009 		uno::Reference< embed::XStorage > xStorage = xDoc->getDocumentStorage();
1010 		if ( xStorage.is() )
1011 		{
1012 			try
1013 			{
1014 				uno::Reference< embed::XLinkageSupport > xLinkSupport( xObjRef.GetObject(), uno::UNO_QUERY_THROW );
1015 				xLinkSupport->breakLink( xStorage, mpImpl->aPersistName );
1016 				DisconnectFileLink_Impl();
1017 				mpImpl->maLinkURL = String();
1018 			}
1019 			catch( ::com::sun::star::uno::Exception& e )
1020 			{
1021 				(void)e;
1022 				DBG_ERROR(
1023 					(OString("SdrOle2Obj::BreakFileLink_Impl(), "
1024 							"exception caught: ") +
1025 					rtl::OUStringToOString(
1026 						comphelper::anyToString( cppu::getCaughtException() ),
1027 						RTL_TEXTENCODING_UTF8 )).getStr() );
1028 			}
1029 		}
1030 	}
1031 }
1032 
1033 // -----------------------------------------------------------------------------
1034 
1035 void SdrOle2Obj::DisconnectFileLink_Impl()
1036 {
1037 	sfx2::LinkManager* pLinkManager = pModel ? pModel->GetLinkManager() : NULL;
1038 	if ( pLinkManager && mpImpl->mpObjectLink )
1039 	{
1040 		pLinkManager->Remove( mpImpl->mpObjectLink );
1041 		mpImpl->mpObjectLink = NULL;
1042 	}
1043 }
1044 
1045 // -----------------------------------------------------------------------------
1046 
1047 void SdrOle2Obj::CheckFileLink_Impl()
1048 {
1049 	if ( pModel && xObjRef.GetObject().is() && !mpImpl->mpObjectLink )
1050 	{
1051 		try
1052 		{
1053 			uno::Reference< embed::XLinkageSupport > xLinkSupport( xObjRef.GetObject(), uno::UNO_QUERY );
1054 			if ( xLinkSupport.is() && xLinkSupport->isLink() )
1055 			{
1056 				String aLinkURL = xLinkSupport->getLinkURL();
1057 				if ( aLinkURL.Len() )
1058 				{
1059 					// this is a file link so the model link manager should handle it
1060 					sfx2::LinkManager* pLinkManager = pModel->GetLinkManager();
1061 					if ( pLinkManager )
1062 					{
1063 						mpImpl->mpObjectLink = new SdrEmbedObjectLink( this );
1064 						mpImpl->maLinkURL = aLinkURL;
1065 						pLinkManager->InsertFileLink( *mpImpl->mpObjectLink, OBJECT_CLIENT_OLE, aLinkURL, NULL, NULL );
1066 						mpImpl->mpObjectLink->Connect();
1067 					}
1068 				}
1069 			}
1070 		}
1071 		catch( ::com::sun::star::uno::Exception& e )
1072 		{
1073 			(void)e;
1074 			DBG_ERROR(
1075 				(OString("SdrOle2Obj::CheckFileLink_Impl(), "
1076 						"exception caught: ") +
1077 				rtl::OUStringToOString(
1078 					comphelper::anyToString( cppu::getCaughtException() ),
1079 					RTL_TEXTENCODING_UTF8 )).getStr() );
1080 		}
1081 	}
1082 }
1083 
1084 // -----------------------------------------------------------------------------
1085 
1086 void SdrOle2Obj::Reconnect_Impl()
1087 {
1088     DBG_ASSERT( mpImpl->mbConnected, "Assigned unconnected object?!" );
1089     Connect_Impl();
1090 }
1091 
1092 void SdrOle2Obj::Connect_Impl()
1093 {
1094     if( pModel && mpImpl->aPersistName.Len() )
1095 	{
1096 		try
1097 		{
1098 			::comphelper::IEmbeddedHelper* pPers = pModel->GetPersist();
1099 			if ( pPers )
1100 			{
1101 				comphelper::EmbeddedObjectContainer& rContainer = pPers->getEmbeddedObjectContainer();
1102 				if ( !rContainer.HasEmbeddedObject( mpImpl->aPersistName )
1103 				  || ( xObjRef.is() && !rContainer.HasEmbeddedObject( xObjRef.GetObject() ) ) )
1104 				{
1105 					// object not known to container document
1106 					// No object -> disaster!
1107 					DBG_ASSERT( xObjRef.is(), "No object in connect!");
1108 					if ( xObjRef.is() )
1109 					{
1110 						// object came from the outside, now add it to the container
1111 						::rtl::OUString aTmp;
1112 						rContainer.InsertEmbeddedObject( xObjRef.GetObject(), aTmp );
1113                         mpImpl->aPersistName = aTmp;
1114 					}
1115 				}
1116 				else if ( !xObjRef.is() )
1117                 {
1118 					xObjRef.Assign( rContainer.GetEmbeddedObject( mpImpl->aPersistName ), xObjRef.GetViewAspect() );
1119                     m_bTypeAsked = false;
1120                 }
1121 
1122 				if ( xObjRef.GetObject().is() )
1123 				{
1124 					xObjRef.AssignToContainer( &rContainer, mpImpl->aPersistName );
1125 					mpImpl->mbConnected = true;
1126 					xObjRef.Lock( sal_True );
1127 				}
1128 			}
1129 
1130 			if ( xObjRef.is() )
1131 			{
1132 				if ( !mpImpl->pLightClient )
1133 				{
1134 					mpImpl->pLightClient = new SdrLightEmbeddedClient_Impl( this );
1135 					mpImpl->pLightClient->acquire();
1136 				}
1137 
1138 				xObjRef->addStateChangeListener( mpImpl->pLightClient );
1139 				xObjRef->addEventListener( uno::Reference< document::XEventListener >( mpImpl->pLightClient ) );
1140 
1141 				if ( xObjRef->getCurrentState() != embed::EmbedStates::LOADED )
1142 					GetSdrGlobalData().GetOLEObjCache().InsertObj(this);
1143 
1144 				CheckFileLink_Impl();
1145 
1146 				uno::Reference< container::XChild > xChild( xObjRef.GetObject(), uno::UNO_QUERY );
1147 				if( xChild.is() )
1148                 {
1149                     uno::Reference< uno::XInterface > xParent( pModel->getUnoModel());
1150                     if( xParent.is())
1151                         xChild->setParent( pModel->getUnoModel() );
1152                 }
1153 
1154 			}
1155 		}
1156 		catch( ::com::sun::star::uno::Exception& e )
1157 		{
1158 			(void)e;
1159 			DBG_ERROR(
1160 				(OString("SdrOle2Obj::Connect_Impl(), "
1161 						"exception caught: ") +
1162 				rtl::OUStringToOString(
1163 					comphelper::anyToString( cppu::getCaughtException() ),
1164 					RTL_TEXTENCODING_UTF8 )).getStr() );
1165 		}
1166 	}
1167 
1168     //TODO/LATER: wait for definition of MiscStatus RESIZEONPRINTERCHANGE
1169     //if ( xObjRef.is() && (*ppObjRef)->GetMiscStatus() & SVOBJ_MISCSTATUS_RESIZEONPRINTERCHANGE )
1170 	{
1171         //TODO/LATER: needs a new handling for OnPrinterChanged
1172         /*
1173 		if (pModel && pModel->GetRefDevice() &&
1174 			pModel->GetRefDevice()->GetOutDevType() == OUTDEV_PRINTER)
1175 		{
1176 			// Kein RefDevice oder RefDevice kein Printer
1177 			sal_Bool bModified = (*ppObjRef)->IsModified();
1178 			Printer* pPrinter = (Printer*) pModel->GetRefDevice();
1179 			(*ppObjRef)->OnDocumentPrinterChanged( pPrinter );
1180 			(*ppObjRef)->SetModified( bModified );
1181         }*/
1182 	}
1183 }
1184 
1185 void SdrOle2Obj::ObjectLoaded()
1186 {
1187     AddListeners_Impl();
1188 }
1189 
1190 void SdrOle2Obj::AddListeners_Impl()
1191 {
1192     if( xObjRef.is() && xObjRef->getCurrentState() != embed::EmbedStates::LOADED )
1193 	{
1194 		// register modify listener
1195         if( !pModifyListener )
1196 		{
1197 			((SdrOle2Obj*)this)->pModifyListener = new SvxUnoShapeModifyListener( (SdrOle2Obj*)this );
1198 			pModifyListener->acquire();
1199 		}
1200 
1201 		uno::Reference< util::XModifyBroadcaster > xBC( getXModel(), uno::UNO_QUERY );
1202 		if( xBC.is() && pModifyListener )
1203 		{
1204 			uno::Reference< util::XModifyListener > xListener( pModifyListener );
1205 			xBC->addModifyListener( xListener );
1206 		}
1207 	}
1208 }
1209 
1210 // -----------------------------------------------------------------------------
1211 
1212 void SdrOle2Obj::Disconnect()
1213 {
1214 	if( IsEmptyPresObj() )
1215 		return;
1216 
1217 	if( !mpImpl->mbConnected )
1218     {
1219         DBG_ERROR("Disconnect() called on disconnected object!");
1220 		return;
1221     }
1222 
1223     RemoveListeners_Impl();
1224     Disconnect_Impl();
1225 }
1226 
1227 void SdrOle2Obj::RemoveListeners_Impl()
1228 {
1229     if( xObjRef.is() && mpImpl->aPersistName.Len() )
1230 	{
1231 		try
1232 		{
1233 			sal_Int32 nState = xObjRef->getCurrentState();
1234 			if ( nState != embed::EmbedStates::LOADED )
1235 			{
1236 				uno::Reference< util::XModifyBroadcaster > xBC( getXModel(), uno::UNO_QUERY );
1237 				if( xBC.is() && pModifyListener )
1238 				{
1239 					uno::Reference< util::XModifyListener > xListener( pModifyListener );
1240 					xBC->removeModifyListener( xListener );
1241 				}
1242 			}
1243 		}
1244 		catch( ::com::sun::star::uno::Exception& e )
1245 		{
1246 			(void)e;
1247 			DBG_ERROR(
1248 				(OString("SdrOle2Obj::RemoveListeners_Impl(), "
1249 						"exception caught: ") +
1250 				rtl::OUStringToOString(
1251 					comphelper::anyToString( cppu::getCaughtException() ),
1252 					RTL_TEXTENCODING_UTF8 )).getStr() );
1253 		}
1254 	}
1255 }
1256 
1257 void SdrOle2Obj::Disconnect_Impl()
1258 {
1259 	try
1260 	{
1261 		if ( pModel && mpImpl->aPersistName.Len() )
1262 		{
1263 			if( pModel->IsInDestruction() )
1264 			{
1265 				// TODO/LATER: here we must assume that the destruction of the model is enough to make clear that we will not
1266 				// remove the object from the container, even if the DrawingObject itself is not destroyed (unfortunately this
1267 				// There is no real need to do the following removing of the object from the container
1268 				// in case the model has correct persistance, but in case of problems such a removing
1269 				// would make the behaviour of the office more stable
1270 
1271 				comphelper::EmbeddedObjectContainer* pContainer = xObjRef.GetContainer();
1272 				if ( pContainer )
1273 				{
1274 					pContainer->CloseEmbeddedObject( xObjRef.GetObject() );
1275 					xObjRef.AssignToContainer( NULL, mpImpl->aPersistName );
1276 				}
1277 
1278 				// happens later than the destruction of the model, so we can't assert that).
1279 				//DBG_ASSERT( bInDestruction, "Model is destroyed, but not me?!" );
1280 				//TODO/LATER: should be make sure that the ObjectShell also forgets the object, because we will close it soon?
1281 				/*
1282 				uno::Reference < util::XCloseable > xClose( xObjRef, uno::UNO_QUERY );
1283 				if ( xClose.is() )
1284 				{
1285 					try
1286 					{
1287 						xClose->close( sal_True );
1288 					}
1289 					catch ( util::CloseVetoException& )
1290 					{
1291 						// there's still someone who needs the object!
1292 					}
1293 				}
1294 
1295 				xObjRef = NULL;*/
1296 			}
1297 			else if ( xObjRef.is() )
1298 			{
1299 				if ( pModel->getUnoModel().is() )
1300 				{
1301 					// remove object, but don't close it (that's up to someone else)
1302 					comphelper::EmbeddedObjectContainer* pContainer = xObjRef.GetContainer();
1303 					if ( pContainer )
1304 					{
1305 						pContainer->RemoveEmbeddedObject( xObjRef.GetObject(), sal_False);
1306 
1307 						// TODO/LATER: mpImpl->aPersistName contains outdated information, to have it uptodate
1308 						// it should be returned from RemoveEmbeddedObject call. Currently it is no problem,
1309 						// since no container is adjusted, actually the empty string could be provided as a name here
1310 						xObjRef.AssignToContainer( NULL, mpImpl->aPersistName );
1311 					}
1312 
1313 					DisconnectFileLink_Impl();
1314 				}
1315 			}
1316 		}
1317 
1318 		if ( xObjRef.is() && mpImpl->pLightClient )
1319 		{
1320 			xObjRef->removeStateChangeListener ( mpImpl->pLightClient );
1321 			xObjRef->removeEventListener( uno::Reference< document::XEventListener >( mpImpl->pLightClient ) );
1322 			xObjRef->setClientSite( NULL );
1323 
1324 			GetSdrGlobalData().GetOLEObjCache().RemoveObj(this);
1325 		}
1326 	}
1327 	catch( ::com::sun::star::uno::Exception& e )
1328 	{
1329 		(void)e;
1330 		DBG_ERROR(
1331 			(OString("SdrOle2Obj::Disconnect_Impl(), "
1332 					"exception caught: ") +
1333 			rtl::OUStringToOString(
1334 				comphelper::anyToString( cppu::getCaughtException() ),
1335 				RTL_TEXTENCODING_UTF8 )).getStr() );
1336 	}
1337 
1338 	mpImpl->mbConnected = false;
1339 }
1340 
1341 // -----------------------------------------------------------------------------
1342 
1343 void SdrOle2Obj::SetModel(SdrModel* pNewModel)
1344 {
1345     ::comphelper::IEmbeddedHelper* pDestPers = pNewModel ? pNewModel->GetPersist() : 0;
1346     ::comphelper::IEmbeddedHelper* pSrcPers  = pModel ? pModel->GetPersist() : 0;
1347 
1348     if ( pNewModel == pModel )
1349     {
1350         // don't know if this is necessary or if it will ever happen, but who know?!
1351         SdrRectObj::SetModel( pNewModel );
1352         return;
1353     }
1354 
1355     // assignment to model has changed
1356     DBG_ASSERT( pSrcPers || !mpImpl->mbConnected, "Connected object without a model?!" );
1357 
1358 	DBG_ASSERT( pDestPers, "The destination model must have a persistence! Please submit an issue!" );
1359 	DBG_ASSERT( pDestPers != pSrcPers, "The source and the destination models should have different persistences! Problems are possible!" );
1360 
1361 	// this is a bug if the target model has no persistence
1362 	// no error handling is possible so just do nothing in this method
1363 	if ( !pDestPers )
1364 		return;
1365 
1366     RemoveListeners_Impl();
1367 
1368     if( pDestPers && pSrcPers && !IsEmptyPresObj() )
1369     {
1370 		try
1371 		{
1372 			// move the objects' storage; ObjectRef remains the same, but PersistName may change
1373 			::rtl::OUString aTmp;
1374 			comphelper::EmbeddedObjectContainer& rContainer = pSrcPers->getEmbeddedObjectContainer();
1375 			uno::Reference < embed::XEmbeddedObject > xObj = rContainer.GetEmbeddedObject( mpImpl->aPersistName );
1376 			DBG_ASSERT( !xObjRef.is() || xObjRef.GetObject() == xObj, "Wrong object identity!" );
1377 			if ( xObj.is() )
1378 			{
1379 				pDestPers->getEmbeddedObjectContainer().MoveEmbeddedObject( rContainer, xObj, aTmp );
1380 				mpImpl->aPersistName = aTmp;
1381 				xObjRef.AssignToContainer( &pDestPers->getEmbeddedObjectContainer(), aTmp );
1382 			}
1383 	        DBG_ASSERT( aTmp.getLength(), "Copying embedded object failed!" );
1384 		}
1385 		catch( ::com::sun::star::uno::Exception& e )
1386 		{
1387 			(void)e;
1388 			DBG_ERROR(
1389 				(OString("SdrOle2Obj::SetModel(), "
1390 						"exception caught: ") +
1391 				rtl::OUStringToOString(
1392 					comphelper::anyToString( cppu::getCaughtException() ),
1393 					RTL_TEXTENCODING_UTF8 )).getStr() );
1394 		}
1395     }
1396 
1397 	SdrRectObj::SetModel( pNewModel );
1398 
1399     // #i43086#
1400     // #i85304 redo the change for charts for the above bugfix, as #i43086# does not ocur anymore
1401     //so maybe the ImpSetVisAreaSize call can be removed here completely
1402     //Nevertheless I leave it in for other objects as I am not sure about the side effects when removing now
1403     if( pModel && !pModel->isLocked() && !IsChart() )
1404         ImpSetVisAreaSize();
1405 
1406     if( pDestPers && !IsEmptyPresObj() )
1407     {
1408         if ( !pSrcPers || IsEmptyPresObj() )
1409             // object wasn't connected, now it should
1410             Connect_Impl();
1411         else
1412             Reconnect_Impl();
1413     }
1414 
1415     AddListeners_Impl();
1416 }
1417 
1418 // -----------------------------------------------------------------------------
1419 
1420 void SdrOle2Obj::SetPage(SdrPage* pNewPage)
1421 {
1422 	FASTBOOL bRemove=pNewPage==NULL && pPage!=NULL;
1423 	FASTBOOL bInsert=pNewPage!=NULL && pPage==NULL;
1424 
1425     if (bRemove && mpImpl->mbConnected )
1426         Disconnect();
1427 
1428 	SdrRectObj::SetPage(pNewPage);
1429 
1430     if (bInsert && !mpImpl->mbConnected )
1431         Connect();
1432 }
1433 
1434 // -----------------------------------------------------------------------------
1435 
1436 void SdrOle2Obj::SetObjRef( const com::sun::star::uno::Reference < com::sun::star::embed::XEmbeddedObject >& rNewObjRef )
1437 {
1438     DBG_ASSERT( !rNewObjRef.is() || !xObjRef.GetObject().is(), "SetObjRef called on already initialized object!");
1439     if( rNewObjRef == xObjRef.GetObject() )
1440 		return;
1441 
1442     // MBA: the caller of the method is responsible to control the old object, it will not be closed here
1443     // Otherwise WW8 import crashes because it tranfers control to OLENode by this method
1444     if ( xObjRef.GetObject().is() )
1445         xObjRef.Lock( sal_False );
1446 
1447     // MBA: avoid removal of object in Disconnect! It is definitely a HACK to call SetObjRef(0)!
1448     // This call will try to close the objects; so if anybody else wants to keep it, it must be locked by a CloseListener
1449     xObjRef.Clear();
1450 
1451     if ( mpImpl->mbConnected )
1452         Disconnect();
1453 
1454     xObjRef.Assign( rNewObjRef, GetAspect() );
1455     m_bTypeAsked = false;
1456 
1457     if ( xObjRef.is() )
1458     {
1459         DELETEZ( pGraphic );
1460 
1461         if ( (xObjRef->getStatus( GetAspect() ) & embed::EmbedMisc::EMBED_NEVERRESIZE ) )
1462             SetResizeProtect(sal_True);
1463 
1464         // #108759# For math objects, set closed state to transparent
1465         if( ImplIsMathObj( rNewObjRef ) )
1466             SetClosedObj( false );
1467 
1468         Connect();
1469     }
1470 
1471 	SetChanged();
1472 	BroadcastObjectChange();
1473 }
1474 
1475 // -----------------------------------------------------------------------------
1476 
1477 void SdrOle2Obj::SetClosedObj( bool bIsClosed )
1478 {
1479     // TODO/LATER: do we still need this hack?
1480     // #108759# Allow changes to the closed state of OLE objects
1481     bClosedObj = bIsClosed;
1482 }
1483 
1484 // -----------------------------------------------------------------------------
1485 
1486 SdrObject* SdrOle2Obj::getFullDragClone() const
1487 {
1488     // special handling for OLE. The default handling works, but is too
1489     // slow when the whole OLE needs to be cloned. Get the Metafile and
1490     // create a graphic object with it
1491     Graphic* pOLEGraphic = GetGraphic();
1492     SdrObject* pClone = 0;
1493 
1494     if(Application::GetSettings().GetStyleSettings().GetHighContrastMode())
1495     {
1496         pOLEGraphic = getEmbeddedObjectRef().GetHCGraphic();
1497     }
1498 
1499     if(pOLEGraphic)
1500     {
1501         pClone = new SdrGrafObj(*pOLEGraphic, GetSnapRect());
1502 
1503         // this would be the place where to copy all attributes
1504         // when OLE will support fill and line style
1505         // pClone->SetMergedItem(pOleObject->GetMergedItemSet());
1506     }
1507     else
1508     {
1509         // #i100710# pOLEGraphic may be zero (no visualisation available),
1510         // so we need to use the OLE replacement graphic
1511         pClone = new SdrRectObj(GetSnapRect());
1512 
1513         // gray outline
1514         pClone->SetMergedItem(XLineStyleItem(XLINE_SOLID));
1515         const svtools::ColorConfig aColorConfig;
1516         const svtools::ColorConfigValue aColor(aColorConfig.GetColorValue(svtools::OBJECTBOUNDARIES));
1517         pClone->SetMergedItem(XLineColorItem(String(), aColor.nColor));
1518 
1519         // bitmap fill
1520         pClone->SetMergedItem(XFillStyleItem(XFILL_BITMAP));
1521         pClone->SetMergedItem(XFillBitmapItem(String(), GetEmtyOLEReplacementBitmap()));
1522         pClone->SetMergedItem(XFillBmpTileItem(false));
1523         pClone->SetMergedItem(XFillBmpStretchItem(false));
1524     }
1525 
1526 	return pClone;
1527 }
1528 
1529 // -----------------------------------------------------------------------------
1530 
1531 void SdrOle2Obj::SetPersistName( const String& rPersistName )
1532 {
1533     DBG_ASSERT( !mpImpl->aPersistName.Len(), "Persist name changed!");
1534 
1535     mpImpl->aPersistName = rPersistName;
1536 	mpImpl->mbLoadingOLEObjectFailed = false;
1537 
1538 	Connect();
1539     SetChanged();
1540 }
1541 
1542 void SdrOle2Obj::AbandonObject()
1543 {
1544     mpImpl->aPersistName.Erase();
1545 	mpImpl->mbLoadingOLEObjectFailed = false;
1546     SetObjRef(0);
1547 }
1548 
1549 // -----------------------------------------------------------------------------
1550 
1551 String SdrOle2Obj::GetPersistName() const
1552 {
1553     return mpImpl->aPersistName;
1554 }
1555 
1556 // -----------------------------------------------------------------------------
1557 
1558 void SdrOle2Obj::TakeObjInfo(SdrObjTransformInfoRec& rInfo) const
1559 {
1560 	rInfo.bRotateFreeAllowed=sal_False;
1561 	rInfo.bRotate90Allowed  =sal_False;
1562 	rInfo.bMirrorFreeAllowed=sal_False;
1563 	rInfo.bMirror45Allowed  =sal_False;
1564 	rInfo.bMirror90Allowed  =sal_False;
1565 	rInfo.bTransparenceAllowed = sal_False;
1566 	rInfo.bGradientAllowed = sal_False;
1567 	rInfo.bShearAllowed     =sal_False;
1568 	rInfo.bEdgeRadiusAllowed=sal_False;
1569 	rInfo.bNoOrthoDesired   =sal_False;
1570 	rInfo.bCanConvToPath    =sal_False;
1571 	rInfo.bCanConvToPoly    =sal_False;
1572 	rInfo.bCanConvToPathLineToArea=sal_False;
1573 	rInfo.bCanConvToPolyLineToArea=sal_False;
1574 	rInfo.bCanConvToContour = sal_False;
1575 }
1576 
1577 // -----------------------------------------------------------------------------
1578 
1579 sal_uInt16 SdrOle2Obj::GetObjIdentifier() const
1580 {
1581 	return bFrame ? sal_uInt16(OBJ_FRAME) : sal_uInt16(OBJ_OLE2);
1582 }
1583 
1584 // -----------------------------------------------------------------------------
1585 
1586 void SdrOle2Obj::TakeObjNameSingul(XubString& rName) const
1587 {
1588 	rName = ImpGetResStr(bFrame ? STR_ObjNameSingulFrame : STR_ObjNameSingulOLE2);
1589 
1590 	const String aName(GetName());
1591 
1592 	if( aName.Len() )
1593 	{
1594 		rName.AppendAscii(" '");
1595 		rName += aName;
1596 		rName += sal_Unicode('\'');
1597 	}
1598 }
1599 
1600 // -----------------------------------------------------------------------------
1601 
1602 void SdrOle2Obj::TakeObjNamePlural(XubString& rName) const
1603 {
1604 	rName=ImpGetResStr(bFrame ? STR_ObjNamePluralFrame : STR_ObjNamePluralOLE2);
1605 }
1606 
1607 // -----------------------------------------------------------------------------
1608 
1609 void SdrOle2Obj::operator=(const SdrObject& rObj)
1610 {
1611     //TODO/LATER: who takes over control of my old object?!
1612     if( &rObj != this )
1613 	{
1614 		// #116235#
1615 		// ImpAssign( rObj );
1616         const SdrOle2Obj& rOle2Obj = static_cast< const SdrOle2Obj& >( rObj );
1617 
1618         uno::Reference < util::XCloseable > xClose( xObjRef.GetObject(), uno::UNO_QUERY );
1619 
1620         if( pModel && mpImpl->mbConnected )
1621             Disconnect();
1622 
1623         SdrRectObj::operator=( rObj );
1624 
1625         // #108867# Manually copying bClosedObj attribute
1626         SetClosedObj( rObj.IsClosedObj() );
1627 
1628         mpImpl->aPersistName = rOle2Obj.mpImpl->aPersistName;
1629         aProgName = rOle2Obj.aProgName;
1630         bFrame = rOle2Obj.bFrame;
1631 
1632         if( rOle2Obj.pGraphic )
1633         {
1634             if( pGraphic )
1635             {
1636                 delete pGraphic;
1637                 delete mpImpl->pGraphicObject;
1638             }
1639 
1640             pGraphic = new Graphic( *rOle2Obj.pGraphic );
1641             mpImpl->pGraphicObject = new GraphicObject( *pGraphic );
1642         }
1643 
1644         if( pModel && rObj.GetModel() && !IsEmptyPresObj() )
1645         {
1646             ::comphelper::IEmbeddedHelper* pDestPers = pModel->GetPersist();
1647             ::comphelper::IEmbeddedHelper* pSrcPers = rObj.GetModel()->GetPersist();
1648             if( pDestPers && pSrcPers )
1649             {
1650     			DBG_ASSERT( !xObjRef.is(), "Object already existing!" );
1651     			comphelper::EmbeddedObjectContainer& rContainer = pSrcPers->getEmbeddedObjectContainer();
1652     			uno::Reference < embed::XEmbeddedObject > xObj = rContainer.GetEmbeddedObject( mpImpl->aPersistName );
1653     			if ( xObj.is() )
1654     			{
1655         			::rtl::OUString aTmp;
1656         			xObjRef.Assign( pDestPers->getEmbeddedObjectContainer().CopyAndGetEmbeddedObject( rContainer, xObj, aTmp ), rOle2Obj.GetAspect() );
1657                     m_bTypeAsked = false;
1658         			mpImpl->aPersistName = aTmp;
1659 					CheckFileLink_Impl();
1660     			}
1661 
1662                 Connect();
1663 
1664                 /* only needed for MSOLE-Objects, now handled inside implementation of Object
1665 				if ( xObjRef.is() && rOle2Obj.xObjRef.is() && rOle2Obj.GetAspect() != embed::Aspects::MSOLE_ICON )
1666 				{
1667 					try
1668 					{
1669 						awt::Size aVisSize = rOle2Obj.xObjRef->getVisualAreaSize( rOle2Obj.GetAspect() );
1670 						if( rOle2Obj.xObjRef->getMapUnit( rOle2Obj.GetAspect() ) == xObjRef->getMapUnit( GetAspect() ) )
1671 						xObjRef->setVisualAreaSize( GetAspect(), aVisSize );
1672 					}
1673 					catch ( embed::WrongStateException& )
1674 					{
1675 						// setting of VisArea not necessary for objects that don't cache it in loaded state
1676 					}
1677 					catch( embed::NoVisualAreaSizeException& )
1678 					{
1679 						// objects my not have visual areas
1680 					}
1681 					catch( uno::Exception& e )
1682 					{
1683 						(void)e;
1684 						DBG_ERROR( "SdrOle2Obj::operator=(), unexcpected exception caught!" );
1685 					}
1686                 }                                                                            */
1687             }
1688         }
1689     }
1690 }
1691 
1692 // -----------------------------------------------------------------------------
1693 
1694 void SdrOle2Obj::ImpSetVisAreaSize()
1695 {
1696 	// currently there is no need to recalculate scaling for iconified objects
1697 	// TODO/LATER: it might be needed in future when it is possible to change the icon
1698 	if ( GetAspect() == embed::Aspects::MSOLE_ICON )
1699 		return;
1700 
1701     // the object area of an embedded object was changed, e.g. by user interaction an a selected object
1702     GetObjRef();
1703     if ( xObjRef.is() )
1704 	{
1705         OSL_ASSERT( pModel );
1706         sal_Int64 nMiscStatus = xObjRef->getStatus( GetAspect() );
1707 
1708 		// the client is required to get access to scaling
1709 		SfxInPlaceClient* pClient = SfxInPlaceClient::GetClient( dynamic_cast<SfxObjectShell*>(pModel->GetPersist()), xObjRef.GetObject() );
1710 		sal_Bool bHasOwnClient =
1711 						( mpImpl->pLightClient
1712 						&& xObjRef->getClientSite() == uno::Reference< embed::XEmbeddedClient >( mpImpl->pLightClient ) );
1713 
1714 		if ( pClient || bHasOwnClient )
1715 		{
1716             // TODO/LATER: IMHO we need to do similar things when object is UIActive or OutplaceActive?! (MBA)
1717             if ( ((nMiscStatus & embed::EmbedMisc::MS_EMBED_RECOMPOSEONRESIZE) &&
1718                     svt::EmbeddedObjectRef::TryRunningState( xObjRef.GetObject() ))
1719                     || xObjRef->getCurrentState() == embed::EmbedStates::INPLACE_ACTIVE
1720                     )
1721 			{
1722 				Fraction aScaleWidth;
1723 				Fraction aScaleHeight;
1724 				if ( pClient )
1725 				{
1726 					aScaleWidth = pClient->GetScaleWidth();
1727 					aScaleHeight = pClient->GetScaleHeight();
1728 				}
1729 				else
1730 				{
1731 					aScaleWidth = mpImpl->pLightClient->GetScaleWidth();
1732 					aScaleHeight = mpImpl->pLightClient->GetScaleHeight();
1733 				}
1734 
1735                 // The object wants to resize itself (f.e. Chart wants to recalculate the layout)
1736                 // or object is inplace active and so has a window that must be resized also
1737                 // In these cases the change in the object area size will be reflected in a change of the
1738                 // objects' visual area. The scaling will not change, but it might exist already and must
1739                 // be used in calculations
1740 				MapUnit aMapUnit = VCLUnoHelper::UnoEmbed2VCLMapUnit( xObjRef->getMapUnit( GetAspect() ) );
1741 				Size aVisSize( (long)( Fraction( aRect.GetWidth() ) / aScaleWidth ),
1742 								(long)( Fraction( aRect.GetHeight() ) / aScaleHeight ) );
1743 
1744 				aVisSize = OutputDevice::LogicToLogic( aVisSize, pModel->GetScaleUnit(), aMapUnit);
1745 				awt::Size aSz;
1746 				aSz.Width = aVisSize.Width();
1747 				aSz.Height = aVisSize.Height();
1748 				xObjRef->setVisualAreaSize( GetAspect(), aSz );
1749 
1750 				try
1751 				{
1752 					aSz = xObjRef->getVisualAreaSize( GetAspect() );
1753 				}
1754 				catch( embed::NoVisualAreaSizeException& )
1755 				{}
1756 
1757 				Rectangle aAcceptedVisArea;
1758 				aAcceptedVisArea.SetSize( Size( (long)( Fraction( long( aSz.Width ) ) * aScaleWidth ),
1759 												(long)( Fraction( long( aSz.Height ) ) * aScaleHeight ) ) );
1760 				if (aVisSize != aAcceptedVisArea.GetSize())
1761 				{
1762                     // server changed VisArea to its liking and the VisArea is different than the suggested one
1763                     // store the new value as given by the object
1764 					MapUnit aNewMapUnit = VCLUnoHelper::UnoEmbed2VCLMapUnit( xObjRef->getMapUnit( GetAspect() ) );
1765 					aRect.SetSize(OutputDevice::LogicToLogic( aAcceptedVisArea.GetSize(), aNewMapUnit, pModel->GetScaleUnit()));
1766                 }
1767 
1768                 // make the new object area known to the client
1769                 // compared to the "else" branch aRect might have been changed by the object and no additional scaling was applied
1770                 // OJ: WHY this -> OSL_ASSERT( pClient );
1771                 if( pClient )
1772                     pClient->SetObjArea(aRect);
1773 
1774                 // we need a new replacement image as the object has resized itself
1775 
1776                 //#i79578# don't request a new replacement image for charts to often
1777                 //a chart sends a modified call to the framework if it was changed
1778                 //thus the replacement update is already handled there
1779                 if( !IsChart() )
1780                     xObjRef.UpdateReplacement();
1781 			}
1782 			else
1783 			{
1784                 // The object isn't active and does not want to resize itself so the changed object area size
1785                 // will be reflected in a changed object scaling
1786 				Fraction aScaleWidth;
1787 				Fraction aScaleHeight;
1788 				Size aObjAreaSize;
1789 				if ( CalculateNewScaling( aScaleWidth, aScaleHeight, aObjAreaSize ) )
1790 				{
1791 					if ( pClient )
1792 					{
1793 						Rectangle aScaleRect(aRect.TopLeft(), aObjAreaSize);
1794 						pClient->SetObjAreaAndScale( aScaleRect, aScaleWidth, aScaleHeight);
1795 					}
1796 					else
1797 					{
1798 						mpImpl->pLightClient->SetSizeScale( aScaleWidth, aScaleHeight );
1799 					}
1800 				}
1801 			}
1802 		}
1803         else if( (nMiscStatus & embed::EmbedMisc::MS_EMBED_RECOMPOSEONRESIZE) &&
1804             svt::EmbeddedObjectRef::TryRunningState( xObjRef.GetObject() ) )
1805         {
1806             //also handle not sfx based ole objects e.g. charts
1807             //#i83860# resizing charts in impress distorts fonts
1808             uno::Reference< embed::XVisualObject > xVisualObject( this->getXModel(), uno::UNO_QUERY );
1809             if( xVisualObject.is() )
1810             {
1811                 MapUnit aMapUnit = VCLUnoHelper::UnoEmbed2VCLMapUnit( xObjRef->getMapUnit( GetAspect() ) );
1812                 Point aTL( aRect.TopLeft() );
1813                 Point aBR( aRect.BottomRight() );
1814                 Point aTL2( OutputDevice::LogicToLogic( aTL, pModel->GetScaleUnit(), aMapUnit) );
1815                 Point aBR2( OutputDevice::LogicToLogic( aBR, pModel->GetScaleUnit(), aMapUnit) );
1816                 Rectangle aNewRect( aTL2, aBR2 );
1817                 xVisualObject->setVisualAreaSize( GetAspect(), awt::Size( aNewRect.GetWidth(), aNewRect.GetHeight() ) );
1818             }
1819         }
1820 	}
1821 }
1822 
1823 // -----------------------------------------------------------------------------
1824 
1825 void SdrOle2Obj::NbcResize(const Point& rRef, const Fraction& xFact, const Fraction& yFact)
1826 {
1827 	if( pModel && !pModel->isLocked() )
1828     {
1829         GetObjRef();
1830         if ( xObjRef.is() && ( xObjRef->getStatus( GetAspect() ) & embed::EmbedMisc::MS_EMBED_RECOMPOSEONRESIZE ) )
1831         {
1832             // if the object needs recompose on resize
1833             // the client site should be created before the resize will take place
1834             // check whether there is no client site and create it if necessary
1835             AddOwnLightClient();
1836         }
1837     }
1838 
1839 	SdrRectObj::NbcResize(rRef,xFact,yFact);
1840 	if (aGeo.nShearWink!=0 || aGeo.nDrehWink!=0) { // kleine Korrekturen
1841 		if (aGeo.nDrehWink>=9000 && aGeo.nDrehWink<27000) {
1842 			aRect.Move(aRect.Left()-aRect.Right(),aRect.Top()-aRect.Bottom());
1843 		}
1844 		aGeo.nDrehWink=0;
1845 		aGeo.nShearWink=0;
1846 		aGeo.nSin=0.0;
1847 		aGeo.nCos=1.0;
1848 		aGeo.nTan=0.0;
1849 		SetRectsDirty();
1850 	}
1851 	if( pModel && !pModel->isLocked() )
1852 		ImpSetVisAreaSize();
1853 }
1854 
1855 // -----------------------------------------------------------------------------
1856 
1857 void SdrOle2Obj::SetGeoData(const SdrObjGeoData& rGeo)
1858 {
1859 	SdrRectObj::SetGeoData(rGeo);
1860 	if( pModel && !pModel->isLocked() )
1861 		ImpSetVisAreaSize();
1862 }
1863 
1864 // -----------------------------------------------------------------------------
1865 
1866 void SdrOle2Obj::NbcSetSnapRect(const Rectangle& rRect)
1867 {
1868 	SdrRectObj::NbcSetSnapRect(rRect);
1869 	if( pModel && !pModel->isLocked() )
1870 		ImpSetVisAreaSize();
1871 
1872     if ( xObjRef.is() && IsChart() )
1873     {
1874         //#i103460# charts do not necessaryly have an own size within ODF files,
1875         //for this case they need to use the size settings from the surrounding frame,
1876         //which is made available with this method as there is no other way
1877         xObjRef.SetDefaultSizeForChart( Size( rRect.GetWidth(), rRect.GetHeight() ) );
1878     }
1879 }
1880 
1881 // -----------------------------------------------------------------------------
1882 
1883 void SdrOle2Obj::NbcSetLogicRect(const Rectangle& rRect)
1884 {
1885 	SdrRectObj::NbcSetLogicRect(rRect);
1886 	if( pModel && !pModel->isLocked() )
1887 		ImpSetVisAreaSize();
1888 }
1889 
1890 Graphic* SdrOle2Obj::GetGraphic() const
1891 {
1892     if ( xObjRef.is() )
1893         return xObjRef.GetGraphic();
1894     return pGraphic;
1895 }
1896 
1897 void SdrOle2Obj::GetNewReplacement()
1898 {
1899 	if ( xObjRef.is() )
1900 		xObjRef.UpdateReplacement();
1901 }
1902 
1903 // -----------------------------------------------------------------------------
1904 
1905 Size SdrOle2Obj::GetOrigObjSize( MapMode* pTargetMapMode ) const
1906 {
1907 	return xObjRef.GetSize( pTargetMapMode );
1908 }
1909 
1910 // -----------------------------------------------------------------------------
1911 
1912 void SdrOle2Obj::NbcMove(const Size& rSize)
1913 {
1914 	SdrRectObj::NbcMove(rSize);
1915 	if( pModel && !pModel->isLocked() )
1916 		ImpSetVisAreaSize();
1917 }
1918 
1919 // -----------------------------------------------------------------------------
1920 
1921 sal_Bool SdrOle2Obj::CanUnloadRunningObj( const uno::Reference< embed::XEmbeddedObject >& xObj, sal_Int64 nAspect )
1922 {
1923 	sal_Bool bResult = sal_False;
1924 
1925 	sal_Int32 nState = xObj->getCurrentState();
1926 	if ( nState == embed::EmbedStates::LOADED )
1927 	{
1928         // the object is already unloaded
1929 		bResult = sal_True;
1930 	}
1931 	else
1932 	{
1933         uno::Reference < util::XModifiable > xModifiable( xObj->getComponent(), uno::UNO_QUERY );
1934         if ( !xModifiable.is() )
1935             bResult = sal_True;
1936         else
1937         {
1938             sal_Int64 nMiscStatus = xObj->getStatus( nAspect );
1939 
1940             if ( embed::EmbedMisc::MS_EMBED_ALWAYSRUN != ( nMiscStatus & embed::EmbedMisc::MS_EMBED_ALWAYSRUN ) &&
1941             embed::EmbedMisc::EMBED_ACTIVATEIMMEDIATELY != ( nMiscStatus & embed::EmbedMisc::EMBED_ACTIVATEIMMEDIATELY ) &&
1942             !( xModifiable.is() && xModifiable->isModified() ) &&
1943             !( nState == embed::EmbedStates::INPLACE_ACTIVE || nState == embed::EmbedStates::UI_ACTIVE || nState == embed::EmbedStates::ACTIVE ) )
1944             {
1945                 bResult = sal_True;
1946             }
1947         }
1948     }
1949 
1950 	return bResult;
1951 }
1952 
1953 // -----------------------------------------------------------------------------
1954 
1955 sal_Bool SdrOle2Obj::Unload( const uno::Reference< embed::XEmbeddedObject >& xObj, sal_Int64 nAspect )
1956 {
1957 	sal_Bool bResult = sal_False;
1958 
1959     if ( CanUnloadRunningObj( xObj, nAspect ) )
1960     {
1961         try
1962         {
1963             xObj->changeState( embed::EmbedStates::LOADED );
1964             bResult = sal_True;
1965         }
1966         catch( ::com::sun::star::uno::Exception& e )
1967         {
1968             (void)e;
1969             DBG_ERROR(
1970                 (OString("SdrOle2Obj::Unload=(), "
1971                         "exception caught: ") +
1972                 rtl::OUStringToOString(
1973                     comphelper::anyToString( cppu::getCaughtException() ),
1974                     RTL_TEXTENCODING_UTF8 )).getStr() );
1975         }
1976     }
1977 
1978 	return bResult;
1979 }
1980 
1981 // -----------------------------------------------------------------------------
1982 
1983 sal_Bool SdrOle2Obj::Unload()
1984 {
1985 	sal_Bool bUnloaded = sal_False;
1986 
1987     if( xObjRef.is() )
1988 	{
1989         //TODO/LATER: no refcounting tricks anymore!
1990         //"customers" must register as state change listeners
1991 		//Nicht notwendig im Doc DTor (MM)
1992         //sal_uIntPtr nRefCount = (*ppObjRef)->GetRefCount();
1993 		// prevent Unload if there are external references
1994         //if( nRefCount > 2 )
1995         //    return sal_False;
1996         //DBG_ASSERT( nRefCount == 2, "Wrong RefCount for unload" );
1997 	}
1998 	else
1999 		bUnloaded = sal_True;
2000 
2001     if ( pModel && xObjRef.is() )
2002     {
2003 		bUnloaded = Unload( xObjRef.GetObject(), GetAspect() );
2004 	}
2005 
2006 	return bUnloaded;
2007 }
2008 
2009 // -----------------------------------------------------------------------------
2010 
2011 void SdrOle2Obj::GetObjRef_Impl()
2012 {
2013     if ( !xObjRef.is() && mpImpl->aPersistName.Len() && pModel && pModel->GetPersist() )
2014     {
2015 		// #107645#
2016         // Only try loading if it did not went wrong up to now
2017 		if(!mpImpl->mbLoadingOLEObjectFailed)
2018 		{
2019             xObjRef.Assign( pModel->GetPersist()->getEmbeddedObjectContainer().GetEmbeddedObject( mpImpl->aPersistName ), GetAspect() );
2020             m_bTypeAsked = false;
2021 			CheckFileLink_Impl();
2022 
2023 			// #107645#
2024 			// If loading of OLE object failed, remember that to not invoke a endless
2025 			// loop trying to load it again and again.
2026             if( xObjRef.is() )
2027 			{
2028 				mpImpl->mbLoadingOLEObjectFailed = sal_True;
2029 			}
2030 
2031             // #108759# For math objects, set closed state to transparent
2032             if( ImplIsMathObj( xObjRef.GetObject() ) )
2033                 SetClosedObj( false );
2034 		}
2035 
2036         if ( xObjRef.is() )
2037 		{
2038 			if( !IsEmptyPresObj() )
2039 			{
2040 				// #75637# remember modified status of model
2041 				const sal_Bool bWasChanged(pModel ? pModel->IsChanged() : sal_False);
2042 
2043 				// perhaps preview not valid anymore
2044 				// #75637# This line changes the modified state of the model
2045                 SetGraphic_Impl( NULL );
2046 
2047 				// #75637# if status was not set before, force it back
2048 				// to not set, so that SetGraphic(0L) above does not
2049 				// set the modified state of the model.
2050 				if(!bWasChanged && pModel && pModel->IsChanged())
2051 				{
2052 					pModel->SetChanged( sal_False );
2053 				}
2054 			}
2055 
2056             sal_Int64 nMiscStatus = xObjRef->getStatus( GetAspect() );
2057             (void)nMiscStatus;
2058             //TODO/LATER: wait until ResizeOnPrinterChange is defined
2059             //if ( nMiscStatus & SVOBJ_MISCSTATUS_RESIZEONPRINTERCHANGE )
2060 			{
2061 				if (pModel && pModel->GetRefDevice() &&
2062 					pModel->GetRefDevice()->GetOutDevType() == OUTDEV_PRINTER)
2063 				{
2064 					if(!bInDestruction)
2065 					{
2066                         //TODO/LATER: printerchange notification
2067                         /*
2068 						// prevent SetModified (don't want no update here)
2069 						sal_Bool bWasEnabled = (*ppObjRef)->IsEnableSetModified();
2070 						if ( bWasEnabled )
2071 							(*ppObjRef)->EnableSetModified( sal_False );
2072 
2073 						// Kein RefDevice oder RefDevice kein Printer
2074 						Printer* pPrinter = (Printer*) pModel->GetRefDevice();
2075 						(*ppObjRef)->OnDocumentPrinterChanged( pPrinter );
2076 
2077 						// reset state
2078                         (*ppObjRef)->EnableSetModified( bWasEnabled );*/
2079 					}
2080 				}
2081 			}
2082         }
2083 
2084         if ( xObjRef.is() )
2085             Connect();
2086     }
2087 
2088     if ( mpImpl->mbConnected )
2089         // move object to first position in cache
2090         GetSdrGlobalData().GetOLEObjCache().InsertObj(this);
2091 }
2092 
2093 uno::Reference < embed::XEmbeddedObject > SdrOle2Obj::GetObjRef() const
2094 {
2095     const_cast<SdrOle2Obj*>(this)->GetObjRef_Impl();
2096     return xObjRef.GetObject();
2097 }
2098 
2099 uno::Reference < embed::XEmbeddedObject > SdrOle2Obj::GetObjRef_NoInit() const
2100 {
2101     return xObjRef.GetObject();
2102 }
2103 
2104 // -----------------------------------------------------------------------------
2105 
2106 uno::Reference< frame::XModel > SdrOle2Obj::getXModel() const
2107 {
2108     GetObjRef();
2109     if ( svt::EmbeddedObjectRef::TryRunningState(xObjRef.GetObject()) )
2110         return uno::Reference< frame::XModel >( xObjRef->getComponent(), uno::UNO_QUERY );
2111     else
2112         return uno::Reference< frame::XModel >();
2113 }
2114 
2115 // -----------------------------------------------------------------------------
2116 
2117 // #109985#
2118 sal_Bool SdrOle2Obj::IsChart() const
2119 {
2120     if ( !m_bTypeAsked )
2121     {
2122         m_bChart = ChartPrettyPainter::IsChart(xObjRef);
2123         m_bTypeAsked = true;
2124     }
2125     return m_bChart;
2126 }
2127 
2128 // -----------------------------------------------------------------------------
2129 void SdrOle2Obj::SetGraphicToObj( const Graphic& aGraphic, const ::rtl::OUString& aMediaType )
2130 {
2131 	xObjRef.SetGraphic( aGraphic, aMediaType );
2132 }
2133 
2134 // -----------------------------------------------------------------------------
2135 void SdrOle2Obj::SetGraphicToObj( const uno::Reference< io::XInputStream >& xGrStream, const ::rtl::OUString& aMediaType )
2136 {
2137 	xObjRef.SetGraphicStream( xGrStream, aMediaType );
2138 }
2139 
2140 // -----------------------------------------------------------------------------
2141 sal_Bool SdrOle2Obj::IsCalc() const
2142 {
2143     if ( !xObjRef.is() )
2144         return false;
2145 
2146     SvGlobalName aObjClsId( xObjRef->getClassID() );
2147     if(    SvGlobalName(SO3_SC_CLASSID_30) == aObjClsId
2148         || SvGlobalName(SO3_SC_CLASSID_40) == aObjClsId
2149         || SvGlobalName(SO3_SC_CLASSID_50) == aObjClsId
2150         || SvGlobalName(SO3_SC_CLASSID_60) == aObjClsId
2151 		|| SvGlobalName(SO3_SC_OLE_EMBED_CLASSID_60) == aObjClsId
2152 		|| SvGlobalName(SO3_SC_OLE_EMBED_CLASSID_8) == aObjClsId
2153 		|| SvGlobalName(SO3_SC_CLASSID) == aObjClsId )
2154     {
2155         return sal_True;
2156     }
2157 
2158 	return sal_False;
2159 }
2160 
2161 // -----------------------------------------------------------------------------
2162 uno::Reference< frame::XModel > SdrOle2Obj::GetParentXModel() const
2163 {
2164     uno::Reference< frame::XModel > xDoc;
2165 	if ( pModel )
2166 		xDoc.set( pModel->getUnoModel(),uno::UNO_QUERY);
2167     return xDoc;
2168 }
2169 
2170 // -----------------------------------------------------------------------------
2171 sal_Bool SdrOle2Obj::CalculateNewScaling( Fraction& aScaleWidth, Fraction& aScaleHeight, Size& aObjAreaSize )
2172 {
2173 	// TODO/LEAN: to avoid rounding errors scaling always uses the VisArea.
2174 	// If we don't cache it for own objects also we must load the object here
2175 	if ( !xObjRef.is() || !pModel )
2176 		return sal_False;
2177 
2178 	MapMode aMapMode( pModel->GetScaleUnit() );
2179 	aObjAreaSize = xObjRef.GetSize( &aMapMode );
2180 
2181 	Size aSize = aRect.GetSize();
2182 	aScaleWidth = Fraction(aSize.Width(),  aObjAreaSize.Width() );
2183 	aScaleHeight = Fraction(aSize.Height(), aObjAreaSize.Height() );
2184 
2185 	// reduce to 10 binary digits
2186 	Kuerzen(aScaleHeight, 10);
2187 	Kuerzen(aScaleWidth,  10);
2188 
2189 	return sal_True;
2190 }
2191 
2192 // -----------------------------------------------------------------------------
2193 sal_Bool SdrOle2Obj::AddOwnLightClient()
2194 {
2195 	// The Own Light Client must be registered in object only using this method!
2196     if ( !SfxInPlaceClient::GetClient( dynamic_cast<SfxObjectShell*>(pModel->GetPersist()), xObjRef.GetObject() )
2197       && !( mpImpl->pLightClient && xObjRef->getClientSite() == uno::Reference< embed::XEmbeddedClient >( mpImpl->pLightClient ) ) )
2198     {
2199 	    Connect();
2200 
2201 	    if ( xObjRef.is() && mpImpl->pLightClient )
2202 	    {
2203 		    Fraction aScaleWidth;
2204 		    Fraction aScaleHeight;
2205 		    Size aObjAreaSize;
2206 		    if ( CalculateNewScaling( aScaleWidth, aScaleHeight, aObjAreaSize ) )
2207 		    {
2208 			    mpImpl->pLightClient->SetSizeScale( aScaleWidth, aScaleHeight );
2209 			    try {
2210 				    xObjRef->setClientSite( mpImpl->pLightClient );
2211 				    return sal_True;
2212 			    } catch( uno::Exception& )
2213 			    {}
2214 		    }
2215 
2216 	    }
2217 
2218     	return sal_False;
2219     }
2220 
2221    	return sal_True;
2222 }
2223 
2224 //////////////////////////////////////////////////////////////////////////////
2225 
2226 Bitmap SdrOle2Obj::GetEmtyOLEReplacementBitmap()
2227 {
2228 	return Bitmap(ResId(BMP_SVXOLEOBJ, *ImpGetResMgr()));
2229 }
2230 
2231 //////////////////////////////////////////////////////////////////////////////
2232 
2233 void SdrOle2Obj::SetWindow(const com::sun::star::uno::Reference < com::sun::star::awt::XWindow >& _xWindow)
2234 {
2235     if ( xObjRef.is() && mpImpl->pLightClient )
2236     {
2237         mpImpl->pLightClient->setWindow(_xWindow);
2238     }
2239 }
2240 
2241 //////////////////////////////////////////////////////////////////////////////
2242 // eof
2243