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