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 <sal/config.h>
24 #include "ChartTransferable.hxx"
26 #include <sot/exchange.hxx>
27 #include <sot/storage.hxx>
28 #include <unotools/streamwrap.hxx>
29 #include <vcl/graph.hxx>
30 #include <svl/itempool.hxx>
31 #include <editeng/eeitem.hxx>
32 #include <editeng/fhgtitem.hxx>
33 #include <svx/svditer.hxx>
34 #include <svx/svdmodel.hxx>
35 #include <svx/unomodel.hxx>
36 #include <svx/svdview.hxx>
38 constexpr sal_uInt32 CHARTTRANSFER_OBJECTTYPE_DRAWMODEL
= 1;
40 using namespace ::com::sun::star
;
42 using ::com::sun::star::uno::Reference
;
47 ChartTransferable::ChartTransferable(
49 SdrObject
* pSelectedObj
,
51 : m_pMarkedObjModel( nullptr )
52 ,m_bDrawing( bDrawing
)
54 std::unique_ptr
<SdrExchangeView
> pExchgView(std::make_unique
<SdrView
>( rSdrModel
));
55 SdrPageView
* pPv
= pExchgView
->ShowSdrPage( rSdrModel
.GetPage( 0 ));
57 pExchgView
->MarkObj( pSelectedObj
, pPv
);
59 pExchgView
->MarkAllObj( pPv
);
60 Graphic
aGraphic( pExchgView
->GetMarkedObjMetaFile(true));
61 m_xMetaFileGraphic
.set( aGraphic
.GetXGraphic());
64 m_pMarkedObjModel
= pExchgView
->CreateMarkedObjModel().release();
68 ChartTransferable::~ChartTransferable()
71 void ChartTransferable::AddSupportedFormats()
75 AddFormat( SotClipboardFormatId::DRAWING
);
77 AddFormat( SotClipboardFormatId::GDIMETAFILE
);
78 AddFormat( SotClipboardFormatId::PNG
);
79 AddFormat( SotClipboardFormatId::BITMAP
);
82 bool ChartTransferable::GetData( const css::datatransfer::DataFlavor
& rFlavor
, const OUString
& /*rDestDoc*/ )
84 SotClipboardFormatId nFormat
= SotExchange::GetFormat( rFlavor
);
87 if( HasFormat( nFormat
))
89 if ( nFormat
== SotClipboardFormatId::DRAWING
)
91 bResult
= SetObject( m_pMarkedObjModel
, CHARTTRANSFER_OBJECTTYPE_DRAWMODEL
, rFlavor
);
93 else if ( nFormat
== SotClipboardFormatId::GDIMETAFILE
)
95 Graphic
aGraphic( m_xMetaFileGraphic
);
96 bResult
= SetGDIMetaFile( aGraphic
.GetGDIMetaFile() );
98 else if( nFormat
== SotClipboardFormatId::BITMAP
)
100 Graphic
aGraphic( m_xMetaFileGraphic
);
101 bResult
= SetBitmapEx( aGraphic
.GetBitmapEx(), rFlavor
);
108 bool ChartTransferable::WriteObject( tools::SvRef
<SotStorageStream
>& rxOStm
, void* pUserObject
, sal_uInt32 nUserObjectId
,
109 const datatransfer::DataFlavor
& /* rFlavor */ )
111 // called from SetObject, put data into stream
114 switch ( nUserObjectId
)
116 case CHARTTRANSFER_OBJECTTYPE_DRAWMODEL
:
118 SdrModel
* pMarkedObjModel
= static_cast< SdrModel
* >( pUserObject
);
119 if ( pMarkedObjModel
)
121 rxOStm
->SetBufferSize( 0xff00 );
123 // for the changed pool defaults from drawing layer pool set those
124 // attributes as hard attributes to preserve them for saving
125 const SfxItemPool
& rItemPool
= pMarkedObjModel
->GetItemPool();
126 const SvxFontHeightItem
& rDefaultFontHeight
= rItemPool
.GetDefaultItem( EE_CHAR_FONTHEIGHT
);
127 sal_uInt16 nCount
= pMarkedObjModel
->GetPageCount();
128 for ( sal_uInt16 i
= 0; i
< nCount
; ++i
)
130 const SdrPage
* pPage
= pMarkedObjModel
->GetPage( i
);
131 SdrObjListIter
aIter( pPage
, SdrIterMode::DeepNoGroups
);
132 while ( aIter
.IsMore() )
134 SdrObject
* pObj
= aIter
.Next();
135 const SvxFontHeightItem
& rItem
= pObj
->GetMergedItem( EE_CHAR_FONTHEIGHT
);
136 if ( rItem
.GetHeight() == rDefaultFontHeight
.GetHeight() )
138 pObj
->SetMergedItem( rDefaultFontHeight
);
143 Reference
< io::XOutputStream
> xDocOut( new utl::OOutputStreamWrapper( *rxOStm
) );
144 if ( SvxDrawingLayerExport( pMarkedObjModel
, xDocOut
) )
149 bRet
= ( rxOStm
->GetError() == ERRCODE_NONE
);
155 OSL_FAIL( "ChartTransferable::WriteObject: unknown object id" );
164 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */