1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
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/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
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 .
18  */
19 
20 #include <com/sun/star/embed/XEmbeddedObject.hpp>
21 #include <com/sun/star/embed/NoVisualAreaSizeException.hpp>
22 
23 #include <toolkit/helper/vclunohelper.hxx>
24 #include <sfx2/objsh.hxx>
25 #include <svx/svditer.hxx>
26 #include <svx/svdobj.hxx>
27 #include <svx/svdmodel.hxx>
28 #include <svx/svdpage.hxx>
29 #include <svx/svdoole2.hxx>
30 
31 #include <client.hxx>
32 #include <tabvwsh.hxx>
33 #include <docsh.hxx>
34 #include <gridwin.hxx>
35 
36 using namespace com::sun::star;
37 
ScClient(ScTabViewShell * pViewShell,vcl::Window * pDraw,SdrModel * pSdrModel,const SdrOle2Obj * pObj)38 ScClient::ScClient( ScTabViewShell* pViewShell, vcl::Window* pDraw, SdrModel* pSdrModel, const SdrOle2Obj* pObj ) :
39     SfxInPlaceClient( pViewShell, pDraw, pObj->GetAspect() ),
40     pModel( pSdrModel )
41 {
42     SetObject( pObj->GetObjRef() );
43 }
44 
~ScClient()45 ScClient::~ScClient()
46 {
47 }
48 
GetDrawObj()49 SdrOle2Obj* ScClient::GetDrawObj()
50 {
51     uno::Reference < embed::XEmbeddedObject > xObj = GetObject();
52     SdrOle2Obj* pOle2Obj = nullptr;
53     OUString aName = GetViewShell()->GetObjectShell()->GetEmbeddedObjectContainer().GetEmbeddedObjectName( xObj );
54 
55     sal_uInt16 nPages = pModel->GetPageCount();
56     for (sal_uInt16 nPNr=0; nPNr<nPages && !pOle2Obj; nPNr++)
57     {
58         SdrPage* pPage = pModel->GetPage(nPNr);
59         SdrObjListIter aIter( pPage, SdrIterMode::DeepNoGroups );
60         SdrObject* pObject = aIter.Next();
61         while (pObject && !pOle2Obj)
62         {
63             if ( pObject->GetObjIdentifier() == OBJ_OLE2 )
64             {
65                 // name from InfoObject is PersistName
66                 if ( static_cast<SdrOle2Obj*>(pObject)->GetPersistName() == aName )
67                     pOle2Obj = static_cast<SdrOle2Obj*>(pObject);
68             }
69             pObject = aIter.Next();
70         }
71     }
72     return pOle2Obj;
73 }
74 
RequestNewObjectArea(tools::Rectangle & aLogicRect)75 void ScClient::RequestNewObjectArea( tools::Rectangle& aLogicRect )
76 {
77     SfxViewShell* pSfxViewSh = GetViewShell();
78     ScTabViewShell* pViewSh = dynamic_cast<ScTabViewShell*>( pSfxViewSh  );
79     if (!pViewSh)
80     {
81         OSL_FAIL("Wrong ViewShell");
82         return;
83     }
84 
85     tools::Rectangle aOldRect = GetObjArea();
86     SdrOle2Obj*  pDrawObj = GetDrawObj();
87     if ( pDrawObj )
88     {
89         if ( pDrawObj->IsResizeProtect() )
90             aLogicRect.SetSize( aOldRect.GetSize() );
91 
92         if ( pDrawObj->IsMoveProtect() )
93             aLogicRect.SetPos( aOldRect.TopLeft() );
94     }
95 
96     sal_uInt16 nTab = pViewSh->GetViewData().GetTabNo();
97     SdrPage* pPage = pModel->GetPage(static_cast<sal_uInt16>(static_cast<sal_Int16>(nTab)));
98     if ( pPage && aLogicRect != aOldRect )
99     {
100         Point aPos;
101         Size aSize = pPage->GetSize();
102         if ( aSize.Width() < 0 )
103         {
104             aPos.setX( aSize.Width() + 1 );       // negative
105             aSize.setWidth( -aSize.Width() );     // positive
106         }
107         tools::Rectangle aPageRect( aPos, aSize );
108 
109         if (aLogicRect.Right() > aPageRect.Right())
110         {
111             long nDiff = aLogicRect.Right() - aPageRect.Right();
112             aLogicRect.AdjustLeft( -nDiff );
113             aLogicRect.AdjustRight( -nDiff );
114         }
115         if (aLogicRect.Bottom() > aPageRect.Bottom())
116         {
117             long nDiff = aLogicRect.Bottom() - aPageRect.Bottom();
118             aLogicRect.AdjustTop( -nDiff );
119             aLogicRect.AdjustBottom( -nDiff );
120         }
121 
122         if (aLogicRect.Left() < aPageRect.Left())
123         {
124             long nDiff = aLogicRect.Left() - aPageRect.Left();
125             aLogicRect.AdjustRight( -nDiff );
126             aLogicRect.AdjustLeft( -nDiff );
127         }
128         if (aLogicRect.Top() < aPageRect.Top())
129         {
130             long nDiff = aLogicRect.Top() - aPageRect.Top();
131             aLogicRect.AdjustBottom( -nDiff );
132             aLogicRect.AdjustTop( -nDiff );
133         }
134     }
135 }
136 
ObjectAreaChanged()137 void ScClient::ObjectAreaChanged()
138 {
139     SfxViewShell* pSfxViewSh = GetViewShell();
140     ScTabViewShell* pViewSh = dynamic_cast<ScTabViewShell*>( pSfxViewSh  );
141     if (!pViewSh)
142     {
143         OSL_FAIL("Wrong ViewShell");
144         return;
145     }
146 
147     // Take over position and size into document
148     SdrOle2Obj* pDrawObj = GetDrawObj();
149     if (pDrawObj)
150     {
151         tools::Rectangle aNewRectangle(GetScaledObjArea());
152 
153         // #i118524# if sheared/rotated, center to non-rotated LogicRect
154         pDrawObj->setSuppressSetVisAreaSize(true);
155 
156         if(pDrawObj->GetGeoStat().nRotationAngle || pDrawObj->GetGeoStat().nShearAngle)
157         {
158             pDrawObj->SetLogicRect( aNewRectangle );
159 
160             const tools::Rectangle& rBoundRect = pDrawObj->GetCurrentBoundRect();
161             const Point aDelta(aNewRectangle.Center() - rBoundRect.Center());
162 
163             aNewRectangle.Move(aDelta.X(), aDelta.Y());
164         }
165 
166         pDrawObj->SetLogicRect( aNewRectangle );
167         pDrawObj->setSuppressSetVisAreaSize(false);
168 
169         //  set document modified (SdrModel::SetChanged is not used)
170         pViewSh->GetViewData().GetDocShell()->SetDrawModified();
171         pViewSh->ScrollToObject(pDrawObj);
172     }
173 }
174 
ViewChanged()175 void ScClient::ViewChanged()
176 {
177     if ( GetAspect() == embed::Aspects::MSOLE_ICON )
178     {
179         // the iconified object seems not to need such a scaling handling
180         // since the replacement image and the size a completely controlled by the container
181         // TODO/LATER: when the icon exchange is implemented the scaling handling might be required again here
182 
183         return;
184     }
185 
186     uno::Reference < embed::XEmbeddedObject > xObj = GetObject();
187 
188     // TODO/LEAN: working with Visual Area can switch object to running state
189     awt::Size aSz;
190     try {
191         aSz = xObj->getVisualAreaSize( GetAspect() );
192     } catch ( embed::NoVisualAreaSizeException& )
193     {
194         OSL_FAIL("The visual area size must be available!");
195     }
196 
197     MapUnit aMapUnit = VCLUnoHelper::UnoEmbed2VCLMapUnit( xObj->getMapUnit( GetAspect() ) );
198     Size aVisSize = OutputDevice::LogicToLogic(Size(aSz.Width, aSz.Height), MapMode(aMapUnit), MapMode(MapUnit::Map100thMM));
199 
200     // Take over position and size into document
201     SdrOle2Obj* pDrawObj = GetDrawObj();
202     if (pDrawObj)
203     {
204         tools::Rectangle aLogicRect = pDrawObj->GetLogicRect();
205         Fraction aFractX = GetScaleWidth() * aVisSize.Width();
206         Fraction aFractY = GetScaleHeight() * aVisSize.Height();
207         aVisSize = Size( static_cast<long>(aFractX), static_cast<long>(aFractY) ); // Scaled for Draw model
208 
209         //  pClientData->SetObjArea before pDrawObj->SetLogicRect, so that we don't
210         //  calculate wrong scalings:
211         //Rectangle aObjArea = aLogicRect;
212         //aObjArea.SetSize( aVisSize );          // Document size from the server
213         //SetObjArea( aObjArea );
214 
215         SfxViewShell* pSfxViewSh = GetViewShell();
216         ScTabViewShell* pViewSh = dynamic_cast<ScTabViewShell*>( pSfxViewSh  );
217         if ( pViewSh )
218         {
219             vcl::Window* pWin = pViewSh->GetActiveWin();
220             if ( pWin->LogicToPixel( aVisSize ) != pWin->LogicToPixel( aLogicRect.GetSize() ) )
221             {
222                 aLogicRect.SetSize( aVisSize );
223                 pDrawObj->SetLogicRect( aLogicRect );
224 
225                 // set document modified (SdrModel::SetChanged is not used)
226                 pViewSh->GetViewData().GetDocShell()->SetDrawModified();
227             }
228         }
229     }
230 }
231 
232 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
233