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 .
22 #include <sot/storage.hxx>
23 #include <svl/itempool.hxx>
25 #include <sot/formats.hxx>
26 #include <vcl/treelistbox.hxx>
27 #include <vcl/treelistentry.hxx>
28 #include <tools/debug.hxx>
29 #include <tools/diagnose_ex.h>
36 using namespace ::com::sun::star::uno
;
37 using namespace ::com::sun::star::datatransfer
;
39 OLocalExchange::OLocalExchange( )
41 ,m_bClipboardOwner( false )
46 void OLocalExchange::copyToClipboard( vcl::Window
* _pWindow
, const GrantAccess
& )
48 if ( m_bClipboardOwner
)
49 { // simulate a lostOwnership to notify parties interested in
50 m_aClipboardListener
.Call( *this );
53 m_bClipboardOwner
= true;
54 CopyToClipboard( _pWindow
);
58 void OLocalExchange::clear()
60 if ( isClipboardOwner() )
64 Reference
< clipboard::XClipboard
> xClipBoard( getOwnClipboard() );
65 if ( xClipBoard
.is() )
66 xClipBoard
->setContents( nullptr, nullptr );
68 catch( const Exception
& )
70 DBG_UNHANDLED_EXCEPTION("svx");
72 m_bClipboardOwner
= false;
77 void SAL_CALL
OLocalExchange::lostOwnership( const Reference
< clipboard::XClipboard
>& _rxClipboard
, const Reference
< XTransferable
>& _rxTrans
)
79 TransferableHelper::implCallOwnLostOwnership( _rxClipboard
, _rxTrans
);
80 m_bClipboardOwner
= false;
82 m_aClipboardListener
.Call( *this );
86 void OLocalExchange::startDrag( vcl::Window
* _pWindow
, sal_Int8 _nDragSourceActions
, const GrantAccess
& )
89 StartDrag( _pWindow
, _nDragSourceActions
);
93 void OLocalExchange::DragFinished( sal_Int8 nDropAction
)
95 TransferableHelper::DragFinished( nDropAction
);
100 bool OLocalExchange::hasFormat( const DataFlavorExVector
& _rFormats
, SotClipboardFormatId _nFormatId
)
102 return std::any_of(_rFormats
.begin(), _rFormats
.end(),
103 [&_nFormatId
](const DataFlavorEx
& rFormat
) { return rFormat
.mnSotId
== _nFormatId
; });
107 bool OLocalExchange::GetData( const css::datatransfer::DataFlavor
& /*_rFlavor*/, const OUString
& /*rDestDoc*/ )
109 return false; // do not have any formats by default
112 OControlTransferData::OControlTransferData( )
113 :m_pFocusEntry( nullptr )
118 OControlTransferData::OControlTransferData( const Reference
< XTransferable
>& _rxTransferable
)
119 :m_pFocusEntry( nullptr )
121 TransferableDataHelper
aExchangedData( _rxTransferable
);
123 // try the formats we know
124 if ( OControlExchange::hasControlPathFormat( aExchangedData
.GetDataFlavorExVector() ) )
125 { // paths to the controls, relative to a root
126 Sequence
< Any
> aControlPathData
;
127 if ( aExchangedData
.GetAny(OControlExchange::getControlPathFormatId(), OUString()) >>= aControlPathData
)
129 DBG_ASSERT( aControlPathData
.getLength() >= 2, "OControlTransferData::OControlTransferData: invalid data for the control path format!" );
130 if ( aControlPathData
.getLength() >= 2 )
132 aControlPathData
[0] >>= m_xFormsRoot
;
133 aControlPathData
[1] >>= m_aControlPaths
;
138 OSL_FAIL( "OControlTransferData::OControlTransferData: invalid data for the control path format (2)!" );
141 if ( OControlExchange::hasHiddenControlModelsFormat( aExchangedData
.GetDataFlavorExVector() ) )
142 { // sequence of models of hidden controls
143 aExchangedData
.GetAny(OControlExchange::getHiddenControlModelsFormatId(), OUString()) >>= m_aHiddenControlModels
;
150 static bool lcl_fillDataFlavorEx( SotClipboardFormatId nId
, DataFlavorEx
& _rFlavor
)
152 _rFlavor
.mnSotId
= nId
;
153 return SotExchange::GetFormatDataFlavor( _rFlavor
.mnSotId
, _rFlavor
);
157 void OControlTransferData::updateFormats( )
159 m_aCurrentFormats
.clear();
160 m_aCurrentFormats
.reserve( 3 );
162 DataFlavorEx aFlavor
;
164 if ( m_aHiddenControlModels
.hasElements() )
166 if ( lcl_fillDataFlavorEx( OControlExchange::getHiddenControlModelsFormatId(), aFlavor
) )
167 m_aCurrentFormats
.push_back( aFlavor
);
170 if ( m_xFormsRoot
.is() && m_aControlPaths
.hasElements() )
172 if ( lcl_fillDataFlavorEx( OControlExchange::getControlPathFormatId(), aFlavor
) )
173 m_aCurrentFormats
.push_back( aFlavor
);
176 if ( !m_aSelectedEntries
.empty() )
178 if ( lcl_fillDataFlavorEx( OControlExchange::getFieldExchangeFormatId(), aFlavor
) )
179 m_aCurrentFormats
.push_back( aFlavor
);
184 size_t OControlTransferData::onEntryRemoved( SvTreeListEntry
* _pEntry
)
186 m_aSelectedEntries
.erase( _pEntry
);
187 return m_aSelectedEntries
.size();
191 void OControlTransferData::addSelectedEntry( SvTreeListEntry
* _pEntry
)
193 m_aSelectedEntries
.insert( _pEntry
);
197 void OControlTransferData::setFocusEntry( SvTreeListEntry
* _pFocusEntry
)
199 m_pFocusEntry
= _pFocusEntry
;
203 void OControlTransferData::addHiddenControlsFormat(const css::uno::Sequence
< css::uno::Reference
< css::uno::XInterface
> >& seqInterfaces
)
205 m_aHiddenControlModels
= seqInterfaces
;
209 void OControlTransferData::buildPathFormat(SvTreeListBox
const * pTreeBox
, SvTreeListEntry
const * pRoot
)
211 m_aControlPaths
.realloc(0);
213 sal_Int32 nEntryCount
= m_aSelectedEntries
.size();
214 if (nEntryCount
== 0)
217 m_aControlPaths
.realloc(nEntryCount
);
218 css::uno::Sequence
<sal_uInt32
>* pAllPaths
= m_aControlPaths
.getArray();
219 for (SvTreeListEntry
* pCurrentEntry
: m_aSelectedEntries
)
221 // first we collect the path in an array
222 ::std::vector
< sal_uInt32
> aCurrentPath
;
224 SvTreeListEntry
* pLoop
= pCurrentEntry
;
225 while (pLoop
!= pRoot
)
227 aCurrentPath
.push_back(pLoop
->GetChildListPos());
228 pLoop
= pTreeBox
->GetParent(pLoop
);
229 DBG_ASSERT((pLoop
!= nullptr) || (pRoot
== nullptr), "OControlTransferData::buildPathFormat: invalid root or entry !");
230 // pLoop == NULL means that I am at the top end, then the whole
231 // thing should abort, which will only be the case with pRoot == NULL
234 // then we can transfer it into css::uno::Sequence
235 Sequence
<sal_uInt32
>& rCurrentPath
= *pAllPaths
;
236 sal_Int32 nDepth
= aCurrentPath
.size();
238 rCurrentPath
.realloc(nDepth
);
239 sal_uInt32
* pSeq
= rCurrentPath
.getArray();
241 for (j
= nDepth
- 1, k
= 0; k
<nDepth
; --j
, ++k
)
242 pSeq
[j
] = aCurrentPath
[k
];
248 void OControlTransferData::buildListFromPath(SvTreeListBox
const * pTreeBox
, SvTreeListEntry
* pRoot
)
250 ListBoxEntrySet aEmpty
;
251 m_aSelectedEntries
.swap( aEmpty
);
253 for (const css::uno::Sequence
<sal_uInt32
>& rPaths
: std::as_const(m_aControlPaths
))
255 SvTreeListEntry
* pSearch
= pRoot
;
256 for (const sal_uInt32 nThisPath
: rPaths
)
257 pSearch
= pTreeBox
->GetEntry(pSearch
, nThisPath
);
259 m_aSelectedEntries
.insert( pSearch
);
263 OControlExchange::OControlExchange( )
268 bool OControlExchange::GetData( const DataFlavor
& _rFlavor
, const OUString
& rDestDoc
)
270 const SotClipboardFormatId nFormatId
= SotExchange::GetFormat( _rFlavor
);
272 if ( getControlPathFormatId( ) == nFormatId
)
274 // ugly. We have to pack all the info into one object
275 Sequence
< Any
> aCompleteInfo( 2 );
276 OSL_ENSURE( m_xFormsRoot
.is(), "OLocalExchange::GetData: invalid forms root for this format!" );
277 aCompleteInfo
.getArray()[ 0 ] <<= m_xFormsRoot
;
278 aCompleteInfo
.getArray()[ 1 ] <<= m_aControlPaths
;
280 SetAny( makeAny( aCompleteInfo
) );
282 else if ( getHiddenControlModelsFormatId() == nFormatId
)
284 // just need to transfer the models
285 SetAny( makeAny( m_aHiddenControlModels
) );
288 return OLocalExchange::GetData(_rFlavor
, rDestDoc
);
294 void OControlExchange::AddSupportedFormats()
296 if (m_pFocusEntry
&& !m_aSelectedEntries
.empty())
297 AddFormat(getFieldExchangeFormatId());
299 if (m_aControlPaths
.hasElements())
300 AddFormat(getControlPathFormatId());
302 if (m_aHiddenControlModels
.hasElements())
303 AddFormat(getHiddenControlModelsFormatId());
307 SotClipboardFormatId
OControlExchange::getControlPathFormatId()
309 static SotClipboardFormatId s_nFormat
=
310 SotExchange::RegisterFormatName("application/x-openoffice;windows_formatname=\"svxform.ControlPathExchange\"");
311 DBG_ASSERT(static_cast<SotClipboardFormatId
>(-1) != s_nFormat
, "OControlExchange::getControlPathFormatId: bad exchange id!");
316 SotClipboardFormatId
OControlExchange::getHiddenControlModelsFormatId()
318 static SotClipboardFormatId s_nFormat
=
319 SotExchange::RegisterFormatName("application/x-openoffice;windows_formatname=\"svxform.HiddenControlModelsExchange\"");
320 DBG_ASSERT(static_cast<SotClipboardFormatId
>(-1) != s_nFormat
, "OControlExchange::getHiddenControlModelsFormatId: bad exchange id!");
325 SotClipboardFormatId
OControlExchange::getFieldExchangeFormatId()
327 static SotClipboardFormatId s_nFormat
=
328 SotExchange::RegisterFormatName("application/x-openoffice;windows_formatname=\"svxform.FieldNameExchange\"");
329 DBG_ASSERT(static_cast<SotClipboardFormatId
>(-1) != s_nFormat
, "OControlExchange::getFieldExchangeFormatId: bad exchange id!");
334 //= OControlExchangeHelper
336 OLocalExchange
* OControlExchangeHelper::createExchange() const
338 return new OControlExchange
;
341 OLocalExchangeHelper::OLocalExchangeHelper(vcl::Window
* _pDragSource
)
342 :m_pDragSource(_pDragSource
)
347 OLocalExchangeHelper::~OLocalExchangeHelper()
353 void OLocalExchangeHelper::startDrag( sal_Int8 nDragSourceActions
)
355 DBG_ASSERT(m_xTransferable
.is(), "OLocalExchangeHelper::startDrag: not prepared!");
356 m_xTransferable
->startDrag( m_pDragSource
, nDragSourceActions
, OLocalExchange::GrantAccess() );
360 void OLocalExchangeHelper::copyToClipboard( ) const
362 DBG_ASSERT( m_xTransferable
.is(), "OLocalExchangeHelper::copyToClipboard: not prepared!" );
363 m_xTransferable
->copyToClipboard( m_pDragSource
, OLocalExchange::GrantAccess() );
367 void OLocalExchangeHelper::implReset()
369 if (m_xTransferable
.is())
371 m_xTransferable
->setClipboardListener( Link
<OLocalExchange
&,void>() );
372 m_xTransferable
.clear();
377 void OLocalExchangeHelper::prepareDrag( )
379 DBG_ASSERT(!m_xTransferable
.is() || !m_xTransferable
->isDragging(), "OLocalExchangeHelper::prepareDrag: recursive DnD?");
382 m_xTransferable
= createExchange();
389 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */