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