Line data Source code
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/NoVisualAreaSizeException.hpp>
21 :
22 : #include <svx/svditer.hxx>
23 : #include <svx/svdograf.hxx>
24 : #include <svx/svdoole2.hxx>
25 : #include <svx/svdouno.hxx>
26 : #include <svx/svdpage.hxx>
27 : #include <svx/svdpagv.hxx>
28 : #include <svx/svdundo.hxx>
29 : #include <svx/xbtmpit.hxx>
30 : #include <svx/xoutbmp.hxx>
31 : #include <sfx2/objsh.hxx>
32 : #include <sfx2/viewfrm.hxx>
33 : #include <toolkit/helper/vclunohelper.hxx>
34 : #include <com/sun/star/embed/Aspects.hpp>
35 :
36 : #include "document.hxx"
37 : #include "viewfunc.hxx"
38 : #include "tabvwsh.hxx"
39 : #include "drawview.hxx"
40 : #include "scmod.hxx"
41 : #include "drwlayer.hxx"
42 : #include "drwtrans.hxx"
43 : #include "globstr.hrc"
44 : #include "chartlis.hxx"
45 : #include "docuno.hxx"
46 : #include "docsh.hxx"
47 : #include "convuno.hxx"
48 : #include "dragdata.hxx"
49 :
50 : extern Point aDragStartDiff;
51 :
52 : bool bPasteIsMove = false;
53 :
54 : using namespace com::sun::star;
55 :
56 :
57 0 : static void lcl_AdjustInsertPos( ScViewData* pData, Point& rPos, Size& rSize )
58 : {
59 0 : SdrPage* pPage = pData->GetScDrawView()->GetModel()->GetPage( static_cast<sal_uInt16>(pData->GetTabNo()) );
60 : OSL_ENSURE(pPage,"pPage ???");
61 0 : Size aPgSize( pPage->GetSize() );
62 0 : if (aPgSize.Width() < 0)
63 0 : aPgSize.Width() = -aPgSize.Width();
64 0 : long x = aPgSize.Width() - rPos.X() - rSize.Width();
65 0 : long y = aPgSize.Height() - rPos.Y() - rSize.Height();
66 : // if necessary: adjustments (80/200) for pixel approx. errors
67 0 : if( x < 0 )
68 0 : rPos.X() += x + 80;
69 0 : if( y < 0 )
70 0 : rPos.Y() += y + 200;
71 0 : rPos.X() += rSize.Width() / 2; // position at paste is center
72 0 : rPos.Y() += rSize.Height() / 2;
73 0 : }
74 :
75 0 : void ScViewFunc::PasteDraw( const Point& rLogicPos, SdrModel* pModel,
76 : bool bGroup, bool bSameDocClipboard )
77 : {
78 0 : MakeDrawLayer();
79 0 : Point aPos( rLogicPos );
80 :
81 : // MapMode at Outliner-RefDevice has to be right (as in FuText::MakeOutliner)
82 : //! merge with FuText::MakeOutliner?
83 0 : MapMode aOldMapMode;
84 0 : OutputDevice* pRef = GetViewData()->GetDocument()->GetDrawLayer()->GetRefDevice();
85 0 : if (pRef)
86 : {
87 0 : aOldMapMode = pRef->GetMapMode();
88 0 : pRef->SetMapMode( MapMode(MAP_100TH_MM) );
89 : }
90 :
91 0 : bool bNegativePage = GetViewData()->GetDocument()->IsNegativePage( GetViewData()->GetTabNo() );
92 :
93 0 : SdrView* pDragEditView = NULL;
94 0 : ScModule* pScMod = SC_MOD();
95 0 : const ScDragData& rData = pScMod->GetDragData();
96 0 : ScDrawTransferObj* pDrawTrans = rData.pDrawTransfer;
97 0 : if (pDrawTrans)
98 : {
99 0 : pDragEditView = pDrawTrans->GetDragSourceView();
100 :
101 0 : aPos -= aDragStartDiff;
102 0 : if ( bNegativePage )
103 : {
104 0 : if (aPos.X() > 0) aPos.X() = 0;
105 : }
106 : else
107 : {
108 0 : if (aPos.X() < 0) aPos.X() = 0;
109 : }
110 0 : if (aPos.Y() < 0) aPos.Y() = 0;
111 : }
112 :
113 0 : ScDrawView* pScDrawView = GetScDrawView();
114 0 : if (bGroup)
115 0 : pScDrawView->BegUndo( ScGlobal::GetRscString( STR_UNDO_PASTE ) );
116 :
117 0 : bool bSameDoc = ( pDragEditView && pDragEditView->GetModel() == pScDrawView->GetModel() );
118 0 : if (bSameDoc)
119 : {
120 : // copy locally - incl. charts
121 :
122 0 : Point aSourceStart = pDragEditView->GetAllMarkedRect().TopLeft();
123 0 : long nDiffX = aPos.X() - aSourceStart.X();
124 0 : long nDiffY = aPos.Y() - aSourceStart.Y();
125 :
126 : // move within a page?
127 :
128 0 : if ( bPasteIsMove &&
129 0 : pScDrawView->GetSdrPageView()->GetPage() ==
130 0 : pDragEditView->GetSdrPageView()->GetPage() )
131 : {
132 0 : if ( nDiffX != 0 || nDiffY != 0 )
133 0 : pDragEditView->MoveAllMarked(Size(nDiffX,nDiffY), false);
134 : }
135 : else
136 : {
137 0 : SdrModel* pDrawModel = pDragEditView->GetModel();
138 0 : SCTAB nTab = GetViewData()->GetTabNo();
139 0 : SdrPage* pDestPage = pDrawModel->GetPage( static_cast< sal_uInt16 >( nTab ) );
140 : OSL_ENSURE(pDestPage,"who is this, Page?");
141 :
142 0 : ::std::vector< OUString > aExcludedChartNames;
143 0 : if ( pDestPage )
144 : {
145 0 : ScChartHelper::GetChartNames( aExcludedChartNames, pDestPage );
146 : }
147 :
148 0 : SdrMarkList aMark = pDragEditView->GetMarkedObjectList();
149 0 : aMark.ForceSort();
150 0 : sal_uLong nMarkAnz=aMark.GetMarkCount();
151 0 : for (sal_uLong nm=0; nm<nMarkAnz; nm++) {
152 0 : const SdrMark* pM=aMark.GetMark(nm);
153 0 : const SdrObject* pObj=pM->GetMarkedSdrObj();
154 :
155 0 : SdrObject* pNeuObj=pObj->Clone();
156 :
157 0 : if (pNeuObj!=NULL)
158 : {
159 0 : pNeuObj->SetModel(pDrawModel);
160 0 : pNeuObj->SetPage(pDestPage);
161 :
162 : // copy graphics within the same model - always needs new name
163 0 : if ( pNeuObj->ISA(SdrGrafObj) && !bPasteIsMove )
164 0 : pNeuObj->SetName(((ScDrawLayer*)pDrawModel)->GetNewGraphicName());
165 :
166 0 : if (nDiffX!=0 || nDiffY!=0)
167 0 : pNeuObj->NbcMove(Size(nDiffX,nDiffY));
168 0 : if (pDestPage)
169 0 : pDestPage->InsertObject( pNeuObj );
170 0 : pScDrawView->AddUndo(new SdrUndoInsertObj( *pNeuObj ));
171 :
172 0 : if (ScDrawLayer::IsCellAnchored(*pNeuObj))
173 0 : ScDrawLayer::SetCellAnchoredFromPosition(*pNeuObj, *GetViewData()->GetDocument(), nTab);
174 : }
175 : }
176 :
177 0 : if (bPasteIsMove)
178 0 : pDragEditView->DeleteMarked();
179 :
180 0 : ScDocument* pDocument = GetViewData()->GetDocument();
181 0 : ScDocShell* pDocShell = GetViewData()->GetDocShell();
182 0 : ScModelObj* pModelObj = ( pDocShell ? ScModelObj::getImplementation( pDocShell->GetModel() ) : NULL );
183 0 : if ( pDocument && pDestPage && pModelObj && pDrawTrans )
184 : {
185 0 : const ScRangeListVector& rProtectedChartRangesVector( pDrawTrans->GetProtectedChartRangesVector() );
186 : ScChartHelper::CreateProtectedChartListenersAndNotify( pDocument, pDestPage, pModelObj, nTab,
187 0 : rProtectedChartRangesVector, aExcludedChartNames, bSameDoc );
188 0 : }
189 : }
190 : }
191 : else
192 : {
193 0 : bPasteIsMove = false; // no internal move happened
194 :
195 0 : SdrView aView(pModel); // #i71529# never create a base class of SdrView directly!
196 0 : SdrPageView* pPv = aView.ShowSdrPage(aView.GetModel()->GetPage(0));
197 0 : aView.MarkAllObj(pPv);
198 0 : Size aSize = aView.GetAllMarkedRect().GetSize();
199 0 : lcl_AdjustInsertPos( GetViewData(), aPos, aSize );
200 :
201 : // don't change marking if OLE object is active
202 : // (at Drop from OLE object it would be deactivated in the middle of ExecuteDrag!)
203 :
204 0 : sal_uLong nOptions = 0;
205 0 : SfxInPlaceClient* pClient = GetViewData()->GetViewShell()->GetIPClient();
206 0 : if ( pClient && pClient->IsObjectInPlaceActive() )
207 0 : nOptions |= SDRINSERT_DONTMARK;
208 :
209 0 : ::std::vector< OUString > aExcludedChartNames;
210 0 : SCTAB nTab = GetViewData()->GetTabNo();
211 0 : SdrPage* pPage = pScDrawView->GetModel()->GetPage( static_cast< sal_uInt16 >( nTab ) );
212 : OSL_ENSURE( pPage, "Page?" );
213 0 : if ( pPage )
214 : {
215 0 : ScChartHelper::GetChartNames( aExcludedChartNames, pPage );
216 : }
217 :
218 : // #89247# Set flag for ScDocument::UpdateChartListeners() which is
219 : // called during paste.
220 0 : if ( !bSameDocClipboard )
221 0 : GetViewData()->GetDocument()->SetPastingDrawFromOtherDoc( true );
222 :
223 0 : pScDrawView->Paste( *pModel, aPos, NULL, nOptions );
224 :
225 0 : if ( !bSameDocClipboard )
226 0 : GetViewData()->GetDocument()->SetPastingDrawFromOtherDoc( false );
227 :
228 : // Paste puts all objects on the active (front) layer
229 : // controls must be on SC_LAYER_CONTROLS
230 0 : if (pPage)
231 : {
232 0 : SdrObjListIter aIter( *pPage, IM_DEEPNOGROUPS );
233 0 : SdrObject* pObject = aIter.Next();
234 0 : while (pObject)
235 : {
236 0 : if ( pObject->ISA(SdrUnoObj) && pObject->GetLayer() != SC_LAYER_CONTROLS )
237 0 : pObject->NbcSetLayer(SC_LAYER_CONTROLS);
238 :
239 0 : if (ScDrawLayer::IsCellAnchored(*pObject))
240 0 : ScDrawLayer::SetCellAnchoredFromPosition(*pObject, *GetViewData()->GetDocument(), nTab);
241 :
242 0 : pObject = aIter.Next();
243 0 : }
244 : }
245 :
246 : // all graphics objects must have names
247 0 : GetViewData()->GetDocument()->EnsureGraphicNames();
248 :
249 0 : ScDocument* pDocument = GetViewData()->GetDocument();
250 0 : ScDocShell* pDocShell = GetViewData()->GetDocShell();
251 0 : ScModelObj* pModelObj = ( pDocShell ? ScModelObj::getImplementation( pDocShell->GetModel() ) : NULL );
252 0 : ScDrawTransferObj* pTransferObj = ScDrawTransferObj::GetOwnClipboard( NULL );
253 0 : if ( pDocument && pPage && pModelObj && ( pTransferObj || pDrawTrans ) )
254 : {
255 : const ScRangeListVector& rProtectedChartRangesVector(
256 0 : pTransferObj ? pTransferObj->GetProtectedChartRangesVector() : pDrawTrans->GetProtectedChartRangesVector() );
257 : ScChartHelper::CreateProtectedChartListenersAndNotify( pDocument, pPage, pModelObj, nTab,
258 0 : rProtectedChartRangesVector, aExcludedChartNames, bSameDocClipboard );
259 0 : }
260 : }
261 :
262 0 : if (bGroup)
263 : {
264 0 : pScDrawView->GroupMarked();
265 0 : pScDrawView->EndUndo();
266 : }
267 :
268 0 : if (pRef)
269 0 : pRef->SetMapMode( aOldMapMode );
270 :
271 : // GetViewData()->GetViewShell()->SetDrawShell( true );
272 : // It is not sufficient to just set the DrawShell if we pasted, for
273 : // example, a chart. SetDrawShellOrSub() would only work for D&D in the
274 : // same document but not if inserting from the clipboard, therefore
275 : // MarkListHasChanged() is what we need.
276 0 : pScDrawView->MarkListHasChanged();
277 :
278 0 : }
279 :
280 0 : bool ScViewFunc::PasteObject( const Point& rPos, const uno::Reference < embed::XEmbeddedObject >& xObj,
281 : const Size* pDescSize, const Graphic* pReplGraph, const OUString& aMediaType, sal_Int64 nAspect )
282 : {
283 0 : MakeDrawLayer();
284 0 : if ( xObj.is() )
285 : {
286 0 : OUString aName;
287 : //TODO/MBA: is that OK?
288 0 : comphelper::EmbeddedObjectContainer& aCnt = GetViewData()->GetViewShell()->GetObjectShell()->GetEmbeddedObjectContainer();
289 0 : if ( !aCnt.HasEmbeddedObject( xObj ) )
290 0 : aCnt.InsertEmbeddedObject( xObj, aName );
291 : else
292 0 : aName = aCnt.GetEmbeddedObjectName( xObj );
293 :
294 0 : svt::EmbeddedObjectRef aObjRef( xObj, nAspect );
295 0 : if ( pReplGraph )
296 0 : aObjRef.SetGraphic( *pReplGraph, aMediaType );
297 :
298 0 : Size aSize;
299 0 : if ( nAspect == embed::Aspects::MSOLE_ICON )
300 : {
301 0 : MapMode aMapMode( MAP_100TH_MM );
302 0 : aSize = aObjRef.GetSize( &aMapMode );
303 : }
304 : else
305 : {
306 : // working with visual area can switch object to running state
307 0 : MapUnit aMapObj = VCLUnoHelper::UnoEmbed2VCLMapUnit( xObj->getMapUnit( nAspect ) );
308 0 : MapUnit aMap100 = MAP_100TH_MM;
309 :
310 0 : if ( pDescSize && pDescSize->Width() && pDescSize->Height() )
311 : {
312 : // use size from object descriptor if given
313 0 : aSize = OutputDevice::LogicToLogic( *pDescSize, aMap100, aMapObj );
314 0 : awt::Size aSz;
315 0 : aSz.Width = aSize.Width();
316 0 : aSz.Height = aSize.Height();
317 0 : xObj->setVisualAreaSize( nAspect, aSz );
318 : }
319 :
320 0 : awt::Size aSz;
321 : try
322 : {
323 0 : aSz = xObj->getVisualAreaSize( nAspect );
324 : }
325 0 : catch ( embed::NoVisualAreaSizeException& )
326 : {
327 : // the default size will be set later
328 : }
329 :
330 0 : aSize = Size( aSz.Width, aSz.Height );
331 0 : aSize = OutputDevice::LogicToLogic( aSize, aMapObj, aMap100 ); // for SdrOle2Obj
332 :
333 0 : if( aSize.Height() == 0 || aSize.Width() == 0 )
334 : {
335 : OSL_FAIL("SvObjectDescriptor::GetSize == 0");
336 0 : aSize.Width() = 5000;
337 0 : aSize.Height() = 5000;
338 0 : aSize = OutputDevice::LogicToLogic( aSize, aMap100, aMapObj );
339 0 : aSz.Width = aSize.Width();
340 0 : aSz.Height = aSize.Height();
341 0 : xObj->setVisualAreaSize( nAspect, aSz );
342 : }
343 : }
344 :
345 : // don't call AdjustInsertPos
346 0 : Point aInsPos = rPos;
347 0 : if ( GetViewData()->GetDocument()->IsNegativePage( GetViewData()->GetTabNo() ) )
348 0 : aInsPos.X() -= aSize.Width();
349 0 : Rectangle aRect( aInsPos, aSize );
350 :
351 0 : ScDrawView* pDrView = GetScDrawView();
352 0 : SdrOle2Obj* pSdrObj = new SdrOle2Obj( aObjRef, aName, aRect );
353 :
354 0 : SdrPageView* pPV = pDrView->GetSdrPageView();
355 0 : pDrView->InsertObjectSafe( pSdrObj, *pPV ); // don't mark if OLE
356 0 : GetViewData()->GetViewShell()->SetDrawShell( true );
357 0 : return true;
358 : }
359 : else
360 0 : return false;
361 : }
362 :
363 0 : bool ScViewFunc::PasteBitmapEx( const Point& rPos, const BitmapEx& rBmpEx )
364 : {
365 0 : OUString aEmpty;
366 0 : Graphic aGraphic(rBmpEx);
367 0 : return PasteGraphic( rPos, aGraphic, aEmpty, aEmpty );
368 : }
369 :
370 0 : bool ScViewFunc::PasteMetaFile( const Point& rPos, const GDIMetaFile& rMtf )
371 : {
372 0 : OUString aEmpty;
373 0 : Graphic aGraphic(rMtf);
374 0 : return PasteGraphic( rPos, aGraphic, aEmpty, aEmpty );
375 : }
376 :
377 0 : bool ScViewFunc::PasteGraphic( const Point& rPos, const Graphic& rGraphic,
378 : const OUString& rFile, const OUString& rFilter )
379 : {
380 0 : MakeDrawLayer();
381 0 : ScDrawView* pScDrawView = GetScDrawView();
382 :
383 0 : if (!pScDrawView)
384 0 : return false;
385 :
386 : // #i123922# check if the drop was over an existing object; if yes, evtl. replace
387 : // the graphic for a SdrGraphObj (including link state updates) or adapt the fill
388 : // style for other objects
389 0 : SdrPageView* pPageView = pScDrawView->GetSdrPageView();
390 0 : if (pPageView)
391 : {
392 0 : SdrObject* pPickObj = 0;
393 0 : if (pScDrawView->PickObj(rPos, pScDrawView->getHitTolLog(), pPickObj, pPageView))
394 : {
395 0 : const OUString aBeginUndo(ScGlobal::GetRscString(STR_UNDO_DRAGDROP));
396 : SdrObject* pResult = pScDrawView->ApplyGraphicToObject(
397 : *pPickObj,
398 : rGraphic,
399 : aBeginUndo,
400 : rFile,
401 0 : rFilter);
402 :
403 0 : if (pResult)
404 : {
405 : // we are done; mark the modified/new object
406 0 : pScDrawView->MarkObj(pResult, pScDrawView->GetSdrPageView());
407 0 : return true;
408 0 : }
409 : }
410 : }
411 :
412 0 : Point aPos( rPos );
413 0 : Window* pWin = GetActiveWin();
414 0 : MapMode aSourceMap = rGraphic.GetPrefMapMode();
415 0 : MapMode aDestMap( MAP_100TH_MM );
416 :
417 0 : if (aSourceMap.GetMapUnit() == MAP_PIXEL)
418 : {
419 : // consider pixel correction, so bitmap fits to screen
420 0 : Fraction aScaleX, aScaleY;
421 0 : pScDrawView->CalcNormScale( aScaleX, aScaleY );
422 0 : aDestMap.SetScaleX(aScaleX);
423 0 : aDestMap.SetScaleY(aScaleY);
424 : }
425 :
426 0 : Size aSize = pWin->LogicToLogic( rGraphic.GetPrefSize(), &aSourceMap, &aDestMap );
427 :
428 0 : if ( GetViewData()->GetDocument()->IsNegativePage( GetViewData()->GetTabNo() ) )
429 0 : aPos.X() -= aSize.Width();
430 :
431 0 : GetViewData()->GetViewShell()->SetDrawShell( true );
432 0 : Rectangle aRect(aPos, aSize);
433 0 : SdrGrafObj* pGrafObj = new SdrGrafObj(rGraphic, aRect);
434 :
435 : // path was the name of the graphic in history
436 :
437 0 : ScDrawLayer* pLayer = (ScDrawLayer*) pScDrawView->GetModel();
438 0 : OUString aName = pLayer->GetNewGraphicName(); // "Graphics"
439 0 : pGrafObj->SetName(aName);
440 :
441 : // don't mark if OLE
442 0 : pScDrawView->InsertObjectSafe(pGrafObj, *pScDrawView->GetSdrPageView());
443 :
444 : // SetGraphicLink has to be used after inserting the object,
445 : // otherwise an empty graphic is swapped in and the contact stuff crashes.
446 : // See #i37444#.
447 0 : if (!rFile.isEmpty())
448 0 : pGrafObj->SetGraphicLink( rFile, ""/*TODO?*/, rFilter );
449 :
450 0 : return true;
451 0 : }
452 :
453 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|