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/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>
36 using namespace ::com::sun::star::uno
;
37 using namespace ::com::sun::star::datatransfer
;
43 OLocalExchange::OLocalExchange( )
45 ,m_bClipboardOwner( false )
50 void OLocalExchange::copyToClipboard( Window
* _pWindow
, const GrantAccess
& )
52 if ( m_bClipboardOwner
)
53 { // simulate a lostOwnership to notify parties interested in
54 if ( m_aClipboardListener
.IsSet() )
55 m_aClipboardListener
.Call( this );
58 m_bClipboardOwner
= true;
59 CopyToClipboard( _pWindow
);
63 void OLocalExchange::clear()
65 if ( isClipboardOwner() )
69 Reference
< clipboard::XClipboard
> xClipBoard( getOwnClipboard() );
70 if ( xClipBoard
.is() )
71 xClipBoard
->setContents( NULL
, NULL
);
73 catch( const Exception
& )
75 DBG_UNHANDLED_EXCEPTION();
77 m_bClipboardOwner
= false;
82 void SAL_CALL
OLocalExchange::lostOwnership( const Reference
< clipboard::XClipboard
>& _rxClipboard
, const Reference
< XTransferable
>& _rxTrans
) throw(RuntimeException
, std::exception
)
84 TransferableHelper::implCallOwnLostOwnership( _rxClipboard
, _rxTrans
);
85 m_bClipboardOwner
= false;
87 if ( m_aClipboardListener
.IsSet() )
88 m_aClipboardListener
.Call( this );
92 void OLocalExchange::startDrag( Window
* _pWindow
, sal_Int8 _nDragSourceActions
, const GrantAccess
& )
95 StartDrag( _pWindow
, _nDragSourceActions
);
99 void OLocalExchange::DragFinished( sal_Int8 nDropAction
)
101 TransferableHelper::DragFinished( nDropAction
);
106 bool OLocalExchange::hasFormat( const DataFlavorExVector
& _rFormats
, sal_uInt32 _nFormatId
)
108 DataFlavorExVector::const_iterator aSearch
;
110 for ( aSearch
= _rFormats
.begin(); aSearch
!= _rFormats
.end(); ++aSearch
)
111 if ( aSearch
->mnSotId
== _nFormatId
)
114 return aSearch
!= _rFormats
.end();
118 bool OLocalExchange::GetData( const ::com::sun::star::datatransfer::DataFlavor
& /*_rFlavor*/ )
120 return false; // do not have any formats by default
124 //= OControlTransferData
127 OControlTransferData::OControlTransferData( )
128 :m_pFocusEntry( NULL
)
133 OControlTransferData::OControlTransferData( const Reference
< XTransferable
>& _rxTransferable
)
134 :m_pFocusEntry( NULL
)
136 TransferableDataHelper
aExchangedData( _rxTransferable
);
138 // try the formats we know
139 if ( OControlExchange::hasControlPathFormat( aExchangedData
.GetDataFlavorExVector() ) )
140 { // paths to the controls, relative to a root
141 Sequence
< Any
> aControlPathData
;
142 if ( aExchangedData
.GetAny( OControlExchange::getControlPathFormatId() ) >>= aControlPathData
)
144 DBG_ASSERT( aControlPathData
.getLength() >= 2, "OControlTransferData::OControlTransferData: invalid data for the control path format!" );
145 if ( aControlPathData
.getLength() >= 2 )
147 aControlPathData
[0] >>= m_xFormsRoot
;
148 aControlPathData
[1] >>= m_aControlPaths
;
153 OSL_FAIL( "OControlTransferData::OControlTransferData: invalid data for the control path format (2)!" );
156 if ( OControlExchange::hasHiddenControlModelsFormat( aExchangedData
.GetDataFlavorExVector() ) )
157 { // sequence of models of hidden controls
158 aExchangedData
.GetAny( OControlExchange::getHiddenControlModelsFormatId() ) >>= m_aHiddenControlModels
;
165 static bool lcl_fillDataFlavorEx( SotFormatStringId nId
, DataFlavorEx
& _rFlavor
)
167 _rFlavor
.mnSotId
= nId
;
168 return SotExchange::GetFormatDataFlavor( _rFlavor
.mnSotId
, _rFlavor
);
172 void OControlTransferData::updateFormats( )
174 m_aCurrentFormats
.clear();
175 m_aCurrentFormats
.reserve( 3 );
177 DataFlavorEx aFlavor
;
179 if ( m_aHiddenControlModels
.getLength() )
181 if ( lcl_fillDataFlavorEx( OControlExchange::getHiddenControlModelsFormatId(), aFlavor
) )
182 m_aCurrentFormats
.push_back( aFlavor
);
185 if ( m_xFormsRoot
.is() && m_aControlPaths
.getLength() )
187 if ( lcl_fillDataFlavorEx( OControlExchange::getControlPathFormatId(), aFlavor
) )
188 m_aCurrentFormats
.push_back( aFlavor
);
191 if ( !m_aSelectedEntries
.empty() )
193 if ( lcl_fillDataFlavorEx( OControlExchange::getFieldExchangeFormatId(), aFlavor
) )
194 m_aCurrentFormats
.push_back( aFlavor
);
199 size_t OControlTransferData::onEntryRemoved( SvTreeListEntry
* _pEntry
)
201 m_aSelectedEntries
.erase( _pEntry
);
202 return m_aSelectedEntries
.size();
206 void OControlTransferData::addSelectedEntry( SvTreeListEntry
* _pEntry
)
208 m_aSelectedEntries
.insert( _pEntry
);
212 void OControlTransferData::setFocusEntry( SvTreeListEntry
* _pFocusEntry
)
214 m_pFocusEntry
= _pFocusEntry
;
218 void OControlTransferData::addHiddenControlsFormat(const ::com::sun::star::uno::Sequence
< ::com::sun::star::uno::Reference
< ::com::sun::star::uno::XInterface
> >& seqInterfaces
)
220 m_aHiddenControlModels
= seqInterfaces
;
224 void OControlTransferData::buildPathFormat(SvTreeListBox
* pTreeBox
, SvTreeListEntry
* pRoot
)
226 m_aControlPaths
.realloc(0);
228 sal_Int32 nEntryCount
= m_aSelectedEntries
.size();
229 if (nEntryCount
== 0)
232 m_aControlPaths
.realloc(nEntryCount
);
233 ::com::sun::star::uno::Sequence
<sal_uInt32
>* pAllPaths
= m_aControlPaths
.getArray();
234 for ( ListBoxEntrySet::const_iterator loop
= m_aSelectedEntries
.begin();
235 loop
!= m_aSelectedEntries
.end();
239 // erst mal sammeln wir den Pfad in einem Array ein
240 ::std::vector
< sal_uInt32
> aCurrentPath
;
241 SvTreeListEntry
* pCurrentEntry
= *loop
;
243 SvTreeListEntry
* pLoop
= pCurrentEntry
;
244 while (pLoop
!= pRoot
)
246 aCurrentPath
.push_back(pLoop
->GetChildListPos());
247 pLoop
= pTreeBox
->GetParent(pLoop
);
248 DBG_ASSERT((pLoop
!= NULL
) || (pRoot
== 0), "OControlTransferData::buildPathFormat: invalid root or entry !");
249 // pLoop == NULL heisst, dass ich am oberen Ende angelangt bin, dann sollte das Ganze abbrechen, was nur bei pRoot == NULL der Fall sein wird
252 // dann koennen wir ihn in die ::com::sun::star::uno::Sequence uebertragen
253 Sequence
<sal_uInt32
>& rCurrentPath
= *pAllPaths
;
254 sal_Int32 nDepth
= aCurrentPath
.size();
256 rCurrentPath
.realloc(nDepth
);
257 sal_uInt32
* pSeq
= rCurrentPath
.getArray();
259 for (j
= nDepth
- 1, k
= 0; k
<nDepth
; --j
, ++k
)
260 pSeq
[j
] = aCurrentPath
[k
];
265 void OControlTransferData::buildListFromPath(SvTreeListBox
* pTreeBox
, SvTreeListEntry
* pRoot
)
267 ListBoxEntrySet aEmpty
;
268 m_aSelectedEntries
.swap( aEmpty
);
270 sal_Int32 nControls
= m_aControlPaths
.getLength();
271 const ::com::sun::star::uno::Sequence
<sal_uInt32
>* pPaths
= m_aControlPaths
.getConstArray();
272 for (sal_Int32 i
=0; i
<nControls
; ++i
)
274 sal_Int32 nThisPatLength
= pPaths
[i
].getLength();
275 const sal_uInt32
* pThisPath
= pPaths
[i
].getConstArray();
276 SvTreeListEntry
* pSearch
= pRoot
;
277 for (sal_Int32 j
=0; j
<nThisPatLength
; ++j
)
278 pSearch
= pTreeBox
->GetEntry(pSearch
, pThisPath
[j
]);
280 m_aSelectedEntries
.insert( pSearch
);
288 OControlExchange::OControlExchange( )
293 bool OControlExchange::GetData( const DataFlavor
& _rFlavor
)
295 const sal_uInt32 nFormatId
= SotExchange::GetFormat( _rFlavor
);
297 if ( getControlPathFormatId( ) == nFormatId
)
299 // ugly. We have to pack all the info into one object
300 Sequence
< Any
> aCompleteInfo( 2 );
301 OSL_ENSURE( m_xFormsRoot
.is(), "OLocalExchange::GetData: invalid forms root for this format!" );
302 aCompleteInfo
.getArray()[ 0 ] <<= m_xFormsRoot
;
303 aCompleteInfo
.getArray()[ 1 ] <<= m_aControlPaths
;
305 SetAny( makeAny( aCompleteInfo
), _rFlavor
);
307 else if ( getHiddenControlModelsFormatId() == nFormatId
)
309 // just need to transfer the models
310 SetAny( makeAny( m_aHiddenControlModels
), _rFlavor
);
313 return OLocalExchange::GetData( _rFlavor
);
319 void OControlExchange::AddSupportedFormats()
321 if (m_pFocusEntry
&& !m_aSelectedEntries
.empty())
322 AddFormat(getFieldExchangeFormatId());
324 if (m_aControlPaths
.getLength())
325 AddFormat(getControlPathFormatId());
327 if (m_aHiddenControlModels
.getLength())
328 AddFormat(getHiddenControlModelsFormatId());
332 sal_uInt32
OControlExchange::getControlPathFormatId()
334 static sal_uInt32 s_nFormat
= (sal_uInt32
)-1;
335 if ((sal_uInt32
)-1 == s_nFormat
)
337 s_nFormat
= SotExchange::RegisterFormatName(OUString("application/x-openoffice;windows_formatname=\"svxform.ControlPathExchange\""));
338 DBG_ASSERT((sal_uInt32
)-1 != s_nFormat
, "OControlExchange::getControlPathFormatId: bad exchange id!");
344 sal_uInt32
OControlExchange::getHiddenControlModelsFormatId()
346 static sal_uInt32 s_nFormat
= (sal_uInt32
)-1;
347 if ((sal_uInt32
)-1 == s_nFormat
)
349 s_nFormat
= SotExchange::RegisterFormatName(OUString("application/x-openoffice;windows_formatname=\"svxform.HiddenControlModelsExchange\""));
350 DBG_ASSERT((sal_uInt32
)-1 != s_nFormat
, "OControlExchange::getHiddenControlModelsFormatId: bad exchange id!");
356 sal_uInt32
OControlExchange::getFieldExchangeFormatId()
358 static sal_uInt32 s_nFormat
= (sal_uInt32
)-1;
359 if ((sal_uInt32
)-1 == s_nFormat
)
361 s_nFormat
= SotExchange::RegisterFormatName(OUString("application/x-openoffice;windows_formatname=\"svxform.FieldNameExchange\""));
362 DBG_ASSERT((sal_uInt32
)-1 != s_nFormat
, "OControlExchange::getFieldExchangeFormatId: bad exchange id!");
368 //= OControlExchangeHelper
370 OLocalExchange
* OControlExchangeHelper::createExchange() const
372 return new OControlExchange
;
376 //= OLocalExchangeHelper
379 OLocalExchangeHelper::OLocalExchangeHelper(Window
* _pDragSource
)
380 :m_pDragSource(_pDragSource
)
381 ,m_pTransferable(NULL
)
386 OLocalExchangeHelper::~OLocalExchangeHelper()
392 void OLocalExchangeHelper::startDrag( sal_Int8 nDragSourceActions
)
394 DBG_ASSERT(m_pTransferable
, "OLocalExchangeHelper::startDrag: not prepared!");
395 m_pTransferable
->startDrag( m_pDragSource
, nDragSourceActions
, OLocalExchange::GrantAccess() );
399 void OLocalExchangeHelper::copyToClipboard( ) const
401 DBG_ASSERT( m_pTransferable
, "OLocalExchangeHelper::copyToClipboard: not prepared!" );
402 m_pTransferable
->copyToClipboard( m_pDragSource
, OLocalExchange::GrantAccess() );
406 void OLocalExchangeHelper::implReset()
410 m_pTransferable
->setClipboardListener( Link() );
411 m_pTransferable
->release();
412 m_pTransferable
= NULL
;
417 void OLocalExchangeHelper::prepareDrag( )
419 DBG_ASSERT(!m_pTransferable
|| !m_pTransferable
->isDragging(), "OLocalExchangeHelper::prepareDrag: recursive DnD?");
422 m_pTransferable
= createExchange();
423 m_pTransferable
->acquire();
430 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */