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 <svx/svditer.hxx>
21 #include <svx/svdograf.hxx>
22 #include <svx/svdogrp.hxx>
23 #include <svx/svdoole2.hxx>
24 #include <svx/svdpage.hxx>
25 #include <svx/svdundo.hxx>
26 #include <sfx2/docfile.hxx>
27 #include <tools/urlobj.hxx>
28 #include <toolkit/helper/vclunohelper.hxx>
30 #include "drawview.hxx"
32 #include "drwlayer.hxx"
33 #include "viewdata.hxx"
34 #include "document.hxx"
36 #include "drwtrans.hxx"
37 #include "transobj.hxx"
38 #include "drawutil.hxx"
40 #include "globstr.hrc"
41 #include "chartarr.hxx"
42 #include <gridwin.hxx>
44 #include <com/sun/star/embed/NoVisualAreaSizeException.hpp>
45 #include <com/sun/star/embed/Aspects.hpp>
46 #include <com/sun/star/embed/XEmbeddedObject.hpp>
47 #include <com/sun/star/embed/XComponentSupplier.hpp>
48 #include <com/sun/star/chart2/XChartTypeContainer.hpp>
49 #include <com/sun/star/chart2/XCoordinateSystemContainer.hpp>
50 #include <com/sun/star/chart2/XDataSeriesContainer.hpp>
52 using namespace com::sun::star
;
56 void ScDrawView::CheckOle( const SdrMarkList
& rMarkList
, bool& rAnyOle
, bool& rOneOle
)
58 rAnyOle
= rOneOle
= false;
59 const size_t nCount
= rMarkList
.GetMarkCount();
60 for (size_t i
=0; i
<nCount
; ++i
)
62 SdrMark
* pMark
= rMarkList
.GetMark(i
);
63 SdrObject
* pObj
= pMark
->GetMarkedSdrObj();
64 sal_uInt16 nSdrObjKind
= pObj
->GetObjIdentifier();
65 if (nSdrObjKind
== OBJ_OLE2
)
68 rOneOle
= (nCount
== 1);
71 else if ( dynamic_cast<const SdrObjGroup
*>( pObj
) != nullptr )
73 SdrObjListIter
aIter( *pObj
, IM_DEEPNOGROUPS
);
74 SdrObject
* pSubObj
= aIter
.Next();
77 if ( pSubObj
->GetObjIdentifier() == OBJ_OLE2
)
80 // rOneOle remains sal_False - a group isn't treated like a single OLE object
83 pSubObj
= aIter
.Next();
89 void ScDrawView::BeginDrag( vcl::Window
* pWindow
, const Point
& rStartPos
)
91 if ( AreObjectsMarked() )
95 Rectangle aMarkedRect
= GetAllMarkedRect();
96 vcl::Region
aRegion( aMarkedRect
);
98 aDragStartDiff
= rStartPos
- aMarkedRect
.TopLeft();
100 bool bAnyOle
, bOneOle
;
101 const SdrMarkList
& rMarkList
= GetMarkedObjectList();
102 CheckOle( rMarkList
, bAnyOle
, bOneOle
);
104 ScDocShellRef aDragShellRef
;
107 aDragShellRef
= new ScDocShell
; // DocShell needs a Ref immediately
108 aDragShellRef
->DoInitNew();
110 ScDrawLayer::SetGlobalDrawPersist(aDragShellRef
);
111 SdrModel
* pModel
= GetMarkedObjModel();
112 ScDrawLayer::SetGlobalDrawPersist(nullptr);
114 // Charts now always copy their data in addition to the source reference, so
115 // there's no need to call SchDLL::Update for the charts in the clipboard doc.
116 // Update with the data (including NumberFormatter) from the live document would
117 // also store the NumberFormatter in the clipboard chart (#88749#)
119 ScDocShell
* pDocSh
= pViewData
->GetDocShell();
121 TransferableObjectDescriptor aObjDesc
;
122 pDocSh
->FillTransferableObjectDescriptor( aObjDesc
);
123 aObjDesc
.maDisplayName
= pDocSh
->GetMedium()->GetURLObject().GetURLNoPass();
124 // maSize is set in ScDrawTransferObj ctor
126 ScDrawTransferObj
* pTransferObj
= new ScDrawTransferObj( pModel
, pDocSh
, aObjDesc
);
127 uno::Reference
<datatransfer::XTransferable
> xTransferable( pTransferObj
);
129 pTransferObj
->SetDrawPersist( &aDragShellRef
); // keep persist for ole objects alive
130 pTransferObj
->SetDragSource( this ); // copies selection
132 SC_MOD()->SetDragObject( nullptr, pTransferObj
); // for internal D&D
133 pTransferObj
->StartDrag( pWindow
, DND_ACTION_COPYMOVE
| DND_ACTION_LINK
);
139 void getRangeFromDataSource( uno::Reference
< chart2::data::XDataSource
> xDataSource
, std::vector
<OUString
>& rRangeRep
)
141 uno::Sequence
<uno::Reference
<chart2::data::XLabeledDataSequence
> > xSeqs
= xDataSource
->getDataSequences();
142 for (sal_Int32 i
= 0, n
= xSeqs
.getLength(); i
< n
; ++i
)
144 uno::Reference
<chart2::data::XLabeledDataSequence
> xLS
= xSeqs
[i
];
145 uno::Reference
<chart2::data::XDataSequence
> xSeq
= xLS
->getValues();
148 OUString aRep
= xSeq
->getSourceRangeRepresentation();
149 rRangeRep
.push_back(aRep
);
151 xSeq
= xLS
->getLabel();
154 OUString aRep
= xSeq
->getSourceRangeRepresentation();
155 rRangeRep
.push_back(aRep
);
160 void getRangeFromErrorBar(const uno::Reference
< chart2::XChartDocument
>& rChartDoc
, std::vector
<OUString
>& rRangeRep
)
162 uno::Reference
<chart2::XDiagram
> xDiagram
= rChartDoc
->getFirstDiagram();
166 uno::Reference
< chart2::XCoordinateSystemContainer
> xCooSysContainer( xDiagram
, uno::UNO_QUERY
);
167 if(!xCooSysContainer
.is())
170 uno::Sequence
< uno::Reference
< chart2::XCoordinateSystem
> > xCooSysSequence( xCooSysContainer
->getCoordinateSystems());
171 for(sal_Int32 i
= 0; i
< xCooSysSequence
.getLength(); ++i
)
173 uno::Reference
< chart2::XChartTypeContainer
> xChartTypeContainer( xCooSysSequence
[i
], uno::UNO_QUERY
);
174 if(!xChartTypeContainer
.is())
177 uno::Sequence
< uno::Reference
< chart2::XChartType
> > xChartTypeSequence( xChartTypeContainer
->getChartTypes() );
178 for(sal_Int32 nChartType
= 0; nChartType
< xChartTypeSequence
.getLength(); ++nChartType
)
180 uno::Reference
< chart2::XDataSeriesContainer
> xDataSequenceContainer( xChartTypeSequence
[nChartType
], uno::UNO_QUERY
);
181 if(!xDataSequenceContainer
.is())
184 uno::Sequence
< uno::Reference
< chart2::XDataSeries
> > xSeriesSequence( xDataSequenceContainer
->getDataSeries() );
185 for(sal_Int32 nDataSeries
= 0; nDataSeries
< xSeriesSequence
.getLength(); ++nDataSeries
)
187 uno::Reference
< chart2::XDataSeries
> xSeries
= xSeriesSequence
[nDataSeries
];
188 uno::Reference
< beans::XPropertySet
> xPropSet( xSeries
, uno::UNO_QUERY
);
189 uno::Reference
< chart2::data::XDataSource
> xErrorBarY
;
190 xPropSet
->getPropertyValue("ErrorBarY") >>= xErrorBarY
;
192 getRangeFromDataSource(xErrorBarY
, rRangeRep
);
193 uno::Reference
< chart2::data::XDataSource
> xErrorBarX
;
194 xPropSet
->getPropertyValue("ErrorBarX") >>= xErrorBarX
;
196 getRangeFromDataSource(xErrorBarX
, rRangeRep
);
202 void getRangeFromOle2Object(const SdrOle2Obj
& rObj
, std::vector
<OUString
>& rRangeRep
)
205 // not a chart object.
208 uno::Reference
<embed::XEmbeddedObject
> xObj
= rObj
.GetObjRef();
212 uno::Reference
<embed::XComponentSupplier
> xCompSupp(xObj
, uno::UNO_QUERY
);
216 uno::Reference
<chart2::XChartDocument
> xChartDoc(xCompSupp
->getComponent(), uno::UNO_QUERY
);
220 if(xChartDoc
->hasInternalDataProvider())
223 getRangeFromErrorBar(xChartDoc
, rRangeRep
);
225 uno::Reference
<chart2::data::XDataSource
> xDataSource(xChartDoc
, uno::UNO_QUERY
);
226 if (!xDataSource
.is())
229 // Get all data sources used in this chart.
230 getRangeFromDataSource(xDataSource
, rRangeRep
);
233 // Get all cell ranges that are referenced by the selected chart objects.
234 void getChartSourceRanges(ScDocument
* pDoc
, const SdrMarkList
& rObjs
, std::vector
<ScRange
>& rRanges
)
236 std::vector
<OUString
> aRangeReps
;
237 for (size_t i
= 0, n
= rObjs
.GetMarkCount(); i
< n
; ++i
)
239 const SdrMark
* pMark
= rObjs
.GetMark(i
);
243 const SdrObject
* pObj
= pMark
->GetMarkedSdrObj();
247 switch (pObj
->GetObjIdentifier())
250 getRangeFromOle2Object(static_cast<const SdrOle2Obj
&>(*pObj
), aRangeReps
);
254 SdrObjListIter
aIter(*pObj
, IM_DEEPNOGROUPS
);
255 for (SdrObject
* pSubObj
= aIter
.Next(); pSubObj
; pSubObj
= aIter
.Next())
257 if (pSubObj
->GetObjIdentifier() != OBJ_OLE2
)
260 getRangeFromOle2Object(static_cast<const SdrOle2Obj
&>(*pSubObj
), aRangeReps
);
270 // Compile all range representation strings into ranges.
271 std::vector
<OUString
>::const_iterator it
= aRangeReps
.begin(), itEnd
= aRangeReps
.end();
272 for (; it
!= itEnd
; ++it
)
276 if (aRange
.Parse(*it
, pDoc
, ScRefFlags::VALID
, pDoc
->GetAddressConvention()) & ScRefFlags::VALID
)
278 for(size_t i
= 0; i
< aRange
.size(); ++i
)
279 rRanges
.push_back(*aRange
[i
]);
281 else if (aAddr
.Parse(*it
, pDoc
, pDoc
->GetAddressConvention()) & ScRefFlags::VALID
)
282 rRanges
.push_back(aAddr
);
286 class InsertTabIndex
: public std::unary_function
<ScRange
, void>
288 std::vector
<SCTAB
>& mrTabs
;
290 explicit InsertTabIndex(std::vector
<SCTAB
>& rTabs
) : mrTabs(rTabs
) {}
291 void operator() (const ScRange
& rRange
)
293 mrTabs
.push_back(rRange
.aStart
.Tab());
297 class CopyRangeData
: public std::unary_function
<ScRange
, void>
302 CopyRangeData(ScDocument
* pSrc
, ScDocument
* pDest
) : mpSrc(pSrc
), mpDest(pDest
) {}
304 void operator() (const ScRange
& rRange
)
307 mpSrc
->GetName(rRange
.aStart
.Tab(), aTabName
);
310 if (!mpDest
->GetTable(aTabName
, nTab
))
311 // Sheet by this name doesn't exist.
314 mpSrc
->CopyStaticToDocument(rRange
, nTab
, mpDest
);
318 void copyChartRefDataToClipDoc(ScDocument
* pSrcDoc
, ScDocument
* pClipDoc
, const std::vector
<ScRange
>& rRanges
)
320 // Get a list of referenced table indices.
321 std::vector
<SCTAB
> aTabs
;
322 std::for_each(rRanges
.begin(), rRanges
.end(), InsertTabIndex(aTabs
));
323 std::sort(aTabs
.begin(), aTabs
.end());
324 aTabs
.erase(std::unique(aTabs
.begin(), aTabs
.end()), aTabs
.end());
330 // Create sheets only for referenced source sheets.
332 std::vector
<SCTAB
>::const_iterator it
= aTabs
.begin(), itEnd
= aTabs
.end();
333 if (!pSrcDoc
->GetName(*it
, aName
))
336 pClipDoc
->SetTabNameOnLoad(0, aName
); // document initially has one sheet.
338 for (++it
; it
!= itEnd
; ++it
)
340 if (!pSrcDoc
->GetName(*it
, aName
))
343 pClipDoc
->AppendTabOnLoad(aName
);
346 std::for_each(rRanges
.begin(), rRanges
.end(), CopyRangeData(pSrcDoc
, pClipDoc
));
351 void ScDrawView::DoCopy()
353 const SdrMarkList
& rMarkList
= GetMarkedObjectList();
354 std::vector
<ScRange
> aRanges
;
355 getChartSourceRanges(pDoc
, rMarkList
, aRanges
);
357 // update ScGlobal::xDrawClipDocShellRef
358 ScDrawLayer::SetGlobalDrawPersist( ScTransferObj::SetDrawClipDoc(!aRanges
.empty()) );
359 if (ScGlobal::xDrawClipDocShellRef
.Is())
361 // Copy data referenced by the chart objects to the draw clip
362 // document. We need to do this before GetMarkedObjModel() below.
363 ScDocShellRef xDocSh
= ScGlobal::xDrawClipDocShellRef
;
364 ScDocument
& rClipDoc
= xDocSh
->GetDocument();
365 copyChartRefDataToClipDoc(pDoc
, &rClipDoc
, aRanges
);
367 SdrModel
* pModel
= GetMarkedObjModel();
368 ScDrawLayer::SetGlobalDrawPersist(nullptr);
370 // Charts now always copy their data in addition to the source reference, so
371 // there's no need to call SchDLL::Update for the charts in the clipboard doc.
372 // Update with the data (including NumberFormatter) from the live document would
373 // also store the NumberFormatter in the clipboard chart (#88749#)
375 ScDocShell
* pDocSh
= pViewData
->GetDocShell();
377 TransferableObjectDescriptor aObjDesc
;
378 pDocSh
->FillTransferableObjectDescriptor( aObjDesc
);
379 aObjDesc
.maDisplayName
= pDocSh
->GetMedium()->GetURLObject().GetURLNoPass();
380 // maSize is set in ScDrawTransferObj ctor
382 ScDrawTransferObj
* pTransferObj
= new ScDrawTransferObj( pModel
, pDocSh
, aObjDesc
);
383 uno::Reference
<datatransfer::XTransferable
> xTransferable( pTransferObj
);
385 if ( ScGlobal::xDrawClipDocShellRef
.Is() )
387 pTransferObj
->SetDrawPersist( ScGlobal::xDrawClipDocShellRef
.get() ); // keep persist for ole objects alive
390 pTransferObj
->CopyToClipboard( pViewData
->GetActiveWin() ); // system clipboard
391 SC_MOD()->SetClipObject( nullptr, pTransferObj
); // internal clipboard
394 uno::Reference
<datatransfer::XTransferable
> ScDrawView::CopyToTransferable()
396 bool bAnyOle
, bOneOle
;
397 const SdrMarkList
& rMarkList
= GetMarkedObjectList();
398 CheckOle( rMarkList
, bAnyOle
, bOneOle
);
400 // update ScGlobal::xDrawClipDocShellRef
401 ScDrawLayer::SetGlobalDrawPersist( ScTransferObj::SetDrawClipDoc( bAnyOle
) );
402 SdrModel
* pModel
= GetMarkedObjModel();
403 ScDrawLayer::SetGlobalDrawPersist(nullptr);
405 // Charts now always copy their data in addition to the source reference, so
406 // there's no need to call SchDLL::Update for the charts in the clipboard doc.
407 // Update with the data (including NumberFormatter) from the live document would
408 // also store the NumberFormatter in the clipboard chart (#88749#)
409 // lcl_RefreshChartData( pModel, pViewData->GetDocument() );
411 ScDocShell
* pDocSh
= pViewData
->GetDocShell();
413 TransferableObjectDescriptor aObjDesc
;
414 pDocSh
->FillTransferableObjectDescriptor( aObjDesc
);
415 aObjDesc
.maDisplayName
= pDocSh
->GetMedium()->GetURLObject().GetURLNoPass();
416 // maSize is set in ScDrawTransferObj ctor
418 ScDrawTransferObj
* pTransferObj
= new ScDrawTransferObj( pModel
, pDocSh
, aObjDesc
);
419 uno::Reference
<datatransfer::XTransferable
> xTransferable( pTransferObj
);
421 if ( ScGlobal::xDrawClipDocShellRef
.Is() )
423 pTransferObj
->SetDrawPersist( ScGlobal::xDrawClipDocShellRef
.get() ); // keep persist for ole objects alive
426 return xTransferable
;
429 // Calculate correction for 100%, regardless of current settings
431 void ScDrawView::CalcNormScale( Fraction
& rFractX
, Fraction
& rFractY
) const
433 double nPPTX
= ScGlobal::nScreenPPTX
;
434 double nPPTY
= ScGlobal::nScreenPPTY
;
437 nPPTX
/= pViewData
->GetDocShell()->GetOutputFactor();
441 pDoc
->GetTableArea( nTab
, nEndCol
, nEndRow
);
448 ScDrawUtil::CalcScale( pDoc
, nTab
, 0,0, nEndCol
,nEndRow
, pDev
, aZoom
,aZoom
,
449 nPPTX
, nPPTY
, rFractX
,rFractY
);
452 void ScDrawView::SetMarkedOriginalSize()
454 SdrUndoGroup
* pUndoGroup
= new SdrUndoGroup(*GetModel());
456 const SdrMarkList
& rMarkList
= GetMarkedObjectList();
458 const size_t nCount
= rMarkList
.GetMarkCount();
459 for (size_t i
=0; i
<nCount
; ++i
)
461 SdrObject
* pObj
= rMarkList
.GetMark(i
)->GetMarkedSdrObj();
462 sal_uInt16 nIdent
= pObj
->GetObjIdentifier();
465 if (nIdent
== OBJ_OLE2
)
467 // TODO/LEAN: working with visual area can switch object to running state
468 uno::Reference
< embed::XEmbeddedObject
> xObj( static_cast<SdrOle2Obj
*>(pObj
)->GetObjRef(), uno::UNO_QUERY
);
469 if ( xObj
.is() ) // NULL for an invalid object that couldn't be loaded
471 sal_Int64 nAspect
= static_cast<SdrOle2Obj
*>(pObj
)->GetAspect();
473 if ( nAspect
== embed::Aspects::MSOLE_ICON
)
475 MapMode
aMapMode( MAP_100TH_MM
);
476 aOriginalSize
= static_cast<SdrOle2Obj
*>(pObj
)->GetOrigObjSize( &aMapMode
);
481 MapUnit aUnit
= VCLUnoHelper::UnoEmbed2VCLMapUnit( xObj
->getMapUnit( static_cast<SdrOle2Obj
*>(pObj
)->GetAspect() ) );
485 aSz
= xObj
->getVisualAreaSize( static_cast<SdrOle2Obj
*>(pObj
)->GetAspect() );
486 aOriginalSize
= OutputDevice::LogicToLogic(
487 Size( aSz
.Width
, aSz
.Height
),
488 aUnit
, MAP_100TH_MM
);
490 } catch( embed::NoVisualAreaSizeException
& )
492 OSL_ENSURE( false, "Can't get the original size of the object!" );
497 else if (nIdent
== OBJ_GRAF
)
499 const Graphic
& rGraphic
= static_cast<SdrGrafObj
*>(pObj
)->GetGraphic();
501 MapMode aSourceMap
= rGraphic
.GetPrefMapMode();
502 MapMode
aDestMap( MAP_100TH_MM
);
503 if (aSourceMap
.GetMapUnit() == MAP_PIXEL
)
505 // consider pixel correction, so that the bitmap is correct on the screen
506 Fraction aNormScaleX
, aNormScaleY
;
507 CalcNormScale( aNormScaleX
, aNormScaleY
);
508 aDestMap
.SetScaleX(aNormScaleX
);
509 aDestMap
.SetScaleY(aNormScaleY
);
513 vcl::Window
* pActWin
= pViewData
->GetActiveWin();
516 aOriginalSize
= pActWin
->LogicToLogic(
517 rGraphic
.GetPrefSize(), &aSourceMap
, &aDestMap
);
525 Rectangle aDrawRect
= pObj
->GetLogicRect();
527 pUndoGroup
->AddAction( new SdrUndoGeoObj( *pObj
) );
528 pObj
->Resize( aDrawRect
.TopLeft(), Fraction( aOriginalSize
.Width(), aDrawRect
.GetWidth() ),
529 Fraction( aOriginalSize
.Height(), aDrawRect
.GetHeight() ) );
534 if (nDone
&& pViewData
)
536 pUndoGroup
->SetComment(ScGlobal::GetRscString( STR_UNDO_ORIGINALSIZE
));
537 ScDocShell
* pDocSh
= pViewData
->GetDocShell();
538 pDocSh
->GetUndoManager()->AddUndoAction(pUndoGroup
);
539 pDocSh
->SetDrawModified();
545 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */