1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
30 #include "ChartDropTargetHelper.hxx"
31 #include "DiagramHelper.hxx"
32 #include "DataSourceHelper.hxx"
34 #include <com/sun/star/chart2/XChartDocument.hpp>
35 #include <com/sun/star/chart2/data/XDataProvider.hpp>
36 #include <com/sun/star/container/XChild.hpp>
38 #include <sot/formats.hxx>
41 using namespace ::com::sun::star
;
43 using ::com::sun::star::uno::Reference
;
44 using ::com::sun::star::uno::Sequence
;
45 using ::rtl::OUString
;
50 ::std::vector
< OUString
> lcl_getStringsFromByteSequence(
51 const Sequence
< sal_Int8
> & aByteSequence
)
53 ::std::vector
< OUString
> aResult
;
54 const sal_Int32 nLength
= aByteSequence
.getLength();
55 const sal_Char
* pBytes( reinterpret_cast< const sal_Char
* >( aByteSequence
.getConstArray()));
56 sal_Int32 nStartPos
= 0;
57 for( sal_Int32 nPos
=0; nPos
<nLength
; ++nPos
)
59 if( pBytes
[nPos
] == '\0' )
61 aResult
.push_back( OUString( pBytes
+ nStartPos
, (nPos
- nStartPos
), RTL_TEXTENCODING_ASCII_US
));
68 } // anonymous namespace
73 ChartDropTargetHelper::ChartDropTargetHelper(
74 const Reference
< datatransfer::dnd::XDropTarget
>& rxDropTarget
,
75 const Reference
< chart2::XChartDocument
> & xChartDocument
) :
76 DropTargetHelper( rxDropTarget
),
77 m_xChartDocument( xChartDocument
)
80 ChartDropTargetHelper::~ChartDropTargetHelper()
83 bool ChartDropTargetHelper::satisfiesPrerequisites() const
85 return ( m_xChartDocument
.is() &&
86 ! m_xChartDocument
->hasInternalDataProvider());
89 sal_Int8
ChartDropTargetHelper::AcceptDrop( const AcceptDropEvent
& rEvt
)
91 sal_Int8 nResult
= DND_ACTION_NONE
;
93 if( ( rEvt
.mnAction
== DND_ACTION_COPY
||
94 rEvt
.mnAction
== DND_ACTION_MOVE
) &&
95 satisfiesPrerequisites() &&
96 IsDropFormatSupported( SOT_FORMATSTR_ID_LINK
) )
98 // @todo: check if the data is suitable. Is this possible without XTransferable?
99 nResult
= rEvt
.mnAction
;
105 sal_Int8
ChartDropTargetHelper::ExecuteDrop( const ExecuteDropEvent
& rEvt
)
107 sal_Int8 nResult
= DND_ACTION_NONE
;
109 if( ( rEvt
.mnAction
== DND_ACTION_COPY
||
110 rEvt
.mnAction
== DND_ACTION_MOVE
) &&
111 rEvt
.maDropEvent
.Transferable
.is() &&
112 satisfiesPrerequisites())
114 TransferableDataHelper
aDataHelper( rEvt
.maDropEvent
.Transferable
);
115 if( aDataHelper
.HasFormat( SOT_FORMATSTR_ID_LINK
))
117 Sequence
< sal_Int8
> aBytes
;
118 if( aDataHelper
.GetSequence( SOT_FORMATSTR_ID_LINK
, aBytes
))
120 ::std::vector
< OUString
> aStrings( lcl_getStringsFromByteSequence( aBytes
));
121 if( aStrings
.size() >= 3 && aStrings
[0] == "soffice" )
123 OUString
aDocName( aStrings
[1] );
124 OUString
aRangeString( aStrings
[2] );
125 Reference
< container::XChild
> xChild( m_xChartDocument
, uno::UNO_QUERY
);
128 Reference
< frame::XModel
> xParentModel( xChild
->getParent(), uno::UNO_QUERY
);
129 if( xParentModel
.is() &&
130 m_xChartDocument
.is())
132 bool bDataComesFromParent
= true;
133 // @todo: get the title somehow and compare it to
134 // aDocName if successful (the document is the
136 if( bDataComesFromParent
)
138 Reference
< chart2::XDiagram
> xDiagram( m_xChartDocument
->getFirstDiagram() );
139 Reference
< chart2::data::XDataProvider
> xDataProvider( m_xChartDocument
->getDataProvider());
140 if( xDataProvider
.is() && xDiagram
.is() &&
141 DataSourceHelper::allArgumentsForRectRangeDetected( m_xChartDocument
))
143 Reference
< chart2::data::XDataSource
> xDataSource(
144 DataSourceHelper::pressUsedDataIntoRectangularFormat( m_xChartDocument
));
145 Sequence
< beans::PropertyValue
> aArguments(
146 xDataProvider
->detectArguments( xDataSource
));
149 beans::PropertyValue
* pCellRange
= 0;
150 for( sal_Int32 i
=0; i
<aArguments
.getLength(); ++i
)
152 if ( aArguments
[i
].Name
== "CellRangeRepresentation" )
154 pCellRange
= (aArguments
.getArray() + i
);
155 aArguments
[i
].Value
>>= aOldRange
;
161 // copy means add ranges, move means replace
162 if( rEvt
.mnAction
== DND_ACTION_COPY
)
164 // @todo: using implcit knowledge that ranges can be
165 // merged with ";". This should be done more general
166 pCellRange
->Value
<<= (aOldRange
+ OUString( sal_Unicode(';')) + aRangeString
);
168 // move means replace range
171 pCellRange
->Value
<<= aRangeString
;
174 xDataSource
.set( xDataProvider
->createDataSource( aArguments
));
175 xDiagram
->setDiagramData( xDataSource
, aArguments
);
177 // always return copy state to avoid deletion of the dragged range
178 nResult
= DND_ACTION_COPY
;
193 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */