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>
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 <svtools/embedhlp.hxx>
32 #include <sfx2/objsh.hxx>
33 #include <sfx2/viewfrm.hxx>
34 #include <toolkit/helper/vclunohelper.hxx>
35 #include <com/sun/star/embed/Aspects.hpp>
37 #include "document.hxx"
38 #include "viewfunc.hxx"
39 #include "tabvwsh.hxx"
40 #include "drawview.hxx"
42 #include "drwlayer.hxx"
43 #include "drwtrans.hxx"
44 #include "globstr.hrc"
45 #include "chartlis.hxx"
48 #include "convuno.hxx"
49 #include "dragdata.hxx"
50 #include <gridwin.hxx>
52 extern Point aDragStartDiff
;
54 bool bPasteIsMove
= false;
56 using namespace com::sun::star
;
58 static void lcl_AdjustInsertPos( ScViewData
* pData
, Point
& rPos
, Size
& rSize
)
60 SdrPage
* pPage
= pData
->GetScDrawView()->GetModel()->GetPage( static_cast<sal_uInt16
>(pData
->GetTabNo()) );
61 OSL_ENSURE(pPage
,"pPage ???");
62 Size
aPgSize( pPage
->GetSize() );
63 if (aPgSize
.Width() < 0)
64 aPgSize
.Width() = -aPgSize
.Width();
65 long x
= aPgSize
.Width() - rPos
.X() - rSize
.Width();
66 long y
= aPgSize
.Height() - rPos
.Y() - rSize
.Height();
67 // if necessary: adjustments (80/200) for pixel approx. errors
72 rPos
.X() += rSize
.Width() / 2; // position at paste is center
73 rPos
.Y() += rSize
.Height() / 2;
76 void ScViewFunc::PasteDraw( const Point
& rLogicPos
, SdrModel
* pModel
,
77 bool bGroup
, const OUString
& rSrcShellID
, const OUString
& rDestShellID
)
79 bool bSameDocClipboard
= rSrcShellID
== rDestShellID
;
82 Point
aPos( rLogicPos
);
84 // MapMode at Outliner-RefDevice has to be right (as in FuText::MakeOutliner)
85 //! merge with FuText::MakeOutliner?
87 OutputDevice
* pRef
= GetViewData().GetDocument()->GetDrawLayer()->GetRefDevice();
90 aOldMapMode
= pRef
->GetMapMode();
91 pRef
->SetMapMode( MapMode(MAP_100TH_MM
) );
94 bool bNegativePage
= GetViewData().GetDocument()->IsNegativePage( GetViewData().GetTabNo() );
96 SdrView
* pDragEditView
= NULL
;
97 ScModule
* pScMod
= SC_MOD();
98 const ScDragData
& rData
= pScMod
->GetDragData();
99 ScDrawTransferObj
* pDrawTrans
= rData
.pDrawTransfer
;
102 pDragEditView
= pDrawTrans
->GetDragSourceView();
104 aPos
-= aDragStartDiff
;
107 if (aPos
.X() > 0) aPos
.X() = 0;
111 if (aPos
.X() < 0) aPos
.X() = 0;
113 if (aPos
.Y() < 0) aPos
.Y() = 0;
116 ScDrawView
* pScDrawView
= GetScDrawView();
118 pScDrawView
->BegUndo( ScGlobal::GetRscString( STR_UNDO_PASTE
) );
120 bool bSameDoc
= ( pDragEditView
&& pDragEditView
->GetModel() == pScDrawView
->GetModel() );
123 // copy locally - incl. charts
125 Point aSourceStart
= pDragEditView
->GetAllMarkedRect().TopLeft();
126 long nDiffX
= aPos
.X() - aSourceStart
.X();
127 long nDiffY
= aPos
.Y() - aSourceStart
.Y();
129 // move within a page?
132 pScDrawView
->GetSdrPageView()->GetPage() ==
133 pDragEditView
->GetSdrPageView()->GetPage() )
135 if ( nDiffX
!= 0 || nDiffY
!= 0 )
136 pDragEditView
->MoveAllMarked(Size(nDiffX
,nDiffY
), false);
140 SdrModel
* pDrawModel
= pDragEditView
->GetModel();
141 SCTAB nTab
= GetViewData().GetTabNo();
142 SdrPage
* pDestPage
= pDrawModel
->GetPage( static_cast< sal_uInt16
>( nTab
) );
143 OSL_ENSURE(pDestPage
,"who is this, Page?");
145 ::std::vector
< OUString
> aExcludedChartNames
;
148 ScChartHelper::GetChartNames( aExcludedChartNames
, pDestPage
);
151 SdrMarkList aMark
= pDragEditView
->GetMarkedObjectList();
153 const size_t nMarkAnz
=aMark
.GetMarkCount();
154 for (size_t nm
=0; nm
<nMarkAnz
; ++nm
) {
155 const SdrMark
* pM
=aMark
.GetMark(nm
);
156 const SdrObject
* pObj
=pM
->GetMarkedSdrObj();
158 SdrObject
* pNeuObj
=pObj
->Clone();
162 pNeuObj
->SetModel(pDrawModel
);
163 pNeuObj
->SetPage(pDestPage
);
165 // copy graphics within the same model - always needs new name
166 if ( pNeuObj
->ISA(SdrGrafObj
) && !bPasteIsMove
)
167 pNeuObj
->SetName(static_cast<ScDrawLayer
*>(pDrawModel
)->GetNewGraphicName());
169 if (nDiffX
!=0 || nDiffY
!=0)
170 pNeuObj
->NbcMove(Size(nDiffX
,nDiffY
));
172 pDestPage
->InsertObject( pNeuObj
);
173 pScDrawView
->AddUndo(new SdrUndoInsertObj( *pNeuObj
));
175 if (ScDrawLayer::IsCellAnchored(*pNeuObj
))
176 ScDrawLayer::SetCellAnchoredFromPosition(*pNeuObj
, *GetViewData().GetDocument(), nTab
);
181 pDragEditView
->DeleteMarked();
183 ScDocument
* pDocument
= GetViewData().GetDocument();
184 ScDocShell
* pDocShell
= GetViewData().GetDocShell();
185 ScModelObj
* pModelObj
= ( pDocShell
? ScModelObj::getImplementation( pDocShell
->GetModel() ) : NULL
);
186 if ( pDocument
&& pDestPage
&& pModelObj
&& pDrawTrans
)
188 const ScRangeListVector
& rProtectedChartRangesVector( pDrawTrans
->GetProtectedChartRangesVector() );
189 ScChartHelper::CreateProtectedChartListenersAndNotify( pDocument
, pDestPage
, pModelObj
, nTab
,
190 rProtectedChartRangesVector
, aExcludedChartNames
, bSameDoc
);
196 bPasteIsMove
= false; // no internal move happened
198 SdrView
aView(pModel
); // #i71529# never create a base class of SdrView directly!
199 SdrPageView
* pPv
= aView
.ShowSdrPage(aView
.GetModel()->GetPage(0));
200 aView
.MarkAllObj(pPv
);
201 Size aSize
= aView
.GetAllMarkedRect().GetSize();
202 lcl_AdjustInsertPos( &GetViewData(), aPos
, aSize
);
204 // don't change marking if OLE object is active
205 // (at Drop from OLE object it would be deactivated in the middle of ExecuteDrag!)
207 SdrInsertFlags nOptions
= SdrInsertFlags::NONE
;
208 SfxInPlaceClient
* pClient
= GetViewData().GetViewShell()->GetIPClient();
209 if ( pClient
&& pClient
->IsObjectInPlaceActive() )
210 nOptions
|= SdrInsertFlags::DONTMARK
;
212 ::std::vector
< OUString
> aExcludedChartNames
;
213 SCTAB nTab
= GetViewData().GetTabNo();
214 SdrPage
* pPage
= pScDrawView
->GetModel()->GetPage( static_cast< sal_uInt16
>( nTab
) );
215 OSL_ENSURE( pPage
, "Page?" );
218 ScChartHelper::GetChartNames( aExcludedChartNames
, pPage
);
221 // #89247# Set flag for ScDocument::UpdateChartListeners() which is
222 // called during paste.
223 if ( !bSameDocClipboard
)
224 GetViewData().GetDocument()->SetPastingDrawFromOtherDoc( true );
226 pScDrawView
->Paste(*pModel
, aPos
, NULL
, nOptions
, rSrcShellID
, rDestShellID
);
228 if ( !bSameDocClipboard
)
229 GetViewData().GetDocument()->SetPastingDrawFromOtherDoc( false );
231 // Paste puts all objects on the active (front) layer
232 // controls must be on SC_LAYER_CONTROLS
235 SdrObjListIter
aIter( *pPage
, IM_DEEPNOGROUPS
);
236 SdrObject
* pObject
= aIter
.Next();
239 if ( pObject
->ISA(SdrUnoObj
) && pObject
->GetLayer() != SC_LAYER_CONTROLS
)
240 pObject
->NbcSetLayer(SC_LAYER_CONTROLS
);
242 if (ScDrawLayer::IsCellAnchored(*pObject
))
243 ScDrawLayer::SetCellAnchoredFromPosition(*pObject
, *GetViewData().GetDocument(), nTab
);
245 pObject
= aIter
.Next();
249 // all graphics objects must have names
250 GetViewData().GetDocument()->EnsureGraphicNames();
252 ScDocument
* pDocument
= GetViewData().GetDocument();
253 ScDocShell
* pDocShell
= GetViewData().GetDocShell();
254 ScModelObj
* pModelObj
= ( pDocShell
? ScModelObj::getImplementation( pDocShell
->GetModel() ) : NULL
);
255 ScDrawTransferObj
* pTransferObj
= ScDrawTransferObj::GetOwnClipboard( NULL
);
256 if ( pDocument
&& pPage
&& pModelObj
&& ( pTransferObj
|| pDrawTrans
) )
258 const ScRangeListVector
& rProtectedChartRangesVector(
259 pTransferObj
? pTransferObj
->GetProtectedChartRangesVector() : pDrawTrans
->GetProtectedChartRangesVector() );
260 ScChartHelper::CreateProtectedChartListenersAndNotify( pDocument
, pPage
, pModelObj
, nTab
,
261 rProtectedChartRangesVector
, aExcludedChartNames
, bSameDocClipboard
);
267 pScDrawView
->GroupMarked();
268 pScDrawView
->EndUndo();
272 pRef
->SetMapMode( aOldMapMode
);
274 // GetViewData().GetViewShell()->SetDrawShell( true );
275 // It is not sufficient to just set the DrawShell if we pasted, for
276 // example, a chart. SetDrawShellOrSub() would only work for D&D in the
277 // same document but not if inserting from the clipboard, therefore
278 // MarkListHasChanged() is what we need.
279 pScDrawView
->MarkListHasChanged();
283 bool ScViewFunc::PasteObject( const Point
& rPos
, const uno::Reference
< embed::XEmbeddedObject
>& xObj
,
284 const Size
* pDescSize
, const Graphic
* pReplGraph
, const OUString
& aMediaType
, sal_Int64 nAspect
)
290 //TODO/MBA: is that OK?
291 comphelper::EmbeddedObjectContainer
& aCnt
= GetViewData().GetViewShell()->GetObjectShell()->GetEmbeddedObjectContainer();
292 if ( !aCnt
.HasEmbeddedObject( xObj
) )
293 aCnt
.InsertEmbeddedObject( xObj
, aName
);
295 aName
= aCnt
.GetEmbeddedObjectName( xObj
);
297 svt::EmbeddedObjectRef
aObjRef( xObj
, nAspect
);
299 aObjRef
.SetGraphic( *pReplGraph
, aMediaType
);
302 if ( nAspect
== embed::Aspects::MSOLE_ICON
)
304 MapMode
aMapMode( MAP_100TH_MM
);
305 aSize
= aObjRef
.GetSize( &aMapMode
);
309 // working with visual area can switch object to running state
310 MapUnit aMapObj
= VCLUnoHelper::UnoEmbed2VCLMapUnit( xObj
->getMapUnit( nAspect
) );
311 MapUnit aMap100
= MAP_100TH_MM
;
313 if ( pDescSize
&& pDescSize
->Width() && pDescSize
->Height() )
315 // use size from object descriptor if given
316 aSize
= OutputDevice::LogicToLogic( *pDescSize
, aMap100
, aMapObj
);
318 aSz
.Width
= aSize
.Width();
319 aSz
.Height
= aSize
.Height();
320 xObj
->setVisualAreaSize( nAspect
, aSz
);
326 aSz
= xObj
->getVisualAreaSize( nAspect
);
328 catch ( embed::NoVisualAreaSizeException
& )
330 // the default size will be set later
333 aSize
= Size( aSz
.Width
, aSz
.Height
);
334 aSize
= OutputDevice::LogicToLogic( aSize
, aMapObj
, aMap100
); // for SdrOle2Obj
336 if( aSize
.Height() == 0 || aSize
.Width() == 0 )
338 OSL_FAIL("SvObjectDescriptor::GetSize == 0");
339 aSize
.Width() = 5000;
340 aSize
.Height() = 5000;
341 aSize
= OutputDevice::LogicToLogic( aSize
, aMap100
, aMapObj
);
342 aSz
.Width
= aSize
.Width();
343 aSz
.Height
= aSize
.Height();
344 xObj
->setVisualAreaSize( nAspect
, aSz
);
348 // don't call AdjustInsertPos
349 Point aInsPos
= rPos
;
350 if ( GetViewData().GetDocument()->IsNegativePage( GetViewData().GetTabNo() ) )
351 aInsPos
.X() -= aSize
.Width();
352 Rectangle
aRect( aInsPos
, aSize
);
354 ScDrawView
* pDrView
= GetScDrawView();
355 SdrOle2Obj
* pSdrObj
= new SdrOle2Obj( aObjRef
, aName
, aRect
);
357 SdrPageView
* pPV
= pDrView
->GetSdrPageView();
358 pDrView
->InsertObjectSafe( pSdrObj
, *pPV
); // don't mark if OLE
359 GetViewData().GetViewShell()->SetDrawShell( true );
366 bool ScViewFunc::PasteBitmapEx( const Point
& rPos
, const BitmapEx
& rBmpEx
)
369 Graphic
aGraphic(rBmpEx
);
370 return PasteGraphic( rPos
, aGraphic
, aEmpty
, aEmpty
);
373 bool ScViewFunc::PasteMetaFile( const Point
& rPos
, const GDIMetaFile
& rMtf
)
376 Graphic
aGraphic(rMtf
);
377 return PasteGraphic( rPos
, aGraphic
, aEmpty
, aEmpty
);
380 bool ScViewFunc::PasteGraphic( const Point
& rPos
, const Graphic
& rGraphic
,
381 const OUString
& rFile
, const OUString
& rFilter
)
384 ScDrawView
* pScDrawView
= GetScDrawView();
389 // #i123922# check if the drop was over an existing object; if yes, evtl. replace
390 // the graphic for a SdrGraphObj (including link state updates) or adapt the fill
391 // style for other objects
392 SdrPageView
* pPageView
= pScDrawView
->GetSdrPageView();
395 SdrObject
* pPickObj
= 0;
396 if (pScDrawView
->PickObj(rPos
, pScDrawView
->getHitTolLog(), pPickObj
, pPageView
))
398 const OUString
aBeginUndo(ScGlobal::GetRscString(STR_UNDO_DRAGDROP
));
399 SdrObject
* pResult
= pScDrawView
->ApplyGraphicToObject(
408 // we are done; mark the modified/new object
409 pScDrawView
->MarkObj(pResult
, pScDrawView
->GetSdrPageView());
416 vcl::Window
* pWin
= GetActiveWin();
417 MapMode aSourceMap
= rGraphic
.GetPrefMapMode();
418 MapMode
aDestMap( MAP_100TH_MM
);
420 if (aSourceMap
.GetMapUnit() == MAP_PIXEL
)
422 // consider pixel correction, so bitmap fits to screen
423 Fraction aScaleX
, aScaleY
;
424 pScDrawView
->CalcNormScale( aScaleX
, aScaleY
);
425 aDestMap
.SetScaleX(aScaleX
);
426 aDestMap
.SetScaleY(aScaleY
);
429 Size aSize
= pWin
->LogicToLogic( rGraphic
.GetPrefSize(), &aSourceMap
, &aDestMap
);
431 if ( GetViewData().GetDocument()->IsNegativePage( GetViewData().GetTabNo() ) )
432 aPos
.X() -= aSize
.Width();
434 GetViewData().GetViewShell()->SetDrawShell( true );
435 Rectangle
aRect(aPos
, aSize
);
436 SdrGrafObj
* pGrafObj
= new SdrGrafObj(rGraphic
, aRect
);
438 // path was the name of the graphic in history
440 ScDrawLayer
* pLayer
= static_cast<ScDrawLayer
*>( pScDrawView
->GetModel() );
441 OUString aName
= pLayer
->GetNewGraphicName(); // "Graphics"
442 pGrafObj
->SetName(aName
);
445 pScDrawView
->InsertObjectSafe(pGrafObj
, *pScDrawView
->GetSdrPageView());
447 // SetGraphicLink has to be used after inserting the object,
448 // otherwise an empty graphic is swapped in and the contact stuff crashes.
450 if (!rFile
.isEmpty())
451 pGrafObj
->SetGraphicLink( rFile
, ""/*TODO?*/, rFilter
);
456 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */