bump product version to 5.0.4.1
[LibreOffice.git] / svx / source / form / fmexch.cxx
blob7d2f2671e63431c7993fe5792cf5e9f9bdf8cf31
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 .
20 #include "fmexch.hxx"
22 #include <sot/storage.hxx>
23 #include <svl/itempool.hxx>
25 #include <sot/formats.hxx>
26 #include <svtools/treelistbox.hxx>
27 #include "svtools/treelistentry.hxx"
28 #include <tools/diagnose_ex.h>
32 namespace svxform
36 using namespace ::com::sun::star::uno;
37 using namespace ::com::sun::star::datatransfer;
39 OLocalExchange::OLocalExchange( )
40 :m_bDragging( false )
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 if ( m_aClipboardListener.IsSet() )
51 m_aClipboardListener.Call( this );
54 m_bClipboardOwner = true;
55 CopyToClipboard( _pWindow );
59 void OLocalExchange::clear()
61 if ( isClipboardOwner() )
63 try
65 Reference< clipboard::XClipboard > xClipBoard( getOwnClipboard() );
66 if ( xClipBoard.is() )
67 xClipBoard->setContents( NULL, NULL );
69 catch( const Exception& )
71 DBG_UNHANDLED_EXCEPTION();
73 m_bClipboardOwner = false;
78 void SAL_CALL OLocalExchange::lostOwnership( const Reference< clipboard::XClipboard >& _rxClipboard, const Reference< XTransferable >& _rxTrans ) throw(RuntimeException, std::exception)
80 TransferableHelper::implCallOwnLostOwnership( _rxClipboard, _rxTrans );
81 m_bClipboardOwner = false;
83 if ( m_aClipboardListener.IsSet() )
84 m_aClipboardListener.Call( this );
88 void OLocalExchange::startDrag( vcl::Window* _pWindow, sal_Int8 _nDragSourceActions, const GrantAccess& )
90 m_bDragging = true;
91 StartDrag( _pWindow, _nDragSourceActions );
95 void OLocalExchange::DragFinished( sal_Int8 nDropAction )
97 TransferableHelper::DragFinished( nDropAction );
98 m_bDragging = false;
102 bool OLocalExchange::hasFormat( const DataFlavorExVector& _rFormats, SotClipboardFormatId _nFormatId )
104 DataFlavorExVector::const_iterator aSearch;
106 for ( aSearch = _rFormats.begin(); aSearch != _rFormats.end(); ++aSearch )
107 if ( aSearch->mnSotId == _nFormatId )
108 break;
110 return aSearch != _rFormats.end();
114 bool OLocalExchange::GetData( const css::datatransfer::DataFlavor& /*_rFlavor*/, const OUString& /*rDestDoc*/ )
116 return false; // do not have any formats by default
119 OControlTransferData::OControlTransferData( )
120 :m_pFocusEntry( NULL )
125 OControlTransferData::OControlTransferData( const Reference< XTransferable >& _rxTransferable )
126 :m_pFocusEntry( NULL )
128 TransferableDataHelper aExchangedData( _rxTransferable );
130 // try the formats we know
131 if ( OControlExchange::hasControlPathFormat( aExchangedData.GetDataFlavorExVector() ) )
132 { // paths to the controls, relative to a root
133 Sequence< Any > aControlPathData;
134 if ( aExchangedData.GetAny(OControlExchange::getControlPathFormatId(), OUString()) >>= aControlPathData )
136 DBG_ASSERT( aControlPathData.getLength() >= 2, "OControlTransferData::OControlTransferData: invalid data for the control path format!" );
137 if ( aControlPathData.getLength() >= 2 )
139 aControlPathData[0] >>= m_xFormsRoot;
140 aControlPathData[1] >>= m_aControlPaths;
143 else
145 OSL_FAIL( "OControlTransferData::OControlTransferData: invalid data for the control path format (2)!" );
148 if ( OControlExchange::hasHiddenControlModelsFormat( aExchangedData.GetDataFlavorExVector() ) )
149 { // sequence of models of hidden controls
150 aExchangedData.GetAny(OControlExchange::getHiddenControlModelsFormatId(), OUString()) >>= m_aHiddenControlModels;
153 updateFormats( );
157 static bool lcl_fillDataFlavorEx( SotClipboardFormatId nId, DataFlavorEx& _rFlavor )
159 _rFlavor.mnSotId = nId;
160 return SotExchange::GetFormatDataFlavor( _rFlavor.mnSotId, _rFlavor );
164 void OControlTransferData::updateFormats( )
166 m_aCurrentFormats.clear();
167 m_aCurrentFormats.reserve( 3 );
169 DataFlavorEx aFlavor;
171 if ( m_aHiddenControlModels.getLength() )
173 if ( lcl_fillDataFlavorEx( OControlExchange::getHiddenControlModelsFormatId(), aFlavor ) )
174 m_aCurrentFormats.push_back( aFlavor );
177 if ( m_xFormsRoot.is() && m_aControlPaths.getLength() )
179 if ( lcl_fillDataFlavorEx( OControlExchange::getControlPathFormatId(), aFlavor ) )
180 m_aCurrentFormats.push_back( aFlavor );
183 if ( !m_aSelectedEntries.empty() )
185 if ( lcl_fillDataFlavorEx( OControlExchange::getFieldExchangeFormatId(), aFlavor ) )
186 m_aCurrentFormats.push_back( aFlavor );
191 size_t OControlTransferData::onEntryRemoved( SvTreeListEntry* _pEntry )
193 m_aSelectedEntries.erase( _pEntry );
194 return m_aSelectedEntries.size();
198 void OControlTransferData::addSelectedEntry( SvTreeListEntry* _pEntry )
200 m_aSelectedEntries.insert( _pEntry );
204 void OControlTransferData::setFocusEntry( SvTreeListEntry* _pFocusEntry )
206 m_pFocusEntry = _pFocusEntry;
210 void OControlTransferData::addHiddenControlsFormat(const ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > >& seqInterfaces)
212 m_aHiddenControlModels = seqInterfaces;
216 void OControlTransferData::buildPathFormat(SvTreeListBox* pTreeBox, SvTreeListEntry* pRoot)
218 m_aControlPaths.realloc(0);
220 sal_Int32 nEntryCount = m_aSelectedEntries.size();
221 if (nEntryCount == 0)
222 return;
224 m_aControlPaths.realloc(nEntryCount);
225 ::com::sun::star::uno::Sequence<sal_uInt32>* pAllPaths = m_aControlPaths.getArray();
226 for ( ListBoxEntrySet::const_iterator loop = m_aSelectedEntries.begin();
227 loop != m_aSelectedEntries.end();
228 ++loop, ++pAllPaths
231 // erst mal sammeln wir den Pfad in einem Array ein
232 ::std::vector< sal_uInt32 > aCurrentPath;
233 SvTreeListEntry* pCurrentEntry = *loop;
235 SvTreeListEntry* pLoop = pCurrentEntry;
236 while (pLoop != pRoot)
238 aCurrentPath.push_back(pLoop->GetChildListPos());
239 pLoop = pTreeBox->GetParent(pLoop);
240 DBG_ASSERT((pLoop != NULL) || (pRoot == 0), "OControlTransferData::buildPathFormat: invalid root or entry !");
241 // pLoop == NULL heisst, dass ich am oberen Ende angelangt bin, dann sollte das Ganze abbrechen, was nur bei pRoot == NULL der Fall sein wird
244 // dann koennen wir ihn in die ::com::sun::star::uno::Sequence uebertragen
245 Sequence<sal_uInt32>& rCurrentPath = *pAllPaths;
246 sal_Int32 nDepth = aCurrentPath.size();
248 rCurrentPath.realloc(nDepth);
249 sal_uInt32* pSeq = rCurrentPath.getArray();
250 sal_Int32 j,k;
251 for (j = nDepth - 1, k = 0; k<nDepth; --j, ++k)
252 pSeq[j] = aCurrentPath[k];
257 void OControlTransferData::buildListFromPath(SvTreeListBox* pTreeBox, SvTreeListEntry* pRoot)
259 ListBoxEntrySet aEmpty;
260 m_aSelectedEntries.swap( aEmpty );
262 sal_Int32 nControls = m_aControlPaths.getLength();
263 const ::com::sun::star::uno::Sequence<sal_uInt32>* pPaths = m_aControlPaths.getConstArray();
264 for (sal_Int32 i=0; i<nControls; ++i)
266 sal_Int32 nThisPatLength = pPaths[i].getLength();
267 const sal_uInt32* pThisPath = pPaths[i].getConstArray();
268 SvTreeListEntry* pSearch = pRoot;
269 for (sal_Int32 j=0; j<nThisPatLength; ++j)
270 pSearch = pTreeBox->GetEntry(pSearch, pThisPath[j]);
272 m_aSelectedEntries.insert( pSearch );
276 OControlExchange::OControlExchange( )
281 bool OControlExchange::GetData( const DataFlavor& _rFlavor, const OUString& rDestDoc )
283 const SotClipboardFormatId nFormatId = SotExchange::GetFormat( _rFlavor );
285 if ( getControlPathFormatId( ) == nFormatId )
287 // ugly. We have to pack all the info into one object
288 Sequence< Any > aCompleteInfo( 2 );
289 OSL_ENSURE( m_xFormsRoot.is(), "OLocalExchange::GetData: invalid forms root for this format!" );
290 aCompleteInfo.getArray()[ 0 ] <<= m_xFormsRoot;
291 aCompleteInfo.getArray()[ 1 ] <<= m_aControlPaths;
293 SetAny( makeAny( aCompleteInfo ), _rFlavor );
295 else if ( getHiddenControlModelsFormatId() == nFormatId )
297 // just need to transfer the models
298 SetAny( makeAny( m_aHiddenControlModels ), _rFlavor );
300 else
301 return OLocalExchange::GetData(_rFlavor, rDestDoc);
303 return true;
307 void OControlExchange::AddSupportedFormats()
309 if (m_pFocusEntry && !m_aSelectedEntries.empty())
310 AddFormat(getFieldExchangeFormatId());
312 if (m_aControlPaths.getLength())
313 AddFormat(getControlPathFormatId());
315 if (m_aHiddenControlModels.getLength())
316 AddFormat(getHiddenControlModelsFormatId());
320 SotClipboardFormatId OControlExchange::getControlPathFormatId()
322 static SotClipboardFormatId s_nFormat = static_cast<SotClipboardFormatId>(-1);
323 if (static_cast<SotClipboardFormatId>(-1) == s_nFormat)
325 s_nFormat = SotExchange::RegisterFormatName(OUString("application/x-openoffice;windows_formatname=\"svxform.ControlPathExchange\""));
326 DBG_ASSERT(static_cast<SotClipboardFormatId>(-1) != s_nFormat, "OControlExchange::getControlPathFormatId: bad exchange id!");
328 return s_nFormat;
332 SotClipboardFormatId OControlExchange::getHiddenControlModelsFormatId()
334 static SotClipboardFormatId s_nFormat = static_cast<SotClipboardFormatId>(-1);
335 if (static_cast<SotClipboardFormatId>(-1) == s_nFormat)
337 s_nFormat = SotExchange::RegisterFormatName(OUString("application/x-openoffice;windows_formatname=\"svxform.HiddenControlModelsExchange\""));
338 DBG_ASSERT(static_cast<SotClipboardFormatId>(-1) != s_nFormat, "OControlExchange::getHiddenControlModelsFormatId: bad exchange id!");
340 return s_nFormat;
344 SotClipboardFormatId OControlExchange::getFieldExchangeFormatId()
346 static SotClipboardFormatId s_nFormat = static_cast<SotClipboardFormatId>(-1);
347 if (static_cast<SotClipboardFormatId>(-1) == s_nFormat)
349 s_nFormat = SotExchange::RegisterFormatName(OUString("application/x-openoffice;windows_formatname=\"svxform.FieldNameExchange\""));
350 DBG_ASSERT(static_cast<SotClipboardFormatId>(-1) != s_nFormat, "OControlExchange::getFieldExchangeFormatId: bad exchange id!");
352 return s_nFormat;
356 //= OControlExchangeHelper
358 OLocalExchange* OControlExchangeHelper::createExchange() const
360 return new OControlExchange;
363 OLocalExchangeHelper::OLocalExchangeHelper(vcl::Window* _pDragSource)
364 :m_pDragSource(_pDragSource)
365 ,m_pTransferable(NULL)
370 OLocalExchangeHelper::~OLocalExchangeHelper()
372 implReset();
376 void OLocalExchangeHelper::startDrag( sal_Int8 nDragSourceActions )
378 DBG_ASSERT(m_pTransferable, "OLocalExchangeHelper::startDrag: not prepared!");
379 m_pTransferable->startDrag( m_pDragSource, nDragSourceActions, OLocalExchange::GrantAccess() );
383 void OLocalExchangeHelper::copyToClipboard( ) const
385 DBG_ASSERT( m_pTransferable, "OLocalExchangeHelper::copyToClipboard: not prepared!" );
386 m_pTransferable->copyToClipboard( m_pDragSource, OLocalExchange::GrantAccess() );
390 void OLocalExchangeHelper::implReset()
392 if (m_pTransferable)
394 m_pTransferable->setClipboardListener( Link<>() );
395 m_pTransferable->release();
396 m_pTransferable = NULL;
401 void OLocalExchangeHelper::prepareDrag( )
403 DBG_ASSERT(!m_pTransferable || !m_pTransferable->isDragging(), "OLocalExchangeHelper::prepareDrag: recursive DnD?");
405 implReset();
406 m_pTransferable = createExchange();
407 m_pTransferable->acquire();
414 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */