xref: /trunk/main/sc/source/ui/app/client.cxx (revision 61dff127b6698e0bae836c8aedd6ec62111483d1)
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_sc.hxx"
30 
31 // INCLUDE ---------------------------------------------------------------
32 
33 
34 #include <com/sun/star/embed/XEmbeddedObject.hpp>
35 #include <com/sun/star/embed/NoVisualAreaSizeException.hpp>
36 
37 #include <toolkit/helper/vclunohelper.hxx>
38 #include <sfx2/objsh.hxx>
39 #include <sfx2/viewfrm.hxx>
40 #include <sot/sotref.hxx>
41 #include <svx/svditer.hxx>
42 #include <svx/svdobj.hxx>
43 #include <svx/svdmodel.hxx>
44 #include <svx/svdpage.hxx>
45 #include <svx/svdoole2.hxx>
46 #include <svx/svdview.hxx>
47 #include <svx/svdograf.hxx>
48 #include <svtools/embedhlp.hxx>
49 
50 #include "client.hxx"
51 #include "tabvwsh.hxx"
52 #include "docsh.hxx"
53 
54 using namespace com::sun::star;
55 
56 //------------------------------------------------------------------------
57 
58 ScClient::ScClient( ScTabViewShell* pViewShell, Window* pDraw, SdrModel* pSdrModel, SdrOle2Obj* pObj ) :
59     SfxInPlaceClient( pViewShell, pDraw, pObj->GetAspect() ),
60     pModel( pSdrModel ),
61     pGrafEdit( 0 )
62 {
63     SetObject( pObj->GetObjRef() );
64 }
65 
66 __EXPORT ScClient::~ScClient()
67 {
68 }
69 
70 SdrOle2Obj* ScClient::GetDrawObj()
71 {
72     uno::Reference < embed::XEmbeddedObject > xObj = GetObject();
73     SdrOle2Obj* pOle2Obj = NULL;
74     String aName = GetViewShell()->GetObjectShell()->GetEmbeddedObjectContainer().GetEmbeddedObjectName( xObj );
75 
76     sal_uInt16 nPages = pModel->GetPageCount();
77     for (sal_uInt16 nPNr=0; nPNr<nPages && !pOle2Obj; nPNr++)
78     {
79         SdrPage* pPage = pModel->GetPage(nPNr);
80         SdrObjListIter aIter( *pPage, IM_DEEPNOGROUPS );
81         SdrObject* pObject = aIter.Next();
82         while (pObject && !pOle2Obj)
83         {
84             if ( pObject->GetObjIdentifier() == OBJ_OLE2 )
85             {
86                 // name from InfoObject is PersistName
87                 if ( ((SdrOle2Obj*)pObject)->GetPersistName() == aName )
88                     pOle2Obj = (SdrOle2Obj*)pObject;
89             }
90             pObject = aIter.Next();
91         }
92     }
93     return pOle2Obj;
94 }
95 
96 void __EXPORT ScClient::RequestNewObjectArea( Rectangle& aLogicRect )
97 {
98     SfxViewShell* pSfxViewSh = GetViewShell();
99     ScTabViewShell* pViewSh = PTR_CAST( ScTabViewShell, pSfxViewSh );
100     if (!pViewSh)
101     {
102         DBG_ERROR("Wrong ViewShell");
103         return;
104     }
105 
106     Rectangle aOldRect = GetObjArea();
107     SdrOle2Obj*  pDrawObj = GetDrawObj();
108     if ( pDrawObj )
109     {
110         if ( pDrawObj->IsResizeProtect() )
111             aLogicRect.SetSize( aOldRect.GetSize() );
112 
113         if ( pDrawObj->IsMoveProtect() )
114             aLogicRect.SetPos( aOldRect.TopLeft() );
115     }
116 
117     sal_uInt16 nTab = pViewSh->GetViewData()->GetTabNo();
118     SdrPage* pPage = pModel->GetPage(static_cast<sal_uInt16>(static_cast<sal_Int16>(nTab)));
119     if ( pPage && aLogicRect != aOldRect )
120     {
121         Point aPos;
122         Size aSize = pPage->GetSize();
123         if ( aSize.Width() < 0 )
124         {
125             aPos.X() = aSize.Width() + 1;       // negative
126             aSize.Width() = -aSize.Width();     // positive
127         }
128         Rectangle aPageRect( aPos, aSize );
129 
130         if (aLogicRect.Right() > aPageRect.Right())
131         {
132             long nDiff = aLogicRect.Right() - aPageRect.Right();
133             aLogicRect.Left() -= nDiff;
134             aLogicRect.Right() -= nDiff;
135         }
136         if (aLogicRect.Bottom() > aPageRect.Bottom())
137         {
138             long nDiff = aLogicRect.Bottom() - aPageRect.Bottom();
139             aLogicRect.Top() -= nDiff;
140             aLogicRect.Bottom() -= nDiff;
141         }
142 
143         if (aLogicRect.Left() < aPageRect.Left())
144         {
145             long nDiff = aLogicRect.Left() - aPageRect.Left();
146             aLogicRect.Right() -= nDiff;
147             aLogicRect.Left() -= nDiff;
148         }
149         if (aLogicRect.Top() < aPageRect.Top())
150         {
151             long nDiff = aLogicRect.Top() - aPageRect.Top();
152             aLogicRect.Bottom() -= nDiff;
153             aLogicRect.Top() -= nDiff;
154         }
155     }
156 }
157 
158 void __EXPORT ScClient::ObjectAreaChanged()
159 {
160     SfxViewShell* pSfxViewSh = GetViewShell();
161     ScTabViewShell* pViewSh = PTR_CAST( ScTabViewShell, pSfxViewSh );
162     if (!pViewSh)
163     {
164         DBG_ERROR("Wrong ViewShell");
165         return;
166     }
167 
168     //  Position und Groesse ins Dokument uebernehmen
169     SdrOle2Obj* pDrawObj = GetDrawObj();
170     if (pDrawObj)
171     {
172         Rectangle aNewRectangle(GetScaledObjArea());
173 
174         // #i118524# if sheared/rotated, center to non-rotated LogicRect
175         pDrawObj->setSuppressSetVisAreaSize(true);
176 
177         if(pDrawObj->GetGeoStat().nDrehWink || pDrawObj->GetGeoStat().nShearWink)
178         {
179             pDrawObj->SetLogicRect( aNewRectangle );
180 
181             const Rectangle& rBoundRect = pDrawObj->GetCurrentBoundRect();
182             const Point aDelta(aNewRectangle.Center() - rBoundRect.Center());
183 
184             aNewRectangle.Move(aDelta.X(), aDelta.Y());
185         }
186 
187         pDrawObj->SetLogicRect( aNewRectangle );
188         pDrawObj->setSuppressSetVisAreaSize(false);
189 
190         //  set document modified (SdrModel::SetChanged is not used)
191         // TODO/LATER: is there a reason that this code is not executed in Draw?
192 //        SfxViewShell* pSfxViewSh = GetViewShell();
193 //        ScTabViewShell* pViewSh = PTR_CAST( ScTabViewShell, pSfxViewSh );
194         if (pViewSh)
195             pViewSh->GetViewData()->GetDocShell()->SetDrawModified();
196     }
197 
198     if (pDrawObj)
199         pViewSh->ScrollToObject( pDrawObj );
200 }
201 
202 void __EXPORT ScClient::ViewChanged()
203 {
204     if ( GetAspect() == embed::Aspects::MSOLE_ICON )
205     {
206         // the iconified object seems not to need such a scaling handling
207         // since the replacement image and the size a completely controlled by the container
208         // TODO/LATER: when the icon exchange is implemented the scaling handling might be required again here
209 
210         return;
211     }
212 
213     uno::Reference < embed::XEmbeddedObject > xObj = GetObject();
214 
215     // TODO/LEAN: working with Visual Area can switch object to running state
216     awt::Size aSz;
217     try {
218         aSz = xObj->getVisualAreaSize( GetAspect() );
219     } catch ( embed::NoVisualAreaSizeException& )
220     {
221         DBG_ERROR("The visual area size must be available!\n");
222     }
223 
224     MapUnit aMapUnit = VCLUnoHelper::UnoEmbed2VCLMapUnit( xObj->getMapUnit( GetAspect() ) );
225     Size aVisSize = OutputDevice::LogicToLogic( Size( aSz.Width, aSz.Height ), aMapUnit, MAP_100TH_MM );
226 
227     //  Groesse ins Dokument uebernehmen
228     SdrOle2Obj* pDrawObj = GetDrawObj();
229     if (pDrawObj)
230     {
231         Rectangle aLogicRect = pDrawObj->GetLogicRect();
232         Fraction aFractX = GetScaleWidth();
233         Fraction aFractY = GetScaleHeight();
234         aFractX *= aVisSize.Width();
235         aFractY *= aVisSize.Height();
236         aVisSize = Size( (long) aFractX, (long) aFractY );      // skaliert fuer Draw-Model
237 
238         //  pClientData->SetObjArea vor pDrawObj->SetLogicRect, damit keine
239         //  falschen Skalierungen ausgerechnet werden:
240         //Rectangle aObjArea = aLogicRect;
241         //aObjArea.SetSize( aVisSize );          // Dokument-Groesse vom Server
242         //SetObjArea( aObjArea );
243 
244         SfxViewShell* pSfxViewSh = GetViewShell();
245         ScTabViewShell* pViewSh = PTR_CAST( ScTabViewShell, pSfxViewSh );
246         if ( pViewSh )
247         {
248             Window* pWin = pViewSh->GetActiveWin();
249             if ( pWin->LogicToPixel( aVisSize ) != pWin->LogicToPixel( aLogicRect.GetSize() ) )
250             {
251                 aLogicRect.SetSize( aVisSize );
252                 pDrawObj->SetLogicRect( aLogicRect );
253 
254                 //  set document modified (SdrModel::SetChanged is not used)
255                 pViewSh->GetViewData()->GetDocShell()->SetDrawModified();
256             }
257         }
258     }
259 }
260 
261 void __EXPORT ScClient::MakeVisible()
262 {
263     SdrOle2Obj* pDrawObj = GetDrawObj();
264     if (pDrawObj)
265     {
266         SfxViewShell* pSfxViewSh = GetViewShell();
267         ScTabViewShell* pViewSh = PTR_CAST( ScTabViewShell, pSfxViewSh );
268         if (pViewSh)
269             pViewSh->ScrollToObject( pDrawObj );
270     }
271 }
272 
273