fix baseline build (old cairo) - 'cairo_rectangle_int_t' does not name a type
[LibreOffice.git] / sc / source / ui / view / viewfun4.cxx
blobe58267b4d06549c196aecbba59a3eba320292f62
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include "scitems.hxx"
21 #include <editeng/eeitem.hxx>
23 #include <editeng/editobj.hxx>
24 #include <editeng/editstat.hxx>
25 #include <editeng/editview.hxx>
26 #include <editeng/flditem.hxx>
27 #include <sot/storage.hxx>
28 #include <svx/hlnkitem.hxx>
29 #include <editeng/langitem.hxx>
30 #include <svx/svxerr.hxx>
31 #include <editeng/unolingu.hxx>
33 #include <sfx2/bindings.hxx>
34 #include <sfx2/dispatch.hxx>
35 #include <sfx2/docfile.hxx>
36 #include <sfx2/fcontnr.hxx>
37 #include <svtools/langtab.hxx>
38 #include <vcl/graphicfilter.hxx>
39 #include <svl/stritem.hxx>
40 #include <svtools/transfer.hxx>
41 #include <svl/urlbmk.hxx>
42 #include <svl/sharedstringpool.hxx>
43 #include <vcl/msgbox.hxx>
44 #include <avmedia/mediawindow.hxx>
46 #include <comphelper/storagehelper.hxx>
47 #include <comphelper/processfactory.hxx>
49 #include "viewfunc.hxx"
50 #include "docsh.hxx"
51 #include "document.hxx"
52 #include "docpool.hxx"
53 #include "globstr.hrc"
54 #include "global.hxx"
55 #include "undoblk.hxx"
56 #include "undocell.hxx"
57 #include "formulacell.hxx"
58 #include "scmod.hxx"
59 #include "spelleng.hxx"
60 #include "patattr.hxx"
61 #include "sc.hrc"
62 #include "tabvwsh.hxx"
63 #include "impex.hxx"
64 #include "editutil.hxx"
65 #include "editable.hxx"
66 #include "dociter.hxx"
67 #include "reffind.hxx"
68 #include "compiler.hxx"
69 #include "tokenarray.hxx"
70 #include <refupdatecontext.hxx>
71 #include <gridwin.hxx>
73 #include <boost/scoped_ptr.hpp>
75 using namespace com::sun::star;
77 // STATIC DATA -----------------------------------------------------------
79 bool bPasteIsDrop = false;
81 void ScViewFunc::PasteRTF( SCCOL nStartCol, SCROW nStartRow,
82 const ::com::sun::star::uno::Reference<
83 ::com::sun::star::datatransfer::XTransferable >& rxTransferable )
85 TransferableDataHelper aDataHelper( rxTransferable );
86 if ( aDataHelper.HasFormat( SotClipboardFormatId::EDITENGINE ) )
88 HideAllCursors();
90 ScDocShell* pDocSh = GetViewData().GetDocShell();
91 ScDocument& rDoc = pDocSh->GetDocument();
92 SCTAB nTab = GetViewData().GetTabNo();
93 const bool bRecord (rDoc.IsUndoEnabled());
95 const ScPatternAttr* pPattern = rDoc.GetPattern( nStartCol, nStartRow, nTab );
96 boost::scoped_ptr<ScTabEditEngine> pEngine(new ScTabEditEngine( *pPattern, rDoc.GetEnginePool() ));
97 pEngine->EnableUndo( false );
99 vcl::Window* pActWin = GetActiveWin();
100 if (pActWin)
102 pEngine->SetPaperSize(Size(100000,100000));
103 ScopedVclPtrInstance< vcl::Window > aWin( pActWin );
104 EditView aEditView( pEngine.get(), aWin.get() );
105 aEditView.SetOutputArea(Rectangle(0,0,100000,100000));
107 // same method now for clipboard or drag&drop
108 // mba: clipboard always must contain absolute URLs (could be from alien source)
109 aEditView.InsertText( rxTransferable, OUString(), true );
112 sal_Int32 nParCnt = pEngine->GetParagraphCount();
113 if (nParCnt)
115 SCROW nEndRow = nStartRow + static_cast<SCROW>(nParCnt) - 1;
116 if (nEndRow > MAXROW)
117 nEndRow = MAXROW;
119 ScDocument* pUndoDoc = NULL;
120 if (bRecord)
122 pUndoDoc = new ScDocument( SCDOCMODE_UNDO );
123 pUndoDoc->InitUndo( &rDoc, nTab, nTab );
124 rDoc.CopyToDocument( nStartCol,nStartRow,nTab, nStartCol,nEndRow,nTab, IDF_ALL, false, pUndoDoc );
127 SCROW nRow = nStartRow;
129 // Temporarily turn off undo generation for this lot
130 bool bUndoEnabled = rDoc.IsUndoEnabled();
131 rDoc.EnableUndo( false );
132 for( sal_Int32 n = 0; n < nParCnt; n++ )
134 boost::scoped_ptr<EditTextObject> pObject(pEngine->CreateTextObject(n));
135 EnterData(nStartCol, nRow, nTab, *pObject, true);
136 if( ++nRow > MAXROW )
137 break;
139 rDoc.EnableUndo(bUndoEnabled);
141 if (bRecord)
143 ScDocument* pRedoDoc = new ScDocument( SCDOCMODE_UNDO );
144 pRedoDoc->InitUndo( &rDoc, nTab, nTab );
145 rDoc.CopyToDocument( nStartCol,nStartRow,nTab, nStartCol,nEndRow,nTab, IDF_ALL|IDF_NOCAPTIONS, false, pRedoDoc );
147 ScRange aMarkRange(nStartCol, nStartRow, nTab, nStartCol, nEndRow, nTab);
148 ScMarkData aDestMark;
149 aDestMark.SetMarkArea( aMarkRange );
150 pDocSh->GetUndoManager()->AddUndoAction(
151 new ScUndoPaste( pDocSh, aMarkRange, aDestMark,
152 pUndoDoc, pRedoDoc, IDF_ALL, NULL));
156 pEngine.reset();
158 ShowAllCursors();
160 else
162 HideAllCursors();
163 ScDocShell* pDocSh = GetViewData().GetDocShell();
164 ScImportExport aImpEx( &pDocSh->GetDocument(),
165 ScAddress( nStartCol, nStartRow, GetViewData().GetTabNo() ) );
167 OUString aStr;
168 tools::SvRef<SotStorageStream> xStream;
169 if ( aDataHelper.GetSotStorageStream( SotClipboardFormatId::RTF, xStream ) && xStream.Is() )
170 // mba: clipboard always must contain absolute URLs (could be from alien source)
171 aImpEx.ImportStream( *xStream, OUString(), SotClipboardFormatId::RTF );
172 else if ( aDataHelper.GetString( SotClipboardFormatId::RTF, aStr ) )
173 aImpEx.ImportString( aStr, SotClipboardFormatId::RTF );
175 AdjustRowHeight( nStartRow, aImpEx.GetRange().aEnd.Row() );
176 pDocSh->UpdateOle(&GetViewData());
177 ShowAllCursors();
180 void ScViewFunc::DoRefConversion( bool bRecord )
182 ScDocument* pDoc = GetViewData().GetDocument();
183 ScMarkData& rMark = GetViewData().GetMarkData();
184 SCTAB nTabCount = pDoc->GetTableCount();
185 if (bRecord && !pDoc->IsUndoEnabled())
186 bRecord = false;
188 ScRange aMarkRange;
189 rMark.MarkToSimple();
190 bool bMulti = rMark.IsMultiMarked();
191 if (bMulti)
192 rMark.GetMultiMarkArea( aMarkRange );
193 else if (rMark.IsMarked())
194 rMark.GetMarkArea( aMarkRange );
195 else
197 aMarkRange = ScRange( GetViewData().GetCurX(),
198 GetViewData().GetCurY(), GetViewData().GetTabNo() );
200 ScEditableTester aTester( pDoc, aMarkRange.aStart.Col(), aMarkRange.aStart.Row(),
201 aMarkRange.aEnd.Col(), aMarkRange.aEnd.Row(),rMark );
202 if (!aTester.IsEditable())
204 ErrorMessage(aTester.GetMessageId());
205 return;
208 ScDocShell* pDocSh = GetViewData().GetDocShell();
209 bool bOk = false;
211 ScDocument* pUndoDoc = NULL;
212 if (bRecord)
214 pUndoDoc = new ScDocument( SCDOCMODE_UNDO );
215 SCTAB nTab = aMarkRange.aStart.Tab();
216 pUndoDoc->InitUndo( pDoc, nTab, nTab );
218 if ( rMark.GetSelectCount() > 1 )
220 ScMarkData::iterator itr = rMark.begin(), itrEnd = rMark.end();
221 for (; itr != itrEnd; ++itr)
222 if ( *itr != nTab )
223 pUndoDoc->AddUndoTab( *itr, *itr );
225 ScRange aCopyRange = aMarkRange;
226 aCopyRange.aStart.SetTab(0);
227 aCopyRange.aEnd.SetTab(nTabCount-1);
228 pDoc->CopyToDocument( aCopyRange, IDF_ALL, bMulti, pUndoDoc, &rMark );
231 ScRangeListRef xRanges;
232 GetViewData().GetMultiArea( xRanges );
233 size_t nCount = xRanges->size();
235 ScMarkData::iterator itr = rMark.begin(), itrEnd = rMark.end();
236 for (; itr != itrEnd; ++itr)
238 SCTAB i = *itr;
239 for (size_t j = 0; j < nCount; ++j)
241 ScRange aRange = *(*xRanges)[j];
242 aRange.aStart.SetTab(i);
243 aRange.aEnd.SetTab(i);
244 ScCellIterator aIter( pDoc, aRange );
245 for (bool bHas = aIter.first(); bHas; bHas = aIter.next())
247 if (aIter.getType() != CELLTYPE_FORMULA)
248 continue;
250 ScFormulaCell* pCell = aIter.getFormulaCell();
251 OUString aOld;
252 pCell->GetFormula(aOld);
253 sal_Int32 nLen = aOld.getLength();
254 ScRefFinder aFinder( aOld, aIter.GetPos(), pDoc, pDoc->GetAddressConvention() );
255 aFinder.ToggleRel( 0, nLen );
256 if (aFinder.GetFound())
258 ScAddress aPos = pCell->aPos;
259 OUString aNew = aFinder.GetText();
260 ScCompiler aComp( pDoc, aPos);
261 aComp.SetGrammar(pDoc->GetGrammar());
262 boost::scoped_ptr<ScTokenArray> pArr(aComp.CompileString(aNew));
263 ScFormulaCell* pNewCell =
264 new ScFormulaCell(
265 pDoc, aPos, *pArr, formula::FormulaGrammar::GRAM_DEFAULT, MM_NONE);
267 pDoc->SetFormulaCell(aPos, pNewCell);
268 bOk = true;
273 if (bRecord)
275 ScDocument* pRedoDoc = new ScDocument( SCDOCMODE_UNDO );
276 SCTAB nTab = aMarkRange.aStart.Tab();
277 pRedoDoc->InitUndo( pDoc, nTab, nTab );
279 if ( rMark.GetSelectCount() > 1 )
281 itr = rMark.begin();
282 for (; itr != itrEnd; ++itr)
283 if ( *itr != nTab )
284 pRedoDoc->AddUndoTab( *itr, *itr );
286 ScRange aCopyRange = aMarkRange;
287 aCopyRange.aStart.SetTab(0);
288 aCopyRange.aEnd.SetTab(nTabCount-1);
289 pDoc->CopyToDocument( aCopyRange, IDF_ALL, bMulti, pRedoDoc, &rMark );
291 pDocSh->GetUndoManager()->AddUndoAction(
292 new ScUndoRefConversion( pDocSh,
293 aMarkRange, rMark, pUndoDoc, pRedoDoc, bMulti, IDF_ALL) );
296 pDocSh->PostPaint( aMarkRange, PAINT_GRID );
297 pDocSh->UpdateOle(&GetViewData());
298 pDocSh->SetDocumentModified();
299 CellContentChanged();
301 if (!bOk)
302 ErrorMessage(STR_ERR_NOREF);
304 // Thesaurus - Undo ok
305 void ScViewFunc::DoThesaurus( bool bRecord )
307 SCCOL nCol;
308 SCROW nRow;
309 SCTAB nTab;
310 ScDocShell* pDocSh = GetViewData().GetDocShell();
311 ScDocument& rDoc = pDocSh->GetDocument();
312 ScMarkData& rMark = GetViewData().GetMarkData();
313 ScSplitPos eWhich = GetViewData().GetActivePart();
314 EESpellState eState;
315 EditView* pEditView = NULL;
316 boost::scoped_ptr<ESelection> pEditSel;
317 boost::scoped_ptr<ScEditEngineDefaulter> pThesaurusEngine;
318 bool bIsEditMode = GetViewData().HasEditView(eWhich);
319 if (bRecord && !rDoc.IsUndoEnabled())
320 bRecord = false;
321 if (bIsEditMode) // Edit-Mode aktiv
323 GetViewData().GetEditView(eWhich, pEditView, nCol, nRow);
324 pEditSel.reset(new ESelection(pEditView->GetSelection()));
325 SC_MOD()->InputEnterHandler();
326 GetViewData().GetBindings().Update(); // sonst kommt der Sfx durcheinander...
328 else
330 nCol = GetViewData().GetCurX();
331 nRow = GetViewData().GetCurY();
333 nTab = GetViewData().GetTabNo();
335 ScAddress aPos(nCol, nRow, nTab);
336 ScEditableTester aTester( &rDoc, nCol, nRow, nCol, nRow, rMark );
337 if (!aTester.IsEditable())
339 ErrorMessage(aTester.GetMessageId());
340 return;
343 ScCellValue aOldText;
344 aOldText.assign(rDoc, aPos);
345 if (aOldText.meType != CELLTYPE_STRING && aOldText.meType != CELLTYPE_EDIT)
347 ErrorMessage(STR_THESAURUS_NO_STRING);
348 return;
351 uno::Reference<linguistic2::XSpellChecker1> xSpeller = LinguMgr::GetSpellChecker();
353 pThesaurusEngine.reset(new ScEditEngineDefaulter(rDoc.GetEnginePool()));
354 pThesaurusEngine->SetEditTextObjectPool( rDoc.GetEditPool() );
355 pThesaurusEngine->SetRefDevice(GetViewData().GetActiveWin());
356 pThesaurusEngine->SetSpeller(xSpeller);
357 MakeEditView(pThesaurusEngine.get(), nCol, nRow );
358 const ScPatternAttr* pPattern = NULL;
359 boost::scoped_ptr<SfxItemSet> pEditDefaults(
360 new SfxItemSet(pThesaurusEngine->GetEmptyItemSet()));
361 pPattern = rDoc.GetPattern(nCol, nRow, nTab);
362 if (pPattern)
364 pPattern->FillEditItemSet( pEditDefaults.get() );
365 pThesaurusEngine->SetDefaults( *pEditDefaults );
368 if (aOldText.meType == CELLTYPE_EDIT)
369 pThesaurusEngine->SetText(*aOldText.mpEditText);
370 else
371 pThesaurusEngine->SetText(aOldText.getString(&rDoc));
373 pEditView = GetViewData().GetEditView(GetViewData().GetActivePart());
374 if (pEditSel)
375 pEditView->SetSelection(*pEditSel);
376 else
377 pEditView->SetSelection(ESelection(0,0,0,0));
379 pThesaurusEngine->ClearModifyFlag();
381 // language is now in EditEngine attributes -> no longer passed to StartThesaurus
383 eState = pEditView->StartThesaurus();
384 OSL_ENSURE(eState != EE_SPELL_NOSPELLER, "No SpellChecker");
386 if (eState == EE_SPELL_ERRORFOUND) // sollte spaeter durch Wrapper geschehen!
388 LanguageType eLnge = ScViewUtil::GetEffLanguage( &rDoc, ScAddress( nCol, nRow, nTab ) );
389 OUString aErr = SvtLanguageTable::GetLanguageString(eLnge);
390 aErr += ScGlobal::GetRscString( STR_SPELLING_NO_LANG );
391 ScopedVclPtrInstance< InfoBox > aBox( GetViewData().GetDialogParent(), aErr );
392 aBox->Execute();
394 if (pThesaurusEngine->IsModified())
396 ScCellValue aNewText;
398 if (aOldText.meType == CELLTYPE_EDIT)
400 // The cell will own the text object instance.
401 EditTextObject* pText = pThesaurusEngine->CreateTextObject();
402 if (rDoc.SetEditText(ScAddress(nCol,nRow,nTab), pText))
403 aNewText.set(*pText);
405 else
407 OUString aStr = pThesaurusEngine->GetText();
408 aNewText.set(rDoc.GetSharedStringPool().intern(aStr));
409 rDoc.SetString(nCol, nRow, nTab, aStr);
412 pDocSh->SetDocumentModified();
413 if (bRecord)
415 GetViewData().GetDocShell()->GetUndoManager()->AddUndoAction(
416 new ScUndoThesaurus(
417 GetViewData().GetDocShell(), nCol, nRow, nTab, aOldText, aNewText));
421 KillEditView(true);
422 pDocSh->PostPaintGridAll();
425 void ScViewFunc::DoHangulHanjaConversion( bool bRecord )
427 ScConversionParam aConvParam( SC_CONVERSION_HANGULHANJA, LANGUAGE_KOREAN, 0, true );
428 DoSheetConversion( aConvParam, bRecord );
431 void ScViewFunc::DoSheetConversion( const ScConversionParam& rConvParam, bool bRecord )
433 SCCOL nCol;
434 SCROW nRow;
435 SCTAB nTab;
436 ScViewData& rViewData = GetViewData();
437 ScDocShell* pDocSh = rViewData.GetDocShell();
438 ScDocument& rDoc = pDocSh->GetDocument();
439 ScMarkData& rMark = rViewData.GetMarkData();
440 ScSplitPos eWhich = rViewData.GetActivePart();
441 EditView* pEditView = NULL;
442 bool bIsEditMode = rViewData.HasEditView(eWhich);
443 if (bRecord && !rDoc.IsUndoEnabled())
444 bRecord = false;
445 if (bIsEditMode) // Edit-Mode aktiv
447 rViewData.GetEditView(eWhich, pEditView, nCol, nRow);
448 SC_MOD()->InputEnterHandler();
450 else
452 nCol = rViewData.GetCurX();
453 nRow = rViewData.GetCurY();
455 AlignToCursor( nCol, nRow, SC_FOLLOW_JUMP);
457 nTab = rViewData.GetTabNo();
459 rMark.MarkToMulti();
460 bool bMarked = rMark.IsMultiMarked();
461 if (bMarked)
463 ScEditableTester aTester( &rDoc, rMark );
464 if (!aTester.IsEditable())
466 ErrorMessage(aTester.GetMessageId());
467 return;
471 ScDocument* pUndoDoc = NULL;
472 ScDocument* pRedoDoc = NULL;
473 if (bRecord)
475 pUndoDoc = new ScDocument( SCDOCMODE_UNDO );
476 pUndoDoc->InitUndo( &rDoc, nTab, nTab );
477 pRedoDoc = new ScDocument( SCDOCMODE_UNDO );
478 pRedoDoc->InitUndo( &rDoc, nTab, nTab );
480 if ( rMark.GetSelectCount() > 1 )
482 ScMarkData::iterator itr = rMark.begin(), itrEnd = rMark.end();
483 for (; itr != itrEnd; ++itr)
484 if ( *itr != nTab )
486 pUndoDoc->AddUndoTab( *itr, *itr );
487 pRedoDoc->AddUndoTab( *itr, *itr );
492 // ab hier kein return mehr
494 bool bOldEnabled = rDoc.IsIdleEnabled();
495 rDoc.EnableIdle(false); // stop online spelling
497 // *** create and init the edit engine *** --------------------------------
499 boost::scoped_ptr<ScConversionEngineBase> pEngine;
500 switch( rConvParam.GetType() )
502 case SC_CONVERSION_SPELLCHECK:
503 pEngine.reset(new ScSpellingEngine(
504 rDoc.GetEnginePool(), rViewData, pUndoDoc, pRedoDoc, LinguMgr::GetSpellChecker() ));
505 break;
506 case SC_CONVERSION_HANGULHANJA:
507 case SC_CONVERSION_CHINESE_TRANSL:
508 pEngine.reset(new ScTextConversionEngine(
509 rDoc.GetEnginePool(), rViewData, rConvParam, pUndoDoc, pRedoDoc ));
510 break;
511 default:
512 OSL_FAIL( "ScViewFunc::DoSheetConversion - unknown conversion type" );
515 MakeEditView( pEngine.get(), nCol, nRow );
516 pEngine->SetRefDevice( rViewData.GetActiveWin() );
517 // dummy Zelle simulieren:
518 pEditView = rViewData.GetEditView( rViewData.GetActivePart() );
519 rViewData.SetSpellingView( pEditView );
520 Rectangle aRect( Point( 0, 0 ), Point( 0, 0 ) );
521 pEditView->SetOutputArea( aRect );
522 pEngine->SetControlWord( EEControlBits::USECHARATTRIBS );
523 pEngine->EnableUndo( false );
524 pEngine->SetPaperSize( aRect.GetSize() );
525 pEngine->SetText( EMPTY_OUSTRING );
527 // *** do the conversion *** ----------------------------------------------
529 pEngine->ClearModifyFlag();
530 pEngine->ConvertAll( *pEditView );
532 // *** undo/redo *** ------------------------------------------------------
534 if( pEngine->IsAnyModified() )
536 if (bRecord)
538 SCCOL nNewCol = rViewData.GetCurX();
539 SCROW nNewRow = rViewData.GetCurY();
540 rViewData.GetDocShell()->GetUndoManager()->AddUndoAction(
541 new ScUndoConversion(
542 pDocSh, rMark,
543 nCol, nRow, nTab, pUndoDoc,
544 nNewCol, nNewRow, nTab, pRedoDoc, rConvParam ) );
547 sc::SetFormulaDirtyContext aCxt;
548 rDoc.SetAllFormulasDirty(aCxt);
550 pDocSh->SetDocumentModified();
552 else
554 delete pUndoDoc;
555 delete pRedoDoc;
558 // *** final cleanup *** --------------------------------------------------
560 rViewData.SetSpellingView( NULL );
561 KillEditView(true);
562 pEngine.reset();
563 pDocSh->PostPaintGridAll();
564 rViewData.GetViewShell()->UpdateInputHandler();
565 rDoc.EnableIdle(bOldEnabled);
568 // Pasten von SotClipboardFormatId::FILE-Items
569 // wird nicht direkt aus Drop aufgerufen, sondern asynchron -> Dialoge sind erlaubt
571 bool ScViewFunc::PasteFile( const Point& rPos, const OUString& rFile, bool bLink )
573 INetURLObject aURL;
574 aURL.SetSmartURL( rFile );
575 OUString aStrURL = aURL.GetMainURL( INetURLObject::NO_DECODE );
577 // is it a media URL?
578 if( ::avmedia::MediaWindow::isMediaURL( aStrURL, ""/*TODO?*/ ) )
580 const SfxStringItem aMediaURLItem( SID_INSERT_AVMEDIA, aStrURL );
581 return ( 0 != GetViewData().GetDispatcher().Execute(
582 SID_INSERT_AVMEDIA, SfxCallMode::SYNCHRON,
583 &aMediaURLItem, 0L ) );
586 if (!bLink) // bei bLink nur Grafik oder URL
588 // 1. Kann ich die Datei oeffnen?
589 const SfxFilter* pFlt = NULL;
591 // nur nach eigenen Filtern suchen, ohne Auswahlbox (wie in ScDocumentLoader)
592 SfxFilterMatcher aMatcher( ScDocShell::Factory().GetFilterContainer()->GetName() );
593 SfxMedium aSfxMedium( aStrURL, (StreamMode::READ | StreamMode::SHARE_DENYNONE) );
594 // #i73992# GuessFilter no longer calls UseInteractionHandler.
595 // This is UI, so it can be called here.
596 aSfxMedium.UseInteractionHandler(true);
597 ErrCode nErr = aMatcher.GuessFilter( aSfxMedium, &pFlt );
599 if ( pFlt && !nErr )
601 // Code aus dem SFX geklaut!
602 SfxDispatcher &rDispatcher = GetViewData().GetDispatcher();
603 SfxStringItem aFileNameItem( SID_FILE_NAME, aStrURL );
604 SfxStringItem aFilterItem( SID_FILTER_NAME, pFlt->GetName() );
605 // #i69524# add target, as in SfxApplication when the Open dialog is used
606 SfxStringItem aTargetItem( SID_TARGETNAME, OUString("_default") );
608 // Asynchron oeffnen, kann naemlich auch aus D&D heraus passieren
609 // und das bekommt dem MAC nicht so gut ...
610 return ( 0 != rDispatcher.Execute( SID_OPENDOC,
611 SfxCallMode::ASYNCHRON, &aFileNameItem, &aFilterItem, &aTargetItem, 0L) );
615 // 2. Kann die Datei ueber die Grafik-Filter eingefuegt werden?
616 // (als Link, weil Gallery das so anbietet)
618 sal_uInt16 nFilterFormat;
619 Graphic aGraphic;
620 GraphicFilter& rGraphicFilter = GraphicFilter::GetGraphicFilter();
622 if (!rGraphicFilter.ImportGraphic(aGraphic, aURL,
623 GRFILTER_FORMAT_DONTKNOW, &nFilterFormat ))
625 if ( bLink )
627 OUString aFltName = rGraphicFilter.GetImportFormatName(nFilterFormat);
628 return PasteGraphic( rPos, aGraphic, aStrURL, aFltName );
630 else
632 // #i76709# if bLink isn't set, pass empty URL/filter, so a non-linked image is inserted
633 return PasteGraphic( rPos, aGraphic, EMPTY_OUSTRING, EMPTY_OUSTRING );
637 if (bLink) // bei bLink alles, was nicht Grafik ist, als URL
639 Rectangle aRect( rPos, Size(0,0) );
640 ScRange aRange = GetViewData().GetDocument()->
641 GetRange( GetViewData().GetTabNo(), aRect );
642 SCCOL nPosX = aRange.aStart.Col();
643 SCROW nPosY = aRange.aStart.Row();
645 InsertBookmark( aStrURL, aStrURL, nPosX, nPosY );
646 return true;
648 else
650 // 3. Kann die Datei als OLE eingefuegt werden?
651 // auch nicht-Storages, z.B. Sounds (#38282#)
652 uno::Reference < embed::XStorage > xStorage = comphelper::OStorageHelper::GetTemporaryStorage();
654 //TODO/LATER: what about "bLink"?
656 uno::Sequence < beans::PropertyValue > aMedium(1);
657 aMedium[0].Name = "URL";
658 aMedium[0].Value <<= OUString( aStrURL );
660 comphelper::EmbeddedObjectContainer aCnt( xStorage );
661 OUString aName;
662 uno::Reference < embed::XEmbeddedObject > xObj = aCnt.InsertEmbeddedObject( aMedium, aName );
663 if( xObj.is() )
664 return PasteObject( rPos, xObj );
666 // If an OLE object can't be created, insert a URL button
668 GetViewData().GetViewShell()->InsertURLButton( aStrURL, aStrURL, EMPTY_OUSTRING, &rPos );
669 return true;
673 bool ScViewFunc::PasteBookmark( SotClipboardFormatId nFormatId,
674 const ::com::sun::star::uno::Reference<
675 ::com::sun::star::datatransfer::XTransferable >& rxTransferable,
676 SCCOL nPosX, SCROW nPosY )
678 INetBookmark aBookmark;
679 TransferableDataHelper aDataHelper( rxTransferable );
680 if ( !aDataHelper.GetINetBookmark( nFormatId, aBookmark ) )
681 return false;
683 InsertBookmark( aBookmark.GetDescription(), aBookmark.GetURL(), nPosX, nPosY );
684 return true;
687 void ScViewFunc::InsertBookmark( const OUString& rDescription, const OUString& rURL,
688 SCCOL nPosX, SCROW nPosY, const OUString* pTarget,
689 bool bTryReplace )
691 ScViewData& rViewData = GetViewData();
692 if ( rViewData.HasEditView( rViewData.GetActivePart() ) &&
693 nPosX >= rViewData.GetEditStartCol() && nPosX <= rViewData.GetEditEndCol() &&
694 nPosY >= rViewData.GetEditStartRow() && nPosY <= rViewData.GetEditEndRow() )
696 // in die gerade editierte Zelle einfuegen
698 OUString aTargetFrame;
699 if (pTarget)
700 aTargetFrame = *pTarget;
701 rViewData.GetViewShell()->InsertURLField( rDescription, rURL, aTargetFrame );
702 return;
705 // in nicht editierte Zelle einfuegen
707 ScDocument* pDoc = GetViewData().GetDocument();
708 SCTAB nTab = GetViewData().GetTabNo();
709 ScAddress aCellPos( nPosX, nPosY, nTab );
710 EditEngine aEngine( pDoc->GetEnginePool() );
712 const EditTextObject* pOld = pDoc->GetEditText(aCellPos);
713 if (pOld)
714 aEngine.SetText(*pOld);
715 else
717 OUString aOld;
718 pDoc->GetInputString(nPosX, nPosY, nTab, aOld);
719 if (!aOld.isEmpty())
720 aEngine.SetText(aOld);
723 sal_Int32 nPara = aEngine.GetParagraphCount();
724 if (nPara)
725 --nPara;
726 sal_Int32 nTxtLen = aEngine.GetTextLen(nPara);
727 ESelection aInsSel( nPara, nTxtLen, nPara, nTxtLen );
729 if ( bTryReplace && HasBookmarkAtCursor( NULL ) )
731 // if called from hyperlink slot and cell contains only a URL,
732 // replace old URL with new one
734 aInsSel = ESelection( 0, 0, 0, 1 ); // replace first character (field)
737 SvxURLField aField( rURL, rDescription, SVXURLFORMAT_APPDEFAULT );
738 if (pTarget)
739 aField.SetTargetFrame(*pTarget);
740 aEngine.QuickInsertField( SvxFieldItem( aField, EE_FEATURE_FIELD ), aInsSel );
742 boost::scoped_ptr<EditTextObject> pData(aEngine.CreateTextObject());
743 EnterData(nPosX, nPosY, nTab, *pData);
746 bool ScViewFunc::HasBookmarkAtCursor( SvxHyperlinkItem* pContent )
748 ScAddress aPos( GetViewData().GetCurX(), GetViewData().GetCurY(), GetViewData().GetTabNo() );
749 ScDocument& rDoc = GetViewData().GetDocShell()->GetDocument();
751 const EditTextObject* pData = rDoc.GetEditText(aPos);
752 if (!pData)
753 return false;
755 if (!pData->IsFieldObject())
756 // not a field object.
757 return false;
759 const SvxFieldItem* pFieldItem = pData->GetField();
760 if (!pFieldItem)
761 // doesn't have a field item.
762 return false;
764 const SvxFieldData* pField = pFieldItem->GetField();
765 if (!pField)
766 // doesn't have a field item data.
767 return false;
769 if (pField->GetClassId() != com::sun::star::text::textfield::Type::URL)
770 // not a URL field.
771 return false;
773 if (pContent)
775 const SvxURLField* pURLField = static_cast<const SvxURLField*>(pField);
776 pContent->SetName( pURLField->GetRepresentation() );
777 pContent->SetURL( pURLField->GetURL() );
778 pContent->SetTargetFrame( pURLField->GetTargetFrame() );
780 return true;
783 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */