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 .
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>
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
)
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
)
70 rtl::Reference
<ScSelectionTransferObj
> ScSelectionTransferObj::CreateFromView( ScTabView
* pView
)
72 rtl::Reference
<ScSelectionTransferObj
> pRet
;
78 ScSelectionTransferMode eMode
= SC_SELTRANS_INVALID
;
80 SdrView
* pSdrView
= pView
->GetScDrawView();
83 // handle selection on drawing layer
84 const SdrMarkList
& rMarkList
= pSdrView
->GetMarkedObjectList();
85 const size_t nMarkCount
= rMarkList
.GetMarkCount();
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
;
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() )
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
;
127 eMode
= SC_SELTRANS_CELLS
;
132 if ( eMode
!= SC_SELTRANS_INVALID
)
133 pRet
= new ScSelectionTransferObj( pView
, eMode
);
143 ScSelectionTransferObj::ScSelectionTransferObj( ScTabView
* pSource
, ScSelectionTransferMode 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)
159 pScMod
->SetSelectionTransfer( nullptr );
162 OSL_ENSURE( !pView
, "ScSelectionTransferObj dtor: ForgetView not called" );
165 void ScSelectionTransferObj::ForgetView()
168 eMode
= SC_SELTRANS_INVALID
;
174 void ScSelectionTransferObj::AddSupportedFormats()
176 // AddSupportedFormats must work without actually creating the
177 // "real" transfer object
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
);
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
);
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
);
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
);
230 case SC_SELTRANS_DRAW_OLE
:
231 AddFormat( SotClipboardFormatId::EMBED_SOURCE
);
232 AddFormat( SotClipboardFormatId::OBJECTDESCRIPTOR
);
233 AddFormat( SotClipboardFormatId::GDIMETAFILE
);
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
);
248 // added to avoid warnings
253 void ScSelectionTransferObj::CreateCellData()
255 OSL_ENSURE( !mxCellData
.is(), "CreateCellData twice" );
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);
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" );
312 // similar to ScDrawView::BeginDrag
314 ScDrawView
* pDrawView
= pView
->GetScDrawView();
317 bool bAnyOle
, bOneOle
;
318 const SdrMarkList
& rMarkList
= pDrawView
->GetMarkedObjectList();
319 ScDrawView::CheckOle( rMarkList
, bAnyOle
, bOneOle
);
321 ScDocShellRef aDragShellRef
;
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
) )
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
) )
364 return mxDrawData
.get();
367 bool ScSelectionTransferObj::GetData(
368 const css::datatransfer::DataFlavor
& rFlavor
, const OUString
& rDestDoc
)
372 uno::Reference
<datatransfer::XTransferable
> xSource
;
375 case SC_SELTRANS_CELL
:
376 case SC_SELTRANS_CELLS
:
377 xSource
= GetCellData();
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();
388 // added to avoid warnings
394 TransferableDataHelper
aHelper( xSource
);
395 uno::Any aAny
= aHelper
.GetAny(rFlavor
, rDestDoc
);
396 bOK
= SetAny( aAny
);
402 void ScSelectionTransferObj::ObjectReleased()
404 // called when another selection is set from outside
408 ScModule
* pScMod
= ScModule::get();
409 if ( pScMod
->GetSelectionTransfer() == this )
410 pScMod
->SetSelectionTransfer( nullptr );
412 TransferableHelper::ObjectReleased();
415 sal_Bool SAL_CALL
ScSelectionTransferObj::isComplex()
419 case SC_SELTRANS_CELL
:
420 case SC_SELTRANS_CELLS
:
427 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */