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/storage.hxx>
23 #include <svl/itempool.hxx>
24 
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>
30 
31 
32 namespace svxform
33 {
34 
35 
36  using namespace ::com::sun::star::uno;
37  using namespace ::com::sun::star::datatransfer;
38 
40  :m_bDragging( false )
41  ,m_bClipboardOwner( false )
42  {
43  }
44 
45 
47  {
48  if ( m_bClipboardOwner )
49  { // simulate a lostOwnership to notify parties interested in
50  m_aClipboardListener.Call( *this );
51  }
52 
53  m_bClipboardOwner = true;
54  CopyToClipboard( _pWindow );
55  }
56 
57 
59  {
60  if ( isClipboardOwner() )
61  {
62  try
63  {
64  Reference< clipboard::XClipboard > xClipBoard( getOwnClipboard() );
65  if ( xClipBoard.is() )
66  xClipBoard->setContents( nullptr, nullptr );
67  }
68  catch( const Exception& )
69  {
71  }
72  m_bClipboardOwner = false;
73  }
74  }
75 
76 
77  void SAL_CALL OLocalExchange::lostOwnership( const Reference< clipboard::XClipboard >& _rxClipboard, const Reference< XTransferable >& _rxTrans )
78  {
79  TransferableHelper::implCallOwnLostOwnership( _rxClipboard, _rxTrans );
80  m_bClipboardOwner = false;
81 
82  m_aClipboardListener.Call( *this );
83  }
84 
85 
86  void OLocalExchange::startDrag( vcl::Window* _pWindow, sal_Int8 _nDragSourceActions, const GrantAccess& )
87  {
88  m_bDragging = true;
89  StartDrag( _pWindow, _nDragSourceActions );
90  }
91 
92 
94  {
95  TransferableHelper::DragFinished( nDropAction );
96  m_bDragging = false;
97  }
98 
99 
101  {
102  return std::any_of(_rFormats.begin(), _rFormats.end(),
103  [&_nFormatId](const DataFlavorEx& rFormat) { return rFormat.mnSotId == _nFormatId; });
104  }
105 
106 
107  bool OLocalExchange::GetData( const css::datatransfer::DataFlavor& /*_rFlavor*/, const OUString& /*rDestDoc*/ )
108  {
109  return false; // do not have any formats by default
110  }
111 
113  :m_pFocusEntry( nullptr )
114  {
115  }
116 
117 
118  OControlTransferData::OControlTransferData( const Reference< XTransferable >& _rxTransferable )
119  :m_pFocusEntry( nullptr )
120  {
121  TransferableDataHelper aExchangedData( _rxTransferable );
122 
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 )
128  {
129  DBG_ASSERT( aControlPathData.getLength() >= 2, "OControlTransferData::OControlTransferData: invalid data for the control path format!" );
130  if ( aControlPathData.getLength() >= 2 )
131  {
132  aControlPathData[0] >>= m_xFormsRoot;
133  aControlPathData[1] >>= m_aControlPaths;
134  }
135  }
136  else
137  {
138  OSL_FAIL( "OControlTransferData::OControlTransferData: invalid data for the control path format (2)!" );
139  }
140  }
141  if ( OControlExchange::hasHiddenControlModelsFormat( aExchangedData.GetDataFlavorExVector() ) )
142  { // sequence of models of hidden controls
143  aExchangedData.GetAny(OControlExchange::getHiddenControlModelsFormatId(), OUString()) >>= m_aHiddenControlModels;
144  }
145 
146  updateFormats( );
147  }
148 
149 
151  {
152  _rFlavor.mnSotId = nId;
153  return SotExchange::GetFormatDataFlavor( _rFlavor.mnSotId, _rFlavor );
154  }
155 
156 
158  {
159  m_aCurrentFormats.clear();
160  m_aCurrentFormats.reserve( 3 );
161 
162  DataFlavorEx aFlavor;
163 
164  if ( m_aHiddenControlModels.hasElements() )
165  {
167  m_aCurrentFormats.push_back( aFlavor );
168  }
169 
170  if ( m_xFormsRoot.is() && m_aControlPaths.hasElements() )
171  {
173  m_aCurrentFormats.push_back( aFlavor );
174  }
175 
176  if ( !m_aSelectedEntries.empty() )
177  {
179  m_aCurrentFormats.push_back( aFlavor );
180  }
181  }
182 
183 
185  {
186  m_aSelectedEntries.erase( _pEntry );
187  return m_aSelectedEntries.size();
188  }
189 
190 
192  {
193  m_aSelectedEntries.insert( _pEntry );
194  }
195 
196 
198  {
199  m_pFocusEntry = _pFocusEntry;
200  }
201 
202 
203  void OControlTransferData::addHiddenControlsFormat(const css::uno::Sequence< css::uno::Reference< css::uno::XInterface > >& seqInterfaces)
204  {
205  m_aHiddenControlModels = seqInterfaces;
206  }
207 
208 
210  {
211  m_aControlPaths.realloc(0);
212 
213  sal_Int32 nEntryCount = m_aSelectedEntries.size();
214  if (nEntryCount == 0)
215  return;
216 
217  m_aControlPaths.realloc(nEntryCount);
218  css::uno::Sequence<sal_uInt32>* pAllPaths = m_aControlPaths.getArray();
219  for (SvTreeListEntry* pCurrentEntry : m_aSelectedEntries)
220  {
221  // first we collect the path in an array
222  ::std::vector< sal_uInt32 > aCurrentPath;
223 
224  SvTreeListEntry* pLoop = pCurrentEntry;
225  while (pLoop != pRoot)
226  {
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
232  }
233 
234  // then we can transfer it into css::uno::Sequence
235  Sequence<sal_uInt32>& rCurrentPath = *pAllPaths;
236  sal_Int32 nDepth = aCurrentPath.size();
237 
238  rCurrentPath.realloc(nDepth);
239  sal_uInt32* pSeq = rCurrentPath.getArray();
240  sal_Int32 j,k;
241  for (j = nDepth - 1, k = 0; k<nDepth; --j, ++k)
242  pSeq[j] = aCurrentPath[k];
243  ++pAllPaths;
244  }
245  }
246 
247 
249  {
250  ListBoxEntrySet aEmpty;
251  m_aSelectedEntries.swap( aEmpty );
252 
253  for (const css::uno::Sequence<sal_uInt32>& rPaths : std::as_const(m_aControlPaths))
254  {
255  SvTreeListEntry* pSearch = pRoot;
256  for (const sal_uInt32 nThisPath : rPaths)
257  pSearch = pTreeBox->GetEntry(pSearch, nThisPath);
258 
259  m_aSelectedEntries.insert( pSearch );
260  }
261  }
262 
264  {
265  }
266 
267 
268  bool OControlExchange::GetData( const DataFlavor& _rFlavor, const OUString& rDestDoc )
269  {
270  const SotClipboardFormatId nFormatId = SotExchange::GetFormat( _rFlavor );
271 
272  if ( getControlPathFormatId( ) == nFormatId )
273  {
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;
279 
280  SetAny( makeAny( aCompleteInfo ) );
281  }
282  else if ( getHiddenControlModelsFormatId() == nFormatId )
283  {
284  // just need to transfer the models
286  }
287  else
288  return OLocalExchange::GetData(_rFlavor, rDestDoc);
289 
290  return true;
291  }
292 
293 
295  {
296  if (m_pFocusEntry && !m_aSelectedEntries.empty())
298 
299  if (m_aControlPaths.hasElements())
301 
302  if (m_aHiddenControlModels.hasElements())
304  }
305 
306 
308  {
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!");
312  return s_nFormat;
313  }
314 
315 
317  {
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!");
321  return s_nFormat;
322  }
323 
324 
326  {
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!");
330  return s_nFormat;
331  }
332 
333 
334  //= OControlExchangeHelper
335 
337  {
338  return new OControlExchange;
339  }
340 
342  :m_pDragSource(_pDragSource)
343  {
344  }
345 
346 
348  {
349  implReset();
350  }
351 
352 
353  void OLocalExchangeHelper::startDrag( sal_Int8 nDragSourceActions )
354  {
355  DBG_ASSERT(m_xTransferable.is(), "OLocalExchangeHelper::startDrag: not prepared!");
356  m_xTransferable->startDrag( m_pDragSource, nDragSourceActions, OLocalExchange::GrantAccess() );
357  }
358 
359 
361  {
362  DBG_ASSERT( m_xTransferable.is(), "OLocalExchangeHelper::copyToClipboard: not prepared!" );
364  }
365 
366 
368  {
369  if (m_xTransferable.is())
370  {
371  m_xTransferable->setClipboardListener( Link<OLocalExchange&,void>() );
372  m_xTransferable.clear();
373  }
374  }
375 
376 
378  {
379  DBG_ASSERT(!m_xTransferable.is() || !m_xTransferable->isDragging(), "OLocalExchangeHelper::prepareDrag: recursive DnD?");
380 
381  implReset();
383  }
384 
385 
386 }
387 
388 
389 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual void AddSupportedFormats() override
Definition: fmexch.cxx:294
SvTreeListEntry * m_pFocusEntry
Definition: fmexch.hxx:138
static SotClipboardFormatId RegisterFormatName(const OUString &rName)
static SotClipboardFormatId getHiddenControlModelsFormatId()
Definition: fmexch.cxx:316
SVX_DLLPRIVATE void implReset()
Definition: fmexch.cxx:367
void startDrag(sal_Int8 nDragSourceActions)
Definition: fmexch.cxx:353
signed char sal_Int8
::std::vector< DataFlavorEx > DataFlavorExVector
Link< OLocalExchange &, void > m_aClipboardListener
Definition: fmexch.hxx:51
css::uno::Sequence< css::uno::Sequence< sal_uInt32 > > m_aControlPaths
Definition: fmexch.hxx:130
virtual void DragFinished(sal_Int8 nDropAction)
virtual bool GetData(const css::datatransfer::DataFlavor &rFlavor, const OUString &rDestDoc) override
Definition: fmexch.cxx:268
::std::set< SvTreeListEntry * > ListBoxEntrySet
Definition: fmexch.hxx:43
css::uno::Sequence< css::uno::Reference< css::uno::XInterface > > m_aHiddenControlModels
Definition: fmexch.hxx:132
VclPtr< vcl::Window > m_pDragSource
Definition: fmexch.hxx:96
void AddFormat(SotClipboardFormatId nFormat)
static bool lcl_fillDataFlavorEx(SotClipboardFormatId nId, DataFlavorEx &_rFlavor)
Definition: fmexch.cxx:150
SotClipboardFormatId
void copyToClipboard() const
Definition: fmexch.cxx:360
static SotClipboardFormatId getFieldExchangeFormatId()
Definition: fmexch.cxx:325
virtual OLocalExchange * createExchange() const override
Definition: fmexch.cxx:336
#define DBG_UNHANDLED_EXCEPTION(...)
ListBoxEntrySet m_aSelectedEntries
Definition: fmexch.hxx:128
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 copyToClipboard(vcl::Window *_pWindow, const GrantAccess &)
Definition: fmexch.cxx:46
const SvTreeListEntry * GetParent(const SvTreeListEntry *pEntry) const
void addHiddenControlsFormat(const css::uno::Sequence< css::uno::Reference< css::uno::XInterface > > &seqInterfaces)
Definition: fmexch.cxx:203
rtl::Reference< OLocalExchange > m_xTransferable
Definition: fmexch.hxx:97
void buildPathFormat(SvTreeListBox const *pTreeBox, SvTreeListEntry const *pRoot)
Definition: fmexch.cxx:209
const css::uno::Reference< css::datatransfer::clipboard::XClipboard > & getOwnClipboard() const
void StartDrag(vcl::Window *pWindow, sal_Int8 nDragSourceActions)
void CopyToClipboard(const css::uno::Reference< css::datatransfer::clipboard::XClipboard > &rClipboard) const
sal_uLong GetChildListPos() const
static bool hasHiddenControlModelsFormat(const DataFlavorExVector &_rFormats)
Definition: fmexch.hxx:241
virtual void SAL_CALL lostOwnership(const css::uno::Reference< css::datatransfer::clipboard::XClipboard > &_rxClipboard, const css::uno::Reference< css::datatransfer::XTransferable > &_rxTrans) override
Definition: fmexch.cxx:77
void startDrag(vcl::Window *pWindow, sal_Int8 nDragSourceActions, const GrantAccess &)
Definition: fmexch.cxx:86
static SotClipboardFormatId GetFormat(const css::datatransfer::DataFlavor &rFlavor)
void setFocusEntry(SvTreeListEntry *_pFocusEntry)
Definition: fmexch.cxx:197
static bool hasControlPathFormat(const DataFlavorExVector &_rFormats)
Definition: fmexch.hxx:236
size_t onEntryRemoved(SvTreeListEntry *_pEntry)
notifies the data transfer object that a certain entry has been removed from the owning tree ...
Definition: fmexch.cxx:184
OLocalExchangeHelper(vcl::Window *_pDragSource)
Definition: fmexch.cxx:341
bool isClipboardOwner() const
Definition: fmexch.hxx:65
virtual SVX_DLLPRIVATE OLocalExchange * createExchange() const =0
virtual bool GetData(const css::datatransfer::DataFlavor &rFlavor, const OUString &rDestDoc) override
Definition: fmexch.cxx:107
SvTreeListEntry * GetEntry(SvTreeListEntry *pParent, sal_uLong nPos) const
class FmSearchEngine - Impl class for FmSearchDialog
void buildListFromPath(SvTreeListBox const *pTreeBox, SvTreeListEntry *pRoot)
Definition: fmexch.cxx:248
SotClipboardFormatId mnSotId
static SotClipboardFormatId getControlPathFormatId()
Definition: fmexch.cxx:307
virtual void DragFinished(sal_Int8 nDropAction) override
Definition: fmexch.cxx:93
bool SetAny(const css::uno::Any &rAny)
css::uno::Reference< css::form::XForms > m_xFormsRoot
Definition: fmexch.hxx:135
static bool hasFormat(const DataFlavorExVector &_rFormats, SotClipboardFormatId _nFormatId)
Definition: fmexch.cxx:100
void addSelectedEntry(SvTreeListEntry *_pEntry)
Definition: fmexch.cxx:191
static bool GetFormatDataFlavor(SotClipboardFormatId nFormat, css::datatransfer::DataFlavor &rFlavor)
css::uno::Any SAL_CALL makeAny(const SharedUNOComponent< INTERFACE, COMPONENT > &value)
DataFlavorExVector m_aCurrentFormats
Definition: fmexch.hxx:125