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/xbitmap.hxx>
30 : #include <svx/xbtmpit.hxx>
31 : #include <svx/xoutbmp.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" // fuer MapMode Initialisierung in PasteDraw
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 :
51 : extern Point aDragStartDiff;
52 :
53 : // STATIC DATA -----------------------------------------------------------
54 :
55 : sal_Bool bPasteIsMove = false;
56 :
57 : using namespace com::sun::star;
58 :
59 : //==================================================================
60 :
61 0 : static void lcl_AdjustInsertPos( ScViewData* pData, Point& rPos, Size& rSize )
62 : {
63 0 : SdrPage* pPage = pData->GetScDrawView()->GetModel()->GetPage( static_cast<sal_uInt16>(pData->GetTabNo()) );
64 : OSL_ENSURE(pPage,"pPage ???");
65 0 : Size aPgSize( pPage->GetSize() );
66 0 : if (aPgSize.Width() < 0)
67 0 : aPgSize.Width() = -aPgSize.Width();
68 0 : long x = aPgSize.Width() - rPos.X() - rSize.Width();
69 0 : long y = aPgSize.Height() - rPos.Y() - rSize.Height();
70 : // ggf. Ajustments (80/200) fuer Pixel-Rundungsfehler
71 0 : if( x < 0 )
72 0 : rPos.X() += x + 80;
73 0 : if( y < 0 )
74 0 : rPos.Y() += y + 200;
75 0 : rPos.X() += rSize.Width() / 2; // Position bei Paste gibt Mittelpunkt an
76 0 : rPos.Y() += rSize.Height() / 2;
77 0 : }
78 :
79 0 : void ScViewFunc::PasteDraw( const Point& rLogicPos, SdrModel* pModel,
80 : sal_Bool bGroup, sal_Bool bSameDocClipboard )
81 : {
82 0 : MakeDrawLayer();
83 0 : Point aPos( rLogicPos );
84 :
85 : // MapMode am Outliner-RefDevice muss stimmen (wie in FuText::MakeOutliner)
86 : //! mit FuText::MakeOutliner zusammenfassen?
87 0 : MapMode aOldMapMode;
88 0 : OutputDevice* pRef = GetViewData()->GetDocument()->GetDrawLayer()->GetRefDevice();
89 0 : if (pRef)
90 : {
91 0 : aOldMapMode = pRef->GetMapMode();
92 0 : pRef->SetMapMode( MapMode(MAP_100TH_MM) );
93 : }
94 :
95 0 : sal_Bool bNegativePage = GetViewData()->GetDocument()->IsNegativePage( GetViewData()->GetTabNo() );
96 :
97 0 : SdrView* pDragEditView = NULL;
98 0 : ScModule* pScMod = SC_MOD();
99 0 : const ScDragData& rData = pScMod->GetDragData();
100 0 : ScDrawTransferObj* pDrawTrans = rData.pDrawTransfer;
101 0 : if (pDrawTrans)
102 : {
103 0 : pDragEditView = pDrawTrans->GetDragSourceView();
104 :
105 0 : aPos -= aDragStartDiff;
106 0 : if ( bNegativePage )
107 : {
108 0 : if (aPos.X() > 0) aPos.X() = 0;
109 : }
110 : else
111 : {
112 0 : if (aPos.X() < 0) aPos.X() = 0;
113 : }
114 0 : if (aPos.Y() < 0) aPos.Y() = 0;
115 : }
116 :
117 0 : ScDrawView* pScDrawView = GetScDrawView();
118 0 : if (bGroup)
119 0 : pScDrawView->BegUndo( ScGlobal::GetRscString( STR_UNDO_PASTE ) );
120 :
121 0 : sal_Bool bSameDoc = ( pDragEditView && pDragEditView->GetModel() == pScDrawView->GetModel() );
122 0 : if (bSameDoc)
123 : {
124 : // lokal kopieren - incl. Charts
125 :
126 0 : Point aSourceStart = pDragEditView->GetAllMarkedRect().TopLeft();
127 0 : long nDiffX = aPos.X() - aSourceStart.X();
128 0 : long nDiffY = aPos.Y() - aSourceStart.Y();
129 :
130 : // innerhalb einer Page verschieben?
131 :
132 0 : if ( bPasteIsMove &&
133 0 : pScDrawView->GetSdrPageView()->GetPage() ==
134 0 : pDragEditView->GetSdrPageView()->GetPage() )
135 : {
136 0 : if ( nDiffX != 0 || nDiffY != 0 )
137 0 : pDragEditView->MoveAllMarked(Size(nDiffX,nDiffY), false);
138 : }
139 : else
140 : {
141 0 : SdrModel* pDrawModel = pDragEditView->GetModel();
142 0 : SCTAB nTab = GetViewData()->GetTabNo();
143 0 : SdrPage* pDestPage = pDrawModel->GetPage( static_cast< sal_uInt16 >( nTab ) );
144 : OSL_ENSURE(pDestPage,"nanu, Page?");
145 :
146 0 : ::std::vector< ::rtl::OUString > aExcludedChartNames;
147 0 : if ( pDestPage )
148 : {
149 0 : ScChartHelper::GetChartNames( aExcludedChartNames, pDestPage );
150 : }
151 :
152 0 : SdrMarkList aMark = pDragEditView->GetMarkedObjectList();
153 0 : aMark.ForceSort();
154 0 : sal_uLong nMarkAnz=aMark.GetMarkCount();
155 0 : for (sal_uLong nm=0; nm<nMarkAnz; nm++) {
156 0 : const SdrMark* pM=aMark.GetMark(nm);
157 0 : const SdrObject* pObj=pM->GetMarkedSdrObj();
158 :
159 0 : SdrObject* pNeuObj=pObj->Clone();
160 :
161 0 : if (pNeuObj!=NULL)
162 : {
163 0 : pNeuObj->SetModel(pDrawModel);
164 0 : pNeuObj->SetPage(pDestPage);
165 :
166 : // copy graphics within the same model - always needs new name
167 0 : if ( pNeuObj->ISA(SdrGrafObj) && !bPasteIsMove )
168 0 : pNeuObj->SetName(((ScDrawLayer*)pDrawModel)->GetNewGraphicName());
169 :
170 0 : if (nDiffX!=0 || nDiffY!=0)
171 0 : pNeuObj->NbcMove(Size(nDiffX,nDiffY));
172 0 : pDestPage->InsertObject( pNeuObj );
173 0 : pScDrawView->AddUndo(new SdrUndoInsertObj( *pNeuObj ));
174 :
175 : // Chart braucht nicht mehr getrennt behandelt zu werden,
176 : // weil es seine Daten jetzt selber hat
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; // kein internes Verschieben passiert
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 : // Markierung nicht aendern, wenn Ole-Objekt aktiv
205 : // (bei Drop aus Ole-Objekt wuerde sonst mitten im ExecuteDrag deaktiviert!)
206 :
207 0 : sal_uLong nOptions = 0;
208 0 : SfxInPlaceClient* pClient = GetViewData()->GetViewShell()->GetIPClient();
209 0 : if ( pClient && pClient->IsObjectInPlaceActive() )
210 0 : nOptions |= SDRINSERT_DONTMARK;
211 :
212 0 : ::std::vector< ::rtl::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( sal_True );
225 :
226 0 : pScDrawView->Paste( *pModel, aPos, NULL, nOptions );
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 0 : pObject = aIter.Next();
242 0 : }
243 : }
244 :
245 : // all graphics objects must have names
246 0 : GetViewData()->GetDocument()->EnsureGraphicNames();
247 :
248 0 : ScDocument* pDocument = GetViewData()->GetDocument();
249 0 : ScDocShell* pDocShell = GetViewData()->GetDocShell();
250 0 : ScModelObj* pModelObj = ( pDocShell ? ScModelObj::getImplementation( pDocShell->GetModel() ) : NULL );
251 0 : ScDrawTransferObj* pTransferObj = ScDrawTransferObj::GetOwnClipboard( NULL );
252 0 : if ( pDocument && pPage && pModelObj && ( pTransferObj || pDrawTrans ) )
253 : {
254 : const ScRangeListVector& rProtectedChartRangesVector(
255 0 : pTransferObj ? pTransferObj->GetProtectedChartRangesVector() : pDrawTrans->GetProtectedChartRangesVector() );
256 : ScChartHelper::CreateProtectedChartListenersAndNotify( pDocument, pPage, pModelObj, nTab,
257 0 : rProtectedChartRangesVector, aExcludedChartNames, bSameDocClipboard );
258 0 : }
259 : }
260 :
261 0 : if (bGroup)
262 : {
263 0 : pScDrawView->GroupMarked();
264 0 : pScDrawView->EndUndo();
265 : }
266 :
267 0 : if (pRef)
268 0 : pRef->SetMapMode( aOldMapMode );
269 :
270 : // GetViewData()->GetViewShell()->SetDrawShell( sal_True );
271 : // It is not sufficient to just set the DrawShell if we pasted, for
272 : // example, a chart. SetDrawShellOrSub() would only work for D&D in the
273 : // same document but not if inserting from the clipboard, therefore
274 : // MarkListHasChanged() is what we need.
275 0 : pScDrawView->MarkListHasChanged();
276 :
277 0 : }
278 :
279 0 : sal_Bool ScViewFunc::PasteObject( const Point& rPos, const uno::Reference < embed::XEmbeddedObject >& xObj,
280 : const Size* pDescSize, const Graphic* pReplGraph, const ::rtl::OUString& aMediaType, sal_Int64 nAspect )
281 : {
282 0 : MakeDrawLayer();
283 0 : if ( xObj.is() )
284 : {
285 0 : ::rtl::OUString aName;
286 : //TODO/MBA: is that OK?
287 0 : comphelper::EmbeddedObjectContainer& aCnt = GetViewData()->GetViewShell()->GetObjectShell()->GetEmbeddedObjectContainer();
288 0 : if ( !aCnt.HasEmbeddedObject( xObj ) )
289 0 : aCnt.InsertEmbeddedObject( xObj, aName );
290 : else
291 0 : aName = aCnt.GetEmbeddedObjectName( xObj );
292 :
293 0 : svt::EmbeddedObjectRef aObjRef( xObj, nAspect );
294 0 : if ( pReplGraph )
295 0 : aObjRef.SetGraphic( *pReplGraph, aMediaType );
296 :
297 0 : Size aSize;
298 0 : if ( nAspect == embed::Aspects::MSOLE_ICON )
299 : {
300 0 : MapMode aMapMode( MAP_100TH_MM );
301 0 : aSize = aObjRef.GetSize( &aMapMode );
302 : }
303 : else
304 : {
305 : // working with visual area can switch object to running state
306 0 : MapUnit aMapObj = VCLUnoHelper::UnoEmbed2VCLMapUnit( xObj->getMapUnit( nAspect ) );
307 0 : MapUnit aMap100 = MAP_100TH_MM;
308 :
309 0 : if ( pDescSize && pDescSize->Width() && pDescSize->Height() )
310 : {
311 : // use size from object descriptor if given
312 0 : aSize = OutputDevice::LogicToLogic( *pDescSize, aMap100, aMapObj );
313 0 : awt::Size aSz;
314 0 : aSz.Width = aSize.Width();
315 0 : aSz.Height = aSize.Height();
316 0 : xObj->setVisualAreaSize( nAspect, aSz );
317 : }
318 :
319 0 : awt::Size aSz;
320 : try
321 : {
322 0 : aSz = xObj->getVisualAreaSize( nAspect );
323 : }
324 0 : catch ( embed::NoVisualAreaSizeException& )
325 : {
326 : // the default size will be set later
327 : }
328 :
329 0 : aSize = Size( aSz.Width, aSz.Height );
330 0 : aSize = OutputDevice::LogicToLogic( aSize, aMapObj, aMap100 ); // fuer SdrOle2Obj
331 :
332 0 : if( aSize.Height() == 0 || aSize.Width() == 0 )
333 : {
334 : OSL_FAIL("SvObjectDescriptor::GetSize == 0");
335 0 : aSize.Width() = 5000;
336 0 : aSize.Height() = 5000;
337 0 : aSize = OutputDevice::LogicToLogic( aSize, aMap100, aMapObj );
338 0 : aSz.Width = aSize.Width();
339 0 : aSz.Height = aSize.Height();
340 0 : xObj->setVisualAreaSize( nAspect, aSz );
341 : }
342 : }
343 :
344 : // don't call AdjustInsertPos
345 0 : Point aInsPos = rPos;
346 0 : if ( GetViewData()->GetDocument()->IsNegativePage( GetViewData()->GetTabNo() ) )
347 0 : aInsPos.X() -= aSize.Width();
348 0 : Rectangle aRect( aInsPos, aSize );
349 :
350 0 : ScDrawView* pDrView = GetScDrawView();
351 0 : SdrOle2Obj* pSdrObj = new SdrOle2Obj( aObjRef, aName, aRect );
352 :
353 0 : SdrPageView* pPV = pDrView->GetSdrPageView();
354 0 : pDrView->InsertObjectSafe( pSdrObj, *pPV ); // nicht markieren wenn Ole
355 0 : GetViewData()->GetViewShell()->SetDrawShell( sal_True );
356 0 : return sal_True;
357 : }
358 : else
359 0 : return false;
360 : }
361 :
362 0 : sal_Bool ScViewFunc::PasteBitmap( const Point& rPos, const Bitmap& rBmp )
363 : {
364 0 : String aEmpty;
365 0 : Graphic aGraphic(rBmp);
366 0 : return PasteGraphic( rPos, aGraphic, aEmpty, aEmpty );
367 : }
368 :
369 0 : sal_Bool ScViewFunc::PasteMetaFile( const Point& rPos, const GDIMetaFile& rMtf )
370 : {
371 0 : String aEmpty;
372 0 : Graphic aGraphic(rMtf);
373 0 : return PasteGraphic( rPos, aGraphic, aEmpty, aEmpty );
374 : }
375 :
376 0 : sal_Bool ScViewFunc::PasteGraphic( const Point& rPos, const Graphic& rGraphic,
377 : const String& rFile, const String& rFilter )
378 : {
379 0 : MakeDrawLayer();
380 0 : ScDrawView* pScDrawView = GetScDrawView();
381 :
382 0 : Point aPos( rPos );
383 0 : Window* pWin = GetActiveWin();
384 0 : MapMode aSourceMap = rGraphic.GetPrefMapMode();
385 0 : MapMode aDestMap( MAP_100TH_MM );
386 :
387 0 : if (aSourceMap.GetMapUnit() == MAP_PIXEL)
388 : {
389 : // Pixel-Korrektur beruecksichtigen, damit Bitmap auf dem Bildschirm stimmt
390 :
391 0 : Fraction aScaleX, aScaleY;
392 0 : pScDrawView->CalcNormScale( aScaleX, aScaleY );
393 0 : aDestMap.SetScaleX(aScaleX);
394 0 : aDestMap.SetScaleY(aScaleY);
395 : }
396 :
397 0 : Size aSize = pWin->LogicToLogic( rGraphic.GetPrefSize(), &aSourceMap, &aDestMap );
398 :
399 0 : if ( GetViewData()->GetDocument()->IsNegativePage( GetViewData()->GetTabNo() ) )
400 0 : aPos.X() -= aSize.Width();
401 :
402 0 : GetViewData()->GetViewShell()->SetDrawShell( sal_True );
403 :
404 0 : Rectangle aRect(aPos, aSize);
405 0 : SdrGrafObj* pGrafObj = new SdrGrafObj(rGraphic, aRect);
406 :
407 : // calling SetGraphicLink here doesn't work
408 :
409 : // Pfad wird nicht mehr als Name der Grafik gesetzt
410 :
411 0 : ScDrawLayer* pLayer = (ScDrawLayer*) pScDrawView->GetModel();
412 0 : String aName = pLayer->GetNewGraphicName(); // "Grafik x"
413 0 : pGrafObj->SetName(aName);
414 :
415 : // nicht markieren wenn Ole
416 0 : pScDrawView->InsertObjectSafe(pGrafObj, *pScDrawView->GetSdrPageView());
417 :
418 : // SetGraphicLink has to be used after inserting the object,
419 : // otherwise an empty graphic is swapped in and the contact stuff crashes.
420 : // See #i37444#.
421 0 : if (rFile.Len())
422 0 : pGrafObj->SetGraphicLink( rFile, rFilter );
423 :
424 0 : return sal_True;
425 : }
426 :
427 0 : sal_Bool ScViewFunc::ApplyGraphicToObject( SdrObject* pPickObj, const Graphic& rGraphic )
428 : {
429 0 : sal_Bool bRet = false;
430 0 : SdrGrafObj* pNewGrafObj = NULL;
431 :
432 0 : ScDrawView* pScDrawView = GetScDrawView();
433 0 : if ( pScDrawView && pPickObj )
434 : {
435 : /**********************************************************************
436 : * Objekt neu attributieren
437 : **********************************************************************/
438 0 : SdrPageView* pPV = pScDrawView->GetSdrPageView();
439 0 : if (pPickObj->ISA(SdrGrafObj))
440 : {
441 : /******************************************************************
442 : * Das Graphik-Objekt bekommt eine neue Graphik
443 : ******************************************************************/
444 0 : pNewGrafObj = (SdrGrafObj*) pPickObj->Clone();
445 0 : pNewGrafObj->SetGraphic(rGraphic);
446 :
447 0 : pScDrawView->BegUndo(ScGlobal::GetRscString(STR_UNDO_DRAGDROP));
448 0 : pScDrawView->ReplaceObjectAtView(pPickObj, *pPV, pNewGrafObj);
449 0 : pScDrawView->EndUndo();
450 :
451 0 : bRet = sal_True;
452 : }
453 0 : else if (pPickObj->IsClosedObj() && !pPickObj->ISA(SdrOle2Obj))
454 : {
455 : /******************************************************************
456 : * Das Objekt wird mit der Graphik gefuellt
457 : ******************************************************************/
458 : //pScDrawView->BegUndo(ScGlobal::GetRscString(STR_UNDO_DRAGDROP));
459 0 : pScDrawView->AddUndo(new SdrUndoAttrObj(*pPickObj));
460 : //pScDrawView->EndUndo();
461 :
462 0 : XOBitmap aXOBitmap( rGraphic.GetBitmap() );
463 0 : SfxItemSet aSet( pScDrawView->GetModel()->GetItemPool(),
464 0 : XATTR_FILLSTYLE, XATTR_FILLBITMAP );
465 0 : aSet.Put(XFillStyleItem(XFILL_BITMAP));
466 0 : aSet.Put(XFillBitmapItem(String(), aXOBitmap));
467 :
468 0 : pPickObj->SetMergedItemSetAndBroadcast(aSet);
469 :
470 0 : bRet = sal_True;
471 : }
472 : }
473 0 : return bRet;
474 15 : }
475 :
476 :
477 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|