1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <com/sun/star/embed/NoVisualAreaSizeException.hpp>
21 #include <com/sun/star/embed/XEmbeddedObject.hpp>
24 #include <IDocumentSettingAccess.hxx>
28 #include <svtools/embedhlp.hxx>
29 #include <osl/diagnose.h>
31 #include <toolkit/helper/vclunohelper.hxx>
35 using namespace com::sun::star
;
37 SwOleClient::SwOleClient(SwView
*pView
, SwEditWin
*pWin
, const svt::EmbeddedObjectRef
& xObj
)
38 : SfxInPlaceClient( pView
, pWin
, xObj
.GetViewAspect() )
40 , m_IsOldCheckForOLEInCaption(pView
->GetWrtShell().IsCheckForOLEInCaption())
42 SetObject( xObj
.GetObject() );
45 void SwOleClient::RequestNewObjectArea( tools::Rectangle
& aLogRect
)
47 // The server wants to change the client size.
48 // We put the desired size in the core. The attributes of the frame
49 // are set to the desired value. This value will be passed on to the
51 // The core accepts or formats the adjusted values not necessarily.
52 // If the Ole-Frame is formatted, then the CalcAndSetScale() of the WrtShell
53 // will be called. There the scaling of the SwOleClient is set if necessary.
55 SwWrtShell
&rSh
= static_cast<SwView
*>(GetViewShell())->GetWrtShell();
59 // the aLogRect will get the preliminary size now
60 aLogRect
.SetSize( rSh
.RequestObjectResize( SwRect( aLogRect
), GetObject() ) );
62 // the EndAllAction() call will trigger CalcAndSetScale() call,
63 // so the embedded object must get the correct size before
64 if ( aLogRect
.GetSize() != GetScaledObjArea().GetSize() )
66 // size has changed, so first change visual area of the object before we resize its view
67 // without this the object always would be scaled - now it has the choice
69 // TODO/LEAN: getMapUnit can switch object to running state
70 MapMode
aObjectMap( VCLUnoHelper::UnoEmbed2VCLMapUnit( GetObject()->getMapUnit( GetAspect() ) ) );
71 MapMode
aClientMap( GetEditWin()->GetMapMode().GetMapUnit() );
73 Size
aNewObjSize( tools::Long( aLogRect
.GetWidth() / GetScaleWidth() ),
74 tools::Long( aLogRect
.GetHeight() / GetScaleHeight() ) );
76 // convert to logical coordinates of the embedded object
77 Size aNewSize
= GetEditWin()->LogicToLogic( aNewObjSize
, &aClientMap
, &aObjectMap
);
78 GetObject()->setVisualAreaSize( GetAspect(), awt::Size( aNewSize
.Width(), aNewSize
.Height() ) );
83 SwRect
aFrame( rSh
.GetAnyCurRect( CurRectType::FlyEmbedded
, nullptr, GetObject() )),
84 aPrt( rSh
.GetAnyCurRect( CurRectType::FlyEmbeddedPrt
, nullptr, GetObject() ));
85 aLogRect
.SetPos( aPrt
.Pos() + aFrame
.Pos() );
86 aLogRect
.SetSize( aPrt
.SSize() );
89 void SwOleClient::ObjectAreaChanged()
91 SwWrtShell
&rSh
= static_cast<SwView
*>(GetViewShell())->GetWrtShell();
92 SwRect
aFrame( rSh
.GetAnyCurRect( CurRectType::FlyEmbedded
, nullptr, GetObject() ));
93 if ( !aFrame
.Overlaps( rSh
.VisArea() ) )
94 rSh
.MakeVisible( aFrame
);
97 void SwOleClient::ViewChanged()
102 if ( GetAspect() == embed::Aspects::MSOLE_ICON
)
104 // the iconified object seems not to need such a scaling handling
105 // since the replacement image and the size a completely controlled by the container
106 // TODO/LATER: when the icon exchange is implemented the scaling handling
107 // might be required again here
111 SwWrtShell
&rSh
= static_cast<SwView
*>(GetViewShell())->GetWrtShell();
113 // Adjust the size of the object in the core. The Scaling must
114 // be considered. Repercussions on the object are considered by
115 // CalcAndSetScale() of the WrtShell if the size / position of
116 // the frame in the core changes.
118 // TODO/LEAN: getMapUnit can switch object to running state
122 aSz
= GetObject()->getVisualAreaSize( GetAspect() );
124 catch (const embed::NoVisualAreaSizeException
&)
126 // Nothing will be done
128 catch (const uno::Exception
&)
130 OSL_FAIL( "Something goes wrong on requesting object size!" );
133 Size
aVisSize( aSz
.Width
, aSz
.Height
);
135 // As long as from the object comes no reasonable size
136 // nothing can be scaled.
137 if( !aVisSize
.Width() || !aVisSize
.Height() )
140 // first convert to TWIPS before scaling, because scaling factors are calculated for
141 // the TWIPS mapping and so they will produce the best results if applied to TWIPS based
143 const MapMode
aMyMap ( MapUnit::MapTwip
);
144 const MapMode
aObjMap( VCLUnoHelper::UnoEmbed2VCLMapUnit( GetObject()->getMapUnit( GetAspect() ) ) );
145 aVisSize
= OutputDevice::LogicToLogic( aVisSize
, aObjMap
, aMyMap
);
147 aVisSize
.setWidth( tools::Long(aVisSize
.Width() * GetScaleWidth()) );
148 aVisSize
.setHeight( tools::Long(aVisSize
.Height() * GetScaleHeight()) );
150 SwRect
aRect( Point( LONG_MIN
, LONG_MIN
), aVisSize
);
151 rSh
.LockView( true ); // Prevent scrolling in the EndAction
152 rSh
.StartAllAction();
153 rSh
.RequestObjectResize( aRect
, GetObject() );
155 rSh
.LockView( false );
158 void SwOleClient::FormatChanged()
160 const uno::Reference
< embed::XEmbeddedObject
>& xObj( GetObject() );
161 SwView
* pView
= dynamic_cast< SwView
* >( GetViewShell() );
162 if ( pView
&& xObj
.is() && SotExchange::IsMath( xObj
->getClassID() ) )
164 SwWrtShell
& rWrtSh
= pView
->GetWrtShell();
165 if (rWrtSh
.GetDoc()->getIDocumentSettingAccess().get( DocumentSettingId::MATH_BASELINE_ALIGNMENT
))
166 rWrtSh
.AlignFormulaToBaseline( xObj
);
170 bool SwOleClient::IsProtected() const
172 auto pView
= dynamic_cast<SwView
*>(GetViewShell());
178 SwWrtShell
& rWrtSh
= pView
->GetWrtShell();
179 if (rWrtSh
.IsTableMode())
184 SwOLENode
* pOLENode
= rWrtSh
.GetCursor()->GetPointNode().GetOLENode();
190 return pOLENode
->GetOLEObj().IsProtected();
193 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */