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/formats.hxx>
23 #include <tools/debug.hxx>
24 #include <comphelper/diagnose_ex.hxx>
28 using namespace ::com::sun::star::uno
;
29 using namespace ::com::sun::star::datatransfer
;
31 OLocalExchange::OLocalExchange( )
33 ,m_bClipboardOwner( false )
37 void OLocalExchange::copyToClipboard(const weld::Widget
& rWidget
, const GrantAccess
&)
39 if ( m_bClipboardOwner
)
40 { // simulate a lostOwnership to notify parties interested in
41 m_aClipboardListener
.Call( *this );
44 m_bClipboardOwner
= true;
45 CopyToClipboard(rWidget
.get_clipboard());
48 void OLocalExchange::clear()
50 if ( !isClipboardOwner() )
55 Reference
< clipboard::XClipboard
> xClipBoard( getOwnClipboard() );
56 if ( xClipBoard
.is() )
57 xClipBoard
->setContents( nullptr, nullptr );
59 catch( const Exception
& )
61 DBG_UNHANDLED_EXCEPTION("svx");
63 m_bClipboardOwner
= false;
66 void SAL_CALL
OLocalExchange::lostOwnership( const Reference
< clipboard::XClipboard
>& _rxClipboard
, const Reference
< XTransferable
>& _rxTrans
)
68 TransferDataContainer::implCallOwnLostOwnership( _rxClipboard
, _rxTrans
);
69 m_bClipboardOwner
= false;
71 m_aClipboardListener
.Call( *this );
74 void OLocalExchange::setDragging(bool bDragging
)
76 m_bDragging
= bDragging
;
79 void OLocalExchange::DragFinished( sal_Int8 nDropAction
)
81 TransferDataContainer::DragFinished( nDropAction
);
85 bool OLocalExchange::hasFormat( const DataFlavorExVector
& _rFormats
, SotClipboardFormatId _nFormatId
)
87 return std::any_of(_rFormats
.begin(), _rFormats
.end(),
88 [&_nFormatId
](const DataFlavorEx
& rFormat
) { return rFormat
.mnSotId
== _nFormatId
; });
91 bool OLocalExchange::GetData( const css::datatransfer::DataFlavor
& /*_rFlavor*/, const OUString
& /*rDestDoc*/ )
93 return false; // do not have any formats by default
96 OControlTransferData::OControlTransferData( )
97 : m_bFocusEntry(false)
101 OControlTransferData::OControlTransferData( const Reference
< XTransferable
>& _rxTransferable
)
102 : m_bFocusEntry(false)
104 TransferableDataHelper
aExchangedData( _rxTransferable
);
106 // try the formats we know
107 if ( OControlExchange::hasControlPathFormat( aExchangedData
.GetDataFlavorExVector() ) )
108 { // paths to the controls, relative to a root
109 Sequence
< Any
> aControlPathData
;
110 if ( aExchangedData
.GetAny(OControlExchange::getControlPathFormatId(), OUString()) >>= aControlPathData
)
112 DBG_ASSERT( aControlPathData
.getLength() >= 2, "OControlTransferData::OControlTransferData: invalid data for the control path format!" );
113 if ( aControlPathData
.getLength() >= 2 )
115 aControlPathData
[0] >>= m_xFormsRoot
;
116 aControlPathData
[1] >>= m_aControlPaths
;
121 OSL_FAIL( "OControlTransferData::OControlTransferData: invalid data for the control path format (2)!" );
124 if ( OControlExchange::hasHiddenControlModelsFormat( aExchangedData
.GetDataFlavorExVector() ) )
125 { // sequence of models of hidden controls
126 aExchangedData
.GetAny(OControlExchange::getHiddenControlModelsFormatId(), OUString()) >>= m_aHiddenControlModels
;
133 static bool lcl_fillDataFlavorEx( SotClipboardFormatId nId
, DataFlavorEx
& _rFlavor
)
135 _rFlavor
.mnSotId
= nId
;
136 return SotExchange::GetFormatDataFlavor( _rFlavor
.mnSotId
, _rFlavor
);
140 void OControlTransferData::updateFormats( )
142 m_aCurrentFormats
.clear();
143 m_aCurrentFormats
.reserve( 3 );
145 DataFlavorEx aFlavor
;
147 if ( m_aHiddenControlModels
.hasElements() )
149 if ( lcl_fillDataFlavorEx( OControlExchange::getHiddenControlModelsFormatId(), aFlavor
) )
150 m_aCurrentFormats
.push_back( aFlavor
);
153 if ( m_xFormsRoot
.is() && m_aControlPaths
.hasElements() )
155 if ( lcl_fillDataFlavorEx( OControlExchange::getControlPathFormatId(), aFlavor
) )
156 m_aCurrentFormats
.push_back( aFlavor
);
159 if ( !m_aSelectedEntries
.empty() )
161 if ( lcl_fillDataFlavorEx( OControlExchange::getFieldExchangeFormatId(), aFlavor
) )
162 m_aCurrentFormats
.push_back( aFlavor
);
166 size_t OControlTransferData::onEntryRemoved(const weld::TreeView
* pView
, const weld::TreeIter
* _pEntry
)
168 auto aIter
= std::find_if(m_aSelectedEntries
.begin(), m_aSelectedEntries
.end(),
169 [pView
, _pEntry
](const auto& rElem
) {
170 return pView
->iter_compare(*rElem
, *_pEntry
) == 0;
172 if (aIter
!= m_aSelectedEntries
.end())
173 m_aSelectedEntries
.erase(aIter
);
175 return m_aSelectedEntries
.size();
178 void OControlTransferData::addSelectedEntry(std::unique_ptr
<weld::TreeIter
> xEntry
)
180 m_aSelectedEntries
.emplace(std::move(xEntry
));
183 void OControlTransferData::setFocusEntry(bool _bFocusEntry
)
185 m_bFocusEntry
= _bFocusEntry
;
188 void OControlTransferData::addHiddenControlsFormat(const css::uno::Sequence
< css::uno::Reference
< css::uno::XInterface
> >& seqInterfaces
)
190 m_aHiddenControlModels
= seqInterfaces
;
193 void OControlTransferData::buildPathFormat(const weld::TreeView
* pTreeBox
, const weld::TreeIter
* pRoot
)
195 m_aControlPaths
.realloc(0);
197 sal_Int32 nEntryCount
= m_aSelectedEntries
.size();
198 if (nEntryCount
== 0)
201 m_aControlPaths
.realloc(nEntryCount
);
202 css::uno::Sequence
<sal_uInt32
>* pAllPaths
= m_aControlPaths
.getArray();
203 for (const auto& rCurrentEntry
: m_aSelectedEntries
)
205 // first we collect the path in an array
206 ::std::vector
< sal_uInt32
> aCurrentPath
;
208 std::unique_ptr
<weld::TreeIter
> xLoop(pTreeBox
->make_iterator(rCurrentEntry
.get()));
209 while (pTreeBox
->iter_compare(*xLoop
, *pRoot
) != 0)
211 aCurrentPath
.push_back(pTreeBox
->get_iter_index_in_parent(*xLoop
));
212 bool bLoop
= pTreeBox
->iter_parent(*xLoop
);
213 assert(bLoop
&& "OControlTransferData::buildPathFormat: invalid root or entry !"); (void)bLoop
;
216 // then we can transfer it into css::uno::Sequence
217 Sequence
<sal_uInt32
>& rCurrentPath
= *pAllPaths
;
218 sal_Int32 nDepth
= aCurrentPath
.size();
220 rCurrentPath
.realloc(nDepth
);
221 sal_uInt32
* pSeq
= rCurrentPath
.getArray();
223 for (j
= nDepth
- 1, k
= 0; k
<nDepth
; --j
, ++k
)
224 pSeq
[j
] = aCurrentPath
[k
];
229 void OControlTransferData::buildListFromPath(const weld::TreeView
* pTreeBox
, const weld::TreeIter
* pRoot
)
231 ListBoxEntrySet().swap(m_aSelectedEntries
);
233 for (const css::uno::Sequence
<sal_uInt32
>& rPaths
: m_aControlPaths
)
235 std::unique_ptr
<weld::TreeIter
> xSearch(pTreeBox
->make_iterator(pRoot
));
236 for (const sal_uInt32 nThisPath
: rPaths
)
237 pTreeBox
->iter_nth_child(*xSearch
, nThisPath
);
238 m_aSelectedEntries
.emplace(std::move(xSearch
));
242 OControlExchange::OControlExchange( )
246 bool OControlExchange::GetData( const DataFlavor
& _rFlavor
, const OUString
& rDestDoc
)
248 const SotClipboardFormatId nFormatId
= SotExchange::GetFormat( _rFlavor
);
250 if ( getControlPathFormatId( ) == nFormatId
)
252 // ugly. We have to pack all the info into one object
253 Sequence
< Any
> aCompleteInfo( 2 );
254 OSL_ENSURE( m_xFormsRoot
.is(), "OLocalExchange::GetData: invalid forms root for this format!" );
255 aCompleteInfo
.getArray()[ 0 ] <<= m_xFormsRoot
;
256 aCompleteInfo
.getArray()[ 1 ] <<= m_aControlPaths
;
258 SetAny( Any( aCompleteInfo
) );
260 else if ( getHiddenControlModelsFormatId() == nFormatId
)
262 // just need to transfer the models
263 SetAny( Any( m_aHiddenControlModels
) );
266 return OLocalExchange::GetData(_rFlavor
, rDestDoc
);
271 void OControlExchange::AddSupportedFormats()
273 if (m_bFocusEntry
&& !m_aSelectedEntries
.empty())
274 AddFormat(getFieldExchangeFormatId());
276 if (m_aControlPaths
.hasElements())
277 AddFormat(getControlPathFormatId());
279 if (m_aHiddenControlModels
.hasElements())
280 AddFormat(getHiddenControlModelsFormatId());
283 SotClipboardFormatId
OControlExchange::getControlPathFormatId()
285 static SotClipboardFormatId s_nFormat
=
286 SotExchange::RegisterFormatName(u
"application/x-openoffice;windows_formatname=\"svxform.ControlPathExchange\""_ustr
);
287 DBG_ASSERT(static_cast<SotClipboardFormatId
>(-1) != s_nFormat
, "OControlExchange::getControlPathFormatId: bad exchange id!");
291 SotClipboardFormatId
OControlExchange::getHiddenControlModelsFormatId()
293 static SotClipboardFormatId s_nFormat
=
294 SotExchange::RegisterFormatName(u
"application/x-openoffice;windows_formatname=\"svxform.HiddenControlModelsExchange\""_ustr
);
295 DBG_ASSERT(static_cast<SotClipboardFormatId
>(-1) != s_nFormat
, "OControlExchange::getHiddenControlModelsFormatId: bad exchange id!");
300 SotClipboardFormatId
OControlExchange::getFieldExchangeFormatId()
302 static SotClipboardFormatId s_nFormat
=
303 SotExchange::RegisterFormatName(u
"application/x-openoffice;windows_formatname=\"svxform.FieldNameExchange\""_ustr
);
304 DBG_ASSERT(static_cast<SotClipboardFormatId
>(-1) != s_nFormat
, "OControlExchange::getFieldExchangeFormatId: bad exchange id!");
308 //= OControlExchangeHelper
309 rtl::Reference
<OLocalExchange
> OControlExchangeHelper::createExchange() const
311 return new OControlExchange
;
314 OLocalExchangeHelper::OLocalExchangeHelper()
318 OLocalExchangeHelper::~OLocalExchangeHelper()
323 void OLocalExchangeHelper::copyToClipboard(const weld::Widget
& rWidget
) const
325 DBG_ASSERT( m_xTransferable
.is(), "OLocalExchangeHelper::copyToClipboard: not prepared!" );
326 m_xTransferable
->copyToClipboard(rWidget
, OLocalExchange::GrantAccess());
329 void OLocalExchangeHelper::implReset()
331 if (m_xTransferable
.is())
333 m_xTransferable
->setClipboardListener( Link
<OLocalExchange
&,void>() );
334 m_xTransferable
.clear();
338 void OLocalExchangeHelper::prepareDrag( )
340 DBG_ASSERT(!m_xTransferable
.is() || !m_xTransferable
->isDragging(), "OLocalExchangeHelper::prepareDrag: recursive DnD?");
343 m_xTransferable
= createExchange();
347 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */