update ooo310-m15
[ooovba.git] / sc / source / ui / view / viewfun5.cxx
blob7e35647ccef2c8013f314baf75a48a7fec3f81cc
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: viewfun5.cxx,v $
10 * $Revision: 1.54 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_sc.hxx"
33 #include <com/sun/star/embed/XEmbedObjectClipboardCreator.hpp>
34 #include <com/sun/star/embed/Aspects.hpp>
37 #include <svx/unomodel.hxx>
38 #include <unotools/streamwrap.hxx>
40 //------------------------------------------------------------------
42 #include <svx/dbexch.hrc>
43 #include <svx/fmmodel.hxx>
44 #include <svx/svdetc.hxx>
45 #include <svx/svditer.hxx>
46 #include <svx/svdobj.hxx>
47 #include <svx/svdogrp.hxx>
48 #include <svx/svdouno.hxx>
49 #include <svx/svdoole2.hxx>
50 #include <svx/svdpage.hxx>
51 #include <sfx2/dispatch.hxx>
52 #include <sfx2/docfile.hxx>
53 #include <sot/clsids.hxx>
54 #include <sot/formats.hxx>
55 #include <sot/filelist.hxx>
56 #include <svtools/pathoptions.hxx>
57 #include <svtools/ptitem.hxx>
58 #include <svtools/stritem.hxx>
59 #include <svtools/transfer.hxx>
60 #include <vcl/graph.hxx>
62 #include <comphelper/storagehelper.hxx>
63 #include <comphelper/processfactory.hxx>
65 #include <sot/formats.hxx>
66 #define SOT_FORMATSTR_ID_STARCALC_CURRENT SOT_FORMATSTR_ID_STARCALC_50
68 #include "viewfunc.hxx"
69 #include "docsh.hxx"
70 #include "drawview.hxx"
71 #include "impex.hxx"
72 #include "dbfunc.hxx"
73 #include "dbcolect.hxx"
74 #include "sc.hrc"
75 #include "filter.hxx"
76 #include "scextopt.hxx"
77 #include "tabvwsh.hxx" // wegen GetViewFrame
78 #include "compiler.hxx"
80 #include "asciiopt.hxx"
81 #include "scabstdlg.hxx"
82 #include <vcl/msgbox.hxx>
83 #include <sfx2/viewfrm.hxx>
84 #include <svx/dbaexchange.hxx>
86 using namespace com::sun::star;
88 //------------------------------------------------------------------
90 BOOL ScViewFunc::PasteDataFormat( ULONG nFormatId,
91 const uno::Reference<datatransfer::XTransferable>& rxTransferable,
92 SCCOL nPosX, SCROW nPosY, Point* pLogicPos, BOOL bLink, BOOL bAllowDialogs )
94 ScDocument* pDoc = GetViewData()->GetDocument();
95 pDoc->SetPastingDrawFromOtherDoc( TRUE );
97 Point aPos; // inserting position (1/100 mm)
98 if (pLogicPos)
99 aPos = *pLogicPos;
100 else
102 // inserting position isn't needed for text formats
103 BOOL bIsTextFormat = ( ScImportExport::IsFormatSupported( nFormatId ) ||
104 nFormatId == FORMAT_RTF );
105 if ( !bIsTextFormat )
107 // Window MapMode isn't drawing MapMode if DrawingLayer hasn't been created yet
109 SCTAB nTab = GetViewData()->GetTabNo();
110 long nXT = 0;
111 for (SCCOL i=0; i<nPosX; i++)
112 nXT += pDoc->GetColWidth(i,nTab);
113 if (pDoc->IsNegativePage(nTab))
114 nXT = -nXT;
115 ULONG nYT = pDoc->FastGetRowHeight( 0, nPosY-1, nTab);
116 aPos = Point( (long)(nXT * HMM_PER_TWIPS), (long)(nYT * HMM_PER_TWIPS) );
120 TransferableDataHelper aDataHelper( rxTransferable );
121 BOOL bRet = FALSE;
124 // handle individual formats
127 if ( nFormatId == SOT_FORMATSTR_ID_EMBED_SOURCE ||
128 nFormatId == SOT_FORMATSTR_ID_LINK_SOURCE ||
129 nFormatId == SOT_FORMATSTR_ID_EMBED_SOURCE_OLE ||
130 nFormatId == SOT_FORMATSTR_ID_LINK_SOURCE_OLE ||
131 nFormatId == SOT_FORMATSTR_ID_EMBEDDED_OBJ_OLE )
133 uno::Reference < io::XInputStream > xStm;
134 TransferableObjectDescriptor aObjDesc;
136 if( aDataHelper.GetTransferableObjectDescriptor( SOT_FORMATSTR_ID_OBJECTDESCRIPTOR, aObjDesc ) &&
137 aDataHelper.GetInputStream( nFormatId, xStm ) )
139 if ( aObjDesc.maClassName == SvGlobalName( SO3_SC_CLASSID_60 ) )
141 uno::Reference < embed::XStorage > xStore = ::comphelper::OStorageHelper::GetStorageFromInputStream( xStm );
143 // mba: BaseURL doesn't make sense for clipboard
144 // #i43716# Medium must be allocated with "new".
145 // DoLoad stores the pointer and deletes it with the SfxObjectShell.
146 SfxMedium* pMedium = new SfxMedium( xStore, String() );
148 // TODO/LATER: is it a problem that we don't support binary formats here?
149 ScDocShellRef xDocShRef = new ScDocShell(SFX_CREATE_MODE_EMBEDDED);
150 if (xDocShRef->DoLoad(pMedium))
152 ScDocument* pSrcDoc = xDocShRef->GetDocument();
153 SCTAB nSrcTab = pSrcDoc->GetVisibleTab();
154 if (!pSrcDoc->HasTable(nSrcTab))
155 nSrcTab = 0;
157 ScMarkData aSrcMark;
158 aSrcMark.SelectOneTable( nSrcTab ); // for CopyToClip
159 ScDocument* pClipDoc = new ScDocument( SCDOCMODE_CLIP );
161 SCCOL nFirstCol, nLastCol;
162 SCROW nFirstRow, nLastRow;
163 if ( pSrcDoc->GetDataStart( nSrcTab, nFirstCol, nFirstRow ) )
164 pSrcDoc->GetCellArea( nSrcTab, nLastCol, nLastRow );
165 else
167 nFirstCol = nLastCol = 0;
168 nFirstRow = nLastRow = 0;
170 pSrcDoc->CopyToClip( nFirstCol, nFirstRow, nLastCol, nLastRow,
171 FALSE, pClipDoc, FALSE, &aSrcMark );
172 ScGlobal::SetClipDocName( xDocShRef->GetTitle( SFX_TITLE_FULLNAME ) );
174 SetCursor( nPosX, nPosY );
175 Unmark();
176 PasteFromClip( IDF_ALL, pClipDoc,
177 PASTE_NOFUNC, FALSE, FALSE, FALSE, INS_NONE, IDF_NONE,
178 bAllowDialogs );
179 delete pClipDoc;
180 bRet = TRUE;
183 xDocShRef->DoClose();
184 xDocShRef.Clear();
186 else
188 ::rtl::OUString aName;
189 uno::Reference < embed::XEmbeddedObject > xObj = GetViewData()->GetViewShell()->GetObjectShell()->
190 GetEmbeddedObjectContainer().InsertEmbeddedObject( xStm, aName );
191 if ( xObj.is() )
193 // try to get the replacement image from the clipboard
194 Graphic aGraphic;
195 ULONG nGrFormat = 0;
196 if( aDataHelper.GetGraphic( SOT_FORMATSTR_ID_SVXB, aGraphic ) )
197 nGrFormat = SOT_FORMATSTR_ID_SVXB;
198 else if( aDataHelper.GetGraphic( FORMAT_GDIMETAFILE, aGraphic ) )
199 nGrFormat = SOT_FORMAT_GDIMETAFILE;
200 else if( aDataHelper.GetGraphic( FORMAT_BITMAP, aGraphic ) )
201 nGrFormat = SOT_FORMAT_BITMAP;
203 // insert replacement image ( if there is one ) into the object helper
204 if ( nGrFormat )
206 datatransfer::DataFlavor aDataFlavor;
207 SotExchange::GetFormatDataFlavor( nGrFormat, aDataFlavor );
208 PasteObject( aPos, xObj, &aObjDesc.maSize, &aGraphic, aDataFlavor.MimeType, aObjDesc.mnViewAspect );
210 else
211 PasteObject( aPos, xObj, &aObjDesc.maSize );
213 bRet = TRUE;
215 else
217 DBG_ERROR("Error in CreateAndLoad");
221 else
223 // uno::Reference < io::XInputStream > xStm;
224 // TransferableObjectDescriptor aObjDesc;
226 if ( aDataHelper.GetTransferableObjectDescriptor( SOT_FORMATSTR_ID_OBJECTDESCRIPTOR_OLE, aObjDesc ) )
228 ::rtl::OUString aName;
229 uno::Reference < embed::XEmbeddedObject > xObj;
231 if ( aDataHelper.GetInputStream( SOT_FORMATSTR_ID_EMBED_SOURCE_OLE, xStm )
232 || aDataHelper.GetInputStream( SOT_FORMATSTR_ID_EMBEDDED_OBJ_OLE, xStm ) )
234 xObj = GetViewData()->GetDocShell()->GetEmbeddedObjectContainer().InsertEmbeddedObject( xStm, aName );
236 else
240 uno::Reference< embed::XStorage > xTmpStor = ::comphelper::OStorageHelper::GetTemporaryStorage();
241 uno::Reference < embed::XEmbedObjectClipboardCreator > xClipboardCreator(
242 ::comphelper::getProcessServiceFactory()->createInstance( ::rtl::OUString(
243 RTL_CONSTASCII_USTRINGPARAM("com.sun.star.embed.MSOLEObjectSystemCreator") ) ),
244 uno::UNO_QUERY_THROW );
246 embed::InsertedObjectInfo aInfo = xClipboardCreator->createInstanceInitFromClipboard(
247 xTmpStor,
248 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM ( "DummyName" ) ),
249 uno::Sequence< beans::PropertyValue >() );
251 // TODO/LATER: in future InsertedObjectInfo will be used to get container related information
252 // for example whether the object should be an iconified one
253 xObj = aInfo.Object;
254 if ( xObj.is() )
255 GetViewData()->GetDocShell()->GetEmbeddedObjectContainer().InsertEmbeddedObject( xObj, aName );
257 catch( uno::Exception& )
261 if ( xObj.is() )
263 // try to get the replacement image from the clipboard
264 Graphic aGraphic;
265 ULONG nGrFormat = 0;
266 if( aDataHelper.GetGraphic( SOT_FORMATSTR_ID_SVXB, aGraphic ) )
267 nGrFormat = SOT_FORMATSTR_ID_SVXB;
268 else if( aDataHelper.GetGraphic( FORMAT_GDIMETAFILE, aGraphic ) )
269 nGrFormat = SOT_FORMAT_GDIMETAFILE;
270 else if( aDataHelper.GetGraphic( FORMAT_BITMAP, aGraphic ) )
271 nGrFormat = SOT_FORMAT_BITMAP;
273 // insert replacement image ( if there is one ) into the object helper
274 if ( nGrFormat )
276 datatransfer::DataFlavor aDataFlavor;
277 SotExchange::GetFormatDataFlavor( nGrFormat, aDataFlavor );
278 PasteObject( aPos, xObj, &aObjDesc.maSize, &aGraphic, aDataFlavor.MimeType, aObjDesc.mnViewAspect );
280 else
281 PasteObject( aPos, xObj, &aObjDesc.maSize );
283 // let object stay in loaded state after insertion
284 SdrOle2Obj::Unload( xObj, embed::Aspects::MSOLE_CONTENT );
285 bRet = TRUE;
287 else
289 DBG_ERROR("Error creating external OLE object");
292 //TODO/LATER: if format is not available, create picture
295 else if ( nFormatId == SOT_FORMATSTR_ID_LINK ) // LINK is also in ScImportExport
297 bRet = PasteDDE( rxTransferable );
299 else if ( ScImportExport::IsFormatSupported( nFormatId ) || nFormatId == SOT_FORMAT_RTF )
301 if ( nFormatId == SOT_FORMAT_RTF && aDataHelper.HasFormat( SOT_FORMATSTR_ID_EDITENGINE ) )
303 // use EditView's PasteSpecial / Drop
304 PasteRTF( nPosX, nPosY, rxTransferable );
305 bRet = TRUE;
307 else
309 ScAddress aCellPos( nPosX, nPosY, GetViewData()->GetTabNo() );
310 ScImportExport aObj( GetViewData()->GetDocument(), aCellPos );
312 ::rtl::OUString aStr;
313 SotStorageStreamRef xStream;
314 if ( aDataHelper.GetSotStorageStream( nFormatId, xStream ) && xStream.Is() )
315 // mba: clipboard always must contain absolute URLs (could be from alien source)
316 bRet = aObj.ImportStream( *xStream, String(), nFormatId );
317 else if (nFormatId == FORMAT_STRING && aDataHelper.GetString( nFormatId, aStr ))
319 // Do CSV dialog if more than one line.
320 sal_Int32 nDelim = aStr.indexOf('\n');
321 #if 0
322 ::rtl::OString tmpStr = OUStringToOString( aStr,
323 RTL_TEXTENCODING_UTF8 );
324 fprintf( stderr, "String is '%s' (%d) [%d]\n", tmpStr.getStr(),
325 tmpStr.getLength(), nDelim);
326 #endif
327 if (nDelim >= 0 && nDelim != aStr.getLength () - 1)
329 ScImportStringStream aStrm( aStr);
330 ScAbstractDialogFactory* pFact =
331 ScAbstractDialogFactory::Create();
332 AbstractScImportAsciiDlg *pDlg =
333 pFact->CreateScImportAsciiDlg( NULL, String(), &aStrm,
334 RID_SCDLG_ASCII);
336 if (pDlg->Execute() == RET_OK)
338 ScAsciiOptions aOptions;
339 pDlg->GetOptions( aOptions );
340 aObj.SetExtOptions( aOptions );
342 bRet = aObj.ImportString( aStr, nFormatId );
344 // TODO: what if (aObj.IsOverflow())
345 // Content was partially pasted, which can be undone by
346 // the user though.
347 if (aObj.IsOverflow())
348 bRet = FALSE;
350 else
351 bRet = TRUE;
352 // Yes, no failure, don't raise a "couldn't paste"
353 // dialog if user cancelled.
354 delete pDlg;
356 else
357 bRet = aObj.ImportString( aStr, nFormatId );
359 else if (nFormatId != FORMAT_STRING && aDataHelper.GetString( nFormatId, aStr ))
360 bRet = aObj.ImportString( aStr, nFormatId );
362 InvalidateAttribs();
363 GetViewData()->UpdateInputHandler();
366 else if (nFormatId == SOT_FORMATSTR_ID_SBA_DATAEXCHANGE)
368 // import of database data into table
370 String sDataDesc;
371 if ( aDataHelper.GetString( nFormatId, sDataDesc ) )
373 SfxStringItem aDataDesc(SID_SBA_IMPORT, sDataDesc);
375 ScDocShell* pDocSh = GetViewData()->GetDocShell();
376 SCTAB nTab = GetViewData()->GetTabNo();
378 ClickCursor(nPosX, nPosY, FALSE); // set cursor position
380 // Creation of database area "Import1" isn't here, but in the DocShell
381 // slot execute, so it can be added to the undo action
383 ScDBData* pDBData = pDocSh->GetDBData( ScRange(nPosX,nPosY,nTab), SC_DB_OLD, FALSE );
384 String sTarget;
385 if (pDBData)
386 sTarget = pDBData->GetName();
387 else
389 ScAddress aCellPos( nPosX,nPosY,nTab );
390 aCellPos.Format( sTarget, SCA_ABS_3D, pDoc, pDoc->GetAddressConvention() );
392 SfxStringItem aTarget(FN_PARAM_1, sTarget);
394 BOOL bAreaIsNew = !pDBData;
395 SfxBoolItem aAreaNew(FN_PARAM_2, bAreaIsNew);
397 ::svx::ODataAccessDescriptor aDesc;
398 DataFlavorExVector& rVector = aDataHelper.GetDataFlavorExVector();
399 ::std::auto_ptr<SfxUsrAnyItem> pCursorItem;
400 if ( ::svx::ODataAccessObjectTransferable::canExtractObjectDescriptor(rVector) )
402 aDesc = ::svx::ODataAccessObjectTransferable::extractObjectDescriptor(aDataHelper);
403 if ( aDesc.has(::svx::daCursor) )
404 pCursorItem.reset(new SfxUsrAnyItem(FN_PARAM_3, aDesc[::svx::daCursor]));
407 // asynchronous, to avoid doing the whole import in drop handler
408 SfxDispatcher& rDisp = GetViewData()->GetDispatcher();
409 rDisp.Execute(SID_SBA_IMPORT, SFX_CALLMODE_ASYNCHRON,
410 &aDataDesc, &aTarget, &aAreaNew, pCursorItem.get(), (void*)0 );
412 bRet = TRUE;
415 else if (nFormatId == SOT_FORMATSTR_ID_SBA_FIELDDATAEXCHANGE)
417 // insert database field control
419 if ( ::svx::OColumnTransferable::canExtractColumnDescriptor( aDataHelper.GetDataFlavorExVector(), CTF_COLUMN_DESCRIPTOR | CTF_CONTROL_EXCHANGE ) )
421 MakeDrawLayer();
422 ScDrawView* pScDrawView = GetScDrawView();
423 SdrObject* pObj = pScDrawView->CreateFieldControl( ::svx::OColumnTransferable::extractColumnDescriptor( aDataHelper ) );
424 if (pObj)
426 Point aInsPos = aPos;
427 Rectangle aRect(pObj->GetLogicRect());
428 aInsPos.X() -= aRect.GetSize().Width() / 2;
429 aInsPos.Y() -= aRect.GetSize().Height() / 2;
430 if ( aInsPos.X() < 0 ) aInsPos.X() = 0;
431 if ( aInsPos.Y() < 0 ) aInsPos.Y() = 0;
432 aRect.SetPos(aInsPos);
433 pObj->SetLogicRect(aRect);
435 if ( pObj->ISA(SdrUnoObj) )
436 pObj->NbcSetLayer(SC_LAYER_CONTROLS);
437 else
438 pObj->NbcSetLayer(SC_LAYER_FRONT);
439 if (pObj->ISA(SdrObjGroup))
441 SdrObjListIter aIter( *pObj, IM_DEEPWITHGROUPS );
442 SdrObject* pSubObj = aIter.Next();
443 while (pSubObj)
445 if ( pSubObj->ISA(SdrUnoObj) )
446 pSubObj->NbcSetLayer(SC_LAYER_CONTROLS);
447 else
448 pSubObj->NbcSetLayer(SC_LAYER_FRONT);
449 pSubObj = aIter.Next();
453 pScDrawView->InsertObjectSafe(pObj, *pScDrawView->GetSdrPageView());
455 GetViewData()->GetViewShell()->SetDrawShell( TRUE );
456 bRet = TRUE;
460 else if (nFormatId == SOT_FORMAT_BITMAP)
462 Bitmap aBmp;
463 if( aDataHelper.GetBitmap( FORMAT_BITMAP, aBmp ) )
464 bRet = PasteBitmap( aPos, aBmp );
466 else if (nFormatId == SOT_FORMAT_GDIMETAFILE)
468 GDIMetaFile aMtf;
469 if( aDataHelper.GetGDIMetaFile( FORMAT_GDIMETAFILE, aMtf ) )
470 bRet = PasteMetaFile( aPos, aMtf );
472 else if (nFormatId == SOT_FORMATSTR_ID_SVXB)
474 SotStorageStreamRef xStm;
475 if( aDataHelper.GetSotStorageStream( SOT_FORMATSTR_ID_SVXB, xStm ) )
477 Graphic aGraphic;
478 *xStm >> aGraphic;
479 bRet = PasteGraphic( aPos, aGraphic, EMPTY_STRING, EMPTY_STRING );
482 else if ( nFormatId == SOT_FORMATSTR_ID_DRAWING )
484 SotStorageStreamRef xStm;
485 if( aDataHelper.GetSotStorageStream( SOT_FORMATSTR_ID_DRAWING, xStm ) )
487 MakeDrawLayer(); // before loading model, so 3D factory has been created
489 SvtPathOptions aPathOpt;
490 String aPath = aPathOpt.GetPalettePath();
492 ScDocShellRef aDragShellRef( new ScDocShell );
493 aDragShellRef->DoInitNew(NULL);
494 FmFormModel* pModel = new FmFormModel( aPath, NULL, aDragShellRef );
496 pModel->GetItemPool().FreezeIdRanges();
497 xStm->Seek(0);
499 com::sun::star::uno::Reference< com::sun::star::io::XInputStream > xInputStream( new utl::OInputStreamWrapper( *xStm ) );
500 SvxDrawingLayerImport( pModel, xInputStream );
502 // set everything to right layer:
503 ULONG nObjCount = 0;
504 USHORT nPages = pModel->GetPageCount();
505 for (USHORT i=0; i<nPages; i++)
507 SdrPage* pPage = pModel->GetPage(i);
508 SdrObjListIter aIter( *pPage, IM_DEEPWITHGROUPS );
509 SdrObject* pObject = aIter.Next();
510 while (pObject)
512 if ( pObject->ISA(SdrUnoObj) )
513 pObject->NbcSetLayer(SC_LAYER_CONTROLS);
514 else
515 pObject->NbcSetLayer(SC_LAYER_FRONT);
516 pObject = aIter.Next();
519 nObjCount += pPage->GetObjCount(); // #105888# count group object only once
522 PasteDraw( aPos, pModel, (nObjCount > 1) ); // grouped if more than 1 object
523 delete pModel;
524 aDragShellRef->DoClose();
525 bRet = TRUE;
528 else if ( (nFormatId == SOT_FORMATSTR_ID_BIFF_5) || (nFormatId == SOT_FORMATSTR_ID_BIFF_8) )
530 // do excel import into a clipboard document
531 //TODO/MBA: testing
532 uno::Reference < io::XInputStream > xStm;
533 if( aDataHelper.GetInputStream( nFormatId, xStm ) )
535 #if 0
536 SotStorage aDest( "d:\\test.xls" ); // to see the file
537 pStor->CopyTo( &aDest );
538 #endif
539 ScDocument* pInsDoc = new ScDocument( SCDOCMODE_CLIP );
540 SCTAB nSrcTab = 0; // Biff5 in clipboard: always sheet 0
541 pInsDoc->ResetClip( pDoc, nSrcTab );
543 SfxMedium aMed;
544 aMed.GetItemSet()->Put( SfxUsrAnyItem( SID_INPUTSTREAM, uno::makeAny( xStm ) ) );
545 FltError eErr = ScFormatFilter::Get().ScImportExcel( aMed, pInsDoc, EIF_AUTO );
546 if ( eErr == eERR_OK )
548 ScRange aSource;
549 const ScExtDocOptions* pExtOpt = pInsDoc->GetExtDocOptions();
550 const ScExtTabSettings* pTabSett = pExtOpt ? pExtOpt->GetTabSettings( nSrcTab ) : 0;
551 if( pTabSett && pTabSett->maUsedArea.IsValid() )
553 aSource = pTabSett->maUsedArea;
554 // ensure correct sheet indexes
555 aSource.aStart.SetTab( nSrcTab );
556 aSource.aEnd.SetTab( nSrcTab );
557 // #92240# don't use selection area: if cursor is moved in Excel after Copy, selection
558 // represents the new cursor position and not the copied area
560 else
562 DBG_ERROR("no dimension"); //! possible?
563 SCCOL nFirstCol, nLastCol;
564 SCROW nFirstRow, nLastRow;
565 if ( pInsDoc->GetDataStart( nSrcTab, nFirstCol, nFirstRow ) )
566 pInsDoc->GetCellArea( nSrcTab, nLastCol, nLastRow );
567 else
569 nFirstCol = nLastCol = 0;
570 nFirstRow = nLastRow = 0;
572 aSource = ScRange( nFirstCol, nFirstRow, nSrcTab,
573 nLastCol, nLastRow, nSrcTab );
576 if ( pLogicPos )
578 // position specified (Drag&Drop) - change selection
579 MoveCursorAbs( nPosX, nPosY, SC_FOLLOW_NONE, FALSE, FALSE );
580 Unmark();
583 pInsDoc->SetClipArea( aSource );
584 PasteFromClip( IDF_ALL, pInsDoc,
585 PASTE_NOFUNC, FALSE, FALSE, FALSE, INS_NONE, IDF_NONE,
586 bAllowDialogs );
587 delete pInsDoc;
589 bRet = TRUE;
593 else if ( nFormatId == SOT_FORMAT_FILE )
595 String aFile;
596 if ( aDataHelper.GetString( nFormatId, aFile ) )
597 bRet = PasteFile( aPos, aFile, bLink );
599 else if ( nFormatId == SOT_FORMAT_FILE_LIST )
601 FileList aFileList;
602 if ( aDataHelper.GetFileList( nFormatId, aFileList ) )
604 ULONG nCount = aFileList.Count();
605 for( ULONG i = 0; i < nCount ; i++ )
607 String aFile = aFileList.GetFile( i );
609 PasteFile( aPos, aFile, bLink );
610 #if 0
611 SfxStringItem aNameItem( FID_INSERT_FILE, aFile );
612 SfxPointItem aPosItem( FN_PARAM_1, aPos );
613 SfxDispatcher* pDisp =
614 GetViewData()->GetViewShell()->GetViewFrame()->GetDispatcher();
615 if (pDisp)
616 pDisp->Execute( FID_INSERT_FILE, SFX_CALLMODE_ASYNCHRON,
617 &aNameItem, &aPosItem, (void*)0 );
618 #endif
620 aPos.X() += 400;
621 aPos.Y() += 400;
623 bRet = TRUE;
626 else if ( nFormatId == SOT_FORMATSTR_ID_SOLK ||
627 nFormatId == SOT_FORMATSTR_ID_UNIFORMRESOURCELOCATOR ||
628 nFormatId == SOT_FORMATSTR_ID_NETSCAPE_BOOKMARK ||
629 nFormatId == SOT_FORMATSTR_ID_FILEGRPDESCRIPTOR )
631 bRet = PasteBookmark( nFormatId, rxTransferable, nPosX, nPosY );
634 pDoc->SetPastingDrawFromOtherDoc( FALSE );
636 return bRet;
639 ByteString lcl_GetSubString( sal_Char* pData, long nStart, long nDataSize )
641 if ( nDataSize <= nStart /* || pData[nDataSize] != 0 */ )
643 DBG_ERROR("DDE Data: invalid data");
644 return ByteString();
646 return ByteString( pData + nStart );
649 BOOL ScViewFunc::PasteDDE( const uno::Reference<datatransfer::XTransferable>& rxTransferable )
651 TransferableDataHelper aDataHelper( rxTransferable );
653 // get link data from transferable before string data,
654 // so the source knows it will be used for a link
656 uno::Sequence<sal_Int8> aSequence;
657 if ( !aDataHelper.GetSequence( SOT_FORMATSTR_ID_LINK, aSequence ) )
659 DBG_ERROR("DDE Data not found.");
660 return FALSE;
663 // check size (only if string is available in transferable)
665 USHORT nCols = 1;
666 USHORT nRows = 1;
667 if ( aDataHelper.HasFormat( SOT_FORMAT_STRING ) )
669 String aDataStr;
670 if ( aDataHelper.GetString( SOT_FORMAT_STRING, aDataStr ) )
672 // get size from string the same way as in ScDdeLink::DataChanged
674 aDataStr.ConvertLineEnd(LINEEND_LF);
675 xub_StrLen nLen = aDataStr.Len();
676 if (nLen && aDataStr.GetChar(nLen-1) == '\n')
677 aDataStr.Erase(nLen-1);
679 if (aDataStr.Len())
681 nRows = aDataStr.GetTokenCount( '\n' );
682 String aLine = aDataStr.GetToken( 0, '\n' );
683 if (aLine.Len())
684 nCols = aLine.GetTokenCount( '\t' );
689 // create formula
691 long nSeqLen = aSequence.getLength();
692 sal_Char* pData = (sal_Char*)aSequence.getConstArray();
694 rtl_TextEncoding eSysEnc = gsl_getSystemTextEncoding();
696 ByteString aByteApp = lcl_GetSubString( pData, 0, nSeqLen );
697 ByteString aByteTopic = lcl_GetSubString( pData, aByteApp.Len() + 1, nSeqLen );
698 ByteString aByteItem = lcl_GetSubString( pData, aByteApp.Len() + aByteTopic.Len() + 2, nSeqLen );
700 String aApp( aByteApp, eSysEnc );
701 String aTopic( aByteTopic, eSysEnc );
702 String aItem( aByteItem, eSysEnc );
704 // TODO: we could define ocQuote for "
705 const String aQuote( '"' );
706 const String& sSep = ScCompiler::GetNativeSymbol( ocSep);
707 String aFormula( '=' );
708 aFormula += ScCompiler::GetNativeSymbol( ocDde);
709 aFormula += ScCompiler::GetNativeSymbol( ocOpen);
710 aFormula += aQuote;
711 aFormula += aApp;
712 aFormula += aQuote;
713 aFormula += sSep;
714 aFormula += aQuote;
715 aFormula += aTopic;
716 aFormula += aQuote;
717 aFormula += sSep;
718 aFormula += aQuote;
719 aFormula += aItem;
720 aFormula += aQuote;
721 aFormula += ScCompiler::GetNativeSymbol( ocClose);
723 // mark range
725 SCTAB nTab = GetViewData()->GetTabNo();
726 SCCOL nCurX = GetViewData()->GetCurX();
727 SCROW nCurY = GetViewData()->GetCurY();
728 HideAllCursors();
729 DoneBlockMode();
730 InitBlockMode( nCurX, nCurY, nTab );
731 MarkCursor( nCurX+static_cast<SCCOL>(nCols)-1, nCurY+static_cast<SCROW>(nRows)-1, nTab );
732 ShowAllCursors();
734 // enter formula
736 EnterMatrix( aFormula );
737 CursorPosChanged();
739 return TRUE;