/src/libreoffice/svx/source/form/fmexch.cxx
Line | Count | Source |
1 | | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
2 | | /* |
3 | | * This file is part of the LibreOffice project. |
4 | | * |
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/. |
8 | | * |
9 | | * This file incorporates work covered by the following license notice: |
10 | | * |
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 . |
18 | | */ |
19 | | |
20 | | #include <fmexch.hxx> |
21 | | |
22 | | #include <sot/formats.hxx> |
23 | | #include <tools/debug.hxx> |
24 | | #include <comphelper/diagnose_ex.hxx> |
25 | | |
26 | | namespace svxform |
27 | | { |
28 | | using namespace ::com::sun::star::uno; |
29 | | using namespace ::com::sun::star::datatransfer; |
30 | | |
31 | | OLocalExchange::OLocalExchange( ) |
32 | 0 | :m_bDragging( false ) |
33 | 0 | ,m_bClipboardOwner( false ) |
34 | 0 | { |
35 | 0 | } |
36 | | |
37 | | void OLocalExchange::copyToClipboard(const weld::Widget& rWidget, const GrantAccess&) |
38 | 0 | { |
39 | 0 | if ( m_bClipboardOwner ) |
40 | 0 | { // simulate a lostOwnership to notify parties interested in |
41 | 0 | m_aClipboardListener.Call( *this ); |
42 | 0 | } |
43 | |
|
44 | 0 | m_bClipboardOwner = true; |
45 | 0 | CopyToClipboard(rWidget.get_clipboard()); |
46 | 0 | } |
47 | | |
48 | | void OLocalExchange::clear() |
49 | 0 | { |
50 | 0 | if ( !isClipboardOwner() ) |
51 | 0 | return; |
52 | | |
53 | 0 | try |
54 | 0 | { |
55 | 0 | Reference< clipboard::XClipboard > xClipBoard( getOwnClipboard() ); |
56 | 0 | if ( xClipBoard.is() ) |
57 | 0 | xClipBoard->setContents( nullptr, nullptr ); |
58 | 0 | } |
59 | 0 | catch( const Exception& ) |
60 | 0 | { |
61 | 0 | DBG_UNHANDLED_EXCEPTION("svx"); |
62 | 0 | } |
63 | 0 | m_bClipboardOwner = false; |
64 | 0 | } |
65 | | |
66 | | void SAL_CALL OLocalExchange::lostOwnership( const Reference< clipboard::XClipboard >& _rxClipboard, const Reference< XTransferable >& _rxTrans ) |
67 | 0 | { |
68 | 0 | TransferDataContainer::implCallOwnLostOwnership( _rxClipboard, _rxTrans ); |
69 | 0 | m_bClipboardOwner = false; |
70 | |
|
71 | 0 | m_aClipboardListener.Call( *this ); |
72 | 0 | } |
73 | | |
74 | | void OLocalExchange::setDragging(bool bDragging) |
75 | 0 | { |
76 | 0 | m_bDragging = bDragging; |
77 | 0 | } |
78 | | |
79 | | void OLocalExchange::DragFinished( sal_Int8 nDropAction ) |
80 | 0 | { |
81 | 0 | TransferDataContainer::DragFinished( nDropAction ); |
82 | 0 | setDragging(false); |
83 | 0 | } |
84 | | |
85 | | bool OLocalExchange::hasFormat( const DataFlavorExVector& _rFormats, SotClipboardFormatId _nFormatId ) |
86 | 0 | { |
87 | 0 | return std::any_of(_rFormats.begin(), _rFormats.end(), |
88 | 0 | [&_nFormatId](const DataFlavorEx& rFormat) { return rFormat.mnSotId == _nFormatId; }); |
89 | 0 | } |
90 | | |
91 | | bool OLocalExchange::GetData( const css::datatransfer::DataFlavor& /*_rFlavor*/, const OUString& /*rDestDoc*/ ) |
92 | 0 | { |
93 | 0 | return false; // do not have any formats by default |
94 | 0 | } |
95 | | |
96 | | OControlTransferData::OControlTransferData( ) |
97 | 0 | : m_bFocusEntry(false) |
98 | 0 | { |
99 | 0 | } |
100 | | |
101 | | OControlTransferData::OControlTransferData( const Reference< XTransferable >& _rxTransferable ) |
102 | 0 | : m_bFocusEntry(false) |
103 | 0 | { |
104 | 0 | TransferableDataHelper aExchangedData( _rxTransferable ); |
105 | | |
106 | | // try the formats we know |
107 | 0 | if ( OControlExchange::hasControlPathFormat( aExchangedData.GetDataFlavorExVector() ) ) |
108 | 0 | { // paths to the controls, relative to a root |
109 | 0 | Sequence< Any > aControlPathData; |
110 | 0 | if ( aExchangedData.GetAny(OControlExchange::getControlPathFormatId(), OUString()) >>= aControlPathData ) |
111 | 0 | { |
112 | 0 | DBG_ASSERT( aControlPathData.getLength() >= 2, "OControlTransferData::OControlTransferData: invalid data for the control path format!" ); |
113 | 0 | if ( aControlPathData.getLength() >= 2 ) |
114 | 0 | { |
115 | 0 | aControlPathData[0] >>= m_xFormsRoot; |
116 | 0 | aControlPathData[1] >>= m_aControlPaths; |
117 | 0 | } |
118 | 0 | } |
119 | 0 | else |
120 | 0 | { |
121 | 0 | OSL_FAIL( "OControlTransferData::OControlTransferData: invalid data for the control path format (2)!" ); |
122 | 0 | } |
123 | 0 | } |
124 | 0 | if ( OControlExchange::hasHiddenControlModelsFormat( aExchangedData.GetDataFlavorExVector() ) ) |
125 | 0 | { // sequence of models of hidden controls |
126 | 0 | aExchangedData.GetAny(OControlExchange::getHiddenControlModelsFormatId(), OUString()) >>= m_aHiddenControlModels; |
127 | 0 | } |
128 | |
|
129 | 0 | updateFormats( ); |
130 | 0 | } |
131 | | |
132 | | |
133 | | static bool lcl_fillDataFlavorEx( SotClipboardFormatId nId, DataFlavorEx& _rFlavor ) |
134 | 0 | { |
135 | 0 | _rFlavor.mnSotId = nId; |
136 | 0 | return SotExchange::GetFormatDataFlavor( _rFlavor.mnSotId, _rFlavor ); |
137 | 0 | } |
138 | | |
139 | | |
140 | | void OControlTransferData::updateFormats( ) |
141 | 0 | { |
142 | 0 | m_aCurrentFormats.clear(); |
143 | 0 | m_aCurrentFormats.reserve( 3 ); |
144 | |
|
145 | 0 | DataFlavorEx aFlavor; |
146 | |
|
147 | 0 | if ( m_aHiddenControlModels.hasElements() ) |
148 | 0 | { |
149 | 0 | if ( lcl_fillDataFlavorEx( OControlExchange::getHiddenControlModelsFormatId(), aFlavor ) ) |
150 | 0 | m_aCurrentFormats.push_back( aFlavor ); |
151 | 0 | } |
152 | |
|
153 | 0 | if ( m_xFormsRoot.is() && m_aControlPaths.hasElements() ) |
154 | 0 | { |
155 | 0 | if ( lcl_fillDataFlavorEx( OControlExchange::getControlPathFormatId(), aFlavor ) ) |
156 | 0 | m_aCurrentFormats.push_back( aFlavor ); |
157 | 0 | } |
158 | |
|
159 | 0 | if ( !m_aSelectedEntries.empty() ) |
160 | 0 | { |
161 | 0 | if ( lcl_fillDataFlavorEx( OControlExchange::getFieldExchangeFormatId(), aFlavor ) ) |
162 | 0 | m_aCurrentFormats.push_back( aFlavor ); |
163 | 0 | } |
164 | 0 | } |
165 | | |
166 | | size_t OControlTransferData::onEntryRemoved(const weld::TreeView* pView, const weld::TreeIter* _pEntry) |
167 | 0 | { |
168 | 0 | auto aIter = std::find_if(m_aSelectedEntries.begin(), m_aSelectedEntries.end(), |
169 | 0 | [pView, _pEntry](const auto& rElem) { |
170 | 0 | return pView->iter_compare(*rElem, *_pEntry) == 0; |
171 | 0 | }); |
172 | 0 | if (aIter != m_aSelectedEntries.end()) |
173 | 0 | m_aSelectedEntries.erase(aIter); |
174 | |
|
175 | 0 | return m_aSelectedEntries.size(); |
176 | 0 | } |
177 | | |
178 | | void OControlTransferData::addSelectedEntry(std::unique_ptr<weld::TreeIter> xEntry) |
179 | 0 | { |
180 | 0 | m_aSelectedEntries.emplace(std::move(xEntry)); |
181 | 0 | } |
182 | | |
183 | | void OControlTransferData::setFocusEntry(bool _bFocusEntry) |
184 | 0 | { |
185 | 0 | m_bFocusEntry = _bFocusEntry; |
186 | 0 | } |
187 | | |
188 | | void OControlTransferData::addHiddenControlsFormat(const css::uno::Sequence< css::uno::Reference< css::uno::XInterface > >& seqInterfaces) |
189 | 0 | { |
190 | 0 | m_aHiddenControlModels = seqInterfaces; |
191 | 0 | } |
192 | | |
193 | | void OControlTransferData::buildPathFormat(const weld::TreeView* pTreeBox, const weld::TreeIter* pRoot) |
194 | 0 | { |
195 | 0 | m_aControlPaths.realloc(0); |
196 | |
|
197 | 0 | sal_Int32 nEntryCount = m_aSelectedEntries.size(); |
198 | 0 | if (nEntryCount == 0) |
199 | 0 | return; |
200 | | |
201 | 0 | m_aControlPaths.realloc(nEntryCount); |
202 | 0 | css::uno::Sequence<sal_uInt32>* pAllPaths = m_aControlPaths.getArray(); |
203 | 0 | for (const auto& rCurrentEntry : m_aSelectedEntries) |
204 | 0 | { |
205 | | // first we collect the path in an array |
206 | 0 | ::std::vector< sal_uInt32 > aCurrentPath; |
207 | |
|
208 | 0 | std::unique_ptr<weld::TreeIter> xLoop(pTreeBox->make_iterator(rCurrentEntry.get())); |
209 | 0 | while (pTreeBox->iter_compare(*xLoop, *pRoot) != 0) |
210 | 0 | { |
211 | 0 | aCurrentPath.push_back(pTreeBox->get_iter_index_in_parent(*xLoop)); |
212 | 0 | bool bLoop = pTreeBox->iter_parent(*xLoop); |
213 | 0 | assert(bLoop && "OControlTransferData::buildPathFormat: invalid root or entry !"); (void)bLoop; |
214 | 0 | } |
215 | | |
216 | | // then we can transfer it into css::uno::Sequence |
217 | 0 | Sequence<sal_uInt32>& rCurrentPath = *pAllPaths; |
218 | 0 | sal_Int32 nDepth = aCurrentPath.size(); |
219 | |
|
220 | 0 | rCurrentPath.realloc(nDepth); |
221 | 0 | sal_uInt32* pSeq = rCurrentPath.getArray(); |
222 | 0 | sal_Int32 j,k; |
223 | 0 | for (j = nDepth - 1, k = 0; k<nDepth; --j, ++k) |
224 | 0 | pSeq[j] = aCurrentPath[k]; |
225 | 0 | ++pAllPaths; |
226 | 0 | } |
227 | 0 | } |
228 | | |
229 | | void OControlTransferData::buildListFromPath(const weld::TreeView* pTreeBox, const weld::TreeIter* pRoot) |
230 | 0 | { |
231 | 0 | ListBoxEntrySet().swap(m_aSelectedEntries); |
232 | |
|
233 | 0 | for (const css::uno::Sequence<sal_uInt32>& rPaths : m_aControlPaths) |
234 | 0 | { |
235 | 0 | std::unique_ptr<weld::TreeIter> xSearch(pTreeBox->make_iterator(pRoot)); |
236 | 0 | for (const sal_uInt32 nThisPath : rPaths) |
237 | 0 | pTreeBox->iter_nth_child(*xSearch, nThisPath); |
238 | 0 | m_aSelectedEntries.emplace(std::move(xSearch)); |
239 | 0 | } |
240 | 0 | } |
241 | | |
242 | | OControlExchange::OControlExchange( ) |
243 | 0 | { |
244 | 0 | } |
245 | | |
246 | | bool OControlExchange::GetData( const DataFlavor& _rFlavor, const OUString& rDestDoc ) |
247 | 0 | { |
248 | 0 | const SotClipboardFormatId nFormatId = SotExchange::GetFormat( _rFlavor ); |
249 | |
|
250 | 0 | if ( getControlPathFormatId( ) == nFormatId ) |
251 | 0 | { |
252 | | // ugly. We have to pack all the info into one object |
253 | 0 | Sequence< Any > aCompleteInfo( 2 ); |
254 | 0 | OSL_ENSURE( m_xFormsRoot.is(), "OLocalExchange::GetData: invalid forms root for this format!" ); |
255 | 0 | aCompleteInfo.getArray()[ 0 ] <<= m_xFormsRoot; |
256 | 0 | aCompleteInfo.getArray()[ 1 ] <<= m_aControlPaths; |
257 | |
|
258 | 0 | SetAny( Any( aCompleteInfo ) ); |
259 | 0 | } |
260 | 0 | else if ( getHiddenControlModelsFormatId() == nFormatId ) |
261 | 0 | { |
262 | | // just need to transfer the models |
263 | 0 | SetAny( Any( m_aHiddenControlModels ) ); |
264 | 0 | } |
265 | 0 | else |
266 | 0 | return OLocalExchange::GetData(_rFlavor, rDestDoc); |
267 | | |
268 | 0 | return true; |
269 | 0 | } |
270 | | |
271 | | void OControlExchange::AddSupportedFormats() |
272 | 0 | { |
273 | 0 | if (m_bFocusEntry && !m_aSelectedEntries.empty()) |
274 | 0 | AddFormat(getFieldExchangeFormatId()); |
275 | |
|
276 | 0 | if (m_aControlPaths.hasElements()) |
277 | 0 | AddFormat(getControlPathFormatId()); |
278 | |
|
279 | 0 | if (m_aHiddenControlModels.hasElements()) |
280 | 0 | AddFormat(getHiddenControlModelsFormatId()); |
281 | 0 | } |
282 | | |
283 | | SotClipboardFormatId OControlExchange::getControlPathFormatId() |
284 | 0 | { |
285 | 0 | static SotClipboardFormatId s_nFormat = |
286 | 0 | SotExchange::RegisterFormatName(u"application/x-openoffice;windows_formatname=\"svxform.ControlPathExchange\""_ustr); |
287 | 0 | DBG_ASSERT(static_cast<SotClipboardFormatId>(-1) != s_nFormat, "OControlExchange::getControlPathFormatId: bad exchange id!"); |
288 | 0 | return s_nFormat; |
289 | 0 | } |
290 | | |
291 | | SotClipboardFormatId OControlExchange::getHiddenControlModelsFormatId() |
292 | 0 | { |
293 | 0 | static SotClipboardFormatId s_nFormat = |
294 | 0 | SotExchange::RegisterFormatName(u"application/x-openoffice;windows_formatname=\"svxform.HiddenControlModelsExchange\""_ustr); |
295 | 0 | DBG_ASSERT(static_cast<SotClipboardFormatId>(-1) != s_nFormat, "OControlExchange::getHiddenControlModelsFormatId: bad exchange id!"); |
296 | 0 | return s_nFormat; |
297 | 0 | } |
298 | | |
299 | | |
300 | | SotClipboardFormatId OControlExchange::getFieldExchangeFormatId() |
301 | 0 | { |
302 | 0 | static SotClipboardFormatId s_nFormat = |
303 | 0 | SotExchange::RegisterFormatName(u"application/x-openoffice;windows_formatname=\"svxform.FieldNameExchange\""_ustr); |
304 | 0 | DBG_ASSERT(static_cast<SotClipboardFormatId>(-1) != s_nFormat, "OControlExchange::getFieldExchangeFormatId: bad exchange id!"); |
305 | 0 | return s_nFormat; |
306 | 0 | } |
307 | | |
308 | | //= OControlExchangeHelper |
309 | | rtl::Reference<OLocalExchange> OControlExchangeHelper::createExchange() const |
310 | 0 | { |
311 | 0 | return new OControlExchange; |
312 | 0 | } |
313 | | |
314 | | OLocalExchangeHelper::OLocalExchangeHelper() |
315 | 0 | { |
316 | 0 | } |
317 | | |
318 | | OLocalExchangeHelper::~OLocalExchangeHelper() |
319 | 0 | { |
320 | 0 | implReset(); |
321 | 0 | } |
322 | | |
323 | | void OLocalExchangeHelper::copyToClipboard(const weld::Widget& rWidget) const |
324 | 0 | { |
325 | 0 | DBG_ASSERT( m_xTransferable.is(), "OLocalExchangeHelper::copyToClipboard: not prepared!" ); |
326 | 0 | m_xTransferable->copyToClipboard(rWidget, OLocalExchange::GrantAccess()); |
327 | 0 | } |
328 | | |
329 | | void OLocalExchangeHelper::implReset() |
330 | 0 | { |
331 | 0 | if (m_xTransferable.is()) |
332 | 0 | { |
333 | 0 | m_xTransferable->setClipboardListener( Link<OLocalExchange&,void>() ); |
334 | 0 | m_xTransferable.clear(); |
335 | 0 | } |
336 | 0 | } |
337 | | |
338 | | void OLocalExchangeHelper::prepareDrag( ) |
339 | 0 | { |
340 | 0 | DBG_ASSERT(!m_xTransferable.is() || !m_xTransferable->isDragging(), "OLocalExchangeHelper::prepareDrag: recursive DnD?"); |
341 | |
|
342 | 0 | implReset(); |
343 | 0 | m_xTransferable = createExchange(); |
344 | 0 | } |
345 | | } |
346 | | |
347 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |