Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #include <com/sun/star/beans/XPropertySet.hpp>
21 : #include <com/sun/star/beans/XPropertySetInfo.hpp>
22 : #include <com/sun/star/form/FormButtonType.hpp>
23 :
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>
29 :
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"
39 :
40 : using namespace com::sun::star;
41 :
42 6 : static bool lcl_IsURLButton( SdrObject* pObject )
43 : {
44 6 : bool bRet = false;
45 :
46 6 : SdrUnoObj* pUnoCtrl = PTR_CAST(SdrUnoObj, pObject);
47 6 : if (pUnoCtrl && FmFormInventor == pUnoCtrl->GetObjInventor())
48 : {
49 0 : uno::Reference<awt::XControlModel> xControlModel = pUnoCtrl->GetUnoControlModel();
50 : OSL_ENSURE( xControlModel.is(), "uno control without model" );
51 0 : if ( xControlModel.is() )
52 : {
53 0 : uno::Reference< beans::XPropertySet > xPropSet( xControlModel, uno::UNO_QUERY );
54 0 : uno::Reference< beans::XPropertySetInfo > xInfo = xPropSet->getPropertySetInfo();
55 :
56 0 : OUString sPropButtonType( "ButtonType" );
57 0 : if(xInfo->hasPropertyByName( sPropButtonType ))
58 : {
59 0 : uno::Any aAny = xPropSet->getPropertyValue( sPropButtonType );
60 : form::FormButtonType eTmp;
61 0 : if ( (aAny >>= eTmp) && eTmp == form::FormButtonType_URL )
62 0 : bRet = true;
63 0 : }
64 0 : }
65 : }
66 :
67 6 : return bRet;
68 : }
69 :
70 4502 : ScSelectionTransferObj* ScSelectionTransferObj::CreateFromView( ScTabView* pView )
71 : {
72 4502 : ScSelectionTransferObj* pRet = NULL;
73 :
74 : try
75 : {
76 4502 : if ( pView )
77 : {
78 4502 : ScSelectionTransferMode eMode = SC_SELTRANS_INVALID;
79 :
80 4502 : SdrView* pSdrView = pView->GetSdrView();
81 4502 : if ( pSdrView )
82 : {
83 : // handle selection on drawing layer
84 3558 : const SdrMarkList& rMarkList = pSdrView->GetMarkedObjectList();
85 3558 : const size_t nMarkCount = rMarkList.GetMarkCount();
86 3558 : if ( nMarkCount )
87 : {
88 8 : if ( nMarkCount == 1 )
89 : {
90 6 : SdrObject* pObj = rMarkList.GetMark(0)->GetMarkedSdrObj();
91 6 : sal_uInt16 nSdrObjKind = pObj->GetObjIdentifier();
92 :
93 6 : if ( nSdrObjKind == OBJ_GRAF )
94 : {
95 0 : if ( static_cast<SdrGrafObj*>(pObj)->GetGraphic().GetType() == GRAPHIC_BITMAP )
96 0 : eMode = SC_SELTRANS_DRAW_BITMAP;
97 : else
98 0 : eMode = SC_SELTRANS_DRAW_GRAPHIC;
99 : }
100 6 : else if ( nSdrObjKind == OBJ_OLE2 )
101 0 : eMode = SC_SELTRANS_DRAW_OLE;
102 6 : else if ( lcl_IsURLButton( pObj ) )
103 0 : eMode = SC_SELTRANS_DRAW_BOOKMARK;
104 : }
105 :
106 8 : if ( eMode == SC_SELTRANS_INVALID )
107 8 : eMode = SC_SELTRANS_DRAW_OTHER; // something selected but no special selection
108 : }
109 : }
110 4502 : if ( eMode == SC_SELTRANS_INVALID ) // no drawing object selected
111 : {
112 4494 : ScRange aRange;
113 4494 : ScViewData& rViewData = pView->GetViewData();
114 4494 : 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 4846 : if ( ( rMark.IsMarked() || rMark.IsMultiMarked() ) &&
120 4 : (((eMarkType = rViewData.GetSimpleArea( aRange )) == SC_MARK_SIMPLE) ||
121 : (eMarkType == SC_MARK_SIMPLE_FILTERED)) )
122 : {
123 : // only for "real" selection, cursor alone isn't used
124 352 : if ( aRange.aStart == aRange.aEnd )
125 200 : eMode = SC_SELTRANS_CELL;
126 : else
127 152 : eMode = SC_SELTRANS_CELLS;
128 : }
129 : }
130 :
131 4502 : if ( eMode != SC_SELTRANS_INVALID )
132 360 : pRet = new ScSelectionTransferObj( pView, eMode );
133 : }
134 : }
135 0 : catch (...)
136 : {
137 : }
138 :
139 4502 : return pRet;
140 : }
141 :
142 360 : ScSelectionTransferObj::ScSelectionTransferObj( ScTabView* pSource, ScSelectionTransferMode eNewMode ) :
143 : pView( pSource ),
144 : eMode( eNewMode ),
145 : pCellData( NULL ),
146 360 : pDrawData( NULL )
147 : {
148 : //! store range for StillValid
149 360 : }
150 :
151 1080 : ScSelectionTransferObj::~ScSelectionTransferObj()
152 : {
153 360 : ScModule* pScMod = SC_MOD();
154 360 : if ( pScMod->GetSelectionTransfer() == this )
155 : {
156 : // this is reached when the object wasn't really copied to the selection
157 : // (CopyToSelection has no effect under Windows)
158 :
159 320 : ForgetView();
160 320 : pScMod->SetSelectionTransfer( NULL );
161 : }
162 :
163 : OSL_ENSURE( !pView, "ScSelectionTransferObj dtor: ForgetView not called" );
164 720 : }
165 :
166 38 : bool ScSelectionTransferObj::StillValid()
167 : {
168 : //! check if view still has same cell selection
169 : //! (but return sal_False if data has changed inbetween)
170 38 : return false;
171 : }
172 :
173 400 : void ScSelectionTransferObj::ForgetView()
174 : {
175 400 : pView = NULL;
176 400 : eMode = SC_SELTRANS_INVALID;
177 :
178 400 : if (pCellData)
179 : {
180 0 : pCellData->release();
181 0 : pCellData = NULL;
182 : }
183 400 : if (pDrawData)
184 : {
185 0 : pDrawData->release();
186 0 : pDrawData = NULL;
187 : }
188 400 : }
189 :
190 0 : void ScSelectionTransferObj::AddSupportedFormats()
191 : {
192 : // AddSupportedFormats must work without actually creating the
193 : // "real" transfer object
194 :
195 0 : switch (eMode)
196 : {
197 : case SC_SELTRANS_CELL:
198 : case SC_SELTRANS_CELLS:
199 : // same formats as in ScTransferObj::AddSupportedFormats
200 0 : AddFormat( SOT_FORMATSTR_ID_EMBED_SOURCE );
201 0 : AddFormat( SOT_FORMATSTR_ID_OBJECTDESCRIPTOR );
202 0 : AddFormat( SOT_FORMAT_GDIMETAFILE );
203 0 : AddFormat( SOT_FORMATSTR_ID_PNG );
204 0 : AddFormat( SOT_FORMAT_BITMAP );
205 0 : AddFormat( SOT_FORMATSTR_ID_HTML );
206 0 : AddFormat( SOT_FORMATSTR_ID_SYLK );
207 0 : AddFormat( SOT_FORMATSTR_ID_LINK );
208 0 : AddFormat( SOT_FORMATSTR_ID_DIF );
209 0 : AddFormat( SOT_FORMAT_STRING );
210 0 : AddFormat( SOT_FORMAT_RTF );
211 0 : if ( eMode == SC_SELTRANS_CELL )
212 0 : AddFormat( SOT_FORMATSTR_ID_EDITENGINE );
213 0 : break;
214 :
215 : // different graphic formats as in ScDrawTransferObj::AddSupportedFormats:
216 :
217 : case SC_SELTRANS_DRAW_BITMAP:
218 0 : AddFormat( SOT_FORMATSTR_ID_OBJECTDESCRIPTOR );
219 0 : AddFormat( SOT_FORMATSTR_ID_SVXB );
220 0 : AddFormat( SOT_FORMATSTR_ID_PNG );
221 0 : AddFormat( SOT_FORMAT_BITMAP );
222 0 : AddFormat( SOT_FORMAT_GDIMETAFILE );
223 0 : break;
224 :
225 : case SC_SELTRANS_DRAW_GRAPHIC:
226 0 : AddFormat( SOT_FORMATSTR_ID_OBJECTDESCRIPTOR );
227 0 : AddFormat( SOT_FORMATSTR_ID_SVXB );
228 0 : AddFormat( SOT_FORMAT_GDIMETAFILE );
229 0 : AddFormat( SOT_FORMATSTR_ID_PNG );
230 0 : AddFormat( SOT_FORMAT_BITMAP );
231 0 : break;
232 :
233 : case SC_SELTRANS_DRAW_BOOKMARK:
234 0 : AddFormat( SOT_FORMATSTR_ID_OBJECTDESCRIPTOR );
235 0 : AddFormat( SOT_FORMATSTR_ID_SOLK );
236 0 : AddFormat( SOT_FORMAT_STRING );
237 0 : AddFormat( SOT_FORMATSTR_ID_UNIFORMRESOURCELOCATOR );
238 0 : AddFormat( SOT_FORMATSTR_ID_NETSCAPE_BOOKMARK );
239 0 : AddFormat( SOT_FORMATSTR_ID_DRAWING );
240 0 : break;
241 :
242 : case SC_SELTRANS_DRAW_OLE:
243 0 : AddFormat( SOT_FORMATSTR_ID_EMBED_SOURCE );
244 0 : AddFormat( SOT_FORMATSTR_ID_OBJECTDESCRIPTOR );
245 0 : AddFormat( SOT_FORMAT_GDIMETAFILE );
246 0 : break;
247 :
248 : case SC_SELTRANS_DRAW_OTHER:
249 : // other drawing objects
250 0 : AddFormat( SOT_FORMATSTR_ID_EMBED_SOURCE );
251 0 : AddFormat( SOT_FORMATSTR_ID_OBJECTDESCRIPTOR );
252 0 : AddFormat( SOT_FORMATSTR_ID_DRAWING );
253 0 : AddFormat( SOT_FORMATSTR_ID_PNG );
254 0 : AddFormat( SOT_FORMAT_BITMAP );
255 0 : AddFormat( SOT_FORMAT_GDIMETAFILE );
256 0 : break;
257 :
258 : default:
259 : {
260 : // added to avoid warnings
261 : }
262 : }
263 0 : }
264 :
265 0 : void ScSelectionTransferObj::CreateCellData()
266 : {
267 : OSL_ENSURE( !pCellData, "CreateCellData twice" );
268 0 : if ( pView )
269 : {
270 0 : ScViewData& rViewData = pView->GetViewData();
271 0 : ScMarkData aNewMark( rViewData.GetMarkData() ); // use local copy for MarkToSimple
272 0 : aNewMark.MarkToSimple();
273 :
274 : // similar to ScViewFunctionSet::BeginDrag
275 0 : if ( aNewMark.IsMarked() && !aNewMark.IsMultiMarked() )
276 : {
277 0 : ScDocShell* pDocSh = rViewData.GetDocShell();
278 :
279 0 : ScRange aSelRange;
280 0 : aNewMark.GetMarkArea( aSelRange );
281 0 : ScDocShellRef aDragShellRef;
282 0 : if ( pDocSh->GetDocument().HasOLEObjectsInArea( aSelRange, &aNewMark ) )
283 : {
284 0 : aDragShellRef = new ScDocShell; // DocShell needs a Ref immediately
285 0 : aDragShellRef->DoInitNew(NULL);
286 : }
287 0 : ScDrawLayer::SetGlobalDrawPersist(aDragShellRef);
288 :
289 0 : ScDocument* pClipDoc = new ScDocument( SCDOCMODE_CLIP );
290 : // bApi = sal_True -> no error messages
291 : // #i18364# bStopEdit = sal_False -> don't end edit mode
292 : // (this may be called from pasting into the edit line)
293 0 : bool bCopied = rViewData.GetView()->CopyToClip( pClipDoc, false, true, true, false );
294 :
295 0 : ScDrawLayer::SetGlobalDrawPersist(NULL);
296 :
297 0 : if ( bCopied )
298 : {
299 0 : TransferableObjectDescriptor aObjDesc;
300 0 : pDocSh->FillTransferableObjectDescriptor( aObjDesc );
301 0 : aObjDesc.maDisplayName = pDocSh->GetMedium()->GetURLObject().GetURLNoPass();
302 : // maSize is set in ScTransferObj ctor
303 :
304 0 : ScTransferObj* pTransferObj = new ScTransferObj( pClipDoc, aObjDesc );
305 0 : uno::Reference<datatransfer::XTransferable> xTransferable( pTransferObj );
306 :
307 : // SetDragHandlePos is not used - there is no mouse position
308 : //? pTransferObj->SetVisibleTab( nTab );
309 :
310 0 : SfxObjectShellRef aPersistRef( aDragShellRef );
311 0 : pTransferObj->SetDrawPersist( aPersistRef ); // keep persist for ole objects alive
312 :
313 0 : pTransferObj->SetDragSource( pDocSh, aNewMark );
314 :
315 0 : pCellData = pTransferObj;
316 0 : pCellData->acquire(); // keep ref count up - released in ForgetView
317 : }
318 : else
319 0 : delete pClipDoc;
320 0 : }
321 : }
322 : OSL_ENSURE( pCellData, "can't create CellData" );
323 0 : }
324 :
325 0 : void ScSelectionTransferObj::CreateDrawData()
326 : {
327 : OSL_ENSURE( !pDrawData, "CreateDrawData twice" );
328 0 : if ( pView )
329 : {
330 : // similar to ScDrawView::BeginDrag
331 :
332 0 : ScDrawView* pDrawView = pView->GetScDrawView();
333 0 : if ( pDrawView )
334 : {
335 : bool bAnyOle, bOneOle;
336 0 : const SdrMarkList& rMarkList = pDrawView->GetMarkedObjectList();
337 0 : ScDrawView::CheckOle( rMarkList, bAnyOle, bOneOle );
338 :
339 0 : ScDocShellRef aDragShellRef;
340 0 : if (bAnyOle)
341 : {
342 0 : aDragShellRef = new ScDocShell; // Without Ref the DocShell does not live
343 0 : aDragShellRef->DoInitNew(NULL);
344 : }
345 :
346 0 : ScDrawLayer::SetGlobalDrawPersist(aDragShellRef);
347 0 : SdrModel* pModel = pDrawView->GetMarkedObjModel();
348 0 : ScDrawLayer::SetGlobalDrawPersist(NULL);
349 :
350 0 : ScViewData& rViewData = pView->GetViewData();
351 0 : ScDocShell* pDocSh = rViewData.GetDocShell();
352 :
353 0 : TransferableObjectDescriptor aObjDesc;
354 0 : pDocSh->FillTransferableObjectDescriptor( aObjDesc );
355 0 : aObjDesc.maDisplayName = pDocSh->GetMedium()->GetURLObject().GetURLNoPass();
356 : // maSize is set in ScDrawTransferObj ctor
357 :
358 0 : ScDrawTransferObj* pTransferObj = new ScDrawTransferObj( pModel, pDocSh, aObjDesc );
359 0 : uno::Reference<datatransfer::XTransferable> xTransferable( pTransferObj );
360 :
361 0 : SfxObjectShellRef aPersistRef( aDragShellRef );
362 0 : pTransferObj->SetDrawPersist( aPersistRef ); // keep persist for ole objects alive
363 0 : pTransferObj->SetDragSource( pDrawView ); // copies selection
364 :
365 0 : pDrawData = pTransferObj;
366 0 : pDrawData->acquire(); // keep ref count up - released in ForgetView
367 : }
368 : }
369 : OSL_ENSURE( pDrawData, "can't create DrawData" );
370 0 : }
371 :
372 0 : ScTransferObj* ScSelectionTransferObj::GetCellData()
373 : {
374 0 : if ( !pCellData && ( eMode == SC_SELTRANS_CELL || eMode == SC_SELTRANS_CELLS ) )
375 0 : CreateCellData();
376 0 : return pCellData;
377 : }
378 :
379 0 : ScDrawTransferObj* ScSelectionTransferObj::GetDrawData()
380 : {
381 0 : if ( !pDrawData && ( eMode == SC_SELTRANS_DRAW_BITMAP || eMode == SC_SELTRANS_DRAW_GRAPHIC ||
382 0 : eMode == SC_SELTRANS_DRAW_BOOKMARK || eMode == SC_SELTRANS_DRAW_OLE ||
383 0 : eMode == SC_SELTRANS_DRAW_OTHER ) )
384 0 : CreateDrawData();
385 0 : return pDrawData;
386 : }
387 :
388 0 : bool ScSelectionTransferObj::GetData(
389 : const css::datatransfer::DataFlavor& rFlavor, const OUString& rDestDoc )
390 : {
391 0 : bool bOK = false;
392 :
393 0 : uno::Reference<datatransfer::XTransferable> xSource;
394 0 : switch (eMode)
395 : {
396 : case SC_SELTRANS_CELL:
397 : case SC_SELTRANS_CELLS:
398 0 : xSource = GetCellData();
399 0 : break;
400 : case SC_SELTRANS_DRAW_BITMAP:
401 : case SC_SELTRANS_DRAW_GRAPHIC:
402 : case SC_SELTRANS_DRAW_BOOKMARK:
403 : case SC_SELTRANS_DRAW_OLE:
404 : case SC_SELTRANS_DRAW_OTHER:
405 0 : xSource = GetDrawData();
406 0 : break;
407 : default:
408 : {
409 : // added to avoid warnings
410 : }
411 : }
412 :
413 0 : if ( xSource.is() )
414 : {
415 0 : TransferableDataHelper aHelper( xSource );
416 0 : uno::Any aAny = aHelper.GetAny(rFlavor, rDestDoc);
417 0 : bOK = SetAny( aAny, rFlavor );
418 : }
419 :
420 0 : return bOK;
421 : }
422 :
423 40 : void ScSelectionTransferObj::ObjectReleased()
424 : {
425 : // called when another selection is set from outside
426 :
427 40 : ForgetView();
428 :
429 40 : ScModule* pScMod = SC_MOD();
430 40 : if ( pScMod->GetSelectionTransfer() == this )
431 0 : pScMod->SetSelectionTransfer( NULL );
432 :
433 40 : TransferableHelper::ObjectReleased();
434 268 : }
435 :
436 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|