tdf#130857 qt weld: Implement QtInstanceWidget::strip_mnemonic
[LibreOffice.git] / sc / source / ui / app / seltrans.cxx
blob29563cc55d461b59736f6914941fb5fe59c42867
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/svdograf.hxx>
27 #include <svx/svdouno.hxx>
28 #include <osl/diagnose.h>
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 && SdrInventor::FmForm == pUnoCtrl->GetObjInventor())
49 const 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( u"ButtonType"_ustr );
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 rtl::Reference<ScSelectionTransferObj> ScSelectionTransferObj::CreateFromView( ScTabView* pView )
72 rtl::Reference<ScSelectionTransferObj> pRet;
74 try
76 if ( pView )
78 ScSelectionTransferMode eMode = SC_SELTRANS_INVALID;
80 SdrView* pSdrView = pView->GetScDrawView();
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 SdrObjKind nSdrObjKind = pObj->GetObjIdentifier();
93 if ( nSdrObjKind == SdrObjKind::Graphic )
95 if ( static_cast<SdrGrafObj*>(pObj)->GetGraphic().GetType() == GraphicType::Bitmap )
96 eMode = SC_SELTRANS_DRAW_BITMAP;
97 else
98 eMode = SC_SELTRANS_DRAW_GRAPHIC;
100 else if ( nSdrObjKind == SdrObjKind::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 ScViewData& rViewData = pView->GetViewData();
113 const ScMarkData& rMark = rViewData.GetMarkData();
114 // allow MultiMarked because GetSimpleArea may be able to merge into a simple range
115 // (GetSimpleArea modifies a local copy of MarkData)
116 // Also allow simple filtered area.
117 if ( rMark.IsMarked() || rMark.IsMultiMarked() )
119 ScRange aRange;
120 ScMarkType eMarkType = rViewData.GetSimpleArea( aRange );
121 if (eMarkType == SC_MARK_SIMPLE || 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;
132 if ( eMode != SC_SELTRANS_INVALID )
133 pRet = new ScSelectionTransferObj( pView, eMode );
136 catch (...)
140 return pRet;
143 ScSelectionTransferObj::ScSelectionTransferObj( ScTabView* pSource, ScSelectionTransferMode eNewMode ) :
144 pView( pSource ),
145 eMode( eNewMode )
147 //! store range for StillValid
150 ScSelectionTransferObj::~ScSelectionTransferObj()
152 ScModule* pScMod = ScModule::get();
153 if (pScMod && pScMod->GetSelectionTransfer() == this)
155 // this is reached when the object wasn't really copied to the selection
156 // (CopyToSelection has no effect under Windows)
158 ForgetView();
159 pScMod->SetSelectionTransfer( nullptr );
162 OSL_ENSURE( !pView, "ScSelectionTransferObj dtor: ForgetView not called" );
165 void ScSelectionTransferObj::ForgetView()
167 pView = nullptr;
168 eMode = SC_SELTRANS_INVALID;
170 mxCellData.clear();
171 mxDrawData.clear();
174 void ScSelectionTransferObj::AddSupportedFormats()
176 // AddSupportedFormats must work without actually creating the
177 // "real" transfer object
179 switch (eMode)
181 case SC_SELTRANS_CELL:
182 case SC_SELTRANS_CELLS:
183 // same formats as in ScTransferObj::AddSupportedFormats
184 AddFormat( SotClipboardFormatId::EMBED_SOURCE );
185 AddFormat( SotClipboardFormatId::OBJECTDESCRIPTOR );
186 AddFormat( SotClipboardFormatId::GDIMETAFILE );
187 AddFormat( SotClipboardFormatId::PNG );
188 AddFormat( SotClipboardFormatId::BITMAP );
189 AddFormat( SotClipboardFormatId::HTML );
190 AddFormat( SotClipboardFormatId::SYLK );
191 AddFormat( SotClipboardFormatId::LINK );
192 AddFormat( SotClipboardFormatId::DIF );
193 AddFormat( SotClipboardFormatId::STRING );
194 AddFormat( SotClipboardFormatId::STRING_TSVC );
195 AddFormat( SotClipboardFormatId::RTF );
196 AddFormat( SotClipboardFormatId::RICHTEXT );
197 if ( eMode == SC_SELTRANS_CELL )
199 AddFormat( SotClipboardFormatId::EDITENGINE_ODF_TEXT_FLAT );
201 break;
203 // different graphic formats as in ScDrawTransferObj::AddSupportedFormats:
205 case SC_SELTRANS_DRAW_BITMAP:
206 AddFormat( SotClipboardFormatId::OBJECTDESCRIPTOR );
207 AddFormat( SotClipboardFormatId::SVXB );
208 AddFormat( SotClipboardFormatId::PNG );
209 AddFormat( SotClipboardFormatId::BITMAP );
210 AddFormat( SotClipboardFormatId::GDIMETAFILE );
211 break;
213 case SC_SELTRANS_DRAW_GRAPHIC:
214 AddFormat( SotClipboardFormatId::OBJECTDESCRIPTOR );
215 AddFormat( SotClipboardFormatId::SVXB );
216 AddFormat( SotClipboardFormatId::GDIMETAFILE );
217 AddFormat( SotClipboardFormatId::PNG );
218 AddFormat( SotClipboardFormatId::BITMAP );
219 break;
221 case SC_SELTRANS_DRAW_BOOKMARK:
222 AddFormat( SotClipboardFormatId::OBJECTDESCRIPTOR );
223 AddFormat( SotClipboardFormatId::SOLK );
224 AddFormat( SotClipboardFormatId::STRING );
225 AddFormat( SotClipboardFormatId::UNIFORMRESOURCELOCATOR );
226 AddFormat( SotClipboardFormatId::NETSCAPE_BOOKMARK );
227 AddFormat( SotClipboardFormatId::DRAWING );
228 break;
230 case SC_SELTRANS_DRAW_OLE:
231 AddFormat( SotClipboardFormatId::EMBED_SOURCE );
232 AddFormat( SotClipboardFormatId::OBJECTDESCRIPTOR );
233 AddFormat( SotClipboardFormatId::GDIMETAFILE );
234 break;
236 case SC_SELTRANS_DRAW_OTHER:
237 // other drawing objects
238 AddFormat( SotClipboardFormatId::EMBED_SOURCE );
239 AddFormat( SotClipboardFormatId::OBJECTDESCRIPTOR );
240 AddFormat( SotClipboardFormatId::DRAWING );
241 AddFormat( SotClipboardFormatId::PNG );
242 AddFormat( SotClipboardFormatId::BITMAP );
243 AddFormat( SotClipboardFormatId::GDIMETAFILE );
244 break;
246 default:
248 // added to avoid warnings
253 void ScSelectionTransferObj::CreateCellData()
255 OSL_ENSURE( !mxCellData.is(), "CreateCellData twice" );
256 if ( pView )
258 ScViewData& rViewData = pView->GetViewData();
259 ScMarkData aNewMark( rViewData.GetMarkData() ); // use local copy for MarkToSimple
260 aNewMark.MarkToSimple();
262 // similar to ScViewFunctionSet::BeginDrag
263 if ( aNewMark.IsMarked() && !aNewMark.IsMultiMarked() )
265 ScDocShell* pDocSh = rViewData.GetDocShell();
267 const ScRange& aSelRange = aNewMark.GetMarkArea();
268 ScDocShellRef aDragShellRef;
269 if ( pDocSh->GetDocument().HasOLEObjectsInArea( aSelRange, &aNewMark ) )
271 aDragShellRef = new ScDocShell; // DocShell needs a Ref immediately
272 aDragShellRef->DoInitNew();
274 ScDrawLayer::SetGlobalDrawPersist( aDragShellRef.get() );
276 ScDocumentUniquePtr pClipDoc(new ScDocument( SCDOCMODE_CLIP ));
277 // bApi = sal_True -> no error messages
278 // #i18364# bStopEdit = sal_False -> don't end edit mode
279 // (this may be called from pasting into the edit line)
280 bool bCopied = rViewData.GetView()->CopyToClip( pClipDoc.get(), false, true, true, false );
282 ScDrawLayer::SetGlobalDrawPersist(nullptr);
284 if ( bCopied )
286 TransferableObjectDescriptor aObjDesc;
287 pDocSh->FillTransferableObjectDescriptor( aObjDesc );
288 aObjDesc.maDisplayName = pDocSh->GetMedium()->GetURLObject().GetURLNoPass();
289 // maSize is set in ScTransferObj ctor
291 rtl::Reference<ScTransferObj> xTransferObj = new ScTransferObj( std::move(pClipDoc), std::move(aObjDesc) );
293 // SetDragHandlePos is not used - there is no mouse position
294 //? xTransferObj->SetVisibleTab( nTab );
296 xTransferObj->SetDrawPersist(aDragShellRef); // keep persist for ole objects alive
298 xTransferObj->SetDragSource( pDocSh, aNewMark );
300 mxCellData = std::move(xTransferObj);
304 OSL_ENSURE( mxCellData.is(), "can't create CellData" );
307 void ScSelectionTransferObj::CreateDrawData()
309 OSL_ENSURE( !mxDrawData.is(), "CreateDrawData twice" );
310 if ( pView )
312 // similar to ScDrawView::BeginDrag
314 ScDrawView* pDrawView = pView->GetScDrawView();
315 if ( pDrawView )
317 bool bAnyOle, bOneOle;
318 const SdrMarkList& rMarkList = pDrawView->GetMarkedObjectList();
319 ScDrawView::CheckOle( rMarkList, bAnyOle, bOneOle );
321 ScDocShellRef aDragShellRef;
322 if (bAnyOle)
324 aDragShellRef = new ScDocShell; // Without Ref the DocShell does not live
325 aDragShellRef->DoInitNew();
328 ScDrawLayer::SetGlobalDrawPersist( aDragShellRef.get() );
329 std::unique_ptr<SdrModel> pModel(pDrawView->CreateMarkedObjModel());
330 ScDrawLayer::SetGlobalDrawPersist(nullptr);
332 ScViewData& rViewData = pView->GetViewData();
333 ScDocShell* pDocSh = rViewData.GetDocShell();
335 TransferableObjectDescriptor aObjDesc;
336 pDocSh->FillTransferableObjectDescriptor( aObjDesc );
337 aObjDesc.maDisplayName = pDocSh->GetMedium()->GetURLObject().GetURLNoPass();
338 // maSize is set in ScDrawTransferObj ctor
340 rtl::Reference<ScDrawTransferObj> pTransferObj = new ScDrawTransferObj( std::move(pModel), pDocSh, std::move(aObjDesc) );
342 pTransferObj->SetDrawPersist(aDragShellRef); // keep persist for ole objects alive
343 pTransferObj->SetDragSource( pDrawView ); // copies selection
345 mxDrawData = std::move(pTransferObj);
348 OSL_ENSURE( mxDrawData.is(), "can't create DrawData" );
351 ScTransferObj* ScSelectionTransferObj::GetCellData()
353 if ( !mxCellData.is() && ( eMode == SC_SELTRANS_CELL || eMode == SC_SELTRANS_CELLS ) )
354 CreateCellData();
355 return mxCellData.get();
358 ScDrawTransferObj* ScSelectionTransferObj::GetDrawData()
360 if ( !mxDrawData.is() && ( eMode == SC_SELTRANS_DRAW_BITMAP || eMode == SC_SELTRANS_DRAW_GRAPHIC ||
361 eMode == SC_SELTRANS_DRAW_BOOKMARK || eMode == SC_SELTRANS_DRAW_OLE ||
362 eMode == SC_SELTRANS_DRAW_OTHER ) )
363 CreateDrawData();
364 return mxDrawData.get();
367 bool ScSelectionTransferObj::GetData(
368 const css::datatransfer::DataFlavor& rFlavor, const OUString& rDestDoc )
370 bool bOK = false;
372 uno::Reference<datatransfer::XTransferable> xSource;
373 switch (eMode)
375 case SC_SELTRANS_CELL:
376 case SC_SELTRANS_CELLS:
377 xSource = GetCellData();
378 break;
379 case SC_SELTRANS_DRAW_BITMAP:
380 case SC_SELTRANS_DRAW_GRAPHIC:
381 case SC_SELTRANS_DRAW_BOOKMARK:
382 case SC_SELTRANS_DRAW_OLE:
383 case SC_SELTRANS_DRAW_OTHER:
384 xSource = GetDrawData();
385 break;
386 default:
388 // added to avoid warnings
392 if ( xSource.is() )
394 TransferableDataHelper aHelper( xSource );
395 uno::Any aAny = aHelper.GetAny(rFlavor, rDestDoc);
396 bOK = SetAny( aAny );
399 return bOK;
402 void ScSelectionTransferObj::ObjectReleased()
404 // called when another selection is set from outside
406 ForgetView();
408 ScModule* pScMod = ScModule::get();
409 if ( pScMod->GetSelectionTransfer() == this )
410 pScMod->SetSelectionTransfer( nullptr );
412 TransferableHelper::ObjectReleased();
415 sal_Bool SAL_CALL ScSelectionTransferObj::isComplex()
417 switch (eMode)
419 case SC_SELTRANS_CELL:
420 case SC_SELTRANS_CELLS:
421 return false;
422 default:
423 return true;
427 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */