Version 5.2.6.1, tag libreoffice-5.2.6.1
[LibreOffice.git] / sc / source / ui / app / seltrans.cxx
blobead955f31c2d20ee1c05f4ece5ed88e414181702
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 <com/sun/star/beans/XPropertySet.hpp>
21 #include <com/sun/star/beans/XPropertySetInfo.hpp>
22 #include <com/sun/star/form/FormButtonType.hpp>
24 #include <tools/urlobj.hxx>
25 #include <sfx2/docfile.hxx>
26 #include <svx/fmglob.hxx>
27 #include <svx/svdograf.hxx>
28 #include <svx/svdouno.hxx>
30 #include "seltrans.hxx"
31 #include "transobj.hxx"
32 #include "drwtrans.hxx"
33 #include "scmod.hxx"
34 #include "dbfunc.hxx"
35 #include "docsh.hxx"
36 #include "drawview.hxx"
37 #include "drwlayer.hxx"
38 #include "markdata.hxx"
40 using namespace com::sun::star;
42 static bool lcl_IsURLButton( SdrObject* pObject )
44 bool bRet = false;
46 SdrUnoObj* pUnoCtrl = dynamic_cast<SdrUnoObj*>( pObject );
47 if (pUnoCtrl && FmFormInventor == pUnoCtrl->GetObjInventor())
49 uno::Reference<awt::XControlModel> xControlModel = pUnoCtrl->GetUnoControlModel();
50 OSL_ENSURE( xControlModel.is(), "uno control without model" );
51 if ( xControlModel.is() )
53 uno::Reference< beans::XPropertySet > xPropSet( xControlModel, uno::UNO_QUERY );
54 uno::Reference< beans::XPropertySetInfo > xInfo = xPropSet->getPropertySetInfo();
56 OUString sPropButtonType( "ButtonType" );
57 if(xInfo->hasPropertyByName( sPropButtonType ))
59 uno::Any aAny = xPropSet->getPropertyValue( sPropButtonType );
60 form::FormButtonType eTmp;
61 if ( (aAny >>= eTmp) && eTmp == form::FormButtonType_URL )
62 bRet = true;
67 return bRet;
70 ScSelectionTransferObj* ScSelectionTransferObj::CreateFromView( ScTabView* pView )
72 ScSelectionTransferObj* pRet = nullptr;
74 try
76 if ( pView )
78 ScSelectionTransferMode eMode = SC_SELTRANS_INVALID;
80 SdrView* pSdrView = pView->GetSdrView();
81 if ( pSdrView )
83 // handle selection on drawing layer
84 const SdrMarkList& rMarkList = pSdrView->GetMarkedObjectList();
85 const size_t nMarkCount = rMarkList.GetMarkCount();
86 if ( nMarkCount )
88 if ( nMarkCount == 1 )
90 SdrObject* pObj = rMarkList.GetMark(0)->GetMarkedSdrObj();
91 sal_uInt16 nSdrObjKind = pObj->GetObjIdentifier();
93 if ( nSdrObjKind == OBJ_GRAF )
95 if ( static_cast<SdrGrafObj*>(pObj)->GetGraphic().GetType() == GRAPHIC_BITMAP )
96 eMode = SC_SELTRANS_DRAW_BITMAP;
97 else
98 eMode = SC_SELTRANS_DRAW_GRAPHIC;
100 else if ( nSdrObjKind == OBJ_OLE2 )
101 eMode = SC_SELTRANS_DRAW_OLE;
102 else if ( lcl_IsURLButton( pObj ) )
103 eMode = SC_SELTRANS_DRAW_BOOKMARK;
106 if ( eMode == SC_SELTRANS_INVALID )
107 eMode = SC_SELTRANS_DRAW_OTHER; // something selected but no special selection
110 if ( eMode == SC_SELTRANS_INVALID ) // no drawing object selected
112 ScRange aRange;
113 ScViewData& rViewData = pView->GetViewData();
114 const ScMarkData& rMark = rViewData.GetMarkData();
115 // allow MultiMarked because GetSimpleArea may be able to merge into a simple range
116 // (GetSimpleArea modifies a local copy of MarkData)
117 // Also allow simple filtered area.
118 ScMarkType eMarkType;
119 if ( ( rMark.IsMarked() || rMark.IsMultiMarked() ) &&
120 (((eMarkType = rViewData.GetSimpleArea( aRange )) == SC_MARK_SIMPLE) ||
121 (eMarkType == SC_MARK_SIMPLE_FILTERED)) )
123 // only for "real" selection, cursor alone isn't used
124 if ( aRange.aStart == aRange.aEnd )
125 eMode = SC_SELTRANS_CELL;
126 else
127 eMode = SC_SELTRANS_CELLS;
131 if ( eMode != SC_SELTRANS_INVALID )
132 pRet = new ScSelectionTransferObj( pView, eMode );
135 catch (...)
139 return pRet;
142 ScSelectionTransferObj::ScSelectionTransferObj( ScTabView* pSource, ScSelectionTransferMode eNewMode ) :
143 pView( pSource ),
144 eMode( eNewMode ),
145 pCellData( nullptr ),
146 pDrawData( nullptr )
148 //! store range for StillValid
151 ScSelectionTransferObj::~ScSelectionTransferObj()
153 ScModule* pScMod = SC_MOD();
154 if ( pScMod->GetSelectionTransfer() == this )
156 // this is reached when the object wasn't really copied to the selection
157 // (CopyToSelection has no effect under Windows)
159 ForgetView();
160 pScMod->SetSelectionTransfer( nullptr );
163 OSL_ENSURE( !pView, "ScSelectionTransferObj dtor: ForgetView not called" );
166 void ScSelectionTransferObj::ForgetView()
168 pView = nullptr;
169 eMode = SC_SELTRANS_INVALID;
171 if (pCellData)
173 pCellData->release();
174 pCellData = nullptr;
176 if (pDrawData)
178 pDrawData->release();
179 pDrawData = nullptr;
183 void ScSelectionTransferObj::AddSupportedFormats()
185 // AddSupportedFormats must work without actually creating the
186 // "real" transfer object
188 switch (eMode)
190 case SC_SELTRANS_CELL:
191 case SC_SELTRANS_CELLS:
192 // same formats as in ScTransferObj::AddSupportedFormats
193 AddFormat( SotClipboardFormatId::EMBED_SOURCE );
194 AddFormat( SotClipboardFormatId::OBJECTDESCRIPTOR );
195 AddFormat( SotClipboardFormatId::GDIMETAFILE );
196 AddFormat( SotClipboardFormatId::PNG );
197 AddFormat( SotClipboardFormatId::BITMAP );
198 AddFormat( SotClipboardFormatId::HTML );
199 AddFormat( SotClipboardFormatId::SYLK );
200 AddFormat( SotClipboardFormatId::LINK );
201 AddFormat( SotClipboardFormatId::DIF );
202 AddFormat( SotClipboardFormatId::STRING );
203 AddFormat( SotClipboardFormatId::RTF );
204 if ( eMode == SC_SELTRANS_CELL )
205 AddFormat( SotClipboardFormatId::EDITENGINE );
206 break;
208 // different graphic formats as in ScDrawTransferObj::AddSupportedFormats:
210 case SC_SELTRANS_DRAW_BITMAP:
211 AddFormat( SotClipboardFormatId::OBJECTDESCRIPTOR );
212 AddFormat( SotClipboardFormatId::SVXB );
213 AddFormat( SotClipboardFormatId::PNG );
214 AddFormat( SotClipboardFormatId::BITMAP );
215 AddFormat( SotClipboardFormatId::GDIMETAFILE );
216 break;
218 case SC_SELTRANS_DRAW_GRAPHIC:
219 AddFormat( SotClipboardFormatId::OBJECTDESCRIPTOR );
220 AddFormat( SotClipboardFormatId::SVXB );
221 AddFormat( SotClipboardFormatId::GDIMETAFILE );
222 AddFormat( SotClipboardFormatId::PNG );
223 AddFormat( SotClipboardFormatId::BITMAP );
224 break;
226 case SC_SELTRANS_DRAW_BOOKMARK:
227 AddFormat( SotClipboardFormatId::OBJECTDESCRIPTOR );
228 AddFormat( SotClipboardFormatId::SOLK );
229 AddFormat( SotClipboardFormatId::STRING );
230 AddFormat( SotClipboardFormatId::UNIFORMRESOURCELOCATOR );
231 AddFormat( SotClipboardFormatId::NETSCAPE_BOOKMARK );
232 AddFormat( SotClipboardFormatId::DRAWING );
233 break;
235 case SC_SELTRANS_DRAW_OLE:
236 AddFormat( SotClipboardFormatId::EMBED_SOURCE );
237 AddFormat( SotClipboardFormatId::OBJECTDESCRIPTOR );
238 AddFormat( SotClipboardFormatId::GDIMETAFILE );
239 break;
241 case SC_SELTRANS_DRAW_OTHER:
242 // other drawing objects
243 AddFormat( SotClipboardFormatId::EMBED_SOURCE );
244 AddFormat( SotClipboardFormatId::OBJECTDESCRIPTOR );
245 AddFormat( SotClipboardFormatId::DRAWING );
246 AddFormat( SotClipboardFormatId::PNG );
247 AddFormat( SotClipboardFormatId::BITMAP );
248 AddFormat( SotClipboardFormatId::GDIMETAFILE );
249 break;
251 default:
253 // added to avoid warnings
258 void ScSelectionTransferObj::CreateCellData()
260 OSL_ENSURE( !pCellData, "CreateCellData twice" );
261 if ( pView )
263 ScViewData& rViewData = pView->GetViewData();
264 ScMarkData aNewMark( rViewData.GetMarkData() ); // use local copy for MarkToSimple
265 aNewMark.MarkToSimple();
267 // similar to ScViewFunctionSet::BeginDrag
268 if ( aNewMark.IsMarked() && !aNewMark.IsMultiMarked() )
270 ScDocShell* pDocSh = rViewData.GetDocShell();
272 ScRange aSelRange;
273 aNewMark.GetMarkArea( aSelRange );
274 ScDocShellRef aDragShellRef;
275 if ( pDocSh->GetDocument().HasOLEObjectsInArea( aSelRange, &aNewMark ) )
277 aDragShellRef = new ScDocShell; // DocShell needs a Ref immediately
278 aDragShellRef->DoInitNew();
280 ScDrawLayer::SetGlobalDrawPersist(aDragShellRef);
282 ScDocument* pClipDoc = new ScDocument( SCDOCMODE_CLIP );
283 // bApi = sal_True -> no error messages
284 // #i18364# bStopEdit = sal_False -> don't end edit mode
285 // (this may be called from pasting into the edit line)
286 bool bCopied = rViewData.GetView()->CopyToClip( pClipDoc, false, true, true, false );
288 ScDrawLayer::SetGlobalDrawPersist(nullptr);
290 if ( bCopied )
292 TransferableObjectDescriptor aObjDesc;
293 pDocSh->FillTransferableObjectDescriptor( aObjDesc );
294 aObjDesc.maDisplayName = pDocSh->GetMedium()->GetURLObject().GetURLNoPass();
295 // maSize is set in ScTransferObj ctor
297 ScTransferObj* pTransferObj = new ScTransferObj( pClipDoc, aObjDesc );
298 uno::Reference<datatransfer::XTransferable> xTransferable( pTransferObj );
300 // SetDragHandlePos is not used - there is no mouse position
301 //? pTransferObj->SetVisibleTab( nTab );
303 SfxObjectShellRef aPersistRef( aDragShellRef );
304 pTransferObj->SetDrawPersist( aPersistRef ); // keep persist for ole objects alive
306 pTransferObj->SetDragSource( pDocSh, aNewMark );
308 pCellData = pTransferObj;
309 pCellData->acquire(); // keep ref count up - released in ForgetView
311 else
312 delete pClipDoc;
315 OSL_ENSURE( pCellData, "can't create CellData" );
318 void ScSelectionTransferObj::CreateDrawData()
320 OSL_ENSURE( !pDrawData, "CreateDrawData twice" );
321 if ( pView )
323 // similar to ScDrawView::BeginDrag
325 ScDrawView* pDrawView = pView->GetScDrawView();
326 if ( pDrawView )
328 bool bAnyOle, bOneOle;
329 const SdrMarkList& rMarkList = pDrawView->GetMarkedObjectList();
330 ScDrawView::CheckOle( rMarkList, bAnyOle, bOneOle );
332 ScDocShellRef aDragShellRef;
333 if (bAnyOle)
335 aDragShellRef = new ScDocShell; // Without Ref the DocShell does not live
336 aDragShellRef->DoInitNew();
339 ScDrawLayer::SetGlobalDrawPersist(aDragShellRef);
340 SdrModel* pModel = pDrawView->GetMarkedObjModel();
341 ScDrawLayer::SetGlobalDrawPersist(nullptr);
343 ScViewData& rViewData = pView->GetViewData();
344 ScDocShell* pDocSh = rViewData.GetDocShell();
346 TransferableObjectDescriptor aObjDesc;
347 pDocSh->FillTransferableObjectDescriptor( aObjDesc );
348 aObjDesc.maDisplayName = pDocSh->GetMedium()->GetURLObject().GetURLNoPass();
349 // maSize is set in ScDrawTransferObj ctor
351 ScDrawTransferObj* pTransferObj = new ScDrawTransferObj( pModel, pDocSh, aObjDesc );
352 uno::Reference<datatransfer::XTransferable> xTransferable( pTransferObj );
354 SfxObjectShellRef aPersistRef( aDragShellRef );
355 pTransferObj->SetDrawPersist( aPersistRef ); // keep persist for ole objects alive
356 pTransferObj->SetDragSource( pDrawView ); // copies selection
358 pDrawData = pTransferObj;
359 pDrawData->acquire(); // keep ref count up - released in ForgetView
362 OSL_ENSURE( pDrawData, "can't create DrawData" );
365 ScTransferObj* ScSelectionTransferObj::GetCellData()
367 if ( !pCellData && ( eMode == SC_SELTRANS_CELL || eMode == SC_SELTRANS_CELLS ) )
368 CreateCellData();
369 return pCellData;
372 ScDrawTransferObj* ScSelectionTransferObj::GetDrawData()
374 if ( !pDrawData && ( eMode == SC_SELTRANS_DRAW_BITMAP || eMode == SC_SELTRANS_DRAW_GRAPHIC ||
375 eMode == SC_SELTRANS_DRAW_BOOKMARK || eMode == SC_SELTRANS_DRAW_OLE ||
376 eMode == SC_SELTRANS_DRAW_OTHER ) )
377 CreateDrawData();
378 return pDrawData;
381 bool ScSelectionTransferObj::GetData(
382 const css::datatransfer::DataFlavor& rFlavor, const OUString& rDestDoc )
384 bool bOK = false;
386 uno::Reference<datatransfer::XTransferable> xSource;
387 switch (eMode)
389 case SC_SELTRANS_CELL:
390 case SC_SELTRANS_CELLS:
391 xSource = GetCellData();
392 break;
393 case SC_SELTRANS_DRAW_BITMAP:
394 case SC_SELTRANS_DRAW_GRAPHIC:
395 case SC_SELTRANS_DRAW_BOOKMARK:
396 case SC_SELTRANS_DRAW_OLE:
397 case SC_SELTRANS_DRAW_OTHER:
398 xSource = GetDrawData();
399 break;
400 default:
402 // added to avoid warnings
406 if ( xSource.is() )
408 TransferableDataHelper aHelper( xSource );
409 uno::Any aAny = aHelper.GetAny(rFlavor, rDestDoc);
410 bOK = SetAny( aAny, rFlavor );
413 return bOK;
416 void ScSelectionTransferObj::ObjectReleased()
418 // called when another selection is set from outside
420 ForgetView();
422 ScModule* pScMod = SC_MOD();
423 if ( pScMod->GetSelectionTransfer() == this )
424 pScMod->SetSelectionTransfer( nullptr );
426 TransferableHelper::ObjectReleased();
429 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */