xref: /aoo42x/main/sc/source/ui/app/client.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_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         pDrawObj->SetLogicRect( GetScaledObjArea() );
173 
174         //  set document modified (SdrModel::SetChanged is not used)
175         // TODO/LATER: is there a reason that this code is not executed in Draw?
176 //        SfxViewShell* pSfxViewSh = GetViewShell();
177 //        ScTabViewShell* pViewSh = PTR_CAST( ScTabViewShell, pSfxViewSh );
178         if (pViewSh)
179             pViewSh->GetViewData()->GetDocShell()->SetDrawModified();
180 	}
181 
182 	if (pDrawObj)
183 		pViewSh->ScrollToObject( pDrawObj );
184 }
185 
186 void __EXPORT ScClient::ViewChanged()
187 {
188 	if ( GetAspect() == embed::Aspects::MSOLE_ICON )
189 	{
190 		// the iconified object seems not to need such a scaling handling
191 		// since the replacement image and the size a completely controlled by the container
192 		// TODO/LATER: when the icon exchange is implemented the scaling handling might be required again here
193 
194 		return;
195 	}
196 
197     uno::Reference < embed::XEmbeddedObject > xObj = GetObject();
198 
199     // TODO/LEAN: working with Visual Area can switch object to running state
200     awt::Size aSz;
201 	try {
202 		aSz = xObj->getVisualAreaSize( GetAspect() );
203 	} catch ( embed::NoVisualAreaSizeException& )
204 	{
205         DBG_ERROR("The visual area size must be available!\n");
206 	}
207 
208     MapUnit aMapUnit = VCLUnoHelper::UnoEmbed2VCLMapUnit( xObj->getMapUnit( GetAspect() ) );
209     Size aVisSize = OutputDevice::LogicToLogic( Size( aSz.Width, aSz.Height ), aMapUnit, MAP_100TH_MM );
210 
211 	//	Groesse ins Dokument uebernehmen
212 	SdrOle2Obj* pDrawObj = GetDrawObj();
213 	if (pDrawObj)
214 	{
215 		Rectangle aLogicRect = pDrawObj->GetLogicRect();
216         Fraction aFractX = GetScaleWidth();
217         Fraction aFractY = GetScaleHeight();
218         aFractX *= aVisSize.Width();
219         aFractY *= aVisSize.Height();
220         aVisSize = Size( (long) aFractX, (long) aFractY );      // skaliert fuer Draw-Model
221 
222         //  pClientData->SetObjArea vor pDrawObj->SetLogicRect, damit keine
223         //  falschen Skalierungen ausgerechnet werden:
224         //Rectangle aObjArea = aLogicRect;
225         //aObjArea.SetSize( aVisSize );          // Dokument-Groesse vom Server
226         //SetObjArea( aObjArea );
227 
228 		SfxViewShell* pSfxViewSh = GetViewShell();
229 		ScTabViewShell* pViewSh = PTR_CAST( ScTabViewShell, pSfxViewSh );
230 		if ( pViewSh )
231 		{
232 			Window* pWin = pViewSh->GetActiveWin();
233 			if ( pWin->LogicToPixel( aVisSize ) != pWin->LogicToPixel( aLogicRect.GetSize() ) )
234 			{
235 				aLogicRect.SetSize( aVisSize );
236 				pDrawObj->SetLogicRect( aLogicRect );
237 
238 				//	set document modified (SdrModel::SetChanged is not used)
239 				pViewSh->GetViewData()->GetDocShell()->SetDrawModified();
240 			}
241 		}
242 	}
243 }
244 
245 void __EXPORT ScClient::MakeVisible()
246 {
247 	SdrOle2Obj* pDrawObj = GetDrawObj();
248 	if (pDrawObj)
249 	{
250 		SfxViewShell* pSfxViewSh = GetViewShell();
251 		ScTabViewShell* pViewSh = PTR_CAST( ScTabViewShell, pSfxViewSh );
252 		if (pViewSh)
253 			pViewSh->ScrollToObject( pDrawObj );
254 	}
255 }
256 
257