xref: /AOO41X/main/sc/source/ui/app/transobj.cxx (revision 6a6ec68d792bd477e5b23798f42a1a9de0925497)
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 
27 // INCLUDE ---------------------------------------------------------------
28 
29 
30 
31 #include "scitems.hxx"
32 #include <editeng/eeitem.hxx>
33 
34 
35 #include <com/sun/star/uno/Sequence.hxx>
36 #include <com/sun/star/embed/XTransactedObject.hpp>
37 
38 #include <unotools/tempfile.hxx>
39 #include <unotools/ucbstreamhelper.hxx>
40 #include <comphelper/storagehelper.hxx>
41 #include <sot/storage.hxx>
42 #include <vcl/svapp.hxx>
43 #include <vcl/virdev.hxx>
44 #include <vos/mutex.hxx>
45 #include <sfx2/app.hxx>
46 #include <sfx2/docfile.hxx>
47 
48 #include "transobj.hxx"
49 #include "document.hxx"
50 #include "viewopti.hxx"
51 #include "editutil.hxx"
52 #include "impex.hxx"
53 #include "cell.hxx"
54 #include "printfun.hxx"
55 #include "docfunc.hxx"
56 #include "scmod.hxx"
57 
58 // for InitDocShell
59 #include <editeng/paperinf.hxx>
60 #include <editeng/sizeitem.hxx>
61 #include <svx/algitem.hxx>
62 #include <svl/intitem.hxx>
63 #include <svl/zforlist.hxx>
64 #include "docsh.hxx"
65 #include "markdata.hxx"
66 #include "stlpool.hxx"
67 #include "viewdata.hxx"
68 #include "dociter.hxx"
69 #include "cellsuno.hxx"
70 
71 using namespace com::sun::star;
72 
73 // -----------------------------------------------------------------------
74 
75 #define SCTRANS_TYPE_IMPEX          1
76 #define SCTRANS_TYPE_EDIT_RTF       2
77 #define SCTRANS_TYPE_EDIT_BIN       3
78 #define SCTRANS_TYPE_EMBOBJ         4
79 
80 // -----------------------------------------------------------------------
81 
82 // static
83 void ScTransferObj::GetAreaSize( ScDocument* pDoc, SCTAB nTab1, SCTAB nTab2, SCROW& nRow, SCCOL& nCol )
84 {
85     SCCOL nMaxCol = 0;
86     SCROW nMaxRow = 0;
87     for( SCTAB nTab = nTab1; nTab <= nTab2; nTab++ )
88     {
89         SCCOL nLastCol = 0;
90         SCROW nLastRow = 0;
91         // GetPrintArea instead of GetCellArea - include drawing objects
92         if( pDoc->GetPrintArea( nTab, nLastCol, nLastRow ) )
93         {
94             if( nLastCol > nMaxCol )
95                 nMaxCol = nLastCol;
96             if( nLastRow > nMaxRow  )
97                 nMaxRow = nLastRow;
98         }
99     }
100     nRow = nMaxRow;
101     nCol = nMaxCol;
102 }
103 
104 // static
105 void ScTransferObj::PaintToDev( OutputDevice* pDev, ScDocument* pDoc, double nPrintFactor,
106                                 const ScRange& rBlock, sal_Bool bMetaFile )
107 {
108     if (!pDoc)
109         return;
110 
111     Point aPoint;
112     Rectangle aBound( aPoint, pDev->GetOutputSize() );      //! use size from clip area?
113 
114     ScViewData aViewData(NULL,NULL);
115     aViewData.InitData( pDoc );
116 
117     aViewData.SetTabNo( rBlock.aEnd.Tab() );
118     aViewData.SetScreen( rBlock.aStart.Col(), rBlock.aStart.Row(),
119                             rBlock.aEnd.Col(), rBlock.aEnd.Row() );
120 
121     ScPrintFunc::DrawToDev( pDoc, pDev, nPrintFactor, aBound, &aViewData, bMetaFile );
122 }
123 
124 // -----------------------------------------------------------------------
125 
126 ScTransferObj::ScTransferObj( ScDocument* pClipDoc, const TransferableObjectDescriptor& rDesc ) :
127     pDoc( pClipDoc ),
128     aObjDesc( rDesc ),
129     nDragHandleX( 0 ),
130     nDragHandleY( 0 ),
131     nDragSourceFlags( 0 ),
132     bDragWasInternal( sal_False ),
133     bUsedForLink( sal_False ),
134     bUseInApi( false )
135 {
136     DBG_ASSERT(pDoc->IsClipboard(), "wrong document");
137 
138     //
139     // get aBlock from clipboard doc
140     //
141 
142     SCCOL nCol1;
143     SCROW nRow1;
144     SCCOL nCol2;
145     SCROW nRow2;
146     pDoc->GetClipStart( nCol1, nRow1 );
147     pDoc->GetClipArea( nCol2, nRow2, sal_True );    // real source area - include filtered rows
148     nCol2 = sal::static_int_cast<SCCOL>( nCol2 + nCol1 );
149     nRow2 = sal::static_int_cast<SCROW>( nRow2 + nRow1 );
150 
151     SCCOL nDummy;
152     pDoc->GetClipArea( nDummy, nNonFiltered, sal_False );
153     bHasFiltered = (nNonFiltered < (nRow2 - nRow1));
154     ++nNonFiltered;     // to get count instead of diff
155 
156     SCTAB nTab1=0;
157     SCTAB nTab2=0;
158     sal_Bool bFirst = sal_True;
159     for (SCTAB i=0; i<=MAXTAB; i++)
160         if (pDoc->HasTable(i))
161         {
162             if (bFirst)
163                 nTab1 = i;
164             nTab2 = i;
165             bFirst = sal_False;
166         }
167     DBG_ASSERT(!bFirst, "no sheet selected");
168 
169     //  only limit to used cells if whole sheet was marked
170     //  (so empty cell areas can be copied)
171     if ( nCol2>=MAXCOL && nRow2>=MAXROW )
172     {
173         SCROW nMaxRow;
174         SCCOL nMaxCol;
175         GetAreaSize( pDoc, nTab1, nTab2, nMaxRow, nMaxCol );
176         if( nMaxRow < nRow2 )
177             nRow2 = nMaxRow;
178         if( nMaxCol < nCol2 )
179             nCol2 = nMaxCol;
180     }
181 
182     aBlock = ScRange( nCol1, nRow1, nTab1, nCol2, nRow2, nTab2 );
183     nVisibleTab = nTab1;    // valid table as default
184 
185     Rectangle aMMRect = pDoc->GetMMRect( nCol1,nRow1, nCol2,nRow2, nTab1 );
186     aObjDesc.maSize = aMMRect.GetSize();
187     PrepareOLE( aObjDesc );
188 }
189 
190 ScTransferObj::~ScTransferObj()
191 {
192     Application::GetSolarMutex().acquire();
193 
194     ScModule* pScMod = SC_MOD();
195     if ( pScMod->GetClipData().pCellClipboard == this )
196     {
197         DBG_ERROR("ScTransferObj wasn't released");
198         pScMod->SetClipObject( NULL, NULL );
199     }
200     if ( pScMod->GetDragData().pCellTransfer == this )
201     {
202         DBG_ERROR("ScTransferObj wasn't released");
203         pScMod->ResetDragObject();
204     }
205 
206     delete pDoc;        // ScTransferObj is owner of clipboard document
207 
208     aDocShellRef.Clear();   // before releasing the mutex
209 
210     aDrawPersistRef.Clear();                    // after the model
211 
212     Application::GetSolarMutex().release();
213 }
214 
215 // static
216 ScTransferObj* ScTransferObj::GetOwnClipboard( Window* pUIWin )
217 {
218     ScTransferObj* pObj = SC_MOD()->GetClipData().pCellClipboard;
219     if ( pObj && pUIWin )
220     {
221         //  check formats to see if pObj is really in the system clipboard
222 
223         //  pUIWin is NULL when called from core (IsClipboardSource),
224         //  in that case don't access the system clipboard, because the call
225         //  may be from other clipboard operations (like flushing, #86059#)
226 
227         TransferableDataHelper aDataHelper( TransferableDataHelper::CreateFromSystemClipboard( pUIWin ) );
228         if ( !aDataHelper.HasFormat( SOT_FORMATSTR_ID_DIF ) )
229         {
230 //          DBG_ERROR("ScTransferObj wasn't released");
231             pObj = NULL;
232         }
233     }
234     return pObj;
235 }
236 
237 void ScTransferObj::AddSupportedFormats()
238 {
239     AddFormat( SOT_FORMATSTR_ID_EMBED_SOURCE );
240     AddFormat( SOT_FORMATSTR_ID_OBJECTDESCRIPTOR );
241     AddFormat( SOT_FORMAT_GDIMETAFILE );
242     AddFormat( SOT_FORMAT_BITMAP );
243 
244     // ScImportExport formats
245     AddFormat( SOT_FORMATSTR_ID_HTML );
246     AddFormat( SOT_FORMATSTR_ID_SYLK );
247     AddFormat( SOT_FORMATSTR_ID_LINK );
248     AddFormat( SOT_FORMATSTR_ID_DIF );
249     AddFormat( SOT_FORMAT_STRING );
250 
251     AddFormat( SOT_FORMAT_RTF );
252     if ( aBlock.aStart == aBlock.aEnd )
253         AddFormat( SOT_FORMATSTR_ID_EDITENGINE );
254 }
255 
256 sal_Bool ScTransferObj::GetData( const datatransfer::DataFlavor& rFlavor )
257 {
258     sal_uInt32  nFormat = SotExchange::GetFormat( rFlavor );
259     sal_Bool    bOK = sal_False;
260 
261     if( HasFormat( nFormat ) )
262     {
263         if ( nFormat == SOT_FORMATSTR_ID_LINKSRCDESCRIPTOR || nFormat == SOT_FORMATSTR_ID_OBJECTDESCRIPTOR )
264         {
265             bOK = SetTransferableObjectDescriptor( aObjDesc, rFlavor );
266         }
267         else if ( ( nFormat == SOT_FORMAT_RTF || nFormat == SOT_FORMATSTR_ID_EDITENGINE ) &&
268                         aBlock.aStart == aBlock.aEnd )
269         {
270             //  RTF from a single cell is handled by EditEngine
271 
272             SCCOL nCol = aBlock.aStart.Col();
273             SCROW nRow = aBlock.aStart.Row();
274             SCTAB nTab = aBlock.aStart.Tab();
275 
276             const ScPatternAttr* pPattern = pDoc->GetPattern( nCol, nRow, nTab );
277             ScTabEditEngine aEngine( *pPattern, pDoc->GetEditPool() );
278             ScBaseCell* pCell = NULL;
279             pDoc->GetCell( nCol, nRow, nTab, pCell );
280             if (pCell)
281             {
282                 if (pCell->GetCellType() == CELLTYPE_EDIT)
283                 {
284                     const EditTextObject* pObj;
285                     ((ScEditCell*)pCell)->GetData(pObj);
286                     aEngine.SetText( *pObj );
287                 }
288                 else
289                 {
290                     String aText;
291                     pDoc->GetString( nCol, nRow, nTab, aText );
292                     aEngine.SetText(aText);
293                 }
294             }
295 
296             bOK = SetObject( &aEngine,
297                             (nFormat == FORMAT_RTF) ? SCTRANS_TYPE_EDIT_RTF : SCTRANS_TYPE_EDIT_BIN,
298                             rFlavor );
299         }
300         else if ( ScImportExport::IsFormatSupported( nFormat ) || nFormat == SOT_FORMAT_RTF )
301         {
302             //  if this transfer object was used to create a DDE link, filtered rows
303             //  have to be included for subsequent calls (to be consistent with link data)
304             if ( nFormat == SOT_FORMATSTR_ID_LINK )
305                 bUsedForLink = sal_True;
306 
307             sal_Bool bIncludeFiltered = pDoc->IsCutMode() || bUsedForLink;
308 
309             ScImportExport aObj( pDoc, aBlock );
310             if ( bUsedForLink )
311                 aObj.SetExportTextOptions( ScExportTextOptions( ScExportTextOptions::ToSpace, ' ', false ) );
312             aObj.SetFormulas( pDoc->GetViewOptions().GetOption( VOPT_FORMULAS ) );
313             aObj.SetIncludeFiltered( bIncludeFiltered );
314 
315             //  DataType depends on format type:
316 
317             if ( rFlavor.DataType.equals( ::getCppuType( (const ::rtl::OUString*) 0 ) ) )
318             {
319                 rtl::OUString aString;
320                 if ( aObj.ExportString( aString, nFormat ) )
321                     bOK = SetString( aString, rFlavor );
322             }
323             else if ( rFlavor.DataType.equals( ::getCppuType( (const uno::Sequence< sal_Int8 >*) 0 ) ) )
324             {
325                 //  SetObject converts a stream into a Int8-Sequence
326                 bOK = SetObject( &aObj, SCTRANS_TYPE_IMPEX, rFlavor );
327             }
328             else
329             {
330                 DBG_ERROR("unknown DataType");
331             }
332         }
333         else if ( nFormat == SOT_FORMAT_BITMAP )
334         {
335             Rectangle aMMRect = pDoc->GetMMRect( aBlock.aStart.Col(), aBlock.aStart.Row(),
336                                                  aBlock.aEnd.Col(), aBlock.aEnd.Row(),
337                                                  aBlock.aStart.Tab() );
338             VirtualDevice aVirtDev;
339             aVirtDev.SetOutputSizePixel( aVirtDev.LogicToPixel( aMMRect.GetSize(), MAP_100TH_MM ) );
340 
341             PaintToDev( &aVirtDev, pDoc, 1.0, aBlock, sal_False );
342 
343             aVirtDev.SetMapMode( MapMode( MAP_PIXEL ) );
344             Bitmap aBmp = aVirtDev.GetBitmap( Point(), aVirtDev.GetOutputSize() );
345             bOK = SetBitmapEx( aBmp, rFlavor );
346         }
347         else if ( nFormat == SOT_FORMAT_GDIMETAFILE )
348         {
349             InitDocShell();
350             SfxObjectShell* pEmbObj = aDocShellRef;
351 
352             // like SvEmbeddedTransfer::GetData:
353 
354             GDIMetaFile     aMtf;
355             VirtualDevice   aVDev;
356             MapMode         aMapMode( pEmbObj->GetMapUnit() );
357             Rectangle       aVisArea( pEmbObj->GetVisArea( ASPECT_CONTENT ) );
358 
359             aVDev.EnableOutput( sal_False );
360             aVDev.SetMapMode( aMapMode );
361             aMtf.SetPrefSize( aVisArea.GetSize() );
362             aMtf.SetPrefMapMode( aMapMode );
363             aMtf.Record( &aVDev );
364 
365             pEmbObj->DoDraw( &aVDev, Point(), aVisArea.GetSize(), JobSetup(), ASPECT_CONTENT );
366 
367             aMtf.Stop();
368             aMtf.WindStart();
369 
370             bOK = SetGDIMetaFile( aMtf, rFlavor );
371         }
372         else if ( nFormat == SOT_FORMATSTR_ID_EMBED_SOURCE )
373         {
374             //TODO/LATER: differentiate between formats?!
375             InitDocShell();         // set aDocShellRef
376 
377             SfxObjectShell* pEmbObj = aDocShellRef;
378             bOK = SetObject( pEmbObj, SCTRANS_TYPE_EMBOBJ, rFlavor );
379         }
380     }
381     return bOK;
382 }
383 
384 sal_Bool ScTransferObj::WriteObject( SotStorageStreamRef& rxOStm, void* pUserObject, sal_uInt32 nUserObjectId,
385                                         const datatransfer::DataFlavor& rFlavor )
386 {
387     // called from SetObject, put data into stream
388 
389     sal_Bool bRet = sal_False;
390     switch (nUserObjectId)
391     {
392         case SCTRANS_TYPE_IMPEX:
393             {
394                 ScImportExport* pImpEx = (ScImportExport*)pUserObject;
395 
396                 sal_uInt32 nFormat = SotExchange::GetFormat( rFlavor );
397                 // mba: no BaseURL for data exchange
398                 if ( pImpEx->ExportStream( *rxOStm, String(), nFormat ) )
399                     bRet = ( rxOStm->GetError() == ERRCODE_NONE );
400             }
401             break;
402 
403         case SCTRANS_TYPE_EDIT_RTF:
404         case SCTRANS_TYPE_EDIT_BIN:
405             {
406                 ScTabEditEngine* pEngine = (ScTabEditEngine*)pUserObject;
407                 if ( nUserObjectId == SCTRANS_TYPE_EDIT_RTF )
408                 {
409                     pEngine->Write( *rxOStm, EE_FORMAT_RTF );
410                     bRet = ( rxOStm->GetError() == ERRCODE_NONE );
411                 }
412                 else
413                 {
414                     //  #107722# can't use Write for EditEngine format because that would
415                     //  write old format without support for unicode characters.
416                     //  Get the data from the EditEngine's transferable instead.
417 
418                     sal_uInt16 nParCnt = pEngine->GetParagraphCount();
419                     if ( nParCnt == 0 )
420                         nParCnt = 1;
421                     ESelection aSel( 0, 0, nParCnt-1, pEngine->GetTextLen(nParCnt-1) );
422 
423                     uno::Reference<datatransfer::XTransferable> xEditTrans = pEngine->CreateTransferable( aSel );
424                     TransferableDataHelper aEditHelper( xEditTrans );
425 
426                     bRet = aEditHelper.GetSotStorageStream( rFlavor, rxOStm );
427                 }
428             }
429             break;
430 
431         case SCTRANS_TYPE_EMBOBJ:
432             {
433                 // TODO/MBA: testing
434                 SfxObjectShell*   pEmbObj = (SfxObjectShell*) pUserObject;
435                 ::utl::TempFile     aTempFile;
436                 aTempFile.EnableKillingFile();
437                 uno::Reference< embed::XStorage > xWorkStore =
438                     ::comphelper::OStorageHelper::GetStorageFromURL( aTempFile.GetURL(), embed::ElementModes::READWRITE );
439 
440                 // write document storage
441                 pEmbObj->SetupStorage( xWorkStore, SOFFICE_FILEFORMAT_CURRENT, sal_False );
442 
443                 // mba: no relative ULRs for clipboard!
444                 SfxMedium aMedium( xWorkStore, String() );
445                 bRet = pEmbObj->DoSaveObjectAs( aMedium, sal_False );
446                 pEmbObj->DoSaveCompleted();
447 
448                 uno::Reference< embed::XTransactedObject > xTransact( xWorkStore, uno::UNO_QUERY );
449                 if ( xTransact.is() )
450                     xTransact->commit();
451 
452                 SvStream* pSrcStm = ::utl::UcbStreamHelper::CreateStream( aTempFile.GetURL(), STREAM_READ );
453                 if( pSrcStm )
454                 {
455                     rxOStm->SetBufferSize( 0xff00 );
456                     *rxOStm << *pSrcStm;
457                     delete pSrcStm;
458                 }
459 
460                 bRet = sal_True;
461 
462                 xWorkStore->dispose();
463                 xWorkStore = uno::Reference < embed::XStorage >();
464                 rxOStm->Commit();
465             }
466             break;
467 
468         default:
469             DBG_ERROR("unknown object id");
470     }
471     return bRet;
472 }
473 
474 void ScTransferObj::ObjectReleased()
475 {
476     ScModule* pScMod = SC_MOD();
477     if ( pScMod->GetClipData().pCellClipboard == this )
478         pScMod->SetClipObject( NULL, NULL );
479 
480     TransferableHelper::ObjectReleased();
481 }
482 
483 void ScTransferObj::DragFinished( sal_Int8 nDropAction )
484 {
485     if ( nDropAction == DND_ACTION_MOVE && !bDragWasInternal && !(nDragSourceFlags & SC_DROP_NAVIGATOR) )
486     {
487         //  move: delete source data
488         ScDocShell* pSourceSh = GetSourceDocShell();
489         if (pSourceSh)
490         {
491             ScMarkData aMarkData = GetSourceMarkData();
492             //  external drag&drop doesn't copy objects, so they also aren't deleted:
493             //  #105703# bApi=sal_True, don't show error messages from drag&drop
494             pSourceSh->GetDocFunc().DeleteContents( aMarkData, IDF_ALL & ~IDF_OBJECTS, sal_True, sal_True );
495         }
496     }
497 
498     ScModule* pScMod = SC_MOD();
499     if ( pScMod->GetDragData().pCellTransfer == this )
500         pScMod->ResetDragObject();
501 
502     xDragSourceRanges = NULL;       // don't keep source after dropping
503 
504     TransferableHelper::DragFinished( nDropAction );
505 }
506 
507 void ScTransferObj::SetDragHandlePos( SCCOL nX, SCROW nY )
508 {
509     nDragHandleX = nX;
510     nDragHandleY = nY;
511 }
512 
513 void ScTransferObj::SetVisibleTab( SCTAB nNew )
514 {
515     nVisibleTab = nNew;
516 }
517 
518 void ScTransferObj::SetDrawPersist( const SfxObjectShellRef& rRef )
519 {
520     aDrawPersistRef = rRef;
521 }
522 
523 void ScTransferObj::SetDragSource( ScDocShell* pSourceShell, const ScMarkData& rMark )
524 {
525     ScRangeList aRanges;
526     rMark.FillRangeListWithMarks( &aRanges, sal_False );
527     xDragSourceRanges = new ScCellRangesObj( pSourceShell, aRanges );
528 }
529 
530 void ScTransferObj::SetDragSourceFlags( sal_uInt16 nFlags )
531 {
532     nDragSourceFlags = nFlags;
533 }
534 
535 void ScTransferObj::SetDragWasInternal()
536 {
537     bDragWasInternal = sal_True;
538 }
539 
540 void ScTransferObj::SetUseInApi( bool bSet )
541 {
542     bUseInApi = bSet;
543 }
544 
545 ScDocument* ScTransferObj::GetSourceDocument()
546 {
547     ScDocShell* pSourceDocSh = GetSourceDocShell();
548     if (pSourceDocSh)
549         return pSourceDocSh->GetDocument();
550     return NULL;
551 }
552 
553 ScDocShell* ScTransferObj::GetSourceDocShell()
554 {
555     ScCellRangesBase* pRangesObj = ScCellRangesBase::getImplementation( xDragSourceRanges );
556     if (pRangesObj)
557         return pRangesObj->GetDocShell();
558 
559     return NULL;    // none set
560 }
561 
562 ScMarkData ScTransferObj::GetSourceMarkData()
563 {
564     ScMarkData aMarkData;
565     ScCellRangesBase* pRangesObj = ScCellRangesBase::getImplementation( xDragSourceRanges );
566     if (pRangesObj)
567     {
568         const ScRangeList& rRanges = pRangesObj->GetRangeList();
569         aMarkData.MarkFromRangeList( rRanges, sal_False );
570     }
571     return aMarkData;
572 }
573 
574 //
575 //  initialize aDocShellRef with a live document from the ClipDoc
576 //
577 
578 void ScTransferObj::InitDocShell()
579 {
580     if ( !aDocShellRef.Is() )
581     {
582         ScDocShell* pDocSh = new ScDocShell;
583         aDocShellRef = pDocSh;      // ref must be there before InitNew
584 
585         pDocSh->DoInitNew(NULL);
586 
587         ScDocument* pDestDoc = pDocSh->GetDocument();
588         ScMarkData aDestMark;
589         aDestMark.SelectTable( 0, sal_True );
590 
591         pDestDoc->SetDocOptions( pDoc->GetDocOptions() );   // #i42666#
592 
593         String aTabName;
594         pDoc->GetName( aBlock.aStart.Tab(), aTabName );
595         pDestDoc->RenameTab( 0, aTabName, sal_False );          // no UpdateRef (empty)
596 
597         pDestDoc->CopyStdStylesFrom( pDoc );
598 
599         SCCOL nStartX = aBlock.aStart.Col();
600         SCROW nStartY = aBlock.aStart.Row();
601         SCCOL nEndX = aBlock.aEnd.Col();
602         SCROW nEndY = aBlock.aEnd.Row();
603 
604         //  widths / heights
605         //  (must be copied before CopyFromClip, for drawing objects)
606 
607         SCCOL nCol, nLastCol;
608         SCTAB nSrcTab = aBlock.aStart.Tab();
609         pDestDoc->SetLayoutRTL(0, pDoc->IsLayoutRTL(nSrcTab));
610         for (nCol=nStartX; nCol<=nEndX; nCol++)
611             if ( pDoc->ColHidden(nCol, nSrcTab, nLastCol) )
612                 pDestDoc->ShowCol( nCol, 0, sal_False );
613             else
614                 pDestDoc->SetColWidth( nCol, 0, pDoc->GetColWidth( nCol, nSrcTab ) );
615 
616         ScBitMaskCompressedArray< SCROW, sal_uInt8> & rDestRowFlags =
617             pDestDoc->GetRowFlagsArrayModifiable(0);
618 
619         for (SCROW nRow = nStartY; nRow <= nEndY; ++nRow)
620         {
621             sal_uInt8 nSourceFlags = pDoc->GetRowFlags(nRow, nSrcTab);
622             SCROW nLastRow = -1;
623             if ( pDoc->RowHidden(nRow, nSrcTab, nLastRow) )
624                 pDestDoc->ShowRow( nRow, 0, sal_False );
625             else
626             {
627                 pDestDoc->SetRowHeight( nRow, 0, pDoc->GetOriginalHeight( nRow, nSrcTab ) );
628 
629                 //  if height was set manually, that flag has to be copied, too
630                 if ( nSourceFlags & CR_MANUALSIZE )
631                     rDestRowFlags.OrValue( nRow, CR_MANUALSIZE);
632             }
633         }
634 
635         if ( pDoc->GetDrawLayer() )
636             pDocSh->MakeDrawLayer();
637 
638         //  cell range is copied to the original position, but on the first sheet
639         //  -> bCutMode must be set
640         //  pDoc is always a Clipboard-document
641 
642         ScRange aDestRange( nStartX,nStartY,0, nEndX,nEndY,0 );
643         sal_Bool bWasCut = pDoc->IsCutMode();
644         if (!bWasCut)
645             pDoc->SetClipArea( aDestRange, sal_True );          // Cut
646         pDestDoc->CopyFromClip( aDestRange, aDestMark, IDF_ALL, NULL, pDoc, sal_False );
647         pDoc->SetClipArea( aDestRange, bWasCut );
648 
649         StripRefs( pDoc, nStartX,nStartY, nEndX,nEndY, pDestDoc, 0,0 );
650 
651         ScRange aMergeRange = aDestRange;
652         pDestDoc->ExtendMerge( aMergeRange, sal_True );
653 
654         pDoc->CopyDdeLinks( pDestDoc );         // copy values of DDE Links
655 
656         //  page format (grid etc) and page size (maximum size for ole object)
657 
658         Size aPaperSize = SvxPaperInfo::GetPaperSize( PAPER_A4 );       // Twips
659         ScStyleSheetPool* pStylePool = pDoc->GetStyleSheetPool();
660         String aStyleName = pDoc->GetPageStyle( aBlock.aStart.Tab() );
661         SfxStyleSheetBase* pStyleSheet = pStylePool->Find( aStyleName, SFX_STYLE_FAMILY_PAGE );
662         if (pStyleSheet)
663         {
664             const SfxItemSet& rSourceSet = pStyleSheet->GetItemSet();
665             aPaperSize = ((const SvxSizeItem&) rSourceSet.Get(ATTR_PAGE_SIZE)).GetSize();
666 
667             //  CopyStyleFrom kopiert SetItems mit richtigem Pool
668             ScStyleSheetPool* pDestPool = pDestDoc->GetStyleSheetPool();
669             pDestPool->CopyStyleFrom( pStylePool, aStyleName, SFX_STYLE_FAMILY_PAGE );
670         }
671 
672         ScViewData aViewData( pDocSh, NULL );
673         aViewData.SetScreen( nStartX,nStartY, nEndX,nEndY );
674         aViewData.SetCurX( nStartX );
675         aViewData.SetCurY( nStartY );
676 
677         pDestDoc->SetViewOptions( pDoc->GetViewOptions() );
678 
679         //      Size
680         //! get while copying sizes
681 
682         long nPosX = 0;
683         long nPosY = 0;
684 
685         for (nCol=0; nCol<nStartX; nCol++)
686             nPosX += pDestDoc->GetColWidth( nCol, 0 );
687         nPosY += pDestDoc->GetRowHeight( 0, nStartY-1, 0 );
688         nPosX = (long) ( nPosX * HMM_PER_TWIPS );
689         nPosY = (long) ( nPosY * HMM_PER_TWIPS );
690 
691 
692         aPaperSize.Width()  *= 2;       // limit OLE object to double of page size
693         aPaperSize.Height() *= 2;
694 
695         long nSizeX = 0;
696         long nSizeY = 0;
697         for (nCol=nStartX; nCol<=nEndX; nCol++)
698         {
699             long nAdd = pDestDoc->GetColWidth( nCol, 0 );
700             if ( nSizeX+nAdd > aPaperSize.Width() && nSizeX )   // above limit?
701                 break;
702             nSizeX += nAdd;
703         }
704         for (SCROW nRow=nStartY; nRow<=nEndY; nRow++)
705         {
706             long nAdd = pDestDoc->GetRowHeight( nRow, 0 );
707             if ( nSizeY+nAdd > aPaperSize.Height() && nSizeY )  // above limit?
708                 break;
709             nSizeY += nAdd;
710         }
711         nSizeX = (long) ( nSizeX * HMM_PER_TWIPS );
712         nSizeY = (long) ( nSizeY * HMM_PER_TWIPS );
713 
714 //      pDocSh->SetVisAreaSize( Size(nSizeX,nSizeY) );
715 
716         Rectangle aNewArea( Point(nPosX,nPosY), Size(nSizeX,nSizeY) );
717         //TODO/LATER: why twice?!
718         //pDocSh->SvInPlaceObject::SetVisArea( aNewArea );
719         pDocSh->SetVisArea( aNewArea );
720 
721         pDocSh->UpdateOle(&aViewData, sal_True);
722 
723         //! SetDocumentModified?
724         if ( pDestDoc->IsChartListenerCollectionNeedsUpdate() )
725             pDestDoc->UpdateChartListenerCollection();
726     }
727 }
728 
729 //  static
730 SfxObjectShell* ScTransferObj::SetDrawClipDoc( sal_Bool bAnyOle )
731 {
732     // update ScGlobal::pDrawClipDocShellRef
733 
734     delete ScGlobal::pDrawClipDocShellRef;
735     if (bAnyOle)
736     {
737         ScGlobal::pDrawClipDocShellRef =
738                         new ScDocShellRef(new ScDocShell(SFX_CREATE_MODE_INTERNAL));      // there must be a ref
739         (*ScGlobal::pDrawClipDocShellRef)->DoInitNew(NULL);
740         return *ScGlobal::pDrawClipDocShellRef;
741     }
742     else
743     {
744         ScGlobal::pDrawClipDocShellRef = NULL;
745         return NULL;
746     }
747 }
748 
749 //  static
750 void ScTransferObj::StripRefs( ScDocument* pDoc,
751                     SCCOL nStartX, SCROW nStartY, SCCOL nEndX, SCROW nEndY,
752                     ScDocument* pDestDoc, SCCOL nSubX, SCROW nSubY )
753 {
754     if (!pDestDoc)
755     {
756         pDestDoc = pDoc;
757         DBG_ASSERT(nSubX==0&&nSubY==0, "can't move within the document");
758     }
759 
760     //  In a clipboard doc the data don't have to be on the first sheet
761 
762     SCTAB nSrcTab = 0;
763     while (nSrcTab<MAXTAB && !pDoc->HasTable(nSrcTab))
764         ++nSrcTab;
765     SCTAB nDestTab = 0;
766     while (nDestTab<MAXTAB && !pDestDoc->HasTable(nDestTab))
767         ++nDestTab;
768 
769     if (!pDoc->HasTable(nSrcTab) || !pDestDoc->HasTable(nDestTab))
770     {
771         DBG_ERROR("Sheet not found in ScTransferObj::StripRefs");
772         return;
773     }
774 
775     SvNumberFormatter* pFormatter = pDoc->GetFormatTable();
776     ScRange aRef;
777 
778     ScCellIterator aIter( pDoc, nStartX, nStartY, nSrcTab, nEndX, nEndY, nSrcTab );
779     ScBaseCell* pCell = aIter.GetFirst();
780     while (pCell)
781     {
782         if (pCell->GetCellType() == CELLTYPE_FORMULA)
783         {
784             ScFormulaCell* pFCell = (ScFormulaCell*) pCell;
785             sal_Bool bOut = sal_False;
786             ScDetectiveRefIter aRefIter( pFCell );
787             while ( !bOut && aRefIter.GetNextRef( aRef ) )
788             {
789                 if ( aRef.aStart.Tab() != nSrcTab || aRef.aEnd.Tab() != nSrcTab ||
790                         aRef.aStart.Col() < nStartX || aRef.aEnd.Col() > nEndX ||
791                         aRef.aStart.Row() < nStartY || aRef.aEnd.Row() > nEndY )
792                     bOut = sal_True;
793             }
794             if (bOut)
795             {
796                 SCCOL nCol = aIter.GetCol() - nSubX;
797                 SCROW nRow = aIter.GetRow() - nSubY;
798 
799                 ScBaseCell* pNew = 0;
800                 sal_uInt16 nErrCode = pFCell->GetErrCode();
801                 if (nErrCode)
802                 {
803                     pNew = new ScStringCell( ScGlobal::GetErrorString(nErrCode) );
804                     if ( ((const SvxHorJustifyItem*) pDestDoc->GetAttr(
805                             nCol,nRow,nDestTab, ATTR_HOR_JUSTIFY))->GetValue() ==
806                             SVX_HOR_JUSTIFY_STANDARD )
807                         pDestDoc->ApplyAttr( nCol,nRow,nDestTab,
808                                 SvxHorJustifyItem(SVX_HOR_JUSTIFY_RIGHT, ATTR_HOR_JUSTIFY) );
809                 }
810                 else if (pFCell->IsValue())
811                 {
812                     double fVal = pFCell->GetValue();
813                     pNew = new ScValueCell( fVal );
814                 }
815                 else
816                 {
817                     String aStr;
818                     pFCell->GetString(aStr);
819                     if ( pFCell->IsMultilineResult() )
820                         pNew = new ScEditCell( aStr, pDestDoc );
821                     else
822                         pNew = new ScStringCell( aStr );
823                 }
824                 pDestDoc->PutCell( nCol,nRow,nDestTab, pNew );
825 
826                 //  number formats
827 
828                 sal_uLong nOldFormat = ((const SfxUInt32Item*)
829                                 pDestDoc->GetAttr(nCol,nRow,nDestTab, ATTR_VALUE_FORMAT))->GetValue();
830                 if ( (nOldFormat % SV_COUNTRY_LANGUAGE_OFFSET) == 0 )
831                 {
832                     sal_uLong nNewFormat = pFCell->GetStandardFormat( *pFormatter,
833                         nOldFormat );
834                     pDestDoc->ApplyAttr( nCol,nRow,nDestTab,
835                                 SfxUInt32Item(ATTR_VALUE_FORMAT, nNewFormat) );
836                 }
837             }
838         }
839         pCell = aIter.GetNext();
840     }
841 }
842 
843 const com::sun::star::uno::Sequence< sal_Int8 >& ScTransferObj::getUnoTunnelId()
844 {
845     static com::sun::star::uno::Sequence< sal_Int8 > aSeq;
846     if( !aSeq.getLength() )
847     {
848         static osl::Mutex           aCreateMutex;
849         osl::Guard< osl::Mutex >    aGuard( aCreateMutex );
850         aSeq.realloc( 16 );
851         rtl_createUuid( reinterpret_cast< sal_uInt8* >( aSeq.getArray() ), 0, sal_True );
852     }
853     return aSeq;
854 }
855 
856 sal_Int64 SAL_CALL ScTransferObj::getSomething( const com::sun::star::uno::Sequence< sal_Int8 >& rId ) throw( com::sun::star::uno::RuntimeException )
857 {
858     sal_Int64 nRet;
859     if( ( rId.getLength() == 16 ) &&
860         ( 0 == rtl_compareMemory( getUnoTunnelId().getConstArray(), rId.getConstArray(), 16 ) ) )
861     {
862         nRet = reinterpret_cast< sal_Int64 >( this );
863     }
864     else
865         nRet = TransferableHelper::getSomething(rId);
866     return nRet;
867 }
868 
869 
870