Stop leaking all ScPostIt instances.
[LibreOffice.git] / sc / source / ui / app / seltrans.cxx
blob47c2bfd7860a8a0add7150166342683b081e9ee1
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 // -----------------------------------------------------------------------
44 static sal_Bool lcl_IsURLButton( SdrObject* pObject )
46 sal_Bool bRet = false;
48 SdrUnoObj* pUnoCtrl = PTR_CAST(SdrUnoObj, pObject);
49 if (pUnoCtrl && FmFormInventor == pUnoCtrl->GetObjInventor())
51 uno::Reference<awt::XControlModel> xControlModel = pUnoCtrl->GetUnoControlModel();
52 OSL_ENSURE( xControlModel.is(), "uno control without model" );
53 if ( xControlModel.is() )
55 uno::Reference< beans::XPropertySet > xPropSet( xControlModel, uno::UNO_QUERY );
56 uno::Reference< beans::XPropertySetInfo > xInfo = xPropSet->getPropertySetInfo();
58 OUString sPropButtonType( "ButtonType" );
59 if(xInfo->hasPropertyByName( sPropButtonType ))
61 uno::Any aAny = xPropSet->getPropertyValue( sPropButtonType );
62 form::FormButtonType eTmp;
63 if ( (aAny >>= eTmp) && eTmp == form::FormButtonType_URL )
64 bRet = sal_True;
69 return bRet;
73 ScSelectionTransferObj* ScSelectionTransferObj::CreateFromView( ScTabView* pView )
75 ScSelectionTransferObj* pRet = NULL;
77 if ( pView )
79 ScSelectionTransferMode eMode = SC_SELTRANS_INVALID;
81 SdrView* pSdrView = pView->GetSdrView();
82 if ( pSdrView )
84 // handle selection on drawing layer
85 const SdrMarkList& rMarkList = pSdrView->GetMarkedObjectList();
86 sal_uLong nMarkCount = rMarkList.GetMarkCount();
87 if ( nMarkCount )
89 if ( nMarkCount == 1 )
91 SdrObject* pObj = rMarkList.GetMark(0)->GetMarkedSdrObj();
92 sal_uInt16 nSdrObjKind = pObj->GetObjIdentifier();
94 if ( nSdrObjKind == OBJ_GRAF )
96 if ( ((SdrGrafObj*)pObj)->GetGraphic().GetType() == GRAPHIC_BITMAP )
97 eMode = SC_SELTRANS_DRAW_BITMAP;
98 else
99 eMode = SC_SELTRANS_DRAW_GRAPHIC;
101 else if ( nSdrObjKind == OBJ_OLE2 )
102 eMode = SC_SELTRANS_DRAW_OLE;
103 else if ( lcl_IsURLButton( pObj ) )
104 eMode = SC_SELTRANS_DRAW_BOOKMARK;
107 if ( eMode == SC_SELTRANS_INVALID )
108 eMode = SC_SELTRANS_DRAW_OTHER; // something selected but no special selection
111 if ( eMode == SC_SELTRANS_INVALID ) // no drawing object selected
113 ScRange aRange;
114 ScViewData* pViewData = pView->GetViewData();
115 const ScMarkData& rMark = pViewData->GetMarkData();
116 // allow MultiMarked because GetSimpleArea may be able to merge into a simple range
117 // (GetSimpleArea modifies a local copy of MarkData)
118 // Also allow simple filtered area.
119 ScMarkType eMarkType;
120 if ( ( rMark.IsMarked() || rMark.IsMultiMarked() ) &&
121 (((eMarkType = pViewData->GetSimpleArea( aRange )) == SC_MARK_SIMPLE) ||
122 (eMarkType == SC_MARK_SIMPLE_FILTERED)) )
124 // only for "real" selection, cursor alone isn't used
125 if ( aRange.aStart == aRange.aEnd )
126 eMode = SC_SELTRANS_CELL;
127 else
128 eMode = SC_SELTRANS_CELLS;
132 if ( eMode != SC_SELTRANS_INVALID )
133 pRet = new ScSelectionTransferObj( pView, eMode );
136 return pRet;
140 ScSelectionTransferObj::ScSelectionTransferObj( ScTabView* pSource, ScSelectionTransferMode eNewMode ) :
141 pView( pSource ),
142 eMode( eNewMode ),
143 pCellData( NULL ),
144 pDrawData( NULL )
146 //! store range for StillValid
149 ScSelectionTransferObj::~ScSelectionTransferObj()
151 ScModule* pScMod = SC_MOD();
152 if ( pScMod->GetSelectionTransfer() == this )
154 // this is reached when the object wasn't really copied to the selection
155 // (CopyToSelection has no effect under Windows)
157 ForgetView();
158 pScMod->SetSelectionTransfer( NULL );
161 OSL_ENSURE( !pView, "ScSelectionTransferObj dtor: ForgetView not called" );
164 sal_Bool ScSelectionTransferObj::StillValid()
166 //! check if view still has same cell selection
167 //! (but return sal_False if data has changed inbetween)
168 return false;
171 void ScSelectionTransferObj::ForgetView()
173 pView = NULL;
174 eMode = SC_SELTRANS_INVALID;
176 if (pCellData)
178 pCellData->release();
179 pCellData = NULL;
181 if (pDrawData)
183 pDrawData->release();
184 pDrawData = NULL;
188 void ScSelectionTransferObj::AddSupportedFormats()
190 // AddSupportedFormats must work without actually creating the
191 // "real" transfer object
193 switch (eMode)
195 case SC_SELTRANS_CELL:
196 case SC_SELTRANS_CELLS:
197 // same formats as in ScTransferObj::AddSupportedFormats
198 AddFormat( SOT_FORMATSTR_ID_EMBED_SOURCE );
199 AddFormat( SOT_FORMATSTR_ID_OBJECTDESCRIPTOR );
200 AddFormat( SOT_FORMAT_GDIMETAFILE );
201 AddFormat( SOT_FORMAT_BITMAP );
202 AddFormat( SOT_FORMATSTR_ID_HTML );
203 AddFormat( SOT_FORMATSTR_ID_SYLK );
204 AddFormat( SOT_FORMATSTR_ID_LINK );
205 AddFormat( SOT_FORMATSTR_ID_DIF );
206 AddFormat( SOT_FORMAT_STRING );
207 AddFormat( SOT_FORMAT_RTF );
208 if ( eMode == SC_SELTRANS_CELL )
209 AddFormat( SOT_FORMATSTR_ID_EDITENGINE );
210 break;
212 // different graphic formats as in ScDrawTransferObj::AddSupportedFormats:
214 case SC_SELTRANS_DRAW_BITMAP:
215 AddFormat( SOT_FORMATSTR_ID_OBJECTDESCRIPTOR );
216 AddFormat( SOT_FORMATSTR_ID_SVXB );
217 AddFormat( SOT_FORMAT_BITMAP );
218 AddFormat( SOT_FORMAT_GDIMETAFILE );
219 break;
221 case SC_SELTRANS_DRAW_GRAPHIC:
222 AddFormat( SOT_FORMATSTR_ID_OBJECTDESCRIPTOR );
223 AddFormat( SOT_FORMATSTR_ID_SVXB );
224 AddFormat( SOT_FORMAT_GDIMETAFILE );
225 AddFormat( SOT_FORMAT_BITMAP );
226 break;
228 case SC_SELTRANS_DRAW_BOOKMARK:
229 AddFormat( SOT_FORMATSTR_ID_OBJECTDESCRIPTOR );
230 AddFormat( SOT_FORMATSTR_ID_SOLK );
231 AddFormat( SOT_FORMAT_STRING );
232 AddFormat( SOT_FORMATSTR_ID_UNIFORMRESOURCELOCATOR );
233 AddFormat( SOT_FORMATSTR_ID_NETSCAPE_BOOKMARK );
234 AddFormat( SOT_FORMATSTR_ID_DRAWING );
235 break;
237 case SC_SELTRANS_DRAW_OLE:
238 AddFormat( SOT_FORMATSTR_ID_EMBED_SOURCE );
239 AddFormat( SOT_FORMATSTR_ID_OBJECTDESCRIPTOR );
240 AddFormat( SOT_FORMAT_GDIMETAFILE );
241 break;
243 case SC_SELTRANS_DRAW_OTHER:
244 // other drawing objects
245 AddFormat( SOT_FORMATSTR_ID_EMBED_SOURCE );
246 AddFormat( SOT_FORMATSTR_ID_OBJECTDESCRIPTOR );
247 AddFormat( SOT_FORMATSTR_ID_DRAWING );
248 AddFormat( SOT_FORMAT_BITMAP );
249 AddFormat( SOT_FORMAT_GDIMETAFILE );
250 break;
252 default:
254 // added to avoid warnings
259 void ScSelectionTransferObj::CreateCellData()
261 OSL_ENSURE( !pCellData, "CreateCellData twice" );
262 if ( pView )
264 ScViewData* pViewData = pView->GetViewData();
265 ScMarkData aNewMark( pViewData->GetMarkData() ); // use local copy for MarkToSimple
266 aNewMark.MarkToSimple();
268 // similar to ScViewFunctionSet::BeginDrag
269 if ( aNewMark.IsMarked() && !aNewMark.IsMultiMarked() )
271 ScDocShell* pDocSh = pViewData->GetDocShell();
273 ScRange aSelRange;
274 aNewMark.GetMarkArea( aSelRange );
275 ScDocShellRef aDragShellRef;
276 if ( pDocSh->GetDocument()->HasOLEObjectsInArea( aSelRange, &aNewMark ) )
278 aDragShellRef = new ScDocShell; // DocShell needs a Ref immediately
279 aDragShellRef->DoInitNew(NULL);
281 ScDrawLayer::SetGlobalDrawPersist(aDragShellRef);
283 ScDocument* pClipDoc = new ScDocument( SCDOCMODE_CLIP );
284 // bApi = sal_True -> no error messages
285 // #i18364# bStopEdit = sal_False -> don't end edit mode
286 // (this may be called from pasting into the edit line)
287 sal_Bool bCopied = pViewData->GetView()->CopyToClip( pClipDoc, false, sal_True, sal_True, false );
289 ScDrawLayer::SetGlobalDrawPersist(NULL);
291 if ( bCopied )
293 TransferableObjectDescriptor aObjDesc;
294 pDocSh->FillTransferableObjectDescriptor( aObjDesc );
295 aObjDesc.maDisplayName = pDocSh->GetMedium()->GetURLObject().GetURLNoPass();
296 // maSize is set in ScTransferObj ctor
298 ScTransferObj* pTransferObj = new ScTransferObj( pClipDoc, aObjDesc );
299 uno::Reference<datatransfer::XTransferable> xTransferable( pTransferObj );
301 // SetDragHandlePos is not used - there is no mouse position
302 //? pTransferObj->SetVisibleTab( nTab );
304 SfxObjectShellRef aPersistRef( aDragShellRef );
305 pTransferObj->SetDrawPersist( aPersistRef ); // keep persist for ole objects alive
307 pTransferObj->SetDragSource( pDocSh, aNewMark );
309 pCellData = pTransferObj;
310 pCellData->acquire(); // keep ref count up - released in ForgetView
312 else
313 delete pClipDoc;
316 OSL_ENSURE( pCellData, "can't create CellData" );
319 void ScSelectionTransferObj::CreateDrawData()
321 OSL_ENSURE( !pDrawData, "CreateDrawData twice" );
322 if ( pView )
324 // similar to ScDrawView::BeginDrag
326 ScDrawView* pDrawView = pView->GetScDrawView();
327 if ( pDrawView )
329 sal_Bool bAnyOle, bOneOle;
330 const SdrMarkList& rMarkList = pDrawView->GetMarkedObjectList();
331 ScDrawView::CheckOle( rMarkList, bAnyOle, bOneOle );
333 //---------------------------------------------------------
334 ScDocShellRef aDragShellRef;
335 if (bAnyOle)
337 aDragShellRef = new ScDocShell; // ohne Ref lebt die DocShell nicht !!!
338 aDragShellRef->DoInitNew(NULL);
340 //---------------------------------------------------------
342 ScDrawLayer::SetGlobalDrawPersist(aDragShellRef);
343 SdrModel* pModel = pDrawView->GetMarkedObjModel();
344 ScDrawLayer::SetGlobalDrawPersist(NULL);
346 ScViewData* pViewData = pView->GetViewData();
347 ScDocShell* pDocSh = pViewData->GetDocShell();
349 TransferableObjectDescriptor aObjDesc;
350 pDocSh->FillTransferableObjectDescriptor( aObjDesc );
351 aObjDesc.maDisplayName = pDocSh->GetMedium()->GetURLObject().GetURLNoPass();
352 // maSize is set in ScDrawTransferObj ctor
354 ScDrawTransferObj* pTransferObj = new ScDrawTransferObj( pModel, pDocSh, aObjDesc );
355 uno::Reference<datatransfer::XTransferable> xTransferable( pTransferObj );
357 SfxObjectShellRef aPersistRef( aDragShellRef );
358 pTransferObj->SetDrawPersist( aPersistRef ); // keep persist for ole objects alive
359 pTransferObj->SetDragSource( pDrawView ); // copies selection
361 pDrawData = pTransferObj;
362 pDrawData->acquire(); // keep ref count up - released in ForgetView
365 OSL_ENSURE( pDrawData, "can't create DrawData" );
368 ScTransferObj* ScSelectionTransferObj::GetCellData()
370 if ( !pCellData && ( eMode == SC_SELTRANS_CELL || eMode == SC_SELTRANS_CELLS ) )
371 CreateCellData();
372 return pCellData;
375 ScDrawTransferObj* ScSelectionTransferObj::GetDrawData()
377 if ( !pDrawData && ( eMode == SC_SELTRANS_DRAW_BITMAP || eMode == SC_SELTRANS_DRAW_GRAPHIC ||
378 eMode == SC_SELTRANS_DRAW_BOOKMARK || eMode == SC_SELTRANS_DRAW_OLE ||
379 eMode == SC_SELTRANS_DRAW_OTHER ) )
380 CreateDrawData();
381 return pDrawData;
384 sal_Bool ScSelectionTransferObj::GetData( const ::com::sun::star::datatransfer::DataFlavor& rFlavor )
386 sal_Bool bOK = false;
388 uno::Reference<datatransfer::XTransferable> xSource;
389 switch (eMode)
391 case SC_SELTRANS_CELL:
392 case SC_SELTRANS_CELLS:
393 xSource = GetCellData();
394 break;
395 case SC_SELTRANS_DRAW_BITMAP:
396 case SC_SELTRANS_DRAW_GRAPHIC:
397 case SC_SELTRANS_DRAW_BOOKMARK:
398 case SC_SELTRANS_DRAW_OLE:
399 case SC_SELTRANS_DRAW_OTHER:
400 xSource = GetDrawData();
401 break;
402 default:
404 // added to avoid warnings
408 if ( xSource.is() )
410 TransferableDataHelper aHelper( xSource );
411 uno::Any aAny = aHelper.GetAny( rFlavor );
412 bOK = SetAny( aAny, rFlavor );
415 return bOK;
418 void ScSelectionTransferObj::ObjectReleased()
420 // called when another selection is set from outside
422 ForgetView();
424 ScModule* pScMod = SC_MOD();
425 if ( pScMod->GetSelectionTransfer() == this )
426 pScMod->SetSelectionTransfer( NULL );
428 TransferableHelper::ObjectReleased();
432 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */