xref: /trunk/main/sc/source/ui/view/viewfun4.cxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_sc.hxx"
30 
31 
32 
33 // INCLUDE ---------------------------------------------------------------
34 
35 #include "scitems.hxx"
36 #include <editeng/eeitem.hxx>
37 
38 #include <editeng/editobj.hxx>
39 #include <editeng/editstat.hxx>
40 #include <editeng/editview.hxx>
41 #include <editeng/flditem.hxx>
42 #include <svx/hlnkitem.hxx>
43 #include <editeng/langitem.hxx>
44 #include <svx/svxerr.hxx>
45 #include <editeng/unolingu.hxx>
46 
47 #include <sfx2/bindings.hxx>
48 #include <sfx2/dispatch.hxx>
49 #include <sfx2/docfile.hxx>
50 #include <sfx2/fcontnr.hxx>
51 #include <svtools/langtab.hxx>
52 #include <svtools/filter.hxx>
53 #include <svl/stritem.hxx>
54 #include <svtools/transfer.hxx>
55 #include <svl/urlbmk.hxx>
56 #include <vcl/msgbox.hxx>
57 #include <avmedia/mediawindow.hxx>
58 
59 #include <comphelper/storagehelper.hxx>
60 #include <comphelper/processfactory.hxx>
61 
62 #include "viewfunc.hxx"
63 #include "docsh.hxx"
64 #include "docsh.hxx"
65 #include "document.hxx"
66 #include "docpool.hxx"
67 #include "globstr.hrc"
68 #include "global.hxx"
69 #include "undoblk.hxx"
70 #include "undocell.hxx"
71 #include "cell.hxx"
72 #include "scmod.hxx"
73 #include "spelleng.hxx"
74 #include "patattr.hxx"
75 #include "sc.hrc"
76 #include "tabvwsh.hxx"
77 #include "impex.hxx"
78 #include "editutil.hxx"
79 #include "editable.hxx"
80 #include "dociter.hxx"
81 #include "reffind.hxx"
82 #include "compiler.hxx"
83 
84 using namespace com::sun::star;
85 
86 // STATIC DATA -----------------------------------------------------------
87 
88 sal_Bool bPasteIsDrop = sal_False;
89 
90 //==================================================================
91 
92 void ScViewFunc::PasteRTF( SCCOL nStartCol, SCROW nStartRow,
93 								const ::com::sun::star::uno::Reference<
94 									::com::sun::star::datatransfer::XTransferable >& rxTransferable )
95 {
96 	TransferableDataHelper aDataHelper( rxTransferable );
97 	if ( aDataHelper.HasFormat( SOT_FORMATSTR_ID_EDITENGINE ) )
98 	{
99 		HideAllCursors();
100 
101 		ScDocument* pUndoDoc = NULL;
102 
103 		ScDocShell* pDocSh = GetViewData()->GetDocShell();
104 		ScDocument* pDoc = pDocSh->GetDocument();
105 		SCTAB nTab = GetViewData()->GetTabNo();
106 		const sal_Bool bRecord (pDoc->IsUndoEnabled());
107 
108 		const ScPatternAttr* pPattern = pDoc->GetPattern( nStartCol, nStartRow, nTab );
109 		ScTabEditEngine* pEngine = new ScTabEditEngine( *pPattern, pDoc->GetEnginePool() );
110 		pEngine->EnableUndo( sal_False );
111 
112 		Window* pActWin = GetActiveWin();
113 		if (pActWin)
114 		{
115 			pEngine->SetPaperSize(Size(100000,100000));
116 			Window aWin( pActWin );
117 			EditView aEditView( pEngine, &aWin );
118 			aEditView.SetOutputArea(Rectangle(0,0,100000,100000));
119 
120 			// same method now for clipboard or drag&drop
121             // mba: clipboard always must contain absolute URLs (could be from alien source)
122             aEditView.InsertText( rxTransferable, String(), sal_True );
123 		}
124 
125 		sal_uLong nParCnt = pEngine->GetParagraphCount();
126 		if (nParCnt)
127 		{
128 			SCROW nEndRow = nStartRow + static_cast<SCROW>(nParCnt) - 1;
129 			if (nEndRow > MAXROW)
130 				nEndRow = MAXROW;
131 
132 			if (bRecord)
133 			{
134 				pUndoDoc = new ScDocument( SCDOCMODE_UNDO );
135 				pUndoDoc->InitUndo( pDoc, nTab, nTab );
136 				pDoc->CopyToDocument( nStartCol,nStartRow,nTab, nStartCol,nEndRow,nTab, IDF_ALL, sal_False, pUndoDoc );
137 			}
138 
139 			SCROW nRow = nStartRow;
140 			for( sal_uInt16 n = 0; n < nParCnt; n++ )
141 			{
142 				EditTextObject* pObject = pEngine->CreateTextObject( n );
143 				EnterData( nStartCol, nRow, nTab, pObject, sal_False, sal_True );
144 							// kein Undo, auf einfache Strings testen
145 				delete pObject;
146 				if( ++nRow > MAXROW )
147 					break;
148 			}
149 
150 			if (bRecord)
151 			{
152 				ScDocument* pRedoDoc = new ScDocument( SCDOCMODE_UNDO );
153 				pRedoDoc->InitUndo( pDoc, nTab, nTab );
154                 pDoc->CopyToDocument( nStartCol,nStartRow,nTab, nStartCol,nEndRow,nTab, IDF_ALL|IDF_NOCAPTIONS, sal_False, pRedoDoc );
155 
156 				ScMarkData aDestMark;
157 				aDestMark.SelectOneTable( nTab );
158 				pDocSh->GetUndoManager()->AddUndoAction(
159 					new ScUndoPaste( pDocSh, nStartCol,nStartRow,nTab, nStartCol,nEndRow,nTab,
160 										aDestMark,
161 										pUndoDoc, pRedoDoc, IDF_ALL, NULL,NULL,NULL,NULL ) );
162 			}
163 		}
164 
165 		delete pEngine;
166 
167 		ShowAllCursors();
168 	}
169 	else
170 	{
171 		HideAllCursors();
172 		ScDocShell* pDocSh = GetViewData()->GetDocShell();
173 		ScImportExport aImpEx( pDocSh->GetDocument(),
174 			ScAddress( nStartCol, nStartRow, GetViewData()->GetTabNo() ) );
175 
176         ::rtl::OUString aStr;
177 		SotStorageStreamRef xStream;
178 		if ( aDataHelper.GetSotStorageStream( SOT_FORMAT_RTF, xStream ) && xStream.Is() )
179             // mba: clipboard always must contain absolute URLs (could be from alien source)
180             aImpEx.ImportStream( *xStream, String(), SOT_FORMAT_RTF );
181 		else if ( aDataHelper.GetString( SOT_FORMAT_RTF, aStr ) )
182 			aImpEx.ImportString( aStr, SOT_FORMAT_RTF );
183 
184 		AdjustRowHeight( nStartRow, aImpEx.GetRange().aEnd.Row() );
185 		pDocSh->UpdateOle(GetViewData());
186 		ShowAllCursors();
187 	}
188 }
189 void ScViewFunc::DoRefConversion( sal_Bool bRecord )
190 {
191     ScDocument* pDoc = GetViewData()->GetDocument();
192     ScMarkData& rMark = GetViewData()->GetMarkData();
193     SCTAB nTabCount = pDoc->GetTableCount();
194     if (bRecord && !pDoc->IsUndoEnabled())
195         bRecord = sal_False;
196 
197     ScRange aMarkRange;
198     rMark.MarkToSimple();
199     sal_Bool bMulti = rMark.IsMultiMarked();
200     if (bMulti)
201         rMark.GetMultiMarkArea( aMarkRange );
202     else if (rMark.IsMarked())
203         rMark.GetMarkArea( aMarkRange );
204     else
205     {
206         aMarkRange = ScRange( GetViewData()->GetCurX(),
207             GetViewData()->GetCurY(), GetViewData()->GetTabNo() );
208     }
209     ScEditableTester aTester( pDoc, aMarkRange.aStart.Col(), aMarkRange.aStart.Row(),
210                             aMarkRange.aEnd.Col(), aMarkRange.aEnd.Row(),rMark );
211     if (!aTester.IsEditable())
212     {
213         ErrorMessage(aTester.GetMessageId());
214         return;
215     }
216 
217     ScDocShell* pDocSh = GetViewData()->GetDocShell();
218     sal_Bool bOk = sal_False;
219 
220     ScDocument* pUndoDoc = NULL;
221     if (bRecord)
222     {
223         pUndoDoc = new ScDocument( SCDOCMODE_UNDO );
224         SCTAB nTab = aMarkRange.aStart.Tab();
225         pUndoDoc->InitUndo( pDoc, nTab, nTab );
226 
227         if ( rMark.GetSelectCount() > 1 )
228         {
229             for (SCTAB i=0; i<nTabCount; i++)
230                 if ( rMark.GetTableSelect(i) && i != nTab )
231                     pUndoDoc->AddUndoTab( i, i );
232         }
233         ScRange aCopyRange = aMarkRange;
234         aCopyRange.aStart.SetTab(0);
235         aCopyRange.aEnd.SetTab(nTabCount-1);
236         pDoc->CopyToDocument( aCopyRange, IDF_ALL, bMulti, pUndoDoc, &rMark );
237     }
238 
239     ScRangeListRef xRanges;
240     GetViewData()->GetMultiArea( xRanges );
241     sal_uLong nCount = xRanges->Count();
242 
243     for (SCTAB i=0; i<nTabCount; i++)
244     {
245         if (rMark.GetTableSelect(i))
246         {
247             for (sal_uLong j=0; j<nCount; j++)
248             {
249                 ScRange aRange = *xRanges->GetObject(j);
250                 aRange.aStart.SetTab(i);
251                 aRange.aEnd.SetTab(i);
252                 ScCellIterator aIter( pDoc, aRange );
253                 ScBaseCell* pCell = aIter.GetFirst();
254                 while ( pCell )
255                 {
256                     if (pCell->GetCellType() == CELLTYPE_FORMULA)
257                     {
258                         String aOld;
259                         ((ScFormulaCell*)pCell)->GetFormula(aOld);
260                         xub_StrLen nLen = aOld.Len();
261                         ScRefFinder aFinder( aOld, pDoc );
262                         aFinder.ToggleRel( 0, nLen );
263                         if (aFinder.GetFound())
264                         {
265                             ScAddress aPos = ((ScFormulaCell*)pCell)->aPos;
266                             String aNew = aFinder.GetText();
267                             ScCompiler aComp( pDoc, aPos);
268                             aComp.SetGrammar(pDoc->GetGrammar());
269                             ScTokenArray* pArr = aComp.CompileString( aNew );
270                             ScFormulaCell* pNewCell = new ScFormulaCell( pDoc, aPos,
271                                                         pArr,formula::FormulaGrammar::GRAM_DEFAULT, MM_NONE );
272                             pDoc->PutCell( aPos, pNewCell );
273                             bOk = sal_True;
274                         }
275                     }
276                     pCell = aIter.GetNext();
277                 }
278             }
279         }
280     }
281     if (bRecord)
282     {
283         ScDocument* pRedoDoc = new ScDocument( SCDOCMODE_UNDO );
284         SCTAB nTab = aMarkRange.aStart.Tab();
285         pRedoDoc->InitUndo( pDoc, nTab, nTab );
286 
287         if ( rMark.GetSelectCount() > 1 )
288         {
289             for (SCTAB i=0; i<nTabCount; i++)
290                 if ( rMark.GetTableSelect(i) && i != nTab )
291                     pRedoDoc->AddUndoTab( i, i );
292         }
293         ScRange aCopyRange = aMarkRange;
294         aCopyRange.aStart.SetTab(0);
295         aCopyRange.aEnd.SetTab(nTabCount-1);
296         pDoc->CopyToDocument( aCopyRange, IDF_ALL, bMulti, pRedoDoc, &rMark );
297 
298         pDocSh->GetUndoManager()->AddUndoAction(
299             new ScUndoRefConversion( pDocSh,
300                                     aMarkRange, rMark, pUndoDoc, pRedoDoc, bMulti, IDF_ALL) );
301     }
302 
303     pDocSh->PostPaint( aMarkRange, PAINT_GRID );
304     pDocSh->UpdateOle(GetViewData());
305     pDocSh->SetDocumentModified();
306     CellContentChanged();
307 
308     if (!bOk)
309         ErrorMessage(STR_ERR_NOREF);
310 }
311 //	Thesaurus - Undo ok
312 void ScViewFunc::DoThesaurus( sal_Bool bRecord )
313 {
314 	SCCOL nCol;
315 	SCROW nRow;
316 	SCTAB nTab;
317 	ScDocShell* pDocSh = GetViewData()->GetDocShell();
318 	ScDocument* pDoc = pDocSh->GetDocument();
319 	ScMarkData& rMark = GetViewData()->GetMarkData();
320 	ScSplitPos eWhich = GetViewData()->GetActivePart();
321 	CellType eCellType;
322 	EESpellState eState;
323 	String sOldText, sNewString;
324 	EditTextObject* pOldTObj = NULL;
325 	const EditTextObject* pTObject = NULL;
326 	ScBaseCell* pCell = NULL;
327 	EditView* pEditView = NULL;
328 	ESelection* pEditSel = NULL;
329 	ScEditEngineDefaulter* pThesaurusEngine;
330 	sal_Bool bIsEditMode = GetViewData()->HasEditView(eWhich);
331 	if (bRecord && !pDoc->IsUndoEnabled())
332 		bRecord = sal_False;
333 	if (bIsEditMode)											// Edit-Mode aktiv
334 	{
335 		GetViewData()->GetEditView(eWhich, pEditView, nCol, nRow);
336 		pEditSel = new ESelection(pEditView->GetSelection());
337 		SC_MOD()->InputEnterHandler();
338 		GetViewData()->GetBindings().Update();			// sonst kommt der Sfx durcheinander...
339 	}
340 	else
341 	{
342 		nCol = GetViewData()->GetCurX();
343 		nRow = GetViewData()->GetCurY();
344 	}
345 	nTab = GetViewData()->GetTabNo();
346 
347 	ScEditableTester aTester( pDoc, nCol, nRow, nCol, nRow, rMark );
348 	if (!aTester.IsEditable())
349 	{
350 		ErrorMessage(aTester.GetMessageId());
351 		delete pEditSel;
352 		return;
353 	}
354 	pDoc->GetCellType(nCol, nRow, nTab, eCellType);
355 	if (eCellType != CELLTYPE_STRING && eCellType != CELLTYPE_EDIT)
356 	{
357 		ErrorMessage(STR_THESAURUS_NO_STRING);
358 		return;
359 	}
360 
361 	com::sun::star::uno::Reference<com::sun::star::linguistic2::XSpellChecker1>
362 										xSpeller = LinguMgr::GetSpellChecker();
363 	//!	if (...)  // thesaurus not available
364 	//!	{
365 	//!		ErrorMessage(STR_EXPORT_ASCII_WARNING);
366 	//!		delete pEditSel;
367 	//!		return;
368 	//!	}
369 
370 	pThesaurusEngine = new ScEditEngineDefaulter( pDoc->GetEnginePool() );
371 	pThesaurusEngine->SetEditTextObjectPool( pDoc->GetEditPool() );
372 	pThesaurusEngine->SetRefDevice(GetViewData()->GetActiveWin());
373 	pThesaurusEngine->SetSpeller(xSpeller);
374 	MakeEditView(pThesaurusEngine, nCol, nRow );
375 	const ScPatternAttr* pPattern = NULL;
376 	SfxItemSet*	pEditDefaults = new SfxItemSet(pThesaurusEngine->GetEmptyItemSet());
377 	pPattern = pDoc->GetPattern(nCol, nRow, nTab);
378 	if (pPattern )
379 	{
380 		pPattern->FillEditItemSet( pEditDefaults );
381 		pThesaurusEngine->SetDefaults( *pEditDefaults );
382 	}
383 
384 	if (eCellType == CELLTYPE_STRING)
385 	{
386 		pDoc->GetString(nCol, nRow, nTab, sOldText);
387 		pThesaurusEngine->SetText(sOldText);
388 	}
389 	else if (eCellType == CELLTYPE_EDIT)
390 	{
391 		pDoc->GetCell(nCol, nRow, nTab, pCell);
392 		if (pCell)
393 		{
394 			((ScEditCell*) pCell)->GetData(pTObject);
395 			if (pTObject)
396 			{
397 				pOldTObj = pTObject->Clone();
398 				pThesaurusEngine->SetText(*pTObject);
399 			}
400 		}
401 	}
402 	else
403 	{
404 		DBG_ERROR("DoThesaurus: Keine String oder Editzelle");
405 	}
406 	pEditView = GetViewData()->GetEditView(GetViewData()->GetActivePart());;
407 	if (pEditSel)
408 		pEditView->SetSelection(*pEditSel);
409 	else
410 		pEditView->SetSelection(ESelection(0,0,0,0));
411 
412 	pThesaurusEngine->ClearModifyFlag();
413 
414 	//	language is now in EditEngine attributes -> no longer passed to StartThesaurus
415 
416 	eState = pEditView->StartThesaurus();
417 	DBG_ASSERT(eState != EE_SPELL_NOSPELLER, "No SpellChecker");
418 
419 	if (eState == EE_SPELL_ERRORFOUND)				// sollte spaeter durch Wrapper geschehen!
420 	{
421 		LanguageType eLnge = ScViewUtil::GetEffLanguage( pDoc, ScAddress( nCol, nRow, nTab ) );
422 		SvtLanguageTable aLangTab;
423 		String aErr = aLangTab.GetString(eLnge);
424 		aErr += ScGlobal::GetRscString( STR_SPELLING_NO_LANG );
425 		InfoBox aBox( GetViewData()->GetDialogParent(), aErr );
426 		aBox.Execute();
427 	}
428 	if (pThesaurusEngine->IsModified())
429 	{
430 		EditTextObject* pNewTObj = NULL;
431 		if (pCell && pTObject)
432 		{
433 			pNewTObj = pThesaurusEngine->CreateTextObject();
434 			pCell = new ScEditCell( pNewTObj, pDoc,
435 				pThesaurusEngine->GetEditTextObjectPool() );
436 			pDoc->PutCell( nCol, nRow, nTab, pCell );
437 		}
438 		else
439 		{
440 			sNewString = pThesaurusEngine->GetText();
441 			pDoc->SetString(nCol, nRow, nTab, sNewString);
442 		}
443 // erack! it's broadcasted
444 //		pDoc->SetDirty();
445 		pDocSh->SetDocumentModified();
446 		if (bRecord)
447 		{
448 			GetViewData()->GetDocShell()->GetUndoManager()->AddUndoAction(
449 				new ScUndoThesaurus( GetViewData()->GetDocShell(),
450 									 nCol, nRow, nTab,
451 									 sOldText, pOldTObj, sNewString, pNewTObj));
452 		}
453 		delete pNewTObj;
454 	}
455 	KillEditView(sal_True);
456 	delete pEditDefaults;
457 	delete pThesaurusEngine;
458 	delete pOldTObj;
459 	delete pEditSel;
460 	pDocSh->PostPaintGridAll();
461 }
462 
463 //UNUSED2008-05  //	Spelling Checker - Undo ok
464 //UNUSED2008-05  void ScViewFunc::DoSpellingChecker( sal_Bool bRecord )
465 //UNUSED2008-05  {
466 //UNUSED2008-05      DoSheetConversion( ScConversionParam( SC_CONVERSION_SPELLCHECK ), bRecord );
467 //UNUSED2008-05  }
468 
469 void ScViewFunc::DoHangulHanjaConversion( sal_Bool bRecord )
470 {
471     ScConversionParam aConvParam( SC_CONVERSION_HANGULHANJA, LANGUAGE_KOREAN, 0, true );
472     DoSheetConversion( aConvParam, bRecord );
473 }
474 
475 void ScViewFunc::DoSheetConversion( const ScConversionParam& rConvParam, sal_Bool bRecord )
476 {
477     SCCOL nCol;
478     SCROW nRow;
479     SCTAB nTab;
480     ScViewData& rViewData = *GetViewData();
481     ScDocShell* pDocSh = rViewData.GetDocShell();
482     ScDocument* pDoc = pDocSh->GetDocument();
483     ScMarkData& rMark = rViewData.GetMarkData();
484     ScSplitPos eWhich = rViewData.GetActivePart();
485     EditView* pEditView = NULL;
486     ESelection* pEditSel = NULL;
487     sal_Bool bIsEditMode = rViewData.HasEditView(eWhich);
488     if (bRecord && !pDoc->IsUndoEnabled())
489         bRecord = sal_False;
490     if (bIsEditMode)                                            // Edit-Mode aktiv
491     {
492         rViewData.GetEditView(eWhich, pEditView, nCol, nRow);
493         pEditSel = new ESelection(pEditView->GetSelection());
494         SC_MOD()->InputEnterHandler();
495     }
496     else
497     {
498         nCol = rViewData.GetCurX();
499         nRow = rViewData.GetCurY();
500 
501         AlignToCursor( nCol, nRow, SC_FOLLOW_JUMP);
502     }
503     nTab = rViewData.GetTabNo();
504 
505     rMark.MarkToMulti();
506     sal_Bool bMarked = rMark.IsMultiMarked();
507     if (bMarked)
508     {
509         ScEditableTester aTester( pDoc, rMark );
510         if (!aTester.IsEditable())
511         {
512             ErrorMessage(aTester.GetMessageId());
513             delete pEditSel;
514             return;
515         }
516     }
517 
518     ScDocument* pUndoDoc = NULL;
519     ScDocument* pRedoDoc = NULL;
520     if (bRecord)
521     {
522         pUndoDoc = new ScDocument( SCDOCMODE_UNDO );
523         pUndoDoc->InitUndo( pDoc, nTab, nTab );
524         pRedoDoc = new ScDocument( SCDOCMODE_UNDO );
525         pRedoDoc->InitUndo( pDoc, nTab, nTab );
526 
527         if ( rMark.GetSelectCount() > 1 )
528         {
529             SCTAB nTabCount = pDoc->GetTableCount();
530             for (SCTAB i=0; i<nTabCount; i++)
531                 if ( rMark.GetTableSelect(i) && i != nTab )
532                 {
533                     pUndoDoc->AddUndoTab( i, i );
534                     pRedoDoc->AddUndoTab( i, i );
535                 }
536         }
537     }
538 
539     //  ab hier kein return mehr
540 
541     sal_Bool bOldDis = pDoc->IsIdleDisabled();
542     pDoc->DisableIdle( sal_True );   // #42726# stop online spelling
543 
544     // *** create and init the edit engine *** --------------------------------
545 
546     ScConversionEngineBase* pEngine = NULL;
547     switch( rConvParam.GetType() )
548     {
549         case SC_CONVERSION_SPELLCHECK:
550             pEngine = new ScSpellingEngine(
551                 pDoc->GetEnginePool(), rViewData, pUndoDoc, pRedoDoc, LinguMgr::GetSpellChecker() );
552         break;
553         case SC_CONVERSION_HANGULHANJA:
554         case SC_CONVERSION_CHINESE_TRANSL:
555             pEngine = new ScTextConversionEngine(
556                 pDoc->GetEnginePool(), rViewData, rConvParam, pUndoDoc, pRedoDoc );
557         break;
558         default:
559             DBG_ERRORFILE( "ScViewFunc::DoSheetConversion - unknown conversion type" );
560     }
561 
562     MakeEditView( pEngine, nCol, nRow );
563     pEngine->SetRefDevice( rViewData.GetActiveWin() );
564                                         // dummy Zelle simulieren:
565     pEditView = rViewData.GetEditView( rViewData.GetActivePart() );
566     rViewData.SetSpellingView( pEditView );
567     Rectangle aRect( Point( 0, 0 ), Point( 0, 0 ) );
568     pEditView->SetOutputArea( aRect );
569     pEngine->SetControlWord( EE_CNTRL_USECHARATTRIBS );
570     pEngine->EnableUndo( sal_False );
571     pEngine->SetPaperSize( aRect.GetSize() );
572     pEngine->SetText( EMPTY_STRING );
573 
574     // *** do the conversion *** ----------------------------------------------
575 
576     pEngine->ClearModifyFlag();
577     pEngine->ConvertAll( *pEditView );
578 
579     // *** undo/redo *** ------------------------------------------------------
580 
581     if( pEngine->IsAnyModified() )
582     {
583         if (bRecord)
584         {
585             SCCOL nNewCol = rViewData.GetCurX();
586             SCROW nNewRow = rViewData.GetCurY();
587             rViewData.GetDocShell()->GetUndoManager()->AddUndoAction(
588                 new ScUndoConversion(
589                         pDocSh, rMark,
590                         nCol, nRow, nTab, pUndoDoc,
591                         nNewCol, nNewRow, nTab, pRedoDoc, rConvParam ) );
592         }
593         pDoc->SetDirty();
594         pDocSh->SetDocumentModified();
595     }
596     else
597     {
598         delete pUndoDoc;
599         delete pRedoDoc;
600     }
601 
602     // *** final cleanup *** --------------------------------------------------
603 
604     rViewData.SetSpellingView( NULL );
605     KillEditView(sal_True);
606     delete pEngine;
607     delete pEditSel;
608     pDocSh->PostPaintGridAll();
609     rViewData.GetViewShell()->UpdateInputHandler();
610     pDoc->DisableIdle(bOldDis);
611 }
612 
613 
614 //UNUSED2008-05  IMPL_LINK_INLINE_START( ScViewFunc, SpellError, void *, nLang )
615 //UNUSED2008-05  {
616 //UNUSED2008-05      SvtLanguageTable aLangTab;
617 //UNUSED2008-05      String aErr = aLangTab.GetString((LanguageType) (sal_uLong) nLang);
618 //UNUSED2008-05      ErrorHandler::HandleError(*new StringErrorInfo(
619 //UNUSED2008-05                                  ERRCODE_SVX_LINGU_LANGUAGENOTEXISTS, aErr) );
620 //UNUSED2008-05
621 //UNUSED2008-05      return 0;
622 //UNUSED2008-05  }
623 //UNUSED2008-05  IMPL_LINK_INLINE_END( ScViewFunc, SpellError, void *, nLang )
624 
625 // Pasten von FORMAT_FILE-Items
626 //	wird nicht direkt aus Drop aufgerufen, sondern asynchron -> Dialoge sind erlaubt
627 
628 sal_Bool ScViewFunc::PasteFile( const Point& rPos, const String& rFile, sal_Bool bLink )
629 {
630 	INetURLObject aURL;
631 	aURL.SetSmartURL( rFile );
632 	String aStrURL = aURL.GetMainURL( INetURLObject::NO_DECODE );
633 
634     // is it a media URL?
635     if( ::avmedia::MediaWindow::isMediaURL( aStrURL ) )
636     {
637         const SfxStringItem aMediaURLItem( SID_INSERT_AVMEDIA, aStrURL );
638         return sal_Bool( 0 != GetViewData()->GetDispatcher().Execute(
639                                 SID_INSERT_AVMEDIA, SFX_CALLMODE_SYNCHRON,
640                                 &aMediaURLItem, 0L ) );
641     }
642 
643 	if (!bLink)		// bei bLink nur Grafik oder URL
644 	{
645 		// 1. Kann ich die Datei oeffnen?
646 		const SfxFilter* pFlt = NULL;
647 
648 		// nur nach eigenen Filtern suchen, ohne Auswahlbox (wie in ScDocumentLoader)
649 		SfxFilterMatcher aMatcher( ScDocShell::Factory().GetFilterContainer()->GetName() );
650 		SfxMedium aSfxMedium( aStrURL, (STREAM_READ | STREAM_SHARE_DENYNONE), sal_False );
651         // #i73992# GuessFilter no longer calls UseInteractionHandler.
652         // This is UI, so it can be called here.
653         aSfxMedium.UseInteractionHandler(sal_True);
654 		ErrCode nErr = aMatcher.GuessFilter( aSfxMedium, &pFlt );
655 
656 		if ( pFlt && !nErr )
657 		{
658 			// Code aus dem SFX geklaut!
659 			SfxDispatcher &rDispatcher = GetViewData()->GetDispatcher();
660 			SfxStringItem aFileNameItem( SID_FILE_NAME, aStrURL );
661 			SfxStringItem aFilterItem( SID_FILTER_NAME, pFlt->GetName() );
662             // #i69524# add target, as in SfxApplication when the Open dialog is used
663             SfxStringItem aTargetItem( SID_TARGETNAME, String::CreateFromAscii("_default") );
664 
665 			// Asynchron oeffnen, kann naemlich auch aus D&D heraus passieren
666 			// und das bekommt dem MAC nicht so gut ...
667 			return sal_Bool( 0 != rDispatcher.Execute( SID_OPENDOC,
668                                     SFX_CALLMODE_ASYNCHRON, &aFileNameItem, &aFilterItem, &aTargetItem, 0L) );
669 		}
670 	}
671 
672 	// 2. Kann die Datei ueber die Grafik-Filter eingefuegt werden?
673 	// (als Link, weil Gallery das so anbietet)
674 
675 	sal_uInt16 nFilterFormat;
676 	Graphic aGraphic;
677 	GraphicFilter* pGraphicFilter = GraphicFilter::GetGraphicFilter();
678 
679 //		GraphicProgress aGraphicProgress(&aGraphicFilter);
680 
681 	if (!pGraphicFilter->ImportGraphic(aGraphic, aURL,
682 			GRFILTER_FORMAT_DONTKNOW, &nFilterFormat ))
683 	{
684         if ( bLink )
685         {
686             String aFltName = pGraphicFilter->GetImportFormatName(nFilterFormat);
687             return PasteGraphic( rPos, aGraphic, aStrURL, aFltName );
688         }
689         else
690         {
691             // #i76709# if bLink isn't set, pass empty URL/filter, so a non-linked image is inserted
692             return PasteGraphic( rPos, aGraphic, EMPTY_STRING, EMPTY_STRING );
693         }
694 	}
695 
696 	if (bLink)						// bei bLink alles, was nicht Grafik ist, als URL
697 	{
698 		Rectangle aRect( rPos, Size(0,0) );
699 		ScRange aRange = GetViewData()->GetDocument()->
700 							GetRange( GetViewData()->GetTabNo(), aRect );
701 		SCCOL nPosX = aRange.aStart.Col();
702 		SCROW nPosY = aRange.aStart.Row();
703 
704 		InsertBookmark( aStrURL, aStrURL, nPosX, nPosY );
705 		return sal_True;
706 	}
707 	else
708 	{
709 		// 3. Kann die Datei als OLE eingefuegt werden?
710 		// auch nicht-Storages, z.B. Sounds (#38282#)
711         uno::Reference < embed::XStorage > xStorage = comphelper::OStorageHelper::GetTemporaryStorage();
712 
713         //TODO/LATER: what about "bLink"?
714 
715         uno::Sequence < beans::PropertyValue > aMedium(1);
716         aMedium[0].Name = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "URL" ) );
717         aMedium[0].Value <<= ::rtl::OUString( aStrURL );
718 
719         comphelper::EmbeddedObjectContainer aCnt( xStorage );
720         ::rtl::OUString aName;
721         uno::Reference < embed::XEmbeddedObject > xObj = aCnt.InsertEmbeddedObject( aMedium, aName );
722         if( xObj.is() )
723             return PasteObject( rPos, xObj );
724 
725 		// #105851# If an OLE object can't be created, insert a URL button
726 
727 		GetViewData()->GetViewShell()->InsertURLButton( aStrURL, aStrURL, EMPTY_STRING, &rPos );
728 		return sal_True;
729 	}
730 }
731 
732 sal_Bool ScViewFunc::PasteBookmark( sal_uLong nFormatId,
733 								const ::com::sun::star::uno::Reference<
734 									::com::sun::star::datatransfer::XTransferable >& rxTransferable,
735 								SCCOL nPosX, SCROW nPosY )
736 {
737 	INetBookmark aBookmark;
738 	TransferableDataHelper aDataHelper( rxTransferable );
739 	if ( !aDataHelper.GetINetBookmark( nFormatId, aBookmark ) )
740 		return sal_False;
741 
742 	InsertBookmark( aBookmark.GetDescription(), aBookmark.GetURL(), nPosX, nPosY );
743 	return sal_True;
744 }
745 
746 void ScViewFunc::InsertBookmark( const String& rDescription, const String& rURL,
747 									SCCOL nPosX, SCROW nPosY, const String* pTarget,
748 									sal_Bool bTryReplace )
749 {
750 	ScViewData* pViewData = GetViewData();
751 	if ( pViewData->HasEditView( pViewData->GetActivePart() ) &&
752 			nPosX >= pViewData->GetEditStartCol() && nPosX <= pViewData->GetEditEndCol() &&
753 			nPosY >= pViewData->GetEditStartRow() && nPosY <= pViewData->GetEditEndRow() )
754 	{
755 		//	in die gerade editierte Zelle einfuegen
756 
757 		String aTargetFrame;
758 		if (pTarget)
759 			aTargetFrame = *pTarget;
760 		pViewData->GetViewShell()->InsertURLField( rDescription, rURL, aTargetFrame );
761 		return;
762 	}
763 
764 	//	in nicht editierte Zelle einfuegen
765 
766 	ScDocument* pDoc = GetViewData()->GetDocument();
767 	SCTAB nTab = GetViewData()->GetTabNo();
768 	ScAddress aCellPos( nPosX, nPosY, nTab );
769 	ScBaseCell* pCell = pDoc->GetCell( aCellPos );
770 	EditEngine aEngine( pDoc->GetEnginePool() );
771 	if (pCell)
772 	{
773 		if (pCell->GetCellType() == CELLTYPE_EDIT)
774 		{
775 			const EditTextObject* pOld = ((ScEditCell*)pCell)->GetData();
776 			if (pOld)
777 				aEngine.SetText(*pOld);
778 		}
779 		else
780 		{
781 			String aOld;
782 			pDoc->GetInputString( nPosX, nPosY, nTab, aOld );
783 			if (aOld.Len())
784 				aEngine.SetText(aOld);
785 		}
786 	}
787 
788 	sal_uInt16 nPara = aEngine.GetParagraphCount();
789 	if (nPara)
790 		--nPara;
791 	xub_StrLen nTxtLen = aEngine.GetTextLen(nPara);
792 	ESelection aInsSel( nPara, nTxtLen, nPara, nTxtLen );
793 
794 	if ( bTryReplace && HasBookmarkAtCursor( NULL ) )
795 	{
796 		//	if called from hyperlink slot and cell contains only a URL,
797 		//	replace old URL with new one
798 
799 		aInsSel = ESelection( 0, 0, 0, 1 );		// replace first character (field)
800 	}
801 
802 	SvxURLField aField( rURL, rDescription, SVXURLFORMAT_APPDEFAULT );
803 	if (pTarget)
804 		aField.SetTargetFrame(*pTarget);
805     aEngine.QuickInsertField( SvxFieldItem( aField, EE_FEATURE_FIELD ), aInsSel );
806 
807 	EditTextObject* pData = aEngine.CreateTextObject();
808 	EnterData( nPosX, nPosY, nTab, pData );
809 	delete pData;
810 }
811 
812 sal_Bool ScViewFunc::HasBookmarkAtCursor( SvxHyperlinkItem* pContent )
813 {
814 	ScAddress aPos( GetViewData()->GetCurX(), GetViewData()->GetCurY(), GetViewData()->GetTabNo() );
815 	ScDocument* pDoc = GetViewData()->GetDocShell()->GetDocument();
816 
817 	ScBaseCell* pCell = pDoc->GetCell( aPos );
818 	if ( pCell && pCell->GetCellType() == CELLTYPE_EDIT )
819 	{
820 		const EditTextObject* pData = ((ScEditCell*)pCell)->GetData();
821 		if (pData)
822 		{
823 			sal_Bool bField = pData->IsFieldObject();
824 			if (bField)
825 			{
826 				const SvxFieldItem* pFieldItem = pData->GetField();
827 				if (pFieldItem)
828 				{
829 					const SvxFieldData* pField = pFieldItem->GetField();
830 					if ( pField && pField->ISA(SvxURLField) )
831 					{
832 						if (pContent)
833 						{
834 							const SvxURLField* pURLField = (const SvxURLField*)pField;
835 							pContent->SetName( pURLField->GetRepresentation() );
836 							pContent->SetURL( pURLField->GetURL() );
837 							pContent->SetTargetFrame( pURLField->GetTargetFrame() );
838 						}
839 						return sal_True;
840 					}
841 				}
842 			}
843 		}
844 	}
845 	return sal_False;
846 }
847 
848 
849