xref: /aoo41x/main/sc/source/ui/view/viewfun5.cxx (revision 45fd3b9a)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_sc.hxx"
26 #include <com/sun/star/embed/XEmbedObjectClipboardCreator.hpp>
27 #include <com/sun/star/embed/Aspects.hpp>
28 
29 
30 #include <svx/unomodel.hxx>
31 #include <unotools/streamwrap.hxx>
32 
33 //------------------------------------------------------------------
34 
35 #include <svx/dbexch.hrc>
36 #include <svx/fmmodel.hxx>
37 #include <svx/svdetc.hxx>
38 #include <svx/svditer.hxx>
39 #include <svx/svdobj.hxx>
40 #include <svx/svdogrp.hxx>
41 #include <svx/svdouno.hxx>
42 #include <svx/svdoole2.hxx>
43 #include <svx/svdpage.hxx>
44 #include <sfx2/dispatch.hxx>
45 #include <sfx2/docfile.hxx>
46 #include <sot/clsids.hxx>
47 #include <sot/formats.hxx>
48 #include <sot/filelist.hxx>
49 #include <unotools/pathoptions.hxx>
50 #include <svl/ptitem.hxx>
51 #include <svl/stritem.hxx>
52 #include <svtools/transfer.hxx>
53 #include <vcl/graph.hxx>
54 
55 #include <comphelper/storagehelper.hxx>
56 #include <comphelper/processfactory.hxx>
57 
58 #include <sot/formats.hxx>
59 #define SOT_FORMATSTR_ID_STARCALC_CURRENT	SOT_FORMATSTR_ID_STARCALC_50
60 
61 #include "viewfunc.hxx"
62 #include "docsh.hxx"
63 #include "drawview.hxx"
64 #include "impex.hxx"
65 #include "dbfunc.hxx"
66 #include "dbcolect.hxx"
67 #include "sc.hrc"
68 #include "filter.hxx"
69 #include "scextopt.hxx"
70 #include "tabvwsh.hxx"		//	wegen GetViewFrame
71 #include "compiler.hxx"
72 
73 #include "asciiopt.hxx"
74 #include "scabstdlg.hxx"
75 #include "clipparam.hxx"
76 #include <vcl/msgbox.hxx>
77 #include <sfx2/viewfrm.hxx>
78 #include <svx/dbaexchange.hxx>
79 
80 using namespace com::sun::star;
81 
82 //------------------------------------------------------------------
83 
84 sal_Bool ScViewFunc::PasteDataFormat( sal_uLong nFormatId,
85 					const uno::Reference<datatransfer::XTransferable>& rxTransferable,
86 					SCCOL nPosX, SCROW nPosY, Point* pLogicPos, sal_Bool bLink, sal_Bool bAllowDialogs )
87 {
88     ScDocument* pDoc = GetViewData()->GetDocument();
89     pDoc->SetPastingDrawFromOtherDoc( sal_True );
90 
91 	Point aPos;						//	inserting position (1/100 mm)
92 	if (pLogicPos)
93 		aPos = *pLogicPos;
94 	else
95 	{
96 		//	inserting position isn't needed for text formats
97 		sal_Bool bIsTextFormat = ( ScImportExport::IsFormatSupported( nFormatId ) ||
98 								nFormatId == FORMAT_RTF );
99 		if ( !bIsTextFormat )
100 		{
101 			//	Window MapMode isn't drawing MapMode if DrawingLayer hasn't been created yet
102 
103 			SCTAB nTab = GetViewData()->GetTabNo();
104 			long nXT = 0;
105 			for (SCCOL i=0; i<nPosX; i++)
106 				nXT += pDoc->GetColWidth(i,nTab);
107 			if (pDoc->IsNegativePage(nTab))
108 				nXT = -nXT;
109 			sal_uLong nYT = pDoc->GetRowHeight( 0, nPosY-1, nTab);
110 			aPos = Point( (long)(nXT * HMM_PER_TWIPS), (long)(nYT * HMM_PER_TWIPS) );
111 		}
112 	}
113 
114 	TransferableDataHelper aDataHelper( rxTransferable );
115 	sal_Bool bRet = sal_False;
116 
117 	//
118 	//	handle individual formats
119 	//
120 
121 	if ( nFormatId == SOT_FORMATSTR_ID_EMBED_SOURCE ||
122 		 nFormatId == SOT_FORMATSTR_ID_LINK_SOURCE ||
123 		 nFormatId == SOT_FORMATSTR_ID_EMBED_SOURCE_OLE ||
124 		 nFormatId == SOT_FORMATSTR_ID_LINK_SOURCE_OLE ||
125 		 nFormatId == SOT_FORMATSTR_ID_EMBEDDED_OBJ_OLE )
126 	{
127         uno::Reference < io::XInputStream > xStm;
128         TransferableObjectDescriptor   aObjDesc;
129 
130 		if( aDataHelper.GetTransferableObjectDescriptor( SOT_FORMATSTR_ID_OBJECTDESCRIPTOR, aObjDesc ) &&
131             aDataHelper.GetInputStream( nFormatId, xStm ) )
132 		{
133             if ( aObjDesc.maClassName == SvGlobalName( SO3_SC_CLASSID_60 ) )
134 			{
135                 uno::Reference < embed::XStorage > xStore = ::comphelper::OStorageHelper::GetStorageFromInputStream( xStm );
136 
137                 // mba: BaseURL doesn't make sense for clipboard
138                 // #i43716# Medium must be allocated with "new".
139                 // DoLoad stores the pointer and deletes it with the SfxObjectShell.
140                 SfxMedium* pMedium = new SfxMedium( xStore, String() );
141 
142                 //  TODO/LATER: is it a problem that we don't support binary formats here?
143 				ScDocShellRef xDocShRef = new ScDocShell(SFX_CREATE_MODE_EMBEDDED);
144                 if (xDocShRef->DoLoad(pMedium))
145 				{
146 					ScDocument* pSrcDoc = xDocShRef->GetDocument();
147 					SCTAB nSrcTab = pSrcDoc->GetVisibleTab();
148 					if (!pSrcDoc->HasTable(nSrcTab))
149 						nSrcTab = 0;
150 
151 					ScMarkData aSrcMark;
152 					aSrcMark.SelectOneTable( nSrcTab );			// for CopyToClip
153 					ScDocument* pClipDoc = new ScDocument( SCDOCMODE_CLIP );
154 
155                     SCCOL nFirstCol, nLastCol;
156                     SCROW nFirstRow, nLastRow;
157                     if ( pSrcDoc->GetDataStart( nSrcTab, nFirstCol, nFirstRow ) )
158 						pSrcDoc->GetCellArea( nSrcTab, nLastCol, nLastRow );
159 					else
160 				    {
161 						nFirstCol = nLastCol = 0;
162 						nFirstRow = nLastRow = 0;
163                     }
164                     ScClipParam aClipParam(ScRange(nFirstCol, nFirstRow, nSrcTab, nLastCol, nLastRow, nSrcTab), false);
165                     pSrcDoc->CopyToClip(aClipParam, pClipDoc, &aSrcMark);
166 					ScGlobal::SetClipDocName( xDocShRef->GetTitle( SFX_TITLE_FULLNAME ) );
167 
168 					SetCursor( nPosX, nPosY );
169 					Unmark();
170 					PasteFromClip( IDF_ALL, pClipDoc,
171 									PASTE_NOFUNC, sal_False, sal_False, sal_False, INS_NONE, IDF_NONE,
172 									bAllowDialogs );
173 					delete pClipDoc;
174 					bRet = sal_True;
175 				}
176 
177                 xDocShRef->DoClose();
178 				xDocShRef.Clear();
179 			}
180 			else
181 			{
182                 ::rtl::OUString aName;
183                 uno::Reference < embed::XEmbeddedObject > xObj = GetViewData()->GetViewShell()->GetObjectShell()->
184                         GetEmbeddedObjectContainer().InsertEmbeddedObject( xStm, aName );
185                 if ( xObj.is() )
186 				{
187 					// try to get the replacement image from the clipboard
188 					Graphic aGraphic;
189 					sal_uLong nGrFormat = 0;
190 // (wg. Selection Manager bei Trustet Solaris)
191 #ifndef SOLARIS
192 /*
193                     if( aDataHelper.GetGraphic( SOT_FORMATSTR_ID_SVXB, aGraphic ) )
194 						nGrFormat = SOT_FORMATSTR_ID_SVXB;
195 					else if( aDataHelper.GetGraphic( FORMAT_GDIMETAFILE, aGraphic ) )
196 						nGrFormat = SOT_FORMAT_GDIMETAFILE;
197 					else if( aDataHelper.GetGraphic( FORMAT_BITMAP, aGraphic ) )
198 						nGrFormat = SOT_FORMAT_BITMAP;
199 */
200 #endif
201 
202 					// insert replacement image ( if there is one ) into the object helper
203 					if ( nGrFormat )
204 					{
205 						datatransfer::DataFlavor aDataFlavor;
206 						SotExchange::GetFormatDataFlavor( nGrFormat, aDataFlavor );
207                     	PasteObject( aPos, xObj, &aObjDesc.maSize, &aGraphic, aDataFlavor.MimeType, aObjDesc.mnViewAspect );
208 					}
209 					else
210                     	PasteObject( aPos, xObj, &aObjDesc.maSize );
211 
212 					bRet = sal_True;
213 				}
214 				else
215 				{
216 					DBG_ERROR("Error in CreateAndLoad");
217 				}
218 			}
219 		}
220 		else
221         {
222 //            uno::Reference < io::XInputStream > xStm;
223 //            TransferableObjectDescriptor    aObjDesc;
224 
225             if ( aDataHelper.GetTransferableObjectDescriptor( SOT_FORMATSTR_ID_OBJECTDESCRIPTOR_OLE, aObjDesc ) )
226 			{
227                 ::rtl::OUString aName;
228                 uno::Reference < embed::XEmbeddedObject > xObj;
229 
230             	if ( aDataHelper.GetInputStream( SOT_FORMATSTR_ID_EMBED_SOURCE_OLE, xStm )
231                   || aDataHelper.GetInputStream( SOT_FORMATSTR_ID_EMBEDDED_OBJ_OLE, xStm ) )
232             	{
233                 	xObj = GetViewData()->GetDocShell()->GetEmbeddedObjectContainer().InsertEmbeddedObject( xStm, aName );
234 				}
235 				else
236 				{
237 					try
238 					{
239 						uno::Reference< embed::XStorage > xTmpStor = ::comphelper::OStorageHelper::GetTemporaryStorage();
240 						uno::Reference < embed::XEmbedObjectClipboardCreator > xClipboardCreator(
241 							::comphelper::getProcessServiceFactory()->createInstance( ::rtl::OUString(
242 											RTL_CONSTASCII_USTRINGPARAM("com.sun.star.embed.MSOLEObjectSystemCreator") ) ),
243 							uno::UNO_QUERY_THROW );
244 
245 						embed::InsertedObjectInfo aInfo = xClipboardCreator->createInstanceInitFromClipboard(
246 															xTmpStor,
247 															::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM ( "DummyName" ) ),
248 															uno::Sequence< beans::PropertyValue >() );
249 
250 						// TODO/LATER: in future InsertedObjectInfo will be used to get container related information
251 						// for example whether the object should be an iconified one
252 						xObj = aInfo.Object;
253 						if ( xObj.is() )
254 							GetViewData()->GetDocShell()->GetEmbeddedObjectContainer().InsertEmbeddedObject( xObj, aName );
255 					}
256 					catch( uno::Exception& )
257 					{}
258 				}
259 
260                 if ( xObj.is() )
261 				{
262 					// try to get the replacement image from the clipboard
263 					Graphic aGraphic;
264                     sal_uLong nGrFormat = 0;
265 
266 // (wg. Selection Manager bei Trustet Solaris)
267 #ifndef SOLARIS
268 					if( aDataHelper.GetGraphic( SOT_FORMATSTR_ID_SVXB, aGraphic ) )
269 						nGrFormat = SOT_FORMATSTR_ID_SVXB;
270 					else if( aDataHelper.GetGraphic( FORMAT_GDIMETAFILE, aGraphic ) )
271 						nGrFormat = SOT_FORMAT_GDIMETAFILE;
272 					else if( aDataHelper.GetGraphic( FORMAT_BITMAP, aGraphic ) )
273 						nGrFormat = SOT_FORMAT_BITMAP;
274 #endif
275 
276 					// insert replacement image ( if there is one ) into the object helper
277 					if ( nGrFormat )
278 					{
279 						datatransfer::DataFlavor aDataFlavor;
280 						SotExchange::GetFormatDataFlavor( nGrFormat, aDataFlavor );
281                     	PasteObject( aPos, xObj, &aObjDesc.maSize, &aGraphic, aDataFlavor.MimeType, aObjDesc.mnViewAspect );
282 					}
283 					else
284                     	PasteObject( aPos, xObj, &aObjDesc.maSize );
285 
286 					// let object stay in loaded state after insertion
287 					SdrOle2Obj::Unload( xObj, embed::Aspects::MSOLE_CONTENT );
288 					bRet = sal_True;
289 				}
290 				else
291 				{
292                     DBG_ERROR("Error creating external OLE object");
293 				}
294 			}
295             //TODO/LATER: if format is not available, create picture
296 		}
297 	}
298 	else if ( nFormatId == SOT_FORMATSTR_ID_LINK )		// LINK is also in ScImportExport
299 	{
300 		bRet = PasteDDE( rxTransferable );
301 	}
302 	else if ( ScImportExport::IsFormatSupported( nFormatId ) || nFormatId == SOT_FORMAT_RTF )
303 	{
304 		if ( nFormatId == SOT_FORMAT_RTF && aDataHelper.HasFormat( SOT_FORMATSTR_ID_EDITENGINE ) )
305 		{
306 			//	use EditView's PasteSpecial / Drop
307 			PasteRTF( nPosX, nPosY, rxTransferable );
308 			bRet = sal_True;
309 		}
310 		else
311 		{
312 			ScAddress aCellPos( nPosX, nPosY, GetViewData()->GetTabNo() );
313 			ScImportExport aObj( GetViewData()->GetDocument(), aCellPos );
314 
315             ::rtl::OUString aStr;
316 			SotStorageStreamRef xStream;
317 			if ( aDataHelper.GetSotStorageStream( nFormatId, xStream ) && xStream.Is() )
318                 // mba: clipboard always must contain absolute URLs (could be from alien source)
319                 bRet = aObj.ImportStream( *xStream, String(), nFormatId );
320 			else if (nFormatId == FORMAT_STRING && aDataHelper.GetString( nFormatId, aStr ))
321             {
322                 // Do CSV dialog if more than one line.
323                 sal_Int32 nDelim = aStr.indexOf('\n');
324 #if 0
325                 ::rtl::OString tmpStr = OUStringToOString( aStr,
326                         RTL_TEXTENCODING_UTF8 );
327                 fprintf( stderr, "String is '%s' (%d) [%d]\n", tmpStr.getStr(),
328                         tmpStr.getLength(), nDelim);
329 #endif
330                 if (nDelim >= 0 && nDelim != aStr.getLength () - 1)
331                 {
332                     ScImportStringStream aStrm( aStr);
333                     ScAbstractDialogFactory* pFact =
334                         ScAbstractDialogFactory::Create();
335                     AbstractScImportAsciiDlg *pDlg =
336                         pFact->CreateScImportAsciiDlg( NULL, String(), &aStrm,
337                                 RID_SCDLG_ASCII);
338 
339                     if (pDlg->Execute() == RET_OK)
340                     {
341                         ScAsciiOptions aOptions;
342                         pDlg->GetOptions( aOptions );
343                         aObj.SetExtOptions( aOptions );
344 
345                         bRet = aObj.ImportString( aStr, nFormatId );
346 
347                         // TODO: what if (aObj.IsOverflow())
348                         // Content was partially pasted, which can be undone by
349                         // the user though.
350                         if (aObj.IsOverflow())
351                             bRet = sal_False;
352                     }
353                     else
354                         bRet = sal_True;
355                         // Yes, no failure, don't raise a "couldn't paste"
356                         // dialog if user cancelled.
357                     delete pDlg;
358                 }
359                 else
360                     bRet = aObj.ImportString( aStr, nFormatId );
361             }
362 			else if (nFormatId != FORMAT_STRING && aDataHelper.GetString( nFormatId, aStr ))
363                 bRet = aObj.ImportString( aStr, nFormatId );
364 
365 			InvalidateAttribs();
366 			GetViewData()->UpdateInputHandler();
367 		}
368 	}
369 	else if (nFormatId == SOT_FORMATSTR_ID_SBA_DATAEXCHANGE)
370 	{
371 		//	import of database data into table
372 
373         const DataFlavorExVector& rVector = aDataHelper.GetDataFlavorExVector();
374         if ( svx::ODataAccessObjectTransferable::canExtractObjectDescriptor(rVector) )
375         {
376             // transport the whole ODataAccessDescriptor as slot parameter
377             svx::ODataAccessDescriptor aDesc = svx::ODataAccessObjectTransferable::extractObjectDescriptor(aDataHelper);
378             uno::Any aDescAny;
379             uno::Sequence<beans::PropertyValue> aProperties = aDesc.createPropertyValueSequence();
380             aDescAny <<= aProperties;
381             SfxUsrAnyItem aDataDesc(SID_SBA_IMPORT, aDescAny);
382 
383 			ScDocShell* pDocSh = GetViewData()->GetDocShell();
384 			SCTAB nTab = GetViewData()->GetTabNo();
385 
386 			ClickCursor(nPosX, nPosY, sal_False);				// set cursor position
387 
388 			//	Creation of database area "Import1" isn't here, but in the DocShell
389 			//	slot execute, so it can be added to the undo action
390 
391 			ScDBData* pDBData = pDocSh->GetDBData( ScRange(nPosX,nPosY,nTab), SC_DB_OLD, SC_DBSEL_KEEP );
392 			String sTarget;
393 			if (pDBData)
394 				sTarget = pDBData->GetName();
395 			else
396 			{
397 				ScAddress aCellPos( nPosX,nPosY,nTab );
398 				aCellPos.Format( sTarget, SCA_ABS_3D, pDoc, pDoc->GetAddressConvention() );
399 			}
400 			SfxStringItem aTarget(FN_PARAM_1, sTarget);
401 
402 			sal_Bool bAreaIsNew = !pDBData;
403 			SfxBoolItem aAreaNew(FN_PARAM_2, bAreaIsNew);
404 
405 			//	asynchronous, to avoid doing the whole import in drop handler
406 			SfxDispatcher& rDisp = GetViewData()->GetDispatcher();
407 			rDisp.Execute(SID_SBA_IMPORT, SFX_CALLMODE_ASYNCHRON,
408                                         &aDataDesc, &aTarget, &aAreaNew, (void*)0 );
409 
410 			bRet = sal_True;
411 		}
412 	}
413 	else if (nFormatId == SOT_FORMATSTR_ID_SBA_FIELDDATAEXCHANGE)
414 	{
415 		//	insert database field control
416 
417         if ( ::svx::OColumnTransferable::canExtractColumnDescriptor( aDataHelper.GetDataFlavorExVector(), CTF_COLUMN_DESCRIPTOR | CTF_CONTROL_EXCHANGE ) )
418 		{
419 			MakeDrawLayer();
420 			ScDrawView* pScDrawView = GetScDrawView();
421             SdrObject* pObj = pScDrawView->CreateFieldControl( ::svx::OColumnTransferable::extractColumnDescriptor( aDataHelper ) );
422 			if (pObj)
423 			{
424 				Point aInsPos = aPos;
425 				Rectangle aRect(pObj->GetLogicRect());
426 				aInsPos.X() -= aRect.GetSize().Width()  / 2;
427 				aInsPos.Y() -= aRect.GetSize().Height() / 2;
428 				if ( aInsPos.X() < 0 ) aInsPos.X() = 0;
429 				if ( aInsPos.Y() < 0 ) aInsPos.Y() = 0;
430 				aRect.SetPos(aInsPos);
431 				pObj->SetLogicRect(aRect);
432 
433 				if ( pObj->ISA(SdrUnoObj) )
434 					pObj->NbcSetLayer(SC_LAYER_CONTROLS);
435 				else
436 					pObj->NbcSetLayer(SC_LAYER_FRONT);
437 				if (pObj->ISA(SdrObjGroup))
438 				{
439 					SdrObjListIter aIter( *pObj, IM_DEEPWITHGROUPS );
440 					SdrObject* pSubObj = aIter.Next();
441 					while (pSubObj)
442 					{
443 						if ( pSubObj->ISA(SdrUnoObj) )
444 							pSubObj->NbcSetLayer(SC_LAYER_CONTROLS);
445 						else
446 							pSubObj->NbcSetLayer(SC_LAYER_FRONT);
447 						pSubObj = aIter.Next();
448 					}
449 				}
450 
451 				pScDrawView->InsertObjectSafe(pObj, *pScDrawView->GetSdrPageView());
452 
453 				GetViewData()->GetViewShell()->SetDrawShell( sal_True );
454 				bRet = sal_True;
455 			}
456 		}
457 	}
458 	else if (nFormatId == SOT_FORMAT_BITMAP)
459 	{
460 		BitmapEx aBmpEx;
461 		if( aDataHelper.GetBitmapEx( FORMAT_BITMAP, aBmpEx ) )
462 			bRet = PasteBitmapEx( aPos, aBmpEx );
463 	}
464 	else if (nFormatId == SOT_FORMAT_GDIMETAFILE)
465 	{
466 		GDIMetaFile aMtf;
467 		if( aDataHelper.GetGDIMetaFile( FORMAT_GDIMETAFILE, aMtf ) )
468 			bRet = PasteMetaFile( aPos, aMtf );
469 	}
470 	else if (nFormatId == SOT_FORMATSTR_ID_SVXB)
471 	{
472 		SotStorageStreamRef xStm;
473 		if( aDataHelper.GetSotStorageStream( SOT_FORMATSTR_ID_SVXB, xStm ) )
474 		{
475 			Graphic aGraphic;
476 			*xStm >> aGraphic;
477 			bRet = PasteGraphic( aPos, aGraphic, EMPTY_STRING, EMPTY_STRING );
478 		}
479 	}
480 	else if ( nFormatId == SOT_FORMATSTR_ID_DRAWING )
481 	{
482 		SotStorageStreamRef xStm;
483 		if( aDataHelper.GetSotStorageStream( SOT_FORMATSTR_ID_DRAWING, xStm ) )
484 		{
485 			MakeDrawLayer();	// before loading model, so 3D factory has been created
486 
487 			SvtPathOptions aPathOpt;
488 			String aPath = aPathOpt.GetPalettePath();
489 
490 			ScDocShellRef aDragShellRef( new ScDocShell );
491 			aDragShellRef->DoInitNew(NULL);
492 			FmFormModel* pModel = new FmFormModel( aPath, NULL, aDragShellRef );
493 
494 			pModel->GetItemPool().FreezeIdRanges();
495 			xStm->Seek(0);
496 
497 			com::sun::star::uno::Reference< com::sun::star::io::XInputStream > xInputStream( new utl::OInputStreamWrapper( *xStm ) );
498 			SvxDrawingLayerImport( pModel, xInputStream );
499 
500 			// set everything to right layer:
501 			sal_uLong nObjCount = 0;
502 			sal_uInt16 nPages = pModel->GetPageCount();
503 			for (sal_uInt16 i=0; i<nPages; i++)
504 			{
505 				SdrPage* pPage = pModel->GetPage(i);
506 				SdrObjListIter aIter( *pPage, IM_DEEPWITHGROUPS );
507 				SdrObject* pObject = aIter.Next();
508 				while (pObject)
509 				{
510 					if ( pObject->ISA(SdrUnoObj) )
511 						pObject->NbcSetLayer(SC_LAYER_CONTROLS);
512 					else
513 						pObject->NbcSetLayer(SC_LAYER_FRONT);
514 					pObject = aIter.Next();
515 				}
516 
517 				nObjCount += pPage->GetObjCount();			// #105888# count group object only once
518 			}
519 
520 			PasteDraw( aPos, pModel, (nObjCount > 1) );		// grouped if more than 1 object
521 			delete pModel;
522 			aDragShellRef->DoClose();
523 			bRet = sal_True;
524 		}
525 	}
526     else if ( (nFormatId == SOT_FORMATSTR_ID_BIFF_5) || (nFormatId == SOT_FORMATSTR_ID_BIFF_8) )
527 	{
528 		//	do excel import into a clipboard document
529         //TODO/MBA: testing
530         uno::Reference < io::XInputStream > xStm;
531         if( aDataHelper.GetInputStream( nFormatId, xStm ) )
532 		{
533 #if 0
534 			SotStorage aDest( "d:\\test.xls" );	// to see the file
535 			pStor->CopyTo( &aDest );
536 #endif
537 			ScDocument* pInsDoc = new ScDocument( SCDOCMODE_CLIP );
538 			SCTAB nSrcTab = 0;		// Biff5 in clipboard: always sheet 0
539 			pInsDoc->ResetClip( pDoc, nSrcTab );
540 
541             SfxMedium aMed;
542             aMed.GetItemSet()->Put( SfxUsrAnyItem( SID_INPUTSTREAM, uno::makeAny( xStm ) ) );
543             FltError eErr = ScFormatFilter::Get().ScImportExcel( aMed, pInsDoc, EIF_AUTO );
544 			if ( eErr == eERR_OK )
545 			{
546 				ScRange aSource;
547 				const ScExtDocOptions* pExtOpt = pInsDoc->GetExtDocOptions();
548                 const ScExtTabSettings* pTabSett = pExtOpt ? pExtOpt->GetTabSettings( nSrcTab ) : 0;
549                 if( pTabSett && pTabSett->maUsedArea.IsValid() )
550 				{
551                     aSource = pTabSett->maUsedArea;
552                     // ensure correct sheet indexes
553                     aSource.aStart.SetTab( nSrcTab );
554                     aSource.aEnd.SetTab( nSrcTab );
555 // #92240# don't use selection area: if cursor is moved in Excel after Copy, selection
556 // represents the new cursor position and not the copied area
557 				}
558 				else
559 				{
560 					DBG_ERROR("no dimension");	//!	possible?
561                     SCCOL nFirstCol, nLastCol;
562                     SCROW nFirstRow, nLastRow;
563                     if ( pInsDoc->GetDataStart( nSrcTab, nFirstCol, nFirstRow ) )
564 						pInsDoc->GetCellArea( nSrcTab, nLastCol, nLastRow );
565 					else
566 					{
567 						nFirstCol = nLastCol = 0;
568 						nFirstRow = nLastRow = 0;
569 					}
570 					aSource = ScRange( nFirstCol, nFirstRow, nSrcTab,
571 										nLastCol, nLastRow, nSrcTab );
572 				}
573 
574 				if ( pLogicPos )
575 				{
576 					// position specified (Drag&Drop) - change selection
577 					MoveCursorAbs( nPosX, nPosY, SC_FOLLOW_NONE, sal_False, sal_False );
578 					Unmark();
579 				}
580 
581 				pInsDoc->SetClipArea( aSource );
582 				PasteFromClip( IDF_ALL, pInsDoc,
583 								PASTE_NOFUNC, sal_False, sal_False, sal_False, INS_NONE, IDF_NONE,
584 								bAllowDialogs );
585 				delete pInsDoc;
586 
587 				bRet = sal_True;
588 			}
589 		}
590 	}
591 	else if ( nFormatId == SOT_FORMAT_FILE )
592 	{
593         String aFile;
594         if ( aDataHelper.GetString( nFormatId, aFile ) )
595             bRet = PasteFile( aPos, aFile, bLink );
596     }
597     else if ( nFormatId == SOT_FORMAT_FILE_LIST )
598     {
599         FileList aFileList;
600         if ( aDataHelper.GetFileList( nFormatId, aFileList ) )
601         {
602             sal_uLong nCount = aFileList.Count();
603             for( sal_uLong i = 0; i < nCount ; i++ )
604 			{
605                 String aFile = aFileList.GetFile( i );
606 
607                 PasteFile( aPos, aFile, bLink );
608 #if 0
609 				SfxStringItem aNameItem( FID_INSERT_FILE, aFile );
610 				SfxPointItem aPosItem( FN_PARAM_1, aPos );
611 				SfxDispatcher* pDisp =
612 					GetViewData()->GetViewShell()->GetViewFrame()->GetDispatcher();
613 				if (pDisp)
614 					pDisp->Execute( FID_INSERT_FILE, SFX_CALLMODE_ASYNCHRON,
615 										&aNameItem, &aPosItem, (void*)0 );
616 #endif
617 
618 				aPos.X() += 400;
619 				aPos.Y() += 400;
620 			}
621 			bRet = sal_True;
622 		}
623 	}
624 	else if ( nFormatId == SOT_FORMATSTR_ID_SOLK ||
625 			  nFormatId == SOT_FORMATSTR_ID_UNIFORMRESOURCELOCATOR ||
626 			  nFormatId == SOT_FORMATSTR_ID_NETSCAPE_BOOKMARK ||
627 			  nFormatId == SOT_FORMATSTR_ID_FILEGRPDESCRIPTOR )
628 	{
629 		bRet = PasteBookmark( nFormatId, rxTransferable, nPosX, nPosY );
630 	}
631 
632     pDoc->SetPastingDrawFromOtherDoc( sal_False );
633 
634 	return bRet;
635 }
636 
637 ByteString lcl_GetSubString( sal_Char* pData, long nStart, long nDataSize )
638 {
639 	if ( nDataSize <= nStart /* || pData[nDataSize] != 0 */ )
640 	{
641 		DBG_ERROR("DDE Data: invalid data");
642 		return ByteString();
643 	}
644 	return ByteString( pData + nStart );
645 }
646 
647 sal_Bool ScViewFunc::PasteDDE( const uno::Reference<datatransfer::XTransferable>& rxTransferable )
648 {
649 	TransferableDataHelper aDataHelper( rxTransferable );
650 
651 	//	get link data from transferable before string data,
652 	//	so the source knows it will be used for a link
653 
654 	uno::Sequence<sal_Int8> aSequence;
655 	if ( !aDataHelper.GetSequence( SOT_FORMATSTR_ID_LINK, aSequence ) )
656 	{
657 		DBG_ERROR("DDE Data not found.");
658 		return sal_False;
659 	}
660 
661 	//	check size (only if string is available in transferable)
662 
663 	sal_uInt16 nCols = 1;
664 	sal_uInt16 nRows = 1;
665 	if ( aDataHelper.HasFormat( SOT_FORMAT_STRING ) )
666 	{
667 		String aDataStr;
668 		if ( aDataHelper.GetString( SOT_FORMAT_STRING, aDataStr ) )
669 		{
670 			//	get size from string the same way as in ScDdeLink::DataChanged
671 
672 			aDataStr.ConvertLineEnd(LINEEND_LF);
673 			xub_StrLen nLen = aDataStr.Len();
674 			if (nLen && aDataStr.GetChar(nLen-1) == '\n')
675 				aDataStr.Erase(nLen-1);
676 
677 			if (aDataStr.Len())
678 			{
679 				nRows = aDataStr.GetTokenCount( '\n' );
680 				String aLine = aDataStr.GetToken( 0, '\n' );
681 				if (aLine.Len())
682 					nCols = aLine.GetTokenCount( '\t' );
683 			}
684 		}
685 	}
686 
687 	//	create formula
688 
689 	long nSeqLen = aSequence.getLength();
690 	sal_Char* pData = (sal_Char*)aSequence.getConstArray();
691 
692 	rtl_TextEncoding eSysEnc = gsl_getSystemTextEncoding();
693 
694     ByteString aByteApp   = lcl_GetSubString( pData, 0, nSeqLen );
695     ByteString aByteTopic = lcl_GetSubString( pData, aByteApp.Len() + 1, nSeqLen );
696     ByteString aByteItem  = lcl_GetSubString( pData, aByteApp.Len() + aByteTopic.Len() + 2, nSeqLen );
697 
698     String aApp( aByteApp, eSysEnc );
699     String aTopic( aByteTopic, eSysEnc );
700     String aItem( aByteItem, eSysEnc );
701 
702     // TODO: we could define ocQuote for "
703     const String aQuote( '"' );
704     const String& sSep = ScCompiler::GetNativeSymbol( ocSep);
705     String aFormula( '=' );
706     aFormula += ScCompiler::GetNativeSymbol( ocDde);
707     aFormula += ScCompiler::GetNativeSymbol( ocOpen);
708     aFormula += aQuote;
709     aFormula += aApp;
710     aFormula += aQuote;
711     aFormula += sSep;
712     aFormula += aQuote;
713     aFormula += aTopic;
714     aFormula += aQuote;
715     aFormula += sSep;
716     aFormula += aQuote;
717     aFormula += aItem;
718     aFormula += aQuote;
719     aFormula += ScCompiler::GetNativeSymbol( ocClose);
720 
721 	//	mark range
722 
723 	SCTAB nTab = GetViewData()->GetTabNo();
724 	SCCOL nCurX = GetViewData()->GetCurX();
725 	SCROW nCurY = GetViewData()->GetCurY();
726 	HideAllCursors();
727 	DoneBlockMode();
728 	InitBlockMode( nCurX, nCurY, nTab );
729 	MarkCursor( nCurX+static_cast<SCCOL>(nCols)-1, nCurY+static_cast<SCROW>(nRows)-1, nTab );
730 	ShowAllCursors();
731 
732 	//	enter formula
733 
734 	EnterMatrix( aFormula );
735 	CursorPosChanged();
736 
737 	return sal_True;
738 }
739 
740 
741